pdf-misc.el (11246B)
1 ;;; pdf-misc.el --- Miscellaneous commands for PDF buffer. -*- lexical-binding: t; -*- 2 3 ;; Copyright (C) 2013, 2014 Andreas Politz 4 5 ;; Author: Andreas Politz <politza@fh-trier.de> 6 ;; Keywords: files, multimedia 7 8 ;; This program is free software; you can redistribute it and/or modify 9 ;; it under the terms of the GNU General Public License as published by 10 ;; the Free Software Foundation, either version 3 of the License, or 11 ;; (at your option) any later version. 12 13 ;; This program is distributed in the hope that it will be useful, 14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 ;; GNU General Public License for more details. 17 18 ;; You should have received a copy of the GNU General Public License 19 ;; along with this program. If not, see <http://www.gnu.org/licenses/>. 20 21 ;;; Commentary: 22 ;; 23 24 25 (require 'pdf-view) 26 (require 'pdf-util) 27 (require 'imenu) 28 29 30 31 ;;; Code: 32 33 (defvar pdf-misc-minor-mode-map 34 (let ((map (make-sparse-keymap))) 35 (define-key map (kbd "I") 'pdf-misc-display-metadata) 36 (define-key map (kbd "C-c C-p") 'pdf-misc-print-document) 37 map) 38 "Keymap used in `pdf-misc-minor-mode'.") 39 40 ;;;###autoload 41 (define-minor-mode pdf-misc-minor-mode 42 "FIXME: Not documented." 43 :group 'pdf-misc) 44 45 ;;;###autoload 46 (define-minor-mode pdf-misc-size-indication-minor-mode 47 "Provide a working size indication in the mode-line." 48 :group 'pdf-misc 49 (pdf-util-assert-pdf-buffer) 50 (cond 51 (pdf-misc-size-indication-minor-mode 52 (unless (assq 'pdf-misc-size-indication-minor-mode 53 mode-line-position) 54 (setq mode-line-position 55 `((pdf-misc-size-indication-minor-mode 56 (:eval (pdf-misc-size-indication))) 57 ,@mode-line-position)))) 58 (t 59 (setq mode-line-position 60 (cl-remove 'pdf-misc-size-indication-minor-mode 61 mode-line-position :key 'car-safe))))) 62 63 (defun pdf-misc-size-indication () 64 "Return size indication string for the mode-line." 65 (let ((top (= (window-vscroll nil t) 0)) 66 (bot (>= (+ (- (nth 3 (window-inside-pixel-edges)) 67 (nth 1 (window-inside-pixel-edges))) 68 (window-vscroll nil t)) 69 (cdr (pdf-view-image-size t))))) 70 (cond 71 ((and top bot) " All") 72 (top " Top") 73 (bot " Bot") 74 (t (format 75 " %d%%%%" 76 (ceiling 77 (* 100 (/ (float (window-vscroll nil t)) 78 (cdr (pdf-view-image-size t)))))))))) 79 80 (defvar pdf-misc-menu-bar-minor-mode-map (make-sparse-keymap) 81 "The keymap used in `pdf-misc-menu-bar-minor-mode'.") 82 83 (easy-menu-define nil pdf-misc-menu-bar-minor-mode-map 84 "Menu for PDF Tools." 85 `("PDF Tools" 86 ["Go Backward" pdf-history-backward 87 :visible (bound-and-true-p pdf-history-minor-mode) 88 :active (and (bound-and-true-p pdf-history-minor-mode) 89 (not (pdf-history-end-of-history-p)))] 90 ["Go Forward" pdf-history-forward 91 :visible (bound-and-true-p pdf-history-minor-mode) 92 :active (not (pdf-history-end-of-history-p))] 93 ["--" nil 94 :visible (derived-mode-p 'pdf-virtual-view-mode)] 95 ["Next file" pdf-virtual-buffer-forward-file 96 :visible (derived-mode-p 'pdf-virtual-view-mode) 97 :active (pdf-virtual-document-next-file 98 (pdf-view-current-page))] 99 ["Previous file" pdf-virtual-buffer-backward-file 100 :visible (derived-mode-p 'pdf-virtual-view-mode) 101 :active (not (eq 1 (pdf-view-current-page)))] 102 ["--" nil 103 :visible (bound-and-true-p pdf-history-minor-mode)] 104 ["Add text annotation" pdf-annot-mouse-add-text-annotation 105 :visible (bound-and-true-p pdf-annot-minor-mode) 106 :keys "\\[pdf-annot-add-text-annotation]"] 107 ("Add markup annotation" 108 :active (pdf-view-active-region-p) 109 :visible (and (bound-and-true-p pdf-annot-minor-mode) 110 (pdf-info-markup-annotations-p)) 111 ["highlight" pdf-annot-add-highlight-markup-annotation] 112 ["squiggly" pdf-annot-add-squiggly-markup-annotation] 113 ["underline" pdf-annot-add-underline-markup-annotation] 114 ["strikeout" pdf-annot-add-strikeout-markup-annotation]) 115 ["--" nil :visible (bound-and-true-p pdf-annot-minor-mode)] 116 ["Display Annotations" pdf-annot-list-annotations 117 :help "List all annotations" 118 :visible (bound-and-true-p pdf-annot-minor-mode)] 119 ["Display Attachments" pdf-annot-attachment-dired 120 :help "Display attachments in a dired buffer" 121 :visible (featurep 'pdf-annot)] 122 ["Display Metadata" pdf-misc-display-metadata 123 :help "Display information about the document" 124 :visible (featurep 'pdf-misc)] 125 ["Display Outline" pdf-outline 126 :help "Display documents outline" 127 :visible (featurep 'pdf-outline)] 128 "--" 129 ("Render Options" 130 ["Printed Mode" (lambda () 131 (interactive) 132 (pdf-view-printer-minor-mode 'toggle)) 133 :style toggle 134 :selected pdf-view-printer-minor-mode 135 :help "Display the PDF as it would be printed."] 136 ["Midnight Mode" (lambda () 137 (interactive) 138 (pdf-view-midnight-minor-mode 'toggle)) 139 :style toggle 140 :selected pdf-view-midnight-minor-mode 141 :help "Apply a color-filter appropriate for past midnight reading."]) 142 "--" 143 ["Copy region" pdf-view-kill-ring-save 144 :keys "\\[kill-ring-save]" 145 :active (pdf-view-active-region-p)] 146 "--" 147 ["Isearch document" isearch-forward 148 :visible (bound-and-true-p pdf-isearch-minor-mode)] 149 ["Occur document" pdf-occur 150 :visible (featurep 'pdf-occur)] 151 "--" 152 ["Locate TeX source" pdf-sync-backward-search-mouse 153 :visible (and (featurep 'pdf-sync) 154 (equal last-command-event 155 last-nonmenu-event))] 156 ["--" nil :visible (and (featurep 'pdf-sync) 157 (equal last-command-event 158 last-nonmenu-event))] 159 ["Print" pdf-misc-print-document 160 :active (and (pdf-view-buffer-file-name) 161 (file-readable-p (pdf-view-buffer-file-name)))] 162 ["Create image" pdf-view-extract-region-image 163 :help "Create an image of the page or the selected region(s)."] 164 ["Create virtual PDF" pdf-virtual-buffer-create 165 :help "Create a PDF containing all documents in this directory." 166 :visible (bound-and-true-p pdf-virtual-global-minor-mode)] 167 "--" 168 ["Revert buffer" pdf-view-revert-buffer 169 :visible (pdf-info-writable-annotations-p)] 170 "--" 171 ["Customize" pdf-tools-customize])) 172 173 ;;;###autoload 174 (define-minor-mode pdf-misc-menu-bar-minor-mode 175 "Display a PDF Tools menu in the menu-bar." 176 :group 'pdf-misc 177 (pdf-util-assert-pdf-buffer)) 178 179 (defvar pdf-misc-context-menu-minor-mode-map 180 (let ((kmap (make-sparse-keymap))) 181 (define-key kmap [down-mouse-3] 'pdf-misc-popup-context-menu) 182 kmap)) 183 184 ;;;###autoload 185 (define-minor-mode pdf-misc-context-menu-minor-mode 186 "Provide a right-click context menu in PDF buffers. 187 188 \\{pdf-misc-context-menu-minor-mode-map}" 189 :group 'pdf-misc 190 (pdf-util-assert-pdf-buffer)) 191 192 (defun pdf-misc-popup-context-menu (_event) 193 "Popup a context menu at position." 194 (interactive "@e") 195 (popup-menu 196 (cons 'keymap 197 (cddr (or (lookup-key pdf-misc-menu-bar-minor-mode-map 198 [menu-bar PDF\ Tools]) 199 (lookup-key pdf-misc-menu-bar-minor-mode-map 200 [menu-bar pdf\ tools])))))) 201 202 (defun pdf-misc-display-metadata () 203 "Display all available metadata in a separate buffer." 204 (interactive) 205 (pdf-util-assert-pdf-buffer) 206 (let* ((buffer (current-buffer)) 207 (md (pdf-info-metadata))) 208 (with-current-buffer (get-buffer-create "*PDF-Metadata*") 209 (let* ((inhibit-read-only t) 210 (pad (apply' max (mapcar (lambda (d) 211 (length (symbol-name (car d)))) 212 md))) 213 (fmt (format "%%%ds:%%s\n" pad))) 214 (erase-buffer) 215 (setq header-line-format (buffer-name buffer) 216 buffer-read-only t) 217 (font-lock-mode 1) 218 (font-lock-add-keywords nil 219 '(("^ *\\(\\(?:\\w\\|-\\)+\\):" 220 (1 font-lock-keyword-face)))) 221 (dolist (d md) 222 (let ((key (car d)) 223 (val (cdr d))) 224 (cl-case key 225 (keywords 226 (setq val (mapconcat 'identity val ", ")))) 227 (let ((beg (+ (length (symbol-name key)) (point) 1)) 228 (fill-prefix 229 (make-string (1+ pad) ?\s))) 230 (insert (format fmt key val)) 231 (fill-region beg (point) ))))) 232 (goto-char 1) 233 (display-buffer (current-buffer))) 234 md)) 235 236 (defgroup pdf-misc nil 237 "Miscellaneous options for PDF documents." 238 :group 'pdf-tools) 239 240 (define-obsolete-variable-alias 'pdf-misc-print-programm 241 'pdf-misc-print-program-executable "1.0") 242 (defcustom pdf-misc-print-program-executable nil 243 "The program used for printing. 244 245 It is called with one argument, the PDF file." 246 :group 'pdf-misc 247 :type 'file) 248 249 (define-obsolete-variable-alias 'pdf-misc-print-programm-args 250 'pdf-misc-print-program-args "1.0") 251 (defcustom pdf-misc-print-program-args nil 252 "List of additional arguments passed to `pdf-misc-print-program'." 253 :group 'pdf-misc 254 :type '(repeat string)) 255 256 (define-obsolete-function-alias 'pdf-misc-print-programm 257 'pdf-misc-print-program "1.0") 258 (defun pdf-misc-print-program (&optional interactive-p) 259 "Return the program used to print PDFs (if the executable is installed). 260 261 If INTERACTIVE-P is non-nil, ask the user for which program to 262 use when printing the PDF. Optionally, save the choice" 263 (or (and pdf-misc-print-program-executable 264 (executable-find pdf-misc-print-program-executable)) 265 (when interactive-p 266 (let* ((default (car (delq nil (mapcar 267 'executable-find 268 '("gtklp" "xpp" "gpr"))))) 269 buffer-file-name 270 (program 271 (expand-file-name 272 (read-file-name 273 "Print with: " default nil t nil 'file-executable-p)))) 274 (when (and program 275 (executable-find program)) 276 (when (y-or-n-p "Save choice using customize? ") 277 (customize-save-variable 278 'pdf-misc-print-program-executable program)) 279 (setq pdf-misc-print-program-executable program)))))) 280 281 (defun pdf-misc-print-document (filename &optional interactive-p) 282 "Print the PDF doc FILENAME. 283 284 `pdf-misc-print-program' handles the print program, which see for 285 definition of INTERACTIVE-P." 286 (interactive 287 (list (pdf-view-buffer-file-name) t)) 288 (cl-check-type filename (and string (satisfies file-readable-p))) 289 (let ((program (pdf-misc-print-program interactive-p)) 290 (args (append pdf-misc-print-program-args (list filename)))) 291 (unless program 292 (error "No print program available")) 293 (apply #'start-process "printing" nil program args) 294 (message "Print job started: %s %s" 295 program (mapconcat #'identity args " ")))) 296 297 298 (provide 'pdf-misc) 299 300 ;;; pdf-misc.el ends here