使用vim作为编辑器,但使用emacs作为代码格式化工具。

4
我看到在erlang社区中格式化代码的常用方式是使用emacs erlang mode的方法。
有没有办法从vim中调用emacs来格式化代码(单行,选定的文本和整个文件)?
同时,我也希望有人能指点一下我一些描述这种缩进逻辑的emacs文档。
编辑:实际上我知道如何从vim中调用某些东西,但我不知道从emacs的哪一方面开始调用。

没有办法让Emacs在Vim中格式化代码。但是有Erlang的Vim插件。 - Rein Henrichs
@ReinHenrichs 我使用vimerl,但它的格式与emacs不同,我的同事们对此不太满意。 - danechkin
3
考虑在Emacs中使用vim键绑定。我听说evil-mode非常非常好。 - event_jr
@event_jr 不要误会,但我不想改变我的环境只是为了正确缩进。也许如果我找到了emacs如何实现它的方法,我可以将缩进逻辑移植到vim中? - danechkin
3
如果您添加了Vim的示例并解释了如何获得它以及Emacs的示例并解释了如何获得它,那么您的问题会更有趣。还要澄清您所谓的“格式化”。 - romainl
显示剩余2条评论
3个回答

6
IntelliJ Erlang插件可以实现这一点:ErlangEmacsFormatAction.java
/*
 * Copyright 2013 Sergey Ignatov
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 */

final GeneralCommandLine commandLine = new GeneralCommandLine();
commandLine.setExePath("emacs");
commandLine.addParameters("--batch", "--eval");

String s = "\n" +
    "(progn (find-file \"{0}\")\n" +
    "    (require ''erlang-start)\n" +
    "    (erlang-mode)\n" +
    "    (untabify (point-min) (point-max))\n" +
    "    (delete-trailing-whitespace)\n" +
    "    (erlang-indent-current-buffer)\n" +
    "    (write-region (point-min) (point-max) \"{1}\")\n" +
    "    (kill-emacs))";

  commandLine.addParameter(MessageFormat.format(s, virtualFile.getCanonicalPath(), tmpFile.getCanonicalPath()));

它调用一个带有文件作为参数的emacs批处理子进程。在vimscript中实现类似功能应该很容易,可以查看Vim外部命令


我尝试了你建议的方法,初看似乎是我需要的,但现在我发现有以下问题:
  1. 这个 eval 要求整个文件,而不是选定的区域(emacs 返回:'(End of file during parsing)')
  2. 当我传递整个文件时,emacs 会将其变成一行,这就是全部问题。
- danechkin
似乎第二个问题已经解决了,我错误地从标准输入读取文件(\n被切掉了),但第一个问题仍然存在。 - danechkin
只需创建一个临时文件,将所选区域的内容输出到该文件中,在该文件上使用emacs批处理模式,然后将临时文件的内容返回以替换您选择的区域。我无法帮助您编写vim代码,但我在bash脚本中测试了整个过程,它完美地工作。 - ratelle
我使用这个脚本./ee.sh file.erl,结果输出在gist的评论中。看起来一切都很顺利,但文件保持不变(即具有相同的格式)。 - danechkin
谢谢伙计!终于对我起作用了。我把最终脚本放在这里(https://gist.github.com/ddosia/5299338#comment-810375)。 - danechkin

3
我所知道的最接近解决这个问题的方法是vim插件vimerlhttps://github.com/jimenezrick/vimerl 我使用它,虽然不完美,但工作得相当不错。
如果它能与Emacs缩进完全一致就太好了,但考虑到运行它所需的工作量,这已经是一个很好的开始了。

可能OP只需要编辑这个插件,甚至添加配置选项来控制其行为,就像cindent使用的那样。(参见:help cinoptions-values - dash-tom-bang
目前我也使用vimerl,但它的格式感与emacs不同,这对我的同事来说是一个问题。 - danechkin

0

你可能会对了解vim-erlang的功能感兴趣。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接