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(+) 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(-) 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(+) 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(+) 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(-) 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(-) 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