aboutsummaryrefslogtreecommitdiff
path: root/sx.el
diff options
context:
space:
mode:
Diffstat (limited to 'sx.el')
-rw-r--r--sx.el27
1 files changed, 22 insertions, 5 deletions
diff --git a/sx.el b/sx.el
index 0bab861..837d43b 100644
--- a/sx.el
+++ b/sx.el
@@ -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