Emacs Lisp: read-from-buffer versus read-from-minibuffer, returns string from editing

Emacs Lisp: read-from-buffer versus read-from-minibuffer, returns string from editing

As I am often editing database fields, and such fields could represent Org Mode or Markdown documents, this function is handy to quickly edit the string and return it back. It has no version control, thus I must take care what I am doing and how I am editing the string. As if something is lost, is lost forever. Unlike the read-from-minibuffer function, this one will open full editing buffer. If I switch the mode I have to use C-M-c option to return back.

I am editing PostgreSQL database fields within GNU Emacs by using this function. It would be used for any type of editing of strings, whenever necessary, and when read-from-minibuffer is simply not enough. I am welcoming improvements to this function.

(defun read-from-buffer (value &optional buffer-name)
  "Edits string and returns it"
  (let ((this-buffer (buffer-name))
    (new-value value)
    (buffy (if buffer-name buffer-name "*edit-string*")))
    (save-excursion
      (switch-to-buffer buffy)
      (set-buffer buffy)
      (text-mode)
      (local-set-key (kbd "C-c C-c") 'exit-recursive-edit)
      (if (stringp value) (insert value))
      (speak "You may quit the buffer with Control C Control C")
      (message "When you're done editing press C-c C-c or C-M-c to continue.")
      (unwind-protect
      (recursive-edit)
    (if (get-buffer-window buffy)
        (progn
          (setq new-value (buffer-substring (point-min) (point-max)))
          (kill-buffer buffy))))
      (switch-to-buffer this-buffer)
      new-value)))

Even this page was edited by using read-from-buffer function.

Related pages

Leave Your Comment or Contact GNU.Support

Contact GNU.Support now. There is a simple rule at GNU.Support: if we can help you, we do, whenever and wherever necessary, and it's the way we've been doing business since 2002, and the only way we know


Full name:


E-mail:


Message: