ambrevar/emacs: Overhaul Emacs string and stream functions.

master
Pierre Neidhardt 2021-01-16 11:53:20 +01:00
parent 416ec2eea8
commit 39af2ace95
1 changed files with 21 additions and 8 deletions

View File

@ -68,18 +68,31 @@ See `princ-emacs-buffer'."
`(with-current-buffer (get-buffer-create ,buffer-or-name)
(insert ,(princ-to-string thing)))))
(export-always 'emacs-buffer-stream)
(defun emacs-buffer-stream (buffer-or-name)
"Return Emacs' BUFFER-OR-NAME content as a stream."
(emacs-eval>
`(with-current-buffer (get-buffer-create ,buffer-or-name)
(buffer-string))))
(defun emacs-unescape (string)
"Since Emacsclient returns escaped strings, return the string in a form
understood by Common Lisp."
(if (<= 2 (length string))
(str:replace-all "\\n" (string #\newline)
(subseq string 1 (1- (length string))))
string))
(export-always 'emacs-buffer-string)
(defun emacs-buffer-string (buffer-or-name)
"Return Emacs' BUFFER-OR-NAME content as a string."
(emacs-$eval
(emacs-unescape
(emacs-$eval
`(with-current-buffer (get-buffer-create ,buffer-or-name)
(buffer-string))))
(buffer-substring-no-properties (point-min) (point-max) )))))
;; (export-always 'emacs-buffer-stream)
(defun emacs-buffer-stream (buffer-or-name)
"Return Emacs' BUFFER-OR-NAME content as a stream."
(make-string-input-stream (emacs-buffer-string buffer-or-name)))
(export-always 'with-emacs-buffer-stream)
(defmacro with-emacs-buffer-stream ((in buffer-or-name) &body body)
"Evaluate BODY with IN bound to an input stream of the Emacs' BUFFER-OR-NAME."
`(with-input-from-string (,in (emacs-buffer-string ,buffer-or-name))
,@body))
;; TODO: Make Emacs buffer output stream.