erc-stamp.el 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  1. ;;; erc-stamp.el --- Timestamping for ERC messages
  2. ;; Copyright (C) 2002-2004, 2006-2012 Free Software Foundation, Inc.
  3. ;; Author: Mario Lang <mlang@delysid.org>
  4. ;; Keywords: comm, processes, timestamp
  5. ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcStamp
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;; The code contained in this module is responsible for inserting
  19. ;; timestamps into ERC buffers. In order to actually activate this,
  20. ;; you must call `erc-timestamp-mode'.
  21. ;; You can choose between two different ways of inserting timestamps.
  22. ;; Customize `erc-insert-timestamp-function' and
  23. ;; `erc-insert-away-timestamp-function'.
  24. ;;; Code:
  25. (require 'erc)
  26. (require 'erc-compat)
  27. (defgroup erc-stamp nil
  28. "For long conversation on IRC it is sometimes quite
  29. useful to have individual messages timestamp. This
  30. group provides settings related to the format and display
  31. of timestamp information in `erc-mode' buffer.
  32. For timestamping to be activated, you just need to load `erc-stamp'
  33. in your .emacs file or interactively using `load-library'."
  34. :group 'erc)
  35. (defcustom erc-timestamp-format "[%H:%M]"
  36. "*If set to a string, messages will be timestamped.
  37. This string is processed using `format-time-string'.
  38. Good examples are \"%T\" and \"%H:%M\".
  39. If nil, timestamping is turned off."
  40. :group 'erc-stamp
  41. :type '(choice (const nil)
  42. (string)))
  43. (defcustom erc-timestamp-format-left "\n[%a %b %e %Y]\n"
  44. "*If set to a string, messages will be timestamped.
  45. This string is processed using `format-time-string'.
  46. Good examples are \"%T\" and \"%H:%M\".
  47. This timestamp is used for timestamps on the left side of the
  48. screen when `erc-insert-timestamp-function' is set to
  49. `erc-insert-timestamp-left-and-right'.
  50. If nil, timestamping is turned off."
  51. :group 'erc-stamp
  52. :type '(choice (const nil)
  53. (string)))
  54. (defcustom erc-timestamp-format-right " [%H:%M]"
  55. "*If set to a string, messages will be timestamped.
  56. This string is processed using `format-time-string'.
  57. Good examples are \"%T\" and \"%H:%M\".
  58. This timestamp is used for timestamps on the right side of the
  59. screen when `erc-insert-timestamp-function' is set to
  60. `erc-insert-timestamp-left-and-right'.
  61. If nil, timestamping is turned off."
  62. :group 'erc-stamp
  63. :type '(choice (const nil)
  64. (string)))
  65. (defcustom erc-insert-timestamp-function 'erc-insert-timestamp-left-and-right
  66. "*Function to use to insert timestamps.
  67. It takes a single argument STRING which is the final string
  68. which all text-properties already appended. This function only cares about
  69. inserting this string at the right position. Narrowing is in effect
  70. while it is called, so (point-min) and (point-max) determine the region to
  71. operate on.
  72. You will probably want to set
  73. `erc-insert-away-timestamp-function' to the same value."
  74. :group 'erc-stamp
  75. :type '(choice (const :tag "Both sides" erc-insert-timestamp-left-and-right)
  76. (const :tag "Right" erc-insert-timestamp-right)
  77. (const :tag "Left" erc-insert-timestamp-left)
  78. function))
  79. (defcustom erc-away-timestamp-format "<%H:%M>"
  80. "*Timestamp format used when marked as being away.
  81. If nil, timestamping is turned off when away unless `erc-timestamp-format'
  82. is set.
  83. If `erc-timestamp-format' is set, this will not be used."
  84. :group 'erc-stamp
  85. :type '(choice (const nil)
  86. (string)))
  87. (defcustom erc-insert-away-timestamp-function
  88. 'erc-insert-timestamp-left-and-right
  89. "*Function to use to insert the away timestamp.
  90. See `erc-insert-timestamp-function' for details."
  91. :group 'erc-stamp
  92. :type '(choice (const :tag "Both sides" erc-insert-timestamp-left-and-right)
  93. (const :tag "Right" erc-insert-timestamp-right)
  94. (const :tag "Left" erc-insert-timestamp-left)
  95. function))
  96. (defcustom erc-hide-timestamps nil
  97. "*If non-nil, timestamps will be invisible.
  98. This is useful for logging, because, although timestamps will be
  99. hidden, they will still be present in the logs."
  100. :group 'erc-stamp
  101. :type 'boolean)
  102. (defcustom erc-echo-timestamps nil
  103. "*If non-nil, print timestamp in the minibuffer when point is moved.
  104. Using this variable, you can turn off normal timestamping,
  105. and simply move point to an irc message to see its timestamp
  106. printed in the minibuffer."
  107. :group 'erc-stamp
  108. :type 'boolean)
  109. (defcustom erc-echo-timestamp-format "Timestamped %A, %H:%M:%S"
  110. "*Format string to be used when `erc-echo-timestamps' is non-nil.
  111. This string specifies the format of the timestamp being echoed in
  112. the minibuffer."
  113. :group 'erc-stamp
  114. :type 'string)
  115. (defcustom erc-timestamp-intangible t
  116. "*Whether the timestamps should be intangible, i.e. prevent the point
  117. from entering them and instead jump over them."
  118. :group 'erc-stamp
  119. :type 'boolean)
  120. (defface erc-timestamp-face '((t (:bold t :foreground "green")))
  121. "ERC timestamp face."
  122. :group 'erc-faces)
  123. ;;;###autoload (autoload 'erc-timestamp-mode "erc-stamp" nil t)
  124. (define-erc-module stamp timestamp
  125. "This mode timestamps messages in the channel buffers."
  126. ((add-hook 'erc-mode-hook 'erc-munge-invisibility-spec)
  127. (add-hook 'erc-insert-modify-hook 'erc-add-timestamp t)
  128. (add-hook 'erc-send-modify-hook 'erc-add-timestamp t))
  129. ((remove-hook 'erc-mode-hook 'erc-munge-invisibility-spec)
  130. (remove-hook 'erc-insert-modify-hook 'erc-add-timestamp)
  131. (remove-hook 'erc-send-modify-hook 'erc-add-timestamp)))
  132. (defun erc-add-timestamp ()
  133. "Add timestamp and text-properties to message.
  134. This function is meant to be called from `erc-insert-modify-hook'
  135. or `erc-send-modify-hook'."
  136. (unless (get-text-property (point) 'invisible)
  137. (let ((ct (current-time)))
  138. (if (fboundp erc-insert-timestamp-function)
  139. (funcall erc-insert-timestamp-function
  140. (erc-format-timestamp ct erc-timestamp-format))
  141. (error "Timestamp function unbound"))
  142. (when (and (fboundp erc-insert-away-timestamp-function)
  143. erc-away-timestamp-format
  144. (erc-away-time)
  145. (not erc-timestamp-format))
  146. (funcall erc-insert-away-timestamp-function
  147. (erc-format-timestamp ct erc-away-timestamp-format)))
  148. (add-text-properties (point-min) (point-max)
  149. (list 'timestamp ct))
  150. (add-text-properties (point-min) (point-max)
  151. (list 'point-entered 'erc-echo-timestamp)))))
  152. (defvar erc-timestamp-last-inserted nil
  153. "Last timestamp inserted into the buffer.")
  154. (make-variable-buffer-local 'erc-timestamp-last-inserted)
  155. (defvar erc-timestamp-last-inserted-left nil
  156. "Last timestamp inserted into the left side of the buffer.
  157. This is used when `erc-insert-timestamp-function' is set to
  158. `erc-timestamp-left-and-right'")
  159. (make-variable-buffer-local 'erc-timestamp-last-inserted-left)
  160. (defvar erc-timestamp-last-inserted-right nil
  161. "Last timestamp inserted into the right side of the buffer.
  162. This is used when `erc-insert-timestamp-function' is set to
  163. `erc-timestamp-left-and-right'")
  164. (make-variable-buffer-local 'erc-timestamp-last-inserted-right)
  165. (defcustom erc-timestamp-only-if-changed-flag t
  166. "*Insert timestamp only if its value changed since last insertion.
  167. If `erc-insert-timestamp-function' is `erc-insert-timestamp-left', a
  168. string of spaces which is the same size as the timestamp is added to
  169. the beginning of the line in its place. If you use
  170. `erc-insert-timestamp-right', nothing gets inserted in place of the
  171. timestamp."
  172. :group 'erc-stamp
  173. :type 'boolean)
  174. (defcustom erc-timestamp-right-column nil
  175. "*If non-nil, the column at which the timestamp is inserted,
  176. if the timestamp is to be printed to the right. If nil,
  177. `erc-insert-timestamp-right' will use other means to determine
  178. the correct column."
  179. :group 'erc-stamp
  180. :type '(choice
  181. (integer :tag "Column number")
  182. (const :tag "Unspecified" nil)))
  183. (defcustom erc-timestamp-use-align-to (and (not (featurep 'xemacs))
  184. (>= emacs-major-version 22)
  185. (eq window-system 'x))
  186. "*If non-nil, use the :align-to display property to align the stamp.
  187. This gives better results when variable-width characters (like
  188. Asian language characters and math symbols) precede a timestamp.
  189. Unfortunately, it only works in Emacs 22 and when using the X
  190. Window System.
  191. A side effect of enabling this is that there will only be one
  192. space before a right timestamp in any saved logs."
  193. :group 'erc-stamp
  194. :type 'boolean)
  195. (defun erc-insert-timestamp-left (string)
  196. "Insert timestamps at the beginning of the line."
  197. (goto-char (point-min))
  198. (let* ((ignore-p (and erc-timestamp-only-if-changed-flag
  199. (string-equal string erc-timestamp-last-inserted)))
  200. (len (length string))
  201. (s (if ignore-p (make-string len ? ) string)))
  202. (unless ignore-p (setq erc-timestamp-last-inserted string))
  203. (erc-put-text-property 0 len 'field 'erc-timestamp s)
  204. (erc-put-text-property 0 len 'invisible 'timestamp s)
  205. (insert s)))
  206. (defun erc-insert-aligned (string pos)
  207. "Insert STRING at the POSth column.
  208. If `erc-timestamp-use-align-to' is t, use the :align-to display
  209. property to get to the POSth column."
  210. (if (not erc-timestamp-use-align-to)
  211. (indent-to pos)
  212. (insert " ")
  213. (put-text-property (1- (point)) (point) 'display
  214. (list 'space ':align-to pos)))
  215. (insert string))
  216. ;; Silence byte-compiler
  217. (defvar erc-fill-column)
  218. (defun erc-insert-timestamp-right (string)
  219. "Insert timestamp on the right side of the screen.
  220. STRING is the timestamp to insert. The function is a possible value
  221. for `erc-insert-timestamp-function'.
  222. If `erc-timestamp-only-if-changed-flag' is nil, a timestamp is always
  223. printed. If this variable is non-nil, a timestamp is only printed if
  224. it is different from the last.
  225. If `erc-timestamp-right-column' is set, its value will be used as the
  226. column at which the timestamp is to be printed. If it is nil, and
  227. `erc-fill-mode' is active, then the timestamp will be printed just
  228. before `erc-fill-column'. Otherwise, if the current buffer is
  229. shown in a window, that window's width is used. If the buffer is
  230. not shown, and `fill-column' is set, then the timestamp will be
  231. printed just `fill-column'. As a last resort, the timestamp will
  232. be printed just before the window-width."
  233. (unless (and erc-timestamp-only-if-changed-flag
  234. (string-equal string erc-timestamp-last-inserted))
  235. (setq erc-timestamp-last-inserted string)
  236. (goto-char (point-max))
  237. (forward-char -1);; before the last newline
  238. (let* ((current-window (get-buffer-window (current-buffer)))
  239. (str-width (string-width string))
  240. (pos (cond
  241. (erc-timestamp-right-column erc-timestamp-right-column)
  242. ((and (boundp 'erc-fill-mode)
  243. erc-fill-mode
  244. (boundp 'erc-fill-column)
  245. erc-fill-column)
  246. (1+ (- erc-fill-column str-width)))
  247. (fill-column
  248. (1+ (- fill-column str-width)))
  249. (t
  250. (- (window-width) str-width 1))))
  251. (from (point))
  252. (col (current-column))
  253. indent)
  254. ;; The following is a kludge used to calculate whether to move
  255. ;; to the next line before inserting a stamp. It allows for
  256. ;; some margin of error if what is displayed on the line differs
  257. ;; from the number of characters on the line.
  258. (setq col (+ col (ceiling (/ (- col (- (point) (point-at-bol))) 1.6))))
  259. (if (< col pos)
  260. (erc-insert-aligned string pos)
  261. (newline)
  262. (indent-to pos)
  263. (setq from (point))
  264. (insert string))
  265. (erc-put-text-property from (point) 'field 'erc-timestamp)
  266. (erc-put-text-property from (point) 'rear-nonsticky t)
  267. (when erc-timestamp-intangible
  268. (erc-put-text-property from (1+ (point)) 'intangible t)))))
  269. (defun erc-insert-timestamp-left-and-right (string)
  270. "This is another function that can be assigned to
  271. `erc-insert-timestamp-function'. If the date is changed, it will
  272. print a blank line, the date, and another blank line. If the time is
  273. changed, it will then print it off to the right."
  274. (let* ((ct (current-time))
  275. (ts-left (erc-format-timestamp ct erc-timestamp-format-left))
  276. (ts-right (erc-format-timestamp ct erc-timestamp-format-right)))
  277. ;; insert left timestamp
  278. (unless (string-equal ts-left erc-timestamp-last-inserted-left)
  279. (goto-char (point-min))
  280. (erc-put-text-property 0 (length ts-left) 'field 'erc-timestamp ts-left)
  281. (insert ts-left)
  282. (setq erc-timestamp-last-inserted-left ts-left))
  283. ;; insert right timestamp
  284. (let ((erc-timestamp-only-if-changed-flag t)
  285. (erc-timestamp-last-inserted erc-timestamp-last-inserted-right))
  286. (erc-insert-timestamp-right ts-right)
  287. (setq erc-timestamp-last-inserted-right ts-right))))
  288. ;; for testing: (setq erc-timestamp-only-if-changed-flag nil)
  289. (defun erc-format-timestamp (time format)
  290. "Return TIME formatted as string according to FORMAT.
  291. Return the empty string if FORMAT is nil."
  292. (if format
  293. (let ((ts (format-time-string format time)))
  294. (erc-put-text-property 0 (length ts) 'face 'erc-timestamp-face ts)
  295. (erc-put-text-property 0 (length ts) 'invisible 'timestamp ts)
  296. (erc-put-text-property 0 (length ts)
  297. 'isearch-open-invisible 'timestamp ts)
  298. ;; N.B. Later use categories instead of this harmless, but
  299. ;; inelegant, hack. -- BPT
  300. (when erc-timestamp-intangible
  301. (erc-put-text-property 0 (length ts) 'intangible t ts))
  302. ts)
  303. ""))
  304. ;; This function is used to munge `buffer-invisibility-spec to an
  305. ;; appropriate value. Currently, it only handles timestamps, thus its
  306. ;; location. If you add other features which affect invisibility,
  307. ;; please modify this function and move it to a more appropriate
  308. ;; location.
  309. (defun erc-munge-invisibility-spec ()
  310. (if erc-hide-timestamps
  311. (setq buffer-invisibility-spec
  312. (if (listp buffer-invisibility-spec)
  313. (cons 'timestamp buffer-invisibility-spec)
  314. (list 't 'timestamp)))
  315. (setq buffer-invisibility-spec
  316. (if (listp buffer-invisibility-spec)
  317. (remove 'timestamp buffer-invisibility-spec)
  318. (list 't)))))
  319. (defun erc-hide-timestamps ()
  320. "Hide timestamp information from display."
  321. (interactive)
  322. (setq erc-hide-timestamps t)
  323. (erc-munge-invisibility-spec))
  324. (defun erc-show-timestamps ()
  325. "Show timestamp information on display.
  326. This function only works if `erc-timestamp-format' was previously
  327. set, and timestamping is already active."
  328. (interactive)
  329. (setq erc-hide-timestamps nil)
  330. (erc-munge-invisibility-spec))
  331. (defun erc-toggle-timestamps ()
  332. "Hide or show timestamps in ERC buffers.
  333. Note that timestamps can only be shown for a message using this
  334. function if `erc-timestamp-format' was set and timestamping was
  335. enabled when the message was inserted."
  336. (interactive)
  337. (if erc-hide-timestamps
  338. (setq erc-hide-timestamps nil)
  339. (setq erc-hide-timestamps t))
  340. (mapc (lambda (buffer)
  341. (with-current-buffer buffer
  342. (erc-munge-invisibility-spec)))
  343. (erc-buffer-list)))
  344. (defun erc-echo-timestamp (before now)
  345. "Print timestamp text-property of an IRC message.
  346. Argument BEFORE is where point was before it got moved and
  347. NOW is position of point currently."
  348. (when erc-echo-timestamps
  349. (let ((stamp (get-text-property now 'timestamp)))
  350. (when stamp
  351. (message "%s" (format-time-string erc-echo-timestamp-format
  352. stamp))))))
  353. (provide 'erc-stamp)
  354. ;;; erc-stamp.el ends here
  355. ;;
  356. ;; Local Variables:
  357. ;; indent-tabs-mode: t
  358. ;; tab-width: 8
  359. ;; End: