change tw based on context

15 views
Skip to first unread message

Josh Guffin

unread,
Nov 18, 2007, 12:40:16 AM11/18/07
to vim...@googlegroups.com
Hi,

I often use vim to write in TeX, and I'd like to set something up so
that the textwidth changes according to whether I'm in an equation
environment or not. For example, normal text would have tw=78, while
equations would have tw=4000.

This is so that large equations and tables can be written with manual
wrapping. Especially for tables, this would help quite a bit when
the number of columns gets large.

Any suggestions?

Thanks,

Josh

DervishD

unread,
Nov 18, 2007, 5:27:43 AM11/18/07
to vim...@googlegroups.com
Hi Josh :)

* Josh Guffin <guf...@uiuc.edu> dixit:


> I often use vim to write in TeX, and I'd like to set something up so
> that the textwidth changes according to whether I'm in an equation
> environment or not. For example, normal text would have tw=78, while
> equations would have tw=4000.

First thing that came to my mind is to use some autocommand, probably
for CursorMovedI event, examining the previous line and if it is the
mark that starts equations, switch tw=4000 OR switch off autoformatting
for that line. Since my TeX is somewhat rusty, could you post an example
with a couple of lines of normal TeX followed by a fragment of equation
TeX? Just to fine tune the autocommand (or to think about other
solutions).

Probably is there a much more powerful way of achieving the same, wait
for the gurus' answers :))

Raúl Núñez de Arenas Coronado

--
Linux Registered User 88736 | http://www.dervishd.net
It's my PC and I'll cry if I want to... RAmen!
We are waiting for 13 Feb 2009 23:31:30 +0000 ...

Ben Schmidt

unread,
Nov 18, 2007, 7:25:46 AM11/18/07
to vim...@googlegroups.com
>> I often use vim to write in TeX, and I'd like to set something up so
>> that the textwidth changes according to whether I'm in an equation
>> environment or not. For example, normal text would have tw=78, while
>> equations would have tw=4000.
>
> First thing that came to my mind is to use some autocommand, probably
> for CursorMovedI event, examining the previous line and if it is the
> mark that starts equations, switch tw=4000 OR switch off autoformatting
> for that line. Since my TeX is somewhat rusty, could you post an example
> with a couple of lines of normal TeX followed by a fragment of equation
> TeX? Just to fine tune the autocommand (or to think about other
> solutions).
>
> Probably is there a much more powerful way of achieving the same, wait
> for the gurus' answers :))

I also don't know enough TeX to be truly helpful...but...if you're using syntax
highlighting, one way to find out when you're in an equation might be to use the
synID() function. This might be more reliable than the searching lines option just
mentioned, especially since IIRC the same delimiter is used to mark the start of
an equation as is used to mark the end.

Ben.

Send instant messages to your online friends http://au.messenger.yahoo.com

Andy Wokula

unread,
Nov 18, 2007, 7:48:30 AM11/18/07
to vim...@googlegroups.com
Ben Schmidt schrieb:

Why not just

:setsyntax <texMathZoneX,texMathZoneY> textwidth=78

oops, sorry, this command doesn't exist yet

--
Andy

A.Politz

unread,
Nov 18, 2007, 8:11:14 AM11/18/07
to vim...@googlegroups.com
Josh Guffin wrote:


"-----------%<----------

func! s:InEquation()
"Funny indent because of google.
return
\searchpair('^\s*\\begin{equation','','^\s*\\end{equation','Wn')
\||
\searchpair('^\s*\\begin{eqnarray','','^\s*\\end{eqnarray','Wn')
endfun

au InsertLeave <buffer> setl tw=78
au InsertEnter <buffer> if s:InEquation() | setl tw=4000 | endif
"-----------%<----------

This changes the 'tw' when you enter insert mode, depending
on the environment and resets it afterwards. You would have
to store this somewhere in your 'ftplugin/tex.vim' file.

-ap

--
Ich hab geträumt, der Krieg wär vorbei.

thomas

unread,
Nov 18, 2007, 10:55:42 AM11/18/07
to vim_use
Hi,

I uploaded something that could be used for this. I created this
plugin because I'd like to have something similar for a slightly
different purpose and because I wanted to test another plugin of mine.
Maybe it meets your demands, maybe not.

http://www.vim.org/scripts/script.php?script_id=2076

Josh Guffin

unread,
Nov 18, 2007, 11:00:13 AM11/18/07
to vim...@googlegroups.com

This seems to be exactly what I'm looking for, but it isn't called
when I'm editing latex files: it is called when editing .vimrc, for
example. I think it may be that when I am in insert mode in a latex
file, the mode is XIM-Insert, not Insert. However, neither
XIMInsertEnter or XimInsertEnter are valid groups. Any suggestions
on how to fix this?

Thanks,

Josh

thomas

unread,
Nov 18, 2007, 11:01:50 AM11/18/07
to vim_use
Okay, I just realized it doesn't actually work because the syntax info
currently gets lost when the cursor is at the end of line. Very
premature upload. Sorry.

A.Politz

unread,
Nov 18, 2007, 1:15:29 PM11/18/07
to vim...@googlegroups.com
Josh Guffin wrote:

First, it would be better to put the autocommands in an agroup, like
suggested here ':h :augroup'.
Second, are you shure you have sourced the lines from the latex
file ? These commands are local to the buffer. For testing you
could replace the '<buffer>' with an asterisk, which would trigger
them in any buffer.
Third, I don't know nothing about XIM, so I can't really comment on
this.But it seems a little harsh to me,if this inputmethod would prevent
autocommands from executing.

Josh Guffin

unread,
Nov 18, 2007, 2:02:06 PM11/18/07
to vim...@googlegroups.com

Ok

> Second, are you shure you have sourced the lines from the latex
> file ? These commands are local to the buffer. For testing you
> could replace the '<buffer>' with an asterisk, which would trigger
> them in any buffer.

Ok, I've traced the problem to a question of tabs: if I open a tex
file from the command line, i.e. vi blah.tex, then the wrapping works
correctly. I have xdvi/vim set up as client-server, so that I can
click on any part of a dvi file, and it will open up the source file
in vi. When I'm editing a document which has several files included
(i.e chapter1.tex, chapter2.tex, etc.), clicking somewhere in chapter
2 in the dvi will open chapter2.tex in a new tab in vi or select the
correct one if it is not already open. However, the settings are
_not_ applied to this tab when it opens.

Is the * necessary for vi to apply the settings to the new tab? Why
is using <buffer> preferred or necessary, will * break something?

Thanks again,

Josh

A.Politz

unread,
Nov 18, 2007, 2:53:36 PM11/18/07
to vim...@googlegroups.com

Josh Guffin wrote:

I was a little quick in applying the general rule in this case.
The augroup construct is to avoid having multiple autocommands,
in case the file gets resourced. But in the buffer-local case,
it would delete autocommands from other buffers as well, because
they would be in the same group. Though I am not really shure about
that, but maybe it is causing the trouble you describe later.
In other words: No augroup construct, or different augroups for
every buffer.


>>uSecond, are you shure you have sourced the lines from the latex


>>file ? These commands are local to the buffer. For testing you
>>could replace the '<buffer>' with an asterisk, which would trigger
>>them in any buffer.
>
>
>Ok, I've traced the problem to a question of tabs: if I open a tex
>file from the command line, i.e. vi blah.tex, then the wrapping works
>correctly. I have xdvi/vim set up as client-server, so that I can
>click on any part of a dvi file, and it will open up the source file
>in vi. When I'm editing a document which has several files included
>(i.e chapter1.tex, chapter2.tex, etc.), clicking somewhere in chapter
>2 in the dvi will open chapter2.tex in a new tab in vi or select the
>correct one if it is not already open. However, the settings are
>_not_ applied to this tab when it opens.
>
>Is the * necessary for vi to apply the settings to the new tab? Why
>is using <buffer> preferred or necessary, will * break something?

We are talking about vim. vi has not autocommands, I believe.
The '*' and '<buffer>' are arguments to the autocommand. They
specify when to apply it. The first one is a file glob pattern,
which matches any filename, so the command would apply to any
file you open, be it *.tex or whatever.
The '<buffer>' argument, on the other side, makes that command
local to the buffer, where it was executed/sourced (e.g. from
the ftplugin/tex.vim file ).
This is to avoid searching for '\begin' expressions in c++
files when you enter insert mode.

>
>Thanks again,
>
>Josh

Josh Guffin

unread,
Nov 18, 2007, 4:40:29 PM11/18/07
to vim...@googlegroups.com

Thanks for your help. I've modified the idea a bit, with the result
as follows:

"----------------------------------------
let g:customTw = 0
let g:oldTextWidth = 78
au InsertLeave *.tex call OutEquation()
au InsertEnter *.tex call InEquation()

func! OutEquation()
if (g:customTw == 1)
execute "setl tw=".g:oldTextWidth
endif
let g:customTw = 0
endfun

func! InEquation()
let s:returnVal = searchpair('\\begin{equation','','\\end
{equation','Wn') > 0 || searchpair('^\s*\\begin{equation','','^\s*\
\end{equation','Wn') > 0 || searchpair('^\s*\\begin{align','','^\s*\
\end{align','Wn') > 0 || searchpair('^\s*\\begin{alignat','','^\s*\
\end{alignat','Wn') > 0 || searchpair('^\s*\\begin{eqnarray','','^\s*\
\end{eqnarray','Wn') > 0

if (s:returnVal == 1 && g:customTw == 0)
let g:oldTextWidth = &tw
let g:customTw = 1
setl tw=0
endif
return s:returnVal
endfun
"----------------------------------------

the "let s:returnVal ..." should be all on one line. This setup will
save the current textwidth before changing it, so that once you leave
editing an equation, your old width is restored.

Caveat; this has only briefly been tested... YMMV

Thanks again,

Josh

Reply all
Reply to author
Forward
0 new messages