Skip to content

Commit

Permalink
Implement option to ignore pasted text in normal mode (cmus#1270)
Browse files Browse the repository at this point in the history
  • Loading branch information
pgaskin committed Jul 16, 2023
1 parent e4a3e60 commit b88b8fa
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 2 deletions.
4 changes: 4 additions & 0 deletions Doc/cmus.txt
Original file line number Diff line number Diff line change
Expand Up @@ -888,6 +888,10 @@ altformat_trackwin [`Format String`]

Note: if empty, *format_trackwin* is used instead.

block_key_paste (true)
Prevent accidental input by only accepting pasted text in the command line.
Only works on terminals which support bracketed paste.

buffer_seconds (10) [1-300]
Size of the player buffer in seconds.

Expand Down
17 changes: 17 additions & 0 deletions options.c
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,7 @@ int stop_after_queue = 0;
int tree_width_percent = 33;
int tree_width_max = 0;
int pause_on_output_change = 0;
int block_key_paste = 1;

int colors[NR_COLORS] = {
-1,
Expand Down Expand Up @@ -1318,6 +1319,21 @@ static void toggle_pause_on_output_change(void *data)
pause_on_output_change ^= 1;
}

static void get_block_key_paste(void *data, char *buf, size_t size)
{
strscpy(buf, bool_names[block_key_paste], size);
}

static void set_block_key_paste(void *data, const char *buf)
{
parse_bool(buf, &pause_on_output_change);
}

static void toggle_block_key_paste(void *data)
{
pause_on_output_change ^= 1;
}

/* }}} */

/* special callbacks (id set) {{{ */
Expand Down Expand Up @@ -1581,6 +1597,7 @@ static const struct {
DN(tree_width_max)
DT(pause_on_output_change)
DN(pl_env_vars)
DT(block_key_paste)
{ NULL, NULL, NULL, NULL, 0 }
};

Expand Down
1 change: 1 addition & 0 deletions options.h
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ extern int stop_after_queue;
extern int tree_width_percent;
extern int tree_width_max;
extern int pause_on_output_change;
extern int block_key_paste;

extern const char * const aaa_mode_names[];
extern const char * const view_names[NR_VIEWS + 1];
Expand Down
27 changes: 25 additions & 2 deletions ui_curses.c
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ static const int default_esc_delay = 25;

static char *title_buf = NULL;

static int in_bracketed_paste = 0;

enum {
CURSED_WIN,
CURSED_WIN_CUR,
Expand Down Expand Up @@ -1988,7 +1990,9 @@ static void handle_ch(uchar ch)
{
clear_error();
if (input_mode == NORMAL_MODE) {
normal_mode_ch(ch);
if (!block_key_paste || !in_bracketed_paste) {
normal_mode_ch(ch);
}
} else if (input_mode == COMMAND_MODE) {
command_mode_ch(ch);
update_commandline();
Expand Down Expand Up @@ -2024,6 +2028,15 @@ static void handle_csi(void) {
if (!done) {
return; // overflow
}

if (buf_n == 4) {
// bracketed paste
// https://invisible-island.net/xterm/ctlseqs/ctlseqs.html#h3-Functions-using-CSI-_-ordered-by-the-final-character_s_
if (buf[0] == '2' && buf[1] == '0' && (buf[2] == '0' || buf[2] == '1') && buf[3] == '~') {
in_bracketed_paste = buf[2] == '0';
return;
}
}
}

static void handle_escape(int c)
Expand All @@ -2044,7 +2057,9 @@ static void handle_key(int key)
{
clear_error();
if (input_mode == NORMAL_MODE) {
normal_mode_key(key);
if (!block_key_paste || !in_bracketed_paste) {
normal_mode_key(key);
}
} else if (input_mode == COMMAND_MODE) {
command_mode_key(key);
update_commandline();
Expand Down Expand Up @@ -2418,6 +2433,10 @@ static void init_all(void)

init_curses();

// enable bracketed paste (will be ignored if not supported)
printf("\033[?2004h");
fflush(stdout);

if (resume_cmus) {
resume_load();
cmus_add(play_queue_append, play_queue_autosave_filename,
Expand All @@ -2436,6 +2455,10 @@ static void exit_all(void)
{
endwin();

// disable bracketed paste
printf("\033[?2004l");
fflush(stdout);

if (resume_cmus)
resume_exit();
options_exit();
Expand Down

0 comments on commit b88b8fa

Please sign in to comment.