From: Jean Louis <bugs@gnu.support>
To: GNU Emacs Help <help-gnu-emacs@gnu.org>
Subject: How to reliably edit a file from within Emacs Lisp and return a string?
Date: Thu, 22 Aug 2019 23:31:03 +0200 [thread overview]
Message-ID: <20190822213103.GA26548@protected.rcdrun.com> (raw)
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))
(new-value value)
(buffy (if buffer-name buffer-name "*edit-string*")))
(save-excursion
(switch-to-buffer buffy)
(set-buffer buffy)
(text-mode)
(setq header-line-format "➜ 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 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)))
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))
(buffer (get-file-buffer file)))
(progn
(switch-to-buffer created)
(set-visited-file-name file)
(if (file-exists-p file)
(insert (file-to-string file)))
(message "Hit C-M-c when done")
;; (add-hook 'kill-buffer-hook 'exit-recursive-edit 0 t) ;; just thinking
;; (local-set-key (kbd "C-x k") 'exit-recursive-edit) ;; just thinking
;; (set-register 77 (recursive-edit)) ;; just thinking
(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
next reply other threads:[~2019-08-22 21:31 UTC|newest]
Thread overview: 39+ messages / expand[flat|nested] mbox.gz Atom feed top
2019-08-22 21:31 Jean Louis [this message]
2019-08-22 22:01 ` How to reliably edit a file from within Emacs Lisp and return a string? Noam Postavsky
2019-08-22 22:46 ` Jean Louis
2019-08-22 23:10 ` Óscar Fuentes
2019-08-22 23:22 ` Noam Postavsky
2019-08-23 1:19 ` Jean Louis
2019-08-23 1:27 ` Noam Postavsky
2019-08-23 7:49 ` Eli Zaretskii
2019-08-23 14:01 ` Jean Louis
2019-08-23 14:14 ` Robert Pluim
2019-08-24 12:20 ` Jean Louis
2019-08-24 12:24 ` Jean Louis
2019-08-23 14:25 ` Eli Zaretskii
2019-08-24 12:19 ` Jean Louis
2019-08-24 12:30 ` Eli Zaretskii
2019-08-24 12:41 ` Jean Louis
2019-08-24 13:17 ` Eli Zaretskii
2019-08-24 14:14 ` Jean Louis
2019-08-24 14:55 ` Jean Louis
2019-08-24 15:10 ` Eli Zaretskii
2019-08-24 15:51 ` Jean Louis
2019-08-24 16:11 ` Eli Zaretskii
2019-08-24 16:44 ` Jean Louis
2019-08-24 16:55 ` Eli Zaretskii
2019-08-24 17:02 ` Eli Zaretskii
2019-08-24 17:17 ` Jean Louis
2019-08-24 17:23 ` Eli Zaretskii
2019-08-24 17:37 ` Jean Louis
2019-08-24 18:25 ` Eli Zaretskii
2019-08-24 18:50 ` Jean Louis
2019-08-24 19:03 ` Eli Zaretskii
2019-08-24 21:30 ` Jean Louis
2019-08-24 17:15 ` Jean Louis
2019-08-24 17:22 ` Eli Zaretskii
2019-08-24 16:18 ` Yuri Khan
2019-08-24 17:07 ` Jean Louis
2019-08-24 13:08 ` Jean Louis
2019-08-23 21:44 ` Marcin Borkowski
2019-08-24 12:15 ` Jean Louis
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/emacs/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20190822213103.GA26548@protected.rcdrun.com \
--to=bugs@gnu.support \
--cc=help-gnu-emacs@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).