* 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; 8+ 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] 8+ 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; 8+ 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] 8+ 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
2025-01-18 11:20 ` Ihor Radchenko
0 siblings, 1 reply; 8+ 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] 8+ messages in thread
* Re: Add org-src-is-fontify-buffer-p (patch attached)
2025-01-18 9:27 ` Ship Mints
@ 2025-01-18 11:20 ` Ihor Radchenko
2025-01-18 11:28 ` Ship Mints
0 siblings, 1 reply; 8+ messages in thread
From: Ihor Radchenko @ 2025-01-18 11:20 UTC (permalink / raw)
To: Ship Mints; +Cc: emacs-orgmode
Ship Mints <shipmints@gmail.com> writes:
> 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.
Ok. Fair enough.
> 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.
From my point of view, the interface you suggest is not generic enough.
It only addresses a single use case - Org fontification.
What about another, more general approach - check if buffer name starts
with space? By convention, buffers with names starting with space are
considered internal.
--
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] 8+ messages in thread
* Re: Add org-src-is-fontify-buffer-p (patch attached)
2025-01-18 11:20 ` Ihor Radchenko
@ 2025-01-18 11:28 ` Ship Mints
2025-01-18 11:38 ` Ihor Radchenko
0 siblings, 1 reply; 8+ messages in thread
From: Ship Mints @ 2025-01-18 11:28 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1575 bytes --]
Yes, could do, and nothing needed by org-mode or markdown-mode. Could also
test for buffer-file-name nil. Or test both. Let's recommend that simpler
approach and I will rescind my markdown PR in favor of this simpler
suggestion.
On Sat, Jan 18, 2025 at 6:18 AM Ihor Radchenko <yantar92@posteo.net> wrote:
> Ship Mints <shipmints@gmail.com> writes:
>
> > 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.
>
> Ok. Fair enough.
>
> > 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.
>
> From my point of view, the interface you suggest is not generic enough.
> It only addresses a single use case - Org fontification.
>
> What about another, more general approach - check if buffer name starts
> with space? By convention, buffers with names starting with space are
> considered internal.
>
> --
> 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: 2322 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Add org-src-is-fontify-buffer-p (patch attached)
2025-01-18 11:28 ` Ship Mints
@ 2025-01-18 11:38 ` Ihor Radchenko
2025-01-18 11:54 ` Ship Mints
0 siblings, 1 reply; 8+ messages in thread
From: Ihor Radchenko @ 2025-01-18 11:38 UTC (permalink / raw)
To: Ship Mints; +Cc: emacs-orgmode
Ship Mints <shipmints@gmail.com> writes:
> Yes, could do, and nothing needed by org-mode or markdown-mode. Could also
> test for buffer-file-name nil. Or test both. Let's recommend that simpler
> approach and I will rescind my markdown PR in favor of this simpler
> suggestion.
Well. Org or markdown should name the buffers according to the
convention :) Org does it though. So, we should be good.
Closing the patch request.
Canceled.
--
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] 8+ messages in thread
* Re: Add org-src-is-fontify-buffer-p (patch attached)
2025-01-18 11:38 ` Ihor Radchenko
@ 2025-01-18 11:54 ` Ship Mints
2025-01-18 11:57 ` Ship Mints
0 siblings, 1 reply; 8+ messages in thread
From: Ship Mints @ 2025-01-18 11:54 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1035 bytes --]
For the record, this is what I've recommended and what I will use:
(defun my/XXX-mode-hook ()
(when (or buffer-file-name
(not (string-prefix-p " " (buffer-name))))
;; do something expensive
))
On Sat, Jan 18, 2025 at 6:36 AM Ihor Radchenko <yantar92@posteo.net> wrote:
> Ship Mints <shipmints@gmail.com> writes:
>
> > Yes, could do, and nothing needed by org-mode or markdown-mode. Could
> also
> > test for buffer-file-name nil. Or test both. Let's recommend that simpler
> > approach and I will rescind my markdown PR in favor of this simpler
> > suggestion.
>
> Well. Org or markdown should name the buffers according to the
> convention :) Org does it though. So, we should be good.
>
> Closing the patch request.
> Canceled.
>
> --
> 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: 1984 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
* Re: Add org-src-is-fontify-buffer-p (patch attached)
2025-01-18 11:54 ` Ship Mints
@ 2025-01-18 11:57 ` Ship Mints
0 siblings, 0 replies; 8+ messages in thread
From: Ship Mints @ 2025-01-18 11:57 UTC (permalink / raw)
To: Ihor Radchenko; +Cc: emacs-orgmode
[-- Attachment #1: Type: text/plain, Size: 1341 bytes --]
This could be added to the org-mode documentation as a hint for those who
use native fontification. Should I send a patch for that or trivial enough
you'd do it, if you agree?
On Sat, Jan 18, 2025 at 6:54 AM Ship Mints <shipmints@gmail.com> wrote:
> For the record, this is what I've recommended and what I will use:
>
> (defun my/XXX-mode-hook ()
> (when (or buffer-file-name
> (not (string-prefix-p " " (buffer-name))))
> ;; do something expensive
> ))
>
> On Sat, Jan 18, 2025 at 6:36 AM Ihor Radchenko <yantar92@posteo.net>
> wrote:
>
>> Ship Mints <shipmints@gmail.com> writes:
>>
>> > Yes, could do, and nothing needed by org-mode or markdown-mode. Could
>> also
>> > test for buffer-file-name nil. Or test both. Let's recommend that
>> simpler
>> > approach and I will rescind my markdown PR in favor of this simpler
>> > suggestion.
>>
>> Well. Org or markdown should name the buffers according to the
>> convention :) Org does it though. So, we should be good.
>>
>> Closing the patch request.
>> Canceled.
>>
>> --
>> 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: 2598 bytes --]
^ permalink raw reply [flat|nested] 8+ messages in thread
end of thread, other threads:[~2025-01-18 12:00 UTC | newest]
Thread overview: 8+ 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
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
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).