>From 85552f85c84f3a911d77edc43f7f89a3cf0d7372 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Droz?= Date: Tue, 31 May 2011 01:06:05 +0200 Subject: [PATCH 4/6] Colored completion, print_filename() process (4/6) Handle the insertion of colored indicators before the filename output during the complete.c:print_filename() process. It is compatible with the "mark-directories" and "visible-stats" options. --- lib/readline/complete.c | 74 +++++++++++++++++++++++++++++++++++++++++----- 1 files changed, 66 insertions(+), 8 deletions(-) diff --git a/lib/readline/complete.c b/lib/readline/complete.c index 0526768..1f6ca81 100644 --- a/lib/readline/complete.c +++ b/lib/readline/complete.c @@ -100,6 +100,14 @@ rl_compdisp_func_t *rl_completion_display_matches_hook = (rl_compdisp_func_t *)N # endif static int stat_char PARAMS((char *)); #endif +#if defined (COLORED_STATS) +# if !defined (X_OK) +# define X_OK 1 +# endif +#include "colors.h" +static int colored_stat_start PARAMS((char *)); +static void colored_stat_end PARAMS((void)); +#endif static int path_isdir PARAMS((const char *)); @@ -189,6 +197,13 @@ int _rl_completion_columns = -1; int rl_visible_stats = 0; #endif /* VISIBLE_STATS */ +#if defined (COLORED_STATS) +/* Non-zero means colorize filename displayed + during listing completion if rl_filename_completion_desired which helps + to indicate the type of file being listed. */ +int rl_colored_stats = 0; +#endif /* !COLORED_STATS */ + /* If non-zero, when completing in the middle of a word, don't insert characters from the match that match characters following point in the word. This means, for instance, completing when the cursor is @@ -607,6 +622,21 @@ stat_char (filename) } #endif /* VISIBLE_STATS */ +#if defined (COLORED_STATS) +static int colored_stat_start (filename) + char *filename; +{ + set_normal_color(); + return print_color_indicator(filename); +} + +static void colored_stat_end (void) +{ + prep_non_filename_text (); + put_indicator (&color_indicator[C_CLR_TO_EOL]); +} +#endif + /* Return the portion of PATHNAME that should be output when listing possible completions. If we are hacking filename completion, we are only interested in the basename, the portion following the @@ -803,13 +833,22 @@ print_filename (to_print, full_pathname, prefix_bytes) char *s, c, *new_full_pathname, *dn; extension_char = 0; - printed_len = fnprint (to_print, prefix_bytes); + /* Postpones printing the filename if we need + to prefix the output with a color indicator. */ +#if defined (COLORED_STATS) + if (! rl_colored_stats) +#endif + printed_len = fnprint (to_print, prefix_bytes); + + if (rl_filename_completion_desired && ( #if defined (VISIBLE_STATS) - if (rl_filename_completion_desired && (rl_visible_stats || _rl_complete_mark_directories)) -#else - if (rl_filename_completion_desired && _rl_complete_mark_directories) + rl_visible_stats || #endif +#if defined (COLORED_STATS) + rl_colored_stats || +#endif + _rl_complete_mark_directories)) { /* If to_print != full_pathname, to_print is the basename of the path passed. In this case, we try to expand the directory @@ -855,9 +894,21 @@ print_filename (to_print, full_pathname, prefix_bytes) extension_char = stat_char (new_full_pathname); else #endif - if (path_isdir (new_full_pathname)) - extension_char = '/'; - + if (_rl_complete_mark_directories && path_isdir (new_full_pathname)) + extension_char = '/'; +#if defined(COLORED_STATS) + if(rl_colored_stats) { + colored_stat_start(new_full_pathname); + printed_len = fnprint (to_print, prefix_bytes); + /* + We can try to raise(SIGKILL) here to test whether or not + there are risks to mess-up with the initial terminal color. + But even ls /usr/bin/ with "page-completions" "off" + seems safe. + */ + colored_stat_end (); + } +#endif xfree (new_full_pathname); to_print[-1] = c; } @@ -869,8 +920,15 @@ print_filename (to_print, full_pathname, prefix_bytes) extension_char = stat_char (s); else #endif - if (path_isdir (s)) + if (_rl_complete_mark_directories && path_isdir (s)) extension_char = '/'; +#if defined(COLORED_STATS) + if(rl_colored_stats) { + colored_stat_start(s); + printed_len = fnprint (to_print, prefix_bytes); + colored_stat_end (); + } +#endif } xfree (s); -- 1.7.3.4