aboutsummaryrefslogtreecommitdiff
path: root/sx.el
diff options
context:
space:
mode:
authorSean Allred <code@seanallred.com>2014-11-25 21:24:00 -0500
committerSean Allred <code@seanallred.com>2014-11-25 21:24:00 -0500
commit49a68e2c95dd72871a232e2601088c375a6b154f (patch)
treed29eabd7ba9102099e0d852c16843b0ebf20c107 /sx.el
parent3a16159966ca345eb3ac81e9c61e1d18cd018e29 (diff)
parent8a46c6f7d6c862eaac43cc3b9d70c5c25d575f2c (diff)
Merge pull request #91 from vermiculus/move-some-macros-to-sx
Move some macros to sx
Diffstat (limited to 'sx.el')
-rw-r--r--sx.el58
1 files changed, 57 insertions, 1 deletions
diff --git a/sx.el b/sx.el
index 061c85d..0bab861 100644
--- a/sx.el
+++ b/sx.el
@@ -27,6 +27,7 @@
;; StackMode.
;;; Code:
+(require 'tabulated-list)
(defconst sx-version "0.1" "Version of the `sx' package.")
@@ -129,7 +130,62 @@ would yield
data))))
-;;; Interpreting request data
+;;; Printing request data
+(defvar sx--overlays nil
+ "Overlays created by sx on this buffer.")
+(make-variable-buffer-local 'sx--overlays)
+
+(defmacro sx--wrap-in-overlay (properties &rest body)
+ "Start a scope with overlay PROPERTIES and execute BODY.
+Overlay is pushed on the buffer-local variable `sx--overlays' and
+given PROPERTIES.
+
+Return the result of BODY."
+ (declare (indent 1)
+ (debug t))
+ `(let ((p (point-marker))
+ (result (progn ,@body)))
+ (let ((ov (make-overlay p (point)))
+ (props ,properties))
+ (while props
+ (overlay-put ov (pop props) (pop props)))
+ (push ov sx--overlays))
+ result))
+
+(defmacro sx--wrap-in-text-property (properties &rest body)
+ "Start a scope with PROPERTIES and execute BODY.
+Return the result of BODY."
+ (declare (indent 1)
+ (debug t))
+ `(let ((p (point-marker))
+ (result (progn ,@body)))
+ (add-text-properties p (point) ,properties)
+ result))
+
+
+;;; Using data in buffer
+(defun sx--data-here ()
+ "Get the text property `sx--data-here'."
+ (or (get-text-property (point) 'sx--data-here)
+ (and (derived-mode-p 'sx-question-list-mode)
+ (tabulated-list-get-id))))
+
+(defun sx-visit (data)
+ "Visit DATA in a web browser.
+DATA can be a question, answer, or comment. Interactively, it is
+derived from point position.
+If DATA is a question, also mark it as read."
+ (interactive (list (sx--data-here)))
+ (sx-assoc-let data
+ (when (stringp .link)
+ (browse-url .link))
+ (when (and .title (fboundp 'sx-question--mark-read))
+ (sx-question--mark-read data)
+ (when ((derived-mode-p 'sx-question-list-mode))
+ (sx-question-list-refresh 'redisplay 'no-update)))))
+
+
+;;; Assoc-let
(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