2011-09-14 17:41:56 +02:00
|
|
|
#include "completion.hh"
|
|
|
|
|
2011-09-22 20:55:45 +02:00
|
|
|
#include "buffer_manager.hh"
|
2011-09-14 17:41:56 +02:00
|
|
|
#include "utils.hh"
|
2012-09-12 19:42:12 +02:00
|
|
|
#include "file.hh"
|
2012-11-29 20:09:37 +01:00
|
|
|
#include "context.hh"
|
2011-09-14 17:41:56 +02:00
|
|
|
|
|
|
|
#include <dirent.h>
|
2012-01-15 03:02:18 +01:00
|
|
|
#include <algorithm>
|
2011-09-14 17:41:56 +02:00
|
|
|
|
2012-11-29 20:09:37 +01:00
|
|
|
#include <boost/regex.hpp>
|
|
|
|
|
2011-09-14 17:41:56 +02:00
|
|
|
namespace Kakoune
|
|
|
|
{
|
|
|
|
|
2012-12-12 13:51:15 +01:00
|
|
|
static boost::regex make_regex_ifp(const String& ex)
|
|
|
|
{
|
|
|
|
boost::regex result;
|
|
|
|
if (not ex.empty())
|
|
|
|
{
|
|
|
|
try
|
|
|
|
{
|
|
|
|
result = boost::regex(ex.c_str());
|
|
|
|
}
|
|
|
|
catch(boost::regex_error&) {}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-08-06 21:37:43 +02:00
|
|
|
CandidateList complete_filename(const Context& context,
|
|
|
|
const String& prefix,
|
2012-10-11 00:41:48 +02:00
|
|
|
ByteCount cursor_pos)
|
2011-09-14 17:41:56 +02:00
|
|
|
{
|
2012-09-12 19:42:12 +02:00
|
|
|
String real_prefix = parse_filename(prefix.substr(0, cursor_pos));
|
2012-04-14 03:17:09 +02:00
|
|
|
String dirname = "./";
|
|
|
|
String dirprefix;
|
|
|
|
String fileprefix = real_prefix;
|
2011-09-14 17:41:56 +02:00
|
|
|
|
2012-12-12 13:51:15 +01:00
|
|
|
boost::regex ignored_files = make_regex_ifp(context.options()["ignored_files"].as_string());
|
2012-11-29 20:09:37 +01:00
|
|
|
|
2012-10-11 00:41:48 +02:00
|
|
|
ByteCount dir_end = -1;
|
|
|
|
for (ByteCount i = 0; i < real_prefix.length(); ++i)
|
2011-09-14 17:41:56 +02:00
|
|
|
{
|
2012-08-07 00:16:51 +02:00
|
|
|
if (real_prefix[i] == '/')
|
|
|
|
dir_end = i;
|
|
|
|
}
|
|
|
|
if (dir_end != -1)
|
|
|
|
{
|
|
|
|
dirname = real_prefix.substr(0, dir_end + 1);
|
2011-12-22 14:44:04 +01:00
|
|
|
dirprefix = dirname;
|
2012-08-07 00:16:51 +02:00
|
|
|
fileprefix = real_prefix.substr(dir_end + 1);
|
2011-09-14 17:41:56 +02:00
|
|
|
}
|
|
|
|
|
2012-06-12 20:45:13 +02:00
|
|
|
DIR* dir = opendir(dirname.c_str());
|
2012-12-12 13:51:15 +01:00
|
|
|
auto closeDir = on_scope_end([=]{ closedir(dir); });
|
2011-09-14 17:41:56 +02:00
|
|
|
|
|
|
|
CandidateList result;
|
2012-03-04 20:43:47 +01:00
|
|
|
if (not dir)
|
|
|
|
return result;
|
|
|
|
|
2012-12-11 13:40:58 +01:00
|
|
|
const bool check_ignored_files = not ignored_files.empty() and
|
2012-12-12 13:51:15 +01:00
|
|
|
not boost::regex_match(fileprefix.c_str(), ignored_files);
|
2012-12-11 13:40:58 +01:00
|
|
|
|
2012-12-12 13:51:15 +01:00
|
|
|
boost::regex file_regex = make_regex_ifp(fileprefix);
|
|
|
|
CandidateList regex_result;
|
2011-09-14 17:41:56 +02:00
|
|
|
while (dirent* entry = readdir(dir))
|
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
String filename = entry->d_name;
|
2012-01-15 03:01:58 +01:00
|
|
|
if (filename.empty())
|
|
|
|
continue;
|
|
|
|
|
2012-12-12 13:51:15 +01:00
|
|
|
if (check_ignored_files and boost::regex_match(filename.c_str(), ignored_files))
|
2012-11-29 20:09:37 +01:00
|
|
|
continue;
|
|
|
|
|
2012-12-12 13:51:15 +01:00
|
|
|
const bool match_prefix = (filename.substr(0, fileprefix.length()) == fileprefix);
|
|
|
|
const bool match_regex = not file_regex.empty() and
|
|
|
|
boost::regex_match(filename.c_str(), file_regex);
|
|
|
|
|
|
|
|
if (match_prefix or match_regex)
|
2011-12-22 14:44:04 +01:00
|
|
|
{
|
2012-04-14 03:17:09 +02:00
|
|
|
String name = dirprefix + filename;
|
2011-12-22 14:44:04 +01:00
|
|
|
if (entry->d_type == DT_DIR)
|
|
|
|
name += '/';
|
2012-08-28 14:10:05 +02:00
|
|
|
if (fileprefix.length() != 0 or filename[0] != '.')
|
2012-12-12 13:51:15 +01:00
|
|
|
{
|
|
|
|
if (match_prefix)
|
|
|
|
result.push_back(escape(name));
|
|
|
|
if (match_regex)
|
|
|
|
regex_result.push_back(escape(name));
|
|
|
|
}
|
2011-12-22 14:44:04 +01:00
|
|
|
}
|
2011-09-14 17:41:56 +02:00
|
|
|
}
|
2012-12-12 13:51:15 +01:00
|
|
|
CandidateList& real_result = result.empty() ? regex_result : result;
|
|
|
|
std::sort(real_result.begin(), real_result.end());
|
|
|
|
return real_result;
|
2011-09-14 17:41:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|