Add 'gf' (goto file) functionality

This commit is contained in:
Maxime Coste 2013-02-27 19:58:38 +01:00
parent 99475f93d9
commit 09901d455e
3 changed files with 32 additions and 0 deletions

View File

@ -185,4 +185,16 @@ void write_buffer_to_file(const Buffer& buffer, const String& filename)
} }
} }
String find_file(const String& filename, const memoryview<String>& paths)
{
for (auto path : paths)
{
String candidate = path + filename;
struct stat buf;
if (stat(candidate.c_str(), &buf) == 0 and S_ISREG(buf.st_mode))
return candidate;
}
return "";
}
} }

View File

@ -30,6 +30,7 @@ String parse_filename(const String& filename);
String read_file(const String& filename); String read_file(const String& filename);
Buffer* create_buffer_from_file(const String& filename); Buffer* create_buffer_from_file(const String& filename);
void write_buffer_to_file(const Buffer& buffer, const String& filename); void write_buffer_to_file(const Buffer& buffer, const String& filename);
String find_file(const String& filename, const memoryview<String>& paths);
} }

View File

@ -93,6 +93,25 @@ void do_go(Context& context)
editor.select(buf.iterator_at_line_begin(buf.line_count() - 1), mode); editor.select(buf.iterator_at_line_begin(buf.line_count() - 1), mode);
break; break;
} }
case 'f':
{
String filename = context.editor().selections().back().content();
static char forbidden[] = { '\'', '\\', '\0' };
for (auto c : forbidden)
if (contains(filename, c))
return;
std::vector<String> paths = { ""_str, "/usr/include/"_str };
const String& buffer_name = context.buffer().name();
auto it = find(reversed(buffer_name), '/');
if (it != buffer_name.rend())
paths.insert(paths.begin(), String{buffer_name.begin(), it.base()});
String path = find_file(filename, paths);
if (not path.empty())
CommandManager::instance().execute("edit '" + path + "'", context);
break;
}
} }
}); });
} }