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