geany  1.38
About: Geany is a text editor (using GTK2) with basic features of an integrated development environment (syntax highlighting, code folding, symbol name auto-completion, ...). F: office T: editor programming GTK+ IDE
  Fossies Dox: geany-1.38.tar.bz2  ("unofficial" and yet experimental doxygen-generated source code documentation)  

document.c
Go to the documentation of this file.
1/*
2 * document.c - this file is part of Geany, a fast and lightweight IDE
3 *
4 * Copyright 2005 The Geany contributors
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19 */
20
21/*
22 * Document related actions: new, save, open, etc.
23 * Also Scintilla search actions.
24 */
25
26#ifdef HAVE_CONFIG_H
27# include "config.h"
28#endif
29
30#include "document.h"
31
32#include "app.h"
33#include "callbacks.h" /* for ignore_callback */
34#include "dialogs.h"
35#include "documentprivate.h"
36#include "encodings.h"
37#include "encodingsprivate.h"
38#include "filetypesprivate.h"
39#include "geany.h" /* FIXME: why is this needed for DOC_FILENAME()? should come from documentprivate.h/document.h */
40#include "geanyobject.h"
41#include "geanywraplabel.h"
42#include "highlighting.h"
43#include "main.h"
44#include "msgwindow.h"
45#include "navqueue.h"
46#include "notebook.h"
47#include "project.h"
48#include "sciwrappers.h"
49#include "sidebar.h"
50#include "support.h"
51#include "symbols.h"
52#include "ui_utils.h"
53#include "utils.h"
54#include "vte.h"
55#include "win32.h"
56
57#ifdef HAVE_SYS_TIME_H
58# include <sys/time.h>
59#endif
60#include <time.h>
61
62#include <unistd.h>
63#include <string.h>
64#include <errno.h>
65
66#ifdef HAVE_SYS_TYPES_H
67# include <sys/types.h>
68#endif
69
70#include <stdlib.h>
71
72/* gstdio.h also includes sys/stat.h */
73#include <glib/gstdio.h>
74
75/* uncomment to use GIO based file monitoring, though it is not completely stable yet */
76/*#define USE_GIO_FILEMON 1*/
77#include <gio/gio.h>
78
79#include <gtk/gtk.h>
80#include <gdk/gdkkeysyms.h>
81
82
83#define USE_GIO_FILE_OPERATIONS (!file_prefs.use_safe_file_saving && file_prefs.use_gio_unsafe_file_saving)
84
85
87GPtrArray *documents_array = NULL;
88
89
90/* an undo action, also used for redo actions */
91typedef struct
92{
93 GTrashStack *next; /* pointer to the next stack element(required for the GTrashStack) */
94 guint type; /* to identify the action */
95 gpointer *data; /* the old value (before the change), in case of a redo action
96 * it contains the new value */
98
99/* Custom document info bar response IDs */
100enum
101{
104};
105
106
107static guint doc_id_counter = 0;
108
109
110static void document_undo_clear_stack(GTrashStack **stack);
111static void document_undo_clear(GeanyDocument *doc);
112static void document_undo_add_internal(GeanyDocument *doc, guint type, gpointer data);
113static void document_redo_add(GeanyDocument *doc, guint type, gpointer data);
114static gboolean remove_page(guint page_num);
115static GtkWidget* document_show_message(GeanyDocument *doc, GtkMessageType msgtype,
116 void (*response_cb)(GtkWidget *info_bar, gint response_id, GeanyDocument *doc),
117 const gchar *btn_1, GtkResponseType response_1,
118 const gchar *btn_2, GtkResponseType response_2,
119 const gchar *btn_3, GtkResponseType response_3,
120 const gchar *extra_text, const gchar *format, ...) G_GNUC_PRINTF(11, 12);
121
122
123/**
124 * Finds a document whose @c real_path field matches the given filename.
125 *
126 * @param realname The filename to search, which should be identical to the
127 * string returned by @c utils_get_real_path().
128 *
129 * @return @transfer{none} @nullable The matching document, or @c NULL.
130 * @note This is only really useful when passing a @c TMSourceFile::file_name.
131 * @see GeanyDocument::real_path.
132 * @see document_find_by_filename().
133 *
134 * @since 0.15
135 **/
136GEANY_API_SYMBOL
138{
139 guint i;
140
141 if (! realname)
142 return NULL; /* file doesn't exist on disk */
143
144 for (i = 0; i < documents_array->len; i++)
145 {
146 GeanyDocument *doc = documents[i];
147
148 if (! doc->is_valid || ! doc->real_path)
149 continue;
150
151 if (utils_filenamecmp(realname, doc->real_path) == 0)
152 {
153 return doc;
154 }
155 }
156 return NULL;
157}
158
159
160/* dereference symlinks, /../ junk in path and return locale encoding */
161static gchar *get_real_path_from_utf8(const gchar *utf8_filename)
162{
163 gchar *locale_name = utils_get_locale_from_utf8(utf8_filename);
164 gchar *realname = utils_get_real_path(locale_name);
165
166 g_free(locale_name);
167 return realname;
168}
169
170
171/**
172 * Finds a document with the given filename.
173 * This matches either an exact GeanyDocument::file_name string, or variant
174 * filenames with relative elements in the path (e.g. @c "/dir/..//name" will
175 * match @c "/name").
176 *
177 * @param utf8_filename The filename to search (in UTF-8 encoding).
178 *
179 * @return @transfer{none} @nullable The matching document, or @c NULL.
180 * @see document_find_by_real_path().
181 **/
182GEANY_API_SYMBOL
183GeanyDocument *document_find_by_filename(const gchar *utf8_filename)
184{
185 guint i;
186 GeanyDocument *doc;
187 gchar *realname;
188
189 g_return_val_if_fail(utf8_filename != NULL, NULL);
190
191 /* First search GeanyDocument::file_name, so we can find documents with a
192 * filename set but not saved on disk, like vcdiff produces */
193 for (i = 0; i < documents_array->len; i++)
194 {
195 doc = documents[i];
196
197 if (! doc->is_valid || doc->file_name == NULL)
198 continue;
199
200 if (utils_filenamecmp(utf8_filename, doc->file_name) == 0)
201 {
202 return doc;
203 }
204 }
205 /* Now try matching based on the realpath(), which is unique per file on disk */
206 realname = get_real_path_from_utf8(utf8_filename);
207 doc = document_find_by_real_path(realname);
208 g_free(realname);
209 return doc;
210}
211
212
213/* returns the document which has sci, or NULL. */
215{
216 guint i;
217
218 g_return_val_if_fail(sci != NULL, NULL);
219
220 for (i = 0; i < documents_array->len; i++)
221 {
222 if (documents[i]->is_valid && documents[i]->editor->sci == sci)
223 return documents[i];
224 }
225 return NULL;
226}
227
228
229/** Lookup an old document by its ID.
230 * Useful when the corresponding document may have been closed since the
231 * ID was retrieved.
232 * @param id The ID of the document to find
233 * @return @transfer{none} @c NULL if the document is no longer open.
234 *
235 * Example:
236 * @code
237 * static guint id;
238 * GeanyDocument *doc = ...;
239 * id = doc->id; // store ID
240 * ...
241 * // time passes - the document may have been closed by now
242 * GeanyDocument *doc = document_find_by_id(id);
243 * gboolean still_open = (doc != NULL);
244 * @endcode
245 * @since 1.25. */
246GEANY_API_SYMBOL
248{
249 guint i;
250
251 if (!id)
252 return NULL;
253
255 {
256 if (documents[i]->id == id)
257 return documents[i];
258 }
259 return NULL;
260}
261
262
263/* gets the widget the main_widgets.notebook consider is its child for this document */
265{
266 GtkWidget *parent;
267 GtkWidget *child;
268
269 g_return_val_if_fail(doc != NULL, NULL);
270
271 child = GTK_WIDGET(doc->editor->sci);
272 parent = gtk_widget_get_parent(child);
273 /* search for the direct notebook child, mirroring document_get_from_page() */
274 while (parent && ! GTK_IS_NOTEBOOK(parent))
275 {
276 child = parent;
277 parent = gtk_widget_get_parent(child);
278 }
279
280 return child;
281}
282
283
284/** Gets the notebook page index for a document.
285 * @param doc The document.
286 * @return The index.
287 * @since 0.19 */
288GEANY_API_SYMBOL
290{
291 GtkWidget *child = document_get_notebook_child(doc);
292
293 return gtk_notebook_page_num(GTK_NOTEBOOK(main_widgets.notebook), child);
294}
295
296
297/*
298 * Recursively searches a containers children until it finds a
299 * Scintilla widget, or NULL if one was not found.
300 */
301static ScintillaObject *locate_sci_in_container(GtkWidget *container)
302{
303 ScintillaObject *sci = NULL;
304 GList *children, *iter;
305
306 g_return_val_if_fail(GTK_IS_CONTAINER(container), NULL);
307
308 children = gtk_container_get_children(GTK_CONTAINER(container));
309 for (iter = children; iter != NULL; iter = g_list_next(iter))
310 {
311 if (IS_SCINTILLA(iter->data))
312 {
313 sci = SCINTILLA(iter->data);
314 break;
315 }
316 else if (GTK_IS_CONTAINER(iter->data))
317 {
318 sci = locate_sci_in_container(iter->data);
319 if (IS_SCINTILLA(sci))
320 break;
321 sci = NULL;
322 }
323 }
324 g_list_free(children);
325
326 return sci;
327}
328
329
330/* Finds the document for the given notebook page widget */
332{
333 ScintillaObject *sci;
334
335 g_return_val_if_fail(GTK_IS_BOX(page), NULL);
336
338 g_return_val_if_fail(IS_SCINTILLA(sci), NULL);
339
341}
342
343
344/**
345 * Finds the document for the given notebook page @a page_num.
346 *
347 * @param page_num The notebook page number to search.
348 *
349 * @return @transfer{none} @nullable The corresponding document for the given notebook page, or @c NULL.
350 **/
351GEANY_API_SYMBOL
353{
354 GtkWidget *parent;
355
356 if (page_num >= documents_array->len)
357 return NULL;
358
359 parent = gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.notebook), page_num);
360
362}
363
364
365/**
366 * Finds the current document.
367 *
368 * @return @transfer{none} @nullable A pointer to the current document or @c NULL if there are no opened documents.
369 **/
370GEANY_API_SYMBOL
372{
373 gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
374
375 if (cur_page == -1)
376 return NULL;
377 else
378 return document_get_from_page((guint) cur_page);
379}
380
381
383{
384 documents_array = g_ptr_array_new();
385}
386
387
389{
390 guint i;
391
392 for (i = 0; i < documents_array->len; i++)
393 g_free(documents[i]);
394 g_ptr_array_free(documents_array, TRUE);
395}
396
397
398/**
399 * Returns the last part of the filename of the given GeanyDocument. The result is also
400 * truncated to a maximum of @a length characters in case the filename is very long.
401 *
402 * @param doc The document to use.
403 * @param length The length of the resulting string or -1 to use a default value.
404 *
405 * @return The ellipsized last part of the filename of @a doc, should be freed when no
406 * longer needed.
407 *
408 * @since 0.17
409 */
410/* TODO make more use of this */
411GEANY_API_SYMBOL
413{
414 gchar *base_name, *short_name;
415
416 g_return_val_if_fail(doc != NULL, NULL);
417
418 if (length < 0)
419 length = 30;
420
421 base_name = g_path_get_basename(DOC_FILENAME(doc));
422 short_name = utils_str_middle_truncate(base_name, (guint)length);
423
424 g_free(base_name);
425
426 return short_name;
427}
428
429
431{
432 gchar *short_name;
433 GtkWidget *parent;
434
435 g_return_if_fail(doc != NULL);
436
437 short_name = document_get_basename_for_display(doc, -1);
438
439 /* we need to use the event box for the tooltip, labels don't get the necessary events */
440 parent = gtk_widget_get_parent(doc->priv->tab_label);
441 parent = gtk_widget_get_parent(parent);
442
443 gtk_label_set_text(GTK_LABEL(doc->priv->tab_label), short_name);
444
445 gtk_widget_set_tooltip_text(parent, DOC_FILENAME(doc));
446
447 g_free(short_name);
448}
449
450
451/**
452 * Updates the tab labels, the status bar, the window title and some save-sensitive buttons
453 * according to the document's save state.
454 * This is called by Geany mostly when opening or saving files.
455 *
456 * @param doc The document to use.
457 * @param changed Whether the document state should indicate changes have been made.
458 **/
459GEANY_API_SYMBOL
460void document_set_text_changed(GeanyDocument *doc, gboolean changed)
461{
462 g_return_if_fail(doc != NULL);
463
464 doc->changed = changed;
465
466 if (! main_status.quitting)
467 {
469 ui_save_buttons_toggle(changed);
471 ui_update_statusbar(doc, -1);
472 }
473}
474
475
476/* returns the next free place in the document list,
477 * or -1 if the documents_array is full */
478static gint document_get_new_idx(void)
479{
480 guint i;
481
482 for (i = 0; i < documents_array->len; i++)
483 {
484 if (documents[i]->editor == NULL)
485 {
486 return (gint) i;
487 }
488 }
489 return -1;
490}
491
492
494{
495 if (doc->priv->colourise_needed)
496 return;
497
498 /* Colourise the editor before it is next drawn */
499 doc->priv->colourise_needed = TRUE;
500
501 /* If the editor doesn't need drawing (e.g. after saving the current
502 * document), we need to force a redraw, so the expose event is triggered.
503 * This ensures we don't start colourising before all documents are opened/saved,
504 * only once the editor is drawn. */
505 gtk_widget_queue_draw(GTK_WIDGET(doc->editor->sci));
506}
507
508
509#ifdef USE_GIO_FILEMON
510static void monitor_file_changed_cb(G_GNUC_UNUSED GFileMonitor *monitor, G_GNUC_UNUSED GFile *file,
511 G_GNUC_UNUSED GFile *other_file, GFileMonitorEvent event,
512 GeanyDocument *doc)
513{
514 g_return_if_fail(doc != NULL);
515
517 return;
518
519 geany_debug("%s: event: %d previous file status: %d",
520 G_STRFUNC, event, doc->priv->file_disk_status);
521 switch (event)
522 {
523 case G_FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
524 {
525 if (doc->priv->file_disk_status == FILE_IGNORE)
527 else
529 g_message("%s: FILE_CHANGED", G_STRFUNC);
530 break;
531 }
532 case G_FILE_MONITOR_EVENT_DELETED:
533 {
535 g_message("%s: FILE_MISSING", G_STRFUNC);
536 break;
537 }
538 default:
539 break;
540 }
541 if (doc->priv->file_disk_status != FILE_OK)
542 {
544 }
545}
546#endif
547
548
550{
551 g_return_if_fail(doc != NULL);
552
553 if (doc->priv->monitor != NULL)
554 {
555 g_object_unref(doc->priv->monitor);
556 doc->priv->monitor = NULL;
557 }
558}
559
560
562{
563 g_return_if_fail(doc != NULL);
564 /* Disable file monitoring completely for remote files (i.e. remote GIO files) as GFileMonitor
565 * doesn't work at all for remote files and legacy polling is too slow. */
566 if (! doc->priv->is_remote)
567 {
568#ifdef USE_GIO_FILEMON
569 gchar *locale_filename;
570
571 /* stop any previous monitoring */
573
574 locale_filename = utils_get_locale_from_utf8(doc->file_name);
575 if (locale_filename != NULL && g_file_test(locale_filename, G_FILE_TEST_EXISTS))
576 {
577 /* get a file monitor and connect to the 'changed' signal */
578 GFile *file = g_file_new_for_path(locale_filename);
579 doc->priv->monitor = g_file_monitor_file(file, G_FILE_MONITOR_NONE, NULL, NULL);
580 g_signal_connect(doc->priv->monitor, "changed",
581 G_CALLBACK(monitor_file_changed_cb), doc);
582
583 /* we set the rate limit according to the GUI pref but it's most probably not used */
584 g_file_monitor_set_rate_limit(doc->priv->monitor, file_prefs.disk_check_timeout * 1000);
585
586 g_object_unref(file);
587 }
588 g_free(locale_filename);
589#endif
590 }
592}
593
594
595void document_try_focus(GeanyDocument *doc, GtkWidget *source_widget)
596{
597 /* doc might not be valid e.g. if user closed a tab whilst Geany is opening files */
598 if (DOC_VALID(doc))
599 {
600 GtkWidget *sci = GTK_WIDGET(doc->editor->sci);
601 GtkWidget *focusw = gtk_window_get_focus(GTK_WINDOW(main_widgets.window));
602
603 if (source_widget == NULL)
604 source_widget = doc->priv->tag_tree;
605
606 if (focusw == source_widget)
607 gtk_widget_grab_focus(sci);
608 }
609}
610
611
612static gboolean on_idle_focus(gpointer doc)
613{
615 return FALSE;
616}
617
618
619/* Creates a new document and editor, adding a tab in the notebook.
620 * @return The created document */
621static GeanyDocument *document_create(const gchar *utf8_filename)
622{
623 GeanyDocument *doc;
624 gint new_idx;
625 gint cur_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
626
627 if (cur_pages == 1)
628 {
629 doc = document_get_current();
630 /* remove the empty document first */
631 if (doc != NULL && doc->file_name == NULL && ! doc->changed)
632 /* prevent immediately opening another new doc with
633 * new_document_after_close pref */
634 remove_page(0);
635 }
636
637 new_idx = document_get_new_idx();
638 if (new_idx == -1) /* expand the array, no free places */
639 {
640 doc = g_new0(GeanyDocument, 1);
641
642 new_idx = documents_array->len;
643 g_ptr_array_add(documents_array, doc);
644 }
645
646 doc = documents[new_idx];
647
648 /* initialize default document settings */
649 doc->priv = g_new0(GeanyDocumentPrivate, 1);
650 doc->id = ++doc_id_counter;
651 doc->index = new_idx;
652 doc->file_name = g_strdup(utf8_filename);
653 doc->editor = editor_create(doc);
654#ifndef USE_GIO_FILEMON
655 doc->priv->last_check = time(NULL);
656#endif
657
658 g_datalist_init(&doc->priv->data);
659
660 sidebar_openfiles_add(doc); /* sets doc->iter */
661
662 notebook_new_tab(doc);
663
664 /* select document in sidebar */
665 {
666 GtkTreeSelection *sel;
667
668 sel = gtk_tree_view_get_selection(GTK_TREE_VIEW(tv.tree_openfiles));
669 gtk_tree_selection_select_iter(sel, &doc->priv->iter);
670 }
671
673
674 doc->is_valid = TRUE; /* do this last to prevent UI updating with NULL items. */
675 return doc;
676}
677
678
679/**
680 * Closes the given document.
681 *
682 * @param doc The document to remove.
683 *
684 * @return @c TRUE if the document was actually removed or @c FALSE otherwise.
685 *
686 * @since 0.15
687 **/
688GEANY_API_SYMBOL
690{
691 g_return_val_if_fail(doc, FALSE);
692
694}
695
696
697/* Call document_remove_page() instead, this is only needed for document_create()
698 * to prevent re-opening a new document when the last document is closed (if enabled). */
699static gboolean remove_page(guint page_num)
700{
701 GeanyDocument *doc = document_get_from_page(page_num);
702
703 g_return_val_if_fail(doc != NULL, FALSE);
704
705 /* if we're closing all, document_account_for_unsaved() has been called already, no need to ask again. */
706 if (! main_status.closing_all && doc->changed && ! dialogs_show_unsaved_file(doc))
707 return FALSE;
708
709 /* tell any plugins that the document is about to be closed */
710 g_signal_emit_by_name(geany_object, "document-close", doc);
711
712 /* Checking real_path makes it likely the file exists on disk */
713 if (! main_status.closing_all && doc->real_path != NULL)
715
716 g_datalist_clear(&doc->priv->data);
717
718 doc->is_valid = FALSE;
719 doc->id = 0;
720
721 if (main_status.quitting)
722 {
723 /* we need to destroy the ScintillaWidget so our handlers on it are
724 * disconnected before we free any data they may use (like the editor).
725 * when not quitting, this is handled by removing the notebook page. */
726 gtk_notebook_remove_page(GTK_NOTEBOOK(main_widgets.notebook), page_num);
727 }
728 else
729 {
730 notebook_remove_page(page_num);
733 msgwin_status_add(_("File %s closed."), DOC_FILENAME(doc));
734 }
735 g_free(doc->encoding);
736 g_free(doc->priv->saved_encoding.encoding);
737 g_free(doc->file_name);
738 g_free(doc->real_path);
739 if (doc->tm_file)
740 {
743 }
744
745 if (doc->priv->tag_tree)
746 gtk_widget_destroy(doc->priv->tag_tree);
747
749 doc->editor = NULL; /* needs to be NULL for document_undo_clear() call below */
750
752
754
755 g_free(doc->priv);
756
757 /* reset document settings to defaults for re-use */
758 memset(doc, 0, sizeof(GeanyDocument));
759
760 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0)
761 {
768 }
769 return TRUE;
770}
771
772
773/**
774 * Removes the given notebook tab at @a page_num and clears all related information
775 * in the document list.
776 *
777 * @param page_num The notebook page number to remove.
778 *
779 * @return @c TRUE if the document was actually removed or @c FALSE otherwise.
780 **/
781GEANY_API_SYMBOL
782gboolean document_remove_page(guint page_num)
783{
784 gboolean done = remove_page(page_num);
785
786 if (done && ui_prefs.new_document_after_close)
788
789 return done;
790}
791
792
793/* used to keep a record of the unchanged document state encoding */
795{
796 g_free(doc->priv->saved_encoding.encoding);
797 doc->priv->saved_encoding.encoding = g_strdup(doc->encoding);
798 doc->priv->saved_encoding.has_bom = doc->has_bom;
799}
800
801
802/* Opens a new empty document only if there are no other documents open */
804{
805 if (gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)) == 0)
807
808 return NULL;
809}
810
811
812/**
813 * Creates a new document.
814 * Line endings in @a text will be converted to the default setting.
815 * Afterwards, the @c "document-new" signal is emitted for plugins.
816 *
817 * @param utf8_filename @nullable The file name in UTF-8 encoding, or @c NULL to open a file as "untitled".
818 * @param ft @nullable The filetype to set or @c NULL to detect it from @a filename if not @c NULL.
819 * @param text @nullable The initial content of the file (in UTF-8 encoding), or @c NULL.
820 *
821 * @return @transfer{none} The new document.
822 **/
823GEANY_API_SYMBOL
824GeanyDocument *document_new_file(const gchar *utf8_filename, GeanyFiletype *ft, const gchar *text)
825{
826 GeanyDocument *doc;
827
828 if (utf8_filename && g_path_is_absolute(utf8_filename))
829 {
830 gchar *tmp;
831 tmp = utils_strdupa(utf8_filename); /* work around const */
832 utils_tidy_path(tmp);
833 utf8_filename = tmp;
834 }
835 doc = document_create(utf8_filename);
836
837 g_assert(doc != NULL);
838
839 sci_set_undo_collection(doc->editor->sci, FALSE); /* avoid creation of an undo action */
840 if (text)
841 {
842 GString *template = g_string_new(text);
844
845 sci_set_text(doc->editor->sci, template->str);
846 g_string_free(template, TRUE);
847 }
848 else
849 sci_clear_all(doc->editor->sci);
850
852
855
857 /* store the opened encoding for undo/redo */
859
860 if (ft == NULL && utf8_filename != NULL) /* guess the filetype from the filename if one is given */
862
863 document_set_filetype(doc, ft); /* also re-parses tags */
864
865 /* now the document is fully ready, display it (see notebook_new_tab()) */
866 gtk_widget_show(document_get_notebook_child(doc));
867
870 document_set_text_changed(doc, FALSE);
871 ui_document_show_hide(doc); /* update the document menu */
872
874 /* bring it in front, jump to the start and grab the focus */
875 editor_goto_pos(doc->editor, 0, FALSE);
877
878#ifdef USE_GIO_FILEMON
880#else
881 doc->priv->mtime = 0;
882#endif
883
884 /* "the" SCI signal (connect after initial setup(i.e. adding text)) */
885 g_signal_connect(doc->editor->sci, "sci-notify", G_CALLBACK(editor_sci_notify_cb), doc->editor);
886
887 g_signal_emit_by_name(geany_object, "document-new", doc);
888
889 msgwin_status_add(_("New file \"%s\" opened."),
890 DOC_FILENAME(doc));
891
892 return doc;
893}
894
895
896/**
897 * Opens a document specified by @a locale_filename.
898 * Afterwards, the @c "document-open" signal is emitted for plugins.
899 *
900 * @param locale_filename The filename of the document to load, in locale encoding.
901 * @param readonly Whether to open the document in read-only mode.
902 * @param ft @nullable The filetype for the document or @c NULL to auto-detect the filetype.
903 * @param forced_enc @nullable The file encoding to use or @c NULL to auto-detect the file encoding.
904 *
905 * @return @transfer{none} @nullable The document opened or @c NULL.
906 **/
907GEANY_API_SYMBOL
908GeanyDocument *document_open_file(const gchar *locale_filename, gboolean readonly,
909 GeanyFiletype *ft, const gchar *forced_enc)
910{
911 return document_open_file_full(NULL, locale_filename, 0, readonly, ft, forced_enc);
912}
913
914
915typedef struct
916{
917 gchar *data; /* null-terminated file data */
918 gsize len; /* string length of data */
919 gchar *enc;
920 gboolean bom;
921 time_t mtime; /* modification time, read by stat::st_mtime */
922 gboolean readonly;
923} FileData;
924
925
926static gboolean get_mtime(const gchar *locale_filename, time_t *time)
927{
928 GError *error = NULL;
929 const gchar *err_msg = NULL;
930
932 {
933 GFile *file = g_file_new_for_path(locale_filename);
934 GFileInfo *info = g_file_query_info(file, G_FILE_ATTRIBUTE_TIME_MODIFIED, G_FILE_QUERY_INFO_NONE, NULL, &error);
935
936 if (info)
937 {
938 GTimeVal timeval;
939
940 g_file_info_get_modification_time(info, &timeval);
941 g_object_unref(info);
942 *time = timeval.tv_sec;
943 }
944 else if (error)
945 err_msg = error->message;
946
947 g_object_unref(file);
948 }
949 else
950 {
951 GStatBuf st;
952
953 if (g_stat(locale_filename, &st) == 0)
954 *time = st.st_mtime;
955 else
956 err_msg = g_strerror(errno);
957 }
958
959 if (err_msg)
960 {
961 gchar *utf8_filename = utils_get_utf8_from_locale(locale_filename);
962
963 ui_set_statusbar(TRUE, _("Could not open file %s (%s)"),
964 utf8_filename, err_msg);
965 g_free(utf8_filename);
966 }
967
968 if (error)
969 g_error_free(error);
970
971 return err_msg == NULL;
972}
973
974
975/* loads textfile data, verifies and converts to forced_enc or UTF-8. Also handles BOM. */
976static gboolean load_text_file(const gchar *locale_filename, const gchar *display_filename,
977 FileData *filedata, const gchar *forced_enc)
978{
979 GError *err = NULL;
980
981 filedata->data = NULL;
982 filedata->len = 0;
983 filedata->enc = NULL;
984 filedata->bom = FALSE;
985 filedata->readonly = FALSE;
986
987 if (!get_mtime(locale_filename, &filedata->mtime))
988 return FALSE;
989
991 {
992 GFile *file = g_file_new_for_path(locale_filename);
993
994 g_file_load_contents(file, NULL, &filedata->data, &filedata->len, NULL, &err);
995 g_object_unref(file);
996 }
997 else
998 g_file_get_contents(locale_filename, &filedata->data, &filedata->len, &err);
999
1000 if (err)
1001 {
1002 ui_set_statusbar(TRUE, "%s", err->message);
1003 g_error_free(err);
1004 return FALSE;
1005 }
1006
1007 if (! encodings_convert_to_utf8_auto(&filedata->data, &filedata->len, forced_enc,
1008 &filedata->enc, &filedata->bom, &filedata->readonly))
1009 {
1010 if (forced_enc)
1011 {
1012 ui_set_statusbar(TRUE, _("The file \"%s\" is not valid %s."),
1013 display_filename, forced_enc);
1014 }
1015 else
1016 {
1017 ui_set_statusbar(TRUE,
1018 _("The file \"%s\" does not look like a text file or the file encoding is not supported."),
1019 display_filename);
1020 }
1021 g_free(filedata->data);
1022 return FALSE;
1023 }
1024
1025 if (filedata->readonly)
1026 {
1027 const gchar *warn_msg = _(
1028 "The file \"%s\" could not be opened properly and has been truncated. " \
1029 "This can occur if the file contains a NULL byte. " \
1030 "Be aware that saving it can cause data loss.\nThe file was set to read-only.");
1031
1032 if (main_status.main_window_realized)
1033 dialogs_show_msgbox(GTK_MESSAGE_WARNING, warn_msg, display_filename);
1034
1035 ui_set_statusbar(TRUE, warn_msg, display_filename);
1036 }
1037
1038 return TRUE;
1039}
1040
1041
1042/* Sets the cursor position on opening a file. First it sets the line when cl_options.goto_line
1043 * is set, otherwise it sets the line when pos is greater than zero and finally it sets the column
1044 * if cl_options.goto_column is set.
1045 *
1046 * returns the new position which may have changed */
1047static gint set_cursor_position(GeanyEditor *editor, gint pos)
1048{
1049 if (cl_options.goto_line >= 0)
1050 { /* goto line which was specified on command line and then undefine the line */
1051 sci_goto_line(editor->sci, cl_options.goto_line - 1, TRUE);
1052 editor->scroll_percent = 0.5F;
1053 cl_options.goto_line = -1;
1054 }
1055 else if (pos > 0)
1056 {
1057 sci_set_current_position(editor->sci, pos, FALSE);
1058 editor->scroll_percent = 0.5F;
1059 }
1060
1061 if (cl_options.goto_column >= 0)
1062 { /* goto column which was specified on command line and then undefine the column */
1063
1064 gint new_pos = sci_get_current_position(editor->sci) + cl_options.goto_column;
1065 sci_set_current_position(editor->sci, new_pos, FALSE);
1066 editor->scroll_percent = 0.5F;
1067 cl_options.goto_column = -1;
1068 return new_pos;
1069 }
1070 return sci_get_current_position(editor->sci);
1071}
1072
1073
1074/* Count lines that start with some hard tabs then a soft tab. */
1075static gboolean detect_tabs_and_spaces(GeanyEditor *editor)
1076{
1077 const GeanyIndentPrefs *iprefs = editor_get_indent_prefs(editor);
1078 ScintillaObject *sci = editor->sci;
1079 gsize count = 0;
1080 struct Sci_TextToFind ttf;
1081 gchar *soft_tab = g_strnfill((gsize)iprefs->width, ' ');
1082 gchar *regex = g_strconcat("^\t+", soft_tab, "[^ ]", NULL);
1083
1084 g_free(soft_tab);
1085
1086 ttf.chrg.cpMin = 0;
1088 ttf.lpstrText = regex;
1089 while (1)
1090 {
1091 gint pos;
1092
1094 if (pos == -1)
1095 break; /* no more matches */
1096 count++;
1097 ttf.chrg.cpMin = ttf.chrgText.cpMax + 1; /* search after this match */
1098 }
1099 g_free(regex);
1100 /* The 0.02 is a low weighting to ignore a few possibly accidental occurrences */
1101 return count > sci_get_line_count(sci) * 0.02;
1102}
1103
1104
1105/* Detect the indent type based on counting the leading indent characters for each line.
1106 * Returns whether detection succeeded, and the detected type in *type_ upon success */
1108{
1109 GeanyEditor *editor = doc->editor;
1110 ScintillaObject *sci = editor->sci;
1111 gint line, line_count;
1112 gsize tabs = 0, spaces = 0;
1113
1114 if (detect_tabs_and_spaces(editor))
1115 {
1116 *type_ = GEANY_INDENT_TYPE_BOTH;
1117 return TRUE;
1118 }
1119
1120 line_count = sci_get_line_count(sci);
1121 for (line = 0; line < line_count; line++)
1122 {
1124 gchar c;
1125
1126 /* most code will have indent total <= 24, otherwise it's more likely to be
1127 * alignment than indentation */
1129 continue;
1130
1131 c = sci_get_char_at(sci, pos);
1132 if (c == '\t')
1133 tabs++;
1134 /* check for at least 2 spaces */
1135 else if (c == ' ' && sci_get_char_at(sci, pos + 1) == ' ')
1136 spaces++;
1137 }
1138 if (spaces == 0 && tabs == 0)
1139 return FALSE;
1140
1141 /* the factors may need to be tweaked */
1142 if (spaces > tabs * 4)
1143 *type_ = GEANY_INDENT_TYPE_SPACES;
1144 else if (tabs > spaces * 4)
1145 *type_ = GEANY_INDENT_TYPE_TABS;
1146 else
1147 *type_ = GEANY_INDENT_TYPE_BOTH;
1148
1149 return TRUE;
1150}
1151
1152
1153/* Detect the indent width based on counting the leading indent characters for each line.
1154 * Returns whether detection succeeded, and the detected width in *width_ upon success */
1155static gboolean detect_indent_width(GeanyEditor *editor, GeanyIndentType type, gint *width_)
1156{
1157 const GeanyIndentPrefs *iprefs = editor_get_indent_prefs(editor);
1158 ScintillaObject *sci = editor->sci;
1159 gint line, line_count;
1160 gint widths[7] = { 0 }; /* width can be from 2 to 8 */
1161 gint count, width, i;
1162
1163 /* can't easily detect the supposed width of a tab, guess the default is OK */
1164 if (type == GEANY_INDENT_TYPE_TABS)
1165 return FALSE;
1166
1167 /* force 8 at detection time for tab & spaces -- anyway we don't use tabs at this point */
1169
1170 line_count = sci_get_line_count(sci);
1171 for (line = 0; line < line_count; line++)
1172 {
1174
1175 /* We probably don't have style info yet, because we're generally called just after
1176 * the document got created, so we can't use highlighting_is_code_style().
1177 * That's not good, but the assumption below that concerning lines start with an
1178 * asterisk (common continuation character for C/C++/Java/...) should do the trick
1179 * without removing too much legitimate lines. */
1180 if (sci_get_char_at(sci, pos) == '*')
1181 continue;
1182
1184 /* most code will have indent total <= 24, otherwise it's more likely to be
1185 * alignment than indentation */
1186 if (width > 24)
1187 continue;
1188 /* < 2 is no indentation */
1189 if (width < 2)
1190 continue;
1191
1192 for (i = G_N_ELEMENTS(widths) - 1; i >= 0; i--)
1193 {
1194 if ((width % (i + 2)) == 0)
1195 widths[i]++;
1196 }
1197 }
1198 count = 0;
1199 width = iprefs->width;
1200 for (i = G_N_ELEMENTS(widths) - 1; i >= 0; i--)
1201 {
1202 /* give large indents higher weight not to be fooled by spurious indents */
1203 if (widths[i] >= count * 1.5)
1204 {
1205 width = i + 2;
1206 count = widths[i];
1207 }
1208 }
1209
1210 if (count == 0)
1211 return FALSE;
1212
1213 *width_ = width;
1214 return TRUE;
1215}
1216
1217
1218/* same as detect_indent_width() but uses editor's indent type */
1220{
1221 return detect_indent_width(doc->editor, doc->editor->indent_type, width_);
1222}
1223
1224
1226{
1228 GeanyIndentType type = iprefs->type;
1229 gint width = iprefs->width;
1230
1231 if (iprefs->detect_type && document_detect_indent_type(doc, &type))
1232 {
1233 if (type != iprefs->type)
1234 {
1235 const gchar *name = NULL;
1236
1237 switch (type)
1238 {
1240 name = _("Spaces");
1241 break;
1243 name = _("Tabs");
1244 break;
1246 name = _("Tabs and Spaces");
1247 break;
1248 }
1249 /* For translators: first wildcard is the indentation mode (Spaces, Tabs, Tabs
1250 * and Spaces), the second one is the filename */
1251 ui_set_statusbar(TRUE, _("Setting %s indentation mode for %s."), name,
1252 DOC_FILENAME(doc));
1253 }
1254 }
1255 else if (doc->file_type->indent_type > -1)
1256 type = doc->file_type->indent_type;
1257
1258 if (iprefs->detect_width && detect_indent_width(doc->editor, type, &width))
1259 {
1260 if (width != iprefs->width)
1261 {
1262 ui_set_statusbar(TRUE, _("Setting indentation width to %d for %s."), width,
1263 DOC_FILENAME(doc));
1264 }
1265 }
1266 else if (doc->file_type->indent_width > -1)
1267 width = doc->file_type->indent_width;
1268
1269 editor_set_indent(doc->editor, type, width);
1270}
1271
1272
1274{
1275 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook),
1277}
1278
1279
1280/* To open a new file, set doc to NULL; filename should be locale encoded.
1281 * To reload a file, set the doc for the document to be reloaded; filename should be NULL.
1282 * pos is the cursor position, which can be overridden by --line and --column.
1283 * forced_enc can be NULL to detect the file encoding.
1284 * Returns: doc of the opened file or NULL if an error occurred. */
1286 gboolean readonly, GeanyFiletype *ft, const gchar *forced_enc)
1287{
1288 gint editor_mode;
1289 gboolean reload = (doc == NULL) ? FALSE : TRUE;
1290 gchar *utf8_filename = NULL;
1291 gchar *display_filename = NULL;
1292 gchar *locale_filename = NULL;
1293 GeanyFiletype *use_ft;
1294 FileData filedata;
1295 UndoReloadData *undo_reload_data;
1296 gboolean add_undo_reload_action;
1297
1298 g_return_val_if_fail(doc == NULL || doc->is_valid, NULL);
1299
1300 if (reload)
1301 {
1302 utf8_filename = g_strdup(doc->file_name);
1303 locale_filename = utils_get_locale_from_utf8(utf8_filename);
1304 }
1305 else
1306 {
1307 /* filename must not be NULL when opening a file */
1308 g_return_val_if_fail(filename, NULL);
1309
1310#ifdef G_OS_WIN32
1311 /* if filename is a shortcut, try to resolve it */
1312 locale_filename = win32_get_shortcut_target(filename);
1313#else
1314 locale_filename = g_strdup(filename);
1315#endif
1316 /* remove relative junk */
1317 utils_tidy_path(locale_filename);
1318
1319 /* try to get the UTF-8 equivalent for the filename, fallback to filename if error */
1320 utf8_filename = utils_get_utf8_from_locale(locale_filename);
1321
1322 /* if file is already open, switch to it and go */
1323 doc = document_find_by_filename(utf8_filename);
1324 if (doc != NULL)
1325 {
1326 ui_add_recent_document(doc); /* either add or reorder recent item */
1327 /* show the doc before reload dialog */
1328 document_show_tab(doc);
1329 document_check_disk_status(doc, TRUE); /* force a file changed check */
1330 }
1331 }
1332 if (reload || doc == NULL)
1333 { /* doc possibly changed */
1334 display_filename = utils_str_middle_truncate(utf8_filename, 100);
1335
1336 if (! load_text_file(locale_filename, display_filename, &filedata, forced_enc))
1337 {
1338 g_free(display_filename);
1339 g_free(utf8_filename);
1340 g_free(locale_filename);
1341 return NULL;
1342 }
1343
1344 if (! reload)
1345 {
1346 doc = document_create(utf8_filename);
1347 g_return_val_if_fail(doc != NULL, NULL); /* really should not happen */
1348
1349 /* file exists on disk, set real_path */
1350 SETPTR(doc->real_path, utils_get_real_path(locale_filename));
1351
1352 doc->priv->is_remote = utils_is_remote_path(locale_filename);
1353 monitor_file_setup(doc);
1354 }
1355
1357 {
1358 sci_set_undo_collection(doc->editor->sci, FALSE); /* avoid creation of an undo action */
1360 undo_reload_data = NULL;
1361 }
1362 else
1363 {
1364 undo_reload_data = (UndoReloadData*) g_malloc(sizeof(UndoReloadData));
1365
1366 /* We will be adding a UNDO_RELOAD action to the undo stack that undoes
1367 * this reload. To do that, we keep collecting undo actions during
1368 * reloading, and at the end add an UNDO_RELOAD action that performs
1369 * all these actions in bulk. To keep track of how many undo actions
1370 * were added during this time, we compare the current undo-stack height
1371 * with its height at the end of the process. Note that g_trash_stack_height()
1372 * is O(N), which is a little ugly, but this seems like the most maintainable
1373 * option. */
1374 undo_reload_data->actions_count = g_trash_stack_height(&doc->priv->undo_actions);
1375
1376 /* We use add_undo_reload_action to track any changes to the document that
1377 * require adding an undo action to revert the reload, but that do not
1378 * generate an undo action themselves. */
1379 add_undo_reload_action = FALSE;
1380 }
1381
1382 /* add the text to the ScintillaObject */
1383 sci_set_readonly(doc->editor->sci, FALSE); /* to allow replacing text */
1384 sci_set_text(doc->editor->sci, filedata.data); /* NULL terminated data */
1385 queue_colourise(doc); /* Ensure the document gets colourised. */
1386
1387 /* detect & set line endings */
1388 editor_mode = utils_get_line_endings(filedata.data, filedata.len);
1389 if (undo_reload_data)
1390 {
1391 undo_reload_data->eol_mode = editor_get_eol_char_mode(doc->editor);
1392 /* Force adding an undo-reload action if the EOL mode changed. */
1393 if (editor_mode != undo_reload_data->eol_mode)
1394 add_undo_reload_action = TRUE;
1395 }
1396 sci_set_eol_mode(doc->editor->sci, editor_mode);
1397 g_free(filedata.data);
1398
1399 sci_set_undo_collection(doc->editor->sci, TRUE);
1400
1401 /* If reloading and the current and new encodings or BOM states differ,
1402 * add appropriate undo actions. */
1403 if (undo_reload_data)
1404 {
1405 if (! utils_str_equal(doc->encoding, filedata.enc))
1406 document_undo_add(doc, UNDO_ENCODING, g_strdup(doc->encoding));
1407 if (doc->has_bom != filedata.bom)
1408 document_undo_add(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
1409 }
1410
1411 doc->priv->mtime = filedata.mtime; /* get the modification time from file and keep it */
1412 g_free(doc->encoding); /* if reloading, free old encoding */
1413 doc->encoding = filedata.enc;
1414 doc->has_bom = filedata.bom;
1415 store_saved_encoding(doc); /* store the opened encoding for undo/redo */
1416
1417 doc->readonly = readonly || filedata.readonly;
1418 sci_set_readonly(doc->editor->sci, doc->readonly);
1419 doc->priv->protected = 0;
1420
1421 /* update line number margin width */
1424
1425 if (! reload)
1426 {
1427
1428 /* "the" SCI signal (connect after initial setup(i.e. adding text)) */
1429 g_signal_connect(doc->editor->sci, "sci-notify", G_CALLBACK(editor_sci_notify_cb),
1430 doc->editor);
1431
1432 use_ft = (ft != NULL) ? ft : filetypes_detect_from_document(doc);
1433 }
1434 else
1435 { /* reloading */
1436 if (undo_reload_data)
1437 {
1438 /* Calculate the number of undo actions that are part of the reloading
1439 * process, and add the UNDO_RELOAD action. */
1440 undo_reload_data->actions_count =
1441 g_trash_stack_height(&doc->priv->undo_actions) - undo_reload_data->actions_count;
1442
1443 /* We only add an undo-reload action if the document has actually changed.
1444 * At the time of writing, this condition is moot because sci_set_text
1445 * generates an undo action even when the text hasn't really changed, so
1446 * actions_count is always greater than zero. In the future this might change.
1447 * It's arguable whether we should add an undo-reload action unconditionally,
1448 * especially since it's possible (if unlikely) that there had only
1449 * been "invisible" changes to the document, such as changes in encoding and
1450 * EOL mode, but for the time being that's how we roll. */
1451 if (undo_reload_data->actions_count > 0 || add_undo_reload_action)
1452 document_undo_add(doc, UNDO_RELOAD, undo_reload_data);
1453 else
1454 g_free(undo_reload_data);
1455
1456 /* We didn't save the document per-se, but its contents are now
1457 * synchronized with the file on disk, hence set a save point here.
1458 * We need to do this in this case only, because we don't clear
1459 * Scintilla's undo stack. */
1461 }
1462 else
1464
1465 use_ft = ft;
1466 }
1467 /* update taglist, typedef keywords and build menu if necessary */
1468 document_set_filetype(doc, use_ft);
1469
1470 /* set indentation settings after setting the filetype */
1471 if (reload)
1472 editor_set_indent(doc->editor, doc->editor->indent_type, doc->editor->indent_width); /* resetup sci */
1473 else
1475
1476 document_set_text_changed(doc, FALSE); /* also updates tab state */
1477 ui_document_show_hide(doc); /* update the document menu */
1478
1479 /* finally add current file to recent files menu, but not the files from the last session */
1480 if (! main_status.opening_session_files)
1482
1483 if (reload)
1484 {
1485 g_signal_emit_by_name(geany_object, "document-reload", doc);
1486 ui_set_statusbar(TRUE, _("File %s reloaded."), display_filename);
1487 }
1488 else
1489 {
1490 g_signal_emit_by_name(geany_object, "document-open", doc);
1491 /* For translators: this is the status window message for opening a file. %d is the number
1492 * of the newly opened file, %s indicates whether the file is opened read-only
1493 * (it is replaced with the string ", read-only"). */
1494 msgwin_status_add(_("File %s opened (%d%s)."),
1495 display_filename, gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook)),
1496 (readonly) ? _(", read-only") : "");
1497 }
1498
1499 /* now the document is fully ready, display it (see notebook_new_tab()) */
1500 gtk_widget_show(document_get_notebook_child(doc));
1501 }
1502
1503 g_free(display_filename);
1504 g_free(utf8_filename);
1505 g_free(locale_filename);
1506
1507 /* set the cursor position according to pos, cl_options.goto_line and cl_options.goto_column */
1509 /* now bring the file in front */
1510 editor_goto_pos(doc->editor, pos, FALSE);
1511
1512 /* finally, let the editor widget grab the focus so you can start coding
1513 * right away */
1514 g_idle_add(on_idle_focus, doc);
1515 return doc;
1516}
1517
1518
1519/* Takes a new line separated list of filename URIs and opens each file.
1520 * length is the length of the string */
1521void document_open_file_list(const gchar *data, gsize length)
1522{
1523 guint i;
1524 gchar **list;
1525
1526 g_return_if_fail(data != NULL);
1527
1528 list = g_strsplit(data, utils_get_eol_char(utils_get_line_endings(data, length)), 0);
1529
1530 /* stop at the end or first empty item, because last item is empty but not null */
1531 for (i = 0; list[i] != NULL && list[i][0] != '\0'; i++)
1532 {
1534
1535 if (filename == NULL)
1536 continue;
1538 g_free(filename);
1539 }
1540
1541 g_strfreev(list);
1542}
1543
1544
1545/**
1546 * Opens each file in the list @a filenames.
1547 * Internally, document_open_file() is called for every list item.
1548 *
1549 * @param filenames @elementtype{filename} A list of filenames to load, in locale encoding.
1550 * @param readonly Whether to open the document in read-only mode.
1551 * @param ft @nullable The filetype for the document or @c NULL to auto-detect the filetype.
1552 * @param forced_enc @nullable The file encoding to use or @c NULL to auto-detect the file encoding.
1553 **/
1554GEANY_API_SYMBOL
1555void document_open_files(const GSList *filenames, gboolean readonly, GeanyFiletype *ft,
1556 const gchar *forced_enc)
1557{
1558 const GSList *item;
1559
1560 for (item = filenames; item != NULL; item = g_slist_next(item))
1561 {
1562 document_open_file(item->data, readonly, ft, forced_enc);
1563 }
1564}
1565
1566
1567static void on_keep_edit_history_on_reload_response(GtkWidget *bar, gint response_id, GeanyDocument *doc)
1568{
1569 if (response_id == GTK_RESPONSE_NO)
1570 {
1573 }
1574 else if (response_id == GTK_RESPONSE_CANCEL)
1575 {
1576 /* this condition cannot be reached via info bar buttons, but by our code
1577 * to replace this bar with a higher priority one */
1579 }
1581 gtk_widget_destroy(bar);
1582}
1583
1584
1585/**
1586 * Reloads the document with the specified file encoding.
1587 * @a forced_enc or @c NULL to auto-detect the file encoding.
1588 *
1589 * @param doc The document to reload.
1590 * @param forced_enc @nullable The file encoding to use or @c NULL to auto-detect the file encoding.
1591 *
1592 * @return @c TRUE if the document was actually reloaded or @c FALSE otherwise.
1593 **/
1594GEANY_API_SYMBOL
1595gboolean document_reload_force(GeanyDocument *doc, const gchar *forced_enc)
1596{
1597 gint pos = 0;
1598 GeanyDocument *new_doc;
1599 GtkWidget *bar;
1600
1601 g_return_val_if_fail(doc != NULL, FALSE);
1602
1603 /* Cancel resave bar if still open from previous file deletion */
1604 if (doc->priv->info_bars[MSG_TYPE_RESAVE] != NULL)
1605 gtk_info_bar_response(GTK_INFO_BAR(doc->priv->info_bars[MSG_TYPE_RESAVE]), GTK_RESPONSE_CANCEL);
1606
1607 /* Use cancel because the response handler would call this recursively */
1608 if (doc->priv->info_bars[MSG_TYPE_RELOAD] != NULL)
1609 gtk_info_bar_response(GTK_INFO_BAR(doc->priv->info_bars[MSG_TYPE_RELOAD]), GTK_RESPONSE_CANCEL);
1610
1611 /* try to set the cursor to the position before reloading */
1613 new_doc = document_open_file_full(doc, NULL, pos, doc->readonly, doc->file_type, forced_enc);
1614
1616 {
1617 bar = document_show_message(doc, GTK_MESSAGE_INFO,
1619 GTK_STOCK_OK, GTK_RESPONSE_ACCEPT,
1620 _("Discard history"), GTK_RESPONSE_NO,
1621 NULL, 0, _("The buffer's previous state is stored in the history and "
1622 "undoing restores it. You can disable this by discarding the history upon "
1623 "reload. This message will not be displayed again but "
1624 "your choice can be changed in the various preferences."),
1625 _("The file has been reloaded."));
1626 doc->priv->info_bars[MSG_TYPE_POST_RELOAD] = bar;
1628 }
1629
1630 return (new_doc != NULL);
1631}
1632
1633
1634/* also used for reloading when forced_enc is NULL */
1635gboolean document_reload_prompt(GeanyDocument *doc, const gchar *forced_enc)
1636{
1637 gchar *base_name;
1638 gboolean prompt, result = FALSE;
1639
1640 g_return_val_if_fail(doc != NULL, FALSE);
1641
1642 /* No need to reload "untitled" (non-file-backed) documents */
1643 if (doc->file_name == NULL)
1644 return FALSE;
1645
1646 if (forced_enc == NULL)
1647 forced_enc = doc->encoding;
1648
1649 base_name = g_path_get_basename(doc->file_name);
1650 /* don't prompt if edit history is maintained, or if file hasn't been edited at all */
1652 (doc->changed || (document_can_undo(doc) || document_can_redo(doc)));
1653
1654 if (!prompt || dialogs_show_question_full(NULL, _("_Reload"), GTK_STOCK_CANCEL,
1655 doc->changed ? _("Any unsaved changes will be lost.") :
1656 _("Undo history will be lost."),
1657 _("Are you sure you want to reload '%s'?"), base_name))
1658 {
1659 result = document_reload_force(doc, forced_enc);
1660 if (forced_enc != NULL)
1661 ui_update_statusbar(doc, -1);
1662 }
1663 g_free(base_name);
1664 return result;
1665}
1666
1667
1668static void document_update_timestamp(GeanyDocument *doc, const gchar *locale_filename)
1669{
1670#ifndef USE_GIO_FILEMON
1671 g_return_if_fail(doc != NULL);
1672
1673 get_mtime(locale_filename, &doc->priv->mtime); /* get the modification time from file and keep it */
1674#endif
1675}
1676
1677
1678/* Sets line and column to the given position byte_pos in the document.
1679 * byte_pos is the position counted in bytes, not characters */
1680static void get_line_column_from_pos(GeanyDocument *doc, guint byte_pos, gint *line, gint *column)
1681{
1682 gint i;
1683 gint line_start;
1684
1685 /* for some reason we can use byte count instead of character count here */
1686 *line = sci_get_line_from_position(doc->editor->sci, byte_pos);
1687 line_start = sci_get_position_from_line(doc->editor->sci, *line);
1688 /* get the column in the line */
1689 *column = byte_pos - line_start;
1690
1691 /* any non-ASCII characters are encoded with two bytes(UTF-8, always in Scintilla), so
1692 * skip one byte(i++) and decrease the column number which is based on byte count */
1693 for (i = line_start; i < (line_start + *column); i++)
1694 {
1695 if (sci_get_char_at(doc->editor->sci, i) < 0)
1696 {
1697 (*column)--;
1698 i++;
1699 }
1700 }
1701}
1702
1703
1705{
1706 gchar *filebase;
1707 gchar *filename;
1708 struct Sci_TextToFind ttf;
1709
1710 g_return_if_fail(doc != NULL);
1711 g_return_if_fail(doc->file_type != NULL);
1712
1713 filebase = g_regex_escape_string(GEANY_STRING_UNTITLED, -1);
1714 if (doc->file_type->extension)
1715 SETPTR(filebase, g_strconcat("\\b", filebase, "\\.\\w+", NULL));
1716 else
1717 SETPTR(filebase, g_strconcat("\\b", filebase, "\\b", NULL));
1718
1719 filename = g_path_get_basename(doc->file_name);
1720
1721 /* only search the first 3 lines */
1722 ttf.chrg.cpMin = 0;
1724 ttf.lpstrText = filebase;
1725
1727 {
1730 sci_replace_target(doc->editor->sci, filename, FALSE);
1731 }
1732 g_free(filebase);
1733 g_free(filename);
1734}
1735
1736
1737/**
1738 * Renames the file in @a doc to @a new_filename. Only the file on disk is actually renamed,
1739 * you still have to call @ref document_save_file_as() to change the @a doc object.
1740 * It also stops monitoring for file changes to prevent receiving too many file change events
1741 * while renaming. File monitoring is setup again in @ref document_save_file_as().
1742 *
1743 * @param doc The current document which should be renamed.
1744 * @param new_filename The new filename in UTF-8 encoding.
1745 *
1746 * @since 0.16
1747 **/
1748GEANY_API_SYMBOL
1749void document_rename_file(GeanyDocument *doc, const gchar *new_filename)
1750{
1751 gchar *old_locale_filename = utils_get_locale_from_utf8(doc->file_name);
1752 gchar *new_locale_filename = utils_get_locale_from_utf8(new_filename);
1753 gint result;
1754
1755 /* stop file monitoring to avoid getting events for deleting/creating files,
1756 * it's re-setup in document_save_file_as() */
1758
1759 result = g_rename(old_locale_filename, new_locale_filename);
1760 if (result != 0)
1761 {
1762 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR,
1763 _("Error renaming file."), g_strerror(errno));
1764 }
1765 g_free(old_locale_filename);
1766 g_free(new_locale_filename);
1767}
1768
1769
1771{
1772 /* do not call queue_colourise because to we want to keep the text-changed indication! */
1773 if (!doc->priv->protected++)
1774 sci_set_readonly(doc->editor->sci, TRUE);
1775
1777}
1778
1779
1781{
1782 g_return_if_fail(doc->priv->protected > 0);
1783
1784 if (!--doc->priv->protected && doc->readonly == FALSE)
1785 sci_set_readonly(doc->editor->sci, FALSE);
1786
1788}
1789
1790
1791/* Return TRUE if the document doesn't have a full filename set.
1792 * This makes filenames without a path show the save as dialog, e.g. for file templates.
1793 * Otherwise just use the set filename instead of asking the user - e.g. for command-line
1794 * new files. */
1796{
1797 g_return_val_if_fail(doc != NULL, FALSE);
1798
1799 return (doc->file_name == NULL || !g_path_is_absolute(doc->file_name));
1800}
1801
1802
1803/**
1804 * Saves the document, detecting the filetype.
1805 *
1806 * @param doc The document for the file to save.
1807 * @param utf8_fname @nullable The new name for the document, in UTF-8, or @c NULL.
1808 * @return @c TRUE if the file was saved or @c FALSE if the file could not be saved.
1809 *
1810 * @see document_save_file().
1811 *
1812 * @since 0.16
1813 **/
1814GEANY_API_SYMBOL
1815gboolean document_save_file_as(GeanyDocument *doc, const gchar *utf8_fname)
1816{
1817 gboolean ret;
1818 gboolean new_file;
1819
1820 g_return_val_if_fail(doc != NULL, FALSE);
1821
1822 new_file = document_need_save_as(doc) || (utf8_fname != NULL && strcmp(doc->file_name, utf8_fname) != 0);
1823 if (utf8_fname != NULL)
1824 SETPTR(doc->file_name, g_strdup(utf8_fname));
1825
1826 /* reset real path, it's retrieved again in document_save() */
1827 SETPTR(doc->real_path, NULL);
1828
1829 /* detect filetype */
1830 if (doc->file_type->id == GEANY_FILETYPES_NONE)
1831 {
1833
1834 document_set_filetype(doc, ft);
1835 if (document_get_current() == doc)
1836 {
1837 ignore_callback = TRUE;
1839 ignore_callback = FALSE;
1840 }
1841 }
1842
1843 if (new_file)
1844 {
1845 // assume user wants to throw away read-only setting
1846 sci_set_readonly(doc->editor->sci, FALSE);
1847 doc->readonly = FALSE;
1848 if (doc->priv->protected > 0)
1849 unprotect_document(doc);
1850 }
1851
1853
1854 ret = document_save_file(doc, TRUE);
1855
1856 /* file monitoring support, add file monitoring after the file has been saved
1857 * to ignore any earlier events */
1858 monitor_file_setup(doc);
1860
1861 if (ret)
1863 return ret;
1864}
1865
1866
1867static gsize save_convert_to_encoding(GeanyDocument *doc, gchar **data, gsize *len)
1868{
1869 GError *conv_error = NULL;
1870 gchar* conv_file_contents = NULL;
1871 gsize bytes_read;
1872 gsize conv_len;
1873
1874 g_return_val_if_fail(data != NULL && *data != NULL, FALSE);
1875 g_return_val_if_fail(len != NULL, FALSE);
1876
1877 /* try to convert it from UTF-8 to original encoding */
1878 conv_file_contents = g_convert(*data, *len - 1, doc->encoding, "UTF-8",
1879 &bytes_read, &conv_len, &conv_error);
1880
1881 if (conv_error != NULL)
1882 {
1883 gchar *text = g_strdup_printf(
1884_("An error occurred while converting the file from UTF-8 in \"%s\". The file remains unsaved."),
1885 doc->encoding);
1886 gchar *error_text;
1887
1888 if (conv_error->code == G_CONVERT_ERROR_ILLEGAL_SEQUENCE)
1889 {
1890 gint line, column;
1891 gint context_len;
1892 gunichar unic;
1893 /* don't read over the doc length */
1894 gint max_len = MIN((gint)bytes_read + 6, (gint)*len - 1);
1895 gchar context[7]; /* read 6 bytes from Sci + '\0' */
1896 sci_get_text_range(doc->editor->sci, bytes_read, max_len, context);
1897
1898 /* take only one valid Unicode character from the context and discard the leftover */
1899 unic = g_utf8_get_char_validated(context, -1);
1900 context_len = g_unichar_to_utf8(unic, context);
1901 context[context_len] = '\0';
1902 get_line_column_from_pos(doc, bytes_read, &line, &column);
1903
1904 error_text = g_strdup_printf(
1905 _("Error message: %s\nThe error occurred at \"%s\" (line: %d, column: %d)."),
1906 conv_error->message, context, line + 1, column);
1907 }
1908 else
1909 error_text = g_strdup_printf(_("Error message: %s."), conv_error->message);
1910
1911 geany_debug("encoding error: %s", conv_error->message);
1912 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR, text, error_text);
1913 g_error_free(conv_error);
1914 g_free(text);
1915 g_free(error_text);
1916 return FALSE;
1917 }
1918 else
1919 {
1920 g_free(*data);
1921 *data = conv_file_contents;
1922 *len = conv_len;
1923 }
1924 return TRUE;
1925}
1926
1927
1928static gchar *write_data_to_disk(const gchar *locale_filename,
1929 const gchar *data, gsize len)
1930{
1931 GError *error = NULL;
1932
1934 {
1935 /* Use old GLib API for safe saving (GVFS-safe, but alters ownership and permissons).
1936 * This is the only option that handles disk space exhaustion. */
1937 if (g_file_set_contents(locale_filename, data, len, &error))
1938 geany_debug("Wrote %s with g_file_set_contents().", locale_filename);
1939 }
1940 else if (USE_GIO_FILE_OPERATIONS)
1941 {
1942 GFile *fp;
1943
1944 /* Use GIO API to save file (GVFS-safe)
1945 * It is best in most GVFS setups but don't seem to work correctly on some more complex
1946 * setups (saving from some VM to their host, over some SMB shares, etc.) */
1947 fp = g_file_new_for_path(locale_filename);
1948 g_file_replace_contents(fp, data, len, NULL, file_prefs.gio_unsafe_save_backup,
1949 G_FILE_CREATE_NONE, NULL, NULL, &error);
1950 g_object_unref(fp);
1951 }
1952 else
1953 {
1954 FILE *fp;
1955 int save_errno;
1956 gchar *display_name = g_filename_display_name(locale_filename);
1957
1958 /* Use POSIX API for unsafe saving (GVFS-unsafe) */
1959 /* The error handling is taken from glib-2.26.0 gfileutils.c */
1960 errno = 0;
1961 fp = g_fopen(locale_filename, "wb");
1962 if (fp == NULL)
1963 {
1964 save_errno = errno;
1965
1966 g_set_error(&error,
1967 G_FILE_ERROR,
1968 g_file_error_from_errno(save_errno),
1969 _("Failed to open file '%s' for writing: fopen() failed: %s"),
1970 display_name,
1971 g_strerror(save_errno));
1972 }
1973 else
1974 {
1975 gsize bytes_written;
1976
1977 errno = 0;
1978 bytes_written = fwrite(data, sizeof(gchar), len, fp);
1979
1980 if (len != bytes_written)
1981 {
1982 save_errno = errno;
1983
1984 g_set_error(&error,
1985 G_FILE_ERROR,
1986 g_file_error_from_errno(save_errno),
1987 _("Failed to write file '%s': fwrite() failed: %s"),
1988 display_name,
1989 g_strerror(save_errno));
1990 }
1991
1992 errno = 0;
1993 /* preserve the fwrite() error if any */
1994 if (fclose(fp) != 0 && error == NULL)
1995 {
1996 save_errno = errno;
1997
1998 g_set_error(&error,
1999 G_FILE_ERROR,
2000 g_file_error_from_errno(save_errno),
2001 _("Failed to close file '%s': fclose() failed: %s"),
2002 display_name,
2003 g_strerror(save_errno));
2004 }
2005 }
2006
2007 g_free(display_name);
2008 }
2009 if (error != NULL)
2010 {
2011 gchar *msg = g_strdup(error->message);
2012 g_error_free(error);
2013 /* geany will warn about file truncation for unsafe saving below */
2014 return msg;
2015 }
2016 return NULL;
2017}
2018
2019
2020static gchar *save_doc(GeanyDocument *doc, const gchar *locale_filename,
2021 const gchar *data, gsize len)
2022{
2023 gchar *err;
2024
2025 g_return_val_if_fail(doc != NULL, g_strdup(g_strerror(EINVAL)));
2026 g_return_val_if_fail(data != NULL, g_strdup(g_strerror(EINVAL)));
2027
2028 err = write_data_to_disk(locale_filename, data, len);
2029 if (err)
2030 return err;
2031
2032 /* now the file is on disk, set real_path */
2033 if (doc->real_path == NULL)
2034 {
2035 doc->real_path = utils_get_real_path(locale_filename);
2036 doc->priv->is_remote = utils_is_remote_path(locale_filename);
2037 monitor_file_setup(doc);
2038 }
2039 return NULL;
2040}
2041
2042
2043static gboolean save_file_handle_infobars(GeanyDocument *doc, gboolean force)
2044{
2045 GtkWidget *bar = NULL;
2046
2047 document_show_tab(doc);
2048
2049 if (doc->priv->info_bars[MSG_TYPE_RELOAD])
2050 {
2051 if (!dialogs_show_question_full(NULL, _("_Overwrite"), GTK_STOCK_CANCEL,
2052 _("Overwrite?"),
2053 _("The file '%s' on the disk is more recent than the current buffer."),
2054 doc->file_name))
2055 return FALSE;
2056 bar = doc->priv->info_bars[MSG_TYPE_RELOAD];
2057 }
2058 else if (doc->priv->info_bars[MSG_TYPE_RESAVE])
2059 {
2060 if (!dialogs_show_question_full(NULL, GTK_STOCK_SAVE, GTK_STOCK_CANCEL,
2061 _("Try to resave the file?"),
2062 _("File \"%s\" was not found on disk!"),
2063 doc->file_name))
2064 return FALSE;
2065 bar = doc->priv->info_bars[MSG_TYPE_RESAVE];
2066 }
2067 else
2068 {
2069 g_assert_not_reached();
2070 return FALSE;
2071 }
2072 gtk_info_bar_response(GTK_INFO_BAR(bar), RESPONSE_DOCUMENT_SAVE);
2073 return TRUE;
2074}
2075
2076
2077/**
2078 * Saves the document.
2079 * Also shows the Save As dialog if necessary.
2080 * If the file is not modified, this function may do nothing unless @a force is set to @c TRUE.
2081 *
2082 * Saving may include replacing tabs with spaces,
2083 * stripping trailing spaces and adding a final new line at the end of the file, depending
2084 * on user preferences. Then the @c "document-before-save" signal is emitted,
2085 * allowing plugins to modify the document before it is saved, and data is
2086 * actually written to disk.
2087 *
2088 * On successful saving:
2089 * - GeanyDocument::real_path is set.
2090 * - The filetype is set again or auto-detected if it wasn't set yet.
2091 * - The @c "document-save" signal is emitted for plugins.
2092 *
2093 * @warning You should ensure @c doc->file_name has an absolute path unless you want the
2094 * Save As dialog to be shown. A @c NULL value also shows the dialog. This behaviour was
2095 * added in Geany 1.22.
2096 *
2097 * @param doc The document to save.
2098 * @param force Whether to save the file even if it is not modified.
2099 *
2100 * @return @c TRUE if the file was saved or @c FALSE if the file could not or should not be saved.
2101 **/
2102GEANY_API_SYMBOL
2103gboolean document_save_file(GeanyDocument *doc, gboolean force)
2104{
2105 gchar *errmsg;
2106 gchar *data;
2107 gsize len;
2108 gchar *locale_filename;
2109 const GeanyFilePrefs *fp;
2110
2111 g_return_val_if_fail(doc != NULL, FALSE);
2112
2113 if (document_need_save_as(doc))
2114 {
2115 /* ensure doc is the current tab before showing the dialog */
2116 document_show_tab(doc);
2117 return dialogs_show_save_as();
2118 }
2119
2120 if (!force && !doc->changed)
2121 return FALSE;
2122 if (doc->readonly)
2123 {
2124 ui_set_statusbar(TRUE,
2125 _("Cannot save read-only document '%s'!"), DOC_FILENAME(doc));
2126 return FALSE;
2127 }
2128 document_check_disk_status(doc, TRUE);
2129 if (doc->priv->protected)
2130 return save_file_handle_infobars(doc, force);
2131
2133 /* replaces tabs with spaces but only if the current file is not a Makefile */
2134 if (fp->replace_tabs && doc->file_type->id != GEANY_FILETYPES_MAKE)
2135 editor_replace_tabs(doc->editor, TRUE);
2136 /* strip trailing spaces */
2137 if (fp->strip_trailing_spaces)
2139 /* ensure the file has a newline at the end */
2140 if (fp->final_new_line)
2142 /* ensure newlines are consistent */
2145
2146 /* notify plugins which may wish to modify the document before it's saved */
2147 g_signal_emit_by_name(geany_object, "document-before-save", doc);
2148
2149 len = sci_get_length(doc->editor->sci) + 1;
2151 { /* always write a UTF-8 BOM because in this moment the text itself is still in UTF-8
2152 * encoding, it will be converted to doc->encoding below and this conversion
2153 * also changes the BOM */
2154 data = (gchar*) g_malloc(len + 3); /* 3 chars for BOM */
2155 data[0] = (gchar) 0xef;
2156 data[1] = (gchar) 0xbb;
2157 data[2] = (gchar) 0xbf;
2158 sci_get_text(doc->editor->sci, len, data + 3);
2159 len += 3;
2160 }
2161 else
2162 {
2163 data = (gchar*) g_malloc(len);
2164 sci_get_text(doc->editor->sci, len, data);
2165 }
2166
2167 /* save in original encoding, skip when it is already UTF-8 or has the encoding "None" */
2168 if (doc->encoding != NULL && ! utils_str_equal(doc->encoding, "UTF-8") &&
2170 {
2171 if (! save_convert_to_encoding(doc, &data, &len))
2172 {
2173 g_free(data);
2174 return FALSE;
2175 }
2176 }
2177 else
2178 {
2179 len = strlen(data);
2180 }
2181
2182 locale_filename = utils_get_locale_from_utf8(doc->file_name);
2183
2184 /* ignore file changed notification when the file is written */
2186
2187 /* actually write the content of data to the file on disk */
2188 errmsg = save_doc(doc, locale_filename, data, len);
2189 g_free(data);
2190
2191 if (errmsg != NULL)
2192 {
2193 ui_set_statusbar(TRUE, _("Error saving file (%s)."), errmsg);
2194
2196 {
2197 SETPTR(errmsg,
2198 g_strdup_printf(_("%s\n\nThe file on disk may now be truncated!"), errmsg));
2199 }
2200 dialogs_show_msgbox_with_secondary(GTK_MESSAGE_ERROR, _("Error saving file."), errmsg);
2202 utils_beep();
2203 g_free(locale_filename);
2204 g_free(errmsg);
2205 return FALSE;
2206 }
2207
2208 /* store the opened encoding for undo/redo */
2210
2211 /* ignore the following things if we are quitting */
2212 if (! main_status.quitting)
2213 {
2215
2217 document_update_timestamp(doc, locale_filename);
2218
2219 /* update filetype-related things */
2221
2223
2224 msgwin_status_add(_("File %s saved."), doc->file_name);
2225 ui_update_statusbar(doc, -1);
2226#ifdef HAVE_VTE
2227 vte_cwd((doc->real_path != NULL) ? doc->real_path : doc->file_name, FALSE);
2228#endif
2229 }
2230 g_free(locale_filename);
2231
2232 g_signal_emit_by_name(geany_object, "document-save", doc);
2233
2234 return TRUE;
2235}
2236
2237
2238/* special search function, used from the find entry in the toolbar
2239 * return TRUE if text was found otherwise FALSE
2240 * return also TRUE if text is empty */
2241gboolean document_search_bar_find(GeanyDocument *doc, const gchar *text, gboolean inc,
2242 gboolean backwards)
2243{
2244 gint start_pos, search_pos;
2245 struct Sci_TextToFind ttf;
2246
2247 g_return_val_if_fail(text != NULL, FALSE);
2248 g_return_val_if_fail(doc != NULL, FALSE);
2249 if (! *text)
2250 return TRUE;
2251
2252 start_pos = (inc || backwards) ? sci_get_selection_start(doc->editor->sci) :
2253 sci_get_selection_end(doc->editor->sci); /* equal if no selection */
2254
2255 /* search cursor to end or start */
2256 ttf.chrg.cpMin = start_pos;
2257 ttf.chrg.cpMax = backwards ? 0 : sci_get_length(doc->editor->sci);
2258 ttf.lpstrText = (gchar *)text;
2259 search_pos = sci_find_text(doc->editor->sci, 0, &ttf);
2260
2261 /* if no match, search start (or end) to cursor */
2262 if (search_pos == -1)
2263 {
2264 if (backwards)
2265 {
2266 ttf.chrg.cpMin = sci_get_length(doc->editor->sci);
2267 ttf.chrg.cpMax = start_pos;
2268 }
2269 else
2270 {
2271 ttf.chrg.cpMin = 0;
2272 ttf.chrg.cpMax = start_pos + strlen(text);
2273 }
2274 search_pos = sci_find_text(doc->editor->sci, 0, &ttf);
2275 }
2276
2277 if (search_pos != -1)
2278 {
2280
2281 /* unfold maybe folded results */
2283
2286
2287 if (! editor_line_in_view(doc->editor, line))
2288 { /* we need to force scrolling in case the cursor is outside of the current visible area
2289 * GeanyDocument::scroll_percent doesn't work because sci isn't always updated
2290 * while searching */
2291 editor_scroll_to_line(doc->editor, -1, 0.3F);
2292 }
2293 else
2294 sci_scroll_caret(doc->editor->sci); /* may need horizontal scrolling */
2295 return TRUE;
2296 }
2297 else
2298 {
2299 if (! inc)
2300 {
2301 ui_set_statusbar(FALSE, _("\"%s\" was not found."), text);
2302 }
2303 utils_beep();
2304 sci_goto_pos(doc->editor->sci, start_pos, FALSE); /* clear selection */
2305 return FALSE;
2306 }
2307}
2308
2309
2310/* General search function, used from the find dialog.
2311 * Returns -1 on failure or the start position of the matching text.
2312 * Will skip past any selection, ignoring it.
2313 *
2314 * @param text Text to find.
2315 * @param original_text Text as it was entered by user, or @c NULL to use @c text
2316 */
2317gint document_find_text(GeanyDocument *doc, const gchar *text, const gchar *original_text,
2318 GeanyFindFlags flags, gboolean search_backwards, GeanyMatchInfo **match_,
2319 gboolean scroll, GtkWidget *parent)
2320{
2321 gint selection_end, selection_start, search_pos;
2322
2323 g_return_val_if_fail(doc != NULL && text != NULL, -1);
2324 if (! *text)
2325 return -1;
2326
2327 /* Sci doesn't support searching backwards with a regex */
2328 if (flags & GEANY_FIND_REGEXP)
2329 search_backwards = FALSE;
2330
2331 if (!original_text)
2332 original_text = text;
2333
2334 selection_start = sci_get_selection_start(doc->editor->sci);
2335 selection_end = sci_get_selection_end(doc->editor->sci);
2336 if ((selection_end - selection_start) > 0)
2337 { /* there's a selection so go to the end */
2338 if (search_backwards)
2339 sci_goto_pos(doc->editor->sci, selection_start, TRUE);
2340 else
2341 sci_goto_pos(doc->editor->sci, selection_end, TRUE);
2342 }
2343
2345 if (search_backwards)
2346 search_pos = search_find_prev(doc->editor->sci, text, flags, match_);
2347 else
2348 search_pos = search_find_next(doc->editor->sci, text, flags, match_);
2349
2350 if (search_pos != -1)
2351 {
2352 /* unfold maybe folded results */
2354 sci_get_line_from_position(doc->editor->sci, search_pos));
2355 if (scroll)
2356 doc->editor->scroll_percent = 0.3F;
2357 }
2358 else
2359 {
2360 gint sci_len = sci_get_length(doc->editor->sci);
2361
2362 /* if we just searched the whole text, give up searching. */
2363 if ((selection_end == 0 && ! search_backwards) ||
2364 (selection_end == sci_len && search_backwards))
2365 {
2366 ui_set_statusbar(FALSE, _("\"%s\" was not found."), original_text);
2367 utils_beep();
2368 return -1;
2369 }
2370
2371 /* we searched only part of the document, so ask whether to wraparound. */
2373 dialogs_show_question_full(parent, GTK_STOCK_FIND, GTK_STOCK_CANCEL,
2374 _("Wrap search and find again?"), _("\"%s\" was not found."), original_text))
2375 {
2376 gint ret;
2377
2378 sci_set_current_position(doc->editor->sci, (search_backwards) ? sci_len : 0, FALSE);
2379 ret = document_find_text(doc, text, original_text, flags, search_backwards, match_, scroll, parent);
2380 if (ret == -1)
2381 { /* return to original cursor position if not found */
2382 sci_set_current_position(doc->editor->sci, selection_start, FALSE);
2383 }
2384 return ret;
2385 }
2386 }
2387 return search_pos;
2388}
2389
2390
2391/* Replaces the selection if it matches, otherwise just finds the next match.
2392 * Returns: start of replaced text, or -1 if no replacement was made
2393 *
2394 * @param find_text Text to find.
2395 * @param original_find_text Text to find as it was entered by user, or @c NULL to use @c find_text
2396 */
2397gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gchar *original_find_text,
2398 const gchar *replace_text, GeanyFindFlags flags, gboolean search_backwards)
2399{
2400 gint selection_end, selection_start, search_pos;
2402
2403 g_return_val_if_fail(doc != NULL && find_text != NULL && replace_text != NULL, -1);
2404
2405 if (! *find_text)
2406 return -1;
2407
2408 /* Sci doesn't support searching backwards with a regex */
2409 if (flags & GEANY_FIND_REGEXP)
2410 search_backwards = FALSE;
2411
2412 if (!original_find_text)
2413 original_find_text = find_text;
2414
2415 selection_start = sci_get_selection_start(doc->editor->sci);
2416 selection_end = sci_get_selection_end(doc->editor->sci);
2417 if (selection_end == selection_start)
2418 {
2419 /* no selection so just find the next match */
2420 document_find_text(doc, find_text, original_find_text, flags, search_backwards, NULL, TRUE, NULL);
2421 return -1;
2422 }
2423 /* there's a selection so go to the start before finding to search through it
2424 * this ensures there is a match */
2425 if (search_backwards)
2426 sci_goto_pos(doc->editor->sci, selection_end, TRUE);
2427 else
2428 sci_goto_pos(doc->editor->sci, selection_start, TRUE);
2429
2430 search_pos = document_find_text(doc, find_text, original_find_text, flags, search_backwards, &match, TRUE, NULL);
2431 /* return if the original selected text did not match (at the start of the selection) */
2432 if (search_pos != selection_start)
2433 {
2434 if (search_pos != -1)
2436 return -1;
2437 }
2438
2439 if (search_pos != -1)
2440 {
2441 gint replace_len = search_replace_match(doc->editor->sci, match, replace_text);
2442 /* select the replacement - find text will skip past the selected text */
2443 sci_set_selection_start(doc->editor->sci, search_pos);
2444 sci_set_selection_end(doc->editor->sci, search_pos + replace_len);
2446 }
2447 else
2448 {
2449 /* no match in the selection */
2450 utils_beep();
2451 }
2452 return search_pos;
2453}
2454
2455
2456static void show_replace_summary(GeanyDocument *doc, gint count, const gchar *original_find_text,
2457 const gchar *original_replace_text)
2458{
2459 gchar *filename;
2460
2461 if (count == 0)
2462 {
2463 ui_set_statusbar(FALSE, _("No matches found for \"%s\"."), original_find_text);
2464 return;
2465 }
2466
2467 filename = g_path_get_basename(DOC_FILENAME(doc));
2469 "%s: replaced %d occurrence of \"%s\" with \"%s\".",
2470 "%s: replaced %d occurrences of \"%s\" with \"%s\".",
2471 count), filename, count, original_find_text, original_replace_text);
2472 g_free(filename);
2473}
2474
2475
2476/* Replace all text matches in a certain range within document.
2477 * If not NULL, *new_range_end is set to the new range endpoint after replacing,
2478 * or -1 if no text was found.
2479 * scroll_to_match is whether to scroll the last replacement in view (which also
2480 * clears the selection).
2481 * Returns: the number of replacements made. */
2482static guint
2483document_replace_range(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
2484 GeanyFindFlags flags, gint start, gint end, gboolean scroll_to_match, gint *new_range_end)
2485{
2486 gint count = 0;
2487 struct Sci_TextToFind ttf;
2488 ScintillaObject *sci;
2489
2490 if (new_range_end != NULL)
2491 *new_range_end = -1;
2492
2493 g_return_val_if_fail(doc != NULL && find_text != NULL && replace_text != NULL, 0);
2494
2495 if (! *find_text || doc->readonly)
2496 return 0;
2497
2498 sci = doc->editor->sci;
2499
2500 ttf.chrg.cpMin = start;
2501 ttf.chrg.cpMax = end;
2502 ttf.lpstrText = (gchar*)find_text;
2503
2505 count = search_replace_range(sci, &ttf, flags, replace_text);
2507
2508 if (count > 0)
2509 { /* scroll last match in view, will destroy the existing selection */
2510 if (scroll_to_match)
2511 sci_goto_pos(sci, ttf.chrg.cpMin, TRUE);
2512
2513 if (new_range_end != NULL)
2514 *new_range_end = ttf.chrg.cpMax;
2515 }
2516 return count;
2517}
2518
2519
2520void document_replace_sel(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
2521 const gchar *original_find_text, const gchar *original_replace_text, GeanyFindFlags flags)
2522{
2523 gint selection_end, selection_start, selection_mode, selected_lines, last_line = 0;
2524 gint max_column = 0, count = 0;
2525 gboolean replaced = FALSE;
2526
2527 g_return_if_fail(doc != NULL && find_text != NULL && replace_text != NULL);
2528
2529 if (! *find_text)
2530 return;
2531
2532 selection_start = sci_get_selection_start(doc->editor->sci);
2533 selection_end = sci_get_selection_end(doc->editor->sci);
2534 /* do we have a selection? */
2535 if ((selection_end - selection_start) == 0)
2536 {
2537 utils_beep();
2538 return;
2539 }
2540
2541 selection_mode = sci_get_selection_mode(doc->editor->sci);
2542 selected_lines = sci_get_lines_selected(doc->editor->sci);
2543 /* handle rectangle, multi line selections (it doesn't matter on a single line) */
2544 if (selection_mode == SC_SEL_RECTANGLE && selected_lines > 1)
2545 {
2546 gint first_line, line;
2547
2549
2550 first_line = sci_get_line_from_position(doc->editor->sci, selection_start);
2551 /* Find the last line with chars selected (not EOL char) */
2552 last_line = sci_get_line_from_position(doc->editor->sci,
2553 selection_end - editor_get_eol_char_len(doc->editor));
2554 last_line = MAX(first_line, last_line);
2555 for (line = first_line; line < (first_line + selected_lines); line++)
2556 {
2557 gint line_start = sci_get_pos_at_line_sel_start(doc->editor->sci, line);
2558 gint line_end = sci_get_pos_at_line_sel_end(doc->editor->sci, line);
2559
2560 /* skip line if there is no selection */
2561 if (line_start != INVALID_POSITION)
2562 {
2563 /* don't let document_replace_range() scroll to match to keep our selection */
2564 gint new_sel_end;
2565
2566 count += document_replace_range(doc, find_text, replace_text, flags,
2567 line_start, line_end, FALSE, &new_sel_end);
2568 if (new_sel_end != -1)
2569 {
2570 replaced = TRUE;
2571 /* this gets the greatest column within the selection after replacing */
2572 max_column = MAX(max_column,
2573 new_sel_end - sci_get_position_from_line(doc->editor->sci, line));
2574 }
2575 }
2576 }
2578 }
2579 else /* handle normal line selection */
2580 {
2581 count += document_replace_range(doc, find_text, replace_text, flags,
2582 selection_start, selection_end, TRUE, &selection_end);
2583 if (selection_end != -1)
2584 replaced = TRUE;
2585 }
2586
2587 if (replaced)
2588 { /* update the selection for the new endpoint */
2589
2590 if (selection_mode == SC_SEL_RECTANGLE && selected_lines > 1)
2591 {
2592 /* now we can scroll to the selection and destroy it because we rebuild it later */
2593 /*sci_goto_pos(doc->editor->sci, selection_start, FALSE);*/
2594
2595 /* Note: the selection will be wrapped to last_line + 1 if max_column is greater than
2596 * the highest column on the last line. The wrapped selection is completely different
2597 * from the original one, so skip the selection at all */
2598 /* TODO is there a better way to handle the wrapped selection? */
2599 if ((sci_get_line_length(doc->editor->sci, last_line) - 1) >= max_column)
2600 { /* for keeping and adjusting the selection in multi line rectangle selection we
2601 * need the last line of the original selection and the greatest column number after
2602 * replacing and set the selection end to the last line at the greatest column */
2603 sci_set_selection_start(doc->editor->sci, selection_start);
2605 sci_get_position_from_line(doc->editor->sci, last_line) + max_column);
2606 sci_set_selection_mode(doc->editor->sci, selection_mode);
2607 }
2608 }
2609 else
2610 {
2611 sci_set_selection_start(doc->editor->sci, selection_start);
2612 sci_set_selection_end(doc->editor->sci, selection_end);
2613 }
2614 }
2615 else /* no replacements */
2616 utils_beep();
2617
2618 show_replace_summary(doc, count, original_find_text, original_replace_text);
2619}
2620
2621
2622/* returns number of replacements made. */
2623gint document_replace_all(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text,
2624 const gchar *original_find_text, const gchar *original_replace_text, GeanyFindFlags flags)
2625{
2626 gint len, count;
2627 g_return_val_if_fail(doc != NULL && find_text != NULL && replace_text != NULL, FALSE);
2628
2629 if (! *find_text)
2630 return FALSE;
2631
2632 len = sci_get_length(doc->editor->sci);
2634 doc, find_text, replace_text, flags, 0, len, TRUE, NULL);
2635
2636 show_replace_summary(doc, count, original_find_text, original_replace_text);
2637 return count;
2638}
2639
2640
2641/*
2642 * Parses or re-parses the document's buffer and updates the type
2643 * keywords and symbol list.
2644 *
2645 * @param doc The document.
2646 */
2648{
2649 guchar *buffer_ptr;
2650 gsize len;
2651
2652 g_return_if_fail(DOC_VALID(doc));
2653 g_return_if_fail(app->tm_workspace != NULL);
2654
2655 /* early out if it's a new file or doesn't support tags */
2656 if (! doc->file_name || ! doc->file_type || !filetype_has_tags(doc->file_type))
2657 {
2658 /* We must call sidebar_update_tag_list() before returning,
2659 * to ensure that the symbol list is always updated properly (e.g.
2660 * when creating a new document with a partial filename set. */
2661 sidebar_update_tag_list(doc, FALSE);
2662 return;
2663 }
2664
2665 /* create a new TM file if there isn't one yet */
2666 if (! doc->tm_file)
2667 {
2668 gchar *locale_filename = utils_get_locale_from_utf8(doc->file_name);
2669 const gchar *name;
2670
2671 /* lookup the name rather than using filetype name to support custom filetypes */
2673 doc->tm_file = tm_source_file_new(locale_filename, name);
2674 g_free(locale_filename);
2675
2676 if (doc->tm_file)
2678 }
2679
2680 /* early out if there's no tm source file and we couldn't create one */
2681 if (doc->tm_file == NULL)
2682 {
2683 /* We must call sidebar_update_tag_list() before returning,
2684 * to ensure that the symbol list is always updated properly (e.g.
2685 * when creating a new document with a partial filename set. */
2686 sidebar_update_tag_list(doc, FALSE);
2687 return;
2688 }
2689
2690 /* Parse Scintilla's buffer directly using TagManager
2691 * Note: this buffer *MUST NOT* be modified */
2692 len = sci_get_length(doc->editor->sci);
2693 buffer_ptr = (guchar *) SSM(doc->editor->sci, SCI_GETCHARACTERPOINTER, 0, 0);
2694 tm_workspace_update_source_file_buffer(doc->tm_file, buffer_ptr, len);
2695
2696 sidebar_update_tag_list(doc, TRUE);
2698}
2699
2700
2701/* Re-highlights type keywords without re-parsing the whole document. */
2703{
2704 GString *keywords_str;
2705 gint keyword_idx;
2706
2707 /* some filetypes support type keywords (such as struct names), but not
2708 * necessarily all filetypes for a particular scintilla lexer. this
2709 * tells us whether the filetype supports keywords, and if so
2710 * which index to use for the scintilla keywords set. */
2711 switch (doc->file_type->id)
2712 {
2713 case GEANY_FILETYPES_C:
2715 case GEANY_FILETYPES_CS:
2716 case GEANY_FILETYPES_D:
2721 case GEANY_FILETYPES_GO:
2722 {
2723
2724 /* index of the keyword set in the Scintilla lexer, for
2725 * example in LexCPP.cxx, see "cppWordLists" global array.
2726 * TODO: this magic number should be a member of the filetype */
2727 keyword_idx = 3;
2728 break;
2729 }
2730 default:
2731 return; /* early out if type keywords are not supported */
2732 }
2734 return;
2735
2736 /* get any type keywords and tell scintilla about them
2737 * this will cause the type keywords to be colourized in scintilla */
2738 keywords_str = symbols_find_typenames_as_string(doc->file_type->lang, FALSE);
2739 if (keywords_str)
2740 {
2741 gchar *keywords = g_string_free(keywords_str, FALSE);
2742 guint hash = g_str_hash(keywords);
2743
2744 if (hash != doc->priv->keyword_hash)
2745 {
2746 sci_set_keywords(doc->editor->sci, keyword_idx, keywords);
2747 queue_colourise(doc); /* force re-highlighting the entire document */
2748 doc->priv->keyword_hash = hash;
2749 }
2750 g_free(keywords);
2751 }
2752}
2753
2754
2755static gboolean on_document_update_tag_list_idle(gpointer data)
2756{
2757 GeanyDocument *doc = data;
2758
2759 if (! DOC_VALID(doc))
2760 return FALSE;
2761
2762 if (! main_status.quitting)
2764
2765 doc->priv->tag_list_update_source = 0;
2766
2767 /* don't update the tags until another modification of the buffer */
2768 return FALSE;
2769}
2770
2771
2773{
2775 return;
2776
2777 /* prevent "stacking up" callback handlers, we only need one to run soon */
2778 if (doc->priv->tag_list_update_source != 0)
2779 g_source_remove(doc->priv->tag_list_update_source);
2780
2781 doc->priv->tag_list_update_source = g_timeout_add_full(G_PRIORITY_LOW,
2783}
2784
2785
2787 gboolean filetype_changed)
2788{
2789 g_return_if_fail(doc);
2790 if (type == NULL)
2792
2793 if (filetype_changed)
2794 {
2795 doc->file_type = type;
2796
2797 /* delete tm file object to force creation of a new one */
2798 if (doc->tm_file != NULL)
2799 {
2802 doc->tm_file = NULL;
2803 }
2804 /* load tags files before highlighting (some lexers highlight global typenames) */
2805 if (type->id != GEANY_FILETYPES_NONE)
2807
2808 highlighting_set_styles(doc->editor->sci, type);
2810 build_menu_update(doc);
2811 queue_colourise(doc);
2812 if (type->priv->symbol_list_sort_mode == SYMBOLS_SORT_USE_PREVIOUS)
2814 else
2816 }
2817
2819}
2820
2821
2822/** Sets the filetype of the document (which controls syntax highlighting and tags)
2823 * @param doc The document to use.
2824 * @param type The filetype. */
2825GEANY_API_SYMBOL
2827{
2828 gboolean ft_changed;
2829 GeanyFiletype *old_ft;
2830
2831 g_return_if_fail(doc);
2832 if (type == NULL)
2834
2835 old_ft = doc->file_type;
2836 geany_debug("%s : %s (%s)",
2837 (doc->file_name != NULL) ? doc->file_name : "unknown",
2838 type->name,
2839 (doc->encoding != NULL) ? doc->encoding : "unknown");
2840
2841 ft_changed = (doc->file_type != type); /* filetype has changed */
2842 document_load_config(doc, type, ft_changed);
2843
2844 if (ft_changed)
2845 {
2847
2848 /* assume that if previous filetype was none and the settings are the default ones, this
2849 * is the first time the filetype is carefully set, so we should apply indent settings */
2850 if ((! old_ft || old_ft->id == GEANY_FILETYPES_NONE) &&
2851 doc->editor->indent_type == iprefs->type &&
2852 doc->editor->indent_width == iprefs->width)
2853 {
2856 }
2857
2858 sidebar_openfiles_update(doc); /* to update the icon */
2859 g_signal_emit_by_name(geany_object, "document-filetype-set", doc, old_ft);
2860 }
2861}
2862
2863
2865{
2866 document_load_config(doc, doc->file_type, TRUE);
2867}
2868
2869
2870/**
2871 * Sets the encoding of a document.
2872 * This function only set the encoding of the %document, it does not any conversions. The new
2873 * encoding is used when e.g. saving the file.
2874 *
2875 * @param doc The document to use.
2876 * @param new_encoding The encoding to be set for the document.
2877 **/
2878GEANY_API_SYMBOL
2879void document_set_encoding(GeanyDocument *doc, const gchar *new_encoding)
2880{
2881 if (doc == NULL || new_encoding == NULL ||
2882 utils_str_equal(new_encoding, doc->encoding))
2883 return;
2884
2885 g_free(doc->encoding);
2886 doc->encoding = g_strdup(new_encoding);
2887
2888 ui_update_statusbar(doc, -1);
2889 gtk_widget_set_sensitive(ui_lookup_widget(main_widgets.window, "menu_write_unicode_bom1"),
2891}
2892
2893
2894/* own Undo / Redo implementation to be able to undo / redo changes
2895 * to the encoding or the Unicode BOM (which are Scintilla independet).
2896 * All Scintilla events are stored in the undo / redo buffer and are passed through. */
2897
2898/* Clears an Undo or Redo buffer. */
2899void document_undo_clear_stack(GTrashStack **stack)
2900{
2901 while (g_trash_stack_height(stack) > 0)
2902 {
2903 undo_action *a = g_trash_stack_pop(stack);
2904
2905 if (G_LIKELY(a != NULL))
2906 {
2907 switch (a->type)
2908 {
2909 case UNDO_ENCODING:
2910 case UNDO_RELOAD:
2911 g_free(a->data); break;
2912 default: break;
2913 }
2914 g_free(a);
2915 }
2916 }
2917 *stack = NULL;
2918}
2919
2920/* Clears the Undo and Redo buffer (to be called when reloading or closing the document) */
2922{
2925
2926 if (! main_status.quitting && doc->editor != NULL)
2927 document_set_text_changed(doc, FALSE);
2928}
2929
2930
2931/* Adds an undo action without clearing the redo stack. This function should
2932 * not be called directly, generally (use document_undo_add() instead), but is
2933 * used by document_redo() in order not to erase the redo stack while moving
2934 * an action from the redo stack to the undo stack. */
2935void document_undo_add_internal(GeanyDocument *doc, guint type, gpointer data)
2936{
2937 undo_action *action;
2938
2939 g_return_if_fail(doc != NULL);
2940
2941 action = g_new0(undo_action, 1);
2942 action->type = type;
2943 action->data = data;
2944
2945 g_trash_stack_push(&doc->priv->undo_actions, action);
2946
2947 /* avoid unnecessary redraws */
2948 if (type != UNDO_SCINTILLA || !doc->changed)
2949 document_set_text_changed(doc, TRUE);
2950
2952}
2953
2954/* note: this is called on SCN_MODIFIED notifications */
2955void document_undo_add(GeanyDocument *doc, guint type, gpointer data)
2956{
2957 /* Clear the redo actions stack before adding the undo action. */
2959
2960 document_undo_add_internal(doc, type, data);
2961}
2962
2963
2965{
2966 g_return_val_if_fail(doc != NULL, FALSE);
2967
2968 if (g_trash_stack_height(&doc->priv->undo_actions) > 0 || sci_can_undo(doc->editor->sci))
2969 return TRUE;
2970 else
2971 return FALSE;
2972}
2973
2974
2976{
2977 doc->changed =
2978 (sci_is_modified(doc->editor->sci) ||
2979 doc->has_bom != doc->priv->saved_encoding.has_bom ||
2982}
2983
2984
2986{
2987 undo_action *action;
2988
2989 g_return_if_fail(doc != NULL);
2990
2991 action = g_trash_stack_pop(&doc->priv->undo_actions);
2992
2993 if (G_UNLIKELY(action == NULL))
2994 {
2995 /* fallback, should not be necessary */
2996 geany_debug("%s: fallback used", G_STRFUNC);
2997 sci_undo(doc->editor->sci);
2998 }
2999 else
3000 {
3001 switch (action->type)
3002 {
3003 case UNDO_SCINTILLA:
3004 {
3006
3007 sci_undo(doc->editor->sci);
3008 break;
3009 }
3010 case UNDO_BOM:
3011 {
3012 document_redo_add(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
3013
3014 doc->has_bom = GPOINTER_TO_INT(action->data);
3015 ui_update_statusbar(doc, -1);
3017 break;
3018 }
3019 case UNDO_ENCODING:
3020 {
3021 /* use the "old" encoding */
3022 document_redo_add(doc, UNDO_ENCODING, g_strdup(doc->encoding));
3023
3024 document_set_encoding(doc, (const gchar*)action->data);
3025 g_free(action->data);
3026
3027 ui_update_statusbar(doc, -1);
3029 break;
3030 }
3031 case UNDO_EOL:
3032 {
3033 undo_action *next_action;
3034
3035 document_redo_add(doc, UNDO_EOL, GINT_TO_POINTER(sci_get_eol_mode(doc->editor->sci)));
3036
3037 sci_set_eol_mode(doc->editor->sci, GPOINTER_TO_INT(action->data));
3038
3039 ui_update_statusbar(doc, -1);
3041
3042 /* When undoing, UNDO_EOL is always followed by UNDO_SCINTILLA
3043 * which undos the line endings in the editor and should be
3044 * performed together with UNDO_EOL. */
3045 next_action = g_trash_stack_peek(&doc->priv->undo_actions);
3046 if (next_action && next_action->type == UNDO_SCINTILLA)
3047 document_undo(doc);
3048 break;
3049 }
3050 case UNDO_RELOAD:
3051 {
3052 UndoReloadData *data = (UndoReloadData*)action->data;
3053 gint eol_mode = data->eol_mode;
3054 guint i;
3055
3056 /* We reuse 'data' for the redo action, so read the current EOL mode
3057 * into it before proceeding. */
3059
3060 /* Undo the rest of the actions which are part of the reloading process. */
3061 for (i = 0; i < data->actions_count; i++)
3062 document_undo(doc);
3063
3064 /* Restore the previous EOL mode. */
3065 sci_set_eol_mode(doc->editor->sci, eol_mode);
3066 /* This might affect the status bar and document menu, so update them. */
3067 ui_update_statusbar(doc, -1);
3069
3070 document_redo_add(doc, UNDO_RELOAD, data);
3071 break;
3072 }
3073 default: break;
3074 }
3075 }
3076 g_free(action); /* free the action which was taken from the stack */
3077
3080}
3081
3082
3084{
3085 g_return_val_if_fail(doc != NULL, FALSE);
3086
3087 if (g_trash_stack_height(&doc->priv->redo_actions) > 0 || sci_can_redo(doc->editor->sci))
3088 return TRUE;
3089 else
3090 return FALSE;
3091}
3092
3093
3095{
3096 undo_action *action;
3097
3098 g_return_if_fail(doc != NULL);
3099
3100 action = g_trash_stack_pop(&doc->priv->redo_actions);
3101
3102 if (G_UNLIKELY(action == NULL))
3103 {
3104 /* fallback, should not be necessary */
3105 geany_debug("%s: fallback used", G_STRFUNC);
3106 sci_redo(doc->editor->sci);
3107 }
3108 else
3109 {
3110 switch (action->type)
3111 {
3112 case UNDO_SCINTILLA:
3113 {
3114 undo_action *next_action;
3115
3117
3118 sci_redo(doc->editor->sci);
3119
3120 /* When redoing an EOL change, the UNDO_SCINTILLA which changes
3121 * the line ends in the editor is followed by UNDO_EOL
3122 * which should be performed together with UNDO_SCINTILLA. */
3123 next_action = g_trash_stack_peek(&doc->priv->redo_actions);
3124 if (next_action != NULL && next_action->type == UNDO_EOL)
3125 document_redo(doc);
3126 break;
3127 }
3128 case UNDO_BOM:
3129 {
3130 document_undo_add_internal(doc, UNDO_BOM, GINT_TO_POINTER(doc->has_bom));
3131
3132 doc->has_bom = GPOINTER_TO_INT(action->data);
3133 ui_update_statusbar(doc, -1);
3135 break;
3136 }
3137 case UNDO_ENCODING:
3138 {
3140
3141 document_set_encoding(doc, (const gchar*)action->data);
3142 g_free(action->data);
3143
3144 ui_update_statusbar(doc, -1);
3146 break;
3147 }
3148 case UNDO_EOL:
3149 {
3150 document_undo_add_internal(doc, UNDO_EOL, GINT_TO_POINTER(sci_get_eol_mode(doc->editor->sci)));
3151
3152 sci_set_eol_mode(doc->editor->sci, GPOINTER_TO_INT(action->data));
3153
3154 ui_update_statusbar(doc, -1);
3156 break;
3157 }
3158 case UNDO_RELOAD:
3159 {
3160 UndoReloadData *data = (UndoReloadData*)action->data;
3161 gint eol_mode = data->eol_mode;
3162 guint i;
3163
3164 /* We reuse 'data' for the undo action, so read the current EOL mode
3165 * into it before proceeding. */
3167
3168 /* Redo the rest of the actions which are part of the reloading process. */
3169 for (i = 0; i < data->actions_count; i++)
3170 document_redo(doc);
3171
3172 /* Restore the previous EOL mode. */
3173 sci_set_eol_mode(doc->editor->sci, eol_mode);
3174 /* This might affect the status bar and document menu, so update them. */
3175 ui_update_statusbar(doc, -1);
3177
3179 break;
3180 }
3181 default: break;
3182 }
3183 }
3184 g_free(action); /* free the action which was taken from the stack */
3185
3188}
3189
3190
3191static void document_redo_add(GeanyDocument *doc, guint type, gpointer data)
3192{
3193 undo_action *action;
3194
3195 g_return_if_fail(doc != NULL);
3196
3197 action = g_new0(undo_action, 1);
3198 action->type = type;
3199 action->data = data;
3200
3201 g_trash_stack_push(&doc->priv->redo_actions, action);
3202
3203 if (type != UNDO_SCINTILLA || !doc->changed)
3204 document_set_text_changed(doc, TRUE);
3205
3207}
3208
3209
3210enum
3211{
3216
3217static struct
3218{
3219 const gchar *name;
3220 GdkColor color;
3221 gboolean loaded;
3223 { "geany-document-status-changed", {0}, FALSE },
3224 { "geany-document-status-disk-changed", {0}, FALSE },
3225 { "geany-document-status-readonly", {0}, FALSE }
3227
3228
3230{
3231 if (doc->changed)
3232 return STATUS_CHANGED;
3233#ifdef USE_GIO_FILEMON
3234 else if (doc->priv->file_disk_status == FILE_CHANGED)
3235#else
3236 else if (doc->priv->protected)
3237#endif
3238 return STATUS_DISK_CHANGED;
3239 else if (doc->readonly)
3240 return STATUS_READONLY;
3241
3242 return -1;
3243}
3244
3245
3246/* returns an identifier that is to be set as a widget name or class to get it styled
3247 * depending on the document status (changed, readonly, etc.)
3248 * a NULL return value means default (unchanged) style */
3250{
3251 gint status;
3252
3253 g_return_val_if_fail(doc != NULL, NULL);
3254
3255 status = document_get_status_id(doc);
3256 if (status < 0)
3257 return NULL;
3258 else
3259 return document_status_styles[status].name;
3260}
3261
3262
3263/**
3264 * Gets the status color of the document, or @c NULL if default widget coloring should be used.
3265 * Returned colors are red if the document has changes, green if the document is read-only
3266 * or simply @c NULL if the document is unmodified but writable.
3267 *
3268 * @param doc The document to use.
3269 *
3270 * @return @nullable The color for the document or @c NULL if the default color should be used.
3271 * The color object is owned by Geany and should not be modified or freed.
3272 *
3273 * @since 0.16
3274 */
3275GEANY_API_SYMBOL
3277{
3278 gint status;
3279
3280 g_return_val_if_fail(doc != NULL, NULL);
3281
3282 status = document_get_status_id(doc);
3283 if (status < 0)
3284 return NULL;
3285 if (! document_status_styles[status].loaded)
3286 {
3287 GdkRGBA color;
3288 GtkWidgetPath *path = gtk_widget_path_new();
3289 GtkStyleContext *ctx = gtk_style_context_new();
3290 gtk_widget_path_append_type(path, GTK_TYPE_WINDOW);
3291 gtk_widget_path_append_type(path, GTK_TYPE_BOX);
3292 gtk_widget_path_append_type(path, GTK_TYPE_NOTEBOOK);
3293 gtk_widget_path_append_type(path, GTK_TYPE_LABEL);
3294 gtk_widget_path_iter_set_name(path, -1, document_status_styles[status].name);
3295 gtk_style_context_set_screen(ctx, gtk_widget_get_screen(GTK_WIDGET(doc->editor->sci)));
3296 gtk_style_context_set_path(ctx, path);
3297 gtk_style_context_get_color(ctx, gtk_style_context_get_state(ctx), &color);
3298 document_status_styles[status].color.red = 0xffff * color.red;
3299 document_status_styles[status].color.green = 0xffff * color.green;
3300 document_status_styles[status].color.blue = 0xffff * color.blue;
3301 document_status_styles[status].loaded = TRUE;
3302 gtk_widget_path_unref(path);
3303 g_object_unref(ctx);
3304 }
3305 return &document_status_styles[status].color;
3306}
3307
3308
3309/** Accessor function for @ref GeanyData::documents_array items.
3310 * @warning Always check the returned document is valid (@c doc->is_valid).
3311 * @param idx @c GeanyData::documents_array index.
3312 * @return @transfer{none} @nullable The document, or @c NULL if @a idx is out of range.
3313 *
3314 * @since 0.16
3315 */
3316GEANY_API_SYMBOL
3318{
3319 return (idx >= 0 && idx < (gint) documents_array->len) ? documents[idx] : NULL;
3320}
3321
3322
3324{
3325 gchar *text;
3326 GeanyDocument *doc;
3327 ScintillaObject *old_sci;
3328
3329 g_return_val_if_fail(old_doc, NULL);
3330 old_sci = old_doc->editor->sci;
3331 if (sci_has_selection(old_sci))
3333 else
3334 text = sci_get_contents(old_sci, -1);
3335
3336 doc = document_new_file(NULL, old_doc->file_type, text);
3337 g_free(text);
3338 document_set_text_changed(doc, TRUE);
3339
3340 /* copy file properties */
3341 doc->editor->line_wrapping = old_doc->editor->line_wrapping;
3342 doc->editor->line_breaking = old_doc->editor->line_breaking;
3343 doc->editor->auto_indent = old_doc->editor->auto_indent;
3345 old_doc->editor->indent_width);
3346 doc->readonly = old_doc->readonly;
3347 doc->has_bom = old_doc->has_bom;
3348 doc->priv->protected = 0;
3349 document_set_encoding(doc, old_doc->encoding);
3351 sci_set_readonly(doc->editor->sci, doc->readonly);
3352
3353 /* update ui */
3355 return doc;
3356}
3357
3358
3359/* @return TRUE if all files were saved or had their changes discarded. */
3361{
3362 guint p, page_count;
3363
3364 page_count = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
3365 /* iterate over documents in tabs order */
3366 for (p = 0; p < page_count; p++)
3367 {
3369
3370 if (DOC_VALID(doc) && doc->changed)
3371 {
3372 if (! dialogs_show_unsaved_file(doc))
3373 return FALSE;
3374 }
3375 }
3376
3377 return TRUE;
3378}
3379
3380
3381static void force_close_all(void)
3382{
3383 guint i;
3384
3385 main_status.closing_all = TRUE;
3386
3388 {
3390 }
3391
3392 main_status.closing_all = FALSE;
3393}
3394
3395
3397{
3399 return FALSE;
3400
3402
3403 return TRUE;
3404}
3405
3406
3407/* *
3408 * Shows a message related to a document.
3409 *
3410 * Use this whenever the user needs to see a document-related message,
3411 * for example when the file was externally modified or deleted.
3412 *
3413 * Any of the buttons can be @c NULL. If not @c NULL, @a btn_1's
3414 * @a response_1 response will be the default for the @c GtkInfoBar or
3415 * @c GtkDialog.
3416 *
3417 * @param doc @c GeanyDocument.
3418 * @param msgtype The type of message.
3419 * @param response_cb A callback function called when there's a response.
3420 * @param btn_1 The first action area button.
3421 * @param response_1 The response for @a btn_1.
3422 * @param btn_2 The second action area button.
3423 * @param response_2 The response for @a btn_2.
3424 * @param btn_3 The third action area button.
3425 * @param response_3 The response for @a btn_3.
3426 * @param extra_text Text to show below the main message.
3427 * @param format The text format for the main message.
3428 * @param ... Used with @a format as in @c printf.
3429 *
3430 * @since 1.25
3431 * */
3432static GtkWidget* document_show_message(GeanyDocument *doc, GtkMessageType msgtype,
3433 void (*response_cb)(GtkWidget *info_bar, gint response_id, GeanyDocument *doc),
3434 const gchar *btn_1, GtkResponseType response_1,
3435 const gchar *btn_2, GtkResponseType response_2,
3436 const gchar *btn_3, GtkResponseType response_3,
3437 const gchar *extra_text, const gchar *format, ...)
3438{
3439 va_list args;
3440 gchar *text, *markup;
3441 GtkWidget *hbox, *icon, *label, *content_area;
3442 GtkWidget *info_widget, *parent;
3443 parent = document_get_notebook_child(doc);
3444
3445 va_start(args, format);
3446 text = g_strdup_vprintf(format, args);
3447 va_end(args);
3448
3449 markup = g_markup_printf_escaped("<span size=\"larger\">%s</span>", text);
3450 g_free(text);
3451
3452 info_widget = gtk_info_bar_new();
3453 /* must be done now else Gtk-WARNING: widget not within a GtkWindow */
3454 gtk_box_pack_start(GTK_BOX(parent), info_widget, FALSE, TRUE, 0);
3455
3456 gtk_info_bar_set_message_type(GTK_INFO_BAR(info_widget), msgtype);
3457
3458 if (btn_1)
3459 gtk_info_bar_add_button(GTK_INFO_BAR(info_widget), btn_1, response_1);
3460 if (btn_2)
3461 gtk_info_bar_add_button(GTK_INFO_BAR(info_widget), btn_2, response_2);
3462 if (btn_3)
3463 gtk_info_bar_add_button(GTK_INFO_BAR(info_widget), btn_3, response_3);
3464
3465 content_area = gtk_info_bar_get_content_area(GTK_INFO_BAR(info_widget));
3466
3468 gtk_label_set_markup(GTK_LABEL(label), markup);
3469 g_free(markup);
3470
3471 g_signal_connect(info_widget, "response", G_CALLBACK(response_cb), doc);
3472
3473 hbox = gtk_box_new(GTK_ORIENTATION_HORIZONTAL, 12);
3474 gtk_box_pack_start(GTK_BOX(content_area), hbox, TRUE, TRUE, 0);
3475
3476 switch (msgtype)
3477 {
3478 case GTK_MESSAGE_INFO:
3479 icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_INFO, GTK_ICON_SIZE_DIALOG);
3480 break;
3481 case GTK_MESSAGE_WARNING:
3482 icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_WARNING, GTK_ICON_SIZE_DIALOG);
3483 break;
3484 case GTK_MESSAGE_QUESTION:
3485 icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_QUESTION, GTK_ICON_SIZE_DIALOG);
3486 break;
3487 case GTK_MESSAGE_ERROR:
3488 icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_ERROR, GTK_ICON_SIZE_DIALOG);
3489 break;
3490 default:
3491 icon = NULL;
3492 break;
3493 }
3494
3495 if (icon)
3496 gtk_box_pack_start(GTK_BOX(hbox), icon, FALSE, TRUE, 0);
3497
3498 if (extra_text)
3499 {
3500 GtkWidget *vbox = gtk_box_new(GTK_ORIENTATION_VERTICAL, 6);
3501 GtkWidget *extra_label = geany_wrap_label_new(extra_text);
3502
3503 gtk_box_pack_start(GTK_BOX(vbox), label, TRUE, TRUE, 0);
3504 gtk_box_pack_start(GTK_BOX(vbox), extra_label, TRUE, TRUE, 0);
3505 gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
3506 }
3507 else
3508 gtk_box_pack_start(GTK_BOX(hbox), label, TRUE, TRUE, 0);
3509
3510 gtk_box_reorder_child(GTK_BOX(parent), info_widget, 0);
3511
3512 gtk_widget_show_all(info_widget);
3513
3514 return info_widget;
3515}
3516
3517
3518static void on_monitor_reload_file_response(GtkWidget *bar, gint response_id, GeanyDocument *doc)
3519{
3520 gboolean close = FALSE;
3521
3522 // disable info bar so actions complete normally
3523 unprotect_document(doc);
3525
3526 if (response_id == RESPONSE_DOCUMENT_RELOAD)
3527 {
3528 close = doc->changed ?
3529 document_reload_prompt(doc, doc->encoding) :
3531 }
3532 else if (response_id == RESPONSE_DOCUMENT_SAVE)
3533 {
3534 close = document_save_file(doc, TRUE); // force overwrite
3535 }
3536 else if (response_id == GTK_RESPONSE_CANCEL)
3537 {
3538 document_set_text_changed(doc, TRUE);
3539 close = TRUE;
3540 }
3541 if (!close)
3542 {
3543 doc->priv->info_bars[MSG_TYPE_RELOAD] = bar;
3544 protect_document(doc);
3545 return;
3546 }
3547 gtk_widget_destroy(bar);
3548}
3549
3550
3551static gboolean on_sci_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
3552{
3553 GtkInfoBar *bar = GTK_INFO_BAR(data);
3554
3555 g_return_val_if_fail(event->type == GDK_KEY_PRESS, FALSE);
3556
3557 switch (event->keyval)
3558 {
3559 case GDK_KEY_Tab:
3560 case GDK_KEY_ISO_Left_Tab:
3561 {
3562 GtkWidget *action_area = gtk_info_bar_get_action_area(bar);
3563 GtkDirectionType dir = event->keyval == GDK_KEY_Tab ? GTK_DIR_TAB_FORWARD : GTK_DIR_TAB_BACKWARD;
3564 gtk_widget_child_focus(action_area, dir);
3565 return TRUE;
3566 }
3567 case GDK_KEY_Escape:
3568 {
3569 gtk_info_bar_response(bar, GTK_RESPONSE_CANCEL);
3570 return TRUE;
3571 }
3572 default:
3573 return FALSE;
3574 }
3575}
3576
3577
3578/* Sets up a signal handler to intercept some keys during the lifetime of the GtkInfoBar */
3579static void enable_key_intercept(GeanyDocument *doc, GtkWidget *bar)
3580{
3581 /* automatically focus editor again on bar close */
3582 g_signal_connect_object(bar, "destroy", G_CALLBACK(gtk_widget_grab_focus), doc->editor->sci,
3583 G_CONNECT_SWAPPED);
3584 g_signal_connect_object(doc->editor->sci, "key-press-event", G_CALLBACK(on_sci_key), bar, 0);
3585}
3586
3587
3589{
3591 {
3593 return;
3594 }
3595
3596 gchar *base_name = g_path_get_basename(doc->file_name);
3597
3598 /* show this message only once */
3599 if (doc->priv->info_bars[MSG_TYPE_RELOAD] == NULL)
3600 {
3601 GtkWidget *bar;
3602
3603 bar = document_show_message(doc, GTK_MESSAGE_QUESTION, on_monitor_reload_file_response,
3604 _("_Reload"), RESPONSE_DOCUMENT_RELOAD,
3605 _("_Overwrite"), RESPONSE_DOCUMENT_SAVE,
3606 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
3607 _("Do you want to reload it?"),
3608 _("The file '%s' on the disk is more recent than the current buffer."),
3609 base_name);
3610
3611 protect_document(doc);
3612 doc->priv->info_bars[MSG_TYPE_RELOAD] = bar;
3613 enable_key_intercept(doc, bar);
3614 }
3615 g_free(base_name);
3616}
3617
3618
3620 gint response_id,
3621 GeanyDocument *doc)
3622{
3623 gboolean close = TRUE;
3624
3625 unprotect_document(doc);
3626
3627 if (response_id == RESPONSE_DOCUMENT_SAVE)
3629
3630 if (close)
3631 {
3633 gtk_widget_destroy(bar);
3634 }
3635 else
3636 {
3637 /* protect back the document if save didn't occur */
3638 protect_document(doc);
3639 }
3640}
3641
3642
3644{
3645 if (doc->priv->info_bars[MSG_TYPE_RESAVE] == NULL)
3646 {
3647 GtkWidget *bar = doc->priv->info_bars[MSG_TYPE_RELOAD];
3648
3649 if (bar != NULL) /* the "file on disk is newer" warning is now moot */
3650 gtk_info_bar_response(GTK_INFO_BAR(bar), GTK_RESPONSE_CANCEL);
3651
3652 bar = document_show_message(doc, GTK_MESSAGE_WARNING,
3654 GTK_STOCK_SAVE, RESPONSE_DOCUMENT_SAVE,
3655 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
3656 NULL, GTK_RESPONSE_NONE,
3657 _("Try to resave the file?"),
3658 _("File \"%s\" was not found on disk!"),
3659 doc->file_name);
3660
3661 protect_document(doc);
3662 document_set_text_changed(doc, TRUE);
3663 /* don't prompt more than once */
3664 SETPTR(doc->real_path, NULL);
3665 doc->priv->info_bars[MSG_TYPE_RESAVE] = bar;
3666 enable_key_intercept(doc, bar);
3667 }
3668}
3669
3670
3671/* Set force to force a disk check, otherwise it is ignored if there was a check
3672 * in the last file_prefs.disk_check_timeout seconds.
3673 * @return @c TRUE if the file has changed. */
3674gboolean document_check_disk_status(GeanyDocument *doc, gboolean force)
3675{
3676 gboolean ret = FALSE;
3677 gboolean use_gio_filemon;
3678 time_t mtime = 0;
3679 gchar *locale_filename;
3680 FileDiskStatus old_status;
3681
3682 g_return_val_if_fail(doc != NULL, FALSE);
3683
3684 /* ignore remote files and documents that have never been saved to disk */
3686 || doc->real_path == NULL || doc->priv->is_remote)
3687 return FALSE;
3688
3689 use_gio_filemon = (doc->priv->monitor != NULL);
3690
3691 if (use_gio_filemon)
3692 {
3693 if (doc->priv->file_disk_status != FILE_CHANGED && ! force)
3694 return FALSE;
3695 }
3696 else
3697 {
3698 time_t cur_time = time(NULL);
3699
3700 if (! force && doc->priv->last_check > (cur_time - file_prefs.disk_check_timeout))
3701 return FALSE;
3702
3703 doc->priv->last_check = cur_time;
3704 }
3705
3706 locale_filename = utils_get_locale_from_utf8(doc->file_name);
3707 if (!get_mtime(locale_filename, &mtime))
3708 {
3710 /* doc may be closed now */
3711 ret = TRUE;
3712 }
3713 else if (doc->priv->mtime < mtime)
3714 {
3715 /* make sure the user is not prompted again after he cancelled the "reload file?" message */
3716 doc->priv->mtime = mtime;
3718 /* doc may be closed now */
3719 ret = TRUE;
3720 }
3721 g_free(locale_filename);
3722
3723 if (DOC_VALID(doc))
3724 { /* doc can get invalid when a document was closed */
3725 old_status = doc->priv->file_disk_status;
3727 if (old_status != doc->priv->file_disk_status)
3729 }
3730 return ret;
3731}
3732
3733
3734/** Compares documents by their display names.
3735 * This matches @c GCompareFunc for use with e.g. @c g_ptr_array_sort().
3736 * @note 'Display name' means the base name of the document's filename.
3737 *
3738 * @param a @c GeanyDocument**.
3739 * @param b @c GeanyDocument**.
3740 * @warning The arguments take the address of each document pointer.
3741 * @return Negative value if a < b; zero if a = b; positive value if a > b.
3742 *
3743 * @since 0.21
3744 */
3745GEANY_API_SYMBOL
3746gint document_compare_by_display_name(gconstpointer a, gconstpointer b)
3747{
3748 GeanyDocument *doc_a = *((GeanyDocument**) a);
3749 GeanyDocument *doc_b = *((GeanyDocument**) b);
3750 gchar *base_name_a, *base_name_b;
3751 gint result;
3752
3753 base_name_a = g_path_get_basename(DOC_FILENAME(doc_a));
3754 base_name_b = g_path_get_basename(DOC_FILENAME(doc_b));
3755
3756 result = strcmp(base_name_a, base_name_b);
3757
3758 g_free(base_name_a);
3759 g_free(base_name_b);
3760
3761 return result;
3762}
3763
3764
3765/** Compares documents by their tab order.
3766 * This matches @c GCompareFunc for use with e.g. @c g_ptr_array_sort().
3767 *
3768 * @param a @c GeanyDocument**.
3769 * @param b @c GeanyDocument**.
3770 * @warning The arguments take the address of each document pointer.
3771 * @return Negative value if a < b; zero if a = b; positive value if a > b.
3772 *
3773 * @since 0.21 (GEANY_API_VERSION 209)
3774 */
3775GEANY_API_SYMBOL
3776gint document_compare_by_tab_order(gconstpointer a, gconstpointer b)
3777{
3778 GeanyDocument *doc_a = *((GeanyDocument**) a);
3779 GeanyDocument *doc_b = *((GeanyDocument**) b);
3780 gint notebook_position_doc_a;
3781 gint notebook_position_doc_b;
3782
3783 notebook_position_doc_a = document_get_notebook_page(doc_a);
3784 notebook_position_doc_b = document_get_notebook_page(doc_b);
3785
3786 if (notebook_position_doc_a < notebook_position_doc_b)
3787 return -1;
3788 if (notebook_position_doc_a > notebook_position_doc_b)
3789 return 1;
3790 /* equality */
3791 return 0;
3792}
3793
3794
3795/** Compares documents by their tab order, in reverse order.
3796 * This matches @c GCompareFunc for use with e.g. @c g_ptr_array_sort().
3797 *
3798 * @param a @c GeanyDocument**.
3799 * @param b @c GeanyDocument**.
3800 * @warning The arguments take the address of each document pointer.
3801 * @return Negative value if a < b; zero if a = b; positive value if a > b.
3802 *
3803 * @since 0.21 (GEANY_API_VERSION 209)
3804 */
3805GEANY_API_SYMBOL
3806gint document_compare_by_tab_order_reverse(gconstpointer a, gconstpointer b)
3807{
3808 return -1 * document_compare_by_tab_order(a, b);
3809}
3810
3811
3813{
3814 g_return_if_fail(doc != NULL);
3815
3816 gtk_widget_grab_focus(GTK_WIDGET(doc->editor->sci));
3817}
3818
3819static void *copy_(void *src) { return src; }
3820static void free_(void *doc) { }
3821
3822/** @gironly
3823 * Gets the GType of GeanyDocument
3824 *
3825 * @return the GeanyDocument type */
3826GEANY_API_SYMBOL
3828
3830
3831
3832gpointer document_get_data(const GeanyDocument *doc, const gchar *key)
3833{
3834 return g_datalist_get_data(&doc->priv->data, key);
3835}
3836
3837
3838void document_set_data(GeanyDocument *doc, const gchar *key, gpointer data)
3839{
3840 g_datalist_set_data(&doc->priv->data, key, data);
3841}
3842
3843
3844void document_set_data_full(GeanyDocument *doc, const gchar *key,
3845 gpointer data, GDestroyNotify free_func)
3846{
3847 g_datalist_set_data_full(&doc->priv->data, key, data, free_func);
3848}
#define INVALID_POSITION
Definition: Scintilla.h:44
#define SCFIND_REGEXP
Definition: Scintilla.h:419
#define SCI_GETCHARACTERPOINTER
Definition: Scintilla.h:870
#define SC_SEL_RECTANGLE
Definition: Scintilla.h:787
Contains the GeanyApp.
void build_menu_update(GeanyDocument *doc)
Definition: build.c:1457
const gchar * label
Definition: build.c:2676
gboolean ignore_callback
Definition: libmain.c:87
gboolean dialogs_show_question_full(GtkWidget *parent, const gchar *yes_btn, const gchar *no_btn, const gchar *extra_text, const gchar *main_text,...)
Definition: dialogs.c:1393
gboolean dialogs_show_save_as(void)
Shows the Save As dialog for the current notebook page.
Definition: dialogs.c:667
gboolean dialogs_show_unsaved_file(GeanyDocument *doc)
Definition: dialogs.c:797
void dialogs_show_msgbox(GtkMessageType type, const gchar *text,...)
Shows a message box of the type type with text.
Definition: dialogs.c:729
void dialogs_show_msgbox_with_secondary(GtkMessageType type, const gchar *text, const gchar *secondary)
Definition: dialogs.c:753
File related dialogs, miscellaneous dialogs, font dialog.
static void on_keep_edit_history_on_reload_response(GtkWidget *bar, gint response_id, GeanyDocument *doc)
Definition: document.c:1567
GeanyDocument * document_index(gint idx)
Accessor function for GeanyData::documents_array items.
Definition: document.c:3317
static void show_replace_summary(GeanyDocument *doc, gint count, const gchar *original_find_text, const gchar *original_replace_text)
Definition: document.c:2456
GType document_get_type(void)
GeanyDocument * document_get_current(void)
Finds the current document.
Definition: document.c:371
void document_init_doclist(void)
Definition: document.c:382
static gboolean on_document_update_tag_list_idle(gpointer data)
Definition: document.c:2755
static gboolean detect_tabs_and_spaces(GeanyEditor *editor)
Definition: document.c:1075
void document_update_tags(GeanyDocument *doc)
Definition: document.c:2647
GeanyDocument * document_open_file_full(GeanyDocument *doc, const gchar *filename, gint pos, gboolean readonly, GeanyFiletype *ft, const gchar *forced_enc)
Definition: document.c:1285
static gboolean save_file_handle_infobars(GeanyDocument *doc, gboolean force)
Definition: document.c:2043
static GtkWidget * document_show_message(GeanyDocument *doc, GtkMessageType msgtype, void(*response_cb)(GtkWidget *info_bar, gint response_id, GeanyDocument *doc), const gchar *btn_1, GtkResponseType response_1, const gchar *btn_2, GtkResponseType response_2, const gchar *btn_3, GtkResponseType response_3, const gchar *extra_text, const gchar *format,...)
Definition: document.c:3432
void document_redo(GeanyDocument *doc)
Definition: document.c:3094
static void get_line_column_from_pos(GeanyDocument *doc, guint byte_pos, gint *line, gint *column)
Definition: document.c:1680
gboolean document_close_all(void)
Definition: document.c:3396
static void force_close_all(void)
Definition: document.c:3381
static GtkWidget * document_get_notebook_child(GeanyDocument *doc)
Definition: document.c:264
static void monitor_file_setup(GeanyDocument *doc)
Definition: document.c:561
GeanyDocument * document_clone(GeanyDocument *old_doc)
Definition: document.c:3323
gint document_find_text(GeanyDocument *doc, const gchar *text, const gchar *original_text, GeanyFindFlags flags, gboolean search_backwards, GeanyMatchInfo **match_, gboolean scroll, GtkWidget *parent)
Definition: document.c:2317
gboolean document_can_redo(GeanyDocument *doc)
Definition: document.c:3083
GeanyDocument * document_new_file_if_non_open(void)
Definition: document.c:803
void document_grab_focus(GeanyDocument *doc)
Definition: document.c:3812
void document_undo_add(GeanyDocument *doc, guint type, gpointer data)
Definition: document.c:2955
void document_set_encoding(GeanyDocument *doc, const gchar *new_encoding)
Sets the encoding of a document.
Definition: document.c:2879
static void store_saved_encoding(GeanyDocument *doc)
Definition: document.c:794
static void unprotect_document(GeanyDocument *doc)
Definition: document.c:1780
void document_set_data_full(GeanyDocument *doc, const gchar *key, gpointer data, GDestroyNotify free_func)
Definition: document.c:3844
static void enable_key_intercept(GeanyDocument *doc, GtkWidget *bar)
Definition: document.c:3579
static void monitor_reload_file(GeanyDocument *doc)
Definition: document.c:3588
const GdkColor * document_get_status_color(GeanyDocument *doc)
Gets the status color of the document, or NULL if default widget coloring should be used.
Definition: document.c:3276
gboolean document_save_file_as(GeanyDocument *doc, const gchar *utf8_fname)
Saves the document, detecting the filetype.
Definition: document.c:1815
static gsize save_convert_to_encoding(GeanyDocument *doc, gchar **data, gsize *len)
Definition: document.c:1867
static guint doc_id_counter
Definition: document.c:107
GeanyDocument * document_get_from_notebook_child(GtkWidget *page)
Definition: document.c:331
static gchar * save_doc(GeanyDocument *doc, const gchar *locale_filename, const gchar *data, gsize len)
Definition: document.c:2020
static void * copy_(void *src)
Definition: document.c:3819
GeanyDocument * document_get_from_page(guint page_num)
Finds the document for the given notebook page page_num.
Definition: document.c:352
gint document_get_notebook_page(GeanyDocument *doc)
Gets the notebook page index for a document.
Definition: document.c:289
static void protect_document(GeanyDocument *doc)
Definition: document.c:1770
G_DEFINE_BOXED_TYPE(GeanyDocument, document, copy_, free_)
gboolean document_search_bar_find(GeanyDocument *doc, const gchar *text, gboolean inc, gboolean backwards)
Definition: document.c:2241
void document_highlight_tags(GeanyDocument *doc)
Definition: document.c:2702
void document_show_tab(GeanyDocument *doc)
Definition: document.c:1273
@ RESPONSE_DOCUMENT_RELOAD
Definition: document.c:102
@ RESPONSE_DOCUMENT_SAVE
Definition: document.c:103
gboolean document_reload_force(GeanyDocument *doc, const gchar *forced_enc)
Reloads the document with the specified file encoding.
Definition: document.c:1595
gboolean document_check_disk_status(GeanyDocument *doc, gboolean force)
Definition: document.c:3674
static void queue_colourise(GeanyDocument *doc)
Definition: document.c:493
static void free_(void *doc)
Definition: document.c:3820
static guint document_replace_range(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text, GeanyFindFlags flags, gint start, gint end, gboolean scroll_to_match, gint *new_range_end)
Definition: document.c:2483
GeanyDocument * document_find_by_sci(ScintillaObject *sci)
Definition: document.c:214
gboolean document_detect_indent_type(GeanyDocument *doc, GeanyIndentType *type_)
Definition: document.c:1107
gboolean document_save_file(GeanyDocument *doc, gboolean force)
Saves the document.
Definition: document.c:2103
void document_try_focus(GeanyDocument *doc, GtkWidget *source_widget)
Definition: document.c:595
gpointer document_get_data(const GeanyDocument *doc, const gchar *key)
Definition: document.c:3832
void document_set_filetype(GeanyDocument *doc, GeanyFiletype *type)
Sets the filetype of the document (which controls syntax highlighting and tags)
Definition: document.c:2826
void document_rename_file(GeanyDocument *doc, const gchar *new_filename)
Renames the file in doc to new_filename.
Definition: document.c:1749
const gchar * name
Definition: document.c:3219
gint document_replace_text(GeanyDocument *doc, const gchar *find_text, const gchar *original_find_text, const gchar *replace_text, GeanyFindFlags flags, gboolean search_backwards)
Definition: document.c:2397
static void on_monitor_resave_missing_file_response(GtkWidget *bar, gint response_id, GeanyDocument *doc)
Definition: document.c:3619
gint document_compare_by_display_name(gconstpointer a, gconstpointer b)
Compares documents by their display names.
Definition: document.c:3746
static void document_redo_add(GeanyDocument *doc, guint type, gpointer data)
Definition: document.c:3191
static gchar * write_data_to_disk(const gchar *locale_filename, const gchar *data, gsize len)
Definition: document.c:1928
gboolean loaded
Definition: document.c:3221
void document_update_tag_list_in_idle(GeanyDocument *doc)
Definition: document.c:2772
void document_apply_indent_settings(GeanyDocument *doc)
Definition: document.c:1225
static gboolean get_mtime(const gchar *locale_filename, time_t *time)
Definition: document.c:926
static void replace_header_filename(GeanyDocument *doc)
Definition: document.c:1704
gint document_compare_by_tab_order(gconstpointer a, gconstpointer b)
Compares documents by their tab order.
Definition: document.c:3776
static ScintillaObject * locate_sci_in_container(GtkWidget *container)
Definition: document.c:301
gboolean document_need_save_as(GeanyDocument *doc)
Definition: document.c:1795
static gint set_cursor_position(GeanyEditor *editor, gint pos)
Definition: document.c:1047
const gchar * document_get_status_widget_class(GeanyDocument *doc)
Definition: document.c:3249
static void monitor_resave_missing_file(GeanyDocument *doc)
Definition: document.c:3643
gboolean document_account_for_unsaved(void)
Definition: document.c:3360
static gint document_get_status_id(GeanyDocument *doc)
Definition: document.c:3229
static void document_stop_file_monitoring(GeanyDocument *doc)
Definition: document.c:549
static void document_update_timestamp(GeanyDocument *doc, const gchar *locale_filename)
Definition: document.c:1668
static gint document_get_new_idx(void)
Definition: document.c:478
@ STATUS_READONLY
Definition: document.c:3214
@ STATUS_CHANGED
Definition: document.c:3212
@ STATUS_DISK_CHANGED
Definition: document.c:3213
GeanyDocument * document_find_by_filename(const gchar *utf8_filename)
Finds a document with the given filename.
Definition: document.c:183
static GeanyDocument * document_create(const gchar *utf8_filename)
Definition: document.c:621
GeanyFilePrefs file_prefs
Definition: document.c:86
static gboolean load_text_file(const gchar *locale_filename, const gchar *display_filename, FileData *filedata, const gchar *forced_enc)
Definition: document.c:976
void document_reload_config(GeanyDocument *doc)
Definition: document.c:2864
GdkColor color
Definition: document.c:3220
static gboolean on_idle_focus(gpointer doc)
Definition: document.c:612
static gboolean on_sci_key(GtkWidget *widget, GdkEventKey *event, gpointer data)
Definition: document.c:3551
static void document_undo_clear(GeanyDocument *doc)
Definition: document.c:2921
static void update_changed_state(GeanyDocument *doc)
Definition: document.c:2975
void document_open_files(const GSList *filenames, gboolean readonly, GeanyFiletype *ft, const gchar *forced_enc)
Opens each file in the list filenames.
Definition: document.c:1555
static void document_undo_add_internal(GeanyDocument *doc, guint type, gpointer data)
Definition: document.c:2935
void document_undo(GeanyDocument *doc)
Definition: document.c:2985
void document_set_data(GeanyDocument *doc, const gchar *key, gpointer data)
Definition: document.c:3838
gboolean document_can_undo(GeanyDocument *doc)
Definition: document.c:2964
#define USE_GIO_FILE_OPERATIONS
Definition: document.c:83
gchar * document_get_basename_for_display(GeanyDocument *doc, gint length)
Returns the last part of the filename of the given GeanyDocument.
Definition: document.c:412
GeanyDocument * document_open_file(const gchar *locale_filename, gboolean readonly, GeanyFiletype *ft, const gchar *forced_enc)
Opens a document specified by locale_filename.
Definition: document.c:908
static gchar * get_real_path_from_utf8(const gchar *utf8_filename)
Definition: document.c:161
GPtrArray * documents_array
Definition: document.c:87
gboolean document_detect_indent_width(GeanyDocument *doc, gint *width_)
Definition: document.c:1219
static gboolean remove_page(guint page_num)
Definition: document.c:699
void document_open_file_list(const gchar *data, gsize length)
Definition: document.c:1521
static void document_load_config(GeanyDocument *doc, GeanyFiletype *type, gboolean filetype_changed)
Definition: document.c:2786
static struct @86 document_status_styles[]
static void document_undo_clear_stack(GTrashStack **stack)
Definition: document.c:2899
GeanyDocument * document_find_by_id(guint id)
Lookup an old document by its ID.
Definition: document.c:247
gboolean document_reload_prompt(GeanyDocument *doc, const gchar *forced_enc)
Definition: document.c:1635
gboolean document_remove_page(guint page_num)
Removes the given notebook tab at page_num and clears all related information in the document list.
Definition: document.c:782
void document_replace_sel(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text, const gchar *original_find_text, const gchar *original_replace_text, GeanyFindFlags flags)
Definition: document.c:2520
gboolean document_close(GeanyDocument *doc)
Closes the given document.
Definition: document.c:689
GeanyDocument * document_find_by_real_path(const gchar *realname)
Finds a document whose real_path field matches the given filename.
Definition: document.c:137
static void on_monitor_reload_file_response(GtkWidget *bar, gint response_id, GeanyDocument *doc)
Definition: document.c:3518
gint document_compare_by_tab_order_reverse(gconstpointer a, gconstpointer b)
Compares documents by their tab order, in reverse order.
Definition: document.c:3806
void document_update_tab_label(GeanyDocument *doc)
Definition: document.c:430
gint document_replace_all(GeanyDocument *doc, const gchar *find_text, const gchar *replace_text, const gchar *original_find_text, const gchar *original_replace_text, GeanyFindFlags flags)
Definition: document.c:2623
void document_set_text_changed(GeanyDocument *doc, gboolean changed)
Updates the tab labels, the status bar, the window title and some save-sensitive buttons according to...
Definition: document.c:460
static gboolean detect_indent_width(GeanyEditor *editor, GeanyIndentType type, gint *width_)
Definition: document.c:1155
GeanyDocument * document_new_file(const gchar *utf8_filename, GeanyFiletype *ft, const gchar *text)
Creates a new document.
Definition: document.c:824
void document_finalize(void)
Definition: document.c:388
Document related actions: new, save, open, etc.
#define DOC_VALID(doc_ptr)
Null-safe way to check GeanyDocument::is_valid.
Definition: document.h:162
#define DOC_FILENAME(doc)
Returns the filename of the document passed or GEANY_STRING_UNTITLED (e.g.
Definition: document.h:170
#define documents
Wraps GeanyData::documents_array so it can be used with C array syntax.
Definition: document.h:130
#define foreach_document(i)
Iterates all valid document indexes.
Definition: document.h:153
FileDiskStatus
@ FILE_CHANGED
@ FILE_IGNORE
@ FILE_OK
@ MSG_TYPE_RELOAD
@ MSG_TYPE_POST_RELOAD
@ MSG_TYPE_RESAVE
@ UNDO_RELOAD
@ UNDO_ENCODING
@ UNDO_SCINTILLA
@ UNDO_BOM
@ UNDO_EOL
gint editor_get_eol_char_mode(GeanyEditor *editor)
Retrieves the end of line characters mode (LF, CR/LF, CR) in the given editor.
Definition: editor.c:4279
gboolean editor_line_in_view(GeanyEditor *editor, gint line)
Definition: editor.c:4087
GeanyEditor * editor_create(GeanyDocument *doc)
Definition: editor.c:5011
const GeanyIndentPrefs * editor_get_indent_prefs(GeanyEditor *editor)
Gets the indentation prefs for the editor.
Definition: editor.c:1267
void editor_replace_tabs(GeanyEditor *editor, gboolean ignore_selection)
Definition: editor.c:4396
void editor_scroll_to_line(GeanyEditor *editor, gint line, gfloat percent_of_view)
Definition: editor.c:3674
void editor_set_indent(GeanyEditor *editor, GeanyIndentType type, gint width)
Definition: editor.c:4668
void editor_sci_notify_cb(G_GNUC_UNUSED GtkWidget *widget, G_GNUC_UNUSED gint scn, gpointer scnt, gpointer data)
Definition: editor.c:1055
gint editor_get_eol_char_len(GeanyEditor *editor)
Retrieves the length of the used end of line characters (LF, CR/LF, CR) in the given editor.
Definition: editor.c:4323
void editor_ensure_final_newline(GeanyEditor *editor)
Definition: editor.c:4580
gchar * text
Definition: editor.c:83
void editor_strip_trailing_spaces(GeanyEditor *editor, gboolean ignore_selection)
Definition: editor.c:4547
ScintillaObject * sci
Definition: editor.c:88
void editor_set_indentation_guides(GeanyEditor *editor)
Definition: editor.c:5091
void editor_destroy(GeanyEditor *editor)
Definition: editor.c:5030
gint pos
Definition: editor.c:87
GeanyEditorPrefs editor_prefs
Definition: editor.c:77
gboolean editor_goto_pos(GeanyEditor *editor, gint pos, gboolean mark)
Moves to position pos, switching to the document if necessary, setting a marker if mark is set.
Definition: editor.c:4732
GeanyIndentType
Whether to use tabs, spaces or both to indent.
Definition: editor.h:45
@ GEANY_INDENT_TYPE_BOTH
Both.
Definition: editor.h:48
@ GEANY_INDENT_TYPE_TABS
Tabs.
Definition: editor.h:47
@ GEANY_INDENT_TYPE_SPACES
Spaces.
Definition: editor.h:46
gboolean encodings_convert_to_utf8_auto(gchar **buf, gsize *size, const gchar *forced_enc, gchar **used_encoding, gboolean *has_bom, gboolean *partial)
Definition: encodings.c:1057
GeanyEncoding encodings[GEANY_ENCODINGS_MAX]
Definition: encodings.c:59
gboolean encodings_is_unicode_charset(const gchar *string)
Definition: encodings.c:855
Encoding conversion and Byte Order Mark (BOM) handling.
@ GEANY_ENCODING_NONE
Definition: encodings.h:121
void error(const errorSelection selection, const char *const format,...)
Definition: error.c:53
currently not working see documentation true uses any is tabs
Definition: filetypes.d:66
@ GEANY_FILETYPES_JAVA
Definition: filetypes.h:65
@ GEANY_FILETYPES_CS
Definition: filetypes.h:72
@ GEANY_FILETYPES_CPP
Definition: filetypes.h:80
@ GEANY_FILETYPES_NONE
Definition: filetypes.h:46
@ GEANY_FILETYPES_D
Definition: filetypes.h:86
@ GEANY_FILETYPES_VALA
Definition: filetypes.h:74
@ GEANY_FILETYPES_C
Definition: filetypes.h:55
@ GEANY_FILETYPES_RUST
Definition: filetypes.h:104
@ GEANY_FILETYPES_GO
Definition: filetypes.h:106
@ GEANY_FILETYPES_OBJECTIVEC
Definition: filetypes.h:99
@ GEANY_FILETYPES_MAKE
Definition: filetypes.h:59
#define filetypes
Wraps GeanyData::filetypes_array so it can be used with C array syntax.
Definition: filetypes.h:178
int errno
#define GEANY_STRING_UNTITLED
Definition: geany.h:49
vString * line
Definition: geany_cobol.c:133
CobolFormat format
Definition: geany_cobol.c:137
unsigned int count
tokenInfo * list
static bool match(const unsigned char *line, const char *word)
Definition: geany_tcl.c:55
GObject * geany_object
Definition: geanyobject.c:41
GtkWidget * geany_wrap_label_new(const gchar *text)
void highlighting_set_styles(ScintillaObject *sci, GeanyFiletype *ft)
Sets up highlighting and other visual settings.
Syntax highlighting for the different filetypes, using the Scintilla lexers.
CommandLineOptions cl_options
Definition: libmain.c:90
GeanyApp * app
Definition: libmain.c:86
GeanyStatus main_status
Definition: libmain.c:89
void geany_debug(gchar const *format,...)
Definition: log.c:67
Main program-related commands.
#define MAX(a, b)
Definition: mio.c:93
void msgwin_status_add(const gchar *format,...)
Logs a formatted status message without setting the status bar.
Definition: msgwindow.c:520
Message window functions (status, compiler, messages windows).
void navqueue_remove_file(const gchar *filename)
Definition: navqueue.c:262
Simple code navigation.
gboolean notebook_switch_in_progress(void)
Definition: notebook.c:298
void notebook_remove_page(gint page_num)
Definition: notebook.c:757
gint notebook_new_tab(GeanyDocument *this)
Definition: notebook.c:671
const GeanyFilePrefs * project_get_file_prefs(void)
Definition: project.c:1327
Project Management.
#define NULL
Definition: rbtree.h:150
void sci_set_savepoint(ScintillaObject *sci)
Definition: sciwrappers.c:895
void sci_set_selection_mode(ScintillaObject *sci, gint mode)
Sets selection mode.
Definition: sciwrappers.c:1341
void sci_set_keywords(ScintillaObject *sci, guint k, const gchar *text)
Definition: sciwrappers.c:1160
void sci_undo(ScintillaObject *sci)
Definition: sciwrappers.c:313
gint sci_get_eol_mode(ScintillaObject *sci)
Definition: sciwrappers.c:262
gint sci_get_line_count(ScintillaObject *sci)
Gets the total number of lines.
Definition: sciwrappers.c:555
gint sci_replace_target(ScintillaObject *sci, const gchar *text, gboolean regex)
Definition: sciwrappers.c:1154
gboolean sci_is_modified(ScintillaObject *sci)
Definition: sciwrappers.c:359
void sci_set_tab_width(ScintillaObject *sci, gint width)
Definition: sciwrappers.c:865
void sci_get_text(ScintillaObject *sci, gint len, gchar *text)
Gets all text.
Definition: sciwrappers.c:727
void sci_set_target_start(ScintillaObject *sci, gint start)
Definition: sciwrappers.c:1140
void sci_goto_pos(ScintillaObject *sci, gint pos, gboolean unfold)
Definition: sciwrappers.c:929
gchar * sci_get_selection_contents(ScintillaObject *sci)
Gets selected text.
Definition: sciwrappers.c:778
void sci_ensure_line_is_visible(ScintillaObject *sci, gint line)
Makes line visible (folding may have hidden it).
Definition: sciwrappers.c:816
gint sci_get_pos_at_line_sel_start(ScintillaObject *sci, gint line)
Definition: sciwrappers.c:1315
gint sci_get_length(ScintillaObject *sci)
Gets the length of all text.
Definition: sciwrappers.c:656
gint sci_get_current_position(ScintillaObject *sci)
Gets the cursor position.
Definition: sciwrappers.c:507
void sci_set_undo_collection(ScintillaObject *sci, gboolean set)
Definition: sciwrappers.c:347
void sci_set_eol_mode(ScintillaObject *sci, gint eolmode)
Definition: sciwrappers.c:268
void sci_set_selection_start(ScintillaObject *sci, gint position)
Sets the selection start position.
Definition: sciwrappers.c:565
gint sci_get_selection_mode(ScintillaObject *sci)
Gets selection mode.
Definition: sciwrappers.c:1331
gint sci_get_line_length(ScintillaObject *sci, gint line)
Gets line length.
Definition: sciwrappers.c:689
void sci_set_text(ScintillaObject *sci, const gchar *text)
Sets all text.
Definition: sciwrappers.c:293
void sci_set_lines_wrapped(ScintillaObject *sci, gboolean set)
Definition: sciwrappers.c:253
void sci_get_text_range(ScintillaObject *sci, gint start, gint end, gchar *text)
Gets text between start and end.
Definition: sciwrappers.c:1085
void sci_scroll_caret(ScintillaObject *sci)
Scrolls the cursor in view.
Definition: sciwrappers.c:955
void sci_set_readonly(ScintillaObject *sci, gboolean readonly)
Definition: sciwrappers.c:1166
void sci_set_line_numbers(ScintillaObject *sci, gboolean set)
Definition: sciwrappers.c:98
gint sci_get_lines_selected(ScintillaObject *sci)
Definition: sciwrappers.c:1199
void sci_set_search_anchor(ScintillaObject *sci)
Definition: sciwrappers.c:936
gint sci_get_line_indentation(ScintillaObject *sci, gint line)
Gets the indentation width of a line.
Definition: sciwrappers.c:1376
gint sci_get_selection_end(ScintillaObject *sci)
Gets the selection end position.
Definition: sciwrappers.c:636
gint sci_get_pos_at_line_sel_end(ScintillaObject *sci, gint line)
Definition: sciwrappers.c:1321
gint sci_get_line_indent_position(ScintillaObject *sci, gint line)
Definition: sciwrappers.c:1271
void sci_clear_all(ScintillaObject *sci)
Definition: sciwrappers.c:853
gint sci_find_text(ScintillaObject *sci, gint flags, struct Sci_TextToFind *ttf)
Finds text in the document.
Definition: sciwrappers.c:995
void sci_convert_eols(ScintillaObject *sci, gint eolmode)
Definition: sciwrappers.c:274
void sci_set_current_position(ScintillaObject *sci, gint position, gboolean scroll_to_caret)
Sets the cursor position.
Definition: sciwrappers.c:529
gboolean sci_can_redo(ScintillaObject *sci)
Definition: sciwrappers.c:307
gchar sci_get_char_at(ScintillaObject *sci, gint pos)
Gets a character.
Definition: sciwrappers.c:889
gboolean sci_has_selection(ScintillaObject *sci)
Checks if there's a selection.
Definition: sciwrappers.c:920
void sci_start_undo_action(ScintillaObject *sci)
Begins grouping a set of edits together as one Undo action.
Definition: sciwrappers.c:331
void sci_end_undo_action(ScintillaObject *sci)
Ends grouping a set of edits together as one Undo action.
Definition: sciwrappers.c:341
void sci_set_target_end(ScintillaObject *sci, gint end)
Definition: sciwrappers.c:1147
void sci_empty_undo_buffer(ScintillaObject *sci)
Definition: sciwrappers.c:353
gchar * sci_get_contents(ScintillaObject *sci, gint buffer_len)
Allocates and fills a buffer with text from the start of the document.
Definition: sciwrappers.c:743
gint sci_get_selection_start(ScintillaObject *sci)
Gets the selection start position.
Definition: sciwrappers.c:626
gint sci_get_line_from_position(ScintillaObject *sci, gint position)
Gets the line number from position.
Definition: sciwrappers.c:469
gint sci_get_position_from_line(ScintillaObject *sci, gint line)
Gets the position for the start of line.
Definition: sciwrappers.c:497
void sci_goto_line(ScintillaObject *sci, gint line, gboolean unfold)
Jumps to the specified line in the document.
Definition: sciwrappers.c:1033
void sci_redo(ScintillaObject *sci)
Definition: sciwrappers.c:320
void sci_set_selection_end(ScintillaObject *sci, gint position)
Sets the selection end position.
Definition: sciwrappers.c:575
gboolean sci_can_undo(ScintillaObject *sci)
Definition: sciwrappers.c:301
Wrapper functions for the Scintilla editor widget SCI_* messages.
void geany_match_info_free(GeanyMatchInfo *info)
Definition: search.c:1184
gint search_find_text(ScintillaObject *sci, GeanyFindFlags flags, struct Sci_TextToFind *ttf, GeanyMatchInfo **match_)
Definition: search.c:2110
guint search_replace_range(ScintillaObject *sci, struct Sci_TextToFind *ttf, GeanyFindFlags flags, const gchar *replace_text)
Definition: search.c:2243
gint search_replace_match(ScintillaObject *sci, const GeanyMatchInfo *match, const gchar *replace_text)
Definition: search.c:2064
gint search_find_next(ScintillaObject *sci, const gchar *str, GeanyFindFlags flags, GeanyMatchInfo **match_)
Definition: search.c:2025
GeanySearchPrefs search_prefs
Definition: search.c:78
gint search_find_prev(ScintillaObject *sci, const gchar *str, GeanyFindFlags flags, GeanyMatchInfo **match_)
Definition: search.c:2012
GeanyFindFlags
Definition: search.h:36
@ GEANY_FIND_REGEXP
Definition: search.h:40
@ GEANY_FIND_MATCHCASE
Definition: search.h:37
GtkWidget * reload
Definition: sidebar.c:58
SidebarTreeviews tv
Definition: sidebar.c:50
void sidebar_remove_document(GeanyDocument *doc)
Definition: sidebar.c:554
void sidebar_openfiles_update(GeanyDocument *doc)
Definition: sidebar.c:507
GtkWidget * close
Definition: sidebar.c:56
void sidebar_openfiles_add(GeanyDocument *doc)
Definition: sidebar.c:462
void sidebar_update_tag_list(GeanyDocument *doc, gboolean update)
Definition: sidebar.c:188
void filetypes_select_radio_item(const GeanyFiletype *ft)
Definition: filetypes.c:784
gboolean filetype_has_tags(GeanyFiletype *ft)
Definition: filetypes.c:1227
GeanyFiletype * filetypes_detect_from_document(GeanyDocument *doc)
Definition: filetypes.c:723
const gchar filename[]
Definition: stash-example.c:4
gtk_widget_show_all(dialog)
gboolean readonly
Definition: document.c:922
gsize len
Definition: document.c:918
gchar * enc
Definition: document.c:919
time_t mtime
Definition: document.c:921
gchar * data
Definition: document.c:917
gboolean bom
Definition: document.c:920
gboolean has_bom
gchar * encoding
const TMWorkspace * tm_workspace
TagManager workspace/session tags.
Definition: app.h:48
GTrashStack * redo_actions
FileDiskStatus file_disk_status
FileEncoding saved_encoding
GTrashStack * undo_actions
GtkWidget * info_bars[NUM_MSG_TYPES]
Structure for representing an open tab with all its properties.
Definition: document.h:81
gchar * file_name
The UTF-8 encoded file name.
Definition: document.h:92
gboolean changed
Whether this document has been changed since it was last saved.
Definition: document.h:107
GeanyFiletype * file_type
The filetype for this document, it's only a reference to one of the elements of the global filetypes ...
Definition: document.h:101
struct GeanyDocumentPrivate * priv
Definition: document.h:121
gboolean has_bom
Internally used flag to indicate whether the file of this document has a byte-order-mark.
Definition: document.h:97
gchar * real_path
The link-dereferenced, locale-encoded file name.
Definition: document.h:115
gchar * encoding
The encoding of the document, must be a valid string representation of an encoding,...
Definition: document.h:95
gboolean readonly
Whether this document is read-only.
Definition: document.h:105
gboolean is_valid
Flag used to check if this document is valid when iterating GeanyData::documents_array.
Definition: document.h:83
gint index
Index in the documents array.
Definition: document.h:84
GeanyEditor * editor
The editor associated with the document.
Definition: document.h:98
guint id
A pseudo-unique ID for this document.
Definition: document.h:119
TMSourceFile * tm_file
TMSourceFile object for this document, or NULL.
Definition: document.h:103
gint autocompletion_update_freq
Definition: editor.h:138
gboolean show_linenumber_margin
Definition: editor.h:109
Editor-owned fields for each document.
Definition: editor.h:150
GeanyIndentType indent_type
Definition: editor.h:157
gboolean line_breaking
Whether to split long lines as you type.
Definition: editor.h:158
gboolean auto_indent
TRUE if auto-indentation is enabled.
Definition: editor.h:154
ScintillaObject * sci
The Scintilla editor GtkWidget.
Definition: editor.h:152
gboolean line_wrapping
TRUE if line wrapping is enabled.
Definition: editor.h:153
gint indent_width
Definition: editor.h:159
gfloat scroll_percent
Percentage to scroll view by on paint, if positive.
Definition: editor.h:156
const gchar * charset
File Prefs.
Definition: document.h:47
gboolean reload_clean_doc_on_file_change
Definition: document.h:68
gboolean use_safe_file_saving
Definition: document.h:60
gboolean keep_edit_history_on_reload
Definition: document.h:66
gint default_new_encoding
Definition: document.h:48
gboolean show_keep_edit_history_on_reload_msg
Definition: document.h:67
gint disk_check_timeout
Definition: document.h:58
gboolean gio_unsafe_save_backup
Definition: document.h:62
gboolean strip_trailing_spaces
Definition: document.h:51
gboolean final_new_line
Definition: document.h:50
gint default_eol_character
Definition: document.h:57
gboolean ensure_convert_new_lines
Definition: document.h:61
gboolean replace_tabs
Definition: document.h:52
Represents a filetype.
Definition: filetypes.h:144
gchar * name
Untranslated short name, such as "C", "None".
Definition: filetypes.h:152
GeanyFiletypeID id
Index in filetypes.
Definition: filetypes.h:145
gint indent_width
Definition: filetypes.h:169
TMParserType lang
Definition: filetypes.h:148
gint indent_type
Definition: filetypes.h:168
struct GeanyFiletypePrivate * priv
Definition: filetypes.h:171
gchar * extension
Default file extension for new files, or NULL.
Definition: filetypes.h:155
Indentation prefs that might be different according to project or filetype.
Definition: editor.h:83
gboolean detect_type
Definition: editor.h:90
GeanyIndentType type
Whether to use tabs, spaces or both to indent.
Definition: editor.h:85
gboolean detect_width
Definition: editor.h:91
gint width
Indent width.
Definition: editor.h:84
gint symbols_sort_mode
symbol list sorting mode
Definition: ui_utils.h:71
GtkWidget * window
Main window.
Definition: ui_utils.h:80
GtkWidget * notebook
Document notebook.
Definition: ui_utils.h:83
gboolean always_wrap
Definition: search.h:58
Sci_PositionCR cpMin
Definition: Scintilla.h:1177
Sci_PositionCR cpMax
Definition: Scintilla.h:1178
const char * lpstrText
Definition: Scintilla.h:1188
struct Sci_CharacterRange chrg
Definition: Scintilla.h:1187
struct Sci_CharacterRange chrgText
Definition: Scintilla.h:1189
GtkWidget * tree_openfiles
Definition: sidebar.h:34
GPtrArray * tags_array
Sorted tags from all source files (just pointers to source file tags, the tag objects are owned by th...
Definition: tm_workspace.h:31
GTrashStack * next
Definition: document.c:93
gpointer * data
Definition: document.c:95
guint type
Definition: document.c:94
Defines internationalization macros.
#define _(String)
Definition: support.h:42
#define ngettext(String, PluralString, Number)
Definition: support.h:41
GString * symbols_find_typenames_as_string(TMParserType lang, gboolean global)
Definition: symbols.c:206
void symbols_global_tags_loaded(guint file_type_idx)
Definition: symbols.c:177
Tag-related functions.
void tm_source_file_free(TMSourceFile *source_file)
Decrements the reference count of source_file.
TMSourceFile * tm_source_file_new(const char *file_name, const char *name)
Initializes a TMSourceFile structure and returns a pointer to it.
const gchar * tm_source_file_get_lang_name(TMParserType lang)
void tm_workspace_add_source_file_noupdate(TMSourceFile *source_file)
Definition: tm_workspace.c:190
void tm_workspace_remove_source_file(TMSourceFile *source_file)
Removes a source file from the workspace if it exists.
Definition: tm_workspace.c:223
void tm_workspace_update_source_file_buffer(TMSourceFile *source_file, guchar *text_buf, gsize buf_size)
Definition: tm_workspace.c:210
void ui_update_statusbar(GeanyDocument *doc, gint pos)
Definition: ui_utils.c:324
void ui_update_tab_status(GeanyDocument *doc)
Definition: ui_utils.c:1745
void ui_save_buttons_toggle(gboolean enable)
Definition: ui_utils.c:830
void ui_set_window_title(GeanyDocument *doc)
Definition: ui_utils.c:366
GeanyMainWidgets main_widgets
Definition: ui_utils.c:72
void ui_set_statusbar(gboolean log, const gchar *format,...)
Displays text on the statusbar.
Definition: ui_utils.c:168
void ui_document_show_hide(GeanyDocument *doc)
Definition: ui_utils.c:1029
UIPrefs ui_prefs
Definition: ui_utils.c:74
void ui_add_recent_document(GeanyDocument *doc)
Definition: ui_utils.c:1232
void ui_document_buttons_update(void)
Definition: ui_utils.c:946
GtkWidget * ui_lookup_widget(GtkWidget *widget, const gchar *widget_name)
Returns a widget from a name in a component, usually created by Glade.
Definition: ui_utils.c:2743
void ui_update_popup_reundo_items(GeanyDocument *doc)
Definition: ui_utils.c:453
GeanyInterfacePrefs interface_prefs
Definition: ui_utils.c:71
User Interface general utility functions.
gchar * utils_get_path_from_uri(const gchar *uri)
Definition: utils.c:1704
gchar * utils_str_middle_truncate(const gchar *string, guint truncate_length)
Truncates the input string to a given length.
Definition: utils.c:549
gchar * utils_get_utf8_from_locale(const gchar *locale_text)
Converts the given string (in locale encoding) into UTF-8 encoding.
Definition: utils.c:1272
void utils_tidy_path(gchar *filename)
Definition: utils.c:1774
void utils_beep(void)
Definition: utils.c:918
void utils_ensure_same_eol_characters(GString *string, gint target_eol_mode)
Definition: utils.c:405
gint utils_get_line_endings(const gchar *buffer, gsize size)
Definition: utils.c:102
const gchar * utils_get_eol_char(gint eol_mode)
Definition: utils.c:393
gboolean utils_str_equal(const gchar *a, const gchar *b)
NULL-safe string comparison.
Definition: utils.c:599
gboolean utils_is_remote_path(const gchar *path)
Definition: utils.c:1741
gchar * utils_get_real_path(const gchar *file_name)
Get a link-dereferenced, absolute version of a file name.
Definition: utils.c:2406
gchar * utils_get_locale_from_utf8(const gchar *utf8_text)
Converts the given UTF-8 encoded string into locale encoding.
Definition: utils.c:1243
General utility functions, non-GTK related.
#define SETPTR(ptr, result)
Assigns result to ptr, then frees the old value.
Definition: utils.h:50
#define utils_strdupa(str)
Duplicates a string on the stack using g_alloca().
Definition: utils.h:72
#define utils_filenamecmp(a, b)
Definition: utils.h:86