aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorArtur Malabarba <bruce.connor.am@gmail.com>2014-12-01 23:09:39 +0000
committerArtur Malabarba <bruce.connor.am@gmail.com>2014-12-01 23:09:39 +0000
commita803e5855553e7ce039d540a3550317e6805730d (patch)
treedb3e4c8d5febe2c8182da634c522402ea7aa1da6
parent5bba68bbbd76ae4db723354ba9112034cc0a724d (diff)
Rename before-functions
-rw-r--r--sx-compose.el80
1 files changed, 49 insertions, 31 deletions
diff --git a/sx-compose.el b/sx-compose.el
index 1eb4aa6..45b2288 100644
--- a/sx-compose.el
+++ b/sx-compose.el
@@ -105,7 +105,17 @@ contents to the API, then calls `sx-compose-after-send-functions'."
;;; Functions to help preparing buffers
-(defun sx-compose--create (site parent &optional before-hooks after-functions)
+(defvar sx-compose--question-headers
+ (insert (concat
+ (propertize "Title: " 'rear-nonsticky t
+ 'read-only t
+ 'field 'sx-compose-header-title)
+ (propertize "\nTags: " 'rear-nonsticky t
+ 'field 'sx-compose-header-tags
+ 'read-only t )))
+ "")
+
+(defun sx-compose--create (site parent &optional before-functions after-functions)
"Create a `sx-compose-mode' buffer.
SITE is the site where it will be posted.
@@ -114,39 +124,47 @@ If composing answers, it is the `question_id'.
If editing answers or questions, it should be the alist data
related to that object.
-Each element of BEFORE-HOOKS and AFTER-FUNCTIONS are respectively
-added locally to `sx-compose-before-send-hook' and
+Each element of BEFORE-FUNCTIONS and AFTER-FUNCTIONS are
+respectively added locally to `sx-compose-before-send-hook' and
`sx-compose-after-send-functions'."
(or (integerp parent) (listp parent)
(error "Invalid PARENT"))
- (with-current-buffer (sx-compose--get-buffer-create site parent)
- (sx-compose-mode)
- (setq sx-compose--send-function
- (if (consp parent)
- (sx-assoc-let parent
- (lambda () (sx-method-call (if .title 'questions 'answers)
- :auth 'warn
- :url-method "POST"
- :filter sx-browse-filter
- :site site
- :keywords (sx-compose--generate-keywords .title)
- :id (or .answer_id .question_id)
- :submethod 'edit)))
- (lambda () (sx-method-call 'questions
- :auth 'warn
- :url-method "POST"
- :filter sx-browse-filter
- :site site
- :keywords (sx-compose--generate-keywords (null parent))
- :id parent
- :submethod (if parent 'answers/add 'add)))))
- ;; Reverse so they're left in the same order.
- (dolist (it (reverse before-hooks))
- (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))
- ;; Return the buffer
- (current-buffer)))
+ (let ((is-question
+ (and (listp parent)
+ (null (cdr (assoc 'answer_id parent))))))
+ (with-current-buffer (sx-compose--get-buffer-create site parent)
+ (sx-compose-mode)
+ (setq sx-compose--send-function
+ (if (consp parent)
+ (sx-assoc-let parent
+ (lambda () (sx-method-call (if .title 'questions 'answers)
+ :auth 'warn
+ :url-method "POST"
+ :filter sx-browse-filter
+ :site site
+ :keywords (sx-compose--generate-keywords is-question)
+ :id (or .answer_id .question_id)
+ :submethod 'edit)))
+ (lambda () (sx-method-call 'questions
+ :auth 'warn
+ :url-method "POST"
+ :filter sx-browse-filter
+ :site site
+ :keywords (sx-compose--generate-keywords is-question)
+ :id parent
+ :submethod (if parent 'answers/add 'add)))))
+ ;; Reverse so they're left in the same order.
+ (dolist (it (reverse before-functions))
+ (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))
+ ;; If the buffer is empty, the draft didn't exist. So prepare the
+ ;; question.
+ (when (and is-question
+ (string= (buffer-string) ""))
+ (insert sx-compose--question-headers))
+ ;; Return the buffer
+ (current-buffer))))
(defun sx-compose--generate-keywords (is-question)
"Reading current buffer, generate a keywords alist.