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 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 149 | static void |
| 150 | trigger_cmd_autocmd(int typechar, int evt) |
| 151 | { |
| 152 | char_u typestr[2]; |
| 153 | |
| 154 | typestr[0] = typechar; |
| 155 | typestr[1] = NUL; |
| 156 | apply_autocmds(evt, typestr, typestr, FALSE, curbuf); |
| 157 | } |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 158 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 159 | /* |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 160 | * Abandon the command line. |
| 161 | */ |
| 162 | static void |
| 163 | abandon_cmdline(void) |
| 164 | { |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 165 | VIM_CLEAR(ccline.cmdbuff); |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 166 | if (msg_scrolled == 0) |
| 167 | compute_cmdrow(); |
| 168 | MSG(""); |
| 169 | redraw_cmdline = TRUE; |
| 170 | } |
| 171 | |
Bram Moolenaar | ee219b0 | 2017-12-17 14:55:01 +0100 | [diff] [blame] | 172 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 173 | /* |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 174 | * Guess that the pattern matches everything. Only finds specific cases, such |
| 175 | * as a trailing \|, which can happen while typing a pattern. |
| 176 | */ |
| 177 | static int |
| 178 | empty_pattern(char_u *p) |
| 179 | { |
Bram Moolenaar | 200ea8f | 2018-01-02 15:37:46 +0100 | [diff] [blame] | 180 | size_t n = STRLEN(p); |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 181 | |
| 182 | /* remove trailing \v and the like */ |
| 183 | while (n >= 2 && p[n - 2] == '\\' |
| 184 | && vim_strchr((char_u *)"mMvVcCZ", p[n - 1]) != NULL) |
| 185 | n -= 2; |
| 186 | return n == 0 || (n >= 2 && p[n - 2] == '\\' && p[n - 1] == '|'); |
| 187 | } |
Bram Moolenaar | ee219b0 | 2017-12-17 14:55:01 +0100 | [diff] [blame] | 188 | #endif |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 189 | |
| 190 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 191 | * getcmdline() - accept a command line starting with firstc. |
| 192 | * |
| 193 | * firstc == ':' get ":" command line. |
| 194 | * firstc == '/' or '?' get search pattern |
| 195 | * firstc == '=' get expression |
| 196 | * firstc == '@' get text for input() function |
| 197 | * firstc == '>' get text for debug mode |
| 198 | * firstc == NUL get text for :insert command |
| 199 | * firstc == -1 like NUL, and break on CTRL-C |
| 200 | * |
| 201 | * The line is collected in ccline.cmdbuff, which is reallocated to fit the |
| 202 | * command line. |
| 203 | * |
| 204 | * Careful: getcmdline() can be called recursively! |
| 205 | * |
| 206 | * Return pointer to allocated string if there is a commandline, NULL |
| 207 | * otherwise. |
| 208 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 209 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 210 | getcmdline( |
| 211 | int firstc, |
| 212 | long count UNUSED, /* only used for incremental search */ |
| 213 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 214 | { |
| 215 | int c; |
| 216 | int i; |
| 217 | int j; |
| 218 | int gotesc = FALSE; /* TRUE when <ESC> just typed */ |
| 219 | int do_abbr; /* when TRUE check for abbr. */ |
| 220 | #ifdef FEAT_CMDHIST |
| 221 | char_u *lookfor = NULL; /* string to match */ |
| 222 | int hiscnt; /* current history line in use */ |
| 223 | int histype; /* history type to be used */ |
| 224 | #endif |
| 225 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 226 | pos_T search_start; /* where 'incsearch' starts searching */ |
| 227 | pos_T save_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 228 | colnr_T old_curswant; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 229 | colnr_T init_curswant = curwin->w_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 230 | colnr_T old_leftcol; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 231 | colnr_T init_leftcol = curwin->w_leftcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 232 | linenr_T old_topline; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 233 | linenr_T init_topline = curwin->w_topline; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 234 | pos_T match_start = curwin->w_cursor; |
| 235 | pos_T match_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 236 | # ifdef FEAT_DIFF |
| 237 | int old_topfill; |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 238 | int init_topfill = curwin->w_topfill; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 239 | # endif |
Bram Moolenaar | 9d34d90 | 2018-04-27 22:18:12 +0200 | [diff] [blame^] | 240 | linenr_T old_botline, old_empty_rows; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 241 | linenr_T init_botline = curwin->w_botline; |
Bram Moolenaar | 9d34d90 | 2018-04-27 22:18:12 +0200 | [diff] [blame^] | 242 | linenr_T init_empty_rows = curwin->w_empty_rows; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 243 | int did_incsearch = FALSE; |
| 244 | int incsearch_postponed = FALSE; |
| 245 | #endif |
| 246 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 247 | int wim_index = 0; /* index in wim_flags[] */ |
| 248 | int res; |
| 249 | int save_msg_scroll = msg_scroll; |
| 250 | int save_State = State; /* remember State when called */ |
| 251 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 252 | #ifdef FEAT_MOUSE |
| 253 | /* mouse drag and release events are ignored, unless they are |
| 254 | * preceded with a mouse down event */ |
| 255 | int ignore_drag_release = TRUE; |
| 256 | #endif |
| 257 | #ifdef FEAT_EVAL |
| 258 | int break_ctrl_c = FALSE; |
| 259 | #endif |
| 260 | expand_T xpc; |
| 261 | long *b_im_ptr = NULL; |
Bram Moolenaar | 4d85051 | 2017-02-09 18:25:14 +0100 | [diff] [blame] | 262 | #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA) |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 263 | /* Everything that may work recursively should save and restore the |
| 264 | * current command line in save_ccline. That includes update_screen(), a |
| 265 | * custom status line may invoke ":normal". */ |
| 266 | struct cmdline_info save_ccline; |
| 267 | #endif |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 268 | int cmdline_type; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 269 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 270 | #ifdef FEAT_EVAL |
| 271 | if (firstc == -1) |
| 272 | { |
| 273 | firstc = NUL; |
| 274 | break_ctrl_c = TRUE; |
| 275 | } |
| 276 | #endif |
| 277 | #ifdef FEAT_RIGHTLEFT |
| 278 | /* start without Hebrew mapping for a command line */ |
| 279 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 280 | cmd_hkmap = 0; |
| 281 | #endif |
| 282 | |
| 283 | ccline.overstrike = FALSE; /* always start in insert mode */ |
| 284 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 285 | CLEAR_POS(&match_end); |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 286 | save_cursor = curwin->w_cursor; /* may be restored later */ |
| 287 | search_start = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 288 | old_curswant = curwin->w_curswant; |
| 289 | old_leftcol = curwin->w_leftcol; |
| 290 | old_topline = curwin->w_topline; |
| 291 | # ifdef FEAT_DIFF |
| 292 | old_topfill = curwin->w_topfill; |
| 293 | # endif |
| 294 | old_botline = curwin->w_botline; |
Bram Moolenaar | 9d34d90 | 2018-04-27 22:18:12 +0200 | [diff] [blame^] | 295 | old_empty_rows = curwin->w_empty_rows; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 296 | #endif |
| 297 | |
| 298 | /* |
| 299 | * set some variables for redrawcmd() |
| 300 | */ |
| 301 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 302 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 303 | |
| 304 | /* alloc initial ccline.cmdbuff */ |
| 305 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 306 | if (ccline.cmdbuff == NULL) |
| 307 | return NULL; /* out of memory */ |
| 308 | ccline.cmdlen = ccline.cmdpos = 0; |
| 309 | ccline.cmdbuff[0] = NUL; |
Bram Moolenaar | f2405ed | 2017-03-16 19:58:25 +0100 | [diff] [blame] | 310 | sb_text_start_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 311 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 312 | /* autoindent for :insert and :append */ |
| 313 | if (firstc <= 0) |
| 314 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 315 | vim_memset(ccline.cmdbuff, ' ', indent); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 316 | ccline.cmdbuff[indent] = NUL; |
| 317 | ccline.cmdpos = indent; |
| 318 | ccline.cmdspos = indent; |
| 319 | ccline.cmdlen = indent; |
| 320 | } |
| 321 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 322 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 323 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 324 | |
| 325 | #ifdef FEAT_RIGHTLEFT |
| 326 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 327 | && (firstc == '/' || firstc == '?')) |
| 328 | cmdmsg_rl = TRUE; |
| 329 | else |
| 330 | cmdmsg_rl = FALSE; |
| 331 | #endif |
| 332 | |
| 333 | redir_off = TRUE; /* don't redirect the typed command */ |
| 334 | if (!cmd_silent) |
| 335 | { |
| 336 | i = msg_scrolled; |
| 337 | msg_scrolled = 0; /* avoid wait_return message */ |
| 338 | gotocmdline(TRUE); |
| 339 | msg_scrolled += i; |
| 340 | redrawcmdprompt(); /* draw prompt or indent */ |
| 341 | set_cmdspos(); |
| 342 | } |
| 343 | xpc.xp_context = EXPAND_NOTHING; |
| 344 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 345 | #ifndef BACKSLASH_IN_FILENAME |
| 346 | xpc.xp_shell = FALSE; |
| 347 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 348 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 349 | #if defined(FEAT_EVAL) |
| 350 | if (ccline.input_fn) |
| 351 | { |
| 352 | xpc.xp_context = ccline.xp_context; |
| 353 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 354 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 355 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 356 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 357 | } |
| 358 | #endif |
| 359 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 360 | /* |
| 361 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 362 | * doing ":@0" when register 0 doesn't contain a CR. |
| 363 | */ |
| 364 | msg_scroll = FALSE; |
| 365 | |
| 366 | State = CMDLINE; |
| 367 | |
| 368 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 369 | { |
| 370 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 371 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 372 | b_im_ptr = &curbuf->b_p_iminsert; |
| 373 | else |
| 374 | b_im_ptr = &curbuf->b_p_imsearch; |
| 375 | if (*b_im_ptr == B_IMODE_LMAP) |
| 376 | State |= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 377 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 378 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 379 | #endif |
| 380 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 381 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 382 | else if (p_imcmdline) |
| 383 | im_set_active(TRUE); |
| 384 | #endif |
| 385 | |
| 386 | #ifdef FEAT_MOUSE |
| 387 | setmouse(); |
| 388 | #endif |
| 389 | #ifdef CURSOR_SHAPE |
| 390 | ui_cursor_shape(); /* may show different cursor shape */ |
| 391 | #endif |
| 392 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 393 | /* When inside an autocommand for writing "exiting" may be set and |
| 394 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 395 | settmode(TMODE_RAW); |
| 396 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 397 | /* Trigger CmdlineEnter autocommands. */ |
| 398 | cmdline_type = firstc == NUL ? '-' : firstc; |
| 399 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINEENTER); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 400 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 401 | #ifdef FEAT_CMDHIST |
| 402 | init_history(); |
| 403 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 404 | histype = hist_char2type(firstc); |
| 405 | #endif |
| 406 | |
| 407 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 408 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 409 | #endif |
| 410 | |
Bram Moolenaar | 15a35c4 | 2014-06-25 12:26:46 +0200 | [diff] [blame] | 411 | /* If something above caused an error, reset the flags, we do want to type |
| 412 | * and execute commands. Display may be messed up a bit. */ |
| 413 | if (did_emsg) |
| 414 | redrawcmd(); |
| 415 | did_emsg = FALSE; |
| 416 | got_int = FALSE; |
| 417 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 418 | /* |
| 419 | * Collect the command string, handling editing keys. |
| 420 | */ |
| 421 | for (;;) |
| 422 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 423 | redir_off = TRUE; /* Don't redirect the typed command. |
| 424 | Repeated, because a ":redir" inside |
| 425 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 426 | #ifdef USE_ON_FLY_SCROLL |
| 427 | dont_scroll = FALSE; /* allow scrolling here */ |
| 428 | #endif |
| 429 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 430 | |
Bram Moolenaar | 72532d3 | 2018-04-07 19:09:09 +0200 | [diff] [blame] | 431 | did_emsg = FALSE; /* There can't really be a reason why an error |
| 432 | that occurs while typing a command should |
| 433 | cause the command not to be executed. */ |
| 434 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 435 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 436 | |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 437 | /* Get a character. Ignore K_IGNORE and K_NOP, they should not do |
| 438 | * anything, such as stop completion. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 439 | do |
| 440 | { |
| 441 | c = safe_vgetc(); |
Bram Moolenaar | c5aa55d | 2017-11-28 20:47:40 +0100 | [diff] [blame] | 442 | } while (c == K_IGNORE || c == K_NOP); |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 443 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 444 | if (KeyTyped) |
| 445 | { |
| 446 | some_key_typed = TRUE; |
| 447 | #ifdef FEAT_RIGHTLEFT |
| 448 | if (cmd_hkmap) |
| 449 | c = hkmap(c); |
| 450 | # ifdef FEAT_FKMAP |
| 451 | if (cmd_fkmap) |
| 452 | c = cmdl_fkmap(c); |
| 453 | # endif |
| 454 | if (cmdmsg_rl && !KeyStuffed) |
| 455 | { |
| 456 | /* Invert horizontal movements and operations. Only when |
| 457 | * typed by the user directly, not when the result of a |
| 458 | * mapping. */ |
| 459 | switch (c) |
| 460 | { |
| 461 | case K_RIGHT: c = K_LEFT; break; |
| 462 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 463 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 464 | case K_LEFT: c = K_RIGHT; break; |
| 465 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 466 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 467 | } |
| 468 | } |
| 469 | #endif |
| 470 | } |
| 471 | |
| 472 | /* |
| 473 | * Ignore got_int when CTRL-C was typed here. |
| 474 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 475 | * ":g/pat/normal /pat" (without the <CR>). |
| 476 | * Don't ignore it for the input() function. |
| 477 | */ |
| 478 | if ((c == Ctrl_C |
| 479 | #ifdef UNIX |
| 480 | || c == intr_char |
| 481 | #endif |
| 482 | ) |
| 483 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 484 | && firstc != '@' |
| 485 | #endif |
| 486 | #ifdef FEAT_EVAL |
| 487 | && !break_ctrl_c |
| 488 | #endif |
| 489 | && !global_busy) |
| 490 | got_int = FALSE; |
| 491 | |
| 492 | #ifdef FEAT_CMDHIST |
| 493 | /* free old command line when finished moving around in the history |
| 494 | * list */ |
| 495 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 496 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 497 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 498 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 499 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 500 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 501 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 502 | VIM_CLEAR(lookfor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 503 | #endif |
| 504 | |
| 505 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 506 | * When there are matching completions to select <S-Tab> works like |
| 507 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 508 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 509 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 510 | c = Ctrl_P; |
| 511 | |
| 512 | #ifdef FEAT_WILDMENU |
| 513 | /* Special translations for 'wildmenu' */ |
| 514 | if (did_wild_list && p_wmnu) |
| 515 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 516 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 517 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 518 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 519 | c = Ctrl_N; |
| 520 | } |
| 521 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 522 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 523 | && ccline.cmdpos > 1 |
| 524 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 525 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 526 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 527 | c = K_DOWN; |
| 528 | #endif |
| 529 | |
| 530 | /* free expanded names when finished walking through matches */ |
| 531 | if (xpc.xp_numfiles != -1 |
| 532 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 533 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 534 | && c != Ctrl_L) |
| 535 | { |
| 536 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 537 | did_wild_list = FALSE; |
| 538 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 539 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 540 | #endif |
| 541 | xpc.xp_context = EXPAND_NOTHING; |
| 542 | wim_index = 0; |
| 543 | #ifdef FEAT_WILDMENU |
| 544 | if (p_wmnu && wild_menu_showing != 0) |
| 545 | { |
| 546 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 547 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 548 | |
| 549 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 550 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 551 | |
| 552 | if (wild_menu_showing == WM_SCROLLED) |
| 553 | { |
| 554 | /* Entered command line, move it up */ |
| 555 | cmdline_row--; |
| 556 | redrawcmd(); |
| 557 | } |
| 558 | else if (save_p_ls != -1) |
| 559 | { |
| 560 | /* restore 'laststatus' and 'winminheight' */ |
| 561 | p_ls = save_p_ls; |
| 562 | p_wmh = save_p_wmh; |
| 563 | last_status(FALSE); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 564 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 565 | update_screen(VALID); /* redraw the screen NOW */ |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 566 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 567 | redrawcmd(); |
| 568 | save_p_ls = -1; |
| 569 | } |
| 570 | else |
| 571 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 572 | win_redraw_last_status(topframe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 573 | redraw_statuslines(); |
| 574 | } |
| 575 | KeyTyped = skt; |
| 576 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 577 | if (ccline.input_fn) |
| 578 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 579 | } |
| 580 | #endif |
| 581 | } |
| 582 | |
| 583 | #ifdef FEAT_WILDMENU |
| 584 | /* Special translations for 'wildmenu' */ |
| 585 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 586 | { |
| 587 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 588 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 589 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 590 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 591 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 592 | { |
| 593 | /* Hitting <Up>: Remove one submenu name in front of the |
| 594 | * cursor */ |
| 595 | int found = FALSE; |
| 596 | |
| 597 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 598 | i = 0; |
| 599 | while (--j > 0) |
| 600 | { |
| 601 | /* check for start of menu name */ |
| 602 | if (ccline.cmdbuff[j] == ' ' |
| 603 | && ccline.cmdbuff[j - 1] != '\\') |
| 604 | { |
| 605 | i = j + 1; |
| 606 | break; |
| 607 | } |
| 608 | /* check for start of submenu name */ |
| 609 | if (ccline.cmdbuff[j] == '.' |
| 610 | && ccline.cmdbuff[j - 1] != '\\') |
| 611 | { |
| 612 | if (found) |
| 613 | { |
| 614 | i = j + 1; |
| 615 | break; |
| 616 | } |
| 617 | else |
| 618 | found = TRUE; |
| 619 | } |
| 620 | } |
| 621 | if (i > 0) |
| 622 | cmdline_del(i); |
| 623 | c = p_wc; |
| 624 | xpc.xp_context = EXPAND_NOTHING; |
| 625 | } |
| 626 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 627 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 628 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 629 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 630 | { |
| 631 | char_u upseg[5]; |
| 632 | |
| 633 | upseg[0] = PATHSEP; |
| 634 | upseg[1] = '.'; |
| 635 | upseg[2] = '.'; |
| 636 | upseg[3] = PATHSEP; |
| 637 | upseg[4] = NUL; |
| 638 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 639 | if (c == K_DOWN |
| 640 | && ccline.cmdpos > 0 |
| 641 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 642 | && (ccline.cmdpos < 3 |
| 643 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 644 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 645 | { |
| 646 | /* go down a directory */ |
| 647 | c = p_wc; |
| 648 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 649 | 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] | 650 | { |
| 651 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 652 | int found = FALSE; |
| 653 | |
| 654 | j = ccline.cmdpos; |
| 655 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 656 | while (--j > i) |
| 657 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 658 | #ifdef FEAT_MBYTE |
| 659 | if (has_mbyte) |
| 660 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 661 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 662 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 663 | { |
| 664 | found = TRUE; |
| 665 | break; |
| 666 | } |
| 667 | } |
| 668 | if (found |
| 669 | && ccline.cmdbuff[j - 1] == '.' |
| 670 | && ccline.cmdbuff[j - 2] == '.' |
| 671 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 672 | { |
| 673 | cmdline_del(j - 2); |
| 674 | c = p_wc; |
| 675 | } |
| 676 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 677 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 678 | { |
| 679 | /* go up a directory */ |
| 680 | int found = FALSE; |
| 681 | |
| 682 | j = ccline.cmdpos - 1; |
| 683 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 684 | while (--j > i) |
| 685 | { |
| 686 | #ifdef FEAT_MBYTE |
| 687 | if (has_mbyte) |
| 688 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 689 | #endif |
| 690 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 691 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 692 | && vim_strchr((char_u *)" *?[{`$%#", |
| 693 | ccline.cmdbuff[j + 1]) == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 694 | #endif |
| 695 | ) |
| 696 | { |
| 697 | if (found) |
| 698 | { |
| 699 | i = j + 1; |
| 700 | break; |
| 701 | } |
| 702 | else |
| 703 | found = TRUE; |
| 704 | } |
| 705 | } |
| 706 | |
| 707 | if (!found) |
| 708 | j = i; |
| 709 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 710 | j += 4; |
| 711 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 712 | && j == i) |
| 713 | j += 3; |
| 714 | else |
| 715 | j = 0; |
| 716 | if (j > 0) |
| 717 | { |
| 718 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 719 | * machine-specific stuff here and in upseg init */ |
| 720 | cmdline_del(j); |
| 721 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 722 | } |
| 723 | else if (ccline.cmdpos > i) |
| 724 | cmdline_del(i); |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 725 | |
| 726 | /* Now complete in the new directory. Set KeyTyped in case the |
| 727 | * Up key came from a mapping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 728 | c = p_wc; |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 729 | KeyTyped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 730 | } |
| 731 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 732 | |
| 733 | #endif /* FEAT_WILDMENU */ |
| 734 | |
| 735 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 736 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 737 | if (c == Ctrl_BSL) |
| 738 | { |
| 739 | ++no_mapping; |
| 740 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 741 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 742 | --no_mapping; |
| 743 | --allow_keys; |
Bram Moolenaar | b735681 | 2012-10-11 04:04:37 +0200 | [diff] [blame] | 744 | /* CTRL-\ e doesn't work when obtaining an expression, unless it |
| 745 | * is in a mapping. */ |
| 746 | if (c != Ctrl_N && c != Ctrl_G && (c != 'e' |
| 747 | || (ccline.cmdfirstc == '=' && KeyTyped))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 748 | { |
| 749 | vungetc(c); |
| 750 | c = Ctrl_BSL; |
| 751 | } |
| 752 | #ifdef FEAT_EVAL |
| 753 | else if (c == 'e') |
| 754 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 755 | char_u *p = NULL; |
| 756 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 757 | |
| 758 | /* |
| 759 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 760 | * Need to save and restore the current command line, to be |
| 761 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 762 | */ |
| 763 | if (ccline.cmdpos == ccline.cmdlen) |
| 764 | new_cmdpos = 99999; /* keep it at the end */ |
| 765 | else |
| 766 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 767 | |
| 768 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 769 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 770 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 771 | if (c == '=') |
| 772 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 773 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 774 | * to avoid nasty things like going to another buffer when |
| 775 | * evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 776 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 777 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 778 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 779 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 780 | restore_cmdline(&save_ccline); |
| 781 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 782 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 783 | { |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 784 | len = (int)STRLEN(p); |
| 785 | if (realloc_cmdbuff(len + 1) == OK) |
| 786 | { |
| 787 | ccline.cmdlen = len; |
| 788 | STRCPY(ccline.cmdbuff, p); |
| 789 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 790 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 791 | /* Restore the cursor or use the position set with |
| 792 | * set_cmdline_pos(). */ |
| 793 | if (new_cmdpos > ccline.cmdlen) |
| 794 | ccline.cmdpos = ccline.cmdlen; |
| 795 | else |
| 796 | ccline.cmdpos = new_cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 797 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 798 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 799 | redrawcmd(); |
| 800 | goto cmdline_changed; |
| 801 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 802 | } |
| 803 | } |
| 804 | beep_flush(); |
Bram Moolenaar | 66b4bf8 | 2010-11-16 14:06:08 +0100 | [diff] [blame] | 805 | got_int = FALSE; /* don't abandon the command line */ |
| 806 | did_emsg = FALSE; |
| 807 | emsg_on_display = FALSE; |
| 808 | redrawcmd(); |
| 809 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 810 | } |
| 811 | #endif |
| 812 | else |
| 813 | { |
| 814 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 815 | restart_edit = 'a'; |
| 816 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 817 | in history */ |
| 818 | goto returncmd; /* back to Normal mode */ |
| 819 | } |
| 820 | } |
| 821 | |
| 822 | #ifdef FEAT_CMDWIN |
| 823 | if (c == cedit_key || c == K_CMDWIN) |
| 824 | { |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 825 | if (ex_normal_busy == 0 && got_int == FALSE) |
| 826 | { |
| 827 | /* |
| 828 | * Open a window to edit the command line (and history). |
| 829 | */ |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 830 | c = open_cmdwin(); |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 831 | some_key_typed = TRUE; |
| 832 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 833 | } |
| 834 | # ifdef FEAT_DIGRAPHS |
| 835 | else |
| 836 | # endif |
| 837 | #endif |
| 838 | #ifdef FEAT_DIGRAPHS |
| 839 | c = do_digraph(c); |
| 840 | #endif |
| 841 | |
| 842 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 843 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 844 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 845 | /* In Ex mode a backslash escapes a newline. */ |
| 846 | if (exmode_active |
| 847 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 848 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 849 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 850 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 851 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 852 | if (c == K_KENTER) |
| 853 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 854 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 855 | else |
| 856 | { |
| 857 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 858 | truncate the cmdline now. */ |
| 859 | if (ccheck_abbr(c + ABBR_OFF)) |
| 860 | goto cmdline_changed; |
| 861 | if (!cmd_silent) |
| 862 | { |
| 863 | windgoto(msg_row, 0); |
| 864 | out_flush(); |
| 865 | } |
| 866 | break; |
| 867 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 868 | } |
| 869 | |
| 870 | /* |
| 871 | * Completion for 'wildchar' or 'wildcharm' key. |
| 872 | * - hitting <ESC> twice means: abandon command line. |
| 873 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 874 | * typed, not when it comes from a macro |
| 875 | */ |
| 876 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 877 | { |
| 878 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 879 | { |
| 880 | /* if 'wildmode' contains "list" may still need to list */ |
| 881 | if (xpc.xp_numfiles > 1 |
| 882 | && !did_wild_list |
| 883 | && (wim_flags[wim_index] & WIM_LIST)) |
| 884 | { |
| 885 | (void)showmatches(&xpc, FALSE); |
| 886 | redrawcmd(); |
| 887 | did_wild_list = TRUE; |
| 888 | } |
| 889 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 890 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 891 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 892 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 893 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 894 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 895 | else |
| 896 | res = OK; /* don't insert 'wildchar' now */ |
| 897 | } |
| 898 | else /* typed p_wc first time */ |
| 899 | { |
| 900 | wim_index = 0; |
| 901 | j = ccline.cmdpos; |
| 902 | /* if 'wildmode' first contains "longest", get longest |
| 903 | * common part */ |
| 904 | if (wim_flags[0] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 905 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 906 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 907 | else |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 908 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP, |
| 909 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 910 | |
| 911 | /* if interrupted while completing, behave like it failed */ |
| 912 | if (got_int) |
| 913 | { |
| 914 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 915 | got_int = FALSE; /* don't abandon the command line */ |
| 916 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 917 | #ifdef FEAT_WILDMENU |
| 918 | xpc.xp_context = EXPAND_NOTHING; |
| 919 | #endif |
| 920 | goto cmdline_changed; |
| 921 | } |
| 922 | |
| 923 | /* when more than one match, and 'wildmode' first contains |
| 924 | * "list", or no change and 'wildmode' contains "longest,list", |
| 925 | * list all matches */ |
| 926 | if (res == OK && xpc.xp_numfiles > 1) |
| 927 | { |
| 928 | /* a "longest" that didn't do anything is skipped (but not |
| 929 | * "list:longest") */ |
| 930 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 931 | wim_index = 1; |
| 932 | if ((wim_flags[wim_index] & WIM_LIST) |
| 933 | #ifdef FEAT_WILDMENU |
| 934 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 935 | #endif |
| 936 | ) |
| 937 | { |
| 938 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 939 | { |
| 940 | #ifdef FEAT_WILDMENU |
| 941 | int p_wmnu_save = p_wmnu; |
| 942 | p_wmnu = 0; |
| 943 | #endif |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 944 | /* remove match */ |
| 945 | nextwild(&xpc, WILD_PREV, 0, firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 946 | #ifdef FEAT_WILDMENU |
| 947 | p_wmnu = p_wmnu_save; |
| 948 | #endif |
| 949 | } |
| 950 | #ifdef FEAT_WILDMENU |
| 951 | (void)showmatches(&xpc, p_wmnu |
| 952 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 953 | #else |
| 954 | (void)showmatches(&xpc, FALSE); |
| 955 | #endif |
| 956 | redrawcmd(); |
| 957 | did_wild_list = TRUE; |
| 958 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 959 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 960 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 961 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 962 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 963 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 964 | } |
| 965 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 966 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 967 | } |
| 968 | #ifdef FEAT_WILDMENU |
| 969 | else if (xpc.xp_numfiles == -1) |
| 970 | xpc.xp_context = EXPAND_NOTHING; |
| 971 | #endif |
| 972 | } |
| 973 | if (wim_index < 3) |
| 974 | ++wim_index; |
| 975 | if (c == ESC) |
| 976 | gotesc = TRUE; |
| 977 | if (res == OK) |
| 978 | goto cmdline_changed; |
| 979 | } |
| 980 | |
| 981 | gotesc = FALSE; |
| 982 | |
| 983 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 984 | if (c == K_S_TAB && KeyTyped) |
| 985 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 986 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK |
| 987 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK |
| 988 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 989 | goto cmdline_changed; |
| 990 | } |
| 991 | |
| 992 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 993 | c = NL; |
| 994 | |
| 995 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 996 | |
| 997 | /* |
| 998 | * Big switch for a typed command line character. |
| 999 | */ |
| 1000 | switch (c) |
| 1001 | { |
| 1002 | case K_BS: |
| 1003 | case Ctrl_H: |
| 1004 | case K_DEL: |
| 1005 | case K_KDEL: |
| 1006 | case Ctrl_W: |
| 1007 | #ifdef FEAT_FKMAP |
| 1008 | if (cmd_fkmap && c == K_BS) |
| 1009 | c = K_DEL; |
| 1010 | #endif |
| 1011 | if (c == K_KDEL) |
| 1012 | c = K_DEL; |
| 1013 | |
| 1014 | /* |
| 1015 | * delete current character is the same as backspace on next |
| 1016 | * character, except at end of line |
| 1017 | */ |
| 1018 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 1019 | ++ccline.cmdpos; |
| 1020 | #ifdef FEAT_MBYTE |
| 1021 | if (has_mbyte && c == K_DEL) |
| 1022 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 1023 | ccline.cmdbuff + ccline.cmdpos); |
| 1024 | #endif |
| 1025 | if (ccline.cmdpos > 0) |
| 1026 | { |
| 1027 | char_u *p; |
| 1028 | |
| 1029 | j = ccline.cmdpos; |
| 1030 | p = ccline.cmdbuff + j; |
| 1031 | #ifdef FEAT_MBYTE |
| 1032 | if (has_mbyte) |
| 1033 | { |
| 1034 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1035 | if (c == Ctrl_W) |
| 1036 | { |
| 1037 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 1038 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1039 | i = mb_get_class(p); |
| 1040 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 1041 | p = mb_prevptr(ccline.cmdbuff, p); |
| 1042 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1043 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1044 | } |
| 1045 | } |
| 1046 | else |
| 1047 | #endif |
| 1048 | if (c == Ctrl_W) |
| 1049 | { |
| 1050 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 1051 | --p; |
| 1052 | i = vim_iswordc(p[-1]); |
| 1053 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 1054 | && vim_iswordc(p[-1]) == i) |
| 1055 | --p; |
| 1056 | } |
| 1057 | else |
| 1058 | --p; |
| 1059 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 1060 | ccline.cmdlen -= j - ccline.cmdpos; |
| 1061 | i = ccline.cmdpos; |
| 1062 | while (i < ccline.cmdlen) |
| 1063 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1064 | |
| 1065 | /* Truncate at the end, required for multi-byte chars. */ |
| 1066 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1067 | #ifdef FEAT_SEARCH_EXTRA |
| 1068 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1069 | { |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1070 | search_start = save_cursor; |
| 1071 | /* save view settings, so that the screen |
| 1072 | * won't be restored at the wrong position */ |
| 1073 | old_curswant = init_curswant; |
| 1074 | old_leftcol = init_leftcol; |
| 1075 | old_topline = init_topline; |
| 1076 | # ifdef FEAT_DIFF |
| 1077 | old_topfill = init_topfill; |
| 1078 | # endif |
| 1079 | old_botline = init_botline; |
Bram Moolenaar | 9d34d90 | 2018-04-27 22:18:12 +0200 | [diff] [blame^] | 1080 | old_empty_rows = init_empty_rows; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1081 | } |
| 1082 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1083 | redrawcmd(); |
| 1084 | } |
| 1085 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 1086 | && ccline.cmdprompt == NULL && indent == 0) |
| 1087 | { |
| 1088 | /* In ex and debug mode it doesn't make sense to return. */ |
| 1089 | if (exmode_active |
| 1090 | #ifdef FEAT_EVAL |
| 1091 | || ccline.cmdfirstc == '>' |
| 1092 | #endif |
| 1093 | ) |
| 1094 | goto cmdline_not_changed; |
| 1095 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 1096 | VIM_CLEAR(ccline.cmdbuff); /* no commandline to return */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1097 | if (!cmd_silent) |
| 1098 | { |
| 1099 | #ifdef FEAT_RIGHTLEFT |
| 1100 | if (cmdmsg_rl) |
| 1101 | msg_col = Columns; |
| 1102 | else |
| 1103 | #endif |
| 1104 | msg_col = 0; |
| 1105 | msg_putchar(' '); /* delete ':' */ |
| 1106 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1107 | #ifdef FEAT_SEARCH_EXTRA |
| 1108 | if (ccline.cmdlen == 0) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1109 | search_start = save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1110 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1111 | redraw_cmdline = TRUE; |
| 1112 | goto returncmd; /* back to cmd mode */ |
| 1113 | } |
| 1114 | goto cmdline_changed; |
| 1115 | |
| 1116 | case K_INS: |
| 1117 | case K_KINS: |
| 1118 | #ifdef FEAT_FKMAP |
| 1119 | /* if Farsi mode set, we are in reverse insert mode - |
| 1120 | Do not change the mode */ |
| 1121 | if (cmd_fkmap) |
| 1122 | beep_flush(); |
| 1123 | else |
| 1124 | #endif |
| 1125 | ccline.overstrike = !ccline.overstrike; |
| 1126 | #ifdef CURSOR_SHAPE |
| 1127 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1128 | #endif |
| 1129 | goto cmdline_not_changed; |
| 1130 | |
| 1131 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1132 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1133 | { |
| 1134 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1135 | State ^= LANGMAP; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1136 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1137 | im_set_active(FALSE); /* Disable input method */ |
| 1138 | #endif |
| 1139 | if (b_im_ptr != NULL) |
| 1140 | { |
| 1141 | if (State & LANGMAP) |
| 1142 | *b_im_ptr = B_IMODE_LMAP; |
| 1143 | else |
| 1144 | *b_im_ptr = B_IMODE_NONE; |
| 1145 | } |
| 1146 | } |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 1147 | #ifdef HAVE_INPUT_METHOD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1148 | else |
| 1149 | { |
| 1150 | /* There are no ":lmap" mappings, toggle IM. When |
| 1151 | * 'imdisable' is set don't try getting the status, it's |
| 1152 | * always off. */ |
| 1153 | if ((p_imdisable && b_im_ptr != NULL) |
| 1154 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1155 | { |
| 1156 | im_set_active(FALSE); /* Disable input method */ |
| 1157 | if (b_im_ptr != NULL) |
| 1158 | *b_im_ptr = B_IMODE_NONE; |
| 1159 | } |
| 1160 | else |
| 1161 | { |
| 1162 | im_set_active(TRUE); /* Enable input method */ |
| 1163 | if (b_im_ptr != NULL) |
| 1164 | *b_im_ptr = B_IMODE_IM; |
| 1165 | } |
| 1166 | } |
| 1167 | #endif |
| 1168 | if (b_im_ptr != NULL) |
| 1169 | { |
| 1170 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1171 | set_iminsert_global(); |
| 1172 | else |
| 1173 | set_imsearch_global(); |
| 1174 | } |
| 1175 | #ifdef CURSOR_SHAPE |
| 1176 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1177 | #endif |
Bram Moolenaar | 4033c55 | 2017-09-16 20:54:51 +0200 | [diff] [blame] | 1178 | #if defined(FEAT_KEYMAP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1179 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1180 | status_redraw_curbuf(); |
| 1181 | #endif |
| 1182 | goto cmdline_not_changed; |
| 1183 | |
| 1184 | /* case '@': only in very old vi */ |
| 1185 | case Ctrl_U: |
| 1186 | /* delete all characters left of the cursor */ |
| 1187 | j = ccline.cmdpos; |
| 1188 | ccline.cmdlen -= j; |
| 1189 | i = ccline.cmdpos = 0; |
| 1190 | while (i < ccline.cmdlen) |
| 1191 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1192 | /* Truncate at the end, required for multi-byte chars. */ |
| 1193 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1194 | #ifdef FEAT_SEARCH_EXTRA |
| 1195 | if (ccline.cmdlen == 0) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1196 | search_start = save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1197 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1198 | redrawcmd(); |
| 1199 | goto cmdline_changed; |
| 1200 | |
| 1201 | #ifdef FEAT_CLIPBOARD |
| 1202 | case Ctrl_Y: |
| 1203 | /* Copy the modeless selection, if there is one. */ |
| 1204 | if (clip_star.state != SELECT_CLEARED) |
| 1205 | { |
| 1206 | if (clip_star.state == SELECT_DONE) |
| 1207 | clip_copy_modeless_selection(TRUE); |
| 1208 | goto cmdline_not_changed; |
| 1209 | } |
| 1210 | break; |
| 1211 | #endif |
| 1212 | |
| 1213 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1214 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1215 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1216 | * ":normal" runs out of characters. */ |
| 1217 | if (exmode_active |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 1218 | && (ex_normal_busy == 0 || typebuf.tb_len > 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1219 | goto cmdline_not_changed; |
| 1220 | |
| 1221 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1222 | putting it in history */ |
| 1223 | goto returncmd; /* back to cmd mode */ |
| 1224 | |
| 1225 | case Ctrl_R: /* insert register */ |
| 1226 | #ifdef USE_ON_FLY_SCROLL |
| 1227 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1228 | #endif |
| 1229 | putcmdline('"', TRUE); |
| 1230 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1231 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1232 | if (i == Ctrl_O) |
| 1233 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1234 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1235 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1236 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1237 | --no_mapping; |
| 1238 | #ifdef FEAT_EVAL |
| 1239 | /* |
| 1240 | * Insert the result of an expression. |
| 1241 | * Need to save the current command line, to be able to enter |
| 1242 | * a new one... |
| 1243 | */ |
| 1244 | new_cmdpos = -1; |
| 1245 | if (c == '=') |
| 1246 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1247 | if (ccline.cmdfirstc == '=')/* can't do this recursively */ |
| 1248 | { |
| 1249 | beep_flush(); |
| 1250 | c = ESC; |
| 1251 | } |
| 1252 | else |
| 1253 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1254 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1255 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1256 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1257 | } |
| 1258 | } |
| 1259 | #endif |
| 1260 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1261 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1262 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1263 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1264 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1265 | /* When there was a serious error abort getting the |
| 1266 | * command line. */ |
| 1267 | if (aborting()) |
| 1268 | { |
| 1269 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1270 | putting it in history */ |
| 1271 | goto returncmd; /* back to cmd mode */ |
| 1272 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1273 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1274 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1275 | #ifdef FEAT_EVAL |
| 1276 | if (new_cmdpos >= 0) |
| 1277 | { |
| 1278 | /* set_cmdline_pos() was used */ |
| 1279 | if (new_cmdpos > ccline.cmdlen) |
| 1280 | ccline.cmdpos = ccline.cmdlen; |
| 1281 | else |
| 1282 | ccline.cmdpos = new_cmdpos; |
| 1283 | } |
| 1284 | #endif |
| 1285 | } |
| 1286 | redrawcmd(); |
| 1287 | goto cmdline_changed; |
| 1288 | |
| 1289 | case Ctrl_D: |
| 1290 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1291 | break; /* Use ^D as normal char instead */ |
| 1292 | |
| 1293 | redrawcmd(); |
| 1294 | continue; /* don't do incremental search now */ |
| 1295 | |
| 1296 | case K_RIGHT: |
| 1297 | case K_S_RIGHT: |
| 1298 | case K_C_RIGHT: |
| 1299 | do |
| 1300 | { |
| 1301 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1302 | break; |
| 1303 | i = cmdline_charsize(ccline.cmdpos); |
| 1304 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1305 | break; |
| 1306 | ccline.cmdspos += i; |
| 1307 | #ifdef FEAT_MBYTE |
| 1308 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1309 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1310 | + ccline.cmdpos); |
| 1311 | else |
| 1312 | #endif |
| 1313 | ++ccline.cmdpos; |
| 1314 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1315 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 1316 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1317 | && ccline.cmdbuff[ccline.cmdpos] != ' '); |
| 1318 | #ifdef FEAT_MBYTE |
| 1319 | if (has_mbyte) |
| 1320 | set_cmdspos_cursor(); |
| 1321 | #endif |
| 1322 | goto cmdline_not_changed; |
| 1323 | |
| 1324 | case K_LEFT: |
| 1325 | case K_S_LEFT: |
| 1326 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1327 | if (ccline.cmdpos == 0) |
| 1328 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1329 | do |
| 1330 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1331 | --ccline.cmdpos; |
| 1332 | #ifdef FEAT_MBYTE |
| 1333 | if (has_mbyte) /* move to first byte of char */ |
| 1334 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1335 | ccline.cmdbuff + ccline.cmdpos); |
| 1336 | #endif |
| 1337 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1338 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1339 | while (ccline.cmdpos > 0 |
| 1340 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1341 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1342 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1343 | #ifdef FEAT_MBYTE |
| 1344 | if (has_mbyte) |
| 1345 | set_cmdspos_cursor(); |
| 1346 | #endif |
| 1347 | goto cmdline_not_changed; |
| 1348 | |
| 1349 | case K_IGNORE: |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 1350 | /* Ignore mouse event or open_cmdwin() result. */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1351 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1352 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1353 | #ifdef FEAT_GUI_W32 |
| 1354 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1355 | * cancelled. */ |
| 1356 | case K_F4: |
| 1357 | if (mod_mask == MOD_MASK_ALT) |
| 1358 | { |
| 1359 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1360 | goto cmdline_not_changed; |
| 1361 | } |
| 1362 | break; |
| 1363 | #endif |
| 1364 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1365 | #ifdef FEAT_MOUSE |
| 1366 | case K_MIDDLEDRAG: |
| 1367 | case K_MIDDLERELEASE: |
| 1368 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1369 | |
| 1370 | case K_MIDDLEMOUSE: |
| 1371 | # ifdef FEAT_GUI |
| 1372 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1373 | if (!gui.in_use) |
| 1374 | # endif |
| 1375 | if (!mouse_has(MOUSE_COMMAND)) |
| 1376 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1377 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1378 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1379 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1380 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1381 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1382 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1383 | redrawcmd(); |
| 1384 | goto cmdline_changed; |
| 1385 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1386 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1387 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1388 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1389 | redrawcmd(); |
| 1390 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1391 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1392 | |
| 1393 | case K_LEFTDRAG: |
| 1394 | case K_LEFTRELEASE: |
| 1395 | case K_RIGHTDRAG: |
| 1396 | case K_RIGHTRELEASE: |
| 1397 | /* Ignore drag and release events when the button-down wasn't |
| 1398 | * seen before. */ |
| 1399 | if (ignore_drag_release) |
| 1400 | goto cmdline_not_changed; |
| 1401 | /* FALLTHROUGH */ |
| 1402 | case K_LEFTMOUSE: |
| 1403 | case K_RIGHTMOUSE: |
| 1404 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1405 | ignore_drag_release = TRUE; |
| 1406 | else |
| 1407 | ignore_drag_release = FALSE; |
| 1408 | # ifdef FEAT_GUI |
| 1409 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1410 | if (!gui.in_use) |
| 1411 | # endif |
| 1412 | if (!mouse_has(MOUSE_COMMAND)) |
| 1413 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1414 | # ifdef FEAT_CLIPBOARD |
| 1415 | if (mouse_row < cmdline_row && clip_star.available) |
| 1416 | { |
| 1417 | int button, is_click, is_drag; |
| 1418 | |
| 1419 | /* |
| 1420 | * Handle modeless selection. |
| 1421 | */ |
| 1422 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1423 | &is_click, &is_drag); |
| 1424 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1425 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1426 | { |
| 1427 | /* Translate shift-left to right button. */ |
| 1428 | button = MOUSE_RIGHT; |
| 1429 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1430 | } |
| 1431 | clip_modeless(button, is_click, is_drag); |
| 1432 | goto cmdline_not_changed; |
| 1433 | } |
| 1434 | # endif |
| 1435 | |
| 1436 | set_cmdspos(); |
| 1437 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 1438 | ++ccline.cmdpos) |
| 1439 | { |
| 1440 | i = cmdline_charsize(ccline.cmdpos); |
| 1441 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 1442 | && mouse_col < ccline.cmdspos % Columns + i) |
| 1443 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1444 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1445 | if (has_mbyte) |
| 1446 | { |
| 1447 | /* Count ">" for double-wide char that doesn't fit. */ |
| 1448 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1449 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1450 | + ccline.cmdpos) - 1; |
| 1451 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1452 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1453 | ccline.cmdspos += i; |
| 1454 | } |
| 1455 | goto cmdline_not_changed; |
| 1456 | |
| 1457 | /* Mouse scroll wheel: ignored here */ |
| 1458 | case K_MOUSEDOWN: |
| 1459 | case K_MOUSEUP: |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1460 | case K_MOUSELEFT: |
| 1461 | case K_MOUSERIGHT: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1462 | /* Alternate buttons ignored here */ |
| 1463 | case K_X1MOUSE: |
| 1464 | case K_X1DRAG: |
| 1465 | case K_X1RELEASE: |
| 1466 | case K_X2MOUSE: |
| 1467 | case K_X2DRAG: |
| 1468 | case K_X2RELEASE: |
Bram Moolenaar | 51b0f37 | 2017-11-18 18:52:04 +0100 | [diff] [blame] | 1469 | case K_MOUSEMOVE: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1470 | goto cmdline_not_changed; |
| 1471 | |
| 1472 | #endif /* FEAT_MOUSE */ |
| 1473 | |
| 1474 | #ifdef FEAT_GUI |
| 1475 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 1476 | case K_LEFTRELEASE_NM: |
| 1477 | goto cmdline_not_changed; |
| 1478 | |
| 1479 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1480 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1481 | { |
| 1482 | gui_do_scroll(); |
| 1483 | redrawcmd(); |
| 1484 | } |
| 1485 | goto cmdline_not_changed; |
| 1486 | |
| 1487 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1488 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1489 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1490 | gui_do_horiz_scroll(scrollbar_value, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1491 | redrawcmd(); |
| 1492 | } |
| 1493 | goto cmdline_not_changed; |
| 1494 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1495 | #ifdef FEAT_GUI_TABLINE |
| 1496 | case K_TABLINE: |
| 1497 | case K_TABMENU: |
| 1498 | /* Don't want to change any tabs here. Make sure the same tab |
| 1499 | * is still selected. */ |
| 1500 | if (gui_use_tabline()) |
| 1501 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 1502 | goto cmdline_not_changed; |
| 1503 | #endif |
| 1504 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1505 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1506 | goto cmdline_not_changed; |
| 1507 | |
| 1508 | case Ctrl_B: /* begin of command line */ |
| 1509 | case K_HOME: |
| 1510 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1511 | case K_S_HOME: |
| 1512 | case K_C_HOME: |
| 1513 | ccline.cmdpos = 0; |
| 1514 | set_cmdspos(); |
| 1515 | goto cmdline_not_changed; |
| 1516 | |
| 1517 | case Ctrl_E: /* end of command line */ |
| 1518 | case K_END: |
| 1519 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1520 | case K_S_END: |
| 1521 | case K_C_END: |
| 1522 | ccline.cmdpos = ccline.cmdlen; |
| 1523 | set_cmdspos_cursor(); |
| 1524 | goto cmdline_not_changed; |
| 1525 | |
| 1526 | case Ctrl_A: /* all matches */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1527 | if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1528 | break; |
| 1529 | goto cmdline_changed; |
| 1530 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1531 | case Ctrl_L: |
| 1532 | #ifdef FEAT_SEARCH_EXTRA |
| 1533 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1534 | { |
| 1535 | /* Add a character from under the cursor for 'incsearch' */ |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1536 | if (did_incsearch) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1537 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1538 | curwin->w_cursor = match_end; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1539 | if (!EQUAL_POS(curwin->w_cursor, search_start)) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1540 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1541 | c = gchar_cursor(); |
| 1542 | /* If 'ignorecase' and 'smartcase' are set and the |
| 1543 | * command line has no uppercase characters, convert |
| 1544 | * the character to lowercase */ |
| 1545 | if (p_ic && p_scs |
| 1546 | && !pat_has_uppercase(ccline.cmdbuff)) |
| 1547 | c = MB_TOLOWER(c); |
| 1548 | if (c != NUL) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1549 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1550 | if (c == firstc || vim_strchr((char_u *)( |
Bram Moolenaar | a693d05 | 2017-06-29 22:23:06 +0200 | [diff] [blame] | 1551 | p_magic ? "\\~^$.*[" : "\\^$"), c) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1552 | != NULL) |
| 1553 | { |
| 1554 | /* put a backslash before special |
| 1555 | * characters */ |
| 1556 | stuffcharReadbuff(c); |
| 1557 | c = '\\'; |
| 1558 | } |
| 1559 | break; |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1560 | } |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1561 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1562 | } |
| 1563 | goto cmdline_not_changed; |
| 1564 | } |
| 1565 | #endif |
| 1566 | |
| 1567 | /* completion: longest common part */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1568 | if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1569 | break; |
| 1570 | goto cmdline_changed; |
| 1571 | |
| 1572 | case Ctrl_N: /* next match */ |
| 1573 | case Ctrl_P: /* previous match */ |
Bram Moolenaar | 7df0f63 | 2016-08-26 19:56:00 +0200 | [diff] [blame] | 1574 | if (xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1575 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1576 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, |
| 1577 | 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1578 | break; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1579 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1580 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1581 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | 2f40d12 | 2017-10-24 21:49:36 +0200 | [diff] [blame] | 1582 | /* FALLTHROUGH */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1583 | case K_UP: |
| 1584 | case K_DOWN: |
| 1585 | case K_S_UP: |
| 1586 | case K_S_DOWN: |
| 1587 | case K_PAGEUP: |
| 1588 | case K_KPAGEUP: |
| 1589 | case K_PAGEDOWN: |
| 1590 | case K_KPAGEDOWN: |
| 1591 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 1592 | goto cmdline_not_changed; |
| 1593 | |
| 1594 | i = hiscnt; |
| 1595 | |
| 1596 | /* save current command string so it can be restored later */ |
| 1597 | if (lookfor == NULL) |
| 1598 | { |
| 1599 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 1600 | goto cmdline_not_changed; |
| 1601 | lookfor[ccline.cmdpos] = NUL; |
| 1602 | } |
| 1603 | |
| 1604 | j = (int)STRLEN(lookfor); |
| 1605 | for (;;) |
| 1606 | { |
| 1607 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1608 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1609 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1610 | { |
| 1611 | if (hiscnt == hislen) /* first time */ |
| 1612 | hiscnt = hisidx[histype]; |
| 1613 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 1614 | hiscnt = hislen - 1; |
| 1615 | else if (hiscnt != hisidx[histype] + 1) |
| 1616 | --hiscnt; |
| 1617 | else /* at top of list */ |
| 1618 | { |
| 1619 | hiscnt = i; |
| 1620 | break; |
| 1621 | } |
| 1622 | } |
| 1623 | else /* one step forwards */ |
| 1624 | { |
| 1625 | /* on last entry, clear the line */ |
| 1626 | if (hiscnt == hisidx[histype]) |
| 1627 | { |
| 1628 | hiscnt = hislen; |
| 1629 | break; |
| 1630 | } |
| 1631 | |
| 1632 | /* not on a history line, nothing to do */ |
| 1633 | if (hiscnt == hislen) |
| 1634 | break; |
| 1635 | if (hiscnt == hislen - 1) /* wrap around */ |
| 1636 | hiscnt = 0; |
| 1637 | else |
| 1638 | ++hiscnt; |
| 1639 | } |
| 1640 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 1641 | { |
| 1642 | hiscnt = i; |
| 1643 | break; |
| 1644 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1645 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1646 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1647 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 1648 | lookfor, (size_t)j) == 0) |
| 1649 | break; |
| 1650 | } |
| 1651 | |
| 1652 | if (hiscnt != i) /* jumped to other entry */ |
| 1653 | { |
| 1654 | char_u *p; |
| 1655 | int len; |
| 1656 | int old_firstc; |
| 1657 | |
| 1658 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1659 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1660 | if (hiscnt == hislen) |
| 1661 | p = lookfor; /* back to the old one */ |
| 1662 | else |
| 1663 | p = history[histype][hiscnt].hisstr; |
| 1664 | |
| 1665 | if (histype == HIST_SEARCH |
| 1666 | && p != lookfor |
| 1667 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 1668 | { |
| 1669 | /* Correct for the separator character used when |
| 1670 | * adding the history entry vs the one used now. |
| 1671 | * First loop: count length. |
| 1672 | * Second loop: copy the characters. */ |
| 1673 | for (i = 0; i <= 1; ++i) |
| 1674 | { |
| 1675 | len = 0; |
| 1676 | for (j = 0; p[j] != NUL; ++j) |
| 1677 | { |
| 1678 | /* Replace old sep with new sep, unless it is |
| 1679 | * escaped. */ |
| 1680 | if (p[j] == old_firstc |
| 1681 | && (j == 0 || p[j - 1] != '\\')) |
| 1682 | { |
| 1683 | if (i > 0) |
| 1684 | ccline.cmdbuff[len] = firstc; |
| 1685 | } |
| 1686 | else |
| 1687 | { |
| 1688 | /* Escape new sep, unless it is already |
| 1689 | * escaped. */ |
| 1690 | if (p[j] == firstc |
| 1691 | && (j == 0 || p[j - 1] != '\\')) |
| 1692 | { |
| 1693 | if (i > 0) |
| 1694 | ccline.cmdbuff[len] = '\\'; |
| 1695 | ++len; |
| 1696 | } |
| 1697 | if (i > 0) |
| 1698 | ccline.cmdbuff[len] = p[j]; |
| 1699 | } |
| 1700 | ++len; |
| 1701 | } |
| 1702 | if (i == 0) |
| 1703 | { |
| 1704 | alloc_cmdbuff(len); |
| 1705 | if (ccline.cmdbuff == NULL) |
| 1706 | goto returncmd; |
| 1707 | } |
| 1708 | } |
| 1709 | ccline.cmdbuff[len] = NUL; |
| 1710 | } |
| 1711 | else |
| 1712 | { |
| 1713 | alloc_cmdbuff((int)STRLEN(p)); |
| 1714 | if (ccline.cmdbuff == NULL) |
| 1715 | goto returncmd; |
| 1716 | STRCPY(ccline.cmdbuff, p); |
| 1717 | } |
| 1718 | |
| 1719 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 1720 | redrawcmd(); |
| 1721 | goto cmdline_changed; |
| 1722 | } |
| 1723 | beep_flush(); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1724 | #endif |
| 1725 | goto cmdline_not_changed; |
| 1726 | |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1727 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1728 | case Ctrl_G: /* next match */ |
| 1729 | case Ctrl_T: /* previous match */ |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1730 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1731 | { |
| 1732 | pos_T t; |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 1733 | char_u *pat; |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1734 | int search_flags = SEARCH_NOOF; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1735 | |
Bram Moolenaar | f8f8b2e | 2017-11-02 19:08:48 +0100 | [diff] [blame] | 1736 | if (ccline.cmdlen == 0) |
| 1737 | goto cmdline_not_changed; |
| 1738 | |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 1739 | if (firstc == ccline.cmdbuff[0]) |
| 1740 | pat = last_search_pattern(); |
| 1741 | else |
| 1742 | pat = ccline.cmdbuff; |
| 1743 | |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1744 | save_last_search_pattern(); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1745 | cursor_off(); |
| 1746 | out_flush(); |
| 1747 | if (c == Ctrl_G) |
| 1748 | { |
| 1749 | t = match_end; |
Bram Moolenaar | f8f8b2e | 2017-11-02 19:08:48 +0100 | [diff] [blame] | 1750 | if (LT_POS(match_start, match_end)) |
| 1751 | /* start searching at the end of the match |
| 1752 | * not at the beginning of the next column */ |
| 1753 | (void)decl(&t); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1754 | search_flags += SEARCH_COL; |
| 1755 | } |
| 1756 | else |
| 1757 | t = match_start; |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 1758 | if (!p_hls) |
| 1759 | search_flags += SEARCH_KEEP; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1760 | ++emsg_off; |
| 1761 | i = searchit(curwin, curbuf, &t, |
| 1762 | c == Ctrl_G ? FORWARD : BACKWARD, |
Bram Moolenaar | d048009 | 2017-11-16 22:20:39 +0100 | [diff] [blame] | 1763 | pat, count, search_flags, |
Bram Moolenaar | fbd0b0a | 2017-06-17 18:44:21 +0200 | [diff] [blame] | 1764 | RE_SEARCH, 0, NULL, NULL); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1765 | --emsg_off; |
| 1766 | if (i) |
| 1767 | { |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1768 | search_start = match_start; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1769 | match_end = t; |
| 1770 | match_start = t; |
| 1771 | if (c == Ctrl_T && firstc == '/') |
| 1772 | { |
| 1773 | /* move just before the current match, so that |
| 1774 | * when nv_search finishes the cursor will be |
| 1775 | * put back on the match */ |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1776 | search_start = t; |
| 1777 | (void)decl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1778 | } |
Bram Moolenaar | da5116d | 2017-07-01 23:11:17 +0200 | [diff] [blame] | 1779 | else if (c == Ctrl_G && firstc == '?') |
| 1780 | { |
| 1781 | /* move just after the current match, so that |
| 1782 | * when nv_search finishes the cursor will be |
| 1783 | * put back on the match */ |
| 1784 | search_start = t; |
| 1785 | (void)incl(&search_start); |
| 1786 | } |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1787 | if (LT_POS(t, search_start) && c == Ctrl_G) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1788 | { |
| 1789 | /* wrap around */ |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1790 | search_start = t; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1791 | if (firstc == '?') |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1792 | (void)incl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1793 | else |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1794 | (void)decl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1795 | } |
| 1796 | |
| 1797 | set_search_match(&match_end); |
| 1798 | curwin->w_cursor = match_start; |
| 1799 | changed_cline_bef_curs(); |
| 1800 | update_topline(); |
| 1801 | validate_cursor(); |
| 1802 | highlight_match = TRUE; |
| 1803 | old_curswant = curwin->w_curswant; |
| 1804 | old_leftcol = curwin->w_leftcol; |
| 1805 | old_topline = curwin->w_topline; |
| 1806 | # ifdef FEAT_DIFF |
| 1807 | old_topfill = curwin->w_topfill; |
| 1808 | # endif |
| 1809 | old_botline = curwin->w_botline; |
Bram Moolenaar | 9d34d90 | 2018-04-27 22:18:12 +0200 | [diff] [blame^] | 1810 | old_empty_rows = curwin->w_empty_rows; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1811 | update_screen(NOT_VALID); |
| 1812 | redrawcmdline(); |
| 1813 | } |
| 1814 | else |
| 1815 | vim_beep(BO_ERROR); |
Bram Moolenaar | a1d5c15 | 2017-12-16 19:05:22 +0100 | [diff] [blame] | 1816 | restore_last_search_pattern(); |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1817 | goto cmdline_not_changed; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1818 | } |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1819 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1820 | #endif |
| 1821 | |
| 1822 | case Ctrl_V: |
| 1823 | case Ctrl_Q: |
| 1824 | #ifdef FEAT_MOUSE |
| 1825 | ignore_drag_release = TRUE; |
| 1826 | #endif |
| 1827 | putcmdline('^', TRUE); |
| 1828 | c = get_literal(); /* get next (two) character(s) */ |
| 1829 | do_abbr = FALSE; /* don't do abbreviation now */ |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1830 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1831 | #ifdef FEAT_MBYTE |
| 1832 | /* may need to remove ^ when composing char was typed */ |
| 1833 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 1834 | { |
| 1835 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 1836 | msg_putchar(' '); |
| 1837 | cursorcmd(); |
| 1838 | } |
| 1839 | #endif |
| 1840 | break; |
| 1841 | |
| 1842 | #ifdef FEAT_DIGRAPHS |
| 1843 | case Ctrl_K: |
| 1844 | #ifdef FEAT_MOUSE |
| 1845 | ignore_drag_release = TRUE; |
| 1846 | #endif |
| 1847 | putcmdline('?', TRUE); |
| 1848 | #ifdef USE_ON_FLY_SCROLL |
| 1849 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1850 | #endif |
| 1851 | c = get_digraph(TRUE); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 1852 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1853 | if (c != NUL) |
| 1854 | break; |
| 1855 | |
| 1856 | redrawcmd(); |
| 1857 | goto cmdline_not_changed; |
| 1858 | #endif /* FEAT_DIGRAPHS */ |
| 1859 | |
| 1860 | #ifdef FEAT_RIGHTLEFT |
| 1861 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 1862 | if (!p_ari) |
| 1863 | break; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1864 | # ifdef FEAT_FKMAP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1865 | if (p_altkeymap) |
| 1866 | { |
| 1867 | cmd_fkmap = !cmd_fkmap; |
| 1868 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 1869 | ccline.overstrike = FALSE; |
| 1870 | } |
| 1871 | else /* Hebrew is default */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1872 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1873 | cmd_hkmap = !cmd_hkmap; |
| 1874 | goto cmdline_not_changed; |
| 1875 | #endif |
| 1876 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 1877 | case K_PS: |
| 1878 | bracketed_paste(PASTE_CMDLINE, FALSE, NULL); |
| 1879 | goto cmdline_changed; |
| 1880 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1881 | default: |
| 1882 | #ifdef UNIX |
| 1883 | if (c == intr_char) |
| 1884 | { |
| 1885 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1886 | putting it in history */ |
| 1887 | goto returncmd; /* back to Normal mode */ |
| 1888 | } |
| 1889 | #endif |
| 1890 | /* |
| 1891 | * Normal character with no special meaning. Just set mod_mask |
| 1892 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 1893 | * the string <S-Space>. This should only happen after ^V. |
| 1894 | */ |
| 1895 | if (!IS_SPECIAL(c)) |
| 1896 | mod_mask = 0x0; |
| 1897 | break; |
| 1898 | } |
| 1899 | /* |
| 1900 | * End of switch on command line character. |
| 1901 | * We come here if we have a normal character. |
| 1902 | */ |
| 1903 | |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1904 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1905 | #ifdef FEAT_MBYTE |
| 1906 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1907 | * what check_abbr() expects. */ |
| 1908 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1909 | #endif |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1910 | c) || c == Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1911 | goto cmdline_changed; |
| 1912 | |
| 1913 | /* |
| 1914 | * put the character in the command line |
| 1915 | */ |
| 1916 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 1917 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 1918 | else |
| 1919 | { |
| 1920 | #ifdef FEAT_MBYTE |
| 1921 | if (has_mbyte) |
| 1922 | { |
| 1923 | j = (*mb_char2bytes)(c, IObuff); |
| 1924 | IObuff[j] = NUL; /* exclude composing chars */ |
| 1925 | put_on_cmdline(IObuff, j, TRUE); |
| 1926 | } |
| 1927 | else |
| 1928 | #endif |
| 1929 | { |
| 1930 | IObuff[0] = c; |
| 1931 | put_on_cmdline(IObuff, 1, TRUE); |
| 1932 | } |
| 1933 | } |
| 1934 | goto cmdline_changed; |
| 1935 | |
| 1936 | /* |
| 1937 | * This part implements incremental searches for "/" and "?" |
| 1938 | * Jump to cmdline_not_changed when a character has been read but the command |
| 1939 | * line did not change. Then we only search and redraw if something changed in |
| 1940 | * the past. |
| 1941 | * Jump to cmdline_changed when the command line did change. |
| 1942 | * (Sorry for the goto's, I know it is ugly). |
| 1943 | */ |
| 1944 | cmdline_not_changed: |
| 1945 | #ifdef FEAT_SEARCH_EXTRA |
| 1946 | if (!incsearch_postponed) |
| 1947 | continue; |
| 1948 | #endif |
| 1949 | |
| 1950 | cmdline_changed: |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 1951 | /* Trigger CmdlineChanged autocommands. */ |
| 1952 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINECHANGED); |
Bram Moolenaar | 153b704 | 2018-01-31 15:48:32 +0100 | [diff] [blame] | 1953 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 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; |
Bram Moolenaar | 9d34d90 | 2018-04-27 22:18:12 +0200 | [diff] [blame^] | 2027 | curwin->w_empty_rows = old_empty_rows; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2028 | changed_cline_bef_curs(); |
| 2029 | update_topline(); |
| 2030 | |
| 2031 | if (i != 0) |
| 2032 | { |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 2033 | pos_T save_pos = curwin->w_cursor; |
| 2034 | |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 2035 | match_start = curwin->w_cursor; |
| 2036 | set_search_match(&curwin->w_cursor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2037 | validate_cursor(); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2038 | end_pos = curwin->w_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 2039 | match_end = end_pos; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 2040 | curwin->w_cursor = save_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2041 | } |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 2042 | else |
| 2043 | end_pos = curwin->w_cursor; /* shutup gcc 4 */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2044 | |
Bram Moolenaar | 6621605 | 2017-12-16 16:33:44 +0100 | [diff] [blame] | 2045 | /* Disable 'hlsearch' highlighting if the pattern matches |
| 2046 | * everything. Avoids a flash when typing "foo\|". */ |
| 2047 | if (empty_pattern(ccline.cmdbuff)) |
| 2048 | SET_NO_HLSEARCH(TRUE); |
| 2049 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2050 | validate_cursor(); |
Bram Moolenaar | 27a2319 | 2006-09-14 09:27:26 +0000 | [diff] [blame] | 2051 | /* May redraw the status line to show the cursor position. */ |
| 2052 | if (p_ru && curwin->w_status_height > 0) |
| 2053 | curwin->w_redr_status = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2054 | |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 2055 | save_cmdline(&save_ccline); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 2056 | update_screen(SOME_VALID); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 2057 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 2e51d9a | 2017-10-29 16:40:30 +0100 | [diff] [blame] | 2058 | restore_last_search_pattern(); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 2059 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 2060 | /* Leave it at the end to make CTRL-R CTRL-W work. */ |
| 2061 | if (i != 0) |
| 2062 | curwin->w_cursor = end_pos; |
| 2063 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2064 | msg_starthere(); |
| 2065 | redrawcmdline(); |
| 2066 | did_incsearch = TRUE; |
| 2067 | } |
| 2068 | #else /* FEAT_SEARCH_EXTRA */ |
| 2069 | ; |
| 2070 | #endif |
| 2071 | |
| 2072 | #ifdef FEAT_RIGHTLEFT |
| 2073 | if (cmdmsg_rl |
| 2074 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2075 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2076 | # endif |
| 2077 | ) |
| 2078 | /* Always redraw the whole command line to fix shaping and |
Bram Moolenaar | 58437e0 | 2012-02-22 17:58:04 +0100 | [diff] [blame] | 2079 | * right-left typing. Not efficient, but it works. |
| 2080 | * Do it only when there are no characters left to read |
| 2081 | * to avoid useless intermediate redraws. */ |
| 2082 | if (vpeekc() == NUL) |
| 2083 | redrawcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2084 | #endif |
| 2085 | } |
| 2086 | |
| 2087 | returncmd: |
| 2088 | |
| 2089 | #ifdef FEAT_RIGHTLEFT |
| 2090 | cmdmsg_rl = FALSE; |
| 2091 | #endif |
| 2092 | |
| 2093 | #ifdef FEAT_FKMAP |
| 2094 | cmd_fkmap = 0; |
| 2095 | #endif |
| 2096 | |
| 2097 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2098 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2099 | |
| 2100 | #ifdef FEAT_SEARCH_EXTRA |
| 2101 | if (did_incsearch) |
| 2102 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 2103 | if (gotesc) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 2104 | curwin->w_cursor = save_cursor; |
| 2105 | else |
| 2106 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2107 | if (!EQUAL_POS(save_cursor, search_start)) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 2108 | { |
| 2109 | /* put the '" mark at the original position */ |
| 2110 | curwin->w_cursor = save_cursor; |
| 2111 | setpcmark(); |
| 2112 | } |
| 2113 | curwin->w_cursor = search_start; |
| 2114 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2115 | curwin->w_curswant = old_curswant; |
| 2116 | curwin->w_leftcol = old_leftcol; |
| 2117 | curwin->w_topline = old_topline; |
| 2118 | # ifdef FEAT_DIFF |
| 2119 | curwin->w_topfill = old_topfill; |
| 2120 | # endif |
| 2121 | curwin->w_botline = old_botline; |
Bram Moolenaar | 9d34d90 | 2018-04-27 22:18:12 +0200 | [diff] [blame^] | 2122 | curwin->w_empty_rows = old_empty_rows; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2123 | highlight_match = FALSE; |
| 2124 | validate_cursor(); /* needed for TAB */ |
Bram Moolenaar | f8f8b2e | 2017-11-02 19:08:48 +0100 | [diff] [blame] | 2125 | redraw_all_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2126 | } |
| 2127 | #endif |
| 2128 | |
| 2129 | if (ccline.cmdbuff != NULL) |
| 2130 | { |
| 2131 | /* |
| 2132 | * Put line in history buffer (":" and "=" only when it was typed). |
| 2133 | */ |
| 2134 | #ifdef FEAT_CMDHIST |
| 2135 | if (ccline.cmdlen && firstc != NUL |
| 2136 | && (some_key_typed || histype == HIST_SEARCH)) |
| 2137 | { |
| 2138 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 2139 | histype == HIST_SEARCH ? firstc : NUL); |
| 2140 | if (firstc == ':') |
| 2141 | { |
| 2142 | vim_free(new_last_cmdline); |
| 2143 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 2144 | } |
| 2145 | } |
| 2146 | #endif |
| 2147 | |
Bram Moolenaar | f8e8c06 | 2017-10-22 14:44:17 +0200 | [diff] [blame] | 2148 | if (gotesc) |
| 2149 | abandon_cmdline(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2150 | } |
| 2151 | |
| 2152 | /* |
| 2153 | * If the screen was shifted up, redraw the whole screen (later). |
| 2154 | * If the line is too long, clear it, so ruler and shown command do |
| 2155 | * not get printed in the middle of it. |
| 2156 | */ |
| 2157 | msg_check(); |
| 2158 | msg_scroll = save_msg_scroll; |
| 2159 | redir_off = FALSE; |
| 2160 | |
| 2161 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 2162 | if (some_key_typed) |
| 2163 | need_wait_return = FALSE; |
| 2164 | |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2165 | /* Trigger CmdlineLeave autocommands. */ |
| 2166 | trigger_cmd_autocmd(cmdline_type, EVENT_CMDLINELEAVE); |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 2167 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2168 | State = save_State; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 2169 | #ifdef HAVE_INPUT_METHOD |
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 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2270 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2271 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2272 | */ |
| 2273 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2274 | curbuf_locked(void) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2275 | { |
| 2276 | if (curbuf_lock > 0) |
| 2277 | { |
| 2278 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2279 | return TRUE; |
| 2280 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2281 | return allbuf_locked(); |
| 2282 | } |
| 2283 | |
| 2284 | /* |
| 2285 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2286 | * message. |
| 2287 | */ |
| 2288 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2289 | allbuf_locked(void) |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2290 | { |
| 2291 | if (allbuf_lock > 0) |
| 2292 | { |
| 2293 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2294 | return TRUE; |
| 2295 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2296 | return FALSE; |
| 2297 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2298 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2299 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2300 | cmdline_charsize(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2301 | { |
| 2302 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2303 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2304 | return 1; |
| 2305 | #endif |
| 2306 | return ptr2cells(ccline.cmdbuff + idx); |
| 2307 | } |
| 2308 | |
| 2309 | /* |
| 2310 | * Compute the offset of the cursor on the command line for the prompt and |
| 2311 | * indent. |
| 2312 | */ |
| 2313 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2314 | set_cmdspos(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2315 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2316 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2317 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2318 | else |
| 2319 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2320 | } |
| 2321 | |
| 2322 | /* |
| 2323 | * Compute the screen position for the cursor on the command line. |
| 2324 | */ |
| 2325 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2326 | set_cmdspos_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2327 | { |
| 2328 | int i, m, c; |
| 2329 | |
| 2330 | set_cmdspos(); |
| 2331 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2332 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2333 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2334 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2335 | m = MAXCOL; |
| 2336 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2337 | else |
| 2338 | m = MAXCOL; |
| 2339 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2340 | { |
| 2341 | c = cmdline_charsize(i); |
| 2342 | #ifdef FEAT_MBYTE |
| 2343 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2344 | if (has_mbyte) |
| 2345 | correct_cmdspos(i, c); |
| 2346 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2347 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2348 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2349 | if ((ccline.cmdspos += c) >= m) |
| 2350 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2351 | ccline.cmdspos -= c; |
| 2352 | break; |
| 2353 | } |
| 2354 | #ifdef FEAT_MBYTE |
| 2355 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2356 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2357 | #endif |
| 2358 | } |
| 2359 | } |
| 2360 | |
| 2361 | #ifdef FEAT_MBYTE |
| 2362 | /* |
| 2363 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2364 | * character that doesn't fit, so that a ">" must be displayed. |
| 2365 | */ |
| 2366 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2367 | correct_cmdspos(int idx, int cells) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2368 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2369 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2370 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2371 | && ccline.cmdspos % Columns + cells > Columns) |
| 2372 | ccline.cmdspos++; |
| 2373 | } |
| 2374 | #endif |
| 2375 | |
| 2376 | /* |
| 2377 | * Get an Ex command line for the ":" command. |
| 2378 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2379 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2380 | getexline( |
| 2381 | int c, /* normally ':', NUL for ":append" */ |
| 2382 | void *cookie UNUSED, |
| 2383 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2384 | { |
| 2385 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2386 | if (exec_from_reg && vpeekc() == ':') |
| 2387 | (void)vgetc(); |
| 2388 | return getcmdline(c, 1L, indent); |
| 2389 | } |
| 2390 | |
| 2391 | /* |
| 2392 | * Get an Ex command line for Ex mode. |
| 2393 | * In Ex mode we only use the OS supplied line editing features and no |
| 2394 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2395 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2396 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2397 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2398 | getexmodeline( |
| 2399 | int promptc, /* normally ':', NUL for ":append" and '?' for |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2400 | :s prompt */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2401 | void *cookie UNUSED, |
| 2402 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2403 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2404 | garray_T line_ga; |
| 2405 | char_u *pend; |
| 2406 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2407 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2408 | int escaped = FALSE; /* CTRL-V typed */ |
| 2409 | int vcol = 0; |
| 2410 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2411 | int prev_char; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2412 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2413 | |
| 2414 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2415 | * confuses the system function that computes tabstops. */ |
| 2416 | cursor_on(); |
| 2417 | |
| 2418 | /* always start in column 0; write a newline if necessary */ |
| 2419 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2420 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2421 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2422 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2423 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2424 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2425 | if (p_prompt) |
| 2426 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2427 | while (indent-- > 0) |
| 2428 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2429 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2430 | } |
| 2431 | |
| 2432 | ga_init2(&line_ga, 1, 30); |
| 2433 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2434 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2435 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2436 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2437 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2438 | while (indent >= 8) |
| 2439 | { |
| 2440 | ga_append(&line_ga, TAB); |
| 2441 | msg_puts((char_u *)" "); |
| 2442 | indent -= 8; |
| 2443 | } |
| 2444 | while (indent-- > 0) |
| 2445 | { |
| 2446 | ga_append(&line_ga, ' '); |
| 2447 | msg_putchar(' '); |
| 2448 | } |
| 2449 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2450 | ++no_mapping; |
| 2451 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2452 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2453 | /* |
| 2454 | * Get the line, one character at a time. |
| 2455 | */ |
| 2456 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2457 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2458 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2459 | long sw; |
| 2460 | char_u *s; |
| 2461 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2462 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2463 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2464 | |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2465 | /* |
| 2466 | * Get one character at a time. |
| 2467 | */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2468 | prev_char = c1; |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2469 | |
| 2470 | /* Check for a ":normal" command and no more characters left. */ |
| 2471 | if (ex_normal_busy > 0 && typebuf.tb_len == 0) |
| 2472 | c1 = '\n'; |
| 2473 | else |
| 2474 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2475 | |
| 2476 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2477 | * Handle line editing. |
| 2478 | * Previously this was left to the system, putting the terminal in |
| 2479 | * 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] | 2480 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2481 | if (got_int) |
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 | msg_putchar('\n'); |
| 2484 | break; |
| 2485 | } |
| 2486 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2487 | if (c1 == K_PS) |
| 2488 | { |
| 2489 | bracketed_paste(PASTE_EX, FALSE, &line_ga); |
| 2490 | goto redraw; |
| 2491 | } |
| 2492 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2493 | if (!escaped) |
| 2494 | { |
| 2495 | /* CR typed means "enter", which is NL */ |
| 2496 | if (c1 == '\r') |
| 2497 | c1 = '\n'; |
| 2498 | |
| 2499 | if (c1 == BS || c1 == K_BS |
| 2500 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2501 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2502 | if (line_ga.ga_len > 0) |
| 2503 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2504 | #ifdef FEAT_MBYTE |
| 2505 | if (has_mbyte) |
| 2506 | { |
| 2507 | p = (char_u *)line_ga.ga_data; |
| 2508 | p[line_ga.ga_len] = NUL; |
| 2509 | len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1; |
| 2510 | line_ga.ga_len -= len; |
| 2511 | } |
| 2512 | else |
| 2513 | #endif |
| 2514 | --line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2515 | goto redraw; |
| 2516 | } |
| 2517 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2518 | } |
| 2519 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2520 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2521 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2522 | msg_col = startcol; |
| 2523 | msg_clr_eos(); |
| 2524 | line_ga.ga_len = 0; |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2525 | goto redraw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | if (c1 == Ctrl_T) |
| 2529 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2530 | sw = get_sw_value(curbuf); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2531 | p = (char_u *)line_ga.ga_data; |
| 2532 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2533 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 2534 | indent += sw - indent % sw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2535 | add_indent: |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2536 | while (get_indent_str(p, 8, FALSE) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2537 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2538 | (void)ga_grow(&line_ga, 2); /* one more for the NUL */ |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2539 | p = (char_u *)line_ga.ga_data; |
| 2540 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2541 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2542 | *s = ' '; |
| 2543 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2544 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2545 | redraw: |
| 2546 | /* redraw the line */ |
| 2547 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2548 | vcol = 0; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2549 | p = (char_u *)line_ga.ga_data; |
| 2550 | p[line_ga.ga_len] = NUL; |
| 2551 | while (p < (char_u *)line_ga.ga_data + line_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2552 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2553 | if (*p == TAB) |
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 | do |
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 | msg_putchar(' '); |
| 2558 | } while (++vcol % 8); |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2559 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2560 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2561 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2562 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2563 | len = MB_PTR2LEN(p); |
| 2564 | msg_outtrans_len(p, len); |
| 2565 | vcol += ptr2cells(p); |
| 2566 | p += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2567 | } |
| 2568 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2569 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2570 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2571 | continue; |
| 2572 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2573 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2574 | if (c1 == Ctrl_D) |
| 2575 | { |
| 2576 | /* Delete one shiftwidth. */ |
| 2577 | p = (char_u *)line_ga.ga_data; |
| 2578 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2579 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2580 | if (prev_char == '^') |
| 2581 | ex_keep_indent = TRUE; |
| 2582 | indent = 0; |
| 2583 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2584 | } |
| 2585 | else |
| 2586 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2587 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2588 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2589 | if (indent > 0) |
| 2590 | { |
| 2591 | --indent; |
| 2592 | indent -= indent % get_sw_value(curbuf); |
| 2593 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2594 | } |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2595 | while (get_indent_str(p, 8, FALSE) > indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2596 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2597 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2598 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2599 | --line_ga.ga_len; |
| 2600 | } |
| 2601 | goto add_indent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2602 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2603 | |
| 2604 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2605 | { |
| 2606 | escaped = TRUE; |
| 2607 | continue; |
| 2608 | } |
| 2609 | |
| 2610 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2611 | if (IS_SPECIAL(c1)) |
| 2612 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2613 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2614 | |
| 2615 | if (IS_SPECIAL(c1)) |
| 2616 | c1 = '?'; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2617 | #ifdef FEAT_MBYTE |
| 2618 | if (has_mbyte) |
| 2619 | len = (*mb_char2bytes)(c1, |
| 2620 | (char_u *)line_ga.ga_data + line_ga.ga_len); |
| 2621 | else |
| 2622 | #endif |
| 2623 | { |
| 2624 | len = 1; |
| 2625 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
| 2626 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2627 | if (c1 == '\n') |
| 2628 | msg_putchar('\n'); |
| 2629 | else if (c1 == TAB) |
| 2630 | { |
| 2631 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2632 | do |
| 2633 | { |
| 2634 | msg_putchar(' '); |
| 2635 | } while (++vcol % 8); |
| 2636 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2637 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2638 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2639 | msg_outtrans_len( |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2640 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, len); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2641 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2642 | } |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2643 | line_ga.ga_len += len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2644 | escaped = FALSE; |
| 2645 | |
| 2646 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2647 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2648 | |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2649 | /* We are done when a NL is entered, but not when it comes after an |
| 2650 | * odd number of backslashes, that results in a NUL. */ |
| 2651 | if (line_ga.ga_len > 0 && pend[-1] == '\n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2652 | { |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2653 | int bcount = 0; |
| 2654 | |
| 2655 | while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') |
| 2656 | ++bcount; |
| 2657 | |
| 2658 | if (bcount > 0) |
| 2659 | { |
| 2660 | /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> |
| 2661 | * "\NL", etc. */ |
| 2662 | line_ga.ga_len -= (bcount + 1) / 2; |
| 2663 | pend -= (bcount + 1) / 2; |
| 2664 | pend[-1] = '\n'; |
| 2665 | } |
| 2666 | |
| 2667 | if ((bcount & 1) == 0) |
| 2668 | { |
| 2669 | --line_ga.ga_len; |
| 2670 | --pend; |
| 2671 | *pend = NUL; |
| 2672 | break; |
| 2673 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2674 | } |
| 2675 | } |
| 2676 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2677 | --no_mapping; |
| 2678 | --allow_keys; |
| 2679 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2680 | /* make following messages go to the next line */ |
| 2681 | msg_didout = FALSE; |
| 2682 | msg_col = 0; |
| 2683 | if (msg_row < Rows - 1) |
| 2684 | ++msg_row; |
| 2685 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 2686 | |
| 2687 | if (got_int) |
| 2688 | ga_clear(&line_ga); |
| 2689 | |
| 2690 | return (char_u *)line_ga.ga_data; |
| 2691 | } |
| 2692 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2693 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 2694 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2695 | /* |
| 2696 | * Return TRUE if ccline.overstrike is on. |
| 2697 | */ |
| 2698 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2699 | cmdline_overstrike(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2700 | { |
| 2701 | return ccline.overstrike; |
| 2702 | } |
| 2703 | |
| 2704 | /* |
| 2705 | * Return TRUE if the cursor is at the end of the cmdline. |
| 2706 | */ |
| 2707 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2708 | cmdline_at_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2709 | { |
| 2710 | return (ccline.cmdpos >= ccline.cmdlen); |
| 2711 | } |
| 2712 | #endif |
| 2713 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2714 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2715 | /* |
| 2716 | * Return the virtual column number at the current cursor position. |
| 2717 | * This is used by the IM code to obtain the start of the preedit string. |
| 2718 | */ |
| 2719 | colnr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2720 | cmdline_getvcol_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2721 | { |
| 2722 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 2723 | return MAXCOL; |
| 2724 | |
| 2725 | # ifdef FEAT_MBYTE |
| 2726 | if (has_mbyte) |
| 2727 | { |
| 2728 | colnr_T col; |
| 2729 | int i = 0; |
| 2730 | |
| 2731 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2732 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2733 | |
| 2734 | return col; |
| 2735 | } |
| 2736 | else |
| 2737 | # endif |
| 2738 | return ccline.cmdpos; |
| 2739 | } |
| 2740 | #endif |
| 2741 | |
| 2742 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2743 | /* |
| 2744 | * If part of the command line is an IM preedit string, redraw it with |
| 2745 | * IM feedback attributes. The cursor position is restored after drawing. |
| 2746 | */ |
| 2747 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2748 | redrawcmd_preedit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2749 | { |
| 2750 | if ((State & CMDLINE) |
| 2751 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 2752 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2753 | && !p_imdisable |
| 2754 | && im_is_preediting()) |
| 2755 | { |
| 2756 | int cmdpos = 0; |
| 2757 | int cmdspos; |
| 2758 | int old_row; |
| 2759 | int old_col; |
| 2760 | colnr_T col; |
| 2761 | |
| 2762 | old_row = msg_row; |
| 2763 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2764 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2765 | |
| 2766 | # ifdef FEAT_MBYTE |
| 2767 | if (has_mbyte) |
| 2768 | { |
| 2769 | for (col = 0; col < preedit_start_col |
| 2770 | && cmdpos < ccline.cmdlen; ++col) |
| 2771 | { |
| 2772 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2773 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2774 | } |
| 2775 | } |
| 2776 | else |
| 2777 | # endif |
| 2778 | { |
| 2779 | cmdspos += preedit_start_col; |
| 2780 | cmdpos += preedit_start_col; |
| 2781 | } |
| 2782 | |
| 2783 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 2784 | msg_col = cmdspos % (int)Columns; |
| 2785 | if (msg_row >= Rows) |
| 2786 | msg_row = Rows - 1; |
| 2787 | |
| 2788 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 2789 | { |
| 2790 | int char_len; |
| 2791 | int char_attr; |
| 2792 | |
| 2793 | char_attr = im_get_feedback_attr(col); |
| 2794 | if (char_attr < 0) |
| 2795 | break; /* end of preedit string */ |
| 2796 | |
| 2797 | # ifdef FEAT_MBYTE |
| 2798 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2799 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2800 | else |
| 2801 | # endif |
| 2802 | char_len = 1; |
| 2803 | |
| 2804 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 2805 | cmdpos += char_len; |
| 2806 | } |
| 2807 | |
| 2808 | msg_row = old_row; |
| 2809 | msg_col = old_col; |
| 2810 | } |
| 2811 | } |
| 2812 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 2813 | |
| 2814 | /* |
| 2815 | * Allocate a new command line buffer. |
| 2816 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
| 2817 | * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. |
| 2818 | */ |
| 2819 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2820 | alloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2821 | { |
| 2822 | /* |
| 2823 | * give some extra space to avoid having to allocate all the time |
| 2824 | */ |
| 2825 | if (len < 80) |
| 2826 | len = 100; |
| 2827 | else |
| 2828 | len += 20; |
| 2829 | |
| 2830 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 2831 | ccline.cmdbufflen = len; |
| 2832 | } |
| 2833 | |
| 2834 | /* |
| 2835 | * Re-allocate the command line to length len + something extra. |
| 2836 | * return FAIL for failure, OK otherwise |
| 2837 | */ |
| 2838 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2839 | realloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2840 | { |
| 2841 | char_u *p; |
| 2842 | |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 2843 | if (len < ccline.cmdbufflen) |
| 2844 | return OK; /* no need to resize */ |
| 2845 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2846 | p = ccline.cmdbuff; |
| 2847 | alloc_cmdbuff(len); /* will get some more */ |
| 2848 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 2849 | { |
| 2850 | ccline.cmdbuff = p; /* keep the old one */ |
| 2851 | return FAIL; |
| 2852 | } |
Bram Moolenaar | 35a3423 | 2010-08-13 16:51:26 +0200 | [diff] [blame] | 2853 | /* There isn't always a NUL after the command, but it may need to be |
| 2854 | * there, thus copy up to the NUL and add a NUL. */ |
| 2855 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
| 2856 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2857 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2858 | |
| 2859 | if (ccline.xpc != NULL |
| 2860 | && ccline.xpc->xp_pattern != NULL |
| 2861 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 2862 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 2863 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 2864 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2865 | |
| 2866 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 2867 | * to point into the newly allocated memory. */ |
| 2868 | if (i >= 0 && i <= ccline.cmdlen) |
| 2869 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 2870 | } |
| 2871 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2872 | return OK; |
| 2873 | } |
| 2874 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2875 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 2876 | static char_u *arshape_buf = NULL; |
| 2877 | |
| 2878 | # if defined(EXITFREE) || defined(PROTO) |
| 2879 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2880 | free_cmdline_buf(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2881 | { |
| 2882 | vim_free(arshape_buf); |
| 2883 | } |
| 2884 | # endif |
| 2885 | #endif |
| 2886 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2887 | /* |
| 2888 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 2889 | * when cmdline_star is TRUE. |
| 2890 | */ |
| 2891 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2892 | draw_cmdline(int start, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2893 | { |
| 2894 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2895 | int i; |
| 2896 | |
| 2897 | if (cmdline_star > 0) |
| 2898 | for (i = 0; i < len; ++i) |
| 2899 | { |
| 2900 | msg_putchar('*'); |
| 2901 | # ifdef FEAT_MBYTE |
| 2902 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2903 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2904 | # endif |
| 2905 | } |
| 2906 | else |
| 2907 | #endif |
| 2908 | #ifdef FEAT_ARABIC |
| 2909 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 2910 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2911 | static int buflen = 0; |
| 2912 | char_u *p; |
| 2913 | int j; |
| 2914 | int newlen = 0; |
| 2915 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 2916 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2917 | int prev_c = 0; |
| 2918 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2919 | int u8c; |
| 2920 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2921 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2922 | |
| 2923 | /* |
| 2924 | * Do arabic shaping into a temporary buffer. This is very |
| 2925 | * inefficient! |
| 2926 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2927 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2928 | { |
| 2929 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 2930 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2931 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2932 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2933 | arshape_buf = alloc(buflen); |
| 2934 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2935 | return; /* out of memory */ |
| 2936 | } |
| 2937 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2938 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 2939 | { |
| 2940 | /* Prepend a space to draw the leading composing char on. */ |
| 2941 | arshape_buf[0] = ' '; |
| 2942 | newlen = 1; |
| 2943 | } |
| 2944 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2945 | for (j = start; j < start + len; j += mb_l) |
| 2946 | { |
| 2947 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2948 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2949 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2950 | if (ARABIC_CHAR(u8c)) |
| 2951 | { |
| 2952 | /* Do Arabic shaping. */ |
| 2953 | if (cmdmsg_rl) |
| 2954 | { |
| 2955 | /* displaying from right to left */ |
| 2956 | pc = prev_c; |
| 2957 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2958 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2959 | if (j + mb_l >= start + len) |
| 2960 | nc = NUL; |
| 2961 | else |
| 2962 | nc = utf_ptr2char(p + mb_l); |
| 2963 | } |
| 2964 | else |
| 2965 | { |
| 2966 | /* displaying from left to right */ |
| 2967 | if (j + mb_l >= start + len) |
| 2968 | pc = NUL; |
| 2969 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2970 | { |
| 2971 | int pcc[MAX_MCO]; |
| 2972 | |
| 2973 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2974 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2975 | pc1 = pcc[0]; |
| 2976 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2977 | nc = prev_c; |
| 2978 | } |
| 2979 | prev_c = u8c; |
| 2980 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2981 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2982 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2983 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2984 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2985 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2986 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 2987 | if (u8cc[1] != 0) |
| 2988 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2989 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2990 | } |
| 2991 | } |
| 2992 | else |
| 2993 | { |
| 2994 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2995 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2996 | newlen += mb_l; |
| 2997 | } |
| 2998 | } |
| 2999 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 3000 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3001 | } |
| 3002 | else |
| 3003 | #endif |
| 3004 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 3005 | } |
| 3006 | |
| 3007 | /* |
| 3008 | * Put a character on the command line. Shifts the following text to the |
| 3009 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 3010 | * "c" must be printable (fit in one display cell)! |
| 3011 | */ |
| 3012 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3013 | putcmdline(int c, int shift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3014 | { |
| 3015 | if (cmd_silent) |
| 3016 | return; |
| 3017 | msg_no_more = TRUE; |
| 3018 | msg_putchar(c); |
| 3019 | if (shift) |
| 3020 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3021 | msg_no_more = FALSE; |
| 3022 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3023 | extra_char = c; |
| 3024 | extra_char_shift = shift; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3025 | } |
| 3026 | |
| 3027 | /* |
| 3028 | * Undo a putcmdline(c, FALSE). |
| 3029 | */ |
| 3030 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3031 | unputcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3032 | { |
| 3033 | if (cmd_silent) |
| 3034 | return; |
| 3035 | msg_no_more = TRUE; |
| 3036 | if (ccline.cmdlen == ccline.cmdpos) |
| 3037 | msg_putchar(' '); |
Bram Moolenaar | 64fdf5c | 2012-06-06 12:03:06 +0200 | [diff] [blame] | 3038 | #ifdef FEAT_MBYTE |
| 3039 | else if (has_mbyte) |
| 3040 | draw_cmdline(ccline.cmdpos, |
| 3041 | (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); |
| 3042 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3043 | else |
| 3044 | draw_cmdline(ccline.cmdpos, 1); |
| 3045 | msg_no_more = FALSE; |
| 3046 | cursorcmd(); |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3047 | extra_char = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3048 | } |
| 3049 | |
| 3050 | /* |
| 3051 | * Put the given string, of the given length, onto the command line. |
| 3052 | * If len is -1, then STRLEN() is used to calculate the length. |
| 3053 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 3054 | * part will be redrawn, otherwise it will not. If this function is called |
| 3055 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 3056 | * called afterwards. |
| 3057 | */ |
| 3058 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3059 | put_on_cmdline(char_u *str, int len, int redraw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3060 | { |
| 3061 | int retval; |
| 3062 | int i; |
| 3063 | int m; |
| 3064 | int c; |
| 3065 | |
| 3066 | if (len < 0) |
| 3067 | len = (int)STRLEN(str); |
| 3068 | |
| 3069 | /* Check if ccline.cmdbuff needs to be longer */ |
| 3070 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3071 | retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3072 | else |
| 3073 | retval = OK; |
| 3074 | if (retval == OK) |
| 3075 | { |
| 3076 | if (!ccline.overstrike) |
| 3077 | { |
| 3078 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3079 | ccline.cmdbuff + ccline.cmdpos, |
| 3080 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 3081 | ccline.cmdlen += len; |
| 3082 | } |
| 3083 | else |
| 3084 | { |
| 3085 | #ifdef FEAT_MBYTE |
| 3086 | if (has_mbyte) |
| 3087 | { |
| 3088 | /* Count nr of characters in the new string. */ |
| 3089 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3090 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3091 | ++m; |
| 3092 | /* Count nr of bytes in cmdline that are overwritten by these |
| 3093 | * characters. */ |
| 3094 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3095 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3096 | --m; |
| 3097 | if (i < ccline.cmdlen) |
| 3098 | { |
| 3099 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3100 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 3101 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 3102 | } |
| 3103 | else |
| 3104 | ccline.cmdlen = ccline.cmdpos + len; |
| 3105 | } |
| 3106 | else |
| 3107 | #endif |
| 3108 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 3109 | ccline.cmdlen = ccline.cmdpos + len; |
| 3110 | } |
| 3111 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 3112 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 3113 | |
| 3114 | #ifdef FEAT_MBYTE |
| 3115 | if (enc_utf8) |
| 3116 | { |
| 3117 | /* When the inserted text starts with a composing character, |
| 3118 | * backup to the character before it. There could be two of them. |
| 3119 | */ |
| 3120 | i = 0; |
| 3121 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3122 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 3123 | { |
| 3124 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3125 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3126 | ccline.cmdpos -= i; |
| 3127 | len += i; |
| 3128 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3129 | } |
| 3130 | # ifdef FEAT_ARABIC |
| 3131 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 3132 | { |
| 3133 | /* Check the previous character for Arabic combining pair. */ |
| 3134 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3135 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3136 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 3137 | + ccline.cmdpos - i), c)) |
| 3138 | { |
| 3139 | ccline.cmdpos -= i; |
| 3140 | len += i; |
| 3141 | } |
| 3142 | else |
| 3143 | i = 0; |
| 3144 | } |
| 3145 | # endif |
| 3146 | if (i != 0) |
| 3147 | { |
| 3148 | /* Also backup the cursor position. */ |
| 3149 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 3150 | ccline.cmdspos -= i; |
| 3151 | msg_col -= i; |
| 3152 | if (msg_col < 0) |
| 3153 | { |
| 3154 | msg_col += Columns; |
| 3155 | --msg_row; |
| 3156 | } |
| 3157 | } |
| 3158 | } |
| 3159 | #endif |
| 3160 | |
| 3161 | if (redraw && !cmd_silent) |
| 3162 | { |
| 3163 | msg_no_more = TRUE; |
| 3164 | i = cmdline_row; |
Bram Moolenaar | 73dc59a | 2011-09-30 17:46:21 +0200 | [diff] [blame] | 3165 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3166 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3167 | /* Avoid clearing the rest of the line too often. */ |
| 3168 | if (cmdline_row != i || ccline.overstrike) |
| 3169 | msg_clr_eos(); |
| 3170 | msg_no_more = FALSE; |
| 3171 | } |
| 3172 | #ifdef FEAT_FKMAP |
| 3173 | /* |
| 3174 | * If we are in Farsi command mode, the character input must be in |
| 3175 | * Insert mode. So do not advance the cmdpos. |
| 3176 | */ |
| 3177 | if (!cmd_fkmap) |
| 3178 | #endif |
| 3179 | { |
| 3180 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3181 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3182 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3183 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 3184 | m = MAXCOL; |
| 3185 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3186 | else |
| 3187 | m = MAXCOL; |
| 3188 | for (i = 0; i < len; ++i) |
| 3189 | { |
| 3190 | c = cmdline_charsize(ccline.cmdpos); |
| 3191 | #ifdef FEAT_MBYTE |
| 3192 | /* count ">" for a double-wide char that doesn't fit. */ |
| 3193 | if (has_mbyte) |
| 3194 | correct_cmdspos(ccline.cmdpos, c); |
| 3195 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3196 | /* Stop cursor at the end of the screen, but do increment the |
| 3197 | * insert position, so that entering a very long command |
| 3198 | * works, even though you can't see it. */ |
| 3199 | if (ccline.cmdspos + c < m) |
| 3200 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3201 | #ifdef FEAT_MBYTE |
| 3202 | if (has_mbyte) |
| 3203 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3204 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3205 | if (c > len - i - 1) |
| 3206 | c = len - i - 1; |
| 3207 | ccline.cmdpos += c; |
| 3208 | i += c; |
| 3209 | } |
| 3210 | #endif |
| 3211 | ++ccline.cmdpos; |
| 3212 | } |
| 3213 | } |
| 3214 | } |
| 3215 | if (redraw) |
| 3216 | msg_check(); |
| 3217 | return retval; |
| 3218 | } |
| 3219 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3220 | static struct cmdline_info prev_ccline; |
| 3221 | static int prev_ccline_used = FALSE; |
| 3222 | |
| 3223 | /* |
| 3224 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 3225 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 3226 | * available globally in prev_ccline. |
| 3227 | */ |
| 3228 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3229 | save_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3230 | { |
| 3231 | if (!prev_ccline_used) |
| 3232 | { |
| 3233 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 3234 | prev_ccline_used = TRUE; |
| 3235 | } |
| 3236 | *ccp = prev_ccline; |
| 3237 | prev_ccline = ccline; |
| 3238 | ccline.cmdbuff = NULL; |
| 3239 | ccline.cmdprompt = NULL; |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3240 | ccline.xpc = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
| 3243 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3244 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3245 | */ |
| 3246 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3247 | restore_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3248 | { |
| 3249 | ccline = prev_ccline; |
| 3250 | prev_ccline = *ccp; |
| 3251 | } |
| 3252 | |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3253 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3254 | /* |
| 3255 | * Save the command line into allocated memory. Returns a pointer to be |
| 3256 | * passed to restore_cmdline_alloc() later. |
| 3257 | * Returns NULL when failed. |
| 3258 | */ |
| 3259 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3260 | save_cmdline_alloc(void) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3261 | { |
| 3262 | struct cmdline_info *p; |
| 3263 | |
| 3264 | p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); |
| 3265 | if (p != NULL) |
| 3266 | save_cmdline(p); |
| 3267 | return (char_u *)p; |
| 3268 | } |
| 3269 | |
| 3270 | /* |
| 3271 | * Restore the command line from the return value of save_cmdline_alloc(). |
| 3272 | */ |
| 3273 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3274 | restore_cmdline_alloc(char_u *p) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3275 | { |
| 3276 | if (p != NULL) |
| 3277 | { |
| 3278 | restore_cmdline((struct cmdline_info *)p); |
| 3279 | vim_free(p); |
| 3280 | } |
| 3281 | } |
| 3282 | #endif |
| 3283 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3284 | /* |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3285 | * Paste a yank register into the command line. |
| 3286 | * Used by CTRL-R command in command-line mode. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3287 | * insert_reg() can't be used here, because special characters from the |
| 3288 | * register contents will be interpreted as commands. |
| 3289 | * |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3290 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3291 | */ |
| 3292 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3293 | cmdline_paste( |
| 3294 | int regname, |
| 3295 | int literally, /* Insert text literally instead of "as typed" */ |
| 3296 | int remcr) /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3297 | { |
| 3298 | long i; |
| 3299 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3300 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3301 | int allocated; |
| 3302 | struct cmdline_info save_ccline; |
| 3303 | |
| 3304 | /* check for valid regname; also accept special characters for CTRL-R in |
| 3305 | * the command line */ |
| 3306 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
| 3307 | && regname != Ctrl_A && !valid_yank_reg(regname, FALSE)) |
| 3308 | return FAIL; |
| 3309 | |
| 3310 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 3311 | * CTRL-C to break the loop. */ |
| 3312 | line_breakcheck(); |
| 3313 | if (got_int) |
| 3314 | return FAIL; |
| 3315 | |
| 3316 | #ifdef FEAT_CLIPBOARD |
| 3317 | regname = may_get_selection(regname); |
| 3318 | #endif |
| 3319 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3320 | /* Need to save and restore ccline. And set "textlock" to avoid nasty |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3321 | * things like going to another buffer when evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3322 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3323 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3324 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3325 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3326 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3327 | |
| 3328 | if (i) |
| 3329 | { |
| 3330 | /* Got the value of a special register in "arg". */ |
| 3331 | if (arg == NULL) |
| 3332 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3333 | |
| 3334 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3335 | * part of the word. */ |
| 3336 | p = arg; |
| 3337 | if (p_is && regname == Ctrl_W) |
| 3338 | { |
| 3339 | char_u *w; |
| 3340 | int len; |
| 3341 | |
| 3342 | /* Locate start of last word in the cmd buffer. */ |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3343 | for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3344 | { |
| 3345 | #ifdef FEAT_MBYTE |
| 3346 | if (has_mbyte) |
| 3347 | { |
| 3348 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3349 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3350 | break; |
| 3351 | w -= len; |
| 3352 | } |
| 3353 | else |
| 3354 | #endif |
| 3355 | { |
| 3356 | if (!vim_iswordc(w[-1])) |
| 3357 | break; |
| 3358 | --w; |
| 3359 | } |
| 3360 | } |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3361 | len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3362 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3363 | p += len; |
| 3364 | } |
| 3365 | |
| 3366 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3367 | if (allocated) |
| 3368 | vim_free(arg); |
| 3369 | return OK; |
| 3370 | } |
| 3371 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3372 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3373 | } |
| 3374 | |
| 3375 | /* |
| 3376 | * Put a string on the command line. |
| 3377 | * When "literally" is TRUE, insert literally. |
| 3378 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3379 | * line. |
| 3380 | */ |
| 3381 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3382 | cmdline_paste_str(char_u *s, int literally) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3383 | { |
| 3384 | int c, cv; |
| 3385 | |
| 3386 | if (literally) |
| 3387 | put_on_cmdline(s, -1, TRUE); |
| 3388 | else |
| 3389 | while (*s != NUL) |
| 3390 | { |
| 3391 | cv = *s; |
| 3392 | if (cv == Ctrl_V && s[1]) |
| 3393 | ++s; |
| 3394 | #ifdef FEAT_MBYTE |
| 3395 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3396 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3397 | else |
| 3398 | #endif |
| 3399 | c = *s++; |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 3400 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
| 3401 | || c == CAR || c == NL || c == Ctrl_L |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3402 | #ifdef UNIX |
| 3403 | || c == intr_char |
| 3404 | #endif |
| 3405 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3406 | stuffcharReadbuff(Ctrl_V); |
| 3407 | stuffcharReadbuff(c); |
| 3408 | } |
| 3409 | } |
| 3410 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3411 | #ifdef FEAT_WILDMENU |
| 3412 | /* |
| 3413 | * Delete characters on the command line, from "from" to the current |
| 3414 | * position. |
| 3415 | */ |
| 3416 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3417 | cmdline_del(int from) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3418 | { |
| 3419 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3420 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3421 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3422 | ccline.cmdpos = from; |
| 3423 | } |
| 3424 | #endif |
| 3425 | |
| 3426 | /* |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 3427 | * This function is called when the screen size changes and with incremental |
| 3428 | * search and in other situations where the command line may have been |
| 3429 | * overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3430 | */ |
| 3431 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3432 | redrawcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3433 | { |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3434 | redrawcmdline_ex(TRUE); |
| 3435 | } |
| 3436 | |
| 3437 | void |
| 3438 | redrawcmdline_ex(int do_compute_cmdrow) |
| 3439 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3440 | if (cmd_silent) |
| 3441 | return; |
| 3442 | need_wait_return = FALSE; |
Bram Moolenaar | 29ae377 | 2017-04-30 19:39:39 +0200 | [diff] [blame] | 3443 | if (do_compute_cmdrow) |
| 3444 | compute_cmdrow(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3445 | redrawcmd(); |
| 3446 | cursorcmd(); |
| 3447 | } |
| 3448 | |
| 3449 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3450 | redrawcmdprompt(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3451 | { |
| 3452 | int i; |
| 3453 | |
| 3454 | if (cmd_silent) |
| 3455 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3456 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3457 | msg_putchar(ccline.cmdfirstc); |
| 3458 | if (ccline.cmdprompt != NULL) |
| 3459 | { |
| 3460 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3461 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3462 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3463 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3464 | --ccline.cmdindent; |
| 3465 | } |
| 3466 | else |
| 3467 | for (i = ccline.cmdindent; i > 0; --i) |
| 3468 | msg_putchar(' '); |
| 3469 | } |
| 3470 | |
| 3471 | /* |
| 3472 | * Redraw what is currently on the command line. |
| 3473 | */ |
| 3474 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3475 | redrawcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3476 | { |
| 3477 | if (cmd_silent) |
| 3478 | return; |
| 3479 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3480 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3481 | if (ccline.cmdbuff == NULL) |
| 3482 | { |
| 3483 | windgoto(cmdline_row, 0); |
| 3484 | msg_clr_eos(); |
| 3485 | return; |
| 3486 | } |
| 3487 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3488 | msg_start(); |
| 3489 | redrawcmdprompt(); |
| 3490 | |
| 3491 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3492 | msg_no_more = TRUE; |
| 3493 | draw_cmdline(0, ccline.cmdlen); |
| 3494 | msg_clr_eos(); |
| 3495 | msg_no_more = FALSE; |
| 3496 | |
| 3497 | set_cmdspos_cursor(); |
Bram Moolenaar | a92522f | 2017-07-15 15:21:38 +0200 | [diff] [blame] | 3498 | if (extra_char != NUL) |
Bram Moolenaar | 6a77d26 | 2017-07-16 15:24:01 +0200 | [diff] [blame] | 3499 | putcmdline(extra_char, extra_char_shift); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3500 | |
| 3501 | /* |
| 3502 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3503 | * in cmdline mode we can reset them now. |
| 3504 | */ |
| 3505 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3506 | |
| 3507 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3508 | * in cmdline mode */ |
| 3509 | skip_redraw = FALSE; |
| 3510 | } |
| 3511 | |
| 3512 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3513 | compute_cmdrow(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3514 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3515 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3516 | cmdline_row = Rows - 1; |
| 3517 | else |
| 3518 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
Bram Moolenaar | e0de17d | 2017-09-24 16:24:34 +0200 | [diff] [blame] | 3519 | + lastwin->w_status_height; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3520 | } |
| 3521 | |
| 3522 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3523 | cursorcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3524 | { |
| 3525 | if (cmd_silent) |
| 3526 | return; |
| 3527 | |
| 3528 | #ifdef FEAT_RIGHTLEFT |
| 3529 | if (cmdmsg_rl) |
| 3530 | { |
| 3531 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3532 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3533 | if (msg_row <= 0) |
| 3534 | msg_row = Rows - 1; |
| 3535 | } |
| 3536 | else |
| 3537 | #endif |
| 3538 | { |
| 3539 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3540 | msg_col = ccline.cmdspos % (int)Columns; |
| 3541 | if (msg_row >= Rows) |
| 3542 | msg_row = Rows - 1; |
| 3543 | } |
| 3544 | |
| 3545 | windgoto(msg_row, msg_col); |
| 3546 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | 5c6dbcb | 2017-08-30 22:00:20 +0200 | [diff] [blame] | 3547 | if (p_imst == IM_ON_THE_SPOT) |
| 3548 | redrawcmd_preedit(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3549 | #endif |
| 3550 | #ifdef MCH_CURSOR_SHAPE |
| 3551 | mch_update_cursor(); |
| 3552 | #endif |
| 3553 | } |
| 3554 | |
| 3555 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3556 | gotocmdline(int clr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3557 | { |
| 3558 | msg_start(); |
| 3559 | #ifdef FEAT_RIGHTLEFT |
| 3560 | if (cmdmsg_rl) |
| 3561 | msg_col = Columns - 1; |
| 3562 | else |
| 3563 | #endif |
| 3564 | msg_col = 0; /* always start in column 0 */ |
| 3565 | if (clr) /* clear the bottom line(s) */ |
| 3566 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3567 | windgoto(cmdline_row, 0); |
| 3568 | } |
| 3569 | |
| 3570 | /* |
| 3571 | * Check the word in front of the cursor for an abbreviation. |
| 3572 | * Called when the non-id character "c" has been entered. |
| 3573 | * When an abbreviation is recognized it is removed from the text with |
| 3574 | * backspaces and the replacement string is inserted, followed by "c". |
| 3575 | */ |
| 3576 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3577 | ccheck_abbr(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3578 | { |
| 3579 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3580 | return FALSE; |
| 3581 | |
| 3582 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0); |
| 3583 | } |
| 3584 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3585 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 3586 | static int |
| 3587 | #ifdef __BORLANDC__ |
| 3588 | _RTLENTRYF |
| 3589 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3590 | sort_func_compare(const void *s1, const void *s2) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3591 | { |
| 3592 | char_u *p1 = *(char_u **)s1; |
| 3593 | char_u *p2 = *(char_u **)s2; |
| 3594 | |
| 3595 | if (*p1 != '<' && *p2 == '<') return -1; |
| 3596 | if (*p1 == '<' && *p2 != '<') return 1; |
| 3597 | return STRCMP(p1, p2); |
| 3598 | } |
| 3599 | #endif |
| 3600 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3601 | /* |
| 3602 | * Return FAIL if this is not an appropriate context in which to do |
| 3603 | * completion of anything, return OK if it is (even if there are no matches). |
| 3604 | * For the caller, this means that the character is just passed through like a |
| 3605 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3606 | */ |
| 3607 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3608 | nextwild( |
| 3609 | expand_T *xp, |
| 3610 | int type, |
| 3611 | int options, /* extra options for ExpandOne() */ |
| 3612 | int escape) /* if TRUE, escape the returned matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3613 | { |
| 3614 | int i, j; |
| 3615 | char_u *p1; |
| 3616 | char_u *p2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3617 | int difflen; |
| 3618 | int v; |
| 3619 | |
| 3620 | if (xp->xp_numfiles == -1) |
| 3621 | { |
| 3622 | set_expand_context(xp); |
| 3623 | cmd_showtail = expand_showtail(xp); |
| 3624 | } |
| 3625 | |
| 3626 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3627 | { |
| 3628 | beep_flush(); |
| 3629 | return OK; /* Something illegal on command line */ |
| 3630 | } |
| 3631 | if (xp->xp_context == EXPAND_NOTHING) |
| 3632 | { |
| 3633 | /* Caller can use the character as a normal char instead */ |
| 3634 | return FAIL; |
| 3635 | } |
| 3636 | |
| 3637 | MSG_PUTS("..."); /* show that we are busy */ |
| 3638 | out_flush(); |
| 3639 | |
| 3640 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3641 | xp->xp_pattern_len = ccline.cmdpos - i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3642 | |
| 3643 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3644 | { |
| 3645 | /* |
| 3646 | * Get next/previous match for a previous expanded pattern. |
| 3647 | */ |
| 3648 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3649 | } |
| 3650 | else |
| 3651 | { |
| 3652 | /* |
| 3653 | * Translate string into pattern and expand it. |
| 3654 | */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3655 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 3656 | xp->xp_context)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3657 | p2 = NULL; |
| 3658 | else |
| 3659 | { |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3660 | int use_options = options | |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 3661 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 3662 | if (escape) |
| 3663 | use_options |= WILD_ESCAPE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3664 | |
| 3665 | if (p_wic) |
| 3666 | use_options += WILD_ICASE; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3667 | p2 = ExpandOne(xp, p1, |
| 3668 | vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3669 | use_options, type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3670 | vim_free(p1); |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3671 | /* longest match: make sure it is not shorter, happens with :help */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3672 | if (p2 != NULL && type == WILD_LONGEST) |
| 3673 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3674 | for (j = 0; j < xp->xp_pattern_len; ++j) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3675 | if (ccline.cmdbuff[i + j] == '*' |
| 3676 | || ccline.cmdbuff[i + j] == '?') |
| 3677 | break; |
| 3678 | if ((int)STRLEN(p2) < j) |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3679 | VIM_CLEAR(p2); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3680 | } |
| 3681 | } |
| 3682 | } |
| 3683 | |
| 3684 | if (p2 != NULL && !got_int) |
| 3685 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3686 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3687 | if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3688 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3689 | v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3690 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3691 | } |
| 3692 | else |
| 3693 | v = OK; |
| 3694 | if (v == OK) |
| 3695 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3696 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 3697 | &ccline.cmdbuff[ccline.cmdpos], |
| 3698 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3699 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3700 | ccline.cmdlen += difflen; |
| 3701 | ccline.cmdpos += difflen; |
| 3702 | } |
| 3703 | } |
| 3704 | vim_free(p2); |
| 3705 | |
| 3706 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3707 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3708 | |
| 3709 | /* When expanding a ":map" command and no matches are found, assume that |
| 3710 | * the key is supposed to be inserted literally */ |
| 3711 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 3712 | return FAIL; |
| 3713 | |
| 3714 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 3715 | beep_flush(); |
| 3716 | else if (xp->xp_numfiles == 1) |
| 3717 | /* free expanded pattern */ |
| 3718 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 3719 | |
| 3720 | return OK; |
| 3721 | } |
| 3722 | |
| 3723 | /* |
| 3724 | * Do wildcard expansion on the string 'str'. |
| 3725 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3726 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3727 | * Return NULL for failure. |
| 3728 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3729 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 3730 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 3731 | * WILD_PREV "orig" should be NULL. |
| 3732 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 3733 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 3734 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3735 | * |
| 3736 | * mode = WILD_FREE: just free previously expanded matches |
| 3737 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 3738 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 3739 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 3740 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 3741 | * mode = WILD_ALL: return all matches concatenated |
| 3742 | * mode = WILD_LONGEST: return longest matched part |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3743 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3744 | * |
| 3745 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 3746 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 3747 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 3748 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 3749 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 3750 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 3751 | * options = WILD_SILENT: don't print warning messages |
| 3752 | * options = WILD_ESCAPE: put backslash before special chars |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3753 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3754 | * |
| 3755 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 3756 | */ |
| 3757 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3758 | ExpandOne( |
| 3759 | expand_T *xp, |
| 3760 | char_u *str, |
| 3761 | char_u *orig, /* allocated copy of original of expanded string */ |
| 3762 | int options, |
| 3763 | int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3764 | { |
| 3765 | char_u *ss = NULL; |
| 3766 | static int findex; |
| 3767 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3768 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3769 | int i; |
| 3770 | long_u len; |
| 3771 | int non_suf_match; /* number without matching suffix */ |
| 3772 | |
| 3773 | /* |
| 3774 | * first handle the case of using an old match |
| 3775 | */ |
| 3776 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 3777 | { |
| 3778 | if (xp->xp_numfiles > 0) |
| 3779 | { |
| 3780 | if (mode == WILD_PREV) |
| 3781 | { |
| 3782 | if (findex == -1) |
| 3783 | findex = xp->xp_numfiles; |
| 3784 | --findex; |
| 3785 | } |
| 3786 | else /* mode == WILD_NEXT */ |
| 3787 | ++findex; |
| 3788 | |
| 3789 | /* |
| 3790 | * When wrapping around, return the original string, set findex to |
| 3791 | * -1. |
| 3792 | */ |
| 3793 | if (findex < 0) |
| 3794 | { |
| 3795 | if (orig_save == NULL) |
| 3796 | findex = xp->xp_numfiles - 1; |
| 3797 | else |
| 3798 | findex = -1; |
| 3799 | } |
| 3800 | if (findex >= xp->xp_numfiles) |
| 3801 | { |
| 3802 | if (orig_save == NULL) |
| 3803 | findex = 0; |
| 3804 | else |
| 3805 | findex = -1; |
| 3806 | } |
| 3807 | #ifdef FEAT_WILDMENU |
| 3808 | if (p_wmnu) |
| 3809 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 3810 | findex, cmd_showtail); |
| 3811 | #endif |
| 3812 | if (findex == -1) |
| 3813 | return vim_strsave(orig_save); |
| 3814 | return vim_strsave(xp->xp_files[findex]); |
| 3815 | } |
| 3816 | else |
| 3817 | return NULL; |
| 3818 | } |
| 3819 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3820 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3821 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 3822 | { |
| 3823 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3824 | xp->xp_numfiles = -1; |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 3825 | VIM_CLEAR(orig_save); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3826 | } |
| 3827 | findex = 0; |
| 3828 | |
| 3829 | if (mode == WILD_FREE) /* only release file name */ |
| 3830 | return NULL; |
| 3831 | |
| 3832 | if (xp->xp_numfiles == -1) |
| 3833 | { |
| 3834 | vim_free(orig_save); |
| 3835 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3836 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3837 | |
| 3838 | /* |
| 3839 | * Do the expansion. |
| 3840 | */ |
| 3841 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 3842 | options) == FAIL) |
| 3843 | { |
| 3844 | #ifdef FNAME_ILLEGAL |
| 3845 | /* Illegal file name has been silently skipped. But when there |
| 3846 | * are wildcards, the real problem is that there was no match, |
| 3847 | * causing the pattern to be added, which has illegal characters. |
| 3848 | */ |
| 3849 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 3850 | EMSG2(_(e_nomatch2), str); |
| 3851 | #endif |
| 3852 | } |
| 3853 | else if (xp->xp_numfiles == 0) |
| 3854 | { |
| 3855 | if (!(options & WILD_SILENT)) |
| 3856 | EMSG2(_(e_nomatch2), str); |
| 3857 | } |
| 3858 | else |
| 3859 | { |
| 3860 | /* Escape the matches for use on the command line. */ |
| 3861 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 3862 | |
| 3863 | /* |
| 3864 | * Check for matching suffixes in file names. |
| 3865 | */ |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3866 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 3867 | && mode != WILD_LONGEST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3868 | { |
| 3869 | if (xp->xp_numfiles) |
| 3870 | non_suf_match = xp->xp_numfiles; |
| 3871 | else |
| 3872 | non_suf_match = 1; |
| 3873 | if ((xp->xp_context == EXPAND_FILES |
| 3874 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3875 | && xp->xp_numfiles > 1) |
| 3876 | { |
| 3877 | /* |
| 3878 | * More than one match; check suffix. |
| 3879 | * The files will have been sorted on matching suffix in |
| 3880 | * expand_wildcards, only need to check the first two. |
| 3881 | */ |
| 3882 | non_suf_match = 0; |
| 3883 | for (i = 0; i < 2; ++i) |
| 3884 | if (match_suffix(xp->xp_files[i])) |
| 3885 | ++non_suf_match; |
| 3886 | } |
| 3887 | if (non_suf_match != 1) |
| 3888 | { |
| 3889 | /* Can we ever get here unless it's while expanding |
| 3890 | * interactively? If not, we can get rid of this all |
| 3891 | * together. Don't really want to wait for this message |
| 3892 | * (and possibly have to hit return to continue!). |
| 3893 | */ |
| 3894 | if (!(options & WILD_SILENT)) |
| 3895 | EMSG(_(e_toomany)); |
| 3896 | else if (!(options & WILD_NO_BEEP)) |
| 3897 | beep_flush(); |
| 3898 | } |
| 3899 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 3900 | ss = vim_strsave(xp->xp_files[0]); |
| 3901 | } |
| 3902 | } |
| 3903 | } |
| 3904 | |
| 3905 | /* Find longest common part */ |
| 3906 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 3907 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3908 | int mb_len = 1; |
| 3909 | int c0, ci; |
| 3910 | |
| 3911 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3912 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3913 | #ifdef FEAT_MBYTE |
| 3914 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3915 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3916 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 3917 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 3918 | } |
| 3919 | else |
| 3920 | #endif |
Bram Moolenaar | e4eda3b | 2015-11-21 16:28:50 +0100 | [diff] [blame] | 3921 | c0 = xp->xp_files[0][len]; |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3922 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 3923 | { |
| 3924 | #ifdef FEAT_MBYTE |
| 3925 | if (has_mbyte) |
| 3926 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 3927 | else |
| 3928 | #endif |
| 3929 | ci = xp->xp_files[i][len]; |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3930 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3931 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3932 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3933 | || xp->xp_context == EXPAND_BUFFERS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3934 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3935 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3936 | break; |
| 3937 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3938 | else if (c0 != ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3939 | break; |
| 3940 | } |
| 3941 | if (i < xp->xp_numfiles) |
| 3942 | { |
| 3943 | if (!(options & WILD_NO_BEEP)) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3944 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3945 | break; |
| 3946 | } |
| 3947 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3948 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3949 | ss = alloc((unsigned)len + 1); |
| 3950 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3951 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3952 | findex = -1; /* next p_wc gets first one */ |
| 3953 | } |
| 3954 | |
| 3955 | /* Concatenate all matching names */ |
| 3956 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 3957 | { |
| 3958 | len = 0; |
| 3959 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3960 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 3961 | ss = lalloc(len, TRUE); |
| 3962 | if (ss != NULL) |
| 3963 | { |
| 3964 | *ss = NUL; |
| 3965 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3966 | { |
| 3967 | STRCAT(ss, xp->xp_files[i]); |
| 3968 | if (i != xp->xp_numfiles - 1) |
| 3969 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 3970 | } |
| 3971 | } |
| 3972 | } |
| 3973 | |
| 3974 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 3975 | ExpandCleanup(xp); |
| 3976 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3977 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3978 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3979 | vim_free(orig); |
| 3980 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3981 | return ss; |
| 3982 | } |
| 3983 | |
| 3984 | /* |
| 3985 | * Prepare an expand structure for use. |
| 3986 | */ |
| 3987 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3988 | ExpandInit(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3989 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3990 | xp->xp_pattern = NULL; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3991 | xp->xp_pattern_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3992 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3993 | #ifndef BACKSLASH_IN_FILENAME |
| 3994 | xp->xp_shell = FALSE; |
| 3995 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3996 | xp->xp_numfiles = -1; |
| 3997 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3998 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 3999 | xp->xp_arg = NULL; |
| 4000 | #endif |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 4001 | xp->xp_line = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4002 | } |
| 4003 | |
| 4004 | /* |
| 4005 | * Cleanup an expand structure after use. |
| 4006 | */ |
| 4007 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4008 | ExpandCleanup(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4009 | { |
| 4010 | if (xp->xp_numfiles >= 0) |
| 4011 | { |
| 4012 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 4013 | xp->xp_numfiles = -1; |
| 4014 | } |
| 4015 | } |
| 4016 | |
| 4017 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4018 | ExpandEscape( |
| 4019 | expand_T *xp, |
| 4020 | char_u *str, |
| 4021 | int numfiles, |
| 4022 | char_u **files, |
| 4023 | int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4024 | { |
| 4025 | int i; |
| 4026 | char_u *p; |
| 4027 | |
| 4028 | /* |
| 4029 | * May change home directory back to "~" |
| 4030 | */ |
| 4031 | if (options & WILD_HOME_REPLACE) |
| 4032 | tilde_replace(str, numfiles, files); |
| 4033 | |
| 4034 | if (options & WILD_ESCAPE) |
| 4035 | { |
| 4036 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | cca92ec | 2011-04-28 17:21:53 +0200 | [diff] [blame] | 4037 | || xp->xp_context == EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4038 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4039 | || xp->xp_context == EXPAND_BUFFERS |
| 4040 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 4041 | { |
| 4042 | /* |
| 4043 | * Insert a backslash into a file name before a space, \, %, # |
| 4044 | * and wildmatch characters, except '~'. |
| 4045 | */ |
| 4046 | for (i = 0; i < numfiles; ++i) |
| 4047 | { |
| 4048 | /* for ":set path=" we need to escape spaces twice */ |
| 4049 | if (xp->xp_backslash == XP_BS_THREE) |
| 4050 | { |
| 4051 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4052 | if (p != NULL) |
| 4053 | { |
| 4054 | vim_free(files[i]); |
| 4055 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 4056 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4057 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 4058 | if (p != NULL) |
| 4059 | { |
| 4060 | vim_free(files[i]); |
| 4061 | files[i] = p; |
| 4062 | } |
| 4063 | #endif |
| 4064 | } |
| 4065 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4066 | #ifdef BACKSLASH_IN_FILENAME |
| 4067 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 4068 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4069 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 4070 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4071 | if (p != NULL) |
| 4072 | { |
| 4073 | vim_free(files[i]); |
| 4074 | files[i] = p; |
| 4075 | } |
| 4076 | |
| 4077 | /* If 'str' starts with "\~", replace "~" at start of |
| 4078 | * files[i] with "\~". */ |
| 4079 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4080 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4081 | } |
| 4082 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4083 | |
| 4084 | /* If the first file starts with a '+' escape it. Otherwise it |
| 4085 | * could be seen as "+cmd". */ |
| 4086 | if (*files[0] == '+') |
| 4087 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4088 | } |
| 4089 | else if (xp->xp_context == EXPAND_TAGS) |
| 4090 | { |
| 4091 | /* |
| 4092 | * Insert a backslash before characters in a tag name that |
| 4093 | * would terminate the ":tag" command. |
| 4094 | */ |
| 4095 | for (i = 0; i < numfiles; ++i) |
| 4096 | { |
| 4097 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 4098 | if (p != NULL) |
| 4099 | { |
| 4100 | vim_free(files[i]); |
| 4101 | files[i] = p; |
| 4102 | } |
| 4103 | } |
| 4104 | } |
| 4105 | } |
| 4106 | } |
| 4107 | |
| 4108 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4109 | * Escape special characters in "fname" for when used as a file name argument |
| 4110 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 4111 | * Returns the result in allocated memory. |
| 4112 | */ |
| 4113 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4114 | vim_strsave_fnameescape(char_u *fname, int shell) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4115 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4116 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4117 | #ifdef BACKSLASH_IN_FILENAME |
| 4118 | char_u buf[20]; |
| 4119 | int j = 0; |
| 4120 | |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4121 | /* Don't escape '[', '{' and '!' if they are in 'isfname'. */ |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4122 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4123 | if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p)) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4124 | buf[j++] = *p; |
| 4125 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4126 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4127 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4128 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 4129 | if (shell && csh_like_shell() && p != NULL) |
| 4130 | { |
| 4131 | char_u *s; |
| 4132 | |
| 4133 | /* For csh and similar shells need to put two backslashes before '!'. |
| 4134 | * One is taken by Vim, one by the shell. */ |
| 4135 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 4136 | vim_free(p); |
| 4137 | p = s; |
| 4138 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4139 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4140 | |
| 4141 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 4142 | * ":write". "cd -" has a special meaning. */ |
Bram Moolenaar | a9d52e3 | 2010-07-31 16:44:19 +0200 | [diff] [blame] | 4143 | if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4144 | escape_fname(&p); |
| 4145 | |
| 4146 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4147 | } |
| 4148 | |
| 4149 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4150 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 4151 | */ |
| 4152 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4153 | escape_fname(char_u **pp) |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4154 | { |
| 4155 | char_u *p; |
| 4156 | |
| 4157 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 4158 | if (p != NULL) |
| 4159 | { |
| 4160 | p[0] = '\\'; |
| 4161 | STRCPY(p + 1, *pp); |
| 4162 | vim_free(*pp); |
| 4163 | *pp = p; |
| 4164 | } |
| 4165 | } |
| 4166 | |
| 4167 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4168 | * For each file name in files[num_files]: |
| 4169 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 4170 | */ |
| 4171 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4172 | tilde_replace( |
| 4173 | char_u *orig_pat, |
| 4174 | int num_files, |
| 4175 | char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4176 | { |
| 4177 | int i; |
| 4178 | char_u *p; |
| 4179 | |
| 4180 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 4181 | { |
| 4182 | for (i = 0; i < num_files; ++i) |
| 4183 | { |
| 4184 | p = home_replace_save(NULL, files[i]); |
| 4185 | if (p != NULL) |
| 4186 | { |
| 4187 | vim_free(files[i]); |
| 4188 | files[i] = p; |
| 4189 | } |
| 4190 | } |
| 4191 | } |
| 4192 | } |
| 4193 | |
| 4194 | /* |
| 4195 | * Show all matches for completion on the command line. |
| 4196 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 4197 | * be inserted like a normal character. |
| 4198 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4199 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4200 | showmatches(expand_T *xp, int wildmenu UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4201 | { |
| 4202 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 4203 | int num_files; |
| 4204 | char_u **files_found; |
| 4205 | int i, j, k; |
| 4206 | int maxlen; |
| 4207 | int lines; |
| 4208 | int columns; |
| 4209 | char_u *p; |
| 4210 | int lastlen; |
| 4211 | int attr; |
| 4212 | int showtail; |
| 4213 | |
| 4214 | if (xp->xp_numfiles == -1) |
| 4215 | { |
| 4216 | set_expand_context(xp); |
| 4217 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 4218 | &num_files, &files_found); |
| 4219 | showtail = expand_showtail(xp); |
| 4220 | if (i != EXPAND_OK) |
| 4221 | return i; |
| 4222 | |
| 4223 | } |
| 4224 | else |
| 4225 | { |
| 4226 | num_files = xp->xp_numfiles; |
| 4227 | files_found = xp->xp_files; |
| 4228 | showtail = cmd_showtail; |
| 4229 | } |
| 4230 | |
| 4231 | #ifdef FEAT_WILDMENU |
| 4232 | if (!wildmenu) |
| 4233 | { |
| 4234 | #endif |
| 4235 | msg_didany = FALSE; /* lines_left will be set */ |
| 4236 | msg_start(); /* prepare for paging */ |
| 4237 | msg_putchar('\n'); |
| 4238 | out_flush(); |
| 4239 | cmdline_row = msg_row; |
| 4240 | msg_didany = FALSE; /* lines_left will be set again */ |
| 4241 | msg_start(); /* prepare for paging */ |
| 4242 | #ifdef FEAT_WILDMENU |
| 4243 | } |
| 4244 | #endif |
| 4245 | |
| 4246 | if (got_int) |
| 4247 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 4248 | #ifdef FEAT_WILDMENU |
| 4249 | else if (wildmenu) |
Bram Moolenaar | ef8eb08 | 2017-03-30 22:04:55 +0200 | [diff] [blame] | 4250 | win_redr_status_matches(xp, num_files, files_found, -1, showtail); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4251 | #endif |
| 4252 | else |
| 4253 | { |
| 4254 | /* find the length of the longest file name */ |
| 4255 | maxlen = 0; |
| 4256 | for (i = 0; i < num_files; ++i) |
| 4257 | { |
| 4258 | if (!showtail && (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4259 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4260 | || xp->xp_context == EXPAND_BUFFERS)) |
| 4261 | { |
| 4262 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 4263 | j = vim_strsize(NameBuff); |
| 4264 | } |
| 4265 | else |
| 4266 | j = vim_strsize(L_SHOWFILE(i)); |
| 4267 | if (j > maxlen) |
| 4268 | maxlen = j; |
| 4269 | } |
| 4270 | |
| 4271 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4272 | lines = num_files; |
| 4273 | else |
| 4274 | { |
| 4275 | /* compute the number of columns and lines for the listing */ |
| 4276 | maxlen += 2; /* two spaces between file names */ |
| 4277 | columns = ((int)Columns + 2) / maxlen; |
| 4278 | if (columns < 1) |
| 4279 | columns = 1; |
| 4280 | lines = (num_files + columns - 1) / columns; |
| 4281 | } |
| 4282 | |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4283 | attr = HL_ATTR(HLF_D); /* find out highlighting for directories */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4284 | |
| 4285 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4286 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4287 | MSG_PUTS_ATTR(_("tagname"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4288 | msg_clr_eos(); |
| 4289 | msg_advance(maxlen - 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4290 | MSG_PUTS_ATTR(_(" kind file\n"), HL_ATTR(HLF_T)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4291 | } |
| 4292 | |
| 4293 | /* list the files line by line */ |
| 4294 | for (i = 0; i < lines; ++i) |
| 4295 | { |
| 4296 | lastlen = 999; |
| 4297 | for (k = i; k < num_files; k += lines) |
| 4298 | { |
| 4299 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4300 | { |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4301 | msg_outtrans_attr(files_found[k], HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4302 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 4303 | msg_advance(maxlen + 1); |
| 4304 | msg_puts(p); |
| 4305 | msg_advance(maxlen + 3); |
Bram Moolenaar | 8820b48 | 2017-03-16 17:23:31 +0100 | [diff] [blame] | 4306 | msg_puts_long_attr(p + 2, HL_ATTR(HLF_D)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4307 | break; |
| 4308 | } |
| 4309 | for (j = maxlen - lastlen; --j >= 0; ) |
| 4310 | msg_putchar(' '); |
| 4311 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4312 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4313 | || xp->xp_context == EXPAND_BUFFERS) |
| 4314 | { |
Bram Moolenaar | b91e59b | 2010-03-17 19:13:27 +0100 | [diff] [blame] | 4315 | /* highlight directories */ |
Bram Moolenaar | 63fa526 | 2010-03-23 18:06:52 +0100 | [diff] [blame] | 4316 | if (xp->xp_numfiles != -1) |
| 4317 | { |
| 4318 | char_u *halved_slash; |
| 4319 | char_u *exp_path; |
| 4320 | |
| 4321 | /* Expansion was done before and special characters |
| 4322 | * were escaped, need to halve backslashes. Also |
| 4323 | * $HOME has been replaced with ~/. */ |
| 4324 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
| 4325 | halved_slash = backslash_halve_save( |
| 4326 | exp_path != NULL ? exp_path : files_found[k]); |
| 4327 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 4328 | : files_found[k]); |
| 4329 | vim_free(exp_path); |
| 4330 | vim_free(halved_slash); |
| 4331 | } |
| 4332 | else |
| 4333 | /* Expansion was done here, file names are literal. */ |
| 4334 | j = mch_isdir(files_found[k]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4335 | if (showtail) |
| 4336 | p = L_SHOWFILE(k); |
| 4337 | else |
| 4338 | { |
| 4339 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 4340 | TRUE); |
| 4341 | p = NameBuff; |
| 4342 | } |
| 4343 | } |
| 4344 | else |
| 4345 | { |
| 4346 | j = FALSE; |
| 4347 | p = L_SHOWFILE(k); |
| 4348 | } |
| 4349 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 4350 | } |
| 4351 | if (msg_col > 0) /* when not wrapped around */ |
| 4352 | { |
| 4353 | msg_clr_eos(); |
| 4354 | msg_putchar('\n'); |
| 4355 | } |
| 4356 | out_flush(); /* show one line at a time */ |
| 4357 | if (got_int) |
| 4358 | { |
| 4359 | got_int = FALSE; |
| 4360 | break; |
| 4361 | } |
| 4362 | } |
| 4363 | |
| 4364 | /* |
| 4365 | * we redraw the command below the lines that we have just listed |
| 4366 | * This is a bit tricky, but it saves a lot of screen updating. |
| 4367 | */ |
| 4368 | cmdline_row = msg_row; /* will put it back later */ |
| 4369 | } |
| 4370 | |
| 4371 | if (xp->xp_numfiles == -1) |
| 4372 | FreeWild(num_files, files_found); |
| 4373 | |
| 4374 | return EXPAND_OK; |
| 4375 | } |
| 4376 | |
| 4377 | /* |
| 4378 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 4379 | * Find tail of file name path, but ignore trailing "/". |
| 4380 | */ |
| 4381 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4382 | sm_gettail(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4383 | { |
| 4384 | char_u *p; |
| 4385 | char_u *t = s; |
| 4386 | int had_sep = FALSE; |
| 4387 | |
| 4388 | for (p = s; *p != NUL; ) |
| 4389 | { |
| 4390 | if (vim_ispathsep(*p) |
| 4391 | #ifdef BACKSLASH_IN_FILENAME |
| 4392 | && !rem_backslash(p) |
| 4393 | #endif |
| 4394 | ) |
| 4395 | had_sep = TRUE; |
| 4396 | else if (had_sep) |
| 4397 | { |
| 4398 | t = p; |
| 4399 | had_sep = FALSE; |
| 4400 | } |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 4401 | MB_PTR_ADV(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4402 | } |
| 4403 | return t; |
| 4404 | } |
| 4405 | |
| 4406 | /* |
| 4407 | * Return TRUE if we only need to show the tail of completion matches. |
| 4408 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4409 | * returned. |
| 4410 | */ |
| 4411 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4412 | expand_showtail(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4413 | { |
| 4414 | char_u *s; |
| 4415 | char_u *end; |
| 4416 | |
| 4417 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4418 | if (xp->xp_context != EXPAND_FILES |
| 4419 | && xp->xp_context != EXPAND_SHELLCMD |
| 4420 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4421 | return FALSE; |
| 4422 | |
| 4423 | end = gettail(xp->xp_pattern); |
| 4424 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4425 | return FALSE; |
| 4426 | |
| 4427 | for (s = xp->xp_pattern; s < end; s++) |
| 4428 | { |
| 4429 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4430 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4431 | if (rem_backslash(s)) |
| 4432 | ++s; |
| 4433 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4434 | return FALSE; |
| 4435 | } |
| 4436 | return TRUE; |
| 4437 | } |
| 4438 | |
| 4439 | /* |
| 4440 | * Prepare a string for expansion. |
| 4441 | * When expanding file names: The string will be used with expand_wildcards(). |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4442 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4443 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4444 | * the name into allocated memory and prepend "^". |
| 4445 | */ |
| 4446 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4447 | addstar( |
| 4448 | char_u *fname, |
| 4449 | int len, |
| 4450 | int context) /* EXPAND_FILES etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4451 | { |
| 4452 | char_u *retval; |
| 4453 | int i, j; |
| 4454 | int new_len; |
| 4455 | char_u *tail; |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4456 | int ends_in_star; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4457 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4458 | if (context != EXPAND_FILES |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4459 | && context != EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4460 | && context != EXPAND_SHELLCMD |
| 4461 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4462 | { |
| 4463 | /* |
| 4464 | * Matching will be done internally (on something other than files). |
| 4465 | * So we convert the file-matching-type wildcards into our kind for |
| 4466 | * use with vim_regcomp(). First work out how long it will be: |
| 4467 | */ |
| 4468 | |
| 4469 | /* For help tags the translation is done in find_help_tags(). |
| 4470 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4471 | if (context == EXPAND_HELP |
| 4472 | || context == EXPAND_COLORS |
| 4473 | || context == EXPAND_COMPILER |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4474 | || context == EXPAND_OWNSYNTAX |
Bram Moolenaar | 883f5d0 | 2010-06-21 06:24:34 +0200 | [diff] [blame] | 4475 | || context == EXPAND_FILETYPE |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4476 | || context == EXPAND_PACKADD |
Bram Moolenaar | ba47b51 | 2017-01-24 21:18:19 +0100 | [diff] [blame] | 4477 | || ((context == EXPAND_TAGS_LISTFILES |
| 4478 | || context == EXPAND_TAGS) |
| 4479 | && fname[0] == '/')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4480 | retval = vim_strnsave(fname, len); |
| 4481 | else |
| 4482 | { |
| 4483 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4484 | for (i = 0; i < len; i++) |
| 4485 | { |
| 4486 | if (fname[i] == '*' || fname[i] == '~') |
| 4487 | new_len++; /* '*' needs to be replaced by ".*" |
| 4488 | '~' needs to be replaced by "\~" */ |
| 4489 | |
| 4490 | /* Buffer names are like file names. "." should be literal */ |
| 4491 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4492 | new_len++; /* "." becomes "\." */ |
| 4493 | |
| 4494 | /* Custom expansion takes care of special things, match |
| 4495 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4496 | if ((context == EXPAND_USER_DEFINED |
| 4497 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4498 | new_len++; /* '\' becomes "\\" */ |
| 4499 | } |
| 4500 | retval = alloc(new_len); |
| 4501 | if (retval != NULL) |
| 4502 | { |
| 4503 | retval[0] = '^'; |
| 4504 | j = 1; |
| 4505 | for (i = 0; i < len; i++, j++) |
| 4506 | { |
| 4507 | /* Skip backslash. But why? At least keep it for custom |
| 4508 | * expansion. */ |
| 4509 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4510 | && context != EXPAND_USER_LIST |
| 4511 | && fname[i] == '\\' |
| 4512 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4513 | break; |
| 4514 | |
| 4515 | switch (fname[i]) |
| 4516 | { |
| 4517 | case '*': retval[j++] = '.'; |
| 4518 | break; |
| 4519 | case '~': retval[j++] = '\\'; |
| 4520 | break; |
| 4521 | case '?': retval[j] = '.'; |
| 4522 | continue; |
| 4523 | case '.': if (context == EXPAND_BUFFERS) |
| 4524 | retval[j++] = '\\'; |
| 4525 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4526 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4527 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4528 | retval[j++] = '\\'; |
| 4529 | break; |
| 4530 | } |
| 4531 | retval[j] = fname[i]; |
| 4532 | } |
| 4533 | retval[j] = NUL; |
| 4534 | } |
| 4535 | } |
| 4536 | } |
| 4537 | else |
| 4538 | { |
| 4539 | retval = alloc(len + 4); |
| 4540 | if (retval != NULL) |
| 4541 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4542 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4543 | |
| 4544 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4545 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4546 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4547 | * ~ would be at the start of the file name, but not the tail. |
| 4548 | * $ could be anywhere in the tail. |
| 4549 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4550 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4551 | */ |
| 4552 | tail = gettail(retval); |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4553 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 4554 | #ifndef BACKSLASH_IN_FILENAME |
| 4555 | for (i = len - 2; i >= 0; --i) |
| 4556 | { |
| 4557 | if (retval[i] != '\\') |
| 4558 | break; |
| 4559 | ends_in_star = !ends_in_star; |
| 4560 | } |
| 4561 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4562 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4563 | && !ends_in_star |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4564 | && vim_strchr(tail, '$') == NULL |
| 4565 | && vim_strchr(retval, '`') == NULL) |
| 4566 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4567 | else if (len > 0 && retval[len - 1] == '$') |
| 4568 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4569 | retval[len] = NUL; |
| 4570 | } |
| 4571 | } |
| 4572 | return retval; |
| 4573 | } |
| 4574 | |
| 4575 | /* |
| 4576 | * Must parse the command line so far to work out what context we are in. |
| 4577 | * Completion can then be done based on that context. |
| 4578 | * This routine sets the variables: |
| 4579 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4580 | * the command line (ends at the cursor). |
| 4581 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4582 | * |
| 4583 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4584 | * the command line, like an unknown command. Caller |
| 4585 | * should beep. |
| 4586 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4587 | * a normal char, rather than for completion. eg |
| 4588 | * :s/^I/ |
| 4589 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4590 | * it. |
| 4591 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4592 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4593 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4594 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4595 | * when we know only directories are of interest. eg |
| 4596 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4597 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4598 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4599 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4600 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4601 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4602 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4603 | * EXPAND_EVENTS Complete event names |
| 4604 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4605 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4606 | * EXPAND_AUGROUP Complete autocommand group names |
| 4607 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4608 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4609 | * eg :unmap a^I , :cunab x^I |
| 4610 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4611 | * eg :call sub^I |
| 4612 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4613 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4614 | * names in expressions, eg :while s^I |
| 4615 | * EXPAND_ENV_VARS Complete environment variable names |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4616 | * EXPAND_USER Complete user names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4617 | */ |
| 4618 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4619 | set_expand_context(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4620 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4621 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4622 | if (ccline.cmdfirstc != ':' |
| 4623 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4624 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4625 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4626 | #endif |
| 4627 | ) |
| 4628 | { |
| 4629 | xp->xp_context = EXPAND_NOTHING; |
| 4630 | return; |
| 4631 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4632 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4633 | } |
| 4634 | |
| 4635 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4636 | set_cmd_context( |
| 4637 | expand_T *xp, |
| 4638 | char_u *str, /* start of command line */ |
| 4639 | int len, /* length of command line (excl. NUL) */ |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4640 | int col, /* position of cursor */ |
| 4641 | int use_ccline UNUSED) /* use ccline for info */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4642 | { |
| 4643 | int old_char = NUL; |
| 4644 | char_u *nextcomm; |
| 4645 | |
| 4646 | /* |
| 4647 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4648 | * written before. |
| 4649 | */ |
| 4650 | if (col < len) |
| 4651 | old_char = str[col]; |
| 4652 | str[col] = NUL; |
| 4653 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4654 | |
| 4655 | #ifdef FEAT_EVAL |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4656 | if (use_ccline && ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4657 | { |
| 4658 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4659 | /* pass CMD_SIZE because there is no real command */ |
| 4660 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4661 | # endif |
| 4662 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4663 | else if (use_ccline && ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4664 | { |
| 4665 | xp->xp_context = ccline.xp_context; |
| 4666 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4667 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4668 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4669 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4670 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4671 | else |
| 4672 | #endif |
| 4673 | while (nextcomm != NULL) |
| 4674 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4675 | |
Bram Moolenaar | a4c8dcb | 2013-06-30 12:21:24 +0200 | [diff] [blame] | 4676 | /* Store the string here so that call_user_expand_func() can get to them |
| 4677 | * easily. */ |
| 4678 | xp->xp_line = str; |
| 4679 | xp->xp_col = col; |
| 4680 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4681 | str[col] = old_char; |
| 4682 | } |
| 4683 | |
| 4684 | /* |
| 4685 | * Expand the command line "str" from context "xp". |
| 4686 | * "xp" must have been set by set_cmd_context(). |
| 4687 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4688 | * starts. |
| 4689 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4690 | * cursor. |
| 4691 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 4692 | * key that triggered expansion literally. |
| 4693 | * Returns EXPAND_OK otherwise. |
| 4694 | */ |
| 4695 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4696 | expand_cmdline( |
| 4697 | expand_T *xp, |
| 4698 | char_u *str, /* start of command line */ |
| 4699 | int col, /* position of cursor */ |
| 4700 | int *matchcount, /* return: nr of matches */ |
| 4701 | char_u ***matches) /* return: array of pointers to matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4702 | { |
| 4703 | char_u *file_str = NULL; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4704 | int options = WILD_ADD_SLASH|WILD_SILENT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4705 | |
| 4706 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 4707 | { |
| 4708 | beep_flush(); |
| 4709 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 4710 | } |
| 4711 | if (xp->xp_context == EXPAND_NOTHING) |
| 4712 | { |
| 4713 | /* Caller can use the character as a normal char instead */ |
| 4714 | return EXPAND_NOTHING; |
| 4715 | } |
| 4716 | |
| 4717 | /* 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] | 4718 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 4719 | 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] | 4720 | if (file_str == NULL) |
| 4721 | return EXPAND_UNSUCCESSFUL; |
| 4722 | |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4723 | if (p_wic) |
| 4724 | options += WILD_ICASE; |
| 4725 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4726 | /* find all files that match the description */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4727 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4728 | { |
| 4729 | *matchcount = 0; |
| 4730 | *matches = NULL; |
| 4731 | } |
| 4732 | vim_free(file_str); |
| 4733 | |
| 4734 | return EXPAND_OK; |
| 4735 | } |
| 4736 | |
| 4737 | #ifdef FEAT_MULTI_LANG |
| 4738 | /* |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4739 | * Cleanup matches for help tags: |
| 4740 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 4741 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4742 | */ |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 4743 | static void cleanup_help_tags(int num_file, char_u **file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4744 | |
| 4745 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4746 | cleanup_help_tags(int num_file, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4747 | { |
| 4748 | int i, j; |
| 4749 | int len; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4750 | char_u buf[4]; |
| 4751 | char_u *p = buf; |
| 4752 | |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4753 | 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] | 4754 | { |
| 4755 | *p++ = '@'; |
| 4756 | *p++ = p_hlg[0]; |
| 4757 | *p++ = p_hlg[1]; |
| 4758 | } |
| 4759 | *p = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4760 | |
| 4761 | for (i = 0; i < num_file; ++i) |
| 4762 | { |
| 4763 | len = (int)STRLEN(file[i]) - 3; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4764 | if (len <= 0) |
| 4765 | continue; |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4766 | if (STRCMP(file[i] + len, "@en") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4767 | { |
| 4768 | /* Sorting on priority means the same item in another language may |
| 4769 | * be anywhere. Search all items for a match up to the "@en". */ |
| 4770 | for (j = 0; j < num_file; ++j) |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4771 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 4772 | && STRNCMP(file[i], file[j], len + 1) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4773 | break; |
| 4774 | if (j == num_file) |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4775 | /* item only exists with @en, remove it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4776 | file[i][len] = NUL; |
| 4777 | } |
| 4778 | } |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4779 | |
| 4780 | if (*buf != NUL) |
| 4781 | for (i = 0; i < num_file; ++i) |
| 4782 | { |
| 4783 | len = (int)STRLEN(file[i]) - 3; |
| 4784 | if (len <= 0) |
| 4785 | continue; |
| 4786 | if (STRCMP(file[i] + len, buf) == 0) |
| 4787 | { |
| 4788 | /* remove the default language */ |
| 4789 | file[i][len] = NUL; |
| 4790 | } |
| 4791 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4792 | } |
| 4793 | #endif |
| 4794 | |
| 4795 | /* |
| 4796 | * Do the expansion based on xp->xp_context and "pat". |
| 4797 | */ |
| 4798 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4799 | ExpandFromContext( |
| 4800 | expand_T *xp, |
| 4801 | char_u *pat, |
| 4802 | int *num_file, |
| 4803 | char_u ***file, |
| 4804 | int options) /* EW_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4805 | { |
| 4806 | #ifdef FEAT_CMDL_COMPL |
| 4807 | regmatch_T regmatch; |
| 4808 | #endif |
| 4809 | int ret; |
| 4810 | int flags; |
| 4811 | |
| 4812 | flags = EW_DIR; /* include directories */ |
| 4813 | if (options & WILD_LIST_NOTFOUND) |
| 4814 | flags |= EW_NOTFOUND; |
| 4815 | if (options & WILD_ADD_SLASH) |
| 4816 | flags |= EW_ADDSLASH; |
| 4817 | if (options & WILD_KEEP_ALL) |
| 4818 | flags |= EW_KEEPALL; |
| 4819 | if (options & WILD_SILENT) |
| 4820 | flags |= EW_SILENT; |
Bram Moolenaar | a245bc7 | 2015-03-05 19:35:25 +0100 | [diff] [blame] | 4821 | if (options & WILD_ALLLINKS) |
| 4822 | flags |= EW_ALLLINKS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4823 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4824 | if (xp->xp_context == EXPAND_FILES |
| 4825 | || xp->xp_context == EXPAND_DIRECTORIES |
| 4826 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4827 | { |
| 4828 | /* |
| 4829 | * Expand file or directory names. |
| 4830 | */ |
| 4831 | int free_pat = FALSE; |
| 4832 | int i; |
| 4833 | |
| 4834 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 4835 | * space */ |
| 4836 | if (xp->xp_backslash != XP_BS_NONE) |
| 4837 | { |
| 4838 | free_pat = TRUE; |
| 4839 | pat = vim_strsave(pat); |
| 4840 | for (i = 0; pat[i]; ++i) |
| 4841 | if (pat[i] == '\\') |
| 4842 | { |
| 4843 | if (xp->xp_backslash == XP_BS_THREE |
| 4844 | && pat[i + 1] == '\\' |
| 4845 | && pat[i + 2] == '\\' |
| 4846 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4847 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4848 | if (xp->xp_backslash == XP_BS_ONE |
| 4849 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4850 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4851 | } |
| 4852 | } |
| 4853 | |
| 4854 | if (xp->xp_context == EXPAND_FILES) |
| 4855 | flags |= EW_FILE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4856 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 4857 | flags |= (EW_FILE | EW_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4858 | else |
| 4859 | flags = (flags | EW_DIR) & ~EW_FILE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4860 | if (options & WILD_ICASE) |
| 4861 | flags |= EW_ICASE; |
| 4862 | |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 4863 | /* Expand wildcards, supporting %:h and the like. */ |
| 4864 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4865 | if (free_pat) |
| 4866 | vim_free(pat); |
| 4867 | return ret; |
| 4868 | } |
| 4869 | |
| 4870 | *file = (char_u **)""; |
| 4871 | *num_file = 0; |
| 4872 | if (xp->xp_context == EXPAND_HELP) |
| 4873 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 4874 | /* With an empty argument we would get all the help tags, which is |
| 4875 | * very slow. Get matches for "help" instead. */ |
| 4876 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 4877 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4878 | { |
| 4879 | #ifdef FEAT_MULTI_LANG |
| 4880 | cleanup_help_tags(*num_file, *file); |
| 4881 | #endif |
| 4882 | return OK; |
| 4883 | } |
| 4884 | return FAIL; |
| 4885 | } |
| 4886 | |
| 4887 | #ifndef FEAT_CMDL_COMPL |
| 4888 | return FAIL; |
| 4889 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4890 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 4891 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4892 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 4893 | return ExpandOldSetting(num_file, file); |
| 4894 | if (xp->xp_context == EXPAND_BUFFERS) |
| 4895 | return ExpandBufnames(pat, num_file, file, options); |
| 4896 | if (xp->xp_context == EXPAND_TAGS |
| 4897 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4898 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 4899 | if (xp->xp_context == EXPAND_COLORS) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4900 | { |
| 4901 | char *directories[] = {"colors", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4902 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 4903 | directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4904 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4905 | if (xp->xp_context == EXPAND_COMPILER) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4906 | { |
Bram Moolenaar | a627c96 | 2011-09-30 16:23:32 +0200 | [diff] [blame] | 4907 | char *directories[] = {"compiler", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4908 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4909 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4910 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4911 | { |
| 4912 | char *directories[] = {"syntax", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4913 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4914 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4915 | if (xp->xp_context == EXPAND_FILETYPE) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4916 | { |
| 4917 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4918 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4919 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4920 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4921 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4922 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4923 | # endif |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4924 | if (xp->xp_context == EXPAND_PACKADD) |
| 4925 | return ExpandPackAddDir(pat, num_file, file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4926 | |
| 4927 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 4928 | if (regmatch.regprog == NULL) |
| 4929 | return FAIL; |
| 4930 | |
| 4931 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 4932 | regmatch.rm_ic = ignorecase(pat); |
| 4933 | |
| 4934 | if (xp->xp_context == EXPAND_SETTINGS |
| 4935 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 4936 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 4937 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 4938 | ret = ExpandMappings(®match, num_file, file); |
| 4939 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4940 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 4941 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 4942 | # endif |
| 4943 | else |
| 4944 | { |
| 4945 | static struct expgen |
| 4946 | { |
| 4947 | int context; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 4948 | char_u *((*func)(expand_T *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4949 | int ic; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4950 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4951 | } tab[] = |
| 4952 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4953 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 4954 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
Bram Moolenaar | cae92dc | 2017-08-06 15:22:15 +0200 | [diff] [blame] | 4955 | {EXPAND_MAPCLEAR, get_mapclear_arg, TRUE, TRUE}, |
Bram Moolenaar | 9e507ca | 2016-10-15 15:39:39 +0200 | [diff] [blame] | 4956 | {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE}, |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 4957 | #ifdef FEAT_CMDHIST |
| 4958 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 4959 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4960 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4961 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 4962 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4963 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 4964 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 4965 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4966 | #endif |
| 4967 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4968 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 4969 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 4970 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 4971 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4972 | #endif |
| 4973 | #ifdef FEAT_MENU |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4974 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 4975 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4976 | #endif |
| 4977 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4978 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4979 | #endif |
Bram Moolenaar | cd9c462 | 2013-06-08 15:24:48 +0200 | [diff] [blame] | 4980 | #ifdef FEAT_PROFILE |
| 4981 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 4982 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4983 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4984 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 4985 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4986 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4987 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4988 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4989 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4990 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4991 | #endif |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 4992 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4993 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 4994 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4995 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 4996 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4997 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 4998 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4999 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5000 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 5001 | {EXPAND_USER, get_users, TRUE, FALSE}, |
Bram Moolenaar | cd43eff | 2018-03-29 15:55:38 +0200 | [diff] [blame] | 5002 | {EXPAND_ARGLIST, get_arglist_name, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5003 | }; |
| 5004 | int i; |
| 5005 | |
| 5006 | /* |
| 5007 | * Find a context in the table and call the ExpandGeneric() with the |
| 5008 | * right function to do the expansion. |
| 5009 | */ |
| 5010 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 5011 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5012 | if (xp->xp_context == tab[i].context) |
| 5013 | { |
| 5014 | if (tab[i].ic) |
| 5015 | regmatch.rm_ic = TRUE; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5016 | ret = ExpandGeneric(xp, ®match, num_file, file, |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5017 | tab[i].func, tab[i].escaped); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5018 | break; |
| 5019 | } |
| 5020 | } |
| 5021 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5022 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5023 | |
| 5024 | return ret; |
| 5025 | #endif /* FEAT_CMDL_COMPL */ |
| 5026 | } |
| 5027 | |
| 5028 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5029 | /* |
| 5030 | * Expand a list of names. |
| 5031 | * |
| 5032 | * Generic function for command line completion. It calls a function to |
| 5033 | * obtain strings, one by one. The strings are matched against a regexp |
| 5034 | * program. Matching strings are copied into an array, which is returned. |
| 5035 | * |
| 5036 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 5037 | */ |
| 5038 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5039 | ExpandGeneric( |
| 5040 | expand_T *xp, |
| 5041 | regmatch_T *regmatch, |
| 5042 | int *num_file, |
| 5043 | char_u ***file, |
| 5044 | char_u *((*func)(expand_T *, int)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5045 | /* returns a string from the list */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5046 | int escaped) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5047 | { |
| 5048 | int i; |
| 5049 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5050 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5051 | char_u *str; |
| 5052 | |
| 5053 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5054 | * round == 0: count the number of matching names |
| 5055 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5056 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5057 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5058 | { |
| 5059 | for (i = 0; ; ++i) |
| 5060 | { |
| 5061 | str = (*func)(xp, i); |
| 5062 | if (str == NULL) /* end of list */ |
| 5063 | break; |
| 5064 | if (*str == NUL) /* skip empty strings */ |
| 5065 | continue; |
| 5066 | |
| 5067 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 5068 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5069 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5070 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 5071 | if (escaped) |
| 5072 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 5073 | else |
| 5074 | str = vim_strsave(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5075 | (*file)[count] = str; |
| 5076 | #ifdef FEAT_MENU |
| 5077 | if (func == get_menu_names && str != NULL) |
| 5078 | { |
| 5079 | /* test for separator added by get_menu_names() */ |
| 5080 | str += STRLEN(str) - 1; |
| 5081 | if (*str == '\001') |
| 5082 | *str = '.'; |
| 5083 | } |
| 5084 | #endif |
| 5085 | } |
| 5086 | ++count; |
| 5087 | } |
| 5088 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5089 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5090 | { |
| 5091 | if (count == 0) |
| 5092 | return OK; |
| 5093 | *num_file = count; |
| 5094 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 5095 | if (*file == NULL) |
| 5096 | { |
| 5097 | *file = (char_u **)""; |
| 5098 | return FAIL; |
| 5099 | } |
| 5100 | count = 0; |
| 5101 | } |
| 5102 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5103 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 5104 | /* Sort the results. Keep menu's in the specified order. */ |
| 5105 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 5106 | { |
| 5107 | if (xp->xp_context == EXPAND_EXPRESSION |
| 5108 | || xp->xp_context == EXPAND_FUNCTIONS |
| 5109 | || xp->xp_context == EXPAND_USER_FUNC) |
| 5110 | /* <SNR> functions should be sorted to the end. */ |
| 5111 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 5112 | sort_func_compare); |
| 5113 | else |
| 5114 | sort_strings(*file, *num_file); |
| 5115 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5116 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 5117 | #ifdef FEAT_CMDL_COMPL |
| 5118 | /* Reset the variables used for special highlight names expansion, so that |
| 5119 | * they don't show up when getting normal highlight names by ID. */ |
| 5120 | reset_expand_highlight(); |
| 5121 | #endif |
| 5122 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5123 | return OK; |
| 5124 | } |
| 5125 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5126 | /* |
| 5127 | * Complete a shell command. |
| 5128 | * Returns FAIL or OK; |
| 5129 | */ |
| 5130 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5131 | expand_shellcmd( |
| 5132 | char_u *filepat, /* pattern to match with command names */ |
| 5133 | int *num_file, /* return: number of matches */ |
| 5134 | char_u ***file, /* return: array with matches */ |
| 5135 | int flagsarg) /* EW_ flags */ |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5136 | { |
| 5137 | char_u *pat; |
| 5138 | int i; |
| 5139 | char_u *path; |
| 5140 | int mustfree = FALSE; |
| 5141 | garray_T ga; |
| 5142 | char_u *buf = alloc(MAXPATHL); |
| 5143 | size_t l; |
| 5144 | char_u *s, *e; |
| 5145 | int flags = flagsarg; |
| 5146 | int ret; |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5147 | int did_curdir = FALSE; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5148 | |
| 5149 | if (buf == NULL) |
| 5150 | return FAIL; |
| 5151 | |
| 5152 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5153 | * space */ |
| 5154 | pat = vim_strsave(filepat); |
| 5155 | for (i = 0; pat[i]; ++i) |
| 5156 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5157 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5158 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5159 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5160 | |
| 5161 | /* For an absolute name we don't use $PATH. */ |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 5162 | if (mch_isFullName(pat)) |
| 5163 | path = (char_u *)" "; |
| 5164 | else if ((pat[0] == '.' && (vim_ispathsep(pat[1]) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5165 | || (pat[1] == '.' && vim_ispathsep(pat[2]))))) |
| 5166 | path = (char_u *)"."; |
| 5167 | else |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5168 | { |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5169 | path = vim_getenv((char_u *)"PATH", &mustfree); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5170 | if (path == NULL) |
| 5171 | path = (char_u *)""; |
| 5172 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5173 | |
| 5174 | /* |
| 5175 | * Go over all directories in $PATH. Expand matches in that directory and |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5176 | * collect them in "ga". When "." is not in $PATH also expand for the |
| 5177 | * current directory, to find "subdir/cmd". |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5178 | */ |
| 5179 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5180 | for (s = path; ; s = e) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5181 | { |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5182 | if (*s == NUL) |
| 5183 | { |
| 5184 | if (did_curdir) |
| 5185 | break; |
| 5186 | /* Find directories in the current directory, path is empty. */ |
| 5187 | did_curdir = TRUE; |
| 5188 | } |
| 5189 | else if (*s == '.') |
| 5190 | did_curdir = TRUE; |
| 5191 | |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 5192 | if (*s == ' ') |
| 5193 | ++s; /* Skip space used for absolute path name. */ |
| 5194 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5195 | #if defined(MSWIN) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5196 | e = vim_strchr(s, ';'); |
| 5197 | #else |
| 5198 | e = vim_strchr(s, ':'); |
| 5199 | #endif |
| 5200 | if (e == NULL) |
| 5201 | e = s + STRLEN(s); |
| 5202 | |
| 5203 | l = e - s; |
| 5204 | if (l > MAXPATHL - 5) |
| 5205 | break; |
| 5206 | vim_strncpy(buf, s, l); |
| 5207 | add_pathsep(buf); |
| 5208 | l = STRLEN(buf); |
| 5209 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 5210 | |
| 5211 | /* Expand matches in one directory of $PATH. */ |
| 5212 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 5213 | if (ret == OK) |
| 5214 | { |
| 5215 | if (ga_grow(&ga, *num_file) == FAIL) |
| 5216 | FreeWild(*num_file, *file); |
| 5217 | else |
| 5218 | { |
| 5219 | for (i = 0; i < *num_file; ++i) |
| 5220 | { |
| 5221 | s = (*file)[i]; |
| 5222 | if (STRLEN(s) > l) |
| 5223 | { |
| 5224 | /* Remove the path again. */ |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5225 | STRMOVE(s, s + l); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5226 | ((char_u **)ga.ga_data)[ga.ga_len++] = s; |
| 5227 | } |
| 5228 | else |
| 5229 | vim_free(s); |
| 5230 | } |
| 5231 | vim_free(*file); |
| 5232 | } |
| 5233 | } |
| 5234 | if (*e != NUL) |
| 5235 | ++e; |
| 5236 | } |
| 5237 | *file = ga.ga_data; |
| 5238 | *num_file = ga.ga_len; |
| 5239 | |
| 5240 | vim_free(buf); |
| 5241 | vim_free(pat); |
| 5242 | if (mustfree) |
| 5243 | vim_free(path); |
| 5244 | return OK; |
| 5245 | } |
| 5246 | |
| 5247 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5248 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 5249 | 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] | 5250 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5251 | /* |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 5252 | * Call "user_expand_func()" to invoke a user defined Vim script function and |
| 5253 | * return the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5254 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5255 | static void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5256 | call_user_expand_func( |
| 5257 | void *(*user_expand_func)(char_u *, int, char_u **, int), |
| 5258 | expand_T *xp, |
| 5259 | int *num_file, |
| 5260 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5261 | { |
Bram Moolenaar | 673b9a3 | 2013-06-30 22:43:27 +0200 | [diff] [blame] | 5262 | int keep = 0; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5263 | char_u num[50]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5264 | char_u *args[3]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5265 | int save_current_SID = current_SID; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5266 | void *ret; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5267 | struct cmdline_info save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5268 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5269 | 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] | 5270 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5271 | *num_file = 0; |
| 5272 | *file = NULL; |
| 5273 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5274 | if (ccline.cmdbuff != NULL) |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5275 | { |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5276 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 5277 | ccline.cmdbuff[ccline.cmdlen] = 0; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5278 | } |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5279 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5280 | args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5281 | args[1] = xp->xp_line; |
| 5282 | sprintf((char *)num, "%d", xp->xp_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5283 | args[2] = num; |
| 5284 | |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5285 | /* Save the cmdline, we don't know what the function may do. */ |
| 5286 | save_ccline = ccline; |
| 5287 | ccline.cmdbuff = NULL; |
| 5288 | ccline.cmdprompt = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5289 | current_SID = xp->xp_scriptID; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5290 | |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5291 | ret = user_expand_func(xp->xp_arg, 3, args, FALSE); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5292 | |
| 5293 | ccline = save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5294 | current_SID = save_current_SID; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5295 | if (ccline.cmdbuff != NULL) |
| 5296 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5297 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5298 | vim_free(args[0]); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5299 | return ret; |
| 5300 | } |
| 5301 | |
| 5302 | /* |
| 5303 | * Expand names with a function defined by the user. |
| 5304 | */ |
| 5305 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5306 | ExpandUserDefined( |
| 5307 | expand_T *xp, |
| 5308 | regmatch_T *regmatch, |
| 5309 | int *num_file, |
| 5310 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5311 | { |
| 5312 | char_u *retstr; |
| 5313 | char_u *s; |
| 5314 | char_u *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5315 | int keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5316 | garray_T ga; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5317 | int skip; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5318 | |
| 5319 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 5320 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5321 | return FAIL; |
| 5322 | |
| 5323 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5324 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5325 | { |
| 5326 | e = vim_strchr(s, '\n'); |
| 5327 | if (e == NULL) |
| 5328 | e = s + STRLEN(s); |
| 5329 | keep = *e; |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5330 | *e = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5331 | |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5332 | skip = xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0; |
| 5333 | *e = keep; |
| 5334 | |
| 5335 | if (!skip) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5336 | { |
Bram Moolenaar | 71a43c0 | 2018-02-11 15:20:20 +0100 | [diff] [blame] | 5337 | if (ga_grow(&ga, 1) == FAIL) |
| 5338 | break; |
| 5339 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 5340 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5341 | } |
| 5342 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5343 | if (*e != NUL) |
| 5344 | ++e; |
| 5345 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5346 | vim_free(retstr); |
| 5347 | *file = ga.ga_data; |
| 5348 | *num_file = ga.ga_len; |
| 5349 | return OK; |
| 5350 | } |
| 5351 | |
| 5352 | /* |
| 5353 | * Expand names with a list returned by a function defined by the user. |
| 5354 | */ |
| 5355 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5356 | ExpandUserList( |
| 5357 | expand_T *xp, |
| 5358 | int *num_file, |
| 5359 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5360 | { |
| 5361 | list_T *retlist; |
| 5362 | listitem_T *li; |
| 5363 | garray_T ga; |
| 5364 | |
| 5365 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 5366 | if (retlist == NULL) |
| 5367 | return FAIL; |
| 5368 | |
| 5369 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 5370 | /* Loop over the items in the list. */ |
| 5371 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 5372 | { |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5373 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 5374 | continue; /* Skip non-string items and empty strings */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5375 | |
| 5376 | if (ga_grow(&ga, 1) == FAIL) |
| 5377 | break; |
| 5378 | |
| 5379 | ((char_u **)ga.ga_data)[ga.ga_len] = |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5380 | vim_strsave(li->li_tv.vval.v_string); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5381 | ++ga.ga_len; |
| 5382 | } |
| 5383 | list_unref(retlist); |
| 5384 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5385 | *file = ga.ga_data; |
| 5386 | *num_file = ga.ga_len; |
| 5387 | return OK; |
| 5388 | } |
| 5389 | #endif |
| 5390 | |
| 5391 | /* |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5392 | * Expand color scheme, compiler or filetype names. |
| 5393 | * Search from 'runtimepath': |
| 5394 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 5395 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 5396 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 5397 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 5398 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5399 | * "dirnames" is an array with one or more directory names. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5400 | */ |
| 5401 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5402 | ExpandRTDir( |
| 5403 | char_u *pat, |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5404 | int flags, |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5405 | int *num_file, |
| 5406 | char_u ***file, |
| 5407 | char *dirnames[]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5408 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5409 | char_u *s; |
| 5410 | char_u *e; |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5411 | char_u *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5412 | garray_T ga; |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5413 | int i; |
| 5414 | int pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5415 | |
| 5416 | *num_file = 0; |
| 5417 | *file = NULL; |
Bram Moolenaar | 5cfe2d7 | 2011-07-07 15:04:52 +0200 | [diff] [blame] | 5418 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5419 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5420 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5421 | for (i = 0; dirnames[i] != NULL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5422 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5423 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
| 5424 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5425 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5426 | ga_clear_strings(&ga); |
| 5427 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5428 | } |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5429 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5430 | globpath(p_rtp, s, &ga, 0); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5431 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5432 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5433 | |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5434 | if (flags & DIP_START) { |
| 5435 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5436 | { |
| 5437 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22)); |
| 5438 | if (s == NULL) |
| 5439 | { |
| 5440 | ga_clear_strings(&ga); |
| 5441 | return FAIL; |
| 5442 | } |
| 5443 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 5444 | globpath(p_pp, s, &ga, 0); |
| 5445 | vim_free(s); |
| 5446 | } |
| 5447 | } |
| 5448 | |
| 5449 | if (flags & DIP_OPT) { |
| 5450 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5451 | { |
| 5452 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20)); |
| 5453 | if (s == NULL) |
| 5454 | { |
| 5455 | ga_clear_strings(&ga); |
| 5456 | return FAIL; |
| 5457 | } |
| 5458 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 5459 | globpath(p_pp, s, &ga, 0); |
| 5460 | vim_free(s); |
| 5461 | } |
| 5462 | } |
| 5463 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5464 | for (i = 0; i < ga.ga_len; ++i) |
| 5465 | { |
| 5466 | match = ((char_u **)ga.ga_data)[i]; |
| 5467 | s = match; |
| 5468 | e = s + STRLEN(s); |
| 5469 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 5470 | { |
| 5471 | e -= 4; |
Bram Moolenaar | 91acfff | 2017-03-12 19:22:36 +0100 | [diff] [blame] | 5472 | for (s = e; s > match; MB_PTR_BACK(match, s)) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5473 | if (s < match || vim_ispathsep(*s)) |
| 5474 | break; |
| 5475 | ++s; |
| 5476 | *e = NUL; |
| 5477 | mch_memmove(match, s, e - s + 1); |
| 5478 | } |
| 5479 | } |
| 5480 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5481 | if (ga.ga_len == 0) |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5482 | return FAIL; |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5483 | |
| 5484 | /* Sort and remove duplicates which can happen when specifying multiple |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5485 | * directories in dirnames. */ |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5486 | remove_duplicates(&ga); |
| 5487 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5488 | *file = ga.ga_data; |
| 5489 | *num_file = ga.ga_len; |
| 5490 | return OK; |
| 5491 | } |
| 5492 | |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5493 | /* |
| 5494 | * Expand loadplugin names: |
| 5495 | * 'packpath'/pack/ * /opt/{pat} |
| 5496 | */ |
| 5497 | static int |
| 5498 | ExpandPackAddDir( |
| 5499 | char_u *pat, |
| 5500 | int *num_file, |
| 5501 | char_u ***file) |
| 5502 | { |
| 5503 | char_u *s; |
| 5504 | char_u *e; |
| 5505 | char_u *match; |
| 5506 | garray_T ga; |
| 5507 | int i; |
| 5508 | int pat_len; |
| 5509 | |
| 5510 | *num_file = 0; |
| 5511 | *file = NULL; |
| 5512 | pat_len = (int)STRLEN(pat); |
| 5513 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 5514 | |
| 5515 | s = alloc((unsigned)(pat_len + 26)); |
| 5516 | if (s == NULL) |
| 5517 | { |
| 5518 | ga_clear_strings(&ga); |
| 5519 | return FAIL; |
| 5520 | } |
| 5521 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 5522 | globpath(p_pp, s, &ga, 0); |
| 5523 | vim_free(s); |
| 5524 | |
| 5525 | for (i = 0; i < ga.ga_len; ++i) |
| 5526 | { |
| 5527 | match = ((char_u **)ga.ga_data)[i]; |
| 5528 | s = gettail(match); |
| 5529 | e = s + STRLEN(s); |
| 5530 | mch_memmove(match, s, e - s + 1); |
| 5531 | } |
| 5532 | |
| 5533 | if (ga.ga_len == 0) |
| 5534 | return FAIL; |
| 5535 | |
| 5536 | /* Sort and remove duplicates which can happen when specifying multiple |
| 5537 | * directories in dirnames. */ |
| 5538 | remove_duplicates(&ga); |
| 5539 | |
| 5540 | *file = ga.ga_data; |
| 5541 | *num_file = ga.ga_len; |
| 5542 | return OK; |
| 5543 | } |
| 5544 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5545 | #endif |
| 5546 | |
| 5547 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 5548 | /* |
| 5549 | * Expand "file" for all comma-separated directories in "path". |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5550 | * Adds the matches to "ga". Caller must init "ga". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5551 | */ |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5552 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5553 | globpath( |
| 5554 | char_u *path, |
| 5555 | char_u *file, |
| 5556 | garray_T *ga, |
| 5557 | int expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5558 | { |
| 5559 | expand_T xpc; |
| 5560 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5561 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5562 | int num_p; |
| 5563 | char_u **p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5564 | |
| 5565 | buf = alloc(MAXPATHL); |
| 5566 | if (buf == NULL) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5567 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5568 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5569 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5570 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5571 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5572 | /* Loop over all entries in {path}. */ |
| 5573 | while (*path != NUL) |
| 5574 | { |
| 5575 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 5576 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 5577 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 5578 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5579 | # if defined(MSWIN) |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5580 | /* Using the platform's path separator (\) makes vim incorrectly |
| 5581 | * treat it as an escape character, use '/' instead. */ |
| 5582 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 5583 | STRCAT(buf, "/"); |
| 5584 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5585 | add_pathsep(buf); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5586 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5587 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5588 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 5589 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5590 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5591 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5592 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5593 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5594 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5595 | for (i = 0; i < num_p; ++i) |
| 5596 | { |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5597 | ((char_u **)ga->ga_data)[ga->ga_len] = |
Bram Moolenaar | 7116aa0 | 2014-05-29 14:36:29 +0200 | [diff] [blame] | 5598 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5599 | ++ga->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5600 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5601 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5602 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5603 | FreeWild(num_p, p); |
| 5604 | } |
| 5605 | } |
| 5606 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5607 | |
| 5608 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5609 | } |
| 5610 | |
| 5611 | #endif |
| 5612 | |
| 5613 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5614 | |
| 5615 | /********************************* |
| 5616 | * Command line history stuff * |
| 5617 | *********************************/ |
| 5618 | |
| 5619 | /* |
| 5620 | * Translate a history character to the associated type number. |
| 5621 | */ |
| 5622 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5623 | hist_char2type(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5624 | { |
| 5625 | if (c == ':') |
| 5626 | return HIST_CMD; |
| 5627 | if (c == '=') |
| 5628 | return HIST_EXPR; |
| 5629 | if (c == '@') |
| 5630 | return HIST_INPUT; |
| 5631 | if (c == '>') |
| 5632 | return HIST_DEBUG; |
| 5633 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5634 | } |
| 5635 | |
| 5636 | /* |
| 5637 | * Table of history names. |
| 5638 | * These names are used in :history and various hist...() functions. |
| 5639 | * It is sufficient to give the significant prefix of a history name. |
| 5640 | */ |
| 5641 | |
| 5642 | static char *(history_names[]) = |
| 5643 | { |
| 5644 | "cmd", |
| 5645 | "search", |
| 5646 | "expr", |
| 5647 | "input", |
| 5648 | "debug", |
| 5649 | NULL |
| 5650 | }; |
| 5651 | |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5652 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5653 | /* |
| 5654 | * Function given to ExpandGeneric() to obtain the possible first |
| 5655 | * arguments of the ":history command. |
| 5656 | */ |
| 5657 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5658 | get_history_arg(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5659 | { |
| 5660 | static char_u compl[2] = { NUL, NUL }; |
| 5661 | char *short_names = ":=@>?/"; |
Bram Moolenaar | 17bd9dc | 2012-05-25 11:02:41 +0200 | [diff] [blame] | 5662 | int short_names_count = (int)STRLEN(short_names); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5663 | int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
| 5664 | |
| 5665 | if (idx < short_names_count) |
| 5666 | { |
| 5667 | compl[0] = (char_u)short_names[idx]; |
| 5668 | return compl; |
| 5669 | } |
| 5670 | if (idx < short_names_count + history_name_count) |
| 5671 | return (char_u *)history_names[idx - short_names_count]; |
| 5672 | if (idx == short_names_count + history_name_count) |
| 5673 | return (char_u *)"all"; |
| 5674 | return NULL; |
| 5675 | } |
| 5676 | #endif |
| 5677 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5678 | /* |
| 5679 | * init_history() - Initialize the command line history. |
| 5680 | * Also used to re-allocate the history when the size changes. |
| 5681 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 5682 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5683 | init_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5684 | { |
| 5685 | int newlen; /* new length of history table */ |
| 5686 | histentry_T *temp; |
| 5687 | int i; |
| 5688 | int j; |
| 5689 | int type; |
| 5690 | |
| 5691 | /* |
| 5692 | * If size of history table changed, reallocate it |
| 5693 | */ |
| 5694 | newlen = (int)p_hi; |
| 5695 | if (newlen != hislen) /* history length changed */ |
| 5696 | { |
| 5697 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 5698 | { |
| 5699 | if (newlen) |
| 5700 | { |
| 5701 | temp = (histentry_T *)lalloc( |
| 5702 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 5703 | if (temp == NULL) /* out of memory! */ |
| 5704 | { |
| 5705 | if (type == 0) /* first one: just keep the old length */ |
| 5706 | { |
| 5707 | newlen = hislen; |
| 5708 | break; |
| 5709 | } |
| 5710 | /* Already changed one table, now we can only have zero |
| 5711 | * length for all tables. */ |
| 5712 | newlen = 0; |
| 5713 | type = -1; |
| 5714 | continue; |
| 5715 | } |
| 5716 | } |
| 5717 | else |
| 5718 | temp = NULL; |
| 5719 | if (newlen == 0 || temp != NULL) |
| 5720 | { |
| 5721 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 5722 | { |
| 5723 | for (i = 0; i < newlen; ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5724 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5725 | } |
| 5726 | else if (newlen > hislen) /* array becomes bigger */ |
| 5727 | { |
| 5728 | for (i = 0; i <= hisidx[type]; ++i) |
| 5729 | temp[i] = history[type][i]; |
| 5730 | j = i; |
| 5731 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5732 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5733 | for ( ; j < hislen; ++i, ++j) |
| 5734 | temp[i] = history[type][j]; |
| 5735 | } |
| 5736 | else /* array becomes smaller or 0 */ |
| 5737 | { |
| 5738 | j = hisidx[type]; |
| 5739 | for (i = newlen - 1; ; --i) |
| 5740 | { |
| 5741 | if (i >= 0) /* copy newest entries */ |
| 5742 | temp[i] = history[type][j]; |
| 5743 | else /* remove older entries */ |
| 5744 | vim_free(history[type][j].hisstr); |
| 5745 | if (--j < 0) |
| 5746 | j = hislen - 1; |
| 5747 | if (j == hisidx[type]) |
| 5748 | break; |
| 5749 | } |
| 5750 | hisidx[type] = newlen - 1; |
| 5751 | } |
| 5752 | vim_free(history[type]); |
| 5753 | history[type] = temp; |
| 5754 | } |
| 5755 | } |
| 5756 | hislen = newlen; |
| 5757 | } |
| 5758 | } |
| 5759 | |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5760 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5761 | clear_hist_entry(histentry_T *hisptr) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5762 | { |
| 5763 | hisptr->hisnum = 0; |
| 5764 | hisptr->viminfo = FALSE; |
| 5765 | hisptr->hisstr = NULL; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 5766 | hisptr->time_set = 0; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5767 | } |
| 5768 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5769 | /* |
| 5770 | * Check if command line 'str' is already in history. |
| 5771 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 5772 | */ |
| 5773 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5774 | in_history( |
| 5775 | int type, |
| 5776 | char_u *str, |
| 5777 | int move_to_front, /* Move the entry to the front if it exists */ |
| 5778 | int sep, |
| 5779 | int writing) /* ignore entries read from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5780 | { |
| 5781 | int i; |
| 5782 | int last_i = -1; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5783 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5784 | |
| 5785 | if (hisidx[type] < 0) |
| 5786 | return FALSE; |
| 5787 | i = hisidx[type]; |
| 5788 | do |
| 5789 | { |
| 5790 | if (history[type][i].hisstr == NULL) |
| 5791 | return FALSE; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5792 | |
| 5793 | /* For search history, check that the separator character matches as |
| 5794 | * well. */ |
| 5795 | p = history[type][i].hisstr; |
| 5796 | if (STRCMP(str, p) == 0 |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5797 | && !(writing && history[type][i].viminfo) |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5798 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5799 | { |
| 5800 | if (!move_to_front) |
| 5801 | return TRUE; |
| 5802 | last_i = i; |
| 5803 | break; |
| 5804 | } |
| 5805 | if (--i < 0) |
| 5806 | i = hislen - 1; |
| 5807 | } while (i != hisidx[type]); |
| 5808 | |
| 5809 | if (last_i >= 0) |
| 5810 | { |
| 5811 | str = history[type][i].hisstr; |
| 5812 | while (i != hisidx[type]) |
| 5813 | { |
| 5814 | if (++i >= hislen) |
| 5815 | i = 0; |
| 5816 | history[type][last_i] = history[type][i]; |
| 5817 | last_i = i; |
| 5818 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5819 | history[type][i].hisnum = ++hisnum[type]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5820 | history[type][i].viminfo = FALSE; |
| 5821 | history[type][i].hisstr = str; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5822 | history[type][i].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5823 | return TRUE; |
| 5824 | } |
| 5825 | return FALSE; |
| 5826 | } |
| 5827 | |
| 5828 | /* |
| 5829 | * Convert history name (from table above) to its HIST_ equivalent. |
| 5830 | * When "name" is empty, return "cmd" history. |
| 5831 | * Returns -1 for unknown history name. |
| 5832 | */ |
| 5833 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5834 | get_histtype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5835 | { |
| 5836 | int i; |
| 5837 | int len = (int)STRLEN(name); |
| 5838 | |
| 5839 | /* No argument: use current history. */ |
| 5840 | if (len == 0) |
| 5841 | return hist_char2type(ccline.cmdfirstc); |
| 5842 | |
| 5843 | for (i = 0; history_names[i] != NULL; ++i) |
| 5844 | if (STRNICMP(name, history_names[i], len) == 0) |
| 5845 | return i; |
| 5846 | |
| 5847 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 5848 | return hist_char2type(name[0]); |
| 5849 | |
| 5850 | return -1; |
| 5851 | } |
| 5852 | |
| 5853 | static int last_maptick = -1; /* last seen maptick */ |
| 5854 | |
| 5855 | /* |
| 5856 | * Add the given string to the given history. If the string is already in the |
| 5857 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 5858 | * values. |
| 5859 | */ |
| 5860 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5861 | add_to_history( |
| 5862 | int histype, |
| 5863 | char_u *new_entry, |
| 5864 | int in_map, /* consider maptick when inside a mapping */ |
| 5865 | int sep) /* separator character used (search hist) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5866 | { |
| 5867 | histentry_T *hisptr; |
| 5868 | int len; |
| 5869 | |
| 5870 | if (hislen == 0) /* no history */ |
| 5871 | return; |
| 5872 | |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 5873 | if (cmdmod.keeppatterns && histype == HIST_SEARCH) |
| 5874 | return; |
| 5875 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5876 | /* |
| 5877 | * Searches inside the same mapping overwrite each other, so that only |
| 5878 | * the last line is kept. Be careful not to remove a line that was moved |
| 5879 | * down, only lines that were added. |
| 5880 | */ |
| 5881 | if (histype == HIST_SEARCH && in_map) |
| 5882 | { |
Bram Moolenaar | 4664371 | 2016-09-09 21:42:36 +0200 | [diff] [blame] | 5883 | if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5884 | { |
| 5885 | /* Current line is from the same mapping, remove it */ |
| 5886 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 5887 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5888 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5889 | --hisnum[histype]; |
| 5890 | if (--hisidx[HIST_SEARCH] < 0) |
| 5891 | hisidx[HIST_SEARCH] = hislen - 1; |
| 5892 | } |
| 5893 | last_maptick = -1; |
| 5894 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5895 | if (!in_history(histype, new_entry, TRUE, sep, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5896 | { |
| 5897 | if (++hisidx[histype] == hislen) |
| 5898 | hisidx[histype] = 0; |
| 5899 | hisptr = &history[histype][hisidx[histype]]; |
| 5900 | vim_free(hisptr->hisstr); |
| 5901 | |
| 5902 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5903 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5904 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 5905 | if (hisptr->hisstr != NULL) |
| 5906 | hisptr->hisstr[len + 1] = sep; |
| 5907 | |
| 5908 | hisptr->hisnum = ++hisnum[histype]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5909 | hisptr->viminfo = FALSE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5910 | hisptr->time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5911 | if (histype == HIST_SEARCH && in_map) |
| 5912 | last_maptick = maptick; |
| 5913 | } |
| 5914 | } |
| 5915 | |
| 5916 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 5917 | |
| 5918 | /* |
| 5919 | * Get identifier of newest history entry. |
| 5920 | * "histype" may be one of the HIST_ values. |
| 5921 | */ |
| 5922 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5923 | get_history_idx(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5924 | { |
| 5925 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5926 | || hisidx[histype] < 0) |
| 5927 | return -1; |
| 5928 | |
| 5929 | return history[histype][hisidx[histype]].hisnum; |
| 5930 | } |
| 5931 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5932 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5933 | * Calculate history index from a number: |
| 5934 | * num > 0: seen as identifying number of a history entry |
| 5935 | * num < 0: relative position in history wrt newest entry |
| 5936 | * "histype" may be one of the HIST_ values. |
| 5937 | */ |
| 5938 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5939 | calc_hist_idx(int histype, int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5940 | { |
| 5941 | int i; |
| 5942 | histentry_T *hist; |
| 5943 | int wrapped = FALSE; |
| 5944 | |
| 5945 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5946 | || (i = hisidx[histype]) < 0 || num == 0) |
| 5947 | return -1; |
| 5948 | |
| 5949 | hist = history[histype]; |
| 5950 | if (num > 0) |
| 5951 | { |
| 5952 | while (hist[i].hisnum > num) |
| 5953 | if (--i < 0) |
| 5954 | { |
| 5955 | if (wrapped) |
| 5956 | break; |
| 5957 | i += hislen; |
| 5958 | wrapped = TRUE; |
| 5959 | } |
| 5960 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 5961 | return i; |
| 5962 | } |
| 5963 | else if (-num <= hislen) |
| 5964 | { |
| 5965 | i += num + 1; |
| 5966 | if (i < 0) |
| 5967 | i += hislen; |
| 5968 | if (hist[i].hisstr != NULL) |
| 5969 | return i; |
| 5970 | } |
| 5971 | return -1; |
| 5972 | } |
| 5973 | |
| 5974 | /* |
| 5975 | * Get a history entry by its index. |
| 5976 | * "histype" may be one of the HIST_ values. |
| 5977 | */ |
| 5978 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5979 | get_history_entry(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5980 | { |
| 5981 | idx = calc_hist_idx(histype, idx); |
| 5982 | if (idx >= 0) |
| 5983 | return history[histype][idx].hisstr; |
| 5984 | else |
| 5985 | return (char_u *)""; |
| 5986 | } |
| 5987 | |
| 5988 | /* |
| 5989 | * Clear all entries of a history. |
| 5990 | * "histype" may be one of the HIST_ values. |
| 5991 | */ |
| 5992 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5993 | clr_history(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5994 | { |
| 5995 | int i; |
| 5996 | histentry_T *hisptr; |
| 5997 | |
| 5998 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 5999 | { |
| 6000 | hisptr = history[histype]; |
| 6001 | for (i = hislen; i--;) |
| 6002 | { |
| 6003 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6004 | clear_hist_entry(hisptr); |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 6005 | hisptr++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6006 | } |
| 6007 | hisidx[histype] = -1; /* mark history as cleared */ |
| 6008 | hisnum[histype] = 0; /* reset identifier counter */ |
| 6009 | return OK; |
| 6010 | } |
| 6011 | return FAIL; |
| 6012 | } |
| 6013 | |
| 6014 | /* |
| 6015 | * Remove all entries matching {str} from a history. |
| 6016 | * "histype" may be one of the HIST_ values. |
| 6017 | */ |
| 6018 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6019 | del_history_entry(int histype, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6020 | { |
| 6021 | regmatch_T regmatch; |
| 6022 | histentry_T *hisptr; |
| 6023 | int idx; |
| 6024 | int i; |
| 6025 | int last; |
| 6026 | int found = FALSE; |
| 6027 | |
| 6028 | regmatch.regprog = NULL; |
| 6029 | regmatch.rm_ic = FALSE; /* always match case */ |
| 6030 | if (hislen != 0 |
| 6031 | && histype >= 0 |
| 6032 | && histype < HIST_COUNT |
| 6033 | && *str != NUL |
| 6034 | && (idx = hisidx[histype]) >= 0 |
| 6035 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 6036 | != NULL) |
| 6037 | { |
| 6038 | i = last = idx; |
| 6039 | do |
| 6040 | { |
| 6041 | hisptr = &history[histype][i]; |
| 6042 | if (hisptr->hisstr == NULL) |
| 6043 | break; |
| 6044 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 6045 | { |
| 6046 | found = TRUE; |
| 6047 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6048 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6049 | } |
| 6050 | else |
| 6051 | { |
| 6052 | if (i != last) |
| 6053 | { |
| 6054 | history[histype][last] = *hisptr; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6055 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6056 | } |
| 6057 | if (--last < 0) |
| 6058 | last += hislen; |
| 6059 | } |
| 6060 | if (--i < 0) |
| 6061 | i += hislen; |
| 6062 | } while (i != idx); |
| 6063 | if (history[histype][idx].hisstr == NULL) |
| 6064 | hisidx[histype] = -1; |
| 6065 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 6066 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6067 | return found; |
| 6068 | } |
| 6069 | |
| 6070 | /* |
| 6071 | * Remove an indexed entry from a history. |
| 6072 | * "histype" may be one of the HIST_ values. |
| 6073 | */ |
| 6074 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6075 | del_history_idx(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6076 | { |
| 6077 | int i, j; |
| 6078 | |
| 6079 | i = calc_hist_idx(histype, idx); |
| 6080 | if (i < 0) |
| 6081 | return FALSE; |
| 6082 | idx = hisidx[histype]; |
| 6083 | vim_free(history[histype][i].hisstr); |
| 6084 | |
| 6085 | /* When deleting the last added search string in a mapping, reset |
| 6086 | * last_maptick, so that the last added search string isn't deleted again. |
| 6087 | */ |
| 6088 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 6089 | last_maptick = -1; |
| 6090 | |
| 6091 | while (i != idx) |
| 6092 | { |
| 6093 | j = (i + 1) % hislen; |
| 6094 | history[histype][i] = history[histype][j]; |
| 6095 | i = j; |
| 6096 | } |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6097 | clear_hist_entry(&history[histype][i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6098 | if (--i < 0) |
| 6099 | i += hislen; |
| 6100 | hisidx[histype] = i; |
| 6101 | return TRUE; |
| 6102 | } |
| 6103 | |
| 6104 | #endif /* FEAT_EVAL */ |
| 6105 | |
| 6106 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 6107 | /* |
| 6108 | * Very specific function to remove the value in ":set key=val" from the |
| 6109 | * history. |
| 6110 | */ |
| 6111 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6112 | remove_key_from_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6113 | { |
| 6114 | char_u *p; |
| 6115 | int i; |
| 6116 | |
| 6117 | i = hisidx[HIST_CMD]; |
| 6118 | if (i < 0) |
| 6119 | return; |
| 6120 | p = history[HIST_CMD][i].hisstr; |
| 6121 | if (p != NULL) |
| 6122 | for ( ; *p; ++p) |
| 6123 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 6124 | { |
| 6125 | p = vim_strchr(p + 3, '='); |
| 6126 | if (p == NULL) |
| 6127 | break; |
| 6128 | ++p; |
Bram Moolenaar | 1c46544 | 2017-03-12 20:10:05 +0100 | [diff] [blame] | 6129 | for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6130 | if (p[i] == '\\' && p[i + 1]) |
| 6131 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6132 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6133 | --p; |
| 6134 | } |
| 6135 | } |
| 6136 | #endif |
| 6137 | |
| 6138 | #endif /* FEAT_CMDHIST */ |
| 6139 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6140 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6141 | /* |
| 6142 | * Get pointer to the command line info to use. cmdline_paste() may clear |
| 6143 | * ccline and put the previous value in prev_ccline. |
| 6144 | */ |
| 6145 | static struct cmdline_info * |
| 6146 | get_ccline_ptr(void) |
| 6147 | { |
| 6148 | if ((State & CMDLINE) == 0) |
| 6149 | return NULL; |
| 6150 | if (ccline.cmdbuff != NULL) |
| 6151 | return &ccline; |
| 6152 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 6153 | return &prev_ccline; |
| 6154 | return NULL; |
| 6155 | } |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6156 | #endif |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6157 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6158 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6159 | /* |
| 6160 | * Get the current command line in allocated memory. |
| 6161 | * Only works when the command line is being edited. |
| 6162 | * Returns NULL when something is wrong. |
| 6163 | */ |
| 6164 | char_u * |
| 6165 | get_cmdline_str(void) |
| 6166 | { |
| 6167 | struct cmdline_info *p = get_ccline_ptr(); |
| 6168 | |
| 6169 | if (p == NULL) |
| 6170 | return NULL; |
| 6171 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
| 6172 | } |
| 6173 | |
| 6174 | /* |
| 6175 | * Get the current command line position, counted in bytes. |
| 6176 | * Zero is the first position. |
| 6177 | * Only works when the command line is being edited. |
| 6178 | * Returns -1 when something is wrong. |
| 6179 | */ |
| 6180 | int |
| 6181 | get_cmdline_pos(void) |
| 6182 | { |
| 6183 | struct cmdline_info *p = get_ccline_ptr(); |
| 6184 | |
| 6185 | if (p == NULL) |
| 6186 | return -1; |
| 6187 | return p->cmdpos; |
| 6188 | } |
| 6189 | |
| 6190 | /* |
| 6191 | * Set the command line byte position to "pos". Zero is the first position. |
| 6192 | * Only works when the command line is being edited. |
| 6193 | * Returns 1 when failed, 0 when OK. |
| 6194 | */ |
| 6195 | int |
| 6196 | set_cmdline_pos( |
| 6197 | int pos) |
| 6198 | { |
| 6199 | struct cmdline_info *p = get_ccline_ptr(); |
| 6200 | |
| 6201 | if (p == NULL) |
| 6202 | return 1; |
| 6203 | |
| 6204 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 6205 | * changed the command line. */ |
| 6206 | if (pos < 0) |
| 6207 | new_cmdpos = 0; |
| 6208 | else |
| 6209 | new_cmdpos = pos; |
| 6210 | return 0; |
| 6211 | } |
| 6212 | #endif |
| 6213 | |
| 6214 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
| 6215 | /* |
| 6216 | * Get the current command-line type. |
| 6217 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
| 6218 | * Only works when the command line is being edited. |
| 6219 | * Returns NUL when something is wrong. |
| 6220 | */ |
| 6221 | int |
| 6222 | get_cmdline_type(void) |
| 6223 | { |
| 6224 | struct cmdline_info *p = get_ccline_ptr(); |
| 6225 | |
| 6226 | if (p == NULL) |
| 6227 | return NUL; |
| 6228 | if (p->cmdfirstc == NUL) |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6229 | return |
| 6230 | # ifdef FEAT_EVAL |
| 6231 | (p->input_fn) ? '@' : |
| 6232 | # endif |
| 6233 | '-'; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6234 | return p->cmdfirstc; |
| 6235 | } |
| 6236 | #endif |
| 6237 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6238 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 6239 | /* |
| 6240 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 6241 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 6242 | * Returns OK if parsed successfully, otherwise FAIL. |
| 6243 | */ |
| 6244 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6245 | get_list_range(char_u **str, int *num1, int *num2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6246 | { |
| 6247 | int len; |
| 6248 | int first = FALSE; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 6249 | varnumber_T num; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6250 | |
| 6251 | *str = skipwhite(*str); |
| 6252 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 6253 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6254 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6255 | *str += len; |
| 6256 | *num1 = (int)num; |
| 6257 | first = TRUE; |
| 6258 | } |
| 6259 | *str = skipwhite(*str); |
| 6260 | if (**str == ',') /* parse "to" part of range */ |
| 6261 | { |
| 6262 | *str = skipwhite(*str + 1); |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6263 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6264 | if (len > 0) |
| 6265 | { |
| 6266 | *num2 = (int)num; |
| 6267 | *str = skipwhite(*str + len); |
| 6268 | } |
| 6269 | else if (!first) /* no number given at all */ |
| 6270 | return FAIL; |
| 6271 | } |
| 6272 | else if (first) /* only one number given */ |
| 6273 | *num2 = *num1; |
| 6274 | return OK; |
| 6275 | } |
| 6276 | #endif |
| 6277 | |
| 6278 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 6279 | /* |
| 6280 | * :history command - print a history |
| 6281 | */ |
| 6282 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6283 | ex_history(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6284 | { |
| 6285 | histentry_T *hist; |
| 6286 | int histype1 = HIST_CMD; |
| 6287 | int histype2 = HIST_CMD; |
| 6288 | int hisidx1 = 1; |
| 6289 | int hisidx2 = -1; |
| 6290 | int idx; |
| 6291 | int i, j, k; |
| 6292 | char_u *end; |
| 6293 | char_u *arg = eap->arg; |
| 6294 | |
| 6295 | if (hislen == 0) |
| 6296 | { |
| 6297 | MSG(_("'history' option is zero")); |
| 6298 | return; |
| 6299 | } |
| 6300 | |
| 6301 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 6302 | { |
| 6303 | end = arg; |
| 6304 | while (ASCII_ISALPHA(*end) |
| 6305 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 6306 | end++; |
| 6307 | i = *end; |
| 6308 | *end = NUL; |
| 6309 | histype1 = get_histtype(arg); |
| 6310 | if (histype1 == -1) |
| 6311 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 6312 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6313 | { |
| 6314 | histype1 = 0; |
| 6315 | histype2 = HIST_COUNT-1; |
| 6316 | } |
| 6317 | else |
| 6318 | { |
| 6319 | *end = i; |
| 6320 | EMSG(_(e_trailing)); |
| 6321 | return; |
| 6322 | } |
| 6323 | } |
| 6324 | else |
| 6325 | histype2 = histype1; |
| 6326 | *end = i; |
| 6327 | } |
| 6328 | else |
| 6329 | end = arg; |
| 6330 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 6331 | { |
| 6332 | EMSG(_(e_trailing)); |
| 6333 | return; |
| 6334 | } |
| 6335 | |
| 6336 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 6337 | { |
| 6338 | STRCPY(IObuff, "\n # "); |
| 6339 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 6340 | MSG_PUTS_TITLE(IObuff); |
| 6341 | idx = hisidx[histype1]; |
| 6342 | hist = history[histype1]; |
| 6343 | j = hisidx1; |
| 6344 | k = hisidx2; |
| 6345 | if (j < 0) |
| 6346 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 6347 | if (k < 0) |
| 6348 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 6349 | if (idx >= 0 && j <= k) |
| 6350 | for (i = idx + 1; !got_int; ++i) |
| 6351 | { |
| 6352 | if (i == hislen) |
| 6353 | i = 0; |
| 6354 | if (hist[i].hisstr != NULL |
| 6355 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 6356 | { |
| 6357 | msg_putchar('\n'); |
| 6358 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 6359 | hist[i].hisnum); |
| 6360 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 6361 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
Bram Moolenaar | 38f5f95 | 2012-01-26 13:01:59 +0100 | [diff] [blame] | 6362 | (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6363 | else |
| 6364 | STRCAT(IObuff, hist[i].hisstr); |
| 6365 | msg_outtrans(IObuff); |
| 6366 | out_flush(); |
| 6367 | } |
| 6368 | if (i == idx) |
| 6369 | break; |
| 6370 | } |
| 6371 | } |
| 6372 | } |
| 6373 | #endif |
| 6374 | |
| 6375 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6376 | /* |
| 6377 | * Buffers for history read from a viminfo file. Only valid while reading. |
| 6378 | */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6379 | static histentry_T *viminfo_history[HIST_COUNT] = |
| 6380 | {NULL, NULL, NULL, NULL, NULL}; |
| 6381 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 6382 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6383 | static int viminfo_add_at_front = FALSE; |
| 6384 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 6385 | static int hist_type2char(int type, int use_question); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6386 | |
| 6387 | /* |
| 6388 | * Translate a history type number to the associated character. |
| 6389 | */ |
| 6390 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6391 | hist_type2char( |
| 6392 | int type, |
| 6393 | int use_question) /* use '?' instead of '/' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6394 | { |
| 6395 | if (type == HIST_CMD) |
| 6396 | return ':'; |
| 6397 | if (type == HIST_SEARCH) |
| 6398 | { |
| 6399 | if (use_question) |
| 6400 | return '?'; |
| 6401 | else |
| 6402 | return '/'; |
| 6403 | } |
| 6404 | if (type == HIST_EXPR) |
| 6405 | return '='; |
| 6406 | return '@'; |
| 6407 | } |
| 6408 | |
| 6409 | /* |
| 6410 | * Prepare for reading the history from the viminfo file. |
| 6411 | * This allocates history arrays to store the read history lines. |
| 6412 | */ |
| 6413 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6414 | prepare_viminfo_history(int asklen, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6415 | { |
| 6416 | int i; |
| 6417 | int num; |
| 6418 | int type; |
| 6419 | int len; |
| 6420 | |
| 6421 | init_history(); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6422 | viminfo_add_at_front = (asklen != 0 && !writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6423 | if (asklen > hislen) |
| 6424 | asklen = hislen; |
| 6425 | |
| 6426 | for (type = 0; type < HIST_COUNT; ++type) |
| 6427 | { |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6428 | /* Count the number of empty spaces in the history list. Entries read |
| 6429 | * from viminfo previously are also considered empty. If there are |
| 6430 | * more spaces available than we request, then fill them up. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6431 | for (i = 0, num = 0; i < hislen; i++) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6432 | if (history[type][i].hisstr == NULL || history[type][i].viminfo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6433 | num++; |
| 6434 | len = asklen; |
| 6435 | if (num > len) |
| 6436 | len = num; |
| 6437 | if (len <= 0) |
| 6438 | viminfo_history[type] = NULL; |
| 6439 | else |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6440 | viminfo_history[type] = (histentry_T *)lalloc( |
| 6441 | (long_u)(len * sizeof(histentry_T)), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6442 | if (viminfo_history[type] == NULL) |
| 6443 | len = 0; |
| 6444 | viminfo_hislen[type] = len; |
| 6445 | viminfo_hisidx[type] = 0; |
| 6446 | } |
| 6447 | } |
| 6448 | |
| 6449 | /* |
| 6450 | * Accept a line from the viminfo, store it in the history array when it's |
| 6451 | * new. |
| 6452 | */ |
| 6453 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6454 | read_viminfo_history(vir_T *virp, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6455 | { |
| 6456 | int type; |
| 6457 | long_u len; |
| 6458 | char_u *val; |
| 6459 | char_u *p; |
| 6460 | |
| 6461 | type = hist_char2type(virp->vir_line[0]); |
| 6462 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6463 | { |
| 6464 | val = viminfo_readstring(virp, 1, TRUE); |
| 6465 | if (val != NULL && *val != NUL) |
| 6466 | { |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6467 | int sep = (*val == ' ' ? NUL : *val); |
| 6468 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6469 | if (!in_history(type, val + (type == HIST_SEARCH), |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6470 | viminfo_add_at_front, sep, writing)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6471 | { |
| 6472 | /* Need to re-allocate to append the separator byte. */ |
| 6473 | len = STRLEN(val); |
| 6474 | p = lalloc(len + 2, TRUE); |
| 6475 | if (p != NULL) |
| 6476 | { |
| 6477 | if (type == HIST_SEARCH) |
| 6478 | { |
| 6479 | /* Search entry: Move the separator from the first |
| 6480 | * column to after the NUL. */ |
| 6481 | mch_memmove(p, val + 1, (size_t)len); |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6482 | p[len] = sep; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6483 | } |
| 6484 | else |
| 6485 | { |
| 6486 | /* Not a search entry: No separator in the viminfo |
| 6487 | * file, add a NUL separator. */ |
| 6488 | mch_memmove(p, val, (size_t)len + 1); |
| 6489 | p[len + 1] = NUL; |
| 6490 | } |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6491 | viminfo_history[type][viminfo_hisidx[type]].hisstr = p; |
| 6492 | viminfo_history[type][viminfo_hisidx[type]].time_set = 0; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6493 | viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE; |
| 6494 | viminfo_history[type][viminfo_hisidx[type]].hisnum = 0; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6495 | viminfo_hisidx[type]++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6496 | } |
| 6497 | } |
| 6498 | } |
| 6499 | vim_free(val); |
| 6500 | } |
| 6501 | return viminfo_readline(virp); |
| 6502 | } |
| 6503 | |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6504 | /* |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6505 | * Accept a new style history line from the viminfo, store it in the history |
| 6506 | * array when it's new. |
| 6507 | */ |
| 6508 | void |
| 6509 | handle_viminfo_history( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6510 | garray_T *values, |
| 6511 | int writing) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6512 | { |
| 6513 | int type; |
| 6514 | long_u len; |
| 6515 | char_u *val; |
| 6516 | char_u *p; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6517 | bval_T *vp = (bval_T *)values->ga_data; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6518 | |
| 6519 | /* Check the format: |
| 6520 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6521 | if (values->ga_len < 4 |
| 6522 | || vp[0].bv_type != BVAL_NR |
| 6523 | || vp[1].bv_type != BVAL_NR |
| 6524 | || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY) |
| 6525 | || vp[3].bv_type != BVAL_STRING) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6526 | return; |
| 6527 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6528 | type = vp[0].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6529 | if (type >= HIST_COUNT) |
| 6530 | return; |
| 6531 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6532 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6533 | val = vp[3].bv_string; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6534 | if (val != NULL && *val != NUL) |
| 6535 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6536 | int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR |
| 6537 | ? vp[2].bv_nr : NUL; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6538 | int idx; |
| 6539 | int overwrite = FALSE; |
| 6540 | |
| 6541 | if (!in_history(type, val, viminfo_add_at_front, sep, writing)) |
| 6542 | { |
| 6543 | /* If lines were written by an older Vim we need to avoid |
| 6544 | * getting duplicates. See if the entry already exists. */ |
| 6545 | for (idx = 0; idx < viminfo_hisidx[type]; ++idx) |
| 6546 | { |
| 6547 | p = viminfo_history[type][idx].hisstr; |
| 6548 | if (STRCMP(val, p) == 0 |
| 6549 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
| 6550 | { |
| 6551 | overwrite = TRUE; |
| 6552 | break; |
| 6553 | } |
| 6554 | } |
| 6555 | |
| 6556 | if (!overwrite) |
| 6557 | { |
| 6558 | /* Need to re-allocate to append the separator byte. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6559 | len = vp[3].bv_len; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6560 | p = lalloc(len + 2, TRUE); |
| 6561 | } |
Bram Moolenaar | 72e697d | 2016-06-13 22:48:01 +0200 | [diff] [blame] | 6562 | else |
| 6563 | len = 0; /* for picky compilers */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6564 | if (p != NULL) |
| 6565 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6566 | viminfo_history[type][idx].time_set = vp[1].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6567 | if (!overwrite) |
| 6568 | { |
| 6569 | mch_memmove(p, val, (size_t)len + 1); |
| 6570 | /* Put the separator after the NUL. */ |
| 6571 | p[len + 1] = sep; |
| 6572 | viminfo_history[type][idx].hisstr = p; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6573 | viminfo_history[type][idx].hisnum = 0; |
| 6574 | viminfo_history[type][idx].viminfo = TRUE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6575 | viminfo_hisidx[type]++; |
| 6576 | } |
| 6577 | } |
| 6578 | } |
| 6579 | } |
| 6580 | } |
| 6581 | } |
| 6582 | |
| 6583 | /* |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6584 | * Concatenate history lines from viminfo after the lines typed in this Vim. |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6585 | */ |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6586 | static void |
| 6587 | concat_history(int type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6588 | { |
| 6589 | int idx; |
| 6590 | int i; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6591 | |
| 6592 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 6593 | if (idx >= hislen) |
| 6594 | idx -= hislen; |
| 6595 | else if (idx < 0) |
| 6596 | idx = hislen - 1; |
| 6597 | if (viminfo_add_at_front) |
| 6598 | hisidx[type] = idx; |
| 6599 | else |
| 6600 | { |
| 6601 | if (hisidx[type] == -1) |
| 6602 | hisidx[type] = hislen - 1; |
| 6603 | do |
| 6604 | { |
| 6605 | if (history[type][idx].hisstr != NULL |
| 6606 | || history[type][idx].viminfo) |
| 6607 | break; |
| 6608 | if (++idx == hislen) |
| 6609 | idx = 0; |
| 6610 | } while (idx != hisidx[type]); |
| 6611 | if (idx != hisidx[type] && --idx < 0) |
| 6612 | idx = hislen - 1; |
| 6613 | } |
| 6614 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6615 | { |
| 6616 | vim_free(history[type][idx].hisstr); |
| 6617 | history[type][idx].hisstr = viminfo_history[type][i].hisstr; |
| 6618 | history[type][idx].viminfo = TRUE; |
| 6619 | history[type][idx].time_set = viminfo_history[type][i].time_set; |
| 6620 | if (--idx < 0) |
| 6621 | idx = hislen - 1; |
| 6622 | } |
| 6623 | idx += 1; |
| 6624 | idx %= hislen; |
| 6625 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6626 | { |
| 6627 | history[type][idx++].hisnum = ++hisnum[type]; |
| 6628 | idx %= hislen; |
| 6629 | } |
| 6630 | } |
| 6631 | |
| 6632 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6633 | static int |
| 6634 | #ifdef __BORLANDC__ |
| 6635 | _RTLENTRYF |
| 6636 | #endif |
| 6637 | sort_hist(const void *s1, const void *s2) |
| 6638 | { |
| 6639 | histentry_T *p1 = *(histentry_T **)s1; |
| 6640 | histentry_T *p2 = *(histentry_T **)s2; |
| 6641 | |
| 6642 | if (p1->time_set < p2->time_set) return -1; |
| 6643 | if (p1->time_set > p2->time_set) return 1; |
| 6644 | return 0; |
| 6645 | } |
| 6646 | #endif |
| 6647 | |
| 6648 | /* |
| 6649 | * Merge history lines from viminfo and lines typed in this Vim based on the |
| 6650 | * timestamp; |
| 6651 | */ |
| 6652 | static void |
| 6653 | merge_history(int type) |
| 6654 | { |
| 6655 | int max_len; |
| 6656 | histentry_T **tot_hist; |
| 6657 | histentry_T *new_hist; |
| 6658 | int i; |
| 6659 | int len; |
| 6660 | |
| 6661 | /* Make one long list with all entries. */ |
| 6662 | max_len = hislen + viminfo_hisidx[type]; |
| 6663 | tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *)); |
| 6664 | new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T)); |
| 6665 | if (tot_hist == NULL || new_hist == NULL) |
| 6666 | { |
| 6667 | vim_free(tot_hist); |
| 6668 | vim_free(new_hist); |
| 6669 | return; |
| 6670 | } |
| 6671 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6672 | tot_hist[i] = &viminfo_history[type][i]; |
| 6673 | len = i; |
| 6674 | for (i = 0; i < hislen; i++) |
| 6675 | if (history[type][i].hisstr != NULL) |
| 6676 | tot_hist[len++] = &history[type][i]; |
| 6677 | |
| 6678 | /* Sort the list on timestamp. */ |
| 6679 | qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist); |
| 6680 | |
| 6681 | /* Keep the newest ones. */ |
| 6682 | for (i = 0; i < hislen; i++) |
| 6683 | { |
| 6684 | if (i < len) |
| 6685 | { |
| 6686 | new_hist[i] = *tot_hist[i]; |
| 6687 | tot_hist[i]->hisstr = NULL; |
| 6688 | if (new_hist[i].hisnum == 0) |
| 6689 | new_hist[i].hisnum = ++hisnum[type]; |
| 6690 | } |
| 6691 | else |
| 6692 | clear_hist_entry(&new_hist[i]); |
| 6693 | } |
Bram Moolenaar | a890f5e | 2016-06-12 23:03:19 +0200 | [diff] [blame] | 6694 | hisidx[type] = (i < len ? i : len) - 1; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6695 | |
| 6696 | /* Free what is not kept. */ |
| 6697 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6698 | vim_free(viminfo_history[type][i].hisstr); |
| 6699 | for (i = 0; i < hislen; i++) |
| 6700 | vim_free(history[type][i].hisstr); |
| 6701 | vim_free(history[type]); |
| 6702 | history[type] = new_hist; |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 6703 | vim_free(tot_hist); |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6704 | } |
| 6705 | |
| 6706 | /* |
| 6707 | * Finish reading history lines from viminfo. Not used when writing viminfo. |
| 6708 | */ |
| 6709 | void |
| 6710 | finish_viminfo_history(vir_T *virp) |
| 6711 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6712 | int type; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6713 | int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6714 | |
| 6715 | for (type = 0; type < HIST_COUNT; ++type) |
| 6716 | { |
| 6717 | if (history[type] == NULL) |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6718 | continue; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6719 | |
| 6720 | if (merge) |
| 6721 | merge_history(type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6722 | else |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6723 | concat_history(type); |
| 6724 | |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 6725 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6726 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6727 | } |
| 6728 | } |
| 6729 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6730 | /* |
| 6731 | * Write history to viminfo file in "fp". |
| 6732 | * When "merge" is TRUE merge history lines with a previously read viminfo |
| 6733 | * file, data is in viminfo_history[]. |
| 6734 | * When "merge" is FALSE just write all history lines. Used for ":wviminfo!". |
| 6735 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6736 | void |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6737 | write_viminfo_history(FILE *fp, int merge) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6738 | { |
| 6739 | int i; |
| 6740 | int type; |
| 6741 | int num_saved; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6742 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6743 | |
| 6744 | init_history(); |
| 6745 | if (hislen == 0) |
| 6746 | return; |
| 6747 | for (type = 0; type < HIST_COUNT; ++type) |
| 6748 | { |
| 6749 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 6750 | if (num_saved == 0) |
| 6751 | continue; |
| 6752 | if (num_saved < 0) /* Use default */ |
| 6753 | num_saved = hislen; |
| 6754 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 6755 | type == HIST_CMD ? _("Command Line") : |
| 6756 | type == HIST_SEARCH ? _("Search String") : |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6757 | type == HIST_EXPR ? _("Expression") : |
| 6758 | type == HIST_INPUT ? _("Input Line") : |
| 6759 | _("Debug Line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6760 | if (num_saved > hislen) |
| 6761 | num_saved = hislen; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6762 | |
| 6763 | /* |
| 6764 | * Merge typed and viminfo history: |
| 6765 | * round 1: history of typed commands. |
| 6766 | * round 2: history from recently read viminfo. |
| 6767 | */ |
| 6768 | for (round = 1; round <= 2; ++round) |
| 6769 | { |
Bram Moolenaar | a8565fe | 2013-04-15 16:14:22 +0200 | [diff] [blame] | 6770 | if (round == 1) |
| 6771 | /* start at newest entry, somewhere in the list */ |
| 6772 | i = hisidx[type]; |
| 6773 | else if (viminfo_hisidx[type] > 0) |
| 6774 | /* start at newest entry, first in the list */ |
| 6775 | i = 0; |
| 6776 | else |
| 6777 | /* empty list */ |
| 6778 | i = -1; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6779 | if (i >= 0) |
| 6780 | while (num_saved > 0 |
| 6781 | && !(round == 2 && i >= viminfo_hisidx[type])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6782 | { |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6783 | char_u *p; |
| 6784 | time_t timestamp; |
| 6785 | int c = NUL; |
| 6786 | |
| 6787 | if (round == 1) |
| 6788 | { |
| 6789 | p = history[type][i].hisstr; |
| 6790 | timestamp = history[type][i].time_set; |
| 6791 | } |
| 6792 | else |
| 6793 | { |
| 6794 | p = viminfo_history[type] == NULL ? NULL |
| 6795 | : viminfo_history[type][i].hisstr; |
| 6796 | timestamp = viminfo_history[type] == NULL ? 0 |
| 6797 | : viminfo_history[type][i].time_set; |
| 6798 | } |
| 6799 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6800 | if (p != NULL && (round == 2 |
| 6801 | || !merge |
| 6802 | || !history[type][i].viminfo)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6803 | { |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6804 | --num_saved; |
| 6805 | fputc(hist_type2char(type, TRUE), fp); |
| 6806 | /* For the search history: put the separator in the |
| 6807 | * second column; use a space if there isn't one. */ |
| 6808 | if (type == HIST_SEARCH) |
| 6809 | { |
| 6810 | c = p[STRLEN(p) + 1]; |
| 6811 | putc(c == NUL ? ' ' : c, fp); |
| 6812 | } |
| 6813 | viminfo_writestring(fp, p); |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6814 | |
| 6815 | { |
| 6816 | char cbuf[NUMBUFLEN]; |
| 6817 | |
| 6818 | /* New style history with a bar line. Format: |
| 6819 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
| 6820 | if (c == NUL) |
| 6821 | cbuf[0] = NUL; |
| 6822 | else |
| 6823 | sprintf(cbuf, "%d", c); |
| 6824 | fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY, |
| 6825 | type, (long)timestamp, cbuf); |
| 6826 | barline_writestring(fp, p, LSIZE - 20); |
| 6827 | putc('\n', fp); |
| 6828 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6829 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6830 | if (round == 1) |
| 6831 | { |
| 6832 | /* Decrement index, loop around and stop when back at |
| 6833 | * the start. */ |
| 6834 | if (--i < 0) |
| 6835 | i = hislen - 1; |
| 6836 | if (i == hisidx[type]) |
| 6837 | break; |
| 6838 | } |
| 6839 | else |
| 6840 | { |
| 6841 | /* Increment index. Stop at the end in the while. */ |
| 6842 | ++i; |
| 6843 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6844 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6845 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6846 | for (i = 0; i < viminfo_hisidx[type]; ++i) |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6847 | if (viminfo_history[type] != NULL) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6848 | vim_free(viminfo_history[type][i].hisstr); |
Bram Moolenaar | d23a823 | 2018-02-10 18:45:26 +0100 | [diff] [blame] | 6849 | VIM_CLEAR(viminfo_history[type]); |
Bram Moolenaar | b70a473 | 2013-04-15 22:22:57 +0200 | [diff] [blame] | 6850 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6851 | } |
| 6852 | } |
| 6853 | #endif /* FEAT_VIMINFO */ |
| 6854 | |
| 6855 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 6856 | /* |
| 6857 | * Write a character at the current cursor+offset position. |
| 6858 | * It is directly written into the command buffer block. |
| 6859 | */ |
| 6860 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6861 | cmd_pchar(int c, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6862 | { |
| 6863 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 6864 | { |
| 6865 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 6866 | return; |
| 6867 | } |
| 6868 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 6869 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 6870 | } |
| 6871 | |
| 6872 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6873 | cmd_gchar(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(_("cmd_gchar beyond the command length")); */ |
| 6878 | return NUL; |
| 6879 | } |
| 6880 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 6881 | } |
| 6882 | #endif |
| 6883 | |
| 6884 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 6885 | /* |
| 6886 | * Open a window on the current command line and history. Allow editing in |
| 6887 | * the window. Returns when the window is closed. |
| 6888 | * Returns: |
| 6889 | * CR if the command is to be executed |
| 6890 | * Ctrl_C if it is to be abandoned |
| 6891 | * K_IGNORE if editing continues |
| 6892 | */ |
| 6893 | static int |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 6894 | open_cmdwin(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6895 | { |
| 6896 | struct cmdline_info save_ccline; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6897 | bufref_T old_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6898 | win_T *old_curwin = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6899 | bufref_T bufref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6900 | win_T *wp; |
| 6901 | int i; |
| 6902 | linenr_T lnum; |
| 6903 | int histtype; |
| 6904 | garray_T winsizes; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6905 | int save_restart_edit = restart_edit; |
| 6906 | int save_State = State; |
| 6907 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6908 | #ifdef FEAT_RIGHTLEFT |
| 6909 | int save_cmdmsg_rl = cmdmsg_rl; |
| 6910 | #endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6911 | #ifdef FEAT_FOLDING |
| 6912 | int save_KeyTyped; |
| 6913 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6914 | |
| 6915 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 6916 | if (cmdwin_type != 0 |
| 6917 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 6918 | || cmdline_star > 0 |
| 6919 | # endif |
| 6920 | ) |
| 6921 | { |
| 6922 | beep_flush(); |
| 6923 | return K_IGNORE; |
| 6924 | } |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6925 | set_bufref(&old_curbuf, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6926 | |
| 6927 | /* Save current window sizes. */ |
| 6928 | win_size_save(&winsizes); |
| 6929 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6930 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6931 | block_autocmds(); |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 6932 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 6933 | /* don't use a new tab page */ |
| 6934 | cmdmod.tab = 0; |
Bram Moolenaar | 3bab939 | 2017-04-07 15:42:25 +0200 | [diff] [blame] | 6935 | cmdmod.noswapfile = 1; |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 6936 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6937 | /* Create a window for the command-line buffer. */ |
| 6938 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 6939 | { |
| 6940 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6941 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6942 | return K_IGNORE; |
| 6943 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6944 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6945 | |
| 6946 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 6947 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6948 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6949 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6950 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 6951 | #ifdef FEAT_FOLDING |
| 6952 | curwin->w_p_fen = FALSE; |
| 6953 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6954 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6955 | curwin->w_p_rl = cmdmsg_rl; |
| 6956 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6957 | # endif |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 6958 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6959 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6960 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6961 | unblock_autocmds(); |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 6962 | /* But don't allow switching to another buffer. */ |
| 6963 | ++curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6964 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6965 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 6966 | need_wait_return = FALSE; |
| 6967 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6968 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6969 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 6970 | { |
| 6971 | if (p_wc == TAB) |
| 6972 | { |
| 6973 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 6974 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 6975 | } |
| 6976 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 6977 | } |
Bram Moolenaar | 1814183 | 2017-06-25 21:17:25 +0200 | [diff] [blame] | 6978 | --curbuf_lock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6979 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6980 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 6981 | * sets 'textwidth' to 78). */ |
| 6982 | curbuf->b_p_tw = 0; |
| 6983 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6984 | /* Fill the buffer with the history. */ |
| 6985 | init_history(); |
| 6986 | if (hislen > 0) |
| 6987 | { |
| 6988 | i = hisidx[histtype]; |
| 6989 | if (i >= 0) |
| 6990 | { |
| 6991 | lnum = 0; |
| 6992 | do |
| 6993 | { |
| 6994 | if (++i == hislen) |
| 6995 | i = 0; |
| 6996 | if (history[histtype][i].hisstr != NULL) |
| 6997 | ml_append(lnum++, history[histtype][i].hisstr, |
| 6998 | (colnr_T)0, FALSE); |
| 6999 | } |
| 7000 | while (i != hisidx[histtype]); |
| 7001 | } |
| 7002 | } |
| 7003 | |
| 7004 | /* Replace the empty last line with the current command-line and put the |
| 7005 | * cursor there. */ |
| 7006 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 7007 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 7008 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7009 | changed_line_abv_curs(); |
| 7010 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 7011 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7012 | |
| 7013 | /* Save the command line info, can be used recursively. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7014 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7015 | |
| 7016 | /* No Ex mode here! */ |
| 7017 | exmode_active = 0; |
| 7018 | |
| 7019 | State = NORMAL; |
| 7020 | # ifdef FEAT_MOUSE |
| 7021 | setmouse(); |
| 7022 | # endif |
| 7023 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7024 | /* Trigger CmdwinEnter autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7025 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINENTER); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 7026 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 7027 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7028 | |
| 7029 | i = RedrawingDisabled; |
| 7030 | RedrawingDisabled = 0; |
| 7031 | |
| 7032 | /* |
| 7033 | * Call the main loop until <CR> or CTRL-C is typed. |
| 7034 | */ |
| 7035 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 7036 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7037 | |
| 7038 | RedrawingDisabled = i; |
| 7039 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7040 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7041 | save_KeyTyped = KeyTyped; |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7042 | # endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7043 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7044 | /* Trigger CmdwinLeave autocommands. */ |
Bram Moolenaar | fafcf0d | 2017-10-19 18:35:51 +0200 | [diff] [blame] | 7045 | trigger_cmd_autocmd(cmdwin_type, EVENT_CMDWINLEAVE); |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7046 | |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7047 | # ifdef FEAT_FOLDING |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 7048 | /* Restore KeyTyped in case it is modified by autocommands */ |
| 7049 | KeyTyped = save_KeyTyped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7050 | # endif |
| 7051 | |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 7052 | /* Restore the command line info. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 7053 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7054 | cmdwin_type = 0; |
| 7055 | |
| 7056 | exmode_active = save_exmode; |
| 7057 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 7058 | /* 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] | 7059 | * this happens! */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7060 | if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7061 | { |
| 7062 | cmdwin_result = Ctrl_C; |
| 7063 | EMSG(_("E199: Active window or buffer deleted")); |
| 7064 | } |
| 7065 | else |
| 7066 | { |
Bram Moolenaar | f2bd8ef | 2018-03-04 18:08:14 +0100 | [diff] [blame] | 7067 | # if defined(FEAT_EVAL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7068 | /* autocmds may abort script processing */ |
| 7069 | if (aborting() && cmdwin_result != K_IGNORE) |
| 7070 | cmdwin_result = Ctrl_C; |
| 7071 | # endif |
| 7072 | /* Set the new command line from the cmdline buffer. */ |
| 7073 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7074 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7075 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7076 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 7077 | |
| 7078 | if (histtype == HIST_CMD) |
| 7079 | { |
| 7080 | /* Execute the command directly. */ |
| 7081 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 7082 | cmdwin_result = CAR; |
| 7083 | } |
| 7084 | else |
| 7085 | { |
| 7086 | /* First need to cancel what we were doing. */ |
| 7087 | ccline.cmdbuff = NULL; |
| 7088 | stuffcharReadbuff(':'); |
| 7089 | stuffReadbuff((char_u *)p); |
| 7090 | stuffcharReadbuff(CAR); |
| 7091 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7092 | } |
| 7093 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 7094 | { |
| 7095 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 7096 | cmdwin_result = CAR; |
| 7097 | } |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 7098 | else if (cmdwin_result == Ctrl_C) |
| 7099 | { |
| 7100 | /* :q or :close, don't execute any command |
| 7101 | * and don't modify the cmd window. */ |
| 7102 | ccline.cmdbuff = NULL; |
| 7103 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7104 | else |
| 7105 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 7106 | if (ccline.cmdbuff == NULL) |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7107 | { |
| 7108 | ccline.cmdbuff = vim_strsave((char_u *)""); |
| 7109 | ccline.cmdlen = 0; |
| 7110 | ccline.cmdbufflen = 1; |
| 7111 | ccline.cmdpos = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7112 | cmdwin_result = Ctrl_C; |
Bram Moolenaar | 5a15b6a | 2017-07-11 15:11:57 +0200 | [diff] [blame] | 7113 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7114 | else |
| 7115 | { |
| 7116 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 7117 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 7118 | ccline.cmdpos = curwin->w_cursor.col; |
| 7119 | if (ccline.cmdpos > ccline.cmdlen) |
| 7120 | ccline.cmdpos = ccline.cmdlen; |
| 7121 | if (cmdwin_result == K_IGNORE) |
| 7122 | { |
| 7123 | set_cmdspos_cursor(); |
| 7124 | redrawcmd(); |
| 7125 | } |
| 7126 | } |
| 7127 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7128 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7129 | block_autocmds(); |
Bram Moolenaar | fa67fbe | 2015-06-25 18:20:36 +0200 | [diff] [blame] | 7130 | # ifdef FEAT_CONCEAL |
| 7131 | /* Avoid command-line window first character being concealed. */ |
| 7132 | curwin->w_p_cole = 0; |
| 7133 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7134 | wp = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7135 | set_bufref(&bufref, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7136 | win_goto(old_curwin); |
| 7137 | win_close(wp, TRUE); |
Bram Moolenaar | 8006d69 | 2010-03-02 17:23:21 +0100 | [diff] [blame] | 7138 | |
| 7139 | /* win_close() may have already wiped the buffer when 'bh' is |
| 7140 | * set to 'wipe' */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7141 | if (bufref_valid(&bufref)) |
| 7142 | close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7143 | |
| 7144 | /* Restore window sizes. */ |
| 7145 | win_size_restore(&winsizes); |
| 7146 | |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7147 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7148 | } |
| 7149 | |
| 7150 | ga_clear(&winsizes); |
| 7151 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7152 | # ifdef FEAT_RIGHTLEFT |
| 7153 | cmdmsg_rl = save_cmdmsg_rl; |
| 7154 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7155 | |
| 7156 | State = save_State; |
| 7157 | # ifdef FEAT_MOUSE |
| 7158 | setmouse(); |
| 7159 | # endif |
| 7160 | |
| 7161 | return cmdwin_result; |
| 7162 | } |
| 7163 | #endif /* FEAT_CMDWIN */ |
| 7164 | |
| 7165 | /* |
| 7166 | * Used for commands that either take a simple command string argument, or: |
| 7167 | * cmd << endmarker |
| 7168 | * {script} |
| 7169 | * endmarker |
| 7170 | * Returns a pointer to allocated memory with {script} or NULL. |
| 7171 | */ |
| 7172 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7173 | script_get(exarg_T *eap, char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7174 | { |
| 7175 | char_u *theline; |
| 7176 | char *end_pattern = NULL; |
| 7177 | char dot[] = "."; |
| 7178 | garray_T ga; |
| 7179 | |
| 7180 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 7181 | return NULL; |
| 7182 | |
| 7183 | ga_init2(&ga, 1, 0x400); |
| 7184 | |
| 7185 | if (cmd[2] != NUL) |
| 7186 | end_pattern = (char *)skipwhite(cmd + 2); |
| 7187 | else |
| 7188 | end_pattern = dot; |
| 7189 | |
| 7190 | for (;;) |
| 7191 | { |
| 7192 | theline = eap->getline( |
| 7193 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7194 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7195 | #endif |
| 7196 | NUL, eap->cookie, 0); |
| 7197 | |
| 7198 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7199 | { |
| 7200 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7201 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7202 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7203 | |
| 7204 | ga_concat(&ga, theline); |
| 7205 | ga_append(&ga, '\n'); |
| 7206 | vim_free(theline); |
| 7207 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 7208 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7209 | |
| 7210 | return (char_u *)ga.ga_data; |
| 7211 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 7212 | |
| 7213 | #ifdef FEAT_SEARCH_EXTRA |
| 7214 | static void |
| 7215 | set_search_match(pos_T *t) |
| 7216 | { |
| 7217 | /* |
| 7218 | * First move cursor to end of match, then to the start. This |
| 7219 | * moves the whole match onto the screen when 'nowrap' is set. |
| 7220 | */ |
| 7221 | t->lnum += search_match_lines; |
| 7222 | t->col = search_match_endcol; |
| 7223 | if (t->lnum > curbuf->b_ml.ml_line_count) |
| 7224 | { |
| 7225 | t->lnum = curbuf->b_ml.ml_line_count; |
| 7226 | coladvance((colnr_T)MAXCOL); |
| 7227 | } |
| 7228 | } |
| 7229 | #endif |