.emacs.d

My Emacs configuration files.
Log | Files | Refs | README

commit 2f1b926a60568d232f0097abded4a5ce9d824138
parent 1fd3d1611e5dfe0e753de050b2d839534bbd7d0c
Author: Hugo Soucy <hugo.soucy@toumoro.com>
Date:   Wed, 18 Sep 2019 13:42:31 -0400

Paste a new duplicate line function

Diffstat:
Mdefuns/duplicate-line.el | 47++++++++++++++++++++++++++++++++++-------------
Mpartials/key-bindings.el | 4++--
2 files changed, 36 insertions(+), 15 deletions(-)

diff --git a/defuns/duplicate-line.el b/defuns/duplicate-line.el @@ -1,15 +1,36 @@ ;; Duplicating lines & commenting them -(defun djcb-duplicate-line (&optional commentfirst) - "comment line at point; if COMMENTFIRST is non-nil, comment the original" - (interactive) - (beginning-of-line) - (let - ((beg (point))) - (end-of-line) - (let ((str (buffer-substring beg (point)))) - (when commentfirst - (comment-region beg (point))) - (insert-string - (concat (if (= 0 (forward-line 1)) "" "\n") str "\n")) - (forward-line -1)))) +(defun duplicate-line (arg) + "Duplicate current line, leaving point in lower line." + (interactive "*p") + + ;; save the point for undo + (setq buffer-undo-list (cons (point) buffer-undo-list)) + + ;; local variables for start and end of line + (let ((bol (save-excursion (beginning-of-line) (point))) + eol) + (save-excursion + + ;; don't use forward-line for this, because you would have + ;; to check whether you are at the end of the buffer + (end-of-line) + (setq eol (point)) + + ;; store the line and disable the recording of undo information + (let ((line (buffer-substring bol eol)) + (buffer-undo-list t) + (count arg)) + ;; insert the line arg times + (while (> count 0) + (newline) ;; because there is no newline in 'line' + (insert line) + (setq count (1- count))) + ) + + ;; create the undo information + (setq buffer-undo-list (cons (cons eol (point)) buffer-undo-list))) + ) ; end-of-let + + ;; put the point in the lowest line and return + (next-line arg)) diff --git a/partials/key-bindings.el b/partials/key-bindings.el @@ -14,10 +14,10 @@ (global-set-key (kbd "C--") 'text-scale-decrease) ;; Duplicate a line -(global-set-key (kbd "M-RET") 'djcb-duplicate-line) +(global-set-key (kbd "M-RET") 'duplicate-line) ;; Duplicate a line and comment the first -(global-set-key (kbd "C-c y") (lambda()(interactive)(djcb-duplicate-line t))) +(global-set-key (kbd "C-c y") (lambda()(interactive)(duplicate-line t))) ;; Compress a few lines into one (global-set-key (kbd "M-é") 'top-join-line)