aboutsummaryrefslogtreecommitdiff
path: root/sx-compose.el
diff options
context:
space:
mode:
authorSean Allred <code@seanallred.com>2015-01-04 16:23:34 -0500
committerSean Allred <code@seanallred.com>2015-01-04 16:23:34 -0500
commit671053bf0824197fefe742ed9dd98c1b9a06565a (patch)
tree3f457adffd80d7c180b52e936cef3698b067f28b /sx-compose.el
parent3b275a93789a568d23ece65086ffebb8de430f3a (diff)
parent3c05aae9915976e749591600f6e8f59cbccef1a4 (diff)
Merge branch 'master' into more-tests
Conflicts: Makefile test/tests.el
Diffstat (limited to 'sx-compose.el')
-rw-r--r--sx-compose.el33
1 files changed, 31 insertions, 2 deletions
diff --git a/sx-compose.el b/sx-compose.el
index ab4a58d..8a8637b 100644
--- a/sx-compose.el
+++ b/sx-compose.el
@@ -82,6 +82,10 @@ Is invoked between `sx-compose-before-send-hook' and
"Headers inserted when composing a new question.
Used by `sx-compose-create'.")
+(defvar sx-compose--site nil
+ "Site which the curent compose buffer belongs to.")
+(make-variable-buffer-local 'sx-compose--site)
+
;;; Major-mode
(define-derived-mode sx-compose-mode markdown-mode "Compose"
@@ -116,6 +120,8 @@ contents to the API, then calls `sx-compose-after-send-functions'."
(run-hook-with-args 'sx-compose-after-send-functions
(current-buffer) result)))))
+
+;;; Functions for use in hooks
(defun sx-compose-quit (buffer _)
"Close BUFFER's window and kill it."
(interactive (list (current-buffer) nil))
@@ -131,6 +137,26 @@ contents to the API, then calls `sx-compose-after-send-functions'."
(with-current-buffer buffer
(kill-new (buffer-string)))))
+(defun sx-compose--check-tags ()
+ "Check if tags in current compose buffer are valid."
+ (save-excursion
+ (goto-char (point-min))
+ (unless (search-forward-regexp
+ "^Tags : *\\([^[:space:]].*\\) *$"
+ (next-single-property-change (point-min) 'sx-compose-separator)
+ 'noerror)
+ (error "No Tags header found"))
+ (let ((invalid-tags
+ (sx-tag--invalid-name-p
+ (split-string (match-string 1) "[[:space:],;]"
+ 'omit-nulls "[[:space:]]")
+ sx-compose--site)))
+ (if invalid-tags
+ ;; If the user doesn't want to create the tags, we return
+ ;; nil and sending is aborted.
+ (y-or-n-p "Following tags don't exist. Create them? %s " invalid-tags)
+ t))))
+
;;; Functions to help preparing buffers
(defun sx-compose-create (site parent &optional before-functions after-functions)
@@ -153,6 +179,7 @@ respectively added locally to `sx-compose-before-send-hook' and
(cdr (assoc 'title parent))))))
(with-current-buffer (sx-compose--get-buffer-create site parent)
(sx-compose-mode)
+ (setq sx-compose--site site)
(setq sx-compose--send-function
(if (consp parent)
(sx-assoc-let parent
@@ -161,7 +188,7 @@ respectively added locally to `sx-compose-before-send-hook' and
(.comment_id 'comments)
(t 'answers))
:auth 'warn
- :url-method "POST"
+ :url-method 'post
:filter sx-browse-filter
:site site
:keywords (sx-compose--generate-keywords is-question)
@@ -169,7 +196,7 @@ respectively added locally to `sx-compose-before-send-hook' and
:submethod 'edit)))
(lambda () (sx-method-call 'questions
:auth 'warn
- :url-method "POST"
+ :url-method 'post
:filter sx-browse-filter
:site site
:keywords (sx-compose--generate-keywords is-question)
@@ -180,6 +207,8 @@ respectively added locally to `sx-compose-before-send-hook' and
(add-hook 'sx-compose-before-send-hook it nil t))
(dolist (it (reverse after-functions))
(add-hook 'sx-compose-after-send-functions it nil t))
+ (when is-question
+ (add-hook 'sx-compose-before-send-hook #'sx-compose--check-tags nil t))
;; If the buffer is empty, the draft didn't exist. So prepare the
;; question.
(when (or (string= (buffer-string) "")