<div dir="ltr">I believe the code in racket.rkt is correct. <br><br>An example situation where (send editor position-paragraph end-pos) would be different from the result of calc-last-para is when you have an editor that has "abc" followed by a newline, followed by "d" as the entire contents of the buffer, and the selection starts at position 0 and goes to position 4 (so the newline is selected, but not the "d").<div>
<br></div><div>In this case the commenting out should not comment out the second line (the one that contains the "d"), which is why calc-last-para returns 0 in that case instead of 1, which is what position-paragraph does. </div>
<div><br></div><div>See also the at-eol discussion in the docs:</div><div><br></div><div> <a href="http://docs.racket-lang.org/gui/editor-overview.html#%28part._editoreol%29">http://docs.racket-lang.org/gui/editor-overview.html#%28part._editoreol%29</a></div>
<div><br></div><div>Robby<br><br><br>On Wed, Nov 27, 2013 at 6:10 AM, Colin Gan <<a href="mailto:gan.colin.e@gmail.com">gan.colin.e@gmail.com</a>> wrote:<br>><br>> Thanks for your pointers Robby. After much digging through of source code, I seem to have located the source of the problem.<br>
><br>> 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.<br>
><br>> It's here:<br>><br>> (define (calc-last-para last-pos)<br>> (let ([last-para (position-paragraph last-pos #t)])<br>> (if (and (> last-pos 0)<br>> (> last-para 0))<br>
> (begin (split-snip last-pos)<br>> (let ([snip (find-snip last-pos 'before)])<br>> (if (member 'hard-newline (send snip get-flags))<br>> (- last-para 1)<br>
> last-para)))<br>> last-para)))<br>><br>> 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.<br>
><br>> 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.<br>
><br>> Have a good day,<br>><br>> C<br>><br>><br>><br>> On Mon, Nov 25, 2013 at 9:23 PM, Robby Findler <<a href="mailto:robby@eecs.northwestern.edu">robby@eecs.northwestern.edu</a>> wrote:<br>
>><br>>> Nice job!<br>>><br>>> 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".<br>
>><br>>> 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:<br>
>><br>>> (define is-line-commented?<br>>> (lambda (editor)<br>>> (define ans<br>>> (send editor find-string <br>>> ";" 'forward (get-start-of-line editor) (get-end-of-line editor)))<br>
>> (message-box "hi" (format "ans ~s" ans))<br>>> ans))<br>>><br>>> Robby<br>>><br>>><br>>><br>>><br>>> On Sat, Nov 23, 2013 at 9:42 AM, Colin Gan <<a href="mailto:gan.colin.e@gmail.com">gan.colin.e@gmail.com</a>> wrote:<br>
>>><br>>>> Hi all,<br>>>><br>>>> 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.<br>
>>><br>>>> 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:<br>
>>><br>>>><br>>>> (define get-start-of-line<br>>>> (lambda (editor)<br>>>> (send editor get-character (send editor line-start-position <br>>>> (send editor position-line (send editor get-start-position))))))<br>
>>><br>>>> (define is-line-commented?<br>>>> (lambda (editor)<br>>>> (cond<br>>>> ((eq? ";" <br>>>> (get-start-of-line editor) )#t)<br>
>>> (else #f))))<br>>>><br>>>> (keybinding "d:/" (λ (editor evt) (if (is-line-commented? editor) <br>>>> (send editor uncomment-selection (send editor get-start-position))<br>
>>> (send editor comment-out-selection (send editor get-start-position))<br>>>> )))<br>>>><br>>>><br>>>> 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:<br>
>>><br>>>> (define get-start-of-line<br>>>> (lambda (editor)<br>>>> (send editor line-start-position <br>>>> (send editor position-line (send editor get-start-position)))))<br>
>>><br>>>> (define get-end-of-line<br>>>> (lambda (editor)<br>>>> (send editor line-end-position <br>>>> (send editor position-line (send editor get-end-position)))))<br>
>>><br>>>> (define is-line-commented?<br>>>> (lambda (editor)<br>>>> (send editor find-string <br>>>> ";" 'forward (get-start-of-line editor) (get-end-of-line editor))))<br>
>>><br>>>><br>>>> (keybinding "d:/" (λ (editor evt) (if (is-line-commented? editor) <br>>>> (send editor uncomment-selection (send editor get-start-position))<br>
>>> (send editor comment-out-selection (send editor get-start-position))<br>>>> )))<br>>>><br>>>><br>>>> 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. :)<br>
>>><br>>>><br>>>> Colin<br>>>><br>>>> ____________________<br>>>> Racket Users list:<br>>>> <a href="http://lists.racket-lang.org/users">http://lists.racket-lang.org/users</a><br>
>>><br>>><br>><br></div></div>