;***********************************************************************
;***                                                                 ***
;***  BRIEF - Basic Reconfigurable Interactive Editing Facility      ***
;***  ---------------------------------------------------------      ***
;***                                                                 ***
;***  This macro opens a window with a hex                           ***
;***  to Ascii translation table.                                    ***
;***                                                                 ***
;***  Roberto T. Edwards                                             ***
;***  800 Victory Boulevard                                          ***
;***  Staten Island, N.Y. 10301                                      ***
;***                                                                 ***
;***  Date: Tuesday, 27 May 1986.  Time: 08:11:00.                   ***
;***                                                                 ***
;***********************************************************************

(macro ascii_table
   (
      (int  asc_tabl_buf   ;*** use this buffer for the table
            edit_buffer    ;*** this was in use before the table was put up
            page_num       ;*** active ascii table page
            key            ;*** last keystroke
            done           ;*** flag 1 => exit  0 => get another keystroke
      )
      (string macro_cmd)
      (global page_num)

      (= done 0)        ; set to 1 to exit from the macro
      ;***
      ;*** create a new buffer for the ascii table
      ;***
      (= asc_tabl_buf (create_buffer "Ascii Table" "$asc$.tmp" 1))
      ;***
      ;*** save the buffer that was current when we started
      ;***
      (= edit_buffer   (inq_buffer))
      ;***
      ;*** make our buffer the current one
      ;***
      (set_buffer asc_tabl_buf)
      ;***
      ;*** open a window for the ascii table
      ;***
      (create_window 1 22 54 1 "PgUp-Prev PgDn-Nxt Home-Fst End-Lst Esc-Exit")
      ;***
      ;*** attach the buffer to the window
      ;***
      (attach_buffer asc_tabl_buf)
      (if (first_time)        
         (= page_num 1)       ;*** make sure we display a valid page
      )

      (sprintf macro_cmd "page_%d" page_num)
      (execute_macro macro_cmd)

      (while (! done )
         (
            (while (== (= key (read_char)) -1))    ;*** wait for a keystroke
            (if (== (& key 0xff) 0)                 ;*** was it a special key?
               (
               ;***
               ;*** Brief doesn't really have an if-then-else construct
               ;*** so the only way to do it is the following. It works,
               ;*** but I sure have to use a lot of parentheses and
               ;*** nesting! If there's another way, I'd love to hear
               ;*** about it.
               ;***
                  (= key (/ key 256))
                  (if (== key 81)                        ;*** PgDn
                     (if (< page_num 5)
                        (+= page_num 1)
                     )
                  ; else             
                     (if (== key 73)                     ;*** PgUp
                        (if (> page_num 1)
                           (-= page_num 1)
                        )
                     ; else
                        (if (== key 71)                  ;*** Home
                           (= page_num 1)
                        ; else
                           (if (== key 79)               ;*** End
                              (= page_num 5)
                           )
                        )
                     )
                  )
                  (sprintf macro_cmd "page_%d" page_num)
                  (execute_macro macro_cmd)
               )
            ;***
            ;*** We get here if key != 0
            ;*** else check if the escape key was hit
            ;***
               (if (== key 283)     ;*** if so, exit 
                  (= done 1)
               )
            )
         )
      );*** end while

      (delete_window)               ;*** close the window we've been using
      (delete_buffer asc_tabl_buf)  ;*** delete the buffer
      (set_buffer edit_buffer)      ;*** make the last buffer current
   )
   ;***
   ;*** All done!
   ;***
)

(macro page_1
   (
      (move_abs 1 1)
      (insert " Dec  Hex  Chr     Dec  Hex  Chr     Dec  Hex  Chr\n")
      (insert " ===  ===  ===     ===  ===  ===     ===  ===  ===\n")
      (insert " 000  00H  NUL     018  12H  DC2    036  24H   $ \n")
      (insert " 001  01H  SOH    019  13H  DC3    037  25H   % \n")
      (insert " 002  02H  STX    020  14H  DC4    038  26H   & \n")
      (insert " 003  03H  ETX    021  15H  NAK    039  27H   ' \n")
      (insert " 004  04H  EOT    022  16H  SYN    040  28H   ( \n")
      (insert " 005  05H  ENQ    023  17H  ETB    041  29H   ) \n")
      (insert " 006  06H  ACK    024  18H  CAN    042  2AH   * \n")
      (insert " 007  07H  BEL    025  19H  EM     043  2BH   + \n")
      (insert " 008  08H  BS  \   026  1AH  SUB     044  2CH   \, \n")
      (insert " 009  09H  HT      027  1BH  ESC \   045  2DH   - \n")
      (insert " 010  0AH  LF      028  1CH  FS  \   046  2EH   . \n")
      (insert " 011  0BH  VT     029  1DH  GS     047  2FH   / \n")
      (insert " 012  0CH  FF     030  1EH  RS     048  30H   0 \n")
      (insert " 013  0DH  CR      031  1FH  US     049  31H   1 \n")
      (insert " 014  0EH  SO     032  20H  SP      050  32H   2 \n")
      (insert " 015  0FH  SI     033  21H  !       051  33H   3 \n")
      (insert " 016  10H  DLE    034  22H  \"       052  34H   4 \n")
      (insert " 017  11H  DC1    035  23H  #       053  35H   5  ")
   )
)

(macro page_2
   (
      (move_abs 1 1)
      (insert " Dec  Hex  Chr     Dec  Hex  Chr     Dec  Hex  Chr\n")
      (insert " ===  ===  ===     ===  ===  ===     ===  ===  ===\n")
      (insert " 054  36H   6      072  48H   H      090  5AH   Z \n")
      (insert " 055  37H   7      073  49H   I      091  5BH   [ \n")
      (insert " 056  38H   8      074  4AH   J      092  5CH   \\ \n")
      (insert " 057  39H   9      075  4BH   K      093  5DH   ] \n")
      (insert " 058  3AH   :      076  4CH   L      094  5EH    ^ \n")
      (insert " 059  3BH   \;      077  4DH   M      095  5FH   _ \n")
      (insert " 060  3CH   <      078  4EH   N      096  60H   ` \n")
      (insert " 061  3DH   =      079  4FH   O      097  61H   a \n")
      (insert " 062  3EH   >      080  50H   P      098  62H   b \n")
      (insert " 063  3FH   ?      081  51H   Q      099  63H   c \n")
      (insert " 064  40H   @      082  52H   R      100  64H   d \n")
      (insert " 065  41H   A      083  53H   S      101  65H   e \n")
      (insert " 066  42H   B      084  54H   T      102  66H   f \n")
      (insert " 067  43H   C      085  55H   U      103  67H   g \n")
      (insert " 068  44H   D      086  56H   V      104  68H   h \n")
      (insert " 069  45H   E      087  57H   W      105  69H   i \n")
      (insert " 070  46H   F      088  58H   X      106  6AH   j \n")
      (insert " 071  47H   G      089  59H   Y      107  6BH   k  ")
   )
)

(macro page_3
   (
      (move_abs 1 1)
      (insert " Dec  Hex  Chr     Dec  Hex  Chr     Dec  Hex  Chr\n")
      (insert " ===  ===  ===     ===  ===  ===     ===  ===  ===\n")
      (insert " 108  6CH   l      126  7EH   ~      144  90H   � \n")
      (insert " 109  6DH   m      127  7FH         145  91H   ‘ \n")
      (insert " 110  6EH   n      128  80H   €      146  92H   ’ \n")
      (insert " 111  6FH   o      129  81H   �      147  93H   “ \n")
      (insert " 112  70H   p      120  82H   ‚      148  94H   ” \n")
      (insert " 113  71H   q      131  83H   ƒ      149  95H   • \n")
      (insert " 114  72H   r      132  84H   „      150  96H   – \n")
      (insert " 115  73H   s      133  85H   …      151  97H   — \n")
      (insert " 116  74H   t      134  86H   †      152  98H   ˜ \n")
      (insert " 117  75H   u      135  87H   ‡      153  99H   ™ \n")
      (insert " 118  76H   v      136  88H   ˆ      154  9AH   š \n")
      (insert " 119  77H   w      137  89H   ‰      155  9BH   › \n")
      (insert " 120  78H   x      138  8AH   Š      156  9CH   œ \n")
      (insert " 121  79H   y      139  8BH   ‹      157  9DH   � \n")
      (insert " 122  7AH   z      140  8CH   Œ      158  9EH   ž \n")
      (insert " 123  7BH   {      141  8DH   �      159  9FH   Ÿ \n")
      (insert " 124  7CH   |      142  8EH   Ž      160  A0H     \n")
      (insert " 125  7DH   }      143  8FH   �      161  A1H   ¡  ")
   )
)

(macro page_4
   (
      (move_abs 1 1)
      (insert " Dec  Hex  Chr     Dec  Hex  Chr     Dec  Hex  Chr\n")
      (insert " ===  ===  ===     ===  ===  ===     ===  ===  ===\n")
      (insert " 162  A2H   ¢      180  B4H   ´      198  C6H   Æ \n")
      (insert " 163  A3H   £      181  B5H   µ      199  C7H   Ç \n")
      (insert " 164  A4H   ¤      182  B6H   ¶      200  C8H   È \n")
      (insert " 165  A5H   ¥      183  B7H   ·      201  C9H   É \n")
      (insert " 166  A6H   ¦      184  B8H   ¸      202  CAH   Ê \n")
      (insert " 167  A7H   §      185  B9H   ¹      203  CBH   Ë \n")
      (insert " 168  A8H   ¨      186  BAH   º      204  CCH   Ì \n")
      (insert " 169  A9H   ©      187  BBH   »      205  CDH   Í \n")
      (insert " 170  AAH   ª      188  BCH   ¼      206  CEH   Î \n")
      (insert " 171  ABH   «      189  BDH   ½      207  CFH   Ï \n")
      (insert " 172  ACH   ¬      190  BEH   ¾      208  D0H   Ð \n")
      (insert " 173  ADH   ­      191  BFH   ¿      209  D1H   Ñ \n")
      (insert " 174  AEH   ®      192  C0H   À      210  D2H   Ò \n")
      (insert " 175  AFH   ¯      193  C1H   Á      211  D3H   Ó \n")
      (insert " 176  B0H   °      194  C2H   Â      212  D4H   Ô \n")
      (insert " 177  B1H   ±      195  C3H   Ã      213  D5H   Õ \n")
      (insert " 178  B2H   ²      196  C4H   Ä      214  D6H   Ö \n")
      (insert " 179  B3H   ³      197  C5H   Å      215  D7H   ×  ")
   )
)

(macro page_5
   (
      (move_abs 1 1)
      (insert " Dec  Hex  Chr     Dec  Hex  Chr     Dec  Hex  Chr\n")
      (insert " ===  ===  ===     ===  ===  ===     ===  ===  ===\n")
      (insert " 216  D8H   Ø      234  EAH   ê      252  FCH   ü \n")
      (insert " 217  D9H   Ù      235  EBH   ë      253  FDH   ý \n")
      (insert " 218  DAH   Ú      236  ECH   ì      254  FEH   þ \n")
      (insert " 219  DBH   Û      237  EDH   í      255  FFH     \n")
      (insert " 220  DCH   Ü      238  EEH   î                   \n")
      (insert " 221  DDH   Þ      239  EFH   ï                   \n")
      (insert " 222  DEH   Þ      240  F0H   ð                   \n")
      (insert " 223  DFH   ß      241  F1H   ñ                   \n")
      (insert " 224  E0H   à      242  F2H   ò                   \n")
      (insert " 225  E1H   á      243  F3H   ó                   \n")
      (insert " 226  E2H   â      244  F4H   ô                   \n")
      (insert " 227  E3H   ã      245  F5H   õ                   \n")
      (insert " 228  E4H   ä      246  F6H   ö                   \n")
      (insert " 229  E5H   å      247  F7H   ÷                   \n")
      (insert " 230  E6H   æ      248  F8H   ø                   \n")
      (insert " 231  E7H   ç      249  F9H   ù                   \n")
      (insert " 232  E8H   è      250  FAH   ú                   \n")
      (insert " 233  E9H   é      251  FBH   û                    ")
   )
)
