0

I wrote this custom function to copy the entire line, if no mark is set

(defun sk-copy-ops (beg end)
  (interactive "r")
  (if mark-active
  (kill-ring-save beg end)
  (kill-ring-save (line-beginning-position) (line-end-position))))

I am seeing two issues,

1) It copies from the current point and not from the beginning of the line

2) And when i keep my point on the last but one line in the above code and do M-x sk-copy-ops, it copies from the Point to the end of the buffer.

Are line-beginning/end-position not the right indicators of the begin/end of lines in buffer?

Madhavan
  • 1,957
  • 12
  • 28
  • How about?: `(point-at-bol)` and `(point-at-eol)`. I've been spending a little too much time looking at `elisp`, because I keep wanting to change the indentation a bit for the `if/then/else` -- :) – lawlist Aug 13 '15 at 03:34
  • 1
    @lawlist: `point-at-bol` and `point-at-eol` are aliases for `line-beginning-position` and `line-end-position`. – Drew Aug 13 '15 at 04:30
  • looks like, it is working... hope, i din't waste too much of your time... thanks folks.... – Madhavan Aug 13 '15 at 04:59

2 Answers2

1

One way is to leverage whole-line-or-region.

This minor mode allows functions to operate on the current line if they would normally operate on a region and region is currently undefined.

The primary use for this is to kill (cut) the current line if no region is defined, and kill-region is invoked. It basically saves you the effort of going to the begining of the line, selecting the text up to the end of the line, and killing. Similarly, when yanking, it's smart enough to know that the string to be yanked was killed as a whole line, and it should be yanked as one, too. So you don't need to position yourself at the start of the line before yanking. If region is defined, though, all functions act as normal.

Then, you can do:

(defun whole-line-or-region-kill-region (prefix)
  "Kill region or PREFIX whole lines."
  (interactive "*p")
  (whole-line-or-region-call-with-region 'kill-region prefix t))

whole-line-or-region can be a bit tricky to integrate with some other big Emacs modes. (e.g. cua and evil)

PythonNut
  • 10,243
  • 2
  • 29
  • 75
1

I don't see either problem with your code -- it works for me. Try again, starting with emacs -Q, and be sure you are calling your command.

However, a minor improvement would be to use (use-region-p) instead of just mark-active.

Drew
  • 75,699
  • 9
  • 109
  • 225