Use main selection index as default when accessing only one register value

Fixes #117
This commit is contained in:
Maxime Coste 2014-06-21 11:31:08 +01:00
parent e202b7af50
commit 7235180614
7 changed files with 24 additions and 12 deletions

View File

@ -316,7 +316,7 @@ String eval_token(const Token& token, Context& context,
return ShellManager::instance().eval(content, context, shell_params,
env_vars);
case Token::Type::RegisterExpand:
return RegisterManager::instance()[content].values(context)[0];
return context.main_sel_register_value(content);
case Token::Type::OptionExpand:
return context.options()[content].get_as_string();
case Token::Type::ValExpand:

View File

@ -2,6 +2,7 @@
#include "client.hh"
#include "user_interface.hh"
#include "register_manager.hh"
#include "window.hh"
namespace Kakoune
@ -226,4 +227,13 @@ void Context::end_edition()
--m_edition_level;
}
StringView Context::main_sel_register_value(StringView reg) const
{
auto strings = RegisterManager::instance()[reg].values(*this);
size_t index = m_selections ? (*m_selections).main_index() : 0;
if (strings.size() < index)
index = strings.size() - 1;
return strings[index];
}
}

View File

@ -64,6 +64,8 @@ public:
void print_status(DisplayLine status) const;
StringView main_sel_register_value(StringView reg) const;
void push_jump();
const SelectionList& jump_forward();
const SelectionList& jump_backward();

View File

@ -358,10 +358,10 @@ HighlighterAndId highlight_search_factory(HighlighterParameters params)
return ColorSpec{ { 0, &Kakoune::get_color("Search") } };
};
auto get_regex = [](const Context&){
auto s = RegisterManager::instance()['/'].values(Context{});
auto s = Context().main_sel_register_value("/");
try
{
return s.empty() ? Regex{} : Regex{s[0].begin(), s[0].end()};
return s.empty() ? Regex{} : Regex{s.begin(), s.end()};
}
catch (boost::regex_error& err)
{

View File

@ -374,7 +374,7 @@ public:
if (m_mode == Mode::InsertReg)
{
String reg = RegisterManager::instance()[key.key].values(context())[0];
StringView reg = context().main_sel_register_value(String{key.key});
m_line_editor.insert(reg);
m_mode = Mode::Default;
}

View File

@ -106,7 +106,7 @@ void register_env_vars()
}, {
"reg_.+",
[](StringView name, const Context& context) -> String
{ return RegisterManager::instance()[name.substr(4_byte)].values(context)[0]; }
{ return context.main_sel_register_value(name.substr(4_byte)); }
}, {
"client_env_.+",
[](StringView name, const Context& context) -> String

View File

@ -384,9 +384,9 @@ void pipe(Context& context, int)
if (event != PromptEvent::Validate)
return;
String real_cmd;
StringView real_cmd;
if (cmdline.empty())
real_cmd = RegisterManager::instance()['|'].values(context)[0];
real_cmd = context.main_sel_register_value("|");
else
{
RegisterManager::instance()['|'] = cmdline;
@ -575,7 +575,7 @@ void search(Context& context, int)
regex_prompt(context, direction == Forward ? "search:" : "reverse search:",
[](Regex ex, Context& context) {
if (ex.empty())
ex = Regex{RegisterManager::instance()['/'].values(context)[0]};
ex = Regex{context.main_sel_register_value("/").str()};
else
RegisterManager::instance()['/'] = String{ex.str()};
if (not ex.empty() and not ex.str().empty())
@ -586,12 +586,12 @@ void search(Context& context, int)
template<SelectMode mode, Direction direction>
void search_next(Context& context, int param)
{
const String& str = RegisterManager::instance()['/'].values(context)[0];
StringView str = context.main_sel_register_value("/");
if (not str.empty())
{
try
{
Regex ex{str};
Regex ex{str.begin(), str.end()};
do {
select_next_match<direction, mode>(context.buffer(), context.selections(), ex);
} while (--param > 0);
@ -632,7 +632,7 @@ void select_regex(Context& context, int)
{
regex_prompt(context, "select:", [](Regex ex, Context& context) {
if (ex.empty())
ex = Regex{RegisterManager::instance()['/'].values(context)[0]};
ex = Regex{context.main_sel_register_value("/").str()};
else
RegisterManager::instance()['/'] = String{ex.str()};
if (not ex.empty() and not ex.str().empty())
@ -644,7 +644,7 @@ void split_regex(Context& context, int)
{
regex_prompt(context, "split:", [](Regex ex, Context& context) {
if (ex.empty())
ex = Regex{RegisterManager::instance()['/'].values(context)[0]};
ex = Regex{context.main_sel_register_value("/").str()};
else
RegisterManager::instance()['/'] = String{ex.str()};
if (not ex.empty() and not ex.str().empty())