*** empty log message ***
[lilypond.git] / lilypond-mode.el
blobfdcc28df9f162670465e9132de24b1311e66190d
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 (if (buffer-modified-p) (save-buffer)))
111 ;;; return (dir base ext)
112 (defun split-file-name (name)
113 (let* ((i (string-match "[^/]*$" name))
114 (dir (if (> i 0) (substring name 0 i) "./"))
115 (file (substring name i (length name)))
116 (i (string-match "[^.]*$" file)))
117 (if (and
118 (> i 0)
119 (< i (length file)))
120 (list dir (substring file 0 (- i 1)) (substring file i (length file)))
121 (list dir file ""))))
124 ;; Should check whether in command-alist?
125 (defcustom LilyPond-command-default "LilyPond"
126 "Default command. Must identify a member of LilyPond-command-alist."
128 :group 'LilyPond
129 :type 'string)
130 ;;;(make-variable-buffer-local 'LilyPond-command-last)
132 (defvar LilyPond-command-current 'LilyPond-command-master)
133 ;;;(make-variable-buffer-local 'LilyPond-command-master)
136 ;; If non-nil, LilyPond-command-query will return the value of this
137 ;; variable instead of quering the user.
138 (defvar LilyPond-command-force nil)
140 (defcustom LilyPond-xdvi-command "xdvi"
141 "Command used to display DVI files."
143 :group 'LilyPond
144 :type 'string)
146 (defcustom LilyPond-gv-command "gv -watch"
147 "Command used to display PS files."
149 :group 'LilyPond
150 :type 'string)
152 (defcustom LilyPond-midi-command "timidity"
153 "Command used to play MIDI files."
155 :group 'LilyPond
156 :type 'string)
158 (defcustom LilyPond-midi-all-command "timidity -ig"
159 "Command used to play MIDI files."
161 :group 'LilyPond
162 :type 'string)
164 ;; This is the major configuration variable.
165 (defcustom LilyPond-command-alist
167 ("LilyPond" . ("lilypond %s" . "TeX"))
168 ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
170 ("2Dvi" . ("ly2dvi %s" . "View"))
171 ("2PS" . ("ly2dvi -P %s" . "View"))
172 ("2Midi" . ("lilypond -m %s" . "View"))
174 ("Book" . ("lilypond-book %x" . "LaTeX"))
175 ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
177 ;; point-n-click (arg: exits upop USR1)
178 ("SmartView" . ("xdvi %d" . "LilyPond"))
180 ;; refreshes when kicked USR1
181 ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
183 ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
185 ("Midi" . (,(concat LilyPond-midi-command " %m") . "LilyPond"))
188 "AList of commands to execute on the current document.
190 The key is the name of the command as it will be presented to the
191 user, the value is a cons of the command string handed to the shell
192 after being expanded, and the next command to be executed upon
193 success. The expansion is done using the information found in
194 LilyPond-expand-list.
196 :group 'LilyPond
197 :type '(repeat (cons :tag "Command Item"
198 (string :tag "Key")
199 (cons :tag "How"
200 (string :tag "Command")
201 (string :tag "Next Key")))))
203 ;; drop this?
204 (defcustom LilyPond-file-extensions '(".ly" ".sly" ".fly")
205 "*File extensions used by manually generated TeX files."
206 :group 'LilyPond
207 :type '(repeat (string :format "%v")))
210 (defcustom LilyPond-expand-alist
212 ("%s" . ".ly")
213 ("%t" . ".tex")
214 ("%d" . ".dvi")
215 ("%p" . ".ps")
216 ("%l" . ".latex")
217 ("%x" . ".tely")
218 ("%m" . ".midi")
221 "Alist of expansion strings for LilyPond command names."
222 :group 'LilyPond
223 :type '(repeat (cons :tag "Alist item"
224 (string :tag "Symbol")
225 (string :tag "Expansion"))))
228 (defcustom LilyPond-command-Show "View"
229 "*The default command to show (view or print) a LilyPond file.
230 Must be the car of an entry in `LilyPond-command-alist'."
231 :group 'LilyPond
232 :type 'string)
233 (make-variable-buffer-local 'LilyPond-command-Show)
235 (defcustom LilyPond-command-Print "Print"
236 "The name of the Print entry in LilyPond-command-Print."
237 :group 'LilyPond
238 :type 'string)
240 (defun xLilyPond-compile-sentinel (process msg)
241 (if (and process
242 (= 0 (process-exit-status process)))
243 (setq LilyPond-command-default
244 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
246 ;; FIXME: shouldn't do this for stray View/xdvi
247 (defun LilyPond-compile-sentinel (buffer msg)
248 (if (string-match "^finished" msg)
249 (setq LilyPond-command-default
250 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
252 ;;(make-variable-buffer-local 'compilation-finish-function)
253 (setq compilation-finish-function 'LilyPond-compile-sentinel)
255 (defun LilyPond-command-query (name)
256 "Query the user for what LilyPond command to use."
257 (let* ((default (cond ((if (string-equal name "emacs-lily")
258 (LilyPond-check-files (concat name ".tex")
259 (list name)
260 LilyPond-file-extensions)
261 (if (buffer-modified-p)
262 (if (y-or-n-p "Save buffer before next command? ")
263 (progn (LilyPond-save-buffer)
264 (setq LilyPond-command-default "LilyPond"))))
265 ;;"LilyPond"
266 LilyPond-command-default))
267 (t LilyPond-command-default)))
269 (completion-ignore-case t)
271 (answer (or LilyPond-command-force
272 (completing-read
273 (concat "Command: (default " default ") ")
274 LilyPond-command-alist nil t nil 'LilyPond-command-history))))
276 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
277 (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
278 (if (and answer
279 (not (string-equal answer "")))
280 answer
281 default))))
284 ;; FIXME: find ``\score'' in buffers / make settable?
285 (defun LilyPond-master-file ()
286 ;; duh
287 (buffer-file-name))
289 (defun LilyPond-command-master ()
290 "Run command on the current document."
291 (interactive)
292 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
293 'LilyPond-master-file))
295 (defun LilyPond-command-lilypond ()
296 "Run lilypond for the current document."
297 (interactive)
298 (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file)
301 (defun LilyPond-command-formatdvi ()
302 "Format the dvi output of the current document."
303 (interactive)
304 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
307 (defun LilyPond-command-formatps ()
308 "Format the ps output of the current document."
309 (interactive)
310 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
313 (defun LilyPond-command-smartview ()
314 "View the dvi output of current document."
315 (interactive)
316 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
319 (defun LilyPond-command-view ()
320 "View the dvi output of current document."
321 (interactive)
322 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
325 (defun LilyPond-command-viewps ()
326 "View the ps output of current document."
327 (interactive)
328 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
331 (defun LilyPond-command-midi ()
332 "Play midi corresponding to the current document."
333 (interactive)
334 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file)
337 (defun count-rexp (start end rexp)
338 "Print number of found regular expressions in the region."
339 (interactive "r")
340 (save-excursion
341 (save-restriction
342 (narrow-to-region start end)
343 (goto-char (point-min))
344 (count-matches rexp))))
346 (defun count-midi-words ()
347 "Print number of scores before the curser."
348 (interactive)
349 (count-rexp (point-min) (point-max) "\\\\midi"))
351 (defun count-midi-words-backwards ()
352 "Print number of scores before the curser."
353 (interactive)
354 (count-rexp (point-min) (point) "\\\\midi"))
356 (defun LilyPond-command-next-midi ()
357 "Play next midi score of the current document."
358 (interactive)
359 (if (Midi-running)
360 (quit-process (get-process "midi") t)
361 (LilyPond-compile-file
362 (let ((fname (LilyPond-master-file))
363 (allcount (string-to-number (substring (count-midi-words) 0 -12)))
364 (count (string-to-number (substring (count-midi-words-backwards) 0 -12))))
365 (concat LilyPond-midi-command " "
366 (substring fname 0 -3) ; suppose ".ly"
367 (if (and (> allcount 1) (> count 0)) ; not first score
368 (if (eq count allcount) ; last score
369 (concat "-" (number-to-string (+ count -1)))
370 (concat "-" (number-to-string count))))
371 ".midi"))
372 "Midi")))
374 (defun LilyPond-command-all-midi ()
375 "Play next midi score of the current document."
376 (interactive)
377 (if (Midi-running)
378 (quit-process (get-process "midi") t)
379 (LilyPond-compile-file
380 (let ((fname (LilyPond-master-file))
381 (allcount (string-to-number (substring (count-midi-words) 0 -12))))
382 (concat LilyPond-midi-all-command " "
383 (if (> allcount 0) ; at least one midi-score
384 (concat (substring fname 0 -3) ".midi "))
385 (if (> allcount 1) ; more than one midi-score
386 (concat (substring fname 0 -3) "-?.midi "))
387 (if (> allcount 9) ; etc.
388 (concat (substring fname 0 -3) "-??.midi"))
389 (if (> allcount 99) ; not first score
390 (concat (substring fname 0 -3) "-???.midi"))))
391 "Midi")))
393 (defun LilyPond-un-comment-region (start end level)
394 "Remove up to LEVEL comment characters from each line in the region."
395 (interactive "*r\np")
396 (comment-region start end (- level)))
398 ;; FIXME, this is broken
399 (defun LilyPond-region-file (begin end)
400 (let (
401 ;; (dir "/tmp/")
402 ;; urg
403 (dir "./")
404 (base "emacs-lily")
405 ;; Hmm
406 (ext (if (string-match "^[\\]score" (buffer-substring begin end))
407 ".ly"
408 (if (< 50 (abs (- begin end)))
409 ".fly"
410 ".sly"))))
411 (concat dir base ext)))
413 (defun LilyPond-command-region (begin end)
414 "Run LilyPond on the current region."
415 (interactive "r")
416 (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
417 (LilyPond-command (LilyPond-command-query
418 (LilyPond-region-file begin end))
419 '(lambda () (LilyPond-region-file begin end))))
421 (defun LilyPond-command-buffer ()
422 "Run LilyPond on buffer."
423 (interactive)
424 (LilyPond-command-region (point-min) (point-max)))
426 (defun LilyPond-command-expand (string file)
427 (let ((case-fold-search nil))
428 (if (string-match "%" string)
429 (let* ((b (match-beginning 0))
430 (e (+ b 2))
431 (l (split-file-name file))
432 (dir (car l))
433 (base (cadr l)))
434 (LilyPond-command-expand
435 (concat (substring string 0 b)
437 base
438 (let ((entry (assoc (substring string b e)
439 LilyPond-expand-alist)))
440 (if entry (cdr entry) ""))
441 (substring string e))
442 file))
443 string)))
445 (defun LilyPond-shell-process (name buffer command)
446 (let ((old (current-buffer)))
447 (switch-to-buffer-other-window buffer)
448 ;; If we empty the buffer don't see messages scroll by.
449 ;; (erase-buffer)
451 (start-process-shell-command name buffer command)
452 (switch-to-buffer-other-window old)))
455 (defun LilyPond-command (name file)
456 "Run command NAME on the file you get by calling FILE.
458 FILE is a function return a file name. It has one optional argument,
459 the extension to use on the file.
461 Use the information in LilyPond-command-alist to determine how to run the
462 command."
464 (let ((entry (assoc name LilyPond-command-alist)))
465 (if entry
466 (let ((command (LilyPond-command-expand (cadr entry)
467 (apply file nil))))
468 (if (string-equal name "View")
469 (let ((buffer-xdvi (get-buffer-create "*view*")))
470 (if LilyPond-kick-xdvi
471 (let ((process-xdvi (get-buffer-process buffer-xdvi)))
472 (if process-xdvi
473 (signal-process (process-id process-xdvi) 'SIGUSR1)
474 (LilyPond-shell-process name buffer-xdvi command)))
475 (LilyPond-shell-process name buffer-xdvi command)))
476 (progn
477 (setq LilyPond-command-default name)
478 (LilyPond-compile-file command name)))))))
480 ;; XEmacs stuff
481 ;; Sadly we need this for a macro in Emacs 19.
482 (eval-when-compile
483 ;; Imenu isn't used in XEmacs, so just ignore load errors.
484 (condition-case ()
485 (require 'imenu)
486 (error nil)))
489 ;;; Keymap
491 (defvar LilyPond-mode-map ()
492 "Keymap used in `LilyPond-mode' buffers.")
494 ;; Note: if you make changes to the map, you must do
495 ;; M-x set-variable LilyPond-mode-map nil
496 ;; M-x eval-buffer
497 ;; M-x LilyPond-mode
498 ;; to let the changest take effect
500 (if LilyPond-mode-map
502 (setq LilyPond-mode-map (make-sparse-keymap))
503 (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
504 (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
505 (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
506 (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-job)
507 (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
508 (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
509 (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
510 (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
511 (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
512 (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
513 (define-key LilyPond-mode-map "\C-c\C-m" 'LilyPond-command-next-midi)
514 (define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
515 (define-key LilyPond-mode-map "\C-ci" 'LilyPond-quick-note-insert)
516 (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
517 (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
518 (define-key LilyPond-mode-map "\C-c:" 'LilyPond-un-comment-region)
519 (define-key LilyPond-mode-map "\C-c;" 'comment-region)
520 (define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren)
521 (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren)
522 (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren)
525 ;;; Menu Support
527 (defun LilyPond-quick-note-insert()
528 "Insert notes with fewer key strokes by using a simple keyboard piano."
529 (interactive)
530 (setq dutch-notes
531 '(("k" "a") ("l" "b") ("a" "c") ("s" "d")
532 ("d" "e") ("f" "f") ("j" "g") ("r" "r")))
533 (setq dutch-note-ends '("eses" "es" "" "is" "isis"))
534 (setq dutch-note-replacements '("" ""))
535 (setq finnish-note-replacements
536 '(("eeses" "eses") ("ees" "es") ("aeses" "asas") ("aes" "as") ("b" "h")
537 ("beses" "heses") ("bes" "b") ("bis" "his") ("bisis" "hisis")))
538 ; add more translations of the note names
539 (setq other-keys "()<>~}")
540 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)
542 (message "Press h for help.") (sit-for 0 750 1)
544 (setq note-replacements dutch-note-replacements)
545 (while (not (= 27 ; esc to quit
546 (setq x (read-char-exclusive
547 (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"
548 (if (string= (car(cdr(assoc "b" note-replacements))) "h")
549 "h" "b")
550 (nth (+ accid 2) dutch-note-ends)
551 (make-string (abs octav) (if (> octav 0) ?' ?,))
552 durat
553 (if (string= durat "") "" (make-string dots ?.)))))))
554 ; (insert (number-to-string x)) ; test numbers for characters
555 (setq note (cdr (assoc (char-to-string x) dutch-notes)))
556 (cond
557 ((= x 127) (backward-kill-word 1)) ; backspace
558 ((= x 13) (progn (insert "\n") (LilyPond-indent-line)))) ; return
559 (setq x (char-to-string x))
560 (cond
561 ((and (string< x "9") (string< "0" x))
562 (progn (setq durat (int-to-string (expt 2 (- (string-to-int x) 1))))
563 (setq dots 0)))
564 ((string= x " ") (insert " "))
565 ((string= x "/") (progn (insert "\\times ")
566 (while (not (and (string< x "9") (string< "0" x)))
567 (setq x (char-to-string (read-char-exclusive "Insert a number for the denominator (\"x/\")"))))
568 (insert (format "%s/" x)) (setq x "/")
569 (while (not (and (string< x "9") (string< "0" x)))
570 (setq x (char-to-string (read-char-exclusive "Insert a number for the numerator (\"/y\")"))))
571 (insert (format "%s { " x))))
572 ((string= x "0") (progn (setq accid 0) (setq octav 0)
573 (setq durat "") (setq dots 0)))
574 ((string= x "i") (setq accid (if (= accid 2) 0 (max (+ accid 1) 1))))
575 ((string= x "e") (setq accid (if (= accid -2) 0 (min (+ accid -1) -1))))
576 ((string= x "'") (setq octav (if (= octav 4) 0 (max (+ octav 1) 1))))
577 ((string= x ",") (setq octav (if (= octav -4) 0 (min (+ octav -1) -1))))
578 ((string= x ".") (setq dots (if (= dots 4) 0 (+ dots 1))))
579 ((not (null (member x (split-string other-keys ""))))
580 (insert (format "%s " x)))
581 ((not (null note))
582 (progn
583 (setq note
584 (format "%s%s" (car note) (if (string= "r" (car note)) ""
585 (nth (+ accid 2) dutch-note-ends))))
586 (setq notetwo (car (cdr (assoc note note-replacements))))
587 (if (not (null notetwo)) (setq note notetwo))
588 (insert
589 (format "%s%s%s%s "
590 note
591 (if (string= "r" note) ""
592 (make-string (abs octav) (if (> octav 0) ?' ?,)))
593 durat
594 (if (string= durat "") "" (make-string dots ?.))))
595 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)))
596 ((string= x "t") (progn (setq note-replacements dutch-note-replacements)
597 (message "Selected Dutch notes")
598 (sit-for 0 750 1))) ; t
599 ((string= x "n") (progn (setq note-replacements finnish-note-replacements)
600 (message "Selected Finnish/Deutsch notes")
601 (sit-for 0 750 1))) ; n
602 ; add more translations of the note names
603 ((string= x "h")
604 (progn (message "Insert notes with fewer key strokes. For example \"i,5.f\" produces \"fis,32. \".") (sit-for 5 0 1)
605 (message "Add also \"a ~ a\"-ties, \"a ( ) b\"-slurs and \"< a b >\"-chords.") (sit-for 5 0 1)
606 (message "Note names are in Du(t)ch by default. Hit 'n' for Fin(n)ish/Deutsch note names.") (sit-for 5 0 1)
607 (message "Backspace deletes last note, return starts a new indented line and Esc quits.") (sit-for 5 0 1)
608 (message "Insert note triplets \"\\times 2/3 { a b } \" by typing \"/23ab}\".") (sit-for 5 0 1)
609 (message "Remember to add all other details as well.") (sit-for 5 0 1)))
612 (define-skeleton LilyPond-insert-tag-notes
613 "LilyPond notes tag."
615 ; (if (bolp) nil ?\n)
616 "\\notes"
617 (if (y-or-n-p "Set \"\\relative\" attribute? ")
618 (concat " \\relative " (skeleton-read "Relative: " "" str)))
619 " { " _ " }")
621 (define-skeleton LilyPond-insert-tag-score
622 "LilyPond score tag."
624 (if (bolp) nil ?\n)
625 "\\score {\n"
626 " " _ "\n"
627 " \\paper { }\n"
628 (if (y-or-n-p "Insert \"\\header\" field? ")
629 (concat " \\header {\n "
630 (skeleton-read "Piece: " "piece = " str) "\n"
631 (if (y-or-n-p "Insert \"opus\" field? ")
632 (concat " " (skeleton-read "Opus: " "opus = " str) "\n"))
633 " }\n"))
634 (if (y-or-n-p "Insert \"\\midi\" field? ")
635 (concat " \\midi { "
636 (skeleton-read "Midi: " "\\tempo 4 = " str)
637 " }\n"))
638 "}\n")
640 (defun LilyPond-command-menu-entry (entry)
641 ;; Return LilyPond-command-alist ENTRY as a menu item.
642 (let ((name (car entry)))
643 (cond ((and (string-equal name LilyPond-command-Print)
644 LilyPond-printer-list)
645 (let ((command LilyPond-print-command)
646 (lookup 1))
647 (append (list LilyPond-command-Print)
648 (mapcar 'LilyPond-command-menu-printer-entry
649 LilyPond-printer-list))))
651 (vector name (list 'LilyPond-command-menu name) t)))))
654 (easy-menu-define LilyPond-command-menu
655 LilyPond-mode-map
656 "Menu used in LilyPond mode."
657 (append '("Command")
658 '(("Command on"
659 [ "Master File" LilyPond-command-select-master
660 :keys "C-c C-c" :style radio
661 :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
662 [ "Buffer" LilyPond-command-select-buffer
663 :keys "C-c C-b" :style radio
664 :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
665 [ "Region" LilyPond-command-select-region
666 :keys "C-c C-r" :style radio
667 :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
668 ; (let ((file 'LilyPond-command-on-current))
669 ; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
670 ;;; Some kind of mapping which includes :keys might be more elegant
671 '([ "LilyPond" (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file) :keys "C-c C-l"])
672 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
673 '([ "2Dvi" (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file) :keys "C-c C-d"])
674 '([ "2PS" (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file) :keys "C-c C-f"])
675 '([ "2Midi" (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-master-file)])
676 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
677 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
678 '([ "SmartView" (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file) :keys "C-c C-s"])
679 '([ "View" (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file) :keys "C-c C-v"])
680 '([ "ViewPS" (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file) :keys "C-c C-p"])
681 '([ "Midi (off)" (LilyPond-command-next-midi) :keys "C-c C-m"])
682 '([ "Midi all" (LilyPond-command-all-midi)])
685 (easy-menu-define LilyPond-mode-menu
686 LilyPond-mode-map
687 "Menu used in LilyPond mode."
688 (append '("LilyPond")
689 '(("Insert"
690 [ "\\notes..." LilyPond-insert-tag-notes t]
691 [ "\\score..." LilyPond-insert-tag-score t]
692 ["Quick Notes" LilyPond-quick-note-insert t]
694 '(("Miscellaneous"
695 ["Uncomment Region" LilyPond-un-comment-region t]
696 ["Comment Region" comment-region t]
697 ["Refontify buffer" font-lock-fontify-buffer t]
701 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z_][a-zA-Z0-9_]*\\) *="
702 "Regexp matching Identifier definitions.")
704 (defvar LilyPond-imenu-generic-expression
705 (list (list nil LilyPond-imenu-generic-re 1))
706 "Expression for imenu")
708 (defun LilyPond-command-select-master ()
709 (interactive)
710 (message "Next command will be on the master file")
711 (setq LilyPond-command-current 'LilyPond-command-master))
713 (defun LilyPond-command-select-buffer ()
714 (interactive)
715 (message "Next command will be on the buffer")
716 (setq LilyPond-command-current 'LilyPond-command-buffer))
718 (defun LilyPond-command-select-region ()
719 (interactive)
720 (message "Next command will be on the region")
721 (setq LilyPond-command-current 'LilyPond-command-region))
723 (defun LilyPond-command-menu (name)
724 ;; Execute LilyPond-command-alist NAME from a menu.
725 (let ((LilyPond-command-force name))
726 (funcall LilyPond-command-current)))
728 (defun LilyPond-mode ()
729 "Major mode for editing LilyPond music files.
731 This mode knows about LilyPond keywords and line comments, not about
732 indentation or block comments. It features easy compilation, error
733 finding and viewing of a LilyPond source buffer or region.
735 COMMANDS
736 \\{LilyPond-mode-map}
737 VARIABLES
739 LilyPond-command-alist\t\talist from name to command
740 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
741 (interactive)
742 ;; set up local variables
743 (kill-all-local-variables)
745 (make-local-variable 'font-lock-defaults)
746 (setq font-lock-defaults '(LilyPond-font-lock-keywords))
748 ;; string and comments are fontified explicitly
749 (make-local-variable 'font-lock-keywords-only)
750 (setq font-lock-keywords-only t)
752 ;; Multi-line font-locking needs Emacs 21.1 or newer.
753 ;; For older versions there is hotkey "C-c f".
754 (make-local-variable 'font-lock-multiline)
755 (setq font-lock-multiline t)
757 (make-local-variable 'paragraph-separate)
758 (setq paragraph-separate "^[ \t]*$")
760 (make-local-variable 'paragraph-start)
761 (setq paragraph-start "^[ \t]*$")
763 (make-local-variable 'comment-start)
764 (setq comment-start "%")
766 (make-local-variable 'comment-start-skip)
767 (setq comment-start-skip "%{? *")
769 (make-local-variable 'comment-end)
770 (setq comment-end "")
772 (make-local-variable 'block-comment-start)
773 (setq block-comment-start "%{")
775 (make-local-variable 'block-comment-end)
776 (setq block-comment-end "%}")
778 (make-local-variable 'indent-line-function)
779 (setq indent-line-function 'LilyPond-indent-line)
781 (set-syntax-table LilyPond-mode-syntax-table)
782 (setq major-mode 'LilyPond-mode)
783 (setq mode-name "LilyPond")
784 (setq local-abbrev-table LilyPond-mode-abbrev-table)
785 (use-local-map LilyPond-mode-map)
787 (make-local-variable 'imenu-generic-expression)
788 (setq imenu-generic-expression LilyPond-imenu-generic-expression)
789 (imenu-add-to-menubar "Index")
791 ;; run the mode hook. LilyPond-mode-hook use is deprecated
792 (run-hooks 'LilyPond-mode-hook))
794 (defun LilyPond-version ()
795 "Echo the current version of `LilyPond-mode' in the minibuffer."
796 (interactive)
797 (message "Using `LilyPond-mode' version %s" LilyPond-version))
799 (provide 'lilypond-mode)
800 ;;; lilypond-mode.el ends here