1 ;;; sndio.el --- Interact with sndio(8) -*- lexical-binding: t; -*-
3 ;; Copyright (C) 2020 Omar Polo
5 ;; Author: Omar Polo <op@omarpolo.com>
7 ;; Keywords: multimedia
8 ;; URL: https://git.omarpolo.com/sndioctl.el
12 ;; This package provides the sndio major mode to interact with
19 (defvar sndioctl-cmd "sndioctl"
20 "Path to the sndioctl executable.")
22 (defvar sndio-step 0.02
23 "Step for `sndio-increase' and `sndio-decrease'.")
25 (define-derived-mode sndio-mode special-mode "sndio"
26 "Major mode for sndio interaction."
28 (define-key sndio-mode-map (kbd "n") #'forward-line)
29 (define-key sndio-mode-map (kbd "p") #'previous-line)
30 (define-key sndio-mode-map (kbd "i") #'sndio-increase)
31 (define-key sndio-mode-map (kbd "d") #'sndio-decrease)
32 (define-key sndio-mode-map (kbd "m") #'sndio-mute)
33 (define-key sndio-mode-map (kbd "t") #'sndio-toggle)
34 (define-key sndio-mode-map (kbd "g") #'sndio-update)
35 (define-key sndio-mode-map (kbd "d") #'kill-buffer)
41 (defun sndio-update ()
42 "Update the current sndio buffer."
44 (with-current-buffer "*sndio*"
46 (let ((inhibit-read-only t))
48 (process-file sndioctl-cmd nil (current-buffer) nil)))))
50 (defun sndio--run (&rest args)
51 "Run `sndioctl-cmd' with ARGS yielding its output."
53 (when (zerop (apply #'process-file sndioctl-cmd nil t nil args))
56 (defun sndio--current-io ()
57 "Yield the input/poutput at point as string."
58 (when-let (end (save-excursion
60 (ignore-errors (search-forward "="))))
61 (buffer-substring-no-properties (line-beginning-position)
64 (defun sndio--update-value (x)
65 "Update the value for the input/output at point setting it to X."
68 (let ((inhibit-read-only t))
69 (delete-region (point) (line-end-position))
70 (insert (string-trim-right x)))))
72 (defun sndio-increase ()
73 "Increase the volume for the input/output at point."
75 (when-let (x (sndio--current-io))
76 (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
77 (sndio--update-value val))))
79 (defun sndio-decrease ()
80 "Decrease the volume for the input/output at point."
82 (when-let (x (sndio--current-io))
83 (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
84 (sndio--update-value val))))
87 "Mute the input/output at point."
89 (when-let (x (sndio--current-io))
90 (when-let (val (sndio--run "-n" (concat x "=0")))
91 (sndio--update-value val))))
93 (defun sndio-toggle ()
94 "Toggle input/output at point."
96 (when-let (x (sndio--current-io))
97 (when-let (val (sndio--run "-n" (concat x "=!")))
98 (sndio--update-value val))))
104 (switch-to-buffer "*sndio*")
108 ;;; sndio.el ends here.