# Power menu (Shutdown, Reboot, Sleep) in ELISP

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."
(interactive)
(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")))))))

• Seems simple enough, from where are you calling this?
– Mast
Apr 7, 2020 at 11:10
• 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. Apr 7, 2020 at 11:18
• 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". Apr 7, 2020 at 15:43

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