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>
11 ;;; Add this to your .emacs.el
12 ;;; (load-library "lilypond-mode.el")
13 ;;; (setq auto-mode-alist
14 ;;; (append '(("\\.ly$" . LilyPond-mode) auto-mode-alist)))
17 (load-library "lilypond-font-lock")
22 (defconst LilyPond-version
"1.3.143"
23 "`LilyPond-mode' version number.")
25 (defconst LilyPond-help-address
"bug-gnu-music@gnu.org"
26 "Address accepting submission of bug reports.")
28 (defvar LilyPond-mode-hook nil
29 "*Hook called by `LilyPond-mode'.")
31 (defvar LilyPond-kick-xdvi nil
32 "If true, no simultaneous xdvi's are started, but reload signal is sent.")
34 (defvar LilyPond-regexp-alist
35 '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
36 "Regexp used to match LilyPond errors. See `compilation-error-regexp-alist'.")
38 (defcustom LilyPond-include-path
".:/tmp"
39 "* LilyPond include path."
44 (defun LilyPond-check-files (derived originals extensions
)
45 "Check that DERIVED is newer than any of the ORIGINALS.
46 Try each original with each member of EXTENSIONS, in all directories
47 in LilyPond-include-path."
49 (regexp (concat "\\`\\("
50 (mapconcat (function (lambda (dir)
51 (regexp-quote (expand-file-name dir
))))
52 LilyPond-include-path
"\\|")
54 (mapconcat 'regexp-quote originals
"\\|")
56 (mapconcat 'regexp-quote extensions
"\\|")
58 (buffers (buffer-list)))
60 (let* ((buffer (car buffers
))
61 (name (buffer-file-name buffer
)))
62 (setq buffers
(cdr buffers
))
63 (if (and name
(string-match regexp name
))
65 (and (buffer-modified-p buffer
)
66 (or (not LilyPond-save-query
)
67 (y-or-n-p (concat "Save file "
68 (buffer-file-name buffer
)
70 (save-excursion (set-buffer buffer
) (save-buffer)))
71 (if (file-newer-than-file-p name derived
)
75 (defun LilyPond-running ()
76 (let ((process (get-process "lilypond")))
78 (eq (process-status process
) 'run
))))
80 (defun LilyPond-kill-job ()
81 "Kill the currently running LilyPond job."
83 ;; What bout TeX, Xdvi?
84 (quit-process (get-process "lilypond") t
))
86 ;; URG, should only run LilyPond-compile for LilyPond
87 ;; not for tex,xdvi (ly2dvi?)
88 (defun LilyPond-compile-file (command name
)
89 ;; We maybe should know what we run here (Lily, ly2dvi, tex)
90 ;; and adjust our error-matching regex ?
91 (compile-internal command
"No more errors" name
))
93 ;; do we still need this, now that we're using compile-internal?
94 (defun LilyPond-save-buffer ()
95 (if (buffer-modified-p) (save-buffer)))
97 ;;; return (dir base ext)
98 (defun split-file-name (name)
99 (let* ((i (string-match "[^/]*$" name
))
100 (dir (if (> i
0) (substring name
0 i
) "./"))
101 (file (substring name i
(length name
)))
102 (i (string-match "[^.]*$" file
)))
106 (list dir
(substring file
0 (- i
1)) (substring file i
(length file
)))
107 (list dir file
""))))
110 ;; Should check whether in command-alist?
111 (defcustom LilyPond-command-default
"LilyPond"
112 "Default command. Must identify a member of LilyPond-command-alist."
116 ;;;(make-variable-buffer-local 'LilyPond-command-last)
118 (defvar LilyPond-command-current
'LilyPond-command-master
)
119 ;;;(make-variable-buffer-local 'LilyPond-command-master)
122 ;; If non-nil, LilyPond-command-query will return the value of this
123 ;; variable instead of quering the user.
124 (defvar LilyPond-command-force nil
)
126 (defcustom LilyPond-xdvi-command
"xdvik"
127 "Command used to display DVI files."
132 ;; This is the major configuration variable.
133 (defcustom LilyPond-command-alist
135 ("LilyPond" .
("lilypond %s" .
"TeX"))
136 ("TeX" .
("tex '\\nonstopmode\\input %t'" .
"View"))
138 ("2Dvi" .
("ly2dvi %s" .
"View"))
140 ("Book" .
("lilypond-book %x" .
"LaTeX"))
141 ("LaTeX" .
("latex '\\nonstopmode\\input %l'" .
"View"))
143 ;; point-n-click (arg: exits upop USR1)
144 ("SmartView" .
("xdvi %d" .
"LilyPond"))
146 ;; refreshes when kicked USR1
147 ("View" .
(,(concat LilyPond-xdvi-command
" %d") .
"LilyPond"))
150 "AList of commands to execute on the current document.
152 The key is the name of the command as it will be presented to the
153 user, the value is a cons of the command string handed to the shell
154 after being expanded, and the next command to be executed upon
155 success. The expansion is done using the information found in
156 LilyPond-expand-list.
159 :type
'(repeat (cons :tag
"Command Item"
162 (string :tag
"Command")
163 (string :tag
"Next Key")))))
166 (defcustom LilyPond-file-extensions
'(".ly" ".sly" ".fly")
167 "*File extensions used by manually generated TeX files."
169 :type
'(repeat (string :format
"%v")))
172 (defcustom LilyPond-expand-alist
182 "Alist of expansion strings for LilyPond command names."
184 :type
'(repeat (cons :tag
"Alist item"
185 (string :tag
"Symbol")
186 (string :tag
"Expansion"))))
189 (defcustom LilyPond-command-Show
"View"
190 "*The default command to show (view or print) a LilyPond file.
191 Must be the car of an entry in `LilyPond-command-alist'."
194 (make-variable-buffer-local 'LilyPond-command-Show
)
196 (defcustom LilyPond-command-Print
"Print"
197 "The name of the Print entry in LilyPond-command-Print."
201 (defun xLilyPond-compile-sentinel (process msg
)
203 (= 0 (process-exit-status process
)))
204 (setq LilyPond-command-default
205 (cddr (assoc LilyPond-command-default LilyPond-command-alist
)))))
207 ;; FIXME: shouldn't do this for stray View/xdvi
208 (defun LilyPond-compile-sentinel (buffer msg
)
209 (if (string-match "^finished" msg
)
210 (setq LilyPond-command-default
211 (cddr (assoc LilyPond-command-default LilyPond-command-alist
)))))
213 ;;(make-variable-buffer-local 'compilation-finish-function)
214 (setq compilation-finish-function
'LilyPond-compile-sentinel
)
216 (defun LilyPond-command-query (name)
217 "Query the user for what LilyPond command to use."
218 (let* ((default (cond ((if (string-equal name
"emacs-lily")
219 (LilyPond-check-files (concat name
".tex")
221 LilyPond-file-extensions
)
223 (LilyPond-save-buffer)
225 LilyPond-command-default
))
226 (t LilyPond-command-default
)))
228 (answer (or LilyPond-command-force
230 (concat "Command: (default " default
") ")
231 LilyPond-command-alist nil t
))))
233 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
234 (let ((answer (car-safe (assoc answer LilyPond-command-alist
))))
236 (not (string-equal answer
"")))
241 ;; FIXME: find ``\score'' in buffers / make settable?
242 (defun LilyPond-master-file ()
246 (defun LilyPond-command-master ()
247 "Run command on the current document."
249 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
250 'LilyPond-master-file
))
252 ;; FIXME, this is broken
253 (defun LilyPond-region-file (begin end
)
260 (ext (if (string-match "^[\\]score" (buffer-substring begin end
))
262 (if (< 50 (abs (- begin end
)))
265 (concat dir base ext
)))
267 (defun LilyPond-command-region (begin end
)
268 "Run LilyPond on the current region."
270 (write-region begin end
(LilyPond-region-file begin end
) nil
'nomsg
)
271 (LilyPond-command (LilyPond-command-query
272 (LilyPond-region-file begin end
))
273 '(lambda () (LilyPond-region-file begin end
))))
275 (defun LilyPond-command-buffer ()
276 "Run LilyPond on buffer."
278 (LilyPond-command-region (point-min) (point-max)))
280 (defun LilyPond-command-expand (string file
)
281 (let ((case-fold-search nil
))
282 (if (string-match "%" string
)
283 (let* ((b (match-beginning 0))
285 (l (split-file-name file
))
288 (LilyPond-command-expand
289 (concat (substring string
0 b
)
292 (let ((entry (assoc (substring string b e
)
293 LilyPond-expand-alist
)))
294 (if entry
(cdr entry
) ""))
295 (substring string e
))
299 (defun LilyPond-shell-process (name buffer command
)
300 (let ((old (current-buffer)))
301 (switch-to-buffer-other-window buffer
)
302 ;; Hmm, if we goto the end of the buffer, we don't see
303 ;; the messages scroll by. I don't know how to fix this,
304 ;; so let's emty the buffer, then
305 ;;(goto-char (point-max))
307 (goto-char (point-min))
308 (start-process-shell-command name buffer command
)
309 (switch-to-buffer-other-window old
)))
312 (defun LilyPond-command (name file
)
313 "Run command NAME on the file you get by calling FILE.
315 FILE is a function return a file name. It has one optional argument,
316 the extension to use on the file.
318 Use the information in LilyPond-command-alist to determine how to run the
321 (let ((entry (assoc name LilyPond-command-alist
)))
323 (let ((command (LilyPond-command-expand (cadr entry
)
325 (if (string-equal name
"View")
326 (let ((buffer-xdvi (get-buffer-create "*view*")))
327 (if LilyPond-kick-xdvi
328 (let ((process-xdvi (get-buffer-process buffer-xdvi
)))
330 ;; Don't open new xdvi window, but force redisplay
331 ;; We could make this an option.
332 (signal-process (process-id process-xdvi
) 'SIGUSR1
)
333 (LilyPond-shell-process name buffer-xdvi command
)))
334 (LilyPond-shell-process name buffer-xdvi command
)))
336 (setq LilyPond-command-default name
)
337 (LilyPond-compile-file command name
)))))))
340 ;; Sadly we need this for a macro in Emacs 19.
342 ;; Imenu isn't used in XEmacs, so just ignore load errors.
350 (defvar LilyPond-mode-map
()
351 "Keymap used in `LilyPond-mode' buffers.")
353 ;; Note: if you make changes to the map, you must do
354 ;; M-x set-variable LilyPond-mode-map nil
357 ;; to let the changest take effect
359 (if LilyPond-mode-map
361 (setq LilyPond-mode-map
(make-sparse-keymap))
362 (define-key LilyPond-mode-map
"\C-c\C-r" 'LilyPond-command-region
)
363 (define-key LilyPond-mode-map
"\C-c\C-b" 'LilyPond-command-buffer
)
364 (define-key LilyPond-mode-map
"\C-c\C-k" 'LilyPond-kill-job
)
365 (define-key LilyPond-mode-map
"\C-c\C-c" 'LilyPond-command-master
)
370 (defun LilyPond-command-menu-entry (entry)
371 ;; Return LilyPond-command-alist ENTRY as a menu item.
372 (let ((name (car entry
)))
373 (cond ((and (string-equal name LilyPond-command-Print
)
374 LilyPond-printer-list
)
375 (let ((command LilyPond-print-command
)
377 (append (list LilyPond-command-Print
)
378 (mapcar 'LilyPond-command-menu-printer-entry
379 LilyPond-printer-list
))))
381 (vector name
(list 'LilyPond-command-menu name
) t
)))))
384 (easy-menu-define LilyPond-mode-menu
386 "Menu used in LilyPond mode."
389 [ "Master File" LilyPond-command-select-master
390 :keys
"C-c C-c" :style radio
391 :selected
(eq LilyPond-command-current
'LilyPond-command-master
) ]
392 [ "Buffer" LilyPond-command-select-buffer
393 :keys
"C-c C-b" :style radio
394 :selected
(eq LilyPond-command-current
'LilyPond-command-buffer
) ]
395 [ "Region" LilyPond-command-select-region
396 :keys
"C-c C-r" :style radio
397 :selected
(eq LilyPond-command-current
'LilyPond-command-region
) ]))
398 (let ((file 'LilyPond-command-on-current
))
399 (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist
))))
402 (defconst LilyPond-imenu-generic-re
"^\\([a-zA-Z_][a-zA-Z0-9_]*\\) *="
403 "Regexp matching Identifier definitions.")
405 (defvar LilyPond-imenu-generic-expression
406 (list (list nil LilyPond-imenu-generic-re
1))
407 "Expression for imenu")
409 (defun LilyPond-command-select-master ()
411 (message "Next command will be on the master file")
412 (setq LilyPond-command-current
'LilyPond-command-master
))
414 (defun LilyPond-command-select-buffer ()
416 (message "Next command will be on the buffer")
417 (setq LilyPond-command-current
'LilyPond-command-buffer
))
419 (defun LilyPond-command-select-region ()
421 (message "Next command will be on the region")
422 (setq LilyPond-command-current
'LilPond-command-region
))
424 (defun LilyPond-command-menu (name)
425 ;; Execute LilyPond-command-alist NAME from a menu.
426 (let ((LilyPond-command-force name
))
427 (funcall LilyPond-command-current
)))
429 (defun LilyPond-mode ()
430 "Major mode for editing LilyPond music files.
432 This mode knows about LilyPond keywords and line comments, not about
433 indentation or block comments. It features easy compilation, error
434 finding and viewing of a LilyPond source buffer or region.
437 \\{LilyPond-mode-map}
440 LilyPond-command-alist\t\talist from name to command
441 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
443 ;; set up local variables
444 (kill-all-local-variables)
446 (make-local-variable 'font-lock-defaults
)
447 (setq font-lock-defaults
'(LilyPond-font-lock-keywords))
449 (make-local-variable 'paragraph-separate
)
450 (setq paragraph-separate
"^[ \t]*$")
452 (make-local-variable 'paragraph-start
)
453 (setq paragraph-start
"^[ \t]*$")
455 (make-local-variable 'comment-start
)
456 (setq comment-start
"%")
458 (make-local-variable 'comment-start-skip
)
459 (setq comment-start-skip
"%{? *")
461 (make-local-variable 'comment-end
)
462 (setq comment-end
"\n")
464 (make-local-variable 'block-comment-start
)
465 (setq block-comment-start
"%{")
467 (make-local-variable 'block-comment-end
)
468 (setq block-comment-end
"%}")
470 (make-local-variable 'indent-line-function
)
471 (setq indent-line-function
'indent-relative-maybe
)
473 (set-syntax-table LilyPond-mode-syntax-table
)
474 (setq major-mode
'LilyPond-mode
)
475 (setq mode-name
"LilyPond")
476 (setq local-abbrev-table LilyPond-mode-abbrev-table
)
477 (use-local-map LilyPond-mode-map
)
479 (make-local-variable 'imenu-generic-expression
)
480 (setq imenu-generic-expression LilyPond-imenu-generic-expression
)
481 (imenu-add-to-menubar "Index")
483 ;; run the mode hook. LilyPond-mode-hook use is deprecated
484 (run-hooks 'LilyPond-mode-hook
))
486 (defun LilyPond-version ()
487 "Echo the current version of `LilyPond-mode' in the minibuffer."
489 (message "Using `LilyPond-mode' version %s" LilyPond-version
))
491 (provide 'lilypond-mode
)
492 ;;; lilypond-mode.el ends here