mirrored from git://git.sv.gnu.org/emacs.git
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathdoc-view.el
2648 lines (2396 loc) · 108 KB
/
doc-view.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; doc-view.el --- Document viewer for Emacs -*- lexical-binding: t -*-
;; Copyright (C) 2007-2025 Free Software Foundation, Inc.
;;
;; Author: Tassilo Horn <tsdh@gnu.org>
;; Keywords: files, pdf, ps, dvi, djvu, epub, cbz, fb2, xps, openxps
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
;;; Requirements:
;; Viewing PS/PDF/DVI files requires Ghostscript, `dvipdf' (comes with
;; Ghostscript) or `dvipdfm' (comes with teTeX or TeXLive) and
;; `pdftotext', which comes with xpdf (https://www.foolabs.com/xpdf/)
;; or poppler (https://poppler.freedesktop.org/). EPUB, CBZ, FB2, XPS
;; and OXPS documents require `mutool' which comes with mupdf
;; (https://mupdf.com/index.html). DjVu documents require `ddjvu'
;; (from DjVuLibre). ODF files require `soffice' (from LibreOffice).
;; `djvused' (from DjVuLibre) can be optionally used to generate imenu
;; outline for DjVu documents when available.
;;; Commentary:
;; DocView is a document viewer for Emacs. It converts a number of
;; document formats (including PDF, PS, DVI, Djvu, ODF, EPUB, CBZ,
;; FB2, XPS and OXPS files) to a set of PNG (or TIFF for djvu) files,
;; one image for each page, and displays the images inside an Emacs
;; buffer. This buffer uses `doc-view-mode' which provides convenient
;; key bindings for browsing the document.
;;
;; To use it simply open a document file with
;;
;; C-x C-f ~/path/to/document RET
;;
;; and the document will be converted and displayed, if your Emacs supports PNG
;; images. With `C-c C-c' you can toggle between the rendered images
;; representation and the source text representation of the document.
;;
;; Since conversion may take some time all the PNG images are cached in a
;; subdirectory of `doc-view-cache-directory' and reused when you want to view
;; that file again. To reconvert a document hit `g' (`doc-view-reconvert-doc')
;; when displaying the document. To delete all cached files use
;; `doc-view-clear-cache'. To open the cache with Dired, so that you can tidy
;; it out use `doc-view-dired-cache'.
;;
;; When conversion is underway the first page will be displayed as soon as it
;; is available and the available pages are refreshed every
;; `doc-view-conversion-refresh-interval' seconds. If that variable is nil the
;; pages won't be displayed before conversion of the document finished
;; completely.
;;
;; DocView lets you select a slice of the displayed pages. This slice
;; will be remembered and applied to all pages of the current
;; document. This enables you to cut away the margins of a document
;; to save some space. To select a slice you can use
;; `doc-view-set-slice' (bound to `c s') which will query you for the
;; coordinates of the slice's top-left corner and its width and
;; height. A much more convenient way to do the same is offered by
;; the command `doc-view-set-slice-using-mouse' (bound to `c m').
;; After invocation you only have to press mouse-1 at the top-left
;; corner and drag it to the bottom-right corner of the desired slice.
;; Even more accurate and convenient is to use
;; `doc-view-set-slice-from-bounding-box' (bound to `c b') which uses
;; the BoundingBox information of the current page to set an optimal
;; slice. To reset the slice use `doc-view-reset-slice' (bound to `c
;; r').
;;
;; You can also search within the document. The command `doc-view-search'
;; (bound to `C-s') queries for a search regexp and initializes a list of all
;; matching pages and messages how many match-pages were found. After that you
;; can jump to the next page containing a match with an additional `C-s'. With
;; `C-r' you can do the same, but backwards. To search for a new regexp give a
;; prefix arg to one of the search functions, e.g. by typing `C-u C-s'. The
;; searching works by using a plain text representation of the document. If
;; that doesn't already exist the first invocation of `doc-view-search' (or
;; `doc-view-search-backward') starts the conversion. When that finishes and
;; you're still viewing the document (i.e. you didn't switch to another buffer)
;; you're queried for the regexp then.
;;
;; Dired users can simply hit `v' on a document file. If it's a PS, PDF or DVI
;; it will be opened using `doc-view-mode'.
;;
;;; Configuration:
;; If the images are too small or too big you should set the "-rXXX" option in
;; `doc-view-ghostscript-options' to another value. (The bigger your screen,
;; the higher the value.)
;;
;; This and all other options can be set with the customization interface.
;; Simply do
;;
;; M-x customize-group RET doc-view RET
;;
;; and modify them to your needs.
;;; Todo:
;; - add print command.
;; - share more code with image-mode.
;; - better menu.
;; - Bind slicing to a drag event.
;; - zoom the region around the cursor (like xdvi).
;; - get rid of the silly arrow in the fringe.
;; - improve anti-aliasing (pdf-utils gets it better).
;;;; About isearch support
;; I tried implementing isearch by setting
;; `isearch-search-fun-function' buffer-locally, but that didn't
;; work too good. The function doing the real search was called
;; endlessly somehow. But even if we'd get that working no real
;; isearch feeling comes up due to the missing match highlighting.
;; Currently I display all lines containing a match in a tooltip and
;; each C-s or C-r jumps directly to the next/previous page with a
;; match. With isearch we could only display the current match. So
;; we had to decide if another C-s jumps to the next page with a
;; match (thus only the first match in a page will be displayed in a
;; tooltip) or to the next match, which would do nothing visible
;; (except the tooltip) if the next match is on the same page.
;; And it's much slower than the current search facility, because
;; isearch really searches for each step forward or backward whereas
;; the current approach searches once and then it knows to which
;; pages to jump.
;; Anyway, if someone with better isearch knowledge wants to give it a try,
;; feel free to do it. --Tassilo
;;; Code:
(require 'cl-lib)
(require 'dired)
(require 'image-mode)
(require 'jka-compr)
(require 'filenotify)
(eval-when-compile (require 'subr-x))
(autoload 'imenu-unavailable-error "imenu")
;;;; Customization Options
(defgroup doc-view nil
"In-buffer document viewer.
The viewer handles PDF, PostScript, DVI, DJVU, ODF, EPUB, CBZ,
FB2, XPS and OXPS files, if the appropriate converter programs
are available (see Info node `(emacs)Document View')."
:link '(function-link doc-view)
:version "22.2"
:group 'applications
:group 'data
:group 'multimedia
:prefix "doc-view-")
(defcustom doc-view-ghostscript-program
(cond
((memq system-type '(windows-nt ms-dos))
(cond
;; Windows Ghostscript
((executable-find "gswin64c") "gswin64c")
((executable-find "gswin32c") "gswin32c")
;; The GS wrapper coming with TeX Live
((executable-find "rungs") "rungs")
;; The MikTeX builtin GS Check if mgs is functional for external
;; non-MikTeX apps. Was available under:
;; http://blog.miktex.org/post/2005/04/07/Starting-mgsexe-at-the-DOS-Prompt.aspx
((and (executable-find "mgs")
(eql 0 (shell-command "mgs -q -dNODISPLAY -c quit")))
"mgs")))
(t "gs"))
"Program to convert PS and PDF files to PNG."
:type 'file
:version "27.1")
(defcustom doc-view-pdfdraw-program
(cond
((executable-find "mutool") "mutool")
((executable-find "pdfdraw") "pdfdraw")
((executable-find "mudraw") "mudraw")
(t "mudraw"))
"Name of MuPDF's program to convert PDF files to PNG."
:type 'file
:version "31.1")
(defcustom doc-view-pdftotext-program-args '("-raw")
"Parameters to give to the pdftotext command."
:version "27.1"
:type '(repeat string))
(defcustom doc-view-pdf->png-converter-function
(if (executable-find doc-view-pdfdraw-program)
#'doc-view-pdf->png-converter-mupdf
#'doc-view-pdf->png-converter-ghostscript)
"Function to call to convert a PDF file into a PNG file."
:type '(radio
(function-item :doc "Use Ghostscript"
doc-view-pdf->png-converter-ghostscript)
(function-item :doc "Use MuPDF"
doc-view-pdf->png-converter-mupdf)
function)
:version "24.4")
(defcustom doc-view-mupdf-use-svg (image-type-available-p 'svg)
"Whether to use svg images for PDF files."
:type 'boolean
:version "30.1")
(defcustom doc-view-djvused-program (and (executable-find "djvused")
"djvused")
"Name of \"djvused\" program to generate imenu outline for DjVu files.
This is part of DjVuLibre."
:type '(choice (const nil) file)
:version "31.1")
(defcustom doc-view-imenu-enabled (and (or (executable-find "mutool")
(executable-find "djvused"))
t)
"Whether to generate imenu outline for PDF and DjVu files.
This uses \"mutool\" for PDF files and \"djvused\" for DjVu files."
:type 'boolean
:version "31.1")
(make-obsolete-variable 'doc-view-imenu-enabled
"Imenu index is generated unconditionally when available."
"31.1")
(defcustom doc-view-imenu-title-format "%t (%p)"
"Format spec for imenu's display of section titles from docview documents.
The special markers '%t' and '%p' are replaced by the section
title and page number in this format string, which uses
`format-spec'.
For instance, setting this variable to \"%t\" will produce items
showing only titles and no page number."
:type 'string
:version "29.1")
(defcustom doc-view-imenu-flatten nil
"Whether to flatten the list of sections in an imenu or show it nested."
:type 'boolean
:version "29.1")
(defface doc-view-svg-face '((t :inherit default
:background "white"
:foreground "black"))
"Face used for SVG images.
See `doc-view-mupdf-use-svg'.
Only background and foreground colors are used as the SVG image's
descriptors; see (info \"(elisp) SVG Images\"). Custom values may
cause low-contrast issues with certain documents."
:version "30.1")
(make-obsolete 'doc-view-svg-background 'doc-view-svg-face "30.1")
(make-obsolete 'doc-view-svg-foreground 'doc-view-svg-face "30.1")
(defcustom doc-view-ghostscript-options
'("-dSAFER" ;; Avoid security problems when rendering files from untrusted
;; sources.
"-dNOPAUSE" "-dTextAlphaBits=4"
"-dBATCH" "-dGraphicsAlphaBits=4" "-dQUIET")
"A list of options to give to ghostscript."
:type '(repeat string))
(defcustom doc-view-ghostscript-device "png16m"
"Output device to give to ghostscript."
:type 'string
:version "27.1")
(defcustom doc-view-resolution 100
"Dots per inch resolution used to render the documents.
Higher values result in larger images."
:type 'natnum)
(defvar doc-view-doc-type nil
"The type of document in the current buffer.
Can be `dvi', `pdf', `ps', `djvu', `odf', `epub', `cbz', `fb2',
`xps' or `oxps'.")
(defvar doc-view--epub-stylesheet-watcher nil
"File watcher for `doc-view-epub-user-stylesheet'.")
(defun doc-view--epub-reconvert (&optional _event)
"Reconvert all epub buffers.
EVENT is unused, but necessary to work with the filenotify API."
(dolist (x (buffer-list))
(with-current-buffer x
(when (eq doc-view-doc-type 'epub)
(doc-view-reconvert-doc)))))
(defun doc-view-custom-set-epub-user-stylesheet (option-name new-value)
"Setter for `doc-view-epub-user-stylesheet'.
Reconverts existing epub buffers when the file used as a user
stylesheet is switched, or its contents modified."
(set-default option-name new-value)
(file-notify-rm-watch doc-view--epub-stylesheet-watcher)
(doc-view--epub-reconvert)
(setq doc-view--epub-stylesheet-watcher
(when new-value
(file-notify-add-watch new-value '(change) #'doc-view--epub-reconvert))))
(defcustom doc-view-epub-user-stylesheet nil
"User stylesheet to use when converting EPUB documents to PDF."
:type '(choice (const nil)
(file :must-match t))
:version "29.1"
:set #'doc-view-custom-set-epub-user-stylesheet)
(defun doc-view-custom-set-epub-font-size (option-name new-value)
(set-default option-name new-value)
(doc-view--epub-reconvert))
;; FIXME: The doc-view-current-* definitions below are macros because they
;; map to accessors which we want to use via `setf' as well!
(defmacro doc-view-current-page (&optional win)
`(image-mode-window-get 'page ,win))
(defmacro doc-view-current-info () '(image-mode-window-get 'info))
(defmacro doc-view-current-overlay () '(image-mode-window-get 'overlay))
(defmacro doc-view-current-image () '(image-mode-window-get 'image))
(defmacro doc-view-current-slice () '(image-mode-window-get 'slice))
(defvar-local doc-view--current-cache-dir nil
"Only used internally.")
(defcustom doc-view-epub-font-size nil
"Font size in points for EPUB layout."
:type '(choice (const nil) integer)
:set #'doc-view-custom-set-epub-font-size
:version "29.1")
(defcustom doc-view-scale-internally t
"Whether we should try to rescale images ourselves.
If nil, the document is re-rendered every time the scaling factor is modified.
This only has an effect if the image libraries linked with Emacs support
scaling."
:version "24.4"
:type 'boolean)
(defcustom doc-view-image-width 850
"Default image width.
Has only an effect if `doc-view-scale-internally' is non-nil and support for
scaling is compiled into Emacs."
:version "24.1"
:type 'natnum)
(defcustom doc-view-dvipdfm-program "dvipdfm"
"Program to convert DVI files to PDF.
DVI file will be converted to PDF before the resulting PDF is
converted to PNG.
If this and `doc-view-dvipdf-program' are set,
`doc-view-dvipdf-program' will be preferred."
:type 'file)
(defcustom doc-view-dvipdf-program "dvipdf"
"Program to convert DVI files to PDF.
DVI file will be converted to PDF before the resulting PDF is
converted to PNG.
If this and `doc-view-dvipdfm-program' are set,
`doc-view-dvipdf-program' will be preferred."
:type 'file)
(define-obsolete-variable-alias 'doc-view-unoconv-program 'doc-view-odf->pdf-converter-program "24.4")
(defcustom doc-view-odf->pdf-converter-program
(cond
((executable-find "soffice") "soffice")
((executable-find "unoconv") "unoconv")
(t "soffice"))
"Program to convert any file type readable by OpenOffice.org to PDF.
Needed for viewing OpenOffice.org (and MS Office) files."
:version "24.4"
:type 'file)
(defcustom doc-view-odf->pdf-converter-function
(if (string-suffix-p "unoconv" doc-view-odf->pdf-converter-program)
#'doc-view-odf->pdf-converter-unoconv
#'doc-view-odf->pdf-converter-soffice)
"Function to call to convert an ODF file into a PDF file."
:type '(radio
(function-item :doc "Use LibreOffice"
doc-view-odf->pdf-converter-soffice)
(function-item :doc "Use unoconv"
doc-view-odf->pdf-converter-unoconv)
function)
:version "24.4")
(defcustom doc-view-ps2pdf-program "ps2pdf"
"Program to convert PS files to PDF.
PS files will be converted to PDF before searching is possible."
:type 'file)
(defcustom doc-view-pdftotext-program "pdftotext"
"Program to convert PDF files to plain text.
Needed for searching."
:type 'file)
(defcustom doc-view-cache-directory
(expand-file-name (format "docview%d" (user-uid))
temporary-file-directory)
"The base directory, where the PNG images will be saved."
:type 'directory)
(defvar doc-view-conversion-buffer " *doc-view conversion output*"
"The buffer where messages from the converter programs go to.")
(defcustom doc-view-conversion-refresh-interval 1
"Interval in seconds between refreshes of the DocView buffer while converting.
After such a refresh newly converted pages will be available for
viewing. If set to nil there won't be any refreshes and the
pages won't be displayed before conversion of the whole document
has finished."
:type '(choice natnum
(const :value nil :tag "No refreshes")))
(defcustom doc-view-continuous nil
"In Continuous mode reaching the page edge advances to next/previous page.
When non-nil, scrolling a line upward at the bottom edge of the page
moves to the next page, and scrolling a line downward at the top edge
of the page moves to the previous page."
:type 'boolean
:version "23.2")
;;;; Internal Variables
(defvar-local doc-view--current-converter-processes nil
"Only used internally.")
(defun doc-view-new-window-function (winprops)
;; (message "New window %s for buf %s" (car winprops) (current-buffer))
;;
;; If the window configuration changed, `image-mode-reapply-winprops'
;; will have erased any previous property list for this window, but
;; without removing existing overlays for the same, so that they must
;; be located and erased before a new overlay is created.
(dolist (tem (car (overlay-lists)))
(when (and (eq (overlay-get tem 'window) (car winprops))
(overlay-get tem 'doc-view))
(delete-overlay tem)))
(cl-assert (or (eq t (car winprops))
(eq (window-buffer (car winprops)) (current-buffer))))
(let ((ol (image-mode-window-get 'overlay winprops)))
(if ol
(progn
(setq ol (copy-overlay ol))
;; `ol' might actually be dead.
(move-overlay ol (point-min) (point-max)))
(setq ol (make-overlay (point-min) (point-max) nil t))
(overlay-put ol 'doc-view t))
(overlay-put ol 'window (car winprops))
(unless (windowp (car winprops))
;; It's a pseudo entry. Let's make sure it's not displayed (the
;; `window' property is only effective if its value is a window).
(cl-assert (eq t (car winprops)))
(delete-overlay ol))
(image-mode-window-put 'overlay ol winprops)
(when (and (windowp (car winprops))
(stringp (overlay-get ol 'display))
(null doc-view--current-converter-processes))
;; We're not displaying an image yet, so let's do so. This happens when
;; the buffer is displayed for the first time.
;; Don't do it if there's a conversion is running, since in that case, it
;; will be done later.
(with-selected-window (car winprops)
(doc-view-goto-page (image-mode-window-get 'page t))))))
(defvar-local doc-view--current-files nil
"Only used internally.")
(defvar-local doc-view--current-timer nil
"Only used internally.")
(defvar-local doc-view--current-search-matches nil
"Only used internally.")
(defvar doc-view--pending-cache-flush nil
"Only used internally.")
(defvar doc-view--buffer-file-name nil
"Only used internally.
The file name used for conversion. Normally it's the same as
`buffer-file-name', but for remote files, compressed files and
files inside an archive it is a temporary copy of
the (uncompressed, extracted) file residing in
`doc-view-cache-directory'.")
(defvar doc-view-single-page-converter-function nil
"Function to call to convert a single page of the document to a bitmap file.
May operate on the source document or on some intermediate (typically PDF)
conversion of it.")
(defvar-local doc-view--image-type nil
"The type of image in the current buffer.
Can be `png' or `tiff'.")
(defvar-local doc-view--image-file-pattern nil
"The `format' pattern of image file names.
Typically \"page-%s.png\".")
;;;; DocView Keymaps
(defvar-keymap doc-view-mode-map
:doc "Keymap used by `doc-view-mode' when displaying a doc as a set of images."
:parent image-mode-map
;; Navigation in the document
"n" #'doc-view-next-page
"p" #'doc-view-previous-page
"<next>" #'forward-page
"<prior>" #'backward-page
"<remap> <forward-page>" #'doc-view-next-page
"<remap> <backward-page>" #'doc-view-previous-page
"SPC" #'doc-view-scroll-up-or-next-page
"S-SPC" #'doc-view-scroll-down-or-previous-page
"DEL" #'doc-view-scroll-down-or-previous-page
"C-n" #'doc-view-next-line-or-next-page
"<down>" #'doc-view-next-line-or-next-page
"C-p" #'doc-view-previous-line-or-previous-page
"<up>" #'doc-view-previous-line-or-previous-page
"M-<" #'doc-view-first-page
"M->" #'doc-view-last-page
"<remap> <goto-line>" #'doc-view-goto-page
"RET" #'image-next-line
;; Zoom in/out.
"+" #'doc-view-enlarge
"=" #'doc-view-enlarge
"-" #'doc-view-shrink
"0" #'doc-view-scale-reset
"<remap> <text-scale-adjust>" #'doc-view-scale-adjust
;; Fit the image to the window
"W" #'doc-view-fit-width-to-window
"H" #'doc-view-fit-height-to-window
"P" #'doc-view-fit-page-to-window
"F" #'doc-view-fit-window-to-page ;F = frame
;; Killing the buffer (and the process)
"k" #'image-kill-buffer
"K" #'doc-view-kill-proc
;; Slicing the image
"c s" #'doc-view-set-slice
"c m" #'doc-view-set-slice-using-mouse
"c b" #'doc-view-set-slice-from-bounding-box
"c r" #'doc-view-reset-slice
;; Centering the image
"c h" #'doc-view-center-page-horizontally
"c v" #'doc-view-center-page-vertically
;; Searching
"C-s" #'doc-view-search
"<find>" #'doc-view-search
"C-r" #'doc-view-search-backward
;; Show the tooltip
"C-t" #'doc-view-show-tooltip
;; Toggle between text and image display or editing
"C-c C-c" #'doc-view-toggle-display
;; Open a new buffer with doc's text contents
"C-c C-t" #'doc-view-open-text
"r" #'revert-buffer
;; Registers
"m" #'doc-view-page-to-register
"'" #'doc-view-jump-to-register)
(define-obsolete-function-alias 'doc-view-revert-buffer #'revert-buffer "27.1")
(defvar revert-buffer-preserve-modes)
(defun doc-view--revert-buffer (orig-fun &rest args)
"Preserve the buffer's current mode and check PDF sanity."
(if (< undo-outer-limit (* 2 (buffer-size)))
;; It's normal for this operation to result in a very large undo entry.
(setq-local undo-outer-limit (* 2 (buffer-size))))
(cl-labels ((revert ()
(let ((revert-buffer-preserve-modes t))
(apply orig-fun args)
;; Update the cached version of the pdf file, too.
;; This is the one that's used when rendering
;; (bug#26996). doc-view--buffer-file-name is nil in
;; the case where we've switched to the editing mode
;; (bug#76478). In that case, we'll update the cached
;; version when switching back to doc-view-mode.
(when (and doc-view--buffer-file-name
(not (equal buffer-file-name
doc-view--buffer-file-name)))
;; FIXME: Lars says he needed to recreate
;; the dir, we should figure out why.
(doc-view-make-safe-dir doc-view-cache-directory)
(write-region nil nil doc-view--buffer-file-name)))))
(if (and (eq 'pdf doc-view-doc-type)
(executable-find "pdfinfo"))
;; We don't want to revert if the PDF file is corrupted which
;; might happen when it is currently recompiled from a tex
;; file. (TODO: We'd like to have something like that also
;; for other types, at least PS, but I don't know a good way
;; to test if a PS file is complete.)
(if (eql 0 (call-process "pdfinfo" nil nil nil
doc-view--buffer-file-name))
(revert)
(when (called-interactively-p 'interactive)
(message "Can't revert right now because the file is corrupted.")))
(revert))))
(easy-menu-define doc-view-menu doc-view-mode-map
"Menu for Doc View mode."
'("DocView"
["Next page" doc-view-next-page
:help "Go to the next page"]
["Previous page" doc-view-previous-page
:help "Go to the previous page"]
("Other Navigation"
["Go to page..." doc-view-goto-page
:help "Go to specific page"]
"---"
["First page" doc-view-first-page
:help "View the first page"]
["Last page" doc-view-last-page
:help "View the last page"]
"---"
["Move forward" doc-view-scroll-up-or-next-page
:help "Scroll page up or go to next page"]
["Move backward" doc-view-scroll-down-or-previous-page
:help "Scroll page down or go to previous page"])
("Continuous Scrolling"
["Off" (setq doc-view-continuous nil)
:style radio :selected (eq doc-view-continuous nil)
:help "Scrolling stops at page beginning and end"]
["On" (setq doc-view-continuous t)
:style radio :selected (eq doc-view-continuous t)
:help "Scrolling continues to next or previous page"]
"---"
["Save as Default" (customize-save-variable 'doc-view-continuous doc-view-continuous)
:help "Save current continuous scrolling option as default"]
)
"---"
("Toggle edit/display"
["Edit document" doc-view-toggle-display
:style radio :selected (eq major-mode 'doc-view--text-view-mode)]
["Display document" (lambda ()) ; ignore but show no keybinding
:style radio :selected (eq major-mode 'doc-view-mode)])
("Adjust Display"
["Fit to window" doc-view-fit-page-to-window
:help "Fit the image to the window"]
["Fit width" doc-view-fit-width-to-window
:help "Fit the image width to the window width"]
["Fit height" doc-view-fit-height-to-window
:help "Fit the image height to the window height"]
"---"
["Enlarge" doc-view-enlarge
:help "Enlarge the document"]
["Shrink" doc-view-shrink
:help "Shrink the document"]
"---"
["Set Slice" doc-view-set-slice-using-mouse
:help "Set the slice of the images that should be displayed"]
["Set Slice (BoundingBox)" doc-view-set-slice-from-bounding-box
:help "Set the slice from the document's BoundingBox information"]
["Set Slice (manual)" doc-view-set-slice
:help "Set the slice of the images that should be displayed"]
["Reset Slice" doc-view-reset-slice
:help "Reset the current slice"
:enabled (image-mode-window-get 'slice)])
"---"
["New Search" doc-view-new-search
:help "Initiate a new search"]
["Search Forward" doc-view-search
:help "Jump to the next match or initiate a new search"]
["Search Backward" doc-view-search-backward
:help "Jump to the previous match or initiate a new search"]
))
(defvar-keymap doc-view-minor-mode-map
:doc "Keymap used by `doc-view-minor-mode'."
;; Toggle between text and image display or editing
"C-c C-c" #'doc-view-toggle-display)
(easy-menu-define doc-view-minor-mode-menu doc-view-minor-mode-map
"Menu for Doc View minor mode."
'("DocView (edit)"
("Toggle edit/display"
["Edit document" (lambda ()) ; ignore but show no keybinding
;; This is always selected since its menu is singular to the
;; display minor mode.
:style radio :selected t]
["Display document" doc-view-toggle-display
:style radio :selected (eq major-mode 'doc-view-mode)])
["Exit DocView Mode" doc-view-minor-mode]))
(defvar doc-view-tool-bar-map
(let ((map (make-sparse-keymap)))
;; Most of these items are the same as in the default tool bar
;; map, but with extraneous items removed, and with extra search
;; and navigation items.
(tool-bar-local-item-from-menu 'find-file "new" map
nil :label "New File"
:vert-only t)
(tool-bar-local-item-from-menu 'menu-find-file-existing "open" map
nil :label "Open" :vert-only t)
(tool-bar-local-item-from-menu 'dired "diropen" map nil :vert-only t)
(tool-bar-local-item-from-menu 'kill-this-buffer "close" map nil
:vert-only t)
(define-key-after map [separator-1] menu-bar-separator)
(tool-bar-local-item-from-menu 'doc-view-new-search "search"
map doc-view-mode-map :vert-only t
:help "Start a new search query.")
(tool-bar-local-item-from-menu 'doc-view-search-backward "left-arrow"
map doc-view-mode-map
:vert-only t
:enable 'doc-view--current-search-matches
:help "Move to the last search result.")
(tool-bar-local-item-from-menu 'doc-view-search "right-arrow"
map doc-view-mode-map :vert-only t
:enable 'doc-view--current-search-matches
:help "Move to the next search result.")
(define-key-after map [separator-2] menu-bar-separator)
(tool-bar-local-item-from-menu 'doc-view-previous-page "last-page"
map doc-view-mode-map :vert-only t
:enable '(> (doc-view-current-page) 1)
:help "Move to the previous page.")
(tool-bar-local-item-from-menu 'doc-view-next-page "next-page"
map doc-view-mode-map :vert-only t
:enable '(< (doc-view-current-page)
(doc-view-last-page-number))
:help "Move to the next page.")
map)
"Like the default `tool-bar-map', but with additions for DocView.")
;;;; Navigation Commands
(defun doc-view-last-page-number ()
(length doc-view--current-files))
(defun doc-view-goto-page (page)
"View the page given by PAGE."
(interactive "nPage: ")
(let ((len (doc-view-last-page-number)))
(if (< page 1)
(setq page 1)
(when (and (> page len)
;; As long as the converter is running, we don't know
;; how many pages will be available.
(null doc-view--current-converter-processes))
(setq page len)))
(force-mode-line-update) ;To update `current-page'.
(setf (doc-view-current-page) page
(doc-view-current-info)
(concat
(propertize
(format "Page %d of %d." page len) 'face 'bold)
;; Tell user if converting isn't finished yet
(and doc-view--current-converter-processes
" (still converting...)")
;; Display context infos if this page matches the last search
(when (and doc-view--current-search-matches
(assq page doc-view--current-search-matches))
(concat "\n" (propertize "Search matches:" 'face 'bold)
(let ((contexts ""))
(dolist (m (cdr (assq page
doc-view--current-search-matches)))
(setq contexts (concat contexts "\n - \"" m "\"")))
contexts)))))
;; Update the buffer
;; We used to find the file name from doc-view--current-files but
;; that's not right if the pages are not generated sequentially
;; or if the page isn't in doc-view--current-files yet.
(let ((file (expand-file-name
(format doc-view--image-file-pattern page)
(doc-view--current-cache-dir))))
(doc-view-insert-image file :pointer 'arrow)
(when (and (not (file-exists-p file))
doc-view--current-converter-processes)
;; The PNG file hasn't been generated yet.
(funcall doc-view-single-page-converter-function
doc-view--buffer-file-name file page
(let ((win (selected-window)))
(lambda ()
(and (eq (current-buffer) (window-buffer win))
;; If we changed page in the mean
;; time, don't mess things up.
(eq (doc-view-current-page win) page)
;; Make sure we don't infloop.
(file-readable-p file)
(with-selected-window win
(doc-view-goto-page page))))))))
(overlay-put (doc-view-current-overlay)
'help-echo (doc-view-current-info))))
(defun doc-view-next-page (&optional arg)
"Browse ARG pages forward."
(interactive "p")
(doc-view-goto-page (+ (doc-view-current-page) (or arg 1))))
(defun doc-view-previous-page (&optional arg)
"Browse ARG pages backward."
(interactive "p")
(doc-view-goto-page (- (doc-view-current-page) (or arg 1))))
(defun doc-view-first-page ()
"View the first page."
(interactive)
(doc-view-goto-page 1))
(defun doc-view-last-page ()
"View the last page."
(interactive)
(doc-view-goto-page (doc-view-last-page-number)))
(defun doc-view-scroll-up-or-next-page (&optional arg)
"Scroll page up ARG lines if possible, else goto next page.
When `doc-view-continuous' is non-nil, scrolling upward
at the bottom edge of the page moves to the next page.
Otherwise, goto next page only on typing SPC (ARG is nil)."
(interactive "P")
(if (or doc-view-continuous (null arg))
(let ((hscroll (window-hscroll))
(cur-page (doc-view-current-page)))
(when (= (window-vscroll nil t) (image-scroll-up arg))
(doc-view-next-page)
(when (/= cur-page (doc-view-current-page))
(image-bob)
(image-bol 1))
(set-window-hscroll (selected-window) hscroll)))
(image-scroll-up arg)))
(defun doc-view-scroll-down-or-previous-page (&optional arg)
"Scroll page down ARG lines if possible, else goto previous page.
When `doc-view-continuous' is non-nil, scrolling downward
at the top edge of the page moves to the previous page.
Otherwise, goto previous page only on typing DEL (ARG is nil)."
(interactive "P")
(if (or doc-view-continuous (null arg))
(let ((hscroll (window-hscroll))
(cur-page (doc-view-current-page)))
(when (= (window-vscroll nil t) (image-scroll-down arg))
(doc-view-previous-page)
(when (/= cur-page (doc-view-current-page))
(image-eob)
(image-bol 1))
(set-window-hscroll (selected-window) hscroll)))
(image-scroll-down arg)))
(defun doc-view-next-line-or-next-page (&optional arg)
"Scroll upward by ARG lines if possible, else goto next page.
When `doc-view-continuous' is non-nil, scrolling a line upward
at the bottom edge of the page moves to the next page."
(interactive "p")
(if doc-view-continuous
(let ((hscroll (window-hscroll))
(cur-page (doc-view-current-page)))
(when (= (window-vscroll nil t) (image-next-line arg))
(doc-view-next-page)
(when (/= cur-page (doc-view-current-page))
(image-bob)
(image-bol 1))
(set-window-hscroll (selected-window) hscroll)))
(image-next-line arg)))
(defun doc-view-previous-line-or-previous-page (&optional arg)
"Scroll downward by ARG lines if possible, else goto previous page.
When `doc-view-continuous' is non-nil, scrolling a line downward
at the top edge of the page moves to the previous page."
(interactive "p")
(if doc-view-continuous
(let ((hscroll (window-hscroll))
(cur-page (doc-view-current-page)))
(when (= (window-vscroll nil t) (image-previous-line arg))
(doc-view-previous-page)
(when (/= cur-page (doc-view-current-page))
(image-eob)
(image-bol 1))
(set-window-hscroll (selected-window) hscroll)))
(image-previous-line arg)))
;;;; Utility Functions
(defun doc-view-kill-proc ()
"Kill the current converter process(es)."
(interactive)
(while (consp doc-view--current-converter-processes)
(ignore-errors ;; Some entries might not be processes, and maybe
; some are dead already?
(kill-process (pop doc-view--current-converter-processes))))
(when doc-view--current-timer
(cancel-timer doc-view--current-timer)
(setq doc-view--current-timer nil))
(setq mode-line-process nil))
(define-obsolete-function-alias 'doc-view-kill-proc-and-buffer
#'image-kill-buffer "25.1")
(defun doc-view-make-safe-dir (dir)
(condition-case nil
;; Create temp files with strict access rights. It's easy to
;; loosen them later, whereas it's impossible to close the
;; time-window of loose permissions otherwise.
(with-file-modes #o0700 (make-directory dir))
(file-already-exists
;; In case it was created earlier with looser rights.
;; We could check the mode info returned by file-attributes, but it's
;; a pain to parse and it may not tell you what we want under
;; non-standard file-systems. So let's just say what we want and let
;; the underlying C code and file-system figure it out.
;; This also ends up checking a bunch of useful conditions: it makes
;; sure we have write-access to the directory and that we own it, thus
;; closing a bunch of security holes.
(condition-case error
(set-file-modes dir #o0700 'nofollow)
(file-error
(error
(format "Unable to use temporary directory %s: %s"
dir (mapconcat #'identity (cdr error) " "))))))))
(defun doc-view--current-cache-dir ()
"Return the directory where the png files of the current doc should be saved.
It's a subdirectory of `doc-view-cache-directory'."
(if doc-view--current-cache-dir
doc-view--current-cache-dir
;; Try and make sure doc-view-cache-directory exists and is safe.
(doc-view-make-safe-dir doc-view-cache-directory)
;; Now compute the subdirectory to use.
(setq doc-view--current-cache-dir
(file-name-as-directory
(expand-file-name
(concat (thread-last
(file-name-nondirectory doc-view--buffer-file-name)
;; bug#13679
(subst-char-in-string ?% ?_)
;; arc-mode concatenates archive name and file name
;; with colon, which isn't allowed on MS-Windows.
(subst-char-in-string ?: ?_))
"-"
(let ((file doc-view--buffer-file-name))
(with-temp-buffer
(set-buffer-multibyte nil)
(insert-file-contents-literally file)
(md5 (current-buffer)))))
doc-view-cache-directory)))))
;;;###autoload
(defun doc-view-mode-p (type)
"Return non-nil if document type TYPE is available for `doc-view'.
Document types are symbols like `dvi', `ps', `pdf', `epub',
`cbz', `fb2', `xps', `oxps', or`odf' (any OpenDocument format)."
(and (display-graphic-p)
(image-type-available-p 'png)
(cond
((eq type 'dvi)
(and (doc-view-mode-p 'pdf)
(or (and doc-view-dvipdf-program
(executable-find doc-view-dvipdf-program))
(and doc-view-dvipdfm-program
(executable-find doc-view-dvipdfm-program)))))
((memq type '(postscript ps eps pdf))
(or (and doc-view-ghostscript-program
(executable-find doc-view-ghostscript-program))
;; for pdf also check for `doc-view-pdfdraw-program'
(when (eq type 'pdf)
(and doc-view-pdfdraw-program
(executable-find doc-view-pdfdraw-program)))))
((eq type 'odf)
(and (executable-find doc-view-odf->pdf-converter-program)
(doc-view-mode-p 'pdf)))
((eq type 'djvu)
(executable-find "ddjvu"))
((memq type '(epub cbz fb2 xps oxps))
;; first check if `doc-view-pdfdraw-program' is set to mutool
(and (string= doc-view-pdfdraw-program "mutool")
(executable-find "mutool")))
(t ;; unknown image type
nil))))
;;;; Conversion Functions
(defvar doc-view-shrink-factor 1.125)
(defun doc-view-enlarge (factor)
"Enlarge the document by FACTOR."
(interactive (list doc-view-shrink-factor))
(if doc-view-scale-internally
(let ((new (ceiling (* factor doc-view-image-width))))
(unless (equal new doc-view-image-width)
(setq-local doc-view-image-width new)
(doc-view-insert-image
(plist-get (cdr (doc-view-current-image)) :file)
:width doc-view-image-width)))