NeoMutt  2024-03-23-147-g885fbc
Teaching an old dog new tricks
DOXYGEN
Loading...
Searching...
No Matches
complete.c
Go to the documentation of this file.
1
29#include "config.h"
30#include <stddef.h>
31#include <stdbool.h>
32#include <string.h>
33#include "mutt/lib.h"
34#include "core/lib.h"
35#include "gui/lib.h"
36#include "lib.h"
37#include "complete/lib.h"
38#include "editor/lib.h"
39#include "history/lib.h"
40#include "mutt_mailbox.h"
41#include "muttlib.h"
42
46int complete_file_mbox(struct EnterWindowData *wdata, int op)
47{
48 if (!wdata)
49 return FR_NO_ACTION;
50
51 struct FileCompletionData *cdata = wdata->cdata;
52
53 if (op == OP_EDITOR_MAILBOX_CYCLE)
54 {
55 wdata->first = true; /* clear input if user types a real key later */
56 buf_mb_wcstombs(wdata->buffer, wdata->state->wbuf, wdata->state->curpos);
57 mutt_mailbox_next(cdata->mailbox, wdata->buffer);
58
59 wdata->state->curpos = wdata->state->lastchar = mutt_mb_mbstowcs(
60 &wdata->state->wbuf, &wdata->state->wbuflen, 0, buf_string(wdata->buffer));
61 return FR_SUCCESS;
62 }
63
64 if ((op != OP_EDITOR_COMPLETE) && (op != OP_EDITOR_COMPLETE_QUERY))
65 return FR_NO_ACTION;
66
67 int rc = FR_SUCCESS;
68 buf_mb_wcstombs(wdata->buffer, wdata->state->wbuf, wdata->state->curpos);
69
70 /* see if the path has changed from the last time */
71 if ((!wdata->tempbuf && !wdata->state->lastchar) ||
72 (wdata->tempbuf && (wdata->templen == wdata->state->lastchar) &&
73 (memcmp(wdata->tempbuf, wdata->state->wbuf,
74 wdata->state->lastchar * sizeof(wchar_t)) == 0)))
75 {
77 if (wdata->hclass == HC_MAILBOX)
78 flags |= MUTT_SEL_FOLDER;
79 if (cdata->multiple)
80 flags |= MUTT_SEL_MULTI;
81
82 dlg_browser(wdata->buffer, flags, cdata->mailbox, cdata->files, cdata->numfiles);
83 if (!buf_is_empty(wdata->buffer))
84 {
86 if (!wdata->pass)
87 mutt_hist_add(wdata->hclass, buf_string(wdata->buffer), true);
88 wdata->done = true;
89 return FR_SUCCESS;
90 }
91
92 /* file selection cancelled */
93 return FR_CONTINUE;
94 }
95
96 if (mutt_complete(wdata->cd, wdata->buffer) == 0)
97 {
98 wdata->templen = wdata->state->lastchar;
99 mutt_mem_realloc(&wdata->tempbuf, wdata->templen * sizeof(wchar_t));
100 if (wdata->tempbuf)
101 memcpy(wdata->tempbuf, wdata->state->wbuf, wdata->templen * sizeof(wchar_t));
102 }
103 else
104 {
105 return FR_ERROR; // let the user know that nothing matched
106 }
107 replace_part(wdata->state, 0, buf_string(wdata->buffer));
108 return rc;
109}
110
114int complete_file_simple(struct EnterWindowData *wdata, int op)
115{
116 if (!wdata || ((op != OP_EDITOR_COMPLETE) && (op != OP_EDITOR_COMPLETE_QUERY)))
117 return FR_NO_ACTION;
118
119 int rc = FR_SUCCESS;
120 size_t i;
121 for (i = wdata->state->curpos;
122 (i > 0) && !mutt_mb_is_shell_char(wdata->state->wbuf[i - 1]); i--)
123 {
124 }
125 buf_mb_wcstombs(wdata->buffer, wdata->state->wbuf + i, wdata->state->curpos - i);
126 if (wdata->tempbuf && (wdata->templen == (wdata->state->lastchar - i)) &&
127 (memcmp(wdata->tempbuf, wdata->state->wbuf + i,
128 (wdata->state->lastchar - i) * sizeof(wchar_t)) == 0))
129 {
130 dlg_browser(wdata->buffer, MUTT_SEL_NO_FLAGS, NULL, NULL, NULL);
131 if (!buf_is_empty(wdata->buffer))
132 replace_part(wdata->state, i, buf_string(wdata->buffer));
133 return FR_CONTINUE;
134 }
135
136 if (mutt_complete(wdata->cd, wdata->buffer) == 0)
137 {
138 wdata->templen = wdata->state->lastchar - i;
139 mutt_mem_realloc(&wdata->tempbuf, wdata->templen * sizeof(wchar_t));
140 if (wdata->tempbuf)
141 memcpy(wdata->tempbuf, wdata->state->wbuf + i, wdata->templen * sizeof(wchar_t));
142 }
143 else
144 {
145 rc = FR_ERROR;
146 }
147
148 replace_part(wdata->state, i, buf_string(wdata->buffer));
149 return rc;
150}
151
157};
158
164};
const struct CompleteOps CompleteFileOps
Auto-Completion of Files.
Definition: complete.c:155
const struct CompleteOps CompleteMailboxOps
Auto-Completion of Files / Mailboxes.
Definition: complete.c:162
#define MUTT_SEL_FOLDER
Select a local directory.
Definition: lib.h:60
#define MUTT_SEL_MULTI
Multi-selection is enabled.
Definition: lib.h:59
#define MUTT_SEL_NO_FLAGS
No flags are set.
Definition: lib.h:57
uint8_t SelectFileFlags
Flags for mutt_select_file(), e.g. MUTT_SEL_MAILBOX.
Definition: lib.h:56
bool buf_is_empty(const struct Buffer *buf)
Is the Buffer empty?
Definition: buffer.c:290
static const char * buf_string(const struct Buffer *buf)
Convert a buffer to a const char * "string".
Definition: buffer.h:96
int mutt_complete(struct CompletionData *cd, struct Buffer *buf)
Attempt to complete a partial pathname.
Definition: complete.c:57
Auto-completion.
Convenience wrapper for the core headers.
@ FR_SUCCESS
Valid function - successfully performed.
Definition: dispatcher.h:39
@ FR_ERROR
Valid function - error occurred.
Definition: dispatcher.h:38
@ FR_CONTINUE
Remain in the Dialog.
Definition: dispatcher.h:34
@ FR_NO_ACTION
Valid function - no action performed.
Definition: dispatcher.h:37
void replace_part(struct EnterState *es, size_t from, const char *buf)
Search and replace on a buffer.
Definition: functions.c:132
Edit a string.
int complete_file_simple(struct EnterWindowData *wdata, int op)
Complete a filename - Implements CompleteOps::complete() -.
Definition: complete.c:114
int complete_file_mbox(struct EnterWindowData *wdata, int op)
Complete a Mailbox - Implements CompleteOps::complete() -.
Definition: complete.c:46
void dlg_browser(struct Buffer *file, SelectFileFlags flags, struct Mailbox *m, char ***files, int *numfiles)
Let the user select a file -.
Definition: dlg_browser.c:1296
Convenience wrapper for the gui headers.
Read/write command history from/to a file.
@ HC_MAILBOX
Mailboxes.
Definition: lib.h:57
void mutt_hist_add(enum HistoryClass hclass, const char *str, bool save)
Add a string to a history.
Definition: history.c:488
bool mutt_mb_is_shell_char(wchar_t ch)
Is character not typically part of a pathname.
Definition: mbyte.c:340
size_t mutt_mb_mbstowcs(wchar_t **pwbuf, size_t *pwbuflen, size_t i, const char *buf)
Convert a string from multibyte to wide characters.
Definition: mbyte.c:291
void buf_mb_wcstombs(struct Buffer *dest, const wchar_t *wstr, size_t wlen)
Convert a string from wide to multibyte characters.
Definition: mbyte.c:256
void mutt_mem_realloc(void *ptr, size_t size)
Resize a block of memory on the heap.
Definition: memory.c:114
Convenience wrapper for the library headers.
struct Mailbox * mutt_mailbox_next(struct Mailbox *m_cur, struct Buffer *s)
Incoming folders completion routine.
Definition: mutt_mailbox.c:359
Mailbox helper functions.
void buf_pretty_mailbox(struct Buffer *buf)
Shorten a mailbox path using '~' or '='.
Definition: muttlib.c:554
Some miscellaneous functions.
Key value store.
enum FunctionRetval(* complete)(struct EnterWindowData *wdata, int op)
Definition: compapi.h:46
size_t curpos
Position of the cursor.
Definition: state.h:36
size_t wbuflen
Length of buffer.
Definition: state.h:34
wchar_t * wbuf
Buffer for the string being entered.
Definition: state.h:33
size_t lastchar
Position of the last character.
Definition: state.h:35
Data to fill the Enter Window.
Definition: wdata.h:46
bool pass
Password mode, conceal characters.
Definition: wdata.h:58
void * cdata
Auto-Completion private data.
Definition: wdata.h:53
struct CompletionData * cd
Auto-completion state data.
Definition: wdata.h:67
struct Buffer * buffer
struct Buffer for the result
Definition: wdata.h:48
bool done
Is text-entry done?
Definition: wdata.h:65
bool first
First time through, no input yet.
Definition: wdata.h:59
wchar_t * tempbuf
Buffer used by completion.
Definition: wdata.h:60
struct EnterState * state
Current state of text entry.
Definition: wdata.h:50
enum HistoryClass hclass
History to use, e.g. HC_NEO_COMMAND.
Definition: wdata.h:51
size_t templen
Length of complete buffer.
Definition: wdata.h:61
Input for the file completion function.
Definition: curs_lib.h:40
char *** files
List of files selected.
Definition: curs_lib.h:43
struct Mailbox * mailbox
Mailbox.
Definition: curs_lib.h:42
bool multiple
Allow multiple selections.
Definition: curs_lib.h:41
int * numfiles
Number of files selected.
Definition: curs_lib.h:44