-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy patherc-view-log.el
209 lines (169 loc) · 6.77 KB
/
erc-view-log.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
;;; erc-view-log.el --- Major mode for viewing ERC logs
;; Copyright (C) 2010 Antoine Levitt
;; Copyright (C) 2010 Thomas Riccardi
;; Author: Antoine Levitt
;; Thomas Riccardi <[email protected]>
;; URL: http://github.com/Niluge-KiWi/erc-view-log/raw/master/erc-view-log.el
;; Homepage: http://github.com/Niluge-KiWi/erc-view-log/
;; Keywords: ERC viewer logs colors
;; This program is free software. It comes without any warranty, to
;; the extent permitted by applicable law. You can redistribute it
;; and/or modify it under the terms of the Do What The Fuck You Want
;; To Public License, Version 2, as published by Sam Hocevar. See
;; http://sam.zoy.org/wtfpl/COPYING for more details.
;;; Commentary:
;; Set colors on an ERC log file
;; Will not work with erc-fill-mode
;; Installation:
;; (require 'erc-view-log)
;; (add-to-list 'auto-mode-alist `(,(format "%s/.*\\.log" (regexp-quote (expand-file-name erc-log-channels-directory))) . erc-view-log-mode))
;; Recommended:
;; (add-hook 'erc-view-log-mode-hook 'turn-on-auto-revert-tail-mode)
;; Options:
;; - erc-view-log-nickname-face-function:
;; A function that returns a face, given a nick, to colorize nicks.
;; Can be nil to use standard ERC face.
;; - erc-view-log-my-nickname-match:
;; Either a regexp or a list of nicks, to match the user's nickname.
;; For the list, each nick should be unique and should not contain any regexps.
;; TODO:
;; - use vlf.el for large logs? has to be adapted (no more major mode, and handle full lines...)
;;; Code:
(require 'erc)
(defcustom erc-view-log-nickname-face-function
nil
"A function that returns a face, given a nick. nil to use default ERC face."
:type 'function
:group 'erc)
(defcustom erc-view-log-my-nickname-match
erc-nick
"A match for the user's nickname: either a regexp, or a list of nicks."
:type '(choice (regexp :tag "A regexp that matches the user's nick.")
(list :tag "A list of used nicks. Each nick should be unique and should not contain any regexps."))
:group 'erc)
;; Warning: do not use group constructions ("\\(some regexp\\)") inside the following regexps
(defvar erc-view-log-timestamp-regexp
"[^<]*"
"Regexp to match timestamps.")
(defvar erc-view-log-nickname-regexp
erc-valid-nick-regexp
"Regexp to match nicknames.")
(defvar erc-view-log-message-regexp
".*"
"Regexp to match messages.")
(defvar erc-view-log-current-nick-regexp
"\\*\\*\\* Users on .*: .*"
"Regexp to match current nicks lines.")
(defvar erc-view-log-notice-regexp
"\\*\\*\\* .*"
"Regexp to match notices.")
(defvar erc-view-log-action-regexp
(format "\\* %s .*" erc-valid-nick-regexp)
"Regexp to match actions.")
(defvar erc-view-log-prompt-regexp
erc-prompt
"Regexp to match prompts.")
(defun erc-log-nick-get-face (nick)
"Returns a face for the given nick."
(if erc-view-log-nickname-face-function
(apply erc-view-log-nickname-face-function (list nick))
'erc-nick-default-face))
(defun erc-log-get-my-nick-regexp ()
"Returns a regexp that matches the user's nick according to custom erc-view-log-my-nickname-match."
(if (listp erc-view-log-my-nickname-match)
(regexp-opt erc-view-log-my-nickname-match)
erc-view-log-my-nickname-match))
;; warning: only works if erc-timestamp-format doesn't contains the pattern "<a_nickname>"
(defun erc-view-log-get-keywords ()
"Returns the font-lock-defaults."
(list
;; own message line
`(,(format "^\\(%s\\) \\(<\\)\\(%s\\)\\(>\\)[ \t]\\(%s\\)$" erc-view-log-timestamp-regexp (erc-log-get-my-nick-regexp) erc-view-log-message-regexp)
(1 'erc-timestamp-face)
(2 'erc-default-face)
(3 'erc-my-nick-face)
(4 'erc-default-face)
(5 'erc-input-face) ;; own message
)
;; standard message line
`(,(format "^\\(%s\\) \\(<\\)\\(%s\\)\\(>\\)[ \t]\\(%s\\)$" erc-view-log-timestamp-regexp erc-view-log-nickname-regexp erc-view-log-message-regexp)
(1 'erc-timestamp-face)
(2 'erc-default-face)
(3 (erc-log-nick-get-face (match-string 3)))
(4 'erc-default-face)
(5 'erc-default-face) ;; other message
)
;; current nicks line
`(,(format "\\(%s\\) \\(%s\\)" erc-view-log-timestamp-regexp erc-view-log-current-nick-regexp)
(1 'erc-timestamp-face)
(2 'erc-current-nick-face)
)
;; notice line
`(,(format "\\(%s\\) \\(%s\\)" erc-view-log-timestamp-regexp erc-view-log-notice-regexp)
(1 'erc-timestamp-face)
(2 'erc-notice-face)
)
;; action line
`(,(format "\\(%s\\) \\(%s\\)" erc-view-log-timestamp-regexp erc-view-log-action-regexp)
(1 'erc-timestamp-face)
(2 'erc-action-face)
)
;; command line
`(,(format "\\(%s\\) \\(%s\\) \\(/.*\\)" erc-view-log-timestamp-regexp erc-view-log-prompt-regexp)
(1 'erc-timestamp-face)
(2 'erc-prompt-face)
(3 'erc-command-indicator-face)
)
))
;; undefine some syntax that's messing up with our coloring (for instance, "")
(defvar erc-view-log-mode-syntax-table
(let ((st (make-syntax-table)))
(modify-syntax-entry ?\" ". " st)
(modify-syntax-entry ?\\ ". " st)
st)
"Syntax table used while in `erc-view-log-mode'.")
(defun erc-view-log-reload-file ()
"Reload the current logfile."
(interactive)
(revert-buffer t t t)
;; revert-buffer removes read-only state
(setq buffer-read-only t))
(defun erc-view-log-previous-mention (&optional arg)
"Move point to previous mention of one's nick.
If ARG is set, move to previous message from one's nick."
(interactive "P")
(re-search-backward
(format (if (null arg) "%s" "^[^<]*<%s>")
(regexp-opt erc-view-log-my-nickname-match))))
(defun erc-view-log-next-mention (&optional arg)
"Move point to next mention of one's nick.
If ARG is set, move to next message from one's nick."
(interactive "P")
(re-search-forward
(format (if (null arg) "%s" "^[^<]*<%s>")
(regexp-opt erc-view-log-my-nickname-match))))
;; Create the keymap for this mode.
(defvar erc-view-log-mode-map
(let ((map (make-sparse-keymap)))
(define-key map "g" 'erc-view-log-reload-file)
(define-key map "p" 'erc-view-log-previous-mention)
(define-key map "n" 'erc-view-log-next-mention)
map)
"Keymap for `erc-view-log-mode'.")
;;;###autoload
(define-derived-mode erc-view-log-mode fundamental-mode
"ERC View Log"
"Major mode for viewing ERC logs.
Colorizes the log file as if it were a standard ERC buffer.
Special commands:
\\{erc-view-log-mode-map}
Turning on `erc-view-log-mode' runs the hook `erc-view-log-mode-hook'."
(setq font-lock-defaults `(,(erc-view-log-get-keywords)))
(setq buffer-read-only t)
;; workaround for emacs bug #11943: 24.1.50; Emacs unusably slow when looking at large files
;;(setq bidi-paragraph-direction 'left-to-right)
;; even faster workaround
(setq bidi-display-reordering nil)
)
(provide 'erc-view-log)
;;; erc-view-log.el ends here