Closed gdindi closed 4 years ago
This function will ensure there is always just one buffer for each epub file. Multiple nov-mode windows are allowed only if they are displaying different epub files. And also always open books into other window.
(defun my/nov--find-file (file index point)
"Open FILE(nil means current buffer) in nov-mode and go to the specified INDEX and POSITION.
Prevent opening same FILE into multiple windows or buffers. Always reuse them if possible."
(let ((same-epub
(car (remove nil
(mapcar
(lambda (buf)
(with-current-buffer buf
(if (and (eq major-mode 'nov-mode)
(string-equal nov-file-name file))
(if (get-buffer-window)
(cons buf (get-buffer-window))
buf))))
(buffer-list))))))
(if (consp same-epub)
(select-window (cdr same-epub))
(if same-epub
(switch-to-buffer-other-window same-epub)
(when file
(find-file-other-window file))))
(unless (eq major-mode 'nov-mode)
(nov-mode))
(when (not (nov--index-valid-p nov-documents index))
(error "Invalid documents index"))
(setq nov-documents-index index)
(nov-render-document)
(goto-char point)))
you can try it by advising existing function:
(advice-add #'nov--find-file :override #'my/nov--find-file)
Thanks. This seems to do the job!
Hi, First of all, thanks for the new functionality allowing interoperation with org-mode. I think it would be nice not to open a new buffer when following an org link if the epub file is already visited by another buffer. Otherwise, we end up with many buffers visiting the same file. Thanks!