-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcsound-mode.el
200 lines (169 loc) · 7.05 KB
/
csound-mode.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
;;; csound-mode.el --- A major mode for interacting and coding Csound
;; Copyright (C) 2017 - 2023 Hlöðver Sigurðsson
;; Author: Hlöðver Sigurðsson <[email protected]>
;; Version: 0.2.9
;; Package-Requires: ((emacs "25") (shut-up "0.3.2") (multi "2.0.1") (dash "2.16.0") (highlight "0"))
;; URL: https://github.com/hlolli/csound-mode
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; This file stores mode-specific bindings to `csound-mode`,
;; "offline" csound-interactions and major-mode definition,
;; syntax table.
;;; Code:
(require 'font-lock)
(require 'cl-lib)
(require 'csound-eldoc)
(require 'csound-font-lock)
(require 'csound-repl)
(require 'csound-indentation)
(require 'csound-score)
(require 'csound-skeleton)
(require 'csound-util)
(require 'csound-manual-lookup)
(require 'dash)
(require 'shut-up)
(defgroup csound-mode nil
"Tiny functionality enhancements for evaluating sexps."
:prefix "csound-mode-"
:group 'csound-mode)
(defvar csound-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?_ "w" st)
;; (modify-syntax-entry ?+ "w" st)
;; (modify-syntax-entry ?- "w" st)
(modify-syntax-entry ?. "w" st)
(modify-syntax-entry ?! "w" st)
(modify-syntax-entry ?% "-" st)
(modify-syntax-entry ?\" "\"\"" st)
;; Comment syntax
(modify-syntax-entry ?\; "<" st)
(modify-syntax-entry ?\n ">" st)
(modify-syntax-entry ?/ ". 12" st)
(modify-syntax-entry ?\n "> " st)
;; good read: https://www.lunaryorn.com/posts/syntactic-fontification-in-emacs.html
(modify-syntax-entry ?/ ". 14b" st)
(modify-syntax-entry ?* ". 23b" st)
st)
"Syntax table for csound-mode")
(defcustom csound-play-flags "-odac"
"Additional flags to pass to csound when playing the file in current buffer."
:group 'csound-mode
:type 'string)
(defcustom csound-render-flags ""
"Additional flags to pass to csound when rendering csound to file."
:group 'csound-mode
:type 'string)
(defun csound-play ()
"Play the csound file in current buffer."
(interactive)
(if csound-repl-start-server-p
(compile (format "csound %s %s" csound-play-flags (buffer-file-name)))
(process-send-string csound-repl--udp-client-proc
(buffer-substring
(point-min) (point-max)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; csound-abort-compilation
;;;
;;; DESCRIPTION:
;;; This function aborts the compilation of a Csound file. This is simply
;;; done by killing the compilation process in the main compilation buffer
;;; (i.e. *compilation*)
;;;
;;; TODO:
;;; - Make this context-sensitive, in case multiple compilation buffers are
;;; active in an Emacs session.
;;;
;;; author: Ruben Philipp
;;; created: 2023-12-26, Lütgendortmund
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun csound-abort-compilation ()
(interactive)
(let ((current-buffer (current-buffer)))
(switch-to-buffer "*compilation*")
(kill-compilation)
(switch-to-buffer current-buffer)))
(eval-after-load 'csound-mode
'(define-key csound-mode-map (kbd "C-c C-k") 'csound-abort-compilation))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
(defun csound-render (bit filename)
"Render csound to file."
(interactive
(list
(read-string "File bit-value(16/24/32), defaults to 16: ")
(read-string (format "Filename, defaults to %s.wav: " (file-name-base)))))
;;(compile (format "csound -o %s" (buffer-file-name)))
;; (message "var1: %s var2: %s" var1 var2)
(let ((filename (if (string= "" filename)
(concat (file-name-base) ".wav")
filename)))
(if csound-repl-start-server-p
(compile (format "csound %s %s -o %s --format=%s %s"
csound-render-flags
(buffer-file-name)
filename
(-> (split-string filename "\\.")
cl-rest cl-first)
(cl-case (string-to-number bit)
(32 "-f")
(24 "-3")
(t "-s"))))
(message "%s" "You did not start a csound server subprocess.
Configure rendering to a file in you CSD file's
<CsOptions> section." ))))
(defun csound-repl-start ()
"Start the csound-repl."
(interactive)
(if (and csound-repl-start-server-p
(not (executable-find "csound")))
(error "Csound is not installed on your computer")
(csound-repl--buffer-create)))
(defvar csound-mode-map nil)
(setq csound-mode-map
(let ((map (make-sparse-keymap)))
;; Offline keybindings
(define-key map (kbd "C-c C-p") 'csound-play)
(define-key map (kbd "C-c C-r") 'csound-render)
;; REPL Keybindings
(define-key map (kbd "C-c C-z") 'csound-repl-start)
(define-key map (kbd "C-M-x") 'csound-repl-evaluate-region)
(define-key map (kbd "C-c C-c") 'csound-repl-evaluate-region)
(define-key map (kbd "C-x C-e") 'csound-repl-evaluate-line)
(define-key map (kbd "C-c C-l") 'csound-repl-interaction-evaluate-last-expression)
;; Utilities
(define-key map (kbd "C-c C-s") 'csound-score-align-block)
(define-key map (kbd "M-.") 'csound-score-find-instr-def)
(define-key map (kbd "C-c C-n") 'csound-skeleton-new-csd)
;; (define-key map (kbd "C-c C-f") 'csound-repl-plot-ftgen)
map))
;;;###autoload
(define-derived-mode csound-mode
fundamental-mode "Csound Mode"
"A major mode for interacting and coding Csound"
:syntax-table csound-mode-syntax-table
(setq-local eldoc-documentation-function 'csound-eldoc-function)
(setq-local comment-start ";; ")
;; (setq-local comment-end "")
(setq-local indent-line-function 'csound-indentation-line)
(setq-local compilation-scroll-output t)
(setq-local ad-redefinition-action 'accept)
(setq-local font-lock-comment-end-skip "\n")
(add-hook 'completion-at-point-functions #'csound-util-opcode-completion-at-point nil t)
;; (add-hook 'skeleton-end-hook #'csound-font-lock-flush-buffer nil t)
(font-lock-add-keywords nil csound-font-lock-list t)
(shut-up
(with-silent-modifications
(csound-font-lock-flush-buffer)
(csound-font-lock--flush-score))))
;;;###autoload
(add-to-list 'auto-mode-alist `(,(concat "\\." (regexp-opt '("csd" "orc" "sco" "udo")) "\\'") . csound-mode))
(provide 'csound-mode)
;;; csound-mode.el ends here