From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Philip Kaludercic Newsgroups: gmane.emacs.devel Subject: Re: New for GNU ELPA: literate-scratch Date: Sat, 15 Jun 2024 13:12:41 +0000 Message-ID: <87sexemjsm.fsf@posteo.net> References: <87ed96wtkr.fsf@melete.silentflame.com> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="35154"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Sean Whitton To: Sean Whitton via "Emacs development discussions." Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Sat Jun 15 15:13:21 2024 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 1sITDc-0008vW-32 for ged-emacs-devel@m.gmane-mx.org; Sat, 15 Jun 2024 15:13:20 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sITD6-0008Tm-DU; Sat, 15 Jun 2024 09:12:48 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sITD5-0008TP-27 for emacs-devel@gnu.org; Sat, 15 Jun 2024 09:12:47 -0400 Original-Received: from mout01.posteo.de ([185.67.36.65]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sITD2-0000oI-KJ for emacs-devel@gnu.org; Sat, 15 Jun 2024 09:12:46 -0400 Original-Received: from submission (posteo.de [185.67.36.169]) by mout01.posteo.de (Postfix) with ESMTPS id 0FDBF240027 for ; Sat, 15 Jun 2024 15:12:42 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1718457162; bh=Ltd0eeKSnzHOQu52Rov+adNAtIiEV5aMuPu+WbI2m9w=; h=From:To:Cc:Subject:OpenPGP:Date:Message-ID:MIME-Version: Content-Type:From; b=TO9vdSR0U6XhaBGVcBKbp2/VtKQMlXJAUPOroXN2zjDlUFqIsWAmYM46llFLQoU7x KPgr7OG/UPDq23GZ0eIjiLoONLaH1viVW5nIa2kXFZjtHfp3J40NweCT6RKkPCErOm ycIoS8Ib5JRKI0jXV4W4w7UoXx5z9JaLBRMXjA6r7Tpdy5i9AruWfi8ncBl+8sCkMM xTzLtiZwGKJgx8p7q5kMrNEQ1mLBMU6Bp4m70giV+dmaEPZZZ8v2epRZMYdBaPV4Q0 1zLhjHfETEz1mbwBX99kTZxHy8QMR4CdObPUJW9oACdD4j56ofLp7b8ZeaUIgjrt4V Ra1AhOWW+ZS/A== Original-Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4W1c554g3cz6tvy; Sat, 15 Jun 2024 15:12:41 +0200 (CEST) In-Reply-To: <87ed96wtkr.fsf@melete.silentflame.com> (Sean Whitton via's message of "Sun, 09 Jun 2024 13:53:08 +0800") OpenPGP: id=7126E1DE2F0CE35C770BED01F2C3CC513DB89F66; url="https://keys.openpgp.org/vks/v1/by-fingerprint/7126E1DE2F0CE35C770BED01F2C3CC513DB89F66"; preference=signencrypt Received-SPF: pass client-ip=185.67.36.65; envelope-from=philipk@posteo.net; helo=mout01.posteo.de X-Spam_score_int: -43 X-Spam_score: -4.4 X-Spam_bar: ---- X-Spam_report: (-4.4 / 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, RCVD_IN_DNSWL_MED=-2.3, RCVD_IN_MSPIKE_H3=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 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-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:320137 Archived-At: Sean Whitton via "Emacs development discussions." writes: > Hello, > > I'm planning to add this to GNU ELPA. Posting here for comments. > > Variant Lisp Interaction mode for easier interleaving of paragraphs of > plain text with Lisp code. This means you can have > > ;; This buffer is for text that is not saved ... > ;; To create a file, visit it with C-x C-f and ... > > (here is some Lisp) > > Here is a plain text paragraph )( including some unmatched parentheses. > Uh oh! Paredit won't like that. > > (here is some more Lisp) > > but (e.g.) Paredit won't complain about the unmatched parentheses. > Indeed, the whole plain text paragraph is font-locked as a comment. > > If you use Paredit but want to be able to use *scratch* for both Lisp > interaction and blocks of plain text, then this mode is for you. > Also compatible with the orgalist-mode and orgtbl-mode minor modes. > > To enable this mode after installing this file, simply customise > `initial-major-mode' to `literate-scratch-mode'. > > ;;; literate-scratch.el --- Lisp Interaction w/ text paragraphs -*- lexical-binding: t -*- > > ;; Copyright (C) 2023-2024 Free Software Foundation, Inc. > > ;; Author: Sean Whitton > ;; Maintainer: Sean Whitton > ;; Package-Requires: ((emacs "29.1")) > ;; Version: 1.0pre > ;; URL: https://git.spwhitton.name/dotfiles/tree/.emacs.d/site-lisp/literate-scratch.el > ;; Keywords: lisp text > > ;; This file 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 file 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 file. If not, see . > > ;;; Commentary: > > ;; Variant Lisp Interaction mode for easier interleaving of paragraphs of > ;; plain text with Lisp code. This means you can have > ;; > ;; ;; This buffer is for text that is not saved ... > ;; ;; To create a file, visit it with C-x C-f and ... > ;; > ;; (here is some Lisp) > ;; > ;; Here is a plain text paragraph )( including some unmatched parentheses. > ;; Uh oh! Paredit won't like that. > ;; > ;; (here is some more Lisp) > ;; > ;; but (e.g.) Paredit won't complain about the unmatched parentheses. > ;; Indeed, the whole plain text paragraph is font-locked as a comment. > ;; > ;; If you use Paredit but want to be able to use *scratch* for both Lisp > ;; interaction and blocks of plain text, then this mode is for you. > ;; Also compatible with the orgalist-mode and orgtbl-mode minor modes. > ;; > ;; To enable this mode after installing this file, simply customise > ;; `initial-major-mode' to `literate-scratch-mode'. > > ;;; News: > > ;; Ver 1.0 YYYY/MM/DD Sean Whitton > ;; Initial release. > > ;;; Code: > > (defun literate-scratch--extend (start end) > (save-excursion > (let ((res1 > (and (goto-char start) > (not (looking-at paragraph-separate)) > (and-let* ((new (car (bounds-of-thing-at-point 'paragraph)))) > (and (< new start) > (setq start new))))) You should be able to merge the `and-let*' and `and' like --8<---------------cut here---------------start------------->8--- (and-let* ((new (car (bounds-of-thing-at-point 'paragraph))) ((< new start))) (setq start new)) --8<---------------cut here---------------end--------------->8--- > (res2 > (and (goto-char end) > (not (looking-at paragraph-separate)) > (and-let* ((new (cdr (bounds-of-thing-at-point 'paragraph)))) > (and (> new end) > (setq end new)))))) > (and (or res1 res2) > (cons start end))))) > > (defun literate-scratch--propertize (start end) > (goto-char start) > (let ((start (1- start))) > (catch 'finish > (while t > (when-let* ((comment-start (nth 8 (syntax-ppss)))) Could `while-let' be useful here? > (put-text-property (1- (point)) (point) 'syntax-table > (eval-when-compile > (string-to-syntax "!"))) > (put-text-property comment-start (point) 'syntax-multiline t)) > (forward-paragraph 1) > (backward-paragraph 1) > (unless (> end (point) start) > (throw 'finish nil)) > (setq start (point)) > (unless > (save-excursion > (catch 'done > (while t > ;; Examine the syntax of the paragraph's first char. > ;; If it's whitespace, we need to check the previous > ;; paragraph, to handle multiple paragraphs within a defun. > (let ((syn > (char-syntax > (char-after > ;; (1+ point) unless at end-of-buf or on first line > ;; of a paragraph beginning right at beg-of-buf. > (and (not (eobp)) > (not (and (bobp) > (looking-at > "[[:space:]]*[^[:space:]\n]"))) You should be able to merge this into a single `looking-at' expression by matching against the beginning of a buffer (\\`). > (1+ (point))))))) > (cond ((bobp) (throw 'done (memq syn '(?\( ?<)))) > ((memq syn '(?\( ?<)) (throw 'done t)) > ((not (eq syn ?\s)) (throw 'done nil)))) > (backward-paragraph 1)))) > (put-text-property (point) (1+ (point)) 'syntax-table > (eval-when-compile > (string-to-syntax "!")))) > (forward-paragraph 1)))) > ;; Now also call the usual `syntax-propertize-function' for this mode. > (elisp-mode-syntax-propertize start end)) > > (define-derived-mode literate-scratch-mode lisp-interaction-mode > "Lisp Interaction" > "Variant `lisp-interaction-mode' designed for the *scratch* buffer. > > Paragraphs that don't start with `(' or `;' are treated as block comments. > This makes it easier to interleave paragraphs of plain text with Lisp. > > You can enable this mode by customizing the variable `initial-major-mode' > to `literate-scratch-mode'." > (add-hook 'syntax-propertize-extend-region-functions > #'syntax-propertize-multiline t t) > (add-hook 'syntax-propertize-extend-region-functions > #'literate-scratch--extend t t) > (setq-local syntax-propertize-function #'literate-scratch--propertize)) > > (provide 'literate-scratch) > > ;;; literate-scratch.el ends here Otherwise looks neat! -- Philip Kaludercic on peregrine