English 中文(简体)
How can I copy the end of the line starting one line above the cursor?
原标题:
  • 时间:2009-12-16 02:55:09
  •  标签:
  • elisp

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))
最佳回答

Try something like this.

(defun dupchar ()
  (interactive)
  (let ((char-above (save-excursion
                      (line-move -1)
                      (following-char))))
    (unless (eq char-above ?
)
      (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.)

问题回答

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 ?
)
      (insert line-above-tail))))

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





相关问题
How to determine operating system in elisp?

How do I programmatically determine which OS Emacs is running under in ELisp? I would like to run different code in .emacs depending on the OS.

Emacs, Zen-Coding mode, and Putty

I use emacs via Putty and since Putty doesn t send certain key combinations to the remote console I generally need to re-bind them to other key combinations. After installing the amazing Zen-Coding ...

Emacs23 php-mode problem

I keep getting this error and similar errors while using php-mode on Emacs23 on Ubuntu: Debugger entered--Lisp error: (void-function php-template-if-hook) php-template-if-hook() abbrev-insert(if #...

Emacs Setting which-function-mode

I would like to have which-function-mode on by default when I open up Emacs. I ve added the following lines to my .emacs file. (setq which-func-mode t) (setq which-function-mode t) When I open ...

eval during emacs lisp macro expansion

How can I fix the simple macro foo in (elisp)Eval During Expansion? None of the followings work: (defmacro foo1 (a) `(setq (eval ,a) t)) (defmacro foo2 (a) `(setq ,(eval a) t)) (defmacro foo3 (...

Get local cvs comment history when committing file/s in emacs

I often commit files with similar cvs comment but not in a single operation. I would like to able to bring up previous comments I ve used in a previous commit when I am in the process of writing a ...

热门标签