From e92a8d062cca54152bd816bdf88dad0bade87120 Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Mon, 7 Feb 2022 15:05:14 +0100 Subject: [PATCH] Show show-paren context in overlay or header-line --- lisp/paren.el | 66 ++++++++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 60 insertions(+), 6 deletions(-) diff --git a/lisp/paren.el b/lisp/paren.el index 6de4364b4f..e0a8d6fc24 100644 --- a/lisp/paren.el +++ b/lisp/paren.el @@ -368,6 +368,51 @@ show-paren--show-context-in-child-frame (add-hook 'post-command-hook #'show-paren--delete-context-child-frame)))) +(defvar-local show-paren--context-overlay nil) + +(defun show-paren--delete-context-overlay () + (when show-paren--context-overlay + (delete-overlay show-paren--context-overlay) + (setq show-paren--context-overlay nil)) + (remove-hook 'post-command-hook #'show-paren--delete-overlays + 'local)) + +(defun show-paren--show-context-in-overlay (text) + "Show TEXT in an overlay at the top-left of the current window." + ;; FIXME: This works pretty well but the overlay text is not + ;; outstanding at all. It's good that it's the font-locked string + ;; but it would be good if we could place a :box or :background face + ;; attribute, too. + (setq text (replace-regexp-in-string "\n" " " text)) + (show-paren--delete-context-overlay) + (let* ((beg (window-start)) + (end (save-excursion + (goto-char beg) + (line-end-position)))) + (setq show-paren--context-overlay (make-overlay beg end))) + (overlay-put show-paren--context-overlay 'display text) + (add-hook 'post-command-hook #'show-paren--delete-context-overlay + nil 'local)) + +(defvar-local show-paren--orig-header-line-format nil) + +(defun show-paren--restore-orig-header-line-format () + (setq header-line-format show-paren--orig-header-line-format) + (remove-hook 'post-command-hook + #'show-paren--restore-orig-header-line-format + 'local)) + +(defun show-paren--show-context-in-header-line (text) + "Show TEXT in the header-line." + ;; FIXME: This bounces the buffer one line down which is a bit + ;; annoying. Can we do anything about it? + (setq text (replace-regexp-in-string "\n" " " text)) + (setq show-paren--orig-header-line-format header-line-format) + (setq header-line-format text) + (add-hook 'post-command-hook + #'show-paren--restore-orig-header-line-format + nil 'local)) + (defun show-paren-function () "Highlight the parentheses until the next input arrives." (let ((data (and show-paren-mode (funcall show-paren-data-function)))) @@ -438,12 +483,21 @@ show-paren-function (let ((open-paren-line-string (blink-paren-open-paren-line-string openparen)) (message-log-max nil)) - (if (and (eq show-paren-context-when-offscreen - 'child-frame) - (display-graphic-p)) - (show-paren--show-context-in-child-frame - open-paren-line-string) - (minibuffer-message "Matches %s" open-paren-line-string))))) + (cond ((and (eq show-paren-context-when-offscreen + 'child-frame) + (display-graphic-p)) + (show-paren--show-context-in-child-frame + open-paren-line-string)) + ((eq show-paren-context-when-offscreen + 'overlay) + (show-paren--show-context-in-overlay + open-paren-line-string)) + ((eq show-paren-context-when-offscreen + 'header-line) + (show-paren--show-context-in-header-line + open-paren-line-string)) + (show-paren-context-when-offscreen + (minibuffer-message "Matches %s" open-paren-line-string)))))) ;; Always set the overlay face, since it varies. (overlay-put show-paren--overlay 'priority show-paren-priority) (overlay-put show-paren--overlay 'face face)))))) -- 2.35.1