summaryrefslogtreecommitdiff
path: root/deb-view.el
blob: 6d6515b602db40ec6277251a60f1af3c250ca760 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
;;; deb-view.el --- view Debian package files with tar-mode

;; Copyright (C) 1998 Rick Macdonald <rickmacd@shaw.ca>
;; Copyright (C) 2003, 2004, 2005, 2009 Peter S Galbraith <psg@debian.org>

;; Author:     Rick Macdonald <rickmacd@shaw.ca>
;; Maintainer: Peter S. Galbraith <psg@debian.org>
;; Version: 1.15

;; This file is not part of GNU Emacs.

;; deb-view is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; deb-view is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

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

;;; Commentary:

;; deb-view presents the contents of debian package archive files for
;; viewing. The viewing is done with the major mode "debview", which
;; is derived from emacs tar-mode with a few enhancements for viewing
;; compressed files, HTML files and formatted man pages.  The normal
;; editing and saving features of tar-mode are not supported by
;; deb-view.

;; deb-view includes a command called deb-find which requires that you
;; have the debian distribution directories on a local or mounted
;; filesystem. Give it a string or regular expression and it presents a
;; buffer of matching deb file names.  Click with the middle mouse button
;; or press RETURN (or ^C^C) and it launches deb-view on the selected
;; file. deb-find can be configured to use locate or find, or any other
;; external command. The find method passes your search specification to
;; egrep, whereas the locate method uses your string directly.

;; deb-view extracts the control.tar.gz and data.tar.gz files from
;; debian package and presents two buffers in a derivitive of
;; tar-mode. See tar-mode for info.

;; Required programs: ar, gzip.
;; Optionally required programs: nroff for formatting man pages.
;; Optionally required programs: dpkg-deb for old-style binary .deb files.
;; Optionally required programs: w3-mode for viewing HTML pages.

;; For new-style .deb files (2.0), dpkg-deb isn't used. Therefore
;; deb-view should work on any platform with the ar command, although
;; "ar -p" doesn't seem to work for .deb files on Solaris 2.4 and 2.5.
;; It works on Solaris 2.6, SGI's IRIX 6.1 and 6.2, and Linux, of course.

;; Old-style .deb files require the dpkg-deb program. I don't know how to
;; extract control.tar.gz from these deb files, so you only get to see
;; the package control file, but nothing else such as the install scripts.
;; If you know how to get the control.tar.gz file out, let me know!
;; The data file is still viewable thanks to the "dpkg-deb --fsys-tarfile"
;; option.

;;; Installation:

;; 1) Quick test to see if you like deb-view.

;; Put this file in your home directory, and call it deb-view.el.
;; Start up emacs and do the following:
;;      ESCAPE x load-file RETURN ~/deb-view.el RETURN
;; Then, view a deb file with CTRL-d in Dired mode,
;; or execute:
;;      ESCAPE x deb-view RETURN {/full/path/of/file.deb} RETURN
;; or execute:
;;      ESCAPE : (setq deb-find-directory "/your/debian/directory") RETURN
;;      ESCAPE x deb-find RETURN {deb-file-search-string} RETURN
;;      and select a deb file to view with RETURN or middle mouse button in
;;      the search results buffer that is created. Exit this buffer with "q".

;; 2) Permanent installation.

;; When installed this way, all find-file operations (such as "f" or "v" in
;; dired-mode) will automatically recognize debian files and load deb-view
;; when required.

;; Put this file somewhere where Emacs can find it (i.e., in one of the paths
;; in your `load-path'), `byte-compile-file' it, and put the following six
;; lines (with semi-colons removed) in your ~/.emacs file (or create ~/.emacs
;; if you don't have one):
;;(autoload 'deb-find            "deb-view" "Debian Archive File Finder" t)
;;(autoload 'deb-view-mode       "deb-view" "Debian Archive File Mode" t)
;;(autoload 'deb-view            "deb-view" "Debian Archive File Viewer" t)
;;(autoload 'deb-view-dired-view "deb-view" "Debian Archive File Viewer" t)
;;(setq auto-mode-alist (append '(("\\.deb$" . deb-view-mode)) auto-mode-alist))
;;(define-key dired-mode-map     "\C-d"     'deb-view-dired-view)

;; If you're not very familiar with emacs customization, here is a simpler
;; approach. Add this line to your ~/.emacs file (or create ~/.emacs if you
;; don't have one):
;;     (load "~/deb-view.el")
;; Or, if you can put deb-view into your load-path (execute
;; "^h v load-path RETURN" to see your load-path setting)
;; then just add the following to your ~./emacs file:
;;     (require 'deb-view)

;; deb-view is mostly unobtrusive, but does bind ^d in dired to
;; deb-view-dired-view.  The "debview" mode is derived from
;; tar-mode.el using derived.el. Compared to tar-mode, debview-mode
;; binds q, N, W, and re-binds v. Also, the normal editing and saving
;; features of tar-mode are not supported by debview mode and those
;; keys are disabled.

;; 3) Configuration

;; deb-find has two variables to set. deb-find-method can be "locate" or
;; "find". Any other value will be assumed to be an external script or
;; program that you supply. If you set deb-find-method to "find" then you
;; must also set deb-find-directory to the directory containing the
;; debian distribution. The find command starts at this point. I originally
;; used the locate option, but contrary to the man page it doesn't seem to
;; understand even simple regular expressions. I prefer the find option. It
;; uses egrep and therefore understands complex regular expressions.
;; You might want to bind deb-find to a special key. I use ^C^D like this:
;; (define-key ctl-x-map "\C-d" 'deb-find)
;; Note that this key is normally the brief list-directory command, a
;; command that I never used anyway.


;;; Usage:

;; In dired, press f or e on the dired line of the .deb file to view.
;; You can also use ^d, which is actually slightly faster since the
;; deb file isn't loaded into a buffer needlessly.

;; Or, execute: ESCAPE x deb-view RETURN, and enter the .deb file name
;; at the prompt.

;; Or, execute: ESCAPE x deb-find RETURN, and enter any substring of a
;; deb file name to search for. A buffer of matches is created.
;; Launch deb-view by selecting a deb file with the middle mouse button,
;; or RETURN or ^c^c. Exit this buffer with "q".

;; You are shown two tar files in debview-mode (see tar-mode for help).
;; In the case of old .deb format files, the control info is shown
;; but not the other files of control.tar, such as install scripts.
;; Note that regular tar-mode commands e, f and RETURN show raw files
;; without any special uncompressing or formatting.
;; Additional features that deb-view adds to tar-mode:
;; q - kill both view buffers (INFO and DATA) and return to the
;;     dired buffer if that's where you executed deb-mode.
;; v - executes deb-view-tar-view instead of tar-view, with the
;;     additional smarts to uncompress .gz and .Z files for viewing.
;; N - Like in dired, formats man pages for viewing, with the
;;     additional smarts to uncompress .gz and .Z man files for viewing.
;; W - use w3-mode to view an HTML file.

;; To view files not supported by deb-view, such as graphics, use the
;; copy command ("c") to copy the file to a temp directory.  You can
;; then do what you want to the file.


;;; History:
;; 

;; 1.3  - modified logic that determines old or new style Debian packages.
;;        On systems where the file command recognizes debian files, it
;;        wrongly always came up with old format.

;; 1.4  - added missing semicolons in the comments for Changelog 1.3.
;;      - fixed various spacing issues in doc strings.
;;      - disabled tar-mode keys that are not applicable to deb-view.

;; 1.5  - added an auto-mode-alist and deb-view mode so that deb-view
;;        is launched from any find-file command.
;;      - added a deb-find command that takes a search string and creates
;;        a buffer of matching deb files. ^C^C, RETURN or middle mouse button
;;        runs deb-view on the selected deb file.
;;      - added deb-view-help to "?" key in deb-view.

;; 1.6  - improved doc strings for deb-find and deb-find-method.
;;      - added (provide 'deb-view) and instructions for using
;;        (require 'deb-view).
;;      - reworked the documentation somewhat, but it's still too long.
;;      - changed the copyright notice to refer to deb-view, not Emacs.

;; 1.7  - make copy of compilation-minor-mode map rather than changing
;;        it directly. It was breaking actual compilation buffer keymaps,
;;        such as grep mode.

;; 1.8  - fixed deb-find when deb-find-method is set to "find". It wasn't
;;        adding "/*" to the end of the directory name for the find command.

;; 1.9  - Added support for handling remote deb files (ange-ftp).
;;      - reworked to use derived.el instead of messing with tar-mode
;;        directly. (Thanks to era eriksson <era@iki.fi>)

;; 1.10 2003-10-30
;;  - New maintainer: Peter S. Galbraith <psg@debian.org>
;;  - checkdoc edits.
;;  - made defvars into defcustoms.

;; 1.11 2004-01-16  Peter S. Galbraith <psg@debian.org>
;;  - Resize top (control) window to fit number of lines since it
;;    doesn't really need to be 1/2 the screen.  Thanks to Dan
;;    Jacobson for suggesting this change (Closes: #224950).

;; 1.12 2005-10-24  Peter S. Galbraith <psg@debian.org>
;;  - Output an error message if the package file is corrupted
;;    (e.g. partial download).
;;    Thanks to Dan Jacobson for suggesting this change (Closes: #235673).
;;  - deb-view-dired-view: Check if file in dired is a .deb before opening.
;;    Thanks to Dan Jacobson for suggesting this change (Closes: #273902)
;;  - deb-view-tar-view: If the file to be opned is from the INFO buffer,
;;    then open in the other (larger) window.
;;    Thanks to Dan Jacobson for suggesting this change (Closes: #321869)

;; 1.13 2006-02-02 Sven Joachim <sven_joachim@web.de>
;;    Bug fix for UTF-8 (Closes: #344260)
;;    The `call-process' and `call-process-region' use
;;    default-process-coding-system rather than coding-system-for-read.
;;    The former is set to '(mule-utf-8 . mule-utf-8) in my setup, and that
;;    caused the problem.  So the solution is to bind
;;    default-process-coding-system as well in deb-view-process

;; 1.14 2009-10-25  Peter S. Galbraith <psg@debian.org>
;;    Added support for data.tar.bz2 deb files (Closes: #457094).

;; 1.15 2009-11-02  Peter S. Galbraith <psg@debian.org>
;;    Fixed stupid bug "deb-view.el fails on own debian-el_30.9-1_all.deb",
;;    thanks to Kevin Ryde (Closes: #554039).

;; 1.16 2011-08-16  Peter S. Galbraith <psg@debian.org>
;;    Added support for data.tar.xz deb files (Closes: #637579).

;;; Code:

(defgroup deb-view nil
  "View Debian package files with tar-mode"
  :group 'tools
  :prefix "deb-view")

(defcustom deb-view-tar-uncompress-program "gzip -cd"
  "*Program to use for uncompression of .gz and .Z files in `deb-view'."
  :group 'deb-view
  :type 'string)

;; Note the following useful variable from tar-mode:
;;(defvar tar-mode-show-date nil
;; "*Non-nil means Tar mode should show the date/time of each subfile.
;;This information is useful, but it takes screen space away from file names.")

(defcustom deb-find-method "find"
  "Internal `deb-find' methods supported: locate or find.
Any other entry is assumed to be an external command.
See also the variable deb-find-directory."
  :group 'deb-view
  :type '(radio (const "find") (const "locate")))

(defcustom deb-find-directory "/usr/local/src/debian"
  "Directory to run find in when deb-find-method is \"find\"."
  :group 'deb-view
  :type 'directory)

(define-derived-mode debview-mode tar-mode "debview"
  "Major mode for debview.\n\n\\{debview-mode-map}")

;; Prohibit things that tar-mode does that deb-view doesn't:
(define-key debview-mode-map "\C-d" 'undefined)
(define-key debview-mode-map "G"    'undefined)
(define-key debview-mode-map "M"    'undefined)
(define-key debview-mode-map "O"    'undefined)
(define-key debview-mode-map "d"    'undefined)
(define-key debview-mode-map "g"    'undefined)
(define-key debview-mode-map "r"    'undefined)
(define-key debview-mode-map "u"    'undefined)
(define-key debview-mode-map "x"    'undefined)
(define-key debview-mode-map ""   'undefined)
             
(define-key debview-mode-map "?"    'deb-view-help)
(define-key debview-mode-map "q"    'deb-view-dired-view-cleanup)
(define-key debview-mode-map "N"    'deb-view-tar-man)
(define-key debview-mode-map "W"    'deb-view-tar-w3)
(define-key debview-mode-map "v"    'deb-view-tar-view)
(define-key debview-mode-map [up]   'tar-previous-line)
(define-key debview-mode-map [down] 'tar-next-line)
(define-key debview-mode-map "\eOA" 'tar-previous-line)
(define-key debview-mode-map "\eOB" 'tar-next-line)
(define-key debview-mode-map "\e[A" 'tar-previous-line)
(define-key debview-mode-map "\e[B" 'tar-next-line)

(defvar deb-view-dired-view-return-buffer ""
  "Return to this buffer after deb-view-dired-view-cleanup is called.")
(make-variable-buffer-local 'deb-view-dired-view-return-buffer)

(defvar deb-view-tempfile ""
  "Flag saying if the deb file is temporary (ange-ftp) and needs deleting.")

(defvar deb-view-file-name ""
  "The file name being processed by `deb-view'.")

;; You might not like the key bindings that I chose:
(if (featurep 'dired)
    (define-key dired-mode-map "\C-d" 'deb-view-dired-view)
  (add-hook
   'dired-load-hook
   (function (lambda ()
               (define-key dired-mode-map "\C-d" 'deb-view-dired-view)))))

;;;###autoload
(defun deb-view-dired-view ()
  "View Debian package control and data files.
Press \"q\" in either window to kill both buffers
and return to the dired buffer. See deb-view."
  (interactive)
  (let  ((file (dired-get-filename)))
    (if (string-match ".deb$" file)
        (deb-view file)
      (error "Not a Debian package file"))))

;;;###autoload
(defun deb-view (debfile)
  "View Debian package DEBFILE's control and data files.
Press \"q\" in either window to kill both buffers.

In dired, press ^d on the dired line of the .deb file to view.
Or, execute: ESCAPE x deb-view RETURN, and enter the .deb file name
at the prompt."
  (interactive "fdeb file to view: ")
  (if (and (or (string-match "Lucid" emacs-version)
 	       (string-match "XEmacs" emacs-version))
 	   (>= emacs-major-version 21))
      (require 'view-less)
    (require 'view))
  (require 'view)
  (if (< (nth 1 (file-attributes debfile)) 0)
      (progn
        ;; This is a remote file.
        ;; Call view-file to force ange-ftp to get it first.
        (message "deb-view remote file: %s" debfile)
        (find-file debfile))
    ;; This is a local file.
    (setq debfile (expand-file-name debfile))
    ;;(message "deb-view local file: %s" debfile)
    (setq deb-view-file-name debfile)
    (setq deb-view-tempfile nil)
    (deb-view-process debfile)))

(defun deb-view-process (debfile)
  "View Debian Archive Files for package DEBFILE."
  (let* ((deb-view-buffer-name (file-name-nondirectory deb-view-file-name))
         (info-buffer-name (concat deb-view-buffer-name "-INFO"))
         (data-buffer-name (concat deb-view-buffer-name "-DATA"))
         (info-buffer (progn (and (get-buffer info-buffer-name)
                                  (kill-buffer (get-buffer info-buffer-name)))
                             (get-buffer-create info-buffer-name)))
         (data-buffer (progn (and (get-buffer data-buffer-name)
                                  (kill-buffer (get-buffer data-buffer-name)))
                             (get-buffer-create data-buffer-name)))
         (return-buffer (current-buffer))
	 (coding-system-for-read 'no-conversion)
         (default-process-coding-system '(no-conversion . no-conversion))
	 file-buffer
         new-archive-format)
    (message "deb-view processing deb file %s..." deb-view-buffer-name)
    ;; info
    (setq file-buffer (get-buffer-create " *file-data*"))
    (setq new-archive-format
          (save-excursion
            (set-buffer file-buffer)
            (erase-buffer)
            (call-process shell-file-name nil t nil shell-command-switch
                          (concat "file " debfile))
            (goto-char 1)
            (if (string-match "archive" (buffer-string))
                t
              (goto-char 1)
              (if (string-match "old debian" (buffer-string))
                  nil
                t))))
    (kill-buffer file-buffer)
    (set-buffer info-buffer)
    (cond
     (new-archive-format
      ;; New deb format (archive)
      (call-process shell-file-name nil t nil shell-command-switch
                    (concat "dpkg-deb --ctrl-tarfile " debfile))
      (goto-char 1)
      (setq buffer-file-name (concat deb-view-file-name "-INFO"))
      (if (fboundp 'set-buffer-multibyte) (set-buffer-multibyte nil))
      (debview-mode)
      ;; Turn off view-mode in this buffer:
      (make-variable-buffer-local 'view-mode-hook)
      (add-hook
       'view-mode-hook
       (function (lambda ()
                   (view-mode -1)
                   (setq view-exit-action 'deb-view-dired-view-cleanup))))
      (message "deb-view processing deb file %s..." deb-view-buffer-name)
      (tar-next-line 1)
      (switch-to-buffer info-buffer t))
     (t
      ;; Old deb format
      (message "deb-view old dpkg binary format")
      (call-process shell-file-name nil t nil shell-command-switch
                    (concat "dpkg-deb -I " debfile))
      (setq buffer-read-only t)
      (set-buffer-modified-p nil)
      (goto-char 1)
      (switch-to-buffer info-buffer t)
      (view-mode-enter return-buffer 'deb-view-dired-view-cleanup)))
    (set-buffer-modified-p nil)
    (setq buffer-read-only t)
    (setq deb-view-dired-view-return-buffer return-buffer)
    (delete-other-windows)
    ;; data
    (set-buffer data-buffer)
    (buffer-disable-undo)
    (cond
     (new-archive-format
      (call-process "ar" nil '(t t) nil "-t" debfile)
      (goto-char 1)
      (cond
       ((re-search-forward "data.tar.gz" nil t)
        (erase-buffer)
        (call-process "ar" nil '(t t) nil "-p" debfile "data.tar.gz")
        (goto-char (point-max))
        (when (search-backward "is not a valid archive" nil t)
          (kill-buffer data-buffer)
          (kill-buffer info-buffer)        
          (error "%s: Not a valid package file" deb-view-buffer-name))
        (call-process-region (point-min) (point-max) "gzip" t t nil "-cd"))
       ((and (goto-char 1)(re-search-forward "data.tar.bz2" nil t))
        (erase-buffer)
        (call-process "ar" nil '(t t) nil "-p" debfile "data.tar.bz2")
        (goto-char (point-max))
        (when (search-backward "is not a valid archive" nil t)
          (kill-buffer data-buffer)
          (kill-buffer info-buffer)        
          (error "%s: Not a valid package file" deb-view-buffer-name))
        (call-process-region (point-min) (point-max) "bzip2" t t nil "-cd"))
       ((and (goto-char 1)(re-search-forward "data.tar.xz" nil t))
        (erase-buffer)
        (call-process "ar" nil '(t t) nil "-p" debfile "data.tar.xz")
        (goto-char (point-max))
        (when (search-backward "is not a valid archive" nil t)
          (kill-buffer data-buffer)
          (kill-buffer info-buffer)        
          (error "%s: Not a valid package file" deb-view-buffer-name))
        (call-process-region (point-min) (point-max) "xz" t t nil "-cd"))))
     (t      
      (call-process shell-file-name nil t nil shell-command-switch
                    (concat "dpkg-deb --fsys-tarfile " debfile))))
    (goto-char 1)
    (setq buffer-file-name (concat deb-view-file-name "-DATA"))
    (if (fboundp 'set-buffer-multibyte) (set-buffer-multibyte nil))
    (debview-mode)
    (message "deb-view processing deb file %s..." deb-view-buffer-name)
    (tar-next-line 1)
    (setq deb-view-dired-view-return-buffer return-buffer)
    (set-buffer-modified-p nil)
    (setq buffer-read-only t)
    (buffer-enable-undo)
    (switch-to-buffer-other-window data-buffer)
    (if new-archive-format (other-window 1))
    (shrink-window-if-larger-than-buffer)
    (when deb-view-tempfile
      (message "deb-view deleting tempfile: %s" debfile)
      (delete-file debfile))
    (message "deb-view: ? for help. q to quit.")))

;;;###autoload
(defun deb-view-mode ()
  "View mode for Debian Archive Files."
  (interactive)
  (let ((debfile buffer-file-name)
        (return-buffer (nth 0 (buffer-list)))
        (curbuf (current-buffer)))
    (setq deb-view-file-name debfile)
    (if (< (nth 1 (file-attributes debfile)) 0)
        (progn
          (message "deb-view remote file: %s" debfile)
          (setq debfile (make-temp-name "/tmp/deb-view."))
          ;;(message "deb-view processing deb file %s..." debfile)
          (write-file debfile)
          (setq deb-view-tempfile t))
      ;;(message "deb-view local file: %s" debfile)
      (setq deb-view-tempfile nil))
    (set-buffer return-buffer)
    (kill-buffer curbuf)
    (deb-view-process debfile)))

;;;###autoload
(defun deb-find ()
  "Search for deb files.
Use the method specified by the variable deb-find-method, and collect
output in a buffer.  See also the variable deb-find-directory.

This command uses a special history list, so you can
easily repeat a `deb-find' command."
  (interactive)
  (require 'compile)
  (let* ((deb-file-string (read-from-minibuffer "deb file to find: "
                                         nil nil nil 'deb-find-history))
         (output-buffer-name "*deb-find*")
         (command (cond ((string-equal deb-find-method "locate")
                         (concat "locate '" deb-file-string
                                 "' | egrep '\.deb$'"))
                        ((string-equal deb-find-method "find")
                         (concat "find " deb-find-directory "/* | egrep '"
                                 deb-file-string "' | egrep '\.deb$'"))
                        (t
                         (concat deb-find-method " '" deb-file-string "'")))))
    (compile-internal command "Not applicable in deb-find" "deb-find" nil nil
                      (function (lambda (mode) output-buffer-name)))
    (switch-to-buffer-other-window output-buffer-name)
    (setq deb-view-find-minor-mode-map
          (copy-keymap compilation-minor-mode-map))
    (use-local-map deb-view-find-minor-mode-map)
    (define-key deb-view-find-minor-mode-map [mouse-2]
      'deb-find-mouse-deb-view)
    (define-key deb-view-find-minor-mode-map "\C-c\C-c" 'deb-find-deb-view)
    (define-key deb-view-find-minor-mode-map "\C-m"     'deb-find-deb-view)
    (define-key deb-view-find-minor-mode-map "?"        'deb-find-help)
    (define-key deb-view-find-minor-mode-map "q"        'kill-this-buffer)
    (define-key deb-view-find-minor-mode-map "\M-n" 'undefined)
    (define-key deb-view-find-minor-mode-map "\M-p" 'undefined)
    (define-key deb-view-find-minor-mode-map "\M-{" 'undefined)
    (define-key deb-view-find-minor-mode-map "\M-}" 'undefined)
    (beginning-of-buffer)
    (message "deb-view: ? for help. q to quit.")))


;;; Internal functions:

(defvar deb-view-version "1.9"
  "The version of `deb-view'.")

(defun deb-view-version ()
  "Return string describing the version of `deb-view'.
When called interactively, displays the version."
  (interactive)
  (if (interactive-p)
      (message "deb-view version %s" (deb-view-version))
    deb-view-version))

(defun deb-view-dired-view-cleanup (&optional buffer)
  "Delete the buffers created by deb-view-dired-view."
  (interactive)
  (let* ((quit-buffer (or buffer (current-buffer)))
         (bufname (buffer-name quit-buffer))
         (debfile (substring bufname 0 (- (length bufname) 5)))
         (info-buffer (get-buffer (concat debfile "-INFO")))
         (data-buffer (get-buffer (concat debfile "-DATA")))
         (ddir-buffer (save-excursion
                        (set-buffer quit-buffer)
                        deb-view-dired-view-return-buffer)))
    (delete-other-windows)
    (and (buffer-live-p info-buffer)
         (kill-buffer info-buffer))
    (and (buffer-live-p data-buffer)
         (kill-buffer data-buffer))
    (and (buffer-live-p quit-buffer)
         (kill-buffer quit-buffer))
    (and (buffer-live-p ddir-buffer)
         (switch-to-buffer ddir-buffer))))

(defun deb-find-help ()
  "Show help information for `deb-find'."
  (interactive)
  (with-output-to-temp-buffer "*Help*"
    (princ (format "deb-find mode:    version %s" (deb-view-version)))
    (princ "\n
RET     - view the deb file on this line with deb-view.
C-c C-c - view the deb file on this line with deb-view.
mouse-2 - view the deb file on this line with deb-view.
? - show deb-find-help.
q - quit deb-find.")
    (save-excursion
      (set-buffer standard-output)
      (help-mode))
    (print-help-return-message)))

(defun deb-view-help ()
  "Show help information for `deb-view'."
  (interactive)
  (with-output-to-temp-buffer "*Help*"
    (princ (format "deb-view mode:    version %s" (deb-view-version)))
    (princ "
Derived from tar-mode, with additional features for viewing deb files.
Execute \"^h m\" to see tar-mode bindings.

You are shown two tar files in tar-mode (see tar-mode for help).
In the case of old .deb format files, the control info is shown
but not the other files of control.tar, such as install scripts.

Note that regular tar-mode commands e, f and RETURN show raw files
without any special uncompressing or formatting.

Additional features that deb-view adds to tar-mode:
? - show deb-view help.
q - kill both view buffers (INFO and DATA) and return to the
    dired buffer if that's where you executed deb-mode.
v - executes deb-view-tar-view instead of tar-view, with the
    additional smarts to uncompress .gz and .Z files for viewing.
N - Like in dired, formats man pages for viewing, with the
    additional smarts to uncompress .gz and .Z man files for viewing.
W - use w3-mode to view an HTML file.
These functions are also available in tar-mode on normal tar files
when deb-view is loaded.

To view files not supported by deb-view, such as graphics, use the
copy command in tar-mode (\"c\") to copy the file to a temp directory.
You can then do what you want to the file.")
    (save-excursion
      (set-buffer standard-output)
      (help-mode))
    (print-help-return-message)))

(defun deb-view-tar-man ()
  "*In Tar mode, view the tar file entry on this line as a man page."
  (interactive)
  (require 'man)
  (let ((auto-mode-alist
         (append '(("\\.gz$" . deb-view-tar-uncompress-while-visiting)
                   ("\\.Z$"  . deb-view-tar-uncompress-while-visiting)
                   ) auto-mode-alist)))
    (tar-extract 'view)
    (setq buffer-read-only nil)
    (shell-command-on-region (point-min) (point-max) "nroff -man -h " t t)
    (Man-cleanup-manpage)
    (setq buffer-read-only t)
    (set-buffer-modified-p nil)
    (message "")))

(defun deb-view-tar-uncompress-while-visiting ()
  "Temporary \"major mode\" used for .Z and .gz files, to uncompress them.
It then selects a major mode from the uncompressed file name and contents.
\(Modifed uncompress-while-visiting from uncompress.el\)"
  (interactive)
  (message "Uncompressing...")
  (let ((buffer-read-only nil))
    (shell-command-on-region (point-min) (point-max)
                             deb-view-tar-uncompress-program t))
  (message "Uncompressing...done")
  (set-buffer-modified-p nil)
  (goto-char 1))

(defun deb-view-tar-view ()
  "*In Tar mode, view the tar file entry on this line.
If the file is from the INFO buffer, then open in the other (larger) window."
  (interactive)
  (let ((auto-mode-alist
         (append '(("\\.gz$" . deb-view-tar-uncompress-while-visiting)
                   ("\\.Z$"  . deb-view-tar-uncompress-while-visiting)
                   ) auto-mode-alist)))
    (if (string-match "INFO$" buffer-file-name)
        (tar-extract-other-window)
      (tar-extract 'view))))

(defun deb-view-tar-w3 ()
  "*In Tar mode, view the tar file entry on this line as HTML with w3-mode."
  (interactive)
  (if (fboundp 'w3-preview-this-buffer)
      (let ((auto-mode-alist
             (append '(("\\.gz$" . deb-view-tar-uncompress-while-visiting)
                       ("\\.Z$"  . deb-view-tar-uncompress-while-visiting)
                       ) auto-mode-alist)))
        (tar-extract 'view)
        (rename-buffer (concat " " (buffer-name)))
        (w3-preview-this-buffer)
        (define-key w3-mode-map "q"	'deb-view-tar-w3-quit))
    (error "Sorry, you don't seem to have w3 loaded")))

(defun deb-view-tar-w3-quit ()
  "Quit WWW mode in a buffer from `deb-view'."
  (interactive)
  (let ((x w3-current-last-buffer))
    (and (fboundp 'w3-mpeg-kill-processes) (w3-mpeg-kill-processes))
    (kill-buffer (current-buffer))
    (if (and (bufferp x) (buffer-name x))
	(if w3-mutable-windows (pop-to-buffer x) (switch-to-buffer x))))
  (view-exit))

(defvar deb-find-history nil
  "History list for `deb-find' commands.")

(defvar deb-find-regexp "^/.*\.deb$"
  "Regexp for deb file names in the `deb-find' buffer.")

(defun deb-find-deb-view ()
  "Run `deb-view' in package under point."
  (interactive)
  (let ((deb-file (thing-at-point 'filename)))
    (if (and deb-file
             (string-match deb-find-regexp deb-file))
        (deb-view (thing-at-point 'filename))
      (error "No deb file on this line"))))

(defun deb-find-mouse-deb-view (event)
  "Run `deb-view' in package under mouse EVENT."
  (interactive "e")
  (pop-to-buffer (window-buffer (posn-window (event-end event))))
  (goto-char (posn-point (event-end event)))
  (let ((deb-file (thing-at-point 'filename)))
    (if (and deb-file
             (string-match deb-find-regexp deb-file))
        (deb-view (thing-at-point 'filename))
      (error "No deb file on this line"))))

(provide 'deb-view)

;;; deb-view.el ends here