Commit | Line | Data |
---|---|---|
bd43c990 RS |
1 | ;;; rcirc.el --- default, simple IRC client. |
2 | ||
d7a0267c | 3 | ;; Copyright (C) 2005, 2006, 2007 Free Software Foundation, Inc. |
bd43c990 RS |
4 | |
5 | ;; Author: Ryan Yeske | |
bd43c990 RS |
6 | ;; URL: http://www.nongnu.org/rcirc |
7 | ;; Keywords: comm | |
8 | ||
b71cef5c | 9 | ;; This file is part of GNU Emacs. |
bd43c990 RS |
10 | |
11 | ;; This file 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 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
15 | ||
16 | ;; This file is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
92c4adc1 | 18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
bd43c990 RS |
19 | ;; GNU General Public License for more details. |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
b71cef5c RF |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the |
23 | ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, | |
24 | ;; Boston, MA 02110-1301, USA. | |
bd43c990 RS |
25 | |
26 | ;;; Commentary: | |
27 | ||
adf794e4 EZ |
28 | ;; Internet Relay Chat (IRC) is a form of instant communication over |
29 | ;; the Internet. It is mainly designed for group (many-to-many) | |
30 | ;; communication in discussion forums called channels, but also allows | |
31 | ;; one-to-one communication. | |
32 | ||
33 | ;; Rcirc has simple defaults and clear and consistent behaviour. | |
34 | ;; Message arrival timestamps, activity notification on the modeline, | |
35 | ;; message filling, nick completion, and keepalive pings are all | |
36 | ;; enabled by default, but can easily be adjusted or turned off. Each | |
37 | ;; discussion takes place in its own buffer and there is a single | |
38 | ;; server buffer per connection. | |
bd43c990 | 39 | |
bd43c990 RS |
40 | ;; Open a new irc connection with: |
41 | ;; M-x irc RET | |
42 | ||
7faa3f8c MB |
43 | ;;; Todo: |
44 | ||
bd43c990 RS |
45 | ;;; Code: |
46 | ||
47 | (require 'ring) | |
48 | (require 'time-date) | |
49 | (eval-when-compile (require 'cl)) | |
50 | ||
adf794e4 EZ |
51 | (defgroup rcirc nil |
52 | "Simple IRC client." | |
53 | :version "22.1" | |
2fbed782 | 54 | :prefix "rcirc-" |
e8f10ddb | 55 | :link '(custom-manual "(rcirc)") |
adf794e4 EZ |
56 | :group 'applications) |
57 | ||
195eca78 SM |
58 | (defcustom rcirc-connections |
59 | '(("irc.freenode.net" :channels ("#rcirc"))) | |
60 | "An alist of IRC connections to establish when running `rcirc'. | |
61 | Each element looks like (SERVER-NAME PARAMETERS). | |
62 | ||
63 | SERVER-NAME is a string describing the server to connect | |
64 | to. | |
65 | ||
66 | PARAMETERS is a plist of optional connection parameters. Valid | |
67 | properties are: nick (a string), port (number or string), | |
68 | user-name (string), full-name (string), and channels (list of | |
69 | strings)." | |
70 | :type '(alist :key-type string | |
71 | :value-type (plist :options ((nick string) | |
72 | (port integer) | |
73 | (user-name string) | |
74 | (full-name string) | |
75 | (channels (repeat string))))) | |
adf794e4 | 76 | :group 'rcirc) |
bd43c990 | 77 | |
a2524d26 | 78 | (defcustom rcirc-default-port 6667 |
adf794e4 EZ |
79 | "The default port to connect to." |
80 | :type 'integer | |
81 | :group 'rcirc) | |
bd43c990 | 82 | |
a2524d26 | 83 | (defcustom rcirc-default-nick (user-login-name) |
adf794e4 EZ |
84 | "Your nick." |
85 | :type 'string | |
86 | :group 'rcirc) | |
bd43c990 | 87 | |
a2524d26 | 88 | (defcustom rcirc-default-user-name (user-login-name) |
adf794e4 EZ |
89 | "Your user name sent to the server when connecting." |
90 | :type 'string | |
91 | :group 'rcirc) | |
bd43c990 | 92 | |
a2524d26 | 93 | (defcustom rcirc-default-user-full-name (if (string= (user-full-name) "") |
18aa2c90 | 94 | rcirc-default-user-name |
02f47e86 | 95 | (user-full-name)) |
adf794e4 EZ |
96 | "The full name sent to the server when connecting." |
97 | :type 'string | |
98 | :group 'rcirc) | |
bd43c990 | 99 | |
adf794e4 EZ |
100 | (defcustom rcirc-fill-flag t |
101 | "*Non-nil means line-wrap messages printed in channel buffers." | |
102 | :type 'boolean | |
103 | :group 'rcirc) | |
bd43c990 | 104 | |
adf794e4 EZ |
105 | (defcustom rcirc-fill-column nil |
106 | "*Column beyond which automatic line-wrapping should happen. | |
195eca78 SM |
107 | If nil, use value of `fill-column'. If 'frame-width, use the |
108 | maximum frame width." | |
adf794e4 EZ |
109 | :type '(choice (const :tag "Value of `fill-column'") |
110 | (const :tag "Full frame width" frame-width) | |
111 | (integer :tag "Number of columns")) | |
112 | :group 'rcirc) | |
bd43c990 | 113 | |
adf794e4 | 114 | (defcustom rcirc-fill-prefix nil |
bd43c990 RS |
115 | "*Text to insert before filled lines. |
116 | If nil, calculate the prefix dynamically to line up text | |
adf794e4 EZ |
117 | underneath each nick." |
118 | :type '(choice (const :tag "Dynamic" nil) | |
119 | (string :tag "Prefix text")) | |
120 | :group 'rcirc) | |
bd43c990 | 121 | |
adf794e4 EZ |
122 | (defvar rcirc-ignore-buffer-activity-flag nil |
123 | "If non-nil, ignore activity in this buffer.") | |
124 | (make-variable-buffer-local 'rcirc-ignore-buffer-activity-flag) | |
bd43c990 | 125 | |
a2524d26 EZ |
126 | (defvar rcirc-low-priority-flag nil |
127 | "If non-nil, activity in this buffer is considered low priority.") | |
128 | (make-variable-buffer-local 'rcirc-low-priority-flag) | |
129 | ||
195eca78 SM |
130 | (defvar rcirc-omit-mode nil |
131 | "Non-nil if Rcirc-Omit mode is enabled. | |
132 | Use the command `rcirc-omit-mode' to change this variable.") | |
133 | (make-variable-buffer-local 'rcirc-omit-mode) | |
134 | ||
adf794e4 | 135 | (defcustom rcirc-time-format "%H:%M " |
bd43c990 | 136 | "*Describes how timestamps are printed. |
adf794e4 EZ |
137 | Used as the first arg to `format-time-string'." |
138 | :type 'string | |
139 | :group 'rcirc) | |
bd43c990 | 140 | |
adf794e4 EZ |
141 | (defcustom rcirc-input-ring-size 1024 |
142 | "*Size of input history ring." | |
143 | :type 'integer | |
144 | :group 'rcirc) | |
bd43c990 | 145 | |
adf794e4 | 146 | (defcustom rcirc-read-only-flag t |
2e398771 | 147 | "*Non-nil means make text in IRC buffers read-only." |
adf794e4 EZ |
148 | :type 'boolean |
149 | :group 'rcirc) | |
bd43c990 | 150 | |
adf794e4 | 151 | (defcustom rcirc-buffer-maximum-lines nil |
bd43c990 RS |
152 | "*The maximum size in lines for rcirc buffers. |
153 | Channel buffers are truncated from the top to be no greater than this | |
92c4adc1 | 154 | number. If zero or nil, no truncating is done." |
adf794e4 EZ |
155 | :type '(choice (const :tag "No truncation" nil) |
156 | (integer :tag "Number of lines")) | |
157 | :group 'rcirc) | |
bd43c990 | 158 | |
d40ac716 | 159 | (defcustom rcirc-scroll-show-maximum-output t |
195eca78 SM |
160 | "*If non-nil, scroll buffer to keep the point at the bottom of |
161 | the window." | |
f8db61b2 EZ |
162 | :type 'boolean |
163 | :group 'rcirc) | |
7faa3f8c | 164 | |
db58efbf EZ |
165 | (defcustom rcirc-authinfo nil |
166 | "List of authentication passwords. | |
167 | Each element of the list is a list with a SERVER-REGEXP string | |
168 | and a method symbol followed by method specific arguments. | |
169 | ||
170 | The valid METHOD symbols are `nickserv', `chanserv' and | |
171 | `bitlbee'. | |
bd43c990 RS |
172 | |
173 | The required ARGUMENTS for each METHOD symbol are: | |
db58efbf EZ |
174 | `nickserv': NICK PASSWORD |
175 | `chanserv': NICK CHANNEL PASSWORD | |
176 | `bitlbee': NICK PASSWORD | |
bd43c990 RS |
177 | |
178 | Example: | |
db58efbf EZ |
179 | ((\"freenode\" nickserv \"bob\" \"p455w0rd\") |
180 | (\"freenode\" chanserv \"bob\" \"#bobland\" \"passwd99\") | |
181 | (\"bitlbee\" bitlbee \"robert\" \"sekrit\"))" | |
182 | :type '(alist :key-type (string :tag "Server") | |
183 | :value-type (choice (list :tag "NickServ" | |
184 | (const nickserv) | |
185 | (string :tag "Nick") | |
186 | (string :tag "Password")) | |
187 | (list :tag "ChanServ" | |
188 | (const chanserv) | |
189 | (string :tag "Nick") | |
190 | (string :tag "Channel") | |
191 | (string :tag "Password")) | |
192 | (list :tag "BitlBee" | |
193 | (const bitlbee) | |
194 | (string :tag "Nick") | |
195 | (string :tag "Password")))) | |
adf794e4 | 196 | :group 'rcirc) |
bd43c990 | 197 | |
db58efbf | 198 | (defcustom rcirc-auto-authenticate-flag t |
bd43c990 | 199 | "*Non-nil means automatically send authentication string to server. |
db58efbf | 200 | See also `rcirc-authinfo'." |
adf794e4 EZ |
201 | :type 'boolean |
202 | :group 'rcirc) | |
bd43c990 | 203 | |
adf794e4 | 204 | (defcustom rcirc-prompt "> " |
2e398771 | 205 | "Prompt string to use in IRC buffers. |
bd43c990 RS |
206 | |
207 | The following replacements are made: | |
208 | %n is your nick. | |
209 | %s is the server. | |
210 | %t is the buffer target, a channel or a user. | |
211 | ||
adf794e4 EZ |
212 | Setting this alone will not affect the prompt; |
213 | use either M-x customize or also call `rcirc-update-prompt'." | |
214 | :type 'string | |
215 | :set 'rcirc-set-changed | |
216 | :initialize 'custom-initialize-default | |
217 | :group 'rcirc) | |
218 | ||
f8db61b2 EZ |
219 | (defcustom rcirc-keywords nil |
220 | "List of keywords to highlight in message text." | |
221 | :type '(repeat string) | |
222 | :group 'rcirc) | |
223 | ||
2c8abe90 AS |
224 | (defcustom rcirc-ignore-list () |
225 | "List of ignored nicks. | |
226 | Use /ignore to list them, use /ignore NICK to add or remove a nick." | |
227 | :type '(repeat string) | |
228 | :group 'rcirc) | |
229 | ||
230 | (defvar rcirc-ignore-list-automatic () | |
231 | "List of ignored nicks added to `rcirc-ignore-list' because of renaming. | |
232 | When an ignored person renames, their nick is added to both lists. | |
233 | Nicks will be removed from the automatic list on follow-up renamings or | |
234 | parts.") | |
235 | ||
f8db61b2 EZ |
236 | (defcustom rcirc-bright-nicks nil |
237 | "List of nicks to be emphasized. | |
02f47e86 | 238 | See `rcirc-bright-nick' face." |
f8db61b2 | 239 | :type '(repeat string) |
02f47e86 MB |
240 | :group 'rcirc) |
241 | ||
f8db61b2 EZ |
242 | (defcustom rcirc-dim-nicks nil |
243 | "List of nicks to be deemphasized. | |
02f47e86 | 244 | See `rcirc-dim-nick' face." |
f8db61b2 | 245 | :type '(repeat string) |
02f47e86 MB |
246 | :group 'rcirc) |
247 | ||
adf794e4 EZ |
248 | (defcustom rcirc-print-hooks nil |
249 | "Hook run after text is printed. | |
250 | Called with 5 arguments, PROCESS, SENDER, RESPONSE, TARGET and TEXT." | |
251 | :type 'hook | |
252 | :group 'rcirc) | |
bd43c990 | 253 | |
db58efbf EZ |
254 | (defcustom rcirc-always-use-server-buffer-flag nil |
255 | "Non-nil means messages without a channel target will go to the server buffer." | |
256 | :type 'boolean | |
257 | :group 'rcirc) | |
258 | ||
02f47e86 | 259 | (defcustom rcirc-decode-coding-system 'utf-8 |
a2524d26 EZ |
260 | "Coding system used to decode incoming irc messages." |
261 | :type 'coding-system | |
262 | :group 'rcirc) | |
263 | ||
264 | (defcustom rcirc-encode-coding-system 'utf-8 | |
265 | "Coding system used to encode outgoing irc messages." | |
266 | :type 'coding-system | |
267 | :group 'rcirc) | |
268 | ||
269 | (defcustom rcirc-coding-system-alist nil | |
f8db61b2 | 270 | "Alist to decide a coding system to use for a channel I/O operation. |
a2524d26 EZ |
271 | The format is ((PATTERN . VAL) ...). |
272 | PATTERN is either a string or a cons of strings. | |
273 | If PATTERN is a string, it is used to match a target. | |
274 | If PATTERN is a cons of strings, the car part is used to match a | |
275 | target, and the cdr part is used to match a server. | |
276 | VAL is either a coding system or a cons of coding systems. | |
277 | If VAL is a coding system, it is used for both decoding and encoding | |
278 | messages. | |
279 | If VAL is a cons of coding systems, the car part is used for decoding, | |
280 | and the cdr part is used for encoding." | |
281 | :type '(alist :key-type (choice (string :tag "Channel Regexp") | |
282 | (cons (string :tag "Channel Regexp") | |
283 | (string :tag "Server Regexp"))) | |
284 | :value-type (choice coding-system | |
285 | (cons (coding-system :tag "Decode") | |
286 | (coding-system :tag "Encode")))) | |
287 | :group 'rcirc) | |
288 | ||
289 | (defcustom rcirc-multiline-major-mode 'fundamental-mode | |
290 | "Major-mode function to use in multiline edit buffers." | |
291 | :type 'function | |
292 | :group 'rcirc) | |
293 | ||
294 | (defvar rcirc-nick nil) | |
295 | ||
bd43c990 RS |
296 | (defvar rcirc-prompt-start-marker nil) |
297 | (defvar rcirc-prompt-end-marker nil) | |
298 | ||
299 | (defvar rcirc-nick-table nil) | |
300 | ||
2c8abe90 AS |
301 | (defvar rcirc-nick-syntax-table |
302 | (let ((table (make-syntax-table text-mode-syntax-table))) | |
303 | (mapc (lambda (c) (modify-syntax-entry c "w" table)) | |
304 | "[]\\`_^{|}-") | |
305 | (modify-syntax-entry ?' "_" table) | |
306 | table) | |
307 | "Syntax table which includes all nick characters as word constituents.") | |
308 | ||
adf794e4 EZ |
309 | ;; each process has an alist of (target . buffer) pairs |
310 | (defvar rcirc-buffer-alist nil) | |
311 | ||
bd43c990 | 312 | (defvar rcirc-activity nil |
a2524d26 | 313 | "List of buffers with unviewed activity.") |
bd43c990 RS |
314 | |
315 | (defvar rcirc-activity-string "" | |
316 | "String displayed in modeline representing `rcirc-activity'.") | |
317 | (put 'rcirc-activity-string 'risky-local-variable t) | |
318 | ||
a2524d26 EZ |
319 | (defvar rcirc-server-buffer nil |
320 | "The server buffer associated with this channel buffer.") | |
bd43c990 RS |
321 | |
322 | (defvar rcirc-target nil | |
323 | "The channel or user associated with this buffer.") | |
324 | ||
bd43c990 RS |
325 | (defvar rcirc-urls nil |
326 | "List of urls seen in the current buffer.") | |
7faa3f8c | 327 | (put 'rcirc-urls 'permanent-local t) |
bd43c990 | 328 | |
2e875089 | 329 | (defvar rcirc-timeout-seconds 600 |
8216fbaf | 330 | "Kill connection after this many seconds if there is no activity.") |
bd43c990 | 331 | |
adf794e4 | 332 | (defconst rcirc-id-string (concat "rcirc on GNU Emacs " emacs-version)) |
bd43c990 | 333 | \f |
bd43c990 | 334 | (defvar rcirc-startup-channels nil) |
195eca78 | 335 | |
bd43c990 | 336 | ;;;###autoload |
db58efbf | 337 | (defun rcirc (arg) |
195eca78 SM |
338 | "Connect to all servers in `rcirc-connections'. |
339 | ||
340 | Do not connect to a server if it is already connected. | |
341 | ||
342 | If ARG is non-nil, instead prompt for connection parameters." | |
db58efbf EZ |
343 | (interactive "P") |
344 | (if arg | |
195eca78 SM |
345 | (let* ((server (completing-read "IRC Server: " |
346 | rcirc-connections | |
347 | nil nil | |
348 | (caar rcirc-connections))) | |
349 | (server-plist (cdr (assoc-string server rcirc-connections))) | |
350 | (port (read-string "IRC Port: " | |
351 | (number-to-string | |
352 | (or (plist-get server-plist 'port) | |
353 | rcirc-default-port)))) | |
354 | (nick (read-string "IRC Nick: " | |
355 | (or (plist-get server-plist 'nick) | |
356 | rcirc-default-nick))) | |
db58efbf EZ |
357 | (channels (split-string |
358 | (read-string "IRC Channels: " | |
195eca78 SM |
359 | (mapconcat 'identity |
360 | (plist-get server-plist | |
361 | 'channels) | |
362 | " ")) | |
db58efbf | 363 | "[, ]+" t))) |
195eca78 SM |
364 | (rcirc-connect server port nick rcirc-default-user-name |
365 | rcirc-default-user-full-name | |
db58efbf | 366 | channels)) |
195eca78 SM |
367 | ;; connect to servers in `rcirc-connections' |
368 | (let (connected-servers) | |
369 | (dolist (c rcirc-connections) | |
370 | (let ((server (car c)) | |
371 | (port (or (plist-get (cdr c) 'port) rcirc-default-port)) | |
372 | (nick (or (plist-get (cdr c) 'nick) rcirc-default-nick)) | |
373 | (user-name (or (plist-get (cdr c) 'user-name) | |
374 | rcirc-default-user-name)) | |
375 | (full-name (or (plist-get (cdr c) 'full-name) | |
376 | rcirc-default-user-full-name)) | |
377 | (channels (plist-get (cdr c) 'channels))) | |
378 | (when server | |
379 | (let (connected) | |
380 | (dolist (p (rcirc-process-list)) | |
381 | (when (string= server (process-name p)) | |
382 | (setq connected p))) | |
383 | (if (not connected) | |
384 | (condition-case e | |
385 | (rcirc-connect server port nick user-name | |
386 | full-name channels) | |
387 | (quit (message "Quit connecting to %s" server))) | |
388 | (with-current-buffer (process-buffer connected) | |
389 | (setq connected-servers | |
390 | (cons (process-contact (get-buffer-process | |
391 | (current-buffer)) :host) | |
392 | connected-servers)))))))) | |
393 | (when connected-servers | |
394 | (message "Already connected to %s" | |
395 | (concat (mapconcat 'identity (butlast connected-servers) ", ") | |
396 | ", and " (car (last connected-servers)))))))) | |
397 | ||
bd43c990 RS |
398 | ;;;###autoload |
399 | (defalias 'irc 'rcirc) | |
400 | ||
401 | \f | |
402 | (defvar rcirc-process-output nil) | |
bd43c990 RS |
403 | (defvar rcirc-topic nil) |
404 | (defvar rcirc-keepalive-timer nil) | |
ad8121fe | 405 | (defvar rcirc-last-server-message-time nil) |
8216fbaf EZ |
406 | (defvar rcirc-server nil) ; server provided by server |
407 | (defvar rcirc-server-name nil) ; server name given by 001 response | |
408 | (defvar rcirc-timeout-timer nil) | |
409 | (defvar rcirc-user-disconnect nil) | |
410 | (defvar rcirc-connecting nil) | |
411 | (defvar rcirc-process nil) | |
8d214091 RF |
412 | |
413 | ;;;###autoload | |
195eca78 SM |
414 | (defun rcirc-connect (server &optional port nick user-name full-name |
415 | startup-channels) | |
bd43c990 RS |
416 | (save-excursion |
417 | (message "Connecting to %s..." server) | |
418 | (let* ((inhibit-eol-conversion) | |
2fbed782 EZ |
419 | (port-number (if port |
420 | (if (stringp port) | |
421 | (string-to-number port) | |
422 | port) | |
a2524d26 | 423 | rcirc-default-port)) |
a2524d26 EZ |
424 | (nick (or nick rcirc-default-nick)) |
425 | (user-name (or user-name rcirc-default-user-name)) | |
426 | (full-name (or full-name rcirc-default-user-full-name)) | |
427 | (startup-channels startup-channels) | |
8216fbaf | 428 | (process (make-network-process :name server :host server :service port-number))) |
bd43c990 RS |
429 | ;; set up process |
430 | (set-process-coding-system process 'raw-text 'raw-text) | |
adf794e4 | 431 | (switch-to-buffer (rcirc-generate-new-buffer-name process nil)) |
bd43c990 | 432 | (set-process-buffer process (current-buffer)) |
bd43c990 | 433 | (rcirc-mode process nil) |
a2524d26 EZ |
434 | (set-process-sentinel process 'rcirc-sentinel) |
435 | (set-process-filter process 'rcirc-filter) | |
8216fbaf EZ |
436 | (make-local-variable 'rcirc-process) |
437 | (setq rcirc-process process) | |
a2524d26 EZ |
438 | (make-local-variable 'rcirc-server) |
439 | (setq rcirc-server server) | |
8216fbaf EZ |
440 | (make-local-variable 'rcirc-server-name) |
441 | (setq rcirc-server-name server) ; update when we get 001 response | |
adf794e4 EZ |
442 | (make-local-variable 'rcirc-buffer-alist) |
443 | (setq rcirc-buffer-alist nil) | |
bd43c990 RS |
444 | (make-local-variable 'rcirc-nick-table) |
445 | (setq rcirc-nick-table (make-hash-table :test 'equal)) | |
bd43c990 RS |
446 | (make-local-variable 'rcirc-nick) |
447 | (setq rcirc-nick nick) | |
448 | (make-local-variable 'rcirc-process-output) | |
449 | (setq rcirc-process-output nil) | |
bd43c990 RS |
450 | (make-local-variable 'rcirc-startup-channels) |
451 | (setq rcirc-startup-channels startup-channels) | |
ad8121fe EZ |
452 | (make-local-variable 'rcirc-last-server-message-time) |
453 | (setq rcirc-last-server-message-time (current-time)) | |
8216fbaf EZ |
454 | (make-local-variable 'rcirc-timeout-timer) |
455 | (setq rcirc-timeout-timer nil) | |
456 | (make-local-variable 'rcirc-user-disconnect) | |
457 | (setq rcirc-user-disconnect nil) | |
458 | (make-local-variable 'rcirc-connecting) | |
459 | (setq rcirc-connecting t) | |
bd43c990 | 460 | |
195eca78 SM |
461 | (add-hook 'auto-save-hook 'rcirc-log-write) |
462 | ||
bd43c990 RS |
463 | ;; identify |
464 | (rcirc-send-string process (concat "NICK " nick)) | |
465 | (rcirc-send-string process (concat "USER " user-name | |
466 | " hostname servername :" | |
467 | full-name)) | |
468 | ||
469 | ;; setup ping timer if necessary | |
8216fbaf EZ |
470 | (unless rcirc-keepalive-timer |
471 | (setq rcirc-keepalive-timer | |
472 | (run-at-time 0 (/ rcirc-timeout-seconds 2) 'rcirc-keepalive))) | |
bd43c990 RS |
473 | |
474 | (message "Connecting to %s...done" server) | |
475 | ||
476 | ;; return process object | |
477 | process))) | |
478 | ||
adf794e4 EZ |
479 | (defmacro with-rcirc-process-buffer (process &rest body) |
480 | (declare (indent 1) (debug t)) | |
481 | `(with-current-buffer (process-buffer ,process) | |
482 | ,@body)) | |
483 | ||
a2524d26 EZ |
484 | (defmacro with-rcirc-server-buffer (&rest body) |
485 | (declare (indent 0) (debug t)) | |
486 | `(with-current-buffer rcirc-server-buffer | |
487 | ,@body)) | |
488 | ||
bd43c990 | 489 | (defun rcirc-keepalive () |
ad8121fe EZ |
490 | "Send keep alive pings to active rcirc processes. |
491 | Kill processes that have not received a server message since the | |
492 | last ping." | |
bd43c990 RS |
493 | (if (rcirc-process-list) |
494 | (mapc (lambda (process) | |
8216fbaf EZ |
495 | (with-rcirc-process-buffer process |
496 | (when (not rcirc-connecting) | |
195eca78 SM |
497 | (rcirc-send-string process |
498 | (format "PRIVMSG %s :\C-aKEEPALIVE %f\C-a" | |
499 | rcirc-nick | |
500 | (time-to-seconds | |
501 | (current-time))))))) | |
bd43c990 | 502 | (rcirc-process-list)) |
8216fbaf | 503 | ;; no processes, clean up timer |
bd43c990 RS |
504 | (cancel-timer rcirc-keepalive-timer) |
505 | (setq rcirc-keepalive-timer nil))) | |
506 | ||
195eca78 SM |
507 | (defun rcirc-handler-ctcp-KEEPALIVE (process target sender message) |
508 | (with-rcirc-process-buffer process | |
509 | (setq header-line-format (format "%f" (- (time-to-seconds (current-time)) | |
510 | (string-to-number message)))))) | |
511 | ||
adf794e4 EZ |
512 | (defvar rcirc-debug-buffer " *rcirc debug*") |
513 | (defvar rcirc-debug-flag nil | |
514 | "If non-nil, write information to `rcirc-debug-buffer'.") | |
515 | (defun rcirc-debug (process text) | |
bd43c990 | 516 | "Add an entry to the debug log including PROCESS and TEXT. |
2e398771 | 517 | Debug text is written to `rcirc-debug-buffer' if `rcirc-debug-flag' |
adf794e4 EZ |
518 | is non-nil." |
519 | (when rcirc-debug-flag | |
bd43c990 | 520 | (save-excursion |
195eca78 SM |
521 | (set-buffer (get-buffer-create rcirc-debug-buffer)) |
522 | (goto-char (point-max)) | |
523 | (insert (concat | |
524 | "[" | |
525 | (format-time-string "%Y-%m-%dT%T ") (process-name process) | |
526 | "] " | |
527 | text))))) | |
adf794e4 | 528 | |
bd43c990 RS |
529 | (defvar rcirc-sentinel-hooks nil |
530 | "Hook functions called when the process sentinel is called. | |
531 | Functions are called with PROCESS and SENTINEL arguments.") | |
532 | ||
533 | (defun rcirc-sentinel (process sentinel) | |
534 | "Called when PROCESS receives SENTINEL." | |
535 | (let ((sentinel (replace-regexp-in-string "\n" "" sentinel))) | |
adf794e4 EZ |
536 | (rcirc-debug process (format "SENTINEL: %S %S\n" process sentinel)) |
537 | (with-rcirc-process-buffer process | |
538 | (dolist (buffer (cons nil (mapcar 'cdr rcirc-buffer-alist))) | |
adf794e4 | 539 | (with-current-buffer (or buffer (current-buffer)) |
db58efbf EZ |
540 | (rcirc-print process "rcirc.el" "ERROR" rcirc-target |
541 | (format "%s: %s (%S)" | |
542 | (process-name process) | |
543 | sentinel | |
8216fbaf | 544 | (process-status process)) (not rcirc-target)) |
195eca78 | 545 | (rcirc-disconnect-buffer))) |
8216fbaf | 546 | (run-hook-with-args 'rcirc-sentinel-hooks process sentinel)))) |
bd43c990 | 547 | |
195eca78 SM |
548 | (defun rcirc-disconnect-buffer (&optional buffer) |
549 | (with-current-buffer (or buffer (current-buffer)) | |
550 | ;; set rcirc-target to nil for each channel so cleanup | |
551 | ;; doesnt happen when we reconnect | |
552 | (setq rcirc-target nil) | |
553 | (setq mode-line-process ":disconnected"))) | |
554 | ||
bd43c990 RS |
555 | (defun rcirc-process-list () |
556 | "Return a list of rcirc processes." | |
557 | (let (ps) | |
558 | (mapc (lambda (p) | |
18aa2c90 | 559 | (when (buffer-live-p (process-buffer p)) |
adf794e4 | 560 | (with-rcirc-process-buffer p |
bd43c990 RS |
561 | (when (eq major-mode 'rcirc-mode) |
562 | (setq ps (cons p ps)))))) | |
563 | (process-list)) | |
564 | ps)) | |
565 | ||
566 | (defvar rcirc-receive-message-hooks nil | |
2e398771 JB |
567 | "Hook functions run when a message is received from server. |
568 | Function is called with PROCESS, COMMAND, SENDER, ARGS and LINE.") | |
bd43c990 RS |
569 | (defun rcirc-filter (process output) |
570 | "Called when PROCESS receives OUTPUT." | |
adf794e4 | 571 | (rcirc-debug process output) |
8216fbaf | 572 | (rcirc-reschedule-timeout process) |
adf794e4 | 573 | (with-rcirc-process-buffer process |
ad8121fe | 574 | (setq rcirc-last-server-message-time (current-time)) |
bd43c990 RS |
575 | (setq rcirc-process-output (concat rcirc-process-output output)) |
576 | (when (= (aref rcirc-process-output | |
577 | (1- (length rcirc-process-output))) ?\n) | |
578 | (mapc (lambda (line) | |
579 | (rcirc-process-server-response process line)) | |
adf794e4 | 580 | (split-string rcirc-process-output "[\n\r]" t)) |
bd43c990 RS |
581 | (setq rcirc-process-output nil)))) |
582 | ||
8216fbaf EZ |
583 | (defun rcirc-reschedule-timeout (process) |
584 | (with-rcirc-process-buffer process | |
585 | (when (not rcirc-connecting) | |
586 | (with-rcirc-process-buffer process | |
587 | (when rcirc-timeout-timer (cancel-timer rcirc-timeout-timer)) | |
588 | (setq rcirc-timeout-timer (run-at-time rcirc-timeout-seconds nil | |
589 | 'rcirc-delete-process | |
590 | process)))))) | |
591 | ||
592 | (defun rcirc-delete-process (process) | |
593 | (message "delete process %S" process) | |
594 | (delete-process process)) | |
595 | ||
adf794e4 | 596 | (defvar rcirc-trap-errors-flag t) |
bd43c990 | 597 | (defun rcirc-process-server-response (process text) |
adf794e4 | 598 | (if rcirc-trap-errors-flag |
bd43c990 RS |
599 | (condition-case err |
600 | (rcirc-process-server-response-1 process text) | |
601 | (error | |
602 | (rcirc-print process "RCIRC" "ERROR" nil | |
adf794e4 | 603 | (format "\"%s\" %s" text err) t))) |
bd43c990 RS |
604 | (rcirc-process-server-response-1 process text))) |
605 | ||
606 | (defun rcirc-process-server-response-1 (process text) | |
607 | (if (string-match "^\\(:\\([^ ]+\\) \\)?\\([^ ]+\\) \\(.+\\)$" text) | |
db58efbf EZ |
608 | (let* ((user (match-string 2 text)) |
609 | (sender (rcirc-user-nick user)) | |
bd43c990 RS |
610 | (cmd (match-string 3 text)) |
611 | (args (match-string 4 text)) | |
612 | (handler (intern-soft (concat "rcirc-handler-" cmd)))) | |
613 | (string-match "^\\([^:]*\\):?\\(.+\\)?$" args) | |
614 | (let* ((args1 (match-string 1 args)) | |
615 | (args2 (match-string 2 args)) | |
adf794e4 EZ |
616 | (args (delq nil (append (split-string args1 " " t) |
617 | (list args2))))) | |
bd43c990 RS |
618 | (if (not (fboundp handler)) |
619 | (rcirc-handler-generic process cmd sender args text) | |
620 | (funcall handler process sender args text)) | |
621 | (run-hook-with-args 'rcirc-receive-message-hooks | |
622 | process cmd sender args text))) | |
623 | (message "UNHANDLED: %s" text))) | |
624 | ||
f8db61b2 EZ |
625 | (defvar rcirc-responses-no-activity '("305" "306") |
626 | "Responses that don't trigger activity in the mode-line indicator.") | |
627 | ||
628 | (defun rcirc-handler-generic (process response sender args text) | |
bd43c990 | 629 | "Generic server response handler." |
f8db61b2 EZ |
630 | (rcirc-print process sender response nil |
631 | (mapconcat 'identity (cdr args) " ") | |
632 | (not (member response rcirc-responses-no-activity)))) | |
bd43c990 RS |
633 | |
634 | (defun rcirc-send-string (process string) | |
635 | "Send PROCESS a STRING plus a newline." | |
a2524d26 | 636 | (let ((string (concat (encode-coding-string string rcirc-encode-coding-system) |
bd43c990 | 637 | "\n"))) |
a2524d26 | 638 | (unless (eq (process-status process) 'open) |
53f831f3 | 639 | (error "Network connection to %s is not open" |
a2524d26 | 640 | (process-name process))) |
adf794e4 | 641 | (rcirc-debug process string) |
bd43c990 RS |
642 | (process-send-string process string))) |
643 | ||
a2524d26 EZ |
644 | (defun rcirc-buffer-process (&optional buffer) |
645 | "Return the process associated with channel BUFFER. | |
646 | With no argument or nil as argument, use the current buffer." | |
8216fbaf EZ |
647 | (or (get-buffer-process (if buffer |
648 | (with-current-buffer buffer | |
649 | rcirc-server-buffer) | |
650 | rcirc-server-buffer)) | |
651 | rcirc-process)) | |
a2524d26 EZ |
652 | |
653 | (defun rcirc-server-name (process) | |
654 | "Return PROCESS server name, given by the 001 response." | |
adf794e4 | 655 | (with-rcirc-process-buffer process |
195eca78 SM |
656 | (or rcirc-server-name |
657 | (warn "server name for process %S unknown" process)))) | |
bd43c990 RS |
658 | |
659 | (defun rcirc-nick (process) | |
660 | "Return PROCESS nick." | |
92c4adc1 | 661 | (with-rcirc-process-buffer process |
a2524d26 EZ |
662 | (or rcirc-nick rcirc-default-nick))) |
663 | ||
664 | (defun rcirc-buffer-nick (&optional buffer) | |
665 | "Return the nick associated with BUFFER. | |
666 | With no argument or nil as argument, use the current buffer." | |
667 | (with-current-buffer (or buffer (current-buffer)) | |
668 | (with-current-buffer rcirc-server-buffer | |
669 | (or rcirc-nick rcirc-default-nick)))) | |
bd43c990 | 670 | |
02f47e86 | 671 | (defvar rcirc-max-message-length 420 |
bd43c990 RS |
672 | "Messages longer than this value will be split.") |
673 | ||
195eca78 | 674 | (defun rcirc-send-message (process target message &optional noticep silent) |
bd43c990 | 675 | "Send TARGET associated with PROCESS a privmsg with text MESSAGE. |
195eca78 SM |
676 | If NOTICEP is non-nil, send a notice instead of privmsg. |
677 | If SILENT is non-nil, do not print the message in any irc buffer." | |
bd43c990 RS |
678 | ;; max message length is 512 including CRLF |
679 | (let* ((response (if noticep "NOTICE" "PRIVMSG")) | |
680 | (oversize (> (length message) rcirc-max-message-length)) | |
681 | (text (if oversize | |
682 | (substring message 0 rcirc-max-message-length) | |
683 | message)) | |
684 | (text (if (string= text "") | |
685 | " " | |
686 | text)) | |
687 | (more (if oversize | |
688 | (substring message rcirc-max-message-length)))) | |
db58efbf | 689 | (rcirc-get-buffer-create process target) |
bd43c990 | 690 | (rcirc-send-string process (concat response " " target " :" text)) |
195eca78 SM |
691 | (unless silent |
692 | (rcirc-print process (rcirc-nick process) response target text)) | |
db58efbf | 693 | (when more (rcirc-send-message process target more noticep)))) |
bd43c990 RS |
694 | |
695 | (defvar rcirc-input-ring nil) | |
696 | (defvar rcirc-input-ring-index 0) | |
697 | (defun rcirc-prev-input-string (arg) | |
698 | (ring-ref rcirc-input-ring (+ rcirc-input-ring-index arg))) | |
699 | ||
700 | (defun rcirc-insert-prev-input (arg) | |
701 | (interactive "p") | |
702 | (when (<= rcirc-prompt-end-marker (point)) | |
703 | (delete-region rcirc-prompt-end-marker (point-max)) | |
704 | (insert (rcirc-prev-input-string 0)) | |
705 | (setq rcirc-input-ring-index (1+ rcirc-input-ring-index)))) | |
706 | ||
707 | (defun rcirc-insert-next-input (arg) | |
708 | (interactive "p") | |
709 | (when (<= rcirc-prompt-end-marker (point)) | |
710 | (delete-region rcirc-prompt-end-marker (point-max)) | |
711 | (setq rcirc-input-ring-index (1- rcirc-input-ring-index)) | |
712 | (insert (rcirc-prev-input-string -1)))) | |
713 | ||
714 | (defvar rcirc-nick-completions nil) | |
715 | (defvar rcirc-nick-completion-start-offset nil) | |
7faa3f8c | 716 | |
bd43c990 RS |
717 | (defun rcirc-complete-nick () |
718 | "Cycle through nick completions from list of nicks in channel." | |
719 | (interactive) | |
7faa3f8c | 720 | (if (eq last-command this-command) |
bd43c990 RS |
721 | (setq rcirc-nick-completions |
722 | (append (cdr rcirc-nick-completions) | |
723 | (list (car rcirc-nick-completions)))) | |
724 | (setq rcirc-nick-completion-start-offset | |
725 | (- (save-excursion | |
726 | (if (re-search-backward " " rcirc-prompt-end-marker t) | |
727 | (1+ (point)) | |
728 | rcirc-prompt-end-marker)) | |
729 | rcirc-prompt-end-marker)) | |
730 | (setq rcirc-nick-completions | |
731 | (let ((completion-ignore-case t)) | |
adf794e4 EZ |
732 | (all-completions |
733 | (buffer-substring | |
bd43c990 RS |
734 | (+ rcirc-prompt-end-marker |
735 | rcirc-nick-completion-start-offset) | |
736 | (point)) | |
737 | (mapcar (lambda (x) (cons x nil)) | |
a2524d26 EZ |
738 | (rcirc-channel-nicks (rcirc-buffer-process) |
739 | rcirc-target)))))) | |
bd43c990 RS |
740 | (let ((completion (car rcirc-nick-completions))) |
741 | (when completion | |
7faa3f8c | 742 | (rcirc-put-nick-channel (rcirc-buffer-process) completion rcirc-target) |
adf794e4 | 743 | (delete-region (+ rcirc-prompt-end-marker |
7faa3f8c MB |
744 | rcirc-nick-completion-start-offset) |
745 | (point)) | |
bd43c990 | 746 | (insert (concat completion |
adf794e4 | 747 | (if (= (+ rcirc-prompt-end-marker |
bd43c990 RS |
748 | rcirc-nick-completion-start-offset) |
749 | rcirc-prompt-end-marker) | |
750 | ": ")))))) | |
751 | ||
a2524d26 EZ |
752 | (defun set-rcirc-decode-coding-system (coding-system) |
753 | "Set the decode coding system used in this channel." | |
754 | (interactive "zCoding system for incoming messages: ") | |
755 | (setq rcirc-decode-coding-system coding-system)) | |
756 | ||
757 | (defun set-rcirc-encode-coding-system (coding-system) | |
758 | "Set the encode coding system used in this channel." | |
759 | (interactive "zCoding system for outgoing messages: ") | |
760 | (setq rcirc-encode-coding-system coding-system)) | |
bd43c990 RS |
761 | |
762 | (defvar rcirc-mode-map (make-sparse-keymap) | |
763 | "Keymap for rcirc mode.") | |
764 | ||
765 | (define-key rcirc-mode-map (kbd "RET") 'rcirc-send-input) | |
766 | (define-key rcirc-mode-map (kbd "M-p") 'rcirc-insert-prev-input) | |
767 | (define-key rcirc-mode-map (kbd "M-n") 'rcirc-insert-next-input) | |
768 | (define-key rcirc-mode-map (kbd "TAB") 'rcirc-complete-nick) | |
769 | (define-key rcirc-mode-map (kbd "C-c C-b") 'rcirc-browse-url) | |
770 | (define-key rcirc-mode-map (kbd "C-c C-c") 'rcirc-edit-multiline) | |
771 | (define-key rcirc-mode-map (kbd "C-c C-j") 'rcirc-cmd-join) | |
772 | (define-key rcirc-mode-map (kbd "C-c C-k") 'rcirc-cmd-kick) | |
a2524d26 | 773 | (define-key rcirc-mode-map (kbd "C-c C-l") 'rcirc-toggle-low-priority) |
bd43c990 RS |
774 | (define-key rcirc-mode-map (kbd "C-c C-d") 'rcirc-cmd-mode) |
775 | (define-key rcirc-mode-map (kbd "C-c C-m") 'rcirc-cmd-msg) | |
776 | (define-key rcirc-mode-map (kbd "C-c C-r") 'rcirc-cmd-nick) ; rename | |
195eca78 | 777 | (define-key rcirc-mode-map (kbd "C-c C-o") 'rcirc-omit-mode) |
bd43c990 RS |
778 | (define-key rcirc-mode-map (kbd "C-c C-p") 'rcirc-cmd-part) |
779 | (define-key rcirc-mode-map (kbd "C-c C-q") 'rcirc-cmd-query) | |
780 | (define-key rcirc-mode-map (kbd "C-c C-t") 'rcirc-cmd-topic) | |
781 | (define-key rcirc-mode-map (kbd "C-c C-n") 'rcirc-cmd-names) | |
782 | (define-key rcirc-mode-map (kbd "C-c C-w") 'rcirc-cmd-whois) | |
783 | (define-key rcirc-mode-map (kbd "C-c C-x") 'rcirc-cmd-quit) | |
784 | (define-key rcirc-mode-map (kbd "C-c TAB") ; C-i | |
adf794e4 | 785 | 'rcirc-toggle-ignore-buffer-activity) |
bd43c990 RS |
786 | (define-key rcirc-mode-map (kbd "C-c C-s") 'rcirc-switch-to-server-buffer) |
787 | (define-key rcirc-mode-map (kbd "C-c C-a") 'rcirc-jump-to-first-unread-line) | |
788 | ||
adf794e4 | 789 | (defvar rcirc-browse-url-map (make-sparse-keymap) |
2e398771 | 790 | "Keymap used for browsing URLs in `rcirc-mode'.") |
adf794e4 EZ |
791 | |
792 | (define-key rcirc-browse-url-map (kbd "RET") 'rcirc-browse-url-at-point) | |
793 | (define-key rcirc-browse-url-map (kbd "<mouse-2>") 'rcirc-browse-url-at-mouse) | |
794 | ||
795 | (defvar rcirc-short-buffer-name nil | |
796 | "Generated abbreviation to use to indicate buffer activity.") | |
797 | ||
bd43c990 RS |
798 | (defvar rcirc-mode-hook nil |
799 | "Hook run when setting up rcirc buffer.") | |
800 | ||
a2524d26 EZ |
801 | (defvar rcirc-last-post-time nil) |
802 | ||
195eca78 SM |
803 | (defvar rcirc-log-alist nil |
804 | "Alist of lines to log to disk when `rcirc-log-flag' is non-nil. | |
805 | Each element looks like (FILENAME . TEXT).") | |
806 | ||
bd43c990 | 807 | (defun rcirc-mode (process target) |
2e398771 | 808 | "Major mode for IRC channel buffers. |
bd43c990 RS |
809 | |
810 | \\{rcirc-mode-map}" | |
811 | (kill-all-local-variables) | |
812 | (use-local-map rcirc-mode-map) | |
813 | (setq mode-name "rcirc") | |
814 | (setq major-mode 'rcirc-mode) | |
195eca78 | 815 | (setq mode-line-process nil) |
bd43c990 RS |
816 | |
817 | (make-local-variable 'rcirc-input-ring) | |
818 | (setq rcirc-input-ring (make-ring rcirc-input-ring-size)) | |
a2524d26 EZ |
819 | (make-local-variable 'rcirc-server-buffer) |
820 | (setq rcirc-server-buffer (process-buffer process)) | |
bd43c990 RS |
821 | (make-local-variable 'rcirc-target) |
822 | (setq rcirc-target target) | |
ad8121fe EZ |
823 | (make-local-variable 'rcirc-topic) |
824 | (setq rcirc-topic nil) | |
a2524d26 EZ |
825 | (make-local-variable 'rcirc-last-post-time) |
826 | (setq rcirc-last-post-time (current-time)) | |
195eca78 SM |
827 | (make-local-variable 'fill-paragraph-function) |
828 | (setq fill-paragraph-function 'rcirc-fill-paragraph) | |
adf794e4 EZ |
829 | |
830 | (make-local-variable 'rcirc-short-buffer-name) | |
831 | (setq rcirc-short-buffer-name nil) | |
bd43c990 | 832 | (make-local-variable 'rcirc-urls) |
bd43c990 | 833 | (setq use-hard-newlines t) |
bd43c990 | 834 | |
a2524d26 EZ |
835 | (make-local-variable 'rcirc-decode-coding-system) |
836 | (make-local-variable 'rcirc-encode-coding-system) | |
837 | (dolist (i rcirc-coding-system-alist) | |
838 | (let ((chan (if (consp (car i)) (caar i) (car i))) | |
839 | (serv (if (consp (car i)) (cdar i) ""))) | |
840 | (when (and (string-match chan (or target "")) | |
841 | (string-match serv (rcirc-server-name process))) | |
aac5d1fd EZ |
842 | (setq rcirc-decode-coding-system (if (consp (cdr i)) (cadr i) (cdr i)) |
843 | rcirc-encode-coding-system (if (consp (cdr i)) (cddr i) (cdr i)))))) | |
a2524d26 | 844 | |
bd43c990 RS |
845 | ;; setup the prompt and markers |
846 | (make-local-variable 'rcirc-prompt-start-marker) | |
847 | (setq rcirc-prompt-start-marker (make-marker)) | |
848 | (set-marker rcirc-prompt-start-marker (point-max)) | |
849 | (make-local-variable 'rcirc-prompt-end-marker) | |
850 | (setq rcirc-prompt-end-marker (make-marker)) | |
851 | (set-marker rcirc-prompt-end-marker (point-max)) | |
852 | (rcirc-update-prompt) | |
853 | (goto-char rcirc-prompt-end-marker) | |
854 | (make-local-variable 'overlay-arrow-position) | |
855 | (setq overlay-arrow-position (make-marker)) | |
856 | (set-marker overlay-arrow-position nil) | |
857 | ||
195eca78 SM |
858 | (setq buffer-invisibility-spec '(rcirc-ignored-user)) |
859 | ||
a2524d26 EZ |
860 | ;; if the user changes the major mode or kills the buffer, there is |
861 | ;; cleanup work to do | |
f8db61b2 EZ |
862 | (add-hook 'change-major-mode-hook 'rcirc-change-major-mode-hook nil t) |
863 | (add-hook 'kill-buffer-hook 'rcirc-kill-buffer-hook nil t) | |
a2524d26 | 864 | |
adf794e4 EZ |
865 | ;; add to buffer list, and update buffer abbrevs |
866 | (when target ; skip server buffer | |
867 | (let ((buffer (current-buffer))) | |
868 | (with-rcirc-process-buffer process | |
869 | (setq rcirc-buffer-alist (cons (cons target buffer) | |
870 | rcirc-buffer-alist)))) | |
871 | (rcirc-update-short-buffer-names)) | |
872 | ||
bd43c990 RS |
873 | (run-hooks 'rcirc-mode-hook)) |
874 | ||
adf794e4 EZ |
875 | (defun rcirc-update-prompt (&optional all) |
876 | "Reset the prompt string in the current buffer. | |
c18a54de | 877 | |
adf794e4 EZ |
878 | If ALL is non-nil, update prompts in all IRC buffers." |
879 | (if all | |
880 | (mapc (lambda (process) | |
881 | (mapc (lambda (buffer) | |
882 | (with-current-buffer buffer | |
883 | (rcirc-update-prompt))) | |
884 | (with-rcirc-process-buffer process | |
885 | (mapcar 'cdr rcirc-buffer-alist)))) | |
886 | (rcirc-process-list)) | |
887 | (let ((inhibit-read-only t) | |
888 | (prompt (or rcirc-prompt ""))) | |
889 | (mapc (lambda (rep) | |
890 | (setq prompt | |
a2524d26 EZ |
891 | (replace-regexp-in-string (car rep) (cdr rep) prompt))) |
892 | (list (cons "%n" (rcirc-buffer-nick)) | |
8216fbaf | 893 | (cons "%s" (with-rcirc-server-buffer rcirc-server-name)) |
adf794e4 EZ |
894 | (cons "%t" (or rcirc-target "")))) |
895 | (save-excursion | |
896 | (delete-region rcirc-prompt-start-marker rcirc-prompt-end-marker) | |
897 | (goto-char rcirc-prompt-start-marker) | |
898 | (let ((start (point))) | |
899 | (insert-before-markers prompt) | |
900 | (set-marker rcirc-prompt-start-marker start) | |
901 | (when (not (zerop (- rcirc-prompt-end-marker | |
902 | rcirc-prompt-start-marker))) | |
903 | (add-text-properties rcirc-prompt-start-marker | |
904 | rcirc-prompt-end-marker | |
905 | (list 'face 'rcirc-prompt | |
906 | 'read-only t 'field t | |
907 | 'front-sticky t 'rear-nonsticky t)))))))) | |
908 | ||
909 | (defun rcirc-set-changed (option value) | |
910 | "Set OPTION to VALUE and do updates after a customization change." | |
911 | (set-default option value) | |
912 | (cond ((eq option 'rcirc-prompt) | |
913 | (rcirc-update-prompt 'all)) | |
914 | (t | |
915 | (error "Bad option %s" option)))) | |
bd43c990 RS |
916 | |
917 | (defun rcirc-channel-p (target) | |
918 | "Return t if TARGET is a channel name." | |
919 | (and target | |
920 | (not (zerop (length target))) | |
921 | (or (eq (aref target 0) ?#) | |
922 | (eq (aref target 0) ?&)))) | |
923 | ||
924 | (defun rcirc-kill-buffer-hook () | |
925 | "Part the channel when killing an rcirc buffer." | |
926 | (when (eq major-mode 'rcirc-mode) | |
a2524d26 EZ |
927 | (rcirc-clean-up-buffer "Killed buffer"))) |
928 | ||
929 | (defun rcirc-change-major-mode-hook () | |
930 | "Part the channel when changing the major-mode." | |
931 | (rcirc-clean-up-buffer "Changed major mode")) | |
932 | ||
933 | (defun rcirc-clean-up-buffer (reason) | |
adf794e4 EZ |
934 | (let ((buffer (current-buffer))) |
935 | (rcirc-clear-activity buffer) | |
a2524d26 EZ |
936 | (when (and (rcirc-buffer-process) |
937 | (eq (process-status (rcirc-buffer-process)) 'open)) | |
938 | (with-rcirc-server-buffer | |
939 | (setq rcirc-buffer-alist | |
940 | (rassq-delete-all buffer rcirc-buffer-alist))) | |
adf794e4 | 941 | (rcirc-update-short-buffer-names) |
bd43c990 | 942 | (if (rcirc-channel-p rcirc-target) |
a2524d26 EZ |
943 | (rcirc-send-string (rcirc-buffer-process) |
944 | (concat "PART " rcirc-target " :" reason)) | |
adf794e4 | 945 | (when rcirc-target |
a2524d26 EZ |
946 | (rcirc-remove-nick-channel (rcirc-buffer-process) |
947 | (rcirc-buffer-nick) | |
195eca78 SM |
948 | rcirc-target)))) |
949 | (setq rcirc-target nil))) | |
adf794e4 | 950 | |
adf794e4 EZ |
951 | (defun rcirc-generate-new-buffer-name (process target) |
952 | "Return a buffer name based on PROCESS and TARGET. | |
2e398771 | 953 | This is used for the initial name given to IRC buffers." |
195eca78 SM |
954 | (substring-no-properties |
955 | (if target | |
956 | (concat target "@" (process-name process)) | |
957 | (concat "*" (process-name process) "*")))) | |
bd43c990 | 958 | |
adf794e4 | 959 | (defun rcirc-get-buffer (process target &optional server) |
bd43c990 | 960 | "Return the buffer associated with the PROCESS and TARGET. |
adf794e4 | 961 | |
adf794e4 EZ |
962 | If optional argument SERVER is non-nil, return the server buffer |
963 | if there is no existing buffer for TARGET, otherwise return nil." | |
964 | (with-rcirc-process-buffer process | |
965 | (if (null target) | |
966 | (current-buffer) | |
967 | (let ((buffer (cdr (assoc-string target rcirc-buffer-alist t)))) | |
968 | (or buffer (when server (current-buffer))))))) | |
bd43c990 RS |
969 | |
970 | (defun rcirc-get-buffer-create (process target) | |
adf794e4 EZ |
971 | "Return the buffer associated with the PROCESS and TARGET. |
972 | Create the buffer if it doesn't exist." | |
973 | (let ((buffer (rcirc-get-buffer process target))) | |
a2524d26 | 974 | (if (and buffer (buffer-live-p buffer)) |
2fbed782 | 975 | (with-current-buffer buffer |
db58efbf | 976 | (when (not rcirc-target) |
2fbed782 | 977 | (setq rcirc-target target)) |
db58efbf | 978 | buffer) |
195eca78 SM |
979 | ;; create the buffer |
980 | (with-rcirc-process-buffer process | |
981 | (let ((new-buffer (get-buffer-create | |
982 | (rcirc-generate-new-buffer-name process target)))) | |
983 | (with-current-buffer new-buffer | |
984 | (rcirc-mode process target)) | |
985 | (rcirc-put-nick-channel process (rcirc-nick process) target) | |
986 | new-buffer))))) | |
bd43c990 RS |
987 | |
988 | (defun rcirc-send-input () | |
989 | "Send input to target associated with the current buffer." | |
990 | (interactive) | |
53f831f3 AS |
991 | (if (< (point) rcirc-prompt-end-marker) |
992 | ;; copy the line down to the input area | |
993 | (progn | |
994 | (forward-line 0) | |
995 | (let ((start (if (eq (point) (point-min)) | |
996 | (point) | |
997 | (if (get-text-property (1- (point)) 'hard) | |
998 | (point) | |
999 | (previous-single-property-change (point) 'hard)))) | |
1000 | (end (next-single-property-change (1+ (point)) 'hard))) | |
1001 | (goto-char (point-max)) | |
1002 | (insert (replace-regexp-in-string | |
1003 | "\n\\s-+" " " | |
1004 | (buffer-substring-no-properties start end))))) | |
1005 | ;; process input | |
1006 | (goto-char (point-max)) | |
a2524d26 EZ |
1007 | (when (not (equal 0 (- (point) rcirc-prompt-end-marker))) |
1008 | ;; delete a trailing newline | |
1009 | (when (eq (point) (point-at-bol)) | |
1010 | (delete-backward-char 1)) | |
1011 | (let ((input (buffer-substring-no-properties | |
1012 | rcirc-prompt-end-marker (point)))) | |
1013 | (dolist (line (split-string input "\n")) | |
1014 | (rcirc-process-input-line line)) | |
1015 | ;; add to input-ring | |
1016 | (save-excursion | |
1017 | (ring-insert rcirc-input-ring input) | |
1018 | (setq rcirc-input-ring-index 0)))))) | |
1019 | ||
195eca78 SM |
1020 | (defun rcirc-fill-paragraph (&optional arg) |
1021 | (interactive "p") | |
1022 | (when (> (point) rcirc-prompt-end-marker) | |
1023 | (save-restriction | |
1024 | (narrow-to-region rcirc-prompt-end-marker (point-max)) | |
1025 | (let ((fill-column rcirc-max-message-length)) | |
1026 | (fill-region (point-min) (point-max)))))) | |
1027 | ||
a2524d26 | 1028 | (defun rcirc-process-input-line (line) |
db58efbf EZ |
1029 | (if (string-match "^/\\([^ ]+\\) ?\\(.*\\)$" line) |
1030 | (rcirc-process-command (match-string 1 line) | |
1031 | (match-string 2 line) | |
1032 | line) | |
1033 | (rcirc-process-message line))) | |
1034 | ||
1035 | (defun rcirc-process-message (line) | |
1036 | (if (not rcirc-target) | |
a2524d26 | 1037 | (message "Not joined (no target)") |
db58efbf | 1038 | (delete-region rcirc-prompt-end-marker (point)) |
a2524d26 EZ |
1039 | (rcirc-send-message (rcirc-buffer-process) rcirc-target line) |
1040 | (setq rcirc-last-post-time (current-time)))) | |
db58efbf EZ |
1041 | |
1042 | (defun rcirc-process-command (command args line) | |
1043 | (if (eq (aref command 0) ?/) | |
1044 | ;; "//text" will send "/text" as a message | |
1045 | (rcirc-process-message (substring line 1)) | |
a2524d26 EZ |
1046 | (let ((fun (intern-soft (concat "rcirc-cmd-" command))) |
1047 | (process (rcirc-buffer-process))) | |
db58efbf EZ |
1048 | (newline) |
1049 | (with-current-buffer (current-buffer) | |
1050 | (delete-region rcirc-prompt-end-marker (point)) | |
1051 | (if (string= command "me") | |
a2524d26 | 1052 | (rcirc-print process (rcirc-buffer-nick) |
db58efbf | 1053 | "ACTION" rcirc-target args) |
a2524d26 | 1054 | (rcirc-print process (rcirc-buffer-nick) |
db58efbf EZ |
1055 | "COMMAND" rcirc-target line)) |
1056 | (set-marker rcirc-prompt-end-marker (point)) | |
1057 | (if (fboundp fun) | |
a2524d26 EZ |
1058 | (funcall fun args process rcirc-target) |
1059 | (rcirc-send-string process | |
f8db61b2 | 1060 | (concat command " :" args))))))) |
db58efbf | 1061 | |
bd43c990 RS |
1062 | (defvar rcirc-parent-buffer nil) |
1063 | (defvar rcirc-window-configuration nil) | |
1064 | (defun rcirc-edit-multiline () | |
1065 | "Move current edit to a dedicated buffer." | |
1066 | (interactive) | |
1067 | (let ((pos (1+ (- (point) rcirc-prompt-end-marker)))) | |
1068 | (goto-char (point-max)) | |
1069 | (let ((text (buffer-substring rcirc-prompt-end-marker (point))) | |
a2524d26 | 1070 | (parent (buffer-name))) |
bd43c990 RS |
1071 | (delete-region rcirc-prompt-end-marker (point)) |
1072 | (setq rcirc-window-configuration (current-window-configuration)) | |
1073 | (pop-to-buffer (concat "*multiline " parent "*")) | |
a2524d26 EZ |
1074 | (funcall rcirc-multiline-major-mode) |
1075 | (rcirc-multiline-minor-mode 1) | |
bd43c990 | 1076 | (setq rcirc-parent-buffer parent) |
bd43c990 | 1077 | (insert text) |
db58efbf EZ |
1078 | (and (> pos 0) (goto-char pos)) |
1079 | (message "Type C-c C-c to return text to %s, or C-c C-k to cancel" parent)))) | |
bd43c990 | 1080 | |
a2524d26 EZ |
1081 | (defvar rcirc-multiline-minor-mode-map (make-sparse-keymap) |
1082 | "Keymap for multiline mode in rcirc.") | |
92c4adc1 | 1083 | (define-key rcirc-multiline-minor-mode-map |
a2524d26 EZ |
1084 | (kbd "C-c C-c") 'rcirc-multiline-minor-submit) |
1085 | (define-key rcirc-multiline-minor-mode-map | |
1086 | (kbd "C-x C-s") 'rcirc-multiline-minor-submit) | |
1087 | (define-key rcirc-multiline-minor-mode-map | |
1088 | (kbd "C-c C-k") 'rcirc-multiline-minor-cancel) | |
1089 | (define-key rcirc-multiline-minor-mode-map | |
1090 | (kbd "ESC ESC ESC") 'rcirc-multiline-minor-cancel) | |
1091 | ||
1092 | (define-minor-mode rcirc-multiline-minor-mode | |
1093 | "Minor mode for editing multiple lines in rcirc." | |
1094 | :init-value nil | |
1095 | :lighter " rcirc-mline" | |
1096 | :keymap rcirc-multiline-minor-mode-map | |
1097 | :global nil | |
1098 | :group 'rcirc | |
bd43c990 | 1099 | (make-local-variable 'rcirc-parent-buffer) |
02f47e86 MB |
1100 | (put 'rcirc-parent-buffer 'permanent-local t) |
1101 | (setq fill-column rcirc-max-message-length)) | |
a2524d26 EZ |
1102 | |
1103 | (defun rcirc-multiline-minor-submit () | |
bd43c990 RS |
1104 | "Send the text in buffer back to parent buffer." |
1105 | (interactive) | |
adf794e4 | 1106 | (untabify (point-min) (point-max)) |
bd43c990 RS |
1107 | (let ((text (buffer-substring (point-min) (point-max))) |
1108 | (buffer (current-buffer)) | |
1109 | (pos (point))) | |
1110 | (set-buffer rcirc-parent-buffer) | |
1111 | (goto-char (point-max)) | |
1112 | (insert text) | |
bd43c990 | 1113 | (kill-buffer buffer) |
adf794e4 EZ |
1114 | (set-window-configuration rcirc-window-configuration) |
1115 | (goto-char (+ rcirc-prompt-end-marker (1- pos))))) | |
bd43c990 | 1116 | |
a2524d26 | 1117 | (defun rcirc-multiline-minor-cancel () |
bd43c990 RS |
1118 | "Cancel the multiline edit." |
1119 | (interactive) | |
bd43c990 RS |
1120 | (kill-buffer (current-buffer)) |
1121 | (set-window-configuration rcirc-window-configuration)) | |
1122 | ||
2fbed782 | 1123 | (defun rcirc-any-buffer (process) |
adf794e4 | 1124 | "Return a buffer for PROCESS, either the one selected or the process buffer." |
2fbed782 EZ |
1125 | (if rcirc-always-use-server-buffer-flag |
1126 | (process-buffer process) | |
1127 | (let ((buffer (window-buffer (selected-window)))) | |
1128 | (if (and buffer | |
1129 | (with-current-buffer buffer | |
1130 | (and (eq major-mode 'rcirc-mode) | |
a2524d26 | 1131 | (eq (rcirc-buffer-process) process)))) |
2fbed782 EZ |
1132 | buffer |
1133 | (process-buffer process))))) | |
bd43c990 | 1134 | |
324e4da7 | 1135 | (defcustom rcirc-response-formats |
195eca78 SM |
1136 | '(("PRIVMSG" . "<%N> %m") |
1137 | ("NOTICE" . "-%N- %m") | |
1138 | ("ACTION" . "[%N %m]") | |
1139 | ("COMMAND" . "%m") | |
1140 | ("ERROR" . "%fw!!! %m") | |
1141 | (t . "%fp*** %fs%n %r %m")) | |
324e4da7 MB |
1142 | "An alist of formats used for printing responses. |
1143 | The format is looked up using the response-type as a key; | |
1144 | if no match is found, the default entry (with a key of `t') is used. | |
1145 | ||
1146 | The entry's value part should be a string, which is inserted with | |
1147 | the of the following escape sequences replaced by the described values: | |
1148 | ||
1149 | %m The message text | |
2fbed782 EZ |
1150 | %n The sender's nick |
1151 | %N The sender's nick (with face `rcirc-my-nick' or `rcirc-other-nick') | |
324e4da7 | 1152 | %r The response-type |
324e4da7 MB |
1153 | %t The target |
1154 | %fw Following text uses the face `font-lock-warning-face' | |
1155 | %fp Following text uses the face `rcirc-server-prefix' | |
1156 | %fs Following text uses the face `rcirc-server' | |
1157 | %f[FACE] Following text uses the face FACE | |
3715419e | 1158 | %f- Following text uses the default face |
a2524d26 | 1159 | %% A literal `%' character" |
324e4da7 MB |
1160 | :type '(alist :key-type (choice (string :tag "Type") |
1161 | (const :tag "Default" t)) | |
1162 | :value-type string) | |
1163 | :group 'rcirc) | |
1164 | ||
195eca78 SM |
1165 | (defcustom rcirc-omit-responses |
1166 | '("JOIN" "PART" "QUIT") | |
1167 | "Responses which will be hidden when `rcirc-omit-mode' is enabled." | |
1168 | :type '(repeat string) | |
1169 | :group 'rcirc) | |
1170 | ||
bd43c990 | 1171 | (defun rcirc-format-response-string (process sender response target text) |
324e4da7 MB |
1172 | "Return a nicely-formatted response string, incorporating TEXT |
1173 | \(and perhaps other arguments). The specific formatting used | |
1174 | is found by looking up RESPONSE in `rcirc-response-formats'." | |
195eca78 SM |
1175 | (with-temp-buffer |
1176 | (insert (or (cdr (assoc response rcirc-response-formats)) | |
1177 | (cdr (assq t rcirc-response-formats)))) | |
1178 | (goto-char (point-min)) | |
1179 | (let ((start (point-min)) | |
1180 | (sender (if (or (not sender) | |
1181 | (string= (rcirc-server-name process) sender)) | |
1182 | "" | |
1183 | sender)) | |
1184 | face) | |
1185 | (while (re-search-forward "%\\(\\(f\\(.\\)\\)\\|\\(.\\)\\)" nil t) | |
1186 | (rcirc-add-face start (match-beginning 0) face) | |
1187 | (setq start (match-beginning 0)) | |
1188 | (replace-match | |
1189 | (case (aref (match-string 1) 0) | |
1190 | (?f (setq face | |
1191 | (case (string-to-char (match-string 3)) | |
1192 | (?w 'font-lock-warning-face) | |
1193 | (?p 'rcirc-server-prefix) | |
1194 | (?s 'rcirc-server) | |
1195 | (t nil))) | |
1196 | "") | |
1197 | (?n sender) | |
1198 | (?N (let ((my-nick (rcirc-nick process))) | |
1199 | (save-match-data | |
1200 | (with-syntax-table rcirc-nick-syntax-table | |
1201 | (rcirc-facify sender | |
1202 | (cond ((string= sender my-nick) | |
1203 | 'rcirc-my-nick) | |
1204 | ((and rcirc-bright-nicks | |
1205 | (string-match | |
1206 | (regexp-opt rcirc-bright-nicks | |
1207 | 'words) | |
1208 | sender)) | |
1209 | 'rcirc-bright-nick) | |
1210 | ((and rcirc-dim-nicks | |
1211 | (string-match | |
1212 | (regexp-opt rcirc-dim-nicks | |
1213 | 'words) | |
1214 | sender)) | |
1215 | 'rcirc-dim-nick) | |
1216 | (t | |
1217 | 'rcirc-other-nick))))))) | |
1218 | (?m (propertize text 'rcirc-text text)) | |
1219 | (?r response) | |
1220 | (?t (or target "")) | |
1221 | (t (concat "UNKNOWN CODE:" (match-string 0)))) | |
1222 | t t nil 0) | |
1223 | (rcirc-add-face (match-beginning 0) (match-end 0) face)) | |
1224 | (rcirc-add-face start (match-beginning 0) face)) | |
1225 | (buffer-substring (point-min) (point-max)))) | |
bd43c990 | 1226 | |
db58efbf EZ |
1227 | (defun rcirc-target-buffer (process sender response target text) |
1228 | "Return a buffer to print the server response." | |
1229 | (assert (not (bufferp target))) | |
1230 | (with-rcirc-process-buffer process | |
1231 | (cond ((not target) | |
2fbed782 | 1232 | (rcirc-any-buffer process)) |
db58efbf EZ |
1233 | ((not (rcirc-channel-p target)) |
1234 | ;; message from another user | |
195eca78 SM |
1235 | (if (or (string= response "PRIVMSG") |
1236 | (string= response "ACTION")) | |
db58efbf EZ |
1237 | (rcirc-get-buffer-create process (if (string= sender rcirc-nick) |
1238 | target | |
1239 | sender)) | |
1240 | (rcirc-get-buffer process target t))) | |
1241 | ((or (rcirc-get-buffer process target) | |
2fbed782 | 1242 | (rcirc-any-buffer process)))))) |
db58efbf | 1243 | |
f8db61b2 EZ |
1244 | (defvar rcirc-activity-types nil) |
1245 | (make-variable-buffer-local 'rcirc-activity-types) | |
a2524d26 EZ |
1246 | (defvar rcirc-last-sender nil) |
1247 | (make-variable-buffer-local 'rcirc-last-sender) | |
7faa3f8c | 1248 | |
195eca78 SM |
1249 | (defcustom rcirc-log-directory "~/.emacs.d/rcirc-log" |
1250 | "Directory to keep IRC logfiles." | |
1251 | :type 'directory | |
1252 | :group 'rcirc) | |
1253 | ||
1254 | (defcustom rcirc-log-flag nil | |
1255 | "Non-nil means log IRC activity to disk. | |
1256 | Logfiles are kept in `rcirc-log-directory'." | |
1257 | :type 'boolean | |
1258 | :group 'rcirc) | |
1259 | ||
bd43c990 RS |
1260 | (defun rcirc-print (process sender response target text &optional activity) |
1261 | "Print TEXT in the buffer associated with TARGET. | |
1262 | Format based on SENDER and RESPONSE. If ACTIVITY is non-nil, | |
1263 | record activity." | |
a2524d26 | 1264 | (or text (setq text "")) |
db58efbf | 1265 | (unless (or (member sender rcirc-ignore-list) |
2c8abe90 | 1266 | (member (with-syntax-table rcirc-nick-syntax-table |
02f47e86 MB |
1267 | (when (string-match "^\\([^/]\\w*\\)[:,]" text) |
1268 | (match-string 1 text))) | |
1269 | rcirc-ignore-list)) | |
db58efbf | 1270 | (let* ((buffer (rcirc-target-buffer process sender response target text)) |
2c8abe90 AS |
1271 | (inhibit-read-only t)) |
1272 | (with-current-buffer buffer | |
1273 | (let ((moving (= (point) rcirc-prompt-end-marker)) | |
1274 | (old-point (point-marker)) | |
1275 | (fill-start (marker-position rcirc-prompt-start-marker))) | |
1276 | ||
1277 | (unless (string= sender (rcirc-nick process)) | |
1278 | ;; only decode text from other senders, not ours | |
a2524d26 | 1279 | (setq text (decode-coding-string text rcirc-decode-coding-system)) |
2c8abe90 AS |
1280 | ;; mark the line with overlay arrow |
1281 | (unless (or (marker-position overlay-arrow-position) | |
195eca78 SM |
1282 | (get-buffer-window (current-buffer)) |
1283 | (member response rcirc-omit-responses)) | |
2c8abe90 AS |
1284 | (set-marker overlay-arrow-position |
1285 | (marker-position rcirc-prompt-start-marker)))) | |
1286 | ||
1287 | ;; temporarily set the marker insertion-type because | |
1288 | ;; insert-before-markers results in hidden text in new buffers | |
1289 | (goto-char rcirc-prompt-start-marker) | |
1290 | (set-marker-insertion-type rcirc-prompt-start-marker t) | |
1291 | (set-marker-insertion-type rcirc-prompt-end-marker t) | |
324e4da7 | 1292 | |
195eca78 SM |
1293 | (let ((start (point))) |
1294 | (insert (rcirc-format-response-string process sender response nil | |
1295 | text) | |
1296 | (propertize "\n" 'hard t)) | |
1297 | ||
1298 | ;; squeeze spaces out of text before rcirc-text | |
1299 | (fill-region fill-start | |
1300 | (1- (or (next-single-property-change fill-start | |
1301 | 'rcirc-text) | |
1302 | rcirc-prompt-end-marker))) | |
1303 | ||
1304 | ;; run markup functions | |
1305 | (save-excursion | |
1306 | (save-restriction | |
1307 | (narrow-to-region start rcirc-prompt-start-marker) | |
1308 | (goto-char (or (next-single-property-change start 'rcirc-text) | |
1309 | (point))) | |
1310 | (when (rcirc-buffer-process) | |
1311 | (save-excursion (rcirc-markup-timestamp sender response)) | |
1312 | (dolist (fn rcirc-markup-text-functions) | |
1313 | (save-excursion (funcall fn sender response))) | |
1314 | (save-excursion (rcirc-markup-fill sender response))) | |
1315 | ||
1316 | (when rcirc-read-only-flag | |
1317 | (add-text-properties (point-min) (point-max) | |
1318 | '(read-only t front-sticky t)))) | |
1319 | ;; make text omittable | |
1320 | (when (and (member response rcirc-omit-responses) | |
1321 | (> start (point-min))) | |
1322 | (put-text-property (1- start) (1- rcirc-prompt-start-marker) | |
1323 | 'invisible 'rcirc-omit)))) | |
1324 | ||
1325 | (set-marker-insertion-type rcirc-prompt-start-marker nil) | |
1326 | (set-marker-insertion-type rcirc-prompt-end-marker nil) | |
2c8abe90 AS |
1327 | |
1328 | ;; truncate buffer if it is very long | |
1329 | (save-excursion | |
1330 | (when (and rcirc-buffer-maximum-lines | |
1331 | (> rcirc-buffer-maximum-lines 0) | |
1332 | (= (forward-line (- rcirc-buffer-maximum-lines)) 0)) | |
1333 | (delete-region (point-min) (point)))) | |
1334 | ||
1335 | ;; set the window point for buffers show in windows | |
1336 | (walk-windows (lambda (w) | |
d40ac716 CY |
1337 | (when (and (not (eq (selected-window) w)) |
1338 | (eq (current-buffer) | |
1339 | (window-buffer w)) | |
1340 | (>= (window-point w) | |
1341 | rcirc-prompt-end-marker)) | |
195eca78 | 1342 | (set-window-point w (point-max)))) |
2c8abe90 AS |
1343 | nil t) |
1344 | ||
1345 | ;; restore the point | |
1346 | (goto-char (if moving rcirc-prompt-end-marker old-point)) | |
1347 | ||
195eca78 | 1348 | ;; keep window on bottom line if it was already there |
d40ac716 CY |
1349 | (when rcirc-scroll-show-maximum-output |
1350 | (walk-windows (lambda (w) | |
1351 | (when (eq (window-buffer w) (current-buffer)) | |
1352 | (with-current-buffer (window-buffer w) | |
1353 | (when (eq major-mode 'rcirc-mode) | |
1354 | (with-selected-window w | |
195eca78 SM |
1355 | (when (<= (- (window-height) |
1356 | (count-screen-lines (window-point) | |
1357 | (window-start)) | |
d40ac716 CY |
1358 | 1) |
1359 | 0) | |
1360 | (recenter -1))))))) | |
195eca78 | 1361 | nil t)) |
d40ac716 | 1362 | |
2c8abe90 AS |
1363 | ;; flush undo (can we do something smarter here?) |
1364 | (buffer-disable-undo) | |
1365 | (buffer-enable-undo)) | |
1366 | ||
1367 | ;; record modeline activity | |
f8db61b2 EZ |
1368 | (when (and activity |
1369 | (not rcirc-ignore-buffer-activity-flag) | |
1370 | (not (and rcirc-dim-nicks sender | |
195eca78 SM |
1371 | (string-match (regexp-opt rcirc-dim-nicks) sender) |
1372 | (rcirc-channel-p target)))) | |
f8db61b2 EZ |
1373 | (rcirc-record-activity (current-buffer) |
1374 | (when (not (rcirc-channel-p rcirc-target)) | |
1375 | 'nick))) | |
2c8abe90 | 1376 | |
195eca78 SM |
1377 | (when rcirc-log-flag |
1378 | (rcirc-log process sender response target text)) | |
1379 | ||
2c8abe90 AS |
1380 | (sit-for 0) ; displayed text before hook |
1381 | (run-hook-with-args 'rcirc-print-hooks | |
1382 | process sender response target text))))) | |
bd43c990 | 1383 | |
195eca78 SM |
1384 | (defun rcirc-log (process sender response target text) |
1385 | "Record line in `rcirc-log', to be later written to disk." | |
1386 | (let* ((filename (rcirc-generate-new-buffer-name process target)) | |
1387 | (cell (assoc-string filename rcirc-log-alist)) | |
1388 | (line (concat (format-time-string rcirc-time-format) | |
1389 | (substring-no-properties | |
1390 | (rcirc-format-response-string process sender | |
1391 | response target text)) | |
1392 | "\n"))) | |
1393 | (if cell | |
1394 | (setcdr cell (concat (cdr cell) line)) | |
1395 | (setq rcirc-log-alist | |
1396 | (cons (cons filename line) rcirc-log-alist))))) | |
1397 | ||
1398 | (defun rcirc-log-write () | |
1399 | "Flush `rcirc-log-alist' data to disk. | |
1400 | ||
1401 | Log data is written to `rcirc-log-directory'." | |
1402 | (make-directory rcirc-log-directory t) | |
1403 | (dolist (cell rcirc-log-alist) | |
1404 | (with-temp-buffer | |
1405 | (insert (cdr cell)) | |
1406 | (write-region (point-min) (point-max) | |
1407 | (concat rcirc-log-directory "/" (car cell)) | |
1408 | t 'quiet))) | |
1409 | (setq rcirc-log-alist nil)) | |
bd43c990 RS |
1410 | |
1411 | (defun rcirc-join-channels (process channels) | |
1412 | "Join CHANNELS." | |
1413 | (save-window-excursion | |
db58efbf EZ |
1414 | (dolist (channel channels) |
1415 | (with-rcirc-process-buffer process | |
1416 | (rcirc-cmd-join channel process))))) | |
bd43c990 RS |
1417 | \f |
1418 | ;;; nick management | |
8216fbaf | 1419 | (defvar rcirc-nick-prefix-chars "~&@%+") |
bd43c990 RS |
1420 | (defun rcirc-user-nick (user) |
1421 | "Return the nick from USER. Remove any non-nick junk." | |
db58efbf | 1422 | (save-match-data |
8216fbaf EZ |
1423 | (if (string-match (concat "^[" rcirc-nick-prefix-chars |
1424 | "]?\\([^! ]+\\)!?") (or user "")) | |
db58efbf EZ |
1425 | (match-string 1 user) |
1426 | user))) | |
bd43c990 | 1427 | |
bd43c990 RS |
1428 | (defun rcirc-nick-channels (process nick) |
1429 | "Return list of channels for NICK." | |
db58efbf EZ |
1430 | (with-rcirc-process-buffer process |
1431 | (mapcar (lambda (x) (car x)) | |
1432 | (gethash nick rcirc-nick-table)))) | |
bd43c990 RS |
1433 | |
1434 | (defun rcirc-put-nick-channel (process nick channel) | |
1435 | "Add CHANNEL to list associated with NICK." | |
2fbed782 EZ |
1436 | (let ((nick (rcirc-user-nick nick))) |
1437 | (with-rcirc-process-buffer process | |
1438 | (let* ((chans (gethash nick rcirc-nick-table)) | |
1439 | (record (assoc-string channel chans t))) | |
1440 | (if record | |
1441 | (setcdr record (current-time)) | |
1442 | (puthash nick (cons (cons channel (current-time)) | |
1443 | chans) | |
1444 | rcirc-nick-table)))))) | |
bd43c990 RS |
1445 | |
1446 | (defun rcirc-nick-remove (process nick) | |
1447 | "Remove NICK from table." | |
adf794e4 | 1448 | (with-rcirc-process-buffer process |
bd43c990 RS |
1449 | (remhash nick rcirc-nick-table))) |
1450 | ||
1451 | (defun rcirc-remove-nick-channel (process nick channel) | |
1452 | "Remove the CHANNEL from list associated with NICK." | |
adf794e4 | 1453 | (with-rcirc-process-buffer process |
db58efbf | 1454 | (let* ((chans (gethash nick rcirc-nick-table)) |
adf794e4 EZ |
1455 | (newchans |
1456 | ;; instead of assoc-string-delete-all: | |
1457 | (let ((record (assoc-string channel chans t))) | |
1458 | (when record | |
1459 | (setcar record 'delete) | |
1460 | (assq-delete-all 'delete chans))))) | |
bd43c990 RS |
1461 | (if newchans |
1462 | (puthash nick newchans rcirc-nick-table) | |
1463 | (remhash nick rcirc-nick-table))))) | |
1464 | ||
a2524d26 EZ |
1465 | (defun rcirc-channel-nicks (process target) |
1466 | "Return the list of nicks associated with TARGET sorted by last activity." | |
1467 | (when target | |
1468 | (if (rcirc-channel-p target) | |
1469 | (with-rcirc-process-buffer process | |
1470 | (let (nicks) | |
1471 | (maphash | |
1472 | (lambda (k v) | |
1473 | (let ((record (assoc-string target v t))) | |
1474 | (if record | |
1475 | (setq nicks (cons (cons k (cdr record)) nicks))))) | |
1476 | rcirc-nick-table) | |
1477 | (mapcar (lambda (x) (car x)) | |
1478 | (sort nicks (lambda (x y) (time-less-p (cdr y) (cdr x))))))) | |
1479 | (list target)))) | |
2c8abe90 AS |
1480 | |
1481 | (defun rcirc-ignore-update-automatic (nick) | |
2e398771 JB |
1482 | "Remove NICK from `rcirc-ignore-list' |
1483 | if NICK is also on `rcirc-ignore-list-automatic'." | |
2c8abe90 AS |
1484 | (when (member nick rcirc-ignore-list-automatic) |
1485 | (setq rcirc-ignore-list-automatic | |
1486 | (delete nick rcirc-ignore-list-automatic) | |
1487 | rcirc-ignore-list | |
1488 | (delete nick rcirc-ignore-list)))) | |
bd43c990 RS |
1489 | \f |
1490 | ;;; activity tracking | |
db58efbf EZ |
1491 | (defvar rcirc-track-minor-mode-map (make-sparse-keymap) |
1492 | "Keymap for rcirc track minor mode.") | |
1493 | ||
1494 | (define-key rcirc-track-minor-mode-map (kbd "C-c `") 'rcirc-next-active-buffer) | |
1495 | (define-key rcirc-track-minor-mode-map (kbd "C-c C-@") 'rcirc-next-active-buffer) | |
1496 | (define-key rcirc-track-minor-mode-map (kbd "C-c C-SPC") 'rcirc-next-active-buffer) | |
1497 | ||
e8f10ddb | 1498 | ;;;###autoload |
db58efbf EZ |
1499 | (define-minor-mode rcirc-track-minor-mode |
1500 | "Global minor mode for tracking activity in rcirc buffers." | |
1501 | :init-value nil | |
1502 | :lighter "" | |
1503 | :keymap rcirc-track-minor-mode-map | |
1504 | :global t | |
1505 | :group 'rcirc | |
1506 | (or global-mode-string (setq global-mode-string '(""))) | |
1507 | ;; toggle the mode-line channel indicator | |
1508 | (if rcirc-track-minor-mode | |
a2524d26 EZ |
1509 | (progn |
1510 | (and (not (memq 'rcirc-activity-string global-mode-string)) | |
1511 | (setq global-mode-string | |
1512 | (append global-mode-string '(rcirc-activity-string)))) | |
1513 | (add-hook 'window-configuration-change-hook | |
1514 | 'rcirc-window-configuration-change)) | |
92c4adc1 | 1515 | (setq global-mode-string |
a2524d26 EZ |
1516 | (delete 'rcirc-activity-string global-mode-string)) |
1517 | (remove-hook 'window-configuration-change-hook | |
1518 | 'rcirc-window-configuration-change))) | |
db58efbf | 1519 | |
adf794e4 | 1520 | (or (assq 'rcirc-ignore-buffer-activity-flag minor-mode-alist) |
bd43c990 | 1521 | (setq minor-mode-alist |
adf794e4 | 1522 | (cons '(rcirc-ignore-buffer-activity-flag " Ignore") minor-mode-alist))) |
a2524d26 EZ |
1523 | (or (assq 'rcirc-low-priority-flag minor-mode-alist) |
1524 | (setq minor-mode-alist | |
1525 | (cons '(rcirc-low-priority-flag " LowPri") minor-mode-alist))) | |
195eca78 SM |
1526 | (or (assq 'rcirc-omit-mode minor-mode-alist) |
1527 | (setq minor-mode-alist | |
1528 | (cons '(rcirc-omit-mode " Omit") minor-mode-alist))) | |
bd43c990 | 1529 | |
db58efbf EZ |
1530 | (defun rcirc-toggle-ignore-buffer-activity () |
1531 | "Toggle the value of `rcirc-ignore-buffer-activity-flag'." | |
1532 | (interactive) | |
1533 | (setq rcirc-ignore-buffer-activity-flag | |
1534 | (not rcirc-ignore-buffer-activity-flag)) | |
1535 | (message (if rcirc-ignore-buffer-activity-flag | |
1536 | "Ignore activity in this buffer" | |
1537 | "Notice activity in this buffer")) | |
bd43c990 RS |
1538 | (force-mode-line-update)) |
1539 | ||
a2524d26 | 1540 | (defun rcirc-toggle-low-priority () |
02f47e86 | 1541 | "Toggle the value of `rcirc-low-priority-flag'." |
a2524d26 EZ |
1542 | (interactive) |
1543 | (setq rcirc-low-priority-flag | |
1544 | (not rcirc-low-priority-flag)) | |
1545 | (message (if rcirc-low-priority-flag | |
1546 | "Activity in this buffer is low priority" | |
1547 | "Activity in this buffer is normal priority")) | |
1548 | (force-mode-line-update)) | |
1549 | ||
195eca78 SM |
1550 | (defun rcirc-omit-mode () |
1551 | "Toggle the Rcirc-Omit mode. | |
1552 | If enabled, \"uninteresting\" lines are not shown. | |
1553 | Uninteresting lines are those whose responses are listed in | |
1554 | `rcirc-omit-responses'." | |
1555 | (interactive) | |
1556 | (setq rcirc-omit-mode (not rcirc-omit-mode)) | |
1557 | (let ((line (1- (count-screen-lines (point) (window-start))))) | |
1558 | (if rcirc-omit-mode | |
1559 | (progn | |
1560 | (add-to-invisibility-spec 'rcirc-omit) | |
1561 | (message "Rcirc-Omit mode enabled")) | |
1562 | (remove-from-invisibility-spec 'rcirc-omit) | |
1563 | (message "Rcirc-Omit mode disabled")) | |
1564 | (recenter line)) | |
1565 | (force-mode-line-update)) | |
bd43c990 RS |
1566 | |
1567 | (defun rcirc-switch-to-server-buffer () | |
1568 | "Switch to the server buffer associated with current channel buffer." | |
1569 | (interactive) | |
195eca78 | 1570 | (switch-to-buffer rcirc-server-buffer)) |
bd43c990 RS |
1571 | |
1572 | (defun rcirc-jump-to-first-unread-line () | |
1573 | "Move the point to the first unread line in this buffer." | |
1574 | (interactive) | |
195eca78 SM |
1575 | (if (marker-position overlay-arrow-position) |
1576 | (goto-char overlay-arrow-position) | |
1577 | (message "No unread messages"))) | |
1578 | ||
1579 | (defun rcirc-non-irc-buffer () | |
1580 | (let ((buflist (buffer-list)) | |
1581 | buffer) | |
1582 | (while (and buflist (not buffer)) | |
1583 | (with-current-buffer (car buflist) | |
1584 | (unless (or (eq major-mode 'rcirc-mode) | |
1585 | (= ?\s (aref (buffer-name) 0)) ; internal buffers | |
1586 | (get-buffer-window (current-buffer))) | |
1587 | (setq buffer (current-buffer)))) | |
1588 | (setq buflist (cdr buflist))) | |
1589 | buffer)) | |
bd43c990 RS |
1590 | |
1591 | (defun rcirc-next-active-buffer (arg) | |
195eca78 SM |
1592 | "Switch to the next rcirc buffer with activity. |
1593 | With prefix ARG, go to the next low priority buffer with activity." | |
a2524d26 EZ |
1594 | (interactive "P") |
1595 | (let* ((pair (rcirc-split-activity rcirc-activity)) | |
1596 | (lopri (car pair)) | |
92c4adc1 | 1597 | (hipri (cdr pair))) |
a2524d26 EZ |
1598 | (if (or (and (not arg) hipri) |
1599 | (and arg lopri)) | |
195eca78 | 1600 | (switch-to-buffer (car (if arg lopri hipri)) t) |
a2524d26 | 1601 | (if (eq major-mode 'rcirc-mode) |
195eca78 | 1602 | (switch-to-buffer (rcirc-non-irc-buffer)) |
92c4adc1 | 1603 | (message (concat |
a2524d26 EZ |
1604 | "No IRC activity." |
1605 | (when lopri | |
92c4adc1 | 1606 | (concat |
a2524d26 EZ |
1607 | " Type C-u " |
1608 | (key-description (this-command-keys)) | |
1609 | " for low priority activity.")))))))) | |
bd43c990 RS |
1610 | |
1611 | (defvar rcirc-activity-hooks nil | |
1612 | "Hook to be run when there is channel activity. | |
1613 | ||
1614 | Functions are called with a single argument, the buffer with the | |
1615 | activity. Only run if the buffer is not visible and | |
adf794e4 | 1616 | `rcirc-ignore-buffer-activity-flag' is non-nil.") |
bd43c990 | 1617 | |
a2524d26 | 1618 | (defun rcirc-record-activity (buffer &optional type) |
bd43c990 RS |
1619 | "Record BUFFER activity with TYPE." |
1620 | (with-current-buffer buffer | |
195eca78 SM |
1621 | (let ((old-activity rcirc-activity) |
1622 | (old-types rcirc-activity-types)) | |
1623 | (when (not (get-buffer-window (current-buffer) t)) | |
1624 | (setq rcirc-activity | |
1625 | (sort (add-to-list 'rcirc-activity (current-buffer)) | |
1626 | (lambda (b1 b2) | |
1627 | (let ((t1 (with-current-buffer b1 rcirc-last-post-time)) | |
1628 | (t2 (with-current-buffer b2 rcirc-last-post-time))) | |
1629 | (time-less-p t2 t1))))) | |
1630 | (pushnew type rcirc-activity-types) | |
1631 | (unless (and (equal rcirc-activity old-activity) | |
1632 | (member type old-types)) | |
1633 | (rcirc-update-activity-string))))) | |
bd43c990 RS |
1634 | (run-hook-with-args 'rcirc-activity-hooks buffer)) |
1635 | ||
1636 | (defun rcirc-clear-activity (buffer) | |
1637 | "Clear the BUFFER activity." | |
1638 | (setq rcirc-activity (delete buffer rcirc-activity)) | |
1639 | (with-current-buffer buffer | |
f8db61b2 | 1640 | (setq rcirc-activity-types nil))) |
bd43c990 | 1641 | |
195eca78 SM |
1642 | (defun rcirc-clear-unread (buffer) |
1643 | "Erase the last read message arrow from BUFFER." | |
1644 | (when (buffer-live-p buffer) | |
1645 | (with-current-buffer buffer | |
1646 | (set-marker overlay-arrow-position nil)))) | |
1647 | ||
a2524d26 EZ |
1648 | (defun rcirc-split-activity (activity) |
1649 | "Return a cons cell with ACTIVITY split into (lopri . hipri)." | |
1650 | (let (lopri hipri) | |
1651 | (dolist (buf rcirc-activity) | |
1652 | (with-current-buffer buf | |
1653 | (if (and rcirc-low-priority-flag | |
f8db61b2 | 1654 | (not (member 'nick rcirc-activity-types))) |
a2524d26 EZ |
1655 | (add-to-list 'lopri buf t) |
1656 | (add-to-list 'hipri buf t)))) | |
1657 | (cons lopri hipri))) | |
1658 | ||
195eca78 SM |
1659 | (defvar rcirc-update-activity-string-hook nil |
1660 | "Hook run whenever the activity string is updated.") | |
1661 | ||
adf794e4 | 1662 | ;; TODO: add mouse properties |
bd43c990 RS |
1663 | (defun rcirc-update-activity-string () |
1664 | "Update mode-line string." | |
a2524d26 EZ |
1665 | (let* ((pair (rcirc-split-activity rcirc-activity)) |
1666 | (lopri (car pair)) | |
1667 | (hipri (cdr pair))) | |
1668 | (setq rcirc-activity-string | |
7faa3f8c | 1669 | (cond ((or hipri lopri) |
195eca78 | 1670 | (concat (and hipri "[") |
7faa3f8c MB |
1671 | (rcirc-activity-string hipri) |
1672 | (and hipri lopri ",") | |
1673 | (and lopri | |
1674 | (concat "(" | |
1675 | (rcirc-activity-string lopri) | |
1676 | ")")) | |
195eca78 | 1677 | (and hipri "]"))) |
7faa3f8c | 1678 | ((not (null (rcirc-process-list))) |
195eca78 SM |
1679 | "[]") |
1680 | (t "[]"))) | |
1681 | (run-hooks 'rcirc-update-activity-string-hook))) | |
a2524d26 EZ |
1682 | |
1683 | (defun rcirc-activity-string (buffers) | |
1684 | (mapconcat (lambda (b) | |
f8db61b2 | 1685 | (let ((s (substring-no-properties (rcirc-short-buffer-name b)))) |
a2524d26 | 1686 | (with-current-buffer b |
f8db61b2 EZ |
1687 | (dolist (type rcirc-activity-types) |
1688 | (rcirc-add-face 0 (length s) | |
1689 | (case type | |
82741a5e CY |
1690 | (nick 'rcirc-track-nick) |
1691 | (keyword 'rcirc-track-keyword)) | |
f8db61b2 EZ |
1692 | s))) |
1693 | s)) | |
a2524d26 | 1694 | buffers ",")) |
bd43c990 RS |
1695 | |
1696 | (defun rcirc-short-buffer-name (buffer) | |
1697 | "Return a short name for BUFFER to use in the modeline indicator." | |
1698 | (with-current-buffer buffer | |
adf794e4 EZ |
1699 | (or rcirc-short-buffer-name (buffer-name)))) |
1700 | ||
195eca78 SM |
1701 | (defun rcirc-visible-buffers () |
1702 | "Return a list of the visible buffers that are in rcirc-mode." | |
1703 | (let (acc) | |
adf794e4 | 1704 | (walk-windows (lambda (w) |
195eca78 SM |
1705 | (with-current-buffer (window-buffer w) |
1706 | (when (eq major-mode 'rcirc-mode) | |
1707 | (push (current-buffer) acc))))) | |
1708 | acc)) | |
1709 | ||
1710 | (defvar rcirc-visible-buffers nil) | |
1711 | (defun rcirc-window-configuration-change () | |
1712 | (unless (minibuffer-window-active-p (minibuffer-window)) | |
1713 | ;; delay this until command has finished to make sure window is | |
1714 | ;; actually visible before clearing activity | |
1715 | (add-hook 'post-command-hook 'rcirc-window-configuration-change-1))) | |
1716 | ||
1717 | (defun rcirc-window-configuration-change-1 () | |
1718 | ;; clear activity and overlay arrows | |
1719 | (let* ((old-activity rcirc-activity) | |
1720 | (hidden-buffers rcirc-visible-buffers)) | |
1721 | ||
1722 | (setq rcirc-visible-buffers (rcirc-visible-buffers)) | |
1723 | ||
1724 | (dolist (vbuf rcirc-visible-buffers) | |
1725 | (setq hidden-buffers (delq vbuf hidden-buffers)) | |
1726 | ;; clear activity for all visible buffers | |
1727 | (rcirc-clear-activity vbuf)) | |
1728 | ||
1729 | ;; clear unread arrow from recently hidden buffers | |
1730 | (dolist (hbuf hidden-buffers) | |
1731 | (rcirc-clear-unread hbuf)) | |
1732 | ||
1733 | ;; remove any killed buffers from list | |
1734 | (setq rcirc-activity | |
1735 | (delq nil (mapcar (lambda (buf) (when (buffer-live-p buf) buf)) | |
1736 | rcirc-activity))) | |
1737 | ;; update the mode-line string | |
1738 | (unless (equal old-activity rcirc-activity) | |
1739 | (rcirc-update-activity-string))) | |
1740 | ||
1741 | (remove-hook 'post-command-hook 'rcirc-window-configuration-change-1)) | |
bd43c990 RS |
1742 | |
1743 | \f | |
adf794e4 EZ |
1744 | ;;; buffer name abbreviation |
1745 | (defun rcirc-update-short-buffer-names () | |
1746 | (let ((bufalist | |
1747 | (apply 'append (mapcar (lambda (process) | |
1748 | (with-rcirc-process-buffer process | |
1749 | rcirc-buffer-alist)) | |
1750 | (rcirc-process-list))))) | |
1751 | (dolist (i (rcirc-abbreviate bufalist)) | |
a2524d26 EZ |
1752 | (when (buffer-live-p (cdr i)) |
1753 | (with-current-buffer (cdr i) | |
1754 | (setq rcirc-short-buffer-name (car i))))))) | |
adf794e4 EZ |
1755 | |
1756 | (defun rcirc-abbreviate (pairs) | |
1757 | (apply 'append (mapcar 'rcirc-rebuild-tree (rcirc-make-trees pairs)))) | |
1758 | ||
1759 | (defun rcirc-rebuild-tree (tree &optional acc) | |
1760 | (let ((ch (char-to-string (car tree)))) | |
1761 | (dolist (x (cdr tree)) | |
1762 | (if (listp x) | |
1763 | (setq acc (append acc | |
1764 | (mapcar (lambda (y) | |
1765 | (cons (concat ch (car y)) | |
1766 | (cdr y))) | |
1767 | (rcirc-rebuild-tree x)))) | |
1768 | (setq acc (cons (cons ch x) acc)))) | |
1769 | acc)) | |
1770 | ||
1771 | (defun rcirc-make-trees (pairs) | |
1772 | (let (alist) | |
1773 | (mapc (lambda (pair) | |
1774 | (if (consp pair) | |
1775 | (let* ((str (car pair)) | |
1776 | (data (cdr pair)) | |
1777 | (char (unless (zerop (length str)) | |
1778 | (aref str 0))) | |
1779 | (rest (unless (zerop (length str)) | |
1780 | (substring str 1))) | |
1781 | (part (if char (assq char alist)))) | |
1782 | (if part | |
1783 | ;; existing partition | |
1784 | (setcdr part (cons (cons rest data) (cdr part))) | |
1785 | ;; new partition | |
1786 | (setq alist (cons (if char | |
1787 | (list char (cons rest data)) | |
1788 | data) | |
1789 | alist)))) | |
1790 | (setq alist (cons pair alist)))) | |
1791 | pairs) | |
1792 | ;; recurse into cdrs of alist | |
1793 | (mapc (lambda (x) | |
1794 | (when (and (listp x) (listp (cadr x))) | |
1795 | (setcdr x (if (> (length (cdr x)) 1) | |
1796 | (rcirc-make-trees (cdr x)) | |
1797 | (setcdr x (list (cdadr x))))))) | |
1798 | alist))) | |
1799 | \f | |
bd43c990 RS |
1800 | ;;; /commands these are called with 3 args: PROCESS, TARGET, which is |
1801 | ;; the current buffer/channel/user, and ARGS, which is a string | |
1802 | ;; containing the text following the /cmd. | |
1803 | ||
adf794e4 | 1804 | (defmacro defun-rcirc-command (command argument docstring interactive-form |
bd43c990 RS |
1805 | &rest body) |
1806 | "Define a command." | |
1807 | `(defun ,(intern (concat "rcirc-cmd-" (symbol-name command))) | |
1808 | (,@argument &optional process target) | |
a2524d26 EZ |
1809 | ,(concat docstring "\n\nNote: If PROCESS or TARGET are nil, the values given" |
1810 | "\nby `rcirc-buffer-process' and `rcirc-target' will be used.") | |
bd43c990 | 1811 | ,interactive-form |
a2524d26 | 1812 | (let ((process (or process (rcirc-buffer-process))) |
bd43c990 RS |
1813 | (target (or target rcirc-target))) |
1814 | ,@body))) | |
1815 | ||
1816 | (defun-rcirc-command msg (message) | |
1817 | "Send private MESSAGE to TARGET." | |
1818 | (interactive "i") | |
1819 | (if (null message) | |
1820 | (progn | |
1821 | (setq target (completing-read "Message nick: " | |
92c4adc1 | 1822 | (with-rcirc-server-buffer |
a2524d26 | 1823 | rcirc-nick-table))) |
bd43c990 RS |
1824 | (when (> (length target) 0) |
1825 | (setq message (read-string (format "Message %s: " target))) | |
1826 | (when (> (length message) 0) | |
1827 | (rcirc-send-message process target message)))) | |
1828 | (if (not (string-match "\\([^ ]+\\) \\(.+\\)" message)) | |
1829 | (message "Not enough args, or something.") | |
1830 | (setq target (match-string 1 message) | |
1831 | message (match-string 2 message)) | |
1832 | (rcirc-send-message process target message)))) | |
1833 | ||
1834 | (defun-rcirc-command query (nick) | |
1835 | "Open a private chat buffer to NICK." | |
1836 | (interactive (list (completing-read "Query nick: " | |
a2524d26 | 1837 | (with-rcirc-server-buffer rcirc-nick-table)))) |
adf794e4 EZ |
1838 | (let ((existing-buffer (rcirc-get-buffer process nick))) |
1839 | (switch-to-buffer (or existing-buffer | |
1840 | (rcirc-get-buffer-create process nick))) | |
1841 | (when (not existing-buffer) | |
bd43c990 RS |
1842 | (rcirc-cmd-whois nick)))) |
1843 | ||
f161b079 | 1844 | (defun-rcirc-command join (channel) |
bd43c990 RS |
1845 | "Join CHANNEL." |
1846 | (interactive "sJoin channel: ") | |
f161b079 JB |
1847 | (let ((buffer (rcirc-get-buffer-create process |
1848 | (car (split-string channel))))) | |
a2524d26 | 1849 | (rcirc-send-string process (concat "JOIN " channel)) |
bd43c990 | 1850 | (when (not (eq (selected-window) (minibuffer-window))) |
195eca78 | 1851 | (switch-to-buffer buffer)))) |
bd43c990 | 1852 | |
195eca78 | 1853 | ;; TODO: /part #channel reason, or consider removing #channel altogether |
bd43c990 RS |
1854 | (defun-rcirc-command part (channel) |
1855 | "Part CHANNEL." | |
1856 | (interactive "sPart channel: ") | |
1857 | (let ((channel (if (> (length channel) 0) channel target))) | |
adf794e4 | 1858 | (rcirc-send-string process (concat "PART " channel " :" rcirc-id-string)))) |
bd43c990 RS |
1859 | |
1860 | (defun-rcirc-command quit (reason) | |
1861 | "Send a quit message to server with REASON." | |
1862 | (interactive "sQuit reason: ") | |
adf794e4 EZ |
1863 | (rcirc-send-string process (concat "QUIT :" |
1864 | (if (not (zerop (length reason))) | |
1865 | reason | |
1866 | rcirc-id-string)))) | |
bd43c990 RS |
1867 | |
1868 | (defun-rcirc-command nick (nick) | |
1869 | "Change nick to NICK." | |
1870 | (interactive "i") | |
1871 | (when (null nick) | |
1872 | (setq nick (read-string "New nick: " (rcirc-nick process)))) | |
1873 | (rcirc-send-string process (concat "NICK " nick))) | |
1874 | ||
1875 | (defun-rcirc-command names (channel) | |
1876 | "Display list of names in CHANNEL or in current channel if CHANNEL is nil. | |
1877 | If called interactively, prompt for a channel when prefix arg is supplied." | |
1878 | (interactive "P") | |
1879 | (if (interactive-p) | |
1880 | (if channel | |
1881 | (setq channel (read-string "List names in channel: " target)))) | |
1882 | (let ((channel (if (> (length channel) 0) | |
1883 | channel | |
1884 | target))) | |
1885 | (rcirc-send-string process (concat "NAMES " channel)))) | |
1886 | ||
1887 | (defun-rcirc-command topic (topic) | |
1888 | "List TOPIC for the TARGET channel. | |
1889 | With a prefix arg, prompt for new topic." | |
1890 | (interactive "P") | |
1891 | (if (and (interactive-p) topic) | |
1892 | (setq topic (read-string "New Topic: " rcirc-topic))) | |
1893 | (rcirc-send-string process (concat "TOPIC " target | |
1894 | (when (> (length topic) 0) | |
1895 | (concat " :" topic))))) | |
1896 | ||
1897 | (defun-rcirc-command whois (nick) | |
1898 | "Request information from server about NICK." | |
1899 | (interactive (list | |
1900 | (completing-read "Whois: " | |
a2524d26 | 1901 | (with-rcirc-server-buffer rcirc-nick-table)))) |
bd43c990 RS |
1902 | (rcirc-send-string process (concat "WHOIS " nick))) |
1903 | ||
1904 | (defun-rcirc-command mode (args) | |
1905 | "Set mode with ARGS." | |
1906 | (interactive (list (concat (read-string "Mode nick or channel: ") | |
1907 | " " (read-string "Mode: ")))) | |
1908 | (rcirc-send-string process (concat "MODE " args))) | |
1909 | ||
1910 | (defun-rcirc-command list (channels) | |
1911 | "Request information on CHANNELS from server." | |
1912 | (interactive "sList Channels: ") | |
1913 | (rcirc-send-string process (concat "LIST " channels))) | |
1914 | ||
1915 | (defun-rcirc-command oper (args) | |
1916 | "Send operator command to server." | |
1917 | (interactive "sOper args: ") | |
1918 | (rcirc-send-string process (concat "OPER " args))) | |
1919 | ||
1920 | (defun-rcirc-command quote (message) | |
1921 | "Send MESSAGE literally to server." | |
1922 | (interactive "sServer message: ") | |
1923 | (rcirc-send-string process message)) | |
1924 | ||
1925 | (defun-rcirc-command kick (arg) | |
1926 | "Kick NICK from current channel." | |
1927 | (interactive (list | |
1928 | (concat (completing-read "Kick nick: " | |
92c4adc1 | 1929 | (rcirc-channel-nicks |
a2524d26 EZ |
1930 | (rcirc-buffer-process) |
1931 | rcirc-target)) | |
bd43c990 RS |
1932 | (read-from-minibuffer "Kick reason: ")))) |
1933 | (let* ((arglist (split-string arg)) | |
adf794e4 | 1934 | (argstring (concat (car arglist) " :" |
bd43c990 RS |
1935 | (mapconcat 'identity (cdr arglist) " ")))) |
1936 | (rcirc-send-string process (concat "KICK " target " " argstring)))) | |
1937 | ||
1938 | (defun rcirc-cmd-ctcp (args &optional process target) | |
1939 | (if (string-match "^\\([^ ]+\\)\\s-+\\(.+\\)$" args) | |
1940 | (let ((target (match-string 1 args)) | |
1941 | (request (match-string 2 args))) | |
adf794e4 EZ |
1942 | (rcirc-send-string process |
1943 | (format "PRIVMSG %s \C-a%s\C-a" | |
1944 | target (upcase request)))) | |
1945 | (rcirc-print process (rcirc-nick process) "ERROR" nil | |
bd43c990 RS |
1946 | "usage: /ctcp NICK REQUEST"))) |
1947 | ||
1948 | (defun rcirc-cmd-me (args &optional process target) | |
1949 | (rcirc-send-string process (format "PRIVMSG %s :\C-aACTION %s\C-a" | |
1950 | target args))) | |
2c8abe90 | 1951 | |
f8db61b2 EZ |
1952 | (defun rcirc-add-or-remove (set &optional elt) |
1953 | (if (and elt (not (string= "" elt))) | |
1954 | (if (member-ignore-case elt set) | |
1955 | (delete elt set) | |
1956 | (cons elt set)) | |
1957 | set)) | |
1958 | ||
2c8abe90 AS |
1959 | (defun-rcirc-command ignore (nick) |
1960 | "Manage the ignore list. | |
1961 | Ignore NICK, unignore NICK if already ignored, or list ignored | |
1962 | nicks when no NICK is given. When listing ignored nicks, the | |
2e398771 | 1963 | ones added to the list automatically are marked with an asterisk." |
2c8abe90 | 1964 | (interactive "sToggle ignoring of nick: ") |
f8db61b2 | 1965 | (setq rcirc-ignore-list (rcirc-add-or-remove rcirc-ignore-list nick)) |
92c4adc1 | 1966 | (rcirc-print process nil "IGNORE" target |
db58efbf EZ |
1967 | (mapconcat |
1968 | (lambda (nick) | |
1969 | (concat nick | |
1970 | (if (member nick rcirc-ignore-list-automatic) | |
1971 | "*" ""))) | |
1972 | rcirc-ignore-list " "))) | |
2c8abe90 | 1973 | |
f8db61b2 EZ |
1974 | (defun-rcirc-command bright (nick) |
1975 | "Manage the bright nick list." | |
1976 | (interactive "sToggle emphasis of nick: ") | |
1977 | (setq rcirc-bright-nicks (rcirc-add-or-remove rcirc-bright-nicks nick)) | |
92c4adc1 | 1978 | (rcirc-print process nil "BRIGHT" target |
f8db61b2 EZ |
1979 | (mapconcat 'identity rcirc-bright-nicks " "))) |
1980 | ||
1981 | (defun-rcirc-command dim (nick) | |
1982 | "Manage the dim nick list." | |
1983 | (interactive "sToggle deemphasis of nick: ") | |
1984 | (setq rcirc-dim-nicks (rcirc-add-or-remove rcirc-dim-nicks nick)) | |
92c4adc1 | 1985 | (rcirc-print process nil "DIM" target |
f8db61b2 EZ |
1986 | (mapconcat 'identity rcirc-dim-nicks " "))) |
1987 | ||
1988 | (defun-rcirc-command keyword (keyword) | |
1989 | "Manage the keyword list. | |
1990 | Mark KEYWORD, unmark KEYWORD if already marked, or list marked | |
1991 | keywords when no KEYWORD is given." | |
1992 | (interactive "sToggle highlighting of keyword: ") | |
1993 | (setq rcirc-keywords (rcirc-add-or-remove rcirc-keywords keyword)) | |
92c4adc1 | 1994 | (rcirc-print process nil "KEYWORD" target |
f8db61b2 EZ |
1995 | (mapconcat 'identity rcirc-keywords " "))) |
1996 | ||
bd43c990 | 1997 | \f |
f8db61b2 EZ |
1998 | (defun rcirc-add-face (start end name &optional object) |
1999 | "Add face NAME to the face text property of the text from START to END." | |
2000 | (when name | |
2001 | (let ((pos start) | |
2002 | next prop) | |
2003 | (while (< pos end) | |
2004 | (setq prop (get-text-property pos 'face object) | |
2005 | next (next-single-property-change pos 'face object end)) | |
2006 | (unless (member name (get-text-property pos 'face object)) | |
2007 | (add-text-properties pos next (list 'face (cons name prop)) object)) | |
2008 | (setq pos next))))) | |
adf794e4 | 2009 | |
bd43c990 RS |
2010 | (defun rcirc-facify (string face) |
2011 | "Return a copy of STRING with FACE property added." | |
f8db61b2 EZ |
2012 | (let ((string (or string ""))) |
2013 | (rcirc-add-face 0 (length string) face string) | |
2014 | string)) | |
bd43c990 | 2015 | |
bd43c990 | 2016 | (defvar rcirc-url-regexp |
2fbed782 EZ |
2017 | (rx-to-string |
2018 | `(and word-boundary | |
92c4adc1 JB |
2019 | (or (and |
2020 | (or (and (or "http" "https" "ftp" "file" "gopher" "news" | |
019ed9c7 EZ |
2021 | "telnet" "wais" "mailto") |
2022 | "://") | |
2023 | "www.") | |
2024 | (1+ (char "-a-zA-Z0-9_.")) | |
7faa3f8c | 2025 | (1+ (char "-a-zA-Z0-9_")) |
019ed9c7 | 2026 | (optional ":" (1+ (char "0-9")))) |
2fbed782 EZ |
2027 | (and (1+ (char "-a-zA-Z0-9_.")) |
2028 | (or ".com" ".net" ".org") | |
2029 | word-boundary)) | |
92c4adc1 | 2030 | (optional |
2fbed782 | 2031 | (and "/" |
195eca78 | 2032 | (1+ (char "-a-zA-Z0-9_='!?#$\@~`%&*+|\\/:;.,{}[]()")) |
f8db61b2 | 2033 | (char "-a-zA-Z0-9_=#$\@~`%&*+|\\/:;{}[]()"))))) |
2e398771 | 2034 | "Regexp matching URLs. Set to nil to disable URL features in rcirc.") |
bd43c990 RS |
2035 | |
2036 | (defun rcirc-browse-url (&optional arg) | |
2e398771 | 2037 | "Prompt for URL to browse based on URLs in buffer." |
02f47e86 | 2038 | (interactive "P") |
bd43c990 RS |
2039 | (let ((completions (mapcar (lambda (x) (cons x nil)) rcirc-urls)) |
2040 | (initial-input (car rcirc-urls)) | |
2041 | (history (cdr rcirc-urls))) | |
2042 | (browse-url (completing-read "rcirc browse-url: " | |
2043 | completions nil nil initial-input 'history) | |
2044 | arg))) | |
2045 | ||
adf794e4 EZ |
2046 | (defun rcirc-browse-url-at-point (point) |
2047 | "Send URL at point to `browse-url'." | |
2048 | (interactive "d") | |
7faa3f8c | 2049 | (let ((beg (previous-single-property-change (1+ point) 'mouse-face)) |
adf794e4 EZ |
2050 | (end (next-single-property-change point 'mouse-face))) |
2051 | (browse-url (buffer-substring-no-properties beg end)))) | |
2052 | ||
2053 | (defun rcirc-browse-url-at-mouse (event) | |
2054 | "Send URL at mouse click to `browse-url'." | |
2055 | (interactive "e") | |
2056 | (let ((position (event-end event))) | |
2057 | (with-current-buffer (window-buffer (posn-window position)) | |
2058 | (rcirc-browse-url-at-point (posn-point position))))) | |
2059 | ||
f8db61b2 EZ |
2060 | \f |
2061 | (defvar rcirc-markup-text-functions | |
195eca78 | 2062 | '(rcirc-markup-attributes |
f8db61b2 EZ |
2063 | rcirc-markup-my-nick |
2064 | rcirc-markup-urls | |
2065 | rcirc-markup-keywords | |
195eca78 SM |
2066 | rcirc-markup-bright-nicks |
2067 | rcirc-markup-fill) | |
2068 | ||
f8db61b2 EZ |
2069 | "List of functions used to manipulate text before it is printed. |
2070 | ||
195eca78 SM |
2071 | Each function takes two arguments, SENDER, RESPONSE. The buffer |
2072 | is narrowed with the text to be printed and the point is at the | |
2073 | beginning of the `rcirc-text' propertized text.") | |
f8db61b2 | 2074 | |
195eca78 SM |
2075 | (defun rcirc-markup-timestamp (sender response) |
2076 | (goto-char (point-min)) | |
2077 | (insert (rcirc-facify (format-time-string rcirc-time-format) | |
2078 | 'rcirc-timestamp))) | |
f8db61b2 | 2079 | |
195eca78 | 2080 | (defun rcirc-markup-attributes (sender response) |
f8db61b2 EZ |
2081 | (while (re-search-forward "\\([\C-b\C-_\C-v]\\).*?\\(\\1\\|\C-o\\)" nil t) |
2082 | (rcirc-add-face (match-beginning 0) (match-end 0) | |
2083 | (case (char-after (match-beginning 1)) | |
2084 | (?\C-b 'bold) | |
2085 | (?\C-v 'italic) | |
2086 | (?\C-_ 'underline))) | |
2087 | ;; keep the ^O since it could terminate other attributes | |
2088 | (when (not (eq ?\C-o (char-before (match-end 2)))) | |
2089 | (delete-region (match-beginning 2) (match-end 2))) | |
2090 | (delete-region (match-beginning 1) (match-end 1)) | |
2091 | (goto-char (1+ (match-beginning 1)))) | |
2092 | ;; remove the ^O characters now | |
2093 | (while (re-search-forward "\C-o+" nil t) | |
2094 | (delete-region (match-beginning 0) (match-end 0)))) | |
2095 | ||
195eca78 | 2096 | (defun rcirc-markup-my-nick (sender response) |
f8db61b2 | 2097 | (with-syntax-table rcirc-nick-syntax-table |
195eca78 SM |
2098 | (while (re-search-forward (concat "\\b" |
2099 | (regexp-quote (rcirc-nick | |
2100 | (rcirc-buffer-process))) | |
f8db61b2 EZ |
2101 | "\\b") |
2102 | nil t) | |
92c4adc1 | 2103 | (rcirc-add-face (match-beginning 0) (match-end 0) |
f8db61b2 EZ |
2104 | 'rcirc-nick-in-message) |
2105 | (when (string= response "PRIVMSG") | |
195eca78 SM |
2106 | (rcirc-add-face (point-min) (point-max) |
2107 | 'rcirc-nick-in-message-full-line) | |
2108 | (rcirc-record-activity (current-buffer) 'nick))))) | |
f8db61b2 | 2109 | |
195eca78 | 2110 | (defun rcirc-markup-urls (sender response) |
f8db61b2 EZ |
2111 | (while (re-search-forward rcirc-url-regexp nil t) |
2112 | (let ((start (match-beginning 0)) | |
2113 | (end (match-end 0))) | |
2114 | (rcirc-add-face start end 'rcirc-url) | |
2115 | (add-text-properties start end (list 'mouse-face 'highlight | |
2116 | 'keymap rcirc-browse-url-map)) | |
2117 | ;; record the url | |
195eca78 SM |
2118 | (push (buffer-substring-no-properties start end) rcirc-urls)))) |
2119 | ||
2120 | (defun rcirc-markup-keywords (sender response) | |
2121 | (when (and (string= response "PRIVMSG") | |
2122 | (not (string= sender (rcirc-nick (rcirc-buffer-process))))) | |
2123 | (let* ((target (or rcirc-target "")) | |
2124 | (keywords (delq nil (mapcar (lambda (keyword) | |
2125 | (when (not (string-match keyword | |
2126 | target)) | |
2127 | keyword)) | |
2128 | rcirc-keywords)))) | |
2129 | (when keywords | |
2130 | (while (re-search-forward (regexp-opt keywords 'words) nil t) | |
2131 | (rcirc-add-face (match-beginning 0) (match-end 0) 'rcirc-keyword) | |
2132 | (rcirc-record-activity (current-buffer) 'keyword)))))) | |
2133 | ||
2134 | (defun rcirc-markup-bright-nicks (sender response) | |
f8db61b2 EZ |
2135 | (when (and rcirc-bright-nicks |
2136 | (string= response "NAMES")) | |
2137 | (with-syntax-table rcirc-nick-syntax-table | |
2138 | (while (re-search-forward (regexp-opt rcirc-bright-nicks 'words) nil t) | |
2139 | (rcirc-add-face (match-beginning 0) (match-end 0) | |
2140 | 'rcirc-bright-nick))))) | |
195eca78 SM |
2141 | |
2142 | (defun rcirc-markup-fill (sender response) | |
2143 | (when (not (string= response "372")) ; /motd | |
2144 | (let ((fill-prefix | |
2145 | (or rcirc-fill-prefix | |
2146 | (make-string (- (point) (line-beginning-position)) ?\s))) | |
2147 | (fill-column (cond ((eq rcirc-fill-column 'frame-width) | |
2148 | (1- (frame-width))) | |
2149 | (rcirc-fill-column | |
2150 | rcirc-fill-column) | |
2151 | (t fill-column)))) | |
2152 | (fill-region (point) (point-max) nil t)))) | |
bd43c990 RS |
2153 | \f |
2154 | ;;; handlers | |
2155 | ;; these are called with the server PROCESS, the SENDER, which is a | |
2156 | ;; server or a user, depending on the command, the ARGS, which is a | |
2157 | ;; list of strings, and the TEXT, which is the original server text, | |
2158 | ;; verbatim | |
2159 | (defun rcirc-handler-001 (process sender args text) | |
2160 | (rcirc-handler-generic process "001" sender args text) | |
2161 | ;; set the real server name | |
adf794e4 | 2162 | (with-rcirc-process-buffer process |
8216fbaf EZ |
2163 | (setq rcirc-connecting nil) |
2164 | (rcirc-reschedule-timeout process) | |
2165 | (setq rcirc-server-name sender) | |
bd43c990 RS |
2166 | (setq rcirc-nick (car args)) |
2167 | (rcirc-update-prompt) | |
2168 | (when rcirc-auto-authenticate-flag (rcirc-authenticate)) | |
adf794e4 | 2169 | (rcirc-join-channels process rcirc-startup-channels))) |
bd43c990 RS |
2170 | |
2171 | (defun rcirc-handler-PRIVMSG (process sender args text) | |
2172 | (let ((target (if (rcirc-channel-p (car args)) | |
2173 | (car args) | |
db58efbf | 2174 | sender)) |
bd43c990 RS |
2175 | (message (or (cadr args) ""))) |
2176 | (if (string-match "^\C-a\\(.*\\)\C-a$" message) | |
2177 | (rcirc-handler-CTCP process target sender (match-string 1 message)) | |
2178 | (rcirc-print process sender "PRIVMSG" target message t)) | |
2179 | ;; update nick timestamp | |
2180 | (if (member target (rcirc-nick-channels process sender)) | |
2181 | (rcirc-put-nick-channel process sender target)))) | |
2182 | ||
2183 | (defun rcirc-handler-NOTICE (process sender args text) | |
2184 | (let ((target (car args)) | |
2185 | (message (cadr args))) | |
adf794e4 EZ |
2186 | (if (string-match "^\C-a\\(.*\\)\C-a$" message) |
2187 | (rcirc-handler-CTCP-response process target sender | |
2188 | (match-string 1 message)) | |
2189 | (rcirc-print process sender "NOTICE" | |
2190 | (cond ((rcirc-channel-p target) | |
2191 | target) | |
2192 | ;;; -ChanServ- [#gnu] Welcome... | |
02f47e86 | 2193 | ((string-match "\\[\\(#[^\] ]+\\)\\]" message) |
adf794e4 EZ |
2194 | (match-string 1 message)) |
2195 | (sender | |
a2524d26 | 2196 | (if (string= sender (rcirc-server-name process)) |
db58efbf EZ |
2197 | nil ; server notice |
2198 | sender))) | |
adf794e4 | 2199 | message t)))) |
bd43c990 RS |
2200 | |
2201 | (defun rcirc-handler-WALLOPS (process sender args text) | |
db58efbf | 2202 | (rcirc-print process sender "WALLOPS" sender (car args) t)) |
bd43c990 RS |
2203 | |
2204 | (defun rcirc-handler-JOIN (process sender args text) | |
db58efbf | 2205 | (let ((channel (car args))) |
bd43c990 RS |
2206 | (rcirc-get-buffer-create process channel) |
2207 | (rcirc-print process sender "JOIN" channel "") | |
2208 | ||
2209 | ;; print in private chat buffer if it exists | |
a2524d26 | 2210 | (when (rcirc-get-buffer (rcirc-buffer-process) sender) |
db58efbf | 2211 | (rcirc-print process sender "JOIN" sender channel)) |
bd43c990 | 2212 | |
adf794e4 | 2213 | (rcirc-put-nick-channel process sender channel))) |
bd43c990 RS |
2214 | |
2215 | ;; PART and KICK are handled the same way | |
2216 | (defun rcirc-handler-PART-or-KICK (process response channel sender nick args) | |
a2524d26 | 2217 | (rcirc-ignore-update-automatic nick) |
bd43c990 RS |
2218 | (if (not (string= nick (rcirc-nick process))) |
2219 | ;; this is someone else leaving | |
adf794e4 EZ |
2220 | (rcirc-remove-nick-channel process nick channel) |
2221 | ;; this is us leaving | |
2222 | (mapc (lambda (n) | |
2223 | (rcirc-remove-nick-channel process n channel)) | |
2224 | (rcirc-channel-nicks process channel)) | |
2225 | ||
2226 | ;; if the buffer is still around, make it inactive | |
2227 | (let ((buffer (rcirc-get-buffer process channel))) | |
2228 | (when buffer | |
195eca78 | 2229 | (rcirc-disconnect-buffer buffer))))) |
bd43c990 RS |
2230 | |
2231 | (defun rcirc-handler-PART (process sender args text) | |
a2524d26 EZ |
2232 | (let* ((channel (car args)) |
2233 | (reason (cadr args)) | |
2234 | (message (concat channel " " reason))) | |
2235 | (rcirc-print process sender "PART" channel message) | |
2236 | ;; print in private chat buffer if it exists | |
2237 | (when (rcirc-get-buffer (rcirc-buffer-process) sender) | |
2238 | (rcirc-print process sender "PART" sender message)) | |
2239 | ||
2240 | (rcirc-handler-PART-or-KICK process "PART" channel sender sender reason))) | |
bd43c990 RS |
2241 | |
2242 | (defun rcirc-handler-KICK (process sender args text) | |
a2524d26 EZ |
2243 | (let* ((channel (car args)) |
2244 | (nick (cadr args)) | |
2245 | (reason (caddr args)) | |
2246 | (message (concat nick " " channel " " reason))) | |
2247 | (rcirc-print process sender "KICK" channel message t) | |
2248 | ;; print in private chat buffer if it exists | |
2249 | (when (rcirc-get-buffer (rcirc-buffer-process) nick) | |
2250 | (rcirc-print process sender "KICK" nick message)) | |
2251 | ||
2252 | (rcirc-handler-PART-or-KICK process "KICK" channel sender nick reason))) | |
bd43c990 RS |
2253 | |
2254 | (defun rcirc-handler-QUIT (process sender args text) | |
db58efbf EZ |
2255 | (rcirc-ignore-update-automatic sender) |
2256 | (mapc (lambda (channel) | |
2257 | (rcirc-print process sender "QUIT" channel (apply 'concat args))) | |
2258 | (rcirc-nick-channels process sender)) | |
bd43c990 | 2259 | |
db58efbf | 2260 | ;; print in private chat buffer if it exists |
a2524d26 | 2261 | (when (rcirc-get-buffer (rcirc-buffer-process) sender) |
db58efbf | 2262 | (rcirc-print process sender "QUIT" sender (apply 'concat args))) |
bd43c990 | 2263 | |
db58efbf | 2264 | (rcirc-nick-remove process sender)) |
bd43c990 RS |
2265 | |
2266 | (defun rcirc-handler-NICK (process sender args text) | |
db58efbf | 2267 | (let* ((old-nick sender) |
bd43c990 RS |
2268 | (new-nick (car args)) |
2269 | (channels (rcirc-nick-channels process old-nick))) | |
2c8abe90 AS |
2270 | ;; update list of ignored nicks |
2271 | (rcirc-ignore-update-automatic old-nick) | |
2272 | (when (member old-nick rcirc-ignore-list) | |
2273 | (add-to-list 'rcirc-ignore-list new-nick) | |
2274 | (add-to-list 'rcirc-ignore-list-automatic new-nick)) | |
bd43c990 RS |
2275 | ;; print message to nick's channels |
2276 | (dolist (target channels) | |
2277 | (rcirc-print process sender "NICK" target new-nick)) | |
2278 | ;; update private chat buffer, if it exists | |
adf794e4 EZ |
2279 | (let ((chat-buffer (rcirc-get-buffer process old-nick))) |
2280 | (when chat-buffer | |
2281 | (with-current-buffer chat-buffer | |
2282 | (rcirc-print process sender "NICK" old-nick new-nick) | |
2283 | (setq rcirc-target new-nick) | |
2284 | (rename-buffer (rcirc-generate-new-buffer-name process new-nick))))) | |
bd43c990 | 2285 | ;; remove old nick and add new one |
adf794e4 | 2286 | (with-rcirc-process-buffer process |
bd43c990 RS |
2287 | (let ((v (gethash old-nick rcirc-nick-table))) |
2288 | (remhash old-nick rcirc-nick-table) | |
2289 | (puthash new-nick v rcirc-nick-table)) | |
2290 | ;; if this is our nick... | |
2291 | (when (string= old-nick rcirc-nick) | |
2292 | (setq rcirc-nick new-nick) | |
adf794e4 | 2293 | (rcirc-update-prompt t) |
bd43c990 RS |
2294 | ;; reauthenticate |
2295 | (when rcirc-auto-authenticate-flag (rcirc-authenticate)))))) | |
2296 | ||
2297 | (defun rcirc-handler-PING (process sender args text) | |
195eca78 | 2298 | (rcirc-send-string process (concat "PONG :" (car args)))) |
bd43c990 RS |
2299 | |
2300 | (defun rcirc-handler-PONG (process sender args text) | |
2301 | ;; do nothing | |
2302 | ) | |
2303 | ||
2304 | (defun rcirc-handler-TOPIC (process sender args text) | |
2305 | (let ((topic (cadr args))) | |
2306 | (rcirc-print process sender "TOPIC" (car args) topic) | |
2307 | (with-current-buffer (rcirc-get-buffer process (car args)) | |
2308 | (setq rcirc-topic topic)))) | |
2309 | ||
a2524d26 EZ |
2310 | (defvar rcirc-nick-away-alist nil) |
2311 | (defun rcirc-handler-301 (process sender args text) | |
2312 | "RPL_AWAY" | |
2313 | (let* ((nick (cadr args)) | |
2314 | (rec (assoc-string nick rcirc-nick-away-alist)) | |
2315 | (away-message (caddr args))) | |
2316 | (when (or (not rec) | |
2317 | (not (string= (cdr rec) away-message))) | |
2318 | ;; away message has changed | |
2319 | (rcirc-handler-generic process "AWAY" nick (cdr args) text) | |
2320 | (if rec | |
2321 | (setcdr rec away-message) | |
2322 | (setq rcirc-nick-away-alist (cons (cons nick away-message) | |
2323 | rcirc-nick-away-alist)))))) | |
2324 | ||
bd43c990 RS |
2325 | (defun rcirc-handler-332 (process sender args text) |
2326 | "RPL_TOPIC" | |
adf794e4 EZ |
2327 | (let ((buffer (or (rcirc-get-buffer process (cadr args)) |
2328 | (rcirc-get-temp-buffer-create process (cadr args))))) | |
2329 | (with-current-buffer buffer | |
2330 | (setq rcirc-topic (caddr args))))) | |
bd43c990 RS |
2331 | |
2332 | (defun rcirc-handler-333 (process sender args text) | |
2333 | "Not in rfc1459.txt" | |
adf794e4 EZ |
2334 | (let ((buffer (or (rcirc-get-buffer process (cadr args)) |
2335 | (rcirc-get-temp-buffer-create process (cadr args))))) | |
2336 | (with-current-buffer buffer | |
2337 | (let ((setter (caddr args)) | |
2338 | (time (current-time-string | |
2339 | (seconds-to-time | |
2340 | (string-to-number (cadddr args)))))) | |
2341 | (rcirc-print process sender "TOPIC" (cadr args) | |
2342 | (format "%s (%s on %s)" rcirc-topic setter time)))))) | |
bd43c990 RS |
2343 | |
2344 | (defun rcirc-handler-477 (process sender args text) | |
2345 | "ERR_NOCHANMODES" | |
2346 | (rcirc-print process sender "477" (cadr args) (caddr args))) | |
2347 | ||
2348 | (defun rcirc-handler-MODE (process sender args text) | |
2349 | (let ((target (car args)) | |
2350 | (msg (mapconcat 'identity (cdr args) " "))) | |
2351 | (rcirc-print process sender "MODE" | |
2352 | (if (string= target (rcirc-nick process)) | |
2353 | nil | |
2354 | target) | |
2355 | msg) | |
2356 | ||
2357 | ;; print in private chat buffers if they exist | |
2358 | (mapc (lambda (nick) | |
db58efbf EZ |
2359 | (when (rcirc-get-buffer process nick) |
2360 | (rcirc-print process sender "MODE" nick msg))) | |
adf794e4 | 2361 | (cddr args)))) |
bd43c990 RS |
2362 | |
2363 | (defun rcirc-get-temp-buffer-create (process channel) | |
2364 | "Return a buffer based on PROCESS and CHANNEL." | |
2365 | (let ((tmpnam (concat " " (downcase channel) "TMP" (process-name process)))) | |
2366 | (get-buffer-create tmpnam))) | |
2367 | ||
2368 | (defun rcirc-handler-353 (process sender args text) | |
2369 | "RPL_NAMREPLY" | |
adf794e4 | 2370 | (let ((channel (caddr args))) |
bd43c990 RS |
2371 | (mapc (lambda (nick) |
2372 | (rcirc-put-nick-channel process nick channel)) | |
adf794e4 | 2373 | (split-string (cadddr args) " " t)) |
bd43c990 RS |
2374 | (with-current-buffer (rcirc-get-temp-buffer-create process channel) |
2375 | (goto-char (point-max)) | |
2376 | (insert (car (last args)) " ")))) | |
2377 | ||
2378 | (defun rcirc-handler-366 (process sender args text) | |
2379 | "RPL_ENDOFNAMES" | |
2380 | (let* ((channel (cadr args)) | |
2381 | (buffer (rcirc-get-temp-buffer-create process channel))) | |
2382 | (with-current-buffer buffer | |
2383 | (rcirc-print process sender "NAMES" channel | |
2384 | (buffer-substring (point-min) (point-max)))) | |
2385 | (kill-buffer buffer))) | |
2386 | ||
2387 | (defun rcirc-handler-433 (process sender args text) | |
2388 | "ERR_NICKNAMEINUSE" | |
2389 | (rcirc-handler-generic process "433" sender args text) | |
2390 | (let* ((new-nick (concat (cadr args) "`"))) | |
adf794e4 | 2391 | (with-rcirc-process-buffer process |
bd43c990 RS |
2392 | (rcirc-cmd-nick new-nick nil process)))) |
2393 | ||
2394 | (defun rcirc-authenticate () | |
2395 | "Send authentication to process associated with current buffer. | |
db58efbf | 2396 | Passwords are stored in `rcirc-authinfo' (which see)." |
bd43c990 | 2397 | (interactive) |
a2524d26 | 2398 | (with-rcirc-server-buffer |
db58efbf | 2399 | (dolist (i rcirc-authinfo) |
a2524d26 EZ |
2400 | (let ((process (rcirc-buffer-process)) |
2401 | (server (car i)) | |
db58efbf EZ |
2402 | (nick (caddr i)) |
2403 | (method (cadr i)) | |
2404 | (args (cdddr i))) | |
2405 | (when (and (string-match server rcirc-server) | |
2406 | (string-match nick rcirc-nick)) | |
2407 | (cond ((equal method 'nickserv) | |
2408 | (rcirc-send-string | |
a2524d26 | 2409 | process |
db58efbf EZ |
2410 | (concat |
2411 | "PRIVMSG nickserv :identify " | |
2412 | (car args)))) | |
2413 | ((equal method 'chanserv) | |
2414 | (rcirc-send-string | |
a2524d26 | 2415 | process |
db58efbf EZ |
2416 | (concat |
2417 | "PRIVMSG chanserv :identify " | |
195eca78 | 2418 | (car args) " " (cadr args)))) |
db58efbf EZ |
2419 | ((equal method 'bitlbee) |
2420 | (rcirc-send-string | |
a2524d26 | 2421 | process |
db58efbf EZ |
2422 | (concat "PRIVMSG &bitlbee :identify " (car args)))) |
2423 | (t | |
2424 | (message "No %S authentication method defined" | |
2425 | method)))))))) | |
adf794e4 | 2426 | |
bd43c990 RS |
2427 | (defun rcirc-handler-INVITE (process sender args text) |
2428 | (rcirc-print process sender "INVITE" nil (mapconcat 'identity args " ") t)) | |
2429 | ||
2430 | (defun rcirc-handler-ERROR (process sender args text) | |
2431 | (rcirc-print process sender "ERROR" nil (mapconcat 'identity args " "))) | |
2432 | ||
2433 | (defun rcirc-handler-CTCP (process target sender text) | |
2434 | (if (string-match "^\\([^ ]+\\) *\\(.*\\)$" text) | |
2435 | (let* ((request (upcase (match-string 1 text))) | |
2436 | (args (match-string 2 text)) | |
bd43c990 RS |
2437 | (handler (intern-soft (concat "rcirc-handler-ctcp-" request)))) |
2438 | (if (not (fboundp handler)) | |
db58efbf EZ |
2439 | (rcirc-print process sender "ERROR" target |
2440 | (format "%s sent unsupported ctcp: %s" sender text) | |
adf794e4 | 2441 | t) |
bd43c990 | 2442 | (funcall handler process target sender args) |
195eca78 SM |
2443 | (unless (or (string= request "ACTION") |
2444 | (string= request "KEEPALIVE")) | |
db58efbf | 2445 | (rcirc-print process sender "CTCP" target |
adf794e4 | 2446 | (format "%s" text) t)))))) |
bd43c990 RS |
2447 | |
2448 | (defun rcirc-handler-ctcp-VERSION (process target sender args) | |
2449 | (rcirc-send-string process | |
db58efbf | 2450 | (concat "NOTICE " sender |
adf794e4 | 2451 | " :\C-aVERSION " rcirc-id-string |
bd43c990 RS |
2452 | "\C-a"))) |
2453 | ||
2454 | (defun rcirc-handler-ctcp-ACTION (process target sender args) | |
2455 | (rcirc-print process sender "ACTION" target args t)) | |
2456 | ||
2457 | (defun rcirc-handler-ctcp-TIME (process target sender args) | |
2458 | (rcirc-send-string process | |
db58efbf | 2459 | (concat "NOTICE " sender |
bd43c990 | 2460 | " :\C-aTIME " (current-time-string) "\C-a"))) |
adf794e4 EZ |
2461 | |
2462 | (defun rcirc-handler-CTCP-response (process target sender message) | |
2463 | (rcirc-print process sender "CTCP" nil message t)) | |
bd43c990 | 2464 | \f |
adf794e4 EZ |
2465 | (defgroup rcirc-faces nil |
2466 | "Faces for rcirc." | |
2467 | :group 'rcirc | |
2468 | :group 'faces) | |
2469 | ||
ad8121fe EZ |
2470 | (defface rcirc-my-nick ; font-lock-function-name-face |
2471 | '((((class color) (min-colors 88) (background light)) (:foreground "Blue1")) | |
2472 | (((class color) (min-colors 88) (background dark)) (:foreground "LightSkyBlue")) | |
2473 | (((class color) (min-colors 16) (background light)) (:foreground "Blue")) | |
2474 | (((class color) (min-colors 16) (background dark)) (:foreground "LightSkyBlue")) | |
2475 | (((class color) (min-colors 8)) (:foreground "blue" :weight bold)) | |
2476 | (t (:inverse-video t :weight bold))) | |
adf794e4 EZ |
2477 | "The face used to highlight my messages." |
2478 | :group 'rcirc-faces) | |
bd43c990 | 2479 | |
ad8121fe EZ |
2480 | (defface rcirc-other-nick ; font-lock-variable-name-face |
2481 | '((((class grayscale) (background light)) | |
2482 | (:foreground "Gray90" :weight bold :slant italic)) | |
bd43c990 | 2483 | (((class grayscale) (background dark)) |
ad8121fe EZ |
2484 | (:foreground "DimGray" :weight bold :slant italic)) |
2485 | (((class color) (min-colors 88) (background light)) (:foreground "DarkGoldenrod")) | |
2486 | (((class color) (min-colors 88) (background dark)) (:foreground "LightGoldenrod")) | |
2487 | (((class color) (min-colors 16) (background light)) (:foreground "DarkGoldenrod")) | |
2488 | (((class color) (min-colors 16) (background dark)) (:foreground "LightGoldenrod")) | |
2489 | (((class color) (min-colors 8)) (:foreground "yellow" :weight light)) | |
2490 | (t (:weight bold :slant italic))) | |
adf794e4 EZ |
2491 | "The face used to highlight other messages." |
2492 | :group 'rcirc-faces) | |
bd43c990 | 2493 | |
02f47e86 MB |
2494 | (defface rcirc-bright-nick |
2495 | '((((class grayscale) (background light)) | |
2496 | (:foreground "LightGray" :weight bold :underline t)) | |
2497 | (((class grayscale) (background dark)) | |
2498 | (:foreground "Gray50" :weight bold :underline t)) | |
2499 | (((class color) (min-colors 88) (background light)) (:foreground "CadetBlue")) | |
2500 | (((class color) (min-colors 88) (background dark)) (:foreground "Aquamarine")) | |
2501 | (((class color) (min-colors 16) (background light)) (:foreground "CadetBlue")) | |
2502 | (((class color) (min-colors 16) (background dark)) (:foreground "Aquamarine")) | |
2503 | (((class color) (min-colors 8)) (:foreground "magenta")) | |
2504 | (t (:weight bold :underline t))) | |
f8db61b2 | 2505 | "Face used for nicks matched by `rcirc-bright-nicks'." |
02f47e86 MB |
2506 | :group 'rcirc-faces) |
2507 | ||
2508 | (defface rcirc-dim-nick | |
2509 | '((t :inherit default)) | |
f8db61b2 | 2510 | "Face used for nicks in `rcirc-dim-nicks'." |
02f47e86 MB |
2511 | :group 'rcirc-faces) |
2512 | ||
ad8121fe EZ |
2513 | (defface rcirc-server ; font-lock-comment-face |
2514 | '((((class grayscale) (background light)) | |
2515 | (:foreground "DimGray" :weight bold :slant italic)) | |
bd43c990 | 2516 | (((class grayscale) (background dark)) |
ad8121fe EZ |
2517 | (:foreground "LightGray" :weight bold :slant italic)) |
2518 | (((class color) (min-colors 88) (background light)) | |
2519 | (:foreground "Firebrick")) | |
2520 | (((class color) (min-colors 88) (background dark)) | |
2521 | (:foreground "chocolate1")) | |
2522 | (((class color) (min-colors 16) (background light)) | |
2523 | (:foreground "red")) | |
2524 | (((class color) (min-colors 16) (background dark)) | |
2525 | (:foreground "red1")) | |
2526 | (((class color) (min-colors 8) (background light)) | |
2527 | ) | |
2528 | (((class color) (min-colors 8) (background dark)) | |
2529 | ) | |
2530 | (t (:weight bold :slant italic))) | |
adf794e4 EZ |
2531 | "The face used to highlight server messages." |
2532 | :group 'rcirc-faces) | |
bd43c990 | 2533 | |
ad8121fe | 2534 | (defface rcirc-server-prefix ; font-lock-comment-delimiter-face |
db58efbf | 2535 | '((default :inherit rcirc-server) |
ad8121fe EZ |
2536 | (((class grayscale))) |
2537 | (((class color) (min-colors 16))) | |
2538 | (((class color) (min-colors 8) (background light)) | |
2539 | :foreground "red") | |
2540 | (((class color) (min-colors 8) (background dark)) | |
2541 | :foreground "red1")) | |
2542 | "The face used to highlight server prefixes." | |
2543 | :group 'rcirc-faces) | |
2544 | ||
2545 | (defface rcirc-timestamp | |
2546 | '((t (:inherit default))) | |
2547 | "The face used to highlight timestamps." | |
2548 | :group 'rcirc-faces) | |
2549 | ||
2550 | (defface rcirc-nick-in-message ; font-lock-keyword-face | |
2551 | '((((class grayscale) (background light)) (:foreground "LightGray" :weight bold)) | |
2552 | (((class grayscale) (background dark)) (:foreground "DimGray" :weight bold)) | |
2553 | (((class color) (min-colors 88) (background light)) (:foreground "Purple")) | |
2554 | (((class color) (min-colors 88) (background dark)) (:foreground "Cyan1")) | |
2555 | (((class color) (min-colors 16) (background light)) (:foreground "Purple")) | |
2556 | (((class color) (min-colors 16) (background dark)) (:foreground "Cyan")) | |
2557 | (((class color) (min-colors 8)) (:foreground "cyan" :weight bold)) | |
2558 | (t (:weight bold))) | |
f8db61b2 | 2559 | "The face used to highlight instances of your nick within messages." |
adf794e4 | 2560 | :group 'rcirc-faces) |
bd43c990 | 2561 | |
f8db61b2 EZ |
2562 | (defface rcirc-nick-in-message-full-line |
2563 | '((t (:bold t))) | |
2564 | "The face used emphasize the entire message when your nick is mentioned." | |
92c4adc1 | 2565 | :group 'rcirc-faces) |
f8db61b2 | 2566 | |
ad8121fe EZ |
2567 | (defface rcirc-prompt ; comint-highlight-prompt |
2568 | '((((min-colors 88) (background dark)) (:foreground "cyan1")) | |
2569 | (((background dark)) (:foreground "cyan")) | |
bd43c990 | 2570 | (t (:foreground "dark blue"))) |
2e398771 | 2571 | "The face used to highlight prompts." |
adf794e4 | 2572 | :group 'rcirc-faces) |
bd43c990 | 2573 | |
f8db61b2 | 2574 | (defface rcirc-track-nick |
8216fbaf EZ |
2575 | '((((type tty)) (:inherit default)) |
2576 | (t (:inverse-video t))) | |
f8db61b2 EZ |
2577 | "The face used in the mode-line when your nick is mentioned." |
2578 | :group 'rcirc-faces) | |
2579 | ||
2580 | (defface rcirc-track-keyword | |
2581 | '((t (:bold t ))) | |
2582 | "The face used in the mode-line when keywords are mentioned." | |
2583 | :group 'rcirc-faces) | |
2584 | ||
2585 | (defface rcirc-url | |
bd43c990 | 2586 | '((t (:bold t))) |
f8db61b2 EZ |
2587 | "The face used to highlight urls." |
2588 | :group 'rcirc-faces) | |
2589 | ||
2590 | (defface rcirc-keyword | |
2591 | '((t (:inherit highlight))) | |
2592 | "The face used to highlight keywords." | |
adf794e4 | 2593 | :group 'rcirc-faces) |
a2524d26 | 2594 | |
bd43c990 | 2595 | \f |
adf794e4 | 2596 | ;; When using M-x flyspell-mode, only check words after the prompt |
bd43c990 RS |
2597 | (put 'rcirc-mode 'flyspell-mode-predicate 'rcirc-looking-at-input) |
2598 | (defun rcirc-looking-at-input () | |
2599 | "Returns true if point is past the input marker." | |
2600 | (>= (point) rcirc-prompt-end-marker)) | |
2601 | \f | |
2602 | ||
2603 | (provide 'rcirc) | |
e636ae15 MB |
2604 | |
2605 | ;; arch-tag: b471b7e8-6b5a-4399-b2c6-a3c78dfc8ffb | |
bd43c990 | 2606 | ;;; rcirc.el ends here |