+(progn ; display system volume in mode-line
+ (defvar b/volume-level 0)
+ (defvar b/volume-mute nil)
+ (defvar b/volume-level-mic 0)
+ (defvar b/volume-mute-mic nil)
+
+ (defun b/volume-get (&optional use-default-source)
+ "Get the default sink volume and mute state.
+If USE-DEFAULT-SOURCE is non-nill, use the default source (e.g. a
+microphone) instead of the default sink."
+ (pcase (split-string ; expecting: <mute> <volume> (e.g. true 15)
+ (string-trim
+ (shell-command-to-string
+ (mapconcat
+ #'identity
+ `("pamixer"
+ ,(when use-default-source "--default-source")
+ "--get-volume"
+ "--get-mute")
+ " "))))
+ (`(,m ,v)
+ (let ((mute (string= "true" m))
+ (volume (string-to-number v)))
+ `(,mute ,volume)))))
+
+ (defun b/volume-update ()
+ "Update system sound volume as displayed in mode-line."
+ (let ((changed))
+ (pcase (b/volume-get)
+ (`(,mute ,volume)
+ (unless (eq mute b/volume-mute)
+ (setq b/volume-mute mute
+ changed t))
+ (unless (= volume b/volume-level)
+ (setq b/volume-level volume
+ changed t))))
+ (pcase (b/volume-get 'mic)
+ (`(,mute ,volume)
+ (unless (eq mute b/volume-mute-mic)
+ (setq b/volume-mute-mic mute
+ changed t))
+ (unless (= volume b/volume-level-mic)
+ (setq b/volume-level-mic volume
+ changed t))))
+ (when changed
+ (force-mode-line-update))))
+
+ (defvar b/volume-timer (run-at-time nil 5 #'b/volume-update))
+
+ (setq-default
+ mode-line-format
+ (append
+ mode-line-format
+ '((:eval
+ (format
+ " [%s%%%%%s %s%%%%%s]"
+ (number-to-string b/volume-level)
+ (if b/volume-mute "-" "+")
+ (number-to-string b/volume-level-mic)
+ (if b/volume-mute-mic "-" "+")))))))
+