]> git.donarmstrong.com Git - lilypond.git/blobdiff - lilypond-indent.el
* mf/feta-toevallig.mf: tweak for bottom of flat sign.
[lilypond.git] / lilypond-indent.el
index 5c2e3be45845aa321338312bd57fade7e8c71306..50db7bb3433585674d7e03ccc634e190cec71613 100644 (file)
@@ -1,10 +1,23 @@
 ;;; lilypond-indent.el --- Auto-indentation for lilypond code
 ;;;
+;;; Heikki Junes <hjunes@cc.hut.fi>
+;;; * redefine Emacs' show-paren-function and XEmacs' paren-highlight
+;;; * 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>
 
 ;;; Variables for customising indentation style
 
+;;; 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
+;;;    * is locality of show-paren-function and paren-highlight possible?
+
 (defcustom LilyPond-indent-level 4
   "*Indentation of lilypond statements with respect to containing block.")
 
 Compares with other text in same context.")
 
 (defcustom LilyPond-angle-offset 0
-  "*Extra indentation for open angled brackets .
+  "*Extra indentation for open angled brackets.
+Compares with other text in same context.")
+
+(defcustom LilyPond-square-offset 0
+  "*Extra indentation for open square brackets.
 Compares with other text in same context.")
 
 (defcustom LilyPond-scheme-paren-offset 0
-  "*Extra indentation for open scheme parens .
+  "*Extra indentation for open scheme parens.
 Compares with other text in same context.")
 
 (defcustom LilyPond-close-brace-offset 0
@@ -26,12 +43,37 @@ Compares with other text in same context.")
 (defcustom LilyPond-close-angle-offset 0
   "*Extra indentation for closing angle brackets.")
 
+(defcustom LilyPond-close-square-offset 0
+  "*Extra indentation for closing square brackets.")
+
 (defcustom LilyPond-close-scheme-paren-offset 0
   "*Extra indentation for closing scheme parens.")
 
 (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.
@@ -42,7 +84,7 @@ Returns nil if line starts inside a string"
     (let ((indent-point (point))
          (case-fold-search nil)
          state)
-      (setq containing-sexp (save-excursion (LilyPond-beginning-of-containing-sexp)))
+      (setq containing-sexp (save-excursion (LilyPond-scan-containing-sexp)))
       (beginning-of-defun)
       (while (< (point) indent-point)
        (setq state (parse-partial-sexp (point) indent-point 0)))
@@ -98,6 +140,8 @@ Returns nil if line starts inside a string"
                            LilyPond-brace-offset)
                           ((= (following-char) ?<) 
                            LilyPond-angle-offset)
+                          ((= (following-char) ?[) 
+                           LilyPond-square-offset)
                           ((= (following-char) ?\))
                            LilyPond-scheme-paren-offset)
                           (t
@@ -108,7 +152,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."
@@ -135,12 +178,16 @@ Return the amount the indentation changed by."
               (setq indent  (+ indent (- LilyPond-close-brace-offset LilyPond-indent-level))))
              ((= (following-char) ?>)
               (setq indent  (+ indent (- LilyPond-close-angle-offset LilyPond-indent-level))))
+             ((= (following-char) ?])
+              (setq indent  (+ indent (- LilyPond-close-square-offset LilyPond-indent-level))))
              ((and (= (following-char) ?\)) (LilyPond-inside-scheme-p))
               (setq indent  (+ indent (- LilyPond-close-scheme-paren-offset LilyPond-indent-level))))
              ((= (following-char) ?{)
               (setq indent  (+ indent LilyPond-brace-offset)))
              ((= (following-char) ?<)
               (setq indent  (+ indent LilyPond-angle-offset)))
+             ((= (following-char) ?[)
+              (setq indent  (+ indent LilyPond-square-offset)))
              ((and (= (following-char) ?\() (LilyPond-inside-scheme-p))
               (setq indent  (+ indent LilyPond-scheme-paren-offset)))
              ))))
@@ -249,54 +296,146 @@ Argument LIM limit."
        (current-column)))))
 
 
-(defconst LilyPond-parens-regexp-alist
-  `(("[^\\]<" .  "[^ \\n\\t_^-]\\s-*>\\|[_^-]\\s-*[-^]\\s-*>")
-    ;; a b c->, a b c^> and a b c_> are not close-angle-brackets, they're accents
-    ;; but a b c^-> and a b c^^> are close brackets with tenuto/marcato before them
-    ;; also \> and \< are hairpins
-    ("{" . "}")))
-
-
-(defconst LilyPond-parens-combined-regexp
-  (concat (mapconcat 'car LilyPond-parens-regexp-alist "\\|")
-         "\\|"
-         (mapconcat 'cdr LilyPond-parens-regexp-alist "\\|")))
+;; Key:   Type of bracket (character). 
+;; Value: Pair of regexps representing the corresponding open and close bracket
+;; () are treated specially (need to indent in Scheme but not in music)
 
-
-(defun LilyPond-beginning-of-containing-sexp ()
-  "Move point to the beginning of the deepest parenthesis pair enclosing point."
-  (interactive)
-  (let ((level 1))
+(defconst LilyPond-parens-regexp-alist
+  `( ( ?>  .  ("\\([^\\]\\|^\\)<" . "\\([^ \\n\\t_^-]\\|[_^-][-^]\\|\\s-\\)\\s-*>"))
+     ;; a b c->, a b c^> and a b c_> are not close-angle-brackets, they're accents
+     ;; but a b c^-> and a b c^^> are close brackets with tenuto/marcato before them
+     ;; also \> and \< are hairpins
+     ;; duh .. a single '>', as in chords '<< ... >>', was not matched here
+     ( ?}  .  ("{" . "}"))
+     ;; ligatures  '\[ ... \]' are skipped in the following expression
+     ( ?]  .  ("\\([^\\]\\([\\][\\]\\)*\\|^\\)[[]" . "\\([^\\]\\([\\][\\]\\)*\\|^\\)[]]"))
+     ( "\\]" . ("\\([^\\]\\|^\\)\\([\\][\\]\\)*[\\][[]" . "\\([^\\]\\|^\\)\\([\\][\\]\\)*[\\][]]"))
+     ( "\\)" . ("\\([^\\]\\|^\\)\\([\\][\\]\\)*[\\][(]" . "\\([^\\]\\|^\\)\\([\\][\\]\\)*[\\][)]"))
+     ))
+
+
+(defconst LilyPond-parens-alist
+  `( ( ?<  .  ?> )    
+     ( ?{  .  ?} )    
+     ( ?[  .  ?] )
+     ( "\\["  .  "\\]" )
+     ( ?\(  .  ?\) )
+     ( "\\("  .  "\\)" )
+     ))
+
+
+(defun LilyPond-matching-paren (bracket-type)
+  "Returns the open corresponding to the close specified by bracket-type, or vice versa"
+  (cond ( (member bracket-type (mapcar 'car LilyPond-parens-alist))
+         (cdr (assoc bracket-type LilyPond-parens-alist)) )
+       ( (member bracket-type (mapcar 'cdr LilyPond-parens-alist))
+         (car (rassoc bracket-type LilyPond-parens-alist)) )
+       nil))
+
+
+(defun LilyPond-scan-containing-sexp (&optional bracket-type slur-paren-p dir)
+  "Move point to the beginning of the deepest parenthesis pair enclosing point. 
+
+If the optional argument bracket-type, a character representing a
+close bracket such as ) or }, is specified, then the parenthesis pairs
+searched are limited to this type.
+
+If the optional argument slur-paren-p is non-nil, then slur
+parentheses () are considered as matching pairs. Otherwise Scheme
+parentheses are considered to be matching pairs, but slurs are not.
+slur-paren-p defaults to nil.
+"
+;;; An user does not call this function directly, or by a key sequence.
+  ;;  (interactive)
+  (let ( (level (if (not (eq dir 1)) 1 -1))
+        (regexp-alist LilyPond-parens-regexp-alist) 
+        (oldpos (point))
+        (assoc-bracket-type (if (not (eq dir 1)) bracket-type (LilyPond-matching-paren bracket-type))))
+    
     (if (LilyPond-inside-scheme-p)
-       (setq paren-regexp "(\\|)" inside-scheme t)
-      (setq paren-regexp LilyPond-parens-combined-regexp inside-scheme nil))
-    (while (and (> level 0) 
-               (re-search-backward paren-regexp nil t)
+       (setq paren-regexp "(\\|)")
+      (if slur-paren-p
+         ;; expressional slurs  '\( ... \)' are not taken into account
+         (setq regexp-alist (cons '( ?\) . ("\\([^\\]\\([\\][\\]\\)*\\|^\\)(" . "\\([^\\]\\([\\][\\]\\)*\\|^\\))")) regexp-alist)))
+      (if (member assoc-bracket-type (mapcar 'car regexp-alist))
+         (progn (setq paren-regexp (cdr (assoc assoc-bracket-type regexp-alist)))
+                (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)
+            (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))))
-      (if (not (save-excursion (goto-char (match-end 0)) 
+;;;      (message "%d" level) (sit-for 0 300)
+      (if (not (save-excursion (goto-char (match-end 0))
+                              ;; skip over strings and comments
                               (LilyPond-inside-string-or-comment-p)))
-         (if (memq match '(?} ?> ?\)))
+         (if (memq match '(?} ?> ?] ?\)))
+             ;; count closing brackets
              (progn (setq level (1+ level))
+                    ;; 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-+>\\|\\({\\|}\\|<\\|>\\|(\\|)\\)>"))
+                             (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-+<\\|\\({\\|}\\|<\\|>\\|(\\|)\\)<"))
+                           (looking-at ".\\s-+<\\|\\({\\|}\\|<\\|>\\|(\\|)\\|[][]\\)<"))
                       (forward-char 1))))))
-    (if (looking-at ".<\\|.>") (forward-char 1))
-    (if (/= level 1) 
+    ;; jump to the matching slur
+    (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 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)
-      nil)))
+      (progn (goto-char oldpos)
+            nil))))
 
 
 (defun LilyPond-inside-scheme-p ()
   "Tests if point is inside embedded Scheme code"
-  (interactive)
+;;; An user does not call this function directly, or by a key sequence.
+  ;;  (interactive)
   (let ( (test-point (point))
         (level 0) )
     (save-excursion 
-      (if (or (and (= (char-after (point)) ?\()
+      (if (or (and (/= (point) (point-max))
+                  (= (char-after (point)) ?\()
                   (or (= (char-after (- (point) 1)) ?#)
                       (and (= (char-after (- (point) 2)) ?#)
                            (= (char-after (- (point) 1)) ?`))))
@@ -314,3 +453,328 @@ Argument LIM limit."
                     (> level 0))))
          t
        nil))))
+
+
+;;; Largely taken from the 'blink-matching-open' in lisp/simple.el in
+;;; the Emacs distribution.
+
+(defun LilyPond-blink-matching-paren (&optional dir)
+  "Move cursor momentarily to the beginning of the sexp before
+point. In lilypond files this is used for closing ), ], } and >, whereas the
+builtin 'blink-matching-open' is not used. In syntax table, see
+`lilypond-font-lock.el', all brackets are punctuation characters."
+;;; An user does not call this function directly, or by a key sequence.
+  ;;  (interactive)
+  (let ( (oldpos (point))
+        (level 0) 
+        (mismatch) )
+    (if (not (or (equal this-command 'LilyPond-electric-close-paren)
+                (eq dir 1)))
+       (goto-char (setq oldpos (- oldpos 1))))
+    ;; Test if a ligature \] or expressional slur \) was encountered
+    (setq bracket-type (char-after (point)))
+    (setq char-before-bracket-type nil)
+    (if (memq bracket-type '(?] ?\) ?[ ?\())
+      (progn 
+       (setq np -1)
+       (while (eq (char-before (- (point) (setq np (+ np 1)))) ?\\)
+         (setq char-before-bracket-type (if char-before-bracket-type nil ?\\)))
+        (if (eq char-before-bracket-type ?\\)
+           (setq bracket-type (string char-before-bracket-type bracket-type)))))
+    (when blink-matching-paren-distance
+      (narrow-to-region
+       (max (point-min) (- (point) blink-matching-paren-distance))
+       (min (point-max) (+ (point) blink-matching-paren-distance))))
+    (if (and (equal this-command 'LilyPond-electric-close-paren)
+            (memq bracket-type '(?> ?} ?< ?{)))
+       ;; < { need to be mutually balanced and nested, so search backwards for both of these bracket types 
+       (LilyPond-scan-containing-sexp nil nil dir)  
+      ;; whereas ( ) slurs within music don't, so only need to search for ( )
+      ;; use same mechanism for [ ] slurs
+      (LilyPond-scan-containing-sexp bracket-type t dir))
+    (setq blinkpos (point))
+    (setq mismatch
+         (or (null (LilyPond-matching-paren (char-after blinkpos)))
+             (/= (char-after oldpos)
+                 (LilyPond-matching-paren (char-after blinkpos)))))
+    (if mismatch (progn (setq blinkpos nil)
+                       (message "Mismatched parentheses")))
+    (if (and blinkpos
+            (equal this-command 'LilyPond-electric-close-paren))
+       (if (pos-visible-in-window-p)
+           (and blink-matching-paren-on-screen
+                (sit-for blink-matching-delay))
+         (message
+          "Matches %s"
+          ;; Show what precedes the open in its line, if anything.
+          (if (save-excursion
+                (skip-chars-backward " \t")
+                (not (bolp)))
+              (buffer-substring (progn (beginning-of-line) (point))
+                                (1+ blinkpos))
+            ;; Show what follows the open in its line, if anything.
+            (if (save-excursion
+                  (forward-char 1)
+                  (skip-chars-forward " \t")
+                  (not (eolp)))
+                (buffer-substring blinkpos
+                                  (progn (end-of-line) (point)))
+              ;; Otherwise show the previous nonblank line,
+              ;; if there is one.
+              (if (save-excursion
+                    (skip-chars-backward "\n \t")
+                    (not (bobp)))
+                  (concat
+                   (buffer-substring (progn
+                                       (skip-chars-backward "\n \t")
+                                       (beginning-of-line)
+                                       (point))
+                                     (progn (end-of-line)
+                                            (skip-chars-backward " \t")
+                                            (point)))
+                   ;; Replace the newline and other whitespace with `...'.
+                   "..."
+                   (buffer-substring blinkpos (1+ blinkpos)))
+                ;; There is nothing to show except the char itself.
+                (buffer-substring blinkpos (1+ blinkpos))))))))
+    (if (not (equal this-command 'LilyPond-electric-close-paren))
+       (goto-char (setq oldpos (+ oldpos 1)))
+      (goto-char oldpos))
+    (if (not (eq dir 1))
+       blinkpos
+      (+ blinkpos 1))))
+
+
+(defun LilyPond-electric-close-paren ()
+  "Blink on the matching open paren when a >, ), } or ] is inserted"
+  (interactive)
+  (let ((oldpos (point)))
+    (self-insert-command 1)
+    ;; 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))
+
+;;; REDEFINITIONS: in future make show-paren-mode and paren-highlight local?
+
+;;; From Emacs' paren.el, with minimal changes (see "LilyPond"-lines)
+;; Find the place to show, if there is one,
+;; and show it until input arrives.
+; (defun show-paren-function ()
+
+
+;;  don't redefine emacs functions. It breaks other modes.
+
+(defun LilyPond-show-paren-function () ; make show-paren-function local ??
+  (if show-paren-mode
+      (let (pos dir mismatch face (oldpos (point)))
+       (cond ((eq (char-syntax (preceding-char)) ?\))
+              (setq dir -1))
+             ((eq (char-syntax (following-char)) ?\()
+              (setq dir 1)))
+       ;;
+       ;; Find the other end of the sexp.
+       (when (and dir
+                  (not (LilyPond-inside-string-or-comment-p)))
+         (save-excursion
+           (save-restriction
+             ;; Determine the range within which to look for a match.
+             (when blink-matching-paren-distance
+               (narrow-to-region
+                (max (point-min) (- (point) blink-matching-paren-distance))
+                (min (point-max) (+ (point) blink-matching-paren-distance))))
+             ;; Scan across one sexp within that range.
+             ;; Errors or nil mean there is a mismatch.
+             (condition-case ()
+                 (setq pos (LilyPond-scan-sexps (point) dir))
+               (error (setq pos t mismatch t)))
+             ;; If found a "matching" paren, see if it is the right
+             ;; kind of paren to match the one we started at.
+             (when (integerp pos)
+               (let ((beg (min pos oldpos)) (end (max pos oldpos)))
+                 (when (/= (char-syntax (char-after beg)) ?\$)
+                   (setq mismatch
+                         (not (eq (char-before end)
+                                  ;; This can give nil.
+                                  (matching-paren (char-after beg)))))))))))
+       ;;
+       ;; Highlight the other end of the sexp, or unhighlight if none.
+       (if (not pos)
+           (progn
+             ;; If not at a paren that has a match,
+             ;; turn off any previous paren highlighting.
+             (and show-paren-overlay (overlay-buffer show-paren-overlay)
+                  (delete-overlay show-paren-overlay))
+             (and show-paren-overlay-1 (overlay-buffer show-paren-overlay-1)
+                  (delete-overlay show-paren-overlay-1)))
+         ;;
+         ;; Use the correct face.
+         (if mismatch
+             (progn
+               (if show-paren-ring-bell-on-mismatch
+                   (beep))
+               (setq face 'show-paren-mismatch-face))
+           (setq face 'show-paren-match-face))
+         ;;
+         ;; If matching backwards, highlight the closeparen
+         ;; before point as well as its matching open.
+         ;; If matching forward, and the openparen is unbalanced,
+         ;; highlight the paren at point to indicate misbalance.
+         ;; Otherwise, turn off any such highlighting.
+         (if (and (= dir 1) (integerp pos))
+             (when (and show-paren-overlay-1
+                        (overlay-buffer show-paren-overlay-1))
+               (delete-overlay show-paren-overlay-1))
+           (let ((from (if (= dir 1)
+                           (point)
+                         (forward-point -1)))
+                 (to (if (= dir 1)
+                         (forward-point 1)
+                       (point))))
+             (if show-paren-overlay-1
+                 (move-overlay show-paren-overlay-1 from to (current-buffer))
+               (setq show-paren-overlay-1 (make-overlay from to)))
+             ;; Always set the overlay face, since it varies.
+             (overlay-put show-paren-overlay-1 'priority show-paren-priority)
+             (overlay-put show-paren-overlay-1 'face face)))
+         ;;
+         ;; Turn on highlighting for the matching paren, if found.
+         ;; If it's an unmatched paren, turn off any such highlighting.
+         (unless (integerp pos)
+           (delete-overlay show-paren-overlay))
+         (let ((to (if (or (eq show-paren-style 'expression)
+                           (and (eq show-paren-style 'mixed)
+                                (not (pos-visible-in-window-p pos))))
+                       (point)
+                     pos))
+               (from (if (or (eq show-paren-style 'expression)
+                             (and (eq show-paren-style 'mixed)
+                                  (not (pos-visible-in-window-p pos))))
+                         pos
+                       (save-excursion
+                         (goto-char pos)
+                         (forward-point (- dir))))))
+           (if show-paren-overlay
+               (move-overlay show-paren-overlay from to (current-buffer))
+             (setq show-paren-overlay (make-overlay from to))))
+         ;;
+         ;; Always set the overlay face, since it varies.
+         (overlay-put show-paren-overlay 'priority show-paren-priority)
+         (overlay-put show-paren-overlay 'face face)))
+    ;; show-paren-mode is nil in this buffer.
+    (and show-paren-overlay
+        (delete-overlay show-paren-overlay))
+    (and show-paren-overlay-1
+        (delete-overlay show-paren-overlay-1))))
+
+;;; From XEmacs' paren.el, with minimal changes (see "LilyPond"-lines)
+;; Find the place to show, if there is one,
+;; and show it until input arrives.
+(if (string-match "XEmacs\\|Lucid" emacs-version)
+    (paren-set-mode 'paren)) ; works if this is set here (, right place?)
+;(defun paren-highlight ()
+(defun LilyPond-paren-highlight () ; make paren-highlight local ??
+  "This highlights matching parentheses.
+
+See the variables:
+  paren-message-offscreen   use modeline when matching paren is offscreen?
+  paren-ding-unmatched     make noise when passing over mismatched parens?
+  paren-mode               'blink-paren, 'paren, or 'sexp
+  blink-matching-paren-distance  maximum distance to search for parens.
+
+and the following faces:
+  paren-match, paren-mismatch, paren-blink-off"
+
+  ;; I suppose I could check here to see if a keyboard macro is executing,
+  ;; but I did a quick empirical check and couldn't tell that there was any
+  ;; difference in performance
+
+  (let ((oldpos (point))
+       (pface nil)                     ; face for paren...nil kills the overlay
+       (dir (and paren-mode
+                 (not (input-pending-p))
+                 (not executing-kbd-macro)
+                 (cond ((eq (char-syntax (preceding-char)) ?\))
+                        -1)
+                       ((eq (char-syntax (following-char)) ?\()
+                        1))))
+       pos mismatch)
+
+    (save-excursion
+      (if (or (not dir)
+             (LilyPond-inside-string-or-comment-p)
+             (not (save-restriction
+                    ;; Determine the range within which to look for a match.
+                    (if blink-matching-paren-distance
+                        (narrow-to-region
+                         (max (point-min)
+                              (- (point) blink-matching-paren-distance))
+                         (min (point-max)
+                              (+ (point) blink-matching-paren-distance))))
+
+                    ;; Scan across one sexp within that range.
+                    (condition-case nil
+                        (setq pos (LilyPond-scan-sexps (point) dir))
+                      ;; NOTE - if blink-matching-paren-distance is set,
+                      ;; then we can have spurious unmatched parens.
+                      (error (paren-maybe-ding)
+                             nil)))))
+
+         ;; do nothing if we didn't find a matching paren...
+         nil
+
+       ;; See if the "matching" paren is the right kind of paren
+       ;; to match the one we started at.
+       (let ((beg (min pos oldpos)) (end (max pos oldpos)))
+         (setq mismatch
+               (and (/= (char-syntax (char-after beg)) ?\\)
+                    (/= (char-syntax (char-after beg)) ?\$)
+                    ;; XEmacs change
+                    (matching-paren (char-after beg))
+                    (/= (char-after (1- end))
+                        (matching-paren (char-after beg)))))
+         (if (eq paren-mode 'sexp)
+             (setq paren-extent (make-extent beg end))))
+       (and mismatch
+            (paren-maybe-ding))
+       (setq pface (if mismatch
+                       'paren-mismatch
+                     'paren-match))
+       (and (memq paren-mode '(blink-paren paren))
+            (setq paren-extent (make-extent (- pos dir) pos)))
+
+       (if (and paren-message-offscreen
+                (eq dir -1)
+                 (not (current-message))
+                (not (window-minibuffer-p (selected-window)))
+                (not (pos-visible-in-window-safe pos)))
+            (paren-describe-match pos mismatch))
+                
+       ;; put the right face on the extent
+       (cond (pface
+              (set-extent-face paren-extent pface) 
+              (set-extent-priority paren-extent 100) ; want this to be high
+              (and (eq paren-mode 'blink-paren)
+                   (setq paren-blink-on-face pface
+                         paren-n-blinks 0
+                         paren-timeout-id
+                         (and paren-blink-interval
+                              (add-timeout paren-blink-interval
+                                           'paren-blink-timeout
+                                           nil
+                                           paren-blink-interval))))))
+       ))))