]> git.donarmstrong.com Git - lilypond.git/blobdiff - lilypond-indent.el
move new-beam-event to beam-event
[lilypond.git] / lilypond-indent.el
index 1940e72ab264e58af82cc2e1470ae79b3d5945d9..4626646d06a7139dd40c96cb843cdf71dc8ceae9 100644 (file)
@@ -1,8 +1,9 @@
 ;;; lilypond-indent.el --- Auto-indentation for lilypond code
 ;;;
 ;;; Heikki Junes <hjunes@cc.hut.fi>
-;;; * redefine Emacs' show-paren-function and XEmacs' paren-highlight
+;;; * ond-char paren matching is handled by context dependent syntax tables
 ;;; * match two-char slurs '\( ... \)' and '\[ ... \]' separately.
+;;; * adopt Emacs' f90-comment-region
 
 ;;; Chris Jackson <chris@fluffhouse.org.uk>
 ;;; some code is taken from ESS (Emacs Speaks Statistics) S-mode by A.J.Rossini <rossini@biostat.washington.edu>
@@ -12,9 +13,6 @@
 ;;; TODO:
 ;;;    * currently, in bracket matching one may need a non-bracket 
 ;;;      chararacter between the bracket characters, like ( ( ) )
-;;;    * in syntax-highlighting slurs are not always highlighted the right way
-;;;      e.g. opening slurs are found found better in "#( ( ) ( ) )" than
-;;;      opening slurs
 
 (defcustom LilyPond-indent-level 4
   "*Indentation of lilypond statements with respect to containing block.")
@@ -50,6 +48,28 @@ Compares with other text in same context.")
 (defcustom LilyPond-fancy-comments t
   "*Non-nil means distiguish between %, %%, and %%% for indentation.")
 
+(defcustom LilyPond-comment-region "%%$"
+  "*String inserted by \\[LilyPond-comment-region]\
+ at start of each line in region.")
+
+(defun LilyPond-comment-region (beg-region end-region)
+  "Comment/uncomment every line in the region.
+Insert LilyPond-comment-region at the beginning of every line in the region
+or, if already present, remove it."
+  (interactive "*r")
+  (let ((end (make-marker)))
+    (set-marker end end-region)
+    (goto-char beg-region)
+    (beginning-of-line)
+    (if (looking-at (regexp-quote LilyPond-comment-region))
+       (delete-region (point) (match-end 0))
+      (insert LilyPond-comment-region))
+    (while (and  (zerop (forward-line 1))
+                (< (point) (marker-position end)))
+      (if (looking-at (regexp-quote LilyPond-comment-region))
+         (delete-region (point) (match-end 0))
+       (insert LilyPond-comment-region)))
+    (set-marker end nil)))
 
 (defun LilyPond-calculate-indent ()
   "Return appropriate indentation for current line as lilypond code.
@@ -128,7 +148,6 @@ Returns nil if line starts inside a string"
                   (current-indentation)))))))))
 
 
-
 (defun LilyPond-indent-line ()
   "Indent current line as lilypond code.
 Return the amount the indentation changed by."
@@ -339,33 +358,51 @@ slur-paren-p defaults to nil.
                 (setq paren-regexp (concat (car paren-regexp) "\\|" (cdr paren-regexp))))
        (setq paren-regexp (concat (mapconcat 'car (mapcar 'cdr regexp-alist) "\\|") "\\|"
                                   (mapconcat 'cdr (mapcar 'cdr regexp-alist) "\\|")))))
+    ;; match concurrent one-char opening and closing slurs
     (if (and (eq dir 1)
-            (memq (char-after oldpos) '(?[ ?{)))
-       (forward-char 1))
+            (not (sequencep bracket-type))
+            (eq (char-syntax (char-after oldpos)) ?\()
+            (not (eq (char-after oldpos) ?<)))
+       ;; anyway do not count open slur, since already level = -1
+        (progn (forward-char 1)
+              (if (eq (following-char) 
+                      (LilyPond-matching-paren (char-after oldpos)))
+                  ;; matching char found, go after it and set level = 0
+                  (progn (forward-char 1)
+                         (setq level 0)))))
+    ;; browse the code until matching slur is found, or report mismatch
     (while (and (if (not (eq dir 1)) 
                    (> level 0) 
                  (< level 0))
+               ;; dir tells whether to search backward or forward
                (if (not (eq dir 1))
                    (re-search-backward paren-regexp nil t)
                  (re-search-forward paren-regexp nil t))
+               ;; note: in case of two-char bracket only latter is compared
                (setq match (char-before (match-end 0))))
 ;;;      (message "%d" level) (sit-for 0 300)
-      (if (not (save-excursion (goto-char (match-end 0)) 
+      (if (not (save-excursion (goto-char (match-end 0))
+                              ;; skip over strings and comments
                               (LilyPond-inside-string-or-comment-p)))
          (if (memq match '(?} ?> ?] ?\)))
+             ;; count closing brackets
              (progn (setq level (1+ level))
-                    ;; single '>' was not matched .. need to correct
+                    ;; slurs may be close to each other, e.g.,
+                    ;; a single '>' was not matched .. need to be corrected
                     (if (and (eq dir 1) (eq (char-after (match-end 0)) match))
                         (if (/= level 0)
                             (progn
                               (setq level (1+ level))
                               (forward-char 1))))
 ;;;                 (message "%d %c" level match) (sit-for 0 300)
+                    ;; hmm..
                     (if (and (= match ?>) 
                              (looking-at ".\\s-+>\\|\\({\\|}\\|<\\|>\\|(\\|)\\|[][]\\)>"))
                         (forward-char 1)))
+           ;; count opening brackets
            (progn (setq level (1- level))
 ;;;               (message "%d %c" level match) (sit-for 0 300)
+                  ;; hmm..
                   (if (and (= match ?<)
                            (looking-at ".\\s-+<\\|\\({\\|}\\|<\\|>\\|(\\|)\\|[][]\\)<"))
                       (forward-char 1))))))
@@ -373,8 +410,12 @@ slur-paren-p defaults to nil.
     (if (not (eq dir 1))
        (progn
          (if (sequencep bracket-type)
+             ;; match the latter char in two-char brackets
              (if (looking-at "..[][)(]") (forward-char 1)))
-         (if (looking-at ".[][><)(]") (forward-char 1)))
+         ;; if the following char is not already a slur
+         (if (and (not (looking-at "[)(]"))
+                  ;; match the slur which follows
+                  (looking-at ".[][><)(]")) (forward-char 1)))
       (backward-char 1))
     (if (= level 0) 
        (point)
@@ -505,36 +546,21 @@ builtin 'blink-matching-open' is not used. In syntax table, see
   (interactive)
   (let ((oldpos (point)))
     (self-insert-command 1)
-    (if (and blink-matching-paren
-            (not (LilyPond-inside-string-or-comment-p))
-            (save-excursion (re-search-backward 
-                             (concat (mapconcat 'cdr (mapcar 'cdr LilyPond-parens-regexp-alist) "\\|") "\\|)") nil t)
-                            (eq oldpos (1- (match-end 0)))))
-       (progn (backward-char 1)
-              (LilyPond-blink-matching-paren)
-              (forward-char 1)))))
-
-;;; REDEFINITIONS
-(defun scan-sexps (pos dir) 
+    ;; Refontify buffer if a block-comment-ender '%}' is inserted
+    (if (and (eq (char-before (point)) ?})
+            (eq (char-before (- (point) 1)) ?%))
+       (font-lock-fontify-buffer)
+      ;; Match paren if the cursor is not inside string or comment.
+      (if (and blink-matching-paren
+              (not (LilyPond-inside-string-or-comment-p))
+              (save-excursion (re-search-backward 
+                               (concat (mapconcat 'cdr (mapcar 'cdr LilyPond-parens-regexp-alist) "\\|") "\\|)") nil t)
+                              (eq oldpos (1- (match-end 0)))))
+         (progn (backward-char 1)
+                (LilyPond-blink-matching-paren)
+                (forward-char 1))))))
+
+(defun LilyPond-scan-sexps (pos dir) 
   "This function is redefined to be used in Emacs' show-paren-function and
 in XEmacs' paren-highlight."
   (LilyPond-blink-matching-paren dir))
-
-;; Emacs and XEmacs have slightly different names for parenthesis highlighting.
-(if (not (string-match "XEmacs\\|Lucid" emacs-version))
-    (progn
-      (fset 'old-show-paren-function (symbol-function 'show-paren-function))
-      (defun show-paren-function ()
-      "Highlights the matching slur if cursor is moved before opening or 
-after closing slur. In this redefinition strings and comments are skipped."
-      (if (not (LilyPond-inside-string-or-comment-p))
-         (old-show-paren-function))))
-  (progn
-    ;; NOTE: paren-set-mode must be set before paren-highlight is redefined
-    (paren-set-mode 'paren)
-    (fset 'old-paren-highlight (symbol-function 'paren-highlight))
-    (defun paren-highlight ()
-      "Highlights the matching slur if cursor is moved before opening or 
-after closing slur. In this redefinition strings and comments are skipped."
-      (if (not (LilyPond-inside-string-or-comment-p))
-         (old-paren-highlight)))))