Add: (org-ql--ensure-buffer)

And use in org-ql-find.  This seems to solve a problem I've been
noticing in helm-org-ql for a long time: that it can apparently
interrupt Org's initialization code, which can leave buffers
unprepared for Org, even stuck in fundamental-mode.  This should also
be used in helm-org-ql...
This commit is contained in:
Adam Porter 2022-06-25 10:56:18 -05:00
parent bbd164c6ae
commit dc02c59cad
2 changed files with 27 additions and 0 deletions

View file

@ -184,6 +184,19 @@ single predicate)."
(optional (repeat 1 3 (0+ space) (repeat 1 15 (not space)))))))))) (optional (repeat 1 3 (0+ space) (repeat 1 15 (not space))))))))))
(org-ql-select buffers-files (org-ql--query-string-to-sexp (concat query-prefix str)) (org-ql-select buffers-files (org-ql--query-string-to-sexp (concat query-prefix str))
:action #'action)))))) :action #'action))))))
;; NOTE: It seems that the `completing-read' machinery can call,
;; abort, and re-call the collection function while the user is
;; typing, which can interrupt the machinery Org uses to prepare
;; an Org buffer when an Org file is loaded. This results in,
;; e.g. the buffer being left in fundamental-mode, unprepared to
;; be used as an Org buffer, which breaks many things and is
;; very confusing for the user. Ideally, of course, we would
;; solve this in `org-ql-select', and we already attempt to, but
;; that function is called by the `completing-read' machinery,
;; which interrupts it, so we must work around this problem by
;; ensuring all of the BUFFERS-FILES are loaded and initialized
;; before calling `completing-read'.
(mapc #'org-ql--ensure-buffer buffers-files)
(let* ((completion-styles '(org-ql-find)) (let* ((completion-styles '(org-ql-find))
(completion-styles-alist (list (list 'org-ql-find #'try #'all "Org QL Find"))) (completion-styles-alist (list (list 'org-ql-find #'try #'all "Org QL Find")))
(selected (completing-read prompt #'collection nil)) (selected (completing-read prompt #'collection nil))

View file

@ -535,6 +535,20 @@ If NARROW is non-nil, buffer will not be widened."
;;;;; Helpers ;;;;; Helpers
(defun org-ql--ensure-buffer (file-or-buffer)
"Ensure a buffer is named or visiting FILE-OR-BUFFER.
If no such buffer exists with the name, and it is the name of a
readable file, `find-file-noselect' it into a buffer."
;; See comment in `org-ql-find'.
;; FIXME: Use this in `helm-org-ql' the same way it's used in
;; `org-ql-find'.
(unless (or (get-buffer file-or-buffer)
(find-buffer-visiting file-or-buffer))
(if (file-readable-p file-or-buffer)
(with-current-buffer (find-file-noselect file-or-buffer)
(cl-assert (eq 'org-mode major-mode) nil (format "Not an Org buffer: %S" file-or-buffer)))
(display-warning 'org-ql (format "Not a readable file: %S" file-or-buffer) :error))))
(defun org-ql--tags-at (position) (defun org-ql--tags-at (position)
;; FIXME: This function actually assumes that point is already at POSITION. ;; FIXME: This function actually assumes that point is already at POSITION.
"Return tags for POSITION in current buffer. "Return tags for POSITION in current buffer.