Emacs org-mode: select last word, insert link onto it

Viewed 82

I want to write an emacs lisp function that selects the last word and links it to the last item in the kill ring. I've written the following:

(defun link-last-word-from-kill ()
    (interactive)
    (push-mark)
    (backward-word)
    (org-insert-link 0 (yank) (buffer-substring (region-beginning) (region-end)))
    (pop-mark)
    )

I'm uncertain I'm reading (org-insert-link &optional COMPLETE-FILE LINK-LOCATION DESCRIPTION) correctly; I think I've set the link-location to be the kill-ring, but I'm still prompted for input to link. What am I misunderstanding?

1 Answers

I think yank uses the buffer as an output. Also you need to replace the previous word with the link. I opted just to create the org-mode link with the markup, for example: [[http://foo.invalid][PreviousWordHere]]

(defun link-last-word-from-kill ()
    (interactive)
    (push-mark)
    (backward-word)
    (insert "[[")
    (yank)
    (insert "][")
    (forward-word)
    (insert "]]")
    (pop-mark))
Related