release commit
[lilypond.git] / elisp / lilypond-mode.el
bloba9c134a4e932e59f1f310d47d312e8bf4c44eac4
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--2003 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;;
8 ;;; Changed 2001--2003 Heikki Junes <heikki.junes@hut.fi>
9 ;;; * Add PS-compilation, PS-viewing and MIDI-play (29th Aug 2001)
10 ;;; * Keyboard shortcuts (12th Sep 2001)
11 ;;; * Inserting tags, inspired on sgml-mode (11th Oct 2001)
12 ;;; * Autocompletion & Info (23rd Nov 2002)
14 ;;; Inspired on auctex
16 ;;; Look lilypond-init.el or Documentation/topdocs/INSTALL.texi
17 ;;; for installing instructions.
19 (require 'easymenu)
20 (require 'compile)
22 (defconst LilyPond-version "1.9.9"
23 "`LilyPond-mode' version number.")
25 (defconst LilyPond-help-address "bug-lilypond@gnu.org"
26 "Address accepting submission of bug reports.")
28 (defvar LilyPond-mode-hook nil
29 "*Hook called by `LilyPond-mode'.")
31 (defvar LilyPond-region-file-prefix "emacs-lily"
32 "File prefix for commands on buffer or region.")
34 ;; FIXME: find ``\score'' in buffers / make settable?
35 (defun LilyPond-master-file ()
36 ;; duh
37 (buffer-file-name))
39 (defvar LilyPond-kick-xdvi nil
40 "If true, no simultaneous xdvi's are started, but reload signal is sent.")
42 (defvar LilyPond-command-history nil
43 "Command history list.")
45 (defvar LilyPond-regexp-alist
46 '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
47 "Regexp used to match LilyPond errors. See `compilation-error-regexp-alist'.")
49 (defvar LilyPond-imenu nil
50 "A flag to tell whether LilyPond-imenu is turned on.")
51 (make-variable-buffer-local 'LilyPond-imenu)
53 (defcustom LilyPond-include-path ".:/tmp"
54 "* LilyPond include path."
55 :type 'string
56 :group 'LilyPond)
58 (defun LilyPond-words-filename ()
59 "The file containing LilyPond \keywords \Identifiers and ReservedWords.
60 Finds file lilypond-words.el from load-path."
61 (let ((fn nil)
62 (lp load-path)
63 (words-file "lilypond.words.el"))
64 (while (and (> (length lp) 0) (not fn))
65 (setq fn (concat (car lp) "/" words-file))
66 (if (not (file-readable-p fn))
67 (progn (setq fn nil) (setq lp (cdr lp)))))
68 (if (not fn)
69 (progn (message "Warning: `lilypond.words.el' not found in `load-path'. See `lilypond-init.el'.")
70 (sit-for 5 0)))
71 fn))
73 (defun LilyPond-add-dictionary-word (x)
74 "Contains all words: \keywords \Identifiers and ReservedWords."
75 (nconc '(("" . 1)) x))
77 ;; creates dictionary if empty
78 (if (and (eq (length (LilyPond-add-dictionary-word ())) 1)
79 (not (eq (LilyPond-words-filename) nil)))
80 (progn
81 (setq b (find-file-noselect (LilyPond-words-filename) t t))
82 (setq m (set-marker (make-marker) 1 (get-buffer b)))
83 (setq i 1)
84 (while (> (buffer-size b) (marker-position m))
85 (setq i (+ i 1))
86 (setq copy (copy-alist (list (eval (symbol-name (read m))))))
87 (setcdr copy i)
88 (LilyPond-add-dictionary-word (list copy)))
89 (kill-buffer b)))
91 (defvar LilyPond-insert-tag-current ""
92 "The last command selected from the LilyPond-Insert -menu.")
94 (defconst LilyPond-menu-keywords
95 (let ((wordlist '())
96 (co (all-completions "" (LilyPond-add-dictionary-word ())))
97 (currword ""))
98 (progn
99 (while (> (length co) 0)
100 (setq currword (car co))
101 (if (string-equal "-" (car (setq co (cdr co))))
102 (progn
103 (add-to-list 'wordlist currword)
104 (while (and (> (length co) 0)
105 (not (string-equal "-" (car (setq co (cdr co))))))))))
106 (reverse wordlist)))
107 "Keywords inserted from LilyPond-Insert-menu.")
109 (defconst LilyPond-keywords
110 (let ((wordlist '("\\score"))
111 (co (all-completions "" (LilyPond-add-dictionary-word ())))
112 (currword ""))
113 (progn
114 (while (> (length co) 0)
115 (setq currword (car co))
116 (if (> (length currword) 1)
117 (if (and (string-equal "\\" (substring currword 0 1))
118 (string-match "[a-z-]+" currword)
119 (= (match-beginning 0) 1)
120 (= (match-end 0) (length currword))
121 (not (string-equal "\\longa" currword))
122 (not (string-equal "\\breve" currword))
123 (not (string-equal "\\maxima" currword))
124 (string-equal (downcase currword) currword))
125 (add-to-list 'wordlist currword)))
126 (if (string-equal "-" (car (setq co (cdr co))))
127 (while (and (> (length co) 0)
128 (not (string-equal "-" (car (setq co (cdr co)))))))))
129 (reverse wordlist)))
130 "LilyPond \\keywords")
132 (defconst LilyPond-identifiers
133 (let ((wordlist '("\\voiceOne"))
134 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
135 (progn
136 (while (> (length co) 0)
137 (setq currword (car co))
138 (if (> (length currword) 1)
139 (if (and (string-equal "\\" (substring currword 0 1))
140 (string-match "[a-zA-Z-]+" currword)
141 (= (match-beginning 0) 1)
142 (= (match-end 0) (length currword))
143 (not (string-equal (downcase currword) currword)))
144 (add-to-list 'wordlist currword)))
145 (if (string-equal "-" (car (setq co (cdr co))))
146 (while (and (> (length co) 0)
147 (not (string-equal "-" (car (setq co (cdr co)))))))))
148 (reverse wordlist)))
149 "LilyPond \\Identifiers")
151 (defconst LilyPond-Capitalized-Reserved-Words
152 (let ((wordlist '("StaffContext"))
153 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
154 (progn
155 (while (> (length co) 0)
156 (setq currword (car co))
157 (if (> (length currword) 0)
158 (if (and (string-match "[a-zA-Z_]+" currword)
159 (= (match-beginning 0) 0)
160 (= (match-end 0) (length currword))
161 (not (string-equal (downcase currword) currword)))
162 (add-to-list 'wordlist currword)))
163 (if (string-equal "-" (car (setq co (cdr co))))
164 (while (and (> (length co) 0)
165 (not (string-equal "-" (car (setq co (cdr co)))))))))
166 (reverse wordlist)))
167 "LilyPond ReservedWords")
169 (defconst LilyPond-non-capitalized-reserved-words
170 (let ((wordlist '("cessess"))
171 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
172 (progn
173 (while (> (length co) 0)
174 (setq currword (car co))
175 (if (> (length currword) 0)
176 (if (and (string-match "[a-z]+" currword)
177 (= (match-beginning 0) 0)
178 (= (match-end 0) (length currword))
179 (string-equal (downcase currword) currword))
180 (add-to-list 'wordlist currword)))
181 (if (string-equal "-" (car (setq co (cdr co))))
182 (while (and (> (length co) 0)
183 (not (string-equal "-" (car (setq co (cdr co)))))))))
184 (reverse wordlist)))
185 "LilyPond notenames")
187 (defun LilyPond-check-files (derived originals extensions)
188 "Check that DERIVED is newer than any of the ORIGINALS.
189 Try each original with each member of EXTENSIONS, in all directories
190 in LilyPond-include-path."
191 (let ((found nil)
192 (regexp (concat "\\`\\("
193 (mapconcat (function (lambda (dir)
194 (regexp-quote (expand-file-name dir))))
195 LilyPond-include-path "\\|")
196 "\\).*\\("
197 (mapconcat 'regexp-quote originals "\\|")
198 "\\)\\.\\("
199 (mapconcat 'regexp-quote extensions "\\|")
200 "\\)\\'"))
201 (buffers (buffer-list)))
202 (while buffers
203 (let* ((buffer (car buffers))
204 (name (buffer-file-name buffer)))
205 (setq buffers (cdr buffers))
206 (if (and name (string-match regexp name))
207 (progn
208 (and (buffer-modified-p buffer)
209 (or (not LilyPond-save-query)
210 (y-or-n-p (concat "Save file "
211 (buffer-file-name buffer)
212 "? ")))
213 (save-excursion (set-buffer buffer) (save-buffer)))
214 (if (file-newer-than-file-p name derived)
215 (setq found t))))))
216 found))
218 (defun LilyPond-running ()
219 "Check the currently running LilyPond compiling jobs."
220 (let ((process-names (list "lilypond" "tex" "2dvi" "2ps" "2midi"
221 "book" "latex"))
222 (running nil))
223 (while (setq process-name (pop process-names))
224 (setq process (get-process process-name))
225 (if (and process
226 (eq (process-status process) 'run))
227 (push process-name running)))
228 running)) ; return the running jobs
230 (defun LilyPond-midi-running ()
231 "Check the currently running Midi processes."
232 (let ((process-names (list "midi" "midiall"))
233 (running nil))
234 (while (setq process-name (pop process-names))
235 (setq process (get-process process-name))
236 (if (and process
237 (eq (process-status process) 'run))
238 (push process-name running)))
239 running)) ; return the running jobs
241 (defun LilyPond-kill-jobs ()
242 "Kill the currently running LilyPond compiling jobs."
243 (interactive)
244 (let ((process-names (LilyPond-running))
245 (killed nil))
246 (while (setq process-name (pop process-names))
247 (quit-process (get-process process-name) t)
248 (push process-name killed))
249 killed)) ; return the killed jobs
251 (defun LilyPond-kill-midi ()
252 "Kill the currently running midi processes."
253 (let ((process-names (LilyPond-midi-running))
254 (killed nil))
255 (while (setq process-name (pop process-names))
256 (quit-process (get-process process-name) t)
257 (push process-name killed))
258 killed)) ; return the killed jobs
260 ;; URG, should only run LilyPond-compile for LilyPond
261 ;; not for tex,xdvi (lilypond?)
262 (defun LilyPond-compile-file (command name)
263 ;; We maybe should know what we run here (Lily, lilypond, tex)
264 ;; and adjust our error-matching regex ?
265 (compile-internal
266 (if (eq LilyPond-command-current 'LilyPond-command-master)
267 command
268 ;; use temporary directory for Commands on Buffer/Region
269 ;; hm.. the directory is set twice, first to default-dir
270 (concat "cd " (LilyPond-temp-directory) "; " command))
271 "No more errors" name))
273 ;; do we still need this, now that we're using compile-internal?
274 (defun LilyPond-save-buffer ()
275 "Save buffer and set default command for compiling."
276 (interactive)
277 (if (buffer-modified-p)
278 (progn (save-buffer)
279 (setq LilyPond-command-default "LilyPond"))))
281 ;;; return (dir base ext)
282 (defun split-file-name (name)
283 (let* ((i (string-match "[^/]*$" name))
284 (dir (if (> i 0) (substring name 0 i) "./"))
285 (file (substring name i (length name)))
286 (i (string-match "[^.]*$" file)))
287 (if (and
288 (> i 0)
289 (< i (length file)))
290 (list dir (substring file 0 (- i 1)) (substring file i (length file)))
291 (list dir file ""))))
294 ;; Should check whether in command-alist?
295 (defcustom LilyPond-command-default "LilyPond"
296 "Default command. Must identify a member of LilyPond-command-alist."
298 :group 'LilyPond
299 :type 'string)
300 ;;;(make-variable-buffer-local 'LilyPond-command-last)
302 (defvar LilyPond-command-current 'LilyPond-command-master)
303 ;;;(make-variable-buffer-local 'LilyPond-command-master)
306 ;; If non-nil, LilyPond-command-query will return the value of this
307 ;; variable instead of quering the user.
308 (defvar LilyPond-command-force nil)
310 (defcustom LilyPond-xdvi-command "xdvi"
311 "Command used to display DVI files."
313 :group 'LilyPond
314 :type 'string)
316 (defcustom LilyPond-gv-command "gv -watch"
317 "Command used to display PS files."
319 :group 'LilyPond
320 :type 'string)
322 (defcustom LilyPond-midi-command "timidity"
323 "Command used to play MIDI files."
325 :group 'LilyPond
326 :type 'string)
328 (defcustom LilyPond-all-midi-command "timidity -ia"
329 "Command used to play MIDI files."
331 :group 'LilyPond
332 :type 'string)
334 (defun LilyPond-command-current-midi ()
335 "Play midi corresponding to the current document."
336 (interactive)
337 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file))
339 (defun LilyPond-command-all-midi ()
340 "Play midi corresponding to the current document."
341 (interactive)
342 (LilyPond-command (LilyPond-command-menu "MidiAll") 'LilyPond-master-file))
344 (defun count-rexp (start end rexp)
345 "Print number of found regular expressions in the region."
346 (interactive "r")
347 (save-excursion
348 (save-restriction
349 (narrow-to-region start end)
350 (goto-char (point-min))
351 (count-matches rexp))))
353 (defun count-midi-words ()
354 "Check number of midi-scores before the curser."
355 (if (eq LilyPond-command-current 'LilyPond-command-region)
356 (count-rexp (mark t) (point) "\\\\midi")
357 (count-rexp (point-min) (point-max) "\\\\midi")))
359 (defun count-midi-words-backwards ()
360 "Check number of midi-scores before the curser."
361 (if (eq LilyPond-command-current 'LilyPond-command-region)
362 (count-rexp (mark t) (point) "\\\\midi")
363 (count-rexp (point-min) (point) "\\\\midi")))
365 (defun LilyPond-string-current-midi ()
366 "Check the midi file of the following midi-score in the current document."
367 (let ((fnameprefix (if (eq LilyPond-command-current 'LilyPond-command-master)
368 (substring (LilyPond-master-file) 0 -3); suppose ".ly"
369 LilyPond-region-file-prefix))
370 (allcount (string-to-number (substring (count-midi-words) 0 -12)))
371 (count (string-to-number (substring (count-midi-words-backwards) 0 -12))))
372 (concat fnameprefix
373 (if (and (> allcount 1) (> count 0)) ; not first score
374 (if (eq count allcount) ; last score
375 (concat "-" (number-to-string (+ count -1)))
376 (concat "-" (number-to-string count))))
377 ".midi")))
379 (defun LilyPond-string-all-midi ()
380 "Return the midi files of the current document in ascending order."
381 (let ((fnameprefix (if (eq LilyPond-command-current 'LilyPond-command-master)
382 (substring (LilyPond-master-file) 0 -3); suppose ".ly"
383 LilyPond-region-file-prefix))
384 (allcount (string-to-number (substring (count-midi-words) 0 -12))))
385 (concat (if (> allcount 0) ; at least one midi-score
386 (concat fnameprefix ".midi "))
387 (if (> allcount 1) ; more than one midi-score
388 (concat fnameprefix "-[1-9].midi "))
389 (if (> allcount 9) ; etc.
390 (concat fnameprefix "-[1-9][0-9].midi"))
391 (if (> allcount 99) ; not first score
392 (concat fnameprefix "-[1-9][0-9][0-9].midi")))))
394 ;; This is the major configuration variable.
395 (defcustom LilyPond-command-alist
396 ;; Should expand this to include possible keyboard shortcuts which
397 ;; could then be mapped to define-key and menu.
399 ("LilyPond" . ("lilypond-bin %s" . "LaTeX"))
400 ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
402 ("2Dvi" . ("lilypond %s" . "View"))
403 ("2PS" . ("lilypond -P %s" . "ViewPS"))
404 ("2Midi" . ("lilypond -m %s" . "View"))
406 ("Book" . ("lilypond-book %x" . "LaTeX"))
407 ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
409 ;; point-n-click (arg: exits upop USR1)
410 ("SmartView" . ("xdvi %d" . "LilyPond"))
412 ;; refreshes when kicked USR1
413 ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
414 ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
416 ;; The following are refreshed in LilyPond-command:
417 ;; - current-midi depends on cursor position and
418 ("Midi" . (,(concat LilyPond-midi-command " " (LilyPond-string-current-midi)) . "LilyPond" )) ;
419 ;; - all-midi depends on number of midi-score.
420 ("MidiAll" . (,(concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)) . "LilyPond"))
423 "AList of commands to execute on the current document.
425 The key is the name of the command as it will be presented to the
426 user, the value is a cons of the command string handed to the shell
427 after being expanded, and the next command to be executed upon
428 success. The expansion is done using the information found in
429 LilyPond-expand-list.
431 :group 'LilyPond
432 :type '(repeat (cons :tag "Command Item"
433 (string :tag "Key")
434 (cons :tag "How"
435 (string :tag "Command")
436 (string :tag "Next Key")))))
438 ;; drop this?
439 (defcustom LilyPond-file-extension ".ly"
440 "*File extension used in LilyPond sources."
441 :group 'LilyPond
442 :type 'string)
445 (defcustom LilyPond-expand-alist
447 ("%s" . ".ly")
448 ("%t" . ".tex")
449 ("%d" . ".dvi")
450 ("%p" . ".ps")
451 ("%l" . ".tex")
452 ("%x" . ".tely")
453 ("%m" . ".midi")
456 "Alist of expansion strings for LilyPond command names."
457 :group 'LilyPond
458 :type '(repeat (cons :tag "Alist item"
459 (string :tag "Symbol")
460 (string :tag "Expansion"))))
463 (defcustom LilyPond-command-Show "View"
464 "*The default command to show (view or print) a LilyPond file.
465 Must be the car of an entry in `LilyPond-command-alist'."
466 :group 'LilyPond
467 :type 'string)
468 (make-variable-buffer-local 'LilyPond-command-Show)
470 (defcustom LilyPond-command-Print "Print"
471 "The name of the Print entry in LilyPond-command-Print."
472 :group 'LilyPond
473 :type 'string)
475 (defun xLilyPond-compile-sentinel (process msg)
476 (if (and process
477 (= 0 (process-exit-status process)))
478 (setq LilyPond-command-default
479 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
481 ;; FIXME: shouldn't do this for stray View/xdvi
482 (defun LilyPond-compile-sentinel (buffer msg)
483 (if (string-match "^finished" msg)
484 (setq LilyPond-command-default
485 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
487 ;;(make-variable-buffer-local 'compilation-finish-function)
488 (setq compilation-finish-function 'LilyPond-compile-sentinel)
490 (defun LilyPond-command-query (name)
491 "Query the user for what LilyPond command to use."
492 (let* ((default (cond ((if (string-equal name LilyPond-region-file-prefix)
493 (LilyPond-check-files (concat name ".tex")
494 (list name)
495 (list LilyPond-file-extension))
496 (if (verify-visited-file-modtime (current-buffer))
497 (if (buffer-modified-p)
498 (if (y-or-n-p "Save buffer before next command? ")
499 (LilyPond-save-buffer)))
500 (if (y-or-n-p "The command will be invoked to an already saved buffer. Revert it? ")
501 (revert-buffer t t)))
502 ;;"LilyPond"
503 LilyPond-command-default))
504 (t LilyPond-command-default)))
506 (completion-ignore-case t)
508 (answer (or LilyPond-command-force
509 (completing-read
510 (concat "Command: (default " default ") ")
511 LilyPond-command-alist nil t nil 'LilyPond-command-history))))
513 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
514 (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
515 (if (and answer
516 (not (string-equal answer "")))
517 answer
518 default))))
520 (defun LilyPond-command-master ()
521 "Run command on the current document."
522 (interactive)
523 (LilyPond-command-select-master)
524 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
525 'LilyPond-master-file))
527 (defun LilyPond-command-lilypond ()
528 "Run lilypond for the current document."
529 (interactive)
530 (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file)
533 (defun LilyPond-command-formatdvi ()
534 "Format the dvi output of the current document."
535 (interactive)
536 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
539 (defun LilyPond-command-formatps ()
540 "Format the ps output of the current document."
541 (interactive)
542 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
545 (defun LilyPond-command-formatmidi ()
546 "Format the midi output of the current document."
547 (interactive)
548 (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-master-file)
551 (defun LilyPond-command-smartview ()
552 "View the dvi output of current document."
553 (interactive)
554 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
557 (defun LilyPond-command-view ()
558 "View the dvi output of current document."
559 (interactive)
560 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
563 (defun LilyPond-command-viewps ()
564 "View the ps output of current document."
565 (interactive)
566 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
569 ;; FIXME, this is broken
570 (defun LilyPond-region-file (begin end)
571 (let (
572 ;; (dir "./")
573 (dir (LilyPond-temp-directory))
574 (base LilyPond-region-file-prefix)
575 (ext LilyPond-file-extension))
576 (concat dir base ext)))
578 ;;; Commands on Region work if there is an appropriate '\score'.
579 (defun LilyPond-command-region (begin end)
580 "Run LilyPond on the current region."
581 (interactive "r")
582 (if (or (> begin (point-min)) (< end (point-max)))
583 (LilyPond-command-select-region))
584 (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
585 (LilyPond-command (LilyPond-command-query
586 (LilyPond-region-file begin end))
587 '(lambda () (LilyPond-region-file begin end)))
588 ;; Region may deactivate even if buffer was intact, reactivate?
589 ;; Currently, also deactived regions are used.
592 (defun LilyPond-command-buffer ()
593 "Run LilyPond on buffer."
594 (interactive)
595 (LilyPond-command-select-buffer)
596 (LilyPond-command-region (point-min) (point-max)))
598 (defun LilyPond-command-expand (string file)
599 (let ((case-fold-search nil))
600 (if (string-match "%" string)
601 (let* ((b (match-beginning 0))
602 (e (+ b 2))
603 (l (split-file-name file))
604 (dir (car l))
605 (base (cadr l)))
606 (LilyPond-command-expand
607 (concat (substring string 0 b)
609 base
610 (let ((entry (assoc (substring string b e)
611 LilyPond-expand-alist)))
612 (if entry (cdr entry) ""))
613 (substring string e))
614 file))
615 string)))
617 (defun LilyPond-shell-process (name buffer command)
618 (let ((old (current-buffer)))
619 (switch-to-buffer-other-window buffer)
620 ;; If we empty the buffer don't see messages scroll by.
621 ;; (erase-buffer)
623 (start-process-shell-command name buffer command)
624 (switch-to-buffer-other-window old)))
627 (defun LilyPond-command (name file)
628 "Run command NAME on the file you get by calling FILE.
630 FILE is a function return a file name. It has one optional argument,
631 the extension to use on the file.
633 Use the information in LilyPond-command-alist to determine how to run the
634 command."
636 (let ((entry (assoc name LilyPond-command-alist)))
637 (if entry
638 (let ((command (LilyPond-command-expand (cadr entry)
639 (apply file nil)))
640 (jobs nil)
641 (job-string "no jobs"))
642 (if (member name (list "View" "ViewPS"))
643 ;; is USR1 a right signal for viewps?
644 (let ((buffer-xdvi (get-buffer-create (concat "*" name "*"))))
645 (if LilyPond-kick-xdvi
646 (let ((process-xdvi (get-buffer-process buffer-xdvi)))
647 (if process-xdvi
648 (signal-process (process-id process-xdvi) 'SIGUSR1)
649 (LilyPond-shell-process name buffer-xdvi command)))
650 (LilyPond-shell-process name buffer-xdvi command)))
651 (progn
652 (if (string-equal name "Midi")
653 (progn
654 (setq command (concat LilyPond-midi-command " " (LilyPond-string-current-midi)))
655 (if (LilyPond-kill-midi)
656 (setq job-string nil)))) ; either stop or start playing
657 (if (string-equal name "MidiAll")
658 (progn
659 (setq command (concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)))
660 (LilyPond-kill-midi))) ; stop and start playing
661 (if (and (member name (list "Midi" "MidiAll")) job-string)
662 (if (file-newer-than-file-p
663 (LilyPond-master-file)
664 (concat (substring (LilyPond-master-file) 0 -3) ".midi"))
665 (if (y-or-n-p "Midi older than source. Reformat midi?")
666 (progn
667 (LilyPond-command-formatmidi)
668 (while (LilyPond-running)
669 (message "Starts playing midi once it is built.")
670 (sit-for 0 100))))))
671 (if (member name (list "LilyPond" "TeX" "2Midi" "2PS" "2Dvi"
672 "Book" "LaTeX"))
673 (if (setq jobs (LilyPond-running))
674 (progn
675 (setq job-string "Process") ; could also suggest compiling after process has ended
676 (while jobs
677 (setq job-string (concat job-string " \"" (pop jobs) "\"")))
678 (setq job-string (concat job-string " is already running; kill it to proceed "))
679 (if (y-or-n-p job-string)
680 (progn
681 (setq job-string "no jobs")
682 (LilyPond-kill-jobs)
683 (while (LilyPond-running)
684 (sit-for 0 100)))
685 (setq job-string nil)))))
687 (setq LilyPond-command-default name)
688 (if (string-equal job-string "no jobs")
689 (LilyPond-compile-file command name))))))))
691 (defun LilyPond-mark-active ()
692 "Check if there is an active mark."
693 (and transient-mark-mode
694 (if (string-match "XEmacs\\|Lucid" emacs-version) (mark) mark-active)))
696 (defun LilyPond-temp-directory ()
697 "Temporary file directory for Commands on Region."
698 (interactive)
699 (if (string-match "XEmacs\\|Lucid" emacs-version)
700 (concat (temp-directory) "/")
701 temporary-file-directory))
703 ;;; Keymap
705 (defvar LilyPond-mode-map ()
706 "Keymap used in `LilyPond-mode' buffers.")
708 ;; Note: if you make changes to the map, you must do
709 ;; M-x set-variable LilyPond-mode-map nil
710 ;; M-x eval-buffer
711 ;; M-x LilyPond-mode
712 ;; to let the changest take effect
714 (if LilyPond-mode-map
716 (setq LilyPond-mode-map (make-sparse-keymap))
717 ;; Put keys to LilyPond-command-alist and fetch them from there somehow.
718 (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
719 (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
720 (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
721 (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-jobs)
722 (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
723 (define-key LilyPond-mode-map "\C-cm" 'LilyPond-command-formatmidi)
724 (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
725 (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
726 (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
727 (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
728 (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
729 (define-key LilyPond-mode-map [(control c) return] 'LilyPond-command-current-midi)
730 (define-key LilyPond-mode-map [(control c) (control return)] 'LilyPond-command-all-midi)
731 (define-key LilyPond-mode-map "\C-x\C-s" 'LilyPond-save-buffer)
732 (define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
733 (define-key LilyPond-mode-map "\C-ci" 'LilyPond-insert-tag-current)
734 ;; the following will should be overriden by Lilypond Quick Insert Mode
735 (define-key LilyPond-mode-map "\C-cq" 'LilyPond-quick-insert-mode)
736 (define-key LilyPond-mode-map "\C-c;" 'LilyPond-comment-region)
737 (define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren)
738 (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren)
739 (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren)
740 (define-key LilyPond-mode-map "]" 'LilyPond-electric-close-paren)
741 (if (string-match "XEmacs\\|Lucid" emacs-version)
742 (define-key LilyPond-mode-map [iso-left-tab] 'LilyPond-autocompletion)
743 (define-key LilyPond-mode-map [iso-lefttab] 'LilyPond-autocompletion))
744 (define-key LilyPond-mode-map "\C-c\t" 'LilyPond-info-index-search)
747 ;;; Menu Support
749 ;;; This mode was originally LilyPond-quick-note-insert by Heikki Junes.
750 ;;; The original version has been junked since CVS-1.97,
751 ;;; in order to merge the efforts done by Nicolas Sceaux.
752 ;;; LilyPond Quick Insert Mode is a major mode, toggled by C-c q.
753 (defun LilyPond-quick-insert-mode ()
754 "Insert notes with fewer key strokes by using a simple keyboard piano."
755 (interactive)
756 (progn
757 (message "Invoke (C-c q) from keyboard. If you still see this message,") (sit-for 5 0)
758 (message "then you have not installed LilyPond Quick Insert Mode (lyqi).") (sit-for 5 0)
759 (message "Download lyqi from http://nicolas.sceaux.free.fr/lilypond/lyqi.html,") (sit-for 5 0)
760 (message "see installation instructions from lyqi's README -file.") (sit-for 5 0)
761 (message "You need also eieio (Enhanced Integration of Emacs Interpreted Objects).") (sit-for 5 0)
762 (message "Download eieio from http://cedet.sourceforge.net/eieio.shtml,") (sit-for 5 0)
763 (message "see installation instructions from eieio's INSTALL -file.") (sit-for 5 0)
764 (message "")
767 (defun LilyPond-pre-word-search ()
768 "Fetch the alphabetic characters and \\ in front of the cursor."
769 (let ((pre "")
770 (prelen 0)
771 (ch (char-before (- (point) 0))))
772 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not bolp, A-Z
773 (and (>= ch 97) (<= ch 122)) ; a-z
774 (= ch 92))) ; \\
775 (setq pre (concat (char-to-string ch) pre))
776 (setq prelen (+ prelen 1))
777 (setq ch (char-before (- (point) prelen))))
778 pre))
780 (defun LilyPond-post-word-search ()
781 "Fetch the alphabetic characters behind the cursor."
782 (let ((post "")
783 (postlen 0)
784 (ch (char-after (+ (point) 0))))
785 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not eolp, A-Z
786 (and (>= ch 97) (<= ch 122)))) ; a-z
787 (setq post (concat post (char-to-string ch)))
788 (setq postlen (+ postlen 1))
789 (setq ch (char-after (+ (point) postlen))))
790 post))
792 (defun LilyPond-autocompletion ()
793 "Show completions in mini-buffer for the given word."
794 (interactive)
795 (let ((pre (LilyPond-pre-word-search))
796 (post (LilyPond-post-word-search))
797 (compsstr ""))
798 ;; insert try-completion and show all-completions
799 (if (> (length pre) 0)
800 (progn
801 (setq trycomp (try-completion pre (LilyPond-add-dictionary-word ())))
802 (if (char-or-string-p trycomp)
803 (if (string-equal (concat pre post) trycomp)
804 (goto-char (+ (point) (length post)))
805 (progn
806 (delete-region (point) (+ (point) (length post)))
807 (insert (substring trycomp (length pre) nil))))
808 (progn
809 (delete-region (point) (+ (point) (length post)))
810 (font-lock-fontify-buffer))) ; only inserting fontifies
812 (setq complist (all-completions pre (LilyPond-add-dictionary-word ())))
813 (while (> (length complist) 0)
814 (setq compsstr (concat compsstr "\"" (car complist) "\" "))
815 (setq complist (cdr complist)))
816 (message compsstr)
817 (sit-for 0 100)))))
819 (defun LilyPond-info ()
820 "Launch Info for lilypond."
821 (interactive)
822 (info "lilypond"))
824 (defun LilyPond-music-glossary-info ()
825 "Launch Info for music-glossary."
826 (interactive)
827 (info "music-glossary"))
829 (defun LilyPond-internals-info ()
830 "Launch Info for lilypond-internals."
831 (interactive)
832 (info "lilypond-internals"))
834 (defun LilyPond-info-index-search ()
835 "In `*info*'-buffer, launch `info lilypond --index-search word-under-cursor'"
836 (interactive)
837 (let ((str (concat (LilyPond-pre-word-search) (LilyPond-post-word-search))))
838 (if (and (> (length str) 0)
839 (string-equal (substring str 0 1) "\\"))
840 (setq str (substring str 1 nil)))
841 (LilyPond-info)
842 (Info-index str)))
844 (defun LilyPond-insert-tag-current (&optional word)
845 "Set the current tag to be inserted."
846 (interactive)
847 (if word
848 (setq LilyPond-insert-tag-current word))
849 (if (memq LilyPond-insert-tag-current LilyPond-menu-keywords)
850 (LilyPond-insert-tag)
851 (message "No tag was selected from LilyPond->Insert tag-menu.")))
853 (defun LilyPond-insert-tag ()
854 "Insert syntax for given tag. The definitions are in LilyPond-words-filename."
855 (interactive)
856 (setq b (find-file-noselect (LilyPond-words-filename) t t))
857 (let ((word LilyPond-insert-tag-current)
858 (found nil)
859 (p nil)
860 (query nil)
861 (m (set-marker (make-marker) 1 (get-buffer b)))
862 (distance (if (LilyPond-mark-active)
863 (abs (- (mark-marker) (point-marker))) 0))
865 ;; find the place first
866 (if (LilyPond-mark-active)
867 (goto-char (min (mark-marker) (point-marker))))
868 (while (and (not found) (> (buffer-size b) (marker-position m)))
869 (setq copy (car (copy-alist (list (eval (symbol-name (read m)))))))
870 (if (string-equal word copy) (setq found t)))
871 (if found (insert word))
872 (if (> (buffer-size b) (marker-position m))
873 (setq copy (car (copy-alist (list (eval (symbol-name (read m))))))))
874 (if (not (string-equal "-" copy))
875 (setq found nil))
876 (while (and found (> (buffer-size b) (marker-position m)))
877 ;; find next symbol
878 (setq copy (car (copy-alist (list (eval (symbol-name (read m)))))))
879 ;; check whether it is the word, or the word has been found
880 (cond
881 ((string-equal "-" copy) (setq found nil))
882 ((string-equal "%" copy) (insert " " (read-string "Give Arguments: ")))
883 ((string-equal "_" copy)
884 (progn
885 (setq p (point))
886 (goto-char (+ p distance))))
887 ((string-equal "\?" copy) (setq query t))
888 ((string-equal "\!" copy) (setq query nil))
889 ((string-equal "\\n" copy)
890 (if (not query)
891 (progn (LilyPond-indent-line) (insert "\n") (LilyPond-indent-line))))
892 ((string-equal "{" copy)
893 (if (not query)
894 (progn (insert " { "))))
895 ((string-equal "}" copy)
896 (if (not query)
897 (progn (insert " } ") (setq query nil) )))
898 ((not query)
899 (insert copy))
900 (query
901 (if (y-or-n-p (concat "Proceed with `" copy "'? "))
902 (progn (insert copy) (setq query nil))))
904 (if p (goto-char p))
905 (kill-buffer b))
908 (defun LilyPond-command-menu-entry (entry)
909 ;; Return LilyPond-command-alist ENTRY as a menu item.
910 (let ((name (car entry)))
911 (cond ((and (string-equal name LilyPond-command-Print)
912 LilyPond-printer-list)
913 (let ((command LilyPond-print-command)
914 (lookup 1))
915 (append (list LilyPond-command-Print)
916 (mapcar 'LilyPond-command-menu-printer-entry
917 LilyPond-printer-list))))
919 (vector name (list 'LilyPond-command-menu name) t)))))
922 (easy-menu-define LilyPond-command-menu
923 LilyPond-mode-map
924 "Menu used in LilyPond mode."
925 (append '("Command")
926 '(("Command on"
927 [ "Master File" LilyPond-command-select-master
928 :keys "C-c C-c" :style radio
929 :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
930 [ "Buffer" LilyPond-command-select-buffer
931 :keys "C-c C-b" :style radio
932 :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
933 [ "Region" LilyPond-command-select-region
934 :keys "C-c C-r" :style radio
935 :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
936 ;;; (let ((file 'LilyPond-command-on-current))
937 ;;; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
938 ;;; Some kind of mapping which includes :keys might be more elegant
939 ;;; Put keys to LilyPond-command-alist and fetch them from there somehow.
940 '([ "LilyPond" LilyPond-command-lilypond t])
941 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
942 '([ "2Dvi" LilyPond-command-formatdvi t])
943 '([ "2PS" LilyPond-command-formatps t])
944 '([ "2Midi" LilyPond-command-formatmidi t])
945 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
946 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
947 '([ "Kill jobs" LilyPond-kill-jobs t])
948 '("-----")
949 '([ "SmartView" LilyPond-command-smartview t])
950 '([ "View" LilyPond-command-view t])
951 '([ "ViewPS" LilyPond-command-viewps t])
952 '("-----")
953 '([ "Midi (toggle)" LilyPond-command-current-midi t])
954 '([ "Midi all" LilyPond-command-all-midi t])
957 (defun LilyPond-menu-keywords-item (arg)
958 "Make vector for LilyPond-mode-keywords."
959 (vector arg (list 'LilyPond-insert-tag-current arg) :style 'radio :selected (list 'eq 'LilyPond-insert-tag-current arg)))
961 (defun LilyPond-menu-keywords ()
962 "Make Insert Tag menu.
964 The Insert Tag -menu is splitted into parts if it is long enough."
966 (let ((li (mapcar 'LilyPond-menu-keywords-item LilyPond-menu-keywords))
967 (w (round (sqrt (length LilyPond-menu-keywords))))
968 (splitted '())
969 (imin 0) imax lw rw)
970 (while (< imin (length LilyPond-menu-keywords))
971 (setq imax (- (min (+ imin w) (length LilyPond-menu-keywords)) 1))
972 (setq lw (nth imin LilyPond-menu-keywords))
973 (setq rw (nth imax LilyPond-menu-keywords))
974 (add-to-list 'splitted
975 (let ((l (list (concat (substring lw 0 (min 7 (length lw)))
976 " ... "
977 (substring rw 0 (min 7 (length rw)))))))
978 (while (<= imin imax)
979 (add-to-list 'l (nth imin li))
980 (setq imin (1+ imin)))
981 (reverse l))))
982 (if (> (length LilyPond-menu-keywords) 12) (reverse splitted) li)))
984 ;;; LilyPond-mode-menu should not be interactive, via "M-x LilyPond-<Tab>"
985 (easy-menu-define LilyPond-mode-menu
986 LilyPond-mode-map
987 "Menu used in LilyPond mode."
988 (append '("LilyPond")
989 '(["Add index menu" LilyPond-add-imenu-menu])
990 (list (cons "Insert tag"
991 (cons ["Previously selected" LilyPond-insert-tag-current t]
992 (cons "-----"
993 (LilyPond-menu-keywords)))))
994 '(("Miscellaneous"
995 ["Autocompletion" LilyPond-autocompletion t]
996 ["(Un)comment Region" LilyPond-comment-region t]
997 ["Refontify buffer" font-lock-fontify-buffer t]
998 "-----"
999 ["Quick Insert Mode" LilyPond-quick-insert-mode :keys "C-c q"]
1001 '(("Info"
1002 ["LilyPond" LilyPond-info t]
1003 ["LilyPond index-search" LilyPond-info-index-search t]
1004 ["Music Glossary" LilyPond-music-glossary-info t]
1005 ["LilyPond internals" LilyPond-internals-info t]
1009 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z]+\\) *="
1010 "Regexp matching Identifier definitions.")
1012 (defvar LilyPond-imenu-generic-expression
1013 (list (list nil LilyPond-imenu-generic-re 1))
1014 "Expression for imenu")
1016 (defun LilyPond-command-select-master ()
1017 (interactive)
1018 (message "Next command will be on the master file")
1019 (setq LilyPond-command-current 'LilyPond-command-master))
1021 (defun LilyPond-command-select-buffer ()
1022 (interactive)
1023 (message "Next command will be on the buffer")
1024 (setq LilyPond-command-current 'LilyPond-command-buffer))
1026 (defun LilyPond-command-select-region ()
1027 (interactive)
1028 (message "Next command will be on the region")
1029 (setq LilyPond-command-current 'LilyPond-command-region))
1031 (defun LilyPond-command-menu (name)
1032 ;; Execute LilyPond-command-alist NAME from a menu.
1033 (let ((LilyPond-command-force name))
1034 (if (eq LilyPond-command-current 'LilyPond-command-region)
1035 (if (eq (mark t) nil)
1036 (progn (message "The mark is not set now") (sit-for 0 500))
1037 (progn (if (not (not (LilyPond-mark-active)))
1038 (progn (message "Region is not active, using region between inactive mark and current point.") (sit-for 0 500)))
1039 (LilyPond-command-region (mark t) (point))))
1040 (funcall LilyPond-command-current))))
1042 (defun LilyPond-add-imenu-menu ()
1043 (interactive)
1044 "Add an imenu menu to the menubar."
1045 (if (not LilyPond-imenu)
1046 (progn
1047 (imenu-add-to-menubar "Index")
1048 (redraw-frame (selected-frame))
1049 (setq LilyPond-imenu t))
1050 (message "%s" "LilyPond-imenu already exists.")))
1051 (put 'LilyPond-add-imenu-menu 'menu-enable '(not LilyPond-imenu))
1053 (defun LilyPond-mode ()
1054 "Major mode for editing LilyPond music files.
1056 This mode knows about LilyPond keywords and line comments, not about
1057 indentation or block comments. It features easy compilation, error
1058 finding and viewing of a LilyPond source buffer or region.
1060 COMMANDS
1061 \\{LilyPond-mode-map}
1062 VARIABLES
1064 LilyPond-command-alist\t\talist from name to command
1065 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
1066 (interactive)
1067 ;; set up local variables
1068 (kill-all-local-variables)
1070 (make-local-variable 'font-lock-defaults)
1071 (setq font-lock-defaults '(LilyPond-font-lock-keywords))
1073 ;; string and comments are fontified explicitly
1074 (make-local-variable 'font-lock-keywords-only)
1075 (setq font-lock-keywords-only t)
1077 ;; Multi-line font-locking needs Emacs 21.1 or newer.
1078 ;; For older versions there is hotkey "C-c f".
1079 (make-local-variable 'font-lock-multiline)
1080 (setq font-lock-multiline t)
1082 (make-local-variable 'paragraph-separate)
1083 (setq paragraph-separate "^[ \t]*$")
1085 (make-local-variable 'paragraph-start)
1086 (setq paragraph-start "^[ \t]*$")
1088 (make-local-variable 'comment-start)
1089 (setq comment-start "%")
1091 (make-local-variable 'comment-start-skip)
1092 (setq comment-start-skip "%{? *")
1094 (make-local-variable 'comment-end)
1095 (setq comment-end "")
1097 (make-local-variable 'block-comment-start)
1098 (setq block-comment-start "%{")
1100 (make-local-variable 'block-comment-end)
1101 (setq block-comment-end "%}")
1103 (make-local-variable 'indent-line-function)
1104 (setq indent-line-function 'LilyPond-indent-line)
1106 (LilyPond-mode-set-syntax-table '(?\< ?\> ?\{ ?\}))
1107 (setq major-mode 'LilyPond-mode)
1108 (setq mode-name "LilyPond")
1109 (setq local-abbrev-table LilyPond-mode-abbrev-table)
1110 (use-local-map LilyPond-mode-map)
1112 ;; In XEmacs imenu was synched up with: FSF 20.4
1113 (make-local-variable 'imenu-generic-expression)
1114 (setq imenu-generic-expression LilyPond-imenu-generic-expression)
1115 ;; (imenu-add-to-menubar "Index") ; see LilyPond-add-imenu-menu
1117 ;; In XEmacs one needs to use 'easy-menu-add'.
1118 (if (string-match "XEmacs\\|Lucid" emacs-version)
1119 (progn
1120 (easy-menu-add LilyPond-mode-menu)
1121 (easy-menu-add LilyPond-command-menu)))
1123 ;; Use Command on Region even for inactive mark (region).
1124 (if (string-match "XEmacs\\|Lucid" emacs-version)
1125 (setq zmacs-regions nil)
1126 (setq mark-even-if-inactive t))
1128 ;; Context dependent syntax tables in LilyPond-mode
1129 (make-local-hook 'post-command-hook) ; XEmacs requires
1130 (add-hook 'post-command-hook 'LilyPond-mode-context-set-syntax-table nil t)
1132 ;; Turn on paren-mode buffer-locally, i.e., in LilyPond-mode
1133 (if (string-match "XEmacs\\|Lucid" emacs-version)
1134 (progn
1135 (make-local-variable 'paren-mode)
1136 (paren-set-mode 'paren)
1137 (make-local-variable 'blink-matching-paren)
1138 (setq blink-matching-paren t)
1140 (progn
1141 (make-local-variable 'blink-matching-paren-on-screen)
1142 (setq blink-matching-paren-on-screen t)
1145 ;; run the mode hook. LilyPond-mode-hook use is deprecated
1146 (run-hooks 'LilyPond-mode-hook))
1148 (defun LilyPond-version ()
1149 "Echo the current version of `LilyPond-mode' in the minibuffer."
1150 (interactive)
1151 (message "Using `LilyPond-mode' version %s" LilyPond-version))
1153 (load-library "lilypond-font-lock")
1154 (load-library "lilypond-indent")
1156 (provide 'lilypond-mode)
1157 ;;; lilypond-mode.el ends here