help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to reliably edit a file from within Emacs Lisp and return a stri


From: Noam Postavsky
Subject: Re: How to reliably edit a file from within Emacs Lisp and return a string?
Date: Thu, 22 Aug 2019 19:22:34 -0400

On Thu, 22 Aug 2019 at 18:46, Jean Louis <bugs@gnu.support> wrote:

> I cannot conclusively know and I do not know how to know it, from
> within Emacs Lisp, that a buffer has been killed.

Adding a function to kill-buffer-hook should work fine for that. But...

> Yes. I need to be able to run function that waits on the buffer to be
> killed, so that I can read string from the file that related to the
> buffer.

The difficulty is the waiting part. Traditionally, you would stuff the
rest of your code into a lambda callback, which means you can't have a
function like edit-and-return, because the flow needs to inverted. You
can have edit-and-do-with-resulting-string:

;;; -*- lexical-binding: t -*-
(defun call-with-edited-value (value function &optional buffer-name)
  "Edit VALUE and then call FUNCTION on it."
  (with-current-buffer (pop-to-buffer-same-window
                        (or buffer-name "*edit-string*"))
    (add-hook 'kill-buffer-hook
              (lambda () (funcall function (buffer-string))))
    (text-mode)
    (insert value)
    (setq header-line-format
          (substitute-command-keys
           "➜ Finish editing with \\[kill-buffer]"))
    (message "When you're done editing press %s to continue."
             (substitute-command-keys "\\[kill-buffer]"))))

;; Example call:
(call-with-edited-value
 "initial value"
 (lambda (v)
   (message "The value is now %S" v)))

It might be possible to use threads for waiting and get more linear
code that way, though there are still some rough spots around user
interaction from non-main threads, so it may also be somewhat tricky.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]