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.19"
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 (defcustom LilyPond-include-path ".:/tmp"
53 "* LilyPond include path."
57 (defun LilyPond-words-filename ()
58 "The file containing LilyPond \keywords \Identifiers and ReservedWords.
59 Finds file lilypond-words from load-path."
62 (words-file "lilypond.words"))
63 (while (and (> (length lp) 0) (not fn))
64 (setq fn (concat (car lp) "/" words-file))
65 (if (not (file-readable-p fn))
66 (progn (setq fn nil) (setq lp (cdr lp)))))
68 (progn (message "Warning: `lilypond.words' not found in `load-path'. See `lilypond-init.el'.")
72 (defun LilyPond-add-dictionary-word (x)
73 "Contains all words: \keywords \Identifiers and ReservedWords."
74 (nconc '(("" . 1)) x))
76 ;; creates dictionary if empty
77 (if (and (eq (length (LilyPond-add-dictionary-word ())) 1)
78 (not (eq (LilyPond-words-filename) nil)))
80 (setq b (find-file-noselect (LilyPond-words-filename) t t))
81 (setq m (set-marker (make-marker) 1 (get-buffer b)))
83 (while (> (buffer-size b) (marker-position m))
85 (setq copy (copy-alist (list (eval (symbol-name (read m))))))
87 (LilyPond-add-dictionary-word (list copy)))
90 (defconst LilyPond-keywords
92 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
94 (while (> (length co) 0)
95 (if (> (length (car co)) 1)
96 (if (and (string-equal "\\" (substring (car co) 0 1))
97 (string-equal (downcase (car co)) (car co)))
98 (add-to-list 'wordlist (car co))))
100 (if (eq (length wordlist) 0)
101 (setq wordlist '("\\score"))) ; add \keywords to lilypond.words
103 "LilyPond \\keywords")
105 (defconst LilyPond-identifiers
107 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
109 (while (> (length co) 0)
110 (if (> (length (car co)) 1)
111 (if (and (string-equal "\\" (substring (car co) 0 1))
112 (not (string-equal (downcase (car co)) (car co))))
113 (add-to-list 'wordlist (car co))))
115 (if (eq (length wordlist) 0)
116 (setq wordlist '("\\voiceOne"))) ; add \Identifiers to lilypond.words
118 "LilyPond \\Identifiers")
120 (defconst LilyPond-reserved-words
122 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
124 (while (> (length co) 0)
125 (if (> (length (car co)) 0)
126 (if (not (string-equal "\\" (substring (car co) 0 1)))
127 (add-to-list 'wordlist (car co))))
129 (if (eq (length wordlist) 0)
130 (setq wordlist '("Staff"))) ; add ReservedWords to lilypond.words
132 "LilyPond ReservedWords")
134 (defun LilyPond-check-files (derived originals extensions)
135 "Check that DERIVED is newer than any of the ORIGINALS.
136 Try each original with each member of EXTENSIONS, in all directories
137 in LilyPond-include-path."
139 (regexp (concat "\\`\\("
140 (mapconcat (function (lambda (dir)
141 (regexp-quote (expand-file-name dir))))
142 LilyPond-include-path "\\|")
144 (mapconcat 'regexp-quote originals "\\|")
146 (mapconcat 'regexp-quote extensions "\\|")
148 (buffers (buffer-list)))
150 (let* ((buffer (car buffers))
151 (name (buffer-file-name buffer)))
152 (setq buffers (cdr buffers))
153 (if (and name (string-match regexp name))
155 (and (buffer-modified-p buffer)
156 (or (not LilyPond-save-query)
157 (y-or-n-p (concat "Save file "
158 (buffer-file-name buffer)
160 (save-excursion (set-buffer buffer) (save-buffer)))
161 (if (file-newer-than-file-p name derived)
165 (defun LilyPond-running ()
166 "Check the currently running LilyPond compiling jobs."
167 (let ((process-names (list "lilypond" "tex" "2dvi" "2ps" "2midi"
170 (while (setq process-name (pop process-names))
171 (setq process (get-process process-name))
173 (eq (process-status process) 'run))
174 (push process-name running)))
175 running)) ; return the running jobs
177 (defun LilyPond-midi-running ()
178 "Check the currently running Midi processes."
179 (let ((process-names (list "midi" "midiall"))
181 (while (setq process-name (pop process-names))
182 (setq process (get-process process-name))
184 (eq (process-status process) 'run))
185 (push process-name running)))
186 running)) ; return the running jobs
188 (defun LilyPond-kill-jobs ()
189 "Kill the currently running LilyPond compiling jobs."
191 (let ((process-names (LilyPond-running))
193 (while (setq process-name (pop process-names))
194 (quit-process (get-process process-name) t)
195 (push process-name killed))
196 killed)) ; return the killed jobs
198 (defun LilyPond-kill-midi ()
199 "Kill the currently running midi processes."
200 (let ((process-names (LilyPond-midi-running))
202 (while (setq process-name (pop process-names))
203 (quit-process (get-process process-name) t)
204 (push process-name killed))
205 killed)) ; return the killed jobs
207 ;; URG, should only run LilyPond-compile for LilyPond
208 ;; not for tex,xdvi (ly2dvi?)
209 (defun LilyPond-compile-file (command name)
210 ;; We maybe should know what we run here (Lily, ly2dvi, tex)
211 ;; and adjust our error-matching regex ?
213 (if (eq LilyPond-command-current 'LilyPond-command-master)
215 ;; use temporary directory for Commands on Buffer/Region
216 ;; hm.. the directory is set twice, first to default-dir
217 (concat "cd " (LilyPond-temp-directory) "; " command))
218 "No more errors" name))
220 ;; do we still need this, now that we're using compile-internal?
221 (defun LilyPond-save-buffer ()
222 "Save buffer and set default command for compiling."
224 (if (buffer-modified-p)
226 (setq LilyPond-command-default "LilyPond"))))
228 ;;; return (dir base ext)
229 (defun split-file-name (name)
230 (let* ((i (string-match "[^/]*$" name))
231 (dir (if (> i 0) (substring name 0 i) "./"))
232 (file (substring name i (length name)))
233 (i (string-match "[^.]*$" file)))
237 (list dir (substring file 0 (- i 1)) (substring file i (length file)))
238 (list dir file ""))))
241 ;; Should check whether in command-alist?
242 (defcustom LilyPond-command-default "LilyPond"
243 "Default command. Must identify a member of LilyPond-command-alist."
247 ;;;(make-variable-buffer-local 'LilyPond-command-last)
249 (defvar LilyPond-command-current 'LilyPond-command-master)
250 ;;;(make-variable-buffer-local 'LilyPond-command-master)
253 ;; If non-nil, LilyPond-command-query will return the value of this
254 ;; variable instead of quering the user.
255 (defvar LilyPond-command-force nil)
257 (defcustom LilyPond-xdvi-command "xdvi"
258 "Command used to display DVI files."
263 (defcustom LilyPond-gv-command "gv -watch"
264 "Command used to display PS files."
269 (defcustom LilyPond-midi-command "timidity"
270 "Command used to play MIDI files."
275 (defcustom LilyPond-all-midi-command "timidity -ia"
276 "Command used to play MIDI files."
281 (defun LilyPond-command-current-midi ()
282 "Play midi corresponding to the current document."
284 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file))
286 (defun LilyPond-command-all-midi ()
287 "Play midi corresponding to the current document."
289 (LilyPond-command (LilyPond-command-menu "MidiAll") 'LilyPond-master-file))
291 (defun count-rexp (start end rexp)
292 "Print number of found regular expressions in the region."
296 (narrow-to-region start end)
297 (goto-char (point-min))
298 (count-matches rexp))))
300 (defun count-midi-words ()
301 "Check number of midi-scores before the curser."
302 (if (eq LilyPond-command-current 'LilyPond-command-region)
303 (count-rexp (mark t) (point) "\\\\midi")
304 (count-rexp (point-min) (point-max) "\\\\midi")))
306 (defun count-midi-words-backwards ()
307 "Check number of midi-scores before the curser."
308 (if (eq LilyPond-command-current 'LilyPond-command-region)
309 (count-rexp (mark t) (point) "\\\\midi")
310 (count-rexp (point-min) (point) "\\\\midi")))
312 (defun LilyPond-string-current-midi ()
313 "Check the midi file of the following midi-score in the current document."
314 (let ((fnameprefix (if (eq LilyPond-command-current 'LilyPond-command-master)
315 (substring (LilyPond-master-file) 0 -3); suppose ".ly"
316 LilyPond-region-file-prefix))
317 (allcount (string-to-number (substring (count-midi-words) 0 -12)))
318 (count (string-to-number (substring (count-midi-words-backwards) 0 -12))))
320 (if (and (> allcount 1) (> count 0)) ; not first score
321 (if (eq count allcount) ; last score
322 (concat "-" (number-to-string (+ count -1)))
323 (concat "-" (number-to-string count))))
326 (defun LilyPond-string-all-midi ()
327 "Return the midi files of the current document in ascending order."
328 (let ((fnameprefix (if (eq LilyPond-command-current 'LilyPond-command-master)
329 (substring (LilyPond-master-file) 0 -3); suppose ".ly"
330 LilyPond-region-file-prefix))
331 (allcount (string-to-number (substring (count-midi-words) 0 -12))))
332 (concat (if (> allcount 0) ; at least one midi-score
333 (concat fnameprefix ".midi "))
334 (if (> allcount 1) ; more than one midi-score
335 (concat fnameprefix "-[1-9].midi "))
336 (if (> allcount 9) ; etc.
337 (concat fnameprefix "-[1-9][0-9].midi"))
338 (if (> allcount 99) ; not first score
339 (concat fnameprefix "-[1-9][0-9][0-9].midi")))))
341 ;; This is the major configuration variable.
342 (defcustom LilyPond-command-alist
343 ;; Should expand this to include possible keyboard shortcuts which
344 ;; could then be mapped to define-key and menu.
346 ("LilyPond" . ("lilypond %s" . "LaTeX"))
347 ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
349 ("2Dvi" . ("ly2dvi %s" . "View"))
350 ("2PS" . ("ly2dvi -P %s" . "ViewPS"))
351 ("2Midi" . ("lilypond -m %s" . "View"))
353 ("Book" . ("lilypond-book %x" . "LaTeX"))
354 ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
356 ;; point-n-click (arg: exits upop USR1)
357 ("SmartView" . ("xdvi %d" . "LilyPond"))
359 ;; refreshes when kicked USR1
360 ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
361 ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
363 ;; The following are refreshed in LilyPond-command:
364 ;; - current-midi depends on cursor position and
365 ("Midi" . (,(concat LilyPond-midi-command " " (LilyPond-string-current-midi)) . "LilyPond" )) ;
366 ;; - all-midi depends on number of midi-score.
367 ("MidiAll" . (,(concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)) . "LilyPond"))
370 "AList of commands to execute on the current document.
372 The key is the name of the command as it will be presented to the
373 user, the value is a cons of the command string handed to the shell
374 after being expanded, and the next command to be executed upon
375 success. The expansion is done using the information found in
376 LilyPond-expand-list.
379 :type '(repeat (cons :tag "Command Item"
382 (string :tag "Command")
383 (string :tag "Next Key")))))
386 (defcustom LilyPond-file-extension ".ly"
387 "*File extension used in LilyPond sources."
392 (defcustom LilyPond-expand-alist
403 "Alist of expansion strings for LilyPond command names."
405 :type '(repeat (cons :tag "Alist item"
406 (string :tag "Symbol")
407 (string :tag "Expansion"))))
410 (defcustom LilyPond-command-Show "View"
411 "*The default command to show (view or print) a LilyPond file.
412 Must be the car of an entry in `LilyPond-command-alist'."
415 (make-variable-buffer-local 'LilyPond-command-Show)
417 (defcustom LilyPond-command-Print "Print"
418 "The name of the Print entry in LilyPond-command-Print."
422 (defun xLilyPond-compile-sentinel (process msg)
424 (= 0 (process-exit-status process)))
425 (setq LilyPond-command-default
426 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
428 ;; FIXME: shouldn't do this for stray View/xdvi
429 (defun LilyPond-compile-sentinel (buffer msg)
430 (if (string-match "^finished" msg)
431 (setq LilyPond-command-default
432 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
434 ;;(make-variable-buffer-local 'compilation-finish-function)
435 (setq compilation-finish-function 'LilyPond-compile-sentinel)
437 (defun LilyPond-command-query (name)
438 "Query the user for what LilyPond command to use."
439 (let* ((default (cond ((if (string-equal name LilyPond-region-file-prefix)
440 (LilyPond-check-files (concat name ".tex")
442 (list LilyPond-file-extension))
443 (if (verify-visited-file-modtime (current-buffer))
444 (if (buffer-modified-p)
445 (if (y-or-n-p "Save buffer before next command? ")
446 (LilyPond-save-buffer)))
447 (if (y-or-n-p "The command will be invoked to an already saved buffer. Revert it? ")
448 (revert-buffer t t)))
450 LilyPond-command-default))
451 (t LilyPond-command-default)))
453 (completion-ignore-case t)
455 (answer (or LilyPond-command-force
457 (concat "Command: (default " default ") ")
458 LilyPond-command-alist nil t nil 'LilyPond-command-history))))
460 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
461 (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
463 (not (string-equal answer "")))
467 (defun LilyPond-command-master ()
468 "Run command on the current document."
470 (LilyPond-command-select-master)
471 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
472 'LilyPond-master-file))
474 (defun LilyPond-command-lilypond ()
475 "Run lilypond for the current document."
477 (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file)
480 (defun LilyPond-command-formatdvi ()
481 "Format the dvi output of the current document."
483 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
486 (defun LilyPond-command-formatps ()
487 "Format the ps output of the current document."
489 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
492 (defun LilyPond-command-formatmidi ()
493 "Format the midi output of the current document."
495 (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-master-file)
498 (defun LilyPond-command-smartview ()
499 "View the dvi output of current document."
501 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
504 (defun LilyPond-command-view ()
505 "View the dvi output of current document."
507 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
510 (defun LilyPond-command-viewps ()
511 "View the ps output of current document."
513 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
516 ;;; Refer to syntax-table (in lilypond-font-lock.el) for comment characters.
517 (defun LilyPond-un-comment-region (start end level)
518 "Remove up to LEVEL comment characters from each line in the region."
519 (interactive "*r\np")
520 (comment-region start end (- level)))
522 ;; FIXME, this is broken
523 (defun LilyPond-region-file (begin end)
526 (dir (LilyPond-temp-directory))
527 (base LilyPond-region-file-prefix)
528 (ext LilyPond-file-extension))
529 (concat dir base ext)))
531 ;;; Commands on Region work if there is an appropriate '\score'.
532 (defun LilyPond-command-region (begin end)
533 "Run LilyPond on the current region."
535 (if (or (> begin (point-min)) (< end (point-max)))
536 (LilyPond-command-select-region))
537 (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
538 (LilyPond-command (LilyPond-command-query
539 (LilyPond-region-file begin end))
540 '(lambda () (LilyPond-region-file begin end)))
541 ;; Region may deactivate even if buffer was intact, reactivate?
542 ;; Currently, also deactived regions are used.
545 (defun LilyPond-command-buffer ()
546 "Run LilyPond on buffer."
548 (LilyPond-command-select-buffer)
549 (LilyPond-command-region (point-min) (point-max)))
551 (defun LilyPond-command-expand (string file)
552 (let ((case-fold-search nil))
553 (if (string-match "%" string)
554 (let* ((b (match-beginning 0))
556 (l (split-file-name file))
559 (LilyPond-command-expand
560 (concat (substring string 0 b)
563 (let ((entry (assoc (substring string b e)
564 LilyPond-expand-alist)))
565 (if entry (cdr entry) ""))
566 (substring string e))
570 (defun LilyPond-shell-process (name buffer command)
571 (let ((old (current-buffer)))
572 (switch-to-buffer-other-window buffer)
573 ;; If we empty the buffer don't see messages scroll by.
576 (start-process-shell-command name buffer command)
577 (switch-to-buffer-other-window old)))
580 (defun LilyPond-command (name file)
581 "Run command NAME on the file you get by calling FILE.
583 FILE is a function return a file name. It has one optional argument,
584 the extension to use on the file.
586 Use the information in LilyPond-command-alist to determine how to run the
589 (let ((entry (assoc name LilyPond-command-alist)))
591 (let ((command (LilyPond-command-expand (cadr entry)
594 (job-string "no jobs"))
595 (if (member name (list "View" "ViewPS"))
596 ;; is USR1 a right signal for viewps?
597 (let ((buffer-xdvi (get-buffer-create (concat "*" name "*"))))
598 (if LilyPond-kick-xdvi
599 (let ((process-xdvi (get-buffer-process buffer-xdvi)))
601 (signal-process (process-id process-xdvi) 'SIGUSR1)
602 (LilyPond-shell-process name buffer-xdvi command)))
603 (LilyPond-shell-process name buffer-xdvi command)))
605 (if (string-equal name "Midi")
607 (setq command (concat LilyPond-midi-command " " (LilyPond-string-current-midi)))
608 (if (LilyPond-kill-midi)
609 (setq job-string nil)))) ; either stop or start playing
610 (if (string-equal name "MidiAll")
612 (setq command (concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)))
613 (LilyPond-kill-midi))) ; stop and start playing
614 (if (and (member name (list "Midi" "MidiAll")) job-string)
615 (if (file-newer-than-file-p
616 (LilyPond-master-file)
617 (concat (substring (LilyPond-master-file) 0 -3) ".midi"))
618 (if (y-or-n-p "Midi older than source. Reformat midi?")
620 (LilyPond-command-formatmidi)
621 (while (LilyPond-running)
622 (message "Starts playing midi once it is built.")
624 (if (member name (list "LilyPond" "TeX" "2Midi" "2PS" "2Dvi"
626 (if (setq jobs (LilyPond-running))
628 (setq job-string "Process") ; could also suggest compiling after process has ended
630 (setq job-string (concat job-string " \"" (pop jobs) "\"")))
631 (setq job-string (concat job-string " is already running; kill it to proceed "))
632 (if (y-or-n-p job-string)
634 (setq job-string "no jobs")
636 (while (LilyPond-running)
638 (setq job-string nil)))))
640 (setq LilyPond-command-default name)
641 (if (string-equal job-string "no jobs")
642 (LilyPond-compile-file command name))))))))
644 (defun LilyPond-mark-active ()
645 "Check if there is an active mark."
646 (and transient-mark-mode
647 (if (string-match "XEmacs\\|Lucid" emacs-version) (mark) mark-active)))
649 (defun LilyPond-temp-directory ()
650 "Temporary file directory for Commands on Region."
652 (if (string-match "XEmacs\\|Lucid" emacs-version)
653 (concat (temp-directory) "/")
654 temporary-file-directory))
658 (defvar LilyPond-mode-map ()
659 "Keymap used in `LilyPond-mode' buffers.")
661 ;; Note: if you make changes to the map, you must do
662 ;; M-x set-variable LilyPond-mode-map nil
665 ;; to let the changest take effect
667 (if LilyPond-mode-map
669 (setq LilyPond-mode-map (make-sparse-keymap))
670 ;; Put keys to Lilypond-command-alist and fetch them from there somehow.
671 (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
672 (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
673 (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
674 (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-jobs)
675 (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
676 (define-key LilyPond-mode-map "\C-cm" 'LilyPond-command-formatmidi)
677 (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
678 (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
679 (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
680 (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
681 (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
682 (define-key LilyPond-mode-map [(control c) return] 'LilyPond-command-current-midi)
683 (define-key LilyPond-mode-map [(control c) (control return)] 'LilyPond-command-all-midi)
684 (define-key LilyPond-mode-map "\C-x\C-s" 'LilyPond-save-buffer)
685 (define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
686 (define-key LilyPond-mode-map "\C-ci" 'LilyPond-quick-note-insert)
687 (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
688 (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
689 (define-key LilyPond-mode-map "\C-c:" 'LilyPond-un-comment-region)
690 (define-key LilyPond-mode-map "\C-c;" 'comment-region)
691 (define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren)
692 (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren)
693 (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren)
694 (define-key LilyPond-mode-map "]" 'LilyPond-electric-close-paren)
695 (if (string-match "XEmacs\\|Lucid" emacs-version)
696 (define-key LilyPond-mode-map [iso-left-tab] 'LilyPond-autocompletion)
697 (define-key LilyPond-mode-map [iso-lefttab] 'LilyPond-autocompletion))
698 (define-key LilyPond-mode-map "\C-c\t" 'LilyPond-info-index-search)
703 (defun LilyPond-quick-note-insert()
704 "Insert notes with fewer key strokes by using a simple keyboard piano."
707 '(("k" "a") ("l" "b") ("a" "c") ("s" "d")
708 ("d" "e") ("f" "f") ("j" "g") ("r" "r")))
709 (setq dutch-note-ends '("eses" "es" "" "is" "isis"))
710 (setq dutch-note-replacements '("" ""))
711 (setq finnish-note-replacements
712 '(("eeses" "eses") ("ees" "es") ("aeses" "asas") ("aes" "as") ("b" "h")
713 ("beses" "heses") ("bes" "b") ("bis" "his") ("bisis" "hisis")))
714 ; add more translations of the note names
715 (setq spanish-note-replacements
716 '(("c" "do") ("d" "re") ("e" "mi") ("f" "fa") ("g" "sol") ("a" "la") ("b
718 ("cis" "dos") ("cisis" "doss") ("ces" "dob") ("ceses" "dobb")
719 ("dis" "res") ("disis" "ress") ("des" "reb") ("deses" "rebb")
720 ("eis" "mis") ("eisis" "miss") ("ees" "mib") ("eeses" "mibb")
721 ("fis" "fas") ("fisis" "fass") ("fes" "fab") ("feses" "fabb")
722 ("gis" "sols") ("gisis" "solss") ("ges" "solb") ("geses" "solbb")
723 ("ais" "las") ("aisis" "lass") ("aes" "lab") ("aeses" "labb")
724 ("bis" "sis") ("bisis" "siss") ("bes" "sib") ("beses" "sibb")))
725 (setq other-keys "(<~>)}|")
726 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)
728 (message "Press h for help.") (sit-for 0 750)
730 (setq note-replacements dutch-note-replacements)
731 (setq xkey 0) (setq xinitpoint (point))
732 (< xinitpoint (point))
733 (while (not (= xkey 27)) ; esc to quit
734 (message (format " a[]s[]d f[]j[]k[]l r with %4s%-4s%3s%-4s ie ,' 12345678 . 0 /%sb\\b\\n Esc"
735 (nth (+ accid 2) dutch-note-ends)
736 (make-string (abs octav) (if (> octav 0) ?' ?,))
738 (if (string= durat "") "" (make-string dots ?.))
740 (setq xkey (read-char-exclusive))
741 (setq x (char-to-string xkey))
742 (setq note (cdr (assoc x dutch-notes)))
744 ((string= x "q") (progn (setq xkey 27))) ; quit
745 ((= xkey 13) (progn (insert "\n") (LilyPond-indent-line))) ; return
746 ((or (= xkey 127) ; backspace is recognized as a char only in Emacs
747 (string= x "b")) ; so, we need to define an another char for XEmacs
748 (progn (narrow-to-region xinitpoint (point))
749 (backward-kill-word 1)
751 ((and (string< x "9") (string< "0" x))
752 (progn (setq durat (int-to-string (expt 2 (- (string-to-int x) 1))))
754 ((string= x " ") (insert " "))
755 ((string= x "/") (progn (insert "\\times ")
756 (message "Insert a number for the denominator (\"x/\")")
757 (while (not (and (string< x "9") (string< "0" x)))
758 (setq x (char-to-string (read-char-exclusive))))
759 (insert (format "%s/" x)) (setq x "/")
760 (message "Insert a number for the numerator (\"/y\")")
761 (while (not (and (string< x "9") (string< "0" x)))
762 (setq x (char-to-string (read-char-exclusive))))
763 (insert (format "%s { " x))))
764 ((string= x "0") (progn (setq accid 0) (setq octav 0)
765 (setq durat "") (setq dots 0)))
766 ((string= x "i") (setq accid (if (= accid 2) 0 (max (+ accid 1) 1))))
767 ((string= x "e") (setq accid (if (= accid -2) 0 (min (+ accid -1) -1))))
768 ((string= x "'") (setq octav (if (= octav 4) 0 (max (+ octav 1) 1))))
769 ((string= x ",") (setq octav (if (= octav -4) 0 (min (+ octav -1) -1))))
770 ((string= x ".") (setq dots (if (= dots 4) 0 (+ dots 1))))
771 ((not (null (member x (split-string other-keys ""))))
772 (insert (format "%s " x)))
776 (format "%s%s" (car note) (if (string= "r" (car note)) ""
777 (nth (+ accid 2) dutch-note-ends))))
778 (setq notetwo (car (cdr (assoc note note-replacements))))
779 (if (not (null notetwo)) (setq note notetwo))
783 (if (string= "r" note) ""
784 (make-string (abs octav) (if (> octav 0) ?' ?,)))
786 (if (string= durat "") "" (make-string dots ?.))))
787 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)))
788 ((string= x "t") (progn (setq note-replacements dutch-note-replacements)
789 (message "Selected Dutch notes")
791 ((string= x "n") (progn (setq note-replacements finnish-note-replacements)
792 (message "Selected Finnish/Deutsch notes")
794 ; add more translations of the note names
795 ((string= x "p") (progn (setq note-replacements spanish-note-replacements)
796 (message "Selected Spanish notes")
799 (progn (message "Insert notes with fewer key strokes. For example \"i,5.f\" produces \"fis,32. \".") (sit-for 5 0)
800 (message "Add also \"a ~ a\"-ties, \"a ( ) b\"-slurs and \"< a b >\"-chords.") (sit-for 5 0)
801 (message "There are Du(t)ch, Fin(n)ish/Deutsch (hit 'n') and S(p)anish note names.") (sit-for 5 0)
802 (message "(B)ackspace deletes last note, Ret starts a new indented line and Esc (q)uits.") (sit-for 5 0)
803 (message "Insert note triplets \"\\times 2/3 { a b } \" by typing \"/23ab}\".") (sit-for 5 0)
804 (message "This mode is experimental. Use normal mode to add further details.") (sit-for 5 0)))
805 (t (progn (message "Quick notes mode. Press Esc or q to quit.") (sit-for 0 500)))))
806 (message "Normal editing mode."))
808 (defun LilyPond-pre-word-search ()
809 "Fetch the alphabetic characters and \\ in front of the cursor."
812 (ch (char-before (- (point) 0))))
813 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not bolp, A-Z
814 (and (>= ch 97) (<= ch 122)) ; a-z
816 (setq pre (concat (char-to-string ch) pre))
817 (setq prelen (+ prelen 1))
818 (setq ch (char-before (- (point) prelen))))
821 (defun LilyPond-post-word-search ()
822 "Fetch the alphabetic characters behind the cursor."
825 (ch (char-after (+ (point) 0))))
826 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not eolp, A-Z
827 (and (>= ch 97) (<= ch 122)))) ; a-z
828 (setq post (concat post (char-to-string ch)))
829 (setq postlen (+ postlen 1))
830 (setq ch (char-after (+ (point) postlen))))
833 (defun LilyPond-autocompletion ()
834 "Show completions in mini-buffer for the given word."
836 (let ((pre (LilyPond-pre-word-search))
837 (post (LilyPond-post-word-search))
839 ;; insert try-completion and show all-completions
840 (if (> (length pre) 0)
842 (setq trycomp (try-completion pre (LilyPond-add-dictionary-word ())))
843 (if (char-or-string-p trycomp)
844 (if (string-equal (concat pre post) trycomp)
845 (goto-char (+ (point) (length post)))
847 (delete-region (point) (+ (point) (length post)))
848 (insert (substring trycomp (length pre) nil))))
850 (delete-region (point) (+ (point) (length post)))
851 (font-lock-fontify-buffer))) ; only inserting fontifies
853 (setq complist (all-completions pre (LilyPond-add-dictionary-word ())))
854 (while (> (length complist) 0)
855 (setq compsstr (concat compsstr "\"" (car complist) "\" "))
856 (setq complist (cdr complist)))
860 (defun LilyPond-info ()
861 "Launch Info for lilypond."
865 (defun LilyPond-music-glossary-info ()
866 "Launch Info for music-glossary."
868 (info "music-glossary"))
870 (defun LilyPond-internals-info ()
871 "Launch Info for lilypond-internals."
873 (info "lilypond-internals"))
875 (defun LilyPond-info-index-search ()
876 "In `*info*'-buffer, launch `info lilypond --index-search word-under-cursor'"
878 (let ((str (concat (LilyPond-pre-word-search) (LilyPond-post-word-search))))
879 (if (and (> (length str) 0)
880 (string-equal (substring str 0 1) "\\"))
881 (setq str (substring str 1 nil)))
885 (defun LilyPond-insert-string (pre)
886 "Insert text to the buffer."
890 (defun LilyPond-insert-between (text pre post)
891 "Insert text to the buffer if non-empty string is given."
892 (let ((str (read-string text)))
893 (if (string-equal str "")
895 (progn (setq pre_str_post (concat pre str post))
896 (insert pre_str_post)
897 (length pre_str_post)))))
899 (defun LilyPond-insert-tag-notes ()
900 "LilyPond notes tag."
902 (setq begin (if (LilyPond-mark-active)
903 (mark-marker) (point-marker)))
904 (setq end (point-marker))
906 (setq l1 (LilyPond-insert-string "\\notes "))
907 (setq l2 (LilyPond-insert-between "Relative (e.g. c'): " "\\relative " " "))
909 (setq l2 (LilyPond-insert-between "Transpose (e.g. c c'): " "\\transpose " " ")))
910 (setq l3 (LilyPond-insert-string "{ "))
911 (goto-char (+ end l1 l2 l3))
912 (LilyPond-insert-string " }")
913 (goto-char (+ end l1 l2 l3)))
915 (defun LilyPond-insert-tag-score ()
916 "LilyPond score tag."
918 (setq begin (if (LilyPond-mark-active)
919 (mark-marker) (point-marker)))
920 (setq end (point-marker))
922 (setq l1 (LilyPond-insert-string "\\score {\n ")) ; keep track of lengths
923 (goto-char (+ end l1))
924 (LilyPond-insert-string "\n \\paper { }\n")
925 (setq l2 (if (y-or-n-p "Insert \"\\header\" field? ")
926 (+ (LilyPond-insert-string " \\header {")
927 (LilyPond-insert-between "Title: " "\n title = \"" "\"")
928 (LilyPond-insert-between "Subtitle: " "\n subtitle = \"" "\"")
929 (LilyPond-insert-between "Piece: " "\n piece = \"" "\"")
930 (LilyPond-insert-between "Opus: " "\n opus = \"" "\"")
931 (LilyPond-insert-string "\n }\n"))
933 (setq l3 (if (y-or-n-p "Insert \"\\midi\" field? ")
934 (+ (LilyPond-insert-string " \\midi {")
935 (LilyPond-insert-between "Tempo: (e.g. 4=60) " " \\tempo " "")
936 (LilyPond-insert-string " }\n"))
938 (setq l4 (LilyPond-insert-string "}\n"))
939 (goto-char (+ end l1)))
941 (defun LilyPond-command-menu-entry (entry)
942 ;; Return LilyPond-command-alist ENTRY as a menu item.
943 (let ((name (car entry)))
944 (cond ((and (string-equal name LilyPond-command-Print)
945 LilyPond-printer-list)
946 (let ((command LilyPond-print-command)
948 (append (list LilyPond-command-Print)
949 (mapcar 'LilyPond-command-menu-printer-entry
950 LilyPond-printer-list))))
952 (vector name (list 'LilyPond-command-menu name) t)))))
955 (easy-menu-define LilyPond-command-menu
957 "Menu used in LilyPond mode."
960 [ "Master File" LilyPond-command-select-master
961 :keys "C-c C-c" :style radio
962 :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
963 [ "Buffer" LilyPond-command-select-buffer
964 :keys "C-c C-b" :style radio
965 :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
966 [ "Region" LilyPond-command-select-region
967 :keys "C-c C-r" :style radio
968 :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
969 ;;; (let ((file 'LilyPond-command-on-current))
970 ;;; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
971 ;;; Some kind of mapping which includes :keys might be more elegant
972 ;;; Put keys to Lilypond-command-alist and fetch them from there somehow.
973 '([ "LilyPond" LilyPond-command-lilypond t])
974 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
975 '([ "2Dvi" LilyPond-command-formatdvi t])
976 '([ "2PS" LilyPond-command-formatps t])
977 '([ "2Midi" LilyPond-command-formatmidi t])
978 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
979 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
980 '([ "Kill jobs" LilyPond-kill-jobs t])
982 '([ "SmartView" LilyPond-command-smartview t])
983 '([ "View" LilyPond-command-view t])
984 '([ "ViewPS" LilyPond-command-viewps t])
986 '([ "Midi (toggle)" LilyPond-command-current-midi t])
987 '([ "Midi all" LilyPond-command-all-midi t])
990 ;;; LilyPond-mode-menu should not be interactive, via "M-x LilyPond-<Tab>"
991 (easy-menu-define LilyPond-mode-menu
993 "Menu used in LilyPond mode."
994 (append '("LilyPond")
996 [ "\\notes..." LilyPond-insert-tag-notes t]
997 [ "\\score..." LilyPond-insert-tag-score t]
998 ["Quick Notes" LilyPond-quick-note-insert t]
999 ["Autocompletion" LilyPond-autocompletion t]
1002 ["Uncomment Region" LilyPond-un-comment-region t]
1003 ["Comment Region" comment-region t]
1004 ["Refontify buffer" font-lock-fontify-buffer t]
1007 ["LilyPond" LilyPond-info t]
1008 ["LilyPond index-search" LilyPond-info-index-search t]
1009 ["Music Glossary" LilyPond-music-glossary-info t]
1010 ["LilyPond internals" LilyPond-internals-info t]
1014 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z]+\\) *="
1015 "Regexp matching Identifier definitions.")
1017 (defvar LilyPond-imenu-generic-expression
1018 (list (list nil LilyPond-imenu-generic-re 1))
1019 "Expression for imenu")
1021 (defun LilyPond-command-select-master ()
1023 (message "Next command will be on the master file")
1024 (setq LilyPond-command-current 'LilyPond-command-master))
1026 (defun LilyPond-command-select-buffer ()
1028 (message "Next command will be on the buffer")
1029 (setq LilyPond-command-current 'LilyPond-command-buffer))
1031 (defun LilyPond-command-select-region ()
1033 (message "Next command will be on the region")
1034 (setq LilyPond-command-current 'LilyPond-command-region))
1036 (defun LilyPond-command-menu (name)
1037 ;; Execute LilyPond-command-alist NAME from a menu.
1038 (let ((LilyPond-command-force name))
1039 (if (eq LilyPond-command-current 'LilyPond-command-region)
1040 (if (eq (mark t) nil)
1041 (progn (message "The mark is not set now") (sit-for 0 500))
1042 (progn (if (not (not (LilyPond-mark-active)))
1043 (progn (message "Region is not active, using region between inactive mark and current point.") (sit-for 0 500)))
1044 (LilyPond-command-region (mark t) (point))))
1045 (funcall LilyPond-command-current))))
1047 (defun LilyPond-mode ()
1048 "Major mode for editing LilyPond music files.
1050 This mode knows about LilyPond keywords and line comments, not about
1051 indentation or block comments. It features easy compilation, error
1052 finding and viewing of a LilyPond source buffer or region.
1055 \\{LilyPond-mode-map}
1058 LilyPond-command-alist\t\talist from name to command
1059 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
1061 ;; set up local variables
1062 (kill-all-local-variables)
1064 (make-local-variable 'font-lock-defaults)
1065 (setq font-lock-defaults '(LilyPond-font-lock-keywords))
1067 ;; string and comments are fontified explicitly
1068 (make-local-variable 'font-lock-keywords-only)
1069 (setq font-lock-keywords-only t)
1071 ;; Multi-line font-locking needs Emacs 21.1 or newer.
1072 ;; For older versions there is hotkey "C-c f".
1073 (make-local-variable 'font-lock-multiline)
1074 (setq font-lock-multiline t)
1076 (make-local-variable 'paragraph-separate)
1077 (setq paragraph-separate "^[ \t]*$")
1079 (make-local-variable 'paragraph-start)
1080 (setq paragraph-start "^[ \t]*$")
1082 (make-local-variable 'comment-start)
1083 (setq comment-start "%")
1085 (make-local-variable 'comment-start-skip)
1086 (setq comment-start-skip "%{? *")
1088 (make-local-variable 'comment-end)
1089 (setq comment-end "")
1091 (make-local-variable 'block-comment-start)
1092 (setq block-comment-start "%{")
1094 (make-local-variable 'block-comment-end)
1095 (setq block-comment-end "%}")
1097 (make-local-variable 'indent-line-function)
1098 (setq indent-line-function 'LilyPond-indent-line)
1100 (set-syntax-table LilyPond-mode-syntax-table)
1101 (setq major-mode 'LilyPond-mode)
1102 (setq mode-name "LilyPond")
1103 (setq local-abbrev-table LilyPond-mode-abbrev-table)
1104 (use-local-map LilyPond-mode-map)
1106 ;; In Emacs blink-...-on-screen needs to be declared.
1107 (if (not (string-match "XEmacs\\|Lucid" emacs-version))
1109 (make-local-variable 'blink-matching-paren-on-screen)
1110 (setq blink-matching-paren-on-screen t)))
1112 ;; In XEmacs imenu was synched up with: FSF 20.4
1113 (make-local-variable 'imenu-generic-expression)
1114 (setq imenu-generic-expression LilyPond-imenu-generic-expression)
1115 (imenu-add-to-menubar "Index")
1117 ;; In XEmacs one needs to use 'easy-menu-add'.
1118 (if (string-match "XEmacs\\|Lucid" emacs-version)
1120 (easy-menu-add LilyPond-mode-menu)
1121 (easy-menu-add LilyPond-command-menu)))
1123 ;; Use Command on Region even for inactive mark (region).
1124 (if (string-match "XEmacs\\|Lucid" emacs-version)
1125 (setq zmacs-regions nil)
1126 (setq mark-even-if-inactive t))
1128 ;; run the mode hook. LilyPond-mode-hook use is deprecated
1129 (run-hooks 'LilyPond-mode-hook))
1131 (defun LilyPond-version ()
1132 "Echo the current version of `LilyPond-mode' in the minibuffer."
1134 (message "Using `LilyPond-mode' version %s" LilyPond-version))
1136 (load-library "lilypond-font-lock")
1137 (load-library "lilypond-indent")
1139 (provide 'lilypond-mode)
1140 ;;; lilypond-mode.el ends here