diff --git a/org-ql-find.el b/org-ql-find.el index bc92277..6a4e7c1 100644 --- a/org-ql-find.el +++ b/org-ql-find.el @@ -184,6 +184,19 @@ single predicate)." (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)) :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)) (completion-styles-alist (list (list 'org-ql-find #'try #'all "Org QL Find"))) (selected (completing-read prompt #'collection nil)) diff --git a/org-ql.el b/org-ql.el index 3747950..0cf6c8a 100644 --- a/org-ql.el +++ b/org-ql.el @@ -535,6 +535,20 @@ If NARROW is non-nil, buffer will not be widened." ;;;;; 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) ;; FIXME: This function actually assumes that point is already at POSITION. "Return tags for POSITION in current buffer.