From 359a48845905feab401b5dc0783733ff230f8956 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 5 Jan 2015 01:03:58 -0200 Subject: Refactor sx-compose--goto-tag-header --- sx-compose.el | 23 ++++++++++++++--------- 1 file changed, 14 insertions(+), 9 deletions(-) (limited to 'sx-compose.el') diff --git a/sx-compose.el b/sx-compose.el index 8a6edc3..ac6700b 100644 --- a/sx-compose.el +++ b/sx-compose.el @@ -137,20 +137,25 @@ contents to the API, then calls `sx-compose-after-send-functions'." (with-current-buffer buffer (kill-new (buffer-string))))) +(defun sx-compose--goto-tag-header () + "Move to the \"Tags:\" header. +Match data is set so group 1 encompasses any already inserted +tags. Return a list of already inserted tags." + (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")) + (split-string (match-string 1) "[[:space:],;]" + 'omit-nulls "[[:space:]]")) + (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))) + sx-compose--site (sx-compose--goto-tag-header)))) (if invalid-tags ;; If the user doesn't want to create the tags, we return ;; nil and sending is aborted. -- cgit v1.2.3 From 60050b7b773a3bfd5d7ba0bfa53f46c6e1b4dca5 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 5 Jan 2015 01:04:11 -0200 Subject: Implement sx-compose-insert-tags --- sx-compose.el | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) (limited to 'sx-compose.el') diff --git a/sx-compose.el b/sx-compose.el index ac6700b..e1f6874 100644 --- a/sx-compose.el +++ b/sx-compose.el @@ -120,6 +120,21 @@ contents to the API, then calls `sx-compose-after-send-functions'." (run-hook-with-args 'sx-compose-after-send-functions (current-buffer) result))))) +(defun sx-compose-insert-tags () + "Prompt for a tag list for this draft and insert them." + (interactive) + (save-excursion + (let* ((old (sx-compose--goto-tag-header)) + (new + (save-match-data + (mapconcat + #'identity + (sx-tag-multiple-read sx-compose--site "Tags" old) + " ")))) + (if (match-string 1) + (replace-match new :fixedcase nil nil 1) + (insert new))))) + ;;; Functions for use in hooks (defun sx-compose-quit (buffer _) @@ -143,12 +158,13 @@ Match data is set so group 1 encompasses any already inserted tags. Return a list of already inserted tags." (goto-char (point-min)) (unless (search-forward-regexp - "^Tags : *\\([^[:space:]].*\\) *$" + (rx bol "Tags : " (group-n 1 (* not-newline)) eol) (next-single-property-change (point-min) 'sx-compose-separator) 'noerror) (error "No Tags header found")) - (split-string (match-string 1) "[[:space:],;]" - 'omit-nulls "[[:space:]]")) + (save-match-data + (split-string (match-string 1) (rx (any space ",;")) + 'omit-nulls (rx space)))) (defun sx-compose--check-tags () "Check if tags in current compose buffer are valid." -- cgit v1.2.3 From f77495881d218a96a8b875e650a0d8acf0f36354 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 5 Jan 2015 11:23:30 -0200 Subject: Bind sx-compose-insert-tags to "\C-c\C-q" Fix #137 --- sx-compose.el | 3 +++ 1 file changed, 3 insertions(+) (limited to 'sx-compose.el') diff --git a/sx-compose.el b/sx-compose.el index e1f6874..ee4f774 100644 --- a/sx-compose.el +++ b/sx-compose.el @@ -107,6 +107,9 @@ with the `sx-compose-create' function. (define-key sx-compose-mode-map "\C-c\C-c" #'sx-compose-send) (define-key sx-compose-mode-map "\C-c\C-k" #'sx-compose-quit) +(sx--define-conditional-key + sx-compose-mode-map "\C-c\C-q" #'sx-compose-insert-tags + (ignore-errors (string= "Title: " (substring (buffer-string) 0 7)))) (defun sx-compose-send () "Finish composing current buffer and send it. -- cgit v1.2.3 From 51b5f280119745074e9d16042b5608f25cbb6e06 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 5 Jan 2015 16:09:37 -0200 Subject: Turn sx-compose--question-headers into a defconst --- sx-compose.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sx-compose.el') diff --git a/sx-compose.el b/sx-compose.el index ee4f774..0e5e35f 100644 --- a/sx-compose.el +++ b/sx-compose.el @@ -67,7 +67,7 @@ succeeds.") Is invoked between `sx-compose-before-send-hook' and `sx-compose-after-send-functions'.") -(defvar sx-compose--question-headers +(defconst sx-compose--question-headers (concat #("Title: " 0 7 (intangible t read-only t rear-nonsticky t)) "%s" -- cgit v1.2.3 From cbd587f6fdcd9936132972e510d4cbd973d3a703 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 5 Jan 2015 16:31:48 -0200 Subject: Add sx-compose--is-question-p variable --- sx-compose.el | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) (limited to 'sx-compose.el') diff --git a/sx-compose.el b/sx-compose.el index 0e5e35f..db76531 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--is-question-p nil + "Non-nil if this `sx-compose-mode' buffer is a question.") +(make-variable-buffer-local 'sx-compose--is-question-p) + (defvar sx-compose--site nil "Site which the curent compose buffer belongs to.") (make-variable-buffer-local 'sx-compose--site) @@ -95,8 +99,11 @@ just implements some extra features related to posting to the API. This mode won't function if `sx-compose--send-function' isn't -set. To make sure you set it correctly, you can create the buffer -with the `sx-compose-create' function. +set. To make sure you set it correctly, you can create the +buffer with the `sx-compose-create' function. + +If creating a question draft, the `sx-compose--is-question-p' +variable should also be set to enable more functionality. \\ \\{sx-compose-mode}" @@ -109,7 +116,7 @@ with the `sx-compose-create' function. (define-key sx-compose-mode-map "\C-c\C-k" #'sx-compose-quit) (sx--define-conditional-key sx-compose-mode-map "\C-c\C-q" #'sx-compose-insert-tags - (ignore-errors (string= "Title: " (substring (buffer-string) 0 7)))) + sx-compose--is-question-p) (defun sx-compose-send () "Finish composing current buffer and send it. @@ -204,6 +211,7 @@ respectively added locally to `sx-compose-before-send-hook' and (with-current-buffer (sx-compose--get-buffer-create site parent) (sx-compose-mode) (setq sx-compose--site site) + (setq sx-compose--is-question-p is-question) (setq sx-compose--send-function (if (consp parent) (sx-assoc-let parent -- cgit v1.2.3 From 023b5511df15df4289783a8701fec2bed95462f1 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Mon, 5 Jan 2015 16:32:06 -0200 Subject: Add header-line to compose-buffer --- sx-compose.el | 14 ++++++++++++++ 1 file changed, 14 insertions(+) (limited to 'sx-compose.el') diff --git a/sx-compose.el b/sx-compose.el index db76531..67c476e 100644 --- a/sx-compose.el +++ b/sx-compose.el @@ -82,6 +82,19 @@ Is invoked between `sx-compose-before-send-hook' and "Headers inserted when composing a new question. Used by `sx-compose-create'.") +(defconst sx-compose--header-line + '(" " + (:propertize "C-c C-c" face mode-line-buffer-id) + ": Finish and Send" + (sx-compose--is-question-p + (" " + (:propertize "C-c C-q" face mode-line-buffer-id) + ": Insert tags")) + " " + (:propertize "C-c C-k" face mode-line-buffer-id) + ": Discard Draft") + "Header-line used on `sx-compose-mode' drafts.") + (defvar sx-compose--is-question-p nil "Non-nil if this `sx-compose-mode' buffer is a question.") (make-variable-buffer-local 'sx-compose--is-question-p) @@ -107,6 +120,7 @@ variable should also be set to enable more functionality. \\ \\{sx-compose-mode}" + (setq header-line-format sx-compose--header-line) (add-hook 'sx-compose-after-send-functions #'sx-compose-quit nil t) (add-hook 'sx-compose-after-send-functions -- cgit v1.2.3