Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

283 line
10 KiB

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