From a39fa2255a253194b2a080a7aa545a0fb52db7ec Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 16 Dec 2014 20:44:56 -0200 Subject: delay-mode-hooks on markdown-mode. Fix #159 --- sx-question-print.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index dc853ba..2bfa60d 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -340,7 +340,7 @@ E.g.: "Return TEXT filled according to `markdown-mode'." (with-temp-buffer (insert text) - (markdown-mode) + (delay-mode-hooks (markdown-mode)) (font-lock-mode -1) (when sx-question-mode-bullet-appearance (font-lock-add-keywords ;; Bullet items. -- cgit v1.2.3 From 4983653696a89a6f7030f761307771035b92d168 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Tue, 16 Dec 2014 21:55:17 -0200 Subject: Display comment score --- sx-question-print.el | 23 +++++++++++++---------- 1 file changed, 13 insertions(+), 10 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index dc853ba..6131684 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -291,17 +291,20 @@ The comment is indented, filled, and then printed according to (list 'sx--data-here comment-data) (sx-assoc-let comment-data (insert + (if (> .score 0) (number-to-string .score) "") + (if (eq .upvoted t) "^" "") + (if (or (> .score 0) .upvoted) " " "") (format - sx-question-mode-comments-format - (sx-question-mode--propertize-display-name .owner) - (substring - ;; We fill with three spaces at the start, so the comment is - ;; slightly indented. - (sx-question-mode--fill-and-fontify - (concat " " .body_markdown)) - ;; Then we remove the spaces from the first line, since we'll - ;; add the username there anyway. - 3)))))) + sx-question-mode-comments-format + (sx-question-mode--propertize-display-name .owner) + (substring + ;; We fill with three spaces at the start, so the comment is + ;; slightly indented. + (sx-question-mode--fill-and-fontify + (concat " " .body_markdown)) + ;; Then we remove the spaces from the first line, since we'll + ;; add the username there anyway. + 3)))))) (defun sx-question-mode--insert-header (&rest args) "Insert propertized ARGS. -- cgit v1.2.3 From 641beffbc3ce72b4e1b9b6bbb79880ba795fdf72 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 18 Dec 2014 16:08:51 -0200 Subject: Hotfix Don't use string-trim. Fix #164 --- sx-question-print.el | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 223049a..3d698cc 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -366,7 +366,7 @@ E.g.: (skip-chars-forward "\r\n[:blank:]") (forward-paragraph) (fill-region beg (point))))) - (string-trim-right (buffer-string)))) + (replace-regexp-in-string "[[:blank:]]+\\'" "" (buffer-string)))) (defun sx-question-mode--dont-fill-here () "If text shouldn't be filled here, return t and skip over it." -- cgit v1.2.3 From 9ba5f7751c088a3bbad7d98a5cf3cf23a08f1bf8 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 18 Dec 2014 16:25:59 -0200 Subject: Don't fill comments. Affects #141 --- sx-question-print.el | 12 ++++++++++++ 1 file changed, 12 insertions(+) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 3d698cc..a1b7589 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -371,6 +371,7 @@ E.g.: (defun sx-question-mode--dont-fill-here () "If text shouldn't be filled here, return t and skip over it." (or (sx-question-mode--skip-and-fontify-pre) + (sx-question-mode--skip-comments) ;; Skip headers and references (let ((pos (point))) (skip-chars-forward "\r\n[:blank:]") @@ -446,5 +447,16 @@ font-locking." (goto-char before) nil))) +(defun sx-question-mode--skip-comments () + "If there's an html comment ahead, skip it and return t." + ;; @TODO: Handle the comment. + ;; "Handling means to store any relevant metadata it might be holding." + (let ((before (point))) + (skip-chars-forward "\r\n[:blank:]") + (if (markdown-match-comments (line-end-position)) + t + (goto-char before) + nil))) + (provide 'sx-question-print) ;;; sx-question-print.el ends here -- cgit v1.2.3 From df276818b6275f795f4504b5df36505a7f79c8ee Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 18 Dec 2014 17:45:42 -0200 Subject: Refactor and simplify sx-question-mode--skip-FOO functions. They no longer need to worry about restoring point. Just move point to its destination and return non-nil if it worked. --- sx-question-print.el | 53 +++++++++++++++++++--------------------------------- 1 file changed, 19 insertions(+), 34 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index a1b7589..46e18ca 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -370,19 +370,19 @@ E.g.: (defun sx-question-mode--dont-fill-here () "If text shouldn't be filled here, return t and skip over it." - (or (sx-question-mode--skip-and-fontify-pre) - (sx-question-mode--skip-comments) - ;; Skip headers and references - (let ((pos (point))) - (skip-chars-forward "\r\n[:blank:]") - (goto-char (line-beginning-position)) - (if (or (looking-at-p (format sx-question-mode--reference-regexp ".+")) - (looking-at-p "^#")) - ;; Returns non-nil - (forward-paragraph) - ;; Go back and return nil - (goto-char pos) - nil)))) + (catch 'sx-question-mode-done + (let ((before (point))) + (skip-chars-forward "\r\n[:blank:]") + (let ((first-non-blank (point))) + (dolist (it '(sx-question-mode--skip-and-fontify-pre + sx-question-mode--skip-comments)) + ;; If something worked, keep point where it is and return t. + (if (funcall it) (throw 'sx-question-mode-done t) + ;; Before calling each new function. Go back to the first + ;; non-blank char. + (goto-char first-non-blank))) + ;; If nothing matched, go back to the very beginning. + (goto-char before))))) (defun sx-question-mode--process-links-in-buffer () "Turn all markdown links in this buffer into compact format." @@ -431,32 +431,17 @@ If ID is nil, use FALLBACK-ID instead." "If there's a pre block ahead, handle it, skip it and return t. Handling means to turn it into a button and remove erroneous font-locking." - (let ((before (point)) - beg end) - (if (markdown-match-pre-blocks - (save-excursion - (skip-chars-forward "\r\n[:blank:]") - (setq beg (point)))) - (progn - (setq end (point)) - (sx-babel--make-pre-button - (save-excursion - (goto-char beg) - (line-beginning-position)) - end)) - (goto-char before) - nil))) + (let ((beg (line-beginning-position))) + ;; To identify code-blocks we need to be at start of line. + (goto-char beg) + (when (markdown-match-pre-blocks (line-end-position)) + (sx-babel--make-pre-button beg (point))))) (defun sx-question-mode--skip-comments () "If there's an html comment ahead, skip it and return t." ;; @TODO: Handle the comment. ;; "Handling means to store any relevant metadata it might be holding." - (let ((before (point))) - (skip-chars-forward "\r\n[:blank:]") - (if (markdown-match-comments (line-end-position)) - t - (goto-char before) - nil))) + (markdown-match-comments (line-end-position))) (provide 'sx-question-print) ;;; sx-question-print.el ends here -- cgit v1.2.3 From b3008bf4e11bb4a5bc67a82f7bebd3f38520d0af Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 18 Dec 2014 17:49:18 -0200 Subject: Don't fill headlines --- sx-question-print.el | 8 ++++++++ 1 file changed, 8 insertions(+) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 46e18ca..6c98c36 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -375,6 +375,7 @@ E.g.: (skip-chars-forward "\r\n[:blank:]") (let ((first-non-blank (point))) (dolist (it '(sx-question-mode--skip-and-fontify-pre + sx-question-mode--skip-headline sx-question-mode--skip-comments)) ;; If something worked, keep point where it is and return t. (if (funcall it) (throw 'sx-question-mode-done t) @@ -443,5 +444,12 @@ font-locking." ;; "Handling means to store any relevant metadata it might be holding." (markdown-match-comments (line-end-position))) +(defun sx-question-mode--skip-headline () + "If there's a headline ahead, skip it and return non-nil." + (when (or (looking-at-p "^#+ ") + (progn (forward-line 1) (looking-at-p "===\\|---"))) + ;; Returns non-nil. + (forward-line 1))) + (provide 'sx-question-print) ;;; sx-question-print.el ends here -- cgit v1.2.3 From 2a77336ccbe19acdfd176b7198736a78be887e04 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 18 Dec 2014 17:49:29 -0200 Subject: Reimplement reference not-filling --- sx-question-print.el | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 6c98c36..97124e9 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -376,6 +376,7 @@ E.g.: (let ((first-non-blank (point))) (dolist (it '(sx-question-mode--skip-and-fontify-pre sx-question-mode--skip-headline + sx-question-mode--skip-references sx-question-mode--skip-comments)) ;; If something worked, keep point where it is and return t. (if (funcall it) (throw 'sx-question-mode-done t) @@ -451,5 +452,11 @@ font-locking." ;; Returns non-nil. (forward-line 1))) +(defun sx-question-mode--skip-references () + "If there's a reference ahead, skip it and return non-nil." + (while (looking-at-p (format sx-question-mode--reference-regexp ".+")) + ;; Returns non-nil + (forward-line 1))) + (provide 'sx-question-print) ;;; sx-question-print.el ends here -- cgit v1.2.3 From 806910361bb26b76a585c23c1bd695e38da2dc9a Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 18 Dec 2014 17:53:18 -0200 Subject: Reorganize functions. --- sx-question-print.el | 40 ++++++++++++++++++++++------------------ 1 file changed, 22 insertions(+), 18 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 97124e9..996b057 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -368,24 +368,8 @@ E.g.: (fill-region beg (point))))) (replace-regexp-in-string "[[:blank:]]+\\'" "" (buffer-string)))) -(defun sx-question-mode--dont-fill-here () - "If text shouldn't be filled here, return t and skip over it." - (catch 'sx-question-mode-done - (let ((before (point))) - (skip-chars-forward "\r\n[:blank:]") - (let ((first-non-blank (point))) - (dolist (it '(sx-question-mode--skip-and-fontify-pre - sx-question-mode--skip-headline - sx-question-mode--skip-references - sx-question-mode--skip-comments)) - ;; If something worked, keep point where it is and return t. - (if (funcall it) (throw 'sx-question-mode-done t) - ;; Before calling each new function. Go back to the first - ;; non-blank char. - (goto-char first-non-blank))) - ;; If nothing matched, go back to the very beginning. - (goto-char before))))) - + +;;; Handling links (defun sx-question-mode--process-links-in-buffer () "Turn all markdown links in this buffer into compact format." (save-excursion @@ -429,6 +413,26 @@ If ID is nil, use FALLBACK-ID instead." nil t) (match-string-no-properties 1))))) + +;;; Things we don't fill +(defun sx-question-mode--dont-fill-here () + "If text shouldn't be filled here, return t and skip over it." + (catch 'sx-question-mode-done + (let ((before (point))) + (skip-chars-forward "\r\n[:blank:]") + (let ((first-non-blank (point))) + (dolist (it '(sx-question-mode--skip-and-fontify-pre + sx-question-mode--skip-headline + sx-question-mode--skip-references + sx-question-mode--skip-comments)) + ;; If something worked, keep point where it is and return t. + (if (funcall it) (throw 'sx-question-mode-done t) + ;; Before calling each new function. Go back to the first + ;; non-blank char. + (goto-char first-non-blank))) + ;; If nothing matched, go back to the very beginning. + (goto-char before))))) + (defun sx-question-mode--skip-and-fontify-pre () "If there's a pre block ahead, handle it, skip it and return t. Handling means to turn it into a button and remove erroneous -- cgit v1.2.3 From 5eb81aebc2b5131fff7c4a4d33c163cec2cf3844 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 18 Dec 2014 18:07:04 -0200 Subject: Fix return values --- sx-question-print.el | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 996b057..c6176fc 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -431,7 +431,9 @@ If ID is nil, use FALLBACK-ID instead." ;; non-blank char. (goto-char first-non-blank))) ;; If nothing matched, go back to the very beginning. - (goto-char before))))) + (goto-char before) + ;; And return nil + nil)))) (defun sx-question-mode--skip-and-fontify-pre () "If there's a pre block ahead, handle it, skip it and return t. @@ -441,7 +443,8 @@ font-locking." ;; To identify code-blocks we need to be at start of line. (goto-char beg) (when (markdown-match-pre-blocks (line-end-position)) - (sx-babel--make-pre-button beg (point))))) + (sx-babel--make-pre-button beg (point)) + t))) (defun sx-question-mode--skip-comments () "If there's an html comment ahead, skip it and return t." -- cgit v1.2.3 From 3be41a8af1f98abfc8b2a43ce09c08586bee1075 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 18 Dec 2014 18:17:53 -0200 Subject: Fix score checking --- sx-question-print.el | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 6131684..d0802d6 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -290,10 +290,11 @@ The comment is indented, filled, and then printed according to (sx--wrap-in-overlay (list 'sx--data-here comment-data) (sx-assoc-let comment-data - (insert - (if (> .score 0) (number-to-string .score) "") - (if (eq .upvoted t) "^" "") - (if (or (> .score 0) .upvoted) " " "") + (when (> .score 0) + (insert (number-to-string .score) + (if (eq .upvoted t) "^" "") + " ")) + (insert (format sx-question-mode-comments-format (sx-question-mode--propertize-display-name .owner) -- cgit v1.2.3 From 767570b1d5d6511726609265ee61c26aa9fe0332 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Thu, 18 Dec 2014 22:31:43 -0200 Subject: The separator above body is no longer a section I got tired of having to hit `n` twice to move past the body. --- sx-question-print.el | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 9f37b10..f11449b 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -238,8 +238,7 @@ DATA can represent a question or an answer." ;; Body (insert "\n" (propertize sx-question-mode-separator - 'face 'sx-question-mode-header - 'sx-question-mode--section 4)) + 'face 'sx-question-mode-header)) (sx--wrap-in-overlay '(face sx-question-mode-content-face) (insert "\n" -- cgit v1.2.3 From a56f77ba8d990e0bf0333b5b93b9cdd1bf55b33f Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Sat, 20 Dec 2014 16:10:01 -0200 Subject: Handle utf-8 encoding the right way. --- sx-question-print.el | 3 --- sx-request.el | 19 ++++++++++++------- 2 files changed, 12 insertions(+), 10 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 2f07132..b58a84a 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -182,9 +182,6 @@ QUESTION must be a data structure returned by `json-read'." (mapc #'sx-question-mode--print-section .answers)) (insert "\n\n ") (insert-text-button "Write an Answer" :type 'sx-button-answer) - ;; Display weird chars correctly - (set-buffer-multibyte nil) - (set-buffer-multibyte t) ;; Go up (goto-char (point-min)) (sx-question-mode-next-section)) diff --git a/sx-request.el b/sx-request.el index 9f2ecdb..1031ea7 100644 --- a/sx-request.el +++ b/sx-request.el @@ -136,13 +136,18 @@ the main content of the response is returned." (delete-region (point-min) (point)) (buffer-string)))) (response-zipped-p (sx-encoding-gzipped-p data)) - (data (if (not response-zipped-p) data - (if (fboundp 'zlib-decompress-region) - (zlib-decompress-region (point-min) (point-max)) - (shell-command-on-region - (point-min) (point-max) - sx-request-unzip-program nil t)) - (buffer-string))) + (data + ;; Turn string of bytes into string of characters. See + ;; http://emacs.stackexchange.com/q/4100/50 + (decode-coding-string + (if (not response-zipped-p) data + (if (fboundp 'zlib-decompress-region) + (zlib-decompress-region (point-min) (point-max)) + (shell-command-on-region + (point-min) (point-max) + sx-request-unzip-program nil t)) + (buffer-string)) + 'utf-8 'nocopy)) ;; @TODO should use `condition-case' here -- set ;; RESPONSE to 'corrupt or something (response (with-demoted-errors "`json' error: %S" -- cgit v1.2.3 From 29946095d687a44224e30b21198b2c872233e8d3 Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Sat, 20 Dec 2014 16:19:35 -0200 Subject: Hotfix, don't barf on links we don't understand. These are usually not links. See the end of http://emacs.stackexchange.com/q/3727/50 --- sx-question-print.el | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index 2f07132..fe1e2b0 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -384,10 +384,11 @@ E.g.: (match-string-no-properties 3) text))) (full-text (match-string-no-properties 0))) - (replace-match "") - (sx-question-mode--insert-link - (if sx-question-mode-pretty-links text full-text) - url))))) + (when (stringp url) + (replace-match "") + (sx-question-mode--insert-link + (if sx-question-mode-pretty-links text full-text) + url)))))) (defun sx-question-mode--insert-link (text url) "Return a link propertized version of string TEXT. -- cgit v1.2.3 From e0f4f8e85e818f718dd8b04deed1b5d4c50541df Mon Sep 17 00:00:00 2001 From: Artur Malabarba Date: Sat, 20 Dec 2014 16:53:20 -0200 Subject: Hot fix #171. Check if .score is a number --- sx-question-print.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'sx-question-print.el') diff --git a/sx-question-print.el b/sx-question-print.el index fe1e2b0..98d3308 100644 --- a/sx-question-print.el +++ b/sx-question-print.el @@ -289,11 +289,11 @@ The comment is indented, filled, and then printed according to (sx--wrap-in-overlay (list 'sx--data-here comment-data) (sx-assoc-let comment-data - (when (> .score 0) + (when (and (numberp .score) (> .score 0)) (insert (number-to-string .score) (if (eq .upvoted t) "^" "") " ")) - (insert + (insert (format sx-question-mode-comments-format (sx-question-mode--propertize-display-name .owner) -- cgit v1.2.3