'keywordprg': support ex commands

- new feature: if the first character of 'keywordprg' is ":", the
  command is invoked as a Vim ex-command prefixed with [count].
- change default 'keywordprg' to :Man
This commit is contained in:
Justin M. Keyes 2015-01-23 20:03:05 -05:00
parent 481049e0c9
commit 998d0ffc09
5 changed files with 39 additions and 40 deletions

View File

@ -3877,18 +3877,20 @@ A jump table for the options with a short description can be found at |Q_op|.
The 'keymodel' option is set by the |:behave| command. The 'keymodel' option is set by the |:behave| command.
*'keywordprg'* *'kp'* *'keywordprg'* *'kp'*
'keywordprg' 'kp' string (default "man" or "man -s", DOS: ":help") 'keywordprg' 'kp' string (default ":Man", Windows: ":help")
global or local to buffer |global-local| global or local to buffer |global-local|
Program to use for the |K| command. Environment variables are Program to use for the |K| command. Environment variables are
expanded |:set_env|. ":help" may be used to access the Vim internal expanded |:set_env|. ":help" may be used to access the Vim internal
help. (Note that previously setting the global option to the empty help. (Note that previously setting the global option to the empty
value did this, which is now deprecated.) value did this, which is now deprecated.)
When "man" is used, Vim will automatically translate a count for the When the first character is ":", the command is invoked as a Vim
"K" command to a section number. Also for "man -s", in which case the command prefixed with [count]. {Nvim}
"-s" is removed when there is no count. When "man" or "man -s" is used, Vim will automatically translate
a [count] for the "K" command to a section number.
See |option-backslash| about including spaces and backslashes. See |option-backslash| about including spaces and backslashes.
Example: > Example: >
:set keywordprg=man\ -s :set keywordprg=man\ -s
:set keywordprg=:Man
< This option cannot be set from a |modeline| or in the |sandbox|, for < This option cannot be set from a |modeline| or in the |sandbox|, for
security reasons. security reasons.

View File

@ -542,7 +542,7 @@ which it was defined is reported.
{not available when compiled without the |+eval| feature} {not available when compiled without the |+eval| feature}
*K* *K*
K Run a program to lookup the keyword under the [count]K Run a program to lookup the keyword under the
cursor. The name of the program is given with the cursor. The name of the program is given with the
'keywordprg' (kp) option (default is "man"). The 'keywordprg' (kp) option (default is "man"). The
keyword is formed of letters, numbers and the keyword is formed of letters, numbers and the
@ -550,19 +550,18 @@ K Run a program to lookup the keyword under the
right of the cursor is used. The same can be done right of the cursor is used. The same can be done
with the command > with the command >
:!{program} {keyword} :!{program} {keyword}
< There is an example of a program to use in the tools < Special cases:
directory of Vim. It is called 'ref' and does a - If 'keywordprg' begins with ":" it is invoked as
simple spelling check. a Vim command with [count].
Special cases:
- If 'keywordprg' is empty, the ":help" command is - If 'keywordprg' is empty, the ":help" command is
used. It's a good idea to include more characters used. It's a good idea to include more characters
in 'iskeyword' then, to be able to find more help. in 'iskeyword' then, to be able to find more help.
- When 'keywordprg' is equal to "man", a count before - When 'keywordprg' is equal to "man", a [count]
"K" is inserted after the "man" command and before before "K" is inserted after the "man" command and
the keyword. For example, using "2K" while the before the keyword. For example, using "2K" while
cursor is on "mkdir", results in: > the cursor is on "mkdir", results in: >
!man 2 mkdir !man 2 mkdir
< - When 'keywordprg' is equal to "man -s", a count < - When 'keywordprg' is equal to "man -s", a [count]
before "K" is inserted after the "-s". If there is before "K" is inserted after the "-s". If there is
no count, the "-s" is removed. no count, the "-s" is removed.

View File

@ -120,7 +120,7 @@ func <SID>GetPage(...)
let sect = "" let sect = ""
endif endif
if s:FindPage(sect, page) == 0 if s:FindPage(sect, page) == 0
echo "\nCannot find a '".page."'." echo "\nNo manual entry for '".page."'."
return return
endif endif
exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%") exec "let s:man_tag_buf_".s:man_tag_depth." = ".bufnr("%")

View File

@ -4213,18 +4213,12 @@ void do_nv_ident(int c1, int c2)
static void nv_ident(cmdarg_T *cap) static void nv_ident(cmdarg_T *cap)
{ {
char_u *ptr = NULL; char_u *ptr = NULL;
char_u *buf;
char_u *newbuf;
char_u *p; char_u *p;
char_u *kp; /* value of 'keywordprg' */
bool kp_help; /* 'keywordprg' is ":help" */
size_t n = 0; /* init for GCC */ size_t n = 0; /* init for GCC */
int cmdchar; int cmdchar;
bool g_cmd; /* "g" command */ bool g_cmd; /* "g" command */
bool tag_cmd = false; bool tag_cmd = false;
char_u *aux_ptr; char_u *aux_ptr;
bool isman;
bool isman_s;
if (cap->cmdchar == 'g') { /* "g*", "g#", "g]" and "gCTRL-]" */ if (cap->cmdchar == 'g') { /* "g*", "g#", "g]" and "gCTRL-]" */
cmdchar = cap->nchar; cmdchar = cap->nchar;
@ -4259,10 +4253,10 @@ static void nv_ident(cmdarg_T *cap)
/* Allocate buffer to put the command in. Inserting backslashes can /* Allocate buffer to put the command in. Inserting backslashes can
* double the length of the word. p_kp / curbuf->b_p_kp could be added * double the length of the word. p_kp / curbuf->b_p_kp could be added
* and some numbers. */ * and some numbers. */
kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp); char_u *kp = (*curbuf->b_p_kp == NUL ? p_kp : curbuf->b_p_kp); //'keywordprg'
kp_help = (*kp == NUL || STRCMP(kp, ":he") == 0 assert(*kp != NUL); //option.c::do_set() should default to ":help" if empty.
|| STRCMP(kp, ":help") == 0); bool kp_ex = (*kp == ':'); //'keywordprg' is an ex command
buf = xmalloc(n * 2 + 30 + STRLEN(kp)); char *buf = xmalloc(n * 2 + 30 + STRLEN(kp));
buf[0] = NUL; buf[0] = NUL;
switch (cmdchar) { switch (cmdchar) {
@ -4283,9 +4277,13 @@ static void nv_ident(cmdarg_T *cap)
break; break;
case 'K': case 'K':
if (kp_help) if (kp_ex) {
STRCPY(buf, "he! "); if (cap->count0 != 0) { // Send the count to the ex command.
else { sprintf(buf, "%" PRId64, (int64_t)(cap->count0));
}
STRCAT(buf, kp);
STRCAT(buf, " ");
} else {
/* An external command will probably use an argument starting /* An external command will probably use an argument starting
* with "-" as an option. To avoid trouble we skip the "-". */ * with "-" as an option. To avoid trouble we skip the "-". */
while (*ptr == '-' && n > 0) { while (*ptr == '-' && n > 0) {
@ -4300,10 +4298,10 @@ static void nv_ident(cmdarg_T *cap)
/* When a count is given, turn it into a range. Is this /* When a count is given, turn it into a range. Is this
* really what we want? */ * really what we want? */
isman = (STRCMP(kp, "man") == 0); bool isman = (STRCMP(kp, "man") == 0);
isman_s = (STRCMP(kp, "man -s") == 0); bool isman_s = (STRCMP(kp, "man -s") == 0);
if (cap->count0 != 0 && !(isman || isman_s)) if (cap->count0 != 0 && !(isman || isman_s))
sprintf((char *)buf, ".,.+%" PRId64, (int64_t)(cap->count0 - 1)); sprintf(buf, ".,.+%" PRId64, (int64_t)(cap->count0 - 1));
STRCAT(buf, "! "); STRCAT(buf, "! ");
if (cap->count0 == 0 && isman_s) if (cap->count0 == 0 && isman_s)
@ -4312,7 +4310,7 @@ static void nv_ident(cmdarg_T *cap)
STRCAT(buf, kp); STRCAT(buf, kp);
STRCAT(buf, " "); STRCAT(buf, " ");
if (cap->count0 != 0 && (isman || isman_s)) { if (cap->count0 != 0 && (isman || isman_s)) {
sprintf((char *)buf + STRLEN(buf), "%" PRId64, (int64_t)cap->count0); sprintf(buf + STRLEN(buf), "%" PRId64, (int64_t)cap->count0);
STRCAT(buf, " "); STRCAT(buf, " ");
} }
} }
@ -4334,19 +4332,19 @@ static void nv_ident(cmdarg_T *cap)
if (g_cmd) if (g_cmd)
STRCPY(buf, "tj "); STRCPY(buf, "tj ");
else else
sprintf((char *)buf, "%" PRId64 "ta ", (int64_t)cap->count0); sprintf(buf, "%" PRId64 "ta ", (int64_t)cap->count0);
} }
} }
/* /*
* Now grab the chars in the identifier * Now grab the chars in the identifier
*/ */
if (cmdchar == 'K' && !kp_help) { if (cmdchar == 'K' && !kp_ex) {
/* Escape the argument properly for a shell command */ /* Escape the argument properly for a shell command */
ptr = vim_strnsave(ptr, n); ptr = vim_strnsave(ptr, n);
p = vim_strsave_shellescape(ptr, true, true); p = vim_strsave_shellescape(ptr, true, true);
xfree(ptr); xfree(ptr);
newbuf = (char_u *)xrealloc(buf, STRLEN(buf) + STRLEN(p) + 1); char *newbuf = xrealloc(buf, STRLEN(buf) + STRLEN(p) + 1);
buf = newbuf; buf = newbuf;
STRCAT(buf, p); STRCAT(buf, p);
xfree(p); xfree(p);
@ -4364,7 +4362,7 @@ static void nv_ident(cmdarg_T *cap)
} else } else
aux_ptr = (char_u *)"\\|\"\n*?["; aux_ptr = (char_u *)"\\|\"\n*?[";
p = buf + STRLEN(buf); p = (char_u *)buf + STRLEN(buf);
while (n-- > 0) { while (n-- > 0) {
/* put a backslash before \ and some others */ /* put a backslash before \ and some others */
if (vim_strchr(aux_ptr, *ptr) != NULL) if (vim_strchr(aux_ptr, *ptr) != NULL)
@ -4391,10 +4389,10 @@ static void nv_ident(cmdarg_T *cap)
STRCAT(buf, "\\>"); STRCAT(buf, "\\>");
/* put pattern in search history */ /* put pattern in search history */
init_history(); init_history();
add_to_history(HIST_SEARCH, buf, true, NUL); add_to_history(HIST_SEARCH, (char_u *)buf, true, NUL);
(void)normal_search(cap, cmdchar == '*' ? '/' : '?', buf, 0); (void)normal_search(cap, cmdchar == '*' ? '/' : '?', (char_u *)buf, 0);
} else } else
do_cmdline_cmd((char *)buf); do_cmdline_cmd(buf);
xfree(buf); xfree(buf);
} }

View File

@ -1329,7 +1329,7 @@ return {
expand=true, expand=true,
varname='p_kp', varname='p_kp',
defaults={ defaults={
if_true={vi="man -s"}, if_true={vi=":Man"},
} }
}, },
{ {