emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
* Add org-src-is-fontify-buffer-p (patch attached)
@ 2025-01-18  0:39 Ship Mints
  2025-01-18  7:29 ` Ihor Radchenko
  0 siblings, 1 reply; 3+ messages in thread
From: Ship Mints @ 2025-01-18  0:39 UTC (permalink / raw)
  To: emacs-orgmode


[-- 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


^ permalink raw reply related	[flat|nested] 3+ messages in thread

* Re: Add org-src-is-fontify-buffer-p (patch attached)
  2025-01-18  0:39 Add org-src-is-fontify-buffer-p (patch attached) Ship Mints
@ 2025-01-18  7:29 ` Ihor Radchenko
  2025-01-18  9:27   ` Ship Mints
  0 siblings, 1 reply; 3+ messages in thread
From: Ihor Radchenko @ 2025-01-18  7:29 UTC (permalink / raw)
  To: Ship Mints; +Cc: emacs-orgmode

Ship Mints <shipmints@gmail.com> writes:

> 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)))

Thanks!

Do you have other examples where the new predicate could be useful apart
from eglot? For eglot specifically, one can do a much simpler test -
check for `buffer-file-name'.

-- 
Ihor Radchenko // yantar92,
Org mode maintainer,
Learn more about Org mode at <https://orgmode.org/>.
Support Org development at <https://liberapay.com/org-mode>,
or support my work at <https://liberapay.com/yantar92>


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Add org-src-is-fontify-buffer-p (patch attached)
  2025-01-18  7:29 ` Ihor Radchenko
@ 2025-01-18  9:27   ` Ship Mints
  0 siblings, 0 replies; 3+ messages in thread
From: Ship Mints @ 2025-01-18  9:27 UTC (permalink / raw)
  To: Ihor Radchenko; +Cc: emacs-orgmode

[-- Attachment #1: Type: text/plain, Size: 1541 bytes --]

Eglot was the first request cited. For python programmers that rely on per
project/directory-local or per-buffer virtual environment set up, this can
be heavier than needed just for fontification. I enable eglot manually, but
I do use python virtual environments and would disable initialization in
fontification buffers.

I'd prefer a more generic interface than propagating org internals like the
format of the fortification buffer name. It's what I submitted to the
markdown repo also. The buffer name scheme can change in the future without
impacting this feature.

-Stephane

On Sat, Jan 18, 2025 at 2:27 AM Ihor Radchenko <yantar92@posteo.net> wrote:

> Ship Mints <shipmints@gmail.com> writes:
>
> > 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)))
>
> Thanks!
>
> Do you have other examples where the new predicate could be useful apart
> from eglot? For eglot specifically, one can do a much simpler test -
> check for `buffer-file-name'.
>
> --
> Ihor Radchenko // yantar92,
> Org mode maintainer,
> Learn more about Org mode at <https://orgmode.org/>.
> Support Org development at <https://liberapay.com/org-mode>,
> or support my work at <https://liberapay.com/yantar92>
>

[-- Attachment #2: Type: text/html, Size: 2540 bytes --]

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2025-01-18  9:30 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-01-18  0:39 Add org-src-is-fontify-buffer-p (patch attached) Ship Mints
2025-01-18  7:29 ` Ihor Radchenko
2025-01-18  9:27   ` Ship Mints

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).