2 ;;; lilypond-mode.el --- Major mode for editing GNU LilyPond music scores
4 ;;; source file of the GNU LilyPond music typesetter
6 ;;; (c) 1999, 2000 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.103"
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-regexp-alist
32 '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
33 "Regexp used to match LilyPond errors. See `compilation-error-regexp-alist'.")
35 (defcustom LilyPond-include-path
".:/tmp"
36 "* LilyPond include path."
41 (defun LilyPond-check-files (derived originals extensions
)
42 "Check that DERIVED is newer than any of the ORIGINALS.
43 Try each original with each member of EXTENSIONS, in all directories
44 in LilyPond-include-path."
46 (regexp (concat "\\`\\("
47 (mapconcat (function (lambda (dir)
48 (regexp-quote (expand-file-name dir
))))
49 LilyPond-include-path
"\\|")
51 (mapconcat 'regexp-quote originals
"\\|")
53 (mapconcat 'regexp-quote extensions
"\\|")
55 (buffers (buffer-list)))
57 (let* ((buffer (car buffers
))
58 (name (buffer-file-name buffer
)))
59 (setq buffers
(cdr buffers
))
60 (if (and name
(string-match regexp name
))
62 (and (buffer-modified-p buffer
)
63 (or (not LilyPond-save-query
)
64 (y-or-n-p (concat "Save file "
65 (buffer-file-name buffer
)
67 (save-excursion (set-buffer buffer
) (save-buffer)))
68 (if (file-newer-than-file-p name derived
)
72 (defun LilyPond-running ()
73 (let ((process (get-process "lilypond")))
75 (eq (process-status process
) 'run
))))
77 (defun LilyPond-kill-job ()
78 "Kill the currently running LilyPond job."
80 ;; What bout TeX, Xdvi?
81 (quit-process (get-process "lilypond") t
))
83 ;; URG, should only run LilyPond-compile for LilyPond
84 ;; not for tex,xdvi (ly2dvi?)
85 (defun LilyPond-compile-file (command name
)
86 ;; We maybe should know what we run here (Lily, ly2dvi, tex)
87 ;; and adjust our error-matching regex ?
88 (compile-internal command
"No more errors" name
))
90 ;; do we still need this, now that we're using compile-internal?
91 (defun LilyPond-save-buffer ()
92 (if (buffer-modified-p) (save-buffer)))
94 ;;; return (dir base ext)
95 (defun split-file-name (name)
96 (let* ((i (string-match "[^/]*$" name
))
97 (dir (if (> i
0) (substring name
0 i
) "./"))
98 (file (substring name i
(length name
)))
99 (i (string-match "[^.]*$" file
)))
103 (list dir
(substring file
0 (- i
1)) (substring file i
(length file
)))
104 (list dir file
""))))
107 ;; Should check whether in command-alist?
108 (defcustom LilyPond-command-default
"LilyPond"
109 "Default command. Must identify a member of LilyPond-command-alist."
113 ;;;(make-variable-buffer-local 'LilyPond-command-last)
115 (defvar LilyPond-command-current
'LilyPond-command-master
)
116 ;;;(make-variable-buffer-local 'LilyPond-command-master)
119 ;; If non-nil, LilyPond-command-query will return the value of this
120 ;; variable instead of quering the user.
121 (defvar LilyPond-command-force nil
)
123 (defcustom LilyPond-xdvi-command
"xdvik"
124 "Command used to display DVI files."
129 ;; This is the major configuration variable.
130 (defcustom LilyPond-command-alist
132 ("LilyPond" .
("lilypond %s" .
"TeX"))
133 ("TeX" .
("tex '\\nonstopmode\\input %t'" .
"View"))
135 ("2Dvi" .
("ly2dvi %s" .
"View"))
137 ("Book" .
("lilypond-book %x" .
"LaTeX"))
138 ("LaTeX" .
("latex '\\nonstopmode\\input %l'" .
"View"))
140 ;; point-n-click (arg: exits upop USR1)
141 ("SmartView" .
("xdvi %d" .
"LilyPond"))
143 ;; refreshes when kicked USR1
144 ("View" .
(,(concat LilyPond-xdvi-command
" %d") .
"LilyPond"))
147 "AList of commands to execute on the current document.
149 The key is the name of the command as it will be presented to the
150 user, the value is a cons of the command string handed to the shell
151 after being expanded, and the next command to be executed upon
152 success. The expansion is done using the information found in
153 LilyPond-expand-list.
156 :type
'(repeat (cons :tag
"Command Item"
159 (string :tag
"Command")
160 (string :tag
"Next Key")))))
163 (defcustom LilyPond-file-extensions
'(".ly" ".sly" ".fly")
164 "*File extensions used by manually generated TeX files."
166 :type
'(repeat (string :format
"%v")))
169 (defcustom LilyPond-expand-alist
179 "Alist of expansion strings for LilyPond command names."
181 :type
'(repeat (cons :tag
"Alist item"
182 (string :tag
"Symbol")
183 (string :tag
"Expansion"))))
186 (defcustom LilyPond-command-Show
"View"
187 "*The default command to show (view or print) a LilyPond file.
188 Must be the car of an entry in `LilyPond-command-alist'."
191 (make-variable-buffer-local 'LilyPond-command-Show
)
193 (defcustom LilyPond-command-Print
"Print"
194 "The name of the Print entry in LilyPond-command-Print."
198 (defun xLilyPond-compile-sentinel (process msg
)
200 (= 0 (process-exit-status process
)))
201 (setq LilyPond-command-default
202 (cddr (assoc LilyPond-command-default LilyPond-command-alist
)))))
204 ;; FIXME: shouldn't do this for stray View/xdvi
205 (defun LilyPond-compile-sentinel (buffer msg
)
206 (if (string-match "^finished" msg
)
207 (setq LilyPond-command-default
208 (cddr (assoc LilyPond-command-default LilyPond-command-alist
)))))
210 ;;(make-variable-buffer-local 'compilation-finish-function)
211 (setq compilation-finish-function
'LilyPond-compile-sentinel
)
213 (defun LilyPond-command-query (name)
214 "Query the user for what LilyPond command to use."
215 (let* ((default (cond ((if (string-equal name
"emacs-lily")
216 (LilyPond-check-files (concat name
".tex")
218 LilyPond-file-extensions
)
220 (LilyPond-save-buffer)
222 LilyPond-command-default
))
223 (t LilyPond-command-default
)))
225 (answer (or LilyPond-command-force
227 (concat "Command: (default " default
") ")
228 LilyPond-command-alist nil t
))))
230 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
231 (let ((answer (car-safe (assoc answer LilyPond-command-alist
))))
233 (not (string-equal answer
"")))
238 ;; FIXME: find ``\score'' in buffers / make settable?
239 (defun LilyPond-master-file ()
243 (defun LilyPond-command-master ()
244 "Run command on the current document."
246 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
247 'LilyPond-master-file
))
249 (defun LilyPond-region-file (begin end
)
256 (ext (if (string-match "^[\\]score" (buffer-substring begin end
))
258 (if (< 50 (abs (- begin end
)))
261 (concat dir base ext
)))
263 (defun LilyPond-command-region (begin end
)
264 "Run LilyPond on the current region."
266 (write-region begin end
(LilyPond-region-file begin end
) nil
'nomsg
)
267 (LilyPond-command (LilyPond-command-query
268 (LilyPond-region-file begin end
))
269 '(lambda () (LilyPond-region-file begin end
))))
271 (defun LilyPond-command-buffer ()
272 "Run LilyPond on buffer."
274 (LilyPond-command-region (point-min) (point-max)))
276 (defun LilyPond-command-expand (string file
)
277 (let ((case-fold-search nil
))
278 (if (string-match "%" string
)
279 (let* ((b (match-beginning 0))
281 (l (split-file-name file
))
284 (LilyPond-command-expand
285 (concat (substring string
0 b
)
288 (let ((entry (assoc (substring string b e
)
289 LilyPond-expand-alist
)))
290 (if entry
(cdr entry
) ""))
291 (substring string e
))
295 (defun LilyPond-command (name file
)
296 "Run command NAME on the file you get by calling FILE.
298 FILE is a function return a file name. It has one optional argument,
299 the extension to use on the file.
301 Use the information in LilyPond-command-alist to determine how to run the
304 (let ((entry (assoc name LilyPond-command-alist
)))
306 (let ((command (LilyPond-command-expand (cadr entry
)
309 (buffer-xdvi (get-buffer "*view*"))
310 (process-xdvi (if buffer-xdvi
(get-buffer-process buffer-xdvi
) nil
)))
311 (if (and process-xdvi
312 (string-equal name
"View"))
313 ;; Don't open new xdvi window, but force redisplay
314 ;; We could make this an option.
315 (signal-process (process-id process-xdvi
) 'SIGUSR1
)
317 (setq LilyPond-command-default name
)
318 (LilyPond-compile-file command name
))))))))
321 ;; Sadly we need this for a macro in Emacs 19.
323 ;; Imenu isn't used in XEmacs, so just ignore load errors.
331 (defvar LilyPond-mode-map
()
332 "Keymap used in `LilyPond-mode' buffers.")
334 ;; Note: if you make changes to the map, you must do
335 ;; M-x set-variable LilyPond-mode-map nil
338 ;; to let the changest take effect
340 (if LilyPond-mode-map
342 (setq LilyPond-mode-map
(make-sparse-keymap))
343 (define-key LilyPond-mode-map
"\C-c\C-r" 'LilyPond-command-region
)
344 (define-key LilyPond-mode-map
"\C-c\C-b" 'LilyPond-command-buffer
)
345 (define-key LilyPond-mode-map
"\C-c\C-k" 'LilyPond-kill-job
)
346 (define-key LilyPond-mode-map
"\C-c\C-c" 'LilyPond-command-master
)
351 (defun LilyPond-command-menu-entry (entry)
352 ;; Return LilyPond-command-alist ENTRY as a menu item.
353 (let ((name (car entry
)))
354 (cond ((and (string-equal name LilyPond-command-Print
)
355 LilyPond-printer-list
)
356 (let ((command LilyPond-print-command
)
358 (append (list LilyPond-command-Print
)
359 (mapcar 'LilyPond-command-menu-printer-entry
360 LilyPond-printer-list
))))
362 (vector name
(list 'LilyPond-command-menu name
) t
)))))
365 (easy-menu-define LilyPond-mode-menu
367 "Menu used in LilyPond mode."
370 [ "Master File" LilyPond-command-select-master
371 :keys
"C-c C-c" :style radio
372 :selected
(eq LilyPond-command-current
'LilyPond-command-master
) ]
373 [ "Buffer" LilyPond-command-select-buffer
374 :keys
"C-c C-b" :style radio
375 :selected
(eq LilyPond-command-current
'LilyPond-command-buffer
) ]
376 [ "Region" LilyPond-command-select-region
377 :keys
"C-c C-r" :style radio
378 :selected
(eq LilyPond-command-current
'LilyPond-command-region
) ]))
379 (let ((file 'LilyPond-command-on-current
))
380 (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist
))))
383 (defconst LilyPond-imenu-generic-re
"^\\([a-zA-Z_][a-zA-Z0-9_]*\\) *="
384 "Regexp matching Identifier definitions.")
386 (defvar LilyPond-imenu-generic-expression
387 (list (list nil LilyPond-imenu-generic-re
1))
388 "Expression for imenu")
390 (defun LilyPond-command-select-master ()
392 (message "Next command will be on the master file")
393 (setq LilyPond-command-current
'LilyPond-command-master
))
395 (defun LilyPond-command-select-buffer ()
397 (message "Next command will be on the buffer")
398 (setq LilyPond-command-current
'LilyPond-command-buffer
))
400 (defun LilyPond-command-select-region ()
402 (message "Next command will be on the region")
403 (setq LilyPond-command-current
'LilPond-command-region
))
405 (defun LilyPond-command-menu (name)
406 ;; Execute LilyPond-command-alist NAME from a menu.
407 (let ((LilyPond-command-force name
))
408 (funcall LilyPond-command-current
)))
410 (defun LilyPond-mode ()
411 "Major mode for editing LilyPond music files.
413 This mode knows about LilyPond keywords and line comments, not about
414 indentation or block comments. It features easy compilation, error
415 finding and viewing of a LilyPond source buffer or region.
418 \\{LilyPond-mode-map}
421 LilyPond-command-alist\t\talist from name to command
422 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
424 ;; set up local variables
425 (kill-all-local-variables)
427 (make-local-variable 'font-lock-defaults
)
428 (setq font-lock-defaults
'(LilyPond-font-lock-keywords))
430 (make-local-variable 'paragraph-separate
)
431 (setq paragraph-separate
"^[ \t]*$")
433 (make-local-variable 'paragraph-start
)
434 (setq paragraph-start
"^[ \t]*$")
436 (make-local-variable 'comment-start
)
437 (setq comment-start
"%")
439 (make-local-variable 'comment-start-skip
)
440 (setq comment-start-skip
"%{? *")
442 (make-local-variable 'comment-end
)
443 (setq comment-end
"\n")
445 (make-local-variable 'block-comment-start
)
446 (setq block-comment-start
"%{")
448 (make-local-variable 'block-comment-end
)
449 (setq block-comment-end
"%}")
451 (make-local-variable 'indent-line-function
)
452 (setq indent-line-function
'indent-relative-maybe
)
454 (set-syntax-table LilyPond-mode-syntax-table
)
455 (setq major-mode
'LilyPond-mode
)
456 (setq mode-name
"LilyPond")
457 (setq local-abbrev-table LilyPond-mode-abbrev-table
)
458 (use-local-map LilyPond-mode-map
)
460 (make-local-variable 'imenu-generic-expression
)
461 (setq imenu-generic-expression LilyPond-imenu-generic-expression
)
462 (imenu-add-to-menubar "Index")
464 ;; run the mode hook. LilyPond-mode-hook use is deprecated
465 (run-hooks 'LilyPond-mode-hook
))
467 (defun LilyPond-version ()
468 "Echo the current version of `LilyPond-mode' in the minibuffer."
470 (message "Using `LilyPond-mode' version %s" LilyPond-version
))
472 (provide 'lilypond-mode
)
473 ;;; lilypond-mode.el ends here