1
1
mirror of https://github.com/mawww/kakoune.git synced 2024-11-29 01:24:52 +03:00

Support % in path option to mean current buffer directory

In the end, % is not that painful to work with as its only set seldomly,
and we usually dont need to use expansion at the same time. Moreover, it
just requires a single \ to be escaped.

Fixes #1562
This commit is contained in:
Maxime Coste 2018-03-23 08:22:34 +11:00
parent 42404ddb3a
commit a732037b53
5 changed files with 24 additions and 11 deletions

View File

@ -48,14 +48,21 @@ public:
: runtime_error(format("fd {}: {}", fd, error_desc)) {}
};
String parse_filename(StringView filename)
String parse_filename(StringView filename, StringView buf_dir)
{
auto prefix = filename.substr(0_byte, 2_byte);
if (prefix == "~" or prefix == "~/")
return homedir() + filename.substr(1_byte);
if ((prefix == "%" or prefix == "%/") and not buf_dir.empty())
return buf_dir + filename.substr(1_byte);
return filename.str();
}
String parse_filename(StringView filename)
{
return parse_filename(filename, {});
}
std::pair<StringView, StringView> split_path(StringView path)
{
auto it = find(path | reverse(), '/');
@ -331,7 +338,7 @@ void write_buffer_to_backup_file(Buffer& buffer)
}
}
String find_file(StringView filename, ConstArrayView<String> paths)
String find_file(StringView filename, StringView buf_dir, ConstArrayView<String> paths)
{
struct stat buf;
if (filename.substr(0_byte, 1_byte) == "/")
@ -348,7 +355,7 @@ String find_file(StringView filename, ConstArrayView<String> paths)
return "";
}
for (auto candidate : paths | transform(parse_filename))
for (auto candidate : paths | transform([&](StringView s) { return parse_filename(s, buf_dir); }))
{
if (not candidate.empty() and candidate.back() != '/')
candidate += '/';

View File

@ -19,8 +19,10 @@ class Regex;
using CandidateList = Vector<String, MemoryDomain::Completion>;
// parse ~/ and $env values in filename and returns the translated filename
// parse ~/ and %/ in filename and returns the translated filename
String parse_filename(StringView filename, StringView buf_dir);
String parse_filename(StringView filename);
String real_path(StringView filename);
String compact_path(StringView filename);
@ -54,7 +56,7 @@ void write_buffer_to_file(Buffer& buffer, StringView filename, bool force = fals
void write_buffer_to_fd(Buffer& buffer, int fd);
void write_buffer_to_backup_file(Buffer& buffer);
String find_file(StringView filename, ConstArrayView<String> paths);
String find_file(StringView filename, StringView buf_dir, ConstArrayView<String> paths);
bool file_exists(StringView filename);
Vector<String> list_files(StringView directory);

View File

@ -246,6 +246,13 @@ InsertCompletion complete_filename(const SelectionList& sels,
{
if (not dir.empty() and dir.back() != '/')
dir += '/';
if (dir.substr(0, 2_byte) == "%/")
{
if (not (buffer.flags() & Buffer::Flags::File))
continue;
dir = split_path(buffer.name()).first.str() + '/' + dir.substr(2_byte);
}
for (auto& filename : Kakoune::complete_filename(dir + prefix,
options["ignored_files"].get<Regex>()))
{

View File

@ -323,7 +323,7 @@ void register_options()
Regex{});
reg.declare_option("filetype", "buffer filetype", ""_str);
reg.declare_option("path", "path to consider when trying to find a file",
Vector<String, MemoryDomain::Options>({ "./", "/usr/include" }));
Vector<String, MemoryDomain::Options>({ "./", "%/", "/usr/include" }));
reg.declare_option("completers", "insert mode completers to execute.",
InsertCompleterDescList({
InsertCompleterDesc{ InsertCompleterDesc::Filename, {} },

View File

@ -278,11 +278,8 @@ void goto_commands(Context& context, NormalParams params)
return;
auto paths = context.options()["path"].get<Vector<String, MemoryDomain::Options>>();
StringView buffer_dir = split_path(buffer.name()).first;
if (not buffer_dir.empty())
paths.insert(paths.begin(), buffer_dir.str());
String path = find_file(filename, paths);
const StringView buffer_dir = split_path(buffer.name()).first;
String path = find_file(filename, buffer_dir, paths);
if (path.empty())
throw runtime_error(format("unable to find file '{}'", filename));