1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
|
/*
* Copyright (C) 2012-2015 Robin Haberkorn
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* 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/>.
*/
#ifdef HAVE_CONFIG_H
#include "config.h"
#endif
#include <string.h>
#include <glib.h>
#include "sciteco.h"
#include "interface.h"
#include "parser.h"
#include "ring.h"
#include "glob.h"
namespace SciTECO {
namespace States {
StateGlob glob;
}
Globber::Globber(const gchar *_pattern)
{
gchar *basename;
dirname = g_path_get_dirname(_pattern);
dir = g_dir_open(dirname, 0, NULL);
/* if dirname does not exist, dir may be NULL */
basename = g_path_get_basename(_pattern);
pattern = g_pattern_spec_new(basename);
g_free(basename);
}
gchar *
Globber::next(void)
{
const gchar *basename;
if (!dir)
return NULL;
while ((basename = g_dir_read_name(dir)))
if (g_pattern_match_string(pattern, basename))
return g_build_filename(dirname, basename, NIL);
return NULL;
}
Globber::~Globber()
{
if (pattern)
g_pattern_spec_free(pattern);
if (dir)
g_dir_close(dir);
g_free(dirname);
}
/*
* Command States
*/
/*$
* EN[pattern]$ -- Get list of files matching a glob pattern
*
* The EN command expands a glob \fIpattern\fP to a list of
* matching file names. This is similar to globbing
* on UNIX but not as powerful.
* A \fIpattern\fP is a file name with \(lq*\(rq and
* \(lq?\(rq wildcards:
* \(lq*\(rq matches an arbitrary, possibly empty, string.
* \(lq?\(rq matches an arbitrary character.
*
* EN will currently only match files in the file name component
* of \fIpattern\fP, not on each component of the path name
* separately.
* In other words, EN only looks through the directory
* of \fIpattern\fP \(em you cannot effectively match
* multiple directories.
*
* The result of the globbing is inserted into the current
* document, at the current position.
* A linefeed is inserted after every file name, i.e.
* every matching file will be on its own line.
*
* String-building characters are enabled for EN.
*/
/*
* NOTE: This does not work like classic TECO's
* EN command (iterative globbing), since the
* position in the directory cannot be reasonably
* reset on rubout with glib's API.
* If we have to perform all the globbing on initialization
* we can just as well return all the results at once.
* And we can add them to the current document since
* when they should be in a register, the user will
* have to edit that register anyway.
*/
State *
StateGlob::done(const gchar *str)
{
BEGIN_EXEC(&States::start);
Globber globber(str);
gchar *filename;
bool text_added = false;
interface.ssm(SCI_BEGINUNDOACTION);
while ((filename = globber.next())) {
size_t len = strlen(filename);
/* overwrite trailing null */
filename[len] = '\n';
/*
* FIXME: Once we're 8-bit clean, we should
* add the filenames null-terminated
* (there may be linebreaks in filename).
*/
interface.ssm(SCI_ADDTEXT, len+1,
(sptr_t)filename);
g_free(filename);
text_added = true;
}
interface.ssm(SCI_SCROLLCARET);
interface.ssm(SCI_ENDUNDOACTION);
if (text_added) {
ring.dirtify();
if (current_doc_must_undo())
interface.undo_ssm(SCI_UNDO);
}
return &States::start;
}
} /* namespace SciTECO */
|