From: F. Jason Park Date: Fri, 3 Nov 2023 23:07:20 +0000 (-0700) Subject: Really fix off-by-one in erc--get-inserted-msg-bounds X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=fb578ddfb25371621df6e300a98a1ea1463dd06b;p=emacs.git Really fix off-by-one in erc--get-inserted-msg-bounds * lisp/erc/erc.el (erc--get-inserted-msg-bounds): Account for `previous-single-property-change' returning a position adjacent to that with an actual changed value. The prior attempt at addressing this was insufficient. * test/lisp/erc/erc-tests.el (erc--get-inserted-msg-bounds): New test. ; * test/lisp/erc/resources/base/local-modules/second.eld: Timeout. ; * test/lisp/erc/resources/base/local-modules/third.eld: Timeout. --- diff --git a/lisp/erc/erc.el b/lisp/erc/erc.el index 0471ee0bbb8..a5457601223 100644 --- a/lisp/erc/erc.el +++ b/lisp/erc/erc.el @@ -3026,16 +3026,19 @@ stored value. Otherwise, return the stored value." "Return the bounds of a message in an ERC buffer. Return ONLY one side when the first arg is `end' or `beg'. With POINT, search from POINT instead of `point'." + ;; TODO add edebug spec. `(let* ((point ,(or point '(point))) (at-start-p (get-text-property point 'erc-msg))) (and-let* - (,@(and (member only '(nil 'beg)) + (,@(and (member only '(nil beg 'beg)) '((b (or (and at-start-p point) (and-let* ((p (previous-single-property-change point 'erc-msg))) - (if (= p (1- point)) p (1- p))))))) - ,@(and (member only '(nil 'end)) + (if (= p (1- point)) + (if (get-text-property p 'erc-msg) p (1- p)) + (1- p))))))) + ,@(and (member only '(nil end 'end)) '((e (1- (next-single-property-change (if at-start-p (1+ point) point) 'erc-msg nil erc-insert-marker)))))) diff --git a/test/lisp/erc/erc-tests.el b/test/lisp/erc/erc-tests.el index 1af087e7e31..916b394c8ff 100644 --- a/test/lisp/erc/erc-tests.el +++ b/test/lisp/erc/erc-tests.el @@ -1432,6 +1432,55 @@ (should-not calls)))))) +(ert-deftest erc--get-inserted-msg-bounds () + (erc-mode) + (erc--initialize-markers (point) nil) + (let ((parsed (make-erc-response :unparsed ":bob PRIVMSG #chan :hi" + :sender "bob" + :command "PRIVMSG" + :command-args (list "#chan" "hi") + :contents "hi")) + (erc--msg-prop-overrides '((erc-ts . 0)))) + (erc-display-message parsed nil (current-buffer) + (erc-format-privmessage "bob" "hi" nil t))) + (goto-char 3) + (should (looking-at " hi")) + (goto-char 11) + (should (looking-back " hi")) + + (ert-info ("Parameter `only' being `beg'") + (dolist (i (number-sequence 3 11)) + (goto-char i) + (ert-info ((format "At %d (%c)" i (char-after i))) + (should (= 3 (erc--get-inserted-msg-bounds 'beg))))) + + (ert-info ("Parameter `point'") + (dolist (i (number-sequence 3 11)) + (ert-info ((format "At %d (%c)" i (char-after i))) + (should (= 3 (erc--get-inserted-msg-bounds 'beg i))))))) + + (ert-info ("Parameter `only' being `end'") + (dolist (i (number-sequence 3 11)) + (goto-char i) + (ert-info ((format "At %d (%c)" i (char-after i))) + (should (= 11 (erc--get-inserted-msg-bounds 'end))))) + + (ert-info ("Parameter `point'") + (dolist (i (number-sequence 3 11)) + (ert-info ((format "At %d (%c)" i (char-after i))) + (should (= 11 (erc--get-inserted-msg-bounds 'end i))))))) + + (ert-info ("Parameter `only' being nil") + (dolist (i (number-sequence 3 11)) + (goto-char i) + (ert-info ((format "At %d (%c)" i (char-after i))) + (should (equal '(3 . 11) (erc--get-inserted-msg-bounds nil))))) + + (ert-info ("Parameter `point'") + (dolist (i (number-sequence 3 11)) + (ert-info ((format "At %d (%c)" i (char-after i))) + (should (equal '(3 . 11) (erc--get-inserted-msg-bounds nil i)))))))) + (ert-deftest erc--delete-inserted-message () (erc-mode) (erc--initialize-markers (point) nil) diff --git a/test/lisp/erc/resources/base/local-modules/second.eld b/test/lisp/erc/resources/base/local-modules/second.eld index a96103b2aa1..5823d63b874 100644 --- a/test/lisp/erc/resources/base/local-modules/second.eld +++ b/test/lisp/erc/resources/base/local-modules/second.eld @@ -41,7 +41,7 @@ (0.07 ":alice!~u@2fzfcku68ehqa.irc PRIVMSG #chan :bob: To you that know them not. This to my mother.") (0.00 ":bob!~u@2fzfcku68ehqa.irc PRIVMSG #chan :alice: Some enigma, some riddle: come, thy l'envoy; begin.")) -((quit 1 "QUIT :\2ERC\2") +((quit 10 "QUIT :\2ERC\2") (0.03 ":tester`!~u@u9iqi96sfwk9s.irc QUIT")) ((drop 0 DROP)) diff --git a/test/lisp/erc/resources/base/local-modules/third.eld b/test/lisp/erc/resources/base/local-modules/third.eld index 19bdd6efcce..e24825c3217 100644 --- a/test/lisp/erc/resources/base/local-modules/third.eld +++ b/test/lisp/erc/resources/base/local-modules/third.eld @@ -37,7 +37,7 @@ (0.00 ":alice!~u@2fzfcku68ehqa.irc PRIVMSG #chan :bob: No remedy, my lord, when walls are so wilful to hear without warning.") (0.01 ":bob!~u@2fzfcku68ehqa.irc PRIVMSG #chan :alice: Let our reciprocal vows be remembered. You have many opportunities to cut him off; if your will want not, time and place will be fruitfully offered. There is nothing done if he return the conqueror; then am I the prisoner, and his bed my gaol; from the loathed warmth whereof deliver me, and supply the place for your labor.")) -((quit 1 "QUIT :\2ERC\2") +((quit 10 "QUIT :\2ERC\2") (0.03 ":tester`!~u@u9iqi96sfwk9s.irc QUIT :Quit")) ((drop 0 DROP))