Merge pull request #735 from crayxt/patch-1
[geany-mirror.git] / src / document.h
blobf601f4e16615365fafde59345fb21312d01d4e74
1 /*
2 * document.h - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 /**
23 * @file document.h
24 * Document related actions: new, save, open, etc.
25 **/
26 /* Also Scintilla search actions - but these should probably be moved to search.c. */
29 #ifndef GEANY_DOCUMENT_H
30 #define GEANY_DOCUMENT_H 1
32 #include "editor.h"
33 #include "filetypes.h"
34 #include "geany.h"
35 #include "search.h"
37 #include "gtkcompat.h" /* Needed by ScintillaWidget.h */
38 #include "Scintilla.h" /* Needed by ScintillaWidget.h */
39 #include "ScintillaWidget.h" /* For ScintillaObject */
41 #include <glib.h>
44 G_BEGIN_DECLS
46 /** File Prefs. */
47 typedef struct GeanyFilePrefs
49 gint default_new_encoding;
50 gint default_open_encoding;
51 gboolean final_new_line;
52 gboolean strip_trailing_spaces;
53 gboolean replace_tabs;
54 gboolean tab_order_ltr;
55 gboolean tab_order_beside;
56 gboolean show_tab_cross;
57 guint mru_length;
58 gint default_eol_character;
59 gint disk_check_timeout;
60 gboolean cmdline_new_files; /* New file if command-line filename doesn't exist */
61 gboolean use_safe_file_saving;
62 gboolean ensure_convert_new_lines;
63 gboolean gio_unsafe_save_backup;
64 gboolean use_gio_unsafe_file_saving; /* whether to use GIO as the unsafe backend */
65 gchar *extract_filetype_regex; /* regex to extract filetype on opening */
66 gboolean tab_close_switch_to_mru;
67 gboolean keep_edit_history_on_reload; /* Keep undo stack upon, and allow undoing of, document reloading. */
68 gboolean show_keep_edit_history_on_reload_msg; /* whether to show the message introducing the above feature */
70 GeanyFilePrefs;
73 /**
74 * Structure for representing an open tab with all its properties.
75 **/
76 typedef struct GeanyDocument
78 /** Flag used to check if this document is valid when iterating @ref documents_array. */
79 gboolean is_valid;
80 gint index; /**< Index in the documents array. */
81 /** Whether this document supports source code symbols(tags) to show in the sidebar. */
82 gboolean has_tags;
83 /** The UTF-8 encoded file name.
84 * Be careful; glibc and GLib file functions expect the locale representation of the
85 * file name which can be different from this.
86 * For conversion into locale encoding, you can use @ref utils_get_locale_from_utf8().
87 * @see real_path. */
88 gchar *file_name;
89 /** The encoding of the document, must be a valid string representation of an encoding, can
90 * be retrieved with @ref encodings_get_charset_from_index. */
91 gchar *encoding;
92 /** Internally used flag to indicate whether the file of this document has a byte-order-mark. */
93 gboolean has_bom;
94 GeanyEditor *editor; /**< The editor associated with the document. */
95 /** The filetype for this document, it's only a reference to one of the elements of the global
96 * filetypes array. */
97 GeanyFiletype *file_type;
98 /** TMSourceFile object for this document, or @c NULL. */
99 TMSourceFile *tm_file;
100 /** Whether this document is read-only. */
101 gboolean readonly;
102 /** Whether this document has been changed since it was last saved. */
103 gboolean changed;
104 /** The link-dereferenced, locale-encoded file name.
105 * If non-NULL, this indicates the file once existed on disk (not just as an
106 * unsaved document with a filename set).
108 * @note This is only assigned after a successful save or open - it should
109 * not be set elsewhere.
110 * @see file_name. */
111 gchar *real_path;
112 /** A pseudo-unique ID for this document.
113 * @c 0 is reserved as an unused value.
114 * @see document_find_by_id(). */
115 guint id;
117 struct GeanyDocumentPrivate *priv; /* should be last, append fields before this item */
119 GeanyDocument;
121 extern GPtrArray *documents_array;
123 /** Wraps @ref documents_array so it can be used with C array syntax.
124 * @warning Always check the returned document is valid (@c doc->is_valid).
126 * Example: @code GeanyDocument *doc = documents[i]; @endcode
127 * @see documents_array(). */
128 #define documents ((GeanyDocument **)GEANY(documents_array)->pdata)
130 /** @deprecated Use @ref foreach_document() instead.
131 * Iterates all valid documents.
132 * Use like a @c for statement.
133 * @param i @c guint index for document_index(). */
134 #ifndef GEANY_DISABLE_DEPRECATED
135 #define documents_foreach(i) foreach_document(i)
136 #endif
138 /** Iterates all valid document indexes.
139 * Use like a @c for statement.
140 * @param i @c guint index for @ref documents_array.
142 * Example:
143 * @code
144 * guint i;
145 * foreach_document(i)
147 * GeanyDocument *doc = documents[i];
148 * g_assert(doc->is_valid);
150 * @endcode */
151 #define foreach_document(i) \
152 for (i = 0; i < GEANY(documents_array)->len; i++)\
153 if (!documents[i]->is_valid)\
155 else /* prevent outside 'else' matching our macro 'if' */
157 /** Null-safe way to check @ref GeanyDocument::is_valid.
158 * @note This should not be used to check the result of the main API functions,
159 * these only need a NULL-pointer check - @c document_get_current() != @c NULL. */
160 #define DOC_VALID(doc_ptr) \
161 ((doc_ptr) != NULL && (doc_ptr)->is_valid)
164 * Returns the filename of the document passed or @c GEANY_STRING_UNTITLED
165 * (e.g. _("untitled")) if the document's filename was not yet set.
166 * This macro never returns @c NULL.
168 #define DOC_FILENAME(doc) \
169 (G_LIKELY((doc)->file_name != NULL) ? ((doc)->file_name) : GEANY_STRING_UNTITLED)
172 GeanyDocument* document_new_file(const gchar *filename, GeanyFiletype *ft, const gchar *text);
174 GeanyDocument *document_get_current(void);
176 GeanyDocument *document_get_from_notebook_child(GtkWidget *page);
178 GeanyDocument* document_get_from_page(guint page_num);
180 GeanyDocument* document_find_by_filename(const gchar *utf8_filename);
182 GeanyDocument* document_find_by_real_path(const gchar *realname);
184 gboolean document_save_file(GeanyDocument *doc, gboolean force);
186 GeanyDocument* document_open_file(const gchar *locale_filename, gboolean readonly,
187 GeanyFiletype *ft, const gchar *forced_enc);
189 void document_open_files(const GSList *filenames, gboolean readonly, GeanyFiletype *ft,
190 const gchar *forced_enc);
192 gboolean document_remove_page(guint page_num);
194 gboolean document_reload_force(GeanyDocument *doc, const gchar *forced_enc);
196 void document_set_encoding(GeanyDocument *doc, const gchar *new_encoding);
198 void document_set_text_changed(GeanyDocument *doc, gboolean changed);
200 void document_set_filetype(GeanyDocument *doc, GeanyFiletype *type);
202 gboolean document_close(GeanyDocument *doc);
204 GeanyDocument *document_index(gint idx);
206 gboolean document_save_file_as(GeanyDocument *doc, const gchar *utf8_fname);
208 void document_rename_file(GeanyDocument *doc, const gchar *new_filename);
210 const GdkColor *document_get_status_color(GeanyDocument *doc);
212 gchar *document_get_basename_for_display(GeanyDocument *doc, gint length);
214 gint document_get_notebook_page(GeanyDocument *doc);
216 gint document_compare_by_display_name(gconstpointer a, gconstpointer b);
218 gint document_compare_by_tab_order(gconstpointer a, gconstpointer b);
220 gint document_compare_by_tab_order_reverse(gconstpointer a, gconstpointer b);
222 GeanyDocument *document_find_by_id(guint id);
225 #ifdef GEANY_PRIVATE
227 #if defined(G_OS_WIN32)
228 # define GEANY_DEFAULT_EOL_CHARACTER SC_EOL_CRLF
229 #else
230 # define GEANY_DEFAULT_EOL_CHARACTER SC_EOL_LF
231 #endif
233 extern GeanyFilePrefs file_prefs;
236 /* These functions will replace the older functions. For now they have a documents_ prefix. */
238 GeanyDocument* document_new_file_if_non_open(void);
240 gboolean document_reload_prompt(GeanyDocument *doc, const gchar *forced_enc);
242 void document_reload_config(GeanyDocument *doc);
244 GeanyDocument *document_find_by_sci(ScintillaObject *sci);
246 void document_show_tab(GeanyDocument *doc);
248 void document_init_doclist(void);
250 void document_finalize(void);
252 void document_try_focus(GeanyDocument *doc, GtkWidget *source_widget);
254 gboolean document_account_for_unsaved(void);
256 gboolean document_close_all(void);
258 GeanyDocument *document_open_file_full(GeanyDocument *doc, const gchar *filename, gint pos,
259 gboolean readonly, GeanyFiletype *ft, const gchar *forced_enc);
261 void document_open_file_list(const gchar *data, gsize length);
263 gboolean document_search_bar_find(GeanyDocument *doc, const gchar *text, gboolean inc,
264 gboolean backwards);
266 gint document_find_text(GeanyDocument *doc, const gchar *text, const gchar *original_text,
267 GeanyFindFlags flags, gboolean search_backwards, GeanyMatchInfo **match_,
268 gboolean scroll, GtkWidget *parent);
270 gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gchar *original_find_text,
271 const gchar *replace_text, GeanyFindFlags flags, gboolean search_backwards);
273 gint document_replace_all(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
274 const gchar *original_find_text, const gchar *original_replace_text, GeanyFindFlags flags);
276 void document_replace_sel(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
277 const gchar *original_find_text, const gchar *original_replace_text, GeanyFindFlags flags);
279 void document_update_tags(GeanyDocument *doc);
281 void document_update_tag_list_in_idle(GeanyDocument *doc);
283 void document_highlight_tags(GeanyDocument *doc);
285 gboolean document_check_disk_status(GeanyDocument *doc, gboolean force);
287 /* own Undo / Redo implementation to be able to undo / redo changes
288 * to the encoding or the Unicode BOM (which are Scintilla independent).
289 * All Scintilla events are stored in the undo / redo buffer and are passed through. */
291 gboolean document_can_undo(GeanyDocument *doc);
293 gboolean document_can_redo(GeanyDocument *doc);
295 void document_undo(GeanyDocument *doc);
297 void document_redo(GeanyDocument *doc);
299 void document_undo_add(GeanyDocument *doc, guint type, gpointer data);
301 void document_update_tab_label(GeanyDocument *doc);
303 const gchar *document_get_status_widget_class(GeanyDocument *doc);
305 gboolean document_need_save_as(GeanyDocument *doc);
307 gboolean document_detect_indent_type(GeanyDocument *doc, GeanyIndentType *type_);
309 gboolean document_detect_indent_width(GeanyDocument *doc, gint *width_);
311 void document_apply_indent_settings(GeanyDocument *doc);
313 void document_grab_focus(GeanyDocument *doc);
315 GeanyDocument *document_clone(GeanyDocument *old_doc);
317 #endif /* GEANY_PRIVATE */
319 G_END_DECLS
321 #endif /* GEANY_DOCUMENT_H */