;;; general.mhtml: -*- Meta-HTML -*-  Generally useful macros.
;;;
;;;  Copyright (c) 1996 Brian J. Fox
;;;  Author: Brian J. Fox (bfox@ai.mit.edu) Wed May 29 09:03:56 1996

<defsubst double-digits whitespace=delete>
  <set-var %0=<subst-in-string <pad <get-var %0> 2 align=right> " " 0>>
</defsubst>

;;; <array-size>, <array-member>, <array-append>, and <array-add-unique>
;;; are now all written as primitives.

<defun copy-package src dest>
  <alist-to-package <package-to-alist <get-var src> strip=true> <get-var dest>>
</defun>

;;; Use instead of the <option> html tag within the select html tag.
;;; It assumes that the text you wish to display on the page is in
;;; fact the value you wish to return from the form when this option
;;; is selected.  Particularly suited to situations where it is 
;;; helpful to know what the currently 'SELECTED' option might be.
;;; Example:
;;;          <select name=airport>
;;;            <select-option airport Atlanta>
;;;            <select-option airport Boston>
;;;            <select-option airport Chicago>
;;;            <select-option airport "Los Angeles">
;;;            <select-option airport "New York">
;;;            <select-option airport Miami>
;;;            <select-option airport "San Francisco">
;;;         </select>
;;;
<defsubst select-option whitespace=delete>
  <if <array-member "%1" "%0">
      <prog <option selected>%1>
    <prog <if <string-eq "%2" "selected">
	      <prog <option selected>%1>
	    <prog <option>%1>>>>
</defsubst>

;;; Use instead of the <option> html tag within the select html tag
;;; when the text you wish to display is actually different from that
;;; you wish to post back to the server.  Also suited to helping the
;;; user see what the currently 'SELECTED' option might be.
;;; 
;;; Example:
;;;          <select name=airport>
;;;            <select-option-value airport ALT Atlanta>
;;;            <select-option-value airport BTN Boston>
;;;            <select-option-value airport CHI Chicago>
;;;            <select-option-value airport LAS "Los Angeles">
;;;            <select-option-value airport NY  "New York">
;;;            <select-option-value airport MI  Miami>
;;;            <select-option-value airport SFO "San Francisco">
;;;         </select>
;;; e.g.
;;; San Francisco is displayed on the page but SFO is actually
;;; what is returned as the selected options value.
;;;
<defsubst select-option-value whitespace=delete>
  <if <array-member "%1" "%0">
      <prog <option value="%1" selected>%2>
    <prog <option value="%1">%2>>
</defsubst>

;;; Returns the properties alist of the specified file if it exists.
<defsubst file-exists-p whitespace=delete>
  <get-file-properties %0>
</defsubst>

<defun file-exists pathname>
  <get-file-properties <get-var pathname>>
</defun>

<defun webpath-exists document-path>
  <if <not <string-eq <substring <get-var document-path> 0 1> "/">>
      <set-var document-path =
	<get-var mhtml::relative-prefix>/<get-var document-path>>>
  <get-file-properties <get-var mhtml::include-prefix document-path>>
</defun>

<defsubst comma-separated whitespace=delete>
  <set-var cs::i=0 cs::size=<array-size "%0">>
  <while <lt cs::i cs::size>>
    <concat
     <get-var %0[cs::i]>
     <if <eq cs::i <sub cs::size 2>> ", and "
       <if <lt cs::i <sub cs::size 2>> ", ">>>
    <increment cs::i>
  </while>
</defsubst>

;;; Delete all variables references in the session to PACKAGE.
<defun session-destroy-package package>
  <set-var temp=<package-to-alist <get-var package>>>
  <package-delete <get-var package>>
  <session-import <get-var package> <get-var package>>
  <set-var expr="%unset-session-var <package-vars <get-var package>>>">
  <subst-in-var expr "^%" "<">
  <get-var expr>
  <package-delete <get-var package>>
  <alist-to-package <get-var temp> <get-var package>>
</defun>

<defun package-merge source dest>
  <alist-to-package <package-to-alist <get-var source>> <get-var dest>>
</defun>

;;; <make-innocuous [package]> turns the contents of PACKAGE into
;;; non-executable text.
<defun make-innocuous package>
  <set-var names[]=<package-vars <get-var package>>>
  <set-var i=0>
  <while <get-var-once names[i]>>
    <subst-in-var <get-var-once names[i]> "&" "&amp;" "<" "&lt;" ">" "&gt;">
    <increment i>
  </while>
</defun>

<defsubst apply><%0 %1></defsubst>
<defmacro mapply><%0 %1 %2 %3 %4 %5 %6 %7 %8 %9>%xbody</%0></defmacro>

<defun percent num total>
  <div 100.0 <div <get-var total>.0 <get-var num>.0>>
</defun>
