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