bastibe / org-journal

A simple org-mode based journaling mode
BSD 3-Clause "New" or "Revised" License
1.23k stars 122 forks source link

After a recently upgrade of org, an error of "cannot be used in non-org buffer" raised #414

Closed zhyzky closed 9 months ago

zhyzky commented 10 months ago

Describe the bug An error with "cannot be used in non-org buffer" raised with the org commit https://github.com/emacs-straight/org-mode/commit/f1359546ad7dee9698b75ca25ad453e6f054bd06 the issue is clear, so maybe need an update of the code

To Reproduce Just invoke org-journal-prev-entry or anything else

Expected behavior org-journal just work well

Desktop (please complete the following information):

Your Emacs Configuration has nothing todo with this

zhyzky commented 10 months ago

By the way, I set org-journal-file-type to 'weekly

zhyzky commented 10 months ago

I finally got that the issue is with macro org-journal--with-journal, within which (generate-new-buffer *) was called and in this buffer, its major mode is fundamental. That's why it raised the error with non-org mode.

After re-write the macro:

(defmacro org-journal--with-journal (file &rest body)
  "Opens JOURNAL-FILE in fundamental mode, or switches to the
buffer which is visiting JOURNAL-FILE.

Returns the last value from BODY. If the buffer didn't exist
before it will be deposed."
  ;; Use find-file... instead of view-file... since
  ;; view-file does not respect auto-mode-alist
  (declare (indent 1))
  `(let* ((buffer-exists (get-buffer (file-name-nondirectory ,file)))
          (buf (if buffer-exists buffer-exists
                 (find-file ,file)))
          result)
     (with-current-buffer buf
       (setq result (progn ,@body)))
     (unless buffer-exists
       (kill-buffer buf))
     result))

where there's no temp buffer exists, and the issue seemes fixed.

zhyzky commented 10 months ago

Update, much less code:

(defmacro org-journal--with-journal (file &rest body)
  "Opens JOURNAL-FILE in fundamental mode, or switches to the
buffer which is visiting JOURNAL-FILE.

Returns the last value from BODY. If the buffer didn't exist
before it will be deposed."
  ;; Use find-file... instead of view-file... since
  ;; view-file does not respect auto-mode-alist
  ;; with-temp-buffer and with explicit declare org-mode seems
  ;; to be a recommend way
   (declare (indent 1))
  `(let ((buffer-exists (get-buffer (file-name-nondirectory ,file))))
     (if buffer-exists
         (with-current-buffer buffer-exists (progn ,@body))
       (with-temp-buffer
         ;; (unless buffer-exists
         ;; (insert-file-contents ,file))
         (insert-file-contents ,file)
         ((let ((major-mode 'org-mode))
         (progn ,@body))))))
zhyzky commented 9 months ago

Since commit has been merged. Closed.