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
|
;;; pdf-occur.el --- Display matching lines of PDF documents. -*- lexical-binding: t -*-
;; Copyright (C) 2013, 2014 Andreas Politz
;; Author: Andreas Politz <politza@fh-trier.de>
;; Keywords: files, multimedia
;; 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 of the License, 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. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;
(require 'pdf-tools)
(require 'pdf-view)
(require 'pdf-util)
(require 'pdf-info)
(require 'pdf-isearch)
(require 'tablist)
(require 'ibuf-ext)
(require 'dired)
(require 'let-alist)
;;; Code:
;; * ================================================================== *
;; * Custom & Variables
;; * ================================================================== *
(defgroup pdf-occur nil
"Display matching lines of PDF documents."
:group 'pdf-tools)
(defface pdf-occur-document-face
'((default (:inherit font-lock-string-face)))
"Face used to highlight documents in the list buffer."
:group 'pdf-occur)
(defface pdf-occur-page-face
'((default (:inherit font-lock-type-face)))
"Face used to highlight page numbers in the list buffer."
:group 'pdf-occur)
(defcustom pdf-occur-search-batch-size 16
"Maximum number of pages searched in one query.
Lower numbers will make Emacs more responsive when searching at
the cost of slightly increased search time."
:group 'pdf-occur
:type 'integer)
(defcustom pdf-occur-prefer-string-search nil
"If non-nil, reverse the meaning of the regexp-p prefix-arg."
:group 'pdf-occur
:type 'boolean)
(defvar pdf-occur-history nil
"The history variable for search strings.")
(defvar pdf-occur-search-pages-left nil
"The toatl number of pages left to search.")
(defvar pdf-occur-search-documents nil
"The list of searched documents.
Each element should be either the filename of a PDF document or a
cons \(FILENAME . PAGES\), where PAGES is the list of pages to
search. See `pdf-info-normalize-page-range' for it's format.")
(defvar pdf-occur-number-of-matches 0
"The number of matches in all searched documents.")
(defvar pdf-occur-search-string nil
"The currently used search string, resp. regexp.")
(defvar pdf-occur-search-regexp-p nil
"Non-nil, if searching for a regexp.")
(defvar pdf-occur-buffer-mode-map
(let ((kmap (make-sparse-keymap)))
(set-keymap-parent kmap tablist-mode-map)
(define-key kmap (kbd "RET") 'pdf-occur-goto-occurrence)
(define-key kmap (kbd "C-o") 'pdf-occur-view-occurrence)
(define-key kmap (kbd "SPC") 'pdf-occur-view-occurrence)
(define-key kmap (kbd "C-c C-f") 'next-error-follow-minor-mode)
(define-key kmap (kbd "g") 'pdf-occur-revert-buffer-with-args)
(define-key kmap (kbd "K") 'pdf-occur-abort-search)
(define-key kmap (kbd "D") 'pdf-occur-tablist-do-delete)
(define-key kmap (kbd "x") 'pdf-occur-tablist-do-flagged-delete)
(define-key kmap (kbd "A") 'pdf-occur-tablist-gather-documents)
kmap)
"The keymap used for `pdf-occur-buffer-mode'.")
;; * ================================================================== *
;; * High level functions
;; * ================================================================== *
(define-derived-mode pdf-occur-buffer-mode tablist-mode "PDFOccur"
"Major mode for output from `pdf-occur`. \\<pdf-occur-buffer-mode-map>
Some useful keys are:
\\[pdf-occur-abort-search] - Abort the search.
\\[pdf-occur-revert-buffer-with-args] - Restart the search.
\\[universal-argument] \\[pdf-occur-revert-buffer-with-args] - Restart search with different regexp.
\\[universal-argument] \\[universal-argument] \\[pdf-occur-revert-buffer-with-args] - Same, but do a plain string search.
\\[tablist-push-regexp-filter] - Filter matches by regexp on current or prefix-th column.
\\[tablist-pop-filter] - Remove last added filter.
\\[pdf-occur-tablist-do-delete] - Remove the current file from the search.
\\[pdf-occur-tablist-gather-documents] - Include marked files from displayed `dired'/`ibuffer' and
`pdf-view-mode' buffers in the search.
\\{pdf-occur-buffer-mode-map}"
(setq-local next-error-function 'pdf-occur-next-error)
(setq-local revert-buffer-function
'pdf-occur-revert-buffer)
(setq next-error-last-buffer (current-buffer))
(setq-local tabulated-list-sort-key nil)
(setq-local tabulated-list-use-header-line t)
(setq-local tablist-operations-function
(lambda (op &rest _)
(cl-case op
(supported-operations '(find-entry))
(find-entry
(let ((display-buffer-overriding-action
'(display-buffer-same-window)))
(pdf-occur-goto-occurrence)))))))
;;;###autoload
(defun pdf-occur (string &optional regexp-p)
"List lines matching STRING or PCRE.
Interactively search for a regexp. Unless a prefix arg was given,
in which case this functions performs a string search.
If `pdf-occur-prefer-string-search' is non-nil, the meaning of
the prefix-arg is inverted."
(interactive
(progn
(pdf-util-assert-pdf-buffer)
(list
(pdf-occur-read-string
(pdf-occur-want-regexp-search-p))
(pdf-occur-want-regexp-search-p))))
(pdf-util-assert-pdf-buffer)
(pdf-occur-search (list (current-buffer)) string regexp-p))
(defvar ibuffer-filtering-qualifiers)
;;;###autoload
(defun pdf-occur-multi-command ()
"Perform `pdf-occur' on multiple buffer.
For a programmatic search of multiple documents see
`pdf-occur-search'."
(interactive)
(ibuffer)
(with-current-buffer "*Ibuffer*"
(pdf-occur-ibuffer-minor-mode)
(unless (member '(derived-mode . pdf-view-mode)
ibuffer-filtering-qualifiers)
(ibuffer-filter-by-derived-mode 'pdf-view-mode))
(message
"%s"
(substitute-command-keys
"Mark a bunch of PDF buffers and type \\[pdf-occur-ibuffer-do-occur]"))
(sit-for 3)))
(defun pdf-occur-revert-buffer (&rest _)
"Restart the search."
(pdf-occur-assert-occur-buffer-p)
(unless pdf-occur-search-documents
(error "No documents to search"))
(unless pdf-occur-search-string
(error "Nothing to search for"))
(let* ((2-columns-p (= 1 (length pdf-occur-search-documents)))
(filename-width
(min 24
(apply 'max
(mapcar 'length
(mapcar 'pdf-occur-abbrev-document
(mapcar 'car pdf-occur-search-documents))))))
(page-sorter (tablist-generate-sorter
(if 2-columns-p 0 1)
'<
'string-to-number)))
(setq tabulated-list-format
(if 2-columns-p
`[("Page" 4 ,page-sorter :right-align t)
("Line" 0 t)]
`[("Document" ,filename-width t)
("Page" 4 ,page-sorter :right-align t)
("Line" 0 t)])
tabulated-list-entries nil))
(tabulated-list-revert)
(pdf-occur-start-search
pdf-occur-search-documents
pdf-occur-search-string
pdf-occur-search-regexp-p)
(pdf-occur-update-header-line)
(setq mode-line-process
'(:propertize ":run" face compilation-mode-line-run)))
(defun pdf-occur-revert-buffer-with-args (string &optional regexp-p documents)
"Restart the search with modified arguments.
Interactively just restart the search, unless a prefix was given.
In this case read a new search string. With `C-u C-u' as prefix
additionally invert the current state of
`pdf-occur-search-regexp-p'."
(interactive
(progn
(pdf-occur-assert-occur-buffer-p)
(cond
(current-prefix-arg
(let ((regexp-p
(if (equal current-prefix-arg '(16))
(not pdf-occur-search-regexp-p)
pdf-occur-search-regexp-p)))
(list
(pdf-occur-read-string regexp-p)
regexp-p)))
(t
(list pdf-occur-search-string
pdf-occur-search-regexp-p)))))
(setq pdf-occur-search-string string
pdf-occur-search-regexp-p regexp-p)
(when documents
(setq pdf-occur-search-documents
(pdf-occur-normalize-documents documents)))
(pdf-occur-revert-buffer))
(defun pdf-occur-abort-search ()
"Abort the current search.
This immediately kills the search process."
(interactive)
(unless (pdf-occur-search-in-progress-p)
(user-error "No search in progress"))
(pdf-info-kill-local-server)
(pdf-occur-search-finished t))
;; * ================================================================== *
;; * Finding occurences
;; * ================================================================== *
(defun pdf-occur-goto-occurrence (&optional no-select-window-p)
"Go to the occurrence at point.
If EVENT is nil, use occurrence at current line. Select the
PDF's window, unless NO-SELECT-WINDOW-P is non-nil.
FIXME: EVENT not used at the moment."
(interactive)
(let ((item (tabulated-list-get-id)))
(when item
(let* ((doc (plist-get item :document))
(page (plist-get item :page))
(match (plist-get item :match-edges))
(buffer (if (bufferp doc)
doc
(or (find-buffer-visiting doc)
(find-file-noselect doc))))
window)
(if no-select-window-p
(setq window (display-buffer buffer))
(pop-to-buffer buffer)
(setq window (selected-window)))
(with-selected-window window
(when page
(pdf-view-goto-page page))
;; Abuse isearch.
(when match
(let ((pixel-match
(pdf-util-scale-relative-to-pixel match))
(pdf-isearch-batch-mode t))
(pdf-isearch-hl-matches pixel-match nil t)
(pdf-isearch-focus-match-batch pixel-match))))))))
(defun pdf-occur-view-occurrence (&optional _event)
"View the occurrence at EVENT.
If EVENT is nil, use occurrence at current line."
(interactive (list last-nonmenu-event))
(pdf-occur-goto-occurrence t))
(defun pdf-occur-next-error (&optional arg reset)
"Move to the Nth (default 1) next match in an PDF Occur mode buffer.
Compatibility function for \\[next-error] invocations."
(interactive "p")
;; we need to run pdf-occur-find-match from within the Occur buffer
(with-current-buffer
;; Choose the buffer and make it current.
(if (next-error-buffer-p (current-buffer))
(current-buffer)
(next-error-find-buffer
nil nil
(lambda ()
(eq major-mode 'pdf-occur-buffer-mode))))
(when (bobp)
(setq reset t))
(if reset
(goto-char (point-min))
(beginning-of-line))
(when (/= arg 0)
(when (eobp)
(forward-line -1))
(when reset
(cl-decf arg))
(let ((line (line-number-at-pos))
(limit (line-number-at-pos
(if (>= arg 0)
(1- (point-max))
(point-min)))))
(when (= line limit)
(error "No more matches"))
(forward-line
(if (>= arg 0)
(min arg (- limit line))
(max arg (- limit line))))))
;; In case the *Occur* buffer is visible in a nonselected window.
(tablist-move-to-major-column)
(let ((win (get-buffer-window (current-buffer) t)))
(if win (set-window-point win (point))))
(pdf-occur-goto-occurrence)))
;; * ================================================================== *
;; * Integration with other modes
;; * ================================================================== *
;;;###autoload
(define-minor-mode pdf-occur-global-minor-mode
"Enable integration of Pdf Occur with other modes.
This global minor mode enables (or disables)
`pdf-occur-ibuffer-minor-mode' and `pdf-occur-dired-minor-mode'
in all current and future ibuffer/dired buffer." nil nil nil
:global t
(let ((arg (if pdf-occur-global-minor-mode 1 -1)))
(dolist (buf (buffer-list))
(with-current-buffer buf
(cond
((derived-mode-p 'dired-mode)
(pdf-occur-dired-minor-mode arg))
((derived-mode-p 'ibuffer-mode)
(pdf-occur-ibuffer-minor-mode arg)))))
(cond
(pdf-occur-global-minor-mode
(add-hook 'dired-mode-hook 'pdf-occur-dired-minor-mode)
(add-hook 'ibuffer-mode-hook 'pdf-occur-ibuffer-minor-mode))
(t
(remove-hook 'dired-mode-hook 'pdf-occur-dired-minor-mode)
(remove-hook 'ibuffer-mode-hook 'pdf-occur-ibuffer-minor-mode)))))
(defvar pdf-occur-ibuffer-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap ibuffer-do-occur] 'pdf-occur-ibuffer-do-occur)
map)
"Keymap used in `pdf-occur-ibuffer-minor-mode'.")
;;;###autoload
(define-minor-mode pdf-occur-ibuffer-minor-mode
"Hack into ibuffer's do-occur binding.
This mode remaps `ibuffer-do-occur' to
`pdf-occur-ibuffer-do-occur', which will start the PDF Tools
version of `occur', if all marked buffer's are in `pdf-view-mode'
and otherwise fallback to `ibuffer-do-occur'."
nil nil nil)
(defun pdf-occur-ibuffer-do-occur (&optional regexp-p)
"Uses `pdf-occur-search', if appropriate.
I.e. all marked buffers are in PDFView mode."
(interactive
(list (pdf-occur-want-regexp-search-p)))
(let* ((buffer (or (ibuffer-get-marked-buffers)
(and (ibuffer-current-buffer)
(list (ibuffer-current-buffer)))))
(pdf-only-p (cl-every
(lambda (buf)
(with-current-buffer buf
(derived-mode-p 'pdf-view-mode)))
buffer)))
(if (not pdf-only-p)
(call-interactively 'ibuffer-do-occur)
(let ((regexp (pdf-occur-read-string regexp-p)))
(pdf-occur-search buffer regexp regexp-p)))))
(defvar pdf-occur-dired-minor-mode-map
(let ((map (make-sparse-keymap)))
(define-key map [remap dired-do-search] 'pdf-occur-dired-do-search)
map)
"Keymap used in `pdf-occur-dired-minor-mode'.")
;;;###autoload
(define-minor-mode pdf-occur-dired-minor-mode
"Hack into dired's `dired-do-search' binding.
This mode remaps `dired-do-search' to
`pdf-occur-dired-do-search', which will start the PDF Tools
version of `occur', if all marked buffer's are in `pdf-view-mode'
and otherwise fallback to `dired-do-search'."
nil nil nil)
(defun pdf-occur-dired-do-search ()
"Uses `pdf-occur-search', if appropriate.
I.e. all marked files look like PDF documents."
(interactive)
(let ((files (dired-get-marked-files)))
(if (not (cl-every (lambda (file)
(string-match-p
(car pdf-tools-auto-mode-alist-entry)
file))
files))
(call-interactively 'dired-do-search)
(let* ((regex-p (pdf-occur-want-regexp-search-p))
(regexp (pdf-occur-read-string regex-p)))
(pdf-occur-search files regexp regex-p)))))
;; * ================================================================== *
;; * Search engine
;; * ================================================================== *
(defun pdf-occur-search (documents string &optional regexp-p)
"Search DOCUMENTS for STRING.
DOCUMENTS should be a list of buffers (objects, not names),
filenames or conses \(BUFFER-OR-FILENAME . PAGES\), where PAGES
determines the scope of the search of the respective document.
See `pdf-info-normalize-page-range' for it's format.
STRING is either the string to search for or, if REGEXP-P is
non-nil, a Perl compatible regular expression (PCRE).
Display the occur buffer and start the search asynchronously.
Returns the window where the buffer is displayed."
(unless documents
(error "No documents to search"))
(when (or (null string) (= (length string) 0))
(error "Not searching for the empty string"))
(with-current-buffer (get-buffer-create "*PDF-Occur*")
(pdf-occur-buffer-mode)
(setq-local pdf-occur-search-documents
(pdf-occur-normalize-documents documents))
(setq-local pdf-occur-search-string string)
(setq-local pdf-occur-search-regexp-p regexp-p)
(setq-local pdf-occur-search-pages-left 0)
(setq-local pdf-occur-number-of-matches 0)
(pdf-occur-revert-buffer)
(display-buffer
(current-buffer))))
(defadvice tabulated-list-init-header (after update-header activate)
"We want our own headers, thank you."
(when (derived-mode-p 'pdf-occur-buffer-mode)
(save-current-buffer
(with-no-warnings (pdf-occur-update-header-line)))))
(defun pdf-occur-create-entry (filename page &optional match)
"Create a `tabulated-list-entries' entry for a search result.
If match is nil, create a fake entry for documents w/o any
matches linked with PAGE."
(let* ((text (or (car match) "[No matches]"))
(edges (cdr match))
(displayed-text
(if match
(replace-regexp-in-string "\n" "\\n" text t t)
(propertize text 'face 'font-lock-warning-face)))
(displayed-page
(if match
(propertize (format "%d" page)
'face 'pdf-occur-page-face)
""))
(displayed-document
(propertize
(pdf-occur-abbrev-document filename)
'face 'pdf-occur-document-face))
(id `(:document ,filename
:page ,page
:match-text ,(if match text)
:match-edges ,(if match edges))))
(list id
(if (= (length pdf-occur-search-documents) 1)
(vector displayed-page displayed-text)
(vector displayed-document
displayed-page
displayed-text)))))
(defun pdf-occur-update-header-line ()
(pdf-occur-assert-occur-buffer-p)
(save-current-buffer
;;force-mode-line-update seems to sometimes spuriously change the
;;current buffer.
(setq header-line-format
`(:eval (concat
(if (= (length pdf-occur-search-documents) 1)
(format "%d match%s in document `%s'"
pdf-occur-number-of-matches
(if (/= 1 pdf-occur-number-of-matches) "es" "")
(pdf-occur-abbrev-document
(caar pdf-occur-search-documents)))
(format "%d match%s in %d documents"
pdf-occur-number-of-matches
(if (/= 1 pdf-occur-number-of-matches) "es" "")
(length pdf-occur-search-documents)))
(if (pdf-occur-search-in-progress-p)
(propertize
(concat " ["
(if (numberp pdf-occur-search-pages-left)
(format "%d pages left"
pdf-occur-search-pages-left)
"Searching")
"]")
'face 'compilation-mode-line-run)))))
(force-mode-line-update)))
(defun pdf-occur-search-finished (&optional abort-p)
(setq pdf-occur-search-pages-left 0)
(setq mode-line-process
(if abort-p
'(:propertize
":aborted" face compilation-mode-line-fail)
'(:propertize
":exit" face compilation-mode-line-exit)))
(let ((unmatched
(mapcar (lambda (doc)
(pdf-occur-create-entry doc 1))
(cl-set-difference
(mapcar 'car
pdf-occur-search-documents)
(mapcar (lambda (elt)
(plist-get (car elt) :document))
tabulated-list-entries)
:test 'equal))))
(when (and unmatched
(> (length pdf-occur-search-documents) 1))
(pdf-occur-insert-entries unmatched)))
(tablist-apply-filter)
(pdf-occur-update-header-line)
(pdf-isearch-message
(if abort-p
"Search aborted."
(format "Occur search finished with %d matches"
pdf-occur-number-of-matches))))
(defun pdf-occur-add-matches (filename matches)
(pdf-occur-assert-occur-buffer-p)
(when matches
(let (entries)
(dolist (match matches)
(let-alist match
(push (pdf-occur-create-entry filename .page (cons .text .edges))
entries)))
(setq entries (nreverse entries))
(pdf-occur-insert-entries entries))))
(defun pdf-occur-insert-entries (entries)
"Insert tabulated-list ENTRIES at the end."
(pdf-occur-assert-occur-buffer-p)
(let ((inhibit-read-only t)
(end-of-buffer (and (eobp) (not (bobp)))))
(save-excursion
(goto-char (point-max))
(dolist (elt entries)
(apply tabulated-list-printer elt))
(set-buffer-modified-p nil))
(when end-of-buffer
(dolist (win (get-buffer-window-list))
(set-window-point win (point-max))))
(setq tabulated-list-entries
(append tabulated-list-entries
entries))))
(defun pdf-occur-search-in-progress-p ()
(and (numberp pdf-occur-search-pages-left)
(> pdf-occur-search-pages-left 0)))
(defun pdf-occur-start-search (documents string
&optional regexp-p)
(pdf-occur-assert-occur-buffer-p)
(pdf-info-make-local-server nil t)
(let ((batches (pdf-occur-create-batches
documents (or pdf-occur-search-batch-size 1))))
(pdf-info-local-batch-query
(lambda (document pages)
(if regexp-p
(pdf-info-search-regexp string pages nil document)
(pdf-info-search-string string pages document)))
(lambda (status response document pages)
(if status
(error "%s" response)
(when (numberp pdf-occur-search-pages-left)
(cl-decf pdf-occur-search-pages-left
(1+ (- (cdr pages) (car pages)))))
(when (cl-member document pdf-occur-search-documents
:key 'car
:test 'equal)
(cl-incf pdf-occur-number-of-matches
(length response))
(pdf-occur-add-matches document response)
(pdf-occur-update-header-line))))
(lambda (status buffer)
(when (buffer-live-p buffer)
(with-current-buffer buffer
(pdf-occur-search-finished (eq status 'killed)))))
batches)
(setq pdf-occur-number-of-matches 0)
(setq pdf-occur-search-pages-left
(apply '+ (mapcar (lambda (elt)
(1+ (- (cdr (nth 1 elt))
(car (nth 1 elt)))))
batches)))))
;; * ================================================================== *
;; * Editing searched documents
;; * ================================================================== *
(defun pdf-occur-tablist-do-delete (&optional arg)
"Delete ARG documents from the search list."
(interactive "P")
(when (pdf-occur-search-in-progress-p)
(user-error "Can't delete while a search is in progress."))
(let* ((items (tablist-get-marked-items arg))
(documents (cl-remove-duplicates
(mapcar (lambda (entry)
(plist-get (car entry) :document))
items)
:test 'equal)))
(unless documents
(error "No documents selected"))
(when (tablist-yes-or-no-p
'Stop\ searching
nil (mapcar (lambda (d) (cons nil (vector d)))
documents))
(setq pdf-occur-search-documents
(cl-remove-if (lambda (elt)
(member (car elt) documents))
pdf-occur-search-documents)
tabulated-list-entries
(cl-remove-if (lambda (elt)
(when (member (plist-get (car elt) :document)
documents)
(when (plist-get (car elt) :match-edges)
(cl-decf pdf-occur-number-of-matches))
t))
tabulated-list-entries))
(tablist-revert)
(pdf-occur-update-header-line)
(tablist-move-to-major-column))))
(defun pdf-occur-tablist-do-flagged-delete (&optional interactive)
"Stop searching all documents marked with a D."
(interactive "p")
(let* ((tablist-marker-char ?D))
(if (save-excursion
(goto-char (point-min))
(re-search-forward (tablist-marker-regexp) nil t))
(pdf-occur-tablist-do-delete)
(or (not interactive)
(message "(No deletions requested)")))))
(defun pdf-occur-tablist-gather-documents ()
"Gather marked documents in windows.
Examine all dired/ibuffer windows and offer to put marked files
in the search list."
(interactive)
(let ((searched (mapcar 'car pdf-occur-search-documents))
files)
(dolist (win (window-list))
(with-selected-window win
(cond
((derived-mode-p 'dired-mode)
(let ((marked (dired-get-marked-files nil nil nil t)))
(when (> (length marked) 1)
(when (eq t (car marked))
(setq marked (cdr marked)))
(setq files
(append files marked nil)))))
((derived-mode-p 'ibuffer-mode)
(dolist (fname (mapcar 'buffer-file-name
(ibuffer-get-marked-buffers)))
(when fname
(push fname files))))
((and (derived-mode-p 'pdf-view-mode)
(buffer-file-name))
(push (buffer-file-name) files)))))
(setq files
(cl-sort ;Looks funny.
(cl-set-difference
(cl-remove-duplicates
(cl-remove-if-not
(lambda (file) (string-match-p
(car pdf-tools-auto-mode-alist-entry)
file))
files)
:test 'file-equal-p)
searched
:test 'file-equal-p)
'string-lessp))
(if (null files)
(message "No marked, new PDF files found in windows")
(when (tablist-yes-or-no-p
'add nil (mapcar (lambda (file)
(cons nil (vector file)))
(cl-sort files 'string-lessp)))
(setq pdf-occur-search-documents
(append pdf-occur-search-documents
(pdf-occur-normalize-documents files)))
(message "Added %d file%s to the list of searched documents%s"
(length files)
(dired-plural-s (length files))
(substitute-command-keys
" - Hit \\[pdf-occur-revert-buffer-with-args]"))))))
;; * ================================================================== *
;; * Utilities
;; * ================================================================== *
(defun pdf-occur-read-string (&optional regexp-p)
(read-string
(concat
(format "List lines %s"
(if regexp-p "matching PCRE" "containing string"))
(if pdf-occur-search-string
(format " (default %s)" pdf-occur-search-string))
": ")
nil 'pdf-occur-history pdf-occur-search-string))
(defun pdf-occur-assert-occur-buffer-p ()
(unless (derived-mode-p 'pdf-occur-buffer-mode)
(error "Not in PDF occur buffer")))
(defun pdf-occur-want-regexp-search-p ()
(or (and current-prefix-arg
pdf-occur-prefer-string-search)
(and (null current-prefix-arg)
(not pdf-occur-prefer-string-search))))
;; FIXME: This will be confusing when searching documents with the
;; same base file-name.
(defun pdf-occur-abbrev-document (file-or-buffer)
(if (bufferp file-or-buffer)
(buffer-name file-or-buffer)
(let ((abbrev (file-name-nondirectory file-or-buffer)))
(if (> (length abbrev) 0)
abbrev
file-or-buffer))))
(defun pdf-occur-create-batches (documents batch-size)
(let (queries)
(dolist (d documents)
(let* ((file-or-buffer (car d))
(pages (pdf-info-normalize-page-range (cdr d)))
(first (car pages))
(last (if (eq (cdr pages) 0)
(pdf-info-number-of-pages file-or-buffer)
(cdr pages)))
(npages (1+ (- last first)))
(nbatches (ceiling
(/ (float npages) batch-size))))
(dotimes (i nbatches)
(push
(list file-or-buffer
(cons (+ first (* i batch-size))
(min last (+ first (1- (* (1+ i) batch-size))))))
queries))))
(nreverse queries)))
(defun pdf-occur-normalize-documents (documents)
"Normalize list of documents.
Replaces buffers with their associated filenames \(if
applicable\) and ensures that every element looks like
\(FILENAME-OR-BUFFER . PAGES\)."
(cl-sort (mapcar (lambda (doc)
(unless (consp doc)
(setq doc (cons doc nil)))
(when (and (bufferp (car doc))
(buffer-file-name (car doc)))
(setq doc (cons (buffer-file-name (car doc))
(cdr doc))))
(if (stringp (car doc))
(cons (expand-file-name (car doc)) (cdr doc))
doc))
documents)
(lambda (a b) (string-lessp
(if (bufferp a) (buffer-name a) a)
(if (bufferp b) (buffer-name b) b)))
:key 'car))
(provide 'pdf-occur)
;;; pdf-occur.el ends here
|