prompting.sl 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % Prompting.SL - NMODE Prompt Line Manager
  4. %
  5. % Author: Alan Snyder
  6. % Hewlett-Packard/CRC
  7. % Date: 19 August 1982
  8. % Revised: 16 February 1983
  9. %
  10. % Adapted from Will Galway's EMODE.
  11. %
  12. % 16-Feb-83 Alan Snyder
  13. % Declare -> Declare-Flavor.
  14. % 7-Feb-83 Alan Snyder
  15. % Use one-window or one-screen refresh.
  16. % 29-Dec-82 Alan Snyder
  17. % Revised input completion support to run completion characters as commands
  18. % rather than terminating and resuming. Added new functions to manipulate the
  19. % input buffer.
  20. % 22-Dec-82 Jeffrey Soreff
  21. % Revised to handle control characters on prompt and message lines.
  22. % 21-Dec-82 Alan Snyder
  23. % Efficiency improvement: Added declarations for virtual screens and buffer
  24. % windows.
  25. %
  26. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  27. (CompileTime (load objects extended-char fast-strings fast-int))
  28. % External variables used:
  29. (fluid
  30. '(nmode-prompt-screen
  31. nmode-message-screen
  32. nmode-input-window
  33. nmode-current-window
  34. ))
  35. % Global variables defined here:
  36. (fluid
  37. '(nmode-input-default
  38. ))
  39. % Internal static variables:
  40. (fluid
  41. '(nmode-prompt-cursor
  42. nmode-message-cursor
  43. nmode-message-string
  44. nmode-input-level
  45. nmode-input-special-command-list
  46. ))
  47. (setf nmode-prompt-cursor 0)
  48. (setf nmode-message-cursor 0)
  49. (setf nmode-message-string "")
  50. (setf nmode-input-level 0)
  51. (setf nmode-input-default NIL)
  52. (declare-flavor virtual-screen nmode-prompt-screen nmode-message-screen)
  53. (declare-flavor buffer-window nmode-input-window nmode-current-window)
  54. (declare-flavor text-buffer input-buffer)
  55. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  56. % String input:
  57. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  58. (de prompt-for-string (prompt-string default-string)
  59. % Prompt for a string (terminated by CR or NL). Use default-string if an
  60. % empty string is returned (and default-string is non-NIL). The original
  61. % message line is restored, but not refreshed. Note: if you attempt to use
  62. % this function recursively, it will automatically throw '$ERROR$. The effect
  63. % of this action is that in string-input mode, commands that request string
  64. % input appear to be undefined. (This assumes that all such commands do
  65. % nothing visible before they first request string input.)
  66. (prompt-for-string-special prompt-string default-string NIL))
  67. (de prompt-for-string-special (prompt-string default-string command-list)
  68. % This function is similar to PROMPT-FOR-STRING, except that it accepts a
  69. % command list that specifies a set of additional commands to be defined
  70. % while the user is typing at the input window.
  71. (if (> nmode-input-level 0)
  72. (throw '$error$ NIL)
  73. % else
  74. (setf nmode-input-special-command-list command-list)
  75. (setf nmode-input-default default-string)
  76. (let ((old-msg nmode-message-string)
  77. (old-window nmode-current-window)
  78. (nmode-input-level (+ nmode-input-level 1)) % FLUID
  79. )
  80. (if default-string
  81. (setf prompt-string
  82. (string-concat prompt-string " (Default is: '" default-string "')")))
  83. (=> (=> nmode-input-window buffer) reset)
  84. (nmode-select-window nmode-input-window)
  85. (set-message prompt-string)
  86. (set-prompt "") % avoid old prompt popping back up when we're done
  87. % Edit the buffer until an "exit" character is typed or the user aborts.
  88. (cond ((eq (NMODE-reader T) 'abort)
  89. (=> nmode-input-window deexpose)
  90. (nmode-select-window old-window)
  91. (set-message old-msg)
  92. (throw 'abort NIL)
  93. ))
  94. % Show the user that his input has been accepted.
  95. (move-to-start-of-line)
  96. (nmode-refresh-one-window nmode-input-window)
  97. % Pick up the string that was typed.
  98. (let ((return-string (current-line)))
  99. % Switch back to old window, etc.
  100. (=> nmode-input-window deexpose)
  101. (nmode-select-window old-window)
  102. % Restore original "message window".
  103. (set-message old-msg)
  104. % If an empty string, use default (unless it's NIL).
  105. (if (and default-string (equal return-string ""))
  106. default-string
  107. return-string
  108. )))))
  109. (de nmode-substitute-default-input ()
  110. % If the input buffer is empty and there is a default string, then stuff the
  111. % default string into the input buffer.
  112. (let ((input-buffer (=> nmode-input-window buffer)))
  113. (if (and (=> input-buffer at-buffer-start?)
  114. (=> input-buffer at-buffer-end?)
  115. nmode-input-default
  116. (stringp nmode-input-default)
  117. )
  118. (=> input-buffer insert-string nmode-input-default)
  119. )))
  120. (de nmode-get-input-string ()
  121. % Return the contents of the input buffer as a string. If the buffer contains
  122. % more than one line, only the current line is returned.
  123. (let ((input-buffer (=> nmode-input-window buffer)))
  124. (=> input-buffer current-line)
  125. ))
  126. (de nmode-replace-input-string (s)
  127. % Replace the contents of the input buffer with the specified string.
  128. (let ((input-buffer (=> nmode-input-window buffer)))
  129. (=> input-buffer reset)
  130. (=> input-buffer insert-string s)
  131. ))
  132. (de nmode-terminate-input ()
  133. % A command bound to this function will act to terminate string input.
  134. (exit-nmode-reader)
  135. )
  136. (de nmode-yank-default-input ()
  137. % A command bound to this function will act to insert the default string into
  138. % the input buffer.
  139. (if nmode-input-default
  140. (insert-string nmode-input-default)
  141. (Ding)
  142. ))
  143. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  144. % Prompt line functions:
  145. %
  146. % NOTE: if your intent is to display a prompt string for user input, you should
  147. % use a function defined in TERMINAL-INPUT rather than one of these.
  148. %
  149. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  150. (de write-prompt (msg)
  151. % Write the specified string to the prompt line and refresh the prompt
  152. % line. Note: the major windows are not refreshed.
  153. (set-prompt msg)
  154. (nmode-refresh-virtual-screen nmode-prompt-screen)
  155. )
  156. (de set-prompt (msg)
  157. % Write the specified string to the prompt window, but do not refresh.
  158. (setf nmode-prompt-cursor 0)
  159. (=> nmode-prompt-screen clear)
  160. (prompt-append-string msg)
  161. )
  162. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  163. % Message line functions:
  164. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  165. (de write-message (msg)
  166. % Display a string to the message window and refresh the message window.
  167. % Note: the major windows are not refreshed.
  168. % Return the previous message string.
  169. (prog1
  170. (set-message msg)
  171. (nmode-refresh-virtual-screen nmode-message-screen)
  172. ))
  173. (de rewrite-message ()
  174. % Rewrite the existing message (used when the default enhancement changes).
  175. (set-message nmode-message-string)
  176. )
  177. (de set-message (msg)
  178. % Display a string in the "message" window, do not refresh.
  179. % Message will not appear until a refresh is done.
  180. % Return the previous message string.
  181. (let ((old-message nmode-message-string))
  182. (setf nmode-message-string msg)
  183. (setf nmode-message-cursor 0)
  184. (=> nmode-message-screen clear)
  185. (message-append-string msg)
  186. old-message
  187. ))
  188. (de reset-message ()
  189. % Clear the "message" window, but do not refresh.
  190. (setf nmode-message-string "")
  191. (setf nmode-message-cursor 0)
  192. (=> nmode-message-screen clear)
  193. )
  194. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  195. % Internal functions:
  196. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  197. (de prompt-append-string (s)
  198. (for (from i 0 (string-upper-bound s))
  199. (do (prompt-append-character (string-fetch s i)))))
  200. (de prompt-append-character (ch)
  201. (cond
  202. ((or (< ch #\space) (= ch #\rubout)) % Control Characters
  203. (=> nmode-prompt-screen write #/^ 0 nmode-prompt-cursor)
  204. (setf nmode-prompt-cursor (+ nmode-prompt-cursor 1))
  205. (=> nmode-prompt-screen write (^ ch 8#100) 0 nmode-prompt-cursor)
  206. (setf nmode-prompt-cursor (+ nmode-prompt-cursor 1)))
  207. (t (=> nmode-prompt-screen write ch 0 nmode-prompt-cursor) % Normal Char
  208. (setf nmode-prompt-cursor (+ nmode-prompt-cursor 1)))))
  209. (de message-append-string (s)
  210. (for (from i 0 (string-upper-bound s))
  211. (do (message-append-character (string-fetch s i)))))
  212. (de message-append-character (ch)
  213. (cond
  214. ((or (< ch #\space) (= ch #\rubout)) % Control Characters
  215. (=> nmode-message-screen write #/^ 0 nmode-message-cursor)
  216. (setf nmode-message-cursor (+ nmode-message-cursor 1))
  217. (=> nmode-message-screen write (^ ch 8#100) 0 nmode-message-cursor)
  218. (setf nmode-message-cursor (+ nmode-message-cursor 1)))
  219. (t (=> nmode-message-screen write ch 0 nmode-message-cursor) % Normal Char
  220. (setf nmode-message-cursor (+ nmode-message-cursor 1)))))
  221. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  222. (undeclare-flavor nmode-prompt-screen nmode-message-screen)
  223. (undeclare-flavor nmode-input-window nmode-current-window)
  224. (undeclare-flavor input-buffer)