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