Skip to content

Commit f956550

Browse files
authored
Merge pull request #74 from vim-denops/input
👍 Improve `input()` and related functions
2 parents eb6fa8b + af97833 commit f956550

File tree

10 files changed

+690
-134
lines changed

10 files changed

+690
-134
lines changed

denops_std/function/_generated.ts

Lines changed: 0 additions & 134 deletions
Original file line numberDiff line numberDiff line change
@@ -3272,140 +3272,6 @@ export function index(denops: Denops, ...args: unknown[]): Promise<unknown> {
32723272
return denops.call("index", ...args);
32733273
}
32743274

3275-
/**
3276-
* The result is a String, which is whatever the user typed on
3277-
* the command-line. The {prompt} argument is either a prompt
3278-
* string, or a blank string (for no prompt). A '\n' can be used
3279-
* in the prompt to start a new line.
3280-
* The highlighting set with |:echohl| is used for the prompt.
3281-
* The input is entered just like a command-line, with the same
3282-
* editing commands and mappings. There is a separate history
3283-
* for lines typed for input().
3284-
* Example:
3285-
* :if input("Coffee or beer? ") == "beer"
3286-
* : echo "Cheers!"
3287-
* :endif
3288-
* If the optional {text} argument is present and not empty, this
3289-
* is used for the default reply, as if the user typed this.
3290-
* Example:
3291-
* :let color = input("Color? ", "white")
3292-
* The optional {completion} argument specifies the type of
3293-
* completion supported for the input. Without it completion is
3294-
* not performed. The supported completion types are the same as
3295-
* that can be supplied to a user-defined command using the
3296-
* "-complete=" argument. Refer to |:command-completion| for
3297-
* more information. Example:
3298-
* let fname = input("File: ", "", "file")
3299-
* NOTE: This function must not be used in a startup file, for
3300-
* the versions that only run in GUI mode (e.g., the Win32 GUI).
3301-
* Note: When input() is called from within a mapping it will
3302-
* consume remaining characters from that mapping, because a
3303-
* mapping is handled like the characters were typed.
3304-
* Use |inputsave()| before input() and |inputrestore()|
3305-
* after input() to avoid that. Another solution is to avoid
3306-
* that further characters follow in the mapping, e.g., by using
3307-
* |:execute| or |:normal|.
3308-
* Example with a mapping:
3309-
* :nmap \x :call GetFoo()<CR>:exe "/" . Foo<CR
3310-
* :function GetFoo()
3311-
* : call inputsave()
3312-
* : let g:Foo = input("enter search pattern: ")
3313-
* : call inputrestore()
3314-
* :endfunction
3315-
* Can also be used as a |method|:
3316-
* GetPrompt()->input()
3317-
*/
3318-
export function input(
3319-
denops: Denops,
3320-
prompt: unknown,
3321-
text?: unknown,
3322-
completion?: unknown,
3323-
): Promise<unknown>;
3324-
export function input(denops: Denops, ...args: unknown[]): Promise<unknown> {
3325-
return denops.call("input", ...args);
3326-
}
3327-
3328-
/**
3329-
* {textlist} must be a |List| of strings. This |List| is
3330-
* displayed, one string per line. The user will be prompted to
3331-
* enter a number, which is returned.
3332-
* The user can also select an item by clicking on it with the
3333-
* mouse. For the first string 0 is returned. When clicking
3334-
* above the first item a negative number is returned. When
3335-
* clicking on the prompt one more than the length of {textlist}
3336-
* is returned.
3337-
* Make sure {textlist} has less than 'lines' entries, otherwise
3338-
* it won't work. It's a good idea to put the entry number at
3339-
* the start of the string. And put a prompt in the first item.
3340-
* Example:
3341-
* let color = inputlist(['Select color:', '1. red',
3342-
* \ '2. green', '3. blue'])
3343-
* Can also be used as a |method|:
3344-
* GetChoices()->inputlist()
3345-
*/
3346-
export function inputlist(denops: Denops, textlist: unknown): Promise<unknown>;
3347-
export function inputlist(
3348-
denops: Denops,
3349-
...args: unknown[]
3350-
): Promise<unknown> {
3351-
return denops.call("inputlist", ...args);
3352-
}
3353-
3354-
/**
3355-
* Restore typeahead that was saved with a previous |inputsave()|.
3356-
* Should be called the same number of times inputsave() is
3357-
* called. Calling it more often is harmless though.
3358-
* Returns 1 when there is nothing to restore, 0 otherwise.
3359-
*/
3360-
export function inputrestore(denops: Denops): Promise<unknown>;
3361-
export function inputrestore(
3362-
denops: Denops,
3363-
...args: unknown[]
3364-
): Promise<unknown> {
3365-
return denops.call("inputrestore", ...args);
3366-
}
3367-
3368-
/**
3369-
* Preserve typeahead (also from mappings) and clear it, so that
3370-
* a following prompt gets input from the user. Should be
3371-
* followed by a matching inputrestore() after the prompt. Can
3372-
* be used several times, in which case there must be just as
3373-
* many inputrestore() calls.
3374-
* Returns 1 when out of memory, 0 otherwise.
3375-
*/
3376-
export function inputsave(denops: Denops): Promise<unknown>;
3377-
export function inputsave(
3378-
denops: Denops,
3379-
...args: unknown[]
3380-
): Promise<unknown> {
3381-
return denops.call("inputsave", ...args);
3382-
}
3383-
3384-
/**
3385-
* This function acts much like the |input()| function with but
3386-
* two exceptions:
3387-
* a) the user's response will be displayed as a sequence of
3388-
* asterisks ("*") thereby keeping the entry secret, and
3389-
* b) the user's response will not be recorded on the input
3390-
* |history| stack.
3391-
* The result is a String, which is whatever the user actually
3392-
* typed on the command-line in response to the issued prompt.
3393-
* NOTE: Command-line completion is not supported.
3394-
* Can also be used as a |method|:
3395-
* GetPrompt()->inputsecret()
3396-
*/
3397-
export function inputsecret(
3398-
denops: Denops,
3399-
prompt: unknown,
3400-
text?: unknown,
3401-
): Promise<unknown>;
3402-
export function inputsecret(
3403-
denops: Denops,
3404-
...args: unknown[]
3405-
): Promise<unknown> {
3406-
return denops.call("inputsecret", ...args);
3407-
}
3408-
34093275
/**
34103276
* When {object} is a |List| or a |Blob| insert {item} at the start
34113277
* of it.

denops_std/function/_manual.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export * from "./buffer.ts";
22
export * from "./common.ts";
33
export * from "./cursor.ts";
4+
export * from "./input.ts";
45
export * from "./types.ts";
56
export * from "./various.ts";

denops_std/function/input.ts

Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
1+
import { Denops } from "../deps.ts";
2+
import { BuiltinCompletion, isValidBuiltinCompletion } from "./types.ts";
3+
4+
/**
5+
* The result is a String, which is whatever the user typed on
6+
* the command-line. The {prompt} argument is either a prompt
7+
* string, or a blank string (for no prompt). A '\n' can be used
8+
* in the prompt to start a new line.
9+
* The highlighting set with |:echohl| is used for the prompt.
10+
* The input is entered just like a command-line, with the same
11+
* editing commands and mappings. There is a separate history
12+
* for lines typed for input().
13+
* Example:
14+
* :if input("Coffee or beer? ") == "beer"
15+
* : echo "Cheers!"
16+
* :endif
17+
* If the optional {text} argument is present and not empty, this
18+
* is used for the default reply, as if the user typed this.
19+
* Example:
20+
* :let color = input("Color? ", "white")
21+
* The optional {completion} argument specifies the type of
22+
* completion supported for the input. Without it completion is
23+
* not performed. The supported completion types are the same as
24+
* that can be supplied to a user-defined command using the
25+
* "-complete=" argument. Refer to |:command-completion| for
26+
* more information. Example:
27+
* let fname = input("File: ", "", "file")
28+
* NOTE: This function must not be used in a startup file, for
29+
* the versions that only run in GUI mode (e.g., the Win32 GUI).
30+
* Note: When input() is called from within a mapping it will
31+
* consume remaining characters from that mapping, because a
32+
* mapping is handled like the characters were typed.
33+
* Use |inputsave()| before input() and |inputrestore()|
34+
* after input() to avoid that. Another solution is to avoid
35+
* that further characters follow in the mapping, e.g., by using
36+
* |:execute| or |:normal|.
37+
* Example with a mapping:
38+
* :nmap \x :call GetFoo()<CR>:exe "/" . Foo<CR
39+
* :function GetFoo()
40+
* : call inputsave()
41+
* : let g:Foo = input("enter search pattern: ")
42+
* : call inputrestore()
43+
* :endfunction
44+
* Can also be used as a |method|:
45+
* GetPrompt()->input()
46+
*/
47+
export function input(
48+
denops: Denops,
49+
prompt: string,
50+
text = "",
51+
completion?: BuiltinCompletion | string,
52+
): Promise<string> {
53+
if (completion && !isValidBuiltinCompletion(completion)) {
54+
if (
55+
!completion.startsWith("custom,") && !completion.startsWith("customlist,")
56+
) {
57+
throw new Error(`Invalid completion '${completion}' specified.`);
58+
}
59+
}
60+
return denops.call("input", prompt, text, completion) as Promise<string>;
61+
}
62+
63+
/**
64+
* {textlist} must be a |List| of strings. This |List| is
65+
* displayed, one string per line. The user will be prompted to
66+
* enter a number, which is returned.
67+
* The user can also select an item by clicking on it with the
68+
* mouse. For the first string 0 is returned. When clicking
69+
* above the first item a negative number is returned. When
70+
* clicking on the prompt one more than the length of {textlist}
71+
* is returned.
72+
* Make sure {textlist} has less than 'lines' entries, otherwise
73+
* it won't work. It's a good idea to put the entry number at
74+
* the start of the string. And put a prompt in the first item.
75+
* Example:
76+
* let color = inputlist(['Select color:', '1. red',
77+
* \ '2. green', '3. blue'])
78+
* Can also be used as a |method|:
79+
* GetChoices()->inputlist()
80+
*/
81+
export function inputlist(denops: Denops, textlist: string[]): Promise<number> {
82+
return denops.call("inputlist", textlist) as Promise<number>;
83+
}
84+
85+
/**
86+
* Restore typeahead that was saved with a previous |inputsave()|.
87+
* Should be called the same number of times inputsave() is
88+
* called. Calling it more often is harmless though.
89+
* Returns 1 when there is nothing to restore, 0 otherwise.
90+
*/
91+
export function inputrestore(denops: Denops): Promise<void> {
92+
return denops.call("inputrestore") as Promise<void>;
93+
}
94+
95+
/**
96+
* Preserve typeahead (also from mappings) and clear it, so that
97+
* a following prompt gets input from the user. Should be
98+
* followed by a matching inputrestore() after the prompt. Can
99+
* be used several times, in which case there must be just as
100+
* many inputrestore() calls.
101+
* Returns 1 when out of memory, 0 otherwise.
102+
*/
103+
export function inputsave(denops: Denops): Promise<void> {
104+
return denops.call("inputsave") as Promise<void>;
105+
}
106+
107+
/**
108+
* This function acts much like the |input()| function with but
109+
* two exceptions:
110+
* a) the user's response will be displayed as a sequence of
111+
* asterisks ("*") thereby keeping the entry secret, and
112+
* b) the user's response will not be recorded on the input
113+
* |history| stack.
114+
* The result is a String, which is whatever the user actually
115+
* typed on the command-line in response to the issued prompt.
116+
* NOTE: Command-line completion is not supported.
117+
* Can also be used as a |method|:
118+
* GetPrompt()->inputsecret()
119+
*/
120+
export function inputsecret(
121+
denops: Denops,
122+
prompt: string,
123+
text = "",
124+
): Promise<string> {
125+
return denops.call("inputsecret", prompt, text) as Promise<string>;
126+
}

0 commit comments

Comments
 (0)