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