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 | |
| 55 | #ifdef FEAT_CMDHIST |
| 56 | typedef struct hist_entry |
| 57 | { |
| 58 | int hisnum; /* identifying number */ |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 59 | int viminfo; /* when TRUE hisstr comes from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 60 | char_u *hisstr; /* actual entry, separator char after the NUL */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 61 | time_t time_set; /* when it was typed, zero if unknown */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 62 | } histentry_T; |
| 63 | |
| 64 | static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; |
| 65 | static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; /* lastused entry */ |
| 66 | static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 67 | /* identifying (unique) number of newest history entry */ |
| 68 | static int hislen = 0; /* actual length of history tables */ |
| 69 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 70 | static int hist_char2type(int c); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 71 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 72 | static int in_history(int, char_u *, int, int, int); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 73 | # ifdef FEAT_EVAL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 74 | static int calc_hist_idx(int histype, int num); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 75 | # endif |
| 76 | #endif |
| 77 | |
| 78 | #ifdef FEAT_RIGHTLEFT |
| 79 | static int cmd_hkmap = 0; /* Hebrew mapping during command line */ |
| 80 | #endif |
| 81 | |
| 82 | #ifdef FEAT_FKMAP |
| 83 | static int cmd_fkmap = 0; /* Farsi mapping during command line */ |
| 84 | #endif |
| 85 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 86 | static int cmdline_charsize(int idx); |
| 87 | static void set_cmdspos(void); |
| 88 | static void set_cmdspos_cursor(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 89 | #ifdef FEAT_MBYTE |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 90 | static void correct_cmdspos(int idx, int cells); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 91 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 92 | static void alloc_cmdbuff(int len); |
| 93 | static int realloc_cmdbuff(int len); |
| 94 | static void draw_cmdline(int start, int len); |
| 95 | static void save_cmdline(struct cmdline_info *ccp); |
| 96 | static void restore_cmdline(struct cmdline_info *ccp); |
| 97 | static int cmdline_paste(int regname, int literally, int remcr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 98 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 99 | static void redrawcmd_preedit(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 100 | #endif |
| 101 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 102 | static void cmdline_del(int from); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 103 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 104 | static void redrawcmdprompt(void); |
| 105 | static void cursorcmd(void); |
| 106 | static int ccheck_abbr(int); |
| 107 | static int nextwild(expand_T *xp, int type, int options, int escape); |
| 108 | static void escape_fname(char_u **pp); |
| 109 | static int showmatches(expand_T *xp, int wildmenu); |
| 110 | static void set_expand_context(expand_T *xp); |
| 111 | static int ExpandFromContext(expand_T *xp, char_u *, int *, char_u ***, int); |
| 112 | static int expand_showtail(expand_T *xp); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 113 | #ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 114 | 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] | 115 | 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] | 116 | static int ExpandPackAddDir(char_u *pat, int *num_file, char_u ***file); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 117 | # ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 118 | static char_u *get_history_arg(expand_T *xp, int idx); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 119 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 120 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 121 | static int ExpandUserDefined(expand_T *xp, regmatch_T *regmatch, int *num_file, char_u ***file); |
| 122 | static int ExpandUserList(expand_T *xp, int *num_file, char_u ***file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 123 | # endif |
| 124 | #endif |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 125 | #ifdef FEAT_CMDHIST |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 126 | static void clear_hist_entry(histentry_T *hisptr); |
Bram Moolenaar | 25a6df9 | 2013-04-06 14:29:00 +0200 | [diff] [blame] | 127 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 128 | |
| 129 | #ifdef FEAT_CMDWIN |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 130 | static int ex_window(void); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 131 | #endif |
| 132 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 133 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 134 | static int |
| 135 | #ifdef __BORLANDC__ |
| 136 | _RTLENTRYF |
| 137 | #endif |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 138 | sort_func_compare(const void *s1, const void *s2); |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 139 | #endif |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 140 | #ifdef FEAT_SEARCH_EXTRA |
| 141 | static void set_search_match(pos_T *t); |
| 142 | #endif |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 143 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 144 | /* |
| 145 | * getcmdline() - accept a command line starting with firstc. |
| 146 | * |
| 147 | * firstc == ':' get ":" command line. |
| 148 | * firstc == '/' or '?' get search pattern |
| 149 | * firstc == '=' get expression |
| 150 | * firstc == '@' get text for input() function |
| 151 | * firstc == '>' get text for debug mode |
| 152 | * firstc == NUL get text for :insert command |
| 153 | * firstc == -1 like NUL, and break on CTRL-C |
| 154 | * |
| 155 | * The line is collected in ccline.cmdbuff, which is reallocated to fit the |
| 156 | * command line. |
| 157 | * |
| 158 | * Careful: getcmdline() can be called recursively! |
| 159 | * |
| 160 | * Return pointer to allocated string if there is a commandline, NULL |
| 161 | * otherwise. |
| 162 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 163 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 164 | getcmdline( |
| 165 | int firstc, |
| 166 | long count UNUSED, /* only used for incremental search */ |
| 167 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 168 | { |
| 169 | int c; |
| 170 | int i; |
| 171 | int j; |
| 172 | int gotesc = FALSE; /* TRUE when <ESC> just typed */ |
| 173 | int do_abbr; /* when TRUE check for abbr. */ |
| 174 | #ifdef FEAT_CMDHIST |
| 175 | char_u *lookfor = NULL; /* string to match */ |
| 176 | int hiscnt; /* current history line in use */ |
| 177 | int histype; /* history type to be used */ |
| 178 | #endif |
| 179 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 180 | pos_T search_start; /* where 'incsearch' starts searching */ |
| 181 | pos_T save_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 182 | colnr_T old_curswant; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 183 | colnr_T init_curswant = curwin->w_curswant; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 184 | colnr_T old_leftcol; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 185 | colnr_T init_leftcol = curwin->w_leftcol; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 186 | linenr_T old_topline; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 187 | linenr_T init_topline = curwin->w_topline; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 188 | pos_T match_start = curwin->w_cursor; |
| 189 | pos_T match_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 190 | # ifdef FEAT_DIFF |
| 191 | int old_topfill; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 192 | int init_topfill = curwin->w_topfill; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 193 | # endif |
| 194 | linenr_T old_botline; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 195 | linenr_T init_botline = curwin->w_botline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 196 | int did_incsearch = FALSE; |
| 197 | int incsearch_postponed = FALSE; |
| 198 | #endif |
| 199 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 200 | int wim_index = 0; /* index in wim_flags[] */ |
| 201 | int res; |
| 202 | int save_msg_scroll = msg_scroll; |
| 203 | int save_State = State; /* remember State when called */ |
| 204 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 205 | #ifdef FEAT_MOUSE |
| 206 | /* mouse drag and release events are ignored, unless they are |
| 207 | * preceded with a mouse down event */ |
| 208 | int ignore_drag_release = TRUE; |
| 209 | #endif |
| 210 | #ifdef FEAT_EVAL |
| 211 | int break_ctrl_c = FALSE; |
| 212 | #endif |
| 213 | expand_T xpc; |
| 214 | long *b_im_ptr = NULL; |
Bram Moolenaar | 4d85051 | 2017-02-09 18:25:14 +0100 | [diff] [blame] | 215 | #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA) |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 216 | /* Everything that may work recursively should save and restore the |
| 217 | * current command line in save_ccline. That includes update_screen(), a |
| 218 | * custom status line may invoke ":normal". */ |
| 219 | struct cmdline_info save_ccline; |
| 220 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 221 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 222 | #ifdef FEAT_EVAL |
| 223 | if (firstc == -1) |
| 224 | { |
| 225 | firstc = NUL; |
| 226 | break_ctrl_c = TRUE; |
| 227 | } |
| 228 | #endif |
| 229 | #ifdef FEAT_RIGHTLEFT |
| 230 | /* start without Hebrew mapping for a command line */ |
| 231 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 232 | cmd_hkmap = 0; |
| 233 | #endif |
| 234 | |
| 235 | ccline.overstrike = FALSE; /* always start in insert mode */ |
| 236 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 237 | CLEAR_POS(&match_end); |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 238 | save_cursor = curwin->w_cursor; /* may be restored later */ |
| 239 | search_start = curwin->w_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 240 | old_curswant = curwin->w_curswant; |
| 241 | old_leftcol = curwin->w_leftcol; |
| 242 | old_topline = curwin->w_topline; |
| 243 | # ifdef FEAT_DIFF |
| 244 | old_topfill = curwin->w_topfill; |
| 245 | # endif |
| 246 | old_botline = curwin->w_botline; |
| 247 | #endif |
| 248 | |
| 249 | /* |
| 250 | * set some variables for redrawcmd() |
| 251 | */ |
| 252 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 253 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 254 | |
| 255 | /* alloc initial ccline.cmdbuff */ |
| 256 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 257 | if (ccline.cmdbuff == NULL) |
| 258 | return NULL; /* out of memory */ |
| 259 | ccline.cmdlen = ccline.cmdpos = 0; |
| 260 | ccline.cmdbuff[0] = NUL; |
| 261 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 262 | /* autoindent for :insert and :append */ |
| 263 | if (firstc <= 0) |
| 264 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 265 | vim_memset(ccline.cmdbuff, ' ', indent); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 266 | ccline.cmdbuff[indent] = NUL; |
| 267 | ccline.cmdpos = indent; |
| 268 | ccline.cmdspos = indent; |
| 269 | ccline.cmdlen = indent; |
| 270 | } |
| 271 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 272 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 273 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 274 | |
| 275 | #ifdef FEAT_RIGHTLEFT |
| 276 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 277 | && (firstc == '/' || firstc == '?')) |
| 278 | cmdmsg_rl = TRUE; |
| 279 | else |
| 280 | cmdmsg_rl = FALSE; |
| 281 | #endif |
| 282 | |
| 283 | redir_off = TRUE; /* don't redirect the typed command */ |
| 284 | if (!cmd_silent) |
| 285 | { |
| 286 | i = msg_scrolled; |
| 287 | msg_scrolled = 0; /* avoid wait_return message */ |
| 288 | gotocmdline(TRUE); |
| 289 | msg_scrolled += i; |
| 290 | redrawcmdprompt(); /* draw prompt or indent */ |
| 291 | set_cmdspos(); |
| 292 | } |
| 293 | xpc.xp_context = EXPAND_NOTHING; |
| 294 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 295 | #ifndef BACKSLASH_IN_FILENAME |
| 296 | xpc.xp_shell = FALSE; |
| 297 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 298 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 299 | #if defined(FEAT_EVAL) |
| 300 | if (ccline.input_fn) |
| 301 | { |
| 302 | xpc.xp_context = ccline.xp_context; |
| 303 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 304 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 305 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 306 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 307 | } |
| 308 | #endif |
| 309 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 310 | /* |
| 311 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 312 | * doing ":@0" when register 0 doesn't contain a CR. |
| 313 | */ |
| 314 | msg_scroll = FALSE; |
| 315 | |
| 316 | State = CMDLINE; |
| 317 | |
| 318 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 319 | { |
| 320 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 321 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 322 | b_im_ptr = &curbuf->b_p_iminsert; |
| 323 | else |
| 324 | b_im_ptr = &curbuf->b_p_imsearch; |
| 325 | if (*b_im_ptr == B_IMODE_LMAP) |
| 326 | State |= LANGMAP; |
| 327 | #ifdef USE_IM_CONTROL |
| 328 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 329 | #endif |
| 330 | } |
| 331 | #ifdef USE_IM_CONTROL |
| 332 | else if (p_imcmdline) |
| 333 | im_set_active(TRUE); |
| 334 | #endif |
| 335 | |
| 336 | #ifdef FEAT_MOUSE |
| 337 | setmouse(); |
| 338 | #endif |
| 339 | #ifdef CURSOR_SHAPE |
| 340 | ui_cursor_shape(); /* may show different cursor shape */ |
| 341 | #endif |
| 342 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 343 | /* When inside an autocommand for writing "exiting" may be set and |
| 344 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 345 | settmode(TMODE_RAW); |
| 346 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 347 | #ifdef FEAT_CMDHIST |
| 348 | init_history(); |
| 349 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 350 | histype = hist_char2type(firstc); |
| 351 | #endif |
| 352 | |
| 353 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 354 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 355 | #endif |
| 356 | |
Bram Moolenaar | 15a35c4 | 2014-06-25 12:26:46 +0200 | [diff] [blame] | 357 | /* If something above caused an error, reset the flags, we do want to type |
| 358 | * and execute commands. Display may be messed up a bit. */ |
| 359 | if (did_emsg) |
| 360 | redrawcmd(); |
| 361 | did_emsg = FALSE; |
| 362 | got_int = FALSE; |
| 363 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 364 | /* |
| 365 | * Collect the command string, handling editing keys. |
| 366 | */ |
| 367 | for (;;) |
| 368 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 369 | redir_off = TRUE; /* Don't redirect the typed command. |
| 370 | Repeated, because a ":redir" inside |
| 371 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 372 | #ifdef USE_ON_FLY_SCROLL |
| 373 | dont_scroll = FALSE; /* allow scrolling here */ |
| 374 | #endif |
| 375 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 376 | |
| 377 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 378 | |
| 379 | /* Get a character. Ignore K_IGNORE, it should not do anything, such |
| 380 | * as stop completion. */ |
| 381 | do |
| 382 | { |
| 383 | c = safe_vgetc(); |
| 384 | } while (c == K_IGNORE); |
| 385 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 386 | if (KeyTyped) |
| 387 | { |
| 388 | some_key_typed = TRUE; |
| 389 | #ifdef FEAT_RIGHTLEFT |
| 390 | if (cmd_hkmap) |
| 391 | c = hkmap(c); |
| 392 | # ifdef FEAT_FKMAP |
| 393 | if (cmd_fkmap) |
| 394 | c = cmdl_fkmap(c); |
| 395 | # endif |
| 396 | if (cmdmsg_rl && !KeyStuffed) |
| 397 | { |
| 398 | /* Invert horizontal movements and operations. Only when |
| 399 | * typed by the user directly, not when the result of a |
| 400 | * mapping. */ |
| 401 | switch (c) |
| 402 | { |
| 403 | case K_RIGHT: c = K_LEFT; break; |
| 404 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 405 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 406 | case K_LEFT: c = K_RIGHT; break; |
| 407 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 408 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 409 | } |
| 410 | } |
| 411 | #endif |
| 412 | } |
| 413 | |
| 414 | /* |
| 415 | * Ignore got_int when CTRL-C was typed here. |
| 416 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 417 | * ":g/pat/normal /pat" (without the <CR>). |
| 418 | * Don't ignore it for the input() function. |
| 419 | */ |
| 420 | if ((c == Ctrl_C |
| 421 | #ifdef UNIX |
| 422 | || c == intr_char |
| 423 | #endif |
| 424 | ) |
| 425 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 426 | && firstc != '@' |
| 427 | #endif |
| 428 | #ifdef FEAT_EVAL |
| 429 | && !break_ctrl_c |
| 430 | #endif |
| 431 | && !global_busy) |
| 432 | got_int = FALSE; |
| 433 | |
| 434 | #ifdef FEAT_CMDHIST |
| 435 | /* free old command line when finished moving around in the history |
| 436 | * list */ |
| 437 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 438 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 439 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 440 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 441 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 442 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 443 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
| 444 | { |
| 445 | vim_free(lookfor); |
| 446 | lookfor = NULL; |
| 447 | } |
| 448 | #endif |
| 449 | |
| 450 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 451 | * When there are matching completions to select <S-Tab> works like |
| 452 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 453 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 454 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 455 | c = Ctrl_P; |
| 456 | |
| 457 | #ifdef FEAT_WILDMENU |
| 458 | /* Special translations for 'wildmenu' */ |
| 459 | if (did_wild_list && p_wmnu) |
| 460 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 461 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 462 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 463 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 464 | c = Ctrl_N; |
| 465 | } |
| 466 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 467 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 468 | && ccline.cmdpos > 1 |
| 469 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 470 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 471 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 472 | c = K_DOWN; |
| 473 | #endif |
| 474 | |
| 475 | /* free expanded names when finished walking through matches */ |
| 476 | if (xpc.xp_numfiles != -1 |
| 477 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 478 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 479 | && c != Ctrl_L) |
| 480 | { |
| 481 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 482 | did_wild_list = FALSE; |
| 483 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 484 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 485 | #endif |
| 486 | xpc.xp_context = EXPAND_NOTHING; |
| 487 | wim_index = 0; |
| 488 | #ifdef FEAT_WILDMENU |
| 489 | if (p_wmnu && wild_menu_showing != 0) |
| 490 | { |
| 491 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 492 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 493 | |
| 494 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 495 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 496 | |
| 497 | if (wild_menu_showing == WM_SCROLLED) |
| 498 | { |
| 499 | /* Entered command line, move it up */ |
| 500 | cmdline_row--; |
| 501 | redrawcmd(); |
| 502 | } |
| 503 | else if (save_p_ls != -1) |
| 504 | { |
| 505 | /* restore 'laststatus' and 'winminheight' */ |
| 506 | p_ls = save_p_ls; |
| 507 | p_wmh = save_p_wmh; |
| 508 | last_status(FALSE); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 509 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 510 | update_screen(VALID); /* redraw the screen NOW */ |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 511 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 512 | redrawcmd(); |
| 513 | save_p_ls = -1; |
| 514 | } |
| 515 | else |
| 516 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 517 | win_redraw_last_status(topframe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 518 | redraw_statuslines(); |
| 519 | } |
| 520 | KeyTyped = skt; |
| 521 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 522 | if (ccline.input_fn) |
| 523 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 524 | } |
| 525 | #endif |
| 526 | } |
| 527 | |
| 528 | #ifdef FEAT_WILDMENU |
| 529 | /* Special translations for 'wildmenu' */ |
| 530 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 531 | { |
| 532 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 533 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 534 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 535 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 536 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 537 | { |
| 538 | /* Hitting <Up>: Remove one submenu name in front of the |
| 539 | * cursor */ |
| 540 | int found = FALSE; |
| 541 | |
| 542 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 543 | i = 0; |
| 544 | while (--j > 0) |
| 545 | { |
| 546 | /* check for start of menu name */ |
| 547 | if (ccline.cmdbuff[j] == ' ' |
| 548 | && ccline.cmdbuff[j - 1] != '\\') |
| 549 | { |
| 550 | i = j + 1; |
| 551 | break; |
| 552 | } |
| 553 | /* check for start of submenu name */ |
| 554 | if (ccline.cmdbuff[j] == '.' |
| 555 | && ccline.cmdbuff[j - 1] != '\\') |
| 556 | { |
| 557 | if (found) |
| 558 | { |
| 559 | i = j + 1; |
| 560 | break; |
| 561 | } |
| 562 | else |
| 563 | found = TRUE; |
| 564 | } |
| 565 | } |
| 566 | if (i > 0) |
| 567 | cmdline_del(i); |
| 568 | c = p_wc; |
| 569 | xpc.xp_context = EXPAND_NOTHING; |
| 570 | } |
| 571 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 572 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 573 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 574 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 575 | { |
| 576 | char_u upseg[5]; |
| 577 | |
| 578 | upseg[0] = PATHSEP; |
| 579 | upseg[1] = '.'; |
| 580 | upseg[2] = '.'; |
| 581 | upseg[3] = PATHSEP; |
| 582 | upseg[4] = NUL; |
| 583 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 584 | if (c == K_DOWN |
| 585 | && ccline.cmdpos > 0 |
| 586 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 587 | && (ccline.cmdpos < 3 |
| 588 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 589 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 590 | { |
| 591 | /* go down a directory */ |
| 592 | c = p_wc; |
| 593 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 594 | 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] | 595 | { |
| 596 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 597 | int found = FALSE; |
| 598 | |
| 599 | j = ccline.cmdpos; |
| 600 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 601 | while (--j > i) |
| 602 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 603 | #ifdef FEAT_MBYTE |
| 604 | if (has_mbyte) |
| 605 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 606 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 607 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 608 | { |
| 609 | found = TRUE; |
| 610 | break; |
| 611 | } |
| 612 | } |
| 613 | if (found |
| 614 | && ccline.cmdbuff[j - 1] == '.' |
| 615 | && ccline.cmdbuff[j - 2] == '.' |
| 616 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 617 | { |
| 618 | cmdline_del(j - 2); |
| 619 | c = p_wc; |
| 620 | } |
| 621 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 622 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 623 | { |
| 624 | /* go up a directory */ |
| 625 | int found = FALSE; |
| 626 | |
| 627 | j = ccline.cmdpos - 1; |
| 628 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 629 | while (--j > i) |
| 630 | { |
| 631 | #ifdef FEAT_MBYTE |
| 632 | if (has_mbyte) |
| 633 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 634 | #endif |
| 635 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 636 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 637 | && vim_strchr((char_u *)" *?[{`$%#", |
| 638 | ccline.cmdbuff[j + 1]) == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 639 | #endif |
| 640 | ) |
| 641 | { |
| 642 | if (found) |
| 643 | { |
| 644 | i = j + 1; |
| 645 | break; |
| 646 | } |
| 647 | else |
| 648 | found = TRUE; |
| 649 | } |
| 650 | } |
| 651 | |
| 652 | if (!found) |
| 653 | j = i; |
| 654 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 655 | j += 4; |
| 656 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 657 | && j == i) |
| 658 | j += 3; |
| 659 | else |
| 660 | j = 0; |
| 661 | if (j > 0) |
| 662 | { |
| 663 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 664 | * machine-specific stuff here and in upseg init */ |
| 665 | cmdline_del(j); |
| 666 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 667 | } |
| 668 | else if (ccline.cmdpos > i) |
| 669 | cmdline_del(i); |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 670 | |
| 671 | /* Now complete in the new directory. Set KeyTyped in case the |
| 672 | * Up key came from a mapping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 673 | c = p_wc; |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 674 | KeyTyped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 675 | } |
| 676 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 677 | |
| 678 | #endif /* FEAT_WILDMENU */ |
| 679 | |
| 680 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 681 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 682 | if (c == Ctrl_BSL) |
| 683 | { |
| 684 | ++no_mapping; |
| 685 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 686 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 687 | --no_mapping; |
| 688 | --allow_keys; |
Bram Moolenaar | b735681 | 2012-10-11 04:04:37 +0200 | [diff] [blame] | 689 | /* CTRL-\ e doesn't work when obtaining an expression, unless it |
| 690 | * is in a mapping. */ |
| 691 | if (c != Ctrl_N && c != Ctrl_G && (c != 'e' |
| 692 | || (ccline.cmdfirstc == '=' && KeyTyped))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 693 | { |
| 694 | vungetc(c); |
| 695 | c = Ctrl_BSL; |
| 696 | } |
| 697 | #ifdef FEAT_EVAL |
| 698 | else if (c == 'e') |
| 699 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 700 | char_u *p = NULL; |
| 701 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 702 | |
| 703 | /* |
| 704 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 705 | * Need to save and restore the current command line, to be |
| 706 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 707 | */ |
| 708 | if (ccline.cmdpos == ccline.cmdlen) |
| 709 | new_cmdpos = 99999; /* keep it at the end */ |
| 710 | else |
| 711 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 712 | |
| 713 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 714 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 715 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 716 | if (c == '=') |
| 717 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 718 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 719 | * to avoid nasty things like going to another buffer when |
| 720 | * evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 721 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 722 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 723 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 724 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 725 | restore_cmdline(&save_ccline); |
| 726 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 727 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 728 | { |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 729 | len = (int)STRLEN(p); |
| 730 | if (realloc_cmdbuff(len + 1) == OK) |
| 731 | { |
| 732 | ccline.cmdlen = len; |
| 733 | STRCPY(ccline.cmdbuff, p); |
| 734 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 735 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 736 | /* Restore the cursor or use the position set with |
| 737 | * set_cmdline_pos(). */ |
| 738 | if (new_cmdpos > ccline.cmdlen) |
| 739 | ccline.cmdpos = ccline.cmdlen; |
| 740 | else |
| 741 | ccline.cmdpos = new_cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 742 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 743 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 744 | redrawcmd(); |
| 745 | goto cmdline_changed; |
| 746 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 747 | } |
| 748 | } |
| 749 | beep_flush(); |
Bram Moolenaar | 66b4bf8 | 2010-11-16 14:06:08 +0100 | [diff] [blame] | 750 | got_int = FALSE; /* don't abandon the command line */ |
| 751 | did_emsg = FALSE; |
| 752 | emsg_on_display = FALSE; |
| 753 | redrawcmd(); |
| 754 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 755 | } |
| 756 | #endif |
| 757 | else |
| 758 | { |
| 759 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 760 | restart_edit = 'a'; |
| 761 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 762 | in history */ |
| 763 | goto returncmd; /* back to Normal mode */ |
| 764 | } |
| 765 | } |
| 766 | |
| 767 | #ifdef FEAT_CMDWIN |
| 768 | if (c == cedit_key || c == K_CMDWIN) |
| 769 | { |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 770 | if (ex_normal_busy == 0 && got_int == FALSE) |
| 771 | { |
| 772 | /* |
| 773 | * Open a window to edit the command line (and history). |
| 774 | */ |
| 775 | c = ex_window(); |
| 776 | some_key_typed = TRUE; |
| 777 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 778 | } |
| 779 | # ifdef FEAT_DIGRAPHS |
| 780 | else |
| 781 | # endif |
| 782 | #endif |
| 783 | #ifdef FEAT_DIGRAPHS |
| 784 | c = do_digraph(c); |
| 785 | #endif |
| 786 | |
| 787 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 788 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 789 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 790 | /* In Ex mode a backslash escapes a newline. */ |
| 791 | if (exmode_active |
| 792 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 793 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 794 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 795 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 796 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 797 | if (c == K_KENTER) |
| 798 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 799 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 800 | else |
| 801 | { |
| 802 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 803 | truncate the cmdline now. */ |
| 804 | if (ccheck_abbr(c + ABBR_OFF)) |
| 805 | goto cmdline_changed; |
| 806 | if (!cmd_silent) |
| 807 | { |
| 808 | windgoto(msg_row, 0); |
| 809 | out_flush(); |
| 810 | } |
| 811 | break; |
| 812 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 813 | } |
| 814 | |
| 815 | /* |
| 816 | * Completion for 'wildchar' or 'wildcharm' key. |
| 817 | * - hitting <ESC> twice means: abandon command line. |
| 818 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 819 | * typed, not when it comes from a macro |
| 820 | */ |
| 821 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 822 | { |
| 823 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 824 | { |
| 825 | /* if 'wildmode' contains "list" may still need to list */ |
| 826 | if (xpc.xp_numfiles > 1 |
| 827 | && !did_wild_list |
| 828 | && (wim_flags[wim_index] & WIM_LIST)) |
| 829 | { |
| 830 | (void)showmatches(&xpc, FALSE); |
| 831 | redrawcmd(); |
| 832 | did_wild_list = TRUE; |
| 833 | } |
| 834 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 835 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 836 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 837 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 838 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 839 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 840 | else |
| 841 | res = OK; /* don't insert 'wildchar' now */ |
| 842 | } |
| 843 | else /* typed p_wc first time */ |
| 844 | { |
| 845 | wim_index = 0; |
| 846 | j = ccline.cmdpos; |
| 847 | /* if 'wildmode' first contains "longest", get longest |
| 848 | * common part */ |
| 849 | if (wim_flags[0] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 850 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 851 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 852 | else |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 853 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP, |
| 854 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 855 | |
| 856 | /* if interrupted while completing, behave like it failed */ |
| 857 | if (got_int) |
| 858 | { |
| 859 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 860 | got_int = FALSE; /* don't abandon the command line */ |
| 861 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 862 | #ifdef FEAT_WILDMENU |
| 863 | xpc.xp_context = EXPAND_NOTHING; |
| 864 | #endif |
| 865 | goto cmdline_changed; |
| 866 | } |
| 867 | |
| 868 | /* when more than one match, and 'wildmode' first contains |
| 869 | * "list", or no change and 'wildmode' contains "longest,list", |
| 870 | * list all matches */ |
| 871 | if (res == OK && xpc.xp_numfiles > 1) |
| 872 | { |
| 873 | /* a "longest" that didn't do anything is skipped (but not |
| 874 | * "list:longest") */ |
| 875 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 876 | wim_index = 1; |
| 877 | if ((wim_flags[wim_index] & WIM_LIST) |
| 878 | #ifdef FEAT_WILDMENU |
| 879 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 880 | #endif |
| 881 | ) |
| 882 | { |
| 883 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 884 | { |
| 885 | #ifdef FEAT_WILDMENU |
| 886 | int p_wmnu_save = p_wmnu; |
| 887 | p_wmnu = 0; |
| 888 | #endif |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 889 | /* remove match */ |
| 890 | nextwild(&xpc, WILD_PREV, 0, firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 891 | #ifdef FEAT_WILDMENU |
| 892 | p_wmnu = p_wmnu_save; |
| 893 | #endif |
| 894 | } |
| 895 | #ifdef FEAT_WILDMENU |
| 896 | (void)showmatches(&xpc, p_wmnu |
| 897 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 898 | #else |
| 899 | (void)showmatches(&xpc, FALSE); |
| 900 | #endif |
| 901 | redrawcmd(); |
| 902 | did_wild_list = TRUE; |
| 903 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 904 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 905 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 906 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 907 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 908 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 909 | } |
| 910 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 911 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 912 | } |
| 913 | #ifdef FEAT_WILDMENU |
| 914 | else if (xpc.xp_numfiles == -1) |
| 915 | xpc.xp_context = EXPAND_NOTHING; |
| 916 | #endif |
| 917 | } |
| 918 | if (wim_index < 3) |
| 919 | ++wim_index; |
| 920 | if (c == ESC) |
| 921 | gotesc = TRUE; |
| 922 | if (res == OK) |
| 923 | goto cmdline_changed; |
| 924 | } |
| 925 | |
| 926 | gotesc = FALSE; |
| 927 | |
| 928 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 929 | if (c == K_S_TAB && KeyTyped) |
| 930 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 931 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK |
| 932 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK |
| 933 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 934 | goto cmdline_changed; |
| 935 | } |
| 936 | |
| 937 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 938 | c = NL; |
| 939 | |
| 940 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 941 | |
| 942 | /* |
| 943 | * Big switch for a typed command line character. |
| 944 | */ |
| 945 | switch (c) |
| 946 | { |
| 947 | case K_BS: |
| 948 | case Ctrl_H: |
| 949 | case K_DEL: |
| 950 | case K_KDEL: |
| 951 | case Ctrl_W: |
| 952 | #ifdef FEAT_FKMAP |
| 953 | if (cmd_fkmap && c == K_BS) |
| 954 | c = K_DEL; |
| 955 | #endif |
| 956 | if (c == K_KDEL) |
| 957 | c = K_DEL; |
| 958 | |
| 959 | /* |
| 960 | * delete current character is the same as backspace on next |
| 961 | * character, except at end of line |
| 962 | */ |
| 963 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 964 | ++ccline.cmdpos; |
| 965 | #ifdef FEAT_MBYTE |
| 966 | if (has_mbyte && c == K_DEL) |
| 967 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 968 | ccline.cmdbuff + ccline.cmdpos); |
| 969 | #endif |
| 970 | if (ccline.cmdpos > 0) |
| 971 | { |
| 972 | char_u *p; |
| 973 | |
| 974 | j = ccline.cmdpos; |
| 975 | p = ccline.cmdbuff + j; |
| 976 | #ifdef FEAT_MBYTE |
| 977 | if (has_mbyte) |
| 978 | { |
| 979 | p = mb_prevptr(ccline.cmdbuff, p); |
| 980 | if (c == Ctrl_W) |
| 981 | { |
| 982 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 983 | p = mb_prevptr(ccline.cmdbuff, p); |
| 984 | i = mb_get_class(p); |
| 985 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 986 | p = mb_prevptr(ccline.cmdbuff, p); |
| 987 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 988 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 989 | } |
| 990 | } |
| 991 | else |
| 992 | #endif |
| 993 | if (c == Ctrl_W) |
| 994 | { |
| 995 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 996 | --p; |
| 997 | i = vim_iswordc(p[-1]); |
| 998 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 999 | && vim_iswordc(p[-1]) == i) |
| 1000 | --p; |
| 1001 | } |
| 1002 | else |
| 1003 | --p; |
| 1004 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 1005 | ccline.cmdlen -= j - ccline.cmdpos; |
| 1006 | i = ccline.cmdpos; |
| 1007 | while (i < ccline.cmdlen) |
| 1008 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1009 | |
| 1010 | /* Truncate at the end, required for multi-byte chars. */ |
| 1011 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1012 | #ifdef FEAT_SEARCH_EXTRA |
| 1013 | if (ccline.cmdlen == 0) |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1014 | { |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1015 | search_start = save_cursor; |
| 1016 | /* save view settings, so that the screen |
| 1017 | * won't be restored at the wrong position */ |
| 1018 | old_curswant = init_curswant; |
| 1019 | old_leftcol = init_leftcol; |
| 1020 | old_topline = init_topline; |
| 1021 | # ifdef FEAT_DIFF |
| 1022 | old_topfill = init_topfill; |
| 1023 | # endif |
| 1024 | old_botline = init_botline; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1025 | } |
| 1026 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1027 | redrawcmd(); |
| 1028 | } |
| 1029 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 1030 | && ccline.cmdprompt == NULL && indent == 0) |
| 1031 | { |
| 1032 | /* In ex and debug mode it doesn't make sense to return. */ |
| 1033 | if (exmode_active |
| 1034 | #ifdef FEAT_EVAL |
| 1035 | || ccline.cmdfirstc == '>' |
| 1036 | #endif |
| 1037 | ) |
| 1038 | goto cmdline_not_changed; |
| 1039 | |
| 1040 | vim_free(ccline.cmdbuff); /* no commandline to return */ |
| 1041 | ccline.cmdbuff = NULL; |
| 1042 | if (!cmd_silent) |
| 1043 | { |
| 1044 | #ifdef FEAT_RIGHTLEFT |
| 1045 | if (cmdmsg_rl) |
| 1046 | msg_col = Columns; |
| 1047 | else |
| 1048 | #endif |
| 1049 | msg_col = 0; |
| 1050 | msg_putchar(' '); /* delete ':' */ |
| 1051 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1052 | #ifdef FEAT_SEARCH_EXTRA |
| 1053 | if (ccline.cmdlen == 0) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1054 | search_start = save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1055 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1056 | redraw_cmdline = TRUE; |
| 1057 | goto returncmd; /* back to cmd mode */ |
| 1058 | } |
| 1059 | goto cmdline_changed; |
| 1060 | |
| 1061 | case K_INS: |
| 1062 | case K_KINS: |
| 1063 | #ifdef FEAT_FKMAP |
| 1064 | /* if Farsi mode set, we are in reverse insert mode - |
| 1065 | Do not change the mode */ |
| 1066 | if (cmd_fkmap) |
| 1067 | beep_flush(); |
| 1068 | else |
| 1069 | #endif |
| 1070 | ccline.overstrike = !ccline.overstrike; |
| 1071 | #ifdef CURSOR_SHAPE |
| 1072 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1073 | #endif |
| 1074 | goto cmdline_not_changed; |
| 1075 | |
| 1076 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1077 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1078 | { |
| 1079 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1080 | State ^= LANGMAP; |
| 1081 | #ifdef USE_IM_CONTROL |
| 1082 | im_set_active(FALSE); /* Disable input method */ |
| 1083 | #endif |
| 1084 | if (b_im_ptr != NULL) |
| 1085 | { |
| 1086 | if (State & LANGMAP) |
| 1087 | *b_im_ptr = B_IMODE_LMAP; |
| 1088 | else |
| 1089 | *b_im_ptr = B_IMODE_NONE; |
| 1090 | } |
| 1091 | } |
| 1092 | #ifdef USE_IM_CONTROL |
| 1093 | else |
| 1094 | { |
| 1095 | /* There are no ":lmap" mappings, toggle IM. When |
| 1096 | * 'imdisable' is set don't try getting the status, it's |
| 1097 | * always off. */ |
| 1098 | if ((p_imdisable && b_im_ptr != NULL) |
| 1099 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1100 | { |
| 1101 | im_set_active(FALSE); /* Disable input method */ |
| 1102 | if (b_im_ptr != NULL) |
| 1103 | *b_im_ptr = B_IMODE_NONE; |
| 1104 | } |
| 1105 | else |
| 1106 | { |
| 1107 | im_set_active(TRUE); /* Enable input method */ |
| 1108 | if (b_im_ptr != NULL) |
| 1109 | *b_im_ptr = B_IMODE_IM; |
| 1110 | } |
| 1111 | } |
| 1112 | #endif |
| 1113 | if (b_im_ptr != NULL) |
| 1114 | { |
| 1115 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1116 | set_iminsert_global(); |
| 1117 | else |
| 1118 | set_imsearch_global(); |
| 1119 | } |
| 1120 | #ifdef CURSOR_SHAPE |
| 1121 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1122 | #endif |
| 1123 | #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) |
| 1124 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1125 | status_redraw_curbuf(); |
| 1126 | #endif |
| 1127 | goto cmdline_not_changed; |
| 1128 | |
| 1129 | /* case '@': only in very old vi */ |
| 1130 | case Ctrl_U: |
| 1131 | /* delete all characters left of the cursor */ |
| 1132 | j = ccline.cmdpos; |
| 1133 | ccline.cmdlen -= j; |
| 1134 | i = ccline.cmdpos = 0; |
| 1135 | while (i < ccline.cmdlen) |
| 1136 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1137 | /* Truncate at the end, required for multi-byte chars. */ |
| 1138 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1139 | #ifdef FEAT_SEARCH_EXTRA |
| 1140 | if (ccline.cmdlen == 0) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1141 | search_start = save_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1142 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1143 | redrawcmd(); |
| 1144 | goto cmdline_changed; |
| 1145 | |
| 1146 | #ifdef FEAT_CLIPBOARD |
| 1147 | case Ctrl_Y: |
| 1148 | /* Copy the modeless selection, if there is one. */ |
| 1149 | if (clip_star.state != SELECT_CLEARED) |
| 1150 | { |
| 1151 | if (clip_star.state == SELECT_DONE) |
| 1152 | clip_copy_modeless_selection(TRUE); |
| 1153 | goto cmdline_not_changed; |
| 1154 | } |
| 1155 | break; |
| 1156 | #endif |
| 1157 | |
| 1158 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1159 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1160 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1161 | * ":normal" runs out of characters. */ |
| 1162 | if (exmode_active |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 1163 | && (ex_normal_busy == 0 || typebuf.tb_len > 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1164 | goto cmdline_not_changed; |
| 1165 | |
| 1166 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1167 | putting it in history */ |
| 1168 | goto returncmd; /* back to cmd mode */ |
| 1169 | |
| 1170 | case Ctrl_R: /* insert register */ |
| 1171 | #ifdef USE_ON_FLY_SCROLL |
| 1172 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1173 | #endif |
| 1174 | putcmdline('"', TRUE); |
| 1175 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1176 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1177 | if (i == Ctrl_O) |
| 1178 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1179 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1180 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1181 | --no_mapping; |
| 1182 | #ifdef FEAT_EVAL |
| 1183 | /* |
| 1184 | * Insert the result of an expression. |
| 1185 | * Need to save the current command line, to be able to enter |
| 1186 | * a new one... |
| 1187 | */ |
| 1188 | new_cmdpos = -1; |
| 1189 | if (c == '=') |
| 1190 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1191 | if (ccline.cmdfirstc == '=')/* can't do this recursively */ |
| 1192 | { |
| 1193 | beep_flush(); |
| 1194 | c = ESC; |
| 1195 | } |
| 1196 | else |
| 1197 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1198 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1199 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1200 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1201 | } |
| 1202 | } |
| 1203 | #endif |
| 1204 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1205 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1206 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1207 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1208 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1209 | /* When there was a serious error abort getting the |
| 1210 | * command line. */ |
| 1211 | if (aborting()) |
| 1212 | { |
| 1213 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1214 | putting it in history */ |
| 1215 | goto returncmd; /* back to cmd mode */ |
| 1216 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1217 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1218 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1219 | #ifdef FEAT_EVAL |
| 1220 | if (new_cmdpos >= 0) |
| 1221 | { |
| 1222 | /* set_cmdline_pos() was used */ |
| 1223 | if (new_cmdpos > ccline.cmdlen) |
| 1224 | ccline.cmdpos = ccline.cmdlen; |
| 1225 | else |
| 1226 | ccline.cmdpos = new_cmdpos; |
| 1227 | } |
| 1228 | #endif |
| 1229 | } |
| 1230 | redrawcmd(); |
| 1231 | goto cmdline_changed; |
| 1232 | |
| 1233 | case Ctrl_D: |
| 1234 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1235 | break; /* Use ^D as normal char instead */ |
| 1236 | |
| 1237 | redrawcmd(); |
| 1238 | continue; /* don't do incremental search now */ |
| 1239 | |
| 1240 | case K_RIGHT: |
| 1241 | case K_S_RIGHT: |
| 1242 | case K_C_RIGHT: |
| 1243 | do |
| 1244 | { |
| 1245 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1246 | break; |
| 1247 | i = cmdline_charsize(ccline.cmdpos); |
| 1248 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1249 | break; |
| 1250 | ccline.cmdspos += i; |
| 1251 | #ifdef FEAT_MBYTE |
| 1252 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1253 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1254 | + ccline.cmdpos); |
| 1255 | else |
| 1256 | #endif |
| 1257 | ++ccline.cmdpos; |
| 1258 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1259 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 1260 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1261 | && ccline.cmdbuff[ccline.cmdpos] != ' '); |
| 1262 | #ifdef FEAT_MBYTE |
| 1263 | if (has_mbyte) |
| 1264 | set_cmdspos_cursor(); |
| 1265 | #endif |
| 1266 | goto cmdline_not_changed; |
| 1267 | |
| 1268 | case K_LEFT: |
| 1269 | case K_S_LEFT: |
| 1270 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1271 | if (ccline.cmdpos == 0) |
| 1272 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1273 | do |
| 1274 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1275 | --ccline.cmdpos; |
| 1276 | #ifdef FEAT_MBYTE |
| 1277 | if (has_mbyte) /* move to first byte of char */ |
| 1278 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1279 | ccline.cmdbuff + ccline.cmdpos); |
| 1280 | #endif |
| 1281 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1282 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1283 | while (ccline.cmdpos > 0 |
| 1284 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1285 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1286 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1287 | #ifdef FEAT_MBYTE |
| 1288 | if (has_mbyte) |
| 1289 | set_cmdspos_cursor(); |
| 1290 | #endif |
| 1291 | goto cmdline_not_changed; |
| 1292 | |
| 1293 | case K_IGNORE: |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1294 | /* Ignore mouse event or ex_window() result. */ |
| 1295 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1296 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1297 | #ifdef FEAT_GUI_W32 |
| 1298 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1299 | * cancelled. */ |
| 1300 | case K_F4: |
| 1301 | if (mod_mask == MOD_MASK_ALT) |
| 1302 | { |
| 1303 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1304 | goto cmdline_not_changed; |
| 1305 | } |
| 1306 | break; |
| 1307 | #endif |
| 1308 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1309 | #ifdef FEAT_MOUSE |
| 1310 | case K_MIDDLEDRAG: |
| 1311 | case K_MIDDLERELEASE: |
| 1312 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1313 | |
| 1314 | case K_MIDDLEMOUSE: |
| 1315 | # ifdef FEAT_GUI |
| 1316 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1317 | if (!gui.in_use) |
| 1318 | # endif |
| 1319 | if (!mouse_has(MOUSE_COMMAND)) |
| 1320 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1321 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1322 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1323 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1324 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1325 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1326 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1327 | redrawcmd(); |
| 1328 | goto cmdline_changed; |
| 1329 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1330 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1331 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1332 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1333 | redrawcmd(); |
| 1334 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1335 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1336 | |
| 1337 | case K_LEFTDRAG: |
| 1338 | case K_LEFTRELEASE: |
| 1339 | case K_RIGHTDRAG: |
| 1340 | case K_RIGHTRELEASE: |
| 1341 | /* Ignore drag and release events when the button-down wasn't |
| 1342 | * seen before. */ |
| 1343 | if (ignore_drag_release) |
| 1344 | goto cmdline_not_changed; |
| 1345 | /* FALLTHROUGH */ |
| 1346 | case K_LEFTMOUSE: |
| 1347 | case K_RIGHTMOUSE: |
| 1348 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1349 | ignore_drag_release = TRUE; |
| 1350 | else |
| 1351 | ignore_drag_release = FALSE; |
| 1352 | # ifdef FEAT_GUI |
| 1353 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1354 | if (!gui.in_use) |
| 1355 | # endif |
| 1356 | if (!mouse_has(MOUSE_COMMAND)) |
| 1357 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1358 | # ifdef FEAT_CLIPBOARD |
| 1359 | if (mouse_row < cmdline_row && clip_star.available) |
| 1360 | { |
| 1361 | int button, is_click, is_drag; |
| 1362 | |
| 1363 | /* |
| 1364 | * Handle modeless selection. |
| 1365 | */ |
| 1366 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1367 | &is_click, &is_drag); |
| 1368 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1369 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1370 | { |
| 1371 | /* Translate shift-left to right button. */ |
| 1372 | button = MOUSE_RIGHT; |
| 1373 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1374 | } |
| 1375 | clip_modeless(button, is_click, is_drag); |
| 1376 | goto cmdline_not_changed; |
| 1377 | } |
| 1378 | # endif |
| 1379 | |
| 1380 | set_cmdspos(); |
| 1381 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 1382 | ++ccline.cmdpos) |
| 1383 | { |
| 1384 | i = cmdline_charsize(ccline.cmdpos); |
| 1385 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 1386 | && mouse_col < ccline.cmdspos % Columns + i) |
| 1387 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1388 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1389 | if (has_mbyte) |
| 1390 | { |
| 1391 | /* Count ">" for double-wide char that doesn't fit. */ |
| 1392 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1393 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1394 | + ccline.cmdpos) - 1; |
| 1395 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1396 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1397 | ccline.cmdspos += i; |
| 1398 | } |
| 1399 | goto cmdline_not_changed; |
| 1400 | |
| 1401 | /* Mouse scroll wheel: ignored here */ |
| 1402 | case K_MOUSEDOWN: |
| 1403 | case K_MOUSEUP: |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1404 | case K_MOUSELEFT: |
| 1405 | case K_MOUSERIGHT: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1406 | /* Alternate buttons ignored here */ |
| 1407 | case K_X1MOUSE: |
| 1408 | case K_X1DRAG: |
| 1409 | case K_X1RELEASE: |
| 1410 | case K_X2MOUSE: |
| 1411 | case K_X2DRAG: |
| 1412 | case K_X2RELEASE: |
| 1413 | goto cmdline_not_changed; |
| 1414 | |
| 1415 | #endif /* FEAT_MOUSE */ |
| 1416 | |
| 1417 | #ifdef FEAT_GUI |
| 1418 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 1419 | case K_LEFTRELEASE_NM: |
| 1420 | goto cmdline_not_changed; |
| 1421 | |
| 1422 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1423 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1424 | { |
| 1425 | gui_do_scroll(); |
| 1426 | redrawcmd(); |
| 1427 | } |
| 1428 | goto cmdline_not_changed; |
| 1429 | |
| 1430 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1431 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1432 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1433 | gui_do_horiz_scroll(scrollbar_value, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1434 | redrawcmd(); |
| 1435 | } |
| 1436 | goto cmdline_not_changed; |
| 1437 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1438 | #ifdef FEAT_GUI_TABLINE |
| 1439 | case K_TABLINE: |
| 1440 | case K_TABMENU: |
| 1441 | /* Don't want to change any tabs here. Make sure the same tab |
| 1442 | * is still selected. */ |
| 1443 | if (gui_use_tabline()) |
| 1444 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 1445 | goto cmdline_not_changed; |
| 1446 | #endif |
| 1447 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1448 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1449 | goto cmdline_not_changed; |
| 1450 | |
| 1451 | case Ctrl_B: /* begin of command line */ |
| 1452 | case K_HOME: |
| 1453 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1454 | case K_S_HOME: |
| 1455 | case K_C_HOME: |
| 1456 | ccline.cmdpos = 0; |
| 1457 | set_cmdspos(); |
| 1458 | goto cmdline_not_changed; |
| 1459 | |
| 1460 | case Ctrl_E: /* end of command line */ |
| 1461 | case K_END: |
| 1462 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1463 | case K_S_END: |
| 1464 | case K_C_END: |
| 1465 | ccline.cmdpos = ccline.cmdlen; |
| 1466 | set_cmdspos_cursor(); |
| 1467 | goto cmdline_not_changed; |
| 1468 | |
| 1469 | case Ctrl_A: /* all matches */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1470 | if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1471 | break; |
| 1472 | goto cmdline_changed; |
| 1473 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1474 | case Ctrl_L: |
| 1475 | #ifdef FEAT_SEARCH_EXTRA |
| 1476 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1477 | { |
| 1478 | /* Add a character from under the cursor for 'incsearch' */ |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1479 | if (did_incsearch) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1480 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1481 | curwin->w_cursor = match_end; |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1482 | if (!EQUAL_POS(curwin->w_cursor, search_start)) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1483 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1484 | c = gchar_cursor(); |
| 1485 | /* If 'ignorecase' and 'smartcase' are set and the |
| 1486 | * command line has no uppercase characters, convert |
| 1487 | * the character to lowercase */ |
| 1488 | if (p_ic && p_scs |
| 1489 | && !pat_has_uppercase(ccline.cmdbuff)) |
| 1490 | c = MB_TOLOWER(c); |
| 1491 | if (c != NUL) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1492 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1493 | if (c == firstc || vim_strchr((char_u *)( |
| 1494 | p_magic ? "\\^$.*[" : "\\^$"), c) |
| 1495 | != NULL) |
| 1496 | { |
| 1497 | /* put a backslash before special |
| 1498 | * characters */ |
| 1499 | stuffcharReadbuff(c); |
| 1500 | c = '\\'; |
| 1501 | } |
| 1502 | break; |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1503 | } |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1504 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1505 | } |
| 1506 | goto cmdline_not_changed; |
| 1507 | } |
| 1508 | #endif |
| 1509 | |
| 1510 | /* completion: longest common part */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1511 | if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1512 | break; |
| 1513 | goto cmdline_changed; |
| 1514 | |
| 1515 | case Ctrl_N: /* next match */ |
| 1516 | case Ctrl_P: /* previous match */ |
Bram Moolenaar | 7df0f63 | 2016-08-26 19:56:00 +0200 | [diff] [blame] | 1517 | if (xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1518 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1519 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, |
| 1520 | 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1521 | break; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1522 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1523 | } |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1524 | /* FALLTHROUGH */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1525 | |
| 1526 | #ifdef FEAT_CMDHIST |
| 1527 | case K_UP: |
| 1528 | case K_DOWN: |
| 1529 | case K_S_UP: |
| 1530 | case K_S_DOWN: |
| 1531 | case K_PAGEUP: |
| 1532 | case K_KPAGEUP: |
| 1533 | case K_PAGEDOWN: |
| 1534 | case K_KPAGEDOWN: |
| 1535 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 1536 | goto cmdline_not_changed; |
| 1537 | |
| 1538 | i = hiscnt; |
| 1539 | |
| 1540 | /* save current command string so it can be restored later */ |
| 1541 | if (lookfor == NULL) |
| 1542 | { |
| 1543 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 1544 | goto cmdline_not_changed; |
| 1545 | lookfor[ccline.cmdpos] = NUL; |
| 1546 | } |
| 1547 | |
| 1548 | j = (int)STRLEN(lookfor); |
| 1549 | for (;;) |
| 1550 | { |
| 1551 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1552 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1553 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1554 | { |
| 1555 | if (hiscnt == hislen) /* first time */ |
| 1556 | hiscnt = hisidx[histype]; |
| 1557 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 1558 | hiscnt = hislen - 1; |
| 1559 | else if (hiscnt != hisidx[histype] + 1) |
| 1560 | --hiscnt; |
| 1561 | else /* at top of list */ |
| 1562 | { |
| 1563 | hiscnt = i; |
| 1564 | break; |
| 1565 | } |
| 1566 | } |
| 1567 | else /* one step forwards */ |
| 1568 | { |
| 1569 | /* on last entry, clear the line */ |
| 1570 | if (hiscnt == hisidx[histype]) |
| 1571 | { |
| 1572 | hiscnt = hislen; |
| 1573 | break; |
| 1574 | } |
| 1575 | |
| 1576 | /* not on a history line, nothing to do */ |
| 1577 | if (hiscnt == hislen) |
| 1578 | break; |
| 1579 | if (hiscnt == hislen - 1) /* wrap around */ |
| 1580 | hiscnt = 0; |
| 1581 | else |
| 1582 | ++hiscnt; |
| 1583 | } |
| 1584 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 1585 | { |
| 1586 | hiscnt = i; |
| 1587 | break; |
| 1588 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1589 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1590 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1591 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 1592 | lookfor, (size_t)j) == 0) |
| 1593 | break; |
| 1594 | } |
| 1595 | |
| 1596 | if (hiscnt != i) /* jumped to other entry */ |
| 1597 | { |
| 1598 | char_u *p; |
| 1599 | int len; |
| 1600 | int old_firstc; |
| 1601 | |
| 1602 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1603 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1604 | if (hiscnt == hislen) |
| 1605 | p = lookfor; /* back to the old one */ |
| 1606 | else |
| 1607 | p = history[histype][hiscnt].hisstr; |
| 1608 | |
| 1609 | if (histype == HIST_SEARCH |
| 1610 | && p != lookfor |
| 1611 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 1612 | { |
| 1613 | /* Correct for the separator character used when |
| 1614 | * adding the history entry vs the one used now. |
| 1615 | * First loop: count length. |
| 1616 | * Second loop: copy the characters. */ |
| 1617 | for (i = 0; i <= 1; ++i) |
| 1618 | { |
| 1619 | len = 0; |
| 1620 | for (j = 0; p[j] != NUL; ++j) |
| 1621 | { |
| 1622 | /* Replace old sep with new sep, unless it is |
| 1623 | * escaped. */ |
| 1624 | if (p[j] == old_firstc |
| 1625 | && (j == 0 || p[j - 1] != '\\')) |
| 1626 | { |
| 1627 | if (i > 0) |
| 1628 | ccline.cmdbuff[len] = firstc; |
| 1629 | } |
| 1630 | else |
| 1631 | { |
| 1632 | /* Escape new sep, unless it is already |
| 1633 | * escaped. */ |
| 1634 | if (p[j] == firstc |
| 1635 | && (j == 0 || p[j - 1] != '\\')) |
| 1636 | { |
| 1637 | if (i > 0) |
| 1638 | ccline.cmdbuff[len] = '\\'; |
| 1639 | ++len; |
| 1640 | } |
| 1641 | if (i > 0) |
| 1642 | ccline.cmdbuff[len] = p[j]; |
| 1643 | } |
| 1644 | ++len; |
| 1645 | } |
| 1646 | if (i == 0) |
| 1647 | { |
| 1648 | alloc_cmdbuff(len); |
| 1649 | if (ccline.cmdbuff == NULL) |
| 1650 | goto returncmd; |
| 1651 | } |
| 1652 | } |
| 1653 | ccline.cmdbuff[len] = NUL; |
| 1654 | } |
| 1655 | else |
| 1656 | { |
| 1657 | alloc_cmdbuff((int)STRLEN(p)); |
| 1658 | if (ccline.cmdbuff == NULL) |
| 1659 | goto returncmd; |
| 1660 | STRCPY(ccline.cmdbuff, p); |
| 1661 | } |
| 1662 | |
| 1663 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 1664 | redrawcmd(); |
| 1665 | goto cmdline_changed; |
| 1666 | } |
| 1667 | beep_flush(); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1668 | #endif |
| 1669 | goto cmdline_not_changed; |
| 1670 | |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1671 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1672 | case Ctrl_G: /* next match */ |
| 1673 | case Ctrl_T: /* previous match */ |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1674 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1675 | { |
| 1676 | pos_T t; |
| 1677 | int search_flags = SEARCH_KEEP + SEARCH_NOOF |
| 1678 | + SEARCH_PEEK; |
| 1679 | |
| 1680 | if (char_avail()) |
| 1681 | continue; |
| 1682 | cursor_off(); |
| 1683 | out_flush(); |
| 1684 | if (c == Ctrl_G) |
| 1685 | { |
| 1686 | t = match_end; |
| 1687 | search_flags += SEARCH_COL; |
| 1688 | } |
| 1689 | else |
| 1690 | t = match_start; |
| 1691 | ++emsg_off; |
| 1692 | i = searchit(curwin, curbuf, &t, |
| 1693 | c == Ctrl_G ? FORWARD : BACKWARD, |
| 1694 | ccline.cmdbuff, count, search_flags, |
| 1695 | RE_SEARCH, 0, NULL); |
| 1696 | --emsg_off; |
| 1697 | if (i) |
| 1698 | { |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1699 | search_start = match_start; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1700 | match_end = t; |
| 1701 | match_start = t; |
| 1702 | if (c == Ctrl_T && firstc == '/') |
| 1703 | { |
| 1704 | /* move just before the current match, so that |
| 1705 | * when nv_search finishes the cursor will be |
| 1706 | * put back on the match */ |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1707 | search_start = t; |
| 1708 | (void)decl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1709 | } |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 1710 | if (LT_POS(t, search_start) && c == Ctrl_G) |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1711 | { |
| 1712 | /* wrap around */ |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1713 | search_start = t; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1714 | if (firstc == '?') |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1715 | (void)incl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1716 | else |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1717 | (void)decl(&search_start); |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1718 | } |
| 1719 | |
| 1720 | set_search_match(&match_end); |
| 1721 | curwin->w_cursor = match_start; |
| 1722 | changed_cline_bef_curs(); |
| 1723 | update_topline(); |
| 1724 | validate_cursor(); |
| 1725 | highlight_match = TRUE; |
| 1726 | old_curswant = curwin->w_curswant; |
| 1727 | old_leftcol = curwin->w_leftcol; |
| 1728 | old_topline = curwin->w_topline; |
| 1729 | # ifdef FEAT_DIFF |
| 1730 | old_topfill = curwin->w_topfill; |
| 1731 | # endif |
| 1732 | old_botline = curwin->w_botline; |
| 1733 | update_screen(NOT_VALID); |
| 1734 | redrawcmdline(); |
| 1735 | } |
| 1736 | else |
| 1737 | vim_beep(BO_ERROR); |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1738 | goto cmdline_not_changed; |
Bram Moolenaar | 1195669 | 2016-08-27 16:26:56 +0200 | [diff] [blame] | 1739 | } |
Bram Moolenaar | 349e7d9 | 2016-09-03 20:04:34 +0200 | [diff] [blame] | 1740 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1741 | #endif |
| 1742 | |
| 1743 | case Ctrl_V: |
| 1744 | case Ctrl_Q: |
| 1745 | #ifdef FEAT_MOUSE |
| 1746 | ignore_drag_release = TRUE; |
| 1747 | #endif |
| 1748 | putcmdline('^', TRUE); |
| 1749 | c = get_literal(); /* get next (two) character(s) */ |
| 1750 | do_abbr = FALSE; /* don't do abbreviation now */ |
| 1751 | #ifdef FEAT_MBYTE |
| 1752 | /* may need to remove ^ when composing char was typed */ |
| 1753 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 1754 | { |
| 1755 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 1756 | msg_putchar(' '); |
| 1757 | cursorcmd(); |
| 1758 | } |
| 1759 | #endif |
| 1760 | break; |
| 1761 | |
| 1762 | #ifdef FEAT_DIGRAPHS |
| 1763 | case Ctrl_K: |
| 1764 | #ifdef FEAT_MOUSE |
| 1765 | ignore_drag_release = TRUE; |
| 1766 | #endif |
| 1767 | putcmdline('?', TRUE); |
| 1768 | #ifdef USE_ON_FLY_SCROLL |
| 1769 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1770 | #endif |
| 1771 | c = get_digraph(TRUE); |
| 1772 | if (c != NUL) |
| 1773 | break; |
| 1774 | |
| 1775 | redrawcmd(); |
| 1776 | goto cmdline_not_changed; |
| 1777 | #endif /* FEAT_DIGRAPHS */ |
| 1778 | |
| 1779 | #ifdef FEAT_RIGHTLEFT |
| 1780 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 1781 | if (!p_ari) |
| 1782 | break; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1783 | # ifdef FEAT_FKMAP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1784 | if (p_altkeymap) |
| 1785 | { |
| 1786 | cmd_fkmap = !cmd_fkmap; |
| 1787 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 1788 | ccline.overstrike = FALSE; |
| 1789 | } |
| 1790 | else /* Hebrew is default */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1791 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1792 | cmd_hkmap = !cmd_hkmap; |
| 1793 | goto cmdline_not_changed; |
| 1794 | #endif |
| 1795 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 1796 | case K_PS: |
| 1797 | bracketed_paste(PASTE_CMDLINE, FALSE, NULL); |
| 1798 | goto cmdline_changed; |
| 1799 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1800 | default: |
| 1801 | #ifdef UNIX |
| 1802 | if (c == intr_char) |
| 1803 | { |
| 1804 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1805 | putting it in history */ |
| 1806 | goto returncmd; /* back to Normal mode */ |
| 1807 | } |
| 1808 | #endif |
| 1809 | /* |
| 1810 | * Normal character with no special meaning. Just set mod_mask |
| 1811 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 1812 | * the string <S-Space>. This should only happen after ^V. |
| 1813 | */ |
| 1814 | if (!IS_SPECIAL(c)) |
| 1815 | mod_mask = 0x0; |
| 1816 | break; |
| 1817 | } |
| 1818 | /* |
| 1819 | * End of switch on command line character. |
| 1820 | * We come here if we have a normal character. |
| 1821 | */ |
| 1822 | |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1823 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1824 | #ifdef FEAT_MBYTE |
| 1825 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1826 | * what check_abbr() expects. */ |
| 1827 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1828 | #endif |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1829 | c) || c == Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1830 | goto cmdline_changed; |
| 1831 | |
| 1832 | /* |
| 1833 | * put the character in the command line |
| 1834 | */ |
| 1835 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 1836 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 1837 | else |
| 1838 | { |
| 1839 | #ifdef FEAT_MBYTE |
| 1840 | if (has_mbyte) |
| 1841 | { |
| 1842 | j = (*mb_char2bytes)(c, IObuff); |
| 1843 | IObuff[j] = NUL; /* exclude composing chars */ |
| 1844 | put_on_cmdline(IObuff, j, TRUE); |
| 1845 | } |
| 1846 | else |
| 1847 | #endif |
| 1848 | { |
| 1849 | IObuff[0] = c; |
| 1850 | put_on_cmdline(IObuff, 1, TRUE); |
| 1851 | } |
| 1852 | } |
| 1853 | goto cmdline_changed; |
| 1854 | |
| 1855 | /* |
| 1856 | * This part implements incremental searches for "/" and "?" |
| 1857 | * Jump to cmdline_not_changed when a character has been read but the command |
| 1858 | * line did not change. Then we only search and redraw if something changed in |
| 1859 | * the past. |
| 1860 | * Jump to cmdline_changed when the command line did change. |
| 1861 | * (Sorry for the goto's, I know it is ugly). |
| 1862 | */ |
| 1863 | cmdline_not_changed: |
| 1864 | #ifdef FEAT_SEARCH_EXTRA |
| 1865 | if (!incsearch_postponed) |
| 1866 | continue; |
| 1867 | #endif |
| 1868 | |
| 1869 | cmdline_changed: |
| 1870 | #ifdef FEAT_SEARCH_EXTRA |
| 1871 | /* |
| 1872 | * 'incsearch' highlighting. |
| 1873 | */ |
| 1874 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1875 | { |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1876 | pos_T end_pos; |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1877 | #ifdef FEAT_RELTIME |
| 1878 | proftime_T tm; |
| 1879 | #endif |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1880 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1881 | /* if there is a character waiting, search and redraw later */ |
| 1882 | if (char_avail()) |
| 1883 | { |
| 1884 | incsearch_postponed = TRUE; |
| 1885 | continue; |
| 1886 | } |
| 1887 | incsearch_postponed = FALSE; |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 1888 | curwin->w_cursor = search_start; /* start at old position */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1889 | |
| 1890 | /* If there is no command line, don't do anything */ |
| 1891 | if (ccline.cmdlen == 0) |
| 1892 | i = 0; |
| 1893 | else |
| 1894 | { |
| 1895 | cursor_off(); /* so the user knows we're busy */ |
| 1896 | out_flush(); |
| 1897 | ++emsg_off; /* So it doesn't beep if bad expr */ |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1898 | #ifdef FEAT_RELTIME |
| 1899 | /* Set the time limit to half a second. */ |
| 1900 | profile_setlimit(500L, &tm); |
| 1901 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1902 | i = do_search(NULL, firstc, ccline.cmdbuff, count, |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1903 | SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK, |
| 1904 | #ifdef FEAT_RELTIME |
| 1905 | &tm |
| 1906 | #else |
| 1907 | NULL |
| 1908 | #endif |
| 1909 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1910 | --emsg_off; |
| 1911 | /* if interrupted while searching, behave like it failed */ |
| 1912 | if (got_int) |
| 1913 | { |
| 1914 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 1915 | got_int = FALSE; /* don't abandon the command line */ |
| 1916 | i = 0; |
| 1917 | } |
| 1918 | else if (char_avail()) |
| 1919 | /* cancelled searching because a char was typed */ |
| 1920 | incsearch_postponed = TRUE; |
| 1921 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1922 | if (i != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1923 | highlight_match = TRUE; /* highlight position */ |
| 1924 | else |
| 1925 | highlight_match = FALSE; /* remove highlight */ |
| 1926 | |
| 1927 | /* first restore the old curwin values, so the screen is |
| 1928 | * positioned in the same way as the actual search command */ |
| 1929 | curwin->w_leftcol = old_leftcol; |
| 1930 | curwin->w_topline = old_topline; |
| 1931 | # ifdef FEAT_DIFF |
| 1932 | curwin->w_topfill = old_topfill; |
| 1933 | # endif |
| 1934 | curwin->w_botline = old_botline; |
| 1935 | changed_cline_bef_curs(); |
| 1936 | update_topline(); |
| 1937 | |
| 1938 | if (i != 0) |
| 1939 | { |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1940 | pos_T save_pos = curwin->w_cursor; |
| 1941 | |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1942 | match_start = curwin->w_cursor; |
| 1943 | set_search_match(&curwin->w_cursor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1944 | validate_cursor(); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1945 | end_pos = curwin->w_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1946 | match_end = end_pos; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1947 | curwin->w_cursor = save_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1948 | } |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1949 | else |
| 1950 | end_pos = curwin->w_cursor; /* shutup gcc 4 */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1951 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1952 | validate_cursor(); |
Bram Moolenaar | 27a2319 | 2006-09-14 09:27:26 +0000 | [diff] [blame] | 1953 | # ifdef FEAT_WINDOWS |
| 1954 | /* May redraw the status line to show the cursor position. */ |
| 1955 | if (p_ru && curwin->w_status_height > 0) |
| 1956 | curwin->w_redr_status = TRUE; |
| 1957 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1958 | |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1959 | save_cmdline(&save_ccline); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 1960 | update_screen(SOME_VALID); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1961 | restore_cmdline(&save_ccline); |
| 1962 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1963 | /* Leave it at the end to make CTRL-R CTRL-W work. */ |
| 1964 | if (i != 0) |
| 1965 | curwin->w_cursor = end_pos; |
| 1966 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1967 | msg_starthere(); |
| 1968 | redrawcmdline(); |
| 1969 | did_incsearch = TRUE; |
| 1970 | } |
| 1971 | #else /* FEAT_SEARCH_EXTRA */ |
| 1972 | ; |
| 1973 | #endif |
| 1974 | |
| 1975 | #ifdef FEAT_RIGHTLEFT |
| 1976 | if (cmdmsg_rl |
| 1977 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1978 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1979 | # endif |
| 1980 | ) |
| 1981 | /* Always redraw the whole command line to fix shaping and |
Bram Moolenaar | 58437e0 | 2012-02-22 17:58:04 +0100 | [diff] [blame] | 1982 | * right-left typing. Not efficient, but it works. |
| 1983 | * Do it only when there are no characters left to read |
| 1984 | * to avoid useless intermediate redraws. */ |
| 1985 | if (vpeekc() == NUL) |
| 1986 | redrawcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1987 | #endif |
| 1988 | } |
| 1989 | |
| 1990 | returncmd: |
| 1991 | |
| 1992 | #ifdef FEAT_RIGHTLEFT |
| 1993 | cmdmsg_rl = FALSE; |
| 1994 | #endif |
| 1995 | |
| 1996 | #ifdef FEAT_FKMAP |
| 1997 | cmd_fkmap = 0; |
| 1998 | #endif |
| 1999 | |
| 2000 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2001 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2002 | |
| 2003 | #ifdef FEAT_SEARCH_EXTRA |
| 2004 | if (did_incsearch) |
| 2005 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 2006 | if (gotesc) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 2007 | curwin->w_cursor = save_cursor; |
| 2008 | else |
| 2009 | { |
Bram Moolenaar | b5aedf3 | 2017-03-12 18:23:53 +0100 | [diff] [blame] | 2010 | if (!EQUAL_POS(save_cursor, search_start)) |
Bram Moolenaar | dda933d | 2016-09-03 21:04:58 +0200 | [diff] [blame] | 2011 | { |
| 2012 | /* put the '" mark at the original position */ |
| 2013 | curwin->w_cursor = save_cursor; |
| 2014 | setpcmark(); |
| 2015 | } |
| 2016 | curwin->w_cursor = search_start; |
| 2017 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2018 | curwin->w_curswant = old_curswant; |
| 2019 | curwin->w_leftcol = old_leftcol; |
| 2020 | curwin->w_topline = old_topline; |
| 2021 | # ifdef FEAT_DIFF |
| 2022 | curwin->w_topfill = old_topfill; |
| 2023 | # endif |
| 2024 | curwin->w_botline = old_botline; |
| 2025 | highlight_match = FALSE; |
| 2026 | validate_cursor(); /* needed for TAB */ |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 2027 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2028 | } |
| 2029 | #endif |
| 2030 | |
| 2031 | if (ccline.cmdbuff != NULL) |
| 2032 | { |
| 2033 | /* |
| 2034 | * Put line in history buffer (":" and "=" only when it was typed). |
| 2035 | */ |
| 2036 | #ifdef FEAT_CMDHIST |
| 2037 | if (ccline.cmdlen && firstc != NUL |
| 2038 | && (some_key_typed || histype == HIST_SEARCH)) |
| 2039 | { |
| 2040 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 2041 | histype == HIST_SEARCH ? firstc : NUL); |
| 2042 | if (firstc == ':') |
| 2043 | { |
| 2044 | vim_free(new_last_cmdline); |
| 2045 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 2046 | } |
| 2047 | } |
| 2048 | #endif |
| 2049 | |
| 2050 | if (gotesc) /* abandon command line */ |
| 2051 | { |
| 2052 | vim_free(ccline.cmdbuff); |
| 2053 | ccline.cmdbuff = NULL; |
| 2054 | if (msg_scrolled == 0) |
| 2055 | compute_cmdrow(); |
| 2056 | MSG(""); |
| 2057 | redraw_cmdline = TRUE; |
| 2058 | } |
| 2059 | } |
| 2060 | |
| 2061 | /* |
| 2062 | * If the screen was shifted up, redraw the whole screen (later). |
| 2063 | * If the line is too long, clear it, so ruler and shown command do |
| 2064 | * not get printed in the middle of it. |
| 2065 | */ |
| 2066 | msg_check(); |
| 2067 | msg_scroll = save_msg_scroll; |
| 2068 | redir_off = FALSE; |
| 2069 | |
| 2070 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 2071 | if (some_key_typed) |
| 2072 | need_wait_return = FALSE; |
| 2073 | |
| 2074 | State = save_State; |
| 2075 | #ifdef USE_IM_CONTROL |
| 2076 | if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) |
| 2077 | im_save_status(b_im_ptr); |
| 2078 | im_set_active(FALSE); |
| 2079 | #endif |
| 2080 | #ifdef FEAT_MOUSE |
| 2081 | setmouse(); |
| 2082 | #endif |
| 2083 | #ifdef CURSOR_SHAPE |
| 2084 | ui_cursor_shape(); /* may show different cursor shape */ |
| 2085 | #endif |
| 2086 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2087 | { |
| 2088 | char_u *p = ccline.cmdbuff; |
| 2089 | |
| 2090 | /* Make ccline empty, getcmdline() may try to use it. */ |
| 2091 | ccline.cmdbuff = NULL; |
| 2092 | return p; |
| 2093 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2094 | } |
| 2095 | |
| 2096 | #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) |
| 2097 | /* |
| 2098 | * Get a command line with a prompt. |
| 2099 | * This is prepared to be called recursively from getcmdline() (e.g. by |
| 2100 | * f_input() when evaluating an expression from CTRL-R =). |
| 2101 | * Returns the command line in allocated memory, or NULL. |
| 2102 | */ |
| 2103 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2104 | getcmdline_prompt( |
| 2105 | int firstc, |
| 2106 | char_u *prompt, /* command line prompt */ |
| 2107 | int attr, /* attributes for prompt */ |
| 2108 | int xp_context, /* type of expansion */ |
| 2109 | char_u *xp_arg) /* user-defined expansion argument */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2110 | { |
| 2111 | char_u *s; |
| 2112 | struct cmdline_info save_ccline; |
| 2113 | int msg_col_save = msg_col; |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2114 | int msg_silent_save = msg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2115 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2116 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2117 | ccline.cmdprompt = prompt; |
| 2118 | ccline.cmdattr = attr; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2119 | # ifdef FEAT_EVAL |
| 2120 | ccline.xp_context = xp_context; |
| 2121 | ccline.xp_arg = xp_arg; |
| 2122 | ccline.input_fn = (firstc == '@'); |
| 2123 | # endif |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2124 | msg_silent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2125 | s = getcmdline(firstc, 1L, 0); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2126 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2127 | msg_silent = msg_silent_save; |
Bram Moolenaar | 1db1f77 | 2011-08-17 16:25:48 +0200 | [diff] [blame] | 2128 | /* Restore msg_col, the prompt from input() may have changed it. |
| 2129 | * But only if called recursively and the commandline is therefore being |
| 2130 | * restored to an old one; if not, the input() prompt stays on the screen, |
| 2131 | * so we need its modified msg_col left intact. */ |
| 2132 | if (ccline.cmdbuff != NULL) |
| 2133 | msg_col = msg_col_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2134 | |
| 2135 | return s; |
| 2136 | } |
| 2137 | #endif |
| 2138 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2139 | /* |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2140 | * Return TRUE when the text must not be changed and we can't switch to |
| 2141 | * another window or buffer. Used when editing the command line, evaluating |
| 2142 | * 'balloonexpr', etc. |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2143 | */ |
| 2144 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2145 | text_locked(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2146 | { |
| 2147 | #ifdef FEAT_CMDWIN |
| 2148 | if (cmdwin_type != 0) |
| 2149 | return TRUE; |
| 2150 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2151 | return textlock != 0; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2152 | } |
| 2153 | |
| 2154 | /* |
| 2155 | * Give an error message for a command that isn't allowed while the cmdline |
| 2156 | * window is open or editing the cmdline in another way. |
| 2157 | */ |
| 2158 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2159 | text_locked_msg(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2160 | { |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2161 | EMSG(_(get_text_locked_msg())); |
| 2162 | } |
| 2163 | |
| 2164 | char_u * |
| 2165 | get_text_locked_msg(void) |
| 2166 | { |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2167 | #ifdef FEAT_CMDWIN |
| 2168 | if (cmdwin_type != 0) |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2169 | return e_cmdwin; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2170 | #endif |
Bram Moolenaar | 5a49789 | 2016-09-03 16:29:04 +0200 | [diff] [blame] | 2171 | return e_secure; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2172 | } |
| 2173 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2174 | #if defined(FEAT_AUTOCMD) || defined(PROTO) |
| 2175 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2176 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2177 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2178 | */ |
| 2179 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2180 | curbuf_locked(void) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2181 | { |
| 2182 | if (curbuf_lock > 0) |
| 2183 | { |
| 2184 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2185 | return TRUE; |
| 2186 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2187 | return allbuf_locked(); |
| 2188 | } |
| 2189 | |
| 2190 | /* |
| 2191 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2192 | * message. |
| 2193 | */ |
| 2194 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2195 | allbuf_locked(void) |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2196 | { |
| 2197 | if (allbuf_lock > 0) |
| 2198 | { |
| 2199 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2200 | return TRUE; |
| 2201 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2202 | return FALSE; |
| 2203 | } |
| 2204 | #endif |
| 2205 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2206 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2207 | cmdline_charsize(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2208 | { |
| 2209 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2210 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2211 | return 1; |
| 2212 | #endif |
| 2213 | return ptr2cells(ccline.cmdbuff + idx); |
| 2214 | } |
| 2215 | |
| 2216 | /* |
| 2217 | * Compute the offset of the cursor on the command line for the prompt and |
| 2218 | * indent. |
| 2219 | */ |
| 2220 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2221 | set_cmdspos(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2222 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2223 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2224 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2225 | else |
| 2226 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2227 | } |
| 2228 | |
| 2229 | /* |
| 2230 | * Compute the screen position for the cursor on the command line. |
| 2231 | */ |
| 2232 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2233 | set_cmdspos_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2234 | { |
| 2235 | int i, m, c; |
| 2236 | |
| 2237 | set_cmdspos(); |
| 2238 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2239 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2240 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2241 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2242 | m = MAXCOL; |
| 2243 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2244 | else |
| 2245 | m = MAXCOL; |
| 2246 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2247 | { |
| 2248 | c = cmdline_charsize(i); |
| 2249 | #ifdef FEAT_MBYTE |
| 2250 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2251 | if (has_mbyte) |
| 2252 | correct_cmdspos(i, c); |
| 2253 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2254 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2255 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2256 | if ((ccline.cmdspos += c) >= m) |
| 2257 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2258 | ccline.cmdspos -= c; |
| 2259 | break; |
| 2260 | } |
| 2261 | #ifdef FEAT_MBYTE |
| 2262 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2263 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2264 | #endif |
| 2265 | } |
| 2266 | } |
| 2267 | |
| 2268 | #ifdef FEAT_MBYTE |
| 2269 | /* |
| 2270 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2271 | * character that doesn't fit, so that a ">" must be displayed. |
| 2272 | */ |
| 2273 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2274 | correct_cmdspos(int idx, int cells) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2275 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2276 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2277 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2278 | && ccline.cmdspos % Columns + cells > Columns) |
| 2279 | ccline.cmdspos++; |
| 2280 | } |
| 2281 | #endif |
| 2282 | |
| 2283 | /* |
| 2284 | * Get an Ex command line for the ":" command. |
| 2285 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2286 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2287 | getexline( |
| 2288 | int c, /* normally ':', NUL for ":append" */ |
| 2289 | void *cookie UNUSED, |
| 2290 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2291 | { |
| 2292 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2293 | if (exec_from_reg && vpeekc() == ':') |
| 2294 | (void)vgetc(); |
| 2295 | return getcmdline(c, 1L, indent); |
| 2296 | } |
| 2297 | |
| 2298 | /* |
| 2299 | * Get an Ex command line for Ex mode. |
| 2300 | * In Ex mode we only use the OS supplied line editing features and no |
| 2301 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2302 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2303 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2304 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2305 | getexmodeline( |
| 2306 | int promptc, /* normally ':', NUL for ":append" and '?' for |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2307 | :s prompt */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2308 | void *cookie UNUSED, |
| 2309 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2310 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2311 | garray_T line_ga; |
| 2312 | char_u *pend; |
| 2313 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2314 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2315 | int escaped = FALSE; /* CTRL-V typed */ |
| 2316 | int vcol = 0; |
| 2317 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2318 | int prev_char; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2319 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2320 | |
| 2321 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2322 | * confuses the system function that computes tabstops. */ |
| 2323 | cursor_on(); |
| 2324 | |
| 2325 | /* always start in column 0; write a newline if necessary */ |
| 2326 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2327 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2328 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2329 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2330 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2331 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2332 | if (p_prompt) |
| 2333 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2334 | while (indent-- > 0) |
| 2335 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2336 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2337 | } |
| 2338 | |
| 2339 | ga_init2(&line_ga, 1, 30); |
| 2340 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2341 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2342 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2343 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2344 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2345 | while (indent >= 8) |
| 2346 | { |
| 2347 | ga_append(&line_ga, TAB); |
| 2348 | msg_puts((char_u *)" "); |
| 2349 | indent -= 8; |
| 2350 | } |
| 2351 | while (indent-- > 0) |
| 2352 | { |
| 2353 | ga_append(&line_ga, ' '); |
| 2354 | msg_putchar(' '); |
| 2355 | } |
| 2356 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2357 | ++no_mapping; |
| 2358 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2359 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2360 | /* |
| 2361 | * Get the line, one character at a time. |
| 2362 | */ |
| 2363 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2364 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2365 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2366 | long sw; |
| 2367 | char_u *s; |
| 2368 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2369 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2370 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2371 | |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2372 | /* |
| 2373 | * Get one character at a time. |
| 2374 | */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2375 | prev_char = c1; |
Bram Moolenaar | ba748c8 | 2017-02-26 14:00:07 +0100 | [diff] [blame] | 2376 | |
| 2377 | /* Check for a ":normal" command and no more characters left. */ |
| 2378 | if (ex_normal_busy > 0 && typebuf.tb_len == 0) |
| 2379 | c1 = '\n'; |
| 2380 | else |
| 2381 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2382 | |
| 2383 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2384 | * Handle line editing. |
| 2385 | * Previously this was left to the system, putting the terminal in |
| 2386 | * 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] | 2387 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2388 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2389 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2390 | msg_putchar('\n'); |
| 2391 | break; |
| 2392 | } |
| 2393 | |
Bram Moolenaar | abbc448 | 2017-01-24 15:57:55 +0100 | [diff] [blame] | 2394 | if (c1 == K_PS) |
| 2395 | { |
| 2396 | bracketed_paste(PASTE_EX, FALSE, &line_ga); |
| 2397 | goto redraw; |
| 2398 | } |
| 2399 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2400 | if (!escaped) |
| 2401 | { |
| 2402 | /* CR typed means "enter", which is NL */ |
| 2403 | if (c1 == '\r') |
| 2404 | c1 = '\n'; |
| 2405 | |
| 2406 | if (c1 == BS || c1 == K_BS |
| 2407 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2408 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2409 | if (line_ga.ga_len > 0) |
| 2410 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2411 | #ifdef FEAT_MBYTE |
| 2412 | if (has_mbyte) |
| 2413 | { |
| 2414 | p = (char_u *)line_ga.ga_data; |
| 2415 | p[line_ga.ga_len] = NUL; |
| 2416 | len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1; |
| 2417 | line_ga.ga_len -= len; |
| 2418 | } |
| 2419 | else |
| 2420 | #endif |
| 2421 | --line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2422 | goto redraw; |
| 2423 | } |
| 2424 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2425 | } |
| 2426 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2427 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2428 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2429 | msg_col = startcol; |
| 2430 | msg_clr_eos(); |
| 2431 | line_ga.ga_len = 0; |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2432 | goto redraw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2433 | } |
| 2434 | |
| 2435 | if (c1 == Ctrl_T) |
| 2436 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2437 | sw = get_sw_value(curbuf); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2438 | p = (char_u *)line_ga.ga_data; |
| 2439 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2440 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 2441 | indent += sw - indent % sw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2442 | add_indent: |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2443 | while (get_indent_str(p, 8, FALSE) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2444 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2445 | (void)ga_grow(&line_ga, 2); /* one more for the NUL */ |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2446 | p = (char_u *)line_ga.ga_data; |
| 2447 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2448 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2449 | *s = ' '; |
| 2450 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2451 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2452 | redraw: |
| 2453 | /* redraw the line */ |
| 2454 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2455 | vcol = 0; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2456 | p = (char_u *)line_ga.ga_data; |
| 2457 | p[line_ga.ga_len] = NUL; |
| 2458 | while (p < (char_u *)line_ga.ga_data + line_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2459 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2460 | if (*p == TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2461 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2462 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2463 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2464 | msg_putchar(' '); |
| 2465 | } while (++vcol % 8); |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2466 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2467 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2468 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2469 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2470 | len = MB_PTR2LEN(p); |
| 2471 | msg_outtrans_len(p, len); |
| 2472 | vcol += ptr2cells(p); |
| 2473 | p += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2474 | } |
| 2475 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2476 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2477 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2478 | continue; |
| 2479 | } |
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 (c1 == Ctrl_D) |
| 2482 | { |
| 2483 | /* Delete one shiftwidth. */ |
| 2484 | p = (char_u *)line_ga.ga_data; |
| 2485 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2486 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2487 | if (prev_char == '^') |
| 2488 | ex_keep_indent = TRUE; |
| 2489 | indent = 0; |
| 2490 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2491 | } |
| 2492 | else |
| 2493 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2494 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2495 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2496 | if (indent > 0) |
| 2497 | { |
| 2498 | --indent; |
| 2499 | indent -= indent % get_sw_value(curbuf); |
| 2500 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2501 | } |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2502 | while (get_indent_str(p, 8, FALSE) > indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2503 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2504 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2505 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2506 | --line_ga.ga_len; |
| 2507 | } |
| 2508 | goto add_indent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2509 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2510 | |
| 2511 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2512 | { |
| 2513 | escaped = TRUE; |
| 2514 | continue; |
| 2515 | } |
| 2516 | |
| 2517 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2518 | if (IS_SPECIAL(c1)) |
| 2519 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2520 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2521 | |
| 2522 | if (IS_SPECIAL(c1)) |
| 2523 | c1 = '?'; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2524 | #ifdef FEAT_MBYTE |
| 2525 | if (has_mbyte) |
| 2526 | len = (*mb_char2bytes)(c1, |
| 2527 | (char_u *)line_ga.ga_data + line_ga.ga_len); |
| 2528 | else |
| 2529 | #endif |
| 2530 | { |
| 2531 | len = 1; |
| 2532 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
| 2533 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2534 | if (c1 == '\n') |
| 2535 | msg_putchar('\n'); |
| 2536 | else if (c1 == TAB) |
| 2537 | { |
| 2538 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2539 | do |
| 2540 | { |
| 2541 | msg_putchar(' '); |
| 2542 | } while (++vcol % 8); |
| 2543 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2544 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2545 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2546 | msg_outtrans_len( |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2547 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, len); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2548 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2549 | } |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2550 | line_ga.ga_len += len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2551 | escaped = FALSE; |
| 2552 | |
| 2553 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2554 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2555 | |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2556 | /* We are done when a NL is entered, but not when it comes after an |
| 2557 | * odd number of backslashes, that results in a NUL. */ |
| 2558 | if (line_ga.ga_len > 0 && pend[-1] == '\n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2559 | { |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2560 | int bcount = 0; |
| 2561 | |
| 2562 | while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') |
| 2563 | ++bcount; |
| 2564 | |
| 2565 | if (bcount > 0) |
| 2566 | { |
| 2567 | /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> |
| 2568 | * "\NL", etc. */ |
| 2569 | line_ga.ga_len -= (bcount + 1) / 2; |
| 2570 | pend -= (bcount + 1) / 2; |
| 2571 | pend[-1] = '\n'; |
| 2572 | } |
| 2573 | |
| 2574 | if ((bcount & 1) == 0) |
| 2575 | { |
| 2576 | --line_ga.ga_len; |
| 2577 | --pend; |
| 2578 | *pend = NUL; |
| 2579 | break; |
| 2580 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2581 | } |
| 2582 | } |
| 2583 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2584 | --no_mapping; |
| 2585 | --allow_keys; |
| 2586 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2587 | /* make following messages go to the next line */ |
| 2588 | msg_didout = FALSE; |
| 2589 | msg_col = 0; |
| 2590 | if (msg_row < Rows - 1) |
| 2591 | ++msg_row; |
| 2592 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 2593 | |
| 2594 | if (got_int) |
| 2595 | ga_clear(&line_ga); |
| 2596 | |
| 2597 | return (char_u *)line_ga.ga_data; |
| 2598 | } |
| 2599 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2600 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 2601 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2602 | /* |
| 2603 | * Return TRUE if ccline.overstrike is on. |
| 2604 | */ |
| 2605 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2606 | cmdline_overstrike(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2607 | { |
| 2608 | return ccline.overstrike; |
| 2609 | } |
| 2610 | |
| 2611 | /* |
| 2612 | * Return TRUE if the cursor is at the end of the cmdline. |
| 2613 | */ |
| 2614 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2615 | cmdline_at_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2616 | { |
| 2617 | return (ccline.cmdpos >= ccline.cmdlen); |
| 2618 | } |
| 2619 | #endif |
| 2620 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2621 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2622 | /* |
| 2623 | * Return the virtual column number at the current cursor position. |
| 2624 | * This is used by the IM code to obtain the start of the preedit string. |
| 2625 | */ |
| 2626 | colnr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2627 | cmdline_getvcol_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2628 | { |
| 2629 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 2630 | return MAXCOL; |
| 2631 | |
| 2632 | # ifdef FEAT_MBYTE |
| 2633 | if (has_mbyte) |
| 2634 | { |
| 2635 | colnr_T col; |
| 2636 | int i = 0; |
| 2637 | |
| 2638 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2639 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2640 | |
| 2641 | return col; |
| 2642 | } |
| 2643 | else |
| 2644 | # endif |
| 2645 | return ccline.cmdpos; |
| 2646 | } |
| 2647 | #endif |
| 2648 | |
| 2649 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2650 | /* |
| 2651 | * If part of the command line is an IM preedit string, redraw it with |
| 2652 | * IM feedback attributes. The cursor position is restored after drawing. |
| 2653 | */ |
| 2654 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2655 | redrawcmd_preedit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2656 | { |
| 2657 | if ((State & CMDLINE) |
| 2658 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 2659 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2660 | && !p_imdisable |
| 2661 | && im_is_preediting()) |
| 2662 | { |
| 2663 | int cmdpos = 0; |
| 2664 | int cmdspos; |
| 2665 | int old_row; |
| 2666 | int old_col; |
| 2667 | colnr_T col; |
| 2668 | |
| 2669 | old_row = msg_row; |
| 2670 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2671 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2672 | |
| 2673 | # ifdef FEAT_MBYTE |
| 2674 | if (has_mbyte) |
| 2675 | { |
| 2676 | for (col = 0; col < preedit_start_col |
| 2677 | && cmdpos < ccline.cmdlen; ++col) |
| 2678 | { |
| 2679 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2680 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2681 | } |
| 2682 | } |
| 2683 | else |
| 2684 | # endif |
| 2685 | { |
| 2686 | cmdspos += preedit_start_col; |
| 2687 | cmdpos += preedit_start_col; |
| 2688 | } |
| 2689 | |
| 2690 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 2691 | msg_col = cmdspos % (int)Columns; |
| 2692 | if (msg_row >= Rows) |
| 2693 | msg_row = Rows - 1; |
| 2694 | |
| 2695 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 2696 | { |
| 2697 | int char_len; |
| 2698 | int char_attr; |
| 2699 | |
| 2700 | char_attr = im_get_feedback_attr(col); |
| 2701 | if (char_attr < 0) |
| 2702 | break; /* end of preedit string */ |
| 2703 | |
| 2704 | # ifdef FEAT_MBYTE |
| 2705 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2706 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2707 | else |
| 2708 | # endif |
| 2709 | char_len = 1; |
| 2710 | |
| 2711 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 2712 | cmdpos += char_len; |
| 2713 | } |
| 2714 | |
| 2715 | msg_row = old_row; |
| 2716 | msg_col = old_col; |
| 2717 | } |
| 2718 | } |
| 2719 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 2720 | |
| 2721 | /* |
| 2722 | * Allocate a new command line buffer. |
| 2723 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
| 2724 | * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. |
| 2725 | */ |
| 2726 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2727 | alloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2728 | { |
| 2729 | /* |
| 2730 | * give some extra space to avoid having to allocate all the time |
| 2731 | */ |
| 2732 | if (len < 80) |
| 2733 | len = 100; |
| 2734 | else |
| 2735 | len += 20; |
| 2736 | |
| 2737 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 2738 | ccline.cmdbufflen = len; |
| 2739 | } |
| 2740 | |
| 2741 | /* |
| 2742 | * Re-allocate the command line to length len + something extra. |
| 2743 | * return FAIL for failure, OK otherwise |
| 2744 | */ |
| 2745 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2746 | realloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2747 | { |
| 2748 | char_u *p; |
| 2749 | |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 2750 | if (len < ccline.cmdbufflen) |
| 2751 | return OK; /* no need to resize */ |
| 2752 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2753 | p = ccline.cmdbuff; |
| 2754 | alloc_cmdbuff(len); /* will get some more */ |
| 2755 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 2756 | { |
| 2757 | ccline.cmdbuff = p; /* keep the old one */ |
| 2758 | return FAIL; |
| 2759 | } |
Bram Moolenaar | 35a3423 | 2010-08-13 16:51:26 +0200 | [diff] [blame] | 2760 | /* There isn't always a NUL after the command, but it may need to be |
| 2761 | * there, thus copy up to the NUL and add a NUL. */ |
| 2762 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
| 2763 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2764 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2765 | |
| 2766 | if (ccline.xpc != NULL |
| 2767 | && ccline.xpc->xp_pattern != NULL |
| 2768 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 2769 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 2770 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 2771 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2772 | |
| 2773 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 2774 | * to point into the newly allocated memory. */ |
| 2775 | if (i >= 0 && i <= ccline.cmdlen) |
| 2776 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 2777 | } |
| 2778 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2779 | return OK; |
| 2780 | } |
| 2781 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2782 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 2783 | static char_u *arshape_buf = NULL; |
| 2784 | |
| 2785 | # if defined(EXITFREE) || defined(PROTO) |
| 2786 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2787 | free_cmdline_buf(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2788 | { |
| 2789 | vim_free(arshape_buf); |
| 2790 | } |
| 2791 | # endif |
| 2792 | #endif |
| 2793 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2794 | /* |
| 2795 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 2796 | * when cmdline_star is TRUE. |
| 2797 | */ |
| 2798 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2799 | draw_cmdline(int start, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2800 | { |
| 2801 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2802 | int i; |
| 2803 | |
| 2804 | if (cmdline_star > 0) |
| 2805 | for (i = 0; i < len; ++i) |
| 2806 | { |
| 2807 | msg_putchar('*'); |
| 2808 | # ifdef FEAT_MBYTE |
| 2809 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2810 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2811 | # endif |
| 2812 | } |
| 2813 | else |
| 2814 | #endif |
| 2815 | #ifdef FEAT_ARABIC |
| 2816 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 2817 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2818 | static int buflen = 0; |
| 2819 | char_u *p; |
| 2820 | int j; |
| 2821 | int newlen = 0; |
| 2822 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 2823 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2824 | int prev_c = 0; |
| 2825 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2826 | int u8c; |
| 2827 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2828 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2829 | |
| 2830 | /* |
| 2831 | * Do arabic shaping into a temporary buffer. This is very |
| 2832 | * inefficient! |
| 2833 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2834 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2835 | { |
| 2836 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 2837 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2838 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2839 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2840 | arshape_buf = alloc(buflen); |
| 2841 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2842 | return; /* out of memory */ |
| 2843 | } |
| 2844 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2845 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 2846 | { |
| 2847 | /* Prepend a space to draw the leading composing char on. */ |
| 2848 | arshape_buf[0] = ' '; |
| 2849 | newlen = 1; |
| 2850 | } |
| 2851 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2852 | for (j = start; j < start + len; j += mb_l) |
| 2853 | { |
| 2854 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2855 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2856 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2857 | if (ARABIC_CHAR(u8c)) |
| 2858 | { |
| 2859 | /* Do Arabic shaping. */ |
| 2860 | if (cmdmsg_rl) |
| 2861 | { |
| 2862 | /* displaying from right to left */ |
| 2863 | pc = prev_c; |
| 2864 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2865 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2866 | if (j + mb_l >= start + len) |
| 2867 | nc = NUL; |
| 2868 | else |
| 2869 | nc = utf_ptr2char(p + mb_l); |
| 2870 | } |
| 2871 | else |
| 2872 | { |
| 2873 | /* displaying from left to right */ |
| 2874 | if (j + mb_l >= start + len) |
| 2875 | pc = NUL; |
| 2876 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2877 | { |
| 2878 | int pcc[MAX_MCO]; |
| 2879 | |
| 2880 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2881 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2882 | pc1 = pcc[0]; |
| 2883 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2884 | nc = prev_c; |
| 2885 | } |
| 2886 | prev_c = u8c; |
| 2887 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2888 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2889 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2890 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2891 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2892 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2893 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 2894 | if (u8cc[1] != 0) |
| 2895 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2896 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2897 | } |
| 2898 | } |
| 2899 | else |
| 2900 | { |
| 2901 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2902 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2903 | newlen += mb_l; |
| 2904 | } |
| 2905 | } |
| 2906 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2907 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2908 | } |
| 2909 | else |
| 2910 | #endif |
| 2911 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 2912 | } |
| 2913 | |
| 2914 | /* |
| 2915 | * Put a character on the command line. Shifts the following text to the |
| 2916 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 2917 | * "c" must be printable (fit in one display cell)! |
| 2918 | */ |
| 2919 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2920 | putcmdline(int c, int shift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2921 | { |
| 2922 | if (cmd_silent) |
| 2923 | return; |
| 2924 | msg_no_more = TRUE; |
| 2925 | msg_putchar(c); |
| 2926 | if (shift) |
| 2927 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2928 | msg_no_more = FALSE; |
| 2929 | cursorcmd(); |
| 2930 | } |
| 2931 | |
| 2932 | /* |
| 2933 | * Undo a putcmdline(c, FALSE). |
| 2934 | */ |
| 2935 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2936 | unputcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2937 | { |
| 2938 | if (cmd_silent) |
| 2939 | return; |
| 2940 | msg_no_more = TRUE; |
| 2941 | if (ccline.cmdlen == ccline.cmdpos) |
| 2942 | msg_putchar(' '); |
Bram Moolenaar | 64fdf5c | 2012-06-06 12:03:06 +0200 | [diff] [blame] | 2943 | #ifdef FEAT_MBYTE |
| 2944 | else if (has_mbyte) |
| 2945 | draw_cmdline(ccline.cmdpos, |
| 2946 | (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); |
| 2947 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2948 | else |
| 2949 | draw_cmdline(ccline.cmdpos, 1); |
| 2950 | msg_no_more = FALSE; |
| 2951 | cursorcmd(); |
| 2952 | } |
| 2953 | |
| 2954 | /* |
| 2955 | * Put the given string, of the given length, onto the command line. |
| 2956 | * If len is -1, then STRLEN() is used to calculate the length. |
| 2957 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 2958 | * part will be redrawn, otherwise it will not. If this function is called |
| 2959 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 2960 | * called afterwards. |
| 2961 | */ |
| 2962 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2963 | put_on_cmdline(char_u *str, int len, int redraw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2964 | { |
| 2965 | int retval; |
| 2966 | int i; |
| 2967 | int m; |
| 2968 | int c; |
| 2969 | |
| 2970 | if (len < 0) |
| 2971 | len = (int)STRLEN(str); |
| 2972 | |
| 2973 | /* Check if ccline.cmdbuff needs to be longer */ |
| 2974 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 2975 | retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2976 | else |
| 2977 | retval = OK; |
| 2978 | if (retval == OK) |
| 2979 | { |
| 2980 | if (!ccline.overstrike) |
| 2981 | { |
| 2982 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 2983 | ccline.cmdbuff + ccline.cmdpos, |
| 2984 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 2985 | ccline.cmdlen += len; |
| 2986 | } |
| 2987 | else |
| 2988 | { |
| 2989 | #ifdef FEAT_MBYTE |
| 2990 | if (has_mbyte) |
| 2991 | { |
| 2992 | /* Count nr of characters in the new string. */ |
| 2993 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2994 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2995 | ++m; |
| 2996 | /* Count nr of bytes in cmdline that are overwritten by these |
| 2997 | * characters. */ |
| 2998 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2999 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3000 | --m; |
| 3001 | if (i < ccline.cmdlen) |
| 3002 | { |
| 3003 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 3004 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 3005 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 3006 | } |
| 3007 | else |
| 3008 | ccline.cmdlen = ccline.cmdpos + len; |
| 3009 | } |
| 3010 | else |
| 3011 | #endif |
| 3012 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 3013 | ccline.cmdlen = ccline.cmdpos + len; |
| 3014 | } |
| 3015 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 3016 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 3017 | |
| 3018 | #ifdef FEAT_MBYTE |
| 3019 | if (enc_utf8) |
| 3020 | { |
| 3021 | /* When the inserted text starts with a composing character, |
| 3022 | * backup to the character before it. There could be two of them. |
| 3023 | */ |
| 3024 | i = 0; |
| 3025 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3026 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 3027 | { |
| 3028 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3029 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3030 | ccline.cmdpos -= i; |
| 3031 | len += i; |
| 3032 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 3033 | } |
| 3034 | # ifdef FEAT_ARABIC |
| 3035 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 3036 | { |
| 3037 | /* Check the previous character for Arabic combining pair. */ |
| 3038 | i = (*mb_head_off)(ccline.cmdbuff, |
| 3039 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 3040 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 3041 | + ccline.cmdpos - i), c)) |
| 3042 | { |
| 3043 | ccline.cmdpos -= i; |
| 3044 | len += i; |
| 3045 | } |
| 3046 | else |
| 3047 | i = 0; |
| 3048 | } |
| 3049 | # endif |
| 3050 | if (i != 0) |
| 3051 | { |
| 3052 | /* Also backup the cursor position. */ |
| 3053 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 3054 | ccline.cmdspos -= i; |
| 3055 | msg_col -= i; |
| 3056 | if (msg_col < 0) |
| 3057 | { |
| 3058 | msg_col += Columns; |
| 3059 | --msg_row; |
| 3060 | } |
| 3061 | } |
| 3062 | } |
| 3063 | #endif |
| 3064 | |
| 3065 | if (redraw && !cmd_silent) |
| 3066 | { |
| 3067 | msg_no_more = TRUE; |
| 3068 | i = cmdline_row; |
Bram Moolenaar | 73dc59a | 2011-09-30 17:46:21 +0200 | [diff] [blame] | 3069 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3070 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3071 | /* Avoid clearing the rest of the line too often. */ |
| 3072 | if (cmdline_row != i || ccline.overstrike) |
| 3073 | msg_clr_eos(); |
| 3074 | msg_no_more = FALSE; |
| 3075 | } |
| 3076 | #ifdef FEAT_FKMAP |
| 3077 | /* |
| 3078 | * If we are in Farsi command mode, the character input must be in |
| 3079 | * Insert mode. So do not advance the cmdpos. |
| 3080 | */ |
| 3081 | if (!cmd_fkmap) |
| 3082 | #endif |
| 3083 | { |
| 3084 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3085 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3086 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3087 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 3088 | m = MAXCOL; |
| 3089 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3090 | else |
| 3091 | m = MAXCOL; |
| 3092 | for (i = 0; i < len; ++i) |
| 3093 | { |
| 3094 | c = cmdline_charsize(ccline.cmdpos); |
| 3095 | #ifdef FEAT_MBYTE |
| 3096 | /* count ">" for a double-wide char that doesn't fit. */ |
| 3097 | if (has_mbyte) |
| 3098 | correct_cmdspos(ccline.cmdpos, c); |
| 3099 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3100 | /* Stop cursor at the end of the screen, but do increment the |
| 3101 | * insert position, so that entering a very long command |
| 3102 | * works, even though you can't see it. */ |
| 3103 | if (ccline.cmdspos + c < m) |
| 3104 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3105 | #ifdef FEAT_MBYTE |
| 3106 | if (has_mbyte) |
| 3107 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3108 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3109 | if (c > len - i - 1) |
| 3110 | c = len - i - 1; |
| 3111 | ccline.cmdpos += c; |
| 3112 | i += c; |
| 3113 | } |
| 3114 | #endif |
| 3115 | ++ccline.cmdpos; |
| 3116 | } |
| 3117 | } |
| 3118 | } |
| 3119 | if (redraw) |
| 3120 | msg_check(); |
| 3121 | return retval; |
| 3122 | } |
| 3123 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3124 | static struct cmdline_info prev_ccline; |
| 3125 | static int prev_ccline_used = FALSE; |
| 3126 | |
| 3127 | /* |
| 3128 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 3129 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 3130 | * available globally in prev_ccline. |
| 3131 | */ |
| 3132 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3133 | save_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3134 | { |
| 3135 | if (!prev_ccline_used) |
| 3136 | { |
| 3137 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 3138 | prev_ccline_used = TRUE; |
| 3139 | } |
| 3140 | *ccp = prev_ccline; |
| 3141 | prev_ccline = ccline; |
| 3142 | ccline.cmdbuff = NULL; |
| 3143 | ccline.cmdprompt = NULL; |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3144 | ccline.xpc = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3145 | } |
| 3146 | |
| 3147 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3148 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3149 | */ |
| 3150 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3151 | restore_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3152 | { |
| 3153 | ccline = prev_ccline; |
| 3154 | prev_ccline = *ccp; |
| 3155 | } |
| 3156 | |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3157 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3158 | /* |
| 3159 | * Save the command line into allocated memory. Returns a pointer to be |
| 3160 | * passed to restore_cmdline_alloc() later. |
| 3161 | * Returns NULL when failed. |
| 3162 | */ |
| 3163 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3164 | save_cmdline_alloc(void) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3165 | { |
| 3166 | struct cmdline_info *p; |
| 3167 | |
| 3168 | p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); |
| 3169 | if (p != NULL) |
| 3170 | save_cmdline(p); |
| 3171 | return (char_u *)p; |
| 3172 | } |
| 3173 | |
| 3174 | /* |
| 3175 | * Restore the command line from the return value of save_cmdline_alloc(). |
| 3176 | */ |
| 3177 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3178 | restore_cmdline_alloc(char_u *p) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3179 | { |
| 3180 | if (p != NULL) |
| 3181 | { |
| 3182 | restore_cmdline((struct cmdline_info *)p); |
| 3183 | vim_free(p); |
| 3184 | } |
| 3185 | } |
| 3186 | #endif |
| 3187 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3188 | /* |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3189 | * Paste a yank register into the command line. |
| 3190 | * Used by CTRL-R command in command-line mode. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3191 | * insert_reg() can't be used here, because special characters from the |
| 3192 | * register contents will be interpreted as commands. |
| 3193 | * |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3194 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3195 | */ |
| 3196 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3197 | cmdline_paste( |
| 3198 | int regname, |
| 3199 | int literally, /* Insert text literally instead of "as typed" */ |
| 3200 | int remcr) /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3201 | { |
| 3202 | long i; |
| 3203 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3204 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3205 | int allocated; |
| 3206 | struct cmdline_info save_ccline; |
| 3207 | |
| 3208 | /* check for valid regname; also accept special characters for CTRL-R in |
| 3209 | * the command line */ |
| 3210 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
| 3211 | && regname != Ctrl_A && !valid_yank_reg(regname, FALSE)) |
| 3212 | return FAIL; |
| 3213 | |
| 3214 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 3215 | * CTRL-C to break the loop. */ |
| 3216 | line_breakcheck(); |
| 3217 | if (got_int) |
| 3218 | return FAIL; |
| 3219 | |
| 3220 | #ifdef FEAT_CLIPBOARD |
| 3221 | regname = may_get_selection(regname); |
| 3222 | #endif |
| 3223 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3224 | /* Need to save and restore ccline. And set "textlock" to avoid nasty |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3225 | * things like going to another buffer when evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3226 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3227 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3228 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3229 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3230 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3231 | |
| 3232 | if (i) |
| 3233 | { |
| 3234 | /* Got the value of a special register in "arg". */ |
| 3235 | if (arg == NULL) |
| 3236 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3237 | |
| 3238 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3239 | * part of the word. */ |
| 3240 | p = arg; |
| 3241 | if (p_is && regname == Ctrl_W) |
| 3242 | { |
| 3243 | char_u *w; |
| 3244 | int len; |
| 3245 | |
| 3246 | /* Locate start of last word in the cmd buffer. */ |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3247 | for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3248 | { |
| 3249 | #ifdef FEAT_MBYTE |
| 3250 | if (has_mbyte) |
| 3251 | { |
| 3252 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3253 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3254 | break; |
| 3255 | w -= len; |
| 3256 | } |
| 3257 | else |
| 3258 | #endif |
| 3259 | { |
| 3260 | if (!vim_iswordc(w[-1])) |
| 3261 | break; |
| 3262 | --w; |
| 3263 | } |
| 3264 | } |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3265 | len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3266 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3267 | p += len; |
| 3268 | } |
| 3269 | |
| 3270 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3271 | if (allocated) |
| 3272 | vim_free(arg); |
| 3273 | return OK; |
| 3274 | } |
| 3275 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3276 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
| 3279 | /* |
| 3280 | * Put a string on the command line. |
| 3281 | * When "literally" is TRUE, insert literally. |
| 3282 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3283 | * line. |
| 3284 | */ |
| 3285 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3286 | cmdline_paste_str(char_u *s, int literally) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3287 | { |
| 3288 | int c, cv; |
| 3289 | |
| 3290 | if (literally) |
| 3291 | put_on_cmdline(s, -1, TRUE); |
| 3292 | else |
| 3293 | while (*s != NUL) |
| 3294 | { |
| 3295 | cv = *s; |
| 3296 | if (cv == Ctrl_V && s[1]) |
| 3297 | ++s; |
| 3298 | #ifdef FEAT_MBYTE |
| 3299 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3300 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3301 | else |
| 3302 | #endif |
| 3303 | c = *s++; |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 3304 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
| 3305 | || c == CAR || c == NL || c == Ctrl_L |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3306 | #ifdef UNIX |
| 3307 | || c == intr_char |
| 3308 | #endif |
| 3309 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3310 | stuffcharReadbuff(Ctrl_V); |
| 3311 | stuffcharReadbuff(c); |
| 3312 | } |
| 3313 | } |
| 3314 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3315 | #ifdef FEAT_WILDMENU |
| 3316 | /* |
| 3317 | * Delete characters on the command line, from "from" to the current |
| 3318 | * position. |
| 3319 | */ |
| 3320 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3321 | cmdline_del(int from) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3322 | { |
| 3323 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3324 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3325 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3326 | ccline.cmdpos = from; |
| 3327 | } |
| 3328 | #endif |
| 3329 | |
| 3330 | /* |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 3331 | * This function is called when the screen size changes and with incremental |
| 3332 | * search and in other situations where the command line may have been |
| 3333 | * overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3334 | */ |
| 3335 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3336 | redrawcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3337 | { |
| 3338 | if (cmd_silent) |
| 3339 | return; |
| 3340 | need_wait_return = FALSE; |
| 3341 | compute_cmdrow(); |
| 3342 | redrawcmd(); |
| 3343 | cursorcmd(); |
| 3344 | } |
| 3345 | |
| 3346 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3347 | redrawcmdprompt(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3348 | { |
| 3349 | int i; |
| 3350 | |
| 3351 | if (cmd_silent) |
| 3352 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3353 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3354 | msg_putchar(ccline.cmdfirstc); |
| 3355 | if (ccline.cmdprompt != NULL) |
| 3356 | { |
| 3357 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3358 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3359 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3360 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3361 | --ccline.cmdindent; |
| 3362 | } |
| 3363 | else |
| 3364 | for (i = ccline.cmdindent; i > 0; --i) |
| 3365 | msg_putchar(' '); |
| 3366 | } |
| 3367 | |
| 3368 | /* |
| 3369 | * Redraw what is currently on the command line. |
| 3370 | */ |
| 3371 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3372 | redrawcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3373 | { |
| 3374 | if (cmd_silent) |
| 3375 | return; |
| 3376 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3377 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3378 | if (ccline.cmdbuff == NULL) |
| 3379 | { |
| 3380 | windgoto(cmdline_row, 0); |
| 3381 | msg_clr_eos(); |
| 3382 | return; |
| 3383 | } |
| 3384 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3385 | msg_start(); |
| 3386 | redrawcmdprompt(); |
| 3387 | |
| 3388 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3389 | msg_no_more = TRUE; |
| 3390 | draw_cmdline(0, ccline.cmdlen); |
| 3391 | msg_clr_eos(); |
| 3392 | msg_no_more = FALSE; |
| 3393 | |
| 3394 | set_cmdspos_cursor(); |
| 3395 | |
| 3396 | /* |
| 3397 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3398 | * in cmdline mode we can reset them now. |
| 3399 | */ |
| 3400 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3401 | |
| 3402 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3403 | * in cmdline mode */ |
| 3404 | skip_redraw = FALSE; |
| 3405 | } |
| 3406 | |
| 3407 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3408 | compute_cmdrow(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3409 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3410 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3411 | cmdline_row = Rows - 1; |
| 3412 | else |
| 3413 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
| 3414 | + W_STATUS_HEIGHT(lastwin); |
| 3415 | } |
| 3416 | |
| 3417 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3418 | cursorcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3419 | { |
| 3420 | if (cmd_silent) |
| 3421 | return; |
| 3422 | |
| 3423 | #ifdef FEAT_RIGHTLEFT |
| 3424 | if (cmdmsg_rl) |
| 3425 | { |
| 3426 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3427 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3428 | if (msg_row <= 0) |
| 3429 | msg_row = Rows - 1; |
| 3430 | } |
| 3431 | else |
| 3432 | #endif |
| 3433 | { |
| 3434 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3435 | msg_col = ccline.cmdspos % (int)Columns; |
| 3436 | if (msg_row >= Rows) |
| 3437 | msg_row = Rows - 1; |
| 3438 | } |
| 3439 | |
| 3440 | windgoto(msg_row, msg_col); |
| 3441 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 3442 | redrawcmd_preedit(); |
| 3443 | #endif |
| 3444 | #ifdef MCH_CURSOR_SHAPE |
| 3445 | mch_update_cursor(); |
| 3446 | #endif |
| 3447 | } |
| 3448 | |
| 3449 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3450 | gotocmdline(int clr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3451 | { |
| 3452 | msg_start(); |
| 3453 | #ifdef FEAT_RIGHTLEFT |
| 3454 | if (cmdmsg_rl) |
| 3455 | msg_col = Columns - 1; |
| 3456 | else |
| 3457 | #endif |
| 3458 | msg_col = 0; /* always start in column 0 */ |
| 3459 | if (clr) /* clear the bottom line(s) */ |
| 3460 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3461 | windgoto(cmdline_row, 0); |
| 3462 | } |
| 3463 | |
| 3464 | /* |
| 3465 | * Check the word in front of the cursor for an abbreviation. |
| 3466 | * Called when the non-id character "c" has been entered. |
| 3467 | * When an abbreviation is recognized it is removed from the text with |
| 3468 | * backspaces and the replacement string is inserted, followed by "c". |
| 3469 | */ |
| 3470 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3471 | ccheck_abbr(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3472 | { |
| 3473 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3474 | return FALSE; |
| 3475 | |
| 3476 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0); |
| 3477 | } |
| 3478 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3479 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 3480 | static int |
| 3481 | #ifdef __BORLANDC__ |
| 3482 | _RTLENTRYF |
| 3483 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3484 | sort_func_compare(const void *s1, const void *s2) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3485 | { |
| 3486 | char_u *p1 = *(char_u **)s1; |
| 3487 | char_u *p2 = *(char_u **)s2; |
| 3488 | |
| 3489 | if (*p1 != '<' && *p2 == '<') return -1; |
| 3490 | if (*p1 == '<' && *p2 != '<') return 1; |
| 3491 | return STRCMP(p1, p2); |
| 3492 | } |
| 3493 | #endif |
| 3494 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3495 | /* |
| 3496 | * Return FAIL if this is not an appropriate context in which to do |
| 3497 | * completion of anything, return OK if it is (even if there are no matches). |
| 3498 | * For the caller, this means that the character is just passed through like a |
| 3499 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3500 | */ |
| 3501 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3502 | nextwild( |
| 3503 | expand_T *xp, |
| 3504 | int type, |
| 3505 | int options, /* extra options for ExpandOne() */ |
| 3506 | int escape) /* if TRUE, escape the returned matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3507 | { |
| 3508 | int i, j; |
| 3509 | char_u *p1; |
| 3510 | char_u *p2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3511 | int difflen; |
| 3512 | int v; |
| 3513 | |
| 3514 | if (xp->xp_numfiles == -1) |
| 3515 | { |
| 3516 | set_expand_context(xp); |
| 3517 | cmd_showtail = expand_showtail(xp); |
| 3518 | } |
| 3519 | |
| 3520 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3521 | { |
| 3522 | beep_flush(); |
| 3523 | return OK; /* Something illegal on command line */ |
| 3524 | } |
| 3525 | if (xp->xp_context == EXPAND_NOTHING) |
| 3526 | { |
| 3527 | /* Caller can use the character as a normal char instead */ |
| 3528 | return FAIL; |
| 3529 | } |
| 3530 | |
| 3531 | MSG_PUTS("..."); /* show that we are busy */ |
| 3532 | out_flush(); |
| 3533 | |
| 3534 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3535 | xp->xp_pattern_len = ccline.cmdpos - i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3536 | |
| 3537 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3538 | { |
| 3539 | /* |
| 3540 | * Get next/previous match for a previous expanded pattern. |
| 3541 | */ |
| 3542 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3543 | } |
| 3544 | else |
| 3545 | { |
| 3546 | /* |
| 3547 | * Translate string into pattern and expand it. |
| 3548 | */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3549 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 3550 | xp->xp_context)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3551 | p2 = NULL; |
| 3552 | else |
| 3553 | { |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3554 | int use_options = options | |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 3555 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 3556 | if (escape) |
| 3557 | use_options |= WILD_ESCAPE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3558 | |
| 3559 | if (p_wic) |
| 3560 | use_options += WILD_ICASE; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3561 | p2 = ExpandOne(xp, p1, |
| 3562 | vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3563 | use_options, type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3564 | vim_free(p1); |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3565 | /* longest match: make sure it is not shorter, happens with :help */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3566 | if (p2 != NULL && type == WILD_LONGEST) |
| 3567 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3568 | for (j = 0; j < xp->xp_pattern_len; ++j) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3569 | if (ccline.cmdbuff[i + j] == '*' |
| 3570 | || ccline.cmdbuff[i + j] == '?') |
| 3571 | break; |
| 3572 | if ((int)STRLEN(p2) < j) |
| 3573 | { |
| 3574 | vim_free(p2); |
| 3575 | p2 = NULL; |
| 3576 | } |
| 3577 | } |
| 3578 | } |
| 3579 | } |
| 3580 | |
| 3581 | if (p2 != NULL && !got_int) |
| 3582 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3583 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3584 | if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3585 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3586 | v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3587 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3588 | } |
| 3589 | else |
| 3590 | v = OK; |
| 3591 | if (v == OK) |
| 3592 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3593 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 3594 | &ccline.cmdbuff[ccline.cmdpos], |
| 3595 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3596 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3597 | ccline.cmdlen += difflen; |
| 3598 | ccline.cmdpos += difflen; |
| 3599 | } |
| 3600 | } |
| 3601 | vim_free(p2); |
| 3602 | |
| 3603 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3604 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3605 | |
| 3606 | /* When expanding a ":map" command and no matches are found, assume that |
| 3607 | * the key is supposed to be inserted literally */ |
| 3608 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 3609 | return FAIL; |
| 3610 | |
| 3611 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 3612 | beep_flush(); |
| 3613 | else if (xp->xp_numfiles == 1) |
| 3614 | /* free expanded pattern */ |
| 3615 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 3616 | |
| 3617 | return OK; |
| 3618 | } |
| 3619 | |
| 3620 | /* |
| 3621 | * Do wildcard expansion on the string 'str'. |
| 3622 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3623 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3624 | * Return NULL for failure. |
| 3625 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3626 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 3627 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 3628 | * WILD_PREV "orig" should be NULL. |
| 3629 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 3630 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 3631 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3632 | * |
| 3633 | * mode = WILD_FREE: just free previously expanded matches |
| 3634 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 3635 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 3636 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 3637 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 3638 | * mode = WILD_ALL: return all matches concatenated |
| 3639 | * mode = WILD_LONGEST: return longest matched part |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3640 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3641 | * |
| 3642 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 3643 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 3644 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 3645 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 3646 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 3647 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 3648 | * options = WILD_SILENT: don't print warning messages |
| 3649 | * options = WILD_ESCAPE: put backslash before special chars |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3650 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3651 | * |
| 3652 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 3653 | */ |
| 3654 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3655 | ExpandOne( |
| 3656 | expand_T *xp, |
| 3657 | char_u *str, |
| 3658 | char_u *orig, /* allocated copy of original of expanded string */ |
| 3659 | int options, |
| 3660 | int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3661 | { |
| 3662 | char_u *ss = NULL; |
| 3663 | static int findex; |
| 3664 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3665 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3666 | int i; |
| 3667 | long_u len; |
| 3668 | int non_suf_match; /* number without matching suffix */ |
| 3669 | |
| 3670 | /* |
| 3671 | * first handle the case of using an old match |
| 3672 | */ |
| 3673 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 3674 | { |
| 3675 | if (xp->xp_numfiles > 0) |
| 3676 | { |
| 3677 | if (mode == WILD_PREV) |
| 3678 | { |
| 3679 | if (findex == -1) |
| 3680 | findex = xp->xp_numfiles; |
| 3681 | --findex; |
| 3682 | } |
| 3683 | else /* mode == WILD_NEXT */ |
| 3684 | ++findex; |
| 3685 | |
| 3686 | /* |
| 3687 | * When wrapping around, return the original string, set findex to |
| 3688 | * -1. |
| 3689 | */ |
| 3690 | if (findex < 0) |
| 3691 | { |
| 3692 | if (orig_save == NULL) |
| 3693 | findex = xp->xp_numfiles - 1; |
| 3694 | else |
| 3695 | findex = -1; |
| 3696 | } |
| 3697 | if (findex >= xp->xp_numfiles) |
| 3698 | { |
| 3699 | if (orig_save == NULL) |
| 3700 | findex = 0; |
| 3701 | else |
| 3702 | findex = -1; |
| 3703 | } |
| 3704 | #ifdef FEAT_WILDMENU |
| 3705 | if (p_wmnu) |
| 3706 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 3707 | findex, cmd_showtail); |
| 3708 | #endif |
| 3709 | if (findex == -1) |
| 3710 | return vim_strsave(orig_save); |
| 3711 | return vim_strsave(xp->xp_files[findex]); |
| 3712 | } |
| 3713 | else |
| 3714 | return NULL; |
| 3715 | } |
| 3716 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3717 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3718 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 3719 | { |
| 3720 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3721 | xp->xp_numfiles = -1; |
| 3722 | vim_free(orig_save); |
| 3723 | orig_save = NULL; |
| 3724 | } |
| 3725 | findex = 0; |
| 3726 | |
| 3727 | if (mode == WILD_FREE) /* only release file name */ |
| 3728 | return NULL; |
| 3729 | |
| 3730 | if (xp->xp_numfiles == -1) |
| 3731 | { |
| 3732 | vim_free(orig_save); |
| 3733 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3734 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3735 | |
| 3736 | /* |
| 3737 | * Do the expansion. |
| 3738 | */ |
| 3739 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 3740 | options) == FAIL) |
| 3741 | { |
| 3742 | #ifdef FNAME_ILLEGAL |
| 3743 | /* Illegal file name has been silently skipped. But when there |
| 3744 | * are wildcards, the real problem is that there was no match, |
| 3745 | * causing the pattern to be added, which has illegal characters. |
| 3746 | */ |
| 3747 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 3748 | EMSG2(_(e_nomatch2), str); |
| 3749 | #endif |
| 3750 | } |
| 3751 | else if (xp->xp_numfiles == 0) |
| 3752 | { |
| 3753 | if (!(options & WILD_SILENT)) |
| 3754 | EMSG2(_(e_nomatch2), str); |
| 3755 | } |
| 3756 | else |
| 3757 | { |
| 3758 | /* Escape the matches for use on the command line. */ |
| 3759 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 3760 | |
| 3761 | /* |
| 3762 | * Check for matching suffixes in file names. |
| 3763 | */ |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3764 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 3765 | && mode != WILD_LONGEST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3766 | { |
| 3767 | if (xp->xp_numfiles) |
| 3768 | non_suf_match = xp->xp_numfiles; |
| 3769 | else |
| 3770 | non_suf_match = 1; |
| 3771 | if ((xp->xp_context == EXPAND_FILES |
| 3772 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3773 | && xp->xp_numfiles > 1) |
| 3774 | { |
| 3775 | /* |
| 3776 | * More than one match; check suffix. |
| 3777 | * The files will have been sorted on matching suffix in |
| 3778 | * expand_wildcards, only need to check the first two. |
| 3779 | */ |
| 3780 | non_suf_match = 0; |
| 3781 | for (i = 0; i < 2; ++i) |
| 3782 | if (match_suffix(xp->xp_files[i])) |
| 3783 | ++non_suf_match; |
| 3784 | } |
| 3785 | if (non_suf_match != 1) |
| 3786 | { |
| 3787 | /* Can we ever get here unless it's while expanding |
| 3788 | * interactively? If not, we can get rid of this all |
| 3789 | * together. Don't really want to wait for this message |
| 3790 | * (and possibly have to hit return to continue!). |
| 3791 | */ |
| 3792 | if (!(options & WILD_SILENT)) |
| 3793 | EMSG(_(e_toomany)); |
| 3794 | else if (!(options & WILD_NO_BEEP)) |
| 3795 | beep_flush(); |
| 3796 | } |
| 3797 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 3798 | ss = vim_strsave(xp->xp_files[0]); |
| 3799 | } |
| 3800 | } |
| 3801 | } |
| 3802 | |
| 3803 | /* Find longest common part */ |
| 3804 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 3805 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3806 | int mb_len = 1; |
| 3807 | int c0, ci; |
| 3808 | |
| 3809 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3810 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3811 | #ifdef FEAT_MBYTE |
| 3812 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3813 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3814 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 3815 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 3816 | } |
| 3817 | else |
| 3818 | #endif |
Bram Moolenaar | e4eda3b | 2015-11-21 16:28:50 +0100 | [diff] [blame] | 3819 | c0 = xp->xp_files[0][len]; |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3820 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 3821 | { |
| 3822 | #ifdef FEAT_MBYTE |
| 3823 | if (has_mbyte) |
| 3824 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 3825 | else |
| 3826 | #endif |
| 3827 | ci = xp->xp_files[i][len]; |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3828 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3829 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3830 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3831 | || xp->xp_context == EXPAND_BUFFERS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3832 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3833 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3834 | break; |
| 3835 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3836 | else if (c0 != ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3837 | break; |
| 3838 | } |
| 3839 | if (i < xp->xp_numfiles) |
| 3840 | { |
| 3841 | if (!(options & WILD_NO_BEEP)) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3842 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3843 | break; |
| 3844 | } |
| 3845 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3846 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3847 | ss = alloc((unsigned)len + 1); |
| 3848 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3849 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3850 | findex = -1; /* next p_wc gets first one */ |
| 3851 | } |
| 3852 | |
| 3853 | /* Concatenate all matching names */ |
| 3854 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 3855 | { |
| 3856 | len = 0; |
| 3857 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3858 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 3859 | ss = lalloc(len, TRUE); |
| 3860 | if (ss != NULL) |
| 3861 | { |
| 3862 | *ss = NUL; |
| 3863 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3864 | { |
| 3865 | STRCAT(ss, xp->xp_files[i]); |
| 3866 | if (i != xp->xp_numfiles - 1) |
| 3867 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 3868 | } |
| 3869 | } |
| 3870 | } |
| 3871 | |
| 3872 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 3873 | ExpandCleanup(xp); |
| 3874 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3875 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3876 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3877 | vim_free(orig); |
| 3878 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3879 | return ss; |
| 3880 | } |
| 3881 | |
| 3882 | /* |
| 3883 | * Prepare an expand structure for use. |
| 3884 | */ |
| 3885 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3886 | ExpandInit(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3887 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3888 | xp->xp_pattern = NULL; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3889 | xp->xp_pattern_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3890 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3891 | #ifndef BACKSLASH_IN_FILENAME |
| 3892 | xp->xp_shell = FALSE; |
| 3893 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3894 | xp->xp_numfiles = -1; |
| 3895 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3896 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 3897 | xp->xp_arg = NULL; |
| 3898 | #endif |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 3899 | xp->xp_line = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3900 | } |
| 3901 | |
| 3902 | /* |
| 3903 | * Cleanup an expand structure after use. |
| 3904 | */ |
| 3905 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3906 | ExpandCleanup(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3907 | { |
| 3908 | if (xp->xp_numfiles >= 0) |
| 3909 | { |
| 3910 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3911 | xp->xp_numfiles = -1; |
| 3912 | } |
| 3913 | } |
| 3914 | |
| 3915 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3916 | ExpandEscape( |
| 3917 | expand_T *xp, |
| 3918 | char_u *str, |
| 3919 | int numfiles, |
| 3920 | char_u **files, |
| 3921 | int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3922 | { |
| 3923 | int i; |
| 3924 | char_u *p; |
| 3925 | |
| 3926 | /* |
| 3927 | * May change home directory back to "~" |
| 3928 | */ |
| 3929 | if (options & WILD_HOME_REPLACE) |
| 3930 | tilde_replace(str, numfiles, files); |
| 3931 | |
| 3932 | if (options & WILD_ESCAPE) |
| 3933 | { |
| 3934 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | cca92ec | 2011-04-28 17:21:53 +0200 | [diff] [blame] | 3935 | || xp->xp_context == EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3936 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3937 | || xp->xp_context == EXPAND_BUFFERS |
| 3938 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3939 | { |
| 3940 | /* |
| 3941 | * Insert a backslash into a file name before a space, \, %, # |
| 3942 | * and wildmatch characters, except '~'. |
| 3943 | */ |
| 3944 | for (i = 0; i < numfiles; ++i) |
| 3945 | { |
| 3946 | /* for ":set path=" we need to escape spaces twice */ |
| 3947 | if (xp->xp_backslash == XP_BS_THREE) |
| 3948 | { |
| 3949 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 3950 | if (p != NULL) |
| 3951 | { |
| 3952 | vim_free(files[i]); |
| 3953 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 3954 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3955 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 3956 | if (p != NULL) |
| 3957 | { |
| 3958 | vim_free(files[i]); |
| 3959 | files[i] = p; |
| 3960 | } |
| 3961 | #endif |
| 3962 | } |
| 3963 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 3964 | #ifdef BACKSLASH_IN_FILENAME |
| 3965 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 3966 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3967 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 3968 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3969 | if (p != NULL) |
| 3970 | { |
| 3971 | vim_free(files[i]); |
| 3972 | files[i] = p; |
| 3973 | } |
| 3974 | |
| 3975 | /* If 'str' starts with "\~", replace "~" at start of |
| 3976 | * files[i] with "\~". */ |
| 3977 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3978 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3979 | } |
| 3980 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3981 | |
| 3982 | /* If the first file starts with a '+' escape it. Otherwise it |
| 3983 | * could be seen as "+cmd". */ |
| 3984 | if (*files[0] == '+') |
| 3985 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3986 | } |
| 3987 | else if (xp->xp_context == EXPAND_TAGS) |
| 3988 | { |
| 3989 | /* |
| 3990 | * Insert a backslash before characters in a tag name that |
| 3991 | * would terminate the ":tag" command. |
| 3992 | */ |
| 3993 | for (i = 0; i < numfiles; ++i) |
| 3994 | { |
| 3995 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 3996 | if (p != NULL) |
| 3997 | { |
| 3998 | vim_free(files[i]); |
| 3999 | files[i] = p; |
| 4000 | } |
| 4001 | } |
| 4002 | } |
| 4003 | } |
| 4004 | } |
| 4005 | |
| 4006 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4007 | * Escape special characters in "fname" for when used as a file name argument |
| 4008 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 4009 | * Returns the result in allocated memory. |
| 4010 | */ |
| 4011 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4012 | vim_strsave_fnameescape(char_u *fname, int shell) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4013 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4014 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4015 | #ifdef BACKSLASH_IN_FILENAME |
| 4016 | char_u buf[20]; |
| 4017 | int j = 0; |
| 4018 | |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4019 | /* Don't escape '[', '{' and '!' if they are in 'isfname'. */ |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4020 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 4021 | if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p)) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4022 | buf[j++] = *p; |
| 4023 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4024 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4025 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 4026 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 4027 | if (shell && csh_like_shell() && p != NULL) |
| 4028 | { |
| 4029 | char_u *s; |
| 4030 | |
| 4031 | /* For csh and similar shells need to put two backslashes before '!'. |
| 4032 | * One is taken by Vim, one by the shell. */ |
| 4033 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 4034 | vim_free(p); |
| 4035 | p = s; |
| 4036 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4037 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4038 | |
| 4039 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 4040 | * ":write". "cd -" has a special meaning. */ |
Bram Moolenaar | a9d52e3 | 2010-07-31 16:44:19 +0200 | [diff] [blame] | 4041 | if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 4042 | escape_fname(&p); |
| 4043 | |
| 4044 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 4045 | } |
| 4046 | |
| 4047 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4048 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 4049 | */ |
| 4050 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4051 | escape_fname(char_u **pp) |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4052 | { |
| 4053 | char_u *p; |
| 4054 | |
| 4055 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 4056 | if (p != NULL) |
| 4057 | { |
| 4058 | p[0] = '\\'; |
| 4059 | STRCPY(p + 1, *pp); |
| 4060 | vim_free(*pp); |
| 4061 | *pp = p; |
| 4062 | } |
| 4063 | } |
| 4064 | |
| 4065 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4066 | * For each file name in files[num_files]: |
| 4067 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 4068 | */ |
| 4069 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4070 | tilde_replace( |
| 4071 | char_u *orig_pat, |
| 4072 | int num_files, |
| 4073 | char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4074 | { |
| 4075 | int i; |
| 4076 | char_u *p; |
| 4077 | |
| 4078 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 4079 | { |
| 4080 | for (i = 0; i < num_files; ++i) |
| 4081 | { |
| 4082 | p = home_replace_save(NULL, files[i]); |
| 4083 | if (p != NULL) |
| 4084 | { |
| 4085 | vim_free(files[i]); |
| 4086 | files[i] = p; |
| 4087 | } |
| 4088 | } |
| 4089 | } |
| 4090 | } |
| 4091 | |
| 4092 | /* |
| 4093 | * Show all matches for completion on the command line. |
| 4094 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 4095 | * be inserted like a normal character. |
| 4096 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4097 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4098 | showmatches(expand_T *xp, int wildmenu UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4099 | { |
| 4100 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 4101 | int num_files; |
| 4102 | char_u **files_found; |
| 4103 | int i, j, k; |
| 4104 | int maxlen; |
| 4105 | int lines; |
| 4106 | int columns; |
| 4107 | char_u *p; |
| 4108 | int lastlen; |
| 4109 | int attr; |
| 4110 | int showtail; |
| 4111 | |
| 4112 | if (xp->xp_numfiles == -1) |
| 4113 | { |
| 4114 | set_expand_context(xp); |
| 4115 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 4116 | &num_files, &files_found); |
| 4117 | showtail = expand_showtail(xp); |
| 4118 | if (i != EXPAND_OK) |
| 4119 | return i; |
| 4120 | |
| 4121 | } |
| 4122 | else |
| 4123 | { |
| 4124 | num_files = xp->xp_numfiles; |
| 4125 | files_found = xp->xp_files; |
| 4126 | showtail = cmd_showtail; |
| 4127 | } |
| 4128 | |
| 4129 | #ifdef FEAT_WILDMENU |
| 4130 | if (!wildmenu) |
| 4131 | { |
| 4132 | #endif |
| 4133 | msg_didany = FALSE; /* lines_left will be set */ |
| 4134 | msg_start(); /* prepare for paging */ |
| 4135 | msg_putchar('\n'); |
| 4136 | out_flush(); |
| 4137 | cmdline_row = msg_row; |
| 4138 | msg_didany = FALSE; /* lines_left will be set again */ |
| 4139 | msg_start(); /* prepare for paging */ |
| 4140 | #ifdef FEAT_WILDMENU |
| 4141 | } |
| 4142 | #endif |
| 4143 | |
| 4144 | if (got_int) |
| 4145 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 4146 | #ifdef FEAT_WILDMENU |
| 4147 | else if (wildmenu) |
| 4148 | win_redr_status_matches(xp, num_files, files_found, 0, showtail); |
| 4149 | #endif |
| 4150 | else |
| 4151 | { |
| 4152 | /* find the length of the longest file name */ |
| 4153 | maxlen = 0; |
| 4154 | for (i = 0; i < num_files; ++i) |
| 4155 | { |
| 4156 | if (!showtail && (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4157 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4158 | || xp->xp_context == EXPAND_BUFFERS)) |
| 4159 | { |
| 4160 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 4161 | j = vim_strsize(NameBuff); |
| 4162 | } |
| 4163 | else |
| 4164 | j = vim_strsize(L_SHOWFILE(i)); |
| 4165 | if (j > maxlen) |
| 4166 | maxlen = j; |
| 4167 | } |
| 4168 | |
| 4169 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4170 | lines = num_files; |
| 4171 | else |
| 4172 | { |
| 4173 | /* compute the number of columns and lines for the listing */ |
| 4174 | maxlen += 2; /* two spaces between file names */ |
| 4175 | columns = ((int)Columns + 2) / maxlen; |
| 4176 | if (columns < 1) |
| 4177 | columns = 1; |
| 4178 | lines = (num_files + columns - 1) / columns; |
| 4179 | } |
| 4180 | |
| 4181 | attr = hl_attr(HLF_D); /* find out highlighting for directories */ |
| 4182 | |
| 4183 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4184 | { |
| 4185 | MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T)); |
| 4186 | msg_clr_eos(); |
| 4187 | msg_advance(maxlen - 3); |
| 4188 | MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T)); |
| 4189 | } |
| 4190 | |
| 4191 | /* list the files line by line */ |
| 4192 | for (i = 0; i < lines; ++i) |
| 4193 | { |
| 4194 | lastlen = 999; |
| 4195 | for (k = i; k < num_files; k += lines) |
| 4196 | { |
| 4197 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4198 | { |
| 4199 | msg_outtrans_attr(files_found[k], hl_attr(HLF_D)); |
| 4200 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 4201 | msg_advance(maxlen + 1); |
| 4202 | msg_puts(p); |
| 4203 | msg_advance(maxlen + 3); |
| 4204 | msg_puts_long_attr(p + 2, hl_attr(HLF_D)); |
| 4205 | break; |
| 4206 | } |
| 4207 | for (j = maxlen - lastlen; --j >= 0; ) |
| 4208 | msg_putchar(' '); |
| 4209 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4210 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4211 | || xp->xp_context == EXPAND_BUFFERS) |
| 4212 | { |
Bram Moolenaar | b91e59b | 2010-03-17 19:13:27 +0100 | [diff] [blame] | 4213 | /* highlight directories */ |
Bram Moolenaar | 63fa526 | 2010-03-23 18:06:52 +0100 | [diff] [blame] | 4214 | if (xp->xp_numfiles != -1) |
| 4215 | { |
| 4216 | char_u *halved_slash; |
| 4217 | char_u *exp_path; |
| 4218 | |
| 4219 | /* Expansion was done before and special characters |
| 4220 | * were escaped, need to halve backslashes. Also |
| 4221 | * $HOME has been replaced with ~/. */ |
| 4222 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
| 4223 | halved_slash = backslash_halve_save( |
| 4224 | exp_path != NULL ? exp_path : files_found[k]); |
| 4225 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 4226 | : files_found[k]); |
| 4227 | vim_free(exp_path); |
| 4228 | vim_free(halved_slash); |
| 4229 | } |
| 4230 | else |
| 4231 | /* Expansion was done here, file names are literal. */ |
| 4232 | j = mch_isdir(files_found[k]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4233 | if (showtail) |
| 4234 | p = L_SHOWFILE(k); |
| 4235 | else |
| 4236 | { |
| 4237 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 4238 | TRUE); |
| 4239 | p = NameBuff; |
| 4240 | } |
| 4241 | } |
| 4242 | else |
| 4243 | { |
| 4244 | j = FALSE; |
| 4245 | p = L_SHOWFILE(k); |
| 4246 | } |
| 4247 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 4248 | } |
| 4249 | if (msg_col > 0) /* when not wrapped around */ |
| 4250 | { |
| 4251 | msg_clr_eos(); |
| 4252 | msg_putchar('\n'); |
| 4253 | } |
| 4254 | out_flush(); /* show one line at a time */ |
| 4255 | if (got_int) |
| 4256 | { |
| 4257 | got_int = FALSE; |
| 4258 | break; |
| 4259 | } |
| 4260 | } |
| 4261 | |
| 4262 | /* |
| 4263 | * we redraw the command below the lines that we have just listed |
| 4264 | * This is a bit tricky, but it saves a lot of screen updating. |
| 4265 | */ |
| 4266 | cmdline_row = msg_row; /* will put it back later */ |
| 4267 | } |
| 4268 | |
| 4269 | if (xp->xp_numfiles == -1) |
| 4270 | FreeWild(num_files, files_found); |
| 4271 | |
| 4272 | return EXPAND_OK; |
| 4273 | } |
| 4274 | |
| 4275 | /* |
| 4276 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 4277 | * Find tail of file name path, but ignore trailing "/". |
| 4278 | */ |
| 4279 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4280 | sm_gettail(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4281 | { |
| 4282 | char_u *p; |
| 4283 | char_u *t = s; |
| 4284 | int had_sep = FALSE; |
| 4285 | |
| 4286 | for (p = s; *p != NUL; ) |
| 4287 | { |
| 4288 | if (vim_ispathsep(*p) |
| 4289 | #ifdef BACKSLASH_IN_FILENAME |
| 4290 | && !rem_backslash(p) |
| 4291 | #endif |
| 4292 | ) |
| 4293 | had_sep = TRUE; |
| 4294 | else if (had_sep) |
| 4295 | { |
| 4296 | t = p; |
| 4297 | had_sep = FALSE; |
| 4298 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4299 | mb_ptr_adv(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4300 | } |
| 4301 | return t; |
| 4302 | } |
| 4303 | |
| 4304 | /* |
| 4305 | * Return TRUE if we only need to show the tail of completion matches. |
| 4306 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4307 | * returned. |
| 4308 | */ |
| 4309 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4310 | expand_showtail(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4311 | { |
| 4312 | char_u *s; |
| 4313 | char_u *end; |
| 4314 | |
| 4315 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4316 | if (xp->xp_context != EXPAND_FILES |
| 4317 | && xp->xp_context != EXPAND_SHELLCMD |
| 4318 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4319 | return FALSE; |
| 4320 | |
| 4321 | end = gettail(xp->xp_pattern); |
| 4322 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4323 | return FALSE; |
| 4324 | |
| 4325 | for (s = xp->xp_pattern; s < end; s++) |
| 4326 | { |
| 4327 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4328 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4329 | if (rem_backslash(s)) |
| 4330 | ++s; |
| 4331 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4332 | return FALSE; |
| 4333 | } |
| 4334 | return TRUE; |
| 4335 | } |
| 4336 | |
| 4337 | /* |
| 4338 | * Prepare a string for expansion. |
| 4339 | * When expanding file names: The string will be used with expand_wildcards(). |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4340 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4341 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4342 | * the name into allocated memory and prepend "^". |
| 4343 | */ |
| 4344 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4345 | addstar( |
| 4346 | char_u *fname, |
| 4347 | int len, |
| 4348 | int context) /* EXPAND_FILES etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4349 | { |
| 4350 | char_u *retval; |
| 4351 | int i, j; |
| 4352 | int new_len; |
| 4353 | char_u *tail; |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4354 | int ends_in_star; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4355 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4356 | if (context != EXPAND_FILES |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4357 | && context != EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4358 | && context != EXPAND_SHELLCMD |
| 4359 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4360 | { |
| 4361 | /* |
| 4362 | * Matching will be done internally (on something other than files). |
| 4363 | * So we convert the file-matching-type wildcards into our kind for |
| 4364 | * use with vim_regcomp(). First work out how long it will be: |
| 4365 | */ |
| 4366 | |
| 4367 | /* For help tags the translation is done in find_help_tags(). |
| 4368 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4369 | if (context == EXPAND_HELP |
| 4370 | || context == EXPAND_COLORS |
| 4371 | || context == EXPAND_COMPILER |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4372 | || context == EXPAND_OWNSYNTAX |
Bram Moolenaar | 883f5d0 | 2010-06-21 06:24:34 +0200 | [diff] [blame] | 4373 | || context == EXPAND_FILETYPE |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4374 | || context == EXPAND_PACKADD |
Bram Moolenaar | ba47b51 | 2017-01-24 21:18:19 +0100 | [diff] [blame] | 4375 | || ((context == EXPAND_TAGS_LISTFILES |
| 4376 | || context == EXPAND_TAGS) |
| 4377 | && fname[0] == '/')) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4378 | retval = vim_strnsave(fname, len); |
| 4379 | else |
| 4380 | { |
| 4381 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4382 | for (i = 0; i < len; i++) |
| 4383 | { |
| 4384 | if (fname[i] == '*' || fname[i] == '~') |
| 4385 | new_len++; /* '*' needs to be replaced by ".*" |
| 4386 | '~' needs to be replaced by "\~" */ |
| 4387 | |
| 4388 | /* Buffer names are like file names. "." should be literal */ |
| 4389 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4390 | new_len++; /* "." becomes "\." */ |
| 4391 | |
| 4392 | /* Custom expansion takes care of special things, match |
| 4393 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4394 | if ((context == EXPAND_USER_DEFINED |
| 4395 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4396 | new_len++; /* '\' becomes "\\" */ |
| 4397 | } |
| 4398 | retval = alloc(new_len); |
| 4399 | if (retval != NULL) |
| 4400 | { |
| 4401 | retval[0] = '^'; |
| 4402 | j = 1; |
| 4403 | for (i = 0; i < len; i++, j++) |
| 4404 | { |
| 4405 | /* Skip backslash. But why? At least keep it for custom |
| 4406 | * expansion. */ |
| 4407 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4408 | && context != EXPAND_USER_LIST |
| 4409 | && fname[i] == '\\' |
| 4410 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4411 | break; |
| 4412 | |
| 4413 | switch (fname[i]) |
| 4414 | { |
| 4415 | case '*': retval[j++] = '.'; |
| 4416 | break; |
| 4417 | case '~': retval[j++] = '\\'; |
| 4418 | break; |
| 4419 | case '?': retval[j] = '.'; |
| 4420 | continue; |
| 4421 | case '.': if (context == EXPAND_BUFFERS) |
| 4422 | retval[j++] = '\\'; |
| 4423 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4424 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4425 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4426 | retval[j++] = '\\'; |
| 4427 | break; |
| 4428 | } |
| 4429 | retval[j] = fname[i]; |
| 4430 | } |
| 4431 | retval[j] = NUL; |
| 4432 | } |
| 4433 | } |
| 4434 | } |
| 4435 | else |
| 4436 | { |
| 4437 | retval = alloc(len + 4); |
| 4438 | if (retval != NULL) |
| 4439 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4440 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4441 | |
| 4442 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4443 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4444 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4445 | * ~ would be at the start of the file name, but not the tail. |
| 4446 | * $ could be anywhere in the tail. |
| 4447 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4448 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4449 | */ |
| 4450 | tail = gettail(retval); |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4451 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 4452 | #ifndef BACKSLASH_IN_FILENAME |
| 4453 | for (i = len - 2; i >= 0; --i) |
| 4454 | { |
| 4455 | if (retval[i] != '\\') |
| 4456 | break; |
| 4457 | ends_in_star = !ends_in_star; |
| 4458 | } |
| 4459 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4460 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4461 | && !ends_in_star |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4462 | && vim_strchr(tail, '$') == NULL |
| 4463 | && vim_strchr(retval, '`') == NULL) |
| 4464 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4465 | else if (len > 0 && retval[len - 1] == '$') |
| 4466 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4467 | retval[len] = NUL; |
| 4468 | } |
| 4469 | } |
| 4470 | return retval; |
| 4471 | } |
| 4472 | |
| 4473 | /* |
| 4474 | * Must parse the command line so far to work out what context we are in. |
| 4475 | * Completion can then be done based on that context. |
| 4476 | * This routine sets the variables: |
| 4477 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4478 | * the command line (ends at the cursor). |
| 4479 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4480 | * |
| 4481 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4482 | * the command line, like an unknown command. Caller |
| 4483 | * should beep. |
| 4484 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4485 | * a normal char, rather than for completion. eg |
| 4486 | * :s/^I/ |
| 4487 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4488 | * it. |
| 4489 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4490 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4491 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4492 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4493 | * when we know only directories are of interest. eg |
| 4494 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4495 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4496 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4497 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4498 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4499 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4500 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4501 | * EXPAND_EVENTS Complete event names |
| 4502 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4503 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4504 | * EXPAND_AUGROUP Complete autocommand group names |
| 4505 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4506 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4507 | * eg :unmap a^I , :cunab x^I |
| 4508 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4509 | * eg :call sub^I |
| 4510 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4511 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4512 | * names in expressions, eg :while s^I |
| 4513 | * EXPAND_ENV_VARS Complete environment variable names |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4514 | * EXPAND_USER Complete user names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4515 | */ |
| 4516 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4517 | set_expand_context(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4518 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4519 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4520 | if (ccline.cmdfirstc != ':' |
| 4521 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4522 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4523 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4524 | #endif |
| 4525 | ) |
| 4526 | { |
| 4527 | xp->xp_context = EXPAND_NOTHING; |
| 4528 | return; |
| 4529 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4530 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4531 | } |
| 4532 | |
| 4533 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4534 | set_cmd_context( |
| 4535 | expand_T *xp, |
| 4536 | char_u *str, /* start of command line */ |
| 4537 | int len, /* length of command line (excl. NUL) */ |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4538 | int col, /* position of cursor */ |
| 4539 | int use_ccline UNUSED) /* use ccline for info */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4540 | { |
| 4541 | int old_char = NUL; |
| 4542 | char_u *nextcomm; |
| 4543 | |
| 4544 | /* |
| 4545 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4546 | * written before. |
| 4547 | */ |
| 4548 | if (col < len) |
| 4549 | old_char = str[col]; |
| 4550 | str[col] = NUL; |
| 4551 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4552 | |
| 4553 | #ifdef FEAT_EVAL |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4554 | if (use_ccline && ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4555 | { |
| 4556 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4557 | /* pass CMD_SIZE because there is no real command */ |
| 4558 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4559 | # endif |
| 4560 | } |
Bram Moolenaar | 33a80ee | 2016-09-05 21:51:14 +0200 | [diff] [blame] | 4561 | else if (use_ccline && ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4562 | { |
| 4563 | xp->xp_context = ccline.xp_context; |
| 4564 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4565 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4566 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4567 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4568 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4569 | else |
| 4570 | #endif |
| 4571 | while (nextcomm != NULL) |
| 4572 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4573 | |
Bram Moolenaar | a4c8dcb | 2013-06-30 12:21:24 +0200 | [diff] [blame] | 4574 | /* Store the string here so that call_user_expand_func() can get to them |
| 4575 | * easily. */ |
| 4576 | xp->xp_line = str; |
| 4577 | xp->xp_col = col; |
| 4578 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4579 | str[col] = old_char; |
| 4580 | } |
| 4581 | |
| 4582 | /* |
| 4583 | * Expand the command line "str" from context "xp". |
| 4584 | * "xp" must have been set by set_cmd_context(). |
| 4585 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4586 | * starts. |
| 4587 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4588 | * cursor. |
| 4589 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 4590 | * key that triggered expansion literally. |
| 4591 | * Returns EXPAND_OK otherwise. |
| 4592 | */ |
| 4593 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4594 | expand_cmdline( |
| 4595 | expand_T *xp, |
| 4596 | char_u *str, /* start of command line */ |
| 4597 | int col, /* position of cursor */ |
| 4598 | int *matchcount, /* return: nr of matches */ |
| 4599 | char_u ***matches) /* return: array of pointers to matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4600 | { |
| 4601 | char_u *file_str = NULL; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4602 | int options = WILD_ADD_SLASH|WILD_SILENT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4603 | |
| 4604 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 4605 | { |
| 4606 | beep_flush(); |
| 4607 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 4608 | } |
| 4609 | if (xp->xp_context == EXPAND_NOTHING) |
| 4610 | { |
| 4611 | /* Caller can use the character as a normal char instead */ |
| 4612 | return EXPAND_NOTHING; |
| 4613 | } |
| 4614 | |
| 4615 | /* 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] | 4616 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 4617 | 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] | 4618 | if (file_str == NULL) |
| 4619 | return EXPAND_UNSUCCESSFUL; |
| 4620 | |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4621 | if (p_wic) |
| 4622 | options += WILD_ICASE; |
| 4623 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4624 | /* find all files that match the description */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4625 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4626 | { |
| 4627 | *matchcount = 0; |
| 4628 | *matches = NULL; |
| 4629 | } |
| 4630 | vim_free(file_str); |
| 4631 | |
| 4632 | return EXPAND_OK; |
| 4633 | } |
| 4634 | |
| 4635 | #ifdef FEAT_MULTI_LANG |
| 4636 | /* |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4637 | * Cleanup matches for help tags: |
| 4638 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 4639 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4640 | */ |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 4641 | static void cleanup_help_tags(int num_file, char_u **file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4642 | |
| 4643 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4644 | cleanup_help_tags(int num_file, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4645 | { |
| 4646 | int i, j; |
| 4647 | int len; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4648 | char_u buf[4]; |
| 4649 | char_u *p = buf; |
| 4650 | |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4651 | 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] | 4652 | { |
| 4653 | *p++ = '@'; |
| 4654 | *p++ = p_hlg[0]; |
| 4655 | *p++ = p_hlg[1]; |
| 4656 | } |
| 4657 | *p = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4658 | |
| 4659 | for (i = 0; i < num_file; ++i) |
| 4660 | { |
| 4661 | len = (int)STRLEN(file[i]) - 3; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4662 | if (len <= 0) |
| 4663 | continue; |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4664 | if (STRCMP(file[i] + len, "@en") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4665 | { |
| 4666 | /* Sorting on priority means the same item in another language may |
| 4667 | * be anywhere. Search all items for a match up to the "@en". */ |
| 4668 | for (j = 0; j < num_file; ++j) |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4669 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 4670 | && STRNCMP(file[i], file[j], len + 1) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4671 | break; |
| 4672 | if (j == num_file) |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4673 | /* item only exists with @en, remove it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4674 | file[i][len] = NUL; |
| 4675 | } |
| 4676 | } |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4677 | |
| 4678 | if (*buf != NUL) |
| 4679 | for (i = 0; i < num_file; ++i) |
| 4680 | { |
| 4681 | len = (int)STRLEN(file[i]) - 3; |
| 4682 | if (len <= 0) |
| 4683 | continue; |
| 4684 | if (STRCMP(file[i] + len, buf) == 0) |
| 4685 | { |
| 4686 | /* remove the default language */ |
| 4687 | file[i][len] = NUL; |
| 4688 | } |
| 4689 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4690 | } |
| 4691 | #endif |
| 4692 | |
| 4693 | /* |
| 4694 | * Do the expansion based on xp->xp_context and "pat". |
| 4695 | */ |
| 4696 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4697 | ExpandFromContext( |
| 4698 | expand_T *xp, |
| 4699 | char_u *pat, |
| 4700 | int *num_file, |
| 4701 | char_u ***file, |
| 4702 | int options) /* EW_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4703 | { |
| 4704 | #ifdef FEAT_CMDL_COMPL |
| 4705 | regmatch_T regmatch; |
| 4706 | #endif |
| 4707 | int ret; |
| 4708 | int flags; |
| 4709 | |
| 4710 | flags = EW_DIR; /* include directories */ |
| 4711 | if (options & WILD_LIST_NOTFOUND) |
| 4712 | flags |= EW_NOTFOUND; |
| 4713 | if (options & WILD_ADD_SLASH) |
| 4714 | flags |= EW_ADDSLASH; |
| 4715 | if (options & WILD_KEEP_ALL) |
| 4716 | flags |= EW_KEEPALL; |
| 4717 | if (options & WILD_SILENT) |
| 4718 | flags |= EW_SILENT; |
Bram Moolenaar | a245bc7 | 2015-03-05 19:35:25 +0100 | [diff] [blame] | 4719 | if (options & WILD_ALLLINKS) |
| 4720 | flags |= EW_ALLLINKS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4721 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4722 | if (xp->xp_context == EXPAND_FILES |
| 4723 | || xp->xp_context == EXPAND_DIRECTORIES |
| 4724 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4725 | { |
| 4726 | /* |
| 4727 | * Expand file or directory names. |
| 4728 | */ |
| 4729 | int free_pat = FALSE; |
| 4730 | int i; |
| 4731 | |
| 4732 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 4733 | * space */ |
| 4734 | if (xp->xp_backslash != XP_BS_NONE) |
| 4735 | { |
| 4736 | free_pat = TRUE; |
| 4737 | pat = vim_strsave(pat); |
| 4738 | for (i = 0; pat[i]; ++i) |
| 4739 | if (pat[i] == '\\') |
| 4740 | { |
| 4741 | if (xp->xp_backslash == XP_BS_THREE |
| 4742 | && pat[i + 1] == '\\' |
| 4743 | && pat[i + 2] == '\\' |
| 4744 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4745 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4746 | if (xp->xp_backslash == XP_BS_ONE |
| 4747 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4748 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4749 | } |
| 4750 | } |
| 4751 | |
| 4752 | if (xp->xp_context == EXPAND_FILES) |
| 4753 | flags |= EW_FILE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4754 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 4755 | flags |= (EW_FILE | EW_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4756 | else |
| 4757 | flags = (flags | EW_DIR) & ~EW_FILE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4758 | if (options & WILD_ICASE) |
| 4759 | flags |= EW_ICASE; |
| 4760 | |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 4761 | /* Expand wildcards, supporting %:h and the like. */ |
| 4762 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4763 | if (free_pat) |
| 4764 | vim_free(pat); |
| 4765 | return ret; |
| 4766 | } |
| 4767 | |
| 4768 | *file = (char_u **)""; |
| 4769 | *num_file = 0; |
| 4770 | if (xp->xp_context == EXPAND_HELP) |
| 4771 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 4772 | /* With an empty argument we would get all the help tags, which is |
| 4773 | * very slow. Get matches for "help" instead. */ |
| 4774 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 4775 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4776 | { |
| 4777 | #ifdef FEAT_MULTI_LANG |
| 4778 | cleanup_help_tags(*num_file, *file); |
| 4779 | #endif |
| 4780 | return OK; |
| 4781 | } |
| 4782 | return FAIL; |
| 4783 | } |
| 4784 | |
| 4785 | #ifndef FEAT_CMDL_COMPL |
| 4786 | return FAIL; |
| 4787 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4788 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 4789 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4790 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 4791 | return ExpandOldSetting(num_file, file); |
| 4792 | if (xp->xp_context == EXPAND_BUFFERS) |
| 4793 | return ExpandBufnames(pat, num_file, file, options); |
| 4794 | if (xp->xp_context == EXPAND_TAGS |
| 4795 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4796 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 4797 | if (xp->xp_context == EXPAND_COLORS) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4798 | { |
| 4799 | char *directories[] = {"colors", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4800 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 4801 | directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4802 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4803 | if (xp->xp_context == EXPAND_COMPILER) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4804 | { |
Bram Moolenaar | a627c96 | 2011-09-30 16:23:32 +0200 | [diff] [blame] | 4805 | char *directories[] = {"compiler", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4806 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4807 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4808 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4809 | { |
| 4810 | char *directories[] = {"syntax", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4811 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4812 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4813 | if (xp->xp_context == EXPAND_FILETYPE) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4814 | { |
| 4815 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4816 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4817 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4818 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4819 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4820 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4821 | # endif |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4822 | if (xp->xp_context == EXPAND_PACKADD) |
| 4823 | return ExpandPackAddDir(pat, num_file, file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4824 | |
| 4825 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 4826 | if (regmatch.regprog == NULL) |
| 4827 | return FAIL; |
| 4828 | |
| 4829 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 4830 | regmatch.rm_ic = ignorecase(pat); |
| 4831 | |
| 4832 | if (xp->xp_context == EXPAND_SETTINGS |
| 4833 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 4834 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 4835 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 4836 | ret = ExpandMappings(®match, num_file, file); |
| 4837 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4838 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 4839 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 4840 | # endif |
| 4841 | else |
| 4842 | { |
| 4843 | static struct expgen |
| 4844 | { |
| 4845 | int context; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 4846 | char_u *((*func)(expand_T *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4847 | int ic; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4848 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4849 | } tab[] = |
| 4850 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4851 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 4852 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
Bram Moolenaar | 9e507ca | 2016-10-15 15:39:39 +0200 | [diff] [blame] | 4853 | {EXPAND_MESSAGES, get_messages_arg, TRUE, TRUE}, |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 4854 | #ifdef FEAT_CMDHIST |
| 4855 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 4856 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4857 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4858 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 4859 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4860 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 4861 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 4862 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4863 | #endif |
| 4864 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4865 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 4866 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 4867 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 4868 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4869 | #endif |
| 4870 | #ifdef FEAT_MENU |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4871 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 4872 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4873 | #endif |
| 4874 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4875 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4876 | #endif |
Bram Moolenaar | cd9c462 | 2013-06-08 15:24:48 +0200 | [diff] [blame] | 4877 | #ifdef FEAT_PROFILE |
| 4878 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 4879 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4880 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4881 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4882 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 4883 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4884 | #endif |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4885 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4886 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4887 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4888 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4889 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4890 | #endif |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 4891 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4892 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 4893 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4894 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 4895 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4896 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 4897 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4898 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4899 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4900 | {EXPAND_USER, get_users, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4901 | }; |
| 4902 | int i; |
| 4903 | |
| 4904 | /* |
| 4905 | * Find a context in the table and call the ExpandGeneric() with the |
| 4906 | * right function to do the expansion. |
| 4907 | */ |
| 4908 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 4909 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4910 | if (xp->xp_context == tab[i].context) |
| 4911 | { |
| 4912 | if (tab[i].ic) |
| 4913 | regmatch.rm_ic = TRUE; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4914 | ret = ExpandGeneric(xp, ®match, num_file, file, |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 4915 | tab[i].func, tab[i].escaped); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4916 | break; |
| 4917 | } |
| 4918 | } |
| 4919 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4920 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4921 | |
| 4922 | return ret; |
| 4923 | #endif /* FEAT_CMDL_COMPL */ |
| 4924 | } |
| 4925 | |
| 4926 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4927 | /* |
| 4928 | * Expand a list of names. |
| 4929 | * |
| 4930 | * Generic function for command line completion. It calls a function to |
| 4931 | * obtain strings, one by one. The strings are matched against a regexp |
| 4932 | * program. Matching strings are copied into an array, which is returned. |
| 4933 | * |
| 4934 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 4935 | */ |
| 4936 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4937 | ExpandGeneric( |
| 4938 | expand_T *xp, |
| 4939 | regmatch_T *regmatch, |
| 4940 | int *num_file, |
| 4941 | char_u ***file, |
| 4942 | char_u *((*func)(expand_T *, int)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4943 | /* returns a string from the list */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4944 | int escaped) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4945 | { |
| 4946 | int i; |
| 4947 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4948 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4949 | char_u *str; |
| 4950 | |
| 4951 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4952 | * round == 0: count the number of matching names |
| 4953 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4954 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4955 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4956 | { |
| 4957 | for (i = 0; ; ++i) |
| 4958 | { |
| 4959 | str = (*func)(xp, i); |
| 4960 | if (str == NULL) /* end of list */ |
| 4961 | break; |
| 4962 | if (*str == NUL) /* skip empty strings */ |
| 4963 | continue; |
| 4964 | |
| 4965 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 4966 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4967 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4968 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4969 | if (escaped) |
| 4970 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 4971 | else |
| 4972 | str = vim_strsave(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4973 | (*file)[count] = str; |
| 4974 | #ifdef FEAT_MENU |
| 4975 | if (func == get_menu_names && str != NULL) |
| 4976 | { |
| 4977 | /* test for separator added by get_menu_names() */ |
| 4978 | str += STRLEN(str) - 1; |
| 4979 | if (*str == '\001') |
| 4980 | *str = '.'; |
| 4981 | } |
| 4982 | #endif |
| 4983 | } |
| 4984 | ++count; |
| 4985 | } |
| 4986 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4987 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4988 | { |
| 4989 | if (count == 0) |
| 4990 | return OK; |
| 4991 | *num_file = count; |
| 4992 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 4993 | if (*file == NULL) |
| 4994 | { |
| 4995 | *file = (char_u **)""; |
| 4996 | return FAIL; |
| 4997 | } |
| 4998 | count = 0; |
| 4999 | } |
| 5000 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5001 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 5002 | /* Sort the results. Keep menu's in the specified order. */ |
| 5003 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 5004 | { |
| 5005 | if (xp->xp_context == EXPAND_EXPRESSION |
| 5006 | || xp->xp_context == EXPAND_FUNCTIONS |
| 5007 | || xp->xp_context == EXPAND_USER_FUNC) |
| 5008 | /* <SNR> functions should be sorted to the end. */ |
| 5009 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 5010 | sort_func_compare); |
| 5011 | else |
| 5012 | sort_strings(*file, *num_file); |
| 5013 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 5014 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 5015 | #ifdef FEAT_CMDL_COMPL |
| 5016 | /* Reset the variables used for special highlight names expansion, so that |
| 5017 | * they don't show up when getting normal highlight names by ID. */ |
| 5018 | reset_expand_highlight(); |
| 5019 | #endif |
| 5020 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5021 | return OK; |
| 5022 | } |
| 5023 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5024 | /* |
| 5025 | * Complete a shell command. |
| 5026 | * Returns FAIL or OK; |
| 5027 | */ |
| 5028 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5029 | expand_shellcmd( |
| 5030 | char_u *filepat, /* pattern to match with command names */ |
| 5031 | int *num_file, /* return: number of matches */ |
| 5032 | char_u ***file, /* return: array with matches */ |
| 5033 | int flagsarg) /* EW_ flags */ |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5034 | { |
| 5035 | char_u *pat; |
| 5036 | int i; |
| 5037 | char_u *path; |
| 5038 | int mustfree = FALSE; |
| 5039 | garray_T ga; |
| 5040 | char_u *buf = alloc(MAXPATHL); |
| 5041 | size_t l; |
| 5042 | char_u *s, *e; |
| 5043 | int flags = flagsarg; |
| 5044 | int ret; |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5045 | int did_curdir = FALSE; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5046 | |
| 5047 | if (buf == NULL) |
| 5048 | return FAIL; |
| 5049 | |
| 5050 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5051 | * space */ |
| 5052 | pat = vim_strsave(filepat); |
| 5053 | for (i = 0; pat[i]; ++i) |
| 5054 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5055 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5056 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5057 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5058 | |
| 5059 | /* For an absolute name we don't use $PATH. */ |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 5060 | if (mch_isFullName(pat)) |
| 5061 | path = (char_u *)" "; |
| 5062 | else if ((pat[0] == '.' && (vim_ispathsep(pat[1]) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5063 | || (pat[1] == '.' && vim_ispathsep(pat[2]))))) |
| 5064 | path = (char_u *)"."; |
| 5065 | else |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5066 | { |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5067 | path = vim_getenv((char_u *)"PATH", &mustfree); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5068 | if (path == NULL) |
| 5069 | path = (char_u *)""; |
| 5070 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5071 | |
| 5072 | /* |
| 5073 | * Go over all directories in $PATH. Expand matches in that directory and |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5074 | * collect them in "ga". When "." is not in $PATH also expand for the |
| 5075 | * current directory, to find "subdir/cmd". |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5076 | */ |
| 5077 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5078 | for (s = path; ; s = e) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5079 | { |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5080 | if (*s == NUL) |
| 5081 | { |
| 5082 | if (did_curdir) |
| 5083 | break; |
| 5084 | /* Find directories in the current directory, path is empty. */ |
| 5085 | did_curdir = TRUE; |
| 5086 | } |
| 5087 | else if (*s == '.') |
| 5088 | did_curdir = TRUE; |
| 5089 | |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 5090 | if (*s == ' ') |
| 5091 | ++s; /* Skip space used for absolute path name. */ |
| 5092 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5093 | #if defined(MSWIN) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5094 | e = vim_strchr(s, ';'); |
| 5095 | #else |
| 5096 | e = vim_strchr(s, ':'); |
| 5097 | #endif |
| 5098 | if (e == NULL) |
| 5099 | e = s + STRLEN(s); |
| 5100 | |
| 5101 | l = e - s; |
| 5102 | if (l > MAXPATHL - 5) |
| 5103 | break; |
| 5104 | vim_strncpy(buf, s, l); |
| 5105 | add_pathsep(buf); |
| 5106 | l = STRLEN(buf); |
| 5107 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 5108 | |
| 5109 | /* Expand matches in one directory of $PATH. */ |
| 5110 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 5111 | if (ret == OK) |
| 5112 | { |
| 5113 | if (ga_grow(&ga, *num_file) == FAIL) |
| 5114 | FreeWild(*num_file, *file); |
| 5115 | else |
| 5116 | { |
| 5117 | for (i = 0; i < *num_file; ++i) |
| 5118 | { |
| 5119 | s = (*file)[i]; |
| 5120 | if (STRLEN(s) > l) |
| 5121 | { |
| 5122 | /* Remove the path again. */ |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5123 | STRMOVE(s, s + l); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5124 | ((char_u **)ga.ga_data)[ga.ga_len++] = s; |
| 5125 | } |
| 5126 | else |
| 5127 | vim_free(s); |
| 5128 | } |
| 5129 | vim_free(*file); |
| 5130 | } |
| 5131 | } |
| 5132 | if (*e != NUL) |
| 5133 | ++e; |
| 5134 | } |
| 5135 | *file = ga.ga_data; |
| 5136 | *num_file = ga.ga_len; |
| 5137 | |
| 5138 | vim_free(buf); |
| 5139 | vim_free(pat); |
| 5140 | if (mustfree) |
| 5141 | vim_free(path); |
| 5142 | return OK; |
| 5143 | } |
| 5144 | |
| 5145 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5146 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 5147 | 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] | 5148 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5149 | /* |
Bram Moolenaar | b544f3c | 2017-02-23 19:03:28 +0100 | [diff] [blame] | 5150 | * Call "user_expand_func()" to invoke a user defined Vim script function and |
| 5151 | * return the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5152 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5153 | static void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5154 | call_user_expand_func( |
| 5155 | void *(*user_expand_func)(char_u *, int, char_u **, int), |
| 5156 | expand_T *xp, |
| 5157 | int *num_file, |
| 5158 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5159 | { |
Bram Moolenaar | 673b9a3 | 2013-06-30 22:43:27 +0200 | [diff] [blame] | 5160 | int keep = 0; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5161 | char_u num[50]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5162 | char_u *args[3]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5163 | int save_current_SID = current_SID; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5164 | void *ret; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5165 | struct cmdline_info save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5166 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5167 | 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] | 5168 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5169 | *num_file = 0; |
| 5170 | *file = NULL; |
| 5171 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5172 | if (ccline.cmdbuff != NULL) |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5173 | { |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5174 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 5175 | ccline.cmdbuff[ccline.cmdlen] = 0; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5176 | } |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5177 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5178 | args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5179 | args[1] = xp->xp_line; |
| 5180 | sprintf((char *)num, "%d", xp->xp_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5181 | args[2] = num; |
| 5182 | |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5183 | /* Save the cmdline, we don't know what the function may do. */ |
| 5184 | save_ccline = ccline; |
| 5185 | ccline.cmdbuff = NULL; |
| 5186 | ccline.cmdprompt = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5187 | current_SID = xp->xp_scriptID; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5188 | |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5189 | ret = user_expand_func(xp->xp_arg, 3, args, FALSE); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5190 | |
| 5191 | ccline = save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5192 | current_SID = save_current_SID; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5193 | if (ccline.cmdbuff != NULL) |
| 5194 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5195 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5196 | vim_free(args[0]); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5197 | return ret; |
| 5198 | } |
| 5199 | |
| 5200 | /* |
| 5201 | * Expand names with a function defined by the user. |
| 5202 | */ |
| 5203 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5204 | ExpandUserDefined( |
| 5205 | expand_T *xp, |
| 5206 | regmatch_T *regmatch, |
| 5207 | int *num_file, |
| 5208 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5209 | { |
| 5210 | char_u *retstr; |
| 5211 | char_u *s; |
| 5212 | char_u *e; |
| 5213 | char_u keep; |
| 5214 | garray_T ga; |
| 5215 | |
| 5216 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 5217 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5218 | return FAIL; |
| 5219 | |
| 5220 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5221 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5222 | { |
| 5223 | e = vim_strchr(s, '\n'); |
| 5224 | if (e == NULL) |
| 5225 | e = s + STRLEN(s); |
| 5226 | keep = *e; |
| 5227 | *e = 0; |
| 5228 | |
| 5229 | if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0) |
| 5230 | { |
| 5231 | *e = keep; |
| 5232 | if (*e != NUL) |
| 5233 | ++e; |
| 5234 | continue; |
| 5235 | } |
| 5236 | |
| 5237 | if (ga_grow(&ga, 1) == FAIL) |
| 5238 | break; |
| 5239 | |
| 5240 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 5241 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5242 | |
| 5243 | *e = keep; |
| 5244 | if (*e != NUL) |
| 5245 | ++e; |
| 5246 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5247 | vim_free(retstr); |
| 5248 | *file = ga.ga_data; |
| 5249 | *num_file = ga.ga_len; |
| 5250 | return OK; |
| 5251 | } |
| 5252 | |
| 5253 | /* |
| 5254 | * Expand names with a list returned by a function defined by the user. |
| 5255 | */ |
| 5256 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5257 | ExpandUserList( |
| 5258 | expand_T *xp, |
| 5259 | int *num_file, |
| 5260 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5261 | { |
| 5262 | list_T *retlist; |
| 5263 | listitem_T *li; |
| 5264 | garray_T ga; |
| 5265 | |
| 5266 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 5267 | if (retlist == NULL) |
| 5268 | return FAIL; |
| 5269 | |
| 5270 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 5271 | /* Loop over the items in the list. */ |
| 5272 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 5273 | { |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5274 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 5275 | continue; /* Skip non-string items and empty strings */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5276 | |
| 5277 | if (ga_grow(&ga, 1) == FAIL) |
| 5278 | break; |
| 5279 | |
| 5280 | ((char_u **)ga.ga_data)[ga.ga_len] = |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5281 | vim_strsave(li->li_tv.vval.v_string); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5282 | ++ga.ga_len; |
| 5283 | } |
| 5284 | list_unref(retlist); |
| 5285 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5286 | *file = ga.ga_data; |
| 5287 | *num_file = ga.ga_len; |
| 5288 | return OK; |
| 5289 | } |
| 5290 | #endif |
| 5291 | |
| 5292 | /* |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5293 | * Expand color scheme, compiler or filetype names. |
| 5294 | * Search from 'runtimepath': |
| 5295 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 5296 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 5297 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 5298 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 5299 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5300 | * "dirnames" is an array with one or more directory names. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5301 | */ |
| 5302 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5303 | ExpandRTDir( |
| 5304 | char_u *pat, |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5305 | int flags, |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5306 | int *num_file, |
| 5307 | char_u ***file, |
| 5308 | char *dirnames[]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5309 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5310 | char_u *s; |
| 5311 | char_u *e; |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5312 | char_u *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5313 | garray_T ga; |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5314 | int i; |
| 5315 | int pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5316 | |
| 5317 | *num_file = 0; |
| 5318 | *file = NULL; |
Bram Moolenaar | 5cfe2d7 | 2011-07-07 15:04:52 +0200 | [diff] [blame] | 5319 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5320 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5321 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5322 | for (i = 0; dirnames[i] != NULL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5323 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5324 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
| 5325 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5326 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5327 | ga_clear_strings(&ga); |
| 5328 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5329 | } |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5330 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5331 | globpath(p_rtp, s, &ga, 0); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5332 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5333 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5334 | |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5335 | if (flags & DIP_START) { |
| 5336 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5337 | { |
| 5338 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22)); |
| 5339 | if (s == NULL) |
| 5340 | { |
| 5341 | ga_clear_strings(&ga); |
| 5342 | return FAIL; |
| 5343 | } |
| 5344 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 5345 | globpath(p_pp, s, &ga, 0); |
| 5346 | vim_free(s); |
| 5347 | } |
| 5348 | } |
| 5349 | |
| 5350 | if (flags & DIP_OPT) { |
| 5351 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5352 | { |
| 5353 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20)); |
| 5354 | if (s == NULL) |
| 5355 | { |
| 5356 | ga_clear_strings(&ga); |
| 5357 | return FAIL; |
| 5358 | } |
| 5359 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 5360 | globpath(p_pp, s, &ga, 0); |
| 5361 | vim_free(s); |
| 5362 | } |
| 5363 | } |
| 5364 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5365 | for (i = 0; i < ga.ga_len; ++i) |
| 5366 | { |
| 5367 | match = ((char_u **)ga.ga_data)[i]; |
| 5368 | s = match; |
| 5369 | e = s + STRLEN(s); |
| 5370 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 5371 | { |
| 5372 | e -= 4; |
| 5373 | for (s = e; s > match; mb_ptr_back(match, s)) |
| 5374 | if (s < match || vim_ispathsep(*s)) |
| 5375 | break; |
| 5376 | ++s; |
| 5377 | *e = NUL; |
| 5378 | mch_memmove(match, s, e - s + 1); |
| 5379 | } |
| 5380 | } |
| 5381 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5382 | if (ga.ga_len == 0) |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5383 | return FAIL; |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5384 | |
| 5385 | /* Sort and remove duplicates which can happen when specifying multiple |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5386 | * directories in dirnames. */ |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5387 | remove_duplicates(&ga); |
| 5388 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5389 | *file = ga.ga_data; |
| 5390 | *num_file = ga.ga_len; |
| 5391 | return OK; |
| 5392 | } |
| 5393 | |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5394 | /* |
| 5395 | * Expand loadplugin names: |
| 5396 | * 'packpath'/pack/ * /opt/{pat} |
| 5397 | */ |
| 5398 | static int |
| 5399 | ExpandPackAddDir( |
| 5400 | char_u *pat, |
| 5401 | int *num_file, |
| 5402 | char_u ***file) |
| 5403 | { |
| 5404 | char_u *s; |
| 5405 | char_u *e; |
| 5406 | char_u *match; |
| 5407 | garray_T ga; |
| 5408 | int i; |
| 5409 | int pat_len; |
| 5410 | |
| 5411 | *num_file = 0; |
| 5412 | *file = NULL; |
| 5413 | pat_len = (int)STRLEN(pat); |
| 5414 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 5415 | |
| 5416 | s = alloc((unsigned)(pat_len + 26)); |
| 5417 | if (s == NULL) |
| 5418 | { |
| 5419 | ga_clear_strings(&ga); |
| 5420 | return FAIL; |
| 5421 | } |
| 5422 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 5423 | globpath(p_pp, s, &ga, 0); |
| 5424 | vim_free(s); |
| 5425 | |
| 5426 | for (i = 0; i < ga.ga_len; ++i) |
| 5427 | { |
| 5428 | match = ((char_u **)ga.ga_data)[i]; |
| 5429 | s = gettail(match); |
| 5430 | e = s + STRLEN(s); |
| 5431 | mch_memmove(match, s, e - s + 1); |
| 5432 | } |
| 5433 | |
| 5434 | if (ga.ga_len == 0) |
| 5435 | return FAIL; |
| 5436 | |
| 5437 | /* Sort and remove duplicates which can happen when specifying multiple |
| 5438 | * directories in dirnames. */ |
| 5439 | remove_duplicates(&ga); |
| 5440 | |
| 5441 | *file = ga.ga_data; |
| 5442 | *num_file = ga.ga_len; |
| 5443 | return OK; |
| 5444 | } |
| 5445 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5446 | #endif |
| 5447 | |
| 5448 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 5449 | /* |
| 5450 | * Expand "file" for all comma-separated directories in "path". |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5451 | * Adds the matches to "ga". Caller must init "ga". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5452 | */ |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5453 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5454 | globpath( |
| 5455 | char_u *path, |
| 5456 | char_u *file, |
| 5457 | garray_T *ga, |
| 5458 | int expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5459 | { |
| 5460 | expand_T xpc; |
| 5461 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5462 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5463 | int num_p; |
| 5464 | char_u **p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5465 | |
| 5466 | buf = alloc(MAXPATHL); |
| 5467 | if (buf == NULL) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5468 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5469 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5470 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5471 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5472 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5473 | /* Loop over all entries in {path}. */ |
| 5474 | while (*path != NUL) |
| 5475 | { |
| 5476 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 5477 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 5478 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 5479 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5480 | # if defined(MSWIN) |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5481 | /* Using the platform's path separator (\) makes vim incorrectly |
| 5482 | * treat it as an escape character, use '/' instead. */ |
| 5483 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 5484 | STRCAT(buf, "/"); |
| 5485 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5486 | add_pathsep(buf); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5487 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5488 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5489 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 5490 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5491 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5492 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5493 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5494 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5495 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5496 | for (i = 0; i < num_p; ++i) |
| 5497 | { |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5498 | ((char_u **)ga->ga_data)[ga->ga_len] = |
Bram Moolenaar | 7116aa0 | 2014-05-29 14:36:29 +0200 | [diff] [blame] | 5499 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5500 | ++ga->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5501 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5502 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5503 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5504 | FreeWild(num_p, p); |
| 5505 | } |
| 5506 | } |
| 5507 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5508 | |
| 5509 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5510 | } |
| 5511 | |
| 5512 | #endif |
| 5513 | |
| 5514 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5515 | |
| 5516 | /********************************* |
| 5517 | * Command line history stuff * |
| 5518 | *********************************/ |
| 5519 | |
| 5520 | /* |
| 5521 | * Translate a history character to the associated type number. |
| 5522 | */ |
| 5523 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5524 | hist_char2type(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5525 | { |
| 5526 | if (c == ':') |
| 5527 | return HIST_CMD; |
| 5528 | if (c == '=') |
| 5529 | return HIST_EXPR; |
| 5530 | if (c == '@') |
| 5531 | return HIST_INPUT; |
| 5532 | if (c == '>') |
| 5533 | return HIST_DEBUG; |
| 5534 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5535 | } |
| 5536 | |
| 5537 | /* |
| 5538 | * Table of history names. |
| 5539 | * These names are used in :history and various hist...() functions. |
| 5540 | * It is sufficient to give the significant prefix of a history name. |
| 5541 | */ |
| 5542 | |
| 5543 | static char *(history_names[]) = |
| 5544 | { |
| 5545 | "cmd", |
| 5546 | "search", |
| 5547 | "expr", |
| 5548 | "input", |
| 5549 | "debug", |
| 5550 | NULL |
| 5551 | }; |
| 5552 | |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5553 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5554 | /* |
| 5555 | * Function given to ExpandGeneric() to obtain the possible first |
| 5556 | * arguments of the ":history command. |
| 5557 | */ |
| 5558 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5559 | get_history_arg(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5560 | { |
| 5561 | static char_u compl[2] = { NUL, NUL }; |
| 5562 | char *short_names = ":=@>?/"; |
Bram Moolenaar | 17bd9dc | 2012-05-25 11:02:41 +0200 | [diff] [blame] | 5563 | int short_names_count = (int)STRLEN(short_names); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5564 | int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
| 5565 | |
| 5566 | if (idx < short_names_count) |
| 5567 | { |
| 5568 | compl[0] = (char_u)short_names[idx]; |
| 5569 | return compl; |
| 5570 | } |
| 5571 | if (idx < short_names_count + history_name_count) |
| 5572 | return (char_u *)history_names[idx - short_names_count]; |
| 5573 | if (idx == short_names_count + history_name_count) |
| 5574 | return (char_u *)"all"; |
| 5575 | return NULL; |
| 5576 | } |
| 5577 | #endif |
| 5578 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5579 | /* |
| 5580 | * init_history() - Initialize the command line history. |
| 5581 | * Also used to re-allocate the history when the size changes. |
| 5582 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 5583 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5584 | init_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5585 | { |
| 5586 | int newlen; /* new length of history table */ |
| 5587 | histentry_T *temp; |
| 5588 | int i; |
| 5589 | int j; |
| 5590 | int type; |
| 5591 | |
| 5592 | /* |
| 5593 | * If size of history table changed, reallocate it |
| 5594 | */ |
| 5595 | newlen = (int)p_hi; |
| 5596 | if (newlen != hislen) /* history length changed */ |
| 5597 | { |
| 5598 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 5599 | { |
| 5600 | if (newlen) |
| 5601 | { |
| 5602 | temp = (histentry_T *)lalloc( |
| 5603 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 5604 | if (temp == NULL) /* out of memory! */ |
| 5605 | { |
| 5606 | if (type == 0) /* first one: just keep the old length */ |
| 5607 | { |
| 5608 | newlen = hislen; |
| 5609 | break; |
| 5610 | } |
| 5611 | /* Already changed one table, now we can only have zero |
| 5612 | * length for all tables. */ |
| 5613 | newlen = 0; |
| 5614 | type = -1; |
| 5615 | continue; |
| 5616 | } |
| 5617 | } |
| 5618 | else |
| 5619 | temp = NULL; |
| 5620 | if (newlen == 0 || temp != NULL) |
| 5621 | { |
| 5622 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 5623 | { |
| 5624 | for (i = 0; i < newlen; ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5625 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5626 | } |
| 5627 | else if (newlen > hislen) /* array becomes bigger */ |
| 5628 | { |
| 5629 | for (i = 0; i <= hisidx[type]; ++i) |
| 5630 | temp[i] = history[type][i]; |
| 5631 | j = i; |
| 5632 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5633 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5634 | for ( ; j < hislen; ++i, ++j) |
| 5635 | temp[i] = history[type][j]; |
| 5636 | } |
| 5637 | else /* array becomes smaller or 0 */ |
| 5638 | { |
| 5639 | j = hisidx[type]; |
| 5640 | for (i = newlen - 1; ; --i) |
| 5641 | { |
| 5642 | if (i >= 0) /* copy newest entries */ |
| 5643 | temp[i] = history[type][j]; |
| 5644 | else /* remove older entries */ |
| 5645 | vim_free(history[type][j].hisstr); |
| 5646 | if (--j < 0) |
| 5647 | j = hislen - 1; |
| 5648 | if (j == hisidx[type]) |
| 5649 | break; |
| 5650 | } |
| 5651 | hisidx[type] = newlen - 1; |
| 5652 | } |
| 5653 | vim_free(history[type]); |
| 5654 | history[type] = temp; |
| 5655 | } |
| 5656 | } |
| 5657 | hislen = newlen; |
| 5658 | } |
| 5659 | } |
| 5660 | |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5661 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5662 | clear_hist_entry(histentry_T *hisptr) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5663 | { |
| 5664 | hisptr->hisnum = 0; |
| 5665 | hisptr->viminfo = FALSE; |
| 5666 | hisptr->hisstr = NULL; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 5667 | hisptr->time_set = 0; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5668 | } |
| 5669 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5670 | /* |
| 5671 | * Check if command line 'str' is already in history. |
| 5672 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 5673 | */ |
| 5674 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5675 | in_history( |
| 5676 | int type, |
| 5677 | char_u *str, |
| 5678 | int move_to_front, /* Move the entry to the front if it exists */ |
| 5679 | int sep, |
| 5680 | int writing) /* ignore entries read from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5681 | { |
| 5682 | int i; |
| 5683 | int last_i = -1; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5684 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5685 | |
| 5686 | if (hisidx[type] < 0) |
| 5687 | return FALSE; |
| 5688 | i = hisidx[type]; |
| 5689 | do |
| 5690 | { |
| 5691 | if (history[type][i].hisstr == NULL) |
| 5692 | return FALSE; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5693 | |
| 5694 | /* For search history, check that the separator character matches as |
| 5695 | * well. */ |
| 5696 | p = history[type][i].hisstr; |
| 5697 | if (STRCMP(str, p) == 0 |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5698 | && !(writing && history[type][i].viminfo) |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5699 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5700 | { |
| 5701 | if (!move_to_front) |
| 5702 | return TRUE; |
| 5703 | last_i = i; |
| 5704 | break; |
| 5705 | } |
| 5706 | if (--i < 0) |
| 5707 | i = hislen - 1; |
| 5708 | } while (i != hisidx[type]); |
| 5709 | |
| 5710 | if (last_i >= 0) |
| 5711 | { |
| 5712 | str = history[type][i].hisstr; |
| 5713 | while (i != hisidx[type]) |
| 5714 | { |
| 5715 | if (++i >= hislen) |
| 5716 | i = 0; |
| 5717 | history[type][last_i] = history[type][i]; |
| 5718 | last_i = i; |
| 5719 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5720 | history[type][i].hisnum = ++hisnum[type]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5721 | history[type][i].viminfo = FALSE; |
| 5722 | history[type][i].hisstr = str; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5723 | history[type][i].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5724 | return TRUE; |
| 5725 | } |
| 5726 | return FALSE; |
| 5727 | } |
| 5728 | |
| 5729 | /* |
| 5730 | * Convert history name (from table above) to its HIST_ equivalent. |
| 5731 | * When "name" is empty, return "cmd" history. |
| 5732 | * Returns -1 for unknown history name. |
| 5733 | */ |
| 5734 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5735 | get_histtype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5736 | { |
| 5737 | int i; |
| 5738 | int len = (int)STRLEN(name); |
| 5739 | |
| 5740 | /* No argument: use current history. */ |
| 5741 | if (len == 0) |
| 5742 | return hist_char2type(ccline.cmdfirstc); |
| 5743 | |
| 5744 | for (i = 0; history_names[i] != NULL; ++i) |
| 5745 | if (STRNICMP(name, history_names[i], len) == 0) |
| 5746 | return i; |
| 5747 | |
| 5748 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 5749 | return hist_char2type(name[0]); |
| 5750 | |
| 5751 | return -1; |
| 5752 | } |
| 5753 | |
| 5754 | static int last_maptick = -1; /* last seen maptick */ |
| 5755 | |
| 5756 | /* |
| 5757 | * Add the given string to the given history. If the string is already in the |
| 5758 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 5759 | * values. |
| 5760 | */ |
| 5761 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5762 | add_to_history( |
| 5763 | int histype, |
| 5764 | char_u *new_entry, |
| 5765 | int in_map, /* consider maptick when inside a mapping */ |
| 5766 | int sep) /* separator character used (search hist) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5767 | { |
| 5768 | histentry_T *hisptr; |
| 5769 | int len; |
| 5770 | |
| 5771 | if (hislen == 0) /* no history */ |
| 5772 | return; |
| 5773 | |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 5774 | if (cmdmod.keeppatterns && histype == HIST_SEARCH) |
| 5775 | return; |
| 5776 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5777 | /* |
| 5778 | * Searches inside the same mapping overwrite each other, so that only |
| 5779 | * the last line is kept. Be careful not to remove a line that was moved |
| 5780 | * down, only lines that were added. |
| 5781 | */ |
| 5782 | if (histype == HIST_SEARCH && in_map) |
| 5783 | { |
Bram Moolenaar | 4664371 | 2016-09-09 21:42:36 +0200 | [diff] [blame] | 5784 | if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5785 | { |
| 5786 | /* Current line is from the same mapping, remove it */ |
| 5787 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 5788 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5789 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5790 | --hisnum[histype]; |
| 5791 | if (--hisidx[HIST_SEARCH] < 0) |
| 5792 | hisidx[HIST_SEARCH] = hislen - 1; |
| 5793 | } |
| 5794 | last_maptick = -1; |
| 5795 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5796 | if (!in_history(histype, new_entry, TRUE, sep, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5797 | { |
| 5798 | if (++hisidx[histype] == hislen) |
| 5799 | hisidx[histype] = 0; |
| 5800 | hisptr = &history[histype][hisidx[histype]]; |
| 5801 | vim_free(hisptr->hisstr); |
| 5802 | |
| 5803 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5804 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5805 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 5806 | if (hisptr->hisstr != NULL) |
| 5807 | hisptr->hisstr[len + 1] = sep; |
| 5808 | |
| 5809 | hisptr->hisnum = ++hisnum[histype]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5810 | hisptr->viminfo = FALSE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5811 | hisptr->time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5812 | if (histype == HIST_SEARCH && in_map) |
| 5813 | last_maptick = maptick; |
| 5814 | } |
| 5815 | } |
| 5816 | |
| 5817 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 5818 | |
| 5819 | /* |
| 5820 | * Get identifier of newest history entry. |
| 5821 | * "histype" may be one of the HIST_ values. |
| 5822 | */ |
| 5823 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5824 | get_history_idx(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5825 | { |
| 5826 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5827 | || hisidx[histype] < 0) |
| 5828 | return -1; |
| 5829 | |
| 5830 | return history[histype][hisidx[histype]].hisnum; |
| 5831 | } |
| 5832 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5833 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5834 | * Calculate history index from a number: |
| 5835 | * num > 0: seen as identifying number of a history entry |
| 5836 | * num < 0: relative position in history wrt newest entry |
| 5837 | * "histype" may be one of the HIST_ values. |
| 5838 | */ |
| 5839 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5840 | calc_hist_idx(int histype, int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5841 | { |
| 5842 | int i; |
| 5843 | histentry_T *hist; |
| 5844 | int wrapped = FALSE; |
| 5845 | |
| 5846 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5847 | || (i = hisidx[histype]) < 0 || num == 0) |
| 5848 | return -1; |
| 5849 | |
| 5850 | hist = history[histype]; |
| 5851 | if (num > 0) |
| 5852 | { |
| 5853 | while (hist[i].hisnum > num) |
| 5854 | if (--i < 0) |
| 5855 | { |
| 5856 | if (wrapped) |
| 5857 | break; |
| 5858 | i += hislen; |
| 5859 | wrapped = TRUE; |
| 5860 | } |
| 5861 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 5862 | return i; |
| 5863 | } |
| 5864 | else if (-num <= hislen) |
| 5865 | { |
| 5866 | i += num + 1; |
| 5867 | if (i < 0) |
| 5868 | i += hislen; |
| 5869 | if (hist[i].hisstr != NULL) |
| 5870 | return i; |
| 5871 | } |
| 5872 | return -1; |
| 5873 | } |
| 5874 | |
| 5875 | /* |
| 5876 | * Get a history entry by its index. |
| 5877 | * "histype" may be one of the HIST_ values. |
| 5878 | */ |
| 5879 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5880 | get_history_entry(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5881 | { |
| 5882 | idx = calc_hist_idx(histype, idx); |
| 5883 | if (idx >= 0) |
| 5884 | return history[histype][idx].hisstr; |
| 5885 | else |
| 5886 | return (char_u *)""; |
| 5887 | } |
| 5888 | |
| 5889 | /* |
| 5890 | * Clear all entries of a history. |
| 5891 | * "histype" may be one of the HIST_ values. |
| 5892 | */ |
| 5893 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5894 | clr_history(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5895 | { |
| 5896 | int i; |
| 5897 | histentry_T *hisptr; |
| 5898 | |
| 5899 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 5900 | { |
| 5901 | hisptr = history[histype]; |
| 5902 | for (i = hislen; i--;) |
| 5903 | { |
| 5904 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5905 | clear_hist_entry(hisptr); |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 5906 | hisptr++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5907 | } |
| 5908 | hisidx[histype] = -1; /* mark history as cleared */ |
| 5909 | hisnum[histype] = 0; /* reset identifier counter */ |
| 5910 | return OK; |
| 5911 | } |
| 5912 | return FAIL; |
| 5913 | } |
| 5914 | |
| 5915 | /* |
| 5916 | * Remove all entries matching {str} from a history. |
| 5917 | * "histype" may be one of the HIST_ values. |
| 5918 | */ |
| 5919 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5920 | del_history_entry(int histype, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5921 | { |
| 5922 | regmatch_T regmatch; |
| 5923 | histentry_T *hisptr; |
| 5924 | int idx; |
| 5925 | int i; |
| 5926 | int last; |
| 5927 | int found = FALSE; |
| 5928 | |
| 5929 | regmatch.regprog = NULL; |
| 5930 | regmatch.rm_ic = FALSE; /* always match case */ |
| 5931 | if (hislen != 0 |
| 5932 | && histype >= 0 |
| 5933 | && histype < HIST_COUNT |
| 5934 | && *str != NUL |
| 5935 | && (idx = hisidx[histype]) >= 0 |
| 5936 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 5937 | != NULL) |
| 5938 | { |
| 5939 | i = last = idx; |
| 5940 | do |
| 5941 | { |
| 5942 | hisptr = &history[histype][i]; |
| 5943 | if (hisptr->hisstr == NULL) |
| 5944 | break; |
| 5945 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 5946 | { |
| 5947 | found = TRUE; |
| 5948 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5949 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5950 | } |
| 5951 | else |
| 5952 | { |
| 5953 | if (i != last) |
| 5954 | { |
| 5955 | history[histype][last] = *hisptr; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5956 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5957 | } |
| 5958 | if (--last < 0) |
| 5959 | last += hislen; |
| 5960 | } |
| 5961 | if (--i < 0) |
| 5962 | i += hislen; |
| 5963 | } while (i != idx); |
| 5964 | if (history[histype][idx].hisstr == NULL) |
| 5965 | hisidx[histype] = -1; |
| 5966 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5967 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5968 | return found; |
| 5969 | } |
| 5970 | |
| 5971 | /* |
| 5972 | * Remove an indexed entry from a history. |
| 5973 | * "histype" may be one of the HIST_ values. |
| 5974 | */ |
| 5975 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5976 | del_history_idx(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5977 | { |
| 5978 | int i, j; |
| 5979 | |
| 5980 | i = calc_hist_idx(histype, idx); |
| 5981 | if (i < 0) |
| 5982 | return FALSE; |
| 5983 | idx = hisidx[histype]; |
| 5984 | vim_free(history[histype][i].hisstr); |
| 5985 | |
| 5986 | /* When deleting the last added search string in a mapping, reset |
| 5987 | * last_maptick, so that the last added search string isn't deleted again. |
| 5988 | */ |
| 5989 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 5990 | last_maptick = -1; |
| 5991 | |
| 5992 | while (i != idx) |
| 5993 | { |
| 5994 | j = (i + 1) % hislen; |
| 5995 | history[histype][i] = history[histype][j]; |
| 5996 | i = j; |
| 5997 | } |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5998 | clear_hist_entry(&history[histype][i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5999 | if (--i < 0) |
| 6000 | i += hislen; |
| 6001 | hisidx[histype] = i; |
| 6002 | return TRUE; |
| 6003 | } |
| 6004 | |
| 6005 | #endif /* FEAT_EVAL */ |
| 6006 | |
| 6007 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 6008 | /* |
| 6009 | * Very specific function to remove the value in ":set key=val" from the |
| 6010 | * history. |
| 6011 | */ |
| 6012 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6013 | remove_key_from_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6014 | { |
| 6015 | char_u *p; |
| 6016 | int i; |
| 6017 | |
| 6018 | i = hisidx[HIST_CMD]; |
| 6019 | if (i < 0) |
| 6020 | return; |
| 6021 | p = history[HIST_CMD][i].hisstr; |
| 6022 | if (p != NULL) |
| 6023 | for ( ; *p; ++p) |
| 6024 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 6025 | { |
| 6026 | p = vim_strchr(p + 3, '='); |
| 6027 | if (p == NULL) |
| 6028 | break; |
| 6029 | ++p; |
| 6030 | for (i = 0; p[i] && !vim_iswhite(p[i]); ++i) |
| 6031 | if (p[i] == '\\' && p[i + 1]) |
| 6032 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6033 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6034 | --p; |
| 6035 | } |
| 6036 | } |
| 6037 | #endif |
| 6038 | |
| 6039 | #endif /* FEAT_CMDHIST */ |
| 6040 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6041 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6042 | /* |
| 6043 | * Get pointer to the command line info to use. cmdline_paste() may clear |
| 6044 | * ccline and put the previous value in prev_ccline. |
| 6045 | */ |
| 6046 | static struct cmdline_info * |
| 6047 | get_ccline_ptr(void) |
| 6048 | { |
| 6049 | if ((State & CMDLINE) == 0) |
| 6050 | return NULL; |
| 6051 | if (ccline.cmdbuff != NULL) |
| 6052 | return &ccline; |
| 6053 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 6054 | return &prev_ccline; |
| 6055 | return NULL; |
| 6056 | } |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6057 | #endif |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6058 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6059 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6060 | /* |
| 6061 | * Get the current command line in allocated memory. |
| 6062 | * Only works when the command line is being edited. |
| 6063 | * Returns NULL when something is wrong. |
| 6064 | */ |
| 6065 | char_u * |
| 6066 | get_cmdline_str(void) |
| 6067 | { |
| 6068 | struct cmdline_info *p = get_ccline_ptr(); |
| 6069 | |
| 6070 | if (p == NULL) |
| 6071 | return NULL; |
| 6072 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
| 6073 | } |
| 6074 | |
| 6075 | /* |
| 6076 | * Get the current command line position, counted in bytes. |
| 6077 | * Zero is the first position. |
| 6078 | * Only works when the command line is being edited. |
| 6079 | * Returns -1 when something is wrong. |
| 6080 | */ |
| 6081 | int |
| 6082 | get_cmdline_pos(void) |
| 6083 | { |
| 6084 | struct cmdline_info *p = get_ccline_ptr(); |
| 6085 | |
| 6086 | if (p == NULL) |
| 6087 | return -1; |
| 6088 | return p->cmdpos; |
| 6089 | } |
| 6090 | |
| 6091 | /* |
| 6092 | * Set the command line byte position to "pos". Zero is the first position. |
| 6093 | * Only works when the command line is being edited. |
| 6094 | * Returns 1 when failed, 0 when OK. |
| 6095 | */ |
| 6096 | int |
| 6097 | set_cmdline_pos( |
| 6098 | int pos) |
| 6099 | { |
| 6100 | struct cmdline_info *p = get_ccline_ptr(); |
| 6101 | |
| 6102 | if (p == NULL) |
| 6103 | return 1; |
| 6104 | |
| 6105 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 6106 | * changed the command line. */ |
| 6107 | if (pos < 0) |
| 6108 | new_cmdpos = 0; |
| 6109 | else |
| 6110 | new_cmdpos = pos; |
| 6111 | return 0; |
| 6112 | } |
| 6113 | #endif |
| 6114 | |
| 6115 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
| 6116 | /* |
| 6117 | * Get the current command-line type. |
| 6118 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
| 6119 | * Only works when the command line is being edited. |
| 6120 | * Returns NUL when something is wrong. |
| 6121 | */ |
| 6122 | int |
| 6123 | get_cmdline_type(void) |
| 6124 | { |
| 6125 | struct cmdline_info *p = get_ccline_ptr(); |
| 6126 | |
| 6127 | if (p == NULL) |
| 6128 | return NUL; |
| 6129 | if (p->cmdfirstc == NUL) |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6130 | return |
| 6131 | # ifdef FEAT_EVAL |
| 6132 | (p->input_fn) ? '@' : |
| 6133 | # endif |
| 6134 | '-'; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6135 | return p->cmdfirstc; |
| 6136 | } |
| 6137 | #endif |
| 6138 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6139 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 6140 | /* |
| 6141 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 6142 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 6143 | * Returns OK if parsed successfully, otherwise FAIL. |
| 6144 | */ |
| 6145 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6146 | get_list_range(char_u **str, int *num1, int *num2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6147 | { |
| 6148 | int len; |
| 6149 | int first = FALSE; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 6150 | varnumber_T num; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6151 | |
| 6152 | *str = skipwhite(*str); |
| 6153 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 6154 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6155 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6156 | *str += len; |
| 6157 | *num1 = (int)num; |
| 6158 | first = TRUE; |
| 6159 | } |
| 6160 | *str = skipwhite(*str); |
| 6161 | if (**str == ',') /* parse "to" part of range */ |
| 6162 | { |
| 6163 | *str = skipwhite(*str + 1); |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6164 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6165 | if (len > 0) |
| 6166 | { |
| 6167 | *num2 = (int)num; |
| 6168 | *str = skipwhite(*str + len); |
| 6169 | } |
| 6170 | else if (!first) /* no number given at all */ |
| 6171 | return FAIL; |
| 6172 | } |
| 6173 | else if (first) /* only one number given */ |
| 6174 | *num2 = *num1; |
| 6175 | return OK; |
| 6176 | } |
| 6177 | #endif |
| 6178 | |
| 6179 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 6180 | /* |
| 6181 | * :history command - print a history |
| 6182 | */ |
| 6183 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6184 | ex_history(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6185 | { |
| 6186 | histentry_T *hist; |
| 6187 | int histype1 = HIST_CMD; |
| 6188 | int histype2 = HIST_CMD; |
| 6189 | int hisidx1 = 1; |
| 6190 | int hisidx2 = -1; |
| 6191 | int idx; |
| 6192 | int i, j, k; |
| 6193 | char_u *end; |
| 6194 | char_u *arg = eap->arg; |
| 6195 | |
| 6196 | if (hislen == 0) |
| 6197 | { |
| 6198 | MSG(_("'history' option is zero")); |
| 6199 | return; |
| 6200 | } |
| 6201 | |
| 6202 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 6203 | { |
| 6204 | end = arg; |
| 6205 | while (ASCII_ISALPHA(*end) |
| 6206 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 6207 | end++; |
| 6208 | i = *end; |
| 6209 | *end = NUL; |
| 6210 | histype1 = get_histtype(arg); |
| 6211 | if (histype1 == -1) |
| 6212 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 6213 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6214 | { |
| 6215 | histype1 = 0; |
| 6216 | histype2 = HIST_COUNT-1; |
| 6217 | } |
| 6218 | else |
| 6219 | { |
| 6220 | *end = i; |
| 6221 | EMSG(_(e_trailing)); |
| 6222 | return; |
| 6223 | } |
| 6224 | } |
| 6225 | else |
| 6226 | histype2 = histype1; |
| 6227 | *end = i; |
| 6228 | } |
| 6229 | else |
| 6230 | end = arg; |
| 6231 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 6232 | { |
| 6233 | EMSG(_(e_trailing)); |
| 6234 | return; |
| 6235 | } |
| 6236 | |
| 6237 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 6238 | { |
| 6239 | STRCPY(IObuff, "\n # "); |
| 6240 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 6241 | MSG_PUTS_TITLE(IObuff); |
| 6242 | idx = hisidx[histype1]; |
| 6243 | hist = history[histype1]; |
| 6244 | j = hisidx1; |
| 6245 | k = hisidx2; |
| 6246 | if (j < 0) |
| 6247 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 6248 | if (k < 0) |
| 6249 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 6250 | if (idx >= 0 && j <= k) |
| 6251 | for (i = idx + 1; !got_int; ++i) |
| 6252 | { |
| 6253 | if (i == hislen) |
| 6254 | i = 0; |
| 6255 | if (hist[i].hisstr != NULL |
| 6256 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 6257 | { |
| 6258 | msg_putchar('\n'); |
| 6259 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 6260 | hist[i].hisnum); |
| 6261 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 6262 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
Bram Moolenaar | 38f5f95 | 2012-01-26 13:01:59 +0100 | [diff] [blame] | 6263 | (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6264 | else |
| 6265 | STRCAT(IObuff, hist[i].hisstr); |
| 6266 | msg_outtrans(IObuff); |
| 6267 | out_flush(); |
| 6268 | } |
| 6269 | if (i == idx) |
| 6270 | break; |
| 6271 | } |
| 6272 | } |
| 6273 | } |
| 6274 | #endif |
| 6275 | |
| 6276 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6277 | /* |
| 6278 | * Buffers for history read from a viminfo file. Only valid while reading. |
| 6279 | */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6280 | static histentry_T *viminfo_history[HIST_COUNT] = |
| 6281 | {NULL, NULL, NULL, NULL, NULL}; |
| 6282 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 6283 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6284 | static int viminfo_add_at_front = FALSE; |
| 6285 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 6286 | static int hist_type2char(int type, int use_question); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6287 | |
| 6288 | /* |
| 6289 | * Translate a history type number to the associated character. |
| 6290 | */ |
| 6291 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6292 | hist_type2char( |
| 6293 | int type, |
| 6294 | int use_question) /* use '?' instead of '/' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6295 | { |
| 6296 | if (type == HIST_CMD) |
| 6297 | return ':'; |
| 6298 | if (type == HIST_SEARCH) |
| 6299 | { |
| 6300 | if (use_question) |
| 6301 | return '?'; |
| 6302 | else |
| 6303 | return '/'; |
| 6304 | } |
| 6305 | if (type == HIST_EXPR) |
| 6306 | return '='; |
| 6307 | return '@'; |
| 6308 | } |
| 6309 | |
| 6310 | /* |
| 6311 | * Prepare for reading the history from the viminfo file. |
| 6312 | * This allocates history arrays to store the read history lines. |
| 6313 | */ |
| 6314 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6315 | prepare_viminfo_history(int asklen, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6316 | { |
| 6317 | int i; |
| 6318 | int num; |
| 6319 | int type; |
| 6320 | int len; |
| 6321 | |
| 6322 | init_history(); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6323 | viminfo_add_at_front = (asklen != 0 && !writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6324 | if (asklen > hislen) |
| 6325 | asklen = hislen; |
| 6326 | |
| 6327 | for (type = 0; type < HIST_COUNT; ++type) |
| 6328 | { |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6329 | /* Count the number of empty spaces in the history list. Entries read |
| 6330 | * from viminfo previously are also considered empty. If there are |
| 6331 | * more spaces available than we request, then fill them up. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6332 | for (i = 0, num = 0; i < hislen; i++) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6333 | if (history[type][i].hisstr == NULL || history[type][i].viminfo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6334 | num++; |
| 6335 | len = asklen; |
| 6336 | if (num > len) |
| 6337 | len = num; |
| 6338 | if (len <= 0) |
| 6339 | viminfo_history[type] = NULL; |
| 6340 | else |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6341 | viminfo_history[type] = (histentry_T *)lalloc( |
| 6342 | (long_u)(len * sizeof(histentry_T)), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6343 | if (viminfo_history[type] == NULL) |
| 6344 | len = 0; |
| 6345 | viminfo_hislen[type] = len; |
| 6346 | viminfo_hisidx[type] = 0; |
| 6347 | } |
| 6348 | } |
| 6349 | |
| 6350 | /* |
| 6351 | * Accept a line from the viminfo, store it in the history array when it's |
| 6352 | * new. |
| 6353 | */ |
| 6354 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6355 | read_viminfo_history(vir_T *virp, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6356 | { |
| 6357 | int type; |
| 6358 | long_u len; |
| 6359 | char_u *val; |
| 6360 | char_u *p; |
| 6361 | |
| 6362 | type = hist_char2type(virp->vir_line[0]); |
| 6363 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6364 | { |
| 6365 | val = viminfo_readstring(virp, 1, TRUE); |
| 6366 | if (val != NULL && *val != NUL) |
| 6367 | { |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6368 | int sep = (*val == ' ' ? NUL : *val); |
| 6369 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6370 | if (!in_history(type, val + (type == HIST_SEARCH), |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6371 | viminfo_add_at_front, sep, writing)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6372 | { |
| 6373 | /* Need to re-allocate to append the separator byte. */ |
| 6374 | len = STRLEN(val); |
| 6375 | p = lalloc(len + 2, TRUE); |
| 6376 | if (p != NULL) |
| 6377 | { |
| 6378 | if (type == HIST_SEARCH) |
| 6379 | { |
| 6380 | /* Search entry: Move the separator from the first |
| 6381 | * column to after the NUL. */ |
| 6382 | mch_memmove(p, val + 1, (size_t)len); |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6383 | p[len] = sep; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6384 | } |
| 6385 | else |
| 6386 | { |
| 6387 | /* Not a search entry: No separator in the viminfo |
| 6388 | * file, add a NUL separator. */ |
| 6389 | mch_memmove(p, val, (size_t)len + 1); |
| 6390 | p[len + 1] = NUL; |
| 6391 | } |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6392 | viminfo_history[type][viminfo_hisidx[type]].hisstr = p; |
| 6393 | viminfo_history[type][viminfo_hisidx[type]].time_set = 0; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6394 | viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE; |
| 6395 | viminfo_history[type][viminfo_hisidx[type]].hisnum = 0; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6396 | viminfo_hisidx[type]++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6397 | } |
| 6398 | } |
| 6399 | } |
| 6400 | vim_free(val); |
| 6401 | } |
| 6402 | return viminfo_readline(virp); |
| 6403 | } |
| 6404 | |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6405 | /* |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6406 | * Accept a new style history line from the viminfo, store it in the history |
| 6407 | * array when it's new. |
| 6408 | */ |
| 6409 | void |
| 6410 | handle_viminfo_history( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6411 | garray_T *values, |
| 6412 | int writing) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6413 | { |
| 6414 | int type; |
| 6415 | long_u len; |
| 6416 | char_u *val; |
| 6417 | char_u *p; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6418 | bval_T *vp = (bval_T *)values->ga_data; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6419 | |
| 6420 | /* Check the format: |
| 6421 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6422 | if (values->ga_len < 4 |
| 6423 | || vp[0].bv_type != BVAL_NR |
| 6424 | || vp[1].bv_type != BVAL_NR |
| 6425 | || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY) |
| 6426 | || vp[3].bv_type != BVAL_STRING) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6427 | return; |
| 6428 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6429 | type = vp[0].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6430 | if (type >= HIST_COUNT) |
| 6431 | return; |
| 6432 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6433 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6434 | val = vp[3].bv_string; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6435 | if (val != NULL && *val != NUL) |
| 6436 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6437 | int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR |
| 6438 | ? vp[2].bv_nr : NUL; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6439 | int idx; |
| 6440 | int overwrite = FALSE; |
| 6441 | |
| 6442 | if (!in_history(type, val, viminfo_add_at_front, sep, writing)) |
| 6443 | { |
| 6444 | /* If lines were written by an older Vim we need to avoid |
| 6445 | * getting duplicates. See if the entry already exists. */ |
| 6446 | for (idx = 0; idx < viminfo_hisidx[type]; ++idx) |
| 6447 | { |
| 6448 | p = viminfo_history[type][idx].hisstr; |
| 6449 | if (STRCMP(val, p) == 0 |
| 6450 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
| 6451 | { |
| 6452 | overwrite = TRUE; |
| 6453 | break; |
| 6454 | } |
| 6455 | } |
| 6456 | |
| 6457 | if (!overwrite) |
| 6458 | { |
| 6459 | /* Need to re-allocate to append the separator byte. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6460 | len = vp[3].bv_len; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6461 | p = lalloc(len + 2, TRUE); |
| 6462 | } |
Bram Moolenaar | 72e697d | 2016-06-13 22:48:01 +0200 | [diff] [blame] | 6463 | else |
| 6464 | len = 0; /* for picky compilers */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6465 | if (p != NULL) |
| 6466 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6467 | viminfo_history[type][idx].time_set = vp[1].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6468 | if (!overwrite) |
| 6469 | { |
| 6470 | mch_memmove(p, val, (size_t)len + 1); |
| 6471 | /* Put the separator after the NUL. */ |
| 6472 | p[len + 1] = sep; |
| 6473 | viminfo_history[type][idx].hisstr = p; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6474 | viminfo_history[type][idx].hisnum = 0; |
| 6475 | viminfo_history[type][idx].viminfo = TRUE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6476 | viminfo_hisidx[type]++; |
| 6477 | } |
| 6478 | } |
| 6479 | } |
| 6480 | } |
| 6481 | } |
| 6482 | } |
| 6483 | |
| 6484 | /* |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6485 | * Concatenate history lines from viminfo after the lines typed in this Vim. |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6486 | */ |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6487 | static void |
| 6488 | concat_history(int type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6489 | { |
| 6490 | int idx; |
| 6491 | int i; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6492 | |
| 6493 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 6494 | if (idx >= hislen) |
| 6495 | idx -= hislen; |
| 6496 | else if (idx < 0) |
| 6497 | idx = hislen - 1; |
| 6498 | if (viminfo_add_at_front) |
| 6499 | hisidx[type] = idx; |
| 6500 | else |
| 6501 | { |
| 6502 | if (hisidx[type] == -1) |
| 6503 | hisidx[type] = hislen - 1; |
| 6504 | do |
| 6505 | { |
| 6506 | if (history[type][idx].hisstr != NULL |
| 6507 | || history[type][idx].viminfo) |
| 6508 | break; |
| 6509 | if (++idx == hislen) |
| 6510 | idx = 0; |
| 6511 | } while (idx != hisidx[type]); |
| 6512 | if (idx != hisidx[type] && --idx < 0) |
| 6513 | idx = hislen - 1; |
| 6514 | } |
| 6515 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6516 | { |
| 6517 | vim_free(history[type][idx].hisstr); |
| 6518 | history[type][idx].hisstr = viminfo_history[type][i].hisstr; |
| 6519 | history[type][idx].viminfo = TRUE; |
| 6520 | history[type][idx].time_set = viminfo_history[type][i].time_set; |
| 6521 | if (--idx < 0) |
| 6522 | idx = hislen - 1; |
| 6523 | } |
| 6524 | idx += 1; |
| 6525 | idx %= hislen; |
| 6526 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6527 | { |
| 6528 | history[type][idx++].hisnum = ++hisnum[type]; |
| 6529 | idx %= hislen; |
| 6530 | } |
| 6531 | } |
| 6532 | |
| 6533 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6534 | static int |
| 6535 | #ifdef __BORLANDC__ |
| 6536 | _RTLENTRYF |
| 6537 | #endif |
| 6538 | sort_hist(const void *s1, const void *s2) |
| 6539 | { |
| 6540 | histentry_T *p1 = *(histentry_T **)s1; |
| 6541 | histentry_T *p2 = *(histentry_T **)s2; |
| 6542 | |
| 6543 | if (p1->time_set < p2->time_set) return -1; |
| 6544 | if (p1->time_set > p2->time_set) return 1; |
| 6545 | return 0; |
| 6546 | } |
| 6547 | #endif |
| 6548 | |
| 6549 | /* |
| 6550 | * Merge history lines from viminfo and lines typed in this Vim based on the |
| 6551 | * timestamp; |
| 6552 | */ |
| 6553 | static void |
| 6554 | merge_history(int type) |
| 6555 | { |
| 6556 | int max_len; |
| 6557 | histentry_T **tot_hist; |
| 6558 | histentry_T *new_hist; |
| 6559 | int i; |
| 6560 | int len; |
| 6561 | |
| 6562 | /* Make one long list with all entries. */ |
| 6563 | max_len = hislen + viminfo_hisidx[type]; |
| 6564 | tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *)); |
| 6565 | new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T)); |
| 6566 | if (tot_hist == NULL || new_hist == NULL) |
| 6567 | { |
| 6568 | vim_free(tot_hist); |
| 6569 | vim_free(new_hist); |
| 6570 | return; |
| 6571 | } |
| 6572 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6573 | tot_hist[i] = &viminfo_history[type][i]; |
| 6574 | len = i; |
| 6575 | for (i = 0; i < hislen; i++) |
| 6576 | if (history[type][i].hisstr != NULL) |
| 6577 | tot_hist[len++] = &history[type][i]; |
| 6578 | |
| 6579 | /* Sort the list on timestamp. */ |
| 6580 | qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist); |
| 6581 | |
| 6582 | /* Keep the newest ones. */ |
| 6583 | for (i = 0; i < hislen; i++) |
| 6584 | { |
| 6585 | if (i < len) |
| 6586 | { |
| 6587 | new_hist[i] = *tot_hist[i]; |
| 6588 | tot_hist[i]->hisstr = NULL; |
| 6589 | if (new_hist[i].hisnum == 0) |
| 6590 | new_hist[i].hisnum = ++hisnum[type]; |
| 6591 | } |
| 6592 | else |
| 6593 | clear_hist_entry(&new_hist[i]); |
| 6594 | } |
Bram Moolenaar | a890f5e | 2016-06-12 23:03:19 +0200 | [diff] [blame] | 6595 | hisidx[type] = (i < len ? i : len) - 1; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6596 | |
| 6597 | /* Free what is not kept. */ |
| 6598 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6599 | vim_free(viminfo_history[type][i].hisstr); |
| 6600 | for (i = 0; i < hislen; i++) |
| 6601 | vim_free(history[type][i].hisstr); |
| 6602 | vim_free(history[type]); |
| 6603 | history[type] = new_hist; |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 6604 | vim_free(tot_hist); |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6605 | } |
| 6606 | |
| 6607 | /* |
| 6608 | * Finish reading history lines from viminfo. Not used when writing viminfo. |
| 6609 | */ |
| 6610 | void |
| 6611 | finish_viminfo_history(vir_T *virp) |
| 6612 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6613 | int type; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6614 | int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6615 | |
| 6616 | for (type = 0; type < HIST_COUNT; ++type) |
| 6617 | { |
| 6618 | if (history[type] == NULL) |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6619 | continue; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6620 | |
| 6621 | if (merge) |
| 6622 | merge_history(type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6623 | else |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6624 | concat_history(type); |
| 6625 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6626 | vim_free(viminfo_history[type]); |
| 6627 | viminfo_history[type] = NULL; |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6628 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6629 | } |
| 6630 | } |
| 6631 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6632 | /* |
| 6633 | * Write history to viminfo file in "fp". |
| 6634 | * When "merge" is TRUE merge history lines with a previously read viminfo |
| 6635 | * file, data is in viminfo_history[]. |
| 6636 | * When "merge" is FALSE just write all history lines. Used for ":wviminfo!". |
| 6637 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6638 | void |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6639 | write_viminfo_history(FILE *fp, int merge) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6640 | { |
| 6641 | int i; |
| 6642 | int type; |
| 6643 | int num_saved; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6644 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6645 | |
| 6646 | init_history(); |
| 6647 | if (hislen == 0) |
| 6648 | return; |
| 6649 | for (type = 0; type < HIST_COUNT; ++type) |
| 6650 | { |
| 6651 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 6652 | if (num_saved == 0) |
| 6653 | continue; |
| 6654 | if (num_saved < 0) /* Use default */ |
| 6655 | num_saved = hislen; |
| 6656 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 6657 | type == HIST_CMD ? _("Command Line") : |
| 6658 | type == HIST_SEARCH ? _("Search String") : |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6659 | type == HIST_EXPR ? _("Expression") : |
| 6660 | type == HIST_INPUT ? _("Input Line") : |
| 6661 | _("Debug Line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6662 | if (num_saved > hislen) |
| 6663 | num_saved = hislen; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6664 | |
| 6665 | /* |
| 6666 | * Merge typed and viminfo history: |
| 6667 | * round 1: history of typed commands. |
| 6668 | * round 2: history from recently read viminfo. |
| 6669 | */ |
| 6670 | for (round = 1; round <= 2; ++round) |
| 6671 | { |
Bram Moolenaar | a8565fe | 2013-04-15 16:14:22 +0200 | [diff] [blame] | 6672 | if (round == 1) |
| 6673 | /* start at newest entry, somewhere in the list */ |
| 6674 | i = hisidx[type]; |
| 6675 | else if (viminfo_hisidx[type] > 0) |
| 6676 | /* start at newest entry, first in the list */ |
| 6677 | i = 0; |
| 6678 | else |
| 6679 | /* empty list */ |
| 6680 | i = -1; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6681 | if (i >= 0) |
| 6682 | while (num_saved > 0 |
| 6683 | && !(round == 2 && i >= viminfo_hisidx[type])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6684 | { |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6685 | char_u *p; |
| 6686 | time_t timestamp; |
| 6687 | int c = NUL; |
| 6688 | |
| 6689 | if (round == 1) |
| 6690 | { |
| 6691 | p = history[type][i].hisstr; |
| 6692 | timestamp = history[type][i].time_set; |
| 6693 | } |
| 6694 | else |
| 6695 | { |
| 6696 | p = viminfo_history[type] == NULL ? NULL |
| 6697 | : viminfo_history[type][i].hisstr; |
| 6698 | timestamp = viminfo_history[type] == NULL ? 0 |
| 6699 | : viminfo_history[type][i].time_set; |
| 6700 | } |
| 6701 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6702 | if (p != NULL && (round == 2 |
| 6703 | || !merge |
| 6704 | || !history[type][i].viminfo)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6705 | { |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6706 | --num_saved; |
| 6707 | fputc(hist_type2char(type, TRUE), fp); |
| 6708 | /* For the search history: put the separator in the |
| 6709 | * second column; use a space if there isn't one. */ |
| 6710 | if (type == HIST_SEARCH) |
| 6711 | { |
| 6712 | c = p[STRLEN(p) + 1]; |
| 6713 | putc(c == NUL ? ' ' : c, fp); |
| 6714 | } |
| 6715 | viminfo_writestring(fp, p); |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6716 | |
| 6717 | { |
| 6718 | char cbuf[NUMBUFLEN]; |
| 6719 | |
| 6720 | /* New style history with a bar line. Format: |
| 6721 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
| 6722 | if (c == NUL) |
| 6723 | cbuf[0] = NUL; |
| 6724 | else |
| 6725 | sprintf(cbuf, "%d", c); |
| 6726 | fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY, |
| 6727 | type, (long)timestamp, cbuf); |
| 6728 | barline_writestring(fp, p, LSIZE - 20); |
| 6729 | putc('\n', fp); |
| 6730 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6731 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6732 | if (round == 1) |
| 6733 | { |
| 6734 | /* Decrement index, loop around and stop when back at |
| 6735 | * the start. */ |
| 6736 | if (--i < 0) |
| 6737 | i = hislen - 1; |
| 6738 | if (i == hisidx[type]) |
| 6739 | break; |
| 6740 | } |
| 6741 | else |
| 6742 | { |
| 6743 | /* Increment index. Stop at the end in the while. */ |
| 6744 | ++i; |
| 6745 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6746 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6747 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6748 | for (i = 0; i < viminfo_hisidx[type]; ++i) |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6749 | if (viminfo_history[type] != NULL) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6750 | vim_free(viminfo_history[type][i].hisstr); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6751 | vim_free(viminfo_history[type]); |
| 6752 | viminfo_history[type] = NULL; |
Bram Moolenaar | b70a473 | 2013-04-15 22:22:57 +0200 | [diff] [blame] | 6753 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6754 | } |
| 6755 | } |
| 6756 | #endif /* FEAT_VIMINFO */ |
| 6757 | |
| 6758 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 6759 | /* |
| 6760 | * Write a character at the current cursor+offset position. |
| 6761 | * It is directly written into the command buffer block. |
| 6762 | */ |
| 6763 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6764 | cmd_pchar(int c, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6765 | { |
| 6766 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 6767 | { |
| 6768 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 6769 | return; |
| 6770 | } |
| 6771 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 6772 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 6773 | } |
| 6774 | |
| 6775 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6776 | cmd_gchar(int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6777 | { |
| 6778 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 6779 | { |
| 6780 | /* EMSG(_("cmd_gchar beyond the command length")); */ |
| 6781 | return NUL; |
| 6782 | } |
| 6783 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 6784 | } |
| 6785 | #endif |
| 6786 | |
| 6787 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 6788 | /* |
| 6789 | * Open a window on the current command line and history. Allow editing in |
| 6790 | * the window. Returns when the window is closed. |
| 6791 | * Returns: |
| 6792 | * CR if the command is to be executed |
| 6793 | * Ctrl_C if it is to be abandoned |
| 6794 | * K_IGNORE if editing continues |
| 6795 | */ |
| 6796 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6797 | ex_window(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6798 | { |
| 6799 | struct cmdline_info save_ccline; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6800 | bufref_T old_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6801 | win_T *old_curwin = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6802 | bufref_T bufref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6803 | win_T *wp; |
| 6804 | int i; |
| 6805 | linenr_T lnum; |
| 6806 | int histtype; |
| 6807 | garray_T winsizes; |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 6808 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6809 | char_u typestr[2]; |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 6810 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6811 | int save_restart_edit = restart_edit; |
| 6812 | int save_State = State; |
| 6813 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6814 | #ifdef FEAT_RIGHTLEFT |
| 6815 | int save_cmdmsg_rl = cmdmsg_rl; |
| 6816 | #endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6817 | #ifdef FEAT_FOLDING |
| 6818 | int save_KeyTyped; |
| 6819 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6820 | |
| 6821 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 6822 | if (cmdwin_type != 0 |
| 6823 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 6824 | || cmdline_star > 0 |
| 6825 | # endif |
| 6826 | ) |
| 6827 | { |
| 6828 | beep_flush(); |
| 6829 | return K_IGNORE; |
| 6830 | } |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6831 | set_bufref(&old_curbuf, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6832 | |
| 6833 | /* Save current window sizes. */ |
| 6834 | win_size_save(&winsizes); |
| 6835 | |
| 6836 | # ifdef FEAT_AUTOCMD |
| 6837 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6838 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6839 | # endif |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 6840 | /* don't use a new tab page */ |
| 6841 | cmdmod.tab = 0; |
| 6842 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6843 | /* Create a window for the command-line buffer. */ |
| 6844 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 6845 | { |
| 6846 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6847 | # ifdef FEAT_AUTOCMD |
| 6848 | unblock_autocmds(); |
| 6849 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6850 | return K_IGNORE; |
| 6851 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6852 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6853 | |
| 6854 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 6855 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6856 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6857 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
| 6858 | set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); |
| 6859 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 6860 | #ifdef FEAT_FOLDING |
| 6861 | curwin->w_p_fen = FALSE; |
| 6862 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6863 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6864 | curwin->w_p_rl = cmdmsg_rl; |
| 6865 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6866 | # endif |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 6867 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6868 | |
| 6869 | # ifdef FEAT_AUTOCMD |
| 6870 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6871 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6872 | # endif |
| 6873 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6874 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 6875 | need_wait_return = FALSE; |
| 6876 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6877 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6878 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 6879 | { |
| 6880 | if (p_wc == TAB) |
| 6881 | { |
| 6882 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 6883 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 6884 | } |
| 6885 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 6886 | } |
| 6887 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6888 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 6889 | * sets 'textwidth' to 78). */ |
| 6890 | curbuf->b_p_tw = 0; |
| 6891 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6892 | /* Fill the buffer with the history. */ |
| 6893 | init_history(); |
| 6894 | if (hislen > 0) |
| 6895 | { |
| 6896 | i = hisidx[histtype]; |
| 6897 | if (i >= 0) |
| 6898 | { |
| 6899 | lnum = 0; |
| 6900 | do |
| 6901 | { |
| 6902 | if (++i == hislen) |
| 6903 | i = 0; |
| 6904 | if (history[histtype][i].hisstr != NULL) |
| 6905 | ml_append(lnum++, history[histtype][i].hisstr, |
| 6906 | (colnr_T)0, FALSE); |
| 6907 | } |
| 6908 | while (i != hisidx[histtype]); |
| 6909 | } |
| 6910 | } |
| 6911 | |
| 6912 | /* Replace the empty last line with the current command-line and put the |
| 6913 | * cursor there. */ |
| 6914 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 6915 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 6916 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6917 | changed_line_abv_curs(); |
| 6918 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 6919 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6920 | |
| 6921 | /* Save the command line info, can be used recursively. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 6922 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6923 | |
| 6924 | /* No Ex mode here! */ |
| 6925 | exmode_active = 0; |
| 6926 | |
| 6927 | State = NORMAL; |
| 6928 | # ifdef FEAT_MOUSE |
| 6929 | setmouse(); |
| 6930 | # endif |
| 6931 | |
| 6932 | # ifdef FEAT_AUTOCMD |
| 6933 | /* Trigger CmdwinEnter autocommands. */ |
| 6934 | typestr[0] = cmdwin_type; |
| 6935 | typestr[1] = NUL; |
| 6936 | apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 6937 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 6938 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6939 | # endif |
| 6940 | |
| 6941 | i = RedrawingDisabled; |
| 6942 | RedrawingDisabled = 0; |
| 6943 | |
| 6944 | /* |
| 6945 | * Call the main loop until <CR> or CTRL-C is typed. |
| 6946 | */ |
| 6947 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 6948 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6949 | |
| 6950 | RedrawingDisabled = i; |
| 6951 | |
| 6952 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6953 | |
| 6954 | # ifdef FEAT_FOLDING |
| 6955 | save_KeyTyped = KeyTyped; |
| 6956 | # endif |
| 6957 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6958 | /* Trigger CmdwinLeave autocommands. */ |
| 6959 | apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf); |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6960 | |
| 6961 | # ifdef FEAT_FOLDING |
| 6962 | /* Restore KeyTyped in case it is modified by autocommands */ |
| 6963 | KeyTyped = save_KeyTyped; |
| 6964 | # endif |
| 6965 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6966 | # endif |
| 6967 | |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 6968 | /* Restore the command line info. */ |
Bram Moolenaar | 1d669c2 | 2017-01-11 22:40:19 +0100 | [diff] [blame] | 6969 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6970 | cmdwin_type = 0; |
| 6971 | |
| 6972 | exmode_active = save_exmode; |
| 6973 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 6974 | /* 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] | 6975 | * this happens! */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6976 | if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6977 | { |
| 6978 | cmdwin_result = Ctrl_C; |
| 6979 | EMSG(_("E199: Active window or buffer deleted")); |
| 6980 | } |
| 6981 | else |
| 6982 | { |
| 6983 | # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 6984 | /* autocmds may abort script processing */ |
| 6985 | if (aborting() && cmdwin_result != K_IGNORE) |
| 6986 | cmdwin_result = Ctrl_C; |
| 6987 | # endif |
| 6988 | /* Set the new command line from the cmdline buffer. */ |
| 6989 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6990 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6991 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6992 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 6993 | |
| 6994 | if (histtype == HIST_CMD) |
| 6995 | { |
| 6996 | /* Execute the command directly. */ |
| 6997 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 6998 | cmdwin_result = CAR; |
| 6999 | } |
| 7000 | else |
| 7001 | { |
| 7002 | /* First need to cancel what we were doing. */ |
| 7003 | ccline.cmdbuff = NULL; |
| 7004 | stuffcharReadbuff(':'); |
| 7005 | stuffReadbuff((char_u *)p); |
| 7006 | stuffcharReadbuff(CAR); |
| 7007 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7008 | } |
| 7009 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 7010 | { |
| 7011 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 7012 | cmdwin_result = CAR; |
| 7013 | } |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 7014 | else if (cmdwin_result == Ctrl_C) |
| 7015 | { |
| 7016 | /* :q or :close, don't execute any command |
| 7017 | * and don't modify the cmd window. */ |
| 7018 | ccline.cmdbuff = NULL; |
| 7019 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7020 | else |
| 7021 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 7022 | if (ccline.cmdbuff == NULL) |
| 7023 | cmdwin_result = Ctrl_C; |
| 7024 | else |
| 7025 | { |
| 7026 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 7027 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 7028 | ccline.cmdpos = curwin->w_cursor.col; |
| 7029 | if (ccline.cmdpos > ccline.cmdlen) |
| 7030 | ccline.cmdpos = ccline.cmdlen; |
| 7031 | if (cmdwin_result == K_IGNORE) |
| 7032 | { |
| 7033 | set_cmdspos_cursor(); |
| 7034 | redrawcmd(); |
| 7035 | } |
| 7036 | } |
| 7037 | |
| 7038 | # ifdef FEAT_AUTOCMD |
| 7039 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7040 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7041 | # endif |
Bram Moolenaar | fa67fbe | 2015-06-25 18:20:36 +0200 | [diff] [blame] | 7042 | # ifdef FEAT_CONCEAL |
| 7043 | /* Avoid command-line window first character being concealed. */ |
| 7044 | curwin->w_p_cole = 0; |
| 7045 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7046 | wp = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7047 | set_bufref(&bufref, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7048 | win_goto(old_curwin); |
| 7049 | win_close(wp, TRUE); |
Bram Moolenaar | 8006d69 | 2010-03-02 17:23:21 +0100 | [diff] [blame] | 7050 | |
| 7051 | /* win_close() may have already wiped the buffer when 'bh' is |
| 7052 | * set to 'wipe' */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7053 | if (bufref_valid(&bufref)) |
| 7054 | close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7055 | |
| 7056 | /* Restore window sizes. */ |
| 7057 | win_size_restore(&winsizes); |
| 7058 | |
| 7059 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7060 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7061 | # endif |
| 7062 | } |
| 7063 | |
| 7064 | ga_clear(&winsizes); |
| 7065 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7066 | # ifdef FEAT_RIGHTLEFT |
| 7067 | cmdmsg_rl = save_cmdmsg_rl; |
| 7068 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7069 | |
| 7070 | State = save_State; |
| 7071 | # ifdef FEAT_MOUSE |
| 7072 | setmouse(); |
| 7073 | # endif |
| 7074 | |
| 7075 | return cmdwin_result; |
| 7076 | } |
| 7077 | #endif /* FEAT_CMDWIN */ |
| 7078 | |
| 7079 | /* |
| 7080 | * Used for commands that either take a simple command string argument, or: |
| 7081 | * cmd << endmarker |
| 7082 | * {script} |
| 7083 | * endmarker |
| 7084 | * Returns a pointer to allocated memory with {script} or NULL. |
| 7085 | */ |
| 7086 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7087 | script_get(exarg_T *eap, char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7088 | { |
| 7089 | char_u *theline; |
| 7090 | char *end_pattern = NULL; |
| 7091 | char dot[] = "."; |
| 7092 | garray_T ga; |
| 7093 | |
| 7094 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 7095 | return NULL; |
| 7096 | |
| 7097 | ga_init2(&ga, 1, 0x400); |
| 7098 | |
| 7099 | if (cmd[2] != NUL) |
| 7100 | end_pattern = (char *)skipwhite(cmd + 2); |
| 7101 | else |
| 7102 | end_pattern = dot; |
| 7103 | |
| 7104 | for (;;) |
| 7105 | { |
| 7106 | theline = eap->getline( |
| 7107 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7108 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7109 | #endif |
| 7110 | NUL, eap->cookie, 0); |
| 7111 | |
| 7112 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7113 | { |
| 7114 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7115 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7116 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7117 | |
| 7118 | ga_concat(&ga, theline); |
| 7119 | ga_append(&ga, '\n'); |
| 7120 | vim_free(theline); |
| 7121 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 7122 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7123 | |
| 7124 | return (char_u *)ga.ga_data; |
| 7125 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 7126 | |
| 7127 | #ifdef FEAT_SEARCH_EXTRA |
| 7128 | static void |
| 7129 | set_search_match(pos_T *t) |
| 7130 | { |
| 7131 | /* |
| 7132 | * First move cursor to end of match, then to the start. This |
| 7133 | * moves the whole match onto the screen when 'nowrap' is set. |
| 7134 | */ |
| 7135 | t->lnum += search_match_lines; |
| 7136 | t->col = search_match_endcol; |
| 7137 | if (t->lnum > curbuf->b_ml.ml_line_count) |
| 7138 | { |
| 7139 | t->lnum = curbuf->b_ml.ml_line_count; |
| 7140 | coladvance((colnr_T)MAXCOL); |
| 7141 | } |
| 7142 | } |
| 7143 | #endif |