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 3 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
31 changes: 17 additions & 14 deletions org-ql-view.el
Original file line number Diff line number Diff line change
Expand Up @@ -1051,20 +1051,23 @@ current buffer. Otherwise BUFFERS-FILES is returned unchanged."
(_ buffers-files))))
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved

(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.
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))))))
"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."
(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.
org-ql-view-buffers-files
(let ((initial-input (when org-ql-view-buffers-files
(org-ql-view--contract-buffers-files
org-ql-view-buffers-files))))
(if (or (not initial-input) (stringp initial-input))
(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-buffers-files))))

(defun org-ql-view--expand-buffers-files (buffers-files)
"Return BUFFERS-FILES expanded to a list of files or buffers.
Expand Down
79 changes: 78 additions & 1 deletion tests/test-org-ql.el
Original file line number Diff line number Diff line change
Expand Up @@ -2111,7 +2111,84 @@ with keyword arg NOW in PLIST."
(it "Refuses to link to non-file-backed buffer"
(expect (var-after-link-save-open 'org-ql-view-buffers-files link-buffer query
:buffer link-buffer)
:to-throw 'user-error '("Views that search non-file-backed buffers can’t be linked to"))))))
:to-throw 'user-error '("Views that search non-file-backed buffers can’t be linked to"))))
(describe "Completion for Files/buffers"
(describe "Contracting org-ql-view-buffers-files"
(it "org-agenda-files from list"
(spy-on 'org-agenda-files :and-return-value temp-filenames)
(expect (org-ql-view--contract-buffers-files temp-filenames) :to-equal "org-agenda-files"))
(it "org-directory"
(spy-on 'org-ql-search-directories-files :and-return-value temp-filenames)
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(spy-on 'org-agenda-files :and-return-value '())
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(expect (org-ql-view--contract-buffers-files temp-filenames) :to-equal temp-filenames))
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(it "buffer"
(with-current-buffer (org-ql-test-data-buffer "data.org")
(expect (org-ql-view--contract-buffers-files (current-buffer)) :to-equal "buffer")))
(it "org-agenda-files from symbol"
(spy-on 'org-agenda-files :and-return-value temp-filenames)
(expect (org-ql-view--contract-buffers-files 'org-agenda-files) :to-equal "org-agenda-files")
(expect (org-ql-view--contract-buffers-files #'org-agenda-files) :to-equal "org-agenda-files"))
(it "Non specific list"
(let ((value1 '("a.org" "b.org"))
(value2 'a))
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(expect (org-ql-view--contract-buffers-files value1) :to-equal value1)
(expect (org-ql-view--contract-buffers-files value2) :to-equal value2))))
(describe "Expanding org-ql-view-buffers-files"
(it "all"
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(let* ((buffer-names (list (make-temp-file "test" nil ".org") (make-temp-file "test" nil ".other")))
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(buffers (mapcar #'get-buffer-create buffer-names)))
(save-excursion
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(switch-to-buffer (car buffers))
(org-mode))
(spy-on 'buffer-list :and-return-value buffers)
(expect (org-ql-view--expand-buffers-files "all") :to-equal (list (car buffers)))))
(it "org-agenda-files"
(spy-on 'org-agenda-files :and-return-value "value for org-agenda-files")
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(expect (org-ql-view--expand-buffers-files "org-agenda-files") :to-equal "value for org-agenda-files"))
(it "org-directory"
(spy-on 'org-ql-search-directories-files :and-return-value "value for org-directory")
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(expect (org-ql-view--expand-buffers-files "org-directory") :to-equal "value for org-directory"))
(it "buffer"
(with-temp-buffer
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(expect (org-ql-view--expand-buffers-files "buffer") :to-equal (current-buffer))))
(it "literal values"
(with-temp-buffer
(expect (org-ql-view--expand-buffers-files (current-buffer)) :to-equal (current-buffer)))
(let ((test-buffer (get-buffer-create (make-temp-file "test"))))
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(expect (org-ql-view--expand-buffers-files test-buffer) :to-equal test-buffer))
(let ((test-list '(1 2 3)))
(expect (org-ql-view--expand-buffers-files test-list) :to-equal test-list))
(let ((string-literal "this is a string"))
(expect (org-ql-view--expand-buffers-files string-literal) :to-equal string-literal))))
(describe "Testing org-ql-view--complete-buffers-files"
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(it "org-agenda-files"
(let ((org-ql-view-buffers-files temp-filenames))
(spy-on 'org-ql-view--contract-buffers-files :and-call-through)
(spy-on 'org-agenda-files :and-return-value temp-filenames)
(spy-on 'completing-read :and-return-value "org-agenda-files")
(expect (org-ql-view--complete-buffers-files) :to-equal temp-filenames)
(expect 'org-ql-view--contract-buffers-files :to-have-been-called-with temp-filenames)
(expect 'completing-read :to-have-been-called-with "Buffers/Files: "
(list 'buffer 'org-agenda-files 'org-directory 'all)
nil nil "org-agenda-files")))
(it "nil"
(let ((org-ql-view-buffers-files nil))
(spy-on 'completing-read :and-return-value nil)
(expect (org-ql-view--complete-buffers-files) :to-equal nil)
(expect 'completing-read :to-have-been-called-with "Buffers/Files: "
(list 'buffer 'org-agenda-files 'org-directory 'all)
nil nil nil)))
(it "list"
(let ((org-ql-view-buffers-files '(1 2 3)))
(spy-on 'completing-read :and-return-value nil)
(expect (org-ql-view--complete-buffers-files) :to-equal org-ql-view-buffers-files)
(expect 'completing-read :not :to-have-been-called)))
(it "buffer"
(let ((org-ql-view-buffers-files (get-buffer-create (make-temp-file "test"))))
ahmed-shariff marked this conversation as resolved.
Show resolved Hide resolved
(spy-on 'completing-read :and-return-value nil)
(expect (org-ql-view--complete-buffers-files) :to-equal org-ql-view-buffers-files)
(expect 'completing-read :not :to-have-been-called)))))))


;; MAYBE: Also test `org-ql-views', although I already know it works now.
;; (describe "org-ql-views")
Expand Down