5

Rainbow-delimiters is a nice library for showing matching parens. It has the nice property that if you have extra right parens, it will color them in some loud color.

e.g. in ( )) the second ) will show up red. I would love it if the other direction worked too. I.e., (( ) showed the first paren bright red. Is there any way to tweak rainbow-delimiters to do this, or some other package that I could use to do so?

4

2 回答 2

2

实际的问题是不清楚或者很难确定哪个开始分隔符是错误的并且必须突出显示。请注意,在示例(()中,第一个或第二个括号可能是错误的。可能是第三个括号关闭了第二个括号,而第一个括号的右括号丢失了,或者第一个括号与第三个括号匹配,而第二个的右括号丢失了。

接下来是一个肮脏的技巧,它突出显示了if提供rainbow-delimiters的区域中的第一个开头分隔符,该分隔符给出了错误。对错误分隔符的猜测可能是错误的。但是,也许破解总比没有好。jit-lockscan-sexps

(defadvice rainbow-delimiters-propertize-region (after hack-unmatching-open activate)
  "Also mark unmatching open delimiters. Fast fragile hack, not regarding type of delimiter."
  (save-excursion
    (goto-char start)
    (while (and
        (< (point) end)
        (re-search-forward "[[{(]" end t)) ;;< first opening delimiter within region
      (let ((pos (1- (point)))) ;; Note, syntax-ppss does shift position anyway. So, avoiding this does not make sense.
    (unless (save-excursion (rainbow-delimiters-char-ineligible-p pos))
      (condition-case err
          (goto-char (scan-sexps pos 1))
        (scan-error
         (rainbow-delimiters-apply-color (cdr (assoc (char-before) '((?\( . "paren") (?\[ . "bracket") (?\{ . "brace")))) -1 pos))))))))

编辑:我已包含scan-sexps在测试中,并在文件末尾删除了深度测试。我的印象是稍微改善了行为。

于 2013-11-16T18:21:19.630 回答
0

Tobias 的建议似乎针对的是rainbow-delimiters. 我不确定是否对最近的版本有很大影响。

这是一个非常接近解决上述问题的解决方案,它通过使用highlight-parentheses包以及rainbow-delimiters. 您可以保存此代码并使用以下命令对其进行测试:

emacs -Q -l ~/debug-rainbow-delimiters.el -f emacs-lisp-mode

(setq debug-on-error t)

(when (>= emacs-major-version 24)
  (require 'package)
  (add-to-list 'package-archives 
               '("melpa" . "http://melpa.milkbox.net/packages/") t)
  (package-initialize)
  )

(require 'highlight-parentheses)

(setq hl-paren-colors '("dodger blue"))

(set-face-attribute 'hl-paren-face nil
                    :foreground 'unspecified
                    :inherit 'show-paren-mismatch)

(require 'paren)
(require 'rainbow-delimiters)
(set-face-attribute 'rainbow-delimiters-unmatched-face nil
                    :foreground 'unspecified
                    :inherit 'show-paren-mismatch)

(defvar my-paren-colors '("dodger blue"))

(setq rainbow-delimiters-max-face-count 1)

(set-face-foreground 'rainbow-delimiters-depth-1-face (elt my-paren-colors 1))

;; Hook the above into various Lisp modes

(add-hook 'lisp-mode-hook 'highlight-parentheses)
(add-hook 'lisp-interaction-mode-hook 'highlight-parentheses)
(add-hook 'emacs-lisp-mode-hook 'highlight-parentheses-mode)
(add-hook 'clojure-mode-hook 'highlight-parentheses-mode)

(add-hook 'lisp-mode-hook 'rainbow-delimiters-mode)
(add-hook 'lisp-interaction-mode-hook 'rainbow-delimiters-mode)
(add-hook 'emacs-lisp-mode-hook 'rainbow-delimiters-mode)
(add-hook 'clojure-mode-hook 'rainbow-delimiters-mode)

;;; Custom

(custom-set-variables
   '(package-selected-packages
     '(highlight-parentheses rainbow-delimiters)))

解决方案并不完美——匹配取决于光标的位置。


在此处输入图像描述


在此处输入图像描述


你可以混淆它:

在此处输入图像描述


也许最重要的是,它具有始终突出显示一级括号的副作用:

在此处输入图像描述


我个人的评估是,这可能会很有帮助如果有更复杂的解决方案,那就太好了!

于 2020-02-28T21:49:51.183 回答