mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy patharc-mode.el
2490 lines (2294 loc) · 97.5 KB
/
arc-mode.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
;;; arc-mode.el --- simple editing of archives -*- lexical-binding: t; -*-
;; Copyright (C) 1995, 1997-1998, 2001-2025 Free Software Foundation,
;; Inc.
;; Author: Morten Welinder <terra@gnu.org>
;; Keywords: files archives ms-dos editing major-mode
;; Favorite-brand-of-beer: None, I hate beer.
;; This file is part of GNU Emacs.
;; GNU Emacs 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 of the License, or
;; (at your option) any later version.
;; GNU Emacs 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. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; NAMING: "arc" is short for "archive" and does not refer specifically
;; to files whose name end in ".arc"
;;
;; This code does not decode any files internally, although it does
;; understand the directory level of the archives. For this reason,
;; you should expect this code to need more fiddling than tar-mode.el
;; (although it at present has fewer bugs :-) In particular, I have
;; not tested this under MS-DOS myself.
;; -------------------------------------
;; INTERACTION: arc-mode.el should play together with
;;
;; * ange-ftp.el: Remote archives (i.e., ones that ange-ftp has brought
;; to you) are handled by doing all updates on a local
;; copy. When you make changes to a remote file the
;; changes will first take effect when the archive buffer
;; is saved. You will be warned about this.
;;
;; * dos-fns.el: You get automatic ^M^J <--> ^J conversion.
;;
;; arc-mode.el does not work well with crypt++.el; for the archives as
;; such this could be fixed (but wouldn't be useful) by declaring such
;; archives to be "remote". For the members this is a general Emacs
;; problem that 19.29's file formats may fix.
;; -------------------------------------
;; ARCHIVE TYPES: Currently only the archives below are handled, but the
;; structure for handling just about anything is in place.
;;
;; Arc Lzh Zip Zoo Rar 7z Ar Squashfs
;; ---------------------------------------------------------------
;; View listing Intern Intern Intern Intern Y Y Y Y
;; Extract member Y Y Y Y Y Y Y Y
;; Save changed member Y Y Y Y N Y Y N
;; Add new member N N N N N N N N
;; Delete member Y Y Y Y N Y N N
;; Rename member Y Y N N N N N N
;; Chmod - Y Y - N N N N
;; Chown - Y - - N N N N
;; Chgrp - Y - - N N N N
;;
;; Special thanks to Bill Brodie <wbrodie@panix.com> for very useful tips
;; on the first released version of this package.
;;
;; This code is partly based on tar-mode.el from Emacs.
;; -------------------------------------
;; ARCHIVE STRUCTURES:
;; (This is mostly for myself.)
;;
;; ARC A series of (header,file). No interactions among members.
;;
;; LZH A series of (header,file). Headers are checksummed. No
;; interaction among members.
;; Headers come in three flavors called level 0, 1 and 2 headers.
;; Level 2 header is free of DOS specific restrictions and most
;; commonly used. Also level 1 and 2 headers consist of base
;; and extension headers. For more details see
;; http://homepage1.nifty.com/dangan/en/Content/Program/Java/jLHA/Notes/Notes.html
;; http://www.osirusoft.com/joejared/lzhformat.html
;;
;; ZIP A series of (lheader,fil) followed by a "central directory"
;; which is a series of (cheader) followed by an end-of-
;; central-dir record possibly followed by junk. The e-o-c-d
;; links to c-d. cheaders link to lheaders which are basically
;; cut-down versions of the cheaders.
;;
;; ZOO An archive header followed by a series of (header,file).
;; Each member header points to the next. The archive is
;; terminated by a bogus header with a zero next link.
;; -------------------------------------
;; HOOKS: `foo' means one of the supported archive types.
;;
;; archive-mode-hook
;; archive-foo-mode-hook
;; archive-extract-hook
;;; Code:
(eval-when-compile (require 'cl-lib))
(eval-when-compile (require 'subr-x))
;; -------------------------------------------------------------------------
;;; Section: Configuration.
(defgroup archive nil
"Simple editing of archives."
:group 'data)
(defcustom archive-tmpdir
;; make-temp-name is safe here because we use this name
;; to create a directory.
(make-temp-name
(expand-file-name (if (eq system-type 'ms-dos) "ar" "archive.tmp")
temporary-file-directory))
"Directory for temporary files made by `arc-mode.el'."
:type 'directory)
(defcustom archive-remote-regexp "^/[^/:]*[^/:.]:"
"Regexp recognizing archive files names that are not local.
A non-local file is one whose file name is not proper outside Emacs.
A local copy of the archive will be used when updating."
:type 'regexp)
(defcustom archive-extract-hook nil
"Hook run when an archive member has been extracted."
:type 'hook)
(defcustom archive-visit-single-files nil
"If non-nil, opening an archive with a single file visits that file.
If nil, visiting such an archive displays the archive summary."
:version "25.1"
:type '(choice (const :tag "Visit the single file" t)
(const :tag "Show the archive summary" nil)))
(defcustom archive-hidden-columns '(Ids)
"Columns hidden from display."
:version "28.1"
:type '(set (const Mode)
(const Ids)
(const Date&Time)
(const Ratio)))
(defconst archive-alternate-hidden-columns '(Mode Date&Time)
"Columns hidden when `archive-alternate-display' is used.")
;; ------------------------------
;; Arc archive configuration
;; We always go via a local file since there seems to be no reliable way
;; to extract to stdout without junk getting added.
(defgroup archive-arc nil
"ARC-specific options to archive."
:group 'archive)
(defcustom archive-arc-extract
'("arc" "x")
"Program and its options to run in order to extract an arc file member.
Extraction should happen to the current directory. Archive and member
name will be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
(defcustom archive-arc-expunge
'("arc" "d")
"Program and its options to run in order to delete arc file members.
Archive and member names will be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
(defcustom archive-arc-write-file-member
'("arc" "u")
"Program and its options to run in order to update an arc file member.
Archive and member name will be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
;; ------------------------------
;; Lzh archive configuration
(defgroup archive-lzh nil
"LZH-specific options to archive."
:group 'archive)
(defcustom archive-lzh-extract
'("lha" "pq")
"Program and its options to run in order to extract an lzh file member.
Extraction should happen to standard output. Archive and member name will
be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
(defcustom archive-lzh-expunge
'("lha" "d")
"Program and its options to run in order to delete lzh file members.
Archive and member names will be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
(defcustom archive-lzh-write-file-member
'("lha" "a")
"Program and its options to run in order to update an lzh file member.
Archive and member name will be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
;; ------------------------------
;; Zip archive configuration
(defvar archive-7z-program (let ((7z (or (executable-find "7z")
(executable-find "7za"))))
(when 7z
(file-name-nondirectory 7z))))
(defgroup archive-zip nil
"ZIP-specific options to archive."
:group 'archive)
(defcustom archive-zip-extract
(cond ((executable-find "unzip")
(if (and (eq system-type 'android)
;; Mind that the unzip provided by Android
;; does not understand -qq or -c, their
;; functions being assumed by -q and -p
;; respectively. Furthermore, the user
;; might install an unzip executable
;; distinct from the system-provided unzip,
;; and such situations must be detected as
;; well.
(member (executable-find "unzip")
'("/bin/unzip"
"/system/bin/unzip")))
'("unzip" "-q" "-p")
'("unzip" "-qq" "-c")))
(archive-7z-program `(,archive-7z-program "x" "-so"))
((executable-find "pkunzip") '("pkunzip" "-e" "-o-"))
(t '("unzip" "-qq" "-c")))
"Program and its options to run in order to extract a zip file member.
Extraction should happen to standard output. Archive and member
name will be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
;; For several reasons the latter behavior is not desirable in general.
;; (1) It uses more disk space. (2) Error checking is worse or non-
;; existent. (3) It tends to do funny things with other systems' file
;; names.
(defcustom archive-zip-expunge
(cond ((executable-find "zip") '("zip" "-d" "-q"))
(archive-7z-program `(,archive-7z-program "d"))
((executable-find "pkzip") '("pkzip" "-d"))
(t '("zip" "-d" "-q")))
"Program and its options to run in order to delete zip file members.
Archive and member names will be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
(defcustom archive-zip-update
(cond ((executable-find "zip") '("zip" "-q"))
(archive-7z-program `(,archive-7z-program "u"))
((executable-find "pkzip") '("pkzip" "-u" "-P"))
(t '("zip" "-q")))
"Program and its options to run in order to update a zip file member.
Options should ensure that specified directory will be put into the zip
file. Archive and member name will be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
(defcustom archive-zip-update-case
(cond ((executable-find "zip") '("zip" "-q" "-k"))
(archive-7z-program `(,archive-7z-program "u"))
((executable-find "pkzip") '("pkzip" "-u" "-P"))
(t '("zip" "-q" "-k")))
"Program and its options to run in order to update a case fiddled zip member.
Options should ensure that specified directory will be put into the zip file.
Archive and member name will be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
(declare-function msdos-long-file-names "msdos.c")
(defcustom archive-zip-case-fiddle (and (eq system-type 'ms-dos)
(not (msdos-long-file-names)))
"If non-nil, then all-caps names of zip file members will be down-cased.
This case fiddling will only happen for members created by a system
that uses caseless file names.
In addition, this flag forces members added/updated in the zip archive
to be truncated to DOS 8+3 file-name restrictions."
:type 'boolean
:version "27.1")
;; ------------------------------
;; Zoo archive configuration
(defgroup archive-zoo nil
"ZOO-specific options to archive."
:group 'archive)
(defcustom archive-zoo-extract
'("zoo" "xpq")
"Program and its options to run in order to extract a zoo file member.
Extraction should happen to standard output. Archive and member name will
be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
(defcustom archive-zoo-expunge
'("zoo" "DqPP")
"Program and its options to run in order to delete zoo file members.
Archive and member names will be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
(defcustom archive-zoo-write-file-member
'("zoo" "a")
"Program and its options to run in order to update a zoo file member.
Archive and member name will be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
;; ------------------------------
;; 7z archive configuration
(defgroup archive-7z nil
"7Z-specific options to archive."
:group 'archive)
(defcustom archive-7z-extract
`(,(or archive-7z-program "7z") "x" "-so")
"Program and its options to run in order to extract a 7z file member.
Extraction should happen to standard output. Archive and member name will
be added."
:version "24.1"
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
(defcustom archive-7z-expunge
`(,(or archive-7z-program "7z") "d")
"Program and its options to run in order to delete 7z file members.
Archive and member names will be added."
:version "24.1"
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
(defcustom archive-7z-update
`(,(or archive-7z-program "7z") "u")
"Program and its options to run in order to update a 7z file member.
Options should ensure that specified directory will be put into the 7z
file. Archive and member name will be added."
:version "24.1"
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v"))))
;; ------------------------------
;; Squashfs archive configuration
(defgroup archive-squashfs nil
"Squashfs-specific options to archive."
:group 'archive)
(defcustom archive-squashfs-extract '("rdsquashfs" "-c")
"Program and its options to run in order to extract a squashsfs file member.
Extraction should happen to standard output. Archive and member name will
be added."
:type '(list (string :tag "Program")
(repeat :tag "Options"
:inline t
(string :format "%v")))
:version "28.1"
:group 'archive-squashfs)
;; -------------------------------------------------------------------------
;;; Section: Variables
(defvar archive-subtype nil "Symbol describing archive type.")
(defvar-local archive-file-list-start nil "Position of first contents line.")
(defvar-local archive-file-list-end nil "Position just after last contents line.")
(defvar-local archive-proper-file-start nil "Position of real archive's start.")
(defvar archive-read-only nil "Non-nil if the archive is read-only on disk.")
(defvar-local archive-local-name nil "Name of local copy of remote archive.")
(defvar archive-mode-map
(let ((map (make-keymap)))
(set-keymap-parent map special-mode-map)
(define-key map " " 'archive-next-line)
(define-key map "a" 'archive-alternate-display)
;;(define-key map "c" 'archive-copy)
(define-key map "d" 'archive-flag-deleted)
(define-key map "\C-d" 'archive-flag-deleted)
(define-key map "e" 'archive-extract)
(define-key map "f" 'archive-extract)
(define-key map "\C-m" 'archive-extract)
(define-key map "C" 'archive-copy-file)
(define-key map "m" 'archive-mark)
(define-key map "n" 'archive-next-line)
(define-key map "\C-n" 'archive-next-line)
(define-key map [down] 'archive-next-line)
(define-key map "o" 'archive-extract-other-window)
(define-key map "p" 'archive-previous-line)
(define-key map "\C-p" 'archive-previous-line)
(define-key map [?\S-\ ] 'archive-previous-line)
(define-key map [up] 'archive-previous-line)
(define-key map "r" 'archive-rename-entry)
(define-key map "u" 'archive-unflag)
(define-key map "\M-\C-?" 'archive-unmark-all-files)
(define-key map "v" 'archive-view)
(define-key map "x" 'archive-expunge)
(define-key map "\177" 'archive-unflag-backwards)
(define-key map "E" 'archive-extract-other-window)
(define-key map "M" 'archive-chmod-entry)
(define-key map "G" 'archive-chgrp-entry)
(define-key map "O" 'archive-chown-entry)
;; Let mouse-1 follow the link.
(define-key map [follow-link] 'mouse-face)
(define-key map [remap undo] #'archive-undo)
(define-key map [mouse-2] 'archive-extract)
(define-key map [menu-bar immediate]
(cons "Immediate" (make-sparse-keymap "Immediate")))
(define-key map [menu-bar immediate alternate]
'(menu-item "Alternate Display" archive-alternate-display
:help "Toggle alternate file info display"))
(define-key map [menu-bar immediate view]
'(menu-item "View This File" archive-view
:help "Display file at cursor in View Mode"))
(define-key map [menu-bar immediate view]
'(menu-item "Copy This File" archive-copy-file
:help "Copy file at cursor to another location"))
(define-key map [menu-bar immediate display]
'(menu-item "Display in Other Window" archive-display-other-window
:help "Display file at cursor in another window"))
(define-key map [menu-bar immediate find-file-other-window]
'(menu-item "Find in Other Window" archive-extract-other-window
:help "Edit file at cursor in another window"))
(define-key map [menu-bar immediate find-file]
'(menu-item "Find This File" archive-extract
:help "Extract file at cursor and edit it"))
(define-key map [menu-bar mark]
(cons "Mark" (make-sparse-keymap "Mark")))
(define-key map [menu-bar mark unmark-all]
'(menu-item "Unmark All" archive-unmark-all-files
:help "Unmark all marked files"))
(define-key map [menu-bar mark deletion]
'(menu-item "Flag" archive-flag-deleted
:help "Flag file at cursor for deletion"))
(define-key map [menu-bar mark unmark]
'(menu-item "Unflag" archive-unflag
:help "Unmark file at cursor"))
(define-key map [menu-bar mark mark]
'(menu-item "Mark" archive-mark
:help "Mark file at cursor"))
(define-key map [menu-bar operate]
(cons "Operate" (make-sparse-keymap "Operate")))
(define-key map [menu-bar operate chown]
'(menu-item "Change Owner..." archive-chown-entry
:enable (fboundp (archive-name "chown-entry"))
:help "Change owner of marked files"))
(define-key map [menu-bar operate chgrp]
'(menu-item "Change Group..." archive-chgrp-entry
:enable (fboundp (archive-name "chgrp-entry"))
:help "Change group ownership of marked files"))
(define-key map [menu-bar operate chmod]
'(menu-item "Change Mode..." archive-chmod-entry
:enable (fboundp (archive-name "chmod-entry"))
:help "Change mode (permissions) of marked files"))
(define-key map [menu-bar operate rename]
'(menu-item "Rename to..." archive-rename-entry
:enable (fboundp (archive-name "rename-entry"))
:help "Rename marked files"))
;;(define-key map [menu-bar operate copy]
;; '(menu-item "Copy to..." archive-copy))
(define-key map [menu-bar operate expunge]
'(menu-item "Expunge Marked Files" archive-expunge
:help "Delete all flagged files from archive"))
map)
"Local keymap for archive mode listings.")
(defvar-local archive-file-name-indent nil "Column where file names start.")
(defvar-local archive-remote nil "Non-nil if the archive is outside file system.")
(put 'archive-remote 'permanent-local t)
(defvar-local archive-member-coding-system nil "Coding-system of archive member.")
(defvar-local archive-alternate-display nil
"Non-nil when alternate information is shown.")
(put 'archive-alternate-display 'permanent-local t)
(defvar archive-superior-buffer nil "In archive members, points to archive.")
(put 'archive-superior-buffer 'permanent-local t)
(defvar-local archive-subfile-mode nil
"Non-nil in archive member buffers.
Its value is an `archive--file-desc'.")
(put 'archive-subfile-mode 'permanent-local t)
(defvar-local archive-file-name-coding-system nil)
(put 'archive-file-name-coding-system 'permanent-local t)
(cl-defstruct (archive--file-desc
(:constructor nil)
(:constructor archive--file-desc
;; ext-file-name and int-file-name are usually `eq'
;; except when int-file-name is the downcased
;; ext-file-name.
(ext-file-name int-file-name mode size time
&key pos ratio uid gid)))
ext-file-name int-file-name
(mode nil :type integer)
(size nil :type integer)
(time nil :type string)
(ratio nil :type string)
uid gid
pos)
;; Features in formats:
;;
;; ARC: size, date&time (date and time strings internally generated)
;; LZH: size, date&time, mode, uid, gid (mode, date, time generated, ugid:int)
;; ZIP: size, date&time, mode (mode, date, time generated)
;; ZOO: size, date&time (date and time strings internally generated)
;; AR : size, date&time, mode, user, group (internally generated)
;; RAR: size, date&time, ratio (all as strings, using `lsar')
;; 7Z : size, date&time (all as strings, using `7z' or `7za')
;;
;; LZH has alternate display (with UID/GID i.s.o MODE/DATE/TIME
(defvar-local archive-files nil
"Vector of `archive--file-desc' objects.")
(defvar tar-archive-from-tar nil)
;; -------------------------------------------------------------------------
;;; Section: Support functions.
(defun arc-insert-unibyte (&rest args)
"Like insert but don't make unibyte string and eight-bit char multibyte."
(dolist (elt args)
(insert (if (and (integerp elt) (>= elt 128))
(decode-char 'eight-bit elt)
elt))))
(defsubst archive-name (suffix)
(intern (concat "archive-" (symbol-name archive-subtype) "-" suffix)))
(defun archive-l-e (str &optional len)
"Convert little endian string/vector STR to integer.
Alternatively, STR may be a buffer position in the current buffer
in which case a second argument, length LEN, should be supplied."
(if (stringp str)
(setq len (length str))
(setq str (buffer-substring str (+ str len))))
(if (multibyte-string-p str)
(setq str (encode-coding-string str 'utf-8-emacs-unix)))
(let ((result 0)
(i 0))
(while (< i len)
(setq i (1+ i)
result (+ (ash result 8)
(aref str (- len i)))))
result))
(define-obsolete-function-alias 'archive-int-to-mode
'file-modes-number-to-symbolic "28.1")
(defun archive-calc-mode (oldmode newmode)
"From the integer OLDMODE and the string NEWMODE calculate a new file mode.
NEWMODE may be an octal number including a leading zero in which case it
will become the new mode.\n
NEWMODE may also be a relative specification like \"og-rwx\" in which case
OLDMODE will be modified accordingly just like chmod(2) would have done."
;; FIXME: Use `file-modes-symbolic-to-number'!
(if (string-match "\\`0[0-7]*\\'" newmode)
(logior (logand oldmode #o177000) (string-to-number newmode 8))
(file-modes-symbolic-to-number newmode oldmode)))
(defun archive-dosdate (date)
"Stringify dos packed DATE record."
(let ((year (+ 1980 (logand (ash date -9) 127)))
(month (logand (ash date -5) 15))
(day (logand date 31)))
(if (or (> month 12) (< month 1))
""
(format "%2d-%s-%d"
day
(aref ["Jan" "Feb" "Mar" "Apr" "May" "Jun"
"Jul" "Aug" "Sep" "Oct" "Nov" "Dec"]
(1- month))
year))))
(defun archive-dostime (time)
"Stringify dos packed TIME record."
(let ((hour (logand (ash time -11) 31))
(minute (logand (ash time -5) 63))
(second (* 2 (logand time 31)))) ; 2 seconds resolution
(format "%02d:%02d:%02d" hour minute second)))
(defun archive-unixdate (low high)
"Stringify Unix (LOW HIGH) date."
(let ((system-time-locale "C"))
(format-time-string "%e-%b-%Y" (list high low))))
(defun archive-unixtime (low high)
"Stringify Unix (LOW HIGH) time."
(format-time-string "%H:%M:%S" (list high low)))
(defun archive-get-lineno ()
(if (>= (point) archive-file-list-start)
(count-lines archive-file-list-start
(line-beginning-position))
0))
(defun archive-get-descr (&optional noerror)
"Return the descriptor vector for file at point.
Does not signal an error if optional argument NOERROR is non-nil."
(let ((no (archive-get-lineno)))
(if (and (>= (point) archive-file-list-start)
(< no (length archive-files)))
(let ((item (aref archive-files no)))
(if (and (archive--file-desc-p item)
(let ((mode (or (archive--file-desc-mode item) 0)))
(zerop (logand 16384 mode))))
item
(if (not noerror)
(user-error "Entry is not a regular member of the archive"))))
(if (not noerror)
(error "Line does not describe a member of the archive")))))
;; -------------------------------------------------------------------------
;;; Section: Helper functions for requiring filename extensions
(defun archive--act-files (command files)
(lambda (archive)
(apply #'call-process (car command)
nil nil nil (append (cdr command) (cons archive files)))))
(defun archive--need-rename-p (&optional archive)
(let ((archive
(file-name-nondirectory (or archive buffer-file-name))))
(cl-case archive-subtype
((zip) (not (seq-contains-p archive ?. #'eq))))))
(defun archive--ensure-extension (archive ensure-extension)
(if ensure-extension
(make-temp-name (expand-file-name (concat archive "_tmp.")))
archive))
(defun archive--maybe-rename (newname need-rename-p)
;; Operating with archive as current buffer, and protect
;; `default-directory' from being modified in `rename-visited-file'.
(when need-rename-p
(let ((default-directory default-directory))
(rename-visited-file newname))))
(defun archive--with-ensure-extension (archive proc-fn)
(let ((saved default-directory))
(with-current-buffer (find-buffer-visiting archive)
(let ((ensure-extension (archive--need-rename-p))
(default-directory saved))
(unwind-protect
;; Some archive programs (like zip) expect filenames to
;; have an extension, so if necessary, temporarily rename
;; an extensionless file for write accesses.
(let ((archive (archive--ensure-extension
archive ensure-extension)))
(archive--maybe-rename archive ensure-extension)
(let ((exitcode (funcall proc-fn archive)))
(or (zerop exitcode)
(error "Updating was unsuccessful (%S)" exitcode))))
(progn (archive--maybe-rename archive ensure-extension)
(revert-buffer nil t)))))))
;; -------------------------------------------------------------------------
;;; Section: the mode definition
;;;###autoload
(defun archive-mode (&optional force)
"Major mode for viewing an archive file in a dired-like way.
You can move around using the usual cursor motion commands.
Letters no longer insert themselves.\\<archive-mode-map>
Type \\[archive-extract] to pull a file out of the archive and into its own buffer;
or click mouse-2 on the file's line in the archive mode buffer.
If you edit a sub-file of this archive (as with the \\[archive-extract] command) and
save it, the contents of that buffer will be saved back into the
archive.
\\{archive-mode-map}"
;; This is not interactive because you shouldn't be turning this
;; mode on and off. You can corrupt things that way.
(if (zerop (buffer-size))
;; At present we cannot create archives from scratch
(funcall (or (default-value 'major-mode) #'fundamental-mode))
(if (and (not force) archive-files) nil
(kill-all-local-variables)
(let* ((type (archive-find-type))
(typename (capitalize (symbol-name type))))
(setq-local archive-subtype type)
;; Buffer contains treated image of file before the file contents
(add-function :around (local 'revert-buffer-function)
#'archive--mode-revert)
(add-hook 'write-contents-functions #'archive-write-file nil t)
(setq-local truncate-lines t)
(setq-local require-final-newline nil)
(setq-local local-enable-local-variables nil)
;; Prevent loss of data when saving the file.
(setq-local file-precious-flag t)
;; Archives which are inside other archives and whose
;; names are invalid for this OS, can't be written.
(setq-local archive-read-only
(or (not (file-writable-p (buffer-file-name)))
(and archive-subfile-mode
(string-match file-name-invalid-regexp
(archive--file-desc-ext-file-name
archive-subfile-mode)))))
;; An archive can contain another archive whose name is invalid
;; on local filesystem. Treat such archives as remote.
(or archive-remote
(setq archive-remote
(or tar-archive-from-tar ; was included in a tar archive
(string-match archive-remote-regexp (buffer-file-name))
(string-match file-name-invalid-regexp
(buffer-file-name)))))
(setq major-mode #'archive-mode)
(setq mode-name (concat typename "-Archive"))
;; Run archive-foo-mode-hook and archive-mode-hook
(run-mode-hooks (archive-name "mode-hook") 'archive-mode-hook)
(use-local-map archive-mode-map))
(setq archive-file-name-coding-system
(or file-name-coding-system
default-file-name-coding-system
locale-coding-system))
(set-buffer-multibyte 'to)
(archive-summarize nil)
(setq buffer-read-only t)
(when (and archive-visit-single-files
auto-compression-mode
(= (length archive-files) 1))
(rename-buffer (concat " " (buffer-name)))
(archive-extract)))))
;; Archive mode is suitable only for specially formatted data.
(put 'archive-mode 'mode-class 'special)
(let ((item1 '(archive-subfile-mode " Archive")))
(or (member item1 minor-mode-alist)
(setq minor-mode-alist (cons item1 minor-mode-alist))))
;; -------------------------------------------------------------------------
(defun archive-find-type ()
(widen)
(goto-char (point-min))
;; The funny [] here make it unlikely that the .elc file will be treated
;; as an archive by other software.
(let (case-fold-search)
(cond ((looking-at "\\(PK00\\)?[P]K\003\004") 'zip)
((looking-at "..-l[hz][0-9ds]-") 'lzh)
((looking-at "....................[\334]\247\304\375") 'zoo)
((and (looking-at "\C-z") ; signature too simple, IMHO
(string-match "\\.[aA][rR][cC]\\'"
(or buffer-file-name (buffer-name))))
'arc)
;; This pattern modeled on the BSD/GNU+Linux `file' command.
;; Have seen capital "LHA's", and file has lower case "LHa's" too.
;; Note this regexp is also in archive-exe-p.
((looking-at "MZ\\(.\\|\n\\)\\{34\\}LH[aA]'s SFX ") 'lzh-exe)
((looking-at "Rar!") 'rar)
((looking-at "!<arch>\n") 'ar)
((and (looking-at "MZ")
(re-search-forward "Rar!" (+ (point) 100000) t))
'rar-exe)
((looking-at "7z\274\257\047\034") '7z)
((looking-at "hsqs") 'squashfs)
(t (error "Buffer format not recognized")))))
;; -------------------------------------------------------------------------
(defun archive-desummarize ()
(let ((inhibit-read-only t)
(modified (buffer-modified-p)))
(widen)
(delete-region (point-min) archive-proper-file-start)
(restore-buffer-modified-p modified)))
(defun archive-summarize (&optional shut-up)
"Parse the contents of the archive file in the current buffer.
Place a dired-like listing on the front;
then narrow to it, so that only that listing
is visible (and the real data of the buffer is hidden).
Optional argument SHUT-UP, if non-nil, means don't print messages
when parsing the archive."
(widen)
(let ((create-lockfiles nil) ; avoid changing dir mtime by lock_file
(inhibit-read-only t))
(setq archive-proper-file-start (copy-marker (point-min) t))
(add-hook 'change-major-mode-hook #'archive-desummarize nil t)
(or shut-up
(message "Parsing archive file..."))
(buffer-disable-undo (current-buffer))
(setq archive-files (funcall (archive-name "summarize")))
(or shut-up
(message "Parsing archive file...done."))
(setq archive-proper-file-start (point-marker))
(narrow-to-region (point-min) (point))
(set-buffer-modified-p nil)
(buffer-enable-undo))
(goto-char archive-file-list-start)
(archive-next-line 0))
(defun archive-resummarize ()
"Recreate the contents listing of an archive."
(let ((no (archive-get-lineno)))
(archive-desummarize)
(archive-summarize t)
(goto-char archive-file-list-start)
(archive-next-line no)))
(cl-defstruct (archive--file-summary
(:constructor nil)
(:constructor archive--file-summary (text name-start name-end)))
text name-start name-end)
(defun archive-summarize-files (files)
"Insert a description of a list of files annotated with proper mouse face."
(setq archive-file-list-start (point-marker))
;; Here we assume that they all start at the same column.
(setq archive-file-name-indent
;; FIXME: We assume chars=columns (no double-wide chars and such).
(if files (archive--file-summary-name-start (car files)) 0))
;; We don't want to do an insert for each element since that takes too
;; long when the archive -- which has to be moved in memory -- is large.
(insert
(mapconcat
(lambda (fil)
;; Using `concat' here copies the text also, so we can add
;; properties without problems.
(let ((text (concat (archive--file-summary-text fil) "\n")))
(add-text-properties
(archive--file-summary-name-start fil)
(archive--file-summary-name-end fil)
'(mouse-face highlight
help-echo "mouse-2: extract this file into a buffer")
text)
text))
files
""))
(setq archive-file-list-end (point-marker)))
(defun archive-alternate-display ()
"Toggle alternative display.
To avoid very long lines archive mode does not show all information.
This function changes the set of information shown for each files."
(interactive)
(setq archive-alternate-display (not archive-alternate-display))
(setq-local archive-hidden-columns
(if archive-alternate-display
archive-alternate-hidden-columns
(eval (car (or (get 'archive-hidden-columns 'customized-value)
(get 'archive-hidden-columns 'standard-value)))
t)))
(archive-resummarize))
(defun archive-hideshow-column (column)
"Toggle visibility of COLUMN."
(interactive
(list (intern
(completing-read "Toggle visibility of: "
'(Mode Ids Ratio Date&Time)
nil t))))
(setq-local archive-hidden-columns
(if (memq column archive-hidden-columns)
(remove column archive-hidden-columns)
(cons column archive-hidden-columns)))
(archive-resummarize))
;; -------------------------------------------------------------------------
;;; Section: Local archive copy handling
(defun archive-unique-fname (fname dir)
"Make sure a file FNAME can be created uniquely in directory DIR.
If FNAME can be uniquely created in DIR, it is returned unaltered.
If FNAME is something our underlying filesystem can't grok, or if another
file by that name already exists in DIR, a unique new name is generated
using `make-temp-file', and the generated name is returned."
(if (file-name-absolute-p fname)
;; We need a file name relative to the filesystem root.
(setq fname (substring fname (1+ (string-search "/" fname)))))
(let ((fullname (expand-file-name fname dir))
(alien (string-match file-name-invalid-regexp fname))
(tmpfile
(expand-file-name
(if (if (fboundp 'msdos-long-file-names)
(not (msdos-long-file-names)))
"am"
"arc-mode.")
dir)))
(if (or alien (file-exists-p fullname))
(progn
;; Make sure all the leading directories in
;; archive-local-name exist under archive-tmpdir, so that
;; the directory structure recorded in the archive is
;; reconstructed in the temporary directory.
(make-directory (file-name-directory tmpfile) t)
(make-temp-file tmpfile))
;; Make sure all the leading directories in `fullname' exist
;; under archive-tmpdir. This is necessary for nested archives
;; (`archive-extract' sets `archive-remote' to t in case
;; an archive occurs inside another archive).
(make-directory (file-name-directory fullname) t)
fullname)))
(defun archive-maybe-copy (archive)
(let ((coding-system-for-write 'no-conversion))
(if archive-remote
(let ((start (point-max))
;; Sometimes ARCHIVE is invalid while its actual name, as
;; recorded in its parent archive, is not. For example, an
;; archive bar.zip inside another archive foo.zip gets a name
;; "foo.zip:bar.zip", which is invalid on DOS/Windows.
;; So use the actual name if available.
(archive-name
(or (and archive-subfile-mode (archive--file-desc-ext-file-name
archive-subfile-mode))
archive)))
(setq archive-local-name
(archive-unique-fname archive-name archive-tmpdir))
(save-restriction
(widen)
(write-region start (point-max) archive-local-name nil 'nomessage))
archive-local-name)
(if (buffer-modified-p) (save-buffer))
archive)))
(defun archive-maybe-update (unchanged)
(if archive-remote
(let ((name archive-local-name)
(modified (buffer-modified-p))
(coding-system-for-read 'no-conversion)
(lno (archive-get-lineno))
(inhibit-read-only t))
(if unchanged nil
;; FIXME: Use archive-resummarize?
(setq archive-files nil)
(erase-buffer)
(insert-file-contents name)
(archive-mode t)
(goto-char archive-file-list-start)
(archive-next-line lno))
(archive-delete-local name)
(if (not unchanged)
(message
"Buffer `%s' must be saved for changes to take effect"
(buffer-name (current-buffer))))
(set-buffer-modified-p (or modified (not unchanged))))))
(defun archive-delete-local (name)
"Delete file NAME and its parents up to and including `archive-tmpdir'."
(let ((again t)
(top (directory-file-name (file-name-as-directory archive-tmpdir))))
(condition-case nil