Guest User

Untitled

a guest
Jan 23rd, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.79 KB | None | 0 0
  1. (global NSMaxRange (do (range) (+ (range first) (range second))))
  2. (global NSBackwardsSearch 4)
  3. (set $textwidth 72) ; set to zero to disable
  4. (if (defined $autoTextWrapEventId)
  5. (eventManager remove:$autoTextWrapEventId))
  6. (set $autoTextWrapEventId (eventManager on:"didModifyDocument" do:(do (doc range delta)
  7. ; Only wrap lines when inserting, and only in text documents, not in source code.
  8. (if (and (gt $textwidth 0) (gt delta 0) (((doc language) name) hasPrefix:"text."))
  9. ; The range parameter is wrapped in an NSValue object.
  10. (set range (range rangeValue))
  11. (set location (range first))
  12. (set textStorage (doc textStorage))
  13. (set lineRange (textStorage rangeOfLineAtLocation:location))
  14. ; Insert a newline if inserting at column > $textwidth.
  15. (set column (- location (lineRange first)))
  16. (if (gt column $textwidth)
  17. ; Find first space before the wrapping column.
  18. (set preRange `(,(lineRange first) ,$textwidth))
  19. (set wrapRange ((textStorage string) rangeOfCharacterFromSet:(NSCharacterSet whitespaceCharacterSet)
  20. options:NSBackwardsSearch
  21. range:preRange))
  22. (if (eq (wrapRange first) NSNotFound)
  23. ; If no space found before, look after.
  24. (set postRange `(,$textwidth ,(- (NSMaxRange lineRange) $textwidth)))
  25. (set wrapRange ((textStorage string) rangeOfCharacterFromSet:(NSCharacterSet whitespaceCharacterSet)
  26. options:0
  27. range:postRange)))
  28. ; Replace the whitespace with a newline, if found.
  29. (unless (eq (wrapRange first) NSNotFound)
  30. ((doc text) replaceRange:wrapRange withString:"\n")) )))))
Add Comment
Please, Sign In to add comment