1 ;;; erc-autoaway.el --- Provides autoaway for ERC
3 ;; Copyright (C) 2002, 2003, 2004, 2006, 2007 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)
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.
28 ;; - Legacy names: erc-auto-discard-away, erc-auto-set-away
34 (defgroup erc-autoaway nil
35 "Set yourself automatically away after some idletime and set
36 yourself back when you type something."
39 (defvar erc-autoaway-idletimer nil
41 This is only used when `erc-autoaway-idle-method' is set to 'emacs.")
43 (defvar erc-autoaway-last-sent-time
(erc-current-time)
44 "The last time the user sent something.")
46 (defvar erc-autoaway-caused-away nil
47 "Indicates whether this module was responsible for setting the
50 (eval-when-compile (defvar erc-autoaway-idle-seconds
))
52 (defun erc-autoaway-reestablish-idletimer ()
53 "Reestablish the Emacs idletimer.
54 If `erc-autoaway-idle-method' is 'emacs, you must call this
55 function each time you change `erc-autoaway-idle-seconds'."
57 (when erc-autoaway-idletimer
58 (erc-cancel-timer erc-autoaway-idletimer
))
59 (setq erc-autoaway-idletimer
60 (run-with-idle-timer erc-autoaway-idle-seconds
62 'erc-autoaway-set-away
63 erc-autoaway-idle-seconds
)))
65 (defun erc-autoaway-some-server-buffer ()
66 "Return some ERC server buffer if its connection is alive.
67 If none is found, return nil."
68 (car (erc-buffer-list #'erc-open-server-buffer-p
)))
70 (defun erc-autoaway-insinuate-maybe (&optional server
&rest ignored
)
71 "Add autoaway reset function to `post-command-hook' if at least one
74 This is used when `erc-autoaway-idle-method' is 'user."
75 (when (or server
(erc-autoaway-some-server-buffer))
76 (add-hook 'post-command-hook
'erc-autoaway-reset-idle-user
)))
78 (defun erc-autoaway-remove-maybe (&rest ignored
)
79 "Remove the autoaway reset function from `post-command-hook' if
80 no ERC process is alive.
82 This is used when `erc-autoaway-idle-method' is 'user."
83 (unless (erc-autoaway-some-server-buffer)
84 (remove-hook 'post-command-hook
'erc-autoaway-reset-idle-user
)))
86 ;;;###autoload (autoload 'erc-autoaway-mode "erc-autoaway")
87 (define-erc-module autoaway nil
88 "In ERC autoaway mode, you can be set away automatically.
89 If `erc-auto-set-away' is set, then you will be set away after
90 the number of seconds specified in `erc-autoaway-idle-seconds'.
92 There are several kinds of being idle:
94 User idle time measures how long you have not been sending any
95 commands to Emacs. This is the default.
97 Emacs idle time measures how long Emacs has been idle. This is
98 currently not useful, since Emacs is non-idle when it handles
99 ping-pong with IRC servers. See `erc-autoaway-idle-method'
100 for more information.
102 IRC idle time measures how long since you last sent something (see
103 `erc-autoaway-last-sent-time').
105 If `erc-auto-discard-away' is set, then typing anything, will
106 set you no longer away.
108 Related variables: `erc-public-away-p' and `erc-away-nickname'."
110 ((when (boundp 'erc-autoaway-idle-method
)
111 (add-hook 'erc-connect-pre-hook
'erc-autoaway-reset-indicators
)
112 (setq erc-autoaway-last-sent-time
(erc-current-time))
114 ((eq erc-autoaway-idle-method
'irc
)
115 (add-hook 'erc-send-completed-hook
'erc-autoaway-reset-idle-irc
)
116 (add-hook 'erc-server-001-functions
'erc-autoaway-reset-idle-irc
))
117 ((eq erc-autoaway-idle-method
'user
)
118 (add-hook 'erc-after-connect
'erc-autoaway-insinuate-maybe
)
119 (add-hook 'erc-disconnected-hook
'erc-autoaway-remove-maybe
)
120 (erc-autoaway-insinuate-maybe))
121 ((eq erc-autoaway-idle-method
'emacs
)
122 (erc-autoaway-reestablish-idletimer)))
123 (add-hook 'erc-timer-hook
'erc-autoaway-possibly-set-away
)
124 (add-hook 'erc-server-305-functions
'erc-autoaway-reset-indicators
)))
126 ((when (boundp 'erc-autoaway-idle-method
)
127 (remove-hook 'erc-connect-pre-hook
'erc-autoaway-reset-indicators
)
129 ((eq erc-autoaway-idle-method
'irc
)
130 (remove-hook 'erc-send-completed-hook
'erc-autoaway-reset-idle-irc
)
131 (remove-hook 'erc-server-001-functions
'erc-autoaway-reset-idle-irc
))
132 ((eq erc-autoaway-idle-method
'user
)
133 (remove-hook 'post-command-hook
'erc-autoaway-reset-idle-user
)
134 (remove-hook 'erc-after-connect
'erc-autoaway-insinuate-maybe
)
135 (remove-hook 'erc-disconnected-hook
'erc-autoaway-remove-maybe
))
136 ((eq erc-autoaway-idle-method
'emacs
)
137 (erc-cancel-timer erc-autoaway-idletimer
)
138 (setq erc-autoaway-idletimer nil
)))
139 (remove-hook 'erc-timer-hook
'erc-autoaway-possibly-set-away
)
140 (remove-hook 'erc-server-305-functions
'erc-autoaway-reset-indicators
))))
142 (defcustom erc-autoaway-idle-method
'user
143 "*The method used to determine how long you have been idle.
144 If 'user, the time of the last command sent to Emacs is used.
145 If 'emacs, the idle time in Emacs is used.
146 If 'irc, the time of the last IRC command is used.
148 The time itself is specified by `erc-autoaway-idle-seconds'.
150 See `erc-autoaway-mode' for more information on the various
151 definitions of being idle."
153 :type
'(choice (const :tag
"User idle time" user
)
154 (const :tag
"Emacs idle time" emacs
)
155 (const :tag
"Last IRC action" irc
))
156 :set
(lambda (sym val
)
157 (if erc-autoaway-mode
159 (erc-autoaway-disable)
161 (erc-autoaway-enable))
164 (defcustom erc-auto-set-away t
165 "*If non-nil, set away after `erc-autoaway-idle-seconds' seconds of idling.
166 ERC autoaway mode can set you away when you idle, and set you no
167 longer away when you type something. This variable controls whether
168 you will be set away when you idle. See `erc-auto-discard-away' for
173 (defcustom erc-auto-discard-away t
174 "*If non-nil, sending anything when away automatically discards away state.
175 ERC autoaway mode can set you away when you idle, and set you no
176 longer away when you type something. This variable controls whether
177 you will be set no longer away when you type something. See
178 `erc-auto-set-away' for the other half.
179 See also `erc-autoaway-no-auto-discard-regexp'."
183 (defcustom erc-autoaway-no-auto-discard-regexp
"^/g?away.*$"
184 "*Input that matches this will not automatically discard away status.
185 See `erc-auto-discard-away'."
189 (defcustom erc-autoaway-idle-seconds
1800
190 "*Number of seconds after which ERC will set you automatically away.
191 If you are changing this variable using lisp instead of customizing it,
192 you have to run `erc-autoaway-reestablish-idletimer' afterwards."
194 :set
(lambda (sym val
)
195 (set-default sym val
)
196 (when (eq erc-autoaway-idle-method
'emacs
)
197 (erc-autoaway-reestablish-idletimer)))
200 (defcustom erc-autoaway-message
201 "I'm gone (autoaway after %i seconds of idletime)"
202 "*Message ERC will use when setting you automatically away.
203 It is used as a `format' string with the argument of the idletime
208 (defun erc-autoaway-reset-idle-user (&rest stuff
)
209 "Reset the stored user idle time.
210 This is one global variable since a user talking on one net can
211 talk on another net too."
212 (when erc-auto-discard-away
213 (erc-autoaway-set-back #'erc-autoaway-remove-maybe
))
214 (setq erc-autoaway-last-sent-time
(erc-current-time)))
216 (defun erc-autoaway-reset-idle-irc (line &rest stuff
)
217 "Reset the stored IRC idle time.
218 This is one global variable since a user talking on one net can
219 talk on another net too."
220 (when (and erc-auto-discard-away
222 (not (string-match erc-autoaway-no-auto-discard-regexp line
)))
223 (erc-autoaway-set-back))
224 (setq erc-autoaway-last-sent-time
(erc-current-time)))
226 (defun erc-autoaway-set-back (&optional none-alive-func
)
227 "Discard the away state globally.
229 NONE-ALIVE-FUNC is the function to call if no ERC processes are alive."
230 (let ((server-buffer (erc-autoaway-some-server-buffer)))
231 (if (and erc-autoaway-caused-away
232 (buffer-live-p server-buffer
)
233 (with-current-buffer server-buffer erc-away
))
235 (when none-alive-func
(funcall none-alive-func
)))))
237 (defun erc-autoaway-some-open-server-buffer ()
238 "Return some ERC server buffer if its connection is alive and the
240 If none is found, return nil."
241 (car (erc-buffer-list (lambda ()
242 (and (erc-open-server-buffer-p)
245 (defun erc-autoaway-possibly-set-away (current-time)
246 "Set autoaway when `erc-auto-set-away' is true and the idletime is
247 exceeds `erc-autoaway-idle-seconds'."
248 ;; A test for (erc-server-process-alive) is not necessary, because
249 ;; this function is called from `erc-timer-hook', which is called
250 ;; whenever the server sends something to the client.
251 (when (and erc-auto-set-away
252 (not erc-autoaway-caused-away
)
253 (erc-autoaway-some-open-server-buffer))
254 (let ((idle-time (erc-time-diff erc-autoaway-last-sent-time
256 (when (>= idle-time erc-autoaway-idle-seconds
)
259 (format "Setting automatically away after %i seconds of idle-time"
261 (erc-autoaway-set-away idle-time t
)))))
263 (defun erc-autoaway-set-away (idle-time &optional notest
)
264 "Set the away state globally.
266 If NOTEST is specified, do not check to see whether there is an
267 activer server buffer available."
268 ;; Note that the idle timer runs, even when Emacs is inactive. In
269 ;; order to prevent flooding when we connect, we test for an
271 (when (or notest
(erc-autoaway-some-open-server-buffer))
272 (setq erc-autoaway-caused-away t
)
273 (erc-cmd-GAWAY (format erc-autoaway-message idle-time
))))
275 (defun erc-autoaway-reset-indicators (&rest stuff
)
276 "Reset indicators used by the erc-autoaway module."
277 (setq erc-autoaway-last-sent-time
(erc-current-time))
278 (setq erc-autoaway-caused-away nil
))
280 (provide 'erc-autoaway
)
282 ;;; erc-autoaway.el ends here
285 ;; indent-tabs-mode: t
289 ;; arch-tag: 16fc241e-8358-4b56-9fe2-116bdd0ba3bc