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