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 ;;; Inspired on auctex
16 ;;; Look lilypond-init.el or Documentation/topdocs/INSTALL.texi
17 ;;; for installing instructions.
22 (defconst LilyPond-version
"1.7.30"
23 "`LilyPond-mode' version number.")
25 (defconst LilyPond-help-address
"bug-lilypond@gnu.org"
26 "Address accepting submission of bug reports.")
28 (defvar LilyPond-mode-hook nil
29 "*Hook called by `LilyPond-mode'.")
31 (defvar LilyPond-region-file-prefix
"emacs-lily"
32 "File prefix for commands on buffer or region.")
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 (defvar LilyPond-imenu nil
50 "A flag to tell whether LilyPond-imenu is turned on.")
51 (make-variable-buffer-local 'LilyPond-imenu
)
53 (defcustom LilyPond-include-path
".:/tmp"
54 "* LilyPond include path."
58 (defun LilyPond-words-filename ()
59 "The file containing LilyPond \keywords \Identifiers and ReservedWords.
60 Finds file lilypond-words from load-path."
63 (words-file "lilypond.words"))
64 (while (and (> (length lp
) 0) (not fn
))
65 (setq fn
(concat (car lp
) "/" words-file
))
66 (if (not (file-readable-p fn
))
67 (progn (setq fn nil
) (setq lp
(cdr lp
)))))
69 (progn (message "Warning: `lilypond.words' not found in `load-path'. See `lilypond-init.el'.")
73 (defun LilyPond-add-dictionary-word (x)
74 "Contains all words: \keywords \Identifiers and ReservedWords."
75 (nconc '(("" .
1)) x
))
77 ;; creates dictionary if empty
78 (if (and (eq (length (LilyPond-add-dictionary-word ())) 1)
79 (not (eq (LilyPond-words-filename) nil
)))
81 (setq b
(find-file-noselect (LilyPond-words-filename) t t
))
82 (setq m
(set-marker (make-marker) 1 (get-buffer b
)))
84 (while (> (buffer-size b
) (marker-position m
))
86 (setq copy
(copy-alist (list (eval (symbol-name (read m
))))))
88 (LilyPond-add-dictionary-word (list copy
)))
91 (defvar LilyPond-insert-tag-current
""
92 "The last command selected from the LilyPond-Insert -menu.")
94 (defconst LilyPond-menu-keywords
95 (let ((wordlist '()) ; add syntax entries to lilypond.words
96 (co (all-completions "" (LilyPond-add-dictionary-word ())))
99 (while (> (length co
) 0)
100 (setq currword
(car co
))
101 (if (string-equal "-" (car (setq co
(cdr co
))))
103 (add-to-list 'wordlist currword
)
104 (while (and (> (length co
) 0)
105 (not (string-equal "-" (car (setq co
(cdr co
))))))))))
107 "Keywords inserted from LilyPond-Insert-menu.")
109 (defconst LilyPond-keywords
110 (let ((wordlist '("\\score")) ; add \keywords to lilypond.words
111 (co (all-completions "" (LilyPond-add-dictionary-word ())))
114 (while (> (length co
) 0)
115 (setq currword
(car co
))
116 (if (> (length currword
) 1)
117 (if (and (string-equal "\\" (substring currword
0 1))
118 (not (string-match "[^a-zA-Z]" (substring currword
1)))
119 (string-equal (downcase currword
) currword
))
120 (add-to-list 'wordlist currword
)))
121 (if (string-equal "-" (car (setq co
(cdr co
))))
122 (while (and (> (length co
) 0)
123 (not (string-equal "-" (car (setq co
(cdr co
)))))))))
125 "LilyPond \\keywords")
127 (defconst LilyPond-identifiers
128 (let ((wordlist '("\\voiceOne")) ; add \Identifiers to lilypond.words
129 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
131 (while (> (length co
) 0)
132 (setq currword
(car co
))
133 (if (> (length currword
) 1)
134 (if (and (string-equal "\\" (substring currword
0 1))
135 (not (string-match "[^a-zA-Z]" (substring currword
1)))
136 (not (string-equal (downcase currword
) currword
)))
137 (add-to-list 'wordlist currword
)))
138 (if (string-equal "-" (car (setq co
(cdr co
))))
139 (while (and (> (length co
) 0)
140 (not (string-equal "-" (car (setq co
(cdr co
)))))))))
142 "LilyPond \\Identifiers")
144 (defconst LilyPond-reserved-words
145 (let ((wordlist '("Staff")) ; add ReservedWords to lilypond.words
146 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
148 (while (> (length co
) 0)
149 (setq currword
(car co
))
150 (if (> (length currword
) 0)
151 (if (not (string-match "[^a-zA-Z]" currword
))
152 (add-to-list 'wordlist currword
)))
153 (if (string-equal "-" (car (setq co
(cdr co
))))
154 (while (and (> (length co
) 0)
155 (not (string-equal "-" (car (setq co
(cdr co
)))))))))
157 "LilyPond ReservedWords")
159 (defun LilyPond-check-files (derived originals extensions
)
160 "Check that DERIVED is newer than any of the ORIGINALS.
161 Try each original with each member of EXTENSIONS, in all directories
162 in LilyPond-include-path."
164 (regexp (concat "\\`\\("
165 (mapconcat (function (lambda (dir)
166 (regexp-quote (expand-file-name dir
))))
167 LilyPond-include-path
"\\|")
169 (mapconcat 'regexp-quote originals
"\\|")
171 (mapconcat 'regexp-quote extensions
"\\|")
173 (buffers (buffer-list)))
175 (let* ((buffer (car buffers
))
176 (name (buffer-file-name buffer
)))
177 (setq buffers
(cdr buffers
))
178 (if (and name
(string-match regexp name
))
180 (and (buffer-modified-p buffer
)
181 (or (not LilyPond-save-query
)
182 (y-or-n-p (concat "Save file "
183 (buffer-file-name buffer
)
185 (save-excursion (set-buffer buffer
) (save-buffer)))
186 (if (file-newer-than-file-p name derived
)
190 (defun LilyPond-running ()
191 "Check the currently running LilyPond compiling jobs."
192 (let ((process-names (list "lilypond" "tex" "2dvi" "2ps" "2midi"
195 (while (setq process-name
(pop process-names
))
196 (setq process
(get-process process-name
))
198 (eq (process-status process
) 'run
))
199 (push process-name running
)))
200 running
)) ; return the running jobs
202 (defun LilyPond-midi-running ()
203 "Check the currently running Midi processes."
204 (let ((process-names (list "midi" "midiall"))
206 (while (setq process-name
(pop process-names
))
207 (setq process
(get-process process-name
))
209 (eq (process-status process
) 'run
))
210 (push process-name running
)))
211 running
)) ; return the running jobs
213 (defun LilyPond-kill-jobs ()
214 "Kill the currently running LilyPond compiling jobs."
216 (let ((process-names (LilyPond-running))
218 (while (setq process-name
(pop process-names
))
219 (quit-process (get-process process-name
) t
)
220 (push process-name killed
))
221 killed
)) ; return the killed jobs
223 (defun LilyPond-kill-midi ()
224 "Kill the currently running midi processes."
225 (let ((process-names (LilyPond-midi-running))
227 (while (setq process-name
(pop process-names
))
228 (quit-process (get-process process-name
) t
)
229 (push process-name killed
))
230 killed
)) ; return the killed jobs
232 ;; URG, should only run LilyPond-compile for LilyPond
233 ;; not for tex,xdvi (ly2dvi?)
234 (defun LilyPond-compile-file (command name
)
235 ;; We maybe should know what we run here (Lily, ly2dvi, tex)
236 ;; and adjust our error-matching regex ?
238 (if (eq LilyPond-command-current
'LilyPond-command-master
)
240 ;; use temporary directory for Commands on Buffer/Region
241 ;; hm.. the directory is set twice, first to default-dir
242 (concat "cd " (LilyPond-temp-directory) "; " command
))
243 "No more errors" name
))
245 ;; do we still need this, now that we're using compile-internal?
246 (defun LilyPond-save-buffer ()
247 "Save buffer and set default command for compiling."
249 (if (buffer-modified-p)
251 (setq LilyPond-command-default
"LilyPond"))))
253 ;;; return (dir base ext)
254 (defun split-file-name (name)
255 (let* ((i (string-match "[^/]*$" name
))
256 (dir (if (> i
0) (substring name
0 i
) "./"))
257 (file (substring name i
(length name
)))
258 (i (string-match "[^.]*$" file
)))
262 (list dir
(substring file
0 (- i
1)) (substring file i
(length file
)))
263 (list dir file
""))))
266 ;; Should check whether in command-alist?
267 (defcustom LilyPond-command-default
"LilyPond"
268 "Default command. Must identify a member of LilyPond-command-alist."
272 ;;;(make-variable-buffer-local 'LilyPond-command-last)
274 (defvar LilyPond-command-current
'LilyPond-command-master
)
275 ;;;(make-variable-buffer-local 'LilyPond-command-master)
278 ;; If non-nil, LilyPond-command-query will return the value of this
279 ;; variable instead of quering the user.
280 (defvar LilyPond-command-force nil
)
282 (defcustom LilyPond-xdvi-command
"xdvi"
283 "Command used to display DVI files."
288 (defcustom LilyPond-gv-command
"gv -watch"
289 "Command used to display PS files."
294 (defcustom LilyPond-midi-command
"timidity"
295 "Command used to play MIDI files."
300 (defcustom LilyPond-all-midi-command
"timidity -ia"
301 "Command used to play MIDI files."
306 (defun LilyPond-command-current-midi ()
307 "Play midi corresponding to the current document."
309 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file
))
311 (defun LilyPond-command-all-midi ()
312 "Play midi corresponding to the current document."
314 (LilyPond-command (LilyPond-command-menu "MidiAll") 'LilyPond-master-file
))
316 (defun count-rexp (start end rexp
)
317 "Print number of found regular expressions in the region."
321 (narrow-to-region start end
)
322 (goto-char (point-min))
323 (count-matches rexp
))))
325 (defun count-midi-words ()
326 "Check number of midi-scores before the curser."
327 (if (eq LilyPond-command-current
'LilyPond-command-region
)
328 (count-rexp (mark t
) (point) "\\\\midi")
329 (count-rexp (point-min) (point-max) "\\\\midi")))
331 (defun count-midi-words-backwards ()
332 "Check number of midi-scores before the curser."
333 (if (eq LilyPond-command-current
'LilyPond-command-region
)
334 (count-rexp (mark t
) (point) "\\\\midi")
335 (count-rexp (point-min) (point) "\\\\midi")))
337 (defun LilyPond-string-current-midi ()
338 "Check the midi file of the following midi-score in the current document."
339 (let ((fnameprefix (if (eq LilyPond-command-current
'LilyPond-command-master
)
340 (substring (LilyPond-master-file) 0 -
3); suppose ".ly"
341 LilyPond-region-file-prefix
))
342 (allcount (string-to-number (substring (count-midi-words) 0 -
12)))
343 (count (string-to-number (substring (count-midi-words-backwards) 0 -
12))))
345 (if (and (> allcount
1) (> count
0)) ; not first score
346 (if (eq count allcount
) ; last score
347 (concat "-" (number-to-string (+ count -
1)))
348 (concat "-" (number-to-string count
))))
351 (defun LilyPond-string-all-midi ()
352 "Return the midi files of the current document in ascending order."
353 (let ((fnameprefix (if (eq LilyPond-command-current
'LilyPond-command-master
)
354 (substring (LilyPond-master-file) 0 -
3); suppose ".ly"
355 LilyPond-region-file-prefix
))
356 (allcount (string-to-number (substring (count-midi-words) 0 -
12))))
357 (concat (if (> allcount
0) ; at least one midi-score
358 (concat fnameprefix
".midi "))
359 (if (> allcount
1) ; more than one midi-score
360 (concat fnameprefix
"-[1-9].midi "))
361 (if (> allcount
9) ; etc.
362 (concat fnameprefix
"-[1-9][0-9].midi"))
363 (if (> allcount
99) ; not first score
364 (concat fnameprefix
"-[1-9][0-9][0-9].midi")))))
366 ;; This is the major configuration variable.
367 (defcustom LilyPond-command-alist
368 ;; Should expand this to include possible keyboard shortcuts which
369 ;; could then be mapped to define-key and menu.
371 ("LilyPond" .
("lilypond %s" .
"LaTeX"))
372 ("TeX" .
("tex '\\nonstopmode\\input %t'" .
"View"))
374 ("2Dvi" .
("ly2dvi %s" .
"View"))
375 ("2PS" .
("ly2dvi -P %s" .
"ViewPS"))
376 ("2Midi" .
("lilypond -m %s" .
"View"))
378 ("Book" .
("lilypond-book %x" .
"LaTeX"))
379 ("LaTeX" .
("latex '\\nonstopmode\\input %l'" .
"View"))
381 ;; point-n-click (arg: exits upop USR1)
382 ("SmartView" .
("xdvi %d" .
"LilyPond"))
384 ;; refreshes when kicked USR1
385 ("View" .
(,(concat LilyPond-xdvi-command
" %d") .
"LilyPond"))
386 ("ViewPS" .
(,(concat LilyPond-gv-command
" %p") .
"LilyPond"))
388 ;; The following are refreshed in LilyPond-command:
389 ;; - current-midi depends on cursor position and
390 ("Midi" .
(,(concat LilyPond-midi-command
" " (LilyPond-string-current-midi)) .
"LilyPond" )) ;
391 ;; - all-midi depends on number of midi-score.
392 ("MidiAll" .
(,(concat LilyPond-all-midi-command
" " (LilyPond-string-all-midi)) .
"LilyPond"))
395 "AList of commands to execute on the current document.
397 The key is the name of the command as it will be presented to the
398 user, the value is a cons of the command string handed to the shell
399 after being expanded, and the next command to be executed upon
400 success. The expansion is done using the information found in
401 LilyPond-expand-list.
404 :type
'(repeat (cons :tag
"Command Item"
407 (string :tag
"Command")
408 (string :tag
"Next Key")))))
411 (defcustom LilyPond-file-extension
".ly"
412 "*File extension used in LilyPond sources."
417 (defcustom LilyPond-expand-alist
428 "Alist of expansion strings for LilyPond command names."
430 :type
'(repeat (cons :tag
"Alist item"
431 (string :tag
"Symbol")
432 (string :tag
"Expansion"))))
435 (defcustom LilyPond-command-Show
"View"
436 "*The default command to show (view or print) a LilyPond file.
437 Must be the car of an entry in `LilyPond-command-alist'."
440 (make-variable-buffer-local 'LilyPond-command-Show
)
442 (defcustom LilyPond-command-Print
"Print"
443 "The name of the Print entry in LilyPond-command-Print."
447 (defun xLilyPond-compile-sentinel (process msg
)
449 (= 0 (process-exit-status process
)))
450 (setq LilyPond-command-default
451 (cddr (assoc LilyPond-command-default LilyPond-command-alist
)))))
453 ;; FIXME: shouldn't do this for stray View/xdvi
454 (defun LilyPond-compile-sentinel (buffer msg
)
455 (if (string-match "^finished" msg
)
456 (setq LilyPond-command-default
457 (cddr (assoc LilyPond-command-default LilyPond-command-alist
)))))
459 ;;(make-variable-buffer-local 'compilation-finish-function)
460 (setq compilation-finish-function
'LilyPond-compile-sentinel
)
462 (defun LilyPond-command-query (name)
463 "Query the user for what LilyPond command to use."
464 (let* ((default (cond ((if (string-equal name LilyPond-region-file-prefix
)
465 (LilyPond-check-files (concat name
".tex")
467 (list LilyPond-file-extension
))
468 (if (verify-visited-file-modtime (current-buffer))
469 (if (buffer-modified-p)
470 (if (y-or-n-p "Save buffer before next command? ")
471 (LilyPond-save-buffer)))
472 (if (y-or-n-p "The command will be invoked to an already saved buffer. Revert it? ")
473 (revert-buffer t t
)))
475 LilyPond-command-default
))
476 (t LilyPond-command-default
)))
478 (completion-ignore-case t
)
480 (answer (or LilyPond-command-force
482 (concat "Command: (default " default
") ")
483 LilyPond-command-alist nil t nil
'LilyPond-command-history
))))
485 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
486 (let ((answer (car-safe (assoc answer LilyPond-command-alist
))))
488 (not (string-equal answer
"")))
492 (defun LilyPond-command-master ()
493 "Run command on the current document."
495 (LilyPond-command-select-master)
496 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
497 'LilyPond-master-file
))
499 (defun LilyPond-command-lilypond ()
500 "Run lilypond for the current document."
502 (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file
)
505 (defun LilyPond-command-formatdvi ()
506 "Format the dvi output of the current document."
508 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file
)
511 (defun LilyPond-command-formatps ()
512 "Format the ps output of the current document."
514 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file
)
517 (defun LilyPond-command-formatmidi ()
518 "Format the midi output of the current document."
520 (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-master-file
)
523 (defun LilyPond-command-smartview ()
524 "View the dvi output of current document."
526 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file
)
529 (defun LilyPond-command-view ()
530 "View the dvi output of current document."
532 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file
)
535 (defun LilyPond-command-viewps ()
536 "View the ps output of current document."
538 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file
)
541 ;; FIXME, this is broken
542 (defun LilyPond-region-file (begin end
)
545 (dir (LilyPond-temp-directory))
546 (base LilyPond-region-file-prefix
)
547 (ext LilyPond-file-extension
))
548 (concat dir base ext
)))
550 ;;; Commands on Region work if there is an appropriate '\score'.
551 (defun LilyPond-command-region (begin end
)
552 "Run LilyPond on the current region."
554 (if (or (> begin
(point-min)) (< end
(point-max)))
555 (LilyPond-command-select-region))
556 (write-region begin end
(LilyPond-region-file begin end
) nil
'nomsg
)
557 (LilyPond-command (LilyPond-command-query
558 (LilyPond-region-file begin end
))
559 '(lambda () (LilyPond-region-file begin end
)))
560 ;; Region may deactivate even if buffer was intact, reactivate?
561 ;; Currently, also deactived regions are used.
564 (defun LilyPond-command-buffer ()
565 "Run LilyPond on buffer."
567 (LilyPond-command-select-buffer)
568 (LilyPond-command-region (point-min) (point-max)))
570 (defun LilyPond-command-expand (string file
)
571 (let ((case-fold-search nil
))
572 (if (string-match "%" string
)
573 (let* ((b (match-beginning 0))
575 (l (split-file-name file
))
578 (LilyPond-command-expand
579 (concat (substring string
0 b
)
582 (let ((entry (assoc (substring string b e
)
583 LilyPond-expand-alist
)))
584 (if entry
(cdr entry
) ""))
585 (substring string e
))
589 (defun LilyPond-shell-process (name buffer command
)
590 (let ((old (current-buffer)))
591 (switch-to-buffer-other-window buffer
)
592 ;; If we empty the buffer don't see messages scroll by.
595 (start-process-shell-command name buffer command
)
596 (switch-to-buffer-other-window old
)))
599 (defun LilyPond-command (name file
)
600 "Run command NAME on the file you get by calling FILE.
602 FILE is a function return a file name. It has one optional argument,
603 the extension to use on the file.
605 Use the information in LilyPond-command-alist to determine how to run the
608 (let ((entry (assoc name LilyPond-command-alist
)))
610 (let ((command (LilyPond-command-expand (cadr entry
)
613 (job-string "no jobs"))
614 (if (member name
(list "View" "ViewPS"))
615 ;; is USR1 a right signal for viewps?
616 (let ((buffer-xdvi (get-buffer-create (concat "*" name
"*"))))
617 (if LilyPond-kick-xdvi
618 (let ((process-xdvi (get-buffer-process buffer-xdvi
)))
620 (signal-process (process-id process-xdvi
) 'SIGUSR1
)
621 (LilyPond-shell-process name buffer-xdvi command
)))
622 (LilyPond-shell-process name buffer-xdvi command
)))
624 (if (string-equal name
"Midi")
626 (setq command
(concat LilyPond-midi-command
" " (LilyPond-string-current-midi)))
627 (if (LilyPond-kill-midi)
628 (setq job-string nil
)))) ; either stop or start playing
629 (if (string-equal name
"MidiAll")
631 (setq command
(concat LilyPond-all-midi-command
" " (LilyPond-string-all-midi)))
632 (LilyPond-kill-midi))) ; stop and start playing
633 (if (and (member name
(list "Midi" "MidiAll")) job-string
)
634 (if (file-newer-than-file-p
635 (LilyPond-master-file)
636 (concat (substring (LilyPond-master-file) 0 -
3) ".midi"))
637 (if (y-or-n-p "Midi older than source. Reformat midi?")
639 (LilyPond-command-formatmidi)
640 (while (LilyPond-running)
641 (message "Starts playing midi once it is built.")
643 (if (member name
(list "LilyPond" "TeX" "2Midi" "2PS" "2Dvi"
645 (if (setq jobs
(LilyPond-running))
647 (setq job-string
"Process") ; could also suggest compiling after process has ended
649 (setq job-string
(concat job-string
" \"" (pop jobs
) "\"")))
650 (setq job-string
(concat job-string
" is already running; kill it to proceed "))
651 (if (y-or-n-p job-string
)
653 (setq job-string
"no jobs")
655 (while (LilyPond-running)
657 (setq job-string nil
)))))
659 (setq LilyPond-command-default name
)
660 (if (string-equal job-string
"no jobs")
661 (LilyPond-compile-file command name
))))))))
663 (defun LilyPond-mark-active ()
664 "Check if there is an active mark."
665 (and transient-mark-mode
666 (if (string-match "XEmacs\\|Lucid" emacs-version
) (mark) mark-active
)))
668 (defun LilyPond-temp-directory ()
669 "Temporary file directory for Commands on Region."
671 (if (string-match "XEmacs\\|Lucid" emacs-version
)
672 (concat (temp-directory) "/")
673 temporary-file-directory
))
677 (defvar LilyPond-mode-map
()
678 "Keymap used in `LilyPond-mode' buffers.")
680 ;; Note: if you make changes to the map, you must do
681 ;; M-x set-variable LilyPond-mode-map nil
684 ;; to let the changest take effect
686 (if LilyPond-mode-map
688 (setq LilyPond-mode-map
(make-sparse-keymap))
689 ;; Put keys to LilyPond-command-alist and fetch them from there somehow.
690 (define-key LilyPond-mode-map
"\C-c\C-l" 'LilyPond-command-lilypond
)
691 (define-key LilyPond-mode-map
"\C-c\C-r" 'LilyPond-command-region
)
692 (define-key LilyPond-mode-map
"\C-c\C-b" 'LilyPond-command-buffer
)
693 (define-key LilyPond-mode-map
"\C-c\C-k" 'LilyPond-kill-jobs
)
694 (define-key LilyPond-mode-map
"\C-c\C-c" 'LilyPond-command-master
)
695 (define-key LilyPond-mode-map
"\C-cm" 'LilyPond-command-formatmidi
)
696 (define-key LilyPond-mode-map
"\C-c\C-d" 'LilyPond-command-formatdvi
)
697 (define-key LilyPond-mode-map
"\C-c\C-f" 'LilyPond-command-formatps
)
698 (define-key LilyPond-mode-map
"\C-c\C-s" 'LilyPond-command-smartview
)
699 (define-key LilyPond-mode-map
"\C-c\C-v" 'LilyPond-command-view
)
700 (define-key LilyPond-mode-map
"\C-c\C-p" 'LilyPond-command-viewps
)
701 (define-key LilyPond-mode-map
[(control c
) return
] 'LilyPond-command-current-midi
)
702 (define-key LilyPond-mode-map
[(control c
) (control return
)] 'LilyPond-command-all-midi
)
703 (define-key LilyPond-mode-map
"\C-x\C-s" 'LilyPond-save-buffer
)
704 (define-key LilyPond-mode-map
"\C-cf" 'font-lock-fontify-buffer
)
705 (define-key LilyPond-mode-map
"\C-ci" 'LilyPond-insert-tag-current
)
706 ;; the following will should be overriden by Lilypond Quick Insert Mode
707 (define-key LilyPond-mode-map
"\C-cq" 'LilyPond-quick-insert-mode
)
708 (define-key LilyPond-mode-map
"\C-c;" 'LilyPond-comment-region
)
709 (define-key LilyPond-mode-map
")" 'LilyPond-electric-close-paren
)
710 (define-key LilyPond-mode-map
">" 'LilyPond-electric-close-paren
)
711 (define-key LilyPond-mode-map
"}" 'LilyPond-electric-close-paren
)
712 (define-key LilyPond-mode-map
"]" 'LilyPond-electric-close-paren
)
713 (if (string-match "XEmacs\\|Lucid" emacs-version
)
714 (define-key LilyPond-mode-map
[iso-left-tab
] 'LilyPond-autocompletion
)
715 (define-key LilyPond-mode-map
[iso-lefttab
] 'LilyPond-autocompletion
))
716 (define-key LilyPond-mode-map
"\C-c\t" 'LilyPond-info-index-search
)
721 ;;; This mode was originally LilyPond-quick-note-insert by Heikki Junes.
722 ;;; The original version has been junked since CVS-1.97,
723 ;;; in order to merge the efforts done by Nicolas Sceaux.
724 ;;; LilyPond Quick Insert Mode is a major mode, toggled by C-c q.
725 (defun LilyPond-quick-insert-mode ()
726 "Insert notes with fewer key strokes by using a simple keyboard piano."
729 (message "Invoke (C-c q) from keyboard. If you still see this message,") (sit-for 5 0)
730 (message "then you have not installed LilyPond Quick Insert Mode (lyqi).") (sit-for 5 0)
731 (message "Download lyqi from http://nicolas.sceaux.free.fr/lilypond/lyqi.html,") (sit-for 5 0)
732 (message "see installation instructions from lyqi's README -file.") (sit-for 5 0)
733 (message "You need also eieio (Enhanced Integration of Emacs Interpreted Objects).") (sit-for 5 0)
734 (message "Download eieio from http://cedet.sourceforge.net/eieio.shtml,") (sit-for 5 0)
735 (message "see installation instructions from eieio's INSTALL -file.") (sit-for 5 0)
739 (defun LilyPond-pre-word-search ()
740 "Fetch the alphabetic characters and \\ in front of the cursor."
743 (ch (char-before (- (point) 0))))
744 (while (and ch
(or (and (>= ch
65) (<= ch
90)) ; not bolp, A-Z
745 (and (>= ch
97) (<= ch
122)) ; a-z
747 (setq pre
(concat (char-to-string ch
) pre
))
748 (setq prelen
(+ prelen
1))
749 (setq ch
(char-before (- (point) prelen
))))
752 (defun LilyPond-post-word-search ()
753 "Fetch the alphabetic characters behind the cursor."
756 (ch (char-after (+ (point) 0))))
757 (while (and ch
(or (and (>= ch
65) (<= ch
90)) ; not eolp, A-Z
758 (and (>= ch
97) (<= ch
122)))) ; a-z
759 (setq post
(concat post
(char-to-string ch
)))
760 (setq postlen
(+ postlen
1))
761 (setq ch
(char-after (+ (point) postlen
))))
764 (defun LilyPond-autocompletion ()
765 "Show completions in mini-buffer for the given word."
767 (let ((pre (LilyPond-pre-word-search))
768 (post (LilyPond-post-word-search))
770 ;; insert try-completion and show all-completions
771 (if (> (length pre
) 0)
773 (setq trycomp
(try-completion pre
(LilyPond-add-dictionary-word ())))
774 (if (char-or-string-p trycomp
)
775 (if (string-equal (concat pre post
) trycomp
)
776 (goto-char (+ (point) (length post
)))
778 (delete-region (point) (+ (point) (length post
)))
779 (insert (substring trycomp
(length pre
) nil
))))
781 (delete-region (point) (+ (point) (length post
)))
782 (font-lock-fontify-buffer))) ; only inserting fontifies
784 (setq complist
(all-completions pre
(LilyPond-add-dictionary-word ())))
785 (while (> (length complist
) 0)
786 (setq compsstr
(concat compsstr
"\"" (car complist
) "\" "))
787 (setq complist
(cdr complist
)))
791 (defun LilyPond-info ()
792 "Launch Info for lilypond."
796 (defun LilyPond-music-glossary-info ()
797 "Launch Info for music-glossary."
799 (info "music-glossary"))
801 (defun LilyPond-internals-info ()
802 "Launch Info for lilypond-internals."
804 (info "lilypond-internals"))
806 (defun LilyPond-info-index-search ()
807 "In `*info*'-buffer, launch `info lilypond --index-search word-under-cursor'"
809 (let ((str (concat (LilyPond-pre-word-search) (LilyPond-post-word-search))))
810 (if (and (> (length str
) 0)
811 (string-equal (substring str
0 1) "\\"))
812 (setq str
(substring str
1 nil
)))
816 (defun LilyPond-insert-tag-current (&optional word
)
817 "Set the current tag to be inserted."
820 (setq LilyPond-insert-tag-current word
))
821 (if (memq LilyPond-insert-tag-current LilyPond-menu-keywords
)
822 (LilyPond-insert-tag)
823 (message "No tag was selected from LilyPond->Insert tag-menu.")))
825 (defun LilyPond-insert-tag ()
826 "Insert syntax for given tag. The definitions are in lilypond.words."
828 (setq b
(find-file-noselect (LilyPond-words-filename) t t
))
829 (let ((word LilyPond-insert-tag-current
)
833 (m (set-marker (make-marker) 1 (get-buffer b
)))
834 (distance (if (LilyPond-mark-active)
835 (abs (- (mark-marker) (point-marker))) 0))
837 ;; find the place first
838 (if (LilyPond-mark-active)
839 (goto-char (min (mark-marker) (point-marker))))
840 (while (and (not found
) (> (buffer-size b
) (marker-position m
)))
841 (setq copy
(car (copy-alist (list (eval (symbol-name (read m
)))))))
842 (if (string-equal word copy
) (setq found t
)))
843 (if found
(insert word
))
844 (if (> (buffer-size b
) (marker-position m
))
845 (setq copy
(car (copy-alist (list (eval (symbol-name (read m
))))))))
846 (if (not (string-equal "-" copy
))
848 (while (and found
(> (buffer-size b
) (marker-position m
)))
850 (setq copy
(car (copy-alist (list (eval (symbol-name (read m
)))))))
851 ;; check whether it is the word, or the word has been found
853 ((string-equal "-" copy
) (setq found nil
))
854 ((string-equal "%" copy
) (insert " " (read-string "Give Arguments: ")))
855 ((string-equal "_" copy
)
858 (goto-char (+ p distance
))))
859 ((string-equal "\?" copy
) (setq query t
))
860 ((string-equal "\!" copy
) (setq query nil
))
861 ((string-equal "\\n" copy
)
863 (progn (LilyPond-indent-line) (insert "\n") (LilyPond-indent-line))))
864 ((string-equal "{" copy
)
866 (progn (insert " { "))))
867 ((string-equal "}" copy
)
869 (progn (insert " } ") (setq query nil
) )))
873 (if (y-or-n-p (concat "Proceed with `" copy
"'? "))
874 (progn (insert copy
) (setq query nil
))))
880 (defun LilyPond-command-menu-entry (entry)
881 ;; Return LilyPond-command-alist ENTRY as a menu item.
882 (let ((name (car entry
)))
883 (cond ((and (string-equal name LilyPond-command-Print
)
884 LilyPond-printer-list
)
885 (let ((command LilyPond-print-command
)
887 (append (list LilyPond-command-Print
)
888 (mapcar 'LilyPond-command-menu-printer-entry
889 LilyPond-printer-list
))))
891 (vector name
(list 'LilyPond-command-menu name
) t
)))))
894 (easy-menu-define LilyPond-command-menu
896 "Menu used in LilyPond mode."
899 [ "Master File" LilyPond-command-select-master
900 :keys
"C-c C-c" :style radio
901 :selected
(eq LilyPond-command-current
'LilyPond-command-master
) ]
902 [ "Buffer" LilyPond-command-select-buffer
903 :keys
"C-c C-b" :style radio
904 :selected
(eq LilyPond-command-current
'LilyPond-command-buffer
) ]
905 [ "Region" LilyPond-command-select-region
906 :keys
"C-c C-r" :style radio
907 :selected
(eq LilyPond-command-current
'LilyPond-command-region
) ]))
908 ;;; (let ((file 'LilyPond-command-on-current))
909 ;;; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
910 ;;; Some kind of mapping which includes :keys might be more elegant
911 ;;; Put keys to LilyPond-command-alist and fetch them from there somehow.
912 '([ "LilyPond" LilyPond-command-lilypond t
])
913 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file
) ])
914 '([ "2Dvi" LilyPond-command-formatdvi t
])
915 '([ "2PS" LilyPond-command-formatps t
])
916 '([ "2Midi" LilyPond-command-formatmidi t
])
917 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file
) ])
918 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file
) ])
919 '([ "Kill jobs" LilyPond-kill-jobs t
])
921 '([ "SmartView" LilyPond-command-smartview t
])
922 '([ "View" LilyPond-command-view t
])
923 '([ "ViewPS" LilyPond-command-viewps t
])
925 '([ "Midi (toggle)" LilyPond-command-current-midi t
])
926 '([ "Midi all" LilyPond-command-all-midi t
])
929 (defun LilyPond-menu-keywords (arg)
930 "Make vector for LilyPond-mode-menu."
931 (vector arg
(list 'LilyPond-insert-tag-current arg
) :style
'radio
:selected
(list 'eq
'LilyPond-insert-tag-current arg
)))
933 ;;; LilyPond-mode-menu should not be interactive, via "M-x LilyPond-<Tab>"
934 (easy-menu-define LilyPond-mode-menu
936 "Menu used in LilyPond mode."
937 (append '("LilyPond")
938 '(["Add index menu" LilyPond-add-imenu-menu
])
939 (list (cons "Insert tag"
940 (cons ["Previously selected" LilyPond-insert-tag-current t
]
942 (mapcar 'LilyPond-menu-keywords
943 (reverse LilyPond-menu-keywords
))))))
945 ["Autocompletion" LilyPond-autocompletion t
]
946 ["(Un)comment Region" LilyPond-comment-region t
]
947 ["Refontify buffer" font-lock-fontify-buffer t
]
949 ["Quick Insert Mode" LilyPond-quick-insert-mode
:keys
"C-c q"]
952 ["LilyPond" LilyPond-info t
]
953 ["LilyPond index-search" LilyPond-info-index-search t
]
954 ["Music Glossary" LilyPond-music-glossary-info t
]
955 ["LilyPond internals" LilyPond-internals-info t
]
959 (defconst LilyPond-imenu-generic-re
"^\\([a-zA-Z]+\\) *="
960 "Regexp matching Identifier definitions.")
962 (defvar LilyPond-imenu-generic-expression
963 (list (list nil LilyPond-imenu-generic-re
1))
964 "Expression for imenu")
966 (defun LilyPond-command-select-master ()
968 (message "Next command will be on the master file")
969 (setq LilyPond-command-current
'LilyPond-command-master
))
971 (defun LilyPond-command-select-buffer ()
973 (message "Next command will be on the buffer")
974 (setq LilyPond-command-current
'LilyPond-command-buffer
))
976 (defun LilyPond-command-select-region ()
978 (message "Next command will be on the region")
979 (setq LilyPond-command-current
'LilyPond-command-region
))
981 (defun LilyPond-command-menu (name)
982 ;; Execute LilyPond-command-alist NAME from a menu.
983 (let ((LilyPond-command-force name
))
984 (if (eq LilyPond-command-current
'LilyPond-command-region
)
985 (if (eq (mark t
) nil
)
986 (progn (message "The mark is not set now") (sit-for 0 500))
987 (progn (if (not (not (LilyPond-mark-active)))
988 (progn (message "Region is not active, using region between inactive mark and current point.") (sit-for 0 500)))
989 (LilyPond-command-region (mark t
) (point))))
990 (funcall LilyPond-command-current
))))
992 (defun LilyPond-add-imenu-menu ()
994 "Add an imenu menu to the menubar."
995 (if (not LilyPond-imenu
)
997 (imenu-add-to-menubar "Index")
998 (redraw-frame (selected-frame))
999 (setq LilyPond-imenu t
))
1000 (message "%s" "LilyPond-imenu already exists.")))
1001 (put 'LilyPond-add-imenu-menu
'menu-enable
'(not LilyPond-imenu
))
1003 (defun LilyPond-mode ()
1004 "Major mode for editing LilyPond music files.
1006 This mode knows about LilyPond keywords and line comments, not about
1007 indentation or block comments. It features easy compilation, error
1008 finding and viewing of a LilyPond source buffer or region.
1011 \\{LilyPond-mode-map}
1014 LilyPond-command-alist\t\talist from name to command
1015 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
1017 ;; set up local variables
1018 (kill-all-local-variables)
1020 (make-local-variable 'font-lock-defaults
)
1021 (setq font-lock-defaults
'(LilyPond-font-lock-keywords))
1023 ;; string and comments are fontified explicitly
1024 (make-local-variable 'font-lock-keywords-only
)
1025 (setq font-lock-keywords-only t
)
1027 ;; Multi-line font-locking needs Emacs 21.1 or newer.
1028 ;; For older versions there is hotkey "C-c f".
1029 (make-local-variable 'font-lock-multiline
)
1030 (setq font-lock-multiline t
)
1032 (make-local-variable 'paragraph-separate
)
1033 (setq paragraph-separate
"^[ \t]*$")
1035 (make-local-variable 'paragraph-start
)
1036 (setq paragraph-start
"^[ \t]*$")
1038 (make-local-variable 'comment-start
)
1039 (setq comment-start
"%")
1041 (make-local-variable 'comment-start-skip
)
1042 (setq comment-start-skip
"%{? *")
1044 (make-local-variable 'comment-end
)
1045 (setq comment-end
"")
1047 (make-local-variable 'block-comment-start
)
1048 (setq block-comment-start
"%{")
1050 (make-local-variable 'block-comment-end
)
1051 (setq block-comment-end
"%}")
1053 (make-local-variable 'indent-line-function
)
1054 (setq indent-line-function
'LilyPond-indent-line
)
1056 (LilyPond-mode-set-syntax-table '(?\
< ?\
> ?\
{ ?\
}))
1057 (setq major-mode
'LilyPond-mode
)
1058 (setq mode-name
"LilyPond")
1059 (setq local-abbrev-table LilyPond-mode-abbrev-table
)
1060 (use-local-map LilyPond-mode-map
)
1062 ;; In XEmacs imenu was synched up with: FSF 20.4
1063 (make-local-variable 'imenu-generic-expression
)
1064 (setq imenu-generic-expression LilyPond-imenu-generic-expression
)
1065 ;; (imenu-add-to-menubar "Index") ; see LilyPond-add-imenu-menu
1067 ;; In XEmacs one needs to use 'easy-menu-add'.
1068 (if (string-match "XEmacs\\|Lucid" emacs-version
)
1070 (easy-menu-add LilyPond-mode-menu
)
1071 (easy-menu-add LilyPond-command-menu
)))
1073 ;; Use Command on Region even for inactive mark (region).
1074 (if (string-match "XEmacs\\|Lucid" emacs-version
)
1075 (setq zmacs-regions nil
)
1076 (setq mark-even-if-inactive t
))
1078 ;; Context dependent syntax tables in LilyPond-mode
1079 (make-local-hook 'post-command-hook
) ; XEmacs requires
1080 (add-hook 'post-command-hook
'LilyPond-mode-context-set-syntax-table nil t
)
1082 ;; Turn on paren-mode buffer-locally, i.e., in LilyPond-mode
1083 (if (string-match "XEmacs\\|Lucid" emacs-version
)
1085 (make-local-variable 'paren-mode
)
1086 (paren-set-mode 'paren
)
1087 (make-local-variable 'blink-matching-paren
)
1088 (setq blink-matching-paren t
)
1091 (make-local-variable 'blink-matching-paren-on-screen
)
1092 (setq blink-matching-paren-on-screen t
)
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