Skip to content

Instantly share code, notes, and snippets.

@ahoka
Created May 17, 2014 09:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ahoka/a5197774db3b1289c8e6 to your computer and use it in GitHub Desktop.
Save ahoka/a5197774db3b1289c8e6 to your computer and use it in GitHub Desktop.
elisp function for generating header guards for C/C++ headers.
(defun c-include-guard ()
"Insert C header include guards."
(interactive)
(let (original-point macro-name)
(set 'original-point (point))
(set 'macro-name
(upcase
(replace-regexp-in-string
"\\."
"_"
(file-name-nondirectory
buffer-file-name))))
(goto-char (point-min))
(insert
(concat "#ifndef " macro-name "\n#define " macro-name "\n\n"))
(set 'original-point (- (+ (point) original-point) 1))
(goto-char (point-max))
(insert "\n#endif\n")
(goto-char original-point)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment