meow-edit / meow

Yet another modal editing on Emacs / 猫态编辑
GNU General Public License v3.0
1.07k stars 128 forks source link

meow-line should grow partially-completed lines #547

Open aecepoglu opened 3 months ago

aecepoglu commented 3 months ago

if the selection is like the following (M: mark, P: point)

........M.........
....................
....P.........

then meow-line should turn it into the following and change the selection-type to (expand . line)

M.................
....................
.............P

but it selects only the line point is on:

.................
....................
M.............P

This can be accomplished with a function like so, and I can open a PR for this you agree with the change:

(defun my:meow-line (n &optional expand)
  "Select the current line, eol is not included.

Create selection with type (expand . line).
For the selection with type (expand . line), expand it by line.
For the other types, change selection type to (expand . line)
 and grow selection to cover the entire lines

Prefix:
numeric, repeat times.
"
  (interactive "p")
  (let* ((orig (mark t))
         (n (if (meow--direction-backward-p)
                (- n)
              n))
         (forward (> n 0)))
    (cond
     ((not (region-active-p))
      (let ((m (if forward
                   (line-beginning-position)
                 (line-end-position)))
            (p (save-mark-and-excursion
                 (if forward
                     (progn
                       (forward-line (1- n))
                       (line-end-position))
                   (progn
                     (forward-line (1+ n))
                     (when (meow--empty-line-p)
                       (backward-char 1))
                     (line-beginning-position))))))
        (thread-first
          (meow--make-selection '(expand . line) m p expand)
          (meow--select))
        (meow--maybe-highlight-num-positions '(meow--backward-line-1 . meow--forward-line-1))))
     ((equal '(expand . line) (meow--selection-type))
      (let (p)
        (save-mark-and-excursion
          (forward-line n)
          (goto-char
           (if forward
               (setq p (line-end-position))
             (setq p (line-beginning-position)))))
        (thread-first
          (meow--make-selection '(expand . line) orig p expand)
          (meow--select))
        (meow--maybe-highlight-num-positions '(meow--backward-line-1 . meow--forward-line-1))))
     (t
      (let ((m (save-mark-and-excursion
         (goto-char orig)
         (if forward (line-beginning-position) (line-end-position))))
        (p (if forward (line-end-position) (line-beginning-position))))
    (thread-first
          (meow--make-selection '(expand . line) m p expand)
          (meow--select))
    (meow--maybe-highlight-num-positions '(meow--backward-line-1 . meow--forward-line-1)))))))
DogLooksGood commented 3 months ago

Meow introduces a concept selection. Here is the explanation.

https://github.com/meow-edit/meow/blob/master/EXPLANATION.org#selection

aecepoglu commented 3 months ago

Yes, I am aware meow has selections. I am a previous kakoune user so I am comfortable using selections.

It's just that meow-line should not cancel the selection. It should instead change the type of the selection and grow it

DogLooksGood commented 3 months ago

The selection will be expanded when current selection is expandable, and has the same type with command. In your case, I guess the selection is created with char movements, it will be canceled when met a line command. This selection is not the one in kakoune.

aecepoglu commented 3 months ago

I understand. my use case is:

when I press the keys o e (meaning (meow-block) (meow-line)) I don't want (meow-line) to cancel my existing selection. Similarly when I do o . l (meaning (meow-block) (meow-bounds-of-thing 'line)) I want to grow my region to contain full lines.

If I wanted to cancel my selection, I'd invoke (meow-cancel) myself.

DogLooksGood commented 3 months ago

To force expand, there's a command meow-line-expand, however it doesn't expand the beginning and the end at the same time. Probably we should make it work on all lines, since the line selection should always keep the same shape.

aecepoglu commented 3 months ago

Would you be OK with me opening a PR for that?

I thin kthe answer is the final cond statement from my code sample above.

DogLooksGood commented 3 months ago

meow-line should stay the same. It should cancel the selection with other types and expand itself.

And we can update meow-line-expand according to your code, also add it to the documentation. As it's still a preserved, undocumented command at the moment.

Yes, I'm OK with a PR.