Commit | Line | Data |
---|---|---|
597993cf MB |
1 | ;;; erc-autoaway.el --- Provides autoaway for ERC |
2 | ||
92ed2bfb | 3 | ;; Copyright (C) 2002, 2003, 2004, 2006 Free Software Foundation, Inc. |
597993cf MB |
4 | |
5 | ;; Author: Jorgen Schaefer <forcer@forcix.cx> | |
6 | ;; URL: http://www.emacswiki.org/cgi-bin/wiki.pl?ErcAutoAway | |
7 | ||
8 | ;; This file is part of GNU Emacs. | |
9 | ||
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. | |
14 | ||
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. | |
19 | ||
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. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; TODO: | |
28 | ;; - Legacy names: erc-auto-discard-away, erc-auto-set-away | |
29 | ||
30 | ;;; Code: | |
31 | ||
32 | (require 'erc) | |
33 | ||
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) | |
38 | ||
39 | (defvar erc-autoaway-idletimer nil | |
40 | "The Emacs idletimer. | |
41 | This is only used when `erc-autoaway-use-emacs-idle' is non-nil.") | |
42 | ||
43 | (defcustom erc-autoaway-use-emacs-idle nil | |
44 | "*If non-nil, the idle time refers to idletime in Emacs. | |
45 | If nil, the idle time refers to idletime on IRC only. | |
46 | The time itself is specified by `erc-autoaway-idle-seconds'. | |
47 | See `erc-autoaway-mode' for more information on the various | |
48 | definitions of being idle. | |
49 | ||
50 | Note that using Emacs idletime is currently broken for most versions, | |
51 | since process activity (as happens all the time on IRC) makes Emacs | |
52 | non-idle. Emacs idle-time and user idle-time are just not the same." | |
53 | :group 'erc-autoaway | |
54 | :type 'boolean) | |
55 | ||
56 | ;;;###autoload (autoload 'erc-autoaway-mode "erc-autoaway") | |
57 | (define-erc-module autoaway nil | |
58 | "In ERC autoaway mode, you can be set away automatically. | |
59 | If `erc-auto-set-away' is set, then you will be set away after | |
60 | the number of seconds specified in `erc-autoaway-idle-seconds'. | |
61 | ||
62 | There are several kinds of being idle: | |
63 | ||
64 | IRC idle time measures how long since you last sent something (see | |
65 | `erc-autoaway-last-sent-time'). This is the default. | |
66 | ||
67 | Emacs idle time measures how long Emacs has been idle. This is | |
68 | currently not useful, since Emacs is non-idle when it handles | |
69 | ping-pong with IRC servers. See `erc-autoaway-use-emacs-idle' for | |
70 | more information. | |
71 | ||
72 | User idle time measures how long you have not been sending any | |
73 | commands to Emacs, or to your system. Emacs currently provides no way | |
74 | to measure user idle time. | |
75 | ||
76 | If `erc-auto-discard-away' is set, then typing anything, will | |
77 | set you no longer away. | |
78 | ||
79 | Related variables: `erc-public-away-p' and `erc-away-nickname'." | |
80 | ;; Enable: | |
81 | ((add-hook 'erc-send-completed-hook 'erc-autoaway-reset-idletime) | |
82 | (add-hook 'erc-server-001-functions 'erc-autoaway-reset-idletime) | |
83 | (add-hook 'erc-timer-hook 'erc-autoaway-possibly-set-away) | |
84 | (when erc-autoaway-use-emacs-idle | |
85 | (erc-autoaway-reestablish-idletimer))) | |
86 | ;; Disable: | |
87 | ((remove-hook 'erc-send-completed-hook 'erc-autoaway-reset-idletime) | |
88 | (remove-hook 'erc-server-001-functions 'erc-autoaway-reset-idletime) | |
89 | (remove-hook 'erc-timer-hook 'erc-autoaway-possibly-set-away) | |
90 | (when erc-autoaway-idletimer | |
91 | (erc-cancel-timer erc-autoaway-idletimer) | |
92 | (setq erc-autoaway-idletimer nil)))) | |
93 | ||
94 | (defcustom erc-auto-set-away t | |
95 | "*If non-nil, set away after `erc-autoaway-idle-seconds' seconds of idling. | |
96 | ERC autoaway mode can set you away when you idle, and set you no | |
97 | longer away when you type something. This variable controls whether | |
98 | you will be set away when you idle. See `erc-auto-discard-away' for | |
99 | the other half." | |
100 | :group 'erc-autoaway | |
101 | :type 'boolean) | |
102 | ||
103 | (defcustom erc-auto-discard-away t | |
104 | "*If non-nil, sending anything when away automatically discards away state. | |
105 | ERC autoaway mode can set you away when you idle, and set you no | |
106 | longer away when you type something. This variable controls whether | |
107 | you will be set no longer away when you type something. See | |
108 | `erc-auto-set-away' for the other half. | |
109 | See also `erc-autoaway-no-auto-discard-regexp'." | |
110 | :group 'erc-autoaway | |
111 | :type 'boolean) | |
112 | ||
113 | (defcustom erc-autoaway-no-auto-discard-regexp "^/g?away.*$" | |
114 | "*Input that matches this will not automatically discard away status. | |
115 | See `erc-auto-discard-away'." | |
116 | :group 'erc-autoaway | |
117 | :type 'regexp) | |
118 | ||
119 | (eval-when-compile (defvar erc-autoaway-idle-seconds)) | |
120 | ||
121 | (defun erc-autoaway-reestablish-idletimer () | |
122 | "Reestablish the emacs idletimer. | |
123 | You have to call this function each time you change | |
124 | `erc-autoaway-idle-seconds', if `erc-autoaway-use-emacs-idle' is set." | |
125 | (interactive) | |
126 | (when erc-autoaway-idletimer | |
127 | (erc-cancel-timer erc-autoaway-idletimer)) | |
128 | (setq erc-autoaway-idletimer | |
129 | (run-with-idle-timer erc-autoaway-idle-seconds | |
130 | t | |
131 | 'erc-autoaway-set-away | |
132 | erc-autoaway-idle-seconds))) | |
133 | ||
134 | (defcustom erc-autoaway-idle-seconds 1800 | |
135 | "*Number of seconds after which ERC will set you automatically away. | |
136 | If you are changing this variable using lisp instead of customizing it, | |
137 | you have to run `erc-autoaway-reestablish-idletimer' afterwards." | |
138 | :group 'erc-autoaway | |
139 | :set (lambda (sym val) | |
140 | (set-default sym val) | |
141 | (when erc-autoaway-use-emacs-idle | |
142 | (erc-autoaway-reestablish-idletimer))) | |
143 | :type 'number) | |
144 | ||
145 | (defcustom erc-autoaway-message | |
146 | "I'm gone (autoaway after %i seconds of idletime)" | |
147 | "*Message ERC will use when he sets you automatically away. | |
148 | It is used as a `format' string with the argument of the idletime in | |
149 | seconds." | |
150 | :group 'erc-autoaway | |
151 | :type 'string) | |
152 | ||
153 | (defvar erc-autoaway-last-sent-time (erc-current-time) | |
154 | "The last time the user sent something.") | |
155 | ||
156 | (defun erc-autoaway-reset-idletime (line &rest stuff) | |
157 | "Reset the stored idletime for the user. | |
158 | This is one global variable since a user talking on one net can talk | |
159 | on another net too." | |
160 | (when (and erc-auto-discard-away | |
161 | (stringp line) | |
162 | (not (string-match erc-autoaway-no-auto-discard-regexp line))) | |
163 | (erc-autoaway-set-back line)) | |
164 | (setq erc-autoaway-last-sent-time (erc-current-time))) | |
165 | ||
166 | (defun erc-autoaway-set-back (line) | |
167 | "Discard the away state globally." | |
168 | (when (erc-away-p) | |
169 | (setq erc-autoaway-last-sent-time (erc-current-time)) | |
170 | (erc-cmd-GAWAY ""))) | |
171 | ||
172 | (defun erc-autoaway-possibly-set-away (current-time) | |
173 | "Set autoaway when `erc-auto-set-away' is true and the idletime is | |
174 | exceeds `erc-autoaway-idle-seconds'." | |
175 | ;; A test for (erc-server-process-alive) is not necessary, because | |
176 | ;; this function is called from `erc-timer-hook', which is called | |
177 | ;; whenever the server sends something to the client. | |
178 | (when (and erc-auto-set-away | |
179 | (not (erc-away-p))) | |
180 | (let ((idle-time (erc-time-diff erc-autoaway-last-sent-time | |
181 | current-time))) | |
182 | (when (>= idle-time erc-autoaway-idle-seconds) | |
183 | (erc-display-message | |
184 | nil 'notice nil | |
185 | (format "Setting automatically away after %i seconds of idle-time" | |
186 | idle-time)) | |
187 | (erc-autoaway-set-away idle-time))))) | |
188 | ||
189 | (defun erc-autoaway-set-away (idle-time) | |
190 | "Set the away state globally." | |
191 | ;; Note that the idle timer runs, even when Emacs is inactive. In | |
192 | ;; order to prevent flooding when we connect, we test for an | |
193 | ;; existing process. | |
194 | (when (and (erc-server-process-alive) | |
195 | (not (erc-away-p))) | |
196 | (erc-cmd-GAWAY (format erc-autoaway-message idle-time)))) | |
197 | ||
198 | (provide 'erc-autoaway) | |
199 | ||
200 | ;;; erc-autoaway.el ends here | |
201 | ;; | |
202 | ;; Local Variables: | |
203 | ;; indent-tabs-mode: t | |
204 | ;; tab-width: 8 | |
205 | ;; End: | |
206 | ||
207 | ;; arch-tag: 16fc241e-8358-4b56-9fe2-116bdd0ba3bc |