Last active
October 21, 2021 01:26
-
-
Save Calvin-LL/c101fd2d1b52bfcc9b5e186868f15da2 to your computer and use it in GitHub Desktop.
DrRacket Comment Keybinding
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#lang s-exp framework/keybinding-lang | |
;; definitions: | |
;; commented line: a line where the first non-blank character is ";" | |
;; non-commented line: any line that isn't a commented line | |
;; | |
;; selected line: all partially or fully selected lines | |
;; blank line an empty line or a line with only spaces | |
;; | |
;; behavior: | |
;; if there is no selection | |
;; if the cursor is on a blank line | |
;; ;; this is a special case because comment/uncomment doesn't work on blank lines | |
;; - add ; | |
;; if the cursor is on a line where the first non-blank character is ";" | |
;; - uncomment | |
;; if the cursor is on a line where the first non-blank character is not ";" | |
;; - comment | |
;; if there is selection | |
;; if all selected lines are blank | |
;; ;; this is a special case because comment/uncomment doesn't work on blank lines | |
;; - add ; to each line | |
;; if all selected non-blank lines have the first non-blank character as ";" | |
;; - uncomment | |
;; if one selected non-blank lines have the first non-blank character not as ";" | |
;; - comment | |
(define (comment-or-uncomment editor event) | |
(define start-pos (send editor get-start-position)) | |
(define end-pos (send editor get-end-position)) | |
(define start-line-number (send editor position-line start-pos)) | |
(define end-line-number (send editor position-line end-pos)) | |
(cond | |
[(line-every editor is-line-blank start-line-number end-line-number) | |
(begin | |
(send editor begin-edit-sequence) | |
(line-for-each editor insert-semi-colon-at-line start-line-number end-line-number) | |
(send editor end-edit-sequence))] | |
[(line-every editor (disjoin is-line-blank is-line-commented) start-line-number end-line-number) | |
(send editor uncomment-selection)] | |
[(line-some editor | |
(disjoin is-line-blank (negate is-line-commented)) | |
start-line-number | |
end-line-number) | |
(send editor comment-out-selection)])) | |
;; proc of shape (proc editor line-number) | |
(define (line-for-each editor proc start-line-number end-line-number) | |
(when (<= start-line-number end-line-number) | |
(begin | |
(proc editor start-line-number) | |
(line-for-each editor proc (+ start-line-number 1) end-line-number)))) | |
;; proc of shape (proc editor line-number) -> boolean | |
(define (line-every editor proc start-line-number end-line-number) | |
(if (<= start-line-number end-line-number) | |
(and (proc editor start-line-number) | |
(line-every editor proc (+ start-line-number 1) end-line-number)) | |
#t)) | |
;; proc of shape (proc editor line-number) -> boolean | |
(define (line-some editor proc start-line-number end-line-number) | |
(if (<= start-line-number end-line-number) | |
(or (proc editor start-line-number) | |
(line-some editor proc (+ start-line-number 1) end-line-number)) | |
#f)) | |
(define (insert-semi-colon-at-line editor line-number) | |
(define line-start-pos (send editor line-start-position line-number)) | |
(send editor insert ";" line-start-pos)) | |
(define (is-line-blank editor line-number) | |
(define line-text (get-text-at-line editor line-number)) | |
(if (regexp-match #rx"^ *$" line-text) #t #f)) | |
(define (is-line-commented editor line-number) | |
(define line-text (get-text-at-line editor line-number)) | |
(if (regexp-match #rx"^ *;" line-text) #t #f)) | |
(define (get-text-at-line editor line-number) | |
(define line-start-pos (send editor line-start-position line-number)) | |
(define line-end-pos (send editor line-end-position line-number)) | |
(send editor get-text line-start-pos line-end-pos)) | |
;; d is for the command key on Macs | |
(keybinding "d:/" comment-or-uncomment) | |
;; c is for the control key, use this keybinding instead if you're not using a Mac | |
; (keybinding "c:/" comment-or-uncomment) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This keybinding comments/uncomments the current line or selected lines when you press
control+/
orcommand+/
To add this binding