home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / epmmac.zip / BUFF.E < prev    next >
Text File  |  1994-02-01  |  6KB  |  187 lines

  1. ;
  2. ;  Test macros for the buffer() opcode.      Bryan Lewis 8/1/88.
  3. ;
  4. ;Changes 8/29 -- look for change marker '|'.
  5. ;* Can specify a buffer to be private, non-shared.  This allows a process
  6. ;  to create any number of buffers for internal use, without running into the
  7. ;  OS/2 limit of 30 shared buffers.
  8. ;* New format option FINAL_NULL to append a null at end of buffer, as needed
  9. ;  for clipboard format.
  10.  
  11. compile if not defined(SMALL)
  12.  include 'stdconst.e'
  13.  define INCLUDING_FILE = 'BUFF.E'
  14.  tryinclude 'MYCNF.E'
  15.  
  16.  compile if not defined(SITE_CONFIG)
  17.     const SITE_CONFIG = 'SITECNF.E'
  18.  compile endif
  19.  compile if SITE_CONFIG
  20.     tryinclude SITE_CONFIG
  21.  compile endif
  22.  
  23.  compile if not defined(NLS_LANGUAGE)
  24.    const NLS_LANGUAGE = 'ENGLISH'
  25.  compile endif
  26.  include NLS_LANGUAGE'.e'
  27. compile endif
  28.  
  29. definit
  30.    universal format, bufname, bufhndl
  31.    format = 0     -- Initialize.  Zero will give standard CR-LF format.
  32.    bufname = 'EBUF'
  33.    bufhndl = 0    -- Always set bufhndl to zero if no buffer is active.
  34.                   -- E will safely reject a zero bufhndl, but has no way of
  35.                   -- knowing whether other arbitrary bufhndl's are valid.
  36.  
  37. define
  38. compile if EVERSION < '5.21'
  39.    MSGC = '.messagecolor'
  40. compile else
  41.    MSGC = 'vMESSAGECOLOR'
  42. compile endif
  43.  
  44. defc buffhelp =
  45.    universal vMESSAGECOLOR
  46.    sayat leftstr(CREATEBUF_HELP__MSG ,72), 1, 4, $MSGC
  47.    sayat leftstr(PUTBUF_HELP__MSG    ,72), 2, 4, $MSGC
  48.    sayat leftstr(GETBUF_HELP__MSG    ,72), 3, 4, $MSGC
  49.    sayat leftstr(FREEBUF_HELP__MSG   ,72), 4, 4, $MSGC
  50.  
  51.  
  52. --| New optional argument:  GETBUF 1 means a private buffer.
  53. --| Means don't try to open and close a named buffer; just use current bufhndl.
  54. defc getbuf    -- Gets entire buffer into current file.
  55.                -- Buffer must have been precreated.
  56.                -- Opens the buffer before the get, closes it afterward.
  57.                -- Note:  if we were only doing this from the same E session
  58.                --        as where we created the buffer, we wouldn't need the
  59.                --        open.  But we might get from another session!
  60.    universal format, bufname
  61.    universal bufhndl --| We use the previous bufhndl if private.
  62.  
  63.    private = 0                   --| new option to create non-shared buffer
  64.    if arg(1) then
  65.       private = 1
  66.    endif
  67.    if not private then
  68.       bufhndl = buffer(OPENBUF, bufname)
  69.       if not bufhndl then sayerror 'OPENBUF' ERROR_NUMBER__MSG RC; stop; endif
  70.    endif
  71.  
  72.    noflines = buffer(GETBUF,bufhndl)
  73.    if not noflines then
  74.       if rc then
  75.          sayerror 'GETBUF' ERROR_NUMBER__MSG RC
  76.       else
  77.          sayerror EMPTYBUF_ERROR__MSG
  78.       endif
  79.       stop
  80.    endif
  81.    usedsize = buffer(USEDSIZEBUF,bufhndl)   -- Get these for info.
  82.    maxsize  = buffer(MAXSIZEBUF,bufhndl)
  83.  
  84.    if not private then  --|
  85.       success  = buffer(FREEBUF,  bufhndl)
  86.       if not success then sayerror 'FREEBUF' ERROR_NUMBER__MSG RC; stop; endif
  87.    endif
  88.  
  89.    sayerror GOT__MSG usedsize BYTES_FROM_A__MSG maxsize || BYTE_BUFFER__MSG'.  'noflines LINES__MSG
  90.  
  91. defc putbuf    -- Puts current file from current line on.
  92.                -- Buffer must be created first.
  93.    universal format, bufhndl
  94.  
  95.    -- From current line to end of file.
  96.    noflines = buffer(PUTBUF,   bufhndl, .line, 0, format)
  97.    if not noflines then sayerror 'PUTBUF' ERROR_NUMBER__MSG RC; stop; endif
  98.    usedsize = buffer(USEDSIZEBUF,bufhndl)   -- Get these for info.
  99.    maxsize  = buffer(MAXSIZEBUF,bufhndl)
  100.    sayerror PUT__MSG usedsize BYTES_TO_A__MSG maxsize || BYTE_BUFFER__MSG'.  'noflines LINES__MSG
  101.  
  102. --| New optional argument:  CREATEBUF 1 means a private buffer.
  103. defc createbuf -- Creates the buffer of default size.
  104.    universal bufname, bufhndl
  105.    private = 0                   --| new option to create non-shared buffer
  106.    if arg(1) then
  107.       private = 1
  108.    endif
  109.    bufhndl = buffer(CREATEBUF, bufname, MAXBUFSIZE, private )  --|
  110.    if not bufhndl then sayerror 'CREATEBUF' ERROR_NUMBER__MSG RC; stop; endif
  111.    sayerror CREATED__MSG
  112.  
  113. defc freebuf
  114.    universal bufhndl
  115.    success = buffer(FREEBUF, bufhndl)
  116.    if not success then sayerror 'FREEBUF' ERROR_NUMBER__MSG RC; stop; endif
  117.    sayerror FREED__MSG
  118.    bufhndl = 0             -- Reset to no-buffer value.
  119.  
  120. defproc put_shared_text(buffer_name, firstline, lastline)
  121.    if buffer_name='' then
  122.       sayerror MISSING_BUFFER__MSG
  123.       stop
  124.    endif
  125.    -- Try to open the buffer.  If it doesn't exist, create it.
  126.    bufhndl = buffer(OPENBUF, buffer_name)
  127.    if bufhndl then
  128.       opened = 1
  129.    else
  130.       -- Make a 64K buffer... memory's plentiful.  Easily changed.
  131.       bufsize = MAXBUFSIZE
  132.       bufhndl = buffer(CREATEBUF, buffer_name, bufsize)
  133.       opened = 0
  134.    endif
  135.    if not bufhndl then
  136.       sayerror CAN_NOT_OPEN__MSG buffer_name '-' ERROR_NUMBER__MSG RC
  137.       stop
  138.    endif
  139.    noflines = buffer(PUTBUF, bufhndl, firstline, lastline)
  140.    if opened then
  141.       call buffer(FREEBUF, bufhndl)
  142.    endif
  143.    if noflines < lastline-firstline+1 then
  144.       sayerror ONLY_ACCEPTED__MSG noflines LINES__MSG
  145.       stop
  146.    endif
  147.  
  148. defproc get_shared_text(buffer_name)
  149.    if buffer_name='' then
  150.       sayerror MISSING_BUFFER__MSG
  151.       stop
  152.    endif
  153.    -- Try to open the buffer.  If it doesn't exist, create it.
  154.    bufhndl = buffer(OPENBUF, buffer_name)
  155.    if bufhndl then
  156.       opened = 1
  157.    else
  158.       -- Make a 64K buffer... memory's plentiful.  Easily changed.
  159.       bufsize = MAXBUFSIZE
  160.       bufhndl = buffer(CREATEBUF, buffer_name, bufsize)
  161.       opened = 0
  162.    endif
  163.    if not bufhndl then
  164.       sayerror CAN_NOT_OPEN__MSG buffer_name '-' ERROR_NUMBER__MSG RC
  165.       stop
  166.    endif
  167.    noflines = buffer(GETBUF, bufhndl)
  168.    if opened then
  169.       call buffer(FREEBUF, bufhndl)
  170.    endif
  171.  
  172. defc pt= -- Put text to buffer.  Argument = noflines.
  173.    noflines = arg(1)
  174.    firstline = .line
  175.    if not noflines then          -- no argument means current line only
  176.       lastline = firstline
  177.    endif
  178.    if noflines='*' then          -- '*' means to end of file
  179.       lastline = .last
  180.    endif
  181.    buffer_name='EXSESS'          -- buffer name:  E across sessions
  182.    call put_shared_text(buffer_name, firstline, lastline)
  183.  
  184. defc gt= -- Get text from buffer.  No argument.
  185.    buffer_name='EXSESS'          -- buffer name:  E across sessions
  186.    call get_shared_text(buffer_name)
  187.