Skip to content

Commit

Permalink
Add djvu layer
Browse files Browse the repository at this point in the history
Update layers/+readers/djvu/packages.el

Co-authored-by: Lucius Hu <1222865+lebensterben@users.noreply.github.com>
  • Loading branch information
dalanicolai and lebensterben committed Sep 1, 2021
1 parent 59852a6 commit 2e86f64
Show file tree
Hide file tree
Showing 7 changed files with 334 additions and 0 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.develop
Original file line number Diff line number Diff line change
Expand Up @@ -445,6 +445,7 @@ In org-agenda-mode
- outshine (thanks to Langston Barrett)
**** Readers
- epub (thanks to Jeremy Dormitzer and André Peric Tavares)
- djvu (thanks to Daniel Nicolai)
**** Spacemacs
- spacemacs-defaults
- spacemacs-modeline
Expand Down Expand Up @@ -477,6 +478,7 @@ In org-agenda-mode
- xclipboard (thanks to Charles Weill, Sylvain Benner, and Alex Palaistras)
**** WEB services
- confluence
- eww (thanks to Colton Kopsa, Maximilian Wolff and Daniel Nicolai)
*** Renamed layers
- =extra-langs= renamed to =major-modes= (thanks to Eivind Fonn)
- =pdf-tools= renamed to =pdf=
Expand Down
88 changes: 88 additions & 0 deletions layers/+readers/djvu/README.org
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#+TITLE: djvu layer
#+TAGS: layer|reader

# The maximum height of the logo should be 200 pixels.
#+CAPTION: source: https://commons.wikimedia.org/wiki/File:DjVu-logo.svg
[[img/djvu-logo.svg]]

# TOC links should be GitHub style anchors.
* Table of Contents :TOC_4_gh:noexport:
- [[#description][Description]]
- [[#features][Features:]]
- [[#install][Install]]
- [[#prerequisites][Prerequisites]]
- [[#dotfile][Dotfile]]
- [[#key-bindings][Key bindings]]

* Description
This layer adds support for reading djvu files with spacemacs.

Because of some difficulties, the layer does not lazy load the =djvu.el= and
=djvu3.el= packages.

** Features:
This layer provides a full featured djvu editor by implementing the [[https://github.com/dalanicolai/djvu2.el][djvu.el
package]] along with the [[https://github.com/dalanicolai/djvu3][djvu3]] extension (a newer alternative to [[https://github.com/dalanicolai/djvu2.el][djvu2.el]]).

- flexible annotation editing and rendering, covering most (but not all) of
[[https://linux.die.net/man/1/djvused][djvused]] annotation definitions
- fast navigation with imenu
- fast search with djvu-occur
- remember last view with djvu-restore
- dark mode

* Install
** Prerequisites
The =djvu= package requires the =djvused= command (from [[http://djvu.sourceforge.net/][djvulibre]]) to be
available in =exec-path=.

Additionally, for =dark-mode= availability of the command =pnminvert= (from
[[http://netpbm.sourceforge.net/][netpbm]]) is required.

** Dotfile
After installing the dependencies, add this to your ~/.spacemacs.

#+BEGIN_SRC emacs-lisp
(setq-default dotspacemacs-configuration-layers '(djvu))
#+END_SRC

After that, syncronize your configuration with SPC f e R.

* Key bindings

*djvu-read-mode*
| Key Binding | Description |
|-------------+----------------------------------------------------------------------------------|
| ~i~ | djvu-image-toggle |
| ~j~ | scroll up |
| ~k~ | scroll down |
| ~J~ | next page |
| ~K~ | previous page |
| ~g~ | goto page |
| | |
| ~d~ | djvu-toggle-invert (dark-mode) |
| ~c~ | djvu-toggle-semi-continuous-scrolling (works only when image larger than window) |
| ~+/-~ | zoom-in/out |
| ~, s~ | djvu-occur |
| ~/~ | djvu fast search |
| ~n~ | djvu search continue |
| | |
| ~SPC j i~ | imenu navigation |
| ~o~ | outline |

*Annotations*
| ~SPC m h~ | keyboard highlight |
| ~mouse-1-drag~ | highlight |
| ~S-mouse-1-drag~ | text annotation ([[https://github.com/dalanicolai/djvu3#comments][comment]]) |
| ~C-mouse-1-drag~ | text pushpin (rendering pushpin not, yet, implemented) |
| ~mouse-2-drag~ | line |
| ~S-mouse-2-drag~ | horizontal line |
| ~C-mouse-2-drag~ | vertical line |
| ~C-S-mouse-2-drag~ | arrow |

*djvu-image-minor-mode*
| ~s~ | save-image |

*djvu-occur-mode*
| ~C-j~ | next entry and follow |
| ~C-k~ | prevoius entry and follow |
24 changes: 24 additions & 0 deletions layers/+readers/djvu/config.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
;;; config.el --- Djvu Layer Configuration File for Spacemacs
;;
;; Copyright (c) 2012-2021 Sylvain Benner & Contributors
;;
;; Author: Daniel Nicolai <dalanicolai@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

(defvar djvu-semi-continuous-scrolling nil
"If non-nil, scroll using visual lines.")
96 changes: 96 additions & 0 deletions layers/+readers/djvu/funcs.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
;;; funcs.el --- DjVu Layer functions File for Spacemacs
;;
;; Copyright (c) 2021 Sylvain Benner & Contributors
;;
;; Author: Daniel Nicolai <dalanicolai@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.


(defun djvu-toggle-semi-continuous-scrolling ()
(interactive)
(setq djvu-semi-continuous-scrolling (not djvu-semi-continuous-scrolling))
(message "Djvu alternative scrolling %s" (if djvu-semi-continuous-scrolling
"enabled"
"disabled")))

(defun spacemacs/djvu-advise-image-toggle (_file &rest args)
(djvu-image-toggle))

;; djvu-continuous of djvu.el does not work with djvu3.el
(defun spacemacs/djvu-scroll-up-or-next-page ()
(interactive)
(if (not djvu-semi-continuous-scrolling)
(if djvu-image-mode
(djvu-image-scroll-up)
(evil-next-visual-line))
(scroll-up-line 5)
(when (= (window-vscroll) 0)
(djvu-next-page 1))))

(defun spacemacs/djvu-scroll-down-or-previous-page ()
(interactive)
(if (not djvu-semi-continuous-scrolling)
(if djvu-image-mode
(djvu-image-scroll-down)
(evil-previous-visual-line))
(if (/= (window-vscroll) 0)
(scroll-down-line 5)
(djvu-prev-page 1)
(scroll-up-command))))

(defun spacemacs/djvu-fast-search (regexp)
(interactive "sSearch (regexp): ")
(when djvu-image-mode
(djvu-image-toggle))
(spacemacs/djvu-search-forward regexp))

(defun spacemacs/djvu-search-forward (query)
"Search forward for match for REGEXP.
Search case-sensitivity is determined by the value of the variable
`case-fold-search', see Info node `(elisp)Searching and Case'.
Use the command `djvu-search-forward-continue' to continue the search."
(interactive "sQuery: ")
(setq djvu-last-search-re query)
(unless (eq (djvu-ref page) (djvu-ref pagemax))
(search-forward query nil t))
(djvu-goto-page (let ((page (djvu-ref page))
(return 1))
(while (and (/= return 0) (< page (+ (djvu-ref pagemax) 1)))
(setq page (1+ page))
(setq return (call-process-shell-command
(format "djvused %s -e 'select %s; print-pure-txt' | grep -i '%s'"
(shell-quote-argument buffer-file-name)
page
query))))
page))
(search-forward query nil t))

(defun spacemacs/djvu-re-search-forward-continue ()
"Continue search forward for match for `djvu-last-search-re'."
(interactive)
(spacemacs/djvu-search-forward djvu-last-search-re))

(defun djvu-occur-next-entry-and-follow ()
(interactive)
(evil-next-visual-line)
(call-interactively 'djvu-occur-show-entry))

(defun djvu-occur-previous-entry-and-follow ()
(interactive)
(evil-previous-visual-line)
(call-interactively 'djvu-occur-show-entry))
12 changes: 12 additions & 0 deletions layers/+readers/djvu/img/djvu-logo.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions layers/+readers/djvu/layers.el
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
(configuration-layer/declare-layer 'spacemacs-evil)
111 changes: 111 additions & 0 deletions layers/+readers/djvu/packages.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
;;; packages.el --- djvu layer packages file for Spacemacs.
;;
;; Copyright (c) 2021 Sylvain Benner & Contributors
;;
;; Author: Daniel Laurens Nicolai <dalanicolai@gmail.com>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(defconst djvu-packages
'(djvu
tablist
(djvu3 :location (recipe
:fetcher github
:repo "dalanicolai/djvu3"))))

(defun djvu/init-djvu ()
(use-package djvu
:defer t
:magic ("%DJVU" . djvu-read-mode)))

(defun djvu/init-tablist ()
(use-package tablist
:defer t))

(defun djvu/init-djvu3 ()
(use-package djvu3
:after djvu
:init
(add-to-list 'spacemacs-large-file-modes-list 'djvu-read-mode t)
(add-hook 'djvu-read-mode-hook (lambda () (setq imenu-create-index-function #'djvu-imenu-create-index)))
(add-hook 'djvu-read-mode-hook (lambda ()
(setq imenu-default-goto-function (lambda (title page)
(djvu-goto-page page djvu-doc)))))

:config
(advice-add 'djvu-find-file :after #'spacemacs/djvu-advise-image-toggle)

(evilified-state-evilify-map djvu-read-mode-map
:mode djvu-read-mode
:bindings
"j" 'spacemacs/djvu-scroll-up-or-next-page
"k" 'spacemacs/djvu-scroll-down-or-previous-page
"J" 'djvu-next-page
"K" 'djvu-prev-page
"g" 'djvu-goto-page
"/" 'spacemacs/djvu-fast-search
"n" 'spacemacs/djvu-re-search-forward-continue
"H" 'djvu-history-backward
"L" 'djvu-history-forward
"c" 'djvu-toggle-semi-continuous-scrolling
"d" 'djvu-toggle-invert
"r" 'djvu-revert-buffer
"q" 'djvu-kill-doc)

(define-key djvu-image-mode-map "s" 'image-save)

(spacemacs/declare-prefix-for-mode 'djvu-read-mode "mb" "buffers")
(spacemacs/set-leader-keys-for-major-mode 'djvu-read-mode
"s" 'djvu-occur
"h" 'djvu-keyboard-annot
"d" 'djvu-toggle-invert
"i" 'djvu-image-toggle
"bs" 'djvu-switch-shared
"bo" 'djvu-switch-outline
"bt" 'djvu-switch-text
"ba" 'djvu-switch-annot
"bb" 'djvu-switch-bookmarks)

;; for some reason can not use dolist here
(define-key djvu-read-mode-map [remap save-buffer] 'djvu-save)
(define-key djvu-script-mode-map [remap save-buffer] 'djvu-save)
(define-key djvu-outline-mode-map [remap save-buffer] 'djvu-save)

(spacemacs/set-leader-keys-for-major-mode 'djvu-script-mode
"r" 'djvu-switch-read
"s" 'djvu-switch-shared
"o" 'djvu-switch-outline
"t" 'djvu-switch-text
"a" 'djvu-switch-annot
"b" 'djvu-switch-bookmarks)

(evilified-state-evilify-map djvu-outline-mode-map
:mode djvu-outline-mode
:bindings
"r" 'djvu-revert-buffer
"q" 'djvu-quit-window)

(evilified-state-evilify-map djvu-occur-mode-map
:mode djvu-occur-mode
:bindings
(kbd "C-j") 'djvu-occur-next-entry-and-follow
(kbd "C-k") 'djvu-occur-previous-entry-and-follow
"r" 'tablist-revert)))

;;; packages.el ends here

0 comments on commit 2e86f64

Please sign in to comment.