From 39af2ace956239cc77e180e3b0f6e25dd0afcdb0 Mon Sep 17 00:00:00 2001 From: Pierre Neidhardt Date: Sat, 16 Jan 2021 11:53:20 +0100 Subject: [PATCH] ambrevar/emacs: Overhaul Emacs string and stream functions. --- .../common-lisp/source/ambrevar/emacs.lisp | 29 ++++++++++++++----- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/.local/share/common-lisp/source/ambrevar/emacs.lisp b/.local/share/common-lisp/source/ambrevar/emacs.lisp index dd651cc7..6ba25643 100644 --- a/.local/share/common-lisp/source/ambrevar/emacs.lisp +++ b/.local/share/common-lisp/source/ambrevar/emacs.lisp @@ -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.