开发者

Adding some comments to the C/C++ header file automatically or manually

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开发者_StackOverflow社区 elisp code to this?

  • Check if #ifdef is not defined.
  • coin the name _HELLO_H_ out of the name of the header file.


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:

#name : #ifndef XXX; #define XXX; #endif
# --
#ifndef ${1:_`(upcase (file-name-nondirectory (file-name-sans-extension (buffer-file-name))))`_H_}
#define $1

$0

#endif

And here is my "license" snippet for c++-mode which adds a copyright information with a current year:

#name : C++ source file license
# --
//
// Copyright (C) `(format-time-string "%Y" (current-time))` Bueller? Bueller?
//
// $Id$
//


I would take a look at Skeleton Mode.


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 use C-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 (like mytemplates.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.


I use a stencil, then global replace text:

#ifndef STENCIL_HPP
#define STENCIL_HPP

class Stencil
{
};

#endif // STENCIL_HPP

Emacs and Xemacs have a smart text replacement, so that 'Stencil' would be replaced with 'Square' instead of 'SQUARE'.


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 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.


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜