Blame


1 c708ecad 2020-12-19 op ;;; sndio.el --- Interact with sndio(8) -*- lexical-binding: t; -*-
2 c708ecad 2020-12-19 op
3 e30060d1 2021-11-14 op ;; Copyright (C) 2020, 2021 Free Software Foundation, Inc.
4 c708ecad 2020-12-19 op
5 c708ecad 2020-12-19 op ;; Author: Omar Polo <op@omarpolo.com>
6 c708ecad 2020-12-19 op ;; Version: 1.0
7 c708ecad 2020-12-19 op ;; Keywords: multimedia
8 b24cd761 2020-12-28 op ;; URL: https://git.omarpolo.com/sndio.el
9 42f29add 2021-11-09 op ;; Package-Requires: ((emacs "25.1"))
10 c708ecad 2020-12-19 op
11 42f29add 2021-11-09 op ;; This program is free software; you can redistribute it and/or modify
12 42f29add 2021-11-09 op ;; it under the terms of the GNU General Public License as published by
13 42f29add 2021-11-09 op ;; the Free Software Foundation, either version 3 of the License, or
14 42f29add 2021-11-09 op ;; (at your option) any later version.
15 42f29add 2021-11-09 op
16 42f29add 2021-11-09 op ;; This program is distributed in the hope that it will be useful,
17 42f29add 2021-11-09 op ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 42f29add 2021-11-09 op ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 42f29add 2021-11-09 op ;; GNU General Public License for more details.
20 42f29add 2021-11-09 op
21 42f29add 2021-11-09 op ;; You should have received a copy of the GNU General Public License
22 42f29add 2021-11-09 op ;; along with this program. If not, see <https://www.gnu.org/licenses/>.
23 42f29add 2021-11-09 op
24 c708ecad 2020-12-19 op ;;; Commentary:
25 c708ecad 2020-12-19 op
26 c708ecad 2020-12-19 op ;; This package provides the sndio major mode to interact with
27 c708ecad 2020-12-19 op ;; OpenBSD' sndio(8).
28 c708ecad 2020-12-19 op
29 c708ecad 2020-12-19 op ;;; Code:
30 c708ecad 2020-12-19 op
31 264408fb 2021-11-09 op (eval-when-compile
32 264408fb 2021-11-09 op (require 'subr-x))
33 c708ecad 2020-12-19 op
34 dc51cb34 2021-11-09 op (defgroup sndio nil
35 dc51cb34 2021-11-09 op "Sndio."
36 dc51cb34 2021-11-09 op :group 'sndio)
37 c708ecad 2020-12-19 op
38 dc51cb34 2021-11-09 op (defcustom sndio-sndioctl-cmd "sndioctl"
39 dc51cb34 2021-11-09 op "Path to the sndioctl executable."
40 dc51cb34 2021-11-09 op :type 'string)
41 c708ecad 2020-12-19 op
42 dc51cb34 2021-11-09 op (defcustom sndio-step 0.02
43 dc51cb34 2021-11-09 op "Step for `sndio-increase' and `sndio-decrease'."
44 dc51cb34 2021-11-09 op :type 'integer)
45 dc51cb34 2021-11-09 op
46 adbfb851 2021-11-09 op (defvar sndio--window nil
47 adbfb851 2021-11-09 op "The sndio window.")
48 adbfb851 2021-11-09 op
49 b24cd761 2020-12-28 op (defvar sndio-mode-map
50 b24cd761 2020-12-28 op (let ((m (make-sparse-keymap)))
51 b24cd761 2020-12-28 op (define-key m (kbd "n") #'forward-line)
52 b24cd761 2020-12-28 op (define-key m (kbd "p") #'previous-line)
53 b24cd761 2020-12-28 op (define-key m (kbd "i") #'sndio-increase)
54 b24cd761 2020-12-28 op (define-key m (kbd "d") #'sndio-decrease)
55 b24cd761 2020-12-28 op (define-key m (kbd "m") #'sndio-mute)
56 b24cd761 2020-12-28 op (define-key m (kbd "t") #'sndio-toggle)
57 b24cd761 2020-12-28 op (define-key m (kbd "g") #'sndio-update)
58 adbfb851 2021-11-09 op (define-key m (kbd "q") #'sndio-quit)
59 b24cd761 2020-12-28 op m)
60 b24cd761 2020-12-28 op "Keymap for sndio.")
61 b24cd761 2020-12-28 op
62 c708ecad 2020-12-19 op (define-derived-mode sndio-mode special-mode "sndio"
63 c708ecad 2020-12-19 op "Major mode for sndio interaction."
64 c708ecad 2020-12-19 op (buffer-disable-undo)
65 c708ecad 2020-12-19 op (sndio-update))
66 c708ecad 2020-12-19 op
67 c708ecad 2020-12-19 op (defun sndio-update ()
68 c708ecad 2020-12-19 op "Update the current sndio buffer."
69 c708ecad 2020-12-19 op (interactive)
70 49269bd9 2021-11-14 op (unless (executable-find sndio-sndioctl-cmd)
71 49269bd9 2021-11-14 op (user-error "Can't find executable %s, is sndio installed?"
72 49269bd9 2021-11-14 op sndio-sndioctl-cmd))
73 a6477e78 2021-11-09 op (when (derived-mode-p 'sndio-mode)
74 b24cd761 2020-12-28 op (let ((inhibit-read-only t))
75 b24cd761 2020-12-28 op (erase-buffer)
76 ee48b410 2021-11-09 op (process-file sndio-sndioctl-cmd nil (current-buffer) nil)
77 ee48b410 2021-11-09 op (goto-char (point-min)))))
78 c708ecad 2020-12-19 op
79 c708ecad 2020-12-19 op (defun sndio--run (&rest args)
80 b24cd761 2020-12-28 op "Run `sndio-sndioctl-cmd' with ARGS yielding its output."
81 c708ecad 2020-12-19 op (with-temp-buffer
82 b24cd761 2020-12-28 op (when (zerop (apply #'process-file sndio-sndioctl-cmd nil t nil args))
83 c708ecad 2020-12-19 op (buffer-string))))
84 c708ecad 2020-12-19 op
85 c708ecad 2020-12-19 op (defun sndio--current-io ()
86 c708ecad 2020-12-19 op "Yield the input/poutput at point as string."
87 c708ecad 2020-12-19 op (when-let (end (save-excursion
88 c708ecad 2020-12-19 op (beginning-of-line)
89 c708ecad 2020-12-19 op (ignore-errors (search-forward "="))))
90 c708ecad 2020-12-19 op (buffer-substring-no-properties (line-beginning-position)
91 c708ecad 2020-12-19 op (1- end))))
92 c708ecad 2020-12-19 op
93 c708ecad 2020-12-19 op (defun sndio--update-value (x)
94 c708ecad 2020-12-19 op "Update the value for the input/output at point setting it to X."
95 c708ecad 2020-12-19 op (save-excursion
96 f05c8ffa 2020-12-20 op (beginning-of-line)
97 c708ecad 2020-12-19 op (search-forward "=")
98 c708ecad 2020-12-19 op (let ((inhibit-read-only t))
99 c708ecad 2020-12-19 op (delete-region (point) (line-end-position))
100 c708ecad 2020-12-19 op (insert (string-trim-right x)))))
101 c708ecad 2020-12-19 op
102 c708ecad 2020-12-19 op (defun sndio-increase ()
103 c708ecad 2020-12-19 op "Increase the volume for the input/output at point."
104 c708ecad 2020-12-19 op (interactive)
105 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
106 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
107 c708ecad 2020-12-19 op (sndio--update-value val))))
108 c708ecad 2020-12-19 op
109 c708ecad 2020-12-19 op (defun sndio-decrease ()
110 c708ecad 2020-12-19 op "Decrease the volume for the input/output at point."
111 c708ecad 2020-12-19 op (interactive)
112 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
113 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
114 c708ecad 2020-12-19 op (sndio--update-value val))))
115 c708ecad 2020-12-19 op
116 c708ecad 2020-12-19 op (defun sndio-mute ()
117 c708ecad 2020-12-19 op "Mute the input/output at point."
118 c708ecad 2020-12-19 op (interactive)
119 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
120 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=0")))
121 c708ecad 2020-12-19 op (sndio--update-value val))))
122 c708ecad 2020-12-19 op
123 c708ecad 2020-12-19 op (defun sndio-toggle ()
124 c708ecad 2020-12-19 op "Toggle input/output at point."
125 c708ecad 2020-12-19 op (interactive)
126 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
127 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=!")))
128 c708ecad 2020-12-19 op (sndio--update-value val))))
129 c708ecad 2020-12-19 op
130 adbfb851 2021-11-09 op (defun sndio-quit ()
131 adbfb851 2021-11-09 op "Quits sndio.
132 adbfb851 2021-11-09 op Call `delete-window' when the sndio popup window is open or
133 adbfb851 2021-11-09 op `quit-window' otherwise."
134 adbfb851 2021-11-09 op (interactive)
135 adbfb851 2021-11-09 op (if (window-live-p sndio--window)
136 adbfb851 2021-11-09 op (delete-window)
137 adbfb851 2021-11-09 op (quit-window)))
138 adbfb851 2021-11-09 op
139 c708ecad 2020-12-19 op ;;;###autoload
140 c708ecad 2020-12-19 op (defun sndio ()
141 c708ecad 2020-12-19 op "Launch sndio."
142 c708ecad 2020-12-19 op (interactive)
143 c708ecad 2020-12-19 op (switch-to-buffer "*sndio*")
144 c708ecad 2020-12-19 op (sndio-mode))
145 c708ecad 2020-12-19 op
146 827716d1 2021-11-09 op ;;;###autoload
147 adbfb851 2021-11-09 op (defun sndio-win-open ()
148 adbfb851 2021-11-09 op "Open an sndio window at the bottom of the frame for quick editing."
149 adbfb851 2021-11-09 op (interactive)
150 adbfb851 2021-11-09 op (unless (window-live-p sndio--window)
151 adbfb851 2021-11-09 op (setq sndio--window
152 adbfb851 2021-11-09 op (select-window
153 adbfb851 2021-11-09 op (let ((ignore-window-parameters t))
154 adbfb851 2021-11-09 op (split-window (frame-root-window)
155 adbfb851 2021-11-09 op -1
156 adbfb851 2021-11-09 op 'below))
157 adbfb851 2021-11-09 op 'norecord))
158 adbfb851 2021-11-09 op (switch-to-buffer (get-buffer-create "*sndio-quick*")
159 adbfb851 2021-11-09 op 'norecord)
160 adbfb851 2021-11-09 op (if (derived-mode-p 'sndio-mode)
161 adbfb851 2021-11-09 op (sndio-update)
162 adbfb851 2021-11-09 op (sndio-mode))
163 ef535dc5 2021-11-09 op (hl-line-mode +1)
164 adbfb851 2021-11-09 op (setq mode-line-format nil
165 adbfb851 2021-11-09 op header-line-format nil
166 adbfb851 2021-11-09 op tab-line-format nil)
167 adbfb851 2021-11-09 op (set-window-hscroll sndio--window 0)
168 adbfb851 2021-11-09 op (set-window-dedicated-p sndio--window t)
169 adbfb851 2021-11-09 op (select-window sndio--window 'norecord)
170 adbfb851 2021-11-09 op (let ((window-resize-pixelwise t)
171 adbfb851 2021-11-09 op (window-size-fixed))
172 adbfb851 2021-11-09 op (fit-window-to-buffer sndio--window nil nil 1))))
173 adbfb851 2021-11-09 op
174 c708ecad 2020-12-19 op (provide 'sndio)
175 b24cd761 2020-12-28 op ;;; sndio.el ends here