unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: "Clément Pit-Claudel" <cpitclaudel@gmail.com>
To: "Mattias Engdegård" <mattiase@acm.org>
Cc: emacs-devel@gnu.org
Subject: Re: [ELPA] New package: xr
Date: Sat, 2 Feb 2019 13:41:22 -0500	[thread overview]
Message-ID: <0b242242-e0c4-0054-f926-03b3c11c9775@gmail.com> (raw)
In-Reply-To: <932C8F08-EFFB-4019-B07F-F5D745FC44B9@acm.org>

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

On 02/02/2019 10.15, Mattias Engdegård wrote:
> Thank you, that's an interesting idea! I don't think I'll work on it right now, but why don't you give it a go, using xr as the backend?

I've attached an implementation and screenshots.  rx seems to work nicely!

The expansion issue is indeed a bit of a problem.  It's also why I don't use rx often, in fact.
Another issue is regular expressions built with concat, since the parts they are made of are typically not 
The last issue is that the code I posted doesn't reveal the underlying expression (i.e. doesn't remove the display property) when the point is on it, which would be needed for a robust implementation.

By the way, could forms like "[A-Za-z]" be converted to a more compact representation? At the moment xr produces (any "A-Z" "a-z"), but it seems that (any "A-Za-z") would work as well and be shorter.

Again, nice work!
Clément.

[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: xr-prettify.el --]
[-- Type: text/x-emacs-lisp; name="xr-prettify.el", 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

[-- Attachment #3: rx.png --]
[-- Type: image/png, Size: 74332 bytes --]

[-- Attachment #4: re.png --]
[-- Type: image/png, Size: 66925 bytes --]

  reply	other threads:[~2019-02-02 18:41 UTC|newest]

Thread overview: 56+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-02-01 14:09 [ELPA] New package: xr Mattias Engdegård
2019-02-01 15:25 ` Clément Pit-Claudel
2019-02-01 15:42   ` Eli Zaretskii
2019-02-01 18:39     ` Clément Pit-Claudel
2019-02-01 15:51   ` Mattias Engdegård
2019-02-01 18:41     ` Clément Pit-Claudel
2019-02-02  9:43       ` Mattias Engdegård
2019-02-02 15:15         ` Mattias Engdegård
2019-02-02 18:41           ` Clément Pit-Claudel [this message]
2019-02-03  9:37             ` Mattias Engdegård
2019-02-03 20:11             ` Juri Linkov
2019-02-03 21:04               ` Mattias Engdegård
2019-02-03 21:13               ` Clément Pit-Claudel
2019-02-05 16:15 ` Stefan Monnier
2019-02-05 22:37   ` Michael Heerdegen
2019-02-06  0:04     ` Mattias Engdegård
2019-02-05 23:00   ` Michael Heerdegen
2019-02-06  0:11     ` Stefan Monnier
2019-02-06  0:25       ` Noam Postavsky
2019-02-06  0:34         ` Michael Heerdegen
2019-02-06  1:36         ` Stefan Monnier
2019-02-06 12:43       ` Mattias Engdegård
2019-02-07  4:34         ` pcre2el Richard Stallman
2019-02-07  7:11           ` pcre2el John Wiegley
2019-02-07 14:54           ` pcre2el Stefan Monnier
2019-02-06  0:02   ` [ELPA] New package: xr Mattias Engdegård
2019-02-06  0:14     ` Stefan Monnier
2019-02-06  7:02   ` Richard Stallman
2019-02-06 15:39     ` Eli Zaretskii
2019-02-07  4:36       ` Richard Stallman
2019-02-07 14:37         ` Eli Zaretskii
2019-02-08  3:21           ` Richard Stallman
2019-02-08  3:50             ` Stefan Monnier
2019-02-08  7:18               ` Eli Zaretskii
2019-02-08 10:06                 ` Stephen Berman
2019-02-09  3:37                   ` Richard Stallman
2019-02-09  8:03                     ` Eli Zaretskii
2019-02-09  3:38                 ` Richard Stallman
2019-02-09  8:05                   ` Eli Zaretskii
2019-02-10  5:51                     ` Richard Stallman
2019-02-10 15:14                       ` Eli Zaretskii
2019-02-10  5:51                     ` Richard Stallman
2019-02-10 15:12                       ` Eli Zaretskii
2019-02-11  5:38                         ` Richard Stallman
2019-02-09  3:37               ` Richard Stallman
2019-02-27 15:06 ` Michael Heerdegen
2019-02-27 16:22   ` Mattias Engdegård
2019-02-27 17:09     ` Michael Heerdegen
2019-02-28 14:10       ` Mattias Engdegård
2019-02-28 14:34         ` Clément Pit-Claudel
2019-02-28 23:06         ` Stefan Monnier
2019-03-01 13:39           ` Mattias Engdegård
2019-03-01 13:51             ` Stefan Monnier
2019-03-01  5:29         ` Van L
2019-03-01 13:54           ` Stefan Monnier
2019-03-01 15:25         ` Michael Heerdegen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=0b242242-e0c4-0054-f926-03b3c11c9775@gmail.com \
    --to=cpitclaudel@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=mattiase@acm.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).