defun-commands.sl 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % Defun-Commands.SL - NMODE DEFUN commands and functions
  4. %
  5. % Author: Alan Snyder
  6. % Hewlett-Packard/CRC
  7. % Date: 12 November 1982
  8. %
  9. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  10. (CompileTime (load objects fast-int))
  11. (fluid '(nmode-command-argument
  12. nmode-command-argument-given
  13. nmode-current-command
  14. ))
  15. % Global variables:
  16. (fluid '(nmode-defun-predicate
  17. nmode-defun-scanner
  18. ))
  19. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  20. % Defun Commands
  21. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  22. (de reposition-window-command ()
  23. % Adjust the current window so that the beginning of the
  24. % current DEFUN is on the top line of the screen. If this change
  25. % would push the current line off the screen, do nothing but ring
  26. % the bell.
  27. (let ((old-pos (buffer-get-position)))
  28. (when (move-to-start-of-current-defun) % if search for defun succeeds
  29. (let ((old-line (buffer-position-line old-pos))
  30. (defun-line (current-line-pos))
  31. )
  32. (if (or (< old-line defun-line) % Impossible?
  33. (>= old-line (+ defun-line (current-window-height)))
  34. )
  35. (Ding) % Old Line wouldn't show on the screen
  36. % otherwise
  37. (current-window-set-top-line defun-line)
  38. ))
  39. (buffer-set-position old-pos)
  40. )))
  41. (de end-of-defun-command ()
  42. % This command has a very strange definition in EMACS. I don't even
  43. % want to try to explain it! It is probably a kludge in EMACS since
  44. % it generates very strange error messages!
  45. (if (< nmode-command-argument 0)
  46. (move-backward))
  47. % First, we must get positioned up at the beginning of the proper defun.
  48. % If we are within a defun, we want to start at the beginning of that
  49. % defun. If we are between defuns, then we want to start at the beginning
  50. % of the next defun.
  51. (if (not (move-to-start-of-current-defun))
  52. (move-forward-defun))
  53. % Next, we move to the requested defun, and complain if we can't find it.
  54. (unless
  55. (cond
  56. ((> nmode-command-argument 1)
  57. (move-over-defuns (- nmode-command-argument 1)))
  58. ((< nmode-command-argument 0)
  59. (move-over-defuns nmode-command-argument))
  60. (t t)
  61. )
  62. (Ding)
  63. )
  64. % Finally, we move to the end of whatever defun we wound up at.
  65. (if (not (move-to-end-of-current-defun)) (Ding))
  66. )
  67. (de mark-defun-command ()
  68. (cond ((or (move-to-end-of-current-defun)
  69. (and (move-forward-defun) (move-to-end-of-current-defun))
  70. )
  71. (set-mark-from-point)
  72. (move-backward-defun)
  73. (when (not (current-line-is-first?))
  74. (move-to-previous-line)
  75. (if (not (current-line-blank?))
  76. (move-to-next-line))
  77. ))
  78. (t (Ding))
  79. ))
  80. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  81. % Defun Functions
  82. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  83. (de move-backward-defun ()
  84. % Move backward at least one character to the previous beginning of a
  85. % "defun". If no defun is found, return NIL and leave point unchanged.
  86. (when (move-backward-character)
  87. (or (beginning-of-defun)
  88. (progn (move-forward-character) NIL) % return NIL
  89. )))
  90. (de beginning-of-defun ()
  91. % Move backward, if necessary, to the beginning of a
  92. % "defun". If no defun is found, return NIL and leave point unchanged.
  93. (let ((old-pos (buffer-get-position)))
  94. (move-to-start-of-line)
  95. (while T
  96. (when (current-line-is-defun?) (exit T))
  97. (when (current-line-is-first?) (buffer-set-position old-pos) (exit NIL))
  98. (move-to-previous-line)
  99. )))
  100. (de move-forward-defun ()
  101. % Move forward at least one character to the next beginning of a
  102. % "defun". If no defun is found, return NIL and leave point unchanged.
  103. (let ((old-pos (buffer-get-position)))
  104. (while T
  105. (when (current-line-is-last?) (buffer-set-position old-pos) (exit NIL))
  106. (move-to-next-line)
  107. (when (current-line-is-defun?) (exit T))
  108. )))
  109. (de move-to-start-of-current-defun ()
  110. % If point lies within the text of a (possibly incomplete) defun, or on
  111. % the last line of a complete defun, then move to the beginning of the
  112. % defun. Otherwise, return NIL and leave point unchanged.
  113. (let ((old-pos (buffer-get-position))) % save original position
  114. (if (beginning-of-defun) % find previous defun start
  115. (let ((start-pos (buffer-get-position))) % save defun starting position
  116. % We succeed if the current defun has no end, or if the end is
  117. % beyond the old position in the buffer.
  118. (if (or (not (scan-past-defun))
  119. (<= (buffer-position-line old-pos) (current-line-pos))
  120. )
  121. (progn (buffer-set-position start-pos) T)
  122. (progn (buffer-set-position old-pos) NIL)
  123. )))))
  124. (de move-to-end-of-current-defun ()
  125. % If point lies within the text of a complete defun, or on the last line
  126. % of the defun, then move to the next line following the end of the defun.
  127. % Otherwise, return NIL and leave point unchanged.
  128. (let ((old-pos (buffer-get-position))) % save original position
  129. (if (and (beginning-of-defun) % find previous defun start
  130. (scan-past-defun) % find end of that defun
  131. (<= (buffer-position-line old-pos) (current-line-pos))
  132. )
  133. (progn (move-to-next-line) T)
  134. (progn (buffer-set-position old-pos) NIL)
  135. )))
  136. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  137. % Basic Defun Scanning Primitives
  138. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  139. (de current-line-is-defun? ()
  140. (if nmode-defun-predicate
  141. (apply nmode-defun-predicate ())
  142. ))
  143. (de scan-past-defun ()
  144. % This function should be called with point at the start of a defun.
  145. % It will scan past the end of the defun (not to the beginning of the
  146. % next line, however). If the end of the defun is not found, it returns
  147. % NIL and leaves point unchanged.
  148. (if nmode-defun-scanner
  149. (apply nmode-defun-scanner ())
  150. ))