;; -*- emacs-lisp -*-
;;; mst-stamp.el --- add a 'stamp' to the buffer.

;; Description: This code is for adding a timestamped comment to a point in the
;; buffer. If a region is selected, a set of comments will wrap the region

;; Author: Mark Triggs <mst@dishevelled.net>
;; Keywords: tools
;; $Id: mst-stamp.el,v 1.39 2005/01/04 23:16:27 mst Exp $

;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.

;;; Code:

(require 'time-stamp)

(defun stamp-region-active-p ()
  "Say whether the region is active."
  (and (boundp 'transient-mark-mode)
       transient-mark-mode
       (boundp 'mark-active)
       mark-active))

(defun stamp-wrap-string (s &optional fill)
  (with-temp-buffer
    (insert s)
    (let ((fill-column (or fill fill-column)))
      (fill-region (point-min) (point-max)))
    (buffer-string)))

(defun mst-stamp ()
  "Insert a timestamp containing the current date, time, username etc.
  If a region is current active, insert a block"
  (interactive)
  (time-stamp)
  (let ((stamp-start (format "Note by %s%s at %s on %s\n"
                             (user-real-login-name)
                             (if (> (length (user-full-name)) 0)
                                 (concat " (" (user-full-name) ")")
                               "")
                             (time-stamp-hh:mm:ss)
                             (time-stamp-dd/mm/yyyy)))
        (stamp-end (format "End of note by %s%s\n"
                           (user-real-login-name)
                           (if (> (length (user-full-name)) 0)
                               (concat " (" (user-full-name) ")")
                             "")))
        (comments (read-string "Comments: ")))
    (let ((conservative-fill-column
           (- fill-column
              (length comment-start)
              2                         ; for spaces
              (length comment-end)
              (progn (if (stamp-region-active-p)
                         (save-excursion
                           (back-to-indentation)
                           (current-column))
                       (prog1 (progn (open-line 1)
                                     (indent-according-to-mode)
                                     (current-column))
                         (beginning-of-line)
                         (kill-line)))))))
      (cond ((stamp-region-active-p)
             (narrow-to-region (region-beginning) (region-end))
             (unwind-protect
                 (let ((region-text (buffer-string)))
                   (goto-char (point-min))
                   (insert (stamp-wrap-string stamp-start
                                              conservative-fill-column))
                   (insert (stamp-wrap-string (concat comments "\n")
                                              conservative-fill-column))
                   (comment-region (point-min) (point))
                   (unwind-protect
                       (delete-region (point) (point-max))
                     (insert region-text))
                   (let ((p (point)))
                     (unless (looking-at "^$")
                       (end-of-line)
                       (newline))
                     (insert (stamp-wrap-string stamp-end
                                                conservative-fill-column))
                     (comment-region p (point))))
               (widen))
             (let ((indent-region-function nil))
               (indent-region (region-beginning) (region-end))))
            (t
             (let ((p (point)))
               (narrow-to-region (point) (point))
               (unwind-protect
                   (progn (insert (stamp-wrap-string stamp-start
                                                     conservative-fill-column))
                          (insert (stamp-wrap-string (concat comments "\n")
                                                     conservative-fill-column))
                          (comment-region (point-min) (point-max)))
                 (widen))
               (let ((indent-region-function nil))
                 (indent-region p (point)))))))))

(provide 'mst-stamp)

;;; mst-stamp.el ends here
