diff --git a/collects/framework/private/frame.rkt b/collects/framework/private/frame.rkt index 758a9b7d47..b731c40367 100644 --- a/collects/framework/private/frame.rkt +++ b/collects/framework/private/frame.rkt @@ -2169,6 +2169,15 @@ (define/override (edit-menu:find-callback menu evt) (unhide-search-and-toggle-focus) #t) (define/override (edit-menu:create-find?) #t) + + (define/override (edit-menu:find-from-selection-callback menu evt) + (unhide-search-and-toggle-focus #:new-search-string-from-selection? #t) + #t) + (define/override (edit-menu:find-from-selection-on-demand item) + (define t (get-text-to-search)) + (send item enable (and t (not (= (send t get-start-position) + (send t get-end-position)))))) + (define/override (edit-menu:create-find-from-selection?) #t) (define/override (edit-menu:find-next-callback menu evt) (search 'forward) #t) (define/override (edit-menu:create-find-next?) #t) @@ -2280,26 +2289,47 @@ (when canvas (send canvas focus)))))) - (define/public (unhide-search focus?) + (define/public (unhide-search focus? #:new-search-string-from-selection? [new-search-string-from-selection? #f]) (when hidden? (set! hidden? #f) (build-search-gui-in-frame) (unless (memq search/replace-panel (send super-root get-children)) (send super-root add-child search/replace-panel)) (search-parameters-changed) + (send find-edit begin-edit-sequence) + (when new-search-string-from-selection? + (update-search-string-from-selection)) (when focus? (send find-edit set-position 0 (send find-edit last-position)) - (send (send find-edit get-canvas) focus)))) + (send (send find-edit get-canvas) focus)) + (send find-edit end-edit-sequence))) - (define/public (unhide-search-and-toggle-focus) + (define/public (unhide-search-and-toggle-focus #:new-search-string-from-selection? [new-search-string-from-selection? #f]) (if hidden? - (unhide-search #t) + (unhide-search #t #:new-search-string-from-selection? new-search-string-from-selection?) (let ([canvas (and text-to-search (send text-to-search get-canvas))]) (cond [(or (not text-to-search) (and canvas (send canvas has-focus?))) + (send find-edit begin-edit-sequence) + (when new-search-string-from-selection? + (update-search-string-from-selection)) (send find-edit set-position 0 (send find-edit last-position)) - (send find-canvas focus)] - [canvas (send canvas focus)])))) + (send find-canvas focus) + (send find-edit end-edit-sequence)] + [canvas + (send canvas focus)])))) + + (define/private (update-search-string-from-selection) + (when (and text-to-search + (not (= (send text-to-search get-start-position) + (send text-to-search get-end-position)))) + (send find-edit delete 0 (send find-edit last-position)) + (send text-to-search move/copy-to-edit + find-edit + (send text-to-search get-start-position) + (send text-to-search get-end-position) + 0 + #:try-to-move? #f))) (define/public (search searching-direction) (unhide-search #f) diff --git a/collects/framework/private/standard-menus-items.rkt b/collects/framework/private/standard-menus-items.rkt index 888346f724..b5ca249337 100644 --- a/collects/framework/private/standard-menus-items.rkt +++ b/collects/framework/private/standard-menus-items.rkt @@ -360,6 +360,14 @@ '(string-constant find-menu-item) edit-menu:edit-target-on-demand #f) + (make-an-item 'edit-menu 'find-from-selection + '(string-constant find-info) + '(λ (item control) (void)) + #\f + '(cons 'shift (get-default-shortcut-prefix)) + '(string-constant find-from-selection-menu-item) + edit-menu:edit-target-on-demand + #f) (make-an-item 'edit-menu 'find-next '(string-constant find-next-info) diff --git a/collects/scribblings/drracket/menus.scrbl b/collects/scribblings/drracket/menus.scrbl index c2d37dbd2e..77ab028c8e 100644 --- a/collects/scribblings/drracket/menus.scrbl +++ b/collects/scribblings/drracket/menus.scrbl @@ -124,13 +124,20 @@ blinking caret. Each window maintains its own Undo and Redo history. @item{@defmenuitem{Wrap Text} Toggles between wrapped text and unwrapped text in the window.} - @item{@defmenuitem{Find...} Opens an interactive search + @item{@defmenuitem{Find} Opens an interactive search window at the bottom of the frame and moves the insertion point to the search string editor (or out of it, if the insertion point is already there). See also @secref["Searching"].} + @item{@defmenuitem{Find From Selection} + Just like @onscreen{Find}, except that it + copies the current selection into the search + window (but without using the clipboard, so + paste will paste whatever was last copied, + not the new search string)} + @item{@defmenuitem{Find Again} Finds the next occurrence of the text in the search window.} diff --git a/collects/scribblings/framework/frame.scrbl b/collects/scribblings/framework/frame.scrbl index b6244be876..d3bc222edb 100644 --- a/collects/scribblings/framework/frame.scrbl +++ b/collects/scribblings/framework/frame.scrbl @@ -821,12 +821,26 @@ This method hides the searching information on the bottom of the frame. } - @defmethod[(unhide-search [move-focus? boolean?]) void?]{ + @defmethod[(unhide-search [move-focus? boolean?] + [#:new-search-string-from-selection? new-search-string-from-selection? boolean? #f]) + void?]{ When the searching sub window is hidden, makes it visible. If @racket[move-focus?] is @racket[#f], the focus is not moved, but if it is any other value, the focus is moved to the find window. + + If @racket[new-search-string-from-selection?] is a true value and the selection + in the result of @method[frame:searchable<%> get-text-to-search] is not empty, + then the search editor is replaced with the selection. } + @defmethod[(unhide-search-and-toggle-focus + [#:new-search-string-from-selection? new-search-string-from-selection? boolean? #f]) + void?]{ + Like @method[frame:searchable<%> unhide-search], except it also moves the focus into the + text to be searched, or into the search string text, depending on where it + currently is. + } + @defmethod[(get-case-sensitive-search?) boolean?]{ Returns @racket[#t] if the search is currently case-sensitive. (This method's value depends on the preference diff --git a/collects/string-constants/private/english-string-constants.rkt b/collects/string-constants/private/english-string-constants.rkt index dfe49e3edb..147b5973a2 100644 --- a/collects/string-constants/private/english-string-constants.rkt +++ b/collects/string-constants/private/english-string-constants.rkt @@ -733,6 +733,7 @@ please adhere to these guidelines: (select-all-menu-item "Select A&ll") (find-menu-item "Find") ;; menu item + (find-from-selection-menu-item "Find From Selection") (find-info "Toggles the keyboard focus between the window being searched and the search bar") (find-next-info "Skip to the next occurrence of the string in the find window")