1
Fork 0
mirror of git://git.sv.gnu.org/emacs.git synced 2026-05-31 01:32:00 -07:00

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.
This commit is contained in:
Stéphane Marks 2026-05-14 13:44:37 -04:00 committed by João Távora
parent 7d84e69a34
commit 997fc2cef7

View file

@ -3043,6 +3043,7 @@ force mode probe. Return a valid mode symbol or nil."
(defvar markdown-ts-code-block-commands '(indent-for-tab-command (defvar markdown-ts-code-block-commands '(indent-for-tab-command
electric-newline-and-maybe-indent electric-newline-and-maybe-indent
completion-at-point completion-at-point
complete-symbol
newline newline
comment-dwim comment-dwim
comment-line 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. "Commands that need a \"thing\" at point in a code-block context.
See `markdown-ts--run-command-in-code-block'.") 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) (defvar markdown-ts-code-block-region-commands '(comment-or-uncomment-region)
"Commands that need a region in a code-block context. "Commands that need a region in a code-block context.
See `markdown-ts--run-command-in-code-block'.") 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-beg (when region-beg (1+ (- orig-point region-beg))))
(adj-region-end (when region-end (1+ (- orig-point region-end)))) (adj-region-end (when region-end (1+ (- orig-point region-end))))
(point-delta 0) (point-delta 0)
(ignore-output
(memq command markdown-ts-code-block-ignore-output-commands))
(source-buffer (current-buffer))) (source-buffer (current-buffer)))
(with-work-buffer (with-work-buffer
(insert str) (insert str)
@ -3208,22 +3215,24 @@ ARGS are captured by `markdown-ts--maybe-run-command-in-code-block'."
(funcall-interactively command (car args))) (funcall-interactively command (car args)))
(t (t
(apply #'funcall-interactively command args))) (apply #'funcall-interactively command args)))
(setq str (buffer-substring-no-properties (point-min) (point-max))) (unless ignore-output
(setq temp-deactivate-mark deactivate-mark) (setq str (buffer-substring-no-properties (point-min) (point-max)))
(setq point-delta (- (point) point))) (setq temp-deactivate-mark deactivate-mark)
(let ((work-buffer (current-buffer))) (setq point-delta (- (point) point))))
(with-current-buffer source-buffer (unless ignore-output
(replace-region-contents beg end work-buffer) (let ((work-buffer (current-buffer)))
;; Propagate mark deactivation to the source buffer. (with-current-buffer source-buffer
(setq deactivate-mark temp-deactivate-mark) (replace-region-contents beg end work-buffer)
;; Move point if it moved in the temp buffer. ;; Propagate mark deactivation to the source buffer.
(goto-char (+ orig-point point-delta)) (setq deactivate-mark temp-deactivate-mark)
;; Record the original command. ;; Move point if it moved in the temp buffer.
(setq this-command command) (goto-char (+ orig-point point-delta))
;; This helps maintain discrete command actions. ;; This helps maintain discrete command actions.
(undo-boundary) (undo-boundary)
;; Make sure the originating region is refontified. ;; Make sure the originating region is refontified.
(font-lock-flush beg end))))))) (font-lock-flush beg end))))
;; Record the original command.
(setq this-command command)))))
(defun markdown-ts--find-code-block-delimiter (pos &optional backward) (defun markdown-ts--find-code-block-delimiter (pos &optional backward)
"Return the next or previous fenced_code_block_delimiter node, or nil. "Return the next or previous fenced_code_block_delimiter node, or nil.