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