]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-mode.el
* scm/music-functions.scm (make-ottava-set):
[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.21"
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 ;; FIXME, this is broken
517 (defun LilyPond-region-file (begin end)
518   (let (
519         ;; (dir "./")
520         (dir (LilyPond-temp-directory))
521         (base LilyPond-region-file-prefix)
522         (ext LilyPond-file-extension))
523     (concat dir base ext)))
524
525 ;;; Commands on Region work if there is an appropriate '\score'.
526 (defun LilyPond-command-region (begin end)
527   "Run LilyPond on the current region."
528   (interactive "r")
529   (if (or (> begin (point-min)) (< end (point-max)))
530       (LilyPond-command-select-region))
531   (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
532   (LilyPond-command (LilyPond-command-query
533                      (LilyPond-region-file begin end))
534                     '(lambda () (LilyPond-region-file begin end)))
535   ;; Region may deactivate even if buffer was intact, reactivate?
536   ;; Currently, also deactived regions are used.
537   )
538
539 (defun LilyPond-command-buffer ()
540   "Run LilyPond on buffer."
541   (interactive)
542   (LilyPond-command-select-buffer)
543   (LilyPond-command-region (point-min) (point-max)))
544
545 (defun LilyPond-command-expand (string file)
546   (let ((case-fold-search nil))
547     (if (string-match "%" string)
548         (let* ((b (match-beginning 0))
549                (e (+ b 2))
550                (l (split-file-name file))
551                (dir (car l))
552                (base (cadr l)))
553           (LilyPond-command-expand
554            (concat (substring string 0 b)
555                    dir
556                    base
557                    (let ((entry (assoc (substring string b e)
558                                        LilyPond-expand-alist)))
559                      (if entry (cdr entry) ""))
560                    (substring string e))
561            file))
562       string)))
563
564 (defun LilyPond-shell-process (name buffer command)
565   (let ((old (current-buffer)))
566     (switch-to-buffer-other-window buffer)
567     ;; If we empty the buffer don't see messages scroll by.
568     ;; (erase-buffer)
569     
570     (start-process-shell-command name buffer command)
571     (switch-to-buffer-other-window old)))
572   
573
574 (defun LilyPond-command (name file)
575   "Run command NAME on the file you get by calling FILE.
576
577 FILE is a function return a file name.  It has one optional argument,
578 the extension to use on the file.
579
580 Use the information in LilyPond-command-alist to determine how to run the
581 command."
582   
583   (let ((entry (assoc name LilyPond-command-alist)))
584     (if entry
585         (let ((command (LilyPond-command-expand (cadr entry)
586                                                 (apply file nil)))
587               (jobs nil)
588               (job-string "no jobs"))
589           (if (member name (list "View" "ViewPS"))
590               ;; is USR1 a right signal for viewps?
591               (let ((buffer-xdvi (get-buffer-create (concat "*" name "*"))))
592                 (if LilyPond-kick-xdvi
593                   (let ((process-xdvi (get-buffer-process buffer-xdvi)))
594                     (if process-xdvi
595                         (signal-process (process-id process-xdvi) 'SIGUSR1)
596                       (LilyPond-shell-process name buffer-xdvi command)))
597                   (LilyPond-shell-process name buffer-xdvi command)))
598             (progn
599               (if (string-equal name "Midi")
600                   (progn
601                     (setq command (concat LilyPond-midi-command " " (LilyPond-string-current-midi)))
602                     (if (LilyPond-kill-midi)
603                         (setq job-string nil)))) ; either stop or start playing
604               (if (string-equal name "MidiAll")
605                   (progn
606                     (setq command (concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)))
607                     (LilyPond-kill-midi))) ; stop and start playing
608               (if (and (member name (list "Midi" "MidiAll")) job-string)
609                   (if (file-newer-than-file-p
610                        (LilyPond-master-file)
611                        (concat (substring (LilyPond-master-file) 0 -3) ".midi"))
612                       (if (y-or-n-p "Midi older than source. Reformat midi?")
613                           (progn
614                             (LilyPond-command-formatmidi)
615                             (while (LilyPond-running)
616                               (message "Starts playing midi once it is built.")
617                               (sit-for 0 100))))))
618               (if (member name (list "LilyPond" "TeX" "2Midi" "2PS" "2Dvi" 
619                                      "Book" "LaTeX"))
620                   (if (setq jobs (LilyPond-running))
621                       (progn
622                         (setq job-string "Process") ; could also suggest compiling after process has ended
623                         (while jobs
624                           (setq job-string (concat job-string " \"" (pop jobs) "\"")))
625                         (setq job-string (concat job-string " is already running; kill it to proceed "))
626                         (if (y-or-n-p job-string)
627                             (progn
628                               (setq job-string "no jobs")
629                               (LilyPond-kill-jobs)
630                               (while (LilyPond-running)
631                                 (sit-for 0 100)))
632                           (setq job-string nil)))))
633
634               (setq LilyPond-command-default name)
635               (if (string-equal job-string "no jobs")
636                   (LilyPond-compile-file command name))))))))
637           
638 (defun LilyPond-mark-active ()
639   "Check if there is an active mark."
640   (and transient-mark-mode
641        (if (string-match "XEmacs\\|Lucid" emacs-version) (mark) mark-active)))
642
643 (defun LilyPond-temp-directory ()
644   "Temporary file directory for Commands on Region."
645   (interactive)
646   (if (string-match "XEmacs\\|Lucid" emacs-version)
647       (concat (temp-directory) "/")
648     temporary-file-directory))
649
650 ;;; Keymap
651
652 (defvar LilyPond-mode-map ()
653   "Keymap used in `LilyPond-mode' buffers.")
654
655 ;; Note:  if you make changes to the map, you must do
656 ;;    M-x set-variable LilyPond-mode-map nil
657 ;;    M-x eval-buffer
658 ;;    M-x LilyPond-mode
659 ;; to let the changest take effect
660
661 (if LilyPond-mode-map
662     ()
663   (setq LilyPond-mode-map (make-sparse-keymap))
664   ;; Put keys to Lilypond-command-alist and fetch them from there somehow.
665   (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
666   (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
667   (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
668   (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-jobs)
669   (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
670   (define-key LilyPond-mode-map "\C-cm" 'LilyPond-command-formatmidi)
671   (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
672   (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
673   (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
674   (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
675   (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
676   (define-key LilyPond-mode-map [(control c) return] 'LilyPond-command-current-midi)
677   (define-key LilyPond-mode-map [(control c) (control return)] 'LilyPond-command-all-midi)
678   (define-key LilyPond-mode-map "\C-x\C-s" 'LilyPond-save-buffer)
679   (define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
680   (define-key LilyPond-mode-map "\C-ci" 'LilyPond-quick-note-insert)
681   (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
682   (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
683   (define-key LilyPond-mode-map "\C-c;" 'LilyPond-comment-region)
684   (define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren)
685   (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren)
686   (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren)
687   (define-key LilyPond-mode-map "]" 'LilyPond-electric-close-paren)
688   (if (string-match "XEmacs\\|Lucid" emacs-version)
689       (define-key LilyPond-mode-map [iso-left-tab] 'LilyPond-autocompletion)
690     (define-key LilyPond-mode-map [iso-lefttab] 'LilyPond-autocompletion))
691   (define-key LilyPond-mode-map "\C-c\t" 'LilyPond-info-index-search)
692   )
693
694 ;;; Menu Support
695
696 (defun LilyPond-quick-note-insert()
697   "Insert notes with fewer key strokes by using a simple keyboard piano."
698   (interactive)
699   (setq dutch-notes
700        '(("k" "a") ("l" "b") ("a" "c") ("s" "d")
701          ("d" "e") ("f" "f") ("j" "g") ("r" "r")))
702   (setq dutch-note-ends '("eses" "es" "" "is" "isis"))
703   (setq dutch-note-replacements '("" ""))
704   (setq finnish-note-replacements
705        '(("eeses" "eses") ("ees" "es") ("aeses" "asas") ("aes" "as") ("b" "h")
706          ("beses" "heses") ("bes" "b") ("bis" "his") ("bisis" "hisis")))
707                              ; add more translations of the note names
708   (setq spanish-note-replacements
709        '(("c" "do") ("d" "re") ("e" "mi") ("f" "fa") ("g" "sol") ("a" "la") ("b
710 " "si")
711       ("cis" "dos") ("cisis" "doss") ("ces" "dob") ("ceses" "dobb")
712       ("dis" "res") ("disis" "ress") ("des" "reb") ("deses" "rebb")
713       ("eis" "mis") ("eisis" "miss") ("ees" "mib") ("eeses" "mibb")
714       ("fis" "fas") ("fisis" "fass") ("fes" "fab") ("feses" "fabb")
715       ("gis" "sols") ("gisis" "solss") ("ges" "solb") ("geses" "solbb")
716       ("ais" "las") ("aisis" "lass") ("aes" "lab") ("aeses" "labb")
717       ("bis" "sis") ("bisis" "siss") ("bes" "sib") ("beses" "sibb")))
718   (setq other-keys "(<~>)}|")
719   (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)
720
721   (message "Press h for help.") (sit-for 0 750)
722
723   (setq note-replacements dutch-note-replacements)
724   (setq xkey 0) (setq xinitpoint (point))
725   (< xinitpoint (point))
726   (while (not (= xkey 27)) ; esc to quit
727     (message (format " a[]s[]d f[]j[]k[]l r with %4s%-4s%3s%-4s ie ,' 12345678 . 0 /%sb\\b\\n Esc"
728                      (nth (+ accid 2) dutch-note-ends)
729                      (make-string (abs octav) (if (> octav 0) ?' ?,))
730                      durat
731                      (if (string= durat "") "" (make-string dots ?.))
732                      other-keys))
733     (setq xkey (read-char-exclusive))
734     (setq x (char-to-string xkey))
735     (setq note (cdr (assoc x dutch-notes)))
736     (cond
737      ((string= x "q") (progn (setq xkey 27))) ; quit
738      ((= xkey 13) (progn (insert "\n") (LilyPond-indent-line))) ; return
739      ((or (= xkey 127)     ; backspace is recognized as a char only in Emacs
740           (string= x "b")) ; so, we need to define an another char for XEmacs
741       (progn (narrow-to-region xinitpoint (point))
742              (backward-kill-word 1)
743              (widen)))
744      ((and (string< x "9") (string< "0" x))
745       (progn (setq durat (int-to-string (expt 2 (- (string-to-int x) 1))))
746              (setq dots 0)))
747      ((string= x " ") (insert " "))
748      ((string= x "/") (progn (insert "\\times ")
749                              (message "Insert a number for the denominator (\"x/\")")
750                              (while (not (and (string< x "9") (string< "0" x)))
751                                (setq x (char-to-string (read-char-exclusive))))
752                              (insert (format "%s/" x)) (setq x "/")
753                              (message "Insert a number for the numerator (\"/y\")")
754                              (while (not (and (string< x "9") (string< "0" x)))
755                                (setq x (char-to-string (read-char-exclusive))))
756                              (insert (format "%s { " x))))
757      ((string= x "0") (progn (setq accid 0) (setq octav 0)
758                              (setq durat "") (setq dots 0)))
759      ((string= x "i") (setq accid (if (= accid 2) 0 (max (+ accid 1) 1))))
760      ((string= x "e") (setq accid (if (= accid -2) 0 (min (+ accid -1) -1))))
761      ((string= x "'") (setq octav (if (= octav 4) 0 (max (+ octav 1) 1))))
762      ((string= x ",") (setq octav (if (= octav -4) 0 (min (+ octav -1) -1))))
763      ((string= x ".") (setq dots (if (= dots 4) 0 (+ dots 1))))
764      ((not (null (member x (split-string other-keys ""))))
765       (insert (format "%s " x)))
766      ((not (null note))
767       (progn
768         (setq note
769               (format "%s%s" (car note) (if (string= "r" (car note)) ""
770                                           (nth (+ accid 2) dutch-note-ends))))
771         (setq notetwo (car (cdr (assoc note note-replacements))))
772         (if (not (null notetwo)) (setq note notetwo))
773         (insert
774          (format "%s%s%s%s "
775                  note
776                  (if (string= "r" note) ""
777                      (make-string (abs octav) (if (> octav 0) ?' ?,)))
778                  durat
779                  (if (string= durat "") "" (make-string dots ?.))))
780         (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)))
781      ((string= x "t") (progn (setq note-replacements dutch-note-replacements)
782                              (message "Selected Dutch notes")
783                              (sit-for 0 750)))
784      ((string= x "n") (progn (setq note-replacements finnish-note-replacements)
785                              (message "Selected Finnish/Deutsch notes")
786                              (sit-for 0 750)))
787                               ; add more translations of the note names
788      ((string= x "p") (progn (setq note-replacements spanish-note-replacements)
789                              (message "Selected Spanish notes")
790                              (sit-for 0 750)))
791      ((string= x "h")
792       (progn (message "Insert notes with fewer key strokes. For example \"i,5.f\" produces \"fis,32. \".") (sit-for 5 0)
793              (message "Add also \"a ~ a\"-ties, \"a ( ) b\"-slurs and \"< a b >\"-chords.") (sit-for 5 0)
794              (message "There are Du(t)ch, Fin(n)ish/Deutsch (hit 'n') and S(p)anish note names.") (sit-for 5 0)
795              (message "(B)ackspace deletes last note, Ret starts a new indented line and Esc (q)uits.") (sit-for 5 0)
796              (message "Insert note triplets \"\\times 2/3 { a b } \" by typing \"/23ab}\".") (sit-for 5 0)
797              (message "This mode is experimental. Use normal mode to add further details.") (sit-for 5 0)))
798      (t (progn (message "Quick notes mode. Press Esc or q to quit.") (sit-for 0 500)))))
799   (message "Normal editing mode."))
800
801 (defun LilyPond-pre-word-search ()
802   "Fetch the alphabetic characters and \\ in front of the cursor."
803   (let ((pre "")
804         (prelen 0)
805         (ch (char-before (- (point) 0))))
806     (while (and ch (or (and (>= ch 65) (<= ch 90))  ; not bolp, A-Z
807                        (and (>= ch 97) (<= ch 122)) ; a-z
808                        (= ch 92)))                  ; \\
809       (setq pre (concat (char-to-string ch) pre))
810       (setq prelen (+ prelen 1))
811       (setq ch (char-before (- (point) prelen))))
812     pre))
813
814 (defun LilyPond-post-word-search ()
815   "Fetch the alphabetic characters behind the cursor."
816   (let ((post "")
817         (postlen 0)
818         (ch (char-after (+ (point) 0))))
819     (while (and ch (or (and (>= ch 65) (<= ch 90))    ; not eolp, A-Z
820                        (and (>= ch 97) (<= ch 122)))) ; a-z
821       (setq post (concat post (char-to-string ch)))
822       (setq postlen (+ postlen 1))
823       (setq ch (char-after (+ (point) postlen))))
824     post))
825
826 (defun LilyPond-autocompletion ()
827   "Show completions in mini-buffer for the given word."
828   (interactive)
829   (let ((pre (LilyPond-pre-word-search))
830         (post (LilyPond-post-word-search))
831         (compsstr ""))
832     ;; insert try-completion and show all-completions
833     (if (> (length pre) 0)
834         (progn
835           (setq trycomp (try-completion pre (LilyPond-add-dictionary-word ())))
836           (if (char-or-string-p trycomp)
837               (if (string-equal (concat pre post) trycomp)
838                   (goto-char (+ (point) (length post)))
839                 (progn
840                   (delete-region (point) (+ (point) (length post)))
841                   (insert (substring trycomp (length pre) nil))))
842             (progn
843               (delete-region (point) (+ (point) (length post)))
844               (font-lock-fontify-buffer))) ; only inserting fontifies
845         
846         (setq complist (all-completions pre (LilyPond-add-dictionary-word ())))
847         (while (> (length complist) 0)
848           (setq compsstr (concat compsstr "\"" (car complist) "\" "))
849           (setq complist (cdr complist)))
850         (message compsstr) 
851         (sit-for 0 100)))))
852
853 (defun LilyPond-info ()
854   "Launch Info for lilypond."
855   (interactive)
856   (info "lilypond"))
857   
858 (defun LilyPond-music-glossary-info ()
859   "Launch Info for music-glossary."
860   (interactive)
861   (info "music-glossary"))
862
863 (defun LilyPond-internals-info ()
864   "Launch Info for lilypond-internals."
865   (interactive)
866   (info "lilypond-internals"))
867   
868 (defun LilyPond-info-index-search ()
869   "In `*info*'-buffer, launch `info lilypond --index-search word-under-cursor'"
870   (interactive)
871   (let ((str (concat (LilyPond-pre-word-search) (LilyPond-post-word-search))))
872     (if (and (> (length str) 0) 
873              (string-equal (substring str 0 1) "\\"))
874         (setq str (substring str 1 nil)))
875     (LilyPond-info)
876     (Info-index str)))
877
878 (defun LilyPond-insert-string (pre)
879   "Insert text to the buffer."
880   (insert pre)
881   (length pre))
882
883 (defun LilyPond-insert-between (text pre post)
884   "Insert text to the buffer if non-empty string is given."
885   (let ((str (read-string text)))
886     (if (string-equal str "")
887         0
888       (progn (setq pre_str_post (concat pre str post))
889              (insert pre_str_post)
890              (length pre_str_post)))))
891
892 (defun LilyPond-insert-tag-notes ()
893   "LilyPond notes tag."
894   (interactive)
895   (setq begin (if (LilyPond-mark-active)
896                   (mark-marker) (point-marker)))
897   (setq end (point-marker))
898   (goto-char begin)
899   (setq l1 (LilyPond-insert-string "\\notes "))
900   (setq l2 (LilyPond-insert-between "Relative (e.g. c'): " "\\relative " " "))
901   (if (eq l2 0)
902       (setq l2 (LilyPond-insert-between "Transpose (e.g. c c'): " "\\transpose " " ")))
903   (setq l3 (LilyPond-insert-string "{ "))
904   (goto-char (+ end l1 l2 l3))
905   (LilyPond-insert-string " }")
906   (goto-char (+ end l1 l2 l3)))
907
908 (defun LilyPond-insert-tag-score ()
909   "LilyPond score tag."
910   (interactive)
911   (setq begin (if (LilyPond-mark-active) 
912                   (mark-marker) (point-marker)))
913   (setq end (point-marker))
914   (goto-char begin)
915   (setq l1 (LilyPond-insert-string "\\score {\n    ")) ; keep track of lengths
916   (goto-char (+ end l1))
917   (LilyPond-insert-string "\n    \\paper { }\n")
918   (setq l2 (if (y-or-n-p "Insert \"\\header\" field? ")
919                (+ (LilyPond-insert-string "    \\header {")
920                   (LilyPond-insert-between "Title: " "\n      title = \"" "\"")
921                   (LilyPond-insert-between "Subtitle: " "\n      subtitle = \"" "\"")
922                   (LilyPond-insert-between "Piece: " "\n      piece = \"" "\"")
923                   (LilyPond-insert-between "Opus: "  "\n      opus = \"" "\"")
924                   (LilyPond-insert-string "\n    }\n"))
925              0))
926   (setq l3 (if (y-or-n-p "Insert \"\\midi\" field? ")
927                (+ (LilyPond-insert-string "    \\midi {")
928                   (LilyPond-insert-between "Tempo: (e.g. 4=60) " " \\tempo " "")
929                   (LilyPond-insert-string " }\n"))
930              0))
931   (setq l4 (LilyPond-insert-string "}\n"))
932   (goto-char (+ end l1)))
933
934 (defun LilyPond-command-menu-entry (entry)
935   ;; Return LilyPond-command-alist ENTRY as a menu item.
936   (let ((name (car entry)))
937     (cond ((and (string-equal name LilyPond-command-Print)
938                 LilyPond-printer-list)
939            (let ((command LilyPond-print-command)
940                  (lookup 1))
941              (append (list LilyPond-command-Print)
942                      (mapcar 'LilyPond-command-menu-printer-entry
943                              LilyPond-printer-list))))
944           (t
945            (vector name (list 'LilyPond-command-menu name) t)))))
946
947
948 (easy-menu-define LilyPond-command-menu
949   LilyPond-mode-map
950   "Menu used in LilyPond mode."
951   (append '("Command")
952           '(("Command on"
953              [ "Master File" LilyPond-command-select-master
954                :keys "C-c C-c" :style radio
955                :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
956              [ "Buffer" LilyPond-command-select-buffer
957                :keys "C-c C-b" :style radio
958                :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
959              [ "Region" LilyPond-command-select-region
960                :keys "C-c C-r" :style radio
961                :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
962 ;;;       (let ((file 'LilyPond-command-on-current))
963 ;;;         (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
964 ;;; Some kind of mapping which includes :keys might be more elegant
965 ;;; Put keys to Lilypond-command-alist and fetch them from there somehow.
966           '([ "LilyPond" LilyPond-command-lilypond t])
967           '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
968           '([ "2Dvi" LilyPond-command-formatdvi t])
969           '([ "2PS" LilyPond-command-formatps t])
970           '([ "2Midi" LilyPond-command-formatmidi t])
971           '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
972           '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
973           '([ "Kill jobs" LilyPond-kill-jobs t])
974           '("-----")
975           '([ "SmartView" LilyPond-command-smartview t])
976           '([ "View" LilyPond-command-view t])
977           '([ "ViewPS" LilyPond-command-viewps t])
978           '("-----")
979           '([ "Midi (toggle)" LilyPond-command-current-midi t])
980           '([ "Midi all" LilyPond-command-all-midi t])
981           ))
982
983 ;;; LilyPond-mode-menu should not be interactive, via "M-x LilyPond-<Tab>"
984 (easy-menu-define LilyPond-mode-menu
985   LilyPond-mode-map
986   "Menu used in LilyPond mode."
987   (append '("LilyPond")
988           '(("Insert"
989              [ "\\notes..."  LilyPond-insert-tag-notes t]
990              [ "\\score..."  LilyPond-insert-tag-score t]
991              ["Quick Notes"  LilyPond-quick-note-insert t]
992              ["Autocompletion"   LilyPond-autocompletion t]
993              ))
994           '(("Miscellaneous"
995              ["(Un)comment Region" LilyPond-comment-region t]
996              ["Refontify buffer" font-lock-fontify-buffer t]
997              ))
998           '(("Info"
999              ["LilyPond" LilyPond-info t]
1000              ["LilyPond index-search" LilyPond-info-index-search t]
1001              ["Music Glossary" LilyPond-music-glossary-info t]
1002              ["LilyPond internals" LilyPond-internals-info t]
1003              ))
1004           ))
1005
1006 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z]+\\) *="
1007   "Regexp matching Identifier definitions.")
1008
1009 (defvar LilyPond-imenu-generic-expression
1010   (list (list nil LilyPond-imenu-generic-re 1))
1011   "Expression for imenu")
1012
1013 (defun LilyPond-command-select-master ()
1014   (interactive)
1015   (message "Next command will be on the master file")
1016   (setq LilyPond-command-current 'LilyPond-command-master))
1017
1018 (defun LilyPond-command-select-buffer ()
1019   (interactive)
1020   (message "Next command will be on the buffer")
1021   (setq LilyPond-command-current 'LilyPond-command-buffer))
1022
1023 (defun LilyPond-command-select-region ()
1024   (interactive)
1025   (message "Next command will be on the region")
1026   (setq LilyPond-command-current 'LilyPond-command-region))
1027
1028 (defun LilyPond-command-menu (name)
1029   ;; Execute LilyPond-command-alist NAME from a menu.
1030   (let ((LilyPond-command-force name))
1031     (if (eq LilyPond-command-current 'LilyPond-command-region)
1032         (if (eq (mark t) nil)
1033             (progn (message "The mark is not set now") (sit-for 0 500))
1034           (progn (if (not (not (LilyPond-mark-active)))
1035                      (progn (message "Region is not active, using region between inactive mark and current point.") (sit-for 0 500)))
1036                  (LilyPond-command-region (mark t) (point))))
1037       (funcall LilyPond-command-current))))
1038
1039 (defun LilyPond-mode ()
1040   "Major mode for editing LilyPond music files.
1041
1042 This mode knows about LilyPond keywords and line comments, not about
1043 indentation or block comments.  It features easy compilation, error
1044 finding and viewing of a LilyPond source buffer or region.
1045
1046 COMMANDS
1047 \\{LilyPond-mode-map}
1048 VARIABLES
1049
1050 LilyPond-command-alist\t\talist from name to command
1051 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
1052   (interactive)
1053   ;; set up local variables
1054   (kill-all-local-variables)
1055
1056   (make-local-variable 'font-lock-defaults)
1057   (setq font-lock-defaults '(LilyPond-font-lock-keywords))
1058
1059   ;; string and comments are fontified explicitly
1060   (make-local-variable 'font-lock-keywords-only)
1061   (setq font-lock-keywords-only t)
1062
1063   ;; Multi-line font-locking needs Emacs 21.1 or newer.
1064   ;; For older versions there is hotkey "C-c f".
1065   (make-local-variable 'font-lock-multiline) 
1066   (setq font-lock-multiline t) 
1067
1068   (make-local-variable 'paragraph-separate)
1069   (setq paragraph-separate "^[ \t]*$")
1070
1071   (make-local-variable 'paragraph-start)
1072   (setq paragraph-start "^[ \t]*$")
1073
1074   (make-local-variable 'comment-start)
1075   (setq comment-start "%")
1076
1077   (make-local-variable 'comment-start-skip)
1078   (setq comment-start-skip "%{? *")
1079
1080   (make-local-variable 'comment-end)
1081   (setq comment-end "")
1082
1083   (make-local-variable 'block-comment-start)
1084   (setq block-comment-start "%{")
1085
1086   (make-local-variable 'block-comment-end)  
1087   (setq block-comment-end   "%}")
1088
1089   (make-local-variable 'indent-line-function)
1090   (setq indent-line-function 'LilyPond-indent-line)
1091
1092   (set-syntax-table LilyPond-mode-syntax-table)
1093   (setq major-mode 'LilyPond-mode)
1094   (setq mode-name "LilyPond")
1095   (setq local-abbrev-table LilyPond-mode-abbrev-table)
1096   (use-local-map LilyPond-mode-map)
1097
1098   ;; In Emacs blink-...-on-screen needs to be declared.
1099   (if (not (string-match "XEmacs\\|Lucid" emacs-version))
1100       (progn
1101         (make-local-variable 'show-paren-mode)
1102         (setq show-paren-mode t)))
1103
1104   (if (not (string-match "XEmacs\\|Lucid" emacs-version))
1105       (progn
1106         (make-local-variable 'show-paren-function)
1107         ;;(setq show-paren-function 'LilyPond-show-paren-function) ; possible ?
1108         )
1109     (progn
1110       (make-local-variable 'paren-highlight)
1111       ;;(setq paren-highlight 'LilyPond-paren-highlight) ; possible ?
1112       ))
1113
1114   ;; In XEmacs imenu was synched up with: FSF 20.4
1115   (make-local-variable 'imenu-generic-expression)
1116   (setq imenu-generic-expression LilyPond-imenu-generic-expression)
1117   (imenu-add-to-menubar "Index")
1118
1119   ;; In XEmacs one needs to use 'easy-menu-add'.
1120   (if (string-match "XEmacs\\|Lucid" emacs-version)
1121       (progn
1122         (easy-menu-add LilyPond-mode-menu)
1123         (easy-menu-add LilyPond-command-menu)))
1124
1125   ;; Use Command on Region even for inactive mark (region).
1126   (if (string-match "XEmacs\\|Lucid" emacs-version)
1127       (setq zmacs-regions nil)
1128     (setq mark-even-if-inactive t))
1129
1130   ;; run the mode hook. LilyPond-mode-hook use is deprecated
1131   (run-hooks 'LilyPond-mode-hook))
1132
1133 (defun LilyPond-version ()
1134   "Echo the current version of `LilyPond-mode' in the minibuffer."
1135   (interactive)
1136   (message "Using `LilyPond-mode' version %s" LilyPond-version))
1137
1138 (load-library "lilypond-font-lock")
1139 (load-library "lilypond-indent")
1140
1141 (provide 'lilypond-mode)
1142 ;;; lilypond-mode.el ends here
1143