aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sx-interaction.el43
-rw-r--r--sx-question-mode.el1
2 files changed, 40 insertions, 4 deletions
diff --git a/sx-interaction.el b/sx-interaction.el
index 305e61c..3f8242c 100644
--- a/sx-interaction.el
+++ b/sx-interaction.el
@@ -189,13 +189,48 @@ OBJECT can be a question or an answer."
(setcdr
com-cell
(apply #'vector
- (append
- (cl-map 'list #'identity
- (cdr com-cell))
- (list comment)))))
+ (append
+ (cl-map 'list #'identity
+ (cdr com-cell))
+ (list comment)))))
;; No previous comments, add it manually.
(setcdr object (cons (car object) (cdr object)))
(setcar object `(comments . [,comment])))))
+
+;;; Answering
+(defun sx-answer (data)
+ "Start composing an answer for question given by DATA.
+DATA is a question alist. Interactively, it is guessed from
+context at point.
+
+TEXT is a string. Interactively, it is read from the minibufer."
+ ;; Answering doesn't really make sense from anywhere other than
+ ;; inside a question. So we don't need `sx--data-here' here.
+ (interactive (list sx-question-mode--data))
+ ;; When clicking the "Write an Answer" button, first arg is a marker.
+ (when (markerp data) (setq data (sx--data-here)))
+ (let ((buffer (current-buffer)))
+ (sx-assoc-let data
+ (pop-to-buffer
+ (sx-compose--create
+ .site .question_id nil
+ ;; After change functions
+ (lambda (_ res)
+ (sx--add-answer-to-question-object res sx-question-mode--data)
+ (when (buffer-live-p buffer)
+ (with-current-buffer buffer
+ (sx-question-mode-refresh 'no-update)))))))))
+
+(defun sx--add-answer-to-question-object (answer question)
+ "Add alist ANSWER to alist QUESTION in the right place."
+ (let ((cell (assoc 'answers question)))
+ (if cell
+ (setcdr cell (apply #'vector
+ (append (cdr cell) (list answer))))
+ ;; No previous comments, add it manually.
+ (setcdr question (cons (car question) (cdr question)))
+ (setcar question `(answers . [,answer])))))
+
(provide 'sx-interaction)
;;; sx-interaction.el ends here
diff --git a/sx-question-mode.el b/sx-question-mode.el
index c44519c..24b2cfb 100644
--- a/sx-question-mode.el
+++ b/sx-question-mode.el
@@ -188,6 +188,7 @@ Letters do not insert themselves; instead, they are commands.
("d" sx-toggle-downvote)
("q" quit-window)
(" " scroll-up-command)
+ ("a" sx-answer)
(,(kbd "S-SPC") scroll-down-command)
([backspace] scroll-down-command)
([tab] forward-button)