(url-retrieve-internal): Bind url-proxy-object to nil.
[emacs.git] / lisp / erc / erc-autoaway.el
blobdcb6bc0756eb848cdf943ee77c681572abecd6d8
1 ;;; erc-autoaway.el --- Provides autoaway for ERC
3 ;; Copyright (C) 2002, 2003, 2004, 2006 Free Software Foundation, Inc.
5 ;; Author: Jorgen Schaefer <forcer@forcix.cx>
6 ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcAutoAway
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
23 ;; Boston, MA 02110-1301, USA.
25 ;;; Commentary:
27 ;; TODO:
28 ;; - Legacy names: erc-auto-discard-away, erc-auto-set-away
30 ;;; Code:
32 (require 'erc)
34 (defgroup erc-autoaway nil
35 "Set yourself automatically away after some idletime and set
36 yourself back when you type something."
37 :group 'erc)
39 (defvar erc-autoaway-idletimer nil
40 "The Emacs idletimer.
41 This is only used when `erc-autoaway-idle-method' is set to 'emacs.")
43 ;;;###autoload (autoload 'erc-autoaway-mode "erc-autoaway")
44 (define-erc-module autoaway nil
45 "In ERC autoaway mode, you can be set away automatically.
46 If `erc-auto-set-away' is set, then you will be set away after
47 the number of seconds specified in `erc-autoaway-idle-seconds'.
49 There are several kinds of being idle:
51 User idle time measures how long you have not been sending any
52 commands to Emacs. This is the default.
54 Emacs idle time measures how long Emacs has been idle. This is
55 currently not useful, since Emacs is non-idle when it handles
56 ping-pong with IRC servers. See `erc-autoaway-idle-method'
57 for more information.
59 IRC idle time measures how long since you last sent something (see
60 `erc-autoaway-last-sent-time').
62 If `erc-auto-discard-away' is set, then typing anything, will
63 set you no longer away.
65 Related variables: `erc-public-away-p' and `erc-away-nickname'."
66 ;; Enable:
67 ((when (boundp 'erc-autoaway-idle-method)
68 (cond
69 ((eq erc-autoaway-idle-method 'irc)
70 (add-hook 'erc-send-completed-hook 'erc-autoaway-reset-idle-irc)
71 (add-hook 'erc-server-001-functions 'erc-autoaway-reset-idle-irc))
72 ((eq erc-autoaway-idle-method 'user)
73 (add-hook 'post-command-hook 'erc-autoaway-reset-idle-user))
74 ((eq erc-autoaway-idle-method 'emacs)
75 (erc-autoaway-reestablish-idletimer)))
76 (add-hook 'erc-timer-hook 'erc-autoaway-possibly-set-away)
77 (add-hook 'erc-server-305-functions 'erc-autoaway-reset-indicators)))
78 ;; Disable:
79 ((when (boundp 'erc-autoaway-idle-method)
80 (cond
81 ((eq erc-autoaway-idle-method 'irc)
82 (remove-hook 'erc-send-completed-hook 'erc-autoaway-reset-idle-irc)
83 (remove-hook 'erc-server-001-functions 'erc-autoaway-reset-idle-irc))
84 ((eq erc-autoaway-idle-method 'user)
85 (remove-hook 'post-command-hook 'erc-autoaway-reset-idle-user))
86 ((eq erc-autoaway-idle-method 'emacs)
87 (erc-cancel-timer erc-autoaway-idletimer)
88 (setq erc-autoaway-idletimer nil)))
89 (remove-hook 'erc-timer-hook 'erc-autoaway-possibly-set-away)
90 (remove-hook 'erc-server-305-functions 'erc-autoaway-reset-indicators))))
92 (defcustom erc-autoaway-idle-method 'user
93 "*The method used to determine how long you have been idle.
94 If 'user, the time of the last command sent to Emacs is used.
95 If 'emacs, the idle time in Emacs is used.
96 If 'irc, the time of the last IRC command is used.
98 The time itself is specified by `erc-autoaway-idle-seconds'.
100 See `erc-autoaway-mode' for more information on the various
101 definitions of being idle."
102 :group 'erc-autoaway
103 :type '(choice (const :tag "User idle time" user)
104 (const :tag "Emacs idle time" emacs)
105 (const :tag "Last IRC action" irc))
106 :set (lambda (sym val)
107 (erc-autoaway-disable)
108 (set-default sym val)
109 (erc-autoaway-enable)))
111 (defcustom erc-auto-set-away t
112 "*If non-nil, set away after `erc-autoaway-idle-seconds' seconds of idling.
113 ERC autoaway mode can set you away when you idle, and set you no
114 longer away when you type something. This variable controls whether
115 you will be set away when you idle. See `erc-auto-discard-away' for
116 the other half."
117 :group 'erc-autoaway
118 :type 'boolean)
120 (defcustom erc-auto-discard-away t
121 "*If non-nil, sending anything when away automatically discards away state.
122 ERC autoaway mode can set you away when you idle, and set you no
123 longer away when you type something. This variable controls whether
124 you will be set no longer away when you type something. See
125 `erc-auto-set-away' for the other half.
126 See also `erc-autoaway-no-auto-discard-regexp'."
127 :group 'erc-autoaway
128 :type 'boolean)
130 (defcustom erc-autoaway-no-auto-discard-regexp "^/g?away.*$"
131 "*Input that matches this will not automatically discard away status.
132 See `erc-auto-discard-away'."
133 :group 'erc-autoaway
134 :type 'regexp)
136 (eval-when-compile (defvar erc-autoaway-idle-seconds))
138 (defun erc-autoaway-reestablish-idletimer ()
139 "Reestablish the Emacs idletimer.
140 If `erc-autoaway-idle-method' is 'emacs, you must call this
141 function each time you change `erc-autoaway-idle-seconds'."
142 (interactive)
143 (when erc-autoaway-idletimer
144 (erc-cancel-timer erc-autoaway-idletimer))
145 (setq erc-autoaway-idletimer
146 (run-with-idle-timer erc-autoaway-idle-seconds
148 'erc-autoaway-set-away
149 erc-autoaway-idle-seconds)))
151 (defcustom erc-autoaway-idle-seconds 1800
152 "*Number of seconds after which ERC will set you automatically away.
153 If you are changing this variable using lisp instead of customizing it,
154 you have to run `erc-autoaway-reestablish-idletimer' afterwards."
155 :group 'erc-autoaway
156 :set (lambda (sym val)
157 (set-default sym val)
158 (when (eq erc-autoaway-idle-method 'emacs)
159 (erc-autoaway-reestablish-idletimer)))
160 :type 'number)
162 (defcustom erc-autoaway-message
163 "I'm gone (autoaway after %i seconds of idletime)"
164 "*Message ERC will use when setting you automatically away.
165 It is used as a `format' string with the argument of the idletime
166 in seconds."
167 :group 'erc-autoaway
168 :type 'string)
170 (defvar erc-autoaway-last-sent-time (erc-current-time)
171 "The last time the user sent something.")
173 (defvar erc-autoaway-caused-away nil
174 "Indicates whether this module was responsible for setting the
175 user's away status.")
177 (defun erc-autoaway-reset-idle-user (&rest stuff)
178 "Reset the stored user idle time.
179 This is one global variable since a user talking on one net can
180 talk on another net too."
181 (when erc-auto-discard-away
182 (erc-autoaway-set-back))
183 (setq erc-autoaway-last-sent-time (erc-current-time)))
185 (defun erc-autoaway-reset-idle-irc (line &rest stuff)
186 "Reset the stored IRC idle time.
187 This is one global variable since a user talking on one net can
188 talk on another net too."
189 (when (and erc-auto-discard-away
190 (stringp line)
191 (not (string-match erc-autoaway-no-auto-discard-regexp line)))
192 (erc-autoaway-set-back))
193 (setq erc-autoaway-last-sent-time (erc-current-time)))
195 (defun erc-autoaway-set-back ()
196 "Discard the away state globally."
197 (let ((server-buffer (car (erc-buffer-list #'erc-server-buffer-p))))
198 (when (and erc-autoaway-caused-away
199 (with-current-buffer server-buffer (erc-away-p)))
200 (erc-cmd-GAWAY ""))))
202 (defun erc-autoaway-possibly-set-away (current-time)
203 "Set autoaway when `erc-auto-set-away' is true and the idletime is
204 exceeds `erc-autoaway-idle-seconds'."
205 ;; A test for (erc-server-process-alive) is not necessary, because
206 ;; this function is called from `erc-timer-hook', which is called
207 ;; whenever the server sends something to the client.
208 (when (and erc-auto-set-away
209 (not erc-autoaway-caused-away)
210 (not (erc-away-p)))
211 (let ((idle-time (erc-time-diff erc-autoaway-last-sent-time
212 current-time)))
213 (when (>= idle-time erc-autoaway-idle-seconds)
214 (erc-display-message
215 nil 'notice nil
216 (format "Setting automatically away after %i seconds of idle-time"
217 idle-time))
218 (erc-autoaway-set-away idle-time)))))
220 (defun erc-autoaway-set-away (idle-time)
221 "Set the away state globally."
222 ;; Note that the idle timer runs, even when Emacs is inactive. In
223 ;; order to prevent flooding when we connect, we test for an
224 ;; existing process.
225 (when (and (erc-server-process-alive)
226 (not (erc-away-p)))
227 (setq erc-autoaway-caused-away t)
228 (erc-cmd-GAWAY (format erc-autoaway-message idle-time))))
230 (defun erc-autoaway-reset-indicators (&rest stuff)
231 "Reset indicators used by the erc-autoaway module."
232 (setq erc-autoaway-last-sent-time (erc-current-time))
233 (setq erc-autoaway-caused-away nil))
235 (provide 'erc-autoaway)
237 ;;; erc-autoaway.el ends here
239 ;; Local Variables:
240 ;; indent-tabs-mode: t
241 ;; tab-width: 8
242 ;; End:
244 ;; arch-tag: 16fc241e-8358-4b56-9fe2-116bdd0ba3bc