Mercurial > vim
comparison src/getchar.c @ 2610:6c05b5e5c1be v7.3.032
updated for version 7.3.032
Problem: maparg() doesn't return the flags, such as <buffer>, <script>,
<silent>. These are needed to save and restore a mapping.
Solution: Improve maparg(). (also by Christian Brabandt)
author | Bram Moolenaar <bram@vim.org> |
---|---|
date | Wed, 20 Oct 2010 21:23:33 +0200 |
parents | 6768ebd0bc04 |
children | 5b154df6fcd8 |
comparison
equal
deleted
inserted
replaced
2609:758822790b43 | 2610:6c05b5e5c1be |
---|---|
3166 int special = FALSE; | 3166 int special = FALSE; |
3167 #ifdef FEAT_EVAL | 3167 #ifdef FEAT_EVAL |
3168 int expr = FALSE; | 3168 int expr = FALSE; |
3169 #endif | 3169 #endif |
3170 int noremap; | 3170 int noremap; |
3171 char_u *orig_rhs; | |
3171 | 3172 |
3172 keys = arg; | 3173 keys = arg; |
3173 map_table = maphash; | 3174 map_table = maphash; |
3174 abbr_table = &first_abbr; | 3175 abbr_table = &first_abbr; |
3175 | 3176 |
3264 ++p; /* skip CTRL-V or backslash */ | 3265 ++p; /* skip CTRL-V or backslash */ |
3265 ++p; | 3266 ++p; |
3266 } | 3267 } |
3267 if (*p != NUL) | 3268 if (*p != NUL) |
3268 *p++ = NUL; | 3269 *p++ = NUL; |
3270 | |
3269 p = skipwhite(p); | 3271 p = skipwhite(p); |
3270 rhs = p; | 3272 rhs = p; |
3271 hasarg = (*rhs != NUL); | 3273 hasarg = (*rhs != NUL); |
3272 haskey = (*keys != NUL); | 3274 haskey = (*keys != NUL); |
3273 | 3275 |
3288 */ | 3290 */ |
3289 if (haskey) | 3291 if (haskey) |
3290 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special); | 3292 keys = replace_termcodes(keys, &keys_buf, TRUE, TRUE, special); |
3291 if (hasarg) | 3293 if (hasarg) |
3292 { | 3294 { |
3295 orig_rhs = rhs; | |
3293 if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */ | 3296 if (STRICMP(rhs, "<nop>") == 0) /* "<Nop>" means nothing */ |
3294 rhs = (char_u *)""; | 3297 rhs = (char_u *)""; |
3295 else | 3298 else |
3296 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special); | 3299 rhs = replace_termcodes(rhs, &arg_buf, FALSE, TRUE, special); |
3297 } | 3300 } |
3298 | 3301 |
3299 #ifdef FEAT_FKMAP | 3302 #ifdef FEAT_FKMAP |
3300 /* | 3303 /* |
3301 * when in right-to-left mode and alternate keymap option set, | 3304 * When in right-to-left mode and alternate keymap option set, |
3302 * reverse the character flow in the rhs in Farsi. | 3305 * reverse the character flow in the rhs in Farsi. |
3303 */ | 3306 */ |
3304 if (p_altkeymap && curwin->w_p_rl) | 3307 if (p_altkeymap && curwin->w_p_rl) |
3305 lrswap(rhs); | 3308 lrswap(rhs); |
3306 #endif | 3309 #endif |
3554 retval = 4; /* no mem */ | 3557 retval = 4; /* no mem */ |
3555 goto theend; | 3558 goto theend; |
3556 } | 3559 } |
3557 vim_free(mp->m_str); | 3560 vim_free(mp->m_str); |
3558 mp->m_str = newstr; | 3561 mp->m_str = newstr; |
3562 vim_free(mp->m_orig_str); | |
3563 mp->m_orig_str = vim_strsave(orig_rhs); | |
3559 mp->m_noremap = noremap; | 3564 mp->m_noremap = noremap; |
3560 mp->m_silent = silent; | 3565 mp->m_silent = silent; |
3561 mp->m_mode = mode; | 3566 mp->m_mode = mode; |
3562 #ifdef FEAT_EVAL | 3567 #ifdef FEAT_EVAL |
3563 mp->m_expr = expr; | 3568 mp->m_expr = expr; |
3631 if (*keys == Ctrl_C) | 3636 if (*keys == Ctrl_C) |
3632 mapped_ctrl_c = TRUE; | 3637 mapped_ctrl_c = TRUE; |
3633 | 3638 |
3634 mp->m_keys = vim_strsave(keys); | 3639 mp->m_keys = vim_strsave(keys); |
3635 mp->m_str = vim_strsave(rhs); | 3640 mp->m_str = vim_strsave(rhs); |
3641 mp->m_orig_str = vim_strsave(orig_rhs); | |
3636 if (mp->m_keys == NULL || mp->m_str == NULL) | 3642 if (mp->m_keys == NULL || mp->m_str == NULL) |
3637 { | 3643 { |
3638 vim_free(mp->m_keys); | 3644 vim_free(mp->m_keys); |
3639 vim_free(mp->m_str); | 3645 vim_free(mp->m_str); |
3646 vim_free(mp->m_orig_str); | |
3640 vim_free(mp); | 3647 vim_free(mp); |
3641 retval = 4; /* no mem */ | 3648 retval = 4; /* no mem */ |
3642 goto theend; | 3649 goto theend; |
3643 } | 3650 } |
3644 mp->m_keylen = (int)STRLEN(mp->m_keys); | 3651 mp->m_keylen = (int)STRLEN(mp->m_keys); |
3680 mapblock_T *mp; | 3687 mapblock_T *mp; |
3681 | 3688 |
3682 mp = *mpp; | 3689 mp = *mpp; |
3683 vim_free(mp->m_keys); | 3690 vim_free(mp->m_keys); |
3684 vim_free(mp->m_str); | 3691 vim_free(mp->m_str); |
3692 vim_free(mp->m_orig_str); | |
3685 *mpp = mp->m_next; | 3693 *mpp = mp->m_next; |
3686 vim_free(mp); | 3694 vim_free(mp); |
3687 } | 3695 } |
3688 | 3696 |
3689 /* | 3697 /* |
3849 mpp = &(mp->m_next); | 3857 mpp = &(mp->m_next); |
3850 } | 3858 } |
3851 } | 3859 } |
3852 } | 3860 } |
3853 | 3861 |
3862 /* | |
3863 * Return characters to represent the map mode in an allocated string. | |
3864 * Returns NULL when out of memory. | |
3865 */ | |
3866 char_u * | |
3867 map_mode_to_chars(mode) | |
3868 int mode; | |
3869 { | |
3870 garray_T mapmode; | |
3871 | |
3872 ga_init2(&mapmode, 1, 7); | |
3873 | |
3874 if ((mode & (INSERT + CMDLINE)) == INSERT + CMDLINE) | |
3875 ga_append(&mapmode, '!'); /* :map! */ | |
3876 else if (mode & INSERT) | |
3877 ga_append(&mapmode, 'i'); /* :imap */ | |
3878 else if (mode & LANGMAP) | |
3879 ga_append(&mapmode, 'l'); /* :lmap */ | |
3880 else if (mode & CMDLINE) | |
3881 ga_append(&mapmode, 'c'); /* :cmap */ | |
3882 else if ((mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING)) | |
3883 == NORMAL + VISUAL + SELECTMODE + OP_PENDING) | |
3884 ga_append(&mapmode, ' '); /* :map */ | |
3885 else | |
3886 { | |
3887 if (mode & NORMAL) | |
3888 ga_append(&mapmode, 'n'); /* :nmap */ | |
3889 if (mode & OP_PENDING) | |
3890 ga_append(&mapmode, 'o'); /* :omap */ | |
3891 if ((mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE) | |
3892 ga_append(&mapmode, 'v'); /* :vmap */ | |
3893 else | |
3894 { | |
3895 if (mode & VISUAL) | |
3896 ga_append(&mapmode, 'x'); /* :xmap */ | |
3897 if (mode & SELECTMODE) | |
3898 ga_append(&mapmode, 's'); /* :smap */ | |
3899 } | |
3900 } | |
3901 | |
3902 ga_append(&mapmode, NUL); | |
3903 return (char_u *)mapmode.ga_data; | |
3904 } | |
3905 | |
3854 static void | 3906 static void |
3855 showmap(mp, local) | 3907 showmap(mp, local) |
3856 mapblock_T *mp; | 3908 mapblock_T *mp; |
3857 int local; /* TRUE for buffer-local map */ | 3909 int local; /* TRUE for buffer-local map */ |
3858 { | 3910 { |
3859 int len = 1; | 3911 int len = 1; |
3912 char_u *mapchars; | |
3860 | 3913 |
3861 if (msg_didout || msg_silent != 0) | 3914 if (msg_didout || msg_silent != 0) |
3862 { | 3915 { |
3863 msg_putchar('\n'); | 3916 msg_putchar('\n'); |
3864 if (got_int) /* 'q' typed at MORE prompt */ | 3917 if (got_int) /* 'q' typed at MORE prompt */ |
3865 return; | 3918 return; |
3866 } | 3919 } |
3867 if ((mp->m_mode & (INSERT + CMDLINE)) == INSERT + CMDLINE) | 3920 |
3868 msg_putchar('!'); /* :map! */ | 3921 mapchars = map_mode_to_chars(mp->m_mode); |
3869 else if (mp->m_mode & INSERT) | 3922 if (mapchars != NULL) |
3870 msg_putchar('i'); /* :imap */ | 3923 { |
3871 else if (mp->m_mode & LANGMAP) | 3924 msg_puts(mapchars); |
3872 msg_putchar('l'); /* :lmap */ | 3925 len = STRLEN(mapchars); |
3873 else if (mp->m_mode & CMDLINE) | 3926 vim_free(mapchars); |
3874 msg_putchar('c'); /* :cmap */ | 3927 } |
3875 else if ((mp->m_mode & (NORMAL + VISUAL + SELECTMODE + OP_PENDING)) | 3928 |
3876 == NORMAL + VISUAL + SELECTMODE + OP_PENDING) | |
3877 msg_putchar(' '); /* :map */ | |
3878 else | |
3879 { | |
3880 len = 0; | |
3881 if (mp->m_mode & NORMAL) | |
3882 { | |
3883 msg_putchar('n'); /* :nmap */ | |
3884 ++len; | |
3885 } | |
3886 if (mp->m_mode & OP_PENDING) | |
3887 { | |
3888 msg_putchar('o'); /* :omap */ | |
3889 ++len; | |
3890 } | |
3891 if ((mp->m_mode & (VISUAL + SELECTMODE)) == VISUAL + SELECTMODE) | |
3892 { | |
3893 msg_putchar('v'); /* :vmap */ | |
3894 ++len; | |
3895 } | |
3896 else | |
3897 { | |
3898 if (mp->m_mode & VISUAL) | |
3899 { | |
3900 msg_putchar('x'); /* :xmap */ | |
3901 ++len; | |
3902 } | |
3903 if (mp->m_mode & SELECTMODE) | |
3904 { | |
3905 msg_putchar('s'); /* :smap */ | |
3906 ++len; | |
3907 } | |
3908 } | |
3909 } | |
3910 while (++len <= 3) | 3929 while (++len <= 3) |
3911 msg_putchar(' '); | 3930 msg_putchar(' '); |
3912 | 3931 |
3913 /* Display the LHS. Get length of what we write. */ | 3932 /* Display the LHS. Get length of what we write. */ |
3914 len = msg_outtrans_special(mp->m_keys, TRUE); | 3933 len = msg_outtrans_special(mp->m_keys, TRUE); |
3929 msg_putchar('@'); | 3948 msg_putchar('@'); |
3930 else | 3949 else |
3931 msg_putchar(' '); | 3950 msg_putchar(' '); |
3932 | 3951 |
3933 /* Use FALSE below if we only want things like <Up> to show up as such on | 3952 /* Use FALSE below if we only want things like <Up> to show up as such on |
3934 * the rhs, and not M-x etc, TRUE gets both -- webb | 3953 * the rhs, and not M-x etc, TRUE gets both -- webb */ |
3935 */ | |
3936 if (*mp->m_str == NUL) | 3954 if (*mp->m_str == NUL) |
3937 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8)); | 3955 msg_puts_attr((char_u *)"<Nop>", hl_attr(HLF_8)); |
3938 else | 3956 else |
3939 msg_outtrans_special(mp->m_str, FALSE); | 3957 msg_outtrans_special(mp->m_str, FALSE); |
3940 #ifdef FEAT_EVAL | 3958 #ifdef FEAT_EVAL |
4993 } | 5011 } |
4994 #endif | 5012 #endif |
4995 sourcing_name = save_name; | 5013 sourcing_name = save_name; |
4996 } | 5014 } |
4997 | 5015 |
4998 #ifdef FEAT_EVAL | 5016 #if defined(FEAT_EVAL) || defined(PROTO) |
4999 /* | 5017 /* |
5000 * Check the string "keys" against the lhs of all mappings | 5018 * Check the string "keys" against the lhs of all mappings. |
5001 * Return pointer to rhs of mapping (mapblock->m_str) | 5019 * Return pointer to rhs of mapping (mapblock->m_str). |
5002 * NULL otherwise | 5020 * NULL when no mapping found. |
5003 */ | 5021 */ |
5004 char_u * | 5022 char_u * |
5005 check_map(keys, mode, exact, ign_mod, abbr) | 5023 check_map(keys, mode, exact, ign_mod, abbr, mp_ptr, local_ptr) |
5006 char_u *keys; | 5024 char_u *keys; |
5007 int mode; | 5025 int mode; |
5008 int exact; /* require exact match */ | 5026 int exact; /* require exact match */ |
5009 int ign_mod; /* ignore preceding modifier */ | 5027 int ign_mod; /* ignore preceding modifier */ |
5010 int abbr; /* do abbreviations */ | 5028 int abbr; /* do abbreviations */ |
5029 mapblock_T **mp_ptr; /* return: pointer to mapblock or NULL */ | |
5030 int *local_ptr; /* return: buffer-local mapping or NULL */ | |
5011 { | 5031 { |
5012 int hash; | 5032 int hash; |
5013 int len, minlen; | 5033 int len, minlen; |
5014 mapblock_T *mp; | 5034 mapblock_T *mp; |
5015 char_u *s; | 5035 char_u *s; |
5060 s += 3; | 5080 s += 3; |
5061 if (len > mp->m_keylen - 3) | 5081 if (len > mp->m_keylen - 3) |
5062 minlen = mp->m_keylen - 3; | 5082 minlen = mp->m_keylen - 3; |
5063 } | 5083 } |
5064 if (STRNCMP(s, keys, minlen) == 0) | 5084 if (STRNCMP(s, keys, minlen) == 0) |
5085 { | |
5086 if (mp_ptr != NULL) | |
5087 *mp_ptr = mp; | |
5088 if (local_ptr != NULL) | |
5089 *local_ptr = local; | |
5065 return mp->m_str; | 5090 return mp->m_str; |
5091 } | |
5066 } | 5092 } |
5067 } | 5093 } |
5068 } | 5094 } |
5069 | 5095 |
5070 return NULL; | 5096 return NULL; |