From 1c9368970134d0be60c425c30d65f34b882b24ea Mon Sep 17 00:00:00 2001 From: Jeremy Bi Date: Thu, 20 Mar 2014 11:05:40 +0800 Subject: [PATCH] Attempt to refactor two "duplicate" functions. Attempt to extract a generic function `prelude-get-positions-of-line-or-region` from `prelude-duplicate-current-line-or-region` and `prelude-duplicate-and-comment-current-line-or-region` --- core/prelude-core.el | 65 ++++++++++++++++++++++---------------------- 1 file changed, 33 insertions(+), 32 deletions(-) diff --git a/core/prelude-core.el b/core/prelude-core.el index fa630b3..278c8a1 100644 --- a/core/prelude-core.el +++ b/core/prelude-core.el @@ -198,50 +198,51 @@ point reaches the beginning or end of the buffer, stop there." (kill-new filename) (message "Copied buffer file name '%s' to the clipboard." filename)))) +(defun prelude-get-positions-of-line-or-region () + "Return positions (beg . end) of the current line +or region." + (let (beg end) + (if (and mark-active (> (point) (mark))) + (exchange-point-and-mark)) + (setq beg (line-beginning-position)) + (if mark-active + (exchange-point-and-mark)) + (setq end (line-end-position)) + (cons beg end))) + (defun prelude-duplicate-current-line-or-region (arg) "Duplicates the current line or region ARG times. If there's no region, the current line will be duplicated. However, if there's a region, all lines that region covers will be duplicated." (interactive "p") - (let (beg end (origin (point))) - (if (and mark-active (> (point) (mark))) - (exchange-point-and-mark)) - (setq beg (line-beginning-position)) - (if mark-active - (exchange-point-and-mark)) - (setq end (line-end-position)) - (let ((region (buffer-substring-no-properties beg end))) - (-dotimes arg - (lambda (n) - (goto-char end) - (newline) - (insert region) - (setq end (point)))) - (goto-char (+ origin (* (length region) arg) arg))))) + (pcase-let* ((origin (point)) + (`(,beg . ,end) (prelude-get-positions-of-line-or-region)) + (region (buffer-substring-no-properties beg end))) + (-dotimes arg + (lambda (n) + (goto-char end) + (newline) + (insert region) + (setq end (point)))) + (goto-char (+ origin (* (length region) arg) arg)))) -;; TODO: Remove code duplication by extracting something more generic (defun prelude-duplicate-and-comment-current-line-or-region (arg) "Duplicates and comments the current line or region ARG times. If there's no region, the current line will be duplicated. However, if there's a region, all lines that region covers will be duplicated." (interactive "p") - (let (beg end (origin (point))) - (if (and mark-active (> (point) (mark))) - (exchange-point-and-mark)) - (setq beg (line-beginning-position)) - (if mark-active - (exchange-point-and-mark)) + (pcase-let* ((origin (point)) + (`(,beg . ,end) (prelude-get-positions-of-line-or-region)) + (region (buffer-substring-no-properties beg end))) + (comment-or-uncomment-region beg end) (setq end (line-end-position)) - (let ((region (buffer-substring-no-properties beg end))) - (comment-or-uncomment-region beg end) - (setq end (line-end-position)) - (-dotimes arg - (lambda (n) - (goto-char end) - (newline) - (insert region) - (setq end (point)))) - (goto-char (+ origin (* (length region) arg) arg))))) + (-dotimes arg + (lambda (n) + (goto-char end) + (newline) + (insert region) + (setq end (point)))) + (goto-char (+ origin (* (length region) arg) arg)))) (defun prelude-rename-file-and-buffer () "Renames current buffer and file it is visiting."