04f8197
To: vim-dev@vim.org
04f8197
Subject: Patch 7.2.266
04f8197
Fcc: outbox
04f8197
From: Bram Moolenaar <Bram@moolenaar.net>
04f8197
Mime-Version: 1.0
04f8197
Content-Type: text/plain; charset=UTF-8
04f8197
Content-Transfer-Encoding: 8bit
04f8197
------------
04f8197
04f8197
Patch 7.2.266
04f8197
Problem:    When an expression abbreviation is triggered, the typed character
04f8197
	    is unknown.
04f8197
Solution:   Make the typed character available in v:char.
04f8197
Files:	    runtime/doc/map.txt, src/eval.c, src/getchar.c, src/ops.c,
04f8197
	    src/proto/eval.pro
04f8197
04f8197
04f8197
*** ../vim-7.2.265/runtime/doc/map.txt	2008-08-09 19:36:49.000000000 +0200
04f8197
--- runtime/doc/map.txt	2009-09-23 19:39:19.000000000 +0200
04f8197
***************
04f8197
*** 224,229 ****
04f8197
--- 224,233 ----
04f8197
  The result of the InsertDot() function will be inserted.  It could check the
04f8197
  text before the cursor and start omni completion when some condition is met.
04f8197
  
04f8197
+ For abbreviations |v:char| is set to the character that was typed to trigger
04f8197
+ the abbreviation.  You can use this to decide how to expand the {lhs}.  You
04f8197
+ can't change v:char and you should not insert it.
04f8197
+ 
04f8197
  Be very careful about side effects!  The expression is evaluated while
04f8197
  obtaining characters, you may very well make the command dysfunctional.
04f8197
  For this reason the following is blocked:
04f8197
*** ../vim-7.2.265/src/eval.c	2009-06-03 14:25:47.000000000 +0200
04f8197
--- src/eval.c	2009-09-23 19:36:32.000000000 +0200
04f8197
***************
04f8197
*** 18101,18106 ****
04f8197
--- 18101,18131 ----
04f8197
  }
04f8197
  
04f8197
  /*
04f8197
+  * Set v:char to character "c".
04f8197
+  */
04f8197
+     void
04f8197
+ set_vim_var_char(c)
04f8197
+     int c;
04f8197
+ {
04f8197
+ #ifdef FEAT_MBYTE
04f8197
+     char_u	buf[MB_MAXBYTES];
04f8197
+ #else
04f8197
+     char_u	buf[2];
04f8197
+ #endif
04f8197
+ 
04f8197
+ #ifdef FEAT_MBYTE
04f8197
+     if (has_mbyte)
04f8197
+ 	buf[(*mb_char2bytes)(c, buf)] = NUL;
04f8197
+     else
04f8197
+ #endif
04f8197
+     {
04f8197
+ 	buf[0] = c;
04f8197
+ 	buf[1] = NUL;
04f8197
+     }
04f8197
+     set_vim_var_string(VV_CHAR, buf, -1);
04f8197
+ }
04f8197
+ 
04f8197
+ /*
04f8197
   * Set v:count to "count" and v:count1 to "count1".
04f8197
   * When "set_prevcount" is TRUE first set v:prevcount from v:count.
04f8197
   */
04f8197
*** ../vim-7.2.265/src/getchar.c	2009-07-14 13:44:43.000000000 +0200
04f8197
--- src/getchar.c	2009-09-23 19:35:54.000000000 +0200
04f8197
***************
04f8197
*** 129,135 ****
04f8197
  static void	validate_maphash __ARGS((void));
04f8197
  static void	showmap __ARGS((mapblock_T *mp, int local));
04f8197
  #ifdef FEAT_EVAL
04f8197
! static char_u	*eval_map_expr __ARGS((char_u *str));
04f8197
  #endif
04f8197
  
04f8197
  /*
04f8197
--- 129,135 ----
04f8197
  static void	validate_maphash __ARGS((void));
04f8197
  static void	showmap __ARGS((mapblock_T *mp, int local));
04f8197
  #ifdef FEAT_EVAL
04f8197
! static char_u	*eval_map_expr __ARGS((char_u *str, int c));
04f8197
  #endif
04f8197
  
04f8197
  /*
04f8197
***************
04f8197
*** 2446,2452 ****
04f8197
  			    if (tabuf.typebuf_valid)
04f8197
  			    {
04f8197
  				vgetc_busy = 0;
04f8197
! 				s = eval_map_expr(mp->m_str);
04f8197
  				vgetc_busy = save_vgetc_busy;
04f8197
  			    }
04f8197
  			    else
04f8197
--- 2446,2452 ----
04f8197
  			    if (tabuf.typebuf_valid)
04f8197
  			    {
04f8197
  				vgetc_busy = 0;
04f8197
! 				s = eval_map_expr(mp->m_str, NUL);
04f8197
  				vgetc_busy = save_vgetc_busy;
04f8197
  			    }
04f8197
  			    else
04f8197
***************
04f8197
*** 4367,4375 ****
04f8197
  	     * abbreviation, but is not inserted into the input stream.
04f8197
  	     */
04f8197
  	    j = 0;
04f8197
- 					/* special key code, split up */
04f8197
  	    if (c != Ctrl_RSB)
04f8197
  	    {
04f8197
  		if (IS_SPECIAL(c) || c == K_SPECIAL)
04f8197
  		{
04f8197
  		    tb[j++] = K_SPECIAL;
04f8197
--- 4367,4375 ----
04f8197
  	     * abbreviation, but is not inserted into the input stream.
04f8197
  	     */
04f8197
  	    j = 0;
04f8197
  	    if (c != Ctrl_RSB)
04f8197
  	    {
04f8197
+ 					/* special key code, split up */
04f8197
  		if (IS_SPECIAL(c) || c == K_SPECIAL)
04f8197
  		{
04f8197
  		    tb[j++] = K_SPECIAL;
04f8197
***************
04f8197
*** 4398,4404 ****
04f8197
  	    }
04f8197
  #ifdef FEAT_EVAL
04f8197
  	    if (mp->m_expr)
04f8197
! 		s = eval_map_expr(mp->m_str);
04f8197
  	    else
04f8197
  #endif
04f8197
  		s = mp->m_str;
04f8197
--- 4398,4404 ----
04f8197
  	    }
04f8197
  #ifdef FEAT_EVAL
04f8197
  	    if (mp->m_expr)
04f8197
! 		s = eval_map_expr(mp->m_str, c);
04f8197
  	    else
04f8197
  #endif
04f8197
  		s = mp->m_str;
04f8197
***************
04f8197
*** 4434,4441 ****
04f8197
   * special characters.
04f8197
   */
04f8197
      static char_u *
04f8197
! eval_map_expr(str)
04f8197
      char_u	*str;
04f8197
  {
04f8197
      char_u	*res;
04f8197
      char_u	*p;
04f8197
--- 4434,4442 ----
04f8197
   * special characters.
04f8197
   */
04f8197
      static char_u *
04f8197
! eval_map_expr(str, c)
04f8197
      char_u	*str;
04f8197
+     int		c;	    /* NUL or typed character for abbreviation */
04f8197
  {
04f8197
      char_u	*res;
04f8197
      char_u	*p;
04f8197
***************
04f8197
*** 4452,4457 ****
04f8197
--- 4453,4459 ----
04f8197
  #ifdef FEAT_EX_EXTRA
04f8197
      ++ex_normal_lock;
04f8197
  #endif
04f8197
+     set_vim_var_char(c);  /* set v:char to the typed character */
04f8197
      save_cursor = curwin->w_cursor;
04f8197
      p = eval_to_string(str, NULL, FALSE);
04f8197
      --textlock;
04f8197
*** ../vim-7.2.265/src/ops.c	2009-07-01 18:04:30.000000000 +0200
04f8197
--- src/ops.c	2009-09-23 19:11:40.000000000 +0200
04f8197
***************
04f8197
*** 4473,4483 ****
04f8197
      int		use_sandbox = was_set_insecurely((char_u *)"formatexpr",
04f8197
  								   OPT_LOCAL);
04f8197
      int		r;
04f8197
- #ifdef FEAT_MBYTE
04f8197
-     char_u	buf[MB_MAXBYTES];
04f8197
- #else
04f8197
-     char_u	buf[2];
04f8197
- #endif
04f8197
  
04f8197
      /*
04f8197
       * Set v:lnum to the first line number and v:count to the number of lines.
04f8197
--- 4473,4478 ----
04f8197
***************
04f8197
*** 4485,4501 ****
04f8197
       */
04f8197
      set_vim_var_nr(VV_LNUM, lnum);
04f8197
      set_vim_var_nr(VV_COUNT, count);
04f8197
! 
04f8197
! #ifdef FEAT_MBYTE
04f8197
!     if (has_mbyte)
04f8197
! 	buf[(*mb_char2bytes)(c, buf)] = NUL;
04f8197
!     else
04f8197
! #endif
04f8197
!     {
04f8197
! 	buf[0] = c;
04f8197
! 	buf[1] = NUL;
04f8197
!     }
04f8197
!     set_vim_var_string(VV_CHAR, buf, -1);
04f8197
  
04f8197
      /*
04f8197
       * Evaluate the function.
04f8197
--- 4480,4486 ----
04f8197
       */
04f8197
      set_vim_var_nr(VV_LNUM, lnum);
04f8197
      set_vim_var_nr(VV_COUNT, count);
04f8197
!     set_vim_var_char(c);
04f8197
  
04f8197
      /*
04f8197
       * Evaluate the function.
04f8197
*** ../vim-7.2.265/src/proto/eval.pro	2008-11-20 16:11:03.000000000 +0100
04f8197
--- src/proto/eval.pro	2009-09-23 19:36:30.000000000 +0200
04f8197
***************
04f8197
*** 61,66 ****
04f8197
--- 61,67 ----
04f8197
  long get_vim_var_nr __ARGS((int idx));
04f8197
  char_u *get_vim_var_str __ARGS((int idx));
04f8197
  list_T *get_vim_var_list __ARGS((int idx));
04f8197
+ void set_vim_var_char __ARGS((int c));
04f8197
  void set_vcount __ARGS((long count, long count1, int set_prevcount));
04f8197
  void set_vim_var_string __ARGS((int idx, char_u *val, int len));
04f8197
  void set_vim_var_list __ARGS((int idx, list_T *val));
04f8197
*** ../vim-7.2.265/src/version.c	2009-09-30 13:23:57.000000000 +0200
04f8197
--- src/version.c	2009-09-30 15:11:29.000000000 +0200
04f8197
***************
04f8197
*** 678,679 ****
04f8197
--- 678,681 ----
04f8197
  {   /* Add new patch number below this line */
04f8197
+ /**/
04f8197
+     266,
04f8197
  /**/
04f8197
04f8197
-- 
04f8197
Life would be so much easier if we could just look at the source code.
04f8197
04f8197
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
04f8197
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
04f8197
\\\        download, build and distribute -- http://www.A-A-P.org        ///
04f8197
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///