Skip to content

Instantly share code, notes, and snippets.

@Calvin-LL
Last active October 21, 2021 01:26
Show Gist options
  • Save Calvin-LL/c101fd2d1b52bfcc9b5e186868f15da2 to your computer and use it in GitHub Desktop.
Save Calvin-LL/c101fd2d1b52bfcc9b5e186868f15da2 to your computer and use it in GitHub Desktop.
DrRacket Comment Keybinding
#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)
@Calvin-LL
Copy link
Author

Calvin-LL commented Sep 15, 2021

This keybinding comments/uncomments the current line or selected lines when you press control+/ or command+/

To add this binding

  1. Download this file
  2. If you're not using a Mac, comment out line 24 and uncomment line 26
  3. Move the file to a safe place, you'll need to keep it on your computer
  4. Go to DrRacket's menu bar -> Edit -> Keybindings -> Add User-defined Keybindings, and select this file

ezgif com-gif-maker (1)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment