/* filename: PrintEnvelope2                                                  */
/*  version: 1.0       03 Aug 96                                             */
/*                                                                           */
/*  purpose: FinalWriter macro to print a return and recipient address on a  */
/*           single-fed envelope on an HP540 (or other HP printers which     */
/*           support single-fed #10 envelopes). This would most often be     */
/*           used from within a document being created which will be         */
/*           mailed to someone, using a business format where their          */
/*           address is part of the document.                                */
/*                                                                           */
/*   syntax: called from within FinalWriter                                  */
/*                                                                           */
/*   author: Jim Dutton                                                      */
/*           jimd@slip106.termserv.siu.edu                                   */
/*                                                                           */
/*  prereqs: none                                                            */
/*                                                                           */
/*    usage: create FW document with both the sending and receiving          */
/*           addresses as part of the document, in a format similar to:      */
/*                                                                           */
/*              My Name                                                      */
/*              My Address (one or more lines)                               */
/*                                                                           */
/*              Recipient Name                                               */
/*              Recipient Address (one or more lines)                        */
/*                                                                           */
/*              <text>                                                       */
/*                                                                           */
/*           highlight both address blocks, and invoke this macro            */
/*                                                                           */
/* comments: the UNIVERS font is preselected - the envelope print setup is   */
/*           predetermined by the printer - only use envelopes that your     */
/*           printer supports                                                */
/*                                                                           */
/*           all commands and command values are SPECIFIC to the HP540       */
/*           printer - other printers may have DIFFERENT commands and/or     */
/*           values which the USER must determine                            */
/*                                                                           */
/*           the From and To address information is extracted from an        */
/*           existing document - this macro does NOT open a new document     */
/*           wherein you would create addressing data seperately             */
/*                                                                           */
/*           there MUST be at least one blank line inbetween the addresses   */
/*                                                                           */
/*           to remove any part of a line in the addresses, such as          */
/*           right-justified date field, precede that part with a carat      */
/*           (shift-6) symbol - remember to remove the carat after printing  */
/*           the envelope                                                    */


/* open up Amiga PARallel device which connects to HP540 */
If ~open(hp540,'PAR:')  then
  Do; say '** Unable to open PARallel port **'; Exit 32; End

/* initialize variables and operating environment */
Address value address();  Options Results

newline = '0a'x; i = 0; fromaddress = ""; toaddress = ""; addresslines. = ""
ignore_sep = "^";  esc = '1B'x; reset = esc || "E"

/* the following data comes from the "Technical Reference Guide for the */
/* HP DeskJet 500 series Printers", (C2170-90099)   800-538-8787 (US)   */

line_termination       = esc || "&k3G"             /* CR=LF=CR+LF; FF=CR+FF */
univers                = esc || "(s1p12v0s0b52T"   /* 14pt, upright, normal, Universal font */
paper_size.envelope    = esc || "&l81A"            /* #10 envelope in envelope feeder */
EnvelopeFeed           = esc || "&l3H"             /* paper input control set to envelope */
LeftMargin50_Skip8Rows = esc || "&a40l&a+10R"      /* left margin=40; CAP move 10 rows down */

/* let's talk - ready to go ? */
ShowMessage 1 0 '"Make sure that BOTH the From and To addresses are highlighted."',
                '"If you want to ignore any subpart of a line, precede it with a carat symbol."',
                '"Are you ready to print the envelope ?"',
                '"Yes" "Cancel" ""'
If result = 2  then  Exit 18

/* get highlighted addresses */
'Extract'; addressblock = result

/* debugging output - uncomment to see exactly what was extracted */
/* say; say 'addressblock = "'addressblock'"'; Exit 48 */

/* seperate FROM subblock and TO subblock - must be at least 1 blank line inbetween */
If length(addressblock) = 1  then
  Do
    ShowMessage 1 0 '"No address text highlighted - quiting PrintEnvelop2 macro" "" ""',
                    '"OK" "" ""'
    Exit 22
  End

/* put blocked data into seperate lines & strip parts to right of carat */
Do while addressblock ~= ""
  i = i + 1; parse var addressblock addresslines.i(newline)addressblock
  loc = pos(ignore_sep,addresslines.i)
  If loc > 0  then    addresslines.i = strip(substr(addresslines.i,1,loc-1))
End  

/* build From address (ie; My Name, My Address) */
curr_addr_line = addresslines.1;  j = 1
Do while curr_addr_line ~= ""
  fromaddress = fromaddress || curr_addr_line || newline
  j = j + 1;  curr_addr_line = addresslines.j
End

If j > i  then
  Do
    ShowMessage 1 0 '"Either only one address was selected, or there"',
                    '"is a blank line missing inbetween them."',
                    '"Quiting PrintEnvelope2 macro ...."',
                    '"OK" "" ""'
    Exit 34
  End

/* skip required blank line(s) and then build To address */
Do while curr_addr_line = ""
  j = j + 1;  curr_addr_line = addresslines.j
End

Do while curr_addr_line ~= ""
  toaddress = toaddress || curr_addr_line || newline
  j = j + 1;  curr_addr_line = addresslines.j
End

/* debugging output */
/* say 'toaddress = "'toaddress'"' */

/* initialize HP540 printer */
duh = writech(hp540,line_termination)
duh = writech(hp540,univers)
duh = writech(hp540,paper_size.envelope)
duh = writech(hp540,EnvelopeFeed)

/* print FROM address in indented, top left corner */
duh = writech(hp540,fromaddress)

/* print TO address in more or less centered, center */
duh = writech(hp540,LeftMargin50_Skip8Rows)
duh = writech(hp540,toaddress)

duh = writech(hp540,reset);  duh = close(hp540)
Exit