]> git.donarmstrong.com Git - lilypond.git/blob - lilypond-mode.el
release: 1.3.122
[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, 2000 Jan Nieuwenhuizen <janneke@gnu.org>
7
8 ;;; Inspired on auctex
9
10 ;;;
11 ;;; Add this to your .emacs.el
12 ;;;     (load-library "lilypond-mode.el")
13 ;;;     (setq auto-mode-alist
14 ;;;      (append '(("\\.ly$" . LilyPond-mode) auto-mode-alist)))
15 ;;; 
16
17 (load-library "lilypond-font-lock")
18
19 (require 'easymenu)
20 (require 'compile)
21
22 (defconst LilyPond-version "1.3.103"
23   "`LilyPond-mode' version number.")
24
25 (defconst LilyPond-help-address "bug-gnu-music@gnu.org"
26   "Address accepting submission of bug reports.")
27
28 (defvar LilyPond-mode-hook nil
29   "*Hook called by `LilyPond-mode'.")
30
31 (defvar LilyPond-regexp-alist
32   '(("\\([a-zA-Z]?:?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 2))
33   "Regexp used to match LilyPond errors.  See `compilation-error-regexp-alist'.")
34
35 (defcustom LilyPond-include-path ".:/tmp"
36   "* LilyPond include path."
37   :type 'string
38   :group 'LilyPond)
39
40
41 (defun LilyPond-check-files (derived originals extensions)
42   "Check that DERIVED is newer than any of the ORIGINALS.
43 Try each original with each member of EXTENSIONS, in all directories
44 in LilyPond-include-path."
45   (let ((found nil)
46         (regexp (concat "\\`\\("
47                         (mapconcat (function (lambda (dir)
48                                       (regexp-quote (expand-file-name dir))))
49                                    LilyPond-include-path "\\|")
50                         "\\).*\\("
51                         (mapconcat 'regexp-quote originals "\\|")
52                         "\\)\\.\\("
53                         (mapconcat 'regexp-quote extensions "\\|")
54                         "\\)\\'"))
55         (buffers (buffer-list)))
56     (while buffers
57       (let* ((buffer (car buffers))
58              (name (buffer-file-name buffer)))
59         (setq buffers (cdr buffers))
60         (if (and name (string-match regexp name))
61             (progn
62               (and (buffer-modified-p buffer)
63                    (or (not LilyPond-save-query)
64                        (y-or-n-p (concat "Save file "
65                                          (buffer-file-name buffer)
66                                          "? ")))
67                    (save-excursion (set-buffer buffer) (save-buffer)))
68               (if (file-newer-than-file-p name derived)
69                   (setq found t))))))
70     found))
71
72 (defun LilyPond-running ()
73   (let ((process (get-process "lilypond")))
74   (and process
75        (eq (process-status process) 'run))))
76
77 (defun LilyPond-kill-job ()
78   "Kill the currently running LilyPond job."
79   (interactive)
80   ;; What bout TeX, Xdvi?
81   (quit-process (get-process "lilypond") t))
82
83 ;; URG, should only run LilyPond-compile for LilyPond
84 ;; not for tex,xdvi (ly2dvi?)
85 (defun LilyPond-compile-file (command name)
86   ;; We maybe should know what we run here (Lily, ly2dvi, tex)
87   ;; and adjust our error-matching regex ?
88   (compile-internal command "No more errors" name ))
89
90 ;; do we still need this, now that we're using compile-internal?
91 (defun LilyPond-save-buffer ()
92   (if (buffer-modified-p) (save-buffer)))
93
94 ;;; return (dir base ext)
95 (defun split-file-name (name)
96   (let* ((i (string-match "[^/]*$" name))
97          (dir (if (> i 0) (substring name 0 i) "./"))
98          (file (substring name i (length name)))
99          (i (string-match "[^.]*$" file)))
100     (if (and
101          (> i 0)
102          (< i (length file)))
103         (list dir (substring file 0 (- i 1)) (substring file i (length file)))
104       (list dir file ""))))
105
106
107 ;; Should check whether in command-alist?
108 (defcustom LilyPond-command-default "LilyPond"
109   "Default command. Must identify a member of LilyPond-command-alist."
110
111   :group 'LilyPond
112   :type 'string)
113 ;;;(make-variable-buffer-local 'LilyPond-command-last)
114
115 (defvar LilyPond-command-current 'LilyPond-command-master)
116 ;;;(make-variable-buffer-local 'LilyPond-command-master)
117
118
119 ;; If non-nil, LilyPond-command-query will return the value of this
120 ;; variable instead of quering the user. 
121 (defvar LilyPond-command-force nil)
122
123 (defcustom LilyPond-xdvi-command "xdvik"
124   "Command used to display DVI files."
125
126   :group 'LilyPond
127   :type 'string)
128
129 ;; This is the major configuration variable.
130 (defcustom LilyPond-command-alist
131   `(
132     ("LilyPond" . ("lilypond %s" . "TeX"))
133     ("TeX" . ("tex '\\nonstopmode\\input %t'" . "View"))
134
135     ("2Dvi" . ("ly2dvi %s" . "View"))
136
137     ("Book" . ("lilypond-book %x" . "LaTeX"))
138     ("LaTeX" . ("latex '\\nonstopmode\\input %l'" . "View"))
139
140     ;; point-n-click (arg: exits upop USR1)
141     ("SmartView" . ("xdvi %d" . "LilyPond"))
142     
143     ;; refreshes when kicked USR1
144     ("View" . (,(concat LilyPond-xdvi-command " %d") . "LilyPond"))
145     )
146
147   "AList of commands to execute on the current document.
148
149 The key is the name of the command as it will be presented to the
150 user, the value is a cons of the command string handed to the shell
151 after being expanded, and the next command to be executed upon
152 success.  The expansion is done using the information found in
153 LilyPond-expand-list.
154 "
155   :group 'LilyPond
156   :type '(repeat (cons :tag "Command Item"
157                        (string :tag "Key")
158                        (cons :tag "How"
159                         (string :tag "Command")
160                         (string :tag "Next Key")))))
161
162 ;; drop this?
163 (defcustom LilyPond-file-extensions '(".ly" ".sly" ".fly")
164   "*File extensions used by manually generated TeX files."
165   :group 'LilyPond
166   :type '(repeat (string :format "%v")))
167
168
169 (defcustom LilyPond-expand-alist 
170   '(
171     ("%s" . ".ly")
172     ("%t" . ".tex")
173     ("%d" . ".dvi")
174     ("%p" . ".ps")
175     ("%l" . ".latex")
176     ("%x" . ".tely")
177     )
178     
179   "Alist of expansion strings for LilyPond command names."
180   :group 'LilyPond
181   :type '(repeat (cons :tag "Alist item"
182                   (string :tag "Symbol")
183                   (string :tag "Expansion")))) 
184
185
186 (defcustom LilyPond-command-Show "View"
187   "*The default command to show (view or print) a LilyPond file.
188 Must be the car of an entry in `LilyPond-command-alist'."
189   :group 'LilyPond
190   :type 'string)
191   (make-variable-buffer-local 'LilyPond-command-Show)
192
193 (defcustom LilyPond-command-Print "Print"
194   "The name of the Print entry in LilyPond-command-Print."
195   :group 'LilyPond
196   :type 'string)
197
198 (defun xLilyPond-compile-sentinel (process msg)
199   (if (and process
200            (= 0 (process-exit-status process)))
201       (setq LilyPond-command-default
202               (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
203
204 ;; FIXME: shouldn't do this for stray View/xdvi
205 (defun LilyPond-compile-sentinel (buffer msg)
206   (if (string-match "^finished" msg)
207       (setq LilyPond-command-default
208             (cddr (assoc LilyPond-command-default LilyPond-command-alist)))))
209
210 ;;(make-variable-buffer-local 'compilation-finish-function)
211 (setq compilation-finish-function 'LilyPond-compile-sentinel)
212
213 (defun LilyPond-command-query (name)
214   "Query the user for what LilyPond command to use."
215   (let* ((default (cond ((if (string-equal name "emacs-lily")
216                              (LilyPond-check-files (concat name ".tex")
217                                                    (list name)
218                                                    LilyPond-file-extensions)
219                            ;; FIXME
220                            (LilyPond-save-buffer)
221                            ;;"LilyPond"
222                            LilyPond-command-default))
223                         (t LilyPond-command-default)))
224          
225          (answer (or LilyPond-command-force
226                      (completing-read
227                       (concat "Command: (default " default ") ")
228                       LilyPond-command-alist nil t))))
229
230     ;; If the answer is "LilyPond" it will not be expanded to "LilyPond"
231     (let ((answer (car-safe (assoc answer LilyPond-command-alist))))
232       (if (and answer
233                (not (string-equal answer "")))
234           answer
235         default))))
236
237
238 ;; FIXME: find ``\score'' in buffers / make settable?
239 (defun LilyPond-master-file ()
240   ;; duh
241   (buffer-file-name))
242
243 (defun LilyPond-command-master ()
244   "Run command on the current document."
245   (interactive)
246   (LilyPond-command (LilyPond-command-query (LilyPond-master-file))
247                     'LilyPond-master-file))
248
249 (defun LilyPond-region-file (begin end)
250   (let (
251         ;; (dir "/tmp/")
252         ;; urg
253         (dir "./")
254         (base "emacs-lily")
255         ;; Hmm
256         (ext (if (string-match "^[\\]score" (buffer-substring begin end))
257                  ".ly"
258                (if (< 50 (abs (- begin end)))
259                    ".fly"
260                  ".sly"))))
261     (concat dir base ext)))
262
263 (defun LilyPond-command-region (begin end)
264   "Run LilyPond on the current region."
265   (interactive "r")
266   (write-region begin end (LilyPond-region-file begin end) nil 'nomsg)
267   (LilyPond-command (LilyPond-command-query
268                      (LilyPond-region-file begin end))
269                     '(lambda () (LilyPond-region-file begin end))))
270
271 (defun LilyPond-command-buffer ()
272   "Run LilyPond on buffer."
273   (interactive)
274   (LilyPond-command-region (point-min) (point-max)))
275
276 (defun LilyPond-command-expand (string file)
277   (let ((case-fold-search nil))
278     (if (string-match "%" string)
279         (let* ((b (match-beginning 0))
280                (e (+ b 2))
281                (l (split-file-name file))
282                (dir (car l))
283                (base (cadr l)))
284           (LilyPond-command-expand
285            (concat (substring string 0 b)
286                    dir
287                    base
288                    (let ((entry (assoc (substring string b e)
289                                        LilyPond-expand-alist)))
290                      (if entry (cdr entry) ""))
291                    (substring string e))
292            file))
293       string)))
294
295 (defun LilyPond-command (name file)
296   "Run command NAME on the file you get by calling FILE.
297
298 FILE is a function return a file name.  It has one optional argument,
299 the extension to use on the file.
300
301 Use the information in LilyPond-command-alist to determine how to run the
302 command."
303   
304   (let ((entry (assoc name LilyPond-command-alist)))
305     (if entry
306         (let ((command (LilyPond-command-expand (cadr entry)
307                                                 (apply file nil))))
308           (let* (
309                  (buffer-xdvi (get-buffer "*view*"))
310                  (process-xdvi (if buffer-xdvi (get-buffer-process buffer-xdvi) nil)))
311             (if (and process-xdvi
312                      (string-equal name "View"))
313                 ;; Don't open new xdvi window, but force redisplay
314                 ;; We could make this an option.
315                 (signal-process (process-id process-xdvi) 'SIGUSR1)
316               (progn
317                 (setq LilyPond-command-default name)
318                 (LilyPond-compile-file command name))))))))
319           
320 ;; XEmacs stuff
321 ;; Sadly we need this for a macro in Emacs 19.
322 (eval-when-compile
323   ;; Imenu isn't used in XEmacs, so just ignore load errors.
324   (condition-case ()
325       (require 'imenu)
326     (error nil)))
327
328
329 ;;; Keymap
330
331 (defvar LilyPond-mode-map ()
332   "Keymap used in `LilyPond-mode' buffers.")
333
334 ;; Note:  if you make changes to the map, you must do
335 ;;    M-x set-variable LilyPond-mode-map nil
336 ;;    M-x eval-buffer
337 ;;    M-x LilyPond-mode
338 ;; to let the changest take effect
339
340 (if LilyPond-mode-map
341     ()
342   (setq LilyPond-mode-map (make-sparse-keymap))
343   (define-key LilyPond-mode-map "\C-c\C-c" 'LilyPond-command-master)
344   (define-key LilyPond-mode-map "\C-c\C-r" 'LilyPond-command-region)
345   (define-key LilyPond-mode-map "\C-c\C-b" 'LilyPond-command-buffer)
346   (define-key LilyPond-mode-map "\C-c\C-k" 'LilyPond-kill-job)
347   )
348
349 ;;; Menu Support
350
351 (defun LilyPond-command-menu-entry (entry)
352   ;; Return LilyPond-command-alist ENTRY as a menu item.
353   (let ((name (car entry)))
354     (cond ((and (string-equal name LilyPond-command-Print)
355                 LilyPond-printer-list)
356            (let ((command LilyPond-print-command)
357                  (lookup 1))
358              (append (list LilyPond-command-Print)
359                      (mapcar 'LilyPond-command-menu-printer-entry
360                              LilyPond-printer-list))))
361           (t
362            (vector name (list 'LilyPond-command-menu name) t)))))
363
364
365 (easy-menu-define LilyPond-mode-menu
366     LilyPond-mode-map
367     "Menu used in LilyPond mode."
368   (append '("Command")
369           '(("Command on"
370              [ "Master File" LilyPond-command-select-master
371                :keys "C-c C-c" :style radio
372                :selected (eq LilyPond-command-current 'LilyPond-command-master) ]
373              [ "Buffer" LilyPond-command-select-buffer
374                :keys "C-c C-b" :style radio
375                :selected (eq LilyPond-command-current 'LilyPond-command-buffer) ]
376              [ "Region" LilyPond-command-select-region
377                :keys "C-c C-r" :style radio
378                :selected (eq LilyPond-command-current 'LilyPond-command-region) ]))
379           (let ((file 'LilyPond-command-on-current))
380             (mapcar 'LilyPond-command-menu-entry LilyPond-command-alist))))
381
382
383 (defconst LilyPond-imenu-generic-re "^\\([a-zA-Z_][a-zA-Z0-9_]*\\) *="
384   "Regexp matching Identifier definitions.")
385
386 (defvar LilyPond-imenu-generic-expression
387   (list (list nil LilyPond-imenu-generic-re 1))
388   "Expression for imenu")
389
390 (defun LilyPond-command-select-master ()
391   (interactive)
392   (message "Next command will be on the master file")
393   (setq LilyPond-command-current 'LilyPond-command-master))
394
395 (defun LilyPond-command-select-buffer ()
396   (interactive)
397   (message "Next command will be on the buffer")
398   (setq LilyPond-command-current 'LilyPond-command-buffer))
399
400 (defun LilyPond-command-select-region ()
401   (interactive)
402   (message "Next command will be on the region")
403   (setq LilyPond-command-current 'LilPond-command-region))
404
405 (defun LilyPond-command-menu (name)
406   ;; Execute LilyPond-command-alist NAME from a menu.
407   (let ((LilyPond-command-force name))
408     (funcall LilyPond-command-current)))
409
410 (defun LilyPond-mode ()
411   "Major mode for editing LilyPond music files."
412   (interactive)
413   ;; set up local variables
414   (kill-all-local-variables)
415
416   (make-local-variable 'font-lock-defaults)
417   (setq font-lock-defaults '(LilyPond-font-lock-keywords))
418
419   (make-local-variable 'paragraph-separate)
420   (setq paragraph-separate "^[ \t]*$")
421
422   (make-local-variable 'paragraph-start)
423   (setq paragraph-start "^[ \t]*$")
424
425   (make-local-variable 'comment-start)
426   (setq comment-start "%")
427
428   (make-local-variable 'comment-start-skip)
429   (setq comment-start-skip "%{? *")
430
431   (make-local-variable 'comment-end)
432   (setq comment-end "\n")
433
434   (make-local-variable 'block-comment-start)
435   (setq block-comment-start "%{")
436
437   (make-local-variable 'block-comment-end)  
438   (setq block-comment-end   "%}")
439
440   (make-local-variable 'indent-line-function)
441   (setq indent-line-function 'indent-relative-maybe)
442  
443     (set-syntax-table LilyPond-mode-syntax-table)
444   (setq major-mode 'LilyPond-mode)
445   (setq mode-name "LilyPond")
446   (setq local-abbrev-table LilyPond-mode-abbrev-table)
447   (use-local-map LilyPond-mode-map)
448
449   (make-local-variable 'imenu-generic-expression)
450   (setq imenu-generic-expression LilyPond-imenu-generic-expression)
451   (imenu-add-to-menubar "Index")
452
453     ;; run the mode hook. LilyPond-mode-hook use is deprecated
454   (run-hooks 'LilyPond-mode-hook))
455
456 (defun LilyPond-version ()
457   "Echo the current version of `LilyPond-mode' in the minibuffer."
458   (interactive)
459   (message "Using `LilyPond-mode' version %s" LilyPond-version))
460
461 (provide 'lilypond-mode)
462 ;;; lilypond-mode.el ends here
463