/
web-vcs.el
2069 lines (1934 loc) · 89.6 KB
/
web-vcs.el
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
;;; web-vcs.el --- Download file trees from VCS web pages
;;
;; Author: Lennart Borgman (lennart O borgman A gmail O com)
;; Created: 2009-11-26 Thu
(defconst web-vcs:version "0.61") ;; Version:
;; Last-Updated: 2009-12-11 Fri
;; URL:
;; Keywords:
;; Compatibility:
;;
;; Features that might be required by this library:
;;
;; None
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Commentary:
;;
;; Update file trees within Emacs from VCS systems using information
;; on their web pages.
;;
;; Available download commands are currently:
;;
;; `web-vcs-nxhtml'
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Change log:
;;
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; This program 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 program 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 this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;;; Code:
(eval-when-compile (require 'cl))
(eval-and-compile (require 'cus-edit))
(eval-and-compile (require 'mm-decode))
(eval-when-compile (require 'url-http))
(require 'advice)
(require 'web-autoload nil t)
;; (require 'url-util)
;; (require 'url)
;;(require 'url-parse)
(defvar web-vcs-comp-dir nil)
(defgroup web-vcs nil
"Customization group for web-vcs."
:group 'nxhtml)
(defcustom web-vcs-links-regexp
`(
(lp ;; Id
;; Comment:
"http://www.launchpad.com/ uses this 2009-11-29 with Loggerhead 1.10 (generic?)"
;; Files URL regexp:
;;
;; Extend this format to catch date/time too.
;;
;; ((patt (rx ...))
;; ;; use subexp numbers
;; (url 1)
;; (time 2)
;; (rev 3))
((time 1)
(url 2)
(patt ,(rx "<td class=\"date\">"
(submatch (regexp "[^<]*"))
"</td>"
(0+ space)
"<td class=\"timedate2\">"
(regexp ".+")
"</td>"
(*? (regexp ".\\|\n"))
"href=\""
(submatch (regexp ".*/download/[^\"]*"))
"\"")))
;; ,(rx "href=\""
;; (submatch (regexp ".*/download/[^\"]*"))
;; "\"")
;; Dirs URL regexp:
,(rx "href=\""
(submatch (regexp ".*%3A/[^\"]*/"))
"\"")
;; File name URL part regexp:
"\\([^\/]*\\)$"
;; Page revision regexp:
,(rx "for revision"
(+ whitespace)
"<span>"
(submatch (+ digit))
"</span>")
;; Release revision regexp:
,(rx "/"
(submatch (+ digit))
"\"" (+ (not (any ">"))) ">"
(optional "Release ")
(+ digit) "." (+ digit) "<")
)
)
"Regexp patterns for matching links on a VCS web page.
The patterns are grouped by VCS web system type.
*Note: It is always sub match 1 from these patterns that are
used."
:type '(repeat
(list
(symbol :tag "VCS web system type specifier")
(string :tag "Description")
(regexp :tag "Files URL regexp")
(regexp :tag "Dirs URL regexp")
(regexp :tag "File name URL part regexp")
(regexp :tag "Page revision regexp")
(regexp :tag "Release revision regexp")
))
:group 'web-vcs)
(defface web-vcs-mode-line
'((t (:foreground "black" :background "OrangeRed")))
"Mode line face during download."
:group 'web-vcs)
(defface web-vcs-mode-line-inactive
'((t (:foreground "black" :background "Orange")))
"Mode line face during download."
:group 'web-vcs)
(defface web-vcs-gold
'((t (:foreground "black" :background "gold")))
"Face for web-vcs messages."
:group 'web-vcs)
(defface web-vcs-red
'((t (:foreground "black" :background "#f86")))
"Face for web-vcs messages."
:group 'web-vcs)
(defface web-vcs-green
'((t (:foreground "black" :background "#8f6")))
"Face for web-vcs messages."
:group 'web-vcs)
(defface web-vcs-yellow
'((t (:foreground "black" :background "yellow")))
"Face for web-vcs messages."
:group 'web-vcs)
(defface web-vcs-pink
'((t (:foreground "black" :background "pink")))
"Face for web-vcs messages."
:group 'web-vcs)
(defcustom web-vcs-default-download-directory
'~/.emacs.d/
"Default download directory."
:type '(choice (const :tag "~/.emacs.d/" '~/.emacs.d/)
(const :tag "Fist site-lisp in `load-path'" 'site-lisp-dir)
(const :tag "Directory where `site-run-file' lives" 'site-run-dir)
(string :tag "Specify directory"))
:group 'web-vcs)
;;(web-vcs-default-download-directory)
;;;###autoload
(defun web-vcs-default-download-directory ()
"Try to find a suitable place.
Considers site-start.el, site-
"
(let ((site-run-dir (when site-run-file
(file-name-directory (locate-library site-run-file))))
(site-lisp-dir (catch 'first-site-lisp
(dolist (d load-path)
(let ((dir (file-name-nondirectory (directory-file-name d))))
(when (string= dir "site-lisp")
(throw 'first-site-lisp (file-name-as-directory d)))))))
)
(message "site-run-dir=%S site-lisp-dir=%S" site-run-dir site-lisp-dir)
(case web-vcs-default-download-directory
('~/.emacs.d/ "~/.emacs.d/")
('site-lisp-dir site-lisp-dir)
('site-run-dir site-run-dir)
(t web-vcs-default-download-directory))
))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Logging
(defcustom web-vcs-log-file "~/.emacs.d/web-vcs-log.org"
"Log file for web-vcs."
:type 'file
:group 'web-vcs)
;;;###autoload
(defun web-vcs-log-edit ()
"Open log file."
(interactive)
(find-file web-vcs-log-file))
(defvar web-vcs-log-save-timer nil)
(defun web-vcs-log-save-when-idle ()
(when (timerp web-vcs-log-save-timer) (cancel-timer web-vcs-log-save-timer))
(run-with-idle-timer 0 nil 'web-vcs-log-save))
(defun web-vcs-log-save ()
(let ((log-buf (find-buffer-visiting web-vcs-log-file)))
(when (and log-buf (buffer-modified-p log-buf))
(with-current-buffer log-buf
(basic-save-buffer)))
log-buf))
(defun web-vcs-log-close ()
(let ((log-buf (web-vcs-log-save)))
(when log-buf
(kill-buffer log-buf))))
;; Fix-me: Add some package descriptor to log
(defun web-vcs-log (url dl-file msg)
(unless (file-exists-p web-vcs-log-file)
(let ((dir (file-name-directory web-vcs-log-file)))
(unless (file-directory-p dir)
(make-directory dir))))
(with-current-buffer (find-file-noselect web-vcs-log-file)
(setq buffer-save-without-query t)
(web-vcs-log-save-when-idle)
(save-restriction
(widen)
(let ((today-entries (format-time-string "* %Y-%m-%d"))
(now (format-time-string "%H:%M:%S GMT" nil t)))
(goto-char (point-max))
(unless (re-search-backward (concat "^" today-entries) nil t)
(goto-char (point-max))
(insert "\n" today-entries "\n"))
(goto-char (point-max))
(when url
(insert "** Downloading file " now "\n"
(format " file [[file:%s][%s]]\n from %s\n" dl-file dl-file url)
))
(cond
((stringp msg)
(goto-char (point-max))
(insert msg "\n"))
(msg (basic-save-buffer)))))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Finding and downloading files
;;;###autoload
(defun web-vcs-get-files-from-root (web-vcs url dl-dir)
"Download a file tree from VCS system using the web interface.
Use WEB-VCS entry in variable `web-vcs-links-regexp' to download
files via http from URL to directory DL-DIR.
Show URL first and offer to visit the page. That page will give
you information about version control system \(VCS) system used
etc."
(unless (web-vcs-contains-moved-files dl-dir)
(when (if (not (y-or-n-p (concat "Download files from \"" url "\".\n"
"You can see on that page which files will be downloaded.\n\n"
"Visit that page before downloading? ")))
t
(browse-url url)
(if (y-or-n-p "Start downloading? ")
t
(message "Aborted")
nil))
(message "")
(web-vcs-get-files-on-page web-vcs url t (file-name-as-directory dl-dir) nil)
t)))
(defun web-vcs-get-files-on-page (web-vcs url recursive dl-dir test)
"Download files listed by WEB-VCS on web page URL.
WEB-VCS is a specifier in `web-vcs-links-regexp'.
If RECURSIVE go into sub folders on the web page and download
files from them too.
Place the files under DL-DIR.
Before downloading check if the downloaded revision already is
the same as the one on the web page. This is stored in the file
web-vcs-revision.txt. After downloading update this file.
If TEST is non-nil then do not download, just list the files."
(unless (string= dl-dir (file-name-as-directory (expand-file-name dl-dir)))
(error "Download dir dl-dir=%S must be a full directory path" dl-dir))
(catch 'command-level
(when (web-vcs-contains-moved-files dl-dir)
(throw 'command-level nil))
(let ((vcs-rec (or (assq web-vcs web-vcs-links-regexp)
(error "Does not know web-cvs %S" web-vcs)))
(start-time (current-time)))
(unless (file-directory-p dl-dir)
(if (yes-or-no-p (format "Directory %S does not exist, create it? "
(file-name-as-directory
(expand-file-name dl-dir))))
(make-directory dl-dir t)
(message "Can't download then")
(throw 'command-level nil)))
;; (let ((old-win (selected-window)))
;; (unless (eq (get-buffer "*Messages*") (window-buffer old-win))
;; (switch-to-buffer-other-window "*Messages*"))
;; (goto-char (point-max))
;; (insert "\n")
;; (insert (propertize (format "\n\nWeb-Vcs Download: %S\n" url) 'face 'web-vcs-gold))
;; (insert "\n")
;; (redisplay t)
;; (set-window-point (selected-window) (point-max))
;; (select-window old-win))
(web-vcs-message-with-face 'web-vcs-gold "\n\nWeb-Vcs Download: %S\n" url)
(web-vcs-display-messages nil)
(let* ((rev-file (expand-file-name "web-vcs-revision.txt" dl-dir))
(rev-buf (find-file-noselect rev-file))
;; Fix-me: Per web vcs speficier.
(old-rev-range (with-current-buffer rev-buf
(widen)
(goto-char (point-min))
(when (re-search-forward (format "%s:\\(.*\\)\n" web-vcs) nil t)
;;(buffer-substring-no-properties (point-min) (line-end-position))
;;(match-string 1)
(cons (match-beginning 1) (match-end 1))
)))
(old-revision (when old-rev-range
(with-current-buffer rev-buf
(buffer-substring-no-properties (car old-rev-range)
(cdr old-rev-range)))))
(dl-revision (web-vcs-get-revision-on-page vcs-rec url))
ret
moved)
(when (and old-revision (string= old-revision dl-revision))
(when (y-or-n-p (format "You already have revision %s. Quit? " dl-revision))
(message "Aborted")
(kill-buffer rev-buf)
(throw 'command-level nil)))
;; We do not have a revision number once we start download.
(with-current-buffer rev-buf
(when old-rev-range
(delete-region (car old-rev-range) (cdr old-rev-range))
(basic-save-buffer)))
(setq ret (web-vcs-get-files-on-page-1
vcs-rec url
dl-dir
""
nil
(if recursive 0 nil)
dl-revision test))
(setq moved (nth 1 ret))
;; Now we have a revision number again.
(with-current-buffer rev-buf
(when (= 0 (buffer-size))
(insert "WEB VCS Revisions\n\n"))
(goto-char (point-max))
(unless (eolp) (insert "\n"))
(insert (format "%s:%s\n" web-vcs dl-revision))
(basic-save-buffer)
(kill-buffer))
(message "-----------------")
(web-vcs-message-with-face 'web-vcs-gold "Web-Vcs Download Ready: %S" url)
(web-vcs-message-with-face 'web-vcs-gold " Time elapsed: %S"
(web-vcs-nice-elapsed start-time (current-time)))
(when (> moved 0)
(web-vcs-message-with-face 'web-vcs-yellow
" %i files updated (old versions renamed to *.moved)"
moved))))))
(defun web-vcs-get-files-on-page-1 (vcs-rec url dl-root dl-relative file-mask recursive dl-revision test)
"Download files listed by VCS-REC on web page URL.
VCS-REC should be an entry like the entries in the list
`web-vcs-links-regexp'.
If FILE-MASK is non nil then it is used to match a file path.
Only matching files will be downloaded. FILE-MASK can have two
forms, a regular expression or a function.
If FILE-MASK is a regular expression then each part of the path
may be a regular expresion \(not containing /).
If FILE-MASK is a function then this function is called in each
directory under DL-ROOT. The function is called with the
directory as a parameter and should return a cons. The first
element of the cons should be a regular expression matching file
names in that directory that should be downloaded. The cdr
should be t if subdirectories should be visited.
If RECURSIVE go into sub folders on the web page and download
files from them too.
Place the files under DL-DIR.
The revision on the page URL should match DL-REVISION if this is non-nil.
If TEST is non-nil then do not download, just list the files"
;;(web-vcs-message-with-face 'font-lock-comment-face "web-vcs-get-files-on-page-1 %S %S %S %S" url dl-root dl-relative file-mask)
(let* ((files-matcher (nth 2 vcs-rec))
(dirs-href-regexp (nth 3 vcs-rec))
(revision-regexp (nth 5 vcs-rec))
(dl-dir (file-name-as-directory (expand-file-name dl-relative dl-root)))
(lst-dl-relative (web-vcs-file-name-as-list dl-relative))
(lst-file-mask (when (stringp file-mask) (web-vcs-file-name-as-list file-mask)))
;;(url-buf (url-retrieve-synchronously url))
this-page-revision
files
suburls
(moved 0)
(temp-file-base (expand-file-name "web-vcs-temp-list.tmp" dl-dir))
temp-list-file
temp-list-buf
folder-res
http-sts)
;; Fix-me: It looks like there is maybe a bug in url-copy-file so
;; that it runs synchronously. Try to workaround the problem by
;; making a new file temp file name.
(web-vcs-display-messages nil)
(unless (file-directory-p dl-dir) (make-directory dl-dir t))
;;(message "TRACE: dl-dir=%S" dl-dir)
(setq temp-list-file (make-temp-name temp-file-base))
(setq temp-list-buf (web-vcs-ass-folder-cache url))
(unless temp-list-buf
;;(setq temp-list-buf (generate-new-buffer "web-wcs-folder"))
;;(web-vcs-url-copy-file-and-check url temp-list-file nil)
(setq folder-res (web-vcs-url-retrieve-synch url))
;; (with-current-buffer temp-list-buf
;; (insert-file-contents temp-list-file))
(unless (memq (cdr folder-res) '(200 201))
(web-vcs-message-with-face 'web-vcs-red "Could not get %S" url)
(web-vcs-display-messages t)
(throw 'command-level nil)))
;;(with-current-buffer temp-list-buf
(with-current-buffer (car folder-res)
;;(delete-file temp-list-file)
;;(find-file-noselect temp-list-file)
(when dl-revision
(setq this-page-revision (web-vcs-get-revision-from-url-buf vcs-rec (current-buffer) url)))
(when dl-revision
(unless (string= dl-revision this-page-revision)
(web-vcs-message-with-face 'web-vcs-red "Revision on %S is %S, but should be %S"
url this-page-revision dl-revision)
(web-vcs-display-messages t)
(throw 'command-level nil)))
;; Find files
(goto-char (point-min))
(let ((files-href-regexp (nth 1 (assq 'patt files-matcher)))
(url-num (nth 1 (assq 'url files-matcher)))
(time-num (nth 1 (assq 'time files-matcher))))
(while (re-search-forward files-href-regexp nil t)
(let ((file (match-string url-num))
(time (match-string time-num)))
(add-to-list 'files (list file time)))))
;; Find subdirs
(when recursive
(goto-char (point-min))
(while (re-search-forward dirs-href-regexp nil t)
(let ((suburl (match-string 1))
(lenurl (length url)))
(when (and (> (length suburl) lenurl)
(string= (substring suburl 0 lenurl) url))
(add-to-list 'suburls suburl)))))
(kill-buffer))
;; Download files
;;(message "TRACE: files=%S" files)
(web-vcs-download-files vcs-rec files dl-dir dl-root file-mask)
;; Download subdirs
(when suburls
(dolist (suburl (reverse suburls))
(let* ((dl-sub-dir (substring suburl (length url)))
(full-dl-sub-dir (file-name-as-directory
(expand-file-name dl-sub-dir dl-dir)))
(rel-dl-sub-dir (file-relative-name full-dl-sub-dir dl-root)))
;;(message "web-vcs-get-revision-from-url-buf dir: %S %S" file-mask rel-dl-sub-dir)
(when (or (not file-mask)
(not (stringp file-mask))
(web-vcs-match-folderwise file-mask rel-dl-sub-dir))
;;(message "matched dir %S" rel-dl-sub-dir)
(unless (web-vcs-contains-file dl-dir full-dl-sub-dir)
(error "Subdir %S not in %S" dl-sub-dir dl-dir))
(let* ((ret (web-vcs-get-files-on-page-1 vcs-rec
suburl
dl-root
rel-dl-sub-dir
file-mask
(1+ recursive)
this-page-revision
test)))
(setq moved (+ moved (nth 1 ret))))))))
(list this-page-revision moved)))
(defun web-vcs-get-missing-matching-files (web-vcs url dl-dir file-mask)
"Download missing files from VCS system using the web interface.
Use WEB-VCS entry in variable `web-vcs-links-regexp' to download
files via http from URL to directory DL-DIR.
Before downloading offer to visit the page from which the
downloading will be made.
"
(let ((vcs-rec (or (assq web-vcs web-vcs-links-regexp)
(error "Does not know web-cvs %S" web-vcs))))
(web-vcs-get-files-on-page-1 vcs-rec url dl-dir "" file-mask 0 nil nil)))
;; (web-vcs-get-files-on-page 'lp "http://bazaar.launchpad.net/%7Enxhtml/nxhtml/main/files/head%3A/" t "c:/test/temp13/" t)
(defvar web-vcs-folder-cache nil) ;; dyn var
(defun web-vcs-add-folder-cache (url buf)
(add-to-list 'web-vcs-folder-cache (list url buf)))
(defun web-vcs-ass-folder-cache (url)
(assoc url web-vcs-folder-cache))
(defun web-vcs-clear-folder-cache ()
(while web-vcs-folder-cache
(let ((ub (car web-vcs-folder-cache)))
(setq web-vcs-folder-cache (cdr web-vcs-folder-cache))
(kill-buffer (nth 1 ub)))))
(defun web-vcs-url-copy-file-and-check (url dl-file dest-file)
"Copy URL to DL-FILE.
Log what happened. Use DEST-FILE in the log, not DL-FILE which is
a temporary file."
(let ((http-sts nil)
(file-nonempty nil)
(fail-reason nil))
(when dest-file (web-vcs-log url dest-file nil))
(web-vcs-display-messages nil)
;;(message "before url-copy-file %S" dl-file)
(setq http-sts (web-vcs-url-copy-file url dl-file nil t)) ;; don't overwrite, keep time
;;(message "after url-copy-file %S" dl-file)
(if (and (file-exists-p dl-file)
(setq file-nonempty (< 0 (nth 7 (file-attributes dl-file)))) ;; file size 0
(memq http-sts '(200 201)))
(when dest-file
(web-vcs-log nil nil " Done.\n"))
(setq fail-reason
(cond
(http-sts (format "HTTP %s" http-sts))
(file-nonempty "File looks bad")
(t "Server did not respond")))
(unless dest-file (web-vcs-log url dl-file "TEMP FILE"))
(web-vcs-log nil nil (format " *Failed:* %s\n" fail-reason))
;; Requires user attention and intervention
(web-vcs-message-with-face 'web-vcs-red "Download failed: %s, %S" fail-reason url)
(web-vcs-display-messages t)
(message "\n")
(web-vcs-message-with-face 'web-vcs-yellow "Please retry what you did before!\n")
(throw 'command-level nil))))
(defvar web-autoload-temp-file-prefix "TEMPORARY-WEB-AUTO-LOAD-")
(defvar web-autoload-active-file-sub-url) ;; Dyn var, active during file download check
(defun web-autoload-acvtive ()
(and (boundp 'web-autoload-active-file-sub-url)
web-autoload-active-file-sub-url))
(defun web-vcs-download-files (vcs-rec files dl-dir dl-root file-mask)
(dolist (file (reverse files))
(let* ((url-file (nth 0 file))
(url-file-time-str (nth 1 file))
;; date-to-time assumes GMT so this is ok:
(url-file-time (when url-file-time-str (date-to-time url-file-time-str)))
(url-file-name-regexp (nth 4 vcs-rec))
(url-file-rel-name (progn
(when (string-match url-file-name-regexp url-file)
(match-string 1 url-file))))
(dl-file-name (expand-file-name url-file-rel-name dl-dir))
(dl-file-time (nth 5 (file-attributes dl-file-name)))
(file-rel-name (file-relative-name dl-file-name dl-root))
(file-name (file-name-nondirectory dl-file-name))
(temp-file (expand-file-name (concat web-autoload-temp-file-prefix file-name) dl-dir))
temp-buf)
(cond
((and file-mask (not (web-vcs-match-folderwise file-mask file-rel-name))))
((and dl-file-time
url-file-time
(progn
;;(message "dl-file-time =%s" (when dl-file-time (current-time-string dl-file-time)))
;;(message "url-file-time=%s" (when url-file-time (current-time-string url-file-time)))
;;(message "url-file-tstr=%s" (when url-file-time url-file-time-str))
t)
(time-less-p url-file-time
(time-add dl-file-time (seconds-to-time 1))))
(web-vcs-message-with-face 'web-vcs-green "Local file %s is newer or same age" file-rel-name))
;;(test (progn (message "TEST url-file=%S" url-file) (message "TEST url-file-rel-name=%S" url-file-rel-name) (message "TEST dl-file-name=%S" dl-file-name) ))
(t
;; Avoid trouble with temp file
(while (setq temp-buf (find-buffer-visiting temp-file))
(set-buffer-modified-p nil) (kill-buffer temp-buf))
(when (file-exists-p temp-file) (delete-file temp-file))
;;(web-vcs-message-with-face 'font-lock-comment-face "Starting url-copy-file %S %S t t" url-file temp-file)
(web-vcs-url-copy-file-and-check url-file temp-file dl-file-name)
;;(web-vcs-message-with-face 'font-lock-comment-face "Finished url-copy-file %S %S t t" url-file temp-file)
(let* ((time-after-url-copy (current-time))
(old-buf-open (find-buffer-visiting dl-file-name)))
(when (and old-buf-open (buffer-modified-p old-buf-open))
(save-excursion
(switch-to-buffer old-buf-open)
(when (y-or-n-p (format "Buffer %S is modified, save to make a backup? " dl-file-name))
(save-buffer))))
(if (and dl-file-time (web-vcs-equal-files dl-file-name temp-file))
(progn
(delete-file temp-file)
(when url-file-time (set-file-times dl-file-name url-file-time))
(web-vcs-message-with-face 'web-vcs-green "File %S was ok" dl-file-name))
(when dl-file-time
(let ((backup (concat dl-file-name ".moved")))
(rename-file dl-file-name backup t)))
;; Be paranoid and let user check here. I actually
;; believe that is a very good thing here.
(web-vcs-be-paranoid temp-file dl-file-name file-rel-name)
(rename-file temp-file dl-file-name)
(when url-file-time (set-file-times dl-file-name url-file-time))
;; (let ((buf (find-buffer-visiting dl-file-name)))
;; (when buf
;; (with-current-buffer buf
;; (message "before revert-buffer")
;; (revert-buffer nil t t)
;; (message "after revert-buffer")
;; )))
(if dl-file-time
(web-vcs-message-with-face 'web-vcs-yellow "Updated %S" dl-file-name)
(web-vcs-message-with-face 'web-vcs-green "Downloaded %S" dl-file-name))
(when old-buf-open
(with-current-buffer old-buf-open
(set-buffer-modified-p nil)
(revert-buffer nil t t)))
(with-current-buffer (find-file-noselect dl-file-name)
(setq header-line-format
(propertize (format-time-string "This file was downloaded %Y-%m-%d %H:%M")
'face 'web-vcs-green))))
(web-vcs-display-messages nil)
;; This is both for user and remote server load. Do not remove this.
(redisplay t) (sit-for (- 1.0 (float-time (time-subtract (current-time) time-after-url-copy))))
;; (unless old-buf-open
;; (when old-buf
;; (kill-buffer old-buf)))
)))
(redisplay t))))
(defun web-vcs-get-revision-on-page (vcs-rec url)
"Get revision number using VCS-REC on page URL.
VCS-REC should be an entry like the entries in the list
`web-vcs-links-regexp'."
;; url-insert-file-contents
(let ((url-buf (url-retrieve-synchronously url)))
(web-vcs-get-revision-from-url-buf vcs-rec url-buf url)))
(defun web-vcs-get-revision-from-url-buf (vcs-rec url-buf url)
"Get revision number using VCS-REC.
VCS-REC should be an entry in the list `web-vcs-links-regexp'.
The buffer URL-BUF should contain the content on page URL."
(let ((revision-regexp (nth 5 vcs-rec)))
;; Get revision number
(with-current-buffer url-buf
(goto-char (point-min))
(if (not (re-search-forward revision-regexp nil t))
(progn
(web-vcs-message-with-face 'web-vcs-red "Can't find revision number on %S" url)
(web-vcs-display-messages t)
(throw 'command-level nil))
(match-string 1)))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Auto Download
;; fix-me: To emulation-mode-map
;; Fix-me: put this on better keys
(defvar web-vcs-paranoid-state-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [(control ?c)(control ?c)] 'exit-recursive-edit)
(define-key map [(control ?c)(control ?n)] 'web-autoload-continue-no-stop)
(define-key map [(control ?c)(control ?r)] 'web-vcs-investigate-elisp-file)
(define-key map [(control ?c)(control ?q)] 'web-vcs-quit-auto-download)
map))
(defun web-vcs-quit-auto-download ()
"Quit download process.
This stops the current web autoload processing."
(interactive)
;; Fix-me.
(when (y-or-n-p "Stop web autoload processing? You can resume it later. ")
(web-vcs-message-with-face 'web-vcs-red
"Stopped autoloading in process. It will be resumed when necessary again.")
(web-vcs-log nil nil "User stopped autoloading")
(throw 'top-level 'web-autoload-stop)))
(define-minor-mode web-vcs-paranoid-state-mode
"Mode used temporarily during user check of downloaded file.
Do not turn on this yourself."
:lighter (concat " " (propertize "Download file check" 'face 'font-lock-warning-face))
:global t
:group 'web-vcs
(or (not web-vcs-paranoid-state-mode)
(web-autoload-acvtive)
(error "This mode can't be used when not downloading")))
(defcustom web-autoload-paranoid t
"Be paranoid and break to check each file after download."
:type 'boolean
:group 'web-vcs)
(defun web-autoload-continue-no-stop ()
"Continue web auto download.
This is used after inspecting downloaded elisp files. Set
`web-autoload-paranoid' to nil before contiuning to avoid further
breaks to check downloaded files."
(interactive)
(setq web-autoload-paranoid nil)
(web-autoload-continue))
(defun web-autoload-continue ()
"Continue web auto download.
This is used after inspecting downloaded elisp files."
(interactive)
(if (< 0 (recursion-depth))
(exit-recursive-edit)
(web-autoload-byte-compile-queue)))
(defun web-vcs-be-paranoid (temp-file file-dl-name file-sub-url)
"Be paranoid and check FILE-DL-NAME."
(when (or (not (boundp 'web-autoload-paranoid))
web-autoload-paranoid)
(save-window-excursion
(let* ((comp-buf (get-buffer "*Compilation*"))
(comp-win (and comp-buf
(get-buffer-window comp-buf)))
(msg-win (web-vcs-display-messages nil))
temp-buf
(kf-desc (lambda (fun)
(let* ((key (where-is-internal fun nil t))
(k-desc (when key (key-description key)))
(fmt-kf "\n %s (or %s)")
(fmt-f "\n %s"))
(if key
(format fmt-kf k-desc fun)
(format fmt-f fun)
)))))
(if comp-win
(progn
(select-window comp-win)
(find-file file-dl-name))
(select-window msg-win)
(find-file-other-window temp-file))
(setq temp-buf (current-buffer))
(web-vcs-log-save)
(message "-")
(message "")
(with-selected-window msg-win
(goto-char (point-max)))
(let ((proceed nil)
(web-autoload-active-file-sub-url file-sub-url)) ;; Dyn var, active during file download check
(web-vcs-paranoid-state-mode 1)
(web-vcs-message-with-face
'secondary-selection
(concat "Please check the downloaded file and then continue by doing"
(funcall kf-desc 'exit-recursive-edit)
(if (fboundp 'web-autoload-continue-no-stop)
(concat
"\n\nOr, for no more breaks to check files do"
(funcall kf-desc 'web-autoload-continue-no-stop))
"")
"\n\nTo stop the web autoloading process for now do"
(funcall kf-desc 'web-autoload-quit-download)
"\n\nTo see the log file you can do"
(funcall kf-desc 'web-vcs-log-edit)
"\n"))
(message "")
(while (not proceed)
(condition-case err
(when (eq 'web-autoload-stop
(catch 'top-level
;; Fix-me: review file before rename!
(setq header-line-format
(propertize
(format "Review for downloading. Continue: C-c C-c%s. Destination: %S"
(if (string= "el" (file-name-extension file-dl-name))
", Check: C-c C-r"
"")
file-dl-name)
'face 'web-vcs-red))
(unwind-protect
(progn
(recursive-edit))
(web-vcs-paranoid-state-mode -1))
(with-current-buffer temp-buf
(set-buffer-modified-p nil)
(kill-buffer temp-buf))
(setq proceed t)))
(throw 'top-level t))
(error (message "%s" (error-message-string err))))))
(web-vcs-display-messages t)
))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;; Auto Download Compile Queue
;;
;; Downloaded elisp files are placed in a compile queue. They are not
;; compiled until all required elisp files are downloaded (and
;; optionally compiled).
;;
;; This mechanism works through
;; - reading (eval-when-compile ...) etc in the files
;; - a defadviced require that is the driver of the process
(defvar web-autoload-compile-queue nil)
(defvar web-autoload-byte-compile-queue-active nil) ;; Dyn var
(defun web-autoload-byte-compile-file (file load comp-fun)
(if nil ;;(file-exists-p file)
(byte-compile-file file load)
(let ((added-entry (list file load comp-fun nil)))
(if (member added-entry web-autoload-compile-queue)
(setq added-entry nil)
(web-vcs-message-with-face 'web-vcs-gold "Add to compile queue (%S %s)" file load)
(setq web-autoload-compile-queue (cons added-entry
web-autoload-compile-queue)))
(when added-entry
(if web-autoload-byte-compile-queue-active
(throw 'web-autoload-comp-restart t)
(web-autoload-byte-compile-queue))))))
;;(web-autoload-byte-compile-queue)
(defun web-autoload-byte-compile-queue ()
(let ((top-entry)
(web-autoload-byte-compile-queue-active t))
(while (and web-autoload-compile-queue
(not (equal top-entry
(car web-autoload-compile-queue))))
(setq top-entry (car web-autoload-compile-queue))
(catch 'web-autoload-comp-restart
(web-autoload-byte-compile-first)
(setq web-autoload-compile-queue (cdr web-autoload-compile-queue))))))
(defun web-autoload-byte-compile-first ()
"Compile first file on compile queue and maybe load it.
Compile the car of `web-autoload-compile-queue' and load if this
entry says so."
(let* ((compiled-it nil)
(first-entry (car web-autoload-compile-queue))
(el-file (nth 0 first-entry))
(load (nth 1 first-entry))
(comp-fun (nth 2 first-entry))
(req-done (nth 3 first-entry))
(elc-file (byte-compile-dest-file el-file))
(need-compile (or (not (file-exists-p elc-file))
(file-newer-than-file-p el-file elc-file))))
(if (not need-compile)
nil ;;(when load (load elc-file))
(unless req-done
(web-autoload-do-eval-requires el-file)
(setcar (nthcdr 3 first-entry) t))
(when (catch 'web-autoload-comp-restart
(condition-case err
(progn
(web-vcs-message-with-face 'font-lock-comment-face "Start byte compiling %S" el-file)
(web-vcs-message-with-face 'web-vcs-pink "Compiling QUEUE: %S" web-autoload-compile-queue)
(let ((web-autoload-skip-require-advice t)) (funcall comp-fun el-file load))
(web-vcs-message-with-face 'font-lock-comment-face "Ready byte compiling %S" el-file)
;; Return nil to tell there are no known problems
(if (file-exists-p elc-file)
nil
(web-vcs-message-with-face
'web-vcs-red "Error: byte compiling did not produce %S" elc-file)
(web-vcs-display-messages nil)
;; Clean up before restart
(web-autoload-try-cleanup-after-failed-compile first-entry)
t))
(error
(web-vcs-message-with-face
'web-vcs-red "Error in byte compiling %S: %s" el-file (error-message-string err))
(web-vcs-display-messages nil)
;; Clean up before restart
(web-autoload-try-cleanup-after-failed-compile first-entry)
t ;; error
)))
(throw 'web-autoload-comp-restart t)
))))
(defun web-autoload-do-eval-requires (el-file)
"Do eval-when-compile and eval-and-compile."
;;(message "web-autoload-do-eval-requires %S" el-file)
(let ((old-buf (find-buffer-visiting el-file)))
(with-current-buffer (or old-buf (find-file-noselect el-file))
(let ((here (point))
(web-autoload-require-skip-noerror-entries t))
(save-restriction
(widen)
(goto-char (point-min))
;;(message "web-autoload-do-eval-requires cb=%s" (current-buffer))
(while (progn
(while (progn (skip-chars-forward " \t\n\^l")
(looking-at ";"))
(forward-line 1))
(not (eobp)))
(let ((form (read (current-buffer))))
(when (memq (car form) '(eval-when-compile eval-and-compile))
(web-vcs-message-with-face 'web-vcs-gold " eval %S" form)
(eval form))
)))
(if old-buf (kill-buffer) (goto-char here))))))
;; Fix-me: protect against deep nesting
(defun web-autoload-do-require (feature filename noerror)
(let* ((feat-name (symbol-name feature))
(lib (or filename feat-name)))
(if (load lib noerror t)
(progn
(unless (featurep feature)
(error "web-autoload: Required feature `%s' was not provided" feature))
feature)
nil
)))
(defvar web-autoload-require-skip-noerror-entries nil)
(defadvice require (around
web-autoload-ad-require)
(let ((feature (ad-get-arg 0))
(filename (ad-get-arg 1))
(noerror (ad-get-arg 2)))
(if (featurep feature)
feature
(if (or filename
(and noerror
(or (not (boundp 'web-autoload-skip-require-advice))
web-autoload-skip-require-advice)))
(progn
(message "Doing nearly original require %s, because skipping" (ad-get-arg 0))
;; Can't ad-do-it because defadviced functions in load
;;(web-autoload-do-require feature filename noerror)
;;
;; Fix-me: Implement lazy loading here? Could it be done with while-no-input?
;;
;;(when (assq feature web-autoload-require-list) )
ad-do-it)
(unless (and noerror
web-autoload-require-skip-noerror-entries)
(let* ((auto-rec (assq feature web-autoload-require-list))
(web-vcs (nth 1 auto-rec))
(base-url (nth 2 auto-rec))
(relative-url (nth 3 auto-rec))
(base-dir (nth 4 auto-rec))
(comp-fun (nth 5 auto-rec)))
(if (not auto-rec)
ad-do-it
(let* ((full-el (concat (expand-file-name relative-url base-dir) ".el"))
(full-elc (byte-compile-dest-file full-el))
(our-buffer (current-buffer)) ;; Need to come back here
(our-wcfg (current-window-configuration))
(mode-line-old (web-vcs-redefine-face 'mode-line 'web-vcs-mode-line))
(mode-line-inactive-old (web-vcs-redefine-face 'mode-line-inactive 'web-vcs-mode-line-inactive))
(header-line-format-old (with-current-buffer "*Messages*"
(prog1
header-line-format
(setq header-line-format
(propertize "Downloading needed files..."
'face 'web-vcs-mode-line
;;'face '(:height 1.5) ;; does not work
))))))
;; Fix-me: can't update while accessing the menus
;;(message "trying (redisplay t) ;; mode line")
;;(sit-for 1) (redisplay t) ;; mode line
(unwind-protect
(progn
(web-vcs-message-with-face 'web-vcs-gold "Doing the really adviced require for %s" feature)
;; Check if already downloaded first
(unless (file-exists-p full-el)
(setq base-url (eval base-url))
;; Download and try again
(setq relative-url (concat relative-url ".el"))
(web-vcs-message-with-face 'web-vcs-green "Need to download feature '%s" feature)
(catch 'web-autoload-comp-restart
(web-vcs-get-missing-matching-files web-vcs base-url base-dir relative-url)))
(set-buffer our-buffer) ;; Before we load..
(when web-autoload-autocompile