Compare commits

..

No commits in common. "609ddfab22f7cae83ba467878c6ff516a8830ad9" and "e957bba771ea200da16f4d3a02aaec7784c65168" have entirely different histories.

2 changed files with 297 additions and 476 deletions

View File

@ -1,288 +0,0 @@
/*
* line-editor.c: a line editor component for the TUI part of liberty
*
* Copyright (c) 2017 - 2018, Přemysl Janouch <p@janouch.name>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
* SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION
* OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN
* CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*
*/
// This is here just for IDE code model reasons
#ifndef HAVE_LIBERTY
#include "liberty/liberty.c"
#include "liberty/liberty-tui.c"
#endif
static void
row_buffer_append_c (struct row_buffer *self, ucs4_t c, chtype attrs)
{
struct row_char current = { .attrs = attrs, .c = c };
struct row_char invalid = { .attrs = attrs, .c = '?', .width = 1 };
current.width = uc_width (current.c, locale_charset ());
if (current.width < 0 || !app_is_character_in_locale (current.c))
current = invalid;
ARRAY_RESERVE (self->chars, 1);
self->chars[self->chars_len++] = current;
self->total_width += current.width;
}
// --- Line editor -------------------------------------------------------------
enum line_editor_action
{
LINE_EDITOR_B_CHAR, ///< Go back a character
LINE_EDITOR_F_CHAR, ///< Go forward a character
LINE_EDITOR_B_WORD, ///< Go back a word
LINE_EDITOR_F_WORD, ///< Go forward a word
LINE_EDITOR_HOME, ///< Go to start of line
LINE_EDITOR_END, ///< Go to end of line
LINE_EDITOR_B_DELETE, ///< Delete last character
LINE_EDITOR_F_DELETE, ///< Delete next character
LINE_EDITOR_B_KILL_WORD, ///< Delete last word
LINE_EDITOR_B_KILL_LINE, ///< Delete everything up to BOL
LINE_EDITOR_F_KILL_LINE, ///< Delete everything up to EOL
};
struct line_editor
{
int point; ///< Caret index into line data
ucs4_t *line; ///< Line data, 0-terminated
int *w; ///< Codepoint widths, 0-terminated
size_t len; ///< Editor length
size_t alloc; ///< Editor allocated
char prompt; ///< Prompt character
void (*on_changed) (void); ///< Callback on text change
void (*on_end) (bool); ///< Callback on abort
};
static void
line_editor_free (struct line_editor *self)
{
free (self->line);
free (self->w);
}
/// Notify whomever invoked the editor that it's been either confirmed or
/// cancelled and clean up editor state
static void
line_editor_abort (struct line_editor *self, bool status)
{
self->on_end (status);
self->on_changed = NULL;
free (self->line);
self->line = NULL;
free (self->w);
self->w = NULL;
self->alloc = 0;
self->len = 0;
self->point = 0;
self->prompt = 0;
}
/// Start the line editor; remember to fill in "change" and "end" callbacks
static void
line_editor_start (struct line_editor *self, char prompt)
{
self->alloc = 16;
self->line = xcalloc (sizeof *self->line, self->alloc);
self->w = xcalloc (sizeof *self->w, self->alloc);
self->len = 0;
self->point = 0;
self->prompt = prompt;
}
static void
line_editor_changed (struct line_editor *self)
{
self->line[self->len] = 0;
self->w[self->len] = 0;
if (self->on_changed)
self->on_changed ();
}
static void
line_editor_move (struct line_editor *self, int to, int from, int len)
{
memmove (self->line + to, self->line + from,
sizeof *self->line * len);
memmove (self->w + to, self->w + from,
sizeof *self->w * len);
}
static void
line_editor_insert (struct line_editor *self, ucs4_t codepoint)
{
while (self->alloc - self->len < 2 /* inserted + sentinel */)
{
self->alloc <<= 1;
self->line = xreallocarray
(self->line, sizeof *self->line, self->alloc);
self->w = xreallocarray
(self->w, sizeof *self->w, self->alloc);
}
line_editor_move (self, self->point + 1, self->point,
self->len - self->point);
self->line[self->point] = codepoint;
self->w[self->point] = app_is_character_in_locale (codepoint)
? uc_width (codepoint, locale_charset ())
: 1 /* the replacement question mark */;
self->point++;
self->len++;
line_editor_changed (self);
}
static bool
line_editor_action (struct line_editor *self, enum line_editor_action action)
{
switch (action)
{
default:
return soft_assert (!"unknown line editor action");
case LINE_EDITOR_B_CHAR:
if (self->point < 1)
return false;
do self->point--;
while (self->point > 0
&& !self->w[self->point]);
return true;
case LINE_EDITOR_F_CHAR:
if (self->point + 1 > (int) self->len)
return false;
do self->point++;
while (self->point < (int) self->len
&& !self->w[self->point]);
return true;
case LINE_EDITOR_B_WORD:
{
if (self->point < 1)
return false;
int i = self->point;
while (i && self->line[--i] == ' ');
while (i-- && self->line[i] != ' ');
self->point = ++i;
return true;
}
case LINE_EDITOR_F_WORD:
{
if (self->point + 1 > (int) self->len)
return false;
int i = self->point;
while (i < (int) self->len && self->line[i] != ' ') i++;
while (i < (int) self->len && self->line[i] == ' ') i++;
self->point = i;
return true;
}
case LINE_EDITOR_HOME:
self->point = 0;
return true;
case LINE_EDITOR_END:
self->point = self->len;
return true;
case LINE_EDITOR_B_DELETE:
{
if (self->point < 1)
return false;
int len = 1;
while (self->point - len > 0
&& !self->w[self->point - len])
len++;
line_editor_move (self, self->point - len, self->point,
self->len - self->point);
self->len -= len;
self->point -= len;
line_editor_changed (self);
return true;
}
case LINE_EDITOR_F_DELETE:
{
if (self->point + 1 > (int) self->len)
return false;
int len = 1;
while (self->point + len < (int) self->len
&& !self->w[self->point + len])
len++;
self->len -= len;
line_editor_move (self, self->point, self->point + len,
self->len - self->point);
line_editor_changed (self);
return true;
}
case LINE_EDITOR_B_KILL_WORD:
{
if (self->point < 1)
return false;
int i = self->point;
while (i && self->line[--i] == ' ');
while (i-- && self->line[i] != ' ');
i++;
line_editor_move (self, i, self->point, (self->len - self->point));
self->len -= self->point - i;
self->point = i;
line_editor_changed (self);
return true;
}
case LINE_EDITOR_B_KILL_LINE:
self->len -= self->point;
line_editor_move (self, 0, self->point, self->len);
self->point = 0;
line_editor_changed (self);
return true;
case LINE_EDITOR_F_KILL_LINE:
self->len = self->point;
line_editor_changed (self);
return true;
}
}
static int
line_editor_write (const struct line_editor *self, struct row_buffer *row,
int width, chtype attrs)
{
if (self->prompt)
{
hard_assert (self->prompt < 127);
row_buffer_append_c (row, self->prompt, attrs);
width--;
}
int following = 0;
for (size_t i = self->point; i < self->len; i++)
following += self->w[i];
int preceding = 0;
size_t start = self->point;
while (start && preceding < width / 2)
preceding += self->w[--start];
// There can be one extra space at the end of the line but this way we
// don't need to care about non-spacing marks following full-width chars
while (start && width - preceding - following > 2 /* widest char */)
preceding += self->w[--start];
// XXX: we should also show < > indicators for overflow but it'd probably
// considerably complicate this algorithm
for (; start < self->len; start++)
row_buffer_append_c (row, self->line[start], attrs);
return !!self->prompt + preceding;
}

441
nncmpp.c
View File

@ -35,10 +35,6 @@
XX( ODD, "odd", -1, -1, 0 ) \
XX( DIRECTORY, "directory", -1, -1, 0 ) \
XX( SELECTION, "selection", -1, -1, A_REVERSE ) \
/* Cyan is good with both black and white.
* Can't use A_REVERSE because bold'd be bright.
* Unfortunately ran out of B&W attributes. */ \
XX( MULTISELECT, "multiselect",-1, 6, 0 ) \
XX( SCROLLBAR, "scrollbar", -1, -1, 0 ) \
/* These are for debugging only */ \
XX( WARNING, "warning", 3, -1, 0 ) \
@ -70,9 +66,6 @@ enum
#include "liberty/liberty.c"
#include "liberty/liberty-tui.c"
#define HAVE_LIBERTY
#include "line-editor.c"
#include <math.h>
#include <locale.h>
#include <termios.h>
@ -540,6 +533,7 @@ item_list_resize (struct item_list *self, size_t len)
// For simplicity, the listview can only work with items that are one row high.
struct tab;
struct row_buffer;
enum action;
/// Try to handle an action in the tab
@ -572,7 +566,6 @@ struct tab
int item_top; ///< Index of the topmost item
int item_selected; ///< Index of the selected item
int item_mark; ///< Multiselect second point index
};
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
@ -637,9 +630,19 @@ static struct app_context
int gauge_offset; ///< Offset to the gauge or -1
int gauge_width; ///< Width of the gauge, if present
struct line_editor editor; ///< Line editor
struct poller_idle refresh_event; ///< Refresh the screen
// Line editor:
int editor_point; ///< Caret index into line data
ucs4_t *editor_line; ///< Line data, 0-terminated
int *editor_w; ///< Codepoint widths, 0-terminated
size_t editor_len; ///< Editor length
size_t editor_alloc; ///< Editor allocated
char editor_prompt; ///< Prompt character
void (*on_editor_changed) (void); ///< Callback on text change
void (*on_editor_end) (bool); ///< Callback on abort
// Terminal:
termo_t *tk; ///< termo handle
@ -667,7 +670,6 @@ tab_init (struct tab *self, const char *name)
// and we'd need to filter it first to replace invalid chars with '?'
self->name_width = u8_strwidth ((uint8_t *) self->name, locale_charset ());
self->item_selected = 0;
self->item_mark = -1;
}
static void
@ -676,17 +678,6 @@ tab_free (struct tab *self)
free (self->name);
}
static struct tab_range { int from, upto; }
tab_selection_range (struct tab *self)
{
if (self->item_selected < 0 || !self->item_count)
return (struct tab_range) { -1, -1 };
if (self->item_mark < 0)
return (struct tab_range) { self->item_selected, self->item_selected };
return (struct tab_range) { MIN (self->item_selected, self->item_mark),
MAX (self->item_selected, self->item_mark) };
}
// --- Configuration -----------------------------------------------------------
static struct config_schema g_config_settings[] =
@ -895,7 +886,8 @@ app_free_context (void)
strv_free (&g.streams);
item_list_free (&g.playlist);
line_editor_free (&g.editor);
free (g.editor_line);
free (g.editor_w);
config_free (&g.config);
poller_free (&g.poller);
@ -1279,16 +1271,8 @@ app_draw_view (void)
{
int item_index = tab->item_top + row;
int row_attrs = (item_index & 1) ? APP_ATTR (ODD) : APP_ATTR (EVEN);
bool override_colors = true;
if (item_index == tab->item_selected)
row_attrs = APP_ATTR (SELECTION);
else if (tab->item_mark > -1 &&
((item_index >= tab->item_mark && item_index <= tab->item_selected)
|| (item_index >= tab->item_selected && item_index <= tab->item_mark)))
row_attrs = APP_ATTR (MULTISELECT);
else
override_colors = false;
struct row_buffer buf = row_buffer_make ();
tab->on_item_draw (item_index, &buf, view_width);
@ -1298,7 +1282,7 @@ app_draw_view (void)
for (size_t i = 0; i < buf.chars_len; i++)
{
chtype *attrs = &buf.chars[i].attrs;
if (override_colors)
if (item_index == tab->item_selected)
*attrs = (*attrs & ~(A_COLOR | A_REVERSE)) | row_attrs;
else if ((*attrs & A_COLOR) && (row_attrs & A_COLOR))
*attrs |= (row_attrs & ~A_COLOR);
@ -1347,15 +1331,7 @@ static void
app_write_mpd_status (struct row_buffer *buf)
{
struct str_map *map = &g.playback_info;
if (g.active_tab->item_mark > -1)
{
struct tab_range r = tab_selection_range (g.active_tab);
char *msg = xstrdup_printf (r.from == r.upto
? "Selected %d item" : "Selected %d items", r.upto - r.from + 1);
row_buffer_append (buf, msg, APP_ATTR (HIGHLIGHT));
free (msg);
}
else if (str_map_find (map, "updating_db"))
if (str_map_find (map, "updating_db"))
row_buffer_append (buf, "Updating database...", APP_ATTR (NORMAL));
else
app_write_mpd_status_playlist (buf);
@ -1385,6 +1361,53 @@ app_write_mpd_status (struct row_buffer *buf)
row_buffer_free (&right);
}
static void
row_buffer_append_c (struct row_buffer *self, ucs4_t c, chtype attrs)
{
struct row_char current = { .attrs = attrs, .c = c };
struct row_char invalid = { .attrs = attrs, .c = '?', .width = 1 };
current.width = uc_width (current.c, locale_charset ());
if (current.width < 0 || !app_is_character_in_locale (current.c))
current = invalid;
ARRAY_RESERVE (self->chars, 1);
self->chars[self->chars_len++] = current;
self->total_width += current.width;
}
static int
app_write_editor (struct row_buffer *row)
{
int limit = COLS;
if (g.editor_prompt)
{
hard_assert (g.editor_prompt < 127);
row_buffer_append_c (row, g.editor_prompt, APP_ATTR (HIGHLIGHT));
limit--;
}
int following = 0;
for (size_t i = g.editor_point; i < g.editor_len; i++)
following += g.editor_w[i];
int preceding = 0;
size_t start = g.editor_point;
while (start && preceding < limit / 2)
preceding += g.editor_w[--start];
// There can be one extra space at the end of the line but this way we
// don't need to care about non-spacing marks following full-width chars
while (start && limit - preceding - following > 2 /* widest char */)
preceding += g.editor_w[--start];
// XXX: we should also show < > indicators for overflow but it'd probably
// considerably complicate this algorithm
for (; start < g.editor_len; start++)
row_buffer_append_c (row, g.editor_line[start], APP_ATTR (HIGHLIGHT));
return !!g.editor_prompt + preceding;
}
static void
app_draw_statusbar (void)
{
@ -1393,8 +1416,8 @@ app_draw_statusbar (void)
struct row_buffer buf = row_buffer_make ();
if (g.message)
row_buffer_append (&buf, g.message, APP_ATTR (HIGHLIGHT));
else if (g.editor.line)
caret = line_editor_write (&g.editor, &buf, COLS, APP_ATTR (HIGHLIGHT));
else if (g.editor_line)
caret = app_write_editor (&buf);
else if (g.client.state == MPD_CONNECTED)
app_write_mpd_status (&buf);
@ -1553,7 +1576,6 @@ app_goto_tab (int tab_index)
XX( CHOOSE, "Choose item" ) \
XX( DELETE, "Delete item" ) \
XX( UP, "Go up a level" ) \
XX( MULTISELECT, "Toggle multiselect" ) \
\
XX( SCROLL_UP, "Scroll up" ) \
XX( SCROLL_DOWN, "Scroll down" ) \
@ -1623,6 +1645,199 @@ action_resolve (const char *name)
return -1;
}
// --- Line editor -------------------------------------------------------------
// TODO: move the editor out as a component to liberty-tui.c
/// Notify whomever invoked the editor that it's been either confirmed or
/// cancelled and clean up editor state
static void
app_editor_abort (bool status)
{
g.on_editor_end (status);
g.on_editor_changed = NULL;
g.on_editor_end = NULL;
free (g.editor_line);
g.editor_line = NULL;
free (g.editor_w);
g.editor_w = NULL;
g.editor_alloc = 0;
g.editor_len = 0;
g.editor_point = 0;
g.editor_prompt = 0;
}
/// Start the line editor; remember to fill in "change" and "abort" callbacks
static void
app_editor_start (char prompt)
{
g.editor_alloc = 16;
g.editor_line = xcalloc (sizeof *g.editor_line, g.editor_alloc);
g.editor_w = xcalloc (sizeof *g.editor_w, g.editor_alloc);
g.editor_len = 0;
g.editor_point = 0;
g.editor_prompt = prompt;
app_invalidate ();
}
static void
app_editor_changed (void)
{
g.editor_line[g.editor_len] = 0;
g.editor_w[g.editor_len] = 0;
if (g.on_editor_changed)
g.on_editor_changed ();
}
static void
app_editor_move (int to, int from, int len)
{
memmove (g.editor_line + to, g.editor_line + from,
sizeof *g.editor_line * len);
memmove (g.editor_w + to, g.editor_w + from,
sizeof *g.editor_w * len);
}
static bool
app_editor_process_action (enum action action)
{
app_invalidate ();
switch (action)
{
case ACTION_QUIT:
app_editor_abort (false);
return true;
case ACTION_EDITOR_CONFIRM:
app_editor_abort (true);
return true;
default:
return false;
case ACTION_EDITOR_B_CHAR:
if (g.editor_point < 1)
return false;
do g.editor_point--;
while (g.editor_point > 0
&& !g.editor_w[g.editor_point]);
return true;
case ACTION_EDITOR_F_CHAR:
if (g.editor_point + 1 > (int) g.editor_len)
return false;
do g.editor_point++;
while (g.editor_point < (int) g.editor_len
&& !g.editor_w[g.editor_point]);
return true;
case ACTION_EDITOR_B_WORD:
{
if (g.editor_point < 1)
return false;
int i = g.editor_point;
while (i && g.editor_line[--i] == ' ');
while (i-- && g.editor_line[i] != ' ');
g.editor_point = ++i;
return true;
}
case ACTION_EDITOR_F_WORD:
{
if (g.editor_point + 1 > (int) g.editor_len)
return false;
int i = g.editor_point;
while (i < (int) g.editor_len && g.editor_line[i] != ' ') i++;
while (i < (int) g.editor_len && g.editor_line[i] == ' ') i++;
g.editor_point = i;
return true;
}
case ACTION_EDITOR_HOME:
g.editor_point = 0;
return true;
case ACTION_EDITOR_END:
g.editor_point = g.editor_len;
return true;
case ACTION_EDITOR_B_DELETE:
{
if (g.editor_point < 1)
return false;
int len = 1;
while (g.editor_point - len > 0
&& !g.editor_w[g.editor_point - len])
len++;
app_editor_move (g.editor_point - len, g.editor_point,
g.editor_len - g.editor_point);
g.editor_len -= len;
g.editor_point -= len;
app_editor_changed ();
return true;
}
case ACTION_EDITOR_F_DELETE:
{
if (g.editor_point + 1 > (int) g.editor_len)
return false;
int len = 1;
while (g.editor_point + len < (int) g.editor_len
&& !g.editor_w[g.editor_point + len])
len++;
g.editor_len -= len;
app_editor_move (g.editor_point, g.editor_point + len,
g.editor_len - g.editor_point);
app_editor_changed ();
return true;
}
case ACTION_EDITOR_B_KILL_WORD:
{
if (g.editor_point < 1)
return false;
int i = g.editor_point;
while (i && g.editor_line[--i] == ' ');
while (i-- && g.editor_line[i] != ' ');
i++;
app_editor_move (i, g.editor_point, (g.editor_len - g.editor_point));
g.editor_len -= g.editor_point - i;
g.editor_point = i;
app_editor_changed ();
return true;
}
case ACTION_EDITOR_B_KILL_LINE:
g.editor_len -= g.editor_point;
app_editor_move (0, g.editor_point, g.editor_len);
g.editor_point = 0;
app_editor_changed ();
return true;
case ACTION_EDITOR_F_KILL_LINE:
g.editor_len = g.editor_point;
app_editor_changed ();
return true;
}
}
static void
app_editor_insert (ucs4_t codepoint)
{
while (g.editor_alloc - g.editor_len < 2 /* inserted + sentinel */)
{
g.editor_alloc <<= 1;
g.editor_line = xreallocarray
(g.editor_line, sizeof *g.editor_line, g.editor_alloc);
g.editor_w = xreallocarray
(g.editor_w, sizeof *g.editor_w, g.editor_alloc);
}
app_editor_move (g.editor_point + 1, g.editor_point,
g.editor_len - g.editor_point);
g.editor_line[g.editor_point] = codepoint;
g.editor_w[g.editor_point] = app_is_character_in_locale (codepoint)
? uc_width (codepoint, locale_charset ())
: 1 /* the replacement question mark */;
g.editor_point++;
g.editor_len++;
app_editor_changed ();
}
// --- User input handling -----------------------------------------------------
static void
@ -1687,7 +1902,7 @@ app_on_editor_end (bool confirmed)
return;
size_t len;
char *u8 = (char *) u32_to_u8 (g.editor.line, g.editor.len + 1, NULL, &len);
char *u8 = (char *) u32_to_u8 (g.editor_line, g.editor_len + 1, NULL, &len);
mpd_client_send_command_raw (c, u8);
free (u8);
@ -1703,25 +1918,13 @@ app_process_action (enum action action)
// First let the tab try to handle this
struct tab *tab = g.active_tab;
if (tab->on_action && tab->on_action (action))
{
tab->item_mark = -1;
app_invalidate ();
return true;
}
switch (action)
{
case ACTION_NONE:
return true;
case ACTION_QUIT:
// It is a pseudomode, avoid surprising the user
if (tab->item_mark > -1)
{
tab->item_mark = -1;
app_invalidate ();
return true;
}
app_quit ();
return true;
case ACTION_REDRAW:
@ -1729,25 +1932,12 @@ app_process_action (enum action action)
app_invalidate ();
return true;
case ACTION_MPD_COMMAND:
line_editor_start (&g.editor, ':');
g.editor.on_end = app_on_editor_end;
app_invalidate ();
app_editor_start (':');
g.on_editor_end = app_on_editor_end;
return true;
default:
return false;
case ACTION_MULTISELECT:
if (!tab->can_multiselect
|| !tab->item_count || tab->item_selected < 0)
return false;
app_invalidate ();
if (tab->item_mark > -1)
tab->item_mark = -1;
else
tab->item_mark = tab->item_selected;
return true;
case ACTION_LAST_TAB:
if (!g.last_tab)
return false;
@ -1816,49 +2006,6 @@ app_process_action (enum action action)
return false;
}
static bool
app_editor_process_action (enum action action)
{
app_invalidate ();
switch (action)
{
case ACTION_QUIT:
line_editor_abort (&g.editor, false);
g.editor.on_end = NULL;
return true;
case ACTION_EDITOR_CONFIRM:
line_editor_abort (&g.editor, true);
g.editor.on_end = NULL;
return true;
default:
return false;
case ACTION_EDITOR_B_CHAR:
return line_editor_action (&g.editor, LINE_EDITOR_B_CHAR);
case ACTION_EDITOR_F_CHAR:
return line_editor_action (&g.editor, LINE_EDITOR_F_CHAR);
case ACTION_EDITOR_B_WORD:
return line_editor_action (&g.editor, LINE_EDITOR_B_WORD);
case ACTION_EDITOR_F_WORD:
return line_editor_action (&g.editor, LINE_EDITOR_F_WORD);
case ACTION_EDITOR_HOME:
return line_editor_action (&g.editor, LINE_EDITOR_HOME);
case ACTION_EDITOR_END:
return line_editor_action (&g.editor, LINE_EDITOR_END);
case ACTION_EDITOR_B_DELETE:
return line_editor_action (&g.editor, LINE_EDITOR_B_DELETE);
case ACTION_EDITOR_F_DELETE:
return line_editor_action (&g.editor, LINE_EDITOR_F_DELETE);
case ACTION_EDITOR_B_KILL_WORD:
return line_editor_action (&g.editor, LINE_EDITOR_B_KILL_WORD);
case ACTION_EDITOR_B_KILL_LINE:
return line_editor_action (&g.editor, LINE_EDITOR_B_KILL_LINE);
case ACTION_EDITOR_F_KILL_LINE:
return line_editor_action (&g.editor, LINE_EDITOR_F_KILL_LINE);
}
}
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
static bool
@ -1939,8 +2086,8 @@ app_process_mouse (termo_mouse_event_t type, int line, int column, int button,
if (type != TERMO_MOUSE_PRESS)
return true;
if (g.editor.line)
line_editor_abort (&g.editor, false);
if (g.editor_line)
app_editor_abort (false);
if (button == 1)
return app_process_left_mouse_click (line, column, double_click);
@ -2000,7 +2147,6 @@ g_normal_defaults[] =
{ "Enter", ACTION_CHOOSE },
{ "Delete", ACTION_DELETE },
{ "Backspace", ACTION_UP },
{ "v", ACTION_MULTISELECT },
{ "a", ACTION_MPD_ADD },
{ "r", ACTION_MPD_REPLACE },
{ ":", ACTION_MPD_COMMAND },
@ -2124,7 +2270,7 @@ static bool
app_process_termo_event (termo_key_t *event)
{
struct binding dummy = { *event, 0, 0 }, *binding;
if (g.editor.line)
if (g.editor_line)
{
if ((binding = bsearch (&dummy, g_editor_keys, g_editor_keys_len,
sizeof *binding, app_binding_cmp)))
@ -2132,7 +2278,7 @@ app_process_termo_event (termo_key_t *event)
if (event->type != TERMO_TYPE_KEY || event->modifiers != 0)
return false;
line_editor_insert (&g.editor, event->code.codepoint);
app_editor_insert (event->code.codepoint);
app_invalidate ();
return true;
}
@ -2199,8 +2345,6 @@ current_tab_move_song (const char *id, int diff)
char *target_str = xstrdup_printf ("%d", target);
mpd_client_send_command (c, "moveid", id, target_str, NULL);
free (target_str);
// TODO: we should create a cancellable action waiting for the move to
// finish, so that holding Shift-arrows works as expected.
mpd_client_add_task (c, mpd_on_simple_response, NULL);
mpd_client_idle (c, 0);
@ -2245,7 +2389,6 @@ current_tab_init (void)
{
struct tab *super = &g_current_tab;
tab_init (super, "Current");
// TODO: implement multiselect, set can_multiselect to true
super->on_action = current_tab_on_action;
super->on_item_draw = current_tab_on_item_draw;
return super;
@ -2471,9 +2614,6 @@ library_tab_on_data (const struct mpd_response *response,
(int (*) (const void *, const void *)) library_tab_compare);
g_library_tab.super.item_count = items->len;
// XXX: this unmarks even if just the database updates
g_library_tab.super.item_mark = -1;
// Don't force the selection visible when there's no need to touch it
if (g_library_tab.super.item_selected >= (int) items->len)
app_move_selection (0);
@ -2494,37 +2634,22 @@ library_tab_reload (const char *new_path)
mpd_client_idle (c, 0);
}
static bool
library_tab_is_range_playable (struct tab_range range)
{
for (int i = range.from; i <= range.upto; i++)
{
struct library_tab_item x =
library_tab_resolve (g_library_tab.items.vector[i]);
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
return true;
}
return false;
}
static bool
library_tab_on_action (enum action action)
{
struct tab *self = g.active_tab;
if (self->item_selected < 0 || !self->item_count)
return false;
struct mpd_client *c = &g.client;
struct tab_range range = tab_selection_range (&g_library_tab.super);
if (range.from < 0 || c->state != MPD_CONNECTED)
if (c->state != MPD_CONNECTED)
return false;
struct library_tab_item x =
library_tab_resolve (g_library_tab.items.vector[range.from]);
library_tab_resolve (g_library_tab.items.vector[self->item_selected]);
switch (action)
{
case ACTION_CHOOSE:
// I can't think of a reasonable way of handling that
if (range.from != range.upto)
break;
switch (x.type)
{
case LIBRARY_ROOT:
@ -2545,41 +2670,26 @@ library_tab_on_action (enum action action)
return parent != NULL;
}
case ACTION_MPD_REPLACE:
if (!library_tab_is_range_playable (range))
if (x.type != LIBRARY_DIR && x.type != LIBRARY_FILE)
break;
// Clears the playlist (which stops playback), add what user wanted
// to replace it with, and eventually restore playback;
// I can't think of a reliable alternative that omits the "play"
mpd_client_list_begin (c);
mpd_client_send_command (c, "clear", NULL);
for (int i = range.from; i <= range.upto; i++)
{
struct library_tab_item x =
library_tab_resolve (g_library_tab.items.vector[i]);
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
mpd_client_send_command (c, "add", x.path, NULL);
}
if (g.state == PLAYER_PLAYING)
mpd_client_send_command (c, "play", NULL);
mpd_client_list_end (c);
mpd_client_add_task (c, mpd_on_simple_response, NULL);
mpd_client_idle (c, 0);
return true;
case ACTION_MPD_ADD:
if (!library_tab_is_range_playable (range))
if (x.type != LIBRARY_DIR && x.type != LIBRARY_FILE)
break;
for (int i = range.from; i <= range.upto; i++)
{
struct library_tab_item x =
library_tab_resolve (g_library_tab.items.vector[i]);
if (x.type == LIBRARY_DIR || x.type == LIBRARY_FILE)
MPD_SIMPLE ("add", x.path);
}
return true;
return MPD_SIMPLE ("add", x.path);
default:
break;
}
@ -2594,7 +2704,6 @@ library_tab_init (void)
struct tab *super = &g_library_tab.super;
tab_init (super, "Library");
super->can_multiselect = true;
super->on_action = library_tab_on_action;
super->on_item_draw = library_tab_on_item_draw;
return super;
@ -3521,7 +3630,7 @@ app_on_key_timer (void *user_data)
termo_key_t event;
if (termo_getkey_force (g.tk, &event) == TERMO_RES_KEY)
if (!app_process_termo_event (&event))
beep ();
app_quit ();
}
static void