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 (defun LilyPond-region-file (begin end
)
259 (ext (if (string-match "^[\\]score" (buffer-substring begin end
))
261 (if (< 50 (abs (- begin end
)))
264 (concat dir base ext
)))
266 (defun LilyPond-command-region (begin end
)
267 "Run LilyPond on the current region."
269 (write-region begin end
(LilyPond-region-file begin end
) nil
'nomsg
)
270 (LilyPond-command (LilyPond-command-query
271 (LilyPond-region-file begin end
))
272 '(lambda () (LilyPond-region-file begin end
))))
274 (defun LilyPond-command-buffer ()
275 "Run LilyPond on buffer."
277 (LilyPond-command-region (point-min) (point-max)))
279 (defun LilyPond-command-expand (string file
)
280 (let ((case-fold-search nil
))
281 (if (string-match "%" string
)
282 (let* ((b (match-beginning 0))
284 (l (split-file-name file
))
287 (LilyPond-command-expand
288 (concat (substring string
0 b
)
291 (let ((entry (assoc (substring string b e
)
292 LilyPond-expand-alist
)))
293 (if entry
(cdr entry
) ""))
294 (substring string e
))
298 (defun LilyPond-shell-process (name buffer command
)
299 (let ((old (current-buffer)))
300 (switch-to-buffer-other-window buffer
)
301 (goto-char (point-max))
302 (start-process-shell-command name buffer command
)
303 (switch-to-buffer-other-window old
)))
306 (defun LilyPond-command (name file
)
307 "Run command NAME on the file you get by calling FILE.
309 FILE is a function return a file name. It has one optional argument,
310 the extension to use on the file.
312 Use the information in LilyPond-command-alist to determine how to run the
315 (let ((entry (assoc name LilyPond-command-alist
)))
317 (let ((command (LilyPond-command-expand (cadr entry
)
319 (if (string-equal name
"View")
320 (let ((buffer-xdvi (get-buffer-create "*view*")))
321 (if LilyPond-kick-xdvi
322 (let ((process-xdvi (get-buffer-process buffer-xdvi
)))
324 ;; Don't open new xdvi window, but force redisplay
325 ;; We could make this an option.
326 (signal-process (process-id process-xdvi
) 'SIGUSR1
)
327 (LilyPond-shell-process name buffer-xdvi command
)))
328 (LilyPond-shell-process name buffer-xdvi command
)))
330 (setq LilyPond-command-default name
)
331 (LilyPond-compile-file command name
)))))))
334 ;; Sadly we need this for a macro in Emacs 19.
336 ;; Imenu isn't used in XEmacs, so just ignore load errors.
344 (defvar LilyPond-mode-map
()
345 "Keymap used in `LilyPond-mode' buffers.")
347 ;; Note: if you make changes to the map, you must do
348 ;; M-x set-variable LilyPond-mode-map nil
351 ;; to let the changest take effect
353 (if LilyPond-mode-map
355 (setq LilyPond-mode-map
(make-sparse-keymap))
356 (define-key LilyPond-mode-map
"\C-c\C-r" 'LilyPond-command-region
)
357 (define-key LilyPond-mode-map
"\C-c\C-b" 'LilyPond-command-buffer
)
358 (define-key LilyPond-mode-map
"\C-c\C-k" 'LilyPond-kill-job
)
359 (define-key LilyPond-mode-map
"\C-c\C-c" 'LilyPond-command-master
)
364 (defun LilyPond-command-menu-entry (entry)
365 ;; Return LilyPond-command-alist ENTRY as a menu item.
366 (let ((name (car entry
)))
367 (cond ((and (string-equal name LilyPond-command-Print
)
368 LilyPond-printer-list
)
369 (let ((command LilyPond-print-command
)
371 (append (list LilyPond-command-Print
)
372 (mapcar 'LilyPond-command-menu-printer-entry
373 LilyPond-printer-list
))))
375 (vector name
(list 'LilyPond-command-menu name
) t
)))))
378 (easy-menu-define LilyPond-mode-menu
380 "Menu used in LilyPond mode."
383 [ "Master File" LilyPond-command-select-master
384 :keys
"C-c C-c" :style radio
385 :selected
(eq LilyPond-command-current
'LilyPond-command-master
) ]
386 [ "Buffer" LilyPond-command-select-buffer
387 :keys
"C-c C-b" :style radio
388 :selected
(eq LilyPond-command-current
'LilyPond-command-buffer
) ]
389 [ "Region" LilyPond-command-select-region
390 :keys
"C-c C-r" :style radio
391 :selected
(eq LilyPond-command-current
'LilyPond-command-region
) ]))
392 (let ((file 'LilyPond-command-on-current
))
393 (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist
))))
396 (defconst LilyPond-imenu-generic-re
"^\\([a-zA-Z_][a-zA-Z0-9_]*\\) *="
397 "Regexp matching Identifier definitions.")
399 (defvar LilyPond-imenu-generic-expression
400 (list (list nil LilyPond-imenu-generic-re
1))
401 "Expression for imenu")
403 (defun LilyPond-command-select-master ()
405 (message "Next command will be on the master file")
406 (setq LilyPond-command-current
'LilyPond-command-master
))
408 (defun LilyPond-command-select-buffer ()
410 (message "Next command will be on the buffer")
411 (setq LilyPond-command-current
'LilyPond-command-buffer
))
413 (defun LilyPond-command-select-region ()
415 (message "Next command will be on the region")
416 (setq LilyPond-command-current
'LilPond-command-region
))
418 (defun LilyPond-command-menu (name)
419 ;; Execute LilyPond-command-alist NAME from a menu.
420 (let ((LilyPond-command-force name
))
421 (funcall LilyPond-command-current
)))
423 (defun LilyPond-mode ()
424 "Major mode for editing LilyPond music files.
426 This mode knows about LilyPond keywords and line comments, not about
427 indentation or block comments. It features easy compilation, error
428 finding and viewing of a LilyPond source buffer or region.
431 \\{LilyPond-mode-map}
434 LilyPond-command-alist\t\talist from name to command
435 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
437 ;; set up local variables
438 (kill-all-local-variables)
440 (make-local-variable 'font-lock-defaults
)
441 (setq font-lock-defaults
'(LilyPond-font-lock-keywords))
443 (make-local-variable 'paragraph-separate
)
444 (setq paragraph-separate
"^[ \t]*$")
446 (make-local-variable 'paragraph-start
)
447 (setq paragraph-start
"^[ \t]*$")
449 (make-local-variable 'comment-start
)
450 (setq comment-start
"%")
452 (make-local-variable 'comment-start-skip
)
453 (setq comment-start-skip
"%{? *")
455 (make-local-variable 'comment-end
)
456 (setq comment-end
"\n")
458 (make-local-variable 'block-comment-start
)
459 (setq block-comment-start
"%{")
461 (make-local-variable 'block-comment-end
)
462 (setq block-comment-end
"%}")
464 (make-local-variable 'indent-line-function
)
465 (setq indent-line-function
'indent-relative-maybe
)
467 (set-syntax-table LilyPond-mode-syntax-table
)
468 (setq major-mode
'LilyPond-mode
)
469 (setq mode-name
"LilyPond")
470 (setq local-abbrev-table LilyPond-mode-abbrev-table
)
471 (use-local-map LilyPond-mode-map
)
473 (make-local-variable 'imenu-generic-expression
)
474 (setq imenu-generic-expression LilyPond-imenu-generic-expression
)
475 (imenu-add-to-menubar "Index")
477 ;; run the mode hook. LilyPond-mode-hook use is deprecated
478 (run-hooks 'LilyPond-mode-hook
))
480 (defun LilyPond-version ()
481 "Echo the current version of `LilyPond-mode' in the minibuffer."
483 (message "Using `LilyPond-mode' version %s" LilyPond-version
))
485 (provide 'lilypond-mode
)
486 ;;; lilypond-mode.el ends here