From a8e752a0454e6ce20162a741dd967ed64380225b Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 3 Nov 2014 00:45:23 +0000 Subject: Question-list mode --- stack-question.el | 34 ++++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) (limited to 'stack-question.el') diff --git a/stack-question.el b/stack-question.el index 3902a39..84f2367 100644 --- a/stack-question.el +++ b/stack-question.el @@ -22,6 +22,7 @@ ;; + ;;; Code: (require 'stack-core) @@ -39,5 +40,38 @@ (page . ,page)) stack-question-browse-filter)) +(defun stack-question--display (data window) + "Display question given by DATA on WINDOW. +If WINDOW is nil, use selected one." + (with-current-buffer + (stack-question--display-buffer window) + (erase-buffer) + (insert + (org-element-interpret-data + (stack-lto--question data))))) + +(defvar stack-question--window nil + "Window where the content of questions is displayed.") + +(defvar stack-question--buffer "*stack-question*" + "Buffer being used to display questions.") + +(defun stack-question--display-buffer (window) + "Display and return the buffer used for displaying a question. +Create the buffer if necessary. +If WINDOW is given, use that to display the buffer." + (unless (buffer-live-p stack-question--buffer) + (setq stack-question--buffer + (generate-new-buffer stack-question--buffer))) + (cond + ;; Window was given, use it. + ((window-live-p window) + (set-window-buffer window stack-question--buffer)) + ;; No window, but the buffer is already being displayed somewhere. + ((get-buffer-window stack-question--buffer 'visible)) + ;; Neither, so we create the window. + (t (switch-to-buffer stack-question--buffer))) + stack-question--buffer) + (provide 'stack-question) ;;; stack-question.el ends here -- cgit v1.2.3 From 41cefa9af40d298d3ad31b8376c1d20c9bb89d48 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 3 Nov 2014 01:52:22 +0000 Subject: Implement a rudimentary question display. --- stack-question.el | 40 ++++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) (limited to 'stack-question.el') diff --git a/stack-question.el b/stack-question.el index 10ff1fc..dedb906 100644 --- a/stack-question.el +++ b/stack-question.el @@ -43,29 +43,45 @@ (page . ,page)) stack-question-browse-filter)) -(defun stack-question--display (data window) - "Display question given by DATA on WINDOW. -If WINDOW is nil, use selected one." - (with-current-buffer - (stack-question--display-buffer window) - (erase-buffer) - (insert - (org-element-interpret-data - (stack-lto--question data))))) - + +;;; Displaying a question (defvar stack-question--window nil "Window where the content of questions is displayed.") -(defvar stack-question--buffer "*stack-question*" +(defvar stack-question--buffer nil "Buffer being used to display questions.") +(defcustom stack-question-use-html t + "If nil, markdown is used for the body." + :type 'boolean + :group 'stack-question) + +(defun stack-question--display (data window) + "Display question given by DATA on WINDOW. +If WINDOW is nil, use selected one." + (let ((stack-lto--body-src-block + (if stack-question-use-html nil + stack-lto--body-src-block)) + (inhibit-read-only t)) + (with-current-buffer + (stack-question--display-buffer window) + (erase-buffer) + (insert + (org-element-interpret-data + (stack-lto--question data))) + (org-mode) + (show-all) + (view-mode) + (current-buffer)))) + (defun stack-question--display-buffer (window) "Display and return the buffer used for displaying a question. Create the buffer if necessary. If WINDOW is given, use that to display the buffer." + ;; Create the buffer if necessary. (unless (buffer-live-p stack-question--buffer) (setq stack-question--buffer - (generate-new-buffer stack-question--buffer))) + (generate-new-buffer "*stack-question*"))) (cond ;; Window was given, use it. ((window-live-p window) -- cgit v1.2.3 From 1276180d11114b445193da1f3446b9a70dd4079f Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 3 Nov 2014 09:50:21 +0000 Subject: require 'stack-lto --- stack-question.el | 1 + 1 file changed, 1 insertion(+) (limited to 'stack-question.el') diff --git a/stack-question.el b/stack-question.el index dedb906..4c67e09 100644 --- a/stack-question.el +++ b/stack-question.el @@ -27,6 +27,7 @@ (require 'stack-core) (require 'stack-filter) +(require 'stack-lto) (defvar stack-question-browse-filter (stack-filter-compile -- cgit v1.2.3 From e298e214bfca97456e16b5f8bf6766e9130a4888 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 3 Nov 2014 13:34:48 +0000 Subject: Huge improvements to the question list --- stack-question-list.el | 60 ++++++++++++++++++++++++++++++++++++-------------- stack-question.el | 12 ++++++++++ 2 files changed, 56 insertions(+), 16 deletions(-) (limited to 'stack-question.el') diff --git a/stack-question-list.el b/stack-question-list.el index 584b2b9..dab5504 100644 --- a/stack-question-list.el +++ b/stack-question-list.el @@ -98,9 +98,10 @@ Letters do not insert themselves; instead, they are commands. (hl-line-mode 1) (stack-question-list--update-mode-line) (setq tabulated-list-format - `[("Vote" 4 nil) ("Answ" 4 nil) ("Date" 4 nil) ("Title" 0 nil)]) - (setq tabulated-list-padding 2) - (setq tabulated-list-sort-key (cons "Date" nil)) + [(" V" 3 t :right-align t) + (" A" 3 t :right-align t) + ("Title" 0 stack-question-list--date-sort)]) + (setq tabulated-list-padding 1) (add-hook 'tabulated-list-revert-hook #'stack-question-list-refresh nil t) (add-hook 'tabulated-list-revert-hook @@ -110,8 +111,10 @@ Letters do not insert themselves; instead, they are commands. (mapc (lambda (x) (define-key stack-question-list-mode-map (car x) (cadr x))) - '(("j" stack-question-list-next) - ("k" stack-question-list-previous) + '(("n" stack-question-list-next) + ("p" stack-question-list-previous) + ("j" stack-question-list-view-next) + ("k" stack-question-list-view-previous) ("g" stack-question-list-refresh) ([?\r] stack-question-list-display-question))) @@ -130,29 +133,54 @@ If REDISPLAY is non-nil, also call `tabulated-list-print'." (mapcar #'stack-question-list--print-info question-list))) (when redisplay (tabulated-list-print 'remember))) -;; (stack-question-list--print-info sample-question-unauthenticated) - (defun stack-question-list--print-info (data) "Convert `json-read' DATA into tabulated-list format." (cl-flet ((ca (x) (cdr (assoc x data)))) (list data - (vector (int-to-string (ca 'score)) - (int-to-string (ca 'answer_count)) - (int-to-string (ca 'last_activity_date)) - (ca 'title))))) - -(defun stack-question-list-previous (n) + (vector + (list (int-to-string (ca 'score)) 'face + (if (ca 'upvoted) 'stack-question-list-score-upvoted + 'stack-question-list-score)) + (list (int-to-string (ca 'answer_count)) 'face + (if (stack-question--accepted-answer data) + 'stack-question-list-answers-accepted + 'stack-question-list-answers)) + (concat + (propertize + (stack-core--decode-entities (ca 'title)) + 'face + (if (stack-question--read-p data) + 'stack-question-list-read-question + 'stack-question-list-unread-question)) + (propertize " " 'display "\n ") + (propertize (stack--time-since (ca 'last_activity_date)) + 'face 'stack-question-list-date) + (propertize (concat " [" (mapconcat #'identity (ca 'tags) "] [") "]") + 'face 'stack-question-list-tags) + (propertize " " 'display "\n")))))) + +(defun stack-question-list-view-previous (n) "Hide this question, move to previous one, display it." (interactive "p") - (stack-question-list-next (- n))) + (stack-question-list-view-next (- n))) -(defun stack-question-list-next (n) +(defun stack-question-list-view-next (n) "Hide this question, move to next one, display it." (interactive "p") - (next-line n) + (stack-question-list-next n) (stack-question-list-display-question)) +(defun stack-question-list-next (n) + "Move to the next entry." + (interactive "p") + (forward-line n)) + +(defun stack-question-list-previous (n) + "Move to the previous entry." + (interactive "p") + (stack-question-list-next (- n))) + (defun stack-question-list-display-question (&optional data focus) "Display question given by DATA. If called interactively (or with DATA being nil), display diff --git a/stack-question.el b/stack-question.el index 4c67e09..2b6cd1f 100644 --- a/stack-question.el +++ b/stack-question.el @@ -44,6 +44,18 @@ (page . ,page)) stack-question-browse-filter)) + +;;; Question Properties +(defun stack-question--read-p (question) + "Non-nil if QUESTION has been read since last updated." + ;; @TODO: + (cl-evenp (random))) + +(defun stack-question--accepted-answer (question) + "Return accepted answer in QUESTION, or nil if none." + ;; @TODO: + (cl-evenp (random))) + ;;; Displaying a question (defvar stack-question--window nil -- cgit v1.2.3 From 20cac24283fa689f9bea679b4675e2415123c026 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 3 Nov 2014 14:05:49 +0000 Subject: Comment out api-filter-request --- stack-question.el | 14 ++++++++------ 1 file changed, 8 insertions(+), 6 deletions(-) (limited to 'stack-question.el') diff --git a/stack-question.el b/stack-question.el index 2b6cd1f..66a4ea3 100644 --- a/stack-question.el +++ b/stack-question.el @@ -29,12 +29,14 @@ (require 'stack-filter) (require 'stack-lto) -(defvar stack-question-browse-filter - (stack-filter-compile - nil - '(user.profile_image shallow_user.profile_image))) - -(stack-filter-store 'question-browse stack-question-browse-filter) +;; I don't know why this is here, but it was causing an API request on require. +(defvar stack-question-browse-filter nil + ;; (stack-filter-compile + ;; nil + ;; '(user.profile_image shallow_user.profile_image)) + ) + +;; (stack-filter-store 'question-browse stack-question-browse-filter) (defun stack-question-get-questions (site &optional page) "Get the page PAGE of questions from SITE." -- cgit v1.2.3 From 33383d630ce43045cd94c2b515df7567063fc885 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 4 Nov 2014 09:35:28 +0000 Subject: Rename -date-sort to -date-more-recent-p --- stack-question-list.el | 9 +++++---- stack-question.el | 6 ++++++ 2 files changed, 11 insertions(+), 4 deletions(-) (limited to 'stack-question.el') diff --git a/stack-question-list.el b/stack-question-list.el index efacb7f..2e354d3 100644 --- a/stack-question-list.el +++ b/stack-question-list.el @@ -100,7 +100,7 @@ Letters do not insert themselves; instead, they are commands. (setq tabulated-list-format [(" V" 3 t :right-align t) (" A" 3 t :right-align t) - ("Title" 0 stack-question-list--date-sort)]) + ("Title" 0 stack-question-list--date-more-recent-p)]) (setq tabulated-list-padding 1) ;; Sorting by title actually sorts by date. It's what we want, but ;; it's not terribly intuitive. @@ -119,10 +119,11 @@ Letters do not insert themselves; instead, they are commands. :group 'stack-question-list) -(defun stack-question-list--date-sort (x y) +(defun stack-question-list--date-more-recent-p (x y) "Non-nil if tabulated-entry X is newer than Y." - (> (cdr (assoc stack-question-list-date-sort-method (car x))) - (cdr (assoc stack-question-list-date-sort-method (car y))))) + (stack-question-list--< + stack-question-list-date-sort-method + (car x) (car y) #'>)) (mapc (lambda (x) (define-key stack-question-list-mode-map diff --git a/stack-question.el b/stack-question.el index 66a4ea3..a7a74df 100644 --- a/stack-question.el +++ b/stack-question.el @@ -58,6 +58,12 @@ ;; @TODO: (cl-evenp (random))) +(defun stack-question--< (property x y &optional pred) + "Non-nil if PROPERTY attribute of question X is less than that of Y. +With optional argument predicate, use it instead of `<'." + (funcall (or pred #'<) + (cdr (assoc property x)) + (cdr (assoc property y)))) ;;; Displaying a question (defvar stack-question--window nil -- cgit v1.2.3 From 1cdbc41dc689edf99bd755d708fb5de72c296c36 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 4 Nov 2014 09:56:50 +0000 Subject: Make WINDOW optional in stack-question--display --- stack-question.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'stack-question.el') diff --git a/stack-question.el b/stack-question.el index a7a74df..9858391 100644 --- a/stack-question.el +++ b/stack-question.el @@ -77,7 +77,7 @@ With optional argument predicate, use it instead of `<'." :type 'boolean :group 'stack-question) -(defun stack-question--display (data window) +(defun stack-question--display (data &optional window) "Display question given by DATA on WINDOW. If WINDOW is nil, use selected one." (let ((stack-lto--body-src-block -- cgit v1.2.3