[PATCH] Update doc: No :normal cursor movement in indentexpr

30 views
Skip to first unread message

Sung Pae

unread,
Feb 8, 2013, 11:01:33 PM2/8/13
to vim...@googlegroups.com
The documentation explicitly states that moving the cursor is okay in
an indentexpr, but this is only true when moved directly with cursor(),
setpos(), search(), etc. Moving the cursor via `normal! w` has side
effects that frustrate the attempts of get_expr_indent() to restore the
original cursor position. However, this is not apparent without reading
the source.

For example:

function! BadIndentExpr()
normal! $
return 8
endfunction
setlocal indentexpr=BadIndentExpr()

The cursor position is not restored when indenting the current line.
---
runtime/doc/options.txt | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/runtime/doc/options.txt b/runtime/doc/options.txt
index 3beb2b0..b0ea21c 100644
--- a/runtime/doc/options.txt
+++ b/runtime/doc/options.txt
@@ -4162,7 +4162,8 @@ A jump table for the options with a short description can be found at |Q_op|.
and |lispindent()|.
The evaluation of the expression must not have side effects! It must
not change the text, jump to another window, etc. Afterwards the
- cursor position is always restored, thus the cursor may be moved.
+ cursor position is always restored, thus the cursor may be moved (but
+ not via |:normal| commands).
Normally this option would be set to call a function: >
:set indentexpr=GetMyIndent()
< Error messages will be suppressed, unless the 'debug' option contains
--
1.8.1.2

Bram Moolenaar

unread,
Feb 9, 2013, 8:10:36 AM2/9/13
to Sung Pae, vim...@googlegroups.com

Sung Pae wrote:

> The documentation explicitly states that moving the cursor is okay in
> an indentexpr, but this is only true when moved directly with cursor(),
> setpos(), search(), etc. Moving the cursor via `normal! w` has side
> effects that frustrate the attempts of get_expr_indent() to restore the
> original cursor position. However, this is not apparent without reading
> the source.
>
> For example:
>
> function! BadIndentExpr()
> normal! $
> return 8
> endfunction
> setlocal indentexpr=BadIndentExpr()
>
> The cursor position is not restored when indenting the current line.

I rather fix the problem than updating the documentation.
You say you read the source code, can you pinpoint why the cursor is not
restored when using :normal?


--
Men may not be seen publicly in any kind of strapless gown.
[real standing law in Florida, United States of America]

/// Bram Moolenaar -- Br...@Moolenaar.net -- http://www.Moolenaar.net \\\
/// sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
\\\ an exciting new programming language -- http://www.Zimbu.org ///
\\\ help me help AIDS victims -- http://ICCF-Holland.org ///

Sung Pae

unread,
Feb 9, 2013, 1:58:03 PM2/9/13
to Bram Moolenaar, vim...@googlegroups.com
On Sat, Feb 09, 2013 at 02:10:36PM +0100, Bram Moolenaar wrote:

> I rather fix the problem than updating the documentation.

Sounds great; I mispredicted your response.

> You say you read the source code, can you pinpoint why the cursor is
> not restored when using :normal?

:normal commands update curwin->w_curswant and curwin->w_set_curswant,
which beginline() uses to set the cursor position. op_reindent calls
beginline() with the BL_SOL flag set, so this bug only appears when
'nostartofline' is set (which I didn't notice before).

Attached patch simply saves and restores both settings after the
'indentexpr' evaluation.

Thank you,
Sung Pae
0001-Restore-curswant-settings-after-indentexpr-evaluatio.patch

Bram Moolenaar

unread,
Feb 9, 2013, 4:53:44 PM2/9/13
to Sung Pae, vim...@googlegroups.com
Sounds right. Thanks for the patch!


--
The Law, in its majestic equality, forbids the rich, as well as the
poor, to sleep under the bridges, to beg in the streets, and to steal
bread. -- Anatole France
Reply all
Reply to author
Forward
0 new messages