*** empty log message ***
[lilypond.git] / lilypond-mode.el
bloba5706faaddc514fad1265b2d2aab29ffe82240e5
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)
13 ;;;
14 ;;; Changed 2002 Carlos Betancourt <carlos.betancourt@chello.be>
15 ;;; * Added spanish-note-replacements
17 ;;; Inspired on auctex
19 ;;; Look lilypond-init.el or Documentation/topdocs/INSTALL.texi
20 ;;; for installing instructions.
22 ;;; TODO:
23 ;;; * XEmacs, broken ?
24 ;;; * parenthesis matching
26 (require 'easymenu)
27 (require 'compile)
29 (defconst LilyPond-version "1.7.12"
30 "`LilyPond-mode' version number.")
32 (defconst LilyPond-help-address "bug-lilypond@gnu.org"
33 "Address accepting submission of bug reports.")
35 (defvar LilyPond-mode-hook nil
36 "*Hook called by `LilyPond-mode'.")
38 ;; FIXME: find ``\score'' in buffers / make settable?
39 (defun LilyPond-master-file ()
40 ;; duh
41 (buffer-file-name))
43 (defvar LilyPond-kick-xdvi nil
44 "If true, no simultaneous xdvi's are started, but reload signal is sent.")
46 (defvar LilyPond-command-history nil
47 "Command history list.")
49 (defvar LilyPond-regexp-alist
50 '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
51 "Regexp used to match LilyPond errors. See `compilation-error-regexp-alist'.")
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 from load-path."
61 (let ((fn nil)
62 (lp load-path)
63 (words-file "lilypond.words"))
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 "Error (shown for 12 seconds; press a key to skip):\n\n File `lilypond.words' containing keywords to be autocompleted and fontified,\n was not found. Place it to your `load-path' (see `lilypond-init.el').\n")
70 (sit-for 12 0 1)))
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 (defconst LilyPond-keywords
92 (let ((wordlist ())
93 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
94 (progn
95 (while (> (length co) 0)
96 (if (> (length (car co)) 1)
97 (if (and (string-equal "\\" (substring (car co) 0 1))
98 (string-equal (downcase (car co)) (car co)))
99 (add-to-list 'wordlist (car co))))
100 (setq co (cdr co)))
101 (if (eq (length wordlist) 0)
102 (setq wordlist '("\\score"))) ; add \keywords to lilypond.words
103 wordlist))
104 "LilyPond \\keywords")
106 (defconst LilyPond-identifiers
107 (let ((wordlist ())
108 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
109 (progn
110 (while (> (length co) 0)
111 (if (> (length (car co)) 1)
112 (if (and (string-equal "\\" (substring (car co) 0 1))
113 (not (string-equal (downcase (car co)) (car co))))
114 (add-to-list 'wordlist (car co))))
115 (setq co (cdr co)))
116 (if (eq (length wordlist) 0)
117 (setq wordlist '("\\voiceOne"))) ; add \Identifiers to lilypond.words
118 wordlist))
119 "LilyPond \\Identifiers")
121 (defconst LilyPond-reserved-words
122 (let ((wordlist ())
123 (co (all-completions "" (LilyPond-add-dictionary-word ()))))
124 (progn
125 (while (> (length co) 0)
126 (if (> (length (car co)) 0)
127 (if (not (string-equal "\\" (substring (car co) 0 1)))
128 (add-to-list 'wordlist (car co))))
129 (setq co (cdr co)))
130 (if (eq (length wordlist) 0)
131 (setq wordlist '("Staff"))) ; add ReservedWords to lilypond.words
132 wordlist))
133 "LilyPond ReservedWords")
135 (defun LilyPond-check-files (derived originals extensions)
136 "Check that DERIVED is newer than any of the ORIGINALS.
137 Try each original with each member of EXTENSIONS, in all directories
138 in LilyPond-include-path."
139 (let ((found nil)
140 (regexp (concat "\\`\\("
141 (mapconcat (function (lambda (dir)
142 (regexp-quote (expand-file-name dir))))
143 LilyPond-include-path "\\|")
144 "\\).*\\("
145 (mapconcat 'regexp-quote originals "\\|")
146 "\\)\\.\\("
147 (mapconcat 'regexp-quote extensions "\\|")
148 "\\)\\'"))
149 (buffers (buffer-list)))
150 (while buffers
151 (let* ((buffer (car buffers))
152 (name (buffer-file-name buffer)))
153 (setq buffers (cdr buffers))
154 (if (and name (string-match regexp name))
155 (progn
156 (and (buffer-modified-p buffer)
157 (or (not LilyPond-save-query)
158 (y-or-n-p (concat "Save file "
159 (buffer-file-name buffer)
160 "? ")))
161 (save-excursion (set-buffer buffer) (save-buffer)))
162 (if (file-newer-than-file-p name derived)
163 (setq found t))))))
164 found))
166 (defun LilyPond-running ()
167 "Check the currently running LilyPond compiling jobs."
168 (interactive)
169 (let ((process-names (list "lilypond" "tex" "2dvi" "2ps" "2midi"
170 "book" "latex"))
171 (running nil))
172 (while (setq process-name (pop process-names))
173 (setq process (get-process process-name))
174 (if (and process
175 (eq (process-status process) 'run))
176 (push process-name running)))
177 running)) ; return the running jobs
179 (defun LilyPond-midi-running ()
180 "Check the currently running Midi processes."
181 (interactive)
182 (let ((process-names (list "midi" "midiall"))
183 (running nil))
184 (while (setq process-name (pop process-names))
185 (setq process (get-process process-name))
186 (if (and process
187 (eq (process-status process) 'run))
188 (push process-name running)))
189 running)) ; return the running jobs
191 (defun LilyPond-kill-jobs ()
192 "Kill the currently running LilyPond compiling jobs."
193 (interactive)
194 (let ((process-names (LilyPond-running))
195 (killed nil))
196 (while (setq process-name (pop process-names))
197 (quit-process (get-process process-name) t)
198 (push process-name killed))
199 killed)) ; return the killed jobs
201 (defun LilyPond-kill-midi ()
202 "Kill the currently running midi processes."
203 (let ((process-names (LilyPond-midi-running))
204 (killed nil))
205 (while (setq process-name (pop process-names))
206 (quit-process (get-process process-name) t)
207 (push process-name killed))
208 killed)) ; return the killed jobs
210 ;; URG, should only run LilyPond-compile for LilyPond
211 ;; not for tex,xdvi (ly2dvi?)
212 (defun LilyPond-compile-file (command name)
213 ;; We maybe should know what we run here (Lily, ly2dvi, tex)
214 ;; and adjust our error-matching regex ?
215 (compile-internal command "No more errors" name ))
217 ;; do we still need this, now that we're using compile-internal?
218 (defun LilyPond-save-buffer ()
219 "Save buffer and set default command for compiling."
220 (interactive)
221 (if (buffer-modified-p)
222 (progn (save-buffer)
223 (setq LilyPond-command-default "LilyPond"))))
225 ;;; return (dir base ext)
226 (defun split-file-name (name)
227 (let* ((i (string-match "[^/]*$" name))
228 (dir (if (> i 0) (substring name 0 i) "./"))
229 (file (substring name i (length name)))
230 (i (string-match "[^.]*$" file)))
231 (if (and
232 (> i 0)
233 (< i (length file)))
234 (list dir (substring file 0 (- i 1)) (substring file i (length file)))
235 (list dir file ""))))
238 ;; Should check whether in command-alist?
239 (defcustom LilyPond-command-default "LilyPond"
240 "Default command. Must identify a member of LilyPond-command-alist."
242 :group 'LilyPond
243 :type 'string)
244 ;;;(make-variable-buffer-local 'LilyPond-command-last)
246 (defvar LilyPond-command-current 'LilyPond-command-master)
247 ;;;(make-variable-buffer-local 'LilyPond-command-master)
250 ;; If non-nil, LilyPond-command-query will return the value of this
251 ;; variable instead of quering the user.
252 (defvar LilyPond-command-force nil)
254 (defcustom LilyPond-xdvi-command "xdvi"
255 "Command used to display DVI files."
257 :group 'LilyPond
258 :type 'string)
260 (defcustom LilyPond-gv-command "gv -watch"
261 "Command used to display PS files."
263 :group 'LilyPond
264 :type 'string)
266 (defcustom LilyPond-midi-command "timidity"
267 "Command used to play MIDI files."
269 :group 'LilyPond
270 :type 'string)
272 (defcustom LilyPond-all-midi-command "timidity -ia"
273 "Command used to play MIDI files."
275 :group 'LilyPond
276 :type 'string)
278 (defun LilyPond-command-current-midi ()
279 "Play midi corresponding to the current document."
280 (interactive)
281 (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file))
283 (defun LilyPond-command-all-midi ()
284 "Play midi corresponding to the current document."
285 (interactive)
286 (LilyPond-command (LilyPond-command-menu "MidiAll") 'LilyPond-master-file))
288 (defun count-rexp (start end rexp)
289 "Print number of found regular expressions in the region."
290 (interactive "r")
291 (save-excursion
292 (save-restriction
293 (narrow-to-region start end)
294 (goto-char (point-min))
295 (count-matches rexp))))
297 (defun count-midi-words ()
298 "Check number of midi-scores before the curser."
299 (interactive)
300 (count-rexp (point-min) (point-max) "\\\\midi"))
302 (defun count-midi-words-backwards ()
303 "Check number of midi-scores before the curser."
304 (interactive)
305 (count-rexp (point-min) (point) "\\\\midi"))
307 (defun LilyPond-string-current-midi ()
308 "Check the midi file of the following midi-score in the current document."
309 (interactive)
310 (let ((fnameprefix (substring (LilyPond-master-file) 0 -3)) ; suppose ".ly"
311 (allcount (string-to-number (substring (count-midi-words) 0 -12)))
312 (count (string-to-number (substring (count-midi-words-backwards) 0 -12))))
313 (concat fnameprefix
314 (if (and (> allcount 1) (> count 0)) ; not first score
315 (if (eq count allcount) ; last score
316 (concat "-" (number-to-string (+ count -1)))
317 (concat "-" (number-to-string count))))
318 ".midi")))
320 (defun LilyPond-string-all-midi ()
321 "Return the midi files of the current document in ascending order."
322 (interactive)
323 (let ((fnameprefix (substring (LilyPond-master-file) 0 -3))
324 (allcount (string-to-number (substring (count-midi-words) 0 -12))))
325 (concat (if (> allcount 0) ; at least one midi-score
326 (concat fnameprefix ".midi "))
327 (if (> allcount 1) ; more than one midi-score
328 (concat fnameprefix "-[1-9].midi "))
329 (if (> allcount 9) ; etc.
330 (concat fnameprefix "-[1-9][0-9].midi"))
331 (if (> allcount 99) ; not first score
332 (concat fnameprefix "-[1-9][0-9][0-9].midi")))))
334 ;; This is the major configuration variable.
335 (defcustom LilyPond-command-alist
336 ;; Should expand this to include possible keyboard shortcuts which
337 ;; could then be mapped to define-key and menu.
339 ("LilyPond" . ("lilypond %s" . "LaTeX"))
340 ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
342 ("2Dvi" . ("ly2dvi %s" . "View"))
343 ("2PS" . ("ly2dvi -P %s" . "ViewPS"))
344 ("2Midi" . ("lilypond -m %s" . "View"))
346 ("Book" . ("lilypond-book %x" . "LaTeX"))
347 ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
349 ;; point-n-click (arg: exits upop USR1)
350 ("SmartView" . ("xdvi %d" . "LilyPond"))
352 ;; refreshes when kicked USR1
353 ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
354 ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
356 ;; The following are refreshed in LilyPond-command:
357 ;; - current-midi depends on cursor position and
358 ("Midi" . (,(concat LilyPond-midi-command " " (LilyPond-string-current-midi)) . "LilyPond" )) ;
359 ;; - all-midi depends on number of midi-score.
360 ("MidiAll" . (,(concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)) . "LilyPond"))
363 "AList of commands to execute on the current document.
365 The key is the name of the command as it will be presented to the
366 user, the value is a cons of the command string handed to the shell
367 after being expanded, and the next command to be executed upon
368 success. The expansion is done using the information found in
369 LilyPond-expand-list.
371 :group 'LilyPond
372 :type '(repeat (cons :tag "Command Item"
373 (string :tag "Key")
374 (cons :tag "How"
375 (string :tag "Command")
376 (string :tag "Next Key")))))
378 ;; drop this?
379 (defcustom LilyPond-file-extensions '(".ly" ".sly" ".fly")
380 "*File extensions used by manually generated TeX files."
381 :group 'LilyPond
382 :type '(repeat (string :format "%v")))
385 (defcustom LilyPond-expand-alist
387 ("%s" . ".ly")
388 ("%t" . ".tex")
389 ("%d" . ".dvi")
390 ("%p" . ".ps")
391 ("%l" . ".tex")
392 ("%x" . ".tely")
393 ("%m" . ".midi")
396 "Alist of expansion strings for LilyPond command names."
397 :group 'LilyPond
398 :type '(repeat (cons :tag "Alist item"
399 (string :tag "Symbol")
400 (string :tag "Expansion"))))
403 (defcustom LilyPond-command-Show "View"
404 "*The default command to show (view or print) a LilyPond file.
405 Must be the car of an entry in `LilyPond-command-alist'."
406 :group 'LilyPond
407 :type 'string)
408 (make-variable-buffer-local 'LilyPond-command-Show)
410 (defcustom LilyPond-command-Print "Print"
411 "The name of the Print entry in LilyPond-command-Print."
412 :group 'LilyPond
413 :type 'string)
415 (defun xLilyPond-compile-sentinel (process msg)
416 (if (and process
417 (= 0 (process-exit-status process)))
418 (setq LilyPond-command-default
419 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
421 ;; FIXME: shouldn't do this for stray View/xdvi
422 (defun LilyPond-compile-sentinel (buffer msg)
423 (if (string-match "^finished" msg)
424 (setq LilyPond-command-default
425 (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
427 ;;(make-variable-buffer-local 'compilation-finish-function)
428 (setq compilation-finish-function 'LilyPond-compile-sentinel)
430 (defun LilyPond-command-query (name)
431 "Query the user for what LilyPond command to use."
432 (let* ((default (cond ((if (string-equal name "emacs-lily")
433 (LilyPond-check-files (concat name ".tex")
434 (list name)
435 LilyPond-file-extensions)
436 (if (verify-visited-file-modtime (current-buffer))
437 (if (buffer-modified-p)
438 (if (y-or-n-p "Save buffer before next command? ")
439 (LilyPond-save-buffer)))
440 (if (y-or-n-p "The command will be invoked to an already saved buffer. Revert it? ")
441 (revert-buffer t t)))
442 ;;"LilyPond"
443 LilyPond-command-default))
444 (t LilyPond-command-default)))
446 (completion-ignore-case t)
448 (answer (or LilyPond-command-force
449 (completing-read
450 (concat "Command: (default " default ") ")
451 LilyPond-command-alist nil t nil 'LilyPond-command-history))))
453 ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
454 (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
455 (if (and answer
456 (not (string-equal answer "")))
457 answer
458 default))))
460 (defun LilyPond-command-master ()
461 "Run command on the current document."
462 (interactive)
463 (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
464 'LilyPond-master-file))
466 (defun LilyPond-command-lilypond ()
467 "Run lilypond for the current document."
468 (interactive)
469 (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file)
472 (defun LilyPond-command-formatdvi ()
473 "Format the dvi output of the current document."
474 (interactive)
475 (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
478 (defun LilyPond-command-formatps ()
479 "Format the ps output of the current document."
480 (interactive)
481 (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
484 (defun LilyPond-command-formatmidi ()
485 "Format the midi output of the current document."
486 (interactive)
487 (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-master-file)
490 (defun LilyPond-command-smartview ()
491 "View the dvi output of current document."
492 (interactive)
493 (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
496 (defun LilyPond-command-view ()
497 "View the dvi output of current document."
498 (interactive)
499 (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
502 (defun LilyPond-command-viewps ()
503 "View the ps output of current document."
504 (interactive)
505 (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
508 ;; FIXME, does not find commend ender
509 (defun LilyPond-un-comment-region (start end level)
510 "Remove up to LEVEL comment characters from each line in the region."
511 (interactive "*r\np")
512 (comment-region start end (- level)))
514 ;; FIXME, this is broken
515 (defun LilyPond-region-file (begin end)
516 (let (
517 ;; (dir "/tmp/")
518 ;; urg
519 (dir "./")
520 (base "emacs-lily")
521 ;; Hmm
522 (ext (if (string-match "^[\\]score" (buffer-substring begin end))
523 ".ly"
524 (if (< 50 (abs (- begin end)))
525 ".fly"
526 ".sly"))))
527 (concat dir base ext)))
529 (defun LilyPond-command-region (begin end)
530 "Run LilyPond on the current region."
531 (interactive "r")
532 (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
533 (LilyPond-command (LilyPond-command-query
534 (LilyPond-region-file begin end))
535 '(lambda () (LilyPond-region-file begin end))))
537 (defun LilyPond-command-buffer ()
538 "Run LilyPond on buffer."
539 (interactive)
540 (LilyPond-command-region (point-min) (point-max)))
542 (defun LilyPond-command-expand (string file)
543 (let ((case-fold-search nil))
544 (if (string-match "%" string)
545 (let* ((b (match-beginning 0))
546 (e (+ b 2))
547 (l (split-file-name file))
548 (dir (car l))
549 (base (cadr l)))
550 (LilyPond-command-expand
551 (concat (substring string 0 b)
553 base
554 (let ((entry (assoc (substring string b e)
555 LilyPond-expand-alist)))
556 (if entry (cdr entry) ""))
557 (substring string e))
558 file))
559 string)))
561 (defun LilyPond-shell-process (name buffer command)
562 (let ((old (current-buffer)))
563 (switch-to-buffer-other-window buffer)
564 ;; If we empty the buffer don't see messages scroll by.
565 ;; (erase-buffer)
567 (start-process-shell-command name buffer command)
568 (switch-to-buffer-other-window old)))
571 (defun LilyPond-command (name file)
572 "Run command NAME on the file you get by calling FILE.
574 FILE is a function return a file name. It has one optional argument,
575 the extension to use on the file.
577 Use the information in LilyPond-command-alist to determine how to run the
578 command."
580 (let ((entry (assoc name LilyPond-command-alist)))
581 (if entry
582 (let ((command (LilyPond-command-expand (cadr entry)
583 (apply file nil)))
584 (jobs nil)
585 (job-string "no jobs"))
586 (if (member name (list "View" "ViewPS"))
587 ;; is USR1 a right signal for viewps?
588 (let ((buffer-xdvi (get-buffer-create (concat "*" name "*"))))
589 (if LilyPond-kick-xdvi
590 (let ((process-xdvi (get-buffer-process buffer-xdvi)))
591 (if process-xdvi
592 (signal-process (process-id process-xdvi) 'SIGUSR1)
593 (LilyPond-shell-process name buffer-xdvi command)))
594 (LilyPond-shell-process name buffer-xdvi command)))
595 (progn
596 (if (string-equal name "Midi")
597 (progn
598 (setq command (concat LilyPond-midi-command " " (LilyPond-string-current-midi)))
599 (if (LilyPond-kill-midi)
600 (setq job-string nil)))) ; either stop or start playing
601 (if (string-equal name "MidiAll")
602 (progn
603 (setq command (concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)))
604 (LilyPond-kill-midi))) ; stop and start playing
605 (if (and (member name (list "Midi" "MidiAll")) job-string)
606 (if (file-newer-than-file-p
607 (LilyPond-master-file)
608 (concat (substring (LilyPond-master-file) 0 -3) ".midi"))
609 (if (y-or-n-p "Midi older than source. Reformat midi?")
610 (progn
611 (LilyPond-command-formatmidi)
612 (while (LilyPond-running)
613 (message "Starts playing midi once it is built.")
614 (sit-for 0 100))))))
615 (if (member name (list "LilyPond" "TeX" "2Midi" "2PS" "2Dvi"
616 "Book" "LaTeX"))
617 (if (setq jobs (LilyPond-running))
618 (progn
619 (setq job-string "Process") ; could also suggest compiling after process has ended
620 (while jobs
621 (setq job-string (concat job-string " \"" (pop jobs) "\"")))
622 (setq job-string (concat job-string " is already running; kill it to proceed "))
623 (if (y-or-n-p job-string)
624 (progn
625 (setq job-string "no jobs")
626 (LilyPond-kill-jobs)
627 (while (LilyPond-running)
628 (sit-for 0 100)))
629 (setq job-string nil)))))
631 (setq LilyPond-command-default name)
632 (if (string-equal job-string "no jobs")
633 (LilyPond-compile-file command name))))))))
635 ;; XEmacs stuff
636 ;; Sadly we need this for a macro in Emacs 19.
637 (eval-when-compile
638 ;; Imenu isn't used in XEmacs, so just ignore load errors.
639 (condition-case ()
640 (require 'imenu)
641 (error nil)))
644 ;;; Keymap
646 (defvar LilyPond-mode-map ()
647 "Keymap used in `LilyPond-mode' buffers.")
649 ;; Note: if you make changes to the map, you must do
650 ;; M-x set-variable LilyPond-mode-map nil
651 ;; M-x eval-buffer
652 ;; M-x LilyPond-mode
653 ;; to let the changest take effect
655 (if LilyPond-mode-map
657 (setq LilyPond-mode-map (make-sparse-keymap))
658 ;; Put keys to Lilypond-command-alist and fetch them from there somehow.
659 (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
660 (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
661 (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
662 (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-jobs)
663 (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
664 (define-key LilyPond-mode-map "\C-cm" 'LilyPond-command-formatmidi)
665 (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
666 (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
667 (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
668 (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
669 (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
670 (define-key LilyPond-mode-map [(control c) return] 'LilyPond-command-current-midi)
671 (define-key LilyPond-mode-map [(control c) (control return)] 'LilyPond-command-all-midi)
672 (define-key LilyPond-mode-map "\C-x\C-s" 'LilyPond-save-buffer)
673 (define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
674 (define-key LilyPond-mode-map "\C-ci" 'LilyPond-quick-note-insert)
675 (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
676 (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
677 (define-key LilyPond-mode-map "\C-c:" 'LilyPond-un-comment-region)
678 (define-key LilyPond-mode-map "\C-c;" 'comment-region)
679 (define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren) ; urgh
680 (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren) ; argh
681 (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren) ; ..rgh
682 (define-key LilyPond-mode-map [S-iso-lefttab] 'LilyPond-autocompletion)
683 (define-key LilyPond-mode-map "\C-c\t" 'LilyPond-info-index-search)
686 ;;; Menu Support
688 (defun LilyPond-quick-note-insert()
689 "Insert notes with fewer key strokes by using a simple keyboard piano."
690 (interactive)
691 (setq dutch-notes
692 '(("k" "a") ("l" "b") ("a" "c") ("s" "d")
693 ("d" "e") ("f" "f") ("j" "g") ("r" "r")))
694 (setq dutch-note-ends '("eses" "es" "" "is" "isis"))
695 (setq dutch-note-replacements '("" ""))
696 (setq finnish-note-replacements
697 '(("eeses" "eses") ("ees" "es") ("aeses" "asas") ("aes" "as") ("b" "h")
698 ("beses" "heses") ("bes" "b") ("bis" "his") ("bisis" "hisis")))
699 ; add more translations of the note names
700 (setq spanish-note-replacements
701 '(("c" "do") ("d" "re") ("e" "mi") ("f" "fa") ("g" "sol") ("a" "la") ("b" "si")
702 ("cis" "dos") ("cisis" "doss") ("ces" "dob") ("ceses" "dobb")
703 ("dis" "res") ("disis" "ress") ("des" "reb") ("deses" "rebb")
704 ("eis" "mis") ("eisis" "miss") ("ees" "mib") ("eeses" "mibb")
705 ("fis" "fas") ("fisis" "fass") ("fes" "fab") ("feses" "fabb")
706 ("gis" "sols") ("gisis" "solss") ("ges" "solb") ("geses" "solbb")
707 ("ais" "las") ("aisis" "lass") ("aes" "lab") ("aeses" "labb")
708 ("bis" "sis") ("bisis" "siss") ("bes" "sib") ("beses" "sibb")))
709 (setq other-keys "()<>~}")
710 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)
712 (message "Press h for help.") (sit-for 0 750 1)
714 (setq note-replacements dutch-note-replacements)
715 (while (not (= 27 ; esc to quit
716 (setq x (read-char-exclusive
717 (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"
718 (if (string= (car(cdr(assoc "b" note-replacements))) "h")
719 "h" "b")
720 (nth (+ accid 2) dutch-note-ends)
721 (make-string (abs octav) (if (> octav 0) ?' ?,))
722 durat
723 (if (string= durat "") "" (make-string dots ?.)))))))
724 ;; (insert (number-to-string x)) ; test numbers for characters
725 (setq note (cdr (assoc (char-to-string x) dutch-notes)))
726 (cond
727 ((= x 127) (backward-kill-word 1)) ; backspace
728 ((= x 13) (progn (insert "\n") (LilyPond-indent-line)))) ; return
729 (setq x (char-to-string x))
730 (cond
731 ((and (string< x "9") (string< "0" x))
732 (progn (setq durat (int-to-string (expt 2 (- (string-to-int x) 1))))
733 (setq dots 0)))
734 ((string= x " ") (insert " "))
735 ((string= x "/") (progn (insert "\\times ")
736 (while (not (and (string< x "9") (string< "0" x)))
737 (setq x (char-to-string (read-char-exclusive "Insert a number for the denominator (\"x/\")"))))
738 (insert (format "%s/" x)) (setq x "/")
739 (while (not (and (string< x "9") (string< "0" x)))
740 (setq x (char-to-string (read-char-exclusive "Insert a number for the numerator (\"/y\")"))))
741 (insert (format "%s { " x))))
742 ((string= x "0") (progn (setq accid 0) (setq octav 0)
743 (setq durat "") (setq dots 0)))
744 ((string= x "i") (setq accid (if (= accid 2) 0 (max (+ accid 1) 1))))
745 ((string= x "e") (setq accid (if (= accid -2) 0 (min (+ accid -1) -1))))
746 ((string= x "'") (setq octav (if (= octav 4) 0 (max (+ octav 1) 1))))
747 ((string= x ",") (setq octav (if (= octav -4) 0 (min (+ octav -1) -1))))
748 ((string= x ".") (setq dots (if (= dots 4) 0 (+ dots 1))))
749 ((not (null (member x (split-string other-keys ""))))
750 (insert (format "%s " x)))
751 ((not (null note))
752 (progn
753 (setq note
754 (format "%s%s" (car note) (if (string= "r" (car note)) ""
755 (nth (+ accid 2) dutch-note-ends))))
756 (setq notetwo (car (cdr (assoc note note-replacements))))
757 (if (not (null notetwo)) (setq note notetwo))
758 (insert
759 (format "%s%s%s%s "
760 note
761 (if (string= "r" note) ""
762 (make-string (abs octav) (if (> octav 0) ?' ?,)))
763 durat
764 (if (string= durat "") "" (make-string dots ?.))))
765 (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)))
766 ((string= x "t") (progn (setq note-replacements dutch-note-replacements)
767 (message "Selected Dutch notes")
768 (sit-for 0 750 1))) ; t
769 ((string= x "n") (progn (setq note-replacements finnish-note-replacements)
770 (message "Selected Finnish/Deutsch notes")
771 (sit-for 0 750 1))) ; n
772 ; add more translations of the note names
773 ((string= x "p") (progn (setq note-replacements spanish-note-replacements)
774 (message "Selected Spanish notes")
775 (sit-for 0 750 1))) ; p
776 ((string= x "h")
777 (progn (message "Insert notes with fewer key strokes. For example \"i,5.f\" produces \"fis,32. \".") (sit-for 5 0 1)
778 (message "Add also \"a ~ a\"-ties, \"a ( ) b\"-slurs and \"< a b >\"-chords.") (sit-for 5 0 1)
779 (message "Note names are in Du(t)ch by default. Hit 'n' for Fin(n)ish/Deutsch note names. Hit 'p' for S(p)anish note names") (sit-for 5 0 1)
780 (message "Backspace deletes last note, return starts a new indented line and Esc quits.") (sit-for 5 0 1)
781 (message "Insert note triplets \"\\times 2/3 { a b } \" by typing \"/23ab}\".") (sit-for 5 0 1)
782 (message "Remember to add all other details as well.") (sit-for 5 0 1)))
785 (defun LilyPond-pre-word-search ()
786 "Fetch the alphabetic characters and \\ in front of the cursor."
787 (interactive)
788 (let ((pre "")
789 (prelen 0)
790 (ch (char-before (- (point) 0))))
791 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not bolp, A-Z
792 (and (>= ch 97) (<= ch 122)) ; a-z
793 (= ch 92))) ; \\
794 (setq pre (concat (char-to-string ch) pre))
795 (setq prelen (+ prelen 1))
796 (setq ch (char-before (- (point) prelen))))
797 pre))
799 (defun LilyPond-post-word-search ()
800 "Fetch the alphabetic characters behind the cursor."
801 (interactive)
802 (let ((post "")
803 (postlen 0)
804 (ch (char-after (+ (point) 0))))
805 (while (and ch (or (and (>= ch 65) (<= ch 90)) ; not eolp, A-Z
806 (and (>= ch 97) (<= ch 122)))) ; a-z
807 (setq post (concat post (char-to-string ch)))
808 (setq postlen (+ postlen 1))
809 (setq ch (char-after (+ (point) postlen))))
810 post))
812 (defun LilyPond-autocompletion ()
813 "Show completions in mini-buffer for the given word."
814 (interactive)
815 (let ((pre (LilyPond-pre-word-search))
816 (post (LilyPond-post-word-search))
817 (compsstr ""))
818 ;; insert try-completion and show all-completions
819 (if (> (length pre) 0)
820 (progn
821 (setq trycomp (try-completion pre (LilyPond-add-dictionary-word ())))
822 (if (char-or-string-p trycomp)
823 (if (string-equal (concat pre post) trycomp)
824 (goto-char (+ (point) (length post)))
825 (progn
826 (delete-region (point) (+ (point) (length post)))
827 (insert (substring trycomp (length pre) nil))))
828 (progn
829 (delete-region (point) (+ (point) (length post)))
830 (font-lock-fontify-buffer))) ; only inserting fontifies
832 (setq complist (all-completions pre (LilyPond-add-dictionary-word ())))
833 (while (> (length complist) 0)
834 (setq compsstr (concat compsstr "\"" (car complist) "\" "))
835 (setq complist (cdr complist)))
836 (message compsstr)
837 (sit-for 0 100 1)))))
839 (defun LilyPond-info ()
840 "Launch Info for lilypond."
841 (interactive)
842 (info "lilypond"))
844 (defun LilyPond-music-glossary-info ()
845 "Launch Info for music-glossary."
846 (interactive)
847 (info "music-glossary"))
849 (defun LilyPond-internals-info ()
850 "Launch Info for lilypond-internals."
851 (interactive)
852 (info "lilypond-internals"))
854 (defun LilyPond-info-index-search ()
855 "Inside Emacs, launch `info lilypond --index-search word-under-cursor'"
856 (interactive)
857 (let ((str (concat (LilyPond-pre-word-search) (LilyPond-post-word-search))))
858 (if (and (> (length str) 0)
859 (string-equal (substring str 0 1) "\\"))
860 (setq str (substring str 1 nil)))
861 (LilyPond-info)
862 (Info-index str)))
864 (defun LilyPond-insert-string (pre)
865 "Insert text to the buffer."
866 (interactive)
867 (insert pre)
868 (length pre))
870 (defun LilyPond-insert-between (text pre post)
871 "Insert text to the buffer if non-empty string is given."
872 (interactive)
873 (let ((str (read-string text)))
874 (if (string-equal str "")
876 (progn (setq pre_str_post (concat pre str post))
877 (insert pre_str_post)
878 (length pre_str_post)))))
880 (defun LilyPond-insert-tag-notes ()
881 "LilyPond notes tag."
882 (interactive)
883 (setq begin (if (and transient-mark-mode mark-active)
884 (mark-marker) (point-marker)))
885 (setq end (point-marker))
886 (goto-char begin)
887 (setq l1 (LilyPond-insert-string "\\notes "))
888 (setq l2 (LilyPond-insert-between "Relative (e.g. c'): " "\\relative " " "))
889 (if (eq l2 0)
890 (setq l2 (LilyPond-insert-between "Transpose (e.g. c c'): " "\\transpose " " ")))
891 (setq l3 (LilyPond-insert-string "{ "))
892 (goto-char (+ end l1 l2 l3))
893 (LilyPond-insert-string " }")
894 (goto-char (+ end l1 l2 l3)))
896 (defun LilyPond-insert-tag-score ()
897 "LilyPond score tag."
898 (interactive)
899 (setq begin (if (and transient-mark-mode mark-active)
900 (mark-marker) (point-marker)))
901 (setq end (point-marker))
902 (goto-char begin)
903 (setq l1 (LilyPond-insert-string "\\score {\n ")) ; keep track of lengths
904 (goto-char (+ end l1))
905 (LilyPond-insert-string "\n \\paper { }\n")
906 (setq l2 (if (y-or-n-p "Insert \"\\header\" field? ")
907 (+ (LilyPond-insert-string " \\header {")
908 (LilyPond-insert-between "Title: " "\n title = \"" "\"")
909 (LilyPond-insert-between "Subtitle: " "\n subtitle = \"" "\"")
910 (LilyPond-insert-between "Piece: " "\n piece = \"" "\"")
911 (LilyPond-insert-between "Opus: " "\n opus = \"" "\"")
912 (LilyPond-insert-string "\n }\n"))
914 (setq l3 (if (y-or-n-p "Insert \"\\midi\" field? ")
915 (+ (LilyPond-insert-string " \\midi {")
916 (LilyPond-insert-between "Tempo: " " \\tempo (e.g. 4 = 60)" "")
917 (LilyPond-insert-string " }\n"))
919 (setq l4 (LilyPond-insert-string "}\n"))
920 (goto-char (+ end l1)))
922 (defun LilyPond-command-menu-entry (entry)
923 ;; Return LilyPond-command-alist ENTRY as a menu item.
924 (let ((name (car entry)))
925 (cond ((and (string-equal name LilyPond-command-Print)
926 LilyPond-printer-list)
927 (let ((command LilyPond-print-command)
928 (lookup 1))
929 (append (list LilyPond-command-Print)
930 (mapcar 'LilyPond-command-menu-printer-entry
931 LilyPond-printer-list))))
933 (vector name (list 'LilyPond-command-menu name) t)))))
936 (easy-menu-define LilyPond-command-menu
937 LilyPond-mode-map
938 "Menu used in LilyPond mode."
939 (append '("Command")
940 '(("Command on"
941 [ "Master File" LilyPond-command-select-master
942 :keys "C-c C-c" :style radio
943 :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
944 [ "Buffer" LilyPond-command-select-buffer
945 :keys "C-c C-b" :style radio
946 :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
947 [ "Region" LilyPond-command-select-region
948 :keys "C-c C-r" :style radio
949 :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
950 ;;; (let ((file 'LilyPond-command-on-current))
951 ;;; (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
952 ;;; Some kind of mapping which includes :keys might be more elegant
953 ;;; Put keys to Lilypond-command-alist and fetch them from there somehow.
954 '([ "LilyPond" LilyPond-command-lilypond t])
955 '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
956 '([ "2Dvi" LilyPond-command-formatdvi t])
957 '([ "2PS" LilyPond-command-formatps t])
958 '([ "2Midi" LilyPond-command-formatmidi t])
959 '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
960 '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
961 '([ "Kill jobs" LilyPond-kill-jobs t])
962 '("-----")
963 '([ "SmartView" LilyPond-command-smartview t])
964 '([ "View" LilyPond-command-view t])
965 '([ "ViewPS" LilyPond-command-viewps t])
966 '("-----")
967 '([ "Midi (toggle)" LilyPond-command-current-midi t])
968 '([ "Midi all" LilyPond-command-all-midi t])
971 (easy-menu-define LilyPond-mode-menu
972 LilyPond-mode-map
973 "Menu used in LilyPond mode."
974 (append '("LilyPond")
975 '(("Insert"
976 [ "\\notes..." LilyPond-insert-tag-notes t]
977 [ "\\score..." LilyPond-insert-tag-score t]
978 ["Quick Notes" LilyPond-quick-note-insert t]
979 ["Autocompletion" LilyPond-autocompletion t]
981 '(("Miscellaneous"
982 ["Uncomment Region" LilyPond-un-comment-region t]
983 ["Comment Region" comment-region t]
984 ["Refontify buffer" font-lock-fontify-buffer t]
986 '(("Info"
987 ["LilyPond" LilyPond-info t]
988 ["LilyPond index-search" LilyPond-info-index-search t]
989 ["Music Glossary" LilyPond-music-glossary-info t]
990 ["LilyPond internals" LilyPond-internals-info t]
994 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z]+\\) *="
995 "Regexp matching Identifier definitions.")
997 (defvar LilyPond-imenu-generic-expression
998 (list (list nil LilyPond-imenu-generic-re 1))
999 "Expression for imenu")
1001 (defun LilyPond-command-select-master ()
1002 (interactive)
1003 (message "Next command will be on the master file")
1004 (setq LilyPond-command-current 'LilyPond-command-master))
1006 (defun LilyPond-command-select-buffer ()
1007 (interactive)
1008 (message "Next command will be on the buffer")
1009 (setq LilyPond-command-current 'LilyPond-command-buffer))
1011 (defun LilyPond-command-select-region ()
1012 (interactive)
1013 (message "Next command will be on the region")
1014 (setq LilyPond-command-current 'LilyPond-command-region))
1016 (defun LilyPond-command-menu (name)
1017 ;; Execute LilyPond-command-alist NAME from a menu.
1018 (let ((LilyPond-command-force name))
1019 (funcall LilyPond-command-current)))
1021 (defun LilyPond-mode ()
1022 "Major mode for editing LilyPond music files.
1024 This mode knows about LilyPond keywords and line comments, not about
1025 indentation or block comments. It features easy compilation, error
1026 finding and viewing of a LilyPond source buffer or region.
1028 COMMANDS
1029 \\{LilyPond-mode-map}
1030 VARIABLES
1032 LilyPond-command-alist\t\talist from name to command
1033 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
1034 (interactive)
1035 ;; set up local variables
1036 (kill-all-local-variables)
1038 (make-local-variable 'font-lock-defaults)
1039 (setq font-lock-defaults '(LilyPond-font-lock-keywords))
1041 ;; string and comments are fontified explicitly
1042 (make-local-variable 'font-lock-keywords-only)
1043 (setq font-lock-keywords-only t)
1045 ;; Multi-line font-locking needs Emacs 21.1 or newer.
1046 ;; For older versions there is hotkey "C-c f".
1047 (make-local-variable 'font-lock-multiline)
1048 (setq font-lock-multiline t)
1050 (make-local-variable 'paragraph-separate)
1051 (setq paragraph-separate "^[ \t]*$")
1053 (make-local-variable 'paragraph-start)
1054 (setq paragraph-start "^[ \t]*$")
1056 (make-local-variable 'comment-start)
1057 (setq comment-start "%")
1059 (make-local-variable 'comment-start-skip)
1060 (setq comment-start-skip "%{? *")
1062 (make-local-variable 'comment-end)
1063 (setq comment-end "")
1065 (make-local-variable 'block-comment-start)
1066 (setq block-comment-start "%{")
1068 (make-local-variable 'block-comment-end)
1069 (setq block-comment-end "%}")
1071 (make-local-variable 'indent-line-function)
1072 (setq indent-line-function 'LilyPond-indent-line)
1074 (set-syntax-table LilyPond-mode-syntax-table)
1075 (setq major-mode 'LilyPond-mode)
1076 (setq mode-name "LilyPond")
1077 (setq local-abbrev-table LilyPond-mode-abbrev-table)
1078 (use-local-map LilyPond-mode-map)
1080 (make-local-variable 'imenu-generic-expression)
1081 (setq imenu-generic-expression LilyPond-imenu-generic-expression)
1082 (imenu-add-to-menubar "Index")
1084 ;; run the mode hook. LilyPond-mode-hook use is deprecated
1085 (run-hooks 'LilyPond-mode-hook))
1087 (defun LilyPond-version ()
1088 "Echo the current version of `LilyPond-mode' in the minibuffer."
1089 (interactive)
1090 (message "Using `LilyPond-mode' version %s" LilyPond-version))
1092 (load-library "lilypond-font-lock")
1093 (load-library "lilypond-indent")
1095 (provide 'lilypond-mode)
1096 ;;; lilypond-mode.el ends here