I've written an interactive function which lets me shudown, reboot my machine as well as put it to sleep.

I'm using emacs and I'm just starting out getting into ELISP programming. As I'm a total beginner, I'd like to know what you would do differently in the following code to make it better, lighter and so on. Many thanks in advance.

(defun db/power-menu ()
  "Interactive menu for shutdown, reboot or sleep."
  (let ((actions '("Shutdown" "Reboot" "Sleep")))
    (setq action (ivy-completing-read "What do you want to do?" actions ))
    (if (y-or-n-p (concat "Execute " action "? Unsaved progress will be lost. "))
        (let ((default-directory "/sudo::"))
          (cond ((equal action "Shutdown")
                 (shell-command "systemctl poweroff"))
                ((equal action "Reboot")
                 (shell-command "systemctl reboot"))
                ((equal action "Sleep")
                 (shell-command "systemctl suspend")))))))
  • \$\begingroup\$ Seems simple enough, from where are you calling this? \$\endgroup\$
    – Mast
    Commented Apr 7, 2020 at 11:10
  • 1
    \$\begingroup\$ I'm just calling this manually at the moment. But I plan on intercepting the ACPI event when the power button is pressed to execute emacsclient and this function. \$\endgroup\$ Commented Apr 7, 2020 at 11:18
  • \$\begingroup\$ This is gathering close votes for having an incomplete description. You may want to separate talking about yourself and talking about the program. This will make it harder to mistake your description as just a long winded "I'm a beginner". \$\endgroup\$
    – Peilonrayz
    Commented Apr 7, 2020 at 15:43

1 Answer 1


Disclaimer: Total beginner here too. So take everything with a grain of salt.

That being said this looks perfectly fine. However, I suggest you to use completing-read instead of ivy-completing-read as it will provide more flexibility. Don't worry, if you use ivy-mode (or customize completing-read-function to ivy-completing-read), then completing-read will use ivy-completing-read.This is especially helpful if you want to copy your snippet to another Emacs configuration where ivy isn't installed.

Other than that, I guess you want to introduce action before you setq it. However, I'm not really sure whether it's necessary.

Note that I will probably use your db/power-menu in my configuration. Well done :)


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.