简体   繁体   English

在LaTeX模式emacs中缩进C代码区域

[英]Indent a region of C code in LaTeX mode emacs

My problem is that I am writing a LaTeX document in emacs that has a lot of C code in it. 我的问题是我在emacs中写了一个LaTeX文档,里面有很多C代码。 I am using both the \\minted and the \\verbatim environments in various places. 我在各个地方使用\\ minted和\\ verbatim环境。 When I compile the LaTeX (using pdflatex), the resulting pdf looks fine. 当我编译LaTeX(使用pdflatex)时,生成的pdf看起来很好。 In the raw LaTeX code, I would like to be able to auto-indent using the rules of the C-major mode. 在原始的LaTeX代码中,我希望能够使用C-major模式的规则进行自动缩进。

For example, I want to be able to mark the following region 例如,我希望能够标记以下区域

\begin{verbatim} 

void main(void)
{
printf("Hello World \n\r");
}

\end{verbatim}

And have emacs auto-format it to look like 并让emacs自动格式化它看起来像

\begin{verbatim}

void main(void)
{
    printf("Hello World \n\r");
}

\end{verbatim}   

In other words, I want to be able to run indent-region on the part that is actually C code using the rules from C mode, even though I am in LaTeX mode. 换句话说,我希望能够使用C模式中的规则在实际C代码的部分上运行indent-region,即使我处于LaTeX模式。

Does anyone know if this is possible? 有谁知道这是否可能?

Mx indent-region indents only the region, not the full buffer, so: Mx indent-region仅缩进区域,而不是整个缓冲区,因此:

  1. Turn on C mode 打开C模式
  2. Indent your region 缩进你的地区
  3. Turn back on LaTeX mode 转回LaTeX模式

You can use Cx 4 c to clone your current buffer to an indirect buffer. 您可以使用Cx 4 c将当前缓冲区克隆到间接缓冲区。 Put that indirect buffer in to c-mode and do your indenting there. 将间接缓冲区放入c模式并在那里进行缩进。 For further information on indirect buffers see the Emacs info manual, node 'Indirect Buffers' . 有关间接缓冲区的更多信息,请参阅Emacs信息手册,节点“间接缓冲区”

Here's a quick fix. 这是一个快速解决方案。 With a bit of work you could make this general - ie, check for the current major-mode, and switch back to that mode after you're done. 通过一些工作,你可以做到这一般 - 即检查当前的主模式,并在完成后切换回该模式。 As is, it switches to c-mode, indents, then switches to LaTeX-mode (AucTex), which solves the immediate problem: 它是切换到c模式,缩进,然后切换到LaTeX模式(AucTex),它解决了当前的问题:

(defun indent-region-as-c (beg end)
  "Switch to c-mode, indent the region, then switch back to LaTeX mode."
  (interactive "r")
  (save-restriction
    (narrow-to-region beg end)
    (c-mode)
    (indent-region (point-min) (point-max)))
    (LaTeX-mode))

Bind that to your favourite key and you should be all set. 将它绑定到您最喜欢的密​​钥,您应该全部设置。

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

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