NeoMutt  2025-12-11-435-g4ac674
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
query.h File Reference

Notmuch query functions. More...

#include <stdbool.h>
#include <stddef.h>
+ Include dependency graph for query.h:
+ This graph shows which files directly or indirectly include this file:

Go to the source code of this file.

Enumerations

enum  NmQueryType { NM_QUERY_TYPE_MESGS = 1 , NM_QUERY_TYPE_THREADS , NM_QUERY_TYPE_UNKNOWN }
 Notmuch Query Types. More...
 
enum  NmWindowQueryRc { NM_WINDOW_QUERY_SUCCESS = 1 , NM_WINDOW_QUERY_INVALID_TIMEBASE , NM_WINDOW_QUERY_INVALID_DURATION }
 Return codes for nm_windowed_query_from_query() More...
 

Functions

enum NmQueryType nm_parse_type_from_query (char *buf, enum NmQueryType fallback)
 Parse a query type out of a query.
 
enum NmQueryType nm_string_to_query_type (const char *str)
 Lookup a query type.
 
enum NmQueryType nm_string_to_query_type_mapper (const char *str)
 Lookup a query type.
 
const char * nm_query_type_to_string (enum NmQueryType query_type)
 Turn a query type into a string.
 
enum NmWindowQueryRc nm_windowed_query_from_query (char *buf, size_t buflen, const bool force_enable, const short duration, const short current_pos, const char *current_search, const char *timebase, const char *or_terms)
 Windows buf with notmuch date: search term.
 
bool nm_query_window_check_timebase (const char *timebase)
 Checks if a given timebase string is valid.
 

Detailed Description

Notmuch query functions.

Authors
  • Austin Ray

This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this program. If not, see http://www.gnu.org/licenses/.

Definition in file query.h.

Enumeration Type Documentation

◆ NmQueryType

Notmuch Query Types.

Read whole-thread or matching messages only?

Enumerator
NM_QUERY_TYPE_MESGS 

Default: Messages only.

NM_QUERY_TYPE_THREADS 

Whole threads.

NM_QUERY_TYPE_UNKNOWN 

Unknown query type. Error in notmuch query.

Definition at line 34 of file query.h.

35{
39};
@ NM_QUERY_TYPE_UNKNOWN
Unknown query type. Error in notmuch query.
Definition query.h:38
@ NM_QUERY_TYPE_THREADS
Whole threads.
Definition query.h:37
@ NM_QUERY_TYPE_MESGS
Default: Messages only.
Definition query.h:36

◆ NmWindowQueryRc

Return codes for nm_windowed_query_from_query()

Enumerator
NM_WINDOW_QUERY_SUCCESS 

Query was successful.

NM_WINDOW_QUERY_INVALID_TIMEBASE 

Invalid timebase.

NM_WINDOW_QUERY_INVALID_DURATION 

Invalid duration.

Definition at line 44 of file query.h.

45{
49};
@ NM_WINDOW_QUERY_SUCCESS
Query was successful.
Definition query.h:46
@ NM_WINDOW_QUERY_INVALID_DURATION
Invalid duration.
Definition query.h:48
@ NM_WINDOW_QUERY_INVALID_TIMEBASE
Invalid timebase.
Definition query.h:47

Function Documentation

◆ nm_parse_type_from_query()

enum NmQueryType nm_parse_type_from_query ( char * buf,
enum NmQueryType fallback )

Parse a query type out of a query.

Parameters
bufBuffer for URL
fallbackFallback query type if buf doesn't contain a type= statement
Return values
enumNmQueryType, Notmuch query type

If a user writes a query for a vfolder and includes a type= statement, that type= will be encoded, which Notmuch will treat as part of the query= statement. This method will remove the type= and return its corresponding NmQueryType representation.

Definition at line 50 of file query.c.

51{
52 enum NmQueryType query_type = fallback;
53
54 if (!buf)
55 return query_type;
56
57 size_t buflen = mutt_str_len(buf);
58 const char *message_ptr = mutt_istrn_rfind(buf, buflen, "type=messages");
59 const char *thread_ptr = mutt_istrn_rfind(buf, buflen, "type=threads");
60
61 // No valid type statement found.
62 if (!message_ptr && !thread_ptr)
63 return query_type;
64
65 // Determine the last valid "type=" statement.
66 if ((!message_ptr && thread_ptr) || (thread_ptr > message_ptr))
67 {
68 query_type = NM_QUERY_TYPE_THREADS;
69 }
70 else
71 {
72 query_type = NM_QUERY_TYPE_MESGS;
73 }
74
75 // Clean-up any valid "type=" statements.
76 // The six variations of how "type=" could appear.
77 const char *variants[6] = { "&type=threads", "&type=messages",
78 "type=threads&", "type=messages&",
79 "type=threads", "type=messages" };
80 int variants_size = countof(variants);
81
82 for (int i = 0; i < variants_size; i++)
83 {
84 mutt_istr_remall(buf, variants[i]);
85 }
86
87 return query_type;
88}
#define countof(x)
Definition memory.h:49
const char * mutt_istrn_rfind(const char *haystack, size_t haystack_length, const char *needle)
Find last instance of a substring, ignoring case.
Definition string.c:473
int mutt_istr_remall(char *str, const char *target)
Remove all occurrences of substring, ignoring case.
Definition string.c:747
size_t mutt_str_len(const char *a)
Calculate the length of a string, safely.
Definition string.c:500
NmQueryType
Notmuch Query Types.
Definition query.h:35
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_string_to_query_type()

enum NmQueryType nm_string_to_query_type ( const char * str)

Lookup a query type.

Parameters
strString to lookup
Return values
enumNmQueryType, e.g. NM_QUERY_TYPE_MESGS

If there's an unknown query type, default to NM_QUERY_TYPE_MESGS.

Definition at line 111 of file query.c.

112{
113 enum NmQueryType query_type = nm_string_to_query_type_mapper(str);
114
115 if (query_type == NM_QUERY_TYPE_UNKNOWN)
116 {
117 mutt_error(_("failed to parse notmuch query type: %s"), NONULL(str));
118 return NM_QUERY_TYPE_MESGS;
119 }
120
121 return query_type;
122}
#define mutt_error(...)
Definition logging2.h:94
#define _(a)
Definition message.h:28
enum NmQueryType nm_string_to_query_type_mapper(const char *str)
Lookup a query type.
Definition query.c:130
#define NONULL(x)
Definition string2.h:44
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_string_to_query_type_mapper()

enum NmQueryType nm_string_to_query_type_mapper ( const char * str)

Lookup a query type.

Parameters
strString to lookup
Return values
numQuery type
NM_QUERY_TYPE_UNKNOWNon error

Definition at line 130 of file query.c.

131{
132 if (mutt_str_equal(str, "threads"))
134 if (mutt_str_equal(str, "messages"))
135 return NM_QUERY_TYPE_MESGS;
136
138}
bool mutt_str_equal(const char *a, const char *b)
Compare two strings.
Definition string.c:662
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_query_type_to_string()

const char * nm_query_type_to_string ( enum NmQueryType query_type)

Turn a query type into a string.

Parameters
query_typeQuery type
Return values
ptrString
Note
This is a static string and must not be freed.

Definition at line 97 of file query.c.

98{
99 if (query_type == NM_QUERY_TYPE_THREADS)
100 return "threads";
101 return "messages";
102}
+ Here is the caller graph for this function:

◆ nm_windowed_query_from_query()

enum NmWindowQueryRc nm_windowed_query_from_query ( char * buf,
size_t buflen,
const bool force_enable,
const short duration,
const short cur_pos,
const char * cur_search,
const char * timebase,
const char * or_terms )

Windows buf with notmuch date: search term.

Parameters
[out]bufallocated string buffer to receive the modified search query
[in]buflenallocated maximum size of the buf string buffer
[in]force_enableEnables windowing for duration=0
[in]durationDuration of time between beginning and end for notmuch date search term
[in]cur_posCurrent position of vfolder window
[in]cur_searchCurrent notmuch search
[in]timebaseTimebase for date: search term. Must be: hour, day, week, month, or year
[in]or_termsAdditional notmuch search terms
Return values
NM_WINDOW_QUERY_SUCCESSPrepended buf with date: search term
NM_WINDOW_QUERY_INVALID_DURATIONDuration out-of-range for search term. buf not prepended with date:
NM_WINDOW_QUERY_INVALID_TIMEBASETimebase isn't one of hour, day, week, month, or year

This is where the magic of windowed queries happens. Taking a vfolder search query string as parameter, it will use the following two user settings:

  • duration and
  • timebase

to amend given vfolder search window. Then using a third parameter:

  • cur_pos

it will generate a proper notmuch date: parameter. For example, given a duration of 2, a timebase set to week and a position defaulting to 0, it will prepend to the 'tag:inbox' notmuch search query the following string:

  • query: tag:inbox
  • buf: date:2week..now and tag:inbox

If the position is set to 4, with duration=3 and timebase=month:

  • query: tag:archived
  • buf: date:12month..9month and tag:archived

The window won't be applied:

  • If the duration of the search query is set to 0 this function will be disabled unless a user explicitly enables windowed queries. This returns NM_WINDOW_QUERY_INVALID_DURATION
  • If the timebase is invalid, it will return NM_WINDOW_QUERY_INVALID_TIMEBASE

Definition at line 207 of file query.c.

210{
211 // if the duration is a non positive integer, disable the window unless the
212 // user explicitly enables windowed queries.
213 if (!force_enable && (duration <= 0))
214 {
216 }
217
218 int beg = duration * (cur_pos + 1);
219 int end = duration * cur_pos;
220
221 // If the duration is 0, we want to generate a query spanning a single timebase.
222 // For example, `date:1month..1month` spans the previous month.
223 if ((duration == 0) && (cur_pos != 0))
224 {
225 end = cur_pos;
226 beg = end;
227 }
228
229 if (!nm_query_window_check_timebase(timebase))
230 {
232 }
233
234 size_t length = 0;
235 if (end == 0)
236 {
237 // Open-ended date allows mail from the future.
238 // This may occur is the sender's time settings are off.
239 length = snprintf(buf, buflen, "date:%d%s..", beg, timebase);
240 }
241 else
242 {
243 length = snprintf(buf, buflen, "date:%d%s..%d%s", beg, timebase, end, timebase);
244 }
245
246 if (!mutt_str_equal(or_terms, ""))
247 {
248 char *date_part = mutt_str_dup(buf);
249 length = snprintf(buf, buflen, "(%s or (%s))", date_part, or_terms);
250 FREE(&date_part);
251 }
252
253 // Add current search to window query.
254 snprintf(buf + length, buflen - length, " and %s", cur_search);
255
257}
#define FREE(x)
Free memory and set the pointer to NULL.
Definition memory.h:68
char * mutt_str_dup(const char *str)
Copy a string, safely.
Definition string.c:257
bool nm_query_window_check_timebase(const char *timebase)
Checks if a given timebase string is valid.
Definition query.c:150
+ Here is the call graph for this function:
+ Here is the caller graph for this function:

◆ nm_query_window_check_timebase()

bool nm_query_window_check_timebase ( const char * timebase)

Checks if a given timebase string is valid.

Parameters
[in]timebasestring containing a time base
Return values
trueThe given time base is valid

This function returns whether a given timebase string is valid or not, which is used to validate the user settable configuration setting:

nm_query_window_timebase

Definition at line 150 of file query.c.

151{
152 if ((mutt_str_equal(timebase, "hour")) || (mutt_str_equal(timebase, "day")) ||
153 (mutt_str_equal(timebase, "week")) ||
154 (mutt_str_equal(timebase, "month")) || (mutt_str_equal(timebase, "year")))
155 {
156 return true;
157 }
158 return false;
159}
+ Here is the call graph for this function:
+ Here is the caller graph for this function: