help-gnu-emacs
[Top][All Lists]
Advanced

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

Re: To switch state of line not working


From: Emanuel Berg
Subject: Re: To switch state of line not working
Date: Tue, 15 Jun 2021 03:07:39 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

Jean Louis wrote:

> 1. [ ] Something to do
> 2. [ ] More to do
> 3. [✔] And more
> 4. [✔] Even more.
>
> This function below is supposed to switch from [✔] to [ ] when cursor
> is on the line but it is not working. It is switching only from [ ] to
> [✔].  Does somebody sees why?
>
> (defun rcd-check (&optional check-in check-out)
>   (interactive)
>   (let* ((start (line-beginning-position))
>        (end (line-end-position))
>        (line (buffer-substring start end))
>        (check-in (or check-in (regexp-quote "[ ]")))
>        (check-out (or check-out (regexp-quote "[✔]"))))
>     (if (string-match check-in line)
>       (progn
>         (replace-line check-in check-out))
>       (if (string-match check-out line)
>         (progn
>           (replace-line check-out check-in))))))

name: "start" -> "beg" for nice align with "end".

first `if' - no need for `progn'.

second if - ditto, also here use `when' instead.

> (defun replace-line (from to)
>   (let* ((start (line-beginning-position))
>        (end (line-end-position))
>        (line (buffer-substring start end)))
>     (save-excursion
>       (replace-string from to nil start end))))

`replace-string' should be `search-forward' and
`replace-match'.

And you are not using "line"!

Also try `buffer-substring-no-properties' if the above
doesn't help.

-- 
underground experts united
https://dataswamp.org/~incal




reply via email to

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