lilypond-1.5.14
[lilypond.git] / lilypond-mode.el
blob565aa0494366d96947e62b827805fa02509af34f
1 ;;;
2 ;;; lilypond-mode.el --- Major mode for editing GNU LilyPond music scores
3 ;;;
4 ;;; source file of the GNU LilyPond music typesetter
5 ;;;
6 ;;; (c) 1999--2001 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;;
8 ;;; Changed 29th Aug 2001 Heikki Junes <heikki.junes@hut.fi>
9 ;;; * Add PS-compilation, PS-viewing and MIDI-play
10 ;;; Changed 12th Sep 2001 Heikki Junes
11 ;;; * Keyboard shortcuts
13 ;;; Inspired on auctex
15 ;;;
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)))
20 ;;;
22 (load-library "lilypond-font-lock")
24 (require 'easymenu)
25 (require 'compile)
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."
48 :type 'string
49 :group 'LilyPond)
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."
56 (let ((found nil)
57 (regexp (concat "\\`\\("
58 (mapconcat (function (lambda (dir)
59 (regexp-quote (expand-file-name dir))))
60 LilyPond-include-path "\\|")
61 "\\).*\\("
62 (mapconcat 'regexp-quote originals "\\|")
63 "\\)\\.\\("
64 (mapconcat 'regexp-quote extensions "\\|")
65 "\\)\\'"))
66 (buffers (buffer-list)))
67 (while buffers
68 (let* ((buffer (car buffers))
69 (name (buffer-file-name buffer)))
70 (setq buffers (cdr buffers))
71 (if (and name (string-match regexp name))
72 (progn
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)
77 "? ")))
78 (save-excursion (set-buffer buffer) (save-buffer)))
79 (if (file-newer-than-file-p name derived)
80 (setq found t))))))
81 found))
83 (defun LilyPond-running ()
84 (let ((process (get-process "lilypond")))
85 (and process
86 (eq (process-status process) 'run))))
88 (defun LilyPond-kill-job ()
89 "Kill the currently running LilyPond job."
90 (interactive)
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)))
111 (if (and
112 (> i 0)
113 (< i (length 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."
122 :group 'LilyPond
123 :type 'string)
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."
137 :group 'LilyPond
138 :type 'string)
140 (defcustom LilyPond-gv-command "gv -watch"
141 "Command used to display PS files."
143 :group 'LilyPond
144 :type 'string)
146 (defcustom LilyPond-midi-command "timidity"
147 "Command used to play MIDI files."
149 :group 'LilyPond
150 :type 'string)
152 ;; This is the major configuration variable.
153 (defcustom LilyPond-command-alist
155 ("LilyPond" . ("lilypond %s" . "TeX"))
156 ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
158 ("2Dvi" . ("ly2dvi %s" . "View"))
159 ("2PS" . ("ly2dvi -P %s" . "View"))
161 ("Book" . ("lilypond-book %x" . "LaTeX"))
162 ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
164 ;; point-n-click (arg: exits upop USR1)
165 ("SmartView" . ("xdvi %d" . "LilyPond"))
167 ;; refreshes when kicked USR1
168 ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
170 ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
172 ("Midi" . (,(concat LilyPond-midi-command " %m") . "LilyPond"))
175 "AList of commands to execute on the current document.
177 The key is the name of the command as it will be presented to the
178 user, the value is a cons of the command string handed to the shell
179 after being expanded, and the next command to be executed upon
180 success. The expansion is done using the information found in
181 LilyPond-expand-list.
183 :group 'LilyPond
184 :type '(repeat (cons :tag "Command Item"
185 (string :tag "Key")
186 (cons :tag "How"
187 (string :tag "Command")
188 (string :tag "Next Key")))))
190 ;; drop this?
191 (defcustom LilyPond-file-extensions '(".ly" ".sly" ".fly")
192 "*File extensions used by manually generated TeX files."
193 :group 'LilyPond
194 :type '(repeat (string :format "%v")))
197 (defcustom LilyPond-expand-alist
199 ("%s" . ".ly")
200 ("%t" . ".tex")
201 ("%d" . ".dvi")
202 ("%p" . ".ps")
203 ("%l" . ".latex")
204 ("%x" . ".tely")
205 ("%m" . ".midi")
208 "Alist of expansion strings for LilyPond command names."
209 :group 'LilyPond
210 :type '(repeat (cons :tag "Alist item"
211 (string :tag "Symbol")
212 (string :tag "Expansion"))))
215 (defcustom LilyPond-command-Show "View"
216 "*The default command to show (view or print) a LilyPond file.
217 Must be the car of an entry in `LilyPond-command-alist'."
218 :group 'LilyPond
219 :type 'string)
220 (make-variable-buffer-local 'LilyPond-command-Show)
222 (defcustom LilyPond-command-Print "Print"
223 "The name of the Print entry in LilyPond-command-Print."
224 :group 'LilyPond
225 :type 'string)
227 (defun xLilyPond-compile-sentinel (process msg)
228 (if (and process
229 (= 0 (process-exit-status process)))
230 (setq LilyPond-command-default
231 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
233 ;; FIXME: shouldn't do this for stray View/xdvi
234 (defun LilyPond-compile-sentinel (buffer msg)
235 (if (string-match "^finished" msg)
236 (setq LilyPond-command-default
237 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
239 ;;(make-variable-buffer-local 'compilation-finish-function)
240 (setq compilation-finish-function 'LilyPond-compile-sentinel)
242 (defun LilyPond-command-query (name)
243 "Query the user for what LilyPond command to use."
244 (let* ((default (cond ((if (string-equal name "emacs-lily")
245 (LilyPond-check-files (concat name ".tex")
246 (list name)
247 LilyPond-file-extensions)
248 ;; FIXME
249 (LilyPond-save-buffer)
250 ;;"LilyPond"
251 LilyPond-command-default))
252 (t LilyPond-command-default)))
254 (answer (or LilyPond-command-force
255 (completing-read
256 (concat "Command: (default " default ") ")
257 LilyPond-command-alist nil t nil 'LilyPond-command-history))))
259 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
260 (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
261 (if (and answer
262 (not (string-equal answer "")))
263 answer
264 default))))
267 ;; FIXME: find ``\score'' in buffers / make settable?
268 (defun LilyPond-master-file ()
269 ;; duh
270 (buffer-file-name))
272 (defun LilyPond-command-master ()
273 "Run command on the current document."
274 (interactive)
275 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
276 'LilyPond-master-file))
278 (defun LilyPond-command-formatdvi ()
279 "Format the dvi output of the current document."
280 (interactive)
281 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
284 (defun LilyPond-command-formatps ()
285 "Format the ps output of the current document."
286 (interactive)
287 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
290 (defun LilyPond-command-smartview ()
291 "View the dvi output of current document."
292 (interactive)
293 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
296 (defun LilyPond-command-view ()
297 "View the dvi output of current document."
298 (interactive)
299 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
302 (defun LilyPond-command-viewps ()
303 "View the ps output of current document."
304 (interactive)
305 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
308 (defun LilyPond-command-midi ()
309 "View the ps output of current document."
310 (interactive)
311 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file)
314 ;; FIXME, this is broken
315 (defun LilyPond-region-file (begin end)
316 (let (
317 ;; (dir "/tmp/")
318 ;; urg
319 (dir "./")
320 (base "emacs-lily")
321 ;; Hmm
322 (ext (if (string-match "^[\\]score" (buffer-substring begin end))
323 ".ly"
324 (if (< 50 (abs (- begin end)))
325 ".fly"
326 ".sly"))))
327 (concat dir base ext)))
329 (defun LilyPond-command-region (begin end)
330 "Run LilyPond on the current region."
331 (interactive "r")
332 (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
333 (LilyPond-command (LilyPond-command-query
334 (LilyPond-region-file begin end))
335 '(lambda () (LilyPond-region-file begin end))))
337 (defun LilyPond-command-buffer ()
338 "Run LilyPond on buffer."
339 (interactive)
340 (LilyPond-command-region (point-min) (point-max)))
342 (defun LilyPond-command-expand (string file)
343 (let ((case-fold-search nil))
344 (if (string-match "%" string)
345 (let* ((b (match-beginning 0))
346 (e (+ b 2))
347 (l (split-file-name file))
348 (dir (car l))
349 (base (cadr l)))
350 (LilyPond-command-expand
351 (concat (substring string 0 b)
353 base
354 (let ((entry (assoc (substring string b e)
355 LilyPond-expand-alist)))
356 (if entry (cdr entry) ""))
357 (substring string e))
358 file))
359 string)))
361 (defun LilyPond-shell-process (name buffer command)
362 (let ((old (current-buffer)))
363 (switch-to-buffer-other-window buffer)
364 ;; If we empty the buffer don't see messages scroll by.
365 ;; (erase-buffer)
367 (start-process-shell-command name buffer command)
368 (switch-to-buffer-other-window old)))
371 (defun LilyPond-command (name file)
372 "Run command NAME on the file you get by calling FILE.
374 FILE is a function return a file name. It has one optional argument,
375 the extension to use on the file.
377 Use the information in LilyPond-command-alist to determine how to run the
378 command."
380 (let ((entry (assoc name LilyPond-command-alist)))
381 (if entry
382 (let ((command (LilyPond-command-expand (cadr entry)
383 (apply file nil))))
384 (if (string-equal name "View")
385 (let ((buffer-xdvi (get-buffer-create "*view*")))
386 (if LilyPond-kick-xdvi
387 (let ((process-xdvi (get-buffer-process buffer-xdvi)))
388 (if process-xdvi
389 (signal-process (process-id process-xdvi) 'SIGUSR1)
390 (LilyPond-shell-process name buffer-xdvi command)))
391 (LilyPond-shell-process name buffer-xdvi command)))
392 (progn
393 (setq LilyPond-command-default name)
394 (LilyPond-compile-file command name)))))))
396 ;; XEmacs stuff
397 ;; Sadly we need this for a macro in Emacs 19.
398 (eval-when-compile
399 ;; Imenu isn't used in XEmacs, so just ignore load errors.
400 (condition-case ()
401 (require 'imenu)
402 (error nil)))
405 ;;; Keymap
407 (defvar LilyPond-mode-map ()
408 "Keymap used in `LilyPond-mode' buffers.")
410 ;; Note: if you make changes to the map, you must do
411 ;; M-x set-variable LilyPond-mode-map nil
412 ;; M-x eval-buffer
413 ;; M-x LilyPond-mode
414 ;; to let the changest take effect
416 (if LilyPond-mode-map
418 (setq LilyPond-mode-map (make-sparse-keymap))
419 (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
420 (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
421 (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-job)
422 (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
423 (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
424 (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
425 (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
426 (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
427 (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
428 (define-key LilyPond-mode-map "\C-c\C-m" 'LilyPond-command-midi)
431 ;;; Menu Support
433 (defun LilyPond-command-menu-entry (entry)
434 ;; Return LilyPond-command-alist ENTRY as a menu item.
435 (let ((name (car entry)))
436 (cond ((and (string-equal name LilyPond-command-Print)
437 LilyPond-printer-list)
438 (let ((command LilyPond-print-command)
439 (lookup 1))
440 (append (list LilyPond-command-Print)
441 (mapcar 'LilyPond-command-menu-printer-entry
442 LilyPond-printer-list))))
444 (vector name (list 'LilyPond-command-menu name) t)))))
447 (easy-menu-define LilyPond-mode-menu
448 LilyPond-mode-map
449 "Menu used in LilyPond mode."
450 (append '("Command")
451 '(("Command on"
452 [ "Master File" LilyPond-command-select-master
453 :keys "C-c C-c" :style radio
454 :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
455 [ "Buffer" LilyPond-command-select-buffer
456 :keys "C-c C-b" :style radio
457 :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
458 [ "Region" LilyPond-command-select-region
459 :keys "C-c C-r" :style radio
460 :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
461 ; (let ((file 'LilyPond-command-on-current))
462 ; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
463 ;;; Some kind of mapping which includes :keys might be more elegant
464 '([ "LilyPond" (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file) ])
465 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
466 '([ "2Dvi" (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file) :keys "C-c C-d"])
467 '([ "2PS" (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file) :keys "C-c C-f"])
468 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
469 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
470 '([ "SmartView" (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file) :keys "C-c C-s"])
471 '([ "View" (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file) :keys "C-c C-v"])
472 '([ "ViewPS" (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file) :keys "C-c C-p"])
473 '([ "Midi" (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file) :keys "C-c C-m"])
477 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z_][a-zA-Z0-9_]*\\) *="
478 "Regexp matching Identifier definitions.")
480 (defvar LilyPond-imenu-generic-expression
481 (list (list nil LilyPond-imenu-generic-re 1))
482 "Expression for imenu")
484 (defun LilyPond-command-select-master ()
485 (interactive)
486 (message "Next command will be on the master file")
487 (setq LilyPond-command-current 'LilyPond-command-master))
489 (defun LilyPond-command-select-buffer ()
490 (interactive)
491 (message "Next command will be on the buffer")
492 (setq LilyPond-command-current 'LilyPond-command-buffer))
494 (defun LilyPond-command-select-region ()
495 (interactive)
496 (message "Next command will be on the region")
497 (setq LilyPond-command-current 'LilyPond-command-region))
499 (defun LilyPond-command-menu (name)
500 ;; Execute LilyPond-command-alist NAME from a menu.
501 (let ((LilyPond-command-force name))
502 (funcall LilyPond-command-current)))
504 (defun LilyPond-mode ()
505 "Major mode for editing LilyPond music files.
507 This mode knows about LilyPond keywords and line comments, not about
508 indentation or block comments. It features easy compilation, error
509 finding and viewing of a LilyPond source buffer or region.
511 COMMANDS
512 \\{LilyPond-mode-map}
513 VARIABLES
515 LilyPond-command-alist\t\talist from name to command
516 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
517 (interactive)
518 ;; set up local variables
519 (kill-all-local-variables)
521 (make-local-variable 'font-lock-defaults)
522 (setq font-lock-defaults '(LilyPond-font-lock-keywords))
524 (make-local-variable 'paragraph-separate)
525 (setq paragraph-separate "^[ \t]*$")
527 (make-local-variable 'paragraph-start)
528 (setq paragraph-start "^[ \t]*$")
530 (make-local-variable 'comment-start)
531 (setq comment-start "%")
533 (make-local-variable 'comment-start-skip)
534 (setq comment-start-skip "%{? *")
536 (make-local-variable 'comment-end)
537 (setq comment-end "")
539 (make-local-variable 'block-comment-start)
540 (setq block-comment-start "%{")
542 (make-local-variable 'block-comment-end)
543 (setq block-comment-end "%}")
545 (make-local-variable 'indent-line-function)
546 (setq indent-line-function 'indent-relative-maybe)
548 (set-syntax-table LilyPond-mode-syntax-table)
549 (setq major-mode 'LilyPond-mode)
550 (setq mode-name "LilyPond")
551 (setq local-abbrev-table LilyPond-mode-abbrev-table)
552 (use-local-map LilyPond-mode-map)
554 (make-local-variable 'imenu-generic-expression)
555 (setq imenu-generic-expression LilyPond-imenu-generic-expression)
556 (imenu-add-to-menubar "Index")
558 ;; run the mode hook. LilyPond-mode-hook use is deprecated
559 (run-hooks 'LilyPond-mode-hook))
561 (defun LilyPond-version ()
562 "Echo the current version of `LilyPond-mode' in the minibuffer."
563 (interactive)
564 (message "Using `LilyPond-mode' version %s" LilyPond-version))
566 (provide 'lilypond-mode)
567 ;;; lilypond-mode.el ends here