* Flexible Killing
@ 2007-01-10 19:00 haws
2007-01-11 1:15 ` Rupert
0 siblings, 1 reply; 3+ messages in thread
From: haws @ 2007-01-10 19:00 UTC (permalink / raw)
Hello!
I'm a emacs beginner and would like to write a function that performs
this:
If there is a region selected, then it would kill-region, otherwise it
would kill-line.
Can anyone tell me how? I bet it's 2 lines of code!
Thank you very much,
Hugo
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Flexible Killing
2007-01-10 19:00 Flexible Killing haws
@ 2007-01-11 1:15 ` Rupert
2007-01-11 5:36 ` Stefan Monnier
0 siblings, 1 reply; 3+ messages in thread
From: Rupert @ 2007-01-11 1:15 UTC (permalink / raw)
haws wrote:
> Hello!
>
> I'm a emacs beginner and would like to write a function that performs
> this:
> If there is a region selected, then it would kill-region, otherwise it
> would kill-line.
> Can anyone tell me how? I bet it's 2 lines of code!
>
> Thank you very much,
>
> Hugo
Well, I'm a bit of a beginner too, but came up with this:
(defun flexikill ()
(interactive)
(let ((m nil))
(if (not (setq m (mark 1)))
(kill-line)
(let ((p (point)))
(if (= p m)
(kill-line)
(kill-region (region-beginning)
(region-end)))))))
The problem is that we can't use (interactive 'r') as if there is no
region, an (uncatchable) error message is printed, rather than us being
able to do something. Thus we need to check that a mark exists in the
buffer and that it's not equal to point (otherwise kill-region wouldn't
make much sense).
Rupert
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Flexible Killing
2007-01-11 1:15 ` Rupert
@ 2007-01-11 5:36 ` Stefan Monnier
0 siblings, 0 replies; 3+ messages in thread
From: Stefan Monnier @ 2007-01-11 5:36 UTC (permalink / raw)
> Well, I'm a bit of a beginner too, but came up with this:
> (defun flexikill ()
> (interactive)
> (let ((m nil))
> (if (not (setq m (mark 1)))
> (kill-line)
> (let ((p (point)))
> (if (= p m)
> (kill-line)
> (kill-region (region-beginning)
> (region-end)))))))
Whenever you use `setq', make sure you really need it
=>
(defun flexikill ()
(interactive)
(let ((m (mark 1)))
(if (not m)
(kill-line)
(let ((p (point)))
(if (= p m)
(kill-line)
(kill-region (region-beginning)
(region-end)))))))
no need to let-bind if you only use the value once:
=>
(defun flexikill ()
(interactive)
(let ((m (mark 1)))
(if (not m)
(kill-line)
(if (= (point) m)
(kill-line)
(kill-region (region-beginning)
(region-end))))))
merge tests to avoid duplicated identical calls
=>
(defun flexikill ()
(interactive)
(let ((m (mark 1)))
(if (or (not m) (= (point) m))
(kill-line)
(kill-region (region-beginning)
(region-end)))))
rather than 1, use a less-arbitrary value
=>
(defun flexikill ()
(interactive)
(let ((m (mark 'force)))
(if (or (not m) (= (point) m))
(kill-line)
(kill-region (region-beginning)
(region-end)))))
and if you ever use mark-even-if-inactive, then you will prefer
(defun flexikill ()
(interactive)
(if (or (not mark-active) (eq (point) (mark 'force)))
(kill-line)
(kill-region (region-beginning)
(region-end))))
-- Stefan
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2007-01-11 5:36 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-01-10 19:00 Flexible Killing haws
2007-01-11 1:15 ` Rupert
2007-01-11 5:36 ` Stefan Monnier
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.