From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Noam Postavsky Newsgroups: gmane.emacs.help 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 Message-ID: References: <20190822213103.GA26548@protected.rcdrun.com> <20190822224604.GA10900@protected.rcdrun.com> Mime-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="23259"; mail-complaints-to="usenet@blaine.gmane.org" Cc: GNU Emacs Help To: Jean Louis Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Fri Aug 23 01:23:07 2019 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1i0wQ5-0005u1-Rf for geh-help-gnu-emacs@m.gmane.org; Fri, 23 Aug 2019 01:23:05 +0200 Original-Received: from localhost ([::1]:48736 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i0wQ4-0001qt-KG for geh-help-gnu-emacs@m.gmane.org; Thu, 22 Aug 2019 19:23:04 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:54747) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i0wPs-0001qa-Od for help-gnu-emacs@gnu.org; Thu, 22 Aug 2019 19:22:53 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i0wPr-0000zV-Qx for help-gnu-emacs@gnu.org; Thu, 22 Aug 2019 19:22:52 -0400 Original-Received: from mail-oi1-x232.google.com ([2607:f8b0:4864:20::232]:33852) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1i0wPr-0000zP-KQ for help-gnu-emacs@gnu.org; Thu, 22 Aug 2019 19:22:51 -0400 Original-Received: by mail-oi1-x232.google.com with SMTP id g128so5709272oib.1 for ; Thu, 22 Aug 2019 16:22:51 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:references:in-reply-to:from:date:message-id:subject:to :cc:content-transfer-encoding; bh=pf97Qes53K33zsiD9zWKx1AmbTiNYvGdnTBoji93ZkI=; b=cA9+qY/c5uMkXv2uQVP9Gla8+IQREU6nRSxsXupIJnuSAqzn7jJHIvHw/IOxWp6mdy 0WO0EEFI0sNTXB+1BWHfq2RgsBW4JpBtgQPdodL/Q0UlvjTnd82opJtbinUVOu2FIzON +m5zCuiulMqEZMHWEiCSRlYkQLi8S20+ocwDnzAYsGFpxBI0TE1E8dEkPCWfE7cBy8jK 9Ua8NIOPAu9k5QUkSh5H/9vB+LZMfQTEGLxB5Lw9JThbsnWJEib/ttLsmwfnctgWwvm2 J5XUFNYgFqjp7TgmdbulllQnNNk7gCt4mgRKp7oFU3tuK0hMCGmTv3I2NyoFAa7IRLL7 LskQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:cc:content-transfer-encoding; bh=pf97Qes53K33zsiD9zWKx1AmbTiNYvGdnTBoji93ZkI=; b=V4l0FW/G5TbBvTJkAMIc7LJZMv069DtkPW7HT536/z90mpGdkqhbLq6S4seNdAJi4X rx4a7SV4krIPsG5PEAxbQ0+c66KA6YQNq0IKOn8v4z01x6Zcq/EUit8KjWa9NS0fGpb+ QGsQDp+qmDtN8uZ5i9cVjproNYL9LrYpuCiSx1EjLBxce93vq9u6ymXF32NEB7hiQoV1 1N3+3LjCL4PsqxjKKhsPc6dWgLaf6FCgOcGp8Ve6fGiT6CQXyhstodWQpXmrwr9O4qT2 n7Nu/86GmsNfuDdMb4QuiVPnkj7ZeXhHFI7xrN4qtdbTEBVmVv+36JJr39RhO+9k9b9M g1ig== X-Gm-Message-State: APjAAAUmMa13H3H8S3Wcvy43qncfVriv6feFeNDLkn/BS2VBVt4stvFt ux31LUHFV+o6xHlthBDMegfriXfPQ8dXU1h5KUSvlC3I X-Google-Smtp-Source: APXvYqx1rgl3KW+UI2OlIXAHIxN1b7csAgTM9htinI60oXm8bx7KDPbY+cL4SU/4n5itFi2mJmo/t2qkWwvVABhZcPE= X-Received: by 2002:aca:c7d2:: with SMTP id x201mr1160422oif.69.1566516170743; Thu, 22 Aug 2019 16:22:50 -0700 (PDT) In-Reply-To: <20190822224604.GA10900@protected.rcdrun.com> X-detected-operating-system: by eggs.gnu.org: Genre and OS details not recognized. X-Received-From: 2607:f8b0:4864:20::232 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: "help-gnu-emacs" Xref: news.gmane.org gmane.emacs.help:121348 Archived-At: On Thu, 22 Aug 2019 at 18:46, Jean Louis 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 "=E2=9E=9C 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.