lilypond-1.3.144
[lilypond.git] / lilypond-mode.el
blob177004d0966e50677b5a2b1a809374b9814d4e9e
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>
8 ;;; Inspired on auctex
10 ;;;
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)))
15 ;;;
17 (load-library "lilypond-font-lock")
19 (require 'easymenu)
20 (require 'compile)
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."
40 :type 'string
41 :group 'LilyPond)
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."
48 (let ((found nil)
49 (regexp (concat "\\`\\("
50 (mapconcat (function (lambda (dir)
51 (regexp-quote (expand-file-name dir))))
52 LilyPond-include-path "\\|")
53 "\\).*\\("
54 (mapconcat 'regexp-quote originals "\\|")
55 "\\)\\.\\("
56 (mapconcat 'regexp-quote extensions "\\|")
57 "\\)\\'"))
58 (buffers (buffer-list)))
59 (while buffers
60 (let* ((buffer (car buffers))
61 (name (buffer-file-name buffer)))
62 (setq buffers (cdr buffers))
63 (if (and name (string-match regexp name))
64 (progn
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)
69 "? ")))
70 (save-excursion (set-buffer buffer) (save-buffer)))
71 (if (file-newer-than-file-p name derived)
72 (setq found t))))))
73 found))
75 (defun LilyPond-running ()
76 (let ((process (get-process "lilypond")))
77 (and process
78 (eq (process-status process) 'run))))
80 (defun LilyPond-kill-job ()
81 "Kill the currently running LilyPond job."
82 (interactive)
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)))
103 (if (and
104 (> i 0)
105 (< i (length 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."
114 :group 'LilyPond
115 :type 'string)
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."
129 :group 'LilyPond
130 :type 'string)
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.
158 :group 'LilyPond
159 :type '(repeat (cons :tag "Command Item"
160 (string :tag "Key")
161 (cons :tag "How"
162 (string :tag "Command")
163 (string :tag "Next Key")))))
165 ;; drop this?
166 (defcustom LilyPond-file-extensions '(".ly" ".sly" ".fly")
167 "*File extensions used by manually generated TeX files."
168 :group 'LilyPond
169 :type '(repeat (string :format "%v")))
172 (defcustom LilyPond-expand-alist
174 ("%s" . ".ly")
175 ("%t" . ".tex")
176 ("%d" . ".dvi")
177 ("%p" . ".ps")
178 ("%l" . ".latex")
179 ("%x" . ".tely")
182 "Alist of expansion strings for LilyPond command names."
183 :group 'LilyPond
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'."
192 :group 'LilyPond
193 :type 'string)
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."
198 :group 'LilyPond
199 :type 'string)
201 (defun xLilyPond-compile-sentinel (process msg)
202 (if (and process
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")
220 (list name)
221 LilyPond-file-extensions)
222 ;; FIXME
223 (LilyPond-save-buffer)
224 ;;"LilyPond"
225 LilyPond-command-default))
226 (t LilyPond-command-default)))
228 (answer (or LilyPond-command-force
229 (completing-read
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))))
235 (if (and answer
236 (not (string-equal answer "")))
237 answer
238 default))))
241 ;; FIXME: find ``\score'' in buffers / make settable?
242 (defun LilyPond-master-file ()
243 ;; duh
244 (buffer-file-name))
246 (defun LilyPond-command-master ()
247 "Run command on the current document."
248 (interactive)
249 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
250 'LilyPond-master-file))
252 (defun LilyPond-region-file (begin end)
253 (let (
254 ;; (dir "/tmp/")
255 ;; urg
256 (dir "./")
257 (base "emacs-lily")
258 ;; Hmm
259 (ext (if (string-match "^[\\]score" (buffer-substring begin end))
260 ".ly"
261 (if (< 50 (abs (- begin end)))
262 ".fly"
263 ".sly"))))
264 (concat dir base ext)))
266 (defun LilyPond-command-region (begin end)
267 "Run LilyPond on the current region."
268 (interactive "r")
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."
276 (interactive)
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))
283 (e (+ b 2))
284 (l (split-file-name file))
285 (dir (car l))
286 (base (cadr l)))
287 (LilyPond-command-expand
288 (concat (substring string 0 b)
290 base
291 (let ((entry (assoc (substring string b e)
292 LilyPond-expand-alist)))
293 (if entry (cdr entry) ""))
294 (substring string e))
295 file))
296 string)))
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
313 command."
315 (let ((entry (assoc name LilyPond-command-alist)))
316 (if entry
317 (let ((command (LilyPond-command-expand (cadr entry)
318 (apply file nil))))
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)))
323 (if process-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)))
329 (progn
330 (setq LilyPond-command-default name)
331 (LilyPond-compile-file command name)))))))
333 ;; XEmacs stuff
334 ;; Sadly we need this for a macro in Emacs 19.
335 (eval-when-compile
336 ;; Imenu isn't used in XEmacs, so just ignore load errors.
337 (condition-case ()
338 (require 'imenu)
339 (error nil)))
342 ;;; Keymap
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
349 ;; M-x eval-buffer
350 ;; M-x LilyPond-mode
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)
362 ;;; Menu Support
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)
370 (lookup 1))
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
379 LilyPond-mode-map
380 "Menu used in LilyPond mode."
381 (append '("Command")
382 '(("Command on"
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 ()
404 (interactive)
405 (message "Next command will be on the master file")
406 (setq LilyPond-command-current 'LilyPond-command-master))
408 (defun LilyPond-command-select-buffer ()
409 (interactive)
410 (message "Next command will be on the buffer")
411 (setq LilyPond-command-current 'LilyPond-command-buffer))
413 (defun LilyPond-command-select-region ()
414 (interactive)
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.
430 COMMANDS
431 \\{LilyPond-mode-map}
432 VARIABLES
434 LilyPond-command-alist\t\talist from name to command
435 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
436 (interactive)
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."
482 (interactive)
483 (message "Using `LilyPond-mode' version %s" LilyPond-version))
485 (provide 'lilypond-mode)
486 ;;; lilypond-mode.el ends here