繁体   English   中英

在不同的主要模式下打开选择。

[英]Open selection in different major mode.

是否可以选择文本的某些部分并在具有不同模式的不同缓冲区中打开它?

例如,如果我经常在ESS模式下工作(R语法突出显示),

astring <- '<form>
<input type="checkbox" id="foo", value="bar">
</form>'

如果选中单引号中的文本,我想在新的缓冲区HTML模式下编辑它(类似于orgmode中的 org-src-lang-modes )。

您所描述的内容称为“间接缓冲区”。 您可以通过调用Mx clone-indirect-buffer创建一个。 这将创建您正在编辑的缓冲区的第二个副本。 在一个缓冲区中进行的任何更改都在另一个缓冲区中进行镜像。 但是,两个缓冲区可以有不同的主要模式处于活动状态,因此一个可以处于ESS模式,另一个可以处于HTML(或任何您喜欢的)。

有关详细信息,请参阅手册

这是使用narrow-to-region处理问题的一种方法 - 该示例考虑在键入Mx narrow-to-single-quotes单引号时点(光标)将介于单引号之间。 一个简单的两行函数可用于退出 - (widen) (ess-mode) ; 或者,您可以通过recursive-edit获得乐趣。 当然,这与在新缓冲区中打开文本不同。 类似的功能也可用于将区域复制到新缓冲区,但我假设原始海报可能希望将编辑后的文本合并回主缓冲区。

(defun narrow-to-single-quotes ()
"When cursor (aka point) is between single quotes, this function will narrow
the region to whatever is between the single quotes, and then change the
major mode to `html-mode`.  To exit, just type `M-x widen` and then
`M-x [whatever-previous-major-mode-was-used]`."
(interactive)
  (let* (
      (init-pos (point))
      beg
      end)
    (re-search-backward "'" nil t)
    (forward-char 1)
    (setq beg (point))
    (re-search-forward "'" nil t)
    (backward-char 1)
    (setq end (point))
    (narrow-to-region beg end)
    (html-mode)
    (goto-char init-pos)))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM