Hello,

On February, 5, 2010, Nico Raffo requested the implementation of the
auto command event InsertCharPre, which would be useful to his Conque
Shell plugin [1]. As the developer of the Vim-R-plugin [2], which
optionally uses the Conque Shell plugin, I was also interested in the
patch. We developed the patch, tested it for some weeks, and now I am
submitting the patch to the Vim developers to evaluate.

Thank you!

Jakson Aquino

[1] http://groups.google.com/group/vim_dev/browse_thread/thread/67020478db647370
[2] http://www.vim.org/scripts/script.php?script_id=2628

-- 
You received this message from the "vim_dev" maillist.
Do not top-post! Type your reply below the text you are replying to.
For more information, visit http://www.vim.org/maillist.php
diff -r 26fb122355d4 runtime/doc/autocmd.txt
--- a/runtime/doc/autocmd.txt	Sat Jan 22 21:25:11 2011 +0100
+++ b/runtime/doc/autocmd.txt	Thu Jan 27 11:49:42 2011 -0300
@@ -299,6 +299,7 @@
 |InsertEnter|		starting Insert mode
 |InsertChange|		when typing <Insert> while in Insert or Replace mode
 |InsertLeave|		when leaving Insert mode
+|InsertCharPre|		when a character was typed in Insert mode, before inserting it
 
 |ColorScheme|		after loading a color scheme
 
@@ -657,6 +658,14 @@
 				indicates the new mode.
 				Be careful not to move the cursor or do
 				anything else that the user does not expect.
+							*InsertCharPre*
+InsertCharPre			When a character is typed in Insert mode,
+				before inserting the char.  Pattern is matched
+				with text before the cursor. The |v:char|
+				variable indicates the char typed and can be
+				changed during the event to insert a different
+				character. The event is not triggered when
+				'paste' is set.
 							*InsertEnter*
 InsertEnter			Just before starting Insert mode.  Also for
 				Replace mode and Virtual Replace mode.  The
diff -r 26fb122355d4 runtime/doc/eval.txt
--- a/runtime/doc/eval.txt	Sat Jan 22 21:25:11 2011 +0100
+++ b/runtime/doc/eval.txt	Thu Jan 27 11:49:42 2011 -0300
@@ -1291,6 +1291,7 @@
 					*v:char* *char-variable*
 v:char		Argument for evaluating 'formatexpr' and used for the typed
 		character when using <expr> in an abbreviation |:map-<expr>|.
+		It is also set by the |InsertPreChar| event.
 
 			*v:charconvert_from* *charconvert_from-variable*
 v:charconvert_from
diff -r 26fb122355d4 runtime/doc/map.txt
--- a/runtime/doc/map.txt	Sat Jan 22 21:25:11 2011 +0100
+++ b/runtime/doc/map.txt	Thu Jan 27 11:49:42 2011 -0300
@@ -226,7 +226,7 @@
 
 For abbreviations |v:char| is set to the character that was typed to trigger
 the abbreviation.  You can use this to decide how to expand the {lhs}.  You
-can't change v:char and you should not insert it.
+you should not either insert or change the v:char.
 
 Be very careful about side effects!  The expression is evaluated while
 obtaining characters, you may very well make the command dysfunctional.
diff -r 26fb122355d4 runtime/doc/todo.txt
--- a/runtime/doc/todo.txt	Sat Jan 22 21:25:11 2011 +0100
+++ b/runtime/doc/todo.txt	Thu Jan 27 11:49:42 2011 -0300
@@ -3419,10 +3419,6 @@
 		    - Before/after ":cd" has been used (for changing the
 		      window title)
     ShutDown	    - when the system is about to shut down
-    InsertCharPre   - user typed character Insert mode, before inserting the
-		      char.  Pattern is matched with text before the cursor.
-		      Set v:char to the character, can be changed.
-		      (not triggered when 'paste' is set).
     InsertCharPost  - user typed a character in Insert mode, after inserting
 		      the char.
     BufModified	    - When a buffer becomes modified, or unmodified (for
diff -r 26fb122355d4 src/edit.c
--- a/src/edit.c	Sat Jan 22 21:25:11 2011 +0100
+++ b/src/edit.c	Thu Jan 27 11:49:42 2011 -0300
@@ -309,6 +309,7 @@
     long	count;
 {
     int		c = 0;
+    char_u	*s;
     char_u	*ptr;
     int		lastc;
     int		mincol;
@@ -1385,6 +1386,29 @@
 	     * Insert a nomal character.
 	     */
 normalchar:
+#ifdef FEAT_AUTOCMD
+	    /* trigger InsertCharPre event */
+	    set_vim_var_char(c);
+	    if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
+	    {
+                /* get new value of v:char */
+                s = get_vim_var_str(VV_CHAR);
+
+                /* if new value is empty string, don't add to buffer */
+                if (*s == NUL)
+                {
+	            set_vim_var_string(VV_CHAR, NULL, -1);
+		    break;
+                }
+                /* get the new value from autocommand function */
+#ifdef FEAT_MBYTE
+		c = (*mb_ptr2char)(s);
+#else
+		c = *s;
+#endif
+	    }
+	    set_vim_var_string(VV_CHAR, NULL, -1);
+#endif
 #ifdef FEAT_SMARTINDENT
 	    /* Try to perform smart-indenting. */
 	    ins_try_si(c);
diff -r 26fb122355d4 src/eval.c
--- a/src/eval.c	Sat Jan 22 21:25:11 2011 +0100
+++ b/src/eval.c	Thu Jan 27 11:49:42 2011 -0300
@@ -352,7 +352,7 @@
     {VV_NAME("swapname",	 VAR_STRING), VV_RO},
     {VV_NAME("swapchoice",	 VAR_STRING), 0},
     {VV_NAME("swapcommand",	 VAR_STRING), VV_RO},
-    {VV_NAME("char",		 VAR_STRING), VV_RO},
+    {VV_NAME("char",		 VAR_STRING), 0},
     {VV_NAME("mouse_win",	 VAR_NUMBER), 0},
     {VV_NAME("mouse_lnum",	 VAR_NUMBER), 0},
     {VV_NAME("mouse_col",	 VAR_NUMBER), 0},
diff -r 26fb122355d4 src/fileio.c
--- a/src/fileio.c	Sat Jan 22 21:25:11 2011 +0100
+++ b/src/fileio.c	Thu Jan 27 11:49:42 2011 -0300
@@ -7666,6 +7666,7 @@
     {"InsertChange",	EVENT_INSERTCHANGE},
     {"InsertEnter",	EVENT_INSERTENTER},
     {"InsertLeave",	EVENT_INSERTLEAVE},
+    {"InsertCharPre",	EVENT_INSERTCHARPRE},
     {"MenuPopup",	EVENT_MENUPOPUP},
     {"QuickFixCmdPost",	EVENT_QUICKFIXCMDPOST},
     {"QuickFixCmdPre",	EVENT_QUICKFIXCMDPRE},
diff -r 26fb122355d4 src/vim.h
--- a/src/vim.h	Sat Jan 22 21:25:11 2011 +0100
+++ b/src/vim.h	Thu Jan 27 11:49:42 2011 -0300
@@ -1281,6 +1281,7 @@
     EVENT_WINENTER,		/* after entering a window */
     EVENT_WINLEAVE,		/* before leaving a window */
     EVENT_ENCODINGCHANGED,	/* after changing the 'encoding' option */
+    EVENT_INSERTCHARPRE,	/* before inserting a char */
     EVENT_CURSORHOLD,		/* cursor in same position for a while */
     EVENT_CURSORHOLDI,		/* idem, in Insert mode */
     EVENT_FUNCUNDEFINED,	/* if calling a function which doesn't exist */

Raspunde prin e-mail lui