crux icon indicating copy to clipboard operation
crux copied to clipboard

`crux-duplicate<-and-comment>-current-line-or-region` using regions blends lines

Open Walheimat opened this issue 3 years ago • 2 comments

Expected behavior

Given a region containing the following lines,

(message "the crux of the matter")
(message "hello from the magic tavern")

crux-duplicate-current-line-or-region yields:

(message "the crux of the matter")
(message "hello from the magic tavern")
(message "the crux of the matter")
(message "hello from the magic tavern")

Using crux-duplicate-and-comment-current-line-or-region yields:

;; (message "the crux of the matter")
;; (message "hello from the magic tavern")
(message "the crux of the matter")
(message "hello from the magic tavern")

Actual behavior

Using crux-duplicate-current-line-or-region I get:

(message "the crux of the matter")
(message "hello from the magic tavern")(message "the crux of the matter")
(message "hello from the magic tavern")

Using crux-duplicate-and-comment-current-line-or-region I get:

;; (message "the crux of the matter")
;; (message "hello from the magic tavern")(message "the crux of the matter")
(message "hello from the magic tavern")

Steps to reproduce the problem

Call the mentioned commands with active region.

Commenting out these line makes the commands work as before:

(unless (use-region-p)
        (newline))

Not sure if that is now the desired behavior but it confuses me.

Environment & Version information

crux version information

I can't find any crux-version command, but from the package:

;; Package-Version: 20210811.436
;; Package-Commit: 6bfd212a7f7ae32e455802fde1f9e3f4fba932a0
;; Version: 0.4.0

Emacs version

28.0.50

Operating system

gnu/linux (Ubuntu 21.04)

Walheimat avatar Sep 08 '21 21:09 Walheimat

Here's the work around that I have:

(defun jnf/duplicate-current-line-or-lines-of-region (arg)
  "Duplicate ARG times current line or the lines of the current region."
  (interactive "p")
  (if (use-region-p)
      (progn
        (when (> (point) (mark))
          (exchange-point-and-mark))
        (beginning-of-line)
        (exchange-point-and-mark)
        (end-of-line)
        (goto-char (+ (point) 1))
        (exchange-point-and-mark)
        (let* ((end (mark))
               (beg (point))
               (region
                (buffer-substring-no-properties beg end)))
          (dotimes (_i arg)
            (goto-char end)
            (insert region)
            (setq end (point)))))
    (crux-duplicate-current-line-or-region arg)))

The behavior is if I'm not acting on a region, use crux-duplicate-current-line-or-region. Otherwise, duplicate each of the lines in the region.

Before: before

After: after

Note, this duplicates the entire lines that are in the region (and not the region itself). I find this is the behavior that I most often want.

jeremyf avatar Nov 20 '21 20:11 jeremyf

Seem this issue came from https://github.com/bbatsov/crux/pull/80

TxGVNN avatar Mar 16 '22 15:03 TxGVNN