move-commands.sl 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % Move-Commands.SL - NMODE Move commands
  4. %
  5. % Author: Alan Snyder
  6. % Hewlett-Packard/CRC
  7. % Date: 23 August 1982
  8. % Revised: 17 February 1983
  9. %
  10. % 17-Feb-83 Alan Snyder
  11. % Bug fix: permanent goal column wasn't permanent.
  12. % 18-Nov-82 Alan Snyder
  13. % Added move-up-list, move-over-list, and move-over-defun commands.
  14. % Changed skip-forward-blanks and skip-backward-blanks.
  15. %
  16. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  17. (CompileTime (load objects fast-int))
  18. (fluid '(nmode-current-buffer
  19. nmode-command-argument
  20. nmode-command-argument-given
  21. nmode-previous-command-function))
  22. % Internal static variables:
  23. (fluid '(nmode-goal-column % permanent goal (set by user)
  24. nmode-temporary-goal-column % temporary goal within cmd sequence
  25. nmode-goal-column-functions % cmds that don't reset temp goal
  26. ))
  27. (setf nmode-goal-column nil)
  28. (setf nmode-temporary-goal-column nil)
  29. (setf nmode-goal-column-functions
  30. (list
  31. (function move-down-command)
  32. (function move-down-extending-command)
  33. (function move-up-command)
  34. (function set-goal-column-command)
  35. ))
  36. (de move-to-buffer-start-command ()
  37. (set-mark-from-point)
  38. (move-to-buffer-start)
  39. )
  40. (de move-to-buffer-end-command ()
  41. (set-mark-from-point)
  42. (move-to-buffer-end)
  43. )
  44. (de move-to-start-of-line-command ()
  45. (current-buffer-goto (+ (current-line-pos) (- nmode-command-argument 1)) 0)
  46. )
  47. (de move-to-end-of-line-command ()
  48. (move-to-start-of-line-command)
  49. (move-to-end-of-line))
  50. (de set-goal-column-command ()
  51. (cond ((= nmode-command-argument 1)
  52. (setf nmode-goal-column (current-display-column))
  53. (write-prompt (BldMsg "Goal Column = %p" nmode-goal-column))
  54. )
  55. (t
  56. (setf nmode-goal-column NIL)
  57. (write-prompt "No Goal Column")
  58. )))
  59. (de setup-goal-column ()
  60. % If this is the first in a new (potential) sequence of up/down commands,
  61. % then set the temporary goal column for that sequence of commands.
  62. (if (not (memq nmode-previous-command-function nmode-goal-column-functions))
  63. (setf nmode-temporary-goal-column (current-display-column)))
  64. )
  65. (de goto-goal-column ()
  66. % Move the cursor to the current goal column, which is the permanent goal
  67. % column (if set by the user) or the temporary goal column (otherwise).
  68. (cond (nmode-goal-column
  69. (set-display-column nmode-goal-column))
  70. (nmode-temporary-goal-column
  71. (set-display-column nmode-temporary-goal-column))
  72. ))
  73. (de move-up-command ()
  74. (setup-goal-column)
  75. (set-line-pos (- (current-line-pos) nmode-command-argument))
  76. (goto-goal-column)
  77. )
  78. (de move-down-extending-command ()
  79. (when (and (not nmode-command-argument-given) (current-line-is-last?))
  80. (let ((old-pos (buffer-get-position)))
  81. (move-to-buffer-end)
  82. (insert-eol)
  83. (buffer-set-position old-pos)
  84. ))
  85. (move-down-command)
  86. )
  87. (de move-down-command ()
  88. (setup-goal-column)
  89. (set-line-pos (+ (current-line-pos) nmode-command-argument))
  90. (goto-goal-column)
  91. )
  92. (de exchange-point-and-mark ()
  93. (let ((old-mark (current-mark)))
  94. (previous-mark) % pop off the old mark
  95. (set-mark-from-point) % push the new one
  96. (buffer-set-position old-mark)
  97. ))
  98. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  99. % Skipping Blanks
  100. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  101. (de char-blank-or-newline? (ch)
  102. (or (char-blank? ch) (= ch #\LF)))
  103. (de skip-forward-blanks ()
  104. % Skip over "blanks", return the first non-blank character seen.
  105. % Cursor is positioned to the left of that character.
  106. (while (and (not (at-buffer-end?))
  107. (char-blank-or-newline? (next-character))
  108. )
  109. (move-forward))
  110. (next-character))
  111. (de skip-backward-blanks ()
  112. % Skip backwards over "blanks", return the first non-blank character seen.
  113. % Cursor is positioned to the right of that character.
  114. (while (and (not (at-buffer-start?))
  115. (char-blank-or-newline? (previous-character))
  116. )
  117. (move-backward))
  118. (previous-character))
  119. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  120. % Move-Over-Characters commands
  121. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  122. (de move-forward-character-command ()
  123. (if (not (move-over-characters nmode-command-argument))
  124. (Ding)))
  125. (de move-backward-character-command ()
  126. (if (not (move-over-characters (- nmode-command-argument)))
  127. (Ding)))
  128. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  129. % Move-Over-Word commands
  130. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  131. (de move-forward-word-command ()
  132. (if (not (move-over-words nmode-command-argument))
  133. (Ding)))
  134. (de move-backward-word-command ()
  135. (if (not (move-over-words (- nmode-command-argument)))
  136. (Ding)))
  137. (de move-over-words (n)
  138. % Move forward (n>0) or backwards (n<0) over |n| words. Return T if the
  139. % specified number of words were found, NIL otherwise. The cursor remains at
  140. % the last word found.
  141. (let ((flag T))
  142. (while (and (> n 0) (setf flag (move-forward-word)))
  143. (setf n (- n 1)))
  144. (while (and (< n 0) (setf flag (move-backward-word)))
  145. (setf n (+ n 1)))
  146. flag))
  147. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  148. % Move-Over-Form commands
  149. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  150. (de move-forward-form-command ()
  151. (if (not (move-over-forms nmode-command-argument))
  152. (Ding)))
  153. (de move-backward-form-command ()
  154. (if (not (move-over-forms (- nmode-command-argument)))
  155. (Ding)))
  156. (de move-over-forms (n)
  157. % Move forward (n>0) or backwards (n<0) over |n| forms. Return T if the
  158. % specified number of forms were found, NIL otherwise. The cursor remains at
  159. % the last form found.
  160. (let ((flag T))
  161. (while (and (> n 0) (setf flag (move-forward-form)))
  162. (setf n (- n 1)))
  163. (while (and (< n 0) (setf flag (move-backward-form)))
  164. (setf n (+ n 1)))
  165. flag))
  166. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  167. % Move-Up-List commands
  168. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  169. (de forward-up-list-command ()
  170. (if (not (move-up-lists nmode-command-argument))
  171. (Ding)))
  172. (de backward-up-list-command ()
  173. (if (not (move-up-lists (- nmode-command-argument)))
  174. (Ding)))
  175. (de move-up-lists (n)
  176. % Move forward (n>0) or backwards (n<0) out of |n| lists (structures).
  177. % Return T if the specified number of brackets were found, NIL otherwise.
  178. % The cursor remains at the last bracket found.
  179. (let ((flag T))
  180. (while (and (> n 0) (setf flag (move-forward-up-list)))
  181. (setf n (- n 1)))
  182. (while (and (< n 0) (setf flag (move-backward-up-list)))
  183. (setf n (+ n 1)))
  184. flag
  185. ))
  186. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  187. % Move-Over-List commands
  188. %
  189. % Note: In EMACS, these commands were motivated by the fact that EMACS did
  190. % not understand Lisp comments. Thus, in EMACS, move-forward-list could be
  191. % used as a move-forward-form that ignored comments. Since NMODE does
  192. % understand comments, it is not clear that these commands have any use.
  193. %
  194. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  195. (de move-forward-list-command ()
  196. (if (not (move-over-lists nmode-command-argument))
  197. (Ding)))
  198. (de move-backward-list-command ()
  199. (if (not (move-over-lists (- nmode-command-argument)))
  200. (Ding)))
  201. (de move-over-lists (n)
  202. % Move forward (n>0) or backwards (n<0) over |n| lists (structures).
  203. % Return T if the specified number of lists were found, NIL otherwise.
  204. % The cursor remains at the last list found.
  205. (let ((flag T))
  206. (while (and (> n 0) (setf flag (move-forward-list)))
  207. (setf n (- n 1)))
  208. (while (and (< n 0) (setf flag (move-backward-list)))
  209. (setf n (+ n 1)))
  210. flag
  211. ))
  212. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  213. % Move-Over-Defun commands
  214. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  215. (de move-forward-defun-command ()
  216. (if (not (move-over-defuns nmode-command-argument))
  217. (Ding)))
  218. (de move-backward-defun-command ()
  219. (if (not (move-over-defuns (- nmode-command-argument)))
  220. (Ding)))
  221. (de move-over-defuns (n)
  222. % Move forward (n>0) or backwards (n<0) over |n| defuns.
  223. % Return T if the specified number of defuns were found, NIL otherwise.
  224. % The cursor remains at the last defun found.
  225. (let ((flag T))
  226. (while (and (> n 0) (setf flag (move-forward-defun)))
  227. (setf n (- n 1)))
  228. (while (and (< n 0) (setf flag (move-backward-defun)))
  229. (setf n (+ n 1)))
  230. flag
  231. ))
  232. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  233. % Basic Character Movement Primitives
  234. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  235. (de move-over-characters (n)
  236. % Move forward (n>0) or backwards (n<0) over |n| characters. Return T if the
  237. % specified number of characters were found, NIL otherwise. The cursor
  238. % remains at the last character found.
  239. (let ((flag T))
  240. (while (and (> n 0) (setf flag (move-forward-character)))
  241. (setf n (- n 1)))
  242. (while (and (< n 0) (setf flag (move-backward-character)))
  243. (setf n (+ n 1)))
  244. flag))
  245. (de move-forward-character ()
  246. % Move forward one character. If there is no next character, leave cursor
  247. % unchanged and return NIL; otherwise, return T.
  248. (if (at-buffer-end?)
  249. NIL
  250. (move-forward)
  251. T
  252. ))
  253. (de move-backward-character ()
  254. % Move backward one character. If there is no previous character, leave
  255. % cursor unchanged and return NIL; otherwise, return T.
  256. (if (at-buffer-start?)
  257. NIL
  258. (move-backward)
  259. T
  260. ))
  261. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  262. % Basic Character Movement Primitives (Hacking Tabs Version)
  263. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  264. (de move-over-characters-hacking-tabs (n)
  265. % Move forward (n>0) or backwards (n<0) over |n| characters. Return T if the
  266. % specified number of characters were found, NIL otherwise. The cursor
  267. % remains at the last character found.
  268. (let ((flag T))
  269. (while (and (> n 0) (setf flag (move-forward-character-hacking-tabs)))
  270. (setf n (- n 1)))
  271. (while (and (< n 0) (setf flag (move-backward-character-hacking-tabs)))
  272. (setf n (+ n 1)))
  273. flag))
  274. (de move-forward-character-hacking-tabs ()
  275. % Move forward one character. If the next character is a tab, first
  276. % replace it with the appropriate number of spaces. If there is no next
  277. % character, leave cursor unchanged and return NIL; otherwise, return T.
  278. (if (at-buffer-end?)
  279. NIL
  280. (cond ((= (next-character) (char TAB))
  281. (delete-next-character)
  282. (let ((n (- 8 (& (current-display-column) 7))))
  283. (insert-string (substring " " 0 n))
  284. (set-char-pos (- (current-char-pos) n))
  285. )))
  286. (move-forward)
  287. T
  288. ))
  289. (de move-backward-character-hacking-tabs ()
  290. % Move backward one character. If the previous character is a tab, first
  291. % replace it with the appropriate number of spaces. If there is no previous
  292. % character, leave cursor unchanged and return NIL; otherwise, return T.
  293. (if (at-buffer-start?)
  294. NIL
  295. (cond ((= (previous-character) (char TAB))
  296. (delete-previous-character)
  297. (let ((n (- 8 (& (current-display-column) 7))))
  298. (insert-string (substring " " 0 n))
  299. )))
  300. (move-backward)
  301. T
  302. ))
  303. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  304. % Basic Word Movement Primitives
  305. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  306. (de word-char? (ch)
  307. (or (AlphanumericP ch) (= ch (char -))))
  308. (de move-forward-word ()
  309. % Move forward one "word", starting from point. Leave cursor to the
  310. % right of the "word". If there is no next word, leave cursor unchanged
  311. % and return NIL; otherwise, return T.
  312. (let ((old-pos (buffer-get-position)))
  313. (while (and (not (at-buffer-end?)) % scan for start of word
  314. (not (word-char? (next-character)))
  315. )
  316. (move-forward))
  317. (cond ((at-buffer-end?)
  318. (buffer-set-position old-pos)
  319. NIL
  320. )
  321. (t
  322. (while (and (not (at-buffer-end?)) % scan for end of word
  323. (word-char? (next-character))
  324. )
  325. (move-forward))
  326. T
  327. ))))
  328. (de move-backward-word ()
  329. % Move backward one "word", starting from point. Leave cursor to the left of
  330. % the "word". If there is no previous word, leave cursor unchanged and
  331. % return NIL; otherwise, return T.
  332. (let ((old-pos (buffer-get-position)))
  333. (while (and (not (at-buffer-start?)) % scan for end of word
  334. (not (word-char? (previous-character)))
  335. )
  336. (move-backward))
  337. (cond ((at-buffer-start?)
  338. (buffer-set-position old-pos)
  339. NIL
  340. )
  341. (t
  342. (while (and (not (at-buffer-start?)) % scan for start of word
  343. (word-char? (previous-character))
  344. )
  345. (move-backward))
  346. T
  347. ))))