summaryrefslogtreecommitdiff
path: root/lisp
diff options
context:
space:
mode:
Diffstat (limited to 'lisp')
-rw-r--r--lisp/misc.el35
1 files changed, 28 insertions, 7 deletions
diff --git a/lisp/misc.el b/lisp/misc.el
index ca013d5f72f..898fe9dd168 100644
--- a/lisp/misc.el
+++ b/lisp/misc.el
@@ -63,21 +63,42 @@ Also see the `duplicate-line' command."
(+ n (point)))))))
(insert string)))
+(defcustom duplicate-line-final-position 0
+ "Where to put point after duplicating the line with `duplicate-line'.
+When 0, leave point on the original line.
+When 1, move point to the first new line.
+When -1, move point to the last new line.
+The same column is preserved after moving to a new line."
+ :type '(choice (const :tag "Leave point on old line" 0)
+ (const :tag "Move point to first new line" 1)
+ (const :tag "Move point to last new line" -1)
+ (integer))
+ :group 'editing
+ :version "29.1")
+
;;;###autoload
(defun duplicate-line (&optional n)
"Duplicate the current line N times.
Interactively, N is the prefix numeric argument, and defaults to 1.
+The user option `duplicate-line-final-position' specifies where to
+move point after duplicating the line.
Also see the `copy-from-above-command' command."
(interactive "p")
(unless n
(setq n 1))
- (let ((line (buffer-substring (line-beginning-position) (line-end-position))))
- (save-excursion
- (forward-line 1)
- (unless (bolp)
- (insert "\n"))
- (dotimes (_ n)
- (insert line "\n")))))
+ (let ((line (buffer-substring (line-beginning-position) (line-end-position)))
+ (pos (point))
+ (col (current-column)))
+ (forward-line 1)
+ (unless (bolp)
+ (insert "\n"))
+ (dotimes (_ n)
+ (insert line "\n"))
+ (unless (< duplicate-line-final-position 0)
+ (goto-char pos))
+ (unless (eq duplicate-line-final-position 0)
+ (forward-line duplicate-line-final-position)
+ (move-to-column col))))
(declare-function rectangle--duplicate-right "rect" (n))