From 09901d455e9e9d9182dfc9ceb13a2cc7735980b4 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 27 Feb 2013 19:58:38 +0100 Subject: [PATCH] Add 'gf' (goto file) functionality --- src/file.cc | 12 ++++++++++++ src/file.hh | 1 + src/main.cc | 19 +++++++++++++++++++ 3 files changed, 32 insertions(+) diff --git a/src/file.cc b/src/file.cc index 985e802f..6d184349 100644 --- a/src/file.cc +++ b/src/file.cc @@ -185,4 +185,16 @@ void write_buffer_to_file(const Buffer& buffer, const String& filename) } } +String find_file(const String& filename, const memoryview& 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 ""; +} + } diff --git a/src/file.hh b/src/file.hh index e816d5b4..82f2015c 100644 --- a/src/file.hh +++ b/src/file.hh @@ -30,6 +30,7 @@ String parse_filename(const String& filename); String read_file(const String& filename); Buffer* create_buffer_from_file(const String& filename); void write_buffer_to_file(const Buffer& buffer, const String& filename); +String find_file(const String& filename, const memoryview& paths); } diff --git a/src/main.cc b/src/main.cc index 083b4f40..96a66aac 100644 --- a/src/main.cc +++ b/src/main.cc @@ -93,6 +93,25 @@ void do_go(Context& context) editor.select(buf.iterator_at_line_begin(buf.line_count() - 1), mode); 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 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; + } } }); }