aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArtur Malabarba <bruce.connor.am@gmail.com>2014-12-17 12:53:25 -0200
committerArtur Malabarba <bruce.connor.am@gmail.com>2014-12-22 10:46:41 -0200
commitad3849d4b4d946bc186473718ccd50953a94c143 (patch)
treec564aecdf8db1e405fbaf05785acc3eeae6ad0e6
parente9f3aa5c9ff474b2700d1c982f93a1d37aa6a3ca (diff)
Refactor comment validity checking
-rw-r--r--sx-interaction.el16
1 files changed, 14 insertions, 2 deletions
diff --git a/sx-interaction.el b/sx-interaction.el
index 177a054..9af9ac6 100644
--- a/sx-interaction.el
+++ b/sx-interaction.el
@@ -234,8 +234,8 @@ TEXT is a string. Interactively, it is read from the minibufer."
"Comment text: "
(when .comment_id
(concat (sx--user-@name .owner) " "))))
- (while (< (string-width text) 15)
- (setq text (read-string "Comment text (at least 15 characters): " text))))
+ (while (not (sx--comment-valid-p text 'silent))
+ (setq text (read-string "Comment text (between 16 and 600 characters): " text))))
;; If non-interactive, `text' could be anything.
(unless (stringp text)
(error "Comment body must be a string"))
@@ -259,6 +259,18 @@ TEXT is a string. Interactively, it is read from the minibufer."
;; Display the changes in `data'.
(sx--maybe-update-display)))))
+(defun sx--comment-valid-p (&optional text silent)
+ "Non-nil if TEXT fits stack exchange comment length limits.
+If TEXT is nil, use `buffer-string'. Must have more than 15 and
+less than 601 characters.
+If SILENT is nil, message the user about this limit."
+ (let ((w (string-width (or text (buffer-string)))))
+ (if (and (< 15 w) (< w 601))
+ t
+ (unless silent
+ (message "Comments must be within 16 and 600 characters."))
+ nil)))
+
(defun sx--get-post (type site id)
"Find in the database a post identified by TYPE, SITE and ID.
TYPE is `question' or `answer'.