Skip to content

Instantly share code, notes, and snippets.

@Metaxal
Last active October 13, 2023 13:22
Show Gist options
  • Save Metaxal/0b1dd0176f9d4700e68a14eba1ceff2a to your computer and use it in GitHub Desktop.
Save Metaxal/0b1dd0176f9d4700e68a14eba1ceff2a to your computer and use it in GitHub Desktop.
quickscript: Like pressing Enter, but keeps the comments at the end of the line
#lang racket/base
(require quickscript
racket/list
racket/class
racket/gui/event)
(script-help-string "Like pressing Enter, but keeps the comments at the end of the line")
;;; Author: Laurent Orseau <[email protected]>
;;; License: [Apache License, Version 2.0](http://www.apache.org/licenses/LICENSE-2.0) or
;;; [MIT license](http://opensource.org/licenses/MIT) at your option.
;; Like pressing Enter, except that the comments at the end of the line
;; are not moved to the next line. Example, where | is the cursor:
;; (a b c |d) ; some comments
;; becomes
;; (a b c ; some comments
;; d)
;; The whitespaces between c and | are deleted, while the whitespaces before the comments are moved
;; with the comments.
(define-script new-line-keep-comments
#:label "new-line-keep-comments"
#:shortcut #\return
#:shortcut-prefix (ctl)
(λ (selection #:editor ed)
(send ed begin-edit-sequence)
(define kmp (send ed get-keymap))
(send kmp call-function "do-return" ed (new event%) #t)
(define start (send ed get-start-position))
(define line (send ed position-line start))
(define end (send ed line-end-position line))
(define txt (send ed get-text start end))
(define m (regexp-match-positions #px"\\s*;.*$" txt))
(when m
(define pos (send ed get-start-position))
(define start-comment (car (first m)))
(define end-comment (cdr (first m)))
(define comment (substring txt start-comment end-comment))
(send ed delete (+ start start-comment) end) ; remove the comment
(send ed set-position (send ed line-end-position (- line 1)))
(send ed insert comment)
(send ed set-position (+ pos (string-length comment))))
(send ed end-edit-sequence)
#f))
(module url2script-info racket/base
(provide filename url)
(define filename "new-line-keep-comments.rkt")
(define url "https://gist.github.com/Metaxal/0b1dd0176f9d4700e68a14eba1ceff2a"))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment