]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-mode.el
add a warning about matching \] or \)
[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 (<~>)/}b\\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     (setq xkey (read-char-exclusive))
740     (setq x (char-to-string xkey))
741     (setq note (cdr (assoc x dutch-notes)))
742     (cond
743      ((string= x "q") (progn (setq xkey 27))) ; quit
744      ((= xkey 13) (progn (insert "\n") (LilyPond-indent-line))) ; return
745      ((or (= xkey 127)     ; backspace is recognized as a char only in Emacs
746           (string= x "b")) ; so, we need to define an another char for XEmacs
747       (progn (narrow-to-region xinitpoint (point))
748              (backward-kill-word 1)
749              (widen)))
750      ((and (string< x "9") (string< "0" x))
751       (progn (setq durat (int-to-string (expt 2 (- (string-to-int x) 1))))
752              (setq dots 0)))
753      ((string= x " ") (insert " "))
754      ((string= x "/") (progn (insert "\\times ")
755                              (message "Insert a number for the denominator (\"x/\")")
756                              (while (not (and (string< x "9") (string< "0" x)))
757                                (setq x (char-to-string (read-char-exclusive))))
758                              (insert (format "%s/" x)) (setq x "/")
759                              (message "Insert a number for the numerator (\"/y\")")
760                              (while (not (and (string< x "9") (string< "0" x)))
761                                (setq x (char-to-string (read-char-exclusive))))
762                              (insert (format "%s { " x))))
763      ((string= x "0") (progn (setq accid 0) (setq octav 0)
764                              (setq durat "") (setq dots 0)))
765      ((string= x "i") (setq accid (if (= accid 2) 0 (max (+ accid 1) 1))))
766      ((string= x "e") (setq accid (if (= accid -2) 0 (min (+ accid -1) -1))))
767      ((string= x "'") (setq octav (if (= octav 4) 0 (max (+ octav 1) 1))))
768      ((string= x ",") (setq octav (if (= octav -4) 0 (min (+ octav -1) -1))))
769      ((string= x ".") (setq dots (if (= dots 4) 0 (+ dots 1))))
770      ((not (null (member x (split-string other-keys ""))))
771       (insert (format "%s " x)))
772      ((not (null note))
773       (progn
774         (setq note
775               (format "%s%s" (car note) (if (string= "r" (car note)) ""
776                                           (nth (+ accid 2) dutch-note-ends))))
777         (setq notetwo (car (cdr (assoc note note-replacements))))
778         (if (not (null notetwo)) (setq note notetwo))
779         (insert
780          (format "%s%s%s%s "
781                  note
782                  (if (string= "r" note) ""
783                      (make-string (abs octav) (if (> octav 0) ?' ?,)))
784                  durat
785                  (if (string= durat "") "" (make-string dots ?.))))
786         (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)))
787      ((string= x "t") (progn (setq note-replacements dutch-note-replacements)
788                              (message "Selected Dutch notes")
789                              (sit-for 0 750)))
790      ((string= x "n") (progn (setq note-replacements finnish-note-replacements)
791                              (message "Selected Finnish/Deutsch notes")
792                              (sit-for 0 750)))
793                               ; add more translations of the note names
794      ((string= x "p") (progn (setq note-replacements spanish-note-replacements)
795                              (message "Selected Spanish notes")
796                              (sit-for 0 750)))
797      ((string= x "h")
798       (progn (message "Insert notes with fewer key strokes. For example \"i,5.f\" produces \"fis,32. \".") (sit-for 5 0)
799              (message "Add also \"a ~ a\"-ties, \"a ( ) b\"-slurs and \"< a b >\"-chords.") (sit-for 5 0)
800              (message "There are Du(t)ch, Fin(n)ish/Deutsch (hit 'n') and S(p)anish note names.") (sit-for 5 0)
801              (message "(B)ackspace deletes last note, Ret starts a new indented line and Esc (q)uits.") (sit-for 5 0)
802              (message "Insert note triplets \"\\times 2/3 { a b } \" by typing \"/23ab}\".") (sit-for 5 0)
803              (message "This mode is experimental. Use normal mode to add further details.") (sit-for 5 0)))))
804   (message "Normal editing mode."))
805
806 (defun LilyPond-pre-word-search ()
807   "Fetch the alphabetic characters and \\ in front of the cursor."
808   (let ((pre "")
809         (prelen 0)
810         (ch (char-before (- (point) 0))))
811     (while (and ch (or (and (>= ch 65) (<= ch 90))  ; not bolp, A-Z
812                        (and (>= ch 97) (<= ch 122)) ; a-z
813                        (= ch 92)))                  ; \\
814       (setq pre (concat (char-to-string ch) pre))
815       (setq prelen (+ prelen 1))
816       (setq ch (char-before (- (point) prelen))))
817     pre))
818
819 (defun LilyPond-post-word-search ()
820   "Fetch the alphabetic characters behind the cursor."
821   (let ((post "")
822         (postlen 0)
823         (ch (char-after (+ (point) 0))))
824     (while (and ch (or (and (>= ch 65) (<= ch 90))    ; not eolp, A-Z
825                        (and (>= ch 97) (<= ch 122)))) ; a-z
826       (setq post (concat post (char-to-string ch)))
827       (setq postlen (+ postlen 1))
828       (setq ch (char-after (+ (point) postlen))))
829     post))
830
831 (defun LilyPond-autocompletion ()
832   "Show completions in mini-buffer for the given word."
833   (interactive)
834   (let ((pre (LilyPond-pre-word-search))
835         (post (LilyPond-post-word-search))
836         (compsstr ""))
837     ;; insert try-completion and show all-completions
838     (if (> (length pre) 0)
839         (progn
840           (setq trycomp (try-completion pre (LilyPond-add-dictionary-word ())))
841           (if (char-or-string-p trycomp)
842               (if (string-equal (concat pre post) trycomp)
843                   (goto-char (+ (point) (length post)))
844                 (progn
845                   (delete-region (point) (+ (point) (length post)))
846                   (insert (substring trycomp (length pre) nil))))
847             (progn
848               (delete-region (point) (+ (point) (length post)))
849               (font-lock-fontify-buffer))) ; only inserting fontifies
850         
851         (setq complist (all-completions pre (LilyPond-add-dictionary-word ())))
852         (while (> (length complist) 0)
853           (setq compsstr (concat compsstr "\"" (car complist) "\" "))
854           (setq complist (cdr complist)))
855         (message compsstr) 
856         (sit-for 0 100)))))
857
858 (defun LilyPond-info ()
859   "Launch Info for lilypond."
860   (interactive)
861   (info "lilypond"))
862   
863 (defun LilyPond-music-glossary-info ()
864   "Launch Info for music-glossary."
865   (interactive)
866   (info "music-glossary"))
867
868 (defun LilyPond-internals-info ()
869   "Launch Info for lilypond-internals."
870   (interactive)
871   (info "lilypond-internals"))
872   
873 (defun LilyPond-info-index-search ()
874   "In `*info*'-buffer, launch `info lilypond --index-search word-under-cursor'"
875   (interactive)
876   (let ((str (concat (LilyPond-pre-word-search) (LilyPond-post-word-search))))
877     (if (and (> (length str) 0) 
878              (string-equal (substring str 0 1) "\\"))
879         (setq str (substring str 1 nil)))
880     (LilyPond-info)
881     (Info-index str)))
882
883 (defun LilyPond-insert-string (pre)
884   "Insert text to the buffer."
885   (insert pre)
886   (length pre))
887
888 (defun LilyPond-insert-between (text pre post)
889   "Insert text to the buffer if non-empty string is given."
890   (let ((str (read-string text)))
891     (if (string-equal str "")
892         0
893       (progn (setq pre_str_post (concat pre str post))
894              (insert pre_str_post)
895              (length pre_str_post)))))
896
897 (defun LilyPond-insert-tag-notes ()
898   "LilyPond notes tag."
899   (interactive)
900   (setq begin (if (LilyPond-mark-active)
901                   (mark-marker) (point-marker)))
902   (setq end (point-marker))
903   (goto-char begin)
904   (setq l1 (LilyPond-insert-string "\\notes "))
905   (setq l2 (LilyPond-insert-between "Relative (e.g. c'): " "\\relative " " "))
906   (if (eq l2 0)
907       (setq l2 (LilyPond-insert-between "Transpose (e.g. c c'): " "\\transpose " " ")))
908   (setq l3 (LilyPond-insert-string "{ "))
909   (goto-char (+ end l1 l2 l3))
910   (LilyPond-insert-string " }")
911   (goto-char (+ end l1 l2 l3)))
912
913 (defun LilyPond-insert-tag-score ()
914   "LilyPond score tag."
915   (interactive)
916   (setq begin (if (LilyPond-mark-active) 
917                   (mark-marker) (point-marker)))
918   (setq end (point-marker))
919   (goto-char begin)
920   (setq l1 (LilyPond-insert-string "\\score {\n    ")) ; keep track of lengths
921   (goto-char (+ end l1))
922   (LilyPond-insert-string "\n    \\paper { }\n")
923   (setq l2 (if (y-or-n-p "Insert \"\\header\" field? ")
924                (+ (LilyPond-insert-string "    \\header {")
925                   (LilyPond-insert-between "Title: " "\n      title = \"" "\"")
926                   (LilyPond-insert-between "Subtitle: " "\n      subtitle = \"" "\"")
927                   (LilyPond-insert-between "Piece: " "\n      piece = \"" "\"")
928                   (LilyPond-insert-between "Opus: "  "\n      opus = \"" "\"")
929                   (LilyPond-insert-string "\n    }\n"))
930              0))
931   (setq l3 (if (y-or-n-p "Insert \"\\midi\" field? ")
932                (+ (LilyPond-insert-string "    \\midi {")
933                   (LilyPond-insert-between "Tempo: " " \\tempo (e.g. 4 = 60)" "")
934                   (LilyPond-insert-string " }\n"))
935              0))
936   (setq l4 (LilyPond-insert-string "}\n"))
937   (goto-char (+ end l1)))
938
939 (defun LilyPond-command-menu-entry (entry)
940   ;; Return LilyPond-command-alist ENTRY as a menu item.
941   (let ((name (car entry)))
942     (cond ((and (string-equal name LilyPond-command-Print)
943                 LilyPond-printer-list)
944            (let ((command LilyPond-print-command)
945                  (lookup 1))
946              (append (list LilyPond-command-Print)
947                      (mapcar 'LilyPond-command-menu-printer-entry
948                              LilyPond-printer-list))))
949           (t
950            (vector name (list 'LilyPond-command-menu name) t)))))
951
952
953 (easy-menu-define LilyPond-command-menu
954   LilyPond-mode-map
955   "Menu used in LilyPond mode."
956   (append '("Command")
957           '(("Command on"
958              [ "Master File" LilyPond-command-select-master
959                :keys "C-c C-c" :style radio
960                :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
961              [ "Buffer" LilyPond-command-select-buffer
962                :keys "C-c C-b" :style radio
963                :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
964              [ "Region" LilyPond-command-select-region
965                :keys "C-c C-r" :style radio
966                :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
967 ;;;       (let ((file 'LilyPond-command-on-current))
968 ;;;         (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
969 ;;; Some kind of mapping which includes :keys might be more elegant
970 ;;; Put keys to Lilypond-command-alist and fetch them from there somehow.
971           '([ "LilyPond" LilyPond-command-lilypond t])
972           '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
973           '([ "2Dvi" LilyPond-command-formatdvi t])
974           '([ "2PS" LilyPond-command-formatps t])
975           '([ "2Midi" LilyPond-command-formatmidi t])
976           '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
977           '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
978           '([ "Kill jobs" LilyPond-kill-jobs t])
979           '("-----")
980           '([ "SmartView" LilyPond-command-smartview t])
981           '([ "View" LilyPond-command-view t])
982           '([ "ViewPS" LilyPond-command-viewps t])
983           '("-----")
984           '([ "Midi (toggle)" LilyPond-command-current-midi t])
985           '([ "Midi all" LilyPond-command-all-midi t])
986           ))
987
988 ;;; LilyPond-mode-menu should not be interactive, via "M-x LilyPond-<Tab>"
989 (easy-menu-define LilyPond-mode-menu
990   LilyPond-mode-map
991   "Menu used in LilyPond mode."
992   (append '("LilyPond")
993           '(("Insert"
994              [ "\\notes..."  LilyPond-insert-tag-notes t]
995              [ "\\score..."  LilyPond-insert-tag-score t]
996              ["Quick Notes"  LilyPond-quick-note-insert t]
997              ["Autocompletion"   LilyPond-autocompletion t]
998              ))
999           '(("Miscellaneous"
1000              ["Uncomment Region" LilyPond-un-comment-region t]
1001              ["Comment Region" comment-region t]
1002              ["Refontify buffer" font-lock-fontify-buffer t]
1003              ))
1004           '(("Info"
1005              ["LilyPond" LilyPond-info t]
1006              ["LilyPond index-search" LilyPond-info-index-search t]
1007              ["Music Glossary" LilyPond-music-glossary-info t]
1008              ["LilyPond internals" LilyPond-internals-info t]
1009              ))
1010           ))
1011
1012 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z]+\\) *="
1013   "Regexp matching Identifier definitions.")
1014
1015 (defvar LilyPond-imenu-generic-expression
1016   (list (list nil LilyPond-imenu-generic-re 1))
1017   "Expression for imenu")
1018
1019 (defun LilyPond-command-select-master ()
1020   (interactive)
1021   (message "Next command will be on the master file")
1022   (setq LilyPond-command-current 'LilyPond-command-master))
1023
1024 (defun LilyPond-command-select-buffer ()
1025   (interactive)
1026   (message "Next command will be on the buffer")
1027   (setq LilyPond-command-current 'LilyPond-command-buffer))
1028
1029 (defun LilyPond-command-select-region ()
1030   (interactive)
1031   (message "Next command will be on the region")
1032   (setq LilyPond-command-current 'LilyPond-command-region))
1033
1034 (defun LilyPond-command-menu (name)
1035   ;; Execute LilyPond-command-alist NAME from a menu.
1036   (let ((LilyPond-command-force name))
1037     (if (eq LilyPond-command-current 'LilyPond-command-region)
1038         (if (eq (mark t) nil)
1039             (progn (message "The mark is not set now") (sit-for 0 500))
1040           (progn (if (not (not (LilyPond-mark-active)))
1041                      (progn (message "Region is not active, using region between inactive mark and current point.") (sit-for 0 500)))
1042                  (LilyPond-command-region (mark t) (point))))
1043       (funcall LilyPond-command-current))))
1044
1045 (defun LilyPond-mode ()
1046   "Major mode for editing LilyPond music files.
1047
1048 This mode knows about LilyPond keywords and line comments, not about
1049 indentation or block comments.  It features easy compilation, error
1050 finding and viewing of a LilyPond source buffer or region.
1051
1052 COMMANDS
1053 \\{LilyPond-mode-map}
1054 VARIABLES
1055
1056 LilyPond-command-alist\t\talist from name to command
1057 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
1058   (interactive)
1059   ;; set up local variables
1060   (kill-all-local-variables)
1061
1062   (make-local-variable 'font-lock-defaults)
1063   (setq font-lock-defaults '(LilyPond-font-lock-keywords))
1064
1065   ;; string and comments are fontified explicitly
1066   (make-local-variable 'font-lock-keywords-only)
1067   (setq font-lock-keywords-only t)
1068
1069   ;; Multi-line font-locking needs Emacs 21.1 or newer.
1070   ;; For older versions there is hotkey "C-c f".
1071   (make-local-variable 'font-lock-multiline) 
1072   (setq font-lock-multiline t) 
1073
1074   (make-local-variable 'paragraph-separate)
1075   (setq paragraph-separate "^[ \t]*$")
1076
1077   (make-local-variable 'paragraph-start)
1078   (setq paragraph-start "^[ \t]*$")
1079
1080   (make-local-variable 'comment-start)
1081   (setq comment-start "%")
1082
1083   (make-local-variable 'comment-start-skip)
1084   (setq comment-start-skip "%{? *")
1085
1086   (make-local-variable 'comment-end)
1087   (setq comment-end "")
1088
1089   (make-local-variable 'block-comment-start)
1090   (setq block-comment-start "%{")
1091
1092   (make-local-variable 'block-comment-end)  
1093   (setq block-comment-end   "%}")
1094
1095   (make-local-variable 'indent-line-function)
1096   (setq indent-line-function 'LilyPond-indent-line)
1097
1098   (set-syntax-table LilyPond-mode-syntax-table)
1099   (setq major-mode 'LilyPond-mode)
1100   (setq mode-name "LilyPond")
1101   (setq local-abbrev-table LilyPond-mode-abbrev-table)
1102   (use-local-map LilyPond-mode-map)
1103
1104   ;; In Emacs blink-...-on-screen needs to be declared.
1105   (if (not (string-match "XEmacs\\|Lucid" emacs-version))
1106       (progn
1107         (make-local-variable 'blink-matching-paren-on-screen)
1108         (setq blink-matching-paren-on-screen t)))
1109
1110   ;; In XEmacs imenu was synched up with: FSF 20.4
1111   (make-local-variable 'imenu-generic-expression)
1112   (setq imenu-generic-expression LilyPond-imenu-generic-expression)
1113   (imenu-add-to-menubar "Index")
1114
1115   ;; In XEmacs one needs to use 'easy-menu-add'.
1116   (if (string-match "XEmacs\\|Lucid" emacs-version)
1117       (progn
1118         (easy-menu-add LilyPond-mode-menu)
1119         (easy-menu-add LilyPond-command-menu)))
1120
1121   ;; Use Command on Region even for inactive mark (region).
1122   (if (string-match "XEmacs\\|Lucid" emacs-version)
1123       (setq zmacs-regions nil)
1124     (setq mark-even-if-inactive t))
1125
1126   ;; run the mode hook. LilyPond-mode-hook use is deprecated
1127   (run-hooks 'LilyPond-mode-hook))
1128
1129 (defun LilyPond-version ()
1130   "Echo the current version of `LilyPond-mode' in the minibuffer."
1131   (interactive)
1132   (message "Using `LilyPond-mode' version %s" LilyPond-version))
1133
1134 (load-library "lilypond-font-lock")
1135 (load-library "lilypond-indent")
1136
1137 (provide 'lilypond-mode)
1138 ;;; lilypond-mode.el ends here
1139