Function: markdown-ts--run-command-in-code-block

markdown-ts--run-command-in-code-block is a byte-compiled function defined in markdown-ts-mode.el.gz.

Signature

(markdown-ts--run-command-in-code-block BLOCK-MODE COMMAND &rest ARGS)

Documentation

Run COMMAND in BLOCK-MODE.

ARGS are captured by markdown-ts--maybe-run-command-in-code-block.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts--run-command-in-code-block (block-mode command &rest args)
  "Run COMMAND in BLOCK-MODE.
ARGS are captured by `markdown-ts--maybe-run-command-in-code-block'."
  (when-let* ((ov (cl-some
                   (lambda (ov)
                     (when (overlay-get ov 'markdown-ts-code-block) ov))
                   (overlays-at (point))))
              (beg (overlay-start ov))
              (end (overlay-end ov))
              (str (buffer-substring-no-properties beg end)))
    ;; Use a temp (or work) buffer because treesit currently confuses
    ;; nodes in an indirect buffer even if the indirect buffer is not
    ;; narrowed.
    (let* ((temp-deactivate-mark)
           (orig-point (point))
           (orig-mark (mark t))
           (orig-mark-active mark-active)
           (region-beg (use-region-beginning))
           (region-end (use-region-end))
           (adj-point (1+ (- orig-point beg)))
           (adj-mark (when orig-mark (1+ (- orig-mark beg))))
           (adj-region-beg (when region-beg (1+ (- region-beg beg))))
           (adj-region-end (when region-end (1+ (- region-end beg))))
           (point-delta 0)
           (ignore-output
            (memq command markdown-ts-code-block-ignore-output-commands))
           (source-buffer (current-buffer)))
      (with-work-buffer
        (insert str)
        (goto-char adj-point)
        ;; Propagate mark (and region).
        (when orig-mark-active
          (set-mark adj-mark))
        (markdown-ts--inhibit-messages-and-warnings
          'markdown-ts-inhibit-code-block-mode-warnings
          (delay-mode-hooks
            (let ((markdown-ts--set-up-inline
                   (provided-mode-derived-p block-mode 'markdown-ts-mode)))
              (funcall block-mode))))
        (let ((point (point))
              (arity (cdr (func-arity command))))
          (cond
           ((memq command markdown-ts-code-block-thing-commands)
            (when-let* ((thing (thing-at-point 'symbol)))
              (funcall-interactively command thing)))
           ((memq command markdown-ts-code-block-region-commands)
            (when (and adj-region-beg adj-region-end)
              (apply #'funcall-interactively command
                     adj-region-beg adj-region-end args)))
           ((zerop arity)
            (funcall-interactively command))
           ((eq 1 arity)
            (funcall-interactively command (car args)))
           (t
            (apply #'funcall-interactively command args)))
          (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)))))