Bram Moolenaar | edf3f97 | 2016-08-29 22:49:24 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2 | * |
| 3 | * VIM - Vi IMproved by Bram Moolenaar |
| 4 | * |
| 5 | * Do ":help uganda" in Vim to read copying and usage conditions. |
| 6 | * Do ":help credits" in Vim to see a list of people who contributed. |
| 7 | * See README.txt for an overview of the Vim source code. |
| 8 | */ |
| 9 | |
| 10 | /* |
| 11 | * ex_getln.c: Functions for entering and editing an Ex command line. |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | /* |
| 17 | * Variables shared between getcmdline(), redrawcmdline() and others. |
| 18 | * These need to be saved when using CTRL-R |, that's why they are in a |
| 19 | * structure. |
| 20 | */ |
| 21 | struct cmdline_info |
| 22 | { |
| 23 | char_u *cmdbuff; /* pointer to command line buffer */ |
| 24 | int cmdbufflen; /* length of cmdbuff */ |
| 25 | int cmdlen; /* number of chars in command line */ |
| 26 | int cmdpos; /* current cursor position */ |
| 27 | int cmdspos; /* cursor column on screen */ |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 28 | int cmdfirstc; /* ':', '/', '?', '=', '>' or NUL */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 29 | int cmdindent; /* number of spaces before cmdline */ |
| 30 | char_u *cmdprompt; /* message in front of cmdline */ |
| 31 | int cmdattr; /* attributes for prompt */ |
| 32 | int overstrike; /* Typing mode on the command line. Shared by |
| 33 | getcmdline() and put_on_cmdline(). */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 34 | expand_T *xpc; /* struct being used for expansion, xp_pattern |
| 35 | may point into cmdbuff */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 36 | int xp_context; /* type of expansion */ |
| 37 | # ifdef FEAT_EVAL |
| 38 | char_u *xp_arg; /* user-defined expansion arg */ |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 39 | int input_fn; /* when TRUE Invoked for input() function */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 40 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 41 | }; |
| 42 | |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 43 | /* The current cmdline_info. It is initialized in getcmdline() and after that |
| 44 | * used by other functions. When invoking getcmdline() recursively it needs |
| 45 | * to be saved with save_cmdline() and restored with restore_cmdline(). |
| 46 | * TODO: make it local to getcmdline() and pass it around. */ |
| 47 | static struct cmdline_info ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 48 | |
| 49 | static int cmd_showtail; /* Only show path tail in lists ? */ |
| 50 | |
| 51 | #ifdef FEAT_EVAL |
| 52 | static int new_cmdpos; /* position set by set_cmdline_pos() */ |
| 53 | #endif |
| 54 | |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 55 | static int extra_char = NUL; /* extra character to display when redrawing |
| 56 | * the command line */ |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 57 | static int extra_char_shift; |
| 58 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 59 | #ifdef FEAT_CMDHIST |
| 60 | typedef struct hist_entry |
| 61 | { |
| 62 | int hisnum; /* identifying number */ |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 63 | int viminfo; /* when TRUE hisstr comes from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 64 | char_u *hisstr; /* actual entry, separator char after the NUL */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 65 | time_t time_set; /* when it was typed, zero if unknown */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 66 | } histentry_T; |
| 67 | |
| 68 | static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; |
| 69 | static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */ |
| 70 | static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 71 | /* identifying (unique) number of newest history entry */ |
| 72 | static int hislen = 0; /* actual length of history tables */ |
| 73 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 74 | static int hist_char2type(int c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 75 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 76 | static int in_history(int, char_u *, int, int, int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 77 | # ifdef FEAT_EVAL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 78 | static int calc_hist_idx(int histype, int num); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 79 | # endif |
| 80 | #endif |
| 81 | |
| 82 | #ifdef FEAT_RIGHTLEFT |
| 83 | static int cmd_hkmap = 0; /* Hebrew mapping during command line */ |
| 84 | #endif |
| 85 | |
| 86 | #ifdef FEAT_FKMAP |
| 87 | static int cmd_fkmap = 0; /* Farsi mapping during command line */ |
| 88 | #endif |
| 89 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 90 | static int cmdline_charsize(int idx); |
| 91 | static void set_cmdspos(void); |
| 92 | static void set_cmdspos_cursor(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 93 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 94 | static void correct_cmdspos(int idx, int cells); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 95 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 96 | static void alloc_cmdbuff(int len); |
| 97 | static int realloc_cmdbuff(int len); |
| 98 | static void draw_cmdline(int start, int len); |
| 99 | static void save_cmdline(struct cmdline_info *ccp); |
| 100 | static void restore_cmdline(struct cmdline_info *ccp); |
| 101 | static int cmdline_paste(int regname, int literally, int remcr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 102 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 103 | static void redrawcmd_preedit(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 104 | #endif |
| 105 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 106 | static void cmdline_del(int from); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 107 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 108 | static void redrawcmdprompt(void); |
| 109 | static void cursorcmd(void); |
| 110 | static int ccheck_abbr(int); |
| 111 | static int nextwild(expand_T *xp, int type, int options, int escape); |
| 112 | static void escape_fname(char_u **pp); |
| 113 | static int showmatches(expand_T *xp, int wildmenu); |
| 114 | static void set_expand_context(expand_T *xp); |
| 115 | static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int); |
| 116 | static int expand_showtail(expand_T *xp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 117 | #ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 118 | static int expand_shellcmd(char_u *filepat, int *num_file, char_u ***file, int flagsarg); |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 119 | static int ExpandRTDir(char_u *pat, int flags, int *num_file, char_u ***file, char *dirname[]); |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 120 | static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 121 | # ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 122 | static char_u *get_history_arg(expand_T *xp, int idx); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 123 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 124 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 125 | static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file); |
| 126 | static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 127 | # endif |
| 128 | #endif |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 129 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 130 | static void clear_hist_entry(histentry_T *hisptr); |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 131 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 132 | |
| 133 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 134 | static int open_cmdwin(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 135 | #endif |
| 136 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 137 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 138 | static int |
| 139 | #ifdef __BORLANDC__ |
| 140 | _RTLENTRYF |
| 141 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 142 | sort_func_compare(const void *s1, const void *s2); |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 143 | #endif |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 144 | #ifdef FEAT_SEARCH_EXTRA |
| 145 | static void set_search_match(pos_T *t); |
| 146 | #endif |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 147 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 148 | |
| 149 | #ifdef FEAT_AUTOCMD |
| 150 | static void |
| 151 | trigger_cmd_autocmd(int typechar, int evt) |
| 152 | { |
| 153 | char_u typestr[2]; |
| 154 | |
| 155 | typestr[0] = typechar; |
| 156 | typestr[1] = NUL; |
| 157 | apply_autocmds(evt, typestr, typestr, FALSE, curbuf); |
| 158 | } |
| 159 | #endif |
| 160 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 161 | /* |
| 162 | * getcmdline() - accept a command line starting with firstc. |
| 163 | * |
| 164 | * firstc == ':' get ":" command line. |
| 165 | * firstc == '/' or '?' get search pattern |
| 166 | * firstc == '=' get expression |
| 167 | * firstc == '@' get text for input() function |
| 168 | * firstc == '>' get text for debug mode |
| 169 | * firstc == NUL get text for :insert command |
| 170 | * firstc == -1 like NUL, and break on CTRL-C |
| 171 | * |
| 172 | * The line is collected in ccline.cmdbuff, which is reallocated to fit the |
| 173 | * command line. |
| 174 | * |
| 175 | * Careful: getcmdline() can be called recursively! |
| 176 | * |
| 177 | * Return pointer to allocated string if there is a commandline, NULL |
| 178 | * otherwise. |
| 179 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 180 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 181 | getcmdline( |
| 182 | int firstc, |
| 183 | long count UNUSED, /* only used for incremental search */ |
| 184 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 185 | { |
| 186 | int c; |
| 187 | int i; |
| 188 | int j; |
| 189 | int gotesc = FALSE; /* TRUE when <ESC> just typed */ |
| 190 | int do_abbr; /* when TRUE check for abbr. */ |
| 191 | #ifdef FEAT_CMDHIST |
| 192 | char_u *lookfor = NULL; /* string to match */ |
| 193 | int hiscnt; /* current history line in use */ |
| 194 | int histype; /* history type to be used */ |
| 195 | #endif |
| 196 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 197 | pos_T search_start; /* where 'incsearch' starts searching */ |
| 198 | pos_T save_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 199 | colnr_T old_curswant; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 200 | colnr_T init_curswant = curwin->w_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 201 | colnr_T old_leftcol; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 202 | colnr_T init_leftcol = curwin->w_leftcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 203 | linenr_T old_topline; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 204 | linenr_T init_topline = curwin->w_topline; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 205 | pos_T match_start = curwin->w_cursor; |
| 206 | pos_T match_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 207 | # ifdef FEAT_DIFF |
| 208 | int old_topfill; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 209 | int init_topfill = curwin->w_topfill; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 210 | # endif |
| 211 | linenr_T old_botline; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 212 | linenr_T init_botline = curwin->w_botline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 213 | int did_incsearch = FALSE; |
| 214 | int incsearch_postponed = FALSE; |
| 215 | #endif |
| 216 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 217 | int wim_index = 0; /* index in wim_flags[] */ |
| 218 | int res; |
| 219 | int save_msg_scroll = msg_scroll; |
| 220 | int save_State = State; /* remember State when called */ |
| 221 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 222 | #ifdef FEAT_MOUSE |
| 223 | /* mouse drag and release events are ignored, unless they are |
| 224 | * preceded with a mouse down event */ |
| 225 | int ignore_drag_release = TRUE; |
| 226 | #endif |
| 227 | #ifdef FEAT_EVAL |
| 228 | int break_ctrl_c = FALSE; |
| 229 | #endif |
| 230 | expand_T xpc; |
| 231 | long *b_im_ptr = NULL; |
Bram Moolenaar | 4d85051 | 2017-02-09 18:25:14 +0100 | [diff] [blame] | 232 | #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA) |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 233 | /* Everything that may work recursively should save and restore the |
| 234 | * current command line in save_ccline. That includes update_screen(), a |
| 235 | * custom status line may invoke ":normal". */ |
| 236 | struct cmdline_info save_ccline; |
| 237 | #endif |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 238 | #ifdef FEAT_AUTOCMD |
| 239 | int cmdline_type; |
| 240 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 241 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 242 | #ifdef FEAT_EVAL |
| 243 | if (firstc == -1) |
| 244 | { |
| 245 | firstc = NUL; |
| 246 | break_ctrl_c = TRUE; |
| 247 | } |
| 248 | #endif |
| 249 | #ifdef FEAT_RIGHTLEFT |
| 250 | /* start without Hebrew mapping for a command line */ |
| 251 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 252 | cmd_hkmap = 0; |
| 253 | #endif |
| 254 | |
| 255 | ccline.overstrike = FALSE; /* always start in insert mode */ |
| 256 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 257 | CLEAR_POS(&match_end); |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 258 | save_cursor = curwin->w_cursor; /* may be restored later */ |
| 259 | search_start = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 260 | old_curswant = curwin->w_curswant; |
| 261 | old_leftcol = curwin->w_leftcol; |
| 262 | old_topline = curwin->w_topline; |
| 263 | # ifdef FEAT_DIFF |
| 264 | old_topfill = curwin->w_topfill; |
| 265 | # endif |
| 266 | old_botline = curwin->w_botline; |
| 267 | #endif |
| 268 | |
| 269 | /* |
| 270 | * set some variables for redrawcmd() |
| 271 | */ |
| 272 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 273 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 274 | |
| 275 | /* alloc initial ccline.cmdbuff */ |
| 276 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 277 | if (ccline.cmdbuff == NULL) |
| 278 | return NULL; /* out of memory */ |
| 279 | ccline.cmdlen = ccline.cmdpos = 0; |
| 280 | ccline.cmdbuff[0] = NUL; |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 281 | sb_text_start_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 282 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 283 | /* autoindent for :insert and :append */ |
| 284 | if (firstc <= 0) |
| 285 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 286 | vim_memset(ccline.cmdbuff, ' ', indent); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 287 | ccline.cmdbuff[indent] = NUL; |
| 288 | ccline.cmdpos = indent; |
| 289 | ccline.cmdspos = indent; |
| 290 | ccline.cmdlen = indent; |
| 291 | } |
| 292 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 293 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 294 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 295 | |
| 296 | #ifdef FEAT_RIGHTLEFT |
| 297 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 298 | && (firstc == '/' || firstc == '?')) |
| 299 | cmdmsg_rl = TRUE; |
| 300 | else |
| 301 | cmdmsg_rl = FALSE; |
| 302 | #endif |
| 303 | |
| 304 | redir_off = TRUE; /* don't redirect the typed command */ |
| 305 | if (!cmd_silent) |
| 306 | { |
| 307 | i = msg_scrolled; |
| 308 | msg_scrolled = 0; /* avoid wait_return message */ |
| 309 | gotocmdline(TRUE); |
| 310 | msg_scrolled += i; |
| 311 | redrawcmdprompt(); /* draw prompt or indent */ |
| 312 | set_cmdspos(); |
| 313 | } |
| 314 | xpc.xp_context = EXPAND_NOTHING; |
| 315 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 316 | #ifndef BACKSLASH_IN_FILENAME |
| 317 | xpc.xp_shell = FALSE; |
| 318 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 319 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 320 | #if defined(FEAT_EVAL) |
| 321 | if (ccline.input_fn) |
| 322 | { |
| 323 | xpc.xp_context = ccline.xp_context; |
| 324 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 325 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 326 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 327 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 328 | } |
| 329 | #endif |
| 330 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 331 | /* |
| 332 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 333 | * doing ":@0" when register 0 doesn't contain a CR. |
| 334 | */ |
| 335 | msg_scroll = FALSE; |
| 336 | |
| 337 | State = CMDLINE; |
| 338 | |
| 339 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 340 | { |
| 341 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 342 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 343 | b_im_ptr = &curbuf->b_p_iminsert; |
| 344 | else |
| 345 | b_im_ptr = &curbuf->b_p_imsearch; |
| 346 | if (*b_im_ptr == B_IMODE_LMAP) |
| 347 | State |= LANGMAP; |
| 348 | #ifdef USE_IM_CONTROL |
| 349 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 350 | #endif |
| 351 | } |
| 352 | #ifdef USE_IM_CONTROL |
| 353 | else if (p_imcmdline) |
| 354 | im_set_active(TRUE); |
| 355 | #endif |
| 356 | |
| 357 | #ifdef FEAT_MOUSE |
| 358 | setmouse(); |
| 359 | #endif |
| 360 | #ifdef CURSOR_SHAPE |
| 361 | ui_cursor_shape(); /* may show different cursor shape */ |
| 362 | #endif |
| 363 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 364 | /* When inside an autocommand for writing "exiting" may be set and |
| 365 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 366 | settmode(TMODE_RAW); |
| 367 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 368 | #ifdef FEAT_AUTOCMD |
| 369 | /* Trigger CmdlineEnter autocommands. */ |
| 370 | cmdline_type = firstc == NUL ? '-' : firstc; |
| 371 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER); |
| 372 | #endif |
| 373 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 374 | #ifdef FEAT_CMDHIST |
| 375 | init_history(); |
| 376 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 377 | histype = hist_char2type(firstc); |
| 378 | #endif |
| 379 | |
| 380 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 381 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 382 | #endif |
| 383 | |
Bram Moolenaar | 15a35c4 | 2014-06-25 12:26:46 +0200 | [diff] [blame] | 384 | /* If something above caused an error, reset the flags, we do want to type |
| 385 | * and execute commands. Display may be messed up a bit. */ |
| 386 | if (did_emsg) |
| 387 | redrawcmd(); |
| 388 | did_emsg = FALSE; |
| 389 | got_int = FALSE; |
| 390 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 391 | /* |
| 392 | * Collect the command string, handling editing keys. |
| 393 | */ |
| 394 | for (;;) |
| 395 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 396 | redir_off = TRUE; /* Don't redirect the typed command. |
| 397 | Repeated, because a ":redir" inside |
| 398 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 399 | #ifdef USE_ON_FLY_SCROLL |
| 400 | dont_scroll = FALSE; /* allow scrolling here */ |
| 401 | #endif |
| 402 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 403 | |
| 404 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 405 | |
| 406 | /* Get a character. Ignore K_IGNORE, it should not do anything, such |
| 407 | * as stop completion. */ |
| 408 | do |
| 409 | { |
| 410 | c = safe_vgetc(); |
| 411 | } while (c == K_IGNORE); |
| 412 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 413 | if (KeyTyped) |
| 414 | { |
| 415 | some_key_typed = TRUE; |
| 416 | #ifdef FEAT_RIGHTLEFT |
| 417 | if (cmd_hkmap) |
| 418 | c = hkmap(c); |
| 419 | # ifdef FEAT_FKMAP |
| 420 | if (cmd_fkmap) |
| 421 | c = cmdl_fkmap(c); |
| 422 | # endif |
| 423 | if (cmdmsg_rl && !KeyStuffed) |
| 424 | { |
| 425 | /* Invert horizontal movements and operations. Only when |
| 426 | * typed by the user directly, not when the result of a |
| 427 | * mapping. */ |
| 428 | switch (c) |
| 429 | { |
| 430 | case K_RIGHT: c = K_LEFT; break; |
| 431 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 432 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 433 | case K_LEFT: c = K_RIGHT; break; |
| 434 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 435 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 436 | } |
| 437 | } |
| 438 | #endif |
| 439 | } |
| 440 | |
| 441 | /* |
| 442 | * Ignore got_int when CTRL-C was typed here. |
| 443 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 444 | * ":g/pat/normal /pat" (without the <CR>). |
| 445 | * Don't ignore it for the input() function. |
| 446 | */ |
| 447 | if ((c == Ctrl_C |
| 448 | #ifdef UNIX |
| 449 | || c == intr_char |
| 450 | #endif |
| 451 | ) |
| 452 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 453 | && firstc != '@' |
| 454 | #endif |
| 455 | #ifdef FEAT_EVAL |
| 456 | && !break_ctrl_c |
| 457 | #endif |
| 458 | && !global_busy) |
| 459 | got_int = FALSE; |
| 460 | |
| 461 | #ifdef FEAT_CMDHIST |
| 462 | /* free old command line when finished moving around in the history |
| 463 | * list */ |
| 464 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 465 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 466 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 467 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 468 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 469 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 470 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
| 471 | { |
| 472 | vim_free(lookfor); |
| 473 | lookfor = NULL; |
| 474 | } |
| 475 | #endif |
| 476 | |
| 477 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 478 | * When there are matching completions to select <S-Tab> works like |
| 479 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 480 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 481 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 482 | c = Ctrl_P; |
| 483 | |
| 484 | #ifdef FEAT_WILDMENU |
| 485 | /* Special translations for 'wildmenu' */ |
| 486 | if (did_wild_list && p_wmnu) |
| 487 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 488 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 489 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 490 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 491 | c = Ctrl_N; |
| 492 | } |
| 493 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 494 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 495 | && ccline.cmdpos > 1 |
| 496 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 497 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 498 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 499 | c = K_DOWN; |
| 500 | #endif |
| 501 | |
| 502 | /* free expanded names when finished walking through matches */ |
| 503 | if (xpc.xp_numfiles != -1 |
| 504 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 505 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 506 | && c != Ctrl_L) |
| 507 | { |
| 508 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 509 | did_wild_list = FALSE; |
| 510 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 511 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 512 | #endif |
| 513 | xpc.xp_context = EXPAND_NOTHING; |
| 514 | wim_index = 0; |
| 515 | #ifdef FEAT_WILDMENU |
| 516 | if (p_wmnu && wild_menu_showing != 0) |
| 517 | { |
| 518 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 519 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 520 | |
| 521 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 522 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 523 | |
| 524 | if (wild_menu_showing == WM_SCROLLED) |
| 525 | { |
| 526 | /* Entered command line, move it up */ |
| 527 | cmdline_row--; |
| 528 | redrawcmd(); |
| 529 | } |
| 530 | else if (save_p_ls != -1) |
| 531 | { |
| 532 | /* restore 'laststatus' and 'winminheight' */ |
| 533 | p_ls = save_p_ls; |
| 534 | p_wmh = save_p_wmh; |
| 535 | last_status(FALSE); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 536 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 537 | update_screen(VALID); /* redraw the screen NOW */ |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 538 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 539 | redrawcmd(); |
| 540 | save_p_ls = -1; |
| 541 | } |
| 542 | else |
| 543 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 544 | win_redraw_last_status(topframe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 545 | redraw_statuslines(); |
| 546 | } |
| 547 | KeyTyped = skt; |
| 548 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 549 | if (ccline.input_fn) |
| 550 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 551 | } |
| 552 | #endif |
| 553 | } |
| 554 | |
| 555 | #ifdef FEAT_WILDMENU |
| 556 | /* Special translations for 'wildmenu' */ |
| 557 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 558 | { |
| 559 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 560 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 561 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 562 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 563 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 564 | { |
| 565 | /* Hitting <Up>: Remove one submenu name in front of the |
| 566 | * cursor */ |
| 567 | int found = FALSE; |
| 568 | |
| 569 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 570 | i = 0; |
| 571 | while (--j > 0) |
| 572 | { |
| 573 | /* check for start of menu name */ |
| 574 | if (ccline.cmdbuff[j] == ' ' |
| 575 | && ccline.cmdbuff[j - 1] != '\\') |
| 576 | { |
| 577 | i = j + 1; |
| 578 | break; |
| 579 | } |
| 580 | /* check for start of submenu name */ |
| 581 | if (ccline.cmdbuff[j] == '.' |
| 582 | && ccline.cmdbuff[j - 1] != '\\') |
| 583 | { |
| 584 | if (found) |
| 585 | { |
| 586 | i = j + 1; |
| 587 | break; |
| 588 | } |
| 589 | else |
| 590 | found = TRUE; |
| 591 | } |
| 592 | } |
| 593 | if (i > 0) |
| 594 | cmdline_del(i); |
| 595 | c = p_wc; |
| 596 | xpc.xp_context = EXPAND_NOTHING; |
| 597 | } |
| 598 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 599 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 600 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 601 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 602 | { |
| 603 | char_u upseg[5]; |
| 604 | |
| 605 | upseg[0] = PATHSEP; |
| 606 | upseg[1] = '.'; |
| 607 | upseg[2] = '.'; |
| 608 | upseg[3] = PATHSEP; |
| 609 | upseg[4] = NUL; |
| 610 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 611 | if (c == K_DOWN |
| 612 | && ccline.cmdpos > 0 |
| 613 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 614 | && (ccline.cmdpos < 3 |
| 615 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 616 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 617 | { |
| 618 | /* go down a directory */ |
| 619 | c = p_wc; |
| 620 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 621 | else if (STRNCMP(xpc.xp_pattern, upseg + 1, 3) == 0 && c == K_DOWN) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 622 | { |
| 623 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 624 | int found = FALSE; |
| 625 | |
| 626 | j = ccline.cmdpos; |
| 627 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 628 | while (--j > i) |
| 629 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 630 | #ifdef FEAT_MBYTE |
| 631 | if (has_mbyte) |
| 632 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 633 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 634 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 635 | { |
| 636 | found = TRUE; |
| 637 | break; |
| 638 | } |
| 639 | } |
| 640 | if (found |
| 641 | && ccline.cmdbuff[j - 1] == '.' |
| 642 | && ccline.cmdbuff[j - 2] == '.' |
| 643 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 644 | { |
| 645 | cmdline_del(j - 2); |
| 646 | c = p_wc; |
| 647 | } |
| 648 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 649 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 650 | { |
| 651 | /* go up a directory */ |
| 652 | int found = FALSE; |
| 653 | |
| 654 | j = ccline.cmdpos - 1; |
| 655 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 656 | while (--j > i) |
| 657 | { |
| 658 | #ifdef FEAT_MBYTE |
| 659 | if (has_mbyte) |
| 660 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 661 | #endif |
| 662 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 663 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 664 | && vim_strchr((char_u *)" *?[{`$%#", |
| 665 | ccline.cmdbuff[j + 1]) == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 666 | #endif |
| 667 | ) |
| 668 | { |
| 669 | if (found) |
| 670 | { |
| 671 | i = j + 1; |
| 672 | break; |
| 673 | } |
| 674 | else |
| 675 | found = TRUE; |
| 676 | } |
| 677 | } |
| 678 | |
| 679 | if (!found) |
| 680 | j = i; |
| 681 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 682 | j += 4; |
| 683 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 684 | && j == i) |
| 685 | j += 3; |
| 686 | else |
| 687 | j = 0; |
| 688 | if (j > 0) |
| 689 | { |
| 690 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 691 | * machine-specific stuff here and in upseg init */ |
| 692 | cmdline_del(j); |
| 693 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 694 | } |
| 695 | else if (ccline.cmdpos > i) |
| 696 | cmdline_del(i); |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 697 | |
| 698 | /* Now complete in the new directory. Set KeyTyped in case the |
| 699 | * Up key came from a mapping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 700 | c = p_wc; |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 701 | KeyTyped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 702 | } |
| 703 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 704 | |
| 705 | #endif /* FEAT_WILDMENU */ |
| 706 | |
| 707 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 708 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 709 | if (c == Ctrl_BSL) |
| 710 | { |
| 711 | ++no_mapping; |
| 712 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 713 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 714 | --no_mapping; |
| 715 | --allow_keys; |
Bram Moolenaar | b735681 | 2012-10-11 04:04:37 +0200 | [diff] [blame] | 716 | /* CTRL-\ e doesn't work when obtaining an expression, unless it |
| 717 | * is in a mapping. */ |
| 718 | if (c != Ctrl_N && c != Ctrl_G && (c != 'e' |
| 719 | || (ccline.cmdfirstc == '=' && KeyTyped))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 720 | { |
| 721 | vungetc(c); |
| 722 | c = Ctrl_BSL; |
| 723 | } |
| 724 | #ifdef FEAT_EVAL |
| 725 | else if (c == 'e') |
| 726 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 727 | char_u *p = NULL; |
| 728 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 729 | |
| 730 | /* |
| 731 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 732 | * Need to save and restore the current command line, to be |
| 733 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 734 | */ |
| 735 | if (ccline.cmdpos == ccline.cmdlen) |
| 736 | new_cmdpos = 99999; /* keep it at the end */ |
| 737 | else |
| 738 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 739 | |
| 740 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 741 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 742 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 743 | if (c == '=') |
| 744 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 745 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 746 | * to avoid nasty things like going to another buffer when |
| 747 | * evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 748 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 749 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 750 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 751 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 752 | restore_cmdline(&save_ccline); |
| 753 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 754 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 755 | { |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 756 | len = (int)STRLEN(p); |
| 757 | if (realloc_cmdbuff(len + 1) == OK) |
| 758 | { |
| 759 | ccline.cmdlen = len; |
| 760 | STRCPY(ccline.cmdbuff, p); |
| 761 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 762 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 763 | /* Restore the cursor or use the position set with |
| 764 | * set_cmdline_pos(). */ |
| 765 | if (new_cmdpos > ccline.cmdlen) |
| 766 | ccline.cmdpos = ccline.cmdlen; |
| 767 | else |
| 768 | ccline.cmdpos = new_cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 769 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 770 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 771 | redrawcmd(); |
| 772 | goto cmdline_changed; |
| 773 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 774 | } |
| 775 | } |
| 776 | beep_flush(); |
Bram Moolenaar | 66b4bf8 | 2010-11-16 14:06:08 +0100 | [diff] [blame] | 777 | got_int = FALSE; /* don't abandon the command line */ |
| 778 | did_emsg = FALSE; |
| 779 | emsg_on_display = FALSE; |
| 780 | redrawcmd(); |
| 781 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 782 | } |
| 783 | #endif |
| 784 | else |
| 785 | { |
| 786 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 787 | restart_edit = 'a'; |
| 788 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 789 | in history */ |
| 790 | goto returncmd; /* back to Normal mode */ |
| 791 | } |
| 792 | } |
| 793 | |
| 794 | #ifdef FEAT_CMDWIN |
| 795 | if (c == cedit_key || c == K_CMDWIN) |
| 796 | { |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 797 | if (ex_normal_busy == 0 && got_int == FALSE) |
| 798 | { |
| 799 | /* |
| 800 | * Open a window to edit the command line (and history). |
| 801 | */ |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 802 | c = open_cmdwin(); |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 803 | some_key_typed = TRUE; |
| 804 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 805 | } |
| 806 | # ifdef FEAT_DIGRAPHS |
| 807 | else |
| 808 | # endif |
| 809 | #endif |
| 810 | #ifdef FEAT_DIGRAPHS |
| 811 | c = do_digraph(c); |
| 812 | #endif |
| 813 | |
| 814 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 815 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 816 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 817 | /* In Ex mode a backslash escapes a newline. */ |
| 818 | if (exmode_active |
| 819 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 820 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 821 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 822 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 823 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 824 | if (c == K_KENTER) |
| 825 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 826 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 827 | else |
| 828 | { |
| 829 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 830 | truncate the cmdline now. */ |
| 831 | if (ccheck_abbr(c + ABBR_OFF)) |
| 832 | goto cmdline_changed; |
| 833 | if (!cmd_silent) |
| 834 | { |
| 835 | windgoto(msg_row, 0); |
| 836 | out_flush(); |
| 837 | } |
| 838 | break; |
| 839 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | /* |
| 843 | * Completion for 'wildchar' or 'wildcharm' key. |
| 844 | * - hitting <ESC> twice means: abandon command line. |
| 845 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 846 | * typed, not when it comes from a macro |
| 847 | */ |
| 848 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 849 | { |
| 850 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 851 | { |
| 852 | /* if 'wildmode' contains "list" may still need to list */ |
| 853 | if (xpc.xp_numfiles > 1 |
| 854 | && !did_wild_list |
| 855 | && (wim_flags[wim_index] & WIM_LIST)) |
| 856 | { |
| 857 | (void)showmatches(&xpc, FALSE); |
| 858 | redrawcmd(); |
| 859 | did_wild_list = TRUE; |
| 860 | } |
| 861 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 862 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 863 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 864 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 865 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 866 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 867 | else |
| 868 | res = OK; /* don't insert 'wildchar' now */ |
| 869 | } |
| 870 | else /* typed p_wc first time */ |
| 871 | { |
| 872 | wim_index = 0; |
| 873 | j = ccline.cmdpos; |
| 874 | /* if 'wildmode' first contains "longest", get longest |
| 875 | * common part */ |
| 876 | if (wim_flags[0] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 877 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 878 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 879 | else |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 880 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP, |
| 881 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 882 | |
| 883 | /* if interrupted while completing, behave like it failed */ |
| 884 | if (got_int) |
| 885 | { |
| 886 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 887 | got_int = FALSE; /* don't abandon the command line */ |
| 888 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 889 | #ifdef FEAT_WILDMENU |
| 890 | xpc.xp_context = EXPAND_NOTHING; |
| 891 | #endif |
| 892 | goto cmdline_changed; |
| 893 | } |
| 894 | |
| 895 | /* when more than one match, and 'wildmode' first contains |
| 896 | * "list", or no change and 'wildmode' contains "longest,list", |
| 897 | * list all matches */ |
| 898 | if (res == OK && xpc.xp_numfiles > 1) |
| 899 | { |
| 900 | /* a "longest" that didn't do anything is skipped (but not |
| 901 | * "list:longest") */ |
| 902 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 903 | wim_index = 1; |
| 904 | if ((wim_flags[wim_index] & WIM_LIST) |
| 905 | #ifdef FEAT_WILDMENU |
| 906 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 907 | #endif |
| 908 | ) |
| 909 | { |
| 910 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 911 | { |
| 912 | #ifdef FEAT_WILDMENU |
| 913 | int p_wmnu_save = p_wmnu; |
| 914 | p_wmnu = 0; |
| 915 | #endif |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 916 | /* remove match */ |
| 917 | nextwild(&xpc, WILD_PREV, 0, firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 918 | #ifdef FEAT_WILDMENU |
| 919 | p_wmnu = p_wmnu_save; |
| 920 | #endif |
| 921 | } |
| 922 | #ifdef FEAT_WILDMENU |
| 923 | (void)showmatches(&xpc, p_wmnu |
| 924 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 925 | #else |
| 926 | (void)showmatches(&xpc, FALSE); |
| 927 | #endif |
| 928 | redrawcmd(); |
| 929 | did_wild_list = TRUE; |
| 930 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 931 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 932 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 933 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 934 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 935 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 936 | } |
| 937 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 938 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 939 | } |
| 940 | #ifdef FEAT_WILDMENU |
| 941 | else if (xpc.xp_numfiles == -1) |
| 942 | xpc.xp_context = EXPAND_NOTHING; |
| 943 | #endif |
| 944 | } |
| 945 | if (wim_index < 3) |
| 946 | ++wim_index; |
| 947 | if (c == ESC) |
| 948 | gotesc = TRUE; |
| 949 | if (res == OK) |
| 950 | goto cmdline_changed; |
| 951 | } |
| 952 | |
| 953 | gotesc = FALSE; |
| 954 | |
| 955 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 956 | if (c == K_S_TAB && KeyTyped) |
| 957 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 958 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK |
| 959 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK |
| 960 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 961 | goto cmdline_changed; |
| 962 | } |
| 963 | |
| 964 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 965 | c = NL; |
| 966 | |
| 967 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 968 | |
| 969 | /* |
| 970 | * Big switch for a typed command line character. |
| 971 | */ |
| 972 | switch (c) |
| 973 | { |
| 974 | case K_BS: |
| 975 | case Ctrl_H: |
| 976 | case K_DEL: |
| 977 | case K_KDEL: |
| 978 | case Ctrl_W: |
| 979 | #ifdef FEAT_FKMAP |
| 980 | if (cmd_fkmap && c == K_BS) |
| 981 | c = K_DEL; |
| 982 | #endif |
| 983 | if (c == K_KDEL) |
| 984 | c = K_DEL; |
| 985 | |
| 986 | /* |
| 987 | * delete current character is the same as backspace on next |
| 988 | * character, except at end of line |
| 989 | */ |
| 990 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 991 | ++ccline.cmdpos; |
| 992 | #ifdef FEAT_MBYTE |
| 993 | if (has_mbyte && c == K_DEL) |
| 994 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 995 | ccline.cmdbuff + ccline.cmdpos); |
| 996 | #endif |
| 997 | if (ccline.cmdpos > 0) |
| 998 | { |
| 999 | char_u *p; |
| 1000 | |
| 1001 | j = ccline.cmdpos; |
| 1002 | p = ccline.cmdbuff + j; |
| 1003 | #ifdef FEAT_MBYTE |
| 1004 | if (has_mbyte) |
| 1005 | { |
| 1006 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1007 | if (c == Ctrl_W) |
| 1008 | { |
| 1009 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 1010 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1011 | i = mb_get_class(p); |
| 1012 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 1013 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1014 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1015 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | else |
| 1019 | #endif |
| 1020 | if (c == Ctrl_W) |
| 1021 | { |
| 1022 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 1023 | --p; |
| 1024 | i = vim_iswordc(p[-1]); |
| 1025 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 1026 | && vim_iswordc(p[-1]) == i) |
| 1027 | --p; |
| 1028 | } |
| 1029 | else |
| 1030 | --p; |
| 1031 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 1032 | ccline.cmdlen -= j - ccline.cmdpos; |
| 1033 | i = ccline.cmdpos; |
| 1034 | while (i < ccline.cmdlen) |
| 1035 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1036 | |
| 1037 | /* Truncate at the end, required for multi-byte chars. */ |
| 1038 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1039 | #ifdef FEAT_SEARCH_EXTRA |
| 1040 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1041 | { |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1042 | search_start = save_cursor; |
| 1043 | /* save view settings, so that the screen |
| 1044 | * won't be restored at the wrong position */ |
| 1045 | old_curswant = init_curswant; |
| 1046 | old_leftcol = init_leftcol; |
| 1047 | old_topline = init_topline; |
| 1048 | # ifdef FEAT_DIFF |
| 1049 | old_topfill = init_topfill; |
| 1050 | # endif |
| 1051 | old_botline = init_botline; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1052 | } |
| 1053 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1054 | redrawcmd(); |
| 1055 | } |
| 1056 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 1057 | && ccline.cmdprompt == NULL && indent == 0) |
| 1058 | { |
| 1059 | /* In ex and debug mode it doesn't make sense to return. */ |
| 1060 | if (exmode_active |
| 1061 | #ifdef FEAT_EVAL |
| 1062 | || ccline.cmdfirstc == '>' |
| 1063 | #endif |
| 1064 | ) |
| 1065 | goto cmdline_not_changed; |
| 1066 | |
| 1067 | vim_free(ccline.cmdbuff); /* no commandline to return */ |
| 1068 | ccline.cmdbuff = NULL; |
| 1069 | if (!cmd_silent) |
| 1070 | { |
| 1071 | #ifdef FEAT_RIGHTLEFT |
| 1072 | if (cmdmsg_rl) |
| 1073 | msg_col = Columns; |
| 1074 | else |
| 1075 | #endif |
| 1076 | msg_col = 0; |
| 1077 | msg_putchar(' '); /* delete ':' */ |
| 1078 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1079 | #ifdef FEAT_SEARCH_EXTRA |
| 1080 | if (ccline.cmdlen == 0) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1081 | search_start = save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1082 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1083 | redraw_cmdline = TRUE; |
| 1084 | goto returncmd; /* back to cmd mode */ |
| 1085 | } |
| 1086 | goto cmdline_changed; |
| 1087 | |
| 1088 | case K_INS: |
| 1089 | case K_KINS: |
| 1090 | #ifdef FEAT_FKMAP |
| 1091 | /* if Farsi mode set, we are in reverse insert mode - |
| 1092 | Do not change the mode */ |
| 1093 | if (cmd_fkmap) |
| 1094 | beep_flush(); |
| 1095 | else |
| 1096 | #endif |
| 1097 | ccline.overstrike = !ccline.overstrike; |
| 1098 | #ifdef CURSOR_SHAPE |
| 1099 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1100 | #endif |
| 1101 | goto cmdline_not_changed; |
| 1102 | |
| 1103 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1104 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1105 | { |
| 1106 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1107 | State ^= LANGMAP; |
| 1108 | #ifdef USE_IM_CONTROL |
| 1109 | im_set_active(FALSE); /* Disable input method */ |
| 1110 | #endif |
| 1111 | if (b_im_ptr != NULL) |
| 1112 | { |
| 1113 | if (State & LANGMAP) |
| 1114 | *b_im_ptr = B_IMODE_LMAP; |
| 1115 | else |
| 1116 | *b_im_ptr = B_IMODE_NONE; |
| 1117 | } |
| 1118 | } |
| 1119 | #ifdef USE_IM_CONTROL |
| 1120 | else |
| 1121 | { |
| 1122 | /* There are no ":lmap" mappings, toggle IM. When |
| 1123 | * 'imdisable' is set don't try getting the status, it's |
| 1124 | * always off. */ |
| 1125 | if ((p_imdisable && b_im_ptr != NULL) |
| 1126 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1127 | { |
| 1128 | im_set_active(FALSE); /* Disable input method */ |
| 1129 | if (b_im_ptr != NULL) |
| 1130 | *b_im_ptr = B_IMODE_NONE; |
| 1131 | } |
| 1132 | else |
| 1133 | { |
| 1134 | im_set_active(TRUE); /* Enable input method */ |
| 1135 | if (b_im_ptr != NULL) |
| 1136 | *b_im_ptr = B_IMODE_IM; |
| 1137 | } |
| 1138 | } |
| 1139 | #endif |
| 1140 | if (b_im_ptr != NULL) |
| 1141 | { |
| 1142 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1143 | set_iminsert_global(); |
| 1144 | else |
| 1145 | set_imsearch_global(); |
| 1146 | } |
| 1147 | #ifdef CURSOR_SHAPE |
| 1148 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1149 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 1150 | #if defined(FEAT_KEYMAP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1151 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1152 | status_redraw_curbuf(); |
| 1153 | #endif |
| 1154 | goto cmdline_not_changed; |
| 1155 | |
| 1156 | /* case '@': only in very old vi */ |
| 1157 | case Ctrl_U: |
| 1158 | /* delete all characters left of the cursor */ |
| 1159 | j = ccline.cmdpos; |
| 1160 | ccline.cmdlen -= j; |
| 1161 | i = ccline.cmdpos = 0; |
| 1162 | while (i < ccline.cmdlen) |
| 1163 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1164 | /* Truncate at the end, required for multi-byte chars. */ |
| 1165 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1166 | #ifdef FEAT_SEARCH_EXTRA |
| 1167 | if (ccline.cmdlen == 0) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1168 | search_start = save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1169 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1170 | redrawcmd(); |
| 1171 | goto cmdline_changed; |
| 1172 | |
| 1173 | #ifdef FEAT_CLIPBOARD |
| 1174 | case Ctrl_Y: |
| 1175 | /* Copy the modeless selection, if there is one. */ |
| 1176 | if (clip_star.state != SELECT_CLEARED) |
| 1177 | { |
| 1178 | if (clip_star.state == SELECT_DONE) |
| 1179 | clip_copy_modeless_selection(TRUE); |
| 1180 | goto cmdline_not_changed; |
| 1181 | } |
| 1182 | break; |
| 1183 | #endif |
| 1184 | |
| 1185 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1186 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1187 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1188 | * ":normal" runs out of characters. */ |
| 1189 | if (exmode_active |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 1190 | && (ex_normal_busy == 0 || typebuf.tb_len > 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1191 | goto cmdline_not_changed; |
| 1192 | |
| 1193 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1194 | putting it in history */ |
| 1195 | goto returncmd; /* back to cmd mode */ |
| 1196 | |
| 1197 | case Ctrl_R: /* insert register */ |
| 1198 | #ifdef USE_ON_FLY_SCROLL |
| 1199 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1200 | #endif |
| 1201 | putcmdline('"', TRUE); |
| 1202 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1203 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1204 | if (i == Ctrl_O) |
| 1205 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1206 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1207 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1208 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1209 | --no_mapping; |
| 1210 | #ifdef FEAT_EVAL |
| 1211 | /* |
| 1212 | * Insert the result of an expression. |
| 1213 | * Need to save the current command line, to be able to enter |
| 1214 | * a new one... |
| 1215 | */ |
| 1216 | new_cmdpos = -1; |
| 1217 | if (c == '=') |
| 1218 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1219 | if (ccline.cmdfirstc == '=')/* can't do this recursively */ |
| 1220 | { |
| 1221 | beep_flush(); |
| 1222 | c = ESC; |
| 1223 | } |
| 1224 | else |
| 1225 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1226 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1227 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1228 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | #endif |
| 1232 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1233 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1234 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1235 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1236 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1237 | /* When there was a serious error abort getting the |
| 1238 | * command line. */ |
| 1239 | if (aborting()) |
| 1240 | { |
| 1241 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1242 | putting it in history */ |
| 1243 | goto returncmd; /* back to cmd mode */ |
| 1244 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1245 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1246 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1247 | #ifdef FEAT_EVAL |
| 1248 | if (new_cmdpos >= 0) |
| 1249 | { |
| 1250 | /* set_cmdline_pos() was used */ |
| 1251 | if (new_cmdpos > ccline.cmdlen) |
| 1252 | ccline.cmdpos = ccline.cmdlen; |
| 1253 | else |
| 1254 | ccline.cmdpos = new_cmdpos; |
| 1255 | } |
| 1256 | #endif |
| 1257 | } |
| 1258 | redrawcmd(); |
| 1259 | goto cmdline_changed; |
| 1260 | |
| 1261 | case Ctrl_D: |
| 1262 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1263 | break; /* Use ^D as normal char instead */ |
| 1264 | |
| 1265 | redrawcmd(); |
| 1266 | continue; /* don't do incremental search now */ |
| 1267 | |
| 1268 | case K_RIGHT: |
| 1269 | case K_S_RIGHT: |
| 1270 | case K_C_RIGHT: |
| 1271 | do |
| 1272 | { |
| 1273 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1274 | break; |
| 1275 | i = cmdline_charsize(ccline.cmdpos); |
| 1276 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1277 | break; |
| 1278 | ccline.cmdspos += i; |
| 1279 | #ifdef FEAT_MBYTE |
| 1280 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1281 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1282 | + ccline.cmdpos); |
| 1283 | else |
| 1284 | #endif |
| 1285 | ++ccline.cmdpos; |
| 1286 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1287 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 1288 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1289 | && ccline.cmdbuff[ccline.cmdpos] != ' '); |
| 1290 | #ifdef FEAT_MBYTE |
| 1291 | if (has_mbyte) |
| 1292 | set_cmdspos_cursor(); |
| 1293 | #endif |
| 1294 | goto cmdline_not_changed; |
| 1295 | |
| 1296 | case K_LEFT: |
| 1297 | case K_S_LEFT: |
| 1298 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1299 | if (ccline.cmdpos == 0) |
| 1300 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1301 | do |
| 1302 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1303 | --ccline.cmdpos; |
| 1304 | #ifdef FEAT_MBYTE |
| 1305 | if (has_mbyte) /* move to first byte of char */ |
| 1306 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1307 | ccline.cmdbuff + ccline.cmdpos); |
| 1308 | #endif |
| 1309 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1310 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1311 | while (ccline.cmdpos > 0 |
| 1312 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1313 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1314 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1315 | #ifdef FEAT_MBYTE |
| 1316 | if (has_mbyte) |
| 1317 | set_cmdspos_cursor(); |
| 1318 | #endif |
| 1319 | goto cmdline_not_changed; |
| 1320 | |
| 1321 | case K_IGNORE: |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1322 | /* Ignore mouse event or open_cmdwin() result. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1323 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1324 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1325 | #ifdef FEAT_GUI_W32 |
| 1326 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1327 | * cancelled. */ |
| 1328 | case K_F4: |
| 1329 | if (mod_mask == MOD_MASK_ALT) |
| 1330 | { |
| 1331 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1332 | goto cmdline_not_changed; |
| 1333 | } |
| 1334 | break; |
| 1335 | #endif |
| 1336 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1337 | #ifdef FEAT_MOUSE |
| 1338 | case K_MIDDLEDRAG: |
| 1339 | case K_MIDDLERELEASE: |
| 1340 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1341 | |
| 1342 | case K_MIDDLEMOUSE: |
| 1343 | # ifdef FEAT_GUI |
| 1344 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1345 | if (!gui.in_use) |
| 1346 | # endif |
| 1347 | if (!mouse_has(MOUSE_COMMAND)) |
| 1348 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1349 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1350 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1351 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1352 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1353 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1354 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1355 | redrawcmd(); |
| 1356 | goto cmdline_changed; |
| 1357 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1358 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1359 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1360 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1361 | redrawcmd(); |
| 1362 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1363 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1364 | |
| 1365 | case K_LEFTDRAG: |
| 1366 | case K_LEFTRELEASE: |
| 1367 | case K_RIGHTDRAG: |
| 1368 | case K_RIGHTRELEASE: |
| 1369 | /* Ignore drag and release events when the button-down wasn't |
| 1370 | * seen before. */ |
| 1371 | if (ignore_drag_release) |
| 1372 | goto cmdline_not_changed; |
| 1373 | /* FALLTHROUGH */ |
| 1374 | case K_LEFTMOUSE: |
| 1375 | case K_RIGHTMOUSE: |
| 1376 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1377 | ignore_drag_release = TRUE; |
| 1378 | else |
| 1379 | ignore_drag_release = FALSE; |
| 1380 | # ifdef FEAT_GUI |
| 1381 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1382 | if (!gui.in_use) |
| 1383 | # endif |
| 1384 | if (!mouse_has(MOUSE_COMMAND)) |
| 1385 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1386 | # ifdef FEAT_CLIPBOARD |
| 1387 | if (mouse_row < cmdline_row && clip_star.available) |
| 1388 | { |
| 1389 | int button, is_click, is_drag; |
| 1390 | |
| 1391 | /* |
| 1392 | * Handle modeless selection. |
| 1393 | */ |
| 1394 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1395 | &is_click, &is_drag); |
| 1396 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1397 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1398 | { |
| 1399 | /* Translate shift-left to right button. */ |
| 1400 | button = MOUSE_RIGHT; |
| 1401 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1402 | } |
| 1403 | clip_modeless(button, is_click, is_drag); |
| 1404 | goto cmdline_not_changed; |
| 1405 | } |
| 1406 | # endif |
| 1407 | |
| 1408 | set_cmdspos(); |
| 1409 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 1410 | ++ccline.cmdpos) |
| 1411 | { |
| 1412 | i = cmdline_charsize(ccline.cmdpos); |
| 1413 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 1414 | && mouse_col < ccline.cmdspos % Columns + i) |
| 1415 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1416 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1417 | if (has_mbyte) |
| 1418 | { |
| 1419 | /* Count ">" for double-wide char that doesn't fit. */ |
| 1420 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1421 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1422 | + ccline.cmdpos) - 1; |
| 1423 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1424 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1425 | ccline.cmdspos += i; |
| 1426 | } |
| 1427 | goto cmdline_not_changed; |
| 1428 | |
| 1429 | /* Mouse scroll wheel: ignored here */ |
| 1430 | case K_MOUSEDOWN: |
| 1431 | case K_MOUSEUP: |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1432 | case K_MOUSELEFT: |
| 1433 | case K_MOUSERIGHT: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1434 | /* Alternate buttons ignored here */ |
| 1435 | case K_X1MOUSE: |
| 1436 | case K_X1DRAG: |
| 1437 | case K_X1RELEASE: |
| 1438 | case K_X2MOUSE: |
| 1439 | case K_X2DRAG: |
| 1440 | case K_X2RELEASE: |
| 1441 | goto cmdline_not_changed; |
| 1442 | |
| 1443 | #endif /* FEAT_MOUSE */ |
| 1444 | |
| 1445 | #ifdef FEAT_GUI |
| 1446 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 1447 | case K_LEFTRELEASE_NM: |
| 1448 | goto cmdline_not_changed; |
| 1449 | |
| 1450 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1451 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1452 | { |
| 1453 | gui_do_scroll(); |
| 1454 | redrawcmd(); |
| 1455 | } |
| 1456 | goto cmdline_not_changed; |
| 1457 | |
| 1458 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1459 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1460 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1461 | gui_do_horiz_scroll(scrollbar_value, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1462 | redrawcmd(); |
| 1463 | } |
| 1464 | goto cmdline_not_changed; |
| 1465 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1466 | #ifdef FEAT_GUI_TABLINE |
| 1467 | case K_TABLINE: |
| 1468 | case K_TABMENU: |
| 1469 | /* Don't want to change any tabs here. Make sure the same tab |
| 1470 | * is still selected. */ |
| 1471 | if (gui_use_tabline()) |
| 1472 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 1473 | goto cmdline_not_changed; |
| 1474 | #endif |
| 1475 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1476 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1477 | goto cmdline_not_changed; |
| 1478 | |
| 1479 | case Ctrl_B: /* begin of command line */ |
| 1480 | case K_HOME: |
| 1481 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1482 | case K_S_HOME: |
| 1483 | case K_C_HOME: |
| 1484 | ccline.cmdpos = 0; |
| 1485 | set_cmdspos(); |
| 1486 | goto cmdline_not_changed; |
| 1487 | |
| 1488 | case Ctrl_E: /* end of command line */ |
| 1489 | case K_END: |
| 1490 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1491 | case K_S_END: |
| 1492 | case K_C_END: |
| 1493 | ccline.cmdpos = ccline.cmdlen; |
| 1494 | set_cmdspos_cursor(); |
| 1495 | goto cmdline_not_changed; |
| 1496 | |
| 1497 | case Ctrl_A: /* all matches */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1498 | if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1499 | break; |
| 1500 | goto cmdline_changed; |
| 1501 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1502 | case Ctrl_L: |
| 1503 | #ifdef FEAT_SEARCH_EXTRA |
| 1504 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1505 | { |
| 1506 | /* Add a character from under the cursor for 'incsearch' */ |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1507 | if (did_incsearch) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1508 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1509 | curwin->w_cursor = match_end; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1510 | if (!EQUAL_POS(curwin->w_cursor, search_start)) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1511 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1512 | c = gchar_cursor(); |
| 1513 | /* If 'ignorecase' and 'smartcase' are set and the |
| 1514 | * command line has no uppercase characters, convert |
| 1515 | * the character to lowercase */ |
| 1516 | if (p_ic && p_scs |
| 1517 | && !pat_has_uppercase(ccline.cmdbuff)) |
| 1518 | c = MB_TOLOWER(c); |
| 1519 | if (c != NUL) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1520 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1521 | if (c == firstc || vim_strchr((char_u *)( |
Bram Moolenaar | a693d05 | 2017-06-29 22:23:06 +0200 | [diff] [blame] | 1522 | p_magic ? "\\~^$.*[" : "\\^$"), c) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1523 | != NULL) |
| 1524 | { |
| 1525 | /* put a backslash before special |
| 1526 | * characters */ |
| 1527 | stuffcharReadbuff(c); |
| 1528 | c = '\\'; |
| 1529 | } |
| 1530 | break; |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1531 | } |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1532 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1533 | } |
| 1534 | goto cmdline_not_changed; |
| 1535 | } |
| 1536 | #endif |
| 1537 | |
| 1538 | /* completion: longest common part */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1539 | if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1540 | break; |
| 1541 | goto cmdline_changed; |
| 1542 | |
| 1543 | case Ctrl_N: /* next match */ |
| 1544 | case Ctrl_P: /* previous match */ |
Bram Moolenaar | 7df0f63 | 2016-08-26 19:56:00 +0200 | [diff] [blame] | 1545 | if (xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1546 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1547 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, |
| 1548 | 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1549 | break; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1550 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1551 | } |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1552 | /* FALLTHROUGH */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1553 | |
| 1554 | #ifdef FEAT_CMDHIST |
| 1555 | case K_UP: |
| 1556 | case K_DOWN: |
| 1557 | case K_S_UP: |
| 1558 | case K_S_DOWN: |
| 1559 | case K_PAGEUP: |
| 1560 | case K_KPAGEUP: |
| 1561 | case K_PAGEDOWN: |
| 1562 | case K_KPAGEDOWN: |
| 1563 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 1564 | goto cmdline_not_changed; |
| 1565 | |
| 1566 | i = hiscnt; |
| 1567 | |
| 1568 | /* save current command string so it can be restored later */ |
| 1569 | if (lookfor == NULL) |
| 1570 | { |
| 1571 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 1572 | goto cmdline_not_changed; |
| 1573 | lookfor[ccline.cmdpos] = NUL; |
| 1574 | } |
| 1575 | |
| 1576 | j = (int)STRLEN(lookfor); |
| 1577 | for (;;) |
| 1578 | { |
| 1579 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1580 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1581 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1582 | { |
| 1583 | if (hiscnt == hislen) /* first time */ |
| 1584 | hiscnt = hisidx[histype]; |
| 1585 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 1586 | hiscnt = hislen - 1; |
| 1587 | else if (hiscnt != hisidx[histype] + 1) |
| 1588 | --hiscnt; |
| 1589 | else /* at top of list */ |
| 1590 | { |
| 1591 | hiscnt = i; |
| 1592 | break; |
| 1593 | } |
| 1594 | } |
| 1595 | else /* one step forwards */ |
| 1596 | { |
| 1597 | /* on last entry, clear the line */ |
| 1598 | if (hiscnt == hisidx[histype]) |
| 1599 | { |
| 1600 | hiscnt = hislen; |
| 1601 | break; |
| 1602 | } |
| 1603 | |
| 1604 | /* not on a history line, nothing to do */ |
| 1605 | if (hiscnt == hislen) |
| 1606 | break; |
| 1607 | if (hiscnt == hislen - 1) /* wrap around */ |
| 1608 | hiscnt = 0; |
| 1609 | else |
| 1610 | ++hiscnt; |
| 1611 | } |
| 1612 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 1613 | { |
| 1614 | hiscnt = i; |
| 1615 | break; |
| 1616 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1617 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1618 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1619 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 1620 | lookfor, (size_t)j) == 0) |
| 1621 | break; |
| 1622 | } |
| 1623 | |
| 1624 | if (hiscnt != i) /* jumped to other entry */ |
| 1625 | { |
| 1626 | char_u *p; |
| 1627 | int len; |
| 1628 | int old_firstc; |
| 1629 | |
| 1630 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1631 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1632 | if (hiscnt == hislen) |
| 1633 | p = lookfor; /* back to the old one */ |
| 1634 | else |
| 1635 | p = history[histype][hiscnt].hisstr; |
| 1636 | |
| 1637 | if (histype == HIST_SEARCH |
| 1638 | && p != lookfor |
| 1639 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 1640 | { |
| 1641 | /* Correct for the separator character used when |
| 1642 | * adding the history entry vs the one used now. |
| 1643 | * First loop: count length. |
| 1644 | * Second loop: copy the characters. */ |
| 1645 | for (i = 0; i <= 1; ++i) |
| 1646 | { |
| 1647 | len = 0; |
| 1648 | for (j = 0; p[j] != NUL; ++j) |
| 1649 | { |
| 1650 | /* Replace old sep with new sep, unless it is |
| 1651 | * escaped. */ |
| 1652 | if (p[j] == old_firstc |
| 1653 | && (j == 0 || p[j - 1] != '\\')) |
| 1654 | { |
| 1655 | if (i > 0) |
| 1656 | ccline.cmdbuff[len] = firstc; |
| 1657 | } |
| 1658 | else |
| 1659 | { |
| 1660 | /* Escape new sep, unless it is already |
| 1661 | * escaped. */ |
| 1662 | if (p[j] == firstc |
| 1663 | && (j == 0 || p[j - 1] != '\\')) |
| 1664 | { |
| 1665 | if (i > 0) |
| 1666 | ccline.cmdbuff[len] = '\\'; |
| 1667 | ++len; |
| 1668 | } |
| 1669 | if (i > 0) |
| 1670 | ccline.cmdbuff[len] = p[j]; |
| 1671 | } |
| 1672 | ++len; |
| 1673 | } |
| 1674 | if (i == 0) |
| 1675 | { |
| 1676 | alloc_cmdbuff(len); |
| 1677 | if (ccline.cmdbuff == NULL) |
| 1678 | goto returncmd; |
| 1679 | } |
| 1680 | } |
| 1681 | ccline.cmdbuff[len] = NUL; |
| 1682 | } |
| 1683 | else |
| 1684 | { |
| 1685 | alloc_cmdbuff((int)STRLEN(p)); |
| 1686 | if (ccline.cmdbuff == NULL) |
| 1687 | goto returncmd; |
| 1688 | STRCPY(ccline.cmdbuff, p); |
| 1689 | } |
| 1690 | |
| 1691 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 1692 | redrawcmd(); |
| 1693 | goto cmdline_changed; |
| 1694 | } |
| 1695 | beep_flush(); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1696 | #endif |
| 1697 | goto cmdline_not_changed; |
| 1698 | |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1699 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1700 | case Ctrl_G: /* next match */ |
| 1701 | case Ctrl_T: /* previous match */ |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1702 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1703 | { |
| 1704 | pos_T t; |
| 1705 | int search_flags = SEARCH_KEEP + SEARCH_NOOF |
| 1706 | + SEARCH_PEEK; |
| 1707 | |
| 1708 | if (char_avail()) |
| 1709 | continue; |
| 1710 | cursor_off(); |
| 1711 | out_flush(); |
| 1712 | if (c == Ctrl_G) |
| 1713 | { |
| 1714 | t = match_end; |
| 1715 | search_flags += SEARCH_COL; |
| 1716 | } |
| 1717 | else |
| 1718 | t = match_start; |
| 1719 | ++emsg_off; |
| 1720 | i = searchit(curwin, curbuf, &t, |
| 1721 | c == Ctrl_G ? FORWARD : BACKWARD, |
| 1722 | ccline.cmdbuff, count, search_flags, |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1723 | RE_SEARCH, 0, NULL, NULL); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1724 | --emsg_off; |
| 1725 | if (i) |
| 1726 | { |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1727 | search_start = match_start; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1728 | match_end = t; |
| 1729 | match_start = t; |
| 1730 | if (c == Ctrl_T && firstc == '/') |
| 1731 | { |
| 1732 | /* move just before the current match, so that |
| 1733 | * when nv_search finishes the cursor will be |
| 1734 | * put back on the match */ |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1735 | search_start = t; |
| 1736 | (void)decl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1737 | } |
Bram Moolenaar | da5116d | 2017-07-01 23:11:17 +0200 | [diff] [blame] | 1738 | else if (c == Ctrl_G && firstc == '?') |
| 1739 | { |
| 1740 | /* move just after the current match, so that |
| 1741 | * when nv_search finishes the cursor will be |
| 1742 | * put back on the match */ |
| 1743 | search_start = t; |
| 1744 | (void)incl(&search_start); |
| 1745 | } |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1746 | if (LT_POS(t, search_start) && c == Ctrl_G) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1747 | { |
| 1748 | /* wrap around */ |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1749 | search_start = t; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1750 | if (firstc == '?') |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1751 | (void)incl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1752 | else |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1753 | (void)decl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1754 | } |
| 1755 | |
| 1756 | set_search_match(&match_end); |
| 1757 | curwin->w_cursor = match_start; |
| 1758 | changed_cline_bef_curs(); |
| 1759 | update_topline(); |
| 1760 | validate_cursor(); |
| 1761 | highlight_match = TRUE; |
| 1762 | old_curswant = curwin->w_curswant; |
| 1763 | old_leftcol = curwin->w_leftcol; |
| 1764 | old_topline = curwin->w_topline; |
| 1765 | # ifdef FEAT_DIFF |
| 1766 | old_topfill = curwin->w_topfill; |
| 1767 | # endif |
| 1768 | old_botline = curwin->w_botline; |
| 1769 | update_screen(NOT_VALID); |
| 1770 | redrawcmdline(); |
| 1771 | } |
| 1772 | else |
| 1773 | vim_beep(BO_ERROR); |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1774 | goto cmdline_not_changed; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1775 | } |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1776 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1777 | #endif |
| 1778 | |
| 1779 | case Ctrl_V: |
| 1780 | case Ctrl_Q: |
| 1781 | #ifdef FEAT_MOUSE |
| 1782 | ignore_drag_release = TRUE; |
| 1783 | #endif |
| 1784 | putcmdline('^', TRUE); |
| 1785 | c = get_literal(); /* get next (two) character(s) */ |
| 1786 | do_abbr = FALSE; /* don't do abbreviation now */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1787 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1788 | #ifdef FEAT_MBYTE |
| 1789 | /* may need to remove ^ when composing char was typed */ |
| 1790 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 1791 | { |
| 1792 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 1793 | msg_putchar(' '); |
| 1794 | cursorcmd(); |
| 1795 | } |
| 1796 | #endif |
| 1797 | break; |
| 1798 | |
| 1799 | #ifdef FEAT_DIGRAPHS |
| 1800 | case Ctrl_K: |
| 1801 | #ifdef FEAT_MOUSE |
| 1802 | ignore_drag_release = TRUE; |
| 1803 | #endif |
| 1804 | putcmdline('?', TRUE); |
| 1805 | #ifdef USE_ON_FLY_SCROLL |
| 1806 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1807 | #endif |
| 1808 | c = get_digraph(TRUE); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1809 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1810 | if (c != NUL) |
| 1811 | break; |
| 1812 | |
| 1813 | redrawcmd(); |
| 1814 | goto cmdline_not_changed; |
| 1815 | #endif /* FEAT_DIGRAPHS */ |
| 1816 | |
| 1817 | #ifdef FEAT_RIGHTLEFT |
| 1818 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 1819 | if (!p_ari) |
| 1820 | break; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1821 | # ifdef FEAT_FKMAP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1822 | if (p_altkeymap) |
| 1823 | { |
| 1824 | cmd_fkmap = !cmd_fkmap; |
| 1825 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 1826 | ccline.overstrike = FALSE; |
| 1827 | } |
| 1828 | else /* Hebrew is default */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1829 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1830 | cmd_hkmap = !cmd_hkmap; |
| 1831 | goto cmdline_not_changed; |
| 1832 | #endif |
| 1833 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 1834 | case K_PS: |
| 1835 | bracketed_paste(PASTE_CMDLINE, FALSE, NULL); |
| 1836 | goto cmdline_changed; |
| 1837 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1838 | default: |
| 1839 | #ifdef UNIX |
| 1840 | if (c == intr_char) |
| 1841 | { |
| 1842 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1843 | putting it in history */ |
| 1844 | goto returncmd; /* back to Normal mode */ |
| 1845 | } |
| 1846 | #endif |
| 1847 | /* |
| 1848 | * Normal character with no special meaning. Just set mod_mask |
| 1849 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 1850 | * the string <S-Space>. This should only happen after ^V. |
| 1851 | */ |
| 1852 | if (!IS_SPECIAL(c)) |
| 1853 | mod_mask = 0x0; |
| 1854 | break; |
| 1855 | } |
| 1856 | /* |
| 1857 | * End of switch on command line character. |
| 1858 | * We come here if we have a normal character. |
| 1859 | */ |
| 1860 | |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1861 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1862 | #ifdef FEAT_MBYTE |
| 1863 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1864 | * what check_abbr() expects. */ |
| 1865 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1866 | #endif |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1867 | c) || c == Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1868 | goto cmdline_changed; |
| 1869 | |
| 1870 | /* |
| 1871 | * put the character in the command line |
| 1872 | */ |
| 1873 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 1874 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 1875 | else |
| 1876 | { |
| 1877 | #ifdef FEAT_MBYTE |
| 1878 | if (has_mbyte) |
| 1879 | { |
| 1880 | j = (*mb_char2bytes)(c, IObuff); |
| 1881 | IObuff[j] = NUL; /* exclude composing chars */ |
| 1882 | put_on_cmdline(IObuff, j, TRUE); |
| 1883 | } |
| 1884 | else |
| 1885 | #endif |
| 1886 | { |
| 1887 | IObuff[0] = c; |
| 1888 | put_on_cmdline(IObuff, 1, TRUE); |
| 1889 | } |
| 1890 | } |
| 1891 | goto cmdline_changed; |
| 1892 | |
| 1893 | /* |
| 1894 | * This part implements incremental searches for "/" and "?" |
| 1895 | * Jump to cmdline_not_changed when a character has been read but the command |
| 1896 | * line did not change. Then we only search and redraw if something changed in |
| 1897 | * the past. |
| 1898 | * Jump to cmdline_changed when the command line did change. |
| 1899 | * (Sorry for the goto's, I know it is ugly). |
| 1900 | */ |
| 1901 | cmdline_not_changed: |
| 1902 | #ifdef FEAT_SEARCH_EXTRA |
| 1903 | if (!incsearch_postponed) |
| 1904 | continue; |
| 1905 | #endif |
| 1906 | |
| 1907 | cmdline_changed: |
| 1908 | #ifdef FEAT_SEARCH_EXTRA |
| 1909 | /* |
| 1910 | * 'incsearch' highlighting. |
| 1911 | */ |
| 1912 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1913 | { |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1914 | pos_T end_pos; |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1915 | #ifdef FEAT_RELTIME |
| 1916 | proftime_T tm; |
| 1917 | #endif |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1918 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1919 | /* if there is a character waiting, search and redraw later */ |
| 1920 | if (char_avail()) |
| 1921 | { |
| 1922 | incsearch_postponed = TRUE; |
| 1923 | continue; |
| 1924 | } |
| 1925 | incsearch_postponed = FALSE; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1926 | curwin->w_cursor = search_start; /* start at old position */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1927 | |
| 1928 | /* If there is no command line, don't do anything */ |
| 1929 | if (ccline.cmdlen == 0) |
| 1930 | i = 0; |
| 1931 | else |
| 1932 | { |
| 1933 | cursor_off(); /* so the user knows we're busy */ |
| 1934 | out_flush(); |
| 1935 | ++emsg_off; /* So it doesn't beep if bad expr */ |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1936 | #ifdef FEAT_RELTIME |
| 1937 | /* Set the time limit to half a second. */ |
| 1938 | profile_setlimit(500L, &tm); |
| 1939 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1940 | i = do_search(NULL, firstc, ccline.cmdbuff, count, |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1941 | SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK, |
| 1942 | #ifdef FEAT_RELTIME |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1943 | &tm, NULL |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1944 | #else |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1945 | NULL, NULL |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1946 | #endif |
| 1947 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1948 | --emsg_off; |
| 1949 | /* if interrupted while searching, behave like it failed */ |
| 1950 | if (got_int) |
| 1951 | { |
| 1952 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 1953 | got_int = FALSE; /* don't abandon the command line */ |
| 1954 | i = 0; |
| 1955 | } |
| 1956 | else if (char_avail()) |
| 1957 | /* cancelled searching because a char was typed */ |
| 1958 | incsearch_postponed = TRUE; |
| 1959 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1960 | if (i != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1961 | highlight_match = TRUE; /* highlight position */ |
| 1962 | else |
| 1963 | highlight_match = FALSE; /* remove highlight */ |
| 1964 | |
| 1965 | /* first restore the old curwin values, so the screen is |
| 1966 | * positioned in the same way as the actual search command */ |
| 1967 | curwin->w_leftcol = old_leftcol; |
| 1968 | curwin->w_topline = old_topline; |
| 1969 | # ifdef FEAT_DIFF |
| 1970 | curwin->w_topfill = old_topfill; |
| 1971 | # endif |
| 1972 | curwin->w_botline = old_botline; |
| 1973 | changed_cline_bef_curs(); |
| 1974 | update_topline(); |
| 1975 | |
| 1976 | if (i != 0) |
| 1977 | { |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1978 | pos_T save_pos = curwin->w_cursor; |
| 1979 | |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1980 | match_start = curwin->w_cursor; |
| 1981 | set_search_match(&curwin->w_cursor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1982 | validate_cursor(); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1983 | end_pos = curwin->w_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1984 | match_end = end_pos; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1985 | curwin->w_cursor = save_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1986 | } |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1987 | else |
| 1988 | end_pos = curwin->w_cursor; /* shutup gcc 4 */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1989 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1990 | validate_cursor(); |
Bram Moolenaar | 27a2319 | 2006-09-14 09:27:26 +0000 | [diff] [blame] | 1991 | /* May redraw the status line to show the cursor position. */ |
| 1992 | if (p_ru && curwin->w_status_height > 0) |
| 1993 | curwin->w_redr_status = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1994 | |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1995 | save_cmdline(&save_ccline); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 1996 | update_screen(SOME_VALID); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1997 | restore_cmdline(&save_ccline); |
| 1998 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1999 | /* Leave it at the end to make CTRL-R CTRL-W work. */ |
| 2000 | if (i != 0) |
| 2001 | curwin->w_cursor = end_pos; |
| 2002 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2003 | msg_starthere(); |
| 2004 | redrawcmdline(); |
| 2005 | did_incsearch = TRUE; |
| 2006 | } |
| 2007 | #else /* FEAT_SEARCH_EXTRA */ |
| 2008 | ; |
| 2009 | #endif |
| 2010 | |
| 2011 | #ifdef FEAT_RIGHTLEFT |
| 2012 | if (cmdmsg_rl |
| 2013 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2014 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2015 | # endif |
| 2016 | ) |
| 2017 | /* Always redraw the whole command line to fix shaping and |
Bram Moolenaar | 58437e0 | 2012-02-22 17:58:04 +0100 | [diff] [blame] | 2018 | * right-left typing. Not efficient, but it works. |
| 2019 | * Do it only when there are no characters left to read |
| 2020 | * to avoid useless intermediate redraws. */ |
| 2021 | if (vpeekc() == NUL) |
| 2022 | redrawcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2023 | #endif |
| 2024 | } |
| 2025 | |
| 2026 | returncmd: |
| 2027 | |
| 2028 | #ifdef FEAT_RIGHTLEFT |
| 2029 | cmdmsg_rl = FALSE; |
| 2030 | #endif |
| 2031 | |
| 2032 | #ifdef FEAT_FKMAP |
| 2033 | cmd_fkmap = 0; |
| 2034 | #endif |
| 2035 | |
| 2036 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2037 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2038 | |
| 2039 | #ifdef FEAT_SEARCH_EXTRA |
| 2040 | if (did_incsearch) |
| 2041 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 2042 | if (gotesc) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 2043 | curwin->w_cursor = save_cursor; |
| 2044 | else |
| 2045 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2046 | if (!EQUAL_POS(save_cursor, search_start)) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 2047 | { |
| 2048 | /* put the '" mark at the original position */ |
| 2049 | curwin->w_cursor = save_cursor; |
| 2050 | setpcmark(); |
| 2051 | } |
| 2052 | curwin->w_cursor = search_start; |
| 2053 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2054 | curwin->w_curswant = old_curswant; |
| 2055 | curwin->w_leftcol = old_leftcol; |
| 2056 | curwin->w_topline = old_topline; |
| 2057 | # ifdef FEAT_DIFF |
| 2058 | curwin->w_topfill = old_topfill; |
| 2059 | # endif |
| 2060 | curwin->w_botline = old_botline; |
| 2061 | highlight_match = FALSE; |
| 2062 | validate_cursor(); /* needed for TAB */ |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 2063 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2064 | } |
| 2065 | #endif |
| 2066 | |
| 2067 | if (ccline.cmdbuff != NULL) |
| 2068 | { |
| 2069 | /* |
| 2070 | * Put line in history buffer (":" and "=" only when it was typed). |
| 2071 | */ |
| 2072 | #ifdef FEAT_CMDHIST |
| 2073 | if (ccline.cmdlen && firstc != NUL |
| 2074 | && (some_key_typed || histype == HIST_SEARCH)) |
| 2075 | { |
| 2076 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 2077 | histype == HIST_SEARCH ? firstc : NUL); |
| 2078 | if (firstc == ':') |
| 2079 | { |
| 2080 | vim_free(new_last_cmdline); |
| 2081 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 2082 | } |
| 2083 | } |
| 2084 | #endif |
| 2085 | |
| 2086 | if (gotesc) /* abandon command line */ |
| 2087 | { |
| 2088 | vim_free(ccline.cmdbuff); |
| 2089 | ccline.cmdbuff = NULL; |
| 2090 | if (msg_scrolled == 0) |
| 2091 | compute_cmdrow(); |
| 2092 | MSG(""); |
| 2093 | redraw_cmdline = TRUE; |
| 2094 | } |
| 2095 | } |
| 2096 | |
| 2097 | /* |
| 2098 | * If the screen was shifted up, redraw the whole screen (later). |
| 2099 | * If the line is too long, clear it, so ruler and shown command do |
| 2100 | * not get printed in the middle of it. |
| 2101 | */ |
| 2102 | msg_check(); |
| 2103 | msg_scroll = save_msg_scroll; |
| 2104 | redir_off = FALSE; |
| 2105 | |
| 2106 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 2107 | if (some_key_typed) |
| 2108 | need_wait_return = FALSE; |
| 2109 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2110 | #ifdef FEAT_AUTOCMD |
| 2111 | /* Trigger CmdlineLeave autocommands. */ |
| 2112 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE); |
| 2113 | #endif |
| 2114 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2115 | State = save_State; |
| 2116 | #ifdef USE_IM_CONTROL |
| 2117 | if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) |
| 2118 | im_save_status(b_im_ptr); |
| 2119 | im_set_active(FALSE); |
| 2120 | #endif |
| 2121 | #ifdef FEAT_MOUSE |
| 2122 | setmouse(); |
| 2123 | #endif |
| 2124 | #ifdef CURSOR_SHAPE |
| 2125 | ui_cursor_shape(); /* may show different cursor shape */ |
| 2126 | #endif |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 2127 | sb_text_end_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2128 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2129 | { |
| 2130 | char_u *p = ccline.cmdbuff; |
| 2131 | |
| 2132 | /* Make ccline empty, getcmdline() may try to use it. */ |
| 2133 | ccline.cmdbuff = NULL; |
| 2134 | return p; |
| 2135 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2136 | } |
| 2137 | |
| 2138 | #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) |
| 2139 | /* |
| 2140 | * Get a command line with a prompt. |
| 2141 | * This is prepared to be called recursively from getcmdline() (e.g. by |
| 2142 | * f_input() when evaluating an expression from CTRL-R =). |
| 2143 | * Returns the command line in allocated memory, or NULL. |
| 2144 | */ |
| 2145 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2146 | getcmdline_prompt( |
| 2147 | int firstc, |
| 2148 | char_u *prompt, /* command line prompt */ |
| 2149 | int attr, /* attributes for prompt */ |
| 2150 | int xp_context, /* type of expansion */ |
| 2151 | char_u *xp_arg) /* user-defined expansion argument */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2152 | { |
| 2153 | char_u *s; |
| 2154 | struct cmdline_info save_ccline; |
| 2155 | int msg_col_save = msg_col; |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2156 | int msg_silent_save = msg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2157 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2158 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2159 | ccline.cmdprompt = prompt; |
| 2160 | ccline.cmdattr = attr; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2161 | # ifdef FEAT_EVAL |
| 2162 | ccline.xp_context = xp_context; |
| 2163 | ccline.xp_arg = xp_arg; |
| 2164 | ccline.input_fn = (firstc == '@'); |
| 2165 | # endif |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2166 | msg_silent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2167 | s = getcmdline(firstc, 1L, 0); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2168 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2169 | msg_silent = msg_silent_save; |
Bram Moolenaar | 1db1f77 | 2011-08-17 16:25:48 +0200 | [diff] [blame] | 2170 | /* Restore msg_col, the prompt from input() may have changed it. |
| 2171 | * But only if called recursively and the commandline is therefore being |
| 2172 | * restored to an old one; if not, the input() prompt stays on the screen, |
| 2173 | * so we need its modified msg_col left intact. */ |
| 2174 | if (ccline.cmdbuff != NULL) |
| 2175 | msg_col = msg_col_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2176 | |
| 2177 | return s; |
| 2178 | } |
| 2179 | #endif |
| 2180 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2181 | /* |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2182 | * Return TRUE when the text must not be changed and we can't switch to |
| 2183 | * another window or buffer. Used when editing the command line, evaluating |
| 2184 | * 'balloonexpr', etc. |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2185 | */ |
| 2186 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2187 | text_locked(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2188 | { |
| 2189 | #ifdef FEAT_CMDWIN |
| 2190 | if (cmdwin_type != 0) |
| 2191 | return TRUE; |
| 2192 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2193 | return textlock != 0; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2194 | } |
| 2195 | |
| 2196 | /* |
| 2197 | * Give an error message for a command that isn't allowed while the cmdline |
| 2198 | * window is open or editing the cmdline in another way. |
| 2199 | */ |
| 2200 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2201 | text_locked_msg(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2202 | { |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2203 | EMSG(_(get_text_locked_msg())); |
| 2204 | } |
| 2205 | |
| 2206 | char_u * |
| 2207 | get_text_locked_msg(void) |
| 2208 | { |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2209 | #ifdef FEAT_CMDWIN |
| 2210 | if (cmdwin_type != 0) |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2211 | return e_cmdwin; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2212 | #endif |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2213 | return e_secure; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2214 | } |
| 2215 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2216 | #if defined(FEAT_AUTOCMD) || defined(PROTO) |
| 2217 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2218 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2219 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2220 | */ |
| 2221 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2222 | curbuf_locked(void) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2223 | { |
| 2224 | if (curbuf_lock > 0) |
| 2225 | { |
| 2226 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2227 | return TRUE; |
| 2228 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2229 | return allbuf_locked(); |
| 2230 | } |
| 2231 | |
| 2232 | /* |
| 2233 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2234 | * message. |
| 2235 | */ |
| 2236 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2237 | allbuf_locked(void) |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2238 | { |
| 2239 | if (allbuf_lock > 0) |
| 2240 | { |
| 2241 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2242 | return TRUE; |
| 2243 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2244 | return FALSE; |
| 2245 | } |
| 2246 | #endif |
| 2247 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2248 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2249 | cmdline_charsize(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2250 | { |
| 2251 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2252 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2253 | return 1; |
| 2254 | #endif |
| 2255 | return ptr2cells(ccline.cmdbuff + idx); |
| 2256 | } |
| 2257 | |
| 2258 | /* |
| 2259 | * Compute the offset of the cursor on the command line for the prompt and |
| 2260 | * indent. |
| 2261 | */ |
| 2262 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2263 | set_cmdspos(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2264 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2265 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2266 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2267 | else |
| 2268 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2269 | } |
| 2270 | |
| 2271 | /* |
| 2272 | * Compute the screen position for the cursor on the command line. |
| 2273 | */ |
| 2274 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2275 | set_cmdspos_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2276 | { |
| 2277 | int i, m, c; |
| 2278 | |
| 2279 | set_cmdspos(); |
| 2280 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2281 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2282 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2283 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2284 | m = MAXCOL; |
| 2285 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2286 | else |
| 2287 | m = MAXCOL; |
| 2288 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2289 | { |
| 2290 | c = cmdline_charsize(i); |
| 2291 | #ifdef FEAT_MBYTE |
| 2292 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2293 | if (has_mbyte) |
| 2294 | correct_cmdspos(i, c); |
| 2295 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2296 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2297 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2298 | if ((ccline.cmdspos += c) >= m) |
| 2299 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2300 | ccline.cmdspos -= c; |
| 2301 | break; |
| 2302 | } |
| 2303 | #ifdef FEAT_MBYTE |
| 2304 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2305 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2306 | #endif |
| 2307 | } |
| 2308 | } |
| 2309 | |
| 2310 | #ifdef FEAT_MBYTE |
| 2311 | /* |
| 2312 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2313 | * character that doesn't fit, so that a ">" must be displayed. |
| 2314 | */ |
| 2315 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2316 | correct_cmdspos(int idx, int cells) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2317 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2318 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2319 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2320 | && ccline.cmdspos % Columns + cells > Columns) |
| 2321 | ccline.cmdspos++; |
| 2322 | } |
| 2323 | #endif |
| 2324 | |
| 2325 | /* |
| 2326 | * Get an Ex command line for the ":" command. |
| 2327 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2328 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2329 | getexline( |
| 2330 | int c, /* normally ':', NUL for ":append" */ |
| 2331 | void *cookie UNUSED, |
| 2332 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2333 | { |
| 2334 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2335 | if (exec_from_reg && vpeekc() == ':') |
| 2336 | (void)vgetc(); |
| 2337 | return getcmdline(c, 1L, indent); |
| 2338 | } |
| 2339 | |
| 2340 | /* |
| 2341 | * Get an Ex command line for Ex mode. |
| 2342 | * In Ex mode we only use the OS supplied line editing features and no |
| 2343 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2344 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2345 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2346 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2347 | getexmodeline( |
| 2348 | int promptc, /* normally ':', NUL for ":append" and '?' for |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2349 | :s prompt */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2350 | void *cookie UNUSED, |
| 2351 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2352 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2353 | garray_T line_ga; |
| 2354 | char_u *pend; |
| 2355 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2356 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2357 | int escaped = FALSE; /* CTRL-V typed */ |
| 2358 | int vcol = 0; |
| 2359 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2360 | int prev_char; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2361 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2362 | |
| 2363 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2364 | * confuses the system function that computes tabstops. */ |
| 2365 | cursor_on(); |
| 2366 | |
| 2367 | /* always start in column 0; write a newline if necessary */ |
| 2368 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2369 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2370 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2371 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2372 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2373 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2374 | if (p_prompt) |
| 2375 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2376 | while (indent-- > 0) |
| 2377 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2378 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
| 2381 | ga_init2(&line_ga, 1, 30); |
| 2382 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2383 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2384 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2385 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2386 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2387 | while (indent >= 8) |
| 2388 | { |
| 2389 | ga_append(&line_ga, TAB); |
| 2390 | msg_puts((char_u *)" "); |
| 2391 | indent -= 8; |
| 2392 | } |
| 2393 | while (indent-- > 0) |
| 2394 | { |
| 2395 | ga_append(&line_ga, ' '); |
| 2396 | msg_putchar(' '); |
| 2397 | } |
| 2398 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2399 | ++no_mapping; |
| 2400 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2401 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2402 | /* |
| 2403 | * Get the line, one character at a time. |
| 2404 | */ |
| 2405 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2406 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2407 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2408 | long sw; |
| 2409 | char_u *s; |
| 2410 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2411 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2412 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2413 | |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2414 | /* |
| 2415 | * Get one character at a time. |
| 2416 | */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2417 | prev_char = c1; |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2418 | |
| 2419 | /* Check for a ":normal" command and no more characters left. */ |
| 2420 | if (ex_normal_busy > 0 && typebuf.tb_len == 0) |
| 2421 | c1 = '\n'; |
| 2422 | else |
| 2423 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2424 | |
| 2425 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2426 | * Handle line editing. |
| 2427 | * Previously this was left to the system, putting the terminal in |
| 2428 | * cooked mode, but then CTRL-D and CTRL-T can't be used properly. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2429 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2430 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2431 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2432 | msg_putchar('\n'); |
| 2433 | break; |
| 2434 | } |
| 2435 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2436 | if (c1 == K_PS) |
| 2437 | { |
| 2438 | bracketed_paste(PASTE_EX, FALSE, &line_ga); |
| 2439 | goto redraw; |
| 2440 | } |
| 2441 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2442 | if (!escaped) |
| 2443 | { |
| 2444 | /* CR typed means "enter", which is NL */ |
| 2445 | if (c1 == '\r') |
| 2446 | c1 = '\n'; |
| 2447 | |
| 2448 | if (c1 == BS || c1 == K_BS |
| 2449 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2450 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2451 | if (line_ga.ga_len > 0) |
| 2452 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2453 | #ifdef FEAT_MBYTE |
| 2454 | if (has_mbyte) |
| 2455 | { |
| 2456 | p = (char_u *)line_ga.ga_data; |
| 2457 | p[line_ga.ga_len] = NUL; |
| 2458 | len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1; |
| 2459 | line_ga.ga_len -= len; |
| 2460 | } |
| 2461 | else |
| 2462 | #endif |
| 2463 | --line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2464 | goto redraw; |
| 2465 | } |
| 2466 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2467 | } |
| 2468 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2469 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2470 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2471 | msg_col = startcol; |
| 2472 | msg_clr_eos(); |
| 2473 | line_ga.ga_len = 0; |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2474 | goto redraw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2475 | } |
| 2476 | |
| 2477 | if (c1 == Ctrl_T) |
| 2478 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2479 | sw = get_sw_value(curbuf); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2480 | p = (char_u *)line_ga.ga_data; |
| 2481 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2482 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 2483 | indent += sw - indent % sw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2484 | add_indent: |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2485 | while (get_indent_str(p, 8, FALSE) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2486 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2487 | (void)ga_grow(&line_ga, 2); /* one more for the NUL */ |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2488 | p = (char_u *)line_ga.ga_data; |
| 2489 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2490 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2491 | *s = ' '; |
| 2492 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2493 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2494 | redraw: |
| 2495 | /* redraw the line */ |
| 2496 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2497 | vcol = 0; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2498 | p = (char_u *)line_ga.ga_data; |
| 2499 | p[line_ga.ga_len] = NUL; |
| 2500 | while (p < (char_u *)line_ga.ga_data + line_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2501 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2502 | if (*p == TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2503 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2504 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2505 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2506 | msg_putchar(' '); |
| 2507 | } while (++vcol % 8); |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2508 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2509 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2510 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2511 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2512 | len = MB_PTR2LEN(p); |
| 2513 | msg_outtrans_len(p, len); |
| 2514 | vcol += ptr2cells(p); |
| 2515 | p += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2516 | } |
| 2517 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2518 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2519 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2520 | continue; |
| 2521 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2522 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2523 | if (c1 == Ctrl_D) |
| 2524 | { |
| 2525 | /* Delete one shiftwidth. */ |
| 2526 | p = (char_u *)line_ga.ga_data; |
| 2527 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2528 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2529 | if (prev_char == '^') |
| 2530 | ex_keep_indent = TRUE; |
| 2531 | indent = 0; |
| 2532 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2533 | } |
| 2534 | else |
| 2535 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2536 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2537 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2538 | if (indent > 0) |
| 2539 | { |
| 2540 | --indent; |
| 2541 | indent -= indent % get_sw_value(curbuf); |
| 2542 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2543 | } |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2544 | while (get_indent_str(p, 8, FALSE) > indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2545 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2546 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2547 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2548 | --line_ga.ga_len; |
| 2549 | } |
| 2550 | goto add_indent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2551 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2552 | |
| 2553 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2554 | { |
| 2555 | escaped = TRUE; |
| 2556 | continue; |
| 2557 | } |
| 2558 | |
| 2559 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2560 | if (IS_SPECIAL(c1)) |
| 2561 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2562 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2563 | |
| 2564 | if (IS_SPECIAL(c1)) |
| 2565 | c1 = '?'; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2566 | #ifdef FEAT_MBYTE |
| 2567 | if (has_mbyte) |
| 2568 | len = (*mb_char2bytes)(c1, |
| 2569 | (char_u *)line_ga.ga_data + line_ga.ga_len); |
| 2570 | else |
| 2571 | #endif |
| 2572 | { |
| 2573 | len = 1; |
| 2574 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
| 2575 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2576 | if (c1 == '\n') |
| 2577 | msg_putchar('\n'); |
| 2578 | else if (c1 == TAB) |
| 2579 | { |
| 2580 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2581 | do |
| 2582 | { |
| 2583 | msg_putchar(' '); |
| 2584 | } while (++vcol % 8); |
| 2585 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2586 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2587 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2588 | msg_outtrans_len( |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2589 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, len); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2590 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2591 | } |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2592 | line_ga.ga_len += len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2593 | escaped = FALSE; |
| 2594 | |
| 2595 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2596 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2597 | |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2598 | /* We are done when a NL is entered, but not when it comes after an |
| 2599 | * odd number of backslashes, that results in a NUL. */ |
| 2600 | if (line_ga.ga_len > 0 && pend[-1] == '\n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2601 | { |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2602 | int bcount = 0; |
| 2603 | |
| 2604 | while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') |
| 2605 | ++bcount; |
| 2606 | |
| 2607 | if (bcount > 0) |
| 2608 | { |
| 2609 | /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> |
| 2610 | * "\NL", etc. */ |
| 2611 | line_ga.ga_len -= (bcount + 1) / 2; |
| 2612 | pend -= (bcount + 1) / 2; |
| 2613 | pend[-1] = '\n'; |
| 2614 | } |
| 2615 | |
| 2616 | if ((bcount & 1) == 0) |
| 2617 | { |
| 2618 | --line_ga.ga_len; |
| 2619 | --pend; |
| 2620 | *pend = NUL; |
| 2621 | break; |
| 2622 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2623 | } |
| 2624 | } |
| 2625 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2626 | --no_mapping; |
| 2627 | --allow_keys; |
| 2628 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2629 | /* make following messages go to the next line */ |
| 2630 | msg_didout = FALSE; |
| 2631 | msg_col = 0; |
| 2632 | if (msg_row < Rows - 1) |
| 2633 | ++msg_row; |
| 2634 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 2635 | |
| 2636 | if (got_int) |
| 2637 | ga_clear(&line_ga); |
| 2638 | |
| 2639 | return (char_u *)line_ga.ga_data; |
| 2640 | } |
| 2641 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2642 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 2643 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2644 | /* |
| 2645 | * Return TRUE if ccline.overstrike is on. |
| 2646 | */ |
| 2647 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2648 | cmdline_overstrike(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2649 | { |
| 2650 | return ccline.overstrike; |
| 2651 | } |
| 2652 | |
| 2653 | /* |
| 2654 | * Return TRUE if the cursor is at the end of the cmdline. |
| 2655 | */ |
| 2656 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2657 | cmdline_at_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2658 | { |
| 2659 | return (ccline.cmdpos >= ccline.cmdlen); |
| 2660 | } |
| 2661 | #endif |
| 2662 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2663 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2664 | /* |
| 2665 | * Return the virtual column number at the current cursor position. |
| 2666 | * This is used by the IM code to obtain the start of the preedit string. |
| 2667 | */ |
| 2668 | colnr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2669 | cmdline_getvcol_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2670 | { |
| 2671 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 2672 | return MAXCOL; |
| 2673 | |
| 2674 | # ifdef FEAT_MBYTE |
| 2675 | if (has_mbyte) |
| 2676 | { |
| 2677 | colnr_T col; |
| 2678 | int i = 0; |
| 2679 | |
| 2680 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2681 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2682 | |
| 2683 | return col; |
| 2684 | } |
| 2685 | else |
| 2686 | # endif |
| 2687 | return ccline.cmdpos; |
| 2688 | } |
| 2689 | #endif |
| 2690 | |
| 2691 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2692 | /* |
| 2693 | * If part of the command line is an IM preedit string, redraw it with |
| 2694 | * IM feedback attributes. The cursor position is restored after drawing. |
| 2695 | */ |
| 2696 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2697 | redrawcmd_preedit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2698 | { |
| 2699 | if ((State & CMDLINE) |
| 2700 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 2701 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2702 | && !p_imdisable |
| 2703 | && im_is_preediting()) |
| 2704 | { |
| 2705 | int cmdpos = 0; |
| 2706 | int cmdspos; |
| 2707 | int old_row; |
| 2708 | int old_col; |
| 2709 | colnr_T col; |
| 2710 | |
| 2711 | old_row = msg_row; |
| 2712 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2713 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2714 | |
| 2715 | # ifdef FEAT_MBYTE |
| 2716 | if (has_mbyte) |
| 2717 | { |
| 2718 | for (col = 0; col < preedit_start_col |
| 2719 | && cmdpos < ccline.cmdlen; ++col) |
| 2720 | { |
| 2721 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2722 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2723 | } |
| 2724 | } |
| 2725 | else |
| 2726 | # endif |
| 2727 | { |
| 2728 | cmdspos += preedit_start_col; |
| 2729 | cmdpos += preedit_start_col; |
| 2730 | } |
| 2731 | |
| 2732 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 2733 | msg_col = cmdspos % (int)Columns; |
| 2734 | if (msg_row >= Rows) |
| 2735 | msg_row = Rows - 1; |
| 2736 | |
| 2737 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 2738 | { |
| 2739 | int char_len; |
| 2740 | int char_attr; |
| 2741 | |
| 2742 | char_attr = im_get_feedback_attr(col); |
| 2743 | if (char_attr < 0) |
| 2744 | break; /* end of preedit string */ |
| 2745 | |
| 2746 | # ifdef FEAT_MBYTE |
| 2747 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2748 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2749 | else |
| 2750 | # endif |
| 2751 | char_len = 1; |
| 2752 | |
| 2753 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 2754 | cmdpos += char_len; |
| 2755 | } |
| 2756 | |
| 2757 | msg_row = old_row; |
| 2758 | msg_col = old_col; |
| 2759 | } |
| 2760 | } |
| 2761 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 2762 | |
| 2763 | /* |
| 2764 | * Allocate a new command line buffer. |
| 2765 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
| 2766 | * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. |
| 2767 | */ |
| 2768 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2769 | alloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2770 | { |
| 2771 | /* |
| 2772 | * give some extra space to avoid having to allocate all the time |
| 2773 | */ |
| 2774 | if (len < 80) |
| 2775 | len = 100; |
| 2776 | else |
| 2777 | len += 20; |
| 2778 | |
| 2779 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 2780 | ccline.cmdbufflen = len; |
| 2781 | } |
| 2782 | |
| 2783 | /* |
| 2784 | * Re-allocate the command line to length len + something extra. |
| 2785 | * return FAIL for failure, OK otherwise |
| 2786 | */ |
| 2787 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2788 | realloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2789 | { |
| 2790 | char_u *p; |
| 2791 | |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 2792 | if (len < ccline.cmdbufflen) |
| 2793 | return OK; /* no need to resize */ |
| 2794 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2795 | p = ccline.cmdbuff; |
| 2796 | alloc_cmdbuff(len); /* will get some more */ |
| 2797 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 2798 | { |
| 2799 | ccline.cmdbuff = p; /* keep the old one */ |
| 2800 | return FAIL; |
| 2801 | } |
Bram Moolenaar | 35a3423 | 2010-08-13 16:51:26 +0200 | [diff] [blame] | 2802 | /* There isn't always a NUL after the command, but it may need to be |
| 2803 | * there, thus copy up to the NUL and add a NUL. */ |
| 2804 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
| 2805 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2806 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2807 | |
| 2808 | if (ccline.xpc != NULL |
| 2809 | && ccline.xpc->xp_pattern != NULL |
| 2810 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 2811 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 2812 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 2813 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2814 | |
| 2815 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 2816 | * to point into the newly allocated memory. */ |
| 2817 | if (i >= 0 && i <= ccline.cmdlen) |
| 2818 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 2819 | } |
| 2820 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2821 | return OK; |
| 2822 | } |
| 2823 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2824 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 2825 | static char_u *arshape_buf = NULL; |
| 2826 | |
| 2827 | # if defined(EXITFREE) || defined(PROTO) |
| 2828 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2829 | free_cmdline_buf(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2830 | { |
| 2831 | vim_free(arshape_buf); |
| 2832 | } |
| 2833 | # endif |
| 2834 | #endif |
| 2835 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2836 | /* |
| 2837 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 2838 | * when cmdline_star is TRUE. |
| 2839 | */ |
| 2840 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2841 | draw_cmdline(int start, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2842 | { |
| 2843 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2844 | int i; |
| 2845 | |
| 2846 | if (cmdline_star > 0) |
| 2847 | for (i = 0; i < len; ++i) |
| 2848 | { |
| 2849 | msg_putchar('*'); |
| 2850 | # ifdef FEAT_MBYTE |
| 2851 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2852 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2853 | # endif |
| 2854 | } |
| 2855 | else |
| 2856 | #endif |
| 2857 | #ifdef FEAT_ARABIC |
| 2858 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 2859 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2860 | static int buflen = 0; |
| 2861 | char_u *p; |
| 2862 | int j; |
| 2863 | int newlen = 0; |
| 2864 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 2865 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2866 | int prev_c = 0; |
| 2867 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2868 | int u8c; |
| 2869 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2870 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2871 | |
| 2872 | /* |
| 2873 | * Do arabic shaping into a temporary buffer. This is very |
| 2874 | * inefficient! |
| 2875 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2876 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2877 | { |
| 2878 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 2879 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2880 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2881 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2882 | arshape_buf = alloc(buflen); |
| 2883 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2884 | return; /* out of memory */ |
| 2885 | } |
| 2886 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2887 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 2888 | { |
| 2889 | /* Prepend a space to draw the leading composing char on. */ |
| 2890 | arshape_buf[0] = ' '; |
| 2891 | newlen = 1; |
| 2892 | } |
| 2893 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2894 | for (j = start; j < start + len; j += mb_l) |
| 2895 | { |
| 2896 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2897 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2898 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2899 | if (ARABIC_CHAR(u8c)) |
| 2900 | { |
| 2901 | /* Do Arabic shaping. */ |
| 2902 | if (cmdmsg_rl) |
| 2903 | { |
| 2904 | /* displaying from right to left */ |
| 2905 | pc = prev_c; |
| 2906 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2907 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2908 | if (j + mb_l >= start + len) |
| 2909 | nc = NUL; |
| 2910 | else |
| 2911 | nc = utf_ptr2char(p + mb_l); |
| 2912 | } |
| 2913 | else |
| 2914 | { |
| 2915 | /* displaying from left to right */ |
| 2916 | if (j + mb_l >= start + len) |
| 2917 | pc = NUL; |
| 2918 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2919 | { |
| 2920 | int pcc[MAX_MCO]; |
| 2921 | |
| 2922 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2923 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2924 | pc1 = pcc[0]; |
| 2925 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2926 | nc = prev_c; |
| 2927 | } |
| 2928 | prev_c = u8c; |
| 2929 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2930 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2931 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2932 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2933 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2934 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2935 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 2936 | if (u8cc[1] != 0) |
| 2937 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2938 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2939 | } |
| 2940 | } |
| 2941 | else |
| 2942 | { |
| 2943 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2944 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2945 | newlen += mb_l; |
| 2946 | } |
| 2947 | } |
| 2948 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2949 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2950 | } |
| 2951 | else |
| 2952 | #endif |
| 2953 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 2954 | } |
| 2955 | |
| 2956 | /* |
| 2957 | * Put a character on the command line. Shifts the following text to the |
| 2958 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 2959 | * "c" must be printable (fit in one display cell)! |
| 2960 | */ |
| 2961 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2962 | putcmdline(int c, int shift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2963 | { |
| 2964 | if (cmd_silent) |
| 2965 | return; |
| 2966 | msg_no_more = TRUE; |
| 2967 | msg_putchar(c); |
| 2968 | if (shift) |
| 2969 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2970 | msg_no_more = FALSE; |
| 2971 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 2972 | extra_char = c; |
| 2973 | extra_char_shift = shift; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2974 | } |
| 2975 | |
| 2976 | /* |
| 2977 | * Undo a putcmdline(c, FALSE). |
| 2978 | */ |
| 2979 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2980 | unputcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2981 | { |
| 2982 | if (cmd_silent) |
| 2983 | return; |
| 2984 | msg_no_more = TRUE; |
| 2985 | if (ccline.cmdlen == ccline.cmdpos) |
| 2986 | msg_putchar(' '); |
Bram Moolenaar | 64fdf5c | 2012-06-06 12:03:06 +0200 | [diff] [blame] | 2987 | #ifdef FEAT_MBYTE |
| 2988 | else if (has_mbyte) |
| 2989 | draw_cmdline(ccline.cmdpos, |
| 2990 | (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); |
| 2991 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2992 | else |
| 2993 | draw_cmdline(ccline.cmdpos, 1); |
| 2994 | msg_no_more = FALSE; |
| 2995 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 2996 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
| 2999 | /* |
| 3000 | * Put the given string, of the given length, onto the command line. |
| 3001 | * If len is -1, then STRLEN() is used to calculate the length. |
| 3002 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 3003 | * part will be redrawn, otherwise it will not. If this function is called |
| 3004 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 3005 | * called afterwards. |
| 3006 | */ |
| 3007 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3008 | put_on_cmdline(char_u *str, int len, int redraw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3009 | { |
| 3010 | int retval; |
| 3011 | int i; |
| 3012 | int m; |
| 3013 | int c; |
| 3014 | |
| 3015 | if (len < 0) |
| 3016 | len = (int)STRLEN(str); |
| 3017 | |
| 3018 | /* Check if ccline.cmdbuff needs to be longer */ |
| 3019 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3020 | retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3021 | else |
| 3022 | retval = OK; |
| 3023 | if (retval == OK) |
| 3024 | { |
| 3025 | if (!ccline.overstrike) |
| 3026 | { |
| 3027 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3028 | ccline.cmdbuff + ccline.cmdpos, |
| 3029 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 3030 | ccline.cmdlen += len; |
| 3031 | } |
| 3032 | else |
| 3033 | { |
| 3034 | #ifdef FEAT_MBYTE |
| 3035 | if (has_mbyte) |
| 3036 | { |
| 3037 | /* Count nr of characters in the new string. */ |
| 3038 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3039 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3040 | ++m; |
| 3041 | /* Count nr of bytes in cmdline that are overwritten by these |
| 3042 | * characters. */ |
| 3043 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3044 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3045 | --m; |
| 3046 | if (i < ccline.cmdlen) |
| 3047 | { |
| 3048 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3049 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 3050 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 3051 | } |
| 3052 | else |
| 3053 | ccline.cmdlen = ccline.cmdpos + len; |
| 3054 | } |
| 3055 | else |
| 3056 | #endif |
| 3057 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 3058 | ccline.cmdlen = ccline.cmdpos + len; |
| 3059 | } |
| 3060 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 3061 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 3062 | |
| 3063 | #ifdef FEAT_MBYTE |
| 3064 | if (enc_utf8) |
| 3065 | { |
| 3066 | /* When the inserted text starts with a composing character, |
| 3067 | * backup to the character before it. There could be two of them. |
| 3068 | */ |
| 3069 | i = 0; |
| 3070 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3071 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 3072 | { |
| 3073 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3074 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3075 | ccline.cmdpos -= i; |
| 3076 | len += i; |
| 3077 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3078 | } |
| 3079 | # ifdef FEAT_ARABIC |
| 3080 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 3081 | { |
| 3082 | /* Check the previous character for Arabic combining pair. */ |
| 3083 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3084 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3085 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 3086 | + ccline.cmdpos - i), c)) |
| 3087 | { |
| 3088 | ccline.cmdpos -= i; |
| 3089 | len += i; |
| 3090 | } |
| 3091 | else |
| 3092 | i = 0; |
| 3093 | } |
| 3094 | # endif |
| 3095 | if (i != 0) |
| 3096 | { |
| 3097 | /* Also backup the cursor position. */ |
| 3098 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 3099 | ccline.cmdspos -= i; |
| 3100 | msg_col -= i; |
| 3101 | if (msg_col < 0) |
| 3102 | { |
| 3103 | msg_col += Columns; |
| 3104 | --msg_row; |
| 3105 | } |
| 3106 | } |
| 3107 | } |
| 3108 | #endif |
| 3109 | |
| 3110 | if (redraw && !cmd_silent) |
| 3111 | { |
| 3112 | msg_no_more = TRUE; |
| 3113 | i = cmdline_row; |
Bram Moolenaar | 73dc59a | 2011-09-30 17:46:21 +0200 | [diff] [blame] | 3114 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3115 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3116 | /* Avoid clearing the rest of the line too often. */ |
| 3117 | if (cmdline_row != i || ccline.overstrike) |
| 3118 | msg_clr_eos(); |
| 3119 | msg_no_more = FALSE; |
| 3120 | } |
| 3121 | #ifdef FEAT_FKMAP |
| 3122 | /* |
| 3123 | * If we are in Farsi command mode, the character input must be in |
| 3124 | * Insert mode. So do not advance the cmdpos. |
| 3125 | */ |
| 3126 | if (!cmd_fkmap) |
| 3127 | #endif |
| 3128 | { |
| 3129 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3130 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3131 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3132 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 3133 | m = MAXCOL; |
| 3134 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3135 | else |
| 3136 | m = MAXCOL; |
| 3137 | for (i = 0; i < len; ++i) |
| 3138 | { |
| 3139 | c = cmdline_charsize(ccline.cmdpos); |
| 3140 | #ifdef FEAT_MBYTE |
| 3141 | /* count ">" for a double-wide char that doesn't fit. */ |
| 3142 | if (has_mbyte) |
| 3143 | correct_cmdspos(ccline.cmdpos, c); |
| 3144 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3145 | /* Stop cursor at the end of the screen, but do increment the |
| 3146 | * insert position, so that entering a very long command |
| 3147 | * works, even though you can't see it. */ |
| 3148 | if (ccline.cmdspos + c < m) |
| 3149 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3150 | #ifdef FEAT_MBYTE |
| 3151 | if (has_mbyte) |
| 3152 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3153 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3154 | if (c > len - i - 1) |
| 3155 | c = len - i - 1; |
| 3156 | ccline.cmdpos += c; |
| 3157 | i += c; |
| 3158 | } |
| 3159 | #endif |
| 3160 | ++ccline.cmdpos; |
| 3161 | } |
| 3162 | } |
| 3163 | } |
| 3164 | if (redraw) |
| 3165 | msg_check(); |
| 3166 | return retval; |
| 3167 | } |
| 3168 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3169 | static struct cmdline_info prev_ccline; |
| 3170 | static int prev_ccline_used = FALSE; |
| 3171 | |
| 3172 | /* |
| 3173 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 3174 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 3175 | * available globally in prev_ccline. |
| 3176 | */ |
| 3177 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3178 | save_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3179 | { |
| 3180 | if (!prev_ccline_used) |
| 3181 | { |
| 3182 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 3183 | prev_ccline_used = TRUE; |
| 3184 | } |
| 3185 | *ccp = prev_ccline; |
| 3186 | prev_ccline = ccline; |
| 3187 | ccline.cmdbuff = NULL; |
| 3188 | ccline.cmdprompt = NULL; |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3189 | ccline.xpc = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3190 | } |
| 3191 | |
| 3192 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3193 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3194 | */ |
| 3195 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3196 | restore_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3197 | { |
| 3198 | ccline = prev_ccline; |
| 3199 | prev_ccline = *ccp; |
| 3200 | } |
| 3201 | |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3202 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3203 | /* |
| 3204 | * Save the command line into allocated memory. Returns a pointer to be |
| 3205 | * passed to restore_cmdline_alloc() later. |
| 3206 | * Returns NULL when failed. |
| 3207 | */ |
| 3208 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3209 | save_cmdline_alloc(void) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3210 | { |
| 3211 | struct cmdline_info *p; |
| 3212 | |
| 3213 | p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); |
| 3214 | if (p != NULL) |
| 3215 | save_cmdline(p); |
| 3216 | return (char_u *)p; |
| 3217 | } |
| 3218 | |
| 3219 | /* |
| 3220 | * Restore the command line from the return value of save_cmdline_alloc(). |
| 3221 | */ |
| 3222 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3223 | restore_cmdline_alloc(char_u *p) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3224 | { |
| 3225 | if (p != NULL) |
| 3226 | { |
| 3227 | restore_cmdline((struct cmdline_info *)p); |
| 3228 | vim_free(p); |
| 3229 | } |
| 3230 | } |
| 3231 | #endif |
| 3232 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3233 | /* |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3234 | * Paste a yank register into the command line. |
| 3235 | * Used by CTRL-R command in command-line mode. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3236 | * insert_reg() can't be used here, because special characters from the |
| 3237 | * register contents will be interpreted as commands. |
| 3238 | * |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3239 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3240 | */ |
| 3241 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3242 | cmdline_paste( |
| 3243 | int regname, |
| 3244 | int literally, /* Insert text literally instead of "as typed" */ |
| 3245 | int remcr) /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3246 | { |
| 3247 | long i; |
| 3248 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3249 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3250 | int allocated; |
| 3251 | struct cmdline_info save_ccline; |
| 3252 | |
| 3253 | /* check for valid regname; also accept special characters for CTRL-R in |
| 3254 | * the command line */ |
| 3255 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
| 3256 | && regname != Ctrl_A && !valid_yank_reg(regname, FALSE)) |
| 3257 | return FAIL; |
| 3258 | |
| 3259 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 3260 | * CTRL-C to break the loop. */ |
| 3261 | line_breakcheck(); |
| 3262 | if (got_int) |
| 3263 | return FAIL; |
| 3264 | |
| 3265 | #ifdef FEAT_CLIPBOARD |
| 3266 | regname = may_get_selection(regname); |
| 3267 | #endif |
| 3268 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3269 | /* Need to save and restore ccline. And set "textlock" to avoid nasty |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3270 | * things like going to another buffer when evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3271 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3272 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3273 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3274 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3275 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3276 | |
| 3277 | if (i) |
| 3278 | { |
| 3279 | /* Got the value of a special register in "arg". */ |
| 3280 | if (arg == NULL) |
| 3281 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3282 | |
| 3283 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3284 | * part of the word. */ |
| 3285 | p = arg; |
| 3286 | if (p_is && regname == Ctrl_W) |
| 3287 | { |
| 3288 | char_u *w; |
| 3289 | int len; |
| 3290 | |
| 3291 | /* Locate start of last word in the cmd buffer. */ |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3292 | for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3293 | { |
| 3294 | #ifdef FEAT_MBYTE |
| 3295 | if (has_mbyte) |
| 3296 | { |
| 3297 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3298 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3299 | break; |
| 3300 | w -= len; |
| 3301 | } |
| 3302 | else |
| 3303 | #endif |
| 3304 | { |
| 3305 | if (!vim_iswordc(w[-1])) |
| 3306 | break; |
| 3307 | --w; |
| 3308 | } |
| 3309 | } |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3310 | len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3311 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3312 | p += len; |
| 3313 | } |
| 3314 | |
| 3315 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3316 | if (allocated) |
| 3317 | vim_free(arg); |
| 3318 | return OK; |
| 3319 | } |
| 3320 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3321 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3322 | } |
| 3323 | |
| 3324 | /* |
| 3325 | * Put a string on the command line. |
| 3326 | * When "literally" is TRUE, insert literally. |
| 3327 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3328 | * line. |
| 3329 | */ |
| 3330 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3331 | cmdline_paste_str(char_u *s, int literally) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3332 | { |
| 3333 | int c, cv; |
| 3334 | |
| 3335 | if (literally) |
| 3336 | put_on_cmdline(s, -1, TRUE); |
| 3337 | else |
| 3338 | while (*s != NUL) |
| 3339 | { |
| 3340 | cv = *s; |
| 3341 | if (cv == Ctrl_V && s[1]) |
| 3342 | ++s; |
| 3343 | #ifdef FEAT_MBYTE |
| 3344 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3345 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3346 | else |
| 3347 | #endif |
| 3348 | c = *s++; |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 3349 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
| 3350 | || c == CAR || c == NL || c == Ctrl_L |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3351 | #ifdef UNIX |
| 3352 | || c == intr_char |
| 3353 | #endif |
| 3354 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3355 | stuffcharReadbuff(Ctrl_V); |
| 3356 | stuffcharReadbuff(c); |
| 3357 | } |
| 3358 | } |
| 3359 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3360 | #ifdef FEAT_WILDMENU |
| 3361 | /* |
| 3362 | * Delete characters on the command line, from "from" to the current |
| 3363 | * position. |
| 3364 | */ |
| 3365 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3366 | cmdline_del(int from) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3367 | { |
| 3368 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3369 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3370 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3371 | ccline.cmdpos = from; |
| 3372 | } |
| 3373 | #endif |
| 3374 | |
| 3375 | /* |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 3376 | * This function is called when the screen size changes and with incremental |
| 3377 | * search and in other situations where the command line may have been |
| 3378 | * overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3379 | */ |
| 3380 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3381 | redrawcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3382 | { |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3383 | redrawcmdline_ex(TRUE); |
| 3384 | } |
| 3385 | |
| 3386 | void |
| 3387 | redrawcmdline_ex(int do_compute_cmdrow) |
| 3388 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3389 | if (cmd_silent) |
| 3390 | return; |
| 3391 | need_wait_return = FALSE; |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3392 | if (do_compute_cmdrow) |
| 3393 | compute_cmdrow(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3394 | redrawcmd(); |
| 3395 | cursorcmd(); |
| 3396 | } |
| 3397 | |
| 3398 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3399 | redrawcmdprompt(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3400 | { |
| 3401 | int i; |
| 3402 | |
| 3403 | if (cmd_silent) |
| 3404 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3405 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3406 | msg_putchar(ccline.cmdfirstc); |
| 3407 | if (ccline.cmdprompt != NULL) |
| 3408 | { |
| 3409 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3410 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3411 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3412 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3413 | --ccline.cmdindent; |
| 3414 | } |
| 3415 | else |
| 3416 | for (i = ccline.cmdindent; i > 0; --i) |
| 3417 | msg_putchar(' '); |
| 3418 | } |
| 3419 | |
| 3420 | /* |
| 3421 | * Redraw what is currently on the command line. |
| 3422 | */ |
| 3423 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3424 | redrawcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3425 | { |
| 3426 | if (cmd_silent) |
| 3427 | return; |
| 3428 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3429 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3430 | if (ccline.cmdbuff == NULL) |
| 3431 | { |
| 3432 | windgoto(cmdline_row, 0); |
| 3433 | msg_clr_eos(); |
| 3434 | return; |
| 3435 | } |
| 3436 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3437 | msg_start(); |
| 3438 | redrawcmdprompt(); |
| 3439 | |
| 3440 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3441 | msg_no_more = TRUE; |
| 3442 | draw_cmdline(0, ccline.cmdlen); |
| 3443 | msg_clr_eos(); |
| 3444 | msg_no_more = FALSE; |
| 3445 | |
| 3446 | set_cmdspos_cursor(); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 3447 | if (extra_char != NUL) |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3448 | putcmdline(extra_char, extra_char_shift); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3449 | |
| 3450 | /* |
| 3451 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3452 | * in cmdline mode we can reset them now. |
| 3453 | */ |
| 3454 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3455 | |
| 3456 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3457 | * in cmdline mode */ |
| 3458 | skip_redraw = FALSE; |
| 3459 | } |
| 3460 | |
| 3461 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3462 | compute_cmdrow(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3463 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3464 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3465 | cmdline_row = Rows - 1; |
| 3466 | else |
| 3467 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
Bram Moolenaar | e0de17d | 2017-09-24 16:24:34 +0200 | [diff] [blame] | 3468 | + lastwin->w_status_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3469 | } |
| 3470 | |
| 3471 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3472 | cursorcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3473 | { |
| 3474 | if (cmd_silent) |
| 3475 | return; |
| 3476 | |
| 3477 | #ifdef FEAT_RIGHTLEFT |
| 3478 | if (cmdmsg_rl) |
| 3479 | { |
| 3480 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3481 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3482 | if (msg_row <= 0) |
| 3483 | msg_row = Rows - 1; |
| 3484 | } |
| 3485 | else |
| 3486 | #endif |
| 3487 | { |
| 3488 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3489 | msg_col = ccline.cmdspos % (int)Columns; |
| 3490 | if (msg_row >= Rows) |
| 3491 | msg_row = Rows - 1; |
| 3492 | } |
| 3493 | |
| 3494 | windgoto(msg_row, msg_col); |
| 3495 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 3496 | if (p_imst == IM_ON_THE_SPOT) |
| 3497 | redrawcmd_preedit(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3498 | #endif |
| 3499 | #ifdef MCH_CURSOR_SHAPE |
| 3500 | mch_update_cursor(); |
| 3501 | #endif |
| 3502 | } |
| 3503 | |
| 3504 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3505 | gotocmdline(int clr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3506 | { |
| 3507 | msg_start(); |
| 3508 | #ifdef FEAT_RIGHTLEFT |
| 3509 | if (cmdmsg_rl) |
| 3510 | msg_col = Columns - 1; |
| 3511 | else |
| 3512 | #endif |
| 3513 | msg_col = 0; /* always start in column 0 */ |
| 3514 | if (clr) /* clear the bottom line(s) */ |
| 3515 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3516 | windgoto(cmdline_row, 0); |
| 3517 | } |
| 3518 | |
| 3519 | /* |
| 3520 | * Check the word in front of the cursor for an abbreviation. |
| 3521 | * Called when the non-id character "c" has been entered. |
| 3522 | * When an abbreviation is recognized it is removed from the text with |
| 3523 | * backspaces and the replacement string is inserted, followed by "c". |
| 3524 | */ |
| 3525 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3526 | ccheck_abbr(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3527 | { |
| 3528 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3529 | return FALSE; |
| 3530 | |
| 3531 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0); |
| 3532 | } |
| 3533 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3534 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 3535 | static int |
| 3536 | #ifdef __BORLANDC__ |
| 3537 | _RTLENTRYF |
| 3538 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3539 | sort_func_compare(const void *s1, const void *s2) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3540 | { |
| 3541 | char_u *p1 = *(char_u **)s1; |
| 3542 | char_u *p2 = *(char_u **)s2; |
| 3543 | |
| 3544 | if (*p1 != '<' && *p2 == '<') return -1; |
| 3545 | if (*p1 == '<' && *p2 != '<') return 1; |
| 3546 | return STRCMP(p1, p2); |
| 3547 | } |
| 3548 | #endif |
| 3549 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3550 | /* |
| 3551 | * Return FAIL if this is not an appropriate context in which to do |
| 3552 | * completion of anything, return OK if it is (even if there are no matches). |
| 3553 | * For the caller, this means that the character is just passed through like a |
| 3554 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3555 | */ |
| 3556 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3557 | nextwild( |
| 3558 | expand_T *xp, |
| 3559 | int type, |
| 3560 | int options, /* extra options for ExpandOne() */ |
| 3561 | int escape) /* if TRUE, escape the returned matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3562 | { |
| 3563 | int i, j; |
| 3564 | char_u *p1; |
| 3565 | char_u *p2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3566 | int difflen; |
| 3567 | int v; |
| 3568 | |
| 3569 | if (xp->xp_numfiles == -1) |
| 3570 | { |
| 3571 | set_expand_context(xp); |
| 3572 | cmd_showtail = expand_showtail(xp); |
| 3573 | } |
| 3574 | |
| 3575 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3576 | { |
| 3577 | beep_flush(); |
| 3578 | return OK; /* Something illegal on command line */ |
| 3579 | } |
| 3580 | if (xp->xp_context == EXPAND_NOTHING) |
| 3581 | { |
| 3582 | /* Caller can use the character as a normal char instead */ |
| 3583 | return FAIL; |
| 3584 | } |
| 3585 | |
| 3586 | MSG_PUTS("..."); /* show that we are busy */ |
| 3587 | out_flush(); |
| 3588 | |
| 3589 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3590 | xp->xp_pattern_len = ccline.cmdpos - i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3591 | |
| 3592 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3593 | { |
| 3594 | /* |
| 3595 | * Get next/previous match for a previous expanded pattern. |
| 3596 | */ |
| 3597 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3598 | } |
| 3599 | else |
| 3600 | { |
| 3601 | /* |
| 3602 | * Translate string into pattern and expand it. |
| 3603 | */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3604 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 3605 | xp->xp_context)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3606 | p2 = NULL; |
| 3607 | else |
| 3608 | { |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3609 | int use_options = options | |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 3610 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 3611 | if (escape) |
| 3612 | use_options |= WILD_ESCAPE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3613 | |
| 3614 | if (p_wic) |
| 3615 | use_options += WILD_ICASE; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3616 | p2 = ExpandOne(xp, p1, |
| 3617 | vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3618 | use_options, type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3619 | vim_free(p1); |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3620 | /* longest match: make sure it is not shorter, happens with :help */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3621 | if (p2 != NULL && type == WILD_LONGEST) |
| 3622 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3623 | for (j = 0; j < xp->xp_pattern_len; ++j) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3624 | if (ccline.cmdbuff[i + j] == '*' |
| 3625 | || ccline.cmdbuff[i + j] == '?') |
| 3626 | break; |
| 3627 | if ((int)STRLEN(p2) < j) |
| 3628 | { |
| 3629 | vim_free(p2); |
| 3630 | p2 = NULL; |
| 3631 | } |
| 3632 | } |
| 3633 | } |
| 3634 | } |
| 3635 | |
| 3636 | if (p2 != NULL && !got_int) |
| 3637 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3638 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3639 | if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3640 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3641 | v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3642 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3643 | } |
| 3644 | else |
| 3645 | v = OK; |
| 3646 | if (v == OK) |
| 3647 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3648 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 3649 | &ccline.cmdbuff[ccline.cmdpos], |
| 3650 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3651 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3652 | ccline.cmdlen += difflen; |
| 3653 | ccline.cmdpos += difflen; |
| 3654 | } |
| 3655 | } |
| 3656 | vim_free(p2); |
| 3657 | |
| 3658 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3659 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3660 | |
| 3661 | /* When expanding a ":map" command and no matches are found, assume that |
| 3662 | * the key is supposed to be inserted literally */ |
| 3663 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 3664 | return FAIL; |
| 3665 | |
| 3666 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 3667 | beep_flush(); |
| 3668 | else if (xp->xp_numfiles == 1) |
| 3669 | /* free expanded pattern */ |
| 3670 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 3671 | |
| 3672 | return OK; |
| 3673 | } |
| 3674 | |
| 3675 | /* |
| 3676 | * Do wildcard expansion on the string 'str'. |
| 3677 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3678 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3679 | * Return NULL for failure. |
| 3680 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3681 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 3682 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 3683 | * WILD_PREV "orig" should be NULL. |
| 3684 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 3685 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 3686 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3687 | * |
| 3688 | * mode = WILD_FREE: just free previously expanded matches |
| 3689 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 3690 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 3691 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 3692 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 3693 | * mode = WILD_ALL: return all matches concatenated |
| 3694 | * mode = WILD_LONGEST: return longest matched part |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3695 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3696 | * |
| 3697 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 3698 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 3699 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 3700 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 3701 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 3702 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 3703 | * options = WILD_SILENT: don't print warning messages |
| 3704 | * options = WILD_ESCAPE: put backslash before special chars |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3705 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3706 | * |
| 3707 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 3708 | */ |
| 3709 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3710 | ExpandOne( |
| 3711 | expand_T *xp, |
| 3712 | char_u *str, |
| 3713 | char_u *orig, /* allocated copy of original of expanded string */ |
| 3714 | int options, |
| 3715 | int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3716 | { |
| 3717 | char_u *ss = NULL; |
| 3718 | static int findex; |
| 3719 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3720 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3721 | int i; |
| 3722 | long_u len; |
| 3723 | int non_suf_match; /* number without matching suffix */ |
| 3724 | |
| 3725 | /* |
| 3726 | * first handle the case of using an old match |
| 3727 | */ |
| 3728 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 3729 | { |
| 3730 | if (xp->xp_numfiles > 0) |
| 3731 | { |
| 3732 | if (mode == WILD_PREV) |
| 3733 | { |
| 3734 | if (findex == -1) |
| 3735 | findex = xp->xp_numfiles; |
| 3736 | --findex; |
| 3737 | } |
| 3738 | else /* mode == WILD_NEXT */ |
| 3739 | ++findex; |
| 3740 | |
| 3741 | /* |
| 3742 | * When wrapping around, return the original string, set findex to |
| 3743 | * -1. |
| 3744 | */ |
| 3745 | if (findex < 0) |
| 3746 | { |
| 3747 | if (orig_save == NULL) |
| 3748 | findex = xp->xp_numfiles - 1; |
| 3749 | else |
| 3750 | findex = -1; |
| 3751 | } |
| 3752 | if (findex >= xp->xp_numfiles) |
| 3753 | { |
| 3754 | if (orig_save == NULL) |
| 3755 | findex = 0; |
| 3756 | else |
| 3757 | findex = -1; |
| 3758 | } |
| 3759 | #ifdef FEAT_WILDMENU |
| 3760 | if (p_wmnu) |
| 3761 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 3762 | findex, cmd_showtail); |
| 3763 | #endif |
| 3764 | if (findex == -1) |
| 3765 | return vim_strsave(orig_save); |
| 3766 | return vim_strsave(xp->xp_files[findex]); |
| 3767 | } |
| 3768 | else |
| 3769 | return NULL; |
| 3770 | } |
| 3771 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3772 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3773 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 3774 | { |
| 3775 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3776 | xp->xp_numfiles = -1; |
| 3777 | vim_free(orig_save); |
| 3778 | orig_save = NULL; |
| 3779 | } |
| 3780 | findex = 0; |
| 3781 | |
| 3782 | if (mode == WILD_FREE) /* only release file name */ |
| 3783 | return NULL; |
| 3784 | |
| 3785 | if (xp->xp_numfiles == -1) |
| 3786 | { |
| 3787 | vim_free(orig_save); |
| 3788 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3789 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3790 | |
| 3791 | /* |
| 3792 | * Do the expansion. |
| 3793 | */ |
| 3794 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 3795 | options) == FAIL) |
| 3796 | { |
| 3797 | #ifdef FNAME_ILLEGAL |
| 3798 | /* Illegal file name has been silently skipped. But when there |
| 3799 | * are wildcards, the real problem is that there was no match, |
| 3800 | * causing the pattern to be added, which has illegal characters. |
| 3801 | */ |
| 3802 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 3803 | EMSG2(_(e_nomatch2), str); |
| 3804 | #endif |
| 3805 | } |
| 3806 | else if (xp->xp_numfiles == 0) |
| 3807 | { |
| 3808 | if (!(options & WILD_SILENT)) |
| 3809 | EMSG2(_(e_nomatch2), str); |
| 3810 | } |
| 3811 | else |
| 3812 | { |
| 3813 | /* Escape the matches for use on the command line. */ |
| 3814 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 3815 | |
| 3816 | /* |
| 3817 | * Check for matching suffixes in file names. |
| 3818 | */ |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3819 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 3820 | && mode != WILD_LONGEST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3821 | { |
| 3822 | if (xp->xp_numfiles) |
| 3823 | non_suf_match = xp->xp_numfiles; |
| 3824 | else |
| 3825 | non_suf_match = 1; |
| 3826 | if ((xp->xp_context == EXPAND_FILES |
| 3827 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3828 | && xp->xp_numfiles > 1) |
| 3829 | { |
| 3830 | /* |
| 3831 | * More than one match; check suffix. |
| 3832 | * The files will have been sorted on matching suffix in |
| 3833 | * expand_wildcards, only need to check the first two. |
| 3834 | */ |
| 3835 | non_suf_match = 0; |
| 3836 | for (i = 0; i < 2; ++i) |
| 3837 | if (match_suffix(xp->xp_files[i])) |
| 3838 | ++non_suf_match; |
| 3839 | } |
| 3840 | if (non_suf_match != 1) |
| 3841 | { |
| 3842 | /* Can we ever get here unless it's while expanding |
| 3843 | * interactively? If not, we can get rid of this all |
| 3844 | * together. Don't really want to wait for this message |
| 3845 | * (and possibly have to hit return to continue!). |
| 3846 | */ |
| 3847 | if (!(options & WILD_SILENT)) |
| 3848 | EMSG(_(e_toomany)); |
| 3849 | else if (!(options & WILD_NO_BEEP)) |
| 3850 | beep_flush(); |
| 3851 | } |
| 3852 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 3853 | ss = vim_strsave(xp->xp_files[0]); |
| 3854 | } |
| 3855 | } |
| 3856 | } |
| 3857 | |
| 3858 | /* Find longest common part */ |
| 3859 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 3860 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3861 | int mb_len = 1; |
| 3862 | int c0, ci; |
| 3863 | |
| 3864 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3865 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3866 | #ifdef FEAT_MBYTE |
| 3867 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3868 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3869 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 3870 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 3871 | } |
| 3872 | else |
| 3873 | #endif |
Bram Moolenaar | e4eda3b | 2015-11-21 16:28:50 +0100 | [diff] [blame] | 3874 | c0 = xp->xp_files[0][len]; |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3875 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 3876 | { |
| 3877 | #ifdef FEAT_MBYTE |
| 3878 | if (has_mbyte) |
| 3879 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 3880 | else |
| 3881 | #endif |
| 3882 | ci = xp->xp_files[i][len]; |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3883 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3884 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3885 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3886 | || xp->xp_context == EXPAND_BUFFERS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3887 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3888 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3889 | break; |
| 3890 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3891 | else if (c0 != ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3892 | break; |
| 3893 | } |
| 3894 | if (i < xp->xp_numfiles) |
| 3895 | { |
| 3896 | if (!(options & WILD_NO_BEEP)) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3897 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3898 | break; |
| 3899 | } |
| 3900 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3901 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3902 | ss = alloc((unsigned)len + 1); |
| 3903 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3904 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3905 | findex = -1; /* next p_wc gets first one */ |
| 3906 | } |
| 3907 | |
| 3908 | /* Concatenate all matching names */ |
| 3909 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 3910 | { |
| 3911 | len = 0; |
| 3912 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3913 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 3914 | ss = lalloc(len, TRUE); |
| 3915 | if (ss != NULL) |
| 3916 | { |
| 3917 | *ss = NUL; |
| 3918 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3919 | { |
| 3920 | STRCAT(ss, xp->xp_files[i]); |
| 3921 | if (i != xp->xp_numfiles - 1) |
| 3922 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 3923 | } |
| 3924 | } |
| 3925 | } |
| 3926 | |
| 3927 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 3928 | ExpandCleanup(xp); |
| 3929 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3930 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3931 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3932 | vim_free(orig); |
| 3933 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3934 | return ss; |
| 3935 | } |
| 3936 | |
| 3937 | /* |
| 3938 | * Prepare an expand structure for use. |
| 3939 | */ |
| 3940 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3941 | ExpandInit(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3942 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3943 | xp->xp_pattern = NULL; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3944 | xp->xp_pattern_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3945 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3946 | #ifndef BACKSLASH_IN_FILENAME |
| 3947 | xp->xp_shell = FALSE; |
| 3948 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3949 | xp->xp_numfiles = -1; |
| 3950 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3951 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 3952 | xp->xp_arg = NULL; |
| 3953 | #endif |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 3954 | xp->xp_line = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3955 | } |
| 3956 | |
| 3957 | /* |
| 3958 | * Cleanup an expand structure after use. |
| 3959 | */ |
| 3960 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3961 | ExpandCleanup(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3962 | { |
| 3963 | if (xp->xp_numfiles >= 0) |
| 3964 | { |
| 3965 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3966 | xp->xp_numfiles = -1; |
| 3967 | } |
| 3968 | } |
| 3969 | |
| 3970 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3971 | ExpandEscape( |
| 3972 | expand_T *xp, |
| 3973 | char_u *str, |
| 3974 | int numfiles, |
| 3975 | char_u **files, |
| 3976 | int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3977 | { |
| 3978 | int i; |
| 3979 | char_u *p; |
| 3980 | |
| 3981 | /* |
| 3982 | * May change home directory back to "~" |
| 3983 | */ |
| 3984 | if (options & WILD_HOME_REPLACE) |
| 3985 | tilde_replace(str, numfiles, files); |
| 3986 | |
| 3987 | if (options & WILD_ESCAPE) |
| 3988 | { |
| 3989 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | cca92ec | 2011-04-28 17:21:53 +0200 | [diff] [blame] | 3990 | || xp->xp_context == EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3991 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3992 | || xp->xp_context == EXPAND_BUFFERS |
| 3993 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3994 | { |
| 3995 | /* |
| 3996 | * Insert a backslash into a file name before a space, \, %, # |
| 3997 | * and wildmatch characters, except '~'. |
| 3998 | */ |
| 3999 | for (i = 0; i < numfiles; ++i) |
| 4000 | { |
| 4001 | /* for ":set path=" we need to escape spaces twice */ |
| 4002 | if (xp->xp_backslash == XP_BS_THREE) |
| 4003 | { |
| 4004 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4005 | if (p != NULL) |
| 4006 | { |
| 4007 | vim_free(files[i]); |
| 4008 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 4009 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4010 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4011 | if (p != NULL) |
| 4012 | { |
| 4013 | vim_free(files[i]); |
| 4014 | files[i] = p; |
| 4015 | } |
| 4016 | #endif |
| 4017 | } |
| 4018 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4019 | #ifdef BACKSLASH_IN_FILENAME |
| 4020 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 4021 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4022 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4023 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4024 | if (p != NULL) |
| 4025 | { |
| 4026 | vim_free(files[i]); |
| 4027 | files[i] = p; |
| 4028 | } |
| 4029 | |
| 4030 | /* If 'str' starts with "\~", replace "~" at start of |
| 4031 | * files[i] with "\~". */ |
| 4032 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4033 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4034 | } |
| 4035 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4036 | |
| 4037 | /* If the first file starts with a '+' escape it. Otherwise it |
| 4038 | * could be seen as "+cmd". */ |
| 4039 | if (*files[0] == '+') |
| 4040 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4041 | } |
| 4042 | else if (xp->xp_context == EXPAND_TAGS) |
| 4043 | { |
| 4044 | /* |
| 4045 | * Insert a backslash before characters in a tag name that |
| 4046 | * would terminate the ":tag" command. |
| 4047 | */ |
| 4048 | for (i = 0; i < numfiles; ++i) |
| 4049 | { |
| 4050 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 4051 | if (p != NULL) |
| 4052 | { |
| 4053 | vim_free(files[i]); |
| 4054 | files[i] = p; |
| 4055 | } |
| 4056 | } |
| 4057 | } |
| 4058 | } |
| 4059 | } |
| 4060 | |
| 4061 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4062 | * Escape special characters in "fname" for when used as a file name argument |
| 4063 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 4064 | * Returns the result in allocated memory. |
| 4065 | */ |
| 4066 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4067 | vim_strsave_fnameescape(char_u *fname, int shell) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4068 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4069 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4070 | #ifdef BACKSLASH_IN_FILENAME |
| 4071 | char_u buf[20]; |
| 4072 | int j = 0; |
| 4073 | |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4074 | /* Don't escape '[', '{' and '!' if they are in 'isfname'. */ |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4075 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4076 | if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p)) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4077 | buf[j++] = *p; |
| 4078 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4079 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4080 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4081 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 4082 | if (shell && csh_like_shell() && p != NULL) |
| 4083 | { |
| 4084 | char_u *s; |
| 4085 | |
| 4086 | /* For csh and similar shells need to put two backslashes before '!'. |
| 4087 | * One is taken by Vim, one by the shell. */ |
| 4088 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 4089 | vim_free(p); |
| 4090 | p = s; |
| 4091 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4092 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4093 | |
| 4094 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 4095 | * ":write". "cd -" has a special meaning. */ |
Bram Moolenaar | a9d52e3 | 2010-07-31 16:44:19 +0200 | [diff] [blame] | 4096 | if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4097 | escape_fname(&p); |
| 4098 | |
| 4099 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4100 | } |
| 4101 | |
| 4102 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4103 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 4104 | */ |
| 4105 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4106 | escape_fname(char_u **pp) |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4107 | { |
| 4108 | char_u *p; |
| 4109 | |
| 4110 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 4111 | if (p != NULL) |
| 4112 | { |
| 4113 | p[0] = '\\'; |
| 4114 | STRCPY(p + 1, *pp); |
| 4115 | vim_free(*pp); |
| 4116 | *pp = p; |
| 4117 | } |
| 4118 | } |
| 4119 | |
| 4120 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4121 | * For each file name in files[num_files]: |
| 4122 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 4123 | */ |
| 4124 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4125 | tilde_replace( |
| 4126 | char_u *orig_pat, |
| 4127 | int num_files, |
| 4128 | char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4129 | { |
| 4130 | int i; |
| 4131 | char_u *p; |
| 4132 | |
| 4133 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 4134 | { |
| 4135 | for (i = 0; i < num_files; ++i) |
| 4136 | { |
| 4137 | p = home_replace_save(NULL, files[i]); |
| 4138 | if (p != NULL) |
| 4139 | { |
| 4140 | vim_free(files[i]); |
| 4141 | files[i] = p; |
| 4142 | } |
| 4143 | } |
| 4144 | } |
| 4145 | } |
| 4146 | |
| 4147 | /* |
| 4148 | * Show all matches for completion on the command line. |
| 4149 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 4150 | * be inserted like a normal character. |
| 4151 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4152 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4153 | showmatches(expand_T *xp, int wildmenu UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4154 | { |
| 4155 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 4156 | int num_files; |
| 4157 | char_u **files_found; |
| 4158 | int i, j, k; |
| 4159 | int maxlen; |
| 4160 | int lines; |
| 4161 | int columns; |
| 4162 | char_u *p; |
| 4163 | int lastlen; |
| 4164 | int attr; |
| 4165 | int showtail; |
| 4166 | |
| 4167 | if (xp->xp_numfiles == -1) |
| 4168 | { |
| 4169 | set_expand_context(xp); |
| 4170 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 4171 | &num_files, &files_found); |
| 4172 | showtail = expand_showtail(xp); |
| 4173 | if (i != EXPAND_OK) |
| 4174 | return i; |
| 4175 | |
| 4176 | } |
| 4177 | else |
| 4178 | { |
| 4179 | num_files = xp->xp_numfiles; |
| 4180 | files_found = xp->xp_files; |
| 4181 | showtail = cmd_showtail; |
| 4182 | } |
| 4183 | |
| 4184 | #ifdef FEAT_WILDMENU |
| 4185 | if (!wildmenu) |
| 4186 | { |
| 4187 | #endif |
| 4188 | msg_didany = FALSE; /* lines_left will be set */ |
| 4189 | msg_start(); /* prepare for paging */ |
| 4190 | msg_putchar('\n'); |
| 4191 | out_flush(); |
| 4192 | cmdline_row = msg_row; |
| 4193 | msg_didany = FALSE; /* lines_left will be set again */ |
| 4194 | msg_start(); /* prepare for paging */ |
| 4195 | #ifdef FEAT_WILDMENU |
| 4196 | } |
| 4197 | #endif |
| 4198 | |
| 4199 | if (got_int) |
| 4200 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 4201 | #ifdef FEAT_WILDMENU |
| 4202 | else if (wildmenu) |
Bram Moolenaar | ef8eb08 | 2017-03-30 22:04:55 +0200 | [diff] [blame] | 4203 | win_redr_status_matches(xp, num_files, files_found, -1, showtail); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4204 | #endif |
| 4205 | else |
| 4206 | { |
| 4207 | /* find the length of the longest file name */ |
| 4208 | maxlen = 0; |
| 4209 | for (i = 0; i < num_files; ++i) |
| 4210 | { |
| 4211 | if (!showtail && (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4212 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4213 | || xp->xp_context == EXPAND_BUFFERS)) |
| 4214 | { |
| 4215 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 4216 | j = vim_strsize(NameBuff); |
| 4217 | } |
| 4218 | else |
| 4219 | j = vim_strsize(L_SHOWFILE(i)); |
| 4220 | if (j > maxlen) |
| 4221 | maxlen = j; |
| 4222 | } |
| 4223 | |
| 4224 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4225 | lines = num_files; |
| 4226 | else |
| 4227 | { |
| 4228 | /* compute the number of columns and lines for the listing */ |
| 4229 | maxlen += 2; /* two spaces between file names */ |
| 4230 | columns = ((int)Columns + 2) / maxlen; |
| 4231 | if (columns < 1) |
| 4232 | columns = 1; |
| 4233 | lines = (num_files + columns - 1) / columns; |
| 4234 | } |
| 4235 | |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4236 | attr = HL_ATTR(HLF_D); /* find out highlighting for directories */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4237 | |
| 4238 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4239 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4240 | MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4241 | msg_clr_eos(); |
| 4242 | msg_advance(maxlen - 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4243 | MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4244 | } |
| 4245 | |
| 4246 | /* list the files line by line */ |
| 4247 | for (i = 0; i < lines; ++i) |
| 4248 | { |
| 4249 | lastlen = 999; |
| 4250 | for (k = i; k < num_files; k += lines) |
| 4251 | { |
| 4252 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4253 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4254 | msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4255 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 4256 | msg_advance(maxlen + 1); |
| 4257 | msg_puts(p); |
| 4258 | msg_advance(maxlen + 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4259 | msg_puts_long_attr(p + 2, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4260 | break; |
| 4261 | } |
| 4262 | for (j = maxlen - lastlen; --j >= 0; ) |
| 4263 | msg_putchar(' '); |
| 4264 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4265 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4266 | || xp->xp_context == EXPAND_BUFFERS) |
| 4267 | { |
Bram Moolenaar | b91e59b | 2010-03-17 19:13:27 +0100 | [diff] [blame] | 4268 | /* highlight directories */ |
Bram Moolenaar | 63fa526 | 2010-03-23 18:06:52 +0100 | [diff] [blame] | 4269 | if (xp->xp_numfiles != -1) |
| 4270 | { |
| 4271 | char_u *halved_slash; |
| 4272 | char_u *exp_path; |
| 4273 | |
| 4274 | /* Expansion was done before and special characters |
| 4275 | * were escaped, need to halve backslashes. Also |
| 4276 | * $HOME has been replaced with ~/. */ |
| 4277 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
| 4278 | halved_slash = backslash_halve_save( |
| 4279 | exp_path != NULL ? exp_path : files_found[k]); |
| 4280 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 4281 | : files_found[k]); |
| 4282 | vim_free(exp_path); |
| 4283 | vim_free(halved_slash); |
| 4284 | } |
| 4285 | else |
| 4286 | /* Expansion was done here, file names are literal. */ |
| 4287 | j = mch_isdir(files_found[k]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4288 | if (showtail) |
| 4289 | p = L_SHOWFILE(k); |
| 4290 | else |
| 4291 | { |
| 4292 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 4293 | TRUE); |
| 4294 | p = NameBuff; |
| 4295 | } |
| 4296 | } |
| 4297 | else |
| 4298 | { |
| 4299 | j = FALSE; |
| 4300 | p = L_SHOWFILE(k); |
| 4301 | } |
| 4302 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 4303 | } |
| 4304 | if (msg_col > 0) /* when not wrapped around */ |
| 4305 | { |
| 4306 | msg_clr_eos(); |
| 4307 | msg_putchar('\n'); |
| 4308 | } |
| 4309 | out_flush(); /* show one line at a time */ |
| 4310 | if (got_int) |
| 4311 | { |
| 4312 | got_int = FALSE; |
| 4313 | break; |
| 4314 | } |
| 4315 | } |
| 4316 | |
| 4317 | /* |
| 4318 | * we redraw the command below the lines that we have just listed |
| 4319 | * This is a bit tricky, but it saves a lot of screen updating. |
| 4320 | */ |
| 4321 | cmdline_row = msg_row; /* will put it back later */ |
| 4322 | } |
| 4323 | |
| 4324 | if (xp->xp_numfiles == -1) |
| 4325 | FreeWild(num_files, files_found); |
| 4326 | |
| 4327 | return EXPAND_OK; |
| 4328 | } |
| 4329 | |
| 4330 | /* |
| 4331 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 4332 | * Find tail of file name path, but ignore trailing "/". |
| 4333 | */ |
| 4334 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4335 | sm_gettail(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4336 | { |
| 4337 | char_u *p; |
| 4338 | char_u *t = s; |
| 4339 | int had_sep = FALSE; |
| 4340 | |
| 4341 | for (p = s; *p != NUL; ) |
| 4342 | { |
| 4343 | if (vim_ispathsep(*p) |
| 4344 | #ifdef BACKSLASH_IN_FILENAME |
| 4345 | && !rem_backslash(p) |
| 4346 | #endif |
| 4347 | ) |
| 4348 | had_sep = TRUE; |
| 4349 | else if (had_sep) |
| 4350 | { |
| 4351 | t = p; |
| 4352 | had_sep = FALSE; |
| 4353 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4354 | MB_PTR_ADV(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4355 | } |
| 4356 | return t; |
| 4357 | } |
| 4358 | |
| 4359 | /* |
| 4360 | * Return TRUE if we only need to show the tail of completion matches. |
| 4361 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4362 | * returned. |
| 4363 | */ |
| 4364 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4365 | expand_showtail(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4366 | { |
| 4367 | char_u *s; |
| 4368 | char_u *end; |
| 4369 | |
| 4370 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4371 | if (xp->xp_context != EXPAND_FILES |
| 4372 | && xp->xp_context != EXPAND_SHELLCMD |
| 4373 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4374 | return FALSE; |
| 4375 | |
| 4376 | end = gettail(xp->xp_pattern); |
| 4377 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4378 | return FALSE; |
| 4379 | |
| 4380 | for (s = xp->xp_pattern; s < end; s++) |
| 4381 | { |
| 4382 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4383 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4384 | if (rem_backslash(s)) |
| 4385 | ++s; |
| 4386 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4387 | return FALSE; |
| 4388 | } |
| 4389 | return TRUE; |
| 4390 | } |
| 4391 | |
| 4392 | /* |
| 4393 | * Prepare a string for expansion. |
| 4394 | * When expanding file names: The string will be used with expand_wildcards(). |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4395 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4396 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4397 | * the name into allocated memory and prepend "^". |
| 4398 | */ |
| 4399 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4400 | addstar( |
| 4401 | char_u *fname, |
| 4402 | int len, |
| 4403 | int context) /* EXPAND_FILES etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4404 | { |
| 4405 | char_u *retval; |
| 4406 | int i, j; |
| 4407 | int new_len; |
| 4408 | char_u *tail; |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4409 | int ends_in_star; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4410 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4411 | if (context != EXPAND_FILES |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4412 | && context != EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4413 | && context != EXPAND_SHELLCMD |
| 4414 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4415 | { |
| 4416 | /* |
| 4417 | * Matching will be done internally (on something other than files). |
| 4418 | * So we convert the file-matching-type wildcards into our kind for |
| 4419 | * use with vim_regcomp(). First work out how long it will be: |
| 4420 | */ |
| 4421 | |
| 4422 | /* For help tags the translation is done in find_help_tags(). |
| 4423 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4424 | if (context == EXPAND_HELP |
| 4425 | || context == EXPAND_COLORS |
| 4426 | || context == EXPAND_COMPILER |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4427 | || context == EXPAND_OWNSYNTAX |
Bram Moolenaar | 883f5d0 | 2010-06-21 06:24:34 +0200 | [diff] [blame] | 4428 | || context == EXPAND_FILETYPE |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4429 | || context == EXPAND_PACKADD |
Bram Moolenaar | ba47b51 | 2017-01-24 21:18:19 +0100 | [diff] [blame] | 4430 | || ((context == EXPAND_TAGS_LISTFILES |
| 4431 | || context == EXPAND_TAGS) |
| 4432 | && fname[0] == '/')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4433 | retval = vim_strnsave(fname, len); |
| 4434 | else |
| 4435 | { |
| 4436 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4437 | for (i = 0; i < len; i++) |
| 4438 | { |
| 4439 | if (fname[i] == '*' || fname[i] == '~') |
| 4440 | new_len++; /* '*' needs to be replaced by ".*" |
| 4441 | '~' needs to be replaced by "\~" */ |
| 4442 | |
| 4443 | /* Buffer names are like file names. "." should be literal */ |
| 4444 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4445 | new_len++; /* "." becomes "\." */ |
| 4446 | |
| 4447 | /* Custom expansion takes care of special things, match |
| 4448 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4449 | if ((context == EXPAND_USER_DEFINED |
| 4450 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4451 | new_len++; /* '\' becomes "\\" */ |
| 4452 | } |
| 4453 | retval = alloc(new_len); |
| 4454 | if (retval != NULL) |
| 4455 | { |
| 4456 | retval[0] = '^'; |
| 4457 | j = 1; |
| 4458 | for (i = 0; i < len; i++, j++) |
| 4459 | { |
| 4460 | /* Skip backslash. But why? At least keep it for custom |
| 4461 | * expansion. */ |
| 4462 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4463 | && context != EXPAND_USER_LIST |
| 4464 | && fname[i] == '\\' |
| 4465 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4466 | break; |
| 4467 | |
| 4468 | switch (fname[i]) |
| 4469 | { |
| 4470 | case '*': retval[j++] = '.'; |
| 4471 | break; |
| 4472 | case '~': retval[j++] = '\\'; |
| 4473 | break; |
| 4474 | case '?': retval[j] = '.'; |
| 4475 | continue; |
| 4476 | case '.': if (context == EXPAND_BUFFERS) |
| 4477 | retval[j++] = '\\'; |
| 4478 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4479 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4480 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4481 | retval[j++] = '\\'; |
| 4482 | break; |
| 4483 | } |
| 4484 | retval[j] = fname[i]; |
| 4485 | } |
| 4486 | retval[j] = NUL; |
| 4487 | } |
| 4488 | } |
| 4489 | } |
| 4490 | else |
| 4491 | { |
| 4492 | retval = alloc(len + 4); |
| 4493 | if (retval != NULL) |
| 4494 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4495 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4496 | |
| 4497 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4498 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4499 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4500 | * ~ would be at the start of the file name, but not the tail. |
| 4501 | * $ could be anywhere in the tail. |
| 4502 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4503 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4504 | */ |
| 4505 | tail = gettail(retval); |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4506 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 4507 | #ifndef BACKSLASH_IN_FILENAME |
| 4508 | for (i = len - 2; i >= 0; --i) |
| 4509 | { |
| 4510 | if (retval[i] != '\\') |
| 4511 | break; |
| 4512 | ends_in_star = !ends_in_star; |
| 4513 | } |
| 4514 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4515 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4516 | && !ends_in_star |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4517 | && vim_strchr(tail, '$') == NULL |
| 4518 | && vim_strchr(retval, '`') == NULL) |
| 4519 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4520 | else if (len > 0 && retval[len - 1] == '$') |
| 4521 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4522 | retval[len] = NUL; |
| 4523 | } |
| 4524 | } |
| 4525 | return retval; |
| 4526 | } |
| 4527 | |
| 4528 | /* |
| 4529 | * Must parse the command line so far to work out what context we are in. |
| 4530 | * Completion can then be done based on that context. |
| 4531 | * This routine sets the variables: |
| 4532 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4533 | * the command line (ends at the cursor). |
| 4534 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4535 | * |
| 4536 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4537 | * the command line, like an unknown command. Caller |
| 4538 | * should beep. |
| 4539 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4540 | * a normal char, rather than for completion. eg |
| 4541 | * :s/^I/ |
| 4542 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4543 | * it. |
| 4544 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4545 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4546 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4547 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4548 | * when we know only directories are of interest. eg |
| 4549 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4550 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4551 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4552 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4553 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4554 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4555 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4556 | * EXPAND_EVENTS Complete event names |
| 4557 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4558 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4559 | * EXPAND_AUGROUP Complete autocommand group names |
| 4560 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4561 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4562 | * eg :unmap a^I , :cunab x^I |
| 4563 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4564 | * eg :call sub^I |
| 4565 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4566 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4567 | * names in expressions, eg :while s^I |
| 4568 | * EXPAND_ENV_VARS Complete environment variable names |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4569 | * EXPAND_USER Complete user names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4570 | */ |
| 4571 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4572 | set_expand_context(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4573 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4574 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4575 | if (ccline.cmdfirstc != ':' |
| 4576 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4577 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4578 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4579 | #endif |
| 4580 | ) |
| 4581 | { |
| 4582 | xp->xp_context = EXPAND_NOTHING; |
| 4583 | return; |
| 4584 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4585 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4586 | } |
| 4587 | |
| 4588 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4589 | set_cmd_context( |
| 4590 | expand_T *xp, |
| 4591 | char_u *str, /* start of command line */ |
| 4592 | int len, /* length of command line (excl. NUL) */ |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4593 | int col, /* position of cursor */ |
| 4594 | int use_ccline UNUSED) /* use ccline for info */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4595 | { |
| 4596 | int old_char = NUL; |
| 4597 | char_u *nextcomm; |
| 4598 | |
| 4599 | /* |
| 4600 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4601 | * written before. |
| 4602 | */ |
| 4603 | if (col < len) |
| 4604 | old_char = str[col]; |
| 4605 | str[col] = NUL; |
| 4606 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4607 | |
| 4608 | #ifdef FEAT_EVAL |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4609 | if (use_ccline && ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4610 | { |
| 4611 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4612 | /* pass CMD_SIZE because there is no real command */ |
| 4613 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4614 | # endif |
| 4615 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4616 | else if (use_ccline && ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4617 | { |
| 4618 | xp->xp_context = ccline.xp_context; |
| 4619 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4620 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4621 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4622 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4623 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4624 | else |
| 4625 | #endif |
| 4626 | while (nextcomm != NULL) |
| 4627 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4628 | |
Bram Moolenaar | a4c8dcb | 2013-06-30 12:21:24 +0200 | [diff] [blame] | 4629 | /* Store the string here so that call_user_expand_func() can get to them |
| 4630 | * easily. */ |
| 4631 | xp->xp_line = str; |
| 4632 | xp->xp_col = col; |
| 4633 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4634 | str[col] = old_char; |
| 4635 | } |
| 4636 | |
| 4637 | /* |
| 4638 | * Expand the command line "str" from context "xp". |
| 4639 | * "xp" must have been set by set_cmd_context(). |
| 4640 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4641 | * starts. |
| 4642 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4643 | * cursor. |
| 4644 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 4645 | * key that triggered expansion literally. |
| 4646 | * Returns EXPAND_OK otherwise. |
| 4647 | */ |
| 4648 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4649 | expand_cmdline( |
| 4650 | expand_T *xp, |
| 4651 | char_u *str, /* start of command line */ |
| 4652 | int col, /* position of cursor */ |
| 4653 | int *matchcount, /* return: nr of matches */ |
| 4654 | char_u ***matches) /* return: array of pointers to matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4655 | { |
| 4656 | char_u *file_str = NULL; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4657 | int options = WILD_ADD_SLASH|WILD_SILENT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4658 | |
| 4659 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 4660 | { |
| 4661 | beep_flush(); |
| 4662 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 4663 | } |
| 4664 | if (xp->xp_context == EXPAND_NOTHING) |
| 4665 | { |
| 4666 | /* Caller can use the character as a normal char instead */ |
| 4667 | return EXPAND_NOTHING; |
| 4668 | } |
| 4669 | |
| 4670 | /* add star to file name, or convert to regexp if not exp. files. */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 4671 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 4672 | file_str = addstar(xp->xp_pattern, xp->xp_pattern_len, xp->xp_context); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4673 | if (file_str == NULL) |
| 4674 | return EXPAND_UNSUCCESSFUL; |
| 4675 | |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4676 | if (p_wic) |
| 4677 | options += WILD_ICASE; |
| 4678 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4679 | /* find all files that match the description */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4680 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4681 | { |
| 4682 | *matchcount = 0; |
| 4683 | *matches = NULL; |
| 4684 | } |
| 4685 | vim_free(file_str); |
| 4686 | |
| 4687 | return EXPAND_OK; |
| 4688 | } |
| 4689 | |
| 4690 | #ifdef FEAT_MULTI_LANG |
| 4691 | /* |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4692 | * Cleanup matches for help tags: |
| 4693 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 4694 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4695 | */ |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 4696 | static void cleanup_help_tags(int num_file, char_u **file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4697 | |
| 4698 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4699 | cleanup_help_tags(int num_file, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4700 | { |
| 4701 | int i, j; |
| 4702 | int len; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4703 | char_u buf[4]; |
| 4704 | char_u *p = buf; |
| 4705 | |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4706 | if (p_hlg[0] != NUL && (p_hlg[0] != 'e' || p_hlg[1] != 'n')) |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4707 | { |
| 4708 | *p++ = '@'; |
| 4709 | *p++ = p_hlg[0]; |
| 4710 | *p++ = p_hlg[1]; |
| 4711 | } |
| 4712 | *p = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4713 | |
| 4714 | for (i = 0; i < num_file; ++i) |
| 4715 | { |
| 4716 | len = (int)STRLEN(file[i]) - 3; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4717 | if (len <= 0) |
| 4718 | continue; |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4719 | if (STRCMP(file[i] + len, "@en") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4720 | { |
| 4721 | /* Sorting on priority means the same item in another language may |
| 4722 | * be anywhere. Search all items for a match up to the "@en". */ |
| 4723 | for (j = 0; j < num_file; ++j) |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4724 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 4725 | && STRNCMP(file[i], file[j], len + 1) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4726 | break; |
| 4727 | if (j == num_file) |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4728 | /* item only exists with @en, remove it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4729 | file[i][len] = NUL; |
| 4730 | } |
| 4731 | } |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4732 | |
| 4733 | if (*buf != NUL) |
| 4734 | for (i = 0; i < num_file; ++i) |
| 4735 | { |
| 4736 | len = (int)STRLEN(file[i]) - 3; |
| 4737 | if (len <= 0) |
| 4738 | continue; |
| 4739 | if (STRCMP(file[i] + len, buf) == 0) |
| 4740 | { |
| 4741 | /* remove the default language */ |
| 4742 | file[i][len] = NUL; |
| 4743 | } |
| 4744 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4745 | } |
| 4746 | #endif |
| 4747 | |
| 4748 | /* |
| 4749 | * Do the expansion based on xp->xp_context and "pat". |
| 4750 | */ |
| 4751 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4752 | ExpandFromContext( |
| 4753 | expand_T *xp, |
| 4754 | char_u *pat, |
| 4755 | int *num_file, |
| 4756 | char_u ***file, |
| 4757 | int options) /* EW_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4758 | { |
| 4759 | #ifdef FEAT_CMDL_COMPL |
| 4760 | regmatch_T regmatch; |
| 4761 | #endif |
| 4762 | int ret; |
| 4763 | int flags; |
| 4764 | |
| 4765 | flags = EW_DIR; /* include directories */ |
| 4766 | if (options & WILD_LIST_NOTFOUND) |
| 4767 | flags |= EW_NOTFOUND; |
| 4768 | if (options & WILD_ADD_SLASH) |
| 4769 | flags |= EW_ADDSLASH; |
| 4770 | if (options & WILD_KEEP_ALL) |
| 4771 | flags |= EW_KEEPALL; |
| 4772 | if (options & WILD_SILENT) |
| 4773 | flags |= EW_SILENT; |
Bram Moolenaar | a245bc7 | 2015-03-05 19:35:25 +0100 | [diff] [blame] | 4774 | if (options & WILD_ALLLINKS) |
| 4775 | flags |= EW_ALLLINKS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4776 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4777 | if (xp->xp_context == EXPAND_FILES |
| 4778 | || xp->xp_context == EXPAND_DIRECTORIES |
| 4779 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4780 | { |
| 4781 | /* |
| 4782 | * Expand file or directory names. |
| 4783 | */ |
| 4784 | int free_pat = FALSE; |
| 4785 | int i; |
| 4786 | |
| 4787 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 4788 | * space */ |
| 4789 | if (xp->xp_backslash != XP_BS_NONE) |
| 4790 | { |
| 4791 | free_pat = TRUE; |
| 4792 | pat = vim_strsave(pat); |
| 4793 | for (i = 0; pat[i]; ++i) |
| 4794 | if (pat[i] == '\\') |
| 4795 | { |
| 4796 | if (xp->xp_backslash == XP_BS_THREE |
| 4797 | && pat[i + 1] == '\\' |
| 4798 | && pat[i + 2] == '\\' |
| 4799 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4800 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4801 | if (xp->xp_backslash == XP_BS_ONE |
| 4802 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4803 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4804 | } |
| 4805 | } |
| 4806 | |
| 4807 | if (xp->xp_context == EXPAND_FILES) |
| 4808 | flags |= EW_FILE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4809 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 4810 | flags |= (EW_FILE | EW_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4811 | else |
| 4812 | flags = (flags | EW_DIR) & ~EW_FILE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4813 | if (options & WILD_ICASE) |
| 4814 | flags |= EW_ICASE; |
| 4815 | |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 4816 | /* Expand wildcards, supporting %:h and the like. */ |
| 4817 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4818 | if (free_pat) |
| 4819 | vim_free(pat); |
| 4820 | return ret; |
| 4821 | } |
| 4822 | |
| 4823 | *file = (char_u **)""; |
| 4824 | *num_file = 0; |
| 4825 | if (xp->xp_context == EXPAND_HELP) |
| 4826 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 4827 | /* With an empty argument we would get all the help tags, which is |
| 4828 | * very slow. Get matches for "help" instead. */ |
| 4829 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 4830 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4831 | { |
| 4832 | #ifdef FEAT_MULTI_LANG |
| 4833 | cleanup_help_tags(*num_file, *file); |
| 4834 | #endif |
| 4835 | return OK; |
| 4836 | } |
| 4837 | return FAIL; |
| 4838 | } |
| 4839 | |
| 4840 | #ifndef FEAT_CMDL_COMPL |
| 4841 | return FAIL; |
| 4842 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4843 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 4844 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4845 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 4846 | return ExpandOldSetting(num_file, file); |
| 4847 | if (xp->xp_context == EXPAND_BUFFERS) |
| 4848 | return ExpandBufnames(pat, num_file, file, options); |
| 4849 | if (xp->xp_context == EXPAND_TAGS |
| 4850 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4851 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 4852 | if (xp->xp_context == EXPAND_COLORS) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4853 | { |
| 4854 | char *directories[] = {"colors", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4855 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 4856 | directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4857 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4858 | if (xp->xp_context == EXPAND_COMPILER) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4859 | { |
Bram Moolenaar | a627c96 | 2011-09-30 16:23:32 +0200 | [diff] [blame] | 4860 | char *directories[] = {"compiler", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4861 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4862 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4863 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4864 | { |
| 4865 | char *directories[] = {"syntax", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4866 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4867 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4868 | if (xp->xp_context == EXPAND_FILETYPE) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4869 | { |
| 4870 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4871 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4872 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4873 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4874 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4875 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4876 | # endif |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4877 | if (xp->xp_context == EXPAND_PACKADD) |
| 4878 | return ExpandPackAddDir(pat, num_file, file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4879 | |
| 4880 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 4881 | if (regmatch.regprog == NULL) |
| 4882 | return FAIL; |
| 4883 | |
| 4884 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 4885 | regmatch.rm_ic = ignorecase(pat); |
| 4886 | |
| 4887 | if (xp->xp_context == EXPAND_SETTINGS |
| 4888 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 4889 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 4890 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 4891 | ret = ExpandMappings(®match, num_file, file); |
| 4892 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4893 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 4894 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 4895 | # endif |
| 4896 | else |
| 4897 | { |
| 4898 | static struct expgen |
| 4899 | { |
| 4900 | int context; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 4901 | char_u *((*func)(expand_T *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4902 | int ic; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4903 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4904 | } tab[] = |
| 4905 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4906 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 4907 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
Bram Moolenaar | cae92dc | 2017-08-06 15:22:15 +0200 | [diff] [blame] | 4908 | {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE}, |
Bram Moolenaar | 9e507ca | 2016-10-15 15:39:39 +0200 | [diff] [blame] | 4909 | {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE}, |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 4910 | #ifdef FEAT_CMDHIST |
| 4911 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 4912 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4913 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4914 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 4915 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4916 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 4917 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 4918 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4919 | #endif |
| 4920 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4921 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 4922 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 4923 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 4924 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4925 | #endif |
| 4926 | #ifdef FEAT_MENU |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4927 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 4928 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4929 | #endif |
| 4930 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4931 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4932 | #endif |
Bram Moolenaar | cd9c462 | 2013-06-08 15:24:48 +0200 | [diff] [blame] | 4933 | #ifdef FEAT_PROFILE |
| 4934 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 4935 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4936 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4937 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4938 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 4939 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4940 | #endif |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4941 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4942 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4943 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4944 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4945 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4946 | #endif |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 4947 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4948 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 4949 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4950 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 4951 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4952 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 4953 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4954 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4955 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4956 | {EXPAND_USER, get_users, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4957 | }; |
| 4958 | int i; |
| 4959 | |
| 4960 | /* |
| 4961 | * Find a context in the table and call the ExpandGeneric() with the |
| 4962 | * right function to do the expansion. |
| 4963 | */ |
| 4964 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 4965 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4966 | if (xp->xp_context == tab[i].context) |
| 4967 | { |
| 4968 | if (tab[i].ic) |
| 4969 | regmatch.rm_ic = TRUE; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4970 | ret = ExpandGeneric(xp, ®match, num_file, file, |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 4971 | tab[i].func, tab[i].escaped); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4972 | break; |
| 4973 | } |
| 4974 | } |
| 4975 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4976 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4977 | |
| 4978 | return ret; |
| 4979 | #endif /* FEAT_CMDL_COMPL */ |
| 4980 | } |
| 4981 | |
| 4982 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4983 | /* |
| 4984 | * Expand a list of names. |
| 4985 | * |
| 4986 | * Generic function for command line completion. It calls a function to |
| 4987 | * obtain strings, one by one. The strings are matched against a regexp |
| 4988 | * program. Matching strings are copied into an array, which is returned. |
| 4989 | * |
| 4990 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 4991 | */ |
| 4992 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4993 | ExpandGeneric( |
| 4994 | expand_T *xp, |
| 4995 | regmatch_T *regmatch, |
| 4996 | int *num_file, |
| 4997 | char_u ***file, |
| 4998 | char_u *((*func)(expand_T *, int)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4999 | /* returns a string from the list */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5000 | int escaped) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5001 | { |
| 5002 | int i; |
| 5003 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5004 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5005 | char_u *str; |
| 5006 | |
| 5007 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5008 | * round == 0: count the number of matching names |
| 5009 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5010 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5011 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5012 | { |
| 5013 | for (i = 0; ; ++i) |
| 5014 | { |
| 5015 | str = (*func)(xp, i); |
| 5016 | if (str == NULL) /* end of list */ |
| 5017 | break; |
| 5018 | if (*str == NUL) /* skip empty strings */ |
| 5019 | continue; |
| 5020 | |
| 5021 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 5022 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5023 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5024 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5025 | if (escaped) |
| 5026 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 5027 | else |
| 5028 | str = vim_strsave(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5029 | (*file)[count] = str; |
| 5030 | #ifdef FEAT_MENU |
| 5031 | if (func == get_menu_names && str != NULL) |
| 5032 | { |
| 5033 | /* test for separator added by get_menu_names() */ |
| 5034 | str += STRLEN(str) - 1; |
| 5035 | if (*str == '\001') |
| 5036 | *str = '.'; |
| 5037 | } |
| 5038 | #endif |
| 5039 | } |
| 5040 | ++count; |
| 5041 | } |
| 5042 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5043 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5044 | { |
| 5045 | if (count == 0) |
| 5046 | return OK; |
| 5047 | *num_file = count; |
| 5048 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 5049 | if (*file == NULL) |
| 5050 | { |
| 5051 | *file = (char_u **)""; |
| 5052 | return FAIL; |
| 5053 | } |
| 5054 | count = 0; |
| 5055 | } |
| 5056 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5057 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 5058 | /* Sort the results. Keep menu's in the specified order. */ |
| 5059 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 5060 | { |
| 5061 | if (xp->xp_context == EXPAND_EXPRESSION |
| 5062 | || xp->xp_context == EXPAND_FUNCTIONS |
| 5063 | || xp->xp_context == EXPAND_USER_FUNC) |
| 5064 | /* <SNR> functions should be sorted to the end. */ |
| 5065 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 5066 | sort_func_compare); |
| 5067 | else |
| 5068 | sort_strings(*file, *num_file); |
| 5069 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5070 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 5071 | #ifdef FEAT_CMDL_COMPL |
| 5072 | /* Reset the variables used for special highlight names expansion, so that |
| 5073 | * they don't show up when getting normal highlight names by ID. */ |
| 5074 | reset_expand_highlight(); |
| 5075 | #endif |
| 5076 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5077 | return OK; |
| 5078 | } |
| 5079 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5080 | /* |
| 5081 | * Complete a shell command. |
| 5082 | * Returns FAIL or OK; |
| 5083 | */ |
| 5084 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5085 | expand_shellcmd( |
| 5086 | char_u *filepat, /* pattern to match with command names */ |
| 5087 | int *num_file, /* return: number of matches */ |
| 5088 | char_u ***file, /* return: array with matches */ |
| 5089 | int flagsarg) /* EW_ flags */ |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5090 | { |
| 5091 | char_u *pat; |
| 5092 | int i; |
| 5093 | char_u *path; |
| 5094 | int mustfree = FALSE; |
| 5095 | garray_T ga; |
| 5096 | char_u *buf = alloc(MAXPATHL); |
| 5097 | size_t l; |
| 5098 | char_u *s, *e; |
| 5099 | int flags = flagsarg; |
| 5100 | int ret; |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5101 | int did_curdir = FALSE; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5102 | |
| 5103 | if (buf == NULL) |
| 5104 | return FAIL; |
| 5105 | |
| 5106 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5107 | * space */ |
| 5108 | pat = vim_strsave(filepat); |
| 5109 | for (i = 0; pat[i]; ++i) |
| 5110 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5111 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5112 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5113 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5114 | |
| 5115 | /* For an absolute name we don't use $PATH. */ |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 5116 | if (mch_isFullName(pat)) |
| 5117 | path = (char_u *)" "; |
| 5118 | else if ((pat[0] == '.' && (vim_ispathsep(pat[1]) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5119 | || (pat[1] == '.' && vim_ispathsep(pat[2]))))) |
| 5120 | path = (char_u *)"."; |
| 5121 | else |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5122 | { |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5123 | path = vim_getenv((char_u *)"PATH", &mustfree); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5124 | if (path == NULL) |
| 5125 | path = (char_u *)""; |
| 5126 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5127 | |
| 5128 | /* |
| 5129 | * Go over all directories in $PATH. Expand matches in that directory and |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5130 | * collect them in "ga". When "." is not in $PATH also expand for the |
| 5131 | * current directory, to find "subdir/cmd". |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5132 | */ |
| 5133 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5134 | for (s = path; ; s = e) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5135 | { |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5136 | if (*s == NUL) |
| 5137 | { |
| 5138 | if (did_curdir) |
| 5139 | break; |
| 5140 | /* Find directories in the current directory, path is empty. */ |
| 5141 | did_curdir = TRUE; |
| 5142 | } |
| 5143 | else if (*s == '.') |
| 5144 | did_curdir = TRUE; |
| 5145 | |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 5146 | if (*s == ' ') |
| 5147 | ++s; /* Skip space used for absolute path name. */ |
| 5148 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5149 | #if defined(MSWIN) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5150 | e = vim_strchr(s, ';'); |
| 5151 | #else |
| 5152 | e = vim_strchr(s, ':'); |
| 5153 | #endif |
| 5154 | if (e == NULL) |
| 5155 | e = s + STRLEN(s); |
| 5156 | |
| 5157 | l = e - s; |
| 5158 | if (l > MAXPATHL - 5) |
| 5159 | break; |
| 5160 | vim_strncpy(buf, s, l); |
| 5161 | add_pathsep(buf); |
| 5162 | l = STRLEN(buf); |
| 5163 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 5164 | |
| 5165 | /* Expand matches in one directory of $PATH. */ |
| 5166 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 5167 | if (ret == OK) |
| 5168 | { |
| 5169 | if (ga_grow(&ga, *num_file) == FAIL) |
| 5170 | FreeWild(*num_file, *file); |
| 5171 | else |
| 5172 | { |
| 5173 | for (i = 0; i < *num_file; ++i) |
| 5174 | { |
| 5175 | s = (*file)[i]; |
| 5176 | if (STRLEN(s) > l) |
| 5177 | { |
| 5178 | /* Remove the path again. */ |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5179 | STRMOVE(s, s + l); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5180 | ((char_u **)ga.ga_data)[ga.ga_len++] = s; |
| 5181 | } |
| 5182 | else |
| 5183 | vim_free(s); |
| 5184 | } |
| 5185 | vim_free(*file); |
| 5186 | } |
| 5187 | } |
| 5188 | if (*e != NUL) |
| 5189 | ++e; |
| 5190 | } |
| 5191 | *file = ga.ga_data; |
| 5192 | *num_file = ga.ga_len; |
| 5193 | |
| 5194 | vim_free(buf); |
| 5195 | vim_free(pat); |
| 5196 | if (mustfree) |
| 5197 | vim_free(path); |
| 5198 | return OK; |
| 5199 | } |
| 5200 | |
| 5201 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5202 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 5203 | static void * call_user_expand_func(void *(*user_expand_func)(char_u *, int, char_u **, int), expand_T *xp, int *num_file, char_u ***file); |
Bram Moolenaar | 3b56eb3 | 2005-07-11 22:40:32 +0000 | [diff] [blame] | 5204 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5205 | /* |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 5206 | * Call "user_expand_func()" to invoke a user defined Vim script function and |
| 5207 | * return the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5208 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5209 | static void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5210 | call_user_expand_func( |
| 5211 | void *(*user_expand_func)(char_u *, int, char_u **, int), |
| 5212 | expand_T *xp, |
| 5213 | int *num_file, |
| 5214 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5215 | { |
Bram Moolenaar | 673b9a3 | 2013-06-30 22:43:27 +0200 | [diff] [blame] | 5216 | int keep = 0; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5217 | char_u num[50]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5218 | char_u *args[3]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5219 | int save_current_SID = current_SID; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5220 | void *ret; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5221 | struct cmdline_info save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5222 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5223 | if (xp->xp_arg == NULL || xp->xp_arg[0] == '\0' || xp->xp_line == NULL) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5224 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5225 | *num_file = 0; |
| 5226 | *file = NULL; |
| 5227 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5228 | if (ccline.cmdbuff != NULL) |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5229 | { |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5230 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 5231 | ccline.cmdbuff[ccline.cmdlen] = 0; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5232 | } |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5233 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5234 | args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5235 | args[1] = xp->xp_line; |
| 5236 | sprintf((char *)num, "%d", xp->xp_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5237 | args[2] = num; |
| 5238 | |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5239 | /* Save the cmdline, we don't know what the function may do. */ |
| 5240 | save_ccline = ccline; |
| 5241 | ccline.cmdbuff = NULL; |
| 5242 | ccline.cmdprompt = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5243 | current_SID = xp->xp_scriptID; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5244 | |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5245 | ret = user_expand_func(xp->xp_arg, 3, args, FALSE); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5246 | |
| 5247 | ccline = save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5248 | current_SID = save_current_SID; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5249 | if (ccline.cmdbuff != NULL) |
| 5250 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5251 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5252 | vim_free(args[0]); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5253 | return ret; |
| 5254 | } |
| 5255 | |
| 5256 | /* |
| 5257 | * Expand names with a function defined by the user. |
| 5258 | */ |
| 5259 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5260 | ExpandUserDefined( |
| 5261 | expand_T *xp, |
| 5262 | regmatch_T *regmatch, |
| 5263 | int *num_file, |
| 5264 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5265 | { |
| 5266 | char_u *retstr; |
| 5267 | char_u *s; |
| 5268 | char_u *e; |
| 5269 | char_u keep; |
| 5270 | garray_T ga; |
| 5271 | |
| 5272 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 5273 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5274 | return FAIL; |
| 5275 | |
| 5276 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5277 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5278 | { |
| 5279 | e = vim_strchr(s, '\n'); |
| 5280 | if (e == NULL) |
| 5281 | e = s + STRLEN(s); |
| 5282 | keep = *e; |
| 5283 | *e = 0; |
| 5284 | |
| 5285 | if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0) |
| 5286 | { |
| 5287 | *e = keep; |
| 5288 | if (*e != NUL) |
| 5289 | ++e; |
| 5290 | continue; |
| 5291 | } |
| 5292 | |
| 5293 | if (ga_grow(&ga, 1) == FAIL) |
| 5294 | break; |
| 5295 | |
| 5296 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 5297 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5298 | |
| 5299 | *e = keep; |
| 5300 | if (*e != NUL) |
| 5301 | ++e; |
| 5302 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5303 | vim_free(retstr); |
| 5304 | *file = ga.ga_data; |
| 5305 | *num_file = ga.ga_len; |
| 5306 | return OK; |
| 5307 | } |
| 5308 | |
| 5309 | /* |
| 5310 | * Expand names with a list returned by a function defined by the user. |
| 5311 | */ |
| 5312 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5313 | ExpandUserList( |
| 5314 | expand_T *xp, |
| 5315 | int *num_file, |
| 5316 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5317 | { |
| 5318 | list_T *retlist; |
| 5319 | listitem_T *li; |
| 5320 | garray_T ga; |
| 5321 | |
| 5322 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 5323 | if (retlist == NULL) |
| 5324 | return FAIL; |
| 5325 | |
| 5326 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 5327 | /* Loop over the items in the list. */ |
| 5328 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 5329 | { |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5330 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 5331 | continue; /* Skip non-string items and empty strings */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5332 | |
| 5333 | if (ga_grow(&ga, 1) == FAIL) |
| 5334 | break; |
| 5335 | |
| 5336 | ((char_u **)ga.ga_data)[ga.ga_len] = |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5337 | vim_strsave(li->li_tv.vval.v_string); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5338 | ++ga.ga_len; |
| 5339 | } |
| 5340 | list_unref(retlist); |
| 5341 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5342 | *file = ga.ga_data; |
| 5343 | *num_file = ga.ga_len; |
| 5344 | return OK; |
| 5345 | } |
| 5346 | #endif |
| 5347 | |
| 5348 | /* |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5349 | * Expand color scheme, compiler or filetype names. |
| 5350 | * Search from 'runtimepath': |
| 5351 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 5352 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 5353 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 5354 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 5355 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5356 | * "dirnames" is an array with one or more directory names. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5357 | */ |
| 5358 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5359 | ExpandRTDir( |
| 5360 | char_u *pat, |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5361 | int flags, |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5362 | int *num_file, |
| 5363 | char_u ***file, |
| 5364 | char *dirnames[]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5365 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5366 | char_u *s; |
| 5367 | char_u *e; |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5368 | char_u *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5369 | garray_T ga; |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5370 | int i; |
| 5371 | int pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5372 | |
| 5373 | *num_file = 0; |
| 5374 | *file = NULL; |
Bram Moolenaar | 5cfe2d7 | 2011-07-07 15:04:52 +0200 | [diff] [blame] | 5375 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5376 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5377 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5378 | for (i = 0; dirnames[i] != NULL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5379 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5380 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
| 5381 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5382 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5383 | ga_clear_strings(&ga); |
| 5384 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5385 | } |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5386 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5387 | globpath(p_rtp, s, &ga, 0); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5388 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5389 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5390 | |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5391 | if (flags & DIP_START) { |
| 5392 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5393 | { |
| 5394 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22)); |
| 5395 | if (s == NULL) |
| 5396 | { |
| 5397 | ga_clear_strings(&ga); |
| 5398 | return FAIL; |
| 5399 | } |
| 5400 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 5401 | globpath(p_pp, s, &ga, 0); |
| 5402 | vim_free(s); |
| 5403 | } |
| 5404 | } |
| 5405 | |
| 5406 | if (flags & DIP_OPT) { |
| 5407 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5408 | { |
| 5409 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20)); |
| 5410 | if (s == NULL) |
| 5411 | { |
| 5412 | ga_clear_strings(&ga); |
| 5413 | return FAIL; |
| 5414 | } |
| 5415 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 5416 | globpath(p_pp, s, &ga, 0); |
| 5417 | vim_free(s); |
| 5418 | } |
| 5419 | } |
| 5420 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5421 | for (i = 0; i < ga.ga_len; ++i) |
| 5422 | { |
| 5423 | match = ((char_u **)ga.ga_data)[i]; |
| 5424 | s = match; |
| 5425 | e = s + STRLEN(s); |
| 5426 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 5427 | { |
| 5428 | e -= 4; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5429 | for (s = e; s > match; MB_PTR_BACK(match, s)) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5430 | if (s < match || vim_ispathsep(*s)) |
| 5431 | break; |
| 5432 | ++s; |
| 5433 | *e = NUL; |
| 5434 | mch_memmove(match, s, e - s + 1); |
| 5435 | } |
| 5436 | } |
| 5437 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5438 | if (ga.ga_len == 0) |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5439 | return FAIL; |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5440 | |
| 5441 | /* Sort and remove duplicates which can happen when specifying multiple |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5442 | * directories in dirnames. */ |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5443 | remove_duplicates(&ga); |
| 5444 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5445 | *file = ga.ga_data; |
| 5446 | *num_file = ga.ga_len; |
| 5447 | return OK; |
| 5448 | } |
| 5449 | |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5450 | /* |
| 5451 | * Expand loadplugin names: |
| 5452 | * 'packpath'/pack/ * /opt/{pat} |
| 5453 | */ |
| 5454 | static int |
| 5455 | ExpandPackAddDir( |
| 5456 | char_u *pat, |
| 5457 | int *num_file, |
| 5458 | char_u ***file) |
| 5459 | { |
| 5460 | char_u *s; |
| 5461 | char_u *e; |
| 5462 | char_u *match; |
| 5463 | garray_T ga; |
| 5464 | int i; |
| 5465 | int pat_len; |
| 5466 | |
| 5467 | *num_file = 0; |
| 5468 | *file = NULL; |
| 5469 | pat_len = (int)STRLEN(pat); |
| 5470 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 5471 | |
| 5472 | s = alloc((unsigned)(pat_len + 26)); |
| 5473 | if (s == NULL) |
| 5474 | { |
| 5475 | ga_clear_strings(&ga); |
| 5476 | return FAIL; |
| 5477 | } |
| 5478 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 5479 | globpath(p_pp, s, &ga, 0); |
| 5480 | vim_free(s); |
| 5481 | |
| 5482 | for (i = 0; i < ga.ga_len; ++i) |
| 5483 | { |
| 5484 | match = ((char_u **)ga.ga_data)[i]; |
| 5485 | s = gettail(match); |
| 5486 | e = s + STRLEN(s); |
| 5487 | mch_memmove(match, s, e - s + 1); |
| 5488 | } |
| 5489 | |
| 5490 | if (ga.ga_len == 0) |
| 5491 | return FAIL; |
| 5492 | |
| 5493 | /* Sort and remove duplicates which can happen when specifying multiple |
| 5494 | * directories in dirnames. */ |
| 5495 | remove_duplicates(&ga); |
| 5496 | |
| 5497 | *file = ga.ga_data; |
| 5498 | *num_file = ga.ga_len; |
| 5499 | return OK; |
| 5500 | } |
| 5501 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5502 | #endif |
| 5503 | |
| 5504 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 5505 | /* |
| 5506 | * Expand "file" for all comma-separated directories in "path". |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5507 | * Adds the matches to "ga". Caller must init "ga". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5508 | */ |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5509 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5510 | globpath( |
| 5511 | char_u *path, |
| 5512 | char_u *file, |
| 5513 | garray_T *ga, |
| 5514 | int expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5515 | { |
| 5516 | expand_T xpc; |
| 5517 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5518 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5519 | int num_p; |
| 5520 | char_u **p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5521 | |
| 5522 | buf = alloc(MAXPATHL); |
| 5523 | if (buf == NULL) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5524 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5525 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5526 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5527 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5528 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5529 | /* Loop over all entries in {path}. */ |
| 5530 | while (*path != NUL) |
| 5531 | { |
| 5532 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 5533 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 5534 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 5535 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5536 | # if defined(MSWIN) |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5537 | /* Using the platform's path separator (\) makes vim incorrectly |
| 5538 | * treat it as an escape character, use '/' instead. */ |
| 5539 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 5540 | STRCAT(buf, "/"); |
| 5541 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5542 | add_pathsep(buf); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5543 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5544 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5545 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 5546 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5547 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5548 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5549 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5550 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5551 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5552 | for (i = 0; i < num_p; ++i) |
| 5553 | { |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5554 | ((char_u **)ga->ga_data)[ga->ga_len] = |
Bram Moolenaar | 7116aa0 | 2014-05-29 14:36:29 +0200 | [diff] [blame] | 5555 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5556 | ++ga->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5557 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5558 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5559 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5560 | FreeWild(num_p, p); |
| 5561 | } |
| 5562 | } |
| 5563 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5564 | |
| 5565 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5566 | } |
| 5567 | |
| 5568 | #endif |
| 5569 | |
| 5570 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5571 | |
| 5572 | /********************************* |
| 5573 | * Command line history stuff * |
| 5574 | *********************************/ |
| 5575 | |
| 5576 | /* |
| 5577 | * Translate a history character to the associated type number. |
| 5578 | */ |
| 5579 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5580 | hist_char2type(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5581 | { |
| 5582 | if (c == ':') |
| 5583 | return HIST_CMD; |
| 5584 | if (c == '=') |
| 5585 | return HIST_EXPR; |
| 5586 | if (c == '@') |
| 5587 | return HIST_INPUT; |
| 5588 | if (c == '>') |
| 5589 | return HIST_DEBUG; |
| 5590 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5591 | } |
| 5592 | |
| 5593 | /* |
| 5594 | * Table of history names. |
| 5595 | * These names are used in :history and various hist...() functions. |
| 5596 | * It is sufficient to give the significant prefix of a history name. |
| 5597 | */ |
| 5598 | |
| 5599 | static char *(history_names[]) = |
| 5600 | { |
| 5601 | "cmd", |
| 5602 | "search", |
| 5603 | "expr", |
| 5604 | "input", |
| 5605 | "debug", |
| 5606 | NULL |
| 5607 | }; |
| 5608 | |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5609 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5610 | /* |
| 5611 | * Function given to ExpandGeneric() to obtain the possible first |
| 5612 | * arguments of the ":history command. |
| 5613 | */ |
| 5614 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5615 | get_history_arg(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5616 | { |
| 5617 | static char_u compl[2] = { NUL, NUL }; |
| 5618 | char *short_names = ":=@>?/"; |
Bram Moolenaar | 17bd9dc | 2012-05-25 11:02:41 +0200 | [diff] [blame] | 5619 | int short_names_count = (int)STRLEN(short_names); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5620 | int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
| 5621 | |
| 5622 | if (idx < short_names_count) |
| 5623 | { |
| 5624 | compl[0] = (char_u)short_names[idx]; |
| 5625 | return compl; |
| 5626 | } |
| 5627 | if (idx < short_names_count + history_name_count) |
| 5628 | return (char_u *)history_names[idx - short_names_count]; |
| 5629 | if (idx == short_names_count + history_name_count) |
| 5630 | return (char_u *)"all"; |
| 5631 | return NULL; |
| 5632 | } |
| 5633 | #endif |
| 5634 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5635 | /* |
| 5636 | * init_history() - Initialize the command line history. |
| 5637 | * Also used to re-allocate the history when the size changes. |
| 5638 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 5639 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5640 | init_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5641 | { |
| 5642 | int newlen; /* new length of history table */ |
| 5643 | histentry_T *temp; |
| 5644 | int i; |
| 5645 | int j; |
| 5646 | int type; |
| 5647 | |
| 5648 | /* |
| 5649 | * If size of history table changed, reallocate it |
| 5650 | */ |
| 5651 | newlen = (int)p_hi; |
| 5652 | if (newlen != hislen) /* history length changed */ |
| 5653 | { |
| 5654 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 5655 | { |
| 5656 | if (newlen) |
| 5657 | { |
| 5658 | temp = (histentry_T *)lalloc( |
| 5659 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 5660 | if (temp == NULL) /* out of memory! */ |
| 5661 | { |
| 5662 | if (type == 0) /* first one: just keep the old length */ |
| 5663 | { |
| 5664 | newlen = hislen; |
| 5665 | break; |
| 5666 | } |
| 5667 | /* Already changed one table, now we can only have zero |
| 5668 | * length for all tables. */ |
| 5669 | newlen = 0; |
| 5670 | type = -1; |
| 5671 | continue; |
| 5672 | } |
| 5673 | } |
| 5674 | else |
| 5675 | temp = NULL; |
| 5676 | if (newlen == 0 || temp != NULL) |
| 5677 | { |
| 5678 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 5679 | { |
| 5680 | for (i = 0; i < newlen; ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5681 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5682 | } |
| 5683 | else if (newlen > hislen) /* array becomes bigger */ |
| 5684 | { |
| 5685 | for (i = 0; i <= hisidx[type]; ++i) |
| 5686 | temp[i] = history[type][i]; |
| 5687 | j = i; |
| 5688 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5689 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5690 | for ( ; j < hislen; ++i, ++j) |
| 5691 | temp[i] = history[type][j]; |
| 5692 | } |
| 5693 | else /* array becomes smaller or 0 */ |
| 5694 | { |
| 5695 | j = hisidx[type]; |
| 5696 | for (i = newlen - 1; ; --i) |
| 5697 | { |
| 5698 | if (i >= 0) /* copy newest entries */ |
| 5699 | temp[i] = history[type][j]; |
| 5700 | else /* remove older entries */ |
| 5701 | vim_free(history[type][j].hisstr); |
| 5702 | if (--j < 0) |
| 5703 | j = hislen - 1; |
| 5704 | if (j == hisidx[type]) |
| 5705 | break; |
| 5706 | } |
| 5707 | hisidx[type] = newlen - 1; |
| 5708 | } |
| 5709 | vim_free(history[type]); |
| 5710 | history[type] = temp; |
| 5711 | } |
| 5712 | } |
| 5713 | hislen = newlen; |
| 5714 | } |
| 5715 | } |
| 5716 | |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5717 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5718 | clear_hist_entry(histentry_T *hisptr) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5719 | { |
| 5720 | hisptr->hisnum = 0; |
| 5721 | hisptr->viminfo = FALSE; |
| 5722 | hisptr->hisstr = NULL; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 5723 | hisptr->time_set = 0; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5724 | } |
| 5725 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5726 | /* |
| 5727 | * Check if command line 'str' is already in history. |
| 5728 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 5729 | */ |
| 5730 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5731 | in_history( |
| 5732 | int type, |
| 5733 | char_u *str, |
| 5734 | int move_to_front, /* Move the entry to the front if it exists */ |
| 5735 | int sep, |
| 5736 | int writing) /* ignore entries read from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5737 | { |
| 5738 | int i; |
| 5739 | int last_i = -1; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5740 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5741 | |
| 5742 | if (hisidx[type] < 0) |
| 5743 | return FALSE; |
| 5744 | i = hisidx[type]; |
| 5745 | do |
| 5746 | { |
| 5747 | if (history[type][i].hisstr == NULL) |
| 5748 | return FALSE; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5749 | |
| 5750 | /* For search history, check that the separator character matches as |
| 5751 | * well. */ |
| 5752 | p = history[type][i].hisstr; |
| 5753 | if (STRCMP(str, p) == 0 |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5754 | && !(writing && history[type][i].viminfo) |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5755 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5756 | { |
| 5757 | if (!move_to_front) |
| 5758 | return TRUE; |
| 5759 | last_i = i; |
| 5760 | break; |
| 5761 | } |
| 5762 | if (--i < 0) |
| 5763 | i = hislen - 1; |
| 5764 | } while (i != hisidx[type]); |
| 5765 | |
| 5766 | if (last_i >= 0) |
| 5767 | { |
| 5768 | str = history[type][i].hisstr; |
| 5769 | while (i != hisidx[type]) |
| 5770 | { |
| 5771 | if (++i >= hislen) |
| 5772 | i = 0; |
| 5773 | history[type][last_i] = history[type][i]; |
| 5774 | last_i = i; |
| 5775 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5776 | history[type][i].hisnum = ++hisnum[type]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5777 | history[type][i].viminfo = FALSE; |
| 5778 | history[type][i].hisstr = str; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5779 | history[type][i].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5780 | return TRUE; |
| 5781 | } |
| 5782 | return FALSE; |
| 5783 | } |
| 5784 | |
| 5785 | /* |
| 5786 | * Convert history name (from table above) to its HIST_ equivalent. |
| 5787 | * When "name" is empty, return "cmd" history. |
| 5788 | * Returns -1 for unknown history name. |
| 5789 | */ |
| 5790 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5791 | get_histtype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5792 | { |
| 5793 | int i; |
| 5794 | int len = (int)STRLEN(name); |
| 5795 | |
| 5796 | /* No argument: use current history. */ |
| 5797 | if (len == 0) |
| 5798 | return hist_char2type(ccline.cmdfirstc); |
| 5799 | |
| 5800 | for (i = 0; history_names[i] != NULL; ++i) |
| 5801 | if (STRNICMP(name, history_names[i], len) == 0) |
| 5802 | return i; |
| 5803 | |
| 5804 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 5805 | return hist_char2type(name[0]); |
| 5806 | |
| 5807 | return -1; |
| 5808 | } |
| 5809 | |
| 5810 | static int last_maptick = -1; /* last seen maptick */ |
| 5811 | |
| 5812 | /* |
| 5813 | * Add the given string to the given history. If the string is already in the |
| 5814 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 5815 | * values. |
| 5816 | */ |
| 5817 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5818 | add_to_history( |
| 5819 | int histype, |
| 5820 | char_u *new_entry, |
| 5821 | int in_map, /* consider maptick when inside a mapping */ |
| 5822 | int sep) /* separator character used (search hist) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5823 | { |
| 5824 | histentry_T *hisptr; |
| 5825 | int len; |
| 5826 | |
| 5827 | if (hislen == 0) /* no history */ |
| 5828 | return; |
| 5829 | |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 5830 | if (cmdmod.keeppatterns && histype == HIST_SEARCH) |
| 5831 | return; |
| 5832 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5833 | /* |
| 5834 | * Searches inside the same mapping overwrite each other, so that only |
| 5835 | * the last line is kept. Be careful not to remove a line that was moved |
| 5836 | * down, only lines that were added. |
| 5837 | */ |
| 5838 | if (histype == HIST_SEARCH && in_map) |
| 5839 | { |
Bram Moolenaar | 4664371 | 2016-09-09 21:42:36 +0200 | [diff] [blame] | 5840 | if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5841 | { |
| 5842 | /* Current line is from the same mapping, remove it */ |
| 5843 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 5844 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5845 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5846 | --hisnum[histype]; |
| 5847 | if (--hisidx[HIST_SEARCH] < 0) |
| 5848 | hisidx[HIST_SEARCH] = hislen - 1; |
| 5849 | } |
| 5850 | last_maptick = -1; |
| 5851 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5852 | if (!in_history(histype, new_entry, TRUE, sep, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5853 | { |
| 5854 | if (++hisidx[histype] == hislen) |
| 5855 | hisidx[histype] = 0; |
| 5856 | hisptr = &history[histype][hisidx[histype]]; |
| 5857 | vim_free(hisptr->hisstr); |
| 5858 | |
| 5859 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5860 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5861 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 5862 | if (hisptr->hisstr != NULL) |
| 5863 | hisptr->hisstr[len + 1] = sep; |
| 5864 | |
| 5865 | hisptr->hisnum = ++hisnum[histype]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5866 | hisptr->viminfo = FALSE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5867 | hisptr->time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5868 | if (histype == HIST_SEARCH && in_map) |
| 5869 | last_maptick = maptick; |
| 5870 | } |
| 5871 | } |
| 5872 | |
| 5873 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 5874 | |
| 5875 | /* |
| 5876 | * Get identifier of newest history entry. |
| 5877 | * "histype" may be one of the HIST_ values. |
| 5878 | */ |
| 5879 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5880 | get_history_idx(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5881 | { |
| 5882 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5883 | || hisidx[histype] < 0) |
| 5884 | return -1; |
| 5885 | |
| 5886 | return history[histype][hisidx[histype]].hisnum; |
| 5887 | } |
| 5888 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5889 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5890 | * Calculate history index from a number: |
| 5891 | * num > 0: seen as identifying number of a history entry |
| 5892 | * num < 0: relative position in history wrt newest entry |
| 5893 | * "histype" may be one of the HIST_ values. |
| 5894 | */ |
| 5895 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5896 | calc_hist_idx(int histype, int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5897 | { |
| 5898 | int i; |
| 5899 | histentry_T *hist; |
| 5900 | int wrapped = FALSE; |
| 5901 | |
| 5902 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5903 | || (i = hisidx[histype]) < 0 || num == 0) |
| 5904 | return -1; |
| 5905 | |
| 5906 | hist = history[histype]; |
| 5907 | if (num > 0) |
| 5908 | { |
| 5909 | while (hist[i].hisnum > num) |
| 5910 | if (--i < 0) |
| 5911 | { |
| 5912 | if (wrapped) |
| 5913 | break; |
| 5914 | i += hislen; |
| 5915 | wrapped = TRUE; |
| 5916 | } |
| 5917 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 5918 | return i; |
| 5919 | } |
| 5920 | else if (-num <= hislen) |
| 5921 | { |
| 5922 | i += num + 1; |
| 5923 | if (i < 0) |
| 5924 | i += hislen; |
| 5925 | if (hist[i].hisstr != NULL) |
| 5926 | return i; |
| 5927 | } |
| 5928 | return -1; |
| 5929 | } |
| 5930 | |
| 5931 | /* |
| 5932 | * Get a history entry by its index. |
| 5933 | * "histype" may be one of the HIST_ values. |
| 5934 | */ |
| 5935 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5936 | get_history_entry(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5937 | { |
| 5938 | idx = calc_hist_idx(histype, idx); |
| 5939 | if (idx >= 0) |
| 5940 | return history[histype][idx].hisstr; |
| 5941 | else |
| 5942 | return (char_u *)""; |
| 5943 | } |
| 5944 | |
| 5945 | /* |
| 5946 | * Clear all entries of a history. |
| 5947 | * "histype" may be one of the HIST_ values. |
| 5948 | */ |
| 5949 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5950 | clr_history(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5951 | { |
| 5952 | int i; |
| 5953 | histentry_T *hisptr; |
| 5954 | |
| 5955 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 5956 | { |
| 5957 | hisptr = history[histype]; |
| 5958 | for (i = hislen; i--;) |
| 5959 | { |
| 5960 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5961 | clear_hist_entry(hisptr); |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 5962 | hisptr++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5963 | } |
| 5964 | hisidx[histype] = -1; /* mark history as cleared */ |
| 5965 | hisnum[histype] = 0; /* reset identifier counter */ |
| 5966 | return OK; |
| 5967 | } |
| 5968 | return FAIL; |
| 5969 | } |
| 5970 | |
| 5971 | /* |
| 5972 | * Remove all entries matching {str} from a history. |
| 5973 | * "histype" may be one of the HIST_ values. |
| 5974 | */ |
| 5975 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5976 | del_history_entry(int histype, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5977 | { |
| 5978 | regmatch_T regmatch; |
| 5979 | histentry_T *hisptr; |
| 5980 | int idx; |
| 5981 | int i; |
| 5982 | int last; |
| 5983 | int found = FALSE; |
| 5984 | |
| 5985 | regmatch.regprog = NULL; |
| 5986 | regmatch.rm_ic = FALSE; /* always match case */ |
| 5987 | if (hislen != 0 |
| 5988 | && histype >= 0 |
| 5989 | && histype < HIST_COUNT |
| 5990 | && *str != NUL |
| 5991 | && (idx = hisidx[histype]) >= 0 |
| 5992 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 5993 | != NULL) |
| 5994 | { |
| 5995 | i = last = idx; |
| 5996 | do |
| 5997 | { |
| 5998 | hisptr = &history[histype][i]; |
| 5999 | if (hisptr->hisstr == NULL) |
| 6000 | break; |
| 6001 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 6002 | { |
| 6003 | found = TRUE; |
| 6004 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6005 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6006 | } |
| 6007 | else |
| 6008 | { |
| 6009 | if (i != last) |
| 6010 | { |
| 6011 | history[histype][last] = *hisptr; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6012 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6013 | } |
| 6014 | if (--last < 0) |
| 6015 | last += hislen; |
| 6016 | } |
| 6017 | if (--i < 0) |
| 6018 | i += hislen; |
| 6019 | } while (i != idx); |
| 6020 | if (history[histype][idx].hisstr == NULL) |
| 6021 | hisidx[histype] = -1; |
| 6022 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6023 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6024 | return found; |
| 6025 | } |
| 6026 | |
| 6027 | /* |
| 6028 | * Remove an indexed entry from a history. |
| 6029 | * "histype" may be one of the HIST_ values. |
| 6030 | */ |
| 6031 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6032 | del_history_idx(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6033 | { |
| 6034 | int i, j; |
| 6035 | |
| 6036 | i = calc_hist_idx(histype, idx); |
| 6037 | if (i < 0) |
| 6038 | return FALSE; |
| 6039 | idx = hisidx[histype]; |
| 6040 | vim_free(history[histype][i].hisstr); |
| 6041 | |
| 6042 | /* When deleting the last added search string in a mapping, reset |
| 6043 | * last_maptick, so that the last added search string isn't deleted again. |
| 6044 | */ |
| 6045 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 6046 | last_maptick = -1; |
| 6047 | |
| 6048 | while (i != idx) |
| 6049 | { |
| 6050 | j = (i + 1) % hislen; |
| 6051 | history[histype][i] = history[histype][j]; |
| 6052 | i = j; |
| 6053 | } |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6054 | clear_hist_entry(&history[histype][i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6055 | if (--i < 0) |
| 6056 | i += hislen; |
| 6057 | hisidx[histype] = i; |
| 6058 | return TRUE; |
| 6059 | } |
| 6060 | |
| 6061 | #endif /* FEAT_EVAL */ |
| 6062 | |
| 6063 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 6064 | /* |
| 6065 | * Very specific function to remove the value in ":set key=val" from the |
| 6066 | * history. |
| 6067 | */ |
| 6068 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6069 | remove_key_from_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6070 | { |
| 6071 | char_u *p; |
| 6072 | int i; |
| 6073 | |
| 6074 | i = hisidx[HIST_CMD]; |
| 6075 | if (i < 0) |
| 6076 | return; |
| 6077 | p = history[HIST_CMD][i].hisstr; |
| 6078 | if (p != NULL) |
| 6079 | for ( ; *p; ++p) |
| 6080 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 6081 | { |
| 6082 | p = vim_strchr(p + 3, '='); |
| 6083 | if (p == NULL) |
| 6084 | break; |
| 6085 | ++p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6086 | for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6087 | if (p[i] == '\\' && p[i + 1]) |
| 6088 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6089 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6090 | --p; |
| 6091 | } |
| 6092 | } |
| 6093 | #endif |
| 6094 | |
| 6095 | #endif /* FEAT_CMDHIST */ |
| 6096 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6097 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6098 | /* |
| 6099 | * Get pointer to the command line info to use. cmdline_paste() may clear |
| 6100 | * ccline and put the previous value in prev_ccline. |
| 6101 | */ |
| 6102 | static struct cmdline_info * |
| 6103 | get_ccline_ptr(void) |
| 6104 | { |
| 6105 | if ((State & CMDLINE) == 0) |
| 6106 | return NULL; |
| 6107 | if (ccline.cmdbuff != NULL) |
| 6108 | return &ccline; |
| 6109 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 6110 | return &prev_ccline; |
| 6111 | return NULL; |
| 6112 | } |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6113 | #endif |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6114 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6115 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6116 | /* |
| 6117 | * Get the current command line in allocated memory. |
| 6118 | * Only works when the command line is being edited. |
| 6119 | * Returns NULL when something is wrong. |
| 6120 | */ |
| 6121 | char_u * |
| 6122 | get_cmdline_str(void) |
| 6123 | { |
| 6124 | struct cmdline_info *p = get_ccline_ptr(); |
| 6125 | |
| 6126 | if (p == NULL) |
| 6127 | return NULL; |
| 6128 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
| 6129 | } |
| 6130 | |
| 6131 | /* |
| 6132 | * Get the current command line position, counted in bytes. |
| 6133 | * Zero is the first position. |
| 6134 | * Only works when the command line is being edited. |
| 6135 | * Returns -1 when something is wrong. |
| 6136 | */ |
| 6137 | int |
| 6138 | get_cmdline_pos(void) |
| 6139 | { |
| 6140 | struct cmdline_info *p = get_ccline_ptr(); |
| 6141 | |
| 6142 | if (p == NULL) |
| 6143 | return -1; |
| 6144 | return p->cmdpos; |
| 6145 | } |
| 6146 | |
| 6147 | /* |
| 6148 | * Set the command line byte position to "pos". Zero is the first position. |
| 6149 | * Only works when the command line is being edited. |
| 6150 | * Returns 1 when failed, 0 when OK. |
| 6151 | */ |
| 6152 | int |
| 6153 | set_cmdline_pos( |
| 6154 | int pos) |
| 6155 | { |
| 6156 | struct cmdline_info *p = get_ccline_ptr(); |
| 6157 | |
| 6158 | if (p == NULL) |
| 6159 | return 1; |
| 6160 | |
| 6161 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 6162 | * changed the command line. */ |
| 6163 | if (pos < 0) |
| 6164 | new_cmdpos = 0; |
| 6165 | else |
| 6166 | new_cmdpos = pos; |
| 6167 | return 0; |
| 6168 | } |
| 6169 | #endif |
| 6170 | |
| 6171 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
| 6172 | /* |
| 6173 | * Get the current command-line type. |
| 6174 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
| 6175 | * Only works when the command line is being edited. |
| 6176 | * Returns NUL when something is wrong. |
| 6177 | */ |
| 6178 | int |
| 6179 | get_cmdline_type(void) |
| 6180 | { |
| 6181 | struct cmdline_info *p = get_ccline_ptr(); |
| 6182 | |
| 6183 | if (p == NULL) |
| 6184 | return NUL; |
| 6185 | if (p->cmdfirstc == NUL) |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6186 | return |
| 6187 | # ifdef FEAT_EVAL |
| 6188 | (p->input_fn) ? '@' : |
| 6189 | # endif |
| 6190 | '-'; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6191 | return p->cmdfirstc; |
| 6192 | } |
| 6193 | #endif |
| 6194 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6195 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 6196 | /* |
| 6197 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 6198 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 6199 | * Returns OK if parsed successfully, otherwise FAIL. |
| 6200 | */ |
| 6201 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6202 | get_list_range(char_u **str, int *num1, int *num2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6203 | { |
| 6204 | int len; |
| 6205 | int first = FALSE; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 6206 | varnumber_T num; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6207 | |
| 6208 | *str = skipwhite(*str); |
| 6209 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 6210 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6211 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6212 | *str += len; |
| 6213 | *num1 = (int)num; |
| 6214 | first = TRUE; |
| 6215 | } |
| 6216 | *str = skipwhite(*str); |
| 6217 | if (**str == ',') /* parse "to" part of range */ |
| 6218 | { |
| 6219 | *str = skipwhite(*str + 1); |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6220 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6221 | if (len > 0) |
| 6222 | { |
| 6223 | *num2 = (int)num; |
| 6224 | *str = skipwhite(*str + len); |
| 6225 | } |
| 6226 | else if (!first) /* no number given at all */ |
| 6227 | return FAIL; |
| 6228 | } |
| 6229 | else if (first) /* only one number given */ |
| 6230 | *num2 = *num1; |
| 6231 | return OK; |
| 6232 | } |
| 6233 | #endif |
| 6234 | |
| 6235 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 6236 | /* |
| 6237 | * :history command - print a history |
| 6238 | */ |
| 6239 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6240 | ex_history(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6241 | { |
| 6242 | histentry_T *hist; |
| 6243 | int histype1 = HIST_CMD; |
| 6244 | int histype2 = HIST_CMD; |
| 6245 | int hisidx1 = 1; |
| 6246 | int hisidx2 = -1; |
| 6247 | int idx; |
| 6248 | int i, j, k; |
| 6249 | char_u *end; |
| 6250 | char_u *arg = eap->arg; |
| 6251 | |
| 6252 | if (hislen == 0) |
| 6253 | { |
| 6254 | MSG(_("'history' option is zero")); |
| 6255 | return; |
| 6256 | } |
| 6257 | |
| 6258 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 6259 | { |
| 6260 | end = arg; |
| 6261 | while (ASCII_ISALPHA(*end) |
| 6262 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 6263 | end++; |
| 6264 | i = *end; |
| 6265 | *end = NUL; |
| 6266 | histype1 = get_histtype(arg); |
| 6267 | if (histype1 == -1) |
| 6268 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 6269 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6270 | { |
| 6271 | histype1 = 0; |
| 6272 | histype2 = HIST_COUNT-1; |
| 6273 | } |
| 6274 | else |
| 6275 | { |
| 6276 | *end = i; |
| 6277 | EMSG(_(e_trailing)); |
| 6278 | return; |
| 6279 | } |
| 6280 | } |
| 6281 | else |
| 6282 | histype2 = histype1; |
| 6283 | *end = i; |
| 6284 | } |
| 6285 | else |
| 6286 | end = arg; |
| 6287 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 6288 | { |
| 6289 | EMSG(_(e_trailing)); |
| 6290 | return; |
| 6291 | } |
| 6292 | |
| 6293 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 6294 | { |
| 6295 | STRCPY(IObuff, "\n # "); |
| 6296 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 6297 | MSG_PUTS_TITLE(IObuff); |
| 6298 | idx = hisidx[histype1]; |
| 6299 | hist = history[histype1]; |
| 6300 | j = hisidx1; |
| 6301 | k = hisidx2; |
| 6302 | if (j < 0) |
| 6303 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 6304 | if (k < 0) |
| 6305 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 6306 | if (idx >= 0 && j <= k) |
| 6307 | for (i = idx + 1; !got_int; ++i) |
| 6308 | { |
| 6309 | if (i == hislen) |
| 6310 | i = 0; |
| 6311 | if (hist[i].hisstr != NULL |
| 6312 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 6313 | { |
| 6314 | msg_putchar('\n'); |
| 6315 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 6316 | hist[i].hisnum); |
| 6317 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 6318 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
Bram Moolenaar | 38f5f95 | 2012-01-26 13:01:59 +0100 | [diff] [blame] | 6319 | (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6320 | else |
| 6321 | STRCAT(IObuff, hist[i].hisstr); |
| 6322 | msg_outtrans(IObuff); |
| 6323 | out_flush(); |
| 6324 | } |
| 6325 | if (i == idx) |
| 6326 | break; |
| 6327 | } |
| 6328 | } |
| 6329 | } |
| 6330 | #endif |
| 6331 | |
| 6332 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6333 | /* |
| 6334 | * Buffers for history read from a viminfo file. Only valid while reading. |
| 6335 | */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6336 | static histentry_T *viminfo_history[HIST_COUNT] = |
| 6337 | {NULL, NULL, NULL, NULL, NULL}; |
| 6338 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 6339 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6340 | static int viminfo_add_at_front = FALSE; |
| 6341 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 6342 | static int hist_type2char(int type, int use_question); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6343 | |
| 6344 | /* |
| 6345 | * Translate a history type number to the associated character. |
| 6346 | */ |
| 6347 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6348 | hist_type2char( |
| 6349 | int type, |
| 6350 | int use_question) /* use '?' instead of '/' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6351 | { |
| 6352 | if (type == HIST_CMD) |
| 6353 | return ':'; |
| 6354 | if (type == HIST_SEARCH) |
| 6355 | { |
| 6356 | if (use_question) |
| 6357 | return '?'; |
| 6358 | else |
| 6359 | return '/'; |
| 6360 | } |
| 6361 | if (type == HIST_EXPR) |
| 6362 | return '='; |
| 6363 | return '@'; |
| 6364 | } |
| 6365 | |
| 6366 | /* |
| 6367 | * Prepare for reading the history from the viminfo file. |
| 6368 | * This allocates history arrays to store the read history lines. |
| 6369 | */ |
| 6370 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6371 | prepare_viminfo_history(int asklen, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6372 | { |
| 6373 | int i; |
| 6374 | int num; |
| 6375 | int type; |
| 6376 | int len; |
| 6377 | |
| 6378 | init_history(); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6379 | viminfo_add_at_front = (asklen != 0 && !writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6380 | if (asklen > hislen) |
| 6381 | asklen = hislen; |
| 6382 | |
| 6383 | for (type = 0; type < HIST_COUNT; ++type) |
| 6384 | { |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6385 | /* Count the number of empty spaces in the history list. Entries read |
| 6386 | * from viminfo previously are also considered empty. If there are |
| 6387 | * more spaces available than we request, then fill them up. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6388 | for (i = 0, num = 0; i < hislen; i++) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6389 | if (history[type][i].hisstr == NULL || history[type][i].viminfo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6390 | num++; |
| 6391 | len = asklen; |
| 6392 | if (num > len) |
| 6393 | len = num; |
| 6394 | if (len <= 0) |
| 6395 | viminfo_history[type] = NULL; |
| 6396 | else |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6397 | viminfo_history[type] = (histentry_T *)lalloc( |
| 6398 | (long_u)(len * sizeof(histentry_T)), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6399 | if (viminfo_history[type] == NULL) |
| 6400 | len = 0; |
| 6401 | viminfo_hislen[type] = len; |
| 6402 | viminfo_hisidx[type] = 0; |
| 6403 | } |
| 6404 | } |
| 6405 | |
| 6406 | /* |
| 6407 | * Accept a line from the viminfo, store it in the history array when it's |
| 6408 | * new. |
| 6409 | */ |
| 6410 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6411 | read_viminfo_history(vir_T *virp, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6412 | { |
| 6413 | int type; |
| 6414 | long_u len; |
| 6415 | char_u *val; |
| 6416 | char_u *p; |
| 6417 | |
| 6418 | type = hist_char2type(virp->vir_line[0]); |
| 6419 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6420 | { |
| 6421 | val = viminfo_readstring(virp, 1, TRUE); |
| 6422 | if (val != NULL && *val != NUL) |
| 6423 | { |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6424 | int sep = (*val == ' ' ? NUL : *val); |
| 6425 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6426 | if (!in_history(type, val + (type == HIST_SEARCH), |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6427 | viminfo_add_at_front, sep, writing)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6428 | { |
| 6429 | /* Need to re-allocate to append the separator byte. */ |
| 6430 | len = STRLEN(val); |
| 6431 | p = lalloc(len + 2, TRUE); |
| 6432 | if (p != NULL) |
| 6433 | { |
| 6434 | if (type == HIST_SEARCH) |
| 6435 | { |
| 6436 | /* Search entry: Move the separator from the first |
| 6437 | * column to after the NUL. */ |
| 6438 | mch_memmove(p, val + 1, (size_t)len); |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6439 | p[len] = sep; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6440 | } |
| 6441 | else |
| 6442 | { |
| 6443 | /* Not a search entry: No separator in the viminfo |
| 6444 | * file, add a NUL separator. */ |
| 6445 | mch_memmove(p, val, (size_t)len + 1); |
| 6446 | p[len + 1] = NUL; |
| 6447 | } |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6448 | viminfo_history[type][viminfo_hisidx[type]].hisstr = p; |
| 6449 | viminfo_history[type][viminfo_hisidx[type]].time_set = 0; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6450 | viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE; |
| 6451 | viminfo_history[type][viminfo_hisidx[type]].hisnum = 0; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6452 | viminfo_hisidx[type]++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6453 | } |
| 6454 | } |
| 6455 | } |
| 6456 | vim_free(val); |
| 6457 | } |
| 6458 | return viminfo_readline(virp); |
| 6459 | } |
| 6460 | |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6461 | /* |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6462 | * Accept a new style history line from the viminfo, store it in the history |
| 6463 | * array when it's new. |
| 6464 | */ |
| 6465 | void |
| 6466 | handle_viminfo_history( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6467 | garray_T *values, |
| 6468 | int writing) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6469 | { |
| 6470 | int type; |
| 6471 | long_u len; |
| 6472 | char_u *val; |
| 6473 | char_u *p; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6474 | bval_T *vp = (bval_T *)values->ga_data; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6475 | |
| 6476 | /* Check the format: |
| 6477 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6478 | if (values->ga_len < 4 |
| 6479 | || vp[0].bv_type != BVAL_NR |
| 6480 | || vp[1].bv_type != BVAL_NR |
| 6481 | || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY) |
| 6482 | || vp[3].bv_type != BVAL_STRING) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6483 | return; |
| 6484 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6485 | type = vp[0].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6486 | if (type >= HIST_COUNT) |
| 6487 | return; |
| 6488 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6489 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6490 | val = vp[3].bv_string; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6491 | if (val != NULL && *val != NUL) |
| 6492 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6493 | int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR |
| 6494 | ? vp[2].bv_nr : NUL; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6495 | int idx; |
| 6496 | int overwrite = FALSE; |
| 6497 | |
| 6498 | if (!in_history(type, val, viminfo_add_at_front, sep, writing)) |
| 6499 | { |
| 6500 | /* If lines were written by an older Vim we need to avoid |
| 6501 | * getting duplicates. See if the entry already exists. */ |
| 6502 | for (idx = 0; idx < viminfo_hisidx[type]; ++idx) |
| 6503 | { |
| 6504 | p = viminfo_history[type][idx].hisstr; |
| 6505 | if (STRCMP(val, p) == 0 |
| 6506 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
| 6507 | { |
| 6508 | overwrite = TRUE; |
| 6509 | break; |
| 6510 | } |
| 6511 | } |
| 6512 | |
| 6513 | if (!overwrite) |
| 6514 | { |
| 6515 | /* Need to re-allocate to append the separator byte. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6516 | len = vp[3].bv_len; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6517 | p = lalloc(len + 2, TRUE); |
| 6518 | } |
Bram Moolenaar | 72e697d | 2016-06-13 22:48:01 +0200 | [diff] [blame] | 6519 | else |
| 6520 | len = 0; /* for picky compilers */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6521 | if (p != NULL) |
| 6522 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6523 | viminfo_history[type][idx].time_set = vp[1].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6524 | if (!overwrite) |
| 6525 | { |
| 6526 | mch_memmove(p, val, (size_t)len + 1); |
| 6527 | /* Put the separator after the NUL. */ |
| 6528 | p[len + 1] = sep; |
| 6529 | viminfo_history[type][idx].hisstr = p; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6530 | viminfo_history[type][idx].hisnum = 0; |
| 6531 | viminfo_history[type][idx].viminfo = TRUE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6532 | viminfo_hisidx[type]++; |
| 6533 | } |
| 6534 | } |
| 6535 | } |
| 6536 | } |
| 6537 | } |
| 6538 | } |
| 6539 | |
| 6540 | /* |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6541 | * Concatenate history lines from viminfo after the lines typed in this Vim. |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6542 | */ |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6543 | static void |
| 6544 | concat_history(int type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6545 | { |
| 6546 | int idx; |
| 6547 | int i; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6548 | |
| 6549 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 6550 | if (idx >= hislen) |
| 6551 | idx -= hislen; |
| 6552 | else if (idx < 0) |
| 6553 | idx = hislen - 1; |
| 6554 | if (viminfo_add_at_front) |
| 6555 | hisidx[type] = idx; |
| 6556 | else |
| 6557 | { |
| 6558 | if (hisidx[type] == -1) |
| 6559 | hisidx[type] = hislen - 1; |
| 6560 | do |
| 6561 | { |
| 6562 | if (history[type][idx].hisstr != NULL |
| 6563 | || history[type][idx].viminfo) |
| 6564 | break; |
| 6565 | if (++idx == hislen) |
| 6566 | idx = 0; |
| 6567 | } while (idx != hisidx[type]); |
| 6568 | if (idx != hisidx[type] && --idx < 0) |
| 6569 | idx = hislen - 1; |
| 6570 | } |
| 6571 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6572 | { |
| 6573 | vim_free(history[type][idx].hisstr); |
| 6574 | history[type][idx].hisstr = viminfo_history[type][i].hisstr; |
| 6575 | history[type][idx].viminfo = TRUE; |
| 6576 | history[type][idx].time_set = viminfo_history[type][i].time_set; |
| 6577 | if (--idx < 0) |
| 6578 | idx = hislen - 1; |
| 6579 | } |
| 6580 | idx += 1; |
| 6581 | idx %= hislen; |
| 6582 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6583 | { |
| 6584 | history[type][idx++].hisnum = ++hisnum[type]; |
| 6585 | idx %= hislen; |
| 6586 | } |
| 6587 | } |
| 6588 | |
| 6589 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6590 | static int |
| 6591 | #ifdef __BORLANDC__ |
| 6592 | _RTLENTRYF |
| 6593 | #endif |
| 6594 | sort_hist(const void *s1, const void *s2) |
| 6595 | { |
| 6596 | histentry_T *p1 = *(histentry_T **)s1; |
| 6597 | histentry_T *p2 = *(histentry_T **)s2; |
| 6598 | |
| 6599 | if (p1->time_set < p2->time_set) return -1; |
| 6600 | if (p1->time_set > p2->time_set) return 1; |
| 6601 | return 0; |
| 6602 | } |
| 6603 | #endif |
| 6604 | |
| 6605 | /* |
| 6606 | * Merge history lines from viminfo and lines typed in this Vim based on the |
| 6607 | * timestamp; |
| 6608 | */ |
| 6609 | static void |
| 6610 | merge_history(int type) |
| 6611 | { |
| 6612 | int max_len; |
| 6613 | histentry_T **tot_hist; |
| 6614 | histentry_T *new_hist; |
| 6615 | int i; |
| 6616 | int len; |
| 6617 | |
| 6618 | /* Make one long list with all entries. */ |
| 6619 | max_len = hislen + viminfo_hisidx[type]; |
| 6620 | tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *)); |
| 6621 | new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T)); |
| 6622 | if (tot_hist == NULL || new_hist == NULL) |
| 6623 | { |
| 6624 | vim_free(tot_hist); |
| 6625 | vim_free(new_hist); |
| 6626 | return; |
| 6627 | } |
| 6628 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6629 | tot_hist[i] = &viminfo_history[type][i]; |
| 6630 | len = i; |
| 6631 | for (i = 0; i < hislen; i++) |
| 6632 | if (history[type][i].hisstr != NULL) |
| 6633 | tot_hist[len++] = &history[type][i]; |
| 6634 | |
| 6635 | /* Sort the list on timestamp. */ |
| 6636 | qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist); |
| 6637 | |
| 6638 | /* Keep the newest ones. */ |
| 6639 | for (i = 0; i < hislen; i++) |
| 6640 | { |
| 6641 | if (i < len) |
| 6642 | { |
| 6643 | new_hist[i] = *tot_hist[i]; |
| 6644 | tot_hist[i]->hisstr = NULL; |
| 6645 | if (new_hist[i].hisnum == 0) |
| 6646 | new_hist[i].hisnum = ++hisnum[type]; |
| 6647 | } |
| 6648 | else |
| 6649 | clear_hist_entry(&new_hist[i]); |
| 6650 | } |
Bram Moolenaar | a890f5e | 2016-06-12 23:03:19 +0200 | [diff] [blame] | 6651 | hisidx[type] = (i < len ? i : len) - 1; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6652 | |
| 6653 | /* Free what is not kept. */ |
| 6654 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6655 | vim_free(viminfo_history[type][i].hisstr); |
| 6656 | for (i = 0; i < hislen; i++) |
| 6657 | vim_free(history[type][i].hisstr); |
| 6658 | vim_free(history[type]); |
| 6659 | history[type] = new_hist; |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 6660 | vim_free(tot_hist); |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6661 | } |
| 6662 | |
| 6663 | /* |
| 6664 | * Finish reading history lines from viminfo. Not used when writing viminfo. |
| 6665 | */ |
| 6666 | void |
| 6667 | finish_viminfo_history(vir_T *virp) |
| 6668 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6669 | int type; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6670 | int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6671 | |
| 6672 | for (type = 0; type < HIST_COUNT; ++type) |
| 6673 | { |
| 6674 | if (history[type] == NULL) |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6675 | continue; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6676 | |
| 6677 | if (merge) |
| 6678 | merge_history(type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6679 | else |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6680 | concat_history(type); |
| 6681 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6682 | vim_free(viminfo_history[type]); |
| 6683 | viminfo_history[type] = NULL; |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6684 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6685 | } |
| 6686 | } |
| 6687 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6688 | /* |
| 6689 | * Write history to viminfo file in "fp". |
| 6690 | * When "merge" is TRUE merge history lines with a previously read viminfo |
| 6691 | * file, data is in viminfo_history[]. |
| 6692 | * When "merge" is FALSE just write all history lines. Used for ":wviminfo!". |
| 6693 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6694 | void |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6695 | write_viminfo_history(FILE *fp, int merge) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6696 | { |
| 6697 | int i; |
| 6698 | int type; |
| 6699 | int num_saved; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6700 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6701 | |
| 6702 | init_history(); |
| 6703 | if (hislen == 0) |
| 6704 | return; |
| 6705 | for (type = 0; type < HIST_COUNT; ++type) |
| 6706 | { |
| 6707 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 6708 | if (num_saved == 0) |
| 6709 | continue; |
| 6710 | if (num_saved < 0) /* Use default */ |
| 6711 | num_saved = hislen; |
| 6712 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 6713 | type == HIST_CMD ? _("Command Line") : |
| 6714 | type == HIST_SEARCH ? _("Search String") : |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6715 | type == HIST_EXPR ? _("Expression") : |
| 6716 | type == HIST_INPUT ? _("Input Line") : |
| 6717 | _("Debug Line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6718 | if (num_saved > hislen) |
| 6719 | num_saved = hislen; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6720 | |
| 6721 | /* |
| 6722 | * Merge typed and viminfo history: |
| 6723 | * round 1: history of typed commands. |
| 6724 | * round 2: history from recently read viminfo. |
| 6725 | */ |
| 6726 | for (round = 1; round <= 2; ++round) |
| 6727 | { |
Bram Moolenaar | a8565fe | 2013-04-15 16:14:22 +0200 | [diff] [blame] | 6728 | if (round == 1) |
| 6729 | /* start at newest entry, somewhere in the list */ |
| 6730 | i = hisidx[type]; |
| 6731 | else if (viminfo_hisidx[type] > 0) |
| 6732 | /* start at newest entry, first in the list */ |
| 6733 | i = 0; |
| 6734 | else |
| 6735 | /* empty list */ |
| 6736 | i = -1; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6737 | if (i >= 0) |
| 6738 | while (num_saved > 0 |
| 6739 | && !(round == 2 && i >= viminfo_hisidx[type])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6740 | { |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6741 | char_u *p; |
| 6742 | time_t timestamp; |
| 6743 | int c = NUL; |
| 6744 | |
| 6745 | if (round == 1) |
| 6746 | { |
| 6747 | p = history[type][i].hisstr; |
| 6748 | timestamp = history[type][i].time_set; |
| 6749 | } |
| 6750 | else |
| 6751 | { |
| 6752 | p = viminfo_history[type] == NULL ? NULL |
| 6753 | : viminfo_history[type][i].hisstr; |
| 6754 | timestamp = viminfo_history[type] == NULL ? 0 |
| 6755 | : viminfo_history[type][i].time_set; |
| 6756 | } |
| 6757 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6758 | if (p != NULL && (round == 2 |
| 6759 | || !merge |
| 6760 | || !history[type][i].viminfo)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6761 | { |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6762 | --num_saved; |
| 6763 | fputc(hist_type2char(type, TRUE), fp); |
| 6764 | /* For the search history: put the separator in the |
| 6765 | * second column; use a space if there isn't one. */ |
| 6766 | if (type == HIST_SEARCH) |
| 6767 | { |
| 6768 | c = p[STRLEN(p) + 1]; |
| 6769 | putc(c == NUL ? ' ' : c, fp); |
| 6770 | } |
| 6771 | viminfo_writestring(fp, p); |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6772 | |
| 6773 | { |
| 6774 | char cbuf[NUMBUFLEN]; |
| 6775 | |
| 6776 | /* New style history with a bar line. Format: |
| 6777 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
| 6778 | if (c == NUL) |
| 6779 | cbuf[0] = NUL; |
| 6780 | else |
| 6781 | sprintf(cbuf, "%d", c); |
| 6782 | fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY, |
| 6783 | type, (long)timestamp, cbuf); |
| 6784 | barline_writestring(fp, p, LSIZE - 20); |
| 6785 | putc('\n', fp); |
| 6786 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6787 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6788 | if (round == 1) |
| 6789 | { |
| 6790 | /* Decrement index, loop around and stop when back at |
| 6791 | * the start. */ |
| 6792 | if (--i < 0) |
| 6793 | i = hislen - 1; |
| 6794 | if (i == hisidx[type]) |
| 6795 | break; |
| 6796 | } |
| 6797 | else |
| 6798 | { |
| 6799 | /* Increment index. Stop at the end in the while. */ |
| 6800 | ++i; |
| 6801 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6802 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6803 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6804 | for (i = 0; i < viminfo_hisidx[type]; ++i) |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6805 | if (viminfo_history[type] != NULL) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6806 | vim_free(viminfo_history[type][i].hisstr); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6807 | vim_free(viminfo_history[type]); |
| 6808 | viminfo_history[type] = NULL; |
Bram Moolenaar | b70a473 | 2013-04-15 22:22:57 +0200 | [diff] [blame] | 6809 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6810 | } |
| 6811 | } |
| 6812 | #endif /* FEAT_VIMINFO */ |
| 6813 | |
| 6814 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 6815 | /* |
| 6816 | * Write a character at the current cursor+offset position. |
| 6817 | * It is directly written into the command buffer block. |
| 6818 | */ |
| 6819 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6820 | cmd_pchar(int c, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6821 | { |
| 6822 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 6823 | { |
| 6824 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 6825 | return; |
| 6826 | } |
| 6827 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 6828 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 6829 | } |
| 6830 | |
| 6831 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6832 | cmd_gchar(int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6833 | { |
| 6834 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 6835 | { |
| 6836 | /* EMSG(_("cmd_gchar beyond the command length")); */ |
| 6837 | return NUL; |
| 6838 | } |
| 6839 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 6840 | } |
| 6841 | #endif |
| 6842 | |
| 6843 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 6844 | /* |
| 6845 | * Open a window on the current command line and history. Allow editing in |
| 6846 | * the window. Returns when the window is closed. |
| 6847 | * Returns: |
| 6848 | * CR if the command is to be executed |
| 6849 | * Ctrl_C if it is to be abandoned |
| 6850 | * K_IGNORE if editing continues |
| 6851 | */ |
| 6852 | static int |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 6853 | open_cmdwin(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6854 | { |
| 6855 | struct cmdline_info save_ccline; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6856 | bufref_T old_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6857 | win_T *old_curwin = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6858 | bufref_T bufref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6859 | win_T *wp; |
| 6860 | int i; |
| 6861 | linenr_T lnum; |
| 6862 | int histtype; |
| 6863 | garray_T winsizes; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6864 | int save_restart_edit = restart_edit; |
| 6865 | int save_State = State; |
| 6866 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6867 | #ifdef FEAT_RIGHTLEFT |
| 6868 | int save_cmdmsg_rl = cmdmsg_rl; |
| 6869 | #endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6870 | #ifdef FEAT_FOLDING |
| 6871 | int save_KeyTyped; |
| 6872 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6873 | |
| 6874 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 6875 | if (cmdwin_type != 0 |
| 6876 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 6877 | || cmdline_star > 0 |
| 6878 | # endif |
| 6879 | ) |
| 6880 | { |
| 6881 | beep_flush(); |
| 6882 | return K_IGNORE; |
| 6883 | } |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6884 | set_bufref(&old_curbuf, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6885 | |
| 6886 | /* Save current window sizes. */ |
| 6887 | win_size_save(&winsizes); |
| 6888 | |
| 6889 | # ifdef FEAT_AUTOCMD |
| 6890 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6891 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6892 | # endif |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 6893 | /* don't use a new tab page */ |
| 6894 | cmdmod.tab = 0; |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 6895 | cmdmod.noswapfile = 1; |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 6896 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6897 | /* Create a window for the command-line buffer. */ |
| 6898 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 6899 | { |
| 6900 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6901 | # ifdef FEAT_AUTOCMD |
| 6902 | unblock_autocmds(); |
| 6903 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6904 | return K_IGNORE; |
| 6905 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6906 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6907 | |
| 6908 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 6909 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6910 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6911 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6912 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 6913 | #ifdef FEAT_FOLDING |
| 6914 | curwin->w_p_fen = FALSE; |
| 6915 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6916 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6917 | curwin->w_p_rl = cmdmsg_rl; |
| 6918 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6919 | # endif |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 6920 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6921 | |
| 6922 | # ifdef FEAT_AUTOCMD |
| 6923 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6924 | unblock_autocmds(); |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 6925 | /* But don't allow switching to another buffer. */ |
| 6926 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6927 | # endif |
| 6928 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6929 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 6930 | need_wait_return = FALSE; |
| 6931 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6932 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6933 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 6934 | { |
| 6935 | if (p_wc == TAB) |
| 6936 | { |
| 6937 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 6938 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 6939 | } |
| 6940 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 6941 | } |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 6942 | # ifdef FEAT_AUTOCMD |
| 6943 | --curbuf_lock; |
| 6944 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6945 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6946 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 6947 | * sets 'textwidth' to 78). */ |
| 6948 | curbuf->b_p_tw = 0; |
| 6949 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6950 | /* Fill the buffer with the history. */ |
| 6951 | init_history(); |
| 6952 | if (hislen > 0) |
| 6953 | { |
| 6954 | i = hisidx[histtype]; |
| 6955 | if (i >= 0) |
| 6956 | { |
| 6957 | lnum = 0; |
| 6958 | do |
| 6959 | { |
| 6960 | if (++i == hislen) |
| 6961 | i = 0; |
| 6962 | if (history[histtype][i].hisstr != NULL) |
| 6963 | ml_append(lnum++, history[histtype][i].hisstr, |
| 6964 | (colnr_T)0, FALSE); |
| 6965 | } |
| 6966 | while (i != hisidx[histtype]); |
| 6967 | } |
| 6968 | } |
| 6969 | |
| 6970 | /* Replace the empty last line with the current command-line and put the |
| 6971 | * cursor there. */ |
| 6972 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 6973 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 6974 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6975 | changed_line_abv_curs(); |
| 6976 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 6977 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6978 | |
| 6979 | /* Save the command line info, can be used recursively. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 6980 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6981 | |
| 6982 | /* No Ex mode here! */ |
| 6983 | exmode_active = 0; |
| 6984 | |
| 6985 | State = NORMAL; |
| 6986 | # ifdef FEAT_MOUSE |
| 6987 | setmouse(); |
| 6988 | # endif |
| 6989 | |
| 6990 | # ifdef FEAT_AUTOCMD |
| 6991 | /* Trigger CmdwinEnter autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 6992 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 6993 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 6994 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6995 | # endif |
| 6996 | |
| 6997 | i = RedrawingDisabled; |
| 6998 | RedrawingDisabled = 0; |
| 6999 | |
| 7000 | /* |
| 7001 | * Call the main loop until <CR> or CTRL-C is typed. |
| 7002 | */ |
| 7003 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 7004 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7005 | |
| 7006 | RedrawingDisabled = i; |
| 7007 | |
| 7008 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7009 | |
| 7010 | # ifdef FEAT_FOLDING |
| 7011 | save_KeyTyped = KeyTyped; |
| 7012 | # endif |
| 7013 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7014 | /* Trigger CmdwinLeave autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7015 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE); |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7016 | |
| 7017 | # ifdef FEAT_FOLDING |
| 7018 | /* Restore KeyTyped in case it is modified by autocommands */ |
| 7019 | KeyTyped = save_KeyTyped; |
| 7020 | # endif |
| 7021 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7022 | # endif |
| 7023 | |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 7024 | /* Restore the command line info. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7025 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7026 | cmdwin_type = 0; |
| 7027 | |
| 7028 | exmode_active = save_exmode; |
| 7029 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 7030 | /* Safety check: The old window or buffer was deleted: It's a bug when |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7031 | * this happens! */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7032 | if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7033 | { |
| 7034 | cmdwin_result = Ctrl_C; |
| 7035 | EMSG(_("E199: Active window or buffer deleted")); |
| 7036 | } |
| 7037 | else |
| 7038 | { |
| 7039 | # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 7040 | /* autocmds may abort script processing */ |
| 7041 | if (aborting() && cmdwin_result != K_IGNORE) |
| 7042 | cmdwin_result = Ctrl_C; |
| 7043 | # endif |
| 7044 | /* Set the new command line from the cmdline buffer. */ |
| 7045 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7046 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7047 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7048 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 7049 | |
| 7050 | if (histtype == HIST_CMD) |
| 7051 | { |
| 7052 | /* Execute the command directly. */ |
| 7053 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 7054 | cmdwin_result = CAR; |
| 7055 | } |
| 7056 | else |
| 7057 | { |
| 7058 | /* First need to cancel what we were doing. */ |
| 7059 | ccline.cmdbuff = NULL; |
| 7060 | stuffcharReadbuff(':'); |
| 7061 | stuffReadbuff((char_u *)p); |
| 7062 | stuffcharReadbuff(CAR); |
| 7063 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7064 | } |
| 7065 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 7066 | { |
| 7067 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 7068 | cmdwin_result = CAR; |
| 7069 | } |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 7070 | else if (cmdwin_result == Ctrl_C) |
| 7071 | { |
| 7072 | /* :q or :close, don't execute any command |
| 7073 | * and don't modify the cmd window. */ |
| 7074 | ccline.cmdbuff = NULL; |
| 7075 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7076 | else |
| 7077 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 7078 | if (ccline.cmdbuff == NULL) |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7079 | { |
| 7080 | ccline.cmdbuff = vim_strsave((char_u *)""); |
| 7081 | ccline.cmdlen = 0; |
| 7082 | ccline.cmdbufflen = 1; |
| 7083 | ccline.cmdpos = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7084 | cmdwin_result = Ctrl_C; |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7085 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7086 | else |
| 7087 | { |
| 7088 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 7089 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 7090 | ccline.cmdpos = curwin->w_cursor.col; |
| 7091 | if (ccline.cmdpos > ccline.cmdlen) |
| 7092 | ccline.cmdpos = ccline.cmdlen; |
| 7093 | if (cmdwin_result == K_IGNORE) |
| 7094 | { |
| 7095 | set_cmdspos_cursor(); |
| 7096 | redrawcmd(); |
| 7097 | } |
| 7098 | } |
| 7099 | |
| 7100 | # ifdef FEAT_AUTOCMD |
| 7101 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7102 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7103 | # endif |
Bram Moolenaar | fa67fbe | 2015-06-25 18:20:36 +0200 | [diff] [blame] | 7104 | # ifdef FEAT_CONCEAL |
| 7105 | /* Avoid command-line window first character being concealed. */ |
| 7106 | curwin->w_p_cole = 0; |
| 7107 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7108 | wp = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7109 | set_bufref(&bufref, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7110 | win_goto(old_curwin); |
| 7111 | win_close(wp, TRUE); |
Bram Moolenaar | 8006d69 | 2010-03-02 17:23:21 +0100 | [diff] [blame] | 7112 | |
| 7113 | /* win_close() may have already wiped the buffer when 'bh' is |
| 7114 | * set to 'wipe' */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7115 | if (bufref_valid(&bufref)) |
| 7116 | close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7117 | |
| 7118 | /* Restore window sizes. */ |
| 7119 | win_size_restore(&winsizes); |
| 7120 | |
| 7121 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7122 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7123 | # endif |
| 7124 | } |
| 7125 | |
| 7126 | ga_clear(&winsizes); |
| 7127 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7128 | # ifdef FEAT_RIGHTLEFT |
| 7129 | cmdmsg_rl = save_cmdmsg_rl; |
| 7130 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7131 | |
| 7132 | State = save_State; |
| 7133 | # ifdef FEAT_MOUSE |
| 7134 | setmouse(); |
| 7135 | # endif |
| 7136 | |
| 7137 | return cmdwin_result; |
| 7138 | } |
| 7139 | #endif /* FEAT_CMDWIN */ |
| 7140 | |
| 7141 | /* |
| 7142 | * Used for commands that either take a simple command string argument, or: |
| 7143 | * cmd << endmarker |
| 7144 | * {script} |
| 7145 | * endmarker |
| 7146 | * Returns a pointer to allocated memory with {script} or NULL. |
| 7147 | */ |
| 7148 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7149 | script_get(exarg_T *eap, char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7150 | { |
| 7151 | char_u *theline; |
| 7152 | char *end_pattern = NULL; |
| 7153 | char dot[] = "."; |
| 7154 | garray_T ga; |
| 7155 | |
| 7156 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 7157 | return NULL; |
| 7158 | |
| 7159 | ga_init2(&ga, 1, 0x400); |
| 7160 | |
| 7161 | if (cmd[2] != NUL) |
| 7162 | end_pattern = (char *)skipwhite(cmd + 2); |
| 7163 | else |
| 7164 | end_pattern = dot; |
| 7165 | |
| 7166 | for (;;) |
| 7167 | { |
| 7168 | theline = eap->getline( |
| 7169 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7170 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7171 | #endif |
| 7172 | NUL, eap->cookie, 0); |
| 7173 | |
| 7174 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7175 | { |
| 7176 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7177 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7178 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7179 | |
| 7180 | ga_concat(&ga, theline); |
| 7181 | ga_append(&ga, '\n'); |
| 7182 | vim_free(theline); |
| 7183 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 7184 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7185 | |
| 7186 | return (char_u *)ga.ga_data; |
| 7187 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 7188 | |
| 7189 | #ifdef FEAT_SEARCH_EXTRA |
| 7190 | static void |
| 7191 | set_search_match(pos_T *t) |
| 7192 | { |
| 7193 | /* |
| 7194 | * First move cursor to end of match, then to the start. This |
| 7195 | * moves the whole match onto the screen when 'nowrap' is set. |
| 7196 | */ |
| 7197 | t->lnum += search_match_lines; |
| 7198 | t->col = search_match_endcol; |
| 7199 | if (t->lnum > curbuf->b_ml.ml_line_count) |
| 7200 | { |
| 7201 | t->lnum = curbuf->b_ml.ml_line_count; |
| 7202 | coladvance((colnr_T)MAXCOL); |
| 7203 | } |
| 7204 | } |
| 7205 | #endif |