]> git.donarmstrong.com Git - lilypond.git/blob - elisp/lilypond-mode.el
s/splitted/split/
[lilypond.git] / elisp / lilypond-mode.el
1 ;;;; lilypond-mode.el -- Major mode for editing GNU LilyPond music scores
2 ;;;; This file is part of LilyPond, the GNU music typesetter.
3 ;;;;  
4 ;;;; Copyright (C) 1999--2011 Jan Nieuwenhuizen <janneke@gnu.org>
5 ;;;; Changed 2001--2003 Heikki Junes <heikki.junes@hut.fi>
6 ;;;;    * Add PS-compilation, PS-viewing and MIDI-play (29th Aug 2001)
7 ;;;;    * Keyboard shortcuts (12th Sep 2001)
8 ;;;;    * Inserting tags, inspired on sgml-mode (11th Oct 2001)
9 ;;;;    * Autocompletion & Info (23rd Nov 2002)
10 ;;;;
11 ;;;; LilyPond is free software: you can redistribute it and/or modify
12 ;;;; it under the terms of the GNU General Public License as published by
13 ;;;; the Free Software Foundation, either version 3 of the License, or
14 ;;;; (at your option) any later version.
15 ;;;;
16 ;;;; LilyPond is distributed in the hope that it will be useful,
17 ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19 ;;;; GNU General Public License for more details.
20 ;;;;
21 ;;;; You should have received a copy of the GNU General Public License
22 ;;;; along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
23
24
25 ;;; Inspired on auctex
26
27 ;;; Look lilypond-init.el or Documentation/topdocs/INSTALL.texi
28 ;;; for installing instructions.
29
30 (require 'easymenu)
31 (require 'compile)
32
33 (defconst LilyPond-version "2.5.20"
34   "`LilyPond-mode' version number.")
35
36 (defconst LilyPond-help-address "bug-lilypond@gnu.org"
37   "Address accepting submission of bug reports.")
38
39 (defvar LilyPond-mode-hook nil
40   "*Hook called by `LilyPond-mode'.")
41
42 (defvar LilyPond-region-file-prefix "emacs-lily"
43   "File prefix for commands on buffer or region.")
44
45 (defvar LilyPond-master-file nil
46   "Master file that LilyPond will be run on.")
47
48 ;; FIXME: find ``\score'' in buffers / make settable?
49 (defun LilyPond-get-master-file ()
50   (or LilyPond-master-file
51       (buffer-file-name)))
52
53 (defvar LilyPond-kick-xdvi nil
54   "If true, no simultaneous xdvi's are started, but reload signal is sent.")
55
56 (defvar LilyPond-command-history nil
57   "Command history list.")
58         
59 (defvar LilyPond-regexp-alist
60   '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
61   "Regexp used to match LilyPond errors.  See `compilation-error-regexp-alist'.")
62
63 (defvar LilyPond-imenu nil
64   "A flag to tell whether LilyPond-imenu is turned on.")
65 (make-variable-buffer-local 'LilyPond-imenu)
66
67 (defcustom LilyPond-include-path ".:/tmp"
68   "* LilyPond include path."
69   :type 'string
70   :group 'LilyPond)
71
72 (defun LilyPond-words-filename ()
73   "The file containing LilyPond \keywords \Identifiers and ReservedWords.
74 Finds file lilypond-words.el from load-path."
75   (let ((fn nil)
76         (lp load-path)
77         (words-file "lilypond-words.el"))
78     (while (and (> (length lp) 0) (not fn))
79       (setq fn (concat (car lp) "/" words-file))
80       (if (not (file-readable-p fn)) 
81           (progn (setq fn nil) (setq lp (cdr lp)))))
82     (if (not fn)
83         (progn (message "Warning: `lilypond-words.el' not found in `load-path'. See `lilypond-init.el'.")
84                (sit-for 5 0)))
85     fn))
86
87 (defun LilyPond-add-dictionary-word (x)
88   "Contains all words: \keywords \Identifiers and ReservedWords."
89   (nconc '(("" . 1)) x))
90
91 (if (> emacs-major-version 20)
92     (defun get-buffer-size (b) (buffer-size b))
93   (defun get-buffer-size (b)
94     (let (size (current-buffer (current-buffer)))
95       (set-buffer b)
96       (setq size (buffer-size))
97       (set-buffer current-buffer)
98       size
99     )))
100
101 ;; creates dictionary if empty
102 (if (and (eq (length (LilyPond-add-dictionary-word ())) 1)
103          (not (eq (LilyPond-words-filename) nil)))
104     (progn
105       (setq b (find-file-noselect (LilyPond-words-filename) t t))
106       (setq m (set-marker (make-marker) 1 (get-buffer b)))
107       (setq i 1)
108       (while (> (get-buffer-size b) (marker-position m))
109         (setq i (+ i 1))
110         (setq copy (copy-alist (list (eval (symbol-name (read m))))))
111         (setcdr copy i)
112         (LilyPond-add-dictionary-word (list copy)))
113       (kill-buffer b)))
114
115 (defvar LilyPond-insert-tag-current ""
116   "The last command selected from the LilyPond-Insert -menu.")
117
118 (defconst LilyPond-menu-keywords 
119   (let ((wordlist '())
120         (co (all-completions "" (LilyPond-add-dictionary-word ())))
121         (currword ""))
122     (progn
123       (while (> (length co) 0)
124         (setq currword (car co))
125         (if (string-equal "-" (car (setq co (cdr co))))
126             (progn
127               (add-to-list 'wordlist currword)
128               (while (and (> (length co) 0)
129                           (not (string-equal "-" (car (setq co (cdr co))))))))))
130       (reverse wordlist)))
131   "Keywords inserted from LilyPond-Insert-menu.")
132
133 (defconst LilyPond-keywords
134   (let ((wordlist '("\\score"))
135         (co (all-completions "" (LilyPond-add-dictionary-word ())))
136         (currword ""))
137     (progn
138       (while (> (length co) 0)
139         (setq currword (car co))
140         (if (> (length currword) 1)
141             (if (and (string-equal "\\" (substring currword 0 1))
142                      (string-match "[a-z-]+" currword)
143                      (= (match-beginning 0) 1)
144                      (= (match-end 0) (length currword))
145                      (not (string-equal "\\longa" currword))
146                      (not (string-equal "\\breve" currword))
147                      (not (string-equal "\\maxima" currword))
148                      (string-equal (downcase currword) currword))
149                 (add-to-list 'wordlist currword)))
150         (if (string-equal "-" (car (setq co (cdr co))))
151             (while (and (> (length co) 0)
152                         (not (string-equal "-" (car (setq co (cdr co)))))))))
153       (reverse wordlist)))
154   "LilyPond \\keywords")
155
156 (defconst LilyPond-identifiers 
157   (let ((wordlist '("\\voiceOne"))
158         (co (all-completions "" (LilyPond-add-dictionary-word ()))))
159     (progn
160       (while (> (length co) 0)
161         (setq currword (car co))
162         (if (> (length currword) 1)
163             (if (and (string-equal "\\" (substring currword 0 1))
164                      (string-match "[a-zA-Z-]+" currword)
165                      (= (match-beginning 0) 1)
166                      (= (match-end 0) (length currword))
167                      (not (string-equal (downcase currword) currword)))
168                 (add-to-list 'wordlist currword)))
169         (if (string-equal "-" (car (setq co (cdr co))))
170             (while (and (> (length co) 0)
171                         (not (string-equal "-" (car (setq co (cdr co)))))))))
172       (reverse wordlist)))
173   "LilyPond \\Identifiers")
174
175 (defconst LilyPond-Capitalized-Reserved-Words 
176   (let ((wordlist '("StaffContext"))
177         (co (all-completions "" (LilyPond-add-dictionary-word ()))))
178     (progn
179       (while (> (length co) 0)
180         (setq currword (car co))
181         (if (> (length currword) 0)
182             (if (and (string-match "[a-zA-Z_]+" currword)
183                      (= (match-beginning 0) 0)
184                      (= (match-end 0) (length currword))
185                      (not (string-equal (downcase currword) currword)))
186                 (add-to-list 'wordlist currword)))
187         (if (string-equal "-" (car (setq co (cdr co))))
188             (while (and (> (length co) 0)
189                         (not (string-equal "-" (car (setq co (cdr co)))))))))
190       (reverse wordlist)))
191   "LilyPond ReservedWords")
192
193 (defconst LilyPond-non-capitalized-reserved-words
194   (let ((wordlist '("cessess"))
195         (co (all-completions "" (LilyPond-add-dictionary-word ()))))
196     (progn
197       (while (> (length co) 0)
198         (setq currword (car co))
199         (if (> (length currword) 0)
200             (if (and (string-match "[a-z]+" currword)
201                      (= (match-beginning 0) 0)
202                      (= (match-end 0) (length currword))
203                      (string-equal (downcase currword) currword))
204                 (add-to-list 'wordlist currword)))
205         (if (string-equal "-" (car (setq co (cdr co))))
206             (while (and (> (length co) 0)
207                         (not (string-equal "-" (car (setq co (cdr co)))))))))
208       (reverse wordlist)))
209   "LilyPond notenames")
210
211 (defun LilyPond-check-files (derived originals extensions)
212   "Check that DERIVED is newer than any of the ORIGINALS.
213 Try each original with each member of EXTENSIONS, in all directories
214 in LilyPond-include-path."
215   (let ((found nil)
216         (regexp (concat "\\`\\("
217                         (mapconcat (function (lambda (dir)
218                                       (regexp-quote (expand-file-name dir))))
219                                    LilyPond-include-path "\\|")
220                         "\\).*\\("
221                         (mapconcat 'regexp-quote originals "\\|")
222                         "\\)\\.\\("
223                         (mapconcat 'regexp-quote extensions "\\|")
224                         "\\)\\'"))
225         (buffers (buffer-list)))
226     (while buffers
227       (let* ((buffer (car buffers))
228              (name (buffer-file-name buffer)))
229         (setq buffers (cdr buffers))
230         (if (and name (string-match regexp name))
231             (progn
232               (and (buffer-modified-p buffer)
233                    (or (not LilyPond-save-query)
234                        (y-or-n-p (concat "Save file "
235                                          (buffer-file-name buffer)
236                                          "? ")))
237                    (save-excursion (set-buffer buffer) (save-buffer)))
238               (if (file-newer-than-file-p name derived)
239                   (setq found t))))))
240     found))
241
242 (defun LilyPond-running ()
243   "Check the currently running LilyPond compiling jobs."
244   (let ((process-names (list "lilypond" "tex" "2ps" "2midi"
245                              "book" "latex"))
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-midi-running ()
255   "Check the currently running Midi processes."
256   (let ((process-names (list "midi" "midiall"))
257         (running nil))
258     (while (setq process-name (pop process-names))
259       (setq process (get-process process-name))
260       (if (and process 
261                (eq (process-status process) 'run))
262           (push process-name running)))
263     running)) ; return the running jobs
264
265 (defun LilyPond-kill-jobs ()
266   "Kill the currently running LilyPond compiling jobs."
267   (interactive)
268   (let ((process-names (LilyPond-running))
269         (killed nil))
270     (while (setq process-name (pop process-names))
271       (quit-process (get-process process-name) t)
272       (push process-name killed))
273     killed)) ; return the killed jobs
274
275 (defun LilyPond-kill-midi ()
276   "Kill the currently running midi processes."
277   (let ((process-names (LilyPond-midi-running))
278         (killed nil))
279     (while (setq process-name (pop process-names))
280       (quit-process (get-process process-name) t)
281       (push process-name killed))
282     killed)) ; return the killed jobs
283
284 ;; URG, should only run LilyPond-compile for LilyPond
285 ;; not for tex,xdvi (lilypond?)
286 (defun LilyPond-compile-file (command name)
287   ;; We maybe should know what we run here (Lily, lilypond, tex)
288   ;; and adjust our error-matching regex ?
289   (compile-internal
290    (if (eq LilyPond-command-current 'LilyPond-command-master)
291        command
292      ;; use temporary directory for Commands on Buffer/Region
293      ;; hm.. the directory is set twice, first to default-dir
294      (concat "cd " (LilyPond-temp-directory) "; " command))
295    "No more errors" name))
296
297 ;; do we still need this, now that we're using compile-internal?
298 (defun LilyPond-save-buffer ()
299   "Save buffer and set default command for compiling."
300   (interactive)
301   (if (buffer-modified-p)
302       (progn (save-buffer)
303              (setq LilyPond-command-next LilyPond-command-default))))
304
305 ;;; return (dir base ext)
306 (defun split-file-name (name)
307   (let* ((i (string-match "[^/]*$" name))
308          (dir (if (> i 0) (substring name 0 i) "./"))
309          (file (substring name i (length name)))
310          (i (string-match "[^.]*$" file)))
311     (if (and
312          (> i 0)
313          (< i (length file)))
314         (list dir (substring file 0 (- i 1)) (substring file i (length file)))
315       (list dir file ""))))
316
317
318 ;; Should check whether in command-alist?
319 (defcustom LilyPond-command-default "LilyPond"
320   "Default command. Must identify a member of LilyPond-command-alist."
321
322   :group 'LilyPond
323   :type 'string)
324 ;;;(make-variable-buffer-local 'LilyPond-command-last)
325
326 (defvar LilyPond-command-next LilyPond-command-default)
327
328 (defvar LilyPond-command-current 'LilyPond-command-master)
329 ;;;(make-variable-buffer-local 'LilyPond-command-master)
330
331
332 ;; If non-nil, LilyPond-command-query will return the value of this
333 ;; variable instead of quering the user. 
334 (defvar LilyPond-command-force nil)
335
336 (defcustom LilyPond-lilypond-command "lilypond"
337   "Command used to compile LY files."
338   :group 'LilyPond
339   :type 'string)
340
341 (defcustom LilyPond-ps-command "gv --watch"
342   "Command used to display PS files."
343
344   :group 'LilyPond
345   :type 'string)
346
347 (defcustom LilyPond-pdf-command "xpdf"
348   "Command used to display PDF files."
349
350   :group 'LilyPond
351   :type 'string)
352
353 (defcustom LilyPond-midi-command "timidity"
354   "Command used to play MIDI files."
355
356   :group 'LilyPond
357   :type 'string)
358
359 (defcustom LilyPond-all-midi-command "timidity -ia"
360   "Command used to play MIDI files."
361
362   :group 'LilyPond
363   :type 'string)
364
365 (defun LilyPond-command-current-midi ()
366   "Play midi corresponding to the current document."
367   (interactive)
368   (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-get-master-file))
369
370 (defun LilyPond-command-all-midi ()
371   "Play midi corresponding to the current document."
372   (interactive)
373   (LilyPond-command (LilyPond-command-menu "MidiAll") 'LilyPond-get-master-file))
374
375 (defun count-matches-as-number (re)
376   "Count-matches in emacs 22 backwards-incompatibly returns a number"
377   (let ((result (count-matches re)))
378     (if (stringp result)
379         (string-to-number result)
380       result)))
381     
382 (defun count-rexp (start end rexp)
383   "Print number of found regular expressions in the region."
384   (interactive "r")
385   (save-excursion
386     (save-restriction
387       (narrow-to-region start end)
388       (goto-char (point-min))
389       (count-matches-as-number rexp))))
390
391 (defun count-midi-words ()
392   "Check number of midi-scores before the curser."
393   (if (eq LilyPond-command-current 'LilyPond-command-region)
394       (count-rexp (mark t) (point) "\\\\midi")
395     (count-rexp (point-min) (point-max) "\\\\midi")))
396  
397 (defun count-midi-words-backwards ()
398   "Check number of midi-scores before the curser."
399   (if (eq LilyPond-command-current 'LilyPond-command-region)
400       (count-rexp (mark t) (point) "\\\\midi")
401     (count-rexp (point-min) (point) "\\\\midi")))
402  
403 (defun LilyPond-string-current-midi ()
404   "Check the midi file of the following midi-score in the current document."
405   (let ((fnameprefix (if (eq LilyPond-command-current 'LilyPond-command-master)
406                          (substring (LilyPond-get-master-file) 0 -3); suppose ".ly"
407                        LilyPond-region-file-prefix))
408         (allcount (count-midi-words))
409         (count (count-midi-words-backwards)))
410     (concat  fnameprefix
411              (if (and (> allcount 1) (> count 0)) ; not first score
412                  (if (eq count allcount)          ; last score
413                      (concat "-" (number-to-string (+ count -1)))
414                    (concat "-" (number-to-string count))))
415              ".midi")))
416
417 (defun LilyPond-string-all-midi ()
418   "Return the midi files of the current document in ascending order."
419   (let ((fnameprefix (if (eq LilyPond-command-current 'LilyPond-command-master)
420                          (substring (LilyPond-get-master-file) 0 -3); suppose ".ly"
421                        LilyPond-region-file-prefix))
422         (allcount (count-midi-words)))
423     (concat (if (> allcount 0)  ; at least one midi-score
424                 (concat fnameprefix ".midi "))
425             (if (> allcount 1)  ; more than one midi-score
426                 (concat fnameprefix "-[1-9].midi "))
427             (if (> allcount 9)  ; etc.
428                 (concat fnameprefix "-[1-9][0-9].midi"))
429             (if (> allcount 99) ; not first score
430                 (concat fnameprefix "-[1-9][0-9][0-9].midi")))))
431
432 ;; This is the major configuration variable.
433 (defcustom LilyPond-command-alist
434   ;; Should expand this to include possible keyboard shortcuts which
435   ;; could then be mapped to define-key and menu.
436   `(
437     ("LilyPond" . (,(concat LilyPond-lilypond-command " %s") "%s" "%l" "View"))
438     ("2PS" . (,(concat LilyPond-lilypond-command " -f ps %s") "%s" "%p" "ViewPS"))
439     ("2Gnome" . (,(concat LilyPond-lilypond-command " -b gnome %s")))
440
441     ("Book" . ("lilypond-book %x" "%x" "%l" "LaTeX"))
442     ("LaTeX" . ("latex '\\nonstopmode\\input %l'" "%l" "%d" "ViewDVI"))
443
444     ;; refreshes when kicked USR1
445     ("View" . (,(concat LilyPond-pdf-command " %f")))
446     ("ViewPDF" . (,(concat LilyPond-pdf-command " %f")))
447     ("ViewPS" . (,(concat LilyPond-ps-command " %p")))
448
449     ;; The following are refreshed in LilyPond-command:
450     ;; - current-midi depends on cursor position and
451     ("Midi" . ("")) ; 
452     ;; - all-midi depends on number of midi-score.
453     ("MidiAll" . (""))
454     )
455
456   "AList of commands to execute on the current document.
457
458 The key is the name of the command as it will be presented to the
459 user, the value is a cons of the command string handed to the shell
460 after being expanded, and the next command to be executed upon
461 success.  The expansion is done using the information found in
462 LilyPond-expand-list.
463 "
464   :group 'LilyPond
465   :type '(repeat (cons :tag "Command Item"
466                        (string :tag "Key")
467                        (cons :tag "How"
468                         (string :tag "Command")
469                         (string :tag "Next Key")))))
470
471 ;; drop this?
472 (defcustom LilyPond-file-extension ".ly"
473   "*File extension used in LilyPond sources."
474   :group 'LilyPond
475   :type 'string)
476
477
478 (defcustom LilyPond-expand-alist 
479   '(
480     ("%s" . ".ly")
481     ("%t" . ".tex")
482     ("%d" . ".dvi")
483     ("%f" . ".pdf")
484     ("%p" . ".ps")
485     ("%l" . ".tex")
486     ("%x" . ".tely")
487     ("%m" . ".midi")
488     )
489     
490   "Alist of expansion strings for LilyPond command names."
491   :group 'LilyPond
492   :type '(repeat (cons :tag "Alist item"
493                   (string :tag "Symbol")
494                   (string :tag "Expansion")))) 
495
496
497 (defcustom LilyPond-command-Show "View"
498   "*The default command to show (view or print) a LilyPond file.
499 Must be the car of an entry in `LilyPond-command-alist'."
500   :group 'LilyPond
501   :type 'string)
502   (make-variable-buffer-local 'LilyPond-command-Show)
503
504 (defcustom LilyPond-command-Print "Print"
505   "The name of the Print entry in LilyPond-command-Print."
506   :group 'LilyPond
507   :type 'string)
508
509 (defun LilyPond-find-required-command (command file)
510   "Find the first command in the chain that is needed to run
511  (input file is newer than the output file)"
512   (let* ((entry (cdr (assoc command LilyPond-command-alist)))
513         (next-command (nth 3 entry)))
514     (if (null next-command)
515         command
516       (let* ((src-string (nth 1 entry))
517             (input (LilyPond-command-expand src-string file))
518             (output (LilyPond-command-expand (nth 2 entry) file)))
519         (if (or (file-newer-than-file-p input output)
520                 (and (equal "%s" src-string)
521                      (not (equal (buffer-name) file))
522                      (file-newer-than-file-p (buffer-name)
523                                              output)))
524             command
525           (LilyPond-find-required-command next-command file))))))
526
527 (defun LilyPond-command-query (name)
528   "Query the user for what LilyPond command to use."
529   (cond ((string-equal name LilyPond-region-file-prefix)
530          (LilyPond-check-files (concat name ".tex")
531                                (list name)
532                                (list LilyPond-file-extension)))
533         ((verify-visited-file-modtime (current-buffer))
534          (and (buffer-modified-p)
535               (y-or-n-p "Save buffer before next command? ")
536               (LilyPond-save-buffer)))
537         ((y-or-n-p "The command will be invoked to an already saved buffer. Revert it? ")
538          (revert-buffer t t)))
539     
540   (let* ((default (LilyPond-find-required-command LilyPond-command-next name))
541          (completion-ignore-case t)
542          (answer (or LilyPond-command-force
543                      (completing-read
544                       (concat "Command: (default " default ") ")
545                       LilyPond-command-alist nil t nil 'LilyPond-command-history))))
546
547     ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
548     (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
549       (if (and answer
550                (not (string-equal answer "")))
551           answer
552         default))))
553
554 (defun LilyPond-command-master ()
555   "Run command on the current document."
556   (interactive)
557   (LilyPond-command-select-master)
558   (LilyPond-command (LilyPond-command-query (LilyPond-get-master-file))
559                     'LilyPond-get-master-file))
560
561 (defun LilyPond-command-lilypond ()
562   "Run lilypond for the current document."
563   (interactive)
564   (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-get-master-file)
565 )
566
567 (defun LilyPond-command-formatps ()
568   "Format the ps output of the current document."
569   (interactive)
570   (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-get-master-file)
571 )
572
573 (defun LilyPond-command-formatgnome ()
574   "Format the gnome output of the current document."
575   (interactive)
576   (LilyPond-command (LilyPond-command-menu "2Gnome") 'LilyPond-get-master-file))
577
578 (defun LilyPond-command-formatmidi ()
579   "Format the midi output of the current document."
580   (interactive)
581   (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-get-master-file))
582
583 (defun LilyPond-command-view ()
584   "View the output of current document."
585   (interactive)
586   (LilyPond-command-viewpdf))
587
588 (defun LilyPond-command-viewpdf ()
589   "View the ps output of current document."
590   (interactive)
591   (LilyPond-command (LilyPond-command-menu "ViewPDF") 'LilyPond-get-master-file))
592
593 (defun LilyPond-command-viewps ()
594   "View the ps output of current document."
595   (interactive)
596   (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-get-master-file))
597
598 ;; FIXME, this is broken
599 (defun LilyPond-region-file (begin end)
600   (let (
601         ;; (dir "./")
602         (dir (LilyPond-temp-directory))
603         (base LilyPond-region-file-prefix)
604         (ext LilyPond-file-extension))
605     (concat dir base ext)))
606
607 ;;; Commands on Region work if there is an appropriate '\score'.
608 (defun LilyPond-command-region (begin end)
609   "Run LilyPond on the current region."
610   (interactive "r")
611   (if (or (> begin (point-min)) (< end (point-max)))
612       (LilyPond-command-select-region))
613   (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
614   (LilyPond-command (LilyPond-command-query
615                      (LilyPond-region-file begin end))
616                     '(lambda () (LilyPond-region-file begin end)))
617   ;; Region may deactivate even if buffer was intact, reactivate?
618   ;; Currently, also deactived regions are used.
619   )
620
621 (defun LilyPond-command-buffer ()
622   "Run LilyPond on buffer."
623   (interactive)
624   (LilyPond-command-select-buffer)
625   (LilyPond-command-region (point-min) (point-max)))
626
627 (defun LilyPond-command-expand (string file)
628   (let ((case-fold-search nil))
629     (if (string-match "%" string)
630         (let* ((b (match-beginning 0))
631                (e (+ b 2))
632                (l (split-file-name file))
633                (dir (car l))
634                (base (cadr l)))
635           (LilyPond-command-expand
636            (concat (substring string 0 b)
637                    (shell-quote-argument (concat dir base))
638                    (let ((entry (assoc (substring string b e)
639                                        LilyPond-expand-alist)))
640                      (if entry (cdr entry) ""))
641                    (substring string e))
642            file))
643       string)))
644
645 (defun LilyPond-shell-process (name buffer command)
646   (let ((old (current-buffer)))
647     (switch-to-buffer-other-window buffer)
648     ;; If we empty the buffer don't see messages scroll by.
649     ;; (erase-buffer)
650     
651     (start-process-shell-command name buffer command)
652     (switch-to-buffer-other-window old)))
653   
654
655 (defun LilyPond-command (name file)
656   "Run command NAME on the file you get by calling FILE.
657
658 FILE is a function return a file name.  It has one optional argument,
659 the extension to use on the file.
660
661 Use the information in LilyPond-command-alist to determine how to run the
662 command."
663
664   (let ((entry (assoc name LilyPond-command-alist)))
665     (if entry
666         (let ((command (LilyPond-command-expand (cadr entry)
667                                                 (apply file nil)))
668               (jobs nil)
669               (job-string "no jobs"))
670           (if (member name (list "View" "ViewPS"))
671               ;; is USR1 a right signal for viewps?
672               (let ((buffer-xdvi (get-buffer-create (concat "*" name "*"))))
673                 ;; what if XEDITOR is set to gedit or so, should we steal it?
674                 (if (not (getenv "XEDITOR"))
675                     (setenv "XEDITOR" "emacsclient --no-wait +%l:%c %f"))
676                 (if LilyPond-kick-xdvi
677                   (let ((process-xdvi (get-buffer-process buffer-xdvi)))
678                     (if process-xdvi
679                         (signal-process (process-id process-xdvi) 'SIGUSR1)
680                       (LilyPond-shell-process name buffer-xdvi command)))
681                   (LilyPond-shell-process name buffer-xdvi command)))
682             (progn
683               (if (string-equal name "Midi")
684                   (progn
685                     (setq command (concat LilyPond-midi-command " " (LilyPond-string-current-midi)))
686                     (if (LilyPond-kill-midi)
687                         (setq job-string nil)))) ; either stop or start playing
688               (if (string-equal name "MidiAll")
689                   (progn
690                     (setq command (concat LilyPond-all-midi-command " " (LilyPond-string-all-midi)))
691                     (LilyPond-kill-midi))) ; stop and start playing
692               (if (and (member name (list "Midi" "MidiAll")) job-string)
693                   (if (file-newer-than-file-p
694                        (LilyPond-get-master-file)
695                        (concat (substring (LilyPond-get-master-file) 0 -3) ".midi"))
696                       (if (y-or-n-p "Midi older than source. Reformat midi?")
697                           (progn
698                             (LilyPond-command-formatmidi)
699                             (while (LilyPond-running)
700                               (message "Starts playing midi once it is built.")
701                               (sit-for 0 100))))))
702               (if (member name (list "LilyPond" "TeX" "2Midi" "2PS"
703                                      "Book" "LaTeX"))
704                   (if (setq jobs (LilyPond-running))
705                       (progn
706                         (setq job-string "Process") ; could also suggest compiling after process has ended
707                         (while jobs
708                           (setq job-string (concat job-string " \"" (pop jobs) "\"")))
709                         (setq job-string (concat job-string " is already running; kill it to proceed "))
710                         (if (y-or-n-p job-string)
711                             (progn
712                               (setq job-string "no jobs")
713                               (LilyPond-kill-jobs)
714                               (while (LilyPond-running)
715                                 (sit-for 0 100)))
716                           (setq job-string nil)))))
717
718               (setq LilyPond-command-next
719                     (let* ((entry (assoc name LilyPond-command-alist))
720                            (next-command (nth 3 (cdr entry))))
721                       (or next-command
722                           LilyPond-command-default)))
723               
724               (if (string-equal job-string "no jobs")
725                   (LilyPond-compile-file command name))))))))
726           
727 (defun LilyPond-mark-active ()
728   "Check if there is an active mark."
729   (and transient-mark-mode
730        (if (string-match "XEmacs\\|Lucid" emacs-version) (mark) mark-active)))
731
732 (defun LilyPond-temp-directory ()
733   "Temporary file directory for Commands on Region."
734   (interactive)
735   (if (string-match "XEmacs\\|Lucid" emacs-version)
736       (concat (temp-directory) "/")
737     temporary-file-directory))
738
739 ;;; Keymap
740
741 (defvar LilyPond-mode-map ()
742   "Keymap used in `LilyPond-mode' buffers.")
743
744 ;; Note:  if you make changes to the map, you must do
745 ;;    M-x set-variable LilyPond-mode-map nil
746 ;;    M-x eval-buffer
747 ;;    M-x LilyPond-mode
748 ;; to let the changest take effect
749
750 (if LilyPond-mode-map
751     ()
752   (setq LilyPond-mode-map (make-sparse-keymap))
753   ;; Put keys to LilyPond-command-alist and fetch them from there somehow.
754   (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
755   (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
756   (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
757   (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-jobs)
758   (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
759   (define-key LilyPond-mode-map "\C-cm" 'LilyPond-command-formatmidi)
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 overridden 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 [(shift 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           '([ "2PS" LilyPond-command-formatps t])
979           '([ "2Midi" LilyPond-command-formatmidi t])
980           '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-get-master-file) ])
981           '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-get-master-file) ])
982           '([ "Kill jobs" LilyPond-kill-jobs t])
983           '("-----")
984           '([ "View" LilyPond-command-view t])
985           '([ "ViewPS" LilyPond-command-viewps t])
986           '("-----")
987           '([ "Midi (toggle)" LilyPond-command-current-midi t])
988           '([ "Midi all" LilyPond-command-all-midi t])
989           ))
990
991 (defun LilyPond-menu-keywords-item (arg)
992   "Make vector for LilyPond-mode-keywords."
993   (vector arg (list 'LilyPond-insert-tag-current arg) :style 'radio :selected (list 'eq 'LilyPond-insert-tag-current arg)))
994
995 (defun LilyPond-menu-keywords ()
996   "Make Insert Tag menu. 
997
998 The Insert Tag -menu is split into parts if it is long enough."
999
1000   (let ((li (mapcar 'LilyPond-menu-keywords-item LilyPond-menu-keywords))
1001         (w (round (sqrt (length LilyPond-menu-keywords))))
1002         (split '())
1003         (imin 0) imax lw rw)
1004     (while (< imin (length LilyPond-menu-keywords))
1005       (setq imax (- (min (+ imin w) (length LilyPond-menu-keywords)) 1))
1006       (setq lw (nth imin LilyPond-menu-keywords)) 
1007       (setq rw (nth imax LilyPond-menu-keywords))
1008       (add-to-list 'split
1009          (let ((l (list (concat (substring lw 0 (min 7 (length lw))) 
1010                                 " ... " 
1011                                 (substring rw 0 (min 7 (length rw)))))))
1012            (while (<= imin imax)
1013              (add-to-list 'l (nth imin li))
1014              (setq imin (1+ imin)))
1015            (reverse l))))
1016     (if (> (length LilyPond-menu-keywords) 12) (reverse split) li)))
1017
1018 ;;; LilyPond-mode-menu should not be interactive, via "M-x LilyPond-<Tab>"
1019 (easy-menu-define LilyPond-mode-menu
1020   LilyPond-mode-map
1021   "Menu used in LilyPond mode."
1022   (append '("LilyPond")
1023           '(["Add index menu" LilyPond-add-imenu-menu])
1024           (list (cons "Insert tag" 
1025                 (cons ["Previously selected" LilyPond-insert-tag-current t] 
1026                 (cons "-----"
1027                       (LilyPond-menu-keywords)))))
1028           '(("Miscellaneous"
1029              ["Autocompletion"   LilyPond-autocompletion t]
1030              ["(Un)comment Region" LilyPond-comment-region t]
1031              ["Refontify buffer" font-lock-fontify-buffer t]
1032              "-----"
1033              ["Quick Insert Mode"  LilyPond-quick-insert-mode :keys "C-c q"]
1034              ))
1035           '(("Info"
1036              ["LilyPond" LilyPond-info t]
1037              ["LilyPond index-search" LilyPond-info-index-search t]
1038              ["Music Glossary" LilyPond-music-glossary-info t]
1039              ["LilyPond internals" LilyPond-internals-info t]
1040              ))
1041           ))
1042
1043 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z]+\\) *="
1044   "Regexp matching Identifier definitions.")
1045
1046 (defvar LilyPond-imenu-generic-expression
1047   (list (list nil LilyPond-imenu-generic-re 1))
1048   "Expression for imenu")
1049
1050 (defun LilyPond-command-select-master ()
1051   (interactive)
1052   (message "Next command will be on the master file")
1053   (setq LilyPond-command-current 'LilyPond-command-master))
1054
1055 (defun LilyPond-command-select-buffer ()
1056   (interactive)
1057   (message "Next command will be on the buffer")
1058   (setq LilyPond-command-current 'LilyPond-command-buffer))
1059
1060 (defun LilyPond-command-select-region ()
1061   (interactive)
1062   (message "Next command will be on the region")
1063   (setq LilyPond-command-current 'LilyPond-command-region))
1064
1065 (defun LilyPond-command-menu (name)
1066   ;; Execute LilyPond-command-alist NAME from a menu.
1067   (let ((LilyPond-command-force name))
1068     (if (eq LilyPond-command-current 'LilyPond-command-region)
1069         (if (eq (mark t) nil)
1070             (progn (message "The mark is not set now") (sit-for 0 500))
1071           (progn (if (not (not (LilyPond-mark-active)))
1072                      (progn (message "Region is not active, using region between inactive mark and current point.") (sit-for 0 500)))
1073                  (LilyPond-command-region (mark t) (point))))
1074       (funcall LilyPond-command-current))))
1075
1076 (defun LilyPond-add-imenu-menu ()
1077   (interactive)
1078   "Add an imenu menu to the menubar."
1079   (if (not LilyPond-imenu)
1080       (progn
1081         (imenu-add-to-menubar "Index")
1082         (redraw-frame (selected-frame))
1083         (setq LilyPond-imenu t))
1084     (message "%s" "LilyPond-imenu already exists.")))
1085 (put 'LilyPond-add-imenu-menu 'menu-enable '(not LilyPond-imenu))
1086
1087 (defun LilyPond-mode ()
1088   "Major mode for editing LilyPond music files.
1089
1090 This mode knows about LilyPond keywords and line comments, not about
1091 indentation or block comments.  It features easy compilation, error
1092 finding and viewing of a LilyPond source buffer or region.
1093
1094 COMMANDS
1095 \\{LilyPond-mode-map}
1096 VARIABLES
1097
1098 LilyPond-command-alist\t\talist from name to command"
1099   (interactive)
1100   ;; set up local variables
1101   (kill-all-local-variables)
1102
1103   (make-local-variable 'font-lock-defaults)
1104   (setq font-lock-defaults '(LilyPond-font-lock-keywords))
1105
1106   ;; string and comments are fontified explicitly
1107   (make-local-variable 'font-lock-keywords-only)
1108   (setq font-lock-keywords-only t)
1109
1110   ;; Multi-line font-locking needs Emacs 21.1 or newer.
1111   ;; For older versions there is hotkey "C-c f".
1112   (make-local-variable 'font-lock-multiline) 
1113   (setq font-lock-multiline t) 
1114
1115   (make-local-variable 'paragraph-separate)
1116   (setq paragraph-separate "^[ \t]*$")
1117
1118   (make-local-variable 'paragraph-start)
1119   (setq paragraph-start "^[ \t]*$")
1120
1121   (make-local-variable 'comment-start)
1122   (setq comment-start "%")
1123
1124   (make-local-variable 'comment-start-skip)
1125   (setq comment-start-skip "%{? *")
1126
1127   (make-local-variable 'comment-end)
1128   (setq comment-end "")
1129
1130   (make-local-variable 'block-comment-start)
1131   (setq block-comment-start "%{")
1132
1133   (make-local-variable 'block-comment-end)  
1134   (setq block-comment-end   "%}")
1135
1136   (make-local-variable 'indent-line-function)
1137   (setq indent-line-function 'LilyPond-indent-line)
1138
1139   (LilyPond-mode-set-syntax-table '(?\< ?\> ?\{ ?\}))
1140   (setq major-mode 'LilyPond-mode)
1141   (setq mode-name "LilyPond")
1142   (setq local-abbrev-table LilyPond-mode-abbrev-table)
1143   (use-local-map LilyPond-mode-map)
1144
1145   ;; In XEmacs imenu was synched up with: FSF 20.4
1146   (make-local-variable 'imenu-generic-expression)
1147   (setq imenu-generic-expression LilyPond-imenu-generic-expression)
1148   ;; (imenu-add-to-menubar "Index") ; see LilyPond-add-imenu-menu
1149
1150   ;; In XEmacs one needs to use 'easy-menu-add'.
1151   (if (string-match "XEmacs\\|Lucid" emacs-version)
1152       (progn
1153         (easy-menu-add LilyPond-mode-menu)
1154         (easy-menu-add LilyPond-command-menu)))
1155
1156   ;; Use Command on Region even for inactive mark (region).
1157   (if (string-match "XEmacs\\|Lucid" emacs-version)
1158       (progn
1159         (setq zmacs-regions nil)
1160         (make-local-hook 'post-command-hook)) ; XEmacs requires
1161     (setq mark-even-if-inactive t))
1162
1163   ;; Context dependent syntax tables in LilyPond-mode
1164   (add-hook 'post-command-hook 'LilyPond-mode-context-set-syntax-table nil t)
1165
1166   ;; Turn on paren-mode buffer-locally, i.e., in LilyPond-mode
1167   (if (string-match "XEmacs\\|Lucid" emacs-version)
1168       (progn
1169         (make-local-variable 'paren-mode)
1170         (paren-set-mode 'paren)
1171         (make-local-variable 'blink-matching-paren)
1172         (setq blink-matching-paren t)
1173         )
1174     (progn
1175       (make-local-variable 'blink-matching-paren-on-screen)
1176       (setq blink-matching-paren-on-screen t)
1177      ))
1178
1179   ;; run the mode hook. LilyPond-mode-hook use is deprecated
1180   (run-hooks 'LilyPond-mode-hook))
1181
1182 (defun LilyPond-version ()
1183   "Echo the current version of `LilyPond-mode' in the minibuffer."
1184   (interactive)
1185   (message "Using `LilyPond-mode' version %s" LilyPond-version))
1186
1187 (load-library "lilypond-font-lock")
1188 (load-library "lilypond-indent")
1189 (load-library "lilypond-what-beat")
1190
1191 (defun LilyPond-guile ()
1192   (interactive)
1193   (require 'ilisp)
1194   (guile "lilyguile" (LilyPond-command-expand (cadr (assoc "LilyPond" LilyPond-command-alist))
1195                                               (funcall 'LilyPond-get-master-file)))
1196   (comint-default-send (ilisp-process) "(define-module (*anonymous-ly-0*))")
1197   (comint-default-send (ilisp-process) "(set! %load-path (cons \"/usr/share/ilisp/\" %load-path))")
1198   (comint-default-send (ilisp-process) "(use-modules (guile-user) (guile-ilisp))")
1199   (comint-default-send (ilisp-process) "(newline)"))
1200
1201 (provide 'lilypond-mode)
1202 ;;; lilypond-mode.el ends here
1203