1 ;;; lilypond-indent.el --- Auto-indentation for lilypond code
3 ;;; Chris Jackson <chris@fluffhouse.org.uk>
4 ;;; some code is taken from ESS (Emacs Speaks Statistics) S-mode by A.J.Rossini <rossini@biostat.washington.edu>
6 ;;; Variables for customising indentation style
8 (defcustom LilyPond-indent-level
4
9 "*Indentation of lilypond statements with respect to containing block.")
11 (defcustom LilyPond-brace-offset
0
12 "*Extra indentation for open braces.
13 Compares with other text in same context.")
15 (defcustom LilyPond-angle-offset
0
16 "*Extra indentation for open angled brackets .
17 Compares with other text in same context.")
19 (defcustom LilyPond-scheme-paren-offset
0
20 "*Extra indentation for open scheme parens .
21 Compares with other text in same context.")
23 (defcustom LilyPond-close-brace-offset
0
24 "*Extra indentation for closing braces.")
26 (defcustom LilyPond-close-angle-offset
0
27 "*Extra indentation for closing angle brackets.")
29 (defcustom LilyPond-close-scheme-paren-offset
0
30 "*Extra indentation for closing scheme parens.")
32 (defcustom LilyPond-fancy-comments t
33 "*Non-nil means distiguish between %, %%, and %%% for indentation.")
36 (defun LilyPond-calculate-indent ()
37 "Return appropriate indentation for current line as lilypond code.
38 In usual case returns an integer: the column to indent to.
39 Returns nil if line starts inside a string"
42 (let ((indent-point (point))
43 (case-fold-search nil
)
45 (setq containing-sexp
(save-excursion (LilyPond-beginning-of-containing-sexp)))
47 (while (< (point) indent-point
)
48 (setq state
(parse-partial-sexp (point) indent-point
0)))
49 ;; (setq containing-sexp (car (cdr state))) is the traditional way for languages
50 ;; with simpler parenthesis delimiters
52 ;; point is in the middle of a string
55 ;; point is in the middle of a block comment
56 (LilyPond-calculate-indent-within-blockcomment))
57 ((null containing-sexp
)
58 ;; Line is at top level - no indent
62 ;; Find previous non-comment character.
63 (goto-char indent-point
)
64 (LilyPond-backward-to-noncomment containing-sexp
)
65 ;; Now we get the answer.
66 ;; Position following last unclosed open.
67 (goto-char containing-sexp
)
69 ;; Is line first statement after an open brace or bracket?
70 ;; If no, find that first statement and indent like it.
73 ;; Skip over comments following open brace.
74 (skip-chars-forward " \t\n")
75 (cond ((looking-at "%{")
77 (and (not (looking-at "%}"))
78 (< (point) (point-max))))
80 (skip-chars-forward " \t\n"))
82 (skip-chars-forward " \t\n"))
84 (while (progn (skip-chars-forward " \t\n")
87 ;; The first following code counts
88 ;; if it is before the line we want to indent.
89 (and (< (point) indent-point
)
91 ;; If no previous statement,
92 ;; indent it relative to line brace is on.
93 ;; For open brace in column zero, don't let statement
94 ;; start there too. If LilyPond-indent-level is zero, use
95 ;; LilyPond-brace-offset instead
96 (+ (if (and (bolp) (zerop LilyPond-indent-level
))
97 (cond ((= (following-char) ?
{)
98 LilyPond-brace-offset
)
99 ((= (following-char) ?
<)
100 LilyPond-angle-offset
)
101 ((= (following-char) ?\
))
102 LilyPond-scheme-paren-offset
)
105 LilyPond-indent-level
)
107 (skip-chars-backward " \t")
108 (current-indentation)))))))))
112 (defun LilyPond-indent-line ()
113 "Indent current line as lilypond code.
114 Return the amount the indentation changed by."
115 (let ((indent (LilyPond-calculate-indent))
117 (case-fold-search nil
)
118 (pos (- (point-max) (point))))
121 (cond ((eq indent nil
)
122 (setq indent
(current-indentation)))
124 (skip-chars-forward " \t")
125 (if (and LilyPond-fancy-comments
(looking-at "%%%\\|%{\\|%}"))
127 (if (and LilyPond-fancy-comments
129 (not (looking-at "%%\\|%{\\|%}")))
130 (setq indent comment-column
)
131 (if (eq indent t
) (setq indent
0))
132 (if (listp indent
) (setq indent
(car indent
)))
134 ((= (following-char) ?
})
135 (setq indent
(+ indent
(- LilyPond-close-brace-offset LilyPond-indent-level
))))
136 ((= (following-char) ?
>)
137 (setq indent
(+ indent
(- LilyPond-close-angle-offset LilyPond-indent-level
))))
138 ((and (= (following-char) ?\
)) (LilyPond-inside-scheme-p))
139 (setq indent
(+ indent
(- LilyPond-close-scheme-paren-offset LilyPond-indent-level
))))
140 ((= (following-char) ?
{)
141 (setq indent
(+ indent LilyPond-brace-offset
)))
142 ((= (following-char) ?
<)
143 (setq indent
(+ indent LilyPond-angle-offset
)))
144 ((and (= (following-char) ?\
() (LilyPond-inside-scheme-p))
145 (setq indent
(+ indent LilyPond-scheme-paren-offset
)))
147 (skip-chars-forward " \t")
148 (setq shift-amt
(- indent
(current-column)))
149 (if (zerop shift-amt
)
150 (if (> (- (point-max) pos
) (point))
151 (goto-char (- (point-max) pos
)))
152 (delete-region beg
(point))
154 ;; If initial point was within line's indentation,
155 ;; position after the indentation.
156 ;; Else stay at same point in text.
157 (if (> (- (point-max) pos
) (point))
158 (goto-char (- (point-max) pos
))))
162 (defun LilyPond-inside-comment-p ()
163 "Return non-nil if point is inside a line or block comment"
164 (setq this-point
(point))
165 (or (save-excursion (beginning-of-line)
166 (skip-chars-forward " \t")
169 ;; point is in the middle of a block comment
170 (setq lastopen
(save-excursion (re-search-backward "%{[ \\t]*" (point-min) t
)))
171 (setq lastclose
(save-excursion (re-search-backward "%}[ \\t]*" (point-min) t
)))
172 (if (or (and (= (char-before) ?%
) (= (char-after) ?
{))
173 (and (= (char-after) ?%
) (= (char-after (1+ (point))) ?
{)))
174 (setq lastopen
(save-excursion (backward-char) (point))))
178 (<= lastclose lastopen
))))
182 (defun LilyPond-inside-string-or-comment-p ()
183 "Test if point is inside a string or a comment"
184 (setq this-point
(point))
185 (or (save-excursion (beginning-of-line)
186 (skip-chars-forward " \t")
190 (while (< (point) this-point
)
191 (setq state
(parse-partial-sexp (point) this-point
0)))
193 ;; point is in the middle of a string
196 ;; point is in the middle of a block comment
202 (defun LilyPond-backward-over-blockcomments (lim)
203 "Move point back to closest non-whitespace character not part of a block comment"
204 (setq lastopen
(save-excursion (re-search-backward "%{[ \\t]*" lim t
)))
205 (setq lastclose
(save-excursion (re-search-backward "%}[ \\t]*" lim t
)))
208 (if (<= lastclose lastopen
)
209 (goto-char lastopen
))
210 (goto-char lastopen
)))
211 (skip-chars-backward " %\t\n\f"))
214 (defun LilyPond-backward-over-linecomments (lim)
215 "Move point back to the closest non-whitespace character not part of a line comment.
219 (skip-chars-backward " \t\n\f" lim
)
220 (setq opoint
(point))
222 (search-forward "%" opoint
'move
)
223 (skip-chars-backward " \t%")
224 (setq stop
(or (/= (preceding-char) ?
\n) (<= (point) lim
)))
226 (beginning-of-line)))))
229 (defun LilyPond-backward-to-noncomment (lim)
230 "Move point back to closest non-whitespace character not part of a comment"
231 (LilyPond-backward-over-linecomments lim
)
232 (LilyPond-backward-over-blockcomments lim
))
235 (defun LilyPond-calculate-indent-within-blockcomment ()
236 "Return the indentation amount for line inside a block comment."
237 (let (end percent-start
)
240 (skip-chars-forward " \t")
241 (skip-chars-backward " \t\n")
244 (skip-chars-forward " \t")
245 (and (re-search-forward "%{[ \t]*" end t
)
246 (goto-char (1+ (match-beginning 0))))
247 (if (and (looking-at "[ \t]*$") (= (preceding-char) ?\%
))
248 (1+ (current-column))
252 ;; Key: Type of bracket (character).
253 ;; Value: Pair of regexps representing the corresponding open and close bracket"
254 ;; () are treated specially (need to indent in Scheme but not in music), and []
255 ;; are handled by the syntax table
257 (defconst LilyPond-parens-regexp-alist
258 `( ( ?
> .
("\\([^\\]\\|^\\)<" .
"[^ \\n\\t_^-]\\s-*>\\|[_^-]\\s-*[-^]\\s-*>"))
259 ;; a b c->, a b c^> and a b c_> are not close-angle-brackets, they're accents
260 ;; but a b c^-> and a b c^^> are close brackets with tenuto/marcato before them
261 ;; also \> and \< are hairpins
266 (defconst LilyPond-parens-alist
273 (defun LilyPond-matching-paren (bracket-type)
274 "Returns the open corresponding to the close specified by bracket-type, or vice versa"
275 (cond ( (memq bracket-type
(mapcar 'car LilyPond-parens-alist
))
276 (cdr (assoc bracket-type LilyPond-parens-alist
)) )
277 ( (memq bracket-type
(mapcar 'cdr LilyPond-parens-alist
))
278 (car (rassoc bracket-type LilyPond-parens-alist
)) )
282 (defun LilyPond-beginning-of-containing-sexp (&optional bracket-type slur-paren-p
)
283 "Move point to the beginning of the deepest parenthesis pair enclosing point.
285 If the optional argument bracket-type, a character representing a
286 close bracket such as ) or }, is specified, then the parenthesis pairs
287 searched are limited to this type.
289 If the optional argument slur-paren-p is non-nil, then slur
290 parentheses () are considered as matching pairs. Otherwise Scheme
291 parentheses are considered to be matching pairs, but slurs are not.
292 slur-paren-p defaults to nil.
296 (regexp-alist LilyPond-parens-regexp-alist
)
298 (if (LilyPond-inside-scheme-p)
299 (setq paren-regexp
"(\\|)")
301 (setq regexp-alist
(cons '( ?\
) .
("(" .
")")) regexp-alist
)))
302 (if (memq bracket-type
(mapcar 'car regexp-alist
))
303 (progn (setq paren-regexp
(cdr (assoc bracket-type regexp-alist
)))
304 (setq paren-regexp
(concat (car paren-regexp
) "\\|" (cdr paren-regexp
))))
305 (setq paren-regexp
(concat (mapconcat 'car
(mapcar 'cdr regexp-alist
) "\\|") "\\|"
306 (mapconcat 'cdr
(mapcar 'cdr regexp-alist
) "\\|")))))
307 (while (and (> level
0)
308 (re-search-backward paren-regexp nil t
)
309 (setq match
(char-before (match-end 0))))
310 (if (not (save-excursion (goto-char (match-end 0))
311 (LilyPond-inside-string-or-comment-p)))
312 (if (memq match
'(?
} ?
> ?\
)))
313 (progn (setq level
(1+ level
))
314 (if (and (= match ?
>)
315 (looking-at ".\\s-+>\\|\\({\\|}\\|<\\|>\\|(\\|)\\)>"))
317 (progn (setq level
(1- level
))
318 (if (and (= match ?
<)
319 (looking-at ".\\s-+<\\|\\({\\|}\\|<\\|>\\|(\\|)\\)<"))
320 (forward-char 1))))))
321 (if (looking-at ".<\\|.>") (forward-char 1))
324 (progn (goto-char oldpos
)
328 (defun LilyPond-inside-scheme-p ()
329 "Tests if point is inside embedded Scheme code"
331 (let ( (test-point (point))
334 (if (or (and (/= (point) (point-max))
335 (= (char-after (point)) ?\
()
336 (or (= (char-after (- (point) 1)) ?
#)
337 (and (= (char-after (- (point) 2)) ?
#)
338 (= (char-after (- (point) 1)) ?
`))))
339 (and (re-search-backward "#(\\|#`(" nil t
)
343 (while (and (> level
0)
344 (re-search-forward "(\\|)" test-point t
)
345 (setq match
(char-after (match-beginning 0)))
346 (<= (point) test-point
))
348 (setq level
(1+ level
))
349 (setq level
(1- level
))))
355 ;;; Largely taken from the 'blink-matching-open' in lisp/simple.el in
356 ;;; the Emacs distribution.
358 (defun LilyPond-blink-matching-open (bracket-type)
359 "Move cursor momentarily to the beginning of the sexp before
360 point. In lilypond files this is used for closing ), } and >, whereas the
361 builtin 'blink-matching-open' is used for closing ], which is in
364 (let ( (oldpos (point))
368 (if blink-matching-paren-distance
369 (narrow-to-region (max (point-min)
370 (- (point) blink-matching-paren-distance
))
372 (if (memq bracket-type
'(?
> ?
}))
373 ;; < { need to be mutually balanced and nested, so search backwards for both of these bracket types
374 (LilyPond-beginning-of-containing-sexp nil nil
)
375 ;; whereas ( ) slurs within music don't, so only need to search for ( )
376 (LilyPond-beginning-of-containing-sexp bracket-type t
))
377 (setq blinkpos
(point))
379 (or (null (LilyPond-matching-paren (char-after blinkpos
)))
380 (/= (char-after oldpos
)
381 (LilyPond-matching-paren (char-after blinkpos
)))))
382 (if mismatch
(progn (setq blinkpos nil
)
383 (message "Mismatched parentheses")))
385 (if (pos-visible-in-window-p)
386 (and blink-matching-paren-on-screen
387 (sit-for blink-matching-delay
))
390 ;; Show what precedes the open in its line, if anything.
392 (skip-chars-backward " \t")
394 (buffer-substring (progn (beginning-of-line) (point))
396 ;; Show what follows the open in its line, if anything.
399 (skip-chars-forward " \t")
401 (buffer-substring blinkpos
402 (progn (end-of-line) (point)))
403 ;; Otherwise show the previous nonblank line,
406 (skip-chars-backward "\n \t")
409 (buffer-substring (progn
410 (skip-chars-backward "\n \t")
414 (skip-chars-backward " \t")
416 ;; Replace the newline and other whitespace with `...'.
418 (buffer-substring blinkpos
(1+ blinkpos
)))
419 ;; There is nothing to show except the char itself.
420 (buffer-substring blinkpos
(1+ blinkpos
))))))))
424 (defun LilyPond-electric-close-paren ()
425 "Blink on the matching open paren when a > or ) is inserted"
427 (let ((oldpos (point)))
428 (self-insert-command 1)
429 (setq close-char
(char-before (point)))
430 (if (and blink-matching-paren
431 (not (LilyPond-inside-string-or-comment-p))
432 (save-excursion (re-search-backward
433 (concat (mapconcat 'cdr
(mapcar 'cdr LilyPond-parens-regexp-alist
) "\\|") "\\|)") nil t
)
434 (eq oldpos
(1- (match-end 0)))))
435 (progn (backward-char 1)
436 (LilyPond-blink-matching-open close-char
)
441 ;;; emulate show-paren-mode