summaryrefslogtreecommitdiff
path: root/common.el
diff options
context:
space:
mode:
authorJSDurand <mmemmew@gmail.com>2021-08-17 11:04:27 +0800
committerJSDurand <mmemmew@gmail.com>2021-08-17 11:04:27 +0800
commit280abe85bb2ecdbac5f427bd44871c2ad716a985 (patch)
tree4118743455b18614c3b001ed9d2ed13f8665ae84 /common.el
parent594dc7eda49288282ee8c02fc0594fc3aeb97fad (diff)
new function: durand-copy-line-dwim
* basic.el (global-map): Bind to "s-;". * common.el (durand-copy-line-dwim): This meets my needs to copy or duplicate lines.
Diffstat (limited to 'common.el')
-rw-r--r--common.el57
1 files changed, 57 insertions, 0 deletions
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.