From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Nicholas Vollmer Newsgroups: gmane.emacs.bugs Subject: bug#71404: 30.0.50; [WISHLIST] string-edit-mode should allow changing major mode Date: Fri, 07 Jun 2024 22:27:58 -0400 Message-ID: <877cf0f9sh.fsf@parenthetic.dev> References: <87msnyax53.fsf@parenthetic.dev> <87bk4cf9yx.fsf@parenthetic.dev> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="19307"; mail-complaints-to="usenet@ciao.gmane.io" To: 71404@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat Jun 08 04:41:08 2024 Return-path: Envelope-to: geb-bug-gnu-emacs@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 1sFm0x-0004p2-Rd for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 08 Jun 2024 04:41:08 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sFm0e-000847-K2; Fri, 07 Jun 2024 22:40: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 1sFm0d-00083e-AU for bug-gnu-emacs@gnu.org; Fri, 07 Jun 2024 22:40:47 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sFm0d-0002kH-2X for bug-gnu-emacs@gnu.org; Fri, 07 Jun 2024 22:40:47 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1sFm0s-0004Rq-Gy for bug-gnu-emacs@gnu.org; Fri, 07 Jun 2024 22:41:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Nicholas Vollmer Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 08 Jun 2024 02:41:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 71404 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.171781444317032 (code B ref -1); Sat, 08 Jun 2024 02:41:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 8 Jun 2024 02:40:43 +0000 Original-Received: from localhost ([127.0.0.1]:58075 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sFm0Y-0004Qc-P1 for submit@debbugs.gnu.org; Fri, 07 Jun 2024 22:40:43 -0400 Original-Received: from lists.gnu.org ([209.51.188.17]:46018) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1sFm0V-0004Pz-Al for submit@debbugs.gnu.org; Fri, 07 Jun 2024 22:40:40 -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 1sFloP-0005mZ-5F for bug-gnu-emacs@gnu.org; Fri, 07 Jun 2024 22:28:09 -0400 Original-Received: from mta-09-4.privateemail.com ([198.54.127.118]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sFloN-0000is-4H for bug-gnu-emacs@gnu.org; Fri, 07 Jun 2024 22:28:08 -0400 Original-Received: from mta-09.privateemail.com (localhost [127.0.0.1]) by mta-09.privateemail.com (Postfix) with ESMTP id 6D30418000AD for ; Fri, 7 Jun 2024 22:28:05 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=parenthetic.dev; s=default; t=1717813685; bh=ZjYNdFRn1ljGYjDyKgoO3Oq2qZQZn2G9u/PB1Fm8txA=; h=From:To:Subject:In-Reply-To:References:Date:From; b=PAN5dh0qR8HOkS3/wcRGb+CJ1dnHQM07sgcCOIO6NS9YXMqhXC2wE8n74fVJxWBiX bjCy2SZ1ppUUxQYKIc4gKvr4d7MOruCURzVjnc65lR9q2J9LlFeaW8EwdOhalBvmp/ BP5UsDETaE0TYFWIzXphDmyLIzK6LjxYoMoxw3taCJXRPhBBlIt/SWaqufdtf6whhG GgzCumjODXW8R+/e42keEtQX7m5cVDWHwQQ0A9VoDjJGCVOTCS3KSHCwPOc4W4xhHR u2MtDn+D9M4JurXMjp3MhMwYMWS7QWXlPXWvT+XMWz3WwINnIOwvXgCZQBecDridGL n+hqlVzT8mI4g== Original-Received: from laptop (c-174-166-84-198.hsd1.nj.comcast.net [174.166.84.198]) by mta-09.privateemail.com (Postfix) with ESMTPA for ; Fri, 7 Jun 2024 22:28:05 -0400 (EDT) In-Reply-To: <87bk4cf9yx.fsf@parenthetic.dev> (Nicholas Vollmer's message of "Fri, 07 Jun 2024 22:24:06 -0400") X-Virus-Scanned: ClamAV using ClamSMTP Received-SPF: pass client-ip=198.54.127.118; envelope-from=nv@parenthetic.dev; helo=MTA-09-4.privateemail.com 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, RCVD_IN_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-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: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:286805 Archived-At: --=-=-= Content-Type: text/plain; format=flowed Nicholas Vollmer writes: > Nicholas Vollmer writes: > > Attached is a proof-of-concept patch which addresses bugs > #71404, #71405, and > #71406 I'll take care of that if/when the design is agreed upon. > > The patch rewrites string-edit-mode as a minor mode. > It utilizes an overlay for the edit prompt (to persist across > major mode > changes) and prevents the prompt separator from being deleted. > It persists the buffer-local editing data so that the major mode > can be changed > and the edit can still be completed. > It uses #'ignore as the default abort function if none is > provided. Sorry. Forgot to attach patch. Attached here. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-lisp-textmodes-string-edit.el-refactor-as-minor-mode.patch >From 10ae067b0590884c7df6854ca04a1863782c368c Mon Sep 17 00:00:00 2001 From: Nicholas Vollmer Date: Fri, 7 Jun 2024 22:11:16 -0400 Subject: [PATCH] lisp/textmodes/string-edit.el: refactor as minor mode --- lisp/textmodes/string-edit.el | 82 +++++++++++++++++++---------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/lisp/textmodes/string-edit.el b/lisp/textmodes/string-edit.el index 03be426ac25..0eeba50b8ed 100644 --- a/lisp/textmodes/string-edit.el +++ b/lisp/textmodes/string-edit.el @@ -23,20 +23,22 @@ ;;; Code: -(require 'cl-lib) - (defface string-edit-prompt '((t (:inherit font-lock-comment-face))) "Face used on `string-edit' help text." :group 'text :version "29.1") -(defvar string-edit--success-callback) -(defvar string-edit--abort-callback) +(defvar-local string-edit--success-callback nil) +(put 'string-edit--success-callback 'permanent-local t) +(defvar-local string-edit--abort-callback nil) +(put 'string-edit--abort-callback 'permanent-local t) +(defvar-local string-edit--prompt nil) +(put 'string-edit--prompt 'permanent-local t) + ;;;###autoload -(cl-defun string-edit (prompt string success-callback - &key abort-callback) +(defun string-edit (prompt string success-callback &optional abort-callback) "Switch to a new buffer to edit STRING. When the user finishes editing (with \\\\[string-edit-done]), SUCCESS-CALLBACK is called with the resulting string. @@ -50,36 +52,16 @@ string-edit Also see `read-string-from-buffer'." (with-current-buffer (generate-new-buffer "*edit string*") - (when prompt - (let ((inhibit-read-only t)) - (insert prompt) - (ensure-empty-lines 0) - (add-text-properties (point-min) (point) - (list 'intangible t - 'face 'string-edit-prompt - 'read-only t)) - (insert (propertize (make-separator-line) 'rear-nonsticky t)) - (add-text-properties (point-min) (point) - (list 'string-edit--prompt t)))) - (let ((start (point))) - (insert string) - (goto-char start)) - + (setq string-edit--prompt prompt + string-edit--success-callback success-callback + string-edit--abort-callback (or abort-callback #'ignore)) + (string-edit-mode) + (save-excursion (insert string)) ;; Use `fit-window-to-buffer' after the buffer is filled with text. (pop-to-buffer (current-buffer) '(display-buffer-below-selected (window-height . (lambda (window) (fit-window-to-buffer window nil 10))))) - - (set-buffer-modified-p nil) - (setq buffer-undo-list nil) - (string-edit-mode) - (setq-local string-edit--success-callback success-callback) - (when abort-callback - (setq-local string-edit--abort-callback abort-callback)) - (setq-local header-line-format - (substitute-command-keys - "Type \\\\[string-edit-done] when you've finished editing or \\[string-edit-abort] to abort")) (message "%s" (substitute-command-keys "Type \\\\[string-edit-done] when you've finished editing")))) @@ -99,9 +81,9 @@ read-string-from-buffer (lambda (edited) (setq string edited) (exit-recursive-edit)) - :abort-callback (lambda () - (exit-recursive-edit) - (error "Aborted edit"))) + (lambda () + (exit-recursive-edit) + (error "Aborted edit"))) (recursive-edit) string) @@ -109,9 +91,35 @@ string-edit-mode-map "C-c C-c" #'string-edit-done "C-c C-k" #'string-edit-abort) -(define-derived-mode string-edit-mode text-mode "String" - "Mode for editing strings." - :interactive nil) +(defun string-edit--prepare-buffer () + "Prepare `string-edit-mode' buffer." + (with-silent-modifications + (when string-edit--prompt + (insert (propertize " " 'intangible t 'read-only t)) + (let ((o (make-overlay (point-min) (point-max)))) + (overlay-put o 'display + (concat (propertize string-edit--prompt 'face 'string-edit-prompt) + "\n" + (propertize "\n" 0 1 'face '(:inherit separator-line :extend t)))) + (overlay-put o 'evaporate nil) + (insert (propertize "\n" 'string-edit--prompt t 'read-only t 'rear-nonsticky t 'intangible t 'front-sticky t))) + (setq string-edit--prompt nil)) + (setq header-line-format + (substitute-command-keys + "Type \\\\[string-edit-done] when you've finished editing or \\[string-edit-abort] to abort")) + (setq buffer-undo-list nil))) + +(define-minor-mode string-edit-mode + "Minor mode for editing strings in a dedicated buffer." + :lighter " string-edit" + (cond + (string-edit-mode + (add-hook 'after-change-major-mode-hook #'string-edit-mode nil t) + (put 'after-change-major-mode-hook 'permanent-local t) + (string-edit--prepare-buffer)) + (t + (remove-hook 'after-change-major-mode-hook #'string-edit-mode t) + (put 'after-change-major-mode-hook 'permanent-local nil)))) (defun string-edit-done () "Finish editing the string and call the callback function. -- 2.45.1 --=-=-=--