Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / erc / erc-button.el
CommitLineData
597993cf
MB
1;; erc-button.el --- A way of buttonizing certain things in ERC buffers
2
3;; Copyright (C) 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
8b72699e 4;; 2006, 2007, 2008 Free Software Foundation, Inc.
597993cf
MB
5
6;; Author: Mario Lang <mlang@delysid.org>
7;; Keywords: irc, button, url, regexp
8;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcButton
9
10;; This file is part of GNU Emacs.
11
4ee57b2a 12;; GNU Emacs is free software: you can redistribute it and/or modify
597993cf 13;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
597993cf
MB
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
4ee57b2a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
597993cf
MB
24
25;;; Commentary:
26
27;; Heavily borrowed from gnus-art.el. Thanks to the original authors.
28;; This buttonizes nicks and other stuff to make it all clickable.
29;; To enable, add to your ~/.emacs:
30;; (require 'erc-button)
31;; (erc-button-mode 1)
32;;
33;; Todo:
34;; * Rewrite all this to do the same, but use button.el from GNU Emacs
35;; if it's available for xemacs too. Why? button.el is much faster,
36;; and much more elegant, and solves the problem we get with large buffers
37;; and a large erc-button-marker-list.
38
39\f
40;;; Code:
41
42(require 'erc)
43(require 'wid-edit)
44(require 'erc-fill)
45
46;;; Minor Mode
47
48(defgroup erc-button nil
49 "Define how text can be turned into clickable buttons."
50 :group 'erc)
51
52;;;###autoload (autoload 'erc-button-mode "erc-button" nil t)
53(define-erc-module button nil
54 "This mode buttonizes all messages according to `erc-button-alist'."
55 ((add-hook 'erc-insert-modify-hook 'erc-button-add-buttons 'append)
56 (add-hook 'erc-send-modify-hook 'erc-button-add-buttons 'append)
ff59d266 57 (add-hook 'erc-complete-functions 'erc-button-next)
5e56b3fb 58 (add-hook 'erc-mode-hook 'erc-button-setup))
597993cf
MB
59 ((remove-hook 'erc-insert-modify-hook 'erc-button-add-buttons)
60 (remove-hook 'erc-send-modify-hook 'erc-button-add-buttons)
ff59d266 61 (remove-hook 'erc-complete-functions 'erc-button-next)
5e56b3fb
MO
62 (remove-hook 'erc-mode-hook 'erc-button-setup)
63 (when (featurep 'xemacs)
64 (dolist (buffer (erc-buffer-list))
65 (with-current-buffer buffer
66 (kill-local-variable 'widget-button-face))))))
597993cf
MB
67
68;;; Variables
69
70(defface erc-button '((t (:bold t)))
71 "ERC button face."
72 :group 'erc-faces)
73
74(defcustom erc-button-face 'erc-button
75 "Face used for highlighting buttons in ERC buffers.
76
77A button is a piece of text that you can activate by pressing
78`RET' or `mouse-2' above it. See also `erc-button-keymap'."
79 :type 'face
80 :group 'erc-faces)
81
82(defcustom erc-button-nickname-face 'erc-nick-default-face
83 "Face used for ERC nickname buttons."
84 :type 'face
85 :group 'erc-faces)
86
87(defcustom erc-button-mouse-face 'highlight
88 "Face used for mouse highlighting in ERC buffers.
89
90Buttons will be displayed in this face when the mouse cursor is
91above them."
92 :type 'face
93 :group 'erc-faces)
94
95(defcustom erc-button-url-regexp
96 (concat "\\(www\\.\\|\\(s?https?\\|"
97 "ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\)"
98 "\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?"
526dc846 99 "[-a-zA-Z0-9_=!?#$@~`%&*+\\/:;.,()]+[-a-zA-Z0-9_=#$@~`%&*+\\/()]")
597993cf
MB
100 "Regular expression that matches URLs."
101 :group 'erc-button
102 :type 'regexp)
103
104(defcustom erc-button-wrap-long-urls nil
105 "If non-nil, \"long\" URLs matching `erc-button-url-regexp' will be wrapped.
106
107If this variable is a number, consider URLs longer than its value to
108be \"long\". If t, URLs will be considered \"long\" if they are
109longer than `erc-fill-column'."
110 :group 'erc-button
111 :type '(choice integer boolean))
112
113(defcustom erc-button-buttonize-nicks t
114 "Flag indicating whether nicks should be buttonized or not."
115 :group 'erc-button
116 :type 'boolean)
117
118(defcustom erc-button-rfc-url "http://www.faqs.org/rfcs/rfc%s.html"
119 "*URL used to browse rfc references.
120%s is replaced by the number."
121 :group 'erc-button
122 :type 'string)
123
124(defcustom erc-button-google-url "http://www.google.com/search?q=%s"
125 "*URL used to browse Google search references.
126%s is replaced by the search string."
127 :group 'erc-button
128 :type 'string)
129
130(defcustom erc-button-alist
131 ;; Since the callback is only executed when the user is clicking on
132 ;; a button, it makes no sense to optimize performance by
133 ;; bytecompiling lambdas in this alist. On the other hand, it makes
134 ;; things hard to maintain.
135 '(('nicknames 0 erc-button-buttonize-nicks erc-nick-popup 0)
136 (erc-button-url-regexp 0 t browse-url 0)
137 ("<URL: *\\([^<> ]+\\) *>" 0 t browse-url 1)
138 ("(\\(\\([^~\n \t@][^\n \t@]*\\)@\\([a-zA-Z0-9.:-]+\\)\\)" 1 t finger 2 3)
139 ;; emacs internal
140 ("[`]\\([a-zA-Z][-a-zA-Z_0-9]+\\)[']" 1 t erc-button-describe-symbol 1)
141 ;; pseudo links
142 ("\\bInfo:[\"]\\([^\"]+\\)[\"]" 0 t Info-goto-node 1)
143 ("\\b\\(Ward\\|Wiki\\|WardsWiki\\|TheWiki\\):\\([A-Z][a-z]+\\([A-Z][a-z]+\\)+\\)"
144 0 t (lambda (page)
145 (browse-url (concat "http://c2.com/cgi-bin/wiki?" page)))
146 2)
147 ("EmacsWiki:\\([A-Z][a-z]+\\([A-Z][a-z]+\\)+\\)" 0 t erc-browse-emacswiki 1)
148 ("Lisp:\\([a-zA-Z.+-]+\\)" 0 t erc-browse-emacswiki-lisp 1)
149 ("\\bGoogle:\\([^ \t\n\r\f]+\\)"
150 0 t (lambda (keywords)
151 (browse-url (format erc-button-google-url keywords)))
152 1)
153 ("\\brfc[#: ]?\\([0-9]+\\)"
154 0 t (lambda (num)
155 (browse-url (format erc-button-rfc-url num)))
156 1)
157 ;; other
158 ("\\s-\\(@\\([0-9][0-9][0-9]\\)\\)" 1 t erc-button-beats-to-time 2))
159 "*Alist of regexps matching buttons in ERC buffers.
160Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
161
162REGEXP is the string matching text around the button or a symbol
163 indicating a variable holding that string, or a list of
164 strings, or an alist with the strings in the car. Note that
165 entries in lists or alists are considered to be nicks or other
166 complete words. Therefore they are enclosed in \\< and \\>
167 while searching. REGEXP can also be the quoted symbol
168 'nicknames, which matches the nickname of any user on the
169 current server.
170
171BUTTON is the number of the regexp grouping actually matching the
172 button, This is ignored if REGEXP is 'nicknames.
173
174FORM is a lisp expression which must eval to true for the button to
175 be added,
176
177CALLBACK is the function to call when the user push this button.
178 CALLBACK can also be a symbol. Its variable value will be used
179 as the callback function.
180
181PAR is a number of a regexp grouping whose text will be passed to
182 CALLBACK. There can be several PAR arguments. If REGEXP is
183 'nicknames, these are ignored, and CALLBACK will be called with
184 the nickname matched as the argument."
185 :group 'erc-button
186 :type '(repeat
187 (list :tag "Button"
188 (choice :tag "Matches"
189 regexp
190 (variable :tag "Variable containing regexp")
191 (const :tag "Nicknames" 'nicknames))
192 (integer :tag "Number of the regexp section that matches")
193 (choice :tag "When to buttonize"
194 (const :tag "Always" t)
195 (sexp :tag "Only when this evaluates to non-nil"))
196 (function :tag "Function to call when button is pressed")
197 (repeat :tag "Sections of regexp to send to the function"
198 :inline t
199 (integer :tag "Regexp section number")))))
200
201(defcustom erc-emacswiki-url "http://www.emacswiki.org/cgi-bin/wiki.pl?"
202 "*URL of the EmacsWiki Homepage."
203 :group 'erc-button
204 :type 'string)
205
206(defcustom erc-emacswiki-lisp-url "http://www.emacswiki.org/elisp/"
207 "*URL of the EmacsWiki ELisp area."
208 :group 'erc-button
209 :type 'string)
210
211(defvar erc-button-keymap
212 (let ((map (make-sparse-keymap)))
213 (define-key map (kbd "RET") 'erc-button-press-button)
214 (if (featurep 'xemacs)
215 (define-key map (kbd "<button2>") 'erc-button-click-button)
216 (define-key map (kbd "<mouse-2>") 'erc-button-click-button))
217 (define-key map (kbd "TAB") 'erc-button-next)
c89e5cd7 218 (define-key map (kbd "<backtab>") 'erc-button-previous)
597993cf
MB
219 (set-keymap-parent map erc-mode-map)
220 map)
221 "Local keymap for ERC buttons.")
222
223(defvar erc-button-syntax-table
224 (let ((table (make-syntax-table)))
225 (modify-syntax-entry ?\( "w" table)
226 (modify-syntax-entry ?\) "w" table)
227 (modify-syntax-entry ?\[ "w" table)
228 (modify-syntax-entry ?\] "w" table)
229 (modify-syntax-entry ?\{ "w" table)
230 (modify-syntax-entry ?\} "w" table)
231 (modify-syntax-entry ?` "w" table)
232 (modify-syntax-entry ?' "w" table)
233 (modify-syntax-entry ?^ "w" table)
234 (modify-syntax-entry ?- "w" table)
235 (modify-syntax-entry ?_ "w" table)
236 (modify-syntax-entry ?| "w" table)
237 (modify-syntax-entry ?\\ "w" table)
238 table)
239 "Syntax table used when buttonizing messages.
dfa0b1fc 240This syntax table should make all the valid nick characters word
597993cf
MB
241constituents.")
242
ff59d266
MB
243(defvar erc-button-keys-added nil
244 "Internal variable used to keep track of whether we've added the
245global-level ERC button keys yet.")
246
5e56b3fb 247(defun erc-button-setup ()
ff59d266 248 "Add ERC mode-level button movement keys. This is only done once."
5e56b3fb
MO
249 ;; Make XEmacs use `erc-button-face'.
250 (when (featurep 'xemacs)
251 (set (make-local-variable 'widget-button-face) nil))
252 ;; Add keys.
ff59d266
MB
253 (unless erc-button-keys-added
254 (define-key erc-mode-map (kbd "<backtab>") 'erc-button-previous)
255 (setq erc-button-keys-added t)))
256
597993cf
MB
257(defun erc-button-add-buttons ()
258 "Find external references in the current buffer and make buttons of them.
259\"External references\" are things like URLs, as
260specified by `erc-button-alist'."
261 (interactive)
262 (save-excursion
263 (with-syntax-table erc-button-syntax-table
264 (let ((buffer-read-only nil)
265 (inhibit-point-motion-hooks t)
266 (inhibit-field-text-motion t)
267 (alist erc-button-alist)
268 entry regexp data)
269 (erc-button-remove-old-buttons)
270 (dolist (entry alist)
271 (if (equal (car entry) (quote (quote nicknames)))
272 (erc-button-add-nickname-buttons entry)
273 (progn
274 (setq regexp (or (and (stringp (car entry)) (car entry))
275 (and (boundp (car entry))
276 (symbol-value (car entry)))))
277 (cond ((stringp regexp)
278 (erc-button-add-buttons-1 regexp entry))
279 ((and (listp regexp) (stringp (car regexp)))
280 (dolist (r regexp)
281 (erc-button-add-buttons-1
282 (concat "\\<" (regexp-quote r) "\\>")
283 entry)))
284 ((and (listp regexp) (listp (car regexp))
285 (stringp (caar regexp)))
286 (dolist (elem regexp)
287 (erc-button-add-buttons-1
288 (concat "\\<" (regexp-quote (car elem)) "\\>")
289 entry)))))))))))
290
291(defun erc-button-add-nickname-buttons (entry)
292 "Search through the buffer for nicknames, and add buttons."
293 (let ((form (nth 2 entry))
294 (fun (nth 3 entry))
295 bounds word)
296 (when (or (eq t form)
297 (eval form))
298 (goto-char (point-min))
299 (while (forward-word 1)
300 (setq bounds (bounds-of-thing-at-point 'word))
301 (setq word (buffer-substring-no-properties
302 (car bounds) (cdr bounds)))
5e56b3fb
MO
303 (when (or (and (erc-server-buffer-p) (erc-get-server-user word))
304 (and erc-channel-users (erc-get-channel-user word)))
305 (erc-button-add-button (car bounds) (cdr bounds)
306 fun t (list word)))))))
597993cf
MB
307
308(defun erc-button-add-buttons-1 (regexp entry)
309 "Search through the buffer for matches to ENTRY and add buttons."
310 (goto-char (point-min))
311 (while (re-search-forward regexp nil t)
312 (let ((start (match-beginning (nth 1 entry)))
313 (end (match-end (nth 1 entry)))
314 (form (nth 2 entry))
315 (fun (nth 3 entry))
316 (data (mapcar 'match-string (nthcdr 4 entry))))
317 (when (or (eq t form)
318 (eval form))
319 (erc-button-add-button start end fun nil data regexp)))))
320
321(defun erc-button-remove-old-buttons ()
322 "Remove all existing buttons.
323This is called with narrowing in effect, just before the text is
324buttonized again. Removing a button means to remove all the properties
325that `erc-button-add-button' adds, except for the face."
326 (remove-text-properties
327 (point-min) (point-max)
328 '(erc-callback nil
329 erc-data nil
330 mouse-face nil
331 keymap nil)))
332
333(defun erc-button-add-button (from to fun nick-p &optional data regexp)
334 "Create a button between FROM and TO with callback FUN and data DATA.
335NICK-P specifies if this is a nickname button.
336REGEXP is the regular expression which matched for this button."
337 ;; Really nasty hack to <URL: > ise urls, and line-wrap them if
338 ;; they're going to be wider than `erc-fill-column'.
339 ;; This could be a lot cleaner, but it works for me -- lawrence.
340 (let (fill-column)
341 (when (and erc-button-wrap-long-urls
342 (string= regexp erc-button-url-regexp)
343 (> (- to from)
344 (setq fill-column (- (if (numberp erc-button-wrap-long-urls)
345 erc-button-wrap-long-urls
346 erc-fill-column)
347 (length erc-fill-prefix)))))
348 (setq to (prog1 (point-marker) (insert ">"))
349 from (prog2 (goto-char from) (point-marker) (insert "<URL: ")))
350 (let ((pos (copy-marker from)))
351 (while (> (- to pos) fill-column)
352 (goto-char (+ pos fill-column))
353 (insert "\n" erc-fill-prefix) ; This ought to figure out
354 ; what type of filling we're
355 ; doing, and indent accordingly.
356 (move-marker pos (point))))))
357 (if nick-p
358 (when erc-button-nickname-face
359 (erc-button-add-face from to erc-button-nickname-face))
360 (when erc-button-face
361 (erc-button-add-face from to erc-button-face)))
362 (add-text-properties
363 from to
364 (nconc (and erc-button-mouse-face
365 (list 'mouse-face erc-button-mouse-face))
366 (list 'erc-callback fun)
367 (list 'keymap erc-button-keymap)
368 (list 'rear-nonsticky t)
369 (and data (list 'erc-data data))))
370 (widget-convert-button 'link from to :action 'erc-button-press-button
371 :suppress-face t
372 ;; Make XEmacs use our faces.
373 :button-face (if nick-p
374 erc-button-nickname-face
375 erc-button-face)
376 ;; Make XEmacs behave with mouse-clicks, for
377 ;; some reason, widget stuff overrides the
378 ;; 'keymap text-property.
379 :mouse-down-action 'erc-button-click-button))
380
381(defun erc-button-add-face (from to face)
382 "Add FACE to the region between FROM and TO."
383 ;; If we just use `add-text-property', then this will overwrite any
384 ;; face text property already used for the button. It will not be
385 ;; merged correctly. If we use overlays, then redisplay will be
386 ;; very slow with lots of buttons. This is why we manually merge
387 ;; face text properties.
388 (let ((old (erc-list (get-text-property from 'face)))
389 (pos from)
390 (end (next-single-property-change from 'face nil to))
391 new)
392 ;; old is the face at pos, in list form. It is nil if there is no
393 ;; face at pos. If nil, the new face is FACE. If not nil, the
394 ;; new face is a list containing FACE and the old stuff. end is
395 ;; where this face changes.
396 (while (< pos to)
397 (setq new (if old (cons face old) face))
398 (put-text-property pos end 'face new)
399 (setq pos end
400 old (erc-list (get-text-property pos 'face))
401 end (next-single-property-change pos 'face nil to)))))
402
403;; widget-button-click calls with two args, we ignore the first.
404;; Since Emacs runs this directly, rather than with
405;; widget-button-click, we need to fake an extra arg in the
406;; interactive spec.
407(defun erc-button-click-button (ignore event)
408 "Call `erc-button-press-button'."
409 (interactive "P\ne")
410 (save-excursion
411 (mouse-set-point event)
412 (erc-button-press-button)))
413
414;; XEmacs calls this via widget-button-press with a bunch of arguments
415;; which we don't care about.
416(defun erc-button-press-button (&rest ignore)
417 "Check text at point for a callback function.
418If the text at point has a `erc-callback' property,
419call it with the value of the `erc-data' text property."
420 (interactive)
421 (let* ((data (get-text-property (point) 'erc-data))
422 (fun (get-text-property (point) 'erc-callback)))
423 (unless fun
424 (message "No button at point"))
425 (when (and fun (symbolp fun) (not (fboundp fun)))
426 (error "Function %S is not bound" fun))
427 (apply fun data)))
428
429(defun erc-button-next ()
430 "Go to the next button in this buffer."
431 (interactive)
432 (let ((here (point)))
433 (when (< here (erc-beg-of-input-line))
434 (while (and (get-text-property here 'erc-callback)
435 (not (= here (point-max))))
436 (setq here (1+ here)))
437 (while (and (not (get-text-property here 'erc-callback))
438 (not (= here (point-max))))
439 (setq here (1+ here)))
440 (if (< here (point-max))
441 (goto-char here)
442 (error "No next button"))
443 t)))
444
d8bd1ad4
MB
445(defun erc-button-previous ()
446 "Go to the previous button in this buffer."
447 (interactive)
448 (let ((here (point)))
449 (when (< here (erc-beg-of-input-line))
450 (while (and (get-text-property here 'erc-callback)
451 (not (= here (point-min))))
452 (setq here (1- here)))
453 (while (and (not (get-text-property here 'erc-callback))
454 (not (= here (point-min))))
455 (setq here (1- here)))
456 (if (> here (point-min))
457 (goto-char here)
458 (error "No previous button"))
459 t)))
460
597993cf
MB
461(defun erc-browse-emacswiki (thing)
462 "Browse to thing in the emacs-wiki."
463 (browse-url (concat erc-emacswiki-url thing)))
464
465(defun erc-browse-emacswiki-lisp (thing)
466 "Browse to THING in the emacs-wiki elisp area."
467 (browse-url (concat erc-emacswiki-lisp-url thing)))
468
469;;; Nickname buttons:
470
471(defcustom erc-nick-popup-alist
472 '(("DeOp" . (erc-cmd-DEOP nick))
473 ("Kick" . (erc-cmd-KICK (concat nick " "
474 (read-from-minibuffer
475 (concat "Kick " nick ", reason: ")))))
476 ("Msg" . (erc-cmd-MSG (concat nick " "
477 (read-from-minibuffer
478 (concat "Message to " nick ": ")))))
479 ("Op" . (erc-cmd-OP nick))
480 ("Query" . (erc-cmd-QUERY nick))
481 ("Whois" . (erc-cmd-WHOIS nick))
482 ("Lastlog" . (erc-cmd-LASTLOG nick)))
483 "*An alist of possible actions to take on a nickname.
484An entry looks like (\"Action\" . SEXP) where SEXP is evaluated with
485the variable `nick' bound to the nick in question.
486
487Examples:
488 (\"DebianDB\" .
489 (shell-command
490 (format
491 \"ldapsearch -x -P 2 -h db.debian.org -b dc=debian,dc=org ircnick=%s\"
492 nick)))"
493 :group 'erc-button
494 :type '(repeat (cons (string :tag "Op")
495 sexp)))
496
497(defun erc-nick-popup (nick)
498 (let* ((completion-ignore-case t)
499 (action (completing-read (concat "What action to take on '" nick "'? ")
500 erc-nick-popup-alist))
501 (code (cdr (assoc action erc-nick-popup-alist))))
502 (when code
503 (erc-set-active-buffer (current-buffer))
504 (eval code))))
505
506;;; Callback functions
507(defun erc-button-describe-symbol (symbol-name)
508 "Describe SYMBOL-NAME.
509Use `describe-function' for functions, `describe-variable' for variables,
510and `apropos' for other symbols."
511 (let ((symbol (intern-soft symbol-name)))
512 (cond ((and symbol (fboundp symbol))
513 (describe-function symbol))
514 ((and symbol (boundp symbol))
515 (describe-variable symbol))
516 (t (apropos symbol-name)))))
517
518(defun erc-button-beats-to-time (beats)
519 "Display BEATS in a readable time format."
520 (let* ((seconds (- (* (string-to-number beats) 86.4)
521 3600
522 (- (car (current-time-zone)))))
523 (hours (mod (floor seconds 3600) 24))
524 (minutes (mod (round seconds 60) 60)))
525 (message (format "@%s is %d:%02d local time"
526 beats hours minutes))))
527
528(provide 'erc-button)
529
530;;; erc-button.el ends here
531;; Local Variables:
532;; indent-tabs-mode: nil
533;; End:
534
535;; arch-tag: 7d23bed4-2f30-4273-a03f-d7a274c605c4