arc-mode.el 86 KB

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