Skip to content

Commit

Permalink
Insert GFM code block and edit-indirect simultaneously
Browse files Browse the repository at this point in the history
Optional prefix argument to C-c C-s C will start edit-indirect
after inserting a new GFM code block.

Closes GH-251.
  • Loading branch information
jrblevin committed Nov 8, 2017
1 parent e065615 commit 8af1076
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 6 deletions.
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
HTML export output buffer. ([GH-224][])
- Add `markdown-edit-code-block-default-mode` to specify default
mode for indirect editing of code blocks. ([GH-251][])
- Insert-and-indirect-edit GFM code blocks simultaneously by
giving a prefix argument to `C-c C-s C`. ([GH-251][])

* Bug fixes:

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,10 @@ can obtain a list of all keybindings by pressing <kbd>C-c C-h</kbd>.
When the [`edit-indirect`][ei] package is installed, <kbd>C-c '</kbd>
(`markdown-edit-code-block`) can be used to edit a code block
in an indirect buffer in the native major mode. Press <kbd>C-c C-c</kbd>
to commit changes and return or <kbd>C-c C-k</kbd> to cancel.
to commit changes and return or <kbd>C-c C-k</kbd> to cancel. You can
also give a prefix argument to the insertion command, as in
<kbd>C-u C-c C-s C</kbd>, to edit the code block in an indirect buffer
upon insertion.

As noted, many of the commands above behave differently depending
on whether Transient Mark mode is enabled or not. When it makes
Expand Down
16 changes: 11 additions & 5 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,10 @@
;; When the [`edit-indirect'][ei] package is installed, `C-c '`
;; (`markdown-edit-code-block`) can be used to edit a code block
;; in an indirect buffer in the native major mode. Press `C-c C-c`
;; to commit changes and return or `C-c C-k` to cancel.
;; to commit changes and return or `C-c C-k` to cancel. You can
;; also give a prefix argument to the insertion command, as in
;; `C-u C-c C-s C`, to edit the code block in an indirect buffer
;; upon insertion.
;;
;; As noted, many of the commands above behave differently depending
;; on whether Transient Mark mode is enabled or not. When it makes
Expand Down Expand Up @@ -5116,12 +5119,14 @@ opening code fence and an info string."
:safe #'natnump
:package-version '(markdown-mode . "2.3"))

(defun markdown-insert-gfm-code-block (&optional lang)
(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."
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
Expand Down Expand Up @@ -5166,12 +5171,13 @@ automatically in order to have the correct markup."
(setq start (point))
(insert "```" lang "\n")
(indent-to indent)
(insert ?\n)
(unless edit (insert ?\n))
(indent-to indent)
(insert "```")
(markdown-ensure-blank-line-after)
(markdown-syntax-propertize-fenced-block-constructs start (point)))
(end-of-line 0)))
(end-of-line 0)
(when edit (markdown-edit-code-block))))

(defun markdown-code-block-lang (&optional pos-prop)
"Return the language name for a GFM or tilde fenced code block.
Expand Down

0 comments on commit 8af1076

Please sign in to comment.