diff options
author | Bram Moolenaar <Bram@vim.org> | 2019-08-06 21:59:57 +0200 |
---|---|---|
committer | Bram Moolenaar <Bram@vim.org> | 2019-08-06 21:59:57 +0200 |
commit | d7663c22c6c1ff0f86b81371586fbc851d3a3e9e (patch) | |
tree | 3979ae5a710ae9ca6fa4dcecde113961eca54cc1 | |
parent | 0acae7acc40b9f12bff88d5e1dae494a761fec07 (diff) |
patch 8.1.1823: command line history code is spread outv8.1.1823
Problem: Command line history code is spread out.
Solution: Put the code in a new file. (Yegappan Lakshmanan, closes #4779)
Also graduate the +cmdline_hist feature.
-rw-r--r-- | Filelist | 1 | ||||
-rw-r--r-- | src/Make_cyg_ming.mak | 1 | ||||
-rw-r--r-- | src/Make_morph.mak | 1 | ||||
-rw-r--r-- | src/Make_mvc.mak | 4 | ||||
-rw-r--r-- | src/Make_vms.mms | 8 | ||||
-rw-r--r-- | src/Makefile | 10 | ||||
-rw-r--r-- | src/README.md | 2 | ||||
-rw-r--r-- | src/cmdhist.c | 762 | ||||
-rw-r--r-- | src/evalfunc.c | 117 | ||||
-rw-r--r-- | src/ex_cmds.c | 4 | ||||
-rw-r--r-- | src/ex_docmd.c | 8 | ||||
-rw-r--r-- | src/ex_getln.c | 735 | ||||
-rw-r--r-- | src/feature.h | 7 | ||||
-rw-r--r-- | src/globals.h | 2 | ||||
-rw-r--r-- | src/misc2.c | 4 | ||||
-rw-r--r-- | src/normal.c | 6 | ||||
-rw-r--r-- | src/ops.c | 2 | ||||
-rw-r--r-- | src/option.c | 7 | ||||
-rw-r--r-- | src/proto.h | 1 | ||||
-rw-r--r-- | src/proto/cmdhist.pro | 19 | ||||
-rw-r--r-- | src/proto/ex_getln.pro | 19 | ||||
-rw-r--r-- | src/search.c | 2 | ||||
-rw-r--r-- | src/tag.c | 6 | ||||
-rw-r--r-- | src/usercmd.c | 2 | ||||
-rw-r--r-- | src/version.c | 6 | ||||
-rw-r--r-- | src/viminfo.c | 10 |
26 files changed, 852 insertions, 894 deletions
@@ -23,6 +23,7 @@ SRC_ALL = \ src/change.c \ src/channel.c \ src/charset.c \ + src/cmdhist.c \ src/crypt.c \ src/crypt_zip.c \ src/debugger.c \ diff --git a/src/Make_cyg_ming.mak b/src/Make_cyg_ming.mak index 2fb4bf86d2..c073b5f748 100644 --- a/src/Make_cyg_ming.mak +++ b/src/Make_cyg_ming.mak @@ -710,6 +710,7 @@ OBJ = \ $(OUTDIR)/buffer.o \ $(OUTDIR)/change.o \ $(OUTDIR)/charset.o \ + $(OUTDIR)/cmdhist.o \ $(OUTDIR)/crypt.o \ $(OUTDIR)/crypt_zip.o \ $(OUTDIR)/debugger.o \ diff --git a/src/Make_morph.mak b/src/Make_morph.mak index f75afd3079..7616ce23b9 100644 --- a/src/Make_morph.mak +++ b/src/Make_morph.mak @@ -30,6 +30,7 @@ SRC = arabic.c \ buffer.c \ change.c \ charset.c \ + cmdhist.c \ crypt.c \ crypt_zip.c \ debugger.c \ diff --git a/src/Make_mvc.mak b/src/Make_mvc.mak index 6b6d073fa0..07ea52abdc 100644 --- a/src/Make_mvc.mak +++ b/src/Make_mvc.mak @@ -719,6 +719,7 @@ OBJ = \ $(OUTDIR)\buffer.obj \ $(OUTDIR)\change.obj \ $(OUTDIR)\charset.obj \ + $(OUTDIR)\cmdhist.obj \ $(OUTDIR)\crypt.obj \ $(OUTDIR)\crypt_zip.obj \ $(OUTDIR)\debugger.obj \ @@ -1446,6 +1447,8 @@ $(OUTDIR)/change.obj: $(OUTDIR) change.c $(INCL) $(OUTDIR)/charset.obj: $(OUTDIR) charset.c $(INCL) +$(OUTDIR)/cmdhist.obj: $(OUTDIR) cmdhist.c $(INCL) + $(OUTDIR)/crypt.obj: $(OUTDIR) crypt.c $(INCL) $(OUTDIR)/crypt_zip.obj: $(OUTDIR) crypt_zip.c $(INCL) @@ -1747,6 +1750,7 @@ proto.h: \ proto/buffer.pro \ proto/change.pro \ proto/charset.pro \ + proto/cmdhist.pro \ proto/crypt.pro \ proto/crypt_zip.pro \ proto/debugger.pro \ diff --git a/src/Make_vms.mms b/src/Make_vms.mms index 39957491b5..4e55ad49e5 100644 --- a/src/Make_vms.mms +++ b/src/Make_vms.mms @@ -308,7 +308,7 @@ ALL_LIBS = $(LIBS) $(GUI_LIB_DIR) $(GUI_LIB) \ $(PERL_LIB) $(PYTHON_LIB) $(TCL_LIB) $(RUBY_LIB) SRC = arabic.c autocmd.c beval.c blob.c blowfish.c buffer.c change.c charset.c \ - crypt.c crypt_zip.c debugger.c dict.c diff.c digraph.c edit.c eval.c \ + cmdhist.c crypt.c crypt_zip.c debugger.c dict.c diff.c digraph.c edit.c eval.c \ evalfunc.c ex_cmds.c ex_cmds2.c ex_docmd.c ex_eval.c ex_getln.c \ if_cscope.c if_xcmdsrv.c fileio.c findfile.c fold.c getchar.c \ hardcopy.c hashtab.c highlight.c indent.c insexpand.c json.c list.c \ @@ -322,7 +322,7 @@ SRC = arabic.c autocmd.c beval.c blob.c blowfish.c buffer.c change.c charset.c \ $(RUBY_SRC) $(HANGULIN_SRC) $(MZSCH_SRC) $(XDIFF_SRC) OBJ = arabic.obj autocmd.obj beval.obj blob.obj blowfish.obj buffer.obj change.obj \ - charset.obj crypt.obj crypt_zip.obj debugger.obj dict.obj diff.obj \ + charset.obj cmdhist.obj crypt.obj crypt_zip.obj debugger.obj dict.obj diff.obj \ digraph.obj edit.obj eval.obj evalfunc.obj ex_cmds.obj ex_cmds2.obj \ ex_docmd.obj ex_eval.obj ex_getln.obj if_cscope.obj if_xcmdsrv.obj \ fileio.obj findfile.obj fold.obj getchar.obj hardcopy.obj hashtab.obj \ @@ -520,6 +520,10 @@ charset.obj : charset.c vim.h [.auto]config.h feature.h os_unix.h \ ascii.h keymap.h term.h macros.h structs.h regexp.h \ gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \ globals.h +cmdhist.obj : cmdhist.c vim.h [.auto]config.h feature.h os_unix.h \ + ascii.h keymap.h term.h macros.h structs.h regexp.h \ + gui.h beval.h [.proto]gui_beval.pro option.h ex_cmds.h proto.h \ + globals.h crypt.obj : crypt.c vim.h [.auto]config.h feature.h os_unix.h \ ascii.h keymap.h term.h macros.h option.h structs.h regexp.h gui.h \ beval.h [.proto]gui_beval.pro alloc.h ex_cmds.h spell.h proto.h \ diff --git a/src/Makefile b/src/Makefile index bcfc5132a9..9eff349179 100644 --- a/src/Makefile +++ b/src/Makefile @@ -1584,6 +1584,7 @@ BASIC_SRC = \ buffer.c \ change.c \ charset.c \ + cmdhist.c \ crypt.c \ crypt_zip.c \ debugger.c \ @@ -1709,6 +1710,7 @@ OBJ_COMMON = \ objects/change.o \ objects/blob.o \ objects/blowfish.o \ + objects/cmdhist.o \ objects/crypt.o \ objects/crypt_zip.o \ objects/debugger.o \ @@ -1847,6 +1849,7 @@ PRO_AUTO = \ buffer.pro \ change.pro \ charset.pro \ + cmdhist.pro \ crypt.pro \ crypt_zip.pro \ debugger.pro \ @@ -3004,6 +3007,9 @@ objects/change.o: change.c objects/charset.o: charset.c $(CCC) -o $@ charset.c +objects/cmdhist.o: cmdhist.c + $(CCC) -o $@ cmdhist.c + objects/crypt.o: crypt.c $(CCC) -o $@ crypt.c @@ -3498,6 +3504,10 @@ objects/charset.o: charset.c vim.h protodef.h auto/config.h feature.h os_unix.h auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \ proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \ proto.h globals.h +objects/cmdhist.o: cmdhist.c vim.h protodef.h auto/config.h feature.h os_unix.h \ + auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \ + proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \ + proto.h globals.h objects/crypt.o: crypt.c vim.h protodef.h auto/config.h feature.h os_unix.h \ auto/osdef.h ascii.h keymap.h term.h macros.h option.h beval.h \ proto/gui_beval.pro structs.h regexp.h gui.h alloc.h ex_cmds.h spell.h \ diff --git a/src/README.md b/src/README.md index c02a69648e..2e774907ef 100644 --- a/src/README.md +++ b/src/README.md @@ -24,8 +24,10 @@ Most code can be found in a file with an obvious name (incomplete list): File name | Description --------------- | ----------- autocmd.c | autocommands +blob.c | blob data type buffer.c | manipulating buffers (loaded files) change.c | handling changes to text +cmdhist.c | command-line history debugger.c | vim script debugger diff.c | diff mode (vimdiff) eval.c | expression evaluation diff --git a/src/cmdhist.c b/src/cmdhist.c new file mode 100644 index 0000000000..72b399821f --- /dev/null +++ b/src/cmdhist.c @@ -0,0 +1,762 @@ +/* vi:set ts=8 sts=4 sw=4 noet: + * + * VIM - Vi IMproved by Bram Moolenaar + * + * Do ":help uganda" in Vim to read copying and usage conditions. + * Do ":help credits" in Vim to see a list of people who contributed. + * See README.txt for an overview of the Vim source code. + */ + +/* + * cmdhist.c: Functions for the history of the command-line. + */ + +#include "vim.h" + +static histentry_T *(history[HIST_COUNT]) = {NULL, NULL, NULL, NULL, NULL}; +static int hisidx[HIST_COUNT] = {-1, -1, -1, -1, -1}; // lastused entry +static int hisnum[HIST_COUNT] = {0, 0, 0, 0, 0}; + // identifying (unique) number of newest history entry +static int hislen = 0; // actual length of history tables + +/* + * Return the length of the history tables + */ + int +get_hislen(void) +{ + return hislen; +} + +/* + * Return a pointer to a specified history table + */ + histentry_T * +get_histentry(int hist_type) +{ + return history[hist_type]; +} + + void +set_histentry(int hist_type, histentry_T *entry) +{ + history[hist_type] = entry; +} + + int * +get_hisidx(int hist_type) +{ + return &hisidx[hist_type]; +} + + int * +get_hisnum(int hist_type) +{ + return &hisnum[hist_type]; +} + +/* + * Translate a history character to the associated type number. + */ + int +hist_char2type(int c) +{ + if (c == ':') + return HIST_CMD; + if (c == '=') + return HIST_EXPR; + if (c == '@') + return HIST_INPUT; + if (c == '>') + return HIST_DEBUG; + return HIST_SEARCH; // must be '?' or '/' +} + +/* + * Table of history names. + * These names are used in :history and various hist...() functions. + * It is sufficient to give the significant prefix of a history name. + */ + +static char *(history_names[]) = +{ + "cmd", + "search", + "expr", + "input", + "debug", + NULL +}; + +#if defined(FEAT_CMDL_COMPL) || defined(PROTO) +/* + * Function given to ExpandGeneric() to obtain the possible first + * arguments of the ":history command. + */ + char_u * +get_history_arg(expand_T *xp UNUSED, int idx) +{ + static char_u compl[2] = { NUL, NUL }; + char *short_names = ":=@>?/"; + int short_names_count = (int)STRLEN(short_names); + int history_name_count = sizeof(history_names) / sizeof(char *) - 1; + + if (idx < short_names_count) + { + compl[0] = (char_u)short_names[idx]; + return compl; + } + if (idx < short_names_count + history_name_count) + return (char_u *)history_names[idx - short_names_count]; + if (idx == short_names_count + history_name_count) + return (char_u *)"all"; + return NULL; +} +#endif + +/* + * init_history() - Initialize the command line history. + * Also used to re-allocate the history when the size changes. + */ + void +init_history(void) +{ + int newlen; // new length of history table + histentry_T *temp; + int i; + int j; + int type; + + // If size of history table changed, reallocate it + newlen = (int)p_hi; + if (newlen != hislen) // history length changed + { + for (type = 0; type < HIST_COUNT; ++type) // adjust the tables + { + if (newlen) + { + temp = ALLOC_MULT(histentry_T, newlen); + if (temp == NULL) // out of memory! + { + if (type == 0) // first one: just keep the old length + { + newlen = hislen; + break; + } + // Already changed one table, now we can only have zero + // length for all tables. + newlen = 0; + type = -1; + continue; + } + } + else + temp = NULL; + if (newlen == 0 || temp != NULL) + { + if (hisidx[type] < 0) // there are no entries yet + { + for (i = 0; i < newlen; ++i) + clear_hist_entry(&temp[i]); + } + else if (newlen > hislen) // array becomes bigger + { + for (i = 0; i <= hisidx[type]; ++i) + temp[i] = history[type][i]; + j = i; + for ( ; i <= newlen - (hislen - hisidx[type]); ++i) + clear_hist_entry(&temp[i]); + for ( ; j < hislen; ++i, ++j) + temp[i] = history[type][j]; + } + else // array becomes smaller or 0 + { + j = hisidx[type]; + for (i = newlen - 1; ; --i) + { + if (i >= 0) // copy newest entries + temp[i] = history[type][j]; + else // remove older entries + vim_free(history[type][j].hisstr); + if (--j < 0) + j = hislen - 1; + if (j == hisidx[type]) + break; + } + hisidx[type] = newlen - 1; + } + vim_free(history[type]); + history[type] = temp; + } + } + hislen = newlen; + } +} + + void +clear_hist_entry(histentry_T *hisptr) +{ + hisptr->hisnum = 0; + hisptr->viminfo = FALSE; + hisptr->hisstr = NULL; + hisptr->time_set = 0; +} + +/* + * Check if command line 'str' is already in history. + * If 'move_to_front' is TRUE, matching entry is moved to end of history. + */ + int +in_history( + int type, + char_u *str, + int move_to_front, // Move the entry to the front if it exists + int sep, + int writing) // ignore entries read from viminfo +{ + int i; + int last_i = -1; + char_u *p; + + if (hisidx[type] < 0) + return FALSE; + i = hisidx[type]; + do + { + if (history[type][i].hisstr == NULL) + return FALSE; + + // For search history, check that the separator character matches as + // well. + p = history[type][i].hisstr; + if (STRCMP(str, p) == 0 + && !(writing && history[type][i].viminfo) + && (type != HIST_SEARCH || sep == p[STRLEN(p) + 1])) + { + if (!move_to_front) + return TRUE; + last_i = i; + break; + } + if (--i < 0) + i = hislen - 1; + } while (i != hisidx[type]); + + if (last_i >= 0) + { + str = history[type][i].hisstr; + while (i != hisidx[type]) + { + if (++i >= hislen) + i = 0; + history[type][last_i] = history[type][i]; + last_i = i; + } + history[type][i].hisnum = ++hisnum[type]; + history[type][i].viminfo = FALSE; + history[type][i].hisstr = str; + history[type][i].time_set = vim_time(); + return TRUE; + } + return FALSE; +} + +/* + * Convert history name (from table above) to its HIST_ equivalent. + * When "name" is empty, return "cmd" history. + * Returns -1 for unknown history name. + */ + static int +get_histtype(char_u *name) +{ + int i; + int len = (int)STRLEN(name); + + // No argument: use current history. + if (len == 0) + return hist_char2type(get_cmdline_firstc()); + + for (i = 0; history_names[i] != NULL; ++i) + if (STRNICMP(name, history_names[i], len) == 0) + return i; + + if (vim_strchr((char_u *)":=@>?/", name[0]) != NULL && name[1] == NUL) + return hist_char2type(name[0]); + + return -1; +} + +static int last_maptick = -1; // last seen maptick + +/* + * Add the given string to the given history. If the string is already in the + * history then it is moved to the front. "histype" may be one of he HIST_ + * values. + */ + void +add_to_history( + int histype, + char_u *new_entry, + int in_map, // consider maptick when inside a mapping + int sep) // separator character used (search hist) +{ + histentry_T *hisptr; + int len; + + if (hislen == 0) // no history + return; + + if (cmdmod.keeppatterns && histype == HIST_SEARCH) + return; + + // Searches inside the same mapping overwrite each other, so that only + // the last line is kept. Be careful not to remove a line that was moved + // down, only lines that were added. + if (histype == HIST_SEARCH && in_map) + { + if (maptick == last_maptick && hisidx[HIST_SEARCH] >= 0) + { + // Current line is from the same mapping, remove it + hisptr = &history[HIST_SEARCH][hisidx[HIST_SEARCH]]; + vim_free(hisptr->hisstr); + clear_hist_entry(hisptr); + --hisnum[histype]; + if (--hisidx[HIST_SEARCH] < 0) + hisidx[HIST_SEARCH] = hislen - 1; + } + last_maptick = -1; + } + if (!in_history(histype, new_entry, TRUE, sep, FALSE)) + { + if (++hisidx[histype] == hislen) + hisidx[histype] = 0; + hisptr = &history[histype][hisidx[histype]]; + vim_free(hisptr->hisstr); + + // Store the separator after the NUL of the string. + len = (int)STRLEN(new_entry); + hisptr->hisstr = vim_strnsave(new_entry, len + 2); + if (hisptr->hisstr != NULL) + hisptr->hisstr[len + 1] = sep; + + hisptr->hisnum = ++hisnum[histype]; + hisptr->viminfo = FALSE; + hisptr->time_set = vim_time(); + if (histype == HIST_SEARCH && in_map) + last_maptick = maptick; + } +} + +#if defined(FEAT_EVAL) || defined(PROTO) + +/* + * Get identifier of newest history entry. + * "histype" may be one of the HIST_ values. + */ + static int +get_history_idx(int histype) +{ + if (hislen == 0 || histype < 0 || histype >= HIST_COUNT + || hisidx[histype] < 0) + return -1; + + return history[histype][hisidx[histype]].hisnum; +} + +/* + * Calculate history index from a number: + * num > 0: seen as identifying number of a history entry + * num < 0: relative position in history wrt newest entry + * "histype" may be one of the HIST_ values. + */ + static int +calc_hist_idx(int histype, int num) +{ + int i; + histentry_T *hist; + int wrapped = FALSE; + + if (hislen == 0 || histype < 0 || histype >= HIST_COUNT + || (i = hisidx[histype]) < 0 || num == 0) + return -1; + + hist = history[histype]; + if (num > 0) + { + while (hist[i].hisnum > num) + if (--i < 0) + { + if (wrapped) + break; + i += hislen; + wrapped = TRUE; + } + if (hist[i].hisnum == num && hist[i].hisstr != NULL) + return i; + } + else if (-num <= hislen) + { + i += num + 1; + if (i < 0) + i += hislen; + if (hist[i].hisstr != NULL) + return i; + } + return -1; +} + +/* + * Get a history entry by its index. + * "histype" may be one of the HIST_ values. + */ + static char_u * +get_history_entry(int histype, int idx) +{ + idx = calc_hist_idx(histype, idx); + if (idx >= 0) + return history[histype][idx].hisstr; + else + return (char_u *)""; +} + +/* + * Clear all entries of a history. + * "histype" may be one of the HIST_ values. + */ + static int +clr_history(int histype) +{ + int i; + histentry_T *hisptr; + + if (hislen != 0 && histype >= 0 && histype < HIST_COUNT) + { + hisptr = history[histype]; + for (i = hislen; i--;) + { + vim_free(hisptr->hisstr); + clear_hist_entry(hisptr); + hisptr++; + } + hisidx[histype] = -1; // mark history as cleared + hisnum[histype] = 0; // reset identifier counter + return OK; + } + return FAIL; +} + +/* + * Remove all entries matching {str} from a history. + * "histype" may be one of the HIST_ values. + */ + static int +del_history_entry(int histype, char_u *str) +{ + regmatch_T regmatch; + histentry_T *hisptr; + int idx; + int i; + int last; + int found = FALSE; + + regmatch.regprog = NULL; + regmatch.rm_ic = FALSE; // always match case + if (hislen != 0 + && histype >= 0 + && histype < HIST_COUNT + && *str != NUL + && (idx = hisidx[histype]) >= 0 + && (regmatch.regprog = vim_regcomp(str, RE_MAGIC + RE_STRING)) + != NULL) + { + i = last = idx; + do + { + hisptr = &history[histype][i]; + if (hisptr->hisstr == NULL) + break; + if (vim_regexec(®match, hisptr->hisstr, (colnr_T)0)) + { + found = TRUE; + vim_free(hisptr->hisstr); + clear_hist_entry(hisptr); + } + else + { + if (i != last) + { + history[histype][last] = *hisptr; + clear_hist_entry(hisptr); + } + if (--last < 0) + last += hislen; + } + if (--i < 0) + i += hislen; + } while (i != idx); + if (history[histype][idx].hisstr == NULL) + hisidx[histype] = -1; + } + vim_regfree(regmatch.regprog); + return found; +} + +/* + * Remove an indexed entry from a history. + * "histype" may be one of the HIST_ values. + */ + static int +del_history_idx(int histype, int idx) +{ + int i, j; + + i = calc_hist_idx(histype, idx); + if (i < 0) + return FALSE; + idx = hisidx[histype]; + vim_free(history[histype][i].hisstr); + + // When deleting the last added search string in a mapping, reset + // last_maptick, so that the last added search string isn't deleted again. + if (histype == HIST_SEARCH && maptick == last_maptick && i == idx) + last_maptick = -1; + + while (i != idx) + { + j = (i + 1) % hislen; + history[histype][i] = history[histype][j]; + i = j; + } + clear_hist_entry(&history[histype][i]); + if (--i < 0) + i += hislen; + hisidx[histype] = i; + return TRUE; +} + +/* + * "histadd()" function + */ + void +f_histadd(typval_T *argvars UNUSED, typval_T *rettv) +{ + int histype; + char_u *str; + char_u buf[NUMBUFLEN]; + + rettv->vval.v_number = FALSE; + if (check_secure()) + return; + str = tv_get_string_chk(&argvars[0]); // NULL on type error + histype = str != NULL ? get_histtype(str) : -1; + if (histype >= 0) + { + str = tv_get_string_buf(&argvars[1], buf); + if (*str != NUL) + { + init_history(); + add_to_history(histype, str, FALSE, NUL); + rettv->vval.v_number = TRUE; + return; + } + } +} + +/* + * "histdel()" function + */ + void +f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED) +{ + int n; + char_u buf[NUMBUFLEN]; + char_u *str; + + str = tv_get_string_chk(&argvars[0]); // NULL on type error + if (str == NULL) + n = 0; + else if (argvars[1].v_type == VAR_UNKNOWN) + // only one argument: clear entire history + n = clr_history(get_histtype(str)); + else if (argvars[1].v_type == VAR_NUMBER) + // index given: remove that entry + n = del_history_idx(get_histtype(str), + (int)tv_get_number(&argvars[1])); + else + // string given: remove all matching entries + n = del_history_entry(get_histtype(str), + tv_get_string_buf(&argvars[1], buf)); + rettv->vval.v_number = n; +} + +/* + * "histget()" function + */ + void +f_histget(typval_T *argvars UNUSED, typval_T *rettv) +{ + int type; + int idx; + char_u *str; + + str = tv_get_string_chk(&argvars[0]); // NULL on type error + if (str == NULL) + rettv->vval.v_string = NULL; + else + { + type = get_histtype(str); + if (argvars[1].v_type == VAR_UNKNOWN) + idx = get_history_idx(type); + else + idx = (int)tv_get_number_chk(&argvars[1], NULL); + // -1 on type error + rettv->vval.v_string = vim_strsave(get_history_entry(type, idx)); + } + rettv->v_type = VAR_STRING; +} + +/* + * "histnr()" function + */ + void +f_histnr(typval_T *argvars UNUSED, typval_T *rettv) +{ + int i; + + char_u *histname = tv_get_string_chk(&argvars[0]); + + i = histname == NULL ? HIST_CMD - 1 : get_histtype(histname); + if (i >= HIST_CMD && i < HIST_COUNT) + i = get_history_idx(i); + else + i = -1; + rettv->vval.v_number = i; +} +#endif // FEAT_EVAL + +#if defined(FEAT_CRYPT) || defined(PROTO) +/* + * Very specific function to remove the value in ":set key=val" from the + * history. + */ + void +remove_key_from_history(void) +{ + char_u *p; + int i; + + i = hisidx[HIST_CMD]; + if (i < 0) + return; + p = history[HIST_CMD][i].hisstr; + if (p != NULL) + for ( ; *p; ++p) + if (STRNCMP(p, "key", 3) == 0 && !isalpha(p[3])) + { + p = vim_strchr(p + 3, '='); + if (p == NULL) + break; + ++p; + for (i = 0; p[i] && !VIM_ISWHITE(p[i]); ++i) + if (p[i] == '\\' && p[i + 1]) + ++i; + STRMOVE(p, p + i); + --p; + } +} +#endif + +/* + * :history command - print a history + */ + void +ex_history(exarg_T *eap) +{ + histentry_T *hist; + int histype1 = HIST_CMD; + int histype2 = HIST_CMD; + int hisidx1 = 1; + int hisidx2 = -1; + int idx; + int i, j, k; + char_u *end; + char_u *arg = eap->arg; + + if (hislen == 0) + { + msg(_("'history' option is zero")); + return; + } + + if (!(VIM_ISDIGIT(*arg) || *arg == '-' || *arg == ',')) + { + end = arg; + while (ASCII_ISALPHA(*end) + || vim_strchr((char_u *)":=@>/?", *end) != NULL) + end++; + i = *end; + *end = NUL; + histype1 = get_histtype(arg); + if (histype1 == -1) + { + if (STRNICMP(arg, "all", STRLEN(arg)) == 0) + { + histype1 = 0; + histype2 = HIST_COUNT-1; + } + else + { + *end = i; + emsg(_(e_trailing)); + return; + } + } + else + histype2 = histype1; + *end = i; + } + else + end = arg; + if (!get_list_range(&end, &hisidx1, &hisidx2) || *end != NUL) + { + emsg(_(e_trailing)); + return; + } + + for (; !got_int && histype1 <= histype2; ++histype1) + { + STRCPY(IObuff, "\n # "); + STRCAT(STRCAT(IObuff, history_names[histype1]), " history"); + msg_puts_title((char *)IObuff); + idx = hisidx[histype1]; + hist = history[histype1]; + j = hisidx1; + k = hisidx2; + if (j < 0) + j = (-j > hislen) ? 0 : hist[(hislen+j+idx+1) % hislen].hisnum; + if (k < 0) + k = (-k > hislen) ? 0 : hist[(hislen+k+idx+1) % hislen].hisnum; + if (idx >= 0 && j <= k) + for (i = idx + 1; !got_int; ++i) + { + if (i == hislen) + i = 0; + if (hist[i].hisstr != NULL + && hist[i].hisnum >= j && hist[i].hisnum <= k) + { + msg_putchar('\n'); + sprintf((char *)IObuff, "%c%6d ", i == idx ? '>' : ' ', + hist[i].hisnum); + if (vim_strsize(hist[i].hisstr) > (int)Columns - 10) + trunc_string(hist[i].hisstr, IObuff + STRLEN(IObuff), + (int)Columns - 10, IOSIZE - (int)STRLEN(IObuff)); + else + STRCAT(IObuff, hist[i].hisstr); + msg_outtrans(IObuff); + out_flush(); + } + if (i == idx) + break; + } + } +} diff --git a/src/evalfunc.c b/src/evalfunc.c index 0c522dd51b..ab8efe92e5 100644 --- a/src/evalfunc.c +++ b/src/evalfunc.c @@ -182,10 +182,6 @@ static void f_glob2regpat(typval_T *argvars, typval_T *rettv); static void f_has(typval_T *argvars, typval_T *rettv); static void f_haslocaldir(typval_T *argvars, typval_T *rettv); static void f_hasmapto(typval_T *argvars, typval_T *rettv); -static void f_histadd(typval_T *argvars, typval_T *rettv); -static void f_histdel(typval_T *argvars, typval_T *rettv); -static void f_histget(typval_T *argvars, typval_T *rettv); -static void f_histnr(typval_T *argvars, typval_T *rettv); static void f_hlID(typval_T *argvars, typval_T *rettv); static void f_hlexists(typval_T *argvars, typval_T *rettv); static void f_hostname(typval_T *argvars, typval_T *rettv); @@ -6114,9 +6110,7 @@ f_has(typval_T *argvars, typval_T *rettv) #ifdef FEAT_CMDL_COMPL "cmdline_compl", #endif -#ifdef FEAT_CMDHIST "cmdline_hist", -#endif #ifdef FEAT_COMMENTS "comments", #endif @@ -6688,117 +6682,6 @@ f_hasmapto(typval_T *argvars, typval_T *rettv) } /* - * "histadd()" function - */ - static void -f_histadd(typval_T *argvars UNUSED, typval_T *rettv) -{ -#ifdef FEAT_CMDHIST - int histype; - char_u *str; - char_u buf[NUMBUFLEN]; -#endif - - rettv->vval.v_number = FALSE; - if (check_secure()) - return; -#ifdef FEAT_CMDHIST - str = tv_get_string_chk(&argvars[0]); /* NULL on type error */ - histype = str != NULL ? get_histtype(str) : -1; - if (histype >= 0) - { - str = tv_get_string_buf(&argvars[1], buf); - if (*str != NUL) - { - init_history(); - add_to_history(histype, str, FALSE, NUL); - rettv->vval.v_number = TRUE; - return; - } - } -#endif -} - -/* - * "histdel()" function - */ - static void -f_histdel(typval_T *argvars UNUSED, typval_T *rettv UNUSED) -{ -#ifdef FEAT_CMDHIST - int n; - char_u buf[NUMBUFLEN]; - char_u *str; - - str = tv_get_string_chk(&argvars[0]); /* NULL on type err |