Tom Gillespie <tgb...@gmail.com> writes:

> 1. I think there needs to be a function to toggle
> org-inline-src-prettify-results as there is e.g. for hyperlinks. I was
> quite confused by the prettified results.

Added org-toggle-inline-results-display.

> 3. I'm not sure about the default choice for prettified delimiters. I
> see there is already a way to customize the delimiters by providing a
> cons. I think a default value of '("" . "") might be a better choice
> since ⟨ and ⟩ being hardcoded seems like it introduces completely
> alien characters. Going with empty strings also seems consistent with
> the behavior for hyperlinks.

Changed to your suggestion.

Awaiting others' thoughts on 2. and 5.

--
Timothy

>From 81c56a48ebe516890691420243efe966f3c50eef Mon Sep 17 00:00:00 2001
From: TEC <t...@tecosaur.com>
Date: Mon, 3 May 2021 11:16:17 +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' to the content.
(org-fontify-inline-src-results): Search for {{{results(...)}}}
constructs.  Then when `org-inline-src-prettify-results` is non-nil,
mimic prettify-symbols and use `compose-region' to substitute visually
simpler elements for the wrapping around the value.

* 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, org-inline-src-prettify-results):
Create variables for use in the new inline src/result fontification
methods in org-src.el.
---
 lisp/org-src.el | 79 +++++++++++++++++++++++++++++++++++++++++++++++++
 lisp/org.el     | 21 ++++++++++++-
 2 files changed, 99 insertions(+), 1 deletion(-)

diff --git a/lisp/org-src.el b/lisp/org-src.el
index a694e5595..1d09f03a8 100644
--- a/lisp/org-src.el
+++ b/lisp/org-src.el
@@ -623,6 +623,85 @@ (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
+      (org-fontify-inline-src-blocks-1 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)))
+    (while (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)
+        (remove-text-properties beg lang-end '(face nil))
+        (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 ?\[))
+            (remove-text-properties pt (point) '(face nil))
+            (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))))
+        (when (and org-inline-src-prettify-results
+                   (re-search-forward "\\= {{{results(" limit t))
+          (font-lock-append-text-property pt (1+ pt) 'face 'org-block)
+          (goto-char pt))))
+    (when org-inline-src-prettify-results
+      (goto-char initial-point)
+      (org-fontify-inline-src-results limit))))
+
+(defun org-fontify-inline-src-results (limit)
+  "Apply prettify-symbols modifications to inline results blocks.
+Performed according to `org-inline-src-prettify-results'."
+  (while (re-search-forward "{{{results(\\(.+?\\))}}}" limit t)
+    (remove-list-of-text-properties (match-beginning 0) (point)
+                                    '(composition
+                                      prettify-symbols-start
+                                      prettify-symbols-end))
+    (font-lock-append-text-property (match-beginning 0) (match-end 0)
+                                    'face 'org-block)
+    (let ((start (match-beginning 0)) (end (match-beginning 1)))
+      (with-silent-modifications
+        (compose-region start end (if (eq org-inline-src-prettify-results t)
+                                      "(" (car org-inline-src-prettify-results)))
+        (add-text-properties start end `(prettify-symbols-start ,start prettify-symbols-end ,end))))
+    (let ((start (match-end 1)) (end (point)))
+      (with-silent-modifications
+        (compose-region start end (if (eq org-inline-src-prettify-results t)
+                                      ")" (cdr org-inline-src-prettify-results)))
+        (add-text-properties start end `(prettify-symbols-start ,start prettify-symbols-end ,end))))))
+
+(defun org-toggle-inline-results-display ()
+  "Toggle the literal or contracted display of inline src blocks results."
+  (interactive)
+  (setq org-inline-src-prettify-results (not org-inline-src-prettify-results))
+  (org-restart-font-lock))
+
 
 ;;; Escape contents
 
diff --git a/lisp/org.el b/lisp/org.el
index 10eeae514..ab817a0a7 100644
--- a/lisp/org.el
+++ b/lisp/org.el
@@ -5239,6 +5239,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)
@@ -5739,7 +5756,9 @@ (defun org-set-font-lock-defaults ()
 		  org-comment-string)
 		 '(9 'org-special-keyword t))
 	   ;; Blocks and meta lines
-	   '(org-fontify-meta-lines-and-blocks))))
+	   '(org-fontify-meta-lines-and-blocks)
+           ;; Inline src blocks
+           '(org-fontify-inline-src-blocks))))
     (setq org-font-lock-extra-keywords (delq nil org-font-lock-extra-keywords))
     (run-hooks 'org-font-lock-set-keywords-hook)
     ;; Now set the full font-lock-keywords
-- 
2.31.1

Reply via email to