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`
This commit is contained in:
parent
08494c79db
commit
1c93689701
1 changed files with 33 additions and 32 deletions
|
@ -198,50 +198,51 @@ 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-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)
|
(defun prelude-duplicate-current-line-or-region (arg)
|
||||||
"Duplicates the current line or region ARG times.
|
"Duplicates 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))
|
(-dotimes arg
|
||||||
(if mark-active
|
(lambda (n)
|
||||||
(exchange-point-and-mark))
|
(goto-char end)
|
||||||
(setq end (line-end-position))
|
(newline)
|
||||||
(let ((region (buffer-substring-no-properties beg end)))
|
(insert region)
|
||||||
(-dotimes arg
|
(setq end (point))))
|
||||||
(lambda (n)
|
(goto-char (+ origin (* (length region) arg) arg))))
|
||||||
(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)
|
(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))
|
(comment-or-uncomment-region beg end)
|
||||||
(if mark-active
|
|
||||||
(exchange-point-and-mark))
|
|
||||||
(setq end (line-end-position))
|
(setq end (line-end-position))
|
||||||
(let ((region (buffer-substring-no-properties beg end)))
|
(-dotimes arg
|
||||||
(comment-or-uncomment-region beg end)
|
(lambda (n)
|
||||||
(setq end (line-end-position))
|
(goto-char end)
|
||||||
(-dotimes arg
|
(newline)
|
||||||
(lambda (n)
|
(insert region)
|
||||||
(goto-char end)
|
(setq end (point))))
|
||||||
(newline)
|
(goto-char (+ origin (* (length region) arg) arg))))
|
||||||
(insert region)
|
|
||||||
(setq end (point))))
|
|
||||||
(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."
|
||||||
|
|
Loading…
Reference in a new issue