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.

80 lines
2.5 KiB

4 years ago
  1. ;;; pdf-loader.el --- Minimal PDF Tools loader -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2017 Andreas Politz
  3. ;; Author: Andreas Politz <politza@hochschule-trier.de>
  4. ;; Keywords:
  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. ;;; Code:
  18. (defconst pdf-loader--auto-mode-alist-item
  19. (copy-sequence "\\.[pP][dD][fF]\\'")
  20. "The item used in `auto-mode-alist'.")
  21. (defconst pdf-loader--magic-mode-alist-item
  22. (copy-sequence "%PDF")
  23. "The item used in`magic-mode-alist'.")
  24. (declare-function pdf-tools-install "pdf-tools.el")
  25. ;;;###autoload
  26. (defun pdf-loader-install (&optional no-query-p skip-dependencies-p
  27. no-error-p force-dependencies-p)
  28. "Prepare Emacs for using PDF Tools.
  29. This function acts as a replacement for `pdf-tools-install' and
  30. makes Emacs load and use PDF Tools as soon as a PDF file is
  31. opened, but not sooner.
  32. The arguments are passed verbatim to `pdf-tools-install', which
  33. see."
  34. (let ((args (list no-query-p skip-dependencies-p
  35. no-error-p force-dependencies-p)))
  36. (if (featurep 'pdf-tools)
  37. (apply #'pdf-tools-install args)
  38. (pdf-loader--install
  39. (lambda ()
  40. (apply #'pdf-loader--load args))))))
  41. (defun pdf-loader--load (&rest args)
  42. (pdf-loader--uninstall)
  43. (save-selected-window
  44. (pdf-tools-install args)))
  45. (defun pdf-loader--install (loader)
  46. (pdf-loader--uninstall)
  47. (push (cons pdf-loader--auto-mode-alist-item loader)
  48. auto-mode-alist)
  49. (push (cons pdf-loader--magic-mode-alist-item loader)
  50. magic-mode-alist))
  51. (defun pdf-loader--uninstall ()
  52. (let ((elt (assoc pdf-loader--auto-mode-alist-item
  53. auto-mode-alist)))
  54. (when elt
  55. (setq auto-mode-alist (remove elt auto-mode-alist))))
  56. (let ((elt (assoc pdf-loader--magic-mode-alist-item
  57. magic-mode-alist)))
  58. (when elt
  59. (setq magic-mode-alist (remove elt magic-mode-alist)))))
  60. (provide 'pdf-loader)
  61. ;;; pdf-loader.el ends here