Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix for issue with initial-input in org-ql-view--complete-buffers-files (Fix #227) #228

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.org
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Read ~QUERY~ and search with ~org-ql~. Interactively, prompt for these variable
+ ~buffer~: search the current buffer
+ ~all~: search all Org buffers
+ ~agenda~: search buffers returned by the function ~org-agenda-files~
+ A space-separated list of file or buffer names
+ A comma-separated list of file, buffer names, or the above keywords

~GROUPS~: An ~org-super-agenda~ group set. See variable ~org-super-agenda-groups~.

Expand Down
145 changes: 102 additions & 43 deletions org-ql-view.el
Original file line number Diff line number Diff line change
Expand Up @@ -658,9 +658,12 @@ When opened, the link searches the buffer it's opened from."
'("file link is in" "files currently searched")
nil t nil nil "file link is in")
("file link is in" nil)
("files currently searched" buffers-files)))
("files currently searched" (cl-typecase buffers-files
(function (funcall buffers-files))
(otherwise buffers-files)))))
(strings-or-file-buffers-p
(thing) (cl-etypecase thing
(functionp t)
(list (cl-every #'strings-or-file-buffers-p thing))
(string thing)
(buffer (or (buffer-file-name thing)
Expand Down Expand Up @@ -1032,54 +1035,110 @@ current buffer. Otherwise BUFFERS-FILES is returned unchanged."
(string (expand-file-name it))
(otherwise it))
list)))
;; TODO: Test this more exhaustively.
(pcase buffers-files
((pred listp)
(pcase (expand-files buffers-files)
((pred (seq-set-equal-p (mapcar #'expand-file-name (org-agenda-files))))
"org-agenda-files")
((and (guard (file-exists-p org-directory))
(pred (seq-set-equal-p (org-ql-search-directories-files
:directories (list org-directory)))))
"org-directory")
(_ buffers-files)))
((pred (equal (current-buffer)))
"buffer")
((or 'org-agenda-files '(function org-agenda-files))
"org-agenda-files")
((and (pred bufferp) (guard (buffer-file-name buffers-files)))
(buffer-file-name buffers-files))
(_ buffers-files))))
(let ((contracted-buffers-files
;; TODO: Test this more exhaustively.
(pcase buffers-files
((pred functionp) (pcase buffers-files
('org-agenda-files "org-agenda-files")
(_ buffers-files)))
((pred listp)
(pcase (expand-files buffers-files)
((pred (seq-set-equal-p (mapcar #'expand-file-name (org-agenda-files))))
"org-agenda-files")
((and (guard (file-exists-p org-directory))
(pred (seq-set-equal-p (org-ql-search-directories-files
:directories (list org-directory)))))
"org-directory")
(_ buffers-files)))
((pred (equal (current-buffer)))
"buffer")
((or 'org-agenda-files '(function org-agenda-files))
"org-agenda-files")
((and (pred bufferp) (guard (buffer-file-name buffers-files)))
(buffer-file-name buffers-files))
((pred bufferp)
(buffer-name buffers-files))
(_ buffers-files))))
;; To filter duplicates with the extend counterpart of this function,
;; this needs to be a string or a list of string.
;; Hence, making sure the buffers are convered to file names or buffer names.
;; Using file-names when it's a file-buffer to avoid duplicates resulting from
;; the file-buffer and file name being entered.
(cl-typecase contracted-buffers-files
(function contracted-buffers-files)
(string contracted-buffers-files)
(list (--> contracted-buffers-files
-flatten -non-nil
(--map
(pcase-exhaustive it
((pred stringp) it)
((pred bufferp) (or (buffer-file-name it)
(buffer-name buffer-file))))
it)
-uniq))
(t (error (format "Value %s is not a string, a valid function or a list of buffer/strings" contracted-buffers-files)))))))

(defun org-ql-view--complete-buffers-files ()
"Return value for `org-ql-view-buffers-files' using completion."
(cl-labels ((initial-input
() (when org-ql-view-buffers-files
(org-ql-view--contract-buffers-files
org-ql-view-buffers-files))))
(if (and org-ql-view-buffers-files
(bufferp org-ql-view-buffers-files))
;; Buffers can't be input by name, so if the default value is a buffer, just use it.
;; TODO: Find a way to fix this.
"Return value for `org-ql-view-buffers-files' using completion.
When `org-ql-view-buffers-files' cannot be contracted to a string
representation `org-ql-view-buffers-files' is returned."
(let* ((contracted-org-ql-view-buffers-files
(when org-ql-view-buffers-files
(org-ql-view--contract-buffers-files
org-ql-view-buffers-files)))
(initial-input (pcase contracted-org-ql-view-buffers-files
('nil nil)
('string contracted-org-ql-view-buffers-files)
((pred functionp) contracted-org-ql-view-buffers-files)
((pred listp)
(mapconcat 'identity contracted-org-ql-view-buffers-files
","))
(_ (format "%s" contracted-org-ql-view-buffers-files))))
(completion-read-result (if (functionp contracted-org-ql-view-buffers-files)
contracted-org-ql-view-buffers-files
(completing-read-multiple
"Buffers/Files: "
(list 'buffer 'org-agenda-files 'org-directory 'all)
nil nil initial-input))))
(if (equal completion-read-result initial-input)
org-ql-view-buffers-files
(org-ql-view--expand-buffers-files
(completing-read "Buffers/Files: "
(list 'buffer 'org-agenda-files 'org-directory 'all)
nil nil (initial-input))))))
(org-ql-view--expand-buffers-files completion-read-result))))

(defun org-ql-view--expand-buffers-files (buffers-files)
"Return BUFFERS-FILES expanded to a list of files or buffers.
The counterpart to `org-ql-view--contract-buffers-files'."
(pcase-exhaustive buffers-files
("all" (--select (equal (buffer-local-value 'major-mode it) 'org-mode)
(buffer-list)))
("org-agenda-files" (org-agenda-files))
("org-directory" (org-ql-search-directories-files))
((or "" "buffer") (current-buffer))
((pred bufferp) buffers-files)
((pred listp) buffers-files)
;; A single filename.
((pred stringp) buffers-files)))
The counterpart to `org-ql-view--contract-buffers-files'.
This always returns a list of string values."
(let ((expanded-buffers-files
(--> buffers-files
-list -non-nil
(-map (lambda (buffer-file)
(pcase-exhaustive buffer-file
("all" (--select (equal (buffer-local-value 'major-mode it) 'org-mode)
(buffer-list)))
("org-agenda-files" (org-agenda-files))
("org-directory" (org-ql-search-directories-files))
((or "" "buffer")
(current-buffer))
((or (pred bufferp)
;; A single filename.
(pred stringp))
buffer-file)
(_ (error (format "Value %s is not a valid buffer/file" buffer-file)))))
it))))
(--> expanded-buffers-files
-flatten
;; removing `nil' again as some values have been expanded.
-non-nil
;; Expanding all buffers to file names or buffer names to remove duplicate entries.
(--map
(pcase-exhaustive it
((pred bufferp) (or (buffer-file-name it)
(buffer-name it)))
;; Any values at this point should be a buffer or string.
;; Testing for string anyways.
((pred stringp) it))
it)
-uniq)))
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved

(defun org-ql-view--complete-super-groups ()
"Return value for `org-ql-view-super-groups' using completion."
Expand Down
1 change: 1 addition & 0 deletions org-ql.el
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,7 @@ each priority the newest items would appear first."
;; It feels unintuitive that `find-file-noselect' returns
;; a buffer if the filename doesn't exist.
(find-file-noselect it))
(get-buffer it)
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(user-error "Can't open file: %s" it)))))
;; Ignore special/hidden buffers.
(--remove (string-prefix-p " " (buffer-name it)))))
Expand Down
Loading