0

Following the advice given here, I wrote the following code to enclose the text selection in phpbb italics tags :

(defun wrap-in-phpbb-italics-tag (b e)
  "does what its name says."
  (interactive "r")
  (save-excursion
      (goto-char b)
      (insert (format "[i]"))
      (goto-char e)
      (insert (format "[/i]"))))

This code almost does what I want : the left tag is positioned correctly but the right tag is three characters too early. Thus, if the selected goes Adjust end index in selected region, the result will be [i]Adjust end index in selected reg[/i]ion.

I tried to fix it as follows, replacing the (e) with (e+3) :

(defun wrap-in-phpbb-italics-tag (b e)
  "does what its name says."
  (interactive "r")
  (save-excursion
      (goto-char b)
      (insert (format "[i]"))
      (goto-char (e+3))
      (insert (format "[/i]"))))

But with this new code, the right tag simply disappears. What is going on here ? How can I fix this ?

1

Your code contains an error: (e+3), it should be (+ e 3). When you run your command, Emacs will report the following error message in Echo Area and the *Messages* buffer

goto-char: Symbol's function definition is void: e+3

Alternatively, you can insert at the end then at the beginning, so you don't need to +3 since b isn't moved at all.

(defun wrap-in-phpbb-italics-tag (b e)
  "does what its name says."
  (interactive "r")
  (save-excursion
    (goto-char e)
    (insert "[/i]")
    (goto-char b)
    (insert "[i]")))
  • exactly the answer I needed, containing both the solution and an explanation on every detail. Thank you very much – Ewan Delanoy Nov 16 '18 at 11:23

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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