Repository navigation
Search
turboframe.search finds and replaces in plain text, with the options of Turbo Pascal's Find dialog. It knows nothing about widgets: use it on an editor's text, a file, a list.
| Use it for | finding and replacing in a text (turboframe.search), in the editor (SearchMixin), in a folder's files (FindInFilesMixin) |
| Import |
from turboframe.search import SearchOptions, compile_pattern, find_wrapping, replace_all; from turboframe.ui.searching import SearchMixin; from turboframe.ui.findfiles import FindInFilesMixin
|
| Needs | nothing beyond TurboFrame |
| Extend | the mixins' hooks (search_target, find_files_*, show_find_results) |
from turboframe.search import SearchError, SearchOptions, compile_pattern, find_wrapping, replace_all
options = SearchOptions(case_sensitive=False, whole_words=True, regex=False)
try:
pattern = compile_pattern("total", options)
except SearchError as error:
print(error) # "Empty search text", "Bad regular expression: ..."
else:
match, wrapped = find_wrapping(text, pattern, cursor) # from the cursor, then from the top
new_text, count = replace_all(text, pattern, "sum", options)SearchOptions(case_sensitive=False, whole_words=False, regex=False, backward=False, entire_scope=False, prompt=True) |
what SearchDialog collects |
compile_pattern(text, options) |
a re.Pattern; SearchError with a message to show |
find(text, pattern, pos, backward=False) |
the next match from pos, or the previous one |
find_wrapping(text, pattern, pos, backward=False) |
the same, then from the other end: (match, wrapped)
|
expand(match, replacement, options) |
the replacement for one match (\1 only with regex) |
replace_all(text, pattern, replacement, options, start=0) |
(new text, count) |
Empty matches are skipped, so a* never loops. In literal mode, metacharacters in the text are not special and \1 in the replacement is just text.
SearchDialog (see Dialogs) collects these options, and step 3 of the tutorial (Tutorial Dialogs) wires both to an editor by hand.
API: turboframe.search.
SearchMixin (turboframe.ui.searching) gives your DesktopApp Search > Find, Replace and Search again over the active editor window (see The Editor):
-
Find (
CM_FIND) opens the Find dialog with the selection, or the word under the cursor, or the last text. OK selects the next match (the previous one, backward), from the cursor or from the edge (Entire scope), wrapping around the end; the status line says when it wrapped. -
Replace (
CM_REPLACE) replaces the next match. Change all replaces every one as a single edit that one Undo restores, or, with Prompt on replace, asks at each (Yes, No, All, Cancel). -
Search again (
CM_SEARCH_AGAIN) repeats the last Find or Replace from the cursor.
from textual import work
from turboframe.ui.app import DesktopApp
from turboframe.ui.editing import EditorMixin
from turboframe.ui.searching import CM_FIND, CM_REPLACE, CM_SEARCH_AGAIN, SearchMixin
class Workbench(SearchMixin, EditorMixin, DesktopApp):
@work(group="command")
async def run_command(self, command: int, info: object = None) -> None:
if not await self.editor_command(command, info):
await self.search_command(command) # True when it was one of SEARCH_COMMANDSBind your keys to the three commands (Ctrl+F, Ctrl+R, Ctrl+L, say; see Application Mixins), or call await self.find(), replace() and search_again() from a worker.
| Hook or attribute | Default |
|---|---|
search_target() |
the editor window searched: active_editor() (from EditorMixin), else the active window when it has an editor
|
search_text, search_options, search_replacement
|
the last text, options and replacement; Find in files shares the first two |
last_search |
what Search again repeats: "find" or "replace"
|
initial_query(editor, last) is the rule for the Find dialog's first text, on its own.
API: turboframe.ui.searching.
turboframe.ui.findfiles looks for a text in every file of a folder, the way Borland Pascal's Tools > Grep did, and lists what it finds in a window: a row per line with a match, parts/header.tpl(12): <the line>. Enter or a double click goes there.
Mix FindInFilesMixin into your DesktopApp, give it a folder, and run find_in_files() from a worker. TurboFrame has no command number for it: number one of yours (Search > Find in files, Shift+F2).
from textual import work
from turboframe.ui.app import DesktopApp
from turboframe.ui.findfiles import FindInFilesDialog, FindInFilesMixin
FindInFilesDialog.MASK_HINT = "*.py;*.toml" # the example in the dialog's Files label
class Workbench(FindInFilesMixin, DesktopApp):
def __init__(self, folder):
super().__init__()
self.folder = folder
@work
async def find(self) -> None: # Search > Find in files (Shift+F2)
await self.find_in_files()
def goto_location(self, path, start, end=None): # a result was chosen: open path, select start:end
...The dialog asks for the text, the files (masks like *.py;*.md; * is every file) and the Find dialog's options. The files are project_files(folder) (see Files): git's list in a repository, else a walk that skips caches and virtual environments. An open editor's text is searched as it is being edited, saved or not.
The search runs in a thread, so the desktop stays responsive, with a spinner in the status line. It stops at turboframe.files.MAX_FILES files (20000), MAX_MATCHES matches (2000) or MAX_SECONDS seconds (20), skips files that are not text or are larger than files.MAX_TEXT_BYTES, and tells the user when a bound stopped it.
The mixin's hooks have defaults; override the ones your application does differently:
| Hook | Default |
|---|---|
find_files_root() |
the application's folder
|
find_files_edited() |
every window with a path and an editor holding text (an untitled editor, whose path is None, is skipped) |
find_files_selection() |
the active window's selection, when it is on one line |
find_files_no_root() |
a message box: "Open a folder first." |
show_find_results(window) |
insert_window(window), and returns it; place it along the bottom, for example |
The last text and options stay in search_text and search_options, which your Search > Find can share. A second search reuses the results window (find_results_window()). The pieces work on their own too: search(root, files, pattern, *, edited=None, limit=MAX_MATCHES, seconds=MAX_SECONDS) returns (hits, files_with_hits), result_items(root, query, mask, hits, in_files) the window's rows, and masks("*.py;*.md") and wanted(rel, patterns) the files a mask takes. The results window is a LocationsWindow (see Windows and Lists): Enter calls your goto_location, which EditorMixin gives.
API: turboframe.ui.findfiles.
TurboFrame · part of Divengine · GPL-3.0-or-later · report an issue
Tutorial
Examples
The desktop
- The Desktop
- Application Mixins
- Windows and Lists
- Dialogs
- Histories
- Progress
- Themes and Colors
- Glyphs
- Settings and Recent Files
- Command Palette
Editing
Tasks
Languages
Running
Reference
Links