From 9307a1668e44dfceb3bd3aabd2ee554b5f4dedd9 Mon Sep 17 00:00:00 2001 From: QC Date: Sun, 10 May 2015 12:08:08 +0200 Subject: [PATCH] Add -format option to dmenu. --- doc/rofi-manpage.markdown | 10 +++++++ doc/rofi.1 | 21 +++++++++++++++ include/rofi.h | 8 +++--- include/x11-helper.h | 1 + source/dialogs/dmenu.c | 55 +++++++++++++++++++++++++-------------- source/dialogs/window.c | 3 +++ 6 files changed, 74 insertions(+), 24 deletions(-) diff --git a/doc/rofi-manpage.markdown b/doc/rofi-manpage.markdown index 432ff985..ba817879 100644 --- a/doc/rofi-manpage.markdown +++ b/doc/rofi-manpage.markdown @@ -512,6 +512,16 @@ The following options are further explained in the theming section: Only return a selected item, do not allow custom entry. This mode always returns an entry, or returns directly when no entries given. +`-format` *format* + + Allows the output of dmenu to be customized: + + * 's' selected string. + * 'i' index. + * 'e' selected string escaped. + + Default: 's' + ### Message dialog `-e` *message* diff --git a/doc/rofi.1 b/doc/rofi.1 index 3ebab373..b03dd81e 100644 --- a/doc/rofi.1 +++ b/doc/rofi.1 @@ -663,6 +663,27 @@ or a set of rows: \-u 0,2 Or any combination: \-u 0,2\-3,9 .fi .RE +.PP +\fB\fC\-only\-match\fR +.PP +.RS +.nf +Only return a selected item, do not allow custom entry. +This mode always returns an entry, or returns directly when no entries given. +.fi +.RE +.PP +\fB\fC\-format\fR \fIformat\fP +.PP +.RS +.nf +Allows the output of dmenu to be customized: + * 's' selected string. + * 'i' index. + * 'e' selected string escaped. +Default: 's' +.fi +.RE .SS Message dialog .PP \fB\fC\-e\fR \fImessage\fP diff --git a/include/rofi.h b/include/rofi.h index 2fd3a816..56afb67d 100644 --- a/include/rofi.h +++ b/include/rofi.h @@ -45,13 +45,13 @@ typedef const char * ( *get_display_value )( unsigned int selected_line, void *d typedef enum { /** Entry is selected. */ - MENU_OK = 0x0010000, + MENU_OK = 0x00010000, /** User canceled the operation. (e.g. pressed escape) */ - MENU_CANCEL = 0x0020000, + MENU_CANCEL = 0x00020000, /** User requested a mode switch */ - MENU_NEXT = 0x0040000, + MENU_NEXT = 0x00040000, /** Custom (non-matched) input was entered. */ - MENU_CUSTOM_INPUT = 0x0080000, + MENU_CUSTOM_INPUT = 0x00080000, /** User wanted to delete entry from history. */ MENU_ENTRY_DELETE = 0x00100000, /** User wants to jump to another switcher. */ diff --git a/include/x11-helper.h b/include/x11-helper.h index 249bedbb..2cf94e81 100644 --- a/include/x11-helper.h +++ b/include/x11-helper.h @@ -41,6 +41,7 @@ int window_get_cardinal_prop ( Display *display, Window w, Atom atom, unsigned l X ( _NET_WM_STATE_SKIP_PAGER ), \ X ( _NET_WM_STATE_ABOVE ), \ X ( _NET_WM_STATE_DEMANDS_ATTENTION ), \ + X ( _NET_WM_STATE_WITHDRAWN ), \ X ( _NET_WM_DESKTOP ), \ X ( CLIPBOARD ), \ X ( UTF8_STRING ), \ diff --git a/source/dialogs/dmenu.c b/source/dialogs/dmenu.c index d54c8f21..0ef372b8 100644 --- a/source/dialogs/dmenu.c +++ b/source/dialogs/dmenu.c @@ -127,6 +127,28 @@ static const char *get_display_data ( unsigned int index, void *data, G_GNUC_UNU return retv[index]; } +static void dmenu_format_line ( const char *format, const char *string, int selected_line ) +{ + for ( int i = 0; format && format[i]; i++ ) { + if ( format[i] == 'i' ) { + fprintf ( stdout, "%d", selected_line ); + } + else if ( format[i] == 's' ) { + fputs ( string, stdout ); + } + else if ( format[i] == 'e' ) { + char *quote = g_shell_quote ( string ); + fputs ( quote, stdout ); + g_free ( quote ); + } + else { + fputc ( format[i], stdout ); + } + } + fputc ( '\n', stdout ); + fflush ( stdout ); +} + int dmenu_switcher_dialog ( char **input ) { char *dmenu_prompt = "dmenu "; @@ -137,10 +159,14 @@ int dmenu_switcher_dialog ( char **input ) int restart = FALSE; int number_mode = FALSE; + char *format = "s"; // Check if the user requested number mode. if ( find_arg ( "-i" ) >= 0 ) { number_mode = TRUE; + format = "i"; } + + find_arg_str ( "-format", &format ); // Check prompt find_arg_str ( "-p", &dmenu_prompt ); find_arg_int ( "-l", &selected_line ); @@ -176,18 +202,11 @@ int dmenu_switcher_dialog ( char **input ) */ restart = TRUE; if ( ( mretv & ( MENU_OK | MENU_QUICK_SWITCH ) ) && list[selected_line] != NULL ) { - if ( number_mode ) { - fprintf ( stdout, "%d", selected_line ); - } - else { - fputs ( list[selected_line], stdout ); - } + dmenu_format_line ( format, list[selected_line], selected_line ); retv = TRUE; if ( ( mretv & MENU_QUICK_SWITCH ) ) { retv = 10 + ( mretv & MENU_LOWER_MASK ); } - fputc ( '\n', stdout ); - fflush ( stdout ); return retv; } selected_line = next_pos - 1; @@ -196,14 +215,12 @@ int dmenu_switcher_dialog ( char **input ) // We normally do not want to restart the loop. restart = FALSE; if ( ( mretv & ( MENU_OK | MENU_CUSTOM_INPUT ) ) && list[selected_line] != NULL ) { - if ( number_mode ) { - fprintf ( stdout, "%d", selected_line ); + if ( ( mretv & MENU_CUSTOM_INPUT ) ) { + dmenu_format_line ( format, *input, -1 ); } - else { - fputs ( list[selected_line], stdout ); + else{ + dmenu_format_line ( format, list[selected_line], selected_line ); } - fputc ( '\n', stdout ); - fflush ( stdout ); if ( ( mretv & MENU_SHIFT ) ) { restart = TRUE; // Move to next line. @@ -215,14 +232,12 @@ int dmenu_switcher_dialog ( char **input ) } } else if ( ( mretv & MENU_QUICK_SWITCH ) ) { - if ( number_mode ) { - fprintf ( stdout, "%d", selected_line ); + if ( ( mretv & MENU_CUSTOM_INPUT ) ) { + dmenu_format_line ( format, *input, -1 ); } - else { - fputs ( list[selected_line], stdout ); + else{ + dmenu_format_line ( format, list[selected_line], selected_line ); } - fputc ( '\n', stdout ); - fflush ( stdout ); restart = FALSE; retv = 10 + ( mretv & MENU_LOWER_MASK ); diff --git a/source/dialogs/window.c b/source/dialogs/window.c index c280af1a..4db5d7b9 100644 --- a/source/dialogs/window.c +++ b/source/dialogs/window.c @@ -408,6 +408,9 @@ static char ** window_mode_get_data ( unsigned int *length, Switcher *sw ) if ( client_has_state ( c, netatoms[_NET_WM_STATE_DEMANDS_ATTENTION] ) ) { c->demands = TRUE; } + if ( client_has_state ( c, netatoms[_NET_WM_STATE_WITHDRAWN] ) ) { + c->demands = TRUE; + } if ( ( c->hint_flags & XUrgencyHint ) == XUrgencyHint ) { c->demands = TRUE; }