Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4: |
| 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 |
| 180 | pos_T old_cursor; |
| 181 | colnr_T old_curswant; |
| 182 | colnr_T old_leftcol; |
| 183 | linenr_T old_topline; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 184 | pos_T cursor_start; |
| 185 | pos_T match_start = curwin->w_cursor; |
| 186 | pos_T match_end; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 187 | # ifdef FEAT_DIFF |
| 188 | int old_topfill; |
| 189 | # endif |
| 190 | linenr_T old_botline; |
| 191 | int did_incsearch = FALSE; |
| 192 | int incsearch_postponed = FALSE; |
| 193 | #endif |
| 194 | int did_wild_list = FALSE; /* did wild_list() recently */ |
| 195 | int wim_index = 0; /* index in wim_flags[] */ |
| 196 | int res; |
| 197 | int save_msg_scroll = msg_scroll; |
| 198 | int save_State = State; /* remember State when called */ |
| 199 | int some_key_typed = FALSE; /* one of the keys was typed */ |
| 200 | #ifdef FEAT_MOUSE |
| 201 | /* mouse drag and release events are ignored, unless they are |
| 202 | * preceded with a mouse down event */ |
| 203 | int ignore_drag_release = TRUE; |
| 204 | #endif |
| 205 | #ifdef FEAT_EVAL |
| 206 | int break_ctrl_c = FALSE; |
| 207 | #endif |
| 208 | expand_T xpc; |
| 209 | long *b_im_ptr = NULL; |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 210 | #if defined(FEAT_WILDMENU) || defined(FEAT_EVAL) || defined(FEAT_SEARCH_EXTRA) |
| 211 | /* Everything that may work recursively should save and restore the |
| 212 | * current command line in save_ccline. That includes update_screen(), a |
| 213 | * custom status line may invoke ":normal". */ |
| 214 | struct cmdline_info save_ccline; |
| 215 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 216 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 217 | #ifdef FEAT_EVAL |
| 218 | if (firstc == -1) |
| 219 | { |
| 220 | firstc = NUL; |
| 221 | break_ctrl_c = TRUE; |
| 222 | } |
| 223 | #endif |
| 224 | #ifdef FEAT_RIGHTLEFT |
| 225 | /* start without Hebrew mapping for a command line */ |
| 226 | if (firstc == ':' || firstc == '=' || firstc == '>') |
| 227 | cmd_hkmap = 0; |
| 228 | #endif |
| 229 | |
| 230 | ccline.overstrike = FALSE; /* always start in insert mode */ |
| 231 | #ifdef FEAT_SEARCH_EXTRA |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 232 | clearpos(&match_end); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 233 | old_cursor = curwin->w_cursor; /* needs to be restored later */ |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 234 | cursor_start = old_cursor; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 235 | old_curswant = curwin->w_curswant; |
| 236 | old_leftcol = curwin->w_leftcol; |
| 237 | old_topline = curwin->w_topline; |
| 238 | # ifdef FEAT_DIFF |
| 239 | old_topfill = curwin->w_topfill; |
| 240 | # endif |
| 241 | old_botline = curwin->w_botline; |
| 242 | #endif |
| 243 | |
| 244 | /* |
| 245 | * set some variables for redrawcmd() |
| 246 | */ |
| 247 | ccline.cmdfirstc = (firstc == '@' ? 0 : firstc); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 248 | ccline.cmdindent = (firstc > 0 ? indent : 0); |
| 249 | |
| 250 | /* alloc initial ccline.cmdbuff */ |
| 251 | alloc_cmdbuff(exmode_active ? 250 : indent + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 252 | if (ccline.cmdbuff == NULL) |
| 253 | return NULL; /* out of memory */ |
| 254 | ccline.cmdlen = ccline.cmdpos = 0; |
| 255 | ccline.cmdbuff[0] = NUL; |
| 256 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 257 | /* autoindent for :insert and :append */ |
| 258 | if (firstc <= 0) |
| 259 | { |
Bram Moolenaar | 2536d4f | 2015-07-17 13:22:51 +0200 | [diff] [blame] | 260 | vim_memset(ccline.cmdbuff, ' ', indent); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 261 | ccline.cmdbuff[indent] = NUL; |
| 262 | ccline.cmdpos = indent; |
| 263 | ccline.cmdspos = indent; |
| 264 | ccline.cmdlen = indent; |
| 265 | } |
| 266 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 267 | ExpandInit(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 268 | ccline.xpc = &xpc; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 269 | |
| 270 | #ifdef FEAT_RIGHTLEFT |
| 271 | if (curwin->w_p_rl && *curwin->w_p_rlc == 's' |
| 272 | && (firstc == '/' || firstc == '?')) |
| 273 | cmdmsg_rl = TRUE; |
| 274 | else |
| 275 | cmdmsg_rl = FALSE; |
| 276 | #endif |
| 277 | |
| 278 | redir_off = TRUE; /* don't redirect the typed command */ |
| 279 | if (!cmd_silent) |
| 280 | { |
| 281 | i = msg_scrolled; |
| 282 | msg_scrolled = 0; /* avoid wait_return message */ |
| 283 | gotocmdline(TRUE); |
| 284 | msg_scrolled += i; |
| 285 | redrawcmdprompt(); /* draw prompt or indent */ |
| 286 | set_cmdspos(); |
| 287 | } |
| 288 | xpc.xp_context = EXPAND_NOTHING; |
| 289 | xpc.xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 290 | #ifndef BACKSLASH_IN_FILENAME |
| 291 | xpc.xp_shell = FALSE; |
| 292 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 293 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 294 | #if defined(FEAT_EVAL) |
| 295 | if (ccline.input_fn) |
| 296 | { |
| 297 | xpc.xp_context = ccline.xp_context; |
| 298 | xpc.xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 299 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 300 | xpc.xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 301 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 302 | } |
| 303 | #endif |
| 304 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 305 | /* |
| 306 | * Avoid scrolling when called by a recursive do_cmdline(), e.g. when |
| 307 | * doing ":@0" when register 0 doesn't contain a CR. |
| 308 | */ |
| 309 | msg_scroll = FALSE; |
| 310 | |
| 311 | State = CMDLINE; |
| 312 | |
| 313 | if (firstc == '/' || firstc == '?' || firstc == '@') |
| 314 | { |
| 315 | /* Use ":lmap" mappings for search pattern and input(). */ |
| 316 | if (curbuf->b_p_imsearch == B_IMODE_USE_INSERT) |
| 317 | b_im_ptr = &curbuf->b_p_iminsert; |
| 318 | else |
| 319 | b_im_ptr = &curbuf->b_p_imsearch; |
| 320 | if (*b_im_ptr == B_IMODE_LMAP) |
| 321 | State |= LANGMAP; |
| 322 | #ifdef USE_IM_CONTROL |
| 323 | im_set_active(*b_im_ptr == B_IMODE_IM); |
| 324 | #endif |
| 325 | } |
| 326 | #ifdef USE_IM_CONTROL |
| 327 | else if (p_imcmdline) |
| 328 | im_set_active(TRUE); |
| 329 | #endif |
| 330 | |
| 331 | #ifdef FEAT_MOUSE |
| 332 | setmouse(); |
| 333 | #endif |
| 334 | #ifdef CURSOR_SHAPE |
| 335 | ui_cursor_shape(); /* may show different cursor shape */ |
| 336 | #endif |
| 337 | |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 338 | /* When inside an autocommand for writing "exiting" may be set and |
| 339 | * terminal mode set to cooked. Need to set raw mode here then. */ |
| 340 | settmode(TMODE_RAW); |
| 341 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 342 | #ifdef FEAT_CMDHIST |
| 343 | init_history(); |
| 344 | hiscnt = hislen; /* set hiscnt to impossible history value */ |
| 345 | histype = hist_char2type(firstc); |
| 346 | #endif |
| 347 | |
| 348 | #ifdef FEAT_DIGRAPHS |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 349 | do_digraph(-1); /* init digraph typeahead */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 350 | #endif |
| 351 | |
Bram Moolenaar | 15a35c4 | 2014-06-25 12:26:46 +0200 | [diff] [blame] | 352 | /* If something above caused an error, reset the flags, we do want to type |
| 353 | * and execute commands. Display may be messed up a bit. */ |
| 354 | if (did_emsg) |
| 355 | redrawcmd(); |
| 356 | did_emsg = FALSE; |
| 357 | got_int = FALSE; |
| 358 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 359 | /* |
| 360 | * Collect the command string, handling editing keys. |
| 361 | */ |
| 362 | for (;;) |
| 363 | { |
Bram Moolenaar | 29b2d26 | 2006-09-10 19:07:28 +0000 | [diff] [blame] | 364 | redir_off = TRUE; /* Don't redirect the typed command. |
| 365 | Repeated, because a ":redir" inside |
| 366 | completion may switch it on. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 367 | #ifdef USE_ON_FLY_SCROLL |
| 368 | dont_scroll = FALSE; /* allow scrolling here */ |
| 369 | #endif |
| 370 | quit_more = FALSE; /* reset after CTRL-D which had a more-prompt */ |
| 371 | |
| 372 | cursorcmd(); /* set the cursor on the right spot */ |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 373 | |
| 374 | /* Get a character. Ignore K_IGNORE, it should not do anything, such |
| 375 | * as stop completion. */ |
| 376 | do |
| 377 | { |
| 378 | c = safe_vgetc(); |
| 379 | } while (c == K_IGNORE); |
| 380 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 381 | if (KeyTyped) |
| 382 | { |
| 383 | some_key_typed = TRUE; |
| 384 | #ifdef FEAT_RIGHTLEFT |
| 385 | if (cmd_hkmap) |
| 386 | c = hkmap(c); |
| 387 | # ifdef FEAT_FKMAP |
| 388 | if (cmd_fkmap) |
| 389 | c = cmdl_fkmap(c); |
| 390 | # endif |
| 391 | if (cmdmsg_rl && !KeyStuffed) |
| 392 | { |
| 393 | /* Invert horizontal movements and operations. Only when |
| 394 | * typed by the user directly, not when the result of a |
| 395 | * mapping. */ |
| 396 | switch (c) |
| 397 | { |
| 398 | case K_RIGHT: c = K_LEFT; break; |
| 399 | case K_S_RIGHT: c = K_S_LEFT; break; |
| 400 | case K_C_RIGHT: c = K_C_LEFT; break; |
| 401 | case K_LEFT: c = K_RIGHT; break; |
| 402 | case K_S_LEFT: c = K_S_RIGHT; break; |
| 403 | case K_C_LEFT: c = K_C_RIGHT; break; |
| 404 | } |
| 405 | } |
| 406 | #endif |
| 407 | } |
| 408 | |
| 409 | /* |
| 410 | * Ignore got_int when CTRL-C was typed here. |
| 411 | * Don't ignore it in :global, we really need to break then, e.g., for |
| 412 | * ":g/pat/normal /pat" (without the <CR>). |
| 413 | * Don't ignore it for the input() function. |
| 414 | */ |
| 415 | if ((c == Ctrl_C |
| 416 | #ifdef UNIX |
| 417 | || c == intr_char |
| 418 | #endif |
| 419 | ) |
| 420 | #if defined(FEAT_EVAL) || defined(FEAT_CRYPT) |
| 421 | && firstc != '@' |
| 422 | #endif |
| 423 | #ifdef FEAT_EVAL |
| 424 | && !break_ctrl_c |
| 425 | #endif |
| 426 | && !global_busy) |
| 427 | got_int = FALSE; |
| 428 | |
| 429 | #ifdef FEAT_CMDHIST |
| 430 | /* free old command line when finished moving around in the history |
| 431 | * list */ |
| 432 | if (lookfor != NULL |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 433 | && c != K_S_DOWN && c != K_S_UP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 434 | && c != K_DOWN && c != K_UP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 435 | && c != K_PAGEDOWN && c != K_PAGEUP |
| 436 | && c != K_KPAGEDOWN && c != K_KPAGEUP |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 437 | && c != K_LEFT && c != K_RIGHT |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 438 | && (xpc.xp_numfiles > 0 || (c != Ctrl_P && c != Ctrl_N))) |
| 439 | { |
| 440 | vim_free(lookfor); |
| 441 | lookfor = NULL; |
| 442 | } |
| 443 | #endif |
| 444 | |
| 445 | /* |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 446 | * When there are matching completions to select <S-Tab> works like |
| 447 | * CTRL-P (unless 'wc' is <S-Tab>). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 448 | */ |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 449 | if (c != p_wc && c == K_S_TAB && xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 450 | c = Ctrl_P; |
| 451 | |
| 452 | #ifdef FEAT_WILDMENU |
| 453 | /* Special translations for 'wildmenu' */ |
| 454 | if (did_wild_list && p_wmnu) |
| 455 | { |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 456 | if (c == K_LEFT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 457 | c = Ctrl_P; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 458 | else if (c == K_RIGHT) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 459 | c = Ctrl_N; |
| 460 | } |
| 461 | /* Hitting CR after "emenu Name.": complete submenu */ |
| 462 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu |
| 463 | && ccline.cmdpos > 1 |
| 464 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.' |
| 465 | && ccline.cmdbuff[ccline.cmdpos - 2] != '\\' |
| 466 | && (c == '\n' || c == '\r' || c == K_KENTER)) |
| 467 | c = K_DOWN; |
| 468 | #endif |
| 469 | |
| 470 | /* free expanded names when finished walking through matches */ |
| 471 | if (xpc.xp_numfiles != -1 |
| 472 | && !(c == p_wc && KeyTyped) && c != p_wcm |
| 473 | && c != Ctrl_N && c != Ctrl_P && c != Ctrl_A |
| 474 | && c != Ctrl_L) |
| 475 | { |
| 476 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 477 | did_wild_list = FALSE; |
| 478 | #ifdef FEAT_WILDMENU |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 479 | if (!p_wmnu || (c != K_UP && c != K_DOWN)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 480 | #endif |
| 481 | xpc.xp_context = EXPAND_NOTHING; |
| 482 | wim_index = 0; |
| 483 | #ifdef FEAT_WILDMENU |
| 484 | if (p_wmnu && wild_menu_showing != 0) |
| 485 | { |
| 486 | int skt = KeyTyped; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 487 | int old_RedrawingDisabled = RedrawingDisabled; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 488 | |
| 489 | if (ccline.input_fn) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 490 | RedrawingDisabled = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 491 | |
| 492 | if (wild_menu_showing == WM_SCROLLED) |
| 493 | { |
| 494 | /* Entered command line, move it up */ |
| 495 | cmdline_row--; |
| 496 | redrawcmd(); |
| 497 | } |
| 498 | else if (save_p_ls != -1) |
| 499 | { |
| 500 | /* restore 'laststatus' and 'winminheight' */ |
| 501 | p_ls = save_p_ls; |
| 502 | p_wmh = save_p_wmh; |
| 503 | last_status(FALSE); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 504 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 505 | update_screen(VALID); /* redraw the screen NOW */ |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 506 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 507 | redrawcmd(); |
| 508 | save_p_ls = -1; |
| 509 | } |
| 510 | else |
| 511 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 512 | win_redraw_last_status(topframe); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 513 | redraw_statuslines(); |
| 514 | } |
| 515 | KeyTyped = skt; |
| 516 | wild_menu_showing = 0; |
Bram Moolenaar | 1e01546 | 2005-09-25 22:16:38 +0000 | [diff] [blame] | 517 | if (ccline.input_fn) |
| 518 | RedrawingDisabled = old_RedrawingDisabled; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 519 | } |
| 520 | #endif |
| 521 | } |
| 522 | |
| 523 | #ifdef FEAT_WILDMENU |
| 524 | /* Special translations for 'wildmenu' */ |
| 525 | if (xpc.xp_context == EXPAND_MENUNAMES && p_wmnu) |
| 526 | { |
| 527 | /* Hitting <Down> after "emenu Name.": complete submenu */ |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 528 | if (c == K_DOWN && ccline.cmdpos > 0 |
| 529 | && ccline.cmdbuff[ccline.cmdpos - 1] == '.') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 530 | c = p_wc; |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 531 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 532 | { |
| 533 | /* Hitting <Up>: Remove one submenu name in front of the |
| 534 | * cursor */ |
| 535 | int found = FALSE; |
| 536 | |
| 537 | j = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 538 | i = 0; |
| 539 | while (--j > 0) |
| 540 | { |
| 541 | /* check for start of menu name */ |
| 542 | if (ccline.cmdbuff[j] == ' ' |
| 543 | && ccline.cmdbuff[j - 1] != '\\') |
| 544 | { |
| 545 | i = j + 1; |
| 546 | break; |
| 547 | } |
| 548 | /* check for start of submenu name */ |
| 549 | if (ccline.cmdbuff[j] == '.' |
| 550 | && ccline.cmdbuff[j - 1] != '\\') |
| 551 | { |
| 552 | if (found) |
| 553 | { |
| 554 | i = j + 1; |
| 555 | break; |
| 556 | } |
| 557 | else |
| 558 | found = TRUE; |
| 559 | } |
| 560 | } |
| 561 | if (i > 0) |
| 562 | cmdline_del(i); |
| 563 | c = p_wc; |
| 564 | xpc.xp_context = EXPAND_NOTHING; |
| 565 | } |
| 566 | } |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 567 | if ((xpc.xp_context == EXPAND_FILES |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 568 | || xpc.xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 569 | || xpc.xp_context == EXPAND_SHELLCMD) && p_wmnu) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 570 | { |
| 571 | char_u upseg[5]; |
| 572 | |
| 573 | upseg[0] = PATHSEP; |
| 574 | upseg[1] = '.'; |
| 575 | upseg[2] = '.'; |
| 576 | upseg[3] = PATHSEP; |
| 577 | upseg[4] = NUL; |
| 578 | |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 579 | if (c == K_DOWN |
| 580 | && ccline.cmdpos > 0 |
| 581 | && ccline.cmdbuff[ccline.cmdpos - 1] == PATHSEP |
| 582 | && (ccline.cmdpos < 3 |
| 583 | || ccline.cmdbuff[ccline.cmdpos - 2] != '.' |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 584 | || ccline.cmdbuff[ccline.cmdpos - 3] != '.')) |
| 585 | { |
| 586 | /* go down a directory */ |
| 587 | c = p_wc; |
| 588 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 589 | 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] | 590 | { |
| 591 | /* If in a direct ancestor, strip off one ../ to go down */ |
| 592 | int found = FALSE; |
| 593 | |
| 594 | j = ccline.cmdpos; |
| 595 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 596 | while (--j > i) |
| 597 | { |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 598 | #ifdef FEAT_MBYTE |
| 599 | if (has_mbyte) |
| 600 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 601 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 602 | if (vim_ispathsep(ccline.cmdbuff[j])) |
| 603 | { |
| 604 | found = TRUE; |
| 605 | break; |
| 606 | } |
| 607 | } |
| 608 | if (found |
| 609 | && ccline.cmdbuff[j - 1] == '.' |
| 610 | && ccline.cmdbuff[j - 2] == '.' |
| 611 | && (vim_ispathsep(ccline.cmdbuff[j - 3]) || j == i + 2)) |
| 612 | { |
| 613 | cmdline_del(j - 2); |
| 614 | c = p_wc; |
| 615 | } |
| 616 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 617 | else if (c == K_UP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 618 | { |
| 619 | /* go up a directory */ |
| 620 | int found = FALSE; |
| 621 | |
| 622 | j = ccline.cmdpos - 1; |
| 623 | i = (int)(xpc.xp_pattern - ccline.cmdbuff); |
| 624 | while (--j > i) |
| 625 | { |
| 626 | #ifdef FEAT_MBYTE |
| 627 | if (has_mbyte) |
| 628 | j -= (*mb_head_off)(ccline.cmdbuff, ccline.cmdbuff + j); |
| 629 | #endif |
| 630 | if (vim_ispathsep(ccline.cmdbuff[j]) |
| 631 | #ifdef BACKSLASH_IN_FILENAME |
Bram Moolenaar | 6aa2cd4 | 2016-02-16 15:06:59 +0100 | [diff] [blame] | 632 | && vim_strchr((char_u *)" *?[{`$%#", |
| 633 | ccline.cmdbuff[j + 1]) == NULL |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 634 | #endif |
| 635 | ) |
| 636 | { |
| 637 | if (found) |
| 638 | { |
| 639 | i = j + 1; |
| 640 | break; |
| 641 | } |
| 642 | else |
| 643 | found = TRUE; |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | if (!found) |
| 648 | j = i; |
| 649 | else if (STRNCMP(ccline.cmdbuff + j, upseg, 4) == 0) |
| 650 | j += 4; |
| 651 | else if (STRNCMP(ccline.cmdbuff + j, upseg + 1, 3) == 0 |
| 652 | && j == i) |
| 653 | j += 3; |
| 654 | else |
| 655 | j = 0; |
| 656 | if (j > 0) |
| 657 | { |
| 658 | /* TODO this is only for DOS/UNIX systems - need to put in |
| 659 | * machine-specific stuff here and in upseg init */ |
| 660 | cmdline_del(j); |
| 661 | put_on_cmdline(upseg + 1, 3, FALSE); |
| 662 | } |
| 663 | else if (ccline.cmdpos > i) |
| 664 | cmdline_del(i); |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 665 | |
| 666 | /* Now complete in the new directory. Set KeyTyped in case the |
| 667 | * Up key came from a mapping. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 668 | c = p_wc; |
Bram Moolenaar | 96a8964 | 2011-12-08 18:44:51 +0100 | [diff] [blame] | 669 | KeyTyped = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 670 | } |
| 671 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 672 | |
| 673 | #endif /* FEAT_WILDMENU */ |
| 674 | |
| 675 | /* CTRL-\ CTRL-N goes to Normal mode, CTRL-\ CTRL-G goes to Insert |
| 676 | * mode when 'insertmode' is set, CTRL-\ e prompts for an expression. */ |
| 677 | if (c == Ctrl_BSL) |
| 678 | { |
| 679 | ++no_mapping; |
| 680 | ++allow_keys; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 681 | c = plain_vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 682 | --no_mapping; |
| 683 | --allow_keys; |
Bram Moolenaar | b735681 | 2012-10-11 04:04:37 +0200 | [diff] [blame] | 684 | /* CTRL-\ e doesn't work when obtaining an expression, unless it |
| 685 | * is in a mapping. */ |
| 686 | if (c != Ctrl_N && c != Ctrl_G && (c != 'e' |
| 687 | || (ccline.cmdfirstc == '=' && KeyTyped))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 688 | { |
| 689 | vungetc(c); |
| 690 | c = Ctrl_BSL; |
| 691 | } |
| 692 | #ifdef FEAT_EVAL |
| 693 | else if (c == 'e') |
| 694 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 695 | char_u *p = NULL; |
| 696 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 697 | |
| 698 | /* |
| 699 | * Replace the command line with the result of an expression. |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 700 | * Need to save and restore the current command line, to be |
| 701 | * able to enter a new one... |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 702 | */ |
| 703 | if (ccline.cmdpos == ccline.cmdlen) |
| 704 | new_cmdpos = 99999; /* keep it at the end */ |
| 705 | else |
| 706 | new_cmdpos = ccline.cmdpos; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 707 | |
| 708 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 709 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 710 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 711 | if (c == '=') |
| 712 | { |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 713 | /* Need to save and restore ccline. And set "textlock" |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 714 | * to avoid nasty things like going to another buffer when |
| 715 | * evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 716 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 717 | ++textlock; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 718 | p = get_expr_line(); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 719 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 720 | restore_cmdline(&save_ccline); |
| 721 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 722 | if (p != NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 723 | { |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 724 | len = (int)STRLEN(p); |
| 725 | if (realloc_cmdbuff(len + 1) == OK) |
| 726 | { |
| 727 | ccline.cmdlen = len; |
| 728 | STRCPY(ccline.cmdbuff, p); |
| 729 | vim_free(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 730 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 731 | /* Restore the cursor or use the position set with |
| 732 | * set_cmdline_pos(). */ |
| 733 | if (new_cmdpos > ccline.cmdlen) |
| 734 | ccline.cmdpos = ccline.cmdlen; |
| 735 | else |
| 736 | ccline.cmdpos = new_cmdpos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 737 | |
Bram Moolenaar | fc3c83e | 2010-10-27 12:58:23 +0200 | [diff] [blame] | 738 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 739 | redrawcmd(); |
| 740 | goto cmdline_changed; |
| 741 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 742 | } |
| 743 | } |
| 744 | beep_flush(); |
Bram Moolenaar | 66b4bf8 | 2010-11-16 14:06:08 +0100 | [diff] [blame] | 745 | got_int = FALSE; /* don't abandon the command line */ |
| 746 | did_emsg = FALSE; |
| 747 | emsg_on_display = FALSE; |
| 748 | redrawcmd(); |
| 749 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 750 | } |
| 751 | #endif |
| 752 | else |
| 753 | { |
| 754 | if (c == Ctrl_G && p_im && restart_edit == 0) |
| 755 | restart_edit = 'a'; |
| 756 | gotesc = TRUE; /* will free ccline.cmdbuff after putting it |
| 757 | in history */ |
| 758 | goto returncmd; /* back to Normal mode */ |
| 759 | } |
| 760 | } |
| 761 | |
| 762 | #ifdef FEAT_CMDWIN |
| 763 | if (c == cedit_key || c == K_CMDWIN) |
| 764 | { |
Bram Moolenaar | 58da707 | 2014-09-09 18:45:49 +0200 | [diff] [blame] | 765 | if (ex_normal_busy == 0 && got_int == FALSE) |
| 766 | { |
| 767 | /* |
| 768 | * Open a window to edit the command line (and history). |
| 769 | */ |
| 770 | c = ex_window(); |
| 771 | some_key_typed = TRUE; |
| 772 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 773 | } |
| 774 | # ifdef FEAT_DIGRAPHS |
| 775 | else |
| 776 | # endif |
| 777 | #endif |
| 778 | #ifdef FEAT_DIGRAPHS |
| 779 | c = do_digraph(c); |
| 780 | #endif |
| 781 | |
| 782 | if (c == '\n' || c == '\r' || c == K_KENTER || (c == ESC |
| 783 | && (!KeyTyped || vim_strchr(p_cpo, CPO_ESC) != NULL))) |
| 784 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 785 | /* In Ex mode a backslash escapes a newline. */ |
| 786 | if (exmode_active |
| 787 | && c != ESC |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 788 | && ccline.cmdpos == ccline.cmdlen |
Bram Moolenaar | 5f2c5db | 2007-07-17 16:15:36 +0000 | [diff] [blame] | 789 | && ccline.cmdpos > 0 |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 790 | && ccline.cmdbuff[ccline.cmdpos - 1] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 791 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 792 | if (c == K_KENTER) |
| 793 | c = '\n'; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 794 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 795 | else |
| 796 | { |
| 797 | gotesc = FALSE; /* Might have typed ESC previously, don't |
| 798 | truncate the cmdline now. */ |
| 799 | if (ccheck_abbr(c + ABBR_OFF)) |
| 800 | goto cmdline_changed; |
| 801 | if (!cmd_silent) |
| 802 | { |
| 803 | windgoto(msg_row, 0); |
| 804 | out_flush(); |
| 805 | } |
| 806 | break; |
| 807 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 808 | } |
| 809 | |
| 810 | /* |
| 811 | * Completion for 'wildchar' or 'wildcharm' key. |
| 812 | * - hitting <ESC> twice means: abandon command line. |
| 813 | * - wildcard expansion is only done when the 'wildchar' key is really |
| 814 | * typed, not when it comes from a macro |
| 815 | */ |
| 816 | if ((c == p_wc && !gotesc && KeyTyped) || c == p_wcm) |
| 817 | { |
| 818 | if (xpc.xp_numfiles > 0) /* typed p_wc at least twice */ |
| 819 | { |
| 820 | /* if 'wildmode' contains "list" may still need to list */ |
| 821 | if (xpc.xp_numfiles > 1 |
| 822 | && !did_wild_list |
| 823 | && (wim_flags[wim_index] & WIM_LIST)) |
| 824 | { |
| 825 | (void)showmatches(&xpc, FALSE); |
| 826 | redrawcmd(); |
| 827 | did_wild_list = TRUE; |
| 828 | } |
| 829 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 830 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 831 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 832 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 833 | res = nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 834 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 835 | else |
| 836 | res = OK; /* don't insert 'wildchar' now */ |
| 837 | } |
| 838 | else /* typed p_wc first time */ |
| 839 | { |
| 840 | wim_index = 0; |
| 841 | j = ccline.cmdpos; |
| 842 | /* if 'wildmode' first contains "longest", get longest |
| 843 | * common part */ |
| 844 | if (wim_flags[0] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 845 | res = nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 846 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 847 | else |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 848 | res = nextwild(&xpc, WILD_EXPAND_KEEP, WILD_NO_BEEP, |
| 849 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 850 | |
| 851 | /* if interrupted while completing, behave like it failed */ |
| 852 | if (got_int) |
| 853 | { |
| 854 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 855 | got_int = FALSE; /* don't abandon the command line */ |
| 856 | (void)ExpandOne(&xpc, NULL, NULL, 0, WILD_FREE); |
| 857 | #ifdef FEAT_WILDMENU |
| 858 | xpc.xp_context = EXPAND_NOTHING; |
| 859 | #endif |
| 860 | goto cmdline_changed; |
| 861 | } |
| 862 | |
| 863 | /* when more than one match, and 'wildmode' first contains |
| 864 | * "list", or no change and 'wildmode' contains "longest,list", |
| 865 | * list all matches */ |
| 866 | if (res == OK && xpc.xp_numfiles > 1) |
| 867 | { |
| 868 | /* a "longest" that didn't do anything is skipped (but not |
| 869 | * "list:longest") */ |
| 870 | if (wim_flags[0] == WIM_LONGEST && ccline.cmdpos == j) |
| 871 | wim_index = 1; |
| 872 | if ((wim_flags[wim_index] & WIM_LIST) |
| 873 | #ifdef FEAT_WILDMENU |
| 874 | || (p_wmnu && (wim_flags[wim_index] & WIM_FULL) != 0) |
| 875 | #endif |
| 876 | ) |
| 877 | { |
| 878 | if (!(wim_flags[0] & WIM_LONGEST)) |
| 879 | { |
| 880 | #ifdef FEAT_WILDMENU |
| 881 | int p_wmnu_save = p_wmnu; |
| 882 | p_wmnu = 0; |
| 883 | #endif |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 884 | /* remove match */ |
| 885 | nextwild(&xpc, WILD_PREV, 0, firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 886 | #ifdef FEAT_WILDMENU |
| 887 | p_wmnu = p_wmnu_save; |
| 888 | #endif |
| 889 | } |
| 890 | #ifdef FEAT_WILDMENU |
| 891 | (void)showmatches(&xpc, p_wmnu |
| 892 | && ((wim_flags[wim_index] & WIM_LIST) == 0)); |
| 893 | #else |
| 894 | (void)showmatches(&xpc, FALSE); |
| 895 | #endif |
| 896 | redrawcmd(); |
| 897 | did_wild_list = TRUE; |
| 898 | if (wim_flags[wim_index] & WIM_LONGEST) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 899 | nextwild(&xpc, WILD_LONGEST, WILD_NO_BEEP, |
| 900 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 901 | else if (wim_flags[wim_index] & WIM_FULL) |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 902 | nextwild(&xpc, WILD_NEXT, WILD_NO_BEEP, |
| 903 | firstc != '@'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 904 | } |
| 905 | else |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 906 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 907 | } |
| 908 | #ifdef FEAT_WILDMENU |
| 909 | else if (xpc.xp_numfiles == -1) |
| 910 | xpc.xp_context = EXPAND_NOTHING; |
| 911 | #endif |
| 912 | } |
| 913 | if (wim_index < 3) |
| 914 | ++wim_index; |
| 915 | if (c == ESC) |
| 916 | gotesc = TRUE; |
| 917 | if (res == OK) |
| 918 | goto cmdline_changed; |
| 919 | } |
| 920 | |
| 921 | gotesc = FALSE; |
| 922 | |
| 923 | /* <S-Tab> goes to last match, in a clumsy way */ |
| 924 | if (c == K_S_TAB && KeyTyped) |
| 925 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 926 | if (nextwild(&xpc, WILD_EXPAND_KEEP, 0, firstc != '@') == OK |
| 927 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK |
| 928 | && nextwild(&xpc, WILD_PREV, 0, firstc != '@') == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 929 | goto cmdline_changed; |
| 930 | } |
| 931 | |
| 932 | if (c == NUL || c == K_ZERO) /* NUL is stored as NL */ |
| 933 | c = NL; |
| 934 | |
| 935 | do_abbr = TRUE; /* default: check for abbreviation */ |
| 936 | |
| 937 | /* |
| 938 | * Big switch for a typed command line character. |
| 939 | */ |
| 940 | switch (c) |
| 941 | { |
| 942 | case K_BS: |
| 943 | case Ctrl_H: |
| 944 | case K_DEL: |
| 945 | case K_KDEL: |
| 946 | case Ctrl_W: |
| 947 | #ifdef FEAT_FKMAP |
| 948 | if (cmd_fkmap && c == K_BS) |
| 949 | c = K_DEL; |
| 950 | #endif |
| 951 | if (c == K_KDEL) |
| 952 | c = K_DEL; |
| 953 | |
| 954 | /* |
| 955 | * delete current character is the same as backspace on next |
| 956 | * character, except at end of line |
| 957 | */ |
| 958 | if (c == K_DEL && ccline.cmdpos != ccline.cmdlen) |
| 959 | ++ccline.cmdpos; |
| 960 | #ifdef FEAT_MBYTE |
| 961 | if (has_mbyte && c == K_DEL) |
| 962 | ccline.cmdpos += mb_off_next(ccline.cmdbuff, |
| 963 | ccline.cmdbuff + ccline.cmdpos); |
| 964 | #endif |
| 965 | if (ccline.cmdpos > 0) |
| 966 | { |
| 967 | char_u *p; |
| 968 | |
| 969 | j = ccline.cmdpos; |
| 970 | p = ccline.cmdbuff + j; |
| 971 | #ifdef FEAT_MBYTE |
| 972 | if (has_mbyte) |
| 973 | { |
| 974 | p = mb_prevptr(ccline.cmdbuff, p); |
| 975 | if (c == Ctrl_W) |
| 976 | { |
| 977 | while (p > ccline.cmdbuff && vim_isspace(*p)) |
| 978 | p = mb_prevptr(ccline.cmdbuff, p); |
| 979 | i = mb_get_class(p); |
| 980 | while (p > ccline.cmdbuff && mb_get_class(p) == i) |
| 981 | p = mb_prevptr(ccline.cmdbuff, p); |
| 982 | if (mb_get_class(p) != i) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 983 | p += (*mb_ptr2len)(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 984 | } |
| 985 | } |
| 986 | else |
| 987 | #endif |
| 988 | if (c == Ctrl_W) |
| 989 | { |
| 990 | while (p > ccline.cmdbuff && vim_isspace(p[-1])) |
| 991 | --p; |
| 992 | i = vim_iswordc(p[-1]); |
| 993 | while (p > ccline.cmdbuff && !vim_isspace(p[-1]) |
| 994 | && vim_iswordc(p[-1]) == i) |
| 995 | --p; |
| 996 | } |
| 997 | else |
| 998 | --p; |
| 999 | ccline.cmdpos = (int)(p - ccline.cmdbuff); |
| 1000 | ccline.cmdlen -= j - ccline.cmdpos; |
| 1001 | i = ccline.cmdpos; |
| 1002 | while (i < ccline.cmdlen) |
| 1003 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1004 | |
| 1005 | /* Truncate at the end, required for multi-byte chars. */ |
| 1006 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1007 | #ifdef FEAT_SEARCH_EXTRA |
| 1008 | if (ccline.cmdlen == 0) |
| 1009 | old_cursor = cursor_start; |
| 1010 | else |
| 1011 | { |
| 1012 | old_cursor = match_start; |
| 1013 | decl(&old_cursor); |
| 1014 | } |
| 1015 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1016 | redrawcmd(); |
| 1017 | } |
| 1018 | else if (ccline.cmdlen == 0 && c != Ctrl_W |
| 1019 | && ccline.cmdprompt == NULL && indent == 0) |
| 1020 | { |
| 1021 | /* In ex and debug mode it doesn't make sense to return. */ |
| 1022 | if (exmode_active |
| 1023 | #ifdef FEAT_EVAL |
| 1024 | || ccline.cmdfirstc == '>' |
| 1025 | #endif |
| 1026 | ) |
| 1027 | goto cmdline_not_changed; |
| 1028 | |
| 1029 | vim_free(ccline.cmdbuff); /* no commandline to return */ |
| 1030 | ccline.cmdbuff = NULL; |
| 1031 | if (!cmd_silent) |
| 1032 | { |
| 1033 | #ifdef FEAT_RIGHTLEFT |
| 1034 | if (cmdmsg_rl) |
| 1035 | msg_col = Columns; |
| 1036 | else |
| 1037 | #endif |
| 1038 | msg_col = 0; |
| 1039 | msg_putchar(' '); /* delete ':' */ |
| 1040 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1041 | #ifdef FEAT_SEARCH_EXTRA |
| 1042 | if (ccline.cmdlen == 0) |
| 1043 | old_cursor = cursor_start; |
| 1044 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1045 | redraw_cmdline = TRUE; |
| 1046 | goto returncmd; /* back to cmd mode */ |
| 1047 | } |
| 1048 | goto cmdline_changed; |
| 1049 | |
| 1050 | case K_INS: |
| 1051 | case K_KINS: |
| 1052 | #ifdef FEAT_FKMAP |
| 1053 | /* if Farsi mode set, we are in reverse insert mode - |
| 1054 | Do not change the mode */ |
| 1055 | if (cmd_fkmap) |
| 1056 | beep_flush(); |
| 1057 | else |
| 1058 | #endif |
| 1059 | ccline.overstrike = !ccline.overstrike; |
| 1060 | #ifdef CURSOR_SHAPE |
| 1061 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1062 | #endif |
| 1063 | goto cmdline_not_changed; |
| 1064 | |
| 1065 | case Ctrl_HAT: |
Bram Moolenaar | 97b2ad3 | 2006-03-18 21:40:56 +0000 | [diff] [blame] | 1066 | if (map_to_exists_mode((char_u *)"", LANGMAP, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1067 | { |
| 1068 | /* ":lmap" mappings exists, toggle use of mappings. */ |
| 1069 | State ^= LANGMAP; |
| 1070 | #ifdef USE_IM_CONTROL |
| 1071 | im_set_active(FALSE); /* Disable input method */ |
| 1072 | #endif |
| 1073 | if (b_im_ptr != NULL) |
| 1074 | { |
| 1075 | if (State & LANGMAP) |
| 1076 | *b_im_ptr = B_IMODE_LMAP; |
| 1077 | else |
| 1078 | *b_im_ptr = B_IMODE_NONE; |
| 1079 | } |
| 1080 | } |
| 1081 | #ifdef USE_IM_CONTROL |
| 1082 | else |
| 1083 | { |
| 1084 | /* There are no ":lmap" mappings, toggle IM. When |
| 1085 | * 'imdisable' is set don't try getting the status, it's |
| 1086 | * always off. */ |
| 1087 | if ((p_imdisable && b_im_ptr != NULL) |
| 1088 | ? *b_im_ptr == B_IMODE_IM : im_get_status()) |
| 1089 | { |
| 1090 | im_set_active(FALSE); /* Disable input method */ |
| 1091 | if (b_im_ptr != NULL) |
| 1092 | *b_im_ptr = B_IMODE_NONE; |
| 1093 | } |
| 1094 | else |
| 1095 | { |
| 1096 | im_set_active(TRUE); /* Enable input method */ |
| 1097 | if (b_im_ptr != NULL) |
| 1098 | *b_im_ptr = B_IMODE_IM; |
| 1099 | } |
| 1100 | } |
| 1101 | #endif |
| 1102 | if (b_im_ptr != NULL) |
| 1103 | { |
| 1104 | if (b_im_ptr == &curbuf->b_p_iminsert) |
| 1105 | set_iminsert_global(); |
| 1106 | else |
| 1107 | set_imsearch_global(); |
| 1108 | } |
| 1109 | #ifdef CURSOR_SHAPE |
| 1110 | ui_cursor_shape(); /* may show different cursor shape */ |
| 1111 | #endif |
| 1112 | #if defined(FEAT_WINDOWS) && defined(FEAT_KEYMAP) |
| 1113 | /* Show/unshow value of 'keymap' in status lines later. */ |
| 1114 | status_redraw_curbuf(); |
| 1115 | #endif |
| 1116 | goto cmdline_not_changed; |
| 1117 | |
| 1118 | /* case '@': only in very old vi */ |
| 1119 | case Ctrl_U: |
| 1120 | /* delete all characters left of the cursor */ |
| 1121 | j = ccline.cmdpos; |
| 1122 | ccline.cmdlen -= j; |
| 1123 | i = ccline.cmdpos = 0; |
| 1124 | while (i < ccline.cmdlen) |
| 1125 | ccline.cmdbuff[i++] = ccline.cmdbuff[j++]; |
| 1126 | /* Truncate at the end, required for multi-byte chars. */ |
| 1127 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1128 | #ifdef FEAT_SEARCH_EXTRA |
| 1129 | if (ccline.cmdlen == 0) |
| 1130 | old_cursor = cursor_start; |
| 1131 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1132 | redrawcmd(); |
| 1133 | goto cmdline_changed; |
| 1134 | |
| 1135 | #ifdef FEAT_CLIPBOARD |
| 1136 | case Ctrl_Y: |
| 1137 | /* Copy the modeless selection, if there is one. */ |
| 1138 | if (clip_star.state != SELECT_CLEARED) |
| 1139 | { |
| 1140 | if (clip_star.state == SELECT_DONE) |
| 1141 | clip_copy_modeless_selection(TRUE); |
| 1142 | goto cmdline_not_changed; |
| 1143 | } |
| 1144 | break; |
| 1145 | #endif |
| 1146 | |
| 1147 | case ESC: /* get here if p_wc != ESC or when ESC typed twice */ |
| 1148 | case Ctrl_C: |
Bram Moolenaar | f4d1145 | 2005-12-02 00:46:37 +0000 | [diff] [blame] | 1149 | /* In exmode it doesn't make sense to return. Except when |
Bram Moolenaar | 7c62692 | 2005-02-07 22:01:03 +0000 | [diff] [blame] | 1150 | * ":normal" runs out of characters. */ |
| 1151 | if (exmode_active |
Bram Moolenaar | e2c3810 | 2016-01-31 14:55:40 +0100 | [diff] [blame] | 1152 | && (ex_normal_busy == 0 || typebuf.tb_len > 0)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1153 | goto cmdline_not_changed; |
| 1154 | |
| 1155 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1156 | putting it in history */ |
| 1157 | goto returncmd; /* back to cmd mode */ |
| 1158 | |
| 1159 | case Ctrl_R: /* insert register */ |
| 1160 | #ifdef USE_ON_FLY_SCROLL |
| 1161 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1162 | #endif |
| 1163 | putcmdline('"', TRUE); |
| 1164 | ++no_mapping; |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1165 | i = c = plain_vgetc(); /* CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1166 | if (i == Ctrl_O) |
| 1167 | i = Ctrl_R; /* CTRL-R CTRL-O == CTRL-R CTRL-R */ |
| 1168 | if (i == Ctrl_R) |
Bram Moolenaar | 61abfd1 | 2007-09-13 16:26:47 +0000 | [diff] [blame] | 1169 | c = plain_vgetc(); /* CTRL-R CTRL-R <char> */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1170 | --no_mapping; |
| 1171 | #ifdef FEAT_EVAL |
| 1172 | /* |
| 1173 | * Insert the result of an expression. |
| 1174 | * Need to save the current command line, to be able to enter |
| 1175 | * a new one... |
| 1176 | */ |
| 1177 | new_cmdpos = -1; |
| 1178 | if (c == '=') |
| 1179 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1180 | if (ccline.cmdfirstc == '=')/* can't do this recursively */ |
| 1181 | { |
| 1182 | beep_flush(); |
| 1183 | c = ESC; |
| 1184 | } |
| 1185 | else |
| 1186 | { |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1187 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1188 | c = get_expr_register(); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 1189 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1190 | } |
| 1191 | } |
| 1192 | #endif |
| 1193 | if (c != ESC) /* use ESC to cancel inserting register */ |
| 1194 | { |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1195 | cmdline_paste(c, i == Ctrl_R, FALSE); |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1196 | |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1197 | #ifdef FEAT_EVAL |
Bram Moolenaar | acf5345 | 2005-12-17 22:06:52 +0000 | [diff] [blame] | 1198 | /* When there was a serious error abort getting the |
| 1199 | * command line. */ |
| 1200 | if (aborting()) |
| 1201 | { |
| 1202 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1203 | putting it in history */ |
| 1204 | goto returncmd; /* back to cmd mode */ |
| 1205 | } |
Bram Moolenaar | a9b1e74 | 2005-12-19 22:14:58 +0000 | [diff] [blame] | 1206 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1207 | KeyTyped = FALSE; /* Don't do p_wc completion. */ |
| 1208 | #ifdef FEAT_EVAL |
| 1209 | if (new_cmdpos >= 0) |
| 1210 | { |
| 1211 | /* set_cmdline_pos() was used */ |
| 1212 | if (new_cmdpos > ccline.cmdlen) |
| 1213 | ccline.cmdpos = ccline.cmdlen; |
| 1214 | else |
| 1215 | ccline.cmdpos = new_cmdpos; |
| 1216 | } |
| 1217 | #endif |
| 1218 | } |
| 1219 | redrawcmd(); |
| 1220 | goto cmdline_changed; |
| 1221 | |
| 1222 | case Ctrl_D: |
| 1223 | if (showmatches(&xpc, FALSE) == EXPAND_NOTHING) |
| 1224 | break; /* Use ^D as normal char instead */ |
| 1225 | |
| 1226 | redrawcmd(); |
| 1227 | continue; /* don't do incremental search now */ |
| 1228 | |
| 1229 | case K_RIGHT: |
| 1230 | case K_S_RIGHT: |
| 1231 | case K_C_RIGHT: |
| 1232 | do |
| 1233 | { |
| 1234 | if (ccline.cmdpos >= ccline.cmdlen) |
| 1235 | break; |
| 1236 | i = cmdline_charsize(ccline.cmdpos); |
| 1237 | if (KeyTyped && ccline.cmdspos + i >= Columns * Rows) |
| 1238 | break; |
| 1239 | ccline.cmdspos += i; |
| 1240 | #ifdef FEAT_MBYTE |
| 1241 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1242 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1243 | + ccline.cmdpos); |
| 1244 | else |
| 1245 | #endif |
| 1246 | ++ccline.cmdpos; |
| 1247 | } |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1248 | while ((c == K_S_RIGHT || c == K_C_RIGHT |
| 1249 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1250 | && ccline.cmdbuff[ccline.cmdpos] != ' '); |
| 1251 | #ifdef FEAT_MBYTE |
| 1252 | if (has_mbyte) |
| 1253 | set_cmdspos_cursor(); |
| 1254 | #endif |
| 1255 | goto cmdline_not_changed; |
| 1256 | |
| 1257 | case K_LEFT: |
| 1258 | case K_S_LEFT: |
| 1259 | case K_C_LEFT: |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1260 | if (ccline.cmdpos == 0) |
| 1261 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1262 | do |
| 1263 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1264 | --ccline.cmdpos; |
| 1265 | #ifdef FEAT_MBYTE |
| 1266 | if (has_mbyte) /* move to first byte of char */ |
| 1267 | ccline.cmdpos -= (*mb_head_off)(ccline.cmdbuff, |
| 1268 | ccline.cmdbuff + ccline.cmdpos); |
| 1269 | #endif |
| 1270 | ccline.cmdspos -= cmdline_charsize(ccline.cmdpos); |
| 1271 | } |
Bram Moolenaar | 49feabd | 2007-12-07 19:28:58 +0000 | [diff] [blame] | 1272 | while (ccline.cmdpos > 0 |
| 1273 | && (c == K_S_LEFT || c == K_C_LEFT |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1274 | || (mod_mask & (MOD_MASK_SHIFT|MOD_MASK_CTRL))) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1275 | && ccline.cmdbuff[ccline.cmdpos - 1] != ' '); |
| 1276 | #ifdef FEAT_MBYTE |
| 1277 | if (has_mbyte) |
| 1278 | set_cmdspos_cursor(); |
| 1279 | #endif |
| 1280 | goto cmdline_not_changed; |
| 1281 | |
| 1282 | case K_IGNORE: |
Bram Moolenaar | 30405d3 | 2008-01-02 20:55:27 +0000 | [diff] [blame] | 1283 | /* Ignore mouse event or ex_window() result. */ |
| 1284 | goto cmdline_not_changed; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1285 | |
Bram Moolenaar | 4770d09 | 2006-01-12 23:22:24 +0000 | [diff] [blame] | 1286 | #ifdef FEAT_GUI_W32 |
| 1287 | /* On Win32 ignore <M-F4>, we get it when closing the window was |
| 1288 | * cancelled. */ |
| 1289 | case K_F4: |
| 1290 | if (mod_mask == MOD_MASK_ALT) |
| 1291 | { |
| 1292 | redrawcmd(); /* somehow the cmdline is cleared */ |
| 1293 | goto cmdline_not_changed; |
| 1294 | } |
| 1295 | break; |
| 1296 | #endif |
| 1297 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1298 | #ifdef FEAT_MOUSE |
| 1299 | case K_MIDDLEDRAG: |
| 1300 | case K_MIDDLERELEASE: |
| 1301 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1302 | |
| 1303 | case K_MIDDLEMOUSE: |
| 1304 | # ifdef FEAT_GUI |
| 1305 | /* When GUI is active, also paste when 'mouse' is empty */ |
| 1306 | if (!gui.in_use) |
| 1307 | # endif |
| 1308 | if (!mouse_has(MOUSE_COMMAND)) |
| 1309 | goto cmdline_not_changed; /* Ignore mouse */ |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1310 | # ifdef FEAT_CLIPBOARD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1311 | if (clip_star.available) |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1312 | cmdline_paste('*', TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1313 | else |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1314 | # endif |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1315 | cmdline_paste(0, TRUE, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1316 | redrawcmd(); |
| 1317 | goto cmdline_changed; |
| 1318 | |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1319 | # ifdef FEAT_DND |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1320 | case K_DROP: |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 1321 | cmdline_paste('~', TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1322 | redrawcmd(); |
| 1323 | goto cmdline_changed; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1324 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1325 | |
| 1326 | case K_LEFTDRAG: |
| 1327 | case K_LEFTRELEASE: |
| 1328 | case K_RIGHTDRAG: |
| 1329 | case K_RIGHTRELEASE: |
| 1330 | /* Ignore drag and release events when the button-down wasn't |
| 1331 | * seen before. */ |
| 1332 | if (ignore_drag_release) |
| 1333 | goto cmdline_not_changed; |
| 1334 | /* FALLTHROUGH */ |
| 1335 | case K_LEFTMOUSE: |
| 1336 | case K_RIGHTMOUSE: |
| 1337 | if (c == K_LEFTRELEASE || c == K_RIGHTRELEASE) |
| 1338 | ignore_drag_release = TRUE; |
| 1339 | else |
| 1340 | ignore_drag_release = FALSE; |
| 1341 | # ifdef FEAT_GUI |
| 1342 | /* When GUI is active, also move when 'mouse' is empty */ |
| 1343 | if (!gui.in_use) |
| 1344 | # endif |
| 1345 | if (!mouse_has(MOUSE_COMMAND)) |
| 1346 | goto cmdline_not_changed; /* Ignore mouse */ |
| 1347 | # ifdef FEAT_CLIPBOARD |
| 1348 | if (mouse_row < cmdline_row && clip_star.available) |
| 1349 | { |
| 1350 | int button, is_click, is_drag; |
| 1351 | |
| 1352 | /* |
| 1353 | * Handle modeless selection. |
| 1354 | */ |
| 1355 | button = get_mouse_button(KEY2TERMCAP1(c), |
| 1356 | &is_click, &is_drag); |
| 1357 | if (mouse_model_popup() && button == MOUSE_LEFT |
| 1358 | && (mod_mask & MOD_MASK_SHIFT)) |
| 1359 | { |
| 1360 | /* Translate shift-left to right button. */ |
| 1361 | button = MOUSE_RIGHT; |
| 1362 | mod_mask &= ~MOD_MASK_SHIFT; |
| 1363 | } |
| 1364 | clip_modeless(button, is_click, is_drag); |
| 1365 | goto cmdline_not_changed; |
| 1366 | } |
| 1367 | # endif |
| 1368 | |
| 1369 | set_cmdspos(); |
| 1370 | for (ccline.cmdpos = 0; ccline.cmdpos < ccline.cmdlen; |
| 1371 | ++ccline.cmdpos) |
| 1372 | { |
| 1373 | i = cmdline_charsize(ccline.cmdpos); |
| 1374 | if (mouse_row <= cmdline_row + ccline.cmdspos / Columns |
| 1375 | && mouse_col < ccline.cmdspos % Columns + i) |
| 1376 | break; |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1377 | # ifdef FEAT_MBYTE |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1378 | if (has_mbyte) |
| 1379 | { |
| 1380 | /* Count ">" for double-wide char that doesn't fit. */ |
| 1381 | correct_cmdspos(ccline.cmdpos, i); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 1382 | ccline.cmdpos += (*mb_ptr2len)(ccline.cmdbuff |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1383 | + ccline.cmdpos) - 1; |
| 1384 | } |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1385 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1386 | ccline.cmdspos += i; |
| 1387 | } |
| 1388 | goto cmdline_not_changed; |
| 1389 | |
| 1390 | /* Mouse scroll wheel: ignored here */ |
| 1391 | case K_MOUSEDOWN: |
| 1392 | case K_MOUSEUP: |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1393 | case K_MOUSELEFT: |
| 1394 | case K_MOUSERIGHT: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1395 | /* Alternate buttons ignored here */ |
| 1396 | case K_X1MOUSE: |
| 1397 | case K_X1DRAG: |
| 1398 | case K_X1RELEASE: |
| 1399 | case K_X2MOUSE: |
| 1400 | case K_X2DRAG: |
| 1401 | case K_X2RELEASE: |
| 1402 | goto cmdline_not_changed; |
| 1403 | |
| 1404 | #endif /* FEAT_MOUSE */ |
| 1405 | |
| 1406 | #ifdef FEAT_GUI |
| 1407 | case K_LEFTMOUSE_NM: /* mousefocus click, ignored */ |
| 1408 | case K_LEFTRELEASE_NM: |
| 1409 | goto cmdline_not_changed; |
| 1410 | |
| 1411 | case K_VER_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1412 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1413 | { |
| 1414 | gui_do_scroll(); |
| 1415 | redrawcmd(); |
| 1416 | } |
| 1417 | goto cmdline_not_changed; |
| 1418 | |
| 1419 | case K_HOR_SCROLLBAR: |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 1420 | if (msg_scrolled == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1421 | { |
Bram Moolenaar | 8d9b40e | 2010-07-25 15:49:07 +0200 | [diff] [blame] | 1422 | gui_do_horiz_scroll(scrollbar_value, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1423 | redrawcmd(); |
| 1424 | } |
| 1425 | goto cmdline_not_changed; |
| 1426 | #endif |
Bram Moolenaar | a23ccb8 | 2006-02-27 00:08:02 +0000 | [diff] [blame] | 1427 | #ifdef FEAT_GUI_TABLINE |
| 1428 | case K_TABLINE: |
| 1429 | case K_TABMENU: |
| 1430 | /* Don't want to change any tabs here. Make sure the same tab |
| 1431 | * is still selected. */ |
| 1432 | if (gui_use_tabline()) |
| 1433 | gui_mch_set_curtab(tabpage_index(curtab)); |
| 1434 | goto cmdline_not_changed; |
| 1435 | #endif |
| 1436 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1437 | case K_SELECT: /* end of Select mode mapping - ignore */ |
| 1438 | goto cmdline_not_changed; |
| 1439 | |
| 1440 | case Ctrl_B: /* begin of command line */ |
| 1441 | case K_HOME: |
| 1442 | case K_KHOME: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1443 | case K_S_HOME: |
| 1444 | case K_C_HOME: |
| 1445 | ccline.cmdpos = 0; |
| 1446 | set_cmdspos(); |
| 1447 | goto cmdline_not_changed; |
| 1448 | |
| 1449 | case Ctrl_E: /* end of command line */ |
| 1450 | case K_END: |
| 1451 | case K_KEND: |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1452 | case K_S_END: |
| 1453 | case K_C_END: |
| 1454 | ccline.cmdpos = ccline.cmdlen; |
| 1455 | set_cmdspos_cursor(); |
| 1456 | goto cmdline_not_changed; |
| 1457 | |
| 1458 | case Ctrl_A: /* all matches */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1459 | if (nextwild(&xpc, WILD_ALL, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1460 | break; |
| 1461 | goto cmdline_changed; |
| 1462 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1463 | case Ctrl_L: |
| 1464 | #ifdef FEAT_SEARCH_EXTRA |
| 1465 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1466 | { |
| 1467 | /* Add a character from under the cursor for 'incsearch' */ |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1468 | if (did_incsearch) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1469 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1470 | curwin->w_cursor = match_end; |
| 1471 | if (!equalpos(curwin->w_cursor, old_cursor)) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1472 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1473 | c = gchar_cursor(); |
| 1474 | /* If 'ignorecase' and 'smartcase' are set and the |
| 1475 | * command line has no uppercase characters, convert |
| 1476 | * the character to lowercase */ |
| 1477 | if (p_ic && p_scs |
| 1478 | && !pat_has_uppercase(ccline.cmdbuff)) |
| 1479 | c = MB_TOLOWER(c); |
| 1480 | if (c != NUL) |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1481 | { |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1482 | if (c == firstc || vim_strchr((char_u *)( |
| 1483 | p_magic ? "\\^$.*[" : "\\^$"), c) |
| 1484 | != NULL) |
| 1485 | { |
| 1486 | /* put a backslash before special |
| 1487 | * characters */ |
| 1488 | stuffcharReadbuff(c); |
| 1489 | c = '\\'; |
| 1490 | } |
| 1491 | break; |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1492 | } |
Bram Moolenaar | 93db975 | 2006-11-21 10:29:45 +0000 | [diff] [blame] | 1493 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1494 | } |
| 1495 | goto cmdline_not_changed; |
| 1496 | } |
| 1497 | #endif |
| 1498 | |
| 1499 | /* completion: longest common part */ |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1500 | if (nextwild(&xpc, WILD_LONGEST, 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1501 | break; |
| 1502 | goto cmdline_changed; |
| 1503 | |
| 1504 | case Ctrl_N: /* next match */ |
| 1505 | case Ctrl_P: /* previous match */ |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1506 | #ifdef FEAT_SEARCH_EXTRA |
| 1507 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1508 | { |
| 1509 | pos_T t; |
| 1510 | int search_flags = SEARCH_KEEP + SEARCH_NOOF |
| 1511 | + SEARCH_PEEK; |
| 1512 | |
| 1513 | if (char_avail()) |
| 1514 | continue; |
| 1515 | cursor_off(); |
| 1516 | out_flush(); |
| 1517 | if (c == Ctrl_N) |
| 1518 | { |
| 1519 | t = match_end; |
| 1520 | search_flags += SEARCH_COL; |
| 1521 | } |
| 1522 | else |
| 1523 | t = match_start; |
| 1524 | ++emsg_off; |
| 1525 | i = searchit(curwin, curbuf, &t, |
| 1526 | c == Ctrl_N ? FORWARD : BACKWARD, |
| 1527 | ccline.cmdbuff, count, search_flags, |
| 1528 | RE_SEARCH, 0, NULL); |
| 1529 | --emsg_off; |
| 1530 | if (i) |
| 1531 | { |
| 1532 | old_cursor = match_start; |
| 1533 | match_end = t; |
| 1534 | match_start = t; |
| 1535 | if (c == Ctrl_P && firstc == '/') |
| 1536 | { |
| 1537 | /* move just before the current match, so that |
| 1538 | * when nv_search finishes the cursor will be |
| 1539 | * put back on the match */ |
| 1540 | old_cursor = t; |
| 1541 | (void)decl(&old_cursor); |
| 1542 | } |
| 1543 | if (lt(t, old_cursor) && c == Ctrl_N) |
| 1544 | { |
| 1545 | /* wrap around */ |
| 1546 | old_cursor = t; |
| 1547 | if (firstc == '?') |
| 1548 | (void)incl(&old_cursor); |
| 1549 | else |
| 1550 | (void)decl(&old_cursor); |
| 1551 | } |
| 1552 | |
| 1553 | set_search_match(&match_end); |
| 1554 | curwin->w_cursor = match_start; |
| 1555 | changed_cline_bef_curs(); |
| 1556 | update_topline(); |
| 1557 | validate_cursor(); |
| 1558 | highlight_match = TRUE; |
| 1559 | old_curswant = curwin->w_curswant; |
| 1560 | old_leftcol = curwin->w_leftcol; |
| 1561 | old_topline = curwin->w_topline; |
| 1562 | # ifdef FEAT_DIFF |
| 1563 | old_topfill = curwin->w_topfill; |
| 1564 | # endif |
| 1565 | old_botline = curwin->w_botline; |
| 1566 | update_screen(NOT_VALID); |
| 1567 | redrawcmdline(); |
| 1568 | } |
| 1569 | else |
| 1570 | vim_beep(BO_ERROR); |
| 1571 | goto cmdline_not_changed; |
| 1572 | } |
| 1573 | #endif |
| 1574 | else if (xpc.xp_numfiles > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1575 | { |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 1576 | if (nextwild(&xpc, (c == Ctrl_P) ? WILD_PREV : WILD_NEXT, |
| 1577 | 0, firstc != '@') == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1578 | break; |
| 1579 | goto cmdline_changed; |
| 1580 | } |
| 1581 | |
| 1582 | #ifdef FEAT_CMDHIST |
| 1583 | case K_UP: |
| 1584 | case K_DOWN: |
| 1585 | case K_S_UP: |
| 1586 | case K_S_DOWN: |
| 1587 | case K_PAGEUP: |
| 1588 | case K_KPAGEUP: |
| 1589 | case K_PAGEDOWN: |
| 1590 | case K_KPAGEDOWN: |
| 1591 | if (hislen == 0 || firstc == NUL) /* no history */ |
| 1592 | goto cmdline_not_changed; |
| 1593 | |
| 1594 | i = hiscnt; |
| 1595 | |
| 1596 | /* save current command string so it can be restored later */ |
| 1597 | if (lookfor == NULL) |
| 1598 | { |
| 1599 | if ((lookfor = vim_strsave(ccline.cmdbuff)) == NULL) |
| 1600 | goto cmdline_not_changed; |
| 1601 | lookfor[ccline.cmdpos] = NUL; |
| 1602 | } |
| 1603 | |
| 1604 | j = (int)STRLEN(lookfor); |
| 1605 | for (;;) |
| 1606 | { |
| 1607 | /* one step backwards */ |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1608 | if (c == K_UP|| c == K_S_UP || c == Ctrl_P |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1609 | || c == K_PAGEUP || c == K_KPAGEUP) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1610 | { |
| 1611 | if (hiscnt == hislen) /* first time */ |
| 1612 | hiscnt = hisidx[histype]; |
| 1613 | else if (hiscnt == 0 && hisidx[histype] != hislen - 1) |
| 1614 | hiscnt = hislen - 1; |
| 1615 | else if (hiscnt != hisidx[histype] + 1) |
| 1616 | --hiscnt; |
| 1617 | else /* at top of list */ |
| 1618 | { |
| 1619 | hiscnt = i; |
| 1620 | break; |
| 1621 | } |
| 1622 | } |
| 1623 | else /* one step forwards */ |
| 1624 | { |
| 1625 | /* on last entry, clear the line */ |
| 1626 | if (hiscnt == hisidx[histype]) |
| 1627 | { |
| 1628 | hiscnt = hislen; |
| 1629 | break; |
| 1630 | } |
| 1631 | |
| 1632 | /* not on a history line, nothing to do */ |
| 1633 | if (hiscnt == hislen) |
| 1634 | break; |
| 1635 | if (hiscnt == hislen - 1) /* wrap around */ |
| 1636 | hiscnt = 0; |
| 1637 | else |
| 1638 | ++hiscnt; |
| 1639 | } |
| 1640 | if (hiscnt < 0 || history[histype][hiscnt].hisstr == NULL) |
| 1641 | { |
| 1642 | hiscnt = i; |
| 1643 | break; |
| 1644 | } |
Bram Moolenaar | 68b76a6 | 2005-03-25 21:53:48 +0000 | [diff] [blame] | 1645 | if ((c != K_UP && c != K_DOWN) |
Bram Moolenaar | bc7aa85 | 2005-03-06 23:38:09 +0000 | [diff] [blame] | 1646 | || hiscnt == i |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1647 | || STRNCMP(history[histype][hiscnt].hisstr, |
| 1648 | lookfor, (size_t)j) == 0) |
| 1649 | break; |
| 1650 | } |
| 1651 | |
| 1652 | if (hiscnt != i) /* jumped to other entry */ |
| 1653 | { |
| 1654 | char_u *p; |
| 1655 | int len; |
| 1656 | int old_firstc; |
| 1657 | |
| 1658 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1659 | xpc.xp_context = EXPAND_NOTHING; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1660 | if (hiscnt == hislen) |
| 1661 | p = lookfor; /* back to the old one */ |
| 1662 | else |
| 1663 | p = history[histype][hiscnt].hisstr; |
| 1664 | |
| 1665 | if (histype == HIST_SEARCH |
| 1666 | && p != lookfor |
| 1667 | && (old_firstc = p[STRLEN(p) + 1]) != firstc) |
| 1668 | { |
| 1669 | /* Correct for the separator character used when |
| 1670 | * adding the history entry vs the one used now. |
| 1671 | * First loop: count length. |
| 1672 | * Second loop: copy the characters. */ |
| 1673 | for (i = 0; i <= 1; ++i) |
| 1674 | { |
| 1675 | len = 0; |
| 1676 | for (j = 0; p[j] != NUL; ++j) |
| 1677 | { |
| 1678 | /* Replace old sep with new sep, unless it is |
| 1679 | * escaped. */ |
| 1680 | if (p[j] == old_firstc |
| 1681 | && (j == 0 || p[j - 1] != '\\')) |
| 1682 | { |
| 1683 | if (i > 0) |
| 1684 | ccline.cmdbuff[len] = firstc; |
| 1685 | } |
| 1686 | else |
| 1687 | { |
| 1688 | /* Escape new sep, unless it is already |
| 1689 | * escaped. */ |
| 1690 | if (p[j] == firstc |
| 1691 | && (j == 0 || p[j - 1] != '\\')) |
| 1692 | { |
| 1693 | if (i > 0) |
| 1694 | ccline.cmdbuff[len] = '\\'; |
| 1695 | ++len; |
| 1696 | } |
| 1697 | if (i > 0) |
| 1698 | ccline.cmdbuff[len] = p[j]; |
| 1699 | } |
| 1700 | ++len; |
| 1701 | } |
| 1702 | if (i == 0) |
| 1703 | { |
| 1704 | alloc_cmdbuff(len); |
| 1705 | if (ccline.cmdbuff == NULL) |
| 1706 | goto returncmd; |
| 1707 | } |
| 1708 | } |
| 1709 | ccline.cmdbuff[len] = NUL; |
| 1710 | } |
| 1711 | else |
| 1712 | { |
| 1713 | alloc_cmdbuff((int)STRLEN(p)); |
| 1714 | if (ccline.cmdbuff == NULL) |
| 1715 | goto returncmd; |
| 1716 | STRCPY(ccline.cmdbuff, p); |
| 1717 | } |
| 1718 | |
| 1719 | ccline.cmdpos = ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 1720 | redrawcmd(); |
| 1721 | goto cmdline_changed; |
| 1722 | } |
| 1723 | beep_flush(); |
| 1724 | goto cmdline_not_changed; |
| 1725 | #endif |
| 1726 | |
| 1727 | case Ctrl_V: |
| 1728 | case Ctrl_Q: |
| 1729 | #ifdef FEAT_MOUSE |
| 1730 | ignore_drag_release = TRUE; |
| 1731 | #endif |
| 1732 | putcmdline('^', TRUE); |
| 1733 | c = get_literal(); /* get next (two) character(s) */ |
| 1734 | do_abbr = FALSE; /* don't do abbreviation now */ |
| 1735 | #ifdef FEAT_MBYTE |
| 1736 | /* may need to remove ^ when composing char was typed */ |
| 1737 | if (enc_utf8 && utf_iscomposing(c) && !cmd_silent) |
| 1738 | { |
| 1739 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 1740 | msg_putchar(' '); |
| 1741 | cursorcmd(); |
| 1742 | } |
| 1743 | #endif |
| 1744 | break; |
| 1745 | |
| 1746 | #ifdef FEAT_DIGRAPHS |
| 1747 | case Ctrl_K: |
| 1748 | #ifdef FEAT_MOUSE |
| 1749 | ignore_drag_release = TRUE; |
| 1750 | #endif |
| 1751 | putcmdline('?', TRUE); |
| 1752 | #ifdef USE_ON_FLY_SCROLL |
| 1753 | dont_scroll = TRUE; /* disallow scrolling here */ |
| 1754 | #endif |
| 1755 | c = get_digraph(TRUE); |
| 1756 | if (c != NUL) |
| 1757 | break; |
| 1758 | |
| 1759 | redrawcmd(); |
| 1760 | goto cmdline_not_changed; |
| 1761 | #endif /* FEAT_DIGRAPHS */ |
| 1762 | |
| 1763 | #ifdef FEAT_RIGHTLEFT |
| 1764 | case Ctrl__: /* CTRL-_: switch language mode */ |
| 1765 | if (!p_ari) |
| 1766 | break; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1767 | # ifdef FEAT_FKMAP |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1768 | if (p_altkeymap) |
| 1769 | { |
| 1770 | cmd_fkmap = !cmd_fkmap; |
| 1771 | if (cmd_fkmap) /* in Farsi always in Insert mode */ |
| 1772 | ccline.overstrike = FALSE; |
| 1773 | } |
| 1774 | else /* Hebrew is default */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 1775 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1776 | cmd_hkmap = !cmd_hkmap; |
| 1777 | goto cmdline_not_changed; |
| 1778 | #endif |
| 1779 | |
| 1780 | default: |
| 1781 | #ifdef UNIX |
| 1782 | if (c == intr_char) |
| 1783 | { |
| 1784 | gotesc = TRUE; /* will free ccline.cmdbuff after |
| 1785 | putting it in history */ |
| 1786 | goto returncmd; /* back to Normal mode */ |
| 1787 | } |
| 1788 | #endif |
| 1789 | /* |
| 1790 | * Normal character with no special meaning. Just set mod_mask |
| 1791 | * to 0x0 so that typing Shift-Space in the GUI doesn't enter |
| 1792 | * the string <S-Space>. This should only happen after ^V. |
| 1793 | */ |
| 1794 | if (!IS_SPECIAL(c)) |
| 1795 | mod_mask = 0x0; |
| 1796 | break; |
| 1797 | } |
| 1798 | /* |
| 1799 | * End of switch on command line character. |
| 1800 | * We come here if we have a normal character. |
| 1801 | */ |
| 1802 | |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1803 | if (do_abbr && (IS_SPECIAL(c) || !vim_iswordc(c)) && (ccheck_abbr( |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1804 | #ifdef FEAT_MBYTE |
| 1805 | /* Add ABBR_OFF for characters above 0x100, this is |
| 1806 | * what check_abbr() expects. */ |
| 1807 | (has_mbyte && c >= 0x100) ? (c + ABBR_OFF) : |
| 1808 | #endif |
Bram Moolenaar | ede3e63 | 2013-06-23 16:16:19 +0200 | [diff] [blame] | 1809 | c) || c == Ctrl_RSB)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1810 | goto cmdline_changed; |
| 1811 | |
| 1812 | /* |
| 1813 | * put the character in the command line |
| 1814 | */ |
| 1815 | if (IS_SPECIAL(c) || mod_mask != 0) |
| 1816 | put_on_cmdline(get_special_key_name(c, mod_mask), -1, TRUE); |
| 1817 | else |
| 1818 | { |
| 1819 | #ifdef FEAT_MBYTE |
| 1820 | if (has_mbyte) |
| 1821 | { |
| 1822 | j = (*mb_char2bytes)(c, IObuff); |
| 1823 | IObuff[j] = NUL; /* exclude composing chars */ |
| 1824 | put_on_cmdline(IObuff, j, TRUE); |
| 1825 | } |
| 1826 | else |
| 1827 | #endif |
| 1828 | { |
| 1829 | IObuff[0] = c; |
| 1830 | put_on_cmdline(IObuff, 1, TRUE); |
| 1831 | } |
| 1832 | } |
| 1833 | goto cmdline_changed; |
| 1834 | |
| 1835 | /* |
| 1836 | * This part implements incremental searches for "/" and "?" |
| 1837 | * Jump to cmdline_not_changed when a character has been read but the command |
| 1838 | * line did not change. Then we only search and redraw if something changed in |
| 1839 | * the past. |
| 1840 | * Jump to cmdline_changed when the command line did change. |
| 1841 | * (Sorry for the goto's, I know it is ugly). |
| 1842 | */ |
| 1843 | cmdline_not_changed: |
| 1844 | #ifdef FEAT_SEARCH_EXTRA |
| 1845 | if (!incsearch_postponed) |
| 1846 | continue; |
| 1847 | #endif |
| 1848 | |
| 1849 | cmdline_changed: |
| 1850 | #ifdef FEAT_SEARCH_EXTRA |
| 1851 | /* |
| 1852 | * 'incsearch' highlighting. |
| 1853 | */ |
| 1854 | if (p_is && !cmd_silent && (firstc == '/' || firstc == '?')) |
| 1855 | { |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1856 | pos_T end_pos; |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1857 | #ifdef FEAT_RELTIME |
| 1858 | proftime_T tm; |
| 1859 | #endif |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1860 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1861 | /* if there is a character waiting, search and redraw later */ |
| 1862 | if (char_avail()) |
| 1863 | { |
| 1864 | incsearch_postponed = TRUE; |
| 1865 | continue; |
| 1866 | } |
| 1867 | incsearch_postponed = FALSE; |
| 1868 | curwin->w_cursor = old_cursor; /* start at old position */ |
| 1869 | |
| 1870 | /* If there is no command line, don't do anything */ |
| 1871 | if (ccline.cmdlen == 0) |
| 1872 | i = 0; |
| 1873 | else |
| 1874 | { |
| 1875 | cursor_off(); /* so the user knows we're busy */ |
| 1876 | out_flush(); |
| 1877 | ++emsg_off; /* So it doesn't beep if bad expr */ |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1878 | #ifdef FEAT_RELTIME |
| 1879 | /* Set the time limit to half a second. */ |
| 1880 | profile_setlimit(500L, &tm); |
| 1881 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1882 | i = do_search(NULL, firstc, ccline.cmdbuff, count, |
Bram Moolenaar | 91a4e82 | 2008-01-19 14:59:58 +0000 | [diff] [blame] | 1883 | SEARCH_KEEP + SEARCH_OPT + SEARCH_NOOF + SEARCH_PEEK, |
| 1884 | #ifdef FEAT_RELTIME |
| 1885 | &tm |
| 1886 | #else |
| 1887 | NULL |
| 1888 | #endif |
| 1889 | ); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1890 | --emsg_off; |
| 1891 | /* if interrupted while searching, behave like it failed */ |
| 1892 | if (got_int) |
| 1893 | { |
| 1894 | (void)vpeekc(); /* remove <C-C> from input stream */ |
| 1895 | got_int = FALSE; /* don't abandon the command line */ |
| 1896 | i = 0; |
| 1897 | } |
| 1898 | else if (char_avail()) |
| 1899 | /* cancelled searching because a char was typed */ |
| 1900 | incsearch_postponed = TRUE; |
| 1901 | } |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1902 | if (i != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1903 | highlight_match = TRUE; /* highlight position */ |
| 1904 | else |
| 1905 | highlight_match = FALSE; /* remove highlight */ |
| 1906 | |
| 1907 | /* first restore the old curwin values, so the screen is |
| 1908 | * positioned in the same way as the actual search command */ |
| 1909 | curwin->w_leftcol = old_leftcol; |
| 1910 | curwin->w_topline = old_topline; |
| 1911 | # ifdef FEAT_DIFF |
| 1912 | curwin->w_topfill = old_topfill; |
| 1913 | # endif |
| 1914 | curwin->w_botline = old_botline; |
| 1915 | changed_cline_bef_curs(); |
| 1916 | update_topline(); |
| 1917 | |
| 1918 | if (i != 0) |
| 1919 | { |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1920 | pos_T save_pos = curwin->w_cursor; |
| 1921 | |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1922 | match_start = curwin->w_cursor; |
| 1923 | set_search_match(&curwin->w_cursor); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1924 | validate_cursor(); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1925 | end_pos = curwin->w_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1926 | match_end = end_pos; |
Bram Moolenaar | ae5bce1 | 2005-08-15 21:41:48 +0000 | [diff] [blame] | 1927 | curwin->w_cursor = save_pos; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1928 | } |
Bram Moolenaar | db552d60 | 2006-03-23 22:59:57 +0000 | [diff] [blame] | 1929 | else |
| 1930 | end_pos = curwin->w_cursor; /* shutup gcc 4 */ |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1931 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1932 | validate_cursor(); |
Bram Moolenaar | 27a2319 | 2006-09-14 09:27:26 +0000 | [diff] [blame] | 1933 | # ifdef FEAT_WINDOWS |
| 1934 | /* May redraw the status line to show the cursor position. */ |
| 1935 | if (p_ru && curwin->w_status_height > 0) |
| 1936 | curwin->w_redr_status = TRUE; |
| 1937 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1938 | |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1939 | save_cmdline(&save_ccline); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 1940 | update_screen(SOME_VALID); |
Bram Moolenaar | 111ff9f | 2005-03-08 22:40:03 +0000 | [diff] [blame] | 1941 | restore_cmdline(&save_ccline); |
| 1942 | |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 1943 | /* Leave it at the end to make CTRL-R CTRL-W work. */ |
| 1944 | if (i != 0) |
| 1945 | curwin->w_cursor = end_pos; |
| 1946 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1947 | msg_starthere(); |
| 1948 | redrawcmdline(); |
| 1949 | did_incsearch = TRUE; |
| 1950 | } |
| 1951 | #else /* FEAT_SEARCH_EXTRA */ |
| 1952 | ; |
| 1953 | #endif |
| 1954 | |
| 1955 | #ifdef FEAT_RIGHTLEFT |
| 1956 | if (cmdmsg_rl |
| 1957 | # ifdef FEAT_ARABIC |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 1958 | || (p_arshape && !p_tbidi && enc_utf8) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1959 | # endif |
| 1960 | ) |
| 1961 | /* Always redraw the whole command line to fix shaping and |
Bram Moolenaar | 58437e0 | 2012-02-22 17:58:04 +0100 | [diff] [blame] | 1962 | * right-left typing. Not efficient, but it works. |
| 1963 | * Do it only when there are no characters left to read |
| 1964 | * to avoid useless intermediate redraws. */ |
| 1965 | if (vpeekc() == NUL) |
| 1966 | redrawcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1967 | #endif |
| 1968 | } |
| 1969 | |
| 1970 | returncmd: |
| 1971 | |
| 1972 | #ifdef FEAT_RIGHTLEFT |
| 1973 | cmdmsg_rl = FALSE; |
| 1974 | #endif |
| 1975 | |
| 1976 | #ifdef FEAT_FKMAP |
| 1977 | cmd_fkmap = 0; |
| 1978 | #endif |
| 1979 | |
| 1980 | ExpandCleanup(&xpc); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 1981 | ccline.xpc = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1982 | |
| 1983 | #ifdef FEAT_SEARCH_EXTRA |
| 1984 | if (did_incsearch) |
| 1985 | { |
| 1986 | curwin->w_cursor = old_cursor; |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 1987 | if (gotesc) |
| 1988 | curwin->w_cursor = cursor_start; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1989 | curwin->w_curswant = old_curswant; |
| 1990 | curwin->w_leftcol = old_leftcol; |
| 1991 | curwin->w_topline = old_topline; |
| 1992 | # ifdef FEAT_DIFF |
| 1993 | curwin->w_topfill = old_topfill; |
| 1994 | # endif |
| 1995 | curwin->w_botline = old_botline; |
| 1996 | highlight_match = FALSE; |
| 1997 | validate_cursor(); /* needed for TAB */ |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 1998 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 1999 | } |
| 2000 | #endif |
| 2001 | |
| 2002 | if (ccline.cmdbuff != NULL) |
| 2003 | { |
| 2004 | /* |
| 2005 | * Put line in history buffer (":" and "=" only when it was typed). |
| 2006 | */ |
| 2007 | #ifdef FEAT_CMDHIST |
| 2008 | if (ccline.cmdlen && firstc != NUL |
| 2009 | && (some_key_typed || histype == HIST_SEARCH)) |
| 2010 | { |
| 2011 | add_to_history(histype, ccline.cmdbuff, TRUE, |
| 2012 | histype == HIST_SEARCH ? firstc : NUL); |
| 2013 | if (firstc == ':') |
| 2014 | { |
| 2015 | vim_free(new_last_cmdline); |
| 2016 | new_last_cmdline = vim_strsave(ccline.cmdbuff); |
| 2017 | } |
| 2018 | } |
| 2019 | #endif |
| 2020 | |
| 2021 | if (gotesc) /* abandon command line */ |
| 2022 | { |
| 2023 | vim_free(ccline.cmdbuff); |
| 2024 | ccline.cmdbuff = NULL; |
| 2025 | if (msg_scrolled == 0) |
| 2026 | compute_cmdrow(); |
| 2027 | MSG(""); |
| 2028 | redraw_cmdline = TRUE; |
| 2029 | } |
| 2030 | } |
| 2031 | |
| 2032 | /* |
| 2033 | * If the screen was shifted up, redraw the whole screen (later). |
| 2034 | * If the line is too long, clear it, so ruler and shown command do |
| 2035 | * not get printed in the middle of it. |
| 2036 | */ |
| 2037 | msg_check(); |
| 2038 | msg_scroll = save_msg_scroll; |
| 2039 | redir_off = FALSE; |
| 2040 | |
| 2041 | /* When the command line was typed, no need for a wait-return prompt. */ |
| 2042 | if (some_key_typed) |
| 2043 | need_wait_return = FALSE; |
| 2044 | |
| 2045 | State = save_State; |
| 2046 | #ifdef USE_IM_CONTROL |
| 2047 | if (b_im_ptr != NULL && *b_im_ptr != B_IMODE_LMAP) |
| 2048 | im_save_status(b_im_ptr); |
| 2049 | im_set_active(FALSE); |
| 2050 | #endif |
| 2051 | #ifdef FEAT_MOUSE |
| 2052 | setmouse(); |
| 2053 | #endif |
| 2054 | #ifdef CURSOR_SHAPE |
| 2055 | ui_cursor_shape(); /* may show different cursor shape */ |
| 2056 | #endif |
| 2057 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2058 | { |
| 2059 | char_u *p = ccline.cmdbuff; |
| 2060 | |
| 2061 | /* Make ccline empty, getcmdline() may try to use it. */ |
| 2062 | ccline.cmdbuff = NULL; |
| 2063 | return p; |
| 2064 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2065 | } |
| 2066 | |
| 2067 | #if (defined(FEAT_CRYPT) || defined(FEAT_EVAL)) || defined(PROTO) |
| 2068 | /* |
| 2069 | * Get a command line with a prompt. |
| 2070 | * This is prepared to be called recursively from getcmdline() (e.g. by |
| 2071 | * f_input() when evaluating an expression from CTRL-R =). |
| 2072 | * Returns the command line in allocated memory, or NULL. |
| 2073 | */ |
| 2074 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2075 | getcmdline_prompt( |
| 2076 | int firstc, |
| 2077 | char_u *prompt, /* command line prompt */ |
| 2078 | int attr, /* attributes for prompt */ |
| 2079 | int xp_context, /* type of expansion */ |
| 2080 | char_u *xp_arg) /* user-defined expansion argument */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2081 | { |
| 2082 | char_u *s; |
| 2083 | struct cmdline_info save_ccline; |
| 2084 | int msg_col_save = msg_col; |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2085 | int msg_silent_save = msg_silent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2086 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2087 | save_cmdline(&save_ccline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2088 | ccline.cmdprompt = prompt; |
| 2089 | ccline.cmdattr = attr; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2090 | # ifdef FEAT_EVAL |
| 2091 | ccline.xp_context = xp_context; |
| 2092 | ccline.xp_arg = xp_arg; |
| 2093 | ccline.input_fn = (firstc == '@'); |
| 2094 | # endif |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2095 | msg_silent = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2096 | s = getcmdline(firstc, 1L, 0); |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 2097 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 6135d0d | 2016-03-22 20:31:13 +0100 | [diff] [blame] | 2098 | msg_silent = msg_silent_save; |
Bram Moolenaar | 1db1f77 | 2011-08-17 16:25:48 +0200 | [diff] [blame] | 2099 | /* Restore msg_col, the prompt from input() may have changed it. |
| 2100 | * But only if called recursively and the commandline is therefore being |
| 2101 | * restored to an old one; if not, the input() prompt stays on the screen, |
| 2102 | * so we need its modified msg_col left intact. */ |
| 2103 | if (ccline.cmdbuff != NULL) |
| 2104 | msg_col = msg_col_save; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2105 | |
| 2106 | return s; |
| 2107 | } |
| 2108 | #endif |
| 2109 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2110 | /* |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2111 | * Return TRUE when the text must not be changed and we can't switch to |
| 2112 | * another window or buffer. Used when editing the command line, evaluating |
| 2113 | * 'balloonexpr', etc. |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2114 | */ |
| 2115 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2116 | text_locked(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2117 | { |
| 2118 | #ifdef FEAT_CMDWIN |
| 2119 | if (cmdwin_type != 0) |
| 2120 | return TRUE; |
| 2121 | #endif |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 2122 | return textlock != 0; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2123 | } |
| 2124 | |
| 2125 | /* |
| 2126 | * Give an error message for a command that isn't allowed while the cmdline |
| 2127 | * window is open or editing the cmdline in another way. |
| 2128 | */ |
| 2129 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2130 | text_locked_msg(void) |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 2131 | { |
| 2132 | #ifdef FEAT_CMDWIN |
| 2133 | if (cmdwin_type != 0) |
| 2134 | EMSG(_(e_cmdwin)); |
| 2135 | else |
| 2136 | #endif |
| 2137 | EMSG(_(e_secure)); |
| 2138 | } |
| 2139 | |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2140 | #if defined(FEAT_AUTOCMD) || defined(PROTO) |
| 2141 | /* |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2142 | * Check if "curbuf_lock" or "allbuf_lock" is set and return TRUE when it is |
| 2143 | * and give an error message. |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2144 | */ |
| 2145 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2146 | curbuf_locked(void) |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2147 | { |
| 2148 | if (curbuf_lock > 0) |
| 2149 | { |
| 2150 | EMSG(_("E788: Not allowed to edit another buffer now")); |
| 2151 | return TRUE; |
| 2152 | } |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2153 | return allbuf_locked(); |
| 2154 | } |
| 2155 | |
| 2156 | /* |
| 2157 | * Check if "allbuf_lock" is set and return TRUE when it is and give an error |
| 2158 | * message. |
| 2159 | */ |
| 2160 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2161 | allbuf_locked(void) |
Bram Moolenaar | bf1b7a7 | 2009-03-05 02:15:53 +0000 | [diff] [blame] | 2162 | { |
| 2163 | if (allbuf_lock > 0) |
| 2164 | { |
| 2165 | EMSG(_("E811: Not allowed to change buffer information now")); |
| 2166 | return TRUE; |
| 2167 | } |
Bram Moolenaar | 910f66f | 2006-04-05 20:41:53 +0000 | [diff] [blame] | 2168 | return FALSE; |
| 2169 | } |
| 2170 | #endif |
| 2171 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2172 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2173 | cmdline_charsize(int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2174 | { |
| 2175 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2176 | if (cmdline_star > 0) /* showing '*', always 1 position */ |
| 2177 | return 1; |
| 2178 | #endif |
| 2179 | return ptr2cells(ccline.cmdbuff + idx); |
| 2180 | } |
| 2181 | |
| 2182 | /* |
| 2183 | * Compute the offset of the cursor on the command line for the prompt and |
| 2184 | * indent. |
| 2185 | */ |
| 2186 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2187 | set_cmdspos(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2188 | { |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2189 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2190 | ccline.cmdspos = 1 + ccline.cmdindent; |
| 2191 | else |
| 2192 | ccline.cmdspos = 0 + ccline.cmdindent; |
| 2193 | } |
| 2194 | |
| 2195 | /* |
| 2196 | * Compute the screen position for the cursor on the command line. |
| 2197 | */ |
| 2198 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2199 | set_cmdspos_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2200 | { |
| 2201 | int i, m, c; |
| 2202 | |
| 2203 | set_cmdspos(); |
| 2204 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2205 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2206 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 2207 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 2208 | m = MAXCOL; |
| 2209 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2210 | else |
| 2211 | m = MAXCOL; |
| 2212 | for (i = 0; i < ccline.cmdlen && i < ccline.cmdpos; ++i) |
| 2213 | { |
| 2214 | c = cmdline_charsize(i); |
| 2215 | #ifdef FEAT_MBYTE |
| 2216 | /* Count ">" for double-wide multi-byte char that doesn't fit. */ |
| 2217 | if (has_mbyte) |
| 2218 | correct_cmdspos(i, c); |
| 2219 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 2220 | /* If the cmdline doesn't fit, show cursor on last visible char. |
| 2221 | * Don't move the cursor itself, so we can still append. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2222 | if ((ccline.cmdspos += c) >= m) |
| 2223 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2224 | ccline.cmdspos -= c; |
| 2225 | break; |
| 2226 | } |
| 2227 | #ifdef FEAT_MBYTE |
| 2228 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2229 | i += (*mb_ptr2len)(ccline.cmdbuff + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2230 | #endif |
| 2231 | } |
| 2232 | } |
| 2233 | |
| 2234 | #ifdef FEAT_MBYTE |
| 2235 | /* |
| 2236 | * Check if the character at "idx", which is "cells" wide, is a multi-byte |
| 2237 | * character that doesn't fit, so that a ">" must be displayed. |
| 2238 | */ |
| 2239 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2240 | correct_cmdspos(int idx, int cells) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2241 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2242 | if ((*mb_ptr2len)(ccline.cmdbuff + idx) > 1 |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2243 | && (*mb_ptr2cells)(ccline.cmdbuff + idx) > 1 |
| 2244 | && ccline.cmdspos % Columns + cells > Columns) |
| 2245 | ccline.cmdspos++; |
| 2246 | } |
| 2247 | #endif |
| 2248 | |
| 2249 | /* |
| 2250 | * Get an Ex command line for the ":" command. |
| 2251 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2252 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2253 | getexline( |
| 2254 | int c, /* normally ':', NUL for ":append" */ |
| 2255 | void *cookie UNUSED, |
| 2256 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2257 | { |
| 2258 | /* When executing a register, remove ':' that's in front of each line. */ |
| 2259 | if (exec_from_reg && vpeekc() == ':') |
| 2260 | (void)vgetc(); |
| 2261 | return getcmdline(c, 1L, indent); |
| 2262 | } |
| 2263 | |
| 2264 | /* |
| 2265 | * Get an Ex command line for Ex mode. |
| 2266 | * In Ex mode we only use the OS supplied line editing features and no |
| 2267 | * mappings or abbreviations. |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2268 | * Returns a string in allocated memory or NULL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2269 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2270 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2271 | getexmodeline( |
| 2272 | int promptc, /* normally ':', NUL for ":append" and '?' for |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2273 | :s prompt */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2274 | void *cookie UNUSED, |
| 2275 | int indent) /* indent for inside conditionals */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2276 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2277 | garray_T line_ga; |
| 2278 | char_u *pend; |
| 2279 | int startcol = 0; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2280 | int c1 = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2281 | int escaped = FALSE; /* CTRL-V typed */ |
| 2282 | int vcol = 0; |
| 2283 | char_u *p; |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2284 | int prev_char; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2285 | int len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2286 | |
| 2287 | /* Switch cursor on now. This avoids that it happens after the "\n", which |
| 2288 | * confuses the system function that computes tabstops. */ |
| 2289 | cursor_on(); |
| 2290 | |
| 2291 | /* always start in column 0; write a newline if necessary */ |
| 2292 | compute_cmdrow(); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2293 | if ((msg_col || msg_didout) && promptc != '?') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2294 | msg_putchar('\n'); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2295 | if (promptc == ':') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2296 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2297 | /* indent that is only displayed, not in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2298 | if (p_prompt) |
| 2299 | msg_putchar(':'); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2300 | while (indent-- > 0) |
| 2301 | msg_putchar(' '); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2302 | startcol = msg_col; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2303 | } |
| 2304 | |
| 2305 | ga_init2(&line_ga, 1, 30); |
| 2306 | |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2307 | /* autoindent for :insert and :append is in the line itself */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2308 | if (promptc <= 0) |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2309 | { |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2310 | vcol = indent; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2311 | while (indent >= 8) |
| 2312 | { |
| 2313 | ga_append(&line_ga, TAB); |
| 2314 | msg_puts((char_u *)" "); |
| 2315 | indent -= 8; |
| 2316 | } |
| 2317 | while (indent-- > 0) |
| 2318 | { |
| 2319 | ga_append(&line_ga, ' '); |
| 2320 | msg_putchar(' '); |
| 2321 | } |
| 2322 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2323 | ++no_mapping; |
| 2324 | ++allow_keys; |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2325 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2326 | /* |
| 2327 | * Get the line, one character at a time. |
| 2328 | */ |
| 2329 | got_int = FALSE; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2330 | while (!got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2331 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2332 | long sw; |
| 2333 | char_u *s; |
| 2334 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2335 | if (ga_grow(&line_ga, 40) == FAIL) |
| 2336 | break; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2337 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2338 | /* Get one character at a time. Don't use inchar(), it can't handle |
| 2339 | * special characters. */ |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2340 | prev_char = c1; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2341 | c1 = vgetc(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2342 | |
| 2343 | /* |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2344 | * Handle line editing. |
| 2345 | * Previously this was left to the system, putting the terminal in |
| 2346 | * 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] | 2347 | */ |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2348 | if (got_int) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2349 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2350 | msg_putchar('\n'); |
| 2351 | break; |
| 2352 | } |
| 2353 | |
| 2354 | if (!escaped) |
| 2355 | { |
| 2356 | /* CR typed means "enter", which is NL */ |
| 2357 | if (c1 == '\r') |
| 2358 | c1 = '\n'; |
| 2359 | |
| 2360 | if (c1 == BS || c1 == K_BS |
| 2361 | || c1 == DEL || c1 == K_DEL || c1 == K_KDEL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2362 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2363 | if (line_ga.ga_len > 0) |
| 2364 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2365 | #ifdef FEAT_MBYTE |
| 2366 | if (has_mbyte) |
| 2367 | { |
| 2368 | p = (char_u *)line_ga.ga_data; |
| 2369 | p[line_ga.ga_len] = NUL; |
| 2370 | len = (*mb_head_off)(p, p + line_ga.ga_len - 1) + 1; |
| 2371 | line_ga.ga_len -= len; |
| 2372 | } |
| 2373 | else |
| 2374 | #endif |
| 2375 | --line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2376 | goto redraw; |
| 2377 | } |
| 2378 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2379 | } |
| 2380 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2381 | if (c1 == Ctrl_U) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2382 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2383 | msg_col = startcol; |
| 2384 | msg_clr_eos(); |
| 2385 | line_ga.ga_len = 0; |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2386 | goto redraw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2387 | } |
| 2388 | |
| 2389 | if (c1 == Ctrl_T) |
| 2390 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2391 | sw = get_sw_value(curbuf); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2392 | p = (char_u *)line_ga.ga_data; |
| 2393 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2394 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | 14f2474 | 2012-08-08 18:01:05 +0200 | [diff] [blame] | 2395 | indent += sw - indent % sw; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2396 | add_indent: |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2397 | while (get_indent_str(p, 8, FALSE) < indent) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2398 | { |
Bram Moolenaar | cde8854 | 2015-08-11 19:14:00 +0200 | [diff] [blame] | 2399 | (void)ga_grow(&line_ga, 2); /* one more for the NUL */ |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2400 | p = (char_u *)line_ga.ga_data; |
| 2401 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2402 | mch_memmove(s + 1, s, line_ga.ga_len - (s - p) + 1); |
| 2403 | *s = ' '; |
| 2404 | ++line_ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2405 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2406 | redraw: |
| 2407 | /* redraw the line */ |
| 2408 | msg_col = startcol; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2409 | vcol = 0; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2410 | p = (char_u *)line_ga.ga_data; |
| 2411 | p[line_ga.ga_len] = NUL; |
| 2412 | while (p < (char_u *)line_ga.ga_data + line_ga.ga_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2413 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2414 | if (*p == TAB) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2415 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2416 | do |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2417 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2418 | msg_putchar(' '); |
| 2419 | } while (++vcol % 8); |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2420 | ++p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2421 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2422 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2423 | { |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2424 | len = MB_PTR2LEN(p); |
| 2425 | msg_outtrans_len(p, len); |
| 2426 | vcol += ptr2cells(p); |
| 2427 | p += len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2428 | } |
| 2429 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2430 | msg_clr_eos(); |
Bram Moolenaar | 7662423 | 2007-07-28 12:21:47 +0000 | [diff] [blame] | 2431 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2432 | continue; |
| 2433 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2434 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2435 | if (c1 == Ctrl_D) |
| 2436 | { |
| 2437 | /* Delete one shiftwidth. */ |
| 2438 | p = (char_u *)line_ga.ga_data; |
| 2439 | if (prev_char == '0' || prev_char == '^') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2440 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2441 | if (prev_char == '^') |
| 2442 | ex_keep_indent = TRUE; |
| 2443 | indent = 0; |
| 2444 | p[--line_ga.ga_len] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2445 | } |
| 2446 | else |
| 2447 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2448 | p[line_ga.ga_len] = NUL; |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2449 | indent = get_indent_str(p, 8, FALSE); |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2450 | if (indent > 0) |
| 2451 | { |
| 2452 | --indent; |
| 2453 | indent -= indent % get_sw_value(curbuf); |
| 2454 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2455 | } |
Bram Moolenaar | 597a422 | 2014-06-25 14:39:50 +0200 | [diff] [blame] | 2456 | while (get_indent_str(p, 8, FALSE) > indent) |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2457 | { |
Bram Moolenaar | da63657 | 2015-04-03 17:11:45 +0200 | [diff] [blame] | 2458 | s = skipwhite(p); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2459 | mch_memmove(s - 1, s, line_ga.ga_len - (s - p) + 1); |
| 2460 | --line_ga.ga_len; |
| 2461 | } |
| 2462 | goto add_indent; |
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 | |
| 2465 | if (c1 == Ctrl_V || c1 == Ctrl_Q) |
| 2466 | { |
| 2467 | escaped = TRUE; |
| 2468 | continue; |
| 2469 | } |
| 2470 | |
| 2471 | /* Ignore special key codes: mouse movement, K_IGNORE, etc. */ |
| 2472 | if (IS_SPECIAL(c1)) |
| 2473 | continue; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2474 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2475 | |
| 2476 | if (IS_SPECIAL(c1)) |
| 2477 | c1 = '?'; |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2478 | #ifdef FEAT_MBYTE |
| 2479 | if (has_mbyte) |
| 2480 | len = (*mb_char2bytes)(c1, |
| 2481 | (char_u *)line_ga.ga_data + line_ga.ga_len); |
| 2482 | else |
| 2483 | #endif |
| 2484 | { |
| 2485 | len = 1; |
| 2486 | ((char_u *)line_ga.ga_data)[line_ga.ga_len] = c1; |
| 2487 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2488 | if (c1 == '\n') |
| 2489 | msg_putchar('\n'); |
| 2490 | else if (c1 == TAB) |
| 2491 | { |
| 2492 | /* Don't use chartabsize(), 'ts' can be different */ |
| 2493 | do |
| 2494 | { |
| 2495 | msg_putchar(' '); |
| 2496 | } while (++vcol % 8); |
| 2497 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2498 | else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2499 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2500 | msg_outtrans_len( |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2501 | ((char_u *)line_ga.ga_data) + line_ga.ga_len, len); |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2502 | vcol += char2cells(c1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2503 | } |
Bram Moolenaar | 2d54ec9 | 2014-06-12 19:44:48 +0200 | [diff] [blame] | 2504 | line_ga.ga_len += len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2505 | escaped = FALSE; |
| 2506 | |
| 2507 | windgoto(msg_row, msg_col); |
Bram Moolenaar | 4399ef4 | 2005-02-12 14:29:27 +0000 | [diff] [blame] | 2508 | pend = (char_u *)(line_ga.ga_data) + line_ga.ga_len; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2509 | |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2510 | /* We are done when a NL is entered, but not when it comes after an |
| 2511 | * odd number of backslashes, that results in a NUL. */ |
| 2512 | if (line_ga.ga_len > 0 && pend[-1] == '\n') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2513 | { |
Bram Moolenaar | 417f5e7 | 2010-09-29 15:50:30 +0200 | [diff] [blame] | 2514 | int bcount = 0; |
| 2515 | |
| 2516 | while (line_ga.ga_len - 2 >= bcount && pend[-2 - bcount] == '\\') |
| 2517 | ++bcount; |
| 2518 | |
| 2519 | if (bcount > 0) |
| 2520 | { |
| 2521 | /* Halve the number of backslashes: "\NL" -> "NUL", "\\NL" -> |
| 2522 | * "\NL", etc. */ |
| 2523 | line_ga.ga_len -= (bcount + 1) / 2; |
| 2524 | pend -= (bcount + 1) / 2; |
| 2525 | pend[-1] = '\n'; |
| 2526 | } |
| 2527 | |
| 2528 | if ((bcount & 1) == 0) |
| 2529 | { |
| 2530 | --line_ga.ga_len; |
| 2531 | --pend; |
| 2532 | *pend = NUL; |
| 2533 | break; |
| 2534 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2535 | } |
| 2536 | } |
| 2537 | |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 2538 | --no_mapping; |
| 2539 | --allow_keys; |
| 2540 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2541 | /* make following messages go to the next line */ |
| 2542 | msg_didout = FALSE; |
| 2543 | msg_col = 0; |
| 2544 | if (msg_row < Rows - 1) |
| 2545 | ++msg_row; |
| 2546 | emsg_on_display = FALSE; /* don't want ui_delay() */ |
| 2547 | |
| 2548 | if (got_int) |
| 2549 | ga_clear(&line_ga); |
| 2550 | |
| 2551 | return (char_u *)line_ga.ga_data; |
| 2552 | } |
| 2553 | |
Bram Moolenaar | e344bea | 2005-09-01 20:46:49 +0000 | [diff] [blame] | 2554 | # if defined(MCH_CURSOR_SHAPE) || defined(FEAT_GUI) \ |
| 2555 | || defined(FEAT_MOUSESHAPE) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2556 | /* |
| 2557 | * Return TRUE if ccline.overstrike is on. |
| 2558 | */ |
| 2559 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2560 | cmdline_overstrike(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2561 | { |
| 2562 | return ccline.overstrike; |
| 2563 | } |
| 2564 | |
| 2565 | /* |
| 2566 | * Return TRUE if the cursor is at the end of the cmdline. |
| 2567 | */ |
| 2568 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2569 | cmdline_at_end(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2570 | { |
| 2571 | return (ccline.cmdpos >= ccline.cmdlen); |
| 2572 | } |
| 2573 | #endif |
| 2574 | |
Bram Moolenaar | 9372a11 | 2005-12-06 19:59:18 +0000 | [diff] [blame] | 2575 | #if (defined(FEAT_XIM) && (defined(FEAT_GUI_GTK))) || defined(PROTO) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2576 | /* |
| 2577 | * Return the virtual column number at the current cursor position. |
| 2578 | * This is used by the IM code to obtain the start of the preedit string. |
| 2579 | */ |
| 2580 | colnr_T |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2581 | cmdline_getvcol_cursor(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2582 | { |
| 2583 | if (ccline.cmdbuff == NULL || ccline.cmdpos > ccline.cmdlen) |
| 2584 | return MAXCOL; |
| 2585 | |
| 2586 | # ifdef FEAT_MBYTE |
| 2587 | if (has_mbyte) |
| 2588 | { |
| 2589 | colnr_T col; |
| 2590 | int i = 0; |
| 2591 | |
| 2592 | for (col = 0; i < ccline.cmdpos; ++col) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2593 | i += (*mb_ptr2len)(ccline.cmdbuff + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2594 | |
| 2595 | return col; |
| 2596 | } |
| 2597 | else |
| 2598 | # endif |
| 2599 | return ccline.cmdpos; |
| 2600 | } |
| 2601 | #endif |
| 2602 | |
| 2603 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 2604 | /* |
| 2605 | * If part of the command line is an IM preedit string, redraw it with |
| 2606 | * IM feedback attributes. The cursor position is restored after drawing. |
| 2607 | */ |
| 2608 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2609 | redrawcmd_preedit(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2610 | { |
| 2611 | if ((State & CMDLINE) |
| 2612 | && xic != NULL |
Bram Moolenaar | 494c82a | 2006-09-14 08:25:49 +0000 | [diff] [blame] | 2613 | /* && im_get_status() doesn't work when using SCIM */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2614 | && !p_imdisable |
| 2615 | && im_is_preediting()) |
| 2616 | { |
| 2617 | int cmdpos = 0; |
| 2618 | int cmdspos; |
| 2619 | int old_row; |
| 2620 | int old_col; |
| 2621 | colnr_T col; |
| 2622 | |
| 2623 | old_row = msg_row; |
| 2624 | old_col = msg_col; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 2625 | cmdspos = ((ccline.cmdfirstc != NUL) ? 1 : 0) + ccline.cmdindent; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2626 | |
| 2627 | # ifdef FEAT_MBYTE |
| 2628 | if (has_mbyte) |
| 2629 | { |
| 2630 | for (col = 0; col < preedit_start_col |
| 2631 | && cmdpos < ccline.cmdlen; ++col) |
| 2632 | { |
| 2633 | cmdspos += (*mb_ptr2cells)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2634 | cmdpos += (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2635 | } |
| 2636 | } |
| 2637 | else |
| 2638 | # endif |
| 2639 | { |
| 2640 | cmdspos += preedit_start_col; |
| 2641 | cmdpos += preedit_start_col; |
| 2642 | } |
| 2643 | |
| 2644 | msg_row = cmdline_row + (cmdspos / (int)Columns); |
| 2645 | msg_col = cmdspos % (int)Columns; |
| 2646 | if (msg_row >= Rows) |
| 2647 | msg_row = Rows - 1; |
| 2648 | |
| 2649 | for (col = 0; cmdpos < ccline.cmdlen; ++col) |
| 2650 | { |
| 2651 | int char_len; |
| 2652 | int char_attr; |
| 2653 | |
| 2654 | char_attr = im_get_feedback_attr(col); |
| 2655 | if (char_attr < 0) |
| 2656 | break; /* end of preedit string */ |
| 2657 | |
| 2658 | # ifdef FEAT_MBYTE |
| 2659 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2660 | char_len = (*mb_ptr2len)(ccline.cmdbuff + cmdpos); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2661 | else |
| 2662 | # endif |
| 2663 | char_len = 1; |
| 2664 | |
| 2665 | msg_outtrans_len_attr(ccline.cmdbuff + cmdpos, char_len, char_attr); |
| 2666 | cmdpos += char_len; |
| 2667 | } |
| 2668 | |
| 2669 | msg_row = old_row; |
| 2670 | msg_col = old_col; |
| 2671 | } |
| 2672 | } |
| 2673 | #endif /* FEAT_XIM && FEAT_GUI_GTK */ |
| 2674 | |
| 2675 | /* |
| 2676 | * Allocate a new command line buffer. |
| 2677 | * Assigns the new buffer to ccline.cmdbuff and ccline.cmdbufflen. |
| 2678 | * Returns the new value of ccline.cmdbuff and ccline.cmdbufflen. |
| 2679 | */ |
| 2680 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2681 | alloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2682 | { |
| 2683 | /* |
| 2684 | * give some extra space to avoid having to allocate all the time |
| 2685 | */ |
| 2686 | if (len < 80) |
| 2687 | len = 100; |
| 2688 | else |
| 2689 | len += 20; |
| 2690 | |
| 2691 | ccline.cmdbuff = alloc(len); /* caller should check for out-of-memory */ |
| 2692 | ccline.cmdbufflen = len; |
| 2693 | } |
| 2694 | |
| 2695 | /* |
| 2696 | * Re-allocate the command line to length len + something extra. |
| 2697 | * return FAIL for failure, OK otherwise |
| 2698 | */ |
| 2699 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2700 | realloc_cmdbuff(int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2701 | { |
| 2702 | char_u *p; |
| 2703 | |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 2704 | if (len < ccline.cmdbufflen) |
| 2705 | return OK; /* no need to resize */ |
| 2706 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2707 | p = ccline.cmdbuff; |
| 2708 | alloc_cmdbuff(len); /* will get some more */ |
| 2709 | if (ccline.cmdbuff == NULL) /* out of memory */ |
| 2710 | { |
| 2711 | ccline.cmdbuff = p; /* keep the old one */ |
| 2712 | return FAIL; |
| 2713 | } |
Bram Moolenaar | 35a3423 | 2010-08-13 16:51:26 +0200 | [diff] [blame] | 2714 | /* There isn't always a NUL after the command, but it may need to be |
| 2715 | * there, thus copy up to the NUL and add a NUL. */ |
| 2716 | mch_memmove(ccline.cmdbuff, p, (size_t)ccline.cmdlen); |
| 2717 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2718 | vim_free(p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2719 | |
| 2720 | if (ccline.xpc != NULL |
| 2721 | && ccline.xpc->xp_pattern != NULL |
| 2722 | && ccline.xpc->xp_context != EXPAND_NOTHING |
| 2723 | && ccline.xpc->xp_context != EXPAND_UNSUCCESSFUL) |
| 2724 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 2725 | int i = (int)(ccline.xpc->xp_pattern - p); |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 2726 | |
| 2727 | /* If xp_pattern points inside the old cmdbuff it needs to be adjusted |
| 2728 | * to point into the newly allocated memory. */ |
| 2729 | if (i >= 0 && i <= ccline.cmdlen) |
| 2730 | ccline.xpc->xp_pattern = ccline.cmdbuff + i; |
| 2731 | } |
| 2732 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2733 | return OK; |
| 2734 | } |
| 2735 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2736 | #if defined(FEAT_ARABIC) || defined(PROTO) |
| 2737 | static char_u *arshape_buf = NULL; |
| 2738 | |
| 2739 | # if defined(EXITFREE) || defined(PROTO) |
| 2740 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2741 | free_cmdline_buf(void) |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2742 | { |
| 2743 | vim_free(arshape_buf); |
| 2744 | } |
| 2745 | # endif |
| 2746 | #endif |
| 2747 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2748 | /* |
| 2749 | * Draw part of the cmdline at the current cursor position. But draw stars |
| 2750 | * when cmdline_star is TRUE. |
| 2751 | */ |
| 2752 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2753 | draw_cmdline(int start, int len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2754 | { |
| 2755 | #if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 2756 | int i; |
| 2757 | |
| 2758 | if (cmdline_star > 0) |
| 2759 | for (i = 0; i < len; ++i) |
| 2760 | { |
| 2761 | msg_putchar('*'); |
| 2762 | # ifdef FEAT_MBYTE |
| 2763 | if (has_mbyte) |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2764 | i += (*mb_ptr2len)(ccline.cmdbuff + start + i) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2765 | # endif |
| 2766 | } |
| 2767 | else |
| 2768 | #endif |
| 2769 | #ifdef FEAT_ARABIC |
| 2770 | if (p_arshape && !p_tbidi && enc_utf8 && len > 0) |
| 2771 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2772 | static int buflen = 0; |
| 2773 | char_u *p; |
| 2774 | int j; |
| 2775 | int newlen = 0; |
| 2776 | int mb_l; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 2777 | int pc, pc1 = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2778 | int prev_c = 0; |
| 2779 | int prev_c1 = 0; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2780 | int u8c; |
| 2781 | int u8cc[MAX_MCO]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2782 | int nc = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2783 | |
| 2784 | /* |
| 2785 | * Do arabic shaping into a temporary buffer. This is very |
| 2786 | * inefficient! |
| 2787 | */ |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2788 | if (len * 2 + 2 > buflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2789 | { |
| 2790 | /* Re-allocate the buffer. We keep it around to avoid a lot of |
| 2791 | * alloc()/free() calls. */ |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2792 | vim_free(arshape_buf); |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2793 | buflen = len * 2 + 2; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2794 | arshape_buf = alloc(buflen); |
| 2795 | if (arshape_buf == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2796 | return; /* out of memory */ |
| 2797 | } |
| 2798 | |
Bram Moolenaar | cafda4f | 2005-09-06 19:25:11 +0000 | [diff] [blame] | 2799 | if (utf_iscomposing(utf_ptr2char(ccline.cmdbuff + start))) |
| 2800 | { |
| 2801 | /* Prepend a space to draw the leading composing char on. */ |
| 2802 | arshape_buf[0] = ' '; |
| 2803 | newlen = 1; |
| 2804 | } |
| 2805 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2806 | for (j = start; j < start + len; j += mb_l) |
| 2807 | { |
| 2808 | p = ccline.cmdbuff + j; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2809 | u8c = utfc_ptr2char_len(p, u8cc, start + len - j); |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2810 | mb_l = utfc_ptr2len_len(p, start + len - j); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2811 | if (ARABIC_CHAR(u8c)) |
| 2812 | { |
| 2813 | /* Do Arabic shaping. */ |
| 2814 | if (cmdmsg_rl) |
| 2815 | { |
| 2816 | /* displaying from right to left */ |
| 2817 | pc = prev_c; |
| 2818 | pc1 = prev_c1; |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2819 | prev_c1 = u8cc[0]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2820 | if (j + mb_l >= start + len) |
| 2821 | nc = NUL; |
| 2822 | else |
| 2823 | nc = utf_ptr2char(p + mb_l); |
| 2824 | } |
| 2825 | else |
| 2826 | { |
| 2827 | /* displaying from left to right */ |
| 2828 | if (j + mb_l >= start + len) |
| 2829 | pc = NUL; |
| 2830 | else |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2831 | { |
| 2832 | int pcc[MAX_MCO]; |
| 2833 | |
| 2834 | pc = utfc_ptr2char_len(p + mb_l, pcc, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2835 | start + len - j - mb_l); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2836 | pc1 = pcc[0]; |
| 2837 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2838 | nc = prev_c; |
| 2839 | } |
| 2840 | prev_c = u8c; |
| 2841 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2842 | u8c = arabic_shape(u8c, NULL, &u8cc[0], pc, pc1, nc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2843 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2844 | newlen += (*mb_char2bytes)(u8c, arshape_buf + newlen); |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2845 | if (u8cc[0] != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2846 | { |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 2847 | newlen += (*mb_char2bytes)(u8cc[0], arshape_buf + newlen); |
| 2848 | if (u8cc[1] != 0) |
| 2849 | newlen += (*mb_char2bytes)(u8cc[1], |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2850 | arshape_buf + newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2851 | } |
| 2852 | } |
| 2853 | else |
| 2854 | { |
| 2855 | prev_c = u8c; |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2856 | mch_memmove(arshape_buf + newlen, p, mb_l); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2857 | newlen += mb_l; |
| 2858 | } |
| 2859 | } |
| 2860 | |
Bram Moolenaar | f461c8e | 2005-06-25 23:04:51 +0000 | [diff] [blame] | 2861 | msg_outtrans_len(arshape_buf, newlen); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2862 | } |
| 2863 | else |
| 2864 | #endif |
| 2865 | msg_outtrans_len(ccline.cmdbuff + start, len); |
| 2866 | } |
| 2867 | |
| 2868 | /* |
| 2869 | * Put a character on the command line. Shifts the following text to the |
| 2870 | * right when "shift" is TRUE. Used for CTRL-V, CTRL-K, etc. |
| 2871 | * "c" must be printable (fit in one display cell)! |
| 2872 | */ |
| 2873 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2874 | putcmdline(int c, int shift) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2875 | { |
| 2876 | if (cmd_silent) |
| 2877 | return; |
| 2878 | msg_no_more = TRUE; |
| 2879 | msg_putchar(c); |
| 2880 | if (shift) |
| 2881 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 2882 | msg_no_more = FALSE; |
| 2883 | cursorcmd(); |
| 2884 | } |
| 2885 | |
| 2886 | /* |
| 2887 | * Undo a putcmdline(c, FALSE). |
| 2888 | */ |
| 2889 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2890 | unputcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2891 | { |
| 2892 | if (cmd_silent) |
| 2893 | return; |
| 2894 | msg_no_more = TRUE; |
| 2895 | if (ccline.cmdlen == ccline.cmdpos) |
| 2896 | msg_putchar(' '); |
Bram Moolenaar | 64fdf5c | 2012-06-06 12:03:06 +0200 | [diff] [blame] | 2897 | #ifdef FEAT_MBYTE |
| 2898 | else if (has_mbyte) |
| 2899 | draw_cmdline(ccline.cmdpos, |
| 2900 | (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos)); |
| 2901 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2902 | else |
| 2903 | draw_cmdline(ccline.cmdpos, 1); |
| 2904 | msg_no_more = FALSE; |
| 2905 | cursorcmd(); |
| 2906 | } |
| 2907 | |
| 2908 | /* |
| 2909 | * Put the given string, of the given length, onto the command line. |
| 2910 | * If len is -1, then STRLEN() is used to calculate the length. |
| 2911 | * If 'redraw' is TRUE then the new part of the command line, and the remaining |
| 2912 | * part will be redrawn, otherwise it will not. If this function is called |
| 2913 | * twice in a row, then 'redraw' should be FALSE and redrawcmd() should be |
| 2914 | * called afterwards. |
| 2915 | */ |
| 2916 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 2917 | put_on_cmdline(char_u *str, int len, int redraw) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2918 | { |
| 2919 | int retval; |
| 2920 | int i; |
| 2921 | int m; |
| 2922 | int c; |
| 2923 | |
| 2924 | if (len < 0) |
| 2925 | len = (int)STRLEN(str); |
| 2926 | |
| 2927 | /* Check if ccline.cmdbuff needs to be longer */ |
| 2928 | if (ccline.cmdlen + len + 1 >= ccline.cmdbufflen) |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 2929 | retval = realloc_cmdbuff(ccline.cmdlen + len + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2930 | else |
| 2931 | retval = OK; |
| 2932 | if (retval == OK) |
| 2933 | { |
| 2934 | if (!ccline.overstrike) |
| 2935 | { |
| 2936 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 2937 | ccline.cmdbuff + ccline.cmdpos, |
| 2938 | (size_t)(ccline.cmdlen - ccline.cmdpos)); |
| 2939 | ccline.cmdlen += len; |
| 2940 | } |
| 2941 | else |
| 2942 | { |
| 2943 | #ifdef FEAT_MBYTE |
| 2944 | if (has_mbyte) |
| 2945 | { |
| 2946 | /* Count nr of characters in the new string. */ |
| 2947 | m = 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2948 | for (i = 0; i < len; i += (*mb_ptr2len)(str + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2949 | ++m; |
| 2950 | /* Count nr of bytes in cmdline that are overwritten by these |
| 2951 | * characters. */ |
| 2952 | for (i = ccline.cmdpos; i < ccline.cmdlen && m > 0; |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 2953 | i += (*mb_ptr2len)(ccline.cmdbuff + i)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 2954 | --m; |
| 2955 | if (i < ccline.cmdlen) |
| 2956 | { |
| 2957 | mch_memmove(ccline.cmdbuff + ccline.cmdpos + len, |
| 2958 | ccline.cmdbuff + i, (size_t)(ccline.cmdlen - i)); |
| 2959 | ccline.cmdlen += ccline.cmdpos + len - i; |
| 2960 | } |
| 2961 | else |
| 2962 | ccline.cmdlen = ccline.cmdpos + len; |
| 2963 | } |
| 2964 | else |
| 2965 | #endif |
| 2966 | if (ccline.cmdpos + len > ccline.cmdlen) |
| 2967 | ccline.cmdlen = ccline.cmdpos + len; |
| 2968 | } |
| 2969 | mch_memmove(ccline.cmdbuff + ccline.cmdpos, str, (size_t)len); |
| 2970 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 2971 | |
| 2972 | #ifdef FEAT_MBYTE |
| 2973 | if (enc_utf8) |
| 2974 | { |
| 2975 | /* When the inserted text starts with a composing character, |
| 2976 | * backup to the character before it. There could be two of them. |
| 2977 | */ |
| 2978 | i = 0; |
| 2979 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 2980 | while (ccline.cmdpos > 0 && utf_iscomposing(c)) |
| 2981 | { |
| 2982 | i = (*mb_head_off)(ccline.cmdbuff, |
| 2983 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 2984 | ccline.cmdpos -= i; |
| 2985 | len += i; |
| 2986 | c = utf_ptr2char(ccline.cmdbuff + ccline.cmdpos); |
| 2987 | } |
| 2988 | # ifdef FEAT_ARABIC |
| 2989 | if (i == 0 && ccline.cmdpos > 0 && arabic_maycombine(c)) |
| 2990 | { |
| 2991 | /* Check the previous character for Arabic combining pair. */ |
| 2992 | i = (*mb_head_off)(ccline.cmdbuff, |
| 2993 | ccline.cmdbuff + ccline.cmdpos - 1) + 1; |
| 2994 | if (arabic_combine(utf_ptr2char(ccline.cmdbuff |
| 2995 | + ccline.cmdpos - i), c)) |
| 2996 | { |
| 2997 | ccline.cmdpos -= i; |
| 2998 | len += i; |
| 2999 | } |
| 3000 | else |
| 3001 | i = 0; |
| 3002 | } |
| 3003 | # endif |
| 3004 | if (i != 0) |
| 3005 | { |
| 3006 | /* Also backup the cursor position. */ |
| 3007 | i = ptr2cells(ccline.cmdbuff + ccline.cmdpos); |
| 3008 | ccline.cmdspos -= i; |
| 3009 | msg_col -= i; |
| 3010 | if (msg_col < 0) |
| 3011 | { |
| 3012 | msg_col += Columns; |
| 3013 | --msg_row; |
| 3014 | } |
| 3015 | } |
| 3016 | } |
| 3017 | #endif |
| 3018 | |
| 3019 | if (redraw && !cmd_silent) |
| 3020 | { |
| 3021 | msg_no_more = TRUE; |
| 3022 | i = cmdline_row; |
Bram Moolenaar | 73dc59a | 2011-09-30 17:46:21 +0200 | [diff] [blame] | 3023 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3024 | draw_cmdline(ccline.cmdpos, ccline.cmdlen - ccline.cmdpos); |
| 3025 | /* Avoid clearing the rest of the line too often. */ |
| 3026 | if (cmdline_row != i || ccline.overstrike) |
| 3027 | msg_clr_eos(); |
| 3028 | msg_no_more = FALSE; |
| 3029 | } |
| 3030 | #ifdef FEAT_FKMAP |
| 3031 | /* |
| 3032 | * If we are in Farsi command mode, the character input must be in |
| 3033 | * Insert mode. So do not advance the cmdpos. |
| 3034 | */ |
| 3035 | if (!cmd_fkmap) |
| 3036 | #endif |
| 3037 | { |
| 3038 | if (KeyTyped) |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3039 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3040 | m = Columns * Rows; |
Bram Moolenaar | 482aaeb | 2005-09-29 18:26:07 +0000 | [diff] [blame] | 3041 | if (m < 0) /* overflow, Columns or Rows at weird value */ |
| 3042 | m = MAXCOL; |
| 3043 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3044 | else |
| 3045 | m = MAXCOL; |
| 3046 | for (i = 0; i < len; ++i) |
| 3047 | { |
| 3048 | c = cmdline_charsize(ccline.cmdpos); |
| 3049 | #ifdef FEAT_MBYTE |
| 3050 | /* count ">" for a double-wide char that doesn't fit. */ |
| 3051 | if (has_mbyte) |
| 3052 | correct_cmdspos(ccline.cmdpos, c); |
| 3053 | #endif |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3054 | /* Stop cursor at the end of the screen, but do increment the |
| 3055 | * insert position, so that entering a very long command |
| 3056 | * works, even though you can't see it. */ |
| 3057 | if (ccline.cmdspos + c < m) |
| 3058 | ccline.cmdspos += c; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3059 | #ifdef FEAT_MBYTE |
| 3060 | if (has_mbyte) |
| 3061 | { |
Bram Moolenaar | 0fa313a | 2005-08-10 21:07:57 +0000 | [diff] [blame] | 3062 | c = (*mb_ptr2len)(ccline.cmdbuff + ccline.cmdpos) - 1; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3063 | if (c > len - i - 1) |
| 3064 | c = len - i - 1; |
| 3065 | ccline.cmdpos += c; |
| 3066 | i += c; |
| 3067 | } |
| 3068 | #endif |
| 3069 | ++ccline.cmdpos; |
| 3070 | } |
| 3071 | } |
| 3072 | } |
| 3073 | if (redraw) |
| 3074 | msg_check(); |
| 3075 | return retval; |
| 3076 | } |
| 3077 | |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3078 | static struct cmdline_info prev_ccline; |
| 3079 | static int prev_ccline_used = FALSE; |
| 3080 | |
| 3081 | /* |
| 3082 | * Save ccline, because obtaining the "=" register may execute "normal :cmd" |
| 3083 | * and overwrite it. But get_cmdline_str() may need it, thus make it |
| 3084 | * available globally in prev_ccline. |
| 3085 | */ |
| 3086 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3087 | save_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3088 | { |
| 3089 | if (!prev_ccline_used) |
| 3090 | { |
| 3091 | vim_memset(&prev_ccline, 0, sizeof(struct cmdline_info)); |
| 3092 | prev_ccline_used = TRUE; |
| 3093 | } |
| 3094 | *ccp = prev_ccline; |
| 3095 | prev_ccline = ccline; |
| 3096 | ccline.cmdbuff = NULL; |
| 3097 | ccline.cmdprompt = NULL; |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3098 | ccline.xpc = NULL; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3099 | } |
| 3100 | |
| 3101 | /* |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 3102 | * Restore ccline after it has been saved with save_cmdline(). |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3103 | */ |
| 3104 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3105 | restore_cmdline(struct cmdline_info *ccp) |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3106 | { |
| 3107 | ccline = prev_ccline; |
| 3108 | prev_ccline = *ccp; |
| 3109 | } |
| 3110 | |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3111 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 3112 | /* |
| 3113 | * Save the command line into allocated memory. Returns a pointer to be |
| 3114 | * passed to restore_cmdline_alloc() later. |
| 3115 | * Returns NULL when failed. |
| 3116 | */ |
| 3117 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3118 | save_cmdline_alloc(void) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3119 | { |
| 3120 | struct cmdline_info *p; |
| 3121 | |
| 3122 | p = (struct cmdline_info *)alloc((unsigned)sizeof(struct cmdline_info)); |
| 3123 | if (p != NULL) |
| 3124 | save_cmdline(p); |
| 3125 | return (char_u *)p; |
| 3126 | } |
| 3127 | |
| 3128 | /* |
| 3129 | * Restore the command line from the return value of save_cmdline_alloc(). |
| 3130 | */ |
| 3131 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3132 | restore_cmdline_alloc(char_u *p) |
Bram Moolenaar | 5a30542 | 2006-04-28 22:38:25 +0000 | [diff] [blame] | 3133 | { |
| 3134 | if (p != NULL) |
| 3135 | { |
| 3136 | restore_cmdline((struct cmdline_info *)p); |
| 3137 | vim_free(p); |
| 3138 | } |
| 3139 | } |
| 3140 | #endif |
| 3141 | |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3142 | /* |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3143 | * Paste a yank register into the command line. |
| 3144 | * Used by CTRL-R command in command-line mode. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3145 | * insert_reg() can't be used here, because special characters from the |
| 3146 | * register contents will be interpreted as commands. |
| 3147 | * |
Bram Moolenaar | 6f62fed | 2015-12-17 14:04:24 +0100 | [diff] [blame] | 3148 | * Return FAIL for failure, OK otherwise. |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3149 | */ |
| 3150 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3151 | cmdline_paste( |
| 3152 | int regname, |
| 3153 | int literally, /* Insert text literally instead of "as typed" */ |
| 3154 | int remcr) /* remove trailing CR */ |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3155 | { |
| 3156 | long i; |
| 3157 | char_u *arg; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3158 | char_u *p; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3159 | int allocated; |
| 3160 | struct cmdline_info save_ccline; |
| 3161 | |
| 3162 | /* check for valid regname; also accept special characters for CTRL-R in |
| 3163 | * the command line */ |
| 3164 | if (regname != Ctrl_F && regname != Ctrl_P && regname != Ctrl_W |
| 3165 | && regname != Ctrl_A && !valid_yank_reg(regname, FALSE)) |
| 3166 | return FAIL; |
| 3167 | |
| 3168 | /* A register containing CTRL-R can cause an endless loop. Allow using |
| 3169 | * CTRL-C to break the loop. */ |
| 3170 | line_breakcheck(); |
| 3171 | if (got_int) |
| 3172 | return FAIL; |
| 3173 | |
| 3174 | #ifdef FEAT_CLIPBOARD |
| 3175 | regname = may_get_selection(regname); |
| 3176 | #endif |
| 3177 | |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3178 | /* Need to save and restore ccline. And set "textlock" to avoid nasty |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3179 | * things like going to another buffer when evaluating an expression. */ |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3180 | save_cmdline(&save_ccline); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3181 | ++textlock; |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3182 | i = get_spec_reg(regname, &arg, &allocated, TRUE); |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 3183 | --textlock; |
Bram Moolenaar | 5f2bb9f | 2005-01-11 21:29:04 +0000 | [diff] [blame] | 3184 | restore_cmdline(&save_ccline); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3185 | |
| 3186 | if (i) |
| 3187 | { |
| 3188 | /* Got the value of a special register in "arg". */ |
| 3189 | if (arg == NULL) |
| 3190 | return FAIL; |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3191 | |
| 3192 | /* When 'incsearch' is set and CTRL-R CTRL-W used: skip the duplicate |
| 3193 | * part of the word. */ |
| 3194 | p = arg; |
| 3195 | if (p_is && regname == Ctrl_W) |
| 3196 | { |
| 3197 | char_u *w; |
| 3198 | int len; |
| 3199 | |
| 3200 | /* Locate start of last word in the cmd buffer. */ |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3201 | for (w = ccline.cmdbuff + ccline.cmdpos; w > ccline.cmdbuff; ) |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3202 | { |
| 3203 | #ifdef FEAT_MBYTE |
| 3204 | if (has_mbyte) |
| 3205 | { |
| 3206 | len = (*mb_head_off)(ccline.cmdbuff, w - 1) + 1; |
| 3207 | if (!vim_iswordc(mb_ptr2char(w - len))) |
| 3208 | break; |
| 3209 | w -= len; |
| 3210 | } |
| 3211 | else |
| 3212 | #endif |
| 3213 | { |
| 3214 | if (!vim_iswordc(w[-1])) |
| 3215 | break; |
| 3216 | --w; |
| 3217 | } |
| 3218 | } |
Bram Moolenaar | 80ae7b2 | 2011-07-07 16:44:37 +0200 | [diff] [blame] | 3219 | len = (int)((ccline.cmdbuff + ccline.cmdpos) - w); |
Bram Moolenaar | efd2bf1 | 2006-03-16 21:41:35 +0000 | [diff] [blame] | 3220 | if (p_ic ? STRNICMP(w, arg, len) == 0 : STRNCMP(w, arg, len) == 0) |
| 3221 | p += len; |
| 3222 | } |
| 3223 | |
| 3224 | cmdline_paste_str(p, literally); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3225 | if (allocated) |
| 3226 | vim_free(arg); |
| 3227 | return OK; |
| 3228 | } |
| 3229 | |
Bram Moolenaar | 1769d5a | 2006-10-17 14:25:24 +0000 | [diff] [blame] | 3230 | return cmdline_paste_reg(regname, literally, remcr); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3231 | } |
| 3232 | |
| 3233 | /* |
| 3234 | * Put a string on the command line. |
| 3235 | * When "literally" is TRUE, insert literally. |
| 3236 | * When "literally" is FALSE, insert as typed, but don't leave the command |
| 3237 | * line. |
| 3238 | */ |
| 3239 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3240 | cmdline_paste_str(char_u *s, int literally) |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3241 | { |
| 3242 | int c, cv; |
| 3243 | |
| 3244 | if (literally) |
| 3245 | put_on_cmdline(s, -1, TRUE); |
| 3246 | else |
| 3247 | while (*s != NUL) |
| 3248 | { |
| 3249 | cv = *s; |
| 3250 | if (cv == Ctrl_V && s[1]) |
| 3251 | ++s; |
| 3252 | #ifdef FEAT_MBYTE |
| 3253 | if (has_mbyte) |
Bram Moolenaar | 48be32b | 2008-06-20 10:56:16 +0000 | [diff] [blame] | 3254 | c = mb_cptr2char_adv(&s); |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3255 | else |
| 3256 | #endif |
| 3257 | c = *s++; |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 3258 | if (cv == Ctrl_V || c == ESC || c == Ctrl_C |
| 3259 | || c == CAR || c == NL || c == Ctrl_L |
Bram Moolenaar | 8299df9 | 2004-07-10 09:47:34 +0000 | [diff] [blame] | 3260 | #ifdef UNIX |
| 3261 | || c == intr_char |
| 3262 | #endif |
| 3263 | || (c == Ctrl_BSL && *s == Ctrl_N)) |
| 3264 | stuffcharReadbuff(Ctrl_V); |
| 3265 | stuffcharReadbuff(c); |
| 3266 | } |
| 3267 | } |
| 3268 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3269 | #ifdef FEAT_WILDMENU |
| 3270 | /* |
| 3271 | * Delete characters on the command line, from "from" to the current |
| 3272 | * position. |
| 3273 | */ |
| 3274 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3275 | cmdline_del(int from) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3276 | { |
| 3277 | mch_memmove(ccline.cmdbuff + from, ccline.cmdbuff + ccline.cmdpos, |
| 3278 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3279 | ccline.cmdlen -= ccline.cmdpos - from; |
| 3280 | ccline.cmdpos = from; |
| 3281 | } |
| 3282 | #endif |
| 3283 | |
| 3284 | /* |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 3285 | * This function is called when the screen size changes and with incremental |
| 3286 | * search and in other situations where the command line may have been |
| 3287 | * overwritten. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3288 | */ |
| 3289 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3290 | redrawcmdline(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3291 | { |
| 3292 | if (cmd_silent) |
| 3293 | return; |
| 3294 | need_wait_return = FALSE; |
| 3295 | compute_cmdrow(); |
| 3296 | redrawcmd(); |
| 3297 | cursorcmd(); |
| 3298 | } |
| 3299 | |
| 3300 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3301 | redrawcmdprompt(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3302 | { |
| 3303 | int i; |
| 3304 | |
| 3305 | if (cmd_silent) |
| 3306 | return; |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3307 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3308 | msg_putchar(ccline.cmdfirstc); |
| 3309 | if (ccline.cmdprompt != NULL) |
| 3310 | { |
| 3311 | msg_puts_attr(ccline.cmdprompt, ccline.cmdattr); |
| 3312 | ccline.cmdindent = msg_col + (msg_row - cmdline_row) * Columns; |
| 3313 | /* do the reverse of set_cmdspos() */ |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 3314 | if (ccline.cmdfirstc != NUL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3315 | --ccline.cmdindent; |
| 3316 | } |
| 3317 | else |
| 3318 | for (i = ccline.cmdindent; i > 0; --i) |
| 3319 | msg_putchar(' '); |
| 3320 | } |
| 3321 | |
| 3322 | /* |
| 3323 | * Redraw what is currently on the command line. |
| 3324 | */ |
| 3325 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3326 | redrawcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3327 | { |
| 3328 | if (cmd_silent) |
| 3329 | return; |
| 3330 | |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 3331 | /* when 'incsearch' is set there may be no command line while redrawing */ |
| 3332 | if (ccline.cmdbuff == NULL) |
| 3333 | { |
| 3334 | windgoto(cmdline_row, 0); |
| 3335 | msg_clr_eos(); |
| 3336 | return; |
| 3337 | } |
| 3338 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3339 | msg_start(); |
| 3340 | redrawcmdprompt(); |
| 3341 | |
| 3342 | /* Don't use more prompt, truncate the cmdline if it doesn't fit. */ |
| 3343 | msg_no_more = TRUE; |
| 3344 | draw_cmdline(0, ccline.cmdlen); |
| 3345 | msg_clr_eos(); |
| 3346 | msg_no_more = FALSE; |
| 3347 | |
| 3348 | set_cmdspos_cursor(); |
| 3349 | |
| 3350 | /* |
| 3351 | * An emsg() before may have set msg_scroll. This is used in normal mode, |
| 3352 | * in cmdline mode we can reset them now. |
| 3353 | */ |
| 3354 | msg_scroll = FALSE; /* next message overwrites cmdline */ |
| 3355 | |
| 3356 | /* Typing ':' at the more prompt may set skip_redraw. We don't want this |
| 3357 | * in cmdline mode */ |
| 3358 | skip_redraw = FALSE; |
| 3359 | } |
| 3360 | |
| 3361 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3362 | compute_cmdrow(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3363 | { |
Bram Moolenaar | 1c7715d | 2005-10-03 22:02:18 +0000 | [diff] [blame] | 3364 | if (exmode_active || msg_scrolled != 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3365 | cmdline_row = Rows - 1; |
| 3366 | else |
| 3367 | cmdline_row = W_WINROW(lastwin) + lastwin->w_height |
| 3368 | + W_STATUS_HEIGHT(lastwin); |
| 3369 | } |
| 3370 | |
| 3371 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3372 | cursorcmd(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3373 | { |
| 3374 | if (cmd_silent) |
| 3375 | return; |
| 3376 | |
| 3377 | #ifdef FEAT_RIGHTLEFT |
| 3378 | if (cmdmsg_rl) |
| 3379 | { |
| 3380 | msg_row = cmdline_row + (ccline.cmdspos / (int)(Columns - 1)); |
| 3381 | msg_col = (int)Columns - (ccline.cmdspos % (int)(Columns - 1)) - 1; |
| 3382 | if (msg_row <= 0) |
| 3383 | msg_row = Rows - 1; |
| 3384 | } |
| 3385 | else |
| 3386 | #endif |
| 3387 | { |
| 3388 | msg_row = cmdline_row + (ccline.cmdspos / (int)Columns); |
| 3389 | msg_col = ccline.cmdspos % (int)Columns; |
| 3390 | if (msg_row >= Rows) |
| 3391 | msg_row = Rows - 1; |
| 3392 | } |
| 3393 | |
| 3394 | windgoto(msg_row, msg_col); |
| 3395 | #if defined(FEAT_XIM) && defined(FEAT_GUI_GTK) |
| 3396 | redrawcmd_preedit(); |
| 3397 | #endif |
| 3398 | #ifdef MCH_CURSOR_SHAPE |
| 3399 | mch_update_cursor(); |
| 3400 | #endif |
| 3401 | } |
| 3402 | |
| 3403 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3404 | gotocmdline(int clr) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3405 | { |
| 3406 | msg_start(); |
| 3407 | #ifdef FEAT_RIGHTLEFT |
| 3408 | if (cmdmsg_rl) |
| 3409 | msg_col = Columns - 1; |
| 3410 | else |
| 3411 | #endif |
| 3412 | msg_col = 0; /* always start in column 0 */ |
| 3413 | if (clr) /* clear the bottom line(s) */ |
| 3414 | msg_clr_eos(); /* will reset clear_cmdline */ |
| 3415 | windgoto(cmdline_row, 0); |
| 3416 | } |
| 3417 | |
| 3418 | /* |
| 3419 | * Check the word in front of the cursor for an abbreviation. |
| 3420 | * Called when the non-id character "c" has been entered. |
| 3421 | * When an abbreviation is recognized it is removed from the text with |
| 3422 | * backspaces and the replacement string is inserted, followed by "c". |
| 3423 | */ |
| 3424 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3425 | ccheck_abbr(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3426 | { |
| 3427 | if (p_paste || no_abbr) /* no abbreviations or in paste mode */ |
| 3428 | return FALSE; |
| 3429 | |
| 3430 | return check_abbr(c, ccline.cmdbuff, ccline.cmdpos, 0); |
| 3431 | } |
| 3432 | |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3433 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 3434 | static int |
| 3435 | #ifdef __BORLANDC__ |
| 3436 | _RTLENTRYF |
| 3437 | #endif |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3438 | sort_func_compare(const void *s1, const void *s2) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 3439 | { |
| 3440 | char_u *p1 = *(char_u **)s1; |
| 3441 | char_u *p2 = *(char_u **)s2; |
| 3442 | |
| 3443 | if (*p1 != '<' && *p2 == '<') return -1; |
| 3444 | if (*p1 == '<' && *p2 != '<') return 1; |
| 3445 | return STRCMP(p1, p2); |
| 3446 | } |
| 3447 | #endif |
| 3448 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3449 | /* |
| 3450 | * Return FAIL if this is not an appropriate context in which to do |
| 3451 | * completion of anything, return OK if it is (even if there are no matches). |
| 3452 | * For the caller, this means that the character is just passed through like a |
| 3453 | * normal character (instead of being expanded). This allows :s/^I^D etc. |
| 3454 | */ |
| 3455 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3456 | nextwild( |
| 3457 | expand_T *xp, |
| 3458 | int type, |
| 3459 | int options, /* extra options for ExpandOne() */ |
| 3460 | int escape) /* if TRUE, escape the returned matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3461 | { |
| 3462 | int i, j; |
| 3463 | char_u *p1; |
| 3464 | char_u *p2; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3465 | int difflen; |
| 3466 | int v; |
| 3467 | |
| 3468 | if (xp->xp_numfiles == -1) |
| 3469 | { |
| 3470 | set_expand_context(xp); |
| 3471 | cmd_showtail = expand_showtail(xp); |
| 3472 | } |
| 3473 | |
| 3474 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 3475 | { |
| 3476 | beep_flush(); |
| 3477 | return OK; /* Something illegal on command line */ |
| 3478 | } |
| 3479 | if (xp->xp_context == EXPAND_NOTHING) |
| 3480 | { |
| 3481 | /* Caller can use the character as a normal char instead */ |
| 3482 | return FAIL; |
| 3483 | } |
| 3484 | |
| 3485 | MSG_PUTS("..."); /* show that we are busy */ |
| 3486 | out_flush(); |
| 3487 | |
| 3488 | i = (int)(xp->xp_pattern - ccline.cmdbuff); |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3489 | xp->xp_pattern_len = ccline.cmdpos - i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3490 | |
| 3491 | if (type == WILD_NEXT || type == WILD_PREV) |
| 3492 | { |
| 3493 | /* |
| 3494 | * Get next/previous match for a previous expanded pattern. |
| 3495 | */ |
| 3496 | p2 = ExpandOne(xp, NULL, NULL, 0, type); |
| 3497 | } |
| 3498 | else |
| 3499 | { |
| 3500 | /* |
| 3501 | * Translate string into pattern and expand it. |
| 3502 | */ |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3503 | if ((p1 = addstar(xp->xp_pattern, xp->xp_pattern_len, |
| 3504 | xp->xp_context)) == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3505 | p2 = NULL; |
| 3506 | else |
| 3507 | { |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3508 | int use_options = options | |
Bram Moolenaar | b347963 | 2012-11-28 16:49:58 +0100 | [diff] [blame] | 3509 | WILD_HOME_REPLACE|WILD_ADD_SLASH|WILD_SILENT; |
| 3510 | if (escape) |
| 3511 | use_options |= WILD_ESCAPE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3512 | |
| 3513 | if (p_wic) |
| 3514 | use_options += WILD_ICASE; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3515 | p2 = ExpandOne(xp, p1, |
| 3516 | vim_strnsave(&ccline.cmdbuff[i], xp->xp_pattern_len), |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3517 | use_options, type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3518 | vim_free(p1); |
Bram Moolenaar | 2660c0e | 2010-01-19 14:59:56 +0100 | [diff] [blame] | 3519 | /* longest match: make sure it is not shorter, happens with :help */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3520 | if (p2 != NULL && type == WILD_LONGEST) |
| 3521 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3522 | for (j = 0; j < xp->xp_pattern_len; ++j) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3523 | if (ccline.cmdbuff[i + j] == '*' |
| 3524 | || ccline.cmdbuff[i + j] == '?') |
| 3525 | break; |
| 3526 | if ((int)STRLEN(p2) < j) |
| 3527 | { |
| 3528 | vim_free(p2); |
| 3529 | p2 = NULL; |
| 3530 | } |
| 3531 | } |
| 3532 | } |
| 3533 | } |
| 3534 | |
| 3535 | if (p2 != NULL && !got_int) |
| 3536 | { |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3537 | difflen = (int)STRLEN(p2) - xp->xp_pattern_len; |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3538 | if (ccline.cmdlen + difflen + 4 > ccline.cmdbufflen) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3539 | { |
Bram Moolenaar | 673b87b | 2010-08-13 19:12:07 +0200 | [diff] [blame] | 3540 | v = realloc_cmdbuff(ccline.cmdlen + difflen + 4); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3541 | xp->xp_pattern = ccline.cmdbuff + i; |
| 3542 | } |
| 3543 | else |
| 3544 | v = OK; |
| 3545 | if (v == OK) |
| 3546 | { |
Bram Moolenaar | 9ba0eb8 | 2005-06-13 22:28:56 +0000 | [diff] [blame] | 3547 | mch_memmove(&ccline.cmdbuff[ccline.cmdpos + difflen], |
| 3548 | &ccline.cmdbuff[ccline.cmdpos], |
| 3549 | (size_t)(ccline.cmdlen - ccline.cmdpos + 1)); |
| 3550 | mch_memmove(&ccline.cmdbuff[i], p2, STRLEN(p2)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3551 | ccline.cmdlen += difflen; |
| 3552 | ccline.cmdpos += difflen; |
| 3553 | } |
| 3554 | } |
| 3555 | vim_free(p2); |
| 3556 | |
| 3557 | redrawcmd(); |
Bram Moolenaar | 009b259 | 2004-10-24 19:18:58 +0000 | [diff] [blame] | 3558 | cursorcmd(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3559 | |
| 3560 | /* When expanding a ":map" command and no matches are found, assume that |
| 3561 | * the key is supposed to be inserted literally */ |
| 3562 | if (xp->xp_context == EXPAND_MAPPINGS && p2 == NULL) |
| 3563 | return FAIL; |
| 3564 | |
| 3565 | if (xp->xp_numfiles <= 0 && p2 == NULL) |
| 3566 | beep_flush(); |
| 3567 | else if (xp->xp_numfiles == 1) |
| 3568 | /* free expanded pattern */ |
| 3569 | (void)ExpandOne(xp, NULL, NULL, 0, WILD_FREE); |
| 3570 | |
| 3571 | return OK; |
| 3572 | } |
| 3573 | |
| 3574 | /* |
| 3575 | * Do wildcard expansion on the string 'str'. |
| 3576 | * Chars that should not be expanded must be preceded with a backslash. |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 3577 | * Return a pointer to allocated memory containing the new string. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3578 | * Return NULL for failure. |
| 3579 | * |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3580 | * "orig" is the originally expanded string, copied to allocated memory. It |
| 3581 | * should either be kept in orig_save or freed. When "mode" is WILD_NEXT or |
| 3582 | * WILD_PREV "orig" should be NULL. |
| 3583 | * |
Bram Moolenaar | fc1421e | 2006-04-20 22:17:20 +0000 | [diff] [blame] | 3584 | * Results are cached in xp->xp_files and xp->xp_numfiles, except when "mode" |
| 3585 | * is WILD_EXPAND_FREE or WILD_ALL. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3586 | * |
| 3587 | * mode = WILD_FREE: just free previously expanded matches |
| 3588 | * mode = WILD_EXPAND_FREE: normal expansion, do not keep matches |
| 3589 | * mode = WILD_EXPAND_KEEP: normal expansion, keep matches |
| 3590 | * mode = WILD_NEXT: use next match in multiple match, wrap to first |
| 3591 | * mode = WILD_PREV: use previous match in multiple match, wrap to first |
| 3592 | * mode = WILD_ALL: return all matches concatenated |
| 3593 | * mode = WILD_LONGEST: return longest matched part |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3594 | * mode = WILD_ALL_KEEP: get all matches, keep matches |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3595 | * |
| 3596 | * options = WILD_LIST_NOTFOUND: list entries without a match |
| 3597 | * options = WILD_HOME_REPLACE: do home_replace() for buffer names |
| 3598 | * options = WILD_USE_NL: Use '\n' for WILD_ALL |
| 3599 | * options = WILD_NO_BEEP: Don't beep for multiple matches |
| 3600 | * options = WILD_ADD_SLASH: add a slash after directory names |
| 3601 | * options = WILD_KEEP_ALL: don't remove 'wildignore' entries |
| 3602 | * options = WILD_SILENT: don't print warning messages |
| 3603 | * options = WILD_ESCAPE: put backslash before special chars |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 3604 | * options = WILD_ICASE: ignore case for files |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3605 | * |
| 3606 | * The variables xp->xp_context and xp->xp_backslash must have been set! |
| 3607 | */ |
| 3608 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3609 | ExpandOne( |
| 3610 | expand_T *xp, |
| 3611 | char_u *str, |
| 3612 | char_u *orig, /* allocated copy of original of expanded string */ |
| 3613 | int options, |
| 3614 | int mode) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3615 | { |
| 3616 | char_u *ss = NULL; |
| 3617 | static int findex; |
| 3618 | static char_u *orig_save = NULL; /* kept value of orig */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3619 | int orig_saved = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3620 | int i; |
| 3621 | long_u len; |
| 3622 | int non_suf_match; /* number without matching suffix */ |
| 3623 | |
| 3624 | /* |
| 3625 | * first handle the case of using an old match |
| 3626 | */ |
| 3627 | if (mode == WILD_NEXT || mode == WILD_PREV) |
| 3628 | { |
| 3629 | if (xp->xp_numfiles > 0) |
| 3630 | { |
| 3631 | if (mode == WILD_PREV) |
| 3632 | { |
| 3633 | if (findex == -1) |
| 3634 | findex = xp->xp_numfiles; |
| 3635 | --findex; |
| 3636 | } |
| 3637 | else /* mode == WILD_NEXT */ |
| 3638 | ++findex; |
| 3639 | |
| 3640 | /* |
| 3641 | * When wrapping around, return the original string, set findex to |
| 3642 | * -1. |
| 3643 | */ |
| 3644 | if (findex < 0) |
| 3645 | { |
| 3646 | if (orig_save == NULL) |
| 3647 | findex = xp->xp_numfiles - 1; |
| 3648 | else |
| 3649 | findex = -1; |
| 3650 | } |
| 3651 | if (findex >= xp->xp_numfiles) |
| 3652 | { |
| 3653 | if (orig_save == NULL) |
| 3654 | findex = 0; |
| 3655 | else |
| 3656 | findex = -1; |
| 3657 | } |
| 3658 | #ifdef FEAT_WILDMENU |
| 3659 | if (p_wmnu) |
| 3660 | win_redr_status_matches(xp, xp->xp_numfiles, xp->xp_files, |
| 3661 | findex, cmd_showtail); |
| 3662 | #endif |
| 3663 | if (findex == -1) |
| 3664 | return vim_strsave(orig_save); |
| 3665 | return vim_strsave(xp->xp_files[findex]); |
| 3666 | } |
| 3667 | else |
| 3668 | return NULL; |
| 3669 | } |
| 3670 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3671 | /* free old names */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3672 | if (xp->xp_numfiles != -1 && mode != WILD_ALL && mode != WILD_LONGEST) |
| 3673 | { |
| 3674 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3675 | xp->xp_numfiles = -1; |
| 3676 | vim_free(orig_save); |
| 3677 | orig_save = NULL; |
| 3678 | } |
| 3679 | findex = 0; |
| 3680 | |
| 3681 | if (mode == WILD_FREE) /* only release file name */ |
| 3682 | return NULL; |
| 3683 | |
| 3684 | if (xp->xp_numfiles == -1) |
| 3685 | { |
| 3686 | vim_free(orig_save); |
| 3687 | orig_save = orig; |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3688 | orig_saved = TRUE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3689 | |
| 3690 | /* |
| 3691 | * Do the expansion. |
| 3692 | */ |
| 3693 | if (ExpandFromContext(xp, str, &xp->xp_numfiles, &xp->xp_files, |
| 3694 | options) == FAIL) |
| 3695 | { |
| 3696 | #ifdef FNAME_ILLEGAL |
| 3697 | /* Illegal file name has been silently skipped. But when there |
| 3698 | * are wildcards, the real problem is that there was no match, |
| 3699 | * causing the pattern to be added, which has illegal characters. |
| 3700 | */ |
| 3701 | if (!(options & WILD_SILENT) && (options & WILD_LIST_NOTFOUND)) |
| 3702 | EMSG2(_(e_nomatch2), str); |
| 3703 | #endif |
| 3704 | } |
| 3705 | else if (xp->xp_numfiles == 0) |
| 3706 | { |
| 3707 | if (!(options & WILD_SILENT)) |
| 3708 | EMSG2(_(e_nomatch2), str); |
| 3709 | } |
| 3710 | else |
| 3711 | { |
| 3712 | /* Escape the matches for use on the command line. */ |
| 3713 | ExpandEscape(xp, str, xp->xp_numfiles, xp->xp_files, options); |
| 3714 | |
| 3715 | /* |
| 3716 | * Check for matching suffixes in file names. |
| 3717 | */ |
Bram Moolenaar | 146e9c3 | 2012-03-07 19:18:23 +0100 | [diff] [blame] | 3718 | if (mode != WILD_ALL && mode != WILD_ALL_KEEP |
| 3719 | && mode != WILD_LONGEST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3720 | { |
| 3721 | if (xp->xp_numfiles) |
| 3722 | non_suf_match = xp->xp_numfiles; |
| 3723 | else |
| 3724 | non_suf_match = 1; |
| 3725 | if ((xp->xp_context == EXPAND_FILES |
| 3726 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3727 | && xp->xp_numfiles > 1) |
| 3728 | { |
| 3729 | /* |
| 3730 | * More than one match; check suffix. |
| 3731 | * The files will have been sorted on matching suffix in |
| 3732 | * expand_wildcards, only need to check the first two. |
| 3733 | */ |
| 3734 | non_suf_match = 0; |
| 3735 | for (i = 0; i < 2; ++i) |
| 3736 | if (match_suffix(xp->xp_files[i])) |
| 3737 | ++non_suf_match; |
| 3738 | } |
| 3739 | if (non_suf_match != 1) |
| 3740 | { |
| 3741 | /* Can we ever get here unless it's while expanding |
| 3742 | * interactively? If not, we can get rid of this all |
| 3743 | * together. Don't really want to wait for this message |
| 3744 | * (and possibly have to hit return to continue!). |
| 3745 | */ |
| 3746 | if (!(options & WILD_SILENT)) |
| 3747 | EMSG(_(e_toomany)); |
| 3748 | else if (!(options & WILD_NO_BEEP)) |
| 3749 | beep_flush(); |
| 3750 | } |
| 3751 | if (!(non_suf_match != 1 && mode == WILD_EXPAND_FREE)) |
| 3752 | ss = vim_strsave(xp->xp_files[0]); |
| 3753 | } |
| 3754 | } |
| 3755 | } |
| 3756 | |
| 3757 | /* Find longest common part */ |
| 3758 | if (mode == WILD_LONGEST && xp->xp_numfiles > 0) |
| 3759 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3760 | int mb_len = 1; |
| 3761 | int c0, ci; |
| 3762 | |
| 3763 | for (len = 0; xp->xp_files[0][len]; len += mb_len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3764 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3765 | #ifdef FEAT_MBYTE |
| 3766 | if (has_mbyte) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3767 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3768 | mb_len = (*mb_ptr2len)(&xp->xp_files[0][len]); |
| 3769 | c0 =(* mb_ptr2char)(&xp->xp_files[0][len]); |
| 3770 | } |
| 3771 | else |
| 3772 | #endif |
Bram Moolenaar | e4eda3b | 2015-11-21 16:28:50 +0100 | [diff] [blame] | 3773 | c0 = xp->xp_files[0][len]; |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3774 | for (i = 1; i < xp->xp_numfiles; ++i) |
| 3775 | { |
| 3776 | #ifdef FEAT_MBYTE |
| 3777 | if (has_mbyte) |
| 3778 | ci =(* mb_ptr2char)(&xp->xp_files[i][len]); |
| 3779 | else |
| 3780 | #endif |
| 3781 | ci = xp->xp_files[i][len]; |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3782 | if (p_fic && (xp->xp_context == EXPAND_DIRECTORIES |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3783 | || xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3784 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 71afbfe | 2013-03-19 16:49:16 +0100 | [diff] [blame] | 3785 | || xp->xp_context == EXPAND_BUFFERS)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3786 | { |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3787 | if (MB_TOLOWER(c0) != MB_TOLOWER(ci)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3788 | break; |
| 3789 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3790 | else if (c0 != ci) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3791 | break; |
| 3792 | } |
| 3793 | if (i < xp->xp_numfiles) |
| 3794 | { |
| 3795 | if (!(options & WILD_NO_BEEP)) |
Bram Moolenaar | 165bc69 | 2015-07-21 17:53:25 +0200 | [diff] [blame] | 3796 | vim_beep(BO_WILD); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3797 | break; |
| 3798 | } |
| 3799 | } |
Bram Moolenaar | 4f8fa16 | 2015-11-19 19:00:05 +0100 | [diff] [blame] | 3800 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3801 | ss = alloc((unsigned)len + 1); |
| 3802 | if (ss) |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 3803 | vim_strncpy(ss, xp->xp_files[0], (size_t)len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3804 | findex = -1; /* next p_wc gets first one */ |
| 3805 | } |
| 3806 | |
| 3807 | /* Concatenate all matching names */ |
| 3808 | if (mode == WILD_ALL && xp->xp_numfiles > 0) |
| 3809 | { |
| 3810 | len = 0; |
| 3811 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3812 | len += (long_u)STRLEN(xp->xp_files[i]) + 1; |
| 3813 | ss = lalloc(len, TRUE); |
| 3814 | if (ss != NULL) |
| 3815 | { |
| 3816 | *ss = NUL; |
| 3817 | for (i = 0; i < xp->xp_numfiles; ++i) |
| 3818 | { |
| 3819 | STRCAT(ss, xp->xp_files[i]); |
| 3820 | if (i != xp->xp_numfiles - 1) |
| 3821 | STRCAT(ss, (options & WILD_USE_NL) ? "\n" : " "); |
| 3822 | } |
| 3823 | } |
| 3824 | } |
| 3825 | |
| 3826 | if (mode == WILD_EXPAND_FREE || mode == WILD_ALL) |
| 3827 | ExpandCleanup(xp); |
| 3828 | |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3829 | /* Free "orig" if it wasn't stored in "orig_save". */ |
Bram Moolenaar | 9642664 | 2007-10-30 16:37:15 +0000 | [diff] [blame] | 3830 | if (!orig_saved) |
Bram Moolenaar | ecf4de5 | 2007-09-30 20:11:26 +0000 | [diff] [blame] | 3831 | vim_free(orig); |
| 3832 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3833 | return ss; |
| 3834 | } |
| 3835 | |
| 3836 | /* |
| 3837 | * Prepare an expand structure for use. |
| 3838 | */ |
| 3839 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3840 | ExpandInit(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3841 | { |
Bram Moolenaar | d6e7cc6 | 2008-09-14 12:42:29 +0000 | [diff] [blame] | 3842 | xp->xp_pattern = NULL; |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 3843 | xp->xp_pattern_len = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3844 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3845 | #ifndef BACKSLASH_IN_FILENAME |
| 3846 | xp->xp_shell = FALSE; |
| 3847 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3848 | xp->xp_numfiles = -1; |
| 3849 | xp->xp_files = NULL; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 3850 | #if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) && defined(FEAT_CMDL_COMPL) |
| 3851 | xp->xp_arg = NULL; |
| 3852 | #endif |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 3853 | xp->xp_line = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3854 | } |
| 3855 | |
| 3856 | /* |
| 3857 | * Cleanup an expand structure after use. |
| 3858 | */ |
| 3859 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3860 | ExpandCleanup(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3861 | { |
| 3862 | if (xp->xp_numfiles >= 0) |
| 3863 | { |
| 3864 | FreeWild(xp->xp_numfiles, xp->xp_files); |
| 3865 | xp->xp_numfiles = -1; |
| 3866 | } |
| 3867 | } |
| 3868 | |
| 3869 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3870 | ExpandEscape( |
| 3871 | expand_T *xp, |
| 3872 | char_u *str, |
| 3873 | int numfiles, |
| 3874 | char_u **files, |
| 3875 | int options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3876 | { |
| 3877 | int i; |
| 3878 | char_u *p; |
| 3879 | |
| 3880 | /* |
| 3881 | * May change home directory back to "~" |
| 3882 | */ |
| 3883 | if (options & WILD_HOME_REPLACE) |
| 3884 | tilde_replace(str, numfiles, files); |
| 3885 | |
| 3886 | if (options & WILD_ESCAPE) |
| 3887 | { |
| 3888 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | cca92ec | 2011-04-28 17:21:53 +0200 | [diff] [blame] | 3889 | || xp->xp_context == EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 3890 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3891 | || xp->xp_context == EXPAND_BUFFERS |
| 3892 | || xp->xp_context == EXPAND_DIRECTORIES) |
| 3893 | { |
| 3894 | /* |
| 3895 | * Insert a backslash into a file name before a space, \, %, # |
| 3896 | * and wildmatch characters, except '~'. |
| 3897 | */ |
| 3898 | for (i = 0; i < numfiles; ++i) |
| 3899 | { |
| 3900 | /* for ":set path=" we need to escape spaces twice */ |
| 3901 | if (xp->xp_backslash == XP_BS_THREE) |
| 3902 | { |
| 3903 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 3904 | if (p != NULL) |
| 3905 | { |
| 3906 | vim_free(files[i]); |
| 3907 | files[i] = p; |
Bram Moolenaar | 4ea8fe1 | 2006-03-09 22:32:39 +0000 | [diff] [blame] | 3908 | #if defined(BACKSLASH_IN_FILENAME) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3909 | p = vim_strsave_escaped(files[i], (char_u *)" "); |
| 3910 | if (p != NULL) |
| 3911 | { |
| 3912 | vim_free(files[i]); |
| 3913 | files[i] = p; |
| 3914 | } |
| 3915 | #endif |
| 3916 | } |
| 3917 | } |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 3918 | #ifdef BACKSLASH_IN_FILENAME |
| 3919 | p = vim_strsave_fnameescape(files[i], FALSE); |
| 3920 | #else |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3921 | p = vim_strsave_fnameescape(files[i], xp->xp_shell); |
Bram Moolenaar | 0356c8c | 2008-05-28 20:02:48 +0000 | [diff] [blame] | 3922 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3923 | if (p != NULL) |
| 3924 | { |
| 3925 | vim_free(files[i]); |
| 3926 | files[i] = p; |
| 3927 | } |
| 3928 | |
| 3929 | /* If 'str' starts with "\~", replace "~" at start of |
| 3930 | * files[i] with "\~". */ |
| 3931 | if (str[0] == '\\' && str[1] == '~' && files[i][0] == '~') |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3932 | escape_fname(&files[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3933 | } |
| 3934 | xp->xp_backslash = XP_BS_NONE; |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 3935 | |
| 3936 | /* If the first file starts with a '+' escape it. Otherwise it |
| 3937 | * could be seen as "+cmd". */ |
| 3938 | if (*files[0] == '+') |
| 3939 | escape_fname(&files[0]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 3940 | } |
| 3941 | else if (xp->xp_context == EXPAND_TAGS) |
| 3942 | { |
| 3943 | /* |
| 3944 | * Insert a backslash before characters in a tag name that |
| 3945 | * would terminate the ":tag" command. |
| 3946 | */ |
| 3947 | for (i = 0; i < numfiles; ++i) |
| 3948 | { |
| 3949 | p = vim_strsave_escaped(files[i], (char_u *)"\\|\""); |
| 3950 | if (p != NULL) |
| 3951 | { |
| 3952 | vim_free(files[i]); |
| 3953 | files[i] = p; |
| 3954 | } |
| 3955 | } |
| 3956 | } |
| 3957 | } |
| 3958 | } |
| 3959 | |
| 3960 | /* |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3961 | * Escape special characters in "fname" for when used as a file name argument |
| 3962 | * after a Vim command, or, when "shell" is non-zero, a shell command. |
| 3963 | * Returns the result in allocated memory. |
| 3964 | */ |
| 3965 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 3966 | vim_strsave_fnameescape(char_u *fname, int shell) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3967 | { |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 3968 | char_u *p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3969 | #ifdef BACKSLASH_IN_FILENAME |
| 3970 | char_u buf[20]; |
| 3971 | int j = 0; |
| 3972 | |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 3973 | /* Don't escape '[', '{' and '!' if they are in 'isfname'. */ |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3974 | for (p = PATH_ESC_CHARS; *p != NUL; ++p) |
Bram Moolenaar | 8f5610d | 2013-11-12 05:28:26 +0100 | [diff] [blame] | 3975 | if ((*p != '[' && *p != '{' && *p != '!') || !vim_isfilec(*p)) |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3976 | buf[j++] = *p; |
| 3977 | buf[j] = NUL; |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 3978 | p = vim_strsave_escaped(fname, buf); |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3979 | #else |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 3980 | p = vim_strsave_escaped(fname, shell ? SHELL_ESC_CHARS : PATH_ESC_CHARS); |
| 3981 | if (shell && csh_like_shell() && p != NULL) |
| 3982 | { |
| 3983 | char_u *s; |
| 3984 | |
| 3985 | /* For csh and similar shells need to put two backslashes before '!'. |
| 3986 | * One is taken by Vim, one by the shell. */ |
| 3987 | s = vim_strsave_escaped(p, (char_u *)"!"); |
| 3988 | vim_free(p); |
| 3989 | p = s; |
| 3990 | } |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3991 | #endif |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 3992 | |
| 3993 | /* '>' and '+' are special at the start of some commands, e.g. ":edit" and |
| 3994 | * ":write". "cd -" has a special meaning. */ |
Bram Moolenaar | a9d52e3 | 2010-07-31 16:44:19 +0200 | [diff] [blame] | 3995 | if (p != NULL && (*p == '>' || *p == '+' || (*p == '-' && p[1] == NUL))) |
Bram Moolenaar | 1b24e4b | 2008-08-08 10:59:17 +0000 | [diff] [blame] | 3996 | escape_fname(&p); |
| 3997 | |
| 3998 | return p; |
Bram Moolenaar | aebaf89 | 2008-05-28 14:49:58 +0000 | [diff] [blame] | 3999 | } |
| 4000 | |
| 4001 | /* |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4002 | * Put a backslash before the file name in "pp", which is in allocated memory. |
| 4003 | */ |
| 4004 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4005 | escape_fname(char_u **pp) |
Bram Moolenaar | 4536002 | 2005-07-21 21:08:21 +0000 | [diff] [blame] | 4006 | { |
| 4007 | char_u *p; |
| 4008 | |
| 4009 | p = alloc((unsigned)(STRLEN(*pp) + 2)); |
| 4010 | if (p != NULL) |
| 4011 | { |
| 4012 | p[0] = '\\'; |
| 4013 | STRCPY(p + 1, *pp); |
| 4014 | vim_free(*pp); |
| 4015 | *pp = p; |
| 4016 | } |
| 4017 | } |
| 4018 | |
| 4019 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4020 | * For each file name in files[num_files]: |
| 4021 | * If 'orig_pat' starts with "~/", replace the home directory with "~". |
| 4022 | */ |
| 4023 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4024 | tilde_replace( |
| 4025 | char_u *orig_pat, |
| 4026 | int num_files, |
| 4027 | char_u **files) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4028 | { |
| 4029 | int i; |
| 4030 | char_u *p; |
| 4031 | |
| 4032 | if (orig_pat[0] == '~' && vim_ispathsep(orig_pat[1])) |
| 4033 | { |
| 4034 | for (i = 0; i < num_files; ++i) |
| 4035 | { |
| 4036 | p = home_replace_save(NULL, files[i]); |
| 4037 | if (p != NULL) |
| 4038 | { |
| 4039 | vim_free(files[i]); |
| 4040 | files[i] = p; |
| 4041 | } |
| 4042 | } |
| 4043 | } |
| 4044 | } |
| 4045 | |
| 4046 | /* |
| 4047 | * Show all matches for completion on the command line. |
| 4048 | * Returns EXPAND_NOTHING when the character that triggered expansion should |
| 4049 | * be inserted like a normal character. |
| 4050 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4051 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4052 | showmatches(expand_T *xp, int wildmenu UNUSED) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4053 | { |
| 4054 | #define L_SHOWFILE(m) (showtail ? sm_gettail(files_found[m]) : files_found[m]) |
| 4055 | int num_files; |
| 4056 | char_u **files_found; |
| 4057 | int i, j, k; |
| 4058 | int maxlen; |
| 4059 | int lines; |
| 4060 | int columns; |
| 4061 | char_u *p; |
| 4062 | int lastlen; |
| 4063 | int attr; |
| 4064 | int showtail; |
| 4065 | |
| 4066 | if (xp->xp_numfiles == -1) |
| 4067 | { |
| 4068 | set_expand_context(xp); |
| 4069 | i = expand_cmdline(xp, ccline.cmdbuff, ccline.cmdpos, |
| 4070 | &num_files, &files_found); |
| 4071 | showtail = expand_showtail(xp); |
| 4072 | if (i != EXPAND_OK) |
| 4073 | return i; |
| 4074 | |
| 4075 | } |
| 4076 | else |
| 4077 | { |
| 4078 | num_files = xp->xp_numfiles; |
| 4079 | files_found = xp->xp_files; |
| 4080 | showtail = cmd_showtail; |
| 4081 | } |
| 4082 | |
| 4083 | #ifdef FEAT_WILDMENU |
| 4084 | if (!wildmenu) |
| 4085 | { |
| 4086 | #endif |
| 4087 | msg_didany = FALSE; /* lines_left will be set */ |
| 4088 | msg_start(); /* prepare for paging */ |
| 4089 | msg_putchar('\n'); |
| 4090 | out_flush(); |
| 4091 | cmdline_row = msg_row; |
| 4092 | msg_didany = FALSE; /* lines_left will be set again */ |
| 4093 | msg_start(); /* prepare for paging */ |
| 4094 | #ifdef FEAT_WILDMENU |
| 4095 | } |
| 4096 | #endif |
| 4097 | |
| 4098 | if (got_int) |
| 4099 | got_int = FALSE; /* only int. the completion, not the cmd line */ |
| 4100 | #ifdef FEAT_WILDMENU |
| 4101 | else if (wildmenu) |
| 4102 | win_redr_status_matches(xp, num_files, files_found, 0, showtail); |
| 4103 | #endif |
| 4104 | else |
| 4105 | { |
| 4106 | /* find the length of the longest file name */ |
| 4107 | maxlen = 0; |
| 4108 | for (i = 0; i < num_files; ++i) |
| 4109 | { |
| 4110 | if (!showtail && (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4111 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4112 | || xp->xp_context == EXPAND_BUFFERS)) |
| 4113 | { |
| 4114 | home_replace(NULL, files_found[i], NameBuff, MAXPATHL, TRUE); |
| 4115 | j = vim_strsize(NameBuff); |
| 4116 | } |
| 4117 | else |
| 4118 | j = vim_strsize(L_SHOWFILE(i)); |
| 4119 | if (j > maxlen) |
| 4120 | maxlen = j; |
| 4121 | } |
| 4122 | |
| 4123 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4124 | lines = num_files; |
| 4125 | else |
| 4126 | { |
| 4127 | /* compute the number of columns and lines for the listing */ |
| 4128 | maxlen += 2; /* two spaces between file names */ |
| 4129 | columns = ((int)Columns + 2) / maxlen; |
| 4130 | if (columns < 1) |
| 4131 | columns = 1; |
| 4132 | lines = (num_files + columns - 1) / columns; |
| 4133 | } |
| 4134 | |
| 4135 | attr = hl_attr(HLF_D); /* find out highlighting for directories */ |
| 4136 | |
| 4137 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4138 | { |
| 4139 | MSG_PUTS_ATTR(_("tagname"), hl_attr(HLF_T)); |
| 4140 | msg_clr_eos(); |
| 4141 | msg_advance(maxlen - 3); |
| 4142 | MSG_PUTS_ATTR(_(" kind file\n"), hl_attr(HLF_T)); |
| 4143 | } |
| 4144 | |
| 4145 | /* list the files line by line */ |
| 4146 | for (i = 0; i < lines; ++i) |
| 4147 | { |
| 4148 | lastlen = 999; |
| 4149 | for (k = i; k < num_files; k += lines) |
| 4150 | { |
| 4151 | if (xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4152 | { |
| 4153 | msg_outtrans_attr(files_found[k], hl_attr(HLF_D)); |
| 4154 | p = files_found[k] + STRLEN(files_found[k]) + 1; |
| 4155 | msg_advance(maxlen + 1); |
| 4156 | msg_puts(p); |
| 4157 | msg_advance(maxlen + 3); |
| 4158 | msg_puts_long_attr(p + 2, hl_attr(HLF_D)); |
| 4159 | break; |
| 4160 | } |
| 4161 | for (j = maxlen - lastlen; --j >= 0; ) |
| 4162 | msg_putchar(' '); |
| 4163 | if (xp->xp_context == EXPAND_FILES |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4164 | || xp->xp_context == EXPAND_SHELLCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4165 | || xp->xp_context == EXPAND_BUFFERS) |
| 4166 | { |
Bram Moolenaar | b91e59b | 2010-03-17 19:13:27 +0100 | [diff] [blame] | 4167 | /* highlight directories */ |
Bram Moolenaar | 63fa526 | 2010-03-23 18:06:52 +0100 | [diff] [blame] | 4168 | if (xp->xp_numfiles != -1) |
| 4169 | { |
| 4170 | char_u *halved_slash; |
| 4171 | char_u *exp_path; |
| 4172 | |
| 4173 | /* Expansion was done before and special characters |
| 4174 | * were escaped, need to halve backslashes. Also |
| 4175 | * $HOME has been replaced with ~/. */ |
| 4176 | exp_path = expand_env_save_opt(files_found[k], TRUE); |
| 4177 | halved_slash = backslash_halve_save( |
| 4178 | exp_path != NULL ? exp_path : files_found[k]); |
| 4179 | j = mch_isdir(halved_slash != NULL ? halved_slash |
| 4180 | : files_found[k]); |
| 4181 | vim_free(exp_path); |
| 4182 | vim_free(halved_slash); |
| 4183 | } |
| 4184 | else |
| 4185 | /* Expansion was done here, file names are literal. */ |
| 4186 | j = mch_isdir(files_found[k]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4187 | if (showtail) |
| 4188 | p = L_SHOWFILE(k); |
| 4189 | else |
| 4190 | { |
| 4191 | home_replace(NULL, files_found[k], NameBuff, MAXPATHL, |
| 4192 | TRUE); |
| 4193 | p = NameBuff; |
| 4194 | } |
| 4195 | } |
| 4196 | else |
| 4197 | { |
| 4198 | j = FALSE; |
| 4199 | p = L_SHOWFILE(k); |
| 4200 | } |
| 4201 | lastlen = msg_outtrans_attr(p, j ? attr : 0); |
| 4202 | } |
| 4203 | if (msg_col > 0) /* when not wrapped around */ |
| 4204 | { |
| 4205 | msg_clr_eos(); |
| 4206 | msg_putchar('\n'); |
| 4207 | } |
| 4208 | out_flush(); /* show one line at a time */ |
| 4209 | if (got_int) |
| 4210 | { |
| 4211 | got_int = FALSE; |
| 4212 | break; |
| 4213 | } |
| 4214 | } |
| 4215 | |
| 4216 | /* |
| 4217 | * we redraw the command below the lines that we have just listed |
| 4218 | * This is a bit tricky, but it saves a lot of screen updating. |
| 4219 | */ |
| 4220 | cmdline_row = msg_row; /* will put it back later */ |
| 4221 | } |
| 4222 | |
| 4223 | if (xp->xp_numfiles == -1) |
| 4224 | FreeWild(num_files, files_found); |
| 4225 | |
| 4226 | return EXPAND_OK; |
| 4227 | } |
| 4228 | |
| 4229 | /* |
| 4230 | * Private gettail for showmatches() (and win_redr_status_matches()): |
| 4231 | * Find tail of file name path, but ignore trailing "/". |
| 4232 | */ |
| 4233 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4234 | sm_gettail(char_u *s) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4235 | { |
| 4236 | char_u *p; |
| 4237 | char_u *t = s; |
| 4238 | int had_sep = FALSE; |
| 4239 | |
| 4240 | for (p = s; *p != NUL; ) |
| 4241 | { |
| 4242 | if (vim_ispathsep(*p) |
| 4243 | #ifdef BACKSLASH_IN_FILENAME |
| 4244 | && !rem_backslash(p) |
| 4245 | #endif |
| 4246 | ) |
| 4247 | had_sep = TRUE; |
| 4248 | else if (had_sep) |
| 4249 | { |
| 4250 | t = p; |
| 4251 | had_sep = FALSE; |
| 4252 | } |
Bram Moolenaar | 1cd871b | 2004-12-19 22:46:22 +0000 | [diff] [blame] | 4253 | mb_ptr_adv(p); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4254 | } |
| 4255 | return t; |
| 4256 | } |
| 4257 | |
| 4258 | /* |
| 4259 | * Return TRUE if we only need to show the tail of completion matches. |
| 4260 | * When not completing file names or there is a wildcard in the path FALSE is |
| 4261 | * returned. |
| 4262 | */ |
| 4263 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4264 | expand_showtail(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4265 | { |
| 4266 | char_u *s; |
| 4267 | char_u *end; |
| 4268 | |
| 4269 | /* When not completing file names a "/" may mean something different. */ |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4270 | if (xp->xp_context != EXPAND_FILES |
| 4271 | && xp->xp_context != EXPAND_SHELLCMD |
| 4272 | && xp->xp_context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4273 | return FALSE; |
| 4274 | |
| 4275 | end = gettail(xp->xp_pattern); |
| 4276 | if (end == xp->xp_pattern) /* there is no path separator */ |
| 4277 | return FALSE; |
| 4278 | |
| 4279 | for (s = xp->xp_pattern; s < end; s++) |
| 4280 | { |
| 4281 | /* Skip escaped wildcards. Only when the backslash is not a path |
| 4282 | * separator, on DOS the '*' "path\*\file" must not be skipped. */ |
| 4283 | if (rem_backslash(s)) |
| 4284 | ++s; |
| 4285 | else if (vim_strchr((char_u *)"*?[", *s) != NULL) |
| 4286 | return FALSE; |
| 4287 | } |
| 4288 | return TRUE; |
| 4289 | } |
| 4290 | |
| 4291 | /* |
| 4292 | * Prepare a string for expansion. |
| 4293 | * When expanding file names: The string will be used with expand_wildcards(). |
Bram Moolenaar | 6bcbcc5 | 2013-11-05 07:13:41 +0100 | [diff] [blame] | 4294 | * Copy "fname[len]" into allocated memory and add a '*' at the end. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4295 | * When expanding other names: The string will be used with regcomp(). Copy |
| 4296 | * the name into allocated memory and prepend "^". |
| 4297 | */ |
| 4298 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4299 | addstar( |
| 4300 | char_u *fname, |
| 4301 | int len, |
| 4302 | int context) /* EXPAND_FILES etc. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4303 | { |
| 4304 | char_u *retval; |
| 4305 | int i, j; |
| 4306 | int new_len; |
| 4307 | char_u *tail; |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4308 | int ends_in_star; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4309 | |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4310 | if (context != EXPAND_FILES |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4311 | && context != EXPAND_FILES_IN_PATH |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4312 | && context != EXPAND_SHELLCMD |
| 4313 | && context != EXPAND_DIRECTORIES) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4314 | { |
| 4315 | /* |
| 4316 | * Matching will be done internally (on something other than files). |
| 4317 | * So we convert the file-matching-type wildcards into our kind for |
| 4318 | * use with vim_regcomp(). First work out how long it will be: |
| 4319 | */ |
| 4320 | |
| 4321 | /* For help tags the translation is done in find_help_tags(). |
| 4322 | * For a tag pattern starting with "/" no translation is needed. */ |
| 4323 | if (context == EXPAND_HELP |
| 4324 | || context == EXPAND_COLORS |
| 4325 | || context == EXPAND_COMPILER |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4326 | || context == EXPAND_OWNSYNTAX |
Bram Moolenaar | 883f5d0 | 2010-06-21 06:24:34 +0200 | [diff] [blame] | 4327 | || context == EXPAND_FILETYPE |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4328 | || context == EXPAND_PACKADD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4329 | || (context == EXPAND_TAGS && fname[0] == '/')) |
| 4330 | retval = vim_strnsave(fname, len); |
| 4331 | else |
| 4332 | { |
| 4333 | new_len = len + 2; /* +2 for '^' at start, NUL at end */ |
| 4334 | for (i = 0; i < len; i++) |
| 4335 | { |
| 4336 | if (fname[i] == '*' || fname[i] == '~') |
| 4337 | new_len++; /* '*' needs to be replaced by ".*" |
| 4338 | '~' needs to be replaced by "\~" */ |
| 4339 | |
| 4340 | /* Buffer names are like file names. "." should be literal */ |
| 4341 | if (context == EXPAND_BUFFERS && fname[i] == '.') |
| 4342 | new_len++; /* "." becomes "\." */ |
| 4343 | |
| 4344 | /* Custom expansion takes care of special things, match |
| 4345 | * backslashes literally (perhaps also for other types?) */ |
Bram Moolenaar | b71eaae | 2006-01-20 23:10:18 +0000 | [diff] [blame] | 4346 | if ((context == EXPAND_USER_DEFINED |
| 4347 | || context == EXPAND_USER_LIST) && fname[i] == '\\') |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4348 | new_len++; /* '\' becomes "\\" */ |
| 4349 | } |
| 4350 | retval = alloc(new_len); |
| 4351 | if (retval != NULL) |
| 4352 | { |
| 4353 | retval[0] = '^'; |
| 4354 | j = 1; |
| 4355 | for (i = 0; i < len; i++, j++) |
| 4356 | { |
| 4357 | /* Skip backslash. But why? At least keep it for custom |
| 4358 | * expansion. */ |
| 4359 | if (context != EXPAND_USER_DEFINED |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4360 | && context != EXPAND_USER_LIST |
| 4361 | && fname[i] == '\\' |
| 4362 | && ++i == len) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4363 | break; |
| 4364 | |
| 4365 | switch (fname[i]) |
| 4366 | { |
| 4367 | case '*': retval[j++] = '.'; |
| 4368 | break; |
| 4369 | case '~': retval[j++] = '\\'; |
| 4370 | break; |
| 4371 | case '?': retval[j] = '.'; |
| 4372 | continue; |
| 4373 | case '.': if (context == EXPAND_BUFFERS) |
| 4374 | retval[j++] = '\\'; |
| 4375 | break; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4376 | case '\\': if (context == EXPAND_USER_DEFINED |
| 4377 | || context == EXPAND_USER_LIST) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4378 | retval[j++] = '\\'; |
| 4379 | break; |
| 4380 | } |
| 4381 | retval[j] = fname[i]; |
| 4382 | } |
| 4383 | retval[j] = NUL; |
| 4384 | } |
| 4385 | } |
| 4386 | } |
| 4387 | else |
| 4388 | { |
| 4389 | retval = alloc(len + 4); |
| 4390 | if (retval != NULL) |
| 4391 | { |
Bram Moolenaar | ce0842a | 2005-07-18 21:58:11 +0000 | [diff] [blame] | 4392 | vim_strncpy(retval, fname, len); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4393 | |
| 4394 | /* |
Bram Moolenaar | c6249bb | 2006-04-15 20:25:09 +0000 | [diff] [blame] | 4395 | * Don't add a star to *, ~, ~user, $var or `cmd`. |
| 4396 | * * would become **, which walks the whole tree. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4397 | * ~ would be at the start of the file name, but not the tail. |
| 4398 | * $ could be anywhere in the tail. |
| 4399 | * ` could be anywhere in the file name. |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4400 | * When the name ends in '$' don't add a star, remove the '$'. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4401 | */ |
| 4402 | tail = gettail(retval); |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4403 | ends_in_star = (len > 0 && retval[len - 1] == '*'); |
| 4404 | #ifndef BACKSLASH_IN_FILENAME |
| 4405 | for (i = len - 2; i >= 0; --i) |
| 4406 | { |
| 4407 | if (retval[i] != '\\') |
| 4408 | break; |
| 4409 | ends_in_star = !ends_in_star; |
| 4410 | } |
| 4411 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4412 | if ((*retval != '~' || tail != retval) |
Bram Moolenaar | 8cd213c | 2010-06-01 21:57:09 +0200 | [diff] [blame] | 4413 | && !ends_in_star |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4414 | && vim_strchr(tail, '$') == NULL |
| 4415 | && vim_strchr(retval, '`') == NULL) |
| 4416 | retval[len++] = '*'; |
Bram Moolenaar | 066b622 | 2008-01-04 14:17:47 +0000 | [diff] [blame] | 4417 | else if (len > 0 && retval[len - 1] == '$') |
| 4418 | --len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4419 | retval[len] = NUL; |
| 4420 | } |
| 4421 | } |
| 4422 | return retval; |
| 4423 | } |
| 4424 | |
| 4425 | /* |
| 4426 | * Must parse the command line so far to work out what context we are in. |
| 4427 | * Completion can then be done based on that context. |
| 4428 | * This routine sets the variables: |
| 4429 | * xp->xp_pattern The start of the pattern to be expanded within |
| 4430 | * the command line (ends at the cursor). |
| 4431 | * xp->xp_context The type of thing to expand. Will be one of: |
| 4432 | * |
| 4433 | * EXPAND_UNSUCCESSFUL Used sometimes when there is something illegal on |
| 4434 | * the command line, like an unknown command. Caller |
| 4435 | * should beep. |
| 4436 | * EXPAND_NOTHING Unrecognised context for completion, use char like |
| 4437 | * a normal char, rather than for completion. eg |
| 4438 | * :s/^I/ |
| 4439 | * EXPAND_COMMANDS Cursor is still touching the command, so complete |
| 4440 | * it. |
| 4441 | * EXPAND_BUFFERS Complete file names for :buf and :sbuf commands. |
| 4442 | * EXPAND_FILES After command with XFILE set, or after setting |
| 4443 | * with P_EXPAND set. eg :e ^I, :w>>^I |
| 4444 | * EXPAND_DIRECTORIES In some cases this is used instead of the latter |
| 4445 | * when we know only directories are of interest. eg |
| 4446 | * :set dir=^I |
Bram Moolenaar | 362e1a3 | 2006-03-06 23:29:24 +0000 | [diff] [blame] | 4447 | * EXPAND_SHELLCMD After ":!cmd", ":r !cmd" or ":w !cmd". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4448 | * EXPAND_SETTINGS Complete variable names. eg :set d^I |
| 4449 | * EXPAND_BOOL_SETTINGS Complete boolean variables only, eg :set no^I |
| 4450 | * EXPAND_TAGS Complete tags from the files in p_tags. eg :ta a^I |
| 4451 | * EXPAND_TAGS_LISTFILES As above, but list filenames on ^D, after :tselect |
| 4452 | * EXPAND_HELP Complete tags from the file 'helpfile'/tags |
| 4453 | * EXPAND_EVENTS Complete event names |
| 4454 | * EXPAND_SYNTAX Complete :syntax command arguments |
| 4455 | * EXPAND_HIGHLIGHT Complete highlight (syntax) group names |
| 4456 | * EXPAND_AUGROUP Complete autocommand group names |
| 4457 | * EXPAND_USER_VARS Complete user defined variable names, eg :unlet a^I |
| 4458 | * EXPAND_MAPPINGS Complete mapping and abbreviation names, |
| 4459 | * eg :unmap a^I , :cunab x^I |
| 4460 | * EXPAND_FUNCTIONS Complete internal or user defined function names, |
| 4461 | * eg :call sub^I |
| 4462 | * EXPAND_USER_FUNC Complete user defined function names, eg :delf F^I |
| 4463 | * EXPAND_EXPRESSION Complete internal or user defined function/variable |
| 4464 | * names in expressions, eg :while s^I |
| 4465 | * EXPAND_ENV_VARS Complete environment variable names |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4466 | * EXPAND_USER Complete user names |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4467 | */ |
| 4468 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4469 | set_expand_context(expand_T *xp) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4470 | { |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4471 | /* only expansion for ':', '>' and '=' command-lines */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4472 | if (ccline.cmdfirstc != ':' |
| 4473 | #ifdef FEAT_EVAL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4474 | && ccline.cmdfirstc != '>' && ccline.cmdfirstc != '=' |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4475 | && !ccline.input_fn |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4476 | #endif |
| 4477 | ) |
| 4478 | { |
| 4479 | xp->xp_context = EXPAND_NOTHING; |
| 4480 | return; |
| 4481 | } |
| 4482 | set_cmd_context(xp, ccline.cmdbuff, ccline.cmdlen, ccline.cmdpos); |
| 4483 | } |
| 4484 | |
| 4485 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4486 | set_cmd_context( |
| 4487 | expand_T *xp, |
| 4488 | char_u *str, /* start of command line */ |
| 4489 | int len, /* length of command line (excl. NUL) */ |
| 4490 | int col) /* position of cursor */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4491 | { |
| 4492 | int old_char = NUL; |
| 4493 | char_u *nextcomm; |
| 4494 | |
| 4495 | /* |
| 4496 | * Avoid a UMR warning from Purify, only save the character if it has been |
| 4497 | * written before. |
| 4498 | */ |
| 4499 | if (col < len) |
| 4500 | old_char = str[col]; |
| 4501 | str[col] = NUL; |
| 4502 | nextcomm = str; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4503 | |
| 4504 | #ifdef FEAT_EVAL |
| 4505 | if (ccline.cmdfirstc == '=') |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4506 | { |
| 4507 | # ifdef FEAT_CMDL_COMPL |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4508 | /* pass CMD_SIZE because there is no real command */ |
| 4509 | set_context_for_expression(xp, str, CMD_SIZE); |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4510 | # endif |
| 4511 | } |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4512 | else if (ccline.input_fn) |
| 4513 | { |
| 4514 | xp->xp_context = ccline.xp_context; |
| 4515 | xp->xp_pattern = ccline.cmdbuff; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4516 | # if defined(FEAT_USR_CMDS) && defined(FEAT_CMDL_COMPL) |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4517 | xp->xp_arg = ccline.xp_arg; |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4518 | # endif |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 4519 | } |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 4520 | else |
| 4521 | #endif |
| 4522 | while (nextcomm != NULL) |
| 4523 | nextcomm = set_one_cmd_context(xp, nextcomm); |
| 4524 | |
Bram Moolenaar | a4c8dcb | 2013-06-30 12:21:24 +0200 | [diff] [blame] | 4525 | /* Store the string here so that call_user_expand_func() can get to them |
| 4526 | * easily. */ |
| 4527 | xp->xp_line = str; |
| 4528 | xp->xp_col = col; |
| 4529 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4530 | str[col] = old_char; |
| 4531 | } |
| 4532 | |
| 4533 | /* |
| 4534 | * Expand the command line "str" from context "xp". |
| 4535 | * "xp" must have been set by set_cmd_context(). |
| 4536 | * xp->xp_pattern points into "str", to where the text that is to be expanded |
| 4537 | * starts. |
| 4538 | * Returns EXPAND_UNSUCCESSFUL when there is something illegal before the |
| 4539 | * cursor. |
| 4540 | * Returns EXPAND_NOTHING when there is nothing to expand, might insert the |
| 4541 | * key that triggered expansion literally. |
| 4542 | * Returns EXPAND_OK otherwise. |
| 4543 | */ |
| 4544 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4545 | expand_cmdline( |
| 4546 | expand_T *xp, |
| 4547 | char_u *str, /* start of command line */ |
| 4548 | int col, /* position of cursor */ |
| 4549 | int *matchcount, /* return: nr of matches */ |
| 4550 | char_u ***matches) /* return: array of pointers to matches */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4551 | { |
| 4552 | char_u *file_str = NULL; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4553 | int options = WILD_ADD_SLASH|WILD_SILENT; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4554 | |
| 4555 | if (xp->xp_context == EXPAND_UNSUCCESSFUL) |
| 4556 | { |
| 4557 | beep_flush(); |
| 4558 | return EXPAND_UNSUCCESSFUL; /* Something illegal on command line */ |
| 4559 | } |
| 4560 | if (xp->xp_context == EXPAND_NOTHING) |
| 4561 | { |
| 4562 | /* Caller can use the character as a normal char instead */ |
| 4563 | return EXPAND_NOTHING; |
| 4564 | } |
| 4565 | |
| 4566 | /* 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] | 4567 | xp->xp_pattern_len = (int)(str + col - xp->xp_pattern); |
| 4568 | 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] | 4569 | if (file_str == NULL) |
| 4570 | return EXPAND_UNSUCCESSFUL; |
| 4571 | |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4572 | if (p_wic) |
| 4573 | options += WILD_ICASE; |
| 4574 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4575 | /* find all files that match the description */ |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4576 | if (ExpandFromContext(xp, file_str, matchcount, matches, options) == FAIL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4577 | { |
| 4578 | *matchcount = 0; |
| 4579 | *matches = NULL; |
| 4580 | } |
| 4581 | vim_free(file_str); |
| 4582 | |
| 4583 | return EXPAND_OK; |
| 4584 | } |
| 4585 | |
| 4586 | #ifdef FEAT_MULTI_LANG |
| 4587 | /* |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4588 | * Cleanup matches for help tags: |
| 4589 | * Remove "@ab" if the top of 'helplang' is "ab" and the language of the first |
| 4590 | * tag matches it. Otherwise remove "@en" if "en" is the only language. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4591 | */ |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 4592 | static void cleanup_help_tags(int num_file, char_u **file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4593 | |
| 4594 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4595 | cleanup_help_tags(int num_file, char_u **file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4596 | { |
| 4597 | int i, j; |
| 4598 | int len; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4599 | char_u buf[4]; |
| 4600 | char_u *p = buf; |
| 4601 | |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4602 | 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] | 4603 | { |
| 4604 | *p++ = '@'; |
| 4605 | *p++ = p_hlg[0]; |
| 4606 | *p++ = p_hlg[1]; |
| 4607 | } |
| 4608 | *p = NUL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4609 | |
| 4610 | for (i = 0; i < num_file; ++i) |
| 4611 | { |
| 4612 | len = (int)STRLEN(file[i]) - 3; |
Bram Moolenaar | 61264d9 | 2016-03-28 19:59:02 +0200 | [diff] [blame] | 4613 | if (len <= 0) |
| 4614 | continue; |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4615 | if (STRCMP(file[i] + len, "@en") == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4616 | { |
| 4617 | /* Sorting on priority means the same item in another language may |
| 4618 | * be anywhere. Search all items for a match up to the "@en". */ |
| 4619 | for (j = 0; j < num_file; ++j) |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4620 | if (j != i && (int)STRLEN(file[j]) == len + 3 |
| 4621 | && STRNCMP(file[i], file[j], len + 1) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4622 | break; |
| 4623 | if (j == num_file) |
Bram Moolenaar | 89c79b9 | 2016-05-05 17:18:41 +0200 | [diff] [blame] | 4624 | /* item only exists with @en, remove it */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4625 | file[i][len] = NUL; |
| 4626 | } |
| 4627 | } |
Bram Moolenaar | 9ccaae0 | 2016-05-07 18:36:48 +0200 | [diff] [blame] | 4628 | |
| 4629 | if (*buf != NUL) |
| 4630 | for (i = 0; i < num_file; ++i) |
| 4631 | { |
| 4632 | len = (int)STRLEN(file[i]) - 3; |
| 4633 | if (len <= 0) |
| 4634 | continue; |
| 4635 | if (STRCMP(file[i] + len, buf) == 0) |
| 4636 | { |
| 4637 | /* remove the default language */ |
| 4638 | file[i][len] = NUL; |
| 4639 | } |
| 4640 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4641 | } |
| 4642 | #endif |
| 4643 | |
| 4644 | /* |
| 4645 | * Do the expansion based on xp->xp_context and "pat". |
| 4646 | */ |
| 4647 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4648 | ExpandFromContext( |
| 4649 | expand_T *xp, |
| 4650 | char_u *pat, |
| 4651 | int *num_file, |
| 4652 | char_u ***file, |
| 4653 | int options) /* EW_ flags */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4654 | { |
| 4655 | #ifdef FEAT_CMDL_COMPL |
| 4656 | regmatch_T regmatch; |
| 4657 | #endif |
| 4658 | int ret; |
| 4659 | int flags; |
| 4660 | |
| 4661 | flags = EW_DIR; /* include directories */ |
| 4662 | if (options & WILD_LIST_NOTFOUND) |
| 4663 | flags |= EW_NOTFOUND; |
| 4664 | if (options & WILD_ADD_SLASH) |
| 4665 | flags |= EW_ADDSLASH; |
| 4666 | if (options & WILD_KEEP_ALL) |
| 4667 | flags |= EW_KEEPALL; |
| 4668 | if (options & WILD_SILENT) |
| 4669 | flags |= EW_SILENT; |
Bram Moolenaar | a245bc7 | 2015-03-05 19:35:25 +0100 | [diff] [blame] | 4670 | if (options & WILD_ALLLINKS) |
| 4671 | flags |= EW_ALLLINKS; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4672 | |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4673 | if (xp->xp_context == EXPAND_FILES |
| 4674 | || xp->xp_context == EXPAND_DIRECTORIES |
| 4675 | || xp->xp_context == EXPAND_FILES_IN_PATH) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4676 | { |
| 4677 | /* |
| 4678 | * Expand file or directory names. |
| 4679 | */ |
| 4680 | int free_pat = FALSE; |
| 4681 | int i; |
| 4682 | |
| 4683 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 4684 | * space */ |
| 4685 | if (xp->xp_backslash != XP_BS_NONE) |
| 4686 | { |
| 4687 | free_pat = TRUE; |
| 4688 | pat = vim_strsave(pat); |
| 4689 | for (i = 0; pat[i]; ++i) |
| 4690 | if (pat[i] == '\\') |
| 4691 | { |
| 4692 | if (xp->xp_backslash == XP_BS_THREE |
| 4693 | && pat[i + 1] == '\\' |
| 4694 | && pat[i + 2] == '\\' |
| 4695 | && pat[i + 3] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4696 | STRMOVE(pat + i, pat + i + 3); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4697 | if (xp->xp_backslash == XP_BS_ONE |
| 4698 | && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 4699 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4700 | } |
| 4701 | } |
| 4702 | |
| 4703 | if (xp->xp_context == EXPAND_FILES) |
| 4704 | flags |= EW_FILE; |
Bram Moolenaar | cc448b3 | 2010-07-14 16:52:17 +0200 | [diff] [blame] | 4705 | else if (xp->xp_context == EXPAND_FILES_IN_PATH) |
| 4706 | flags |= (EW_FILE | EW_PATH); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4707 | else |
| 4708 | flags = (flags | EW_DIR) & ~EW_FILE; |
Bram Moolenaar | 94950a9 | 2010-12-02 16:01:29 +0100 | [diff] [blame] | 4709 | if (options & WILD_ICASE) |
| 4710 | flags |= EW_ICASE; |
| 4711 | |
Bram Moolenaar | d7834d3 | 2009-12-02 16:14:36 +0000 | [diff] [blame] | 4712 | /* Expand wildcards, supporting %:h and the like. */ |
| 4713 | ret = expand_wildcards_eval(&pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4714 | if (free_pat) |
| 4715 | vim_free(pat); |
| 4716 | return ret; |
| 4717 | } |
| 4718 | |
| 4719 | *file = (char_u **)""; |
| 4720 | *num_file = 0; |
| 4721 | if (xp->xp_context == EXPAND_HELP) |
| 4722 | { |
Bram Moolenaar | c62e2fe | 2008-08-06 13:03:07 +0000 | [diff] [blame] | 4723 | /* With an empty argument we would get all the help tags, which is |
| 4724 | * very slow. Get matches for "help" instead. */ |
| 4725 | if (find_help_tags(*pat == NUL ? (char_u *)"help" : pat, |
| 4726 | num_file, file, FALSE) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4727 | { |
| 4728 | #ifdef FEAT_MULTI_LANG |
| 4729 | cleanup_help_tags(*num_file, *file); |
| 4730 | #endif |
| 4731 | return OK; |
| 4732 | } |
| 4733 | return FAIL; |
| 4734 | } |
| 4735 | |
| 4736 | #ifndef FEAT_CMDL_COMPL |
| 4737 | return FAIL; |
| 4738 | #else |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4739 | if (xp->xp_context == EXPAND_SHELLCMD) |
| 4740 | return expand_shellcmd(pat, num_file, file, flags); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4741 | if (xp->xp_context == EXPAND_OLD_SETTING) |
| 4742 | return ExpandOldSetting(num_file, file); |
| 4743 | if (xp->xp_context == EXPAND_BUFFERS) |
| 4744 | return ExpandBufnames(pat, num_file, file, options); |
| 4745 | if (xp->xp_context == EXPAND_TAGS |
| 4746 | || xp->xp_context == EXPAND_TAGS_LISTFILES) |
| 4747 | return expand_tags(xp->xp_context == EXPAND_TAGS, pat, num_file, file); |
| 4748 | if (xp->xp_context == EXPAND_COLORS) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4749 | { |
| 4750 | char *directories[] = {"colors", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4751 | return ExpandRTDir(pat, DIP_START + DIP_OPT, num_file, file, |
| 4752 | directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4753 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4754 | if (xp->xp_context == EXPAND_COMPILER) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4755 | { |
Bram Moolenaar | a627c96 | 2011-09-30 16:23:32 +0200 | [diff] [blame] | 4756 | char *directories[] = {"compiler", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4757 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4758 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4759 | if (xp->xp_context == EXPAND_OWNSYNTAX) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4760 | { |
| 4761 | char *directories[] = {"syntax", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4762 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4763 | } |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 4764 | if (xp->xp_context == EXPAND_FILETYPE) |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4765 | { |
| 4766 | char *directories[] = {"syntax", "indent", "ftplugin", NULL}; |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 4767 | return ExpandRTDir(pat, 0, num_file, file, directories); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 4768 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4769 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4770 | if (xp->xp_context == EXPAND_USER_LIST) |
Bram Moolenaar | c9b4b05 | 2006-04-30 18:54:39 +0000 | [diff] [blame] | 4771 | return ExpandUserList(xp, num_file, file); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 4772 | # endif |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 4773 | if (xp->xp_context == EXPAND_PACKADD) |
| 4774 | return ExpandPackAddDir(pat, num_file, file); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4775 | |
| 4776 | regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0); |
| 4777 | if (regmatch.regprog == NULL) |
| 4778 | return FAIL; |
| 4779 | |
| 4780 | /* set ignore-case according to p_ic, p_scs and pat */ |
| 4781 | regmatch.rm_ic = ignorecase(pat); |
| 4782 | |
| 4783 | if (xp->xp_context == EXPAND_SETTINGS |
| 4784 | || xp->xp_context == EXPAND_BOOL_SETTINGS) |
| 4785 | ret = ExpandSettings(xp, ®match, num_file, file); |
| 4786 | else if (xp->xp_context == EXPAND_MAPPINGS) |
| 4787 | ret = ExpandMappings(®match, num_file, file); |
| 4788 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
| 4789 | else if (xp->xp_context == EXPAND_USER_DEFINED) |
| 4790 | ret = ExpandUserDefined(xp, ®match, num_file, file); |
| 4791 | # endif |
| 4792 | else |
| 4793 | { |
| 4794 | static struct expgen |
| 4795 | { |
| 4796 | int context; |
Bram Moolenaar | d99df42 | 2016-01-29 23:20:40 +0100 | [diff] [blame] | 4797 | char_u *((*func)(expand_T *, int)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4798 | int ic; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4799 | int escaped; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4800 | } tab[] = |
| 4801 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4802 | {EXPAND_COMMANDS, get_command_name, FALSE, TRUE}, |
| 4803 | {EXPAND_BEHAVE, get_behave_arg, TRUE, TRUE}, |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 4804 | #ifdef FEAT_CMDHIST |
| 4805 | {EXPAND_HISTORY, get_history_arg, TRUE, TRUE}, |
| 4806 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4807 | #ifdef FEAT_USR_CMDS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4808 | {EXPAND_USER_COMMANDS, get_user_commands, FALSE, TRUE}, |
Bram Moolenaar | f1d6ccf | 2014-12-08 04:16:44 +0100 | [diff] [blame] | 4809 | {EXPAND_USER_ADDR_TYPE, get_user_cmd_addr_type, FALSE, TRUE}, |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4810 | {EXPAND_USER_CMD_FLAGS, get_user_cmd_flags, FALSE, TRUE}, |
| 4811 | {EXPAND_USER_NARGS, get_user_cmd_nargs, FALSE, TRUE}, |
| 4812 | {EXPAND_USER_COMPLETE, get_user_cmd_complete, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4813 | #endif |
| 4814 | #ifdef FEAT_EVAL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4815 | {EXPAND_USER_VARS, get_user_var_name, FALSE, TRUE}, |
| 4816 | {EXPAND_FUNCTIONS, get_function_name, FALSE, TRUE}, |
| 4817 | {EXPAND_USER_FUNC, get_user_func_name, FALSE, TRUE}, |
| 4818 | {EXPAND_EXPRESSION, get_expr_name, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4819 | #endif |
| 4820 | #ifdef FEAT_MENU |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4821 | {EXPAND_MENUS, get_menu_name, FALSE, TRUE}, |
| 4822 | {EXPAND_MENUNAMES, get_menu_names, FALSE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4823 | #endif |
| 4824 | #ifdef FEAT_SYN_HL |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4825 | {EXPAND_SYNTAX, get_syntax_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4826 | #endif |
Bram Moolenaar | cd9c462 | 2013-06-08 15:24:48 +0200 | [diff] [blame] | 4827 | #ifdef FEAT_PROFILE |
| 4828 | {EXPAND_SYNTIME, get_syntime_arg, TRUE, TRUE}, |
| 4829 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4830 | {EXPAND_HIGHLIGHT, get_highlight_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4831 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4832 | {EXPAND_EVENTS, get_event_name, TRUE, TRUE}, |
| 4833 | {EXPAND_AUGROUP, get_augroup_name, TRUE, TRUE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4834 | #endif |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4835 | #ifdef FEAT_CSCOPE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4836 | {EXPAND_CSCOPE, get_cscope_name, TRUE, TRUE}, |
Bram Moolenaar | f4580d8 | 2009-03-18 11:52:53 +0000 | [diff] [blame] | 4837 | #endif |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4838 | #ifdef FEAT_SIGNS |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4839 | {EXPAND_SIGN, get_sign_name, TRUE, TRUE}, |
Bram Moolenaar | 3c65e31 | 2009-04-29 16:47:23 +0000 | [diff] [blame] | 4840 | #endif |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 4841 | #ifdef FEAT_PROFILE |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4842 | {EXPAND_PROFILE, get_profile_name, TRUE, TRUE}, |
Bram Moolenaar | f86f26c | 2010-02-03 15:14:22 +0100 | [diff] [blame] | 4843 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4844 | #if (defined(HAVE_LOCALE_H) || defined(X_LOCALE)) \ |
| 4845 | && (defined(FEAT_GETTEXT) || defined(FEAT_MBYTE)) |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4846 | {EXPAND_LANGUAGE, get_lang_arg, TRUE, FALSE}, |
| 4847 | {EXPAND_LOCALES, get_locales, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4848 | #endif |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4849 | {EXPAND_ENV_VARS, get_env_name, TRUE, TRUE}, |
Bram Moolenaar | 2430586 | 2012-08-15 14:05:05 +0200 | [diff] [blame] | 4850 | {EXPAND_USER, get_users, TRUE, FALSE}, |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4851 | }; |
| 4852 | int i; |
| 4853 | |
| 4854 | /* |
| 4855 | * Find a context in the table and call the ExpandGeneric() with the |
| 4856 | * right function to do the expansion. |
| 4857 | */ |
| 4858 | ret = FAIL; |
Bram Moolenaar | af0167f | 2009-05-16 15:31:32 +0000 | [diff] [blame] | 4859 | for (i = 0; i < (int)(sizeof(tab) / sizeof(struct expgen)); ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4860 | if (xp->xp_context == tab[i].context) |
| 4861 | { |
| 4862 | if (tab[i].ic) |
| 4863 | regmatch.rm_ic = TRUE; |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4864 | ret = ExpandGeneric(xp, ®match, num_file, file, |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 4865 | tab[i].func, tab[i].escaped); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4866 | break; |
| 4867 | } |
| 4868 | } |
| 4869 | |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 4870 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4871 | |
| 4872 | return ret; |
| 4873 | #endif /* FEAT_CMDL_COMPL */ |
| 4874 | } |
| 4875 | |
| 4876 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 4877 | /* |
| 4878 | * Expand a list of names. |
| 4879 | * |
| 4880 | * Generic function for command line completion. It calls a function to |
| 4881 | * obtain strings, one by one. The strings are matched against a regexp |
| 4882 | * program. Matching strings are copied into an array, which is returned. |
| 4883 | * |
| 4884 | * Returns OK when no problems encountered, FAIL for error (out of memory). |
| 4885 | */ |
| 4886 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4887 | ExpandGeneric( |
| 4888 | expand_T *xp, |
| 4889 | regmatch_T *regmatch, |
| 4890 | int *num_file, |
| 4891 | char_u ***file, |
| 4892 | char_u *((*func)(expand_T *, int)), |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4893 | /* returns a string from the list */ |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4894 | int escaped) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4895 | { |
| 4896 | int i; |
| 4897 | int count = 0; |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4898 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4899 | char_u *str; |
| 4900 | |
| 4901 | /* do this loop twice: |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4902 | * round == 0: count the number of matching names |
| 4903 | * round == 1: copy the matching names into allocated memory |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4904 | */ |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4905 | for (round = 0; round <= 1; ++round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4906 | { |
| 4907 | for (i = 0; ; ++i) |
| 4908 | { |
| 4909 | str = (*func)(xp, i); |
| 4910 | if (str == NULL) /* end of list */ |
| 4911 | break; |
| 4912 | if (*str == NUL) /* skip empty strings */ |
| 4913 | continue; |
| 4914 | |
| 4915 | if (vim_regexec(regmatch, str, (colnr_T)0)) |
| 4916 | { |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4917 | if (round) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4918 | { |
Bram Moolenaar | 9b486ca | 2011-05-19 18:26:40 +0200 | [diff] [blame] | 4919 | if (escaped) |
| 4920 | str = vim_strsave_escaped(str, (char_u *)" \t\\."); |
| 4921 | else |
| 4922 | str = vim_strsave(str); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4923 | (*file)[count] = str; |
| 4924 | #ifdef FEAT_MENU |
| 4925 | if (func == get_menu_names && str != NULL) |
| 4926 | { |
| 4927 | /* test for separator added by get_menu_names() */ |
| 4928 | str += STRLEN(str) - 1; |
| 4929 | if (*str == '\001') |
| 4930 | *str = '.'; |
| 4931 | } |
| 4932 | #endif |
| 4933 | } |
| 4934 | ++count; |
| 4935 | } |
| 4936 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4937 | if (round == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4938 | { |
| 4939 | if (count == 0) |
| 4940 | return OK; |
| 4941 | *num_file = count; |
| 4942 | *file = (char_u **)alloc((unsigned)(count * sizeof(char_u *))); |
| 4943 | if (*file == NULL) |
| 4944 | { |
| 4945 | *file = (char_u **)""; |
| 4946 | return FAIL; |
| 4947 | } |
| 4948 | count = 0; |
| 4949 | } |
| 4950 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4951 | |
Bram Moolenaar | 7fc904b | 2006-04-13 20:37:35 +0000 | [diff] [blame] | 4952 | /* Sort the results. Keep menu's in the specified order. */ |
| 4953 | if (xp->xp_context != EXPAND_MENUNAMES && xp->xp_context != EXPAND_MENUS) |
Bram Moolenaar | db710ed | 2011-10-26 22:02:15 +0200 | [diff] [blame] | 4954 | { |
| 4955 | if (xp->xp_context == EXPAND_EXPRESSION |
| 4956 | || xp->xp_context == EXPAND_FUNCTIONS |
| 4957 | || xp->xp_context == EXPAND_USER_FUNC) |
| 4958 | /* <SNR> functions should be sorted to the end. */ |
| 4959 | qsort((void *)*file, (size_t)*num_file, sizeof(char_u *), |
| 4960 | sort_func_compare); |
| 4961 | else |
| 4962 | sort_strings(*file, *num_file); |
| 4963 | } |
Bram Moolenaar | 90cfdbe | 2005-08-12 19:59:19 +0000 | [diff] [blame] | 4964 | |
Bram Moolenaar | 4f68858 | 2007-07-24 12:34:30 +0000 | [diff] [blame] | 4965 | #ifdef FEAT_CMDL_COMPL |
| 4966 | /* Reset the variables used for special highlight names expansion, so that |
| 4967 | * they don't show up when getting normal highlight names by ID. */ |
| 4968 | reset_expand_highlight(); |
| 4969 | #endif |
| 4970 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 4971 | return OK; |
| 4972 | } |
| 4973 | |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4974 | /* |
| 4975 | * Complete a shell command. |
| 4976 | * Returns FAIL or OK; |
| 4977 | */ |
| 4978 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 4979 | expand_shellcmd( |
| 4980 | char_u *filepat, /* pattern to match with command names */ |
| 4981 | int *num_file, /* return: number of matches */ |
| 4982 | char_u ***file, /* return: array with matches */ |
| 4983 | int flagsarg) /* EW_ flags */ |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4984 | { |
| 4985 | char_u *pat; |
| 4986 | int i; |
| 4987 | char_u *path; |
| 4988 | int mustfree = FALSE; |
| 4989 | garray_T ga; |
| 4990 | char_u *buf = alloc(MAXPATHL); |
| 4991 | size_t l; |
| 4992 | char_u *s, *e; |
| 4993 | int flags = flagsarg; |
| 4994 | int ret; |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 4995 | int did_curdir = FALSE; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 4996 | |
| 4997 | if (buf == NULL) |
| 4998 | return FAIL; |
| 4999 | |
| 5000 | /* for ":set path=" and ":set tags=" halve backslashes for escaped |
| 5001 | * space */ |
| 5002 | pat = vim_strsave(filepat); |
| 5003 | for (i = 0; pat[i]; ++i) |
| 5004 | if (pat[i] == '\\' && pat[i + 1] == ' ') |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5005 | STRMOVE(pat + i, pat + i + 1); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5006 | |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5007 | flags |= EW_FILE | EW_EXEC | EW_SHELLCMD; |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5008 | |
| 5009 | /* For an absolute name we don't use $PATH. */ |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 5010 | if (mch_isFullName(pat)) |
| 5011 | path = (char_u *)" "; |
| 5012 | else if ((pat[0] == '.' && (vim_ispathsep(pat[1]) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5013 | || (pat[1] == '.' && vim_ispathsep(pat[2]))))) |
| 5014 | path = (char_u *)"."; |
| 5015 | else |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5016 | { |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5017 | path = vim_getenv((char_u *)"PATH", &mustfree); |
Bram Moolenaar | 27d9ece | 2010-11-10 15:37:05 +0100 | [diff] [blame] | 5018 | if (path == NULL) |
| 5019 | path = (char_u *)""; |
| 5020 | } |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5021 | |
| 5022 | /* |
| 5023 | * Go over all directories in $PATH. Expand matches in that directory and |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5024 | * collect them in "ga". When "." is not in $PATH also expand for the |
| 5025 | * current directory, to find "subdir/cmd". |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5026 | */ |
| 5027 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5028 | for (s = path; ; s = e) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5029 | { |
Bram Moolenaar | b597114 | 2015-03-21 17:32:19 +0100 | [diff] [blame] | 5030 | if (*s == NUL) |
| 5031 | { |
| 5032 | if (did_curdir) |
| 5033 | break; |
| 5034 | /* Find directories in the current directory, path is empty. */ |
| 5035 | did_curdir = TRUE; |
| 5036 | } |
| 5037 | else if (*s == '.') |
| 5038 | did_curdir = TRUE; |
| 5039 | |
Bram Moolenaar | 68c3174 | 2006-09-02 15:54:18 +0000 | [diff] [blame] | 5040 | if (*s == ' ') |
| 5041 | ++s; /* Skip space used for absolute path name. */ |
| 5042 | |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5043 | #if defined(MSWIN) |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5044 | e = vim_strchr(s, ';'); |
| 5045 | #else |
| 5046 | e = vim_strchr(s, ':'); |
| 5047 | #endif |
| 5048 | if (e == NULL) |
| 5049 | e = s + STRLEN(s); |
| 5050 | |
| 5051 | l = e - s; |
| 5052 | if (l > MAXPATHL - 5) |
| 5053 | break; |
| 5054 | vim_strncpy(buf, s, l); |
| 5055 | add_pathsep(buf); |
| 5056 | l = STRLEN(buf); |
| 5057 | vim_strncpy(buf + l, pat, MAXPATHL - 1 - l); |
| 5058 | |
| 5059 | /* Expand matches in one directory of $PATH. */ |
| 5060 | ret = expand_wildcards(1, &buf, num_file, file, flags); |
| 5061 | if (ret == OK) |
| 5062 | { |
| 5063 | if (ga_grow(&ga, *num_file) == FAIL) |
| 5064 | FreeWild(*num_file, *file); |
| 5065 | else |
| 5066 | { |
| 5067 | for (i = 0; i < *num_file; ++i) |
| 5068 | { |
| 5069 | s = (*file)[i]; |
| 5070 | if (STRLEN(s) > l) |
| 5071 | { |
| 5072 | /* Remove the path again. */ |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5073 | STRMOVE(s, s + l); |
Bram Moolenaar | 1f35bf9 | 2006-03-07 22:38:47 +0000 | [diff] [blame] | 5074 | ((char_u **)ga.ga_data)[ga.ga_len++] = s; |
| 5075 | } |
| 5076 | else |
| 5077 | vim_free(s); |
| 5078 | } |
| 5079 | vim_free(*file); |
| 5080 | } |
| 5081 | } |
| 5082 | if (*e != NUL) |
| 5083 | ++e; |
| 5084 | } |
| 5085 | *file = ga.ga_data; |
| 5086 | *num_file = ga.ga_len; |
| 5087 | |
| 5088 | vim_free(buf); |
| 5089 | vim_free(pat); |
| 5090 | if (mustfree) |
| 5091 | vim_free(path); |
| 5092 | return OK; |
| 5093 | } |
| 5094 | |
| 5095 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5096 | # if defined(FEAT_USR_CMDS) && defined(FEAT_EVAL) |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 5097 | 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] | 5098 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5099 | /* |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5100 | * Call "user_expand_func()" to invoke a user defined VimL function and return |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5101 | * the result (either a string or a List). |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5102 | */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5103 | static void * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5104 | call_user_expand_func( |
| 5105 | void *(*user_expand_func)(char_u *, int, char_u **, int), |
| 5106 | expand_T *xp, |
| 5107 | int *num_file, |
| 5108 | char_u ***file) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5109 | { |
Bram Moolenaar | 673b9a3 | 2013-06-30 22:43:27 +0200 | [diff] [blame] | 5110 | int keep = 0; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5111 | char_u num[50]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5112 | char_u *args[3]; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5113 | int save_current_SID = current_SID; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5114 | void *ret; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5115 | struct cmdline_info save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5116 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5117 | 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] | 5118 | return NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5119 | *num_file = 0; |
| 5120 | *file = NULL; |
| 5121 | |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5122 | if (ccline.cmdbuff != NULL) |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5123 | { |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5124 | keep = ccline.cmdbuff[ccline.cmdlen]; |
| 5125 | ccline.cmdbuff[ccline.cmdlen] = 0; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5126 | } |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5127 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5128 | args[0] = vim_strnsave(xp->xp_pattern, xp->xp_pattern_len); |
Bram Moolenaar | b751546 | 2013-06-29 12:58:33 +0200 | [diff] [blame] | 5129 | args[1] = xp->xp_line; |
| 5130 | sprintf((char *)num, "%d", xp->xp_col); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5131 | args[2] = num; |
| 5132 | |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5133 | /* Save the cmdline, we don't know what the function may do. */ |
| 5134 | save_ccline = ccline; |
| 5135 | ccline.cmdbuff = NULL; |
| 5136 | ccline.cmdprompt = NULL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5137 | current_SID = xp->xp_scriptID; |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5138 | |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5139 | ret = user_expand_func(xp->xp_arg, 3, args, FALSE); |
Bram Moolenaar | 592e0a2 | 2004-07-03 16:05:59 +0000 | [diff] [blame] | 5140 | |
| 5141 | ccline = save_ccline; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5142 | current_SID = save_current_SID; |
Bram Moolenaar | 21669c0 | 2008-01-18 12:16:16 +0000 | [diff] [blame] | 5143 | if (ccline.cmdbuff != NULL) |
| 5144 | ccline.cmdbuff[ccline.cmdlen] = keep; |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5145 | |
Bram Moolenaar | 67b891e | 2009-09-18 15:25:52 +0000 | [diff] [blame] | 5146 | vim_free(args[0]); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5147 | return ret; |
| 5148 | } |
| 5149 | |
| 5150 | /* |
| 5151 | * Expand names with a function defined by the user. |
| 5152 | */ |
| 5153 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5154 | ExpandUserDefined( |
| 5155 | expand_T *xp, |
| 5156 | regmatch_T *regmatch, |
| 5157 | int *num_file, |
| 5158 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5159 | { |
| 5160 | char_u *retstr; |
| 5161 | char_u *s; |
| 5162 | char_u *e; |
| 5163 | char_u keep; |
| 5164 | garray_T ga; |
| 5165 | |
| 5166 | retstr = call_user_expand_func(call_func_retstr, xp, num_file, file); |
| 5167 | if (retstr == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5168 | return FAIL; |
| 5169 | |
| 5170 | ga_init2(&ga, (int)sizeof(char *), 3); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5171 | for (s = retstr; *s != NUL; s = e) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5172 | { |
| 5173 | e = vim_strchr(s, '\n'); |
| 5174 | if (e == NULL) |
| 5175 | e = s + STRLEN(s); |
| 5176 | keep = *e; |
| 5177 | *e = 0; |
| 5178 | |
| 5179 | if (xp->xp_pattern[0] && vim_regexec(regmatch, s, (colnr_T)0) == 0) |
| 5180 | { |
| 5181 | *e = keep; |
| 5182 | if (*e != NUL) |
| 5183 | ++e; |
| 5184 | continue; |
| 5185 | } |
| 5186 | |
| 5187 | if (ga_grow(&ga, 1) == FAIL) |
| 5188 | break; |
| 5189 | |
| 5190 | ((char_u **)ga.ga_data)[ga.ga_len] = vim_strnsave(s, (int)(e - s)); |
| 5191 | ++ga.ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5192 | |
| 5193 | *e = keep; |
| 5194 | if (*e != NUL) |
| 5195 | ++e; |
| 5196 | } |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5197 | vim_free(retstr); |
| 5198 | *file = ga.ga_data; |
| 5199 | *num_file = ga.ga_len; |
| 5200 | return OK; |
| 5201 | } |
| 5202 | |
| 5203 | /* |
| 5204 | * Expand names with a list returned by a function defined by the user. |
| 5205 | */ |
| 5206 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5207 | ExpandUserList( |
| 5208 | expand_T *xp, |
| 5209 | int *num_file, |
| 5210 | char_u ***file) |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5211 | { |
| 5212 | list_T *retlist; |
| 5213 | listitem_T *li; |
| 5214 | garray_T ga; |
| 5215 | |
| 5216 | retlist = call_user_expand_func(call_func_retlist, xp, num_file, file); |
| 5217 | if (retlist == NULL) |
| 5218 | return FAIL; |
| 5219 | |
| 5220 | ga_init2(&ga, (int)sizeof(char *), 3); |
| 5221 | /* Loop over the items in the list. */ |
| 5222 | for (li = retlist->lv_first; li != NULL; li = li->li_next) |
| 5223 | { |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5224 | if (li->li_tv.v_type != VAR_STRING || li->li_tv.vval.v_string == NULL) |
| 5225 | continue; /* Skip non-string items and empty strings */ |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5226 | |
| 5227 | if (ga_grow(&ga, 1) == FAIL) |
| 5228 | break; |
| 5229 | |
| 5230 | ((char_u **)ga.ga_data)[ga.ga_len] = |
Bram Moolenaar | 8d3b8c4 | 2009-06-24 15:05:00 +0000 | [diff] [blame] | 5231 | vim_strsave(li->li_tv.vval.v_string); |
Bram Moolenaar | 35fdbb5 | 2005-07-09 21:08:57 +0000 | [diff] [blame] | 5232 | ++ga.ga_len; |
| 5233 | } |
| 5234 | list_unref(retlist); |
| 5235 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5236 | *file = ga.ga_data; |
| 5237 | *num_file = ga.ga_len; |
| 5238 | return OK; |
| 5239 | } |
| 5240 | #endif |
| 5241 | |
| 5242 | /* |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5243 | * Expand color scheme, compiler or filetype names. |
| 5244 | * Search from 'runtimepath': |
| 5245 | * 'runtimepath'/{dirnames}/{pat}.vim |
| 5246 | * When "flags" has DIP_START: search also from 'start' of 'packpath': |
| 5247 | * 'packpath'/pack/ * /start/ * /{dirnames}/{pat}.vim |
| 5248 | * When "flags" has DIP_OPT: search also from 'opt' of 'packpath': |
| 5249 | * 'packpath'/pack/ * /opt/ * /{dirnames}/{pat}.vim |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5250 | * "dirnames" is an array with one or more directory names. |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5251 | */ |
| 5252 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5253 | ExpandRTDir( |
| 5254 | char_u *pat, |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5255 | int flags, |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5256 | int *num_file, |
| 5257 | char_u ***file, |
| 5258 | char *dirnames[]) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5259 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5260 | char_u *s; |
| 5261 | char_u *e; |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5262 | char_u *match; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5263 | garray_T ga; |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5264 | int i; |
| 5265 | int pat_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5266 | |
| 5267 | *num_file = 0; |
| 5268 | *file = NULL; |
Bram Moolenaar | 5cfe2d7 | 2011-07-07 15:04:52 +0200 | [diff] [blame] | 5269 | pat_len = (int)STRLEN(pat); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5270 | ga_init2(&ga, (int)sizeof(char *), 10); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5271 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5272 | for (i = 0; dirnames[i] != NULL; ++i) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5273 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5274 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 7)); |
| 5275 | if (s == NULL) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5276 | { |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5277 | ga_clear_strings(&ga); |
| 5278 | return FAIL; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5279 | } |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5280 | sprintf((char *)s, "%s/%s*.vim", dirnames[i], pat); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5281 | globpath(p_rtp, s, &ga, 0); |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5282 | vim_free(s); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5283 | } |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5284 | |
Bram Moolenaar | 52f9c19 | 2016-03-13 13:24:45 +0100 | [diff] [blame] | 5285 | if (flags & DIP_START) { |
| 5286 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5287 | { |
| 5288 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 22)); |
| 5289 | if (s == NULL) |
| 5290 | { |
| 5291 | ga_clear_strings(&ga); |
| 5292 | return FAIL; |
| 5293 | } |
| 5294 | sprintf((char *)s, "pack/*/start/*/%s/%s*.vim", dirnames[i], pat); |
| 5295 | globpath(p_pp, s, &ga, 0); |
| 5296 | vim_free(s); |
| 5297 | } |
| 5298 | } |
| 5299 | |
| 5300 | if (flags & DIP_OPT) { |
| 5301 | for (i = 0; dirnames[i] != NULL; ++i) |
| 5302 | { |
| 5303 | s = alloc((unsigned)(STRLEN(dirnames[i]) + pat_len + 20)); |
| 5304 | if (s == NULL) |
| 5305 | { |
| 5306 | ga_clear_strings(&ga); |
| 5307 | return FAIL; |
| 5308 | } |
| 5309 | sprintf((char *)s, "pack/*/opt/*/%s/%s*.vim", dirnames[i], pat); |
| 5310 | globpath(p_pp, s, &ga, 0); |
| 5311 | vim_free(s); |
| 5312 | } |
| 5313 | } |
| 5314 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5315 | for (i = 0; i < ga.ga_len; ++i) |
| 5316 | { |
| 5317 | match = ((char_u **)ga.ga_data)[i]; |
| 5318 | s = match; |
| 5319 | e = s + STRLEN(s); |
| 5320 | if (e - 4 > s && STRNICMP(e - 4, ".vim", 4) == 0) |
| 5321 | { |
| 5322 | e -= 4; |
| 5323 | for (s = e; s > match; mb_ptr_back(match, s)) |
| 5324 | if (s < match || vim_ispathsep(*s)) |
| 5325 | break; |
| 5326 | ++s; |
| 5327 | *e = NUL; |
| 5328 | mch_memmove(match, s, e - s + 1); |
| 5329 | } |
| 5330 | } |
| 5331 | |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5332 | if (ga.ga_len == 0) |
Bram Moolenaar | e79abdd | 2012-06-29 13:44:41 +0200 | [diff] [blame] | 5333 | return FAIL; |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5334 | |
| 5335 | /* Sort and remove duplicates which can happen when specifying multiple |
Bram Moolenaar | 0c7437a | 2011-06-26 19:40:23 +0200 | [diff] [blame] | 5336 | * directories in dirnames. */ |
Bram Moolenaar | 1587a1e | 2010-07-29 20:59:59 +0200 | [diff] [blame] | 5337 | remove_duplicates(&ga); |
| 5338 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5339 | *file = ga.ga_data; |
| 5340 | *num_file = ga.ga_len; |
| 5341 | return OK; |
| 5342 | } |
| 5343 | |
Bram Moolenaar | 35ca0e7 | 2016-03-05 17:41:49 +0100 | [diff] [blame] | 5344 | /* |
| 5345 | * Expand loadplugin names: |
| 5346 | * 'packpath'/pack/ * /opt/{pat} |
| 5347 | */ |
| 5348 | static int |
| 5349 | ExpandPackAddDir( |
| 5350 | char_u *pat, |
| 5351 | int *num_file, |
| 5352 | char_u ***file) |
| 5353 | { |
| 5354 | char_u *s; |
| 5355 | char_u *e; |
| 5356 | char_u *match; |
| 5357 | garray_T ga; |
| 5358 | int i; |
| 5359 | int pat_len; |
| 5360 | |
| 5361 | *num_file = 0; |
| 5362 | *file = NULL; |
| 5363 | pat_len = (int)STRLEN(pat); |
| 5364 | ga_init2(&ga, (int)sizeof(char *), 10); |
| 5365 | |
| 5366 | s = alloc((unsigned)(pat_len + 26)); |
| 5367 | if (s == NULL) |
| 5368 | { |
| 5369 | ga_clear_strings(&ga); |
| 5370 | return FAIL; |
| 5371 | } |
| 5372 | sprintf((char *)s, "pack/*/opt/%s*", pat); |
| 5373 | globpath(p_pp, s, &ga, 0); |
| 5374 | vim_free(s); |
| 5375 | |
| 5376 | for (i = 0; i < ga.ga_len; ++i) |
| 5377 | { |
| 5378 | match = ((char_u **)ga.ga_data)[i]; |
| 5379 | s = gettail(match); |
| 5380 | e = s + STRLEN(s); |
| 5381 | mch_memmove(match, s, e - s + 1); |
| 5382 | } |
| 5383 | |
| 5384 | if (ga.ga_len == 0) |
| 5385 | return FAIL; |
| 5386 | |
| 5387 | /* Sort and remove duplicates which can happen when specifying multiple |
| 5388 | * directories in dirnames. */ |
| 5389 | remove_duplicates(&ga); |
| 5390 | |
| 5391 | *file = ga.ga_data; |
| 5392 | *num_file = ga.ga_len; |
| 5393 | return OK; |
| 5394 | } |
| 5395 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5396 | #endif |
| 5397 | |
| 5398 | #if defined(FEAT_CMDL_COMPL) || defined(FEAT_EVAL) || defined(PROTO) |
| 5399 | /* |
| 5400 | * Expand "file" for all comma-separated directories in "path". |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5401 | * Adds the matches to "ga". Caller must init "ga". |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5402 | */ |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5403 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5404 | globpath( |
| 5405 | char_u *path, |
| 5406 | char_u *file, |
| 5407 | garray_T *ga, |
| 5408 | int expand_options) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5409 | { |
| 5410 | expand_T xpc; |
| 5411 | char_u *buf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5412 | int i; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5413 | int num_p; |
| 5414 | char_u **p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5415 | |
| 5416 | buf = alloc(MAXPATHL); |
| 5417 | if (buf == NULL) |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5418 | return; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5419 | |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5420 | ExpandInit(&xpc); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5421 | xpc.xp_context = EXPAND_FILES; |
Bram Moolenaar | 8ada17c | 2006-01-19 22:16:24 +0000 | [diff] [blame] | 5422 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5423 | /* Loop over all entries in {path}. */ |
| 5424 | while (*path != NUL) |
| 5425 | { |
| 5426 | /* Copy one item of the path to buf[] and concatenate the file name. */ |
| 5427 | copy_option_part(&path, buf, MAXPATHL, ","); |
| 5428 | if (STRLEN(buf) + STRLEN(file) + 2 < MAXPATHL) |
| 5429 | { |
Bram Moolenaar | 48e330a | 2016-02-23 14:53:34 +0100 | [diff] [blame] | 5430 | # if defined(MSWIN) |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5431 | /* Using the platform's path separator (\) makes vim incorrectly |
| 5432 | * treat it as an escape character, use '/' instead. */ |
| 5433 | if (*buf != NUL && !after_pathsep(buf, buf + STRLEN(buf))) |
| 5434 | STRCAT(buf, "/"); |
| 5435 | # else |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5436 | add_pathsep(buf); |
Bram Moolenaar | 84f888a | 2010-08-05 21:40:16 +0200 | [diff] [blame] | 5437 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5438 | STRCAT(buf, file); |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5439 | if (ExpandFromContext(&xpc, buf, &num_p, &p, |
| 5440 | WILD_SILENT|expand_options) != FAIL && num_p > 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5441 | { |
Bram Moolenaar | bb5ddda | 2008-11-28 10:01:10 +0000 | [diff] [blame] | 5442 | ExpandEscape(&xpc, buf, num_p, p, WILD_SILENT|expand_options); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5443 | |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5444 | if (ga_grow(ga, num_p) == OK) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5445 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5446 | for (i = 0; i < num_p; ++i) |
| 5447 | { |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5448 | ((char_u **)ga->ga_data)[ga->ga_len] = |
Bram Moolenaar | 7116aa0 | 2014-05-29 14:36:29 +0200 | [diff] [blame] | 5449 | vim_strnsave(p[i], (int)STRLEN(p[i])); |
Bram Moolenaar | 1b1063a | 2014-05-07 18:35:30 +0200 | [diff] [blame] | 5450 | ++ga->ga_len; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5451 | } |
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 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5454 | FreeWild(num_p, p); |
| 5455 | } |
| 5456 | } |
| 5457 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5458 | |
| 5459 | vim_free(buf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5460 | } |
| 5461 | |
| 5462 | #endif |
| 5463 | |
| 5464 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 5465 | |
| 5466 | /********************************* |
| 5467 | * Command line history stuff * |
| 5468 | *********************************/ |
| 5469 | |
| 5470 | /* |
| 5471 | * Translate a history character to the associated type number. |
| 5472 | */ |
| 5473 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5474 | hist_char2type(int c) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5475 | { |
| 5476 | if (c == ':') |
| 5477 | return HIST_CMD; |
| 5478 | if (c == '=') |
| 5479 | return HIST_EXPR; |
| 5480 | if (c == '@') |
| 5481 | return HIST_INPUT; |
| 5482 | if (c == '>') |
| 5483 | return HIST_DEBUG; |
| 5484 | return HIST_SEARCH; /* must be '?' or '/' */ |
| 5485 | } |
| 5486 | |
| 5487 | /* |
| 5488 | * Table of history names. |
| 5489 | * These names are used in :history and various hist...() functions. |
| 5490 | * It is sufficient to give the significant prefix of a history name. |
| 5491 | */ |
| 5492 | |
| 5493 | static char *(history_names[]) = |
| 5494 | { |
| 5495 | "cmd", |
| 5496 | "search", |
| 5497 | "expr", |
| 5498 | "input", |
| 5499 | "debug", |
| 5500 | NULL |
| 5501 | }; |
| 5502 | |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5503 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 5504 | /* |
| 5505 | * Function given to ExpandGeneric() to obtain the possible first |
| 5506 | * arguments of the ":history command. |
| 5507 | */ |
| 5508 | static char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5509 | get_history_arg(expand_T *xp UNUSED, int idx) |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5510 | { |
| 5511 | static char_u compl[2] = { NUL, NUL }; |
| 5512 | char *short_names = ":=@>?/"; |
Bram Moolenaar | 17bd9dc | 2012-05-25 11:02:41 +0200 | [diff] [blame] | 5513 | int short_names_count = (int)STRLEN(short_names); |
Bram Moolenaar | 5ae636b | 2012-04-30 18:48:53 +0200 | [diff] [blame] | 5514 | int history_name_count = sizeof(history_names) / sizeof(char *) - 1; |
| 5515 | |
| 5516 | if (idx < short_names_count) |
| 5517 | { |
| 5518 | compl[0] = (char_u)short_names[idx]; |
| 5519 | return compl; |
| 5520 | } |
| 5521 | if (idx < short_names_count + history_name_count) |
| 5522 | return (char_u *)history_names[idx - short_names_count]; |
| 5523 | if (idx == short_names_count + history_name_count) |
| 5524 | return (char_u *)"all"; |
| 5525 | return NULL; |
| 5526 | } |
| 5527 | #endif |
| 5528 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5529 | /* |
| 5530 | * init_history() - Initialize the command line history. |
| 5531 | * Also used to re-allocate the history when the size changes. |
| 5532 | */ |
Bram Moolenaar | 1ec484f | 2005-06-24 23:07:47 +0000 | [diff] [blame] | 5533 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5534 | init_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5535 | { |
| 5536 | int newlen; /* new length of history table */ |
| 5537 | histentry_T *temp; |
| 5538 | int i; |
| 5539 | int j; |
| 5540 | int type; |
| 5541 | |
| 5542 | /* |
| 5543 | * If size of history table changed, reallocate it |
| 5544 | */ |
| 5545 | newlen = (int)p_hi; |
| 5546 | if (newlen != hislen) /* history length changed */ |
| 5547 | { |
| 5548 | for (type = 0; type < HIST_COUNT; ++type) /* adjust the tables */ |
| 5549 | { |
| 5550 | if (newlen) |
| 5551 | { |
| 5552 | temp = (histentry_T *)lalloc( |
| 5553 | (long_u)(newlen * sizeof(histentry_T)), TRUE); |
| 5554 | if (temp == NULL) /* out of memory! */ |
| 5555 | { |
| 5556 | if (type == 0) /* first one: just keep the old length */ |
| 5557 | { |
| 5558 | newlen = hislen; |
| 5559 | break; |
| 5560 | } |
| 5561 | /* Already changed one table, now we can only have zero |
| 5562 | * length for all tables. */ |
| 5563 | newlen = 0; |
| 5564 | type = -1; |
| 5565 | continue; |
| 5566 | } |
| 5567 | } |
| 5568 | else |
| 5569 | temp = NULL; |
| 5570 | if (newlen == 0 || temp != NULL) |
| 5571 | { |
| 5572 | if (hisidx[type] < 0) /* there are no entries yet */ |
| 5573 | { |
| 5574 | for (i = 0; i < newlen; ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5575 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5576 | } |
| 5577 | else if (newlen > hislen) /* array becomes bigger */ |
| 5578 | { |
| 5579 | for (i = 0; i <= hisidx[type]; ++i) |
| 5580 | temp[i] = history[type][i]; |
| 5581 | j = i; |
| 5582 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5583 | clear_hist_entry(&temp[i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5584 | for ( ; j < hislen; ++i, ++j) |
| 5585 | temp[i] = history[type][j]; |
| 5586 | } |
| 5587 | else /* array becomes smaller or 0 */ |
| 5588 | { |
| 5589 | j = hisidx[type]; |
| 5590 | for (i = newlen - 1; ; --i) |
| 5591 | { |
| 5592 | if (i >= 0) /* copy newest entries */ |
| 5593 | temp[i] = history[type][j]; |
| 5594 | else /* remove older entries */ |
| 5595 | vim_free(history[type][j].hisstr); |
| 5596 | if (--j < 0) |
| 5597 | j = hislen - 1; |
| 5598 | if (j == hisidx[type]) |
| 5599 | break; |
| 5600 | } |
| 5601 | hisidx[type] = newlen - 1; |
| 5602 | } |
| 5603 | vim_free(history[type]); |
| 5604 | history[type] = temp; |
| 5605 | } |
| 5606 | } |
| 5607 | hislen = newlen; |
| 5608 | } |
| 5609 | } |
| 5610 | |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5611 | static void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5612 | clear_hist_entry(histentry_T *hisptr) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5613 | { |
| 5614 | hisptr->hisnum = 0; |
| 5615 | hisptr->viminfo = FALSE; |
| 5616 | hisptr->hisstr = NULL; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 5617 | hisptr->time_set = 0; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5618 | } |
| 5619 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5620 | /* |
| 5621 | * Check if command line 'str' is already in history. |
| 5622 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 5623 | */ |
| 5624 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5625 | in_history( |
| 5626 | int type, |
| 5627 | char_u *str, |
| 5628 | int move_to_front, /* Move the entry to the front if it exists */ |
| 5629 | int sep, |
| 5630 | int writing) /* ignore entries read from viminfo */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5631 | { |
| 5632 | int i; |
| 5633 | int last_i = -1; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5634 | char_u *p; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5635 | |
| 5636 | if (hisidx[type] < 0) |
| 5637 | return FALSE; |
| 5638 | i = hisidx[type]; |
| 5639 | do |
| 5640 | { |
| 5641 | if (history[type][i].hisstr == NULL) |
| 5642 | return FALSE; |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5643 | |
| 5644 | /* For search history, check that the separator character matches as |
| 5645 | * well. */ |
| 5646 | p = history[type][i].hisstr; |
| 5647 | if (STRCMP(str, p) == 0 |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5648 | && !(writing && history[type][i].viminfo) |
Bram Moolenaar | 4c40223 | 2011-07-27 17:58:46 +0200 | [diff] [blame] | 5649 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5650 | { |
| 5651 | if (!move_to_front) |
| 5652 | return TRUE; |
| 5653 | last_i = i; |
| 5654 | break; |
| 5655 | } |
| 5656 | if (--i < 0) |
| 5657 | i = hislen - 1; |
| 5658 | } while (i != hisidx[type]); |
| 5659 | |
| 5660 | if (last_i >= 0) |
| 5661 | { |
| 5662 | str = history[type][i].hisstr; |
| 5663 | while (i != hisidx[type]) |
| 5664 | { |
| 5665 | if (++i >= hislen) |
| 5666 | i = 0; |
| 5667 | history[type][last_i] = history[type][i]; |
| 5668 | last_i = i; |
| 5669 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5670 | history[type][i].hisnum = ++hisnum[type]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5671 | history[type][i].viminfo = FALSE; |
| 5672 | history[type][i].hisstr = str; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5673 | history[type][i].time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5674 | return TRUE; |
| 5675 | } |
| 5676 | return FALSE; |
| 5677 | } |
| 5678 | |
| 5679 | /* |
| 5680 | * Convert history name (from table above) to its HIST_ equivalent. |
| 5681 | * When "name" is empty, return "cmd" history. |
| 5682 | * Returns -1 for unknown history name. |
| 5683 | */ |
| 5684 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5685 | get_histtype(char_u *name) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5686 | { |
| 5687 | int i; |
| 5688 | int len = (int)STRLEN(name); |
| 5689 | |
| 5690 | /* No argument: use current history. */ |
| 5691 | if (len == 0) |
| 5692 | return hist_char2type(ccline.cmdfirstc); |
| 5693 | |
| 5694 | for (i = 0; history_names[i] != NULL; ++i) |
| 5695 | if (STRNICMP(name, history_names[i], len) == 0) |
| 5696 | return i; |
| 5697 | |
| 5698 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 5699 | return hist_char2type(name[0]); |
| 5700 | |
| 5701 | return -1; |
| 5702 | } |
| 5703 | |
| 5704 | static int last_maptick = -1; /* last seen maptick */ |
| 5705 | |
| 5706 | /* |
| 5707 | * Add the given string to the given history. If the string is already in the |
| 5708 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 5709 | * values. |
| 5710 | */ |
| 5711 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5712 | add_to_history( |
| 5713 | int histype, |
| 5714 | char_u *new_entry, |
| 5715 | int in_map, /* consider maptick when inside a mapping */ |
| 5716 | int sep) /* separator character used (search hist) */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5717 | { |
| 5718 | histentry_T *hisptr; |
| 5719 | int len; |
| 5720 | |
| 5721 | if (hislen == 0) /* no history */ |
| 5722 | return; |
| 5723 | |
Bram Moolenaar | a939e43 | 2013-11-09 05:30:26 +0100 | [diff] [blame] | 5724 | if (cmdmod.keeppatterns && histype == HIST_SEARCH) |
| 5725 | return; |
| 5726 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5727 | /* |
| 5728 | * Searches inside the same mapping overwrite each other, so that only |
| 5729 | * the last line is kept. Be careful not to remove a line that was moved |
| 5730 | * down, only lines that were added. |
| 5731 | */ |
| 5732 | if (histype == HIST_SEARCH && in_map) |
| 5733 | { |
| 5734 | if (maptick == last_maptick) |
| 5735 | { |
| 5736 | /* Current line is from the same mapping, remove it */ |
| 5737 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 5738 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5739 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5740 | --hisnum[histype]; |
| 5741 | if (--hisidx[HIST_SEARCH] < 0) |
| 5742 | hisidx[HIST_SEARCH] = hislen - 1; |
| 5743 | } |
| 5744 | last_maptick = -1; |
| 5745 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 5746 | if (!in_history(histype, new_entry, TRUE, sep, FALSE)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5747 | { |
| 5748 | if (++hisidx[histype] == hislen) |
| 5749 | hisidx[histype] = 0; |
| 5750 | hisptr = &history[histype][hisidx[histype]]; |
| 5751 | vim_free(hisptr->hisstr); |
| 5752 | |
| 5753 | /* Store the separator after the NUL of the string. */ |
Bram Moolenaar | a93fa7e | 2006-04-17 22:14:47 +0000 | [diff] [blame] | 5754 | len = (int)STRLEN(new_entry); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5755 | hisptr->hisstr = vim_strnsave(new_entry, len + 2); |
| 5756 | if (hisptr->hisstr != NULL) |
| 5757 | hisptr->hisstr[len + 1] = sep; |
| 5758 | |
| 5759 | hisptr->hisnum = ++hisnum[histype]; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5760 | hisptr->viminfo = FALSE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 5761 | hisptr->time_set = vim_time(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5762 | if (histype == HIST_SEARCH && in_map) |
| 5763 | last_maptick = maptick; |
| 5764 | } |
| 5765 | } |
| 5766 | |
| 5767 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 5768 | |
| 5769 | /* |
| 5770 | * Get identifier of newest history entry. |
| 5771 | * "histype" may be one of the HIST_ values. |
| 5772 | */ |
| 5773 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5774 | get_history_idx(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5775 | { |
| 5776 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5777 | || hisidx[histype] < 0) |
| 5778 | return -1; |
| 5779 | |
| 5780 | return history[histype][hisidx[histype]].hisnum; |
| 5781 | } |
| 5782 | |
Bram Moolenaar | bfd8fc0 | 2005-09-20 23:22:24 +0000 | [diff] [blame] | 5783 | /* |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5784 | * Calculate history index from a number: |
| 5785 | * num > 0: seen as identifying number of a history entry |
| 5786 | * num < 0: relative position in history wrt newest entry |
| 5787 | * "histype" may be one of the HIST_ values. |
| 5788 | */ |
| 5789 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5790 | calc_hist_idx(int histype, int num) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5791 | { |
| 5792 | int i; |
| 5793 | histentry_T *hist; |
| 5794 | int wrapped = FALSE; |
| 5795 | |
| 5796 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 5797 | || (i = hisidx[histype]) < 0 || num == 0) |
| 5798 | return -1; |
| 5799 | |
| 5800 | hist = history[histype]; |
| 5801 | if (num > 0) |
| 5802 | { |
| 5803 | while (hist[i].hisnum > num) |
| 5804 | if (--i < 0) |
| 5805 | { |
| 5806 | if (wrapped) |
| 5807 | break; |
| 5808 | i += hislen; |
| 5809 | wrapped = TRUE; |
| 5810 | } |
| 5811 | if (hist[i].hisnum == num && hist[i].hisstr != NULL) |
| 5812 | return i; |
| 5813 | } |
| 5814 | else if (-num <= hislen) |
| 5815 | { |
| 5816 | i += num + 1; |
| 5817 | if (i < 0) |
| 5818 | i += hislen; |
| 5819 | if (hist[i].hisstr != NULL) |
| 5820 | return i; |
| 5821 | } |
| 5822 | return -1; |
| 5823 | } |
| 5824 | |
| 5825 | /* |
| 5826 | * Get a history entry by its index. |
| 5827 | * "histype" may be one of the HIST_ values. |
| 5828 | */ |
| 5829 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5830 | get_history_entry(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5831 | { |
| 5832 | idx = calc_hist_idx(histype, idx); |
| 5833 | if (idx >= 0) |
| 5834 | return history[histype][idx].hisstr; |
| 5835 | else |
| 5836 | return (char_u *)""; |
| 5837 | } |
| 5838 | |
| 5839 | /* |
| 5840 | * Clear all entries of a history. |
| 5841 | * "histype" may be one of the HIST_ values. |
| 5842 | */ |
| 5843 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5844 | clr_history(int histype) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5845 | { |
| 5846 | int i; |
| 5847 | histentry_T *hisptr; |
| 5848 | |
| 5849 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 5850 | { |
| 5851 | hisptr = history[histype]; |
| 5852 | for (i = hislen; i--;) |
| 5853 | { |
| 5854 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5855 | clear_hist_entry(hisptr); |
Bram Moolenaar | 119d469 | 2016-03-05 21:21:24 +0100 | [diff] [blame] | 5856 | hisptr++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5857 | } |
| 5858 | hisidx[histype] = -1; /* mark history as cleared */ |
| 5859 | hisnum[histype] = 0; /* reset identifier counter */ |
| 5860 | return OK; |
| 5861 | } |
| 5862 | return FAIL; |
| 5863 | } |
| 5864 | |
| 5865 | /* |
| 5866 | * Remove all entries matching {str} from a history. |
| 5867 | * "histype" may be one of the HIST_ values. |
| 5868 | */ |
| 5869 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5870 | del_history_entry(int histype, char_u *str) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5871 | { |
| 5872 | regmatch_T regmatch; |
| 5873 | histentry_T *hisptr; |
| 5874 | int idx; |
| 5875 | int i; |
| 5876 | int last; |
| 5877 | int found = FALSE; |
| 5878 | |
| 5879 | regmatch.regprog = NULL; |
| 5880 | regmatch.rm_ic = FALSE; /* always match case */ |
| 5881 | if (hislen != 0 |
| 5882 | && histype >= 0 |
| 5883 | && histype < HIST_COUNT |
| 5884 | && *str != NUL |
| 5885 | && (idx = hisidx[histype]) >= 0 |
| 5886 | && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) |
| 5887 | != NULL) |
| 5888 | { |
| 5889 | i = last = idx; |
| 5890 | do |
| 5891 | { |
| 5892 | hisptr = &history[histype][i]; |
| 5893 | if (hisptr->hisstr == NULL) |
| 5894 | break; |
| 5895 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
| 5896 | { |
| 5897 | found = TRUE; |
| 5898 | vim_free(hisptr->hisstr); |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5899 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5900 | } |
| 5901 | else |
| 5902 | { |
| 5903 | if (i != last) |
| 5904 | { |
| 5905 | history[histype][last] = *hisptr; |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5906 | clear_hist_entry(hisptr); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5907 | } |
| 5908 | if (--last < 0) |
| 5909 | last += hislen; |
| 5910 | } |
| 5911 | if (--i < 0) |
| 5912 | i += hislen; |
| 5913 | } while (i != idx); |
| 5914 | if (history[histype][idx].hisstr == NULL) |
| 5915 | hisidx[histype] = -1; |
| 5916 | } |
Bram Moolenaar | 473de61 | 2013-06-08 18:19:48 +0200 | [diff] [blame] | 5917 | vim_regfree(regmatch.regprog); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5918 | return found; |
| 5919 | } |
| 5920 | |
| 5921 | /* |
| 5922 | * Remove an indexed entry from a history. |
| 5923 | * "histype" may be one of the HIST_ values. |
| 5924 | */ |
| 5925 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5926 | del_history_idx(int histype, int idx) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5927 | { |
| 5928 | int i, j; |
| 5929 | |
| 5930 | i = calc_hist_idx(histype, idx); |
| 5931 | if (i < 0) |
| 5932 | return FALSE; |
| 5933 | idx = hisidx[histype]; |
| 5934 | vim_free(history[histype][i].hisstr); |
| 5935 | |
| 5936 | /* When deleting the last added search string in a mapping, reset |
| 5937 | * last_maptick, so that the last added search string isn't deleted again. |
| 5938 | */ |
| 5939 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 5940 | last_maptick = -1; |
| 5941 | |
| 5942 | while (i != idx) |
| 5943 | { |
| 5944 | j = (i + 1) % hislen; |
| 5945 | history[histype][i] = history[histype][j]; |
| 5946 | i = j; |
| 5947 | } |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 5948 | clear_hist_entry(&history[histype][i]); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5949 | if (--i < 0) |
| 5950 | i += hislen; |
| 5951 | hisidx[histype] = i; |
| 5952 | return TRUE; |
| 5953 | } |
| 5954 | |
| 5955 | #endif /* FEAT_EVAL */ |
| 5956 | |
| 5957 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 5958 | /* |
| 5959 | * Very specific function to remove the value in ":set key=val" from the |
| 5960 | * history. |
| 5961 | */ |
| 5962 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 5963 | remove_key_from_history(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5964 | { |
| 5965 | char_u *p; |
| 5966 | int i; |
| 5967 | |
| 5968 | i = hisidx[HIST_CMD]; |
| 5969 | if (i < 0) |
| 5970 | return; |
| 5971 | p = history[HIST_CMD][i].hisstr; |
| 5972 | if (p != NULL) |
| 5973 | for ( ; *p; ++p) |
| 5974 | if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) |
| 5975 | { |
| 5976 | p = vim_strchr(p + 3, '='); |
| 5977 | if (p == NULL) |
| 5978 | break; |
| 5979 | ++p; |
| 5980 | for (i = 0; p[i] && !vim_iswhite(p[i]); ++i) |
| 5981 | if (p[i] == '\\' && p[i + 1]) |
| 5982 | ++i; |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 5983 | STRMOVE(p, p + i); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 5984 | --p; |
| 5985 | } |
| 5986 | } |
| 5987 | #endif |
| 5988 | |
| 5989 | #endif /* FEAT_CMDHIST */ |
| 5990 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 5991 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 5992 | /* |
| 5993 | * Get pointer to the command line info to use. cmdline_paste() may clear |
| 5994 | * ccline and put the previous value in prev_ccline. |
| 5995 | */ |
| 5996 | static struct cmdline_info * |
| 5997 | get_ccline_ptr(void) |
| 5998 | { |
| 5999 | if ((State & CMDLINE) == 0) |
| 6000 | return NULL; |
| 6001 | if (ccline.cmdbuff != NULL) |
| 6002 | return &ccline; |
| 6003 | if (prev_ccline_used && prev_ccline.cmdbuff != NULL) |
| 6004 | return &prev_ccline; |
| 6005 | return NULL; |
| 6006 | } |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6007 | #endif |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6008 | |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6009 | #if defined(FEAT_EVAL) || defined(PROTO) |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6010 | /* |
| 6011 | * Get the current command line in allocated memory. |
| 6012 | * Only works when the command line is being edited. |
| 6013 | * Returns NULL when something is wrong. |
| 6014 | */ |
| 6015 | char_u * |
| 6016 | get_cmdline_str(void) |
| 6017 | { |
| 6018 | struct cmdline_info *p = get_ccline_ptr(); |
| 6019 | |
| 6020 | if (p == NULL) |
| 6021 | return NULL; |
| 6022 | return vim_strnsave(p->cmdbuff, p->cmdlen); |
| 6023 | } |
| 6024 | |
| 6025 | /* |
| 6026 | * Get the current command line position, counted in bytes. |
| 6027 | * Zero is the first position. |
| 6028 | * Only works when the command line is being edited. |
| 6029 | * Returns -1 when something is wrong. |
| 6030 | */ |
| 6031 | int |
| 6032 | get_cmdline_pos(void) |
| 6033 | { |
| 6034 | struct cmdline_info *p = get_ccline_ptr(); |
| 6035 | |
| 6036 | if (p == NULL) |
| 6037 | return -1; |
| 6038 | return p->cmdpos; |
| 6039 | } |
| 6040 | |
| 6041 | /* |
| 6042 | * Set the command line byte position to "pos". Zero is the first position. |
| 6043 | * Only works when the command line is being edited. |
| 6044 | * Returns 1 when failed, 0 when OK. |
| 6045 | */ |
| 6046 | int |
| 6047 | set_cmdline_pos( |
| 6048 | int pos) |
| 6049 | { |
| 6050 | struct cmdline_info *p = get_ccline_ptr(); |
| 6051 | |
| 6052 | if (p == NULL) |
| 6053 | return 1; |
| 6054 | |
| 6055 | /* The position is not set directly but after CTRL-\ e or CTRL-R = has |
| 6056 | * changed the command line. */ |
| 6057 | if (pos < 0) |
| 6058 | new_cmdpos = 0; |
| 6059 | else |
| 6060 | new_cmdpos = pos; |
| 6061 | return 0; |
| 6062 | } |
| 6063 | #endif |
| 6064 | |
| 6065 | #if defined(FEAT_EVAL) || defined(FEAT_CMDWIN) || defined(PROTO) |
| 6066 | /* |
| 6067 | * Get the current command-line type. |
| 6068 | * Returns ':' or '/' or '?' or '@' or '>' or '-' |
| 6069 | * Only works when the command line is being edited. |
| 6070 | * Returns NUL when something is wrong. |
| 6071 | */ |
| 6072 | int |
| 6073 | get_cmdline_type(void) |
| 6074 | { |
| 6075 | struct cmdline_info *p = get_ccline_ptr(); |
| 6076 | |
| 6077 | if (p == NULL) |
| 6078 | return NUL; |
| 6079 | if (p->cmdfirstc == NUL) |
Bram Moolenaar | 064154c | 2016-03-19 22:50:43 +0100 | [diff] [blame] | 6080 | return |
| 6081 | # ifdef FEAT_EVAL |
| 6082 | (p->input_fn) ? '@' : |
| 6083 | # endif |
| 6084 | '-'; |
Bram Moolenaar | d293b2b | 2016-03-19 22:29:49 +0100 | [diff] [blame] | 6085 | return p->cmdfirstc; |
| 6086 | } |
| 6087 | #endif |
| 6088 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6089 | #if defined(FEAT_QUICKFIX) || defined(FEAT_CMDHIST) || defined(PROTO) |
| 6090 | /* |
| 6091 | * Get indices "num1,num2" that specify a range within a list (not a range of |
| 6092 | * text lines in a buffer!) from a string. Used for ":history" and ":clist". |
| 6093 | * Returns OK if parsed successfully, otherwise FAIL. |
| 6094 | */ |
| 6095 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6096 | get_list_range(char_u **str, int *num1, int *num2) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6097 | { |
| 6098 | int len; |
| 6099 | int first = FALSE; |
Bram Moolenaar | 22fcfad | 2016-07-01 18:17:26 +0200 | [diff] [blame] | 6100 | varnumber_T num; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6101 | |
| 6102 | *str = skipwhite(*str); |
| 6103 | if (**str == '-' || vim_isdigit(**str)) /* parse "from" part of range */ |
| 6104 | { |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6105 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6106 | *str += len; |
| 6107 | *num1 = (int)num; |
| 6108 | first = TRUE; |
| 6109 | } |
| 6110 | *str = skipwhite(*str); |
| 6111 | if (**str == ',') /* parse "to" part of range */ |
| 6112 | { |
| 6113 | *str = skipwhite(*str + 1); |
Bram Moolenaar | 887c1fe | 2016-01-02 17:56:35 +0100 | [diff] [blame] | 6114 | vim_str2nr(*str, NULL, &len, 0, &num, NULL, 0); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6115 | if (len > 0) |
| 6116 | { |
| 6117 | *num2 = (int)num; |
| 6118 | *str = skipwhite(*str + len); |
| 6119 | } |
| 6120 | else if (!first) /* no number given at all */ |
| 6121 | return FAIL; |
| 6122 | } |
| 6123 | else if (first) /* only one number given */ |
| 6124 | *num2 = *num1; |
| 6125 | return OK; |
| 6126 | } |
| 6127 | #endif |
| 6128 | |
| 6129 | #if defined(FEAT_CMDHIST) || defined(PROTO) |
| 6130 | /* |
| 6131 | * :history command - print a history |
| 6132 | */ |
| 6133 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6134 | ex_history(exarg_T *eap) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6135 | { |
| 6136 | histentry_T *hist; |
| 6137 | int histype1 = HIST_CMD; |
| 6138 | int histype2 = HIST_CMD; |
| 6139 | int hisidx1 = 1; |
| 6140 | int hisidx2 = -1; |
| 6141 | int idx; |
| 6142 | int i, j, k; |
| 6143 | char_u *end; |
| 6144 | char_u *arg = eap->arg; |
| 6145 | |
| 6146 | if (hislen == 0) |
| 6147 | { |
| 6148 | MSG(_("'history' option is zero")); |
| 6149 | return; |
| 6150 | } |
| 6151 | |
| 6152 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 6153 | { |
| 6154 | end = arg; |
| 6155 | while (ASCII_ISALPHA(*end) |
| 6156 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 6157 | end++; |
| 6158 | i = *end; |
| 6159 | *end = NUL; |
| 6160 | histype1 = get_histtype(arg); |
| 6161 | if (histype1 == -1) |
| 6162 | { |
Bram Moolenaar | b9c1e96 | 2009-04-22 11:52:33 +0000 | [diff] [blame] | 6163 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6164 | { |
| 6165 | histype1 = 0; |
| 6166 | histype2 = HIST_COUNT-1; |
| 6167 | } |
| 6168 | else |
| 6169 | { |
| 6170 | *end = i; |
| 6171 | EMSG(_(e_trailing)); |
| 6172 | return; |
| 6173 | } |
| 6174 | } |
| 6175 | else |
| 6176 | histype2 = histype1; |
| 6177 | *end = i; |
| 6178 | } |
| 6179 | else |
| 6180 | end = arg; |
| 6181 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 6182 | { |
| 6183 | EMSG(_(e_trailing)); |
| 6184 | return; |
| 6185 | } |
| 6186 | |
| 6187 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 6188 | { |
| 6189 | STRCPY(IObuff, "\n # "); |
| 6190 | STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); |
| 6191 | MSG_PUTS_TITLE(IObuff); |
| 6192 | idx = hisidx[histype1]; |
| 6193 | hist = history[histype1]; |
| 6194 | j = hisidx1; |
| 6195 | k = hisidx2; |
| 6196 | if (j < 0) |
| 6197 | j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; |
| 6198 | if (k < 0) |
| 6199 | k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; |
| 6200 | if (idx >= 0 && j <= k) |
| 6201 | for (i = idx + 1; !got_int; ++i) |
| 6202 | { |
| 6203 | if (i == hislen) |
| 6204 | i = 0; |
| 6205 | if (hist[i].hisstr != NULL |
| 6206 | && hist[i].hisnum >= j && hist[i].hisnum <= k) |
| 6207 | { |
| 6208 | msg_putchar('\n'); |
| 6209 | sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', |
| 6210 | hist[i].hisnum); |
| 6211 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
| 6212 | trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), |
Bram Moolenaar | 38f5f95 | 2012-01-26 13:01:59 +0100 | [diff] [blame] | 6213 | (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6214 | else |
| 6215 | STRCAT(IObuff, hist[i].hisstr); |
| 6216 | msg_outtrans(IObuff); |
| 6217 | out_flush(); |
| 6218 | } |
| 6219 | if (i == idx) |
| 6220 | break; |
| 6221 | } |
| 6222 | } |
| 6223 | } |
| 6224 | #endif |
| 6225 | |
| 6226 | #if (defined(FEAT_VIMINFO) && defined(FEAT_CMDHIST)) || defined(PROTO) |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6227 | /* |
| 6228 | * Buffers for history read from a viminfo file. Only valid while reading. |
| 6229 | */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6230 | static histentry_T *viminfo_history[HIST_COUNT] = |
| 6231 | {NULL, NULL, NULL, NULL, NULL}; |
| 6232 | static int viminfo_hisidx[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 6233 | static int viminfo_hislen[HIST_COUNT] = {0, 0, 0, 0, 0}; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6234 | static int viminfo_add_at_front = FALSE; |
| 6235 | |
Bram Moolenaar | d25c16e | 2016-01-29 22:13:30 +0100 | [diff] [blame] | 6236 | static int hist_type2char(int type, int use_question); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6237 | |
| 6238 | /* |
| 6239 | * Translate a history type number to the associated character. |
| 6240 | */ |
| 6241 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6242 | hist_type2char( |
| 6243 | int type, |
| 6244 | int use_question) /* use '?' instead of '/' */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6245 | { |
| 6246 | if (type == HIST_CMD) |
| 6247 | return ':'; |
| 6248 | if (type == HIST_SEARCH) |
| 6249 | { |
| 6250 | if (use_question) |
| 6251 | return '?'; |
| 6252 | else |
| 6253 | return '/'; |
| 6254 | } |
| 6255 | if (type == HIST_EXPR) |
| 6256 | return '='; |
| 6257 | return '@'; |
| 6258 | } |
| 6259 | |
| 6260 | /* |
| 6261 | * Prepare for reading the history from the viminfo file. |
| 6262 | * This allocates history arrays to store the read history lines. |
| 6263 | */ |
| 6264 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6265 | prepare_viminfo_history(int asklen, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6266 | { |
| 6267 | int i; |
| 6268 | int num; |
| 6269 | int type; |
| 6270 | int len; |
| 6271 | |
| 6272 | init_history(); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6273 | viminfo_add_at_front = (asklen != 0 && !writing); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6274 | if (asklen > hislen) |
| 6275 | asklen = hislen; |
| 6276 | |
| 6277 | for (type = 0; type < HIST_COUNT; ++type) |
| 6278 | { |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6279 | /* Count the number of empty spaces in the history list. Entries read |
| 6280 | * from viminfo previously are also considered empty. If there are |
| 6281 | * more spaces available than we request, then fill them up. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6282 | for (i = 0, num = 0; i < hislen; i++) |
Bram Moolenaar | b3049f4 | 2013-04-05 18:58:47 +0200 | [diff] [blame] | 6283 | if (history[type][i].hisstr == NULL || history[type][i].viminfo) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6284 | num++; |
| 6285 | len = asklen; |
| 6286 | if (num > len) |
| 6287 | len = num; |
| 6288 | if (len <= 0) |
| 6289 | viminfo_history[type] = NULL; |
| 6290 | else |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6291 | viminfo_history[type] = (histentry_T *)lalloc( |
| 6292 | (long_u)(len * sizeof(histentry_T)), FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6293 | if (viminfo_history[type] == NULL) |
| 6294 | len = 0; |
| 6295 | viminfo_hislen[type] = len; |
| 6296 | viminfo_hisidx[type] = 0; |
| 6297 | } |
| 6298 | } |
| 6299 | |
| 6300 | /* |
| 6301 | * Accept a line from the viminfo, store it in the history array when it's |
| 6302 | * new. |
| 6303 | */ |
| 6304 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6305 | read_viminfo_history(vir_T *virp, int writing) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6306 | { |
| 6307 | int type; |
| 6308 | long_u len; |
| 6309 | char_u *val; |
| 6310 | char_u *p; |
| 6311 | |
| 6312 | type = hist_char2type(virp->vir_line[0]); |
| 6313 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6314 | { |
| 6315 | val = viminfo_readstring(virp, 1, TRUE); |
| 6316 | if (val != NULL && *val != NUL) |
| 6317 | { |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6318 | int sep = (*val == ' ' ? NUL : *val); |
| 6319 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6320 | if (!in_history(type, val + (type == HIST_SEARCH), |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6321 | viminfo_add_at_front, sep, writing)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6322 | { |
| 6323 | /* Need to re-allocate to append the separator byte. */ |
| 6324 | len = STRLEN(val); |
| 6325 | p = lalloc(len + 2, TRUE); |
| 6326 | if (p != NULL) |
| 6327 | { |
| 6328 | if (type == HIST_SEARCH) |
| 6329 | { |
| 6330 | /* Search entry: Move the separator from the first |
| 6331 | * column to after the NUL. */ |
| 6332 | mch_memmove(p, val + 1, (size_t)len); |
Bram Moolenaar | d87fbc2 | 2012-02-04 22:44:32 +0100 | [diff] [blame] | 6333 | p[len] = sep; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6334 | } |
| 6335 | else |
| 6336 | { |
| 6337 | /* Not a search entry: No separator in the viminfo |
| 6338 | * file, add a NUL separator. */ |
| 6339 | mch_memmove(p, val, (size_t)len + 1); |
| 6340 | p[len + 1] = NUL; |
| 6341 | } |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6342 | viminfo_history[type][viminfo_hisidx[type]].hisstr = p; |
| 6343 | viminfo_history[type][viminfo_hisidx[type]].time_set = 0; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6344 | viminfo_history[type][viminfo_hisidx[type]].viminfo = TRUE; |
| 6345 | viminfo_history[type][viminfo_hisidx[type]].hisnum = 0; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6346 | viminfo_hisidx[type]++; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6347 | } |
| 6348 | } |
| 6349 | } |
| 6350 | vim_free(val); |
| 6351 | } |
| 6352 | return viminfo_readline(virp); |
| 6353 | } |
| 6354 | |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6355 | /* |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6356 | * Accept a new style history line from the viminfo, store it in the history |
| 6357 | * array when it's new. |
| 6358 | */ |
| 6359 | void |
| 6360 | handle_viminfo_history( |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6361 | garray_T *values, |
| 6362 | int writing) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6363 | { |
| 6364 | int type; |
| 6365 | long_u len; |
| 6366 | char_u *val; |
| 6367 | char_u *p; |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6368 | bval_T *vp = (bval_T *)values->ga_data; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6369 | |
| 6370 | /* Check the format: |
| 6371 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6372 | if (values->ga_len < 4 |
| 6373 | || vp[0].bv_type != BVAL_NR |
| 6374 | || vp[1].bv_type != BVAL_NR |
| 6375 | || (vp[2].bv_type != BVAL_NR && vp[2].bv_type != BVAL_EMPTY) |
| 6376 | || vp[3].bv_type != BVAL_STRING) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6377 | return; |
| 6378 | |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6379 | type = vp[0].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6380 | if (type >= HIST_COUNT) |
| 6381 | return; |
| 6382 | if (viminfo_hisidx[type] < viminfo_hislen[type]) |
| 6383 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6384 | val = vp[3].bv_string; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6385 | if (val != NULL && *val != NUL) |
| 6386 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6387 | int sep = type == HIST_SEARCH && vp[2].bv_type == BVAL_NR |
| 6388 | ? vp[2].bv_nr : NUL; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6389 | int idx; |
| 6390 | int overwrite = FALSE; |
| 6391 | |
| 6392 | if (!in_history(type, val, viminfo_add_at_front, sep, writing)) |
| 6393 | { |
| 6394 | /* If lines were written by an older Vim we need to avoid |
| 6395 | * getting duplicates. See if the entry already exists. */ |
| 6396 | for (idx = 0; idx < viminfo_hisidx[type]; ++idx) |
| 6397 | { |
| 6398 | p = viminfo_history[type][idx].hisstr; |
| 6399 | if (STRCMP(val, p) == 0 |
| 6400 | && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) |
| 6401 | { |
| 6402 | overwrite = TRUE; |
| 6403 | break; |
| 6404 | } |
| 6405 | } |
| 6406 | |
| 6407 | if (!overwrite) |
| 6408 | { |
| 6409 | /* Need to re-allocate to append the separator byte. */ |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6410 | len = vp[3].bv_len; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6411 | p = lalloc(len + 2, TRUE); |
| 6412 | } |
Bram Moolenaar | 72e697d | 2016-06-13 22:48:01 +0200 | [diff] [blame] | 6413 | else |
| 6414 | len = 0; /* for picky compilers */ |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6415 | if (p != NULL) |
| 6416 | { |
Bram Moolenaar | 46bbb0c | 2016-06-11 21:04:39 +0200 | [diff] [blame] | 6417 | viminfo_history[type][idx].time_set = vp[1].bv_nr; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6418 | if (!overwrite) |
| 6419 | { |
| 6420 | mch_memmove(p, val, (size_t)len + 1); |
| 6421 | /* Put the separator after the NUL. */ |
| 6422 | p[len + 1] = sep; |
| 6423 | viminfo_history[type][idx].hisstr = p; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6424 | viminfo_history[type][idx].hisnum = 0; |
| 6425 | viminfo_history[type][idx].viminfo = TRUE; |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6426 | viminfo_hisidx[type]++; |
| 6427 | } |
| 6428 | } |
| 6429 | } |
| 6430 | } |
| 6431 | } |
| 6432 | } |
| 6433 | |
| 6434 | /* |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6435 | * Concatenate history lines from viminfo after the lines typed in this Vim. |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6436 | */ |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6437 | static void |
| 6438 | concat_history(int type) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6439 | { |
| 6440 | int idx; |
| 6441 | int i; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6442 | |
| 6443 | idx = hisidx[type] + viminfo_hisidx[type]; |
| 6444 | if (idx >= hislen) |
| 6445 | idx -= hislen; |
| 6446 | else if (idx < 0) |
| 6447 | idx = hislen - 1; |
| 6448 | if (viminfo_add_at_front) |
| 6449 | hisidx[type] = idx; |
| 6450 | else |
| 6451 | { |
| 6452 | if (hisidx[type] == -1) |
| 6453 | hisidx[type] = hislen - 1; |
| 6454 | do |
| 6455 | { |
| 6456 | if (history[type][idx].hisstr != NULL |
| 6457 | || history[type][idx].viminfo) |
| 6458 | break; |
| 6459 | if (++idx == hislen) |
| 6460 | idx = 0; |
| 6461 | } while (idx != hisidx[type]); |
| 6462 | if (idx != hisidx[type] && --idx < 0) |
| 6463 | idx = hislen - 1; |
| 6464 | } |
| 6465 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6466 | { |
| 6467 | vim_free(history[type][idx].hisstr); |
| 6468 | history[type][idx].hisstr = viminfo_history[type][i].hisstr; |
| 6469 | history[type][idx].viminfo = TRUE; |
| 6470 | history[type][idx].time_set = viminfo_history[type][i].time_set; |
| 6471 | if (--idx < 0) |
| 6472 | idx = hislen - 1; |
| 6473 | } |
| 6474 | idx += 1; |
| 6475 | idx %= hislen; |
| 6476 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6477 | { |
| 6478 | history[type][idx++].hisnum = ++hisnum[type]; |
| 6479 | idx %= hislen; |
| 6480 | } |
| 6481 | } |
| 6482 | |
| 6483 | #if defined(FEAT_CMDL_COMPL) || defined(PROTO) |
| 6484 | static int |
| 6485 | #ifdef __BORLANDC__ |
| 6486 | _RTLENTRYF |
| 6487 | #endif |
| 6488 | sort_hist(const void *s1, const void *s2) |
| 6489 | { |
| 6490 | histentry_T *p1 = *(histentry_T **)s1; |
| 6491 | histentry_T *p2 = *(histentry_T **)s2; |
| 6492 | |
| 6493 | if (p1->time_set < p2->time_set) return -1; |
| 6494 | if (p1->time_set > p2->time_set) return 1; |
| 6495 | return 0; |
| 6496 | } |
| 6497 | #endif |
| 6498 | |
| 6499 | /* |
| 6500 | * Merge history lines from viminfo and lines typed in this Vim based on the |
| 6501 | * timestamp; |
| 6502 | */ |
| 6503 | static void |
| 6504 | merge_history(int type) |
| 6505 | { |
| 6506 | int max_len; |
| 6507 | histentry_T **tot_hist; |
| 6508 | histentry_T *new_hist; |
| 6509 | int i; |
| 6510 | int len; |
| 6511 | |
| 6512 | /* Make one long list with all entries. */ |
| 6513 | max_len = hislen + viminfo_hisidx[type]; |
| 6514 | tot_hist = (histentry_T **)alloc(max_len * (int)sizeof(histentry_T *)); |
| 6515 | new_hist = (histentry_T *)alloc(hislen * (int)sizeof(histentry_T)); |
| 6516 | if (tot_hist == NULL || new_hist == NULL) |
| 6517 | { |
| 6518 | vim_free(tot_hist); |
| 6519 | vim_free(new_hist); |
| 6520 | return; |
| 6521 | } |
| 6522 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6523 | tot_hist[i] = &viminfo_history[type][i]; |
| 6524 | len = i; |
| 6525 | for (i = 0; i < hislen; i++) |
| 6526 | if (history[type][i].hisstr != NULL) |
| 6527 | tot_hist[len++] = &history[type][i]; |
| 6528 | |
| 6529 | /* Sort the list on timestamp. */ |
| 6530 | qsort((void *)tot_hist, (size_t)len, sizeof(histentry_T *), sort_hist); |
| 6531 | |
| 6532 | /* Keep the newest ones. */ |
| 6533 | for (i = 0; i < hislen; i++) |
| 6534 | { |
| 6535 | if (i < len) |
| 6536 | { |
| 6537 | new_hist[i] = *tot_hist[i]; |
| 6538 | tot_hist[i]->hisstr = NULL; |
| 6539 | if (new_hist[i].hisnum == 0) |
| 6540 | new_hist[i].hisnum = ++hisnum[type]; |
| 6541 | } |
| 6542 | else |
| 6543 | clear_hist_entry(&new_hist[i]); |
| 6544 | } |
Bram Moolenaar | a890f5e | 2016-06-12 23:03:19 +0200 | [diff] [blame] | 6545 | hisidx[type] = (i < len ? i : len) - 1; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6546 | |
| 6547 | /* Free what is not kept. */ |
| 6548 | for (i = 0; i < viminfo_hisidx[type]; i++) |
| 6549 | vim_free(viminfo_history[type][i].hisstr); |
| 6550 | for (i = 0; i < hislen; i++) |
| 6551 | vim_free(history[type][i].hisstr); |
| 6552 | vim_free(history[type]); |
| 6553 | history[type] = new_hist; |
Bram Moolenaar | 62f8b4e | 2016-06-11 15:31:47 +0200 | [diff] [blame] | 6554 | vim_free(tot_hist); |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6555 | } |
| 6556 | |
| 6557 | /* |
| 6558 | * Finish reading history lines from viminfo. Not used when writing viminfo. |
| 6559 | */ |
| 6560 | void |
| 6561 | finish_viminfo_history(vir_T *virp) |
| 6562 | { |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6563 | int type; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6564 | int merge = virp->vir_version >= VIMINFO_VERSION_WITH_HISTORY; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6565 | |
| 6566 | for (type = 0; type < HIST_COUNT; ++type) |
| 6567 | { |
| 6568 | if (history[type] == NULL) |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6569 | continue; |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6570 | |
| 6571 | if (merge) |
| 6572 | merge_history(type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6573 | else |
Bram Moolenaar | 1fd99c1 | 2016-06-09 20:24:28 +0200 | [diff] [blame] | 6574 | concat_history(type); |
| 6575 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6576 | vim_free(viminfo_history[type]); |
| 6577 | viminfo_history[type] = NULL; |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6578 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6579 | } |
| 6580 | } |
| 6581 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6582 | /* |
| 6583 | * Write history to viminfo file in "fp". |
| 6584 | * When "merge" is TRUE merge history lines with a previously read viminfo |
| 6585 | * file, data is in viminfo_history[]. |
| 6586 | * When "merge" is FALSE just write all history lines. Used for ":wviminfo!". |
| 6587 | */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6588 | void |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6589 | write_viminfo_history(FILE *fp, int merge) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6590 | { |
| 6591 | int i; |
| 6592 | int type; |
| 6593 | int num_saved; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6594 | int round; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6595 | |
| 6596 | init_history(); |
| 6597 | if (hislen == 0) |
| 6598 | return; |
| 6599 | for (type = 0; type < HIST_COUNT; ++type) |
| 6600 | { |
| 6601 | num_saved = get_viminfo_parameter(hist_type2char(type, FALSE)); |
| 6602 | if (num_saved == 0) |
| 6603 | continue; |
| 6604 | if (num_saved < 0) /* Use default */ |
| 6605 | num_saved = hislen; |
| 6606 | fprintf(fp, _("\n# %s History (newest to oldest):\n"), |
| 6607 | type == HIST_CMD ? _("Command Line") : |
| 6608 | type == HIST_SEARCH ? _("Search String") : |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6609 | type == HIST_EXPR ? _("Expression") : |
| 6610 | type == HIST_INPUT ? _("Input Line") : |
| 6611 | _("Debug Line")); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6612 | if (num_saved > hislen) |
| 6613 | num_saved = hislen; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6614 | |
| 6615 | /* |
| 6616 | * Merge typed and viminfo history: |
| 6617 | * round 1: history of typed commands. |
| 6618 | * round 2: history from recently read viminfo. |
| 6619 | */ |
| 6620 | for (round = 1; round <= 2; ++round) |
| 6621 | { |
Bram Moolenaar | a8565fe | 2013-04-15 16:14:22 +0200 | [diff] [blame] | 6622 | if (round == 1) |
| 6623 | /* start at newest entry, somewhere in the list */ |
| 6624 | i = hisidx[type]; |
| 6625 | else if (viminfo_hisidx[type] > 0) |
| 6626 | /* start at newest entry, first in the list */ |
| 6627 | i = 0; |
| 6628 | else |
| 6629 | /* empty list */ |
| 6630 | i = -1; |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6631 | if (i >= 0) |
| 6632 | while (num_saved > 0 |
| 6633 | && !(round == 2 && i >= viminfo_hisidx[type])) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6634 | { |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6635 | char_u *p; |
| 6636 | time_t timestamp; |
| 6637 | int c = NUL; |
| 6638 | |
| 6639 | if (round == 1) |
| 6640 | { |
| 6641 | p = history[type][i].hisstr; |
| 6642 | timestamp = history[type][i].time_set; |
| 6643 | } |
| 6644 | else |
| 6645 | { |
| 6646 | p = viminfo_history[type] == NULL ? NULL |
| 6647 | : viminfo_history[type][i].hisstr; |
| 6648 | timestamp = viminfo_history[type] == NULL ? 0 |
| 6649 | : viminfo_history[type][i].time_set; |
| 6650 | } |
| 6651 | |
Bram Moolenaar | ff1806f | 2013-06-15 16:31:47 +0200 | [diff] [blame] | 6652 | if (p != NULL && (round == 2 |
| 6653 | || !merge |
| 6654 | || !history[type][i].viminfo)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6655 | { |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6656 | --num_saved; |
| 6657 | fputc(hist_type2char(type, TRUE), fp); |
| 6658 | /* For the search history: put the separator in the |
| 6659 | * second column; use a space if there isn't one. */ |
| 6660 | if (type == HIST_SEARCH) |
| 6661 | { |
| 6662 | c = p[STRLEN(p) + 1]; |
| 6663 | putc(c == NUL ? ' ' : c, fp); |
| 6664 | } |
| 6665 | viminfo_writestring(fp, p); |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6666 | |
| 6667 | { |
| 6668 | char cbuf[NUMBUFLEN]; |
| 6669 | |
| 6670 | /* New style history with a bar line. Format: |
| 6671 | * |{bartype},{histtype},{timestamp},{separator},"text" */ |
| 6672 | if (c == NUL) |
| 6673 | cbuf[0] = NUL; |
| 6674 | else |
| 6675 | sprintf(cbuf, "%d", c); |
| 6676 | fprintf(fp, "|%d,%d,%ld,%s,", BARTYPE_HISTORY, |
| 6677 | type, (long)timestamp, cbuf); |
| 6678 | barline_writestring(fp, p, LSIZE - 20); |
| 6679 | putc('\n', fp); |
| 6680 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6681 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6682 | if (round == 1) |
| 6683 | { |
| 6684 | /* Decrement index, loop around and stop when back at |
| 6685 | * the start. */ |
| 6686 | if (--i < 0) |
| 6687 | i = hislen - 1; |
| 6688 | if (i == hisidx[type]) |
| 6689 | break; |
| 6690 | } |
| 6691 | else |
| 6692 | { |
| 6693 | /* Increment index. Stop at the end in the while. */ |
| 6694 | ++i; |
| 6695 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6696 | } |
Bram Moolenaar | 6f852a5 | 2013-04-14 16:26:15 +0200 | [diff] [blame] | 6697 | } |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6698 | for (i = 0; i < viminfo_hisidx[type]; ++i) |
Bram Moolenaar | f687cf3 | 2013-04-24 15:39:11 +0200 | [diff] [blame] | 6699 | if (viminfo_history[type] != NULL) |
Bram Moolenaar | 45d2eea | 2016-06-06 21:07:52 +0200 | [diff] [blame] | 6700 | vim_free(viminfo_history[type][i].hisstr); |
Bram Moolenaar | 07219f9 | 2013-04-14 23:19:36 +0200 | [diff] [blame] | 6701 | vim_free(viminfo_history[type]); |
| 6702 | viminfo_history[type] = NULL; |
Bram Moolenaar | b70a473 | 2013-04-15 22:22:57 +0200 | [diff] [blame] | 6703 | viminfo_hisidx[type] = 0; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6704 | } |
| 6705 | } |
| 6706 | #endif /* FEAT_VIMINFO */ |
| 6707 | |
| 6708 | #if defined(FEAT_FKMAP) || defined(PROTO) |
| 6709 | /* |
| 6710 | * Write a character at the current cursor+offset position. |
| 6711 | * It is directly written into the command buffer block. |
| 6712 | */ |
| 6713 | void |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6714 | cmd_pchar(int c, int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6715 | { |
| 6716 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 6717 | { |
| 6718 | EMSG(_("E198: cmd_pchar beyond the command length")); |
| 6719 | return; |
| 6720 | } |
| 6721 | ccline.cmdbuff[ccline.cmdpos + offset] = (char_u)c; |
| 6722 | ccline.cmdbuff[ccline.cmdlen] = NUL; |
| 6723 | } |
| 6724 | |
| 6725 | int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6726 | cmd_gchar(int offset) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6727 | { |
| 6728 | if (ccline.cmdpos + offset >= ccline.cmdlen || ccline.cmdpos + offset < 0) |
| 6729 | { |
| 6730 | /* EMSG(_("cmd_gchar beyond the command length")); */ |
| 6731 | return NUL; |
| 6732 | } |
| 6733 | return (int)ccline.cmdbuff[ccline.cmdpos + offset]; |
| 6734 | } |
| 6735 | #endif |
| 6736 | |
| 6737 | #if defined(FEAT_CMDWIN) || defined(PROTO) |
| 6738 | /* |
| 6739 | * Open a window on the current command line and history. Allow editing in |
| 6740 | * the window. Returns when the window is closed. |
| 6741 | * Returns: |
| 6742 | * CR if the command is to be executed |
| 6743 | * Ctrl_C if it is to be abandoned |
| 6744 | * K_IGNORE if editing continues |
| 6745 | */ |
| 6746 | static int |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 6747 | ex_window(void) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6748 | { |
| 6749 | struct cmdline_info save_ccline; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6750 | bufref_T old_curbuf; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6751 | win_T *old_curwin = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6752 | bufref_T bufref; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6753 | win_T *wp; |
| 6754 | int i; |
| 6755 | linenr_T lnum; |
| 6756 | int histtype; |
| 6757 | garray_T winsizes; |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 6758 | #ifdef FEAT_AUTOCMD |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6759 | char_u typestr[2]; |
Bram Moolenaar | 7693ec6 | 2008-07-24 18:29:37 +0000 | [diff] [blame] | 6760 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6761 | int save_restart_edit = restart_edit; |
| 6762 | int save_State = State; |
| 6763 | int save_exmode = exmode_active; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6764 | #ifdef FEAT_RIGHTLEFT |
| 6765 | int save_cmdmsg_rl = cmdmsg_rl; |
| 6766 | #endif |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6767 | #ifdef FEAT_FOLDING |
| 6768 | int save_KeyTyped; |
| 6769 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6770 | |
| 6771 | /* Can't do this recursively. Can't do it when typing a password. */ |
| 6772 | if (cmdwin_type != 0 |
| 6773 | # if defined(FEAT_CRYPT) || defined(FEAT_EVAL) |
| 6774 | || cmdline_star > 0 |
| 6775 | # endif |
| 6776 | ) |
| 6777 | { |
| 6778 | beep_flush(); |
| 6779 | return K_IGNORE; |
| 6780 | } |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6781 | set_bufref(&old_curbuf, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6782 | |
| 6783 | /* Save current window sizes. */ |
| 6784 | win_size_save(&winsizes); |
| 6785 | |
| 6786 | # ifdef FEAT_AUTOCMD |
| 6787 | /* Don't execute autocommands while creating the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6788 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6789 | # endif |
Bram Moolenaar | df1bdc9 | 2006-02-23 21:32:16 +0000 | [diff] [blame] | 6790 | /* don't use a new tab page */ |
| 6791 | cmdmod.tab = 0; |
| 6792 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6793 | /* Create a window for the command-line buffer. */ |
| 6794 | if (win_split((int)p_cwh, WSP_BOT) == FAIL) |
| 6795 | { |
| 6796 | beep_flush(); |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6797 | # ifdef FEAT_AUTOCMD |
| 6798 | unblock_autocmds(); |
| 6799 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6800 | return K_IGNORE; |
| 6801 | } |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6802 | cmdwin_type = get_cmdline_type(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6803 | |
| 6804 | /* Create the command-line buffer empty. */ |
Bram Moolenaar | 701f7af | 2008-11-15 13:12:07 +0000 | [diff] [blame] | 6805 | (void)do_ecmd(0, NULL, NULL, NULL, ECMD_ONE, ECMD_HIDE, NULL); |
Bram Moolenaar | 446cb83 | 2008-06-24 21:56:24 +0000 | [diff] [blame] | 6806 | (void)setfname(curbuf, (char_u *)"[Command Line]", NULL, TRUE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6807 | set_option_value((char_u *)"bt", 0L, (char_u *)"nofile", OPT_LOCAL); |
| 6808 | set_option_value((char_u *)"swf", 0L, NULL, OPT_LOCAL); |
| 6809 | curbuf->b_p_ma = TRUE; |
Bram Moolenaar | 876f6d7 | 2009-04-29 10:05:51 +0000 | [diff] [blame] | 6810 | #ifdef FEAT_FOLDING |
| 6811 | curwin->w_p_fen = FALSE; |
| 6812 | #endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6813 | # ifdef FEAT_RIGHTLEFT |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6814 | curwin->w_p_rl = cmdmsg_rl; |
| 6815 | cmdmsg_rl = FALSE; |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6816 | # endif |
Bram Moolenaar | 3368ea2 | 2010-09-21 16:56:35 +0200 | [diff] [blame] | 6817 | RESET_BINDING(curwin); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6818 | |
| 6819 | # ifdef FEAT_AUTOCMD |
| 6820 | /* Do execute autocommands for setting the filetype (load syntax). */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6821 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6822 | # endif |
| 6823 | |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6824 | /* Showing the prompt may have set need_wait_return, reset it. */ |
| 6825 | need_wait_return = FALSE; |
| 6826 | |
Bram Moolenaar | e8bd5ce | 2009-03-02 01:12:48 +0000 | [diff] [blame] | 6827 | histtype = hist_char2type(cmdwin_type); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6828 | if (histtype == HIST_CMD || histtype == HIST_DEBUG) |
| 6829 | { |
| 6830 | if (p_wc == TAB) |
| 6831 | { |
| 6832 | add_map((char_u *)"<buffer> <Tab> <C-X><C-V>", INSERT); |
| 6833 | add_map((char_u *)"<buffer> <Tab> a<C-X><C-V>", NORMAL); |
| 6834 | } |
| 6835 | set_option_value((char_u *)"ft", 0L, (char_u *)"vim", OPT_LOCAL); |
| 6836 | } |
| 6837 | |
Bram Moolenaar | f4b8e57 | 2004-06-24 15:53:16 +0000 | [diff] [blame] | 6838 | /* Reset 'textwidth' after setting 'filetype' (the Vim filetype plugin |
| 6839 | * sets 'textwidth' to 78). */ |
| 6840 | curbuf->b_p_tw = 0; |
| 6841 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6842 | /* Fill the buffer with the history. */ |
| 6843 | init_history(); |
| 6844 | if (hislen > 0) |
| 6845 | { |
| 6846 | i = hisidx[histtype]; |
| 6847 | if (i >= 0) |
| 6848 | { |
| 6849 | lnum = 0; |
| 6850 | do |
| 6851 | { |
| 6852 | if (++i == hislen) |
| 6853 | i = 0; |
| 6854 | if (history[histtype][i].hisstr != NULL) |
| 6855 | ml_append(lnum++, history[histtype][i].hisstr, |
| 6856 | (colnr_T)0, FALSE); |
| 6857 | } |
| 6858 | while (i != hisidx[histtype]); |
| 6859 | } |
| 6860 | } |
| 6861 | |
| 6862 | /* Replace the empty last line with the current command-line and put the |
| 6863 | * cursor there. */ |
| 6864 | ml_replace(curbuf->b_ml.ml_line_count, ccline.cmdbuff, TRUE); |
| 6865 | curwin->w_cursor.lnum = curbuf->b_ml.ml_line_count; |
| 6866 | curwin->w_cursor.col = ccline.cmdpos; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6867 | changed_line_abv_curs(); |
| 6868 | invalidate_botline(); |
Bram Moolenaar | 600dddc | 2006-03-12 22:05:10 +0000 | [diff] [blame] | 6869 | redraw_later(SOME_VALID); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6870 | |
| 6871 | /* Save the command line info, can be used recursively. */ |
| 6872 | save_ccline = ccline; |
| 6873 | ccline.cmdbuff = NULL; |
| 6874 | ccline.cmdprompt = NULL; |
| 6875 | |
| 6876 | /* No Ex mode here! */ |
| 6877 | exmode_active = 0; |
| 6878 | |
| 6879 | State = NORMAL; |
| 6880 | # ifdef FEAT_MOUSE |
| 6881 | setmouse(); |
| 6882 | # endif |
| 6883 | |
| 6884 | # ifdef FEAT_AUTOCMD |
| 6885 | /* Trigger CmdwinEnter autocommands. */ |
| 6886 | typestr[0] = cmdwin_type; |
| 6887 | typestr[1] = NUL; |
| 6888 | apply_autocmds(EVENT_CMDWINENTER, typestr, typestr, FALSE, curbuf); |
Bram Moolenaar | 5495cc9 | 2006-08-16 14:23:04 +0000 | [diff] [blame] | 6889 | if (restart_edit != 0) /* autocmd with ":startinsert" */ |
| 6890 | stuffcharReadbuff(K_NOP); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6891 | # endif |
| 6892 | |
| 6893 | i = RedrawingDisabled; |
| 6894 | RedrawingDisabled = 0; |
| 6895 | |
| 6896 | /* |
| 6897 | * Call the main loop until <CR> or CTRL-C is typed. |
| 6898 | */ |
| 6899 | cmdwin_result = 0; |
Bram Moolenaar | 26a60b4 | 2005-02-22 08:49:11 +0000 | [diff] [blame] | 6900 | main_loop(TRUE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6901 | |
| 6902 | RedrawingDisabled = i; |
| 6903 | |
| 6904 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6905 | |
| 6906 | # ifdef FEAT_FOLDING |
| 6907 | save_KeyTyped = KeyTyped; |
| 6908 | # endif |
| 6909 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6910 | /* Trigger CmdwinLeave autocommands. */ |
| 6911 | apply_autocmds(EVENT_CMDWINLEAVE, typestr, typestr, FALSE, curbuf); |
Bram Moolenaar | 42f06f9 | 2014-08-17 17:24:07 +0200 | [diff] [blame] | 6912 | |
| 6913 | # ifdef FEAT_FOLDING |
| 6914 | /* Restore KeyTyped in case it is modified by autocommands */ |
| 6915 | KeyTyped = save_KeyTyped; |
| 6916 | # endif |
| 6917 | |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6918 | # endif |
| 6919 | |
Bram Moolenaar | ccc1822 | 2007-05-10 18:25:20 +0000 | [diff] [blame] | 6920 | /* Restore the command line info. */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6921 | ccline = save_ccline; |
| 6922 | cmdwin_type = 0; |
| 6923 | |
| 6924 | exmode_active = save_exmode; |
| 6925 | |
Bram Moolenaar | f982106 | 2008-06-20 16:31:07 +0000 | [diff] [blame] | 6926 | /* 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] | 6927 | * this happens! */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6928 | if (!win_valid(old_curwin) || !bufref_valid(&old_curbuf)) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6929 | { |
| 6930 | cmdwin_result = Ctrl_C; |
| 6931 | EMSG(_("E199: Active window or buffer deleted")); |
| 6932 | } |
| 6933 | else |
| 6934 | { |
| 6935 | # if defined(FEAT_AUTOCMD) && defined(FEAT_EVAL) |
| 6936 | /* autocmds may abort script processing */ |
| 6937 | if (aborting() && cmdwin_result != K_IGNORE) |
| 6938 | cmdwin_result = Ctrl_C; |
| 6939 | # endif |
| 6940 | /* Set the new command line from the cmdline buffer. */ |
| 6941 | vim_free(ccline.cmdbuff); |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6942 | if (cmdwin_result == K_XF1 || cmdwin_result == K_XF2) /* :qa[!] typed */ |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6943 | { |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 6944 | char *p = (cmdwin_result == K_XF2) ? "qa" : "qa!"; |
| 6945 | |
| 6946 | if (histtype == HIST_CMD) |
| 6947 | { |
| 6948 | /* Execute the command directly. */ |
| 6949 | ccline.cmdbuff = vim_strsave((char_u *)p); |
| 6950 | cmdwin_result = CAR; |
| 6951 | } |
| 6952 | else |
| 6953 | { |
| 6954 | /* First need to cancel what we were doing. */ |
| 6955 | ccline.cmdbuff = NULL; |
| 6956 | stuffcharReadbuff(':'); |
| 6957 | stuffReadbuff((char_u *)p); |
| 6958 | stuffcharReadbuff(CAR); |
| 6959 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6960 | } |
| 6961 | else if (cmdwin_result == K_XF2) /* :qa typed */ |
| 6962 | { |
| 6963 | ccline.cmdbuff = vim_strsave((char_u *)"qa"); |
| 6964 | cmdwin_result = CAR; |
| 6965 | } |
Bram Moolenaar | 9bd1a7e | 2011-05-19 14:50:54 +0200 | [diff] [blame] | 6966 | else if (cmdwin_result == Ctrl_C) |
| 6967 | { |
| 6968 | /* :q or :close, don't execute any command |
| 6969 | * and don't modify the cmd window. */ |
| 6970 | ccline.cmdbuff = NULL; |
| 6971 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6972 | else |
| 6973 | ccline.cmdbuff = vim_strsave(ml_get_curline()); |
| 6974 | if (ccline.cmdbuff == NULL) |
| 6975 | cmdwin_result = Ctrl_C; |
| 6976 | else |
| 6977 | { |
| 6978 | ccline.cmdlen = (int)STRLEN(ccline.cmdbuff); |
| 6979 | ccline.cmdbufflen = ccline.cmdlen + 1; |
| 6980 | ccline.cmdpos = curwin->w_cursor.col; |
| 6981 | if (ccline.cmdpos > ccline.cmdlen) |
| 6982 | ccline.cmdpos = ccline.cmdlen; |
| 6983 | if (cmdwin_result == K_IGNORE) |
| 6984 | { |
| 6985 | set_cmdspos_cursor(); |
| 6986 | redrawcmd(); |
| 6987 | } |
| 6988 | } |
| 6989 | |
| 6990 | # ifdef FEAT_AUTOCMD |
| 6991 | /* Don't execute autocommands while deleting the window. */ |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 6992 | block_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6993 | # endif |
Bram Moolenaar | fa67fbe | 2015-06-25 18:20:36 +0200 | [diff] [blame] | 6994 | # ifdef FEAT_CONCEAL |
| 6995 | /* Avoid command-line window first character being concealed. */ |
| 6996 | curwin->w_p_cole = 0; |
| 6997 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 6998 | wp = curwin; |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 6999 | set_bufref(&bufref, curbuf); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7000 | win_goto(old_curwin); |
| 7001 | win_close(wp, TRUE); |
Bram Moolenaar | 8006d69 | 2010-03-02 17:23:21 +0100 | [diff] [blame] | 7002 | |
| 7003 | /* win_close() may have already wiped the buffer when 'bh' is |
| 7004 | * set to 'wipe' */ |
Bram Moolenaar | 7c0a2f3 | 2016-07-10 22:11:16 +0200 | [diff] [blame] | 7005 | if (bufref_valid(&bufref)) |
| 7006 | close_buffer(NULL, bufref.br_buf, DOBUF_WIPE, FALSE); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7007 | |
| 7008 | /* Restore window sizes. */ |
| 7009 | win_size_restore(&winsizes); |
| 7010 | |
| 7011 | # ifdef FEAT_AUTOCMD |
Bram Moolenaar | 78ab331 | 2007-09-29 12:16:41 +0000 | [diff] [blame] | 7012 | unblock_autocmds(); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7013 | # endif |
| 7014 | } |
| 7015 | |
| 7016 | ga_clear(&winsizes); |
| 7017 | restart_edit = save_restart_edit; |
Bram Moolenaar | 4615234 | 2005-09-07 21:18:43 +0000 | [diff] [blame] | 7018 | # ifdef FEAT_RIGHTLEFT |
| 7019 | cmdmsg_rl = save_cmdmsg_rl; |
| 7020 | # endif |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7021 | |
| 7022 | State = save_State; |
| 7023 | # ifdef FEAT_MOUSE |
| 7024 | setmouse(); |
| 7025 | # endif |
| 7026 | |
| 7027 | return cmdwin_result; |
| 7028 | } |
| 7029 | #endif /* FEAT_CMDWIN */ |
| 7030 | |
| 7031 | /* |
| 7032 | * Used for commands that either take a simple command string argument, or: |
| 7033 | * cmd << endmarker |
| 7034 | * {script} |
| 7035 | * endmarker |
| 7036 | * Returns a pointer to allocated memory with {script} or NULL. |
| 7037 | */ |
| 7038 | char_u * |
Bram Moolenaar | 78c0b7d | 2016-01-30 15:52:46 +0100 | [diff] [blame] | 7039 | script_get(exarg_T *eap, char_u *cmd) |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7040 | { |
| 7041 | char_u *theline; |
| 7042 | char *end_pattern = NULL; |
| 7043 | char dot[] = "."; |
| 7044 | garray_T ga; |
| 7045 | |
| 7046 | if (cmd[0] != '<' || cmd[1] != '<' || eap->getline == NULL) |
| 7047 | return NULL; |
| 7048 | |
| 7049 | ga_init2(&ga, 1, 0x400); |
| 7050 | |
| 7051 | if (cmd[2] != NUL) |
| 7052 | end_pattern = (char *)skipwhite(cmd + 2); |
| 7053 | else |
| 7054 | end_pattern = dot; |
| 7055 | |
| 7056 | for (;;) |
| 7057 | { |
| 7058 | theline = eap->getline( |
| 7059 | #ifdef FEAT_EVAL |
Bram Moolenaar | 1280586 | 2005-01-05 22:16:17 +0000 | [diff] [blame] | 7060 | eap->cstack->cs_looplevel > 0 ? -1 : |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7061 | #endif |
| 7062 | NUL, eap->cookie, 0); |
| 7063 | |
| 7064 | if (theline == NULL || STRCMP(end_pattern, theline) == 0) |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7065 | { |
| 7066 | vim_free(theline); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7067 | break; |
Bram Moolenaar | be555e7 | 2008-08-06 12:19:26 +0000 | [diff] [blame] | 7068 | } |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7069 | |
| 7070 | ga_concat(&ga, theline); |
| 7071 | ga_append(&ga, '\n'); |
| 7072 | vim_free(theline); |
| 7073 | } |
Bram Moolenaar | 269ec65 | 2004-07-29 08:43:53 +0000 | [diff] [blame] | 7074 | ga_append(&ga, NUL); |
Bram Moolenaar | 071d427 | 2004-06-13 20:20:40 +0000 | [diff] [blame] | 7075 | |
| 7076 | return (char_u *)ga.ga_data; |
| 7077 | } |
Bram Moolenaar | 4d6f32c | 2016-08-26 19:13:46 +0200 | [diff] [blame] | 7078 | |
| 7079 | #ifdef FEAT_SEARCH_EXTRA |
| 7080 | static void |
| 7081 | set_search_match(pos_T *t) |
| 7082 | { |
| 7083 | /* |
| 7084 | * First move cursor to end of match, then to the start. This |
| 7085 | * moves the whole match onto the screen when 'nowrap' is set. |
| 7086 | */ |
| 7087 | t->lnum += search_match_lines; |
| 7088 | t->col = search_match_endcol; |
| 7089 | if (t->lnum > curbuf->b_ml.ml_line_count) |
| 7090 | { |
| 7091 | t->lnum = curbuf->b_ml.ml_line_count; |
| 7092 | coladvance((colnr_T)MAXCOL); |
| 7093 | } |
| 7094 | } |
| 7095 | #endif |