Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

This is best described on an example. Let's say you have the following C source file:

if (something is true)
} else if (something else is true) {
} else { // something even different must be true

Now in emacs, when you type the last }, it will say Matches } else {.

How can I make it say the whole line, ie. Matches } else { // something even different must be true?

Thanks in advance.

share|improve this question
up vote 2 down vote accepted

This change to 'blink-matching-open does what you want. I've renamed it to 'my-blink-matching-open so you don't lose the original. Following the definition is the setting that makes this routine take effect.

(defun my-blink-matching-open ()
  "Move cursor momentarily to the beginning of the sexp before point."
  (when (and (> (point) (point-min))
             ;; Verify an even number of quoting characters precede the close.
             (= 1 (logand 1 (- (point)
                                 (forward-char -1)
                                 (skip-syntax-backward "/\\")
    (let* ((oldpos (point))
           (message-log-max nil)  ; Don't log messages about paren matching.
           (atdollar (eq (syntax-class (syntax-after (1- oldpos))) 8))
                (if blink-matching-paren-distance
                     (max (minibuffer-prompt-end) ;(point-min) unless minibuf.
                          (- (point) blink-matching-paren-distance))
                (let ((parse-sexp-ignore-comments
                       (and parse-sexp-ignore-comments
                            (not blink-matching-paren-dont-ignore-comments))))
                  (condition-case ()
                      (scan-sexps oldpos -1)
                    (error nil))))))
            (and blinkpos
                 ;; Not syntax '$'.
                 (not (setq isdollar
                            (eq (syntax-class (syntax-after blinkpos)) 8)))
                 (let ((syntax (syntax-after blinkpos)))
                   (and (consp syntax)
                        (eq (syntax-class syntax) 4)
                        (cdr syntax))))))
       ;; isdollar is for:
       ((not (or (and isdollar blinkpos)
                 (and atdollar (not blinkpos)) ; see below
                 (eq matching-paren (char-before oldpos))
                 ;; The cdr might hold a new paren-class info rather than
                 ;; a matching-char info, in which case the two CDRs
                 ;; should match.
                 (eq matching-paren (cdr (syntax-after (1- oldpos))))))
        (if (minibufferp)
            (minibuffer-message " [Mismatched parentheses]")
          (message "Mismatched parentheses")))
       ((not blinkpos)
        (or blink-matching-paren-distance
            ;; Don't complain when `$' with no blinkpos, because it
            ;; could just be the first one typed in the buffer.
            (if (minibufferp)
                (minibuffer-message " [Unmatched parenthesis]")
              (message "Unmatched parenthesis"))))
       ((pos-visible-in-window-p blinkpos)
        ;; Matching open within window, temporarily move to blinkpos but only
        ;; if `blink-matching-paren-on-screen' is non-nil.
        (and blink-matching-paren-on-screen
             (not show-paren-mode)
               (goto-char blinkpos)
               (sit-for blink-matching-delay))))
          (goto-char blinkpos)
          (let ((open-paren-line-string
                  ;; When there's something following the brace
                  ;; show the entire line
                     (forward-char 1)
                     (skip-chars-forward " \t")
                     (not (eolp)))
                   (buffer-substring (line-beginning-position)
                  ;; Otherwise show the previous nonblank line,
                  ;; if there is one.
                  ((save-excursion (skip-chars-backward "\n \t") (not (bobp)))
                    (buffer-substring (progn
                                        (skip-chars-backward "\n \t")
                                      (progn (end-of-line)
                                             (skip-chars-backward " \t")
                    ;; Replace the newline and other whitespace with `...'.
                    (buffer-substring blinkpos (1+ blinkpos))))
                  ;; There is nothing to show except the char itself.
                  (t (buffer-substring blinkpos (1+ blinkpos))))))
            (message "Matches %s"
                     (substring-no-properties open-paren-line-string)))))))))

(setq blink-paren-function 'my-blink-matching-open)
share|improve this answer
Awesome, thanks! – houbysoft Aug 30 '10 at 12:20

I don't have much experience with emacs, so I don't know if this would work, but I think it's definitely worth a try: try putting the comment between the else and the curly brace:

} else /* something even different must be true */ {
share|improve this answer
That worked, thanks. However, I prefer putting it at the end (and have existing code where it's like that), so I'm going to just upvote this for now :) – houbysoft Aug 30 '10 at 1:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .