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