an emacs macro

By Hal Canary, 2012-09-25 12:00:00 (link)

(defun guard ()
  "http://en.wikipedia.org/wiki/Include_guard"
  (interactive)
  (if (buffer-file-name)
      (let ((macro-name 
	     (concat 
	      (upcase
	       (file-name-sans-extension
		(file-name-nondirectory
		 (buffer-file-name))))
	      "_H")))
	(save-excursion
	  (goto-char (point-min))
	  (insert 
	   (concat 
	    "#ifndef " 
	    macro-name
	    "\n#define "
	    macro-name
	    "\n"))
	  (goto-char (point-max))
	  (insert 
	   (concat
	    "\n#endif /* " 
	    macro-name
	    " */\n"))))))

(back)