-
Notifications
You must be signed in to change notification settings - Fork 25
/
eaf-pdf-viewer.el
759 lines (663 loc) · 30.4 KB
/
eaf-pdf-viewer.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
;;; eaf-pdf-viewer.el --- PDF Viewer -*- lexical-binding: t; -*-
;; Filename: eaf-pdf-viewer.el
;; Description: PDF Viewer
;; Author: Andy Stewart <[email protected]>
;; Maintainer: Andy Stewart <[email protected]>
;; Copyright (C) 2021, Andy Stewart, all rights reserved.
;; Created: 2021-07-20 22:16:40
;; Version: 0.1
;; Last-Updated: Fri Jul 30 00:48:38 2021 (-0400)
;; By: Mingde (Matthew) Zeng
;; URL: http://www.emacswiki.org/emacs/download/eaf-pdf-viewer.el
;; Keywords:
;; Compatibility: GNU Emacs 28.0.50
;;
;; Features that might be required by this library:
;;
;;
;;
;;; This file is NOT part of GNU Emacs
;;; License
;;
;; This program 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, or (at your option)
;; any later version.
;; This program 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 this program; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth
;; Floor, Boston, MA 02110-1301, USA.
;;; Commentary:
;;
;; PDF Viewer
;;
;;; Installation:
;;
;; Put eaf-pdf-viewer.el to your load-path.
;; The load-path is usually ~/elisp/.
;; It's set in your ~/.emacs like this:
;; (add-to-list 'load-path (expand-file-name "~/elisp"))
;;
;; And the following to your ~/.emacs startup file.
;;
;; (require 'eaf-pdf-viewer)
;;
;; No need more.
;;; Customize:
(defgroup eaf-pdf-viewer nil
"The PDF viewer application of Emacs application framework."
:group 'eaf)
(defcustom eaf-pdf-extension-list
'("pdf" "xps" "oxps" "cbz" "epub" "fb2" "fbz")
"The extension list of pdf application."
:type 'list
:group 'eaf-pdf-viewer)
(defcustom eaf-office-extension-list
'("docx" "doc" "ppt" "pptx" "xlsx" "xls")
"The extension list of office application."
:type 'list
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-store-history t
"If it is t, the pdf file path will be stored in eaf-config-location/pdf/history/log.txt for eaf-open-pdf-from-history to use"
:type 'boolean
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-notify-file-changed t
"If it is t, pdf-viewer will notify that the displayed pdf file is changed. Otherwise the pdf-viewer buffer will be refreshed silently."
:type 'boolean
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-show-progress-on-page t
"If it is t, pdf-viewer will show progress (in percentage) and page number directly on the document."
:type 'boolean
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-dark-mode "ignore"
"Whether to enable inverted color rendering when starting the pdf-viewer app.
Possible values are
- \"follow\" Follow the background color of the theme of user.
- \"force\" Force inverted color rendering on start-up.
- \"ignore\" Don't do inverted rendering."
:type '(choice (string :tag "Force inverted color rendering." "force")
(string :tag "Follow the background color of user's theme." "follow")
(other :tag "Do normal rendering." "ignore"))
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-default-zoom 1.0
"The default zooming percentage when starting the pdf-viewer app."
:type 'float
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-zoom-step 0.2
"The ratio step of the current page size to perform zoom in, zoom out."
:type 'float
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-scroll-ratio 0.05
"The ratio of the page in each step when scrolling."
:type 'float
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-dark-exclude-image t
"Don't invert images when toggling inverted color rendering.
Nil means also invert images.
Non-nil means don't invert images."
:type 'boolean
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-marker-fontsize 8
"The font size used by pdf marker."
:type 'integer
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-inline-text-annot-fontsize 8
"The font size used by pdf inline text annot."
:type 'integer
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-inline-text-annot-color "#ec3f00"
"The color used by pdf inline text annot."
:type 'string
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-text-highlight-annot-color "#ffd815"
"The color used by pdf text highlighting annot."
:type 'string
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-text-underline-annot-color "#11e32a"
"The color used by pdf text underlining annot."
:type 'string
:group 'eaf-pdf-viewer)
(defcustom eaf-pdf-viewer-keybinding
'(("j" . "scroll_up")
("<down>" . "scroll_up")
("C-n" . "scroll_up")
("k" . "scroll_down")
("<up>" . "scroll_down")
("C-p" . "scroll_down")
("h" . "scroll_left")
("<left>" . "scroll_left")
("C-b" . "scroll_left")
("l" . "scroll_right")
("<right>" . "scroll_right")
("C-f" . "scroll_right")
("SPC" . "scroll_up_page")
("b" . "scroll_down_page")
("C-v" . "scroll_up_page")
("M-v" . "scroll_down_page")
("c" . "scroll_center_horizontal")
("t" . "toggle_read_mode")
("0" . "zoom_reset")
("=" . "zoom_in")
("-" . "zoom_out")
("w" . "zoom_fit_text_width")
("W" . "zoom_close_to_text_width")
("g" . "scroll_to_begin")
("G" . "scroll_to_end")
("p" . "jump_to_page")
("P" . "jump_to_percent")
("[" . "save_current_pos")
("]" . "jump_to_saved_pos")
("i" . "toggle_inverted_mode")
("C-i" . "toggle_inverted_image_mode")
("m" . "toggle_mark_link")
("f" . "jump_to_link")
("M-w" . "copy_select")
("C-s" . "search_text_forward")
("C-r" . "search_text_backward")
("C-/" . "undo_annot_action")
("C-?" . "redo_annot_action")
("x" . "close_buffer")
("z" . "eaf-ocr-buffer")
("r" . "reload_document")
("C-<right>" . "rotate_clockwise")
("C-<left>" . "rotate_counterclockwise")
("M-h" . "add_annot_highlight")
("M-u" . "add_annot_underline")
("M-s" . "add_annot_squiggly")
("M-d" . "add_annot_strikeout_or_delete_annot")
("M-t" . "add_annot_popup_text")
("M-T" . "add_annot_inline_text")
("M-e" . "edit_annot_text")
("M-r" . "move_annot_text")
("M-p" . "toggle_presentation_mode")
("<escape>" . "quit_presentation_mode")
("J" . "select_left_tab")
("K" . "select_right_tab")
("o" . "eaf-pdf-outline")
("O" . "eaf-pdf-outline-edit")
("T" . "toggle_trim_white_margin"))
"The keybinding of EAF PDF Viewer."
:type '(alist :key-type (string :tag "Key bindings (e.g. \"C-n\", \"<f4>\", etc.)")
:value-type (string :tag "Function name"))
:group 'eaf-pdf-viewer)
;;
;; All of the above can customize by:
;; M-x customize-group RET eaf-pdf-viewer RET
;;
;;; Change log:
;;
;; 2021/07/20
;; * First released.
;; 2021/08/16
;; * More elaborate defcustoms
;; * Divide code via outline-mode
;;
;;; Acknowledgements:
;;
;;
;;
;;; TODO
;;
;;
;;
;;; Require
(require 'outline)
;;; Code:
;;;; Outline buffer & Imenu
(defcustom eaf-pdf-outline-buffer-indent 4
"The level of indent in the Outline buffer."
:type 'integer
:group 'eaf-pdf-viewer)
(defvar-local eaf-pdf-outline-pdf-document nil
"The PDF filename or buffer corresponding to this outline
buffer.")
(defvar-local eaf-pdf--outline-window-configuration nil
"Save window configure before popup outline buffer.")
(defvar eaf-pdf-outline-mode-map
(let ((map (make-sparse-keymap)))
(dotimes (i 10)
(define-key map (vector (+ i ?0)) 'digit-argument))
(define-key map "-" 'negative-argument)
;; Navigation
(define-key map (kbd "p") 'previous-line)
(define-key map (kbd "P") 'eaf-pdf-outline-view-prev)
(define-key map (kbd "n") 'next-line)
(define-key map (kbd "N") 'eaf-pdf-outline-view-next)
(define-key map (kbd "SPC") 'eaf-pdf-outline-view-next)
(define-key map (kbd "RET") 'eaf-pdf-outline-jump)
(define-key map (kbd "o") 'eaf-pdf-outline-view)
(define-key map (kbd "v") 'eaf-pdf-outline-view)
;; Outline-mode stuffs
(define-key map (kbd "b") 'outline-backward-same-level)
(define-key map (kbd "d") 'outline-hide-subtree)
(define-key map (kbd "a") 'outline-show-all)
(define-key map (kbd "s") 'outline-show-subtree)
(define-key map (kbd "f") 'outline-forward-same-level)
(define-key map (kbd "Q") 'outline-hide-sublevels)
(define-key map (kbd "RET") 'eaf-pdf-outline-jump)
(define-key map (kbd "Q") 'hide-sublevels)
map)
"Keymap used in `eaf-pdf-outline-mode'.")
(defvar eaf-pdf-outline-edit-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap org-insert-heading-respect-content] #'eaf-pdf-outline-edit-jump)
(define-key map [remap org-ctrl-c-ctrl-c] #'eaf-pdf-outline-edit-buffer-confirm)
(define-key map [remap org-kill-note-or-show-branches] #'kill-buffer-and-window)
map)
"Keymap used in `eaf-pdf-outline-edit-mode'.")
(define-derived-mode eaf-pdf-outline-mode outline-mode "PDF Outline"
"EAF pdf outline mode."
(setq-local outline-regexp "\\( *\\).")
(setq-local outline-level
#'(lambda nil (1+ (/ (length (match-string 1))
eaf-pdf-outline-buffer-indent))))
(toggle-truncate-lines 1)
(setq buffer-read-only t))
(define-derived-mode eaf-pdf-outline-edit-mode org-mode "PDF Outline Editing"
"EAF pdf outline edit mode."
(toggle-truncate-lines 1))
(defun eaf-pdf-outline-buffer-name (&optional pdf-buffer)
(unless pdf-buffer (setq pdf-buffer (current-buffer)))
(format "*Outline: %s*"
(if (bufferp pdf-buffer)
(buffer-name pdf-buffer)
pdf-buffer)))
(defun eaf-pdf-outline-edit-buffer-name (&optional pdf-buffer)
(interactive)
(unless pdf-buffer (setq pdf-buffer (if (local-variable-p 'eaf-pdf-outline-pdf-document) eaf-pdf-outline-pdf-document (current-buffer))))
(format "*Outline Edit: %s*"
(if (bufferp pdf-buffer)
(buffer-name pdf-buffer)
pdf-buffer)))
(defun eaf-pdf-outline ()
"Display an PDF outline of the current buffer."
(interactive)
(let ((pdf-buffer (current-buffer))
(toc (eaf-call-sync "execute_function" eaf--buffer-id "get_toc"))
(page-number (string-to-number (eaf-call-sync "execute_function" eaf--buffer-id "current_page")))
(outline-buf (get-buffer-create (eaf-pdf-outline-buffer-name))))
;; Save window configuration before outline.
(setq eaf-pdf--outline-window-configuration (current-window-configuration))
;; Insert outline content.
(with-current-buffer outline-buf
(setq buffer-read-only nil)
(erase-buffer)
(insert toc)
(setq toc (mapcar #'(lambda (line)
(string-to-number (car (last (split-string line " ")))))
(butlast (split-string (buffer-string) "\n"))))
(goto-line (seq-count (apply-partially #'>= page-number) toc))
(let ((view-read-only nil))
(read-only-mode 1))
(eaf-pdf-outline-mode)
(setq-local eaf-pdf-outline-pdf-document pdf-buffer))
;; Popup outline buffer.
(pop-to-buffer outline-buf)))
(defun eaf-pdf-outline-edit ()
(interactive)
(let* ((pdf-buffer (if (local-variable-p 'eaf-pdf-outline-pdf-document) eaf-pdf-outline-pdf-document (current-buffer)))
(buffer-id (buffer-local-value 'eaf--buffer-id (get-buffer pdf-buffer)))
(toc (eaf-call-sync "execute_function" buffer-id "get_toc_to_edit"))
(page-number (string-to-number (or (eaf-call-sync "execute_function" eaf--buffer-id "current_page") "1")))
(outline-edit-buffer (generate-new-buffer (eaf-pdf-outline-edit-buffer-name))))
(with-current-buffer outline-edit-buffer
(setq buffer-read-only nil)
(erase-buffer)
(insert toc)
(setq toc (mapcar #'(lambda (line)
(string-to-number (car (last (split-string line " ")))))
(butlast (split-string (buffer-string) "\n"))))
(goto-line (seq-count (apply-partially #'>= page-number) toc))
(eaf-pdf-outline-edit-mode)
(set (make-local-variable 'eaf--buffer-id) buffer-id)
)
(pop-to-buffer outline-edit-buffer)))
(defun eaf-pdf-outline-jump ()
"Jump into specific page."
(interactive)
(let* ((line (thing-at-point 'line))
(page-num (substring-no-properties (replace-regexp-in-string "\n" "" (car (last (split-string line " ")))))))
;; Jump to page.
(switch-to-buffer-other-window eaf-pdf-outline-pdf-document)
(eaf-call-sync "execute_function_with_args" eaf--buffer-id "jump_to_page_with_num" (format "%s" page-num))
;; Restore window configuration before outline operation.
(when eaf-pdf--outline-window-configuration
(set-window-configuration eaf-pdf--outline-window-configuration)
(setq eaf-pdf--outline-window-configuration nil))))
(defun eaf-pdf-outline-edit-jump ()
"Jump into specific page."
(interactive)
(let* ((raw-value (org-element-property :raw-value (org-element-at-point)))
(page-num (and (string-match (rx (1+ num) string-end) raw-value) (match-string 0 raw-value)))
)
(if page-num
(eaf-call-sync "execute_function_with_args" eaf--buffer-id "jump_to_page_with_num" (format "%s" page-num))
(error "Has no corresponding page number!"))))
(defun eaf-pdf-outline-view ()
"View the specific page."
(interactive)
(let* ((line (thing-at-point 'line))
(page-num (substring-no-properties (replace-regexp-in-string "\n" "" (car (last (s-split " " line)))))))
;; Jump to page.
(eaf-call-async "execute_function_with_args"
(buffer-local-value 'eaf--buffer-id eaf-pdf-outline-pdf-document)
"jump_to_page_with_num" (format "%s" page-num))))
(defun eaf-pdf-outline-view-prev ()
"View the specific page in the previous line."
(interactive)
(previous-line)
(eaf-pdf-outline-view))
(defun eaf-pdf-outline-view-next ()
"View the specific page in the next line."
(interactive)
(next-line)
(eaf-pdf-outline-view))
(defun eaf-pdf-imenu-create-index-from-toc ()
"Create an alist based on the table of contents of this buffer.
It call the Python's function \"get_toc\" then from the output, make an alist
with each element that looks like
(\"CHAPTER_NAME\" PAGE_NUMBER 'eaf-pdf-imenu-go-to-index nil).
(See why the element has to be that way in `imenu--index-alist'
Hint: Look for \"Special elements\" in the documentation.)
the \"CHAPTER_NAME\" part will be replace with \"Page PAGE_NUMBER\"
when there is no table of contents for the buffer."
(interactive)
(or imenu--index-alist
(setq imenu--index-alist
(let ((toc (eaf-call-sync "execute_function" eaf--buffer-id "get_toc")))
(cond ((string= toc "")
(mapcar #'(lambda (page-num)
(list (concat "Page " (number-to-string page-num)) page-num
#'eaf-pdf-imenu-go-to-index
nil))
(number-sequence 1
(string-to-number
(eaf-call-sync "execute_function"
eaf--buffer-id
"page_total_number")))))
(t
(mapcar #'(lambda (line)
(let ((line-split (split-string line " ")))
(list (string-join (butlast line-split) " ")
(string-to-number (car (last line-split)))
#'eaf-pdf-imenu-go-to-index
nil)))
(split-string toc "\n"))))))))
(defun eaf-pdf-imenu-go-to-index (_chapter-name page-num _arg)
"Ignore _CHAPTER-NAME and _ARG, call Python's \"jump_page\" function with PAGE-NUM as its argument.
The _CHAPTER-NAME is from the car of a element in `eaf-pdf-imenu-create-index-from-toc'
The _ARG is hardcoded to be nil from `eaf-pdf-imenu-create-index-from-toc'
Just ignore them and call \"jump_page\" to PAGE-NUM."
(eaf-call-async "handle_input_response" eaf--buffer-id "jump_page" page-num))
(defun eaf-pdf-imenu-setup ()
(setq imenu-create-index-function 'eaf-pdf-imenu-create-index-from-toc))
(add-hook 'eaf-pdf-viewer-hook 'eaf-pdf-imenu-setup)
;;;; PDF-viewer
(defun eaf-pdf-get-page-annots (page)
"Return a map of annotations on PAGE.
The key is the annot id on PAGE."
(eaf-call-sync "execute_function_with_args" eaf--buffer-id "get_page_annots" (format "%s" page)))
(defun eaf-pdf-get-document-annots ()
"Return a map of page_index of annots.
The key is the page_index."
(eaf-call-sync "execute_function" eaf--buffer-id "get_document_annots"))
(defun eaf-pdf-jump-to-annot (annot)
"Jump to specifical pdf annot."
(let ((rect (gethash "rect" annot))
(page (gethash "page" annot)))
(eaf-call-sync "execute_function_with_args" eaf--buffer-id "jump_to_rect" (format "%s" page) rect)))
(defun eaf--pdf-viewer-bookmark ()
"Restore EAF buffer according to pdf bookmark from the current file path or web URL."
`((handler . eaf--bookmark-restore)
(eaf-app . "pdf-viewer")
(defaults . ,(list eaf--bookmark-title))
(filename . ,(eaf-get-path-or-url))))
(defun eaf--pdf-viewer-bookmark-restore (bookmark)
(eaf-open (cdr (assq 'filename bookmark))))
(defun eaf--pdf-update-position (buffer-id page-index page-total-number)
"Format mode line position indicator to show the current page and the total pages."
(let ((buffer (eaf-get-buffer buffer-id))
(page-index (number-to-string page-index))
(page-total-number (number-to-string page-total-number)))
(when buffer
(with-current-buffer buffer
(let ((need-update
(condition-case ex
(or (not (equal (cadr mode-line-position) page-index))
(not (equal (cadddr mode-line-position) page-total-number)))
('error t))))
(when need-update
(setq-local mode-line-position `(" P" ,page-index
"/" ,page-total-number))
(force-mode-line-update)))))))
(defun eaf-open-pdf-from-history ()
"A wrapper around `eaf-open' that provides pdf history candidates.
This function works best if paired with a fuzzy search package."
(interactive)
(let* ((pdf-history-file-path
(concat eaf-config-location
(file-name-as-directory "pdf")
(file-name-as-directory "history")
"log.txt"))
(history-pattern "^\\(.+\\)\\.pdf$")
(history-file-exists (file-exists-p pdf-history-file-path))
(eaf-files-opened (mapcar (lambda (buf)
(buffer-local-value 'eaf--buffer-url buf))
(eaf--get-eaf-buffers)))
(history-pdf (completing-read
"[EAF/pdf] Search || History: "
(cl-remove-if (lambda (x)
(or (null x)
(member x eaf-files-opened)))
(if history-file-exists
(mapcar
(lambda (h) (when (string-match history-pattern h)
(if (file-exists-p h)
(format "%s" h))))
(with-temp-buffer (insert-file-contents pdf-history-file-path)
(split-string (buffer-string) "\n" t)))
(make-directory (file-name-directory pdf-history-file-path) t)
(with-temp-file pdf-history-file-path ""))))))
(if history-pdf (eaf-open history-pdf))))
(defun eaf-pdf-delete-invalid-file-record-from-history ()
" delete invalid file record from eaf pdf history file"
(interactive)
(let* ((pdf-history-file-path
(concat eaf-config-location
(file-name-as-directory "pdf")
(file-name-as-directory "history")
"log.txt"))
(history-pattern "^\\(.+\\)\\.pdf$")
(history-file-exists (file-exists-p pdf-history-file-path))
file-content)
(when history-file-exists
(setq file-content (with-temp-buffer
(insert-file-contents pdf-history-file-path)
(buffer-string)))
(dolist (each-file (split-string file-content "\n" t))
(unless (file-exists-p each-file)
(setq file-content (replace-regexp-in-string (rx--to-expr (format "%s\n" each-file)) "" file-content))
(message "delete %s record from history" each-file)))
(with-temp-file pdf-history-file-path
(insert file-content))
)))
(defun eaf-pdf-delete-pages (page-num)
" Delete pdf pages
1 => delete page 1
1 3 => delete page 1 2 3
"
(interactive "s delete pages : ")
(let* ( confirmp start-page end-page (tmp-pages (s-split " " page-num)))
(setq start-page (car tmp-pages))
(if (> (length tmp-pages) 1)
(progn
(setq end-page (car (cdr tmp-pages)))
(setq confirmp (yes-or-no-p (format "confirm delete page %s to %s" start-page end-page))))
(setq confirmp (yes-or-no-p (format "confirm delete page %s" start-page))))
(if confirmp
(eaf-call-sync "execute_function_with_args" eaf--buffer-id "delete_pdf_pages" (format "%s" page-num))
(message "give up delete page"))))
;;;###autoload
(defun eaf-open-office (file)
"View Microsoft Office FILE as READ-ONLY PDF."
(interactive "f[EAF/office] Open Office file as PDF: ")
(if (executable-find "libreoffice")
(let* ((file-md5 (eaf-get-file-md5 file))
(basename (file-name-base file))
(pdf-file (format "/tmp/%s.pdf" file-md5))
(pdf-argument (format "%s.%s_office_pdf" basename (file-name-extension file))))
(if (file-exists-p pdf-file)
(eaf-open pdf-file "pdf-viewer" pdf-argument)
(message "Converting %s to PDF, EAF will start shortly..." file)
(make-process
:name ""
:buffer " *eaf-open-office*"
:command (list "libreoffice" "--headless" "--convert-to" "pdf" (file-truename file) "--outdir" "/tmp")
:sentinel (lambda (_ event)
(when (string= (substring event 0 -1) "finished")
(rename-file (format "/tmp/%s.pdf" basename) pdf-file)
(eaf-open pdf-file "pdf-viewer" pdf-argument)
)))))
(error "[EAF/office] libreoffice is required convert Office file to PDF!")))
(defun eaf-get-file-md5 (file)
"Get the MD5 value of a specified FILE."
(car (split-string (shell-command-to-string (format "md5sum '%s'" (file-truename file))) " ")))
;;;; Synctex support
(defvar eaf-pdf-synctex-path "synctex"
"Path of synctex tool.")
(defvar eaf-pdf-synctex-file-directory-function 'eaf-pdf--get-synctex-file-directory
"Function to get the *.synctex.gz file directory, look `eaf-pdf--get-synctex-file-directory'")
(defun eaf-pdf--get-synctex-file-directory (pdf-file)
"Get *.synctex.gz file directory through `pdf-file'"
(file-name-directory (directory-file-name pdf-file)))
(defun eaf-pdf--find-buffer (pdf-url)
"Find opened buffer by `pdf-url'"
(let ((opened-buffer))
(catch 'found-match-buffer
(dolist (buffer (buffer-list))
(set-buffer buffer)
(when (equal major-mode 'eaf-mode)
(when (and (string= eaf--buffer-url pdf-url)
(string= eaf--buffer-app-name "pdf-viewer"))
(setq opened-buffer buffer)
(throw 'found-match-buffer t)))))
opened-buffer))
(defun eaf-pdf--get-synctex-info (tex-file line-num pdf-file)
"Use synctex tool to get the page num of `pdf-file' through `tex-file' and `line-num'."
(if (executable-find eaf-pdf-synctex-path)
(let ((synctex-result)
(page-num 1) (pos-x 0) (pos-y 0)
(synctex-view-command (format "%s view -i %s:1:%s -o %s"
eaf-pdf-synctex-path line-num
(prin1-to-string tex-file)
(prin1-to-string pdf-file))))
(setq synctex-result (shell-command-to-string synctex-view-command))
;; (message "Synctex Result: %s" synctex-result)
(when synctex-result
(and (string-match "Page:\\([0-9]+\\)\n" synctex-result)
(setq page-num (string-to-number (match-string 1 synctex-result))))
(and (string-match "x:\\([0-9\\.]+\\)\n" synctex-result)
(setq pos-x (string-to-number (match-string 1 synctex-result))))
(and (string-match "y:\\([0-9\\.]+\\)\n" synctex-result)
(setq pos-y (string-to-number (match-string 1 synctex-result)))))
(format "%d:%f:%f" page-num pos-x pos-y))
(message "Can not found %s" eaf-pdf-synctex-path)))
(defun eaf-pdf--get-tex-and-line (pdf-file page-num x y)
"Use synctex tool to get tex file and line num through `page-file', `page-num', `x' and `y'."
(if (executable-find eaf-pdf-synctex-path)
(let* ((synctex-result)
(synctex-dir (funcall eaf-pdf-synctex-file-directory-function pdf-file))
(synctex-edit-command (format "%s edit -o %s:%s:%s:%s -d %s"
eaf-pdf-synctex-path page-num x y
(prin1-to-string pdf-file)
(prin1-to-string synctex-dir)))
(tex-file nil)
(line-num nil))
(setq synctex-result (shell-command-to-string synctex-edit-command))
(if (and synctex-result (string-match "Input:\\(.*\\)\nLine:\\([0-9]+\\)\n" synctex-result))
(setq tex-file (expand-file-name (match-string 1 synctex-result))
line-num (string-to-number (match-string 2 synctex-result)))
(message "Failed to get tex file and line number. Did you run latex with `--synctex=1'?"))
`(,tex-file ,line-num))
(message "Can not found %s" eaf-pdf-synctex-path)
nil))
(defun eaf-pdf-jump-to-page (url page-num)
(let* ((pdf-url (expand-file-name url))
(opened-buffer (eaf-pdf--find-buffer pdf-url))
(synctex-info (format "%s:0:0" page-num)))
(if (not opened-buffer)
(eaf-open pdf-url "pdf-viewer" (format "synctex_info=%s" synctex-info))
(display-buffer opened-buffer)
(eaf-call-sync "execute_function_with_args" eaf--buffer-id
"jump_to_page_synctex" (format "%s" synctex-info)))))
(defun eaf-pdf-synctex-forward-view ()
"View the PDF file of Tex synchronously."
(interactive)
(let* ((pdf-url (expand-file-name (TeX-active-master (TeX-output-extension))))
(tex-buffer (window-buffer (minibuffer-selected-window)))
(tex-file (buffer-file-name tex-buffer))
(line-num (progn (set-buffer tex-buffer) (line-number-at-pos)))
(opened-buffer (eaf-pdf--find-buffer pdf-url))
(synctex-info (eaf-pdf--get-synctex-info tex-file line-num pdf-url)))
(when (and (one-window-p) (not opened-buffer))
;; If the window is sole, then split window
;; Original `split-window-sensibly' conflict with `visual-fill-column'
(if (fboundp 'visual-fill-column-split-window-sensibly)
(visual-fill-column-split-window-sensibly)
(split-window-sensibly))
(other-window 1))
(if (not opened-buffer)
(eaf-open pdf-url "pdf-viewer" (format "synctex_info=%s" synctex-info))
(display-buffer opened-buffer)
(eaf-call-sync "execute_function_with_args" eaf--buffer-id
"jump_to_page_synctex" (format "%s" synctex-info)))))
(defun eaf-pdf-synctex-backward-edit (pdf-file page-num x y)
"Edit the Tex file corresponding to (`page-num', `x' , `y') of the `pdf-file'."
(let* ((tex-and-line (eaf-pdf--get-tex-and-line pdf-file page-num x y))
(tex-file (nth 0 tex-and-line))
(line-num (nth 1 tex-and-line)))
(when (and tex-file line-num)
(let ((buffer (get-buffer (file-name-nondirectory tex-file))))
(if buffer
(switch-to-buffer-other-window buffer)
(find-file-other-window tex-file))
(goto-line line-num)))))
(defun eaf-pdf-outline-edit-buffer-confirm ()
(interactive)
(let* ((payload
(org-element-map (org-element-parse-buffer) 'headline
(lambda (headline) (let* ((raw-value (org-element-property :raw-value headline))
(level (org-element-property :level headline))
(page-num (and (string-match (rx (1+ num) string-end) raw-value) (match-string 0 raw-value)))
(title (format "%s" (string-trim-right raw-value page-num)))
)
(if page-num
(list level (string-trim-right title) (string-to-number page-num))
(error "Title: %s has no corresponding page number!" title)))) t))
)
(eaf-call-async "execute_function_with_args" eaf--buffer-id "edit_outline_confirm" payload)))
(defun eaf-pdf-extract-page-text ()
"Display the text of current page in a new buffer."
(interactive)
(let ((page-text-buffer (get-buffer-create (format "*Page text: %s*" (buffer-name))))
(page-text (eaf-call-sync "execute_function" eaf--buffer-id "get_page_text")))
(unless (string-empty-p page-text)
(with-current-buffer page-text-buffer
(erase-buffer)
(insert page-text)
(goto-char (point-min)))
(switch-to-buffer-other-window page-text-buffer))))
;;;; Register as module for EAF
(add-to-list 'eaf-app-binding-alist '("pdf-viewer" . eaf-pdf-viewer-keybinding))
(setq eaf-pdf-viewer-module-path (concat (file-name-directory load-file-name) "eaf_pdf_buffer.py"))
(add-to-list 'eaf-app-module-path-alist '("pdf-viewer" . eaf-pdf-viewer-module-path))
(add-to-list 'eaf-app-bookmark-handlers-alist '("pdf-viewer" . eaf--pdf-viewer-bookmark))
(add-to-list 'eaf-app-bookmark-restore-alist '("pdf-viewer" . eaf--pdf-viewer-bookmark-restore))
(add-to-list 'eaf-app-extensions-alist '("pdf-viewer" . eaf-pdf-extension-list))
(add-to-list 'eaf-app-extensions-alist '("office" . eaf-office-extension-list))
(provide 'eaf-pdf-viewer)
;;; eaf-pdf-viewer.el ends here