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)
{
  meh();
} else if (something else is true) {
  foo();
} else { // something even different must be true
  bar();
}

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."
  (interactive)
  (when (and (> (point) (point-min))
             blink-matching-paren
             ;; Verify an even number of quoting characters precede the close.
             (= 1 (logand 1 (- (point)
                               (save-excursion
                                 (forward-char -1)
                                 (skip-syntax-backward "/\\")
                                 (point))))))
    (let* ((oldpos (point))
           (message-log-max nil)  ; Don't log messages about paren matching.
           (atdollar (eq (syntax-class (syntax-after (1- oldpos))) 8))
           (isdollar)
           (blinkpos
            (save-excursion
              (save-restriction
                (if blink-matching-paren-distance
                    (narrow-to-region
                     (max (minibuffer-prompt-end) ;(point-min) unless minibuf.
                          (- (point) blink-matching-paren-distance))
                     oldpos))
                (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))))))
           (matching-paren
            (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))))))
      (cond
       ;; isdollar is for:
       ;; http://lists.gnu.org/archive/html/emacs-devel/2007-10/msg00871.html
       ((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.
            atdollar
            (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)
             (save-excursion
               (goto-char blinkpos)
               (sit-for blink-matching-delay))))
       (t
        (save-excursion
          (goto-char blinkpos)
          (let ((open-paren-line-string
                 (cond
                  ;; When there's something following the brace
                  ;; show the entire line
                  ((save-excursion
                     (forward-char 1)
                     (skip-chars-forward " \t")
                     (not (eolp)))
                   (buffer-substring (line-beginning-position)
                                     (line-end-position)))
                  ;; Otherwise show the previous nonblank line,
                  ;; if there is one.
                  ((save-excursion (skip-chars-backward "\n \t") (not (bobp)))
                   (concat
                    (buffer-substring (progn
                                        (skip-chars-backward "\n \t")
                                        (line-beginning-position))
                                      (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.
                  (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 .