2 ;;; lilypond-mode.el --- Major mode for editing GNU LilyPond music scores
4 ;;; source file of the GNU LilyPond music typesetter
6 ;;; (c) 1999--2001 Jan Nieuwenhuizen <janneke@gnu.org>
8 ;;; Changed 2001 Heikki Junes <heikki.junes@hut.fi>
9 ;;; * Add PS-compilation, PS-viewing and MIDI-play (29th Aug 2001)
10 ;;; * Keyboard shortcuts (12th Sep 2001)
11 ;;; * Inserting tags, inspired on sgml-mode (11th Oct 2001)
13 ;;; Inspired on auctex
16 ;;; Add this to your ~/.emacs or ~/.emacs.el
17 ;;; (load-library "lilypond-mode.el")
18 ;;; (setq auto-mode-alist
19 ;;; (append '(("\\.ly$" . LilyPond-mode) auto-mode-alist)))
22 (load-library "lilypond-font-lock")
27 (defconst LilyPond-version "1.3.143"
28 "`LilyPond-mode' version number.")
30 (defconst LilyPond-help-address "bug-gnu-music@gnu.org"
31 "Address accepting submission of bug reports.")
33 (defvar LilyPond-mode-hook nil
34 "*Hook called by `LilyPond-mode'.")
36 (defvar LilyPond-kick-xdvi nil
37 "If true, no simultaneous xdvi's are started, but reload signal is sent.")
39 (defvar LilyPond-command-history nil
40 "Command history list.")
42 (defvar LilyPond-regexp-alist
43 '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
44 "Regexp used to match LilyPond errors. See `compilation-error-regexp-alist'.")
46 (defcustom LilyPond-include-path ".:/tmp"
47 "* LilyPond include path."
52 (defun LilyPond-check-files (derived originals extensions)
53 "Check that DERIVED is newer than any of the ORIGINALS.
54 Try each original with each member of EXTENSIONS, in all directories
55 in LilyPond-include-path."
57 (regexp (concat "\\`\\("
58 (mapconcat (function (lambda (dir)
59 (regexp-quote (expand-file-name dir))))
60 LilyPond-include-path "\\|")
62 (mapconcat 'regexp-quote originals "\\|")
64 (mapconcat 'regexp-quote extensions "\\|")
66 (buffers (buffer-list)))
68 (let* ((buffer (car buffers))
69 (name (buffer-file-name buffer)))
70 (setq buffers (cdr buffers))
71 (if (and name (string-match regexp name))
73 (and (buffer-modified-p buffer)
74 (or (not LilyPond-save-query)
75 (y-or-n-p (concat "Save file "
76 (buffer-file-name buffer)
78 (save-excursion (set-buffer buffer) (save-buffer)))
79 (if (file-newer-than-file-p name derived)
83 (defun LilyPond-running ()
84 (let ((process (get-process "lilypond")))
86 (eq (process-status process) 'run))))
88 (defun LilyPond-kill-job ()
89 "Kill the currently running LilyPond job."
91 ;; What bout TeX, Xdvi?
92 (quit-process (get-process "lilypond") t))
94 ;; URG, should only run LilyPond-compile for LilyPond
95 ;; not for tex,xdvi (ly2dvi?)
96 (defun LilyPond-compile-file (command name)
97 ;; We maybe should know what we run here (Lily, ly2dvi, tex)
98 ;; and adjust our error-matching regex ?
99 (compile-internal command "No more errors" name ))
101 ;; do we still need this, now that we're using compile-internal?
102 (defun LilyPond-save-buffer ()
103 (if (buffer-modified-p) (save-buffer)))
105 ;;; return (dir base ext)
106 (defun split-file-name (name)
107 (let* ((i (string-match "[^/]*$" name))
108 (dir (if (> i 0) (substring name 0 i) "./"))
109 (file (substring name i (length name)))
110 (i (string-match "[^.]*$" file)))
114 (list dir (substring file 0 (- i 1)) (substring file i (length file)))
115 (list dir file ""))))
118 ;; Should check whether in command-alist?
119 (defcustom LilyPond-command-default "LilyPond"
120 "Default command. Must identify a member of LilyPond-command-alist."
124 ;;;(make-variable-buffer-local 'LilyPond-command-last)
126 (defvar LilyPond-command-current 'LilyPond-command-master)
127 ;;;(make-variable-buffer-local 'LilyPond-command-master)
130 ;; If non-nil, LilyPond-command-query will return the value of this
131 ;; variable instead of quering the user.
132 (defvar LilyPond-command-force nil)
134 (defcustom LilyPond-xdvi-command "xdvik"
135 "Command used to display DVI files."
140 (defcustom LilyPond-gv-command "gv -watch"
141 "Command used to display PS files."
146 (defcustom LilyPond-gv-command "gv -watch"
147 "Command used to display PS files."
152 (defcustom LilyPond-midi-command "timidity"
153 "Command used to play MIDI files."
158 ;; This is the major configuration variable.
159 (defcustom LilyPond-command-alist
161 ("LilyPond" . ("lilypond %s" . "TeX"))
162 ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
164 ("2Dvi" . ("ly2dvi %s" . "View"))
165 ("2PS" . ("ly2dvi -P %s" . "View"))
167 ("Book" . ("lilypond-book %x" . "LaTeX"))
168 ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
170 ;; point-n-click (arg: exits upop USR1)
171 ("SmartView" . ("xdvi %d" . "LilyPond"))
173 ;; refreshes when kicked USR1
174 ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
176 ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
178 ("Midi" . (,(concat LilyPond-midi-command " %m") . "LilyPond"))
181 "AList of commands to execute on the current document.
183 The key is the name of the command as it will be presented to the
184 user, the value is a cons of the command string handed to the shell
185 after being expanded, and the next command to be executed upon
186 success. The expansion is done using the information found in
187 LilyPond-expand-list.
190 :type '(repeat (cons :tag "Command Item"
193 (string :tag "Command")
194 (string :tag "Next Key")))))
197 (defcustom LilyPond-file-extensions '(".ly" ".sly" ".fly")
198 "*File extensions used by manually generated TeX files."
200 :type '(repeat (string :format "%v")))
203 (defcustom LilyPond-expand-alist
214 "Alist of expansion strings for LilyPond command names."
216 :type '(repeat (cons :tag "Alist item"
217 (string :tag "Symbol")
218 (string :tag "Expansion"))))
221 (defcustom LilyPond-command-Show "View"
222 "*The default command to show (view or print) a LilyPond file.
223 Must be the car of an entry in `LilyPond-command-alist'."
226 (make-variable-buffer-local 'LilyPond-command-Show)
228 (defcustom LilyPond-command-Print "Print"
229 "The name of the Print entry in LilyPond-command-Print."
233 (defun xLilyPond-compile-sentinel (process msg)
235 (= 0 (process-exit-status process)))
236 (setq LilyPond-command-default
237 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
239 ;; FIXME: shouldn't do this for stray View/xdvi
240 (defun LilyPond-compile-sentinel (buffer msg)
241 (if (string-match "^finished" msg)
242 (setq LilyPond-command-default
243 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
245 ;;(make-variable-buffer-local 'compilation-finish-function)
246 (setq compilation-finish-function 'LilyPond-compile-sentinel)
248 (defun LilyPond-command-query (name)
249 "Query the user for what LilyPond command to use."
250 (let* ((default (cond ((if (string-equal name "emacs-lily")
251 (LilyPond-check-files (concat name ".tex")
253 LilyPond-file-extensions)
255 (LilyPond-save-buffer)
257 LilyPond-command-default))
258 (t LilyPond-command-default)))
260 (answer (or LilyPond-command-force
262 (concat "Command: (default " default ") ")
263 LilyPond-command-alist nil t nil 'LilyPond-command-history))))
265 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
266 (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
268 (not (string-equal answer "")))
273 ;; FIXME: find ``\score'' in buffers / make settable?
274 (defun LilyPond-master-file ()
278 (defun LilyPond-command-master ()
279 "Run command on the current document."
281 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
282 'LilyPond-master-file))
284 (defun LilyPond-command-formatdvi ()
285 "Format the dvi output of the current document."
287 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
290 (defun LilyPond-command-formatps ()
291 "Format the ps output of the current document."
293 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
296 (defun LilyPond-command-smartview ()
297 "View the dvi output of current document."
299 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
302 (defun LilyPond-command-view ()
303 "View the dvi output of current document."
305 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
308 (defun LilyPond-command-viewps ()
309 "View the ps output of current document."
311 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
314 (defun LilyPond-command-midi ()
315 "Play midi corresponding to the current document."
317 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file)
320 (defun count-rexp (start end rexp)
321 "Print number of found regular expressions in the region."
325 (narrow-to-region start end)
326 (goto-char (point-min))
327 (count-matches rexp))))
329 (defun count-midi-words ()
330 "Print number of scores before the curser."
332 (count-rexp (point-min) (point-max) "\\\\midi"))
334 (defun count-midi-words-backwards ()
335 "Print number of scores before the curser."
337 (count-rexp (point-min) (point) "\\\\midi"))
339 (defun LilyPond-command-next-midi ()
340 "Play next midi score of the current document."
342 (LilyPond-compile-file
343 (let ((allscores (count-midi-words))
344 (scores (count-midi-words-backwards))
345 (fname (LilyPond-master-file)))
346 (let ((count (string-to-number (substring scores 0 (+ (length scores) -12)))))
347 (concat LilyPond-midi-command " "
348 (substring fname 0 (+ (length fname) -3)) ; suppose ".ly"
349 (if (not (string= "1 occurrences" allscores)) ; only one score
350 (if (not (eq count 0)) ; first score
351 (if (string= scores allscores) ; last score
352 (concat "-" (number-to-string (+ count -1)))
353 (concat "-" (number-to-string count)))))
357 ;; FIXME, this is broken
358 (defun LilyPond-region-file (begin end)
365 (ext (if (string-match "^[\\]score" (buffer-substring begin end))
367 (if (< 50 (abs (- begin end)))
370 (concat dir base ext)))
372 (defun LilyPond-command-region (begin end)
373 "Run LilyPond on the current region."
375 (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
376 (LilyPond-command (LilyPond-command-query
377 (LilyPond-region-file begin end))
378 '(lambda () (LilyPond-region-file begin end))))
380 (defun LilyPond-command-buffer ()
381 "Run LilyPond on buffer."
383 (LilyPond-command-region (point-min) (point-max)))
385 (defun LilyPond-command-expand (string file)
386 (let ((case-fold-search nil))
387 (if (string-match "%" string)
388 (let* ((b (match-beginning 0))
390 (l (split-file-name file))
393 (LilyPond-command-expand
394 (concat (substring string 0 b)
397 (let ((entry (assoc (substring string b e)
398 LilyPond-expand-alist)))
399 (if entry (cdr entry) ""))
400 (substring string e))
404 (defun LilyPond-shell-process (name buffer command)
405 (let ((old (current-buffer)))
406 (switch-to-buffer-other-window buffer)
407 ;; If we empty the buffer don't see messages scroll by.
410 (start-process-shell-command name buffer command)
411 (switch-to-buffer-other-window old)))
414 (defun LilyPond-command (name file)
415 "Run command NAME on the file you get by calling FILE.
417 FILE is a function return a file name. It has one optional argument,
418 the extension to use on the file.
420 Use the information in LilyPond-command-alist to determine how to run the
423 (let ((entry (assoc name LilyPond-command-alist)))
425 (let ((command (LilyPond-command-expand (cadr entry)
427 (if (string-equal name "View")
428 (let ((buffer-xdvi (get-buffer-create "*view*")))
429 (if LilyPond-kick-xdvi
430 (let ((process-xdvi (get-buffer-process buffer-xdvi)))
432 (signal-process (process-id process-xdvi) 'SIGUSR1)
433 (LilyPond-shell-process name buffer-xdvi command)))
434 (LilyPond-shell-process name buffer-xdvi command)))
436 (setq LilyPond-command-default name)
437 (LilyPond-compile-file command name)))))))
440 ;; Sadly we need this for a macro in Emacs 19.
442 ;; Imenu isn't used in XEmacs, so just ignore load errors.
450 (defvar LilyPond-mode-map ()
451 "Keymap used in `LilyPond-mode' buffers.")
453 ;; Note: if you make changes to the map, you must do
454 ;; M-x set-variable LilyPond-mode-map nil
457 ;; to let the changest take effect
459 (if LilyPond-mode-map
461 (setq LilyPond-mode-map (make-sparse-keymap))
462 (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
463 (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
464 (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-job)
465 (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
466 (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
467 (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
468 (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
469 (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
470 (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
471 (define-key LilyPond-mode-map "\C-c\C-m" 'LilyPond-command-next-midi)
472 (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
473 (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
478 (define-skeleton LilyPond-insert-tag-notes
479 "LilyPond notes tag."
481 ; (if (bolp) nil ?\n)
483 (if (y-or-n-p "Set \"\\relative\" attribute? ")
484 (concat " \\relative " (skeleton-read "Relative: " "" str)))
487 (define-skeleton LilyPond-insert-tag-score
488 "LilyPond score tag."
494 (if (y-or-n-p "Insert \"\\header\" field? ")
495 (concat " \\header {\n "
496 (skeleton-read "Piece: " "piece = " str) "\n"
497 (if (y-or-n-p "Insert \"opus\" field? ")
498 (concat " " (skeleton-read "Opus: " "opus = " str) "\n"))
500 (if (y-or-n-p "Insert \"\\midi\" field? ")
502 (skeleton-read "Midi: " "\\tempo 4 = " str)
506 (defun LilyPond-command-menu-entry (entry)
507 ;; Return LilyPond-command-alist ENTRY as a menu item.
508 (let ((name (car entry)))
509 (cond ((and (string-equal name LilyPond-command-Print)
510 LilyPond-printer-list)
511 (let ((command LilyPond-print-command)
513 (append (list LilyPond-command-Print)
514 (mapcar 'LilyPond-command-menu-printer-entry
515 LilyPond-printer-list))))
517 (vector name (list 'LilyPond-command-menu name) t)))))
520 (easy-menu-define LilyPond-mode-menu
522 "Menu used in LilyPond mode."
525 [ "Master File" LilyPond-command-select-master
526 :keys "C-c C-c" :style radio
527 :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
528 [ "Buffer" LilyPond-command-select-buffer
529 :keys "C-c C-b" :style radio
530 :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
531 [ "Region" LilyPond-command-select-region
532 :keys "C-c C-r" :style radio
533 :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
535 [ "\\notes..." LilyPond-insert-tag-notes
537 [ "\\score..." LilyPond-insert-tag-score
540 ; (let ((file 'LilyPond-command-on-current))
541 ; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
542 ;;; Some kind of mapping which includes :keys might be more elegant
543 '([ "LilyPond" (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file) ])
544 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
545 '([ "2Dvi" (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file) :keys "C-c C-d"])
546 '([ "2PS" (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file) :keys "C-c C-f"])
547 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
548 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
549 '([ "SmartView" (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file) :keys "C-c C-s"])
550 '([ "View" (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file) :keys "C-c C-v"])
551 '([ "ViewPS" (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file) :keys "C-c C-p"])
552 '([ "Midi" (LilyPond-command-next-midi) :keys "C-c C-m"])
555 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z_][a-zA-Z0-9_]*\\) *="
556 "Regexp matching Identifier definitions.")
558 (defvar LilyPond-imenu-generic-expression
559 (list (list nil LilyPond-imenu-generic-re 1))
560 "Expression for imenu")
562 (defun LilyPond-command-select-master ()
564 (message "Next command will be on the master file")
565 (setq LilyPond-command-current 'LilyPond-command-master))
567 (defun LilyPond-command-select-buffer ()
569 (message "Next command will be on the buffer")
570 (setq LilyPond-command-current 'LilyPond-command-buffer))
572 (defun LilyPond-command-select-region ()
574 (message "Next command will be on the region")
575 (setq LilyPond-command-current 'LilyPond-command-region))
577 (defun LilyPond-command-menu (name)
578 ;; Execute LilyPond-command-alist NAME from a menu.
579 (let ((LilyPond-command-force name))
580 (funcall LilyPond-command-current)))
582 (defun LilyPond-mode ()
583 "Major mode for editing LilyPond music files.
585 This mode knows about LilyPond keywords and line comments, not about
586 indentation or block comments. It features easy compilation, error
587 finding and viewing of a LilyPond source buffer or region.
590 \\{LilyPond-mode-map}
593 LilyPond-command-alist\t\talist from name to command
594 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
596 ;; set up local variables
597 (kill-all-local-variables)
599 (make-local-variable 'font-lock-defaults)
600 (setq font-lock-defaults '(LilyPond-font-lock-keywords))
602 (make-local-variable 'paragraph-separate)
603 (setq paragraph-separate "^[ \t]*$")
605 (make-local-variable 'paragraph-start)
606 (setq paragraph-start "^[ \t]*$")
608 (make-local-variable 'comment-start)
609 (setq comment-start "%")
611 (make-local-variable 'comment-start-skip)
612 (setq comment-start-skip "%{? *")
614 (make-local-variable 'comment-end)
615 (setq comment-end "")
617 (make-local-variable 'block-comment-start)
618 (setq block-comment-start "%{")
620 (make-local-variable 'block-comment-end)
621 (setq block-comment-end "%}")
623 (make-local-variable 'indent-line-function)
624 (setq indent-line-function 'indent-relative-maybe)
626 (set-syntax-table LilyPond-mode-syntax-table)
627 (setq major-mode 'LilyPond-mode)
628 (setq mode-name "LilyPond")
629 (setq local-abbrev-table LilyPond-mode-abbrev-table)
630 (use-local-map LilyPond-mode-map)
632 (make-local-variable 'imenu-generic-expression)
633 (setq imenu-generic-expression LilyPond-imenu-generic-expression)
634 (imenu-add-to-menubar "Index")
636 ;; run the mode hook. LilyPond-mode-hook use is deprecated
637 (run-hooks 'LilyPond-mode-hook))
639 (defun LilyPond-version ()
640 "Echo the current version of `LilyPond-mode' in the minibuffer."
642 (message "Using `LilyPond-mode' version %s" LilyPond-version))
644 (provide 'lilypond-mode)
645 ;;; lilypond-mode.el ends here