Function: markdown-ts--set-up

markdown-ts--set-up is a byte-compiled function defined in markdown-ts-mode.el.gz.

Signature

(markdown-ts--set-up)

Documentation

Set up the buffer for markdown-ts-mode.

If markdown-ts--set-up-inline is non-nil, use a lightweight set up for embedded inline markdown-ts-mode buffers.

NOTE: Call this function only when the treesit markdown and markdown-inline parsers are available.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/markdown-ts-mode.el.gz
(defun markdown-ts--set-up ()
  "Set up the buffer for `markdown-ts-mode'.
If `markdown-ts--set-up-inline' is non-nil, use a lightweight set up for
embedded inline `markdown-ts-mode' buffers.

NOTE: Call this function only when the treesit `markdown' and
`markdown-inline' parsers are available."

  ;; Set these up for both master and inline for code-block buffers.
  (setq-local comment-start "<!-- "
              comment-end " -->"
              comment-start-skip "<!--[ \t]*"
              comment-end-skip "[ \t]*-->"
              comment-use-syntax nil)

  (setq-local fill-paragraph-function
              #'markdown-ts--fill-paragraph
              fill-forward-paragraph-function
              #'markdown-ts--fill-forward-paragraph)

  ;; `adaptive-fill-function' takes precedence over
  ;; `adaptive-fill-regexp'; the default regexp is fine as a
  ;; fallback for plain (non-list) paragraphs.
  (setq-local adaptive-fill-function #'markdown-ts--adaptive-fill)

  ;; Create and configure the parsers.
  (setq treesit-primary-parser
        (treesit-parser-create 'markdown))

  (setq-local font-lock-defaults nil)
  (setq-local treesit-font-lock-settings markdown-ts--treesit-settings)
  (setq-local treesit-font-lock-feature-list '((delimiter heading)
                                               (paragraph)
                                               (paragraph-inline)
                                               (image-preview error)))

  (cond (markdown-ts--set-up-inline
         (treesit-parser-create 'markdown-inline)
         (setq-local treesit-range-settings
                     (treesit-range-rules
                      :embed 'markdown-inline
                      :host 'markdown
                      '((inline) @markdown-inline))))
        (t
         ;; Range settings differ in the master buffer vs. inline above.
         (setq-local treesit-range-settings (markdown-ts--range-settings))

         ;; Configure features needed only in the master buffer.

         ;; Imenu support.
         (setq-local treesit-simple-imenu-settings
                     `(("Headings" ,#'markdown-ts--imenu-heading-node-p
                        nil ,#'markdown-ts--imenu-heading-name-function)
                       ("Code Blocks" ,#'markdown-ts--imenu-code-block-node-p
                        nil ,#'markdown-ts--imenu-code-block-name-function)))

         ;; Outline support.
         (setq-local treesit-outline-predicate #'markdown-ts--outline-predicate)
         (setq-local outline-minor-mode-cycle t)
         (outline-minor-mode 1)
         (markdown-ts--apply-ellipsis)

         ;; NOTE: `outline-view-change-hook' was obsoleted in 29.1 (commit
         ;; 53b1e6f96cb) on the grounds that only lazy-lock used it, but no
         ;; replacement was provided and outline.el itself still runs the hook
         ;; in 10+ places.
         (with-suppressed-warnings ((obsolete outline-view-change-hook))
           (add-hook 'outline-view-change-hook
                     #'markdown-ts--outline-view-change nil t))

         (progn
           (make-local-variable 'font-lock-extra-managed-props)
           (dolist (prop '(invisible display button category action help-echo))
             (add-to-list 'font-lock-extra-managed-props prop)))

         (when (treesit-ready-p 'html t)
           (treesit-parser-create 'html)
           (require 'html-ts-mode)
           (defvar html-ts-mode--font-lock-settings)
           (defvar html-ts-mode--treesit-font-lock-feature-list)
           (setq-local treesit-font-lock-settings
                       (append treesit-font-lock-settings
                               html-ts-mode--font-lock-settings))
           (setq-local treesit-font-lock-feature-list
                       (treesit-merge-font-lock-feature-list
                        treesit-font-lock-feature-list
                        html-ts-mode--treesit-font-lock-feature-list))
           (setq-local treesit-range-settings
                       (append treesit-range-settings
                               (treesit-range-rules
                                :embed 'html
                                :host 'markdown
                                :local t
                                '((html_block) @html)

                                :embed 'html
                                :host 'markdown-inline
                                '((html_tag) @html)))))

         (when (treesit-ready-p 'yaml t)
           (require 'yaml-ts-mode)
           (defvar yaml-ts-mode--font-lock-settings)
           (defvar yaml-ts-mode--font-lock-feature-list)
           (setq-local treesit-font-lock-settings
                       (append treesit-font-lock-settings
                               yaml-ts-mode--font-lock-settings))
           (setq-local treesit-font-lock-feature-list
                       (treesit-merge-font-lock-feature-list
                        treesit-font-lock-feature-list
                        yaml-ts-mode--font-lock-feature-list))
           (setq-local treesit-range-settings
                       (append treesit-range-settings
                               (treesit-range-rules
                                :embed 'yaml
                                :host 'markdown
                                :local t
                                '((minus_metadata) @yaml)))))

         (when (treesit-ready-p 'toml t)
           (require 'toml-ts-mode)
           (defvar toml-ts-mode--font-lock-settings)
           (defvar toml-ts-mode--font-lock-feature-list)
           (setq treesit-font-lock-settings
                 (append treesit-font-lock-settings
                         toml-ts-mode--font-lock-settings))
           (setq-local treesit-font-lock-feature-list
                       (treesit-merge-font-lock-feature-list
                        treesit-font-lock-feature-list
                        toml-ts-mode--font-lock-feature-list))
           (setq-local treesit-range-settings
                       (append treesit-range-settings
                               (treesit-range-rules
                                :embed 'toml
                                :host 'markdown
                                :local t
                                '((plus_metadata) @toml)))))

         ;; Support for executing commands in a code-block context.
         (when markdown-ts-enable-code-block-context-mode
           (markdown-ts-code-block-context-mode))

         ;; Support for table mode.
         (when markdown-ts-enable-table-mode
           (markdown-ts-table-mode))))

  (treesit-major-mode-setup)

  ;; Do not enable `jit-lock-mode' in indirect buffers such as the one
  ;; we use for code block commands.
  (unless (buffer-base-buffer)
    (jit-lock-register #'markdown-ts--fontify-bare-uri))

  (unless markdown-ts--set-up-inline
    ;; Order matters: `markdown-ts--set-hide-markup' calls `font-lock-flush'
    ;; (only meaningful once `treesit-major-mode-setup' has wired up
    ;; font-lock), and `markdown-ts-default-folding' calls outline
    ;; commands that rely on `outline-search-function', which
    ;; `treesit-major-mode-setup' installs from `treesit-outline-predicate'.
    (markdown-ts--set-hide-markup markdown-ts-hide-markup)
    ;; Respect the user's default outline folding.
    (pcase markdown-ts-default-folding
      ('show-all (ignore))
      ('fold-all (outline-hide-sublevels 1))
      ('fold-headings (outline-show-all)
                      (outline-hide-region-body (point-min) (point-max))))))