emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Timothy <tecosaur@gmail.com>
To: Tom Gillespie <tgbugs@gmail.com>
Cc: org-mode-email <emacs-orgmode@gnu.org>
Subject: Re: [PATCH] Fontification for inline src blocks
Date: Mon, 03 May 2021 11:29:06 +0800	[thread overview]
Message-ID: <878s4wtrzx.fsf@gmail.com> (raw)
In-Reply-To: <CA+G3_PMta3Sgx-Hdz8h4=Giftk7TBsfB9TkftCFHfStFuJvsrQ@mail.gmail.com>

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


Tom Gillespie <tgbugs@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


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: 0001-org-src-Implement-native-inline-src-fontification.patch --]
[-- Type: text/x-patch, Size: 7451 bytes --]

From 81c56a48ebe516890691420243efe966f3c50eef Mon Sep 17 00:00:00 2001
From: TEC <tec@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))
+
 \f
 ;;; 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


  parent reply	other threads:[~2021-05-03  3:29 UTC|newest]

Thread overview: 43+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-31 15:00 [PATCH] Fontification for inline src blocks Timothy
2021-04-28  7:14 ` Timothy
2021-05-02 20:17   ` Timothy
2021-05-02 20:57     ` Tom Gillespie
2021-05-02 21:03       ` Timothy
2021-05-02 21:13         ` Tom Gillespie
2021-05-02 23:54           ` Tom Gillespie
2021-05-03  3:29       ` Timothy [this message]
2021-05-12 11:15         ` Timothy
2021-05-12 14:24           ` Ihor Radchenko
2021-05-12 14:47             ` Timothy
2021-05-12 15:53               ` Ihor Radchenko
2021-05-12 16:39                 ` Timothy
2021-05-13  2:38                   ` Tim Cross
2021-05-13  5:31                   ` Ihor Radchenko
2021-05-18 12:06                   ` Sébastien Miquel
2021-05-18 13:34                     ` Timothy
2021-05-18 14:36                       ` Sébastien Miquel
2021-04-29 22:59 ` TRS-80
2021-10-03  7:14 ` Ihor Radchenko
2021-10-03  7:16   ` Timothy
2021-10-03  9:09     ` Ihor Radchenko
2021-10-03  9:22       ` Timothy
2021-10-04 20:02       ` Protesilaos Stavrou
2021-11-21 14:09 ` Timothy
2021-11-22 11:52   ` Timothy
2021-11-22 12:23     ` Ihor Radchenko
2021-11-22 13:43       ` Timothy
2021-11-22 14:35         ` Ihor Radchenko
2021-11-22 14:37           ` Timothy
2021-11-23 13:30             ` Ihor Radchenko
2021-11-29 19:21               ` Timothy
2021-11-30 11:44                 ` Timothy
2021-11-30 12:45                   ` Sébastien Miquel
2021-11-30 12:46                     ` Timothy
2021-11-30 12:21                 ` Ihor Radchenko
2021-12-02 12:53     ` Eric S Fraga
2021-12-02 13:57       ` Faces for inline src blocks (was: [PATCH] Fontification for inline src blocks) Timothy
2021-12-02 15:52         ` Faces for inline src blocks Eric S Fraga
2021-12-02 15:56           ` Timothy
2021-12-02 16:15             ` Eric S Fraga
2021-11-23 10:45   ` [PATCH] Fontification " Vitaly Ankh
2021-11-23 13:45     ` Vitaly Ankh

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=878s4wtrzx.fsf@gmail.com \
    --to=tecosaur@gmail.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=tgbugs@gmail.com \
    /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
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).