Blame


1 c708ecad 2020-12-19 op ;;; sndio.el --- Interact with sndio(8) -*- lexical-binding: t; -*-
2 c708ecad 2020-12-19 op
3 f1a89491 2021-11-09 op ;; Copyright (C) 2020, 2021 Omar Polo
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 a6477e78 2021-11-09 op (when (derived-mode-p 'sndio-mode)
71 b24cd761 2020-12-28 op (let ((inhibit-read-only t))
72 b24cd761 2020-12-28 op (erase-buffer)
73 ee48b410 2021-11-09 op (process-file sndio-sndioctl-cmd nil (current-buffer) nil)
74 ee48b410 2021-11-09 op (goto-char (point-min)))))
75 c708ecad 2020-12-19 op
76 c708ecad 2020-12-19 op (defun sndio--run (&rest args)
77 b24cd761 2020-12-28 op "Run `sndio-sndioctl-cmd' with ARGS yielding its output."
78 c708ecad 2020-12-19 op (with-temp-buffer
79 b24cd761 2020-12-28 op (when (zerop (apply #'process-file sndio-sndioctl-cmd nil t nil args))
80 c708ecad 2020-12-19 op (buffer-string))))
81 c708ecad 2020-12-19 op
82 c708ecad 2020-12-19 op (defun sndio--current-io ()
83 c708ecad 2020-12-19 op "Yield the input/poutput at point as string."
84 c708ecad 2020-12-19 op (when-let (end (save-excursion
85 c708ecad 2020-12-19 op (beginning-of-line)
86 c708ecad 2020-12-19 op (ignore-errors (search-forward "="))))
87 c708ecad 2020-12-19 op (buffer-substring-no-properties (line-beginning-position)
88 c708ecad 2020-12-19 op (1- end))))
89 c708ecad 2020-12-19 op
90 c708ecad 2020-12-19 op (defun sndio--update-value (x)
91 c708ecad 2020-12-19 op "Update the value for the input/output at point setting it to X."
92 c708ecad 2020-12-19 op (save-excursion
93 f05c8ffa 2020-12-20 op (beginning-of-line)
94 c708ecad 2020-12-19 op (search-forward "=")
95 c708ecad 2020-12-19 op (let ((inhibit-read-only t))
96 c708ecad 2020-12-19 op (delete-region (point) (line-end-position))
97 c708ecad 2020-12-19 op (insert (string-trim-right x)))))
98 c708ecad 2020-12-19 op
99 c708ecad 2020-12-19 op (defun sndio-increase ()
100 c708ecad 2020-12-19 op "Increase the volume for the input/output at point."
101 c708ecad 2020-12-19 op (interactive)
102 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
103 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
104 c708ecad 2020-12-19 op (sndio--update-value val))))
105 c708ecad 2020-12-19 op
106 c708ecad 2020-12-19 op (defun sndio-decrease ()
107 c708ecad 2020-12-19 op "Decrease the volume for the input/output at point."
108 c708ecad 2020-12-19 op (interactive)
109 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
110 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
111 c708ecad 2020-12-19 op (sndio--update-value val))))
112 c708ecad 2020-12-19 op
113 c708ecad 2020-12-19 op (defun sndio-mute ()
114 c708ecad 2020-12-19 op "Mute the input/output at point."
115 c708ecad 2020-12-19 op (interactive)
116 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
117 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=0")))
118 c708ecad 2020-12-19 op (sndio--update-value val))))
119 c708ecad 2020-12-19 op
120 c708ecad 2020-12-19 op (defun sndio-toggle ()
121 c708ecad 2020-12-19 op "Toggle input/output at point."
122 c708ecad 2020-12-19 op (interactive)
123 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
124 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=!")))
125 c708ecad 2020-12-19 op (sndio--update-value val))))
126 c708ecad 2020-12-19 op
127 adbfb851 2021-11-09 op (defun sndio-quit ()
128 adbfb851 2021-11-09 op "Quits sndio.
129 adbfb851 2021-11-09 op Call `delete-window' when the sndio popup window is open or
130 adbfb851 2021-11-09 op `quit-window' otherwise."
131 adbfb851 2021-11-09 op (interactive)
132 adbfb851 2021-11-09 op (if (window-live-p sndio--window)
133 adbfb851 2021-11-09 op (delete-window)
134 adbfb851 2021-11-09 op (quit-window)))
135 adbfb851 2021-11-09 op
136 c708ecad 2020-12-19 op ;;;###autoload
137 c708ecad 2020-12-19 op (defun sndio ()
138 c708ecad 2020-12-19 op "Launch sndio."
139 c708ecad 2020-12-19 op (interactive)
140 c708ecad 2020-12-19 op (switch-to-buffer "*sndio*")
141 c708ecad 2020-12-19 op (sndio-mode))
142 c708ecad 2020-12-19 op
143 827716d1 2021-11-09 op ;;;###autoload
144 adbfb851 2021-11-09 op (defun sndio-win-open ()
145 adbfb851 2021-11-09 op "Open an sndio window at the bottom of the frame for quick editing."
146 adbfb851 2021-11-09 op (interactive)
147 adbfb851 2021-11-09 op (unless (window-live-p sndio--window)
148 adbfb851 2021-11-09 op (setq sndio--window
149 adbfb851 2021-11-09 op (select-window
150 adbfb851 2021-11-09 op (let ((ignore-window-parameters t))
151 adbfb851 2021-11-09 op (split-window (frame-root-window)
152 adbfb851 2021-11-09 op -1
153 adbfb851 2021-11-09 op 'below))
154 adbfb851 2021-11-09 op 'norecord))
155 adbfb851 2021-11-09 op (switch-to-buffer (get-buffer-create "*sndio-quick*")
156 adbfb851 2021-11-09 op 'norecord)
157 adbfb851 2021-11-09 op (if (derived-mode-p 'sndio-mode)
158 adbfb851 2021-11-09 op (sndio-update)
159 adbfb851 2021-11-09 op (sndio-mode))
160 ef535dc5 2021-11-09 op (hl-line-mode +1)
161 adbfb851 2021-11-09 op (setq mode-line-format nil
162 adbfb851 2021-11-09 op header-line-format nil
163 adbfb851 2021-11-09 op tab-line-format nil)
164 adbfb851 2021-11-09 op (set-window-hscroll sndio--window 0)
165 adbfb851 2021-11-09 op (set-window-dedicated-p sndio--window t)
166 adbfb851 2021-11-09 op (select-window sndio--window 'norecord)
167 adbfb851 2021-11-09 op (let ((window-resize-pixelwise t)
168 adbfb851 2021-11-09 op (window-size-fixed))
169 adbfb851 2021-11-09 op (fit-window-to-buffer sndio--window nil nil 1))))
170 adbfb851 2021-11-09 op
171 c708ecad 2020-12-19 op (provide 'sndio)
172 b24cd761 2020-12-28 op ;;; sndio.el ends here