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

View file

@ -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'."

View file

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

View file

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

View file

@ -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<pulse-audio/toggle-mute>*"
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<pulse-audio/toggle-mute>*"
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<pulse-audio/toggle-mute>*"
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<pulse-audio/toggle-mute>*"
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?

View file

@ -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<screen-brightness/increase>*"
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<screen-brightness/decrease>*"
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?

View file

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

View file

@ -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<wallpaper/set>*"
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."