new file.
[lilypond.git] / lilypond-mode.el
blob3e5053befd75c142d1f226ccb09cfd8b92cbc980
1 ;;;
2 ;;; lilypond-mode.el --- Major mode for editing GNU LilyPond music scores
3 ;;;
4 ;;; source file of the GNU LilyPond music typesetter
5 ;;;
6 ;;; (c) 1999--2001 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;;
8 ;;; Changed 2001--2003 Heikki Junes <heikki.junes@hut.fi>
9 ;;; * Add PS-compilation, PS-viewing and MIDI-play (29th Aug 2001)
10 ;;; * Keyboard shortcuts (12th Sep 2001)
11 ;;; * Inserting tags, inspired on sgml-mode (11th Oct 2001)
12 ;;; * Autocompletion & Info (23rd Nov 2002)
13 ;;;
14 ;;; Changed 2002 Carlos Betancourt <carlos.betancourt@chello.be>
15 ;;; * Added spanish-note-replacements
17 ;;; Inspired on auctex
19 ;;; Look lilypond-init.el or Documentation/topdocs/INSTALL.texi
20 ;;; for installing instructions.
22 ;;; TODO:
23 ;;; * XEmacs, broken ?
24 ;;; * parenthesis matching
26 (require 'easymenu)
27 (require 'compile)
29 (defconst LilyPond-version "1.7.12"
30 "`LilyPond-mode' version number.")
32 (defconst LilyPond-help-address "bug-lilypond@gnu.org"
33 "Address accepting submission of bug reports.")
35 (defvar LilyPond-mode-hook nil
36 "*Hook called by `LilyPond-mode'.")
38 ;; FIXME: find ``\score'' in buffers / make settable?
39 (defun LilyPond-master-file ()
40 ;; duh
41 (buffer-file-name))
43 (defvar LilyPond-kick-xdvi nil
44 "If true, no simultaneous xdvi's are started, but reload signal is sent.")
46 (defvar LilyPond-command-history nil
47 "Command history list.")
49 (defvar LilyPond-regexp-alist
50 '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
51 "Regexp used to match LilyPond errors. See `compilation-error-regexp-alist'.")
53 (defcustom LilyPond-include-path ".:/tmp"
54 "* LilyPond include path."
55 :type 'string
56 :group 'LilyPond)
58 (defun LilyPond-words-filename ()
59 "The file containing LilyPond \keywords \Identifiers and ReservedWords.
60 Finds file lilypond-words from load-path."
61 (let ((fn nil)
62 (lp load-path)
63 (words-file "lilypond.words"))
64 (while (and (> (length lp) 0) (not fn))
65 (setq fn (concat (car lp) "/" words-file))
66 (if (not (file-readable-p fn))
67 (progn (setq fn nil) (setq lp (cdr lp)))))
68 fn))
70 (defun LilyPond-add-dictionary-word (x)
71 "Contains all words: keywords, identifiers, reserved words."
72 (nconc '(("" . 1)) x))
74 ;; creates dictionary if empty
75 (if (eq (length (LilyPond-add-dictionary-word ())) 1)
76 (progn
77 (setq fn (LilyPond-words-filename))
78 (setq b (find-file-noselect fn t t))
79 (setq m (set-marker (make-marker) 1 (get-buffer b)))
80 (setq i 1)
81 (while (> (buffer-size b) (marker-position m))
82 (setq i (+ i 1))
83 (setq copy (copy-alist (list (eval (symbol-name (read m))))))
84 (setcdr copy i)
85 (LilyPond-add-dictionary-word (list copy)))
86 (kill-buffer b)))
88 (defconst LilyPond-keywords
89 (let ((wordlist ())
90 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
91 (progn
92 (while (> (length co) 0)
93 (if (> (length (car co)) 1)
94 (if (and (string-equal "\\" (substring (car co) 0 1))
95 (string-equal (downcase (car co)) (car co)))
96 (add-to-list 'wordlist (car co))))
97 (setq co (cdr co)))
98 wordlist))
99 "LilyPond \\keywords")
101 (defconst LilyPond-identifiers
102 (let ((wordlist ())
103 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
104 (progn
105 (while (> (length co) 0)
106 (if (> (length (car co)) 1)
107 (if (and (string-equal "\\" (substring (car co) 0 1))
108 (not (string-equal (downcase (car co)) (car co))))
109 (add-to-list 'wordlist (car co))))
110 (setq co (cdr co)))
111 wordlist))
112 "LilyPond \\Identifiers")
114 (defconst LilyPond-reserved-words
115 (let ((wordlist ())
116 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
117 (progn
118 (while (> (length co) 0)
119 (if (> (length (car co)) 0)
120 (if (not (string-equal "\\" (substring (car co) 0 1)))
121 (add-to-list 'wordlist (car co))))
122 (setq co (cdr co)))
123 wordlist))
124 "LilyPond ReservedWords")
126 (defun LilyPond-check-files (derived originals extensions)
127 "Check that DERIVED is newer than any of the ORIGINALS.
128 Try each original with each member of EXTENSIONS, in all directories
129 in LilyPond-include-path."
130 (let ((found nil)
131 (regexp (concat "\\`\\("
132 (mapconcat (function (lambda (dir)
133 (regexp-quote (expand-file-name dir))))
134 LilyPond-include-path "\\|")
135 "\\).*\\("
136 (mapconcat 'regexp-quote originals "\\|")
137 "\\)\\.\\("
138 (mapconcat 'regexp-quote extensions "\\|")
139 "\\)\\'"))
140 (buffers (buffer-list)))
141 (while buffers
142 (let* ((buffer (car buffers))
143 (name (buffer-file-name buffer)))
144 (setq buffers (cdr buffers))
145 (if (and name (string-match regexp name))
146 (progn
147 (and (buffer-modified-p buffer)
148 (or (not LilyPond-save-query)
149 (y-or-n-p (concat "Save file "
150 (buffer-file-name buffer)
151 "? ")))
152 (save-excursion (set-buffer buffer) (save-buffer)))
153 (if (file-newer-than-file-p name derived)
154 (setq found t))))))
155 found))
157 (defun LilyPond-running ()
158 "Check the currently running LilyPond compiling jobs."
159 (interactive)
160 (let ((process-names (list "lilypond" "tex" "2dvi" "2ps" "2midi"
161 "book" "latex"))
162 (running nil))
163 (while (setq process-name (pop process-names))
164 (setq process (get-process process-name))
165 (if (and process
166 (eq (process-status process) 'run))
167 (push process-name running)))
168 running)) ; return the running jobs
170 (defun LilyPond-midi-running ()
171 "Check the currently running Midi processes."
172 (interactive)
173 (let ((process-names (list "midi" "midiall"))
174 (running nil))
175 (while (setq process-name (pop process-names))
176 (setq process (get-process process-name))
177 (if (and process
178 (eq (process-status process) 'run))
179 (push process-name running)))
180 running)) ; return the running jobs
182 (defun LilyPond-kill-jobs ()
183 "Kill the currently running LilyPond compiling jobs."
184 (interactive)
185 (let ((process-names (LilyPond-running))
186 (killed nil))
187 (while (setq process-name (pop process-names))
188 (quit-process (get-process process-name) t)
189 (push process-name killed))
190 killed)) ; return the killed jobs
192 (defun LilyPond-kill-midi ()
193 "Kill the currently running midi processes."
194 (let ((process-names (LilyPond-midi-running))
195 (killed nil))
196 (while (setq process-name (pop process-names))
197 (quit-process (get-process process-name) t)
198 (push process-name killed))
199 killed)) ; return the killed jobs
201 ;; URG, should only run LilyPond-compile for LilyPond
202 ;; not for tex,xdvi (ly2dvi?)
203 (defun LilyPond-compile-file (command name)
204 ;; We maybe should know what we run here (Lily, ly2dvi, tex)
205 ;; and adjust our error-matching regex ?
206 (compile-internal command "No more errors" name ))
208 ;; do we still need this, now that we're using compile-internal?
209 (defun LilyPond-save-buffer ()
210 "Save buffer and set default command for compiling."
211 (interactive)
212 (if (buffer-modified-p)
213 (progn (save-buffer)
214 (setq LilyPond-command-default "LilyPond"))))
216 ;;; return (dir base ext)
217 (defun split-file-name (name)
218 (let* ((i (string-match "[^/]*$" name))
219 (dir (if (> i 0) (substring name 0 i) "./"))
220 (file (substring name i (length name)))
221 (i (string-match "[^.]*$" file)))
222 (if (and
223 (> i 0)
224 (< i (length file)))
225 (list dir (substring file 0 (- i 1)) (substring file i (length file)))
226 (list dir file ""))))
229 ;; Should check whether in command-alist?
230 (defcustom LilyPond-command-default "LilyPond"
231 "Default command. Must identify a member of LilyPond-command-alist."
233 :group 'LilyPond
234 :type 'string)
235 ;;;(make-variable-buffer-local 'LilyPond-command-last)
237 (defvar LilyPond-command-current 'LilyPond-command-master)
238 ;;;(make-variable-buffer-local 'LilyPond-command-master)
241 ;; If non-nil, LilyPond-command-query will return the value of this
242 ;; variable instead of quering the user.
243 (defvar LilyPond-command-force nil)
245 (defcustom LilyPond-xdvi-command "xdvi"
246 "Command used to display DVI files."
248 :group 'LilyPond
249 :type 'string)
251 (defcustom LilyPond-gv-command "gv -watch"
252 "Command used to display PS files."
254 :group 'LilyPond
255 :type 'string)
257 (defcustom LilyPond-midi-command "timidity"
258 "Command used to play MIDI files."
260 :group 'LilyPond
261 :type 'string)
263 (defcustom LilyPond-all-midi-command "timidity -ia"
264 "Command used to play MIDI files."
266 :group 'LilyPond
267 :type 'string)
269 (defun LilyPond-command-current-midi ()
270 "Play midi corresponding to the current document."
271 (interactive)
272 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file))
274 (defun LilyPond-command-all-midi ()
275 "Play midi corresponding to the current document."
276 (interactive)
277 (LilyPond-command (LilyPond-command-menu "MidiAll") 'LilyPond-master-file))
279 (defun count-rexp (start end rexp)
280 "Print number of found regular expressions in the region."
281 (interactive "r")
282 (save-excursion
283 (save-restriction
284 (narrow-to-region start end)
285 (goto-char (point-min))
286 (count-matches rexp))))
288 (defun count-midi-words ()
289 "Check number of midi-scores before the curser."
290 (interactive)
291 (count-rexp (point-min) (point-max) "\\\\midi"))
293 (defun count-midi-words-backwards ()
294 "Check number of midi-scores before the curser."
295 (interactive)
296 (count-rexp (point-min) (point) "\\\\midi"))
298 (defun LilyPond-string-current-midi ()
299 "Check the midi file of the following midi-score in the current document."
300 (interactive)
301 (let ((fnameprefix (substring (LilyPond-master-file) 0 -3)) ; suppose ".ly"
302 (allcount (string-to-number (substring (count-midi-words) 0 -12)))
303 (count (string-to-number (substring (count-midi-words-backwards) 0 -12))))
304 (concat fnameprefix
305 (if (and (> allcount 1) (> count 0)) ; not first score
306 (if (eq count allcount) ; last score
307 (concat "-" (number-to-string (+ count -1)))
308 (concat "-" (number-to-string count))))
309 ".midi")))
311 (defun LilyPond-string-all-midi ()
312 "Return the midi files of the current document in ascending order."
313 (interactive)
314 (let ((fnameprefix (substring (LilyPond-master-file) 0 -3))
315 (allcount (string-to-number (substring (count-midi-words) 0 -12))))
316 (concat (if (> allcount 0) ; at least one midi-score
317 (concat fnameprefix ".midi "))
318 (if (> allcount 1) ; more than one midi-score
319 (concat fnameprefix "-[1-9].midi "))
320 (if (> allcount 9) ; etc.
321 (concat fnameprefix "-[1-9][0-9].midi"))
322 (if (> allcount 99) ; not first score
323 (concat fnameprefix "-[1-9][0-9][0-9].midi")))))
325 ;; This is the major configuration variable.
326 (defcustom LilyPond-command-alist
327 ;; Should expand this to include possible keyboard shortcuts which
328 ;; could then be mapped to define-key and menu.
330 ("LilyPond" . ("lilypond %s" . "LaTeX"))
331 ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
333 ("2Dvi" . ("ly2dvi %s" . "View"))
334 ("2PS" . ("ly2dvi -P %s" . "ViewPS"))
335 ("2Midi" . ("lilypond -m %s" . "View"))
337 ("Book" . ("lilypond-book %x" . "LaTeX"))
338 ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
340 ;; point-n-click (arg: exits upop USR1)
341 ("SmartView" . ("xdvi %d" . "LilyPond"))
343 ;; refreshes when kicked USR1
344 ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
345 ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
347 ;; The following are refreshed in LilyPond-command:
348 ;; - current-midi depends on cursor position and
349 ("Midi" . (,(concat LilyPond-midi-command " " (LilyPond-string-current-midi)) . "LilyPond" )) ;
350 ;; - all-midi depends on number of midi-score.
351 ("MidiAll" . (,(concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)) . "LilyPond"))
354 "AList of commands to execute on the current document.
356 The key is the name of the command as it will be presented to the
357 user, the value is a cons of the command string handed to the shell
358 after being expanded, and the next command to be executed upon
359 success. The expansion is done using the information found in
360 LilyPond-expand-list.
362 :group 'LilyPond
363 :type '(repeat (cons :tag "Command Item"
364 (string :tag "Key")
365 (cons :tag "How"
366 (string :tag "Command")
367 (string :tag "Next Key")))))
369 ;; drop this?
370 (defcustom LilyPond-file-extensions '(".ly" ".sly" ".fly")
371 "*File extensions used by manually generated TeX files."
372 :group 'LilyPond
373 :type '(repeat (string :format "%v")))
376 (defcustom LilyPond-expand-alist
378 ("%s" . ".ly")
379 ("%t" . ".tex")
380 ("%d" . ".dvi")
381 ("%p" . ".ps")
382 ("%l" . ".tex")
383 ("%x" . ".tely")
384 ("%m" . ".midi")
387 "Alist of expansion strings for LilyPond command names."
388 :group 'LilyPond
389 :type '(repeat (cons :tag "Alist item"
390 (string :tag "Symbol")
391 (string :tag "Expansion"))))
394 (defcustom LilyPond-command-Show "View"
395 "*The default command to show (view or print) a LilyPond file.
396 Must be the car of an entry in `LilyPond-command-alist'."
397 :group 'LilyPond
398 :type 'string)
399 (make-variable-buffer-local 'LilyPond-command-Show)
401 (defcustom LilyPond-command-Print "Print"
402 "The name of the Print entry in LilyPond-command-Print."
403 :group 'LilyPond
404 :type 'string)
406 (defun xLilyPond-compile-sentinel (process msg)
407 (if (and process
408 (= 0 (process-exit-status process)))
409 (setq LilyPond-command-default
410 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
412 ;; FIXME: shouldn't do this for stray View/xdvi
413 (defun LilyPond-compile-sentinel (buffer msg)
414 (if (string-match "^finished" msg)
415 (setq LilyPond-command-default
416 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
418 ;;(make-variable-buffer-local 'compilation-finish-function)
419 (setq compilation-finish-function 'LilyPond-compile-sentinel)
421 (defun LilyPond-command-query (name)
422 "Query the user for what LilyPond command to use."
423 (let* ((default (cond ((if (string-equal name "emacs-lily")
424 (LilyPond-check-files (concat name ".tex")
425 (list name)
426 LilyPond-file-extensions)
427 (if (verify-visited-file-modtime (current-buffer))
428 (if (buffer-modified-p)
429 (if (y-or-n-p "Save buffer before next command? ")
430 (LilyPond-save-buffer)))
431 (if (y-or-n-p "The command will be invoked to an already saved buffer. Revert it? ")
432 (revert-buffer t t)))
433 ;;"LilyPond"
434 LilyPond-command-default))
435 (t LilyPond-command-default)))
437 (completion-ignore-case t)
439 (answer (or LilyPond-command-force
440 (completing-read
441 (concat "Command: (default " default ") ")
442 LilyPond-command-alist nil t nil 'LilyPond-command-history))))
444 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
445 (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
446 (if (and answer
447 (not (string-equal answer "")))
448 answer
449 default))))
451 (defun LilyPond-command-master ()
452 "Run command on the current document."
453 (interactive)
454 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
455 'LilyPond-master-file))
457 (defun LilyPond-command-lilypond ()
458 "Run lilypond for the current document."
459 (interactive)
460 (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file)
463 (defun LilyPond-command-formatdvi ()
464 "Format the dvi output of the current document."
465 (interactive)
466 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
469 (defun LilyPond-command-formatps ()
470 "Format the ps output of the current document."
471 (interactive)
472 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
475 (defun LilyPond-command-formatmidi ()
476 "Format the midi output of the current document."
477 (interactive)
478 (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-master-file)
481 (defun LilyPond-command-smartview ()
482 "View the dvi output of current document."
483 (interactive)
484 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
487 (defun LilyPond-command-view ()
488 "View the dvi output of current document."
489 (interactive)
490 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
493 (defun LilyPond-command-viewps ()
494 "View the ps output of current document."
495 (interactive)
496 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
499 ;; FIXME, does not find commend ender
500 (defun LilyPond-un-comment-region (start end level)
501 "Remove up to LEVEL comment characters from each line in the region."
502 (interactive "*r\np")
503 (comment-region start end (- level)))
505 ;; FIXME, this is broken
506 (defun LilyPond-region-file (begin end)
507 (let (
508 ;; (dir "/tmp/")
509 ;; urg
510 (dir "./")
511 (base "emacs-lily")
512 ;; Hmm
513 (ext (if (string-match "^[\\]score" (buffer-substring begin end))
514 ".ly"
515 (if (< 50 (abs (- begin end)))
516 ".fly"
517 ".sly"))))
518 (concat dir base ext)))
520 (defun LilyPond-command-region (begin end)
521 "Run LilyPond on the current region."
522 (interactive "r")
523 (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
524 (LilyPond-command (LilyPond-command-query
525 (LilyPond-region-file begin end))
526 '(lambda () (LilyPond-region-file begin end))))
528 (defun LilyPond-command-buffer ()
529 "Run LilyPond on buffer."
530 (interactive)
531 (LilyPond-command-region (point-min) (point-max)))
533 (defun LilyPond-command-expand (string file)
534 (let ((case-fold-search nil))
535 (if (string-match "%" string)
536 (let* ((b (match-beginning 0))
537 (e (+ b 2))
538 (l (split-file-name file))
539 (dir (car l))
540 (base (cadr l)))
541 (LilyPond-command-expand
542 (concat (substring string 0 b)
544 base
545 (let ((entry (assoc (substring string b e)
546 LilyPond-expand-alist)))
547 (if entry (cdr entry) ""))
548 (substring string e))
549 file))
550 string)))
552 (defun LilyPond-shell-process (name buffer command)
553 (let ((old (current-buffer)))
554 (switch-to-buffer-other-window buffer)
555 ;; If we empty the buffer don't see messages scroll by.
556 ;; (erase-buffer)
558 (start-process-shell-command name buffer command)
559 (switch-to-buffer-other-window old)))
562 (defun LilyPond-command (name file)
563 "Run command NAME on the file you get by calling FILE.
565 FILE is a function return a file name. It has one optional argument,
566 the extension to use on the file.
568 Use the information in LilyPond-command-alist to determine how to run the
569 command."
571 (let ((entry (assoc name LilyPond-command-alist)))
572 (if entry
573 (let ((command (LilyPond-command-expand (cadr entry)
574 (apply file nil)))
575 (jobs nil)
576 (job-string "no jobs"))
577 (if (member name (list "View" "ViewPS"))
578 ;; is USR1 a right signal for viewps?
579 (let ((buffer-xdvi (get-buffer-create (concat "*" name "*"))))
580 (if LilyPond-kick-xdvi
581 (let ((process-xdvi (get-buffer-process buffer-xdvi)))
582 (if process-xdvi
583 (signal-process (process-id process-xdvi) 'SIGUSR1)
584 (LilyPond-shell-process name buffer-xdvi command)))
585 (LilyPond-shell-process name buffer-xdvi command)))
586 (progn
587 (if (string-equal name "Midi")
588 (progn
589 (setq command (concat LilyPond-midi-command " " (LilyPond-string-current-midi)))
590 (if (LilyPond-kill-midi)
591 (setq job-string nil)))) ; either stop or start playing
592 (if (string-equal name "MidiAll")
593 (progn
594 (setq command (concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)))
595 (LilyPond-kill-midi))) ; stop and start playing
596 (if (and (member name (list "Midi" "MidiAll")) job-string)
597 (if (file-newer-than-file-p
598 (LilyPond-master-file)
599 (concat (substring (LilyPond-master-file) 0 -3) ".midi"))
600 (if (y-or-n-p "Midi older than source. Reformat midi?")
601 (progn
602 (LilyPond-command-formatmidi)
603 (while (LilyPond-running)
604 (message "Starts playing midi once it is built.")
605 (sit-for 0 100))))))
606 (if (member name (list "LilyPond" "TeX" "2Midi" "2PS" "2Dvi"
607 "Book" "LaTeX"))
608 (if (setq jobs (LilyPond-running))
609 (progn
610 (setq job-string "Process") ; could also suggest compiling after process has ended
611 (while jobs
612 (setq job-string (concat job-string " \"" (pop jobs) "\"")))
613 (setq job-string (concat job-string " is already running; kill it to proceed "))
614 (if (y-or-n-p job-string)
615 (progn
616 (setq job-string "no jobs")
617 (LilyPond-kill-jobs)
618 (while (LilyPond-running)
619 (sit-for 0 100)))
620 (setq job-string nil)))))
622 (setq LilyPond-command-default name)
623 (if (string-equal job-string "no jobs")
624 (LilyPond-compile-file command name))))))))
626 ;; XEmacs stuff
627 ;; Sadly we need this for a macro in Emacs 19.
628 (eval-when-compile
629 ;; Imenu isn't used in XEmacs, so just ignore load errors.
630 (condition-case ()
631 (require 'imenu)
632 (error nil)))
635 ;;; Keymap
637 (defvar LilyPond-mode-map ()
638 "Keymap used in `LilyPond-mode' buffers.")
640 ;; Note: if you make changes to the map, you must do
641 ;; M-x set-variable LilyPond-mode-map nil
642 ;; M-x eval-buffer
643 ;; M-x LilyPond-mode
644 ;; to let the changest take effect
646 (if LilyPond-mode-map
648 (setq LilyPond-mode-map (make-sparse-keymap))
649 ;; Put keys to Lilypond-command-alist and fetch them from there somehow.
650 (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
651 (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
652 (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
653 (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-jobs)
654 (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
655 (define-key LilyPond-mode-map "\C-cm" 'LilyPond-command-formatmidi)
656 (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
657 (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
658 (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
659 (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
660 (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
661 (define-key LilyPond-mode-map [(control c) return] 'LilyPond-command-current-midi)
662 (define-key LilyPond-mode-map [(control c) (control return)] 'LilyPond-command-all-midi)
663 (define-key LilyPond-mode-map "\C-x\C-s" 'LilyPond-save-buffer)
664 (define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
665 (define-key LilyPond-mode-map "\C-ci" 'LilyPond-quick-note-insert)
666 (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
667 (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
668 (define-key LilyPond-mode-map "\C-c:" 'LilyPond-un-comment-region)
669 (define-key LilyPond-mode-map "\C-c;" 'comment-region)
670 (define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren) ; urgh
671 (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren) ; argh
672 (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren) ; ..rgh
673 (define-key LilyPond-mode-map [S-iso-lefttab] 'LilyPond-autocompletion)
674 (define-key LilyPond-mode-map "\C-c\t" 'LilyPond-info-index-search)
677 ;;; Menu Support
679 (defun LilyPond-quick-note-insert()
680 "Insert notes with fewer key strokes by using a simple keyboard piano."
681 (interactive)
682 (setq dutch-notes
683 '(("k" "a") ("l" "b") ("a" "c") ("s" "d")
684 ("d" "e") ("f" "f") ("j" "g") ("r" "r")))
685 (setq dutch-note-ends '("eses" "es" "" "is" "isis"))
686 (setq dutch-note-replacements '("" ""))
687 (setq finnish-note-replacements
688 '(("eeses" "eses") ("ees" "es") ("aeses" "asas") ("aes" "as") ("b" "h")
689 ("beses" "heses") ("bes" "b") ("bis" "his") ("bisis" "hisis")))
690 ; add more translations of the note names
691 (setq spanish-note-replacements
692 '(("c" "do") ("d" "re") ("e" "mi") ("f" "fa") ("g" "sol") ("a" "la") ("b" "si")
693 ("cis" "dos") ("cisis" "doss") ("ces" "dob") ("ceses" "dobb")
694 ("dis" "res") ("disis" "ress") ("des" "reb") ("deses" "rebb")
695 ("eis" "mis") ("eisis" "miss") ("ees" "mib") ("eeses" "mibb")
696 ("fis" "fas") ("fisis" "fass") ("fes" "fab") ("feses" "fabb")
697 ("gis" "sols") ("gisis" "solss") ("ges" "solb") ("geses" "solbb")
698 ("ais" "las") ("aisis" "lass") ("aes" "lab") ("aeses" "labb")
699 ("bis" "sis") ("bisis" "siss") ("bes" "sib") ("beses" "sibb")))
700 (setq other-keys "()<>~}")
701 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)
703 (message "Press h for help.") (sit-for 0 750 1)
705 (setq note-replacements dutch-note-replacements)
706 (while (not (= 27 ; esc to quit
707 (setq x (read-char-exclusive
708 (format " | a[_]s[_]d | f[_]j[_]k[_]l | r with ie ,' 12345678 . 0 (<~>)/}\\b\\n Esc \n | c | d | e | f | g | a | %s | r with %s%s%s%s"
709 (if (string= (car(cdr(assoc "b" note-replacements))) "h")
710 "h" "b")
711 (nth (+ accid 2) dutch-note-ends)
712 (make-string (abs octav) (if (> octav 0) ?' ?,))
713 durat
714 (if (string= durat "") "" (make-string dots ?.)))))))
715 ;; (insert (number-to-string x)) ; test numbers for characters
716 (setq note (cdr (assoc (char-to-string x) dutch-notes)))
717 (cond
718 ((= x 127) (backward-kill-word 1)) ; backspace
719 ((= x 13) (progn (insert "\n") (LilyPond-indent-line)))) ; return
720 (setq x (char-to-string x))
721 (cond
722 ((and (string< x "9") (string< "0" x))
723 (progn (setq durat (int-to-string (expt 2 (- (string-to-int x) 1))))
724 (setq dots 0)))
725 ((string= x " ") (insert " "))
726 ((string= x "/") (progn (insert "\\times ")
727 (while (not (and (string< x "9") (string< "0" x)))
728 (setq x (char-to-string (read-char-exclusive "Insert a number for the denominator (\"x/\")"))))
729 (insert (format "%s/" x)) (setq x "/")
730 (while (not (and (string< x "9") (string< "0" x)))
731 (setq x (char-to-string (read-char-exclusive "Insert a number for the numerator (\"/y\")"))))
732 (insert (format "%s { " x))))
733 ((string= x "0") (progn (setq accid 0) (setq octav 0)
734 (setq durat "") (setq dots 0)))
735 ((string= x "i") (setq accid (if (= accid 2) 0 (max (+ accid 1) 1))))
736 ((string= x "e") (setq accid (if (= accid -2) 0 (min (+ accid -1) -1))))
737 ((string= x "'") (setq octav (if (= octav 4) 0 (max (+ octav 1) 1))))
738 ((string= x ",") (setq octav (if (= octav -4) 0 (min (+ octav -1) -1))))
739 ((string= x ".") (setq dots (if (= dots 4) 0 (+ dots 1))))
740 ((not (null (member x (split-string other-keys ""))))
741 (insert (format "%s " x)))
742 ((not (null note))
743 (progn
744 (setq note
745 (format "%s%s" (car note) (if (string= "r" (car note)) ""
746 (nth (+ accid 2) dutch-note-ends))))
747 (setq notetwo (car (cdr (assoc note note-replacements))))
748 (if (not (null notetwo)) (setq note notetwo))
749 (insert
750 (format "%s%s%s%s "
751 note
752 (if (string= "r" note) ""
753 (make-string (abs octav) (if (> octav 0) ?' ?,)))
754 durat
755 (if (string= durat "") "" (make-string dots ?.))))
756 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)))
757 ((string= x "t") (progn (setq note-replacements dutch-note-replacements)
758 (message "Selected Dutch notes")
759 (sit-for 0 750 1))) ; t
760 ((string= x "n") (progn (setq note-replacements finnish-note-replacements)
761 (message "Selected Finnish/Deutsch notes")
762 (sit-for 0 750 1))) ; n
763 ; add more translations of the note names
764 ((string= x "p") (progn (setq note-replacements spanish-note-replacements)
765 (message "Selected Spanish notes")
766 (sit-for 0 750 1))) ; p
767 ((string= x "h")
768 (progn (message "Insert notes with fewer key strokes. For example \"i,5.f\" produces \"fis,32. \".") (sit-for 5 0 1)
769 (message "Add also \"a ~ a\"-ties, \"a ( ) b\"-slurs and \"< a b >\"-chords.") (sit-for 5 0 1)
770 (message "Note names are in Du(t)ch by default. Hit 'n' for Fin(n)ish/Deutsch note names. Hit 'p' for S(p)anish note names") (sit-for 5 0 1)
771 (message "Backspace deletes last note, return starts a new indented line and Esc quits.") (sit-for 5 0 1)
772 (message "Insert note triplets \"\\times 2/3 { a b } \" by typing \"/23ab}\".") (sit-for 5 0 1)
773 (message "Remember to add all other details as well.") (sit-for 5 0 1)))
776 (defun LilyPond-pre-word-search ()
777 "Fetch the alphabetic characters and \\ in front of the cursor."
778 (interactive)
779 (let ((pre "")
780 (prelen 0)
781 (ch (char-before (- (point) 0))))
782 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not bolp, A-Z
783 (and (>= ch 97) (<= ch 122)) ; a-z
784 (= ch 92))) ; \\
785 (setq pre (concat (char-to-string ch) pre))
786 (setq prelen (+ prelen 1))
787 (setq ch (char-before (- (point) prelen))))
788 pre))
790 (defun LilyPond-post-word-search ()
791 "Fetch the alphabetic characters behind the cursor."
792 (interactive)
793 (let ((post "")
794 (postlen 0)
795 (ch (char-after (+ (point) 0))))
796 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not eolp, A-Z
797 (and (>= ch 97) (<= ch 122)))) ; a-z
798 (setq post (concat post (char-to-string ch)))
799 (setq postlen (+ postlen 1))
800 (setq ch (char-after (+ (point) postlen))))
801 post))
803 (defun LilyPond-autocompletion ()
804 "Show completions in mini-buffer for the given word."
805 (interactive)
806 (let ((pre (LilyPond-pre-word-search))
807 (post (LilyPond-post-word-search))
808 (compsstr ""))
809 ;; insert try-completion and show all-completions
810 (if (> (length pre) 0)
811 (progn
812 (setq trycomp (try-completion pre (LilyPond-add-dictionary-word ())))
813 (if (char-or-string-p trycomp)
814 (if (string-equal (concat pre post) trycomp)
815 (goto-char (+ (point) (length post)))
816 (progn
817 (delete-region (point) (+ (point) (length post)))
818 (insert (substring trycomp (length pre) nil))))
819 (progn
820 (delete-region (point) (+ (point) (length post)))
821 (font-lock-fontify-buffer))) ; only inserting fontifies
823 (setq complist (all-completions pre (LilyPond-add-dictionary-word ())))
824 (while (> (length complist) 0)
825 (setq compsstr (concat compsstr "\"" (car complist) "\" "))
826 (setq complist (cdr complist)))
827 (message compsstr)
828 (sit-for 0 100 1)))))
830 (defun LilyPond-info ()
831 "Launch Info for lilypond."
832 (interactive)
833 (info "lilypond"))
835 (defun LilyPond-music-glossary-info ()
836 "Launch Info for music-glossary."
837 (interactive)
838 (info "music-glossary"))
840 (defun LilyPond-internals-info ()
841 "Launch Info for lilypond-internals."
842 (interactive)
843 (info "lilypond-internals"))
845 (defun LilyPond-info-index-search ()
846 "Inside Emacs, launch `info lilypond --index-search word-under-cursor'"
847 (interactive)
848 (let ((str (concat (LilyPond-pre-word-search) (LilyPond-post-word-search))))
849 (if (and (> (length str) 0)
850 (string-equal (substring str 0 1) "\\"))
851 (setq str (substring str 1 nil)))
852 (LilyPond-info)
853 (Info-index str)))
855 (defun LilyPond-insert-string (pre)
856 "Insert text to the buffer."
857 (interactive)
858 (insert pre)
859 (length pre))
861 (defun LilyPond-insert-between (text pre post)
862 "Insert text to the buffer if non-empty string is given."
863 (interactive)
864 (let ((str (read-string text)))
865 (if (string-equal str "")
867 (progn (setq pre_str_post (concat pre str post))
868 (insert pre_str_post)
869 (length pre_str_post)))))
871 (defun LilyPond-insert-tag-notes ()
872 "LilyPond notes tag."
873 (interactive)
874 (setq begin (if (and transient-mark-mode mark-active)
875 (mark-marker) (point-marker)))
876 (setq end (point-marker))
877 (goto-char begin)
878 (setq l1 (LilyPond-insert-string "\\notes "))
879 (setq l2 (LilyPond-insert-between "Relative (e.g. c'): " "\\relative " " "))
880 (if (eq l2 0)
881 (setq l2 (LilyPond-insert-between "Transpose (e.g. c c'): " "\\transpose " " ")))
882 (setq l3 (LilyPond-insert-string "{ "))
883 (goto-char (+ end l1 l2 l3))
884 (LilyPond-insert-string " }")
885 (goto-char (+ end l1 l2 l3)))
887 (defun LilyPond-insert-tag-score ()
888 "LilyPond score tag."
889 (interactive)
890 (setq begin (if (and transient-mark-mode mark-active)
891 (mark-marker) (point-marker)))
892 (setq end (point-marker))
893 (goto-char begin)
894 (setq l1 (LilyPond-insert-string "\\score {\n ")) ; keep track of lengths
895 (goto-char (+ end l1))
896 (LilyPond-insert-string "\n \\paper { }\n")
897 (setq l2 (if (y-or-n-p "Insert \"\\header\" field? ")
898 (+ (LilyPond-insert-string " \\header {")
899 (LilyPond-insert-between "Title: " "\n title = \"" "\"")
900 (LilyPond-insert-between "Subtitle: " "\n subtitle = \"" "\"")
901 (LilyPond-insert-between "Piece: " "\n piece = \"" "\"")
902 (LilyPond-insert-between "Opus: " "\n opus = \"" "\"")
903 (LilyPond-insert-string "\n }\n"))
905 (setq l3 (if (y-or-n-p "Insert \"\\midi\" field? ")
906 (+ (LilyPond-insert-string " \\midi {")
907 (LilyPond-insert-between "Tempo: " " \\tempo (e.g. 4 = 60)" "")
908 (LilyPond-insert-string " }\n"))
910 (setq l4 (LilyPond-insert-string "}\n"))
911 (goto-char (+ end l1)))
913 (defun LilyPond-command-menu-entry (entry)
914 ;; Return LilyPond-command-alist ENTRY as a menu item.
915 (let ((name (car entry)))
916 (cond ((and (string-equal name LilyPond-command-Print)
917 LilyPond-printer-list)
918 (let ((command LilyPond-print-command)
919 (lookup 1))
920 (append (list LilyPond-command-Print)
921 (mapcar 'LilyPond-command-menu-printer-entry
922 LilyPond-printer-list))))
924 (vector name (list 'LilyPond-command-menu name) t)))))
927 (easy-menu-define LilyPond-command-menu
928 LilyPond-mode-map
929 "Menu used in LilyPond mode."
930 (append '("Command")
931 '(("Command on"
932 [ "Master File" LilyPond-command-select-master
933 :keys "C-c C-c" :style radio
934 :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
935 [ "Buffer" LilyPond-command-select-buffer
936 :keys "C-c C-b" :style radio
937 :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
938 [ "Region" LilyPond-command-select-region
939 :keys "C-c C-r" :style radio
940 :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
941 ;;; (let ((file 'LilyPond-command-on-current))
942 ;;; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
943 ;;; Some kind of mapping which includes :keys might be more elegant
944 ;;; Put keys to Lilypond-command-alist and fetch them from there somehow.
945 '([ "LilyPond" LilyPond-command-lilypond t])
946 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
947 '([ "2Dvi" LilyPond-command-formatdvi t])
948 '([ "2PS" LilyPond-command-formatps t])
949 '([ "2Midi" LilyPond-command-formatmidi t])
950 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
951 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
952 '([ "Kill jobs" LilyPond-kill-jobs t])
953 '("-----")
954 '([ "SmartView" LilyPond-command-smartview t])
955 '([ "View" LilyPond-command-view t])
956 '([ "ViewPS" LilyPond-command-viewps t])
957 '("-----")
958 '([ "Midi (toggle)" LilyPond-command-current-midi t])
959 '([ "Midi all" LilyPond-command-all-midi t])
962 (easy-menu-define LilyPond-mode-menu
963 LilyPond-mode-map
964 "Menu used in LilyPond mode."
965 (append '("LilyPond")
966 '(("Insert"
967 [ "\\notes..." LilyPond-insert-tag-notes t]
968 [ "\\score..." LilyPond-insert-tag-score t]
969 ["Quick Notes" LilyPond-quick-note-insert t]
970 ["Autocompletion" LilyPond-autocompletion t]
972 '(("Miscellaneous"
973 ["Uncomment Region" LilyPond-un-comment-region t]
974 ["Comment Region" comment-region t]
975 ["Refontify buffer" font-lock-fontify-buffer t]
977 '(("Info"
978 ["LilyPond" LilyPond-info t]
979 ["LilyPond index-search" LilyPond-info-index-search t]
980 ["Music Glossary" LilyPond-music-glossary-info t]
981 ["LilyPond internals" LilyPond-internals-info t]
985 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z]+\\) *="
986 "Regexp matching Identifier definitions.")
988 (defvar LilyPond-imenu-generic-expression
989 (list (list nil LilyPond-imenu-generic-re 1))
990 "Expression for imenu")
992 (defun LilyPond-command-select-master ()
993 (interactive)
994 (message "Next command will be on the master file")
995 (setq LilyPond-command-current 'LilyPond-command-master))
997 (defun LilyPond-command-select-buffer ()
998 (interactive)
999 (message "Next command will be on the buffer")
1000 (setq LilyPond-command-current 'LilyPond-command-buffer))
1002 (defun LilyPond-command-select-region ()
1003 (interactive)
1004 (message "Next command will be on the region")
1005 (setq LilyPond-command-current 'LilyPond-command-region))
1007 (defun LilyPond-command-menu (name)
1008 ;; Execute LilyPond-command-alist NAME from a menu.
1009 (let ((LilyPond-command-force name))
1010 (funcall LilyPond-command-current)))
1012 (defun LilyPond-mode ()
1013 "Major mode for editing LilyPond music files.
1015 This mode knows about LilyPond keywords and line comments, not about
1016 indentation or block comments. It features easy compilation, error
1017 finding and viewing of a LilyPond source buffer or region.
1019 COMMANDS
1020 \\{LilyPond-mode-map}
1021 VARIABLES
1023 LilyPond-command-alist\t\talist from name to command
1024 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
1025 (interactive)
1026 ;; set up local variables
1027 (kill-all-local-variables)
1029 (make-local-variable 'font-lock-defaults)
1030 (setq font-lock-defaults '(LilyPond-font-lock-keywords))
1032 ;; string and comments are fontified explicitly
1033 (make-local-variable 'font-lock-keywords-only)
1034 (setq font-lock-keywords-only t)
1036 ;; Multi-line font-locking needs Emacs 21.1 or newer.
1037 ;; For older versions there is hotkey "C-c f".
1038 (make-local-variable 'font-lock-multiline)
1039 (setq font-lock-multiline t)
1041 (make-local-variable 'paragraph-separate)
1042 (setq paragraph-separate "^[ \t]*$")
1044 (make-local-variable 'paragraph-start)
1045 (setq paragraph-start "^[ \t]*$")
1047 (make-local-variable 'comment-start)
1048 (setq comment-start "%")
1050 (make-local-variable 'comment-start-skip)
1051 (setq comment-start-skip "%{? *")
1053 (make-local-variable 'comment-end)
1054 (setq comment-end "")
1056 (make-local-variable 'block-comment-start)
1057 (setq block-comment-start "%{")
1059 (make-local-variable 'block-comment-end)
1060 (setq block-comment-end "%}")
1062 (make-local-variable 'indent-line-function)
1063 (setq indent-line-function 'LilyPond-indent-line)
1065 (set-syntax-table LilyPond-mode-syntax-table)
1066 (setq major-mode 'LilyPond-mode)
1067 (setq mode-name "LilyPond")
1068 (setq local-abbrev-table LilyPond-mode-abbrev-table)
1069 (use-local-map LilyPond-mode-map)
1071 (make-local-variable 'imenu-generic-expression)
1072 (setq imenu-generic-expression LilyPond-imenu-generic-expression)
1073 (imenu-add-to-menubar "Index")
1075 ;; run the mode hook. LilyPond-mode-hook use is deprecated
1076 (run-hooks 'LilyPond-mode-hook))
1078 (defun LilyPond-version ()
1079 "Echo the current version of `LilyPond-mode' in the minibuffer."
1080 (interactive)
1081 (message "Using `LilyPond-mode' version %s" LilyPond-version))
1083 (load-library "lilypond-font-lock")
1084 (load-library "lilypond-indent")
1086 (provide 'lilypond-mode)
1087 ;;; lilypond-mode.el ends here