arc-mode.el 89 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313
  1. ;;; arc-mode.el --- simple editing of archives
  2. ;; Copyright (C) 1995, 1997-1998, 2001-2017 Free Software Foundation,
  3. ;; Inc.
  4. ;; Author: Morten Welinder <terra@gnu.org>
  5. ;; Keywords: files archives ms-dos editing major-mode
  6. ;; Favorite-brand-of-beer: None, I hate beer.
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; NAMING: "arc" is short for "archive" and does not refer specifically
  20. ;; to files whose name end in ".arc"
  21. ;;
  22. ;; This code does not decode any files internally, although it does
  23. ;; understand the directory level of the archives. For this reason,
  24. ;; you should expect this code to need more fiddling than tar-mode.el
  25. ;; (although it at present has fewer bugs :-) In particular, I have
  26. ;; not tested this under MS-DOS myself.
  27. ;; -------------------------------------
  28. ;; INTERACTION: arc-mode.el should play together with
  29. ;;
  30. ;; * ange-ftp.el: Remote archives (i.e., ones that ange-ftp has brought
  31. ;; to you) are handled by doing all updates on a local
  32. ;; copy. When you make changes to a remote file the
  33. ;; changes will first take effect when the archive buffer
  34. ;; is saved. You will be warned about this.
  35. ;;
  36. ;; * dos-fns.el: (Part of Emacs 19). You get automatic ^M^J <--> ^J
  37. ;; conversion.
  38. ;;
  39. ;; arc-mode.el does not work well with crypt++.el; for the archives as
  40. ;; such this could be fixed (but wouldn't be useful) by declaring such
  41. ;; archives to be "remote". For the members this is a general Emacs
  42. ;; problem that 19.29's file formats may fix.
  43. ;; -------------------------------------
  44. ;; ARCHIVE TYPES: Currently only the archives below are handled, but the
  45. ;; structure for handling just about anything is in place.
  46. ;;
  47. ;; Arc Lzh Zip Zoo Rar 7z
  48. ;; --------------------------------------------
  49. ;; View listing Intern Intern Intern Intern Y Y
  50. ;; Extract member Y Y Y Y Y Y
  51. ;; Save changed member Y Y Y Y N Y
  52. ;; Add new member N N N N N N
  53. ;; Delete member Y Y Y Y N Y
  54. ;; Rename member Y Y N N N N
  55. ;; Chmod - Y Y - N N
  56. ;; Chown - Y - - N N
  57. ;; Chgrp - Y - - N N
  58. ;;
  59. ;; Special thanks to Bill Brodie <wbrodie@panix.com> for very useful tips
  60. ;; on the first released version of this package.
  61. ;;
  62. ;; This code is partly based on tar-mode.el from Emacs.
  63. ;; -------------------------------------
  64. ;; ARCHIVE STRUCTURES:
  65. ;; (This is mostly for myself.)
  66. ;;
  67. ;; ARC A series of (header,file). No interactions among members.
  68. ;;
  69. ;; LZH A series of (header,file). Headers are checksummed. No
  70. ;; interaction among members.
  71. ;; Headers come in three flavors called level 0, 1 and 2 headers.
  72. ;; Level 2 header is free of DOS specific restrictions and most
  73. ;; commonly used. Also level 1 and 2 headers consist of base
  74. ;; and extension headers. For more details see
  75. ;; http://homepage1.nifty.com/dangan/en/Content/Program/Java/jLHA/Notes/Notes.html
  76. ;; http://www.osirusoft.com/joejared/lzhformat.html
  77. ;;
  78. ;; ZIP A series of (lheader,fil) followed by a "central directory"
  79. ;; which is a series of (cheader) followed by an end-of-
  80. ;; central-dir record possibly followed by junk. The e-o-c-d
  81. ;; links to c-d. cheaders link to lheaders which are basically
  82. ;; cut-down versions of the cheaders.
  83. ;;
  84. ;; ZOO An archive header followed by a series of (header,file).
  85. ;; Each member header points to the next. The archive is
  86. ;; terminated by a bogus header with a zero next link.
  87. ;; -------------------------------------
  88. ;; HOOKS: `foo' means one of the supported archive types.
  89. ;;
  90. ;; archive-mode-hook
  91. ;; archive-foo-mode-hook
  92. ;; archive-extract-hook
  93. ;;; Code:
  94. ;; -------------------------------------------------------------------------
  95. ;;; Section: Configuration.
  96. (defgroup archive nil
  97. "Simple editing of archives."
  98. :group 'data)
  99. (defgroup archive-arc nil
  100. "ARC-specific options to archive."
  101. :group 'archive)
  102. (defgroup archive-lzh nil
  103. "LZH-specific options to archive."
  104. :group 'archive)
  105. (defgroup archive-zip nil
  106. "ZIP-specific options to archive."
  107. :group 'archive)
  108. (defgroup archive-zoo nil
  109. "ZOO-specific options to archive."
  110. :group 'archive)
  111. (defcustom archive-tmpdir
  112. ;; make-temp-name is safe here because we use this name
  113. ;; to create a directory.
  114. (make-temp-name
  115. (expand-file-name (if (eq system-type 'ms-dos) "ar" "archive.tmp")
  116. temporary-file-directory))
  117. "Directory for temporary files made by `arc-mode.el'."
  118. :type 'directory
  119. :group 'archive)
  120. (defcustom archive-remote-regexp "^/[^/:]*[^/:.]:"
  121. "Regexp recognizing archive files names that are not local.
  122. A non-local file is one whose file name is not proper outside Emacs.
  123. A local copy of the archive will be used when updating."
  124. :type 'regexp
  125. :group 'archive)
  126. (define-obsolete-variable-alias 'archive-extract-hooks
  127. 'archive-extract-hook "24.3")
  128. (defcustom archive-extract-hook nil
  129. "Hook run when an archive member has been extracted."
  130. :type 'hook
  131. :group 'archive)
  132. (defcustom archive-visit-single-files nil
  133. "If non-nil, opening an archive with a single file visits that file.
  134. If nil, visiting such an archive displays the archive summary."
  135. :version "25.1"
  136. :type '(choice (const :tag "Visit the single file" t)
  137. (const :tag "Show the archive summary" nil))
  138. :group 'archive)
  139. ;; ------------------------------
  140. ;; Arc archive configuration
  141. ;; We always go via a local file since there seems to be no reliable way
  142. ;; to extract to stdout without junk getting added.
  143. (defcustom archive-arc-extract
  144. '("arc" "x")
  145. "Program and its options to run in order to extract an arc file member.
  146. Extraction should happen to the current directory. Archive and member
  147. name will be added."
  148. :type '(list (string :tag "Program")
  149. (repeat :tag "Options"
  150. :inline t
  151. (string :format "%v")))
  152. :group 'archive-arc)
  153. (defcustom archive-arc-expunge
  154. '("arc" "d")
  155. "Program and its options to run in order to delete arc file members.
  156. Archive and member names will be added."
  157. :type '(list (string :tag "Program")
  158. (repeat :tag "Options"
  159. :inline t
  160. (string :format "%v")))
  161. :group 'archive-arc)
  162. (defcustom archive-arc-write-file-member
  163. '("arc" "u")
  164. "Program and its options to run in order to update an arc file member.
  165. Archive and member name will be added."
  166. :type '(list (string :tag "Program")
  167. (repeat :tag "Options"
  168. :inline t
  169. (string :format "%v")))
  170. :group 'archive-arc)
  171. ;; ------------------------------
  172. ;; Lzh archive configuration
  173. (defcustom archive-lzh-extract
  174. '("lha" "pq")
  175. "Program and its options to run in order to extract an lzh file member.
  176. Extraction should happen to standard output. Archive and member name will
  177. be added."
  178. :type '(list (string :tag "Program")
  179. (repeat :tag "Options"
  180. :inline t
  181. (string :format "%v")))
  182. :group 'archive-lzh)
  183. (defcustom archive-lzh-expunge
  184. '("lha" "d")
  185. "Program and its options to run in order to delete lzh file members.
  186. Archive and member names will be added."
  187. :type '(list (string :tag "Program")
  188. (repeat :tag "Options"
  189. :inline t
  190. (string :format "%v")))
  191. :group 'archive-lzh)
  192. (defcustom archive-lzh-write-file-member
  193. '("lha" "a")
  194. "Program and its options to run in order to update an lzh file member.
  195. Archive and member name will be added."
  196. :type '(list (string :tag "Program")
  197. (repeat :tag "Options"
  198. :inline t
  199. (string :format "%v")))
  200. :group 'archive-lzh)
  201. ;; ------------------------------
  202. ;; Zip archive configuration
  203. (defvar archive-7z-program (let ((7z (or (executable-find "7z")
  204. (executable-find "7za"))))
  205. (when 7z
  206. (file-name-nondirectory 7z))))
  207. (defcustom archive-zip-extract
  208. (cond ((executable-find "unzip") '("unzip" "-qq" "-c"))
  209. (archive-7z-program `(,archive-7z-program "x" "-so"))
  210. ((executable-find "pkunzip") '("pkunzip" "-e" "-o-"))
  211. (t '("unzip" "-qq" "-c")))
  212. "Program and its options to run in order to extract a zip file member.
  213. Extraction should happen to standard output. Archive and member name will
  214. be added."
  215. :type '(list (string :tag "Program")
  216. (repeat :tag "Options"
  217. :inline t
  218. (string :format "%v")))
  219. :group 'archive-zip)
  220. ;; For several reasons the latter behavior is not desirable in general.
  221. ;; (1) It uses more disk space. (2) Error checking is worse or non-
  222. ;; existent. (3) It tends to do funny things with other systems' file
  223. ;; names.
  224. (defcustom archive-zip-expunge
  225. (cond ((executable-find "zip") '("zip" "-d" "-q"))
  226. (archive-7z-program `(,archive-7z-program "d"))
  227. ((executable-find "pkzip") '("pkzip" "-d"))
  228. (t '("zip" "-d" "-q")))
  229. "Program and its options to run in order to delete zip file members.
  230. Archive and member names will be added."
  231. :type '(list (string :tag "Program")
  232. (repeat :tag "Options"
  233. :inline t
  234. (string :format "%v")))
  235. :group 'archive-zip)
  236. (defcustom archive-zip-update
  237. (cond ((executable-find "zip") '("zip" "-q"))
  238. (archive-7z-program `(,archive-7z-program "u"))
  239. ((executable-find "pkzip") '("pkzip" "-u" "-P"))
  240. (t '("zip" "-q")))
  241. "Program and its options to run in order to update a zip file member.
  242. Options should ensure that specified directory will be put into the zip
  243. file. Archive and member name will be added."
  244. :type '(list (string :tag "Program")
  245. (repeat :tag "Options"
  246. :inline t
  247. (string :format "%v")))
  248. :group 'archive-zip)
  249. (defcustom archive-zip-update-case
  250. (cond ((executable-find "zip") '("zip" "-q" "-k"))
  251. (archive-7z-program `(,archive-7z-program "u"))
  252. ((executable-find "pkzip") '("pkzip" "-u" "-P"))
  253. (t '("zip" "-q" "-k")))
  254. "Program and its options to run in order to update a case fiddled zip member.
  255. Options should ensure that specified directory will be put into the zip file.
  256. Archive and member name will be added."
  257. :type '(list (string :tag "Program")
  258. (repeat :tag "Options"
  259. :inline t
  260. (string :format "%v")))
  261. :group 'archive-zip)
  262. (defcustom archive-zip-case-fiddle t
  263. "If non-nil then zip file members may be down-cased.
  264. This case fiddling will only happen for members created by a system
  265. that uses caseless file names."
  266. :type 'boolean
  267. :group 'archive-zip)
  268. ;; ------------------------------
  269. ;; Zoo archive configuration
  270. (defcustom archive-zoo-extract
  271. '("zoo" "xpq")
  272. "Program and its options to run in order to extract a zoo file member.
  273. Extraction should happen to standard output. Archive and member name will
  274. be added."
  275. :type '(list (string :tag "Program")
  276. (repeat :tag "Options"
  277. :inline t
  278. (string :format "%v")))
  279. :group 'archive-zoo)
  280. (defcustom archive-zoo-expunge
  281. '("zoo" "DqPP")
  282. "Program and its options to run in order to delete zoo file members.
  283. Archive and member names will be added."
  284. :type '(list (string :tag "Program")
  285. (repeat :tag "Options"
  286. :inline t
  287. (string :format "%v")))
  288. :group 'archive-zoo)
  289. (defcustom archive-zoo-write-file-member
  290. '("zoo" "a")
  291. "Program and its options to run in order to update a zoo file member.
  292. Archive and member name will be added."
  293. :type '(list (string :tag "Program")
  294. (repeat :tag "Options"
  295. :inline t
  296. (string :format "%v")))
  297. :group 'archive-zoo)
  298. ;; ------------------------------
  299. ;; 7z archive configuration
  300. (defcustom archive-7z-extract
  301. `(,(or archive-7z-program "7z") "x" "-so")
  302. "Program and its options to run in order to extract a 7z file member.
  303. Extraction should happen to standard output. Archive and member name will
  304. be added."
  305. :version "24.1"
  306. :type '(list (string :tag "Program")
  307. (repeat :tag "Options"
  308. :inline t
  309. (string :format "%v")))
  310. :group 'archive-7z)
  311. (defcustom archive-7z-expunge
  312. `(,(or archive-7z-program "7z") "d")
  313. "Program and its options to run in order to delete 7z file members.
  314. Archive and member names will be added."
  315. :version "24.1"
  316. :type '(list (string :tag "Program")
  317. (repeat :tag "Options"
  318. :inline t
  319. (string :format "%v")))
  320. :group 'archive-7z)
  321. (defcustom archive-7z-update
  322. `(,(or archive-7z-program "7z") "u")
  323. "Program and its options to run in order to update a 7z file member.
  324. Options should ensure that specified directory will be put into the 7z
  325. file. Archive and member name will be added."
  326. :version "24.1"
  327. :type '(list (string :tag "Program")
  328. (repeat :tag "Options"
  329. :inline t
  330. (string :format "%v")))
  331. :group 'archive-7z)
  332. ;; -------------------------------------------------------------------------
  333. ;;; Section: Variables
  334. (defvar archive-subtype nil "Symbol describing archive type.")
  335. (defvar archive-file-list-start nil "Position of first contents line.")
  336. (defvar archive-file-list-end nil "Position just after last contents line.")
  337. (defvar archive-proper-file-start nil "Position of real archive's start.")
  338. (defvar archive-read-only nil "Non-nil if the archive is read-only on disk.")
  339. (defvar archive-local-name nil "Name of local copy of remote archive.")
  340. (defvar archive-mode-map
  341. (let ((map (make-keymap)))
  342. (set-keymap-parent map special-mode-map)
  343. (define-key map " " 'archive-next-line)
  344. (define-key map "a" 'archive-alternate-display)
  345. ;;(define-key map "c" 'archive-copy)
  346. (define-key map "d" 'archive-flag-deleted)
  347. (define-key map "\C-d" 'archive-flag-deleted)
  348. (define-key map "e" 'archive-extract)
  349. (define-key map "f" 'archive-extract)
  350. (define-key map "\C-m" 'archive-extract)
  351. (define-key map "m" 'archive-mark)
  352. (define-key map "n" 'archive-next-line)
  353. (define-key map "\C-n" 'archive-next-line)
  354. (define-key map [down] 'archive-next-line)
  355. (define-key map "o" 'archive-extract-other-window)
  356. (define-key map "p" 'archive-previous-line)
  357. (define-key map "\C-p" 'archive-previous-line)
  358. (define-key map [?\S-\ ] 'archive-previous-line)
  359. (define-key map [up] 'archive-previous-line)
  360. (define-key map "r" 'archive-rename-entry)
  361. (define-key map "u" 'archive-unflag)
  362. (define-key map "\M-\C-?" 'archive-unmark-all-files)
  363. (define-key map "v" 'archive-view)
  364. (define-key map "x" 'archive-expunge)
  365. (define-key map "\177" 'archive-unflag-backwards)
  366. (define-key map "E" 'archive-extract-other-window)
  367. (define-key map "M" 'archive-chmod-entry)
  368. (define-key map "G" 'archive-chgrp-entry)
  369. (define-key map "O" 'archive-chown-entry)
  370. ;; Let mouse-1 follow the link.
  371. (define-key map [follow-link] 'mouse-face)
  372. (if (fboundp 'command-remapping)
  373. (progn
  374. (define-key map [remap advertised-undo] 'archive-undo)
  375. (define-key map [remap undo] 'archive-undo))
  376. (substitute-key-definition 'advertised-undo 'archive-undo map global-map)
  377. (substitute-key-definition 'undo 'archive-undo map global-map))
  378. (define-key map
  379. (if (featurep 'xemacs) 'button2 [mouse-2]) 'archive-extract)
  380. (if (featurep 'xemacs)
  381. () ; out of luck
  382. (define-key map [menu-bar immediate]
  383. (cons "Immediate" (make-sparse-keymap "Immediate")))
  384. (define-key map [menu-bar immediate alternate]
  385. '(menu-item "Alternate Display" archive-alternate-display
  386. :enable (boundp (archive-name "alternate-display"))
  387. :help "Toggle alternate file info display"))
  388. (define-key map [menu-bar immediate view]
  389. '(menu-item "View This File" archive-view
  390. :help "Display file at cursor in View Mode"))
  391. (define-key map [menu-bar immediate display]
  392. '(menu-item "Display in Other Window" archive-display-other-window
  393. :help "Display file at cursor in another window"))
  394. (define-key map [menu-bar immediate find-file-other-window]
  395. '(menu-item "Find in Other Window" archive-extract-other-window
  396. :help "Edit file at cursor in another window"))
  397. (define-key map [menu-bar immediate find-file]
  398. '(menu-item "Find This File" archive-extract
  399. :help "Extract file at cursor and edit it"))
  400. (define-key map [menu-bar mark]
  401. (cons "Mark" (make-sparse-keymap "Mark")))
  402. (define-key map [menu-bar mark unmark-all]
  403. '(menu-item "Unmark All" archive-unmark-all-files
  404. :help "Unmark all marked files"))
  405. (define-key map [menu-bar mark deletion]
  406. '(menu-item "Flag" archive-flag-deleted
  407. :help "Flag file at cursor for deletion"))
  408. (define-key map [menu-bar mark unmark]
  409. '(menu-item "Unflag" archive-unflag
  410. :help "Unmark file at cursor"))
  411. (define-key map [menu-bar mark mark]
  412. '(menu-item "Mark" archive-mark
  413. :help "Mark file at cursor"))
  414. (define-key map [menu-bar operate]
  415. (cons "Operate" (make-sparse-keymap "Operate")))
  416. (define-key map [menu-bar operate chown]
  417. '(menu-item "Change Owner..." archive-chown-entry
  418. :enable (fboundp (archive-name "chown-entry"))
  419. :help "Change owner of marked files"))
  420. (define-key map [menu-bar operate chgrp]
  421. '(menu-item "Change Group..." archive-chgrp-entry
  422. :enable (fboundp (archive-name "chgrp-entry"))
  423. :help "Change group ownership of marked files"))
  424. (define-key map [menu-bar operate chmod]
  425. '(menu-item "Change Mode..." archive-chmod-entry
  426. :enable (fboundp (archive-name "chmod-entry"))
  427. :help "Change mode (permissions) of marked files"))
  428. (define-key map [menu-bar operate rename]
  429. '(menu-item "Rename to..." archive-rename-entry
  430. :enable (fboundp (archive-name "rename-entry"))
  431. :help "Rename marked files"))
  432. ;;(define-key map [menu-bar operate copy]
  433. ;; '(menu-item "Copy to..." archive-copy))
  434. (define-key map [menu-bar operate expunge]
  435. '(menu-item "Expunge Marked Files" archive-expunge
  436. :help "Delete all flagged files from archive"))
  437. map))
  438. "Local keymap for archive mode listings.")
  439. (defvar archive-file-name-indent nil "Column where file names start.")
  440. (defvar archive-remote nil "Non-nil if the archive is outside file system.")
  441. (make-variable-buffer-local 'archive-remote)
  442. (put 'archive-remote 'permanent-local t)
  443. (defvar archive-member-coding-system nil "Coding-system of archive member.")
  444. (make-variable-buffer-local 'archive-member-coding-system)
  445. (defvar archive-alternate-display nil
  446. "Non-nil when alternate information is shown.")
  447. (make-variable-buffer-local 'archive-alternate-display)
  448. (put 'archive-alternate-display 'permanent-local t)
  449. (defvar archive-superior-buffer nil "In archive members, points to archive.")
  450. (put 'archive-superior-buffer 'permanent-local t)
  451. (defvar archive-subfile-mode nil "Non-nil in archive member buffers.")
  452. (make-variable-buffer-local 'archive-subfile-mode)
  453. (put 'archive-subfile-mode 'permanent-local t)
  454. (defvar archive-file-name-coding-system nil)
  455. (make-variable-buffer-local 'archive-file-name-coding-system)
  456. (put 'archive-file-name-coding-system 'permanent-local t)
  457. (defvar archive-files nil
  458. "Vector of file descriptors.
  459. Each descriptor is a vector of the form
  460. [EXT-FILE-NAME INT-FILE-NAME CASE-FIDDLED MODE ...]")
  461. (make-variable-buffer-local 'archive-files)
  462. ;; -------------------------------------------------------------------------
  463. ;;; Section: Support functions.
  464. (eval-when-compile
  465. (defsubst byte-after (pos)
  466. "Like char-after but an eight-bit char is converted to unibyte."
  467. (multibyte-char-to-unibyte (char-after pos)))
  468. (defsubst insert-unibyte (&rest args)
  469. "Like insert but don't make unibyte string and eight-bit char multibyte."
  470. (dolist (elt args)
  471. (if (integerp elt)
  472. (insert (if (< elt 128) elt (decode-char 'eight-bit elt)))
  473. (insert (string-to-multibyte elt)))))
  474. )
  475. (defsubst archive-name (suffix)
  476. (intern (concat "archive-" (symbol-name archive-subtype) "-" suffix)))
  477. (defun archive-l-e (str &optional len float)
  478. "Convert little endian string/vector STR to integer.
  479. Alternatively, STR may be a buffer position in the current buffer
  480. in which case a second argument, length LEN, should be supplied.
  481. FLOAT, if non-nil, means generate and return a float instead of an integer
  482. \(use this for numbers that can overflow the Emacs integer)."
  483. (if (stringp str)
  484. (setq len (length str))
  485. (setq str (buffer-substring str (+ str len))))
  486. (setq str (string-as-unibyte str))
  487. (let ((result 0)
  488. (i 0))
  489. (while (< i len)
  490. (setq i (1+ i)
  491. result (+ (if float (* result 256.0) (ash result 8))
  492. (aref str (- len i)))))
  493. result))
  494. (defun archive-int-to-mode (mode)
  495. "Turn an integer like 0700 (i.e., 448) into a mode string like -rwx------."
  496. ;; FIXME: merge with tar-grind-file-mode.
  497. (string
  498. (if (zerop (logand 8192 mode))
  499. (if (zerop (logand 16384 mode)) ?- ?d)
  500. ?c) ; completeness
  501. (if (zerop (logand 256 mode)) ?- ?r)
  502. (if (zerop (logand 128 mode)) ?- ?w)
  503. (if (zerop (logand 64 mode))
  504. (if (zerop (logand 2048 mode)) ?- ?S)
  505. (if (zerop (logand 2048 mode)) ?x ?s))
  506. (if (zerop (logand 32 mode)) ?- ?r)
  507. (if (zerop (logand 16 mode)) ?- ?w)
  508. (if (zerop (logand 8 mode))
  509. (if (zerop (logand 1024 mode)) ?- ?S)
  510. (if (zerop (logand 1024 mode)) ?x ?s))
  511. (if (zerop (logand 4 mode)) ?- ?r)
  512. (if (zerop (logand 2 mode)) ?- ?w)
  513. (if (zerop (logand 1 mode)) ?- ?x)))
  514. (defun archive-calc-mode (oldmode newmode &optional error)
  515. "From the integer OLDMODE and the string NEWMODE calculate a new file mode.
  516. NEWMODE may be an octal number including a leading zero in which case it
  517. will become the new mode.\n
  518. NEWMODE may also be a relative specification like \"og-rwx\" in which case
  519. OLDMODE will be modified accordingly just like chmod(2) would have done.\n
  520. If optional third argument ERROR is non-nil an error will be signaled if
  521. the mode is invalid. If ERROR is nil then nil will be returned."
  522. (cond ((string-match "^0[0-7]*$" newmode)
  523. (let ((result 0)
  524. (len (length newmode))
  525. (i 1))
  526. (while (< i len)
  527. (setq result (+ (lsh result 3) (aref newmode i) (- ?0))
  528. i (1+ i)))
  529. (logior (logand oldmode 65024) result)))
  530. ((string-match "^\\([agou]+\\)\\([---+=]\\)\\([rwxst]+\\)$" newmode)
  531. (let ((who 0)
  532. (result oldmode)
  533. (op (aref newmode (match-beginning 2)))
  534. (bits 0)
  535. (i (match-beginning 3)))
  536. (while (< i (match-end 3))
  537. (let ((rwx (aref newmode i)))
  538. (setq bits (logior bits (cond ((= rwx ?r) 292)
  539. ((= rwx ?w) 146)
  540. ((= rwx ?x) 73)
  541. ((= rwx ?s) 3072)
  542. ((= rwx ?t) 512)))
  543. i (1+ i))))
  544. (while (< who (match-end 1))
  545. (let* ((whoc (aref newmode who))
  546. (whomask (cond ((= whoc ?a) 4095)
  547. ((= whoc ?u) 1472)
  548. ((= whoc ?g) 2104)
  549. ((= whoc ?o) 7))))
  550. (if (= op ?=)
  551. (setq result (logand result (lognot whomask))))
  552. (if (= op ?-)
  553. (setq result (logand result (lognot (logand whomask bits))))
  554. (setq result (logior result (logand whomask bits)))))
  555. (setq who (1+ who)))
  556. result))
  557. (t
  558. (if error
  559. (error "Invalid mode specification: %s" newmode)))))
  560. (defun archive-dosdate (date)
  561. "Stringify dos packed DATE record."
  562. (let ((year (+ 1980 (logand (ash date -9) 127)))
  563. (month (logand (ash date -5) 15))
  564. (day (logand date 31)))
  565. (if (or (> month 12) (< month 1))
  566. ""
  567. (format "%2d-%s-%d"
  568. day
  569. (aref ["Jan" "Feb" "Mar" "Apr" "May" "Jun"
  570. "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"] (1- month))
  571. year))))
  572. (defun archive-dostime (time)
  573. "Stringify dos packed TIME record."
  574. (let ((hour (logand (ash time -11) 31))
  575. (minute (logand (ash time -5) 63))
  576. (second (* 2 (logand time 31)))) ; 2 seconds resolution
  577. (format "%02d:%02d:%02d" hour minute second)))
  578. (defun archive-unixdate (low high)
  579. "Stringify Unix (LOW HIGH) date."
  580. (let* ((time (cons high low))
  581. (str (current-time-string time)))
  582. (format "%s-%s-%s"
  583. (substring str 8 10)
  584. (substring str 4 7)
  585. (format-time-string "%Y" time))))
  586. (defun archive-unixtime (low high)
  587. "Stringify Unix (LOW HIGH) time."
  588. (let ((str (current-time-string (cons high low))))
  589. (substring str 11 19)))
  590. (defun archive-get-lineno ()
  591. (if (>= (point) archive-file-list-start)
  592. (count-lines archive-file-list-start
  593. (line-beginning-position))
  594. 0))
  595. (defun archive-get-descr (&optional noerror)
  596. "Return the descriptor vector for file at point.
  597. Does not signal an error if optional argument NOERROR is non-nil."
  598. (let ((no (archive-get-lineno)))
  599. (if (and (>= (point) archive-file-list-start)
  600. (< no (length archive-files)))
  601. (let ((item (aref archive-files no)))
  602. (if (vectorp item)
  603. item
  604. (if (not noerror)
  605. (error "Entry is not a regular member of the archive"))))
  606. (if (not noerror)
  607. (error "Line does not describe a member of the archive")))))
  608. ;; -------------------------------------------------------------------------
  609. ;;; Section: the mode definition
  610. ;;;###autoload
  611. (defun archive-mode (&optional force)
  612. "Major mode for viewing an archive file in a dired-like way.
  613. You can move around using the usual cursor motion commands.
  614. Letters no longer insert themselves.
  615. Type `e' to pull a file out of the archive and into its own buffer;
  616. or click mouse-2 on the file's line in the archive mode buffer.
  617. If you edit a sub-file of this archive (as with the `e' command) and
  618. save it, the contents of that buffer will be saved back into the
  619. archive.
  620. \\{archive-mode-map}"
  621. ;; This is not interactive because you shouldn't be turning this
  622. ;; mode on and off. You can corrupt things that way.
  623. (if (zerop (buffer-size))
  624. ;; At present we cannot create archives from scratch
  625. (funcall (or (default-value 'major-mode) 'fundamental-mode))
  626. (if (and (not force) archive-files) nil
  627. (kill-all-local-variables)
  628. (let* ((type (archive-find-type))
  629. (typename (capitalize (symbol-name type))))
  630. (make-local-variable 'archive-subtype)
  631. (setq archive-subtype type)
  632. ;; Buffer contains treated image of file before the file contents
  633. (make-local-variable 'revert-buffer-function)
  634. (setq revert-buffer-function 'archive-mode-revert)
  635. (auto-save-mode 0)
  636. (add-hook 'write-contents-functions 'archive-write-file nil t)
  637. (make-local-variable 'require-final-newline)
  638. (setq require-final-newline nil)
  639. (make-local-variable 'local-enable-local-variables)
  640. (setq local-enable-local-variables nil)
  641. ;; Prevent loss of data when saving the file.
  642. (make-local-variable 'file-precious-flag)
  643. (setq file-precious-flag t)
  644. (make-local-variable 'archive-read-only)
  645. ;; Archives which are inside other archives and whose
  646. ;; names are invalid for this OS, can't be written.
  647. (setq archive-read-only
  648. (or (not (file-writable-p (buffer-file-name)))
  649. (and archive-subfile-mode
  650. (string-match file-name-invalid-regexp
  651. (aref archive-subfile-mode 0)))))
  652. ;; Should we use a local copy when accessing from outside Emacs?
  653. (make-local-variable 'archive-local-name)
  654. ;; An archive can contain another archive whose name is invalid
  655. ;; on local filesystem. Treat such archives as remote.
  656. (or archive-remote
  657. (setq archive-remote
  658. (or (string-match archive-remote-regexp (buffer-file-name))
  659. (string-match file-name-invalid-regexp
  660. (buffer-file-name)))))
  661. (setq major-mode 'archive-mode)
  662. (setq mode-name (concat typename "-Archive"))
  663. ;; Run archive-foo-mode-hook and archive-mode-hook
  664. (run-mode-hooks (archive-name "mode-hook") 'archive-mode-hook)
  665. (use-local-map archive-mode-map))
  666. (make-local-variable 'archive-proper-file-start)
  667. (make-local-variable 'archive-file-list-start)
  668. (make-local-variable 'archive-file-list-end)
  669. (make-local-variable 'archive-file-name-indent)
  670. (setq archive-file-name-coding-system
  671. (or file-name-coding-system
  672. default-file-name-coding-system
  673. locale-coding-system))
  674. (if (default-value 'enable-multibyte-characters)
  675. (set-buffer-multibyte 'to))
  676. (archive-summarize nil)
  677. (setq buffer-read-only t)
  678. (when (and archive-visit-single-files
  679. auto-compression-mode
  680. (= (length archive-files) 1))
  681. (rename-buffer (concat " " (buffer-name)))
  682. (archive-extract)))))
  683. ;; Archive mode is suitable only for specially formatted data.
  684. (put 'archive-mode 'mode-class 'special)
  685. (let ((item1 '(archive-subfile-mode " Archive")))
  686. (or (member item1 minor-mode-alist)
  687. (setq minor-mode-alist (cons item1 minor-mode-alist))))
  688. ;; -------------------------------------------------------------------------
  689. (defun archive-find-type ()
  690. (widen)
  691. (goto-char (point-min))
  692. ;; The funny [] here make it unlikely that the .elc file will be treated
  693. ;; as an archive by other software.
  694. (let (case-fold-search)
  695. (cond ((looking-at "\\(PK00\\)?[P]K\003\004") 'zip)
  696. ((looking-at "..-l[hz][0-9ds]-") 'lzh)
  697. ((looking-at "....................[\334]\247\304\375") 'zoo)
  698. ((and (looking-at "\C-z") ; signature too simple, IMHO
  699. (string-match "\\.[aA][rR][cC]\\'"
  700. (or buffer-file-name (buffer-name))))
  701. 'arc)
  702. ;; This pattern modeled on the BSD/GNU+Linux `file' command.
  703. ;; Have seen capital "LHA's", and file has lower case "LHa's" too.
  704. ;; Note this regexp is also in archive-exe-p.
  705. ((looking-at "MZ\\(.\\|\n\\)\\{34\\}LH[aA]'s SFX ") 'lzh-exe)
  706. ((looking-at "Rar!") 'rar)
  707. ((looking-at "!<arch>\n") 'ar)
  708. ((and (looking-at "MZ")
  709. (re-search-forward "Rar!" (+ (point) 100000) t))
  710. 'rar-exe)
  711. ((looking-at "7z\274\257\047\034") '7z)
  712. (t (error "Buffer format not recognized")))))
  713. ;; -------------------------------------------------------------------------
  714. (defun archive-desummarize ()
  715. (let ((inhibit-read-only t)
  716. (modified (buffer-modified-p)))
  717. (widen)
  718. (delete-region (point-min) archive-proper-file-start)
  719. (restore-buffer-modified-p modified)))
  720. (defun archive-summarize (&optional shut-up)
  721. "Parse the contents of the archive file in the current buffer.
  722. Place a dired-like listing on the front;
  723. then narrow to it, so that only that listing
  724. is visible (and the real data of the buffer is hidden).
  725. Optional argument SHUT-UP, if non-nil, means don't print messages
  726. when parsing the archive."
  727. (widen)
  728. (let ((buffer-file-truename nil) ; avoid changing dir mtime by lock_file
  729. (inhibit-read-only t))
  730. (setq archive-proper-file-start (copy-marker (point-min) t))
  731. (set (make-local-variable 'change-major-mode-hook) 'archive-desummarize)
  732. (or shut-up
  733. (message "Parsing archive file..."))
  734. (buffer-disable-undo (current-buffer))
  735. (setq archive-files (funcall (archive-name "summarize")))
  736. (or shut-up
  737. (message "Parsing archive file...done."))
  738. (setq archive-proper-file-start (point-marker))
  739. (narrow-to-region (point-min) (point))
  740. (set-buffer-modified-p nil)
  741. (buffer-enable-undo))
  742. (goto-char archive-file-list-start)
  743. (archive-next-line 0))
  744. (defun archive-resummarize ()
  745. "Recreate the contents listing of an archive."
  746. (let ((no (archive-get-lineno)))
  747. (archive-desummarize)
  748. (archive-summarize t)
  749. (goto-char archive-file-list-start)
  750. (archive-next-line no)))
  751. (defun archive-summarize-files (files)
  752. "Insert a description of a list of files annotated with proper mouse face."
  753. (setq archive-file-list-start (point-marker))
  754. (setq archive-file-name-indent (if files (aref (car files) 1) 0))
  755. ;; We don't want to do an insert for each element since that takes too
  756. ;; long when the archive -- which has to be moved in memory -- is large.
  757. (insert
  758. (apply
  759. #'concat
  760. (mapcar
  761. (lambda (fil)
  762. ;; Using `concat' here copies the text also, so we can add
  763. ;; properties without problems.
  764. (let ((text (concat (aref fil 0) "\n")))
  765. (if (featurep 'xemacs)
  766. () ; out of luck
  767. (add-text-properties
  768. (aref fil 1) (aref fil 2)
  769. '(mouse-face highlight
  770. help-echo "mouse-2: extract this file into a buffer")
  771. text))
  772. text))
  773. files)))
  774. (setq archive-file-list-end (point-marker)))
  775. (defun archive-alternate-display ()
  776. "Toggle alternative display.
  777. To avoid very long lines archive mode does not show all information.
  778. This function changes the set of information shown for each files."
  779. (interactive)
  780. (setq archive-alternate-display (not archive-alternate-display))
  781. (archive-resummarize))
  782. ;; -------------------------------------------------------------------------
  783. ;;; Section: Local archive copy handling
  784. (defun archive-unique-fname (fname dir)
  785. "Make sure a file FNAME can be created uniquely in directory DIR.
  786. If FNAME can be uniquely created in DIR, it is returned unaltered.
  787. If FNAME is something our underlying filesystem can't grok, or if another
  788. file by that name already exists in DIR, a unique new name is generated
  789. using `make-temp-file', and the generated name is returned."
  790. (let ((fullname (expand-file-name fname dir))
  791. (alien (string-match file-name-invalid-regexp fname))
  792. (tmpfile
  793. (expand-file-name
  794. (if (if (fboundp 'msdos-long-file-names)
  795. (not (msdos-long-file-names)))
  796. "am"
  797. "arc-mode.")
  798. dir)))
  799. (if (or alien (file-exists-p fullname))
  800. (progn
  801. ;; Make sure all the leading directories in
  802. ;; archive-local-name exist under archive-tmpdir, so that
  803. ;; the directory structure recorded in the archive is
  804. ;; reconstructed in the temporary directory.
  805. (make-directory (file-name-directory tmpfile) t)
  806. (make-temp-file tmpfile))
  807. ;; Make sure all the leading directories in `fullname' exist
  808. ;; under archive-tmpdir. This is necessary for nested archives
  809. ;; (`archive-extract' sets `archive-remote' to t in case
  810. ;; an archive occurs inside another archive).
  811. (make-directory (file-name-directory fullname) t)
  812. fullname)))
  813. (defun archive-maybe-copy (archive)
  814. (let ((coding-system-for-write 'no-conversion))
  815. (if archive-remote
  816. (let ((start (point-max))
  817. ;; Sometimes ARCHIVE is invalid while its actual name, as
  818. ;; recorded in its parent archive, is not. For example, an
  819. ;; archive bar.zip inside another archive foo.zip gets a name
  820. ;; "foo.zip:bar.zip", which is invalid on DOS/Windows.
  821. ;; So use the actual name if available.
  822. (archive-name
  823. (or (and archive-subfile-mode (aref archive-subfile-mode 0))
  824. archive)))
  825. (setq archive-local-name
  826. (archive-unique-fname archive-name archive-tmpdir))
  827. (save-restriction
  828. (widen)
  829. (write-region start (point-max) archive-local-name nil 'nomessage))
  830. archive-local-name)
  831. (if (buffer-modified-p) (save-buffer))
  832. archive)))
  833. (defun archive-maybe-update (unchanged)
  834. (if archive-remote
  835. (let ((name archive-local-name)
  836. (modified (buffer-modified-p))
  837. (coding-system-for-read 'no-conversion)
  838. (lno (archive-get-lineno))
  839. (inhibit-read-only t))
  840. (if unchanged nil
  841. (setq archive-files nil)
  842. (erase-buffer)
  843. (insert-file-contents name)
  844. (archive-mode t)
  845. (goto-char archive-file-list-start)
  846. (archive-next-line lno))
  847. (archive-delete-local name)
  848. (if (not unchanged)
  849. (message
  850. "Buffer `%s' must be saved for changes to take effect"
  851. (buffer-name (current-buffer))))
  852. (set-buffer-modified-p (or modified (not unchanged))))))
  853. (defun archive-delete-local (name)
  854. "Delete file NAME and its parents up to and including `archive-tmpdir'."
  855. (let ((again t)
  856. (top (directory-file-name (file-name-as-directory archive-tmpdir))))
  857. (condition-case nil
  858. (delete-file name)
  859. (error nil))
  860. (while again
  861. (setq name (directory-file-name (file-name-directory name)))
  862. (condition-case nil
  863. (delete-directory name)
  864. (error nil))
  865. (if (string= name top) (setq again nil)))))
  866. ;; -------------------------------------------------------------------------
  867. ;;; Section: Member extraction
  868. (defun archive-try-jka-compr ()
  869. (when (and auto-compression-mode
  870. (jka-compr-get-compression-info buffer-file-name))
  871. (let* ((basename (file-name-nondirectory buffer-file-name))
  872. (tmpname (if (string-match ":\\([^:]+\\)\\'" basename)
  873. (match-string 1 basename) basename))
  874. (tmpfile (make-temp-file (file-name-sans-extension tmpname)
  875. nil
  876. (file-name-extension tmpname 'period))))
  877. (unwind-protect
  878. (progn
  879. (let ((coding-system-for-write 'no-conversion)
  880. ;; Don't re-compress this data just before decompressing it.
  881. (jka-compr-inhibit t))
  882. (write-region (point-min) (point-max) tmpfile nil 'quiet))
  883. (erase-buffer)
  884. (let ((coding-system-for-read 'no-conversion))
  885. (insert-file-contents tmpfile)))
  886. (delete-file tmpfile)))))
  887. (defun archive-file-name-handler (op &rest args)
  888. (or (eq op 'file-exists-p)
  889. (let ((file-name-handler-alist nil))
  890. (apply op args))))
  891. (defun archive-set-buffer-as-visiting-file (filename)
  892. "Set the current buffer as if it were visiting FILENAME."
  893. (save-excursion
  894. (goto-char (point-min))
  895. (let ((buffer-undo-list t)
  896. (coding
  897. (or coding-system-for-read
  898. (and set-auto-coding-function
  899. (save-excursion
  900. (funcall set-auto-coding-function
  901. filename (- (point-max) (point-min)))))
  902. ;; The following let-binding of file-name-handler-alist forces
  903. ;; find-file-not-found-set-buffer-file-coding-system to ignore
  904. ;; the file's name (see dos-w32.el).
  905. (let ((file-name-handler-alist
  906. '(("" . archive-file-name-handler))))
  907. (car (find-operation-coding-system
  908. 'insert-file-contents
  909. (cons filename (current-buffer)) t))))))
  910. (unless (or coding-system-for-read
  911. enable-multibyte-characters)
  912. (setq coding
  913. (coding-system-change-text-conversion coding 'raw-text)))
  914. (unless (memq coding '(nil no-conversion))
  915. (decode-coding-region (point-min) (point-max) coding)
  916. (setq last-coding-system-used coding))
  917. (set-buffer-modified-p nil)
  918. (kill-local-variable 'buffer-file-coding-system)
  919. (after-insert-file-set-coding (- (point-max) (point-min))))))
  920. (define-obsolete-function-alias 'archive-mouse-extract 'archive-extract "22.1")
  921. (defun archive-extract (&optional other-window-p event)
  922. "In archive mode, extract this entry of the archive into its own buffer."
  923. (interactive (list nil last-input-event))
  924. (if event (posn-set-point (event-end event)))
  925. (let* ((view-p (eq other-window-p 'view))
  926. (descr (archive-get-descr))
  927. (ename (aref descr 0))
  928. (iname (aref descr 1))
  929. (archive-buffer (current-buffer))
  930. (arcdir default-directory)
  931. (archive (buffer-file-name))
  932. (arcname (file-name-nondirectory archive))
  933. (bufname (concat (file-name-nondirectory iname) " (" arcname ")"))
  934. (extractor (archive-name "extract"))
  935. ;; Members with file names which aren't valid for the
  936. ;; underlying filesystem, are treated as read-only.
  937. (read-only-p (or archive-read-only
  938. view-p
  939. (string-match file-name-invalid-regexp ename)))
  940. (arcfilename (expand-file-name (concat arcname ":" iname)))
  941. (buffer (get-buffer bufname))
  942. (just-created nil)
  943. (file-name-coding archive-file-name-coding-system))
  944. (if (and buffer
  945. (string= (buffer-file-name buffer) arcfilename))
  946. nil
  947. (setq archive (archive-maybe-copy archive))
  948. (setq bufname (generate-new-buffer-name bufname))
  949. (setq buffer (get-buffer-create bufname))
  950. (setq just-created t)
  951. (with-current-buffer buffer
  952. (setq buffer-file-name arcfilename)
  953. (setq buffer-file-truename
  954. (abbreviate-file-name buffer-file-name))
  955. ;; Set the default-directory to the dir of the superior buffer.
  956. (setq default-directory arcdir)
  957. (make-local-variable 'archive-superior-buffer)
  958. (setq archive-superior-buffer archive-buffer)
  959. (add-hook 'write-file-functions #'archive-write-file-member nil t)
  960. (setq archive-subfile-mode descr)
  961. (setq archive-file-name-coding-system file-name-coding)
  962. (if (and
  963. (null
  964. (let (;; We may have to encode the file name argument for
  965. ;; external programs.
  966. (coding-system-for-write
  967. (and enable-multibyte-characters
  968. archive-file-name-coding-system))
  969. ;; We read an archive member by no-conversion at
  970. ;; first, then decode appropriately by calling
  971. ;; archive-set-buffer-as-visiting-file later.
  972. (coding-system-for-read 'no-conversion))
  973. (condition-case err
  974. (if (fboundp extractor)
  975. (funcall extractor archive ename)
  976. (archive-*-extract archive ename
  977. (symbol-value extractor)))
  978. (error
  979. (ding (message "%s" (error-message-string err)))
  980. nil))))
  981. just-created)
  982. (progn
  983. (set-buffer-modified-p nil)
  984. (kill-buffer buffer))
  985. (archive-try-jka-compr) ;Pretty ugly hack :-(
  986. (archive-set-buffer-as-visiting-file ename)
  987. (goto-char (point-min))
  988. (rename-buffer bufname)
  989. (setq buffer-read-only read-only-p)
  990. (setq buffer-undo-list nil)
  991. (set-buffer-modified-p nil)
  992. (setq buffer-saved-size (buffer-size))
  993. (normal-mode)
  994. ;; Just in case an archive occurs inside another archive.
  995. (when (derived-mode-p 'archive-mode)
  996. (setq archive-remote t)
  997. (if read-only-p (setq archive-read-only t))
  998. ;; We will write out the archive ourselves if it is
  999. ;; part of another archive.
  1000. (remove-hook 'write-contents-functions #'archive-write-file t))
  1001. (run-hooks 'archive-extract-hook)
  1002. (if archive-read-only
  1003. (message "Note: altering this archive is not implemented."))))
  1004. (archive-maybe-update t))
  1005. (or (not (buffer-name buffer))
  1006. (cond
  1007. (view-p
  1008. (view-buffer buffer (and just-created 'kill-buffer-if-not-modified)))
  1009. ((eq other-window-p 'display) (display-buffer buffer))
  1010. (other-window-p (switch-to-buffer-other-window buffer))
  1011. (t (switch-to-buffer buffer))))))
  1012. (defun archive-*-extract (archive name command)
  1013. (let* ((default-directory (file-name-as-directory archive-tmpdir))
  1014. (tmpfile (expand-file-name (file-name-nondirectory name)
  1015. default-directory))
  1016. exit-status success)
  1017. (make-directory (directory-file-name default-directory) t)
  1018. (setq exit-status
  1019. (apply #'call-process
  1020. (car command)
  1021. nil
  1022. nil
  1023. nil
  1024. (append (cdr command) (list archive name))))
  1025. (cond ((and (numberp exit-status) (zerop exit-status))
  1026. (if (not (file-exists-p tmpfile))
  1027. (ding (message "`%s': no such file or directory" tmpfile))
  1028. (insert-file-contents tmpfile)
  1029. (setq success t)))
  1030. ((numberp exit-status)
  1031. (ding
  1032. (message "`%s' exited with status %d" (car command) exit-status)))
  1033. ((stringp exit-status)
  1034. (ding (message "`%s' aborted: %s" (car command) exit-status)))
  1035. (t
  1036. (ding (message "`%s' failed" (car command)))))
  1037. (archive-delete-local tmpfile)
  1038. success))
  1039. (defun archive-extract-by-stdout (archive name command &optional stderr-test)
  1040. (let ((stderr-file (make-temp-file "arc-stderr")))
  1041. (unwind-protect
  1042. (prog1
  1043. (apply #'call-process
  1044. (car command)
  1045. nil
  1046. (if stderr-file (list t stderr-file) t)
  1047. nil
  1048. (append (cdr command) (list archive name)))
  1049. (with-temp-buffer
  1050. (insert-file-contents stderr-file)
  1051. (goto-char (point-min))
  1052. (when (if (stringp stderr-test)
  1053. (not (re-search-forward stderr-test nil t))
  1054. (> (buffer-size) 0))
  1055. (message "%s" (buffer-string)))))
  1056. (if (file-exists-p stderr-file)
  1057. (delete-file stderr-file)))))
  1058. (defun archive-extract-by-file (archive name command &optional stdout-test)
  1059. (let ((dest (make-temp-file "arc-dir" 'dir))
  1060. (stdout-file (make-temp-file "arc-stdout")))
  1061. (unwind-protect
  1062. (prog1
  1063. (apply #'call-process
  1064. (car command)
  1065. nil
  1066. `(:file ,stdout-file)
  1067. nil
  1068. `(,archive ,name ,@(cdr command) ,dest))
  1069. (with-temp-buffer
  1070. (insert-file-contents stdout-file)
  1071. (goto-char (point-min))
  1072. (when (if (stringp stdout-test)
  1073. (not (re-search-forward stdout-test nil t))
  1074. (> (buffer-size) 0))
  1075. (message "%s" (buffer-string))))
  1076. (if (file-exists-p (expand-file-name name dest))
  1077. (insert-file-contents-literally (expand-file-name name dest))))
  1078. (if (file-exists-p stdout-file)
  1079. (delete-file stdout-file))
  1080. (if (file-exists-p (expand-file-name name dest))
  1081. (delete-file (expand-file-name name dest)))
  1082. (while (file-name-directory name)
  1083. (setq name (directory-file-name (file-name-directory name)))
  1084. (when (file-directory-p (expand-file-name name dest))
  1085. (delete-directory (expand-file-name name dest))))
  1086. (when (file-directory-p dest)
  1087. (delete-directory dest)))))
  1088. (defun archive-extract-other-window ()
  1089. "In archive mode, find this member in another window."
  1090. (interactive)
  1091. (archive-extract t))
  1092. (defun archive-display-other-window ()
  1093. "In archive mode, display this member in another window."
  1094. (interactive)
  1095. (archive-extract 'display))
  1096. (defun archive-view ()
  1097. "In archive mode, view the member on this line."
  1098. (interactive)
  1099. (archive-extract 'view))
  1100. (defun archive-add-new-member (arcbuf name)
  1101. "Add current buffer to the archive in ARCBUF naming it NAME."
  1102. (interactive
  1103. (list (get-buffer
  1104. (read-buffer "Buffer containing archive: "
  1105. ;; Find first archive buffer and suggest that
  1106. (let ((bufs (buffer-list)))
  1107. (while (and bufs
  1108. (not (with-current-buffer (car bufs)
  1109. (derived-mode-p 'archive-mode))))
  1110. (setq bufs (cdr bufs)))
  1111. (if bufs
  1112. (car bufs)
  1113. (error "There are no archive buffers")))
  1114. t))
  1115. (read-string "File name in archive: "
  1116. (if buffer-file-name
  1117. (file-name-nondirectory buffer-file-name)
  1118. ""))))
  1119. (with-current-buffer arcbuf
  1120. (or (derived-mode-p 'archive-mode)
  1121. (error "Buffer is not an archive buffer"))
  1122. (if archive-read-only
  1123. (error "Archive is read-only")))
  1124. (if (eq arcbuf (current-buffer))
  1125. (error "An archive buffer cannot be added to itself"))
  1126. (if (string= name "")
  1127. (error "Archive members may not be given empty names"))
  1128. (let ((func (with-current-buffer arcbuf
  1129. (archive-name "add-new-member")))
  1130. (membuf (current-buffer)))
  1131. (if (fboundp func)
  1132. (with-current-buffer arcbuf
  1133. (funcall func buffer-file-name membuf name))
  1134. (error "Adding a new member is not supported for this archive type"))))
  1135. ;; -------------------------------------------------------------------------
  1136. ;;; Section: IO stuff
  1137. (defun archive-write-file-member ()
  1138. (save-excursion
  1139. (save-restriction
  1140. (message "Updating archive...")
  1141. (widen)
  1142. (let ((writer (with-current-buffer archive-superior-buffer
  1143. (archive-name "write-file-member")))
  1144. (archive (with-current-buffer archive-superior-buffer
  1145. (archive-maybe-copy (buffer-file-name)))))
  1146. (if (fboundp writer)
  1147. (funcall writer archive archive-subfile-mode)
  1148. (archive-*-write-file-member archive
  1149. archive-subfile-mode
  1150. (symbol-value writer)))
  1151. (set-buffer-modified-p nil)
  1152. (message "Updating archive...done"))
  1153. (set-buffer archive-superior-buffer)
  1154. (if (not archive-remote) (revert-buffer) (archive-maybe-update nil))))
  1155. ;; Restore the value of last-coding-system-used, so that basic-save-buffer
  1156. ;; won't reset the coding-system of this archive member.
  1157. (if (local-variable-p 'archive-member-coding-system)
  1158. (setq last-coding-system-used archive-member-coding-system))
  1159. t)
  1160. (defun archive-*-write-file-member (archive descr command)
  1161. (let* ((ename (aref descr 0))
  1162. (tmpfile (expand-file-name ename archive-tmpdir))
  1163. (top (directory-file-name (file-name-as-directory archive-tmpdir)))
  1164. (default-directory (file-name-as-directory top)))
  1165. (unwind-protect
  1166. (progn
  1167. (make-directory (file-name-directory tmpfile) t)
  1168. ;; If the member is itself an archive, write it without
  1169. ;; the dired-like listing we created.
  1170. (if (eq major-mode 'archive-mode)
  1171. (archive-write-file tmpfile)
  1172. (write-region nil nil tmpfile nil 'nomessage))
  1173. ;; basic-save-buffer needs last-coding-system-used to have
  1174. ;; the value used to write the file, so save it before any
  1175. ;; further processing clobbers it (we restore it in
  1176. ;; archive-write-file-member, above).
  1177. (setq archive-member-coding-system last-coding-system-used)
  1178. (if (aref descr 3)
  1179. ;; Set the file modes, but make sure we can read it.
  1180. (set-file-modes tmpfile (logior ?\400 (aref descr 3))))
  1181. (setq ename
  1182. (encode-coding-string ename archive-file-name-coding-system))
  1183. (let* ((coding-system-for-write 'no-conversion)
  1184. (exitcode (apply #'call-process
  1185. (car command)
  1186. nil
  1187. nil
  1188. nil
  1189. (append (cdr command)
  1190. (list archive ename)))))
  1191. (or (zerop exitcode)
  1192. (error "Updating was unsuccessful (%S)" exitcode))))
  1193. (archive-delete-local tmpfile))))
  1194. (defun archive-write-file (&optional file)
  1195. (save-excursion
  1196. (let ((coding-system-for-write 'no-conversion))
  1197. (write-region archive-proper-file-start (point-max)
  1198. (or file buffer-file-name) nil t)
  1199. (set-buffer-modified-p nil))
  1200. t))
  1201. ;; -------------------------------------------------------------------------
  1202. ;;; Section: Marking and unmarking.
  1203. (defun archive-flag-deleted (p &optional type)
  1204. "In archive mode, mark this member to be deleted from the archive.
  1205. With a prefix argument, mark that many files."
  1206. (interactive "p")
  1207. (or type (setq type ?D))
  1208. (beginning-of-line)
  1209. (let ((sign (if (>= p 0) +1 -1))
  1210. (modified (buffer-modified-p))
  1211. (inhibit-read-only t))
  1212. (while (not (zerop p))
  1213. (if (archive-get-descr t)
  1214. (progn
  1215. (delete-char 1)
  1216. (insert type)))
  1217. (forward-line sign)
  1218. (setq p (- p sign)))
  1219. (restore-buffer-modified-p modified))
  1220. (archive-next-line 0))
  1221. (defun archive-unflag (p)
  1222. "In archive mode, un-mark this member if it is marked to be deleted.
  1223. With a prefix argument, un-mark that many files forward."
  1224. (interactive "p")
  1225. (archive-flag-deleted p ?\s))
  1226. (defun archive-unflag-backwards (p)
  1227. "In archive mode, un-mark this member if it is marked to be deleted.
  1228. With a prefix argument, un-mark that many members backward."
  1229. (interactive "p")
  1230. (archive-flag-deleted (- p) ?\s))
  1231. (defun archive-unmark-all-files ()
  1232. "Remove all marks."
  1233. (interactive)
  1234. (let ((modified (buffer-modified-p))
  1235. (inhibit-read-only t))
  1236. (save-excursion
  1237. (goto-char archive-file-list-start)
  1238. (while (< (point) archive-file-list-end)
  1239. (or (= (following-char) ?\s)
  1240. (progn (delete-char 1) (insert ?\s)))
  1241. (forward-line 1)))
  1242. (restore-buffer-modified-p modified)))
  1243. (defun archive-mark (p)
  1244. "In archive mode, mark this member for group operations.
  1245. With a prefix argument, mark that many members.
  1246. Use \\[archive-unmark-all-files] to remove all marks."
  1247. (interactive "p")
  1248. (archive-flag-deleted p ?*))
  1249. (defun archive-get-marked (mark &optional default)
  1250. (let (files)
  1251. (save-excursion
  1252. (goto-char archive-file-list-start)
  1253. (while (< (point) archive-file-list-end)
  1254. (if (= (following-char) mark)
  1255. (setq files (cons (archive-get-descr) files)))
  1256. (forward-line 1)))
  1257. (or (nreverse files)
  1258. (and default
  1259. (list (archive-get-descr))))))
  1260. ;; -------------------------------------------------------------------------
  1261. ;;; Section: Operate
  1262. (defun archive-next-line (p)
  1263. (interactive "p")
  1264. (forward-line p)
  1265. (or (eobp)
  1266. (forward-char archive-file-name-indent)))
  1267. (defun archive-previous-line (p)
  1268. (interactive "p")
  1269. (archive-next-line (- p)))
  1270. (defun archive-chmod-entry (new-mode)
  1271. "Change the protection bits associated with all marked or this member.
  1272. The new protection bits can either be specified as an octal number or
  1273. as a relative change like \"g+rw\" as for chmod(2)."
  1274. (interactive "sNew mode (octal or relative): ")
  1275. (if archive-read-only (error "Archive is read-only"))
  1276. (let ((func (archive-name "chmod-entry")))
  1277. (if (fboundp func)
  1278. (progn
  1279. (funcall func new-mode (archive-get-marked ?* t))
  1280. (archive-resummarize))
  1281. (error "Setting mode bits is not supported for this archive type"))))
  1282. (defun archive-chown-entry (new-uid)
  1283. "Change the owner of all marked or this member."
  1284. (interactive "nNew uid: ")
  1285. (if archive-read-only (error "Archive is read-only"))
  1286. (let ((func (archive-name "chown-entry")))
  1287. (if (fboundp func)
  1288. (progn
  1289. (funcall func new-uid (archive-get-marked ?* t))
  1290. (archive-resummarize))
  1291. (error "Setting owner is not supported for this archive type"))))
  1292. (defun archive-chgrp-entry (new-gid)
  1293. "Change the group of all marked or this member."
  1294. (interactive "nNew gid: ")
  1295. (if archive-read-only (error "Archive is read-only"))
  1296. (let ((func (archive-name "chgrp-entry")))
  1297. (if (fboundp func)
  1298. (progn
  1299. (funcall func new-gid (archive-get-marked ?* t))
  1300. (archive-resummarize))
  1301. (error "Setting group is not supported for this archive type"))))
  1302. (defun archive-expunge ()
  1303. "Do the flagged deletions."
  1304. (interactive)
  1305. (let (files)
  1306. (save-excursion
  1307. (goto-char archive-file-list-start)
  1308. (while (< (point) archive-file-list-end)
  1309. (if (= (following-char) ?D)
  1310. (setq files (cons (aref (archive-get-descr) 0) files)))
  1311. (forward-line 1)))
  1312. (setq files (nreverse files))
  1313. (and files
  1314. (or (not archive-read-only)
  1315. (error "Archive is read-only"))
  1316. (or (yes-or-no-p (format "Really delete %d member%s? "
  1317. (length files)
  1318. (if (null (cdr files)) "" "s")))
  1319. (error "Operation aborted"))
  1320. (let ((archive (archive-maybe-copy (buffer-file-name)))
  1321. (expunger (archive-name "expunge")))
  1322. (if (fboundp expunger)
  1323. (funcall expunger archive files)
  1324. (archive-*-expunge archive files (symbol-value expunger)))
  1325. (archive-maybe-update nil)
  1326. (if archive-remote
  1327. (archive-resummarize)
  1328. (revert-buffer))))))
  1329. (defun archive-*-expunge (archive files command)
  1330. (apply #'call-process
  1331. (car command)
  1332. nil
  1333. nil
  1334. nil
  1335. (append (cdr command) (cons archive files))))
  1336. (defun archive-rename-entry (newname)
  1337. "Change the name associated with this entry in the archive file."
  1338. (interactive "sNew name: ")
  1339. (if archive-read-only (error "Archive is read-only"))
  1340. (if (string= newname "")
  1341. (error "Archive members may not be given empty names"))
  1342. (let ((func (archive-name "rename-entry"))
  1343. (descr (archive-get-descr)))
  1344. (if (fboundp func)
  1345. (progn
  1346. (funcall func
  1347. (encode-coding-string newname
  1348. archive-file-name-coding-system)
  1349. descr)
  1350. (archive-resummarize))
  1351. (error "Renaming is not supported for this archive type"))))
  1352. ;; Revert the buffer and recompute the dired-like listing.
  1353. (defun archive-mode-revert (&optional _no-auto-save _no-confirm)
  1354. (let ((no (archive-get-lineno)))
  1355. (setq archive-files nil)
  1356. (let ((revert-buffer-function nil)
  1357. (coding-system-for-read 'no-conversion))
  1358. (revert-buffer t t))
  1359. (archive-mode)
  1360. (goto-char archive-file-list-start)
  1361. (archive-next-line no)))
  1362. (defun archive-undo ()
  1363. "Undo in an archive buffer.
  1364. This doesn't recover lost files, it just undoes changes in the buffer itself."
  1365. (interactive)
  1366. (let ((inhibit-read-only t))
  1367. (undo)))
  1368. ;; -------------------------------------------------------------------------
  1369. ;;; Section: Arc Archives
  1370. (defun archive-arc-summarize ()
  1371. (let ((p 1)
  1372. (totalsize 0)
  1373. (maxlen 8)
  1374. files
  1375. visual)
  1376. (while (and (< (+ p 29) (point-max))
  1377. (= (byte-after p) ?\C-z)
  1378. (> (byte-after (1+ p)) 0))
  1379. (let* ((namefld (buffer-substring (+ p 2) (+ p 2 13)))
  1380. (fnlen (or (string-match "\0" namefld) 13))
  1381. (efnname (decode-coding-string (substring namefld 0 fnlen)
  1382. archive-file-name-coding-system))
  1383. ;; Convert to float to avoid overflow for very large files.
  1384. (csize (archive-l-e (+ p 15) 4 'float))
  1385. (moddate (archive-l-e (+ p 19) 2))
  1386. (modtime (archive-l-e (+ p 21) 2))
  1387. (ucsize (archive-l-e (+ p 25) 4 'float))
  1388. (fiddle (string= efnname (upcase efnname)))
  1389. (ifnname (if fiddle (downcase efnname) efnname))
  1390. (text (format " %8.0f %-11s %-8s %s"
  1391. ucsize
  1392. (archive-dosdate moddate)
  1393. (archive-dostime modtime)
  1394. ifnname)))
  1395. (setq maxlen (max maxlen fnlen)
  1396. totalsize (+ totalsize ucsize)
  1397. visual (cons (vector text
  1398. (- (length text) (length ifnname))
  1399. (length text))
  1400. visual)
  1401. files (cons (vector efnname ifnname fiddle nil (1- p))
  1402. files)
  1403. ;; p needs to stay an integer, since we use it in char-after
  1404. ;; above. Passing through `round' limits the compressed size
  1405. ;; to most-positive-fixnum, but if the compressed size exceeds
  1406. ;; that, we cannot visit the archive anyway.
  1407. p (+ p 29 (round csize)))))
  1408. (goto-char (point-min))
  1409. (let ((dash (concat "- -------- ----------- -------- "
  1410. (make-string maxlen ?-)
  1411. "\n")))
  1412. (insert "M Length Date Time File\n"
  1413. dash)
  1414. (archive-summarize-files (nreverse visual))
  1415. (insert dash
  1416. (format " %8.0f %d file%s"
  1417. totalsize
  1418. (length files)
  1419. (if (= 1 (length files)) "" "s"))
  1420. "\n"))
  1421. (apply #'vector (nreverse files))))
  1422. (defun archive-arc-rename-entry (newname descr)
  1423. (if (string-match "[:\\\\/]" newname)
  1424. (error "File names in arc files must not contain a directory component"))
  1425. (if (> (length newname) 12)
  1426. (error "File names in arc files are limited to 12 characters"))
  1427. (let ((name (concat newname (substring "\0\0\0\0\0\0\0\0\0\0\0\0\0"
  1428. (length newname))))
  1429. (inhibit-read-only t))
  1430. (save-restriction
  1431. (save-excursion
  1432. (widen)
  1433. (goto-char (+ archive-proper-file-start (aref descr 4) 2))
  1434. (delete-char 13)
  1435. (insert-unibyte name)))))
  1436. ;; -------------------------------------------------------------------------
  1437. ;;; Section: Lzh Archives
  1438. (defun archive-lzh-summarize (&optional start)
  1439. (let ((p (or start 1)) ;; 1 for .lzh, something further on for .exe
  1440. (totalsize 0)
  1441. (maxlen 8)
  1442. files
  1443. visual)
  1444. (while (progn (goto-char p) ;beginning of a base header.
  1445. (looking-at "\\(.\\|\n\\)\\(.\\|\n\\)-l[hz][0-9ds]-"))
  1446. (let* ((hsize (byte-after p)) ;size of the base header (level 0 and 1)
  1447. ;; Convert to float to avoid overflow for very large files.
  1448. (csize (archive-l-e (+ p 7) 4 'float)) ;size of a compressed file to follow (level 0 and 2),
  1449. ;size of extended headers + the compressed file to follow (level 1).
  1450. (ucsize (archive-l-e (+ p 11) 4 'float)) ;size of an uncompressed file.
  1451. (time1 (archive-l-e (+ p 15) 2)) ;date/time (MSDOS format in level 0, 1 headers
  1452. (time2 (archive-l-e (+ p 17) 2)) ;and UNIX format in level 2 header.)
  1453. (hdrlvl (byte-after (+ p 20))) ;header level
  1454. thsize ;total header size (base + extensions)
  1455. fnlen efnname osid fiddle ifnname width p2
  1456. neh ;beginning of next extension header (level 1 and 2)
  1457. mode modestr uid gid text dir prname
  1458. gname uname modtime moddate)
  1459. (if (= hdrlvl 3) (error "can't handle lzh level 3 header type"))
  1460. (when (or (= hdrlvl 0) (= hdrlvl 1))
  1461. (setq fnlen (byte-after (+ p 21))) ;filename length
  1462. (setq efnname (let ((str (buffer-substring (+ p 22) (+ p 22 fnlen)))) ;filename from offset 22
  1463. (decode-coding-string
  1464. str archive-file-name-coding-system)))
  1465. (setq p2 (+ p 22 fnlen))) ;
  1466. (if (= hdrlvl 1)
  1467. (setq neh (+ p2 3)) ;specific to level 1 header
  1468. (if (= hdrlvl 2)
  1469. (setq neh (+ p 24)))) ;specific to level 2 header
  1470. (if neh ;if level 1 or 2 we expect extension headers to follow
  1471. (let* ((ehsize (archive-l-e neh 2)) ;size of the extension header
  1472. (etype (byte-after (+ neh 2)))) ;extension type
  1473. (while (not (= ehsize 0))
  1474. (cond
  1475. ((= etype 1) ;file name
  1476. (let ((i (+ neh 3)))
  1477. (while (< i (+ neh ehsize))
  1478. (setq efnname (concat efnname (char-to-string (byte-after i))))
  1479. (setq i (1+ i)))))
  1480. ((= etype 2) ;directory name
  1481. (let ((i (+ neh 3)))
  1482. (while (< i (+ neh ehsize))
  1483. (setq dir (concat dir
  1484. (if (= (byte-after i)
  1485. 255)
  1486. "/"
  1487. (char-to-string
  1488. (char-after i)))))
  1489. (setq i (1+ i)))))
  1490. ((= etype 80) ;Unix file permission
  1491. (setq mode (archive-l-e (+ neh 3) 2)))
  1492. ((= etype 81) ;UNIX file group/user ID
  1493. (progn (setq uid (archive-l-e (+ neh 3) 2))
  1494. (setq gid (archive-l-e (+ neh 5) 2))))
  1495. ((= etype 82) ;UNIX file group name
  1496. (let ((i (+ neh 3)))
  1497. (while (< i (+ neh ehsize))
  1498. (setq gname (concat gname (char-to-string (char-after i))))
  1499. (setq i (1+ i)))))
  1500. ((= etype 83) ;UNIX file user name
  1501. (let ((i (+ neh 3)))
  1502. (while (< i (+ neh ehsize))
  1503. (setq uname (concat uname (char-to-string (char-after i))))
  1504. (setq i (1+ i)))))
  1505. )
  1506. (setq neh (+ neh ehsize))
  1507. (setq ehsize (archive-l-e neh 2))
  1508. (setq etype (byte-after (+ neh 2))))
  1509. ;;get total header size for level 1 and 2 headers
  1510. (setq thsize (- neh p))))
  1511. (if (= hdrlvl 0) ;total header size
  1512. (setq thsize hsize))
  1513. ;; OS ID field not present in level 0 header, use code 0 "generic"
  1514. ;; in that case as per lha program header.c get_header()
  1515. (setq osid (cond ((= hdrlvl 0) 0)
  1516. ((= hdrlvl 1) (char-after (+ p 22 fnlen 2)))
  1517. ((= hdrlvl 2) (char-after (+ p 23)))))
  1518. ;; Filename fiddling must follow the lha program, otherwise the name
  1519. ;; passed to "lha pq" etc won't match (which for an extract silently
  1520. ;; results in no output). As of version 1.14i it goes from the OS ID,
  1521. ;; - For 'M' MSDOS: msdos_to_unix_filename() downcases always, and
  1522. ;; converts "\" to "/".
  1523. ;; - For 0 generic: generic_to_unix_filename() downcases if there's
  1524. ;; no lower case already present, and converts "\" to "/".
  1525. ;; - For 'm' macOS: macos_to_unix_filename() changes "/" to ":" and
  1526. ;; ":" to "/"
  1527. (setq fiddle (cond ((= ?M osid) t)
  1528. ((= 0 osid) (string= efnname (upcase efnname)))))
  1529. (setq ifnname (if fiddle (downcase efnname) efnname))
  1530. (setq prname (if dir (concat dir ifnname) ifnname))
  1531. (setq width (if prname (string-width prname) 0))
  1532. (setq modestr (if mode (archive-int-to-mode mode) "??????????"))
  1533. (setq moddate (if (= hdrlvl 2)
  1534. (archive-unixdate time1 time2) ;level 2 header in UNIX format
  1535. (archive-dosdate time2))) ;level 0 and 1 header in DOS format
  1536. (setq modtime (if (= hdrlvl 2)
  1537. (archive-unixtime time1 time2)
  1538. (archive-dostime time1)))
  1539. (setq text (if archive-alternate-display
  1540. (format " %8.0f %5S %5S %s"
  1541. ucsize
  1542. (or uid "?")
  1543. (or gid "?")
  1544. ifnname)
  1545. (format " %10s %8.0f %-11s %-8s %s"
  1546. modestr
  1547. ucsize
  1548. moddate
  1549. modtime
  1550. prname)))
  1551. (setq maxlen (max maxlen width)
  1552. totalsize (+ totalsize ucsize)
  1553. visual (cons (vector text
  1554. (- (length text) (length prname))
  1555. (length text))
  1556. visual)
  1557. files (cons (vector prname ifnname fiddle mode (1- p))
  1558. files))
  1559. (cond ((= hdrlvl 1)
  1560. ;; p needs to stay an integer, since we use it in goto-char
  1561. ;; above. Passing through `round' limits the compressed size
  1562. ;; to most-positive-fixnum, but if the compressed size exceeds
  1563. ;; that, we cannot visit the archive anyway.
  1564. (setq p (+ p hsize 2 (round csize))))
  1565. ((or (= hdrlvl 2) (= hdrlvl 0))
  1566. (setq p (+ p thsize 2 (round csize)))))
  1567. ))
  1568. (goto-char (point-min))
  1569. (let ((dash (concat (if archive-alternate-display
  1570. "- -------- ----- ----- "
  1571. "- ---------- -------- ----------- -------- ")
  1572. (make-string maxlen ?-)
  1573. "\n"))
  1574. (header (if archive-alternate-display
  1575. "M Length Uid Gid File\n"
  1576. "M Filemode Length Date Time File\n"))
  1577. (sumline (if archive-alternate-display
  1578. " %8.0f %d file%s"
  1579. " %8.0f %d file%s")))
  1580. (insert header dash)
  1581. (archive-summarize-files (nreverse visual))
  1582. (insert dash
  1583. (format sumline
  1584. totalsize
  1585. (length files)
  1586. (if (= 1 (length files)) "" "s"))
  1587. "\n"))
  1588. (apply #'vector (nreverse files))))
  1589. (defconst archive-lzh-alternate-display t)
  1590. (defun archive-lzh-extract (archive name)
  1591. (archive-extract-by-stdout archive name archive-lzh-extract))
  1592. (defun archive-lzh-resum (p count)
  1593. (let ((sum 0))
  1594. (while (> count 0)
  1595. (setq count (1- count)
  1596. sum (+ sum (byte-after p))
  1597. p (1+ p)))
  1598. (logand sum 255)))
  1599. (defun archive-lzh-rename-entry (newname descr)
  1600. (save-restriction
  1601. (save-excursion
  1602. (widen)
  1603. (let* ((p (+ archive-proper-file-start (aref descr 4)))
  1604. (oldhsize (byte-after p))
  1605. (oldfnlen (byte-after (+ p 21)))
  1606. (newfnlen (length newname))
  1607. (newhsize (+ oldhsize newfnlen (- oldfnlen)))
  1608. (inhibit-read-only t))
  1609. (if (> newhsize 255)
  1610. (error "The file name is too long"))
  1611. (goto-char (+ p 21))
  1612. (delete-char (1+ oldfnlen))
  1613. (insert-unibyte newfnlen newname)
  1614. (goto-char p)
  1615. (delete-char 2)
  1616. (insert-unibyte newhsize (archive-lzh-resum p newhsize))))))
  1617. (defun archive-lzh-ogm (newval files errtxt ofs)
  1618. (save-excursion
  1619. (save-restriction
  1620. (widen)
  1621. (dolist (fil files)
  1622. (let* ((p (+ archive-proper-file-start (aref fil 4)))
  1623. (hsize (byte-after p))
  1624. (fnlen (byte-after (+ p 21)))
  1625. (p2 (+ p 22 fnlen))
  1626. (creator (if (>= (- hsize fnlen) 24) (byte-after (+ p2 2)) 0))
  1627. (inhibit-read-only t))
  1628. (if (= creator ?U)
  1629. (progn
  1630. (or (numberp newval)
  1631. (setq newval (funcall newval (archive-l-e (+ p2 ofs) 2))))
  1632. (goto-char (+ p2 ofs))
  1633. (delete-char 2)
  1634. (insert-unibyte (logand newval 255) (lsh newval -8))
  1635. (goto-char (1+ p))
  1636. (delete-char 1)
  1637. (insert-unibyte (archive-lzh-resum (1+ p) hsize)))
  1638. (message "Member %s does not have %s field"
  1639. (aref fil 1) errtxt)))))))
  1640. (defun archive-lzh-chown-entry (newuid files)
  1641. (archive-lzh-ogm newuid files "an uid" 10))
  1642. (defun archive-lzh-chgrp-entry (newgid files)
  1643. (archive-lzh-ogm newgid files "a gid" 12))
  1644. (defun archive-lzh-chmod-entry (newmode files)
  1645. (archive-lzh-ogm
  1646. ;; This should work even though newmode will be dynamically accessed.
  1647. (lambda (old) (archive-calc-mode old newmode t))
  1648. files "a unix-style mode" 8))
  1649. ;; -------------------------------------------------------------------------
  1650. ;;; Section: Lzh Self-Extracting .exe Archives
  1651. ;;
  1652. ;; No support for modifying these files. It looks like the lha for unix
  1653. ;; program (as of version 1.14i) can't create or retain the DOS exe part.
  1654. ;; If you do an "lha a" on a .exe for instance it renames and writes to a
  1655. ;; plain .lzh.
  1656. (defun archive-lzh-exe-summarize ()
  1657. "Summarize the contents of an LZH self-extracting exe, for `archive-mode'."
  1658. ;; Skip the initial executable code part and apply archive-lzh-summarize
  1659. ;; to the archive part proper. The "-lh5-" etc regexp here for the start
  1660. ;; is the same as in archive-find-type.
  1661. ;;
  1662. ;; The lha program (version 1.14i) does this in skip_msdos_sfx1_code() by
  1663. ;; a similar scan. It looks for "..-l..-" plus for level 0 or 1 a test of
  1664. ;; the header checksum, or level 2 a test of the "attribute" and size.
  1665. ;;
  1666. (re-search-forward "..-l[hz][0-9ds]-" nil)
  1667. (archive-lzh-summarize (match-beginning 0)))
  1668. ;; `archive-lzh-extract' runs "lha pq", and that works for .exe as well as
  1669. ;; .lzh files
  1670. (defalias 'archive-lzh-exe-extract 'archive-lzh-extract
  1671. "Extract a member from an LZH self-extracting exe, for `archive-mode'.")
  1672. ;; -------------------------------------------------------------------------
  1673. ;;; Section: Zip Archives
  1674. (defun archive-zip-summarize ()
  1675. (goto-char (- (point-max) (- 22 18)))
  1676. (search-backward-regexp "[P]K\005\006")
  1677. (let ((p (archive-l-e (+ (point) 16) 4))
  1678. (maxlen 8)
  1679. (totalsize 0)
  1680. files
  1681. visual
  1682. emacs-int-has-32bits)
  1683. (when (= p -1)
  1684. ;; If the offset of end-of-central-directory is -1, this is a
  1685. ;; Zip64 extended ZIP file format, and we need to glean the info
  1686. ;; from Zip64 records instead.
  1687. ;;
  1688. ;; First, find the Zip64 end-of-central-directory locator.
  1689. (search-backward "PK\006\007")
  1690. ;; Pay attention: the offset of Zip64 end-of-central-directory
  1691. ;; is a 64-bit field, so it could overflow the Emacs integer
  1692. ;; even on a 64-bit host, let alone 32-bit one. But since we've
  1693. ;; already read the zip file into a buffer, and this is a byte
  1694. ;; offset into the file we've read, it must be short enough, so
  1695. ;; such an overflow can never happen, and we can safely read
  1696. ;; these 8 bytes into an Emacs integer. Moreover, on host with
  1697. ;; 32-bit Emacs integer we can only read 4 bytes, since they are
  1698. ;; stored in little-endian byte order.
  1699. (setq emacs-int-has-32bits (<= most-positive-fixnum #x1fffffff))
  1700. (setq p (+ (point-min)
  1701. (archive-l-e (+ (point) 8) (if emacs-int-has-32bits 4 8))))
  1702. (goto-char p)
  1703. ;; We should be at Zip64 end-of-central-directory record now.
  1704. (or (string= "PK\006\006" (buffer-substring p (+ p 4)))
  1705. (error "Unrecognized ZIP file format"))
  1706. ;; Offset to central directory:
  1707. (setq p (archive-l-e (+ p 48) (if emacs-int-has-32bits 4 8))))
  1708. (setq p (+ p (point-min)))
  1709. (while (string= "PK\001\002" (buffer-substring p (+ p 4)))
  1710. (let* ((creator (byte-after (+ p 5)))
  1711. ;; (method (archive-l-e (+ p 10) 2))
  1712. (modtime (archive-l-e (+ p 12) 2))
  1713. (moddate (archive-l-e (+ p 14) 2))
  1714. ;; Convert to float to avoid overflow for very large files.
  1715. (ucsize (archive-l-e (+ p 24) 4 'float))
  1716. (fnlen (archive-l-e (+ p 28) 2))
  1717. (exlen (archive-l-e (+ p 30) 2))
  1718. (fclen (archive-l-e (+ p 32) 2))
  1719. (lheader (archive-l-e (+ p 42) 4))
  1720. (efnname (let ((str (buffer-substring (+ p 46) (+ p 46 fnlen))))
  1721. (decode-coding-string
  1722. str archive-file-name-coding-system)))
  1723. (isdir (and (= ucsize 0)
  1724. (string= (file-name-nondirectory efnname) "")))
  1725. (mode (cond ((memq creator '(2 3)) ; Unix
  1726. (archive-l-e (+ p 40) 2))
  1727. ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
  1728. (logior ?\444
  1729. (if isdir (logior 16384 ?\111) 0)
  1730. (if (zerop
  1731. (logand 1 (byte-after (+ p 38))))
  1732. ?\222 0)))
  1733. (t nil)))
  1734. (modestr (if mode (archive-int-to-mode mode) "??????????"))
  1735. (fiddle (and archive-zip-case-fiddle
  1736. (not (not (memq creator '(0 2 4 5 9))))
  1737. (string= (upcase efnname) efnname)))
  1738. (ifnname (if fiddle (downcase efnname) efnname))
  1739. (width (string-width ifnname))
  1740. (text (format " %10s %8.0f %-11s %-8s %s"
  1741. modestr
  1742. ucsize
  1743. (archive-dosdate moddate)
  1744. (archive-dostime modtime)
  1745. ifnname)))
  1746. (setq maxlen (max maxlen width)
  1747. totalsize (+ totalsize ucsize)
  1748. visual (cons (vector text
  1749. (- (length text) (length ifnname))
  1750. (length text))
  1751. visual)
  1752. files (cons (if isdir
  1753. nil
  1754. (vector efnname ifnname fiddle mode
  1755. (list (1- p) lheader)))
  1756. files)
  1757. p (+ p 46 fnlen exlen fclen))))
  1758. (goto-char (point-min))
  1759. (let ((dash (concat "- ---------- -------- ----------- -------- "
  1760. (make-string maxlen ?-)
  1761. "\n")))
  1762. (insert "M Filemode Length Date Time File\n"
  1763. dash)
  1764. (archive-summarize-files (nreverse visual))
  1765. (insert dash
  1766. (format " %8.0f %d file%s"
  1767. totalsize
  1768. (length files)
  1769. (if (= 1 (length files)) "" "s"))
  1770. "\n"))
  1771. (apply #'vector (nreverse files))))
  1772. (defun archive-zip-extract (archive name)
  1773. (cond
  1774. ((member-ignore-case (car archive-zip-extract) '("pkunzip" "pkzip"))
  1775. (archive-*-extract archive name archive-zip-extract))
  1776. ((equal (car archive-zip-extract) archive-7z-program)
  1777. (let ((archive-7z-extract archive-zip-extract))
  1778. (archive-7z-extract archive name)))
  1779. (t
  1780. (archive-extract-by-stdout
  1781. archive
  1782. ;; unzip expands wildcards in NAME, so we need to quote it. But
  1783. ;; not on DOS/Windows, since that fails extraction on those
  1784. ;; systems (unless w32-quote-process-args is nil), and file names
  1785. ;; with wildcards in zip archives don't work there anyway.
  1786. ;; FIXME: Does pkunzip need similar treatment?
  1787. (if (and (or (not (memq system-type '(windows-nt ms-dos)))
  1788. (and (boundp 'w32-quote-process-args)
  1789. (null w32-quote-process-args)))
  1790. (equal (car archive-zip-extract) "unzip"))
  1791. (shell-quote-argument name)
  1792. name)
  1793. archive-zip-extract))))
  1794. (defun archive-zip-write-file-member (archive descr)
  1795. (archive-*-write-file-member
  1796. archive
  1797. descr
  1798. (if (aref descr 2) archive-zip-update-case archive-zip-update)))
  1799. (defun archive-zip-chmod-entry (newmode files)
  1800. (save-restriction
  1801. (save-excursion
  1802. (widen)
  1803. (dolist (fil files)
  1804. (let* ((p (+ archive-proper-file-start (car (aref fil 4))))
  1805. (creator (byte-after (+ p 5)))
  1806. (oldmode (aref fil 3))
  1807. (newval (archive-calc-mode oldmode newmode t))
  1808. (inhibit-read-only t))
  1809. (cond ((memq creator '(2 3)) ; Unix
  1810. (goto-char (+ p 40))
  1811. (delete-char 2)
  1812. (insert-unibyte (logand newval 255) (lsh newval -8)))
  1813. ((memq creator '(0 5 6 7 10 11 15)) ; Dos etc.
  1814. (goto-char (+ p 38))
  1815. (insert-unibyte (logior (logand (byte-after (point)) 254)
  1816. (logand (logxor 1 (lsh newval -7)) 1)))
  1817. (delete-char 1))
  1818. (t (message "Don't know how to change mode for this member"))))
  1819. ))))
  1820. ;; -------------------------------------------------------------------------
  1821. ;;; Section: Zoo Archives
  1822. (defun archive-zoo-summarize ()
  1823. (let ((p (1+ (archive-l-e 25 4)))
  1824. (maxlen 8)
  1825. (totalsize 0)
  1826. files
  1827. visual)
  1828. (while (and (string= "\334\247\304\375" (buffer-substring p (+ p 4)))
  1829. (> (archive-l-e (+ p 6) 4) 0))
  1830. (let* ((next (1+ (archive-l-e (+ p 6) 4)))
  1831. (moddate (archive-l-e (+ p 14) 2))
  1832. (modtime (archive-l-e (+ p 16) 2))
  1833. ;; Convert to float to avoid overflow for very large files.
  1834. (ucsize (archive-l-e (+ p 20) 4 'float))
  1835. (namefld (buffer-substring (+ p 38) (+ p 38 13)))
  1836. (dirtype (byte-after (+ p 4)))
  1837. (lfnlen (if (= dirtype 2) (byte-after (+ p 56)) 0))
  1838. (ldirlen (if (= dirtype 2) (byte-after (+ p 57)) 0))
  1839. (fnlen (or (string-match "\0" namefld) 13))
  1840. (efnname (let ((str
  1841. (concat
  1842. (if (> ldirlen 0)
  1843. (concat (buffer-substring
  1844. (+ p 58 lfnlen)
  1845. (+ p 58 lfnlen ldirlen -1))
  1846. "/")
  1847. "")
  1848. (if (> lfnlen 0)
  1849. (buffer-substring (+ p 58)
  1850. (+ p 58 lfnlen -1))
  1851. (substring namefld 0 fnlen)))))
  1852. (decode-coding-string
  1853. str archive-file-name-coding-system)))
  1854. (fiddle (and (= lfnlen 0) (string= efnname (upcase efnname))))
  1855. (ifnname (if fiddle (downcase efnname) efnname))
  1856. (width (string-width ifnname))
  1857. (text (format " %8.0f %-11s %-8s %s"
  1858. ucsize
  1859. (archive-dosdate moddate)
  1860. (archive-dostime modtime)
  1861. ifnname)))
  1862. (setq maxlen (max maxlen width)
  1863. totalsize (+ totalsize ucsize)
  1864. visual (cons (vector text
  1865. (- (length text) (length ifnname))
  1866. (length text))
  1867. visual)
  1868. files (cons (vector efnname ifnname fiddle nil (1- p))
  1869. files)
  1870. p next)))
  1871. (goto-char (point-min))
  1872. (let ((dash (concat "- -------- ----------- -------- "
  1873. (make-string maxlen ?-)
  1874. "\n")))
  1875. (insert "M Length Date Time File\n"
  1876. dash)
  1877. (archive-summarize-files (nreverse visual))
  1878. (insert dash
  1879. (format " %8.0f %d file%s"
  1880. totalsize
  1881. (length files)
  1882. (if (= 1 (length files)) "" "s"))
  1883. "\n"))
  1884. (apply #'vector (nreverse files))))
  1885. (defun archive-zoo-extract (archive name)
  1886. (archive-extract-by-stdout archive name archive-zoo-extract))
  1887. ;; -------------------------------------------------------------------------
  1888. ;;; Section: Rar Archives
  1889. (defun archive-rar-summarize (&optional file)
  1890. ;; File is used internally for `archive-rar-exe-summarize'.
  1891. (unless file (setq file buffer-file-name))
  1892. (let* ((copy (file-local-copy file))
  1893. (maxname 10)
  1894. (maxsize 5)
  1895. (files ()))
  1896. (with-temp-buffer
  1897. (call-process "lsar" nil t nil "-l" (or file copy))
  1898. (if copy (delete-file copy))
  1899. (goto-char (point-min))
  1900. (re-search-forward "^\\(\s+=+\s?+\\)+\n")
  1901. (while (looking-at (concat "^\s+[0-9.]+\s+-+\s+" ; Flags
  1902. "\\([0-9-]+\\)\s+" ; Size
  1903. "\\([0-9.%]+\\)\s+" ; Ratio
  1904. "\\([0-9a-zA-Z]+\\)\s+" ; Mode
  1905. "\\([0-9-]+\\)\s+" ; Date
  1906. "\\([0-9:]+\\)\s+" ; Time
  1907. "\\(.*\\)\n" ; Name
  1908. ))
  1909. (goto-char (match-end 0))
  1910. (let ((name (match-string 6))
  1911. (size (match-string 1)))
  1912. (if (> (length name) maxname) (setq maxname (length name)))
  1913. (if (> (length size) maxsize) (setq maxsize (length size)))
  1914. (push (vector name name nil nil
  1915. ;; Size, Ratio.
  1916. size (match-string 2)
  1917. ;; Date, Time.
  1918. (match-string 4) (match-string 5))
  1919. files))))
  1920. (setq files (nreverse files))
  1921. (goto-char (point-min))
  1922. (let* ((format (format " %%s %%s %%%ds %%5s %%s" maxsize))
  1923. (sep (format format "----------" "-----" (make-string maxsize ?-)
  1924. "-----" ""))
  1925. (column (length sep)))
  1926. (insert (format format " Date " "Time " "Size" "Ratio" "Filename") "\n")
  1927. (insert sep (make-string maxname ?-) "\n")
  1928. (archive-summarize-files (mapcar (lambda (desc)
  1929. (let ((text
  1930. (format format
  1931. (aref desc 6)
  1932. (aref desc 7)
  1933. (aref desc 4)
  1934. (aref desc 5)
  1935. (aref desc 1))))
  1936. (vector text
  1937. column
  1938. (length text))))
  1939. files))
  1940. (insert sep (make-string maxname ?-) "\n")
  1941. (apply #'vector files))))
  1942. (defun archive-rar-extract (archive name)
  1943. ;; unrar-free seems to have no way to extract to stdout or even to a file.
  1944. (if (file-name-absolute-p name)
  1945. ;; The code below assumes the name is relative and may do undesirable
  1946. ;; things otherwise.
  1947. (error "Can't extract files with non-relative names")
  1948. (archive-extract-by-file archive name `("unar" "-no-directory" "-o") "Successfully extracted")))
  1949. ;;; Section: Rar self-extracting .exe archives.
  1950. (defun archive-rar-exe-summarize ()
  1951. (let ((tmpfile (make-temp-file "rarexe")))
  1952. (unwind-protect
  1953. (progn
  1954. (goto-char (point-min))
  1955. (re-search-forward "Rar!")
  1956. (write-region (match-beginning 0) (point-max) tmpfile)
  1957. (archive-rar-summarize tmpfile))
  1958. (delete-file tmpfile))))
  1959. (defun archive-rar-exe-extract (archive name)
  1960. (let* ((tmpfile (make-temp-file "rarexe"))
  1961. (buf (find-buffer-visiting archive))
  1962. (tmpbuf (unless buf (generate-new-buffer " *rar-exe*"))))
  1963. (unwind-protect
  1964. (progn
  1965. (with-current-buffer (or buf tmpbuf)
  1966. (save-excursion
  1967. (save-restriction
  1968. (if buf
  1969. ;; point-max unwidened is assumed to be the end of the
  1970. ;; summary text and the beginning of the actual file data.
  1971. (progn (goto-char (point-max)) (widen))
  1972. (insert-file-contents-literally archive)
  1973. (goto-char (point-min)))
  1974. (re-search-forward "Rar!")
  1975. (write-region (match-beginning 0) (point-max) tmpfile))))
  1976. (archive-rar-extract tmpfile name))
  1977. (if tmpbuf (kill-buffer tmpbuf))
  1978. (delete-file tmpfile))))
  1979. ;; -------------------------------------------------------------------------
  1980. ;;; Section: 7z Archives
  1981. (defun archive-7z-summarize ()
  1982. (let ((maxname 10)
  1983. (maxsize 5)
  1984. (file buffer-file-name)
  1985. (files ()))
  1986. (with-temp-buffer
  1987. (call-process archive-7z-program nil t nil "l" "-slt" file)
  1988. (goto-char (point-min))
  1989. ;; Four dashes start the meta info section that should be skipped.
  1990. ;; Archive members start with more than four dashes.
  1991. (re-search-forward "^-----+\n")
  1992. (while (re-search-forward "^Path = \\(.*\\)\n" nil t)
  1993. (goto-char (match-end 0))
  1994. (let ((name (match-string 1))
  1995. (size (save-excursion
  1996. (and (re-search-forward "^Size = \\(.*\\)\n")
  1997. (match-string 1))))
  1998. (time (save-excursion
  1999. (and (re-search-forward "^Modified = \\(.*\\)\n")
  2000. (match-string 1)))))
  2001. (if (> (length name) maxname) (setq maxname (length name)))
  2002. (if (> (length size) maxsize) (setq maxsize (length size)))
  2003. (push (vector name name nil nil time nil nil size)
  2004. files))))
  2005. (setq files (nreverse files))
  2006. (goto-char (point-min))
  2007. (let* ((format (format " %%%ds %%s %%s" maxsize))
  2008. (sep (format format (make-string maxsize ?-) "-------------------" ""))
  2009. (column (length sep)))
  2010. (insert (format format "Size " "Date Time " " Filename") "\n")
  2011. (insert sep (make-string maxname ?-) "\n")
  2012. (archive-summarize-files (mapcar (lambda (desc)
  2013. (let ((text
  2014. (format format
  2015. (aref desc 7)
  2016. (aref desc 4)
  2017. (aref desc 1))))
  2018. (vector text
  2019. column
  2020. (length text))))
  2021. files))
  2022. (insert sep (make-string maxname ?-) "\n")
  2023. (apply #'vector files))))
  2024. (defun archive-7z-extract (archive name)
  2025. ;; 7z doesn't provide a `quiet' option to suppress non-essential
  2026. ;; stderr messages. So redirect stderr to a temp file and display it
  2027. ;; in the echo area when it contains no message indicating success.
  2028. (archive-extract-by-stdout
  2029. archive name archive-7z-extract "Everything is Ok"))
  2030. (defun archive-7z-write-file-member (archive descr)
  2031. (archive-*-write-file-member
  2032. archive
  2033. descr
  2034. archive-7z-update))
  2035. ;; -------------------------------------------------------------------------
  2036. ;;; Section `ar' archives.
  2037. ;; TODO: we currently only handle the basic format of ar archives,
  2038. ;; not the GNU nor the BSD extensions. As it turns out, this is sufficient
  2039. ;; for .deb packages.
  2040. (autoload 'tar-grind-file-mode "tar-mode")
  2041. (defconst archive-ar-file-header-re
  2042. "\\(.\\{16\\}\\)\\([ 0-9]\\{12\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-9]\\{6\\}\\)\\([ 0-7]\\{8\\}\\)\\([ 0-9]\\{10\\}\\)`\n")
  2043. (defun archive-ar-summarize ()
  2044. ;; File is used internally for `archive-rar-exe-summarize'.
  2045. (let* ((maxname 10)
  2046. (maxtime 16)
  2047. (maxuser 5)
  2048. (maxgroup 5)
  2049. (maxmode 8)
  2050. (maxsize 5)
  2051. (files ()))
  2052. (goto-char (point-min))
  2053. (search-forward "!<arch>\n")
  2054. (while (looking-at archive-ar-file-header-re)
  2055. (let ((name (match-string 1))
  2056. extname
  2057. ;; Emacs will automatically use float here because those
  2058. ;; timestamps don't fit in our ints.
  2059. (time (string-to-number (match-string 2)))
  2060. (user (match-string 3))
  2061. (group (match-string 4))
  2062. (mode (string-to-number (match-string 5) 8))
  2063. (size (string-to-number (match-string 6))))
  2064. ;; Move to the beginning of the data.
  2065. (goto-char (match-end 0))
  2066. (setq time (format-time-string "%Y-%m-%d %H:%M" time))
  2067. (setq extname
  2068. (cond ((equal name "// ")
  2069. (propertize ".<ExtNamesTable>." 'face 'italic))
  2070. ((equal name "/ ")
  2071. (propertize ".<LookupTable>." 'face 'italic))
  2072. ((string-match "/? *\\'" name)
  2073. (substring name 0 (match-beginning 0)))))
  2074. (setq user (substring user 0 (string-match " +\\'" user)))
  2075. (setq group (substring group 0 (string-match " +\\'" group)))
  2076. (setq mode (tar-grind-file-mode mode))
  2077. ;; Move to the end of the data.
  2078. (forward-char size) (if (eq ?\n (char-after)) (forward-char 1))
  2079. (setq size (number-to-string size))
  2080. (if (> (length name) maxname) (setq maxname (length name)))
  2081. (if (> (length time) maxtime) (setq maxtime (length time)))
  2082. (if (> (length user) maxuser) (setq maxuser (length user)))
  2083. (if (> (length group) maxgroup) (setq maxgroup (length group)))
  2084. (if (> (length mode) maxmode) (setq maxmode (length mode)))
  2085. (if (> (length size) maxsize) (setq maxsize (length size)))
  2086. (push (vector name extname nil mode
  2087. time user group size)
  2088. files)))
  2089. (setq files (nreverse files))
  2090. (goto-char (point-min))
  2091. (let* ((format (format "%%%ds %%%ds/%%-%ds %%%ds %%%ds %%s"
  2092. maxmode maxuser maxgroup maxsize maxtime))
  2093. (sep (format format (make-string maxmode ?-)
  2094. (make-string maxuser ?-)
  2095. (make-string maxgroup ?-)
  2096. (make-string maxsize ?-)
  2097. (make-string maxtime ?-) ""))
  2098. (column (length sep)))
  2099. (insert (format format " Mode " "User" "Group" " Size "
  2100. " Date " "Filename")
  2101. "\n")
  2102. (insert sep (make-string maxname ?-) "\n")
  2103. (archive-summarize-files (mapcar (lambda (desc)
  2104. (let ((text
  2105. (format format
  2106. (aref desc 3)
  2107. (aref desc 5)
  2108. (aref desc 6)
  2109. (aref desc 7)
  2110. (aref desc 4)
  2111. (aref desc 1))))
  2112. (vector text
  2113. column
  2114. (length text))))
  2115. files))
  2116. (insert sep (make-string maxname ?-) "\n")
  2117. (apply #'vector files))))
  2118. (defun archive-ar-extract (archive name)
  2119. (let ((destbuf (current-buffer))
  2120. (archivebuf (find-file-noselect archive))
  2121. (from nil) size)
  2122. (with-current-buffer archivebuf
  2123. (save-restriction
  2124. ;; We may be in archive-mode or not, so either with or without
  2125. ;; narrowing and with or without a prepended summary.
  2126. (save-excursion
  2127. (widen)
  2128. (search-forward "!<arch>\n")
  2129. (while (and (not from) (looking-at archive-ar-file-header-re))
  2130. (let ((this (match-string 1)))
  2131. (setq size (string-to-number (match-string 6)))
  2132. (goto-char (match-end 0))
  2133. (if (equal name this)
  2134. (setq from (point))
  2135. ;; Move to the end of the data.
  2136. (forward-char size) (if (eq ?\n (char-after)) (forward-char 1)))))
  2137. (when from
  2138. (set-buffer-multibyte nil)
  2139. (with-current-buffer destbuf
  2140. ;; Do it within the `widen'.
  2141. (insert-buffer-substring archivebuf from (+ from size)))
  2142. (set-buffer-multibyte 'to)
  2143. ;; Inform the caller that the call succeeded.
  2144. t))))))
  2145. ;; -------------------------------------------------------------------------
  2146. ;; This line was a mistake; it is kept now for compatibility.
  2147. ;; rms 15 Oct 98
  2148. (provide 'archive-mode)
  2149. (provide 'arc-mode)
  2150. ;;; arc-mode.el ends here