diff options
author | Artur Malabarba <bruce.connor.am@gmail.com> | 2014-11-25 01:39:36 +0000 |
---|---|---|
committer | Artur Malabarba <bruce.connor.am@gmail.com> | 2014-11-25 01:41:16 +0000 |
commit | 5dbe39b002418f5a663984f341dd7a240bca90c2 (patch) | |
tree | f0805e5393e55f4b7d49ea7aa3b5bee1e528e810 | |
parent | 8a46c6f7d6c862eaac43cc3b9d70c5c25d575f2c (diff) |
.site is now a special symbol in sx-assoc-let
sx-question.el no longer manually inserts the `site` property on fetched
questions. This was insufficient because we also need this property on
other objects.
-rw-r--r-- | sx-question.el | 43 | ||||
-rw-r--r-- | sx.el | 27 |
2 files changed, 41 insertions, 29 deletions
diff --git a/sx-question.el b/sx-question.el index 06e8648..cca789e 100644 --- a/sx-question.el +++ b/sx-question.el @@ -56,13 +56,11 @@ properties returned by the API with an added (site SITE) property. `sx-method-call' is used with `sx-question-browse-filter'." - (mapcar - (lambda (question) (cons (cons 'site site) question)) - (sx-method-call 'questions - :keywords `((page . ,page)) - :site site - :auth t - :filter sx-question-browse-filter))) + (sx-method-call 'questions + :keywords `((page . ,page)) + :site site + :auth t + :filter sx-question-browse-filter)) (defun sx-question-get-question (site question-id) "Query SITE for a QUESTION-ID and return it. @@ -159,23 +157,20 @@ If no cache exists for it, initialize one with SITE." (defun sx-question--mark-hidden (question) "Mark QUESTION as being hidden." - (sx-assoc-let question - (sx-question--ensure-hidden-list .site) - (let ((site-cell (assoc .site sx-question--user-hidden-list)) - cell) - ;; If question already hidden, do nothing. - (unless (memq .question_id site-cell) - ;; First question from this site. - (if (null site-cell) - (push (list .site .question_id) sx-question--user-hidden-list) - ;; Question wasn't present. - ;; Add it in, but make sure it's sorted (just in case we need - ;; it later). - (sx-sorted-insert-skip-first .question_id site-cell >)) - ;; This causes a small lag on `j' and `k' as the list gets large. - ;; Should we do this on a timer? - ;; Save the results. - (sx-cache-set 'hidden-questions sx-question--user-hidden-list))))) + (let ((site-cell (assoc .site sx-question--user-hidden-list)) + cell) + ;; If question already hidden, do nothing. + (unless (memq .question_id site-cell) + ;; First question from this site. + (push (list .site .question_id) sx-question--user-hidden-list) + ;; Question wasn't present. + ;; Add it in, but make sure it's sorted (just in case we need + ;; it later). + (sx-sorted-insert-skip-first .question_id site-cell >) + ;; This causes a small lag on `j' and `k' as the list gets large. + ;; Should we do this on a timer? + ;; Save the results. + (sx-cache-set 'hidden-questions sx-question--user-hidden-list)))) ;;;; Other data @@ -186,6 +186,19 @@ If DATA is a question, also mark it as read." ;;; Assoc-let +(defun sx--site (data) + "Get the site in which DATA belongs. +DATA can be a question, answer, comment, or user (or any object +with a `link' property). +DATA can also be the link itself." + (let ((link (if (stringp data) data + (cdr (assoc 'link data))))) + (unless (stringp link) + (error "Data has no link property")) + (replace-regexp-in-string + "^https?://\\(?:\\(?1:[^/]+\\)\\.stackexchange\\|\\(?2:[^/]+\\)\\)\\.[^.]+/.*$" + "\\1\\2" link))) + (defun sx--deep-dot-search (data) "Find symbols somewhere inside DATA which start with a `.'. Returns a list where each element is a cons cell. The car is the @@ -206,6 +219,8 @@ symbol, the cdr is the symbol without the `.'." "Use dotted symbols let-bound to their values in ALIST and execute BODY. Dotted symbol is any symbol starting with a `.'. Only those present in BODY are letbound, which leads to optimal performance. +The .site symbol is special, it is derived from the .link symbol +using `sx--site'. For instance, the following code @@ -217,11 +232,13 @@ is equivalent to (let ((.title (cdr (assoc 'title alist))) (.body (cdr (assoc 'body alist)))) (list .title .body))" - (declare (indent 1) - (debug t)) - (let ((symbol-alist (sx--deep-dot-search body))) - `(let ,(mapcar (lambda (x) `(,(car x) (cdr (assoc ',(cdr x) ,alist)))) - (delete-dups symbol-alist)) + (declare (indent 1) (debug t)) + (let* ((symbol-alist (sx--deep-dot-search body)) + (has-site (assoc '.site symbol-alist))) + `(let ,(append + (when has-site `((.site (sx--site (cdr (assoc 'link ,alist)))))) + (mapcar (lambda (x) `(,(car x) (cdr (assoc ',(cdr x) ,alist)))) + (remove '(.site . site) (delete-dups symbol-alist)))) ,@body))) (defcustom sx-init-hook nil |