diff options
author | JSDurand <mmemmew@gmail.com> | 2021-08-17 11:04:27 +0800 |
---|---|---|
committer | JSDurand <mmemmew@gmail.com> | 2021-08-17 11:04:27 +0800 |
commit | 280abe85bb2ecdbac5f427bd44871c2ad716a985 (patch) | |
tree | 4118743455b18614c3b001ed9d2ed13f8665ae84 | |
parent | 594dc7eda49288282ee8c02fc0594fc3aeb97fad (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.
-rw-r--r-- | basic.el | 4 | ||||
-rw-r--r-- | common.el | 57 |
2 files changed, 61 insertions, 0 deletions
@@ -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 @@ -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. |