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