diff options
author | Artur Malabarba <bruce.connor.am@gmail.com> | 2015-04-19 13:46:21 +0100 |
---|---|---|
committer | Artur Malabarba <bruce.connor.am@gmail.com> | 2015-04-19 13:46:31 +0100 |
commit | 706530f8f96dec5725a46774d85fe98c6b54912a (patch) | |
tree | 829c2d3ffeb8a992598d45e1f3e5dc94f29056af | |
parent | 48887789f077b0a9d810f6c9dc9694899d873514 (diff) |
Add Accept button to answers. Fixes #276
-rw-r--r-- | sx-button.el | 8 | ||||
-rw-r--r-- | sx-interaction.el | 15 | ||||
-rw-r--r-- | sx-question-print.el | 12 |
3 files changed, 34 insertions, 1 deletions
diff --git a/sx-button.el b/sx-button.el index 194c292..05ebadb 100644 --- a/sx-button.el +++ b/sx-button.el @@ -185,6 +185,14 @@ usually part of a code-block." 'action #'sx-comment :supertype 'sx-button) +(define-button-type 'sx-button-accept + 'help-echo (concat "mouse-1, RET" + (propertize ": accept answer" + 'face 'minibuffer-prompt)) + 'face 'sx-custom-button + 'action #'sx-accept + :supertype 'sx-button) + (define-button-type 'sx-button-answer 'help-echo (concat "mouse-1, RET" (propertize ": write an answer" diff --git a/sx-interaction.el b/sx-interaction.el index 63b7dff..ec11710 100644 --- a/sx-interaction.el +++ b/sx-interaction.el @@ -80,6 +80,13 @@ thrown unless NOERROR is non-nil." (and (null noerror) (error "No %s found here" (or type "data"))))) +(defun sx--marker-to-data (marker &rest rest) + "Get the data at MARKER. +REST is passed to `sx--data-here'." + (save-excursion + (goto-char marker) + (apply #'sx--data-here rest))) + (defun sx--error-if-unread (data) "Throw a user-error if DATA is an unread question. If it's not a question, or if it is read, return DATA." @@ -293,6 +300,10 @@ Interactively, it is guessed from context at point. With the UNDO prefix argument, unaccept the question instead." (interactive (list (sx--data-here 'answer) current-prefix-arg)) + (sx-ensure-authentication) + ;; When clicking the "Accept" button, first arg is a marker. + (when (markerp data) + (setq data (sx--marker-to-data data 'answer))) (sx-method-post-from-data data (if undo 'accept/undo 'accept) :callback (sx--copy-update-callback data))) @@ -324,6 +335,7 @@ DATA can be a question, answer, or comment. TYPE can be Besides posting to the api, DATA is also altered to reflect the changes." + (sx-ensure-authentication) (sx-method-post-from-data data (concat type (unless status "/undo")) :callback (sx--copy-update-callback data))) @@ -337,6 +349,7 @@ guessed from context at point. With UNDO prefix argument, undelete instead." (interactive (list (sx--error-if-unread (sx--data-here)) current-prefix-arg)) + (sx-ensure-authentication) (when (y-or-n-p (format "DELETE this %s? " (let-alist data (cond (.comment_id "comment") @@ -362,7 +375,7 @@ TEXT is a string. Interactively, it is read from the minibufer." (sx-ensure-authentication) ;; When clicking the "Add a Comment" button, first arg is a marker. (when (markerp data) - (setq data (sx--data-here)) + (setq data (sx--marker-to-data data)) (setq text 'query)) (sx-assoc-let data ;; Get the comment text diff --git a/sx-question-print.el b/sx-question-print.el index 3dcb4ae..f99b4be 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -28,6 +28,8 @@ (require 'sx-babel) (require 'sx-user) +(defvar sx-question-mode--data) + (defgroup sx-question-mode nil "Customization group for sx-question-mode." :prefix "sx-question-mode-" @@ -275,6 +277,14 @@ DETAILS, when given is an alist further describing the close." (overlay-put ov 'face 'sx-question-mode-closed-reason) (push ov sx--overlays))))) +(defun sx-question-mode--maybe-print-accept-button () + "Print accept button if you own this question." + (when (sx-assoc-let sx-question-mode--data + (= .owner.user_id + (cdr (assq 'user_id (sx-network-user .site_par))))) + (insert " ") + (insert-text-button "Accept" :type 'sx-button-accept))) + (defun sx-question-mode--print-section (data) "Print a section corresponding to DATA. DATA can represent a question or an answer." @@ -294,6 +304,8 @@ DATA can represent a question or an answer." 'face (if .is_accepted 'sx-question-mode-accepted 'sx-question-mode-title) :type 'sx-question-mode-title) + (when (not (or .title .is_accepted)) + (sx-question-mode--maybe-print-accept-button)) ;; Sections can be hidden with overlays (sx--wrap-in-overlay |