From d26df2730db01a4904ad71eb3590b5d90c015767 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Sun, 4 Jan 2015 23:16:44 -0200 Subject: Implement sx-tag-list--get --- sx-tag.el | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) (limited to 'sx-tag.el') diff --git a/sx-tag.el b/sx-tag.el index 5e75890..0f726fd 100644 --- a/sx-tag.el +++ b/sx-tag.el @@ -62,6 +62,23 @@ Returns a list." (mapcar (lambda (x) (cdr (assoc 'name x))) (sx-tag--get-some-tags-containing site string))) + +;;; Getting tags from our data branch. Without the API. +;;;; @TODO: Once the cache is finished, this can probably be made into +;;;; a cache variasble with 1 day expiration time. +(defvar sx-tag-list-alist nil + "Alist where the tag list for each site is stored. +Elements are of the type (SITE . TAG-LIST).") + +(defun sx-tag-list--get (site) + "Retrieve all tags from SITE in a single request. +This does not access the API. Instead, it uses +`sx-request-get-data', which accesses SX's tag cache." + (or (cdr (assoc site sx-tag-list-alist)) + (let ((list (sx-request-get-data (concat "tags/" site)))) + (push (cons site list) sx-tag-list-alist) + list))) + ;;; Check tag validity (defun sx-tag--invalid-name-p (site tags) -- cgit v1.2.3 From 065ef960b3fcf80ea68dbcbbcca8c276a2d09b07 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 5 Jan 2015 00:53:01 -0200 Subject: Move sx--multiple-read to sx-tag-multiple-read --- sx-search.el | 13 ++++--------- sx-tag.el | 33 +++++++++++++++++++++++++++++++++ sx.el | 18 ------------------ 3 files changed, 37 insertions(+), 27 deletions(-) (limited to 'sx-tag.el') diff --git a/sx-search.el b/sx-search.el index d47905e..8614f49 100644 --- a/sx-search.el +++ b/sx-search.el @@ -32,13 +32,11 @@ (require 'sx) (require 'sx-question-list) +(require 'sx-tag) (defvar sx-search--query-history nil "Query history for interactive prompts.") -(defvar sx-search--tag-history nil - "Tags history for interactive prompts.") - ;;; Basic function (defun sx-search-get-questions (site page query &optional tags excluded-tags keywords) @@ -84,15 +82,12 @@ prefix argument, the user is asked for everything." (when (string= query "") (setq query nil)) (when current-prefix-arg - (setq tags (sx--multiple-read - (format "Tags (%s)" - (if query "optional" "mandatory")) - 'sx-search--tag-history)) + (setq tags (sx-tag-multiple-read + site (format "Tags%s" (if query " (optional)" "")))) (when (and (not query) (string= "" tags)) (sx-user-error "Must supply either QUERY or TAGS")) (setq excluded-tags - (sx--multiple-read - "Excluded tags (optional)" 'sx-search--tag-history))) + (sx-tag-multiple-read site "Excluded tags (optional)"))) (list site query tags excluded-tags))) ;; Here starts the actual function diff --git a/sx-tag.el b/sx-tag.el index 0f726fd..41ed9eb 100644 --- a/sx-tag.el +++ b/sx-tag.el @@ -99,6 +99,39 @@ Return the list of invalid tags in TAGS." :site site)))) (cl-remove-if (lambda (x) (member x result)) tags))) + +;;; Prompt the user for tags. +(defvar sx-tag-history nil + "Tags history for interactive prompts.") + +;;; @TODO: Make it so that hitting BACKSPACE with an empty input +;;; deletes a previously submitted tag. +(defun sx-tag-multiple-read (site prompt &optional initial-value) + "Interactively read a list of tags for SITE. +Call `sx-completing-read' multiple times, until input is empty. +Return a list of tags given by the user. + +PROMPT is a string displayed to the user and should not end with +a space nor a colon. INITIAL-VALUE is a list of already-selected +tags." + (let ((completion-list (sx-tag-list--get site)) + (list initial-value) + input) + (while (not (string= + "" + (setq input (sx-completing-read + (concat prompt " [" + (mapconcat #'identity list ",") + "]: ") + completion-list + (lambda (x) (not (member x list))) + nil + 'require-match + nil + 'sx-tag-history)))) + (push input list)) + list)) + (provide 'sx-tag) ;;; sx-tag.el ends here diff --git a/sx.el b/sx.el index 3271755..9924308 100644 --- a/sx.el +++ b/sx.el @@ -176,24 +176,6 @@ All ARGS are passed to `completing-read' or `ido-completing-read'." (apply (if ido-mode #'ido-completing-read #'completing-read) args)) -(defun sx--multiple-read (prompt hist-var) - "Interactively query the user for a list of strings. -Call `read-string' multiple times, until the input is empty. - -PROMPT is a string displayed to the user and should not end with -a space nor a colon. HIST-VAR is a quoted symbol, indicating a -list in which to store input history." - (let (list input) - (while (not (string= - "" - (setq input (read-string - (concat prompt " [" - (mapconcat #'identity list ",") - "]: ") - "" hist-var)))) - (push input list)) - list)) - (defmacro sx-sorted-insert-skip-first (newelt list &optional predicate) "Inserted NEWELT into LIST sorted by PREDICATE. This is designed for the (site id id ...) lists. So the first car -- cgit v1.2.3 From 34ea6829153a1b5b45104c0650a9d0a148491aef Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 5 Jan 2015 11:16:50 -0200 Subject: Fix sx-tag-completing-read --- sx-tag.el | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) (limited to 'sx-tag.el') diff --git a/sx-tag.el b/sx-tag.el index 41ed9eb..b2ad375 100644 --- a/sx-tag.el +++ b/sx-tag.el @@ -108,29 +108,30 @@ Return the list of invalid tags in TAGS." ;;; deletes a previously submitted tag. (defun sx-tag-multiple-read (site prompt &optional initial-value) "Interactively read a list of tags for SITE. -Call `sx-completing-read' multiple times, until input is empty. +Call `sx-completing-read' multiple times, until input is empty, +with completion options given by the tag list of SITE. Return a list of tags given by the user. PROMPT is a string displayed to the user and should not end with a space nor a colon. INITIAL-VALUE is a list of already-selected tags." (let ((completion-list (sx-tag-list--get site)) - (list initial-value) + (list (reverse initial-value)) + (empty-string + (propertize "--\x000-some-string-representing-empty-\x000--" + 'display "DONE")) input) (while (not (string= - "" + empty-string (setq input (sx-completing-read (concat prompt " [" - (mapconcat #'identity list ",") + (mapconcat #'identity (reverse list) ",") "]: ") completion-list - (lambda (x) (not (member x list))) - nil - 'require-match - nil - 'sx-tag-history)))) + nil 'require-match nil 'sx-tag-history + empty-string)))) (push input list)) - list)) + (reverse list))) (provide 'sx-tag) ;;; sx-tag.el ends here -- cgit v1.2.3