2 ;;; lilypond-mode.el --- Major mode for editing GNU LilyPond music scores
4 ;;; source file of the GNU LilyPond music typesetter
6 ;;; (c) 1999--2001 Jan Nieuwenhuizen <janneke@gnu.org>
8 ;;; Changed 2001 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)
13 ;;; Changed 2002 Carlos Betancourt <carlos.betancourt@chello.be>
14 ;;; * Added spanish-note-replacements
16 ;;; Inspired on auctex
19 ;;; Add this to your ~/.emacs or ~/.emacs.el
20 ;;; (load-library "lilypond-mode.el")
21 ;;; (setq auto-mode-alist
22 ;;; (cons '("\\.ly$" . LilyPond-mode) auto-mode-alist))
25 (load-library "lilypond-font-lock")
26 (load-library "lilypond-indent")
31 (defconst LilyPond-version "1.7.6"
32 "`LilyPond-mode' version number.")
34 (defconst LilyPond-help-address "bug-lilypond@gnu.org"
35 "Address accepting submission of bug reports.")
37 (defvar LilyPond-mode-hook nil
38 "*Hook called by `LilyPond-mode'.")
40 (defvar LilyPond-kick-xdvi nil
41 "If true, no simultaneous xdvi's are started, but reload signal is sent.")
43 (defvar LilyPond-command-history nil
44 "Command history list.")
46 (defvar LilyPond-regexp-alist
47 '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
48 "Regexp used to match LilyPond errors. See `compilation-error-regexp-alist'.")
50 (defcustom LilyPond-include-path ".:/tmp"
51 "* LilyPond include path."
56 (defun LilyPond-check-files (derived originals extensions)
57 "Check that DERIVED is newer than any of the ORIGINALS.
58 Try each original with each member of EXTENSIONS, in all directories
59 in LilyPond-include-path."
61 (regexp (concat "\\`\\("
62 (mapconcat (function (lambda (dir)
63 (regexp-quote (expand-file-name dir))))
64 LilyPond-include-path "\\|")
66 (mapconcat 'regexp-quote originals "\\|")
68 (mapconcat 'regexp-quote extensions "\\|")
70 (buffers (buffer-list)))
72 (let* ((buffer (car buffers))
73 (name (buffer-file-name buffer)))
74 (setq buffers (cdr buffers))
75 (if (and name (string-match regexp name))
77 (and (buffer-modified-p buffer)
78 (or (not LilyPond-save-query)
79 (y-or-n-p (concat "Save file "
80 (buffer-file-name buffer)
82 (save-excursion (set-buffer buffer) (save-buffer)))
83 (if (file-newer-than-file-p name derived)
87 (defun LilyPond-running ()
88 (let ((process (get-process "lilypond")))
90 (eq (process-status process) 'run))))
92 (defun Midi-running ()
93 (let ((process (get-process "midi")))
95 (eq (process-status process) 'run))))
97 (defun LilyPond-kill-job ()
98 "Kill the currently running LilyPond job."
100 ;; What bout TeX, Xdvi?
101 (quit-process (get-process "lilypond") t))
103 ;; URG, should only run LilyPond-compile for LilyPond
104 ;; not for tex,xdvi (ly2dvi?)
105 (defun LilyPond-compile-file (command name)
106 ;; We maybe should know what we run here (Lily, ly2dvi, tex)
107 ;; and adjust our error-matching regex ?
108 (compile-internal command "No more errors" name ))
110 ;; do we still need this, now that we're using compile-internal?
111 (defun LilyPond-save-buffer ()
112 "Save buffer and set default command for compiling."
114 (if (buffer-modified-p)
116 (setq LilyPond-command-default "LilyPond"))))
118 ;;; return (dir base ext)
119 (defun split-file-name (name)
120 (let* ((i (string-match "[^/]*$" name))
121 (dir (if (> i 0) (substring name 0 i) "./"))
122 (file (substring name i (length name)))
123 (i (string-match "[^.]*$" file)))
127 (list dir (substring file 0 (- i 1)) (substring file i (length file)))
128 (list dir file ""))))
131 ;; Should check whether in command-alist?
132 (defcustom LilyPond-command-default "LilyPond"
133 "Default command. Must identify a member of LilyPond-command-alist."
137 ;;;(make-variable-buffer-local 'LilyPond-command-last)
139 (defvar LilyPond-command-current 'LilyPond-command-master)
140 ;;;(make-variable-buffer-local 'LilyPond-command-master)
143 ;; If non-nil, LilyPond-command-query will return the value of this
144 ;; variable instead of quering the user.
145 (defvar LilyPond-command-force nil)
147 (defcustom LilyPond-xdvi-command "xdvi"
148 "Command used to display DVI files."
153 (defcustom LilyPond-gv-command "gv -watch"
154 "Command used to display PS files."
159 (defcustom LilyPond-midi-command "timidity"
160 "Command used to play MIDI files."
165 (defcustom LilyPond-midi-all-command "timidity -ig"
166 "Command used to play MIDI files."
171 ;; This is the major configuration variable.
172 (defcustom LilyPond-command-alist
174 ("LilyPond" . ("lilypond %s" . "LaTeX"))
175 ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
177 ("2Dvi" . ("ly2dvi %s" . "View"))
178 ("2PS" . ("ly2dvi -P %s" . "View"))
179 ("2Midi" . ("lilypond -m %s" . "View"))
181 ("Book" . ("lilypond-book %x" . "LaTeX"))
182 ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
184 ;; point-n-click (arg: exits upop USR1)
185 ("SmartView" . ("xdvi %d" . "LilyPond"))
187 ;; refreshes when kicked USR1
188 ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
190 ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
192 ("Midi" . (,(concat LilyPond-midi-command " %m") . "LilyPond"))
195 "AList of commands to execute on the current document.
197 The key is the name of the command as it will be presented to the
198 user, the value is a cons of the command string handed to the shell
199 after being expanded, and the next command to be executed upon
200 success. The expansion is done using the information found in
201 LilyPond-expand-list.
204 :type '(repeat (cons :tag "Command Item"
207 (string :tag "Command")
208 (string :tag "Next Key")))))
211 (defcustom LilyPond-file-extensions '(".ly" ".sly" ".fly")
212 "*File extensions used by manually generated TeX files."
214 :type '(repeat (string :format "%v")))
217 (defcustom LilyPond-expand-alist
228 "Alist of expansion strings for LilyPond command names."
230 :type '(repeat (cons :tag "Alist item"
231 (string :tag "Symbol")
232 (string :tag "Expansion"))))
235 (defcustom LilyPond-command-Show "View"
236 "*The default command to show (view or print) a LilyPond file.
237 Must be the car of an entry in `LilyPond-command-alist'."
240 (make-variable-buffer-local 'LilyPond-command-Show)
242 (defcustom LilyPond-command-Print "Print"
243 "The name of the Print entry in LilyPond-command-Print."
247 (defun xLilyPond-compile-sentinel (process msg)
249 (= 0 (process-exit-status process)))
250 (setq LilyPond-command-default
251 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
253 ;; FIXME: shouldn't do this for stray View/xdvi
254 (defun LilyPond-compile-sentinel (buffer msg)
255 (if (string-match "^finished" msg)
256 (setq LilyPond-command-default
257 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
259 ;;(make-variable-buffer-local 'compilation-finish-function)
260 (setq compilation-finish-function 'LilyPond-compile-sentinel)
262 (defun LilyPond-command-query (name)
263 "Query the user for what LilyPond command to use."
264 (let* ((default (cond ((if (string-equal name "emacs-lily")
265 (LilyPond-check-files (concat name ".tex")
267 LilyPond-file-extensions)
268 (if (buffer-modified-p)
269 (if (y-or-n-p "Save buffer before next command? ")
270 (LilyPond-save-buffer)))
272 LilyPond-command-default))
273 (t LilyPond-command-default)))
275 (completion-ignore-case t)
277 (answer (or LilyPond-command-force
279 (concat "Command: (default " default ") ")
280 LilyPond-command-alist nil t nil 'LilyPond-command-history))))
282 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
283 (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
285 (not (string-equal answer "")))
290 ;; FIXME: find ``\score'' in buffers / make settable?
291 (defun LilyPond-master-file ()
295 (defun LilyPond-command-master ()
296 "Run command on the current document."
298 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
299 'LilyPond-master-file))
301 (defun LilyPond-command-lilypond ()
302 "Run lilypond for the current document."
304 (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file)
307 (defun LilyPond-command-formatdvi ()
308 "Format the dvi output of the current document."
310 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
313 (defun LilyPond-command-formatps ()
314 "Format the ps output of the current document."
316 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
319 (defun LilyPond-command-smartview ()
320 "View the dvi output of current document."
322 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
325 (defun LilyPond-command-view ()
326 "View the dvi output of current document."
328 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
331 (defun LilyPond-command-viewps ()
332 "View the ps output of current document."
334 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
337 (defun LilyPond-command-midi ()
338 "Play midi corresponding to the current document."
340 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file)
343 (defun count-rexp (start end rexp)
344 "Print number of found regular expressions in the region."
348 (narrow-to-region start end)
349 (goto-char (point-min))
350 (count-matches rexp))))
352 (defun count-midi-words ()
353 "Print number of scores before the curser."
355 (count-rexp (point-min) (point-max) "\\\\midi"))
357 (defun count-midi-words-backwards ()
358 "Print number of scores before the curser."
360 (count-rexp (point-min) (point) "\\\\midi"))
362 (defun LilyPond-command-next-midi ()
363 "Play next midi score of the current document."
366 (quit-process (get-process "midi") t)
367 (LilyPond-compile-file
368 (let ((fname (LilyPond-master-file))
369 (allcount (string-to-number (substring (count-midi-words) 0 -12)))
370 (count (string-to-number (substring (count-midi-words-backwards) 0 -12))))
371 (concat LilyPond-midi-command " "
372 (substring fname 0 -3) ; suppose ".ly"
373 (if (and (> allcount 1) (> count 0)) ; not first score
374 (if (eq count allcount) ; last score
375 (concat "-" (number-to-string (+ count -1)))
376 (concat "-" (number-to-string count))))
380 (defun LilyPond-command-all-midi ()
381 "Play next midi score of the current document."
384 (quit-process (get-process "midi") t)
385 (LilyPond-compile-file
386 (let ((fname (LilyPond-master-file))
387 (allcount (string-to-number (substring (count-midi-words) 0 -12))))
388 (concat LilyPond-midi-all-command " "
389 (if (> allcount 0) ; at least one midi-score
390 (concat (substring fname 0 -3) ".midi "))
391 (if (> allcount 1) ; more than one midi-score
392 (concat (substring fname 0 -3) "-?.midi "))
393 (if (> allcount 9) ; etc.
394 (concat (substring fname 0 -3) "-??.midi"))
395 (if (> allcount 99) ; not first score
396 (concat (substring fname 0 -3) "-???.midi"))))
399 (defun LilyPond-un-comment-region (start end level)
400 "Remove up to LEVEL comment characters from each line in the region."
401 (interactive "*r\np")
402 (comment-region start end (- level)))
404 ;; FIXME, this is broken
405 (defun LilyPond-region-file (begin end)
412 (ext (if (string-match "^[\\]score" (buffer-substring begin end))
414 (if (< 50 (abs (- begin end)))
417 (concat dir base ext)))
419 (defun LilyPond-command-region (begin end)
420 "Run LilyPond on the current region."
422 (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
423 (LilyPond-command (LilyPond-command-query
424 (LilyPond-region-file begin end))
425 '(lambda () (LilyPond-region-file begin end))))
427 (defun LilyPond-command-buffer ()
428 "Run LilyPond on buffer."
430 (LilyPond-command-region (point-min) (point-max)))
432 (defun LilyPond-command-expand (string file)
433 (let ((case-fold-search nil))
434 (if (string-match "%" string)
435 (let* ((b (match-beginning 0))
437 (l (split-file-name file))
440 (LilyPond-command-expand
441 (concat (substring string 0 b)
444 (let ((entry (assoc (substring string b e)
445 LilyPond-expand-alist)))
446 (if entry (cdr entry) ""))
447 (substring string e))
451 (defun LilyPond-shell-process (name buffer command)
452 (let ((old (current-buffer)))
453 (switch-to-buffer-other-window buffer)
454 ;; If we empty the buffer don't see messages scroll by.
457 (start-process-shell-command name buffer command)
458 (switch-to-buffer-other-window old)))
461 (defun LilyPond-command (name file)
462 "Run command NAME on the file you get by calling FILE.
464 FILE is a function return a file name. It has one optional argument,
465 the extension to use on the file.
467 Use the information in LilyPond-command-alist to determine how to run the
470 (let ((entry (assoc name LilyPond-command-alist)))
472 (let ((command (LilyPond-command-expand (cadr entry)
474 (if (string-equal name "View")
475 (let ((buffer-xdvi (get-buffer-create "*view*")))
476 (if LilyPond-kick-xdvi
477 (let ((process-xdvi (get-buffer-process buffer-xdvi)))
479 (signal-process (process-id process-xdvi) 'SIGUSR1)
480 (LilyPond-shell-process name buffer-xdvi command)))
481 (LilyPond-shell-process name buffer-xdvi command)))
483 (setq LilyPond-command-default name)
484 (LilyPond-compile-file command name)))))))
487 ;; Sadly we need this for a macro in Emacs 19.
489 ;; Imenu isn't used in XEmacs, so just ignore load errors.
497 (defvar LilyPond-mode-map ()
498 "Keymap used in `LilyPond-mode' buffers.")
500 ;; Note: if you make changes to the map, you must do
501 ;; M-x set-variable LilyPond-mode-map nil
504 ;; to let the changest take effect
506 (if LilyPond-mode-map
508 (setq LilyPond-mode-map (make-sparse-keymap))
509 (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
510 (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
511 (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
512 (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-job)
513 (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
514 (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
515 (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
516 (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
517 (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
518 (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
519 (define-key LilyPond-mode-map "\C-c\C-m" 'LilyPond-command-next-midi)
520 (define-key LilyPond-mode-map "\C-x\C-s" 'LilyPond-save-buffer)
521 (define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
522 (define-key LilyPond-mode-map "\C-ci" 'LilyPond-quick-note-insert)
523 (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
524 (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
525 (define-key LilyPond-mode-map "\C-c:" 'LilyPond-un-comment-region)
526 (define-key LilyPond-mode-map "\C-c;" 'comment-region)
527 (define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren)
528 (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren)
529 (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren)
534 (defun LilyPond-quick-note-insert()
535 "Insert notes with fewer key strokes by using a simple keyboard piano."
538 '(("k" "a") ("l" "b") ("a" "c") ("s" "d")
539 ("d" "e") ("f" "f") ("j" "g") ("r" "r")))
540 (setq dutch-note-ends '("eses" "es" "" "is" "isis"))
541 (setq dutch-note-replacements '("" ""))
542 (setq finnish-note-replacements
543 '(("eeses" "eses") ("ees" "es") ("aeses" "asas") ("aes" "as") ("b" "h")
544 ("beses" "heses") ("bes" "b") ("bis" "his") ("bisis" "hisis")))
545 ; add more translations of the note names
546 (setq spanish-note-replacements
547 '(("c" "do") ("d" "re") ("e" "mi") ("f" "fa") ("g" "sol") ("a" "la") ("b" "si")
548 ("cis" "dos") ("cisis" "doss") ("ces" "dob") ("ceses" "dobb")
549 ("dis" "res") ("disis" "ress") ("des" "reb") ("deses" "rebb")
550 ("eis" "mis") ("eisis" "miss") ("ees" "mib") ("eeses" "mibb")
551 ("fis" "fas") ("fisis" "fass") ("fes" "fab") ("feses" "fabb")
552 ("gis" "sols") ("gisis" "solss") ("ges" "solb") ("geses" "solbb")
553 ("ais" "las") ("aisis" "lass") ("aes" "lab") ("aeses" "labb")
554 ("bis" "sis") ("bisis" "siss") ("bes" "sib") ("beses" "sibb")))
555 (setq other-keys "()<>~}")
556 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)
558 (message "Press h for help.") (sit-for 0 750 1)
560 (setq note-replacements dutch-note-replacements)
561 (while (not (= 27 ; esc to quit
562 (setq x (read-char-exclusive
563 (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"
564 (if (string= (car(cdr(assoc "b" note-replacements))) "h")
566 (nth (+ accid 2) dutch-note-ends)
567 (make-string (abs octav) (if (> octav 0) ?' ?,))
569 (if (string= durat "") "" (make-string dots ?.)))))))
570 ; (insert (number-to-string x)) ; test numbers for characters
571 (setq note (cdr (assoc (char-to-string x) dutch-notes)))
573 ((= x 127) (backward-kill-word 1)) ; backspace
574 ((= x 13) (progn (insert "\n") (LilyPond-indent-line)))) ; return
575 (setq x (char-to-string x))
577 ((and (string< x "9") (string< "0" x))
578 (progn (setq durat (int-to-string (expt 2 (- (string-to-int x) 1))))
580 ((string= x " ") (insert " "))
581 ((string= x "/") (progn (insert "\\times ")
582 (while (not (and (string< x "9") (string< "0" x)))
583 (setq x (char-to-string (read-char-exclusive "Insert a number for the denominator (\"x/\")"))))
584 (insert (format "%s/" x)) (setq x "/")
585 (while (not (and (string< x "9") (string< "0" x)))
586 (setq x (char-to-string (read-char-exclusive "Insert a number for the numerator (\"/y\")"))))
587 (insert (format "%s { " x))))
588 ((string= x "0") (progn (setq accid 0) (setq octav 0)
589 (setq durat "") (setq dots 0)))
590 ((string= x "i") (setq accid (if (= accid 2) 0 (max (+ accid 1) 1))))
591 ((string= x "e") (setq accid (if (= accid -2) 0 (min (+ accid -1) -1))))
592 ((string= x "'") (setq octav (if (= octav 4) 0 (max (+ octav 1) 1))))
593 ((string= x ",") (setq octav (if (= octav -4) 0 (min (+ octav -1) -1))))
594 ((string= x ".") (setq dots (if (= dots 4) 0 (+ dots 1))))
595 ((not (null (member x (split-string other-keys ""))))
596 (insert (format "%s " x)))
600 (format "%s%s" (car note) (if (string= "r" (car note)) ""
601 (nth (+ accid 2) dutch-note-ends))))
602 (setq notetwo (car (cdr (assoc note note-replacements))))
603 (if (not (null notetwo)) (setq note notetwo))
607 (if (string= "r" note) ""
608 (make-string (abs octav) (if (> octav 0) ?' ?,)))
610 (if (string= durat "") "" (make-string dots ?.))))
611 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)))
612 ((string= x "t") (progn (setq note-replacements dutch-note-replacements)
613 (message "Selected Dutch notes")
614 (sit-for 0 750 1))) ; t
615 ((string= x "n") (progn (setq note-replacements finnish-note-replacements)
616 (message "Selected Finnish/Deutsch notes")
617 (sit-for 0 750 1))) ; n
618 ; add more translations of the note names
619 ((string= x "p") (progn (setq note-replacements spanish-note-replacements)
620 (message "Selected Spanish notes")
621 (sit-for 0 750 1))) ; p
623 (progn (message "Insert notes with fewer key strokes. For example \"i,5.f\" produces \"fis,32. \".") (sit-for 5 0 1)
624 (message "Add also \"a ~ a\"-ties, \"a ( ) b\"-slurs and \"< a b >\"-chords.") (sit-for 5 0 1)
625 (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)
626 (message "Backspace deletes last note, return starts a new indented line and Esc quits.") (sit-for 5 0 1)
627 (message "Insert note triplets \"\\times 2/3 { a b } \" by typing \"/23ab}\".") (sit-for 5 0 1)
628 (message "Remember to add all other details as well.") (sit-for 5 0 1)))
631 (define-skeleton LilyPond-insert-tag-notes
632 "LilyPond notes tag."
634 ; (if (bolp) nil ?\n)
636 (if (y-or-n-p "Set \"\\relative\" attribute? ")
637 (concat " \\relative " (skeleton-read "Relative: " "" str)))
640 (define-skeleton LilyPond-insert-tag-score
641 "LilyPond score tag."
647 (if (y-or-n-p "Insert \"\\header\" field? ")
648 (concat " \\header {\n "
649 (skeleton-read "Piece: " "piece = " str) "\n"
650 (if (y-or-n-p "Insert \"opus\" field? ")
651 (concat " " (skeleton-read "Opus: " "opus = " str) "\n"))
653 (if (y-or-n-p "Insert \"\\midi\" field? ")
655 (skeleton-read "Midi: " "\\tempo 4 = " str)
659 (defun LilyPond-command-menu-entry (entry)
660 ;; Return LilyPond-command-alist ENTRY as a menu item.
661 (let ((name (car entry)))
662 (cond ((and (string-equal name LilyPond-command-Print)
663 LilyPond-printer-list)
664 (let ((command LilyPond-print-command)
666 (append (list LilyPond-command-Print)
667 (mapcar 'LilyPond-command-menu-printer-entry
668 LilyPond-printer-list))))
670 (vector name (list 'LilyPond-command-menu name) t)))))
673 (easy-menu-define LilyPond-command-menu
675 "Menu used in LilyPond mode."
678 [ "Master File" LilyPond-command-select-master
679 :keys "C-c C-c" :style radio
680 :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
681 [ "Buffer" LilyPond-command-select-buffer
682 :keys "C-c C-b" :style radio
683 :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
684 [ "Region" LilyPond-command-select-region
685 :keys "C-c C-r" :style radio
686 :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
687 ; (let ((file 'LilyPond-command-on-current))
688 ; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
689 ;;; Some kind of mapping which includes :keys might be more elegant
690 '([ "LilyPond" (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file) :keys "C-c C-l"])
691 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
692 '([ "2Dvi" (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file) :keys "C-c C-d"])
693 '([ "2PS" (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file) :keys "C-c C-f"])
694 '([ "2Midi" (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-master-file)])
695 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
696 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
697 '([ "SmartView" (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file) :keys "C-c C-s"])
698 '([ "View" (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file) :keys "C-c C-v"])
699 '([ "ViewPS" (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file) :keys "C-c C-p"])
700 '([ "Midi (off)" (LilyPond-command-next-midi) :keys "C-c C-m"])
701 '([ "Midi all" (LilyPond-command-all-midi)])
704 (easy-menu-define LilyPond-mode-menu
706 "Menu used in LilyPond mode."
707 (append '("LilyPond")
709 [ "\\notes..." LilyPond-insert-tag-notes t]
710 [ "\\score..." LilyPond-insert-tag-score t]
711 ["Quick Notes" LilyPond-quick-note-insert t]
714 ["Uncomment Region" LilyPond-un-comment-region t]
715 ["Comment Region" comment-region t]
716 ["Refontify buffer" font-lock-fontify-buffer t]
720 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z_][a-zA-Z0-9_]*\\) *="
721 "Regexp matching Identifier definitions.")
723 (defvar LilyPond-imenu-generic-expression
724 (list (list nil LilyPond-imenu-generic-re 1))
725 "Expression for imenu")
727 (defun LilyPond-command-select-master ()
729 (message "Next command will be on the master file")
730 (setq LilyPond-command-current 'LilyPond-command-master))
732 (defun LilyPond-command-select-buffer ()
734 (message "Next command will be on the buffer")
735 (setq LilyPond-command-current 'LilyPond-command-buffer))
737 (defun LilyPond-command-select-region ()
739 (message "Next command will be on the region")
740 (setq LilyPond-command-current 'LilyPond-command-region))
742 (defun LilyPond-command-menu (name)
743 ;; Execute LilyPond-command-alist NAME from a menu.
744 (let ((LilyPond-command-force name))
745 (funcall LilyPond-command-current)))
747 (defun LilyPond-mode ()
748 "Major mode for editing LilyPond music files.
750 This mode knows about LilyPond keywords and line comments, not about
751 indentation or block comments. It features easy compilation, error
752 finding and viewing of a LilyPond source buffer or region.
755 \\{LilyPond-mode-map}
758 LilyPond-command-alist\t\talist from name to command
759 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
761 ;; set up local variables
762 (kill-all-local-variables)
764 (make-local-variable 'font-lock-defaults)
765 (setq font-lock-defaults '(LilyPond-font-lock-keywords))
767 ;; string and comments are fontified explicitly
768 (make-local-variable 'font-lock-keywords-only)
769 (setq font-lock-keywords-only t)
771 ;; Multi-line font-locking needs Emacs 21.1 or newer.
772 ;; For older versions there is hotkey "C-c f".
773 (make-local-variable 'font-lock-multiline)
774 (setq font-lock-multiline t)
776 (make-local-variable 'paragraph-separate)
777 (setq paragraph-separate "^[ \t]*$")
779 (make-local-variable 'paragraph-start)
780 (setq paragraph-start "^[ \t]*$")
782 (make-local-variable 'comment-start)
783 (setq comment-start "%")
785 (make-local-variable 'comment-start-skip)
786 (setq comment-start-skip "%{? *")
788 (make-local-variable 'comment-end)
789 (setq comment-end "")
791 (make-local-variable 'block-comment-start)
792 (setq block-comment-start "%{")
794 (make-local-variable 'block-comment-end)
795 (setq block-comment-end "%}")
797 (make-local-variable 'indent-line-function)
798 (setq indent-line-function 'LilyPond-indent-line)
800 (set-syntax-table LilyPond-mode-syntax-table)
801 (setq major-mode 'LilyPond-mode)
802 (setq mode-name "LilyPond")
803 (setq local-abbrev-table LilyPond-mode-abbrev-table)
804 (use-local-map LilyPond-mode-map)
806 (make-local-variable 'imenu-generic-expression)
807 (setq imenu-generic-expression LilyPond-imenu-generic-expression)
808 (imenu-add-to-menubar "Index")
810 ;; run the mode hook. LilyPond-mode-hook use is deprecated
811 (run-hooks 'LilyPond-mode-hook))
813 (defun LilyPond-version ()
814 "Echo the current version of `LilyPond-mode' in the minibuffer."
816 (message "Using `LilyPond-mode' version %s" LilyPond-version))
818 (provide 'lilypond-mode)
819 ;;; lilypond-mode.el ends here