Skip to content

Commit

Permalink
Revise top-level keys; unify subtree/list editing
Browse files Browse the repository at this point in the history
Previously, `markdown-move-{up,down}` only worked for list items and
operated on entire list item subtrees.  Meanwhile,
`markdown-{promote,demote}` for headings operated on the current
heading only, not the full subtree.  However, for list items they
operated on entire subtrees.

This commit unifies these functions so that both operate on list
items (and children), when the point is in a list item, and on entire
heading subtrees otherwise.  As such, we only need one set of
keybindings.

This commit also moves the keys to C-c <left>, C-c <right>, C-c <up>,
and C-c <down> in order to free up top-level keybindings M-<left> and
M-<right>, for `left-word` and `right-word` again.

Update tests, since heading markup no longer cycles back around at
end.

Closes GH-164.
  • Loading branch information
jrblevin committed Jun 21, 2017
1 parent 2e3ab74 commit 9c7c0c4
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 55 deletions.
14 changes: 14 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,19 @@
- Footnote and wiki link insertion have been moved to the
markup insertion prefix, as <kbd>C-c C-s f</kbd> and
<kbd>C-c C-s w</kbd>.
- The list and outline editing commands have been removed from
the top-level positions (previously <kbd>M-<left></kbd>,
<kbd>M-<right></kbd>, <kbd>M-<up></kbd>, <kbd>M-<down></kbd>)
and moved to major mode keybindings under <kbd>C-c</kbd> to
<kbd>C-c <left></kbd>, <kbd>C-c <right></kbd>,
<kbd>C-c <up></kbd>, and <kbd>C-c <down></kbd>, respectively.
([GH-164][])
- The list and outline editing commands have also been unified
so that they all operate on entire subtrees of list items and
subtrees of atx headings, symmetrically. Previously there were
separate commands for editing heading subtrees, but promoting
a single section is easy enough by directly inserting or
removing a hash mark or using the markup replacement commands.
- Jumping between references and reference definitions via
`markdown-jump`, previously bound to <kbd>C-c C-l</kbd>, has
been moved to <kbd>C-c C-d</kbd> and rebranded as
Expand Down Expand Up @@ -196,6 +209,7 @@
[gh-130]: https://github.com/jrblevin/markdown-mode/issues/130
[gh-134]: https://github.com/jrblevin/markdown-mode/issues/134
[gh-144]: https://github.com/jrblevin/markdown-mode/issues/144
[gh-164]: https://github.com/jrblevin/markdown-mode/issues/164
[gh-172]: https://github.com/jrblevin/markdown-mode/issues/172
[gh-176]: https://github.com/jrblevin/markdown-mode/issues/176
[gh-185]: https://github.com/jrblevin/markdown-mode/issues/185
Expand Down
116 changes: 66 additions & 50 deletions markdown-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@
;; moving backward or forward through the list of rule strings in
;; `markdown-hr-strings'. For bold and italic text, promotion and
;; demotion means changing the markup from underscores to asterisks.
;; Press `C-c C--` or `M-LEFT` to promote the element at the point
;; Press `C-c C--` or `C-c <left>` to promote the element at the point
;; if possible.
;;
;; To remember these commands, note that `-` is for decreasing the
Expand All @@ -415,26 +415,33 @@
;; completes the markup at the point, if it is determined to be
;; incomplete.
;;
;; * Editing Lists: `M-RET`, `M-UP`, `M-DOWN`, `M-LEFT`, and `M-RIGHT`
;; * Editing Lists: `M-RET`, `C-c <up>`, `C-c <down>`, `C-c <left>`, and `C-c <right>`
;;
;; New list items can be inserted with `M-RET` or `C-c C-j`. This
;; command determines the appropriate marker (one of the possible
;; unordered list markers or the next number in sequence for an
;; ordered list) and indentation level by examining nearby list
;; items. If there is no list before or after the point, start a
;; new list. Prefix this command by `C-u` to decrease the
;; indentation by one level. Prefix this command by `C-u C-u` to
;; increase the indentation by one level.
;; new list. As with heading insertion, you may prefix this
;; command by `C-u` to decrease the indentation by one level.
;; Prefix this command by `C-u C-u` to increase the indentation by
;; one level.
;;
;; Existing list items can be moved up or down with `M-UP` or
;; `M-DOWN` and indented or outdented with `M-RIGHT` or `M-LEFT`.
;; Existing list items (and their nested sub-items) can be moved
;; up or down with `C-c <up>` or `C-c <down>` and indented or
;; outdented with `C-c <right>` or `C-c <left>`.
;;
;; * Editing Subtrees: `M-S-UP`, `M-S-DOWN`, `M-S-LEFT`, and `M-S-RIGHT`
;; * Editing Subtrees: `C-c <up>`, `C-c <down>`, `C-c <left>`, and `C-c <right>`
;;
;; Entire subtrees of ATX headings can be promoted and demoted
;; with `M-S-LEFT` and `M-S-RIGHT`, which mirror the bindings
;; for promotion and demotion of list items. Similarly, subtrees
;; can be moved up and down with `M-S-UP` and `M-S-DOWN`.
;; with `C-c <left>` and `C-c <right>`, which are the same keybindings
;; used for promotion and demotion of list items. If the point is in
;; a list item, the operate on the list item. Otherwise, they operate
;; on the current heading subtree. Similarly, subtrees can be
;; moved up and down with `C-c <up>` and `C-c <down>`.
;;
;; These commands currently do not work properly if there are
;; Setext headings in the affected region.
;;
;; Please note the following "boundary" behavior for promotion and
;; demotion. Any level-six headings will not be demoted further
Expand Down Expand Up @@ -5574,20 +5581,15 @@ Assumes match data is available for `markdown-regex-italic'."
(define-key map (kbd "C-c C-c n") 'markdown-cleanup-list-numbers)
(define-key map (kbd "C-c C-c ]") 'markdown-complete-buffer)
(define-key map (kbd "C-c '") 'markdown-edit-code-block)
;; List editing
(define-key map (kbd "M-<up>") 'markdown-move-up)
(define-key map (kbd "M-<down>") 'markdown-move-down)
(define-key map (kbd "M-<left>") 'markdown-promote)
(define-key map (kbd "M-<right>") 'markdown-demote)
(define-key map (kbd "M-<return>") 'markdown-insert-list-item)
(define-key map (kbd "C-c C-j") 'markdown-insert-list-item)
;; Subtree editing
(define-key map (kbd "M-S-<up>") 'markdown-move-subtree-up)
(define-key map (kbd "M-S-<down>") 'markdown-move-subtree-down)
(define-key map (kbd "M-S-<left>") 'markdown-promote-subtree)
(define-key map (kbd "M-S-<right>") 'markdown-demote-subtree)
;; Subtree and list editing
(define-key map (kbd "C-c <up>") 'markdown-move-up)
(define-key map (kbd "C-c <down>") 'markdown-move-down)
(define-key map (kbd "C-c <left>") 'markdown-promote)
(define-key map (kbd "C-c <right>") 'markdown-demote)
(define-key map (kbd "C-c C-M-h") 'markdown-mark-subtree)
(define-key map (kbd "C-x n s") 'markdown-narrow-to-subtree)
(define-key map (kbd "M-<return>") 'markdown-insert-list-item)
(define-key map (kbd "C-c C-j") 'markdown-insert-list-item)
;; Blocks
(define-key map [remap backward-paragraph] 'markdown-backward-block)
(define-key map [remap forward-paragraph] 'markdown-forward-block)
Expand Down Expand Up @@ -5615,7 +5617,6 @@ Assumes match data is available for `markdown-regex-italic'."
(define-key map (kbd "C-c C-x d") 'markdown-move-down)
(define-key map (kbd "C-c C-x l") 'markdown-promote)
(define-key map (kbd "C-c C-x r") 'markdown-demote)
(define-key map (kbd "C-c C-x m") 'markdown-insert-list-item)
;; Deprecated keys that may be removed in a future version
(define-key map (kbd "C-c C-a L") 'markdown-insert-link) ;; C-c C-l
(define-key map (kbd "C-c C-a l") 'markdown-insert-link) ;; C-c C-l
Expand All @@ -5638,6 +5639,7 @@ Assumes match data is available for `markdown-regex-italic'."
(define-key map (kbd "C-c C-i i") 'markdown-insert-image)
(define-key map (kbd "C-c C-i I") 'markdown-insert-reference-image)
(define-key map (kbd "C-c C-i C-t") 'markdown-toggle-inline-images)
(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 -") 'markdown-insert-hr)
map)
Expand Down Expand Up @@ -5710,12 +5712,10 @@ See also `markdown-mode-map'.")
["Second Level Setext" markdown-insert-header-setext-2 :keys "C-c C-s @"])
["Horizontal Rule" markdown-insert-hr :keys "C-c C-s -"]
"---"
["Move Subtree Up" markdown-move-subtree-up :keys "M-S-<up>"]
["Move Subtree Down" markdown-move-subtree-down :keys "M-S-<down>"]
["Promote Subtree" markdown-promote-subtree :keys "M-S-<left>"]
["Demote Subtree" markdown-demote-subtree :keys "M-S-<right>"]
["Promote Header" markdown-promote :keys "M-<left>"]
["Demote Header" markdown-demote :keys "M-<right>"])
["Move Subtree Up" markdown-move-up :keys "C-c <up>"]
["Move Subtree Down" markdown-move-down :keys "C-c <down>"]
["Promote Subtree" markdown-promote :keys "C-c <left>"]
["Demote Subtree" markdown-demote :keys "C-c <right>"])
("Region & Mark"
["Indent Region" markdown-indent-region]
["Outdent Region" markdown-outdent-region]
Expand All @@ -5726,10 +5726,10 @@ See also `markdown-mode-map'.")
["Mark Subtree" markdown-mark-subtree])
("Lists"
["Insert List Item" markdown-insert-list-item]
["Move List Item Up" markdown-move-up :keys "M-<up>"]
["Move List Item Down" markdown-move-down :keys "M-<down>"]
["Indent List Item" markdown-demote :keys "M-<right>"]
["Outdent List Item" markdown-promote :keys "M-<left>"]
["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>"]
["Outdent Subtree" markdown-promote :keys "C-c <left>"]
["Renumber List" markdown-cleanup-list-numbers]
["Toggle Task List Item" markdown-toggle-gfm-checkbox :keys "C-c C-d"])
("Links & Images"
Expand Down Expand Up @@ -6203,7 +6203,8 @@ increase the indentation by one level."
(insert new-indent marker)))))))

(defun markdown-move-list-item-up ()
"Move the current list item up in the list when possible."
"Move the current list item up in the list when possible.
In nested lists, move child items with the parent item."
(interactive)
(let (cur prev old)
(when (setq cur (markdown-cur-list-item-bounds))
Expand All @@ -6222,7 +6223,8 @@ increase the indentation by one level."
(goto-char old)))))

(defun markdown-move-list-item-down ()
"Move the current list item down in the list when possible."
"Move the current list item down in the list when possible.
In nested lists, move child items with the parent item."
(interactive)
(let (cur next old)
(when (setq cur (markdown-cur-list-item-bounds))
Expand All @@ -6241,7 +6243,8 @@ increase the indentation by one level."

(defun markdown-demote-list-item (&optional bounds)
"Indent (or demote) the current list item.
Optionally, BOUNDS of the current list item may be provided if available."
Optionally, BOUNDS of the current list item may be provided if available.
In nested lists, demote child items as well."
(interactive)
(when (or bounds (setq bounds (markdown-cur-list-item-bounds)))
(save-excursion
Expand All @@ -6254,7 +6257,8 @@ Optionally, BOUNDS of the current list item may be provided if available."

(defun markdown-promote-list-item (&optional bounds)
"Unindent (or promote) the current list item.
Optionally, BOUNDS of the current list item may be provided if available."
Optionally, BOUNDS of the current list item may be provided if available.
In nested lists, demote child items as well."
(interactive)
(when (or bounds (setq bounds (markdown-cur-list-item-bounds)))
(save-excursion
Expand Down Expand Up @@ -6985,16 +6989,28 @@ This puts point at the start of the current subtree, and mark at the end."
;;; Generic Structure Editing, Completion, and Cycling Commands ===============

(defun markdown-move-up ()
"Move list item up.
Calls `markdown-move-list-item-up'."
"Move thing at point up.
When in a list item, call `markdown-move-list-item-up'.
Otherwise, move the current heading subtree up with
`markdown-move-subtree-up'."
(interactive)
(markdown-move-list-item-up))
(cond
((markdown-list-item-at-point-p)
(markdown-move-list-item-up))
(t
(markdown-move-subtree-up))))

(defun markdown-move-down ()
"Move list item down.
Calls `markdown-move-list-item-down'."
"Move thing at point down.
When in a list item, call `markdown-move-list-item-down'.
Otherwise, move the current heading subtree up with
`markdown-move-subtree-down'."
(interactive)
(markdown-move-list-item-down))
(cond
((markdown-list-item-at-point-p)
(markdown-move-list-item-down))
(t
(markdown-move-subtree-down))))

(defun markdown-promote ()
"Either promote header or list item at point or cycle markup.
Expand All @@ -7003,10 +7019,10 @@ See `markdown-cycle-atx', `markdown-cycle-setext', and
(interactive)
(let (bounds)
(cond
;; Promote atx header
;; Promote atx heading subtree
((thing-at-point-looking-at markdown-regex-header-atx)
(markdown-cycle-atx -1))
;; Promote setext header
(markdown-promote-subtree))
;; Promote setext heading
((thing-at-point-looking-at markdown-regex-header-setext)
(markdown-cycle-setext -1))
;; Promote horizonal rule
Expand All @@ -7031,10 +7047,10 @@ See `markdown-cycle-atx', `markdown-cycle-setext', and
(interactive)
(let (bounds)
(cond
;; Demote atx header
;; Demote atx heading subtree
((thing-at-point-looking-at markdown-regex-header-atx)
(markdown-cycle-atx 1))
;; Demote setext header
(markdown-demote-subtree))
;; Demote setext heading
((thing-at-point-looking-at markdown-regex-header-setext)
(markdown-cycle-setext 1))
;; Demote horizonal rule
Expand Down
16 changes: 11 additions & 5 deletions tests/markdown-test.el
Original file line number Diff line number Diff line change
Expand Up @@ -1703,13 +1703,19 @@ Should not cause an infinite loop."

(ert-deftest test-markdown-cycle/atx-header ()
"Test `markdown-demote' cycling for atx headers."
(markdown-test-string "##### test"
(markdown-test-string "# test"
(call-interactively 'markdown-demote)
(should (string-equal (buffer-string) "###### test ######"))
(should (string-equal (buffer-string) "## test ##"))
(call-interactively 'markdown-demote)
(should (string-equal (buffer-string) "# test #"))
(should (string-equal (buffer-string) "### test ###"))
(call-interactively 'markdown-demote)
(should (string-equal (buffer-string) "## test ##"))))
(should (string-equal (buffer-string) "#### test ####"))
(call-interactively 'markdown-demote)
(should (string-equal (buffer-string) "##### test #####"))
(call-interactively 'markdown-demote)
(should (string-equal (buffer-string) "###### test ######"))
(call-interactively 'markdown-demote)
(should (string-equal (buffer-string) "###### test ######"))))

(ert-deftest test-markdown-cycle/setext-header ()
"Test `markdown-demote' cycling for setext headers."
Expand All @@ -1725,7 +1731,7 @@ Should not cause an infinite loop."
(call-interactively 'markdown-demote)
(should (string-equal (buffer-string) "###### test ######"))
(call-interactively 'markdown-demote)
(should (string-equal (buffer-string) "# test #"))))
(should (string-equal (buffer-string) "###### test ######"))))

(ert-deftest test-markdown-cycle/hr ()
"Test cycling of horizontal rules."
Expand Down

0 comments on commit 9c7c0c4

Please sign in to comment.