Merge from emacs-23; up to 2010-05-26T14:19:15Z!monnier@iro.umontreal.ca.
[bpt/emacs.git] / lisp / erc / erc-stamp.el
index d67dffe..cfe8616 100644 (file)
@@ -1,6 +1,6 @@
 ;;; erc-stamp.el --- Timestamping for ERC messages
 
-;; Copyright (C) 2002, 2003, 2004, 2006, 2007 Free Software Foundation, Inc.
+;; Copyright (C) 2002-2004, 2006-2011  Free Software Foundation, Inc.
 
 ;; Author: Mario Lang <mlang@delysid.org>
 ;; Keywords: comm, processes, timestamp
@@ -8,10 +8,10 @@
 
 ;; This file is part of GNU Emacs.
 
-;; GNU Emacs is free software; you can redistribute it and/or modify
+;; GNU Emacs 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 3, or (at your option)
-;; any later version.
+;; the Free Software Foundation, either version 3 of the License, or
+;; (at your option) any later version.
 
 ;; GNU Emacs is distributed in the hope that it will be useful,
 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
@@ -19,9 +19,7 @@
 ;; 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., 51 Franklin Street, Fifth Floor,
-;; Boston, MA 02110-1301, USA.
+;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.
 
 ;;; Commentary:
 
@@ -58,16 +56,48 @@ If nil, timestamping is turned off."
   :type '(choice (const nil)
                 (string)))
 
-(defcustom erc-insert-timestamp-function 'erc-insert-timestamp-right
+(defcustom erc-timestamp-format-left "\n[%a %b %e %Y]\n"
+  "*If set to a string, messages will be timestamped.
+This string is processed using `format-time-string'.
+Good examples are \"%T\" and \"%H:%M\".
+
+This timestamp is used for timestamps on the left side of the
+screen when `erc-insert-timestamp-function' is set to
+`erc-insert-timestamp-left-and-right'.
+
+If nil, timestamping is turned off."
+  :group 'erc-stamp
+  :type '(choice (const nil)
+                (string)))
+
+(defcustom erc-timestamp-format-right " [%H:%M]"
+  "*If set to a string, messages will be timestamped.
+This string is processed using `format-time-string'.
+Good examples are \"%T\" and \"%H:%M\".
+
+This timestamp is used for timestamps on the right side of the
+screen when `erc-insert-timestamp-function' is set to
+`erc-insert-timestamp-left-and-right'.
+
+If nil, timestamping is turned off."
+  :group 'erc-stamp
+  :type '(choice (const nil)
+                (string)))
+
+(defcustom erc-insert-timestamp-function 'erc-insert-timestamp-left-and-right
   "*Function to use to insert timestamps.
 
 It takes a single argument STRING which is the final string
 which all text-properties already appended.  This function only cares about
 inserting this string at the right position.  Narrowing is in effect
 while it is called, so (point-min) and (point-max) determine the region to
-operate on."
+operate on.
+
+You will probably want to set
+`erc-insert-away-timestamp-function' to the same value."
   :group 'erc-stamp
-  :type '(choice (const :tag "Right" erc-insert-timestamp-right)
+  :type '(choice (const :tag "Both sides" erc-insert-timestamp-left-and-right)
+                (const :tag "Right" erc-insert-timestamp-right)
                 (const :tag "Left" erc-insert-timestamp-left)
                 function))
 
@@ -82,12 +112,14 @@ If `erc-timestamp-format' is set, this will not be used."
   :type '(choice (const nil)
                 (string)))
 
-(defcustom erc-insert-away-timestamp-function 'erc-insert-timestamp-right
+(defcustom erc-insert-away-timestamp-function
+  'erc-insert-timestamp-left-and-right
   "*Function to use to insert the away timestamp.
 
 See `erc-insert-timestamp-function' for details."
   :group 'erc-stamp
-  :type '(choice (const :tag "Right" erc-insert-timestamp-right)
+  :type '(choice (const :tag "Both sides" erc-insert-timestamp-left-and-right)
+                (const :tag "Right" erc-insert-timestamp-right)
                 (const :tag "Left" erc-insert-timestamp-left)
                 function))
 
@@ -160,6 +192,18 @@ or `erc-send-modify-hook'."
   "Last timestamp inserted into the buffer.")
 (make-variable-buffer-local 'erc-timestamp-last-inserted)
 
+(defvar erc-timestamp-last-inserted-left nil
+  "Last timestamp inserted into the left side of the buffer.
+This is used when `erc-insert-timestamp-function' is set to
+`erc-timestamp-left-and-right'")
+(make-variable-buffer-local 'erc-timestamp-last-inserted-left)
+
+(defvar erc-timestamp-last-inserted-right nil
+  "Last timestamp inserted into the right side of the buffer.
+This is used when `erc-insert-timestamp-function' is set to
+`erc-timestamp-left-and-right'")
+(make-variable-buffer-local 'erc-timestamp-last-inserted-right)
+
 (defcustom erc-timestamp-only-if-changed-flag t
   "*Insert timestamp only if its value changed since last insertion.
 If `erc-insert-timestamp-function' is `erc-insert-timestamp-left', a
@@ -218,6 +262,9 @@ property to get to the POSth column."
                       (list 'space ':align-to pos)))
   (insert string))
 
+;; Silence byte-compiler
+(defvar erc-fill-column)
+
 (defun erc-insert-timestamp-right (string)
   "Insert timestamp on the right side of the screen.
 STRING is the timestamp to insert.  The function is a possible value
@@ -272,6 +319,26 @@ be printed just before the window-width."
       (when erc-timestamp-intangible
        (erc-put-text-property from (1+ (point)) 'intangible t)))))
 
+(defun erc-insert-timestamp-left-and-right (string)
+  "This is another function that can be assigned to
+`erc-insert-timestamp-function'.  If the date is changed, it will
+print a blank line, the date, and another blank line.  If the time is
+changed, it will then print it off to the right."
+  (let* ((ct (current-time))
+        (ts-left (erc-format-timestamp ct erc-timestamp-format-left))
+        (ts-right (erc-format-timestamp ct erc-timestamp-format-right)))
+    ;; insert left timestamp
+    (unless (string-equal ts-left erc-timestamp-last-inserted-left)
+      (goto-char (point-min))
+      (erc-put-text-property 0 (length ts-left) 'field 'erc-timestamp ts-left)
+      (insert ts-left)
+      (setq erc-timestamp-last-inserted-left ts-left))
+    ;; insert right timestamp
+    (let ((erc-timestamp-only-if-changed-flag t)
+         (erc-timestamp-last-inserted erc-timestamp-last-inserted-right))
+      (erc-insert-timestamp-right ts-right)
+      (setq erc-timestamp-last-inserted-right ts-right))))
+
 ;; for testing: (setq erc-timestamp-only-if-changed-flag nil)
 
 (defun erc-format-timestamp (time format)
@@ -342,8 +409,8 @@ NOW is position of point currently."
   (when erc-echo-timestamps
     (let ((stamp (get-text-property now 'timestamp)))
       (when stamp
-       (message (format-time-string erc-echo-timestamp-format
-                                    stamp))))))
+       (message "%s" (format-time-string erc-echo-timestamp-format
+                                         stamp))))))
 
 (provide 'erc-stamp)
 
@@ -354,4 +421,3 @@ NOW is position of point currently."
 ;; tab-width: 8
 ;; End:
 
-;; arch-tag: 57aefab4-63e0-4c48-91d5-6efa145487e0