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 MAJOR-MODE-SYM READ)

Documentation

Switch to a new buffer to edit STRING.

Call MAJOR-MODE-SYM (defaulting to string-edit-mode) to set up the new buffer, and insert PROMPT (defaulting to nothing) at the start of the buffer.

When the user finishes editing (with C-c C-c (string-edit-done)), call READ (defaulting to identity) on the resulting string, omitting PROMPT if any.

If READ returns without an error, quit the buffer and call SUCCESS-CALLBACK on the result.

If the user aborts (with C-c C-k (string-edit-abort)), call ABORT-CALLBACK (if any) with no parameters.

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 major-mode-sym read)
  "Switch to a new buffer to edit STRING.

Call MAJOR-MODE-SYM (defaulting to `string-edit-mode') to set up the new
buffer, and insert PROMPT (defaulting to nothing) at the start of the
buffer.

When the user finishes editing (with \\<string-edit-minor-mode-map>\\[string-edit-done]), call
READ (defaulting to `identity') on the resulting string, omitting PROMPT if any.

If READ returns without an error, quit the buffer and call
SUCCESS-CALLBACK on the result.

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

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)
                            'read-only t '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)
    (funcall (or major-mode-sym #'string-edit-mode))
    (string-edit-minor-mode)
    (setq-local string-edit--success-callback success-callback)
    (setq-local string-edit--abort-callback abort-callback)
    (setq-local string-edit--read read)
    (setq-local header-line-format
                (substitute-command-keys
                 "Type \\<string-edit-minor-mode-map>\\[string-edit-done] when you've finished editing or \\[string-edit-abort] to abort"))
    (message "%s" (substitute-command-keys
                   "Type \\<string-edit-minor-mode-map>\\[string-edit-done] when you've finished editing"))))