向 C/C++ 添加一些注释自动或手动头文件
我一直在头文件中添加以下预处理器代码。
#ifdef _HELLO_H_ #define _HELLO_H_ #endif
有没有办法自动(我的意思是,当我第一次加载头文件时,emacs 只是添加代码)或手动(我的意思是,我有一些 Mx 东西)?
如果不存在,我该如何编写 elisp 代码呢?
- 检查#ifdef 是否未定义。
- 从头文件的名称中创造出名称 _HELLO_H_ 。
I add the following preprocessor code in header files all the time.
#ifdef _HELLO_H_ #define _HELLO_H_ #endif
Is there a way to do this automatically (I mean, when I load the header file for the first time, the emacs just adds the code), or manually (I mean, I have some M-x SOMETHING)?
If none exists, how can I program the elisp code to this?
- Check if #ifdef is not defined.
- coin the name _HELLO_H_ out of the name of the header file.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Mx auto-insert
完全按照您的要求进行操作,并且它作为标准内置到 emacs 中
只需打开头文件并运行命令
M-x auto-insert
Does exactly what you ask and its built into emacs as standard
Just open the header file and run the command
我使用 YaSnippet ,效果非常好。它默认带有许多针对不同语言和模式的代码片段,而不仅仅是 C++。另外,您可以编写自己的模板(片段),甚至可以在其中使用 Lisp(即生成带有包括当前年份在内的版权信息的文件头)。还有一个很好的文档。
这是一个“once”代码片段的示例,当您在 cc 模式下键入“once”并点击“tab”按钮时,该代码片段将被展开:
这是我的 c++ 模式的“许可证”代码片段,其中添加了版权信息当前年份:
I use YaSnippet and it works just great. It comes default with a lot of snippets for different languages and modes, not only for C++. Plus, you can write your own templates (snippets) and even use Lisp inside them (i.e. generate file header with copyright information including current year). There is also a good documentation.
Here is a an example of "once" snippet which is being expanded when you type "once" and hit "tab" button in cc-mode:
And here is my "license" snippet for c++-mode which adds a copyright information with a current year:
我会看一下骨架模式。
I would take a look at Skeleton Mode.
如果您使用的是 Emacs 23.2,或者安装了 CEDET,则可以使用 SRecode。如果启用
global-srecode-minor-mode
,您可以使用Cc //
插入预定义模板之一。默认情况下,在空的.h
文件中,它将提供空模板并插入文本,如上面所示。由于 SRecode 具有分层模板,因此您可以通过立即从菜单中选择“编辑模板”并将其复制到中的模板文件(如
目录。使用与上面相同的空模板插入技术来启动新的模板文件。mytemplates.srt
)中来轻松覆盖它的功能。 ~/.srecode如果您想要插入复杂的代码模式,SRecode 非常有用,因为它具有丰富的语言用于组合和重用模板,这使得使用语义标签生成代码或构建代码生成应用程序变得容易。
如果您喜欢模板插入作为编码模式,例如快速插入 if{} 块等,我必须推荐 yasnippet,因为它具有更好的 UI。
If you are using Emacs 23.2, or have CEDET otherwise installed, you can use SRecode. If
global-srecode-minor-mode
is turned on, you can useC-c / /
to insert one of the predefined templates. By default, in an empty.h
file, it will offer the empty template and insert text much as you show above. Since SRecode has hierarchical templates, you can easily override what it does by selecting "Edit Template" immediately afterward from the menu, and copying it into a template file (likemytemplates.srt
) in your~/.srecode
directory. Use the same empty template insertion technique as above to start the new template file.SRecode is nice if you have complex code patterns you'd like to insert since it has a rich language for combining and reusing templates which makes it easy to generate code using tags from Semantic or build code generating applications.
If you like template insertion as a coding pattern, such as quickly inserting if{} blocks and such, I'd have to recommend yasnippet as having a much nicer UI.
我使用模板,然后全局替换文本:
Emacs 和 Xemacs 有智能文本替换,因此“Stencil”将替换为“Square”而不是“SQUARE”。
I use a stencil, then global replace text:
Emacs and Xemacs have a smart text replacement, so that 'Stencil' would be replaced with 'Square' instead of 'SQUARE'.
大多数现代编译器支持
#pragma Once
,但 YMMV。在你的编译器中尝试一下(我猜既然你使用 emacs,它就是 gcc,它已经支持它很长一段时间了,至少从版本 2.95 开始,可能更早。)我想 Visual Studio 肯定也支持它是 MS 发起了这次大会。
Most modern compilers support
#pragma once
, but YMMV. Try it in your compiler (I'm guessing since you use emacs that it's gcc, which has supported it for quite some time now, at least as of version 2.95, probably earlier.)Visual Studio definitely supports it too, I'd imagine it was MS that started the convention.
YASnippet 很酷,我也使用它,但它不适合这项工作。
解决这个问题的方法就是使用模板框架。 emacs 中有几个。我不记得在设置 defaultcontent.el 之前评估过的所有模板系统。它基本上是执行 托马斯·马修斯的方法。
我尝试过 SkeletonMode,但觉得它太不模板化了,无法作为模板包。使用defaultcontent.el,我在模板文件中为每种文件类型定义模板。不需要 elisp。
还有 YASnippet 吗?它用于嵌入代码片段——在模块中出现 1-n 次的东西。示例:for 循环、方法定义、类定义、case 语句。相比之下,文件模板每次都保存进入每个文件的内容。代码片段是我选择添加或不添加到文件中的内容,具体取决于编码要求。
YASnippet is cool, and I use it, but it's the wrong tool for this job.
The solution to this question is to use a template framework. There are several within emacs. I don't remember all the template systems I evaluated before I set upon defaultcontent.el. It's basically an automated way of performing Thomas Matthews' approach.
I tried SkeletonMode, but felt it was too un-templatey to be a template package. With defaultcontent.el, I define the template for each file type, in a template file. There's no elisp necessary.
And YASnippet? It's for embedding snippets of code - stuff that appears 1-n times in a module. Examples: for loops, method definitions, class definitions, case statements. In contrast, the file template holds stuff that goes into EVERY FILE, every time. The snippet is something I choose to add to a file or not, depending on the coding requirement.