Blame


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