在emacs内实现html标签 和 html实体 的相互转换

输出html代码的时候希望将其转义后输出,像这样的

1
<h1>this is h1</h1>

但是在编辑的时候形态是这样的

1
<h1>this is h1</h1>

如果借用外部的资源是很好解决的,只需将 < 替换为 &lt; >
替换为 &gt;即可.

使用emacs的话,写两个elisp函数即可:


(defun region-entity-to-html (start end)
  "Replace entities to html in region …."
  (interactive "r")
  (save-restriction
        (narrow-to-region start end)

        (goto-char (point-min))
        (while (search-forward "<" nil t) (replace-match "<" nil="" t))="" (goto-char="" (point-min))="" (while="" (search-forward="" ">"="" t)="" (replace-match="" "="">" nil t))

        ;; more here
        )
  )


(defun region-html-to-entity (start end)
  "Replace html to entities in region …."
  (interactive "r")
  (save-restriction
        (narrow-to-region start end)

        (goto-char (point-min))
        (while (search-forward "<" nil="" t)="" (replace-match="" "<"="" t))="" (goto-char="" (point-min))="" (while="" (search-forward="" "="">" nil t) (replace-match ">" nil t))

        ;; more here
        )
  )

将这两个函数放到~/.emacs中,以后要想将一段html转为html,只需
选中这段文本,然后执行命令M-x region-html-to-entity 或者 region-entity-to-html