diff options
author | Artur Malabarba <bruce.connor.am@gmail.com> | 2014-12-05 14:50:53 +0000 |
---|---|---|
committer | Artur Malabarba <bruce.connor.am@gmail.com> | 2014-12-05 14:50:53 +0000 |
commit | 9a8a3842f31a3e31e04e073689bdf1e29330f356 (patch) | |
tree | 27a379de8229618b7eaa85d1f375c541ec6b6f7a | |
parent | 049555600add426d2e539648e6e2927e6e7e4360 (diff) |
Check tags before sending in compose-mode
-rw-r--r-- | sx-compose.el | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/sx-compose.el b/sx-compose.el index d7d3ff3..5d22bf7 100644 --- a/sx-compose.el +++ b/sx-compose.el @@ -79,6 +79,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" @@ -113,6 +117,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 _) "Kill BUFFER." (interactive (list (current-buffer) nil)) @@ -125,6 +131,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) @@ -146,6 +172,7 @@ respectively added locally to `sx-compose-before-send-hook' and (null (cdr (assoc 'answer_id 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 @@ -170,6 +197,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 (and is-question (string= (buffer-string) "")) |