From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: =?UTF-8?Q?Cl=c3=a9ment_Pit-Claudel?= Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] A program to overlay Elisp regular expressions in rx form Date: Sun, 15 Nov 2020 09:03:51 -0500 Message-ID: References: Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------D9049E7D1F21851CB05FD0C9" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38166"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sun Nov 15 15:05:17 2020 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1keIea-0009m8-EE for ged-emacs-devel@m.gmane-mx.org; Sun, 15 Nov 2020 15:05:16 +0100 Original-Received: from localhost ([::1]:58498 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1keIeZ-00086Q-Fa for ged-emacs-devel@m.gmane-mx.org; Sun, 15 Nov 2020 09:05:15 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:49654) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1keIdM-0007D0-Mt for emacs-devel@gnu.org; Sun, 15 Nov 2020 09:04:02 -0500 Original-Received: from mail-qt1-x830.google.com ([2607:f8b0:4864:20::830]:45739) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1keIdH-0006xS-Ut for emacs-devel@gnu.org; Sun, 15 Nov 2020 09:04:00 -0500 Original-Received: by mail-qt1-x830.google.com with SMTP id v11so10783738qtq.12 for ; Sun, 15 Nov 2020 06:03:55 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=subject:to:references:from:message-id:date:user-agent:mime-version :in-reply-to:content-language; bh=jbD9fXvM09ZW6096lZ/ZpOBJ4MgIClrafgHLPRbsnj4=; b=ul7ZwD4p/DGiQKkfGr/24/xEey8Nao+YgHn1e3iQ21lQLfEhMn5xBvO7CterhilStb RvDVRMgnkdBxu+M8LvULCPjDLMlXWxu36SirSxRO5+IGjZ6NCnyiUHrpcTY2pyQCOnMR 84zj83QeAmzoxRKLn5BG1ngzGDQ6zTfmfJtGFeq3o7k4aXEJqqG85fMrsvSbiw3hlCAo ob/vbuwUyMZZcbipUnD/MbJXKsDkxQ2dCtzuK2eTqDUvqr1D31v8GgRujFt0x6Q9MgKY osuDSNF31BrL1AsT0OvpVG1wthDCMeMYMSV2O6NlJ4sMFxZsZl+7pSP/TxeEmKaCMctE ZCJw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:subject:to:references:from:message-id:date :user-agent:mime-version:in-reply-to:content-language; bh=jbD9fXvM09ZW6096lZ/ZpOBJ4MgIClrafgHLPRbsnj4=; b=Kkq8xI9DIKqLWxHNFEboEP8xLCbWVuTVh9/4+QuHe4GmBlLa7ktJn6pqmaiYGjtZtL Fh0Uhrx6J0Ia1BMjgunciNLizYAQAatkH7EkdCZM+JXX1Nx3jWns3/2lFSeZ0PonmKRD Cm8TgtnrYjaNWGx/zpXl5147zxL0JE8PJ1PMYMWwgs9OXi1nmmpZ2IrOHRPWKkVwO4RF qYmmx2frDSeEoofV2FlkuZQbidutE3VrExbcVoNnLcJHbM2JO6U+mA619KIfEOTrtwn7 PVWsYJpSk9t6hdcughIeZia3gBWNxEPWbRhnh1dUi1OK2hfdUXeqFLWKbl8UUZObKjcD 9H9g== X-Gm-Message-State: AOAM530ZU4ClQUjGFt2n36ycJvLbTf2XMPd4MQb6+Rb6efRjc5hWqUTG OBClQI/cg+ODxW8daJWRxNTQByfCbG4= X-Google-Smtp-Source: ABdhPJyhflsDrIn4tcYkTjRojKAJUg3I83IjFnJl3gVJkdSwvjxqxfAXEAwfM16II7Vyp8S/I5XA6w== X-Received: by 2002:ac8:7b30:: with SMTP id l16mr10421233qtu.360.1605449034283; Sun, 15 Nov 2020 06:03:54 -0800 (PST) Original-Received: from [192.168.1.3] (c-24-61-240-80.hsd1.ma.comcast.net. [24.61.240.80]) by smtp.googlemail.com with ESMTPSA id o125sm10253953qke.56.2020.11.15.06.03.52 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Nov 2020 06:03:52 -0800 (PST) In-Reply-To: Content-Language: en-GB Received-SPF: pass client-ip=2607:f8b0:4864:20::830; envelope-from=cpitclaudel@gmail.com; helo=mail-qt1-x830.google.com X-detected-operating-system: by eggs.gnu.org: No matching host in p0f cache. That's all we know. X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, NICE_REPLY_A=-0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:259199 Archived-At: This is a multi-part message in MIME format. --------------D9049E7D1F21851CB05FD0C9 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit 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. --------------D9049E7D1F21851CB05FD0C9 Content-Type: text/x-emacs-lisp; charset=UTF-8; name="xr-prettify.el" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="xr-prettify.el" ;;; xr-prettify.el --- Display regular expressions as xr forms ;; Copyright (C) 2018 Clément Pit--Claudel ;; Author: Clément Pit--Claudel ;; 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 . ;;; 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 --------------D9049E7D1F21851CB05FD0C9--