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.
>From 314fd049642b4d41438da7cae395b00ea020fe4f 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). * 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. --- lisp/org-src.el | 9 ++++++++- lisp/org.el | 3 +++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lisp/org-src.el b/lisp/org-src.el index 971d6e938..4de31b975 100644 --- a/lisp/org-src.el +++ b/lisp/org-src.el @@ -669,7 +669,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 +742,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..524741af6 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 -- 2.51.2
