tags:

views:

98

answers:

2

I want to copy the line above the cursor, but just from the current column to the end of that line.

Here's an illustration:

photo of before action

photo of result of action

This was my attempt, but it doesn't work so well :-(

(defun dupchar()
  (save-excursion
    (line-move (-1) nil nil nil)
    (setq mychar (thing-at-point 'char))
    (insert mychar))
+1  A: 

Try something like this.

(defun dupchar ()
  (interactive)
  (let ((char-above (save-excursion
                      (line-move -1)
                      (following-char))))
    (unless (eq char-above ?\n)
      (insert char-above))))

(define-key global-map [(meta \")] 'dupchar)

A few comments on the function you wrote:

  1. You need to use (interactive) otherwise you can't bind the function to a key.

  2. It's not a good idea to just randomly setq things—that creates a global variable. In this case you don't need a variable at all; you can make use of the return value from save-excursion. (In the later version of this I needed to use a let.)

  3. Parentheses call a function in (e)lisp, so you need to use -1 instead of (-1).

  4. The 2nd-4th arguments to 'line-move will default to nil, so there's no need to specify them.

(Note: I modified this to stop at the end of the line; it's again hard to understand what you wrote, but this is my best guess.)

Nicholas Riley
thanks Nicholas I got you solution more beautiful code in lisp :-), still have a problem if current cursor at the end of line (maybe newline) will not success for dupchar
leedit
What do you want it to do at the end of the line?
Nicholas Riley
for example I've the line I want duplicate this line char by char so I press RET open new open and press (meta \") the result is not correct
leedit
I'm sorry, I still don't understand. What result are you getting? What result do you expect?
Nicholas Riley
a picture is attachedhttp://farm3.static.flickr.com/2682/4189641600_560be88a71_o.pnghttp://farm3.static.flickr.com/2674/4189645716_998944a657_o.png
leedit
All I can say is that it works for me. Maybe there's some issue typing M-" on your machine? You can try M-x dupchar and see if it works instead. For an example of it in action on my machine, see http://vimeo.com/8211826.
Nicholas Riley
Thanks again, key binding M-" is working which work well if inside middle of text, but not working if cursor is end of position in current line
leedit
OK, well pick another binding then :-)
Nicholas Riley
A: 

Hi,

not sure if you intended to copy the whole end of the previous line with one keystroke. At least I thought this would be useful so I modified Nicholas (thanks!) code to dupline:

(defun dupline ()
  (interactive)
  (let ((line-above-tail (save-excursion
                           (line-move -1)
                           (buffer-substring-no-properties (point) (line-end-position)))))
    (unless (eq line-above-tail ?\n)
      (insert line-above-tail))))

Copying to the end of the line was inspired by How do I duplicate a whole line in Emacs?

danielpoe
dupline I'm simply using hippie-expand with first charfor dupchar I want to cut half line similar with previous fill few piece and same again :-)
leedit