Allow opening books in an external program

* calibre-library.el (calibre-external-programs): New user option.
  (calibre-library-open-book-external): New command.
  (calibre-library-open-book): Defer to
  calibre-library-open-book-external if an appropriate entry exists in
  calibre-external-programs.

* doc/calibre.texi (Reading books): Add information about
  calibre-external-programs and calibre-library-open-book-external.

* etc/NEWS: Mention calibre-external-programs and
  calibre-library-open-book-external.
This commit is contained in:
Kjartan Oli Agustsson 2024-01-06 14:37:44 +00:00
parent 339258061e
commit 828be28570
Signed by: kjartanoli
GPG Key ID: D7572FE3605EE6B0
3 changed files with 57 additions and 5 deletions

View File

@ -31,6 +31,17 @@
(require 'calibre-virtual-library) (require 'calibre-virtual-library)
(require 'calibre-edit) (require 'calibre-edit)
(defcustom calibre-external-programs nil
"A mapping of formats to external programs used to read them.
This is an alist, where each entry is of the form (FORMAT
. PROGRAM). FORMAT is a symbol identifying a book format and
PROGRAM is a string naming an external program to use when
opening books in that format."
:type '(repeat (cons (symbol :tag "Format") (string :tag "Program")))
:group 'calibre
:package-version '("calibre" . "1.4.0"))
;;;###autoload ;;;###autoload
(defun calibre-library-add-book (file) (defun calibre-library-add-book (file)
"Add FILE to the Calibre library." "Add FILE to the Calibre library."
@ -107,7 +118,10 @@ If called with a prefix argument prompt the user for the format."
(let ((format (if arg (let ((format (if arg
(completing-read "Format: " (calibre-book-formats book) nil t) (completing-read "Format: " (calibre-book-formats book) nil t)
(calibre-book--pick-format book)))) (calibre-book--pick-format book))))
(find-file (calibre-book--file book format)))) (let ((program (alist-get format calibre-external-programs)))
(if program
(calibre-library-open-book-external book program)
(find-file (calibre-book--file book format))))))
(defun calibre-library-open-book-other-window (book &optional arg) (defun calibre-library-open-book-other-window (book &optional arg)
"Open BOOK in its preferred format, in another window. "Open BOOK in its preferred format, in another window.
@ -120,6 +134,24 @@ If called with a prefix argument prompt the user for the format."
(calibre-book--pick-format book)))) (calibre-book--pick-format book))))
(find-file-other-window (calibre-book--file book format)))) (find-file-other-window (calibre-book--file book format))))
(defun calibre-library-open-book-external (book &optional command arg)
"Open BOOK in an external program.
If called with a prefix argument prompt the user for the format."
(interactive (list (tabulated-list-get-id)
nil
current-prefix-arg)
calibre-library-mode)
(let* ((format (if arg
(completing-read "Format: " (calibre-book-formats book) nil t)
(calibre-book--pick-format book)))
(command (if command
command
(read-shell-command "Open with: " (alist-get format calibre-external-programs)))))
(start-process (format "%s-external" (calibre-book-title book))
nil
command
(calibre-book--file book format))))
(defvar-keymap calibre-library-mode-map (defvar-keymap calibre-library-mode-map
:doc "Local keymap for Calibre Library buffers." :doc "Local keymap for Calibre Library buffers."
:parent tabulated-list-mode-map :parent tabulated-list-mode-map

View File

@ -121,10 +121,20 @@ To read a book simply press @kbd{@key{RET}}
the @file{*Library*} buffer. This will open a new buffer containing the the @file{*Library*} buffer. This will open a new buffer containing the
book at point in the current window. Alternatively you can also press book at point in the current window. Alternatively you can also press
@kbd{o} (@code{calibre-library-open-book-other-window}), which will open @kbd{o} (@code{calibre-library-open-book-other-window}), which will open
the book in a different window. To actually read a book Emacs must of the book in a different window.
course be able to display the file format in which the book is stored,
which may require the presence of additional tools on your system @findex calibre-library-open-book-external
@xref{Document View,,,Emacs}. @vindex calibre-external-formats
To actually read a book Emacs must of course be able to display the file
format in which the book is stored, which may require the presence of
additional tools on your system @xref{Document View,,,Emacs}. If Emacs
is not able to display your chosen format, or you simply prefer using
another program, you can also open the book in an external program using
@code{calibre-library-open-book-external} or customize
@code{calibre-external-formats}. If an entry for a particular format
exists en @code{calibre-external-formats}
@code{calibre-library-open-book} will use the specified program instead
of opening the file in Emacs.
@menu @menu
* Choosing formats:: * Choosing formats::

View File

@ -6,6 +6,16 @@ See the end of the file for license conditions.
This file is about changes in calibre.el, the Emacs client for This file is about changes in calibre.el, the Emacs client for
Calibre. Calibre.
* Changes in calibre.el 1.4.0
** Allow opening books in external programs
The new calibre-library-open-book-external command allows opening
books using an external program.
The new calibre-external-programs user option allows specifying
external programs to use for specific formats instead of opening the
file in Emacs.
* Changes in calibre.el 1.3.4 * Changes in calibre.el 1.3.4
** Allow searching by title in composite filters ** Allow searching by title in composite filters