Karsten Hopp 4e9760
To: vim_dev@googlegroups.com
Karsten Hopp 4e9760
Subject: Patch 7.3.461
Karsten Hopp 4e9760
Fcc: outbox
Karsten Hopp 4e9760
From: Bram Moolenaar <Bram@moolenaar.net>
Karsten Hopp 4e9760
Mime-Version: 1.0
Karsten Hopp 4e9760
Content-Type: text/plain; charset=UTF-8
Karsten Hopp 4e9760
Content-Transfer-Encoding: 8bit
Karsten Hopp 4e9760
------------
Karsten Hopp 4e9760
Karsten Hopp 4e9760
Patch 7.3.461
Karsten Hopp 4e9760
Problem:    The InsertCharPre autocommand event is not triggered during
Karsten Hopp 4e9760
	    completion and when typing several characters quickly.
Karsten Hopp 4e9760
Solution:   Also trigger InsertCharPre during completion.  Do not read ahead
Karsten Hopp 4e9760
	    when an InsertCharPre autocommand is defined. (Yasuhiro Matsumoto)
Karsten Hopp 4e9760
Files:	    src/edit.c, src/fileio.c, src/proto/fileio.pro
Karsten Hopp 4e9760
Karsten Hopp 4e9760
Karsten Hopp 4e9760
*** ../vim-7.3.460/src/edit.c	2012-02-04 23:34:57.000000000 +0100
Karsten Hopp 4e9760
--- src/edit.c	2012-02-29 18:17:31.000000000 +0100
Karsten Hopp 4e9760
***************
Karsten Hopp 4e9760
*** 259,264 ****
Karsten Hopp 4e9760
--- 259,267 ----
Karsten Hopp 4e9760
  static void ins_try_si __ARGS((int c));
Karsten Hopp 4e9760
  #endif
Karsten Hopp 4e9760
  static colnr_T get_nolist_virtcol __ARGS((void));
Karsten Hopp 4e9760
+ #ifdef FEAT_AUTOCMD
Karsten Hopp 4e9760
+ static char_u *do_insert_char_pre __ARGS((int c));
Karsten Hopp 4e9760
+ #endif
Karsten Hopp 4e9760
  
Karsten Hopp 4e9760
  static colnr_T	Insstart_textlen;	/* length of line when insert started */
Karsten Hopp 4e9760
  static colnr_T	Insstart_blank_vcol;	/* vcol for first inserted blank */
Karsten Hopp 4e9760
***************
Karsten Hopp 4e9760
*** 784,790 ****
Karsten Hopp 4e9760
  		 * completion: Add to "compl_leader". */
Karsten Hopp 4e9760
  		if (ins_compl_accept_char(c))
Karsten Hopp 4e9760
  		{
Karsten Hopp 4e9760
! 		    ins_compl_addleader(c);
Karsten Hopp 4e9760
  		    continue;
Karsten Hopp 4e9760
  		}
Karsten Hopp 4e9760
  
Karsten Hopp 4e9760
--- 787,806 ----
Karsten Hopp 4e9760
  		 * completion: Add to "compl_leader". */
Karsten Hopp 4e9760
  		if (ins_compl_accept_char(c))
Karsten Hopp 4e9760
  		{
Karsten Hopp 4e9760
! #ifdef FEAT_AUTOCMD
Karsten Hopp 4e9760
! 		    /* Trigger InsertCharPre. */
Karsten Hopp 4e9760
! 		    char_u *str = do_insert_char_pre(c);
Karsten Hopp 4e9760
! 		    char_u *p;
Karsten Hopp 4e9760
! 
Karsten Hopp 4e9760
! 		    if (str != NULL)
Karsten Hopp 4e9760
! 		    {
Karsten Hopp 4e9760
! 			for (p = str; *p != NUL; mb_ptr_adv(p))
Karsten Hopp 4e9760
! 			    ins_compl_addleader(PTR2CHAR(p));
Karsten Hopp 4e9760
! 			vim_free(str);
Karsten Hopp 4e9760
! 		    }
Karsten Hopp 4e9760
! 		    else
Karsten Hopp 4e9760
! #endif
Karsten Hopp 4e9760
! 			ins_compl_addleader(c);
Karsten Hopp 4e9760
  		    continue;
Karsten Hopp 4e9760
  		}
Karsten Hopp 4e9760
  
Karsten Hopp 4e9760
***************
Karsten Hopp 4e9760
*** 1393,1426 ****
Karsten Hopp 4e9760
  #ifdef FEAT_AUTOCMD
Karsten Hopp 4e9760
  	    if (!p_paste)
Karsten Hopp 4e9760
  	    {
Karsten Hopp 4e9760
! 		/* Trigger the InsertCharPre event.  Lock the text to avoid
Karsten Hopp 4e9760
! 		 * weird things from happening. */
Karsten Hopp 4e9760
! 		set_vim_var_char(c);
Karsten Hopp 4e9760
! 		++textlock;
Karsten Hopp 4e9760
! 		if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL,
Karsten Hopp 4e9760
! 							       FALSE, curbuf))
Karsten Hopp 4e9760
! 		{
Karsten Hopp 4e9760
! 		    /* Get the new value of v:char.  If it is more than one
Karsten Hopp 4e9760
! 		     * character insert it literally. */
Karsten Hopp 4e9760
! 		    char_u *s = get_vim_var_str(VV_CHAR);
Karsten Hopp 4e9760
! 		    if (MB_CHARLEN(s) > 1)
Karsten Hopp 4e9760
  		    {
Karsten Hopp 4e9760
! 			if (stop_arrow() != FAIL)
Karsten Hopp 4e9760
  			{
Karsten Hopp 4e9760
! 			    ins_str(s);
Karsten Hopp 4e9760
! 			    AppendToRedobuffLit(s, -1);
Karsten Hopp 4e9760
  			}
Karsten Hopp 4e9760
! 			c = NUL;
Karsten Hopp 4e9760
  		    }
Karsten Hopp 4e9760
! 		    else
Karsten Hopp 4e9760
! 			c = PTR2CHAR(s);
Karsten Hopp 4e9760
  		}
Karsten Hopp 4e9760
  
Karsten Hopp 4e9760
! 		set_vim_var_string(VV_CHAR, NULL, -1);
Karsten Hopp 4e9760
! 		--textlock;
Karsten Hopp 4e9760
! 
Karsten Hopp 4e9760
! 		/* If the new value is an empty string then don't insert a
Karsten Hopp 4e9760
! 		 * char. */
Karsten Hopp 4e9760
  		if (c == NUL)
Karsten Hopp 4e9760
  		    break;
Karsten Hopp 4e9760
  	    }
Karsten Hopp 4e9760
--- 1409,1439 ----
Karsten Hopp 4e9760
  #ifdef FEAT_AUTOCMD
Karsten Hopp 4e9760
  	    if (!p_paste)
Karsten Hopp 4e9760
  	    {
Karsten Hopp 4e9760
! 		/* Trigger InsertCharPre. */
Karsten Hopp 4e9760
! 		char_u *str = do_insert_char_pre(c);
Karsten Hopp 4e9760
! 		char_u *p;
Karsten Hopp 4e9760
! 
Karsten Hopp 4e9760
! 		if (str != NULL)
Karsten Hopp 4e9760
! 		{
Karsten Hopp 4e9760
! 		    if (*str != NUL && stop_arrow() != FAIL)
Karsten Hopp 4e9760
  		    {
Karsten Hopp 4e9760
! 			/* Insert the new value of v:char literally. */
Karsten Hopp 4e9760
! 			for (p = str; *p != NUL; mb_ptr_adv(p))
Karsten Hopp 4e9760
  			{
Karsten Hopp 4e9760
! 			    c = PTR2CHAR(p);
Karsten Hopp 4e9760
! 			    if (c == CAR || c == K_KENTER || c == NL)
Karsten Hopp 4e9760
! 				ins_eol(c);
Karsten Hopp 4e9760
! 			    else
Karsten Hopp 4e9760
! 				ins_char(c);
Karsten Hopp 4e9760
  			}
Karsten Hopp 4e9760
! 			AppendToRedobuffLit(str, -1);
Karsten Hopp 4e9760
  		    }
Karsten Hopp 4e9760
! 		    vim_free(str);
Karsten Hopp 4e9760
! 		    c = NUL;
Karsten Hopp 4e9760
  		}
Karsten Hopp 4e9760
  
Karsten Hopp 4e9760
! 		/* If the new value is already inserted or an empty string
Karsten Hopp 4e9760
! 		 * then don't insert any character. */
Karsten Hopp 4e9760
  		if (c == NUL)
Karsten Hopp 4e9760
  		    break;
Karsten Hopp 4e9760
  	    }
Karsten Hopp 4e9760
***************
Karsten Hopp 4e9760
*** 5883,5888 ****
Karsten Hopp 4e9760
--- 5896,5903 ----
Karsten Hopp 4e9760
       * Don't do this when 'cindent' or 'indentexpr' is set, because we might
Karsten Hopp 4e9760
       * need to re-indent at a ':', or any other character (but not what
Karsten Hopp 4e9760
       * 'paste' is set)..
Karsten Hopp 4e9760
+      * Don't do this when there an InsertCharPre autocommand is defined,
Karsten Hopp 4e9760
+      * because we need to fire the event for every character.
Karsten Hopp 4e9760
       */
Karsten Hopp 4e9760
  #ifdef USE_ON_FLY_SCROLL
Karsten Hopp 4e9760
      dont_scroll = FALSE;		/* allow scrolling here */
Karsten Hopp 4e9760
***************
Karsten Hopp 4e9760
*** 5900,5905 ****
Karsten Hopp 4e9760
--- 5915,5923 ----
Karsten Hopp 4e9760
  #ifdef FEAT_RIGHTLEFT
Karsten Hopp 4e9760
  	    && !p_ri
Karsten Hopp 4e9760
  #endif
Karsten Hopp 4e9760
+ #ifdef FEAT_AUTOCMD
Karsten Hopp 4e9760
+ 	    && !has_insertcharpre()
Karsten Hopp 4e9760
+ #endif
Karsten Hopp 4e9760
  	       )
Karsten Hopp 4e9760
      {
Karsten Hopp 4e9760
  #define INPUT_BUFLEN 100
Karsten Hopp 4e9760
***************
Karsten Hopp 4e9760
*** 10068,10070 ****
Karsten Hopp 4e9760
--- 10086,10123 ----
Karsten Hopp 4e9760
      validate_virtcol();
Karsten Hopp 4e9760
      return curwin->w_virtcol;
Karsten Hopp 4e9760
  }
Karsten Hopp 4e9760
+ 
Karsten Hopp 4e9760
+ #ifdef FEAT_AUTOCMD
Karsten Hopp 4e9760
+ /*
Karsten Hopp 4e9760
+  * Handle the InsertCharPre autocommand.
Karsten Hopp 4e9760
+  * "c" is the character that was typed.
Karsten Hopp 4e9760
+  * Return a pointer to allocated memory with the replacement string.
Karsten Hopp 4e9760
+  * Return NULL to continue inserting "c".
Karsten Hopp 4e9760
+  */
Karsten Hopp 4e9760
+     static char_u *
Karsten Hopp 4e9760
+ do_insert_char_pre(c)
Karsten Hopp 4e9760
+     int c;
Karsten Hopp 4e9760
+ {
Karsten Hopp 4e9760
+     char_u *res;
Karsten Hopp 4e9760
+ 
Karsten Hopp 4e9760
+     /* Return quickly when there is nothing to do. */
Karsten Hopp 4e9760
+     if (!has_insertcharpre())
Karsten Hopp 4e9760
+ 	return NULL;
Karsten Hopp 4e9760
+ 
Karsten Hopp 4e9760
+     /* Lock the text to avoid weird things from happening. */
Karsten Hopp 4e9760
+     ++textlock;
Karsten Hopp 4e9760
+     set_vim_var_char(c);  /* set v:char */
Karsten Hopp 4e9760
+ 
Karsten Hopp 4e9760
+     if (apply_autocmds(EVENT_INSERTCHARPRE, NULL, NULL, FALSE, curbuf))
Karsten Hopp 4e9760
+ 	/* Get the new value of v:char.  It may be empty or more than one
Karsten Hopp 4e9760
+ 	 * character. */
Karsten Hopp 4e9760
+ 	res = vim_strsave(get_vim_var_str(VV_CHAR));
Karsten Hopp 4e9760
+     else
Karsten Hopp 4e9760
+ 	res = NULL;
Karsten Hopp 4e9760
+ 
Karsten Hopp 4e9760
+     set_vim_var_string(VV_CHAR, NULL, -1);  /* clear v:char */
Karsten Hopp 4e9760
+     --textlock;
Karsten Hopp 4e9760
+ 
Karsten Hopp 4e9760
+     return res;
Karsten Hopp 4e9760
+ }
Karsten Hopp 4e9760
+ #endif
Karsten Hopp 4e9760
*** ../vim-7.3.460/src/fileio.c	2012-02-12 20:13:55.000000000 +0100
Karsten Hopp 4e9760
--- src/fileio.c	2012-02-29 17:50:32.000000000 +0100
Karsten Hopp 4e9760
***************
Karsten Hopp 4e9760
*** 9116,9121 ****
Karsten Hopp 4e9760
--- 9116,9130 ----
Karsten Hopp 4e9760
      return (first_autopat[(int)EVENT_CURSORMOVEDI] != NULL);
Karsten Hopp 4e9760
  }
Karsten Hopp 4e9760
  
Karsten Hopp 4e9760
+ /*
Karsten Hopp 4e9760
+  * Return TRUE when there is an InsertCharPre autocommand defined.
Karsten Hopp 4e9760
+  */
Karsten Hopp 4e9760
+     int
Karsten Hopp 4e9760
+ has_insertcharpre()
Karsten Hopp 4e9760
+ {
Karsten Hopp 4e9760
+     return (first_autopat[(int)EVENT_INSERTCHARPRE] != NULL);
Karsten Hopp 4e9760
+ }
Karsten Hopp 4e9760
+ 
Karsten Hopp 4e9760
      static int
Karsten Hopp 4e9760
  apply_autocmds_group(event, fname, fname_io, force, group, buf, eap)
Karsten Hopp 4e9760
      event_T	event;
Karsten Hopp 4e9760
*** ../vim-7.3.460/src/proto/fileio.pro	2012-02-12 20:13:55.000000000 +0100
Karsten Hopp 4e9760
--- src/proto/fileio.pro	2012-02-29 17:50:38.000000000 +0100
Karsten Hopp 4e9760
***************
Karsten Hopp 4e9760
*** 44,49 ****
Karsten Hopp 4e9760
--- 44,50 ----
Karsten Hopp 4e9760
  int trigger_cursorhold __ARGS((void));
Karsten Hopp 4e9760
  int has_cursormoved __ARGS((void));
Karsten Hopp 4e9760
  int has_cursormovedI __ARGS((void));
Karsten Hopp 4e9760
+ int has_insertcharpre __ARGS((void));
Karsten Hopp 4e9760
  void block_autocmds __ARGS((void));
Karsten Hopp 4e9760
  void unblock_autocmds __ARGS((void));
Karsten Hopp 4e9760
  int has_autocmd __ARGS((event_T event, char_u *sfname, buf_T *buf));
Karsten Hopp 4e9760
*** ../vim-7.3.460/src/version.c	2012-02-29 16:56:35.000000000 +0100
Karsten Hopp 4e9760
--- src/version.c	2012-02-29 18:15:34.000000000 +0100
Karsten Hopp 4e9760
***************
Karsten Hopp 4e9760
*** 716,717 ****
Karsten Hopp 4e9760
--- 716,719 ----
Karsten Hopp 4e9760
  {   /* Add new patch number below this line */
Karsten Hopp 4e9760
+ /**/
Karsten Hopp 4e9760
+     461,
Karsten Hopp 4e9760
  /**/
Karsten Hopp 4e9760
Karsten Hopp 4e9760
-- 
Karsten Hopp 4e9760
"Computers in the future may weigh no more than 1.5 tons."
Karsten Hopp 4e9760
                                   Popular Mechanics, 1949
Karsten Hopp 4e9760
Karsten Hopp 4e9760
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
Karsten Hopp 4e9760
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
Karsten Hopp 4e9760
\\\  an exciting new programming language -- http://www.Zimbu.org        ///
Karsten Hopp 4e9760
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///