merge trunk
[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
acaf905b 3;; Copyright (C) 1996-2004, 2006-2012 Free Software Foundation, Inc.
597993cf
MB
4
5;; Author: Mario Lang <mlang@delysid.org>
df5d5f59 6;; Maintainer: FSF
597993cf
MB
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.
865fe16f 29;; To enable, add to your init file:
597993cf
MB
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)
d4aa710a 57 (add-hook 'erc-complete-functions 'erc-button-next-function)
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)
d4aa710a 61 (remove-hook 'erc-complete-functions 'erc-button-next-function)
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
4b56d0fe 70(defface erc-button '((t :weight bold))
597993cf
MB
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"
fb7ada5f 119 "URL used to browse rfc references.
597993cf
MB
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"
fb7ada5f 125 "URL used to browse Google search references.
597993cf
MB
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)
2255788d 138;;; ("(\\(\\([^~\n \t@][^\n \t@]*\\)@\\([a-zA-Z0-9.:-]+\\)\\)" 1 t finger 2 3)
597993cf
MB
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))
fb7ada5f 159 "Alist of regexps matching buttons in ERC buffers.
597993cf
MB
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
2255788d 186 :version "24.3" ; remove finger (bug#4443)
597993cf
MB
187 :type '(repeat
188 (list :tag "Button"
189 (choice :tag "Matches"
190 regexp
191 (variable :tag "Variable containing regexp")
192 (const :tag "Nicknames" 'nicknames))
193 (integer :tag "Number of the regexp section that matches")
194 (choice :tag "When to buttonize"
195 (const :tag "Always" t)
196 (sexp :tag "Only when this evaluates to non-nil"))
197 (function :tag "Function to call when button is pressed")
198 (repeat :tag "Sections of regexp to send to the function"
199 :inline t
200 (integer :tag "Regexp section number")))))
201
202(defcustom erc-emacswiki-url "http://www.emacswiki.org/cgi-bin/wiki.pl?"
fb7ada5f 203 "URL of the EmacsWiki Homepage."
597993cf
MB
204 :group 'erc-button
205 :type 'string)
206
207(defcustom erc-emacswiki-lisp-url "http://www.emacswiki.org/elisp/"
fb7ada5f 208 "URL of the EmacsWiki ELisp area."
597993cf
MB
209 :group 'erc-button
210 :type 'string)
211
212(defvar erc-button-keymap
213 (let ((map (make-sparse-keymap)))
214 (define-key map (kbd "RET") 'erc-button-press-button)
215 (if (featurep 'xemacs)
216 (define-key map (kbd "<button2>") 'erc-button-click-button)
217 (define-key map (kbd "<mouse-2>") 'erc-button-click-button))
218 (define-key map (kbd "TAB") 'erc-button-next)
c89e5cd7 219 (define-key map (kbd "<backtab>") 'erc-button-previous)
081bd373 220 (define-key map [follow-link] 'mouse-face)
597993cf
MB
221 (set-keymap-parent map erc-mode-map)
222 map)
223 "Local keymap for ERC buttons.")
224
225(defvar erc-button-syntax-table
226 (let ((table (make-syntax-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 (modify-syntax-entry ?| "w" table)
239 (modify-syntax-entry ?\\ "w" table)
240 table)
241 "Syntax table used when buttonizing messages.
dfa0b1fc 242This syntax table should make all the valid nick characters word
597993cf
MB
243constituents.")
244
ff59d266
MB
245(defvar erc-button-keys-added nil
246 "Internal variable used to keep track of whether we've added the
247global-level ERC button keys yet.")
248
5e56b3fb 249(defun erc-button-setup ()
ff59d266 250 "Add ERC mode-level button movement keys. This is only done once."
5e56b3fb
MO
251 ;; Make XEmacs use `erc-button-face'.
252 (when (featurep 'xemacs)
253 (set (make-local-variable 'widget-button-face) nil))
254 ;; Add keys.
ff59d266
MB
255 (unless erc-button-keys-added
256 (define-key erc-mode-map (kbd "<backtab>") 'erc-button-previous)
257 (setq erc-button-keys-added t)))
258
597993cf
MB
259(defun erc-button-add-buttons ()
260 "Find external references in the current buffer and make buttons of them.
261\"External references\" are things like URLs, as
262specified by `erc-button-alist'."
263 (interactive)
264 (save-excursion
265 (with-syntax-table erc-button-syntax-table
266 (let ((buffer-read-only nil)
267 (inhibit-point-motion-hooks t)
268 (inhibit-field-text-motion t)
269 (alist erc-button-alist)
270 entry regexp data)
271 (erc-button-remove-old-buttons)
272 (dolist (entry alist)
273 (if (equal (car entry) (quote (quote nicknames)))
274 (erc-button-add-nickname-buttons entry)
275 (progn
276 (setq regexp (or (and (stringp (car entry)) (car entry))
277 (and (boundp (car entry))
278 (symbol-value (car entry)))))
279 (cond ((stringp regexp)
280 (erc-button-add-buttons-1 regexp entry))
281 ((and (listp regexp) (stringp (car regexp)))
282 (dolist (r regexp)
283 (erc-button-add-buttons-1
284 (concat "\\<" (regexp-quote r) "\\>")
285 entry)))
286 ((and (listp regexp) (listp (car regexp))
287 (stringp (caar regexp)))
288 (dolist (elem regexp)
289 (erc-button-add-buttons-1
290 (concat "\\<" (regexp-quote (car elem)) "\\>")
291 entry)))))))))))
292
293(defun erc-button-add-nickname-buttons (entry)
294 "Search through the buffer for nicknames, and add buttons."
295 (let ((form (nth 2 entry))
296 (fun (nth 3 entry))
297 bounds word)
298 (when (or (eq t form)
299 (eval form))
300 (goto-char (point-min))
301 (while (forward-word 1)
302 (setq bounds (bounds-of-thing-at-point 'word))
303 (setq word (buffer-substring-no-properties
304 (car bounds) (cdr bounds)))
5e56b3fb
MO
305 (when (or (and (erc-server-buffer-p) (erc-get-server-user word))
306 (and erc-channel-users (erc-get-channel-user word)))
307 (erc-button-add-button (car bounds) (cdr bounds)
308 fun t (list word)))))))
597993cf
MB
309
310(defun erc-button-add-buttons-1 (regexp entry)
311 "Search through the buffer for matches to ENTRY and add buttons."
312 (goto-char (point-min))
313 (while (re-search-forward regexp nil t)
314 (let ((start (match-beginning (nth 1 entry)))
315 (end (match-end (nth 1 entry)))
316 (form (nth 2 entry))
317 (fun (nth 3 entry))
318 (data (mapcar 'match-string (nthcdr 4 entry))))
319 (when (or (eq t form)
320 (eval form))
321 (erc-button-add-button start end fun nil data regexp)))))
322
323(defun erc-button-remove-old-buttons ()
324 "Remove all existing buttons.
325This is called with narrowing in effect, just before the text is
326buttonized again. Removing a button means to remove all the properties
327that `erc-button-add-button' adds, except for the face."
328 (remove-text-properties
329 (point-min) (point-max)
330 '(erc-callback nil
331 erc-data nil
332 mouse-face nil
333 keymap nil)))
334
335(defun erc-button-add-button (from to fun nick-p &optional data regexp)
336 "Create a button between FROM and TO with callback FUN and data DATA.
337NICK-P specifies if this is a nickname button.
338REGEXP is the regular expression which matched for this button."
339 ;; Really nasty hack to <URL: > ise urls, and line-wrap them if
340 ;; they're going to be wider than `erc-fill-column'.
341 ;; This could be a lot cleaner, but it works for me -- lawrence.
342 (let (fill-column)
343 (when (and erc-button-wrap-long-urls
344 (string= regexp erc-button-url-regexp)
345 (> (- to from)
346 (setq fill-column (- (if (numberp erc-button-wrap-long-urls)
347 erc-button-wrap-long-urls
348 erc-fill-column)
349 (length erc-fill-prefix)))))
350 (setq to (prog1 (point-marker) (insert ">"))
351 from (prog2 (goto-char from) (point-marker) (insert "<URL: ")))
352 (let ((pos (copy-marker from)))
353 (while (> (- to pos) fill-column)
354 (goto-char (+ pos fill-column))
355 (insert "\n" erc-fill-prefix) ; This ought to figure out
356 ; what type of filling we're
357 ; doing, and indent accordingly.
358 (move-marker pos (point))))))
359 (if nick-p
360 (when erc-button-nickname-face
361 (erc-button-add-face from to erc-button-nickname-face))
362 (when erc-button-face
363 (erc-button-add-face from to erc-button-face)))
364 (add-text-properties
365 from to
366 (nconc (and erc-button-mouse-face
367 (list 'mouse-face erc-button-mouse-face))
368 (list 'erc-callback fun)
369 (list 'keymap erc-button-keymap)
370 (list 'rear-nonsticky t)
371 (and data (list 'erc-data data))))
9c7b2dd3
JB
372 (when (featurep 'xemacs)
373 (widget-convert-button 'link from to :action 'erc-button-press-button
374 :suppress-face t
375 ;; Make XEmacs use our faces.
376 :button-face (if nick-p
377 erc-button-nickname-face
378 erc-button-face)
379 ;; Make XEmacs behave with mouse-clicks, for
380 ;; some reason, widget stuff overrides the
381 ;; 'keymap text-property.
382 :mouse-down-action 'erc-button-click-button)))
597993cf
MB
383
384(defun erc-button-add-face (from to face)
385 "Add FACE to the region between FROM and TO."
386 ;; If we just use `add-text-property', then this will overwrite any
387 ;; face text property already used for the button. It will not be
388 ;; merged correctly. If we use overlays, then redisplay will be
389 ;; very slow with lots of buttons. This is why we manually merge
390 ;; face text properties.
391 (let ((old (erc-list (get-text-property from 'face)))
392 (pos from)
393 (end (next-single-property-change from 'face nil to))
394 new)
395 ;; old is the face at pos, in list form. It is nil if there is no
396 ;; face at pos. If nil, the new face is FACE. If not nil, the
397 ;; new face is a list containing FACE and the old stuff. end is
398 ;; where this face changes.
399 (while (< pos to)
400 (setq new (if old (cons face old) face))
401 (put-text-property pos end 'face new)
402 (setq pos end
403 old (erc-list (get-text-property pos 'face))
404 end (next-single-property-change pos 'face nil to)))))
405
406;; widget-button-click calls with two args, we ignore the first.
407;; Since Emacs runs this directly, rather than with
408;; widget-button-click, we need to fake an extra arg in the
409;; interactive spec.
410(defun erc-button-click-button (ignore event)
411 "Call `erc-button-press-button'."
412 (interactive "P\ne")
413 (save-excursion
414 (mouse-set-point event)
415 (erc-button-press-button)))
416
417;; XEmacs calls this via widget-button-press with a bunch of arguments
418;; which we don't care about.
419(defun erc-button-press-button (&rest ignore)
420 "Check text at point for a callback function.
421If the text at point has a `erc-callback' property,
422call it with the value of the `erc-data' text property."
423 (interactive)
424 (let* ((data (get-text-property (point) 'erc-data))
425 (fun (get-text-property (point) 'erc-callback)))
426 (unless fun
427 (message "No button at point"))
428 (when (and fun (symbolp fun) (not (fboundp fun)))
429 (error "Function %S is not bound" fun))
430 (apply fun data)))
431
d4aa710a
SM
432(defun erc-button-next-function ()
433 "Pseudo completion function that actually jumps to the next button.
434For use on `completion-at-point-functions'."
953ea722
AL
435 (when (< (point) (erc-beg-of-input-line))
436 `(lambda ()
437 (let ((here ,(point)))
438 (while (and (get-text-property here 'erc-callback)
439 (not (= here (point-max))))
440 (setq here (1+ here)))
441 (while (and (not (get-text-property here 'erc-callback))
442 (not (= here (point-max))))
443 (setq here (1+ here)))
444 (if (< here (point-max))
445 (goto-char here)
446 (error "No next button"))
447 t))))
d4aa710a 448
597993cf
MB
449(defun erc-button-next ()
450 "Go to the next button in this buffer."
451 (interactive)
d4aa710a
SM
452 (let ((f (erc-button-next-function)))
453 (if f (funcall f))))
597993cf 454
d8bd1ad4
MB
455(defun erc-button-previous ()
456 "Go to the previous button in this buffer."
457 (interactive)
458 (let ((here (point)))
459 (when (< here (erc-beg-of-input-line))
460 (while (and (get-text-property here 'erc-callback)
461 (not (= here (point-min))))
462 (setq here (1- here)))
463 (while (and (not (get-text-property here 'erc-callback))
464 (not (= here (point-min))))
465 (setq here (1- here)))
466 (if (> here (point-min))
467 (goto-char here)
468 (error "No previous button"))
469 t)))
470
597993cf
MB
471(defun erc-browse-emacswiki (thing)
472 "Browse to thing in the emacs-wiki."
473 (browse-url (concat erc-emacswiki-url thing)))
474
475(defun erc-browse-emacswiki-lisp (thing)
476 "Browse to THING in the emacs-wiki elisp area."
477 (browse-url (concat erc-emacswiki-lisp-url thing)))
478
479;;; Nickname buttons:
480
481(defcustom erc-nick-popup-alist
482 '(("DeOp" . (erc-cmd-DEOP nick))
483 ("Kick" . (erc-cmd-KICK (concat nick " "
484 (read-from-minibuffer
485 (concat "Kick " nick ", reason: ")))))
486 ("Msg" . (erc-cmd-MSG (concat nick " "
487 (read-from-minibuffer
488 (concat "Message to " nick ": ")))))
489 ("Op" . (erc-cmd-OP nick))
490 ("Query" . (erc-cmd-QUERY nick))
491 ("Whois" . (erc-cmd-WHOIS nick))
492 ("Lastlog" . (erc-cmd-LASTLOG nick)))
fb7ada5f 493 "An alist of possible actions to take on a nickname.
597993cf
MB
494An entry looks like (\"Action\" . SEXP) where SEXP is evaluated with
495the variable `nick' bound to the nick in question.
496
497Examples:
498 (\"DebianDB\" .
499 (shell-command
500 (format
501 \"ldapsearch -x -P 2 -h db.debian.org -b dc=debian,dc=org ircnick=%s\"
502 nick)))"
503 :group 'erc-button
504 :type '(repeat (cons (string :tag "Op")
505 sexp)))
506
507(defun erc-nick-popup (nick)
508 (let* ((completion-ignore-case t)
509 (action (completing-read (concat "What action to take on '" nick "'? ")
510 erc-nick-popup-alist))
511 (code (cdr (assoc action erc-nick-popup-alist))))
512 (when code
513 (erc-set-active-buffer (current-buffer))
514 (eval code))))
515
516;;; Callback functions
517(defun erc-button-describe-symbol (symbol-name)
518 "Describe SYMBOL-NAME.
519Use `describe-function' for functions, `describe-variable' for variables,
520and `apropos' for other symbols."
521 (let ((symbol (intern-soft symbol-name)))
522 (cond ((and symbol (fboundp symbol))
523 (describe-function symbol))
524 ((and symbol (boundp symbol))
525 (describe-variable symbol))
526 (t (apropos symbol-name)))))
527
528(defun erc-button-beats-to-time (beats)
529 "Display BEATS in a readable time format."
530 (let* ((seconds (- (* (string-to-number beats) 86.4)
531 3600
532 (- (car (current-time-zone)))))
533 (hours (mod (floor seconds 3600) 24))
534 (minutes (mod (round seconds 60) 60)))
535 (message (format "@%s is %d:%02d local time"
536 beats hours minutes))))
537
538(provide 'erc-button)
539
540;;; erc-button.el ends here
541;; Local Variables:
542;; indent-tabs-mode: nil
543;; End:
544