1

I got a simple snippet for the opening a file in PHP:

fopen('${1:./filename.txt}', '${2:$$(yas-choose-value '("r" "w" "a" "x" "r+" "w+" "a+" "x+"))}') or die();

So when I get to the choose the file opening mode, r,w,a,x... I get the dropdown:

r
w
a
.

But I would like to add a description of whats the function of each mode, like:

r   - Open read only
w   - Open for write erases content
.
.

Is it possible to do this with yas-choose-value?

1
  • 1
    Users are not restricted to yas-choose-value or yas-completing-read. There are a handful of built-in functions, but you (or another forum participant) is free to write a custom function. It looks like this: (unless (or yas-moving-away-p yas-modified-p) ...) with the ... needing to be replaced with the custom Lisp that does whatever you want.
    – lawlist
    Commented Jan 3, 2021 at 17:12

1 Answer 1

3

I would suggest using an alist of choices, with the car being the choice+description and the cdr being the actual choice. There are a variety of options that yasnippet offers for user input (see yas-prompt-functions with a default value of yas-dropdown-prompt, yas-completing-prompt, yas-maybe-ido-prompt, and yas-no-prompt), some of which may not support an alist of choices. As indicated in a comment above, the user is not confined to either yas-choose-value or yas-completing-read. The key ingredient is (unless (or yas-moving-away-p yas-modified-p) ...) with the ... needing to be replaced with the custom Lisp that does whatever you want. Here is a custom example that uses ido:

fopen('${1:./filename.txt}', '${2:$$(unless (or yas-moving-away-p yas-modified-p)
  (require 'ido)
  (let* ((choices-alist '(("r - Open read only" . "r")
                          ("w - Open for write erases content" . "w")
                          ("a - The 'a' description." . "a")
                          ("x - The 'x' description." . "x")
                          ("r+ - The 'r+' description." . "r+")
                          ("w+ - The 'w+' description." . "w+")
                          ("a+ - The 'a+' description." . "a+")
                          ("x+ - The 'x+' description." . "x+")))
         (choice (ido-completing-read "Please choose one:  " choices-alist nil 'confirm)))
    (cdr (assoc choice choices-alist))))}') or die();

The following ticket requests that the Emacs team implement support for ido tab completion using an alist collection, and I have provided a draft proof concept for said implementation: http://debbugs.gnu.org/cgi/bugreport.cgi?bug=46091

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.