Update docstrings and comments to use "init file" terminology.
[emacs.git] / lisp / erc / erc-button.el
bloba0593dcb7435c475093bd3778468ff121d6fa71c
1 ;; erc-button.el --- A way of buttonizing certain things in ERC buffers
3 ;; Copyright (C) 1996-2004, 2006-2012 Free Software Foundation, Inc.
5 ;; Author: Mario Lang <mlang@delysid.org>
6 ;; Keywords: irc, button, url, regexp
7 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcButton
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs 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 3 of the License, or
14 ;; (at your option) any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
26 ;; Heavily borrowed from gnus-art.el. Thanks to the original authors.
27 ;; This buttonizes nicks and other stuff to make it all clickable.
28 ;; To enable, add to your init file:
29 ;; (require 'erc-button)
30 ;; (erc-button-mode 1)
32 ;; Todo:
33 ;; * Rewrite all this to do the same, but use button.el from GNU Emacs
34 ;; if it's available for xemacs too. Why? button.el is much faster,
35 ;; and much more elegant, and solves the problem we get with large buffers
36 ;; and a large erc-button-marker-list.
39 ;;; Code:
41 (require 'erc)
42 (require 'wid-edit)
43 (require 'erc-fill)
45 ;;; Minor Mode
47 (defgroup erc-button nil
48 "Define how text can be turned into clickable buttons."
49 :group 'erc)
51 ;;;###autoload (autoload 'erc-button-mode "erc-button" nil t)
52 (define-erc-module button nil
53 "This mode buttonizes all messages according to `erc-button-alist'."
54 ((add-hook 'erc-insert-modify-hook 'erc-button-add-buttons 'append)
55 (add-hook 'erc-send-modify-hook 'erc-button-add-buttons 'append)
56 (add-hook 'erc-complete-functions 'erc-button-next-function)
57 (add-hook 'erc-mode-hook 'erc-button-setup))
58 ((remove-hook 'erc-insert-modify-hook 'erc-button-add-buttons)
59 (remove-hook 'erc-send-modify-hook 'erc-button-add-buttons)
60 (remove-hook 'erc-complete-functions 'erc-button-next-function)
61 (remove-hook 'erc-mode-hook 'erc-button-setup)
62 (when (featurep 'xemacs)
63 (dolist (buffer (erc-buffer-list))
64 (with-current-buffer buffer
65 (kill-local-variable 'widget-button-face))))))
67 ;;; Variables
69 (defface erc-button '((t :weight bold))
70 "ERC button face."
71 :group 'erc-faces)
73 (defcustom erc-button-face 'erc-button
74 "Face used for highlighting buttons in ERC buffers.
76 A button is a piece of text that you can activate by pressing
77 `RET' or `mouse-2' above it. See also `erc-button-keymap'."
78 :type 'face
79 :group 'erc-faces)
81 (defcustom erc-button-nickname-face 'erc-nick-default-face
82 "Face used for ERC nickname buttons."
83 :type 'face
84 :group 'erc-faces)
86 (defcustom erc-button-mouse-face 'highlight
87 "Face used for mouse highlighting in ERC buffers.
89 Buttons will be displayed in this face when the mouse cursor is
90 above them."
91 :type 'face
92 :group 'erc-faces)
94 (defcustom erc-button-url-regexp
95 (concat "\\(www\\.\\|\\(s?https?\\|"
96 "ftp\\|file\\|gopher\\|news\\|telnet\\|wais\\|mailto\\):\\)"
97 "\\(//[-a-zA-Z0-9_.]+:[0-9]*\\)?"
98 "[-a-zA-Z0-9_=!?#$@~`%&*+\\/:;.,()]+[-a-zA-Z0-9_=#$@~`%&*+\\/()]")
99 "Regular expression that matches URLs."
100 :group 'erc-button
101 :type 'regexp)
103 (defcustom erc-button-wrap-long-urls nil
104 "If non-nil, \"long\" URLs matching `erc-button-url-regexp' will be wrapped.
106 If this variable is a number, consider URLs longer than its value to
107 be \"long\". If t, URLs will be considered \"long\" if they are
108 longer than `erc-fill-column'."
109 :group 'erc-button
110 :type '(choice integer boolean))
112 (defcustom erc-button-buttonize-nicks t
113 "Flag indicating whether nicks should be buttonized or not."
114 :group 'erc-button
115 :type 'boolean)
117 (defcustom erc-button-rfc-url "http://www.faqs.org/rfcs/rfc%s.html"
118 "URL used to browse rfc references.
119 %s is replaced by the number."
120 :group 'erc-button
121 :type 'string)
123 (defcustom erc-button-google-url "http://www.google.com/search?q=%s"
124 "URL used to browse Google search references.
125 %s is replaced by the search string."
126 :group 'erc-button
127 :type 'string)
129 (defcustom erc-button-alist
130 ;; Since the callback is only executed when the user is clicking on
131 ;; a button, it makes no sense to optimize performance by
132 ;; bytecompiling lambdas in this alist. On the other hand, it makes
133 ;; things hard to maintain.
134 '(('nicknames 0 erc-button-buttonize-nicks erc-nick-popup 0)
135 (erc-button-url-regexp 0 t browse-url 0)
136 ("<URL: *\\([^<> ]+\\) *>" 0 t browse-url 1)
137 ("(\\(\\([^~\n \t@][^\n \t@]*\\)@\\([a-zA-Z0-9.:-]+\\)\\)" 1 t finger 2 3)
138 ;; emacs internal
139 ("[`]\\([a-zA-Z][-a-zA-Z_0-9]+\\)[']" 1 t erc-button-describe-symbol 1)
140 ;; pseudo links
141 ("\\bInfo:[\"]\\([^\"]+\\)[\"]" 0 t Info-goto-node 1)
142 ("\\b\\(Ward\\|Wiki\\|WardsWiki\\|TheWiki\\):\\([A-Z][a-z]+\\([A-Z][a-z]+\\)+\\)"
143 0 t (lambda (page)
144 (browse-url (concat "http://c2.com/cgi-bin/wiki?" page)))
146 ("EmacsWiki:\\([A-Z][a-z]+\\([A-Z][a-z]+\\)+\\)" 0 t erc-browse-emacswiki 1)
147 ("Lisp:\\([a-zA-Z.+-]+\\)" 0 t erc-browse-emacswiki-lisp 1)
148 ("\\bGoogle:\\([^ \t\n\r\f]+\\)"
149 0 t (lambda (keywords)
150 (browse-url (format erc-button-google-url keywords)))
152 ("\\brfc[#: ]?\\([0-9]+\\)"
153 0 t (lambda (num)
154 (browse-url (format erc-button-rfc-url num)))
156 ;; other
157 ("\\s-\\(@\\([0-9][0-9][0-9]\\)\\)" 1 t erc-button-beats-to-time 2))
158 "Alist of regexps matching buttons in ERC buffers.
159 Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
161 REGEXP is the string matching text around the button or a symbol
162 indicating a variable holding that string, or a list of
163 strings, or an alist with the strings in the car. Note that
164 entries in lists or alists are considered to be nicks or other
165 complete words. Therefore they are enclosed in \\< and \\>
166 while searching. REGEXP can also be the quoted symbol
167 'nicknames, which matches the nickname of any user on the
168 current server.
170 BUTTON is the number of the regexp grouping actually matching the
171 button, This is ignored if REGEXP is 'nicknames.
173 FORM is a lisp expression which must eval to true for the button to
174 be added,
176 CALLBACK is the function to call when the user push this button.
177 CALLBACK can also be a symbol. Its variable value will be used
178 as the callback function.
180 PAR is a number of a regexp grouping whose text will be passed to
181 CALLBACK. There can be several PAR arguments. If REGEXP is
182 'nicknames, these are ignored, and CALLBACK will be called with
183 the nickname matched as the argument."
184 :group 'erc-button
185 :type '(repeat
186 (list :tag "Button"
187 (choice :tag "Matches"
188 regexp
189 (variable :tag "Variable containing regexp")
190 (const :tag "Nicknames" 'nicknames))
191 (integer :tag "Number of the regexp section that matches")
192 (choice :tag "When to buttonize"
193 (const :tag "Always" t)
194 (sexp :tag "Only when this evaluates to non-nil"))
195 (function :tag "Function to call when button is pressed")
196 (repeat :tag "Sections of regexp to send to the function"
197 :inline t
198 (integer :tag "Regexp section number")))))
200 (defcustom erc-emacswiki-url "http://www.emacswiki.org/cgi-bin/wiki.pl?"
201 "URL of the EmacsWiki Homepage."
202 :group 'erc-button
203 :type 'string)
205 (defcustom erc-emacswiki-lisp-url "http://www.emacswiki.org/elisp/"
206 "URL of the EmacsWiki ELisp area."
207 :group 'erc-button
208 :type 'string)
210 (defvar erc-button-keymap
211 (let ((map (make-sparse-keymap)))
212 (define-key map (kbd "RET") 'erc-button-press-button)
213 (if (featurep 'xemacs)
214 (define-key map (kbd "<button2>") 'erc-button-click-button)
215 (define-key map (kbd "<mouse-2>") 'erc-button-click-button))
216 (define-key map (kbd "TAB") 'erc-button-next)
217 (define-key map (kbd "<backtab>") 'erc-button-previous)
218 (define-key map [follow-link] 'mouse-face)
219 (set-keymap-parent map erc-mode-map)
220 map)
221 "Local keymap for ERC buttons.")
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.
240 This syntax table should make all the valid nick characters word
241 constituents.")
243 (defvar erc-button-keys-added nil
244 "Internal variable used to keep track of whether we've added the
245 global-level ERC button keys yet.")
247 (defun erc-button-setup ()
248 "Add ERC mode-level button movement keys. This is only done once."
249 ;; Make XEmacs use `erc-button-face'.
250 (when (featurep 'xemacs)
251 (set (make-local-variable 'widget-button-face) nil))
252 ;; Add keys.
253 (unless erc-button-keys-added
254 (define-key erc-mode-map (kbd "<backtab>") 'erc-button-previous)
255 (setq erc-button-keys-added t)))
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
260 specified 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)))))))))))
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)))
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)))))))
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)))))
321 (defun erc-button-remove-old-buttons ()
322 "Remove all existing buttons.
323 This is called with narrowing in effect, just before the text is
324 buttonized again. Removing a button means to remove all the properties
325 that `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)))
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.
335 NICK-P specifies if this is a nickname button.
336 REGEXP 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 (when (featurep 'xemacs)
371 (widget-convert-button 'link from to :action 'erc-button-press-button
372 :suppress-face t
373 ;; Make XEmacs use our faces.
374 :button-face (if nick-p
375 erc-button-nickname-face
376 erc-button-face)
377 ;; Make XEmacs behave with mouse-clicks, for
378 ;; some reason, widget stuff overrides the
379 ;; 'keymap text-property.
380 :mouse-down-action 'erc-button-click-button)))
382 (defun erc-button-add-face (from to face)
383 "Add FACE to the region between FROM and TO."
384 ;; If we just use `add-text-property', then this will overwrite any
385 ;; face text property already used for the button. It will not be
386 ;; merged correctly. If we use overlays, then redisplay will be
387 ;; very slow with lots of buttons. This is why we manually merge
388 ;; face text properties.
389 (let ((old (erc-list (get-text-property from 'face)))
390 (pos from)
391 (end (next-single-property-change from 'face nil to))
392 new)
393 ;; old is the face at pos, in list form. It is nil if there is no
394 ;; face at pos. If nil, the new face is FACE. If not nil, the
395 ;; new face is a list containing FACE and the old stuff. end is
396 ;; where this face changes.
397 (while (< pos to)
398 (setq new (if old (cons face old) face))
399 (put-text-property pos end 'face new)
400 (setq pos end
401 old (erc-list (get-text-property pos 'face))
402 end (next-single-property-change pos 'face nil to)))))
404 ;; widget-button-click calls with two args, we ignore the first.
405 ;; Since Emacs runs this directly, rather than with
406 ;; widget-button-click, we need to fake an extra arg in the
407 ;; interactive spec.
408 (defun erc-button-click-button (ignore event)
409 "Call `erc-button-press-button'."
410 (interactive "P\ne")
411 (save-excursion
412 (mouse-set-point event)
413 (erc-button-press-button)))
415 ;; XEmacs calls this via widget-button-press with a bunch of arguments
416 ;; which we don't care about.
417 (defun erc-button-press-button (&rest ignore)
418 "Check text at point for a callback function.
419 If the text at point has a `erc-callback' property,
420 call it with the value of the `erc-data' text property."
421 (interactive)
422 (let* ((data (get-text-property (point) 'erc-data))
423 (fun (get-text-property (point) 'erc-callback)))
424 (unless fun
425 (message "No button at point"))
426 (when (and fun (symbolp fun) (not (fboundp fun)))
427 (error "Function %S is not bound" fun))
428 (apply fun data)))
430 (defun erc-button-next-function ()
431 "Pseudo completion function that actually jumps to the next button.
432 For use on `completion-at-point-functions'."
433 (when (< (point) (erc-beg-of-input-line))
434 `(lambda ()
435 (let ((here ,(point)))
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))))
447 (defun erc-button-next ()
448 "Go to the next button in this buffer."
449 (interactive)
450 (let ((f (erc-button-next-function)))
451 (if f (funcall f))))
453 (defun erc-button-previous ()
454 "Go to the previous button in this buffer."
455 (interactive)
456 (let ((here (point)))
457 (when (< here (erc-beg-of-input-line))
458 (while (and (get-text-property here 'erc-callback)
459 (not (= here (point-min))))
460 (setq here (1- here)))
461 (while (and (not (get-text-property here 'erc-callback))
462 (not (= here (point-min))))
463 (setq here (1- here)))
464 (if (> here (point-min))
465 (goto-char here)
466 (error "No previous button"))
467 t)))
469 (defun erc-browse-emacswiki (thing)
470 "Browse to thing in the emacs-wiki."
471 (browse-url (concat erc-emacswiki-url thing)))
473 (defun erc-browse-emacswiki-lisp (thing)
474 "Browse to THING in the emacs-wiki elisp area."
475 (browse-url (concat erc-emacswiki-lisp-url thing)))
477 ;;; Nickname buttons:
479 (defcustom erc-nick-popup-alist
480 '(("DeOp" . (erc-cmd-DEOP nick))
481 ("Kick" . (erc-cmd-KICK (concat nick " "
482 (read-from-minibuffer
483 (concat "Kick " nick ", reason: ")))))
484 ("Msg" . (erc-cmd-MSG (concat nick " "
485 (read-from-minibuffer
486 (concat "Message to " nick ": ")))))
487 ("Op" . (erc-cmd-OP nick))
488 ("Query" . (erc-cmd-QUERY nick))
489 ("Whois" . (erc-cmd-WHOIS nick))
490 ("Lastlog" . (erc-cmd-LASTLOG nick)))
491 "An alist of possible actions to take on a nickname.
492 An entry looks like (\"Action\" . SEXP) where SEXP is evaluated with
493 the variable `nick' bound to the nick in question.
495 Examples:
496 (\"DebianDB\" .
497 (shell-command
498 (format
499 \"ldapsearch -x -P 2 -h db.debian.org -b dc=debian,dc=org ircnick=%s\"
500 nick)))"
501 :group 'erc-button
502 :type '(repeat (cons (string :tag "Op")
503 sexp)))
505 (defun erc-nick-popup (nick)
506 (let* ((completion-ignore-case t)
507 (action (completing-read (concat "What action to take on '" nick "'? ")
508 erc-nick-popup-alist))
509 (code (cdr (assoc action erc-nick-popup-alist))))
510 (when code
511 (erc-set-active-buffer (current-buffer))
512 (eval code))))
514 ;;; Callback functions
515 (defun erc-button-describe-symbol (symbol-name)
516 "Describe SYMBOL-NAME.
517 Use `describe-function' for functions, `describe-variable' for variables,
518 and `apropos' for other symbols."
519 (let ((symbol (intern-soft symbol-name)))
520 (cond ((and symbol (fboundp symbol))
521 (describe-function symbol))
522 ((and symbol (boundp symbol))
523 (describe-variable symbol))
524 (t (apropos symbol-name)))))
526 (defun erc-button-beats-to-time (beats)
527 "Display BEATS in a readable time format."
528 (let* ((seconds (- (* (string-to-number beats) 86.4)
529 3600
530 (- (car (current-time-zone)))))
531 (hours (mod (floor seconds 3600) 24))
532 (minutes (mod (round seconds 60) 60)))
533 (message (format "@%s is %d:%02d local time"
534 beats hours minutes))))
536 (provide 'erc-button)
538 ;;; erc-button.el ends here
539 ;; Local Variables:
540 ;; indent-tabs-mode: nil
541 ;; End: