From: Fernando D. Mato Mira
Subject: Contexts
Date: 
Message-ID: <3818B9AE.9743FB5@iname.com>
This kind of stuff is done all the time, but I forgot where I saw some
nice general macros, so here's today's take at it:

(defmacro bind-context (var ac &body forms)
  (let (currp currval check access)
    (if (symbolp var)
        (setq currval var
              currp  (gensym))
      (setq currval (car var)
            currp   (or (cadr var) (gensym))))
    (if (symbolp ac)
        (setq access ac
              currp  t)
      (setq access (car ac)
            check  (or (cadr ac) t)))
    `(let* ((,currp ,check)
            (,currval (when ,currp ,access)))
         ,@forms)))

(defmacro preserving-context (var ac newval &body forms)
  (let ((prevp (gensym))
        (prevval (if (null var) (gensym) var))
        access restore check acargs)
    (if (symbolp ac)
        (setq access  ac
              restore `(setf ,ac nil)
              acargs  ac)
      (setq access  (car ac)
            restore (or (cadr ac) `(setf ,access nil))
            check   (caddr ac)
            acargs  (if check (list access check) access)))
     `(bind-context (,prevval ,prevp) ,acargs
        (setf ,access ,newval)
        (unwind-protect
            ,@forms
          (if ,prevp
             (setf ,access ,prevval)
            ,restore)))))


Example:

(preserving-context nil ((ext:search-list "clm:")
                         (ext:clear-search-list "clm:")
                         (ext:search-list-defined-p "clm:"))
                   (list (make-pathname :directory (pathname-directory
*load-pathname*))
                         #p"library:")
    (ext:load-foreign (list #p"clm:unixsocket.o"
                            #p"clm:io.o")
                      :libraries '("-lc" "-lm"))))

--
Fernando D. Mato Mira
Real-Time SW Eng & Networking
Advanced Systems Engineering Division
CSEM
Jaquet-Droz 1                   email: matomira AT acm DOT org
CH-2007 Neuchatel                 tel:       +41 (32) 720-5157
Switzerland                       FAX:       +41 (32) 720-5720

www.csem.ch      www.vrai.com     ligwww.epfl.ch/matomira.html
From: Marco Antoniotti
Subject: Re: Contexts
Date: 
Message-ID: <lwwvs6wk1v.fsf@parades.rm.cnr.it>
"Fernando D. Mato Mira" <········@iname.com> writes:

> This kind of stuff is done all the time, but I forgot where I saw some
> nice general macros, so here's today's take at it:
> 
> Example:
> 
> (preserving-context nil ((ext:search-list "clm:")
>                          (ext:clear-search-list "clm:")
>                          (ext:search-list-defined-p "clm:"))
>                    (list (make-pathname :directory (pathname-directory
> *load-pathname*))
>                          #p"library:")
>     (ext:load-foreign (list #p"clm:unixsocket.o"
>                             #p"clm:io.o")
>                       :libraries '("-lc" "-lm"))))

Ouch.  You touched a nerve.  I strongly believe that CMUCL search
lists are evil.  With all their problems, Logical Pathnames are the
way to go.

Cheers

-- 
Marco Antoniotti ===========================================
PARADES, Via San Pantaleo 66, I-00186 Rome, ITALY
tel. +39 - 06 68 10 03 17, fax. +39 - 06 68 80 79 26
http://www.parades.rm.cnr.it/~marcoxa