20af70f
To: vim-dev@vim.org
20af70f
Subject: Patch 7.2.109
20af70f
Fcc: outbox
20af70f
From: Bram Moolenaar <Bram@moolenaar.net>
20af70f
Mime-Version: 1.0
20af70f
Content-Type: text/plain; charset=ISO-8859-1
20af70f
Content-Transfer-Encoding: 8bit
20af70f
------------
20af70f
20af70f
Patch 7.2.109
20af70f
Problem:    'langmap' does not work for multi-byte characters.
20af70f
Solution:   Add a list of mapped multi-byte characters. (based on work by
20af70f
            Konstantin Korikov, Agathoklis Hatzimanikas)
20af70f
Files:      runtime/doc/options.txt, src/edit.c, src/getchar.c, src/macros.h,
20af70f
            src/normal.c, src/option.c, src/proto/option.pro, src/window.c
20af70f
    
20af70f
20af70f
*** ../vim-7.2.108/runtime/doc/options.txt	Fri Nov 28 10:59:57 2008
20af70f
--- runtime/doc/options.txt	Wed Feb 11 18:59:34 2009
20af70f
***************
20af70f
*** 4175,4183 ****
20af70f
  	be able to execute Normal mode commands.
20af70f
  	This is the opposite of the 'keymap' option, where characters are
20af70f
  	mapped in Insert mode.
20af70f
- 	This only works for 8-bit characters.  The value of 'langmap' may be
20af70f
- 	specified with multi-byte characters (e.g., UTF-8), but only the lower
20af70f
- 	8 bits of each character will be used.
20af70f
  
20af70f
  	Example (for Greek, in UTF-8):				*greek*  >
20af70f
  	    :set langmap=ΑA,ΒB,ΨC,ΔD,ΕE,ΦF,ΓG,ΗH,ΙI,ΞJ,ΚK,ΛL,ΜM,ΝN,ΟO,ΠP,QQ,ΡR,ΣS,ΤT,ΘU,ΩV,WW,ΧX,ΥY,ΖZ,αa,βb,ψc,δd,εe,φf,γg,ηh,ιi,ξj,κk,λl,μm,νn,οo,πp,qq,ρr,σs,τt,θu,ωv,ςw,χx,υy,ζz
20af70f
--- 4188,4193 ----
20af70f
*** ../vim-7.2.108/src/edit.c	Wed Feb  4 11:19:40 2009
20af70f
--- src/edit.c	Sat Feb 21 19:54:03 2009
20af70f
***************
20af70f
*** 7703,7711 ****
20af70f
       */
20af70f
      ++no_mapping;
20af70f
      regname = plain_vgetc();
20af70f
- #ifdef FEAT_LANGMAP
20af70f
      LANGMAP_ADJUST(regname, TRUE);
20af70f
- #endif
20af70f
      if (regname == Ctrl_R || regname == Ctrl_O || regname == Ctrl_P)
20af70f
      {
20af70f
  	/* Get a third key for literal register insertion */
20af70f
--- 7703,7709 ----
20af70f
***************
20af70f
*** 7714,7722 ****
20af70f
  	add_to_showcmd_c(literally);
20af70f
  #endif
20af70f
  	regname = plain_vgetc();
20af70f
- #ifdef FEAT_LANGMAP
20af70f
  	LANGMAP_ADJUST(regname, TRUE);
20af70f
- #endif
20af70f
      }
20af70f
      --no_mapping;
20af70f
  
20af70f
--- 7712,7718 ----
20af70f
*** ../vim-7.2.108/src/macros.h	Wed Aug 15 20:41:07 2007
20af70f
--- src/macros.h	Sat Feb 21 19:55:38 2009
20af70f
***************
20af70f
*** 127,141 ****
20af70f
  #ifdef FEAT_LANGMAP
20af70f
  /*
20af70f
   * Adjust chars in a language according to 'langmap' option.
20af70f
!  * NOTE that there is NO overhead if 'langmap' is not set; but even
20af70f
!  * when set we only have to do 2 ifs and an array lookup.
20af70f
   * Don't apply 'langmap' if the character comes from the Stuff buffer.
20af70f
   * The do-while is just to ignore a ';' after the macro.
20af70f
   */
20af70f
! # define LANGMAP_ADJUST(c, condition) do { \
20af70f
! 	if (*p_langmap && (condition) && !KeyStuffed && (c) >= 0 && (c) < 256) \
20af70f
! 	    c = langmap_mapchar[c]; \
20af70f
      } while (0)
20af70f
  #endif
20af70f
  
20af70f
  /*
20af70f
--- 127,157 ----
20af70f
  #ifdef FEAT_LANGMAP
20af70f
  /*
20af70f
   * Adjust chars in a language according to 'langmap' option.
20af70f
!  * NOTE that there is no noticeable overhead if 'langmap' is not set.
20af70f
!  * When set the overhead for characters < 256 is small.
20af70f
   * Don't apply 'langmap' if the character comes from the Stuff buffer.
20af70f
   * The do-while is just to ignore a ';' after the macro.
20af70f
   */
20af70f
! # ifdef FEAT_MBYTE
20af70f
! #  define LANGMAP_ADJUST(c, condition) \
20af70f
!     do { \
20af70f
!         if (*p_langmap && (condition) && !KeyStuffed && (c) >= 0) \
20af70f
! 	{ \
20af70f
! 	    if ((c) < 256) \
20af70f
! 		c = langmap_mapchar[c]; \
20af70f
! 	    else \
20af70f
! 		c = langmap_adjust_mb(c); \
20af70f
! 	} \
20af70f
      } while (0)
20af70f
+ # else
20af70f
+ #  define LANGMAP_ADJUST(c, condition) \
20af70f
+     do { \
20af70f
+         if (*p_langmap && (condition) && !KeyStuffed && (c) >= 0 && (c) < 256) \
20af70f
+             c = langmap_mapchar[c]; \
20af70f
+     } while (0)
20af70f
+ # endif
20af70f
+ #else
20af70f
+ # define LANGMAP_ADJUST(c, condition) /* nop */
20af70f
  #endif
20af70f
  
20af70f
  /*
20af70f
*** ../vim-7.2.108/src/normal.c	Wed Feb  4 11:45:28 2009
20af70f
--- src/normal.c	Sat Feb 21 19:55:17 2009
20af70f
***************
20af70f
*** 651,660 ****
20af70f
       * Get the command character from the user.
20af70f
       */
20af70f
      c = safe_vgetc();
20af70f
- 
20af70f
- #ifdef FEAT_LANGMAP
20af70f
      LANGMAP_ADJUST(c, TRUE);
20af70f
- #endif
20af70f
  
20af70f
  #ifdef FEAT_VISUAL
20af70f
      /*
20af70f
--- 651,657 ----
20af70f
***************
20af70f
*** 744,752 ****
20af70f
  	    }
20af70f
  	    ++no_zero_mapping;		/* don't map zero here */
20af70f
  	    c = plain_vgetc();
20af70f
- #ifdef FEAT_LANGMAP
20af70f
  	    LANGMAP_ADJUST(c, TRUE);
20af70f
- #endif
20af70f
  	    --no_zero_mapping;
20af70f
  	    if (ctrl_w)
20af70f
  	    {
20af70f
--- 741,747 ----
20af70f
***************
20af70f
*** 769,777 ****
20af70f
  	    ++no_mapping;
20af70f
  	    ++allow_keys;		/* no mapping for nchar, but keys */
20af70f
  	    c = plain_vgetc();		/* get next character */
20af70f
- #ifdef FEAT_LANGMAP
20af70f
  	    LANGMAP_ADJUST(c, TRUE);
20af70f
- #endif
20af70f
  	    --no_mapping;
20af70f
  	    --allow_keys;
20af70f
  #ifdef FEAT_CMDL_INFO
20af70f
--- 764,770 ----
20af70f
***************
20af70f
*** 959,967 ****
20af70f
  	     * "gr", "g'" and "g`".
20af70f
  	     */
20af70f
  	    ca.nchar = plain_vgetc();
20af70f
- #ifdef FEAT_LANGMAP
20af70f
  	    LANGMAP_ADJUST(ca.nchar, TRUE);
20af70f
- #endif
20af70f
  #ifdef FEAT_CMDL_INFO
20af70f
  	    need_flushbuf |= add_to_showcmd(ca.nchar);
20af70f
  #endif
20af70f
--- 952,958 ----
20af70f
***************
20af70f
*** 1062,1071 ****
20af70f
  		}
20af70f
  #endif
20af70f
  
20af70f
- #ifdef FEAT_LANGMAP
20af70f
  		/* adjust chars > 127, except after "tTfFr" commands */
20af70f
  		LANGMAP_ADJUST(*cp, !lang);
20af70f
- #endif
20af70f
  #ifdef FEAT_RIGHTLEFT
20af70f
  		/* adjust Hebrew mapped char */
20af70f
  		if (p_hkmap && lang && KeyTyped)
20af70f
--- 1053,1060 ----
20af70f
***************
20af70f
*** 4630,4638 ****
20af70f
  	    ++no_mapping;
20af70f
  	    ++allow_keys;   /* no mapping for nchar, but allow key codes */
20af70f
  	    nchar = plain_vgetc();
20af70f
- #ifdef FEAT_LANGMAP
20af70f
  	    LANGMAP_ADJUST(nchar, TRUE);
20af70f
- #endif
20af70f
  	    --no_mapping;
20af70f
  	    --allow_keys;
20af70f
  #ifdef FEAT_CMDL_INFO
20af70f
--- 4619,4625 ----
20af70f
***************
20af70f
*** 4988,4996 ****
20af70f
  		++no_mapping;
20af70f
  		++allow_keys;   /* no mapping for nchar, but allow key codes */
20af70f
  		nchar = plain_vgetc();
20af70f
- #ifdef FEAT_LANGMAP
20af70f
  		LANGMAP_ADJUST(nchar, TRUE);
20af70f
- #endif
20af70f
  		--no_mapping;
20af70f
  		--allow_keys;
20af70f
  #ifdef FEAT_CMDL_INFO
20af70f
--- 4975,4981 ----
20af70f
*** ../vim-7.2.108/src/option.c	Wed Feb 11 22:47:32 2009
20af70f
--- src/option.c	Sat Feb 21 19:46:13 2009
20af70f
***************
20af70f
*** 10153,10177 ****
20af70f
  
20af70f
  #ifdef FEAT_LANGMAP
20af70f
  /*
20af70f
!  * Any character has an equivalent character.  This is used for keyboards that
20af70f
!  * have a special language mode that sends characters above 128 (although
20af70f
!  * other characters can be translated too).
20af70f
   */
20af70f
  
20af70f
  /*
20af70f
!  * char_u langmap_mapchar[256];
20af70f
!  * Normally maps each of the 128 upper chars to an <128 ascii char; used to
20af70f
!  * "translate" native lang chars in normal mode or some cases of
20af70f
!  * insert mode without having to tediously switch lang mode back&forth.
20af70f
   */
20af70f
  
20af70f
      static void
20af70f
  langmap_init()
20af70f
  {
20af70f
      int i;
20af70f
  
20af70f
!     for (i = 0; i < 256; i++)		/* we init with a-one-to one map */
20af70f
! 	langmap_mapchar[i] = i;
20af70f
  }
20af70f
  
20af70f
  /*
20af70f
--- 10153,10262 ----
20af70f
  
20af70f
  #ifdef FEAT_LANGMAP
20af70f
  /*
20af70f
!  * Any character has an equivalent 'langmap' character.  This is used for
20af70f
!  * keyboards that have a special language mode that sends characters above
20af70f
!  * 128 (although other characters can be translated too).  The "to" field is a
20af70f
!  * Vim command character.  This avoids having to switch the keyboard back to
20af70f
!  * ASCII mode when leaving Insert mode.
20af70f
!  *
20af70f
!  * langmap_mapchar[] maps any of 256 chars to an ASCII char used for Vim
20af70f
!  * commands.
20af70f
!  * When FEAT_MBYTE is defined langmap_mapga.ga_data is a sorted table of
20af70f
!  * langmap_entry_T.  This does the same as langmap_mapchar[] for characters >=
20af70f
!  * 256.
20af70f
!  */
20af70f
! # ifdef FEAT_MBYTE
20af70f
! /*
20af70f
!  * With multi-byte support use growarray for 'langmap' chars >= 256
20af70f
   */
20af70f
+ typedef struct
20af70f
+ {
20af70f
+     int	    from;
20af70f
+     int     to;
20af70f
+ } langmap_entry_T;
20af70f
+ 
20af70f
+ static garray_T langmap_mapga;
20af70f
+ static void langmap_set_entry __ARGS((int from, int to));
20af70f
+ 
20af70f
+ /*
20af70f
+  * Search for an entry in "langmap_mapga" for "from".  If found set the "to"
20af70f
+  * field.  If not found insert a new entry at the appropriate location.
20af70f
+  */
20af70f
+     static void
20af70f
+ langmap_set_entry(from, to)
20af70f
+     int    from;
20af70f
+     int    to;
20af70f
+ {
20af70f
+     langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
20af70f
+     int             a = 0;
20af70f
+     int             b = langmap_mapga.ga_len;
20af70f
+ 
20af70f
+     /* Do a binary search for an existing entry. */
20af70f
+     while (a != b)
20af70f
+     {
20af70f
+ 	int i = (a + b) / 2;
20af70f
+ 	int d = entries[i].from - from;
20af70f
+ 
20af70f
+ 	if (d == 0)
20af70f
+ 	{
20af70f
+ 	    entries[i].to = to;
20af70f
+ 	    return;
20af70f
+ 	}
20af70f
+ 	if (d < 0)
20af70f
+ 	    a = i + 1;
20af70f
+ 	else
20af70f
+ 	    b = i;
20af70f
+     }
20af70f
+ 
20af70f
+     if (ga_grow(&langmap_mapga, 1) != OK)
20af70f
+ 	return;  /* out of memory */
20af70f
+ 
20af70f
+     /* insert new entry at position "a" */
20af70f
+     entries = (langmap_entry_T *)(langmap_mapga.ga_data) + a;
20af70f
+     mch_memmove(entries + 1, entries,
20af70f
+ 			(langmap_mapga.ga_len - a) * sizeof(langmap_entry_T));
20af70f
+     ++langmap_mapga.ga_len;
20af70f
+     entries[0].from = from;
20af70f
+     entries[0].to = to;
20af70f
+ }
20af70f
  
20af70f
  /*
20af70f
!  * Apply 'langmap' to multi-byte character "c" and return the result.
20af70f
   */
20af70f
+     int
20af70f
+ langmap_adjust_mb(c)
20af70f
+     int c;
20af70f
+ {
20af70f
+     langmap_entry_T *entries = (langmap_entry_T *)(langmap_mapga.ga_data);
20af70f
+     int a = 0;
20af70f
+     int b = langmap_mapga.ga_len;
20af70f
+ 
20af70f
+     while (a != b)
20af70f
+     {
20af70f
+ 	int i = (a + b) / 2;
20af70f
+ 	int d = entries[i].from - c;
20af70f
+ 
20af70f
+ 	if (d == 0)
20af70f
+ 	    return entries[i].to;  /* found matching entry */
20af70f
+ 	if (d < 0)
20af70f
+ 	    a = i + 1;
20af70f
+ 	else
20af70f
+ 	    b = i;
20af70f
+     }
20af70f
+     return c;  /* no entry found, return "c" unmodified */
20af70f
+ }
20af70f
+ # endif
20af70f
  
20af70f
      static void
20af70f
  langmap_init()
20af70f
  {
20af70f
      int i;
20af70f
  
20af70f
!     for (i = 0; i < 256; i++)
20af70f
! 	langmap_mapchar[i] = i;	 /* we init with a one-to-one map */
20af70f
! # ifdef FEAT_MBYTE
20af70f
!     ga_init2(&langmap_mapga, sizeof(langmap_entry_T), 8);
20af70f
! # endif
20af70f
  }
20af70f
  
20af70f
  /*
20af70f
***************
20af70f
*** 10185,10191 ****
20af70f
      char_u  *p2;
20af70f
      int	    from, to;
20af70f
  
20af70f
!     langmap_init();			    /* back to one-to-one map first */
20af70f
  
20af70f
      for (p = p_langmap; p[0] != NUL; )
20af70f
      {
20af70f
--- 10270,10279 ----
20af70f
      char_u  *p2;
20af70f
      int	    from, to;
20af70f
  
20af70f
! #ifdef FEAT_MBYTE
20af70f
!     ga_clear(&langmap_mapga);		    /* clear the previous map first */
20af70f
! #endif
20af70f
!     langmap_init();			    /* back to one-to-one map */
20af70f
  
20af70f
      for (p = p_langmap; p[0] != NUL; )
20af70f
      {
20af70f
***************
20af70f
*** 10235,10241 ****
20af70f
  							     transchar(from));
20af70f
  		return;
20af70f
  	    }
20af70f
! 	    langmap_mapchar[from & 255] = to;
20af70f
  
20af70f
  	    /* Advance to next pair */
20af70f
  	    mb_ptr_adv(p);
20af70f
--- 10323,10335 ----
20af70f
  							     transchar(from));
20af70f
  		return;
20af70f
  	    }
20af70f
! 
20af70f
! #ifdef FEAT_MBYTE
20af70f
! 	    if (from >= 256)
20af70f
! 		langmap_set_entry(from, to);
20af70f
! 	    else
20af70f
! #endif
20af70f
! 		langmap_mapchar[from & 255] = to;
20af70f
  
20af70f
  	    /* Advance to next pair */
20af70f
  	    mb_ptr_adv(p);
20af70f
*** ../vim-7.2.108/src/proto/option.pro	Sat May  5 19:28:04 2007
20af70f
--- src/proto/option.pro	Wed Feb 11 21:21:05 2009
20af70f
***************
20af70f
*** 44,49 ****
20af70f
--- 44,50 ----
20af70f
  void set_context_in_set_cmd __ARGS((expand_T *xp, char_u *arg, int opt_flags));
20af70f
  int ExpandSettings __ARGS((expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file));
20af70f
  int ExpandOldSetting __ARGS((int *num_file, char_u ***file));
20af70f
+ int langmap_adjust_mb __ARGS((int c));
20af70f
  int has_format_option __ARGS((int x));
20af70f
  int shortmess __ARGS((int x));
20af70f
  void vimrc_found __ARGS((char_u *fname, char_u *envname));
20af70f
*** ../vim-7.2.108/src/window.c	Fri Nov 28 21:26:50 2008
20af70f
--- src/window.c	Sat Feb 21 19:55:25 2009
20af70f
***************
20af70f
*** 594,602 ****
20af70f
  		++allow_keys;   /* no mapping for xchar, but allow key codes */
20af70f
  		if (xchar == NUL)
20af70f
  		    xchar = plain_vgetc();
20af70f
- #ifdef FEAT_LANGMAP
20af70f
  		LANGMAP_ADJUST(xchar, TRUE);
20af70f
- #endif
20af70f
  		--no_mapping;
20af70f
  		--allow_keys;
20af70f
  #ifdef FEAT_CMDL_INFO
20af70f
--- 594,600 ----
20af70f
*** ../vim-7.2.108/src/version.c	Wed Feb 11 22:47:32 2009
20af70f
--- src/version.c	Sat Feb 21 19:34:28 2009
20af70f
***************
20af70f
*** 678,679 ****
20af70f
--- 678,681 ----
20af70f
  {   /* Add new patch number below this line */
20af70f
+ /**/
20af70f
+     109,
20af70f
  /**/
20af70f
20af70f
-- 
20af70f
hundred-and-one symptoms of being an internet addict:
20af70f
99. The hum of a cooling fan and the click of keys is comforting to you.
20af70f
20af70f
 /// Bram Moolenaar -- Bram@Moolenaar.net -- http://www.Moolenaar.net   \\\
20af70f
///        sponsor Vim, vote for features -- http://www.Vim.org/sponsor/ \\\
20af70f
\\\        download, build and distribute -- http://www.A-A-P.org        ///
20af70f
 \\\            help me help AIDS victims -- http://ICCF-Holland.org    ///