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