2 ;;; lilypond-mode.el --- Major mode for editing GNU LilyPond music scores
4 ;;; source file of the GNU LilyPond music typesetter
6 ;;; (c) 1999--2003 Jan Nieuwenhuizen <janneke@gnu.org>
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)
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.
25 (defconst LilyPond-version "1.7.21"
26 "`LilyPond-mode' version number.")
28 (defconst LilyPond-help-address "bug-lilypond@gnu.org"
29 "Address accepting submission of bug reports.")
31 (defvar LilyPond-mode-hook nil
32 "*Hook called by `LilyPond-mode'.")
34 (defvar LilyPond-region-file-prefix "emacs-lily"
35 "File prefix for commands on buffer or region.")
37 ;; FIXME: find ``\score'' in buffers / make settable?
38 (defun LilyPond-master-file ()
42 (defvar LilyPond-kick-xdvi nil
43 "If true, no simultaneous xdvi's are started, but reload signal is sent.")
45 (defvar LilyPond-command-history nil
46 "Command history list.")
48 (defvar LilyPond-regexp-alist
49 '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
50 "Regexp used to match LilyPond errors. See `compilation-error-regexp-alist'.")
52 (defvar LilyPond-imenu nil
53 "A flag to tell whether LilyPond-imenu is turned on.")
54 (make-variable-buffer-local 'LilyPond-imenu)
56 (defcustom LilyPond-include-path ".:/tmp"
57 "* LilyPond include path."
61 (defun LilyPond-words-filename ()
62 "The file containing LilyPond \keywords \Identifiers and ReservedWords.
63 Finds file lilypond-words from load-path."
66 (words-file "lilypond.words"))
67 (while (and (> (length lp) 0) (not fn))
68 (setq fn (concat (car lp) "/" words-file))
69 (if (not (file-readable-p fn))
70 (progn (setq fn nil) (setq lp (cdr lp)))))
72 (progn (message "Warning: `lilypond.words' not found in `load-path'. See `lilypond-init.el'.")
76 (defun LilyPond-add-dictionary-word (x)
77 "Contains all words: \keywords \Identifiers and ReservedWords."
78 (nconc '(("" . 1)) x))
80 ;; creates dictionary if empty
81 (if (and (eq (length (LilyPond-add-dictionary-word ())) 1)
82 (not (eq (LilyPond-words-filename) nil)))
84 (setq b (find-file-noselect (LilyPond-words-filename) t t))
85 (setq m (set-marker (make-marker) 1 (get-buffer b)))
87 (while (> (buffer-size b) (marker-position m))
89 (setq copy (copy-alist (list (eval (symbol-name (read m))))))
91 (LilyPond-add-dictionary-word (list copy)))
94 (defconst LilyPond-keywords
96 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
98 (while (> (length co) 0)
99 (if (> (length (car co)) 1)
100 (if (and (string-equal "\\" (substring (car co) 0 1))
101 (string-equal (downcase (car co)) (car co)))
102 (add-to-list 'wordlist (car co))))
104 (if (eq (length wordlist) 0)
105 (setq wordlist '("\\score"))) ; add \keywords to lilypond.words
107 "LilyPond \\keywords")
109 (defconst LilyPond-identifiers
111 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
113 (while (> (length co) 0)
114 (if (> (length (car co)) 1)
115 (if (and (string-equal "\\" (substring (car co) 0 1))
116 (not (string-equal (downcase (car co)) (car co))))
117 (add-to-list 'wordlist (car co))))
119 (if (eq (length wordlist) 0)
120 (setq wordlist '("\\voiceOne"))) ; add \Identifiers to lilypond.words
122 "LilyPond \\Identifiers")
124 (defconst LilyPond-reserved-words
126 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
128 (while (> (length co) 0)
129 (if (> (length (car co)) 0)
130 (if (not (string-equal "\\" (substring (car co) 0 1)))
131 (add-to-list 'wordlist (car co))))
133 (if (eq (length wordlist) 0)
134 (setq wordlist '("Staff"))) ; add ReservedWords to lilypond.words
136 "LilyPond ReservedWords")
138 (defun LilyPond-check-files (derived originals extensions)
139 "Check that DERIVED is newer than any of the ORIGINALS.
140 Try each original with each member of EXTENSIONS, in all directories
141 in LilyPond-include-path."
143 (regexp (concat "\\`\\("
144 (mapconcat (function (lambda (dir)
145 (regexp-quote (expand-file-name dir))))
146 LilyPond-include-path "\\|")
148 (mapconcat 'regexp-quote originals "\\|")
150 (mapconcat 'regexp-quote extensions "\\|")
152 (buffers (buffer-list)))
154 (let* ((buffer (car buffers))
155 (name (buffer-file-name buffer)))
156 (setq buffers (cdr buffers))
157 (if (and name (string-match regexp name))
159 (and (buffer-modified-p buffer)
160 (or (not LilyPond-save-query)
161 (y-or-n-p (concat "Save file "
162 (buffer-file-name buffer)
164 (save-excursion (set-buffer buffer) (save-buffer)))
165 (if (file-newer-than-file-p name derived)
169 (defun LilyPond-running ()
170 "Check the currently running LilyPond compiling jobs."
171 (let ((process-names (list "lilypond" "tex" "2dvi" "2ps" "2midi"
174 (while (setq process-name (pop process-names))
175 (setq process (get-process process-name))
177 (eq (process-status process) 'run))
178 (push process-name running)))
179 running)) ; return the running jobs
181 (defun LilyPond-midi-running ()
182 "Check the currently running Midi processes."
183 (let ((process-names (list "midi" "midiall"))
185 (while (setq process-name (pop process-names))
186 (setq process (get-process process-name))
188 (eq (process-status process) 'run))
189 (push process-name running)))
190 running)) ; return the running jobs
192 (defun LilyPond-kill-jobs ()
193 "Kill the currently running LilyPond compiling jobs."
195 (let ((process-names (LilyPond-running))
197 (while (setq process-name (pop process-names))
198 (quit-process (get-process process-name) t)
199 (push process-name killed))
200 killed)) ; return the killed jobs
202 (defun LilyPond-kill-midi ()
203 "Kill the currently running midi processes."
204 (let ((process-names (LilyPond-midi-running))
206 (while (setq process-name (pop process-names))
207 (quit-process (get-process process-name) t)
208 (push process-name killed))
209 killed)) ; return the killed jobs
211 ;; URG, should only run LilyPond-compile for LilyPond
212 ;; not for tex,xdvi (ly2dvi?)
213 (defun LilyPond-compile-file (command name)
214 ;; We maybe should know what we run here (Lily, ly2dvi, tex)
215 ;; and adjust our error-matching regex ?
217 (if (eq LilyPond-command-current 'LilyPond-command-master)
219 ;; use temporary directory for Commands on Buffer/Region
220 ;; hm.. the directory is set twice, first to default-dir
221 (concat "cd " (LilyPond-temp-directory) "; " command))
222 "No more errors" name))
224 ;; do we still need this, now that we're using compile-internal?
225 (defun LilyPond-save-buffer ()
226 "Save buffer and set default command for compiling."
228 (if (buffer-modified-p)
230 (setq LilyPond-command-default "LilyPond"))))
232 ;;; return (dir base ext)
233 (defun split-file-name (name)
234 (let* ((i (string-match "[^/]*$" name))
235 (dir (if (> i 0) (substring name 0 i) "./"))
236 (file (substring name i (length name)))
237 (i (string-match "[^.]*$" file)))
241 (list dir (substring file 0 (- i 1)) (substring file i (length file)))
242 (list dir file ""))))
245 ;; Should check whether in command-alist?
246 (defcustom LilyPond-command-default "LilyPond"
247 "Default command. Must identify a member of LilyPond-command-alist."
251 ;;;(make-variable-buffer-local 'LilyPond-command-last)
253 (defvar LilyPond-command-current 'LilyPond-command-master)
254 ;;;(make-variable-buffer-local 'LilyPond-command-master)
257 ;; If non-nil, LilyPond-command-query will return the value of this
258 ;; variable instead of quering the user.
259 (defvar LilyPond-command-force nil)
261 (defcustom LilyPond-xdvi-command "xdvi"
262 "Command used to display DVI files."
267 (defcustom LilyPond-gv-command "gv -watch"
268 "Command used to display PS files."
273 (defcustom LilyPond-midi-command "timidity"
274 "Command used to play MIDI files."
279 (defcustom LilyPond-all-midi-command "timidity -ia"
280 "Command used to play MIDI files."
285 (defun LilyPond-command-current-midi ()
286 "Play midi corresponding to the current document."
288 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file))
290 (defun LilyPond-command-all-midi ()
291 "Play midi corresponding to the current document."
293 (LilyPond-command (LilyPond-command-menu "MidiAll") 'LilyPond-master-file))
295 (defun count-rexp (start end rexp)
296 "Print number of found regular expressions in the region."
300 (narrow-to-region start end)
301 (goto-char (point-min))
302 (count-matches rexp))))
304 (defun count-midi-words ()
305 "Check number of midi-scores before the curser."
306 (if (eq LilyPond-command-current 'LilyPond-command-region)
307 (count-rexp (mark t) (point) "\\\\midi")
308 (count-rexp (point-min) (point-max) "\\\\midi")))
310 (defun count-midi-words-backwards ()
311 "Check number of midi-scores before the curser."
312 (if (eq LilyPond-command-current 'LilyPond-command-region)
313 (count-rexp (mark t) (point) "\\\\midi")
314 (count-rexp (point-min) (point) "\\\\midi")))
316 (defun LilyPond-string-current-midi ()
317 "Check the midi file of the following midi-score in the current document."
318 (let ((fnameprefix (if (eq LilyPond-command-current 'LilyPond-command-master)
319 (substring (LilyPond-master-file) 0 -3); suppose ".ly"
320 LilyPond-region-file-prefix))
321 (allcount (string-to-number (substring (count-midi-words) 0 -12)))
322 (count (string-to-number (substring (count-midi-words-backwards) 0 -12))))
324 (if (and (> allcount 1) (> count 0)) ; not first score
325 (if (eq count allcount) ; last score
326 (concat "-" (number-to-string (+ count -1)))
327 (concat "-" (number-to-string count))))
330 (defun LilyPond-string-all-midi ()
331 "Return the midi files of the current document in ascending order."
332 (let ((fnameprefix (if (eq LilyPond-command-current 'LilyPond-command-master)
333 (substring (LilyPond-master-file) 0 -3); suppose ".ly"
334 LilyPond-region-file-prefix))
335 (allcount (string-to-number (substring (count-midi-words) 0 -12))))
336 (concat (if (> allcount 0) ; at least one midi-score
337 (concat fnameprefix ".midi "))
338 (if (> allcount 1) ; more than one midi-score
339 (concat fnameprefix "-[1-9].midi "))
340 (if (> allcount 9) ; etc.
341 (concat fnameprefix "-[1-9][0-9].midi"))
342 (if (> allcount 99) ; not first score
343 (concat fnameprefix "-[1-9][0-9][0-9].midi")))))
345 ;; This is the major configuration variable.
346 (defcustom LilyPond-command-alist
347 ;; Should expand this to include possible keyboard shortcuts which
348 ;; could then be mapped to define-key and menu.
350 ("LilyPond" . ("lilypond %s" . "LaTeX"))
351 ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
353 ("2Dvi" . ("ly2dvi %s" . "View"))
354 ("2PS" . ("ly2dvi -P %s" . "ViewPS"))
355 ("2Midi" . ("lilypond -m %s" . "View"))
357 ("Book" . ("lilypond-book %x" . "LaTeX"))
358 ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
360 ;; point-n-click (arg: exits upop USR1)
361 ("SmartView" . ("xdvi %d" . "LilyPond"))
363 ;; refreshes when kicked USR1
364 ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
365 ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
367 ;; The following are refreshed in LilyPond-command:
368 ;; - current-midi depends on cursor position and
369 ("Midi" . (,(concat LilyPond-midi-command " " (LilyPond-string-current-midi)) . "LilyPond" )) ;
370 ;; - all-midi depends on number of midi-score.
371 ("MidiAll" . (,(concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)) . "LilyPond"))
374 "AList of commands to execute on the current document.
376 The key is the name of the command as it will be presented to the
377 user, the value is a cons of the command string handed to the shell
378 after being expanded, and the next command to be executed upon
379 success. The expansion is done using the information found in
380 LilyPond-expand-list.
383 :type '(repeat (cons :tag "Command Item"
386 (string :tag "Command")
387 (string :tag "Next Key")))))
390 (defcustom LilyPond-file-extension ".ly"
391 "*File extension used in LilyPond sources."
396 (defcustom LilyPond-expand-alist
407 "Alist of expansion strings for LilyPond command names."
409 :type '(repeat (cons :tag "Alist item"
410 (string :tag "Symbol")
411 (string :tag "Expansion"))))
414 (defcustom LilyPond-command-Show "View"
415 "*The default command to show (view or print) a LilyPond file.
416 Must be the car of an entry in `LilyPond-command-alist'."
419 (make-variable-buffer-local 'LilyPond-command-Show)
421 (defcustom LilyPond-command-Print "Print"
422 "The name of the Print entry in LilyPond-command-Print."
426 (defun xLilyPond-compile-sentinel (process msg)
428 (= 0 (process-exit-status process)))
429 (setq LilyPond-command-default
430 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
432 ;; FIXME: shouldn't do this for stray View/xdvi
433 (defun LilyPond-compile-sentinel (buffer msg)
434 (if (string-match "^finished" msg)
435 (setq LilyPond-command-default
436 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
438 ;;(make-variable-buffer-local 'compilation-finish-function)
439 (setq compilation-finish-function 'LilyPond-compile-sentinel)
441 (defun LilyPond-command-query (name)
442 "Query the user for what LilyPond command to use."
443 (let* ((default (cond ((if (string-equal name LilyPond-region-file-prefix)
444 (LilyPond-check-files (concat name ".tex")
446 (list LilyPond-file-extension))
447 (if (verify-visited-file-modtime (current-buffer))
448 (if (buffer-modified-p)
449 (if (y-or-n-p "Save buffer before next command? ")
450 (LilyPond-save-buffer)))
451 (if (y-or-n-p "The command will be invoked to an already saved buffer. Revert it? ")
452 (revert-buffer t t)))
454 LilyPond-command-default))
455 (t LilyPond-command-default)))
457 (completion-ignore-case t)
459 (answer (or LilyPond-command-force
461 (concat "Command: (default " default ") ")
462 LilyPond-command-alist nil t nil 'LilyPond-command-history))))
464 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
465 (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
467 (not (string-equal answer "")))
471 (defun LilyPond-command-master ()
472 "Run command on the current document."
474 (LilyPond-command-select-master)
475 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
476 'LilyPond-master-file))
478 (defun LilyPond-command-lilypond ()
479 "Run lilypond for the current document."
481 (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file)
484 (defun LilyPond-command-formatdvi ()
485 "Format the dvi output of the current document."
487 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
490 (defun LilyPond-command-formatps ()
491 "Format the ps output of the current document."
493 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
496 (defun LilyPond-command-formatmidi ()
497 "Format the midi output of the current document."
499 (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-master-file)
502 (defun LilyPond-command-smartview ()
503 "View the dvi output of current document."
505 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
508 (defun LilyPond-command-view ()
509 "View the dvi output of current document."
511 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
514 (defun LilyPond-command-viewps ()
515 "View the ps output of current document."
517 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
520 ;; FIXME, this is broken
521 (defun LilyPond-region-file (begin end)
524 (dir (LilyPond-temp-directory))
525 (base LilyPond-region-file-prefix)
526 (ext LilyPond-file-extension))
527 (concat dir base ext)))
529 ;;; Commands on Region work if there is an appropriate '\score'.
530 (defun LilyPond-command-region (begin end)
531 "Run LilyPond on the current region."
533 (if (or (> begin (point-min)) (< end (point-max)))
534 (LilyPond-command-select-region))
535 (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
536 (LilyPond-command (LilyPond-command-query
537 (LilyPond-region-file begin end))
538 '(lambda () (LilyPond-region-file begin end)))
539 ;; Region may deactivate even if buffer was intact, reactivate?
540 ;; Currently, also deactived regions are used.
543 (defun LilyPond-command-buffer ()
544 "Run LilyPond on buffer."
546 (LilyPond-command-select-buffer)
547 (LilyPond-command-region (point-min) (point-max)))
549 (defun LilyPond-command-expand (string file)
550 (let ((case-fold-search nil))
551 (if (string-match "%" string)
552 (let* ((b (match-beginning 0))
554 (l (split-file-name file))
557 (LilyPond-command-expand
558 (concat (substring string 0 b)
561 (let ((entry (assoc (substring string b e)
562 LilyPond-expand-alist)))
563 (if entry (cdr entry) ""))
564 (substring string e))
568 (defun LilyPond-shell-process (name buffer command)
569 (let ((old (current-buffer)))
570 (switch-to-buffer-other-window buffer)
571 ;; If we empty the buffer don't see messages scroll by.
574 (start-process-shell-command name buffer command)
575 (switch-to-buffer-other-window old)))
578 (defun LilyPond-command (name file)
579 "Run command NAME on the file you get by calling FILE.
581 FILE is a function return a file name. It has one optional argument,
582 the extension to use on the file.
584 Use the information in LilyPond-command-alist to determine how to run the
587 (let ((entry (assoc name LilyPond-command-alist)))
589 (let ((command (LilyPond-command-expand (cadr entry)
592 (job-string "no jobs"))
593 (if (member name (list "View" "ViewPS"))
594 ;; is USR1 a right signal for viewps?
595 (let ((buffer-xdvi (get-buffer-create (concat "*" name "*"))))
596 (if LilyPond-kick-xdvi
597 (let ((process-xdvi (get-buffer-process buffer-xdvi)))
599 (signal-process (process-id process-xdvi) 'SIGUSR1)
600 (LilyPond-shell-process name buffer-xdvi command)))
601 (LilyPond-shell-process name buffer-xdvi command)))
603 (if (string-equal name "Midi")
605 (setq command (concat LilyPond-midi-command " " (LilyPond-string-current-midi)))
606 (if (LilyPond-kill-midi)
607 (setq job-string nil)))) ; either stop or start playing
608 (if (string-equal name "MidiAll")
610 (setq command (concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)))
611 (LilyPond-kill-midi))) ; stop and start playing
612 (if (and (member name (list "Midi" "MidiAll")) job-string)
613 (if (file-newer-than-file-p
614 (LilyPond-master-file)
615 (concat (substring (LilyPond-master-file) 0 -3) ".midi"))
616 (if (y-or-n-p "Midi older than source. Reformat midi?")
618 (LilyPond-command-formatmidi)
619 (while (LilyPond-running)
620 (message "Starts playing midi once it is built.")
622 (if (member name (list "LilyPond" "TeX" "2Midi" "2PS" "2Dvi"
624 (if (setq jobs (LilyPond-running))
626 (setq job-string "Process") ; could also suggest compiling after process has ended
628 (setq job-string (concat job-string " \"" (pop jobs) "\"")))
629 (setq job-string (concat job-string " is already running; kill it to proceed "))
630 (if (y-or-n-p job-string)
632 (setq job-string "no jobs")
634 (while (LilyPond-running)
636 (setq job-string nil)))))
638 (setq LilyPond-command-default name)
639 (if (string-equal job-string "no jobs")
640 (LilyPond-compile-file command name))))))))
642 (defun LilyPond-mark-active ()
643 "Check if there is an active mark."
644 (and transient-mark-mode
645 (if (string-match "XEmacs\\|Lucid" emacs-version) (mark) mark-active)))
647 (defun LilyPond-temp-directory ()
648 "Temporary file directory for Commands on Region."
650 (if (string-match "XEmacs\\|Lucid" emacs-version)
651 (concat (temp-directory) "/")
652 temporary-file-directory))
656 (defvar LilyPond-mode-map ()
657 "Keymap used in `LilyPond-mode' buffers.")
659 ;; Note: if you make changes to the map, you must do
660 ;; M-x set-variable LilyPond-mode-map nil
663 ;; to let the changest take effect
665 (if LilyPond-mode-map
667 (setq LilyPond-mode-map (make-sparse-keymap))
668 ;; Put keys to Lilypond-command-alist and fetch them from there somehow.
669 (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
670 (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
671 (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
672 (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-jobs)
673 (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
674 (define-key LilyPond-mode-map "\C-cm" 'LilyPond-command-formatmidi)
675 (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
676 (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
677 (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
678 (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
679 (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
680 (define-key LilyPond-mode-map [(control c) return] 'LilyPond-command-current-midi)
681 (define-key LilyPond-mode-map [(control c) (control return)] 'LilyPond-command-all-midi)
682 (define-key LilyPond-mode-map "\C-x\C-s" 'LilyPond-save-buffer)
683 (define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
684 (define-key LilyPond-mode-map "\C-ci" 'LilyPond-quick-note-insert)
685 (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
686 (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
687 (define-key LilyPond-mode-map "\C-c;" 'LilyPond-comment-region)
688 (define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren)
689 (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren)
690 (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren)
691 (define-key LilyPond-mode-map "]" 'LilyPond-electric-close-paren)
692 (if (string-match "XEmacs\\|Lucid" emacs-version)
693 (define-key LilyPond-mode-map [iso-left-tab] 'LilyPond-autocompletion)
694 (define-key LilyPond-mode-map [iso-lefttab] 'LilyPond-autocompletion))
695 (define-key LilyPond-mode-map "\C-c\t" 'LilyPond-info-index-search)
700 (defun LilyPond-quick-note-insert()
701 "Insert notes with fewer key strokes by using a simple keyboard piano."
704 '(("k" "a") ("l" "b") ("a" "c") ("s" "d")
705 ("d" "e") ("f" "f") ("j" "g") ("r" "r")))
706 (setq dutch-note-ends '("eses" "es" "" "is" "isis"))
707 (setq dutch-note-replacements '("" ""))
708 (setq finnish-note-replacements
709 '(("eeses" "eses") ("ees" "es") ("aeses" "asas") ("aes" "as") ("b" "h")
710 ("beses" "heses") ("bes" "b") ("bis" "his") ("bisis" "hisis")))
711 ; add more translations of the note names
712 (setq spanish-note-replacements
713 '(("c" "do") ("d" "re") ("e" "mi") ("f" "fa") ("g" "sol") ("a" "la") ("b
715 ("cis" "dos") ("cisis" "doss") ("ces" "dob") ("ceses" "dobb")
716 ("dis" "res") ("disis" "ress") ("des" "reb") ("deses" "rebb")
717 ("eis" "mis") ("eisis" "miss") ("ees" "mib") ("eeses" "mibb")
718 ("fis" "fas") ("fisis" "fass") ("fes" "fab") ("feses" "fabb")
719 ("gis" "sols") ("gisis" "solss") ("ges" "solb") ("geses" "solbb")
720 ("ais" "las") ("aisis" "lass") ("aes" "lab") ("aeses" "labb")
721 ("bis" "sis") ("bisis" "siss") ("bes" "sib") ("beses" "sibb")))
722 (setq other-keys "(<~>)}|")
723 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)
725 (message "Press h for help.") (sit-for 0 750)
727 (setq note-replacements dutch-note-replacements)
728 (setq xkey 0) (setq xinitpoint (point))
729 (< xinitpoint (point))
730 (while (not (= xkey 27)) ; esc to quit
731 (message (format " a[]s[]d f[]j[]k[]l r with %4s%-4s%3s%-4s ie ,' 12345678 . 0 /%sb\\b\\n Esc"
732 (nth (+ accid 2) dutch-note-ends)
733 (make-string (abs octav) (if (> octav 0) ?' ?,))
735 (if (string= durat "") "" (make-string dots ?.))
737 (setq xkey (read-char-exclusive))
738 (setq x (char-to-string xkey))
739 (setq note (cdr (assoc x dutch-notes)))
741 ((string= x "q") (progn (setq xkey 27))) ; quit
742 ((= xkey 13) (progn (insert "\n") (LilyPond-indent-line))) ; return
743 ((or (= xkey 127) ; backspace is recognized as a char only in Emacs
744 (string= x "b")) ; so, we need to define an another char for XEmacs
745 (progn (narrow-to-region xinitpoint (point))
746 (backward-kill-word 1)
748 ((and (string< x "9") (string< "0" x))
749 (progn (setq durat (int-to-string (expt 2 (- (string-to-int x) 1))))
751 ((string= x " ") (insert " "))
752 ((string= x "/") (progn (insert "\\times ")
753 (message "Insert a number for the denominator (\"x/\")")
754 (while (not (and (string< x "9") (string< "0" x)))
755 (setq x (char-to-string (read-char-exclusive))))
756 (insert (format "%s/" x)) (setq x "/")
757 (message "Insert a number for the numerator (\"/y\")")
758 (while (not (and (string< x "9") (string< "0" x)))
759 (setq x (char-to-string (read-char-exclusive))))
760 (insert (format "%s { " x))))
761 ((string= x "0") (progn (setq accid 0) (setq octav 0)
762 (setq durat "") (setq dots 0)))
763 ((string= x "i") (setq accid (if (= accid 2) 0 (max (+ accid 1) 1))))
764 ((string= x "e") (setq accid (if (= accid -2) 0 (min (+ accid -1) -1))))
765 ((string= x "'") (setq octav (if (= octav 4) 0 (max (+ octav 1) 1))))
766 ((string= x ",") (setq octav (if (= octav -4) 0 (min (+ octav -1) -1))))
767 ((string= x ".") (setq dots (if (= dots 4) 0 (+ dots 1))))
768 ((not (null (member x (split-string other-keys ""))))
769 (insert (format "%s " x)))
773 (format "%s%s" (car note) (if (string= "r" (car note)) ""
774 (nth (+ accid 2) dutch-note-ends))))
775 (setq notetwo (car (cdr (assoc note note-replacements))))
776 (if (not (null notetwo)) (setq note notetwo))
780 (if (string= "r" note) ""
781 (make-string (abs octav) (if (> octav 0) ?' ?,)))
783 (if (string= durat "") "" (make-string dots ?.))))
784 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)))
785 ((string= x "t") (progn (setq note-replacements dutch-note-replacements)
786 (message "Selected Dutch notes")
788 ((string= x "n") (progn (setq note-replacements finnish-note-replacements)
789 (message "Selected Finnish/Deutsch notes")
791 ; add more translations of the note names
792 ((string= x "p") (progn (setq note-replacements spanish-note-replacements)
793 (message "Selected Spanish notes")
796 (progn (message "Insert notes with fewer key strokes. For example \"i,5.f\" produces \"fis,32. \".") (sit-for 5 0)
797 (message "Add also \"a ~ a\"-ties, \"a ( ) b\"-slurs and \"< a b >\"-chords.") (sit-for 5 0)
798 (message "There are Du(t)ch, Fin(n)ish/Deutsch (hit 'n') and S(p)anish note names.") (sit-for 5 0)
799 (message "(B)ackspace deletes last note, Ret starts a new indented line and Esc (q)uits.") (sit-for 5 0)
800 (message "Insert note triplets \"\\times 2/3 { a b } \" by typing \"/23ab}\".") (sit-for 5 0)
801 (message "This mode is experimental. Use normal mode to add further details.") (sit-for 5 0)))
802 (t (progn (message "Quick notes mode. Press Esc or q to quit.") (sit-for 0 500)))))
803 (message "Normal editing mode."))
805 (defun LilyPond-pre-word-search ()
806 "Fetch the alphabetic characters and \\ in front of the cursor."
809 (ch (char-before (- (point) 0))))
810 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not bolp, A-Z
811 (and (>= ch 97) (<= ch 122)) ; a-z
813 (setq pre (concat (char-to-string ch) pre))
814 (setq prelen (+ prelen 1))
815 (setq ch (char-before (- (point) prelen))))
818 (defun LilyPond-post-word-search ()
819 "Fetch the alphabetic characters behind the cursor."
822 (ch (char-after (+ (point) 0))))
823 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not eolp, A-Z
824 (and (>= ch 97) (<= ch 122)))) ; a-z
825 (setq post (concat post (char-to-string ch)))
826 (setq postlen (+ postlen 1))
827 (setq ch (char-after (+ (point) postlen))))
830 (defun LilyPond-autocompletion ()
831 "Show completions in mini-buffer for the given word."
833 (let ((pre (LilyPond-pre-word-search))
834 (post (LilyPond-post-word-search))
836 ;; insert try-completion and show all-completions
837 (if (> (length pre) 0)
839 (setq trycomp (try-completion pre (LilyPond-add-dictionary-word ())))
840 (if (char-or-string-p trycomp)
841 (if (string-equal (concat pre post) trycomp)
842 (goto-char (+ (point) (length post)))
844 (delete-region (point) (+ (point) (length post)))
845 (insert (substring trycomp (length pre) nil))))
847 (delete-region (point) (+ (point) (length post)))
848 (font-lock-fontify-buffer))) ; only inserting fontifies
850 (setq complist (all-completions pre (LilyPond-add-dictionary-word ())))
851 (while (> (length complist) 0)
852 (setq compsstr (concat compsstr "\"" (car complist) "\" "))
853 (setq complist (cdr complist)))
857 (defun LilyPond-info ()
858 "Launch Info for lilypond."
862 (defun LilyPond-music-glossary-info ()
863 "Launch Info for music-glossary."
865 (info "music-glossary"))
867 (defun LilyPond-internals-info ()
868 "Launch Info for lilypond-internals."
870 (info "lilypond-internals"))
872 (defun LilyPond-info-index-search ()
873 "In `*info*'-buffer, launch `info lilypond --index-search word-under-cursor'"
875 (let ((str (concat (LilyPond-pre-word-search) (LilyPond-post-word-search))))
876 (if (and (> (length str) 0)
877 (string-equal (substring str 0 1) "\\"))
878 (setq str (substring str 1 nil)))
882 (defun LilyPond-insert-string (pre)
883 "Insert text to the buffer."
887 (defun LilyPond-insert-between (text pre post)
888 "Insert text to the buffer if non-empty string is given."
889 (let ((str (read-string text)))
890 (if (string-equal str "")
892 (progn (setq pre_str_post (concat pre str post))
893 (insert pre_str_post)
894 (length pre_str_post)))))
896 (defun LilyPond-insert-tag-notes ()
897 "LilyPond notes tag."
899 (setq begin (if (LilyPond-mark-active)
900 (mark-marker) (point-marker)))
901 (setq end (point-marker))
903 (setq l1 (LilyPond-insert-string "\\notes "))
904 (setq l2 (LilyPond-insert-between "Relative (e.g. c'): " "\\relative " " "))
906 (setq l2 (LilyPond-insert-between "Transpose (e.g. c c'): " "\\transpose " " ")))
907 (setq l3 (LilyPond-insert-string "{ "))
908 (goto-char (+ end l1 l2 l3))
909 (LilyPond-insert-string " }")
910 (goto-char (+ end l1 l2 l3)))
912 (defun LilyPond-insert-tag-score ()
913 "LilyPond score tag."
915 (setq begin (if (LilyPond-mark-active)
916 (mark-marker) (point-marker)))
917 (setq end (point-marker))
919 (setq l1 (LilyPond-insert-string "\\score {\n ")) ; keep track of lengths
920 (goto-char (+ end l1))
921 (LilyPond-insert-string "\n \\paper { }\n")
922 (setq l2 (if (y-or-n-p "Insert \"\\header\" field? ")
923 (+ (LilyPond-insert-string " \\header {")
924 (LilyPond-insert-between "Title: " "\n title = \"" "\"")
925 (LilyPond-insert-between "Subtitle: " "\n subtitle = \"" "\"")
926 (LilyPond-insert-between "Piece: " "\n piece = \"" "\"")
927 (LilyPond-insert-between "Opus: " "\n opus = \"" "\"")
928 (LilyPond-insert-string "\n }\n"))
930 (setq l3 (if (y-or-n-p "Insert \"\\midi\" field? ")
931 (+ (LilyPond-insert-string " \\midi {")
932 (LilyPond-insert-between "Tempo: (e.g. 4=60) " " \\tempo " "")
933 (LilyPond-insert-string " }\n"))
935 (setq l4 (LilyPond-insert-string "}\n"))
936 (goto-char (+ end l1)))
938 (defun LilyPond-command-menu-entry (entry)
939 ;; Return LilyPond-command-alist ENTRY as a menu item.
940 (let ((name (car entry)))
941 (cond ((and (string-equal name LilyPond-command-Print)
942 LilyPond-printer-list)
943 (let ((command LilyPond-print-command)
945 (append (list LilyPond-command-Print)
946 (mapcar 'LilyPond-command-menu-printer-entry
947 LilyPond-printer-list))))
949 (vector name (list 'LilyPond-command-menu name) t)))))
952 (easy-menu-define LilyPond-command-menu
954 "Menu used in LilyPond mode."
957 [ "Master File" LilyPond-command-select-master
958 :keys "C-c C-c" :style radio
959 :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
960 [ "Buffer" LilyPond-command-select-buffer
961 :keys "C-c C-b" :style radio
962 :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
963 [ "Region" LilyPond-command-select-region
964 :keys "C-c C-r" :style radio
965 :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
966 ;;; (let ((file 'LilyPond-command-on-current))
967 ;;; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
968 ;;; Some kind of mapping which includes :keys might be more elegant
969 ;;; Put keys to Lilypond-command-alist and fetch them from there somehow.
970 '([ "LilyPond" LilyPond-command-lilypond t])
971 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
972 '([ "2Dvi" LilyPond-command-formatdvi t])
973 '([ "2PS" LilyPond-command-formatps t])
974 '([ "2Midi" LilyPond-command-formatmidi t])
975 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
976 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
977 '([ "Kill jobs" LilyPond-kill-jobs t])
979 '([ "SmartView" LilyPond-command-smartview t])
980 '([ "View" LilyPond-command-view t])
981 '([ "ViewPS" LilyPond-command-viewps t])
983 '([ "Midi (toggle)" LilyPond-command-current-midi t])
984 '([ "Midi all" LilyPond-command-all-midi t])
987 ;;; LilyPond-mode-menu should not be interactive, via "M-x LilyPond-<Tab>"
988 (easy-menu-define LilyPond-mode-menu
990 "Menu used in LilyPond mode."
991 (append '("LilyPond")
993 [ "\\notes..." LilyPond-insert-tag-notes t]
994 [ "\\score..." LilyPond-insert-tag-score t]
995 ["Quick Notes" LilyPond-quick-note-insert t]
996 ["Autocompletion" LilyPond-autocompletion t]
999 ["(Un)comment Region" LilyPond-comment-region t]
1000 ["Refontify buffer" font-lock-fontify-buffer t]
1001 ["Add index menu" LilyPond-add-imenu-menu]
1004 ["LilyPond" LilyPond-info t]
1005 ["LilyPond index-search" LilyPond-info-index-search t]
1006 ["Music Glossary" LilyPond-music-glossary-info t]
1007 ["LilyPond internals" LilyPond-internals-info t]
1011 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z]+\\) *="
1012 "Regexp matching Identifier definitions.")
1014 (defvar LilyPond-imenu-generic-expression
1015 (list (list nil LilyPond-imenu-generic-re 1))
1016 "Expression for imenu")
1018 (defun LilyPond-command-select-master ()
1020 (message "Next command will be on the master file")
1021 (setq LilyPond-command-current 'LilyPond-command-master))
1023 (defun LilyPond-command-select-buffer ()
1025 (message "Next command will be on the buffer")
1026 (setq LilyPond-command-current 'LilyPond-command-buffer))
1028 (defun LilyPond-command-select-region ()
1030 (message "Next command will be on the region")
1031 (setq LilyPond-command-current 'LilyPond-command-region))
1033 (defun LilyPond-command-menu (name)
1034 ;; Execute LilyPond-command-alist NAME from a menu.
1035 (let ((LilyPond-command-force name))
1036 (if (eq LilyPond-command-current 'LilyPond-command-region)
1037 (if (eq (mark t) nil)
1038 (progn (message "The mark is not set now") (sit-for 0 500))
1039 (progn (if (not (not (LilyPond-mark-active)))
1040 (progn (message "Region is not active, using region between inactive mark and current point.") (sit-for 0 500)))
1041 (LilyPond-command-region (mark t) (point))))
1042 (funcall LilyPond-command-current))))
1044 (defun LilyPond-add-imenu-menu ()
1046 "Add an imenu menu to the menubar."
1047 (if (not LilyPond-imenu)
1049 (imenu-add-to-menubar "Index")
1050 (redraw-frame (selected-frame))
1051 (setq LilyPond-imenu t))
1052 (message "%s" "LilyPond-imenu already exists.")))
1053 (put 'LilyPond-add-imenu-menu 'menu-enable '(not LilyPond-imenu))
1055 (defun LilyPond-mode ()
1056 "Major mode for editing LilyPond music files.
1058 This mode knows about LilyPond keywords and line comments, not about
1059 indentation or block comments. It features easy compilation, error
1060 finding and viewing of a LilyPond source buffer or region.
1063 \\{LilyPond-mode-map}
1066 LilyPond-command-alist\t\talist from name to command
1067 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
1069 ;; set up local variables
1070 (kill-all-local-variables)
1072 (make-local-variable 'font-lock-defaults)
1073 (setq font-lock-defaults '(LilyPond-font-lock-keywords))
1075 ;; string and comments are fontified explicitly
1076 (make-local-variable 'font-lock-keywords-only)
1077 (setq font-lock-keywords-only t)
1079 ;; Multi-line font-locking needs Emacs 21.1 or newer.
1080 ;; For older versions there is hotkey "C-c f".
1081 (make-local-variable 'font-lock-multiline)
1082 (setq font-lock-multiline t)
1084 (make-local-variable 'paragraph-separate)
1085 (setq paragraph-separate "^[ \t]*$")
1087 (make-local-variable 'paragraph-start)
1088 (setq paragraph-start "^[ \t]*$")
1090 (make-local-variable 'comment-start)
1091 (setq comment-start "%")
1093 (make-local-variable 'comment-start-skip)
1094 (setq comment-start-skip "%{? *")
1096 (make-local-variable 'comment-end)
1097 (setq comment-end "")
1099 (make-local-variable 'block-comment-start)
1100 (setq block-comment-start "%{")
1102 (make-local-variable 'block-comment-end)
1103 (setq block-comment-end "%}")
1105 (make-local-variable 'indent-line-function)
1106 (setq indent-line-function 'LilyPond-indent-line)
1108 (set-syntax-table LilyPond-mode-syntax-table)
1109 (setq major-mode 'LilyPond-mode)
1110 (setq mode-name "LilyPond")
1111 (setq local-abbrev-table LilyPond-mode-abbrev-table)
1112 (use-local-map LilyPond-mode-map)
1114 ;; In Emacs blink-...-on-screen needs to be declared.
1115 (if (not (string-match "XEmacs\\|Lucid" emacs-version))
1117 (make-local-variable 'show-paren-mode)
1118 (setq show-paren-mode t)))
1120 (if (not (string-match "XEmacs\\|Lucid" emacs-version))
1122 (make-local-variable 'show-paren-function)
1123 ;;(setq show-paren-function 'LilyPond-show-paren-function) ; possible ?
1126 (make-local-variable 'paren-highlight)
1127 ;;(setq paren-highlight 'LilyPond-paren-highlight) ; possible ?
1130 ;; In XEmacs imenu was synched up with: FSF 20.4
1131 (make-local-variable 'imenu-generic-expression)
1132 (setq imenu-generic-expression LilyPond-imenu-generic-expression)
1133 ;; (imenu-add-to-menubar "Index") ; see LilyPond-add-imenu-menu
1135 ;; In XEmacs one needs to use 'easy-menu-add'.
1136 (if (string-match "XEmacs\\|Lucid" emacs-version)
1138 (easy-menu-add LilyPond-mode-menu)
1139 (easy-menu-add LilyPond-command-menu)))
1141 ;; Use Command on Region even for inactive mark (region).
1142 (if (string-match "XEmacs\\|Lucid" emacs-version)
1143 (setq zmacs-regions nil)
1144 (setq mark-even-if-inactive t))
1146 ;; run the mode hook. LilyPond-mode-hook use is deprecated
1147 (run-hooks 'LilyPond-mode-hook))
1149 (defun LilyPond-version ()
1150 "Echo the current version of `LilyPond-mode' in the minibuffer."
1152 (message "Using `LilyPond-mode' version %s" LilyPond-version))
1154 (load-library "lilypond-font-lock")
1155 (load-library "lilypond-indent")
1157 (provide 'lilypond-mode)
1158 ;;; lilypond-mode.el ends here