erc-stamp.el 15 KB

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