Hi All,

Since the contentious component of my previous patches has been the
`{{{results()}}}' prettification, I’ve prepared an alternate version that I’m
hoping everybody will be fairly happy with (fingers crossed!) that tosses out
the results prettification for now.

I think Protesilaos’ comments on making some new faces deserve consideration,
but could easily be done subsequently, as I’m quite keen to get this merged for
now.

All the best,
Timothy
>From 53d2851e248c4f4e4076878a999cbf647b401578 Mon Sep 17 00:00:00 2001
From: TEC <t...@tecosaur.com>
Date: Tue, 13 Jul 2021 02:43:29 +0800
Subject: [PATCH] org-src: Implement native inline src fontification

* lisp/org-src.el (org-fontify-inline-src-blocks,
org-fontify-inline-src-blocks-1): Create a function to search the buffer
up to a limit for inline src blocks.  Light fontification is applied to
matched inline src blocks.  When `org-src-fontify-natively' is
set, `org-src-font-lock-fontify-block' is applied to the content.

* lisp/org.el (org-set-font-lock-defaults): Add
`org-fontify-inline-src-blocks' to `org-font-lock-extra-keywords', which
is locally bound inside `org-set-font-lock-defaults'.
(org-inline-src-fontify-max-length): Create a variable to limit the
maximum length of an inline-src block fontified, to protect from lag
spikes (e.g. when typing out src_lang{ and half of the buffer is
fontified).
---
 lisp/org-src.el | 46 ++++++++++++++++++++++++++++++++++++++++++++++
 lisp/org.el     | 18 ++++++++++++++++++
 2 files changed, 64 insertions(+)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index 51dde602d..c3a6a40bc 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -654,6 +654,52 @@ (defun org-src-font-lock-fontify-block (lang start end)
 	 '(font-lock-fontified t fontified t font-lock-multiline t))
 	(set-buffer-modified-p modified)))))
 
+(defun org-fontify-inline-src-blocks (limit)
+  "Try to apply `org-fontify-inline-src-blocks-1'."
+  (condition-case nil
+      (progn
+        (org-fontify-inline-src-blocks-1 limit)
+        (org-fontify-inline-src-results limit))
+    (error (message "Org mode fontification error in %S at %d"
+                    (current-buffer)
+                    (line-number-at-pos)))))
+
+(defun org-fontify-inline-src-blocks-1 (limit)
+  "Fontify inline src_LANG blocks, from `point' up to LIMIT."
+  (let ((case-fold-search t)
+        (initial-point (point)))
+    (when (re-search-forward "\\_<src_\\([^ \t\n[{]+\\)[{[]?" limit t) ; copied from `org-element-inline-src-block-parser'
+      (let ((beg (match-beginning 0))
+            (lang-beg (match-beginning 1))
+            (lang-end (match-end 1))
+            pt)
+        (font-lock-append-text-property lang-beg lang-end 'face 'org-meta-line)
+        (font-lock-append-text-property beg lang-beg 'face 'shadow)
+        (font-lock-append-text-property beg lang-end 'face 'org-block)
+        (setq pt (goto-char lang-end))
+        ;; `org-element--parse-paired-brackets' doesn't take a limit, so to
+        ;; prevent it searching the entire rest of the buffer we temporarily
+        ;; narrow the active region.
+        (save-restriction
+          (narrow-to-region beg (min (point-max)
+                                     limit
+                                     (+ lang-end org-inline-src-fontify-max-length)))
+          (when (ignore-errors (org-element--parse-paired-brackets ?\[))
+            (font-lock-append-text-property pt (point) 'face 'org-block)
+            (setq pt (point)))
+          (when (ignore-errors (org-element--parse-paired-brackets ?\{))
+            (remove-text-properties pt (point) '(face nil))
+            (font-lock-append-text-property pt (1+ pt) 'face '(org-block shadow))
+            (unless (= (1+ pt) (1- (point)))
+              (if org-src-fontify-natively
+                  (org-src-font-lock-fontify-block
+                   (buffer-substring-no-properties lang-beg lang-end)
+                   (1+ pt) (1- (point)))
+                (font-lock-append-text-property (1+ pt) (1- (point)) 'face 'org-block)))
+            (font-lock-append-text-property (1- (point)) (point)'face '(org-block shadow))
+            (setq pt (point)))))
+      t)))
+
 
 ;;; Escape contents
 
diff --git a/lisp/org.el b/lisp/org.el
index eeefb4af3..6a424991c 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5279,6 +5279,23 @@ (defcustom org-allow-promoting-top-level-subtree nil
   :version "24.1"
   :group 'org-appearance)
 
+(defcustom org-inline-src-fontify-max-length 200
+  "Maximum content length of an inline src block that will be fontified.
+This is only relevant when `org-src-fontify-natively' is t."
+  :type 'integer
+  :package-version '(Org . "9.5")
+  :group 'org-appearance
+  :group 'org-babel)
+
+(defcustom org-inline-src-prettify-results t
+  "Whether to use (ab)use prettify-symbols-mode on {{{results(...)}}}.
+Either t or a cons cell of strings which are used as substitutions
+for the start and end of inline results, respectively."
+  :type '(choice boolean (cons string string))
+  :package-version '(Org . "9.5")
+  :group 'org-appearance
+  :group 'org-babel)
+
 (defun org-fontify-meta-lines-and-blocks (limit)
   (condition-case nil
       (org-fontify-meta-lines-and-blocks-1 limit)
@@ -5785,6 +5802,7 @@ (defun org-set-font-lock-defaults ()
 		'(9 'org-special-keyword t))
 	  ;; Blocks and meta lines
 	  '(org-fontify-meta-lines-and-blocks)
+          '(org-fontify-inline-src-blocks)
           ;; Citations
           '(org-cite-activate))))
     (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
-- 
2.33.1

Reply via email to