(require 'easymenu)
(require 'compile)
-(defconst LilyPond-version "1.7.30"
+(defconst LilyPond-version "1.8.0"
"`LilyPond-mode' version number.")
(defconst LilyPond-help-address "bug-lilypond@gnu.org"
(LilyPond-add-dictionary-word (list copy)))
(kill-buffer b)))
-(defconst LilyPond-keywords
+(defvar LilyPond-insert-tag-current ""
+ "The last command selected from the LilyPond-Insert -menu.")
+
+(defconst LilyPond-menu-keywords
+ (let ((wordlist '()) ; add syntax entries to lilypond.words
+ (co (all-completions "" (LilyPond-add-dictionary-word ())))
+ (currword ""))
+ (progn
+ (while (> (length co) 0)
+ (setq currword (car co))
+ (if (string-equal "-" (car (setq co (cdr co))))
+ (progn
+ (add-to-list 'wordlist currword)
+ (while (and (> (length co) 0)
+ (not (string-equal "-" (car (setq co (cdr co))))))))))
+ (reverse wordlist)))
+ "Keywords inserted from LilyPond-Insert-menu.")
+
+(defconst LilyPond-keywords
(let ((wordlist '("\\score")) ; add \keywords to lilypond.words
- (co (all-completions "" (LilyPond-add-dictionary-word ()))))
+ (co (all-completions "" (LilyPond-add-dictionary-word ())))
+ (currword ""))
(progn
(while (> (length co) 0)
- (if (> (length (car co)) 1)
- (if (and (string-equal "\\" (substring (car co) 0 1))
- (string-equal (downcase (car co)) (car co)))
- (if (not (member (car co) wordlist))
- (add-to-list 'wordlist (car co)))))
- (setq co (cdr co)))
+ (setq currword (car co))
+ (if (> (length currword) 1)
+ (if (and (string-equal "\\" (substring currword 0 1))
+ (not (string-match "[^a-zA-Z]" (substring currword 1)))
+ (string-equal (downcase currword) currword))
+ (add-to-list 'wordlist currword)))
+ (if (string-equal "-" (car (setq co (cdr co))))
+ (while (and (> (length co) 0)
+ (not (string-equal "-" (car (setq co (cdr co)))))))))
wordlist))
"LilyPond \\keywords")
(co (all-completions "" (LilyPond-add-dictionary-word ()))))
(progn
(while (> (length co) 0)
- (if (> (length (car co)) 1)
- (if (and (string-equal "\\" (substring (car co) 0 1))
- (not (string-equal (downcase (car co)) (car co))))
- (if (not (member (car co) wordlist))
- (add-to-list 'wordlist (car co)))))
- (setq co (cdr co)))
+ (setq currword (car co))
+ (if (> (length currword) 1)
+ (if (and (string-equal "\\" (substring currword 0 1))
+ (not (string-match "[^a-zA-Z]" (substring currword 1)))
+ (not (string-equal (downcase currword) currword)))
+ (add-to-list 'wordlist currword)))
+ (if (string-equal "-" (car (setq co (cdr co))))
+ (while (and (> (length co) 0)
+ (not (string-equal "-" (car (setq co (cdr co)))))))))
wordlist))
"LilyPond \\Identifiers")
(co (all-completions "" (LilyPond-add-dictionary-word ()))))
(progn
(while (> (length co) 0)
- (if (> (length (car co)) 0)
- (if (not (string-match "[^a-zA-Z]" (car co)))
- (if (not (member (car co) wordlist))
- (add-to-list 'wordlist (car co)))))
- (setq co (cdr co)))
+ (setq currword (car co))
+ (if (> (length currword) 0)
+ (if (not (string-match "[^a-zA-Z]" currword))
+ (add-to-list 'wordlist currword)))
+ (if (string-equal "-" (car (setq co (cdr co))))
+ (while (and (> (length co) 0)
+ (not (string-equal "-" (car (setq co (cdr co)))))))))
wordlist))
"LilyPond ReservedWords")
(define-key LilyPond-mode-map [(control c) (control return)] 'LilyPond-command-all-midi)
(define-key LilyPond-mode-map "\C-x\C-s" 'LilyPond-save-buffer)
(define-key LilyPond-mode-map "\C-cf" 'font-lock-fontify-buffer)
+ (define-key LilyPond-mode-map "\C-ci" 'LilyPond-insert-tag-current)
;; the following will should be overriden by Lilypond Quick Insert Mode
(define-key LilyPond-mode-map "\C-cq" 'LilyPond-quick-insert-mode)
- (define-key LilyPond-mode-map "\C-cn" 'LilyPond-insert-tag-notes)
- (define-key LilyPond-mode-map "\C-cs" 'LilyPond-insert-tag-score)
(define-key LilyPond-mode-map "\C-c;" 'LilyPond-comment-region)
(define-key LilyPond-mode-map ")" 'LilyPond-electric-close-paren)
(define-key LilyPond-mode-map ">" 'LilyPond-electric-close-paren)
(LilyPond-info)
(Info-index str)))
-(defun LilyPond-insert-string (pre)
- "Insert text to the buffer."
- (insert pre)
- (length pre))
-
-(defun LilyPond-insert-between (text pre post)
- "Insert text to the buffer if non-empty string is given."
- (let ((str (read-string text)))
- (if (string-equal str "")
- 0
- (progn (setq pre_str_post (concat pre str post))
- (insert pre_str_post)
- (length pre_str_post)))))
-
-(defun LilyPond-insert-tag-notes ()
- "LilyPond notes tag."
+(defun LilyPond-insert-tag-current (&optional word)
+ "Set the current tag to be inserted."
(interactive)
- (setq begin (if (LilyPond-mark-active)
- (mark-marker) (point-marker)))
- (setq end (point-marker))
- (goto-char begin)
- (setq l1 (LilyPond-insert-string "\\notes "))
- (setq l2 (LilyPond-insert-between "Relative (e.g. c'): " "\\relative " " "))
- (if (eq l2 0)
- (setq l2 (LilyPond-insert-between "Transpose (e.g. c c'): " "\\transpose " " ")))
- (setq l3 (LilyPond-insert-string "{ "))
- (goto-char (+ end l1 l2 l3))
- (LilyPond-insert-string " }")
- (goto-char (+ end l1 l2 l3)))
-
-(defun LilyPond-insert-tag-score ()
- "LilyPond score tag."
+ (if word
+ (setq LilyPond-insert-tag-current word))
+ (if (memq LilyPond-insert-tag-current LilyPond-menu-keywords)
+ (LilyPond-insert-tag)
+ (message "No tag was selected from LilyPond->Insert tag-menu.")))
+
+(defun LilyPond-insert-tag ()
+ "Insert syntax for given tag. The definitions are in lilypond.words."
(interactive)
- (setq begin (if (LilyPond-mark-active)
- (mark-marker) (point-marker)))
- (setq end (point-marker))
- (goto-char begin)
- (setq l1 (LilyPond-insert-string "\\score {\n ")) ; keep track of lengths
- (goto-char (+ end l1))
- (LilyPond-insert-string "\n \\paper { }\n")
- (setq l2 (if (y-or-n-p "Insert \"\\header\" field? ")
- (+ (LilyPond-insert-string " \\header {")
- (LilyPond-insert-between "Title: " "\n title = \"" "\"")
- (LilyPond-insert-between "Subtitle: " "\n subtitle = \"" "\"")
- (LilyPond-insert-between "Piece: " "\n piece = \"" "\"")
- (LilyPond-insert-between "Opus: " "\n opus = \"" "\"")
- (LilyPond-insert-string "\n }\n"))
- 0))
- (setq l3 (if (y-or-n-p "Insert \"\\midi\" field? ")
- (+ (LilyPond-insert-string " \\midi {")
- (LilyPond-insert-between "Tempo: (e.g. 4=60) " " \\tempo " "")
- (LilyPond-insert-string " }\n"))
- 0))
- (setq l4 (LilyPond-insert-string "}\n"))
- (goto-char (+ end l1)))
+ (setq b (find-file-noselect (LilyPond-words-filename) t t))
+ (let ((word LilyPond-insert-tag-current)
+ (found nil)
+ (p nil)
+ (query nil)
+ (m (set-marker (make-marker) 1 (get-buffer b)))
+ (distance (if (LilyPond-mark-active)
+ (abs (- (mark-marker) (point-marker))) 0))
+ )
+ ;; find the place first
+ (if (LilyPond-mark-active)
+ (goto-char (min (mark-marker) (point-marker))))
+ (while (and (not found) (> (buffer-size b) (marker-position m)))
+ (setq copy (car (copy-alist (list (eval (symbol-name (read m)))))))
+ (if (string-equal word copy) (setq found t)))
+ (if found (insert word))
+ (if (> (buffer-size b) (marker-position m))
+ (setq copy (car (copy-alist (list (eval (symbol-name (read m))))))))
+ (if (not (string-equal "-" copy))
+ (setq found nil))
+ (while (and found (> (buffer-size b) (marker-position m)))
+ ;; find next symbol
+ (setq copy (car (copy-alist (list (eval (symbol-name (read m)))))))
+ ;; check whether it is the word, or the word has been found
+ (cond
+ ((string-equal "-" copy) (setq found nil))
+ ((string-equal "%" copy) (insert " " (read-string "Give Arguments: ")))
+ ((string-equal "_" copy)
+ (progn
+ (setq p (point))
+ (goto-char (+ p distance))))
+ ((string-equal "\?" copy) (setq query t))
+ ((string-equal "\!" copy) (setq query nil))
+ ((string-equal "\\n" copy)
+ (if (not query)
+ (progn (LilyPond-indent-line) (insert "\n") (LilyPond-indent-line))))
+ ((string-equal "{" copy)
+ (if (not query)
+ (progn (insert " { "))))
+ ((string-equal "}" copy)
+ (if (not query)
+ (progn (insert " } ") (setq query nil) )))
+ ((not query)
+ (insert copy))
+ (query
+ (if (y-or-n-p (concat "Proceed with `" copy "'? "))
+ (progn (insert copy) (setq query nil))))
+ ))
+ (if p (goto-char p))
+ (kill-buffer b))
+)
(defun LilyPond-command-menu-entry (entry)
;; Return LilyPond-command-alist ENTRY as a menu item.
'([ "Midi all" LilyPond-command-all-midi t])
))
+(defun LilyPond-menu-keywords-item (arg)
+ "Make vector for LilyPond-mode-keywords."
+ (vector arg (list 'LilyPond-insert-tag-current arg) :style 'radio :selected (list 'eq 'LilyPond-insert-tag-current arg)))
+
+(defun LilyPond-menu-keywords ()
+ "Make Insert Tag menu.
+
+The Insert Tag -menu is splitted into parts if it is long enough."
+
+ (let ((li (mapcar 'LilyPond-menu-keywords-item LilyPond-menu-keywords))
+ (w (round (sqrt (length LilyPond-menu-keywords))))
+ (splitted '())
+ (imin 0) imax lw rw)
+ (while (< imin (length LilyPond-menu-keywords))
+ (setq imax (- (min (+ imin w) (length LilyPond-menu-keywords)) 1))
+ (setq lw (nth imin LilyPond-menu-keywords))
+ (setq rw (nth imax LilyPond-menu-keywords))
+ (add-to-list 'splitted
+ (let ((l (list (concat (substring lw 0 (min 7 (length lw)))
+ " ... "
+ (substring rw 0 (min 7 (length rw)))))))
+ (while (<= imin imax)
+ (add-to-list 'l (nth imin li))
+ (setq imin (1+ imin)))
+ (reverse l))))
+ (if (> (length LilyPond-menu-keywords) 12) (reverse splitted) li)))
+
;;; LilyPond-mode-menu should not be interactive, via "M-x LilyPond-<Tab>"
(easy-menu-define LilyPond-mode-menu
LilyPond-mode-map
"Menu used in LilyPond mode."
(append '("LilyPond")
- '(("Insert"
- [ "\\notes..." LilyPond-insert-tag-notes t]
- [ "\\score..." LilyPond-insert-tag-score t]
- ["Autocompletion" LilyPond-autocompletion t]
- ))
+ '(["Add index menu" LilyPond-add-imenu-menu])
+ (list (cons "Insert tag"
+ (cons ["Previously selected" LilyPond-insert-tag-current t]
+ (cons "-----"
+ (LilyPond-menu-keywords)))))
'(("Miscellaneous"
+ ["Autocompletion" LilyPond-autocompletion t]
["(Un)comment Region" LilyPond-comment-region t]
["Refontify buffer" font-lock-fontify-buffer t]
- ["Add index menu" LilyPond-add-imenu-menu]
+ "-----"
["Quick Insert Mode" LilyPond-quick-insert-mode :keys "C-c q"]
))
'(("Info"