1 ;; erc-button.el --- A way of buttonizing certain things in ERC buffers
3 ;; Copyright (C) 1996-2004, 2006-2013 Free Software Foundation, Inc.
5 ;; 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 init file:
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-function
)
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-function
)
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 :weight bold
))
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."
186 :version
"24.3" ; remove finger (bug#4443)
189 (choice :tag
"Matches"
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"
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."
207 (defcustom erc-emacswiki-lisp-url
"http://www.emacswiki.org/elisp/"
208 "URL of the EmacsWiki ELisp area."
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 (define-key map
[follow-link
] 'mouse-face
)
221 (set-keymap-parent map erc-mode-map
)
223 "Local keymap for ERC buttons.")
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
)
241 "Syntax table used when buttonizing messages.
242 This syntax table should make all the valid nick characters word
245 (defvar erc-button-keys-added nil
246 "Internal variable used to keep track of whether we've added the
247 global-level ERC button keys yet.")
249 (defun erc-button-setup ()
250 "Add ERC mode-level button movement keys. This is only done once."
251 ;; Make XEmacs use `erc-button-face'.
252 (when (featurep 'xemacs
)
253 (set (make-local-variable 'widget-button-face
) nil
))
255 (unless erc-button-keys-added
256 (define-key erc-mode-map
(kbd "<backtab>") 'erc-button-previous
)
257 (setq erc-button-keys-added t
)))
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
262 specified by `erc-button-alist'."
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
)
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
)
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
)))
283 (erc-button-add-buttons-1
284 (concat "\\<" (regexp-quote r
) "\\>")
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
)) "\\>")
293 (defun erc-button-add-nickname-buttons (entry)
294 "Search through the buffer for nicknames, and add buttons."
295 (let ((form (nth 2 entry
))
298 (when (or (eq t 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
)))
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
)))))))
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
)))
318 (data (mapcar 'match-string
(nthcdr 4 entry
))))
319 (when (or (eq t form
)
321 (erc-button-add-button start end fun nil data regexp
)))))
323 (defun erc-button-remove-old-buttons ()
324 "Remove all existing buttons.
325 This is called with narrowing in effect, just before the text is
326 buttonized again. Removing a button means to remove all the properties
327 that `erc-button-add-button' adds, except for the face."
328 (remove-text-properties
329 (point-min) (point-max)
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.
337 NICK-P specifies if this is a nickname button.
338 REGEXP 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.
343 (when (and erc-button-wrap-long-urls
344 (string= regexp erc-button-url-regexp
)
346 (setq fill-column
(- (if (numberp erc-button-wrap-long-urls
)
347 erc-button-wrap-long-urls
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))))))
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
)))
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
))))
372 (when (featurep 'xemacs
)
373 (widget-convert-button 'link from to
:action
'erc-button-press-button
375 ;; Make XEmacs use our faces.
376 :button-face
(if nick-p
377 erc-button-nickname-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
)))
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
)))
393 (end (next-single-property-change from
'face nil to
))
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.
400 (setq new
(if old
(cons face old
) face
))
401 (put-text-property pos end
'face new
)
403 old
(erc-list (get-text-property pos
'face
))
404 end
(next-single-property-change pos
'face nil to
)))))
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
410 (defun erc-button-click-button (ignore event
)
411 "Call `erc-button-press-button'."
414 (mouse-set-point event
)
415 (erc-button-press-button)))
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.
421 If the text at point has a `erc-callback' property,
422 call it with the value of the `erc-data' text property."
424 (let* ((data (get-text-property (point) 'erc-data
))
425 (fun (get-text-property (point) 'erc-callback
)))
427 (message "No button at point"))
428 (when (and fun
(symbolp fun
) (not (fboundp fun
)))
429 (error "Function %S is not bound" fun
))
432 (defun erc-button-next-function ()
433 "Pseudo completion function that actually jumps to the next button.
434 For use on `completion-at-point-functions'."
435 (when (< (point) (erc-beg-of-input-line))
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))
446 (error "No next button"))
449 (defun erc-button-next ()
450 "Go to the next button in this buffer."
452 (let ((f (erc-button-next-function)))
455 (defun erc-button-previous ()
456 "Go to the previous button in this buffer."
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))
468 (error "No previous button"))
471 (defun erc-browse-emacswiki (thing)
472 "Browse to thing in the emacs-wiki."
473 (browse-url (concat erc-emacswiki-url thing
)))
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
)))
479 ;;; Nickname buttons:
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
)))
493 "An alist of possible actions to take on a nickname.
494 An entry looks like (\"Action\" . SEXP) where SEXP is evaluated with
495 the variable `nick' bound to the nick in question.
501 \"ldapsearch -x -P 2 -h db.debian.org -b dc=debian,dc=org ircnick=%s\"
504 :type
'(repeat (cons (string :tag
"Op")
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
))))
513 (erc-set-active-buffer (current-buffer))
516 ;;; Callback functions
517 (defun erc-button-describe-symbol (symbol-name)
518 "Describe SYMBOL-NAME.
519 Use `describe-function' for functions, `describe-variable' for variables,
520 and `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
)))))
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)
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
))))
538 (provide 'erc-button
)
540 ;;; erc-button.el ends here
542 ;; indent-tabs-mode: nil