<div dir="ltr">Thanks for your pointers Robby. After much digging through of source code, I seem to have located the source of the problem.<div><br></div><div>The definition of comment-out-selection in collects/framework/private/racket.rt has a loop which goes through and inserts a semi-colon at the start of every paragraph in the selection. To determine where it should end, it calculates the number of the last paragraph by calling a function named calc-last-para.</div>
<div><br></div><div>It's here:</div><div><br></div><div><div>    (define (calc-last-para last-pos)</div><div>      (let ([last-para (position-paragraph last-pos #t)])</div><div>        (if (and (> last-pos 0)</div>
<div>                 (> last-para 0))</div><div>            (begin (split-snip last-pos)</div><div>                   (let ([snip (find-snip last-pos 'before)])</div><div>                     (if (member 'hard-newline (send snip get-flags))</div>
<div>                         (- last-para 1)</div><div>                         last-para)))</div><div>            last-para)))</div></div><div><br></div><div>The 'begin body' is what I have difficulty understanding. I'm guessing it finds the very last snip before the last position, determine if it is a newline, and if it is then it decrements the position of the last paragraph by 1. If this is done when the caret is positioned at the start of the line, the last paragraph position is off by 1. This means that the caret could be on paragraph 10 and calc-last-para would return 9, resulting in the line not being commented.</div>
<div><br></div><div>To remedy this, I just use (send editor position-paragraph end-pos) instead of calc-last-para. I also removed a when expression in comment-out-selection which I found was causing the semicolon to become selected when commenting out on a new line. You can look at what I have done in the file I have attached: comment-toggle.rkt.</div>
<div><br></div><div>Have a good day,</div><div><br></div><div>C</div><div><br></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Mon, Nov 25, 2013 at 9:23 PM, Robby Findler <span dir="ltr"><<a href="mailto:robby@eecs.northwestern.edu" target="_blank">robby@eecs.northwestern.edu</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Nice job!<div><br></div><div>One point where the docs are confusing: "line"s are soft-wrapped, not hard-wrapped lines. For code you want the methods with the word "paragraph" in the name instead of "line".</div>

<div><br></div><div>I'm not seeing why your code doesn't work at the beginning of the line, but if you start up drracket from the command-line and then put printfs into your functions, you should be able to see intermediate values and you can check what's going on. Or, if you're on windows, you might find it easier to call "message-box", ie:</div>

<div><br></div><div><div><div><font face="monospace">(define is-line-commented?</font></div><div><font face="monospace">  (lambda (editor)</font></div><div><font face="monospace">    (define ans</font></div><div class="im">
<div><font face="monospace">      (send editor find-string </font></div>
<div><font face="monospace">            ";" 'forward (get-start-of-line editor) (get-end-of-line editor)))</font></div></div><div><font face="monospace">    (message-box "hi" (format "ans ~s" ans))</font></div>

<div><font face="monospace">    ans))</font></div><div style="font-family:monospace"><br></div></div></div><div style="font-family:monospace">Robby</div><div><br></div><div><br></div></div><div class="gmail_extra"><br><br>

<div class="gmail_quote"><div><div class="h5">On Sat, Nov 23, 2013 at 9:42 AM, Colin Gan <span dir="ltr"><<a href="mailto:gan.colin.e@gmail.com" target="_blank">gan.colin.e@gmail.com</a>></span> wrote:<br></div></div>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div class="h5">
<div dir="ltr">Hi all,<div><br></div><div>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.</div>


<div><br></div><div>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:</div>


<div><br></div><div><div><br></div><div><div>(define get-start-of-line</div><div>  (lambda (editor)</div><div>    (send editor get-character (send editor line-start-position </div><div>          (send editor position-line (send editor get-start-position))))))</div>


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


<div><br></div><div>(keybinding "d:/" (ë (editor evt) (if (is-line-commented? editor) </div><div>                                      (send editor uncomment-selection (send editor get-start-position))</div><div>


                                      (send editor comment-out-selection (send editor get-start-position))</div><div>                                      )))</div></div></div><div><br></div><div><br></div><div>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:</div>


<div><br></div><div><div>(define get-start-of-line</div><div>  (lambda (editor)</div><div>    (send editor line-start-position </div><div>          (send editor position-line (send editor get-start-position)))))</div><div>


<br></div><div>(define get-end-of-line</div><div>  (lambda (editor)</div><div>    (send editor line-end-position </div><div>          (send editor position-line (send editor get-end-position)))))</div><div><br></div><div>


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

<div>
<br></div><div>(keybinding "d:/" (ë (editor evt) (if (is-line-commented? editor) </div><div>                                      (send editor uncomment-selection (send editor get-start-position))</div><div>                                      (send editor comment-out-selection (send editor get-start-position))</div>


<div>                                      )))</div></div><div><br></div><div><br></div><div>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. :)</div>

<span><font color="#888888">
<div><br></div><div><br></div><div>Colin</div></font></span></div>
<br></div></div>____________________<br>
  Racket Users list:<br>
  <a href="http://lists.racket-lang.org/users" target="_blank">http://lists.racket-lang.org/users</a><br>
<br></blockquote></div><br></div>
</blockquote></div><br></div></div>