summaryrefslogtreecommitdiff
path: root/elisp/erbot/contrib/idledo.el
blob: a75f1c01fb9f58831663e832f4d2b12a10f06a55 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
;;; idledo.el --- do stuff when emacs is idle..
;; Time-stamp: <2004-11-14 22:37:04 deego>
;; Copyright (C) Deepak Goel 2001
;; Emacs Lisp Archive entry
;; Filename: idledo.el
;; Package: idledo
;; Author: Deepak Goel <deego@gnufans.org>
;; Keywords:  idle startup speed timer 
;; Version: 0.3
;; Author's homepage: http://deego.gnufans.org/~deego
;; REQUIRES: timerfunctions.el 1.2.7 or later.
;; ALSO uses: emacs' ('cl during compile.. for all the backquoting..)
;; For latest version:

(defvar idledo-home-page  
  "http://deego.gnufans.org/~deego/emacspub/lisp-mine/idledo")

;; Requires: timerfunctions.el
;; See also: Jari's tinyload.el, dope.el. 

 
;; This file is NOT (yet) part of GNU Emacs.
 
;; This is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
 
;; This is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
 



;; Quick start:
(defvar idledo-quick-start
  "Drop idledo.el and timerfunctions.el somewhere in your
load-path. In your .emacs, type (require 'idledo) and (require
'timerfunctions).  In there, also create idledo-list-- a list of
expresions, either by hand, or by using one of the many functions and
macros provided.  Then, write (idledo-start), and idledo will start
doing the tasks mentioned in the idledo-list whenever emacs is idle.

Here, for example, are some
possible lines of code from a .emacs--->

  
  (idledo-require 'bbdb 'bbdb-com 'bbdb-gnus)
  (idledo-add-action
   '(progn      (unless (file-locked-p \"~/emacs/.bbdb\")
	(bbdb-records))
      nil))
  (idledo-require-now 'mailabbrev)
  ;; as below, or simply (idledo-gc)
  (idledo-add-action 
		       '(garbage-collect))

  (idledo-add-action '(load \"aliases-my\"))

  (idledo-add-action '(progn
			(garbage-collect)
			nil))

  (idledo-load \"mode-hook-functions-my\")
  (add-to-list 'idledo-list '(progn (message \"Just a sample\")))
  (idledo-require 'disp-table)
  (idledo-require 'gnus-score 'gnus 'gnus-msg)

A simple long example is (idledo-example-setup) which can be called
from your .emacs.  Alternatively, a more complicated example of how to
set up idledo-list can be seen in the function idledo-example.  That
one tries to save even more time by: moving the task of setting up an
idledo-list itself into the first idledo, and on top of that, calls
idledo-start not from emacs, but from an idle-timer.

To maintain idledo-history, see idledo-after-action-hooks

This author currently uses exactly 105 idledo's.
PS: timerfunctions.el can be obtained from:
http://deeego.gnufans.org/~deego/emacspub/lisp-mine/timerfunctions/"
)

(defun idledo-quick-start ()
  "Provides electric help for function `idledo-quick-start'."
  (interactive)
  (with-electric-help
   '(lambda () (insert idledo-quick-start) nil) "*doc*"))

;;; Introduction:
;; Stuff that gets posted to gnu.emacs.sources
;; as introduction
(defvar idledo-introduction
  "Idledo does stuff for you when emacs is idle. 


The actions can be simple one-time actions or repetitive.  You can
include as many actions as you want.  Thus, with apprpriate actions,
if you leave emacs running for sometime, take a break and come back,
your emacs should have (require)'d almost everything you will ever
need..you can now start your gnus or eshell or w3 instantly.. When you
are using gnus, you can check mail periodically.. Make
color-theme-random a periodic idledo and you can convert emacs into a
shapeshifting color-changing aquarium..

idledo will probably someday be interfaced with a prioritizer, which
will include all sorts of enhanced capabilites, like weighting of
repetitive actions etc.

See also M-x idledo-quick-start
"
)

;;;###autoload
(defun idledo-introduction ()
  "Provides electric help for function `idledo-introduction'."
  (interactive)
  (with-electric-help
   '(lambda () (insert idledo-introduction) nil) "*doc*"))

;;; Commentary:
(defvar idledo-commentary
  "First type M-x idledo-introduction.
Also see M-x idledo-quick-start

You give idledo a list of commands, and it will eval them when emacs is
idle and you are away..  Thus, if you take a hike and come back, your
w3, gnus, eshell should all start instantly..  Your gnus-news should
be checked periodically for you.. and *Group* buffer updated.. of
course, you have to set this all up :/)

If emacs is idle *not* because you are away, but because you are
deeply absorbed using info, you probably don't want idledo springing into
action and loading eshell for you.. So, idledo tries to alert you before
loading anything, and gives you enough time to cancel any action
before it is taken..

As an example, see the function idledo-example.  I call that function
from my .emacs as follows..

/(idledo-example/)

where:

Note: If you specify many idle-loads and thus make your emacs very big
with (idle) time, your emacs will get slow and do frequent gc.  Some
remedies:

* First, turn garbage-collection messages on to see what i am sayin,
  for yourself: (setq garbage-collection-messages t) in .emacs

* Next, increase gc-cons-threshold to say, 10 times its value:
 (setq gc-cons-threshold 40000000) in .emacs.

* Finally, ask idledo to do garbage-collections for you when emacs is
  idle.  See an example in idledo-example-setup.  In that example, once
  all my other idledo's are taken care of, emacs then alternates
  between doing garbage-collection and color-theme-random when it is
  idle..  Thus, trying to ensure that when I get back to work, least gc
  takes place...


0.1 new features:
* Now called idledo, to avoid a name-conflict with another package.
  Sorry about that, and Thanks to all who pointed this out.
* Macros like ido-add-require now called idledo-require.
* Minor bug fixed in idledo-add-periodic-action-to-beginning-crud
"  )

(defun idledo-commentary ()
  "Provides electric help for function `idledo-commentary'."
  (interactive)
  (with-electric-help
   '(lambda () (insert idledo-commentary) nil) "*doc*"))

;;; History:

;;; New features:
(defvar idledo-new-features
  "New in 0.3:
Some Bugfixes. Made compatible with the current
timerfunctions.el--posted here.
Improved doc."
)

(defun idledo-new-features ()
  "Provides electric help for function `idledo-new-features'."
  (interactive)
  (with-electric-help
   '(lambda () (insert idledo-new-features) nil) "*doc*"))

(defvar idledo-version "0.3")

(defvar idledo-todo
"TODO:
* Ideally, one should be able to cancel the timer if idledo-list
   becomes nil.

* Write a prioritizer, and interface the same with idledo. The priotizer
  should. among other things like weights and \(arbitrarily specified\)
  repetitivity, try to support different idle times for different
  tasks.."
)


;;==========================================
;;; Code:
(defgroup idledo nil
  "idledo.el --- do stuff when emacs is idle..  "
  :group 'applications)

(defcustom idledo-before-load-hooks nil "."
  :type 'hook
  :group 'idledo
  )
(defcustom idledo-after-load-hooks nil "."
  :type 'hook
  :group 'idledo
  )
(run-hooks 'idledo-before-load-hooks)
(eval-when-compile (require 'cl))

(defcustom idledo-before-action-hooks nil
  "

This hook is run even if idledo-once is callesd byhand. 

"
  :type 'hook
  :group 'idledo
  )

(defcustom idledo-before-idle-action-hooks nil
  "."
  :type 'hook
  :group 'idledo
  )

(defcustom idledo-after-action-hooks nil 
  "Hooks to run after performing idledo-actions.
You could insert the command idledo-history-update into this hook. 
This hook is run even if idledo-once is called by hand. 
"
  :type 'hook
  :group 'idledo)

(defcustom idledo-after-idle-action-hooks nil 
  "Hooks to run after performing idledo-actions.
You could insert the command idledo-history-update into this hook. 
"
  :type 'hook
  :group 'idledo)

(defcustom idledo-before-possible-action-hooks nil "."
  :type 'hook
  :group 'idledo)

(defcustom idledo-after-possible-action-hooks nil "."
  :type 'hook
  :group 'idledo)



(defcustom idledo-list nil
  "A list of actions to perform.."
  :type 'list
  :group 'idledo
  )


(defcustom idledo-verbosity 0
  "Suggested: Anywhere from -100 to 100.

The design is such that a value of 0 should be optimum.
viz.: Once you are experienced with this library, you might prefer a value
of 0 for this variable if this is > 0 right now."
  :type 'integer
  :group 'idledo
)

(defvar idledo-active-p nil
  "If t, no more idledo's can be initiated..
The aim is to only have one idledo active at a time.

Why?  I don't know.  You can easily setq this to nil, and start yet
another `idledo-start' if you want.

Why do i want only one idledo at a time?  My experience is that \(GNU\)
Emacs bahaves unpredictably if the activation of 2 or more timers
collide... maybe i am wrong?  It seems to me that sometimes, both get
executed, someimtes one, and sometimes none..  Although the one or
none situations seem to be rare, each of thses situations can be
potentially bad..particularly if: Suppose the timer is a
self-reinforcing timer \(as can be done by calls to
`tf-run-with-idle-timer'\).  Then, the very first time it fails to get
executed, the process gets killed and you want get those cherished
repetitions as long as Emacs remains idle.."
)

(defcustom idledo-interval 30
  "The interval to wait the first time Emacs goes idle..
An additional small interval will be allowed to enable the user to
cancel the action.

Note that you can assign to this this interval any expression that
will be eval'ed at run-time \(see timerfunctions.el for more details..\)"
  :type 'list
  :group 'idledo
)

(defcustom idledo-interval-subsequent 1
  "When Emacs remains idle, time to wait before next action.

Time is in seconds.. floats might work too.
Note that you can assign to this this interval any expression that
will be eval'ed at run-time \(see timerfunctions.el for more details..\)"
  :type 'list
  :group 'idledo
)

(defcustom idledo-interval-small  5
  "Time to warn for before performing the imminent idledo.

Before beginning any action, idledo will flash a warning, and will
wait for these many seconds.. if you do something in this time, the
action will be cancelled.

Note that you can assign to this this interval any expression that
will be eval'ed at run-time \(see timerfunctions.el for more details..\)"  :type 'hook
  :group 'idledo
)

(defvar idledo-timer nil
  "The timer stored here.. so can be cancelled.. Internal..")

(defvar idledo-last-action nil
  "Will store the last action. 
--if the user needs this for any purpose. ")
(defvar idledo-last-result nil
  "The result of the eval of the last idledo-action.
provided in case the user needs this. ")

(defvar idledo-history nil
  "Stores, optionally, the reverse-history of idledo-actions and their
results. ")

(defcustom idledo-history-max-length 100
  "Max length of history to maintain. Nil means no limit.
When length exceeded, oldest entries are discarded. "
:group 'idledo
)

(defvar idledo-counter 0
  "The number of idledos performed.  ")



(defun idledo-history ()
  (interactive)
  (message "idledo-counter: %S   idledo-history: %S" 
	  idledo-counter idledo-history))

(defun idledo-history-update ()
  (interactive)
  (push (list (copy-tree idledo-last-action)
	      (copy-tree idledo-last-result))
	idledo-history)
  (setq idledo-counter (+ 1 idledo-counter))
  (while (and (integerp idledo-history-max-length)
	      (> (length idledo-history) idledo-history-max-length))
    (setq idledo-history (reverse (cdr (reverse idledo-history))))))


(defun idledo-start-forced-risky ()
  "Internal.
USED ONLY FOR DEBUGGING.. USE AT YOUR OWN RISK.. STARTS A PARALLEL
version of idledo if there already exists one..."
  (interactive)
  (tf-run-with-idle-timer
   'idledo-interval t
   'idledo-interval-subsequent
   t nil
   'idledo-one-action))


;;;###autoload
(defun idledo-stop ()
  "Stop any idledo."
  (interactive)
  (when (timerp idledo-timer)
    (cancel-timer idledo-timer))
  (setq idledo-active-p nil))



;;;###autoload
(defun idledo-start ()
  "Start idledo.

See also `idledo-active-p'.  Also returns the timer."
  (interactive)
  (if (not idledo-active-p)
      (progn
	(idledo-stop)
	(setq idledo-active-p t)
	(setq idledo-timer
	      (tf-run-with-idle-timer
	       'idledo-interval t
	       'idledo-interval-subsequent
	       t nil
	       'idledo-one-action)))
    (error "Idledo is already active")))
    
(defcustom idledo-interval-done 1
  "Time to wait before showing the 'done' message.
Idledo will wait for this much time before flashing a 'done-action'
message"
  :group 'idledo
)


(defcustom idledo-action-imminent-string
  "idledo imminent unless keypress ---> "
  "The `idledo-action-imminent-string'."
  :type 'string
  :group 'idledo
)

(defun idledo-one-action ()
  "Internal.
Does one instance of processing of action."
  (when (not (null idledo-list))
    (run-hooks 'idledo-before-possible-action-hooks)
    (idledo-message 25
     (concat idledo-action-imminent-string
	     (idledo-shorten (format "%S" (car idledo-list)))))
    (if (sit-for idledo-interval-small)
	(progn
	  (run-hooks 'idledo-before-idle-action-hooks)
	  (idledo-once 1)
	  (run-hooks 'idledo-after-idle-action-hooks)
	  (sit-for idledo-interval-done)
	  (idledo-message 60 "%S more idledo(s) remainig.. "
		   (length idledo-list)))
      
      
      (idledo-message 20
       (concat "IDLEDO's action canceled.."
	       (idledo-shorten (format "%S" (car idledo-list)))))
      )
    (run-hooks 'idledo-after-possible-action-hooks)))

(defun idledo-all ()
  "Tell the amount of time saved through idledo's.
Start emacs and run M-x idledo-all.  That will run all your
idledo's at once and show you how much time all of that took. 

More like, it will run as many idledo's as there are currently in
your idledo-list, which may not correspond to ALL idledo's since you
may have repetitive idledo's"
  (interactive)
  (let ((ta (current-time))
	(len (length idledo-list))
	tb tott)
    (idledo-once len)
    (setq tb (current-time))
    (setq tott (idledo-time-diff tb ta))
    (message "That took %S milliseconds. " tott)))


(defun idledo-time-diff (tb ta)
  "Get the difference bet times TB and TA, in milliseconds.  A float."
  (+
   (* 0.001 (- (caddr tb) (caddr ta)))
   (* 1000.0
      (+
       (- (second tb) (second ta))
       (* 65536.0
	  (- (car tb) (car ta)))))))

;;;###autoload
(defun idledo-once (arg)
  "Call this if you wanna run something in yr `idledo-list' NOW...
Provide numerical prefix ARG for multiple arguments...
but note that doesn't run after-action hooks etc."
  (interactive "p")
  (while
      (>= arg 1)
    (setq arg (- arg 1))
    (run-hooks 'idledo-before-action-hooks)
    (progn
      (idledo-message 20
       (concat "IDLEDO doing action.."
	       (idledo-shorten (format "%S" (car idledo-list)))))
      (let ((carval (car idledo-list)))
	(setq idledo-last-action carval)
	(setq idledo-list (cdr idledo-list))
	(setq idledo-last-result
	      (idledo-ignore-errors (eval  carval)))))
    (run-hooks 'idledo-after-action-hooks)

    ))

  
  
(defun idledo-add-periodic-action-crude (action)
  "Add a action to `idledo-list' to be repeated endlessly.
Is a crude mechanism for adding action to the `idledo-list' and make it
repetitive.  ACTION is a (quoted) list which will be evaled to perform an
eval.

Note that the ACTION this way is added to the END of `idledo-list'.
And ACTION is added to list no matter what (even if there is a similar
action already waiting in the list)."
  (setq
   idledo-list
   (append
    idledo-list
    (list
     `(progn
	,action
	(idledo-add-periodic-action-crude
	 (quote ,action)))))))

(defun idledo-add-periodic-action-to-beginning-crude (action)
  "Add an action to `idledo-list' to be repeated endlessly.

Is a crude mechanism for adding action to the `idledo-list' and make it
periodic.  ACTION is a list which will be evaled to perform an
eval.
Note that the ACTION this way is added to the BEGINNING and subsequent
calls are also added to the beginning of the list.
And ACTION is added to list no matter what (even if there is a similar
action already waiting in the list)."
  (idledo-add-action-forced
   `(progn
      ,action
      (idledo-add-periodic-action-to-beginning-crude
       (quote ,action)))))


   

;;;###autoload
(defun idledo-add-to-end-of-list (list-var element)
  "Like `add-to-list', but add at the end, if at all.

Add to the end of the list LIST-VAR, the element ELEMENT"
  (if (member element (symbol-value list-var))
      (symbol-value list-var)
    (set list-var (append  (symbol-value list-var) (list element)))))

(defun idledo-add-action (action)
  "Add ACTION to ideldo-list.

ACTION is an expression to be evaled.  Action is added at the
beginning if at all.  See similar commands too."
  (add-to-list 'idledo-list action))

(defun idledo-add-action-forced (action)
  "Add action ACTION to `idledo-list' even if it already exists."
  (setq idledo-list (cons action idledo-list)))

(defun idledo-add-action-at-end (&rest actions)
  "Add actions ACTIONS to the end of `idledo-list'."
  (mapcar
   (lambda (action)
     (idledo-add-to-end-of-list 'idledo-list action))
   actions))

(defmacro idledo-load (&rest files)
  "Add, for each of FILES,  a (load file) action to `idledo-list'."
  (cons 'progn
	(mapcar
	 (lambda (arg)
	   `(idledo-add-action-at-end '(load ,arg)))
	 files)))

;;; 2001-11-03 T13:42:01-0500 (Saturday)    Deepak Goel
(defmacro idledo-load-now (&rest files)
  "Add, for each of FILES, a (load-file) action to `idledo-list'.

The action is added to the beginning of `idledo-list'."
  (cons 'progn
	(mapcar
	 (lambda (arg)
	   `(idledo-add-action '(load ,arg)))
	 files)))


(defmacro idledo-require (&rest features)
  "Add, for each of the FEATURES, a (require) action to `idledo-list'."
  (cons 'progn
	(mapcar
	 (lambda (arg)
	   `(idledo-add-action-at-end '(require ,arg)))
	 features)))


(defmacro idledo-require-now (feature)
  "Add a (require FEATURE) action to `idledo-list'.

The addition is done to the beginning of `idledo-list'."
  `(idledo-add-action '(require ,feature)))

(defun idledo-add-action-at-end-forced (action)
  "Add ACTION to the end of `idledo-list'.

Action is added even if it exists in the list already."
  (setq idledo-list (append idledo-list (list action))))

(defun idledo-initialize (initial-list)
  "Initialize `idledo-list' to INITIAL-LIST."
  (setq idledo-list initial-list))

(defun idledo-remove-action (action)
  "Remove ACTION from `idledo-list'."
  (idledo-remove-from-list 'idledo-list action))

(defun idledo-remove-from-list (listname elt)
  "INTERNAL.

Remove, from list LISTNAME, element ELT."
  (set listname (idledo-list-without-element
		 (eval listname)
		 elt)))

(defun idledo-list-without-element (list elt)
  "INTERNAL.
Returns the value of the LIST without the element ELT."
  (if (null list)
      list
    (if (equal (car list) elt)
	(idledo-list-without-element (cdr list) elt)
      (cons
       (car list)
       (idledo-list-without-element
	(cdr list) elt)))))


;; Thanks to Kim F. Storm for the suggestion: 
(defun idledo-gc ()
  (idledo-add-action '(garbage-collect)))

(defun idledo-shorten (string)
  "Internal, return a shortened version with no newlines.
Internal, returns a shortened version of STRING with no newlines."
  (let
      ((string-no-enter
	(with-temp-buffer
	  (insert string)
	  (goto-char (point-min))
	  (while (search-forward "\n" nil t)
	    (replace-match " " nil t))
	  (buffer-substring (point-min) (point-max)))))
    (if (> (length string-no-enter) 55)
	(substring string-no-enter 0 55)
		   string-no-enter)))


(defcustom idledo-ignore-errors-p t
  "This should always be t unless you know what you are doing.

For regular idledo's if this is not t and an error occurs, this means
that your entire idle-timer might get canceled due to the error.  The
only place where this = nil makes sense is when you are running M-x
idledo-once by hand and want to debug the idledo action which is
giving you an error.  See also idledo-toggle-ignore-errors.")

(defun idledo-toggle-ignore-errors-p (&optional arg)
  "See idledo-ignore-errors-p. "
  (interactive "P")
  (let ((num (prefix-numeric-value arg)))
    (cond
     ((or (not arg) (equal num 0))
      (setq idledo-ignore-errors-p (not idledo-ignore-errors-p)))
     ((> num 0) (set idledo-ignore-errors-p t))
     ((< num 0) (set idledo-ignore-errors-p nil)))
    (message "Symbol %S set to %S" 
	     'idledo-ignore-errors-p
	     idledo-ignore-errors-p)
    idledo-ignore-errors-p))


(defmacro idledo-ignore-errors (&rest body)
   "Like `ignore-errors', but tell the error..

A wrapper around the BODY."

   (if idledo-ignore-errors-p
       (let ((err (gensym)))
	 `(condition-case ,err (progn ,@body)
	    (error
	     (ding t)
	     (ding t)
	     (ding t)
	     (idledo-message 90 "IGNORED ERROR: %s" 
			     (error-message-string ,err))
	     (sit-for 1)
	     nil)))
     `(progn ,@body)))


;;;###autoload
(defun idledo-example ()
  "Sample of code to include in your .emacs..
See this and `idledo-example-setup'.
Define a similar function idledo-yourname for yourself in your .emacs,
and call it in yr .emacs by inserting (idledo-yourname) somewhere.

See \\[idledo-quick-start] for simple examples.

This function tries to go one step further to and defers the setting
up of the `idledo-list' itself to a time when Emacs goes idle, so as to
try to save more .emacs loading time."
  (interactive)
  (message "Setting up idledo and starting it..")
  ;; testing
  ;;(setq idledo-interval 300)

  ;;(setq idledo-list nil)
  (idledo-add-action-at-end '(idledo-example-setup))
  
  
  (setq idledo-action-imminent-string
	"idledo imminent--> ")
  (idledo-start)
  (message "Setting up idledo and starting it..done")

)

  

(defun idledo-message (points &rest args)
  "Signal message, depending on POINTS and `idledo-verbosity'.
ARGS are passed to `message'."
  (unless (minusp (+ points idledo-verbosity))
    (apply #'message args)))

(defcustom idledo-message-nice-sit 1 "" :group 'idledo)

(defun idledo-message-nice (points &rest args)
  (unless (minusp (+ points idledo-verbosity))
    (with-temp-message (apply 'format args)
      (sit-for 0.5))))

;;;###autoload
(defun idledo-length-list ()
  "For you to quickly find the length of idledo-list..
If you use idledo bigtime, you will frequently find yourself wanting
to find out the length.. and you don't want to eval that parenthesised
expression all the time.. perhaps.."
  (interactive)
  (idledo-message
   (if (interactive-p) 135 35)
   "%s"
   (format "Length=  %S     ... %S..." (length idledo-list)
	   (first idledo-list)))
  (length idledo-list))














(defun idledo-example-setup ()
  "Called by `idledo-example'.
This extra step is taken so that setting
up idledo itself takes place only when Emacs has gone idle..
This function is actually used by this user's .emacs. 
"
  ;; The preference in all of below should be to load stuff that takes
  ;; time asap.. small libraries can always be loaded later.. or even
  ;; if they are not loaded, they do not make the user wait anyways
  ;; when they finally get  loaded..

  ;; once bbdb is loaded.. let's get the frobnicating stuff over with..
  
  ;; made interactive only for test purposes..
  (interactive)
  
  ;; hm, i now prefer directly setting the idledo list... 
  

  (idledo-gc)
  
  (idledo-require 'bbdb 'bbdb-com 'bbdb-gnus)
  (idledo-add-action
   '(progn
      (require 'bbdb)
      (when (boundp 'bbdb-file)
	(unless (file-locked-p  bbdb-file)
	  (bbdb-records)))
      nil))
  (idledo-require-now 'mailabbrev)
  (idledo-add-action '(progn
			(garbage-collect)
			nil))
  (idledo-load "gnus-functions-my")
  (idledo-load "macros-my")
  (idledo-add-action '(load "aliases-my"))
  (idledo-gc)

  (idledo-load "mode-hook-functions-my")
  (idledo-require 'disp-table)
  (idledo-require 'gnus-score 'gnus 'gnus-msg)
  (idledo-require 'gnus-cache)
  (idledo-require 'gnus-ml 'gnus-cite)
  (idledo-require 'timerfunctions)

  ;;maybe emacs needs a GC now.. we need to make sure GC is done when
  ;;emacs is idle..
  (idledo-gc)

  (idledo-require 'esh-mode
		  'em-alias)

  (idledo-require 'em-banner 'em-basic 'em-cmpl 'em-dirs 'em-glob
		  'em-hist 'em-ls 'em-prompt 'em-script 'em-term
		  'em-xtra 'etags
		  'ange-ftp
		  ;; no longer needed since pcomplete is now bundled
		  ;; with emacs (21..)
		  ;;'pcmpl-auto
		  'pcomplete
		  ;; 2002-05-02 T11:57:07-0400 (Thursday)    D. Goel
		  'shellhist
		  ;; 2002-05-02 T11:57:25-0400 (Thursday)    D. Goel
		  'pcmpl-unix
		  
		  ;; no longer needed since eshell is now bundled
		  ;; with emacs (21)
		  ;;'eshell-auto

		  'em-unix 'bytecomp 'eshell 'runshell )
  (idledo-add-action '(progn
			(garbage-collect)
			nil))
  (idledo-add-action '(progn
			(recentf-mode 1)
			nil))
  (idledo-load "cl-seq")

  (idledo-require 'autokey)
  (idledo-require 'thingatpt 'ispell 'info)
  (idledo-require 'elder)
  
  (idledo-require 'mail-extr )
  (idledo-require 'autorevert 'view)
  (idledo-require 'time-stamp )
  (idledo-require 'imenu)
  (idledo-load "kinsoku")
  (idledo-require 'edlib )
  (idledo-require 'phonemode)
  (idledo-add-action '(progn
			(garbage-collect)
			nil))
  
  ;; bytecomp should be required before this...
  (idledo-add-action-at-end '(load "byte-opt"))
  
  ;;(idledo-load 'tex-mode)
  (idledo-require 'boxquote)
  (idledo-require 'dired)
  (idledo-require 'dired-x)
  (idledo-require 'bytecomp)
  (idledo-require 'find-func)
  (idledo-require 'diff 'diff-mode)
  (idledo-require 'add-log)
  (idledo-require 'calendar)
  (idledo-require 'mule-util)
  (idledo-require 'cal-move)
  (idledo-require 'advice)
  (idledo-require 'browse-kill-ring)
  ;; add for fsbot
  (idledo-require 'browse-url)
  (idledo-add-action '(progn
			(garbage-collect)
			nil))

  (idledo-require 'debug)
  ;;(idledo-require 'ell)
  (idledo-require 'table)
  (idledo-require 'tabify)
  (idledo-require 'edebug)
  ;; 2002-04-25 T15:43:21-0400 (Thursday)    Deepak Goel
  ;; this will shorten the time it takes to find a tag..
  (idledo-add-action
   '(progn
      (visit-tags-table  "~/TAGS")
      nil))
  (idledo-require 'gnus-cus)
  (idledo-require 'gnus-async)
  ;;(idledo-require 'smiley)
  ;;(idledo-add-action
  ;;(progn
  ;; (require 'smiley "smiley-ems")
  ;;nil))
  (idledo-require 'cus-edit)
  (idledo-require 'newcomment)
  (idledo-require 'genauto)
  (idledo-require 'mkback)
  (idledo-add-action '(progn
			(mkback-install-for-eshell)
			nil))
  (idledo-require 'flow-fill)
  (idledo-require 'findutils)
  (idledo-require 'erc)
  (idledo-add-periodic-action-crude
   '(progn
      (garbage-collect) nil))

					;  (idledo-add-action
					;   '(progn
					;      (numshift-install)
					;      nil))
  (idledo-add-action
   '(progn
      (if (display-mouse-p)
	  (mouse-avoidance-mode 'animate))
      nil))
  (idledo-add-action
   '(progn
      (iswitchb-mode 1)
      nil))
  (idledo-require 'spook)
  (idledo-require 'autoinsert)
  (idledo-require 'sregex)
  (idledo-require 'choose)
  (idledo-require 'erc-complete)
  (idledo-require 'buffer-stack)
  (idledo-require 'emacs-wiki)
  (idledo-require 'planner)
  (idledo-add-action
   '(progn
      (require 'eldoc)
      (utils-add-minor-mode 'lisp-mode 'eldoc-mode)
      (utils-add-minor-mode 'emacs-lisp-mode 'eldoc-mode)))
   


  (idledo-add-action '(progn
			(global-font-lock-mode t)
			nil))


  (idledo-add-action
   '(progn
      (if
	  (locate-library "bbdb" nil nil)
	  (require 'bbdb)
	(message "NO BBDB found..."))
      nil))


  (idledo-add-action
   (progn
     ;; CVS's type break currently has an annoying "sabve file? "
     ;; question. 
     (when (< emacs-minor-version 3)
       (type-break-mode 1))
     nil))

  (idledo-require 'emacs-wiki)
  

  ;; top priority stuff...
  (idledo-add-action
   '(progn
      ;; do we still need all of these for emacs21?
      (ignore-errors-my
       (add-to-list 'ispell-skip-region-alist
		    '("\\\\[a-z]?ref{". "}"))
       (add-to-list 'ispell-skip-region-alist
		    '("\\[\\[\\$". "\\$\\]\\]")) ; for latex..
       (add-to-list 'ispell-skip-region-alist
		    '("\\\\[a-z]?cite{". "}"))
       (add-to-list 'ispell-skip-region-alist
		    '("\\\\begin{al[a-z]*}" . "\\\\end{al[a-z]*}"))
       (add-to-list 'ispell-skip-region-alist
		    '("(\\[ebf\\]ll". "\\[eef\\])")) ; see the function
					; regexp-quote..
       )))


  (idledo-add-action
   '(windmove-default-keybindings))
  ;;(idledo-add-action
  ;;`(progn
  ;;   (load "chess-auto")
  ;;   nil))
  ;;(idledo-require 'scroll-in-place)
  (idledo-require 'auto-recompile)
  (idledo-add-action
   '(progn
      (require 'elder-beautify)
      (elder-beautify-latex)
      nil))
  
  (idledo-add-action
   (progn
     (ignore-errors-my (elder-editing-modes))
     nil))


  ;; NB: that these are just autoload-definitions..  so their only use
  ;; is really for fsbot. 
  (idledo-require 'calc)
  (idledo-require 'calc-ext)
  
  ;;(idledo-require 'elder-set-keys)

  ;; at the very end.. we want this!
  (idledo-add-action
   '(progn
      (icomplete-mode 1)
      nil))

  (idledo-require-now 'fetch)

  (idledo-require 'emacs-wiki)


  (idledo-require 'boxquote)
  (idledo-require 'assoc)
  (idledo-require 'spam-stat)
  ;; for fsbot
  (idledo-require 'cc-mode)
  (idledo-require 'custom)

  (idledo-require 'repeat)
  (idledo-require 'thinks)
  (idledo-add-action '(mwheel-install))
  (idledo-add-action
   '(progn
     (setq vel-verbosity 0)
     (setq vel-echo-status-p t)
     (require 'vel)
     (setq-default vel-mode t)))

  (idledo-add-action
   '(progn
      (auto-compression-mode 1)
      nil))
  (idledo-require 'windmove)
  (idledo-add-action
   '(windmove-default-keybindings))



  (idledo-require 'parse-time)
  ;;(idledo-add-action
  ;;'(progn
  ;;  (require 'color-theme)
  ;;(color-theme-parus)
  ;;(color-theme-fischmeister)
  ;;(color-theme-gray1)
  ;;(utils-color-theme-nice-random-contextual)
  ;;))

  
;;   (idledo-add-periodic-action-crude
;;    '(progn
;;       (setq idledo-verbosity -100)
;;       (utils-color-theme-random-contextual) nil))
  
  ;;   (idledo-add-action 
  ;;    '(utils-color-theme-nice-random-contextual))

  (idledo-add-periodic-action-crude
   '(progn
      (require 'diary-lib)
      (require 'appt)
      (diary)
      (message (format "%S" appt-time-msg-list))
      (appt-check)
      
      ))


  ;; initialize woman..
  (idledo-add-action-at-end
   '(when (sit-for 300)
      (require 'woman)
      (woman-file-name "")))
  
  


;;   (idledo-add-action
;;    '(progn
;;       (require 'remem)
;;       (remem-toggle)))



  (idledo-add-action
   '(progn
      (tabbar-mode 1)))


  (idledo-add-action
   '(when window-system
      (require 'highlight-tail)
      (call-interactively 'highlight-tail-mode 1)))


  (idledo-add-action '(dabbrev-hover-install t t))


  ;; top priority
  (idledo-add-action
   '(progn
      (require 'fetch)
      (miniedit-install)
      (fetch-install-for-eshell)
      (mkback-install-for-eshell)
      nil))




  (idledo-add-action-at-end
   '(when (sit-for 4200) 
      (when (or (not (fboundp 'gnus-alive-p))
		(not (gnus-alive-p)))
	(spam-stat-doit-my))))

  )



 
;;;###autoload
(defun idledo-nullify ()
  (interactive)
  (setq idledo-list nil)
  (message "Idledo-list set to nil"))


(provide 'idledo)
(run-hooks 'idledo-after-load-hooks)
;;; idledo.el ends here