lem icon indicating copy to clipboard operation
lem copied to clipboard

kill region or current line

Open timmydo opened this issue 2 years ago • 5 comments

In my emacs config I have something like this:

(put 'kill-region 'interactive-form      
     '(interactive
       (if (use-region-p)
           (list (region-beginning) (region-end))
         (list (line-beginning-position) (line-beginning-position 2)))))

So if I don't have a region and I press C-w it will kill the current line.

For Lem I threw this together (but it doesn't work on the last line in the buffer):

(defun wipe-region (start end)
  (let ((repeat-command (continue-flag :kill)))
	  (let ((killed-string (delete-character start (count-characters start end))))
	    (with-killring-context (:appending repeat-command)
	      (copy-to-clipboard-with-killring killed-string)))))

(define-command kill-region-or-line () ()
  "Kill the text of region or the current line if no region."
  (if (buffer-mark (current-buffer))
      (with-point ((start (cursor-region-beginning (current-point)))
                   (end (cursor-region-end (current-point))))
	(wipe-region start end))
      (with-point ((start (current-point))
		   (end (current-point)))
	(line-start start)
	(line-start end)
	(line-offset end 1)
	(when (point= start end)
	  (line-end end))
	(wipe-region start end)
	)))


(define-key lem:*global-keymap* "C-w" 'kill-region-or-line)

I'm wondering if there is a better way to get this behavior in lem without resorting to custom code? (I was reminded of this when I saw #1055. )

timmydo avatar Sep 04 '23 16:09 timmydo

I think this may work:

(define-command kill-region-or-line () ()
  "Kill the text of region or the current line if no region."
  (if (buffer-mark (current-buffer))
        (kill-region (region-beginning)
                     (region-end))
        (and (line-start (current-point))
             (kill-whole-line))))

Keep in mind that this doesn't work with the vi-mode visual line, mostly because it doesn't uses the emacs-mode mark

Sasanidas avatar Sep 04 '23 19:09 Sasanidas

I also wrote.

(defmethod execute :around (mode (command kill-region) argument)
  (if (buffer-mark-p (current-buffer))
      (call-next-method)
      (with-point ((start (current-point))
                   (end (current-point)))
        (line-start start)
        (line-offset end 1)
        (kill-region start end))))

cxxxr avatar Sep 05 '23 12:09 cxxxr

Thanks

@Sasanidas your implementation causes the cursor to move up after kiling the line. Also the killed line doesn't include the newline character.

@cxxxr I think your implementation has the same issue as mine where it wouldn't kill the last line in the buffer. I didn't know about execute :around. That's pretty neat.

I wrote another implementation but need to fine tune it because it doesn't work with the first line in the buffer.

timmydo avatar Sep 06 '23 02:09 timmydo

Is it just me or do things stop working after a couple of kill-regions? After I killed a region with a mark, I don't see a mark anymore but when I wipe it kills an unexpected region of text. I was also using undo so it's possible it got confused there.

timmydo avatar Sep 06 '23 04:09 timmydo

Ping @timmydo , mostly because I'm not sure if the issue got resolve :sweat_smile:

Sasanidas avatar Dec 07 '23 14:12 Sasanidas