aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--sx-question-list.el42
-rw-r--r--sx-search.el14
-rw-r--r--sx-tab.el288
-rw-r--r--test/test-printing.el13
4 files changed, 198 insertions, 159 deletions
diff --git a/sx-question-list.el b/sx-question-list.el
index 7757503..5812ff2 100644
--- a/sx-question-list.el
+++ b/sx-question-list.el
@@ -230,12 +230,11 @@ This is ignored if `sx-question-list--refresh-function' is set.")
": Quit")
"Header-line used on the question list.")
-(defconst sx-question-list--order-methods
+(defvar sx-question-list--order-methods
'(("Recent Activity" . activity)
("Creation Date" . creation)
("Most Voted" . votes)
- ("Score" . votes)
- ("Hot" . hot))
+ ("Score" . votes))
"Alist of possible values to be passed to the `sort' keyword.")
(make-variable-buffer-local 'sx-question-list--order-methods)
@@ -248,6 +247,20 @@ is used."
(mapcar #'car sx-question-list--order-methods))))
(cdr-safe (assoc-string order sx-question-list--order-methods))))
+(defun sx-question-list--make-pager (method &optional submethod)
+ "Return a function suitable for use as a question list pager.
+Meant to be used as `sx-question-list--next-page-function'."
+ (lambda (page)
+ (sx-method-call method
+ :keywords `((page . ,page)
+ ,@(when sx-question-list--order
+ `((order . ,(if sx-question-list--descending 'desc 'asc))
+ (sort . ,sx-question-list--order))))
+ :site sx-question-list--site
+ :auth t
+ :submethod submethod
+ :filter sx-browse-filter)))
+
;;; Mode Definition
(define-derived-mode sx-question-list-mode
@@ -367,6 +380,9 @@ into consideration. The same holds for `sx-question-list--order'.
([?\r] sx-display)
))
+(sx--define-conditional-key sx-question-list-mode-map "O" #'sx-question-list-order-by
+ (and (boundp 'sx-question-list--order) sx-question-list--order))
+
(defun sx-question-list-hide (data)
"Hide question under point.
Non-interactively, DATA is a question alist."
@@ -443,7 +459,14 @@ Non-interactively, DATA is a question alist."
(make-variable-buffer-local 'sx-question-list--site)
(defvar sx-question-list--order nil
- "Order being displayed in the *question-list* buffer.")
+ "Order being displayed in the *question-list* buffer.
+This is also affected by `sx-question-list--descending'.")
+(make-variable-buffer-local 'sx-question-list--order)
+
+(defvar sx-question-list--descending t
+ "In which direction should `sx-question-list--order' be sorted.
+If non-nil (default), descending.
+If nil, ascending.")
(make-variable-buffer-local 'sx-question-list--order)
(defun sx-question-list-refresh (&optional redisplay no-update)
@@ -618,17 +641,22 @@ Sets `sx-question-list--site' and then call
(setq sx-question-list--site site)
(sx-question-list-refresh 'redisplay)))
-(defun sx-question-list-order-by (sort)
+(defun sx-question-list-order-by (sort &optional ascend)
"Order questions in the current list by the method SORT.
Sets `sx-question-list--order' and then calls
-`sx-question-list-refresh' with `redisplay'."
+`sx-question-list-refresh' with `redisplay'.
+
+With a prefix argument or a non-nil ASCEND, invert the sorting
+order."
(interactive
(list (when sx-question-list--order
- (sx-question-list--interactive-order-prompt))))
+ (sx-question-list--interactive-order-prompt))
+ current-prefix-arg))
(unless sx-question-list--order
(sx-user-error "This list can't be reordered"))
(when (and sort (symbolp sort))
(setq sx-question-list--order sort)
+ (setq sx-question-list--descending (not ascend))
(sx-question-list-refresh 'redisplay)))
(provide 'sx-question-list)
diff --git a/sx-search.el b/sx-search.el
index 55964b9..4b0a0b2 100644
--- a/sx-search.el
+++ b/sx-search.el
@@ -64,13 +64,18 @@ KEYWORDS is passed to `sx-method-call'."
(defconst sx-search--order-methods
(cons '("Relevance" . relevance)
- (cl-remove-if (lambda (x) (eq (cdr x) 'hot))
- (default-value 'sx-question-list--order-methods)))
+ (default-value 'sx-question-list--order-methods))
"Alist of possible values to be passed to the `sort' keyword.")
-(defvar sx-search-default-order 'activity
+(defcustom sx-search-default-order 'activity
"Default ordering method used on new searches.
-Possible values are the cdrs of `sx-search--order-methods'.")
+Possible values are the cdrs of `sx-search--order-methods'."
+ :type (cons 'choice
+ (mapcar (lambda (c) `(const :tag ,(car c) ,(cdr c)))
+ (cl-remove-duplicates
+ sx-search--order-methods
+ :key #'cdr)))
+ :group 'sx-question-list)
;;;###autoload
@@ -110,6 +115,7 @@ prefix argument, the user is asked for everything."
(sx-search-get-questions
sx-question-list--site page
query tags excluded-tags
+ (cons 'order (if sx-question-list--descending 'desc 'asc))
(cons 'sort sx-question-list--order))))
(setq sx-question-list--site site)
(setq sx-question-list--order sx-search-default-order)
diff --git a/sx-tab.el b/sx-tab.el
index 2d605a5..a3c6107 100644
--- a/sx-tab.el
+++ b/sx-tab.el
@@ -53,9 +53,34 @@
t)))
(funcall (intern (format "sx-tab-%s" (downcase tab)))))
+(defconst sx-tab--order-methods
+ `(,@(default-value 'sx-question-list--order-methods)
+ ("Hottest Now" . hot)
+ ("Weekly Hottest" . week)
+ ("Monthly Hottest" . month))
+ "Alist of possible values to be passed to the `sort' keyword.")
+
+(defcustom sx-tab-default-order 'activity
+ "Default ordering method used on `sx-tab-questions' and the likes.
+Possible values are the cdrs of `sx-tab--order-methods'."
+ :type (cons 'choice
+ (mapcar (lambda (c) `(const :tag ,(car c) ,(cdr c)))
+ (cl-remove-duplicates
+ sx-tab--order-methods
+ :key #'cdr)))
+ :group 'sx-question-list)
+
+(defconst sx-tab--docstring-format
+ "Display a list of %s questions for SITE.
+The variable `sx-tab-default-order' can be used to customize the
+sorting of the resulting list.
+
+NO-UPDATE (the prefix arg) is passed to `sx-question-list-refresh'.
+If SITE is nil, use `sx-default-site'.")
+
;;; The main macro
-(defmacro sx-tab--define (tab pager &optional printer refresher
+(defmacro sx-tab--define (tab pager &optional printer refresher obsolete
&rest body)
"Define a StackExchange tab called TAB.
TAB is a capitalized string.
@@ -70,23 +95,26 @@ respectively used to set the value of the variables
`sx-question-list--next-page-function'.
BODY is evaluated after activating the mode and setting these
-variables, but before refreshing the display."
+variables, but before refreshing the display.
+
+If OBSOLETE is non-nil, it should be a string indicating the tab
+to use instead."
(declare (indent 1) (debug t))
(let* ((name (downcase tab))
(buffer-variable
- (intern (concat "sx-tab--" name "-buffer"))))
+ (intern (format "sx-tab--%s-buffer"
+ (if obsolete (downcase obsolete)
+ name))))
+ (function-name
+ (intern (concat "sx-tab-" name)))
+ (use-instead
+ (when obsolete (intern (concat "sx-tab-" (downcase obsolete))))))
`(progn
- (defvar ,buffer-variable nil
- ,(format "Buffer where the %s questions are displayed."
- tab))
- (defun
- ,(intern (concat "sx-tab-" name))
- (&optional no-update site)
- ,(format "Display a list of %s questions for SITE.
-
-NO-UPDATE (the prefix arg) is passed to `sx-question-list-refresh'.
-If SITE is nil, use `sx-default-site'."
- tab)
+ ,(unless obsolete
+ `(defvar ,buffer-variable nil
+ ,(format "Buffer where the %s questions are displayed." tab)))
+ (defun ,function-name (&optional no-update site)
+ ,(format sx-tab--docstring-format tab)
(interactive
(list current-prefix-arg
(sx--interactive-site-prompt)))
@@ -95,187 +123,163 @@ If SITE is nil, use `sx-default-site'."
;; Create the buffer
(unless (buffer-live-p ,buffer-variable)
(setq ,buffer-variable
- (generate-new-buffer "*question-list*")))
+ (generate-new-buffer
+ ,(format "*question-list: %s *" (or obsolete tab)))))
;; Fill the buffer with content.
(with-current-buffer ,buffer-variable
(sx-question-list-mode)
- ,(when printer
- `(setq sx-question-list--print-function ,printer))
- ,(when refresher
- `(setq sx-question-list--refresh-function ,refresher))
- ,(when pager
- `(setq sx-question-list--next-page-function ,pager))
+ (when ,printer (setq sx-question-list--print-function ,printer))
+ (when ,refresher (setq sx-question-list--refresh-function ,refresher))
+ (setq sx-question-list--next-page-function ,pager)
(setq sx-question-list--site site)
- (setq sx-question-list--current-tab ,tab)
+ (setq sx-question-list--order 'activity)
+ (setq sx-question-list--current-tab ,(or obsolete tab))
,@body
(sx-question-list-refresh 'redisplay no-update))
(switch-to-buffer ,buffer-variable))
+ ,(when obsolete
+ `(make-obsolete ',function-name ',use-instead nil))
;; Add this tab to the list of existing tabs. So we can prompt
;; the user with completion and stuff.
- (add-to-list 'sx-tab--list ,tab))))
+ (unless ,obsolete
+ (add-to-list 'sx-tab--list ,tab)))))
-;;; FrontPage
-(sx-tab--define "FrontPage"
- (lambda (page)
- (sx-question-get-questions
- sx-question-list--site page '((sort . activity)))))
+;;; Entry commands
+(sx-tab--define "All-Questions"
+ (sx-question-list--make-pager 'questions)
+ nil nil nil
+ (setq sx-question-list--order-methods
+ sx-tab--order-methods))
;;;###autoload
-(autoload 'sx-tab-frontpage
+(autoload 'sx-tab-all-questions
(expand-file-name
- "sx-tab"
- (when load-file-name
- (file-name-directory load-file-name)))
+ "sx-tab" (when load-file-name (file-name-directory load-file-name)))
nil t)
-
-;;; Newest
-(sx-tab--define "Newest"
- (lambda (page)
- (sx-question-get-questions
- sx-question-list--site page '((sort . creation)))))
+(sx-tab--define "Unanswered"
+ (sx-question-list--make-pager 'questions 'unanswered))
;;;###autoload
-(autoload 'sx-tab-newest
+(autoload 'sx-tab-unanswered
(expand-file-name
- "sx-tab"
- (when load-file-name
- (file-name-directory load-file-name)))
+ "sx-tab" (when load-file-name (file-name-directory load-file-name)))
nil t)
-
-
-;;; TopVoted
-(sx-tab--define "TopVoted"
- (lambda (page)
- (sx-question-get-questions
- sx-question-list--site page '((sort . votes)))))
+(sx-tab--define "Unanswered-My-Tags"
+ (sx-question-list--make-pager 'questions 'unanswered/my-tags))
;;;###autoload
-(autoload 'sx-tab-topvoted
+(autoload 'sx-tab-unanswered-my-tags
(expand-file-name
- "sx-tab"
- (when load-file-name
- (file-name-directory load-file-name)))
+ "sx-tab" (when load-file-name (file-name-directory load-file-name)))
nil t)
-
-
-;;; Hot
-(sx-tab--define "Hot"
- (lambda (page)
- (sx-question-get-questions
- sx-question-list--site page '((sort . hot)))))
+(sx-tab--define "Featured"
+ (sx-question-list--make-pager 'questions 'featured))
;;;###autoload
-(autoload 'sx-tab-hot
+(autoload 'sx-tab-featured
(expand-file-name
- "sx-tab"
- (when load-file-name
- (file-name-directory load-file-name)))
+ "sx-tab" (when load-file-name (file-name-directory load-file-name)))
nil t)
-
-
-;;; Week
-(sx-tab--define "Week"
- (lambda (page)
- (sx-question-get-questions
- sx-question-list--site page '((sort . week)))))
+(sx-tab--define "Starred"
+ (sx-question-list--make-pager 'me 'favorites))
;;;###autoload
-(autoload 'sx-tab-week
+(autoload 'sx-tab-featured
(expand-file-name
- "sx-tab"
- (when load-file-name
- (file-name-directory load-file-name)))
+ "sx-tab" (when load-file-name (file-name-directory load-file-name)))
nil t)
+
+;;; Inter-modes navigation
+(defun sx-tab-meta-or-main ()
+ "Switch to the meta version of a main site, or vice-versa.
+Inside a question, go to the frontpage of the site this question
+belongs to."
+ (interactive)
+ (if (and (derived-mode-p 'sx-question-list-mode)
+ sx-question-list--site)
+ (sx-question-list-switch-site
+ (if (string-match "\\`meta\\." sx-question-list--site)
+ (replace-match "" :fixedcase nil sx-question-list--site)
+ (concat "meta." sx-question-list--site)))
+ (sx-tab-all-questions nil (sx--site (sx--data-here 'question)))))
-;;; Month
-(sx-tab--define "Month"
- (lambda (page)
- (sx-question-get-questions
- sx-question-list--site page '((sort . month)))))
+;;; Obsolete tabs
+(defconst sx-tab--basic-question-pager
+ (sx-question-list--make-pager 'questions))
+
+(sx-tab--define "FrontPage"
+ sx-tab--basic-question-pager
+ nil nil "All-Questions"
+ (setq sx-question-list--order 'activity)
+ (setq sx-question-list--order-methods
+ sx-tab--order-methods))
;;;###autoload
-(autoload 'sx-tab-month
+(autoload 'sx-tab-frontpage
(expand-file-name
- "sx-tab"
- (when load-file-name
- (file-name-directory load-file-name)))
+ "sx-tab" (when load-file-name (file-name-directory load-file-name)))
nil t)
-
-;;; Unanswered
-(sx-tab--define "Unanswered"
- (lambda (page)
- (sx-question-get-questions
- sx-question-list--site page nil 'unanswered)))
+(sx-tab--define "Newest"
+ sx-tab--basic-question-pager
+ nil nil "All-Questions"
+ (setq sx-question-list--order 'creation)
+ (setq sx-question-list--order-methods
+ sx-tab--order-methods))
;;;###autoload
-(autoload 'sx-tab-unanswered
+(autoload 'sx-tab-newest
(expand-file-name
- "sx-tab"
- (when load-file-name
- (file-name-directory load-file-name)))
+ "sx-tab" (when load-file-name (file-name-directory load-file-name)))
nil t)
-
-;;; Unanswered My-tags
-(sx-tab--define "Unanswered-my-tags"
- (lambda (page)
- (sx-question-get-questions
- sx-question-list--site page nil 'unanswered/my-tags)))
+(sx-tab--define "TopVoted"
+ sx-tab--basic-question-pager
+ nil nil "All-Questions"
+ (setq sx-question-list--order 'votes)
+ (setq sx-question-list--order-methods
+ sx-tab--order-methods))
;;;###autoload
-(autoload 'sx-tab-unanswered-my-tags
+(autoload 'sx-tab-topvoted
(expand-file-name
- "sx-tab"
- (when load-file-name
- (file-name-directory load-file-name)))
+ "sx-tab" (when load-file-name (file-name-directory load-file-name)))
nil t)
-
-;;; Featured
-(sx-tab--define "Featured"
- (lambda (page)
- (sx-question-get-questions
- sx-question-list--site page nil 'featured)))
+(sx-tab--define "Hot"
+ sx-tab--basic-question-pager
+ nil nil "All-Questions"
+ (setq sx-question-list--order 'hot)
+ (setq sx-question-list--order-methods
+ sx-tab--order-methods))
;;;###autoload
-(autoload 'sx-tab-featured
+(autoload 'sx-tab-hot
(expand-file-name
- "sx-tab"
- (when load-file-name
- (file-name-directory load-file-name)))
+ "sx-tab" (when load-file-name (file-name-directory load-file-name)))
nil t)
-
-;;; Starred
-(sx-tab--define "Starred"
- (lambda (page)
- (sx-method-call 'me
- :page page
- :site sx-question-list--site
- :auth t
- :submethod 'favorites
- :filter sx-browse-filter)))
+(sx-tab--define "Week"
+ sx-tab--basic-question-pager
+ nil nil "All-Questions"
+ (setq sx-question-list--order 'week)
+ (setq sx-question-list--order-methods
+ sx-tab--order-methods))
;;;###autoload
-(autoload 'sx-tab-featured
+(autoload 'sx-tab-week
(expand-file-name
- "sx-tab"
- (when load-file-name
- (file-name-directory load-file-name)))
+ "sx-tab" (when load-file-name (file-name-directory load-file-name)))
nil t)
-
-;;; Inter-modes navigation
-(defun sx-tab-meta-or-main ()
- "Switch to the meta version of a main site, or vice-versa.
-Inside a question, go to the frontpage of the site this question
-belongs to."
- (interactive)
- (if (and (derived-mode-p 'sx-question-list-mode)
- sx-question-list--site)
- (sx-question-list-switch-site
- (if (string-match "\\`meta\\." sx-question-list--site)
- (replace-match "" :fixedcase nil sx-question-list--site)
- (concat "meta." sx-question-list--site)))
- (sx-tab-frontpage nil (sx--site (sx--data-here 'question)))))
+(sx-tab--define "Month"
+ sx-tab--basic-question-pager
+ nil nil "All-Questions"
+ (setq sx-question-list--order 'month)
+ (setq sx-question-list--order-methods
+ sx-tab--order-methods))
+;;;###autoload
+(autoload 'sx-tab-month
+ (expand-file-name
+ "sx-tab" (when load-file-name (file-name-directory load-file-name)))
+ nil t)
(provide 'sx-tab)
;;; sx-tab.el ends here
diff --git a/test/test-printing.el b/test/test-printing.el
index bcc3dd9..6fa77b9 100644
--- a/test/test-printing.el
+++ b/test/test-printing.el
@@ -49,10 +49,11 @@ after being run through `sx-question--tag-format'."
(ert-deftest question-list-display ()
(cl-letf (((symbol-function #'sx-request-make)
(lambda (&rest _) sx-test-data-questions)))
- (sx-tab-frontpage nil "emacs")
- (switch-to-buffer "*question-list*")
+ (sx-tab-all-questions nil "emacs")
+ (switch-to-buffer sx-tab--all-questions-buffer)
(goto-char (point-min))
- (should (equal (buffer-name) "*question-list*"))
+ (should (equal (buffer-name)
+ (format "*question-list: %s *" sx-question-list--current-tab)))
(line-should-match
(question-list-regex
"Focus-hook: attenuate colours when losing focus"
@@ -63,9 +64,9 @@ after being run through `sx-question--tag-format'."
"Babel doesn't wrap results in verbatim"
0 1 "org-mode" "org-export" "org-babel"))
;; ;; Use this when we have a real sx-question buffer.
- ;; (call-interactively 'sx-question-list-display-question)
- ;; (should (equal (buffer-name) "*sx-question*"))
- (switch-to-buffer "*question-list*")
+ ;; (save-excursion
+ ;; (call-interactively 'sx-question-list-display-question)
+ ;; (should (equal (buffer-name) "*sx-question*")))
(sx-question-list-previous 4)
(line-should-match
(question-list-regex