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)
40 (defun sndio-update ()
41 "Update the current sndio buffer."
43 (with-current-buffer "*sndio*"
45 (let ((inhibit-read-only t))
47 (process-file sndioctl-cmd nil (current-buffer) nil)))))
49 (defun sndio--run (&rest args)
50 "Run `sndioctl-cmd' with ARGS yielding its output."
52 (when (zerop (apply #'process-file sndioctl-cmd nil t nil args))
55 (defun sndio--current-io ()
56 "Yield the input/poutput at point as string."
57 (when-let (end (save-excursion
59 (ignore-errors (search-forward "="))))
60 (buffer-substring-no-properties (line-beginning-position)
63 (defun sndio--update-value (x)
64 "Update the value for the input/output at point setting it to X."
67 (let ((inhibit-read-only t))
68 (delete-region (point) (line-end-position))
69 (insert (string-trim-right x)))))
71 (defun sndio-increase ()
72 "Increase the volume for the input/output at point."
74 (when-let (x (sndio--current-io))
75 (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
76 (sndio--update-value val))))
78 (defun sndio-decrease ()
79 "Decrease the volume for the input/output at point."
81 (when-let (x (sndio--current-io))
82 (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
83 (sndio--update-value val))))
86 "Mute the input/output at point."
88 (when-let (x (sndio--current-io))
89 (when-let (val (sndio--run "-n" (concat x "=0")))
90 (sndio--update-value val))))
92 (defun sndio-toggle ()
93 "Toggle input/output at point."
95 (when-let (x (sndio--current-io))
96 (when-let (val (sndio--run "-n" (concat x "=!")))
97 (sndio--update-value val))))
103 (switch-to-buffer "*sndio*")
107 ;;; sndio.el ends here.