在 TextMate的,一个人可以使用 CTRL移瓦特 将文本包装在Open / Close标记中,然后使用ctrl-shift-cmd -w将每行包装在Open / Close标记的区域中。如何使用emacs lisp在Emacs中实现相同的功能?
emacs的 变 <P> emacs的</ P>
而......
emacs的 TextMate的 六 变 <LI> emacs的</ LI> <LI> TextMate的</ LI> <LI>六</ LI>
在 TextMate的,一个人可以使用 CTRL移瓦特 将文本包装在Open / Close标记中,然后使用ctrl-shift-cmd -w将每行包装在Open / Close标记的区域中。如何使用emacs lisp在Emacs中实现相同的功能?
emacs的 变 <P> emacs的</ P>
而......
emacs的 TextMate的 六 变 <LI> emacs的</ LI> <LI> TextMate的</ LI> <LI>六</ LI>
这个答案 为您提供包裹区域的解决方案(一旦您修改它以使用尖括号)。
此例程将提示您使用该标记,并应使用该类型的打开/关闭标记标记该区域中的每一行:
(defun my-tag-lines (b e tag)
"'tag' every line in the region with a tag"
(interactive "r\nMTag for line: ")
(save-restriction
(narrow-to-region b e)
(save-excursion
(goto-char (point-min))
(while (< (point) (point-max))
(beginning-of-line)
(insert (format "<%s>" tag))
(end-of-line)
(insert (format "</%s>" tag))
(forward-line 1)))))
*注意:*如果你想要的话 tag
永远是 li
,然后删除标记参数,删除文本 \nMTag for line:
从调用到交互,并更新插入调用只是插入 "<li\>"
正如你所料。
对于 sgml-mode
deratives,mark region to tagify,type M-x sgml-tag
,然后输入您要使用的标签名称(按 TAB
获取可用HTML元素的列表)。尽管如此,此方法不允许您标记区域中的每一行,您可以通过录制键盘宏来解决此问题。
yasnippet 是一个特别好的Textmate的Emacs片段语法的实现。有了它,您可以导入所有Textmate的片段。如果你安装它,我写的这个片段应该做你想要的:
(defun wrap-region-or-point-with-html-tag (start end)
"Wraps the selected text or the point with a tag"
(interactive "r")
(let (string)
(if mark-active
(list (setq string (buffer-substring start end))
(delete-region start end)))
(yas/expand-snippet (point)
(point)
(concat "<${1:p}>" string "$0</${1:$(replace-regexp-in-string \" .*\" \"\" text)}>"))))
(global-set-key (kbd "C-W") 'wrap-region-or-point-with-html-tag)
编辑:(好的,这是我最后一次尝试解决这个问题。它与Textmate的版本完全相同。它甚至会在结束标记中的空格后忽略字符)
对不起,我误解了你的问题。此功能应编辑区域中的每一行。
(defun wrap-lines-in-region-with-html-tag (start end)
"Wraps the selected text or the point with a tag"
(interactive "r")
(let (string)
(if mark-active
(list (setq string (buffer-substring start end))
(delete-region start end)))
(yas/expand-snippet
(replace-regexp-in-string "\\(<$1>\\).*\\'" "<${1:p}>"
(mapconcat
(lambda (line) (format "%s" line))
(mapcar
(lambda (match) (concat "<$1>" match "</${1:$(replace-regexp-in-string \" .*\" \"\" text)}>"))
(split-string string "[\r\n]")) "\n") t nil 1) (point) (point))))
Trey的答案中的这个变体也将正确地缩进html。
(defun wrap-lines-region-html (b e tag)
"'tag' every line in the region with a tag"
(interactive "r\nMTag for line: ")
(setq p (point-marker))
(save-excursion
(goto-char b)
(while (< (point) p)
(beginning-of-line)
(indent-according-to-mode)
(insert (format "<%s>" tag))
(end-of-line)
(insert (format "</%s>" tag))
(forward-line 1))))