Skip to content

Commit

Permalink
Use WithCoord parameter parsing for argv
Browse files Browse the repository at this point in the history
This moves the parameter parsing for the `+<line>:<column>` switch
of the commandline to the `ParametersParser`. The position switch now
respects the `--` separator between switches and files.

The manpage does not document the old behaviour of `+:[column]`.
`+:[column]` works exactly like `+`. This is compatible with the
previously documented behaviour for `+:`.

The help message is does not mention that omitting the line or column
moves the cursor to the last line or column. The explanation in the
manpage should suffice.
  • Loading branch information
pjungkamp committed Jul 1, 2024
1 parent 0cdb088 commit 5b888e5
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 42 deletions.
9 changes: 5 additions & 4 deletions doc/kak.1
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
.Op Fl ui Ar ui_type
.Op Fl e Ar command
.Op Fl E Ar command
.Op Sy + Ns Ar line Ns Oo Sy \&: Ns Ar column Oc | Sy +:
.Op Sy + Ns Oo Ns Ar line Oc | Sy + Ns Ar line Ns Sy \&: Ns Oo Ns Ar column Oc
.Op Fl Fl
.Op Ar file ...
.
.Nm
Expand Down Expand Up @@ -151,10 +152,10 @@ Begin in
.Em readonly mode ,
all the buffers opened will not be written to disk.
.
.It Sy + Ns Ar line Ns Oo Sy \&: Ns Ar column Oc | Sy +:
.It Sy + Ns Oo Ns Ar line Oc | Sy + Ns Ar line Ns Sy \&: Ns Oo Ns Ar column Oc
Specify a target line and column for the first file.
When the plus sign is followed by only a colon, then the cursor is sent
to the last line of the file.
When the line is omitted the cursor will go to the end of the buffer.
When colon is present but the column is omitted the cursor will got to the end of the given line.
.
.It Ar file ...
One or more
Expand Down
48 changes: 10 additions & 38 deletions src/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -1102,18 +1102,17 @@ int main(int argc, char* argv[])
{ "debug", { ArgCompleter{}, "initial debug option value" } },
{ "version", { {}, "display kakoune version and exit" } },
{ "ro", { {}, "readonly mode" } },
{ "help", { {}, "display a help message and quit" } } }
{ "help", { {}, "display a help message and quit" } } },
ParameterDesc::Flags::WithCoord
};

try
{
auto show_usage = [&]() {
write_stdout(format("Usage: {} [options] [--] [file]... [+<line>[:<col>]|+:]\n\n"
write_stdout(format("Usage: {} [options] [+[line] | +line:[column]] [--] [file]...\n\n"
"Options:\n"
"{}\n"
"Prefixing a positional argument with a plus (`+`) sign will place the\n"
"cursor at a given set of coordinates, or the end of the buffer if the plus\n"
"sign is followed only by a colon (`:`)\n",
"You can specify the initial position of the cursor using +[line] or +line:[column].\n",
argv[0], generate_switches_doc(param_desc.switches)));
return 0;
};
Expand Down Expand Up @@ -1182,32 +1181,8 @@ int main(int argc, char* argv[])
parser.get_switch("i").value_or(StringView{}));
}

Vector<StringView> files;
Optional<BufferCoord> init_coord;
for (auto& name : parser)
{
if (not name.empty() and name[0_byte] == '+')
{
if (name == "+" or name == "+:")
{
client_init = client_init + "; exec gj";
continue;
}
auto colon = find(name, ':');
if (auto line = str_to_int_ifp({name.begin()+1, colon}))
{
init_coord = std::max<BufferCoord>({0,0}, {
*line - 1,
colon != name.end() ?
str_to_int_ifp({colon+1, name.end()}).value_or(1) - 1
: 0
});
continue;
}
}

files.emplace_back(name);
}
auto init_coord = parser.get_coord();
auto files = parser | gather<Vector<StringView>>();

if (auto server_session = parser.get_switch("c"))
{
Expand All @@ -1219,14 +1194,11 @@ int main(int argc, char* argv[])
return -1;
}
}

String new_files;
for (auto name : files) {
new_files += format("edit '{}'", escape(real_path(name), "'", '\''));
if (init_coord) {
new_files += format(" {} {}", init_coord->line + 1, init_coord->column + 1);
init_coord.reset();
}
new_files += ";";
for (auto file : files)
{
new_files += format("edit -- '{}'\n", escape(real_path(file), "'", '\''));
}

return run_client(*server_session, {}, new_files + client_init, init_coord, ui_type, false);
Expand Down

0 comments on commit 5b888e5

Please sign in to comment.