erc-stamp.el 15.4 KB
Newer Older
1
;;; erc-stamp.el --- Timestamping for ERC messages
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 2002-2004, 2006-2019 Free Software Foundation, Inc.
4 5 6 7 8 9 10

;; Author: Mario Lang <mlang@delysid.org>
;; Keywords: comm, processes, timestamp
;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcStamp

;; This file is part of GNU Emacs.

11
;; GNU Emacs is free software: you can redistribute it and/or modify
12
;; it under the terms of the GNU General Public License as published by
13 14
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
15 16 17 18 19 20 21

;; GNU Emacs 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
22
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45

;;; Commentary:

;; The code contained in this module is responsible for inserting
;; timestamps into ERC buffers.  In order to actually activate this,
;; you must call `erc-timestamp-mode'.

;; You can choose between two different ways of inserting timestamps.
;; Customize `erc-insert-timestamp-function' and
;; `erc-insert-away-timestamp-function'.

;;; Code:

(require 'erc)
(require 'erc-compat)

(defgroup erc-stamp nil
  "For long conversation on IRC it is sometimes quite
useful to have individual messages timestamp.  This
group provides settings related to the format and display
of timestamp information in `erc-mode' buffer.

For timestamping to be activated, you just need to load `erc-stamp'
46
in your init file or interactively using `load-library'."
47 48 49
  :group 'erc)

(defcustom erc-timestamp-format "[%H:%M]"
50
  "If set to a string, messages will be timestamped.
51 52 53 54 55 56 57 58
This string is processed using `format-time-string'.
Good examples are \"%T\" and \"%H:%M\".

If nil, timestamping is turned off."
  :group 'erc-stamp
  :type '(choice (const nil)
		 (string)))

59
(defcustom erc-timestamp-format-left "\n[%a %b %e %Y]\n"
60
  "If set to a string, messages will be timestamped.
61 62 63 64 65 66 67 68 69 70 71 72 73
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]"
74
  "If set to a string, messages will be timestamped.
75 76 77 78 79 80 81 82 83 84 85 86 87
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
88
  "Function to use to insert timestamps.
89 90 91 92 93

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
94 95 96 97
operate on.

You will probably want to set
`erc-insert-away-timestamp-function' to the same value."
98
  :group 'erc-stamp
99 100
  :type '(choice (const :tag "Both sides" erc-insert-timestamp-left-and-right)
		 (const :tag "Right" erc-insert-timestamp-right)
101 102 103 104
		 (const :tag "Left" erc-insert-timestamp-left)
		 function))

(defcustom erc-away-timestamp-format "<%H:%M>"
105
  "Timestamp format used when marked as being away.
106 107 108 109 110 111 112 113 114

If nil, timestamping is turned off when away unless `erc-timestamp-format'
is set.

If `erc-timestamp-format' is set, this will not be used."
  :group 'erc-stamp
  :type '(choice (const nil)
		 (string)))

115
(defcustom erc-insert-away-timestamp-function
116
  #'erc-insert-timestamp-left-and-right
117
  "Function to use to insert the away timestamp.
118 119 120

See `erc-insert-timestamp-function' for details."
  :group 'erc-stamp
121 122
  :type '(choice (const :tag "Both sides" erc-insert-timestamp-left-and-right)
		 (const :tag "Right" erc-insert-timestamp-right)
123 124 125 126
		 (const :tag "Left" erc-insert-timestamp-left)
		 function))

(defcustom erc-hide-timestamps nil
127
  "If non-nil, timestamps will be invisible.
128 129 130 131 132 133 134

This is useful for logging, because, although timestamps will be
hidden, they will still be present in the logs."
  :group 'erc-stamp
  :type 'boolean)

(defcustom erc-echo-timestamps nil
135
  "If non-nil, print timestamp in the minibuffer when point is moved.
136 137 138 139 140 141 142
Using this variable, you can turn off normal timestamping,
and simply move point to an irc message to see its timestamp
printed in the minibuffer."
  :group 'erc-stamp
  :type 'boolean)

(defcustom erc-echo-timestamp-format "Timestamped %A, %H:%M:%S"
143
  "Format string to be used when `erc-echo-timestamps' is non-nil.
144 145 146 147 148
This string specifies the format of the timestamp being echoed in
the minibuffer."
  :group 'erc-stamp
  :type 'string)

l3thal's avatar
l3thal committed
149
(defcustom erc-timestamp-intangible nil
150
  "Whether the timestamps should be intangible, i.e. prevent the point
151 152
from entering them and instead jump over them."
  :group 'erc-stamp
Kelvin White's avatar
Kelvin White committed
153
  :version "24.5"
154 155
  :type 'boolean)

156
(defface erc-timestamp-face '((t :weight bold :foreground "green"))
157 158 159
  "ERC timestamp face."
  :group 'erc-faces)

160
;;;###autoload(autoload 'erc-timestamp-mode "erc-stamp" nil t)
161 162
(define-erc-module stamp timestamp
  "This mode timestamps messages in the channel buffers."
163 164 165 166 167 168
  ((add-hook 'erc-mode-hook #'erc-munge-invisibility-spec)
   (add-hook 'erc-insert-modify-hook #'erc-add-timestamp t)
   (add-hook 'erc-send-modify-hook #'erc-add-timestamp t))
  ((remove-hook 'erc-mode-hook #'erc-munge-invisibility-spec)
   (remove-hook 'erc-insert-modify-hook #'erc-add-timestamp)
   (remove-hook 'erc-send-modify-hook #'erc-add-timestamp)))
169 170 171 172 173 174 175 176 177 178 179 180 181 182

(defun erc-add-timestamp ()
  "Add timestamp and text-properties to message.

This function is meant to be called from `erc-insert-modify-hook'
or `erc-send-modify-hook'."
  (unless (get-text-property (point) 'invisible)
    (let ((ct (current-time)))
      (if (fboundp erc-insert-timestamp-function)
	  (funcall erc-insert-timestamp-function
		   (erc-format-timestamp ct erc-timestamp-format))
	(error "Timestamp function unbound"))
      (when (and (fboundp erc-insert-away-timestamp-function)
		 erc-away-timestamp-format
Miles Bader's avatar
Miles Bader committed
183
		 (erc-away-time)
184 185 186 187 188 189
		 (not erc-timestamp-format))
	(funcall erc-insert-away-timestamp-function
		 (erc-format-timestamp ct erc-away-timestamp-format)))
      (add-text-properties (point-min) (point-max)
			   (list 'timestamp ct))
      (add-text-properties (point-min) (point-max)
190 191
			   (list 'cursor-sensor-functions
				 (list #'erc-echo-timestamp))))))
192 193 194 195 196

(defvar erc-timestamp-last-inserted nil
  "Last timestamp inserted into the buffer.")
(make-variable-buffer-local 'erc-timestamp-last-inserted)

197 198 199 200 201 202 203 204 205 206 207 208
(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)

209
(defcustom erc-timestamp-only-if-changed-flag t
210
  "Insert timestamp only if its value changed since last insertion.
211 212 213 214 215 216 217 218 219
If `erc-insert-timestamp-function' is `erc-insert-timestamp-left', a
string of spaces which is the same size as the timestamp is added to
the beginning of the line in its place. If you use
`erc-insert-timestamp-right', nothing gets inserted in place of the
timestamp."
  :group 'erc-stamp
  :type 'boolean)

(defcustom erc-timestamp-right-column nil
220
  "If non-nil, the column at which the timestamp is inserted,
221 222 223 224 225 226 227 228
if the timestamp is to be printed to the right.  If nil,
`erc-insert-timestamp-right' will use other means to determine
the correct column."
  :group 'erc-stamp
  :type '(choice
	  (integer :tag "Column number")
	  (const :tag "Unspecified" nil)))

229 230 231
(defcustom erc-timestamp-use-align-to (and (not (featurep 'xemacs))
					   (>= emacs-major-version 22)
					   (eq window-system 'x))
232
  "If non-nil, use the :align-to display property to align the stamp.
233 234
This gives better results when variable-width characters (like
Asian language characters and math symbols) precede a timestamp.
235
Unfortunately, it only works in Emacs 22 and when using the X
236 237 238 239
Window System.

A side effect of enabling this is that there will only be one
space before a right timestamp in any saved logs."
240 241 242
  :group 'erc-stamp
  :type 'boolean)

243 244 245 246 247 248 249 250 251
(defun erc-insert-timestamp-left (string)
  "Insert timestamps at the beginning of the line."
  (goto-char (point-min))
  (let* ((ignore-p (and erc-timestamp-only-if-changed-flag
			(string-equal string erc-timestamp-last-inserted)))
	 (len (length string))
	 (s (if ignore-p (make-string len ? ) string)))
    (unless ignore-p (setq erc-timestamp-last-inserted string))
    (erc-put-text-property 0 len 'field 'erc-timestamp s)
Miles Bader's avatar
Miles Bader committed
252
    (erc-put-text-property 0 len 'invisible 'timestamp s)
253 254
    (insert s)))

255
(defun erc-insert-aligned (string pos)
256
  "Insert STRING at the POSth column.
257

258 259 260
If `erc-timestamp-use-align-to' is t, use the :align-to display
property to get to the POSth column."
  (if (not erc-timestamp-use-align-to)
261
      (indent-to pos)
262
    (insert " ")
263 264
    (put-text-property (1- (point)) (point) 'display
		       (list 'space ':align-to pos)))
265
  (insert string))
266

267
;; Silence byte-compiler
268
(defvar erc-fill-column)
269

270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291
(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
for `erc-insert-timestamp-function'.

If `erc-timestamp-only-if-changed-flag' is nil, a timestamp is always
printed.  If this variable is non-nil, a timestamp is only printed if
it is different from the last.

If `erc-timestamp-right-column' is set, its value will be used as the
column at which the timestamp is to be printed.  If it is nil, and
`erc-fill-mode' is active, then the timestamp will be printed just
before `erc-fill-column'.  Otherwise, if the current buffer is
shown in a window, that window's width is used.  If the buffer is
not shown, and `fill-column' is set, then the timestamp will be
printed just `fill-column'.  As a last resort, the timestamp will
be printed just before the window-width."
  (unless (and erc-timestamp-only-if-changed-flag
	       (string-equal string erc-timestamp-last-inserted))
    (setq erc-timestamp-last-inserted string)
    (goto-char (point-max))
    (forward-char -1);; before the last newline
292
    (let* ((str-width (string-width string))
293
	   (pos (cond
294
		 (erc-timestamp-right-column erc-timestamp-right-column)
295 296
		 ((and (boundp 'erc-fill-mode)
		       erc-fill-mode
297 298
		       (boundp 'erc-fill-column)
		       erc-fill-column)
299
		  (1+ (- erc-fill-column str-width)))
300
		 (fill-column
301
		  (1+ (- fill-column str-width)))
302
		 (t
303
		  (- (window-width) str-width 1))))
304
	   (from (point))
305
	   (col (current-column)))
306 307 308 309 310
      ;; The following is a kludge used to calculate whether to move
      ;; to the next line before inserting a stamp.  It allows for
      ;; some margin of error if what is displayed on the line differs
      ;; from the number of characters on the line.
      (setq col (+ col (ceiling (/ (- col (- (point) (point-at-bol))) 1.6))))
311
      (if (< col pos)
312
	  (erc-insert-aligned string pos)
313 314
	(newline)
	(indent-to pos)
315
	(setq from (point))
316
	(insert string))
Miles Bader's avatar
Miles Bader committed
317 318
      (erc-put-text-property from (point) 'field 'erc-timestamp)
      (erc-put-text-property from (point) 'rear-nonsticky t)
319
      (when erc-timestamp-intangible
320
	(erc-put-text-property from (1+ (point)) 'cursor-intangible t)))))
321

322
(defun erc-insert-timestamp-left-and-right (_string)
323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341
  "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))))

342 343 344 345 346 347 348
;; for testing: (setq erc-timestamp-only-if-changed-flag nil)

(defun erc-format-timestamp (time format)
  "Return TIME formatted as string according to FORMAT.
Return the empty string if FORMAT is nil."
  (if format
      (let ((ts (format-time-string format time)))
349 350
	(erc-put-text-property 0 (length ts)
			       'font-lock-face 'erc-timestamp-face ts)
351 352 353 354 355
	(erc-put-text-property 0 (length ts) 'invisible 'timestamp ts)
	(erc-put-text-property 0 (length ts)
			       'isearch-open-invisible 'timestamp ts)
	;; N.B. Later use categories instead of this harmless, but
	;; inelegant, hack. -- BPT
356 357
	(and erc-timestamp-intangible
	     (not erc-hide-timestamps)	; bug#11706
358
	     (erc-put-text-property 0 (length ts) 'cursor-intangible t ts))
359 360 361 362 363 364 365 366 367
	ts)
    ""))

;; This function is used to munge `buffer-invisibility-spec to an
;; appropriate value. Currently, it only handles timestamps, thus its
;; location.  If you add other features which affect invisibility,
;; please modify this function and move it to a more appropriate
;; location.
(defun erc-munge-invisibility-spec ()
368 369 370 371
  (and erc-timestamp-intangible (not (bound-and-true-p cursor-intangible-mode))
       (cursor-intangible-mode 1))
  (and erc-echo-timestamps (not (bound-and-true-p cursor-sensor-mode))
       (cursor-sensor-mode 1))
372
  (if erc-hide-timestamps
373 374
      (add-to-invisibility-spec 'timestamp)
    (remove-from-invisibility-spec 'timestamp)))
375 376 377 378 379 380 381 382 383 384 385 386 387 388 389

(defun erc-hide-timestamps ()
  "Hide timestamp information from display."
  (interactive)
  (setq erc-hide-timestamps t)
  (erc-munge-invisibility-spec))

(defun erc-show-timestamps ()
  "Show timestamp information on display.
This function only works if `erc-timestamp-format' was previously
set, and timestamping is already active."
  (interactive)
  (setq erc-hide-timestamps nil)
  (erc-munge-invisibility-spec))

Miles Bader's avatar
Miles Bader committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404
(defun erc-toggle-timestamps ()
  "Hide or show timestamps in ERC buffers.

Note that timestamps can only be shown for a message using this
function if `erc-timestamp-format' was set and timestamping was
enabled when the message was inserted."
  (interactive)
  (if erc-hide-timestamps
      (setq erc-hide-timestamps nil)
    (setq erc-hide-timestamps t))
  (mapc (lambda (buffer)
	  (with-current-buffer buffer
	    (erc-munge-invisibility-spec)))
	(erc-buffer-list)))

405 406 407 408 409
(defun erc-echo-timestamp (window _before dir)
  "Print timestamp text-property of an IRC message."
  (when (and erc-echo-timestamps (eq 'entered dir))
    (let* ((now (window-point window))
	   (stamp (get-text-property now 'timestamp)))
410
      (when stamp
411 412
	(message "%s" (format-time-string erc-echo-timestamp-format
					  stamp))))))
413 414 415 416 417 418

(provide 'erc-stamp)

;;; erc-stamp.el ends here
;;
;; Local Variables:
419
;; generated-autoload-file: "erc-loaddefs.el"
420 421 422
;; indent-tabs-mode: t
;; tab-width: 8
;; End: