From 43fac1c2fa7f54251aee237bec97574d4412e73d Mon Sep 17 00:00:00 2001 From: Ilya Khaprov Date: Mon, 25 Nov 2013 20:48:47 -0800 Subject: [PATCH] enable/disable-heredoc-syntax macros --- src/heredoc.lisp | 32 ++++++++++++++++++++++++++++++++ src/package.lisp | 9 ++++++--- 2 files changed, 38 insertions(+), 3 deletions(-) diff --git a/src/heredoc.lisp b/src/heredoc.lisp index 8a9081d..18ab369 100644 --- a/src/heredoc.lisp +++ b/src/heredoc.lisp @@ -57,3 +57,35 @@ CL-USER> (set-dispatch-macro-character #\# #\> #'cl-heredoc:read-heredoc) CL-USER> #>eof>Write whatever (you) \"want\"!eof => Write whatever (you) \"want\"!" (declare (ignore arg)) (read-until-match stream (read-until-match stream (string char)))) + +(defvar *previous-readtables* nil + "A stack which holds the previous readtables that have been pushed +here by ENABLE-HEREDOC-SYNTAX.") + +(defun %enable-heredoc-syntax () + "Internal function used to enable reader syntax and store current +readtable on stack." + (push *readtable* + *previous-readtables*) + (setq *readtable* (copy-readtable)) + (set-dispatch-macro-character #\# #\> #'read-heredoc) + (values)) + +(defun %disable-heredoc-syntax () + "Internal function used to restore previous readtable." + (if *previous-readtables* + (setq *readtable* (pop *previous-readtables*)) + (setq *readtable* (copy-readtable nil))) + (values)) + +(defmacro enable-heredoc-syntax () + "Enable CL-HEREDOC reader syntax." + `(eval-when (:compile-toplevel :load-toplevel :execute) + (%enable-heredoc-syntax))) + +(defmacro disable-heredoc-syntax () + "Restore readtable which was active before last call to +ENABLE-HEREDOC-SYNTAX. If there was no such call, the standard +readtable is used." + `(eval-when (:compile-toplevel :load-toplevel :execute) + (%disable-heredoc-syntax))) diff --git a/src/package.lisp b/src/package.lisp index bb9447b..fe58576 100644 --- a/src/package.lisp +++ b/src/package.lisp @@ -16,6 +16,9 @@ (in-package :cl-heredoc-system) -(defpackage :cl-heredoc - (:use :cl) - (:export :read-heredoc :read-until-match)) +(defpackage #:cl-heredoc + (:use #:cl) + (:export #:read-heredoc + #:read-until-match + #:enable-heredoc-syntax + #:disable-heredoc-syntax))