unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* master fb4f2aa038: * lisp/textmodes/paragraphs.el (repunctuate-sentences-filter): New function.
@ 2022-01-04 17:21 jakanakaevangeli
  2022-01-04 18:03 ` Juri Linkov
  0 siblings, 1 reply; 4+ messages in thread
From: jakanakaevangeli @ 2022-01-04 17:21 UTC (permalink / raw)
  To: juri; +Cc: emacs-devel

> +(defun repunctuate-sentences-filter (_start _end)
> +  "Search filter used by `repunctuate-sentences' to skip unneeded spaces.
> +By default, it skips occurrences that already have two spaces.
> +It is advised to put `advice-add' on this function to add more filters,
> +for example, `(looking-back (rx (or \"e.g.\" \"i.e.\") \" \") 5)'
> +with a set of predefined abbreviations to skip from adding two spaces."
> +  (not (length= (match-string 4) 2)))

Similarly to filter-buffer-substring-function, I believe it would be
better if this was a variable, on which 'add-function' could be used.
This would make it easier to set buffer locally if needed.



^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: master fb4f2aa038: * lisp/textmodes/paragraphs.el (repunctuate-sentences-filter): New function.
  2022-01-04 17:21 master fb4f2aa038: * lisp/textmodes/paragraphs.el (repunctuate-sentences-filter): New function jakanakaevangeli
@ 2022-01-04 18:03 ` Juri Linkov
  2022-01-04 20:44   ` Stefan Monnier
  0 siblings, 1 reply; 4+ messages in thread
From: Juri Linkov @ 2022-01-04 18:03 UTC (permalink / raw)
  To: jakanakaevangeli; +Cc: emacs-devel

>> +(defun repunctuate-sentences-filter (_start _end)
>> +  "Search filter used by `repunctuate-sentences' to skip unneeded spaces.
>> +By default, it skips occurrences that already have two spaces.
>> +It is advised to put `advice-add' on this function to add more filters,
>> +for example, `(looking-back (rx (or \"e.g.\" \"i.e.\") \" \") 5)'
>> +with a set of predefined abbreviations to skip from adding two spaces."
>> +  (not (length= (match-string 4) 2)))
>
> Similarly to filter-buffer-substring-function, I believe it would be
> better if this was a variable, on which 'add-function' could be used.
> This would make it easier to set buffer locally if needed.

Thanks, good idea.  Then, for example,

#+begin_src emacs-lisp
  (add-function :after-while (local 'repunctuate-sentences-filter)
                  (lambda (start end)
                    (not (looking-back (rx (or "e.g." "i.e.") " ") 5))))
#+end_src

will be possible with:

diff --git a/lisp/textmodes/paragraphs.el b/lisp/textmodes/paragraphs.el
index fc10a0dd1b..e3b5ecbcca 100644
--- a/lisp/textmodes/paragraphs.el
+++ b/lisp/textmodes/paragraphs.el
@@ -487,6 +487,8 @@ repunctuate-sentences-filter
 with a set of predefined abbreviations to skip from adding two spaces."
   (not (length= (match-string 4) 2)))
 
+(defvar repunctuate-sentences-filter #'repunctuate-sentences-filter)
+
 (defun repunctuate-sentences (&optional no-query start end)
   "Put two spaces at the end of sentences from point to the end of buffer.
 It works using `query-replace-regexp'.  In Transient Mark mode,
@@ -507,10 +509,10 @@ repunctuate-sentences
       (unwind-protect
           (progn
             (add-function :after-while isearch-filter-predicate
-                          #'repunctuate-sentences-filter)
+                          repunctuate-sentences-filter)
             (query-replace-regexp regexp to-string nil start end))
         (remove-function isearch-filter-predicate
-                         #'repunctuate-sentences-filter)))))
+                         repunctuate-sentences-filter)))))
 
--



^ permalink raw reply related	[flat|nested] 4+ messages in thread

* Re: master fb4f2aa038: * lisp/textmodes/paragraphs.el (repunctuate-sentences-filter): New function.
  2022-01-04 18:03 ` Juri Linkov
@ 2022-01-04 20:44   ` Stefan Monnier
  2022-01-05 18:47     ` Juri Linkov
  0 siblings, 1 reply; 4+ messages in thread
From: Stefan Monnier @ 2022-01-04 20:44 UTC (permalink / raw)
  To: Juri Linkov; +Cc: jakanakaevangeli, emacs-devel

> diff --git a/lisp/textmodes/paragraphs.el b/lisp/textmodes/paragraphs.el
> index fc10a0dd1b..e3b5ecbcca 100644
> --- a/lisp/textmodes/paragraphs.el
> +++ b/lisp/textmodes/paragraphs.el
> @@ -487,6 +487,8 @@ repunctuate-sentences-filter
>  with a set of predefined abbreviations to skip from adding two spaces."
>    (not (length= (match-string 4) 2)))
>  
> +(defvar repunctuate-sentences-filter #'repunctuate-sentences-filter)

Note that the default function (whose body is visible above) depends on
the match data set by the caller, so `add-function` risks messing it up
if it clobbers the match data with code that runs before this default.

At the very least, we need a docstring that makes it very clear what
match-data is expected to be set when the function is called and that
explains how/when it should be preserved.

Tho maybe a better approach is to change the calling convention so we
don't need to depend this way on the match data set by the caller.


        Stefan




^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: master fb4f2aa038: * lisp/textmodes/paragraphs.el (repunctuate-sentences-filter): New function.
  2022-01-04 20:44   ` Stefan Monnier
@ 2022-01-05 18:47     ` Juri Linkov
  0 siblings, 0 replies; 4+ messages in thread
From: Juri Linkov @ 2022-01-05 18:47 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: jakanakaevangeli, emacs-devel

>>    (not (length= (match-string 4) 2)))
>
> Note that the default function (whose body is visible above) depends on
> the match data set by the caller, so `add-function` risks messing it up
> if it clobbers the match data with code that runs before this default.
>
> At the very least, we need a docstring that makes it very clear what
> match-data is expected to be set when the function is called and that
> explains how/when it should be preserved.
>
> Tho maybe a better approach is to change the calling convention so we
> don't need to depend this way on the match data set by the caller.

Another reason not to use the match data is not to depend on
group numbering in the regexp such as (match-string 4).
So I replaced it with:

  (/= 2 (- (point) (save-excursion (skip-chars-backward " ") (point))))

Then there is no need to add a warning to the docstring because
query-replace-regexp is permissive in regard to interfering user regexps.



^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2022-01-05 18:47 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-01-04 17:21 master fb4f2aa038: * lisp/textmodes/paragraphs.el (repunctuate-sentences-filter): New function jakanakaevangeli
2022-01-04 18:03 ` Juri Linkov
2022-01-04 20:44   ` Stefan Monnier
2022-01-05 18:47     ` Juri Linkov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.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).