aboutsummaryrefslogtreecommitdiff
path: root/sx-question-mode.el
diff options
context:
space:
mode:
authorArtur Malabarba <bruce.connor.am@gmail.com>2014-11-26 00:55:06 +0000
committerArtur Malabarba <bruce.connor.am@gmail.com>2014-11-26 00:55:06 +0000
commit2e8d2015639cc09e31b9ebf35b1d1bddbd0fdc88 (patch)
tree8624fd62e8c75000cb42efedc7ed3a9c8d36f163 /sx-question-mode.el
parentc38dd4d9d23cbf1299e10b8a441855ba22ff567f (diff)
parent3a13169bb9b02a4f5935d992e603f53115ddf3dd (diff)
Merge branch 'voting' into commenting
Diffstat (limited to 'sx-question-mode.el')
-rw-r--r--sx-question-mode.el69
1 files changed, 51 insertions, 18 deletions
diff --git a/sx-question-mode.el b/sx-question-mode.el
index dda65ab..06413f2 100644
--- a/sx-question-mode.el
+++ b/sx-question-mode.el
@@ -54,13 +54,17 @@
If WINDOW is nil, use selected one.
Returns the question buffer."
+ (with-current-buffer
+ (sx-question-mode--display-buffer window)
+ (sx-question-mode--erase-and-print-question data)))
+
+(defun sx-question-mode--erase-and-print-question (data)
+ "Erase contents of buffer and print question given by DATA."
(let ((inhibit-read-only t))
- (with-current-buffer
- (sx-question-mode--display-buffer window)
- (erase-buffer)
- (sx-question-mode)
- (sx-question-mode--print-question data)
- (current-buffer))))
+ (erase-buffer)
+ (sx-question-mode)
+ (sx-question-mode--print-question data)
+ (current-buffer)))
(defun sx-question-mode--display-buffer (window)
"Display and return the buffer used for displaying a question.
@@ -134,11 +138,32 @@ If WINDOW is given, use that to display the buffer."
"Face used for author names in the question buffer."
:group 'sx-question-mode-faces)
+(defface sx-question-mode-score
+ '((t))
+ "Face used for the score in the question buffer."
+ :group 'sx-question-mode-faces)
+
+(defface sx-question-mode-score-downvoted
+ '((t :inherit (font-lock-warning-face sx-question-mode-score)))
+ "Face used for downvoted score in the question buffer."
+ :group 'sx-question-mode-faces)
+
+(defface sx-question-mode-score-upvoted
+ '((t :weight bold
+ :inherit (font-lock-function-name-face sx-question-mode-score)))
+ "Face used for downvoted score in the question buffer."
+ :group 'sx-question-mode-faces)
+
(defcustom sx-question-mode-header-tags "\nTags: "
"String used before the question tags at the header."
:type 'string
:group 'sx-question-mode)
+(defcustom sx-question-mode-header-score "\nScore: "
+ "String used before the question score at the header."
+ :type 'string
+ :group 'sx-question-mode)
+
(defface sx-question-mode-content-face
'((((background dark)) :background "#090909")
(((background light)) :background "#f4f4f4"))
@@ -195,10 +220,9 @@ QUESTION must be a data structure returned by `json-read'."
;; Print everything
(sx-question-mode--print-section question)
(sx-assoc-let question
- (mapc #'sx-question-mode--print-section .answers))
+ (mapc #'sx-question-mode--print-section .answers))
(goto-char (point-min))
- (with-selected-window sx-question-mode--window
- (sx-question-mode-next-section)))
+ (sx-question-mode-next-section))
(defvar sx-question-mode--section-help-echo
(format
@@ -249,6 +273,13 @@ DATA can represent a question or an answer."
(sx-time-since .last_edit_date)
(sx-question-mode--propertize-display-name .last_editor))))
'sx-question-mode-date)
+ (sx-question-mode--insert-header
+ sx-question-mode-header-score
+ (format "%s" .score)
+ (cond
+ ((eq .upvoted t) 'sx-question-mode-score-upvoted)
+ ((eq .downvoted t) 'sx-question-mode-score-downvoted)
+ (t 'sx-question-mode-score)))
(when .title
;; Tags
(sx-question-mode--insert-header
@@ -332,7 +363,6 @@ where `value' is given `face' as its face.
(defun sx-question-mode--fill-and-fontify (text)
"Return TEXT filled according to `markdown-mode'."
(with-temp-buffer
- (erase-buffer)
(insert text)
(markdown-mode)
(font-lock-mode -1)
@@ -344,8 +374,8 @@ where `value' is given `face' as its face.
(font-lock-add-keywords ;; Highlight usernames.
nil
`((,(rx (or blank line-start)
- (group-n 1 (and "@" (1+ (or (syntax word) (syntax symbol)))))
- symbol-end)
+ (group-n 1 (and "@" (1+ (or (syntax word) (syntax symbol)))))
+ symbol-end)
1 font-lock-builtin-face)))
;; Everything.
(font-lock-fontify-region (point-min) (point-max))
@@ -564,12 +594,15 @@ With non-nil prefix argument NO-UPDATE, just redisplay, don't
query the api."
(interactive "P")
(sx-question-mode--ensure-mode)
- (sx-question-mode--display
- (if no-update
- sx-question-mode--data
- (sx-assoc-let sx-question-mode--data
- (sx-question-get-question .site .question_id)))
- (selected-window)))
+ (let ((point (point)))
+ (sx-question-mode--erase-and-print-question
+ (if no-update
+ sx-question-mode--data
+ (sx-assoc-let sx-question-mode--data
+ (sx-question-get-question .site .question_id))))
+ (goto-char point)
+ (when (get-buffer-window (current-buffer))
+ (recenter))))
(defun sx-question-mode--ensure-mode ()
"Ensures we are in question mode, erroring otherwise."