How to tag text in emacs

梦想的初衷 提交于 2019-12-20 02:33:00

问题


I'm developing a text annotation system in emacs, where the format of the annotation is something like this. If this is the text:

Lorem ipsem por favor

I need to annotate it like this:

{latin}Lorem imsem{/latin} {spanish}por favor{/spanish}

So what I want to do is select a region and then run a function or a macro that will prompt for the tag name, and insert the curly braces, the closing / and the tag name into the buffer at the start and end of the region.

This is probably pretty straightforward, but I've always found emacs lisp to be rather confusing to get started with, because I don't use it very often at all.


回答1:


Well, there are a bunch of ways. yasnippet is one, and I'm sure a bunch of the various xml/html modes have one.

For simplicity, you can use some elisp like:

(defun my-tag-region (b e tag)
  "'tag' a region"
  (interactive "r\nMTag for region: ")
  (save-excursion
    (goto-char e)
    (insert (format "{/%s}" tag))
    (goto-char b)
    (insert (format "{%s}" tag))))



回答2:


If you want point to be after the closing } when you're done, I think this is more robust than searching:

(defun my-tag-region (b e tag)
  "'tag' a region"
  (interactive "r\nMTag for region: ")
  (let ((e (copy-marker e)))
    (goto-char b)
    (insert (format "{%s}" tag))
    (goto-char e)
    (insert (format "{/%s}" tag))))


来源:https://stackoverflow.com/questions/1397113/how-to-tag-text-in-emacs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!