Skip to content

Commit fba98e3

Browse files
committed
generalize make-visible, locate-file, and handler:edit-file
and then use that generalization inside srcloc-snip related to racket/htdp#231
1 parent 7b077bc commit fba98e3

File tree

7 files changed

+72
-47
lines changed

7 files changed

+72
-47
lines changed

gui-doc/scribblings/framework/frame.scrbl

+13-3
Original file line numberDiff line numberDiff line change
@@ -93,11 +93,21 @@
9393
If @racket[temp] is a box, it is filled with @racket[#t] or @racket[#f],
9494
depending if the filename is a temporary filename.
9595
}
96-
@defmethod*[(((make-visible (filename string?)) void?))]{
96+
@defmethod[(make-visible [filename (or/c path-string? symbol?)]
97+
[#:start-pos start-pos #f (or/c #f exact-nonnegative-integer?)]
98+
[#:end-pos end-pos start-pos (or/c #f exact-nonnegative-integer?)])
99+
void?]{
97100
Makes the file named by @racket[filename] visible (intended for
98-
use with tabbed editing).
101+
use with tabbed editing), using @method[text:basic<%> port-name-matches?]
102+
to find the editor if @racket[filename] is a @racket[symbol?].
99103

100-
}
104+
If both @racket[start-pos] and @racket[end-pos]
105+
are numbers, sets the insertion point to the range from
106+
@racket[start-pos] and @racket[end-pos].
107+
108+
@history[#:changed "1.75" @list{generalized the @racket[filename] argument to allow
109+
symbols and added the @racket[start-pos] and @racket[end-pos] arguments.}]
110+
}
101111
}
102112
@defmixin[frame:basic-mixin (frame%) (frame:basic<%>)]{
103113
This mixin provides the basic functionality that the framework expects. It

gui-doc/scribblings/framework/group.scrbl

+8-1
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,15 @@
8484
Calls the @method[top-level-window<%> can-close?] method of each frame in
8585
the group.
8686
}
87-
@defmethod*[(((locate-file [name path?]) (or/c false/c (is-a?/c frame:basic<%>))))]{
87+
@defmethod*[(((locate-file [name (or/c path? symbol?)]) (or/c false/c (is-a?/c frame:basic<%>))))]{
8888
Returns the frame that is editing or viewing the file @racket[name].
89+
90+
If @racket[name] is a @racket[symbol?], uses the
91+
@method[text:basic<%> port-name-matches?] method to
92+
find a window that's editing this file.
93+
94+
@history[#:changed "1.75" @list{generalized the @racket[filename] argument to allow
95+
symbols and added the @racket[start-pos] and @racket[end-pos] arguments.}]
8996
}
9097
}
9198

gui-lib/framework/main.rkt

+18-8
Original file line numberDiff line numberDiff line change
@@ -1004,31 +1004,41 @@ was never saved, then the first element of the list is @racket[#f].
10041004

10051005
(proc-doc/names
10061006
handler:edit-file
1007-
(->* ((or/c path? false/c))
1008-
((-> (is-a?/c frame:editor<%>)))
1007+
(->* ((or/c path? symbol? #f))
1008+
((-> (is-a?/c frame:editor<%>))
1009+
#:start-pos (or/c exact-nonnegative-integer? #f)
1010+
#:end-pos (or/c exact-nonnegative-integer? #f))
10091011
(or/c false/c (is-a?/c frame:editor<%>)))
10101012
((filename)
10111013
((make-default
1012-
(λ () ((handler:current-create-new-window) filename)))))
1014+
(λ () ((handler:current-create-new-window) (and (path? filename) filename))))
1015+
(start-pos #f)
1016+
(end-pos start-pos)))
10131017
@{This function invokes the appropriate format handler to open the file (see
10141018
@racket[handler:insert-format-handler]).
10151019

10161020
@itemize[
1017-
@item{If @racket[filename] is a string, this function checks the result
1018-
of @racket[group:get-the-frame-group] to see if the
1021+
@item{If @racket[filename] is a string or a symbol, this function checks the result
1022+
of @racket[group:get-the-frame-group]'s @method[group:% locate-file] method to see if the
10191023
@racket[filename] is already open by a frame in the group.
10201024
@itemize[
10211025
@item{If so, it returns the frame.}
1022-
@item{If not, this function calls
1026+
@item{If not and if @racket[filename] is a string, this function calls
10231027
@racket[handler:find-format-handler] with
10241028
@racket[filename].
10251029
@itemize[
10261030
@item{If a handler is found, it is applied to
10271031
@racket[filename] and its result is the final
10281032
result.}
1029-
@item{If not, @racket[make-default] is used.}]}]}
1033+
@item{If not, @racket[make-default] is used.}]}
1034+
@item{If the file is not already open by a frame in the group
1035+
and if @racket[filename] is a symbol,
1036+
@racket[make-default] is used.}]}
10301037
@item{If @racket[filename] is @racket[#f], @racket[make-default] is
1031-
used.}]})
1038+
used.}]
1039+
1040+
@history[#:changed "1.75" @list{generalized the @racket[filename] argument to allow
1041+
symbols and added the @racket[start-pos] and @racket[end-pos] arguments.}]})
10321042

10331043
(parameter-doc
10341044
handler:current-create-new-window

gui-lib/framework/private/frame.rkt

+14-4
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@
179179
(exit)
180180
(exit:set-exiting #f))))
181181

182-
(define/public (make-visible filename) (void))
182+
(define/public (make-visible filename #:start-pos [start-pos #f] #:end-pos [end-pos start-pos]) (void))
183183
(define/public get-filename
184184
(case-lambda
185185
[() (get-filename #f)]
@@ -1449,9 +1449,19 @@
14491449
(with-handlers ((exn:fail? (λ (x) #f)))
14501450
(equal? (normal-case-path (normalize-path x))
14511451
(normal-case-path (normalize-path y)))))])
1452-
(let ([this-fn (get-filename)])
1453-
(and this-fn
1454-
(path-equal? filename (get-filename))))))
1452+
(define ed (get-editor))
1453+
(cond
1454+
[(let ([fn (get-filename)])
1455+
(and fn (path-equal? filename (get-filename))))
1456+
;; it would be nice to remove this case, as
1457+
;; port-name-matches? is doing something similar.
1458+
;; unfortunately, it isn't quite the same; here
1459+
;; normalize-path is used and there it isn't.
1460+
#t]
1461+
[(is-a? ed text:basic<%>)
1462+
(send ed port-name-matches? filename)]
1463+
[else
1464+
#f])))
14551465

14561466
(define/override (get-all-open-files)
14571467
(with-handlers ((exn:fail? (λ (x) '())))

gui-lib/framework/private/handler.rkt

+9-4
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,10 @@
7575
frame))))
7676

7777
(define (edit-file filename [make-default
78-
(λ () ((current-create-new-window) filename))])
78+
(λ () ((current-create-new-window)
79+
(and (path? filename) filename)))]
80+
#:start-pos [start-pos #f]
81+
#:end-pos [end-pos #f])
7982
(with-handlers ([(λ (x) #f) ;exn:fail?
8083
(λ (exn)
8184
(message-box
@@ -97,14 +100,16 @@
97100
filename)])
98101
(cond
99102
[already-open
100-
(send already-open make-visible filename)
103+
(send already-open make-visible filename
104+
#:start-pos start-pos #:end-pos end-pos)
101105
(send already-open show #t)
102106
already-open]
103107
[else
104108
(let ([handler (and (path? filename)
105109
(find-format-handler filename))])
106-
(add-to-recent filename)
107-
(if handler (handler filename) (make-default)))]))
110+
(when (path? filename)
111+
(add-to-recent filename))
112+
(if (and (path? filename) handler) (handler filename) (make-default)))]))
108113
(make-default))))))
109114

110115
;; type recent-list-item = (list/p string? number? number?)

gui-lib/framework/private/srcloc-snip.rkt

+9-26
Original file line numberDiff line numberDiff line change
@@ -13,35 +13,18 @@
1313
(import [prefix frame: framework:frame^]
1414
[prefix group: framework:group^]
1515
[prefix text: framework:text^]
16-
[prefix editor: framework:editor^])
16+
[prefix editor: framework:editor^]
17+
[prefix handler: framework:handler^])
1718
(export (rename framework:srcloc-snip^ [-snip% snip%]))
1819

1920
(define (select-srcloc srcloc)
20-
(let frame-loop ([frames (send (group:get-the-frame-group) get-frames)])
21-
(unless (null? frames)
22-
(let ([frame (car frames)])
23-
(cond
24-
[(and (is-a? frame frame:editor<%>)
25-
(send frame find-editor
26-
(lambda (editor)
27-
(send editor port-name-matches? (srcloc-source srcloc)))))
28-
=> (lambda (editor)
29-
(show-editor frame editor)
30-
(send (send frame get-interactions-text)
31-
highlight-error
32-
editor (srcloc-position srcloc) (+ (srcloc-position srcloc) (srcloc-span srcloc))))]
33-
[else
34-
(frame-loop (cdr frames))])))))
35-
36-
(define (show-editor frame editor)
37-
(let* ([current-tab (send editor get-tab)]
38-
[frame (send current-tab get-frame)])
39-
(let loop ([tabs (send frame get-tabs)] [i 0])
40-
(unless (null? tabs)
41-
(if (eq? (car tabs) current-tab)
42-
(send frame change-to-nth-tab i)
43-
(loop (cdr tabs) (+ i 1)))))
44-
(send frame show #t)))
21+
(define pos (srcloc-position srcloc))
22+
(define span (srcloc-span srcloc))
23+
(handler:edit-file (srcloc-source srcloc)
24+
#:start-pos (and pos (- pos 1))
25+
#:end-pos (cond
26+
[(and pos span) (+ pos span -1)]
27+
[else (and pos (- pos 1))])))
4528

4629
; honest attempt
4730
(define (source->datum source)

gui-lib/info.rkt

+1-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@
3434

3535
(define pkg-authors '(mflatt robby))
3636

37-
(define version "1.74")
37+
(define version "1.75")
3838

3939
(define license
4040
'(Apache-2.0 OR MIT))

0 commit comments

Comments
 (0)