buffer.sl 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. %
  2. % Buffer.SL - Individual Buffer Manipulation Functions
  3. %
  4. % Author: Alan Snyder
  5. % Hewlett-Packard/CRC
  6. % Date: 27 July 1982
  7. %
  8. % This file contains functions that manipulate individual buffers.
  9. % It is intended that someday EMODE will be reorganized
  10. % so that all such functions will eventually be in this file.
  11. %
  12. % This file requires COMMON.
  13. %
  14. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  15. (fluid '(CurrentLine CurrentBufferSize CurrentLineIndex point))
  16. (de char-blank? (ch)
  17. (or (= ch (char space)) (= ch (char tab))))
  18. (de current-line-length () (length CurrentLine))
  19. (de current-line-empty () (= (length CurrentLine) 0))
  20. (de current-line-blank? ()
  21. (for (in ch CurrentLine)
  22. (always (char-blank? ch))
  23. ))
  24. (de at-buffer-end? ()
  25. (and (current-line-is-last?) (= point (current-line-length))))
  26. (de at-buffer-start? ()
  27. (and (= CurrentLineIndex 0) (= point 0)))
  28. (de current-line-is-last? ()
  29. (>= CurrentLineIndex (- CurrentBufferSize 1)))
  30. (de current-line-is-first? ()
  31. (= CurrentLineIndex 0))
  32. (de current-line-fetch (n) (car (pnth CurrentLine (+ n 1))))
  33. (de current-line-store (n c)
  34. (setf CurrentLine (InsertListEntry (DeleteListEntry CurrentLine n) n c)))
  35. (de current-buffer-size ()
  36. % Return the number of lines in the current buffer. Note that if the
  37. % buffer does not end with an incomplete line, then its last line will
  38. % be empty. (See CURRENT-BUFFER-VISIBLE-SIZE, which corrects for this
  39. % anomaly.)
  40. CurrentBufferSize)
  41. (de current-buffer-visible-size ()
  42. % Return the visible number of lines in the current buffer. In other words,
  43. % don't count the last line if it is empty, since that is just an artifact of
  44. % the buffer representation.
  45. (let* ((buffer-size CurrentBufferSize)
  46. (last-line-index (- buffer-size 1))
  47. )
  48. (if (= CurrentLineIndex last-line-index) % CurrentLine hack!
  49. (if CurrentLine buffer-size (- buffer-size 1))
  50. (if (>= (size (GetBufferText last-line-index)) 0)
  51. buffer-size (- buffer-size 1))
  52. )))
  53. (de current-buffer-goto (line-number char-number)
  54. (SelectLine line-number)
  55. (setf point char-number)
  56. )
  57. (de move-to-next-line ()
  58. (let ((next-index (+ CurrentLineIndex 1)))
  59. (cond ((< next-index CurrentBufferSize)
  60. (SelectLine next-index) (setf point 0))
  61. (t (setf point (length CurrentLine)) (PutLine))
  62. )))
  63. (de move-to-previous-line ()
  64. (let ((next-index (- CurrentLineIndex 1)))
  65. (cond ((>= next-index 0)
  66. (SelectLine next-index) (setf point 0))
  67. (t (setf point 0) (PutLine))
  68. )))