summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2011-09-14 15:41:56 +0000
committerMaxime Coste <frrrwww@gmail.com>2011-09-14 15:41:56 +0000
commitb59ad6a174b49a049fd6789cbeb54dc40fd85fa1 (patch)
tree71c8a6c43696e080cc3bcfbe68c89e2516b24c69 /src
parent4b38cd3cd0131b2ab276310cc234ab666bd99363 (diff)
Completion: dedicated completion header and basic filename completion
Diffstat (limited to 'src')
-rw-r--r--src/command_manager.cc7
-rw-r--r--src/command_manager.hh12
-rw-r--r--src/completion.cc34
-rw-r--r--src/completion.hh25
4 files changed, 67 insertions, 11 deletions
diff --git a/src/command_manager.cc b/src/command_manager.cc
index d62fbdc2..26751db4 100644
--- a/src/command_manager.cc
+++ b/src/command_manager.cc
@@ -96,6 +96,13 @@ Completions CommandManager::complete(const std::string& command_line, size_t cur
return result;
}
+ if (token_to_complete == 1) // filename completion
+ {
+ Completions result(tokens[1].first, cursor_pos);
+ std::string prefix = command_line.substr(tokens[1].first, cursor_pos);
+ result.candidates = complete_filename(prefix);
+ return result;
+ }
return Completions(cursor_pos, cursor_pos);
}
diff --git a/src/command_manager.hh b/src/command_manager.hh
index fc3d81f3..e46c5a77 100644
--- a/src/command_manager.hh
+++ b/src/command_manager.hh
@@ -7,7 +7,7 @@
#include <functional>
#include "exception.hh"
-
+#include "completion.hh"
namespace Kakoune
{
@@ -20,16 +20,6 @@ struct wrong_argument_count : runtime_error
typedef std::vector<std::string> CommandParameters;
typedef std::function<void (const CommandParameters&)> Command;
-struct Completions
-{
- CommandParameters candidates;
- size_t start;
- size_t end;
-
- Completions(size_t start, size_t end)
- : start(start), end(end) {}
-};
-
class CommandManager
{
public:
diff --git a/src/completion.cc b/src/completion.cc
new file mode 100644
index 00000000..a1eed12a
--- /dev/null
+++ b/src/completion.cc
@@ -0,0 +1,34 @@
+#include "completion.hh"
+
+#include "utils.hh"
+
+#include <dirent.h>
+
+namespace Kakoune
+{
+
+CandidateList complete_filename(const std::string& prefix)
+{
+ size_t dir_end = prefix.find_last_of('/');
+ std::string dirname = "./";
+ std::string fileprefix = prefix;
+
+ if (dir_end != std::string::npos)
+ {
+ dirname = prefix.substr(0, dir_end + 1);
+ fileprefix = prefix.substr(dir_end + 1, std::string::npos);
+ }
+
+ auto dir = auto_raii(opendir(dirname.c_str()), closedir);
+
+ CandidateList result;
+ while (dirent* entry = readdir(dir))
+ {
+ std::string filename = entry->d_name;
+ if (filename.substr(0, fileprefix.length()) == fileprefix)
+ result.push_back(filename);
+ }
+ return result;
+}
+
+}
diff --git a/src/completion.hh b/src/completion.hh
new file mode 100644
index 00000000..497fa54f
--- /dev/null
+++ b/src/completion.hh
@@ -0,0 +1,25 @@
+#ifndef completion_hh_INCLUDED
+#define completion_hh_INCLUDED
+
+#include <string>
+#include <vector>
+
+namespace Kakoune
+{
+
+typedef std::vector<std::string> CandidateList;
+
+struct Completions
+{
+ CandidateList candidates;
+ size_t start;
+ size_t end;
+
+ Completions(size_t start, size_t end)
+ : start(start), end(end) {}
+};
+
+CandidateList complete_filename(const std::string& prefix);
+
+}
+#endif // completion_hh_INCLUDED