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 b24cd761 2020-12-28 op (defvar sndio-sndioctl-cmd "sndioctl"
35 c708ecad 2020-12-19 op "Path to the sndioctl executable.")
36 c708ecad 2020-12-19 op
37 c708ecad 2020-12-19 op (defvar sndio-step 0.02
38 c708ecad 2020-12-19 op "Step for `sndio-increase' and `sndio-decrease'.")
39 c708ecad 2020-12-19 op
40 adbfb851 2021-11-09 op (defvar sndio--window nil
41 adbfb851 2021-11-09 op "The sndio window.")
42 adbfb851 2021-11-09 op
43 b24cd761 2020-12-28 op (defvar sndio-mode-map
44 b24cd761 2020-12-28 op (let ((m (make-sparse-keymap)))
45 b24cd761 2020-12-28 op (define-key m (kbd "n") #'forward-line)
46 b24cd761 2020-12-28 op (define-key m (kbd "p") #'previous-line)
47 b24cd761 2020-12-28 op (define-key m (kbd "i") #'sndio-increase)
48 b24cd761 2020-12-28 op (define-key m (kbd "d") #'sndio-decrease)
49 b24cd761 2020-12-28 op (define-key m (kbd "m") #'sndio-mute)
50 b24cd761 2020-12-28 op (define-key m (kbd "t") #'sndio-toggle)
51 b24cd761 2020-12-28 op (define-key m (kbd "g") #'sndio-update)
52 adbfb851 2021-11-09 op (define-key m (kbd "q") #'sndio-quit)
53 b24cd761 2020-12-28 op m)
54 b24cd761 2020-12-28 op "Keymap for sndio.")
55 b24cd761 2020-12-28 op
56 c708ecad 2020-12-19 op (define-derived-mode sndio-mode special-mode "sndio"
57 c708ecad 2020-12-19 op "Major mode for sndio interaction."
58 c708ecad 2020-12-19 op (buffer-disable-undo)
59 c708ecad 2020-12-19 op (sndio-update))
60 c708ecad 2020-12-19 op
61 c708ecad 2020-12-19 op (defun sndio-update ()
62 c708ecad 2020-12-19 op "Update the current sndio buffer."
63 c708ecad 2020-12-19 op (interactive)
64 a6477e78 2021-11-09 op (when (derived-mode-p 'sndio-mode)
65 b24cd761 2020-12-28 op (let ((inhibit-read-only t))
66 b24cd761 2020-12-28 op (erase-buffer)
67 ee48b410 2021-11-09 op (process-file sndio-sndioctl-cmd nil (current-buffer) nil)
68 ee48b410 2021-11-09 op (goto-char (point-min)))))
69 c708ecad 2020-12-19 op
70 c708ecad 2020-12-19 op (defun sndio--run (&rest args)
71 b24cd761 2020-12-28 op "Run `sndio-sndioctl-cmd' with ARGS yielding its output."
72 c708ecad 2020-12-19 op (with-temp-buffer
73 b24cd761 2020-12-28 op (when (zerop (apply #'process-file sndio-sndioctl-cmd nil t nil args))
74 c708ecad 2020-12-19 op (buffer-string))))
75 c708ecad 2020-12-19 op
76 c708ecad 2020-12-19 op (defun sndio--current-io ()
77 c708ecad 2020-12-19 op "Yield the input/poutput at point as string."
78 c708ecad 2020-12-19 op (when-let (end (save-excursion
79 c708ecad 2020-12-19 op (beginning-of-line)
80 c708ecad 2020-12-19 op (ignore-errors (search-forward "="))))
81 c708ecad 2020-12-19 op (buffer-substring-no-properties (line-beginning-position)
82 c708ecad 2020-12-19 op (1- end))))
83 c708ecad 2020-12-19 op
84 c708ecad 2020-12-19 op (defun sndio--update-value (x)
85 c708ecad 2020-12-19 op "Update the value for the input/output at point setting it to X."
86 c708ecad 2020-12-19 op (save-excursion
87 f05c8ffa 2020-12-20 op (beginning-of-line)
88 c708ecad 2020-12-19 op (search-forward "=")
89 c708ecad 2020-12-19 op (let ((inhibit-read-only t))
90 c708ecad 2020-12-19 op (delete-region (point) (line-end-position))
91 c708ecad 2020-12-19 op (insert (string-trim-right x)))))
92 c708ecad 2020-12-19 op
93 c708ecad 2020-12-19 op (defun sndio-increase ()
94 c708ecad 2020-12-19 op "Increase the volume for the input/output at point."
95 c708ecad 2020-12-19 op (interactive)
96 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
97 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=+" (number-to-string sndio-step))))
98 c708ecad 2020-12-19 op (sndio--update-value val))))
99 c708ecad 2020-12-19 op
100 c708ecad 2020-12-19 op (defun sndio-decrease ()
101 c708ecad 2020-12-19 op "Decrease the volume for the input/output at point."
102 c708ecad 2020-12-19 op (interactive)
103 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
104 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=-" (number-to-string sndio-step))))
105 c708ecad 2020-12-19 op (sndio--update-value val))))
106 c708ecad 2020-12-19 op
107 c708ecad 2020-12-19 op (defun sndio-mute ()
108 c708ecad 2020-12-19 op "Mute the input/output at point."
109 c708ecad 2020-12-19 op (interactive)
110 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
111 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=0")))
112 c708ecad 2020-12-19 op (sndio--update-value val))))
113 c708ecad 2020-12-19 op
114 c708ecad 2020-12-19 op (defun sndio-toggle ()
115 c708ecad 2020-12-19 op "Toggle input/output at point."
116 c708ecad 2020-12-19 op (interactive)
117 c708ecad 2020-12-19 op (when-let (x (sndio--current-io))
118 c708ecad 2020-12-19 op (when-let (val (sndio--run "-n" (concat x "=!")))
119 c708ecad 2020-12-19 op (sndio--update-value val))))
120 c708ecad 2020-12-19 op
121 adbfb851 2021-11-09 op (defun sndio-quit ()
122 adbfb851 2021-11-09 op "Quits sndio.
123 adbfb851 2021-11-09 op Call `delete-window' when the sndio popup window is open or
124 adbfb851 2021-11-09 op `quit-window' otherwise."
125 adbfb851 2021-11-09 op (interactive)
126 adbfb851 2021-11-09 op (if (window-live-p sndio--window)
127 adbfb851 2021-11-09 op (delete-window)
128 adbfb851 2021-11-09 op (quit-window)))
129 adbfb851 2021-11-09 op
130 c708ecad 2020-12-19 op ;;;###autoload
131 c708ecad 2020-12-19 op (defun sndio ()
132 c708ecad 2020-12-19 op "Launch sndio."
133 c708ecad 2020-12-19 op (interactive)
134 c708ecad 2020-12-19 op (switch-to-buffer "*sndio*")
135 c708ecad 2020-12-19 op (sndio-mode))
136 c708ecad 2020-12-19 op
137 827716d1 2021-11-09 op ;;;###autoload
138 adbfb851 2021-11-09 op (defun sndio-win-open ()
139 adbfb851 2021-11-09 op "Open an sndio window at the bottom of the frame for quick editing."
140 adbfb851 2021-11-09 op (interactive)
141 adbfb851 2021-11-09 op (unless (window-live-p sndio--window)
142 adbfb851 2021-11-09 op (setq sndio--window
143 adbfb851 2021-11-09 op (select-window
144 adbfb851 2021-11-09 op (let ((ignore-window-parameters t))
145 adbfb851 2021-11-09 op (split-window (frame-root-window)
146 adbfb851 2021-11-09 op -1
147 adbfb851 2021-11-09 op 'below))
148 adbfb851 2021-11-09 op 'norecord))
149 adbfb851 2021-11-09 op (switch-to-buffer (get-buffer-create "*sndio-quick*")
150 adbfb851 2021-11-09 op 'norecord)
151 adbfb851 2021-11-09 op (if (derived-mode-p 'sndio-mode)
152 adbfb851 2021-11-09 op (sndio-update)
153 adbfb851 2021-11-09 op (sndio-mode))
154 ef535dc5 2021-11-09 op (hl-line-mode +1)
155 adbfb851 2021-11-09 op (setq mode-line-format nil
156 adbfb851 2021-11-09 op header-line-format nil
157 adbfb851 2021-11-09 op tab-line-format nil)
158 adbfb851 2021-11-09 op (set-window-hscroll sndio--window 0)
159 adbfb851 2021-11-09 op (set-window-dedicated-p sndio--window t)
160 adbfb851 2021-11-09 op (select-window sndio--window 'norecord)
161 adbfb851 2021-11-09 op (let ((window-resize-pixelwise t)
162 adbfb851 2021-11-09 op (window-size-fixed))
163 adbfb851 2021-11-09 op (fit-window-to-buffer sndio--window nil nil 1))))
164 adbfb851 2021-11-09 op
165 c708ecad 2020-12-19 op (provide 'sndio)
166 b24cd761 2020-12-28 op ;;; sndio.el ends here