I’m trying to write a function that will:
- If there is no region selected, then kill the current line.
- If there a region selected, then kill the rectangle between point and mark.
By (2) I mean the same thing that happens if you run M-x kill-rectangle.
Here is my attempt at the function:
(defun cut-line-or-rectangle ()
"Cut rectangle if selection exists, cut line otherwise"
(interactive)
(if mark-active
(kill-rectangle (point) (mark))
(kill-whole-line)
)
)
This fullfils (1) but does nothing if a region is active. How can I make emacs obey the kill-rectangle function in this circumstance?
kill-rectangletakesstartandendin that order, so your code only works when point < mark.The typical way to do this sort of thing is to give your function
startandendarguments, and then use(interactive "r").