emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Ship Mints <shipmints@gmail.com>
To: emacs-orgmode@gnu.org
Subject: Add org-src-is-fontify-buffer-p (patch attached)
Date: Fri, 17 Jan 2025 19:39:01 -0500	[thread overview]
Message-ID: <CAN+1HboBH2JzNT0Op3z995zZnVrsuE_Vd6ZRjvtsV9vs8vUPSQ@mail.gmail.com> (raw)


[-- Attachment #1.1: Type: text/plain, Size: 952 bytes --]

Greetings, org-mode maintainers,

I've become an occasional contributor over on the Emacs side. I thought I'd
give an org-mode contribution a try.

The attached patch came out of the discussion here
https://www.reddit.com/r/emacs/comments/1i3mk6m/disable_eglot_in_orgmode_source_blocks/

I've added the predicate function org-src-is-fontify-buffer-p which can be
used in a prog-mode hook to avoid resource-intensive features such as eglot
inside a fontification buffer. This short example should make it clear.

(defun my/emacs-lisp-hook ()
  (unless (and (featurep 'org) (org-src-is-fontify-buffer-p))
    (eglot-ensure)))

I made a similar patch, submitted via a github pull request, to the
markdown-mode maintainers for the same functionality in their fontification
buffers.

The patch was tested against today's main. I hope you find this useful. Let
me know your feedback.

Thanks for org-mode,

-Stephane Marks

P.S. I am on record with the FSF.

[-- Attachment #1.2: Type: text/html, Size: 2447 bytes --]

[-- Attachment #2: 0001-Add-predicate-function-org-src-is-fontify-buffer-p.patch --]
[-- Type: application/octet-stream, Size: 2717 bytes --]

From 745f9b6720d099c714ce92f8f82f43f66711df4a Mon Sep 17 00:00:00 2001
From: shipmints <shipmints@gmail.com>
Date: Fri, 17 Jan 2025 19:32:17 -0500
Subject: [PATCH] Add predicate function org-src-is-fontify-buffer-p

This can be used in a prog-mode hook to avoid resource-intensive
features such as eglot inside a fontification buffer. A short example
can be found in the function's docstring and in the corresponding NEWS
entry.
---
 etc/ORG-NEWS    |  6 ++++++
 lisp/org-src.el | 20 ++++++++++++++++++++
 2 files changed, 26 insertions(+)

diff --git a/etc/ORG-NEWS b/etc/ORG-NEWS
index adc48f304..0911c3bc9 100644
--- a/etc/ORG-NEWS
+++ b/etc/ORG-NEWS
@@ -200,6 +200,12 @@ take the date as an argument, and generate a list of pairs for
 ~org-datetree-find-create-hierarchy~.  This allows for creating new
 types of datetrees (e.g. for lunar calendars, academic calendars,
 retail 4-4-5 calendars, etc).
+*** Predicate test for fontify buffer
+
+The predicate function ~org-src-is-fontify-buffer-p~ can be used in a
+`prog-mode' hook to avoid resource-intensive features such as `eglot'
+inside a fontification buffer. A short example can be found in the
+function's docstring.
 
 ** New and changed options
 
diff --git a/lisp/org-src.el b/lisp/org-src.el
index 74343bde1..6459fb935 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -660,6 +660,25 @@ Leave point in edit buffer."
 \f
 ;;; Fontification of source blocks
 
+(defvar-local org-src--is-fontify-buffer nil)
+(put 'org-src--is-fontify-buffer 'permanent-local t) ; needs to survive major-mode housecleaning
+
+;;;###autoload
+(defun org-src-is-fontify-buffer-p (&optional buffer)
+  "Return t if the current buffer is a source 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 \\='org)
+               (org-src-is-fontify-buffer-p))
+    (eglot-ensure))"
+  (buffer-local-value 'org-src--is-fontify-buffer
+                      (or buffer (current-buffer))))
+
 (defvar org-src-fontify-natively) ; Defined in org.el
 (defun org-src-font-lock-fontify-block (lang start end)
   "Fontify code block between START and END using LANG's syntax.
@@ -678,6 +697,7 @@ as `org-src-fontify-natively' is non-nil."
 	      (erase-buffer)
 	      ;; Add string and a final space to ensure property change.
 	      (insert string " "))
+            (setq org-src--is-fontify-buffer t) ; let the mode know this is a fontify buffer
 	    (unless (eq major-mode lang-mode) (funcall lang-mode))
             (setq native-tab-width tab-width)
             (font-lock-ensure)
-- 
2.47.1


             reply	other threads:[~2025-01-18  5:53 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-01-18  0:39 Ship Mints [this message]
2025-01-18  7:29 ` Add org-src-is-fontify-buffer-p (patch attached) Ihor Radchenko
2025-01-18  9:27   ` Ship Mints
2025-01-18 11:20     ` Ihor Radchenko
2025-01-18 11:28       ` Ship Mints
2025-01-18 11:38         ` Ihor Radchenko
2025-01-18 11:54           ` Ship Mints
2025-01-18 11:57             ` Ship Mints
2025-01-18 12:15           ` Jens Schmidt

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.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=CAN+1HboBH2JzNT0Op3z995zZnVrsuE_Vd6ZRjvtsV9vs8vUPSQ@mail.gmail.com \
    --to=shipmints@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    /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/org-mode.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).