Skip to content

Commit 118ecba

Browse files
committed
Document move-dup functions and remove interactivity for duplicate-line and duplicate-region
1 parent ed06e59 commit 118ecba

File tree

1 file changed

+80
-20
lines changed

1 file changed

+80
-20
lines changed

move-dup.el

Lines changed: 80 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,17 @@
1616
;; this program. If not, see <http://www.gnu.org/licenses/>.
1717

1818
;; Author: Jimmy Yuen Ho Wong <[email protected]>
19-
;; Version: 0.1.2
20-
;; Created 11 June 2014
19+
;; Version: 0.1.3
20+
;; Created: 11 June 2014
2121
;; Keywords: convenience wp
2222

2323
;; This file is not part of GNU Emacs.
2424

2525
;;; Commentary:
2626

2727
;; This package offers convenient editing commands much like Eclipse's ability
28-
;; to move and duplicate lines or rectangular selections.
28+
;; to move and duplicate lines or rectangular selections by way of
29+
;; `move-dup-mode'.
2930

3031
;; If you aren't using `package.el' or plan to customize the default
3132
;; key-bindings, you need to put `move-dup.el' into your Emacs' load-path and
@@ -34,7 +35,7 @@
3435
;; (require 'move-dup)
3536

3637
;; If you don't want to toggle the minor mode, you can bind these functions like
37-
;; so. All of these functions work on a single line or a rectangle.
38+
;; so. All of these functions work on a single line or a rectangle.
3839

3940
;; (global-set-key (kbd "M-<up>") 'md/move-lines-up)
4041
;; (global-set-key (kbd "M-<down>") 'md/move-lines-down)
@@ -43,13 +44,19 @@
4344

4445
;; If you used `package.el' to install `move-dup.el', this is equivalent to all
4546
;; of the above.
46-
;; (global-move-dup-minor-mode)
47+
;; (global-move-dup-mode)
4748

48-
;; You can also turn on `move-dup-minor-mode' individually for each buffer.
49-
;; (move-dup-minor-mode)
49+
;; You can also turn on `move-dup-mode' individually for each buffer.
50+
;; (move-dup-mode)
5051

5152
;;; Code:
5253
(defun md/ensure-rectangle ()
54+
"Normalizes the selection by making sure it's always a rectangle.
55+
56+
After normalization, the point always comes after mark. The
57+
region will always be expanded such that it will always begin
58+
from the beginning of the line the mark is on, and ends at the
59+
beginning of the next line of the end of the region."
5360
(if (< (point) (mark))
5461
(exchange-point-and-mark))
5562
(when (not (char-equal (char-before (region-end)) 10))
@@ -60,7 +67,14 @@
6067
(exchange-point-and-mark))
6168

6269
(defun md/move-region (&optional n)
63-
(interactive "p")
70+
"Interactive function to move the currect selection N lines.
71+
72+
If the selection is not a rectangle, this function will expand
73+
the selection to a rectangle via the
74+
function `(ensure-rectangle)` and move it accordingly. If the
75+
prefix N is positive, this function moves the rectangle forward N
76+
lines; otherwise backward."
77+
(interactive "*p")
6478
(md/ensure-rectangle)
6579
(let* ((start (region-beginning))
6680
(end (region-end))
@@ -80,41 +94,69 @@
8094
(exchange-point-and-mark))))
8195

8296
(defun md/move-line (&optional n)
83-
(interactive "p")
97+
"Interactive function to move the current line N line.
98+
99+
If the prefix N is positive, this function moves the current line
100+
forward N lines; otherwise backward."
101+
(interactive "*p")
84102
(let ((col (current-column)))
85103
(forward-line 1)
86104
(transpose-lines n)
87105
(forward-line -1)
88106
(move-to-column col)))
89107

90108
(defun md/move-line-or-region (n)
109+
"Decides whether a line or selection should be moved N lines."
91110
(if (use-region-p)
92111
(md/move-region n)
93112
(md/move-line n)))
94113

95114
(defun md/move-lines-up (&optional n)
96-
(interactive "p")
115+
"Interactive function to move the current line or selection up.
116+
117+
If the prefix N is positive, this function moves the current line
118+
or selection up N lines; otherwise down."
119+
(interactive "*p")
97120
(md/move-line-or-region (if (or (null n) (= n 0)) -1 (- n))))
98121

99122
(defun md/move-lines-down (&optional n)
100-
(interactive "p")
123+
"Interactive function to move the current line or selection down.
124+
125+
If the prefix N is positive, this function moves the current line
126+
or selection down N lines; otherwise up."
127+
(interactive "*p")
101128
(md/move-line-or-region (if (or (null n) (= n 0)) 1 n)))
102129

103-
(defun md/duplicate-up (&optional times)
104-
(interactive "p")
105-
(dotimes (i times) (md/duplicate-line-or-region "up")))
130+
(defun md/duplicate-up (&optional n)
131+
"Interactive function to duplicate the current line or selection upward.
132+
133+
If the prefix N is positive, this function makes N duplicates of
134+
the current line or selection and place them above the current
135+
line or selection."
136+
(interactive "*p")
137+
(dotimes (i n) (md/duplicate-line-or-region "up")))
138+
139+
(defun md/duplicate-down (&optional n)
140+
"Interactive function to duplicate the current line or selection downward.
106141
107-
(defun md/duplicate-down (&optional times)
108-
(interactive "p")
109-
(dotimes (i times) (md/duplicate-line-or-region "down")))
142+
If the prefix N is positive, this function makes N duplicates of
143+
the current line or selection and place them below the current
144+
line or selection."
145+
(interactive "*p")
146+
(dotimes (i n) (md/duplicate-line-or-region "down")))
110147

111148
(defun md/duplicate-line-or-region (direction)
149+
"Decides whether a line or selection should be duplicated.
150+
151+
DIRECTION must be one of \"up\" or \"down\"."
112152
(if (use-region-p)
113153
(md/duplicate-region direction)
114154
(md/duplicate-line direction)))
115155

116156
(defun md/duplicate-line (direction)
117-
(interactive "p")
157+
"Function to duplicate the current line.
158+
159+
DIRECTION must be one of \"up\" or \"down\"."
118160
(let ((text (buffer-substring (line-beginning-position) (line-end-position)))
119161
(col (current-column)))
120162
(forward-line)
@@ -125,7 +167,16 @@
125167
(move-to-column col)))
126168

127169
(defun md/duplicate-region (direction)
128-
(interactive "p")
170+
"Function to duplicate the current selection.
171+
172+
DIRECTION must be one of \"up\" or \"down\".
173+
174+
If the selection is not a rectangle, this function will expand
175+
the selection to a rectangle via the
176+
function `(ensure-rectangle)` and duplicate it accordingly. If
177+
the DIRECTION is \"up\", this function duplicates the selected
178+
rectangle and places it __below__ the selection; __above__ if
179+
DIRECTION is \"down\"."
129180
(md/ensure-rectangle)
130181
(let* ((start (region-beginning))
131182
(end (region-end))
@@ -149,14 +200,23 @@
149200

150201
;;;###autoload
151202
(define-minor-mode move-dup-mode
152-
"Eclipse-like moving and duplicating lines or rectangles with default key-bindings."
203+
"Eclipse-like moving and duplicating lines or rectangles with
204+
default key bindings.
205+
206+
The default key bindings are:
207+
208+
\([M-up] . md/move-lines-up)
209+
\([M-down] . md/move-lines-down)
210+
\([C-M-up] . md/duplicate-up)
211+
\([C-M-down] . md/duplicate-down)"
153212
:lighter " md"
154213
:keymap '(([M-up] . md/move-lines-up)
155214
([M-down] . md/move-lines-down)
156215
([C-M-up] . md/duplicate-up)
157216
([C-M-down] . md/duplicate-down)))
158217

159218
(defun move-dup-on ()
219+
"Decides whether the function `move-dup-mode' should be called with t."
160220
(unless (minibufferp)
161221
(move-dup-mode 1)))
162222

0 commit comments

Comments
 (0)