Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 1 | /* vi:set ts=8 sts=4 sw=4 noet: |
| 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 | * cmdhist.c: Functions for the history of the command-line. |
| 12 | */ |
| 13 | |
| 14 | #include "vim.h" |
| 15 | |
| 16 | static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; |
| 17 | static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; // lastused entry |
| 18 | static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; |
| 19 | // identifying (unique) number of newest history entry |
| 20 | static int hislen = 0; // actual length of history tables |
| 21 | |
| 22 | /* |
| 23 | * Return the length of the history tables |
| 24 | */ |
| 25 | int |
| 26 | get_hislen(void) |
| 27 | { |
| 28 | return hislen; |
| 29 | } |
| 30 | |
| 31 | /* |
| 32 | * Return a pointer to a specified history table |
| 33 | */ |
| 34 | histentry_T * |
| 35 | get_histentry(int hist_type) |
| 36 | { |
| 37 | return history[hist_type]; |
| 38 | } |
| 39 | |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 40 | #if defined(FEAT_VIMINFO) || defined(PROTO) |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 41 | void |
| 42 | set_histentry(int hist_type, histentry_T *entry) |
| 43 | { |
| 44 | history[hist_type] = entry; |
| 45 | } |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 46 | #endif |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 47 | |
| 48 | int * |
| 49 | get_hisidx(int hist_type) |
| 50 | { |
| 51 | return &hisidx[hist_type]; |
| 52 | } |
| 53 | |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 54 | #if defined(FEAT_VIMINFO) || defined(PROTO) |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 55 | int * |
| 56 | get_hisnum(int hist_type) |
| 57 | { |
| 58 | return &hisnum[hist_type]; |
| 59 | } |
Dominique Pelle | 748b308 | 2022-01-08 12:41:16 +0000 | [diff] [blame] | 60 | #endif |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 61 | |
| 62 | /* |
| 63 | * Translate a history character to the associated type number. |
| 64 | */ |
| 65 | int |
| 66 | hist_char2type(int c) |
| 67 | { |
| 68 | if (c == ':') |
| 69 | return HIST_CMD; |
| 70 | if (c == '=') |
| 71 | return HIST_EXPR; |
| 72 | if (c == '@') |
| 73 | return HIST_INPUT; |
| 74 | if (c == '>') |
| 75 | return HIST_DEBUG; |
| 76 | return HIST_SEARCH; // must be '?' or '/' |
| 77 | } |
| 78 | |
| 79 | /* |
| 80 | * Table of history names. |
| 81 | * These names are used in :history and various hist...() functions. |
| 82 | * It is sufficient to give the significant prefix of a history name. |
| 83 | */ |
| 84 | |
| 85 | static char *(history_names[]) = |
| 86 | { |
| 87 | "cmd", |
| 88 | "search", |
| 89 | "expr", |
| 90 | "input", |
| 91 | "debug", |
| 92 | NULL |
| 93 | }; |
| 94 | |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 95 | /* |
| 96 | * Function given to ExpandGeneric() to obtain the possible first |
| 97 | * arguments of the ":history command. |
| 98 | */ |
| 99 | char_u * |
| 100 | get_history_arg(expand_T *xp UNUSED, int idx) |
| 101 | { |
Bram Moolenaar | 5ff595d | 2022-08-26 22:36:41 +0100 | [diff] [blame] | 102 | char *short_names = ":=@>?/"; |
| 103 | int short_names_count = (int)STRLEN(short_names); |
| 104 | int history_name_count = ARRAY_LENGTH(history_names) - 1; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 105 | |
| 106 | if (idx < short_names_count) |
| 107 | { |
Bram Moolenaar | 5ff595d | 2022-08-26 22:36:41 +0100 | [diff] [blame] | 108 | xp->xp_buf[0] = (char_u)short_names[idx]; |
| 109 | xp->xp_buf[1] = NUL; |
| 110 | return xp->xp_buf; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 111 | } |
| 112 | if (idx < short_names_count + history_name_count) |
| 113 | return (char_u *)history_names[idx - short_names_count]; |
| 114 | if (idx == short_names_count + history_name_count) |
| 115 | return (char_u *)"all"; |
| 116 | return NULL; |
| 117 | } |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 118 | |
| 119 | /* |
| 120 | * init_history() - Initialize the command line history. |
| 121 | * Also used to re-allocate the history when the size changes. |
| 122 | */ |
| 123 | void |
| 124 | init_history(void) |
| 125 | { |
| 126 | int newlen; // new length of history table |
| 127 | histentry_T *temp; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 128 | int type; |
| 129 | |
| 130 | // If size of history table changed, reallocate it |
| 131 | newlen = (int)p_hi; |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 132 | if (newlen == hislen) // history length didn't change |
| 133 | return; |
| 134 | |
| 135 | // history length changed |
| 136 | for (type = 0; type < HIST_COUNT; ++type) // adjust the tables |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 137 | { |
Bram Moolenaar | b298fe6 | 2022-11-14 14:36:41 +0000 | [diff] [blame] | 138 | if (newlen > 0) |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 139 | { |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 140 | temp = ALLOC_MULT(histentry_T, newlen); |
| 141 | if (temp == NULL) // out of memory! |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 142 | { |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 143 | if (type == 0) // first one: just keep the old length |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 144 | { |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 145 | newlen = hislen; |
| 146 | break; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 147 | } |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 148 | // Already changed one table, now we can only have zero |
| 149 | // length for all tables. |
| 150 | newlen = 0; |
| 151 | type = -1; |
| 152 | continue; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 153 | } |
| 154 | } |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 155 | else |
| 156 | temp = NULL; |
| 157 | |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 158 | if (hisidx[type] < 0) // there are no entries yet |
| 159 | { |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 160 | int i; |
| 161 | |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 162 | for (i = 0; i < newlen; ++i) |
| 163 | clear_hist_entry(&temp[i]); |
| 164 | } |
| 165 | else if (newlen > hislen) // array becomes bigger |
| 166 | { |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 167 | int i; |
| 168 | int j; |
| 169 | |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 170 | for (i = 0; i <= hisidx[type]; ++i) |
| 171 | temp[i] = history[type][i]; |
| 172 | j = i; |
| 173 | for ( ; i <= newlen - (hislen - hisidx[type]); ++i) |
| 174 | clear_hist_entry(&temp[i]); |
| 175 | for ( ; j < hislen; ++i, ++j) |
| 176 | temp[i] = history[type][j]; |
| 177 | } |
| 178 | else // array becomes smaller or 0 |
| 179 | { |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 180 | int i; |
| 181 | int j; |
| 182 | |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 183 | j = hisidx[type]; |
| 184 | for (i = newlen - 1; ; --i) |
| 185 | { |
| 186 | if (i >= 0) // copy newest entries |
| 187 | temp[i] = history[type][j]; |
| 188 | else // remove older entries |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 189 | { |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 190 | vim_free(history[type][j].hisstr); |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 191 | history[type][j].hisstrlen = 0; |
| 192 | } |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 193 | if (--j < 0) |
| 194 | j = hislen - 1; |
| 195 | if (j == hisidx[type]) |
| 196 | break; |
| 197 | } |
| 198 | hisidx[type] = newlen - 1; |
| 199 | } |
| 200 | vim_free(history[type]); |
| 201 | history[type] = temp; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 202 | } |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 203 | hislen = newlen; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void |
| 207 | clear_hist_entry(histentry_T *hisptr) |
| 208 | { |
| 209 | hisptr->hisnum = 0; |
| 210 | hisptr->viminfo = FALSE; |
| 211 | hisptr->hisstr = NULL; |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 212 | hisptr->hisstrlen = 0; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 213 | hisptr->time_set = 0; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * Check if command line 'str' is already in history. |
| 218 | * If 'move_to_front' is TRUE, matching entry is moved to end of history. |
| 219 | */ |
| 220 | int |
| 221 | in_history( |
| 222 | int type, |
| 223 | char_u *str, |
| 224 | int move_to_front, // Move the entry to the front if it exists |
| 225 | int sep, |
| 226 | int writing) // ignore entries read from viminfo |
| 227 | { |
| 228 | int i; |
| 229 | int last_i = -1; |
| 230 | char_u *p; |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 231 | size_t len; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 232 | |
| 233 | if (hisidx[type] < 0) |
| 234 | return FALSE; |
| 235 | i = hisidx[type]; |
| 236 | do |
| 237 | { |
| 238 | if (history[type][i].hisstr == NULL) |
| 239 | return FALSE; |
| 240 | |
| 241 | // For search history, check that the separator character matches as |
| 242 | // well. |
| 243 | p = history[type][i].hisstr; |
| 244 | if (STRCMP(str, p) == 0 |
| 245 | && !(writing && history[type][i].viminfo) |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 246 | && (type != HIST_SEARCH || sep == p[history[type][i].hisstrlen + 1])) |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 247 | { |
| 248 | if (!move_to_front) |
| 249 | return TRUE; |
| 250 | last_i = i; |
| 251 | break; |
| 252 | } |
| 253 | if (--i < 0) |
| 254 | i = hislen - 1; |
| 255 | } while (i != hisidx[type]); |
| 256 | |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 257 | if (last_i < 0) |
| 258 | return FALSE; |
| 259 | |
| 260 | str = history[type][i].hisstr; |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 261 | len = history[type][i].hisstrlen; |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 262 | while (i != hisidx[type]) |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 263 | { |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 264 | if (++i >= hislen) |
| 265 | i = 0; |
| 266 | history[type][last_i] = history[type][i]; |
| 267 | last_i = i; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 268 | } |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 269 | history[type][i].hisnum = ++hisnum[type]; |
| 270 | history[type][i].viminfo = FALSE; |
| 271 | history[type][i].hisstr = str; |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 272 | history[type][i].hisstrlen = len; |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 273 | history[type][i].time_set = vim_time(); |
| 274 | return TRUE; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | /* |
| 278 | * Convert history name (from table above) to its HIST_ equivalent. |
| 279 | * When "name" is empty, return "cmd" history. |
| 280 | * Returns -1 for unknown history name. |
| 281 | */ |
| 282 | static int |
| 283 | get_histtype(char_u *name) |
| 284 | { |
| 285 | int i; |
| 286 | int len = (int)STRLEN(name); |
| 287 | |
| 288 | // No argument: use current history. |
| 289 | if (len == 0) |
| 290 | return hist_char2type(get_cmdline_firstc()); |
| 291 | |
| 292 | for (i = 0; history_names[i] != NULL; ++i) |
| 293 | if (STRNICMP(name, history_names[i], len) == 0) |
| 294 | return i; |
| 295 | |
| 296 | if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) |
| 297 | return hist_char2type(name[0]); |
| 298 | |
| 299 | return -1; |
| 300 | } |
| 301 | |
| 302 | static int last_maptick = -1; // last seen maptick |
| 303 | |
| 304 | /* |
| 305 | * Add the given string to the given history. If the string is already in the |
| 306 | * history then it is moved to the front. "histype" may be one of he HIST_ |
| 307 | * values. |
| 308 | */ |
| 309 | void |
| 310 | add_to_history( |
| 311 | int histype, |
| 312 | char_u *new_entry, |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 313 | size_t new_entrylen, |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 314 | int in_map, // consider maptick when inside a mapping |
| 315 | int sep) // separator character used (search hist) |
| 316 | { |
| 317 | histentry_T *hisptr; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 318 | |
| 319 | if (hislen == 0) // no history |
| 320 | return; |
| 321 | |
Bram Moolenaar | e100440 | 2020-10-24 20:49:43 +0200 | [diff] [blame] | 322 | if ((cmdmod.cmod_flags & CMOD_KEEPPATTERNS) && histype == HIST_SEARCH) |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 323 | return; |
| 324 | |
| 325 | // Searches inside the same mapping overwrite each other, so that only |
| 326 | // the last line is kept. Be careful not to remove a line that was moved |
| 327 | // down, only lines that were added. |
| 328 | if (histype == HIST_SEARCH && in_map) |
| 329 | { |
| 330 | if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0) |
| 331 | { |
| 332 | // Current line is from the same mapping, remove it |
| 333 | hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; |
| 334 | vim_free(hisptr->hisstr); |
| 335 | clear_hist_entry(hisptr); |
| 336 | --hisnum[histype]; |
| 337 | if (--hisidx[HIST_SEARCH] < 0) |
| 338 | hisidx[HIST_SEARCH] = hislen - 1; |
| 339 | } |
| 340 | last_maptick = -1; |
| 341 | } |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 342 | |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 343 | if (in_history(histype, new_entry, TRUE, sep, FALSE)) |
| 344 | return; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 345 | |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 346 | if (++hisidx[histype] == hislen) |
| 347 | hisidx[histype] = 0; |
| 348 | hisptr = &history[histype][hisidx[histype]]; |
| 349 | vim_free(hisptr->hisstr); |
| 350 | |
| 351 | // Store the separator after the NUL of the string. |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 352 | hisptr->hisstr = vim_strnsave(new_entry, new_entrylen + 2); |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 353 | if (hisptr->hisstr == NULL) |
| 354 | hisptr->hisstrlen = 0; |
| 355 | else |
| 356 | { |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 357 | hisptr->hisstr[new_entrylen + 1] = sep; |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 358 | hisptr->hisstrlen = new_entrylen; |
| 359 | } |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 360 | |
| 361 | hisptr->hisnum = ++hisnum[histype]; |
| 362 | hisptr->viminfo = FALSE; |
| 363 | hisptr->time_set = vim_time(); |
| 364 | if (histype == HIST_SEARCH && in_map) |
| 365 | last_maptick = maptick; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 366 | } |
| 367 | |
| 368 | #if defined(FEAT_EVAL) || defined(PROTO) |
| 369 | |
| 370 | /* |
| 371 | * Get identifier of newest history entry. |
| 372 | * "histype" may be one of the HIST_ values. |
| 373 | */ |
| 374 | static int |
| 375 | get_history_idx(int histype) |
| 376 | { |
| 377 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 378 | || hisidx[histype] < 0) |
| 379 | return -1; |
| 380 | |
| 381 | return history[histype][hisidx[histype]].hisnum; |
| 382 | } |
| 383 | |
| 384 | /* |
| 385 | * Calculate history index from a number: |
| 386 | * num > 0: seen as identifying number of a history entry |
| 387 | * num < 0: relative position in history wrt newest entry |
| 388 | * "histype" may be one of the HIST_ values. |
| 389 | */ |
| 390 | static int |
| 391 | calc_hist_idx(int histype, int num) |
| 392 | { |
| 393 | int i; |
| 394 | histentry_T *hist; |
| 395 | int wrapped = FALSE; |
| 396 | |
| 397 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT |
| 398 | || (i = hisidx[histype]) < 0 || num == 0) |
| 399 | return -1; |
| 400 | |
| 401 | hist = history[histype]; |
| 402 | if (num > 0) |
| 403 | { |
| 404 | while (hist[i].hisnum > num) |
| 405 | if (--i < 0) |
| 406 | { |
| 407 | if (wrapped) |
| 408 | break; |
| 409 | i += hislen; |
| 410 | wrapped = TRUE; |
| 411 | } |
Bram Moolenaar | 8d588cc | 2020-02-25 21:47:45 +0100 | [diff] [blame] | 412 | if (i >= 0 && hist[i].hisnum == num && hist[i].hisstr != NULL) |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 413 | return i; |
| 414 | } |
| 415 | else if (-num <= hislen) |
| 416 | { |
| 417 | i += num + 1; |
| 418 | if (i < 0) |
| 419 | i += hislen; |
| 420 | if (hist[i].hisstr != NULL) |
| 421 | return i; |
| 422 | } |
| 423 | return -1; |
| 424 | } |
| 425 | |
| 426 | /* |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 427 | * Clear all entries of a history. |
| 428 | * "histype" may be one of the HIST_ values. |
| 429 | */ |
| 430 | static int |
| 431 | clr_history(int histype) |
| 432 | { |
| 433 | int i; |
| 434 | histentry_T *hisptr; |
| 435 | |
| 436 | if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) |
| 437 | { |
| 438 | hisptr = history[histype]; |
| 439 | for (i = hislen; i--;) |
| 440 | { |
| 441 | vim_free(hisptr->hisstr); |
| 442 | clear_hist_entry(hisptr); |
| 443 | hisptr++; |
| 444 | } |
| 445 | hisidx[histype] = -1; // mark history as cleared |
| 446 | hisnum[histype] = 0; // reset identifier counter |
| 447 | return OK; |
| 448 | } |
| 449 | return FAIL; |
| 450 | } |
| 451 | |
| 452 | /* |
| 453 | * Remove all entries matching {str} from a history. |
| 454 | * "histype" may be one of the HIST_ values. |
| 455 | */ |
| 456 | static int |
| 457 | del_history_entry(int histype, char_u *str) |
| 458 | { |
| 459 | regmatch_T regmatch; |
| 460 | histentry_T *hisptr; |
| 461 | int idx; |
| 462 | int i; |
| 463 | int last; |
| 464 | int found = FALSE; |
| 465 | |
Yegappan Lakshmanan | 465de3a | 2022-12-26 12:50:04 +0000 | [diff] [blame] | 466 | if (hislen == 0 || histype < 0 || histype >= HIST_COUNT || *str == NUL |
| 467 | || hisidx[histype] < 0) |
| 468 | return FALSE; |
| 469 | |
| 470 | idx = hisidx[histype]; |
| 471 | regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING); |
| 472 | if (regmatch.regprog == NULL) |
| 473 | return FALSE; |
| 474 | |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 475 | regmatch.rm_ic = FALSE; // always match case |
Yegappan Lakshmanan | 465de3a | 2022-12-26 12:50:04 +0000 | [diff] [blame] | 476 | |
| 477 | i = last = idx; |
| 478 | do |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 479 | { |
Yegappan Lakshmanan | 465de3a | 2022-12-26 12:50:04 +0000 | [diff] [blame] | 480 | hisptr = &history[histype][i]; |
| 481 | if (hisptr->hisstr == NULL) |
| 482 | break; |
| 483 | if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 484 | { |
Yegappan Lakshmanan | 465de3a | 2022-12-26 12:50:04 +0000 | [diff] [blame] | 485 | found = TRUE; |
| 486 | vim_free(hisptr->hisstr); |
| 487 | clear_hist_entry(hisptr); |
| 488 | } |
| 489 | else |
| 490 | { |
| 491 | if (i != last) |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 492 | { |
Yegappan Lakshmanan | 465de3a | 2022-12-26 12:50:04 +0000 | [diff] [blame] | 493 | history[histype][last] = *hisptr; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 494 | clear_hist_entry(hisptr); |
| 495 | } |
Yegappan Lakshmanan | 465de3a | 2022-12-26 12:50:04 +0000 | [diff] [blame] | 496 | if (--last < 0) |
| 497 | last += hislen; |
| 498 | } |
| 499 | if (--i < 0) |
| 500 | i += hislen; |
| 501 | } while (i != idx); |
| 502 | |
| 503 | if (history[histype][idx].hisstr == NULL) |
| 504 | hisidx[histype] = -1; |
| 505 | |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 506 | vim_regfree(regmatch.regprog); |
| 507 | return found; |
| 508 | } |
| 509 | |
| 510 | /* |
| 511 | * Remove an indexed entry from a history. |
| 512 | * "histype" may be one of the HIST_ values. |
| 513 | */ |
| 514 | static int |
| 515 | del_history_idx(int histype, int idx) |
| 516 | { |
| 517 | int i, j; |
| 518 | |
| 519 | i = calc_hist_idx(histype, idx); |
| 520 | if (i < 0) |
| 521 | return FALSE; |
| 522 | idx = hisidx[histype]; |
| 523 | vim_free(history[histype][i].hisstr); |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 524 | history[histype][i].hisstrlen = 0; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 525 | |
| 526 | // When deleting the last added search string in a mapping, reset |
| 527 | // last_maptick, so that the last added search string isn't deleted again. |
| 528 | if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) |
| 529 | last_maptick = -1; |
| 530 | |
| 531 | while (i != idx) |
| 532 | { |
| 533 | j = (i + 1) % hislen; |
| 534 | history[histype][i] = history[histype][j]; |
| 535 | i = j; |
| 536 | } |
| 537 | clear_hist_entry(&history[histype][i]); |
| 538 | if (--i < 0) |
| 539 | i += hislen; |
| 540 | hisidx[histype] = i; |
| 541 | return TRUE; |
| 542 | } |
| 543 | |
| 544 | /* |
| 545 | * "histadd()" function |
| 546 | */ |
| 547 | void |
| 548 | f_histadd(typval_T *argvars UNUSED, typval_T *rettv) |
| 549 | { |
| 550 | int histype; |
| 551 | char_u *str; |
| 552 | char_u buf[NUMBUFLEN]; |
| 553 | |
| 554 | rettv->vval.v_number = FALSE; |
| 555 | if (check_secure()) |
| 556 | return; |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 557 | |
| 558 | if (in_vim9script() |
| 559 | && (check_for_string_arg(argvars, 0) == FAIL |
| 560 | || check_for_string_arg(argvars, 1) == FAIL)) |
| 561 | return; |
| 562 | |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 563 | str = tv_get_string_chk(&argvars[0]); // NULL on type error |
| 564 | histype = str != NULL ? get_histtype(str) : -1; |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 565 | if (histype < 0) |
| 566 | return; |
| 567 | |
| 568 | str = tv_get_string_buf(&argvars[1], buf); |
| 569 | if (*str == NUL) |
| 570 | return; |
| 571 | |
| 572 | init_history(); |
John Marriott | 8c85a2a | 2024-05-20 19:18:26 +0200 | [diff] [blame] | 573 | add_to_history(histype, str, STRLEN(str), FALSE, NUL); |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 574 | rettv->vval.v_number = TRUE; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | /* |
| 578 | * "histdel()" function |
| 579 | */ |
| 580 | void |
| 581 | f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED) |
| 582 | { |
| 583 | int n; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 584 | char_u *str; |
| 585 | |
Yegappan Lakshmanan | 0ad871d | 2021-07-23 20:37:56 +0200 | [diff] [blame] | 586 | if (in_vim9script() |
| 587 | && (check_for_string_arg(argvars, 0) == FAIL |
| 588 | || check_for_opt_string_or_number_arg(argvars, 1) == FAIL)) |
| 589 | return; |
| 590 | |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 591 | str = tv_get_string_chk(&argvars[0]); // NULL on type error |
| 592 | if (str == NULL) |
| 593 | n = 0; |
| 594 | else if (argvars[1].v_type == VAR_UNKNOWN) |
| 595 | // only one argument: clear entire history |
| 596 | n = clr_history(get_histtype(str)); |
| 597 | else if (argvars[1].v_type == VAR_NUMBER) |
| 598 | // index given: remove that entry |
| 599 | n = del_history_idx(get_histtype(str), |
| 600 | (int)tv_get_number(&argvars[1])); |
| 601 | else |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 602 | { |
| 603 | char_u buf[NUMBUFLEN]; |
| 604 | |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 605 | // string given: remove all matching entries |
| 606 | n = del_history_entry(get_histtype(str), |
| 607 | tv_get_string_buf(&argvars[1], buf)); |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 608 | } |
| 609 | |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 610 | rettv->vval.v_number = n; |
| 611 | } |
| 612 | |
| 613 | /* |
| 614 | * "histget()" function |
| 615 | */ |
| 616 | void |
| 617 | f_histget(typval_T *argvars UNUSED, typval_T *rettv) |
| 618 | { |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 619 | char_u *str; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 620 | |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 621 | if (in_vim9script() |
| 622 | && (check_for_string_arg(argvars, 0) == FAIL |
Yegappan Lakshmanan | 83494b4 | 2021-07-20 17:51:51 +0200 | [diff] [blame] | 623 | || check_for_opt_number_arg(argvars, 1) == FAIL)) |
Yegappan Lakshmanan | 1a71d31 | 2021-07-15 12:49:58 +0200 | [diff] [blame] | 624 | return; |
| 625 | |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 626 | str = tv_get_string_chk(&argvars[0]); // NULL on type error |
| 627 | if (str == NULL) |
| 628 | rettv->vval.v_string = NULL; |
| 629 | else |
| 630 | { |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 631 | int type; |
| 632 | int idx; |
| 633 | |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 634 | type = get_histtype(str); |
| 635 | if (argvars[1].v_type == VAR_UNKNOWN) |
| 636 | idx = get_history_idx(type); |
| 637 | else |
| 638 | idx = (int)tv_get_number_chk(&argvars[1], NULL); |
| 639 | // -1 on type error |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 640 | |
| 641 | idx = calc_hist_idx(type, idx); |
| 642 | if (idx < 0) |
| 643 | rettv->vval.v_string = vim_strnsave((char_u *)"", 0); |
| 644 | else |
| 645 | rettv->vval.v_string = vim_strnsave(history[type][idx].hisstr, history[type][idx].hisstrlen); |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 646 | } |
| 647 | rettv->v_type = VAR_STRING; |
| 648 | } |
| 649 | |
| 650 | /* |
| 651 | * "histnr()" function |
| 652 | */ |
| 653 | void |
| 654 | f_histnr(typval_T *argvars UNUSED, typval_T *rettv) |
| 655 | { |
| 656 | int i; |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 657 | char_u *histname; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 658 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 659 | if (in_vim9script() && check_for_string_arg(argvars, 0) == FAIL) |
| 660 | return; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 661 | |
Yegappan Lakshmanan | 4490ec4 | 2021-07-27 22:00:44 +0200 | [diff] [blame] | 662 | histname = tv_get_string_chk(&argvars[0]); |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 663 | i = histname == NULL ? HIST_CMD - 1 : get_histtype(histname); |
| 664 | if (i >= HIST_CMD && i < HIST_COUNT) |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 665 | rettv->vval.v_number = get_history_idx(i); |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 666 | else |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 667 | rettv->vval.v_number = -1; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 668 | } |
| 669 | #endif // FEAT_EVAL |
| 670 | |
| 671 | #if defined(FEAT_CRYPT) || defined(PROTO) |
| 672 | /* |
| 673 | * Very specific function to remove the value in ":set key=val" from the |
| 674 | * history. |
| 675 | */ |
| 676 | void |
| 677 | remove_key_from_history(void) |
| 678 | { |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 679 | char_u *p_start; |
| 680 | char_u *p_end; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 681 | char_u *p; |
| 682 | int i; |
| 683 | |
| 684 | i = hisidx[HIST_CMD]; |
| 685 | if (i < 0) |
| 686 | return; |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 687 | p_start = history[HIST_CMD][i].hisstr; |
| 688 | if (p_start == NULL) |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 689 | return; |
| 690 | |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 691 | p_end = p_start + history[HIST_CMD][i].hisstrlen; |
| 692 | for (p = p_start; *p; ++p) |
| 693 | { |
Keith Thompson | 184f71c | 2024-01-04 21:19:04 +0100 | [diff] [blame] | 694 | if (STRNCMP(p, "key", 3) == 0 && !SAFE_isalpha(p[3])) |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 695 | { |
| 696 | p = vim_strchr(p + 3, '='); |
| 697 | if (p == NULL) |
| 698 | break; |
| 699 | ++p; |
| 700 | for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i) |
| 701 | if (p[i] == '\\' && p[i + 1]) |
| 702 | ++i; |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 703 | |
| 704 | mch_memmove(p, p + i, (p_end - (p + i)) + 1); // +1 for the NUL |
| 705 | p_end -= i; // adjust p_end for shortened string |
Yegappan Lakshmanan | 623e94e | 2022-11-13 18:11:17 +0000 | [diff] [blame] | 706 | --p; |
| 707 | } |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 708 | } |
| 709 | |
| 710 | history[HIST_CMD][i].hisstrlen = (size_t)(p_end - p_start); |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 711 | } |
| 712 | #endif |
| 713 | |
| 714 | /* |
| 715 | * :history command - print a history |
| 716 | */ |
| 717 | void |
| 718 | ex_history(exarg_T *eap) |
| 719 | { |
| 720 | histentry_T *hist; |
| 721 | int histype1 = HIST_CMD; |
| 722 | int histype2 = HIST_CMD; |
| 723 | int hisidx1 = 1; |
| 724 | int hisidx2 = -1; |
| 725 | int idx; |
| 726 | int i, j, k; |
| 727 | char_u *end; |
| 728 | char_u *arg = eap->arg; |
| 729 | |
| 730 | if (hislen == 0) |
| 731 | { |
| 732 | msg(_("'history' option is zero")); |
| 733 | return; |
| 734 | } |
| 735 | |
| 736 | if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) |
| 737 | { |
| 738 | end = arg; |
| 739 | while (ASCII_ISALPHA(*end) |
| 740 | || vim_strchr((char_u *)":=@>/?", *end) != NULL) |
| 741 | end++; |
| 742 | i = *end; |
| 743 | *end = NUL; |
| 744 | histype1 = get_histtype(arg); |
| 745 | if (histype1 == -1) |
| 746 | { |
| 747 | if (STRNICMP(arg, "all", STRLEN(arg)) == 0) |
| 748 | { |
| 749 | histype1 = 0; |
| 750 | histype2 = HIST_COUNT-1; |
| 751 | } |
| 752 | else |
| 753 | { |
| 754 | *end = i; |
Bram Moolenaar | 74409f6 | 2022-01-01 15:58:22 +0000 | [diff] [blame] | 755 | semsg(_(e_trailing_characters_str), arg); |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 756 | return; |
| 757 | } |
| 758 | } |
| 759 | else |
| 760 | histype2 = histype1; |
| 761 | *end = i; |
| 762 | } |
| 763 | else |
| 764 | end = arg; |
| 765 | if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) |
| 766 | { |
Christian Brabandt | 9198c1f | 2023-10-26 21:29:32 +0200 | [diff] [blame] | 767 | if (*end != NUL) |
| 768 | semsg(_(e_trailing_characters_str), end); |
| 769 | else |
| 770 | semsg(_(e_val_too_large), arg); |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 771 | return; |
| 772 | } |
| 773 | |
| 774 | for (; !got_int && histype1 <= histype2; ++histype1) |
| 775 | { |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 776 | vim_snprintf((char *)IObuff, IOSIZE, "\n # %s history", history_names[histype1]); |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 777 | msg_puts_title((char *)IObuff); |
| 778 | idx = hisidx[histype1]; |
| 779 | hist = history[histype1]; |
| 780 | j = hisidx1; |
| 781 | k = hisidx2; |
| 782 | if (j < 0) |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 783 | j = (-j > hislen) ? 0 : hist[(hislen + j + idx + 1) % hislen].hisnum; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 784 | if (k < 0) |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 785 | k = (-k > hislen) ? 0 : hist[(hislen + k + idx + 1) % hislen].hisnum; |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 786 | if (idx >= 0 && j <= k) |
| 787 | for (i = idx + 1; !got_int; ++i) |
| 788 | { |
| 789 | if (i == hislen) |
| 790 | i = 0; |
| 791 | if (hist[i].hisstr != NULL |
Christian Brabandt | 42a5b5a | 2024-05-24 07:39:34 +0200 | [diff] [blame] | 792 | && hist[i].hisnum >= j && hist[i].hisnum <= k |
| 793 | && !message_filtered(hist[i].hisstr)) |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 794 | { |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 795 | int len; |
| 796 | |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 797 | msg_putchar('\n'); |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 798 | len = vim_snprintf((char *)IObuff, IOSIZE, |
| 799 | "%c%6d ", i == idx ? '>' : ' ', hist[i].hisnum); |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 800 | if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 801 | trunc_string(hist[i].hisstr, IObuff + len, |
| 802 | (int)Columns - 10, IOSIZE - (int)len); |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 803 | else |
John Marriott | 8df07d0 | 2024-10-21 22:37:07 +0200 | [diff] [blame] | 804 | STRCPY(IObuff + len, hist[i].hisstr); |
Bram Moolenaar | d7663c2 | 2019-08-06 21:59:57 +0200 | [diff] [blame] | 805 | msg_outtrans(IObuff); |
| 806 | out_flush(); |
| 807 | } |
| 808 | if (i == idx) |
| 809 | break; |
| 810 | } |
| 811 | } |
| 812 | } |