;;; /info/www/ext/epa/mater/int/gen-html
;;; lfdb subsystem for HTML generation and revision

;;; -----------------------------------------------------
;;; Key functions for opening a file that is to be generated,
;;; choosing its name from the available specifications,
;;; and also opening the style file and invoking the generator

(defop 'chepar '(prog (fi)
  (setq fi 
      (open "/info/www/ext/epa/mater/int/gen-html.lisp" :direction :input))
 lop (setq a (read fi))
  (cond ((stringp a)(princ a))
        (t (princ (car a))))
  (terpri)
  (go lop)))

(defun instantiate (nm tf mode level)(insta2 nm (dirof tf) mode level))

(defun insta2 (n ti mode level)(insta3 n ti mode level nil))

(defun insta3 (n ti mode level norepeat)(prog (nam ptn tfi tfj extn xflag a)
;;; nm is the name of the style file, by default the name of the result
;;; tf is the file of the current object, which defines the directory
;;;   of the result
;;; mode is LATEX, HTML, etc
;;; level is |document|, |entity|, etc
  (princ "Enter insta3")(terpri)
  (setq nam n)
  (setq tfi ti)
  (setq ptn (case mode
     (txt   (getpattern *curag* nam "txt"   ".gtxt"))
     (html  (getpattern *curag* nam "html"  ".ght"))
     (latex (getpattern *curag* nam "latex" ".gtex")) ))
  (setq extn (case mode
                     (txt   "txt")
                     (html  "html")
                     (latex "tex"(break)
  (cond ((null ptn)
         (princ "Pattern not defined")(terpri)
         (return nil)))
L (cond ((listp (car ptn))(case (caar ptn)
           (eval (eval (cadar ptn))(return 'ok))
           (setname
             (princ "Interpreting: ")(princ (car ptn))(terpri)
             (setq nam (cadar ptn))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (prefix 
             (setq nam (concatenate 'string (cadar ptn) nam))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (postfix
             (setq nam (concatenate 'string nam (cadar ptn)))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (setdir
             (setq tfi (cadar ptn))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (concdir
             (setq tfi (concatenate 'string tfi 
                (string (getpa *curag* (cadar ptn) 'object)) "/"))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (concdir2
             (setq tfi (concatenate 'string tfi 
                (subseq (string (getpa *curag* (cadar ptn) 'object)) 2)
                  "/"))
             (princ tfi)(terpri)
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (refdir
             (setq tfi (getpa *curag* (cadar ptn)(caddar ptn)))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (dirname
             (setq tfi (concatenate 'string tfi nam "/"))
             (setq nam (cadar ptn))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (xtn
             (setq extn (cadar ptn))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (xflag (setq xflag t)
             (setq ptn (cdr ptn))
             (and ptn (go L)))
      )))
  (setq tfi (concatenate 'string tfi nam "." extn))
  (princ "* Generating: ")(princ tfi)(terpri)
  (cond ((and norepeat (open tfi :direction :probe))(return 'ok)))
  (setq tfj (open tfi :direction :output))
  (PrintElemHtml ptn (get *curag* level) tfj)
  (close tfj) 
  (cond ((or (isprefix "/info/www/ext/" tfi)(ishomedir tfi))
         (groupacc tfi)))
  (cond (xflag (system (concatenate 'string 
         "chmod a+x " tfi))))
  (return 'ok)
))

(defun getpattern (ag nam tool xtn)(prog (g gl)
  (setq gl (reverse (get ag 'levels)))
  (dolist (x gl)
    (setq g (getpatternlev (cadr x) ag nam tool xtn))
    (cond (g (return g))))
  (cond (g (return g)))
  (princ "No pattern file available")(terpri)))

(defun getpatternlev (lev ag nam tool xtn)(prog (ffi ffj)
  (setq ffi (getpa ag lev 'file))
  (cond ((null ffi)(return)))
  (setq ffi (concatenate 'string (dirofa ffi) tool "/" nam xtn))
  (setq ffj (open ffi :direction :probe))
  (cond (ffj (setq ffj (open ffi :direction :input))
             (setq ptn (read ffj))
             (close ffj)
             (return ptn)))
   )) 

(defop 'inher '(inher-fn (read-string)))
   ;; inher letters finds a file  ../html/letters.html in a
   ;; superordinate level, and copies it to the present
   ;; volume

(defun inher-fn (nam)(prog (oper)
   (dolist (lev (reverse (get *curag* 'levels)))
       (setq ffi (getpa *curag* (cadr lev) 'file))
       (cond (ffi
          (setq ffi (concatenate 'string (dirofa ffi) "html/" nam ".html"))
          (cond ((open ffi :direction :probe)
              (setq oper (concatenate 'string "cp " ffi " "
                 (dirof (getpa *curag* '|volume| 'file)) nam ".html"))
              (princ oper)(terpri) 
              (system oper)
              (return))))))))

;;; -----------------------------------------------------
;;; Top-level operations for *regenerating* html pages associated 
;;; with current office entity
;;; by taking level above and going for its ---.p.html page

(defop 'rghx '(rgh-fun *curag* (read) (read-string) ".html" T))
(defop 'rgh  '(rgh-fun *curag* (read) (read-string) ".html" nil))
(defop 'rgt  '(rgh-fun *curag* (read) (read-string) ".txt" nil))

(defop 'rghv-loop '(dolist 
    (v (getpa *curag* '|series| 'volumes))
    (chafunsub *curag* 'v '|volume| (car v))
    (rgh-fun *curag* 'v "indlis" ".html" nil)
    (rgh-fun *curag* 'v "indaut" ".html" nil)
        ))

(defop 'rgh-v '(rgh-fun *curag* 'v (read-string) ".html" nil))

(defun rgh-fun (ag i fi xtn expdirflg)(prog (j k)
  (setq j (getassoc (get ag 'levels) i))
  (setq k (getassoc *abovelevel* j))
  (setq inf (concatenate 'string (docdiry (get ag j))
         fi ".p" xtn)) 
  (setq outf (concatenate 'string 
        (cond (expdirflg (getpa ag j 'expdir))
              (T (docdiry (get ag j))))
         fi xtn))
  (cond ((null (open inf :direction :probe))
         (princ inf)(princ " - undefined file")(terpri)(return)))
  (CopyRest (open inf :direction :input)
            (open outf :direction :output)
            (get ag j)) 
  (cond ((or (isprefix "/info/www/ext/" outf)(ishomedir outf))
         (groupacc outf)))
  (return 'ok)))

(defop 'rghe  '(rghe-fna 
   ;; current entity has an xxx.p.html; generate its xxx.html
   (get *curag* '|entity|)
   (get *curag* '|collection|)))

;;; the following function obsolete, uncertain whether there are still
;;; any calls to it. Better to use rghe-fna which is *curag* independent.
(defun rghe-fn (e)(rghe-fna 
   (get *curag* '|entity|)
   (get *curag* '|collection|)))
     ;; here e is the symbol of the present entity

(defun rghe-fna (ent coll)(prog (j k)
     ;; here ent is the alist of the present entity
     ;; and coll similarly for the collection
  (setq fi (concatenate 'string (docdiry coll)
       (String (getassoc ent 'object))))
  (setq inf (concatenate 'string fi ".p.html"))
  (setq outf (concatenate 'string fi ".html"))
  (princ inf)(terpri)
  (cond ((null (open inf :direction :probe))
         (princ inf)(princ " - undefined file")(terpri)(return)))
  (CopyRest (open inf :direction :input)
            (open outf :direction :output)
            ent)
  (cond ((or (isprefix "/info/www/ext/" outf)(ishomedir outf))
         (groupacc outf)))
  (return 'ok)))

;;; -----------------------------------------------------
;;; Instantiate and regenerate for individual letter in a
;;; multi-letter collection

(defop 'inhl '(inhl-fn (string (read))
    (read-string)(get *curag* '|collection|)))
(defop 'inhll '(prog (name)
    (setq name (read-string))
    (dolist (let *alphabet*)
          (inhl-fn (string let) name (get *curag* '|collection|)))  ))

(defun inhl-fn (letter name obj)(prog (in out)
   (setq dir (docdiry obj))
   (setq in (open (concatenate 'string dir name ".p.html")
       :direction :input))
   (setq out (open (concatenate 'string dir letter "/" name ".p.html")
       :direction :output))
   (CopyRest in out (cons (list 'curlet letter) obj))))

(defop 'rghl '(rghl-fn (string (read))
    (read-string)(get *curag* '|collection|)))

(defop 'rghll '(prog (name)
    (setq name (read-string))
    (dolist (let *alphabet*)
          (rghl-fn (string let) name (get *curag* '|collection|)))  ))

(defun rghl-fn (letter name obj)(prog (in ou out)
   (princ letter)(terpri)
   (princ name)(terpri)
   (princ obj)(terpri)

   (setq dir (docdiry obj))
   (setq in (open (concatenate 'string dir letter "/" name ".p.html")
       :direction :input))
   (setq ou (concatenate 'string dir letter "/" name ".html"))
   (setq out (open ou :direction :output))
   (CopyRest in out (cons (list 'curlet letter) obj))
   (graccfile ou)))

;;; -----------------------------------------------------
;;; Top-level operations for *instantiating* html pages or latex
;;; files associated with current *document*

;;; Patterns have been set up for spai, generalized for system top level
;;; Also some in office witas
;;; Be careful not to disrupt previously existing index.html pages

(defop 'instl '(prog (nam)
  ;; instantiate latex for current document and set style
  (setq nam (read-string))
  (putpa *curag* '|document| 'opensty nam)
  (saveadl *curag* '|document|)
  (return (instl-fun nam))))

(defop 'instla '(prog (nam)
  ;; instantiate latex for current document for alternate style
  (setq nam (read-string))
  (putpa *curag* '|document| 'altsty nam)
  (return (instl-fun nam))))

(defop 'instd '(prog (nam)
  ;; instantiate latex for descr file for current document
  (return (instl-fun "descr")) ))

(defun instl-fun (nam)
   (insta2 nam 
       (dirof (getpa *curag* '|document| 'file))
       'latex '|document|))

(defop 'insth '(prog (tfi)
  ;; instantiate html for current document
  (setq *nam* (read-string))
  (setq tfi (getpa *curag* '|document| 'file))
  (cond ((null tfi)(princ "No document descriptor available")(terpri)))
  (putpa *curag* '|document| 'openstyh *nam*)
  (saveadl *curag* '|document|)
  (return (insth-fun *nam* tfi))))

(defop 'inst-h '(prog (tfi)
  ;; instantiate html for current document
  (setq *nam* (read-string))
  (setq tfi (getpa *curag* '|document| 'file))
  (cond ((null tfi)(princ "No document descriptor available")(terpri)))
  (return (insth-fun *nam* tfi))))

(defop 'inst-hr-fix '(prog (fi tfi)  ;;ignore this one
  ;; instantiate html for each document in current register
  (setq *nam* (read-string))
  (dolist (m (getpa *curag* '|register| 'documents))
      (princ (cadr m))(princ "  ")
      (chafun2 *curag* '|document| (intern (car m)) t)
      (cond ((null tfi)(princ "No document descriptor available")(terpri))
            (T (insth-fun *nam* (get *curag* '|document|)))))
  (return t)))

(defop 'inst-hr '(prog (fi obj)
  ;; instantiate html for each document in current register
  (setq *nam* (read-string))
  (dolist (m (getpa *curag* '|register| 'documents))
      (princ (cadr m))(princ "  ")
      (setq fi (concatenate 'string (cadr m) "descrip.adl"))
      (princ fi)(terpri)
      (setq obj (load-adl fi))
      (setf (get *curag* '|document|) obj)
      (princ (getassoc obj 'object))(terpri)
      (cond ((null obj)(princ "No document descriptor available")(terpri))
            (T (insth-fun *nam* (getassoc obj 'file)))))
  (return t)))

(defop 'rinst '(prog (tfi)
  ;; re-instantiate html for current document
  (setq tfi (getpa *curag* '|document| 'file))
  (cond ((null tfi)(princ "No document descriptor available")(terpri)))
  (return (insth-fun (getpa *curag* '|document| 'openstyh) tfi))))

(defun insth-fun (nm tf)(instantiate nm tf 'html '|document|))
(defun instt-fun (nm tf)(instantiate nm tf 'txt '|document|))

;;; -----------------------------------------------------
;;; Top-level operations for *instantiating* *html* pages or latlex
;;; files associated with current *office object* other than document

(defop 'insthe '(prog (tfi)
  ;; instantiate html descriptor file for current entity
  (setq tfi (getpa *curag* '|entity| 'file))
  (cond ((null tfi)(princ "No document descriptor available")(terpri)))
  (return (insthe-fun-old (read-string) tfi))))

(defop 'insthe* '(prog (obj tfi s dir)
  ;; instantiate html descriptor file for all entities in current collection
  (setq s (read-string))
  (setq dir (docdiry (get *curag* '|collection|)))
  (dolist (c (getpa *curag* '|collection| 'entities))
     (setq obj (load-adl (concatenate 'string dir (string (car c)) ".adl")))
     (setq tfi (getassoc obj 'file))
     (cond ((null tfi)(princ "No document descriptor available")(terpri)))
     (princ tfi)(terpri)(terpri)
     (setf (get *curag* '|entity|) obj)
     (insthe-fun-old s tfi)
     )))

(defun insthe-fun (pat tf)(insta2 
   (string (getpa *curag* '|entity| 'object))
   (dirofa tf)
   'html
   '|entity| ))

(defun insth-sfn (str type)
   (princ str)(princ " ")(princ type)(terpri)
   (insta2 str
     (dirof (getpa *curag* type 'file))
      'html  type ))

(defun insth-sfn-norepeat (str type)
   (princ str)(princ " ")(princ type)(terpri)
   (insta3 str
     (dirof (getpa *curag* type 'file))
      'html  type T))

(defun instl-sfn (str type)
   (princ str)(princ " ")(princ type)(terpri)
   (insta2 str
     (dirof (getpa *curag* type 'file))
      'latex type ))

(defun instl-sfn-norepeat (str type)
   (princ str)(princ " ")(princ type)(terpri)
   (insta3 str
     (dirof (getpa *curag* type 'file))
      'latex type T))

(defun instt-sfn (str type)
   (princ str)(princ " ")(princ type)(terpri)
   (insta2 str
     (dirof (getpa *curag* type 'file))
      'txt type ))

(defop 'insth-b  '(insth-sfn (read-string) '|base|))

(defop 'insth-c  '(insth-sfn (read-string) '|collection|))

(defop 'insth-r  '(insth-sfn (read-string) '|register|))

(defop 'insth-p  '(insth-sfn (read-string) '|publication|))

(defop 'instt-p  '(instt-sfn (read-string) '|publication|))

(defop 'insth-v  '(insth-sfn (read-string) '|volume|))

(defop 'insth-s  '(insth-sfn (read-string) '|series|))
  ;; instantiate an html page for the current series (journal, etc)
  ;; read name of recipe e.g.   jmain
  ;; obtained from /... base .../html/jmain.ght
  ;; note /... base .../ will end as e.g.  /.... /jr/

(defop 'instl-p  '(instl-sfn (read-string) '|publication|))

(defop 'inah-b '(prog (s)
  ;; do that for all series in current base
  ;; e.g.   inah-b jmain

  (setq s (read-string))
  (setq l (getpa *curag* '|base| 'series))
  (dolist (x l)
     (princ (car x))(terpri)
     (chafun2 *curag* '|series| '|base| (car x) nil)
     (insth-sfn s '|series|))
  (return 'ok)))

;;; -----------------------------------------------------
;;; Top-level operations for *editing definitions* for generating
;;; html pages associated with current office object

(defop 'edgh-b  '(edgh-fn (read-string) '|base|))
  ;; edit "generate HTML" page for current base and with given name 
  ;; used by the following operations
  ;; e.g.       edgh-b jmain
  ;; edits the procedure page   jmain.ght  in the appropriate place

(defop 'edgh-d  '(edgh-fn (read-string) '|desk|))
  ;; edit "generate HTML" page for current desk and with given name 
  ;; used by the following operations

(defun edgh-fn (str ind)(prog (ffi ffj ptn)
  (setq ffi (getpa *curag* ind 'file))
  (cond ((null ffi)(princ "No ")(princ ind)(princ " file available")))
  (setq ffi (concatenate 'string (dirof ffi) "html/" str ".ght"))
  (princ "File ffi: ")(princ ffi)(terpri)
  (system (concatenate 'string "emacs " ffi))
  (return 'ok)))


;;; -----------------------------------------------------
;;;  have to reconsider this one - it's not in much use
;;;    doesn't seem to fit in very well


(defop 'indesk '(prog (ffi ffj ptn tfi tfj)
;;;  instantiate an index.html page for the current desk
;;;  realized for spai and ntlr so far
;;;  shall probably be followed by use of   regd   command
;;;  for regenerating material between <label> and </label>
  (setq tfi (getpa *curag* '|desk| 'file))
  (cond ((null tfi)(princ "No desk descriptor available")(terpri)))
  (setq tfi (dirof tfi))
  (setq ffi (getpa *curag* '|office| 'file))
  (cond ((null ffi)(princ "No office available")))
  (setq ffi (concatenate 'string (dirof ffi) "html/index.dsk.ght"))
  (setq ffj (open ffi :direction :input))
  (cond ((null ffj)(princ "No pattern file available")(terpri)))
  (setq ptn (read ffj))
  (close ffj)
  (cond ((and
           (listp (car ptn))
           (equal (caar ptn) 'setname))
         (setq *nam* (cadar ptn))
         (setq ptn (cdr ptn)) ))
  (setq tfi (concatenate 'string tfi "index.html"))
  (setq tfj (open tfi :direction :output))
  (PrintElemHtml ptn (get *curag* '|desk|) tfj)
  (close tfj) 
  (return 'ok) 
))


;;; -----------------------------------------------------
;;; Auxiliary functions 
;;; Some auxiliary functions for the present file may be on
;;; file regen.

;;; -----------------------------------------------------
;;; Interpreter for script language

(setq *peh-stack* nil)


(defun PrintElemHtml (l obj outs)(and obj (prog ()
  (dolist (x l)(cond
    ((stringp x)(princ x outs))
    ((atom x)(princ (getassoc obj x) outs))
    (t (case (car x)
       (trace
         (prin1 obj)
         (terpri))
       (talk (PrintElemHtml (cdr x) obj nil))
       (setcur   ;; (setcur 'd '|ep-cis|) sets current agent to that
           (chafunsub *curag* (cadr x)
                (getassoc (get *curag* 'levels) (cadr x)) (caddr x)))
       (substring  (princ (subseq 
                (string (getassoc obj (cadr x)))
                (caddr x)(cadddr x)) outs))
       (inslash     ;; inslash object does 98065 -> 98/065
           (prog (a)(setq a (string (getassoc obj (cadr x))))
              (princ (subseq a 0 2) outs)
              (princ "/" outs)
              (princ (subseq a 2) outs)))
       (substringend  (prog (a n)
           (setq a (string (getassoc obj (cadr x))))
           (setq n (length a))
           (princ (subseq a (- n (caddr x)) (- n (cadddr x))) outs)                          ))
       (beforehyphen (prog (a n)
           (setq a (string (getassoc obj (cadr x))))
           (setq n (position '\#- a))
           (princ (subseq a 0 n) outs)))
       (filltwoplace (prog (a n)
           (setq a (string (getassoc obj (cadr x))))
           (cond ((= (length a) 1)(princ "0" outs)))
           (princ a outs)))
       (plus  (princ (+
           (cond ((numberp (cadr x))(cadr x))
                 (t (getassoc obj (cadr x))))
           (cond ((numberp (caddr x))(caddr x))
                 (t (getassoc obj (caddr x))))
            ) outs))
       (plus4  (princ (+
           (cond ((numberp (cadr x))(cadr x))
                 (t (getassoc obj (cadr x))))
           (cond ((numberp (caddr x))(caddr x))
                 (t (getassoc obj (caddr x))))
           (cond ((numberp (cadddr x))(cadddr x))
                 (t (getassoc obj (cadddr x))))
           (cond ((numberp (cadddr (cdr x))) (cadddr (cdr x)))
                 (t (getassoc obj (cadddr (cdr x)))))
            ) outs))
       (docdir (princ (docdiry obj) outs))
;       (docdir (prog (n l)
;         (setq n (getassoc obj (cadr x)))
;         (setq l (length n))
;         (princ (subseq n 0 (- l 11)) outs)
;           ))
       (globs (dolist (y (cdr x))(princ (eval y) outs)))
       (twolevel (terpri outs)
                 (princ "<expand>" outs)(terpri outs)(princ "(" outs)
                 (ExpandTwoLevel (cdr x) obj outs)
                 (terpri outs)(princ ")" outs)(terpri outs))

       (initheading (setq *iter* nil))
       (subheading (cond
          ((not (equal *iter* (getassoc obj (cadr x))))
           (setq *iter* (getassoc obj (cadr x)))
           (PrintElemHtml (cddr x) obj outs))))

       (insert (cond
          ((open (cadr x) :direction :probe)
           (CopyRestContinue (open (cadr x) :direction :input)
                             outs obj))
          (T (princ "Insert file can't be found")(terpri) nil)))

       (insert-descrip ;; (insert-descrip)
                       ;; inserts data on a robocup team
            (insertTeamData (get *curag* '|document|) outs))

       (OLDinsert-descrip ;; (insert-descrip)
                       ;; inserts a descrip.txt file of a robocup team
	               ;; and all its members addresses.
          (prog (fi)
            (setq fi (concatenate 'string
			(docdiry (get *curag* '|document|))
			"descrip.txt"))
            (cond ((open fi :direction :probe)
                   (PrintMetaData fi (open fi :direction :input)
                             outs))
                  (T (princ "Insert file 'descrip.txt' not found")(terpri) nil))))

       (insertrel   ;; (insert |document| abstract)
                    ;, inserts the file specified in ABSTRACT property
          (prog (fi)
            (setq fi (concatenate 'string
               (docdiry (get *curag* (cadr x)))
               (getpa *curag* (cadr x)(caddr x))))
            (cond ((open fi :direction :probe)
                   (CopyRestContinue (open fi :direction :input)
                             outs obj))
                  (T (princ "Insert file can't be found")(terpri) nil))))

       (insertsource ;; (insertsource "aml")
                     ;, inserts the source file in aml format
          (prog (fi)
            (setq fi (concatenate 'string
               (docdir) "body." (cadr x)))
            (cond ((open fi :direction :probe)
                   (CopyRestContinue (open fi :direction :input)
                             outs obj))
                  (T (princ "Insert file can't be found")(terpri) nil))))
       (insertfile ;; (insertfile "file.aml")
                     ;, inserts the file in aml format
          (prog (fi)
            (setq fi (concatenate 'string
               (docdir) (cadr x)))
            (cond ((open fi :direction :probe)
                   (CopyRestContinue (open fi :direction :input)
                             outs obj))
                  (T (princ "Insert file can't be found")(terpri) nil))))
       (insert_aml_h     ;; (insert_aml_h "file")
                         ;; inserts an aml file as html
          (prog (fi)
            (setq fi (concatenate 'string
               (docdir) (cadr x)))
	    (setq fi (filetrans fi "aml" "html"))
            (cond ((open fi :direction :probe)
                   (CopyRestContinue (open fi :direction :input)
                             outs obj))
                  (T (princ "Insert file can't be found")(terpri) nil))))
       (insert_latex_h   ;; (insert_latex_h "file")
                         ;; inserts a latex file as html
          (prog (fi)
            (setq fi (concatenate 'string
               (docdir) (cadr x) ".tex"))
;;;	    (setq fi (filetrans fi "tex" "html"))   ;;; Doesn't work for tex->html
            (cond ((open fi :direction :probe)
                   (CopyRestContinue (open fi :direction :input)
                             outs obj))
                  (T (princ "Insert file can't be found")(terpri) nil))))
       (insert-authors
          (prog (auth-list)
	     (setq auth-list (getassoc obj 'AUTHORS))
             (dolist (x auth-list)
	         (prog (y)
		     (setq y (cadr x))
		     (princ "\\author{ " outs)
		     (princ (atexstring (getassoc y 'FIRSTNAME)) outs)
                     (princ " " outs)
		     (princ (atexstring (getassoc y 'LASTNAME)) outs)
		     (princ "  \\\\" outs)
		     (terpri outs)
		     (princ "         email:" outs)
		     (princ (atexstring (getassoc y 'EMAIL)) outs)
		     (princ "  \\\\" outs)
		     (terpri outs)
		     (princ "         " outs)
		     (princ (atexstring (getassoc y 'AFFILIATION)) outs)
		     (princ " }" outs)
		     (terpri outs)))))

	
       ;;;; tdm oriented commands

       (agprop (princ (or (getpa *curag* (cadr x)(caddr x)) 
                          (getpa *curag* (cadr x)(cadddr x))) outs)
               )

       (aml2html   ;; property should be a string, or list of string,
                   ;;  print it with aml elimin
                   ;;  obs dubbla definitioner av denna !!!!!!

           (prog (v)
              (setq v (getassoc obj (cadr x)))
              (cond ((stringp v)
                     (princ (ahstring v) outs)
                     (return)))
           L  (princ (ahstring (car v)) outs)
              (cond ((cdr v)(terpri outs)(setq v (cdr v))(go l)))
               ))

       (aml--html   (amlscan (cond  
                          ((stringp (cadr x))(cadr x))
			  (T  (getassoc obj (cadr x))))
                       'html (list nil)(list nil) nil outs))

       (aml-html   ;; property should be a string, or list of string,
                   ;;  print it with aml elimin

           (prog (v)
              (setq v (getassoc obj (cadr x)))
              (cond ((stringp v)
                     (princ (ahstring v) outs)
                     (return)))
           L  (princ (ahstring (car v)) outs)
              (cond ((cdr v)(terpri outs)(setq v (cdr v))(go l)))
               ))

       (aml2tex   ;; property should be a string, or list of string,
                   ;;  print it with aml elimin

           (prog (v)
              (setq v (getassoc obj (cadr x)))
              (cond ((stringp v)
                     (princ (atexstring v) outs)
                     (return)))
           L  (princ (atexstring (car v)) outs)
              (cond ((cdr v)(terpri outs)(setq v (cdr v))(go l)))
               ))

       (aml2txt   ;; property should be a string, or list of string,
                   ;;  print it with aml elimin
           (prog (v)
              (setq v (getassoc obj (cadr x)))
              (cond ((stringp v)(princ (atstring v) outs))
                    (T (dolist (x v)(princ (atstring x) outs)
                                    (princ " " outs) )))))

       (aml2txtcr   ;; property should be a string, or list of strings,
                   ;;  print it with aml elimin and carriage returns
           (prog (v)
              (setq v (getassoc obj (cadr x)))
              (cond ((stringp v)(princ (atstring v) outs)(terpri outs))
                    (T  (princ (atstring (car v)) outs)
                        (dolist (xx (cdr v))
                                (terpri outs)(princ (caddr x) outs)
                                (princ (atstring xx) outs)
                         )))))

       (httpify   ;; (httpify summary) assumes property is /info/www/ext/...
                  ;; prints corresponding http://www.ida.liu.se/ext/...
           (princ "http://www.ida.liu.se/" outs)
           (princ (subseq (getassoc obj (cadr x)) 10) outs))

       (latexlist (dolist (y (cdr x))
                     (princ '#\\ outs)
                     (princ y outs)
                     (terpri outs)))
       (linearize  (prog (v)
           (setq v (getassoc obj (cadr x)))
           (cond ((listp v)(dolist (r v)
                      (princ r outs)
                      (or (equal r (car (last v)))
                          (terpri outs))))
                 (t (princ v outs)))))
       (linpunct (prog (v)
           (setq v (getassoc obj (cadr x)))
         lop (cond ((null v)(return)))
             (princ (car v) outs)
             (princ (cond ((cdr v) (caddr x))(t (cadddr x))) outs)
             (setq v (cdr v)) 
             (go lop)))
       (stringlist (dolist (y (cdr x))
                     (princ y outs)
                     (terpri outs)))
       (latexcoms (dolist (y (cdr x))
                     (princ '#\\ outs)
                     (princ (car y) outs)
                     (princ "{" outs)
                     (PrintElemHtml (cdr y) obj outs)
                     (princ "}" outs)
                     (terpri outs)))
       (aml2latex    (amlscan (cond  
                          ((stringp (cadr x))(cadr x))
			  (T  (or (getassoc obj (cadr x)) "")))
                       'tex (list nil)(list nil) nil outs))

       (remzeros     ;; (remzeros prop) prints two digits skipping beg zeros
                     ;; (remzeros (agprop |volume| ind)) similar
          (prog (s d)
            (setq s (cond
               ((stringp (cadr x))(cadr x))
               ((symbolp (cadr x))(or (getassoc obj (cadr x)) ""))
               (T (setq d (cadr x))
                  (case (car d)
                     (agprop (or (getpa *curag* (cadr d)(caddr d))
                                 (getpa *curag* (cadr d)(cadddr d))))))))
            (cond ((symbolp s)(setq s (string s))))
            (princ (remzeros s) outs)))

       (twoplace   (prog (a)
                       (setq a (getassoc obj (cadr x)))
                       (cond ((stringp a))(t (setq a (string a))))
                       (princ (subseq a (- (length a) 2)) outs)))

       (firsttwo   (prog (a)
                       (setq a (getassoc obj (cadr x)))
                       (cond ((stringp a))(t (setq a (string a))))
                       (princ (subseq a 0 2) outs)))

       (lasttwo   (prog (a)
                       (setq a (getassoc obj (cadr x)))
                       (cond ((stringp a))(t (setq a (string a))))
                       (princ (subseq a 2 4) outs)))

       (dotcond     ;; (dotcond title) prints a . on outs unless the
                    ;; title property ends with a ., ?, or !
              (dotcond-fn (getassoc obj (cadr x)) outs))

       (fillzeros   ;; (fillzeros 3 ind) obtains ind property and prints
                    ;; preceded by zeros if necessary to obtain 3 positions
                    (fillzeros (cadr x) (getassoc obj (caddr x)) outs))

       (raise      ;; (raise ind) print ind property but raises it first
                    (princ (string-upcase (string (getassoc obj (cadr x))))
                            outs))


       (acres        ;;; (acres oplist pubid pubid ... pubid)
                     ;;; prints all of those for inclusion in a table
                     ;;; separated by oplist
                      (princ (cddr x))(terpri)    
                      (dolist (y (cddr x))
                        (acres-print y outs)
                        (princ y)(princ " ")(acres-print y nil)(terpri)
                        (PrintElemHtml (cadr x) obj outs)))

       (pubid       (prog (v p w)   ;; print conference pub id
                       (setq v (subseq (string (getassoc obj (cadr x))) 3))
                       (setq w v)
                       (princ "c-" outs)
                       (setq p (position '#\/ v))
                       (cond ((null p)(return)))
                       (princ (subseq v 0 p) outs)
                       (princ "-" outs)
                       (setq v (subseq v (+ p 3)))
                       (princ (subseq v 0 2) outs)
                       (princ "-" outs)
                       (setq v (subseq v 3))
                       (cond ((isprefix "au" v)(setq v (subseq v 3))))
                       (setq p (position '#\/ v))
                       (cond ((or (null p)(= p 0)) (princ p) (princ " ")
                           (princ w)(princ " ")(princ v)(terpri)
                           (return)))
                       (setq v (subseq v 0 p))
                     lop (setq p (position '#\- v))
                       (cond (p (setf (elt v p) '#\_)(go lop)))
                       (princ v outs)
                    ))
                  ;;; to be generalized

       (jpubid       (prog (v p w)   ;; print journal pub id
                       (setq v (string (getassoc obj 'file)))
                       (setq v (subseq v 23))
                       (setq v (subseq v 0 (- (length v) 11)))
                       (princ ">> ")(princ v)(terpri)
                       (setq w v)
                       (princ "j-" outs)
                       (setq p (position '#\/ v))
                       (cond ((null p)(return)))
                       (princ (subseq v 0 p) outs)
                       (princ "-" outs)
                       (setq v (subseq v (+ p 1)))
                       (setq p (position '#\/ v))
                       (princ (remzeros (subseq v 0 p)) outs)
                       (princ "-" outs)
                       (setq v (subseq v (+ p 1)(- (length v) 1)))
                       (princ (remzeros v) outs)
                       (return)))
         
;                       (cond ((isprefix "au" v)(setq v (subseq v 3))))
;                       (setq p (position '#\/ v))
;                       (cond ((or (null p)(= p 0)) (princ p) (princ " ")
;                           (princ w)(princ " ")(princ v)(terpri)
;                           (return)))
;                       (setq v (subseq v 0 p))
;                     lop (setq p (position '#\- v))
;                       (cond (p (setf (elt v p) '#\_)(go lop)))
;                       (princ v outs)))
;                    

                  ;;; to be generalized
       (nth         (prog (s n)
                          (setq s (getassoc obj (cadr x)))
                          (cond ((null s)(return)))
                          (princ s outs)(princ "'" outs)
                          (setq s (concatenate 'string "0" s))
                          (setq n (length s))
                          (princ (case (elt s (- n 2))
                             (#\1 "th ")
                             (T (case (elt s (- n 1)) 
                                (#\1 "st ")
                                (#\2 "nd ")
                                (#\3 "rd ")
                                (T   "th "))))  outs)))

       (getauthor    ;; (getauthor lastname) looks up lastname
                     ;; property of author which is iauthor of
                     ;; present object
                     (prog (av)
                        (setq av (getassoc obj 'iauthor))
                        (cond ((null av)(return)))
                        (princ (getpa av '|authors| (cadr x)) outs)))

      (authorbibtex  ;; (authorbibtex) prints all authors in latex
                     ;; style and with ' and ' between author names
          (prog (v)
             (setq v (getassoc obj 'author))
             (cond ((atom v)(setq v (cons v (getassoc obj 'otherauthors)))))
             (authorfix-tex (car v) outs)
             (dolist (x (cdr v))
                (princ " and " outs)
                (authorfix-tex x outs))))

       (authorfix    ;; (authorfix author) allows author property
                     ;; to be a single author or a list of those,
                     ;; and prints them firstname_lastname
                     ;; (authorfix allauthors) takes otherauthors into acc
                     ;; (authorfix allauthors T) uses hot links
                     ;; (authorfix allauthors tex) latex style
                     ;; (authorfix allauthors latex) also puts a star
                     ;;    on the word if a www link exists 
                     ;; (authorfix allauthors txs) produces .txs
                     ;; (authorfix allauthors "main") uses target main
                     ;; Multiple flags allowed but string must be first
             (cond ((equal (cadr x) 'allauthors)
                    (authorfix2  
                       (getassoc obj 'author)
                       (getassoc obj 'otherauthors)
                       (cddr x)
                       outs))
                   (T  (princ (getassoc obj (cadr x)))(terpri)
                       (authorfix2 (getassoc obj (cadr x)) nil
                           (cddr x)
                           outs))))
       (authorvert   ;; like authorfix but prints them vertically with
                     ;; HTML separator in-between
             (cond ((equal (cadr x) 'allauthors)
                    (authorvert2  
                       (getassoc obj 'author)
                       (getassoc obj 'otherauthors)
                       (cddr x)
                       outs))
                   (T  (authorvert (getassoc obj (cadr x))
                           (cddr x)
                           outs))))

       (author-univ-rep  ;; print list of authors in format required
                        ;; by university reporting
              (author-univ-rep obj outs))

       (skipzeros    (prog (v)(setq v (getassoc obj (cadr x)))
                        (return (cond ((equal (elt v 0) "0")
                                       (subseq v 1)) (t v)))))
       (twolast      ;; (twolast year) prints last two digits in year
                     ;; (twolast (agprop |volume| ind)) similar
          (prog (s d)
            (setq s (cond
               ((atom (cadr x))(or (getassoc obj (cadr x)) "1997"))
               (T (setq d (cadr x))
                  (case (car d)
                     (agprop (or (getpa *curag* (cadr d)(caddr d))
                                 (getpa *curag* (cadr d)(cadddr d))))))))
            (cond ((symbolp s)(setq s (string s))))
            (princ (subseq s 2) outs)))


       (threelast      ;; (threelast year) prints last two digits in year
                     ;; (threelast (agprop |volume| ind)) similar
          (prog (s d)
            (setq s (cond
               ((atom (cadr x))(or (getassoc obj (cadr x)) "1997"))
               (T (setq d (cadr x))
                  (case (car d)
                     (agprop (or (getpa *curag* (cadr d)(caddr d))
                                 (getpa *curag* (cadr d)(cadddr d))))))))
            (cond ((symbolp s)(setq s (string s))))
            (princ (subseq s (- (length s) 3)) outs)))


       (fpacode      ;; (fpacode object) prints characters up to the
                     ;; first stop
          (prog (s p)
            (setq s (or (getassoc obj (cadr x)) "f-cis.linep.se"))
            (cond ((symbolp s)(setq s (string s))))
            (setq p (position '#\. s))
            (princ (subseq s 0 p) outs)))

       (twoyear      ;; (twoyear |register|) does like twolast but
                     ;; using year property of register of current agent
          (prog (s)
            (setq s (or (getpa *curag* (cadr x) 'year) "1997"))
            (princ (subseq s 2) outs)))

       (ntdlast      ;; (ntdlast) prints 97 if register is ntd97
                     ;; using year property of register of current agent
          (prog (s)
            (setq s (getpa *curag* '|register| 'object))
            (cond ((null s)(princ "Default year to 97")(terpri)
                   (setq s '|ntd97|)))
            (princ (subseq (string s) 3) outs)))

       (tryfile (prog (v fi)
          (setq fi (concatenate 'string (docdiry obj) (cadr x)))
          (setq v (open fi :direction :probe))
          (cond (v (princ "<li> <a href= " outs)
                   (princ '|"| outs)
                   (princ (cadr x) outs)
                   (princ '|"| outs)
                   (princ ">[" outs)
                   (princ (caddr x) outs)
                   (princ "]</a><p>" outs)(terpri outs)
                ))))

       (inclsty   ;; include a file containing a style expression
         (prog (fi)
           (setq fi (open (cadr x) :direction :input))
           (PrintElemHtml (read fi) obj outs)
           (close fi)))

       (incl    ;; include the file in present directory named as arg.html
          (prog (fi infi)
            (setq fi (getassoc obj 'file))
            (setq fi (subseq fi 0 (- (length fi) 11)))
            (setq infi (concatenate 'string fi (cadr x) ".html"))
            (setq infi (open infi :direction :input))
            (cond ((null infi)(princ "Included HTML file missing")
                         (terpri)(return)))
            (CopyRestContinue infi outs obj)))

       (inclref  ;; include the file in present directory locally named as 
                 ;; specified in the argument property

          (prog (fi infi)
            (setq fi (getassoc obj 'file))
            (setq fi (subseq fi 0 (- (length fi) 11)))
            (setq infi (concatenate 'string fi 
                 (getassoc obj (cadr x)) ".html"))
            (setq infi (open infi :direction :input))
            (cond ((null infi)(princ "Included HTML file missing")
                         (terpri)(return)))
            (CopyRestContinue infi outs obj)))

       (incgref  ;; include the file in present directory globally named as 
                 ;; specified in the argument property

          (prog (fi infi)
            (setq fi (getassoc obj (cadr x)))
            (setq infi (open fi :direction :input))
            (cond ((null infi)(princ "Included HTML file missing")
                         (terpri)(return)))
            (CopyRestContinue infi outs obj)))

       (incltex  ;; include the file in present directory named as arg.tex
          (prog (fi infi)
            (setq fi (getassoc obj 'file))
            (setq fi (subseq fi 0 (- (length fi) 11)))
            (setq infi (concatenate 'string fi (cadr x) ".tex"))
            (setq infi (open infi :direction :input))
            (cond ((null infi)(princ "Included TEX file missing")
                         (terpri)(return)))
            (CopyRestContinue infi outs obj)))

       (include    ;; include the file fully named in arg
          (prog (infi)
            (setq infi (open (cadr x) :direction :input))
            (cond ((null infi)(princ "Included HTML file missing")
                         (terpri)(return)))
            (CopyRestContinue infi outs obj)))

       (incaml   ;; include a whole aml file after translation
          (princ "incaml ")(princ x)(terpri)
          (prog (d fi v)
            (setq d (docdiry obj))
            (princ d)(terpri)
            (setq fi (concatenate 'string d (cadr x) ".aml"))
            (princ fi)(terpri)
            (setq v (open fi :direction :input))
            (princ v)(terpri)
            (and v (source2other 'aml 'html 
                      (append (cddr x)(list (list 'dir d))) v outs)) ))   


       (loopolist       ;; (loopolist ind op op ... op)
                        ;; picks up the ind property of the current object
                        ;; it should be a list of objects designated as
                        ;; full filenames. Loop over them, load the object,
                        ;; and apply the op's.
           (prog ()
               (dolist (y (getassoc obj (cadr x)))
                    (PrintElemHtml (cddr x)(load-adl y) outs))))

       (loopstrings     ;; (loopstrings ind op op ... op)
                        ;; picks up the ind property of the current object
                        ;; it should be a list of strings.
                        ;; Loop over it, construct an object of the form
                        ;;    ((STRING  "...")) 
                        ;; for each of them, and apply the op's.
           (prog ()
               (dolist (y (getassoc obj (cadr x)))
                    (PrintElemHtml (cddr x)
                               (list (list 'string y)) outs))))

       (loopover        ;; let *curag* loop on level indicated as first
                        ;; arg, and do the rest of the args as commands
                        ;; first arg given as e.g.  E for  |entity|
                  ;; this should be corrected: need to save & reset
                  ;; the state of the current agent
          (prog (i j k lc l)
            (setq i (cadr x))
            (setq j (getassoc (get *curag* 'levels) i))
            (setq k (getassoc *abovelevel* j))
            (setq lc (getpa *curag* 'levelcontents k))
            (setq l (getpa *curag* k lc))
            (dolist (c l)
                (chafunsub *curag* i j (car c))
                (PrintElemHtml (cddr x)
                    (get *curag* j) outs))))

       (looposort       ;; eg. (looposort |series| fullname op op ...)
                        ;; let *curag* loop on level indicated as first
                        ;; arg, but first sort them according to string
                        ;; comparison of the property given as second arg,
                        ;; Then like loopover
          (prog (n d l v)
            (setq n (list nil))
            (setq d (docdiry obj))
            (dolist (c (getassoc obj (cadr x)))
               (setq v (load-adl (concatenate 'string d 
                   (string (car c)) "/descrip.adl")))
               (nconc n (list v))
                    )
            (setq l (sort (cdr n) #'string-lessp :key
                     #'(lambda (xx)(string (getassoc xx (caddr x))))))
            (dolist (c l)
                (PrintElemHtml (cdddr x) c outs))))

       (loopdocs        ;; look up property of current document indicated
                        ;; by first argument, loop over it, and obtain
                        ;; sister documents with those names; execute rest
          (dolist (o (getassoc obj (cadr x)))
                  (princ (concatenate 'string
                           (docdiry (get *curag* '|register| 'file))
                           (string o) "/descrip.adl"))
                  (terpri)
                  (PrintElemHtml (cddr x)
                      (load-adl (concatenate 'string
                           (docdiry (get *curag* '|register| 'file))
                           (string o) "/descrip.adl"))
                      outs)))


       (loopaltdocs     ;; similar to previous one but the property
                        ;; is an alist binding name to fragment of filename
          (dolist (o (getassoc obj (cadr x)))
                  (princ (concatenate 'string
                           (docdiry (get *curag* '|register| 'file))
                           (string (car o)) "/descrip.adl"))
                  (terpri)
                  (PrintElemHtml (cddr x)
                    (cons
                       (list 'tag (car o))
                       (cons
                         (list 'ref-from (getassoc obj 'object))
                         (load-adl (concatenate 'string
                           "/info/www/ext/etai/" (cadr o)
                           "/descrip.adl"))))
                      outs)))

       (loopent         ;; let *curag* loop over entities of present coll,
                        ;; and do the rest of the args as commands
          (prog (i j k)
            (dolist (c (getassoc obj 'entities)) 
                (chafune *curag* (car c))
                (PrintElemHtml (cdr x)
                    (get *curag* '|entity|) outs))))

       (loopentd        ;; like loopent but members obtained by dir
           (prog (dir typ let lis)
              (setq dir (dirof (getpa *curag* '|collection| 'file)))
              (setq typ (getpa *curag* '|collection| 'object)) 
              (setq lis (getmemberdir dir nil ".adl"))
              (setq *dirlis* lis)
              (princ "Members of type: ")(princ typ)(terpri)
              (dolist (ent lis)
                   (setq obj (get ent typ))
                   (cond ((null obj)
                          (setq fi (concatenate 'string dir
                              (string ent) ".adl"))
                          (setq obj (load-adl-check fi))
                          (setf (get ent typ) obj)))
                   (or (equal ent '|descrip|)
                       (null obj)
                       (PrintElemHtml (cdr x) obj outs))
                   (and obj (princ (getassoc obj 'file)))(terpri)
                )))  

       (loopentdir      ;; like loopent but obtained from current
                 ;; object argument of this func, not from *curag*
           (prog (dir typ let lis ob)
              (setq dir (dirof (getassoc obj 'file)))
              (setq lis (getmemberdir dir nil ".adl"))
              (setq *dirlis* lis)
              (setq typ (or (getpa *curag* '|collection| 'object)
                            (getpa *curag* '|publication| 'object)))
              (dolist (ent lis)
                   (cond (T
                          (setq fi (concatenate 'string dir
                              (string ent) ".adl"))
                          (setq ob (load-adl fi))
                          (setf (get ent typ) ob)))
                   (or (equal ent '|descrip|)
                       (PrintElemHtml (cdr x) ob outs)))))

       (loopenti        ;; let *curag* loop over entities of present coll,
                        ;; and do the rest of the args as commands
          (prog (coll)
            (setq coll (getpa *curag* '|collection| 'object))
            (dolist (c (getpa *curag* '|collection| 'entities))
                (PrintElemHtml (cdr x)
                    (getdaba2 (car c) coll *curag*) outs))))

       (loopkind         ;; let *curag* loop over members of kind in 1st arg;
                        ;; and do the rest of the args as commands
          (prog (i j k)
            (dolist (c (or
                    (getpa (cadr x) '|collection| 'entities)
                    (getpa (cadr x) '|collection| 'volumes)))
                (PrintElemHtml (cddr x) 
                     (getdaba2 (car c)(cadr x) *curag*)  outs))))

       (looplist        ;; e.g. (looplist |eurolist| ...)
                        ;; loops over (current collection):LISTS:eurolist
                        ;; which should be a list of name - kind pairs

                        ;; e.g. (looplist (lists |eurolist|) ...)
                        ;; does the same but arbitrary first element

            (dolist (c (cond
                         ((listp (cadr x))
                           (getpa2 *curag* '|collection| (caadr x) (cadadr x)))
                         (T 
                           (getpa2 *curag* '|collection| 'lists (cadr x)))))
                (PrintElemHtml (cddr x)
                    (getdaba2 (car c)(cadr c) *curag*) outs)))

       (loopprop        ;; e.g. (loopprop tenants ...
                        ;; loops over (current collection):tenants
                        ;; which should be a list of name - kind pairs
            (dolist (c (getpa *curag* '|collection| (cadr x)))
                (PrintElemHtml (cddr x)
                    (getdaba2 (car c)(cadr c) *curag*) outs)))

       (loopalist       ;; e.g. (loopalist subfiles
                        ;; loops over (current object): subfiles
                        ;; which should be a list of tag - value  pairs
                        ;; bind them to subtag, subvalue

            (dolist (c (getassoc obj (cadr x)))
                (PrintElemHtml (cddr x)
                    (cons (list 'subtag (car c))
                          (cons (list 'subvalue (cadr c)) obj)) outs)))

;  Old definition (worry that someone still depends on it)
;            (dolist (c (getpa *curag* '|entity| (cadr x)))
;                (PrintElemHtml (cddr x)
;                    (cons (list 'subtag (car c))
;                          (cons (list 'subvalue (cadr c)) obj)) outs))

       (loopreg        ;; e.g. (loopreg ...)
                        ;; loops over (current register):documents
          (prog (v)
            (dolist (c (getpa *curag* '|register| 'documents))
              (setq v (load-adl (concatenate 'string (cadr c) "descrip.adl")))
              (princ (getassoc v 'object))(terpri)
              (PrintElemHtml (cdr x) v outs))))

       (loopdesk        ;; e.g. (loopdesk ...)
                        ;; loops over (current desk):registers
          (prog (v)
            (dolist (c (getpa *curag* '|desk| 'registers))
              (setq v (load-adl (concatenate 'string (cadr c) "descrip.adl")))
              (PrintElemHtml (cdr x) v outs))))

       (loopdocprop     ;; e.g. (loopregprop "/info/www/.... " letters ...)
                        ;; concatenates path with successive members
                        ;; of letters property, loads object, etc
          (prog (v)
            (dolist (c (getassoc obj (caddr x)))
              (princ "--> ")(princ c)(terpri)
              (setq v (load-adl (concatenate 'string (cadr x) c 
                             "/descrip.adl")))
              (PrintElemHtml (cdddr x) v outs))))

       (loopsubregs    ;; e.g. (loopsubregs (|1996| |1997|) ...)
                       ;; assumes current object to be a desk
                       ;; loops over those registers, and for them all
                       ;; the documents, and does the ...
          (princ "loopsubregs entered")(terpri)
          (prog (r v)
            (dolist (c (getpa *curag* '|desk| 'registers))
              (cond ((member (car c) (cadr x))
                     (setq r (load-adl (concatenate 'string (cadr c) 
                                        "descrip.adl")))
                     (dolist (d (getassoc r 'documents))
                       (setq v (cons (list 'year (car c))
                                     (load-adl (concatenate 'string 
                                                (cadr d) "descrip.adl"))))
                       (PrintElemHtml (cddr x) v outs)))))))

       (loop-pub        ;; e.g. (loop-pub  publics ...
                        ;; loops over (current entity) : publics
                        ;; which should be a sequence of strings
                        ;;  such as  "cf/fapr/1996/au/Svensson/"
                        ;; reads each one and applies the lest
            (princ "Entering loop-pub")(terpri)
            (dolist (c (getpa *curag* '|entity| (cadr x)))
                (PrintElemHtml (cddr x)
                    (load-adl-check (concatenate 'string
                       "/info/www/ext/acres/" (string c) 
                       "descrip.adl")) outs)))

       (loop-docpub     ;; e.g. (loop-docpub  publics ...
                        ;; loops over (current document) : publics
                        ;; which should be a sequence of strings
                        ;;  such as  "cf/fapr/1996/au/Svensson/"
                        ;; reads each one and applies the lest
            (prog (v)
            (dolist (c (getpa *curag* '|document| (cadr x)))
               (setq v (open (concatenate 'string
                       "/info/www/ext/acres/" (string c) 
                       "tabel.html") :direction :probe))
               (cond (v
                    (setq in (open (concatenate 'string
                       "/info/www/ext/acres/" (string c) 
                       "tabel.html") :direction :input))
                    (CopyRestContinue in outs nil))))))

       (loopvol        ;; e.g. (loopvol publications ...)
                        ;; loops over (current volume):tenants
                        ;; which should be a list of name - kind pairs
          (prog (n v)
            (setq n (string (getassoc obj 'propername)))
            (dolist (c (getpa *curag* '|volume| (cadr x)))
                (princ (car c))(terpri)
                (setq v (intern (concatenate 'string n (string (car c)) "/")))
                (princ (get v '|publication|)) (terpri)
                (princ (getdaba33 v '|publication| nil))(terpri)
                (PrintElemHtml (cddr x)
                    (getdaba33 v '|publication| *curag*) outs)
                (PrintElemHtml (cddr x)
                    (getdaba33 v '|publication| *curag*) nil) 
               )))

       (loopvold        ;; like loopvol but members obtained by dir
           (prog (dir volnam lis obj)
              (setq dir (dirof (getpa *curag* '|volume| 'file)))
              (setq volnam (getpa *curag* '|volume| 'propername)) 
              (setq lis (getmemberdir dir nil nil))
              (setq lis (remove '|au| lis))
              (setq lis (remove '|parse| lis))
              (cond ((getpa *curag* '|volume| 'thousands)
                     (setq lis (sort lis #'string-lessp
                                 :key (function fourstring)))))
              (cond (lis)
                    (T (setq dir (concatenate 'string dir "au/"))
                       (setq lis (getmemberdir dir nil nil))))
              (dolist (ent lis)
                 (or (equal ent '|au|)(progn
                   (setq obj (get ent volnam))
                   (cond ((null obj)
                          (setq fi (concatenate 'string dir
                              (string ent) "/descrip.adl"))
                          (setq obj (load-adl fi))
                          (setf (get ent volnam) obj)))
                   (PrintElemHtml (cdr x) obj outs))))))

       (loopvold-nocache     ;; like loopvold but doesnt use cache
           (prog (dir volnam lis obj)
              (setq dir (dirof (getpa *curag* '|volume| 'file)))
              (setq volnam (getpa *curag* '|volume| 'propername)) 
              (setq lis (getmemberdir dir nil nil))
              (setq lis (remove '|au| lis))
              (setq lis (remove '|parse| lis))
              (cond ((getpa *curag* '|volume| 'thousands)
                     (setq lis (sort lis #'string-lessp
                                 :key (function fourstring)))))
              (cond (lis)
                    (T (setq dir (concatenate 'string dir "au/"))
                       (setq lis (getmemberdir dir nil nil))))
              (dolist (ent lis)
                 (or (equal ent '|au|)(progn
                        (setq fi (concatenate 'string dir
                              (string ent) "/descrip.adl"))
                        (setq obj (load-adl fi))
                        (setf (get ent volnam) obj)
                        (PrintElemHtml (cdr x) obj outs))))))

       (setvol          ;; during loopserd, set current object to
                        ;; become volume of *curag*
              (setf (get *curag* '|volume|) obj))

       (loopserd        ;; like loopvold but for all volumes in a series
           (prog (dir volnam lis obj)
              (setq *curletter* "Z")
              (setq dir (dirof (getpa *curag* '|series| 'file)))
              (setq volnam (getpa *curag* '|series| 'propername)) 
              (setq lis (getmemberdir dir nil nil))

;              (setq lis (subseq lis 0 15))
                 
              (dolist (ent lis)(progn
                   (setq obj (get ent volnam))
                   (cond (t  ;(null obj)
                          (setq fi (concatenate 'string dir
                              (string ent) "/descrip.adl"))
                          (setq obj (open fi :direction :probe))
                          (cond
                            ((null obj)
;                               (setq obj (list
;                                 (list 'object ent)(list 'firstname NIL)))
                                )
                            (t (setq obj (load-adl fi))
                              ;(setf (get ent volnam) obj)  
                               ))))
                   (cond (obj (princ "+ ")
                              (princ (getassoc obj 'object))(terpri)
                              (PrintElemHtml (cdr x) obj outs)
                              (PrintElemHtml (cdr x) obj T)))))
              (princ "Loop completed")(terpri)))

       (alph-head  ;; to be used under e.g. the previous operation
                  ;; (alph-head ...) has the effect of checking whether
                  ;; the first letter of the present object symbol is 
                  ;; diff from *curletter*. If so, ... is printed as a
                  ;; heading, and *curletter* is reset
          (prog (r s)
              (setq r (string-upcase 
                  (subseq (string (getassoc obj 'object)) 0 1)))
              (princ (list r *curletter*))(terpri)
              (cond ((string= r *curletter*)(return)))
              (setq *curletter* r)
              (princ "-------------> ")(princ r)(terpri)
              (PrintElemHtml (cdr x) obj outs)))

       (firstchar  ;; same - prints first letter of current object's name
              (princ (subseq (string (getassoc obj 'object)) 0 1) outs))

       (loopvoltoc        ;; like loopvold but a TOC property of the
                  ;; volume is used for directing the printout, and the
                  ;; first "argument" of the command specifies how to
                  ;; print session headings
           (prog (dir volnam lis)
              (setq dir (dirof (getpa *curag* '|volume| 'file)))
              (princ dir)(terpri)
              (setq volnam (getpa *curag* '|volume| 'propername)) 
              (setq lis (getpa *curag* '|volume| 'toc))
              (princ "Members of volume: ")(princ volnam)(terpri)
              (dolist (ent lis)(cond
                ((stringp ent)  ;; then it's a session head
                 (PrintElemHtml (cadr x) (list (list 'title ent)) outs))
                (T
                   (setq obj (get ent volnam))
                   (princ obj)(terpri)
                   (cond ((null obj)
                          (setq fi (concatenate 'string dir
                              (string ent) "/descrip.adl"))
                          (princ fi)(terpri)
                          (setq obj (load-adl fi))
                          (setf (get ent volnam) obj)))
                   (PrintElemHtml (cddr x) obj T)
                   (PrintElemHtml (cddr x) obj outs))))))

       (loopalpha   ;; assumes that current cursor is D-like and
            ;; that current collection is organized by alphabet
            ;; subdivided members.
            ;; makes a loop over all those members, and applies the
            ;; following operations to each of them.
           (prog (dir typ let lis)
              (setq dir (dirof (getpa *curag* '|collection| 'file)))
              (setq typ (getpa *curag* '|collection| '|object|)) 
              (dolist (le *alphabet*)
                (setq let (string le))
                (setq lis (getmemberdir 
                  (concatenate 'string dir let "/")   nil ".adl"))
                (princ "<center><h3>-- <a name=\"let" outs)
                (princ let outs)
                (princ "\">" outs)
                (princ let outs)
                (princ "</a> --</h3></center>" outs)(terpri outs)
                (dolist (pers lis)
                   (setq obj (get pers typ))
                   (cond ((null obj)
                          (setq fi (concatenate 'string dir
                              let "/" (string pers) ".adl"))
                          (setq obj (load-adl fi))
                          (setf (get pers typ) obj)))
                   (PrintElemHtml (cdr x) obj outs)))))

       (loop-alpha   
            ;; format (loop-alpha (heading) (footing) cmd cmd ...)
            ;; assumes that current cursor is D-like and
            ;; that current collection is organized by alphabet
            ;; subdivided members.
            ;; Expands heading for each letter
            ;; and cmd cmd... for each member in that letter,
            ;; then expands footing
            ;; binds curlet to current letter for heading and footing

           (prog (dir typ let lis)
              (setq dir (dirof (getpa *curag* '|collection| 'file)))
              (setq typ (getpa *curag* '|collection| '|object|)) 
              (dolist (le *alphabet*)
                (setq let (string le))
                (setq lis (getmemberdir 
                  (concatenate 'string dir let "/")   nil ".adl"))
                (PrintElemHtml (cadr x) (cons (list 'curlet le) obj) outs)
                (dolist (pers lis)
                   (setq obj (get pers typ))
                   (cond ((null obj)
                          (setq fi (concatenate 'string dir
                              let "/" (string pers) ".adl"))
                          (setq obj (load-adl fi))
                          (setf (get pers typ) obj)))
                   (PrintElemHtml (cdddr x) obj outs))
                (PrintElemHtml (caddr x) (cons (list 'curlet le) obj) outs)
               )))

       (loop-one-alpha   
            ;; format (loop-alpha "x" cmd cmd ...)
            ;; assumes that current cursor is D-like and
            ;; that current collection is organized by alphabet
            ;; subdivided members.
            ;; Does cmd cmd... for each member in the letter x (string of cap)

           (prog (dir typ let lis)
              (setq dir (dirof (getpa *curag* '|collection| 'file)))
              (setq typ (getpa *curag* '|collection| '|object|)) 
              (setq lis (getmemberdir 
                  (concatenate 'string dir (cadr x) "/")   nil ".adl"))
              (setq let (getassoc obj 'curlet))
              (dolist (pers lis)
                   (setq obj (get pers typ))
                   (cond (T   ;(null obj)
                          (setq fi (concatenate 'string dir
                              let "/" (string pers) ".adl"))
                          (setq obj (load-adl fi))
;                          (cond ((equal (get obj 'object)
;                                        (getpa *curag* '|entity| 'object))
;                                 (setf (get *curag* '|entity|) object)))
                          (setf (get pers typ) obj)))
                   (PrintElemHtml (cddr x) obj outs))))

       (meta (PrintElemHtml (cdr x)(getassoc obj 'meta) outs))
       (langof (prog (v)
          (setq v (getassoc obj (cadr x)))
          (and (cddr x)(condload v (caddr x)))
          (princ (or
               (getassoc2 (get v (caddr x)) 'langname *language*)
               v)
            outs)))
       (revparts
          (princ (cadr (getassoc obj (cadr x))) outs)
          (princ " " outs)
          (princ (car (getassoc obj (cadr x))) outs))
       (mailname   ;; (revname to tocorr) t ex
          (prog (email lm)
            (setq email (getassoc obj (cadr x)))
            (setq lm (assoc (intern email) *mailnames*))
            (cond (lm (setq lm (cdr lm))))
            (cond ((null lm)
                   (setq lm (getassoc obj (caddr x)))))
            (cond ((null lm)
                   (setq lm (list email ""))))
            (princ (cadr lm) outs)
            (princ  " " outs)
            (princ (car lm) outs)))
       (date (princ (date-time nil) outs))
       (datez (princ (date-time 'Z) outs))
       (datetime (princ (date-time T) outs))
       (month (princ (getassoc *monthlist* (getassoc obj (cadr x))) outs))
       (datee ;; takes property on form "07-25", prints as "July 25"
          (datee-fn (getassoc obj (cadr x)) outs))
       (datex ;; takes property on form "07-25", prints as "25.7"
          (datex-fn (getassoc obj (cadr x)) outs))
       (datexy ;; takes property on form "1998-07-25", prints as "25.7.1998"
          (datexy-fn (getassoc obj (cadr x)) outs))
       (datum (prog (md d)
           (setq md (getassoc2 obj 'meta 'symbol))
           (setq d (maildate2date md))
           (princ (cadr d) outs)
           (princ " " outs)
           (princ (maildate2month md *manadnamn*) outs)))
       (veckdag  
           (princ (maildate2weekday (getassoc2 obj 'meta 'symbol)
                        *veckdag*) outs))

       (begweb
         (princ "<A HREF=" outs)
         (princ '|"| outs)          
         (princ (getassoc obj (cadr x)) outs)
         (princ '|"| outs)          
         (cond ((cddr x)(princ " target=\"" outs)
                   (prin1 (cond
                      ((stringp (caddr x))(caddr x))
                      (t (getassoc obj (caddr x)))) outs)
                   (princ "\"" outs)))
         (princ ">" outs))
       (endweb
         (princ "</A>" outs))

       (genhtml     ;;; use as (genhtml indic text)
         (princ "<A HREF=" outs)
         (princ '|"| outs)          
         (princ (getassoc obj (cadr x)) outs)
         (princ '|"| outs)          
         (princ ">" outs)
         (princ (caddr x) outs)
         (princ "</A>" outs))

       (listref
         (dolist (y (getassoc obj (cadr x)))
            (princ "[<a href=" outs)
            (princ '|"| outs)
            (princ (caddr x) outs)
            (princ y outs)
            (princ ".html" outs)
            (princ '|"| outs)
            (princ ">" outs)
            (princ y outs)
            (princ "</a>]" outs)
            (cond ((eq y (car (last (getassoc obj (cadr x))))))
                  (t (princ ", " outs)))
              ))
       (prevdate
         (princ (objfile (prevdate obj) '|Maildates| ".html") outs)) 
       (nextdate
         (princ (objfile (nextdate obj) '|Maildates| ".html") outs))


       (prin1 (prin1 (getassoc obj (cadr x)) outs))
       (tp (terpri outs))
       (tpi (terpri outs)(princ "  " outs))
       (udq (princ '|"| outs))
       (dq (princ '|"| outs)
           (PrintElemHtml (cdr x) obj outs)
           (princ '|"| outs))
       (dflt (prog (y)
            (setq y (cdr x))
        lop (cond
               ((null y)(return))
               ((or (eq (caar y) t)
                    (getassoc obj (caar y)))
                (PrintElemHtml (cdar y) obj outs)
                (return)))
            (setq y (cdr y))
            (go lop)))
       (enum (prog (v)
          (setq v (getassoc obj (cadr x)))
          (cond
             ((null v) (princ "---" outs))
             ((atom v) (princ v outs))
             (T (princ (car v) outs)
                (dolist (vv (cdr v))
                   (princ ", " outs)
                   (princ vv outs))))))

       (dispobj ;;; (dispobj ind cmd cmd ...)
          ;;; assumes taht ind property of present object is an ADL 
          ;;; file name; file is loaded and its object is used
            (PrintElemHtml (cddr x) 
                 (load-adl (getassoc obj (cadr x))) outs))

       (dispag ;;; (dispag |series| cmd cmd ...)
          ;;; looks up the series property of *curag* and uses it
            (PrintElemHtml (cddr x) 
                 (get *curag* (cadr x)) outs))

       (dispaut ;;; (dispaut author cmd cmd...)
          ;;; assumes the author property of current object is an author
          ;;; identifier or list of such - obtains it (the first) and works on
            (prog (a b)
             (setq a (getassoc obj (cadr x)))

             (princ "***> ")(princ a)(terpri)(terpri)

             (cond ((null a)(return))
                   ((stringp a)(return))
                   ((listp a)(setq a (car a))))

             (getperson a)

             (princ "===> ")(princ a)(terpri)(terpri)

             (setq b (or (get a '|entity|)(get a '|persons|)(get a '|names|)
                (get a '|volume|)))

             (princ "...> ")(princ b)(terpri)(terpri)

             (cond (b (PrintElemHtml (cddr x) b outs)))))

       (dispmail ;;; (dispmail)
          ;;; assumes the present object has author and otherauthors
          ;;; properties. Obtains the email addresses of all
            (prog (a b n)
             (setq a (getassoc obj 'author))
             (or (listp a)(setq a (cons a (getassoc obj 'otherauthors))))
             (princ "--> ")(princ a)(terpri)
             (dolist (x a)(cond ((symbolp x)
                 (getperson x)
                 (setq b (or (get x '|entity|)
                             (get x '|persons|)(get x '|names|)
                             (get x '|volume|)))
                 (cond ((null (getassoc b 'email)))
                       (n (princ ", " outs)
                          (princ (getassoc b 'email) outs))
                       (T (princ (getassoc b 'email) outs)
                          (setq n t))))))))

     (displaypur
            (PrintElemHtml (cddr x)
                 (load-adl (concatenate 'string
                    "/info/www/ext/pur/x" 
                    (string (getassoc obj (cadr x)))
                    "/descrip.adl")) outs))
       (display (prog (v val oldc olde newc newe) 
            (setq v (getassoc obj (car (cadr x))))
            (setq oldc (getpa 'd '|collection| 'object))
            (setq olde (getpa 'd '|entity| 'object))
            (setq newc (cadadr x))
            (setq newe v) 
;            (chafunc 'd newc)
;            (chafune 'd newe)
            (setq val (getdaba33 newe newc 'd))
          '((princ "Display: ")
              (princ (getassoc obj 'object))(terpri)
              (princ obj)(terpri)
              (princ v)(terpri)
              (princ val)(terpri)
              (princ "OK?")(read-line))
            (and val (PrintElemHtml (cddr x) val outs))
;            (chafunc 'd oldc)
;            (chafune 'd olde)
                ))

       (otherauthors
          (print-other-authors (getassoc obj 'otherauthors)
                               outs 'comm-fatand))
       (displayauthors
          (print-other-authors (getassoc obj (cadr x))
                               outs (and (cddr x)(caddr x))))
       (confseries
           ;;; look up conferenc series in main ACRES structure
           ;;; corresponding to object name of present object
           ;;; and evaluate commands relative to it
                 (prog (n v)
                    (setq n (string (getassoc obj 'object)))
                    (setq v (concatenate 'string
                       "/info/www/ext/acres/cf/" 
                       (string-downcase (subseq n 0 (- (length n) 3)))
                       "/descrip.adl"))
                    (setq v (load-adl-check v))   
                    (PrintElemHtml (cdr x) v outs)))

       (confinst
           ;;; like previous but for conference instance
                 (prog (n v)
                    (setq n (string (getassoc obj 'object)))
                    (setq v (concatenate 'string
                       "/info/www/ext/acres/cf/" 
                       (string-downcase (subseq n 0 (- (length n) 3)))
                       "/19"
                       (subseq n (- (length n) 2))
                       "/descrip.adl"))
                    (setq v (load-adl-check v))   
                    (PrintElemHtml (cdr x) v outs)))

       (descriprop
          ;; used for printing multiple properties into
          ;; object-describing html file
          (prog (j)(setq j (getassoc obj (cadr x)))
             (cond ((atom j)(princ j outs)(terpri outs)(return)))
             (princ (car j) outs)(terpri)
             (dolist (h (cdr j))
               (princ "     " outs)
               (princ "<br> " outs)
               (princ h outs)(terpri outs))))

       (refer
           (PrintElemHtml (cdddr x)
                          (get (getassoc obj (cadr x))(caddr x))
                          outs))

       (lookup
           (PrintElemHtml 
             (cddddr x)
             (load-adl-check (concatenate 'string (cadr x)
                 (string (getassoc obj (caddr x))) (cadddr x)))
             outs))

       (subprop
           (setq *peh-stack* (cons obj *peh-stack*))
           (PrintElemHtml (cddr x)
                          (getassoc obj (cadr x))
                          outs)
           (setq *peh-stack* (cdr *peh-stack*)))
       (pop (PrintElemHtml (cdr x)(car *peh-stack*) outs))
       (call 
           (prog (j)
               (setq j (get (cadr x) 'markup-format))
               (cond ((null j)(princ (cadr x))
                   (princ " is undefined markup script")(terpri)
                   (return)))
               (PrintElemHtml j  (append (cddr x) obj) outs)))
       (callcase
           (PrintElemHtml (get (or (getassoc2 obj 'meta 'type)
                                   (getassoc obj 'doctype)
                                   (getassoc obj 'isa))
                               (cadr x))
                          obj outs)
              )
       (datatype (prog (j)
           (setq j (getassoc obj (cadr x)))
           (PrintElemHtml
              (cdr (or (assoc (type-of j) (cddr x))
                       '(nil (string (type-of j)) " [*** undefined ***]")))
              obj outs)))
       (variant (cond
          ((eq (cadr x) *variant*)
           (PrintElemHtml (cddr x) obj outs))))
       (listlength (prog (j)
          (setq j (getassoc obj (cadr x)))
          (cond (j (princ "<b>[" outs)
                   (princ (length j) outs)
                   (princ "]</b>" outs)))))
       (typecase 
          (PrintElemHtml
            (cdr (or (assoc (getassoc2 obj 'meta 'type)(cdr x))
                     (assoc (getassoc obj 'doctype)(cdr x))
                     (assoc (getassoc obj 'isa)(cdr x))
                     (last x)))
            obj outs))
       (cond (prog (v d) 
                  (setq v (cond
                    ((atom (cadr x))(getassoc obj (cadr x)))
                    (T (setq d (cadr x))
                       (case (car d)
                           (agprop (getpa *curag* (cadr d)(caddr d)))
                         ))))
                  (cond (v (PrintElemHtml (cddr x) obj outs)))))
       (condval (prog (val y)
          (setq val (getassoc obj (cadr x)))
          (setq y (cddr x))
        lop  (cond ((null y)(return))
               ((or (equal val (caar y))
                    (equal (caar y) 'else)
                    (and (listp (caar y))(member val (caar y))))
                (PrintElemHtml (cdar y) obj outs)
                (return)))
             (setq y (cdr y))
             (go lop)))

       (condval-dflt 
          ;; (condval-dflt editmode |register| (x op op ... op)(y op ...))
          ;; looks up EDITMODE property of current object. If
          ;; absent, looks up EDITMODE property of register of current agent.
          ;; Value retrieved is matched against x, y, ... as a case statement.
          (prog (val y)
          (setq val (getassoc obj (cadr x)))
          (cond ((null val)(setq val (getpa *curag* (caddr x)(cadr x)))))
          (setq y (cdddr x))
        lop  (cond ((null y)(return))
               ((or (equal val (caar y))
                    (equal (caar y) 'else)
                    (and (listp (caar y))(member val (caar y))))
                (PrintElemHtml (cdar y) obj outs)
                (return)))
             (setq y (cdr y))
             (go lop)))
       (condmember 
          (cond ((member (caadr x)(getassoc obj (cadadr x)))
                 (PrintElemHtml (cddr x) obj outs))))
       (condfile
          (princ (concatenate 'string (docdiry obj) (cadr x)))
          (terpri)
          (cond ((open (concatenate 'string (docdiry obj) (cadr x))
                       :direction :probe)
                 (PrintElemHtml (cddr x) obj outs))))
       (condnofile
          (princ (concatenate 'string (docdiry obj) (cadr x)))
          (terpri)
          (cond ((null (open (concatenate 'string (docdiry obj) (cadr x))
                       :direction :probe))
                 (PrintElemHtml (cddr x) obj outs))))
       (condsev (cond
          ((null (getassoc obj (cadr x))) nil)
          ((null (cdr (getassoc obj (cadr x)))) nil)
          (T (PrintElemHtml (cddr x) obj outs))))

       (condmulauthors  ;; two sequences of commands, one for one
                        ;; author, one for several authors
        (cond ((and
                 (null (getassoc obj 'otherauthors))
                 (not (listp (getassoc obj 'author))))
                 (PrintElemHtml (cadr x) obj outs))
              (T (PrintElemHtml (caddr x) obj outs))))

       (condnot (prog (v d) 
                  (setq v (cond
                    ((atom (cadr x))(getassoc obj (cadr x)))
                    (T (setq d (cadr x))
                       (case (car d)
                           (agprop (getpa *curag* (cadr d)(caddr d)))
                         ))))
                  (cond ((null v)(PrintElemHtml (cddr x) obj outs)))))

       (condincluded 
          (cond ((member (getassoc obj (cadr x)) *includelist*)
                 (PrintElemHtml (cddr x) obj outs))))

       (condnotself (cond
          ((notself (getassoc obj (cadr x)))
           (PrintElemHtml (cddr x) obj outs))))
       (condself (cond
          ((notself (getassoc obj (cadr x))))
          (T (PrintElemHtml (cddr x) obj outs)) ))
       (condbib (cond
          ((getassoc obj (cadr x))
           (princ "," outs)
           (terpri outs)
           (princ "   " outs)
           (princ (caddr x) outs)
           (princ " = {" outs)
           (princ (getassoc obj (cadr x)) outs)
           (princ "}" outs))))

       (bindq (PrintElemHtml (cdddr x)(cons (list (cadr x)(caddr x)) obj)
                  outs))

       (apply (funcall (cadr x) obj outs)) 

;;; The following "obsolete" in the sense that apply for old MATER system

       (sourcefile 
         (princ (objobjfile obj (cadr x)) outs))
       (altsourcefile (prog (v)
         (setq v (objobjfile obj (cadr x)))
         (princ (cond ((open v :direction :probe) v)
                      (T (objobjfile obj (caddr x))))  outs)))

       (otherwise nil))) )) )))

(defop 'rdrep '(prog (fi l n)
   (setq n (read-string))
   (setq l (list nil))
   (setq fi (open (concatenate 'string (docdiry (get *curag* '|collection|)) 
                      n ".txt")
              :direction :input))
 lop (setq s (trimleft (trimright (read-line fi nil "*eof*"))))
   (cond ((string= s "*eof*")(setq *includelist* (cdr l))
          (close fi)(return))
         ((string= s "")(go lop)))
   (nconc l (list (intern s)))
   (go lop)
  ))

(defop 'edrep '(prog (n s)
   (setq n (read-string))
   (setq s (concatenate 'string (docdiry (get *curag* '|collection|)) n
                        ".txt"))
   (system (concatenate 'string "emacs " s))
   (groupacc s)))
   

(defun ExpandTwoLevel (l obj outs)(dolist (x l)(cond
   ((stringp x)(prin1 x outs)(terpri))
   ((symbolp x) (princ x outs))
   (t (case (car x)
         (lpar  (princ "(" outs)
                (cond ((cdr x)(princ " " outs)(princ (cadr x) outs))))
         (dlpar (princ "((" outs)
                (cond ((cdr x)(princ " " outs)(princ (cadr x) outs))))
         (rpar  (princ ")" outs))
         (drpar (princ "))" outs))
         (btp   (princ "(tp)" outs))
         (flex  (dolist (v (cdr x))(princ v outs)))
         (slex  (princ "\"" outs)
                (PrintElemHtml (cdr x) obj outs)
                (princ "\"" outs))
         (dbex  (PrintElemHtml (cdr x) obj outs))
     )))))

(setq *language* '|english|)

(defun dotcond-fn (s outs)(cond
  ((listp s)(dotcond-fn (car (last s)) outs))
  ((member (elt s (- (length s) 1))
           '(#\. #\! #\?)))
  (T (princ "." outs)) ))

(defun remzeros (s)
 (cond
  ((not (stringp s))(remzeros (string s)))
  ((string-equal s "") "")
  ((string-equal (subseq s 0 1) "0")
    (remzeros (subseq s 1)))
  (T s) ))

(defun fillzeros (pos a outs)(prog (n s)
  (setq s (cond ((stringp a) a)(t (string a))))
  (setq n (- pos (length s)))
 l (cond ((> n 0)(princ "0" outs)(setq n (- n 1))(go l)))
  (princ s outs)))

(defun terindent (ch)(terpri ch)(princ "  " ch))

(defun fourstring (a)(prog (s)
  (setq s (string a))
  (cond ((= (length s) 3)(setq s (concatenate 'string "0" s))))
  (return s)))

;(defun authorfix2 (l m hot outs)   ;; old definition
;  (authorfix (cond ((listp l)(append l m))(t (cons l m)))
;             hot outs))

(defun authorfix2 (l m hot outs)(progn
  (cond
   ((equal (car hot) 'txs)
      (authorfix-a (cond ((listp l)(append l m))(t (cons l m)))
                   (cdr hot) outs 'txs))
   ((equal (car hot) 'tex)
      (authorfix-tex (cond ((listp l)(append l m))(t (cons l m)))
                   outs))
   (T (authorfix-a (cond ((listp l)(append l m))(t (cons l m)))
                   hot outs nil)))))

(defun authorfix (l hot outs)(cond
   ((equal (car hot) 'txs)(authorfix-a l (cdr hot) outs 'txs))
   (T (authorfix-a l hot outs nil))))

(defun authorfix-tex (l outs)
 (cond
  ((null l)(princ "[Author unknown]" outs))
  ((stringp l)(princname (ahxstring l 'tex) outs))
  ((atom l)(prog (j n m)
    (getperson l)
    (setq j (or (get l '|entity|)(get l '|persons|)(get l '|names|)
                (get l '|volume|)))
    (cond ((null j)
           (setq n (string l))
           (setq m (position '#\, n))
           (cond ((null m)(princ l outs)(return)))
           (princ (subseq n (+ m 1)) outs)
           (princ " " outs)
           (princ (subseq n 0 m) outs)
           (return)))
    (princ (ahxstring (getassoc j 'firstname) 'tex) outs)
    (princ " " outs)
    (princ (ahxstring (or (getassoc j 'lastname)
                          (string (getassoc j 'object)))  'tex) outs)
        ))
  (t (prog (j flgg)
    (setq j l)
    (setq flgg (and (cdr l)(cddr l)))
  lop (authorfix-tex (car j) outs)
    (setq j (cdr j))
    (cond ((null j)(return))
          ((null (cdr j))(princ (cond (flgg ", and ")(t " and ")) outs))
          (T (princ ", " outs)))
    (go lop)))))

(defun authorfix-a (l hot outs flg)
 (princ l)(terpri)
 (cond
  ((null l)(princ "[Author unknown]" outs))
  ((stringp l)(princ "Author string: ")(princ l)(terpri)
       (princname (ahxstring l flg) outs))
  ((atom l)(prog (j n m)
    (getperson l)
    (setq j (or (get l '|entity|)(get l '|persons|)(get l '|names|)
                (get l '|volume|)))
    (cond ((null j)
           (princ ">>>> Unknown person: ")(princ l)(terpri)
           (setq n (string l))
           (setq m (position '#\, n))
           (cond ((null m)(princ l outs)(return)))
           (princ (subseq n (+ m 1)) outs)
           (princ " " outs)
           (princ (subseq n 0 m) outs)
           (return)))
    (cond ((null (getassoc j 'webpage)))
          ((member t hot)
           (princ "<a href=\"" outs)
           (princ (getassoc j 'webpage) outs)
           (cond ((stringp (car hot))
                  (princ "\" target=\"" outs)(princ (car hot) outs)))
           (princ "\">" outs)))

    (princ (ahxstring (getassoc j 'firstname) flg) outs)
    (princ " " outs)
    (princ (ahxstring (or (getassoc j 'lastname)
                          (string (getassoc j 'object)))  flg) outs)
    (cond ((null (getassoc j 'webpage)))
          ((member t hot)
           (princ "</a>" outs))
          ((member 'latex hot)
           (princ "$^*$" outs)))
        ))
  (t (prog (j flgg)
    (setq j l)
    (setq flgg (and (cdr l)(cddr l)))
  lop (authorfix-a (car j) hot outs flg)
    (setq j (cdr j))
    (cond ((null j)(return))
          ((null (cdr j))(princ (cond (flgg ", and ")(t " and ")) outs))
          (T (princ ", " outs)))
    (go lop)))))


(defun author-univ-rep (obj outs)(prog (s il fn ln j)
   (setq s (getassoc obj 'author))
   (or (listp s)
       (setq s (cons s (getassoc obj 'otherauthors))))
   (setq il '(
       "Huvudförfattarens"
       "Medförf 1"
       "Medförf 2"
       "Medförf 3"
       "Medförf 4"
       "Medförf 5"
       "Medförf 6"))
   (dolist (l s)
       (cond ((symbolp l)
              (getperson l)
              (setq j (or (get l '|entity|)(get l '|persons|)(get l '|names|)
                          (get l '|volume|)))
              (setq fn (getassoc j 'firstname))
              (setq ln (or (getassoc j 'lastname)
                           (string (getassoc j 'object)))))
             (T (setq ln (string (lastname l)))
                (setq fn (subseq l 0 (- (length l)(+ (length ln) 1))))))
       (princ (car il) outs) 
       (princ " förnamn: " outs)
       (princ (atstring fn) outs)
       (terpri outs)
       (princ (car il) outs) 
       (princ " efternamn: " outs)
       (princ (atstring ln) outs)
       (terpri outs)
       (cond ((cdr il)(setq il (cdr il))))  )
      ))

(defun authorvert2 (l m hot outs)
  (authorvert (cond ((listp l)(append l m))(t (cons l m)))
             hot outs))

(defun authorvert (l hot outs)
 (cond
  ((null l)(princ "[Author unknown]" outs))
  ((stringp l)(princname (ahstring l) outs))
  ((atom l)(prog (j)
    (getperson l)
    (setq j (or (get l '|entity|)(get l '|persons|)(get l '|names|)))
    (cond ((null (getassoc j 'webpage)))
          ((equal (car hot) t)
           (princ "<a href=\"" outs)
           (princ (getassoc j 'webpage) outs)
           (cond ((stringp (car hot))
                  (princ "\" target=\"" outs)(princ (car hot) outs)))
           (princ "\">" outs)))
    (princ (ahstring (getassoc j 'firstname)) outs)
    (princ " " outs)
    (princ (ahstring (or (getassoc j 'lastname)
                          (string (getassoc j 'object)))  flg) outs)
    (cond ((null (getassoc j 'webpage)))
          ((equal (car hot) t)
           (princ "</a>" outs))
          ((equal (car hot) 'latex)
           (princ "$^*$" outs)))
        ))
  (t (prog (j flg)
    (setq j l)
    (setq flg (and (cdr l)(cddr l)))
  lop (authorvert (car j) hot outs)
    (setq j (cdr j))
    (cond ((null j)(return))
          (T (princ "<br>" outs)))
    (go lop)))))

(defun princname (s outs)(prog (p)
  (setq p (position '#\, s))
  (cond (p (princ (subseq s (+ p 1)) outs)
           (princ " " outs)
           (princ (subseq s 0 p) outs))
        (t (princ s outs)) )
   ))

;; author handling, older generation

(defun print-other-authors (l outs flg)
  ;;; l contains at least one symbol of type authors
  ;;; choices of flg:
  ;;; comm-fatand   comma before each name xc bold 'and' btw 2nd-last and last
  ;;; xcomm-fatand  like previous except no initial comma
  ;;; comm-and      comma before each name xc ordinary 'and' 
  ;;;               btw 2nd-last and last
  ;;; xcomm-and     like previous except no initial comma
  ;;; xcomm-and-brs like previous except link to brs instead of homepage
  ;;; bib           'and' before each name
  ;;; nil           don't understand this one
  (cond 
    ((null l) nil)
    ((eq flg 'bib)
     (dolist (a l)
       (princ " and " outs)
       (cond ((stringp a)(princ (ahstring a) outs))
             (T  (condload a '|authors|)
                 (PrintElemHtml (get '|authorname| 'markup-format)
                               (get a '|authors|) outs)))))
    ((eq flg 'xcomm-fatand)
     (printauthor (car l) outs)
     (print-other-authors (cdr l) outs 'comm-fatand))
    ((eq flg 'xcomm-and)
     (printauthor (car l) outs)
     (print-other-authors (cdr l) outs 'comm-and))
    ((eq flg 'xcomm-and-brs)
     (print-other-authors-brs l outs))
    ((cdr l) 
       (cond 
         (flg (princ ", " outs)))
       (printauthor (car l) outs)
       (print-other-authors (cdr l) outs flg))
    ((eq flg 'comm-fatand)
      (princ "</b> and <b>" outs)
      (printauthor (car l) outs))
    ((eq flg 'comm-and)
      (princ " and " outs)
      (printauthor (car l) outs))
    (T
      (princ " and " outs)
      (printauthor (car l) outs)) ))

(defun print-other-authors-brs (l outs)
  ;;; continue printing in case of xcomm-and-brs above
  (cond 
    ((null l) nil)
    ((cdr l) 
       (printauthor-brs (car l) outs)
       (princ ", " outs)
       (print-other-authors-brs (cdr l) outs))
    (T
      (princ " and " outs)
      (printauthor-brs (car l) outs))))

(defun printauthor (s outs)(cond
  ((stringp s) (princ (ahstring s) outs))
  (T 
     (condload s '|authors|)
     (PrintElemHtml (get '|mentionauth| 'markup-format)
        (get s '|authors|) outs))))

(defun printauthor-brs (s outs)(cond
  ((stringp s) (princ s outs))
  (T 
     (condload s '|authors|)
     (PrintElemHtml (get '|mentionauth-brs| 'markup-format)
        (get s '|authors|) outs))))

(defun date-time (flg)(prog (j k)
  (setq j (open *slask* :direction :output))
  (setq k (truename j))
  (system (concatenate 'string 
     (cond (flg "date '+%d.%m.19%y  kl  %H:%M' >")(T "date '+%d.%m.19%y' >"))
     k))
  (close j)
  (setq j (open *slask* :direction :input))
  (setq k (read-line j))
  (close j)
  (cond ((eq flg 'Z)
         (setq k (concatenate 'string
            (remzeros (subseq k 0 3))
            (remzeros (subseq k 3 6))
            (subseq k 6 10)))))
  (return k)))

(defun datee-fn (s outs)
  (princ (getassoc '((|01| "January")
  (|02| "February")
  (|03| "March")
  (|04| "April")
  (|05| "May")
  (|06| "June")
  (|07| "July")
  (|08| "August")
  (|09| "September")
  (|10| "October")
  (|11| "November")
  (|12| "December")
    )   (intern (subseq s 0 2)) )  outs)
  (princ " " outs)
  (princ (remzeros (subseq s 3)) outs))

(defun datex-fn (s outs)
  (princ (remzeros (subseq s 3)) outs)
  (princ "." outs)
  (princ (remzeros (subseq s 0 2)) outs)
     )

(defun datexy-fn (s outs)
  (princ (remzeros (subseq s 8 10)) outs)
  (princ "." outs)
  (princ (remzeros (subseq s 5 7)) outs)
  (princ "." outs)
  (princ (remzeros (subseq s 0 4)) outs)
     )

;;; ------------------------------------------------------
;;; Functions for printing HTML viewable object descriptions

(defun GenHtmlObj (obj type)(prog (fn of)
   (setq of (objfile obj type ".html"))
   (setq fn (open of :direction :output))
   (princ "<pre>" fn)
   (terpri fn)(terpri fn)
   (princ "(putobj '" fn)
   (prinh obj fn) 
   (princ " '" fn)
   (prinh type fn)
   (princ " '(" fn)
   (terpri fn)
   (PrintHtmlAlist (get obj type) "   " fn)
   (princ "   ))" fn)
   (close fn)
     ))

(defun PrintHtmlAlist (l s o)
  (dolist (x l)
    (princ s o)
    (princ "(" o)
    (prinh (car x) o)
    (princ " " o)
    (cond
     ((null (cddr x))
       (prinh (cadr x) o))
     (t (case (caddr x)
       (alist
          (princ "(" o)
          (terpri o)
          (PrintHtmlAlist (cadr x)(concatenate 'string s "    ") o)
          (princ s o)
          (princ "   ) ALIST" o))
       (longlist
          (princ "(" o)
          (prog (n ss)
            (setq ss (concatenate 'string s "    "))
            (setq n 0)
            (dolist (y (cadr x))
               (cond ((eq n 0)
                      (setq n 5)
                      (terpri o)
                      (princ ss o)))
               (prinh y o)
               (setq n (- n 1))
               (princ " " o)))
          (princ ") LONGLIST" o))
       (t (prinh (cadr x) o)
          (princ " " o)
          (prinh (caddr x) o))
       )))
    (princ ")" o)
    (terpri o)   ))

(defun prinh (object o)(prog (ty fi)
   (cond ((symbolp object))
         ((prin1 object o)(return)))
   (setq ty (get object 'typelist))
   (cond ((and ty (null (cdr ty)))
          (setq ty (car ty))
          (setq fi (objfile obj ty ".html"))
          (cond ((open fi :direction :probe)
                 (princ "<A HREF=" o)
                 (prin1 fi o)
                 (princ ">" o)
                 (prin1 object o)
                 (princ "</A>" o))
                (T (prin1 object o))) )
         (T (prin1 object o)) ) ))

;;; ------------------------------------------------------
;;; probably obsolete; has been replaced above

(defun insthe-fun-old (pat tf)
    (insthe-funa pat tf (get *curag* '|entity|)))

(defun insthe-funa (pat tf ent)(prog (nam ptn tfi tfj)
  (setq tfi (dirofa tf))
  (setq nam (string (getassoc ent 'object)))
  (setq ptn (getpattern *curag* pat "html" ".ght"))
  (cond ((null ptn)(return nil)))
  (princ "Pattern found")(terpri)
  (princ ptn)(terpri)(terpri)
L (cond ((listp (car ptn))(case (caar ptn)
           (setname
             (princ "Interpreting: ")(princ (car ptn))(terpri)
             (setq nam (cadar ptn))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (prefix
             (setq nam (concatenate 'string (cadar ptn) nam))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (postfix
             (setq nam (concatenate 'string nam (cadar ptn)))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (setdir
             (setq tfi (cadar ptn))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (refdir
             (setq tfi (getpa *curag* (cadar ptn)(caddar ptn)))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
           (dirname
             (setq tfi (concatenate 'string tfi nam "/"))
             (setq nam (cadar ptn))
             (setq ptn (cdr ptn))
             (and ptn (go L)))
       )))
  (setq tfi (concatenate 'string tfi nam ".html"))
  (princ tfi)(terpri)
  (setq tfj (open tfi :direction :output))
  (PrintElemHtml ptn ent tfj)
  (close tfj) 
  (cond ((or (isprefix "/info/www/ext/" tfi)(ishomedir tfi))
         (groupacc tfi)))
  (return 'ok)
))
