From 5837de87ff051f1c135111369a091590b500b314 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 25 Nov 2014 00:11:45 +0000 Subject: Move wrap-in-text-property/overlay to sx --- sx.el | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 061c85d..ef6bea7 100644 --- a/sx.el +++ b/sx.el @@ -129,7 +129,40 @@ 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)) + + +;;; 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 -- cgit v1.2.3 From fb1058921f841f92e99dce56206b79b22684b9a6 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 25 Nov 2014 00:22:57 +0000 Subject: Refactor visit from question-mode to sx --- sx-question-mode.el | 18 ++---------------- sx.el | 14 ++++++++++++++ 2 files changed, 16 insertions(+), 16 deletions(-) (limited to 'sx.el') diff --git a/sx-question-mode.el b/sx-question-mode.el index abea6bf..59313d1 100644 --- a/sx-question-mode.el +++ b/sx-question-mode.el @@ -217,8 +217,7 @@ QUESTION must be a data structure returned by `json-read'." (defun sx-question-mode--print-section (data) "Print a section corresponding to DATA. DATA can represent a question or an answer." - ;; This makes `data' accessible through - ;; `(get-text-property (point) 'sx--data-here)' + ;; This makes `data' accessible through `sx--data-here'. (sx-assoc-let data (sx--wrap-in-text-property (list 'sx--data-here data) @@ -542,7 +541,7 @@ Letters do not insert themselves; instead, they are commands. `(("n" sx-question-mode-next-section) ("p" sx-question-mode-previous-section) ("g" sx-question-mode-refresh) - ("v" sx-question-mode-visit) + ("v" sx-visit) ("q" quit-window) (" " scroll-up-command) (,(kbd "S-SPC") scroll-down-command) @@ -553,19 +552,6 @@ Letters do not insert themselves; instead, they are commands. (,(kbd "") backward-button) ([return] push-button))) -(defun sx-question-mode-visit () - "Visit the currently displayed question." - (interactive) - (sx-question-mode--ensure-mode) - (sx-assoc-let - ;; This allows us to visit the thing-at-point. Which could be a - ;; question or an answer. We use `append', so that if one - ;; doesn't have a `link' item we can fallback to - ;; `sx-question-mode--data'. - (append (get-text-property (point) 'sx--data-here) - sx-question-mode--data) - (browse-url .link))) - (defun sx-question-mode-refresh () "Refresh currently displayed question. Queries the API for any changes to the question or its answers or diff --git a/sx.el b/sx.el index ef6bea7..cae5b29 100644 --- a/sx.el +++ b/sx.el @@ -161,6 +161,20 @@ Return the result of BODY." (add-text-properties p (point) ,properties) result)) + +;;; Using data in buffer +(defun sx--data-here () + "Get the text property `sx--data-here'." + (get-text-property (point) 'sx--data-here)) + +(defun sx-visit () + "Visit in a web browser the object under point. +Object can be a question, answer, or comment." + (interactive) + (sx-assoc-let (sx--data-here) + (when (stringp .link) + (browse-url .link)))) + ;;; Assoc-let (defun sx--deep-dot-search (data) -- cgit v1.2.3 From b9988e7ae3df4503d30da36f749b8537eaa6465b Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 25 Nov 2014 00:29:35 +0000 Subject: Refactor visit from question-list to sx --- sx-question-list.el | 12 +----------- sx.el | 16 ++++++++++++---- 2 files changed, 13 insertions(+), 15 deletions(-) (limited to 'sx.el') diff --git a/sx-question-list.el b/sx-question-list.el index 4d80305..ddc04c7 100644 --- a/sx-question-list.el +++ b/sx-question-list.el @@ -287,7 +287,7 @@ into consideration. ("K" sx-question-list-previous-far) ("g" sx-question-list-refresh) (":" sx-question-list-switch-site) - ("v" sx-question-list-visit) + ("v" sx-visit) ("h" sx-question-list-hide) ("m" sx-question-list-mark-read) ([?\r] sx-question-list-display-question))) @@ -383,16 +383,6 @@ a new list before redisplaying." (cl-remove-if #'sx-question--hidden-p question-list)))) (when redisplay (tabulated-list-print 'remember))) -(defun sx-question-list-visit (&optional data) - "Visits question under point (or from DATA) using `browse-url'." - (interactive) - (unless data (setq data (tabulated-list-get-id))) - (unless data (error "No question here!")) - (sx-assoc-let data - (browse-url .link)) - (sx-question--mark-read data) - (sx-question-list-refresh 'redisplay 'no-update)) - (defcustom sx-question-list-ago-string " ago" "String appended to descriptions of the time since something happened. Used in the questions list to indicate a question was updated diff --git a/sx.el b/sx.el index cae5b29..018b1ec 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.") @@ -165,15 +166,22 @@ Return the result of BODY." ;;; Using data in buffer (defun sx--data-here () "Get the text property `sx--data-here'." - (get-text-property (point) '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 () "Visit in a web browser the object under point. Object can be a question, answer, or comment." (interactive) - (sx-assoc-let (sx--data-here) - (when (stringp .link) - (browse-url .link)))) + (let ((data (sx--data-here))) + (sx-assoc-let data + (when (stringp .link) + (browse-url .link)) + (when .title + (sx-question--mark-read data) + (when (derived-mode-p 'sx-question-list-mode) + (sx-question-list-refresh 'redisplay 'no-update)))))) ;;; Assoc-let -- cgit v1.2.3 From 8a46c6f7d6c862eaac43cc3b9d70c5c25d575f2c Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 25 Nov 2014 00:44:49 +0000 Subject: visit takes an argument --- sx.el | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) (limited to 'sx.el') diff --git a/sx.el b/sx.el index 018b1ec..0bab861 100644 --- a/sx.el +++ b/sx.el @@ -170,18 +170,19 @@ Return the result of BODY." (and (derived-mode-p 'sx-question-list-mode) (tabulated-list-get-id)))) -(defun sx-visit () - "Visit in a web browser the object under point. -Object can be a question, answer, or comment." - (interactive) - (let ((data (sx--data-here))) - (sx-assoc-let data - (when (stringp .link) - (browse-url .link)) - (when .title - (sx-question--mark-read data) - (when (derived-mode-p 'sx-question-list-mode) - (sx-question-list-refresh 'redisplay 'no-update)))))) +(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 -- cgit v1.2.3