From 5785a5d126210e8c9651da038b6c8b083041fef6 Mon Sep 17 00:00:00 2001 From: William Carroll Date: Mon, 23 Dec 2019 17:31:42 +0000 Subject: Support prelude/start-process If you refer to the previous commit where I change shell-command usages to start-process function calls, you'll see the rationale for why I prefer start-process. This commit introduces a more ergonomic API for start-process that fits most of my current use-cases of it. This cleans up the code. I have introduced a bug in the way that I'm tokenizing the COMMAND value. I've tracked that with a TODO. For now it only affects the `xmodmap -e ''` calls, which isn't too disruptive. --- configs/shared/.emacs.d/wpc/display.el | 21 ++++++++---- configs/shared/.emacs.d/wpc/keyboard.el | 13 ++++--- configs/shared/.emacs.d/wpc/playback.el | 22 ++++++++++-- configs/shared/.emacs.d/wpc/prelude.el | 17 +++++++++- configs/shared/.emacs.d/wpc/pulse-audio.el | 43 +++++++++--------------- configs/shared/.emacs.d/wpc/screen-brightness.el | 24 ++++++------- configs/shared/.emacs.d/wpc/string.el | 5 ++- configs/shared/.emacs.d/wpc/wallpaper.el | 10 +++--- 8 files changed, 93 insertions(+), 62 deletions(-) diff --git a/configs/shared/.emacs.d/wpc/display.el b/configs/shared/.emacs.d/wpc/display.el index 716f3e5f5742..7f2f5e034609 100644 --- a/configs/shared/.emacs.d/wpc/display.el +++ b/configs/shared/.emacs.d/wpc/display.el @@ -13,6 +13,12 @@ ;;; Code: +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Dependencies +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(require 'prelude) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -31,17 +37,18 @@ (defun display/enable-4k () "Attempt to connect to my 4K monitor." (interactive) - (shell-command - (string/format "xrandr --output %s --dpi 144 --auto --right-of %s" - display/4k - display/primary))) + (prelude/start-process + :name "display" + :command (string/format "xrandr --output %s --dpi 144 --auto --right-of %s" + display/4k + display/primary))) (defun display/disable-4k () "Disconnect from the 4K monitor." (interactive) - (shell-command - (string/format "xrandr --output %s --off" - display/4k))) + (prelude/start-process + :name "display/disable-4k" + :command (string/format "xrandr --output %s --off" display/4k))) (provide 'display) ;;; display.el ends here diff --git a/configs/shared/.emacs.d/wpc/keyboard.el b/configs/shared/.emacs.d/wpc/keyboard.el index ac778d1698d8..176ac074dcd0 100644 --- a/configs/shared/.emacs.d/wpc/keyboard.el +++ b/configs/shared/.emacs.d/wpc/keyboard.el @@ -51,8 +51,9 @@ (rate keyboard/repeat-rate) (delay keyboard/repeat-delay)) "Use xset to set the key-repeat RATE and DELAY." - (shell-command - (string/format "xset r rate %s %s" delay rate))) + (prelude/start-process + :name "keyboard/set-key-repeat" + :command (string/format "xset r rate %s %s" delay rate))) ;; NOTE: Settings like this are machine-dependent. For instance I only need to ;; do this on my laptop and other devices where I don't have access to my split @@ -64,8 +65,12 @@ (defun keyboard/swap-caps-lock-and-escape () "Swaps the caps lock and escape keys using xmodmap." (interactive) - (shell-command "xmodmap -e 'remove Lock = Caps_Lock'") - (shell-command "xmodmap -e 'keysym Caps_Lock = Escape'")) + (prelude/start-process + :name "keyboard/swap-caps-lock-and-escape" + :command "xmodmap -e 'remove Lock = Caps_Lock'") + (prelude/start-process + :name "keyboard/swap-caps-lock-and-escape" + :command "xmodmap -e 'keysym Caps_Lock = Escape'")) (defun keyboard/inc-repeat-rate () "Increment `keyboard/repeat-rate'." diff --git a/configs/shared/.emacs.d/wpc/playback.el b/configs/shared/.emacs.d/wpc/playback.el index 9ab1e30ef0ac..e7ad4b2481a4 100644 --- a/configs/shared/.emacs.d/wpc/playback.el +++ b/configs/shared/.emacs.d/wpc/playback.el @@ -6,20 +6,36 @@ ;;; Code: +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Dependencies +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(require 'prelude) + +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Library +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + (defun playback/prev () "Move to the previous song." (interactive) - (shell-command "playerctl previous")) + (prelude/start-process + :name "playback/prev" + :command "playerctl previous")) (defun playback/next () "Move to the next song." (interactive) - (shell-command "playerctl next")) + (prelude/start-process + :name "playback/next" + :command "playerctl next")) (defun playback/play-pause () "Play or pause the current song." (interactive) - (shell-command "playerctl play-pause")) + (prelude/start-process + :name "playback/play-pause" + :command "playerctl play-pause")) (provide 'playback) ;;; playback.el ends here diff --git a/configs/shared/.emacs.d/wpc/prelude.el b/configs/shared/.emacs.d/wpc/prelude.el index fb459627899d..c1579d558985 100644 --- a/configs/shared/.emacs.d/wpc/prelude.el +++ b/configs/shared/.emacs.d/wpc/prelude.el @@ -20,7 +20,6 @@ (require 'dash) (require 'f) - ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Libraries ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -111,6 +110,22 @@ difficult to troubleshoot bugs in your init files." "Read input from user with PROMPT." (read-string prompt)) +;; TODO: Fix the bug with tokenizing here, since it will split any whitespace +;; character, (even though it shouldn't in the case of quoted string in shell). +;; e.g. - "xmodmap -e 'one two three'" => '("xmodmap" "-e" "'one two three'") +(cl-defun prelude/start-process (&key name command) + "Pass command string, COMMAND, and the function name, NAME. +This is a wrapper around `start-process' that has an API that resembles +`shell-command'." + (let* ((tokens (string/split " " command)) + (program-name (list/head tokens)) + (program-args (list/tail tokens))) + (apply #'start-process + `(,(string/format "*%s<%s>*" program-name name) + ,nil + ,program-name + ,@program-args)))) + (defun prelude/executable-exists? (name) "Return t if CLI tool NAME exists according to `exec-path'." (let ((file (locate-file name exec-path))) diff --git a/configs/shared/.emacs.d/wpc/pulse-audio.el b/configs/shared/.emacs.d/wpc/pulse-audio.el index 87e0c7c5ed30..b81f88c1b098 100644 --- a/configs/shared/.emacs.d/wpc/pulse-audio.el +++ b/configs/shared/.emacs.d/wpc/pulse-audio.el @@ -10,6 +10,7 @@ ;; Dependencies ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(require 'prelude) (require 'string) ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -33,49 +34,35 @@ (defun pulse-audio/toggle-mute () "Mute the default sink." (interactive) - (start-process - "*pactl*" - nil - "pactl" - "set-sink-mute" - "@DEFAULT_SINK@" - "toggle") + (prelude/start-process + :name "pulse-audio/toggle-mute" + :command "pactl set-sink-mute @DEFAULT_SINK@ toggle") (pulse-audio/message "Mute toggled.")) (defun pulse-audio/toggle-microphone () "Mute the default sink." (interactive) - (start-process - "*pactl*" - nil - "pactl" - "set-source-mute" - "@DEFAULT_SOURCE@" - "toggle") + (prelude/start-process + :name "pulse-audio/toggle-microphone" + :command "pactl set-source-mute @DEFAULT_SOURCE@ toggle") (pulse-audio/message "Microphone toggled.")) (defun pulse-audio/decrease-volume () "Low the volume output of the default sink." (interactive) - (start-process - "*pactl*" - nil - "pactl" - "set-sink-volume" - "@DEFAULT_SINK@" - (string/format "-%s%%" pulse-audio/step-size)) + (prelude/start-process + :name "pulse-audio/decrease-volume" + :command (string/format "pactl set-sink-volume @DEFAULT_SINK@ -%s%%" + pulse-audio/step-size)) (pulse-audio/message "Volume decreased.")) (defun pulse-audio/increase-volume () "Raise the volume output of the default sink." (interactive) - (start-process - "*pactl*" - nil - "pactl" - "set-sink-volume" - "@DEFAULT_SINK@" - (string/format "+%s%%" pulse-audio/step-size)) + (prelude/start-process + :name "pulse-audio/increase-volume" + :command (string/format "pactl set-sink-volume @DEFAULT_SINK@ +%s%%" + pulse-audio/step-size)) (pulse-audio/message "Volume increased.")) (when pulse-audio/install-kbds? diff --git a/configs/shared/.emacs.d/wpc/screen-brightness.el b/configs/shared/.emacs.d/wpc/screen-brightness.el index 8183196cccbc..715fc7baee35 100644 --- a/configs/shared/.emacs.d/wpc/screen-brightness.el +++ b/configs/shared/.emacs.d/wpc/screen-brightness.el @@ -8,6 +8,12 @@ ;; TODO: Define some isomorphisms. E.g. int->string, string->int. +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +;; Dependencies +;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; + +(require 'prelude) + ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;; Constants ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; @@ -25,23 +31,17 @@ (defun screen-brightness/increase () "Increase the screen brightness." (interactive) - (start-process - "*xbacklight*" - nil - "xbacklight" - "-inc" - (int-to-string screen-brightness/step-size)) + (prelude/start-process + :name "screen-brightness/increase" + :command (string/format "xbacklight -inc %s" screen-brightness/step-size)) (message "[screen-brightness.el] Increased screen brightness.")) (defun screen-brightness/decrease () "Decrease the screen brightness." (interactive) - (start-process - "*xbacklight*" - nil - "xbacklight" - "-dec" - (int-to-string screen-brightness/step-size)) + (prelude/start-process + :name "screen-brightness/decrease" + :command (string/format "xbacklight -dec %s" screen-brightness/step-size)) (message "[screen-brightness.el] Decreased screen brightness.")) (when screen-brightness/install-kbds? diff --git a/configs/shared/.emacs.d/wpc/string.el b/configs/shared/.emacs.d/wpc/string.el index 9ffe84572af5..1b766f5207a3 100644 --- a/configs/shared/.emacs.d/wpc/string.el +++ b/configs/shared/.emacs.d/wpc/string.el @@ -22,11 +22,14 @@ (defconst string/test? t "When t, run the tests.") -;; Strings (defun string/hookify (x) "Append \"-hook\" to X." (s-append "-hook" x)) +(defun string/split (y x) + "Map string X into a list of strings that were separated by Y." + (s-split y x)) + (defun string/ensure-hookified (x) "Ensure that X has \"-hook\" appended to it." (if (s-ends-with? "-hook" x) diff --git a/configs/shared/.emacs.d/wpc/wallpaper.el b/configs/shared/.emacs.d/wpc/wallpaper.el index 841fe5428e7a..08758aef09c7 100644 --- a/configs/shared/.emacs.d/wpc/wallpaper.el +++ b/configs/shared/.emacs.d/wpc/wallpaper.el @@ -10,6 +10,7 @@ ;; Dependencies ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; +(require 'prelude) (require 'fs) (require 'cycle) (require 'string) @@ -34,12 +35,9 @@ "Set computer wallpaper to image at `PATH' using `feh` under-the-hood. `PATH' can be absolute or relative since `f-expand' is called in the function body to ensure feh can resolve the path." - (start-process "*feh*" - nil - "feh" - "--bg-scale" - "--no-feh-bg" - (f-expand path))) + (prelude/start-process + :name "wallpaper/set" + :command (string/format "feh --bg-scale --no-feh-bg %s" (f-expand path)))) (defun wallpaper/whitelist-set (wallpaper) "Focuses the WALLPAPER in the `wallpaper/whitelist' cycle." -- cgit 1.4.1