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