1

I have this code:

(when (string-match "^[ ]*\\\\usepackage{url}" (buffer-string))
    (comment-region (match-beginning 0) (match-end 0)))

I tested it on these lines:

%% LaTeX file test
The quick brown fox jumps over the lazy dog.
\usepackage{url}
The quick brown fox jumps over the lazy dog.

And I obtained:

%% LaTeX file test
The quick brown fox jumps over the lazy dog.
%% \usepackage{url
}
The quick brown fox jumps over the lazy dog.

I corrected my code to:

  (when (string-match "^[ ]*\\\\usepackage{url}" (buffer-string))
    (comment-region (match-beginning 0) (+ (match-end 0) 1)))

and got:

%% LaTeX file test
The quick brown fox jumps over the lazy dog.
%% \usepackage{url}
The quick brown fox jumps over the lazy dog.

Is it correct to use (+ (match-end 0) 1)? Could there be any side effects?

PS I'm curious: Why doesn't the first code work as I expected?

1
  • 2
    @onner-irotsab: you're mixing functions intended for string usage with functions intended for buffer usage. If you want to change stuff in the buffer, use eg re-search-forward to find the text you want to modify, since then match-end will be using the 1-based indexing of the buffer
    – rpluim
    Commented Aug 11, 2023 at 10:00

1 Answer 1

2

Buffer position is counted from 1, whereas match-{beginning,end} uses 0-based index.

You can test:

(when (string-match "^[ ]*\\\\usepackage{url}" (buffer-string))
  (goto-char (match-beginning 0)))

The cursor will appear at the end of line.

I suggest you 1+ both:

(when (string-match "^[ ]*\\\\usepackage{url}" (buffer-string))
  (comment-region (1+ (match-beginning 0)) (1+ (match-end 0))))
11
  • 2
    Really they should be using re-search-forward and/or looking-at rather than extracting the whole buffer contents into a string....
    – rpluim
    Commented Aug 11, 2023 at 9:43
  • @rpluim: I just answered OP's question, and what function OP ultimately choose is up to himself. But indeed, you're right, but you should mention OP, otherwise he might miss your comment. Or, feel free to edit my answer to add this alternative :-)
    – shynur
    Commented Aug 11, 2023 at 9:53
  • You answered their question, but it was an x-y problem :-) (and for some reason I'm failing to get his handle to appear in my comment, maybe because it contains a '-')
    – rpluim
    Commented Aug 11, 2023 at 10:03
  • 1
    I usually use re-search-forward or looking-at, but I thought it was possible to work on the strings found by string-match. So I'll do as I always have, with re-search-forward or looking-at. Commented Aug 11, 2023 at 10:53
  • 1
    No, I mean that buffer positions count starting from 1, and string positions count starting from 0, so when you mix buffer functions with string functions you need to either add or subtract 1 in various places.
    – rpluim
    Commented Aug 11, 2023 at 11:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.