Remove some function declarations, no longer needed or correct
[bpt/emacs.git] / lisp / erc / erc-button.el
CommitLineData
d0fcaff5 1;; erc-button.el --- A way of buttonizing certain things in ERC buffers -*- lexical-binding:t -*-
597993cf 2
ba318903 3;; Copyright (C) 1996-2004, 2006-2014 Free Software Foundation, Inc.
597993cf
MB
4
5;; Author: Mario Lang <mlang@delysid.org>
34dc21db 6;; Maintainer: emacs-devel@gnu.org
597993cf
MB
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
4ee57b2a 12;; GNU Emacs is free software: you can redistribute it and/or modify
597993cf 13;; it under the terms of the GNU General Public License as published by
4ee57b2a
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
597993cf
MB
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
4ee57b2a 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
597993cf
MB
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.
865fe16f 29;; To enable, add to your init file:
597993cf
MB
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)
d4aa710a 57 (add-hook 'erc-complete-functions 'erc-button-next-function)
5e56b3fb 58 (add-hook 'erc-mode-hook 'erc-button-setup))
597993cf
MB
59 ((remove-hook 'erc-insert-modify-hook 'erc-button-add-buttons)
60 (remove-hook 'erc-send-modify-hook 'erc-button-add-buttons)
d4aa710a 61 (remove-hook 'erc-complete-functions 'erc-button-next-function)
5e56b3fb
MO
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))))))
597993cf
MB
67
68;;; Variables
69
4b56d0fe 70(defface erc-button '((t :weight bold))
597993cf
MB
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
77A 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
90Buttons will be displayed in this face when the mouse cursor is
91above 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]*\\)?"
526dc846 99 "[-a-zA-Z0-9_=!?#$@~`%&*+\\/:;.,()]+[-a-zA-Z0-9_=#$@~`%&*+\\/()]")
597993cf
MB
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
107If this variable is a number, consider URLs longer than its value to
108be \"long\". If t, URLs will be considered \"long\" if they are
109longer 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"
fb7ada5f 119 "URL used to browse rfc references.
597993cf
MB
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"
fb7ada5f 125 "URL used to browse Google search references.
597993cf
MB
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)
2255788d 138;;; ("(\\(\\([^~\n \t@][^\n \t@]*\\)@\\([a-zA-Z0-9.:-]+\\)\\)" 1 t finger 2 3)
597993cf
MB
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))
fb7ada5f 159 "Alist of regexps matching buttons in ERC buffers.
597993cf
MB
160Each entry has the form (REGEXP BUTTON FORM CALLBACK PAR...), where
161
162REGEXP 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
171BUTTON is the number of the regexp grouping actually matching the
172 button, This is ignored if REGEXP is 'nicknames.
173
174FORM is a lisp expression which must eval to true for the button to
175 be added,
176
177CALLBACK 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
181PAR 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
2255788d 186 :version "24.3" ; remove finger (bug#4443)
597993cf
MB
187 :type '(repeat
188 (list :tag "Button"
189 (choice :tag "Matches"
190 regexp
191 (variable :tag "Variable containing regexp")
946618ba
GM
192 ;; FIXME It really does mean 'nicknames
193 ;; rather than just nicknames.
194 (const :tag "Nicknames" 'nicknames))
597993cf
MB
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"
201 :inline t
202 (integer :tag "Regexp section number")))))
203
204(defcustom erc-emacswiki-url "http://www.emacswiki.org/cgi-bin/wiki.pl?"
fb7ada5f 205 "URL of the EmacsWiki Homepage."
597993cf
MB
206 :group 'erc-button
207 :type 'string)
208
209(defcustom erc-emacswiki-lisp-url "http://www.emacswiki.org/elisp/"
fb7ada5f 210 "URL of the EmacsWiki ELisp area."
597993cf
MB
211 :group 'erc-button
212 :type 'string)
213
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)
c89e5cd7 221 (define-key map (kbd "<backtab>") 'erc-button-previous)
081bd373 222 (define-key map [follow-link] 'mouse-face)
597993cf
MB
223 (set-keymap-parent map erc-mode-map)
224 map)
225 "Local keymap for ERC buttons.")
226
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)
242 table)
243 "Syntax table used when buttonizing messages.
dfa0b1fc 244This syntax table should make all the valid nick characters word
597993cf
MB
245constituents.")
246
ff59d266
MB
247(defvar erc-button-keys-added nil
248 "Internal variable used to keep track of whether we've added the
249global-level ERC button keys yet.")
250
5e56b3fb 251(defun erc-button-setup ()
ff59d266 252 "Add ERC mode-level button movement keys. This is only done once."
5e56b3fb
MO
253 ;; Make XEmacs use `erc-button-face'.
254 (when (featurep 'xemacs)
255 (set (make-local-variable 'widget-button-face) nil))
256 ;; Add keys.
ff59d266
MB
257 (unless erc-button-keys-added
258 (define-key erc-mode-map (kbd "<backtab>") 'erc-button-previous)
259 (setq erc-button-keys-added t)))
260
597993cf
MB
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
264specified by `erc-button-alist'."
265 (interactive)
266 (save-excursion
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)
49a053fc 272 regexp)
597993cf
MB
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)
277 (progn
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)))
284 (dolist (r regexp)
285 (erc-button-add-buttons-1
286 (concat "\\<" (regexp-quote r) "\\>")
287 entry)))
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)) "\\>")
293 entry)))))))))))
294
295(defun erc-button-add-nickname-buttons (entry)
296 "Search through the buffer for nicknames, and add buttons."
297 (let ((form (nth 2 entry))
298 (fun (nth 3 entry))
299 bounds word)
300 (when (or (eq t form)
301 (eval 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)))
5e56b3fb
MO
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)))))))
597993cf
MB
311
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)))
318 (form (nth 2 entry))
319 (fun (nth 3 entry))
320 (data (mapcar 'match-string (nthcdr 4 entry))))
321 (when (or (eq t form)
322 (eval form))
323 (erc-button-add-button start end fun nil data regexp)))))
324
325(defun erc-button-remove-old-buttons ()
326 "Remove all existing buttons.
327This is called with narrowing in effect, just before the text is
328buttonized again. Removing a button means to remove all the properties
329that `erc-button-add-button' adds, except for the face."
330 (remove-text-properties
331 (point-min) (point-max)
332 '(erc-callback nil
333 erc-data nil
334 mouse-face nil
335 keymap nil)))
336
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.
339NICK-P specifies if this is a nickname button.
340REGEXP 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.
344 (let (fill-column)
345 (when (and erc-button-wrap-long-urls
346 (string= regexp erc-button-url-regexp)
347 (> (- to from)
348 (setq fill-column (- (if (numberp erc-button-wrap-long-urls)
349 erc-button-wrap-long-urls
350 erc-fill-column)
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))))))
361 (if nick-p
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)))
366 (add-text-properties
367 from to
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))))
9c7b2dd3
JB
374 (when (featurep 'xemacs)
375 (widget-convert-button 'link from to :action 'erc-button-press-button
376 :suppress-face t
377 ;; Make XEmacs use our faces.
378 :button-face (if nick-p
379 erc-button-nickname-face
380 erc-button-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)))
597993cf
MB
385
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)))
394 (pos from)
395 (end (next-single-property-change from 'face nil to))
396 new)
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.
401 (while (< pos to)
402 (setq new (if old (cons face old) face))
403 (put-text-property pos end 'face new)
404 (setq pos end
405 old (erc-list (get-text-property pos 'face))
406 end (next-single-property-change pos 'face nil to)))))
407
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
411;; interactive spec.
a022856b 412(defun erc-button-click-button (_ignore event)
597993cf
MB
413 "Call `erc-button-press-button'."
414 (interactive "P\ne")
415 (save-excursion
416 (mouse-set-point event)
417 (erc-button-press-button)))
418
419;; XEmacs calls this via widget-button-press with a bunch of arguments
420;; which we don't care about.
a022856b 421(defun erc-button-press-button (&rest _ignore)
597993cf
MB
422 "Check text at point for a callback function.
423If the text at point has a `erc-callback' property,
424call it with the value of the `erc-data' text property."
425 (interactive)
426 (let* ((data (get-text-property (point) 'erc-data))
427 (fun (get-text-property (point) 'erc-callback)))
428 (unless fun
429 (message "No button at point"))
430 (when (and fun (symbolp fun) (not (fboundp fun)))
431 (error "Function %S is not bound" fun))
432 (apply fun data)))
433
d4aa710a
SM
434(defun erc-button-next-function ()
435 "Pseudo completion function that actually jumps to the next button.
436For use on `completion-at-point-functions'."
d0fcaff5
SM
437 ;; FIXME: This is an abuse of completion-at-point-functions.
438 (when (< (point) (erc-beg-of-input-line))
439 (let ((start (point)))
440 (lambda ()
441 (let ((here start))
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))
450 (goto-char here)
451 (error "No next button"))
452 t)))))
d4aa710a 453
597993cf
MB
454(defun erc-button-next ()
455 "Go to the next button in this buffer."
456 (interactive)
d4aa710a
SM
457 (let ((f (erc-button-next-function)))
458 (if f (funcall f))))
597993cf 459
d8bd1ad4
MB
460(defun erc-button-previous ()
461 "Go to the previous button in this buffer."
462 (interactive)
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))
472 (goto-char here)
473 (error "No previous button"))
474 t)))
475
597993cf
MB
476(defun erc-browse-emacswiki (thing)
477 "Browse to thing in the emacs-wiki."
478 (browse-url (concat erc-emacswiki-url thing)))
479
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)))
483
484;;; Nickname buttons:
485
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)))
fb7ada5f 498 "An alist of possible actions to take on a nickname.
597993cf
MB
499An entry looks like (\"Action\" . SEXP) where SEXP is evaluated with
500the variable `nick' bound to the nick in question.
501
502Examples:
503 (\"DebianDB\" .
504 (shell-command
505 (format
506 \"ldapsearch -x -P 2 -h db.debian.org -b dc=debian,dc=org ircnick=%s\"
507 nick)))"
508 :group 'erc-button
509 :type '(repeat (cons (string :tag "Op")
510 sexp)))
511
512(defun erc-nick-popup (nick)
513 (let* ((completion-ignore-case t)
514 (action (completing-read (concat "What action to take on '" nick "'? ")
515 erc-nick-popup-alist))
516 (code (cdr (assoc action erc-nick-popup-alist))))
517 (when code
518 (erc-set-active-buffer (current-buffer))
1288751c 519 (eval code `((nick . ,nick))))))
597993cf
MB
520
521;;; Callback functions
522(defun erc-button-describe-symbol (symbol-name)
523 "Describe SYMBOL-NAME.
524Use `describe-function' for functions, `describe-variable' for variables,
525and `apropos' for other symbols."
526 (let ((symbol (intern-soft symbol-name)))
527 (cond ((and symbol (fboundp symbol))
528 (describe-function symbol))
529 ((and symbol (boundp symbol))
530 (describe-variable symbol))
531 (t (apropos symbol-name)))))
532
533(defun erc-button-beats-to-time (beats)
534 "Display BEATS in a readable time format."
535 (let* ((seconds (- (* (string-to-number beats) 86.4)
536 3600
537 (- (car (current-time-zone)))))
538 (hours (mod (floor seconds 3600) 24))
539 (minutes (mod (round seconds 60) 60)))
540 (message (format "@%s is %d:%02d local time"
541 beats hours minutes))))
542
543(provide 'erc-button)
544
545;;; erc-button.el ends here
546;; Local Variables:
547;; indent-tabs-mode: nil
548;; End:
549