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