;; ;; Adds command M-X make-guard ;; this function inserts the standard C header ;; guard in an .h file around any existing text ;; for example if the file ;; is foo-bar_baz.h ;; --------------------------- ;; #ifndef __FOO_BAR_BAZ_H__ ;; #define __FOO_BAR_BAZ_H__ ;; ;; ;; #ifdef __cplusplus ;; extern "C" { ;; #endif ;; ;; existing code ;; ;; ;; #ifdef __cplusplus ;; } /* extern "C" */ ;; #endif ;; #endif /* __FOO_BAR_BAZ_H__*/ ;; ;; ---------------------------------- (defun make-guard () (interactive) (let ((buffer-name (buffer-name (current-buffer)))) (if (eq (compare-strings buffer-name (- (length buffer-name) 2) nil ".h" 0 nil t) t) (insert-header-guard buffer-name) (message "File must be a header file (.h)")))) (defun insert-header-guard (buffer-name) (let* ((split-name (split-string buffer-name "[-_.]")) (guard-def (concat "__" (mapconcat (lambda (x) (upcase x)) split-name "_") "__"))) (save-excursion (progn (goto-char 0) (insert "#ifndef ") (insert guard-def) (insert "\n") (insert "#define ") (insert guard-def) (insert "\n\n\n") (insert "#ifdef __cplusplus extern \"C\" { #endif\n\n") (goto-char (point-max)) (insert "\n\n#ifdef __cplusplus } /* extern \"C\" */ #endif\n\n") (insert "\n\n#endif /* ") (insert guard-def) (insert "*/\n")))))