unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* How to reliably edit a file from within Emacs Lisp and return a string?
@ 2019-08-22 21:31 Jean Louis
  2019-08-22 22:01 ` Noam Postavsky
  0 siblings, 1 reply; 39+ messages in thread
From: Jean Louis @ 2019-08-22 21:31 UTC (permalink / raw)
  To: GNU Emacs Help

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



^ permalink raw reply	[flat|nested] 39+ messages in thread

end of thread, other threads:[~2019-08-24 21:30 UTC | newest]

Thread overview: 39+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2019-08-22 21:31 How to reliably edit a file from within Emacs Lisp and return a string? Jean Louis
2019-08-22 22:01 ` 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

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).