From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Jean Louis Newsgroups: gmane.emacs.help Subject: How to reliably edit a file from within Emacs Lisp and return a string? Date: Thu, 22 Aug 2019 23:31:03 +0200 Message-ID: <20190822213103.GA26548@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="102973"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mutt/1.10.1 (2018-07-13) To: GNU Emacs Help Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Aug 22 23:32:04 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 1i0uge-000Qd7-3N for geh-help-gnu-emacs@m.gmane.org; Thu, 22 Aug 2019 23:32:04 +0200 Original-Received: from localhost ([::1]:47996 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i0ugc-0001cZ-2N for geh-help-gnu-emacs@m.gmane.org; Thu, 22 Aug 2019 17:32:02 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:39544) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i0ugJ-0001cO-1Y for help-gnu-emacs@gnu.org; Thu, 22 Aug 2019 17:31:44 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i0ugH-0003yd-MW for help-gnu-emacs@gnu.org; Thu, 22 Aug 2019 17:31:42 -0400 Original-Received: from stw1.rcdrun.com ([217.170.207.13]:45251) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i0ugH-0003mX-Fp for help-gnu-emacs@gnu.org; Thu, 22 Aug 2019 17:31:41 -0400 Original-Received: from protected.rcdrun.com ([::ffff:31.223.149.45]) (AUTH: PLAIN admin, TLS: TLS1.2,256bits,ECDHE_RSA_AES_256_GCM_SHA384) by stw1.rcdrun.com with ESMTPSA id 00000000000320E9.000000005D5F0999.0000031C; Thu, 22 Aug 2019 14:31:05 -0700 Original-Received: from localhost (localhost [127.0.0.1]) (uid 1001) by protected.rcdrun.com with local id 00000000000C9452.000000005D5F0997.00006870; Thu, 22 Aug 2019 23:31:03 +0200 Content-Disposition: inline X-Mime-Autoconverted: from 8bit to quoted-printable by courier 1.0.6 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-Received-From: 217.170.207.13 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:121344 Archived-At: I need to reliably edit the file from within Emacs Lisp, and then return the string by reading the file. And I have difficulties in doing so. Purpose of it is to read database field values, and feed them back into the database. I have been doing that for years, reliably, because I was calling Emacs from outside of Emacs. For example, common lisp would wait for the Emacs to finish editing and then it would read the file as a string. Since I have switched to editing within Emacs, I am forced so far to use various tricks like (recursive-edit) and similar. I am forced to quit editing with C-c C-c or C-M-c which is all not reliable, as if mode chances, maybe C-c C-c changes, killing the buffer is also not conclusive for Emacs Lisp, basically from within Emacs Lisp, how can I know that find-file has finished its job, that buffer has been closed, so that I can read the string? Function like this one below is just an alternative, but it is simply not reliable, as too many things can happen with the buffer, and I better rely on temporary files, and not temporary buffers for database field value editing. (defun read-from-buffer (value &optional buffer-name) "Edits string and returns it" (let ((this-buffer (buffer-name)) =09(new-value value) =09(buffy (if buffer-name buffer-name "*edit-string*"))) (save-excursion (switch-to-buffer buffy) (set-buffer buffy) (text-mode) (setq header-line-format "=E2=9E=9C Finish editing with C-c C-c or = C-M-c") (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 contin= ue.") (unwind-protect =09 (recursive-edit) =09(if (get-buffer-window buffy) =09 (progn =09 (setq new-value (buffer-substring (point-min) (point-max))) =09 (kill-buffer buffy)))) (switch-to-buffer this-buffer) new-value))) The function like this one below is simply not perfect. The `a` will be set on the end only if I press C-M-c, while C-c C-c will mostly be changed as keybinding if I change the mode, like changing to Org mode, the C-c C-c is also changing. (defun edit-temp-file (file) "Edits temporary file and returns it as string" (let ((created (create-file-buffer file)) =09(buffer (get-file-buffer file))) (progn (switch-to-buffer created) (set-visited-file-name file) (if (file-exists-p file) =09(insert (file-to-string file))) (message "Hit C-M-c when done")=20 ;; (add-hook 'kill-buffer-hook 'exit-recursive-edit 0 t) ;; just thin= king ;; (local-set-key (kbd "C-x k") 'exit-recursive-edit) ;; just thin= king ;; (set-register 77 (recursive-edit)) ;; just thin= king=20 (recursive-edit) ;; (speak "Finished editing")) ;; This way I can know what happened (kill-buffer buffer) (file-to-string file))) (defun file-to-string (file) "File to string function" (with-temp-buffer (insert-file-contents file) (buffer-string))) (setq a (edit-temp-file "New1234.md")) What I really need is following: - to be able to provide file name to Emacs Lisp function - for Emacs Lisp to have it know with guarantee that buffer has been killed, if saved or not saved, is left to editing choice, - for Emacs Lisp to read the string after buffer has been killed To repeat again, to do that is extremely easy from outside programming language. I supply the file, edit it with emacs, and once finished, I read the file into string. My concept is of course following: (defun edit-temp-file (file) (find-file file) (file-to-string file)) But of course I am not getting the result. If anybody knows the solution, = let me know. Jean