home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Professional / OS2PRO194.ISO / os2 / wps / editor / epmtools / epmmac / stdprocs.e < prev    next >
Encoding:
Text File  |  1993-07-29  |  58.4 KB  |  1,762 lines

  1. compile if EVERSION >= 4 & EVERSION < '5.51'  -- 5.51 & above define this internally.
  2. defproc address(var varname) =
  3.    return selector(varname) || offset(varname)
  4. compile endif
  5.  
  6. ; A useful subroutine:  asks "Are you sure (Y/N)?" (same as DOS's prompt
  7. ; after "erase *.*") and returns uppercase keystroke.
  8. ; If called with a string parameter, displays it at start of prompt, e.g.
  9. ;   usersays = askyesno("About to erase.")
  10. ;   ==>   "About to erase. Are you sure (Y/N)? "
  11. ; EPM 5.12B:  Now enabled for EPM, using entrybox().  Optional second argument
  12. ; is a flag to prevent the "Are you sure" from being appended.
  13. ; EPM 5.15:  Now uses WinMessageBox to get Yes, No buttons.  [\toolktxx\c\include\pmwin.h]
  14. defproc askyesno
  15.    prompt=arg(1)
  16. compile if EVERSION < 5
  17.    if not arg(2) then
  18.       prompt=prompt || ARE_YOU_SURE_YN__MSG
  19.    endif
  20.    return upcase(mgetkey(prompt))     /* Accept key from macro. */
  21. compile else
  22.    if not arg(2) then
  23.       prompt=prompt\13 || ARE_YOU_SURE__MSG
  24.    endif
  25.    return substr(YES_CHAR || NO_CHAR, winmessagebox('', prompt, 16388) - 5, 1)  -- YESNO + MOVEABLE
  26. compile endif
  27.  
  28.  
  29. compile if EVERSION >= 4
  30. ; Does an atol of its argument, then a word reversal and returns the result.
  31. defproc atol_swap(num)
  32.    hwnd=atol(num)
  33.  compile if EVERSION >= '5.17'
  34.    return rightstr(hwnd,2) || leftstr(hwnd,2)
  35.  compile else
  36.    return substr(hwnd,3,2) || substr(hwnd,1,2)
  37.  compile endif
  38. compile endif
  39.  
  40.  
  41. defproc checkmark()        /* Common routine, save space.  from Jim Hurley.*/
  42.   if marktype()='' then
  43. compile if EPM
  44.     sayerror NO_MARK_HERE__MSG
  45. compile else
  46.     sayerror NO_MARK__MSG
  47. compile endif
  48.     stop
  49.   endif
  50.  
  51. ; Routine to tell if a mark is visible on the screen.  (Actually, only on the
  52. ; current window; if the window is less than full size, a mark could be visible
  53. ; in an inactive window without our being able to tell.)  Also, if a character
  54. ; mark begins above the top of the window and ends below the bottom, and the
  55. ; window contains only blank lines, then this routine will return 1 (since the
  56. ; mark spans the window) even though no sign of the mark will be visible.
  57. defproc check_mark_on_screen =
  58.    if marktype() = '' then return 0; endif  -- If no mark, then not on screen.
  59.    getmark first_mark_line, last_mark_line, first_mark_col, last_mark_col
  60.    first_screen_line = .line - .cursory + 1
  61.    last_screen_line = .line - .cursory + .windowheight
  62.    if last_mark_line < first_screen_line then return 0; endif
  63.    if first_mark_line > last_screen_line then return 0; endif
  64.    no_char_overlap = marktype()<>'CHAR' or first_mark_line=last_mark_line
  65.    if last_mark_col < .col - .cursorx + 1 and
  66.       (no_char_overlap or last_mark_line=first_screen_line)
  67.    then return 0; endif
  68.    if first_mark_col > .col - .cursorx + .windowwidth and
  69.       (no_char_overlap or first_mark_line=last_screen_line)
  70.    then return 0; endif
  71.    return 1
  72.  
  73. ; Tests whether the "filename" is actually a printer
  74. ; device, so we'll know whether to test printer readiness first.
  75. ; Called by savefile() in SAVELOAD.E.  Returns 0 if not, else printer number.
  76. defproc check_for_printer(name)
  77.    if not name then return 0; endif
  78. compile if EVERSION >= '5.50'
  79.    if leftstr(name,1)='"' & rightstr(name,1)='"' then
  80.       name=substr(name,2,length(name)-2)
  81.    endif
  82. compile endif
  83. compile if EVERSION >= '5.17'
  84.    if rightstr(name,1) = ':' then  -- a device
  85.       name = leftstr(name,length(name)-1)
  86. compile else
  87.    if substr(name,length(name),1) = ':' then  -- a device
  88.       name = substr(name,1,length(name)-1)
  89. compile endif
  90.    else       -- Might be a full pathspec, C:\EDIT\PRN, and still go to a device!
  91.       indx = lastpos('\',name)
  92.       if not indx then indx = lastpos(':',name) endif
  93.       if indx then name=substr(name,indx+1) endif
  94.       indx = pos('.',name)
  95.       if indx then name=substr(name,1,indx-1) endif
  96.    endif
  97.    if upcase(name)='PRN' then return 1; endif
  98. compile if EVERSION >= 4  -- Check_for_printer always returns true, so we don't need to distinguish COMn.
  99.    return (4+pos('.'upcase(name)'.','.LPT1.LPT2.LPT3.LPT4.LPT5.LPT6.LPT7.LPT8.LPT9.COM1.COM2.COM3.COM4.')) % 5
  100. compile else
  101.    return (4+pos('.'upcase(name)'.','.LPT1.LPT2.LPT3.')) % 5
  102. compile endif
  103.  
  104. compile if WANT_WINDOWS
  105. ; This proc is called only by DEFC EDIT in messy-desk mode.
  106. defproc create_window_for_each_file(emptyfileid)
  107.    fileidlist=''
  108.    activatefile emptyfileid /* Start list at beginning so we get 'em all.    */
  109.    nextfile                 /* Except first one, can leave one in each ring. */
  110.    loop
  111.       nextfile
  112.       .box=1
  113.       getfileid fileid
  114.       if fileid=emptyfileid then
  115.          leave
  116.       endif
  117.       fileidlist=fileidlist fileid
  118.    endloop
  119.    rest=fileidlist
  120.    loop
  121.       parse value rest with fileid rest
  122.       if fileid='' then
  123.          leave
  124.       endif
  125.       rc=0
  126.       newwindow fileid
  127.       if rc then leave endif
  128.       getfileid cur_fileid
  129.       activatefile fileid
  130.       quitview
  131.       activatefile cur_fileid
  132.    endloop
  133. compile endif
  134.  
  135.  
  136. COMPILE IF EVERSION >= 4
  137. defproc dec_to_string(string)    -- for dynalink usage
  138.    line = ''
  139.    for i = 1 to length(string)
  140.      line= line || asc(substr(string,i,1)) || ' '
  141.    endfor
  142.    return line
  143. COMPILE ENDIF
  144.  
  145. define
  146. compile if EVERSION >= '5.20'  -- 5.20 adds a HINI to the *Profile calls.
  147.    HINI_PARM = 'HINI_PROFILE,'
  148. compile else
  149.    HINI_PARM = ' '
  150. compile endif
  151.  
  152. defproc default_printer
  153. compile if defined(my_printer)
  154.    return MY_PRINTER
  155. compile elseif EPM
  156.    parse value queryprofile($HINI_PARM 'PM_SPOOLER', 'PRINTER') with printername ';'
  157.    if printername<>'' then
  158.       parse value queryprofile($HINI_PARM 'PM_SPOOLER_PRINTER', printername) with dev ';'
  159.       if dev<>'' then return dev; endif
  160.    endif
  161. compile endif
  162.    return 'LPT1'
  163.  
  164. ;  Returns DOS version number, multiplied by 100 so we can treat
  165. ;  it as an integer string.  That is, DOS 3.2 is reported as "320".
  166. ;  Needed by DEFPROC SUBDIR.
  167.  
  168. defproc dos_version()
  169. compile if E3
  170.    parse value int86x(DOS_INT,DOS_GET_VERSION,'') with ax .
  171.    major = ax // 256                  /* AL = major version number */
  172. ;  minor = (ax - major) % 256
  173.    return 100*major + (ax - major) % 256
  174. compile elseif EPM32
  175.       verbuf = copies(\0,8)
  176.       res= dynalink32('DOSCALLS',          /* dynamic link library name */
  177.                      '#348',              /* ordinal for DOS32QuerySysInfo */
  178.                      atol(11)         ||  -- Start index (Major version number)
  179.                      atol(12)         ||  -- End index (Minor version number)
  180.                      address(verbuf)  ||  -- buffer
  181.                      atol(8),2)           -- Buffer length
  182. ;     major = ltoa(leftstr(verbuf,4),10)
  183. ;     minor = ltoa(rightstr(verbuf,4),10)
  184.       return 100*ltoa(leftstr(verbuf,4),10) + ltoa(rightstr(verbuf,4),10)
  185. compile else
  186.       verbuf = 'nn'
  187.       res= dynalink('DOSCALLS',          /* dynamic link library name */
  188.                     '#92',               /* ordinal for DOSGETVERSION */
  189.                     address(verbuf))
  190. ;     major = asc(substr(verbuf,2,1))
  191. ;     minor = asc(substr(verbuf,1,1))
  192.       return 100*asc(substr(verbuf,2,1)) + asc(substr(verbuf,1,1))
  193. compile endif
  194.  
  195.  
  196. compile if WANT_ET_COMMAND     -- Let user omit ET command.
  197. defproc ec_position_on_error(tempfile)   /* load file containing error */
  198.    'xcom e 'tempfile
  199.    if rc then    -- Unexpected error.
  200.       sayerror ERROR_LOADING__MSG tempfile
  201.       if rc=-282 then 'xcom q'; endif  -- sayerror('New file')
  202.       return
  203.    endif
  204.    if .last<=4 then
  205.       getline msg,.last
  206.       'xcom q'
  207.    else
  208.       getline msg,2
  209. compile if EPM
  210.       if leftstr(msg,3)='(C)' then  -- 5.20 changed output
  211.          getline msg,4
  212.       endif
  213. compile endif
  214.       getline temp,.last
  215.       parse value temp with 'col= ' col
  216.       getline temp,.last-1
  217.       parse value temp with 'line= ' line
  218.       getline temp,.last-2
  219.       parse value temp with 'filename=' filename
  220.       'xcom q'
  221.       'e 'filename               -- not xcom here, respect user's window style
  222.       if line<>'' and col<>'' then
  223. compile if EPM
  224.          .cursory=min(.windowheight%2,.last)
  225. compile else
  226.          .cursory=15
  227. compile endif
  228.          if col>0 then
  229.             .col=col
  230.             line
  231.          else
  232.             .line=line-1   /* sometimes the compiler is off by 1 */
  233.             getline s
  234.             .col=length(s) /* position cursor at end of previous line */
  235.          endif
  236.       endif
  237.    endif
  238.    sayerror msg
  239. compile endif
  240.  
  241. defproc einsert_line
  242.    insert
  243.    up
  244.    getline line
  245.    parse value pmargins() with leftcol . paracol .
  246.    if line='' or not .line then
  247.       .col=paracol
  248.    else
  249.       call pfirst_nonblank()
  250.       if .col=paracol then .col=leftcol; endif
  251.    endif
  252.    down
  253.  
  254. compile if ENHANCED_ENTER_KEYS
  255. defproc enter_common(action)
  256.  compile if WANT_STREAM_MODE = 'SWITCH'
  257.    universal stream_mode
  258.    if stream_mode then
  259.  compile endif
  260.  compile if WANT_STREAM_MODE
  261.       if .line then
  262.          split
  263.          .col=1
  264.          down
  265.       else
  266.          insert
  267.          .col=1
  268.       endif
  269.       return
  270.  compile endif
  271.  compile if WANT_STREAM_MODE = 'SWITCH'
  272.    endif
  273.  compile endif
  274.  compile if WANT_STREAM_MODE <> 1
  275.    if .line = .last  & (action=3 | action=5) then  -- 'ADDATEND' | 'DEPENDS+'
  276.       call einsert_line()
  277.       down                       -- This keeps the === Bottom === line visible.
  278.       return
  279.    endif
  280. ;     'NEXTLINE' 'ADDATEND'                        'DEPENDS'  'DEPENDS+'
  281.    if action=2 | action=3 | (not insert_state() & (action=4 | action=5)) then
  282.       down                          -- go to next line
  283.       begin_line
  284.       return
  285.    endif
  286.    if action=6 then
  287.       call splitlines()
  288.       call pfirst_nonblank()
  289.       down
  290. ;;    refresh
  291.       return
  292.    endif
  293.    call einsert_line()           -- insert a line
  294.  compile endif  -- WANT_STREAM_MODE <> 1
  295. compile endif
  296.  
  297. ;  Erasetemp erases a file quietly (no "File not found" message) on both DOS
  298. ;  and OS/2.  Thanks to Larry Margolis.  Returns 0 if successful erase, or
  299. ;  the error code (if on DOS) which will usually be 2 for 'file not found'.
  300. defproc erasetemp(filename)
  301.    asciiz = filename\0
  302. compile if E3
  303.    call free()    -- Keep variables from moving around before int86x.
  304.    parse value int86x(DOS_INT,DOS_UNLINK 0 0 ofs(asciiz), seg(asciiz)) with ax . . . . . cf ',' .
  305.    -- Most callers will ignore error code, don't care file doesn't exist.
  306.    -- if cf then sayerror 'DOS error code' ax endif
  307.    if cf then return ax; endif
  308. compile elseif EPM32
  309.    return dynalink32('DOSCALLS',          /* dynamic link library name */
  310.                     '#259',               /* ordinal value for DOSDELETE */
  311.                     address(asciiz) )
  312. compile else
  313.    return dynalink('DOSCALLS',          /* dynamic link library name */
  314.                    '#60',               /* ordinal value for DOSDELETE */
  315.                    address(asciiz) ||
  316.                    atoi(0)         ||   /* reserved                  */
  317.                    atoi(0))             /* reserved                  */
  318. compile endif
  319.  
  320. compile if EPM
  321. defproc find_token(var startcol, var endcol)  -- find a token around the cursor.
  322.    if arg(3)='' then
  323.       token_separators = ' ~`!%^&*()-+=][{}|\:;?/><,'''\t
  324.    else
  325.       token_separators = arg(3)
  326.    endif
  327.    if arg(4)='' then
  328.       diads = '-> ++ -- << >> <= >= && || += -= *= /= %= ¬= &= |='
  329.    else
  330.       diads = arg(4)
  331.    endif
  332.    getline line
  333.    len = length(line)
  334.    if .col>len | pos(substr(line, .col, 1), ' '\t) then
  335.       return  -- Past end of line, or over whitespace
  336.    endif
  337.    endcol = verify(line, token_separators, 'M', .col)
  338.    if endcol = .col then  -- On an operator.
  339.       startcol = endcol
  340.       if wordpos(substr(line, startcol, 2), diads) then
  341.          endcol = endcol + 1  -- On first character
  342.       elseif .col > 1 then
  343.          if wordpos(substr(line, endcol-1, 2), diads) then
  344.             startcol = startcol - 1  -- -- On last character
  345.          endif
  346.       endif
  347.       return 2
  348.    endif
  349.    if endcol then
  350.       endcol = endcol - 1
  351.    else
  352.       endcol = len
  353.    endif
  354.    startcol = verify(reverse(line), token_separators, 'M', len - .col + 1)
  355.    if startcol then
  356.       startcol = len - startcol + 2
  357.    else
  358.       startcol = 1
  359.    endif
  360.    return 1
  361. compile endif
  362.  
  363. ; Note on a speed trick:  The following routine is used to both verify that
  364. ; an external program exists, and to get its path.  After that first search,
  365. ; the exact path location of the routine is known; it can be remembered so that
  366. ; all future calls can supply the exact location to avoid the path search.
  367. ; See SUBDIR for an example of its use.
  368.  
  369. defproc find_routine(utility)  -- Split from SUBDIR
  370.    parse arg util opts         -- take first word, so can pass options too.
  371.    findfile fully_qualified,util,'PATH','P'
  372.    if rc then return -1 endif
  373. compile if E3
  374.    if dos_version() < 300 then
  375.       return utility             --DOS 2 can't handle the path
  376.    endif                         --in front of the command.
  377. compile endif
  378.    return fully_qualified opts
  379.  
  380. compile if EVERSION >='5.50'    -- For GPI version, we must manage the cursor ourself
  381. defproc fixup_cursor()
  382.  compile if UNDERLINE_CURSOR
  383.    cursorh = 3 - 67*insert_state()         -- 0 -> 3; 1 -> -64
  384.    cursorw = '-128'
  385.  compile else
  386.    cursorw = 2 - 130*(not insert_state())  -- 0 -> -128; 1 -> 2
  387.    cursorh = '-128'
  388.  compile endif
  389.    cursor_dimensions cursorw, cursorh
  390. compile endif
  391.  
  392. compile if EVERSION < 5
  393. defproc init_operation_on_commandline
  394.    universal comsfileid,oldline
  395.    if command_state() then
  396.       activatefile comsfileid
  397.       oldline=.line
  398.       getcommand line,col,scrollpos
  399.       insertline line,.last+1
  400.       .cursorx=col-scrollpos+1
  401.       .col=col
  402.       bottom
  403.    endif
  404. compile endif
  405.  
  406. ; Returns true if parameter given is a number.
  407. ; Leading and trailing spaces are ignored.
  408. defproc isnum
  409.    zzi=pos('-',arg(1))           -- Optional minus sign?
  410.    if zzi then                   -- If there is one,
  411.       parse arg zz1 '-' zz zz2   --   zz1 <- before it, zz <- number, zz2 <- after
  412.    else
  413.       parse arg zz zz1 zz2       --   zz <- number; zz1, zz2 <- after it
  414.    endif
  415.    zz=strip(zz)                  -- Delete leading & trailing spaces.
  416.    if zz1||zz2 <> '' or          -- If there were more tokens on the line
  417.       zz==''                     -- or if the result is null
  418.    then return 0 endif           -- then not a number.
  419. compile if EVERSION >= 4         -- OS/2 version - real numbers
  420.    if pos(DECIMAL,zz) <> lastpos(DECIMAL,zz) then return 0 endif
  421.                                  -- Max. of one decimal point.
  422.    return not verify(zz,'0123456789'DECIMAL)
  423. compile else                        -- DOS version - integers only
  424.    return not verify(zz,'0123456789')
  425. compile endif
  426.  
  427. defproc isoption(var cmdline,optionletter)
  428.    i=pos(argsep||upcase(optionletter),upcase(cmdline))
  429.    if i then
  430. compile if EPM
  431.       cmdline=delstr(cmdline,i,2)
  432. compile else
  433.       cmdline=substr(cmdline,1,i-1)||substr(cmdline,i+2)
  434. compile endif
  435.       return 1
  436.    endif
  437.  
  438. defproc joinlines()
  439.    if .line<.last and .line then
  440. compile if EPM           -- Can't use REPLACELINE because it wipes out attributes.
  441.       oldcol = .col
  442.       down                   -- ensure one space at start of second line
  443.       call pfirst_nonblank()
  444.       col2 = .col
  445.       .col = 1
  446.       getsearch savesearch
  447.       if col2 > 2 then       -- Shift line left
  448.          'xcom c/'copies(' ',col2-2)'//'  -- Change first n-1 blanks to null
  449.       elseif col2=1 then     -- Shift line right
  450.          'xcom c/^/ /g'         -- insert a space at beginning of line
  451.       endif
  452.       setsearch savesearch
  453.       up                     -- ensure no spaces at end of first line
  454.       .col = length(strip(textline(.line),'T')) + 1
  455.       erase_end_line
  456.       .col = oldcol
  457. compile else           -- E3 and EOS2 can still use the old, simple way.
  458.       /* remove all but one trailing space of current line */
  459.       getline line
  460.       replaceline strip(line,'T')' '
  461.       /* remove all leading spaces of next line */
  462.       getline line,.line+1
  463.       replaceline strip(line),.line+1
  464. compile endif
  465.       join
  466.    endif
  467.  
  468. compile if EVERSION < 5
  469. defproc leave_last_command
  470.    if (not arg() or arg(2)) and arg(1) then
  471.       cursor_command
  472.       up
  473.       for i = 1 to arg(1)-1
  474.          right
  475.       endfor
  476.    endif
  477. compile endif
  478.  
  479. compile if WANT_LAN_SUPPORT
  480. defproc lock
  481.    file=.filename\0
  482.  compile if EPM32
  483.    newhandle='????'
  484.    actiontaken=atol(1)   -- File exists
  485.    result = dynalink32('DOSCALLS',
  486.                       '#273',                     /* dos32open          */
  487.                       address(file)         ||
  488.                       address(newhandle)    ||
  489.                       address(actiontaken)  ||
  490.                       atol(0)    ||       -- file size
  491.                       atol(0)    ||       -- file attribute
  492.                       atol(17)   ||       -- open flag; open if exists, else create
  493.                       atol(146)  ||       -- openmode; deny Read/Write
  494.                       atol(0),2)
  495.  compile else
  496.    newhandle='??'
  497.    actiontaken=atoi(1)   -- File exists
  498.    result = dynalink('DOSCALLS',
  499.                      '#70',                     /* dosopen          */
  500.                      address(file)        ||
  501.                      address(newhandle)   ||
  502.                      address(actiontaken) ||
  503.                      atol(0)              || -- file size
  504.                      atoi(0)              || -- file attribute
  505.                      atoi(17)             || -- open flag; open if exists, else create
  506.                      atoi(146)            || -- openmode; deny Read/Write
  507.                      atol(0))
  508.  compile endif
  509.    if result then
  510. ;     'quit'  /* quit since the file could not be locked */
  511.       messageNwait('DOSOPEN' ERROR__MSG result NOT_LOCKED__MSG)
  512.       return result
  513.    endif
  514.  compile if EPM32
  515.    if newhandle = \0\0\0\0 then  -- Handle of 0 - bad news
  516.       newhandle2=\255\255\255\255
  517.       result = dynalink32('DOSCALLS',
  518.                          '#260',                     /* Dos32DupHandle     */
  519.                          newhandle ||
  520.                          address( newhandle2 ), 2)
  521.       call dynalink32('DOSCALLS',    -- Free the original handle
  522.                      '#257',                    -- dos32close
  523.                      newhandle, 2)
  524.       if result then
  525.          messageNwait('DosDupHandle' ERROR__MSG result NOT_LOCKED__MSG)
  526.          return result
  527.       endif
  528.       newhandle = newhandle2
  529.    endif
  530.    .lockhandle=ltoa(newhandle,10)
  531.  compile else
  532.    if newhandle = \0\0 then  -- Handle of 0 - bad news
  533.       newhandle2=atoi(65535)
  534.       result = dynalink('DOSCALLS',
  535.                         '#61',                     /* DosDupHandle     */
  536.                         newhandle ||
  537.                         address( newhandle2 ))
  538.       call dynalink('DOSCALLS',    -- Free the original handle
  539.                     '#59',                    -- dosclose
  540.                     newhandle)
  541.       if result then
  542.          messageNwait('DosDupHandle' ERROR__MSG result NOT_LOCKED__MSG)
  543.          return result
  544.       endif
  545.       newhandle = newhandle2
  546.    endif
  547.    .lockhandle=itoa(newhandle,10)
  548.  compile endif
  549. compile endif
  550.  
  551. defproc max(a,b)  -- Support as many arguments as E3 will allow.
  552.    maximum=a
  553.    do i=2 to arg()
  554.       if maximum<arg(i) then maximum=arg(i); endif
  555.    end
  556.    return maximum
  557.  
  558. compile if E3
  559. definit  /* Keep this definit close to the proc it serves. */
  560.    universal lines_entered
  561.    lines_entered=0
  562.  
  563. defproc maybe_autosave    -- For E3 users, this routine increments the autosave
  564.    universal autosave,lines_entered  -- counter, and does an autosave if necessary.
  565.    if autosave then
  566.       lines_entered = lines_entered +1
  567.       if lines_entered >= autosave then
  568.          'xcom save' MakeTempName()  -- Don't worry about HPFS files in E3.
  569.          if rc=-2  then  -- sayerror('File not found') -> Invalid filename
  570.             'xcom save' MakeTempName('BAD-NAME')
  571.          endif
  572.          .modify=1                  /* Reraise the modify flag. */
  573.          lines_entered =0
  574.       endif
  575.    endif
  576. compile endif
  577.  
  578.  
  579. compile if BACKUP_PATH <> ''
  580. ;  Procedure to pick a filename for backup purposes, like STDPROCS.E$.
  581. defproc MakeBakName
  582.    name = arg(1)
  583.    if name = '' then   /* if no arg given, default to current filename */
  584.       name = .filename
  585.    endif
  586.    -- Change name as little as possible, but enough to identify it as
  587.    -- a noncritical file.  Replace the last character with '$'.
  588.    ext = filetype(name)
  589.    if length(ext)=3 then
  590.       ext = substr(ext,1,2)'$'
  591.    else
  592.       ext = ext'$'
  593.    endif
  594.    -- We still use MakeTempName() for its handling of host names.
  595.    bakname = MakeTempName(name)
  596.    i=lastpos('\',bakname)       -- but with a different directory
  597.    if i then
  598.       bakname = substr(bakname,i+1)
  599.    endif
  600.    parse value bakname with fname'.'.
  601.  compile if BACKUP_PATH = '='
  602.    bakname = fname'.'ext
  603.    i=lastpos('\',name)       -- Use original file's directory
  604.    if i then
  605.       bakname = substr(name,1,i) || bakname
  606.    endif
  607.  compile else
  608.    bakname = BACKUP_PATH || fname'.'ext
  609.  compile endif
  610.    return bakname
  611. compile endif
  612.  
  613.  
  614. ;  Procedure to pick a temporary filename like ORIGNAME.$$1.
  615. ;  First argument is the filename, 2nd is the fileid.  Both are optional,
  616. ;  default to the current filename and fileid if absent.
  617. ;  Revised by BTTUCKER to catch all cases and work with E3EMUL.
  618. defproc MakeTempName
  619.    universal vAUTOSAVE_PATH
  620.    TempName  = arg(1)
  621.    extension = arg(2)
  622.    if TempName = '' then   /* if no arg given, default to current filename */
  623.       TempName = .filename
  624.    endif
  625.    if TempName = '' then
  626.       TempName = '$'       /* new file? o.k. then $  */
  627.    else /* We want only PC file name, VM filename, or MVS firstname          */
  628.         /* These next statements will strip everything else off...           */
  629.      p = lastpos('\',TempName)                      /* PC filename with path */
  630.      if p then TempName=substr(TempName,p+1) endif
  631.      p = pos('.',TempName)                          /* PC or MVS filename    */
  632.      if p then TempName=substr(TempName,1,p-1) endif
  633.      p = pos(' ',TempName)                          /* VM filename (or HPFS) */
  634.      if p then TempName=substr(TempName,1,p-1) endif
  635.      p = pos(':',TempName)                          /* VM or MVS filename    */
  636.      if p then TempName=substr(TempName,p+1) endif
  637.      p = pos("'",TempName)                          /* MVS filename          */
  638.      if p then TempName=substr(TempName,p+1) endif
  639.      if length(tempname)>8 then tempname=substr(tempname,1,8); endif  /* HPFS names */
  640.    endif
  641.  
  642.    -- TempName might still be blank, as for '.Unnamed file'.
  643.    if TempName='' then TempName='$'; endif
  644.  
  645.    TempName = vAUTOSAVE_PATH || TempName
  646.    if extension='' then            /* default is current fileid              */
  647.       getfileid extension
  648.    endif
  649. compile if EVERSION < 5
  650.    extension = '$$' || extension
  651. compile else
  652.    /* In EPM we can have the same filename in multiple edit windows without
  653.     * knowing it, because different edit windows are actually separate
  654.     * instances of the editor.  So try to make the tempname unique by
  655.     * combining the window handle with the fileid.  Combine two middle
  656.     * digits of the window handle with the last digit of the fileid.
  657.     */
  658.    extension = substr(getpminfo(EPMINFO_EDITCLIENT),2,2) || extension
  659. compile endif
  660.    if length(extension)>3 then     /* could be >one digit, or something else */
  661.       extension=substr(extension,2,3)
  662.    endif
  663.    return TempName'.'extension
  664.  
  665. defproc message
  666.    getfileid fileid
  667.    sayerror arg(1)
  668.    activatefile fileid
  669.  
  670. ; Print message and wait for a key press.
  671. ; Preserve active file and activate ring.
  672. ; Note:  There is no need to use "call" to invoke this procedure,  since it
  673. ; returns the null string.  Execution of a null string does nothing
  674. defproc messageNwait
  675.    getfileid zzfileid
  676. compile if EPM
  677.    display -4                    -- Force a messagebox popup from the SAYERROR
  678. compile endif
  679.    sayerror arg(1)
  680. compile if EVERSION < 5
  681.    call getkey()
  682. compile else
  683.    display 4
  684. compile endif
  685.    activatefile zzfileid
  686.  
  687. compile if EVERSION < 5
  688. ; Mgetkey() acts the same as a call to getkey(), but first checks
  689. ; whether we're in mid-execution of a key-string (Ctrl-R/Ctrl-T).
  690. ; If so it gets the next key from the string.  Call this in place of
  691. ; getkey() if you want the user to be able to record the response.
  692. ; Don't call this for unusual inputs, such as messageNwait after errors.
  693.  
  694. ; Optional argument is prompt string, will be displayed on status line.
  695.  
  696. defproc mgetkey()
  697.    universal Kstring,inKstring          /* See c_r in STDKEYS.E. */
  698.    prompt=arg(1)
  699.    if prompt<>'' and inKstring<=0 then
  700.       sayerror prompt
  701.    endif
  702.    if inKstring=0 then     /* If not recording or replaying, normal input. */
  703.       k=getkey()
  704.    elseif inKstring=-1 then /* If recording, stash key in string.          */
  705.       k=getkey()
  706.       Kstring=Kstring||k   /* Trust that it doesn't get longer than 255.   */
  707.    else           /* inKstring>0 ==> replaying; get next key from Kstring. */
  708.       k=substr(Kstring,inKstring,1)
  709.       ksize=1
  710.       if k==substr(esc,1,1) then       /* extended key ? */
  711.          k=substr(Kstring,inKstring,2) /* Yes, 2 bytes for extended key.   */
  712.          ksize=2
  713.       endif
  714.       inKstring=inKstring+ksize        /* bump index AFTER execution */
  715.    endif
  716.    if prompt<>'' and inKstring<=0 then
  717.       sayerror 0
  718.    endif
  719.    return k
  720. compile endif
  721.  
  722. defproc min(a,b)  -- Support as many arguments as E3 will allow.
  723.    minimum=a
  724.    do i=2 to arg()
  725.       if minimum>arg(i) then minimum=arg(i); endif
  726.    end
  727.    return minimum
  728.  
  729. compile if EVERSION < 5
  730. defproc move_results_to_commandline
  731.    universal oldline
  732.    if command_state() then
  733.       getline line
  734.       deleteline
  735.       setcommand line,.col,.col-.cursorx+1
  736.       oldline
  737.    endif
  738. compile endif
  739.  
  740. ; The following two routines (from Larry Margolis) let the
  741. ; user decide what action should be taken when the Enter and Ctrl-Enter
  742. ; keys are pressed.  The possible values for the action constants are
  743. ; defined in STDCNF.
  744.  
  745. compile if C_ENTER_ACTION & not ENHANCED_ENTER_KEYS  -- If null, don't define - user will supply.
  746. defproc my_c_enter
  747.    compile if C_ENTER_ACTION = 'ADDATEND' | C_ENTER_ACTION = 'DEPENDS+'
  748.    if .line = .last then         -- If we're on the last line, then add a line.
  749. compile if EVERSION < '4.12'
  750.       call maybe_autosave()
  751. compile endif
  752.       call einsert_line()
  753.       down                       -- This keeps the === Bottom === line visible.
  754.    else
  755.    compile endif
  756.  
  757.    compile if C_ENTER_ACTION = 'DEPENDS' | C_ENTER_ACTION = 'DEPENDS+'
  758.    if insert_state() then        -- DEPENDS means if insertstate() then ...
  759.    compile endif
  760.  
  761.    compile if C_ENTER_ACTION = 'NEXTLINE' | C_ENTER_ACTION = 'DEPENDS' |
  762.               C_ENTER_ACTION = 'ADDATEND' | C_ENTER_ACTION = 'DEPENDS+'
  763.    down                          -- go to next line
  764.    begin_line
  765.    compile endif
  766.  
  767.    compile if C_ENTER_ACTION = 'DEPENDS' | C_ENTER_ACTION = 'DEPENDS+'
  768.    else                          -- otherwise ...
  769.    compile endif
  770.  
  771.    compile if C_ENTER_ACTION = 'ADDLINE' | C_ENTER_ACTION = 'DEPENDS' | C_ENTER_ACTION = 'DEPENDS+'
  772. compile if EVERSION < '4.12'
  773.    call maybe_autosave()
  774. compile endif
  775.    call einsert_line()           -- insert a line
  776.    compile endif
  777.  
  778.    compile if C_ENTER_ACTION = 'DEPENDS' | C_ENTER_ACTION='ADDATEND' | C_ENTER_ACTION = 'DEPENDS+'
  779.    endif
  780.    compile endif
  781.  
  782.    compile if C_ENTER_ACTION = 'DEPENDS+'
  783.    endif
  784.    compile endif
  785.  
  786.    compile if C_ENTER_ACTION = 'STREAM'
  787.    call splitlines()
  788.    call pfirst_nonblank()
  789.    down
  790.     compile if EPM
  791.    refresh
  792.     compile endif
  793.    compile endif
  794. compile endif
  795.  
  796. compile if not ENHANCED_ENTER_KEYS & ENTER_ACTION   -- If null, don't define - user will supply.
  797. defproc my_enter
  798.  compile if WANT_STREAM_MODE = 'SWITCH'
  799.    universal stream_mode
  800.  compile endif
  801.  compile if EVERSION < 5
  802.    if command_state() then
  803.       execute
  804.  compile else
  805.    if 0 then   -- EPM has no command_state()
  806.  compile endif
  807.  compile if WANT_STREAM_MODE = 'SWITCH'
  808.    elseif stream_mode then
  809.  compile endif
  810.  compile if WANT_STREAM_MODE
  811.       if .line then
  812.          split
  813.          .col=1
  814.          down
  815.       else
  816.          insert
  817.          .col=1
  818.       endif
  819.       return
  820.  compile endif
  821.  compile if WANT_STREAM_MODE <> 1
  822.  compile if ENTER_ACTION = 'ADDATEND' | ENTER_ACTION = 'DEPENDS+'
  823.    elseif .line = .last then     -- If we're on the last line, then add a line.
  824. compile if EVERSION <= '4.12'
  825.       call maybe_autosave()
  826. compile endif
  827.       call einsert_line()
  828.       down                       -- This keeps the === Bottom === line visible.
  829.  compile endif
  830.    else
  831.       compile if ENTER_ACTION = 'DEPENDS' | ENTER_ACTION = 'DEPENDS+'
  832.       if insert_state() then     -- DEPENDS means if insertstate() then ...
  833.       compile endif
  834.  
  835.       compile if ENTER_ACTION = 'ADDLINE' | ENTER_ACTION = 'DEPENDS' | ENTER_ACTION = 'DEPENDS+'
  836. compile if EVERSION < '4.12'
  837.       call maybe_autosave()
  838. compile endif
  839.       call einsert_line()        -- insert a line
  840.       compile endif
  841.  
  842.       compile if ENTER_ACTION = 'DEPENDS' | ENTER_ACTION = 'DEPENDS+'
  843.       else                       -- otherwise ...
  844.       compile endif
  845.  
  846.       compile if ENTER_ACTION = 'NEXTLINE' | ENTER_ACTION = 'DEPENDS' |
  847.                  ENTER_ACTION = 'ADDATEND' | ENTER_ACTION = 'DEPENDS+'
  848.       down                       -- go to next line
  849.       begin_line
  850.       compile endif
  851.  
  852.       compile if ENTER_ACTION = 'DEPENDS' | ENTER_ACTION = 'DEPENDS+'
  853.       endif
  854.       compile endif
  855.  
  856.       compile if ENTER_ACTION = 'STREAM'
  857.       if .line then
  858.          if .col<=length(textline(.line)) then
  859.             split
  860.             .col=1
  861.          else
  862.             split
  863.             call pfirst_nonblank()
  864.          endif
  865.          down
  866.       else
  867.          insert
  868.          .col=1
  869.       endif
  870.        compile if EPM
  871.       refresh
  872.        compile endif
  873.       compile endif
  874.  compile endif  -- WANT_STREAM_MODE <> 1
  875.    endif
  876. compile endif
  877.  
  878.  
  879. ;  A common routine to parse an argument string containing a mix of
  880. ;  options and DOS file specs.  The DOS file specs can contain an "=" for the
  881. ;  path or the fileid, which will be replaced by the corresponding part of the
  882. ;  previous file (initially, the current filename).
  883. defproc parse_file_n_opts(argstr)
  884.    prev_filename = .filename
  885.    output = ''
  886.    do while argstr<>''
  887. compile if EVERSION >= '5.50'
  888.       parse value argstr with filename rest
  889.       if leftstr(filename,1)='"' then
  890.          parse value argstr with '"' filename '"' argstr
  891.          filename = '"'filename'"'
  892.       else
  893.          argstr = rest
  894.       endif
  895. compile else
  896.       parse value argstr with filename argstr
  897. compile endif
  898.       if substr(filename,1,1)<>'/' then
  899.          call parse_filename(filename,prev_filename)
  900.          prev_filename = filename
  901.       endif
  902.       output = output filename
  903.    end
  904.    return substr(output,2)
  905.  
  906. ;  A common routine to parse a DOS file name.  Optional second argument
  907. ;  gives source for = when used for path or fileid.  RC is 0 if successful, or
  908. ;  position of "=" in first arg if no second arg given but was needed.
  909. defproc parse_filename(var filename)
  910. compile if EVERSION >= '5.50'
  911.    if leftstr(filename,1)='"' & rightstr(filename,1)='"' then
  912.       return 0
  913.    endif
  914. compile endif
  915.    sourcefile = strip(arg(2))
  916.    if sourcefile='' then return pos('=',filename) endif
  917.  
  918.    if filename='=' then filename=sourcefile; return 0; endif
  919.  
  920.    lastsep = lastpos('\',sourcefile)
  921.    if not lastsep & substr(sourcefile,2,1) = ':' then lastsep=2; endif
  922.  
  923.    /* E doesn't handle the = prefix if it's on the first file given on      */
  924.    /* the E command line.  This replaces = with path of current file.  LAM  */
  925.    if substr(filename,1,1) = '=' & lastsep then
  926.       if substr(filename,2,1) = '.' then filename='='filename endif
  927.       filename=substr(sourcefile,1,lastsep) || substr(filename,2)
  928.    endif
  929.  
  930.    /* Also accept '=' after the pathspec, like 'c:\bat\=', */
  931.    /* or c:\bat\=.bat or c:\doc\new.=                      */
  932.    p = pos('=',filename)
  933.    if p > 1 then
  934.       sourcefileid=substr(sourcefile,max(lastsep+1,1))
  935.       parse value sourcefileid with sourcefilename '.' sourcefileext
  936.       lastsep2 = lastpos('\',filename)
  937.       if not lastsep2 & substr(filename,2,1) = ':' then lastsep2=2; endif
  938.       dot1=pos('.',filename,max(lastsep2,1))
  939.       firstpart=substr(filename,1,p-1)
  940.       if dot1 then
  941.          if dot1<p then  -- filename.=
  942.             filename= firstpart || sourcefileext
  943.          else            -- =.ext
  944.             filename= firstpart || sourcefilename || substr(filename,dot1)
  945.          endif
  946.       else            -- d:\path\         ||        filename.ext
  947.          filename= firstpart || sourcefileid
  948.       endif -- dot1
  949.    endif -- p > 1
  950.    return 0
  951.  
  952. ;  This proc is called by DEFC EDIT.
  953. ;  Does *not* assume all options are specified before filenames.
  954. defproc parse_leading_options(var rest,var options)
  955.    options=''
  956.    loop
  957.       parse value rest with wrd more
  958.       if substr(wrd,1,1)='/' then
  959.          options = options wrd
  960.          rest = more
  961.       else
  962.          leave
  963.       endif
  964.    endloop
  965.  
  966.  
  967. ; PBEGIN_MARK: this procedure moves the cursor to the first character of the
  968. ; mark area.  If the mark area is not in the active file, the marked file is
  969. ; activated.
  970. defproc pbegin_mark
  971.    call checkmark()
  972.    getmark  firstline,lastline,firstcol,lastcol,fileid
  973.    activatefile fileid
  974. compile if EVERSION < 5
  975.    cursor_data
  976. compile endif
  977.    firstline
  978.    if marktype()<>'LINE' then
  979.       .col=firstcol
  980.    endif
  981.  
  982.  
  983. ; PBEGIN_WORD: moves the cursor to the beginning of the word if the cursor is on
  984. ; this word.  If it's not on a word, it's moved to the beginning of the first
  985. ; word on the left.  If there is no word on the left it's moved to the beginning
  986. ; of the word on the right.  If the line is empty the cursor doesn't move.
  987. defproc pbegin_word
  988.    getline line,.line
  989.    if  substr(line,.col,1)=' ' then
  990.       p=verify(line,' ')       /* 1st case: the cursor on a space */
  991.       if p>=.col then
  992.          .col=p
  993.       else
  994.          if p then
  995.             q=p
  996.             loop
  997.                p=verify(line,' ','M',p)
  998.                if not p or p>.col then leave endif
  999.                p=verify(line,' ','',p)
  1000.                if not p or p>.col then leave endif
  1001.                q=p
  1002.             endloop
  1003.             .col=q
  1004.          endif
  1005.       endif
  1006.    else
  1007.       if .col<>1 then          /* 2nd case: not on a space */
  1008.          .col=lastpos(' ',line,.col)+1
  1009.       endif
  1010.    endif
  1011.  
  1012.  
  1013. ; PBLOCK_REFLOW: reflow the text in the marked area.  Then the destination block
  1014. ; area must be selected and a second call to this procedure reflow the source
  1015. ; block in the destination block.  The source block is fill with spaces.
  1016. ;   option=0 saves the marked block in temp file
  1017. ;   option=1 reflow temp file text and copies it to marked area
  1018. defproc pblock_reflow(option,var spc,var tempofid)
  1019.    call checkmark()
  1020.    if not option then
  1021.       usedmk=marktype()
  1022.       getmark  firstline1,lastline1,firstcol1,lastcol1,fileid1
  1023.       /* move the source mark to a temporary file */
  1024. compile if EPM
  1025.       'xcom e /c .tempo'
  1026.       if rc<>sayerror('New file') then
  1027.          sayerror ERROR__MSG rc BAD_TMP_FILE__MSG sayerrortext(rc)
  1028.          return rc
  1029.       endif
  1030.       .visible = 0                                  -- Make hidden
  1031. compile else
  1032.       'xcom e 'argsep'q 'argsep'n 'argsep'h .tempo'
  1033.       if rc & rc<>sayerror('New file') then
  1034.          sayerror ERROR__MSG rc BAD_TMP_FILE__MSG
  1035.          return rc
  1036.       endif
  1037.       sayerror 1
  1038. compile endif
  1039.       getfileid tempofid
  1040.       activatefile tempofid
  1041.       call pcopy_mark()
  1042.       activatefile fileid1
  1043. compile if EVERSION < 5
  1044.       cursor_data
  1045. compile endif
  1046.       call pset_mark(firstline1,lastline1,firstcol1,lastcol1,usedmk,fileid1)
  1047.       if usedmk='LINE' then
  1048.          begin_line
  1049.       endif
  1050.       spc=usedmk firstline1 lastline1 firstcol1 lastcol1 fileid1
  1051.       return 0
  1052.    else
  1053.       getfileid startfid
  1054.       if marktype() <> 'BLOCK' then
  1055.          sayerror NEED_BLOCK_MARK__MSG
  1056.          /* release tempo */
  1057.          rc=0
  1058.          activatefile tempofid
  1059.          if rc then return rc; endif
  1060.          .modify=0
  1061.          'xcom q'
  1062.          activatefile startfid
  1063.          return 1
  1064.       endif
  1065. compile if EPM  -- Make sure temp file is good before deleting current file's text.
  1066.       rc=0
  1067.       activatefile tempofid
  1068.       if rc then return rc; endif
  1069.       activatefile startfid
  1070. compile endif
  1071.       parse value spc with usedmk firstline1 lastline1 firstcol1 lastcol1 fileid1
  1072.       getmark  firstline2,lastline2,firstcol2,lastcol2,fileid2
  1073.       /* fill source with space */
  1074.       if usedmk='LINE' then
  1075.          for i = firstline1 to lastline1
  1076.             replaceline '',i,fileid2
  1077.          endfor
  1078.       else
  1079.          call pset_mark(firstline1,lastline1,firstcol1,lastcol1,usedmk,fileid1)
  1080.          call pfill_mark(' ')
  1081.       endif
  1082.       call pset_mark(firstline2,lastline2,firstcol2,lastcol2,'BLOCK',fileid2)
  1083.       delete_mark
  1084.       /* let's reflow in the hidden file */
  1085.       activatefile tempofid
  1086.       width = lastcol2+1-firstcol2
  1087.       height = lastline2+1-firstline2
  1088. compile if EVERSION < '4.12'
  1089.       savemargins= pmargins()
  1090. compile endif
  1091.       'xcom ma 1 'width
  1092.       unmark; mark_line; bottom; mark_line
  1093.       reflow
  1094. compile if EVERSION < '4.12'
  1095.       'xcom ma 'savemargins
  1096. compile endif
  1097.       nbl = .last
  1098.       /* go back to the destination */
  1099.       activatefile fileid2
  1100.       if nbl > height then
  1101.          fix = nbl-height
  1102.          getline line,lastline2
  1103.          for i = 1 to fix
  1104.             insertline line,lastline2+1
  1105.          endfor
  1106.       elseif nbl < height then
  1107.          fix=0
  1108.          for i = nbl+1 to height
  1109.             insertline '',tempofid.last+1,tempofid
  1110.          endfor
  1111.          nbl=height
  1112.       else
  1113.          fix=0
  1114.       endif
  1115.       call pset_mark(1,nbl,1,width,'BLOCK',tempofid)
  1116.       firstline2; .col=firstcol2; copy_mark; unmark
  1117.       call pset_mark(firstline2,lastline2+fix,firstcol2,lastcol2,'BLOCK',fileid2)
  1118.       /* release tempo */
  1119.       activatefile tempofid
  1120.       .modify=0
  1121.       'xcom q'
  1122.       activatefile fileid2
  1123.       sayerror 1
  1124.     endif
  1125.  
  1126.  
  1127. ; PCENTER_MARK: center the strings between the block marks
  1128. defproc pcenter_mark
  1129.    if  marktype() = 'BLOCK' then
  1130.       getmark  firstline,lastline,firstcol,lastcol,fileid
  1131.    elseif marktype() = 'LINE' then
  1132.       getmark  firstline,lastline,firstcol,lastcol,fileid
  1133.       parse value pmargins() with  firstcol lastcol .
  1134.    elseif marktype() = '' then
  1135.       getfileid fileid
  1136.       parse value pmargins() with  firstcol lastcol .
  1137.       firstline=.line;lastline=.line
  1138.    else
  1139.       sayerror CHAR_INVALID__MSG
  1140.       stop
  1141.    endif
  1142.    sz = lastcol+1-firstcol
  1143.    for i=firstline to lastline
  1144.       getline line,i,fileid
  1145.       inblock=strip(substr(line,firstcol,sz))
  1146.       if inblock='' then iterate endif
  1147. compile if EPM
  1148.       replaceline strip(overlay(center(inblock, sz), line, firstcol),'T'), i, fileid
  1149. compile else
  1150.       replaceline substr(line,1,firstcol-1) ||
  1151.          substr(substr('',1,(sz-length(inblock))%2)||inblock,1,sz) ||
  1152.          substr(line,lastcol+1) ,i,fileid
  1153. compile endif
  1154.    endfor
  1155.  
  1156.  
  1157. compile if EVERSION < 5
  1158. ;  A built-in function command_state() is now provided for better
  1159. ;  efficiency.  This defproc is kept only for compatibility with older macros.
  1160. ;  Please use command_state() instead.
  1161. defproc pcommand_state
  1162.    return command_state()
  1163.  
  1164.  
  1165. ; PCOMMON_TAB_MARGIN: subroutine common to ptabs and pmargins
  1166.  
  1167. defproc pcommon_tab_margin(TabOrMargins)
  1168. ;    the tricky stuff:  execute ma (or tabs) and get the result from coms.e file
  1169.    getcommand oldcmd,oldcol,oldscroll    -- Save old cmdline status
  1170.    TabOrMargins                          -- Execute the command
  1171.    getcommand setting                    -- Get current setting from cmdline
  1172.    setcommand oldcmd,oldcol,oldscroll    -- Restore old cmdline status
  1173.    parse value setting with . val        -- Get the stuff we want
  1174.    return val
  1175. compile endif
  1176.  
  1177. compile if 0    -- The following two routines are unused; why waste space??  LAM
  1178. ; PDISPLAY_MARGINS: put the margins mark on the current line
  1179.  
  1180. defproc pdisplay_margins()
  1181.    i=insert_state()
  1182.    if i then insert_toggle endif
  1183.    call psave_pos(save_pos)
  1184.    insert
  1185.    parse value pmargins() with lm rm pm .
  1186.    .col=lm;keyin'L';.col=pm;keyin'P';.col=rm;keyin'R'
  1187.    begin_line
  1188.    call prestore_pos(save_pos)
  1189.    if i then insert_toggle endif
  1190.    return 0
  1191.  
  1192.  
  1193. ; PDISPLAY_TABS: put the tab stops on the current line
  1194.  
  1195. defproc pdisplay_tabs()
  1196.    i=insert_state()
  1197.    if i then insert_toggle endif
  1198.    call psave_pos(save_pos)
  1199.    insert
  1200.    tabstops = ptabs()
  1201.    do forever
  1202.       parse value tabstops with tabx tabstops
  1203.       if tabx = '' then leave endif
  1204.       .col=tabx
  1205.       keyin'T'
  1206.    end
  1207.    begin_line
  1208.    call prestore_pos(save_pos)
  1209.    if i then insert_toggle endif
  1210.    return 0
  1211. compile endif
  1212.  
  1213. ; PEND_MARK: moves the cursor to the end of the marked area
  1214. defproc pend_mark
  1215.    call checkmark()
  1216.    getmark  firstline,lastline,firstcol,lastcol,fileid
  1217.    activatefile fileid
  1218. compile if EVERSION < 5
  1219.    cursor_data
  1220. compile endif
  1221.    if marktype()<>'LINE' then
  1222.       .col=lastcol
  1223.    endif
  1224.    lastline
  1225.  
  1226. ; PEND_WORD: moves the cursor to the end of the word if the cursor is on this
  1227. ; word.  If it's not on a word, it's moved to the end of the first word on the
  1228. ; right.  If there is no word on the right it's moved to the end of the word on
  1229. ; the left.  If the line is empty the cursor doesn't move.
  1230. defproc pend_word
  1231.    getline line,.line
  1232.    if  substr(line,.col,1)=' '  then
  1233.       if substr(line,.col)=' ' then
  1234.          if  line<> ' ' then
  1235.             for i=.col to 2 by -1
  1236.                if substr(line,i-1,1)<>' ' then leave endif
  1237.             endfor
  1238.            .col=i-1
  1239.          endif
  1240.       else
  1241.          p=verify(line,' ','',.col)
  1242.          p=verify(line' ',' ','M',p)
  1243.          .col=p-1
  1244.       endif
  1245.    else
  1246.       if .col<>MAXCOL then
  1247.          i=pos(' ',line,.col)
  1248.          if i then
  1249.             .col=i-1
  1250.          else
  1251.             .col=length(line)
  1252.          endif
  1253.       endif
  1254.    endif
  1255.  
  1256.  
  1257. defproc pfile_exists /* Check if file already exists in ring */
  1258.    if substr(arg(1),2,1)=':'  then
  1259.       /* parse off drive specifier and try again */
  1260.       getfileid zzfileid,substr(arg(1),3)
  1261.    else
  1262.       getfileid zzfileid,arg(1)
  1263.    endif
  1264.    return zzfileid<>''
  1265.  
  1266. defproc pfind_blank_line
  1267.    -- Find first blank line after the current one.  Make that the new current
  1268.    -- line.  If no such line is found before the end of file, don't change the
  1269.    -- current line.
  1270.    for i = .line+1 to .last
  1271.       getline line,i
  1272.       -- Ver 3.11:  Modified to respect GML tags:  stop at first blank line
  1273.       -- or first line with a period or a colon (".:") in column 1.
  1274.       if line='' or not verify(substr(line,1,1), ".:" ) then
  1275.          i
  1276.          leave
  1277.       endif
  1278.    endfor
  1279.  
  1280. defproc pfirst_nonblank
  1281.    /* different from PE */
  1282.    if not .line then .col=1
  1283.    else
  1284.       getline line
  1285.       .col=max(1,verify(line,' '))
  1286.    endif
  1287.  
  1288.  
  1289. ; PLOWERCASE: force to lowercase the marked area
  1290.  
  1291. defproc plowercase
  1292.    call checkmark()
  1293.    /* invoke pinit_extract, pextract_string, pput_string_back to do the job */
  1294.    call psave_pos(save_pos)
  1295.    call pinit_extract()
  1296.    do forever
  1297.       code = pextract_string(string)
  1298.       if code = 1 then leave; endif
  1299.       if code = 0 then
  1300.          string = lowcase(string)
  1301.          call pput_string_back(string)
  1302.       endif
  1303.    end
  1304.    call prestore_pos(save_pos)
  1305.  
  1306.  
  1307. ; PMARGINS: return the current margins setting. (Uses pcommon_tab_margin)
  1308.  
  1309. defproc pmargins
  1310. compile if EVERSION < 5
  1311.    return pcommon_tab_margin('ma')
  1312. compile else
  1313.    return .margins
  1314. compile endif
  1315.  
  1316.  
  1317. ; PMARK: mark at the cursor position (mark type received as argument).  Used by
  1318. ; pset_mark
  1319. defproc pmark(mt)
  1320.    if mt= 'LINE' then
  1321.       mark_line
  1322.    elseif mt = 'CHAR' then
  1323.       mark_char
  1324.    else
  1325.       mark_block
  1326.    endif
  1327.  
  1328.  
  1329. ; PMARK_WORD: mark the word pointed at by the cursor.  If the cursor is on a
  1330. ; space, the word at the right is marked.  If there is no word on the right, the
  1331. ; word on the left is marked.
  1332. defproc pmark_word
  1333.    if marktype()<>'' then
  1334.       sayerror -279  -- 'Text already marked'
  1335.       stop
  1336.    endif
  1337.    call pend_word()
  1338.    mark_block
  1339.    call pbegin_word()
  1340.    mark_block
  1341. compile if EVERSION > 5
  1342.   'Copy2SharBuff'       /* Copy mark to shared text buffer */
  1343. compile endif
  1344.  
  1345.  
  1346. ; PRESTORE_MARK: restore the current marks (cannot be used as a stack) See also
  1347. ; psave_mark
  1348. defproc prestore_mark(savemark)
  1349.    unmark
  1350.    parse value savemark with savefirstline savelastline savefirstcol savelastcol savemkfileid savemt
  1351.    if savemt<>'' then
  1352.       call pset_mark(savefirstline,savelastline,savefirstcol,savelastcol,savemt,savemkfileid)
  1353.    endif
  1354.  
  1355.  
  1356. ; PRESTORE_POS: restore the cursor position (cannot be used as a stack) See
  1357. ; also psave_pos()
  1358. defproc prestore_pos(save_pos)
  1359.    parse value save_pos with svline svcol svcx svcy
  1360.    .cursory = svcy                          -- set .cursory
  1361.    min(svline, .last)                       -- set .line
  1362.    .col = MAXCOL; .col = svcol - svcx + 1   -- set left edge of window
  1363.    .col = svcol                             -- set .col
  1364.  
  1365.  
  1366. ;  Printer_ready( printer_number ) tests whether printer is ready.
  1367. ;
  1368. ;  Enter with printer_number = 1 for the first printer (LPT1), 2 for LPT2.
  1369. ;  No argument at all defaults to LPT1.
  1370. ;
  1371. ;  Returns 1 (true)  for printer attached and ready.
  1372. ;  Returns 0 (false) for printer not attached or not ready.
  1373. ;
  1374. ;  Note:  Assumes the standard BIOS responses for an IBM PC.
  1375. ;  The BIOS responds with AH=90 hex for printer ready.
  1376. ;  Might not work on clones and other strange machines.
  1377. ;
  1378. ;  If we're on OS/2 we don't check because the spooler protects us from
  1379. ;  a hang if the printer's off-line.  We always return "ready" on OS/2.
  1380. ;
  1381. defproc printer_ready
  1382. compile if EVERSION >= 4
  1383.    return 1
  1384. compile else
  1385.    if arg(1)='' then
  1386.       printer_number=1
  1387.    elseif not isnum(arg(1)) then
  1388.       sayerror 'Printer_ready:  'INVALID_NUMBER__MSG
  1389.       return 0
  1390.    else
  1391.       printer_number = arg(1)
  1392.    endif
  1393.    /* Call BIOS interrupt 17 hex with AH=2, printer status query. */
  1394.    parse value int86x(23,512 0 0 printer_number-1,'') with printer_status .
  1395. ;    IBM PC family returns '90' for printer ready (not busy + selected).
  1396. ;    Some clones return 'D0' (not busy + acknowledge + selected).
  1397. ;    Here, we'll accept either value.
  1398. ;                    hex2dec('9000'):              hex2dec('D000'):
  1399.    return (printer_status == -28672) or (printer_status == -12288)
  1400. compile endif
  1401.  
  1402.  
  1403. ; PSAVE_MARK: save the current marks (cannot be used as a stack) See also
  1404. ; prestore_pos()
  1405. defproc psave_mark(var savemark)
  1406.    savemt=marktype()
  1407.    if savemt then
  1408.       getmark  savefirstline,savelastline,savefirstcol,savelastcol,savemkfileid
  1409.       unmark
  1410.       savemark=savefirstline savelastline savefirstcol savelastcol savemkfileid savemt
  1411.    else
  1412.       savemark=''
  1413.    endif
  1414.  
  1415.  
  1416. ; PSAVE_POS: save the cursor position (cannot be used as a stack) See also
  1417. ; prestore_pos()
  1418. defproc psave_pos(var save_pos)
  1419.    save_pos=.line .col .cursorx .cursory
  1420.  
  1421.  
  1422. defproc pset_mark(firstline,lastline,firstcol,lastcol,mt,fileid)
  1423. compile if EVERSION >= '5.50'
  1424.    setmark firstline,lastline,firstcol,lastcol,wordpos(mt,'LINE CHAR BLOCK CHARG BLOCKG')-1,fileid
  1425. compile else
  1426.    getfileid actfileid    /* preserve current active fileid */
  1427.    rc = 0
  1428.    activatefile fileid
  1429.  compile if not E3
  1430.    if rc=sayerror('Invalid fileid') then stop; endif
  1431.  compile endif
  1432.    call psave_pos(save_pos)
  1433.    unmark
  1434.    if lastcol then
  1435.       .col=lastcol; lastline
  1436.    else
  1437.       lastline-1; .col=MAXCOL
  1438.    endif
  1439.    call  pmark(mt)
  1440.    .col=firstcol; firstline
  1441.    call pmark(mt)
  1442.    call prestore_pos(save_pos)
  1443.    activatefile actfileid         /* restore the initial active file */
  1444. compile endif
  1445.  
  1446. ; PTABS: return the current tabs setting. (Uses pcommon_tab_margin)
  1447.  
  1448. defproc ptabs
  1449. compile if EVERSION < 5
  1450.    return pcommon_tab_margin('tabs')
  1451. compile else
  1452.    return .tabs
  1453. compile endif
  1454.  
  1455.  
  1456. ; PUPPERCASE: force to uppercase the marked area
  1457.  
  1458. defproc puppercase
  1459.    call checkmark()
  1460.    /* invoke pinit_extract, pextract_string, pput_string_back to do the job */
  1461.    call psave_pos(save_pos)
  1462.    call pinit_extract()
  1463.    do forever
  1464.       code = pextract_string(string)
  1465.       if code = 1 then leave endif
  1466.       if code = 0 then
  1467.          string = upcase(string)
  1468.          call pput_string_back(string)
  1469.       endif
  1470.    end
  1471.    call prestore_pos(save_pos)
  1472.  
  1473. ;defproc remove_trailing_spaces
  1474. ;   /* This is no longer used by any file in standard E.  Use strip()  */
  1475. ;   /* instead.  But left here for compatibility with older procs.     */
  1476. ;   return strip(arg(1),'T')
  1477.  
  1478. compile if EPM
  1479. ; In E3 and EOS2, we can use a_X to enter the value of any key.  In EPM,
  1480. ; we can't, so the following routine is used by KEY and LOOPKEY to convert
  1481. ; from an ASCII key name to the internal value.  It handles shift or alt +
  1482. ; any letter, or a function key (optionally, with any shift prefix).  LAM
  1483. defproc resolve_key(k)
  1484.    kl=lowcase(k)
  1485.    suffix=\2                           -- For unshifted function keys
  1486.    if length(k)>=3 & pos(substr(k,2,1),'_-+') then
  1487.       if length(k)>3 & substr(kl,3,1)='f' then     -- Shifted function key
  1488.          suffix=substr(\10\34\18,pos(leftstr(kl,1),'sac'),1)  -- Set suffix,
  1489.          kl=substr(kl,3)             -- strip shift prefix, and more later...
  1490.       else                        -- alt+letter or ctrl+letter
  1491.          k=substr(kl,3,1) || substr(' ',pos(leftstr(kl,1),'ac'),1)
  1492.       endif
  1493.    endif
  1494.    if leftstr(kl,1)='f' & isnum(substr(kl,2)) then
  1495.       k=chr(substr(kl,2)+31) || suffix
  1496.    endif
  1497.    return k
  1498. compile endif
  1499.  
  1500. compile if EVERSION < 5
  1501. defproc restore_command_state(cstate)
  1502.    if command_state()<>cstate then
  1503.       command_toggle
  1504.    endif
  1505.  
  1506. defproc save_command_state(var cstate)
  1507.    cstate=command_state()
  1508.    cursor_data
  1509.    refresh            /* Force E to update the cursor position */
  1510. compile endif
  1511.  
  1512.  
  1513. -- 4.10:  Saving with tab compression is built in now.  No need for
  1514. -- the make-do proc savefilewithtabs().  DOS version still needs it for
  1515. -- people editing MAKE files, but we make it optional via WANT_TABS.
  1516.  
  1517. compile if E3 & WANT_TABS
  1518. ; Note:  This does not tabify the entire file; it just replaces 8 blanks
  1519. ; in the first column with a tab character.
  1520. defproc savefilewithtabs(filename)
  1521.    options=arg(2)
  1522.    call psave_pos(save_pos)
  1523.    getfileid fileid
  1524.    unmark;bottom;markline;top;markline
  1525.    call prestore_pos(save_pos)
  1526.    'xcom e 'argsep'n .';deleteline
  1527.    if rc and rc<>-282 then  -- sayerror("new file")
  1528.       return rc
  1529.    endif
  1530.    rc=0
  1531.    copymark
  1532.    if rc then return rc endif
  1533.    unmark
  1534.    top;.col=1;markblock;bottom;.col=8;markblock
  1535.    .col=1;top
  1536.    'c/        /'\t'/m*'     /* replace first column 8 spaces with tab */
  1537.    sayerror 1  /* Turn off pending messages */
  1538.    unmark
  1539.    savestatus=savefile(filename,options)
  1540.    .modify=0
  1541.    'xcom q'
  1542.    if savestatus then return savestatus endif
  1543.    activatefile fileid
  1544.    if filename=.filename then
  1545.       .modify=0
  1546.    endif
  1547.    return 0
  1548. compile endif
  1549.  
  1550. define
  1551. compile if EVERSION < '5.21'
  1552.    MSGC = '.messagecolor'
  1553. compile elseif EVERSION < '5.50'
  1554.    MSGC = 'vMESSAGECOLOR'
  1555. compile else            -- GPI version
  1556.    MSGC = 'color'
  1557. compile endif
  1558.  
  1559. ; Paste up a message in a box, using SAYAT's.  Useful for "Processing..." msgs.
  1560. defproc sayatbox
  1561. compile if EVERSION >= '5.21'
  1562.    universal vMESSAGECOLOR
  1563. compile endif
  1564. compile if WANT_DBCS_SUPPORT
  1565.    universal ondbcs
  1566. compile endif
  1567.  
  1568. compile if EVERSION >= '5.50'  -- GPI version; doesn't use background color in SAYATs.
  1569.    color = sayat_color()
  1570. compile endif
  1571. compile if WANT_DBCS_SUPPORT
  1572.    if ondbcs then
  1573.       middle = substr('',1,length(arg(1)),\x06)
  1574.       sayat '  '\x01\x06||middle||\x06\x02'  ',1,2, $MSGC
  1575.       sayat '  '\x05' 'arg(1)' '\x05'  ',2,2, $MSGC
  1576.       sayat '  '\x03\x06||middle\x06\x04'  ',3,2, $MSGC
  1577.    else
  1578. compile endif
  1579.       middle = substr('',1,length(arg(1)),'═')
  1580.       sayat '  ╔═'middle'═╗  ',1,2, $MSGC
  1581.       sayat '  ║ 'arg(1)' ║  ',2,2, $MSGC
  1582.       sayat '  ╚═'middle'═╝  ',3,2, $MSGC
  1583. compile if WANT_DBCS_SUPPORT
  1584.    endif
  1585. compile endif
  1586.  
  1587. compile if EVERSION >= '5.50'
  1588. defproc sayat_color =          -- Pick a color for SAYAT that doesn't conflict w/ foreground or background color.
  1589.    universal vMESSAGECOLOR
  1590.    if vMESSAGECOLOR // 16 <> .textcolor // 16 & vMESSAGECOLOR // 16 <> .textcolor % 16 then
  1591.       return vMESSAGECOLOR       -- Preference is the message color.
  1592.    endif
  1593.    if vMESSAGECOLOR // 16 <> LIGHT_RED then
  1594.       return LIGHT_RED           -- Second choice is light red.
  1595.    endif
  1596.    if .textcolor // 16 <> LIGHT_BLUE & .textcolor % 16 <> LIGHT_BLUE then
  1597.       return LIGHT_BLUE          -- If that's used, then how about light blue
  1598.    endif
  1599.    return GREEN                  -- Final fallback is green.
  1600. compile endif
  1601.  
  1602. defproc splitlines()
  1603.    if .line then
  1604.       split
  1605.       oldcol=.col
  1606.       call pfirst_nonblank()
  1607. compile if EPM           -- Can't use REPLACELINE because it wipes out attributes.
  1608.       col1 = .col
  1609.       down
  1610.       if textline(.line)<>'' then
  1611.          call pfirst_nonblank()
  1612.          col2 = .col
  1613.          getsearch savesearch
  1614.          .col = 1
  1615.          if col1 < col2 then       -- Shift new line left
  1616.             'xcom c/'copies(' ',col2-col1)'//'  -- Change first n blanks to null
  1617.          elseif col1 > col2 then   -- Shift new line right
  1618. ;;            c = substr(textline(.line),1,1)  -- LAM: simpler to use GREP
  1619. ;;            if c='/' then d='?'; else d='/'; endif  -- Choose delimiter
  1620. ;;            'xcom c 'd||c||d||substr('',1,col1-col2)||c||d  -- Add blanks before 1st char.
  1621.             'xcom c /^/'copies(' ',col1-col2)'/g'  -- Add blanks before 1st char.
  1622.          endif
  1623.          setsearch savesearch
  1624.       endif
  1625.       up
  1626. compile else           -- E3 and EOS2 can still use the old, simple way.
  1627.       getline line,.line+1
  1628.       replaceline substr('',1,.col-1) ||    -- indent like previous line
  1629.          strip(line,'L'),                   -- (remove leading spaces)
  1630.          .line+1
  1631. compile endif
  1632.       .col=oldcol
  1633.    endif
  1634.  
  1635.  
  1636. ; Note on a speed trick:  subdir_present is initialized to null at start-up.
  1637. ; This causes defproc subdir(), the first time it's called, to execute a
  1638. ; FINDFILE (by way of find_routine) to search the path for the subdir program.
  1639. ; (See DEFC HELP for another example of findfile.)
  1640. ; After that first search the exact path location of subdir is known; it's
  1641. ; remembered in the universal variable subdir_present.  All future calls supply
  1642. ; the exact location (as in "C:\UTIL\SUBDIR.COM") to avoid the path search.
  1643.  
  1644. definit  /* Keep this definit close to the proc it serves. */
  1645.    universal subdir_present
  1646.    subdir_present=''
  1647.  
  1648. defproc subdir
  1649.    universal subdir_present
  1650.    if subdir_present='' then  -- First time; look for the program
  1651.  compile if E3
  1652.       subdir_present=find_routine('SUBDIR /Q')
  1653.       if subdir_present == -1 then      -- Not found
  1654.          if Dos_Version() >= 500 then   -- If DOS version is 5, can use DIR
  1655.             subdir_present='dir /b /s'  -- (SUBDIR preferable for leading wildcards)
  1656.          endif
  1657.       endif
  1658.  compile else
  1659.       if Dos_Version() >= 2000 then   -- If OS/2 2.0 or above, use DIR
  1660.          subdir_present='dir /b /s'   -- (OS/2 DIR supports leading wildcards)
  1661.       else
  1662.          subdir_present=find_routine('FILEFIND')
  1663.       endif
  1664.  compile endif
  1665.       if subdir_present == -1 then     -- Not found, try ATTRIB
  1666.          subdir_present=find_routine('ATTRIB /S')
  1667.       endif
  1668.    endif
  1669.    if subdir_present == -1 then
  1670.       sayerror CANT_FIND_PROG__MSG 'ATTRIB'
  1671.       stop
  1672.    endif
  1673.    quietshell subdir_present arg(1)
  1674.  
  1675. compile if EVERSION >= 4
  1676. defproc swapwords(num)
  1677.    return substr(num,3,2) || substr(num,1,2)
  1678. compile endif
  1679.  
  1680.  
  1681. compile if E3 or (EPM & not (EVERSION >= '5.17'))
  1682. ;  EOS2 & EPM have a TEXTLINE() function built in.  This is added here so that
  1683. ;  E3 macro programmers can use TEXTLINE also, if they like.
  1684. defproc textline(linenum)
  1685.    getline line,linenum; return line
  1686. compile endif
  1687.  
  1688. -- Standard text reflow, moved from Alt+P definition in STDKEYS.E.
  1689. -- Only called from Alt+P if no mark exists; users wishing to call
  1690. -- this from their own code must save & restore the mark themselves
  1691. -- if that's desired.
  1692. defproc text_reflow
  1693.    if .line then
  1694.       getline line
  1695.       if line<>'' then  -- If currently on a blank line, don't reflow.
  1696.          oldcursory=.cursory;oldcursorx=.cursorx; oldline=.line;oldcol=.col;
  1697.          unmark;mark_line
  1698.          call pfind_blank_line()
  1699.          -- Ver 3.11:  slightly revised test works better with GML sensitivity.
  1700.          if .line<>oldline then
  1701.             up
  1702.          else
  1703.             bottom
  1704.          endif
  1705.          mark_line
  1706.          reflow
  1707.  
  1708. compile if REFLOW_LIKE_PE   /* position on next paragraph (like PE) */
  1709.          down                       /* Thanks to Doug Short. */
  1710.          for i=.line+1 to .last
  1711.             getline line,i
  1712.             if line<>'' then i; leave; endif
  1713.          endfor
  1714. compile else
  1715.          /* or like old E */
  1716.          getmark firstline,lastline
  1717.          firstline
  1718.          .cursory=oldcursory;.cursorx=oldcursorx; oldline;.col=oldcol
  1719. compile endif
  1720.          unmark
  1721.       endif
  1722.    endif
  1723.  
  1724. ;  A truncate function to maintain compatibility of macros between this
  1725. ;  version and the OS/2 version which will have floating point.  Two
  1726. ;  functions in DOSUTIL.E need this.
  1727. ;
  1728. ;  If we're passed a floating point number with a decimal point in it,
  1729. ;  like "4.0", drop the decimal part.
  1730. ;  If we're passed an exponential-format number like "6E3", fatal error.
  1731. defproc trunc(num)
  1732.    if not verify('E',upcase(num)) then
  1733.       sayerror NO_FLOAT__MSG num
  1734.       stop
  1735.    endif
  1736.    parse value num with whole'.'.
  1737.    return whole
  1738.  
  1739. compile if WANT_LAN_SUPPORT
  1740. defproc unlock(fileid)
  1741.    if fileid.lockhandle = 0 then
  1742.       sayerror fileid.filename NOT_LOCKED__MSG
  1743.       return 1
  1744.    endif
  1745.  compile if EPM32
  1746.    result = dynalink32('DOSCALLS',    -- Free the original handle
  1747.                        '#257',                    -- dos32close
  1748.                        atol(fileid.lockhandle), 2)
  1749.  compile else
  1750.    result = dynalink('DOSCALLS',
  1751.                      '#59',                    /* dosclose */
  1752.                      atoi(fileid.lockhandle))
  1753.  compile endif
  1754.    if result then
  1755.       sayerror 'DOSCLOSE' ERROR_NUMBER__MSG result
  1756.    else
  1757.       fileid.lockhandle = 0
  1758.    endif
  1759.    return result
  1760. compile endif
  1761.  
  1762.