Function: markdown-insert-header-dwim
markdown-insert-header-dwim is an interactive and byte-compiled
function defined in markdown-mode.el.
Signature
(markdown-insert-header-dwim &optional ARG SETEXT)
Documentation
Insert or replace header markup.
The level and type of the header are determined automatically by the type and level of the previous header, unless a prefix argument is given via ARG. With a numeric prefix valued 1 to 6, insert a header of the given level, with the type being determined automatically (note that only level 1 or 2 setext headers are possible).
With a C-u (universal-argument) prefix (i.e., when ARG is (4)),
promote the heading by one level.
With two C-u (universal-argument) prefixes (i.e., when ARG is (16)),
demote the heading by one level.
When SETEXT is non-nil, prefer setext-style headers when
possible (levels one and two).
When there is an active region, use it for the header text. When
the point is at an existing header, change the type and level
according to the rules above.
Otherwise, if the line is not empty, create a header using the
text on the current line as the header text.
Finally, if the point is on a blank line, insert empty header
markup (atx) or prompt for text (setext).
See markdown-insert-header for more details about how the
header text is determined.
Key Bindings
Source Code
;; Defined in ~/.emacs.d/elpa/markdown-mode-20260321.143/markdown-mode.el
(defun markdown-insert-header-dwim (&optional arg setext)
"Insert or replace header markup.
The level and type of the header are determined automatically by
the type and level of the previous header, unless a prefix
argument is given via ARG.
With a numeric prefix valued 1 to 6, insert a header of the given
level, with the type being determined automatically (note that
only level 1 or 2 setext headers are possible).
With a \\[universal-argument] prefix (i.e., when ARG is (4)),
promote the heading by one level.
With two \\[universal-argument] prefixes (i.e., when ARG is (16)),
demote the heading by one level.
When SETEXT is non-nil, prefer setext-style headers when
possible (levels one and two).
When there is an active region, use it for the header text. When
the point is at an existing header, change the type and level
according to the rules above.
Otherwise, if the line is not empty, create a header using the
text on the current line as the header text.
Finally, if the point is on a blank line, insert empty header
markup (atx) or prompt for text (setext).
See `markdown-insert-header' for more details about how the
header text is determined."
(interactive "*P")
(let (level)
(save-excursion
(when (or (thing-at-point-looking-at markdown-regex-header)
(re-search-backward markdown-regex-header nil t))
;; level of current or previous header
(setq level (markdown-outline-level))
;; match group 1 indicates a setext header
(setq setext (match-end 1))))
;; check prefix argument
(cond
((and (equal arg '(4)) level (> level 1)) ;; C-u
(cl-decf level))
((and (equal arg '(16)) level (< level 6)) ;; C-u C-u
(cl-incf level))
(arg ;; numeric prefix
(setq level (prefix-numeric-value arg))))
;; setext headers must be level one or two
(and level (setq setext (and setext (<= level 2))))
;; insert the heading
(markdown-insert-header level nil setext)))