;; -*- Mode: Emacs-Lisp -*-
;; -*- lisp -*-
;; $Id: .emacs-d-autoinsert,v 1.3 2002/07/01 12:49:56 amos-cvs Exp $
;; Copyright (C) Jan Borsodi 1998-2001
;;
;; Default auto-insert functions

; Auto-insert text when making new *.cpp, *.cc, *.h files.
(add-hook 'find-file-hooks 'auto-insert)


; If you create a file called Test.php, this function will replace:
;
;   @@@ with TEST
;   ||| with Test
;   !!! with test
;

(defun auto-update-php-file ()
  (let (classname)
    (setq classname (project-capitalize-classname (file-name-sans-extension
						   (file-name-nondirectory buffer-file-name))))
    (setq classname (read-from-minibuffer "Enter name of PHP class (Enter for default): "
					  classname))
    (project-replace-class-name (current-buffer) classname "" '() (file-name-nondirectory buffer-file-name))))

; If you create a file called Test.hpp, this function will replace:
;
;   @@@ with TEST
;   ||| with Test
;   !!! with test
;
; The first one is useful for #ifdefs, the second one for the header
; description, for example.

(defun auto-update-header-file ()
  (if project-use-auto-insert
      (let ()
	(save-excursion
	  (while (search-forward project-upcase-name-match nil t)
	    (save-restriction
	      (narrow-to-region (match-beginning 0) (match-end 0))
	      (replace-match
	       (upcase
		(file-name-sans-extension
		 (file-name-nondirectory buffer-file-name)))))))
	(save-excursion
	  (while (search-forward project-normal-name-match nil t)
	    (save-restriction
	      (narrow-to-region (match-beginning 0) (match-end 0))
	      (replace-match
	       (file-name-sans-extension
		(file-name-nondirectory buffer-file-name))))))
	(save-excursion
	  (while (search-forward project-downcase-name-match nil t)
	    (save-restriction
	      (narrow-to-region (match-beginning 0) (match-end 0))
	      (replace-match
	       (downcase
		(file-name-sans-extension
		 (file-name-nondirectory buffer-file-name)))))))
	(save-excursion
	  (while (search-forward "\<real-name\>" nil t)
	    (save-restriction
	      (narrow-to-region (match-beginning 0) (match-end 0))
	      (replace-match user-full-name))))
	(save-excursion
	  (while (search-forward "\<login-name\>" nil t)
	    (save-restriction
	      (narrow-to-region (match-beginning 0) (match-end 0))
	      (replace-match user-login-name))))
	(save-excursion
	  (while (search-forward "\<mail-name\>" nil t)
	    (save-restriction
	      (narrow-to-region (match-beginning 0) (match-end 0))
	      (replace-match project-mail-account)))))))

; If you create a file called Test.pro, this function will replace:
;
;   @@@ with test
;   ||| with Test
;   !!! with test
;
; It will also create a directory for objects if a OBJECTS_DIR is present

(defun auto-update-project-file ()
  (if project-use-auto-insert
      (let ()
	(save-excursion
	  (while (search-forward project-upcase-name-match nil t)
	    (save-restriction
	      (narrow-to-region (match-beginning 0) (match-end 0))
	      (replace-match
	       (upcase
		(file-name-sans-extension
		 (file-name-nondirectory buffer-file-name)))))))
	(save-excursion
	  (while (search-forward project-normal-name-match nil t)
	  (save-restriction
	    (narrow-to-region (match-beginning 0) (match-end 0))
	    (replace-match
	     (file-name-sans-extension
	      (file-name-nondirectory buffer-file-name))))))
	(save-excursion
	  (while (search-forward project-downcase-name-match nil t)
	  (save-restriction
	    (narrow-to-region (match-beginning 0) (match-end 0))
	    (replace-match
	     (downcase
	      (file-name-sans-extension
	       (file-name-nondirectory buffer-file-name)))))))
	(save-excursion
	  (while (search-forward "\<real-name\>" nil t)
	  (save-restriction
	    (narrow-to-region (match-beginning 0) (match-end 0))
	    (replace-match user-full-name))))
	(save-excursion
	  (while (search-forward "\<login-name\>" nil t)
	  (save-restriction
	    (narrow-to-region (match-beginning 0) (match-end 0))
	    (replace-match user-login-name))))
	(save-excursion
	  (while (search-forward "\<mail-name\>" nil t)
	  (save-restriction
	    (narrow-to-region (match-beginning 0) (match-end 0))
	    (replace-match project-mail-account))))
	(save-excursion
	  (while (search-forward "OBJECTS_DIR" nil t)
	  (save-excursion
	    (search-forward "=" nil t)
	    (save-restriction
	      (if (search-forward-regexp "[ \t]*\\([a-zA-Z]+\\)[ \t]*$" nil t)
		  (if (not (file-exists-p (match-string 1 )))
		      (make-directory (match-string 1))))))))
	(save-excursion
	  (while (search-forward "MOC_DIR" nil t)
	  (save-excursion
	    (search-forward "=" nil t)
	    (save-restriction
	      (if (search-forward-regexp "[ \t]*\\([a-zA-Z]+\\)[ \t]*$" nil t)
		  (if (not (file-exists-p (match-string 1 )))
		      (make-directory (match-string 1))))))))
	(save-buffer)
	(shell-command (project-creation-line  (file-relative-name (buffer-file-name buffer-file-name) (pwd)))))))

;; Replaces the creation-tag with the current date/time and user
(defun insert-creation-date ()
  "Inserts the date of the creation if it finds the keyword."
  (interactive)
  (save-excursion
    (beginning-of-buffer)
    (if (search-forward "\<creation-tag\>" nil t)
	(save-restriction
	  (narrow-to-region (match-beginning 0) (match-end 0))
	  (replace-match (concat "Created on: <"
				 (time-stamp-string)
				 ">")))))
  nil)

(add-hook 'write-file-hooks 'insert-creation-date)

