0

can you please help me to write a small emacs function? This is the first lisp function I am writing and I think I am getting confused with point and mark placement.

I want to write a function that yanks text and that than replace parts of it with a regular expression. Example: I have in my killring

TEXT_TEXT
TEXT_TEXT

and I want in my buffer

TEXT_TEXT:10=TEXT_TEXT
TEXT_TEXT:10=TEXT_TEXT

These are the functions I have been writing. query-replace-in-yank does what I want, exept that I have to press ! every time I use it. replace-in-yank is what I cant get to work.

(defun replace-in-yank (numstr)
  (interactive "sNumber: ")
  (let ((repstr (concat "\\1:" numstr "=\\1")))
    (let ((startp (point)))
      (yank nil)
       (let ((endp (point)))
          (goto-char startp)
          (while (re-search-forward "\\([A-Z\\_]+\\)" endp t)replace-match repstr t nil))))
   (newline-and-indent)))


(defun query-replace-in-yank (numstr)
 (interactive "sNumber: ")
 (let ((repstr (concat "\\1:" numstr "=\\1")))
  (yank nil)
  (exchange-point-and-mark nil)
  (query-replace-regexp "\\([A-Z\\_]+\\)" repstr  nil (mark) (point) nil nil))
 (newline-and-indent))
5

Some of your parens were in the wrong place. I think you were expecting endp to be updated when you do the replace-match, but it has a fixed value, so I changed it to use copy-marker instead, which will get updated.

(defun replace-in-yank (numstr)
  (interactive "sNumber: ")
  (save-excursion
    (let ((repstr (concat "\\1:" numstr "=\\1"))
          (startp (point)))
      (yank)
      (let ((endp (copy-marker (point))))
        (goto-char startp)
        (while (re-search-forward "\\([A-Z\\_]+\\)" endp t)
          (replace-match repstr t nil)))
      (newline-and-indent))))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.