sudeepdino008
sudeepdino008

Reputation: 3364

Line duplication code for emacs

I am trying to write my function for line duplcation. I have to maintain the cursor position. This is the code in the .emacs file :

(defun line-duplicate
  "line duplication."
  (setq position (point))       ;Store the original position of cursor               
  (global-set-key "\C-d" "\C-a\C-k\C-k\C-y\C-y")
  (goto-char position)            ;move the cursor to original position
)

(line-duplicate)

However, some error is arising. What is the mistake?

EDIT

Just discovered the command-execute function:

(defun line-duplicate ()
  (interactive)
  (setq position (point))
  (command-execute (kbd "C-a C-k C-k C-y C-y"))
  (goto-char position)
)

(global-set-key (kbd "C-d") 'line-duplicate)

And this worked.

Upvotes: 2

Views: 108

Answers (2)

Oleg Pavliv
Oleg Pavliv

Reputation: 21182

I guess you want to bind line duplicate to C-d.

You can write the following function:

(defun my:dublicate-line ()
  (interactive)
  (save-excursion
  (let ((begin (line-beginning-position)) (end (line-end-position)))
    (move-beginning-of-line 2)
    (insert (concat  (buffer-substring-no-properties begin end) "\n")))))

(global-set-key (kbd "C-d") 'my:dublicate-line)

Upvotes: 1

choroba
choroba

Reputation: 242113

global-set-key takes two arguments, KEY and COMMAND. COMMAND is not a key sequence, it is a function (usually it is a symbol naming an interactively-callable function, says the documentation). You probably do not want to change the behaviour of C-d when the function is called, you want to bind the function to the key.

Upvotes: 1

Related Questions