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 | /* |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 162 | * Abandon the command line. |
| 163 | */ |
| 164 | static void |
| 165 | abandon_cmdline(void) |
| 166 | { |
| 167 | vim_free(ccline.cmdbuff); |
| 168 | ccline.cmdbuff = NULL; |
| 169 | if (msg_scrolled == 0) |
| 170 | compute_cmdrow(); |
| 171 | MSG(""); |
| 172 | redraw_cmdline = TRUE; |
| 173 | } |
| 174 | |
Bram Moolenaar | ee219b0 | 2017-12-17 14:55:01 +0100 | [diff] [blame] | 175 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 176 | /* |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 177 | * Guess that the pattern matches everything. Only finds specific cases, such |
| 178 | * as a trailing \|, which can happen while typing a pattern. |
| 179 | */ |
| 180 | static int |
| 181 | empty_pattern(char_u *p) |
| 182 | { |
| 183 | int n = STRLEN(p); |
| 184 | |
| 185 | /* remove trailing \v and the like */ |
| 186 | while (n >= 2 && p[n - 2] == '\\' |
| 187 | && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL) |
| 188 | n -= 2; |
| 189 | return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|'); |
| 190 | } |
Bram Moolenaar | ee219b0 | 2017-12-17 14:55:01 +0100 | [diff] [blame] | 191 | #endif |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 192 | |
| 193 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 194 | * getcmdline() - accept a command line starting with firstc. |
| 195 | * |
| 196 | * firstc == ':' get ":" command line. |
| 197 | * firstc == '/' or '?' get search pattern |
| 198 | * firstc == '=' get expression |
| 199 | * firstc == '@' get text for input() function |
| 200 | * firstc == '>' get text for debug mode |
| 201 | * firstc == NUL get text for :insert command |
| 202 | * firstc == -1 like NUL, and break on CTRL-C |
| 203 | * |
| 204 | * The line is collected in ccline.cmdbuff, which is reallocated to fit the |
| 205 | * command line. |
| 206 | * |
| 207 | * Careful: getcmdline() can be called recursively! |
| 208 | * |
| 209 | * Return pointer to allocated string if there is a commandline, NULL |
| 210 | * otherwise. |
| 211 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 212 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 213 | getcmdline( |
| 214 | int firstc, |
| 215 | long count UNUSED, /* only used for incremental search */ |
| 216 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 217 | { |
| 218 | int c; |
| 219 | int i; |
| 220 | int j; |
| 221 | int gotesc = FALSE; /* TRUE when <ESC> just typed */ |
| 222 | int do_abbr; /* when TRUE check for abbr. */ |
| 223 | #ifdef FEAT_CMDHIST |
| 224 | char_u *lookfor = NULL; /* string to match */ |
| 225 | int hiscnt; /* current history line in use */ |
| 226 | int histype; /* history type to be used */ |
| 227 | #endif |
| 228 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 229 | pos_T search_start; /* where 'incsearch' starts searching */ |
| 230 | pos_T save_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 231 | colnr_T old_curswant; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 232 | colnr_T init_curswant = curwin->w_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 233 | colnr_T old_leftcol; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 234 | colnr_T init_leftcol = curwin->w_leftcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | linenr_T old_topline; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 236 | linenr_T init_topline = curwin->w_topline; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 237 | pos_T match_start = curwin->w_cursor; |
| 238 | pos_T match_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 239 | # ifdef FEAT_DIFF |
| 240 | int old_topfill; |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 241 | int init_topfill = curwin->w_topfill; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 242 | # endif |
| 243 | linenr_T old_botline; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 244 | linenr_T init_botline = curwin->w_botline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 245 | int did_incsearch = FALSE; |
| 246 | int incsearch_postponed = FALSE; |
| 247 | #endif |
| 248 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 249 | int wim_index = 0; /* index in wim_flags[] */ |
| 250 | int res; |
| 251 | int save_msg_scroll = msg_scroll; |
| 252 | int save_State = State; /* remember State when called */ |
| 253 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 254 | #ifdef FEAT_MOUSE |
| 255 | /* mouse drag and release events are ignored, unless they are |
| 256 | * preceded with a mouse down event */ |
| 257 | int ignore_drag_release = TRUE; |
| 258 | #endif |
| 259 | #ifdef FEAT_EVAL |
| 260 | int break_ctrl_c = FALSE; |
| 261 | #endif |
| 262 | expand_T xpc; |
| 263 | long *b_im_ptr = NULL; |
Bram Moolenaar | 4d85051 | 2017-02-09 18:25:14 +0100 | [diff] [blame] | 264 | #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA) |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 265 | /* Everything that may work recursively should save and restore the |
| 266 | * current command line in save_ccline. That includes update_screen(), a |
| 267 | * custom status line may invoke ":normal". */ |
| 268 | struct cmdline_info save_ccline; |
| 269 | #endif |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 270 | #ifdef FEAT_AUTOCMD |
| 271 | int cmdline_type; |
| 272 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 273 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | #ifdef FEAT_EVAL |
| 275 | if (firstc == -1) |
| 276 | { |
| 277 | firstc = NUL; |
| 278 | break_ctrl_c = TRUE; |
| 279 | } |
| 280 | #endif |
| 281 | #ifdef FEAT_RIGHTLEFT |
| 282 | /* start without Hebrew mapping for a command line */ |
| 283 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 284 | cmd_hkmap = 0; |
| 285 | #endif |
| 286 | |
| 287 | ccline.overstrike = FALSE; /* always start in insert mode */ |
| 288 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 289 | CLEAR_POS(&match_end); |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 290 | save_cursor = curwin->w_cursor; /* may be restored later */ |
| 291 | search_start = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 292 | old_curswant = curwin->w_curswant; |
| 293 | old_leftcol = curwin->w_leftcol; |
| 294 | old_topline = curwin->w_topline; |
| 295 | # ifdef FEAT_DIFF |
| 296 | old_topfill = curwin->w_topfill; |
| 297 | # endif |
| 298 | old_botline = curwin->w_botline; |
| 299 | #endif |
| 300 | |
| 301 | /* |
| 302 | * set some variables for redrawcmd() |
| 303 | */ |
| 304 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 305 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 306 | |
| 307 | /* alloc initial ccline.cmdbuff */ |
| 308 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 309 | if (ccline.cmdbuff == NULL) |
| 310 | return NULL; /* out of memory */ |
| 311 | ccline.cmdlen = ccline.cmdpos = 0; |
| 312 | ccline.cmdbuff[0] = NUL; |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 313 | sb_text_start_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 314 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 315 | /* autoindent for :insert and :append */ |
| 316 | if (firstc <= 0) |
| 317 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 318 | vim_memset(ccline.cmdbuff, ' ', indent); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 319 | ccline.cmdbuff[indent] = NUL; |
| 320 | ccline.cmdpos = indent; |
| 321 | ccline.cmdspos = indent; |
| 322 | ccline.cmdlen = indent; |
| 323 | } |
| 324 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 325 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 326 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 327 | |
| 328 | #ifdef FEAT_RIGHTLEFT |
| 329 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 330 | && (firstc == '/' || firstc == '?')) |
| 331 | cmdmsg_rl = TRUE; |
| 332 | else |
| 333 | cmdmsg_rl = FALSE; |
| 334 | #endif |
| 335 | |
| 336 | redir_off = TRUE; /* don't redirect the typed command */ |
| 337 | if (!cmd_silent) |
| 338 | { |
| 339 | i = msg_scrolled; |
| 340 | msg_scrolled = 0; /* avoid wait_return message */ |
| 341 | gotocmdline(TRUE); |
| 342 | msg_scrolled += i; |
| 343 | redrawcmdprompt(); /* draw prompt or indent */ |
| 344 | set_cmdspos(); |
| 345 | } |
| 346 | xpc.xp_context = EXPAND_NOTHING; |
| 347 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 348 | #ifndef BACKSLASH_IN_FILENAME |
| 349 | xpc.xp_shell = FALSE; |
| 350 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 351 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 352 | #if defined(FEAT_EVAL) |
| 353 | if (ccline.input_fn) |
| 354 | { |
| 355 | xpc.xp_context = ccline.xp_context; |
| 356 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 357 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 358 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 359 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 360 | } |
| 361 | #endif |
| 362 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 363 | /* |
| 364 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 365 | * doing ":@0" when register 0 doesn't contain a CR. |
| 366 | */ |
| 367 | msg_scroll = FALSE; |
| 368 | |
| 369 | State = CMDLINE; |
| 370 | |
| 371 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 372 | { |
| 373 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 374 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 375 | b_im_ptr = &curbuf->b_p_iminsert; |
| 376 | else |
| 377 | b_im_ptr = &curbuf->b_p_imsearch; |
| 378 | if (*b_im_ptr == B_IMODE_LMAP) |
| 379 | State |= LANGMAP; |
Bram Moolenaar | 819edbe | 2017-11-25 17:14:33 +0100 | [diff] [blame] | 380 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 381 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 382 | #endif |
| 383 | } |
Bram Moolenaar | 819edbe | 2017-11-25 17:14:33 +0100 | [diff] [blame] | 384 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 385 | else if (p_imcmdline) |
| 386 | im_set_active(TRUE); |
| 387 | #endif |
| 388 | |
| 389 | #ifdef FEAT_MOUSE |
| 390 | setmouse(); |
| 391 | #endif |
| 392 | #ifdef CURSOR_SHAPE |
| 393 | ui_cursor_shape(); /* may show different cursor shape */ |
| 394 | #endif |
| 395 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 396 | /* When inside an autocommand for writing "exiting" may be set and |
| 397 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 398 | settmode(TMODE_RAW); |
| 399 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 400 | #ifdef FEAT_AUTOCMD |
| 401 | /* Trigger CmdlineEnter autocommands. */ |
| 402 | cmdline_type = firstc == NUL ? '-' : firstc; |
| 403 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER); |
| 404 | #endif |
| 405 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 406 | #ifdef FEAT_CMDHIST |
| 407 | init_history(); |
| 408 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 409 | histype = hist_char2type(firstc); |
| 410 | #endif |
| 411 | |
| 412 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 413 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 414 | #endif |
| 415 | |
Bram Moolenaar | 15a35c4 | 2014-06-25 12:26:46 +0200 | [diff] [blame] | 416 | /* If something above caused an error, reset the flags, we do want to type |
| 417 | * and execute commands. Display may be messed up a bit. */ |
| 418 | if (did_emsg) |
| 419 | redrawcmd(); |
| 420 | did_emsg = FALSE; |
| 421 | got_int = FALSE; |
| 422 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 423 | /* |
| 424 | * Collect the command string, handling editing keys. |
| 425 | */ |
| 426 | for (;;) |
| 427 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 428 | redir_off = TRUE; /* Don't redirect the typed command. |
| 429 | Repeated, because a ":redir" inside |
| 430 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 431 | #ifdef USE_ON_FLY_SCROLL |
| 432 | dont_scroll = FALSE; /* allow scrolling here */ |
| 433 | #endif |
| 434 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 435 | |
| 436 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 437 | |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 438 | /* Get a character. Ignore K_IGNORE and K_NOP, they should not do |
| 439 | * anything, such as stop completion. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 440 | do |
| 441 | { |
| 442 | c = safe_vgetc(); |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 443 | } while (c == K_IGNORE || c == K_NOP); |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 444 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 445 | if (KeyTyped) |
| 446 | { |
| 447 | some_key_typed = TRUE; |
| 448 | #ifdef FEAT_RIGHTLEFT |
| 449 | if (cmd_hkmap) |
| 450 | c = hkmap(c); |
| 451 | # ifdef FEAT_FKMAP |
| 452 | if (cmd_fkmap) |
| 453 | c = cmdl_fkmap(c); |
| 454 | # endif |
| 455 | if (cmdmsg_rl && !KeyStuffed) |
| 456 | { |
| 457 | /* Invert horizontal movements and operations. Only when |
| 458 | * typed by the user directly, not when the result of a |
| 459 | * mapping. */ |
| 460 | switch (c) |
| 461 | { |
| 462 | case K_RIGHT: c = K_LEFT; break; |
| 463 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 464 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 465 | case K_LEFT: c = K_RIGHT; break; |
| 466 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 467 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 468 | } |
| 469 | } |
| 470 | #endif |
| 471 | } |
| 472 | |
| 473 | /* |
| 474 | * Ignore got_int when CTRL-C was typed here. |
| 475 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 476 | * ":g/pat/normal /pat" (without the <CR>). |
| 477 | * Don't ignore it for the input() function. |
| 478 | */ |
| 479 | if ((c == Ctrl_C |
| 480 | #ifdef UNIX |
| 481 | || c == intr_char |
| 482 | #endif |
| 483 | ) |
| 484 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 485 | && firstc != '@' |
| 486 | #endif |
| 487 | #ifdef FEAT_EVAL |
| 488 | && !break_ctrl_c |
| 489 | #endif |
| 490 | && !global_busy) |
| 491 | got_int = FALSE; |
| 492 | |
| 493 | #ifdef FEAT_CMDHIST |
| 494 | /* free old command line when finished moving around in the history |
| 495 | * list */ |
| 496 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 497 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 498 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 499 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 500 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 501 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 502 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
| 503 | { |
| 504 | vim_free(lookfor); |
| 505 | lookfor = NULL; |
| 506 | } |
| 507 | #endif |
| 508 | |
| 509 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 510 | * When there are matching completions to select <S-Tab> works like |
| 511 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 512 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 513 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 514 | c = Ctrl_P; |
| 515 | |
| 516 | #ifdef FEAT_WILDMENU |
| 517 | /* Special translations for 'wildmenu' */ |
| 518 | if (did_wild_list && p_wmnu) |
| 519 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 520 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 521 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 522 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 523 | c = Ctrl_N; |
| 524 | } |
| 525 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 526 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 527 | && ccline.cmdpos > 1 |
| 528 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 529 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 530 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 531 | c = K_DOWN; |
| 532 | #endif |
| 533 | |
| 534 | /* free expanded names when finished walking through matches */ |
| 535 | if (xpc.xp_numfiles != -1 |
| 536 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 537 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 538 | && c != Ctrl_L) |
| 539 | { |
| 540 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 541 | did_wild_list = FALSE; |
| 542 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 543 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 544 | #endif |
| 545 | xpc.xp_context = EXPAND_NOTHING; |
| 546 | wim_index = 0; |
| 547 | #ifdef FEAT_WILDMENU |
| 548 | if (p_wmnu && wild_menu_showing != 0) |
| 549 | { |
| 550 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 551 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 552 | |
| 553 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 554 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 555 | |
| 556 | if (wild_menu_showing == WM_SCROLLED) |
| 557 | { |
| 558 | /* Entered command line, move it up */ |
| 559 | cmdline_row--; |
| 560 | redrawcmd(); |
| 561 | } |
| 562 | else if (save_p_ls != -1) |
| 563 | { |
| 564 | /* restore 'laststatus' and 'winminheight' */ |
| 565 | p_ls = save_p_ls; |
| 566 | p_wmh = save_p_wmh; |
| 567 | last_status(FALSE); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 568 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 569 | update_screen(VALID); /* redraw the screen NOW */ |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 570 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 571 | redrawcmd(); |
| 572 | save_p_ls = -1; |
| 573 | } |
| 574 | else |
| 575 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 576 | win_redraw_last_status(topframe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 577 | redraw_statuslines(); |
| 578 | } |
| 579 | KeyTyped = skt; |
| 580 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 581 | if (ccline.input_fn) |
| 582 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 583 | } |
| 584 | #endif |
| 585 | } |
| 586 | |
| 587 | #ifdef FEAT_WILDMENU |
| 588 | /* Special translations for 'wildmenu' */ |
| 589 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 590 | { |
| 591 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 592 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 593 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 594 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 595 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 596 | { |
| 597 | /* Hitting <Up>: Remove one submenu name in front of the |
| 598 | * cursor */ |
| 599 | int found = FALSE; |
| 600 | |
| 601 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 602 | i = 0; |
| 603 | while (--j > 0) |
| 604 | { |
| 605 | /* check for start of menu name */ |
| 606 | if (ccline.cmdbuff[j] == ' ' |
| 607 | && ccline.cmdbuff[j - 1] != '\\') |
| 608 | { |
| 609 | i = j + 1; |
| 610 | break; |
| 611 | } |
| 612 | /* check for start of submenu name */ |
| 613 | if (ccline.cmdbuff[j] == '.' |
| 614 | && ccline.cmdbuff[j - 1] != '\\') |
| 615 | { |
| 616 | if (found) |
| 617 | { |
| 618 | i = j + 1; |
| 619 | break; |
| 620 | } |
| 621 | else |
| 622 | found = TRUE; |
| 623 | } |
| 624 | } |
| 625 | if (i > 0) |
| 626 | cmdline_del(i); |
| 627 | c = p_wc; |
| 628 | xpc.xp_context = EXPAND_NOTHING; |
| 629 | } |
| 630 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 631 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 632 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 633 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 634 | { |
| 635 | char_u upseg[5]; |
| 636 | |
| 637 | upseg[0] = PATHSEP; |
| 638 | upseg[1] = '.'; |
| 639 | upseg[2] = '.'; |
| 640 | upseg[3] = PATHSEP; |
| 641 | upseg[4] = NUL; |
| 642 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 643 | if (c == K_DOWN |
| 644 | && ccline.cmdpos > 0 |
| 645 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 646 | && (ccline.cmdpos < 3 |
| 647 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 648 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 649 | { |
| 650 | /* go down a directory */ |
| 651 | c = p_wc; |
| 652 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 653 | 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] | 654 | { |
| 655 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 656 | int found = FALSE; |
| 657 | |
| 658 | j = ccline.cmdpos; |
| 659 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 660 | while (--j > i) |
| 661 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 662 | #ifdef FEAT_MBYTE |
| 663 | if (has_mbyte) |
| 664 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 665 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 666 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 667 | { |
| 668 | found = TRUE; |
| 669 | break; |
| 670 | } |
| 671 | } |
| 672 | if (found |
| 673 | && ccline.cmdbuff[j - 1] == '.' |
| 674 | && ccline.cmdbuff[j - 2] == '.' |
| 675 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 676 | { |
| 677 | cmdline_del(j - 2); |
| 678 | c = p_wc; |
| 679 | } |
| 680 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 681 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 682 | { |
| 683 | /* go up a directory */ |
| 684 | int found = FALSE; |
| 685 | |
| 686 | j = ccline.cmdpos - 1; |
| 687 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 688 | while (--j > i) |
| 689 | { |
| 690 | #ifdef FEAT_MBYTE |
| 691 | if (has_mbyte) |
| 692 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 693 | #endif |
| 694 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 695 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 696 | && vim_strchr((char_u *)" *?[{`$%#", |
| 697 | ccline.cmdbuff[j + 1]) == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 698 | #endif |
| 699 | ) |
| 700 | { |
| 701 | if (found) |
| 702 | { |
| 703 | i = j + 1; |
| 704 | break; |
| 705 | } |
| 706 | else |
| 707 | found = TRUE; |
| 708 | } |
| 709 | } |
| 710 | |
| 711 | if (!found) |
| 712 | j = i; |
| 713 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 714 | j += 4; |
| 715 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 716 | && j == i) |
| 717 | j += 3; |
| 718 | else |
| 719 | j = 0; |
| 720 | if (j > 0) |
| 721 | { |
| 722 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 723 | * machine-specific stuff here and in upseg init */ |
| 724 | cmdline_del(j); |
| 725 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 726 | } |
| 727 | else if (ccline.cmdpos > i) |
| 728 | cmdline_del(i); |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 729 | |
| 730 | /* Now complete in the new directory. Set KeyTyped in case the |
| 731 | * Up key came from a mapping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 732 | c = p_wc; |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 733 | KeyTyped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 734 | } |
| 735 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 736 | |
| 737 | #endif /* FEAT_WILDMENU */ |
| 738 | |
| 739 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 740 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 741 | if (c == Ctrl_BSL) |
| 742 | { |
| 743 | ++no_mapping; |
| 744 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 745 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 746 | --no_mapping; |
| 747 | --allow_keys; |
Bram Moolenaar | b735681 | 2012-10-11 04:04:37 +0200 | [diff] [blame] | 748 | /* CTRL-\ e doesn't work when obtaining an expression, unless it |
| 749 | * is in a mapping. */ |
| 750 | if (c != Ctrl_N && c != Ctrl_G && (c != 'e' |
| 751 | || (ccline.cmdfirstc == '=' && KeyTyped))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 752 | { |
| 753 | vungetc(c); |
| 754 | c = Ctrl_BSL; |
| 755 | } |
| 756 | #ifdef FEAT_EVAL |
| 757 | else if (c == 'e') |
| 758 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 759 | char_u *p = NULL; |
| 760 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 761 | |
| 762 | /* |
| 763 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 764 | * Need to save and restore the current command line, to be |
| 765 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 766 | */ |
| 767 | if (ccline.cmdpos == ccline.cmdlen) |
| 768 | new_cmdpos = 99999; /* keep it at the end */ |
| 769 | else |
| 770 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 771 | |
| 772 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 773 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 774 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 775 | if (c == '=') |
| 776 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 777 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 778 | * to avoid nasty things like going to another buffer when |
| 779 | * evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 780 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 781 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 782 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 783 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 784 | restore_cmdline(&save_ccline); |
| 785 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 786 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 787 | { |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 788 | len = (int)STRLEN(p); |
| 789 | if (realloc_cmdbuff(len + 1) == OK) |
| 790 | { |
| 791 | ccline.cmdlen = len; |
| 792 | STRCPY(ccline.cmdbuff, p); |
| 793 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 794 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 795 | /* Restore the cursor or use the position set with |
| 796 | * set_cmdline_pos(). */ |
| 797 | if (new_cmdpos > ccline.cmdlen) |
| 798 | ccline.cmdpos = ccline.cmdlen; |
| 799 | else |
| 800 | ccline.cmdpos = new_cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 801 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 802 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 803 | redrawcmd(); |
| 804 | goto cmdline_changed; |
| 805 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 806 | } |
| 807 | } |
| 808 | beep_flush(); |
Bram Moolenaar | 66b4bf8 | 2010-11-16 14:06:08 +0100 | [diff] [blame] | 809 | got_int = FALSE; /* don't abandon the command line */ |
| 810 | did_emsg = FALSE; |
| 811 | emsg_on_display = FALSE; |
| 812 | redrawcmd(); |
| 813 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 814 | } |
| 815 | #endif |
| 816 | else |
| 817 | { |
| 818 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 819 | restart_edit = 'a'; |
| 820 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 821 | in history */ |
| 822 | goto returncmd; /* back to Normal mode */ |
| 823 | } |
| 824 | } |
| 825 | |
| 826 | #ifdef FEAT_CMDWIN |
| 827 | if (c == cedit_key || c == K_CMDWIN) |
| 828 | { |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 829 | if (ex_normal_busy == 0 && got_int == FALSE) |
| 830 | { |
| 831 | /* |
| 832 | * Open a window to edit the command line (and history). |
| 833 | */ |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 834 | c = open_cmdwin(); |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 835 | some_key_typed = TRUE; |
| 836 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 837 | } |
| 838 | # ifdef FEAT_DIGRAPHS |
| 839 | else |
| 840 | # endif |
| 841 | #endif |
| 842 | #ifdef FEAT_DIGRAPHS |
| 843 | c = do_digraph(c); |
| 844 | #endif |
| 845 | |
| 846 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 847 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 848 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 849 | /* In Ex mode a backslash escapes a newline. */ |
| 850 | if (exmode_active |
| 851 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 852 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 853 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 854 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 855 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 856 | if (c == K_KENTER) |
| 857 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 858 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 859 | else |
| 860 | { |
| 861 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 862 | truncate the cmdline now. */ |
| 863 | if (ccheck_abbr(c + ABBR_OFF)) |
| 864 | goto cmdline_changed; |
| 865 | if (!cmd_silent) |
| 866 | { |
| 867 | windgoto(msg_row, 0); |
| 868 | out_flush(); |
| 869 | } |
| 870 | break; |
| 871 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 872 | } |
| 873 | |
| 874 | /* |
| 875 | * Completion for 'wildchar' or 'wildcharm' key. |
| 876 | * - hitting <ESC> twice means: abandon command line. |
| 877 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 878 | * typed, not when it comes from a macro |
| 879 | */ |
| 880 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 881 | { |
| 882 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 883 | { |
| 884 | /* if 'wildmode' contains "list" may still need to list */ |
| 885 | if (xpc.xp_numfiles > 1 |
| 886 | && !did_wild_list |
| 887 | && (wim_flags[wim_index] & WIM_LIST)) |
| 888 | { |
| 889 | (void)showmatches(&xpc, FALSE); |
| 890 | redrawcmd(); |
| 891 | did_wild_list = TRUE; |
| 892 | } |
| 893 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 894 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 895 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 896 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 897 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 898 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 899 | else |
| 900 | res = OK; /* don't insert 'wildchar' now */ |
| 901 | } |
| 902 | else /* typed p_wc first time */ |
| 903 | { |
| 904 | wim_index = 0; |
| 905 | j = ccline.cmdpos; |
| 906 | /* if 'wildmode' first contains "longest", get longest |
| 907 | * common part */ |
| 908 | if (wim_flags[0] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 909 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 910 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 911 | else |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 912 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP, |
| 913 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 914 | |
| 915 | /* if interrupted while completing, behave like it failed */ |
| 916 | if (got_int) |
| 917 | { |
| 918 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 919 | got_int = FALSE; /* don't abandon the command line */ |
| 920 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 921 | #ifdef FEAT_WILDMENU |
| 922 | xpc.xp_context = EXPAND_NOTHING; |
| 923 | #endif |
| 924 | goto cmdline_changed; |
| 925 | } |
| 926 | |
| 927 | /* when more than one match, and 'wildmode' first contains |
| 928 | * "list", or no change and 'wildmode' contains "longest,list", |
| 929 | * list all matches */ |
| 930 | if (res == OK && xpc.xp_numfiles > 1) |
| 931 | { |
| 932 | /* a "longest" that didn't do anything is skipped (but not |
| 933 | * "list:longest") */ |
| 934 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 935 | wim_index = 1; |
| 936 | if ((wim_flags[wim_index] & WIM_LIST) |
| 937 | #ifdef FEAT_WILDMENU |
| 938 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 939 | #endif |
| 940 | ) |
| 941 | { |
| 942 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 943 | { |
| 944 | #ifdef FEAT_WILDMENU |
| 945 | int p_wmnu_save = p_wmnu; |
| 946 | p_wmnu = 0; |
| 947 | #endif |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 948 | /* remove match */ |
| 949 | nextwild(&xpc, WILD_PREV, 0, firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 950 | #ifdef FEAT_WILDMENU |
| 951 | p_wmnu = p_wmnu_save; |
| 952 | #endif |
| 953 | } |
| 954 | #ifdef FEAT_WILDMENU |
| 955 | (void)showmatches(&xpc, p_wmnu |
| 956 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 957 | #else |
| 958 | (void)showmatches(&xpc, FALSE); |
| 959 | #endif |
| 960 | redrawcmd(); |
| 961 | did_wild_list = TRUE; |
| 962 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 963 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 964 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 965 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 966 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 967 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 968 | } |
| 969 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 970 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 971 | } |
| 972 | #ifdef FEAT_WILDMENU |
| 973 | else if (xpc.xp_numfiles == -1) |
| 974 | xpc.xp_context = EXPAND_NOTHING; |
| 975 | #endif |
| 976 | } |
| 977 | if (wim_index < 3) |
| 978 | ++wim_index; |
| 979 | if (c == ESC) |
| 980 | gotesc = TRUE; |
| 981 | if (res == OK) |
| 982 | goto cmdline_changed; |
| 983 | } |
| 984 | |
| 985 | gotesc = FALSE; |
| 986 | |
| 987 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 988 | if (c == K_S_TAB && KeyTyped) |
| 989 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 990 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK |
| 991 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK |
| 992 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 993 | goto cmdline_changed; |
| 994 | } |
| 995 | |
| 996 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 997 | c = NL; |
| 998 | |
| 999 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 1000 | |
| 1001 | /* |
| 1002 | * Big switch for a typed command line character. |
| 1003 | */ |
| 1004 | switch (c) |
| 1005 | { |
| 1006 | case K_BS: |
| 1007 | case Ctrl_H: |
| 1008 | case K_DEL: |
| 1009 | case K_KDEL: |
| 1010 | case Ctrl_W: |
| 1011 | #ifdef FEAT_FKMAP |
| 1012 | if (cmd_fkmap && c == K_BS) |
| 1013 | c = K_DEL; |
| 1014 | #endif |
| 1015 | if (c == K_KDEL) |
| 1016 | c = K_DEL; |
| 1017 | |
| 1018 | /* |
| 1019 | * delete current character is the same as backspace on next |
| 1020 | * character, except at end of line |
| 1021 | */ |
| 1022 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 1023 | ++ccline.cmdpos; |
| 1024 | #ifdef FEAT_MBYTE |
| 1025 | if (has_mbyte && c == K_DEL) |
| 1026 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 1027 | ccline.cmdbuff + ccline.cmdpos); |
| 1028 | #endif |
| 1029 | if (ccline.cmdpos > 0) |
| 1030 | { |
| 1031 | char_u *p; |
| 1032 | |
| 1033 | j = ccline.cmdpos; |
| 1034 | p = ccline.cmdbuff + j; |
| 1035 | #ifdef FEAT_MBYTE |
| 1036 | if (has_mbyte) |
| 1037 | { |
| 1038 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1039 | if (c == Ctrl_W) |
| 1040 | { |
| 1041 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 1042 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1043 | i = mb_get_class(p); |
| 1044 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 1045 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1046 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1047 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1048 | } |
| 1049 | } |
| 1050 | else |
| 1051 | #endif |
| 1052 | if (c == Ctrl_W) |
| 1053 | { |
| 1054 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 1055 | --p; |
| 1056 | i = vim_iswordc(p[-1]); |
| 1057 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 1058 | && vim_iswordc(p[-1]) == i) |
| 1059 | --p; |
| 1060 | } |
| 1061 | else |
| 1062 | --p; |
| 1063 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 1064 | ccline.cmdlen -= j - ccline.cmdpos; |
| 1065 | i = ccline.cmdpos; |
| 1066 | while (i < ccline.cmdlen) |
| 1067 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1068 | |
| 1069 | /* Truncate at the end, required for multi-byte chars. */ |
| 1070 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1071 | #ifdef FEAT_SEARCH_EXTRA |
| 1072 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1073 | { |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1074 | search_start = save_cursor; |
| 1075 | /* save view settings, so that the screen |
| 1076 | * won't be restored at the wrong position */ |
| 1077 | old_curswant = init_curswant; |
| 1078 | old_leftcol = init_leftcol; |
| 1079 | old_topline = init_topline; |
| 1080 | # ifdef FEAT_DIFF |
| 1081 | old_topfill = init_topfill; |
| 1082 | # endif |
| 1083 | old_botline = init_botline; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1084 | } |
| 1085 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1086 | redrawcmd(); |
| 1087 | } |
| 1088 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 1089 | && ccline.cmdprompt == NULL && indent == 0) |
| 1090 | { |
| 1091 | /* In ex and debug mode it doesn't make sense to return. */ |
| 1092 | if (exmode_active |
| 1093 | #ifdef FEAT_EVAL |
| 1094 | || ccline.cmdfirstc == '>' |
| 1095 | #endif |
| 1096 | ) |
| 1097 | goto cmdline_not_changed; |
| 1098 | |
| 1099 | vim_free(ccline.cmdbuff); /* no commandline to return */ |
| 1100 | ccline.cmdbuff = NULL; |
| 1101 | if (!cmd_silent) |
| 1102 | { |
| 1103 | #ifdef FEAT_RIGHTLEFT |
| 1104 | if (cmdmsg_rl) |
| 1105 | msg_col = Columns; |
| 1106 | else |
| 1107 | #endif |
| 1108 | msg_col = 0; |
| 1109 | msg_putchar(' '); /* delete ':' */ |
| 1110 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1111 | #ifdef FEAT_SEARCH_EXTRA |
| 1112 | if (ccline.cmdlen == 0) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1113 | search_start = save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1114 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1115 | redraw_cmdline = TRUE; |
| 1116 | goto returncmd; /* back to cmd mode */ |
| 1117 | } |
| 1118 | goto cmdline_changed; |
| 1119 | |
| 1120 | case K_INS: |
| 1121 | case K_KINS: |
| 1122 | #ifdef FEAT_FKMAP |
| 1123 | /* if Farsi mode set, we are in reverse insert mode - |
| 1124 | Do not change the mode */ |
| 1125 | if (cmd_fkmap) |
| 1126 | beep_flush(); |
| 1127 | else |
| 1128 | #endif |
| 1129 | ccline.overstrike = !ccline.overstrike; |
| 1130 | #ifdef CURSOR_SHAPE |
| 1131 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1132 | #endif |
| 1133 | goto cmdline_not_changed; |
| 1134 | |
| 1135 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1136 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1137 | { |
| 1138 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1139 | State ^= LANGMAP; |
Bram Moolenaar | 819edbe | 2017-11-25 17:14:33 +0100 | [diff] [blame] | 1140 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1141 | im_set_active(FALSE); /* Disable input method */ |
| 1142 | #endif |
| 1143 | if (b_im_ptr != NULL) |
| 1144 | { |
| 1145 | if (State & LANGMAP) |
| 1146 | *b_im_ptr = B_IMODE_LMAP; |
| 1147 | else |
| 1148 | *b_im_ptr = B_IMODE_NONE; |
| 1149 | } |
| 1150 | } |
Bram Moolenaar | 819edbe | 2017-11-25 17:14:33 +0100 | [diff] [blame] | 1151 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1152 | else |
| 1153 | { |
| 1154 | /* There are no ":lmap" mappings, toggle IM. When |
| 1155 | * 'imdisable' is set don't try getting the status, it's |
| 1156 | * always off. */ |
| 1157 | if ((p_imdisable && b_im_ptr != NULL) |
| 1158 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1159 | { |
| 1160 | im_set_active(FALSE); /* Disable input method */ |
| 1161 | if (b_im_ptr != NULL) |
| 1162 | *b_im_ptr = B_IMODE_NONE; |
| 1163 | } |
| 1164 | else |
| 1165 | { |
| 1166 | im_set_active(TRUE); /* Enable input method */ |
| 1167 | if (b_im_ptr != NULL) |
| 1168 | *b_im_ptr = B_IMODE_IM; |
| 1169 | } |
| 1170 | } |
| 1171 | #endif |
| 1172 | if (b_im_ptr != NULL) |
| 1173 | { |
| 1174 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1175 | set_iminsert_global(); |
| 1176 | else |
| 1177 | set_imsearch_global(); |
| 1178 | } |
| 1179 | #ifdef CURSOR_SHAPE |
| 1180 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1181 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 1182 | #if defined(FEAT_KEYMAP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1183 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1184 | status_redraw_curbuf(); |
| 1185 | #endif |
| 1186 | goto cmdline_not_changed; |
| 1187 | |
| 1188 | /* case '@': only in very old vi */ |
| 1189 | case Ctrl_U: |
| 1190 | /* delete all characters left of the cursor */ |
| 1191 | j = ccline.cmdpos; |
| 1192 | ccline.cmdlen -= j; |
| 1193 | i = ccline.cmdpos = 0; |
| 1194 | while (i < ccline.cmdlen) |
| 1195 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1196 | /* Truncate at the end, required for multi-byte chars. */ |
| 1197 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1198 | #ifdef FEAT_SEARCH_EXTRA |
| 1199 | if (ccline.cmdlen == 0) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1200 | search_start = save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1201 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1202 | redrawcmd(); |
| 1203 | goto cmdline_changed; |
| 1204 | |
| 1205 | #ifdef FEAT_CLIPBOARD |
| 1206 | case Ctrl_Y: |
| 1207 | /* Copy the modeless selection, if there is one. */ |
| 1208 | if (clip_star.state != SELECT_CLEARED) |
| 1209 | { |
| 1210 | if (clip_star.state == SELECT_DONE) |
| 1211 | clip_copy_modeless_selection(TRUE); |
| 1212 | goto cmdline_not_changed; |
| 1213 | } |
| 1214 | break; |
| 1215 | #endif |
| 1216 | |
| 1217 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1218 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1219 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1220 | * ":normal" runs out of characters. */ |
| 1221 | if (exmode_active |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 1222 | && (ex_normal_busy == 0 || typebuf.tb_len > 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1223 | goto cmdline_not_changed; |
| 1224 | |
| 1225 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1226 | putting it in history */ |
| 1227 | goto returncmd; /* back to cmd mode */ |
| 1228 | |
| 1229 | case Ctrl_R: /* insert register */ |
| 1230 | #ifdef USE_ON_FLY_SCROLL |
| 1231 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1232 | #endif |
| 1233 | putcmdline('"', TRUE); |
| 1234 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1235 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1236 | if (i == Ctrl_O) |
| 1237 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1238 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1239 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1240 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1241 | --no_mapping; |
| 1242 | #ifdef FEAT_EVAL |
| 1243 | /* |
| 1244 | * Insert the result of an expression. |
| 1245 | * Need to save the current command line, to be able to enter |
| 1246 | * a new one... |
| 1247 | */ |
| 1248 | new_cmdpos = -1; |
| 1249 | if (c == '=') |
| 1250 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1251 | if (ccline.cmdfirstc == '=')/* can't do this recursively */ |
| 1252 | { |
| 1253 | beep_flush(); |
| 1254 | c = ESC; |
| 1255 | } |
| 1256 | else |
| 1257 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1258 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1259 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1260 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1261 | } |
| 1262 | } |
| 1263 | #endif |
| 1264 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1265 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1266 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1267 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1268 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1269 | /* When there was a serious error abort getting the |
| 1270 | * command line. */ |
| 1271 | if (aborting()) |
| 1272 | { |
| 1273 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1274 | putting it in history */ |
| 1275 | goto returncmd; /* back to cmd mode */ |
| 1276 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1277 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1278 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1279 | #ifdef FEAT_EVAL |
| 1280 | if (new_cmdpos >= 0) |
| 1281 | { |
| 1282 | /* set_cmdline_pos() was used */ |
| 1283 | if (new_cmdpos > ccline.cmdlen) |
| 1284 | ccline.cmdpos = ccline.cmdlen; |
| 1285 | else |
| 1286 | ccline.cmdpos = new_cmdpos; |
| 1287 | } |
| 1288 | #endif |
| 1289 | } |
| 1290 | redrawcmd(); |
| 1291 | goto cmdline_changed; |
| 1292 | |
| 1293 | case Ctrl_D: |
| 1294 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1295 | break; /* Use ^D as normal char instead */ |
| 1296 | |
| 1297 | redrawcmd(); |
| 1298 | continue; /* don't do incremental search now */ |
| 1299 | |
| 1300 | case K_RIGHT: |
| 1301 | case K_S_RIGHT: |
| 1302 | case K_C_RIGHT: |
| 1303 | do |
| 1304 | { |
| 1305 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1306 | break; |
| 1307 | i = cmdline_charsize(ccline.cmdpos); |
| 1308 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1309 | break; |
| 1310 | ccline.cmdspos += i; |
| 1311 | #ifdef FEAT_MBYTE |
| 1312 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1313 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1314 | + ccline.cmdpos); |
| 1315 | else |
| 1316 | #endif |
| 1317 | ++ccline.cmdpos; |
| 1318 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1319 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 1320 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1321 | && ccline.cmdbuff[ccline.cmdpos] != ' '); |
| 1322 | #ifdef FEAT_MBYTE |
| 1323 | if (has_mbyte) |
| 1324 | set_cmdspos_cursor(); |
| 1325 | #endif |
| 1326 | goto cmdline_not_changed; |
| 1327 | |
| 1328 | case K_LEFT: |
| 1329 | case K_S_LEFT: |
| 1330 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1331 | if (ccline.cmdpos == 0) |
| 1332 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1333 | do |
| 1334 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1335 | --ccline.cmdpos; |
| 1336 | #ifdef FEAT_MBYTE |
| 1337 | if (has_mbyte) /* move to first byte of char */ |
| 1338 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1339 | ccline.cmdbuff + ccline.cmdpos); |
| 1340 | #endif |
| 1341 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1342 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1343 | while (ccline.cmdpos > 0 |
| 1344 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1345 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1346 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1347 | #ifdef FEAT_MBYTE |
| 1348 | if (has_mbyte) |
| 1349 | set_cmdspos_cursor(); |
| 1350 | #endif |
| 1351 | goto cmdline_not_changed; |
| 1352 | |
| 1353 | case K_IGNORE: |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1354 | /* Ignore mouse event or open_cmdwin() result. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1355 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1356 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1357 | #ifdef FEAT_GUI_W32 |
| 1358 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1359 | * cancelled. */ |
| 1360 | case K_F4: |
| 1361 | if (mod_mask == MOD_MASK_ALT) |
| 1362 | { |
| 1363 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1364 | goto cmdline_not_changed; |
| 1365 | } |
| 1366 | break; |
| 1367 | #endif |
| 1368 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1369 | #ifdef FEAT_MOUSE |
| 1370 | case K_MIDDLEDRAG: |
| 1371 | case K_MIDDLERELEASE: |
| 1372 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1373 | |
| 1374 | case K_MIDDLEMOUSE: |
| 1375 | # ifdef FEAT_GUI |
| 1376 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1377 | if (!gui.in_use) |
| 1378 | # endif |
| 1379 | if (!mouse_has(MOUSE_COMMAND)) |
| 1380 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1381 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1382 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1383 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1384 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1385 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1386 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1387 | redrawcmd(); |
| 1388 | goto cmdline_changed; |
| 1389 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1390 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1391 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1392 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1393 | redrawcmd(); |
| 1394 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1395 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1396 | |
| 1397 | case K_LEFTDRAG: |
| 1398 | case K_LEFTRELEASE: |
| 1399 | case K_RIGHTDRAG: |
| 1400 | case K_RIGHTRELEASE: |
| 1401 | /* Ignore drag and release events when the button-down wasn't |
| 1402 | * seen before. */ |
| 1403 | if (ignore_drag_release) |
| 1404 | goto cmdline_not_changed; |
| 1405 | /* FALLTHROUGH */ |
| 1406 | case K_LEFTMOUSE: |
| 1407 | case K_RIGHTMOUSE: |
| 1408 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1409 | ignore_drag_release = TRUE; |
| 1410 | else |
| 1411 | ignore_drag_release = FALSE; |
| 1412 | # ifdef FEAT_GUI |
| 1413 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1414 | if (!gui.in_use) |
| 1415 | # endif |
| 1416 | if (!mouse_has(MOUSE_COMMAND)) |
| 1417 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1418 | # ifdef FEAT_CLIPBOARD |
| 1419 | if (mouse_row < cmdline_row && clip_star.available) |
| 1420 | { |
| 1421 | int button, is_click, is_drag; |
| 1422 | |
| 1423 | /* |
| 1424 | * Handle modeless selection. |
| 1425 | */ |
| 1426 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1427 | &is_click, &is_drag); |
| 1428 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1429 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1430 | { |
| 1431 | /* Translate shift-left to right button. */ |
| 1432 | button = MOUSE_RIGHT; |
| 1433 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1434 | } |
| 1435 | clip_modeless(button, is_click, is_drag); |
| 1436 | goto cmdline_not_changed; |
| 1437 | } |
| 1438 | # endif |
| 1439 | |
| 1440 | set_cmdspos(); |
| 1441 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 1442 | ++ccline.cmdpos) |
| 1443 | { |
| 1444 | i = cmdline_charsize(ccline.cmdpos); |
| 1445 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 1446 | && mouse_col < ccline.cmdspos % Columns + i) |
| 1447 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1448 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1449 | if (has_mbyte) |
| 1450 | { |
| 1451 | /* Count ">" for double-wide char that doesn't fit. */ |
| 1452 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1453 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1454 | + ccline.cmdpos) - 1; |
| 1455 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1456 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1457 | ccline.cmdspos += i; |
| 1458 | } |
| 1459 | goto cmdline_not_changed; |
| 1460 | |
| 1461 | /* Mouse scroll wheel: ignored here */ |
| 1462 | case K_MOUSEDOWN: |
| 1463 | case K_MOUSEUP: |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1464 | case K_MOUSELEFT: |
| 1465 | case K_MOUSERIGHT: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1466 | /* Alternate buttons ignored here */ |
| 1467 | case K_X1MOUSE: |
| 1468 | case K_X1DRAG: |
| 1469 | case K_X1RELEASE: |
| 1470 | case K_X2MOUSE: |
| 1471 | case K_X2DRAG: |
| 1472 | case K_X2RELEASE: |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1473 | case K_MOUSEMOVE: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1474 | goto cmdline_not_changed; |
| 1475 | |
| 1476 | #endif /* FEAT_MOUSE */ |
| 1477 | |
| 1478 | #ifdef FEAT_GUI |
| 1479 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 1480 | case K_LEFTRELEASE_NM: |
| 1481 | goto cmdline_not_changed; |
| 1482 | |
| 1483 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1484 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1485 | { |
| 1486 | gui_do_scroll(); |
| 1487 | redrawcmd(); |
| 1488 | } |
| 1489 | goto cmdline_not_changed; |
| 1490 | |
| 1491 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1492 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1493 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1494 | gui_do_horiz_scroll(scrollbar_value, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1495 | redrawcmd(); |
| 1496 | } |
| 1497 | goto cmdline_not_changed; |
| 1498 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1499 | #ifdef FEAT_GUI_TABLINE |
| 1500 | case K_TABLINE: |
| 1501 | case K_TABMENU: |
| 1502 | /* Don't want to change any tabs here. Make sure the same tab |
| 1503 | * is still selected. */ |
| 1504 | if (gui_use_tabline()) |
| 1505 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 1506 | goto cmdline_not_changed; |
| 1507 | #endif |
| 1508 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1509 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1510 | goto cmdline_not_changed; |
| 1511 | |
| 1512 | case Ctrl_B: /* begin of command line */ |
| 1513 | case K_HOME: |
| 1514 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1515 | case K_S_HOME: |
| 1516 | case K_C_HOME: |
| 1517 | ccline.cmdpos = 0; |
| 1518 | set_cmdspos(); |
| 1519 | goto cmdline_not_changed; |
| 1520 | |
| 1521 | case Ctrl_E: /* end of command line */ |
| 1522 | case K_END: |
| 1523 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1524 | case K_S_END: |
| 1525 | case K_C_END: |
| 1526 | ccline.cmdpos = ccline.cmdlen; |
| 1527 | set_cmdspos_cursor(); |
| 1528 | goto cmdline_not_changed; |
| 1529 | |
| 1530 | case Ctrl_A: /* all matches */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1531 | if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1532 | break; |
| 1533 | goto cmdline_changed; |
| 1534 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1535 | case Ctrl_L: |
| 1536 | #ifdef FEAT_SEARCH_EXTRA |
| 1537 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1538 | { |
| 1539 | /* Add a character from under the cursor for 'incsearch' */ |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1540 | if (did_incsearch) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1541 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1542 | curwin->w_cursor = match_end; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1543 | if (!EQUAL_POS(curwin->w_cursor, search_start)) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1544 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1545 | c = gchar_cursor(); |
| 1546 | /* If 'ignorecase' and 'smartcase' are set and the |
| 1547 | * command line has no uppercase characters, convert |
| 1548 | * the character to lowercase */ |
| 1549 | if (p_ic && p_scs |
| 1550 | && !pat_has_uppercase(ccline.cmdbuff)) |
| 1551 | c = MB_TOLOWER(c); |
| 1552 | if (c != NUL) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1553 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1554 | if (c == firstc || vim_strchr((char_u *)( |
Bram Moolenaar | a693d05 | 2017-06-29 22:23:06 +0200 | [diff] [blame] | 1555 | p_magic ? "\\~^$.*[" : "\\^$"), c) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1556 | != NULL) |
| 1557 | { |
| 1558 | /* put a backslash before special |
| 1559 | * characters */ |
| 1560 | stuffcharReadbuff(c); |
| 1561 | c = '\\'; |
| 1562 | } |
| 1563 | break; |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1564 | } |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1565 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1566 | } |
| 1567 | goto cmdline_not_changed; |
| 1568 | } |
| 1569 | #endif |
| 1570 | |
| 1571 | /* completion: longest common part */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1572 | if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1573 | break; |
| 1574 | goto cmdline_changed; |
| 1575 | |
| 1576 | case Ctrl_N: /* next match */ |
| 1577 | case Ctrl_P: /* previous match */ |
Bram Moolenaar | 7df0f63 | 2016-08-26 19:56:00 +0200 | [diff] [blame] | 1578 | if (xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1579 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1580 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, |
| 1581 | 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1582 | break; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1583 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1584 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1585 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | 2f40d12 | 2017-10-24 21:49:36 +0200 | [diff] [blame] | 1586 | /* FALLTHROUGH */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1587 | case K_UP: |
| 1588 | case K_DOWN: |
| 1589 | case K_S_UP: |
| 1590 | case K_S_DOWN: |
| 1591 | case K_PAGEUP: |
| 1592 | case K_KPAGEUP: |
| 1593 | case K_PAGEDOWN: |
| 1594 | case K_KPAGEDOWN: |
| 1595 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 1596 | goto cmdline_not_changed; |
| 1597 | |
| 1598 | i = hiscnt; |
| 1599 | |
| 1600 | /* save current command string so it can be restored later */ |
| 1601 | if (lookfor == NULL) |
| 1602 | { |
| 1603 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 1604 | goto cmdline_not_changed; |
| 1605 | lookfor[ccline.cmdpos] = NUL; |
| 1606 | } |
| 1607 | |
| 1608 | j = (int)STRLEN(lookfor); |
| 1609 | for (;;) |
| 1610 | { |
| 1611 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1612 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1613 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1614 | { |
| 1615 | if (hiscnt == hislen) /* first time */ |
| 1616 | hiscnt = hisidx[histype]; |
| 1617 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 1618 | hiscnt = hislen - 1; |
| 1619 | else if (hiscnt != hisidx[histype] + 1) |
| 1620 | --hiscnt; |
| 1621 | else /* at top of list */ |
| 1622 | { |
| 1623 | hiscnt = i; |
| 1624 | break; |
| 1625 | } |
| 1626 | } |
| 1627 | else /* one step forwards */ |
| 1628 | { |
| 1629 | /* on last entry, clear the line */ |
| 1630 | if (hiscnt == hisidx[histype]) |
| 1631 | { |
| 1632 | hiscnt = hislen; |
| 1633 | break; |
| 1634 | } |
| 1635 | |
| 1636 | /* not on a history line, nothing to do */ |
| 1637 | if (hiscnt == hislen) |
| 1638 | break; |
| 1639 | if (hiscnt == hislen - 1) /* wrap around */ |
| 1640 | hiscnt = 0; |
| 1641 | else |
| 1642 | ++hiscnt; |
| 1643 | } |
| 1644 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 1645 | { |
| 1646 | hiscnt = i; |
| 1647 | break; |
| 1648 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1649 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1650 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1651 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 1652 | lookfor, (size_t)j) == 0) |
| 1653 | break; |
| 1654 | } |
| 1655 | |
| 1656 | if (hiscnt != i) /* jumped to other entry */ |
| 1657 | { |
| 1658 | char_u *p; |
| 1659 | int len; |
| 1660 | int old_firstc; |
| 1661 | |
| 1662 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1663 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1664 | if (hiscnt == hislen) |
| 1665 | p = lookfor; /* back to the old one */ |
| 1666 | else |
| 1667 | p = history[histype][hiscnt].hisstr; |
| 1668 | |
| 1669 | if (histype == HIST_SEARCH |
| 1670 | && p != lookfor |
| 1671 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 1672 | { |
| 1673 | /* Correct for the separator character used when |
| 1674 | * adding the history entry vs the one used now. |
| 1675 | * First loop: count length. |
| 1676 | * Second loop: copy the characters. */ |
| 1677 | for (i = 0; i <= 1; ++i) |
| 1678 | { |
| 1679 | len = 0; |
| 1680 | for (j = 0; p[j] != NUL; ++j) |
| 1681 | { |
| 1682 | /* Replace old sep with new sep, unless it is |
| 1683 | * escaped. */ |
| 1684 | if (p[j] == old_firstc |
| 1685 | && (j == 0 || p[j - 1] != '\\')) |
| 1686 | { |
| 1687 | if (i > 0) |
| 1688 | ccline.cmdbuff[len] = firstc; |
| 1689 | } |
| 1690 | else |
| 1691 | { |
| 1692 | /* Escape new sep, unless it is already |
| 1693 | * escaped. */ |
| 1694 | if (p[j] == firstc |
| 1695 | && (j == 0 || p[j - 1] != '\\')) |
| 1696 | { |
| 1697 | if (i > 0) |
| 1698 | ccline.cmdbuff[len] = '\\'; |
| 1699 | ++len; |
| 1700 | } |
| 1701 | if (i > 0) |
| 1702 | ccline.cmdbuff[len] = p[j]; |
| 1703 | } |
| 1704 | ++len; |
| 1705 | } |
| 1706 | if (i == 0) |
| 1707 | { |
| 1708 | alloc_cmdbuff(len); |
| 1709 | if (ccline.cmdbuff == NULL) |
| 1710 | goto returncmd; |
| 1711 | } |
| 1712 | } |
| 1713 | ccline.cmdbuff[len] = NUL; |
| 1714 | } |
| 1715 | else |
| 1716 | { |
| 1717 | alloc_cmdbuff((int)STRLEN(p)); |
| 1718 | if (ccline.cmdbuff == NULL) |
| 1719 | goto returncmd; |
| 1720 | STRCPY(ccline.cmdbuff, p); |
| 1721 | } |
| 1722 | |
| 1723 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 1724 | redrawcmd(); |
| 1725 | goto cmdline_changed; |
| 1726 | } |
| 1727 | beep_flush(); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1728 | #endif |
| 1729 | goto cmdline_not_changed; |
| 1730 | |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1731 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1732 | case Ctrl_G: /* next match */ |
| 1733 | case Ctrl_T: /* previous match */ |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1734 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1735 | { |
| 1736 | pos_T t; |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 1737 | char_u *pat; |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1738 | int search_flags = SEARCH_NOOF; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1739 | |
Bram Moolenaar | f8f8b2e | 2017-11-02 19:08:48 +0100 | [diff] [blame] | 1740 | if (ccline.cmdlen == 0) |
| 1741 | goto cmdline_not_changed; |
| 1742 | |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 1743 | if (firstc == ccline.cmdbuff[0]) |
| 1744 | pat = last_search_pattern(); |
| 1745 | else |
| 1746 | pat = ccline.cmdbuff; |
| 1747 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1748 | save_last_search_pattern(); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1749 | cursor_off(); |
| 1750 | out_flush(); |
| 1751 | if (c == Ctrl_G) |
| 1752 | { |
| 1753 | t = match_end; |
Bram Moolenaar | f8f8b2e | 2017-11-02 19:08:48 +0100 | [diff] [blame] | 1754 | if (LT_POS(match_start, match_end)) |
| 1755 | /* start searching at the end of the match |
| 1756 | * not at the beginning of the next column */ |
| 1757 | (void)decl(&t); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1758 | search_flags += SEARCH_COL; |
| 1759 | } |
| 1760 | else |
| 1761 | t = match_start; |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1762 | if (!p_hls) |
| 1763 | search_flags += SEARCH_KEEP; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1764 | ++emsg_off; |
| 1765 | i = searchit(curwin, curbuf, &t, |
| 1766 | c == Ctrl_G ? FORWARD : BACKWARD, |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 1767 | pat, count, search_flags, |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1768 | RE_SEARCH, 0, NULL, NULL); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1769 | --emsg_off; |
| 1770 | if (i) |
| 1771 | { |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1772 | search_start = match_start; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1773 | match_end = t; |
| 1774 | match_start = t; |
| 1775 | if (c == Ctrl_T && firstc == '/') |
| 1776 | { |
| 1777 | /* move just before the current match, so that |
| 1778 | * when nv_search finishes the cursor will be |
| 1779 | * put back on the match */ |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1780 | search_start = t; |
| 1781 | (void)decl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1782 | } |
Bram Moolenaar | da5116d | 2017-07-01 23:11:17 +0200 | [diff] [blame] | 1783 | else if (c == Ctrl_G && firstc == '?') |
| 1784 | { |
| 1785 | /* move just after the current match, so that |
| 1786 | * when nv_search finishes the cursor will be |
| 1787 | * put back on the match */ |
| 1788 | search_start = t; |
| 1789 | (void)incl(&search_start); |
| 1790 | } |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1791 | if (LT_POS(t, search_start) && c == Ctrl_G) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1792 | { |
| 1793 | /* wrap around */ |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1794 | search_start = t; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1795 | if (firstc == '?') |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1796 | (void)incl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1797 | else |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1798 | (void)decl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1799 | } |
| 1800 | |
| 1801 | set_search_match(&match_end); |
| 1802 | curwin->w_cursor = match_start; |
| 1803 | changed_cline_bef_curs(); |
| 1804 | update_topline(); |
| 1805 | validate_cursor(); |
| 1806 | highlight_match = TRUE; |
| 1807 | old_curswant = curwin->w_curswant; |
| 1808 | old_leftcol = curwin->w_leftcol; |
| 1809 | old_topline = curwin->w_topline; |
| 1810 | # ifdef FEAT_DIFF |
| 1811 | old_topfill = curwin->w_topfill; |
| 1812 | # endif |
| 1813 | old_botline = curwin->w_botline; |
| 1814 | update_screen(NOT_VALID); |
| 1815 | redrawcmdline(); |
| 1816 | } |
| 1817 | else |
| 1818 | vim_beep(BO_ERROR); |
Bram Moolenaar | a1d5c15 | 2017-12-16 19:05:22 +0100 | [diff] [blame] | 1819 | restore_last_search_pattern(); |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1820 | goto cmdline_not_changed; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1821 | } |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1822 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1823 | #endif |
| 1824 | |
| 1825 | case Ctrl_V: |
| 1826 | case Ctrl_Q: |
| 1827 | #ifdef FEAT_MOUSE |
| 1828 | ignore_drag_release = TRUE; |
| 1829 | #endif |
| 1830 | putcmdline('^', TRUE); |
| 1831 | c = get_literal(); /* get next (two) character(s) */ |
| 1832 | do_abbr = FALSE; /* don't do abbreviation now */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1833 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1834 | #ifdef FEAT_MBYTE |
| 1835 | /* may need to remove ^ when composing char was typed */ |
| 1836 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 1837 | { |
| 1838 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 1839 | msg_putchar(' '); |
| 1840 | cursorcmd(); |
| 1841 | } |
| 1842 | #endif |
| 1843 | break; |
| 1844 | |
| 1845 | #ifdef FEAT_DIGRAPHS |
| 1846 | case Ctrl_K: |
| 1847 | #ifdef FEAT_MOUSE |
| 1848 | ignore_drag_release = TRUE; |
| 1849 | #endif |
| 1850 | putcmdline('?', TRUE); |
| 1851 | #ifdef USE_ON_FLY_SCROLL |
| 1852 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1853 | #endif |
| 1854 | c = get_digraph(TRUE); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1855 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1856 | if (c != NUL) |
| 1857 | break; |
| 1858 | |
| 1859 | redrawcmd(); |
| 1860 | goto cmdline_not_changed; |
| 1861 | #endif /* FEAT_DIGRAPHS */ |
| 1862 | |
| 1863 | #ifdef FEAT_RIGHTLEFT |
| 1864 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 1865 | if (!p_ari) |
| 1866 | break; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1867 | # ifdef FEAT_FKMAP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1868 | if (p_altkeymap) |
| 1869 | { |
| 1870 | cmd_fkmap = !cmd_fkmap; |
| 1871 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 1872 | ccline.overstrike = FALSE; |
| 1873 | } |
| 1874 | else /* Hebrew is default */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1875 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1876 | cmd_hkmap = !cmd_hkmap; |
| 1877 | goto cmdline_not_changed; |
| 1878 | #endif |
| 1879 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 1880 | case K_PS: |
| 1881 | bracketed_paste(PASTE_CMDLINE, FALSE, NULL); |
| 1882 | goto cmdline_changed; |
| 1883 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1884 | default: |
| 1885 | #ifdef UNIX |
| 1886 | if (c == intr_char) |
| 1887 | { |
| 1888 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1889 | putting it in history */ |
| 1890 | goto returncmd; /* back to Normal mode */ |
| 1891 | } |
| 1892 | #endif |
| 1893 | /* |
| 1894 | * Normal character with no special meaning. Just set mod_mask |
| 1895 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 1896 | * the string <S-Space>. This should only happen after ^V. |
| 1897 | */ |
| 1898 | if (!IS_SPECIAL(c)) |
| 1899 | mod_mask = 0x0; |
| 1900 | break; |
| 1901 | } |
| 1902 | /* |
| 1903 | * End of switch on command line character. |
| 1904 | * We come here if we have a normal character. |
| 1905 | */ |
| 1906 | |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1907 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1908 | #ifdef FEAT_MBYTE |
| 1909 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1910 | * what check_abbr() expects. */ |
| 1911 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1912 | #endif |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1913 | c) || c == Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1914 | goto cmdline_changed; |
| 1915 | |
| 1916 | /* |
| 1917 | * put the character in the command line |
| 1918 | */ |
| 1919 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 1920 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 1921 | else |
| 1922 | { |
| 1923 | #ifdef FEAT_MBYTE |
| 1924 | if (has_mbyte) |
| 1925 | { |
| 1926 | j = (*mb_char2bytes)(c, IObuff); |
| 1927 | IObuff[j] = NUL; /* exclude composing chars */ |
| 1928 | put_on_cmdline(IObuff, j, TRUE); |
| 1929 | } |
| 1930 | else |
| 1931 | #endif |
| 1932 | { |
| 1933 | IObuff[0] = c; |
| 1934 | put_on_cmdline(IObuff, 1, TRUE); |
| 1935 | } |
| 1936 | } |
| 1937 | goto cmdline_changed; |
| 1938 | |
| 1939 | /* |
| 1940 | * This part implements incremental searches for "/" and "?" |
| 1941 | * Jump to cmdline_not_changed when a character has been read but the command |
| 1942 | * line did not change. Then we only search and redraw if something changed in |
| 1943 | * the past. |
| 1944 | * Jump to cmdline_changed when the command line did change. |
| 1945 | * (Sorry for the goto's, I know it is ugly). |
| 1946 | */ |
| 1947 | cmdline_not_changed: |
| 1948 | #ifdef FEAT_SEARCH_EXTRA |
| 1949 | if (!incsearch_postponed) |
| 1950 | continue; |
| 1951 | #endif |
| 1952 | |
| 1953 | cmdline_changed: |
| 1954 | #ifdef FEAT_SEARCH_EXTRA |
| 1955 | /* |
| 1956 | * 'incsearch' highlighting. |
| 1957 | */ |
| 1958 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1959 | { |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1960 | pos_T end_pos; |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1961 | #ifdef FEAT_RELTIME |
| 1962 | proftime_T tm; |
| 1963 | #endif |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1964 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1965 | /* if there is a character waiting, search and redraw later */ |
| 1966 | if (char_avail()) |
| 1967 | { |
| 1968 | incsearch_postponed = TRUE; |
| 1969 | continue; |
| 1970 | } |
| 1971 | incsearch_postponed = FALSE; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1972 | curwin->w_cursor = search_start; /* start at old position */ |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1973 | save_last_search_pattern(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1974 | |
| 1975 | /* If there is no command line, don't do anything */ |
| 1976 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1977 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1978 | i = 0; |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1979 | SET_NO_HLSEARCH(TRUE); /* turn off previous highlight */ |
Bram Moolenaar | f8f8b2e | 2017-11-02 19:08:48 +0100 | [diff] [blame] | 1980 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1981 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1982 | else |
| 1983 | { |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1984 | int search_flags = SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1985 | cursor_off(); /* so the user knows we're busy */ |
| 1986 | out_flush(); |
| 1987 | ++emsg_off; /* So it doesn't beep if bad expr */ |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1988 | #ifdef FEAT_RELTIME |
| 1989 | /* Set the time limit to half a second. */ |
| 1990 | profile_setlimit(500L, &tm); |
| 1991 | #endif |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1992 | if (!p_hls) |
| 1993 | search_flags += SEARCH_KEEP; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1994 | i = do_search(NULL, firstc, ccline.cmdbuff, count, |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1995 | search_flags, |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1996 | #ifdef FEAT_RELTIME |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1997 | &tm, NULL |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1998 | #else |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1999 | NULL, NULL |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 2000 | #endif |
| 2001 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2002 | --emsg_off; |
| 2003 | /* if interrupted while searching, behave like it failed */ |
| 2004 | if (got_int) |
| 2005 | { |
| 2006 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 2007 | got_int = FALSE; /* don't abandon the command line */ |
| 2008 | i = 0; |
| 2009 | } |
| 2010 | else if (char_avail()) |
| 2011 | /* cancelled searching because a char was typed */ |
| 2012 | incsearch_postponed = TRUE; |
| 2013 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2014 | if (i != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2015 | highlight_match = TRUE; /* highlight position */ |
| 2016 | else |
| 2017 | highlight_match = FALSE; /* remove highlight */ |
| 2018 | |
| 2019 | /* first restore the old curwin values, so the screen is |
| 2020 | * positioned in the same way as the actual search command */ |
| 2021 | curwin->w_leftcol = old_leftcol; |
| 2022 | curwin->w_topline = old_topline; |
| 2023 | # ifdef FEAT_DIFF |
| 2024 | curwin->w_topfill = old_topfill; |
| 2025 | # endif |
| 2026 | curwin->w_botline = old_botline; |
| 2027 | changed_cline_bef_curs(); |
| 2028 | update_topline(); |
| 2029 | |
| 2030 | if (i != 0) |
| 2031 | { |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 2032 | pos_T save_pos = curwin->w_cursor; |
| 2033 | |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 2034 | match_start = curwin->w_cursor; |
| 2035 | set_search_match(&curwin->w_cursor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2036 | validate_cursor(); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2037 | end_pos = curwin->w_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 2038 | match_end = end_pos; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 2039 | curwin->w_cursor = save_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2040 | } |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 2041 | else |
| 2042 | end_pos = curwin->w_cursor; /* shutup gcc 4 */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2043 | |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 2044 | /* Disable 'hlsearch' highlighting if the pattern matches |
| 2045 | * everything. Avoids a flash when typing "foo\|". */ |
| 2046 | if (empty_pattern(ccline.cmdbuff)) |
| 2047 | SET_NO_HLSEARCH(TRUE); |
| 2048 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2049 | validate_cursor(); |
Bram Moolenaar | 27a2319 | 2006-09-14 09:27:26 +0000 | [diff] [blame] | 2050 | /* May redraw the status line to show the cursor position. */ |
| 2051 | if (p_ru && curwin->w_status_height > 0) |
| 2052 | curwin->w_redr_status = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2053 | |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 2054 | save_cmdline(&save_ccline); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 2055 | update_screen(SOME_VALID); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 2056 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 2057 | restore_last_search_pattern(); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 2058 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2059 | /* Leave it at the end to make CTRL-R CTRL-W work. */ |
| 2060 | if (i != 0) |
| 2061 | curwin->w_cursor = end_pos; |
| 2062 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2063 | msg_starthere(); |
| 2064 | redrawcmdline(); |
| 2065 | did_incsearch = TRUE; |
| 2066 | } |
| 2067 | #else /* FEAT_SEARCH_EXTRA */ |
| 2068 | ; |
| 2069 | #endif |
| 2070 | |
| 2071 | #ifdef FEAT_RIGHTLEFT |
| 2072 | if (cmdmsg_rl |
| 2073 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2074 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2075 | # endif |
| 2076 | ) |
| 2077 | /* Always redraw the whole command line to fix shaping and |
Bram Moolenaar | 58437e0 | 2012-02-22 17:58:04 +0100 | [diff] [blame] | 2078 | * right-left typing. Not efficient, but it works. |
| 2079 | * Do it only when there are no characters left to read |
| 2080 | * to avoid useless intermediate redraws. */ |
| 2081 | if (vpeekc() == NUL) |
| 2082 | redrawcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2083 | #endif |
| 2084 | } |
| 2085 | |
| 2086 | returncmd: |
| 2087 | |
| 2088 | #ifdef FEAT_RIGHTLEFT |
| 2089 | cmdmsg_rl = FALSE; |
| 2090 | #endif |
| 2091 | |
| 2092 | #ifdef FEAT_FKMAP |
| 2093 | cmd_fkmap = 0; |
| 2094 | #endif |
| 2095 | |
| 2096 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2097 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2098 | |
| 2099 | #ifdef FEAT_SEARCH_EXTRA |
| 2100 | if (did_incsearch) |
| 2101 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 2102 | if (gotesc) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 2103 | curwin->w_cursor = save_cursor; |
| 2104 | else |
| 2105 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2106 | if (!EQUAL_POS(save_cursor, search_start)) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 2107 | { |
| 2108 | /* put the '" mark at the original position */ |
| 2109 | curwin->w_cursor = save_cursor; |
| 2110 | setpcmark(); |
| 2111 | } |
| 2112 | curwin->w_cursor = search_start; |
| 2113 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2114 | curwin->w_curswant = old_curswant; |
| 2115 | curwin->w_leftcol = old_leftcol; |
| 2116 | curwin->w_topline = old_topline; |
| 2117 | # ifdef FEAT_DIFF |
| 2118 | curwin->w_topfill = old_topfill; |
| 2119 | # endif |
| 2120 | curwin->w_botline = old_botline; |
| 2121 | highlight_match = FALSE; |
| 2122 | validate_cursor(); /* needed for TAB */ |
Bram Moolenaar | f8f8b2e | 2017-11-02 19:08:48 +0100 | [diff] [blame] | 2123 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2124 | } |
| 2125 | #endif |
| 2126 | |
| 2127 | if (ccline.cmdbuff != NULL) |
| 2128 | { |
| 2129 | /* |
| 2130 | * Put line in history buffer (":" and "=" only when it was typed). |
| 2131 | */ |
| 2132 | #ifdef FEAT_CMDHIST |
| 2133 | if (ccline.cmdlen && firstc != NUL |
| 2134 | && (some_key_typed || histype == HIST_SEARCH)) |
| 2135 | { |
| 2136 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 2137 | histype == HIST_SEARCH ? firstc : NUL); |
| 2138 | if (firstc == ':') |
| 2139 | { |
| 2140 | vim_free(new_last_cmdline); |
| 2141 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 2142 | } |
| 2143 | } |
| 2144 | #endif |
| 2145 | |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 2146 | if (gotesc) |
| 2147 | abandon_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2148 | } |
| 2149 | |
| 2150 | /* |
| 2151 | * If the screen was shifted up, redraw the whole screen (later). |
| 2152 | * If the line is too long, clear it, so ruler and shown command do |
| 2153 | * not get printed in the middle of it. |
| 2154 | */ |
| 2155 | msg_check(); |
| 2156 | msg_scroll = save_msg_scroll; |
| 2157 | redir_off = FALSE; |
| 2158 | |
| 2159 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 2160 | if (some_key_typed) |
| 2161 | need_wait_return = FALSE; |
| 2162 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2163 | #ifdef FEAT_AUTOCMD |
| 2164 | /* Trigger CmdlineLeave autocommands. */ |
| 2165 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE); |
| 2166 | #endif |
| 2167 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2168 | State = save_State; |
Bram Moolenaar | 819edbe | 2017-11-25 17:14:33 +0100 | [diff] [blame] | 2169 | #ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2170 | if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) |
| 2171 | im_save_status(b_im_ptr); |
| 2172 | im_set_active(FALSE); |
| 2173 | #endif |
| 2174 | #ifdef FEAT_MOUSE |
| 2175 | setmouse(); |
| 2176 | #endif |
| 2177 | #ifdef CURSOR_SHAPE |
| 2178 | ui_cursor_shape(); /* may show different cursor shape */ |
| 2179 | #endif |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 2180 | sb_text_end_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2181 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2182 | { |
| 2183 | char_u *p = ccline.cmdbuff; |
| 2184 | |
| 2185 | /* Make ccline empty, getcmdline() may try to use it. */ |
| 2186 | ccline.cmdbuff = NULL; |
| 2187 | return p; |
| 2188 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) |
| 2192 | /* |
| 2193 | * Get a command line with a prompt. |
| 2194 | * This is prepared to be called recursively from getcmdline() (e.g. by |
| 2195 | * f_input() when evaluating an expression from CTRL-R =). |
| 2196 | * Returns the command line in allocated memory, or NULL. |
| 2197 | */ |
| 2198 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2199 | getcmdline_prompt( |
| 2200 | int firstc, |
| 2201 | char_u *prompt, /* command line prompt */ |
| 2202 | int attr, /* attributes for prompt */ |
| 2203 | int xp_context, /* type of expansion */ |
| 2204 | char_u *xp_arg) /* user-defined expansion argument */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2205 | { |
| 2206 | char_u *s; |
| 2207 | struct cmdline_info save_ccline; |
| 2208 | int msg_col_save = msg_col; |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2209 | int msg_silent_save = msg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2210 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2211 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2212 | ccline.cmdprompt = prompt; |
| 2213 | ccline.cmdattr = attr; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2214 | # ifdef FEAT_EVAL |
| 2215 | ccline.xp_context = xp_context; |
| 2216 | ccline.xp_arg = xp_arg; |
| 2217 | ccline.input_fn = (firstc == '@'); |
| 2218 | # endif |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2219 | msg_silent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2220 | s = getcmdline(firstc, 1L, 0); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2221 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2222 | msg_silent = msg_silent_save; |
Bram Moolenaar | 1db1f77 | 2011-08-17 16:25:48 +0200 | [diff] [blame] | 2223 | /* Restore msg_col, the prompt from input() may have changed it. |
| 2224 | * But only if called recursively and the commandline is therefore being |
| 2225 | * restored to an old one; if not, the input() prompt stays on the screen, |
| 2226 | * so we need its modified msg_col left intact. */ |
| 2227 | if (ccline.cmdbuff != NULL) |
| 2228 | msg_col = msg_col_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2229 | |
| 2230 | return s; |
| 2231 | } |
| 2232 | #endif |
| 2233 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2234 | /* |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2235 | * Return TRUE when the text must not be changed and we can't switch to |
| 2236 | * another window or buffer. Used when editing the command line, evaluating |
| 2237 | * 'balloonexpr', etc. |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2238 | */ |
| 2239 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2240 | text_locked(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2241 | { |
| 2242 | #ifdef FEAT_CMDWIN |
| 2243 | if (cmdwin_type != 0) |
| 2244 | return TRUE; |
| 2245 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2246 | return textlock != 0; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2247 | } |
| 2248 | |
| 2249 | /* |
| 2250 | * Give an error message for a command that isn't allowed while the cmdline |
| 2251 | * window is open or editing the cmdline in another way. |
| 2252 | */ |
| 2253 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2254 | text_locked_msg(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2255 | { |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2256 | EMSG(_(get_text_locked_msg())); |
| 2257 | } |
| 2258 | |
| 2259 | char_u * |
| 2260 | get_text_locked_msg(void) |
| 2261 | { |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2262 | #ifdef FEAT_CMDWIN |
| 2263 | if (cmdwin_type != 0) |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2264 | return e_cmdwin; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2265 | #endif |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2266 | return e_secure; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2267 | } |
| 2268 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2269 | #if defined(FEAT_AUTOCMD) || defined(PROTO) |
| 2270 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2271 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2272 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2273 | */ |
| 2274 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2275 | curbuf_locked(void) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2276 | { |
| 2277 | if (curbuf_lock > 0) |
| 2278 | { |
| 2279 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2280 | return TRUE; |
| 2281 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2282 | return allbuf_locked(); |
| 2283 | } |
| 2284 | |
| 2285 | /* |
| 2286 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2287 | * message. |
| 2288 | */ |
| 2289 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2290 | allbuf_locked(void) |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2291 | { |
| 2292 | if (allbuf_lock > 0) |
| 2293 | { |
| 2294 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2295 | return TRUE; |
| 2296 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2297 | return FALSE; |
| 2298 | } |
| 2299 | #endif |
| 2300 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2301 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2302 | cmdline_charsize(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2303 | { |
| 2304 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2305 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2306 | return 1; |
| 2307 | #endif |
| 2308 | return ptr2cells(ccline.cmdbuff + idx); |
| 2309 | } |
| 2310 | |
| 2311 | /* |
| 2312 | * Compute the offset of the cursor on the command line for the prompt and |
| 2313 | * indent. |
| 2314 | */ |
| 2315 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2316 | set_cmdspos(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2317 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2318 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2319 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2320 | else |
| 2321 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2322 | } |
| 2323 | |
| 2324 | /* |
| 2325 | * Compute the screen position for the cursor on the command line. |
| 2326 | */ |
| 2327 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2328 | set_cmdspos_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2329 | { |
| 2330 | int i, m, c; |
| 2331 | |
| 2332 | set_cmdspos(); |
| 2333 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2334 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2335 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2336 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2337 | m = MAXCOL; |
| 2338 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2339 | else |
| 2340 | m = MAXCOL; |
| 2341 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2342 | { |
| 2343 | c = cmdline_charsize(i); |
| 2344 | #ifdef FEAT_MBYTE |
| 2345 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2346 | if (has_mbyte) |
| 2347 | correct_cmdspos(i, c); |
| 2348 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2349 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2350 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2351 | if ((ccline.cmdspos += c) >= m) |
| 2352 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2353 | ccline.cmdspos -= c; |
| 2354 | break; |
| 2355 | } |
| 2356 | #ifdef FEAT_MBYTE |
| 2357 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2358 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2359 | #endif |
| 2360 | } |
| 2361 | } |
| 2362 | |
| 2363 | #ifdef FEAT_MBYTE |
| 2364 | /* |
| 2365 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2366 | * character that doesn't fit, so that a ">" must be displayed. |
| 2367 | */ |
| 2368 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2369 | correct_cmdspos(int idx, int cells) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2370 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2371 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2372 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2373 | && ccline.cmdspos % Columns + cells > Columns) |
| 2374 | ccline.cmdspos++; |
| 2375 | } |
| 2376 | #endif |
| 2377 | |
| 2378 | /* |
| 2379 | * Get an Ex command line for the ":" command. |
| 2380 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2381 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2382 | getexline( |
| 2383 | int c, /* normally ':', NUL for ":append" */ |
| 2384 | void *cookie UNUSED, |
| 2385 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2386 | { |
| 2387 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2388 | if (exec_from_reg && vpeekc() == ':') |
| 2389 | (void)vgetc(); |
| 2390 | return getcmdline(c, 1L, indent); |
| 2391 | } |
| 2392 | |
| 2393 | /* |
| 2394 | * Get an Ex command line for Ex mode. |
| 2395 | * In Ex mode we only use the OS supplied line editing features and no |
| 2396 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2397 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2398 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2399 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2400 | getexmodeline( |
| 2401 | int promptc, /* normally ':', NUL for ":append" and '?' for |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2402 | :s prompt */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2403 | void *cookie UNUSED, |
| 2404 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2405 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2406 | garray_T line_ga; |
| 2407 | char_u *pend; |
| 2408 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2409 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2410 | int escaped = FALSE; /* CTRL-V typed */ |
| 2411 | int vcol = 0; |
| 2412 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2413 | int prev_char; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2414 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2415 | |
| 2416 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2417 | * confuses the system function that computes tabstops. */ |
| 2418 | cursor_on(); |
| 2419 | |
| 2420 | /* always start in column 0; write a newline if necessary */ |
| 2421 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2422 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2423 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2424 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2425 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2426 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2427 | if (p_prompt) |
| 2428 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2429 | while (indent-- > 0) |
| 2430 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2431 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2432 | } |
| 2433 | |
| 2434 | ga_init2(&line_ga, 1, 30); |
| 2435 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2436 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2437 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2438 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2439 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2440 | while (indent >= 8) |
| 2441 | { |
| 2442 | ga_append(&line_ga, TAB); |
| 2443 | msg_puts((char_u *)" "); |
| 2444 | indent -= 8; |
| 2445 | } |
| 2446 | while (indent-- > 0) |
| 2447 | { |
| 2448 | ga_append(&line_ga, ' '); |
| 2449 | msg_putchar(' '); |
| 2450 | } |
| 2451 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2452 | ++no_mapping; |
| 2453 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2454 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2455 | /* |
| 2456 | * Get the line, one character at a time. |
| 2457 | */ |
| 2458 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2459 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2460 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2461 | long sw; |
| 2462 | char_u *s; |
| 2463 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2464 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2465 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2466 | |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2467 | /* |
| 2468 | * Get one character at a time. |
| 2469 | */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2470 | prev_char = c1; |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2471 | |
| 2472 | /* Check for a ":normal" command and no more characters left. */ |
| 2473 | if (ex_normal_busy > 0 && typebuf.tb_len == 0) |
| 2474 | c1 = '\n'; |
| 2475 | else |
| 2476 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2477 | |
| 2478 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2479 | * Handle line editing. |
| 2480 | * Previously this was left to the system, putting the terminal in |
| 2481 | * 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] | 2482 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2483 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2484 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2485 | msg_putchar('\n'); |
| 2486 | break; |
| 2487 | } |
| 2488 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2489 | if (c1 == K_PS) |
| 2490 | { |
| 2491 | bracketed_paste(PASTE_EX, FALSE, &line_ga); |
| 2492 | goto redraw; |
| 2493 | } |
| 2494 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2495 | if (!escaped) |
| 2496 | { |
| 2497 | /* CR typed means "enter", which is NL */ |
| 2498 | if (c1 == '\r') |
| 2499 | c1 = '\n'; |
| 2500 | |
| 2501 | if (c1 == BS || c1 == K_BS |
| 2502 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
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 | if (line_ga.ga_len > 0) |
| 2505 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2506 | #ifdef FEAT_MBYTE |
| 2507 | if (has_mbyte) |
| 2508 | { |
| 2509 | p = (char_u *)line_ga.ga_data; |
| 2510 | p[line_ga.ga_len] = NUL; |
| 2511 | len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1; |
| 2512 | line_ga.ga_len -= len; |
| 2513 | } |
| 2514 | else |
| 2515 | #endif |
| 2516 | --line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2517 | goto redraw; |
| 2518 | } |
| 2519 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2520 | } |
| 2521 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2522 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2523 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2524 | msg_col = startcol; |
| 2525 | msg_clr_eos(); |
| 2526 | line_ga.ga_len = 0; |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2527 | goto redraw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2528 | } |
| 2529 | |
| 2530 | if (c1 == Ctrl_T) |
| 2531 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2532 | sw = get_sw_value(curbuf); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2533 | p = (char_u *)line_ga.ga_data; |
| 2534 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2535 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 2536 | indent += sw - indent % sw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2537 | add_indent: |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2538 | while (get_indent_str(p, 8, FALSE) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2539 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2540 | (void)ga_grow(&line_ga, 2); /* one more for the NUL */ |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2541 | p = (char_u *)line_ga.ga_data; |
| 2542 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2543 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2544 | *s = ' '; |
| 2545 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2546 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2547 | redraw: |
| 2548 | /* redraw the line */ |
| 2549 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2550 | vcol = 0; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2551 | p = (char_u *)line_ga.ga_data; |
| 2552 | p[line_ga.ga_len] = NUL; |
| 2553 | while (p < (char_u *)line_ga.ga_data + line_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2554 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2555 | if (*p == TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2556 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2557 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2558 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2559 | msg_putchar(' '); |
| 2560 | } while (++vcol % 8); |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2561 | ++p; |
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 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2564 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2565 | len = MB_PTR2LEN(p); |
| 2566 | msg_outtrans_len(p, len); |
| 2567 | vcol += ptr2cells(p); |
| 2568 | p += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2569 | } |
| 2570 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2571 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2572 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2573 | continue; |
| 2574 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2575 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2576 | if (c1 == Ctrl_D) |
| 2577 | { |
| 2578 | /* Delete one shiftwidth. */ |
| 2579 | p = (char_u *)line_ga.ga_data; |
| 2580 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2581 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2582 | if (prev_char == '^') |
| 2583 | ex_keep_indent = TRUE; |
| 2584 | indent = 0; |
| 2585 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2586 | } |
| 2587 | else |
| 2588 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2589 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2590 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2591 | if (indent > 0) |
| 2592 | { |
| 2593 | --indent; |
| 2594 | indent -= indent % get_sw_value(curbuf); |
| 2595 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2596 | } |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2597 | while (get_indent_str(p, 8, FALSE) > indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2598 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2599 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2600 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2601 | --line_ga.ga_len; |
| 2602 | } |
| 2603 | goto add_indent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2604 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2605 | |
| 2606 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2607 | { |
| 2608 | escaped = TRUE; |
| 2609 | continue; |
| 2610 | } |
| 2611 | |
| 2612 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2613 | if (IS_SPECIAL(c1)) |
| 2614 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2615 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2616 | |
| 2617 | if (IS_SPECIAL(c1)) |
| 2618 | c1 = '?'; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2619 | #ifdef FEAT_MBYTE |
| 2620 | if (has_mbyte) |
| 2621 | len = (*mb_char2bytes)(c1, |
| 2622 | (char_u *)line_ga.ga_data + line_ga.ga_len); |
| 2623 | else |
| 2624 | #endif |
| 2625 | { |
| 2626 | len = 1; |
| 2627 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
| 2628 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2629 | if (c1 == '\n') |
| 2630 | msg_putchar('\n'); |
| 2631 | else if (c1 == TAB) |
| 2632 | { |
| 2633 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2634 | do |
| 2635 | { |
| 2636 | msg_putchar(' '); |
| 2637 | } while (++vcol % 8); |
| 2638 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2639 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2640 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2641 | msg_outtrans_len( |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2642 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, len); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2643 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2644 | } |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2645 | line_ga.ga_len += len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2646 | escaped = FALSE; |
| 2647 | |
| 2648 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2649 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2650 | |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2651 | /* We are done when a NL is entered, but not when it comes after an |
| 2652 | * odd number of backslashes, that results in a NUL. */ |
| 2653 | if (line_ga.ga_len > 0 && pend[-1] == '\n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2654 | { |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2655 | int bcount = 0; |
| 2656 | |
| 2657 | while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') |
| 2658 | ++bcount; |
| 2659 | |
| 2660 | if (bcount > 0) |
| 2661 | { |
| 2662 | /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> |
| 2663 | * "\NL", etc. */ |
| 2664 | line_ga.ga_len -= (bcount + 1) / 2; |
| 2665 | pend -= (bcount + 1) / 2; |
| 2666 | pend[-1] = '\n'; |
| 2667 | } |
| 2668 | |
| 2669 | if ((bcount & 1) == 0) |
| 2670 | { |
| 2671 | --line_ga.ga_len; |
| 2672 | --pend; |
| 2673 | *pend = NUL; |
| 2674 | break; |
| 2675 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2676 | } |
| 2677 | } |
| 2678 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2679 | --no_mapping; |
| 2680 | --allow_keys; |
| 2681 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2682 | /* make following messages go to the next line */ |
| 2683 | msg_didout = FALSE; |
| 2684 | msg_col = 0; |
| 2685 | if (msg_row < Rows - 1) |
| 2686 | ++msg_row; |
| 2687 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 2688 | |
| 2689 | if (got_int) |
| 2690 | ga_clear(&line_ga); |
| 2691 | |
| 2692 | return (char_u *)line_ga.ga_data; |
| 2693 | } |
| 2694 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2695 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 2696 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2697 | /* |
| 2698 | * Return TRUE if ccline.overstrike is on. |
| 2699 | */ |
| 2700 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2701 | cmdline_overstrike(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2702 | { |
| 2703 | return ccline.overstrike; |
| 2704 | } |
| 2705 | |
| 2706 | /* |
| 2707 | * Return TRUE if the cursor is at the end of the cmdline. |
| 2708 | */ |
| 2709 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2710 | cmdline_at_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2711 | { |
| 2712 | return (ccline.cmdpos >= ccline.cmdlen); |
| 2713 | } |
| 2714 | #endif |
| 2715 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2716 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2717 | /* |
| 2718 | * Return the virtual column number at the current cursor position. |
| 2719 | * This is used by the IM code to obtain the start of the preedit string. |
| 2720 | */ |
| 2721 | colnr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2722 | cmdline_getvcol_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2723 | { |
| 2724 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 2725 | return MAXCOL; |
| 2726 | |
| 2727 | # ifdef FEAT_MBYTE |
| 2728 | if (has_mbyte) |
| 2729 | { |
| 2730 | colnr_T col; |
| 2731 | int i = 0; |
| 2732 | |
| 2733 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2734 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2735 | |
| 2736 | return col; |
| 2737 | } |
| 2738 | else |
| 2739 | # endif |
| 2740 | return ccline.cmdpos; |
| 2741 | } |
| 2742 | #endif |
| 2743 | |
| 2744 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2745 | /* |
| 2746 | * If part of the command line is an IM preedit string, redraw it with |
| 2747 | * IM feedback attributes. The cursor position is restored after drawing. |
| 2748 | */ |
| 2749 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2750 | redrawcmd_preedit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2751 | { |
| 2752 | if ((State & CMDLINE) |
| 2753 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 2754 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2755 | && !p_imdisable |
| 2756 | && im_is_preediting()) |
| 2757 | { |
| 2758 | int cmdpos = 0; |
| 2759 | int cmdspos; |
| 2760 | int old_row; |
| 2761 | int old_col; |
| 2762 | colnr_T col; |
| 2763 | |
| 2764 | old_row = msg_row; |
| 2765 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2766 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2767 | |
| 2768 | # ifdef FEAT_MBYTE |
| 2769 | if (has_mbyte) |
| 2770 | { |
| 2771 | for (col = 0; col < preedit_start_col |
| 2772 | && cmdpos < ccline.cmdlen; ++col) |
| 2773 | { |
| 2774 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2775 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2776 | } |
| 2777 | } |
| 2778 | else |
| 2779 | # endif |
| 2780 | { |
| 2781 | cmdspos += preedit_start_col; |
| 2782 | cmdpos += preedit_start_col; |
| 2783 | } |
| 2784 | |
| 2785 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 2786 | msg_col = cmdspos % (int)Columns; |
| 2787 | if (msg_row >= Rows) |
| 2788 | msg_row = Rows - 1; |
| 2789 | |
| 2790 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 2791 | { |
| 2792 | int char_len; |
| 2793 | int char_attr; |
| 2794 | |
| 2795 | char_attr = im_get_feedback_attr(col); |
| 2796 | if (char_attr < 0) |
| 2797 | break; /* end of preedit string */ |
| 2798 | |
| 2799 | # ifdef FEAT_MBYTE |
| 2800 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2801 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2802 | else |
| 2803 | # endif |
| 2804 | char_len = 1; |
| 2805 | |
| 2806 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 2807 | cmdpos += char_len; |
| 2808 | } |
| 2809 | |
| 2810 | msg_row = old_row; |
| 2811 | msg_col = old_col; |
| 2812 | } |
| 2813 | } |
| 2814 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 2815 | |
| 2816 | /* |
| 2817 | * Allocate a new command line buffer. |
| 2818 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
| 2819 | * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. |
| 2820 | */ |
| 2821 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2822 | alloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2823 | { |
| 2824 | /* |
| 2825 | * give some extra space to avoid having to allocate all the time |
| 2826 | */ |
| 2827 | if (len < 80) |
| 2828 | len = 100; |
| 2829 | else |
| 2830 | len += 20; |
| 2831 | |
| 2832 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 2833 | ccline.cmdbufflen = len; |
| 2834 | } |
| 2835 | |
| 2836 | /* |
| 2837 | * Re-allocate the command line to length len + something extra. |
| 2838 | * return FAIL for failure, OK otherwise |
| 2839 | */ |
| 2840 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2841 | realloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2842 | { |
| 2843 | char_u *p; |
| 2844 | |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 2845 | if (len < ccline.cmdbufflen) |
| 2846 | return OK; /* no need to resize */ |
| 2847 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2848 | p = ccline.cmdbuff; |
| 2849 | alloc_cmdbuff(len); /* will get some more */ |
| 2850 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 2851 | { |
| 2852 | ccline.cmdbuff = p; /* keep the old one */ |
| 2853 | return FAIL; |
| 2854 | } |
Bram Moolenaar | 35a3423 | 2010-08-13 16:51:26 +0200 | [diff] [blame] | 2855 | /* There isn't always a NUL after the command, but it may need to be |
| 2856 | * there, thus copy up to the NUL and add a NUL. */ |
| 2857 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
| 2858 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2859 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2860 | |
| 2861 | if (ccline.xpc != NULL |
| 2862 | && ccline.xpc->xp_pattern != NULL |
| 2863 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 2864 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 2865 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 2866 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2867 | |
| 2868 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 2869 | * to point into the newly allocated memory. */ |
| 2870 | if (i >= 0 && i <= ccline.cmdlen) |
| 2871 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 2872 | } |
| 2873 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2874 | return OK; |
| 2875 | } |
| 2876 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2877 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 2878 | static char_u *arshape_buf = NULL; |
| 2879 | |
| 2880 | # if defined(EXITFREE) || defined(PROTO) |
| 2881 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2882 | free_cmdline_buf(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2883 | { |
| 2884 | vim_free(arshape_buf); |
| 2885 | } |
| 2886 | # endif |
| 2887 | #endif |
| 2888 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2889 | /* |
| 2890 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 2891 | * when cmdline_star is TRUE. |
| 2892 | */ |
| 2893 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2894 | draw_cmdline(int start, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2895 | { |
| 2896 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2897 | int i; |
| 2898 | |
| 2899 | if (cmdline_star > 0) |
| 2900 | for (i = 0; i < len; ++i) |
| 2901 | { |
| 2902 | msg_putchar('*'); |
| 2903 | # ifdef FEAT_MBYTE |
| 2904 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2905 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2906 | # endif |
| 2907 | } |
| 2908 | else |
| 2909 | #endif |
| 2910 | #ifdef FEAT_ARABIC |
| 2911 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 2912 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2913 | static int buflen = 0; |
| 2914 | char_u *p; |
| 2915 | int j; |
| 2916 | int newlen = 0; |
| 2917 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 2918 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2919 | int prev_c = 0; |
| 2920 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2921 | int u8c; |
| 2922 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2923 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2924 | |
| 2925 | /* |
| 2926 | * Do arabic shaping into a temporary buffer. This is very |
| 2927 | * inefficient! |
| 2928 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2929 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2930 | { |
| 2931 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 2932 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2933 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2934 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2935 | arshape_buf = alloc(buflen); |
| 2936 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2937 | return; /* out of memory */ |
| 2938 | } |
| 2939 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2940 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 2941 | { |
| 2942 | /* Prepend a space to draw the leading composing char on. */ |
| 2943 | arshape_buf[0] = ' '; |
| 2944 | newlen = 1; |
| 2945 | } |
| 2946 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2947 | for (j = start; j < start + len; j += mb_l) |
| 2948 | { |
| 2949 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2950 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2951 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2952 | if (ARABIC_CHAR(u8c)) |
| 2953 | { |
| 2954 | /* Do Arabic shaping. */ |
| 2955 | if (cmdmsg_rl) |
| 2956 | { |
| 2957 | /* displaying from right to left */ |
| 2958 | pc = prev_c; |
| 2959 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2960 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2961 | if (j + mb_l >= start + len) |
| 2962 | nc = NUL; |
| 2963 | else |
| 2964 | nc = utf_ptr2char(p + mb_l); |
| 2965 | } |
| 2966 | else |
| 2967 | { |
| 2968 | /* displaying from left to right */ |
| 2969 | if (j + mb_l >= start + len) |
| 2970 | pc = NUL; |
| 2971 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2972 | { |
| 2973 | int pcc[MAX_MCO]; |
| 2974 | |
| 2975 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2976 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2977 | pc1 = pcc[0]; |
| 2978 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2979 | nc = prev_c; |
| 2980 | } |
| 2981 | prev_c = u8c; |
| 2982 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2983 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2984 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2985 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2986 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2987 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2988 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 2989 | if (u8cc[1] != 0) |
| 2990 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2991 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2992 | } |
| 2993 | } |
| 2994 | else |
| 2995 | { |
| 2996 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2997 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2998 | newlen += mb_l; |
| 2999 | } |
| 3000 | } |
| 3001 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3002 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3003 | } |
| 3004 | else |
| 3005 | #endif |
| 3006 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 3007 | } |
| 3008 | |
| 3009 | /* |
| 3010 | * Put a character on the command line. Shifts the following text to the |
| 3011 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 3012 | * "c" must be printable (fit in one display cell)! |
| 3013 | */ |
| 3014 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3015 | putcmdline(int c, int shift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3016 | { |
| 3017 | if (cmd_silent) |
| 3018 | return; |
| 3019 | msg_no_more = TRUE; |
| 3020 | msg_putchar(c); |
| 3021 | if (shift) |
| 3022 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3023 | msg_no_more = FALSE; |
| 3024 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3025 | extra_char = c; |
| 3026 | extra_char_shift = shift; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3027 | } |
| 3028 | |
| 3029 | /* |
| 3030 | * Undo a putcmdline(c, FALSE). |
| 3031 | */ |
| 3032 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3033 | unputcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3034 | { |
| 3035 | if (cmd_silent) |
| 3036 | return; |
| 3037 | msg_no_more = TRUE; |
| 3038 | if (ccline.cmdlen == ccline.cmdpos) |
| 3039 | msg_putchar(' '); |
Bram Moolenaar | 64fdf5c | 2012-06-06 12:03:06 +0200 | [diff] [blame] | 3040 | #ifdef FEAT_MBYTE |
| 3041 | else if (has_mbyte) |
| 3042 | draw_cmdline(ccline.cmdpos, |
| 3043 | (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); |
| 3044 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3045 | else |
| 3046 | draw_cmdline(ccline.cmdpos, 1); |
| 3047 | msg_no_more = FALSE; |
| 3048 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3049 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3050 | } |
| 3051 | |
| 3052 | /* |
| 3053 | * Put the given string, of the given length, onto the command line. |
| 3054 | * If len is -1, then STRLEN() is used to calculate the length. |
| 3055 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 3056 | * part will be redrawn, otherwise it will not. If this function is called |
| 3057 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 3058 | * called afterwards. |
| 3059 | */ |
| 3060 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3061 | put_on_cmdline(char_u *str, int len, int redraw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3062 | { |
| 3063 | int retval; |
| 3064 | int i; |
| 3065 | int m; |
| 3066 | int c; |
| 3067 | |
| 3068 | if (len < 0) |
| 3069 | len = (int)STRLEN(str); |
| 3070 | |
| 3071 | /* Check if ccline.cmdbuff needs to be longer */ |
| 3072 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3073 | retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3074 | else |
| 3075 | retval = OK; |
| 3076 | if (retval == OK) |
| 3077 | { |
| 3078 | if (!ccline.overstrike) |
| 3079 | { |
| 3080 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3081 | ccline.cmdbuff + ccline.cmdpos, |
| 3082 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 3083 | ccline.cmdlen += len; |
| 3084 | } |
| 3085 | else |
| 3086 | { |
| 3087 | #ifdef FEAT_MBYTE |
| 3088 | if (has_mbyte) |
| 3089 | { |
| 3090 | /* Count nr of characters in the new string. */ |
| 3091 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3092 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3093 | ++m; |
| 3094 | /* Count nr of bytes in cmdline that are overwritten by these |
| 3095 | * characters. */ |
| 3096 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3097 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3098 | --m; |
| 3099 | if (i < ccline.cmdlen) |
| 3100 | { |
| 3101 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3102 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 3103 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 3104 | } |
| 3105 | else |
| 3106 | ccline.cmdlen = ccline.cmdpos + len; |
| 3107 | } |
| 3108 | else |
| 3109 | #endif |
| 3110 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 3111 | ccline.cmdlen = ccline.cmdpos + len; |
| 3112 | } |
| 3113 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 3114 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 3115 | |
| 3116 | #ifdef FEAT_MBYTE |
| 3117 | if (enc_utf8) |
| 3118 | { |
| 3119 | /* When the inserted text starts with a composing character, |
| 3120 | * backup to the character before it. There could be two of them. |
| 3121 | */ |
| 3122 | i = 0; |
| 3123 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3124 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 3125 | { |
| 3126 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3127 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3128 | ccline.cmdpos -= i; |
| 3129 | len += i; |
| 3130 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3131 | } |
| 3132 | # ifdef FEAT_ARABIC |
| 3133 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 3134 | { |
| 3135 | /* Check the previous character for Arabic combining pair. */ |
| 3136 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3137 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3138 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 3139 | + ccline.cmdpos - i), c)) |
| 3140 | { |
| 3141 | ccline.cmdpos -= i; |
| 3142 | len += i; |
| 3143 | } |
| 3144 | else |
| 3145 | i = 0; |
| 3146 | } |
| 3147 | # endif |
| 3148 | if (i != 0) |
| 3149 | { |
| 3150 | /* Also backup the cursor position. */ |
| 3151 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 3152 | ccline.cmdspos -= i; |
| 3153 | msg_col -= i; |
| 3154 | if (msg_col < 0) |
| 3155 | { |
| 3156 | msg_col += Columns; |
| 3157 | --msg_row; |
| 3158 | } |
| 3159 | } |
| 3160 | } |
| 3161 | #endif |
| 3162 | |
| 3163 | if (redraw && !cmd_silent) |
| 3164 | { |
| 3165 | msg_no_more = TRUE; |
| 3166 | i = cmdline_row; |
Bram Moolenaar | 73dc59a | 2011-09-30 17:46:21 +0200 | [diff] [blame] | 3167 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3168 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3169 | /* Avoid clearing the rest of the line too often. */ |
| 3170 | if (cmdline_row != i || ccline.overstrike) |
| 3171 | msg_clr_eos(); |
| 3172 | msg_no_more = FALSE; |
| 3173 | } |
| 3174 | #ifdef FEAT_FKMAP |
| 3175 | /* |
| 3176 | * If we are in Farsi command mode, the character input must be in |
| 3177 | * Insert mode. So do not advance the cmdpos. |
| 3178 | */ |
| 3179 | if (!cmd_fkmap) |
| 3180 | #endif |
| 3181 | { |
| 3182 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3183 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3184 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3185 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 3186 | m = MAXCOL; |
| 3187 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3188 | else |
| 3189 | m = MAXCOL; |
| 3190 | for (i = 0; i < len; ++i) |
| 3191 | { |
| 3192 | c = cmdline_charsize(ccline.cmdpos); |
| 3193 | #ifdef FEAT_MBYTE |
| 3194 | /* count ">" for a double-wide char that doesn't fit. */ |
| 3195 | if (has_mbyte) |
| 3196 | correct_cmdspos(ccline.cmdpos, c); |
| 3197 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3198 | /* Stop cursor at the end of the screen, but do increment the |
| 3199 | * insert position, so that entering a very long command |
| 3200 | * works, even though you can't see it. */ |
| 3201 | if (ccline.cmdspos + c < m) |
| 3202 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3203 | #ifdef FEAT_MBYTE |
| 3204 | if (has_mbyte) |
| 3205 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3206 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3207 | if (c > len - i - 1) |
| 3208 | c = len - i - 1; |
| 3209 | ccline.cmdpos += c; |
| 3210 | i += c; |
| 3211 | } |
| 3212 | #endif |
| 3213 | ++ccline.cmdpos; |
| 3214 | } |
| 3215 | } |
| 3216 | } |
| 3217 | if (redraw) |
| 3218 | msg_check(); |
| 3219 | return retval; |
| 3220 | } |
| 3221 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3222 | static struct cmdline_info prev_ccline; |
| 3223 | static int prev_ccline_used = FALSE; |
| 3224 | |
| 3225 | /* |
| 3226 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 3227 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 3228 | * available globally in prev_ccline. |
| 3229 | */ |
| 3230 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3231 | save_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3232 | { |
| 3233 | if (!prev_ccline_used) |
| 3234 | { |
| 3235 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 3236 | prev_ccline_used = TRUE; |
| 3237 | } |
| 3238 | *ccp = prev_ccline; |
| 3239 | prev_ccline = ccline; |
| 3240 | ccline.cmdbuff = NULL; |
| 3241 | ccline.cmdprompt = NULL; |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3242 | ccline.xpc = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3243 | } |
| 3244 | |
| 3245 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3246 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3247 | */ |
| 3248 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3249 | restore_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3250 | { |
| 3251 | ccline = prev_ccline; |
| 3252 | prev_ccline = *ccp; |
| 3253 | } |
| 3254 | |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3255 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3256 | /* |
| 3257 | * Save the command line into allocated memory. Returns a pointer to be |
| 3258 | * passed to restore_cmdline_alloc() later. |
| 3259 | * Returns NULL when failed. |
| 3260 | */ |
| 3261 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3262 | save_cmdline_alloc(void) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3263 | { |
| 3264 | struct cmdline_info *p; |
| 3265 | |
| 3266 | p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); |
| 3267 | if (p != NULL) |
| 3268 | save_cmdline(p); |
| 3269 | return (char_u *)p; |
| 3270 | } |
| 3271 | |
| 3272 | /* |
| 3273 | * Restore the command line from the return value of save_cmdline_alloc(). |
| 3274 | */ |
| 3275 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3276 | restore_cmdline_alloc(char_u *p) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3277 | { |
| 3278 | if (p != NULL) |
| 3279 | { |
| 3280 | restore_cmdline((struct cmdline_info *)p); |
| 3281 | vim_free(p); |
| 3282 | } |
| 3283 | } |
| 3284 | #endif |
| 3285 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3286 | /* |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3287 | * Paste a yank register into the command line. |
| 3288 | * Used by CTRL-R command in command-line mode. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3289 | * insert_reg() can't be used here, because special characters from the |
| 3290 | * register contents will be interpreted as commands. |
| 3291 | * |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3292 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3293 | */ |
| 3294 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3295 | cmdline_paste( |
| 3296 | int regname, |
| 3297 | int literally, /* Insert text literally instead of "as typed" */ |
| 3298 | int remcr) /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3299 | { |
| 3300 | long i; |
| 3301 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3302 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3303 | int allocated; |
| 3304 | struct cmdline_info save_ccline; |
| 3305 | |
| 3306 | /* check for valid regname; also accept special characters for CTRL-R in |
| 3307 | * the command line */ |
| 3308 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
| 3309 | && regname != Ctrl_A && !valid_yank_reg(regname, FALSE)) |
| 3310 | return FAIL; |
| 3311 | |
| 3312 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 3313 | * CTRL-C to break the loop. */ |
| 3314 | line_breakcheck(); |
| 3315 | if (got_int) |
| 3316 | return FAIL; |
| 3317 | |
| 3318 | #ifdef FEAT_CLIPBOARD |
| 3319 | regname = may_get_selection(regname); |
| 3320 | #endif |
| 3321 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3322 | /* Need to save and restore ccline. And set "textlock" to avoid nasty |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3323 | * things like going to another buffer when evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3324 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3325 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3326 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3327 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3328 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3329 | |
| 3330 | if (i) |
| 3331 | { |
| 3332 | /* Got the value of a special register in "arg". */ |
| 3333 | if (arg == NULL) |
| 3334 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3335 | |
| 3336 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3337 | * part of the word. */ |
| 3338 | p = arg; |
| 3339 | if (p_is && regname == Ctrl_W) |
| 3340 | { |
| 3341 | char_u *w; |
| 3342 | int len; |
| 3343 | |
| 3344 | /* Locate start of last word in the cmd buffer. */ |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3345 | for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3346 | { |
| 3347 | #ifdef FEAT_MBYTE |
| 3348 | if (has_mbyte) |
| 3349 | { |
| 3350 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3351 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3352 | break; |
| 3353 | w -= len; |
| 3354 | } |
| 3355 | else |
| 3356 | #endif |
| 3357 | { |
| 3358 | if (!vim_iswordc(w[-1])) |
| 3359 | break; |
| 3360 | --w; |
| 3361 | } |
| 3362 | } |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3363 | len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3364 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3365 | p += len; |
| 3366 | } |
| 3367 | |
| 3368 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3369 | if (allocated) |
| 3370 | vim_free(arg); |
| 3371 | return OK; |
| 3372 | } |
| 3373 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3374 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3375 | } |
| 3376 | |
| 3377 | /* |
| 3378 | * Put a string on the command line. |
| 3379 | * When "literally" is TRUE, insert literally. |
| 3380 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3381 | * line. |
| 3382 | */ |
| 3383 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3384 | cmdline_paste_str(char_u *s, int literally) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3385 | { |
| 3386 | int c, cv; |
| 3387 | |
| 3388 | if (literally) |
| 3389 | put_on_cmdline(s, -1, TRUE); |
| 3390 | else |
| 3391 | while (*s != NUL) |
| 3392 | { |
| 3393 | cv = *s; |
| 3394 | if (cv == Ctrl_V && s[1]) |
| 3395 | ++s; |
| 3396 | #ifdef FEAT_MBYTE |
| 3397 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3398 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3399 | else |
| 3400 | #endif |
| 3401 | c = *s++; |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 3402 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
| 3403 | || c == CAR || c == NL || c == Ctrl_L |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3404 | #ifdef UNIX |
| 3405 | || c == intr_char |
| 3406 | #endif |
| 3407 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3408 | stuffcharReadbuff(Ctrl_V); |
| 3409 | stuffcharReadbuff(c); |
| 3410 | } |
| 3411 | } |
| 3412 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3413 | #ifdef FEAT_WILDMENU |
| 3414 | /* |
| 3415 | * Delete characters on the command line, from "from" to the current |
| 3416 | * position. |
| 3417 | */ |
| 3418 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3419 | cmdline_del(int from) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3420 | { |
| 3421 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3422 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3423 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3424 | ccline.cmdpos = from; |
| 3425 | } |
| 3426 | #endif |
| 3427 | |
| 3428 | /* |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 3429 | * This function is called when the screen size changes and with incremental |
| 3430 | * search and in other situations where the command line may have been |
| 3431 | * overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3432 | */ |
| 3433 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3434 | redrawcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3435 | { |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3436 | redrawcmdline_ex(TRUE); |
| 3437 | } |
| 3438 | |
| 3439 | void |
| 3440 | redrawcmdline_ex(int do_compute_cmdrow) |
| 3441 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3442 | if (cmd_silent) |
| 3443 | return; |
| 3444 | need_wait_return = FALSE; |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3445 | if (do_compute_cmdrow) |
| 3446 | compute_cmdrow(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3447 | redrawcmd(); |
| 3448 | cursorcmd(); |
| 3449 | } |
| 3450 | |
| 3451 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3452 | redrawcmdprompt(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3453 | { |
| 3454 | int i; |
| 3455 | |
| 3456 | if (cmd_silent) |
| 3457 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3458 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3459 | msg_putchar(ccline.cmdfirstc); |
| 3460 | if (ccline.cmdprompt != NULL) |
| 3461 | { |
| 3462 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3463 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3464 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3465 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3466 | --ccline.cmdindent; |
| 3467 | } |
| 3468 | else |
| 3469 | for (i = ccline.cmdindent; i > 0; --i) |
| 3470 | msg_putchar(' '); |
| 3471 | } |
| 3472 | |
| 3473 | /* |
| 3474 | * Redraw what is currently on the command line. |
| 3475 | */ |
| 3476 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3477 | redrawcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3478 | { |
| 3479 | if (cmd_silent) |
| 3480 | return; |
| 3481 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3482 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3483 | if (ccline.cmdbuff == NULL) |
| 3484 | { |
| 3485 | windgoto(cmdline_row, 0); |
| 3486 | msg_clr_eos(); |
| 3487 | return; |
| 3488 | } |
| 3489 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3490 | msg_start(); |
| 3491 | redrawcmdprompt(); |
| 3492 | |
| 3493 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3494 | msg_no_more = TRUE; |
| 3495 | draw_cmdline(0, ccline.cmdlen); |
| 3496 | msg_clr_eos(); |
| 3497 | msg_no_more = FALSE; |
| 3498 | |
| 3499 | set_cmdspos_cursor(); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 3500 | if (extra_char != NUL) |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3501 | putcmdline(extra_char, extra_char_shift); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3502 | |
| 3503 | /* |
| 3504 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3505 | * in cmdline mode we can reset them now. |
| 3506 | */ |
| 3507 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3508 | |
| 3509 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3510 | * in cmdline mode */ |
| 3511 | skip_redraw = FALSE; |
| 3512 | } |
| 3513 | |
| 3514 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3515 | compute_cmdrow(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3516 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3517 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3518 | cmdline_row = Rows - 1; |
| 3519 | else |
| 3520 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
Bram Moolenaar | e0de17d | 2017-09-24 16:24:34 +0200 | [diff] [blame] | 3521 | + lastwin->w_status_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3522 | } |
| 3523 | |
| 3524 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3525 | cursorcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3526 | { |
| 3527 | if (cmd_silent) |
| 3528 | return; |
| 3529 | |
| 3530 | #ifdef FEAT_RIGHTLEFT |
| 3531 | if (cmdmsg_rl) |
| 3532 | { |
| 3533 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3534 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3535 | if (msg_row <= 0) |
| 3536 | msg_row = Rows - 1; |
| 3537 | } |
| 3538 | else |
| 3539 | #endif |
| 3540 | { |
| 3541 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3542 | msg_col = ccline.cmdspos % (int)Columns; |
| 3543 | if (msg_row >= Rows) |
| 3544 | msg_row = Rows - 1; |
| 3545 | } |
| 3546 | |
| 3547 | windgoto(msg_row, msg_col); |
| 3548 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 3549 | if (p_imst == IM_ON_THE_SPOT) |
| 3550 | redrawcmd_preedit(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3551 | #endif |
| 3552 | #ifdef MCH_CURSOR_SHAPE |
| 3553 | mch_update_cursor(); |
| 3554 | #endif |
| 3555 | } |
| 3556 | |
| 3557 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3558 | gotocmdline(int clr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3559 | { |
| 3560 | msg_start(); |
| 3561 | #ifdef FEAT_RIGHTLEFT |
| 3562 | if (cmdmsg_rl) |
| 3563 | msg_col = Columns - 1; |
| 3564 | else |
| 3565 | #endif |
| 3566 | msg_col = 0; /* always start in column 0 */ |
| 3567 | if (clr) /* clear the bottom line(s) */ |
| 3568 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3569 | windgoto(cmdline_row, 0); |
| 3570 | } |
| 3571 | |
| 3572 | /* |
| 3573 | * Check the word in front of the cursor for an abbreviation. |
| 3574 | * Called when the non-id character "c" has been entered. |
| 3575 | * When an abbreviation is recognized it is removed from the text with |
| 3576 | * backspaces and the replacement string is inserted, followed by "c". |
| 3577 | */ |
| 3578 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3579 | ccheck_abbr(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3580 | { |
| 3581 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3582 | return FALSE; |
| 3583 | |
| 3584 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0); |
| 3585 | } |
| 3586 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3587 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 3588 | static int |
| 3589 | #ifdef __BORLANDC__ |
| 3590 | _RTLENTRYF |
| 3591 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3592 | sort_func_compare(const void *s1, const void *s2) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3593 | { |
| 3594 | char_u *p1 = *(char_u **)s1; |
| 3595 | char_u *p2 = *(char_u **)s2; |
| 3596 | |
| 3597 | if (*p1 != '<' && *p2 == '<') return -1; |
| 3598 | if (*p1 == '<' && *p2 != '<') return 1; |
| 3599 | return STRCMP(p1, p2); |
| 3600 | } |
| 3601 | #endif |
| 3602 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3603 | /* |
| 3604 | * Return FAIL if this is not an appropriate context in which to do |
| 3605 | * completion of anything, return OK if it is (even if there are no matches). |
| 3606 | * For the caller, this means that the character is just passed through like a |
| 3607 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3608 | */ |
| 3609 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3610 | nextwild( |
| 3611 | expand_T *xp, |
| 3612 | int type, |
| 3613 | int options, /* extra options for ExpandOne() */ |
| 3614 | int escape) /* if TRUE, escape the returned matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3615 | { |
| 3616 | int i, j; |
| 3617 | char_u *p1; |
| 3618 | char_u *p2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3619 | int difflen; |
| 3620 | int v; |
| 3621 | |
| 3622 | if (xp->xp_numfiles == -1) |
| 3623 | { |
| 3624 | set_expand_context(xp); |
| 3625 | cmd_showtail = expand_showtail(xp); |
| 3626 | } |
| 3627 | |
| 3628 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3629 | { |
| 3630 | beep_flush(); |
| 3631 | return OK; /* Something illegal on command line */ |
| 3632 | } |
| 3633 | if (xp->xp_context == EXPAND_NOTHING) |
| 3634 | { |
| 3635 | /* Caller can use the character as a normal char instead */ |
| 3636 | return FAIL; |
| 3637 | } |
| 3638 | |
| 3639 | MSG_PUTS("..."); /* show that we are busy */ |
| 3640 | out_flush(); |
| 3641 | |
| 3642 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3643 | xp->xp_pattern_len = ccline.cmdpos - i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3644 | |
| 3645 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3646 | { |
| 3647 | /* |
| 3648 | * Get next/previous match for a previous expanded pattern. |
| 3649 | */ |
| 3650 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3651 | } |
| 3652 | else |
| 3653 | { |
| 3654 | /* |
| 3655 | * Translate string into pattern and expand it. |
| 3656 | */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3657 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 3658 | xp->xp_context)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3659 | p2 = NULL; |
| 3660 | else |
| 3661 | { |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3662 | int use_options = options | |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 3663 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 3664 | if (escape) |
| 3665 | use_options |= WILD_ESCAPE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3666 | |
| 3667 | if (p_wic) |
| 3668 | use_options += WILD_ICASE; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3669 | p2 = ExpandOne(xp, p1, |
| 3670 | vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3671 | use_options, type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3672 | vim_free(p1); |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3673 | /* longest match: make sure it is not shorter, happens with :help */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3674 | if (p2 != NULL && type == WILD_LONGEST) |
| 3675 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3676 | for (j = 0; j < xp->xp_pattern_len; ++j) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3677 | if (ccline.cmdbuff[i + j] == '*' |
| 3678 | || ccline.cmdbuff[i + j] == '?') |
| 3679 | break; |
| 3680 | if ((int)STRLEN(p2) < j) |
| 3681 | { |
| 3682 | vim_free(p2); |
| 3683 | p2 = NULL; |
| 3684 | } |
| 3685 | } |
| 3686 | } |
| 3687 | } |
| 3688 | |
| 3689 | if (p2 != NULL && !got_int) |
| 3690 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3691 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3692 | if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3693 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3694 | v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3695 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3696 | } |
| 3697 | else |
| 3698 | v = OK; |
| 3699 | if (v == OK) |
| 3700 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3701 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 3702 | &ccline.cmdbuff[ccline.cmdpos], |
| 3703 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3704 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3705 | ccline.cmdlen += difflen; |
| 3706 | ccline.cmdpos += difflen; |
| 3707 | } |
| 3708 | } |
| 3709 | vim_free(p2); |
| 3710 | |
| 3711 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3712 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3713 | |
| 3714 | /* When expanding a ":map" command and no matches are found, assume that |
| 3715 | * the key is supposed to be inserted literally */ |
| 3716 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 3717 | return FAIL; |
| 3718 | |
| 3719 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 3720 | beep_flush(); |
| 3721 | else if (xp->xp_numfiles == 1) |
| 3722 | /* free expanded pattern */ |
| 3723 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 3724 | |
| 3725 | return OK; |
| 3726 | } |
| 3727 | |
| 3728 | /* |
| 3729 | * Do wildcard expansion on the string 'str'. |
| 3730 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3731 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3732 | * Return NULL for failure. |
| 3733 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3734 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 3735 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 3736 | * WILD_PREV "orig" should be NULL. |
| 3737 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 3738 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 3739 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3740 | * |
| 3741 | * mode = WILD_FREE: just free previously expanded matches |
| 3742 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 3743 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 3744 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 3745 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 3746 | * mode = WILD_ALL: return all matches concatenated |
| 3747 | * mode = WILD_LONGEST: return longest matched part |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3748 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3749 | * |
| 3750 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 3751 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 3752 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 3753 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 3754 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 3755 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 3756 | * options = WILD_SILENT: don't print warning messages |
| 3757 | * options = WILD_ESCAPE: put backslash before special chars |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3758 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3759 | * |
| 3760 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 3761 | */ |
| 3762 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3763 | ExpandOne( |
| 3764 | expand_T *xp, |
| 3765 | char_u *str, |
| 3766 | char_u *orig, /* allocated copy of original of expanded string */ |
| 3767 | int options, |
| 3768 | int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3769 | { |
| 3770 | char_u *ss = NULL; |
| 3771 | static int findex; |
| 3772 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3773 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3774 | int i; |
| 3775 | long_u len; |
| 3776 | int non_suf_match; /* number without matching suffix */ |
| 3777 | |
| 3778 | /* |
| 3779 | * first handle the case of using an old match |
| 3780 | */ |
| 3781 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 3782 | { |
| 3783 | if (xp->xp_numfiles > 0) |
| 3784 | { |
| 3785 | if (mode == WILD_PREV) |
| 3786 | { |
| 3787 | if (findex == -1) |
| 3788 | findex = xp->xp_numfiles; |
| 3789 | --findex; |
| 3790 | } |
| 3791 | else /* mode == WILD_NEXT */ |
| 3792 | ++findex; |
| 3793 | |
| 3794 | /* |
| 3795 | * When wrapping around, return the original string, set findex to |
| 3796 | * -1. |
| 3797 | */ |
| 3798 | if (findex < 0) |
| 3799 | { |
| 3800 | if (orig_save == NULL) |
| 3801 | findex = xp->xp_numfiles - 1; |
| 3802 | else |
| 3803 | findex = -1; |
| 3804 | } |
| 3805 | if (findex >= xp->xp_numfiles) |
| 3806 | { |
| 3807 | if (orig_save == NULL) |
| 3808 | findex = 0; |
| 3809 | else |
| 3810 | findex = -1; |
| 3811 | } |
| 3812 | #ifdef FEAT_WILDMENU |
| 3813 | if (p_wmnu) |
| 3814 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 3815 | findex, cmd_showtail); |
| 3816 | #endif |
| 3817 | if (findex == -1) |
| 3818 | return vim_strsave(orig_save); |
| 3819 | return vim_strsave(xp->xp_files[findex]); |
| 3820 | } |
| 3821 | else |
| 3822 | return NULL; |
| 3823 | } |
| 3824 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3825 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3826 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 3827 | { |
| 3828 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3829 | xp->xp_numfiles = -1; |
| 3830 | vim_free(orig_save); |
| 3831 | orig_save = NULL; |
| 3832 | } |
| 3833 | findex = 0; |
| 3834 | |
| 3835 | if (mode == WILD_FREE) /* only release file name */ |
| 3836 | return NULL; |
| 3837 | |
| 3838 | if (xp->xp_numfiles == -1) |
| 3839 | { |
| 3840 | vim_free(orig_save); |
| 3841 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3842 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3843 | |
| 3844 | /* |
| 3845 | * Do the expansion. |
| 3846 | */ |
| 3847 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 3848 | options) == FAIL) |
| 3849 | { |
| 3850 | #ifdef FNAME_ILLEGAL |
| 3851 | /* Illegal file name has been silently skipped. But when there |
| 3852 | * are wildcards, the real problem is that there was no match, |
| 3853 | * causing the pattern to be added, which has illegal characters. |
| 3854 | */ |
| 3855 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 3856 | EMSG2(_(e_nomatch2), str); |
| 3857 | #endif |
| 3858 | } |
| 3859 | else if (xp->xp_numfiles == 0) |
| 3860 | { |
| 3861 | if (!(options & WILD_SILENT)) |
| 3862 | EMSG2(_(e_nomatch2), str); |
| 3863 | } |
| 3864 | else |
| 3865 | { |
| 3866 | /* Escape the matches for use on the command line. */ |
| 3867 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 3868 | |
| 3869 | /* |
| 3870 | * Check for matching suffixes in file names. |
| 3871 | */ |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3872 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 3873 | && mode != WILD_LONGEST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3874 | { |
| 3875 | if (xp->xp_numfiles) |
| 3876 | non_suf_match = xp->xp_numfiles; |
| 3877 | else |
| 3878 | non_suf_match = 1; |
| 3879 | if ((xp->xp_context == EXPAND_FILES |
| 3880 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3881 | && xp->xp_numfiles > 1) |
| 3882 | { |
| 3883 | /* |
| 3884 | * More than one match; check suffix. |
| 3885 | * The files will have been sorted on matching suffix in |
| 3886 | * expand_wildcards, only need to check the first two. |
| 3887 | */ |
| 3888 | non_suf_match = 0; |
| 3889 | for (i = 0; i < 2; ++i) |
| 3890 | if (match_suffix(xp->xp_files[i])) |
| 3891 | ++non_suf_match; |
| 3892 | } |
| 3893 | if (non_suf_match != 1) |
| 3894 | { |
| 3895 | /* Can we ever get here unless it's while expanding |
| 3896 | * interactively? If not, we can get rid of this all |
| 3897 | * together. Don't really want to wait for this message |
| 3898 | * (and possibly have to hit return to continue!). |
| 3899 | */ |
| 3900 | if (!(options & WILD_SILENT)) |
| 3901 | EMSG(_(e_toomany)); |
| 3902 | else if (!(options & WILD_NO_BEEP)) |
| 3903 | beep_flush(); |
| 3904 | } |
| 3905 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 3906 | ss = vim_strsave(xp->xp_files[0]); |
| 3907 | } |
| 3908 | } |
| 3909 | } |
| 3910 | |
| 3911 | /* Find longest common part */ |
| 3912 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 3913 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3914 | int mb_len = 1; |
| 3915 | int c0, ci; |
| 3916 | |
| 3917 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3918 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3919 | #ifdef FEAT_MBYTE |
| 3920 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3921 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3922 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 3923 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 3924 | } |
| 3925 | else |
| 3926 | #endif |
Bram Moolenaar | e4eda3b | 2015-11-21 16:28:50 +0100 | [diff] [blame] | 3927 | c0 = xp->xp_files[0][len]; |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3928 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 3929 | { |
| 3930 | #ifdef FEAT_MBYTE |
| 3931 | if (has_mbyte) |
| 3932 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 3933 | else |
| 3934 | #endif |
| 3935 | ci = xp->xp_files[i][len]; |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3936 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3937 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3938 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3939 | || xp->xp_context == EXPAND_BUFFERS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3940 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3941 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3942 | break; |
| 3943 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3944 | else if (c0 != ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3945 | break; |
| 3946 | } |
| 3947 | if (i < xp->xp_numfiles) |
| 3948 | { |
| 3949 | if (!(options & WILD_NO_BEEP)) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3950 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3951 | break; |
| 3952 | } |
| 3953 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3954 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3955 | ss = alloc((unsigned)len + 1); |
| 3956 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3957 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3958 | findex = -1; /* next p_wc gets first one */ |
| 3959 | } |
| 3960 | |
| 3961 | /* Concatenate all matching names */ |
| 3962 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 3963 | { |
| 3964 | len = 0; |
| 3965 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3966 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 3967 | ss = lalloc(len, TRUE); |
| 3968 | if (ss != NULL) |
| 3969 | { |
| 3970 | *ss = NUL; |
| 3971 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3972 | { |
| 3973 | STRCAT(ss, xp->xp_files[i]); |
| 3974 | if (i != xp->xp_numfiles - 1) |
| 3975 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 3976 | } |
| 3977 | } |
| 3978 | } |
| 3979 | |
| 3980 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 3981 | ExpandCleanup(xp); |
| 3982 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3983 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3984 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3985 | vim_free(orig); |
| 3986 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3987 | return ss; |
| 3988 | } |
| 3989 | |
| 3990 | /* |
| 3991 | * Prepare an expand structure for use. |
| 3992 | */ |
| 3993 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3994 | ExpandInit(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3995 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3996 | xp->xp_pattern = NULL; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3997 | xp->xp_pattern_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3998 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3999 | #ifndef BACKSLASH_IN_FILENAME |
| 4000 | xp->xp_shell = FALSE; |
| 4001 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4002 | xp->xp_numfiles = -1; |
| 4003 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 4004 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 4005 | xp->xp_arg = NULL; |
| 4006 | #endif |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 4007 | xp->xp_line = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4008 | } |
| 4009 | |
| 4010 | /* |
| 4011 | * Cleanup an expand structure after use. |
| 4012 | */ |
| 4013 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4014 | ExpandCleanup(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4015 | { |
| 4016 | if (xp->xp_numfiles >= 0) |
| 4017 | { |
| 4018 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4019 | xp->xp_numfiles = -1; |
| 4020 | } |
| 4021 | } |
| 4022 | |
| 4023 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4024 | ExpandEscape( |
| 4025 | expand_T *xp, |
| 4026 | char_u *str, |
| 4027 | int numfiles, |
| 4028 | char_u **files, |
| 4029 | int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4030 | { |
| 4031 | int i; |
| 4032 | char_u *p; |
| 4033 | |
| 4034 | /* |
| 4035 | * May change home directory back to "~" |
| 4036 | */ |
| 4037 | if (options & WILD_HOME_REPLACE) |
| 4038 | tilde_replace(str, numfiles, files); |
| 4039 | |
| 4040 | if (options & WILD_ESCAPE) |
| 4041 | { |
| 4042 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | cca92ec | 2011-04-28 17:21:53 +0200 | [diff] [blame] | 4043 | || xp->xp_context == EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4044 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4045 | || xp->xp_context == EXPAND_BUFFERS |
| 4046 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4047 | { |
| 4048 | /* |
| 4049 | * Insert a backslash into a file name before a space, \, %, # |
| 4050 | * and wildmatch characters, except '~'. |
| 4051 | */ |
| 4052 | for (i = 0; i < numfiles; ++i) |
| 4053 | { |
| 4054 | /* for ":set path=" we need to escape spaces twice */ |
| 4055 | if (xp->xp_backslash == XP_BS_THREE) |
| 4056 | { |
| 4057 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4058 | if (p != NULL) |
| 4059 | { |
| 4060 | vim_free(files[i]); |
| 4061 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 4062 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4063 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4064 | if (p != NULL) |
| 4065 | { |
| 4066 | vim_free(files[i]); |
| 4067 | files[i] = p; |
| 4068 | } |
| 4069 | #endif |
| 4070 | } |
| 4071 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4072 | #ifdef BACKSLASH_IN_FILENAME |
| 4073 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 4074 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4075 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4076 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4077 | if (p != NULL) |
| 4078 | { |
| 4079 | vim_free(files[i]); |
| 4080 | files[i] = p; |
| 4081 | } |
| 4082 | |
| 4083 | /* If 'str' starts with "\~", replace "~" at start of |
| 4084 | * files[i] with "\~". */ |
| 4085 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4086 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4087 | } |
| 4088 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4089 | |
| 4090 | /* If the first file starts with a '+' escape it. Otherwise it |
| 4091 | * could be seen as "+cmd". */ |
| 4092 | if (*files[0] == '+') |
| 4093 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4094 | } |
| 4095 | else if (xp->xp_context == EXPAND_TAGS) |
| 4096 | { |
| 4097 | /* |
| 4098 | * Insert a backslash before characters in a tag name that |
| 4099 | * would terminate the ":tag" command. |
| 4100 | */ |
| 4101 | for (i = 0; i < numfiles; ++i) |
| 4102 | { |
| 4103 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 4104 | if (p != NULL) |
| 4105 | { |
| 4106 | vim_free(files[i]); |
| 4107 | files[i] = p; |
| 4108 | } |
| 4109 | } |
| 4110 | } |
| 4111 | } |
| 4112 | } |
| 4113 | |
| 4114 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4115 | * Escape special characters in "fname" for when used as a file name argument |
| 4116 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 4117 | * Returns the result in allocated memory. |
| 4118 | */ |
| 4119 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4120 | vim_strsave_fnameescape(char_u *fname, int shell) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4121 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4122 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4123 | #ifdef BACKSLASH_IN_FILENAME |
| 4124 | char_u buf[20]; |
| 4125 | int j = 0; |
| 4126 | |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4127 | /* Don't escape '[', '{' and '!' if they are in 'isfname'. */ |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4128 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4129 | if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p)) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4130 | buf[j++] = *p; |
| 4131 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4132 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4133 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4134 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 4135 | if (shell && csh_like_shell() && p != NULL) |
| 4136 | { |
| 4137 | char_u *s; |
| 4138 | |
| 4139 | /* For csh and similar shells need to put two backslashes before '!'. |
| 4140 | * One is taken by Vim, one by the shell. */ |
| 4141 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 4142 | vim_free(p); |
| 4143 | p = s; |
| 4144 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4145 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4146 | |
| 4147 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 4148 | * ":write". "cd -" has a special meaning. */ |
Bram Moolenaar | a9d52e3 | 2010-07-31 16:44:19 +0200 | [diff] [blame] | 4149 | if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4150 | escape_fname(&p); |
| 4151 | |
| 4152 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4153 | } |
| 4154 | |
| 4155 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4156 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 4157 | */ |
| 4158 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4159 | escape_fname(char_u **pp) |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4160 | { |
| 4161 | char_u *p; |
| 4162 | |
| 4163 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 4164 | if (p != NULL) |
| 4165 | { |
| 4166 | p[0] = '\\'; |
| 4167 | STRCPY(p + 1, *pp); |
| 4168 | vim_free(*pp); |
| 4169 | *pp = p; |
| 4170 | } |
| 4171 | } |
| 4172 | |
| 4173 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4174 | * For each file name in files[num_files]: |
| 4175 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 4176 | */ |
| 4177 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4178 | tilde_replace( |
| 4179 | char_u *orig_pat, |
| 4180 | int num_files, |
| 4181 | char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4182 | { |
| 4183 | int i; |
| 4184 | char_u *p; |
| 4185 | |
| 4186 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 4187 | { |
| 4188 | for (i = 0; i < num_files; ++i) |
| 4189 | { |
| 4190 | p = home_replace_save(NULL, files[i]); |
| 4191 | if (p != NULL) |
| 4192 | { |
| 4193 | vim_free(files[i]); |
| 4194 | files[i] = p; |
| 4195 | } |
| 4196 | } |
| 4197 | } |
| 4198 | } |
| 4199 | |
| 4200 | /* |
| 4201 | * Show all matches for completion on the command line. |
| 4202 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 4203 | * be inserted like a normal character. |
| 4204 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4205 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4206 | showmatches(expand_T *xp, int wildmenu UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4207 | { |
| 4208 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 4209 | int num_files; |
| 4210 | char_u **files_found; |
| 4211 | int i, j, k; |
| 4212 | int maxlen; |
| 4213 | int lines; |
| 4214 | int columns; |
| 4215 | char_u *p; |
| 4216 | int lastlen; |
| 4217 | int attr; |
| 4218 | int showtail; |
| 4219 | |
| 4220 | if (xp->xp_numfiles == -1) |
| 4221 | { |
| 4222 | set_expand_context(xp); |
| 4223 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 4224 | &num_files, &files_found); |
| 4225 | showtail = expand_showtail(xp); |
| 4226 | if (i != EXPAND_OK) |
| 4227 | return i; |
| 4228 | |
| 4229 | } |
| 4230 | else |
| 4231 | { |
| 4232 | num_files = xp->xp_numfiles; |
| 4233 | files_found = xp->xp_files; |
| 4234 | showtail = cmd_showtail; |
| 4235 | } |
| 4236 | |
| 4237 | #ifdef FEAT_WILDMENU |
| 4238 | if (!wildmenu) |
| 4239 | { |
| 4240 | #endif |
| 4241 | msg_didany = FALSE; /* lines_left will be set */ |
| 4242 | msg_start(); /* prepare for paging */ |
| 4243 | msg_putchar('\n'); |
| 4244 | out_flush(); |
| 4245 | cmdline_row = msg_row; |
| 4246 | msg_didany = FALSE; /* lines_left will be set again */ |
| 4247 | msg_start(); /* prepare for paging */ |
| 4248 | #ifdef FEAT_WILDMENU |
| 4249 | } |
| 4250 | #endif |
| 4251 | |
| 4252 | if (got_int) |
| 4253 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 4254 | #ifdef FEAT_WILDMENU |
| 4255 | else if (wildmenu) |
Bram Moolenaar | ef8eb08 | 2017-03-30 22:04:55 +0200 | [diff] [blame] | 4256 | win_redr_status_matches(xp, num_files, files_found, -1, showtail); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4257 | #endif |
| 4258 | else |
| 4259 | { |
| 4260 | /* find the length of the longest file name */ |
| 4261 | maxlen = 0; |
| 4262 | for (i = 0; i < num_files; ++i) |
| 4263 | { |
| 4264 | if (!showtail && (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 | { |
| 4268 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 4269 | j = vim_strsize(NameBuff); |
| 4270 | } |
| 4271 | else |
| 4272 | j = vim_strsize(L_SHOWFILE(i)); |
| 4273 | if (j > maxlen) |
| 4274 | maxlen = j; |
| 4275 | } |
| 4276 | |
| 4277 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4278 | lines = num_files; |
| 4279 | else |
| 4280 | { |
| 4281 | /* compute the number of columns and lines for the listing */ |
| 4282 | maxlen += 2; /* two spaces between file names */ |
| 4283 | columns = ((int)Columns + 2) / maxlen; |
| 4284 | if (columns < 1) |
| 4285 | columns = 1; |
| 4286 | lines = (num_files + columns - 1) / columns; |
| 4287 | } |
| 4288 | |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4289 | attr = HL_ATTR(HLF_D); /* find out highlighting for directories */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4290 | |
| 4291 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4292 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4293 | MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4294 | msg_clr_eos(); |
| 4295 | msg_advance(maxlen - 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4296 | MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4297 | } |
| 4298 | |
| 4299 | /* list the files line by line */ |
| 4300 | for (i = 0; i < lines; ++i) |
| 4301 | { |
| 4302 | lastlen = 999; |
| 4303 | for (k = i; k < num_files; k += lines) |
| 4304 | { |
| 4305 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4306 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4307 | msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4308 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 4309 | msg_advance(maxlen + 1); |
| 4310 | msg_puts(p); |
| 4311 | msg_advance(maxlen + 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4312 | msg_puts_long_attr(p + 2, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4313 | break; |
| 4314 | } |
| 4315 | for (j = maxlen - lastlen; --j >= 0; ) |
| 4316 | msg_putchar(' '); |
| 4317 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4318 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4319 | || xp->xp_context == EXPAND_BUFFERS) |
| 4320 | { |
Bram Moolenaar | b91e59b | 2010-03-17 19:13:27 +0100 | [diff] [blame] | 4321 | /* highlight directories */ |
Bram Moolenaar | 63fa526 | 2010-03-23 18:06:52 +0100 | [diff] [blame] | 4322 | if (xp->xp_numfiles != -1) |
| 4323 | { |
| 4324 | char_u *halved_slash; |
| 4325 | char_u *exp_path; |
| 4326 | |
| 4327 | /* Expansion was done before and special characters |
| 4328 | * were escaped, need to halve backslashes. Also |
| 4329 | * $HOME has been replaced with ~/. */ |
| 4330 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
| 4331 | halved_slash = backslash_halve_save( |
| 4332 | exp_path != NULL ? exp_path : files_found[k]); |
| 4333 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 4334 | : files_found[k]); |
| 4335 | vim_free(exp_path); |
| 4336 | vim_free(halved_slash); |
| 4337 | } |
| 4338 | else |
| 4339 | /* Expansion was done here, file names are literal. */ |
| 4340 | j = mch_isdir(files_found[k]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4341 | if (showtail) |
| 4342 | p = L_SHOWFILE(k); |
| 4343 | else |
| 4344 | { |
| 4345 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 4346 | TRUE); |
| 4347 | p = NameBuff; |
| 4348 | } |
| 4349 | } |
| 4350 | else |
| 4351 | { |
| 4352 | j = FALSE; |
| 4353 | p = L_SHOWFILE(k); |
| 4354 | } |
| 4355 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 4356 | } |
| 4357 | if (msg_col > 0) /* when not wrapped around */ |
| 4358 | { |
| 4359 | msg_clr_eos(); |
| 4360 | msg_putchar('\n'); |
| 4361 | } |
| 4362 | out_flush(); /* show one line at a time */ |
| 4363 | if (got_int) |
| 4364 | { |
| 4365 | got_int = FALSE; |
| 4366 | break; |
| 4367 | } |
| 4368 | } |
| 4369 | |
| 4370 | /* |
| 4371 | * we redraw the command below the lines that we have just listed |
| 4372 | * This is a bit tricky, but it saves a lot of screen updating. |
| 4373 | */ |
| 4374 | cmdline_row = msg_row; /* will put it back later */ |
| 4375 | } |
| 4376 | |
| 4377 | if (xp->xp_numfiles == -1) |
| 4378 | FreeWild(num_files, files_found); |
| 4379 | |
| 4380 | return EXPAND_OK; |
| 4381 | } |
| 4382 | |
| 4383 | /* |
| 4384 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 4385 | * Find tail of file name path, but ignore trailing "/". |
| 4386 | */ |
| 4387 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4388 | sm_gettail(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4389 | { |
| 4390 | char_u *p; |
| 4391 | char_u *t = s; |
| 4392 | int had_sep = FALSE; |
| 4393 | |
| 4394 | for (p = s; *p != NUL; ) |
| 4395 | { |
| 4396 | if (vim_ispathsep(*p) |
| 4397 | #ifdef BACKSLASH_IN_FILENAME |
| 4398 | && !rem_backslash(p) |
| 4399 | #endif |
| 4400 | ) |
| 4401 | had_sep = TRUE; |
| 4402 | else if (had_sep) |
| 4403 | { |
| 4404 | t = p; |
| 4405 | had_sep = FALSE; |
| 4406 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4407 | MB_PTR_ADV(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4408 | } |
| 4409 | return t; |
| 4410 | } |
| 4411 | |
| 4412 | /* |
| 4413 | * Return TRUE if we only need to show the tail of completion matches. |
| 4414 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4415 | * returned. |
| 4416 | */ |
| 4417 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4418 | expand_showtail(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4419 | { |
| 4420 | char_u *s; |
| 4421 | char_u *end; |
| 4422 | |
| 4423 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4424 | if (xp->xp_context != EXPAND_FILES |
| 4425 | && xp->xp_context != EXPAND_SHELLCMD |
| 4426 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4427 | return FALSE; |
| 4428 | |
| 4429 | end = gettail(xp->xp_pattern); |
| 4430 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4431 | return FALSE; |
| 4432 | |
| 4433 | for (s = xp->xp_pattern; s < end; s++) |
| 4434 | { |
| 4435 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4436 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4437 | if (rem_backslash(s)) |
| 4438 | ++s; |
| 4439 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4440 | return FALSE; |
| 4441 | } |
| 4442 | return TRUE; |
| 4443 | } |
| 4444 | |
| 4445 | /* |
| 4446 | * Prepare a string for expansion. |
| 4447 | * When expanding file names: The string will be used with expand_wildcards(). |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4448 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4449 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4450 | * the name into allocated memory and prepend "^". |
| 4451 | */ |
| 4452 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4453 | addstar( |
| 4454 | char_u *fname, |
| 4455 | int len, |
| 4456 | int context) /* EXPAND_FILES etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4457 | { |
| 4458 | char_u *retval; |
| 4459 | int i, j; |
| 4460 | int new_len; |
| 4461 | char_u *tail; |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4462 | int ends_in_star; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4463 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4464 | if (context != EXPAND_FILES |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4465 | && context != EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4466 | && context != EXPAND_SHELLCMD |
| 4467 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4468 | { |
| 4469 | /* |
| 4470 | * Matching will be done internally (on something other than files). |
| 4471 | * So we convert the file-matching-type wildcards into our kind for |
| 4472 | * use with vim_regcomp(). First work out how long it will be: |
| 4473 | */ |
| 4474 | |
| 4475 | /* For help tags the translation is done in find_help_tags(). |
| 4476 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4477 | if (context == EXPAND_HELP |
| 4478 | || context == EXPAND_COLORS |
| 4479 | || context == EXPAND_COMPILER |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4480 | || context == EXPAND_OWNSYNTAX |
Bram Moolenaar | 883f5d0 | 2010-06-21 06:24:34 +0200 | [diff] [blame] | 4481 | || context == EXPAND_FILETYPE |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4482 | || context == EXPAND_PACKADD |
Bram Moolenaar | ba47b51 | 2017-01-24 21:18:19 +0100 | [diff] [blame] | 4483 | || ((context == EXPAND_TAGS_LISTFILES |
| 4484 | || context == EXPAND_TAGS) |
| 4485 | && fname[0] == '/')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4486 | retval = vim_strnsave(fname, len); |
| 4487 | else |
| 4488 | { |
| 4489 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4490 | for (i = 0; i < len; i++) |
| 4491 | { |
| 4492 | if (fname[i] == '*' || fname[i] == '~') |
| 4493 | new_len++; /* '*' needs to be replaced by ".*" |
| 4494 | '~' needs to be replaced by "\~" */ |
| 4495 | |
| 4496 | /* Buffer names are like file names. "." should be literal */ |
| 4497 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4498 | new_len++; /* "." becomes "\." */ |
| 4499 | |
| 4500 | /* Custom expansion takes care of special things, match |
| 4501 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4502 | if ((context == EXPAND_USER_DEFINED |
| 4503 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4504 | new_len++; /* '\' becomes "\\" */ |
| 4505 | } |
| 4506 | retval = alloc(new_len); |
| 4507 | if (retval != NULL) |
| 4508 | { |
| 4509 | retval[0] = '^'; |
| 4510 | j = 1; |
| 4511 | for (i = 0; i < len; i++, j++) |
| 4512 | { |
| 4513 | /* Skip backslash. But why? At least keep it for custom |
| 4514 | * expansion. */ |
| 4515 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4516 | && context != EXPAND_USER_LIST |
| 4517 | && fname[i] == '\\' |
| 4518 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4519 | break; |
| 4520 | |
| 4521 | switch (fname[i]) |
| 4522 | { |
| 4523 | case '*': retval[j++] = '.'; |
| 4524 | break; |
| 4525 | case '~': retval[j++] = '\\'; |
| 4526 | break; |
| 4527 | case '?': retval[j] = '.'; |
| 4528 | continue; |
| 4529 | case '.': if (context == EXPAND_BUFFERS) |
| 4530 | retval[j++] = '\\'; |
| 4531 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4532 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4533 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4534 | retval[j++] = '\\'; |
| 4535 | break; |
| 4536 | } |
| 4537 | retval[j] = fname[i]; |
| 4538 | } |
| 4539 | retval[j] = NUL; |
| 4540 | } |
| 4541 | } |
| 4542 | } |
| 4543 | else |
| 4544 | { |
| 4545 | retval = alloc(len + 4); |
| 4546 | if (retval != NULL) |
| 4547 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4548 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4549 | |
| 4550 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4551 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4552 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4553 | * ~ would be at the start of the file name, but not the tail. |
| 4554 | * $ could be anywhere in the tail. |
| 4555 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4556 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4557 | */ |
| 4558 | tail = gettail(retval); |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4559 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 4560 | #ifndef BACKSLASH_IN_FILENAME |
| 4561 | for (i = len - 2; i >= 0; --i) |
| 4562 | { |
| 4563 | if (retval[i] != '\\') |
| 4564 | break; |
| 4565 | ends_in_star = !ends_in_star; |
| 4566 | } |
| 4567 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4568 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4569 | && !ends_in_star |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4570 | && vim_strchr(tail, '$') == NULL |
| 4571 | && vim_strchr(retval, '`') == NULL) |
| 4572 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4573 | else if (len > 0 && retval[len - 1] == '$') |
| 4574 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4575 | retval[len] = NUL; |
| 4576 | } |
| 4577 | } |
| 4578 | return retval; |
| 4579 | } |
| 4580 | |
| 4581 | /* |
| 4582 | * Must parse the command line so far to work out what context we are in. |
| 4583 | * Completion can then be done based on that context. |
| 4584 | * This routine sets the variables: |
| 4585 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4586 | * the command line (ends at the cursor). |
| 4587 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4588 | * |
| 4589 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4590 | * the command line, like an unknown command. Caller |
| 4591 | * should beep. |
| 4592 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4593 | * a normal char, rather than for completion. eg |
| 4594 | * :s/^I/ |
| 4595 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4596 | * it. |
| 4597 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4598 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4599 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4600 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4601 | * when we know only directories are of interest. eg |
| 4602 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4603 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4604 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4605 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4606 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4607 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4608 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4609 | * EXPAND_EVENTS Complete event names |
| 4610 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4611 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4612 | * EXPAND_AUGROUP Complete autocommand group names |
| 4613 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4614 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4615 | * eg :unmap a^I , :cunab x^I |
| 4616 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4617 | * eg :call sub^I |
| 4618 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4619 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4620 | * names in expressions, eg :while s^I |
| 4621 | * EXPAND_ENV_VARS Complete environment variable names |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4622 | * EXPAND_USER Complete user names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4623 | */ |
| 4624 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4625 | set_expand_context(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4626 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4627 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4628 | if (ccline.cmdfirstc != ':' |
| 4629 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4630 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4631 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4632 | #endif |
| 4633 | ) |
| 4634 | { |
| 4635 | xp->xp_context = EXPAND_NOTHING; |
| 4636 | return; |
| 4637 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4638 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4639 | } |
| 4640 | |
| 4641 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4642 | set_cmd_context( |
| 4643 | expand_T *xp, |
| 4644 | char_u *str, /* start of command line */ |
| 4645 | int len, /* length of command line (excl. NUL) */ |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4646 | int col, /* position of cursor */ |
| 4647 | int use_ccline UNUSED) /* use ccline for info */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4648 | { |
| 4649 | int old_char = NUL; |
| 4650 | char_u *nextcomm; |
| 4651 | |
| 4652 | /* |
| 4653 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4654 | * written before. |
| 4655 | */ |
| 4656 | if (col < len) |
| 4657 | old_char = str[col]; |
| 4658 | str[col] = NUL; |
| 4659 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4660 | |
| 4661 | #ifdef FEAT_EVAL |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4662 | if (use_ccline && ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4663 | { |
| 4664 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4665 | /* pass CMD_SIZE because there is no real command */ |
| 4666 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4667 | # endif |
| 4668 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4669 | else if (use_ccline && ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4670 | { |
| 4671 | xp->xp_context = ccline.xp_context; |
| 4672 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4673 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4674 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4675 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4676 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4677 | else |
| 4678 | #endif |
| 4679 | while (nextcomm != NULL) |
| 4680 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4681 | |
Bram Moolenaar | a4c8dcb | 2013-06-30 12:21:24 +0200 | [diff] [blame] | 4682 | /* Store the string here so that call_user_expand_func() can get to them |
| 4683 | * easily. */ |
| 4684 | xp->xp_line = str; |
| 4685 | xp->xp_col = col; |
| 4686 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4687 | str[col] = old_char; |
| 4688 | } |
| 4689 | |
| 4690 | /* |
| 4691 | * Expand the command line "str" from context "xp". |
| 4692 | * "xp" must have been set by set_cmd_context(). |
| 4693 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4694 | * starts. |
| 4695 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4696 | * cursor. |
| 4697 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 4698 | * key that triggered expansion literally. |
| 4699 | * Returns EXPAND_OK otherwise. |
| 4700 | */ |
| 4701 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4702 | expand_cmdline( |
| 4703 | expand_T *xp, |
| 4704 | char_u *str, /* start of command line */ |
| 4705 | int col, /* position of cursor */ |
| 4706 | int *matchcount, /* return: nr of matches */ |
| 4707 | char_u ***matches) /* return: array of pointers to matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4708 | { |
| 4709 | char_u *file_str = NULL; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4710 | int options = WILD_ADD_SLASH|WILD_SILENT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4711 | |
| 4712 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 4713 | { |
| 4714 | beep_flush(); |
| 4715 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 4716 | } |
| 4717 | if (xp->xp_context == EXPAND_NOTHING) |
| 4718 | { |
| 4719 | /* Caller can use the character as a normal char instead */ |
| 4720 | return EXPAND_NOTHING; |
| 4721 | } |
| 4722 | |
| 4723 | /* 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] | 4724 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 4725 | 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] | 4726 | if (file_str == NULL) |
| 4727 | return EXPAND_UNSUCCESSFUL; |
| 4728 | |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4729 | if (p_wic) |
| 4730 | options += WILD_ICASE; |
| 4731 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4732 | /* find all files that match the description */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4733 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4734 | { |
| 4735 | *matchcount = 0; |
| 4736 | *matches = NULL; |
| 4737 | } |
| 4738 | vim_free(file_str); |
| 4739 | |
| 4740 | return EXPAND_OK; |
| 4741 | } |
| 4742 | |
| 4743 | #ifdef FEAT_MULTI_LANG |
| 4744 | /* |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4745 | * Cleanup matches for help tags: |
| 4746 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 4747 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4748 | */ |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 4749 | static void cleanup_help_tags(int num_file, char_u **file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4750 | |
| 4751 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4752 | cleanup_help_tags(int num_file, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4753 | { |
| 4754 | int i, j; |
| 4755 | int len; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4756 | char_u buf[4]; |
| 4757 | char_u *p = buf; |
| 4758 | |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4759 | 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] | 4760 | { |
| 4761 | *p++ = '@'; |
| 4762 | *p++ = p_hlg[0]; |
| 4763 | *p++ = p_hlg[1]; |
| 4764 | } |
| 4765 | *p = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4766 | |
| 4767 | for (i = 0; i < num_file; ++i) |
| 4768 | { |
| 4769 | len = (int)STRLEN(file[i]) - 3; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4770 | if (len <= 0) |
| 4771 | continue; |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4772 | if (STRCMP(file[i] + len, "@en") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4773 | { |
| 4774 | /* Sorting on priority means the same item in another language may |
| 4775 | * be anywhere. Search all items for a match up to the "@en". */ |
| 4776 | for (j = 0; j < num_file; ++j) |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4777 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 4778 | && STRNCMP(file[i], file[j], len + 1) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4779 | break; |
| 4780 | if (j == num_file) |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4781 | /* item only exists with @en, remove it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4782 | file[i][len] = NUL; |
| 4783 | } |
| 4784 | } |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4785 | |
| 4786 | if (*buf != NUL) |
| 4787 | for (i = 0; i < num_file; ++i) |
| 4788 | { |
| 4789 | len = (int)STRLEN(file[i]) - 3; |
| 4790 | if (len <= 0) |
| 4791 | continue; |
| 4792 | if (STRCMP(file[i] + len, buf) == 0) |
| 4793 | { |
| 4794 | /* remove the default language */ |
| 4795 | file[i][len] = NUL; |
| 4796 | } |
| 4797 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4798 | } |
| 4799 | #endif |
| 4800 | |
| 4801 | /* |
| 4802 | * Do the expansion based on xp->xp_context and "pat". |
| 4803 | */ |
| 4804 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4805 | ExpandFromContext( |
| 4806 | expand_T *xp, |
| 4807 | char_u *pat, |
| 4808 | int *num_file, |
| 4809 | char_u ***file, |
| 4810 | int options) /* EW_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4811 | { |
| 4812 | #ifdef FEAT_CMDL_COMPL |
| 4813 | regmatch_T regmatch; |
| 4814 | #endif |
| 4815 | int ret; |
| 4816 | int flags; |
| 4817 | |
| 4818 | flags = EW_DIR; /* include directories */ |
| 4819 | if (options & WILD_LIST_NOTFOUND) |
| 4820 | flags |= EW_NOTFOUND; |
| 4821 | if (options & WILD_ADD_SLASH) |
| 4822 | flags |= EW_ADDSLASH; |
| 4823 | if (options & WILD_KEEP_ALL) |
| 4824 | flags |= EW_KEEPALL; |
| 4825 | if (options & WILD_SILENT) |
| 4826 | flags |= EW_SILENT; |
Bram Moolenaar | a245bc7 | 2015-03-05 19:35:25 +0100 | [diff] [blame] | 4827 | if (options & WILD_ALLLINKS) |
| 4828 | flags |= EW_ALLLINKS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4829 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4830 | if (xp->xp_context == EXPAND_FILES |
| 4831 | || xp->xp_context == EXPAND_DIRECTORIES |
| 4832 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4833 | { |
| 4834 | /* |
| 4835 | * Expand file or directory names. |
| 4836 | */ |
| 4837 | int free_pat = FALSE; |
| 4838 | int i; |
| 4839 | |
| 4840 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 4841 | * space */ |
| 4842 | if (xp->xp_backslash != XP_BS_NONE) |
| 4843 | { |
| 4844 | free_pat = TRUE; |
| 4845 | pat = vim_strsave(pat); |
| 4846 | for (i = 0; pat[i]; ++i) |
| 4847 | if (pat[i] == '\\') |
| 4848 | { |
| 4849 | if (xp->xp_backslash == XP_BS_THREE |
| 4850 | && pat[i + 1] == '\\' |
| 4851 | && pat[i + 2] == '\\' |
| 4852 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4853 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4854 | if (xp->xp_backslash == XP_BS_ONE |
| 4855 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4856 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4857 | } |
| 4858 | } |
| 4859 | |
| 4860 | if (xp->xp_context == EXPAND_FILES) |
| 4861 | flags |= EW_FILE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4862 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 4863 | flags |= (EW_FILE | EW_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4864 | else |
| 4865 | flags = (flags | EW_DIR) & ~EW_FILE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4866 | if (options & WILD_ICASE) |
| 4867 | flags |= EW_ICASE; |
| 4868 | |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 4869 | /* Expand wildcards, supporting %:h and the like. */ |
| 4870 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4871 | if (free_pat) |
| 4872 | vim_free(pat); |
| 4873 | return ret; |
| 4874 | } |
| 4875 | |
| 4876 | *file = (char_u **)""; |
| 4877 | *num_file = 0; |
| 4878 | if (xp->xp_context == EXPAND_HELP) |
| 4879 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 4880 | /* With an empty argument we would get all the help tags, which is |
| 4881 | * very slow. Get matches for "help" instead. */ |
| 4882 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 4883 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4884 | { |
| 4885 | #ifdef FEAT_MULTI_LANG |
| 4886 | cleanup_help_tags(*num_file, *file); |
| 4887 | #endif |
| 4888 | return OK; |
| 4889 | } |
| 4890 | return FAIL; |
| 4891 | } |
| 4892 | |
| 4893 | #ifndef FEAT_CMDL_COMPL |
| 4894 | return FAIL; |
| 4895 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4896 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 4897 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4898 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 4899 | return ExpandOldSetting(num_file, file); |
| 4900 | if (xp->xp_context == EXPAND_BUFFERS) |
| 4901 | return ExpandBufnames(pat, num_file, file, options); |
| 4902 | if (xp->xp_context == EXPAND_TAGS |
| 4903 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4904 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 4905 | if (xp->xp_context == EXPAND_COLORS) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4906 | { |
| 4907 | char *directories[] = {"colors", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4908 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 4909 | directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4910 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4911 | if (xp->xp_context == EXPAND_COMPILER) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4912 | { |
Bram Moolenaar | a627c96 | 2011-09-30 16:23:32 +0200 | [diff] [blame] | 4913 | char *directories[] = {"compiler", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4914 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4915 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4916 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4917 | { |
| 4918 | char *directories[] = {"syntax", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4919 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4920 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4921 | if (xp->xp_context == EXPAND_FILETYPE) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4922 | { |
| 4923 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4924 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4925 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4926 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4927 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4928 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4929 | # endif |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4930 | if (xp->xp_context == EXPAND_PACKADD) |
| 4931 | return ExpandPackAddDir(pat, num_file, file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4932 | |
| 4933 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 4934 | if (regmatch.regprog == NULL) |
| 4935 | return FAIL; |
| 4936 | |
| 4937 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 4938 | regmatch.rm_ic = ignorecase(pat); |
| 4939 | |
| 4940 | if (xp->xp_context == EXPAND_SETTINGS |
| 4941 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 4942 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 4943 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 4944 | ret = ExpandMappings(®match, num_file, file); |
| 4945 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4946 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 4947 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 4948 | # endif |
| 4949 | else |
| 4950 | { |
| 4951 | static struct expgen |
| 4952 | { |
| 4953 | int context; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 4954 | char_u *((*func)(expand_T *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4955 | int ic; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4956 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4957 | } tab[] = |
| 4958 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4959 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 4960 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
Bram Moolenaar | cae92dc | 2017-08-06 15:22:15 +0200 | [diff] [blame] | 4961 | {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE}, |
Bram Moolenaar | 9e507ca | 2016-10-15 15:39:39 +0200 | [diff] [blame] | 4962 | {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE}, |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 4963 | #ifdef FEAT_CMDHIST |
| 4964 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 4965 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4966 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4967 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 4968 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4969 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 4970 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 4971 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4972 | #endif |
| 4973 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4974 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 4975 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 4976 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 4977 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4978 | #endif |
| 4979 | #ifdef FEAT_MENU |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4980 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 4981 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4982 | #endif |
| 4983 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4984 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4985 | #endif |
Bram Moolenaar | cd9c462 | 2013-06-08 15:24:48 +0200 | [diff] [blame] | 4986 | #ifdef FEAT_PROFILE |
| 4987 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 4988 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4989 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4990 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4991 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 4992 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4993 | #endif |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4994 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4995 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4996 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4997 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4998 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4999 | #endif |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5000 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5001 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 5002 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5003 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 5004 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5005 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 5006 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5007 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5008 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 5009 | {EXPAND_USER, get_users, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5010 | }; |
| 5011 | int i; |
| 5012 | |
| 5013 | /* |
| 5014 | * Find a context in the table and call the ExpandGeneric() with the |
| 5015 | * right function to do the expansion. |
| 5016 | */ |
| 5017 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 5018 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5019 | if (xp->xp_context == tab[i].context) |
| 5020 | { |
| 5021 | if (tab[i].ic) |
| 5022 | regmatch.rm_ic = TRUE; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5023 | ret = ExpandGeneric(xp, ®match, num_file, file, |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5024 | tab[i].func, tab[i].escaped); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5025 | break; |
| 5026 | } |
| 5027 | } |
| 5028 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5029 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5030 | |
| 5031 | return ret; |
| 5032 | #endif /* FEAT_CMDL_COMPL */ |
| 5033 | } |
| 5034 | |
| 5035 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5036 | /* |
| 5037 | * Expand a list of names. |
| 5038 | * |
| 5039 | * Generic function for command line completion. It calls a function to |
| 5040 | * obtain strings, one by one. The strings are matched against a regexp |
| 5041 | * program. Matching strings are copied into an array, which is returned. |
| 5042 | * |
| 5043 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 5044 | */ |
| 5045 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5046 | ExpandGeneric( |
| 5047 | expand_T *xp, |
| 5048 | regmatch_T *regmatch, |
| 5049 | int *num_file, |
| 5050 | char_u ***file, |
| 5051 | char_u *((*func)(expand_T *, int)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5052 | /* returns a string from the list */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5053 | int escaped) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5054 | { |
| 5055 | int i; |
| 5056 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5057 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5058 | char_u *str; |
| 5059 | |
| 5060 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5061 | * round == 0: count the number of matching names |
| 5062 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5063 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5064 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5065 | { |
| 5066 | for (i = 0; ; ++i) |
| 5067 | { |
| 5068 | str = (*func)(xp, i); |
| 5069 | if (str == NULL) /* end of list */ |
| 5070 | break; |
| 5071 | if (*str == NUL) /* skip empty strings */ |
| 5072 | continue; |
| 5073 | |
| 5074 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 5075 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5076 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5077 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5078 | if (escaped) |
| 5079 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 5080 | else |
| 5081 | str = vim_strsave(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5082 | (*file)[count] = str; |
| 5083 | #ifdef FEAT_MENU |
| 5084 | if (func == get_menu_names && str != NULL) |
| 5085 | { |
| 5086 | /* test for separator added by get_menu_names() */ |
| 5087 | str += STRLEN(str) - 1; |
| 5088 | if (*str == '\001') |
| 5089 | *str = '.'; |
| 5090 | } |
| 5091 | #endif |
| 5092 | } |
| 5093 | ++count; |
| 5094 | } |
| 5095 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5096 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5097 | { |
| 5098 | if (count == 0) |
| 5099 | return OK; |
| 5100 | *num_file = count; |
| 5101 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 5102 | if (*file == NULL) |
| 5103 | { |
| 5104 | *file = (char_u **)""; |
| 5105 | return FAIL; |
| 5106 | } |
| 5107 | count = 0; |
| 5108 | } |
| 5109 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5110 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 5111 | /* Sort the results. Keep menu's in the specified order. */ |
| 5112 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 5113 | { |
| 5114 | if (xp->xp_context == EXPAND_EXPRESSION |
| 5115 | || xp->xp_context == EXPAND_FUNCTIONS |
| 5116 | || xp->xp_context == EXPAND_USER_FUNC) |
| 5117 | /* <SNR> functions should be sorted to the end. */ |
| 5118 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 5119 | sort_func_compare); |
| 5120 | else |
| 5121 | sort_strings(*file, *num_file); |
| 5122 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5123 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 5124 | #ifdef FEAT_CMDL_COMPL |
| 5125 | /* Reset the variables used for special highlight names expansion, so that |
| 5126 | * they don't show up when getting normal highlight names by ID. */ |
| 5127 | reset_expand_highlight(); |
| 5128 | #endif |
| 5129 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5130 | return OK; |
| 5131 | } |
| 5132 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5133 | /* |
| 5134 | * Complete a shell command. |
| 5135 | * Returns FAIL or OK; |
| 5136 | */ |
| 5137 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5138 | expand_shellcmd( |
| 5139 | char_u *filepat, /* pattern to match with command names */ |
| 5140 | int *num_file, /* return: number of matches */ |
| 5141 | char_u ***file, /* return: array with matches */ |
| 5142 | int flagsarg) /* EW_ flags */ |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5143 | { |
| 5144 | char_u *pat; |
| 5145 | int i; |
| 5146 | char_u *path; |
| 5147 | int mustfree = FALSE; |
| 5148 | garray_T ga; |
| 5149 | char_u *buf = alloc(MAXPATHL); |
| 5150 | size_t l; |
| 5151 | char_u *s, *e; |
| 5152 | int flags = flagsarg; |
| 5153 | int ret; |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5154 | int did_curdir = FALSE; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5155 | |
| 5156 | if (buf == NULL) |
| 5157 | return FAIL; |
| 5158 | |
| 5159 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5160 | * space */ |
| 5161 | pat = vim_strsave(filepat); |
| 5162 | for (i = 0; pat[i]; ++i) |
| 5163 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5164 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5165 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5166 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5167 | |
| 5168 | /* For an absolute name we don't use $PATH. */ |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 5169 | if (mch_isFullName(pat)) |
| 5170 | path = (char_u *)" "; |
| 5171 | else if ((pat[0] == '.' && (vim_ispathsep(pat[1]) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5172 | || (pat[1] == '.' && vim_ispathsep(pat[2]))))) |
| 5173 | path = (char_u *)"."; |
| 5174 | else |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5175 | { |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5176 | path = vim_getenv((char_u *)"PATH", &mustfree); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5177 | if (path == NULL) |
| 5178 | path = (char_u *)""; |
| 5179 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5180 | |
| 5181 | /* |
| 5182 | * Go over all directories in $PATH. Expand matches in that directory and |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5183 | * collect them in "ga". When "." is not in $PATH also expand for the |
| 5184 | * current directory, to find "subdir/cmd". |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5185 | */ |
| 5186 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5187 | for (s = path; ; s = e) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5188 | { |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5189 | if (*s == NUL) |
| 5190 | { |
| 5191 | if (did_curdir) |
| 5192 | break; |
| 5193 | /* Find directories in the current directory, path is empty. */ |
| 5194 | did_curdir = TRUE; |
| 5195 | } |
| 5196 | else if (*s == '.') |
| 5197 | did_curdir = TRUE; |
| 5198 | |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 5199 | if (*s == ' ') |
| 5200 | ++s; /* Skip space used for absolute path name. */ |
| 5201 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5202 | #if defined(MSWIN) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5203 | e = vim_strchr(s, ';'); |
| 5204 | #else |
| 5205 | e = vim_strchr(s, ':'); |
| 5206 | #endif |
| 5207 | if (e == NULL) |
| 5208 | e = s + STRLEN(s); |
| 5209 | |
| 5210 | l = e - s; |
| 5211 | if (l > MAXPATHL - 5) |
| 5212 | break; |
| 5213 | vim_strncpy(buf, s, l); |
| 5214 | add_pathsep(buf); |
| 5215 | l = STRLEN(buf); |
| 5216 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 5217 | |
| 5218 | /* Expand matches in one directory of $PATH. */ |
| 5219 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 5220 | if (ret == OK) |
| 5221 | { |
| 5222 | if (ga_grow(&ga, *num_file) == FAIL) |
| 5223 | FreeWild(*num_file, *file); |
| 5224 | else |
| 5225 | { |
| 5226 | for (i = 0; i < *num_file; ++i) |
| 5227 | { |
| 5228 | s = (*file)[i]; |
| 5229 | if (STRLEN(s) > l) |
| 5230 | { |
| 5231 | /* Remove the path again. */ |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5232 | STRMOVE(s, s + l); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5233 | ((char_u **)ga.ga_data)[ga.ga_len++] = s; |
| 5234 | } |
| 5235 | else |
| 5236 | vim_free(s); |
| 5237 | } |
| 5238 | vim_free(*file); |
| 5239 | } |
| 5240 | } |
| 5241 | if (*e != NUL) |
| 5242 | ++e; |
| 5243 | } |
| 5244 | *file = ga.ga_data; |
| 5245 | *num_file = ga.ga_len; |
| 5246 | |
| 5247 | vim_free(buf); |
| 5248 | vim_free(pat); |
| 5249 | if (mustfree) |
| 5250 | vim_free(path); |
| 5251 | return OK; |
| 5252 | } |
| 5253 | |
| 5254 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5255 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 5256 | 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] | 5257 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5258 | /* |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 5259 | * Call "user_expand_func()" to invoke a user defined Vim script function and |
| 5260 | * return the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5261 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5262 | static void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5263 | call_user_expand_func( |
| 5264 | void *(*user_expand_func)(char_u *, int, char_u **, int), |
| 5265 | expand_T *xp, |
| 5266 | int *num_file, |
| 5267 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5268 | { |
Bram Moolenaar | 673b9a3 | 2013-06-30 22:43:27 +0200 | [diff] [blame] | 5269 | int keep = 0; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5270 | char_u num[50]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5271 | char_u *args[3]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5272 | int save_current_SID = current_SID; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5273 | void *ret; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5274 | struct cmdline_info save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5275 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5276 | 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] | 5277 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5278 | *num_file = 0; |
| 5279 | *file = NULL; |
| 5280 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5281 | if (ccline.cmdbuff != NULL) |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5282 | { |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5283 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 5284 | ccline.cmdbuff[ccline.cmdlen] = 0; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5285 | } |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5286 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5287 | args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5288 | args[1] = xp->xp_line; |
| 5289 | sprintf((char *)num, "%d", xp->xp_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5290 | args[2] = num; |
| 5291 | |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5292 | /* Save the cmdline, we don't know what the function may do. */ |
| 5293 | save_ccline = ccline; |
| 5294 | ccline.cmdbuff = NULL; |
| 5295 | ccline.cmdprompt = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5296 | current_SID = xp->xp_scriptID; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5297 | |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5298 | ret = user_expand_func(xp->xp_arg, 3, args, FALSE); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5299 | |
| 5300 | ccline = save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5301 | current_SID = save_current_SID; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5302 | if (ccline.cmdbuff != NULL) |
| 5303 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5304 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5305 | vim_free(args[0]); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5306 | return ret; |
| 5307 | } |
| 5308 | |
| 5309 | /* |
| 5310 | * Expand names with a function defined by the user. |
| 5311 | */ |
| 5312 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5313 | ExpandUserDefined( |
| 5314 | expand_T *xp, |
| 5315 | regmatch_T *regmatch, |
| 5316 | int *num_file, |
| 5317 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5318 | { |
| 5319 | char_u *retstr; |
| 5320 | char_u *s; |
| 5321 | char_u *e; |
| 5322 | char_u keep; |
| 5323 | garray_T ga; |
| 5324 | |
| 5325 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 5326 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5327 | return FAIL; |
| 5328 | |
| 5329 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5330 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5331 | { |
| 5332 | e = vim_strchr(s, '\n'); |
| 5333 | if (e == NULL) |
| 5334 | e = s + STRLEN(s); |
| 5335 | keep = *e; |
| 5336 | *e = 0; |
| 5337 | |
| 5338 | if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0) |
| 5339 | { |
| 5340 | *e = keep; |
| 5341 | if (*e != NUL) |
| 5342 | ++e; |
| 5343 | continue; |
| 5344 | } |
| 5345 | |
| 5346 | if (ga_grow(&ga, 1) == FAIL) |
| 5347 | break; |
| 5348 | |
| 5349 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 5350 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5351 | |
| 5352 | *e = keep; |
| 5353 | if (*e != NUL) |
| 5354 | ++e; |
| 5355 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5356 | vim_free(retstr); |
| 5357 | *file = ga.ga_data; |
| 5358 | *num_file = ga.ga_len; |
| 5359 | return OK; |
| 5360 | } |
| 5361 | |
| 5362 | /* |
| 5363 | * Expand names with a list returned by a function defined by the user. |
| 5364 | */ |
| 5365 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5366 | ExpandUserList( |
| 5367 | expand_T *xp, |
| 5368 | int *num_file, |
| 5369 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5370 | { |
| 5371 | list_T *retlist; |
| 5372 | listitem_T *li; |
| 5373 | garray_T ga; |
| 5374 | |
| 5375 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 5376 | if (retlist == NULL) |
| 5377 | return FAIL; |
| 5378 | |
| 5379 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 5380 | /* Loop over the items in the list. */ |
| 5381 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 5382 | { |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5383 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 5384 | continue; /* Skip non-string items and empty strings */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5385 | |
| 5386 | if (ga_grow(&ga, 1) == FAIL) |
| 5387 | break; |
| 5388 | |
| 5389 | ((char_u **)ga.ga_data)[ga.ga_len] = |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5390 | vim_strsave(li->li_tv.vval.v_string); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5391 | ++ga.ga_len; |
| 5392 | } |
| 5393 | list_unref(retlist); |
| 5394 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5395 | *file = ga.ga_data; |
| 5396 | *num_file = ga.ga_len; |
| 5397 | return OK; |
| 5398 | } |
| 5399 | #endif |
| 5400 | |
| 5401 | /* |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5402 | * Expand color scheme, compiler or filetype names. |
| 5403 | * Search from 'runtimepath': |
| 5404 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 5405 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 5406 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 5407 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 5408 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5409 | * "dirnames" is an array with one or more directory names. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5410 | */ |
| 5411 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5412 | ExpandRTDir( |
| 5413 | char_u *pat, |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5414 | int flags, |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5415 | int *num_file, |
| 5416 | char_u ***file, |
| 5417 | char *dirnames[]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5418 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5419 | char_u *s; |
| 5420 | char_u *e; |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5421 | char_u *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5422 | garray_T ga; |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5423 | int i; |
| 5424 | int pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5425 | |
| 5426 | *num_file = 0; |
| 5427 | *file = NULL; |
Bram Moolenaar | 5cfe2d7 | 2011-07-07 15:04:52 +0200 | [diff] [blame] | 5428 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5429 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5430 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5431 | for (i = 0; dirnames[i] != NULL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5432 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5433 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
| 5434 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5435 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5436 | ga_clear_strings(&ga); |
| 5437 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5438 | } |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5439 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5440 | globpath(p_rtp, s, &ga, 0); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5441 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5442 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5443 | |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5444 | if (flags & DIP_START) { |
| 5445 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5446 | { |
| 5447 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22)); |
| 5448 | if (s == NULL) |
| 5449 | { |
| 5450 | ga_clear_strings(&ga); |
| 5451 | return FAIL; |
| 5452 | } |
| 5453 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 5454 | globpath(p_pp, s, &ga, 0); |
| 5455 | vim_free(s); |
| 5456 | } |
| 5457 | } |
| 5458 | |
| 5459 | if (flags & DIP_OPT) { |
| 5460 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5461 | { |
| 5462 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20)); |
| 5463 | if (s == NULL) |
| 5464 | { |
| 5465 | ga_clear_strings(&ga); |
| 5466 | return FAIL; |
| 5467 | } |
| 5468 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 5469 | globpath(p_pp, s, &ga, 0); |
| 5470 | vim_free(s); |
| 5471 | } |
| 5472 | } |
| 5473 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5474 | for (i = 0; i < ga.ga_len; ++i) |
| 5475 | { |
| 5476 | match = ((char_u **)ga.ga_data)[i]; |
| 5477 | s = match; |
| 5478 | e = s + STRLEN(s); |
| 5479 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 5480 | { |
| 5481 | e -= 4; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5482 | for (s = e; s > match; MB_PTR_BACK(match, s)) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5483 | if (s < match || vim_ispathsep(*s)) |
| 5484 | break; |
| 5485 | ++s; |
| 5486 | *e = NUL; |
| 5487 | mch_memmove(match, s, e - s + 1); |
| 5488 | } |
| 5489 | } |
| 5490 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5491 | if (ga.ga_len == 0) |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5492 | return FAIL; |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5493 | |
| 5494 | /* Sort and remove duplicates which can happen when specifying multiple |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5495 | * directories in dirnames. */ |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5496 | remove_duplicates(&ga); |
| 5497 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5498 | *file = ga.ga_data; |
| 5499 | *num_file = ga.ga_len; |
| 5500 | return OK; |
| 5501 | } |
| 5502 | |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5503 | /* |
| 5504 | * Expand loadplugin names: |
| 5505 | * 'packpath'/pack/ * /opt/{pat} |
| 5506 | */ |
| 5507 | static int |
| 5508 | ExpandPackAddDir( |
| 5509 | char_u *pat, |
| 5510 | int *num_file, |
| 5511 | char_u ***file) |
| 5512 | { |
| 5513 | char_u *s; |
| 5514 | char_u *e; |
| 5515 | char_u *match; |
| 5516 | garray_T ga; |
| 5517 | int i; |
| 5518 | int pat_len; |
| 5519 | |
| 5520 | *num_file = 0; |
| 5521 | *file = NULL; |
| 5522 | pat_len = (int)STRLEN(pat); |
| 5523 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 5524 | |
| 5525 | s = alloc((unsigned)(pat_len + 26)); |
| 5526 | if (s == NULL) |
| 5527 | { |
| 5528 | ga_clear_strings(&ga); |
| 5529 | return FAIL; |
| 5530 | } |
| 5531 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 5532 | globpath(p_pp, s, &ga, 0); |
| 5533 | vim_free(s); |
| 5534 | |
| 5535 | for (i = 0; i < ga.ga_len; ++i) |
| 5536 | { |
| 5537 | match = ((char_u **)ga.ga_data)[i]; |
| 5538 | s = gettail(match); |
| 5539 | e = s + STRLEN(s); |
| 5540 | mch_memmove(match, s, e - s + 1); |
| 5541 | } |
| 5542 | |
| 5543 | if (ga.ga_len == 0) |
| 5544 | return FAIL; |
| 5545 | |
| 5546 | /* Sort and remove duplicates which can happen when specifying multiple |
| 5547 | * directories in dirnames. */ |
| 5548 | remove_duplicates(&ga); |
| 5549 | |
| 5550 | *file = ga.ga_data; |
| 5551 | *num_file = ga.ga_len; |
| 5552 | return OK; |
| 5553 | } |
| 5554 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5555 | #endif |
| 5556 | |
| 5557 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 5558 | /* |
| 5559 | * Expand "file" for all comma-separated directories in "path". |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5560 | * Adds the matches to "ga". Caller must init "ga". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5561 | */ |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5562 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5563 | globpath( |
| 5564 | char_u *path, |
| 5565 | char_u *file, |
| 5566 | garray_T *ga, |
| 5567 | int expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5568 | { |
| 5569 | expand_T xpc; |
| 5570 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5571 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5572 | int num_p; |
| 5573 | char_u **p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5574 | |
| 5575 | buf = alloc(MAXPATHL); |
| 5576 | if (buf == NULL) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5577 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5578 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5579 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5580 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5581 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5582 | /* Loop over all entries in {path}. */ |
| 5583 | while (*path != NUL) |
| 5584 | { |
| 5585 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 5586 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 5587 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 5588 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5589 | # if defined(MSWIN) |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5590 | /* Using the platform's path separator (\) makes vim incorrectly |
| 5591 | * treat it as an escape character, use '/' instead. */ |
| 5592 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 5593 | STRCAT(buf, "/"); |
| 5594 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5595 | add_pathsep(buf); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5596 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5597 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5598 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 5599 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5600 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5601 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5602 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5603 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5604 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5605 | for (i = 0; i < num_p; ++i) |
| 5606 | { |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5607 | ((char_u **)ga->ga_data)[ga->ga_len] = |
Bram Moolenaar | 7116aa0 | 2014-05-29 14:36:29 +0200 | [diff] [blame] | 5608 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5609 | ++ga->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5610 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5611 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5612 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5613 | FreeWild(num_p, p); |
| 5614 | } |
| 5615 | } |
| 5616 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5617 | |
| 5618 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5619 | } |
| 5620 | |
| 5621 | #endif |
| 5622 | |
| 5623 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5624 | |
| 5625 | /********************************* |
| 5626 | * Command line history stuff * |
| 5627 | *********************************/ |
| 5628 | |
| 5629 | /* |
| 5630 | * Translate a history character to the associated type number. |
| 5631 | */ |
| 5632 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5633 | hist_char2type(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5634 | { |
| 5635 | if (c == ':') |
| 5636 | return HIST_CMD; |
| 5637 | if (c == '=') |
| 5638 | return HIST_EXPR; |
| 5639 | if (c == '@') |
| 5640 | return HIST_INPUT; |
| 5641 | if (c == '>') |
| 5642 | return HIST_DEBUG; |
| 5643 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5644 | } |
| 5645 | |
| 5646 | /* |
| 5647 | * Table of history names. |
| 5648 | * These names are used in :history and various hist...() functions. |
| 5649 | * It is sufficient to give the significant prefix of a history name. |
| 5650 | */ |
| 5651 | |
| 5652 | static char *(history_names[]) = |
| 5653 | { |
| 5654 | "cmd", |
| 5655 | "search", |
| 5656 | "expr", |
| 5657 | "input", |
| 5658 | "debug", |
| 5659 | NULL |
| 5660 | }; |
| 5661 | |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5662 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5663 | /* |
| 5664 | * Function given to ExpandGeneric() to obtain the possible first |
| 5665 | * arguments of the ":history command. |
| 5666 | */ |
| 5667 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5668 | get_history_arg(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5669 | { |
| 5670 | static char_u compl[2] = { NUL, NUL }; |
| 5671 | char *short_names = ":=@>?/"; |
Bram Moolenaar | 17bd9dc | 2012-05-25 11:02:41 +0200 | [diff] [blame] | 5672 | int short_names_count = (int)STRLEN(short_names); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5673 | int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
| 5674 | |
| 5675 | if (idx < short_names_count) |
| 5676 | { |
| 5677 | compl[0] = (char_u)short_names[idx]; |
| 5678 | return compl; |
| 5679 | } |
| 5680 | if (idx < short_names_count + history_name_count) |
| 5681 | return (char_u *)history_names[idx - short_names_count]; |
| 5682 | if (idx == short_names_count + history_name_count) |
| 5683 | return (char_u *)"all"; |
| 5684 | return NULL; |
| 5685 | } |
| 5686 | #endif |
| 5687 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5688 | /* |
| 5689 | * init_history() - Initialize the command line history. |
| 5690 | * Also used to re-allocate the history when the size changes. |
| 5691 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 5692 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5693 | init_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5694 | { |
| 5695 | int newlen; /* new length of history table */ |
| 5696 | histentry_T *temp; |
| 5697 | int i; |
| 5698 | int j; |
| 5699 | int type; |
| 5700 | |
| 5701 | /* |
| 5702 | * If size of history table changed, reallocate it |
| 5703 | */ |
| 5704 | newlen = (int)p_hi; |
| 5705 | if (newlen != hislen) /* history length changed */ |
| 5706 | { |
| 5707 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 5708 | { |
| 5709 | if (newlen) |
| 5710 | { |
| 5711 | temp = (histentry_T *)lalloc( |
| 5712 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 5713 | if (temp == NULL) /* out of memory! */ |
| 5714 | { |
| 5715 | if (type == 0) /* first one: just keep the old length */ |
| 5716 | { |
| 5717 | newlen = hislen; |
| 5718 | break; |
| 5719 | } |
| 5720 | /* Already changed one table, now we can only have zero |
| 5721 | * length for all tables. */ |
| 5722 | newlen = 0; |
| 5723 | type = -1; |
| 5724 | continue; |
| 5725 | } |
| 5726 | } |
| 5727 | else |
| 5728 | temp = NULL; |
| 5729 | if (newlen == 0 || temp != NULL) |
| 5730 | { |
| 5731 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 5732 | { |
| 5733 | for (i = 0; i < newlen; ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5734 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5735 | } |
| 5736 | else if (newlen > hislen) /* array becomes bigger */ |
| 5737 | { |
| 5738 | for (i = 0; i <= hisidx[type]; ++i) |
| 5739 | temp[i] = history[type][i]; |
| 5740 | j = i; |
| 5741 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5742 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5743 | for ( ; j < hislen; ++i, ++j) |
| 5744 | temp[i] = history[type][j]; |
| 5745 | } |
| 5746 | else /* array becomes smaller or 0 */ |
| 5747 | { |
| 5748 | j = hisidx[type]; |
| 5749 | for (i = newlen - 1; ; --i) |
| 5750 | { |
| 5751 | if (i >= 0) /* copy newest entries */ |
| 5752 | temp[i] = history[type][j]; |
| 5753 | else /* remove older entries */ |
| 5754 | vim_free(history[type][j].hisstr); |
| 5755 | if (--j < 0) |
| 5756 | j = hislen - 1; |
| 5757 | if (j == hisidx[type]) |
| 5758 | break; |
| 5759 | } |
| 5760 | hisidx[type] = newlen - 1; |
| 5761 | } |
| 5762 | vim_free(history[type]); |
| 5763 | history[type] = temp; |
| 5764 | } |
| 5765 | } |
| 5766 | hislen = newlen; |
| 5767 | } |
| 5768 | } |
| 5769 | |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5770 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5771 | clear_hist_entry(histentry_T *hisptr) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5772 | { |
| 5773 | hisptr->hisnum = 0; |
| 5774 | hisptr->viminfo = FALSE; |
| 5775 | hisptr->hisstr = NULL; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 5776 | hisptr->time_set = 0; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5777 | } |
| 5778 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5779 | /* |
| 5780 | * Check if command line 'str' is already in history. |
| 5781 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 5782 | */ |
| 5783 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5784 | in_history( |
| 5785 | int type, |
| 5786 | char_u *str, |
| 5787 | int move_to_front, /* Move the entry to the front if it exists */ |
| 5788 | int sep, |
| 5789 | int writing) /* ignore entries read from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5790 | { |
| 5791 | int i; |
| 5792 | int last_i = -1; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5793 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5794 | |
| 5795 | if (hisidx[type] < 0) |
| 5796 | return FALSE; |
| 5797 | i = hisidx[type]; |
| 5798 | do |
| 5799 | { |
| 5800 | if (history[type][i].hisstr == NULL) |
| 5801 | return FALSE; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5802 | |
| 5803 | /* For search history, check that the separator character matches as |
| 5804 | * well. */ |
| 5805 | p = history[type][i].hisstr; |
| 5806 | if (STRCMP(str, p) == 0 |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5807 | && !(writing && history[type][i].viminfo) |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5808 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5809 | { |
| 5810 | if (!move_to_front) |
| 5811 | return TRUE; |
| 5812 | last_i = i; |
| 5813 | break; |
| 5814 | } |
| 5815 | if (--i < 0) |
| 5816 | i = hislen - 1; |
| 5817 | } while (i != hisidx[type]); |
| 5818 | |
| 5819 | if (last_i >= 0) |
| 5820 | { |
| 5821 | str = history[type][i].hisstr; |
| 5822 | while (i != hisidx[type]) |
| 5823 | { |
| 5824 | if (++i >= hislen) |
| 5825 | i = 0; |
| 5826 | history[type][last_i] = history[type][i]; |
| 5827 | last_i = i; |
| 5828 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5829 | history[type][i].hisnum = ++hisnum[type]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5830 | history[type][i].viminfo = FALSE; |
| 5831 | history[type][i].hisstr = str; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5832 | history[type][i].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5833 | return TRUE; |
| 5834 | } |
| 5835 | return FALSE; |
| 5836 | } |
| 5837 | |
| 5838 | /* |
| 5839 | * Convert history name (from table above) to its HIST_ equivalent. |
| 5840 | * When "name" is empty, return "cmd" history. |
| 5841 | * Returns -1 for unknown history name. |
| 5842 | */ |
| 5843 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5844 | get_histtype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5845 | { |
| 5846 | int i; |
| 5847 | int len = (int)STRLEN(name); |
| 5848 | |
| 5849 | /* No argument: use current history. */ |
| 5850 | if (len == 0) |
| 5851 | return hist_char2type(ccline.cmdfirstc); |
| 5852 | |
| 5853 | for (i = 0; history_names[i] != NULL; ++i) |
| 5854 | if (STRNICMP(name, history_names[i], len) == 0) |
| 5855 | return i; |
| 5856 | |
| 5857 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 5858 | return hist_char2type(name[0]); |
| 5859 | |
| 5860 | return -1; |
| 5861 | } |
| 5862 | |
| 5863 | static int last_maptick = -1; /* last seen maptick */ |
| 5864 | |
| 5865 | /* |
| 5866 | * Add the given string to the given history. If the string is already in the |
| 5867 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 5868 | * values. |
| 5869 | */ |
| 5870 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5871 | add_to_history( |
| 5872 | int histype, |
| 5873 | char_u *new_entry, |
| 5874 | int in_map, /* consider maptick when inside a mapping */ |
| 5875 | int sep) /* separator character used (search hist) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5876 | { |
| 5877 | histentry_T *hisptr; |
| 5878 | int len; |
| 5879 | |
| 5880 | if (hislen == 0) /* no history */ |
| 5881 | return; |
| 5882 | |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 5883 | if (cmdmod.keeppatterns && histype == HIST_SEARCH) |
| 5884 | return; |
| 5885 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5886 | /* |
| 5887 | * Searches inside the same mapping overwrite each other, so that only |
| 5888 | * the last line is kept. Be careful not to remove a line that was moved |
| 5889 | * down, only lines that were added. |
| 5890 | */ |
| 5891 | if (histype == HIST_SEARCH && in_map) |
| 5892 | { |
Bram Moolenaar | 4664371 | 2016-09-09 21:42:36 +0200 | [diff] [blame] | 5893 | if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5894 | { |
| 5895 | /* Current line is from the same mapping, remove it */ |
| 5896 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 5897 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5898 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5899 | --hisnum[histype]; |
| 5900 | if (--hisidx[HIST_SEARCH] < 0) |
| 5901 | hisidx[HIST_SEARCH] = hislen - 1; |
| 5902 | } |
| 5903 | last_maptick = -1; |
| 5904 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5905 | if (!in_history(histype, new_entry, TRUE, sep, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5906 | { |
| 5907 | if (++hisidx[histype] == hislen) |
| 5908 | hisidx[histype] = 0; |
| 5909 | hisptr = &history[histype][hisidx[histype]]; |
| 5910 | vim_free(hisptr->hisstr); |
| 5911 | |
| 5912 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5913 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5914 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 5915 | if (hisptr->hisstr != NULL) |
| 5916 | hisptr->hisstr[len + 1] = sep; |
| 5917 | |
| 5918 | hisptr->hisnum = ++hisnum[histype]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5919 | hisptr->viminfo = FALSE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5920 | hisptr->time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5921 | if (histype == HIST_SEARCH && in_map) |
| 5922 | last_maptick = maptick; |
| 5923 | } |
| 5924 | } |
| 5925 | |
| 5926 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 5927 | |
| 5928 | /* |
| 5929 | * Get identifier of newest history entry. |
| 5930 | * "histype" may be one of the HIST_ values. |
| 5931 | */ |
| 5932 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5933 | get_history_idx(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5934 | { |
| 5935 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5936 | || hisidx[histype] < 0) |
| 5937 | return -1; |
| 5938 | |
| 5939 | return history[histype][hisidx[histype]].hisnum; |
| 5940 | } |
| 5941 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5942 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5943 | * Calculate history index from a number: |
| 5944 | * num > 0: seen as identifying number of a history entry |
| 5945 | * num < 0: relative position in history wrt newest entry |
| 5946 | * "histype" may be one of the HIST_ values. |
| 5947 | */ |
| 5948 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5949 | calc_hist_idx(int histype, int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5950 | { |
| 5951 | int i; |
| 5952 | histentry_T *hist; |
| 5953 | int wrapped = FALSE; |
| 5954 | |
| 5955 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5956 | || (i = hisidx[histype]) < 0 || num == 0) |
| 5957 | return -1; |
| 5958 | |
| 5959 | hist = history[histype]; |
| 5960 | if (num > 0) |
| 5961 | { |
| 5962 | while (hist[i].hisnum > num) |
| 5963 | if (--i < 0) |
| 5964 | { |
| 5965 | if (wrapped) |
| 5966 | break; |
| 5967 | i += hislen; |
| 5968 | wrapped = TRUE; |
| 5969 | } |
| 5970 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 5971 | return i; |
| 5972 | } |
| 5973 | else if (-num <= hislen) |
| 5974 | { |
| 5975 | i += num + 1; |
| 5976 | if (i < 0) |
| 5977 | i += hislen; |
| 5978 | if (hist[i].hisstr != NULL) |
| 5979 | return i; |
| 5980 | } |
| 5981 | return -1; |
| 5982 | } |
| 5983 | |
| 5984 | /* |
| 5985 | * Get a history entry by its index. |
| 5986 | * "histype" may be one of the HIST_ values. |
| 5987 | */ |
| 5988 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5989 | get_history_entry(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5990 | { |
| 5991 | idx = calc_hist_idx(histype, idx); |
| 5992 | if (idx >= 0) |
| 5993 | return history[histype][idx].hisstr; |
| 5994 | else |
| 5995 | return (char_u *)""; |
| 5996 | } |
| 5997 | |
| 5998 | /* |
| 5999 | * Clear all entries of a history. |
| 6000 | * "histype" may be one of the HIST_ values. |
| 6001 | */ |
| 6002 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6003 | clr_history(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6004 | { |
| 6005 | int i; |
| 6006 | histentry_T *hisptr; |
| 6007 | |
| 6008 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 6009 | { |
| 6010 | hisptr = history[histype]; |
| 6011 | for (i = hislen; i--;) |
| 6012 | { |
| 6013 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6014 | clear_hist_entry(hisptr); |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 6015 | hisptr++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6016 | } |
| 6017 | hisidx[histype] = -1; /* mark history as cleared */ |
| 6018 | hisnum[histype] = 0; /* reset identifier counter */ |
| 6019 | return OK; |
| 6020 | } |
| 6021 | return FAIL; |
| 6022 | } |
| 6023 | |
| 6024 | /* |
| 6025 | * Remove all entries matching {str} from a history. |
| 6026 | * "histype" may be one of the HIST_ values. |
| 6027 | */ |
| 6028 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6029 | del_history_entry(int histype, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6030 | { |
| 6031 | regmatch_T regmatch; |
| 6032 | histentry_T *hisptr; |
| 6033 | int idx; |
| 6034 | int i; |
| 6035 | int last; |
| 6036 | int found = FALSE; |
| 6037 | |
| 6038 | regmatch.regprog = NULL; |
| 6039 | regmatch.rm_ic = FALSE; /* always match case */ |
| 6040 | if (hislen != 0 |
| 6041 | && histype >= 0 |
| 6042 | && histype < HIST_COUNT |
| 6043 | && *str != NUL |
| 6044 | && (idx = hisidx[histype]) >= 0 |
| 6045 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 6046 | != NULL) |
| 6047 | { |
| 6048 | i = last = idx; |
| 6049 | do |
| 6050 | { |
| 6051 | hisptr = &history[histype][i]; |
| 6052 | if (hisptr->hisstr == NULL) |
| 6053 | break; |
| 6054 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 6055 | { |
| 6056 | found = TRUE; |
| 6057 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6058 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6059 | } |
| 6060 | else |
| 6061 | { |
| 6062 | if (i != last) |
| 6063 | { |
| 6064 | history[histype][last] = *hisptr; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6065 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6066 | } |
| 6067 | if (--last < 0) |
| 6068 | last += hislen; |
| 6069 | } |
| 6070 | if (--i < 0) |
| 6071 | i += hislen; |
| 6072 | } while (i != idx); |
| 6073 | if (history[histype][idx].hisstr == NULL) |
| 6074 | hisidx[histype] = -1; |
| 6075 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6076 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6077 | return found; |
| 6078 | } |
| 6079 | |
| 6080 | /* |
| 6081 | * Remove an indexed entry from a history. |
| 6082 | * "histype" may be one of the HIST_ values. |
| 6083 | */ |
| 6084 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6085 | del_history_idx(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6086 | { |
| 6087 | int i, j; |
| 6088 | |
| 6089 | i = calc_hist_idx(histype, idx); |
| 6090 | if (i < 0) |
| 6091 | return FALSE; |
| 6092 | idx = hisidx[histype]; |
| 6093 | vim_free(history[histype][i].hisstr); |
| 6094 | |
| 6095 | /* When deleting the last added search string in a mapping, reset |
| 6096 | * last_maptick, so that the last added search string isn't deleted again. |
| 6097 | */ |
| 6098 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 6099 | last_maptick = -1; |
| 6100 | |
| 6101 | while (i != idx) |
| 6102 | { |
| 6103 | j = (i + 1) % hislen; |
| 6104 | history[histype][i] = history[histype][j]; |
| 6105 | i = j; |
| 6106 | } |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6107 | clear_hist_entry(&history[histype][i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6108 | if (--i < 0) |
| 6109 | i += hislen; |
| 6110 | hisidx[histype] = i; |
| 6111 | return TRUE; |
| 6112 | } |
| 6113 | |
| 6114 | #endif /* FEAT_EVAL */ |
| 6115 | |
| 6116 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 6117 | /* |
| 6118 | * Very specific function to remove the value in ":set key=val" from the |
| 6119 | * history. |
| 6120 | */ |
| 6121 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6122 | remove_key_from_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6123 | { |
| 6124 | char_u *p; |
| 6125 | int i; |
| 6126 | |
| 6127 | i = hisidx[HIST_CMD]; |
| 6128 | if (i < 0) |
| 6129 | return; |
| 6130 | p = history[HIST_CMD][i].hisstr; |
| 6131 | if (p != NULL) |
| 6132 | for ( ; *p; ++p) |
| 6133 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 6134 | { |
| 6135 | p = vim_strchr(p + 3, '='); |
| 6136 | if (p == NULL) |
| 6137 | break; |
| 6138 | ++p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6139 | for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6140 | if (p[i] == '\\' && p[i + 1]) |
| 6141 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6142 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6143 | --p; |
| 6144 | } |
| 6145 | } |
| 6146 | #endif |
| 6147 | |
| 6148 | #endif /* FEAT_CMDHIST */ |
| 6149 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6150 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6151 | /* |
| 6152 | * Get pointer to the command line info to use. cmdline_paste() may clear |
| 6153 | * ccline and put the previous value in prev_ccline. |
| 6154 | */ |
| 6155 | static struct cmdline_info * |
| 6156 | get_ccline_ptr(void) |
| 6157 | { |
| 6158 | if ((State & CMDLINE) == 0) |
| 6159 | return NULL; |
| 6160 | if (ccline.cmdbuff != NULL) |
| 6161 | return &ccline; |
| 6162 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 6163 | return &prev_ccline; |
| 6164 | return NULL; |
| 6165 | } |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6166 | #endif |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6167 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6168 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6169 | /* |
| 6170 | * Get the current command line in allocated memory. |
| 6171 | * Only works when the command line is being edited. |
| 6172 | * Returns NULL when something is wrong. |
| 6173 | */ |
| 6174 | char_u * |
| 6175 | get_cmdline_str(void) |
| 6176 | { |
| 6177 | struct cmdline_info *p = get_ccline_ptr(); |
| 6178 | |
| 6179 | if (p == NULL) |
| 6180 | return NULL; |
| 6181 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
| 6182 | } |
| 6183 | |
| 6184 | /* |
| 6185 | * Get the current command line position, counted in bytes. |
| 6186 | * Zero is the first position. |
| 6187 | * Only works when the command line is being edited. |
| 6188 | * Returns -1 when something is wrong. |
| 6189 | */ |
| 6190 | int |
| 6191 | get_cmdline_pos(void) |
| 6192 | { |
| 6193 | struct cmdline_info *p = get_ccline_ptr(); |
| 6194 | |
| 6195 | if (p == NULL) |
| 6196 | return -1; |
| 6197 | return p->cmdpos; |
| 6198 | } |
| 6199 | |
| 6200 | /* |
| 6201 | * Set the command line byte position to "pos". Zero is the first position. |
| 6202 | * Only works when the command line is being edited. |
| 6203 | * Returns 1 when failed, 0 when OK. |
| 6204 | */ |
| 6205 | int |
| 6206 | set_cmdline_pos( |
| 6207 | int pos) |
| 6208 | { |
| 6209 | struct cmdline_info *p = get_ccline_ptr(); |
| 6210 | |
| 6211 | if (p == NULL) |
| 6212 | return 1; |
| 6213 | |
| 6214 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 6215 | * changed the command line. */ |
| 6216 | if (pos < 0) |
| 6217 | new_cmdpos = 0; |
| 6218 | else |
| 6219 | new_cmdpos = pos; |
| 6220 | return 0; |
| 6221 | } |
| 6222 | #endif |
| 6223 | |
| 6224 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
| 6225 | /* |
| 6226 | * Get the current command-line type. |
| 6227 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
| 6228 | * Only works when the command line is being edited. |
| 6229 | * Returns NUL when something is wrong. |
| 6230 | */ |
| 6231 | int |
| 6232 | get_cmdline_type(void) |
| 6233 | { |
| 6234 | struct cmdline_info *p = get_ccline_ptr(); |
| 6235 | |
| 6236 | if (p == NULL) |
| 6237 | return NUL; |
| 6238 | if (p->cmdfirstc == NUL) |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6239 | return |
| 6240 | # ifdef FEAT_EVAL |
| 6241 | (p->input_fn) ? '@' : |
| 6242 | # endif |
| 6243 | '-'; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6244 | return p->cmdfirstc; |
| 6245 | } |
| 6246 | #endif |
| 6247 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6248 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 6249 | /* |
| 6250 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 6251 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 6252 | * Returns OK if parsed successfully, otherwise FAIL. |
| 6253 | */ |
| 6254 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6255 | get_list_range(char_u **str, int *num1, int *num2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6256 | { |
| 6257 | int len; |
| 6258 | int first = FALSE; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 6259 | varnumber_T num; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6260 | |
| 6261 | *str = skipwhite(*str); |
| 6262 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 6263 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6264 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6265 | *str += len; |
| 6266 | *num1 = (int)num; |
| 6267 | first = TRUE; |
| 6268 | } |
| 6269 | *str = skipwhite(*str); |
| 6270 | if (**str == ',') /* parse "to" part of range */ |
| 6271 | { |
| 6272 | *str = skipwhite(*str + 1); |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6273 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6274 | if (len > 0) |
| 6275 | { |
| 6276 | *num2 = (int)num; |
| 6277 | *str = skipwhite(*str + len); |
| 6278 | } |
| 6279 | else if (!first) /* no number given at all */ |
| 6280 | return FAIL; |
| 6281 | } |
| 6282 | else if (first) /* only one number given */ |
| 6283 | *num2 = *num1; |
| 6284 | return OK; |
| 6285 | } |
| 6286 | #endif |
| 6287 | |
| 6288 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 6289 | /* |
| 6290 | * :history command - print a history |
| 6291 | */ |
| 6292 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6293 | ex_history(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6294 | { |
| 6295 | histentry_T *hist; |
| 6296 | int histype1 = HIST_CMD; |
| 6297 | int histype2 = HIST_CMD; |
| 6298 | int hisidx1 = 1; |
| 6299 | int hisidx2 = -1; |
| 6300 | int idx; |
| 6301 | int i, j, k; |
| 6302 | char_u *end; |
| 6303 | char_u *arg = eap->arg; |
| 6304 | |
| 6305 | if (hislen == 0) |
| 6306 | { |
| 6307 | MSG(_("'history' option is zero")); |
| 6308 | return; |
| 6309 | } |
| 6310 | |
| 6311 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 6312 | { |
| 6313 | end = arg; |
| 6314 | while (ASCII_ISALPHA(*end) |
| 6315 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 6316 | end++; |
| 6317 | i = *end; |
| 6318 | *end = NUL; |
| 6319 | histype1 = get_histtype(arg); |
| 6320 | if (histype1 == -1) |
| 6321 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 6322 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6323 | { |
| 6324 | histype1 = 0; |
| 6325 | histype2 = HIST_COUNT-1; |
| 6326 | } |
| 6327 | else |
| 6328 | { |
| 6329 | *end = i; |
| 6330 | EMSG(_(e_trailing)); |
| 6331 | return; |
| 6332 | } |
| 6333 | } |
| 6334 | else |
| 6335 | histype2 = histype1; |
| 6336 | *end = i; |
| 6337 | } |
| 6338 | else |
| 6339 | end = arg; |
| 6340 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 6341 | { |
| 6342 | EMSG(_(e_trailing)); |
| 6343 | return; |
| 6344 | } |
| 6345 | |
| 6346 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 6347 | { |
| 6348 | STRCPY(IObuff, "\n # "); |
| 6349 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 6350 | MSG_PUTS_TITLE(IObuff); |
| 6351 | idx = hisidx[histype1]; |
| 6352 | hist = history[histype1]; |
| 6353 | j = hisidx1; |
| 6354 | k = hisidx2; |
| 6355 | if (j < 0) |
| 6356 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 6357 | if (k < 0) |
| 6358 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 6359 | if (idx >= 0 && j <= k) |
| 6360 | for (i = idx + 1; !got_int; ++i) |
| 6361 | { |
| 6362 | if (i == hislen) |
| 6363 | i = 0; |
| 6364 | if (hist[i].hisstr != NULL |
| 6365 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 6366 | { |
| 6367 | msg_putchar('\n'); |
| 6368 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 6369 | hist[i].hisnum); |
| 6370 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 6371 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
Bram Moolenaar | 38f5f95 | 2012-01-26 13:01:59 +0100 | [diff] [blame] | 6372 | (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6373 | else |
| 6374 | STRCAT(IObuff, hist[i].hisstr); |
| 6375 | msg_outtrans(IObuff); |
| 6376 | out_flush(); |
| 6377 | } |
| 6378 | if (i == idx) |
| 6379 | break; |
| 6380 | } |
| 6381 | } |
| 6382 | } |
| 6383 | #endif |
| 6384 | |
| 6385 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6386 | /* |
| 6387 | * Buffers for history read from a viminfo file. Only valid while reading. |
| 6388 | */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6389 | static histentry_T *viminfo_history[HIST_COUNT] = |
| 6390 | {NULL, NULL, NULL, NULL, NULL}; |
| 6391 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 6392 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6393 | static int viminfo_add_at_front = FALSE; |
| 6394 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 6395 | static int hist_type2char(int type, int use_question); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6396 | |
| 6397 | /* |
| 6398 | * Translate a history type number to the associated character. |
| 6399 | */ |
| 6400 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6401 | hist_type2char( |
| 6402 | int type, |
| 6403 | int use_question) /* use '?' instead of '/' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6404 | { |
| 6405 | if (type == HIST_CMD) |
| 6406 | return ':'; |
| 6407 | if (type == HIST_SEARCH) |
| 6408 | { |
| 6409 | if (use_question) |
| 6410 | return '?'; |
| 6411 | else |
| 6412 | return '/'; |
| 6413 | } |
| 6414 | if (type == HIST_EXPR) |
| 6415 | return '='; |
| 6416 | return '@'; |
| 6417 | } |
| 6418 | |
| 6419 | /* |
| 6420 | * Prepare for reading the history from the viminfo file. |
| 6421 | * This allocates history arrays to store the read history lines. |
| 6422 | */ |
| 6423 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6424 | prepare_viminfo_history(int asklen, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6425 | { |
| 6426 | int i; |
| 6427 | int num; |
| 6428 | int type; |
| 6429 | int len; |
| 6430 | |
| 6431 | init_history(); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6432 | viminfo_add_at_front = (asklen != 0 && !writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6433 | if (asklen > hislen) |
| 6434 | asklen = hislen; |
| 6435 | |
| 6436 | for (type = 0; type < HIST_COUNT; ++type) |
| 6437 | { |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6438 | /* Count the number of empty spaces in the history list. Entries read |
| 6439 | * from viminfo previously are also considered empty. If there are |
| 6440 | * more spaces available than we request, then fill them up. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6441 | for (i = 0, num = 0; i < hislen; i++) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6442 | if (history[type][i].hisstr == NULL || history[type][i].viminfo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6443 | num++; |
| 6444 | len = asklen; |
| 6445 | if (num > len) |
| 6446 | len = num; |
| 6447 | if (len <= 0) |
| 6448 | viminfo_history[type] = NULL; |
| 6449 | else |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6450 | viminfo_history[type] = (histentry_T *)lalloc( |
| 6451 | (long_u)(len * sizeof(histentry_T)), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6452 | if (viminfo_history[type] == NULL) |
| 6453 | len = 0; |
| 6454 | viminfo_hislen[type] = len; |
| 6455 | viminfo_hisidx[type] = 0; |
| 6456 | } |
| 6457 | } |
| 6458 | |
| 6459 | /* |
| 6460 | * Accept a line from the viminfo, store it in the history array when it's |
| 6461 | * new. |
| 6462 | */ |
| 6463 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6464 | read_viminfo_history(vir_T *virp, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6465 | { |
| 6466 | int type; |
| 6467 | long_u len; |
| 6468 | char_u *val; |
| 6469 | char_u *p; |
| 6470 | |
| 6471 | type = hist_char2type(virp->vir_line[0]); |
| 6472 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6473 | { |
| 6474 | val = viminfo_readstring(virp, 1, TRUE); |
| 6475 | if (val != NULL && *val != NUL) |
| 6476 | { |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6477 | int sep = (*val == ' ' ? NUL : *val); |
| 6478 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6479 | if (!in_history(type, val + (type == HIST_SEARCH), |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6480 | viminfo_add_at_front, sep, writing)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6481 | { |
| 6482 | /* Need to re-allocate to append the separator byte. */ |
| 6483 | len = STRLEN(val); |
| 6484 | p = lalloc(len + 2, TRUE); |
| 6485 | if (p != NULL) |
| 6486 | { |
| 6487 | if (type == HIST_SEARCH) |
| 6488 | { |
| 6489 | /* Search entry: Move the separator from the first |
| 6490 | * column to after the NUL. */ |
| 6491 | mch_memmove(p, val + 1, (size_t)len); |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6492 | p[len] = sep; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6493 | } |
| 6494 | else |
| 6495 | { |
| 6496 | /* Not a search entry: No separator in the viminfo |
| 6497 | * file, add a NUL separator. */ |
| 6498 | mch_memmove(p, val, (size_t)len + 1); |
| 6499 | p[len + 1] = NUL; |
| 6500 | } |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6501 | viminfo_history[type][viminfo_hisidx[type]].hisstr = p; |
| 6502 | viminfo_history[type][viminfo_hisidx[type]].time_set = 0; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6503 | viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE; |
| 6504 | viminfo_history[type][viminfo_hisidx[type]].hisnum = 0; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6505 | viminfo_hisidx[type]++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6506 | } |
| 6507 | } |
| 6508 | } |
| 6509 | vim_free(val); |
| 6510 | } |
| 6511 | return viminfo_readline(virp); |
| 6512 | } |
| 6513 | |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6514 | /* |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6515 | * Accept a new style history line from the viminfo, store it in the history |
| 6516 | * array when it's new. |
| 6517 | */ |
| 6518 | void |
| 6519 | handle_viminfo_history( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6520 | garray_T *values, |
| 6521 | int writing) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6522 | { |
| 6523 | int type; |
| 6524 | long_u len; |
| 6525 | char_u *val; |
| 6526 | char_u *p; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6527 | bval_T *vp = (bval_T *)values->ga_data; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6528 | |
| 6529 | /* Check the format: |
| 6530 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6531 | if (values->ga_len < 4 |
| 6532 | || vp[0].bv_type != BVAL_NR |
| 6533 | || vp[1].bv_type != BVAL_NR |
| 6534 | || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY) |
| 6535 | || vp[3].bv_type != BVAL_STRING) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6536 | return; |
| 6537 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6538 | type = vp[0].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6539 | if (type >= HIST_COUNT) |
| 6540 | return; |
| 6541 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6542 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6543 | val = vp[3].bv_string; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6544 | if (val != NULL && *val != NUL) |
| 6545 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6546 | int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR |
| 6547 | ? vp[2].bv_nr : NUL; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6548 | int idx; |
| 6549 | int overwrite = FALSE; |
| 6550 | |
| 6551 | if (!in_history(type, val, viminfo_add_at_front, sep, writing)) |
| 6552 | { |
| 6553 | /* If lines were written by an older Vim we need to avoid |
| 6554 | * getting duplicates. See if the entry already exists. */ |
| 6555 | for (idx = 0; idx < viminfo_hisidx[type]; ++idx) |
| 6556 | { |
| 6557 | p = viminfo_history[type][idx].hisstr; |
| 6558 | if (STRCMP(val, p) == 0 |
| 6559 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
| 6560 | { |
| 6561 | overwrite = TRUE; |
| 6562 | break; |
| 6563 | } |
| 6564 | } |
| 6565 | |
| 6566 | if (!overwrite) |
| 6567 | { |
| 6568 | /* Need to re-allocate to append the separator byte. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6569 | len = vp[3].bv_len; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6570 | p = lalloc(len + 2, TRUE); |
| 6571 | } |
Bram Moolenaar | 72e697d | 2016-06-13 22:48:01 +0200 | [diff] [blame] | 6572 | else |
| 6573 | len = 0; /* for picky compilers */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6574 | if (p != NULL) |
| 6575 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6576 | viminfo_history[type][idx].time_set = vp[1].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6577 | if (!overwrite) |
| 6578 | { |
| 6579 | mch_memmove(p, val, (size_t)len + 1); |
| 6580 | /* Put the separator after the NUL. */ |
| 6581 | p[len + 1] = sep; |
| 6582 | viminfo_history[type][idx].hisstr = p; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6583 | viminfo_history[type][idx].hisnum = 0; |
| 6584 | viminfo_history[type][idx].viminfo = TRUE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6585 | viminfo_hisidx[type]++; |
| 6586 | } |
| 6587 | } |
| 6588 | } |
| 6589 | } |
| 6590 | } |
| 6591 | } |
| 6592 | |
| 6593 | /* |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6594 | * Concatenate history lines from viminfo after the lines typed in this Vim. |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6595 | */ |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6596 | static void |
| 6597 | concat_history(int type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6598 | { |
| 6599 | int idx; |
| 6600 | int i; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6601 | |
| 6602 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 6603 | if (idx >= hislen) |
| 6604 | idx -= hislen; |
| 6605 | else if (idx < 0) |
| 6606 | idx = hislen - 1; |
| 6607 | if (viminfo_add_at_front) |
| 6608 | hisidx[type] = idx; |
| 6609 | else |
| 6610 | { |
| 6611 | if (hisidx[type] == -1) |
| 6612 | hisidx[type] = hislen - 1; |
| 6613 | do |
| 6614 | { |
| 6615 | if (history[type][idx].hisstr != NULL |
| 6616 | || history[type][idx].viminfo) |
| 6617 | break; |
| 6618 | if (++idx == hislen) |
| 6619 | idx = 0; |
| 6620 | } while (idx != hisidx[type]); |
| 6621 | if (idx != hisidx[type] && --idx < 0) |
| 6622 | idx = hislen - 1; |
| 6623 | } |
| 6624 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6625 | { |
| 6626 | vim_free(history[type][idx].hisstr); |
| 6627 | history[type][idx].hisstr = viminfo_history[type][i].hisstr; |
| 6628 | history[type][idx].viminfo = TRUE; |
| 6629 | history[type][idx].time_set = viminfo_history[type][i].time_set; |
| 6630 | if (--idx < 0) |
| 6631 | idx = hislen - 1; |
| 6632 | } |
| 6633 | idx += 1; |
| 6634 | idx %= hislen; |
| 6635 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6636 | { |
| 6637 | history[type][idx++].hisnum = ++hisnum[type]; |
| 6638 | idx %= hislen; |
| 6639 | } |
| 6640 | } |
| 6641 | |
| 6642 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6643 | static int |
| 6644 | #ifdef __BORLANDC__ |
| 6645 | _RTLENTRYF |
| 6646 | #endif |
| 6647 | sort_hist(const void *s1, const void *s2) |
| 6648 | { |
| 6649 | histentry_T *p1 = *(histentry_T **)s1; |
| 6650 | histentry_T *p2 = *(histentry_T **)s2; |
| 6651 | |
| 6652 | if (p1->time_set < p2->time_set) return -1; |
| 6653 | if (p1->time_set > p2->time_set) return 1; |
| 6654 | return 0; |
| 6655 | } |
| 6656 | #endif |
| 6657 | |
| 6658 | /* |
| 6659 | * Merge history lines from viminfo and lines typed in this Vim based on the |
| 6660 | * timestamp; |
| 6661 | */ |
| 6662 | static void |
| 6663 | merge_history(int type) |
| 6664 | { |
| 6665 | int max_len; |
| 6666 | histentry_T **tot_hist; |
| 6667 | histentry_T *new_hist; |
| 6668 | int i; |
| 6669 | int len; |
| 6670 | |
| 6671 | /* Make one long list with all entries. */ |
| 6672 | max_len = hislen + viminfo_hisidx[type]; |
| 6673 | tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *)); |
| 6674 | new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T)); |
| 6675 | if (tot_hist == NULL || new_hist == NULL) |
| 6676 | { |
| 6677 | vim_free(tot_hist); |
| 6678 | vim_free(new_hist); |
| 6679 | return; |
| 6680 | } |
| 6681 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6682 | tot_hist[i] = &viminfo_history[type][i]; |
| 6683 | len = i; |
| 6684 | for (i = 0; i < hislen; i++) |
| 6685 | if (history[type][i].hisstr != NULL) |
| 6686 | tot_hist[len++] = &history[type][i]; |
| 6687 | |
| 6688 | /* Sort the list on timestamp. */ |
| 6689 | qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist); |
| 6690 | |
| 6691 | /* Keep the newest ones. */ |
| 6692 | for (i = 0; i < hislen; i++) |
| 6693 | { |
| 6694 | if (i < len) |
| 6695 | { |
| 6696 | new_hist[i] = *tot_hist[i]; |
| 6697 | tot_hist[i]->hisstr = NULL; |
| 6698 | if (new_hist[i].hisnum == 0) |
| 6699 | new_hist[i].hisnum = ++hisnum[type]; |
| 6700 | } |
| 6701 | else |
| 6702 | clear_hist_entry(&new_hist[i]); |
| 6703 | } |
Bram Moolenaar | a890f5e | 2016-06-12 23:03:19 +0200 | [diff] [blame] | 6704 | hisidx[type] = (i < len ? i : len) - 1; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6705 | |
| 6706 | /* Free what is not kept. */ |
| 6707 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6708 | vim_free(viminfo_history[type][i].hisstr); |
| 6709 | for (i = 0; i < hislen; i++) |
| 6710 | vim_free(history[type][i].hisstr); |
| 6711 | vim_free(history[type]); |
| 6712 | history[type] = new_hist; |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 6713 | vim_free(tot_hist); |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6714 | } |
| 6715 | |
| 6716 | /* |
| 6717 | * Finish reading history lines from viminfo. Not used when writing viminfo. |
| 6718 | */ |
| 6719 | void |
| 6720 | finish_viminfo_history(vir_T *virp) |
| 6721 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6722 | int type; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6723 | int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6724 | |
| 6725 | for (type = 0; type < HIST_COUNT; ++type) |
| 6726 | { |
| 6727 | if (history[type] == NULL) |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6728 | continue; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6729 | |
| 6730 | if (merge) |
| 6731 | merge_history(type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6732 | else |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6733 | concat_history(type); |
| 6734 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6735 | vim_free(viminfo_history[type]); |
| 6736 | viminfo_history[type] = NULL; |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6737 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6738 | } |
| 6739 | } |
| 6740 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6741 | /* |
| 6742 | * Write history to viminfo file in "fp". |
| 6743 | * When "merge" is TRUE merge history lines with a previously read viminfo |
| 6744 | * file, data is in viminfo_history[]. |
| 6745 | * When "merge" is FALSE just write all history lines. Used for ":wviminfo!". |
| 6746 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6747 | void |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6748 | write_viminfo_history(FILE *fp, int merge) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6749 | { |
| 6750 | int i; |
| 6751 | int type; |
| 6752 | int num_saved; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6753 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6754 | |
| 6755 | init_history(); |
| 6756 | if (hislen == 0) |
| 6757 | return; |
| 6758 | for (type = 0; type < HIST_COUNT; ++type) |
| 6759 | { |
| 6760 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 6761 | if (num_saved == 0) |
| 6762 | continue; |
| 6763 | if (num_saved < 0) /* Use default */ |
| 6764 | num_saved = hislen; |
| 6765 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 6766 | type == HIST_CMD ? _("Command Line") : |
| 6767 | type == HIST_SEARCH ? _("Search String") : |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6768 | type == HIST_EXPR ? _("Expression") : |
| 6769 | type == HIST_INPUT ? _("Input Line") : |
| 6770 | _("Debug Line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6771 | if (num_saved > hislen) |
| 6772 | num_saved = hislen; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6773 | |
| 6774 | /* |
| 6775 | * Merge typed and viminfo history: |
| 6776 | * round 1: history of typed commands. |
| 6777 | * round 2: history from recently read viminfo. |
| 6778 | */ |
| 6779 | for (round = 1; round <= 2; ++round) |
| 6780 | { |
Bram Moolenaar | a8565fe | 2013-04-15 16:14:22 +0200 | [diff] [blame] | 6781 | if (round == 1) |
| 6782 | /* start at newest entry, somewhere in the list */ |
| 6783 | i = hisidx[type]; |
| 6784 | else if (viminfo_hisidx[type] > 0) |
| 6785 | /* start at newest entry, first in the list */ |
| 6786 | i = 0; |
| 6787 | else |
| 6788 | /* empty list */ |
| 6789 | i = -1; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6790 | if (i >= 0) |
| 6791 | while (num_saved > 0 |
| 6792 | && !(round == 2 && i >= viminfo_hisidx[type])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6793 | { |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6794 | char_u *p; |
| 6795 | time_t timestamp; |
| 6796 | int c = NUL; |
| 6797 | |
| 6798 | if (round == 1) |
| 6799 | { |
| 6800 | p = history[type][i].hisstr; |
| 6801 | timestamp = history[type][i].time_set; |
| 6802 | } |
| 6803 | else |
| 6804 | { |
| 6805 | p = viminfo_history[type] == NULL ? NULL |
| 6806 | : viminfo_history[type][i].hisstr; |
| 6807 | timestamp = viminfo_history[type] == NULL ? 0 |
| 6808 | : viminfo_history[type][i].time_set; |
| 6809 | } |
| 6810 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6811 | if (p != NULL && (round == 2 |
| 6812 | || !merge |
| 6813 | || !history[type][i].viminfo)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6814 | { |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6815 | --num_saved; |
| 6816 | fputc(hist_type2char(type, TRUE), fp); |
| 6817 | /* For the search history: put the separator in the |
| 6818 | * second column; use a space if there isn't one. */ |
| 6819 | if (type == HIST_SEARCH) |
| 6820 | { |
| 6821 | c = p[STRLEN(p) + 1]; |
| 6822 | putc(c == NUL ? ' ' : c, fp); |
| 6823 | } |
| 6824 | viminfo_writestring(fp, p); |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6825 | |
| 6826 | { |
| 6827 | char cbuf[NUMBUFLEN]; |
| 6828 | |
| 6829 | /* New style history with a bar line. Format: |
| 6830 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
| 6831 | if (c == NUL) |
| 6832 | cbuf[0] = NUL; |
| 6833 | else |
| 6834 | sprintf(cbuf, "%d", c); |
| 6835 | fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY, |
| 6836 | type, (long)timestamp, cbuf); |
| 6837 | barline_writestring(fp, p, LSIZE - 20); |
| 6838 | putc('\n', fp); |
| 6839 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6840 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6841 | if (round == 1) |
| 6842 | { |
| 6843 | /* Decrement index, loop around and stop when back at |
| 6844 | * the start. */ |
| 6845 | if (--i < 0) |
| 6846 | i = hislen - 1; |
| 6847 | if (i == hisidx[type]) |
| 6848 | break; |
| 6849 | } |
| 6850 | else |
| 6851 | { |
| 6852 | /* Increment index. Stop at the end in the while. */ |
| 6853 | ++i; |
| 6854 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6855 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6856 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6857 | for (i = 0; i < viminfo_hisidx[type]; ++i) |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6858 | if (viminfo_history[type] != NULL) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6859 | vim_free(viminfo_history[type][i].hisstr); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6860 | vim_free(viminfo_history[type]); |
| 6861 | viminfo_history[type] = NULL; |
Bram Moolenaar | b70a473 | 2013-04-15 22:22:57 +0200 | [diff] [blame] | 6862 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6863 | } |
| 6864 | } |
| 6865 | #endif /* FEAT_VIMINFO */ |
| 6866 | |
| 6867 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 6868 | /* |
| 6869 | * Write a character at the current cursor+offset position. |
| 6870 | * It is directly written into the command buffer block. |
| 6871 | */ |
| 6872 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6873 | cmd_pchar(int c, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6874 | { |
| 6875 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 6876 | { |
| 6877 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 6878 | return; |
| 6879 | } |
| 6880 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 6881 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 6882 | } |
| 6883 | |
| 6884 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6885 | cmd_gchar(int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6886 | { |
| 6887 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 6888 | { |
| 6889 | /* EMSG(_("cmd_gchar beyond the command length")); */ |
| 6890 | return NUL; |
| 6891 | } |
| 6892 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 6893 | } |
| 6894 | #endif |
| 6895 | |
| 6896 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 6897 | /* |
| 6898 | * Open a window on the current command line and history. Allow editing in |
| 6899 | * the window. Returns when the window is closed. |
| 6900 | * Returns: |
| 6901 | * CR if the command is to be executed |
| 6902 | * Ctrl_C if it is to be abandoned |
| 6903 | * K_IGNORE if editing continues |
| 6904 | */ |
| 6905 | static int |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 6906 | open_cmdwin(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6907 | { |
| 6908 | struct cmdline_info save_ccline; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6909 | bufref_T old_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6910 | win_T *old_curwin = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6911 | bufref_T bufref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6912 | win_T *wp; |
| 6913 | int i; |
| 6914 | linenr_T lnum; |
| 6915 | int histtype; |
| 6916 | garray_T winsizes; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6917 | int save_restart_edit = restart_edit; |
| 6918 | int save_State = State; |
| 6919 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6920 | #ifdef FEAT_RIGHTLEFT |
| 6921 | int save_cmdmsg_rl = cmdmsg_rl; |
| 6922 | #endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6923 | #ifdef FEAT_FOLDING |
| 6924 | int save_KeyTyped; |
| 6925 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6926 | |
| 6927 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 6928 | if (cmdwin_type != 0 |
| 6929 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 6930 | || cmdline_star > 0 |
| 6931 | # endif |
| 6932 | ) |
| 6933 | { |
| 6934 | beep_flush(); |
| 6935 | return K_IGNORE; |
| 6936 | } |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6937 | set_bufref(&old_curbuf, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6938 | |
| 6939 | /* Save current window sizes. */ |
| 6940 | win_size_save(&winsizes); |
| 6941 | |
| 6942 | # ifdef FEAT_AUTOCMD |
| 6943 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6944 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6945 | # endif |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 6946 | /* don't use a new tab page */ |
| 6947 | cmdmod.tab = 0; |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 6948 | cmdmod.noswapfile = 1; |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 6949 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6950 | /* Create a window for the command-line buffer. */ |
| 6951 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 6952 | { |
| 6953 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6954 | # ifdef FEAT_AUTOCMD |
| 6955 | unblock_autocmds(); |
| 6956 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6957 | return K_IGNORE; |
| 6958 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6959 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6960 | |
| 6961 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 6962 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6963 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6964 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6965 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 6966 | #ifdef FEAT_FOLDING |
| 6967 | curwin->w_p_fen = FALSE; |
| 6968 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6969 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6970 | curwin->w_p_rl = cmdmsg_rl; |
| 6971 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6972 | # endif |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 6973 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6974 | |
| 6975 | # ifdef FEAT_AUTOCMD |
| 6976 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6977 | unblock_autocmds(); |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 6978 | /* But don't allow switching to another buffer. */ |
| 6979 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6980 | # endif |
| 6981 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6982 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 6983 | need_wait_return = FALSE; |
| 6984 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6985 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6986 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 6987 | { |
| 6988 | if (p_wc == TAB) |
| 6989 | { |
| 6990 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 6991 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 6992 | } |
| 6993 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 6994 | } |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 6995 | # ifdef FEAT_AUTOCMD |
| 6996 | --curbuf_lock; |
| 6997 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6998 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6999 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 7000 | * sets 'textwidth' to 78). */ |
| 7001 | curbuf->b_p_tw = 0; |
| 7002 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7003 | /* Fill the buffer with the history. */ |
| 7004 | init_history(); |
| 7005 | if (hislen > 0) |
| 7006 | { |
| 7007 | i = hisidx[histtype]; |
| 7008 | if (i >= 0) |
| 7009 | { |
| 7010 | lnum = 0; |
| 7011 | do |
| 7012 | { |
| 7013 | if (++i == hislen) |
| 7014 | i = 0; |
| 7015 | if (history[histtype][i].hisstr != NULL) |
| 7016 | ml_append(lnum++, history[histtype][i].hisstr, |
| 7017 | (colnr_T)0, FALSE); |
| 7018 | } |
| 7019 | while (i != hisidx[histtype]); |
| 7020 | } |
| 7021 | } |
| 7022 | |
| 7023 | /* Replace the empty last line with the current command-line and put the |
| 7024 | * cursor there. */ |
| 7025 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 7026 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 7027 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7028 | changed_line_abv_curs(); |
| 7029 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 7030 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7031 | |
| 7032 | /* Save the command line info, can be used recursively. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7033 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7034 | |
| 7035 | /* No Ex mode here! */ |
| 7036 | exmode_active = 0; |
| 7037 | |
| 7038 | State = NORMAL; |
| 7039 | # ifdef FEAT_MOUSE |
| 7040 | setmouse(); |
| 7041 | # endif |
| 7042 | |
| 7043 | # ifdef FEAT_AUTOCMD |
| 7044 | /* Trigger CmdwinEnter autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7045 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 7046 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 7047 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7048 | # endif |
| 7049 | |
| 7050 | i = RedrawingDisabled; |
| 7051 | RedrawingDisabled = 0; |
| 7052 | |
| 7053 | /* |
| 7054 | * Call the main loop until <CR> or CTRL-C is typed. |
| 7055 | */ |
| 7056 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 7057 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7058 | |
| 7059 | RedrawingDisabled = i; |
| 7060 | |
| 7061 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7062 | |
| 7063 | # ifdef FEAT_FOLDING |
| 7064 | save_KeyTyped = KeyTyped; |
| 7065 | # endif |
| 7066 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7067 | /* Trigger CmdwinLeave autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7068 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE); |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7069 | |
| 7070 | # ifdef FEAT_FOLDING |
| 7071 | /* Restore KeyTyped in case it is modified by autocommands */ |
| 7072 | KeyTyped = save_KeyTyped; |
| 7073 | # endif |
| 7074 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7075 | # endif |
| 7076 | |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 7077 | /* Restore the command line info. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7078 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7079 | cmdwin_type = 0; |
| 7080 | |
| 7081 | exmode_active = save_exmode; |
| 7082 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 7083 | /* 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] | 7084 | * this happens! */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7085 | if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7086 | { |
| 7087 | cmdwin_result = Ctrl_C; |
| 7088 | EMSG(_("E199: Active window or buffer deleted")); |
| 7089 | } |
| 7090 | else |
| 7091 | { |
| 7092 | # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 7093 | /* autocmds may abort script processing */ |
| 7094 | if (aborting() && cmdwin_result != K_IGNORE) |
| 7095 | cmdwin_result = Ctrl_C; |
| 7096 | # endif |
| 7097 | /* Set the new command line from the cmdline buffer. */ |
| 7098 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7099 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7100 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7101 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 7102 | |
| 7103 | if (histtype == HIST_CMD) |
| 7104 | { |
| 7105 | /* Execute the command directly. */ |
| 7106 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 7107 | cmdwin_result = CAR; |
| 7108 | } |
| 7109 | else |
| 7110 | { |
| 7111 | /* First need to cancel what we were doing. */ |
| 7112 | ccline.cmdbuff = NULL; |
| 7113 | stuffcharReadbuff(':'); |
| 7114 | stuffReadbuff((char_u *)p); |
| 7115 | stuffcharReadbuff(CAR); |
| 7116 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7117 | } |
| 7118 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 7119 | { |
| 7120 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 7121 | cmdwin_result = CAR; |
| 7122 | } |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 7123 | else if (cmdwin_result == Ctrl_C) |
| 7124 | { |
| 7125 | /* :q or :close, don't execute any command |
| 7126 | * and don't modify the cmd window. */ |
| 7127 | ccline.cmdbuff = NULL; |
| 7128 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7129 | else |
| 7130 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 7131 | if (ccline.cmdbuff == NULL) |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7132 | { |
| 7133 | ccline.cmdbuff = vim_strsave((char_u *)""); |
| 7134 | ccline.cmdlen = 0; |
| 7135 | ccline.cmdbufflen = 1; |
| 7136 | ccline.cmdpos = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7137 | cmdwin_result = Ctrl_C; |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7138 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7139 | else |
| 7140 | { |
| 7141 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 7142 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 7143 | ccline.cmdpos = curwin->w_cursor.col; |
| 7144 | if (ccline.cmdpos > ccline.cmdlen) |
| 7145 | ccline.cmdpos = ccline.cmdlen; |
| 7146 | if (cmdwin_result == K_IGNORE) |
| 7147 | { |
| 7148 | set_cmdspos_cursor(); |
| 7149 | redrawcmd(); |
| 7150 | } |
| 7151 | } |
| 7152 | |
| 7153 | # ifdef FEAT_AUTOCMD |
| 7154 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7155 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7156 | # endif |
Bram Moolenaar | fa67fbe | 2015-06-25 18:20:36 +0200 | [diff] [blame] | 7157 | # ifdef FEAT_CONCEAL |
| 7158 | /* Avoid command-line window first character being concealed. */ |
| 7159 | curwin->w_p_cole = 0; |
| 7160 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7161 | wp = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7162 | set_bufref(&bufref, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7163 | win_goto(old_curwin); |
| 7164 | win_close(wp, TRUE); |
Bram Moolenaar | 8006d69 | 2010-03-02 17:23:21 +0100 | [diff] [blame] | 7165 | |
| 7166 | /* win_close() may have already wiped the buffer when 'bh' is |
| 7167 | * set to 'wipe' */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7168 | if (bufref_valid(&bufref)) |
| 7169 | close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7170 | |
| 7171 | /* Restore window sizes. */ |
| 7172 | win_size_restore(&winsizes); |
| 7173 | |
| 7174 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7175 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7176 | # endif |
| 7177 | } |
| 7178 | |
| 7179 | ga_clear(&winsizes); |
| 7180 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7181 | # ifdef FEAT_RIGHTLEFT |
| 7182 | cmdmsg_rl = save_cmdmsg_rl; |
| 7183 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7184 | |
| 7185 | State = save_State; |
| 7186 | # ifdef FEAT_MOUSE |
| 7187 | setmouse(); |
| 7188 | # endif |
| 7189 | |
| 7190 | return cmdwin_result; |
| 7191 | } |
| 7192 | #endif /* FEAT_CMDWIN */ |
| 7193 | |
| 7194 | /* |
| 7195 | * Used for commands that either take a simple command string argument, or: |
| 7196 | * cmd << endmarker |
| 7197 | * {script} |
| 7198 | * endmarker |
| 7199 | * Returns a pointer to allocated memory with {script} or NULL. |
| 7200 | */ |
| 7201 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7202 | script_get(exarg_T *eap, char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7203 | { |
| 7204 | char_u *theline; |
| 7205 | char *end_pattern = NULL; |
| 7206 | char dot[] = "."; |
| 7207 | garray_T ga; |
| 7208 | |
| 7209 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 7210 | return NULL; |
| 7211 | |
| 7212 | ga_init2(&ga, 1, 0x400); |
| 7213 | |
| 7214 | if (cmd[2] != NUL) |
| 7215 | end_pattern = (char *)skipwhite(cmd + 2); |
| 7216 | else |
| 7217 | end_pattern = dot; |
| 7218 | |
| 7219 | for (;;) |
| 7220 | { |
| 7221 | theline = eap->getline( |
| 7222 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7223 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7224 | #endif |
| 7225 | NUL, eap->cookie, 0); |
| 7226 | |
| 7227 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7228 | { |
| 7229 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7230 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7231 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7232 | |
| 7233 | ga_concat(&ga, theline); |
| 7234 | ga_append(&ga, '\n'); |
| 7235 | vim_free(theline); |
| 7236 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 7237 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7238 | |
| 7239 | return (char_u *)ga.ga_data; |
| 7240 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 7241 | |
| 7242 | #ifdef FEAT_SEARCH_EXTRA |
| 7243 | static void |
| 7244 | set_search_match(pos_T *t) |
| 7245 | { |
| 7246 | /* |
| 7247 | * First move cursor to end of match, then to the start. This |
| 7248 | * moves the whole match onto the screen when 'nowrap' is set. |
| 7249 | */ |
| 7250 | t->lnum += search_match_lines; |
| 7251 | t->col = search_match_endcol; |
| 7252 | if (t->lnum > curbuf->b_ml.ml_line_count) |
| 7253 | { |
| 7254 | t->lnum = curbuf->b_ml.ml_line_count; |
| 7255 | coladvance((colnr_T)MAXCOL); |
| 7256 | } |
| 7257 | } |
| 7258 | #endif |