From a467d7311593dc4a7f6ffe4a4db9254e5ade3b8c Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 12 Sep 2012 19:42:12 +0200 Subject: [PATCH] use parse_filename when opening files or completing filenames so that ~ and env vars are handled --- src/completion.cc | 3 ++- src/file.cc | 9 +++++---- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/src/completion.cc b/src/completion.cc index 2cbc1468..5db7ffb7 100644 --- a/src/completion.cc +++ b/src/completion.cc @@ -2,6 +2,7 @@ #include "buffer_manager.hh" #include "utils.hh" +#include "file.hh" #include #include @@ -13,7 +14,7 @@ CandidateList complete_filename(const Context& context, const String& prefix, CharCount cursor_pos) { - String real_prefix = prefix.substr(0, cursor_pos); + String real_prefix = parse_filename(prefix.substr(0, cursor_pos)); String dirname = "./"; String dirprefix; String fileprefix = real_prefix; diff --git a/src/file.cc b/src/file.cc index 69a7f530..2723893e 100644 --- a/src/file.cc +++ b/src/file.cc @@ -21,7 +21,7 @@ bool isidentifier(char c) String parse_filename(const String& filename) { - if (filename.length() > 2 and filename[0] == '~' and filename[1] == '/') + if (filename.length() >= 2 and filename[0] == '~' and filename[1] == '/') return parse_filename("$HOME/" + filename.substr(2)); CharCount pos = 0; @@ -50,7 +50,7 @@ String parse_filename(const String& filename) String read_file(const String& filename) { - int fd = open(filename.c_str(), O_RDONLY); + int fd = open(parse_filename(filename).c_str(), O_RDONLY); if (fd == -1) { if (errno == ENOENT) @@ -75,7 +75,7 @@ String read_file(const String& filename) Buffer* create_buffer_from_file(const String& filename) { - int fd = open(filename.c_str(), O_RDONLY); + int fd = open(parse_filename(filename).c_str(), O_RDONLY); if (fd == -1) { if (errno == ENOENT) @@ -165,7 +165,8 @@ void write_buffer_to_file(const Buffer& buffer, const String& filename) eolformat = "\n"; auto eoldata = eolformat.data(); - int fd = open(filename.c_str(), O_CREAT | O_WRONLY | O_TRUNC, 0644); + int fd = open(parse_filename(filename).c_str(), + O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd == -1) throw file_access_error(filename, strerror(errno));