autofill.sl 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  2. %
  3. % AUTOFILL.SL - NMODE Auto-Fill Mode
  4. %
  5. % Author: Jeff Soreff
  6. % Hewlett-Packard/CRC
  7. % Date: 3 November 1982
  8. % Revised: 18 January 1983
  9. %
  10. % 16-Nov-82 Jeff Soreff
  11. % Fixed bugs (handling very long lines, breaking at punctuation)
  12. % and improved efficiency.
  13. % 29-Nov-82 Jeff Soreff
  14. % Fixed bug with too-long word.
  15. % 18-Jan-83 Jeff Soreff
  16. % Made autofill preserve textual context of buffer position.
  17. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  18. (CompileTime (load extended-char fast-int fast-strings fast-vectors))
  19. % Externals used here:
  20. (fluid '(nmode-command-argument nmode-command-argument-given))
  21. % Globals defined here:
  22. (fluid '(fill-prefix fill-column auto-fill-mode))
  23. (setf fill-prefix nil)
  24. (setf fill-column 70)
  25. (setf auto-fill-mode
  26. (nmode-define-mode "Fill" '((auto-fill-setup))))
  27. %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
  28. (de auto-fill-mode-command ()
  29. (toggle-minor-mode auto-fill-mode))
  30. (de auto-fill-setup ()
  31. (if (eq (dispatch-table-lookup (x-char SPACE)) 'insert-self-command)
  32. (nmode-define-command (x-char SPACE) 'auto-fill-space)
  33. ))
  34. (de set-fill-column-command ()
  35. (if nmode-command-argument-given
  36. (setq fill-column nmode-command-argument)
  37. (setq fill-column (current-display-column)))
  38. (write-message
  39. (bldmsg "%w%p" "Fill Column = " fill-column)))
  40. (de set-fill-prefix-command ()
  41. (let ((temp (buffer-get-position)))
  42. (cond ((at-line-start?)
  43. (setq fill-prefix nil)
  44. (write-message "Fill Prefix now empty"))
  45. (t (move-to-start-of-line)
  46. (setq fill-prefix
  47. (extract-text
  48. nil (buffer-get-position)
  49. temp))
  50. (buffer-set-position temp)
  51. (write-message
  52. (bldmsg "%w%p" "Fill Prefix now "
  53. (vector-fetch fill-prefix 0)))))))
  54. (de blank-char (char) (or (= char #\tab) (= char #\blank)))
  55. (de skip-forward-blanks-in-line ()
  56. (while (and (not (at-line-end?))
  57. (blank-char (next-character)))
  58. (move-forward)))
  59. (de skip-backward-blanks-in-line ()
  60. (while (and (not (at-line-start?))
  61. (blank-char (previous-character)))
  62. (move-backward)))
  63. (de skip-forward-nonblanks-in-line ()
  64. (while (and (not (at-line-end?))
  65. (not (blank-char (next-character))))
  66. (move-forward)))
  67. (de auto-fill-space ()
  68. (for (from i 1 nmode-command-argument 1)
  69. (do (insert-character #\blank)))
  70. (when (> (current-display-column) fill-column)
  71. (let ((word-too-long nil)
  72. (current-place (buffer-get-position)))
  73. (set-display-column fill-column)
  74. (while (or (not (at-line-end?)) word-too-long)
  75. (let ((start nil)(end nil))
  76. (while (not (or (at-line-start?)
  77. (and (blank-char % start natural break
  78. (next-character))
  79. (not (blank-char
  80. (previous-character))))))
  81. (move-backward))
  82. (unless (setf word-too-long
  83. (and (at-line-start?)
  84. (not (blank-char (next-character)))))
  85. (setf start (buffer-get-position))
  86. (skip-forward-blanks-in-line)
  87. (setf end (buffer-get-position))
  88. (when (buffer-position-lessp start current-place) % Correct for
  89. (if (buffer-position-lessp current-place end) % the extraction.
  90. (setf current-place start) % Within extracted interval
  91. (setf current-place % After extracted interval
  92. (buffer-position-create
  93. (buffer-position-line current-place)
  94. (- (buffer-position-column current-place)
  95. (- (buffer-position-column end)
  96. (buffer-position-column start)))))))
  97. (extract-text t start end)
  98. (when (buffer-position-lessp (buffer-get-position) current-place)
  99. (setf current-place % Correct for new line break being added
  100. (buffer-position-create
  101. (+ (buffer-position-line current-place) 1)
  102. (- (buffer-position-column current-place)
  103. (current-char-pos)))))
  104. (insert-eol)
  105. (when fill-prefix
  106. (insert-text fill-prefix)
  107. (setf current-place % Correct for prefix length
  108. (buffer-position-create
  109. (buffer-position-line current-place)
  110. (+ (buffer-position-column current-place)
  111. (string-length (vector-fetch fill-prefix 0))))))))
  112. (if word-too-long
  113. (move-to-end-of-line)
  114. (set-display-column fill-column)))
  115. (buffer-set-position current-place))))