Re-indent.

This commit is contained in:
QC 2014-06-04 21:29:23 +02:00
parent 7667a88732
commit 9fbd7fc99c
8 changed files with 400 additions and 812 deletions

View File

@ -13,11 +13,11 @@ indent_brace = 0
nl_enum_brace = add # "enum {" vs "enum \n {"
nl_union_brace = add # "union {" vs "union \n {"
nl_struct_brace = add # "struct {" vs "struct \n {"
nl_do_brace = add # "do {" vs "do \n {"
nl_if_brace = add # "if () {" vs "if () \n {"
nl_for_brace = add # "for () {" vs "for () \n {"
nl_else_brace = add # "else {" vs "else \n {"
nl_while_brace = add # "while () {" vs "while () \n {"
nl_do_brace = remove # "do {" vs "do \n {"
nl_if_brace = remove # "if () {" vs "if () \n {"
nl_for_brace = remove # "for () {" vs "for () \n {"
nl_else_brace = remove # "else {" vs "else \n {"
nl_while_brace = remove # "while () {" vs "while () \n {"
nl_switch_brace = add # "switch () {" vs "switch () \n {"
# nl_func_var_def_blk = 1
# nl_before_case = 1
@ -28,9 +28,9 @@ nl_brace_while = remove
nl_brace_else = add
nl_squeeze_ifdef = FALSE
# mod_paren_on_return = add # "return 1;" vs "return (1);"
mod_paren_on_return = remove # "return 1;" vs "return (1);"
mod_full_brace_if = add # "if (a) a--;" vs "if (a) { a--; }"
mod_full_brace_for = add # "for () a--;" vs "for () { a--; }"
mod_full_brace_for = add # "for () a--;" vs "for () { a--; }"
mod_full_brace_do = add # "do a--; while ();" vs "do { a--; } while ();"
mod_full_brace_while = add # "while (a) a--;" vs "while (a) { a--; }"

View File

@ -44,11 +44,9 @@ static char **get_dmenu ( void )
char **retv = NULL;
int index = 0;
while ( fgets ( buffer, 1024, stdin ) != NULL )
{
while ( fgets ( buffer, 1024, stdin ) != NULL ) {
char **tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) );
if ( tr == NULL )
{
if ( tr == NULL ) {
return retv;
}
retv = tr;
@ -56,8 +54,7 @@ static char **get_dmenu ( void )
retv[index + 1] = NULL;
// Filter out line-end.
if ( retv[index][strlen ( buffer ) - 1] == '\n' )
{
if ( retv[index][strlen ( buffer ) - 1] == '\n' ) {
retv[index][strlen ( buffer ) - 1] = '\0';
}
@ -76,26 +73,21 @@ SwitcherMode dmenu_switcher_dialog ( char **input )
int mretv = menu ( list, input, dmenu_prompt, NULL, NULL,
token_match, NULL, &selected_line );
if ( mretv == MENU_NEXT )
{
if ( mretv == MENU_NEXT ) {
retv = DMENU_DIALOG;
}
else if ( mretv == MENU_OK && list[selected_line] != NULL )
{
else if ( mretv == MENU_OK && list[selected_line] != NULL ) {
fputs ( list[selected_line], stdout );
}
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' )
{
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
fputs ( *input, stdout );
}
for ( unsigned int i = 0; list != NULL && list[i] != NULL; i++ )
{
for ( unsigned int i = 0; list != NULL && list[i] != NULL; i++ ) {
free ( list[i] );
}
if ( list != NULL )
{
if ( list != NULL ) {
free ( list );
}

View File

@ -51,8 +51,7 @@ static int __element_sort_func ( const void *ea, const void *eb )
static void __history_write_element_list ( FILE *fd, _element **list, unsigned int length )
{
if ( list == NULL )
{
if ( list == NULL ) {
return;
}
// Sort the list before writing out.
@ -65,8 +64,7 @@ static void __history_write_element_list ( FILE *fd, _element **list, unsigned i
length = ( length > HISTORY_MAX_ENTRIES ) ? HISTORY_MAX_ENTRIES : length;
// Write out entries.
for ( unsigned int iter = 0; iter < length; iter++ )
{
for ( unsigned int iter = 0; iter < length; iter++ ) {
fprintf ( fd, "%ld %s\n", list[iter]->index - min_value, list[iter]->name );
}
}
@ -76,28 +74,23 @@ static _element ** __history_get_element_list ( FILE *fd, unsigned int *length )
char buffer[HISTORY_NAME_LENGTH + 16];
_element **retv = NULL;
if ( length == NULL )
{
if ( length == NULL ) {
return NULL;
}
*length = 0;
if ( fd == NULL )
{
if ( fd == NULL ) {
return NULL;
}
while ( fgets ( buffer, HISTORY_NAME_LENGTH + 16, fd ) != NULL )
{
while ( fgets ( buffer, HISTORY_NAME_LENGTH + 16, fd ) != NULL ) {
char * start = NULL;
// Skip empty lines.
if ( strlen ( buffer ) == 0 )
{
if ( strlen ( buffer ) == 0 ) {
continue;
}
// Resize and check.
_element **tr = realloc ( retv, ( *length + 2 ) * sizeof ( _element* ) );
if ( tr == NULL )
{
if ( tr == NULL ) {
return retv;
}
retv = tr;
@ -126,8 +119,7 @@ void history_set ( const char *filename, const char *entry )
_element **list = NULL;
// Open file for reading and writing.
FILE *fd = fopen ( filename, "a+" );
if ( fd == NULL )
{
if ( fd == NULL ) {
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
return;
}
@ -135,32 +127,26 @@ void history_set ( const char *filename, const char *entry )
list = __history_get_element_list ( fd, &length );
// Look if the entry exists.
for ( unsigned int iter = 0; !found && iter < length; iter++ )
{
if ( strcmp ( list[iter]->name, entry ) == 0 )
{
for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
if ( strcmp ( list[iter]->name, entry ) == 0 ) {
curr = iter;
found = 1;
}
}
if ( found )
{
if ( found ) {
// If exists, increment list index number
list[curr]->index++;
}
else
{
else{
// If not exists, add it.
// Increase list by one
_element **tr = realloc ( list, ( length + 2 ) * sizeof ( _element* ) );
if ( tr != NULL )
{
if ( tr != NULL ) {
list = tr;
list[length] = malloc ( sizeof ( _element ) );
// Copy name
if ( list[length] != NULL )
{
if ( list[length] != NULL ) {
strncpy ( list[length]->name, entry, HISTORY_NAME_LENGTH );
list[length]->name[HISTORY_NAME_LENGTH - 1] = '\0';
// set # hits
@ -175,18 +161,15 @@ void history_set ( const char *filename, const char *entry )
// Rewind.
fseek ( fd, 0L, SEEK_SET );
// Clear file.
if ( ftruncate ( fileno ( fd ), 0 ) == 0 )
{
if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) {
// Write list.
__history_write_element_list ( fd, list, length );
}
else
{
else{
fprintf ( stderr, "Failed to truncate file: %s\n", strerror ( errno ) );
}
// Free the list.
for ( unsigned int iter = 0; iter < length; iter++ )
{
for ( unsigned int iter = 0; iter < length; iter++ ) {
free ( list[iter] );
}
free ( list );
@ -202,8 +185,7 @@ void history_remove ( const char *filename, const char *entry )
unsigned int length = 0;
// Open file for reading and writing.
FILE *fd = fopen ( filename, "a+" );
if ( fd == NULL )
{
if ( fd == NULL ) {
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
return;
}
@ -211,18 +193,15 @@ void history_remove ( const char *filename, const char *entry )
list = __history_get_element_list ( fd, &length );
// Find entry.
for ( unsigned int iter = 0; !found && iter < length; iter++ )
{
if ( strcmp ( list[iter]->name, entry ) == 0 )
{
for ( unsigned int iter = 0; !found && iter < length; iter++ ) {
if ( strcmp ( list[iter]->name, entry ) == 0 ) {
curr = iter;
found = 1;
}
}
// If found, remove it and write out new file.
if ( found )
{
if ( found ) {
// Remove the entry.
free ( list[curr] );
// Swap last to here (if list is size 1, we just swap empty sets).
@ -234,24 +213,20 @@ void history_remove ( const char *filename, const char *entry )
// Rewind.
fseek ( fd, 0L, SEEK_SET );
// Clear list.
if ( ftruncate ( fileno ( fd ), 0 ) == 0 )
{
if ( ftruncate ( fileno ( fd ), 0 ) == 0 ) {
// Write list.
__history_write_element_list ( fd, list, length );
}
else
{
else{
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
}
}
// Free the list.
for ( unsigned int iter = 0; iter < length; iter++ )
{
for ( unsigned int iter = 0; iter < length; iter++ ) {
free ( list[iter] );
}
if ( list != NULL )
{
if ( list != NULL ) {
free ( list );
}
// Close file.
@ -264,12 +239,10 @@ char ** history_get_list ( const char *filename, unsigned int *length )
char **retv = NULL;
// Open file.
FILE *fd = fopen ( filename, "r" );
if ( fd == NULL )
{
if ( fd == NULL ) {
// File that does not exists is not an error, so ignore it.
// Everything else? panic.
if ( errno != ENOENT )
{
if ( errno != ENOENT ) {
fprintf ( stderr, "Failed to open file: %s\n", strerror ( errno ) );
}
return NULL;
@ -279,11 +252,9 @@ char ** history_get_list ( const char *filename, unsigned int *length )
// Copy list in right format.
// Lists are always short, so performance should not be an issue.
if ( ( *length ) > 0 )
{
if ( ( *length ) > 0 ) {
retv = malloc ( ( ( *length ) + 1 ) * sizeof ( char * ) );
for ( int iter = 0; iter < ( *length ); iter++ )
{
for ( int iter = 0; iter < ( *length ); iter++ ) {
retv[iter] = strdup ( list[iter]->name );
free ( list[iter] );
}

File diff suppressed because it is too large Load Diff

View File

@ -51,8 +51,7 @@
static inline int execsh ( const char *cmd, int run_in_term )
{
// use sh for args parsing
if ( run_in_term )
{
if ( run_in_term ) {
return execlp ( config.terminal_emulator, config.terminal_emulator, "-e", "sh", "-c", cmd, NULL );
}
@ -62,16 +61,14 @@ static inline int execsh ( const char *cmd, int run_in_term )
// execute sub-process
static pid_t exec_cmd ( const char *cmd, int run_in_term )
{
if ( !cmd || !cmd[0] )
{
if ( !cmd || !cmd[0] ) {
return -1;
}
signal ( SIGCHLD, catch_exit );
pid_t pid = fork ();
if ( !pid )
{
if ( !pid ) {
setsid ();
execsh ( cmd, run_in_term );
exit ( EXIT_FAILURE );
@ -82,8 +79,7 @@ static pid_t exec_cmd ( const char *cmd, int run_in_term )
* It is allowed to be a bit slower.
*/
char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 )
{
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 ) {
return -1;
}
history_set ( path, cmd );
@ -100,8 +96,7 @@ static void delete_entry ( const char *cmd )
* It is allowed to be a bit slower.
*/
char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 )
{
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) == -1 ) {
return;
}
history_remove ( path, cmd );
@ -125,14 +120,12 @@ static char ** get_apps ( void )
clock_gettime ( CLOCK_REALTIME, &start );
#endif
if ( getenv ( "PATH" ) == NULL )
{
if ( getenv ( "PATH" ) == NULL ) {
return NULL;
}
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) > 0 )
{
if ( asprintf ( &path, "%s/%s", cache_dir, RUN_CACHE_FILE ) > 0 ) {
retv = history_get_list ( path, &index );
free ( path );
// Keep track of how many where loaded as favorite.
@ -144,20 +137,16 @@ static char ** get_apps ( void )
for ( const char *dirname = strtok ( path, ":" );
dirname != NULL;
dirname = strtok ( NULL, ":" ) )
{
dirname = strtok ( NULL, ":" ) ) {
DIR *dir = opendir ( dirname );
if ( dir != NULL )
{
if ( dir != NULL ) {
struct dirent *dent;
while ( ( dent = readdir ( dir ) ) != NULL )
{
while ( ( dent = readdir ( dir ) ) != NULL ) {
if ( dent->d_type != DT_REG &&
dent->d_type != DT_LNK &&
dent->d_type != DT_UNKNOWN )
{
dent->d_type != DT_UNKNOWN ) {
continue;
}
@ -165,22 +154,18 @@ static char ** get_apps ( void )
// This is a nice little penalty, but doable? time will tell.
// given num_favorites is max 25.
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ )
{
if ( strcasecmp ( dent->d_name, retv[j] ) == 0 )
{
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
if ( strcasecmp ( dent->d_name, retv[j] ) == 0 ) {
found = 1;
}
}
if ( found == 1 )
{
if ( found == 1 ) {
continue;
}
char ** tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) );
if ( tr != NULL )
{
if ( tr != NULL ) {
retv = tr;
retv[index] = strdup ( dent->d_name );
retv[index + 1] = NULL;
@ -193,16 +178,14 @@ static char ** get_apps ( void )
}
// TODO: check this is still fast enough. (takes 1ms on laptop.)
if ( index > num_favorites )
{
if ( index > num_favorites ) {
qsort ( &retv[num_favorites], index - num_favorites, sizeof ( char* ), sort_func );
}
free ( path );
#ifdef TIMING
clock_gettime ( CLOCK_REALTIME, &stop );
if ( stop.tv_sec != start.tv_sec )
{
if ( stop.tv_sec != start.tv_sec ) {
stop.tv_nsec += ( stop.tv_sec - start.tv_sec ) * 1e9;
}
@ -220,8 +203,7 @@ SwitcherMode run_switcher_dialog ( char **input )
// act as a launcher
char **cmd_list = get_apps ( );
if ( cmd_list == NULL )
{
if ( cmd_list == NULL ) {
cmd_list = malloc ( 2 * sizeof ( char * ) );
cmd_list[0] = strdup ( "No applications found" );
cmd_list[1] = NULL;
@ -229,31 +211,25 @@ SwitcherMode run_switcher_dialog ( char **input )
int mretv = menu ( cmd_list, input, "run:", NULL, &shift, token_match, NULL, &selected_line );
if ( mretv == MENU_NEXT )
{
if ( mretv == MENU_NEXT ) {
retv = NEXT_DIALOG;
}
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL )
{
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) {
exec_cmd ( cmd_list[selected_line], shift );
}
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' )
{
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
exec_cmd ( *input, shift );
}
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] )
{
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] ) {
delete_entry ( cmd_list[selected_line] );
retv = RUN_DIALOG;
}
for ( int i = 0; cmd_list != NULL && cmd_list[i] != NULL; i++ )
{
for ( int i = 0; cmd_list != NULL && cmd_list[i] != NULL; i++ ) {
free ( cmd_list[i] );
}
if ( cmd_list != NULL )
{
if ( cmd_list != NULL ) {
free ( cmd_list );
}

View File

@ -56,11 +56,9 @@ static inline int execshssh ( const char *host )
char **args = malloc ( sizeof ( char* ) * 7 );
int i = 0;
args[i++] = config.terminal_emulator;
if ( config.ssh_set_title )
{
if ( config.ssh_set_title ) {
char *buffer = NULL;
if ( asprintf ( &buffer, "ssh %s", host ) > 0 )
{
if ( asprintf ( &buffer, "ssh %s", host ) > 0 ) {
args[i++] = strdup ( "-T" );
args[i++] = buffer;
}
@ -72,10 +70,8 @@ static inline int execshssh ( const char *host )
int retv = execvp ( config.terminal_emulator, (char * const *) args );
// Free the args list.
for ( i = 0; i < 7; i++ )
{
if ( args[i] != NULL )
{
for ( i = 0; i < 7; i++ ) {
if ( args[i] != NULL ) {
free ( args[i] );
}
}
@ -85,16 +81,14 @@ static inline int execshssh ( const char *host )
// execute sub-process
static pid_t exec_ssh ( const char *cmd )
{
if ( !cmd || !cmd[0] )
{
if ( !cmd || !cmd[0] ) {
return -1;
}
signal ( SIGCHLD, catch_exit );
pid_t pid = fork ();
if ( !pid )
{
if ( !pid ) {
setsid ();
execshssh ( cmd );
exit ( EXIT_FAILURE );
@ -105,8 +99,7 @@ static pid_t exec_ssh ( const char *cmd )
* It is allowed to be a bit slower.
*/
char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 )
{
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
history_set ( path, cmd );
free ( path );
}
@ -114,13 +107,11 @@ static pid_t exec_ssh ( const char *cmd )
}
static void delete_ssh ( const char *cmd )
{
if ( !cmd || !cmd[0] )
{
if ( !cmd || !cmd[0] ) {
return;
}
char *path = NULL;
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 )
{
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
history_remove ( path, cmd );
free ( path );
}
@ -142,13 +133,11 @@ static char ** get_ssh ( void )
clock_gettime ( CLOCK_REALTIME, &start );
#endif
if ( getenv ( "HOME" ) == NULL )
{
if ( getenv ( "HOME" ) == NULL ) {
return NULL;
}
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 )
{
if ( asprintf ( &path, "%s/%s", cache_dir, SSH_CACHE_FILE ) > 0 ) {
retv = history_get_list ( path, &index );
free ( path );
num_favorites = index;
@ -157,58 +146,47 @@ static char ** get_ssh ( void )
FILE *fd = NULL;
const char *hd = getenv ( "HOME" );
if ( asprintf ( &path, "%s/%s", hd, ".ssh/config" ) >= 0 )
{
if ( asprintf ( &path, "%s/%s", hd, ".ssh/config" ) >= 0 ) {
fd = fopen ( path, "r" );
}
if ( fd != NULL )
{
if ( fd != NULL ) {
char buffer[1024];
while ( fgets ( buffer, 1024, fd ) != NULL )
{
if ( strncasecmp ( buffer, "Host", 4 ) == 0 )
{
while ( fgets ( buffer, 1024, fd ) != NULL ) {
if ( strncasecmp ( buffer, "Host", 4 ) == 0 ) {
int start = 0, stop = 0;
buffer[strlen ( buffer ) - 1] = '\0';
for ( start = 4; isspace ( buffer[start] ); start++ )
{
for ( start = 4; isspace ( buffer[start] ); start++ ) {
;
}
for ( stop = start; isalnum ( buffer[stop] ) ||
buffer[stop] == '_' ||
buffer[stop] == '.'; stop++ )
{
buffer[stop] == '.'; stop++ ) {
;
}
int found = 0;
if ( start == stop )
{
if ( start == stop ) {
continue;
}
// This is a nice little penalty, but doable? time will tell.
// given num_favorites is max 25.
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ )
{
if ( strncasecmp ( &buffer[start], retv[j], stop - start ) == 0 )
{
for ( unsigned int j = 0; found == 0 && j < num_favorites; j++ ) {
if ( strncasecmp ( &buffer[start], retv[j], stop - start ) == 0 ) {
found = 1;
}
}
if ( found == 1 )
{
if ( found == 1 ) {
continue;
}
char **tr = realloc ( retv, ( index + 2 ) * sizeof ( char* ) );
if ( tr != NULL )
{
if ( tr != NULL ) {
retv = tr;
retv[index] = strndup ( &buffer[start], stop - start );
retv[index + 1] = NULL;
@ -221,16 +199,14 @@ static char ** get_ssh ( void )
}
// TODO: check this is still fast enough. (takes 1ms on laptop.)
if ( index > num_favorites )
{
if ( index > num_favorites ) {
qsort ( &retv[num_favorites], index - num_favorites, sizeof ( char* ), sort_func );
}
free ( path );
#ifdef TIMING
clock_gettime ( CLOCK_REALTIME, &stop );
if ( stop.tv_sec != start.tv_sec )
{
if ( stop.tv_sec != start.tv_sec ) {
stop.tv_nsec += ( stop.tv_sec - start.tv_sec ) * 1e9;
}
@ -246,8 +222,7 @@ SwitcherMode ssh_switcher_dialog ( char **input )
// act as a launcher
char **cmd_list = get_ssh ( );
if ( cmd_list == NULL )
{
if ( cmd_list == NULL ) {
cmd_list = malloc ( 2 * sizeof ( char * ) );
cmd_list[0] = strdup ( "No ssh hosts found" );
cmd_list[1] = NULL;
@ -257,32 +232,26 @@ SwitcherMode ssh_switcher_dialog ( char **input )
int selected_line = 0;
int mretv = menu ( cmd_list, input, "ssh:", NULL, &shift, token_match, NULL, &selected_line );
if ( mretv == MENU_NEXT )
{
if ( mretv == MENU_NEXT ) {
retv = NEXT_DIALOG;
}
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL )
{
else if ( mretv == MENU_OK && cmd_list[selected_line] != NULL ) {
exec_ssh ( cmd_list[selected_line] );
}
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' )
{
else if ( mretv == MENU_CUSTOM_INPUT && *input != NULL && *input[0] != '\0' ) {
exec_ssh ( *input );
}
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] )
{
else if ( mretv == MENU_ENTRY_DELETE && cmd_list[selected_line] ) {
delete_ssh ( cmd_list[selected_line] );
// Stay
retv = SSH_DIALOG;
}
for ( int i = 0; cmd_list[i] != NULL; i++ )
{
for ( int i = 0; cmd_list[i] != NULL; i++ ) {
free ( cmd_list[i] );
}
if ( cmd_list != NULL )
{
if ( cmd_list != NULL ) {
free ( cmd_list );
}

View File

@ -87,8 +87,7 @@ textbox* textbox_create ( Window parent,
textbox_moveresize ( tb, tb->x, tb->y, tb->w, tb->h );
// edit mode controls
if ( tb->flags & TB_EDITABLE )
{
if ( tb->flags & TB_EDITABLE ) {
tb->xim = XOpenIM ( display, NULL, NULL, NULL );
tb->xic = XCreateIC ( tb->xim, XNInputStyle, XIMPreeditNothing | XIMStatusNothing, XNClientWindow, tb->window, XNFocusWindow, tb->window, NULL );
}
@ -134,8 +133,7 @@ void textbox_extents ( textbox *tb )
// set the default text to display
void textbox_text ( textbox *tb, char *text )
{
if ( tb->text )
{
if ( tb->text ) {
free ( tb->text );
}
@ -146,8 +144,7 @@ void textbox_text ( textbox *tb, char *text )
void textbox_move ( textbox *tb, int x, int y )
{
if ( x != tb->x || y != tb->y )
{
if ( x != tb->x || y != tb->y ) {
tb->x = x;
tb->y = y;
XMoveResizeWindow ( display, tb->window, tb->x, tb->y, tb->w, tb->h );
@ -156,25 +153,20 @@ void textbox_move ( textbox *tb, int x, int y )
// within the parent. handled auto width/height modes
void textbox_moveresize ( textbox *tb, int x, int y, int w, int h )
{
if ( tb->flags & TB_AUTOHEIGHT )
{
if ( tb->flags & TB_AUTOHEIGHT ) {
h = tb->font->ascent + tb->font->descent;
}
if ( tb->flags & TB_AUTOWIDTH )
{
if ( w > 1 )
{
if ( tb->flags & TB_AUTOWIDTH ) {
if ( w > 1 ) {
w = MIN ( w, tb->extents.width + 2 * SIDE_MARGIN );
}
else
{
else{
w = tb->extents.width + 2 * SIDE_MARGIN;
}
}
if ( x != tb->x || y != tb->y || w != tb->w || h != tb->h )
{
if ( x != tb->x || y != tb->y || w != tb->w || h != tb->h ) {
tb->x = x;
tb->y = y;
tb->w = MAX ( 1, w );
@ -196,14 +188,12 @@ void textbox_hide ( textbox *tb )
// will also unmap the window if still displayed
void textbox_free ( textbox *tb )
{
if ( tb->flags & TB_EDITABLE )
{
if ( tb->flags & TB_EDITABLE ) {
XDestroyIC ( tb->xic );
XCloseIM ( tb->xim );
}
if ( tb->text )
{
if ( tb->text ) {
free ( tb->text );
}
@ -231,8 +221,7 @@ void textbox_draw ( textbox *tb )
int cursor_x = 0;
int cursor_width = MAX ( 2, line_height / 10 );
if ( tb->flags & TB_EDITABLE )
{
if ( tb->flags & TB_EDITABLE ) {
int cursor_offset = 0;
cursor_offset = MIN ( tb->cursor, text_len );
@ -240,8 +229,7 @@ void textbox_draw ( textbox *tb )
// The replace by _ is needed, one way or the other.
// Make a copy, and replace all trailing spaces.
char *test = strdup ( text );
for ( int iter = strlen ( text ) - 1; iter >= 0 && test[iter] == ' '; iter-- )
{
for ( int iter = strlen ( text ) - 1; iter >= 0 && test[iter] == ' '; iter-- ) {
test[iter] = '_';
}
// calc cursor position
@ -254,30 +242,25 @@ void textbox_draw ( textbox *tb )
// calc full input text width
// Calculate the right size, so no characters are cut off.
// TODO: Check performance of this.
do
{
do {
XftTextExtentsUtf8 ( display, tb->font, ( unsigned char * ) text, length, &extents );
line_width = extents.width;
if ( line_width <= ( tb->w - 2 * SIDE_MARGIN ) )
{
if ( line_width <= ( tb->w - 2 * SIDE_MARGIN ) ) {
break;
}
for ( length -= 1; length > 0 && ( text[length] & 0xc0 ) == 0x80; length -= 1 )
{
for ( length -= 1; length > 0 && ( text[length] & 0xc0 ) == 0x80; length -= 1 ) {
;
}
} while ( line_width > 0 );
int x = SIDE_MARGIN, y = tb->font->ascent;
if ( tb->flags & TB_RIGHT )
{
if ( tb->flags & TB_RIGHT ) {
x = tb->w - line_width;
}
if ( tb->flags & TB_CENTER )
{
if ( tb->flags & TB_CENTER ) {
x = ( tb->w - line_width ) / 2;
}
@ -285,8 +268,7 @@ void textbox_draw ( textbox *tb )
XftDrawStringUtf8 ( draw, &tb->color_fg, tb->font, x, y, ( unsigned char * ) text, length );
// draw the cursor
if ( tb->flags & TB_EDITABLE )
{
if ( tb->flags & TB_EDITABLE ) {
XftDrawRect ( draw, &tb->color_fg, cursor_x + SIDE_MARGIN, 2, cursor_width, line_height - 4 );
}
@ -305,8 +287,7 @@ static size_t nextrune ( textbox *tb, int inc )
ssize_t n;
/* return location of next utf8 rune in the given direction (+1 or -1) */
for ( n = tb->cursor + inc; n + inc >= 0 && ( tb->text[n] & 0xc0 ) == 0x80; n += inc )
{
for ( n = tb->cursor + inc; n + inc >= 0 && ( tb->text[n] & 0xc0 ) == 0x80; n += inc ) {
;
}
return n;
@ -372,8 +353,7 @@ void textbox_cursor_del ( textbox *tb )
// back up and delete one character
void textbox_cursor_bkspc ( textbox *tb )
{
if ( tb->cursor > 0 )
{
if ( tb->cursor > 0 ) {
textbox_cursor_dec ( tb );
textbox_cursor_del ( tb );
}
@ -390,21 +370,18 @@ int textbox_keypress ( textbox *tb, XEvent *ev )
char pad[32];
int len;
if ( !( tb->flags & TB_EDITABLE ) )
{
if ( !( tb->flags & TB_EDITABLE ) ) {
return 0;
}
len = Xutf8LookupString ( tb->xic, &ev->xkey, pad, sizeof ( pad ), &key, &stat );
pad[len] = 0;
if ( key == XK_Left )
{
if ( key == XK_Left ) {
textbox_cursor_dec ( tb );
return 1;
}
else if ( key == XK_Right )
{
else if ( key == XK_Right ) {
textbox_cursor_inc ( tb );
return 1;
} /*else if ( key == XK_Home ) {
@ -415,22 +392,18 @@ int textbox_keypress ( textbox *tb, XEvent *ev )
textbox_cursor_end( tb );
return 1;
} */
else if ( key == XK_Delete )
{
else if ( key == XK_Delete ) {
textbox_cursor_del ( tb );
return 1;
}
else if ( key == XK_BackSpace )
{
else if ( key == XK_BackSpace ) {
textbox_cursor_bkspc ( tb );
return 1;
}
else if ( key == XK_Return )
{
else if ( key == XK_Return ) {
return -1;
}
else if ( !iscntrl ( *pad ) )
{
else if ( !iscntrl ( *pad ) ) {
textbox_insert ( tb, tb->cursor, pad );
textbox_cursor_inc ( tb );
return 1;
@ -465,8 +438,7 @@ void textbox_setup (
void textbox_cleanup ()
{
if ( font != NULL )
{
if ( font != NULL ) {
Visual *visual = DefaultVisual ( display, DefaultScreen ( display ) );
Colormap colormap = DefaultColormap ( display, DefaultScreen ( display ) );

View File

@ -112,8 +112,7 @@ void parse_xresource_options ( Display *display )
XrmInitialize ();
xRMS = XResourceManagerString ( display );
if ( xRMS == NULL )
{
if ( xRMS == NULL ) {
return;
}
XrmDatabase xDB = XrmGetStringDatabase ( xRMS );
@ -123,21 +122,15 @@ void parse_xresource_options ( Display *display )
const char * namePrefix = "rofi";
const char * classPrefix = "rofi";
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i )
{
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i ) {
char *name, *class;
if ( asprintf ( &name, "%s.%s", namePrefix, xrmOptions[i].name ) == -1 )
{
if ( asprintf ( &name, "%s.%s", namePrefix, xrmOptions[i].name ) == -1 ) {
continue;
}
if ( asprintf ( &class, "%s.%s", classPrefix, xrmOptions[i].name ) > 0 )
{
if ( XrmGetResource ( xDB, name, class, &xrmType, &xrmValue ) )
{
if ( xrmOptions[i].type == xrm_String )
{
if ( xrmOptions[i].mem != NULL )
{
if ( asprintf ( &class, "%s.%s", classPrefix, xrmOptions[i].name ) > 0 ) {
if ( XrmGetResource ( xDB, name, class, &xrmType, &xrmValue ) ) {
if ( xrmOptions[i].type == xrm_String ) {
if ( xrmOptions[i].mem != NULL ) {
free ( xrmOptions[i].mem );
xrmOptions[i].mem = NULL;
}
@ -147,18 +140,14 @@ void parse_xresource_options ( Display *display )
// Memory
xrmOptions[i].mem = ( *xrmOptions[i].str );
}
else if ( xrmOptions[i].type == xrm_Number )
{
else if ( xrmOptions[i].type == xrm_Number ) {
*xrmOptions[i].num = strtol ( xrmValue.addr, NULL, 10 );
}
else if ( xrmOptions[i].type == xrm_Boolean )
{
if ( xrmValue.size > 0 && strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 )
{
else if ( xrmOptions[i].type == xrm_Boolean ) {
if ( xrmValue.size > 0 && strncasecmp ( xrmValue.addr, "true", xrmValue.size ) == 0 ) {
*xrmOptions[i].num = TRUE;
}
else
{
else{
*xrmOptions[i].num = FALSE;
}
}
@ -173,10 +162,8 @@ void parse_xresource_options ( Display *display )
void parse_xresource_free ( void )
{
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i )
{
if ( xrmOptions[i].mem != NULL )
{
for ( unsigned int i = 0; i < sizeof ( xrmOptions ) / sizeof ( *xrmOptions ); ++i ) {
if ( xrmOptions[i].mem != NULL ) {
free ( xrmOptions[i].mem );
xrmOptions[i].mem = NULL;
}
@ -187,13 +174,10 @@ void xresource_dump ( void )
{
const char * namePrefix = "rofi";
unsigned int entries = sizeof ( xrmOptions ) / sizeof ( *xrmOptions );
for ( unsigned int i = 0; i < entries; ++i )
{
for ( unsigned int i = 0; i < entries; ++i ) {
// Skip duplicates.
if ( ( i + 1 ) < entries )
{
if ( xrmOptions[i].str == xrmOptions[i + 1].str )
{
if ( ( i + 1 ) < entries ) {
if ( xrmOptions[i].str == xrmOptions[i + 1].str ) {
continue;
}
}