-
Notifications
You must be signed in to change notification settings - Fork 14
/
Copy pathcsound-indentation.el
398 lines (363 loc) · 17.3 KB
/
csound-indentation.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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
;;; csound-indentation.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:
;; Indentation rules for csound-mode
;;; Code:
(require 'csound-util)
(require 'csound-score)
;;; define some customizable variables
(defcustom csound-indentation-spaces 2
"Set how many spaces are in indentation."
:type 'integer
:group 'csound-mode)
(defcustom csound-indentation-aggressive-score nil
"If true, then align blocks will be called for every indent
calls in a score file or within CsScore tags. Works well
when used in combination with aggressive-indent mode.
(defaults to nil(false))"
:type 'boolean
:group 'csound-mode)
(defcustom csound-indentation-indent-goto nil
"If true, then anything that comes after goto symbol
will be indented."
:type 'boolean
:group 'csound-mode)
;;; define some predicate functions for checking regexp
(defun csound-indentation--current-line-breaks-p ()
"returns t if a \ is found not in the middle of a word in the current line, otherwise nil"
(if (save-excursion
(beginning-of-line 0)
(search-forward-regexp
"\\B\\\\\\B" (csound-util-line-boundry) t 1))
t nil))
(defun csound-indentation--previous-line-breaks-p ()
"returns t if a \ not in the middle of word is found in the previous
line, otherwise nil"
(if (save-excursion
(beginning-of-line 0)
(search-forward-regexp "\\B\\\\\\B" (csound-util-line-boundry) t 1))
t nil))
(defun csound-indentation--current-line-empty-p ()
"returns t if the current line is empty, otherwise nil"
(and
(save-excursion
(beginning-of-line)
;; (looking-at-p "[[:blank:]]*$") ; is this a mistake?
(search-forward-regexp "[[:blank:]]*$" (csound-util-line-boundry)
t 1)) ; did you mean this instead?
;; don't account for line continuation token being empty
(not (and (csound-indentation--previous-line-breaks-p)
(csound-indentation--current-line-breaks-p)))))
(defun csound-indentation--pointer-inside-paren-p ()
"returns t when pointer is inside a parenthesis, otherwise nil"
(let* ((case-fold-search nil)
(last-open (save-excursion (search-backward-regexp "(" nil t)))
(last-closed (save-excursion (search-backward-regexp ")" nil t))))
(cond ((eq 'nil last-open) nil)
((and (numberp last-open) (eq 'nil last-closed)) t)
((< last-closed last-open) t)
(t nil))))
(defun csound-indentation--cursor-behind-indentation-point-p ()
"returns t if the cursor is behind an indentation point, which is
whitespace in the beginning of a line, otherwise nil"
(> (or
(save-excursion
(beginning-of-line)
;; (search-forward-regexp "^\\s-*" (csound-util-line-boundry) t
;; 1) ; is this a mistake?
(search-forward-regexp "^\\s-+" (csound-util-line-boundry) t
1)) ; did you mean this instead?
(point))
(point)))
(defun csound-indentation-begin-of-expr-p ()
"returns t when in the next line a instr or opcode block starts, otherwise nil"
(save-excursion
(beginning-of-line 1)
(search-forward-regexp "\\b\\(instr\\|opcode\\)\\b" (line-end-position 1) t)))
(defun csound-indentation-end-of-expr-p ()
"returns t when in the next line a instr or opcode block is closed, otherwise nil"
(save-excursion
(beginning-of-line 1)
(search-forward-regexp "\\b\\(endin\\|endop\\)\\b" (line-end-position 1) t)))
(defun csound-indentation-inside-instr-p ()
"returns t when point is inside a instr block, otherwise nil"
(let* ((case-fold-search nil)
(last-instr (save-excursion (search-backward-regexp "^\\s-*\\(instr\\)\\b" nil t)))
(last-endin (save-excursion (search-backward-regexp "^\\s-*\\(endin\\)\\b" nil t))))
(cond ((eq 'nil last-instr) nil)
((and (numberp last-instr) (eq 'nil last-endin)) t)
((< last-endin last-instr) t)
(t nil))))
(defun csound-indentation-inside-opcode-p ()
"returns t when point is inside a opcode block, otherwise nil"
(let* ((case-fold-search nil)
(last-opcode (save-excursion (search-backward-regexp "^\\s-*\\(opcode\\)\\b" nil t)))
(last-endop (save-excursion (search-backward-regexp "^\\s-*\\(endop\\)\\b" nil t))))
(cond ((eq 'nil last-opcode) nil)
((and (numberp last-opcode) (eq 'nil last-endop)) t)
((< last-endop last-opcode) t)
(t nil))))
(defun csound-indentation-beginning-of-bool-p ()
"returns 1 when the next line a boolean expressions starts but without a goto,
otherwise 0"
(save-excursion
(beginning-of-line 1)
(if (and (search-forward-regexp
"\\<\\(if\\|while\\|else\\|elseif\\|until\\)\\>"
(csound-util-line-boundry) t 1)
;; if in mix with gotos
;; dont have endif therefore
;; dont create logical blocks
(prog2
(beginning-of-line)
(not (search-forward-regexp "\\<\\(kgoto\\|igoto\\|goto\\)\\>" (csound-util-line-boundry) t 1))))
1 0)))
(defun csound-indentation-end-of-bool-p ()
"returns 1 when in the next line a boolean expresion is closed,
otherwise 0"
(save-excursion
(beginning-of-line 1)
(if (search-forward-regexp
"\\<\\(endif\\|od\\|else\\|elseif\\)\\>"
(csound-util-line-boundry) t 1)
1 0)))
;;; not needed
;; (defun csound-indentation-count-goto-if-mix
;; (end-of-expr cnt current-depth)
;; "if"
;; (if (or (> current-depth 50) (<= end-of-expr (point)))
;; ;; if curren-depth is > 50 or pointer is behind the end of the
;; ;; current instr or opcode block return cnt
;; cnt
;; ;; else do this
;; (prog2
;; (beginning-of-line 1) ;; go down one line
;; (if ;; if a if statement and a goto statement is found count
;; ;; upwards, if not return current cnt
;; (and (search-forward-regexp "\\<\\(if\\)\\>" (csound-util-line-boundry) t 1)
;; ;; (search-forward-regexp "\\<\\(goto\\)\\>"
;; ;; (csound-util-line-boundry) t
;; ;; 1)
;; ;; (search-forward-regexp "\\<\\(igoto\\|kgoto\\|goto\\)\\>"
;; ;; (csound-util-line-boundry) t
;; ;; 1)
;; (search-forward-regexp "\\<\\(kgoto\\|igoto\\|goto\\)\\>"
;; (csound-util-line-boundry) t
;; 1))
;; (csound-indentation-count-goto-if-mix end-of-expr (1+ cnt)
;; (1+ current-depth))
;; (csound-indentation-count-goto-if-mix end-of-expr cnt (1+ current-depth))))))
;;; this function is never used
;; (defun csound-indentation-expression-first-arg (expr-string non-comma-cnt initial-recur-p)
;; (let ((trimmed-str (csound-util-chomp expr-string)))
;; (let* ((beginning-of-delimination (string-match "[\s\t,]" trimmed-str 0))
;; (end-of-delimination (when beginning-of-delimination
;; (string-match "[^\s^\t^,]" trimmed-str beginning-of-delimination)))
;; (comma-inbetween (when end-of-delimination
;; (string-match-p ","
;; (substring-no-properties trimmed-str beginning-of-delimination
;; end-of-delimination))))
;; (next-form (when end-of-delimination
;; (substring-no-properties trimmed-str end-of-delimination (length trimmed-str))))
;; (next-beginning (string-match "[\s\t,]" next-form 0))
;; (next-end (when next-beginning
;; (string-match "[^\s^\t^,]" next-form next-beginning)))
;; (next-comma (when next-end
;; (string-match-p ","
;; (substring-no-properties next-form next-beginning next-end)))))
;; (if (or (not (= 0 non-comma-cnt))
;; (and initial-recur-p (not comma-inbetween) next-comma))
;; (substring-no-properties trimmed-str end-of-delimination
;; (or (string-match "[\s\|\t]" trimmed-str end-of-delimination)
;; (length trimmed-str)))
;; (when next-form
;; (csound-indentation-expression-first-arg
;; next-form
;; (if comma-inbetween non-comma-cnt (1+ non-comma-cnt)) nil))))))
;;; this functions is never used
;; (defun csound-indentation-line-break-indent ()
;; ;; If it's the second occourance in a row
;; (if (save-excursion
;; (beginning-of-line 0)
;; (csound-indentation-line-break-escape-p) ; this function
;; ; doesn't exists
;; )
;; (indent-line-to (save-excursion
;; (beginning-of-line 0)
;; (search-forward-regexp "[^\s^\t]" (line-end-position 1) t 1)
;; (1- (current-column))))
;; (let* ((assignment-operator (save-excursion
;; (beginning-of-line 0)
;; (search-forward-regexp "=" (csound-util-line-boundry) t 1)))
;; (paren-open (when assignment-operator
;; (save-excursion
;; (goto-char assignment-operator)
;; (search-forward-regexp "(" (csound-util-line-boundry) t 1)))))
;; (cond
;; ;; ivar = poscil( |ival, ival2)
;; ((and assignment-operator paren-open)
;; (indent-line-to (save-excursion
;; (goto-char paren-open)
;; (search-forward-regexp "[^\s^\t^(]" (csound-util-line-boundry) t 1)
;; (1- (current-column)))))
;; ;; ivar = |"coulbeanything"
;; ((and assignment-operator (not paren-open))
;; (indent-line-to (save-excursion
;; (goto-char assignment-operator)
;; (search-forward-regexp "[^\s^\t]" (csound-util-line-boundry) t 1)
;; (current-column))))
;; ;; First element that has comma after an element without comma
;; (t (let* ((first-arg
;; (csound-indentation-expression-first-arg
;; (csound-util-remove-comment-in-string
;; (buffer-substring-no-properties
;; (line-beginning-position 0)
;; (line-end-position 0)))
;; 0 t))
;; (first-arg-pos
;; (save-excursion
;; (beginning-of-line 0)
;; (search-forward first-arg (line-end-position 1) t 1)
;; (current-column))))
;; (if (and first-arg first-arg-pos)
;; (indent-line-to (- first-arg-pos (length first-arg)))
;; ;; Fallback, ident +2 from first non-whitespace char above
;; (indent-line-to
;; (save-excursion
;; (beginning-of-line 0)
;; (search-forward-regexp "[^\s^\t]" (csound-util-line-boundry) t 1)
;; (current-column))))))))))
(defun csound-indentation-inside-expression-calc (expr-type)
"calculates the indentation level for current line an indent it"
(let* ((beginning-of-expr (if (eq 'instr expr-type)
(save-excursion
(search-backward-regexp "\\s-*\\<\\(instr\\)\\b" nil t))
(save-excursion
(search-backward-regexp "\\s-*\\<\\(opcode\\)\\b" nil t))))
(end-of-expr (or (if (eq 'instr expr-type)
(save-excursion
(search-forward-regexp "\\s-*\\<\\(endin\\)\\b" nil t))
(save-excursion
(search-forward-regexp "\\s-*\\<\\(endop\\)\\b" nil t)))
(point-max)))
(ending-of-current-line (line-end-position))
(expression-to-point (replace-regexp-in-string
"\".*\"\\|;;.*\\|//.*" ""
(buffer-substring beginning-of-expr (line-end-position 1)) ))
(expression-to-line-above (buffer-substring beginning-of-expr (line-end-position 0)))
(count-if-statements (save-excursion
(csound-util-recursive-count
"\\<\\(if\\)\\((\\|\\>\\)"
expression-to-point 0)))
(goto-if-mix (save-excursion
(csound-util-recursive-count
"\\<if\\>.*\\<\\(goto\\|igoto\\|kgoto\\)\\>"
expression-to-point 0)))
;; (count-elseif-statements (recursive-count
;; "\\b\\(elseif\\)\\b" (buffer-substring beginning-of-expr
;; (line-end-position 1)) 0))
(count-endif-statements (csound-util-recursive-count "\\s-?\\(endif\\)\\s-?" expression-to-point 0))
(count-while-statements (csound-util-recursive-count "\\s-?\\(while\\)\\s-?" expression-to-point 0))
(count-od-statements (csound-util-recursive-count "\\<\\(od\\)\\>" expression-to-point 0))
;;(count-switch-statements (csound-util-recursive-count "\\s-?\\(switch\\)\\s-?" expression-to-point 0))
;;(count-endw-statements (csound-util-recursive-count "\\s-?\\(endsw\\)\\s-?" expression-to-point 0))
(count-multiline-string-open (csound-util-recursive-count "{{" expression-to-line-above 0))
(count-multiline-string-close (csound-util-recursive-count "}}" expression-to-point 0))
(after-goto-statement
(if csound-indentation-indent-goto
(if (and (string-match-p "\\<\\w*:\\b" expression-to-point)
(= 0 (- count-multiline-string-open count-multiline-string-close)))
1 0)
0))
(line-at-goto-statement
(if (and csound-indentation-indent-goto
(save-excursion
(beginning-of-line)
(search-forward-regexp "\\<\\w*\\:\\s-*$" (line-end-position 1) t 1)))
1 0))
(begin-of-bool-p (csound-indentation-beginning-of-bool-p))
(previous-line-break-adjust
(if (csound-indentation--previous-line-breaks-p)
1 0))
(unbalanced-parens
(if (csound-indentation--pointer-inside-paren-p)
1 0))
(unbalanced-parens-or-line-break
(if (or (eq 1 unbalanced-parens) (eq 1 previous-line-break-adjust))
1 0))
(tab-count (max 1 (1+ (- (+ count-if-statements
after-goto-statement
count-multiline-string-open
;; count-elseif-statements
count-while-statements
;;count-switch-statements
;;previous-line-break-adjust
unbalanced-parens-or-line-break)
count-endif-statements
count-od-statements
;;count-endw-statements
begin-of-bool-p
(if (> after-goto-statement 0) line-at-goto-statement 0)
goto-if-mix
count-multiline-string-close
;;end-of-bool-p
)))))
;; (message "topoint: %s" expression-to-point)
;; (message "bool-begin: %d, ods: %d, line-at-goto: %d, aft-goto: %d,
;; count-if: %d, count-endif: %d mix: %d mls: %d.%d unbalanced-parens: %d
;; tab-count: %d"
;; begin-of-bool-p
;; count-od-statements
;; line-at-goto-statement
;; after-goto-statement
;; count-if-statements
;; count-endif-statements
;; goto-if-mix
;; count-multiline-string-open
;; count-multiline-string-close
;; unbalanced-parens
;; tab-count)
;; (message "RES %d" (* csound-indentation-spaces tab-count))
;; ;;(message "str-open: %d str-close: %d " count-string-open count-string-close)
;; (message "multistr-open: %d multistr-close: %d " count-multiline-string-open count-multiline-string-close)
;; (when (and (eq 't end-of-bool-p) (not (eq 't begin-of-bool-p))) (indent-line-to (* csound-indentation-spaces (1- tab-count))))
(indent-line-to (* csound-indentation-spaces tab-count))))
(defun csound-indentation--for-each-line (start end fn)
(while (< (point) end)
(funcall fn)
(forward-line)))
(defun csound-indentation--do-indent ()
(let ((score-p (or (save-excursion (search-backward "<CsScore" nil t 1))
(string-match-p ".sco$" (buffer-name (current-buffer))))))
(cond
(score-p (if csound-indentation-aggressive-score
(csound-score-align-block)
(indent-line-to 0)))
((csound-indentation-begin-of-expr-p) (indent-line-to 0))
((csound-indentation-end-of-expr-p) (indent-line-to 0))
;; ((csound-indentation-line-break-escape-p) (csound-indentation-line-break-indent))
((csound-indentation-inside-instr-p) (csound-indentation-inside-expression-calc 'instr))
((csound-indentation-inside-opcode-p) (csound-indentation-inside-expression-calc 'opcode))
(t (indent-line-to 0)))))
(defun csound-indentation-line ()
"Indent current line."
(if (or (csound-indentation--current-line-empty-p)
(csound-indentation--cursor-behind-indentation-point-p))
(csound-indentation--do-indent)
(save-excursion (csound-indentation--do-indent))))
(provide 'csound-indentation)
;;; csound-indentation.el ends here