emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: leo@relevant-information.com
To: emacs-orgmode@gnu.org
Cc: Leo Okawa Ericson <git@relevant-information.com>
Subject: [PATCH 1/1] org-src.el: Add option to delay fontification of source blocks
Date: Thu, 25 Mar 2021 19:25:02 +0100	[thread overview]
Message-ID: <20210325182502.107089-2-leo@relevant-information.com> (raw)
In-Reply-To: <20210325182502.107089-1-leo@relevant-information.com>

From: Leo Okawa Ericson <git@relevant-information.com>

* lisp/org-src.el (org-src-font-lock-fontify-block): Add option to delay
fontification of source blocks.  If
`org-src-font-lock-fontify-idle-delay' is non-nil fontification of
code blocks is delayed until the user has become
idle.

Fontification of source blocks can be very slow. This will add an option
for users to delay the fontification until they have become idle so that
the typing delay is kept low. The trade-off is that there will be no
syntax highlighting when the user is typing.
---
 lisp/org-src.el | 29 +++++++++++++++++++++++++++++
 1 file changed, 29 insertions(+)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index 20acee4e6..b1446e105 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -584,11 +584,40 @@ (defun org-src--edit-element
 
 \f
 ;;; Fontification of source blocks
+(defvar org-src-font-lock-fontify-idle-timer nil
+  "Idle timer to use for when fontifying with a timer.")
+
+
+(defvar org-src-font-lock-fontify-idle-delay nil
+  "Duration of the delay until fontification of source blocks.
+If non-nil, source blocks are fontified when the user has been
+idle for `org-src-font-lock-fontify-idle-delay' seconds. This
+means that instead of applying syntax highlighting when you type
+it is delayed until you become idle. Not that when typing there
+will be no fontification.
+")
 
 (defun org-src-font-lock-fontify-block (lang start end)
   "Fontify code block.
 This function is called by emacs automatic fontification, as long
 as `org-src-fontify-natively' is non-nil."
+  (if org-src-font-lock-fontify-idle-delay
+      (progn
+        (when org-src-font-lock-fontify-idle-timer
+          (cancel-timer org-src-font-lock-fontify-idle-timer))
+        (setq org-src-font-lock-fontify-idle-timer
+              (let ((buf (current-buffer)))
+                (run-with-idle-timer
+                 org-src-font-lock-fontify-idle-delay
+                 nil
+                 (lambda ()
+                   (with-current-buffer buf
+                     (org-src-font-lock-fontify-block-1 lang start end)
+                     (when org-src-font-lock-fontify-idle-timer
+                       (cancel-timer org-src-font-lock-fontify-idle-timer)) ))))))
+    (org-src-font-lock-fontify-block-1 lang start end)))
+
+(defun org-src-font-lock-fontify-block-1 (lang start end)
   (let ((lang-mode (org-src-get-lang-mode lang)))
     (when (fboundp lang-mode)
       (let ((string (buffer-substring-no-properties start end))
-- 
2.25.1



  reply	other threads:[~2021-03-25 18:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-25 18:25 [PATCH 0/1] " leo
2021-03-25 18:25 ` leo [this message]
2021-04-25  3:49 ` Timothy
  -- strict thread matches above, loose matches on Subject: below --
2021-03-25 17:43 Leo Okawa Ericson
2021-03-25 17:43 ` [PATCH 1/1] org-src.el: " Leo Okawa Ericson
2021-03-25 17:19 [PATCH 0/1] " Leo Okawa Ericson
2021-03-25 17:19 ` [PATCH 1/1] org-src.el: " Leo Okawa Ericson

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=20210325182502.107089-2-leo@relevant-information.com \
    --to=leo@relevant-information.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=git@relevant-information.com \
    --subject='Re: [PATCH 1/1] org-src.el: Add option to delay fontification of source blocks' \
    /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

Code repositories for project(s) associated with this 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).