all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
blob d3f614ca94c7fe55c3c89d993e14df40abcf0de9 4683 bytes (raw)
name: lisp/textmodes/string-edit.el 	 # note: path name is non-authoritative(*)

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
 
;;; string-edit.el --- editing long strings  -*- lexical-binding: t; -*-

;; Copyright (C) 2022 Free Software Foundation, Inc.

;; Maintainer: emacs-devel@gnu.org

;; This file is part of GNU Emacs.

;; GNU Emacs 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.

;; GNU Emacs 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 GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.

;;; Commentary:

;;; 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)

;;;###autoload
(cl-defun string-edit (prompt string success-callback
                              &key abort-callback)
  "Switch to a new buffer to edit STRING.
When the user finishes editing (with \\<string-edit-mode-map>\\[string-edit-done]), SUCCESS-CALLBACK
is called with the resulting string.

If the user aborts (with \\<string-edit-mode-map>\\[string-edit-abort]), ABORT-CALLBACK (if any) is
called with no parameters.

PROMPT will be inserted at the start of the buffer, but won't be
included in the resulting string.  If PROMPT is nil, no help text
will be inserted."
  (pop-to-buffer (generate-new-buffer "*edit string*")
                 '(display-buffer-below-selected
                   (window-height . (lambda (window)
                                      (fit-window-to-buffer window nil 10)))))
  (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))
  (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-mode-map>\\[string-edit-done] when you've finished editing or \\[string-edit-abort] to abort"))
  (message "%s" (substitute-command-keys
                 "Type \\<string-edit-mode-map>\\[string-edit-done] when you've finished editing")))

;;;###autoload
(defun read-string-from-buffer (prompt string)
  "Switch to a new buffer to edit STRING in a recursive edit.
The user finishes editing with \\<string-edit-mode-map>\\[string-edit-done], or aborts with \\<string-edit-mode-map>\\[string-edit-abort]).

PROMPT will be inserted at the start of the buffer, but won't be
included in the resulting string.  If nil, no prompt will be
inserted in the buffer."
  (string-edit
   prompt
   string
   (lambda (edited)
     (setq string edited)
     (exit-recursive-edit))
   :abort-callback (lambda ()
                     (exit-recursive-edit)
                     (error "Aborted edit")))
  (recursive-edit)
  string)

(defvar-keymap 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-done ()
  "Finish editing the string and call the callback function.
This will kill the current buffer."
  (interactive)
  (goto-char (point-min))
  ;; Skip past the help text.
  (when-let ((match (text-property-search-forward
                     'string-edit--prompt nil t)))
    (goto-char (prop-match-beginning match)))
  (let ((string (buffer-substring (point) (point-max)))
        (callback string-edit--success-callback))
    (quit-window 'kill)
    (funcall callback string)))

(defun string-edit-abort ()
  "Abort editing the current string."
  (interactive)
  (let ((callback string-edit--abort-callback))
    (quit-window 'kill)
    (when callback
      (funcall callback))))

(provide 'string-edit)

;;; string-edit.el ends here

debug log:

solving d3f614ca94 ...
found d3f614ca94 in https://yhetil.org/emacs/86ee13j348.fsf@mail.linkov.net/
found ab0b3b3bd7 in https://git.savannah.gnu.org/cgit/emacs.git
preparing index
index prepared:
100644 ab0b3b3bd7cede4310004bdf6b479e89b9338fac	lisp/textmodes/string-edit.el

applying [1/1] https://yhetil.org/emacs/86ee13j348.fsf@mail.linkov.net/
diff --git a/lisp/textmodes/string-edit.el b/lisp/textmodes/string-edit.el
index ab0b3b3bd7..d3f614ca94 100644

Checking patch lisp/textmodes/string-edit.el...
Applied patch lisp/textmodes/string-edit.el cleanly.

index at:
100644 d3f614ca94c7fe55c3c89d993e14df40abcf0de9	lisp/textmodes/string-edit.el

(*) Git path names are given by the tree(s) the blob belongs to.
    Blobs themselves have no identifier aside from the hash of its contents.^

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.