Function: string-edit

string-edit is an autoloaded and byte-compiled function defined in string-edit.el.gz.

Signature

(string-edit PROMPT STRING SUCCESS-CALLBACK &key ABORT-CALLBACK)

Documentation

Switch to a new buffer to edit STRING.

When the user finishes editing (with C-c C-c (string-edit-done)), SUCCESS-CALLBACK is called with the resulting string.

If the user aborts (with C-c C-k (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.

Also see read-string-from-buffer.

Probably introduced at or before Emacs version 29.1.

Source Code

;; Defined in /usr/src/emacs/lisp/textmodes/string-edit.el.gz
;;;###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.

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

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