Provide an API to allow completion with associated data.
Completion with data "packed" within the selected data.
(acomplete PROMPT COLLECTION &optional :string-fn FN :data-fn FN :finalize-fn FN)
Let the user choose between the items in COLLECTION. The items can be any kind of data. The string representation and the data associated with it can be optionally fine tuned with functions.
In the most basic case, the user selects an item and the function returns the item itself.
The item to be returned can be modified by DATA-FN, however. That is,
DATA-FN maps an item of COLLECTION to the item which will be returned
by the selection. DATA-FN defaults to identity
.
The representation of the items of COLLECTION can be further
controlled by STRING-FN. Think of STRING-FN as 'the function which
determines how the item is represented to the user.' The value of
STRING-FN can be either a function or itself a string. As a function,
STRING-FN takes the original (!) item (that is, the item before it is
passed to DATA-FN) and returns its string representation. If STRING-FN
is itself a string, this string is simply passed to the format
function with the item as its only argument. STRING-FN defaults to
"%s"
'.
For pretty printing, the string created so far can be modified via
FINALIZE-FN. Note that this function receives the complete
selection string as its argument, where the text property data
contains the associated return value. This function thus allows to
modify the string in response to its associated data (i.e., colorize
the string according to the data type). To access the data, use
acomplete-get-data
, which receives the propertized string and
returns the associated data item.
Summary:
STRING-FN
: Receive a data item and return a string. Can be either a
function or a string which is passed to format
. Defaults to "%s"
.
DATA-FN
: Modify the data item before it is 'packed' into the
string as its associated return value. Defaults to identity
.
FINALIZE-FN
: Receive a propertized string and return a (propertized)
string. Defaults to identity
.
You can access the data slot by passing the propertized string to the function `acomplete-get-data'. This is sometimes useful when finalizing a string.
(acomplete "Select number: " '(1 2 3))
-> returns the number (as an integer)
Nicer strings:
(acomplete "Select number: " '(1 2 3) :string-fn "Number %d.")
-> returns the number (as an integer)
(acomplete "Select color: " (defined-colors)
:finalize-fn (lambda (color-string) (propertize color-string 'face `(:foreground ,color-string))))
-> returns the color string
(acomplete "Select buffer: " (buffer-list)
:string-fn #'buffer-name)
-> returns the buffer object
(acomplete "Select open file: " (seq-filter #'buffer-file-name (buffer-list))
:data-fn #'buffer-file-name)
-> returns the complete filename
(acomplete "Select color: " (defined-colors)
:string-fn (lambda (color-string)
(concat color-string " "
(apply #'color-rgb-to-hex (append (color-name-to-rgb color-string) '(2)))))
:finalize-fn (lambda (propertized-string)
(let* ((color-string (acomplete-get-data propertized-string)))
(propertize propertized-string 'face `(:foreground ,color-string)))))
-> returns the color name
(acomplete "Select buffer: " (buffer-list)
:require-match nil)