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.

Drew
  • 77,472
  • 10
  • 114
  • 243
Phoenix
  • 351
  • 1
  • 9

1 Answers1

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)))

Zeta
  • 1,093
  • 9
  • 18
  • 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 05: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 05:17
  • @Zeta, as written in the comment: v27.1. OS I am using is Fedora 32. – Phoenix Oct 21 '20 at 05:19
  • Did some more testing. After a restart and loading this "el"-file, positioning point onto "calendar-day-name-array", it will still write the help for "append" with "SEQUENCES" in bold. After I C-h v the variable, it shows what this variable is: calendar-day-name-array: Array of capitalized strings giving, in order from Sunday, the day names. So, something is off here. – Phoenix Oct 21 '20 at 05:27
  • @Phoenix I've tried a workaround with a customization variable that falls back on some predefined days. – Zeta Oct 21 '20 at 05:43
  • @Zeta, maybe it is my version of Emacs, but copy'n'pasting the second part will fail outright upon starting Emacs with Warning (initialization): An error occurred while loading ‘/Users/Portable/janms/.emacs.d/init.el’: and the error is the same message. If I can avoid using this pre-defined list and create one of my own, maybe that will solve it. I just never managed to create a list. Another thing seemingly more complicated than in other scripting languages, like bash, PHP or JavaScript. – Phoenix Oct 21 '20 at 05:49