Merge pull request #499 from bixuanzju/patch-1

Attempt to refactor two "duplicate" functions.
This commit is contained in:
Bozhidar Batsov 2014-03-24 11:48:36 +02:00
commit 180d963b25

View file

@ -198,41 +198,42 @@ point reaches the beginning or end of the buffer, stop there."
(kill-new filename) (kill-new filename)
(message "Copied buffer file name '%s' to the clipboard." filename)))) (message "Copied buffer file name '%s' to the clipboard." filename))))
(defun prelude-duplicate-current-line-or-region (arg) (defun prelude-get-positions-of-line-or-region ()
"Duplicates the current line or region ARG times. "Return positions (beg . end) of the current line
If there's no region, the current line will be duplicated. However, if or region."
there's a region, all lines that region covers will be duplicated." (let (beg end)
(interactive "p")
(let (beg end (origin (point)))
(if (and mark-active (> (point) (mark))) (if (and mark-active (> (point) (mark)))
(exchange-point-and-mark)) (exchange-point-and-mark))
(setq beg (line-beginning-position)) (setq beg (line-beginning-position))
(if mark-active (if mark-active
(exchange-point-and-mark)) (exchange-point-and-mark))
(setq end (line-end-position)) (setq end (line-end-position))
(let ((region (buffer-substring-no-properties beg end))) (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")
(pcase-let* ((origin (point))
(`(,beg . ,end) (prelude-get-positions-of-line-or-region))
(region (buffer-substring-no-properties beg end)))
(-dotimes arg (-dotimes arg
(lambda (n) (lambda (n)
(goto-char end) (goto-char end)
(newline) (newline)
(insert region) (insert region)
(setq end (point)))) (setq end (point))))
(goto-char (+ origin (* (length region) arg) arg))))) (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) (defun prelude-duplicate-and-comment-current-line-or-region (arg)
"Duplicates and comments the current line or region ARG times. "Duplicates and comments the current line or region ARG times.
If there's no region, the current line will be duplicated. However, if 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." there's a region, all lines that region covers will be duplicated."
(interactive "p") (interactive "p")
(let (beg end (origin (point))) (pcase-let* ((origin (point))
(if (and mark-active (> (point) (mark))) (`(,beg . ,end) (prelude-get-positions-of-line-or-region))
(exchange-point-and-mark)) (region (buffer-substring-no-properties beg end)))
(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)))
(comment-or-uncomment-region beg end) (comment-or-uncomment-region beg end)
(setq end (line-end-position)) (setq end (line-end-position))
(-dotimes arg (-dotimes arg
@ -241,7 +242,7 @@ there's a region, all lines that region covers will be duplicated."
(newline) (newline)
(insert region) (insert region)
(setq end (point)))) (setq end (point))))
(goto-char (+ origin (* (length region) arg) arg))))) (goto-char (+ origin (* (length region) arg) arg))))
(defun prelude-rename-file-and-buffer () (defun prelude-rename-file-and-buffer ()
"Renames current buffer and file it is visiting." "Renames current buffer and file it is visiting."