Function: markdown-insert-gfm-code-block

markdown-insert-gfm-code-block is an interactive and byte-compiled function defined in markdown-mode.el.

Signature

(markdown-insert-gfm-code-block &optional LANG EDIT)

Documentation

Insert GFM code block for language LANG.

If LANG is nil, the language will be queried from user. If a region is active, wrap this region with the markup instead. If the region boundaries are not on empty lines, these are added automatically in order to have the correct markup. When EDIT is non-nil (e.g., when C-u (universal-argument) is given), edit the code block in an indirect buffer after insertion.

Key Bindings

Source Code

;; Defined in ~/.emacs.d/elpa/markdown-mode-20260321.143/markdown-mode.el
(defun markdown-insert-gfm-code-block (&optional lang edit)
  "Insert GFM code block for language LANG.
If LANG is nil, the language will be queried from user.  If a
region is active, wrap this region with the markup instead.  If
the region boundaries are not on empty lines, these are added
automatically in order to have the correct markup.  When EDIT is
non-nil (e.g., when \\[universal-argument] is given), edit the
code block in an indirect buffer after insertion."
  (interactive
   (list (let ((completion-ignore-case nil))
           (condition-case nil
               (markdown-clean-language-string
                (completing-read
                 "Programming language: "
                 (markdown-gfm-get-corpus)
                 nil 'confirm (car markdown-gfm-used-languages)
                 'markdown-gfm-language-history))
             (quit "")))
         current-prefix-arg))
  (unless (string= lang "") (markdown-gfm-add-used-language lang))
  (when (and (> (length lang) 0)
             (not markdown-code-block-braces))
    (setq lang (concat (make-string markdown-spaces-after-code-fence ?\s)
                       lang)))
  (let ((gfm-open-brace (if markdown-code-block-braces "{" ""))
        (gfm-close-brace (if markdown-code-block-braces "}" "")))
    (if (use-region-p)
        (let* ((b (region-beginning)) (e (region-end)) end
               (indent (progn (goto-char b) (current-indentation))))
          (goto-char e)
          ;; if we're on a blank line, don't newline, otherwise the ```
          ;; should go on its own line
          (unless (looking-back "\n" nil)
            (newline))
          (indent-to indent)
          (insert "```")
          (markdown-ensure-blank-line-after)
          (setq end (point))
          (goto-char b)
          ;; if we're on a blank line, insert the quotes here, otherwise
          ;; add a new line first
          (unless (looking-at-p "\n")
            (newline)
            (forward-line -1))
          (markdown-ensure-blank-line-before)
          (indent-to indent)
          (insert "```" gfm-open-brace lang gfm-close-brace)
          (markdown-syntax-propertize-fenced-block-constructs (line-beginning-position) end))
      (let ((indent (current-indentation))
            start-bol)
        (delete-horizontal-space :backward-only)
        (markdown-ensure-blank-line-before)
        (indent-to indent)
        (setq start-bol (line-beginning-position))
        (insert "```" gfm-open-brace lang gfm-close-brace "\n")
        (indent-to indent)
        (unless edit (insert ?\n))
        (indent-to indent)
        (insert "```")
        (markdown-ensure-blank-line-after)
        (markdown-syntax-propertize-fenced-block-constructs start-bol (point)))
      (end-of-line 0)
      (when edit (markdown-edit-code-block)))))