PasteRack.org
Paste # 476
2023-06-15 02:33:15

Fork as a new paste.

Paste viewed 746 times.


Embed:

delete-trailing-whitespace quickscript

  1. #lang racket/base
  2.  
  3. (require racket/class quickscript)
  4.  
  5. ;; Returns a replacement string for the selected string `selection`
  6. ;; ("" if no text is selected), or `#f` to leave the selection as is.
  7. (define-script delete-trailing-whitespace
  8.   #:label "Delete trailing whitespace and save"
  9.   #:help-string "Delete trailing whitespace from all lines in the file, then save it"
  10.   #:menu-path ("&Utils")
  11.   #:output-to #f
  12.   #:shortcut #\s
  13.   #:shortcut-prefix (ctl alt)
  14.   (λ (selection #:definitions d)
  15.     (for ([line (in-range 0 (send d last-line))])
  16.       (let* ([sol (send d line-start-position line)]
  17.              [eol (send d line-end-position line)]
  18.              [last-non-whitespace (for/first ([pos (in-inclusive-range eol sol -1)]
  19.                                               #:unless (char-whitespace? (send d get-character pos)))
  20.                                     pos)])
  21.         (cond
  22.           ((eq? last-non-whitespace #f) ; Entire line is whitespace
  23.            (send d delete sol eol))
  24.           ((not (= last-non-whitespace eol))
  25.            (send d delete (+ last-non-whitespace 1) eol)))))
  26.     (send d save-file)
  27.     #f))

=>

syntax-local-lift-module-end-declaration: not currently

transforming an expression within a module declaration

  form to lift: #<syntax:/home/pasterack/racket88/share/pkgs

/quickscript/script.rkt:18:12 (generate-submodule)>