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