Markus
Markus

Reputation: 261

How can I change a word in a buffer using elisp?

How can I change the word at point using elisp? Something quite like (upcase-word) but using my own function?

Background: I've written a function that detects the base of the number at point and can convert it to any other base. What I would like to do is to change the number directly in the buffer.

TIA Markus

Upvotes: 2

Views: 64

Answers (1)

abo-abo
abo-abo

Reputation: 20342

Try this code. I've included a sample interactive function using upcase:

(defun change-word-at-point (fun)
  (cl-destructuring-bind (beg . end)
      (bounds-of-thing-at-point 'word)
    (let ((str (buffer-substring-no-properties beg end)))
      (delete-region beg end)
      (insert (funcall fun str)))))

(defun upcase-word ()
  (interactive)
  (change-word-at-point 'upcase))

Upvotes: 2

Related Questions