From 1274da95c81d293554fddf4ecc172ec4b161965b Mon Sep 17 00:00:00 2001 From: shipmints Date: Fri, 17 Jan 2025 17:52:28 -0500 Subject: [PATCH 1/3] Add defun markdown-is-fontify-buffer-p This is extremely useful in a `prog-mode' hook to avoid resource-intensive features such as `eglot' inside a fontification buffer. --- markdown-mode.el | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/markdown-mode.el b/markdown-mode.el index a2626231..ba0f7174 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -9137,6 +9137,29 @@ Use matching function MATCHER." "Add text properties to next tilde fenced code block from point to LAST." (markdown-fontify-code-blocks-generic 'markdown-match-fenced-code-blocks last)) +(defvar-local markdown--is-fontify-buffer nil) +(put 'markdown--is-fontify-buffer 'permanent-local t) ; needs to survive major-mode housecleaning + +;;;###autoload +(defun markdown-is-fontify-buffer-p (&optional buffer) + "Return t if the current buffer is a code-block fontify BUFFER. + +If BUFFER is nil, the current buffer is used. + +This is useful in a `prog-mode' hook to avoid resource-intensive +features such as `eglot' inside a fontification buffer. + +Example: + (unless (and (featurep 'markdown-mode) + (markdown-is-fontify-buffer-p)) + (eglot-ensure))" + (buffer-local-value 'markdown--is-fontify-buffer + (or buffer (current-buffer)))) + +(unless (and (featurep 'markdown-mode) + (markdown-is-fontify-buffer-p)) + (eglot-ensure)) + ;; Based on `org-src-font-lock-fontify-block' from org-src.el. (defun markdown-fontify-code-block-natively (lang start end) "Fontify given GFM or fenced code block. @@ -9160,7 +9183,9 @@ position." (let ((inhibit-modification-hooks nil)) (delete-region (point-min) (point-max)) (insert string " ")) ;; so there's a final property change - (unless (eq major-mode lang-mode) (funcall lang-mode)) + (setq markdown--is-fontify-buffer t) ; let the mode know this is a fontify buffer + (unless (eq major-mode lang-mode) + (funcall lang-mode)) (font-lock-ensure) (setq pos (point-min)) (while (setq next (next-single-property-change pos 'face)) From 1e949a0990c8540aa635a7cc516312e3b09a6ca0 Mon Sep 17 00:00:00 2001 From: shipmints Date: Fri, 17 Jan 2025 17:55:20 -0500 Subject: [PATCH 2/3] Make the byte compiler happier --- markdown-mode.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/markdown-mode.el b/markdown-mode.el index ba0f7174..7270790d 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -9150,7 +9150,7 @@ This is useful in a `prog-mode' hook to avoid resource-intensive features such as `eglot' inside a fontification buffer. Example: - (unless (and (featurep 'markdown-mode) + (unless (and (featurep \\='markdown-mode) (markdown-is-fontify-buffer-p)) (eglot-ensure))" (buffer-local-value 'markdown--is-fontify-buffer From fcd5397018019a5024d4cc48012e426e62e4e773 Mon Sep 17 00:00:00 2001 From: shipmints Date: Fri, 17 Jan 2025 18:13:56 -0500 Subject: [PATCH 3/3] Erroneous code removed. Feel free to squash these commits. --- markdown-mode.el | 4 ---- 1 file changed, 4 deletions(-) diff --git a/markdown-mode.el b/markdown-mode.el index 7270790d..f9d217a2 100644 --- a/markdown-mode.el +++ b/markdown-mode.el @@ -9156,10 +9156,6 @@ Example: (buffer-local-value 'markdown--is-fontify-buffer (or buffer (current-buffer)))) -(unless (and (featurep 'markdown-mode) - (markdown-is-fontify-buffer-p)) - (eglot-ensure)) - ;; Based on `org-src-font-lock-fontify-block' from org-src.el. (defun markdown-fontify-code-block-natively (lang start end) "Fontify given GFM or fenced code block.