Steven Allen <[email protected]> writes: > Ihor Radchenko <[email protected]> writes: > >> Steven Allen <[email protected]> writes: >> >>> This patch applies the syntax table from each source-block's >>> major-mode to the source-block itself. This makes it easier to work >>> with source blocks without editing them in a new buffer: >>> >>> - s-expression and symbol navigation/selection work as >>> expected. >>> - "<" is not treated as a parentheses within source blocks unless >>> appropriate for the source block's mode, etc. >>> - "C-h f" suggests the correct function at point, even when quoted. >>> >>> From my testing, this seems to work as expected but I'm happy to put >>> this behind a config variable if there are concerns about it changing >>> default behavior. >> >> You are not the first who tried this approach. >> Just try make test and see that this seemingly innocent patch breaks >> Org. > > Also not the first time I failed to run the tests, sorry about that. > > I've attached a new patch that: > > 1. Only applies to source blocks. > 2. Avoids modifying the syntax table of the surrounding newlines. > > This passes the tests now, but I'm wondering if there might still be > things I'm not understanding here.
Well, at the very least, I need to cleanup the syntax table (patch attached). This also deserves some tests, but I don't want to continue hacking on this unless you think it has a chance of going somewhere. Feel free to say "too risky".
>From 55555a834c829bf5319d045b1fbe3bb0ecfd96c5 Mon Sep 17 00:00:00 2001 From: Steven Allen <[email protected]> Date: Fri, 7 Nov 2025 11:31:39 -0800 Subject: [PATCH] Apply the mode's syntax table when fontifying natively This makes it easier to work with source blocks without editing them in a new buffer: s-expression and symbol navigation/selection work as expected, < is not treated as a parentheses within source blocks unless appropriate for the source block's mode, etc. * lisp/org-src.el (org-src-font-lock-fontify-block): Apply the syntax-table from the source-block's mode to the source block text (blocks only). (org-src--edit-element): Remove any pre-existing syntax table properties. * lisp/org.el (org-mode): Obey the syntax table text property in commands. This ensures that, e.g., "C-h f" correctly suggests the symbol at point and doesn't, e.g., include any quotes, etc. (org-unfontify-region): Cleanup the syntax-table property. --- lisp/org-src.el | 14 ++++++++++++-- lisp/org.el | 6 +++++- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/lisp/org-src.el b/lisp/org-src.el index 971d6e938..ce64f1e35 100644 --- a/lisp/org-src.el +++ b/lisp/org-src.el @@ -609,7 +609,10 @@ defun org-src--edit-element ;; Insert contents. (insert contents) (remove-text-properties (point-min) (point-max) - '(display nil invisible nil intangible nil)) + '( display nil + invisible nil + intangible nil + syntax-table nil )) (let ((lf (eq type 'latex-fragment))) (unless preserve-ind (org-do-remove-indentation (and lf block-ind) lf))) (set-buffer-modified-p nil) @@ -669,7 +672,12 @@ defun org-src-font-lock-fontify-block "Fontify code block between START and END using LANG's syntax. This function is called by Emacs's automatic fontification, as long as `org-src-fontify-natively' is non-nil." - (let ((modified (buffer-modified-p)) native-tab-width) + (let ((modified (buffer-modified-p)) + (is-block (and (eq (char-after start) ?\n) + (eq (char-before end) ?\n) + (< start end) + (< 2 (- end start)))) + native-tab-width) (remove-text-properties start end '(face nil)) (when-let* ((lang-mode (org-src-get-lang-mode-if-bound lang))) (condition-case nil @@ -737,6 +745,8 @@ defun org-src-font-lock-fontify-block 'org-src-invisible new-prop org-buffer))))))) (setq pos next))) + (when is-block + (put-text-property (1+ start) (1- end) 'syntax-table (syntax-table) org-buffer)) (set-buffer-modified-p nil))) (error (message "Native code fontification error in %S at pos%d\n Error: %S" diff --git a/lisp/org.el b/lisp/org.el index ed012d2e6..4f10bcb00 100644 --- a/lisp/org.el +++ b/lisp/org.el @@ -5178,6 +5178,9 @@ define-derived-mode org-mode (org-setup-filling) ;; Comments. (org-setup-comments-handling) + ;; Obey the syntax-table text property when navigating text (used in + ;; source blocks). + (setq-local parse-sexp-lookup-properties t) ;; Beginning/end of defun (setq-local beginning-of-defun-function 'org-backward-element) (setq-local end-of-defun-function @@ -6338,7 +6341,8 @@ defun org-unfontify-region (remove-text-properties beg end '(mouse-face t keymap t org-linked-text t invisible t intangible t - org-emphasis t)) + org-emphasis t + syntax-table t)) (org-fold-core-update-optimisation beg end) (org-remove-font-lock-display-properties beg end))) -- 2.51.2
