comparison lisp/erc/erc-autoaway.el @ 76856:2fae574a2382

Release ERC 5.2. I have updated the version of ERC to 5.2, since it fixes a bug with C-c C-SPC being bound globally by default. For the full list of changes in this version, see etc/ERC-NEWS. Revision: emacs@sv.gnu.org/emacs--devo--0--patch-687 Creator: Michael Olson <mwolson@gnu.org>
author Miles Bader <miles@gnu.org>
date Sun, 01 Apr 2007 13:36:38 +0000
parents 7a3f13e2dd57
children 85d67fae9a94
comparison
equal deleted inserted replaced
76855:e6686c0a3d45 76856:2fae574a2382
38 38
39 (defvar erc-autoaway-idletimer nil 39 (defvar erc-autoaway-idletimer nil
40 "The Emacs idletimer. 40 "The Emacs idletimer.
41 This is only used when `erc-autoaway-idle-method' is set to 'emacs.") 41 This is only used when `erc-autoaway-idle-method' is set to 'emacs.")
42 42
43 ;;;###autoload (autoload 'erc-autoaway-mode "erc-autoaway") 43 (defvar erc-autoaway-last-sent-time (erc-current-time)
44 (define-erc-module autoaway nil 44 "The last time the user sent something.")
45 "In ERC autoaway mode, you can be set away automatically. 45
46 If `erc-auto-set-away' is set, then you will be set away after 46 (defvar erc-autoaway-caused-away nil
47 the number of seconds specified in `erc-autoaway-idle-seconds'. 47 "Indicates whether this module was responsible for setting the
48 48 user's away status.")
49 There are several kinds of being idle:
50
51 User idle time measures how long you have not been sending any
52 commands to Emacs. This is the default.
53
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.
58
59 IRC idle time measures how long since you last sent something (see
60 `erc-autoaway-last-sent-time').
61
62 If `erc-auto-discard-away' is set, then typing anything, will
63 set you no longer away.
64
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))))
91
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.
97
98 The time itself is specified by `erc-autoaway-idle-seconds'.
99
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)))
110
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)
119
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)
129
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)
135 49
136 (eval-when-compile (defvar erc-autoaway-idle-seconds)) 50 (eval-when-compile (defvar erc-autoaway-idle-seconds))
137 51
138 (defun erc-autoaway-reestablish-idletimer () 52 (defun erc-autoaway-reestablish-idletimer ()
139 "Reestablish the Emacs idletimer. 53 "Reestablish the Emacs idletimer.
146 (run-with-idle-timer erc-autoaway-idle-seconds 60 (run-with-idle-timer erc-autoaway-idle-seconds
147 t 61 t
148 'erc-autoaway-set-away 62 'erc-autoaway-set-away
149 erc-autoaway-idle-seconds))) 63 erc-autoaway-idle-seconds)))
150 64
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)))
69
70 (defun erc-autoaway-insinuate-maybe (&optional server &rest ignored)
71 "Add autoaway reset function to `post-command-hook' if at least one
72 ERC process is alive.
73
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)))
77
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.
81
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)))
85
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'.
91
92 There are several kinds of being idle:
93
94 User idle time measures how long you have not been sending any
95 commands to Emacs. This is the default.
96
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.
101
102 IRC idle time measures how long since you last sent something (see
103 `erc-autoaway-last-sent-time').
104
105 If `erc-auto-discard-away' is set, then typing anything, will
106 set you no longer away.
107
108 Related variables: `erc-public-away-p' and `erc-away-nickname'."
109 ;; Enable:
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))
113 (cond
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)))
125 ;; Disable:
126 ((when (boundp 'erc-autoaway-idle-method)
127 (remove-hook 'erc-connect-pre-hook 'erc-autoaway-reset-indicators)
128 (cond
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))))
141
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.
147
148 The time itself is specified by `erc-autoaway-idle-seconds'.
149
150 See `erc-autoaway-mode' for more information on the various
151 definitions of being idle."
152 :group 'erc-autoaway
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
158 (progn
159 (erc-autoaway-disable)
160 (set sym val)
161 (erc-autoaway-enable))
162 (set sym val))))
163
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
169 the other half."
170 :group 'erc-autoaway
171 :type 'boolean)
172
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'."
180 :group 'erc-autoaway
181 :type 'boolean)
182
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'."
186 :group 'erc-autoaway
187 :type 'regexp)
188
151 (defcustom erc-autoaway-idle-seconds 1800 189 (defcustom erc-autoaway-idle-seconds 1800
152 "*Number of seconds after which ERC will set you automatically away. 190 "*Number of seconds after which ERC will set you automatically away.
153 If you are changing this variable using lisp instead of customizing it, 191 If you are changing this variable using lisp instead of customizing it,
154 you have to run `erc-autoaway-reestablish-idletimer' afterwards." 192 you have to run `erc-autoaway-reestablish-idletimer' afterwards."
155 :group 'erc-autoaway 193 :group 'erc-autoaway
165 It is used as a `format' string with the argument of the idletime 203 It is used as a `format' string with the argument of the idletime
166 in seconds." 204 in seconds."
167 :group 'erc-autoaway 205 :group 'erc-autoaway
168 :type 'string) 206 :type 'string)
169 207
170 (defvar erc-autoaway-last-sent-time (erc-current-time)
171 "The last time the user sent something.")
172
173 (defvar erc-autoaway-caused-away nil
174 "Indicates whether this module was responsible for setting the
175 user's away status.")
176
177 (defun erc-autoaway-reset-idle-user (&rest stuff) 208 (defun erc-autoaway-reset-idle-user (&rest stuff)
178 "Reset the stored user idle time. 209 "Reset the stored user idle time.
179 This is one global variable since a user talking on one net can 210 This is one global variable since a user talking on one net can
180 talk on another net too." 211 talk on another net too."
181 (when erc-auto-discard-away 212 (when erc-auto-discard-away
182 (erc-autoaway-set-back)) 213 (erc-autoaway-set-back #'erc-autoaway-remove-maybe))
183 (setq erc-autoaway-last-sent-time (erc-current-time))) 214 (setq erc-autoaway-last-sent-time (erc-current-time)))
184 215
185 (defun erc-autoaway-reset-idle-irc (line &rest stuff) 216 (defun erc-autoaway-reset-idle-irc (line &rest stuff)
186 "Reset the stored IRC idle time. 217 "Reset the stored IRC idle time.
187 This is one global variable since a user talking on one net can 218 This is one global variable since a user talking on one net can
190 (stringp line) 221 (stringp line)
191 (not (string-match erc-autoaway-no-auto-discard-regexp line))) 222 (not (string-match erc-autoaway-no-auto-discard-regexp line)))
192 (erc-autoaway-set-back)) 223 (erc-autoaway-set-back))
193 (setq erc-autoaway-last-sent-time (erc-current-time))) 224 (setq erc-autoaway-last-sent-time (erc-current-time)))
194 225
195 (defun erc-autoaway-set-back () 226 (defun erc-autoaway-set-back (&optional none-alive-func)
196 "Discard the away state globally." 227 "Discard the away state globally.
197 (let ((server-buffer (car (erc-buffer-list #'erc-server-buffer-p)))) 228
198 (when (and erc-autoaway-caused-away 229 NONE-ALIVE-FUNC is the function to call if no ERC processes are alive."
199 (with-current-buffer server-buffer (erc-away-p))) 230 (let ((server-buffer (erc-autoaway-some-server-buffer)))
200 (erc-cmd-GAWAY "")))) 231 (if (and erc-autoaway-caused-away
232 (buffer-live-p server-buffer)
233 (with-current-buffer server-buffer erc-away))
234 (erc-cmd-GAWAY "")
235 (when none-alive-func (funcall none-alive-func)))))
236
237 (defun erc-autoaway-some-open-server-buffer ()
238 "Return some ERC server buffer if its connection is alive and the
239 user is not away.
240 If none is found, return nil."
241 (car (erc-buffer-list (lambda ()
242 (and (erc-open-server-buffer-p)
243 (not erc-away))))))
201 244
202 (defun erc-autoaway-possibly-set-away (current-time) 245 (defun erc-autoaway-possibly-set-away (current-time)
203 "Set autoaway when `erc-auto-set-away' is true and the idletime is 246 "Set autoaway when `erc-auto-set-away' is true and the idletime is
204 exceeds `erc-autoaway-idle-seconds'." 247 exceeds `erc-autoaway-idle-seconds'."
205 ;; A test for (erc-server-process-alive) is not necessary, because 248 ;; A test for (erc-server-process-alive) is not necessary, because
206 ;; this function is called from `erc-timer-hook', which is called 249 ;; this function is called from `erc-timer-hook', which is called
207 ;; whenever the server sends something to the client. 250 ;; whenever the server sends something to the client.
208 (when (and erc-auto-set-away 251 (when (and erc-auto-set-away
209 (not erc-autoaway-caused-away) 252 (not erc-autoaway-caused-away)
210 (not (erc-away-p))) 253 (erc-autoaway-some-open-server-buffer))
211 (let ((idle-time (erc-time-diff erc-autoaway-last-sent-time 254 (let ((idle-time (erc-time-diff erc-autoaway-last-sent-time
212 current-time))) 255 current-time)))
213 (when (>= idle-time erc-autoaway-idle-seconds) 256 (when (>= idle-time erc-autoaway-idle-seconds)
214 (erc-display-message 257 (erc-display-message
215 nil 'notice nil 258 nil 'notice nil
216 (format "Setting automatically away after %i seconds of idle-time" 259 (format "Setting automatically away after %i seconds of idle-time"
217 idle-time)) 260 idle-time))
218 (erc-autoaway-set-away idle-time))))) 261 (erc-autoaway-set-away idle-time t)))))
219 262
220 (defun erc-autoaway-set-away (idle-time) 263 (defun erc-autoaway-set-away (idle-time &optional notest)
221 "Set the away state globally." 264 "Set the away state globally.
265
266 If NOTEST is specified, do not check to see whether there is an
267 activer server buffer available."
222 ;; Note that the idle timer runs, even when Emacs is inactive. In 268 ;; Note that the idle timer runs, even when Emacs is inactive. In
223 ;; order to prevent flooding when we connect, we test for an 269 ;; order to prevent flooding when we connect, we test for an
224 ;; existing process. 270 ;; existing process.
225 (when (and (erc-server-process-alive) 271 (when (or notest (erc-autoaway-some-open-server-buffer))
226 (not (erc-away-p)))
227 (setq erc-autoaway-caused-away t) 272 (setq erc-autoaway-caused-away t)
228 (erc-cmd-GAWAY (format erc-autoaway-message idle-time)))) 273 (erc-cmd-GAWAY (format erc-autoaway-message idle-time))))
229 274
230 (defun erc-autoaway-reset-indicators (&rest stuff) 275 (defun erc-autoaway-reset-indicators (&rest stuff)
231 "Reset indicators used by the erc-autoaway module." 276 "Reset indicators used by the erc-autoaway module."