From 997fc2cef771d05f866480153a8909ad51484d03 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?St=C3=A9phane=20Marks?= Date: Thu, 14 May 2026 13:44:37 -0400 Subject: [PATCH] Allow markdown-ts--run-command-in-code-block to ignore output (bug#81041) Do not assume every command run in 'markdown-ts--run-command-in-code-block' produces output that needs to be merged from the temp/work buffer into the source buffer. One example is 'xref-find-definitions', the temp buffer of which is unrelated to the source buffer. * lisp/textmodes/markdown-ts-mode.el (markdown-ts-code-block-commands): Add 'complete-symbol'. (markdown-ts-code-block-ignore-output-commands): New defvar. (markdown-ts--run-command-in-code-block): Ignore command output when necessary. --- lisp/textmodes/markdown-ts-mode.el | 41 ++++++++++++++++++------------ 1 file changed, 25 insertions(+), 16 deletions(-) diff --git a/lisp/textmodes/markdown-ts-mode.el b/lisp/textmodes/markdown-ts-mode.el index d2f4fcd8fa7..515d0620127 100644 --- a/lisp/textmodes/markdown-ts-mode.el +++ b/lisp/textmodes/markdown-ts-mode.el @@ -3043,6 +3043,7 @@ force mode probe. Return a valid mode symbol or nil." (defvar markdown-ts-code-block-commands '(indent-for-tab-command electric-newline-and-maybe-indent completion-at-point + complete-symbol newline comment-dwim comment-line @@ -3056,6 +3057,10 @@ See `markdown-ts--run-command-in-code-block'.") "Commands that need a \"thing\" at point in a code-block context. See `markdown-ts--run-command-in-code-block'.") +(defvar markdown-ts-code-block-ignore-output-commands '(xref-find-definitions) + "Commands whose output to ignore when executed in a code-block context. +See `markdown-ts--run-command-in-code-block'.") + (defvar markdown-ts-code-block-region-commands '(comment-or-uncomment-region) "Commands that need a region in a code-block context. See `markdown-ts--run-command-in-code-block'.") @@ -3179,6 +3184,8 @@ ARGS are captured by `markdown-ts--maybe-run-command-in-code-block'." (adj-region-beg (when region-beg (1+ (- orig-point region-beg)))) (adj-region-end (when region-end (1+ (- orig-point region-end)))) (point-delta 0) + (ignore-output + (memq command markdown-ts-code-block-ignore-output-commands)) (source-buffer (current-buffer))) (with-work-buffer (insert str) @@ -3208,22 +3215,24 @@ ARGS are captured by `markdown-ts--maybe-run-command-in-code-block'." (funcall-interactively command (car args))) (t (apply #'funcall-interactively command args))) - (setq str (buffer-substring-no-properties (point-min) (point-max))) - (setq temp-deactivate-mark deactivate-mark) - (setq point-delta (- (point) point))) - (let ((work-buffer (current-buffer))) - (with-current-buffer source-buffer - (replace-region-contents beg end work-buffer) - ;; Propagate mark deactivation to the source buffer. - (setq deactivate-mark temp-deactivate-mark) - ;; Move point if it moved in the temp buffer. - (goto-char (+ orig-point point-delta)) - ;; Record the original command. - (setq this-command command) - ;; This helps maintain discrete command actions. - (undo-boundary) - ;; Make sure the originating region is refontified. - (font-lock-flush beg end))))))) + (unless ignore-output + (setq str (buffer-substring-no-properties (point-min) (point-max))) + (setq temp-deactivate-mark deactivate-mark) + (setq point-delta (- (point) point)))) + (unless ignore-output + (let ((work-buffer (current-buffer))) + (with-current-buffer source-buffer + (replace-region-contents beg end work-buffer) + ;; Propagate mark deactivation to the source buffer. + (setq deactivate-mark temp-deactivate-mark) + ;; Move point if it moved in the temp buffer. + (goto-char (+ orig-point point-delta)) + ;; This helps maintain discrete command actions. + (undo-boundary) + ;; Make sure the originating region is refontified. + (font-lock-flush beg end)))) + ;; Record the original command. + (setq this-command command))))) (defun markdown-ts--find-code-block-delimiter (pos &optional backward) "Return the next or previous fenced_code_block_delimiter node, or nil.