Cleaning up functions and improvements for clarity.
This commit is contained in:
parent
aa9c6ccdfa
commit
cea165192d
2 changed files with 73 additions and 64 deletions
|
|
@ -1032,7 +1032,7 @@ current buffer. Otherwise BUFFERS-FILES is returned unchanged."
|
||||||
(string (expand-file-name it))
|
(string (expand-file-name it))
|
||||||
(otherwise it))
|
(otherwise it))
|
||||||
list)))
|
list)))
|
||||||
(-->
|
(let ((contracted-buffers-files
|
||||||
;; TODO: Test this more exhaustively.
|
;; TODO: Test this more exhaustively.
|
||||||
(pcase buffers-files
|
(pcase buffers-files
|
||||||
((pred listp)
|
((pred listp)
|
||||||
|
|
@ -1052,19 +1052,21 @@ current buffer. Otherwise BUFFERS-FILES is returned unchanged."
|
||||||
(buffer-file-name buffers-files))
|
(buffer-file-name buffers-files))
|
||||||
((pred bufferp)
|
((pred bufferp)
|
||||||
(buffer-name buffers-files))
|
(buffer-name buffers-files))
|
||||||
(_ buffers-files))
|
(_ buffers-files))))
|
||||||
;; All items needs to be strings to pick duplicates when used with the extend conterpart.
|
;; To filter duplicates with the extend counterpart of this function,
|
||||||
;; So making sure the buffers are convered to file names
|
;; this needs to be a string or a list of string.
|
||||||
(if (stringp it)
|
;; Hence, making sure the buffers are convered to file names or buffer names.
|
||||||
it
|
;; Using file-names when it's a file-buffer to avoid duplicates resulting from
|
||||||
(-map
|
;; the file-buffer and file name being entered.
|
||||||
(lambda (buffer-file)
|
(cl-typecase contracted-buffers-files
|
||||||
(if (bufferp buffer-file)
|
(string contracted-buffers-files)
|
||||||
(--if-let (buffer-file-name buffer-file)
|
(list (--map
|
||||||
it
|
(pcase-exhaustive it
|
||||||
(buffer-name buffer-file))
|
((pred stringp) it)
|
||||||
buffer-file))
|
((pred bufferp) (or (buffer-file-name it)
|
||||||
it)))))
|
(buffer-name buffer-file))))
|
||||||
|
contracted-buffers-files))
|
||||||
|
(t (error (format "Value %s is not a string or a list of buffer/strings" contracted-buffers-files)))))))
|
||||||
|
|
||||||
(defun org-ql-view--complete-buffers-files ()
|
(defun org-ql-view--complete-buffers-files ()
|
||||||
"Return value for `org-ql-view-buffers-files' using completion.
|
"Return value for `org-ql-view-buffers-files' using completion.
|
||||||
|
|
@ -1093,7 +1095,9 @@ representation `org-ql-view-buffers-files' is returned."
|
||||||
"Return BUFFERS-FILES expanded to a list of files or buffers.
|
"Return BUFFERS-FILES expanded to a list of files or buffers.
|
||||||
The counterpart to `org-ql-view--contract-buffers-files'.
|
The counterpart to `org-ql-view--contract-buffers-files'.
|
||||||
This always returns a list of string values."
|
This always returns a list of string values."
|
||||||
(-->
|
(let ((expanded-buffers-files
|
||||||
|
(--> buffers-files
|
||||||
|
-list -non-nil
|
||||||
(-map (lambda (buffer-file)
|
(-map (lambda (buffer-file)
|
||||||
(pcase-exhaustive buffer-file
|
(pcase-exhaustive buffer-file
|
||||||
("all" (--select (equal (buffer-local-value 'major-mode it) 'org-mode)
|
("all" (--select (equal (buffer-local-value 'major-mode it) 'org-mode)
|
||||||
|
|
@ -1102,21 +1106,26 @@ This always returns a list of string values."
|
||||||
("org-directory" (org-ql-search-directories-files))
|
("org-directory" (org-ql-search-directories-files))
|
||||||
((or "" "buffer")
|
((or "" "buffer")
|
||||||
(current-buffer))
|
(current-buffer))
|
||||||
((pred bufferp) (list buffer-file))
|
((or (pred bufferp)
|
||||||
;; A single filename.
|
;; A single filename.
|
||||||
((pred stringp) (list buffer-file))
|
(pred stringp))
|
||||||
|
buffer-file)
|
||||||
(_ (error (format "Value %s is not a valid buffer/file" buffer-file)))))
|
(_ (error (format "Value %s is not a valid buffer/file" buffer-file)))))
|
||||||
(-list buffers-files))
|
it))))
|
||||||
-flatten -non-nil
|
(--> expanded-buffers-files
|
||||||
;; expanding all file-buffers to file names to avoid duplicate entries being formed
|
-flatten
|
||||||
(-map (lambda (buffer-file)
|
;; removing `nil' again as some values have been expanded.
|
||||||
(if (bufferp buffer-file)
|
-non-nil
|
||||||
(--if-let (buffer-file-name buffer-file)
|
;; Expanding all buffers to file names or buffer names to remove duplicate entries.
|
||||||
it
|
(--map
|
||||||
(buffer-name buffer-file))
|
(pcase-exhaustive it
|
||||||
buffer-file))
|
((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)
|
it)
|
||||||
-uniq))
|
-uniq)))
|
||||||
|
|
||||||
(defun org-ql-view--complete-super-groups ()
|
(defun org-ql-view--complete-super-groups ()
|
||||||
"Return value for `org-ql-view-super-groups' using completion."
|
"Return value for `org-ql-view-super-groups' using completion."
|
||||||
|
|
|
||||||
|
|
@ -2130,11 +2130,11 @@ with keyword arg NOW in PLIST."
|
||||||
(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")
|
||||||
(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 "arbitarary list of buffers/files"
|
(it "arbitarary list of buffers/files"
|
||||||
(let ((value1 '("a.org" "b.org"))
|
(let ((list-of-strings '("a.org" "b.org"))
|
||||||
(value2 'a))
|
(invalid-type 'a))
|
||||||
(expect (org-ql-view--contract-buffers-files value1) :to-equal value1)
|
(expect (org-ql-view--contract-buffers-files list-of-strings) :to-equal list-of-strings)
|
||||||
;; If the value does not result to a buffer, file, or string, throws error
|
;; Signal error if value is not a buffer, file, or string.
|
||||||
(expect (org-ql-view--contract-buffers-files value2) :to-throw))))
|
(expect (org-ql-view--contract-buffers-files invalid-type) :to-throw))))
|
||||||
(describe "expanding org-ql-view-buffers-files"
|
(describe "expanding org-ql-view-buffers-files"
|
||||||
(it "returns all buffers with `org-mode' as the major-mode"
|
(it "returns all buffers with `org-mode' as the major-mode"
|
||||||
(let ((buffers (list (generate-new-buffer "test.org") (generate-new-buffer "test.other"))))
|
(let ((buffers (list (generate-new-buffer "test.org") (generate-new-buffer "test.other"))))
|
||||||
|
|
@ -2150,7 +2150,7 @@ with keyword arg NOW in PLIST."
|
||||||
(generate-new-buffer "test2")))))
|
(generate-new-buffer "test2")))))
|
||||||
(expect (org-ql-view--expand-buffers-files "org-agenda-files") :to-equal org-agenda-files)))
|
(expect (org-ql-view--expand-buffers-files "org-agenda-files") :to-equal org-agenda-files)))
|
||||||
(it "returns values of \"org-directory\""
|
(it "returns values of \"org-directory\""
|
||||||
;; Also indirectly tests `org-ql-view--expand-buffers-files'
|
;; Also indirectly tests `org-ql-view--expand-buffers-files'.
|
||||||
(let ((org-directory temp-dir))
|
(let ((org-directory temp-dir))
|
||||||
(expect (org-ql-view--expand-buffers-files "org-directory") :to-equal temp-filenames)))
|
(expect (org-ql-view--expand-buffers-files "org-directory") :to-equal temp-filenames)))
|
||||||
(it "returns the current buffer"
|
(it "returns the current buffer"
|
||||||
|
|
@ -2163,7 +2163,7 @@ with keyword arg NOW in PLIST."
|
||||||
(expect (org-ql-view--expand-buffers-files test-buffer) :to-equal (list (buffer-name test-buffer))))
|
(expect (org-ql-view--expand-buffers-files test-buffer) :to-equal (list (buffer-name test-buffer))))
|
||||||
(let ((list-of-numbers '(1 2 3))
|
(let ((list-of-numbers '(1 2 3))
|
||||||
(literal-string "random string"))
|
(literal-string "random string"))
|
||||||
;; If the value does not result to a buffer, file, or string, throws error
|
;; Signal error if any of the values are not a buffer, file, or string.
|
||||||
(expect (org-ql-view--expand-buffers-files list-of-numbers) :to-throw)
|
(expect (org-ql-view--expand-buffers-files list-of-numbers) :to-throw)
|
||||||
(expect (org-ql-view--expand-buffers-files literal-string) :to-equal (list literal-string)))))
|
(expect (org-ql-view--expand-buffers-files literal-string) :to-equal (list literal-string)))))
|
||||||
(describe "testing `org-ql-view--complete-buffers-files'"
|
(describe "testing `org-ql-view--complete-buffers-files'"
|
||||||
|
|
@ -2174,7 +2174,7 @@ with keyword arg NOW in PLIST."
|
||||||
(spy-on 'completing-read-multiple :and-return-value "org-agenda-files")
|
(spy-on 'completing-read-multiple :and-return-value "org-agenda-files")
|
||||||
(expect (org-ql-view--complete-buffers-files) :to-equal temp-filenames)
|
(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 'org-ql-view--contract-buffers-files :to-have-been-called-with temp-filenames)
|
||||||
;; Also testing if the initial values are set correctly
|
;; Also testing if the initial values are set correctly.
|
||||||
(expect 'completing-read-multiple :to-have-been-called-with "Buffers/Files: "
|
(expect 'completing-read-multiple :to-have-been-called-with "Buffers/Files: "
|
||||||
(list 'buffer 'org-agenda-files 'org-directory 'all)
|
(list 'buffer 'org-agenda-files 'org-directory 'all)
|
||||||
nil nil "org-agenda-files")))
|
nil nil "org-agenda-files")))
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue