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