|
16 | 16 | ;; this program. If not, see <http://www.gnu.org/licenses/>. |
17 | 17 |
|
18 | 18 | ;; 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 |
21 | 21 | ;; Keywords: convenience wp |
22 | 22 |
|
23 | 23 | ;; This file is not part of GNU Emacs. |
24 | 24 |
|
25 | 25 | ;;; Commentary: |
26 | 26 |
|
27 | 27 | ;; 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'. |
29 | 30 |
|
30 | 31 | ;; If you aren't using `package.el' or plan to customize the default |
31 | 32 | ;; key-bindings, you need to put `move-dup.el' into your Emacs' load-path and |
|
34 | 35 | ;; (require 'move-dup) |
35 | 36 |
|
36 | 37 | ;; 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. |
38 | 39 |
|
39 | 40 | ;; (global-set-key (kbd "M-<up>") 'md/move-lines-up) |
40 | 41 | ;; (global-set-key (kbd "M-<down>") 'md/move-lines-down) |
|
43 | 44 |
|
44 | 45 | ;; If you used `package.el' to install `move-dup.el', this is equivalent to all |
45 | 46 | ;; of the above. |
46 | | -;; (global-move-dup-minor-mode) |
| 47 | +;; (global-move-dup-mode) |
47 | 48 |
|
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) |
50 | 51 |
|
51 | 52 | ;;; Code: |
52 | 53 | (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." |
53 | 60 | (if (< (point) (mark)) |
54 | 61 | (exchange-point-and-mark)) |
55 | 62 | (when (not (char-equal (char-before (region-end)) 10)) |
|
60 | 67 | (exchange-point-and-mark)) |
61 | 68 |
|
62 | 69 | (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") |
64 | 78 | (md/ensure-rectangle) |
65 | 79 | (let* ((start (region-beginning)) |
66 | 80 | (end (region-end)) |
|
80 | 94 | (exchange-point-and-mark)))) |
81 | 95 |
|
82 | 96 | (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") |
84 | 102 | (let ((col (current-column))) |
85 | 103 | (forward-line 1) |
86 | 104 | (transpose-lines n) |
87 | 105 | (forward-line -1) |
88 | 106 | (move-to-column col))) |
89 | 107 |
|
90 | 108 | (defun md/move-line-or-region (n) |
| 109 | + "Decides whether a line or selection should be moved N lines." |
91 | 110 | (if (use-region-p) |
92 | 111 | (md/move-region n) |
93 | 112 | (md/move-line n))) |
94 | 113 |
|
95 | 114 | (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") |
97 | 120 | (md/move-line-or-region (if (or (null n) (= n 0)) -1 (- n)))) |
98 | 121 |
|
99 | 122 | (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") |
101 | 128 | (md/move-line-or-region (if (or (null n) (= n 0)) 1 n))) |
102 | 129 |
|
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. |
106 | 141 |
|
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"))) |
110 | 147 |
|
111 | 148 | (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\"." |
112 | 152 | (if (use-region-p) |
113 | 153 | (md/duplicate-region direction) |
114 | 154 | (md/duplicate-line direction))) |
115 | 155 |
|
116 | 156 | (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\"." |
118 | 160 | (let ((text (buffer-substring (line-beginning-position) (line-end-position))) |
119 | 161 | (col (current-column))) |
120 | 162 | (forward-line) |
|
125 | 167 | (move-to-column col))) |
126 | 168 |
|
127 | 169 | (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\"." |
129 | 180 | (md/ensure-rectangle) |
130 | 181 | (let* ((start (region-beginning)) |
131 | 182 | (end (region-end)) |
|
149 | 200 |
|
150 | 201 | ;;;###autoload |
151 | 202 | (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)" |
153 | 212 | :lighter " md" |
154 | 213 | :keymap '(([M-up] . md/move-lines-up) |
155 | 214 | ([M-down] . md/move-lines-down) |
156 | 215 | ([C-M-up] . md/duplicate-up) |
157 | 216 | ([C-M-down] . md/duplicate-down))) |
158 | 217 |
|
159 | 218 | (defun move-dup-on () |
| 219 | + "Decides whether the function `move-dup-mode' should be called with t." |
160 | 220 | (unless (minibufferp) |
161 | 221 | (move-dup-mode 1))) |
162 | 222 |
|
|
0 commit comments