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, 2007, 2008 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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
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)
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.
48 (defgroup erc-button nil
49 "Define how text can be turned into clickable buttons."
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
))))))
70 (defface erc-button
'((t (:bold t
)))
74 (defcustom erc-button-face
'erc-button
75 "Face used for highlighting buttons in ERC buffers.
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'."
82 (defcustom erc-button-nickname-face
'erc-nick-default-face
83 "Face used for ERC nickname buttons."
87 (defcustom erc-button-mouse-face
'highlight
88 "Face used for mouse highlighting in ERC buffers.
90 Buttons will be displayed in this face when the mouse cursor is
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."
104 (defcustom erc-button-wrap-long-urls nil
105 "If non-nil, \"long\" URLs matching `erc-button-url-regexp' will be wrapped.
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'."
111 :type
'(choice integer boolean
))
113 (defcustom erc-button-buttonize-nicks t
114 "Flag indicating whether nicks should be buttonized or not."
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."
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."
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)
140 ("[`]\\([a-zA-Z][-a-zA-Z_0-9]+\\)[']" 1 t erc-button-describe-symbol
1)
142 ("\\bInfo:[\"]\\([^\"]+\\)[\"]" 0 t Info-goto-node
1)
143 ("\\b\\(Ward\\|Wiki\\|WardsWiki\\|TheWiki\\):\\([A-Z][a-z]+\\([A-Z][a-z]+\\)+\\)"
145 (browse-url (concat "http://c2.com/cgi-bin/wiki?" page
)))
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
)))
153 ("\\brfc[#: ]?\\([0-9]+\\)"
155 (browse-url (format erc-button-rfc-url num
)))
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
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
171 BUTTON is the number of the regexp grouping actually matching the
172 button, This is ignored if REGEXP is 'nicknames.
174 FORM is a lisp expression which must eval to true for the button to
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.
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."
188 (choice :tag
"Matches"
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"
199 (integer :tag
"Regexp section number")))))
201 (defcustom erc-emacswiki-url
"http://www.emacswiki.org/cgi-bin/wiki.pl?"
202 "*URL of the EmacsWiki Homepage."
206 (defcustom erc-emacswiki-lisp-url
"http://www.emacswiki.org/elisp/"
207 "*URL of the EmacsWiki ELisp area."
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 (set-keymap-parent map erc-mode-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
)
239 "Syntax table used when buttonizing messages.
240 This syntax table should make all the valid nick characters word
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
))
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'."
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
)
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
)
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
)))
281 (erc-button-add-buttons-1
282 (concat "\\<" (regexp-quote r
) "\\>")
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
)) "\\>")
291 (defun erc-button-add-nickname-buttons (entry)
292 "Search through the buffer for nicknames, and add buttons."
293 (let ((form (nth 2 entry
))
296 (when (or (eq t 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
)))
316 (data (mapcar 'match-string
(nthcdr 4 entry
))))
317 (when (or (eq t 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)
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.
341 (when (and erc-button-wrap-long-urls
342 (string= regexp erc-button-url-regexp
)
344 (setq fill-column
(- (if (numberp erc-button-wrap-long-urls
)
345 erc-button-wrap-long-urls
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))))))
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
)))
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
372 ;; Make XEmacs use our faces.
373 :button-face
(if nick-p
374 erc-button-nickname-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
))
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
)))
390 (end (next-single-property-change from
'face nil to
))
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.
397 (setq new
(if old
(cons face old
) face
))
398 (put-text-property pos end
'face new
)
400 old
(erc-list (get-text-property pos
'face
))
401 end
(next-single-property-change pos
'face nil to
)))))
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
407 (defun erc-button-click-button (ignore event
)
408 "Call `erc-button-press-button'."
411 (mouse-set-point event
)
412 (erc-button-press-button)))
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.
418 If the text at point has a `erc-callback' property,
419 call it with the value of the `erc-data' text property."
421 (let* ((data (get-text-property (point) 'erc-data
))
422 (fun (get-text-property (point) 'erc-callback
)))
424 (message "No button at point"))
425 (when (and fun
(symbolp fun
) (not (fboundp fun
)))
426 (error "Function %S is not bound" fun
))
429 (defun erc-button-next ()
430 "Go to the next button in this buffer."
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))
442 (error "No next button"))
445 (defun erc-button-previous ()
446 "Go to the previous button in this buffer."
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))
458 (error "No previous button"))
461 (defun erc-browse-emacswiki (thing)
462 "Browse to thing in the emacs-wiki."
463 (browse-url (concat erc-emacswiki-url thing
)))
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
)))
469 ;;; Nickname buttons:
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.
484 An entry looks like (\"Action\" . SEXP) where SEXP is evaluated with
485 the variable `nick' bound to the nick in question.
491 \"ldapsearch -x -P 2 -h db.debian.org -b dc=debian,dc=org ircnick=%s\"
494 :type
'(repeat (cons (string :tag
"Op")
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
))))
503 (erc-set-active-buffer (current-buffer))
506 ;;; Callback functions
507 (defun erc-button-describe-symbol (symbol-name)
508 "Describe SYMBOL-NAME.
509 Use `describe-function' for functions, `describe-variable' for variables,
510 and `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
)))))
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)
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
))))
528 (provide 'erc-button
)
530 ;;; erc-button.el ends here
532 ;; indent-tabs-mode: nil
535 ;; arch-tag: 7d23bed4-2f30-4273-a03f-d7a274c605c4