Add a -existing switch to edit command

with this switch, fail if the file is not found rather than creating
a new one. Use that in grep and make jump commands.
This commit is contained in:
Maxime Coste 2014-07-31 22:10:01 +01:00
parent bcb0dd451b
commit beb6065a71
3 changed files with 16 additions and 16 deletions

View File

@ -34,7 +34,7 @@ decl str jumpclient
def jump %{ def jump %{
exec 'xs^([^:]+):(\d+):(\d+)?<ret>' exec 'xs^([^:]+):(\d+):(\d+)?<ret>'
set buffer _grep_current_line %val{cursor_line} set buffer _grep_current_line %val{cursor_line}
eval -try-client %opt{jumpclient} edit %reg{1} %reg{2} %reg{3} eval -try-client %opt{jumpclient} edit -existing %reg{1} %reg{2} %reg{3}
try %{ focus %opt{jumpclient} } try %{ focus %opt{jumpclient} }
} }

View File

@ -33,12 +33,12 @@ def errjump -docstring 'Jump to error location' %{
exec gll<a-?> "Entering directory" <ret> exec gll<a-?> "Entering directory" <ret>
exec s "Entering directory '([^']+)'.*\n([^:]+):(\d+):(\d+):([^\n]+)\'" <ret>l exec s "Entering directory '([^']+)'.*\n([^:]+):(\d+):(\d+):([^\n]+)\'" <ret>l
set buffer _make_current_error_line %val{cursor_line} set buffer _make_current_error_line %val{cursor_line}
eval -try-client %opt{jumpclient} %rec{edit %reg{1}/%reg{2} %reg{3} %reg{4}; echo -color Information '%reg{5}'} eval -try-client %opt{jumpclient} %rec{edit -existing %reg{1}/%reg{2} %reg{3} %reg{4}; echo -color Information '%reg{5}'}
try %{ focus %opt{jumpclient} } try %{ focus %opt{jumpclient} }
} catch %{ } catch %{
exec ghgl s "([^:]+):(\d+):(\d+):([^\n]+)\'" <ret>l exec ghgl s "([^:]+):(\d+):(\d+):([^\n]+)\'" <ret>l
set buffer _make_current_error_line %val{cursor_line} set buffer _make_current_error_line %val{cursor_line}
eval -try-client %opt{jumpclient} %rec{edit %reg{1} %reg{2} %reg{3}; echo -color Information '%reg{4}'} eval -try-client %opt{jumpclient} %rec{edit -existing %reg{1} %reg{2} %reg{3}; echo -color Information '%reg{4}'}
try %{ focus %opt{jumpclient} } try %{ focus %opt{jumpclient} }
} }
} }

View File

@ -34,17 +34,6 @@ namespace Kakoune
namespace namespace
{ {
Buffer* open_or_create(const String& filename, Context& context)
{
Buffer* buffer = create_buffer_from_file(filename);
if (not buffer)
{
context.print_status({ "new file " + filename, get_face("StatusLine") });
buffer = new Buffer(filename, Buffer::Flags::File | Buffer::Flags::New);
}
return buffer;
}
Buffer* open_fifo(const String& name , const String& filename, bool scroll) Buffer* open_fifo(const String& name , const String& filename, bool scroll)
{ {
int fd = open(parse_filename(filename).c_str(), O_RDONLY); int fd = open(parse_filename(filename).c_str(), O_RDONLY);
@ -116,7 +105,17 @@ void edit(const ParametersParser& parser, Context& context)
else if (parser.has_option("fifo")) else if (parser.has_option("fifo"))
buffer = open_fifo(name, parser.option_value("fifo"), parser.has_option("scroll")); buffer = open_fifo(name, parser.option_value("fifo"), parser.has_option("scroll"));
else else
buffer = open_or_create(name, context); {
buffer = create_buffer_from_file(name);
if (not buffer)
{
if (parser.has_option("existing"))
throw runtime_error("unable to open " + name);
context.print_status({ "new file " + name, get_face("StatusLine") });
buffer = new Buffer(name, Buffer::Flags::File | Buffer::Flags::New);
}
}
} }
BufferManager::instance().set_last_used_buffer(*buffer); BufferManager::instance().set_last_used_buffer(*buffer);
@ -142,7 +141,8 @@ void edit(const ParametersParser& parser, Context& context)
} }
ParameterDesc edit_params{ ParameterDesc edit_params{
SwitchMap{ { "scratch", { false, "create a scratch buffer, not linked to a file" } }, SwitchMap{ { "existing", { false, "fail if the file does not exists, do not open a new file" } },
{ "scratch", { false, "create a scratch buffer, not linked to a file" } },
{ "fifo", { true, "create a buffer reading its content from a named fifo" } }, { "fifo", { true, "create a buffer reading its content from a named fifo" } },
{ "scroll", { false, "place the initial cursor so that the fifo will scroll to show new data" } } }, { "scroll", { false, "place the initial cursor so that the fifo will scroll to show new data" } } },
ParameterDesc::Flags::None, 0, 3 ParameterDesc::Flags::None, 0, 3