-;;; djvu.el --- edit and view Djvu files via djvused
+;;; djvu.el --- Edit and view Djvu files via djvused
;; Copyright (C) 2011 Free Software Foundation, Inc.
;; You should have received a copy of the GNU General Public License
;; along with djvu.el. If not, see <http://www.gnu.org/licenses/>.
-;;; Commentary
+;;; Commentary:
-;; djvu.el is a front end for the command-line program djvused
-;; from DjVuLibre, see http://djvu.sourceforge.net/
-;;
-;; This code requires you have the programs djvused, djview, and ddjvu
-;; installed.
+;; This package is a front end for the command-line program djvused
+;; from DjVuLibre, see http://djvu.sourceforge.net/. It assumes you
+;; have the programs djvused, djview, and ddjvu installed.
;;
;; A normal work flow is as follows:
;;
-;; To visit a djvu file use `fjvu-find-file'. This command is the
-;; only entry point to this package. You may want to bind this command
-;; to a key you like. I use
+;; To visit a djvu file type M-x fjvu-find-file. This command is the
+;; only entry point to this package. You may want to bind this
+;; command to a key you like. I use
;;
;; (global-set-key "\C-cd" 'djvu-find-file)
;;
-;; If you use this command to visit file foo.djvu, it puts you into the
-;; (not editable) buffer foo@djvu. Normally, this buffer is all you
+;; If you use this command to visit file foo.djvu, it puts you into
+;; the (read-only) buffer foo@djvu. Normally, this buffer is all you
;; need.
;;
;; The menu bar of this buffer lists most of the commands with their
-;; repsective key bindings. For example, you can:
+;; repsective key bindings. For example, you can:
;;
;; - Use `g' to go to the page you want. (Yes, this package operates on
;; one page at a time. I guess that anything else would be too slow
;; for large documents.)
;;
;; - Use `v' to (re)start djview using the position in foo.djvu
-;; matching where point is in foo@djvu. (I find djview fast enough
+;; matching where point is in foo@djvu. (I find djview fast enough
;; for this, even for larger documents.)
;;
-;; - To highlight a region in foo.djvu mark the corresponding region in
-;; foo@djvu (as usual, `transient-mark-mode' comes handy for this).
-;; Then type `h' and add a comment in the minibuffer if you like.
-;; Type C-x C-s to save this editing. Then type `v' to (re)start
-;; djview to show what you have done.
+;; - To highlight a region in foo.djvu mark the corresponding region
+;; in foo@djvu (as usual, `transient-mark-mode' comes handy for
+;; this). Then type `h' and add a comment in the minibuffer if you
+;; like. Type C-x C-s to save this editing. Then type `v' to
+;; (re)start djview to show what you have done.
;;
;; - Type i to enable `djvu-image-mode', a minor mode displaying the
-;; current page as an image. Then
+;; current page as an image. Then
;; drag-mouse-1 defines a region where to put a comment,
;; C-drag-mouse-1 defines a region where to put a pushpin comment,
;; S-drag-mouse-1 defines a region to highlight
;;
;; - The editing of the text, annotation and outline (bookmark) layers
;; really happens in the buffers foo@djvu-t.el, foo@djvu-a.el, and
-;; foo@djvu-o.el. (The djvused syntax used in these buffers is so
+;; foo@djvu-o.el. (The djvused syntax used in these buffers is so
;; close to elisp that it was natural to give these buffers a
;; djvu-edit-mode that is derived from emacs-lisp-mode.)
;;
-;; You can check what is happening by switching to these buffers. The
-;; respective switching commands put point in these buffers such that
-;; it matches where you were in foo@djvu.
+;; You can check what is happening by switching to these buffers.
+;; The respective switching commands put point in these buffers such
+;; that it matches where you were in foo@djvu.
;;
;; In these buffers, the menu bar lists a few low-level commands
-;; available for editing these buffers directly. If you know the
+;; available for editing these buffers directly. If you know the
;; djvused syntax, sometimes it can also be helpful to do such
;; editing "by hand".
;;
-;; But wait: The syntax in the annotations buffer foo@djvu-a.el is a
-;; slightly modified djvused syntax. djvused can only highlight
-;; rectangles. So the highlighting of larger regions of text must
-;; use multiple rectangles (i.e., multiple djvused "mapareas").
-;; To make editing easier, these are combined in the buffer foo@djvu-a.el.
+;; But wait: the syntax in the annotations buffer foo@djvu-a.el is a
+;; slightly modified djvused syntax. djvused can only highlight
+;; rectangles. So the highlighting of larger regions of text must use
+;; multiple rectangles (i.e., multiple djvused "mapareas"). To make
+;; editing easier, these are combined in the buffer foo@djvu-a.el.
;; (Before saving these things, they are converted using the proper
;; djvused syntax.)
;;
-;; When you visit a djvu file, djvu-mode recognizes mapareas
-;; belonging together by checking that "everything else in these
-;; mapareas except for the rects" is the same. So if you entered a
-;; (unique) comment, this allows djvu-mode to combine all the
-;; mapareas when you visit such a file the second time. Without a
-;; comment, this fails!
+;; When you visit a djvu file, djvu-mode recognizes mapareas belonging
+;; together by checking that "everything else in these mapareas except
+;; for the rects" is the same. So if you entered a (unique) comment,
+;; this allows djvu-mode to combine all the mapareas when you visit
+;; such a file the second time. Without a comment, this fails!
;;
;; A second difference between what is displayed in the djvu buffers
-;; and the input/output of djvused refers to nonascii characters.
-;; I am using djvused from DjVuLibre-3.5.22 which handles utf-8 by
-;; backslash sequences. So djvu mode converts these backslash
-;; sequences into the corresponding utf-8 characters. (More recent
+;; and the input/output of djvused refers to nonascii characters. I
+;; am using djvused from DjVuLibre-3.5.22 which handles utf-8 by
+;; backslash sequences. So djvu mode converts these backslash
+;; sequences into the corresponding utf-8 characters. (More recent
;; versions of djvused can do this conversion, too.)
-;;
+
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;;
;; Djvu internals:
;; (see /usr/share/doc/libdjvulibre-dev/djvu3spec.djvu)
;;
-;; Area attribute rect oval poly line text
+;; Supported area attributes rect oval poly line text
;; (none)/(xor)/(border c) X X X X X
;; (shadow_* t) X
;; (border_avis) X X X
;; c = #RRGGBB t = thickness (1..32)
;; o = opacity = 0..100
-;;; Code
+;;; Code:
(defvar djvu-color-highlight "yellow"
"Default color for highlighting.")
(interactive (list (completing-read "Color: " djvu-color-alist nil t)))
(setq djvu-color-highlight color))
-(defun djvu-kill-view ()
- (when (djvu-doc-view-proc)
- (unless (memq (process-status (djvu-doc-view-proc))
+(defun djvu-kill-view (&optional doc)
+ (when (djvu-doc-view-proc doc)
+ (unless (memq (process-status (djvu-doc-view-proc doc))
'(exit signal))
- (kill-process (djvu-doc-view-proc)))
- (djvu-doc-set-view-proc nil)))
+ (kill-process (djvu-doc-view-proc doc)))
+ (djvu-doc-set-view-proc nil doc)))
-(defun djvu-kill-doc ()
+(defun djvu-kill-doc (&optional doc)
(interactive)
- (djvu-save nil t)
- (djvu-kill-view)
- (mapc 'kill-buffer (list (djvu-doc-text-buf) (djvu-doc-read-buf)
- (djvu-doc-annot-buf) (djvu-doc-outline-buf))))
+ (djvu-save doc t)
+ (djvu-kill-view doc)
+ (mapc 'kill-buffer (list (djvu-doc-text-buf doc) (djvu-doc-read-buf doc)
+ (djvu-doc-annot-buf doc) (djvu-doc-outline-buf doc))))
(defsubst djvu-delete-file (script)
(unless djvu-test (delete-file script)))
(unless (file-regular-p file)
(error "Cannot open Djvu file `%s'." file))
;; Initialize `djvu-doc' for FILE.
- (let* ((doc (make-vector djvu-doc-length nil))
- (djvu-doc doc)
- (basename (file-name-sans-extension
+ (let* ((basename (file-name-sans-extension
(file-name-nondirectory file)))
(read-buf (concat basename (nth 0 djvu-buffer-name-extensions)))
(text-buf (concat basename (nth 1 djvu-buffer-name-extensions)))
(annot-buf (concat basename (nth 2 djvu-buffer-name-extensions)))
(outline-buf (concat basename (nth 3 djvu-buffer-name-extensions)))
- (buffers (list text-buf read-buf annot-buf outline-buf)))
+ (buffers (list text-buf read-buf annot-buf outline-buf))
+ doc)
;; Do nothing if we are already visiting FILE such that all required
;; buffers are properly defined. If some buffers were killed
;; do not attempt to recycle the remaining buffers.
- (unless (eval (cons 'and (mapcar 'get-buffer buffers)))
+ (if (eval (cons 'and (mapcar 'get-buffer buffers)))
+ (with-current-buffer read-buf
+ (setq doc djvu-doc))
+ (setq doc (make-vector djvu-doc-length nil))
(dolist (buf buffers)
(if (get-buffer buf) (kill-buffer buf)))
- (djvu-doc-set-file file)
- (djvu-doc-set-basename basename)
- (djvu-doc-set-text-buf (get-buffer-create text-buf))
- (djvu-doc-set-read-buf (get-buffer-create read-buf))
- (djvu-doc-set-annot-buf (get-buffer-create annot-buf))
- (djvu-doc-set-outline-buf (get-buffer-create outline-buf))
+ (djvu-doc-set-file file doc)
+ (djvu-doc-set-basename basename doc)
+ (djvu-doc-set-text-buf (get-buffer-create text-buf) doc)
+ (djvu-doc-set-read-buf (get-buffer-create read-buf) doc)
+ (djvu-doc-set-annot-buf (get-buffer-create annot-buf) doc)
+ (djvu-doc-set-outline-buf (get-buffer-create outline-buf) doc)
;; Initialize all buffers.
- (dolist (buf (list (djvu-doc-text-buf) (djvu-doc-annot-buf)
- (djvu-doc-outline-buf)))
+ (dolist (buf (list (djvu-doc-text-buf doc) (djvu-doc-annot-buf doc)
+ (djvu-doc-outline-buf doc)))
(with-current-buffer buf
(djvu-edit-mode)
(setq djvu-doc doc)
(cd (file-name-directory (djvu-doc-file)))
(add-hook 'post-command-hook 'djvu-modified nil t)))
- (with-current-buffer (djvu-doc-read-buf)
+ (with-current-buffer (djvu-doc-read-buf doc)
(djvu-read-mode)
(setq djvu-doc doc)
(cd (file-name-directory (djvu-doc-file)))
(interactive (list djvu-doc))
(if (not (window-system))
(message "No window system available")
- (djvu-kill-view)
+ (djvu-kill-view doc)
(let* ((djvu-doc doc)
(pos (or (djvu-doc-pos) (djvu-read-pos)))
(px (/ (float (car pos))