aboutsummaryrefslogtreecommitdiff
path: root/sx-question-mode.el
diff options
context:
space:
mode:
Diffstat (limited to 'sx-question-mode.el')
-rw-r--r--sx-question-mode.el82
1 files changed, 47 insertions, 35 deletions
diff --git a/sx-question-mode.el b/sx-question-mode.el
index 089ee12..f8a0d1e 100644
--- a/sx-question-mode.el
+++ b/sx-question-mode.el
@@ -19,8 +19,6 @@
;;; Commentary:
-;;
-
;;; Code:
(require 'markdown-mode)
@@ -54,6 +52,7 @@
(defun sx-question-mode--display (data &optional window)
"Display question given by DATA on WINDOW.
If WINDOW is nil, use selected one.
+
Returns the question buffer."
(let ((inhibit-read-only t))
(with-current-buffer
@@ -65,7 +64,7 @@ Returns the question buffer."
(defun sx-question-mode--display-buffer (window)
"Display and return the buffer used for displaying a question.
-Create the buffer if necessary.
+Create `sx-question-mode--buffer' if necessary.
If WINDOW is given, use that to display the buffer."
;; Create the buffer if necessary.
(unless (buffer-live-p sx-question-mode--buffer)
@@ -84,8 +83,9 @@ If WINDOW is given, use that to display the buffer."
;;; Printing a question's content
;;;; Faces and Variables
+
(defvar sx-question-mode--overlays nil
- "")
+ "Question mode overlays.")
(make-variable-buffer-local 'sx-question-mode--overlays)
(defface sx-question-mode-header
@@ -147,14 +147,14 @@ If WINDOW is given, use that to display the buffer."
'((((background dark)) :background "#090909")
(((background light)) :background "#f4f4f4"))
"Face used on the question body in the question buffer.
-Shouldn't have a foreground, or this will interfere with
+This shouldn't have a foreground, or this will interfere with
font-locking."
:group 'sx-question-mode-faces)
(defcustom sx-question-mode-last-edit-format " (edited %s ago by %s)"
"Format used to describe last edit date in the header.
-First %s is replaced with the date, and the second %s with the
-editor's name."
+First \"%s\" is replaced with the date and the second \"%s\" with
+the editor's name."
:type 'string
:group 'sx-question-mode)
@@ -176,8 +176,8 @@ editor's name."
(defcustom sx-question-mode-comments-format "%s: %s\n"
"Format used to display comments.
-First \"%s\" is replaced with user name.
-Second \"%s\" is replaced with the comment."
+First \"%s\" is replaced with user name. Second \"%s\" is
+replaced with the comment."
:type 'string
:group 'sx-question-mode)
@@ -206,17 +206,17 @@ QUESTION must be a data structure returned by `json-read'."
(defvar sx-question-mode--section-help-echo
(format
- (propertize "%s to hide/display content" 'face 'minibuffer-prompt)
- (propertize "RET" 'face 'font-lock-function-name-face))
- "")
+ (propertize "%s to hide/display content" 'face 'minibuffer-prompt)
+ (propertize "RET" 'face 'font-lock-function-name-face))
+ "Help echoed in the minibuffer when point is on a section.")
(defvar sx-question-mode--title-properties
`(face sx-question-mode-title
action sx-question-mode-hide-show-section
help-echo ,sx-question-mode--section-help-echo
button t
- follow-link t)
- "")
+ follow-link t)
+ "Title properties.")
(defun sx-question-mode--print-section (data)
"Print a section corresponding to DATA.
@@ -293,9 +293,11 @@ DATA can represent a question or an answer."
(propertize .display_name
'face 'sx-question-mode-author)))
-(defun sx-question-mode--print-comment (data)
- "Print the comment described by alist DATA."
- (sx-assoc-let data
+(defun sx-question-mode--print-comment (comment-data)
+ "Print the comment described by alist COMMENT-DATA.
+The comment is indented, filled, and then printed according to
+`sx-question-mode-comments-format'."
+ (sx-assoc-let comment-data
(insert
(format
sx-question-mode-comments-format
@@ -310,8 +312,10 @@ DATA can represent a question or an answer."
3)))))
(defmacro sx-question-mode--wrap-in-overlay (properties &rest body)
- "Execute BODY and wrap any inserted text in an overlay.
-Overlay is pushed on `sx-question-mode--overlays' and given PROPERTIES.
+ "Start a scope with overlay PROPERTIES and execute BODY.
+Overlay is pushed on `sx-question-mode--overlays' and given
+PROPERTIES.
+
Return the result of BODY."
(declare (indent 1)
(debug t))
@@ -325,7 +329,7 @@ Return the result of BODY."
result))
(defmacro sx-question-mode--wrap-in-text-property (properties &rest body)
- "Execute BODY and PROPERTIES to any inserted text.
+ "Start a scope with PROPERTIES and execute BODY.
Return the result of BODY."
(declare (indent 1)
(debug t))
@@ -335,9 +339,14 @@ Return the result of BODY."
result))
(defun sx-question-mode--insert-header (&rest args)
- "Insert HEADER and VALUE.
-HEADER is given `sx-question-mode-header' face, and value is given FACE.
-\(fn header value face [header value face] [header value face] ...)"
+ "Insert propertized ARGS.
+ARGS is a list of repeating values -- `header', `value', and
+`face'. `header' is given `sx-question-mode-header' as a face,
+where `value' is given `face' as its face.
+
+Syntax:
+
+ \(fn HEADER VALUE FACE [HEADER VALUE FACE] [HEADER VALUE FACE] ...)"
(while args
(insert
(propertize (pop args) 'face 'sx-question-mode-header)
@@ -351,7 +360,7 @@ HEADER is given `sx-question-mode-header' face, and value is given FACE.
"String to be displayed as the bullet of markdown list items.")
(defun sx-question-mode--fill-and-fontify (text)
- "Fill TEXT according to `markdown-mode' and return it."
+ "Return TEXT filled according to `markdown-mode'."
(with-temp-buffer
(erase-buffer)
(insert text)
@@ -415,7 +424,7 @@ URL is used as 'help-echo and 'url properties."
;; Mouse-over
'help-echo (format
(propertize "URL: %s, %s to visit" 'face 'minibuffer-prompt)
- (propertize url 'face 'default)
+ (propertize url 'face 'default)
(propertize "RET" 'face 'font-lock-function-name-face))
;; In case we need it.
'url url
@@ -430,22 +439,22 @@ URL is used as 'help-echo and 'url properties."
'action #'sx-question-mode-follow-link))
(defun sx-question-mode-follow-link (&optional pos)
- "Follow link at POS or point"
+ "Follow link at POS. If POS is nil, use `point'."
(interactive)
(browse-url
(or (get-text-property (or pos (point)) 'url)
- (error "No url under point: %s" (or pos (point))))))
+ (user-error "No url under point: %s" (or pos (point))))))
-(defun sx-question-mode-find-reference (id &optional id2)
+(defun sx-question-mode-find-reference (id &optional fallback-id)
"Find url identified by reference ID in current buffer.
-If ID is nil, use ID2 instead."
+If ID is nil, use FALLBACK-ID instead."
(save-excursion
(save-match-data
(goto-char (point-min))
(when (search-forward-regexp
(format (rx line-start (0+ blank) "[%s]:" (0+ blank)
(group-n 1 (1+ (not blank))))
- (or id id2))
+ (or id fallback-id))
nil t)
(match-string-no-properties 1)))))
@@ -468,6 +477,7 @@ If ID is nil, use ID2 instead."
"Screen line to which we recenter after moving between sections.
This is used as an argument to `recenter', only used if the end
of section is outside the window.
+
If nil, no recentering is performed."
:type '(choice (const :tag "Don't recenter" nil)
integer)
@@ -497,13 +507,14 @@ Prefix argument N moves N sections down or up."
(defun sx-question-mode-previous-section (&optional n)
"Move down to previous section (question or answer) of this buffer.
-Prefix argument N moves N sections up or down."
+Prefix argument moves N sections up or down."
(interactive "p")
(sx-question-mode-next-section (- (or n 1))))
(defun sx-question-mode--goto-property-change (prop &optional direction)
- "Move forward until the value of text-property sx-question-mode--PROP changes.
+ "Move forward to the next change of text-property sx-question-mode--PROP.
Return the new value of PROP at point.
+
If DIRECTION is negative, move backwards instead."
(let ((prop (intern (format "sx-question-mode--%s" prop)))
(func (if (and (numberp direction)
@@ -516,9 +527,9 @@ If DIRECTION is negative, move backwards instead."
(goto-char (funcall func (point) prop nil limit))
(get-text-property (point) prop)))
-;;; Optional argument is for `push-button'.
(defun sx-question-mode-hide-show-section (&optional _)
- "Hide or show section under point."
+ "Hide or show section under point.
+Optional argument _ is for `push-button'."
(interactive)
(let ((ov (car (or (sx-question-mode--section-overlays-at (point))
(sx-question-mode--section-overlays-at
@@ -537,8 +548,9 @@ If DIRECTION is negative, move backwards instead."
;;; Major-mode
(define-derived-mode sx-question-mode markdown-mode "Question"
- "Major mode for a question and its answers.
+ "Major mode to display and navigate a question and its answers.
Letters do not insert themselves; instead, they are commands.
+
\\<sx-question-mode>
\\{sx-question-mode}"
;; Determine how to close this window.