Skip to content

Commit

Permalink
Add GFM checkbox insertion command, use as markdown-do fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
knu authored and jrblevin committed Oct 10, 2017
1 parent bc79248 commit 7772fc9
Show file tree
Hide file tree
Showing 4 changed files with 128 additions and 3 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -866,7 +866,9 @@ by `markdown-mode` and `gfm-mode` as described below.
(and it is set to t by default). These checkboxes can be
toggled by clicking `mouse-1`, pressing <kbd>RET</kbd> over the button,
or by pressing <kbd>C-c C-d</kbd> (`markdown-do`) with the point anywhere
in the task list item.
in the task list item. A normal list item can be turned to a check
list item by the same command, or more specifically `C-c C-x [`
(`markdown-add-gfm-checkbox`).

* **Wiki links:** Generic wiki links are supported in
`markdown-mode`, but in `gfm-mode` specifically they will be
Expand Down
48 changes: 46 additions & 2 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -882,7 +882,9 @@
;; (and it is set to t by default). These checkboxes can be
;; toggled by clicking `mouse-1`, pressing `RET` over the button,
;; or by pressing `C-c C-d` (`markdown-do`) with the point anywhere
;; in the task list item.
;; in the task list item. A normal list item can be turned to a
;; check list item by the same command, or more specifically
;; `C-c C-x [` (`markdown-add-gfm-checkbox`).
;;
;; * **Wiki links:** Generic wiki links are supported in
;; `markdown-mode', but in `gfm-mode' specifically they will be
Expand Down Expand Up @@ -5876,6 +5878,7 @@ Assumes match data is available for `markdown-regex-italic'."
(define-key map (kbd "C-c C-i") 'markdown-insert-image)
(define-key map (kbd "C-c C-x m") 'markdown-insert-list-item) ;; C-c C-j
(define-key map (kbd "C-c C-x C-x") 'markdown-toggle-gfm-checkbox) ;; C-c C-d
(define-key map (kbd "C-c C-x [") 'markdown-add-gfm-checkbox)
(define-key map (kbd "C-c -") 'markdown-insert-hr)
map)
"Keymap for Markdown major mode.")
Expand Down Expand Up @@ -5963,6 +5966,7 @@ See also `markdown-mode-map'.")
["Mark Subtree" markdown-mark-subtree])
("Lists"
["Insert List Item" markdown-insert-list-item]
["Add Checkbox" markdown-add-gfm-checkbox :keys "C-c C-x ["]
["Move Subtree Up" markdown-move-up :keys "C-c <up>"]
["Move Subtree Down" markdown-move-down :keys "C-c <down>"]
["Indent Subtree" markdown-demote :keys "C-c <right>"]
Expand Down Expand Up @@ -8302,7 +8306,7 @@ markers and footnote text."
(markdown-toggle-gfm-checkbox))
;; Otherwise
(t
(user-error "Nothing to do in context at point"))))
(markdown-add-gfm-checkbox))))


;;; Miscellaneous =============================================================
Expand Down Expand Up @@ -8568,6 +8572,46 @@ is found, the return value is the same value returned by
(setq bounds (markdown-cur-list-item-bounds)))
(> (length (nth 5 bounds)) 0))


(defun markdown-add-gfm-checkbox ()
"Add GFM checkbox at point.
Returns t if added.
Returns nil if non-applicable."
(interactive)
(let ((bounds (markdown-cur-list-item-bounds)))
(if bounds
(unless (cl-sixth bounds)
(let ((pos (+ (cl-first bounds) (cl-fourth bounds)))
(markup "[ ] "))
(if (< pos (point))
(save-excursion
(goto-char pos)
(insert markup))
(goto-char pos)
(insert markup))
t))
(unless (save-excursion
(back-to-indentation)
(or (markdown-list-item-at-point-p)
(markdown-heading-at-point)
(markdown-in-comment-p)
(markdown-code-block-at-point-p)))
(let ((pos (save-excursion
(back-to-indentation)
(point)))
(markup (concat (or (save-excursion
(beginning-of-line 0)
(cl-fifth (markdown-cur-list-item-bounds)))
"- ")
"[ ] ")))
(if (< pos (point))
(save-excursion
(goto-char pos)
(insert markup))
(goto-char pos)
(insert markup))
t)))))

(defun markdown-toggle-gfm-checkbox ()
"Toggle GFM checkbox at point.
Returns the resulting status as a string, either \"[x]\" or \"[ ]\".
Expand Down
12 changes: 12 additions & 0 deletions tests/check-items.text
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@


item1

item2

- item3

- item4

* [ ] item5

67 changes: 67 additions & 0 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -3802,6 +3802,73 @@ puts 'hello, world'

Another paragraph of item 2"))))

(ert-deftest test-markdown-lists/add-gfm-checkbox ()
(markdown-test-file "check-items.text"
(goto-char (point-min))
(end-of-line)
(should (markdown-add-gfm-checkbox))
(should (= (line-number-at-pos (point)) 1))
(should (eolp))
(should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
" - [ ] "))

(forward-line 2)
(back-to-indentation)
(should (markdown-add-gfm-checkbox))
(should (= (line-number-at-pos (point)) 3))
(should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
" - [ ] "))
(should (string-equal (buffer-substring-no-properties (point) (line-end-position))
"item1"))

(forward-line 2)
(back-to-indentation)
(forward-char 1)
(should (markdown-add-gfm-checkbox))
(should (= (line-number-at-pos (point)) 5))
(should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
" - [ ] i"))
(should (string-equal (buffer-substring-no-properties (point) (line-end-position))
"tem2"))

(forward-line 2)
(back-to-indentation)
(forward-char 2)
(should (markdown-add-gfm-checkbox))
(should (= (line-number-at-pos (point)) 7))
(should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
" - [ ] "))
(should (string-equal (buffer-substring-no-properties (point) (line-end-position))
"item3"))

(forward-line 2)
(back-to-indentation)
(forward-char 3)
(should (markdown-add-gfm-checkbox))
(should (= (line-number-at-pos (point)) 9))
(should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
" - [ ] i"))
(should (string-equal (buffer-substring-no-properties (point) (line-end-position))
"tem4"))

(forward-line 2)
(end-of-line)
(should-not (markdown-add-gfm-checkbox))
(should (= (line-number-at-pos (point)) 11))
(should (eolp))
(should (string-equal (buffer-substring-no-properties (line-beginning-position) (point))
" * [ ] item5"))

(forward-line 1)
(back-to-indentation)
(should (markdown-add-gfm-checkbox))
(should (= (line-number-at-pos (point)) 12))
(should (eolp))
(should (string-equal (buffer-substring-no-properties
(line-beginning-position)
(point))
" * [ ] "))))

(ert-deftest test-markdown-lists/toggle-gfm-checkbox ()
(markdown-test-string " - [X] GFM task list item"
(should (string-equal (markdown-toggle-gfm-checkbox) "[ ]"))
Expand Down

0 comments on commit 7772fc9

Please sign in to comment.