(Lyric_combine_music_iterator): small bugfix
[lilypond.git] / lilypond-mode.el
blob6c947e00e6a35a39af9914bbd24193c9b3cc4b11
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>
7 ;;;
8 ;;; Changed 2001 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)
13 ;;; Inspired on auctex
15 ;;;
16 ;;; Add this to your ~/.emacs or ~/.emacs.el
17 ;;; (load-library "lilypond-mode.el")
18 ;;; (setq auto-mode-alist
19 ;;; (cons '("\\.ly$" . LilyPond-mode) auto-mode-alist))
20 ;;;
22 (load-library "lilypond-font-lock")
23 (load-library "lilypond-indent")
25 (require 'easymenu)
26 (require 'compile)
28 (defconst LilyPond-version "1.5.58"
29 "`LilyPond-mode' version number.")
31 (defconst LilyPond-help-address "bug-lilypond@gnu.org"
32 "Address accepting submission of bug reports.")
34 (defvar LilyPond-mode-hook nil
35 "*Hook called by `LilyPond-mode'.")
37 (defvar LilyPond-kick-xdvi nil
38 "If true, no simultaneous xdvi's are started, but reload signal is sent.")
40 (defvar LilyPond-command-history nil
41 "Command history list.")
43 (defvar LilyPond-regexp-alist
44 '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
45 "Regexp used to match LilyPond errors. See `compilation-error-regexp-alist'.")
47 (defcustom LilyPond-include-path ".:/tmp"
48 "* LilyPond include path."
49 :type 'string
50 :group 'LilyPond)
53 (defun LilyPond-check-files (derived originals extensions)
54 "Check that DERIVED is newer than any of the ORIGINALS.
55 Try each original with each member of EXTENSIONS, in all directories
56 in LilyPond-include-path."
57 (let ((found nil)
58 (regexp (concat "\\`\\("
59 (mapconcat (function (lambda (dir)
60 (regexp-quote (expand-file-name dir))))
61 LilyPond-include-path "\\|")
62 "\\).*\\("
63 (mapconcat 'regexp-quote originals "\\|")
64 "\\)\\.\\("
65 (mapconcat 'regexp-quote extensions "\\|")
66 "\\)\\'"))
67 (buffers (buffer-list)))
68 (while buffers
69 (let* ((buffer (car buffers))
70 (name (buffer-file-name buffer)))
71 (setq buffers (cdr buffers))
72 (if (and name (string-match regexp name))
73 (progn
74 (and (buffer-modified-p buffer)
75 (or (not LilyPond-save-query)
76 (y-or-n-p (concat "Save file "
77 (buffer-file-name buffer)
78 "? ")))
79 (save-excursion (set-buffer buffer) (save-buffer)))
80 (if (file-newer-than-file-p name derived)
81 (setq found t))))))
82 found))
84 (defun LilyPond-running ()
85 (let ((process (get-process "lilypond")))
86 (and process
87 (eq (process-status process) 'run))))
89 (defun Midi-running ()
90 (let ((process (get-process "midi")))
91 (and process
92 (eq (process-status process) 'run))))
94 (defun LilyPond-kill-job ()
95 "Kill the currently running LilyPond job."
96 (interactive)
97 ;; What bout TeX, Xdvi?
98 (quit-process (get-process "lilypond") t))
100 ;; URG, should only run LilyPond-compile for LilyPond
101 ;; not for tex,xdvi (ly2dvi?)
102 (defun LilyPond-compile-file (command name)
103 ;; We maybe should know what we run here (Lily, ly2dvi, tex)
104 ;; and adjust our error-matching regex ?
105 (compile-internal command "No more errors" name ))
107 ;; do we still need this, now that we're using compile-internal?
108 (defun LilyPond-save-buffer ()
109 "Save buffer and set default command for compiling."
110 (interactive)
111 (if (buffer-modified-p)
112 (progn (save-buffer)
113 (setq LilyPond-command-default "LilyPond"))))
115 ;;; return (dir base ext)
116 (defun split-file-name (name)
117 (let* ((i (string-match "[^/]*$" name))
118 (dir (if (> i 0) (substring name 0 i) "./"))
119 (file (substring name i (length name)))
120 (i (string-match "[^.]*$" file)))
121 (if (and
122 (> i 0)
123 (< i (length file)))
124 (list dir (substring file 0 (- i 1)) (substring file i (length file)))
125 (list dir file ""))))
128 ;; Should check whether in command-alist?
129 (defcustom LilyPond-command-default "LilyPond"
130 "Default command. Must identify a member of LilyPond-command-alist."
132 :group 'LilyPond
133 :type 'string)
134 ;;;(make-variable-buffer-local 'LilyPond-command-last)
136 (defvar LilyPond-command-current 'LilyPond-command-master)
137 ;;;(make-variable-buffer-local 'LilyPond-command-master)
140 ;; If non-nil, LilyPond-command-query will return the value of this
141 ;; variable instead of quering the user.
142 (defvar LilyPond-command-force nil)
144 (defcustom LilyPond-xdvi-command "xdvi"
145 "Command used to display DVI files."
147 :group 'LilyPond
148 :type 'string)
150 (defcustom LilyPond-gv-command "gv -watch"
151 "Command used to display PS files."
153 :group 'LilyPond
154 :type 'string)
156 (defcustom LilyPond-midi-command "timidity"
157 "Command used to play MIDI files."
159 :group 'LilyPond
160 :type 'string)
162 (defcustom LilyPond-midi-all-command "timidity -ig"
163 "Command used to play MIDI files."
165 :group 'LilyPond
166 :type 'string)
168 ;; This is the major configuration variable.
169 (defcustom LilyPond-command-alist
171 ("LilyPond" . ("lilypond %s" . "TeX"))
172 ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
174 ("2Dvi" . ("ly2dvi %s" . "View"))
175 ("2PS" . ("ly2dvi -P %s" . "View"))
176 ("2Midi" . ("lilypond -m %s" . "View"))
178 ("Book" . ("lilypond-book %x" . "LaTeX"))
179 ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
181 ;; point-n-click (arg: exits upop USR1)
182 ("SmartView" . ("xdvi %d" . "LilyPond"))
184 ;; refreshes when kicked USR1
185 ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
187 ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
189 ("Midi" . (,(concat LilyPond-midi-command " %m") . "LilyPond"))
192 "AList of commands to execute on the current document.
194 The key is the name of the command as it will be presented to the
195 user, the value is a cons of the command string handed to the shell
196 after being expanded, and the next command to be executed upon
197 success. The expansion is done using the information found in
198 LilyPond-expand-list.
200 :group 'LilyPond
201 :type '(repeat (cons :tag "Command Item"
202 (string :tag "Key")
203 (cons :tag "How"
204 (string :tag "Command")
205 (string :tag "Next Key")))))
207 ;; drop this?
208 (defcustom LilyPond-file-extensions '(".ly" ".sly" ".fly")
209 "*File extensions used by manually generated TeX files."
210 :group 'LilyPond
211 :type '(repeat (string :format "%v")))
214 (defcustom LilyPond-expand-alist
216 ("%s" . ".ly")
217 ("%t" . ".tex")
218 ("%d" . ".dvi")
219 ("%p" . ".ps")
220 ("%l" . ".latex")
221 ("%x" . ".tely")
222 ("%m" . ".midi")
225 "Alist of expansion strings for LilyPond command names."
226 :group 'LilyPond
227 :type '(repeat (cons :tag "Alist item"
228 (string :tag "Symbol")
229 (string :tag "Expansion"))))
232 (defcustom LilyPond-command-Show "View"
233 "*The default command to show (view or print) a LilyPond file.
234 Must be the car of an entry in `LilyPond-command-alist'."
235 :group 'LilyPond
236 :type 'string)
237 (make-variable-buffer-local 'LilyPond-command-Show)
239 (defcustom LilyPond-command-Print "Print"
240 "The name of the Print entry in LilyPond-command-Print."
241 :group 'LilyPond
242 :type 'string)
244 (defun xLilyPond-compile-sentinel (process msg)
245 (if (and process
246 (= 0 (process-exit-status process)))
247 (setq LilyPond-command-default
248 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
250 ;; FIXME: shouldn't do this for stray View/xdvi
251 (defun LilyPond-compile-sentinel (buffer msg)
252 (if (string-match "^finished" msg)
253 (setq LilyPond-command-default
254 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
256 ;;(make-variable-buffer-local 'compilation-finish-function)
257 (setq compilation-finish-function 'LilyPond-compile-sentinel)
259 (defun LilyPond-command-query (name)
260 "Query the user for what LilyPond command to use."
261 (let* ((default (cond ((if (string-equal name "emacs-lily")
262 (LilyPond-check-files (concat name ".tex")
263 (list name)
264 LilyPond-file-extensions)
265 (if (buffer-modified-p)
266 (if (y-or-n-p "Save buffer before next command? ")
267 (LilyPond-save-buffer)))
268 ;;"LilyPond"
269 LilyPond-command-default))
270 (t LilyPond-command-default)))
272 (completion-ignore-case t)
274 (answer (or LilyPond-command-force
275 (completing-read
276 (concat "Command: (default " default ") ")
277 LilyPond-command-alist nil t nil 'LilyPond-command-history))))
279 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
280 (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
281 (if (and answer
282 (not (string-equal answer "")))
283 answer
284 default))))
287 ;; FIXME: find ``\score'' in buffers / make settable?
288 (defun LilyPond-master-file ()
289 ;; duh
290 (buffer-file-name))
292 (defun LilyPond-command-master ()
293 "Run command on the current document."
294 (interactive)
295 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
296 'LilyPond-master-file))
298 (defun LilyPond-command-lilypond ()
299 "Run lilypond for the current document."
300 (interactive)
301 (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file)
304 (defun LilyPond-command-formatdvi ()
305 "Format the dvi output of the current document."
306 (interactive)
307 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
310 (defun LilyPond-command-formatps ()
311 "Format the ps output of the current document."
312 (interactive)
313 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
316 (defun LilyPond-command-smartview ()
317 "View the dvi output of current document."
318 (interactive)
319 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
322 (defun LilyPond-command-view ()
323 "View the dvi output of current document."
324 (interactive)
325 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
328 (defun LilyPond-command-viewps ()
329 "View the ps output of current document."
330 (interactive)
331 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
334 (defun LilyPond-command-midi ()
335 "Play midi corresponding to the current document."
336 (interactive)
337 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file)
340 (defun count-rexp (start end rexp)
341 "Print number of found regular expressions in the region."
342 (interactive "r")
343 (save-excursion
344 (save-restriction
345 (narrow-to-region start end)
346 (goto-char (point-min))
347 (count-matches rexp))))
349 (defun count-midi-words ()
350 "Print number of scores before the curser."
351 (interactive)
352 (count-rexp (point-min) (point-max) "\\\\midi"))
354 (defun count-midi-words-backwards ()
355 "Print number of scores before the curser."
356 (interactive)
357 (count-rexp (point-min) (point) "\\\\midi"))
359 (defun LilyPond-command-next-midi ()
360 "Play next midi score of the current document."
361 (interactive)
362 (if (Midi-running)
363 (quit-process (get-process "midi") t)
364 (LilyPond-compile-file
365 (let ((fname (LilyPond-master-file))
366 (allcount (string-to-number (substring (count-midi-words) 0 -12)))
367 (count (string-to-number (substring (count-midi-words-backwards) 0 -12))))
368 (concat LilyPond-midi-command " "
369 (substring fname 0 -3) ; suppose ".ly"
370 (if (and (> allcount 1) (> count 0)) ; not first score
371 (if (eq count allcount) ; last score
372 (concat "-" (number-to-string (+ count -1)))
373 (concat "-" (number-to-string count))))
374 ".midi"))
375 "Midi")))
377 (defun LilyPond-command-all-midi ()
378 "Play next midi score of the current document."
379 (interactive)
380 (if (Midi-running)
381 (quit-process (get-process "midi") t)
382 (LilyPond-compile-file
383 (let ((fname (LilyPond-master-file))
384 (allcount (string-to-number (substring (count-midi-words) 0 -12))))
385 (concat LilyPond-midi-all-command " "
386 (if (> allcount 0) ; at least one midi-score
387 (concat (substring fname 0 -3) ".midi "))
388 (if (> allcount 1) ; more than one midi-score
389 (concat (substring fname 0 -3) "-?.midi "))
390 (if (> allcount 9) ; etc.
391 (concat (substring fname 0 -3) "-??.midi"))
392 (if (> allcount 99) ; not first score
393 (concat (substring fname 0 -3) "-???.midi"))))
394 "Midi")))
396 (defun LilyPond-un-comment-region (start end level)
397 "Remove up to LEVEL comment characters from each line in the region."
398 (interactive "*r\np")
399 (comment-region start end (- level)))
401 ;; FIXME, this is broken
402 (defun LilyPond-region-file (begin end)
403 (let (
404 ;; (dir "/tmp/")
405 ;; urg
406 (dir "./")
407 (base "emacs-lily")
408 ;; Hmm
409 (ext (if (string-match "^[\\]score" (buffer-substring begin end))
410 ".ly"
411 (if (< 50 (abs (- begin end)))
412 ".fly"
413 ".sly"))))
414 (concat dir base ext)))
416 (defun LilyPond-command-region (begin end)
417 "Run LilyPond on the current region."
418 (interactive "r")
419 (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
420 (LilyPond-command (LilyPond-command-query
421 (LilyPond-region-file begin end))
422 '(lambda () (LilyPond-region-file begin end))))
424 (defun LilyPond-command-buffer ()
425 "Run LilyPond on buffer."
426 (interactive)
427 (LilyPond-command-region (point-min) (point-max)))
429 (defun LilyPond-command-expand (string file)
430 (let ((case-fold-search nil))
431 (if (string-match "%" string)
432 (let* ((b (match-beginning 0))
433 (e (+ b 2))
434 (l (split-file-name file))
435 (dir (car l))
436 (base (cadr l)))
437 (LilyPond-command-expand
438 (concat (substring string 0 b)
440 base
441 (let ((entry (assoc (substring string b e)
442 LilyPond-expand-alist)))
443 (if entry (cdr entry) ""))
444 (substring string e))
445 file))
446 string)))
448 (defun LilyPond-shell-process (name buffer command)
449 (let ((old (current-buffer)))
450 (switch-to-buffer-other-window buffer)
451 ;; If we empty the buffer don't see messages scroll by.
452 ;; (erase-buffer)
454 (start-process-shell-command name buffer command)
455 (switch-to-buffer-other-window old)))
458 (defun LilyPond-command (name file)
459 "Run command NAME on the file you get by calling FILE.
461 FILE is a function return a file name. It has one optional argument,
462 the extension to use on the file.
464 Use the information in LilyPond-command-alist to determine how to run the
465 command."
467 (let ((entry (assoc name LilyPond-command-alist)))
468 (if entry
469 (let ((command (LilyPond-command-expand (cadr entry)
470 (apply file nil))))
471 (if (string-equal name "View")
472 (let ((buffer-xdvi (get-buffer-create "*view*")))
473 (if LilyPond-kick-xdvi
474 (let ((process-xdvi (get-buffer-process buffer-xdvi)))
475 (if process-xdvi
476 (signal-process (process-id process-xdvi) 'SIGUSR1)
477 (LilyPond-shell-process name buffer-xdvi command)))
478 (LilyPond-shell-process name buffer-xdvi command)))
479 (progn
480 (setq LilyPond-command-default name)
481 (LilyPond-compile-file command name)))))))
483 ;; XEmacs stuff
484 ;; Sadly we need this for a macro in Emacs 19.
485 (eval-when-compile
486 ;; Imenu isn't used in XEmacs, so just ignore load errors.
487 (condition-case ()
488 (require 'imenu)
489 (error nil)))
492 ;;; Keymap
494 (defvar LilyPond-mode-map ()
495 "Keymap used in `LilyPond-mode' buffers.")
497 ;; Note: if you make changes to the map, you must do
498 ;; M-x set-variable LilyPond-mode-map nil
499 ;; M-x eval-buffer
500 ;; M-x LilyPond-mode
501 ;; to let the changest take effect
503 (if LilyPond-mode-map
505 (setq LilyPond-mode-map (make-sparse-keymap))
506 (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
507 (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
508 (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
509 (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-job)
510 (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
511 (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
512 (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
513 (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
514 (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
515 (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
516 (define-key LilyPond-mode-map "\C-c\C-m" 'LilyPond-command-next-midi)
517 (define-key LilyPond-mode-map "\C-x\C-s" 'LilyPond-save-buffer)
518 (define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
519 (define-key LilyPond-mode-map "\C-ci" 'LilyPond-quick-note-insert)
520 (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
521 (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
522 (define-key LilyPond-mode-map "\C-c:" 'LilyPond-un-comment-region)
523 (define-key LilyPond-mode-map "\C-c;" 'comment-region)
524 (define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren)
525 (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren)
526 (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren)
529 ;;; Menu Support
531 (defun LilyPond-quick-note-insert()
532 "Insert notes with fewer key strokes by using a simple keyboard piano."
533 (interactive)
534 (setq dutch-notes
535 '(("k" "a") ("l" "b") ("a" "c") ("s" "d")
536 ("d" "e") ("f" "f") ("j" "g") ("r" "r")))
537 (setq dutch-note-ends '("eses" "es" "" "is" "isis"))
538 (setq dutch-note-replacements '("" ""))
539 (setq finnish-note-replacements
540 '(("eeses" "eses") ("ees" "es") ("aeses" "asas") ("aes" "as") ("b" "h")
541 ("beses" "heses") ("bes" "b") ("bis" "his") ("bisis" "hisis")))
542 ; add more translations of the note names
543 (setq other-keys "()<>~}")
544 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)
546 (message "Press h for help.") (sit-for 0 750 1)
548 (setq note-replacements dutch-note-replacements)
549 (while (not (= 27 ; esc to quit
550 (setq x (read-char-exclusive
551 (format " | a[_]s[_]d | f[_]j[_]k[_]l | r with ie ,' 12345678 . 0 (<~>)/}\\b\\n Esc \n | c | d | e | f | g | a | %s | r with %s%s%s%s"
552 (if (string= (car(cdr(assoc "b" note-replacements))) "h")
553 "h" "b")
554 (nth (+ accid 2) dutch-note-ends)
555 (make-string (abs octav) (if (> octav 0) ?' ?,))
556 durat
557 (if (string= durat "") "" (make-string dots ?.)))))))
558 ; (insert (number-to-string x)) ; test numbers for characters
559 (setq note (cdr (assoc (char-to-string x) dutch-notes)))
560 (cond
561 ((= x 127) (backward-kill-word 1)) ; backspace
562 ((= x 13) (progn (insert "\n") (LilyPond-indent-line)))) ; return
563 (setq x (char-to-string x))
564 (cond
565 ((and (string< x "9") (string< "0" x))
566 (progn (setq durat (int-to-string (expt 2 (- (string-to-int x) 1))))
567 (setq dots 0)))
568 ((string= x " ") (insert " "))
569 ((string= x "/") (progn (insert "\\times ")
570 (while (not (and (string< x "9") (string< "0" x)))
571 (setq x (char-to-string (read-char-exclusive "Insert a number for the denominator (\"x/\")"))))
572 (insert (format "%s/" x)) (setq x "/")
573 (while (not (and (string< x "9") (string< "0" x)))
574 (setq x (char-to-string (read-char-exclusive "Insert a number for the numerator (\"/y\")"))))
575 (insert (format "%s { " x))))
576 ((string= x "0") (progn (setq accid 0) (setq octav 0)
577 (setq durat "") (setq dots 0)))
578 ((string= x "i") (setq accid (if (= accid 2) 0 (max (+ accid 1) 1))))
579 ((string= x "e") (setq accid (if (= accid -2) 0 (min (+ accid -1) -1))))
580 ((string= x "'") (setq octav (if (= octav 4) 0 (max (+ octav 1) 1))))
581 ((string= x ",") (setq octav (if (= octav -4) 0 (min (+ octav -1) -1))))
582 ((string= x ".") (setq dots (if (= dots 4) 0 (+ dots 1))))
583 ((not (null (member x (split-string other-keys ""))))
584 (insert (format "%s " x)))
585 ((not (null note))
586 (progn
587 (setq note
588 (format "%s%s" (car note) (if (string= "r" (car note)) ""
589 (nth (+ accid 2) dutch-note-ends))))
590 (setq notetwo (car (cdr (assoc note note-replacements))))
591 (if (not (null notetwo)) (setq note notetwo))
592 (insert
593 (format "%s%s%s%s "
594 note
595 (if (string= "r" note) ""
596 (make-string (abs octav) (if (> octav 0) ?' ?,)))
597 durat
598 (if (string= durat "") "" (make-string dots ?.))))
599 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)))
600 ((string= x "t") (progn (setq note-replacements dutch-note-replacements)
601 (message "Selected Dutch notes")
602 (sit-for 0 750 1))) ; t
603 ((string= x "n") (progn (setq note-replacements finnish-note-replacements)
604 (message "Selected Finnish/Deutsch notes")
605 (sit-for 0 750 1))) ; n
606 ; add more translations of the note names
607 ((string= x "h")
608 (progn (message "Insert notes with fewer key strokes. For example \"i,5.f\" produces \"fis,32. \".") (sit-for 5 0 1)
609 (message "Add also \"a ~ a\"-ties, \"a ( ) b\"-slurs and \"< a b >\"-chords.") (sit-for 5 0 1)
610 (message "Note names are in Du(t)ch by default. Hit 'n' for Fin(n)ish/Deutsch note names.") (sit-for 5 0 1)
611 (message "Backspace deletes last note, return starts a new indented line and Esc quits.") (sit-for 5 0 1)
612 (message "Insert note triplets \"\\times 2/3 { a b } \" by typing \"/23ab}\".") (sit-for 5 0 1)
613 (message "Remember to add all other details as well.") (sit-for 5 0 1)))
616 (define-skeleton LilyPond-insert-tag-notes
617 "LilyPond notes tag."
619 ; (if (bolp) nil ?\n)
620 "\\notes"
621 (if (y-or-n-p "Set \"\\relative\" attribute? ")
622 (concat " \\relative " (skeleton-read "Relative: " "" str)))
623 " { " _ " }")
625 (define-skeleton LilyPond-insert-tag-score
626 "LilyPond score tag."
628 (if (bolp) nil ?\n)
629 "\\score {\n"
630 " " _ "\n"
631 " \\paper { }\n"
632 (if (y-or-n-p "Insert \"\\header\" field? ")
633 (concat " \\header {\n "
634 (skeleton-read "Piece: " "piece = " str) "\n"
635 (if (y-or-n-p "Insert \"opus\" field? ")
636 (concat " " (skeleton-read "Opus: " "opus = " str) "\n"))
637 " }\n"))
638 (if (y-or-n-p "Insert \"\\midi\" field? ")
639 (concat " \\midi { "
640 (skeleton-read "Midi: " "\\tempo 4 = " str)
641 " }\n"))
642 "}\n")
644 (defun LilyPond-command-menu-entry (entry)
645 ;; Return LilyPond-command-alist ENTRY as a menu item.
646 (let ((name (car entry)))
647 (cond ((and (string-equal name LilyPond-command-Print)
648 LilyPond-printer-list)
649 (let ((command LilyPond-print-command)
650 (lookup 1))
651 (append (list LilyPond-command-Print)
652 (mapcar 'LilyPond-command-menu-printer-entry
653 LilyPond-printer-list))))
655 (vector name (list 'LilyPond-command-menu name) t)))))
658 (easy-menu-define LilyPond-command-menu
659 LilyPond-mode-map
660 "Menu used in LilyPond mode."
661 (append '("Command")
662 '(("Command on"
663 [ "Master File" LilyPond-command-select-master
664 :keys "C-c C-c" :style radio
665 :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
666 [ "Buffer" LilyPond-command-select-buffer
667 :keys "C-c C-b" :style radio
668 :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
669 [ "Region" LilyPond-command-select-region
670 :keys "C-c C-r" :style radio
671 :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
672 ; (let ((file 'LilyPond-command-on-current))
673 ; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
674 ;;; Some kind of mapping which includes :keys might be more elegant
675 '([ "LilyPond" (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file) :keys "C-c C-l"])
676 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
677 '([ "2Dvi" (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file) :keys "C-c C-d"])
678 '([ "2PS" (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file) :keys "C-c C-f"])
679 '([ "2Midi" (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-master-file)])
680 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
681 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
682 '([ "SmartView" (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file) :keys "C-c C-s"])
683 '([ "View" (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file) :keys "C-c C-v"])
684 '([ "ViewPS" (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file) :keys "C-c C-p"])
685 '([ "Midi (off)" (LilyPond-command-next-midi) :keys "C-c C-m"])
686 '([ "Midi all" (LilyPond-command-all-midi)])
689 (easy-menu-define LilyPond-mode-menu
690 LilyPond-mode-map
691 "Menu used in LilyPond mode."
692 (append '("LilyPond")
693 '(("Insert"
694 [ "\\notes..." LilyPond-insert-tag-notes t]
695 [ "\\score..." LilyPond-insert-tag-score t]
696 ["Quick Notes" LilyPond-quick-note-insert t]
698 '(("Miscellaneous"
699 ["Uncomment Region" LilyPond-un-comment-region t]
700 ["Comment Region" comment-region t]
701 ["Refontify buffer" font-lock-fontify-buffer t]
705 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z_][a-zA-Z0-9_]*\\) *="
706 "Regexp matching Identifier definitions.")
708 (defvar LilyPond-imenu-generic-expression
709 (list (list nil LilyPond-imenu-generic-re 1))
710 "Expression for imenu")
712 (defun LilyPond-command-select-master ()
713 (interactive)
714 (message "Next command will be on the master file")
715 (setq LilyPond-command-current 'LilyPond-command-master))
717 (defun LilyPond-command-select-buffer ()
718 (interactive)
719 (message "Next command will be on the buffer")
720 (setq LilyPond-command-current 'LilyPond-command-buffer))
722 (defun LilyPond-command-select-region ()
723 (interactive)
724 (message "Next command will be on the region")
725 (setq LilyPond-command-current 'LilyPond-command-region))
727 (defun LilyPond-command-menu (name)
728 ;; Execute LilyPond-command-alist NAME from a menu.
729 (let ((LilyPond-command-force name))
730 (funcall LilyPond-command-current)))
732 (defun LilyPond-mode ()
733 "Major mode for editing LilyPond music files.
735 This mode knows about LilyPond keywords and line comments, not about
736 indentation or block comments. It features easy compilation, error
737 finding and viewing of a LilyPond source buffer or region.
739 COMMANDS
740 \\{LilyPond-mode-map}
741 VARIABLES
743 LilyPond-command-alist\t\talist from name to command
744 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
745 (interactive)
746 ;; set up local variables
747 (kill-all-local-variables)
749 (make-local-variable 'font-lock-defaults)
750 (setq font-lock-defaults '(LilyPond-font-lock-keywords))
752 ;; string and comments are fontified explicitly
753 (make-local-variable 'font-lock-keywords-only)
754 (setq font-lock-keywords-only t)
756 ;; Multi-line font-locking needs Emacs 21.1 or newer.
757 ;; For older versions there is hotkey "C-c f".
758 (make-local-variable 'font-lock-multiline)
759 (setq font-lock-multiline t)
761 (make-local-variable 'paragraph-separate)
762 (setq paragraph-separate "^[ \t]*$")
764 (make-local-variable 'paragraph-start)
765 (setq paragraph-start "^[ \t]*$")
767 (make-local-variable 'comment-start)
768 (setq comment-start "%")
770 (make-local-variable 'comment-start-skip)
771 (setq comment-start-skip "%{? *")
773 (make-local-variable 'comment-end)
774 (setq comment-end "")
776 (make-local-variable 'block-comment-start)
777 (setq block-comment-start "%{")
779 (make-local-variable 'block-comment-end)
780 (setq block-comment-end "%}")
782 (make-local-variable 'indent-line-function)
783 (setq indent-line-function 'LilyPond-indent-line)
785 (set-syntax-table LilyPond-mode-syntax-table)
786 (setq major-mode 'LilyPond-mode)
787 (setq mode-name "LilyPond")
788 (setq local-abbrev-table LilyPond-mode-abbrev-table)
789 (use-local-map LilyPond-mode-map)
791 (make-local-variable 'imenu-generic-expression)
792 (setq imenu-generic-expression LilyPond-imenu-generic-expression)
793 (imenu-add-to-menubar "Index")
795 ;; run the mode hook. LilyPond-mode-hook use is deprecated
796 (run-hooks 'LilyPond-mode-hook))
798 (defun LilyPond-version ()
799 "Echo the current version of `LilyPond-mode' in the minibuffer."
800 (interactive)
801 (message "Using `LilyPond-mode' version %s" LilyPond-version))
803 (provide 'lilypond-mode)
804 ;;; lilypond-mode.el ends here