1 |
On Tue, 2020-09-01 at 19:59 -0700, Zac Medico wrote: |
2 |
> Automatically detect regular expressions when the search string |
3 |
> contains any of these regular expression characters or character |
4 |
> sequences: |
5 |
> |
6 |
> ^ $ * [ ] { } | ? .+ |
7 |
|
8 |
Isn't this going to break search for gtk+? |
9 |
|
10 |
> |
11 |
> This simplifies usage, so that users no longer have to remember |
12 |
> to prefix regular expressions with the % character. The new |
13 |
> behavior can be disabled by --regex-search-auto=n, in case the |
14 |
> regular expressions interpretation causes some kind of problem. |
15 |
> |
16 |
> Note that fuzzy search and regular expression search are |
17 |
> mutually exclusive, and fuzzy search remains the default for |
18 |
> search strings that do not contain any regular expression |
19 |
> characters. |
20 |
> |
21 |
> Bug: https://bugs.gentoo.org/737480 |
22 |
> Signed-off-by: Zac Medico <zmedico@g.o> |
23 |
> --- |
24 |
> [PATCH v2] Recognize .+ as suggested by Arfrever. |
25 |
> |
26 |
> lib/_emerge/actions.py | 1 + |
27 |
> lib/_emerge/main.py | 6 ++++++ |
28 |
> lib/_emerge/search.py | 12 +++++++++++- |
29 |
> man/emerge.1 | 12 +++++++++++- |
30 |
> 4 files changed, 29 insertions(+), 2 deletions(-) |
31 |
> |
32 |
> diff --git a/lib/_emerge/actions.py b/lib/_emerge/actions.py |
33 |
> index a4ecfe43d..f57269817 100644 |
34 |
> --- a/lib/_emerge/actions.py |
35 |
> +++ b/lib/_emerge/actions.py |
36 |
> @@ -2036,6 +2036,7 @@ def action_search(root_config, myopts, myfiles, spinner): |
37 |
> search_index=myopts.get("--search-index", "y") != "n", |
38 |
> search_similarity=myopts.get("--search-similarity"), |
39 |
> fuzzy=myopts.get("--fuzzy-search") != "n", |
40 |
> + regex_auto=myopts.get("--regex-search-auto") != "n", |
41 |
> ) |
42 |
> for mysearch in myfiles: |
43 |
> try: |
44 |
> diff --git a/lib/_emerge/main.py b/lib/_emerge/main.py |
45 |
> index 975738762..5075f7f57 100644 |
46 |
> --- a/lib/_emerge/main.py |
47 |
> +++ b/lib/_emerge/main.py |
48 |
> @@ -709,6 +709,12 @@ def parse_opts(tmpcmdline, silent=False): |
49 |
> "action" : "store" |
50 |
> }, |
51 |
> |
52 |
> + "--regex-search-auto": { |
53 |
> + "help" : "Enable or disable automatic regular expression detection for search actions", |
54 |
> + "choices": y_or_n, |
55 |
> + "default": "y", |
56 |
> + }, |
57 |
> + |
58 |
> "--root": { |
59 |
> "help" : "specify the target root filesystem for merging packages", |
60 |
> "action" : "store" |
61 |
> diff --git a/lib/_emerge/search.py b/lib/_emerge/search.py |
62 |
> index a59191c1a..61eed0827 100644 |
63 |
> --- a/lib/_emerge/search.py |
64 |
> +++ b/lib/_emerge/search.py |
65 |
> @@ -28,7 +28,7 @@ class search: |
66 |
> # |
67 |
> def __init__(self, root_config, spinner, searchdesc, |
68 |
> verbose, usepkg, usepkgonly, search_index=True, |
69 |
> - search_similarity=None, fuzzy=True): |
70 |
> + search_similarity=None, fuzzy=True, regex_auto=False): |
71 |
> """Searches the available and installed packages for the supplied search key. |
72 |
> The list of available and installed packages is created at object instantiation. |
73 |
> This makes successive searches faster.""" |
74 |
> @@ -42,6 +42,7 @@ class search: |
75 |
> self.spinner = None |
76 |
> self.root_config = root_config |
77 |
> self.setconfig = root_config.setconfig |
78 |
> + self.regex_auto = regex_auto |
79 |
> self.fuzzy = fuzzy |
80 |
> self.search_similarity = (80 if search_similarity is None |
81 |
> else search_similarity) |
82 |
> @@ -259,6 +260,15 @@ class search: |
83 |
> if '/' in self.searchkey: |
84 |
> match_category = 1 |
85 |
> fuzzy = False |
86 |
> + |
87 |
> + if self.regex_auto and not regexsearch and re.search(r'[\^\$\*\[\]\{\}\|\?]|\.\+', self.searchkey) is not None: |
88 |
> + try: |
89 |
> + re.compile(self.searchkey, re.I) |
90 |
> + except Exception: |
91 |
> + pass |
92 |
> + else: |
93 |
> + regexsearch = True |
94 |
> + |
95 |
> if regexsearch: |
96 |
> self.searchre=re.compile(self.searchkey,re.I) |
97 |
> else: |
98 |
> diff --git a/man/emerge.1 b/man/emerge.1 |
99 |
> index fe7d05a21..c1bcd0220 100644 |
100 |
> --- a/man/emerge.1 |
101 |
> +++ b/man/emerge.1 |
102 |
> @@ -229,7 +229,9 @@ explicitly discarded by running `emaint \-\-fix cleanresume` (see |
103 |
> .BR \-\-search ", " \-s |
104 |
> Searches for matches of the supplied string in the ebuild repository. |
105 |
> By default emerge uses a case-insensitive simple search, but you can |
106 |
> -enable a regular expression search by prefixing the search string with %. |
107 |
> +enable a regular expression search by prefixing the search string with % |
108 |
> +(the % prefix can often be omitted if the |
109 |
> +\fB\-\-regex\-search\-auto\fR option is enabled). |
110 |
> For example, \fBemerge \-\-search "%^kde"\fR searches for any package whose |
111 |
> name starts with "kde"; \fBemerge \-\-search "%gcc$"\fR searches for any |
112 |
> package that ends with "gcc"; \fBemerge \-\-search "office"\fR searches for |
113 |
> @@ -764,6 +766,14 @@ matching packages due to \fB\-\-rebuild\fR. |
114 |
> A space separated list of package names or slot atoms. Emerge will not rebuild |
115 |
> packages that depend on matching packages due to \fB\-\-rebuild\fR. |
116 |
> .TP |
117 |
> +.BR "\-\-regex\-search\-auto < y | n >" |
118 |
> +Enable or disable automatic regular expression detection for search actions. |
119 |
> +If this option is enabled (the default), then regular expression search |
120 |
> +is automatically enabled when the search string is a valid regular expression |
121 |
> +which contains any of these commonly used regular expression characters or |
122 |
> +character sequences: |
123 |
> +^ $ * [ ] { } | ? .+ |
124 |
> +.TP |
125 |
> .BR \-\-oneshot ", " \-1 |
126 |
> Emerge as normal, but do not add the packages to the world file |
127 |
> for later updating. |
128 |
|
129 |
-- |
130 |
Best regards, |
131 |
Michał Górny |