home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / UTILITY / KEYBOARD / CMDED2E5.ZIP / HISTORY.ASM < prev    next >
Encoding:
Assembly Source File  |  1990-12-16  |  9.3 KB  |  370 lines

  1. ; HISTORY.ASM
  2. ; (c) 1989, 1990 Ashok P. Nadkarni
  3. ;
  4. ; This module implements the history feature of the cmdedit program.
  5. ; DFA stopped lines of length less than min_length being stored on stack
  6. ; (December 1990).
  7.  
  8.     INCLUDE common.inc
  9.     INCLUDE    buffers.inc
  10.  
  11.     PUBLIC    hist_top
  12.     PUBLIC    hist_init
  13.     PUBLIC    hist_fwd
  14.     PUBLIC    hist_back
  15.     PUBLIC    hist_fwd_match
  16.     PUBLIC    hist_bck_match
  17.     PUBLIC    remember
  18.     PUBLIC    hist_type
  19.     PUBLIC    execute_auto_recall
  20.     PUBLIC    execute_rsthist
  21.     PUBLIC    hist_ptr
  22.     PUBLIC    history
  23.  
  24. CSEG    SEGMENT    PARA PUBLIC 'CODE'
  25.  
  26.  
  27.     EXTRN    linebuf:BYTE        ;line buffer
  28.     EXTRN    lastchar:WORD
  29.     EXTRN    dot:WORD        ;current position in line buffer
  30.     EXTRN   min_length:WORD  ;added by dfa
  31.  
  32.  
  33. ; The history stack is maintained as a string stack using the functions in
  34. ; the string stack library. The top and bottom of the stack are always
  35. ; zero length strings (the bottom is always a zero length sentinel
  36. ; implemented by the string stack package but the top is explicitly 
  37. ; maintained by the history code.
  38. history    $string_stack 2 DUP (<>)    ;Buffer descriptors
  39. hist_ptr    DW    ?        ;Ptr to current buffer descriptor
  40.  
  41. DGROUP    GROUP    CSEG
  42.  
  43.     EXTRN    bell:PROC
  44.     EXTRN    set_disp_marks:PROC
  45.     EXTRN    erase_to_dot:PROC
  46.  
  47.     ASSUME    CS:DGROUP,DS:DGROUP,ES:DGROUP,SS:DGROUP
  48.  
  49.  
  50. ;+
  51. ; FUNCTION : hist_init
  52. ;
  53. ; Initializes the various data structures associated with the
  54. ; command history buffers. CALLER MUST ENSURE PASSED ADDRESSES ARE VALID
  55. ; AND BUFFER IS LARGE ENOUGH.
  56. ;
  57. ; Parameters:
  58. ;    AX     - length of buffer in bytes
  59. ;    BX    - address of buffer
  60. ;    CX    - 0 if DOS buffer, any other value if application buffer
  61. ; Returns:
  62. ;    Nothing.
  63. ; Registers destroyed : BX,CX
  64. ;-
  65. hist_init proc    near
  66.     push    bx
  67.     mov    bx,offset DGROUP:history ;bx := address of buffer descriptors
  68.     jcxz    @hist_init_1        ;if DOS, bx OK
  69.     add    bx,TYPE $string_stack    ;else point to application descriptor
  70. @hist_init_1:
  71.     xchg    ax,cx            ;CX = buffer size
  72.     pop    ax            ;AX = Buffer address
  73.                     ;BX points to appropriate descriptor
  74.     call    near ptr strstk_init    ;Initialize buffer and descriptor
  75.     xor    al,al            ;Push a zero length string onto stack
  76.     mov    cl,1            ;Force onto stack
  77.     call    near ptr strstk_push    ;Assumes no errors
  78.     ret
  79. hist_init    endp
  80.  
  81.  
  82. ;+
  83. ; FUNCTION : execute_rsthist
  84. execute_rsthist proc    near
  85.     mov    bx,offset DGROUP:history ;bx := address of buffer descriptors
  86.     jcxz    @execute_rsthist_1    ;if DOS, bx OK
  87.     add    bx,TYPE $string_stack    ;else point to application descriptor
  88. @execute_rsthist_1:
  89.     call    near ptr strstk_reset    ;Initialize buffer and descriptor
  90.     xor    al,al            ;Push a zero length string onto stack
  91.     mov    cl,1            ;Force onto stack
  92.     call    near ptr strstk_push    ;Assumes no errors
  93.     ret
  94. execute_rsthist    endp
  95.  
  96.  
  97.  
  98. ;+
  99. ; FUNCTION : hist_type
  100. ;
  101. ;    Sets the hist_ptr global to point at the descriptor for the
  102. ;    current caller (DOS or application).
  103. ;
  104. ; Parameters:
  105. ;    CX =    0 if DOS, anything else for application
  106. ;
  107. ; Returns:
  108. ;    Nothing.
  109. ; Registers destroyed : BX
  110. ;-
  111. hist_type proc near
  112.     mov    bx,offset DGROUP:history ;address of first descriptor
  113.     mov    hist_ptr,bx
  114.     ret
  115. hist_type endp
  116.  
  117.  
  118. ;+
  119. ; FUNCTION : hist_top
  120. ;
  121. ;    Resets the history pointer to the null string at top of history stack.
  122. ;    The line buffer itslef is NOT changed.
  123. ; Parameters:
  124. ;    None.
  125. ;
  126. ; Returns:
  127. ;    Nothing.
  128. ; Registers destroyed:  BX
  129. ;-
  130. hist_top proc near
  131.     mov    bx,hist_ptr        ;Point to current descriptor
  132.     call    near ptr strstk_settop    ;Set to top of history stack.
  133.  
  134.     ret
  135. hist_top endp
  136.  
  137.  
  138. ;+
  139. ; FUNCTION : hist_back
  140. ;
  141. ;    Gets the previous history line (if there is one) and stores it
  142. ;    in the line buffer. Also makes it the current line.
  143. ;
  144. ; Parameters:
  145. ;    None.
  146. ;
  147. ; Returns:
  148. ;    Nothing.
  149. ; Registers destroyed: AX,BX,CX
  150. ;-
  151. hist_back proc near
  152.     mov    dot,offset DGROUP:linebuf
  153.     jmp    hist_bck_match        ;Will return to caller
  154. hist_back endp
  155.  
  156.  
  157.  
  158. ;+
  159. ; FUNCTION : hist_fwd
  160. ;
  161. ;    Gets the next history line (if there is one) and stores it
  162. ;    in the line buffer. Also makes it the current line.
  163. ;
  164. ; Parameters:
  165. ;    None.
  166. ;
  167. ; Returns:
  168. ;    Nothing.
  169. ; Registers destroyed :
  170. ;-
  171. hist_fwd proc near
  172.     mov    dot,offset DGROUP:linebuf
  173.     jmp    hist_fwd_match        ;Will return to caller
  174. hist_fwd endp
  175.  
  176.  
  177.  
  178. ;+
  179. ; FUNCTION : hist_copy
  180. ;
  181. ;    Copies the current history line into the line buffer. The dot
  182. ;    is set to the beginning of the line.
  183. ;
  184. ; Parameters:
  185. ;    BX =    points to the beginning of a line in the history buffer.
  186. ;
  187. ; Returns:
  188. ;    Nothing.
  189. ;
  190. ; Registers destroyed: 
  191. ;-
  192. hist_copy proc near
  193.     mov    bx,hist_ptr    ;Current descriptor
  194.     mov    ax,offset DGROUP:linebuf ;the history line storage
  195.     push    ax        ;Remember
  196.     mov    dot,ax        ;Cursor will be at the beginning of line
  197.     xchg    ax,dx        ;dx->first char in line (parameter)
  198.     mov    ax,lastchar    ;ax->upper limit (parameter)
  199.     call    near ptr set_disp_marks ;Indicate changed range
  200.                 ;No registers destroyed
  201.     mov    ax,dx        ;Restore beginning of line
  202.     sub    cx,LINEBUF_SIZE    ;cx<-max length of line
  203.     push    cx        ;Save it
  204.     call    near ptr strstk_copy ;Copy history line into linebuf
  205.                 ;AX == length of history string
  206.     pop    cx        ;
  207.     jnc    @hist_copy_90    ;Jump if no error from strstk_copy
  208.     call    near ptr bell    ;History line too long for buffer
  209.     mov    ax,cx        ;Number of bytes = max length of line
  210. @hist_copy_90:
  211.     pop    dx        ;dx->beginning of line
  212.     add    ax,dx        ;ax->end
  213.     mov    lastchar,ax    ;Update end of line
  214.     call    near ptr set_disp_marks ;Indicate changed range
  215.                 ;No registers destroyed
  216.     ret
  217. hist_copy endp
  218.  
  219.  
  220.  
  221. ;+
  222. ; FUNCTION : remember
  223. ;
  224. ;    Saves the line buffer in the history area. If the line already
  225. ;    exists, it is moved to the top of the stack.
  226. ;   Added by dfa:  lines less than min_length are ignored.
  227. ;
  228. ; Parameters:
  229. ;    None.
  230. ;
  231. ; Returns:
  232. ;    Nothing.
  233. ;
  234. ; Registers destroyed:
  235. ;    AX,BX,CX,DX
  236. ;-
  237. remember proc near
  238.     mov    bx,hist_ptr        ;Descriptor
  239.     call    near ptr strstk_settop    ;Reset stack pointer
  240.     mov    ax,offset DGROUP:linebuf ;AX->line to be stored
  241.     mov    cx,lastchar
  242.     sub    cx,ax            ;CX<-length of line
  243.     cmp cx,min_length    ;these two lines added by dfa
  244.     jb @remember_20      ;ignore request if line length too short
  245.     call    near ptr strstk_bck_find ;Look for it in history
  246. ;                      buffer. Note that if it is a
  247. ;                      null string, the sentinel at
  248. ;                      top of the stack is not found
  249. ;                      which is at it should be.
  250.     jc    @remember_10        ;Not in stack
  251.     call    near ptr strstk_kill    ;If found delete it from stack
  252. @remember_10:
  253.     call    near ptr strstk_settop    ;Point to sentinel at top of stack
  254.     call    near ptr strstk_kill    ;Delete it
  255.     mov    dx,offset DGROUP:linebuf ;String to be remembered
  256.     mov    ax,lastchar        ;Length of string
  257.     sub    ax,dx            ;AX(AL)<-length of string
  258.     mov    cl,1            ;Force the push
  259.     call    near ptr strstk_push    ;ignore success/fail status
  260.     xor    ax,ax            ;Zero length sentinel
  261.     mov    cl,1
  262.     call    near ptr strstk_push    ;Push it
  263. @remember_20:  ; label added by dfa
  264.     ret
  265. remember endp
  266.  
  267.  
  268.  
  269. ;+
  270. ; FUNCTION : hist_fwd_match, hist_bck_match
  271. ;
  272. ;    Looks fwd/back thru the history buffer starting from the current
  273. ;    history location looking for a line whose first n chracters match
  274. ;    the n characters before the current position in the line buffer.
  275. ;
  276. ; Parameters:
  277. ;    None.
  278. ;
  279. ; Returns:
  280. ;    CF    = 0 if match found
  281. ;          1 if no match
  282. ;
  283. ; Registers destroyed:
  284. ;-
  285. hist_match proc near
  286. hist_fwd_match LABEL near
  287.     mov    dx,offset DGROUP:strstk_fwd_match
  288.     jmp    short @hist_match_10
  289. hist_bck_match label near
  290.     mov    dx,offset DGROUP:strstk_bck_match
  291. @hist_match_10:
  292.     mov    bx,hist_ptr    ;Descriptor address
  293.     mov    ax,offset DGROUP:linebuf ;start of pattern
  294.     mov    cx,dot        ;Current position in linebuffer
  295.     sub    cx,ax        ;Number of chars to match
  296.     push    cx        ;Save
  297.     push    ax
  298.     call    dx        ;Will set CF if no match, else sets
  299.                 ; 'current' to matched string
  300.     pushf            ;Remember flags
  301.     jc    @hist_match_20    ;No match
  302.     call    near ptr hist_copy ;Copy and display the history line
  303.                 ;Now reset it to original position
  304. @hist_match_20:
  305.     popf            ;Restore flags
  306.     pop    ax        ;Line address
  307.     pop    cx        ;Restore count
  308.     pushf            ;Save flags
  309.     add    ax,cx        ;AX->dot
  310.     mov    dot,ax
  311.     popf            ;Restore flags
  312.     jnc    @hist_match_99    ;If match was found, exit
  313.     mov    ax,lastchar    ;AX->end-of-line
  314.     call    erase_to_dot    ;Else delete remaining chars in the
  315. ;                 line (chars between AX and dot)
  316.     stc            ;Indicate no match
  317. @hist_match_99:
  318.     ret
  319. hist_match endp
  320.  
  321.  
  322.  
  323. ;+
  324. ; FUNCTION : execute_auto_recall
  325. ;
  326. ;    This function looks backward through the history buffer for a
  327. ;    line with a prefix that matches the characters to the left of
  328. ;    the dot. The search begins with the CURRENT history line. ie.
  329. ;    if the current history line has a matching prefix, the current
  330. ;    pointer is not changed. If no match is found, the history stack
  331. ;    is reset.
  332. ;
  333. ; Parameters:
  334. ;    None.
  335. ;
  336. ; Returns:
  337. ;    CF    = 0 if match found
  338. ;          1 if no match
  339. ; Register(s) destroyed:
  340. ;    AX,BX,CX,DX
  341. ;-
  342. execute_auto_recall proc near
  343.     mov    bx,hist_ptr        ;BX->current history stack descriptor
  344.     mov    ax,offset DGROUP:linebuf ;AX->pattern
  345.     mov    cx,dot
  346.     sub    cx,ax            ;CX<-length of pattern
  347.     call    near ptr strstk_prefix    ;Current history line matches?
  348.     jne    @execute_auto_recall_50    ;No
  349.     clc                ;Yes, current line matches
  350.     jmp    short @execute_auto_recall_99
  351.  
  352. @execute_auto_recall_50:
  353.     call    near ptr hist_bck_match    ;Search backward
  354.     jnc    @execute_auto_recall_99    ;Found a match
  355. ;    No match, reset history stack pointer
  356.     mov    bx,hist_ptr        ;BX->current history stack descriptor
  357.     call    near ptr hist_top    ;Reset history stack
  358.     stc                ;Indicate no match
  359. @execute_auto_recall_99:
  360.     ret
  361. execute_auto_recall endp
  362.  
  363. CSEG    ENDS
  364.  
  365.     END
  366.