emacs-orgmode@gnu.org archives
 help / color / mirror / code / Atom feed
From: Kyle Meyer <kyle@kyleam.com>
To: Org Mode List <emacs-orgmode@gnu.org>
Cc: Matt Lundin <mdl@imapmail.org>
Subject: Re: [PATCH] Fix moving cursor in org-set-tags-command
Date: Sat, 09 May 2020 01:44:12 +0000	[thread overview]
Message-ID: <87zhahdgr7.fsf@kyleam.com> (raw)
In-Reply-To: <87ftca6ewc.fsf@nicolasgoaziou.fr>

Nicolas Goaziou writes:

> Matt Lundin <mdl@imapmail.org> 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	other threads:[~2020-05-09  1:44 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-05-08  0:20 [PATCH] Fix moving cursor in org-set-tags-command Matt Lundin
2020-05-08  2:42 ` Kyle Meyer
2020-05-08  7:53 ` Nicolas Goaziou
2020-05-09  1:44   ` Kyle Meyer [this message]
2020-05-09  8:12     ` Nicolas Goaziou
2020-05-09 20:01       ` Kyle Meyer
2020-05-10 17:39   ` Matthew Lundin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.orgmode.org/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zhahdgr7.fsf@kyleam.com \
    --to=kyle@kyleam.com \
    --cc=emacs-orgmode@gnu.org \
    --cc=mdl@imapmail.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).