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 '<command-string>'` calls, which
isn't too disruptive.
This commit is contained in:
William Carroll 2019-12-23 17:31:42 +00:00
parent c078f04526
commit 5785a5d126
8 changed files with 93 additions and 62 deletions

View file

@ -13,6 +13,12 @@
;;; Code: ;;; Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'prelude)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Constants ;; Constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -31,17 +37,18 @@
(defun display/enable-4k () (defun display/enable-4k ()
"Attempt to connect to my 4K monitor." "Attempt to connect to my 4K monitor."
(interactive) (interactive)
(shell-command (prelude/start-process
(string/format "xrandr --output %s --dpi 144 --auto --right-of %s" :name "display"
:command (string/format "xrandr --output %s --dpi 144 --auto --right-of %s"
display/4k display/4k
display/primary))) display/primary)))
(defun display/disable-4k () (defun display/disable-4k ()
"Disconnect from the 4K monitor." "Disconnect from the 4K monitor."
(interactive) (interactive)
(shell-command (prelude/start-process
(string/format "xrandr --output %s --off" :name "display/disable-4k"
display/4k))) :command (string/format "xrandr --output %s --off" display/4k)))
(provide 'display) (provide 'display)
;;; display.el ends here ;;; display.el ends here

View file

@ -51,8 +51,9 @@
(rate keyboard/repeat-rate) (rate keyboard/repeat-rate)
(delay keyboard/repeat-delay)) (delay keyboard/repeat-delay))
"Use xset to set the key-repeat RATE and DELAY." "Use xset to set the key-repeat RATE and DELAY."
(shell-command (prelude/start-process
(string/format "xset r rate %s %s" delay rate))) :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 ;; 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 ;; 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 () (defun keyboard/swap-caps-lock-and-escape ()
"Swaps the caps lock and escape keys using xmodmap." "Swaps the caps lock and escape keys using xmodmap."
(interactive) (interactive)
(shell-command "xmodmap -e 'remove Lock = Caps_Lock'") (prelude/start-process
(shell-command "xmodmap -e 'keysym Caps_Lock = Escape'")) :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 () (defun keyboard/inc-repeat-rate ()
"Increment `keyboard/repeat-rate'." "Increment `keyboard/repeat-rate'."

View file

@ -6,20 +6,36 @@
;;; Code: ;;; Code:
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'prelude)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Library
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun playback/prev () (defun playback/prev ()
"Move to the previous song." "Move to the previous song."
(interactive) (interactive)
(shell-command "playerctl previous")) (prelude/start-process
:name "playback/prev"
:command "playerctl previous"))
(defun playback/next () (defun playback/next ()
"Move to the next song." "Move to the next song."
(interactive) (interactive)
(shell-command "playerctl next")) (prelude/start-process
:name "playback/next"
:command "playerctl next"))
(defun playback/play-pause () (defun playback/play-pause ()
"Play or pause the current song." "Play or pause the current song."
(interactive) (interactive)
(shell-command "playerctl play-pause")) (prelude/start-process
:name "playback/play-pause"
:command "playerctl play-pause"))
(provide 'playback) (provide 'playback)
;;; playback.el ends here ;;; playback.el ends here

View file

@ -20,7 +20,6 @@
(require 'dash) (require 'dash)
(require 'f) (require 'f)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Libraries ;; Libraries
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -111,6 +110,22 @@ difficult to troubleshoot bugs in your init files."
"Read input from user with PROMPT." "Read input from user with PROMPT."
(read-string 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) (defun prelude/executable-exists? (name)
"Return t if CLI tool NAME exists according to `exec-path'." "Return t if CLI tool NAME exists according to `exec-path'."
(let ((file (locate-file name exec-path))) (let ((file (locate-file name exec-path)))

View file

@ -10,6 +10,7 @@
;; Dependencies ;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'prelude)
(require 'string) (require 'string)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -33,49 +34,35 @@
(defun pulse-audio/toggle-mute () (defun pulse-audio/toggle-mute ()
"Mute the default sink." "Mute the default sink."
(interactive) (interactive)
(start-process (prelude/start-process
"*pactl<pulse-audio/toggle-mute>*" :name "pulse-audio/toggle-mute"
nil :command "pactl set-sink-mute @DEFAULT_SINK@ toggle")
"pactl"
"set-sink-mute"
"@DEFAULT_SINK@"
"toggle")
(pulse-audio/message "Mute toggled.")) (pulse-audio/message "Mute toggled."))
(defun pulse-audio/toggle-microphone () (defun pulse-audio/toggle-microphone ()
"Mute the default sink." "Mute the default sink."
(interactive) (interactive)
(start-process (prelude/start-process
"*pactl<pulse-audio/toggle-mute>*" :name "pulse-audio/toggle-microphone"
nil :command "pactl set-source-mute @DEFAULT_SOURCE@ toggle")
"pactl"
"set-source-mute"
"@DEFAULT_SOURCE@"
"toggle")
(pulse-audio/message "Microphone toggled.")) (pulse-audio/message "Microphone toggled."))
(defun pulse-audio/decrease-volume () (defun pulse-audio/decrease-volume ()
"Low the volume output of the default sink." "Low the volume output of the default sink."
(interactive) (interactive)
(start-process (prelude/start-process
"*pactl<pulse-audio/toggle-mute>*" :name "pulse-audio/decrease-volume"
nil :command (string/format "pactl set-sink-volume @DEFAULT_SINK@ -%s%%"
"pactl" pulse-audio/step-size))
"set-sink-volume"
"@DEFAULT_SINK@"
(string/format "-%s%%" pulse-audio/step-size))
(pulse-audio/message "Volume decreased.")) (pulse-audio/message "Volume decreased."))
(defun pulse-audio/increase-volume () (defun pulse-audio/increase-volume ()
"Raise the volume output of the default sink." "Raise the volume output of the default sink."
(interactive) (interactive)
(start-process (prelude/start-process
"*pactl<pulse-audio/toggle-mute>*" :name "pulse-audio/increase-volume"
nil :command (string/format "pactl set-sink-volume @DEFAULT_SINK@ +%s%%"
"pactl" pulse-audio/step-size))
"set-sink-volume"
"@DEFAULT_SINK@"
(string/format "+%s%%" pulse-audio/step-size))
(pulse-audio/message "Volume increased.")) (pulse-audio/message "Volume increased."))
(when pulse-audio/install-kbds? (when pulse-audio/install-kbds?

View file

@ -8,6 +8,12 @@
;; TODO: Define some isomorphisms. E.g. int->string, string->int. ;; TODO: Define some isomorphisms. E.g. int->string, string->int.
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'prelude)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Constants ;; Constants
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
@ -25,23 +31,17 @@
(defun screen-brightness/increase () (defun screen-brightness/increase ()
"Increase the screen brightness." "Increase the screen brightness."
(interactive) (interactive)
(start-process (prelude/start-process
"*xbacklight<screen-brightness/increase>*" :name "screen-brightness/increase"
nil :command (string/format "xbacklight -inc %s" screen-brightness/step-size))
"xbacklight"
"-inc"
(int-to-string screen-brightness/step-size))
(message "[screen-brightness.el] Increased screen brightness.")) (message "[screen-brightness.el] Increased screen brightness."))
(defun screen-brightness/decrease () (defun screen-brightness/decrease ()
"Decrease the screen brightness." "Decrease the screen brightness."
(interactive) (interactive)
(start-process (prelude/start-process
"*xbacklight<screen-brightness/decrease>*" :name "screen-brightness/decrease"
nil :command (string/format "xbacklight -dec %s" screen-brightness/step-size))
"xbacklight"
"-dec"
(int-to-string screen-brightness/step-size))
(message "[screen-brightness.el] Decreased screen brightness.")) (message "[screen-brightness.el] Decreased screen brightness."))
(when screen-brightness/install-kbds? (when screen-brightness/install-kbds?

View file

@ -22,11 +22,14 @@
(defconst string/test? t (defconst string/test? t
"When t, run the tests.") "When t, run the tests.")
;; Strings
(defun string/hookify (x) (defun string/hookify (x)
"Append \"-hook\" to X." "Append \"-hook\" to X."
(s-append "-hook" 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) (defun string/ensure-hookified (x)
"Ensure that X has \"-hook\" appended to it." "Ensure that X has \"-hook\" appended to it."
(if (s-ends-with? "-hook" x) (if (s-ends-with? "-hook" x)

View file

@ -10,6 +10,7 @@
;; Dependencies ;; Dependencies
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(require 'prelude)
(require 'fs) (require 'fs)
(require 'cycle) (require 'cycle)
(require 'string) (require 'string)
@ -34,12 +35,9 @@
"Set computer wallpaper to image at `PATH' using `feh` under-the-hood. "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 `PATH' can be absolute or relative since `f-expand' is called in the function
body to ensure feh can resolve the path." body to ensure feh can resolve the path."
(start-process "*feh<wallpaper/set>*" (prelude/start-process
nil :name "wallpaper/set"
"feh" :command (string/format "feh --bg-scale --no-feh-bg %s" (f-expand path))))
"--bg-scale"
"--no-feh-bg"
(f-expand path)))
(defun wallpaper/whitelist-set (wallpaper) (defun wallpaper/whitelist-set (wallpaper)
"Focuses the WALLPAPER in the `wallpaper/whitelist' cycle." "Focuses the WALLPAPER in the `wallpaper/whitelist' cycle."