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