views:

167

answers:

6

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

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.

Ken Simon
+1 for pragma once
Viktor Sehr
#pragma once sometimes just doesn't work. For example, when header is copied as a part of some installation script (make install?) and due to some mistake in include path (like once it is included as "mylib/header.h" and another time is goes just like "header.h"). Plus, don't forget about some buggy compilers who doesn't handle hardlinks correctly. Plus, this is not a part of a standard.
Vlad Lazarenko
+4  A: 

I would take a look at Skeleton mode. Skeleton Mode

krousey
+2  A: 

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

Thomas Matthews
This is what defaultcontent.el does, automatically.
Cheeso
+3  A: 

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$
//
Vlad Lazarenko
+1 for YASnippet
slomojo
YASnippet is a great tool, but it's the wrong thing for this purpose, which is *templates*.
Cheeso
@Cheeso: This is arguable. What is the difference between snippet template and a template described in the function?
Vlad Lazarenko
+1  A: 

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.

Eric
A: 

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.

Cheeso