Closed dvzubarev closed 5 years ago
Can you try this definition of sx-question-mode--print-question
? I think the bug is in this use of sx--deleted-p
.
(defun sx-question-mode--print-question (question)
"Print a buffer describing QUESTION.
QUESTION must be a data structure returned by `json-read'."
(when (sx--deleted-p question)
(sx-user-error "This is a deleted question"))
(setq sx-question-mode--data question)
;; Clear the overlays
(mapc #'delete-overlay sx--overlays)
(setq sx--overlays nil)
;; Print everything
(sx-assoc-let question
(when .closed_reason
(add-to-list 'mode-line-format sx-question-mode--closed-mode-line-string)
(sx-question-mode--print-close-reason .closed_reason .closed_date .closed_details))
(sx-question-mode--print-section question)
(mapc #'sx-question-mode--print-section
(cl-sort .answers sx-question-mode-answer-sort-function)))
(insert "\n\n ")
(insert-text-button "Write an Answer" :type 'sx-button-answer)
(run-hooks 'sx-question-mode-after-print-hook)
;; Go up
(goto-char (point-min))
(sx-question-mode-next-section))
Unfortunately, this code didn't solve the problem. I noticed that some answers are removed from the list after cl-sort
. I'm not sure why but making a copy of answers before sorting, fixes the problem.
(cl-sort (copy-sequence .answers)
sx-question-mode-answer-sort-function))
Hm, is cl-sort destructive? That would be surprising. I can try to look tonight.
Yes, cl-sort is destructive, from the docs This is a destructive function; it reuses the storage of SEQ if possible.
I can make a PR, if you want.
Re cl-sort, yes, but I imagine it would still return the sorted list. Ahh, except that .answers
is direct reference to the question object! It's not sorting it the second time that's the problem, it's sorting it the first time!
Does this patch (also in the PR above) work for you?
@@ -244,7 +244,7 @@ QUESTION must be a data structure returned by `json-read'."
(mapc #'sx-question-mode--print-section
(cl-remove-if
#'sx--deleted-p
- (cl-sort .answers sx-question-mode-answer-sort-function))))
+ (sort .answers sx-question-mode-answer-sort-function))))
(insert "\n\n ")
(insert-text-button "Write an Answer" :type 'sx-button-answer)
(run-hooks 'sx-question-mode-after-print-hook)
Note I left in the check for #'sx--deleted-p
; it shouldn't be doing any harm. A separate patch could make it work for answers here (assuming I'm correct and that it's not working right now).
It seems that sort
is destructive too. https://www.emacswiki.org/emacs/DestructiveOperations
So it doesn't work.
Damn I need to read the docs more carefully.
Ok, we'll just copy the sequence before as you suggested.
Should be fixed once MELPA updates in a few hours
sx-question-mode-order-by
( O )sx-question-mode-order-by
again and choose "More active first"