From b1911a30f250da09c737d5e68c644eff112a511a Mon Sep 17 00:00:00 2001 From: Lionel Henry Date: Mon, 23 Nov 2015 16:08:42 +0000 Subject: [PATCH] Prevent infloops on blanks travelling --- lisp/ess-r-syntax.el | 31 ++++++++++++++++++------------- test/generate-literate-cases | 2 +- test/literate/syntax.R | 27 +++++++++++++++++++++++++++ 3 files changed, 46 insertions(+), 14 deletions(-) create mode 100644 test/literate/syntax.R diff --git a/lisp/ess-r-syntax.el b/lisp/ess-r-syntax.el index 476001e4e..35d155766 100644 --- a/lisp/ess-r-syntax.el +++ b/lisp/ess-r-syntax.el @@ -189,12 +189,13 @@ return the prefix." into account." (when (ess-skip-blanks-backward-1) (prog1 t - (when newlines - (while (= (point) (line-beginning-position)) - (forward-line -1) - (goto-char (line-end-position)) - (ess-climb-comment) - (ess-skip-blanks-backward-1)))))) + (while (and newlines + (/= (point) (point-min)) + (= (point) (line-beginning-position))) + (forward-line -1) + (goto-char (line-end-position)) + (ess-climb-comment) + (ess-skip-blanks-backward-1))))) (defun ess-skip-blanks-backward-1 () (and (/= (point) (point-min)) @@ -203,13 +204,17 @@ into account." (defun ess-skip-blanks-forward (&optional newlines) "Skip blanks and newlines forward, taking end-of-line comments into account." - (when (skip-chars-forward " \t") - (prog1 t - (when newlines - (while (= (point) (ess-code-end-position)) - (forward-line) - (ess-back-to-indentation) - (skip-chars-forward " \t")))))) + (ess-any ((/= 0 (skip-chars-forward " \t"))) + ((ess-while (and newlines + (= (point) (ess-code-end-position)) + (when (ess-save-excursion-when-nil + ;; Handles corner cases such as point being on last line + (let ((orig-point (point))) + (forward-line) + (ess-back-to-indentation) + (> (point) orig-point))) + (skip-chars-forward " \t") + t)))))) (defun ess-jump-char (char) (ess-save-excursion-when-nil diff --git a/test/generate-literate-cases b/test/generate-literate-cases index e6123ede1..2262d8686 100755 --- a/test/generate-literate-cases +++ b/test/generate-literate-cases @@ -14,4 +14,4 @@ (mapc (lambda (file) (ess-ltest-R (expand-file-name file ess-literate-path))) - '("roxy.R" "code-fill.R" "misc.R")) + '("roxy.R" "code-fill.R" "misc.R" "syntax.R")) diff --git a/test/literate/syntax.R b/test/literate/syntax.R new file mode 100644 index 000000000..74ca46ed0 --- /dev/null +++ b/test/literate/syntax.R @@ -0,0 +1,27 @@ + +##### Blanks, Characters and Comments + +### 1 ---------------------------------------------------------------- + +text¶ +text + +##! (ess-skip-blanks-forward t) + +text +¶text + + +### 2 ---------------------------------------------------------------- + +text¶ # comment + +##! (when (not (ess-skip-blanks-forward t)) +##! (insert "failure")) + +text ¶# comment + +##> (when (ess-skip-blanks-forward t) +##> (insert "failure")) + +text ¶# comment