diff --git a/ChangeLog b/ChangeLog index 64ce7979..39fa1096 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,15 @@ Make doc_at() debug function check idx is within range. * src/build.c, src/search.c: Minor formatting. + * src/build.c, src/prefs.c, src/plugindata.h, src/callbacks.c, + src/search.c, src/document.c, src/document.h, src/ui_utils.c: + Bring back GeanyDocument::is_valid field as it is clearer and more + descriptive than using doc->index != -1. + Add deprecated macros DOC_IDX_VALID and DOC_IDX in plugindata.h, + which can make porting outside plugins easier; of course, it is + better to rewrite the code to use document pointers. + Use is_valid instead of the DOC_VALID macro when iterating over + documents_array, as there are never NULL pointers in it. 2008-06-16 Enrico Tröger diff --git a/src/build.c b/src/build.c index b5ac2103..9cf27cd7 100644 --- a/src/build.c +++ b/src/build.c @@ -390,7 +390,7 @@ static void clear_errors(GeanyDocument *doc) for (i = 0; i < documents_array->len; i++) { - if (DOC_VALID(documents[i])) + if (documents[i]->is_valid) editor_clear_indicators(documents[i]); } break; diff --git a/src/callbacks.c b/src/callbacks.c index 56cd4150..63a1e1ef 100644 --- a/src/callbacks.c +++ b/src/callbacks.c @@ -97,7 +97,7 @@ static gboolean check_no_unsaved(void) for (i = 0; i < documents_array->len; i++) { - if (DOC_VALID(documents[i]) && documents[i]->changed) + if (documents[i]->is_valid && documents[i]->changed) { return FALSE; } diff --git a/src/document.c b/src/document.c index be4fa229..d1713d46 100644 --- a/src/document.c +++ b/src/document.c @@ -127,7 +127,7 @@ GeanyDocument* document_find_by_real_path(const gchar *realname) { GeanyDocument *doc = documents[i]; - if (documents[i]->index == -1 || ! doc->real_path) continue; + if (! documents[i]->is_valid || ! doc->real_path) continue; if (filenamecmp(realname, doc->real_path) == 0) { @@ -175,7 +175,7 @@ GeanyDocument *document_find_by_filename(const gchar *utf8_filename) { doc = documents[i]; - if (documents[i]->index == -1 || doc->file_name == NULL) continue; + if (! documents[i]->is_valid || doc->file_name == NULL) continue; if (filenamecmp(utf8_filename, doc->file_name) == 0) { @@ -200,7 +200,7 @@ GeanyDocument *document_find_by_sci(ScintillaObject *sci) for (i = 0; i < documents_array->len; i++) { - if (documents[i]->index != -1 && documents[i]->sci == sci) + if (documents[i]->is_valid && documents[i]->sci == sci) return documents[i]; } return NULL; @@ -327,15 +327,15 @@ void document_apply_update_prefs(GeanyDocument *doc) } -/* Sets index to -1 and initializes some members to NULL, to mark it uninitialized. - * The document index is set in document_create() to mark it as valid. */ +/* Sets is_valid to FALSE and initializes some members to NULL, to mark it uninitialized. + * The flag is_valid is set to TRUE in document_create(). */ static void init_doc_struct(GeanyDocument *new_doc) { Document *full_doc = DOCUMENT(new_doc); memset(full_doc, 0, sizeof(Document)); - new_doc->index = -1; + new_doc->is_valid = FALSE; new_doc->has_tags = FALSE; new_doc->auto_indent = (editor_prefs.indent_mode != INDENT_NONE); new_doc->line_wrapping = editor_prefs.line_wrapping; @@ -477,6 +477,7 @@ static GeanyDocument *document_create(const gchar *utf8_filename) } this = documents[new_idx]; init_doc_struct(this); /* initialize default document settings */ + this->index = new_idx; this->file_name = g_strdup(utf8_filename); @@ -504,7 +505,7 @@ static GeanyDocument *document_create(const gchar *utf8_filename) ui_document_buttons_update(); - this->index = new_idx; /* do this last to prevent UI updating with NULL items. */ + this->is_valid = TRUE; /* do this last to prevent UI updating with NULL items. */ return this; } @@ -543,7 +544,7 @@ gboolean document_remove_page(guint page_num) g_free(doc->real_path); tm_workspace_remove_object(doc->tm_file, TRUE, TRUE); - doc->index = -1; + doc->is_valid = FALSE; doc->sci = NULL; doc->file_name = NULL; doc->real_path = NULL; @@ -2487,7 +2488,7 @@ void document_delay_colourise() doc_indexes = g_array_new(FALSE, FALSE, sizeof(gint)); for (n = 0; n < (gint) documents_array->len; n++) { - if (DOC_VALID(documents[n])) + if (documents[n]->is_valid) g_array_append_val(doc_indexes, n); } delay_colourise = TRUE; @@ -2527,7 +2528,7 @@ void document_colourise_new() /* colourise all in the doc_set */ for (n = 0; n < documents_array->len; n++) { - if (doc_set[n] && documents[n]->index != -1) + if (doc_set[n] && documents[n]->is_valid) sci_colourise(documents[n]->sci, 0, -1); } delay_colourise = FALSE; @@ -2589,7 +2590,7 @@ gboolean document_account_for_unsaved(void) /* all documents should now be accounted for, so ignore any changes */ for (i = 0; i < len; i++) { - if (documents[i]->index != -1 && documents[i]->changed) + if (documents[i]->is_valid && documents[i]->changed) { documents[i]->changed = FALSE; } @@ -2605,7 +2606,7 @@ static void force_close_all(void) /* check all documents have been accounted for */ for (i = 0; i < len; i++) { - if (documents[i]->index != -1) + if (documents[i]->is_valid) { g_return_if_fail(!documents[i]->changed); } diff --git a/src/document.h b/src/document.h index 476fc344..677b35b9 100644 --- a/src/document.h +++ b/src/document.h @@ -69,6 +69,8 @@ extern GeanyFilePrefs file_prefs; **/ struct GeanyDocument { + /** General flag to represent this document is active and all properties are set correctly. */ + gboolean is_valid; gint index; /**< Index in the documents array. */ /** Whether this %document support source code symbols(tags) to show in the sidebar. */ gboolean has_tags; @@ -131,15 +133,9 @@ extern GPtrArray *documents_array; * This is useful when @a doc_ptr was stored some time earlier and documents may have been * closed since then. * @note This should not be used to check the result of the main API functions, - * these only need a NULL-pointer check - @c p_document->get_current() != @c NULL. - * This is only useful when iterating over the documents array or when using stored document - * pointers like in msgwindow treeviews. */ + * these only need a NULL-pointer check - @c p_document->get_current() != @c NULL. */ #define DOC_VALID(doc_ptr) \ - ((doc_ptr) != NULL && (doc_ptr)->index != -1) - -/** NULL-safe way to get the index of @a doc_ptr in the documents array. */ -#define DOC_IDX(doc_ptr) \ - (doc_ptr ? doc_ptr->index : -1) + ((doc_ptr) != NULL && (doc_ptr)->is_valid) /** * DOC_FILENAME returns the filename of the %document corresponding to the passed index or diff --git a/src/plugindata.h b/src/plugindata.h index d57f10ee..bd5eeee7 100644 --- a/src/plugindata.h +++ b/src/plugindata.h @@ -36,12 +36,12 @@ /* The API version should be incremented whenever any plugin data types below are * modified or appended to. */ -static const gint api_version = 69; +static const gint api_version = 70; /* The ABI version should be incremented whenever existing fields in the plugin * data types below have to be changed or reordered. It should stay the same if fields * are only appended, as this doesn't affect existing fields. */ -static const gint abi_version = 38; +static const gint abi_version = 39; /** Check the plugin can be loaded by Geany. * This performs runtime checks that try to ensure: @@ -460,6 +460,12 @@ typedef PluginCallback GeanyCallback; #define doc_array documents_array +/** NULL-safe way to get the index of @a doc_ptr in the documents array. */ +#define DOC_IDX(doc_ptr) \ + (doc_ptr ? doc_ptr->index : -1) +#define DOC_IDX_VALID(doc_idx) \ + ((doc_idx) >= 0 && (guint)(doc_idx) < documents_array->len && documents[doc_idx]->is_valid) + #endif /* GEANY_DISABLE_DEPRECATED */ #endif diff --git a/src/prefs.c b/src/prefs.c index 7d13b59a..8fce8837 100644 --- a/src/prefs.c +++ b/src/prefs.c @@ -824,7 +824,7 @@ on_prefs_button_clicked(GtkDialog *dialog, gint response, gpointer user_data) editor_prefs.use_tabs = use_tabs; for (i = 0; i < documents_array->len; i++) { - if (DOC_VALID(documents[i])) + if (documents[i]->is_valid) editor_set_use_tabs(documents[i], editor_prefs.use_tabs); } } @@ -977,7 +977,7 @@ on_prefs_button_clicked(GtkDialog *dialog, gint response, gpointer user_data) /* re-colourise all open documents, if tab width or long line settings have changed */ for (i = 0; i < documents_array->len; i++) { - if (DOC_VALID(documents[i])) + if (documents[i]->is_valid) { document_apply_update_prefs(documents[i]); if (! editor_prefs.folding) @@ -1046,7 +1046,7 @@ void on_prefs_font_choosed(GtkFontButton *widget, gpointer user_data) { Document *fdoc = DOCUMENT(documents[i]); - if (DOC_VALID(documents[i]) && GTK_IS_WIDGET(fdoc->tag_tree)) + if (documents[i]->is_valid && GTK_IS_WIDGET(fdoc->tag_tree)) ui_widget_modify_font_from_string(fdoc->tag_tree, interface_prefs.tagbar_font); } diff --git a/src/search.c b/src/search.c index f68bd442..84438049 100644 --- a/src/search.c +++ b/src/search.c @@ -1407,7 +1407,7 @@ void search_find_usage(const gchar *search_text, gint flags, gboolean in_session guint i; for (i = 0; i < documents_array->len; i++) { - if (DOC_VALID(documents[i])) + if (documents[i]->is_valid) if (find_document_usage(documents[i], search_text, flags) > 0) found = TRUE; } diff --git a/src/ui_utils.c b/src/ui_utils.c index a5c631b2..9ae217da 100644 --- a/src/ui_utils.c +++ b/src/ui_utils.c @@ -555,7 +555,7 @@ void ui_save_buttons_toggle(gboolean enable) { /* check whether there are files where changes were made and if there are some, * we need the save all button / item */ - if (DOC_VALID(documents[i]) && documents[i]->changed) + if (documents[i]->is_valid && documents[i]->changed) { dirty_tabs = TRUE; break;