summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--basic.el4
-rw-r--r--common.el57
2 files changed, 61 insertions, 0 deletions
diff --git a/basic.el b/basic.el
index e23688d..cd5fdff 100644
--- a/basic.el
+++ b/basic.el
@@ -209,6 +209,10 @@ This will maintain the frame's width and height as well."
(display-buffer-in-tab durand-display-in-one-window)
(tab-name . "man page"))))
+;;; copy-duplicate dwim
+
+(define-key global-map (vector ?\s-\;) #'durand-copy-line-dwim)
+
;;; Custom kill buffer function
;;;###autoload
diff --git a/common.el b/common.el
index 0417da5..75fe201 100644
--- a/common.el
+++ b/common.el
@@ -393,6 +393,63 @@ Comparison is done by TEST if that is non-nil, else by `equal'."
((setq temp (cdr temp)))))
temp))
+;;;###autoload
+(defun durand-copy-line-dwim (&optional arg)
+ "Copy line or duplicate line, according to ARG.
+If ARG is nil, then duplicate line and put cursor in the line
+below.
+
+If ARG is '(4), just copy line.
+
+If ARG is '(16), copy and duplicate line at the same time.
+
+If ARG is a number strictly greater than 0, then copy and
+duplicate the line ARG times.
+
+In any other case, copy line and use minibuffer to read a number,
+and duplicate the line at the line that is ARG away from the
+current line; negative ARG means to put the line upwards."
+ (interactive "P")
+ (cond
+ ((null arg)
+ (let ((line (buffer-substring (line-beginning-position)
+ (line-end-position))))
+ (goto-char (line-end-position))
+ (insert "\n" line)))
+ ((equal arg (list 4))
+ (let ((line (concat (buffer-substring (line-beginning-position)
+ (line-end-position))
+ "\n")))
+ (kill-new line)
+ (message "Copied \"%s\"" (substring-no-properties line))))
+ ((equal arg (list 16))
+ (let ((line (buffer-substring (line-beginning-position)
+ (line-end-position))))
+ (kill-new (concat line "\n"))
+ (message "Copied \"%s\n\"" (substring-no-properties line))
+ (goto-char (line-end-position))
+ (insert "\n")
+ (insert line)))
+ ((and (integerp arg)
+ (> arg 0))
+ (let* ((line (buffer-substring (line-beginning-position)
+ (line-end-position)))
+ (lines (mapconcat (lambda (_arg) line)
+ (number-sequence 1 arg)
+ "\n")))
+ (kill-new (concat line "\n"))
+ (message "Copied \"%s\n\"" (substring-no-properties line))
+ (insert "\n" lines)))
+ (t
+ (let ((line (buffer-substring (line-beginning-position)
+ (line-end-position)))
+ (num (read-number "Duplicate to which line? ")))
+ (kill-new (concat line "\n"))
+ (message "Copied \"%s\n\"" (substring-no-properties line))
+ (goto-char (line-end-position (cond ((/= num 0) (1+ num))
+ (num))))
+ (insert "\n" line)))))
+
(provide 'common)
;;; common.el ends here.