From: Leo Liu <sdl.web@gmail.com>
To: Stefan Monnier <monnier@iro.umontreal.ca>
Cc: 14395@debbugs.gnu.org
Subject: bug#14395: 24.3; [PATCH] new feature smie-highlight-matching-block
Date: Thu, 16 May 2013 11:27:31 +0800 [thread overview]
Message-ID: <m1wqqzd2os.fsf@gmail.com> (raw)
In-Reply-To: <jwvsj1nk6qc.fsf-monnier+emacs@gnu.org> (Stefan Monnier's message of "Wed, 15 May 2013 22:31:47 -0400")
On 2013-05-16 10:31 +0800, Stefan Monnier wrote:
>> +(define-minor-mode smie-highlight-matching-block-mode nil
>
> Please provide a docstring.
Is the automatically-provided docstring good enough?
New patch as follows.
diff --git a/lisp/emacs-lisp/smie.el b/lisp/emacs-lisp/smie.el
index bbdd9f83..de91c21f 100644
--- a/lisp/emacs-lisp/smie.el
+++ b/lisp/emacs-lisp/smie.el
@@ -1021,6 +1021,85 @@ (defun smie-blink-matching-open ()
(let ((blink-matching-check-function #'smie-blink-matching-check))
(blink-matching-open))))))))
+(defface smie-matching-block-highlight '((t (:inherit highlight)))
+ "Face used to highlight matching block."
+ :group 'smie)
+
+(defvar-local smie--highlight-matching-block-overlay nil)
+(defvar-local smie--highlight-matching-block-lastpos -1)
+
+(defun smie-highlight-matching-block ()
+ (when (and smie-closer-alist
+ (/= (point) smie--highlight-matching-block-lastpos))
+ (unless (overlayp smie--highlight-matching-block-overlay)
+ (setq smie--highlight-matching-block-overlay
+ (make-overlay (point) (point))))
+ (setq smie--highlight-matching-block-lastpos (point))
+ (let ((beg-of-tok
+ (lambda (&optional start)
+ "Move to the beginning of current token at START."
+ (let* ((token)
+ (start (or start (point)))
+ (beg (progn
+ (funcall smie-backward-token-function)
+ (forward-comment (point-max))
+ (point)))
+ (end (progn
+ (setq token (funcall smie-forward-token-function))
+ (forward-comment (- (point)))
+ (point))))
+ (if (and (<= beg start) (<= start end)
+ (or (assoc token smie-closer-alist)
+ (rassoc token smie-closer-alist)))
+ (progn (goto-char beg) token)
+ (goto-char start)
+ nil))))
+ (highlight
+ (lambda (beg end)
+ (move-overlay smie--highlight-matching-block-overlay beg end)
+ (overlay-put smie--highlight-matching-block-overlay
+ 'face 'smie-matching-block-highlight))))
+ (save-excursion
+ (condition-case nil
+ (if (nth 8 (syntax-ppss))
+ (overlay-put smie--highlight-matching-block-overlay 'face nil)
+ (let ((token
+ (or (funcall beg-of-tok)
+ (funcall beg-of-tok
+ (prog1 (point)
+ (funcall smie-forward-token-function))))))
+ (cond
+ ((assoc token smie-closer-alist) ; opener
+ (forward-sexp 1)
+ (let ((end (point))
+ (closer (funcall smie-backward-token-function)))
+ (when (rassoc closer smie-closer-alist)
+ (funcall highlight (point) end))))
+ ((rassoc token smie-closer-alist) ; closer
+ (funcall smie-forward-token-function)
+ (forward-sexp -1)
+ (let ((beg (point))
+ (opener (funcall smie-forward-token-function)))
+ (when (assoc opener smie-closer-alist)
+ (funcall highlight beg (point)))))
+ (t (overlay-put smie--highlight-matching-block-overlay
+ 'face nil)))))
+ (scan-error
+ (overlay-put smie--highlight-matching-block-overlay 'face nil)))))))
+
+(defvar smie--highlight-matching-block-timer nil)
+
+;;;###autoload
+(define-minor-mode smie-highlight-matching-block-mode nil :global t
+ (when (timerp smie--highlight-matching-block-timer)
+ (cancel-timer smie--highlight-matching-block-timer))
+ (if smie-highlight-matching-block-mode
+ (setq smie--highlight-matching-block-timer
+ (run-with-idle-timer 0.2 t #'smie-highlight-matching-block))
+ (when (timerp smie--highlight-matching-block-timer)
+ (cancel-timer smie--highlight-matching-block-timer))
+ (setq smie--highlight-matching-block-timer nil)))
+
;;; The indentation engine.
(defcustom smie-indent-basic 4
@@ -1698,8 +1777,11 @@ (defun smie-setup (grammar rules-function &rest keywords)
;; Only needed for interactive calls to blink-matching-open.
(set (make-local-variable 'blink-matching-check-function)
#'smie-blink-matching-check)
- (add-hook 'post-self-insert-hook
- #'smie-blink-matching-open 'append 'local)
+ (if smie-highlight-matching-block-mode
+ (remove-hook 'post-self-insert-hook
+ #'smie-blink-matching-open 'local)
+ (add-hook 'post-self-insert-hook
+ #'smie-blink-matching-open 'append 'local))
(set (make-local-variable 'smie-blink-matching-triggers)
(append smie-blink-matching-triggers
;; Rather than wait for SPC to blink, try to blink as
next prev parent reply other threads:[~2013-05-16 3:27 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2013-05-14 2:49 bug#14395: 24.3; [PATCH] new feature smie-highlight-matching-block Leo Liu
2013-05-14 16:02 ` Stefan Monnier
2013-05-15 7:13 ` Leo Liu
2013-05-16 2:31 ` Stefan Monnier
2013-05-16 3:27 ` Leo Liu [this message]
2013-05-16 13:24 ` Stefan Monnier
2013-05-16 16:06 ` Leo Liu
2013-05-16 17:51 ` Stefan Monnier
2013-05-16 23:09 ` Leo Liu
2013-05-17 0:40 ` Stefan Monnier
2013-05-17 1:30 ` Leo Liu
2013-05-22 18:50 ` Stefan Monnier
2013-05-16 4:45 ` Glenn Morris
2013-05-16 5:33 ` Leo Liu
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=m1wqqzd2os.fsf@gmail.com \
--to=sdl.web@gmail.com \
--cc=14395@debbugs.gnu.org \
--cc=monnier@iro.umontreal.ca \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
Code repositories for project(s) associated with this public inbox
https://git.savannah.gnu.org/cgit/emacs.git
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).