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.18"
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 ;; FIXME: find ``\score'' in buffers / make settable?
35 (defun LilyPond-master-file ()
39 (defvar LilyPond-kick-xdvi nil
40 "If true, no simultaneous xdvi's are started, but reload signal is sent.")
42 (defvar LilyPond-command-history nil
43 "Command history list.")
45 (defvar LilyPond-regexp-alist
46 '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
47 "Regexp used to match LilyPond errors. See `compilation-error-regexp-alist'.")
49 (defcustom LilyPond-include-path ".:/tmp"
50 "* LilyPond include path."
54 (defun LilyPond-words-filename ()
55 "The file containing LilyPond \keywords \Identifiers and ReservedWords.
56 Finds file lilypond-words from load-path."
59 (words-file "lilypond.words"))
60 (while (and (> (length lp) 0) (not fn))
61 (setq fn (concat (car lp) "/" words-file))
62 (if (not (file-readable-p fn))
63 (progn (setq fn nil) (setq lp (cdr lp)))))
65 (progn (message "Error (shown for 12 seconds; press a key to skip):\n\n File `lilypond.words' containing keywords to be autocompleted and fontified,\n was not found. Place it to your `load-path' (see `lilypond-init.el').\n")
69 (defun LilyPond-add-dictionary-word (x)
70 "Contains all words: \keywords \Identifiers and ReservedWords."
71 (nconc '(("" . 1)) x))
73 ;; creates dictionary if empty
74 (if (and (eq (length (LilyPond-add-dictionary-word ())) 1)
75 (not (eq (LilyPond-words-filename) nil)))
77 (setq b (find-file-noselect (LilyPond-words-filename) t t))
78 (setq m (set-marker (make-marker) 1 (get-buffer b)))
80 (while (> (buffer-size b) (marker-position m))
82 (setq copy (copy-alist (list (eval (symbol-name (read m))))))
84 (LilyPond-add-dictionary-word (list copy)))
87 (defconst LilyPond-keywords
89 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
91 (while (> (length co) 0)
92 (if (> (length (car co)) 1)
93 (if (and (string-equal "\\" (substring (car co) 0 1))
94 (string-equal (downcase (car co)) (car co)))
95 (add-to-list 'wordlist (car co))))
97 (if (eq (length wordlist) 0)
98 (setq wordlist '("\\score"))) ; add \keywords to lilypond.words
100 "LilyPond \\keywords")
102 (defconst LilyPond-identifiers
104 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
106 (while (> (length co) 0)
107 (if (> (length (car co)) 1)
108 (if (and (string-equal "\\" (substring (car co) 0 1))
109 (not (string-equal (downcase (car co)) (car co))))
110 (add-to-list 'wordlist (car co))))
112 (if (eq (length wordlist) 0)
113 (setq wordlist '("\\voiceOne"))) ; add \Identifiers to lilypond.words
115 "LilyPond \\Identifiers")
117 (defconst LilyPond-reserved-words
119 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
121 (while (> (length co) 0)
122 (if (> (length (car co)) 0)
123 (if (not (string-equal "\\" (substring (car co) 0 1)))
124 (add-to-list 'wordlist (car co))))
126 (if (eq (length wordlist) 0)
127 (setq wordlist '("Staff"))) ; add ReservedWords to lilypond.words
129 "LilyPond ReservedWords")
131 (defun LilyPond-check-files (derived originals extensions)
132 "Check that DERIVED is newer than any of the ORIGINALS.
133 Try each original with each member of EXTENSIONS, in all directories
134 in LilyPond-include-path."
136 (regexp (concat "\\`\\("
137 (mapconcat (function (lambda (dir)
138 (regexp-quote (expand-file-name dir))))
139 LilyPond-include-path "\\|")
141 (mapconcat 'regexp-quote originals "\\|")
143 (mapconcat 'regexp-quote extensions "\\|")
145 (buffers (buffer-list)))
147 (let* ((buffer (car buffers))
148 (name (buffer-file-name buffer)))
149 (setq buffers (cdr buffers))
150 (if (and name (string-match regexp name))
152 (and (buffer-modified-p buffer)
153 (or (not LilyPond-save-query)
154 (y-or-n-p (concat "Save file "
155 (buffer-file-name buffer)
157 (save-excursion (set-buffer buffer) (save-buffer)))
158 (if (file-newer-than-file-p name derived)
162 (defun LilyPond-running ()
163 "Check the currently running LilyPond compiling jobs."
165 (let ((process-names (list "lilypond" "tex" "2dvi" "2ps" "2midi"
168 (while (setq process-name (pop process-names))
169 (setq process (get-process process-name))
171 (eq (process-status process) 'run))
172 (push process-name running)))
173 running)) ; return the running jobs
175 (defun LilyPond-midi-running ()
176 "Check the currently running Midi processes."
178 (let ((process-names (list "midi" "midiall"))
180 (while (setq process-name (pop process-names))
181 (setq process (get-process process-name))
183 (eq (process-status process) 'run))
184 (push process-name running)))
185 running)) ; return the running jobs
187 (defun LilyPond-kill-jobs ()
188 "Kill the currently running LilyPond compiling jobs."
190 (let ((process-names (LilyPond-running))
192 (while (setq process-name (pop process-names))
193 (quit-process (get-process process-name) t)
194 (push process-name killed))
195 killed)) ; return the killed jobs
197 (defun LilyPond-kill-midi ()
198 "Kill the currently running midi processes."
199 (let ((process-names (LilyPond-midi-running))
201 (while (setq process-name (pop process-names))
202 (quit-process (get-process process-name) t)
203 (push process-name killed))
204 killed)) ; return the killed jobs
206 ;; URG, should only run LilyPond-compile for LilyPond
207 ;; not for tex,xdvi (ly2dvi?)
208 (defun LilyPond-compile-file (command name)
209 ;; We maybe should know what we run here (Lily, ly2dvi, tex)
210 ;; and adjust our error-matching regex ?
211 (compile-internal command "No more errors" name ))
213 ;; do we still need this, now that we're using compile-internal?
214 (defun LilyPond-save-buffer ()
215 "Save buffer and set default command for compiling."
217 (if (buffer-modified-p)
219 (setq LilyPond-command-default "LilyPond"))))
221 ;;; return (dir base ext)
222 (defun split-file-name (name)
223 (let* ((i (string-match "[^/]*$" name))
224 (dir (if (> i 0) (substring name 0 i) "./"))
225 (file (substring name i (length name)))
226 (i (string-match "[^.]*$" file)))
230 (list dir (substring file 0 (- i 1)) (substring file i (length file)))
231 (list dir file ""))))
234 ;; Should check whether in command-alist?
235 (defcustom LilyPond-command-default "LilyPond"
236 "Default command. Must identify a member of LilyPond-command-alist."
240 ;;;(make-variable-buffer-local 'LilyPond-command-last)
242 (defvar LilyPond-command-current 'LilyPond-command-master)
243 ;;;(make-variable-buffer-local 'LilyPond-command-master)
246 ;; If non-nil, LilyPond-command-query will return the value of this
247 ;; variable instead of quering the user.
248 (defvar LilyPond-command-force nil)
250 (defcustom LilyPond-xdvi-command "xdvi"
251 "Command used to display DVI files."
256 (defcustom LilyPond-gv-command "gv -watch"
257 "Command used to display PS files."
262 (defcustom LilyPond-midi-command "timidity"
263 "Command used to play MIDI files."
268 (defcustom LilyPond-all-midi-command "timidity -ia"
269 "Command used to play MIDI files."
274 (defun LilyPond-command-current-midi ()
275 "Play midi corresponding to the current document."
277 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file))
279 (defun LilyPond-command-all-midi ()
280 "Play midi corresponding to the current document."
282 (LilyPond-command (LilyPond-command-menu "MidiAll") 'LilyPond-master-file))
284 (defun count-rexp (start end rexp)
285 "Print number of found regular expressions in the region."
289 (narrow-to-region start end)
290 (goto-char (point-min))
291 (count-matches rexp))))
293 (defun count-midi-words ()
294 "Check number of midi-scores before the curser."
296 (count-rexp (point-min) (point-max) "\\\\midi"))
298 (defun count-midi-words-backwards ()
299 "Check number of midi-scores before the curser."
301 (count-rexp (point-min) (point) "\\\\midi"))
303 (defun LilyPond-string-current-midi ()
304 "Check the midi file of the following midi-score in the current document."
306 (let ((fnameprefix (substring (LilyPond-master-file) 0 -3)) ; suppose ".ly"
307 (allcount (string-to-number (substring (count-midi-words) 0 -12)))
308 (count (string-to-number (substring (count-midi-words-backwards) 0 -12))))
310 (if (and (> allcount 1) (> count 0)) ; not first score
311 (if (eq count allcount) ; last score
312 (concat "-" (number-to-string (+ count -1)))
313 (concat "-" (number-to-string count))))
316 (defun LilyPond-string-all-midi ()
317 "Return the midi files of the current document in ascending order."
319 (let ((fnameprefix (substring (LilyPond-master-file) 0 -3))
320 (allcount (string-to-number (substring (count-midi-words) 0 -12))))
321 (concat (if (> allcount 0) ; at least one midi-score
322 (concat fnameprefix ".midi "))
323 (if (> allcount 1) ; more than one midi-score
324 (concat fnameprefix "-[1-9].midi "))
325 (if (> allcount 9) ; etc.
326 (concat fnameprefix "-[1-9][0-9].midi"))
327 (if (> allcount 99) ; not first score
328 (concat fnameprefix "-[1-9][0-9][0-9].midi")))))
330 ;; This is the major configuration variable.
331 (defcustom LilyPond-command-alist
332 ;; Should expand this to include possible keyboard shortcuts which
333 ;; could then be mapped to define-key and menu.
335 ("LilyPond" . ("lilypond %s" . "LaTeX"))
336 ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
338 ("2Dvi" . ("ly2dvi %s" . "View"))
339 ("2PS" . ("ly2dvi -P %s" . "ViewPS"))
340 ("2Midi" . ("lilypond -m %s" . "View"))
342 ("Book" . ("lilypond-book %x" . "LaTeX"))
343 ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
345 ;; point-n-click (arg: exits upop USR1)
346 ("SmartView" . ("xdvi %d" . "LilyPond"))
348 ;; refreshes when kicked USR1
349 ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
350 ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
352 ;; The following are refreshed in LilyPond-command:
353 ;; - current-midi depends on cursor position and
354 ("Midi" . (,(concat LilyPond-midi-command " " (LilyPond-string-current-midi)) . "LilyPond" )) ;
355 ;; - all-midi depends on number of midi-score.
356 ("MidiAll" . (,(concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)) . "LilyPond"))
359 "AList of commands to execute on the current document.
361 The key is the name of the command as it will be presented to the
362 user, the value is a cons of the command string handed to the shell
363 after being expanded, and the next command to be executed upon
364 success. The expansion is done using the information found in
365 LilyPond-expand-list.
368 :type '(repeat (cons :tag "Command Item"
371 (string :tag "Command")
372 (string :tag "Next Key")))))
375 (defcustom LilyPond-file-extensions '(".ly" ".sly" ".fly")
376 "*File extensions used by manually generated TeX files."
378 :type '(repeat (string :format "%v")))
381 (defcustom LilyPond-expand-alist
392 "Alist of expansion strings for LilyPond command names."
394 :type '(repeat (cons :tag "Alist item"
395 (string :tag "Symbol")
396 (string :tag "Expansion"))))
399 (defcustom LilyPond-command-Show "View"
400 "*The default command to show (view or print) a LilyPond file.
401 Must be the car of an entry in `LilyPond-command-alist'."
404 (make-variable-buffer-local 'LilyPond-command-Show)
406 (defcustom LilyPond-command-Print "Print"
407 "The name of the Print entry in LilyPond-command-Print."
411 (defun xLilyPond-compile-sentinel (process msg)
413 (= 0 (process-exit-status process)))
414 (setq LilyPond-command-default
415 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
417 ;; FIXME: shouldn't do this for stray View/xdvi
418 (defun LilyPond-compile-sentinel (buffer msg)
419 (if (string-match "^finished" msg)
420 (setq LilyPond-command-default
421 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
423 ;;(make-variable-buffer-local 'compilation-finish-function)
424 (setq compilation-finish-function 'LilyPond-compile-sentinel)
426 (defun LilyPond-command-query (name)
427 "Query the user for what LilyPond command to use."
428 (let* ((default (cond ((if (string-equal name "emacs-lily")
429 (LilyPond-check-files (concat name ".tex")
431 LilyPond-file-extensions)
432 (if (verify-visited-file-modtime (current-buffer))
433 (if (buffer-modified-p)
434 (if (y-or-n-p "Save buffer before next command? ")
435 (LilyPond-save-buffer)))
436 (if (y-or-n-p "The command will be invoked to an already saved buffer. Revert it? ")
437 (revert-buffer t t)))
439 LilyPond-command-default))
440 (t LilyPond-command-default)))
442 (completion-ignore-case t)
444 (answer (or LilyPond-command-force
446 (concat "Command: (default " default ") ")
447 LilyPond-command-alist nil t nil 'LilyPond-command-history))))
449 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
450 (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
452 (not (string-equal answer "")))
456 (defun LilyPond-command-master ()
457 "Run command on the current document."
459 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
460 'LilyPond-master-file))
462 (defun LilyPond-command-lilypond ()
463 "Run lilypond for the current document."
465 (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file)
468 (defun LilyPond-command-formatdvi ()
469 "Format the dvi output of the current document."
471 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
474 (defun LilyPond-command-formatps ()
475 "Format the ps output of the current document."
477 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
480 (defun LilyPond-command-formatmidi ()
481 "Format the midi output of the current document."
483 (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-master-file)
486 (defun LilyPond-command-smartview ()
487 "View the dvi output of current document."
489 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
492 (defun LilyPond-command-view ()
493 "View the dvi output of current document."
495 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
498 (defun LilyPond-command-viewps ()
499 "View the ps output of current document."
501 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
504 ;;; Refer to syntax-table (in lilypond-font-lock.el) for comment characters.
505 (defun LilyPond-un-comment-region (start end level)
506 "Remove up to LEVEL comment characters from each line in the region."
507 (interactive "*r\np")
508 (comment-region start end (- level)))
510 ;; FIXME, this is broken
511 (defun LilyPond-region-file (begin end)
518 (ext (if (string-match "^[\\]score" (buffer-substring begin end))
520 (if (< 50 (abs (- begin end)))
523 (concat dir base ext)))
525 (defun LilyPond-command-region (begin end)
526 "Run LilyPond on the current region."
528 (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
529 (LilyPond-command (LilyPond-command-query
530 (LilyPond-region-file begin end))
531 '(lambda () (LilyPond-region-file begin end))))
533 (defun LilyPond-command-buffer ()
534 "Run LilyPond on buffer."
536 (LilyPond-command-region (point-min) (point-max)))
538 (defun LilyPond-command-expand (string file)
539 (let ((case-fold-search nil))
540 (if (string-match "%" string)
541 (let* ((b (match-beginning 0))
543 (l (split-file-name file))
546 (LilyPond-command-expand
547 (concat (substring string 0 b)
550 (let ((entry (assoc (substring string b e)
551 LilyPond-expand-alist)))
552 (if entry (cdr entry) ""))
553 (substring string e))
557 (defun LilyPond-shell-process (name buffer command)
558 (let ((old (current-buffer)))
559 (switch-to-buffer-other-window buffer)
560 ;; If we empty the buffer don't see messages scroll by.
563 (start-process-shell-command name buffer command)
564 (switch-to-buffer-other-window old)))
567 (defun LilyPond-command (name file)
568 "Run command NAME on the file you get by calling FILE.
570 FILE is a function return a file name. It has one optional argument,
571 the extension to use on the file.
573 Use the information in LilyPond-command-alist to determine how to run the
576 (let ((entry (assoc name LilyPond-command-alist)))
578 (let ((command (LilyPond-command-expand (cadr entry)
581 (job-string "no jobs"))
582 (if (member name (list "View" "ViewPS"))
583 ;; is USR1 a right signal for viewps?
584 (let ((buffer-xdvi (get-buffer-create (concat "*" name "*"))))
585 (if LilyPond-kick-xdvi
586 (let ((process-xdvi (get-buffer-process buffer-xdvi)))
588 (signal-process (process-id process-xdvi) 'SIGUSR1)
589 (LilyPond-shell-process name buffer-xdvi command)))
590 (LilyPond-shell-process name buffer-xdvi command)))
592 (if (string-equal name "Midi")
594 (setq command (concat LilyPond-midi-command " " (LilyPond-string-current-midi)))
595 (if (LilyPond-kill-midi)
596 (setq job-string nil)))) ; either stop or start playing
597 (if (string-equal name "MidiAll")
599 (setq command (concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)))
600 (LilyPond-kill-midi))) ; stop and start playing
601 (if (and (member name (list "Midi" "MidiAll")) job-string)
602 (if (file-newer-than-file-p
603 (LilyPond-master-file)
604 (concat (substring (LilyPond-master-file) 0 -3) ".midi"))
605 (if (y-or-n-p "Midi older than source. Reformat midi?")
607 (LilyPond-command-formatmidi)
608 (while (LilyPond-running)
609 (message "Starts playing midi once it is built.")
611 (if (member name (list "LilyPond" "TeX" "2Midi" "2PS" "2Dvi"
613 (if (setq jobs (LilyPond-running))
615 (setq job-string "Process") ; could also suggest compiling after process has ended
617 (setq job-string (concat job-string " \"" (pop jobs) "\"")))
618 (setq job-string (concat job-string " is already running; kill it to proceed "))
619 (if (y-or-n-p job-string)
621 (setq job-string "no jobs")
623 (while (LilyPond-running)
625 (setq job-string nil)))))
627 (setq LilyPond-command-default name)
628 (if (string-equal job-string "no jobs")
629 (LilyPond-compile-file command name))))))))
632 ;; Sadly we need this for a macro in Emacs 19.
634 ;; Imenu isn't used in XEmacs, so just ignore load errors.
642 (defvar LilyPond-mode-map ()
643 "Keymap used in `LilyPond-mode' buffers.")
645 ;; Note: if you make changes to the map, you must do
646 ;; M-x set-variable LilyPond-mode-map nil
649 ;; to let the changest take effect
651 (if LilyPond-mode-map
653 (setq LilyPond-mode-map (make-sparse-keymap))
654 ;; Put keys to Lilypond-command-alist and fetch them from there somehow.
655 (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
656 (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
657 (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
658 (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-jobs)
659 (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
660 (define-key LilyPond-mode-map "\C-cm" 'LilyPond-command-formatmidi)
661 (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
662 (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
663 (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
664 (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
665 (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
666 (define-key LilyPond-mode-map [(control c) return] 'LilyPond-command-current-midi)
667 (define-key LilyPond-mode-map [(control c) (control return)] 'LilyPond-command-all-midi)
668 (define-key LilyPond-mode-map "\C-x\C-s" 'LilyPond-save-buffer)
669 (define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
670 (define-key LilyPond-mode-map "\C-ci" 'LilyPond-quick-note-insert)
671 (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
672 (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
673 (define-key LilyPond-mode-map "\C-c:" 'LilyPond-un-comment-region)
674 (define-key LilyPond-mode-map "\C-c;" 'comment-region)
675 (define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren)
676 (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren)
677 (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren)
678 (define-key LilyPond-mode-map [iso-lefttab] 'LilyPond-autocompletion) ; Emacs
679 (define-key LilyPond-mode-map [iso-left-tab] 'LilyPond-autocompletion) ; XEmacs
680 (define-key LilyPond-mode-map "\C-c\t" 'LilyPond-info-index-search)
685 (defun LilyPond-quick-note-insert()
686 "Insert notes with fewer key strokes by using a simple keyboard piano."
689 '(("k" "a") ("l" "b") ("a" "c") ("s" "d")
690 ("d" "e") ("f" "f") ("j" "g") ("r" "r")))
691 (setq dutch-note-ends '("eses" "es" "" "is" "isis"))
692 (setq dutch-note-replacements '("" ""))
693 (setq finnish-note-replacements
694 '(("eeses" "eses") ("ees" "es") ("aeses" "asas") ("aes" "as") ("b" "h")
695 ("beses" "heses") ("bes" "b") ("bis" "his") ("bisis" "hisis")))
696 ; add more translations of the note names
697 (setq spanish-note-replacements
698 '(("c" "do") ("d" "re") ("e" "mi") ("f" "fa") ("g" "sol") ("a" "la") ("b
700 ("cis" "dos") ("cisis" "doss") ("ces" "dob") ("ceses" "dobb")
701 ("dis" "res") ("disis" "ress") ("des" "reb") ("deses" "rebb")
702 ("eis" "mis") ("eisis" "miss") ("ees" "mib") ("eeses" "mibb")
703 ("fis" "fas") ("fisis" "fass") ("fes" "fab") ("feses" "fabb")
704 ("gis" "sols") ("gisis" "solss") ("ges" "solb") ("geses" "solbb")
705 ("ais" "las") ("aisis" "lass") ("aes" "lab") ("aeses" "labb")
706 ("bis" "sis") ("bisis" "siss") ("bes" "sib") ("beses" "sibb")))
707 (setq other-keys "()<>~}")
708 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)
710 (message "Press h for help.") (sit-for 0 750)
712 (setq note-replacements dutch-note-replacements)
713 (setq xkey 0) (setq xinitpoint (point))
714 (< xinitpoint (point))
715 (while (not (= xkey 27)) ; esc to quit
716 (message (format " | a[_]s[_]d | f[_]j[_]k[_]l | r with ie ,' 12345678 . 0 (<~>)/}b\\b\\n Esc \n | c | d | e | f | g | a | %s | r with %s%s%s%s"
717 (if (string= (car(cdr(assoc "b" note-replacements))) "h")
719 (nth (+ accid 2) dutch-note-ends)
720 (make-string (abs octav) (if (> octav 0) ?' ?,))
722 (if (string= durat "") "" (make-string dots ?.))))
723 (setq xkey (read-char-exclusive))
724 (setq x (char-to-string xkey))
725 (setq note (cdr (assoc x dutch-notes)))
727 ((= xkey 13) (progn (insert "\n") (LilyPond-indent-line))) ; return
728 ((or (= xkey 127) (string= x "b")) ; backspace is a char only in Emacs
729 (progn (narrow-to-region xinitpoint (point))
730 (backward-kill-word 1)
732 ((and (string< x "9") (string< "0" x))
733 (progn (setq durat (int-to-string (expt 2 (- (string-to-int x) 1))))
735 ((string= x " ") (insert " "))
736 ((string= x "/") (progn (insert "\\times ")
737 (message "Insert a number for the denominator (\"x/\")")
738 (while (not (and (string< x "9") (string< "0" x)))
739 (setq x (char-to-string (read-char-exclusive))))
740 (insert (format "%s/" x)) (setq x "/")
741 (message "Insert a number for the numerator (\"/y\")")
742 (while (not (and (string< x "9") (string< "0" x)))
743 (setq x (char-to-string (read-char-exclusive))))
744 (insert (format "%s { " x))))
745 ((string= x "0") (progn (setq accid 0) (setq octav 0)
746 (setq durat "") (setq dots 0)))
747 ((string= x "i") (setq accid (if (= accid 2) 0 (max (+ accid 1) 1))))
748 ((string= x "e") (setq accid (if (= accid -2) 0 (min (+ accid -1) -1))))
749 ((string= x "'") (setq octav (if (= octav 4) 0 (max (+ octav 1) 1))))
750 ((string= x ",") (setq octav (if (= octav -4) 0 (min (+ octav -1) -1))))
751 ((string= x ".") (setq dots (if (= dots 4) 0 (+ dots 1))))
752 ((not (null (member x (split-string other-keys ""))))
753 (insert (format "%s " x)))
757 (format "%s%s" (car note) (if (string= "r" (car note)) ""
758 (nth (+ accid 2) dutch-note-ends))))
759 (setq notetwo (car (cdr (assoc note note-replacements))))
760 (if (not (null notetwo)) (setq note notetwo))
764 (if (string= "r" note) ""
765 (make-string (abs octav) (if (> octav 0) ?' ?,)))
767 (if (string= durat "") "" (make-string dots ?.))))
768 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)))
769 ((string= x "t") (progn (setq note-replacements dutch-note-replacements)
770 (message "Selected Dutch notes")
772 ((string= x "n") (progn (setq note-replacements finnish-note-replacements)
773 (message "Selected Finnish/Deutsch notes")
775 ; add more translations of the note names
776 ((string= x "p") (progn (setq note-replacements spanish-note-replacements)
777 (message "Selected Spanish notes")
780 (progn (message "Insert notes with fewer key strokes. For example \"i,5.f\" produces \"fis,32. \".") (sit-for 5 0)
781 (message "Add also \"a ~ a\"-ties, \"a ( ) b\"-slurs and \"< a b >\"-chords.") (sit-for 5 0)
782 (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)
783 (message "Backspace deletes last note, return starts a new indented line and Esc quits.") (sit-for 5 0)
784 (message "Insert note triplets \"\\times 2/3 { a b } \" by typing \"/23ab}\".") (sit-for 5 0)
785 (message "Remember to add all other details as well.") (sit-for 5 0)))))
786 (message "Normal editing mode."))
788 (defun LilyPond-pre-word-search ()
789 "Fetch the alphabetic characters and \\ in front of the cursor."
793 (ch (char-before (- (point) 0))))
794 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not bolp, A-Z
795 (and (>= ch 97) (<= ch 122)) ; a-z
797 (setq pre (concat (char-to-string ch) pre))
798 (setq prelen (+ prelen 1))
799 (setq ch (char-before (- (point) prelen))))
802 (defun LilyPond-post-word-search ()
803 "Fetch the alphabetic characters behind the cursor."
807 (ch (char-after (+ (point) 0))))
808 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not eolp, A-Z
809 (and (>= ch 97) (<= ch 122)))) ; a-z
810 (setq post (concat post (char-to-string ch)))
811 (setq postlen (+ postlen 1))
812 (setq ch (char-after (+ (point) postlen))))
815 (defun LilyPond-autocompletion ()
816 "Show completions in mini-buffer for the given word."
818 (let ((pre (LilyPond-pre-word-search))
819 (post (LilyPond-post-word-search))
821 ;; insert try-completion and show all-completions
822 (if (> (length pre) 0)
824 (setq trycomp (try-completion pre (LilyPond-add-dictionary-word ())))
825 (if (char-or-string-p trycomp)
826 (if (string-equal (concat pre post) trycomp)
827 (goto-char (+ (point) (length post)))
829 (delete-region (point) (+ (point) (length post)))
830 (insert (substring trycomp (length pre) nil))))
832 (delete-region (point) (+ (point) (length post)))
833 (font-lock-fontify-buffer))) ; only inserting fontifies
835 (setq complist (all-completions pre (LilyPond-add-dictionary-word ())))
836 (while (> (length complist) 0)
837 (setq compsstr (concat compsstr "\"" (car complist) "\" "))
838 (setq complist (cdr complist)))
842 (defun LilyPond-info ()
843 "Launch Info for lilypond."
847 (defun LilyPond-music-glossary-info ()
848 "Launch Info for music-glossary."
850 (info "music-glossary"))
852 (defun LilyPond-internals-info ()
853 "Launch Info for lilypond-internals."
855 (info "lilypond-internals"))
857 (defun LilyPond-info-index-search ()
858 "Inside Emacs, launch `info lilypond --index-search word-under-cursor'"
860 (let ((str (concat (LilyPond-pre-word-search) (LilyPond-post-word-search))))
861 (if (and (> (length str) 0)
862 (string-equal (substring str 0 1) "\\"))
863 (setq str (substring str 1 nil)))
867 (defun LilyPond-insert-string (pre)
868 "Insert text to the buffer."
873 (defun LilyPond-insert-between (text pre post)
874 "Insert text to the buffer if non-empty string is given."
876 (let ((str (read-string text)))
877 (if (string-equal str "")
879 (progn (setq pre_str_post (concat pre str post))
880 (insert pre_str_post)
881 (length pre_str_post)))))
883 (defun LilyPond-insert-tag-notes ()
884 "LilyPond notes tag."
886 (setq begin (if (and transient-mark-mode mark-active)
887 (mark-marker) (point-marker)))
888 (setq end (point-marker))
890 (setq l1 (LilyPond-insert-string "\\notes "))
891 (setq l2 (LilyPond-insert-between "Relative (e.g. c'): " "\\relative " " "))
893 (setq l2 (LilyPond-insert-between "Transpose (e.g. c c'): " "\\transpose " " ")))
894 (setq l3 (LilyPond-insert-string "{ "))
895 (goto-char (+ end l1 l2 l3))
896 (LilyPond-insert-string " }")
897 (goto-char (+ end l1 l2 l3)))
899 (defun LilyPond-insert-tag-score ()
900 "LilyPond score tag."
902 (setq begin (if (and transient-mark-mode mark-active)
903 (mark-marker) (point-marker)))
904 (setq end (point-marker))
906 (setq l1 (LilyPond-insert-string "\\score {\n ")) ; keep track of lengths
907 (goto-char (+ end l1))
908 (LilyPond-insert-string "\n \\paper { }\n")
909 (setq l2 (if (y-or-n-p "Insert \"\\header\" field? ")
910 (+ (LilyPond-insert-string " \\header {")
911 (LilyPond-insert-between "Title: " "\n title = \"" "\"")
912 (LilyPond-insert-between "Subtitle: " "\n subtitle = \"" "\"")
913 (LilyPond-insert-between "Piece: " "\n piece = \"" "\"")
914 (LilyPond-insert-between "Opus: " "\n opus = \"" "\"")
915 (LilyPond-insert-string "\n }\n"))
917 (setq l3 (if (y-or-n-p "Insert \"\\midi\" field? ")
918 (+ (LilyPond-insert-string " \\midi {")
919 (LilyPond-insert-between "Tempo: " " \\tempo (e.g. 4 = 60)" "")
920 (LilyPond-insert-string " }\n"))
922 (setq l4 (LilyPond-insert-string "}\n"))
923 (goto-char (+ end l1)))
925 (defun LilyPond-command-menu-entry (entry)
926 ;; Return LilyPond-command-alist ENTRY as a menu item.
927 (let ((name (car entry)))
928 (cond ((and (string-equal name LilyPond-command-Print)
929 LilyPond-printer-list)
930 (let ((command LilyPond-print-command)
932 (append (list LilyPond-command-Print)
933 (mapcar 'LilyPond-command-menu-printer-entry
934 LilyPond-printer-list))))
936 (vector name (list 'LilyPond-command-menu name) t)))))
939 (easy-menu-define LilyPond-command-menu
941 "Menu used in LilyPond mode."
944 [ "Master File" LilyPond-command-select-master
945 :keys "C-c C-c" :style radio
946 :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
947 [ "Buffer" LilyPond-command-select-buffer
948 :keys "C-c C-b" :style radio
949 :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
950 [ "Region" LilyPond-command-select-region
951 :keys "C-c C-r" :style radio
952 :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
953 ;;; (let ((file 'LilyPond-command-on-current))
954 ;;; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
955 ;;; Some kind of mapping which includes :keys might be more elegant
956 ;;; Put keys to Lilypond-command-alist and fetch them from there somehow.
957 '([ "LilyPond" LilyPond-command-lilypond t])
958 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
959 '([ "2Dvi" LilyPond-command-formatdvi t])
960 '([ "2PS" LilyPond-command-formatps t])
961 '([ "2Midi" LilyPond-command-formatmidi t])
962 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
963 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
964 '([ "Kill jobs" LilyPond-kill-jobs t])
966 '([ "SmartView" LilyPond-command-smartview t])
967 '([ "View" LilyPond-command-view t])
968 '([ "ViewPS" LilyPond-command-viewps t])
970 '([ "Midi (toggle)" LilyPond-command-current-midi t])
971 '([ "Midi all" LilyPond-command-all-midi t])
974 (easy-menu-define LilyPond-mode-menu
976 "Menu used in LilyPond mode."
977 (append '("LilyPond")
979 [ "\\notes..." LilyPond-insert-tag-notes t]
980 [ "\\score..." LilyPond-insert-tag-score t]
981 ["Quick Notes" LilyPond-quick-note-insert t]
982 ["Autocompletion" LilyPond-autocompletion t]
985 ["Uncomment Region" LilyPond-un-comment-region t]
986 ["Comment Region" comment-region t]
987 ["Refontify buffer" font-lock-fontify-buffer t]
990 ["LilyPond" LilyPond-info t]
991 ["LilyPond index-search" LilyPond-info-index-search t]
992 ["Music Glossary" LilyPond-music-glossary-info t]
993 ["LilyPond internals" LilyPond-internals-info t]
997 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z]+\\) *="
998 "Regexp matching Identifier definitions.")
1000 (defvar LilyPond-imenu-generic-expression
1001 (list (list nil LilyPond-imenu-generic-re 1))
1002 "Expression for imenu")
1004 (defun LilyPond-command-select-master ()
1006 (message "Next command will be on the master file")
1007 (setq LilyPond-command-current 'LilyPond-command-master))
1009 (defun LilyPond-command-select-buffer ()
1011 (message "Next command will be on the buffer")
1012 (setq LilyPond-command-current 'LilyPond-command-buffer))
1014 (defun LilyPond-command-select-region ()
1016 (message "Next command will be on the region")
1017 (setq LilyPond-command-current 'LilyPond-command-region))
1019 (defun LilyPond-command-menu (name)
1020 ;; Execute LilyPond-command-alist NAME from a menu.
1021 (let ((LilyPond-command-force name))
1022 (funcall LilyPond-command-current)))
1024 (defun LilyPond-mode ()
1025 "Major mode for editing LilyPond music files.
1027 This mode knows about LilyPond keywords and line comments, not about
1028 indentation or block comments. It features easy compilation, error
1029 finding and viewing of a LilyPond source buffer or region.
1032 \\{LilyPond-mode-map}
1035 LilyPond-command-alist\t\talist from name to command
1036 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
1038 ;; set up local variables
1039 (kill-all-local-variables)
1041 (make-local-variable 'font-lock-defaults)
1042 (setq font-lock-defaults '(LilyPond-font-lock-keywords))
1044 ;; string and comments are fontified explicitly
1045 (make-local-variable 'font-lock-keywords-only)
1046 (setq font-lock-keywords-only t)
1048 ;; Multi-line font-locking needs Emacs 21.1 or newer.
1049 ;; For older versions there is hotkey "C-c f".
1050 (make-local-variable 'font-lock-multiline)
1051 (setq font-lock-multiline t)
1053 (make-local-variable 'paragraph-separate)
1054 (setq paragraph-separate "^[ \t]*$")
1056 (make-local-variable 'paragraph-start)
1057 (setq paragraph-start "^[ \t]*$")
1059 (make-local-variable 'comment-start)
1060 (setq comment-start "%")
1062 (make-local-variable 'comment-start-skip)
1063 (setq comment-start-skip "%{? *")
1065 (make-local-variable 'comment-end)
1066 (setq comment-end "")
1068 (make-local-variable 'block-comment-start)
1069 (setq block-comment-start "%{")
1071 (make-local-variable 'block-comment-end)
1072 (setq block-comment-end "%}")
1074 (make-local-variable 'indent-line-function)
1075 (setq indent-line-function 'LilyPond-indent-line)
1077 (set-syntax-table LilyPond-mode-syntax-table)
1078 (setq major-mode 'LilyPond-mode)
1079 (setq mode-name "LilyPond")
1080 (setq local-abbrev-table LilyPond-mode-abbrev-table)
1081 (use-local-map LilyPond-mode-map)
1083 ;; In Emacs blink-...-on-screen needs to be declared (, not in XEmacs).
1084 (make-local-variable 'blink-matching-paren-on-screen)
1085 (setq blink-matching-paren-on-screen t)
1087 (make-local-variable 'imenu-generic-expression)
1088 (setq imenu-generic-expression LilyPond-imenu-generic-expression)
1089 (imenu-add-to-menubar "Index")
1091 ;; In XEmacs one needs to use 'easy-menu-add' (, not in Emacs).
1092 (easy-menu-add LilyPond-mode-menu)
1093 (easy-menu-add LilyPond-command-menu)
1095 ;; run the mode hook. LilyPond-mode-hook use is deprecated
1096 (run-hooks 'LilyPond-mode-hook))
1098 (defun LilyPond-version ()
1099 "Echo the current version of `LilyPond-mode' in the minibuffer."
1101 (message "Using `LilyPond-mode' version %s" LilyPond-version))
1103 (load-library "lilypond-font-lock")
1104 (load-library "lilypond-indent")
1106 (provide 'lilypond-mode)
1107 ;;; lilypond-mode.el ends here