From: "Clément Pit-Claudel" <cpitclaudel@gmail.com>
To: emacs-devel@gnu.org
Subject: Re: [PATCH] A program to overlay Elisp regular expressions in rx form
Date: Sun, 15 Nov 2020 09:03:51 -0500 [thread overview]
Message-ID: <c6f5718e-ab84-9307-6836-ce7dad7d3974@gmail.com> (raw)
In-Reply-To: <m1a6vi27kr.fsf@yahoo.es>
[-- 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
next prev parent reply other threads:[~2020-11-15 14:03 UTC|newest]
Thread overview: 7+ messages / expand[flat|nested] mbox.gz Atom feed top
[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 [this message]
2020-11-16 19:20 ` Daniel Martín
2020-11-15 18:15 ` Drew Adams
2020-11-16 13:25 ` Daniel Martín
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=c6f5718e-ab84-9307-6836-ce7dad7d3974@gmail.com \
--to=cpitclaudel@gmail.com \
--cc=emacs-devel@gnu.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).