]> code.delx.au - gnu-emacs/blob - lisp/erc/erc-button.el
Add 2012 to FSF copyright years for Emacs files (do not merge to trunk)
[gnu-emacs] / lisp / erc / erc-button.el
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,
4 ;; 2006, 2007, 2008, 2009, 2010, 2011, 2012 Free Software Foundation, Inc.
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
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 3 of the License, or
15 ;; (at your option) any later version.
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
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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)
57 (add-hook 'erc-complete-functions 'erc-button-next)
58 (add-hook 'erc-mode-hook 'erc-button-setup))
59 ((remove-hook 'erc-insert-modify-hook 'erc-button-add-buttons)
60 (remove-hook 'erc-send-modify-hook 'erc-button-add-buttons)
61 (remove-hook 'erc-complete-functions 'erc-button-next)
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))))))
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
77 A 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
90 Buttons will be displayed in this face when the mouse cursor is
91 above 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]*\\)?"
99 "[-a-zA-Z0-9_=!?#$@~`%&*+\\/:;.,()]+[-a-zA-Z0-9_=#$@~`%&*+\\/()]")
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
107 If this variable is a number, consider URLs longer than its value to
108 be \"long\". If t, URLs will be considered \"long\" if they are
109 longer 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.
160 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
161
162 REGEXP 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
171 BUTTON is the number of the regexp grouping actually matching the
172 button, This is ignored if REGEXP is 'nicknames.
173
174 FORM is a lisp expression which must eval to true for the button to
175 be added,
176
177 CALLBACK 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
181 PAR 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)
218 (define-key map (kbd "<backtab>") 'erc-button-previous)
219 (define-key map [follow-link] 'mouse-face)
220 (set-keymap-parent map erc-mode-map)
221 map)
222 "Local keymap for ERC buttons.")
223
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 valid nick characters word
242 constituents.")
243
244 (defvar erc-button-keys-added nil
245 "Internal variable used to keep track of whether we've added the
246 global-level ERC button keys yet.")
247
248 (defun erc-button-setup ()
249 "Add ERC mode-level button movement keys. This is only done once."
250 ;; Make XEmacs use `erc-button-face'.
251 (when (featurep 'xemacs)
252 (set (make-local-variable 'widget-button-face) nil))
253 ;; Add keys.
254 (unless erc-button-keys-added
255 (define-key erc-mode-map (kbd "<backtab>") 'erc-button-previous)
256 (setq erc-button-keys-added t)))
257
258 (defun erc-button-add-buttons ()
259 "Find external references in the current buffer and make buttons of them.
260 \"External references\" are things like URLs, as
261 specified by `erc-button-alist'."
262 (interactive)
263 (save-excursion
264 (with-syntax-table erc-button-syntax-table
265 (let ((buffer-read-only nil)
266 (inhibit-point-motion-hooks t)
267 (inhibit-field-text-motion t)
268 (alist erc-button-alist)
269 entry regexp data)
270 (erc-button-remove-old-buttons)
271 (dolist (entry alist)
272 (if (equal (car entry) (quote (quote nicknames)))
273 (erc-button-add-nickname-buttons entry)
274 (progn
275 (setq regexp (or (and (stringp (car entry)) (car entry))
276 (and (boundp (car entry))
277 (symbol-value (car entry)))))
278 (cond ((stringp regexp)
279 (erc-button-add-buttons-1 regexp entry))
280 ((and (listp regexp) (stringp (car regexp)))
281 (dolist (r regexp)
282 (erc-button-add-buttons-1
283 (concat "\\<" (regexp-quote r) "\\>")
284 entry)))
285 ((and (listp regexp) (listp (car regexp))
286 (stringp (caar regexp)))
287 (dolist (elem regexp)
288 (erc-button-add-buttons-1
289 (concat "\\<" (regexp-quote (car elem)) "\\>")
290 entry)))))))))))
291
292 (defun erc-button-add-nickname-buttons (entry)
293 "Search through the buffer for nicknames, and add buttons."
294 (let ((form (nth 2 entry))
295 (fun (nth 3 entry))
296 bounds word)
297 (when (or (eq t form)
298 (eval form))
299 (goto-char (point-min))
300 (while (forward-word 1)
301 (setq bounds (bounds-of-thing-at-point 'word))
302 (setq word (buffer-substring-no-properties
303 (car bounds) (cdr bounds)))
304 (when (or (and (erc-server-buffer-p) (erc-get-server-user word))
305 (and erc-channel-users (erc-get-channel-user word)))
306 (erc-button-add-button (car bounds) (cdr bounds)
307 fun t (list word)))))))
308
309 (defun erc-button-add-buttons-1 (regexp entry)
310 "Search through the buffer for matches to ENTRY and add buttons."
311 (goto-char (point-min))
312 (while (re-search-forward regexp nil t)
313 (let ((start (match-beginning (nth 1 entry)))
314 (end (match-end (nth 1 entry)))
315 (form (nth 2 entry))
316 (fun (nth 3 entry))
317 (data (mapcar 'match-string (nthcdr 4 entry))))
318 (when (or (eq t form)
319 (eval form))
320 (erc-button-add-button start end fun nil data regexp)))))
321
322 (defun erc-button-remove-old-buttons ()
323 "Remove all existing buttons.
324 This is called with narrowing in effect, just before the text is
325 buttonized again. Removing a button means to remove all the properties
326 that `erc-button-add-button' adds, except for the face."
327 (remove-text-properties
328 (point-min) (point-max)
329 '(erc-callback nil
330 erc-data nil
331 mouse-face nil
332 keymap nil)))
333
334 (defun erc-button-add-button (from to fun nick-p &optional data regexp)
335 "Create a button between FROM and TO with callback FUN and data DATA.
336 NICK-P specifies if this is a nickname button.
337 REGEXP is the regular expression which matched for this button."
338 ;; Really nasty hack to <URL: > ise urls, and line-wrap them if
339 ;; they're going to be wider than `erc-fill-column'.
340 ;; This could be a lot cleaner, but it works for me -- lawrence.
341 (let (fill-column)
342 (when (and erc-button-wrap-long-urls
343 (string= regexp erc-button-url-regexp)
344 (> (- to from)
345 (setq fill-column (- (if (numberp erc-button-wrap-long-urls)
346 erc-button-wrap-long-urls
347 erc-fill-column)
348 (length erc-fill-prefix)))))
349 (setq to (prog1 (point-marker) (insert ">"))
350 from (prog2 (goto-char from) (point-marker) (insert "<URL: ")))
351 (let ((pos (copy-marker from)))
352 (while (> (- to pos) fill-column)
353 (goto-char (+ pos fill-column))
354 (insert "\n" erc-fill-prefix) ; This ought to figure out
355 ; what type of filling we're
356 ; doing, and indent accordingly.
357 (move-marker pos (point))))))
358 (if nick-p
359 (when erc-button-nickname-face
360 (erc-button-add-face from to erc-button-nickname-face))
361 (when erc-button-face
362 (erc-button-add-face from to erc-button-face)))
363 (add-text-properties
364 from to
365 (nconc (and erc-button-mouse-face
366 (list 'mouse-face erc-button-mouse-face))
367 (list 'erc-callback fun)
368 (list 'keymap erc-button-keymap)
369 (list 'rear-nonsticky t)
370 (and data (list 'erc-data data))))
371 (when (featurep 'xemacs)
372 (widget-convert-button 'link from to :action 'erc-button-press-button
373 :suppress-face t
374 ;; Make XEmacs use our faces.
375 :button-face (if nick-p
376 erc-button-nickname-face
377 erc-button-face)
378 ;; Make XEmacs behave with mouse-clicks, for
379 ;; some reason, widget stuff overrides the
380 ;; 'keymap text-property.
381 :mouse-down-action 'erc-button-click-button)))
382
383 (defun erc-button-add-face (from to face)
384 "Add FACE to the region between FROM and TO."
385 ;; If we just use `add-text-property', then this will overwrite any
386 ;; face text property already used for the button. It will not be
387 ;; merged correctly. If we use overlays, then redisplay will be
388 ;; very slow with lots of buttons. This is why we manually merge
389 ;; face text properties.
390 (let ((old (erc-list (get-text-property from 'face)))
391 (pos from)
392 (end (next-single-property-change from 'face nil to))
393 new)
394 ;; old is the face at pos, in list form. It is nil if there is no
395 ;; face at pos. If nil, the new face is FACE. If not nil, the
396 ;; new face is a list containing FACE and the old stuff. end is
397 ;; where this face changes.
398 (while (< pos to)
399 (setq new (if old (cons face old) face))
400 (put-text-property pos end 'face new)
401 (setq pos end
402 old (erc-list (get-text-property pos 'face))
403 end (next-single-property-change pos 'face nil to)))))
404
405 ;; widget-button-click calls with two args, we ignore the first.
406 ;; Since Emacs runs this directly, rather than with
407 ;; widget-button-click, we need to fake an extra arg in the
408 ;; interactive spec.
409 (defun erc-button-click-button (ignore event)
410 "Call `erc-button-press-button'."
411 (interactive "P\ne")
412 (save-excursion
413 (mouse-set-point event)
414 (erc-button-press-button)))
415
416 ;; XEmacs calls this via widget-button-press with a bunch of arguments
417 ;; which we don't care about.
418 (defun erc-button-press-button (&rest ignore)
419 "Check text at point for a callback function.
420 If the text at point has a `erc-callback' property,
421 call it with the value of the `erc-data' text property."
422 (interactive)
423 (let* ((data (get-text-property (point) 'erc-data))
424 (fun (get-text-property (point) 'erc-callback)))
425 (unless fun
426 (message "No button at point"))
427 (when (and fun (symbolp fun) (not (fboundp fun)))
428 (error "Function %S is not bound" fun))
429 (apply fun data)))
430
431 (defun erc-button-next ()
432 "Go to the next 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-max))))
438 (setq here (1+ here)))
439 (while (and (not (get-text-property here 'erc-callback))
440 (not (= here (point-max))))
441 (setq here (1+ here)))
442 (if (< here (point-max))
443 (goto-char here)
444 (error "No next button"))
445 t)))
446
447 (defun erc-button-previous ()
448 "Go to the previous button in this buffer."
449 (interactive)
450 (let ((here (point)))
451 (when (< here (erc-beg-of-input-line))
452 (while (and (get-text-property here 'erc-callback)
453 (not (= here (point-min))))
454 (setq here (1- here)))
455 (while (and (not (get-text-property here 'erc-callback))
456 (not (= here (point-min))))
457 (setq here (1- here)))
458 (if (> here (point-min))
459 (goto-char here)
460 (error "No previous button"))
461 t)))
462
463 (defun erc-browse-emacswiki (thing)
464 "Browse to thing in the emacs-wiki."
465 (browse-url (concat erc-emacswiki-url thing)))
466
467 (defun erc-browse-emacswiki-lisp (thing)
468 "Browse to THING in the emacs-wiki elisp area."
469 (browse-url (concat erc-emacswiki-lisp-url thing)))
470
471 ;;; Nickname buttons:
472
473 (defcustom erc-nick-popup-alist
474 '(("DeOp" . (erc-cmd-DEOP nick))
475 ("Kick" . (erc-cmd-KICK (concat nick " "
476 (read-from-minibuffer
477 (concat "Kick " nick ", reason: ")))))
478 ("Msg" . (erc-cmd-MSG (concat nick " "
479 (read-from-minibuffer
480 (concat "Message to " nick ": ")))))
481 ("Op" . (erc-cmd-OP nick))
482 ("Query" . (erc-cmd-QUERY nick))
483 ("Whois" . (erc-cmd-WHOIS nick))
484 ("Lastlog" . (erc-cmd-LASTLOG nick)))
485 "*An alist of possible actions to take on a nickname.
486 An entry looks like (\"Action\" . SEXP) where SEXP is evaluated with
487 the variable `nick' bound to the nick in question.
488
489 Examples:
490 (\"DebianDB\" .
491 (shell-command
492 (format
493 \"ldapsearch -x -P 2 -h db.debian.org -b dc=debian,dc=org ircnick=%s\"
494 nick)))"
495 :group 'erc-button
496 :type '(repeat (cons (string :tag "Op")
497 sexp)))
498
499 (defun erc-nick-popup (nick)
500 (let* ((completion-ignore-case t)
501 (action (completing-read (concat "What action to take on '" nick "'? ")
502 erc-nick-popup-alist))
503 (code (cdr (assoc action erc-nick-popup-alist))))
504 (when code
505 (erc-set-active-buffer (current-buffer))
506 (eval code))))
507
508 ;;; Callback functions
509 (defun erc-button-describe-symbol (symbol-name)
510 "Describe SYMBOL-NAME.
511 Use `describe-function' for functions, `describe-variable' for variables,
512 and `apropos' for other symbols."
513 (let ((symbol (intern-soft symbol-name)))
514 (cond ((and symbol (fboundp symbol))
515 (describe-function symbol))
516 ((and symbol (boundp symbol))
517 (describe-variable symbol))
518 (t (apropos symbol-name)))))
519
520 (defun erc-button-beats-to-time (beats)
521 "Display BEATS in a readable time format."
522 (let* ((seconds (- (* (string-to-number beats) 86.4)
523 3600
524 (- (car (current-time-zone)))))
525 (hours (mod (floor seconds 3600) 24))
526 (minutes (mod (round seconds 60) 60)))
527 (message (format "@%s is %d:%02d local time"
528 beats hours minutes))))
529
530 (provide 'erc-button)
531
532 ;;; erc-button.el ends here
533 ;; Local Variables:
534 ;; indent-tabs-mode: nil
535 ;; End:
536
537 ;; arch-tag: 7d23bed4-2f30-4273-a03f-d7a274c605c4