unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* [PATCH] A program to overlay Elisp regular expressions in rx form
       [not found] <m1a6vi27kr.fsf.ref@yahoo.es>
@ 2020-11-15 11:16 ` Daniel Martín
  2020-11-15 11:21   ` tumashu
                     ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: Daniel Martín @ 2020-11-15 11:16 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1134 bytes --]


Understanding complex regular expressions in Emacs Lisp code (for
example, the ones in compile.el) is sometimes difficult with all the
backslashes, so I've written a program that, using the ELPA package
xr[1], enters a minor mode that overlays the rx form of any regular
expression string.  This idea came from macroexpand and how it helps
with understanding Lisp macros.

This program autoloads a new function: regexp-expand.  When the point is
in a program string and regexp-expand is invoked, Emacs enters a minor
mode that puts the buffer temporarily in read-only mode and shows the
regular expression in rx form as an overlay.  You can ask for help about
the rx notation by pressing e.  You can exit the minor mode and return
to the original text by pressing q.

Is there general interest in this kind of functionality as a free
program?  I don't know if this could be a new feature of the xr package
(most probably), or if it could be its own separate package.

A first version of this program is attached to this email, along with
some tests that demonstrate how it works.

Thanks.

[1] https://elpa.gnu.org/packages/xr.html


[-- Attachment #2: regexp-expand.el --]
[-- Type: application/emacs-lisp, Size: 8702 bytes --]

[-- Attachment #3: regexp-expand-test.el --]
[-- Type: application/emacs-lisp, Size: 5027 bytes --]

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

* Re:[PATCH] A program to overlay Elisp regular expressions in rx form
  2020-11-15 11:16 ` [PATCH] A program to overlay Elisp regular expressions in rx form Daniel Martín
@ 2020-11-15 11:21   ` tumashu
  2020-11-15 11:50   ` [PATCH] " Jean Louis
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 7+ messages in thread
From: tumashu @ 2020-11-15 11:21 UTC (permalink / raw)
  To: Daniel Martín ; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 1403 bytes --]

cool





--
发自我的网易邮箱手机智能版
<br/><br/><br/>


----- Original Message -----
From: "Daniel Martín" <mardani29@yahoo.es>
To: emacs-devel@gnu.org
Sent: Sun, 15 Nov 2020 12:16:52 +0100
Subject: [PATCH] A program to overlay Elisp regular expressions in rx form


Understanding complex regular expressions in Emacs Lisp code (for
example, the ones in compile.el) is sometimes difficult with all the
backslashes, so I've written a program that, using the ELPA package
xr[1], enters a minor mode that overlays the rx form of any regular
expression string.  This idea came from macroexpand and how it helps
with understanding Lisp macros.

This program autoloads a new function: regexp-expand.  When the point is
in a program string and regexp-expand is invoked, Emacs enters a minor
mode that puts the buffer temporarily in read-only mode and shows the
regular expression in rx form as an overlay.  You can ask for help about
the rx notation by pressing e.  You can exit the minor mode and return
to the original text by pressing q.

Is there general interest in this kind of functionality as a free
program?  I don't know if this could be a new feature of the xr package
(most probably), or if it could be its own separate package.

A first version of this program is attached to this email, along with
some tests that demonstrate how it works.

Thanks.

[1] https://elpa.gnu.org/packages/xr.html


[-- Attachment #2: Type: text/html, Size: 2617 bytes --]

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

* Re: [PATCH] A program to overlay Elisp regular expressions in rx form
  2020-11-15 11:16 ` [PATCH] A program to overlay Elisp regular expressions in rx form Daniel Martín
  2020-11-15 11:21   ` tumashu
@ 2020-11-15 11:50   ` Jean Louis
  2020-11-15 14:03   ` Clément Pit-Claudel
  2020-11-15 18:15   ` Drew Adams
  3 siblings, 0 replies; 7+ messages in thread
From: Jean Louis @ 2020-11-15 11:50 UTC (permalink / raw)
  To: Daniel Martín; +Cc: emacs-devel

* Daniel Martín <mardani29@yahoo.es> [2020-11-15 14:18]:
> 
> Understanding complex regular expressions in Emacs Lisp code (for
> example, the ones in compile.el) is sometimes difficult with all the
> backslashes, so I've written a program that, using the ELPA package
> xr[1], enters a minor mode that overlays the rx form of any regular
> expression string.  This idea came from macroexpand and how it helps
> with understanding Lisp macros.

That will be useful.

-- 
There are 38 messages yet in my incoming mailbox.



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

* Re: [PATCH] A program to overlay Elisp regular expressions in rx form
  2020-11-15 11:16 ` [PATCH] A program to overlay Elisp regular expressions in rx form Daniel Martín
  2020-11-15 11:21   ` tumashu
  2020-11-15 11:50   ` [PATCH] " Jean Louis
@ 2020-11-15 14:03   ` Clément Pit-Claudel
  2020-11-16 19:20     ` Daniel Martín
  2020-11-15 18:15   ` Drew Adams
  3 siblings, 1 reply; 7+ messages in thread
From: Clément Pit-Claudel @ 2020-11-15 14:03 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 787 bytes --]

On 11/15/20 6:16 AM, Daniel Martín wrote:
> Is there general interest in this kind of functionality as a free
> program?  I don't know if this could be a new feature of the xr package
> (most probably), or if it could be its own separate package.
This is very nice.  I have a version of this in my local Emacs config, but it's on by default and it doesn't put the buffer in read-only more: it uses an overlay to display the rx regexp instead of the string-based one instead.  I've attached the code if you want to have a look; what's missing is logic to automatically "unprettify" the regexp when the point is in it, to make edits easy.

I was originally planning to include this in the easy-escape package, but I haven't found the time to do it, so feel free to go ahead :)

Clément.

[-- Attachment #2: xr-prettify.el --]
[-- Type: text/x-emacs-lisp, Size: 3523 bytes --]

;;; xr-prettify.el --- Display regular expressions as xr forms

;; Copyright (C) 2018 Clément Pit--Claudel
;; Author: Clément Pit--Claudel <clement.pitclaudel@live.com>
;; Version: 0.1
;; Keywords: convenience, lisp, tools

;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with this program.  If not, see <http://www.gnu.org/licenses/>.

;;; Commentary:

;; `xr-prettify-minor-mode' uses syntax highlighting and display properties to make ELisp
;; regular expressions more readable.  More precisely, it replaces "complex
;; enough" regular expressions with an equivalent "rx" form.  The underlying
;; buffer text is not modified.
;;
;; For example, `xr-prettify` prettifies this:
;;   "\\(?:\\_<\\\\newc\\_>\\s-*\\)?"
;; into this (`^' indicates a different color):
;;   (opt symbol-start "\\newc" symbol-end (zero-or-more (syntax whitespace)))
;;
;; The appearance of the replacement can be customized using `xr-prettify-face'
;; (which see).
;;
;; Suggested setup:
;;   (add-hook 'lisp-mode-hook 'xr-prettify-minor-mode)

;;; Code:

(require 'font-lock)
(require 'xr)

(defgroup xr-prettify nil
  "Display regular expressions in `rx' form."
  :group 'programming)

;; FIXME reveal while typing

(defun xr-prettify--maybe-replace-string ()
  "Possibly display the string starting before (point) as an `rx' form."
  (save-excursion
    (let ((ppss-state (syntax-ppss (point))))
      (when (nth 3 ppss-state)
        (let* ((start (1- (point)))
               (eol (point-at-eol))
               (end (save-excursion
                      (parse-partial-sexp (point) (1+ eol) nil
                                          nil ppss-state 'syntax-table)
                      (point))))
          (when (and (<= end eol)
                     (search-forward "\\\\" end t))
            (let* ((txt (buffer-substring-no-properties start end))
                   (str (ignore-errors (read txt)))
                   (rx (and str (ignore-errors (xr str))))
                   (pretty (and rx (prin1-to-string `(rx ,rx)))))
              (when (and pretty (not (string= pretty txt)))
                (put-text-property start end 'display pretty)))))))
    nil))

(defconst xr-prettify--keywords
  '(("\"" (0 (progn (xr-prettify--maybe-replace-string) nil))))
  "Font-lock keyword list used internally.")

;;;###autoload
(define-minor-mode xr-prettify-minor-mode
  "Display regular expressions in `rx' form to improve readability.
When this mode is active, strings are displayed in `rx' syntax,
and highlighted with `xr-prettify-face'."
  :lighter " xr"
  :group 'xr-prettify
  (cond
   (xr-prettify-minor-mode
    (font-lock-add-keywords nil xr-prettify--keywords)
    (make-local-variable 'font-lock-extra-managed-props)
    (add-to-list 'font-lock-extra-managed-props 'display))
   (t (font-lock-remove-keywords nil xr-prettify--keywords)))
  (if (>= emacs-major-version 25) (font-lock-flush)
    (with-no-warnings (font-lock-fontify-buffer))))

(provide 'xr-prettify)
;;; xr-prettify.el ends here

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

* RE: [PATCH] A program to overlay Elisp regular expressions in rx form
  2020-11-15 11:16 ` [PATCH] A program to overlay Elisp regular expressions in rx form Daniel Martín
                     ` (2 preceding siblings ...)
  2020-11-15 14:03   ` Clément Pit-Claudel
@ 2020-11-15 18:15   ` Drew Adams
  2020-11-16 13:25     ` Daniel Martín
  3 siblings, 1 reply; 7+ messages in thread
From: Drew Adams @ 2020-11-15 18:15 UTC (permalink / raw)
  To: Daniel Martín, emacs-devel

> Understanding complex regular expressions in Emacs Lisp code (for
> example, the ones in compile.el) is sometimes difficult with all the
> backslashes, so I've written a program that, using the ELPA package
> xr[1], enters a minor mode that overlays the rx form of any regular
> expression string.  This idea came from macroexpand and how it helps
> with understanding Lisp macros.
> 
> This program autoloads a new function: regexp-expand.  When the point
> is
> in a program string and regexp-expand is invoked, Emacs enters a minor
> mode that puts the buffer temporarily in read-only mode and shows the
> regular expression in rx form as an overlay.  You can ask for help
> about
> the rx notation by pressing e.  You can exit the minor mode and return
> to the original text by pressing q.
> 
> Is there general interest in this kind of functionality as a free
> program?  I don't know if this could be a new feature of the xr package
> (most probably), or if it could be its own separate package.
> 
> A first version of this program is attached to this email, along with
> some tests that demonstrate how it works.

This kind of feature was discussed earlier (here, I think).
I'm glad to see it get implemented.  Thanks for working on
this.

Seeing a corresponding rx expression in an overlay helps.

I think it would also help to be able to get hold of that
rx expression, as usable text.

Think of `pp.el', for example.  You can show the output in
the echo area or in a separate buffer.  (Library `pp+.el'
lets you optionally show it in a tooltip.)

My point is that there are different use cases, including
(1) various ways to view the rx expression and (2) some
way to obtain, and make use of, its text.

Typically in Emacs (in the past), the gimme-the-plain-text
feature (i.e., #2) has come first.  It's the most useful,
I think.

Maybe you or Clement have already provided a way to
optionally show the text in a separate buffer (i.e., #2).
If so, great.  If not, maybe consider that as a possible
enhancement.
___


https://www.emacswiki.org/emacs/PpPlus

https://www.emacswiki.org/emacs/download/pp%2b.el




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

* Re: [PATCH] A program to overlay Elisp regular expressions in rx form
  2020-11-15 18:15   ` Drew Adams
@ 2020-11-16 13:25     ` Daniel Martín
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel Martín @ 2020-11-16 13:25 UTC (permalink / raw)
  To: Drew Adams; +Cc: emacs-devel

Drew Adams <drew.adams@oracle.com> writes:
>
> Maybe you or Clement have already provided a way to
> optionally show the text in a separate buffer (i.e., #2).
> If so, great.  If not, maybe consider that as a possible
> enhancement.

Showing the text in a separate buffer is not implemented yet.  I think
it's a good enhancement, users could switch between an overlay or a
separate buffer with C-u.  Thanks for the suggestion.



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

* Re: [PATCH] A program to overlay Elisp regular expressions in rx form
  2020-11-15 14:03   ` Clément Pit-Claudel
@ 2020-11-16 19:20     ` Daniel Martín
  0 siblings, 0 replies; 7+ messages in thread
From: Daniel Martín @ 2020-11-16 19:20 UTC (permalink / raw)
  To: Clément Pit-Claudel; +Cc: emacs-devel

Clément Pit-Claudel <cpitclaudel@gmail.com> writes:

> On 11/15/20 6:16 AM, Daniel Martín wrote:
>> Is there general interest in this kind of functionality as a free
>> program?  I don't know if this could be a new feature of the xr package
>> (most probably), or if it could be its own separate package.
> This is very nice.  I have a version of this in my local Emacs config,
> but it's on by default and it doesn't put the buffer in read-only
> more: it uses an overlay to display the rx regexp instead of the
> string-based one instead.  I've attached the code if you want to have
> a look; what's missing is logic to automatically "unprettify" the
> regexp when the point is in it, to make edits easy.
>
> I was originally planning to include this in the easy-escape package, but I haven't found the time to do it, so feel free to go ahead :)
>
> Clément.

Thanks for the link, your approach looks simpler, which is nice.  As you
said, the workflow is a little bit different: Your package prettifies
every regular expression at once and mine does so on a case by case
basis; I think it's a matter of preference.  I also prettify the final
rx form to make it more readable.

I didn't know about the easy-escape package.  I see it was suggested to
be included in Emacs core around 2017, but there were no responses back
then.  Maybe we could have this kind of functionality at least in ELPA?
I think it's a generally useful package.



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

end of thread, other threads:[~2020-11-16 19:20 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <m1a6vi27kr.fsf.ref@yahoo.es>
2020-11-15 11:16 ` [PATCH] A program to overlay Elisp regular expressions in rx form Daniel Martín
2020-11-15 11:21   ` tumashu
2020-11-15 11:50   ` [PATCH] " Jean Louis
2020-11-15 14:03   ` Clément Pit-Claudel
2020-11-16 19:20     ` Daniel Martín
2020-11-15 18:15   ` Drew Adams
2020-11-16 13:25     ` Daniel Martín

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).