home *** CD-ROM | disk | FTP | other *** search
/ PC Press 1997 July / Sezamfile97_1.iso / msdos / editor / tsetd2.a02 / EXPAND.S < prev    next >
Text File  |  1994-12-14  |  5KB  |  185 lines

  1. /*************************************************************************
  2.   Expand      Expands the current partial word based on match in current file.
  3.  
  4.   Author:     SemWare (Based on ideas from Mel Hulse and Peter Birch)
  5.  
  6.   Date:       Jul  8, 1994
  7.  
  8.   Overview:
  9.  
  10.   This macro file provides a macro that operates like a limited,
  11.   dynamic Template mode.  It expands, or completes, the "partial"
  12.   word to the left of the cursor based on other words in the file.
  13.   When you execute Expand from the Potpourri PickList, the Expand
  14.   macro is assigned to <Ctrl H>.
  15.  
  16.   Keys:
  17.               <Ctrl H>  Expand()
  18.  
  19.   Usage notes:
  20.  
  21.   With this macro activated, just press <Ctrl H> each time you want
  22.   to complete the partial word immediately preceding the cursor.
  23.   The macro searches backward and forward in the file for words
  24.   that begin with the string that you typed as the partial word.
  25.   If only one match is found, the partial word is replaced with the
  26.   expanded word.  If multiple matches are found, the macro first
  27.   displays a PickList of expanded entries from which you can
  28.   choose.  To select an expanded word from the PickList, move the
  29.   cursor bar over the desired entry and press <Enter>.  Your
  30.   partial word is then replaced in the text with the expanded word
  31.   you selected.
  32.  
  33.   Copyright 1992-1994 SemWare Corporation.  All Rights Reserved Worldwide.
  34.  
  35.   Use, modification, and distribution of this SAL macro is encouraged by
  36.   SemWare provided that this statement, including the above copyright
  37.   notice, is not removed; and provided that no fee or other remuneration
  38.   is received for distribution.  You may add your own copyright notice
  39.   to cover new matter you add to the macro, but SemWare Corporation will
  40.   neither support nor assume legal responsibility for any material added
  41.   or any changes made to the macro.
  42.  
  43. *************************************************************************/
  44.  
  45. constant
  46.     EXPAND_KEY = <Ctrl h>,
  47.     COMMAND_COMPLETION_KEY = <CtrlShift h>,
  48.     RANGE = 1000
  49.  
  50. integer work_id, curr_id, maxlen
  51.  
  52. proc PushString(string s)
  53.     integer i = Length(s)
  54.  
  55.     while i
  56.         PushKey(Asc(s[i]))
  57.         i = i - 1
  58.     endwhile
  59. end
  60.  
  61. /************************************************************************
  62.   Gather replacement candidates for abbrev, using the current marked block.
  63.  ************************************************************************/
  64. proc GatherCandidates(string abbrev, string options, integer minlen)
  65.     string s[80]
  66.     integer p
  67.  
  68.     PushPosition()
  69.     while lFind(abbrev, options)
  70.         p = CurrPos()
  71.         if not Left() or not isWord()
  72.             GotoPos(p + minlen)
  73.             if isWord()
  74.                 while isWord() and Right()
  75.                 endwhile
  76.                 s = GetText(p, CurrPos() - p)
  77.                 GotoBufferId(work_id)
  78.                 if not lFind(s, "^$g")
  79.                     EndFile()
  80.                     AddLine(s)
  81.                     if Length(s) > maxlen
  82.                         maxlen = Length(s)
  83.                     endif
  84.                 endif
  85.                 GotoBufferId(curr_id)
  86.             endif
  87.         endif
  88.         GotoPos(p)
  89.     endwhile
  90.     PopPosition()
  91. end
  92.  
  93. keydef ListKeyDef
  94. <SpaceBar>  Down()
  95. end
  96.  
  97. proc GrabSpaceBar()
  98.     Enable(ListKeyDef)
  99.     UnHook(GrabSpaceBar)
  100. end
  101.  
  102. integer proc Expand()
  103.     string abbrev[80], replacement[80]
  104.     integer len, line, ok
  105.  
  106.     Set(Break, on)
  107.     curr_id = GetBufferId()
  108.     maxlen = 12
  109.  
  110.     ok = FALSE
  111.  
  112. // get word to left of cursor
  113.     PushPosition()
  114.     if not Left() or not isWord()
  115.         PopPosition()
  116.         return (ok)
  117.     endif
  118.  
  119.     PushBlock()
  120.     UnMarkBlock()
  121.     MarkColumn()
  122.     BegWord()
  123.     PushPosition()
  124.     abbrev = GetMarkedText()
  125.     len = Length(abbrev)
  126.  
  127. // look above
  128.     if CurrLine() > 1
  129.         MarkLine(CurrLine() - RANGE, CurrLine())
  130.         GatherCandidates(abbrev, "bl", len)
  131.     endif
  132.  
  133. // look below
  134.     if CurrLine() < NumLines()
  135.         MarkLine(CurrLine(), CurrLine() + RANGE)
  136.         GatherCandidates(abbrev, "l+", len)
  137.     endif
  138.  
  139.     PopPosition()               // at replace point
  140. // Choose which one
  141.     GotoBufferId(work_id)
  142.     if NumLines() == 0
  143.         PopPosition()
  144.         Message("Nothing found...")
  145.     else
  146.         GotoLine(1)
  147.         line = 1
  148.         if NumLines() > 1
  149.             PushString(abbrev)
  150.             Hook(_LIST_STARTUP_, GrabSpaceBar)
  151.             line = List("", maxlen)
  152.         endif
  153.         if line == 0
  154.             PopPosition()
  155.         else
  156.             KillPosition()
  157.             replacement = GetText(1, CurrLineLen())
  158.             GotoBufferId(curr_id)
  159.             ok = lReplace(abbrev, replacement, "1")
  160.         endif
  161.     endif
  162.     EmptyBuffer(work_id)
  163.  
  164.     PopBlock()
  165.     return (ok)
  166. end
  167.  
  168. proc WhenLoaded()
  169.     integer id = GetBufferId()
  170.  
  171.     work_id = CreateTempBuffer()
  172.     GotoBufferId(id)
  173.     if work_id == 0
  174.         PurgeMacro(CurrMacroFilename())
  175.     endif
  176. end
  177.  
  178. proc WhenPurged()
  179.     AbandonFile(work_id)
  180. end
  181.  
  182. <EXPAND_KEY>                Expand()
  183. //<COMMAND_COMPLETION_KEY>    CompleteCommand()
  184.  
  185.