emacs-orgmode
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [PATCH] Fix moving cursor in org-set-tags-command


From: Kyle Meyer
Subject: Re: [PATCH] Fix moving cursor in org-set-tags-command
Date: Sat, 09 May 2020 01:44:12 +0000

Nicolas Goaziou writes:

> Matt Lundin <address@hidden> writes:
>
>> -    (when (save-excursion (skip-chars-backward "*") (bolp))
>> -      (forward-char))))
>> +    (and (looking-at " ")
>> +     (string-match "\\*+" (buffer-substring (point-at-bol) (point)))
>> +     (forward-char))))
>
> Please replace `and' with `when' if side-effects are involved.
>
> Also, note that
>
>   (save-excursion (skip-chars-backward "*") (bolp))
>
> is faster and more accurate than
>
>   (string-match "\\*+" (buffer-substring (point-at-bol) (point)))
>
> because the latter matches, e.g.,
>
>   ab*|c
>
> where "|" is point.

Ah, I didn't spot the inaccuracy that you point out, thinking about it
as though the regexp was anchored.  I think anchoring on both ends would
resolve the inaccuracy, though I agree with your preference for the
skip-chars-backward variant.

> Besides, I don't understand how this is related to empty headlines
> since, AFAICT, this part of code is supposed to fix the issue on empty
> headlines.

I've tried to capture the issues in the tests below.  The first added
check would fail before 450452de4 (and its replacement, 44ec473c1).  The
second check would fail with 44ec473c1, the third with both 450452de4
and 44ec473c1.  Matt's patch would get past the first three checks, but
fail with the last one, due to the issue you note.  All these checks
pass if the string-match call is anchored or replaced by the
skip-chars-backward form you suggest.

diff --git a/testing/lisp/test-org.el b/testing/lisp/test-org.el
index 7d420b599..29ac0a8f9 100644
--- a/testing/lisp/test-org.el
+++ b/testing/lisp/test-org.el
@@ -6184,7 +6184,47 @@ (ert-deftest test-org/set-tags-command ()
    (equal "* H1 :foo:\n* H2 :bar:"
          (org-test-with-temp-text "* H1    :foo:\n* H2    :bar:"
            (let ((org-tags-column 1)) (org-set-tags-command '(4)))
-           (buffer-string)))))
+           (buffer-string))))
+  ;; Point does not move with empty headline.
+  (should
+   (equal ":foo:"
+         (org-test-with-temp-text "* <point>"
+           (cl-letf (((symbol-function 'completing-read)
+                      (lambda (&rest args) ":foo:")))
+             (let ((org-use-fast-tag-selection nil)
+                   (org-tags-column 1))
+               (org-set-tags-command)))
+           (buffer-substring (point) (line-end-position)))))
+  ;; Point does not move at start of line.
+  (should
+   (equal "* H1 :foo:"
+         (org-test-with-temp-text "* H1"
+           (cl-letf (((symbol-function 'completing-read)
+                      (lambda (&rest args) ":foo:")))
+             (let ((org-use-fast-tag-selection nil)
+                   (org-tags-column 1))
+               (org-set-tags-command)))
+           (buffer-substring (point) (line-end-position)))))
+  ;; Point does not move when within *'s.
+  (should
+   (equal "* H1 :foo:"
+         (org-test-with-temp-text "*<point>* H1"
+           (cl-letf (((symbol-function 'completing-read)
+                      (lambda (&rest args) ":foo:")))
+             (let ((org-use-fast-tag-selection nil)
+                   (org-tags-column 1))
+               (org-set-tags-command)))
+           (buffer-substring (point) (line-end-position)))))
+  ;; Point workaround does not get fooled when looking at a space.
+  (should
+   (equal " b :foo:"
+         (org-test-with-temp-text "* a<point> b"
+           (cl-letf (((symbol-function 'completing-read)
+                      (lambda (&rest args) ":foo:")))
+             (let ((org-use-fast-tag-selection nil)
+                   (org-tags-column 1))
+               (org-set-tags-command)))
+           (buffer-substring (point) (line-end-position))))))
 
 (ert-deftest test-org/toggle-tag ()
   "Test `org-toggle-tag' specifications."



reply via email to

[Prev in Thread] Current Thread [Next in Thread]