1 ;; erc-button.el --- A way of buttonizing certain things in ERC buffers -*- lexical-binding:t -*-
3 ;; Copyright (C) 1996-2004, 2006-2015 Free Software Foundation, Inc.
5 ;; Author: Mario Lang <mlang@delysid.org>
6 ;; Maintainer: emacs-devel@gnu.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.1" ; remove finger (bug#4443)
189 (choice :tag
"Matches"
191 (variable :tag
"Variable containing regexp")
192 ;; FIXME It really does mean 'nicknames
193 ;; rather than just nicknames.
194 (const :tag
"Nicknames" 'nicknames
))
195 (integer :tag
"Number of the regexp section that matches")
196 (choice :tag
"When to buttonize"
197 (const :tag
"Always" t
)
198 (sexp :tag
"Only when this evaluates to non-nil"))
199 (function :tag
"Function to call when button is pressed")
200 (repeat :tag
"Sections of regexp to send to the function"
202 (integer :tag
"Regexp section number")))))
204 (defcustom erc-emacswiki-url
"http://www.emacswiki.org/cgi-bin/wiki.pl?"
205 "URL of the EmacsWiki Homepage."
209 (defcustom erc-emacswiki-lisp-url
"http://www.emacswiki.org/elisp/"
210 "URL of the EmacsWiki ELisp area."
214 (defvar erc-button-keymap
215 (let ((map (make-sparse-keymap)))
216 (define-key map
(kbd "RET") 'erc-button-press-button
)
217 (if (featurep 'xemacs
)
218 (define-key map
(kbd "<button2>") 'erc-button-click-button
)
219 (define-key map
(kbd "<mouse-2>") 'erc-button-click-button
))
220 (define-key map
(kbd "TAB") 'erc-button-next
)
221 (define-key map
(kbd "<backtab>") 'erc-button-previous
)
222 (define-key map
[follow-link
] 'mouse-face
)
223 (set-keymap-parent map erc-mode-map
)
225 "Local keymap for ERC buttons.")
227 (defvar erc-button-syntax-table
228 (let ((table (make-syntax-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
)
240 (modify-syntax-entry ?|
"w" table
)
241 (modify-syntax-entry ?
\\ "w" table
)
243 "Syntax table used when buttonizing messages.
244 This syntax table should make all the valid nick characters word
247 (defvar erc-button-keys-added nil
248 "Internal variable used to keep track of whether we've added the
249 global-level ERC button keys yet.")
251 (defun erc-button-setup ()
252 "Add ERC mode-level button movement keys. This is only done once."
253 ;; Make XEmacs use `erc-button-face'.
254 (when (featurep 'xemacs
)
255 (set (make-local-variable 'widget-button-face
) nil
))
257 (unless erc-button-keys-added
258 (define-key erc-mode-map
(kbd "<backtab>") 'erc-button-previous
)
259 (setq erc-button-keys-added t
)))
261 (defun erc-button-add-buttons ()
262 "Find external references in the current buffer and make buttons of them.
263 \"External references\" are things like URLs, as
264 specified by `erc-button-alist'."
267 (with-syntax-table erc-button-syntax-table
268 (let ((buffer-read-only nil
)
269 (inhibit-point-motion-hooks t
)
270 (inhibit-field-text-motion t
)
271 (alist erc-button-alist
)
273 (erc-button-remove-old-buttons)
274 (dolist (entry alist
)
275 (if (equal (car entry
) (quote (quote nicknames
)))
276 (erc-button-add-nickname-buttons entry
)
278 (setq regexp
(or (and (stringp (car entry
)) (car entry
))
279 (and (boundp (car entry
))
280 (symbol-value (car entry
)))))
281 (cond ((stringp regexp
)
282 (erc-button-add-buttons-1 regexp entry
))
283 ((and (listp regexp
) (stringp (car regexp
)))
285 (erc-button-add-buttons-1
286 (concat "\\<" (regexp-quote r
) "\\>")
288 ((and (listp regexp
) (listp (car regexp
))
289 (stringp (caar regexp
)))
290 (dolist (elem regexp
)
291 (erc-button-add-buttons-1
292 (concat "\\<" (regexp-quote (car elem
)) "\\>")
295 (defun erc-button-add-nickname-buttons (entry)
296 "Search through the buffer for nicknames, and add buttons."
297 (let ((form (nth 2 entry
))
300 (when (or (eq t form
)
302 (goto-char (point-min))
303 (while (forward-word 1)
304 (setq bounds
(bounds-of-thing-at-point 'word
))
305 (setq word
(buffer-substring-no-properties
306 (car bounds
) (cdr bounds
)))
307 (when (or (and (erc-server-buffer-p) (erc-get-server-user word
))
308 (and erc-channel-users
(erc-get-channel-user word
)))
309 (erc-button-add-button (car bounds
) (cdr bounds
)
310 fun t
(list word
)))))))
312 (defun erc-button-add-buttons-1 (regexp entry
)
313 "Search through the buffer for matches to ENTRY and add buttons."
314 (goto-char (point-min))
315 (while (re-search-forward regexp nil t
)
316 (let ((start (match-beginning (nth 1 entry
)))
317 (end (match-end (nth 1 entry
)))
320 (data (mapcar 'match-string
(nthcdr 4 entry
))))
321 (when (or (eq t form
)
323 (erc-button-add-button start end fun nil data regexp
)))))
325 (defun erc-button-remove-old-buttons ()
326 "Remove all existing buttons.
327 This is called with narrowing in effect, just before the text is
328 buttonized again. Removing a button means to remove all the properties
329 that `erc-button-add-button' adds, except for the face."
330 (remove-text-properties
331 (point-min) (point-max)
337 (defun erc-button-add-button (from to fun nick-p
&optional data regexp
)
338 "Create a button between FROM and TO with callback FUN and data DATA.
339 NICK-P specifies if this is a nickname button.
340 REGEXP is the regular expression which matched for this button."
341 ;; Really nasty hack to <URL: > ise urls, and line-wrap them if
342 ;; they're going to be wider than `erc-fill-column'.
343 ;; This could be a lot cleaner, but it works for me -- lawrence.
345 (when (and erc-button-wrap-long-urls
346 (string= regexp erc-button-url-regexp
)
348 (setq fill-column
(- (if (numberp erc-button-wrap-long-urls
)
349 erc-button-wrap-long-urls
351 (length erc-fill-prefix
)))))
352 (setq to
(prog1 (point-marker) (insert ">"))
353 from
(prog2 (goto-char from
) (point-marker) (insert "<URL: ")))
354 (let ((pos (copy-marker from
)))
355 (while (> (- to pos
) fill-column
)
356 (goto-char (+ pos fill-column
))
357 (insert "\n" erc-fill-prefix
) ; This ought to figure out
358 ; what type of filling we're
359 ; doing, and indent accordingly.
360 (move-marker pos
(point))))))
362 (when erc-button-nickname-face
363 (erc-button-add-face from to erc-button-nickname-face
))
364 (when erc-button-face
365 (erc-button-add-face from to erc-button-face
)))
368 (nconc (and erc-button-mouse-face
369 (list 'mouse-face erc-button-mouse-face
))
370 (list 'erc-callback fun
)
371 (list 'keymap erc-button-keymap
)
372 (list 'rear-nonsticky t
)
373 (and data
(list 'erc-data data
))))
374 (when (featurep 'xemacs
)
375 (widget-convert-button 'link from to
:action
'erc-button-press-button
377 ;; Make XEmacs use our faces.
378 :button-face
(if nick-p
379 erc-button-nickname-face
381 ;; Make XEmacs behave with mouse-clicks, for
382 ;; some reason, widget stuff overrides the
383 ;; 'keymap text-property.
384 :mouse-down-action
'erc-button-click-button
)))
386 (defun erc-button-add-face (from to face
)
387 "Add FACE to the region between FROM and TO."
388 ;; If we just use `add-text-property', then this will overwrite any
389 ;; face text property already used for the button. It will not be
390 ;; merged correctly. If we use overlays, then redisplay will be
391 ;; very slow with lots of buttons. This is why we manually merge
392 ;; face text properties.
393 (let ((old (erc-list (get-text-property from
'face
)))
395 (end (next-single-property-change from
'face nil to
))
397 ;; old is the face at pos, in list form. It is nil if there is no
398 ;; face at pos. If nil, the new face is FACE. If not nil, the
399 ;; new face is a list containing FACE and the old stuff. end is
400 ;; where this face changes.
402 (setq new
(if old
(cons face old
) face
))
403 (put-text-property pos end
'face new
)
405 old
(erc-list (get-text-property pos
'face
))
406 end
(next-single-property-change pos
'face nil to
)))))
408 ;; widget-button-click calls with two args, we ignore the first.
409 ;; Since Emacs runs this directly, rather than with
410 ;; widget-button-click, we need to fake an extra arg in the
412 (defun erc-button-click-button (_ignore event
)
413 "Call `erc-button-press-button'."
416 (mouse-set-point event
)
417 (erc-button-press-button)))
419 ;; XEmacs calls this via widget-button-press with a bunch of arguments
420 ;; which we don't care about.
421 (defun erc-button-press-button (&rest _ignore
)
422 "Check text at point for a callback function.
423 If the text at point has a `erc-callback' property,
424 call it with the value of the `erc-data' text property."
426 (let* ((data (get-text-property (point) 'erc-data
))
427 (fun (get-text-property (point) 'erc-callback
)))
429 (message "No button at point"))
430 (when (and fun
(symbolp fun
) (not (fboundp fun
)))
431 (error "Function %S is not bound" fun
))
434 (defun erc-button-next-function ()
435 "Pseudo completion function that actually jumps to the next button.
436 For use on `completion-at-point-functions'."
437 ;; FIXME: This is an abuse of completion-at-point-functions.
438 (when (< (point) (erc-beg-of-input-line))
439 (let ((start (point)))
442 ;; FIXME: Use next-single-property-change.
443 (while (and (get-text-property here
'erc-callback
)
444 (not (= here
(point-max))))
445 (setq here
(1+ here
)))
446 (while (not (or (get-text-property here
'erc-callback
)
447 (= here
(point-max))))
448 (setq here
(1+ here
)))
449 (if (< here
(point-max))
451 (error "No next button"))
454 (defun erc-button-next ()
455 "Go to the next button in this buffer."
457 (let ((f (erc-button-next-function)))
460 (defun erc-button-previous ()
461 "Go to the previous button in this buffer."
463 (let ((here (point)))
464 (when (< here
(erc-beg-of-input-line))
465 (while (and (get-text-property here
'erc-callback
)
466 (not (= here
(point-min))))
467 (setq here
(1- here
)))
468 (while (and (not (get-text-property here
'erc-callback
))
469 (not (= here
(point-min))))
470 (setq here
(1- here
)))
471 (if (> here
(point-min))
473 (error "No previous button"))
476 (defun erc-browse-emacswiki (thing)
477 "Browse to thing in the emacs-wiki."
478 (browse-url (concat erc-emacswiki-url thing
)))
480 (defun erc-browse-emacswiki-lisp (thing)
481 "Browse to THING in the emacs-wiki elisp area."
482 (browse-url (concat erc-emacswiki-lisp-url thing
)))
484 ;;; Nickname buttons:
486 (defcustom erc-nick-popup-alist
487 '(("DeOp" .
(erc-cmd-DEOP nick
))
488 ("Kick" .
(erc-cmd-KICK (concat nick
" "
489 (read-from-minibuffer
490 (concat "Kick " nick
", reason: ")))))
491 ("Msg" .
(erc-cmd-MSG (concat nick
" "
492 (read-from-minibuffer
493 (concat "Message to " nick
": ")))))
494 ("Op" .
(erc-cmd-OP nick
))
495 ("Query" .
(erc-cmd-QUERY nick
))
496 ("Whois" .
(erc-cmd-WHOIS nick
))
497 ("Lastlog" .
(erc-cmd-LASTLOG nick
)))
498 "An alist of possible actions to take on a nickname.
499 An entry looks like (\"Action\" . SEXP) where SEXP is evaluated with
500 the variable `nick' bound to the nick in question.
506 \"ldapsearch -x -P 2 -h db.debian.org -b dc=debian,dc=org ircnick=%s\"
509 :type
'(repeat (cons (string :tag
"Op")
512 (defun erc-nick-popup (nick)
513 (let* ((completion-ignore-case t
)
514 (action (completing-read (format-message
515 "What action to take on `%s'? " nick
)
516 erc-nick-popup-alist
))
517 (code (cdr (assoc action erc-nick-popup-alist
))))
519 (erc-set-active-buffer (current-buffer))
520 (eval code
`((nick .
,nick
))))))
522 ;;; Callback functions
523 (defun erc-button-describe-symbol (symbol-name)
524 "Describe SYMBOL-NAME.
525 Use `describe-function' for functions, `describe-variable' for variables,
526 and `apropos' for other symbols."
527 (let ((symbol (intern-soft symbol-name
)))
528 (cond ((and symbol
(fboundp symbol
))
529 (describe-function symbol
))
530 ((and symbol
(boundp symbol
))
531 (describe-variable symbol
))
532 (t (apropos symbol-name
)))))
534 (defun erc-button-beats-to-time (beats)
535 "Display BEATS in a readable time format."
536 (let* ((seconds (- (* (string-to-number beats
) 86.4)
538 (- (car (current-time-zone)))))
539 (hours (mod (floor seconds
3600) 24))
540 (minutes (mod (round seconds
60) 60)))
541 (message "@%s is %d:%02d local time"
542 beats hours minutes
)))
544 (provide 'erc-button
)
546 ;;; erc-button.el ends here
548 ;; indent-tabs-mode: nil