[racket] Writing a single key binding in DrRacket for commenting/uncommenting

From: Colin Gan (gan.colin.e at gmail.com)
Date: Sat Nov 23 10:42:00 EST 2013

Hi all,

I have recently picked up Scheme and would like to customize the key
bindings in DrScheme. Specifically, I find the default binding for
commenting and uncommenting out to be cumbersome; on OSX it is esc;c:= and
esc;c:semicolon.

What I would like to achieve is use one key to comment and uncomment, so it
comments an uncommented line, and vice versa. Unfortunately I'm finding the
documentation on key-binding a little sparse. Here's what I've been working
at:


(define get-start-of-line
  (lambda (editor)
    (send editor get-character (send editor line-start-position
          (send editor position-line (send editor get-start-position))))))

(define is-line-commented?
  (lambda (editor)
    (cond
      ((eq? ";"
            (get-start-of-line editor) )#t)
      (else #f))))

(keybinding "d:/" (λ (editor evt) (if (is-line-commented? editor)
                                      (send editor uncomment-selection
(send editor get-start-position))
                                      (send editor comment-out-selection
(send editor get-start-position))
                                      )))


I know it's not very well written; I've only just finished the recursion
section of The Little Schemer. I was going to leave it here, then I had
another look at the documentation and had another go:

(define get-start-of-line
  (lambda (editor)
    (send editor line-start-position
          (send editor position-line (send editor get-start-position)))))

(define get-end-of-line
  (lambda (editor)
    (send editor line-end-position
          (send editor position-line (send editor get-end-position)))))

(define is-line-commented?
  (lambda (editor)
     (send editor find-string
           ";" 'forward (get-start-of-line editor) (get-end-of-line
editor))))


(keybinding "d:/" (λ (editor evt) (if (is-line-commented? editor)
                                      (send editor uncomment-selection
(send editor get-start-position))
                                      (send editor comment-out-selection
(send editor get-start-position))
                                      )))


This one actually works, but of course it is clumsy and dumb; it doesn't
work when the caret is at the start of each line, and doesn't know if the
semi-colon is escaped. I'm thrilled anyway. :)


Colin
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.racket-lang.org/users/archive/attachments/20131123/1fd548dc/attachment.html>

Posted on the users mailing list.