Emacs read multiple choice

Reading a string

As part of my personal write-up about skeletons, I learned about read-string. Here is an example:

(concat "Hello, " (read-string "name? "))
Hello, Stephen

Read multiple choice

Then I learned about read-multiple-choice. Below is the example in the documentation. When I execute it and respond with "s" the return value is this list: (115 "session only").

(read-multiple-choice "Continue connecting?"
                      '((?a "always")
                        (?s "session only")
                        (?n "no")))

Here I ask the user if they want to use a logarithm and if so what kind. (The use case is for creating a plot using R's lattice package.

(cdr (read-multiple-choice
      "logarithm axis?"
      '((?t "log = TRUE,")
        (?f "log = FALSE,")
        (?2 "log = 2,")
        (?e "log = TRUE,")    ; e xp()
        (?n "log = TRUE,")    ; n atural log
        (?c "log = 10,"))))   ; c ommon log

Wrapping the above in a function

We use substring and retain the 1st (not the 0th) and second to the last characters of the string. Without this, our string would include the parentheses.

(substring
 (format "%s"
         (cdr
          (read-multiple-choice "answer: "
                                '((?a "alpha") (?b "beta")))))
 1 -1)

Author: Stephen Weigand

Created: 2024-10-07 Mon 21:23

Validate