wasamasa / nov.el

Major mode for reading EPUBs in Emacs
GNU General Public License v3.0
655 stars 34 forks source link

Following org link always opens a new buffer #67

Closed gdindi closed 4 years ago

gdindi commented 4 years ago

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!

AloisJanicek commented 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)
gdindi commented 4 years ago

Thanks. This seems to do the job!