]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-mode.el
release commit
[lilypond.git] / lilypond-mode.el
1 ;;;
2 ;;; lilypond-mode.el --- Major mode for editing GNU LilyPond music scores
3 ;;;
4 ;;; source file of the GNU LilyPond music typesetter
5 ;;;  
6 ;;; (c) 1999--2001 Jan Nieuwenhuizen <janneke@gnu.org>
7 ;;; 
8 ;;; Changed 2001 Heikki Junes <heikki.junes@hut.fi>
9 ;;;    * Add PS-compilation, PS-viewing and MIDI-play (29th Aug 2001)
10 ;;;    * Keyboard shortcuts (12th Sep 2001)
11 ;;;    * Inserting tags, inspired on sgml-mode (11th Oct 2001)
12
13 ;;; Inspired on auctex
14
15 ;;;
16 ;;; Add this to your ~/.emacs or ~/.emacs.el
17 ;;;     (load-library "lilypond-mode.el")
18 ;;;     (setq auto-mode-alist
19 ;;;      (append '(("\\.ly$" . LilyPond-mode) auto-mode-alist)))
20 ;;; 
21
22 (load-library "lilypond-font-lock")
23 (load-library "lilypond-indent")
24
25 (require 'easymenu)
26 (require 'compile)
27
28 (defconst LilyPond-version "1.5.58"
29   "`LilyPond-mode' version number.")
30
31 (defconst LilyPond-help-address "bug-lilypond@gnu.org"
32   "Address accepting submission of bug reports.")
33
34 (defvar LilyPond-mode-hook nil
35   "*Hook called by `LilyPond-mode'.")
36
37 (defvar LilyPond-kick-xdvi nil
38   "If true, no simultaneous xdvi's are started, but reload signal is sent.")
39
40 (defvar LilyPond-command-history nil
41   "Command history list.")
42         
43 (defvar LilyPond-regexp-alist
44   '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
45   "Regexp used to match LilyPond errors.  See `compilation-error-regexp-alist'.")
46
47 (defcustom LilyPond-include-path ".:/tmp"
48   "* LilyPond include path."
49   :type 'string
50   :group 'LilyPond)
51
52
53 (defun LilyPond-check-files (derived originals extensions)
54   "Check that DERIVED is newer than any of the ORIGINALS.
55 Try each original with each member of EXTENSIONS, in all directories
56 in LilyPond-include-path."
57   (let ((found nil)
58         (regexp (concat "\\`\\("
59                         (mapconcat (function (lambda (dir)
60                                       (regexp-quote (expand-file-name dir))))
61                                    LilyPond-include-path "\\|")
62                         "\\).*\\("
63                         (mapconcat 'regexp-quote originals "\\|")
64                         "\\)\\.\\("
65                         (mapconcat 'regexp-quote extensions "\\|")
66                         "\\)\\'"))
67         (buffers (buffer-list)))
68     (while buffers
69       (let* ((buffer (car buffers))
70              (name (buffer-file-name buffer)))
71         (setq buffers (cdr buffers))
72         (if (and name (string-match regexp name))
73             (progn
74               (and (buffer-modified-p buffer)
75                    (or (not LilyPond-save-query)
76                        (y-or-n-p (concat "Save file "
77                                          (buffer-file-name buffer)
78                                          "? ")))
79                    (save-excursion (set-buffer buffer) (save-buffer)))
80               (if (file-newer-than-file-p name derived)
81                   (setq found t))))))
82     found))
83
84 (defun LilyPond-running ()
85   (let ((process (get-process "lilypond")))
86   (and process
87        (eq (process-status process) 'run))))
88
89 (defun Midi-running ()
90   (let ((process (get-process "midi")))
91   (and process
92        (eq (process-status process) 'run))))
93
94 (defun LilyPond-kill-job ()
95   "Kill the currently running LilyPond job."
96   (interactive)
97   ;; What bout TeX, Xdvi?
98   (quit-process (get-process "lilypond") t))
99
100 ;; URG, should only run LilyPond-compile for LilyPond
101 ;; not for tex,xdvi (ly2dvi?)
102 (defun LilyPond-compile-file (command name)
103   ;; We maybe should know what we run here (Lily, ly2dvi, tex)
104   ;; and adjust our error-matching regex ?
105   (compile-internal command "No more errors" name ))
106
107 ;; do we still need this, now that we're using compile-internal?
108 (defun LilyPond-save-buffer ()
109   (if (buffer-modified-p) (save-buffer)))
110
111 ;;; return (dir base ext)
112 (defun split-file-name (name)
113   (let* ((i (string-match "[^/]*$" name))
114          (dir (if (> i 0) (substring name 0 i) "./"))
115          (file (substring name i (length name)))
116          (i (string-match "[^.]*$" file)))
117     (if (and
118          (> i 0)
119          (< i (length file)))
120         (list dir (substring file 0 (- i 1)) (substring file i (length file)))
121       (list dir file ""))))
122
123
124 ;; Should check whether in command-alist?
125 (defcustom LilyPond-command-default "LilyPond"
126   "Default command. Must identify a member of LilyPond-command-alist."
127
128   :group 'LilyPond
129   :type 'string)
130 ;;;(make-variable-buffer-local 'LilyPond-command-last)
131
132 (defvar LilyPond-command-current 'LilyPond-command-master)
133 ;;;(make-variable-buffer-local 'LilyPond-command-master)
134
135
136 ;; If non-nil, LilyPond-command-query will return the value of this
137 ;; variable instead of quering the user. 
138 (defvar LilyPond-command-force nil)
139
140 (defcustom LilyPond-xdvi-command "xdvi"
141   "Command used to display DVI files."
142
143   :group 'LilyPond
144   :type 'string)
145
146 (defcustom LilyPond-gv-command "gv -watch"
147   "Command used to display PS files."
148
149   :group 'LilyPond
150   :type 'string)
151
152 (defcustom LilyPond-midi-command "timidity"
153   "Command used to play MIDI files."
154
155   :group 'LilyPond
156   :type 'string)
157
158 (defcustom LilyPond-midi-all-command "timidity -ig"
159   "Command used to play MIDI files."
160
161   :group 'LilyPond
162   :type 'string)
163
164 ;; This is the major configuration variable.
165 (defcustom LilyPond-command-alist
166   `(
167     ("LilyPond" . ("lilypond %s" . "TeX"))
168     ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
169
170     ("2Dvi" . ("ly2dvi %s" . "View"))
171     ("2PS" . ("ly2dvi -P %s" . "View"))
172     ("2Midi" . ("lilypond -m %s" . "View"))
173
174     ("Book" . ("lilypond-book %x" . "LaTeX"))
175     ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
176
177     ;; point-n-click (arg: exits upop USR1)
178     ("SmartView" . ("xdvi %d" . "LilyPond"))
179     
180     ;; refreshes when kicked USR1
181     ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
182
183     ("ViewPS" . (,(concat LilyPond-gv-command " %p") . "LilyPond"))
184
185     ("Midi" . (,(concat LilyPond-midi-command " %m") . "LilyPond"))
186     )
187
188   "AList of commands to execute on the current document.
189
190 The key is the name of the command as it will be presented to the
191 user, the value is a cons of the command string handed to the shell
192 after being expanded, and the next command to be executed upon
193 success.  The expansion is done using the information found in
194 LilyPond-expand-list.
195 "
196   :group 'LilyPond
197   :type '(repeat (cons :tag "Command Item"
198                        (string :tag "Key")
199                        (cons :tag "How"
200                         (string :tag "Command")
201                         (string :tag "Next Key")))))
202
203 ;; drop this?
204 (defcustom LilyPond-file-extensions '(".ly" ".sly" ".fly")
205   "*File extensions used by manually generated TeX files."
206   :group 'LilyPond
207   :type '(repeat (string :format "%v")))
208
209
210 (defcustom LilyPond-expand-alist 
211   '(
212     ("%s" . ".ly")
213     ("%t" . ".tex")
214     ("%d" . ".dvi")
215     ("%p" . ".ps")
216     ("%l" . ".latex")
217     ("%x" . ".tely")
218     ("%m" . ".midi")
219     )
220     
221   "Alist of expansion strings for LilyPond command names."
222   :group 'LilyPond
223   :type '(repeat (cons :tag "Alist item"
224                   (string :tag "Symbol")
225                   (string :tag "Expansion")))) 
226
227
228 (defcustom LilyPond-command-Show "View"
229   "*The default command to show (view or print) a LilyPond file.
230 Must be the car of an entry in `LilyPond-command-alist'."
231   :group 'LilyPond
232   :type 'string)
233   (make-variable-buffer-local 'LilyPond-command-Show)
234
235 (defcustom LilyPond-command-Print "Print"
236   "The name of the Print entry in LilyPond-command-Print."
237   :group 'LilyPond
238   :type 'string)
239
240 (defun xLilyPond-compile-sentinel (process msg)
241   (if (and process
242            (= 0 (process-exit-status process)))
243       (setq LilyPond-command-default
244               (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
245
246 ;; FIXME: shouldn't do this for stray View/xdvi
247 (defun LilyPond-compile-sentinel (buffer msg)
248   (if (string-match "^finished" msg)
249       (setq LilyPond-command-default
250             (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
251
252 ;;(make-variable-buffer-local 'compilation-finish-function)
253 (setq compilation-finish-function 'LilyPond-compile-sentinel)
254
255 (defun LilyPond-command-query (name)
256   "Query the user for what LilyPond command to use."
257   (let* ((default (cond ((if (string-equal name "emacs-lily")
258                              (LilyPond-check-files (concat name ".tex")
259                                                    (list name)
260                                                    LilyPond-file-extensions)
261                            ;; FIXME
262                            (LilyPond-save-buffer)
263                            ;;"LilyPond"
264                            LilyPond-command-default))
265                         (t LilyPond-command-default)))
266
267          (completion-ignore-case t)
268          
269          (answer (or LilyPond-command-force
270                      (completing-read
271                       (concat "Command: (default " default ") ")
272                       LilyPond-command-alist nil t nil 'LilyPond-command-history))))
273
274     ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
275     (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
276       (if (and answer
277                (not (string-equal answer "")))
278           answer
279         default))))
280
281
282 ;; FIXME: find ``\score'' in buffers / make settable?
283 (defun LilyPond-master-file ()
284   ;; duh
285   (buffer-file-name))
286
287 (defun LilyPond-command-master ()
288   "Run command on the current document."
289   (interactive)
290   (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
291                     'LilyPond-master-file))
292
293 (defun LilyPond-command-lilypond ()
294   "Run lilypond for the current document."
295   (interactive)
296   (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file)
297 )
298
299 (defun LilyPond-command-formatdvi ()
300   "Format the dvi output of the current document."
301   (interactive)
302   (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file)
303 )
304
305 (defun LilyPond-command-formatps ()
306   "Format the ps output of the current document."
307   (interactive)
308   (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file)
309 )
310
311 (defun LilyPond-command-smartview ()
312   "View the dvi output of current document."
313   (interactive)
314   (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file)
315 )
316
317 (defun LilyPond-command-view ()
318   "View the dvi output of current document."
319   (interactive)
320   (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file)
321 )
322
323 (defun LilyPond-command-viewps ()
324   "View the ps output of current document."
325   (interactive)
326   (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file)
327 )
328
329 (defun LilyPond-command-midi ()
330   "Play midi corresponding to the current document."
331   (interactive)
332   (LilyPond-command (LilyPond-command-menu "Midi") 'LilyPond-master-file)
333 )
334
335 (defun count-rexp (start end rexp)
336   "Print number of found regular expressions in the region."
337   (interactive "r")
338   (save-excursion
339     (save-restriction
340       (narrow-to-region start end)
341       (goto-char (point-min))
342       (count-matches rexp))))
343
344 (defun count-midi-words ()
345   "Print number of scores before the curser."
346   (interactive)
347   (count-rexp (point-min) (point-max) "\\\\midi"))
348  
349 (defun count-midi-words-backwards ()
350   "Print number of scores before the curser."
351   (interactive)
352   (count-rexp (point-min) (point) "\\\\midi"))
353  
354 (defun LilyPond-command-next-midi ()
355   "Play next midi score of the current document."
356   (interactive)
357   (if (Midi-running)
358       (quit-process (get-process "midi") t)
359     (LilyPond-compile-file 
360      (let ((fname (LilyPond-master-file))
361            (allcount (string-to-number (substring (count-midi-words) 0 -12)))
362            (count (string-to-number (substring (count-midi-words-backwards) 0 -12))))
363        (concat  LilyPond-midi-command " "
364                 (substring fname 0 -3) ; suppose ".ly"
365                 (if (and (> allcount 1) (> count 0)) ; not first score
366                     (if (eq count allcount)          ; last score
367                         (concat "-" (number-to-string (+ count -1)))
368                       (concat "-" (number-to-string count))))
369                 ".midi"))
370      "Midi")))
371
372 (defun LilyPond-command-all-midi ()
373   "Play next midi score of the current document."
374   (interactive)
375   (if (Midi-running)
376       (quit-process (get-process "midi") t)
377     (LilyPond-compile-file 
378      (let ((fname (LilyPond-master-file))
379            (allcount (string-to-number (substring (count-midi-words) 0 -12))))
380        (concat  LilyPond-midi-all-command " "
381                 (if (> allcount 0) ; at least one midi-score
382                     (concat (substring fname 0 -3) ".midi "))
383                 (if (> allcount 1) ; more than one midi-score
384                     (concat (substring fname 0 -3) "-?.midi "))
385                 (if (> allcount 9) ; etc.
386                     (concat (substring fname 0 -3) "-??.midi"))
387                 (if (> allcount 99) ; not first score
388                     (concat (substring fname 0 -3) "-???.midi"))))
389      "Midi")))
390
391 (defun LilyPond-un-comment-region (start end level)
392   "Remove up to LEVEL comment characters from each line in the region."
393   (interactive "*r\np") 
394   (comment-region start end (- level)))
395
396 ;; FIXME, this is broken
397 (defun LilyPond-region-file (begin end)
398   (let (
399         ;; (dir "/tmp/")
400         ;; urg
401         (dir "./")
402         (base "emacs-lily")
403         ;; Hmm
404         (ext (if (string-match "^[\\]score" (buffer-substring begin end))
405                  ".ly"
406                (if (< 50 (abs (- begin end)))
407                    ".fly"
408                  ".sly"))))
409     (concat dir base ext)))
410
411 (defun LilyPond-command-region (begin end)
412   "Run LilyPond on the current region."
413   (interactive "r")
414   (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
415   (LilyPond-command (LilyPond-command-query
416                      (LilyPond-region-file begin end))
417                     '(lambda () (LilyPond-region-file begin end))))
418
419 (defun LilyPond-command-buffer ()
420   "Run LilyPond on buffer."
421   (interactive)
422   (LilyPond-command-region (point-min) (point-max)))
423
424 (defun LilyPond-command-expand (string file)
425   (let ((case-fold-search nil))
426     (if (string-match "%" string)
427         (let* ((b (match-beginning 0))
428                (e (+ b 2))
429                (l (split-file-name file))
430                (dir (car l))
431                (base (cadr l)))
432           (LilyPond-command-expand
433            (concat (substring string 0 b)
434                    dir
435                    base
436                    (let ((entry (assoc (substring string b e)
437                                        LilyPond-expand-alist)))
438                      (if entry (cdr entry) ""))
439                    (substring string e))
440            file))
441       string)))
442
443 (defun LilyPond-shell-process (name buffer command)
444   (let ((old (current-buffer)))
445     (switch-to-buffer-other-window buffer)
446     ;; If we empty the buffer don't see messages scroll by.
447     ;; (erase-buffer)
448     
449     (start-process-shell-command name buffer command)
450     (switch-to-buffer-other-window old)))
451   
452
453 (defun LilyPond-command (name file)
454   "Run command NAME on the file you get by calling FILE.
455
456 FILE is a function return a file name.  It has one optional argument,
457 the extension to use on the file.
458
459 Use the information in LilyPond-command-alist to determine how to run the
460 command."
461   
462   (let ((entry (assoc name LilyPond-command-alist)))
463     (if entry
464         (let ((command (LilyPond-command-expand (cadr entry)
465                                                 (apply file nil))))
466           (if (string-equal name "View")
467               (let ((buffer-xdvi (get-buffer-create "*view*")))
468                 (if LilyPond-kick-xdvi
469                   (let ((process-xdvi (get-buffer-process buffer-xdvi)))
470                     (if process-xdvi
471                         (signal-process (process-id process-xdvi) 'SIGUSR1)
472                       (LilyPond-shell-process name buffer-xdvi command)))
473                   (LilyPond-shell-process name buffer-xdvi command)))
474             (progn
475               (setq LilyPond-command-default name)
476               (LilyPond-compile-file command name)))))))
477           
478 ;; XEmacs stuff
479 ;; Sadly we need this for a macro in Emacs 19.
480 (eval-when-compile
481   ;; Imenu isn't used in XEmacs, so just ignore load errors.
482   (condition-case ()
483       (require 'imenu)
484     (error nil)))
485
486
487 ;;; Keymap
488
489 (defvar LilyPond-mode-map ()
490   "Keymap used in `LilyPond-mode' buffers.")
491
492 ;; Note:  if you make changes to the map, you must do
493 ;;    M-x set-variable LilyPond-mode-map nil
494 ;;    M-x eval-buffer
495 ;;    M-x LilyPond-mode
496 ;; to let the changest take effect
497
498 (if LilyPond-mode-map
499     ()
500   (setq LilyPond-mode-map (make-sparse-keymap))
501   (define-key LilyPond-mode-map "\C-c\C-l" 'LilyPond-command-lilypond)
502   (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
503   (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
504   (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-job)
505   (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
506   (define-key LilyPond-mode-map "\C-c\C-d" 'LilyPond-command-formatdvi)
507   (define-key LilyPond-mode-map "\C-c\C-f" 'LilyPond-command-formatps)
508   (define-key LilyPond-mode-map "\C-c\C-s" 'LilyPond-command-smartview)
509   (define-key LilyPond-mode-map "\C-c\C-v" 'LilyPond-command-view)
510   (define-key LilyPond-mode-map "\C-c\C-p" 'LilyPond-command-viewps)
511   (define-key LilyPond-mode-map "\C-c\C-m" 'LilyPond-command-next-midi)
512   (define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
513   (define-key LilyPond-mode-map "\C-ci" 'LilyPond-quick-note-insert)
514   (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
515   (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
516   (define-key LilyPond-mode-map "\C-c:" 'LilyPond-un-comment-region)
517   (define-key LilyPond-mode-map "\C-c;" 'comment-region)
518   (define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren)
519   (define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren)
520   (define-key LilyPond-mode-map "}" 'LilyPond-electric-close-paren)
521   )
522
523 ;;; Menu Support
524
525 (defun LilyPond-quick-note-insert()
526   "Insert notes with fewer key strokes by using a simple keyboard piano."
527   (interactive)
528   (setq dutch-notes
529         '(("k" "a") ("l" "b") ("a" "c") ("s" "d") 
530           ("d" "e") ("f" "f") ("j" "g") ("r" "r")))
531   (setq dutch-note-ends '("eses" "es" "" "is" "isis"))
532   (setq dutch-note-replacements '("" ""))
533   (setq finnish-note-replacements
534         '(("eeses" "eses") ("ees" "es") ("aeses" "asas") ("aes" "as") ("b" "h")
535           ("beses" "heses") ("bes" "b") ("bis" "his") ("bisis" "hisis")))
536                               ; add more translations of the note names
537   (setq other-keys "()<>~}")
538   (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)
539
540   (message "Press h for help.") (sit-for 0 750 1)
541
542   (setq note-replacements dutch-note-replacements)
543   (while (not (= 27 ; esc to quit
544     (setq x (read-char-exclusive 
545              (format " | a[_]s[_]d | f[_]j[_]k[_]l | r with ie ,' 12345678 . 0 (<~>)/}\\b\\n Esc \n | c | d | e | f | g | a | %s | r with %s%s%s%s"
546                      (if (string= (car(cdr(assoc "b" note-replacements))) "h")
547                          "h" "b")
548                      (nth (+ accid 2) dutch-note-ends)
549                      (make-string (abs octav) (if (> octav 0) ?' ?,)) 
550                      durat 
551                      (if (string= durat "") "" (make-string dots ?.)))))))
552 ;    (insert (number-to-string x)) ; test numbers for characters
553     (setq note (cdr (assoc (char-to-string x) dutch-notes)))
554     (cond
555      ((= x 127) (backward-kill-word 1)) ; backspace
556      ((= x 13) (progn (insert "\n") (LilyPond-indent-line)))) ; return
557     (setq x (char-to-string x))
558     (cond
559      ((and (string< x "9") (string< "0" x))
560       (progn (setq durat (int-to-string (expt 2 (- (string-to-int x) 1))))
561              (setq dots 0)))
562      ((string= x " ") (insert " "))
563      ((string= x "/") (progn (insert "\\times ")
564                              (while (not (and (string< x "9") (string< "0" x)))
565                                (setq x (char-to-string (read-char-exclusive "Insert a number for the denominator (\"x/\")"))))
566                              (insert (format "%s/" x)) (setq x "/")
567                              (while (not (and (string< x "9") (string< "0" x)))
568                                (setq x (char-to-string (read-char-exclusive "Insert a number for the numerator (\"/y\")"))))
569                              (insert (format "%s { " x))))
570      ((string= x "0") (progn (setq accid 0) (setq octav 0) 
571                              (setq durat "") (setq dots 0)))
572      ((string= x "i") (setq accid (if (= accid 2) 0 (max (+ accid 1) 1))))
573      ((string= x "e") (setq accid (if (= accid -2) 0 (min (+ accid -1) -1))))
574      ((string= x "'") (setq octav (if (= octav 4) 0 (max (+ octav 1) 1))))
575      ((string= x ",") (setq octav (if (= octav -4) 0 (min (+ octav -1) -1))))
576      ((string= x ".") (setq dots (if (= dots 4) 0 (+ dots 1))))
577      ((not (null (member x (split-string other-keys ""))))
578       (insert (format "%s " x)))
579      ((not (null note))
580       (progn
581         (setq note 
582               (format "%s%s" (car note) (if (string= "r" (car note)) "" 
583                                           (nth (+ accid 2) dutch-note-ends))))
584         (setq notetwo (car (cdr (assoc note note-replacements))))
585         (if (not (null notetwo)) (setq note notetwo))
586         (insert
587          (format "%s%s%s%s " 
588                  note
589                  (if (string= "r" note) ""
590                      (make-string (abs octav) (if (> octav 0) ?' ?,)))
591                  durat
592                  (if (string= durat "") "" (make-string dots ?.))))
593         (setq accid 0) (setq octav 0) (setq durat "") (setq dots 0)))
594      ((string= x "t") (progn (setq note-replacements dutch-note-replacements)
595                              (message "Selected Dutch notes") 
596                              (sit-for 0 750 1))) ; t
597      ((string= x "n") (progn (setq note-replacements finnish-note-replacements)
598                              (message "Selected Finnish/Deutsch notes") 
599                              (sit-for 0 750 1))) ; n
600                               ; add more translations of the note names
601      ((string= x "h") 
602       (progn (message "Insert notes with fewer key strokes. For example \"i,5.f\" produces \"fis,32. \".") (sit-for 5 0 1) 
603              (message "Add also \"a ~ a\"-ties, \"a ( ) b\"-slurs and \"< a b >\"-chords.") (sit-for 5 0 1) 
604              (message "Note names are in Du(t)ch by default. Hit 'n' for Fin(n)ish/Deutsch note names.") (sit-for 5 0 1) 
605              (message "Backspace deletes last note, return starts a new indented line and Esc quits.") (sit-for 5 0 1) 
606              (message "Insert note triplets \"\\times 2/3 { a b } \" by typing \"/23ab}\".") (sit-for 5 0 1) 
607              (message "Remember to add all other details as well.") (sit-for 5 0 1)))
608     )))
609
610 (define-skeleton LilyPond-insert-tag-notes
611   "LilyPond notes tag."
612   nil
613 ;  (if (bolp) nil ?\n)
614   "\\notes"
615   (if (y-or-n-p "Set \"\\relative\" attribute? ")
616       (concat " \\relative " (skeleton-read "Relative: " "" str)))
617   " { " _ " }")
618
619 (define-skeleton LilyPond-insert-tag-score
620   "LilyPond score tag."
621   nil
622   (if (bolp) nil ?\n)
623   "\\score {\n"
624   "   " _ "\n"
625   "   \\paper {  }\n"
626   (if (y-or-n-p "Insert \"\\header\" field? ")
627       (concat "   \\header {\n      " 
628               (skeleton-read "Piece: " "piece = " str) "\n"
629               (if (y-or-n-p "Insert \"opus\" field? ")
630                   (concat "      " (skeleton-read "Opus: " "opus = " str) "\n"))
631               "   }\n"))
632   (if (y-or-n-p "Insert \"\\midi\" field? ")
633       (concat "   \\midi { " 
634               (skeleton-read "Midi: " "\\tempo 4 = " str)  
635               " }\n"))
636   "}\n")
637
638 (defun LilyPond-command-menu-entry (entry)
639   ;; Return LilyPond-command-alist ENTRY as a menu item.
640   (let ((name (car entry)))
641     (cond ((and (string-equal name LilyPond-command-Print)
642                 LilyPond-printer-list)
643            (let ((command LilyPond-print-command)
644                  (lookup 1))
645              (append (list LilyPond-command-Print)
646                      (mapcar 'LilyPond-command-menu-printer-entry
647                              LilyPond-printer-list))))
648           (t
649            (vector name (list 'LilyPond-command-menu name) t)))))
650
651
652 (easy-menu-define LilyPond-command-menu
653     LilyPond-mode-map
654     "Menu used in LilyPond mode."
655   (append '("Command")
656           '(("Command on"
657              [ "Master File" LilyPond-command-select-master
658                :keys "C-c C-c" :style radio
659                :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
660              [ "Buffer" LilyPond-command-select-buffer
661                :keys "C-c C-b" :style radio
662                :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
663              [ "Region" LilyPond-command-select-region
664                :keys "C-c C-r" :style radio
665                :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
666 ;         (let ((file 'LilyPond-command-on-current))
667 ;           (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))
668 ;;; Some kind of mapping which includes :keys might be more elegant
669           '([ "LilyPond" (LilyPond-command (LilyPond-command-menu "LilyPond") 'LilyPond-master-file) :keys "C-c C-l"])
670           '([ "TeX" (LilyPond-command (LilyPond-command-menu "TeX") 'LilyPond-master-file) ])
671           '([ "2Dvi" (LilyPond-command (LilyPond-command-menu "2Dvi") 'LilyPond-master-file) :keys "C-c C-d"])
672           '([ "2PS" (LilyPond-command (LilyPond-command-menu "2PS") 'LilyPond-master-file) :keys "C-c C-f"])
673           '([ "2Midi" (LilyPond-command (LilyPond-command-menu "2Midi") 'LilyPond-master-file)])
674           '([ "Book" (LilyPond-command (LilyPond-command-menu "Book") 'LilyPond-master-file) ])
675           '([ "LaTeX" (LilyPond-command (LilyPond-command-menu "LaTeX") 'LilyPond-master-file) ])
676           '([ "SmartView" (LilyPond-command (LilyPond-command-menu "SmartView") 'LilyPond-master-file) :keys "C-c C-s"])
677           '([ "View" (LilyPond-command (LilyPond-command-menu "View") 'LilyPond-master-file) :keys "C-c C-v"])
678           '([ "ViewPS" (LilyPond-command (LilyPond-command-menu "ViewPS") 'LilyPond-master-file) :keys "C-c C-p"])
679           '([ "Midi (off)" (LilyPond-command-next-midi) :keys "C-c C-m"])
680           '([ "Midi all" (LilyPond-command-all-midi)])
681           ))
682
683 (easy-menu-define LilyPond-mode-menu
684   LilyPond-mode-map
685   "Menu used in LilyPond mode."
686   (append '("LilyPond")
687           '(("Insert"
688              [ "\\notes..."  LilyPond-insert-tag-notes t]
689              [ "\\score..."  LilyPond-insert-tag-score t]
690              ["Quick Notes"  LilyPond-quick-note-insert t]
691              ))
692           '(("Miscellaneous"
693              ["Uncomment Region" LilyPond-un-comment-region t]
694              ["Comment Region" comment-region t]
695              ["Refontify buffer" font-lock-fontify-buffer t]
696              ))
697           ))
698
699 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z_][a-zA-Z0-9_]*\\) *="
700   "Regexp matching Identifier definitions.")
701
702 (defvar LilyPond-imenu-generic-expression
703   (list (list nil LilyPond-imenu-generic-re 1))
704   "Expression for imenu")
705
706 (defun LilyPond-command-select-master ()
707   (interactive)
708   (message "Next command will be on the master file")
709   (setq LilyPond-command-current 'LilyPond-command-master))
710
711 (defun LilyPond-command-select-buffer ()
712   (interactive)
713   (message "Next command will be on the buffer")
714   (setq LilyPond-command-current 'LilyPond-command-buffer))
715
716 (defun LilyPond-command-select-region ()
717   (interactive)
718   (message "Next command will be on the region")
719   (setq LilyPond-command-current 'LilyPond-command-region))
720
721 (defun LilyPond-command-menu (name)
722   ;; Execute LilyPond-command-alist NAME from a menu.
723   (let ((LilyPond-command-force name))
724     (funcall LilyPond-command-current)))
725
726 (defun LilyPond-mode ()
727   "Major mode for editing LilyPond music files.
728
729 This mode knows about LilyPond keywords and line comments, not about
730 indentation or block comments.  It features easy compilation, error
731 finding and viewing of a LilyPond source buffer or region.
732
733 COMMANDS
734 \\{LilyPond-mode-map}
735 VARIABLES
736
737 LilyPond-command-alist\t\talist from name to command
738 LilyPond-xdvi-command\t\tcommand to display dvi files -- bit superfluous"
739   (interactive)
740   ;; set up local variables
741   (kill-all-local-variables)
742
743   (make-local-variable 'font-lock-defaults)
744   (setq font-lock-defaults '(LilyPond-font-lock-keywords))
745
746   ;; string and comments are fontified explicitly
747   (make-local-variable 'font-lock-keywords-only)
748   (setq font-lock-keywords-only t)
749
750   ;; Multi-line font-locking needs Emacs 21.1 or newer.
751   ;; For older versions there is hotkey "C-c f".
752   (make-local-variable 'font-lock-multiline) 
753   (setq font-lock-multiline t) 
754
755   (make-local-variable 'paragraph-separate)
756   (setq paragraph-separate "^[ \t]*$")
757
758   (make-local-variable 'paragraph-start)
759   (setq paragraph-start "^[ \t]*$")
760
761   (make-local-variable 'comment-start)
762   (setq comment-start "%")
763
764   (make-local-variable 'comment-start-skip)
765   (setq comment-start-skip "%{? *")
766
767   (make-local-variable 'comment-end)
768   (setq comment-end "")
769
770   (make-local-variable 'block-comment-start)
771   (setq block-comment-start "%{")
772
773   (make-local-variable 'block-comment-end)  
774   (setq block-comment-end   "%}")
775
776   (make-local-variable 'indent-line-function)
777   (setq indent-line-function 'LilyPond-indent-line)
778
779     (set-syntax-table LilyPond-mode-syntax-table)
780   (setq major-mode 'LilyPond-mode)
781   (setq mode-name "LilyPond")
782   (setq local-abbrev-table LilyPond-mode-abbrev-table)
783   (use-local-map LilyPond-mode-map)
784
785   (make-local-variable 'imenu-generic-expression)
786   (setq imenu-generic-expression LilyPond-imenu-generic-expression)
787   (imenu-add-to-menubar "Index")
788
789     ;; run the mode hook. LilyPond-mode-hook use is deprecated
790   (run-hooks 'LilyPond-mode-hook))
791
792 (defun LilyPond-version ()
793   "Echo the current version of `LilyPond-mode' in the minibuffer."
794   (interactive)
795   (message "Using `LilyPond-mode' version %s" LilyPond-version))
796
797 (provide 'lilypond-mode)
798 ;;; lilypond-mode.el ends here
799