0

While there are org-templates creating entire bullet points with content, what I am looking for is a simple user query (similar to read-string) where the user can use a pre-defined list of words/phrases to choose from (up/down or left/right arrow keys).

Background:

I am with our IT department and among other things order hardware for our users. Once an order is out I would like to tell them when their hardware is expected to arrive. The general text stays the same, but I would like to choose the week's workdays from a list to complete this text snippet. Plus points, if I can have a second choice based on the first to choose this or next week. This means when I choose "today" or "tomorrow", it shall not ask for the week. Likewise choosing "Monday" or "Tuesday" shall fix the second choice to "next week" (since otherwise I could have chosen "today" or "tomorrow" if it were this week).

Simplified result (with choices highlighted):

I ordered your hardware and it is estimated to arrive Wednesday next week.

Currently I realize that snippet with a script inside the aging AutoKey GTK application, but would love to consolidate what I have there into Emacs. I already have my phrase templates as well as full-auto scripts in Emacs, but I am struggling to get the user input stuff to work the way I would like to have it.

read-string has a history argument, but I am not sure if one can misuse(?) that for what I intend. Being still fairly new to elisp, maybe there are better or more sophisticated ways to do so. Since Emacs' built-in help as well as most online resources are notoriously sparse on examples to demonstrate its capabilities, an example would go a long way.

1
2

This sounds like completing-read, maybe via ivy. Here's a quick snippet that reuses calendar-day-name-array to get the usual names, but you can use your usual service days instead:

(require 'calendar)  ; for calendar-day-name-array
(defun my/arrival ()
  "Place an arrival date"
  (interactive)
  (let ((choice (completing-read "Arrival: " (append calendar-day-name-array '("today" "tomorrow")))))
    (pcase choice
      ("today" choice)     ; Return today and tomorrow as-is
      ("tomorrow" choice)
      (other
       (let ((week (completing-read (format "Which %s? " choice) '("this week" "next week"))))
         (format "%s %s" choice week))))))

For more flexibility, you can customize the possible arrival days and also add a function to insert your choice at point:

(require 'calendar)
(defcustom my/arrivaldays
  (append 
    (or calendar-day-name-array 
       '("Monday" "Tuesday" "Wednesday" "Thursday" "Friday")) nil)
  "The possible arrival dates of hardware."
  :type '(repeat string))  

(defun my/arrival ()
  "Ask for an arrival date"
  (interactive)
  (let ((choice (completing-read "Arrival: " (append my/arrivaldays '("today" "tomorrow")))))
    (pcase choice
      ("today" choice)     ; Return today and tomorrow as-is
      ("tomorrow" choice)
      (other
       (let ((week (completing-read (format "Which %s? " choice) '("this week" "next week"))))
         (format "%s %s" choice week))))))

(defun my/arrival-insert ()
  "Insert an arrival date at point"
  (interactive)
  (insert (my/arrival)))
9
  • I have seen no problems with the Lisp you have posted so far in your recent answers.
    – NickD
    Oct 20 '20 at 20:32
  • @NickD Thanks for the feedback, very appreciated!
    – Zeta
    Oct 20 '20 at 21:25
  • Strangely, this works for me only after I C-h v the variable "calendar-day-name-array". Otherwise after a restart of Emacs (v27.1) it ends up with completing-read: Symbol’s value as variable is void: calendar-day-name-array. Do I need to initialize this variable and/or customize it to work?
    – Phoenix
    Oct 21 '20 at 5:10
  • @Phoenix Hm, that's strange. At least on my plattform (Emacs 26.3 on Kubuntu 18.04) It's initialized to the usual days. What's your Emacs version?
    – Zeta
    Oct 21 '20 at 5:17
  • @Zeta, as written in the comment: v27.1. OS I am using is Fedora 32.
    – Phoenix
    Oct 21 '20 at 5:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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