GtkTextView: various code clean-ups

- only one blank line is enough to separate code sections.
- the 'signals' variable was in the middle of function prototypes.
- compare pointers to NULL in some conditions ("if(blah) should be used
  only if blah is a boolean variable). It makes the code clearer.
- various other things.
This commit is contained in:
Sébastien Wilmet
2014-08-15 20:54:01 +02:00
parent 7bc819e88d
commit f39d211021
2 changed files with 47 additions and 56 deletions

View File

@ -40,7 +40,6 @@
#include "gtkprivate.h" #include "gtkprivate.h"
#include "gtkintl.h" #include "gtkintl.h"
/** /**
* SECTION:gtktextbuffer * SECTION:gtktextbuffer
* @Short_description: Stores attributed text for display in a GtkTextView * @Short_description: Stores attributed text for display in a GtkTextView
@ -53,7 +52,6 @@
* types related to the text widget and how they work together. * types related to the text widget and how they work together.
*/ */
typedef struct _GtkTextLogAttrCache GtkTextLogAttrCache; typedef struct _GtkTextLogAttrCache GtkTextLogAttrCache;
struct _GtkTextBufferPrivate struct _GtkTextBufferPrivate
@ -81,7 +79,6 @@ struct _GtkTextBufferPrivate
guint has_selection : 1; guint has_selection : 1;
}; };
typedef struct _ClipboardRequest ClipboardRequest; typedef struct _ClipboardRequest ClipboardRequest;
struct _ClipboardRequest struct _ClipboardRequest
@ -161,8 +158,6 @@ static GtkTextBuffer *create_clipboard_contents_buffer (GtkTextBuffer *buffer);
static void gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer); static void gtk_text_buffer_free_target_lists (GtkTextBuffer *buffer);
static guint signals[LAST_SIGNAL] = { 0 };
static void gtk_text_buffer_set_property (GObject *object, static void gtk_text_buffer_set_property (GObject *object,
guint prop_id, guint prop_id,
const GValue *value, const GValue *value,
@ -174,6 +169,8 @@ static void gtk_text_buffer_get_property (GObject *object,
static void gtk_text_buffer_notify (GObject *object, static void gtk_text_buffer_notify (GObject *object,
GParamSpec *pspec); GParamSpec *pspec);
static guint signals[LAST_SIGNAL] = { 0 };
G_DEFINE_TYPE_WITH_PRIVATE (GtkTextBuffer, gtk_text_buffer, G_TYPE_OBJECT) G_DEFINE_TYPE_WITH_PRIVATE (GtkTextBuffer, gtk_text_buffer, G_TYPE_OBJECT)
static void static void
@ -3892,7 +3889,6 @@ gtk_text_buffer_backspace (GtkTextBuffer *buffer,
GtkTextIter end; GtkTextIter end;
gboolean retval = FALSE; gboolean retval = FALSE;
const PangoLogAttr *attrs; const PangoLogAttr *attrs;
int offset;
gboolean backspace_deletes_character; gboolean backspace_deletes_character;
g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE); g_return_val_if_fail (GTK_IS_TEXT_BUFFER (buffer), FALSE);
@ -3906,9 +3902,9 @@ gtk_text_buffer_backspace (GtkTextBuffer *buffer,
/* For no good reason, attrs is NULL for the empty last line in /* For no good reason, attrs is NULL for the empty last line in
* a buffer. Special case that here. (#156164) * a buffer. Special case that here. (#156164)
*/ */
if (attrs) if (attrs != NULL)
{ {
offset = gtk_text_iter_get_line_offset (&start); gint offset = gtk_text_iter_get_line_offset (&start);
backspace_deletes_character = attrs[offset].backspace_deletes_character; backspace_deletes_character = attrs[offset].backspace_deletes_character;
} }
else else
@ -4289,24 +4285,23 @@ struct _GtkTextLogAttrCache
static void static void
free_log_attr_cache (GtkTextLogAttrCache *cache) free_log_attr_cache (GtkTextLogAttrCache *cache)
{ {
gint i = 0; gint i;
while (i < ATTR_CACHE_SIZE)
{ for (i = 0; i < ATTR_CACHE_SIZE; i++)
g_free (cache->entries[i].attrs); g_free (cache->entries[i].attrs);
++i;
}
g_slice_free (GtkTextLogAttrCache, cache); g_slice_free (GtkTextLogAttrCache, cache);
} }
static void static void
clear_log_attr_cache (GtkTextLogAttrCache *cache) clear_log_attr_cache (GtkTextLogAttrCache *cache)
{ {
gint i = 0; gint i;
while (i < ATTR_CACHE_SIZE)
for (i = 0; i < ATTR_CACHE_SIZE; i++)
{ {
g_free (cache->entries[i].attrs); g_free (cache->entries[i].attrs);
cache->entries[i].attrs = NULL; cache->entries[i].attrs = NULL;
++i;
} }
} }
@ -4332,9 +4327,9 @@ compute_log_attrs (const GtkTextIter *iter,
g_assert (char_len > 0); g_assert (char_len > 0);
if (char_lenp) if (char_lenp != NULL)
*char_lenp = char_len; *char_lenp = char_len;
attrs = g_new (PangoLogAttr, char_len + 1); attrs = g_new (PangoLogAttr, char_len + 1);
/* FIXME we need to follow PangoLayout and allow different language /* FIXME we need to follow PangoLayout and allow different language
@ -4352,7 +4347,7 @@ compute_log_attrs (const GtkTextIter *iter,
/* The return value from this is valid until you call this a second time. /* The return value from this is valid until you call this a second time.
*/ */
const PangoLogAttr* const PangoLogAttr *
_gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer, _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
const GtkTextIter *anywhere_in_line, const GtkTextIter *anywhere_in_line,
gint *char_len) gint *char_len)
@ -4371,7 +4366,7 @@ _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
if (gtk_text_iter_is_end (anywhere_in_line) && if (gtk_text_iter_is_end (anywhere_in_line) &&
gtk_text_iter_get_line_offset (anywhere_in_line) == 0) gtk_text_iter_get_line_offset (anywhere_in_line) == 0)
{ {
if (char_len) if (char_len != NULL)
*char_len = 0; *char_len = 0;
return NULL; return NULL;
} }
@ -4395,19 +4390,17 @@ _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
cache = priv->log_attr_cache; cache = priv->log_attr_cache;
line = gtk_text_iter_get_line (anywhere_in_line); line = gtk_text_iter_get_line (anywhere_in_line);
i = 0; for (i = 0; i < ATTR_CACHE_SIZE; i++)
while (i < ATTR_CACHE_SIZE)
{ {
if (cache->entries[i].attrs && if (cache->entries[i].attrs != NULL &&
cache->entries[i].line == line) cache->entries[i].line == line)
{ {
if (char_len) if (char_len != NULL)
*char_len = cache->entries[i].char_len; *char_len = cache->entries[i].char_len;
return cache->entries[i].attrs; return cache->entries[i].attrs;
} }
++i;
} }
/* Not in cache; open up the first cache entry */ /* Not in cache; open up the first cache entry */
g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs); g_free (cache->entries[ATTR_CACHE_SIZE-1].attrs);
@ -4418,9 +4411,9 @@ _gtk_text_buffer_get_line_log_attrs (GtkTextBuffer *buffer,
cache->entries[0].attrs = compute_log_attrs (anywhere_in_line, cache->entries[0].attrs = compute_log_attrs (anywhere_in_line,
&cache->entries[0].char_len); &cache->entries[0].char_len);
if (char_len) if (char_len != NULL)
*char_len = cache->entries[0].char_len; *char_len = cache->entries[0].char_len;
return cache->entries[0].attrs; return cache->entries[0].attrs;
} }

View File

@ -2911,9 +2911,9 @@ find_word_end_func (const PangoLogAttr *attrs,
static gboolean static gboolean
is_word_end_func (const PangoLogAttr *attrs, is_word_end_func (const PangoLogAttr *attrs,
gint offset, gint offset,
gint min_offset, gint min_offset,
gint len) gint len)
{ {
return attrs[offset].is_word_end; return attrs[offset].is_word_end;
} }
@ -2945,18 +2945,18 @@ find_word_start_func (const PangoLogAttr *attrs,
static gboolean static gboolean
is_word_start_func (const PangoLogAttr *attrs, is_word_start_func (const PangoLogAttr *attrs,
gint offset, gint offset,
gint min_offset, gint min_offset,
gint len) gint len)
{ {
return attrs[offset].is_word_start; return attrs[offset].is_word_start;
} }
static gboolean static gboolean
inside_word_func (const PangoLogAttr *attrs, inside_word_func (const PangoLogAttr *attrs,
gint offset, gint offset,
gint min_offset, gint min_offset,
gint len) gint len)
{ {
/* Find next word start or end */ /* Find next word start or end */
while (offset >= min_offset && while (offset >= min_offset &&
@ -2998,9 +2998,9 @@ find_sentence_end_func (const PangoLogAttr *attrs,
static gboolean static gboolean
is_sentence_end_func (const PangoLogAttr *attrs, is_sentence_end_func (const PangoLogAttr *attrs,
gint offset, gint offset,
gint min_offset, gint min_offset,
gint len) gint len)
{ {
return attrs[offset].is_sentence_end; return attrs[offset].is_sentence_end;
} }
@ -3032,18 +3032,18 @@ find_sentence_start_func (const PangoLogAttr *attrs,
static gboolean static gboolean
is_sentence_start_func (const PangoLogAttr *attrs, is_sentence_start_func (const PangoLogAttr *attrs,
gint offset, gint offset,
gint min_offset, gint min_offset,
gint len) gint len)
{ {
return attrs[offset].is_sentence_start; return attrs[offset].is_sentence_start;
} }
static gboolean static gboolean
inside_sentence_func (const PangoLogAttr *attrs, inside_sentence_func (const PangoLogAttr *attrs,
gint offset, gint offset,
gint min_offset, gint min_offset,
gint len) gint len)
{ {
/* Find next sentence start or end */ /* Find next sentence start or end */
while (offset >= min_offset && while (offset >= min_offset &&
@ -3059,7 +3059,7 @@ test_log_attrs (const GtkTextIter *iter,
{ {
gint char_len; gint char_len;
const PangoLogAttr *attrs; const PangoLogAttr *attrs;
int offset; gint offset;
gboolean result = FALSE; gboolean result = FALSE;
g_return_val_if_fail (iter != NULL, FALSE); g_return_val_if_fail (iter != NULL, FALSE);
@ -3071,12 +3071,11 @@ test_log_attrs (const GtkTextIter *iter,
/* char_len may be 0 and attrs will be NULL if so, if /* char_len may be 0 and attrs will be NULL if so, if
* iter is the end iter and the last line is empty. * iter is the end iter and the last line is empty.
* *
* offset may be equal to char_len, since attrs contains an entry * offset may be equal to char_len, since attrs contains an entry
* for one past the end * for one past the end.
*/ */
if (attrs != NULL && offset <= char_len)
if (attrs && offset <= char_len)
result = (* func) (attrs, offset, 0, char_len); result = (* func) (attrs, offset, 0, char_len);
return result; return result;
@ -3090,7 +3089,7 @@ find_line_log_attrs (const GtkTextIter *iter,
{ {
gint char_len; gint char_len;
const PangoLogAttr *attrs; const PangoLogAttr *attrs;
int offset; gint offset;
gboolean result = FALSE; gboolean result = FALSE;
g_return_val_if_fail (iter != NULL, FALSE); g_return_val_if_fail (iter != NULL, FALSE);
@ -3101,10 +3100,9 @@ find_line_log_attrs (const GtkTextIter *iter,
offset = gtk_text_iter_get_line_offset (iter); offset = gtk_text_iter_get_line_offset (iter);
/* char_len may be 0 and attrs will be NULL if so, if /* char_len may be 0 and attrs will be NULL if so, if
* iter is the end iter and the last line is empty * iter is the end iter and the last line is empty.
*/ */
if (attrs != NULL)
if (attrs)
result = (* func) (attrs, offset, char_len, found_offset, result = (* func) (attrs, offset, char_len, found_offset,
already_moved_initially); already_moved_initially);