让 emacs 在 } else { 之后显示注释

让 emacs 在 } else { 之后显示注释

最好通过一个例子来描述这一点。假设您有以下 C 源文件:

if (something is true)
{
  meh();
} else if (something else is true) {
  foo();
} else { // something even different must be true
  bar();
}

现在在 emacs 中,当你输入最后一个时},它会说Matches } else {

我怎样才能让它说出整行,即Matches } else { // something even different must be true

提前致谢。

答案1

将此更改为'blink-matching-open可实现您的要求。我已将其重命名为,'my-blink-matching-open以便您不会丢失原始内容。定义之后是使此例程生效的设置。

(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)

答案2

我对 emacs 没有太多经验,所以我不知道这是否可行,但我认为绝对值得一试:尝试将注释放在 else 和花括号之间:

} else /* something even different must be true */ {

相关内容