dotemacs

My Emacs configuration
git clone git://git.entf.net/dotemacs
Log | Files | Refs | LICENSE

pdf-loader.el (2600B)


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