-
Notifications
You must be signed in to change notification settings - Fork 611
/
Copy pathsaveactions.c
1651 lines (1287 loc) · 51.2 KB
/
saveactions.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* saveactions.c - this file is part of Geany, a fast and lightweight IDE
*
* Copyright 2007 The Geany contributors
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif
#include "geanyplugin.h"
#include "gtkcompat.h"
#include <stdio.h>
#include <fcntl.h>
#include <unistd.h>
#include <string.h>
#include <errno.h>
#include <glib/gstdio.h>
GeanyPlugin *geany_plugin;
GeanyData *geany_data;
PLUGIN_VERSION_CHECK(GEANY_API_VERSION)
PLUGIN_SET_INFO(_("Save Actions"), _("This plugin provides different actions related to saving of files."),
PACKAGE_VERSION, _("The Geany developer team"))
enum
{
NOTEBOOK_PAGE_AUTOSAVE = 0,
NOTEBOOK_PAGE_BACKUPCOPY,
NOTEBOOK_PAGE_UNTITLEDDOC
};
enum
{
NOTEBOOK_UNTITLEDDOC_RADIO_DISABLED = 0,
NOTEBOOK_UNTITLEDDOC_RADIO_INSTANTSAVE,
NOTEBOOK_UNTITLEDDOC_RADIO_PERSISTENT
};
static struct
{
GtkWidget *checkbox_enable_autosave;
GtkWidget *checkbox_enable_autosave_losing_focus;
GtkWidget *checkbox_enable_backupcopy;
GtkWidget *autosave_interval_spin;
GtkWidget *autosave_print_msg_checkbox;
GtkWidget *autosave_save_all_radio1;
GtkWidget *autosave_save_all_radio2;
GtkWidget *backupcopy_entry_dir;
GtkWidget *backupcopy_entry_time;
GtkWidget *backupcopy_spin_dir_levels;
GtkWidget *untitled_doc_disabled_radio;
GtkWidget *untitled_doc_instantsave_radio;
GtkWidget *untitled_doc_persistent_radio;
GtkWidget *untitled_doc_ft_combo;
GtkWidget *instantsave_entry_dir;
GtkWidget *persistent_doc_interval_spin;
GtkWidget *persistent_doc_entry_dir;
}
pref_widgets;
#define PERSISTENT_UNTITLED_DOC_FILE_NAME_PREFIX "untitled_"
static gboolean enable_autosave;
static gboolean enable_autosave_losing_focus;
static gboolean enable_instantsave;
static gboolean enable_backupcopy;
static gboolean enable_persistent_docs;
static gint autosave_interval;
static gboolean autosave_print_msg;
static gboolean autosave_save_all;
static guint autosave_src_id = 0;
static gchar *backupcopy_backup_dir; /* path to an existing directory in locale encoding */
static gchar *backupcopy_time_fmt;
static gint backupcopy_dir_levels;
static gchar *untitled_doc_default_ft;
static gchar *instantsave_target_dir;
static gint persistent_docs_updater_interval_ms;
static guint persistent_docs_updater_src_id = 0;
static gchar *persistent_docs_target_dir;
static gchar *config_file;
/* all persistent untitled documents should be loaded into session when session is changed
(so after geany launch or when project session is started/closed) */
static gboolean session_is_changing = FALSE;
static gint count_opened_notebook_tabs(void)
{
return gtk_notebook_get_n_pages(GTK_NOTEBOOK(geany->main_widgets->notebook));
}
static gboolean is_directory_accessible(const gchar *dirpath_locale)
{
return dirpath_locale != NULL &&
g_path_is_absolute(dirpath_locale) &&
g_file_test(dirpath_locale, G_FILE_TEST_EXISTS) &&
g_file_test(dirpath_locale, G_FILE_TEST_IS_DIR);
}
/* Ensures utf8_dir exists and is writable and
* set target to the locale encoded form of utf8_dir */
static gboolean store_target_directory(const gchar *utf8_dir, gchar **target)
{
gchar *tmp;
if (G_UNLIKELY(EMPTY(utf8_dir)) || target == NULL)
return FALSE;
tmp = utils_get_locale_from_utf8(utf8_dir);
if (! is_directory_accessible(tmp))
{
g_free(tmp);
return FALSE;
}
/** TODO add utils_is_file_writeable() to the plugin API and make use of it **/
SETPTR(*target, tmp);
return TRUE;
}
static gchar *backupcopy_skip_root(gchar *filename)
{
/* first skip the root (e.g. c:\ on windows) */
const gchar *dir = g_path_skip_root(filename);
/* if this has failed, use the filename again */
if (dir == NULL)
dir = filename;
/* check again for leading / or \ */
while (*dir == G_DIR_SEPARATOR)
dir++;
return (gchar *) dir;
}
static gchar *backupcopy_create_dir_parts(const gchar *filename)
{
gint cnt_dir_parts = 0;
gchar *cp;
gchar *dirname;
gchar last_char = 0;
gint error;
gchar *result;
gchar *target_dir;
if (backupcopy_dir_levels == 0)
return g_strdup("");
dirname = g_path_get_dirname(filename);
cp = dirname;
/* walk to the end of the string */
while (*cp != '\0')
cp++;
/* walk backwards to find directory parts */
while (cp > dirname)
{
if (*cp == G_DIR_SEPARATOR && last_char != G_DIR_SEPARATOR)
cnt_dir_parts++;
if (cnt_dir_parts == backupcopy_dir_levels)
break;
last_char = *cp;
cp--;
}
result = backupcopy_skip_root(cp); /* skip leading slash/backslash and c:\ */
target_dir = g_build_filename(backupcopy_backup_dir, result, NULL);
error = utils_mkdir(target_dir, TRUE);
if (error != 0)
{
ui_set_statusbar(FALSE, _("Backup Copy: Directory could not be created (%s)."),
g_strerror(error));
result = g_strdup(""); /* return an empty string in case of an error */
}
else
result = g_strdup(result);
g_free(dirname);
g_free(target_dir);
return result;
}
static void backupcopy_document_save_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
{
FILE *src, *dst;
gchar *locale_filename_src;
gchar *locale_filename_dst;
gchar *basename_src;
gchar *dir_parts_src;
gchar *stamp;
gchar buf[512];
gint fd_dst = -1;
locale_filename_src = utils_get_locale_from_utf8(doc->file_name);
if ((src = g_fopen(locale_filename_src, "r")) == NULL)
{
/* it's unlikely that this happens */
ui_set_statusbar(FALSE, _("Backup Copy: File could not be read (%s)."),
g_strerror(errno));
g_free(locale_filename_src);
return;
}
stamp = utils_get_date_time(backupcopy_time_fmt, NULL);
basename_src = g_path_get_basename(locale_filename_src);
dir_parts_src = backupcopy_create_dir_parts(locale_filename_src);
locale_filename_dst = g_strconcat(
backupcopy_backup_dir, G_DIR_SEPARATOR_S,
dir_parts_src, G_DIR_SEPARATOR_S,
basename_src, ".", stamp, NULL);
g_free(basename_src);
g_free(dir_parts_src);
#ifdef G_OS_WIN32
if ((dst = g_fopen(locale_filename_dst, "wb")) == NULL)
#else
/* Use g_open() on non-Windows to set file permissions to 600 atomically.
* On Windows, seting file permissions would require specific Windows API. */
fd_dst = g_open(locale_filename_dst, O_CREAT | O_WRONLY, S_IWUSR | S_IRUSR);
if (fd_dst == -1 || (dst = fdopen(fd_dst, "w")) == NULL)
#endif
{
ui_set_statusbar(FALSE, _("Backup Copy: File could not be saved (%s)."),
g_strerror(errno));
g_free(locale_filename_src);
g_free(locale_filename_dst);
g_free(stamp);
fclose(src);
if (fd_dst != -1)
close(fd_dst);
return;
}
while (fgets(buf, sizeof(buf), src) != NULL)
{
fputs(buf, dst);
}
fclose(src);
fclose(dst);
if (fd_dst != -1)
close(fd_dst);
g_free(locale_filename_src);
g_free(locale_filename_dst);
g_free(stamp);
}
static GeanyFiletype *get_doc_filetype(GeanyDocument *doc)
{
GeanyFiletype *ft = doc->file_type;
if (ft == NULL || ft->id == GEANY_FILETYPES_NONE)
/* ft is NULL when a new file without template was opened, so use the
* configured default file type */
ft = filetypes_lookup_by_name(untitled_doc_default_ft);
return ft;
}
static void instantsave_document_new(GeanyDocument *doc)
{
if (doc->real_path == NULL && doc->file_name == NULL)
{
const gchar *directory;
gchar *new_filename;
gint fd;
GeanyFiletype *ft = get_doc_filetype(doc);
/* construct filename */
directory = !EMPTY(instantsave_target_dir) ? instantsave_target_dir : g_get_tmp_dir();
new_filename = g_build_filename(directory, "gis_XXXXXX", NULL);
if (ft != NULL && !EMPTY(ft->extension))
SETPTR(new_filename, g_strconcat(new_filename, ".", ft->extension, NULL));
/* create new file */
fd = g_mkstemp(new_filename);
if (fd == -1)
{
gchar *message = g_strdup_printf(
_("Instant Save filename could not be generated (%s)."), g_strerror(errno));
ui_set_statusbar(TRUE, "%s", message);
g_warning("%s", message);
g_free(message);
g_free(new_filename);
return;
}
close(fd); /* close the returned file descriptor as we only need the filename */
doc->file_name = new_filename;
if (ft != NULL)
document_set_filetype(doc, ft);
/* force saving the file to enable all the related actions(tab name, filetype, etc.) */
document_save_file(doc, TRUE);
}
}
static gboolean is_persistent_doc_file_name(const gchar *filename)
{
if (filename == NULL)
return FALSE;
return g_str_has_prefix(filename, PERSISTENT_UNTITLED_DOC_FILE_NAME_PREFIX);
}
static gboolean is_persistent_doc_file_path(const gchar *file_path_utf8)
{
gchar *filename, *dirname, *file_path_locale;
gboolean matched;
if (file_path_utf8 == NULL)
return FALSE;
file_path_locale = utils_get_locale_from_utf8(file_path_utf8);
dirname = g_path_get_dirname(file_path_locale);
matched = g_str_equal(dirname, persistent_docs_target_dir);
g_free(file_path_locale);
g_free(dirname);
if (!matched)
return FALSE;
filename = g_path_get_basename(file_path_utf8);
matched = is_persistent_doc_file_name(filename);
g_free(filename);
return matched;
}
static gchar* create_new_persistent_doc_file_name(GeanyDocument *doc, GeanyFiletype *filetype)
{
gint i;
gchar *extension_postfix;
if (filetype != NULL && !EMPTY(filetype->extension))
extension_postfix = g_strconcat(".", filetype->extension, NULL);
else
extension_postfix = g_strdup("");
for (i = 1; i < 1000; i++)
{
gchar *next_file_name, *next_file_path;
gboolean file_exists;
next_file_name = g_strdup_printf("%s%d%s", PERSISTENT_UNTITLED_DOC_FILE_NAME_PREFIX, i, extension_postfix);
next_file_path = g_strdup_printf("%s%c%s", persistent_docs_target_dir, G_DIR_SEPARATOR, next_file_name);
file_exists = g_file_test(next_file_path, G_FILE_TEST_EXISTS);
g_free(next_file_path);
if (file_exists)
{
g_free(next_file_name);
}
else
{
g_free(extension_postfix);
return next_file_name;
}
}
g_free(extension_postfix);
return NULL;
}
static void persistent_doc_new(GeanyDocument *doc)
{
if (doc->real_path == NULL && doc->file_name == NULL)
{
gchar *files_dir_utf8, *new_file_name_utf8, *new_file_path_utf8;
GeanyFiletype *ft;
if (EMPTY(persistent_docs_target_dir))
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
_("Persistent untitled document directory does not exist or is not writable."));
return;
}
ft = get_doc_filetype(doc);
new_file_name_utf8 = create_new_persistent_doc_file_name(doc, ft);
if (ft != NULL)
document_set_filetype(doc, ft);
if (new_file_name_utf8 == NULL)
{
return;
}
files_dir_utf8 = utils_get_utf8_from_locale(persistent_docs_target_dir);
new_file_path_utf8 = g_strconcat(files_dir_utf8,
G_DIR_SEPARATOR_S, new_file_name_utf8, NULL);
document_save_file_as(doc, new_file_path_utf8);
g_free(new_file_name_utf8);
g_free(files_dir_utf8);
g_free(new_file_path_utf8);
}
}
static gboolean document_is_empty(GeanyDocument *doc)
{
/* if total text length is 1 while line count is 2 - then the only character of whole text is a linebreak,
which is how completely empty document saved by Geany to disk looks like */
return sci_get_length(doc->editor->sci) == 0
|| (sci_get_length(doc->editor->sci) == 1 && sci_get_line_count(doc->editor->sci) == 2);
}
/* Open document in idle - useful when trying to re-open document from 'document-close' callback
*
* @param pointer to document path (locale-encoded)
*
* @return always FALSE = Just a one shot execution
*
*/
static gboolean open_document_once_idle(gpointer p_locale_file_path)
{
gchar *locale_file_path = (gchar *) p_locale_file_path;
document_open_file(locale_file_path, FALSE, NULL, NULL);
g_free(locale_file_path);
return FALSE;
}
static gint run_dialog_for_persistent_doc_tab_closing(const gchar *msg, const gchar *msg2)
{
GtkWidget *dialog, *button;
gint ret;
dialog = gtk_message_dialog_new(GTK_WINDOW(geany->main_widgets->window), GTK_DIALOG_DESTROY_WITH_PARENT,
GTK_MESSAGE_QUESTION, GTK_BUTTONS_NONE, "%s", msg);
gtk_window_set_title(GTK_WINDOW(dialog), _("Question"));
gtk_message_dialog_format_secondary_text(GTK_MESSAGE_DIALOG(dialog), "%s", msg2);
gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL);
button = ui_button_new_with_image(GTK_STOCK_CLEAR, _("_Don't save (discard)"));
gtk_dialog_add_action_widget(GTK_DIALOG(dialog), button, GTK_RESPONSE_NO);
gtk_widget_show(button);
gtk_dialog_add_button(GTK_DIALOG(dialog), GTK_STOCK_SAVE, GTK_RESPONSE_YES);
gtk_dialog_set_default_response(GTK_DIALOG(dialog), GTK_RESPONSE_YES);
ret = gtk_dialog_run(GTK_DIALOG(dialog));
gtk_widget_destroy(dialog);
return ret;
}
static void show_dialog_for_persistent_doc_tab_closing(
GeanyDocument *doc, const gchar *short_filename)
{
gchar *msg, *old_file_path_locale;
const gchar *msg2;
gint response;
msg = g_strdup_printf(_("Untitled document %s is not saved."), short_filename);
msg2 = _("Do you want to save it?");
response = run_dialog_for_persistent_doc_tab_closing(msg, msg2);
g_free(msg);
switch (response)
{
case GTK_RESPONSE_YES:
old_file_path_locale = g_strdup(doc->real_path);
if (dialogs_show_save_as())
{
if (! g_str_equal(old_file_path_locale, doc->real_path))
{
/* remove untitled doc file if it was saved as some other file */
g_remove(old_file_path_locale);
}
}
else
{
plugin_idle_add(geany_plugin, open_document_once_idle, g_strdup(old_file_path_locale));
}
g_free(old_file_path_locale);
break;
case GTK_RESPONSE_NO:
g_remove(doc->real_path);
ui_set_statusbar(TRUE, _("Untitled document file %s was deleted"), short_filename);
break;
case GTK_RESPONSE_CANCEL:
default:
plugin_idle_add(geany_plugin, open_document_once_idle, g_strdup(doc->real_path));
break;
}
}
static void persistent_doc_before_save_as_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
{
if (enable_persistent_docs)
{
gchar *old_file_path_utf8 = DOC_FILENAME(doc);
if (is_persistent_doc_file_path(old_file_path_utf8))
{
/* we have to store old filename inside document data to be able to somehow
pass it to document-save callback that is called directly after this one */
plugin_set_document_data_full(geany_plugin, doc, "file-name-before-save-as",
g_strdup(old_file_path_utf8), g_free);
}
}
}
static void persistent_doc_save_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
{
gchar *new_file_path_utf8, *old_file_path_utf8;
new_file_path_utf8 = DOC_FILENAME(doc);
old_file_path_utf8 = plugin_get_document_data(geany_plugin, doc, "file-name-before-save-as");
if (old_file_path_utf8 != NULL)
{
if (is_persistent_doc_file_path(old_file_path_utf8)
&& ! g_str_equal(old_file_path_utf8, new_file_path_utf8))
{
/* remove untitled doc file if it was saved as some other file */
gchar *locale_old_file_path = utils_get_locale_from_utf8(old_file_path_utf8);
g_remove(locale_old_file_path);
g_free(locale_old_file_path);
msgwin_status_add(_("Untitled document file %s was deleted"), old_file_path_utf8);
}
plugin_set_document_data(geany_plugin, doc, "file-name-before-save-as", NULL); /* clear value */
}
}
static void load_all_persistent_docs_into_editor(void)
{
GDir *dir;
GError *error = NULL;
const gchar *filename;
dir = g_dir_open(persistent_docs_target_dir, 0, &error);
if (dir == NULL)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR, _("Persistent untitled document directory not found"));
return;
}
foreach_dir(filename, dir)
{
if (is_persistent_doc_file_name(filename))
{
gchar *locale_file_path, *file_path_utf8;
GeanyDocument *doc;
locale_file_path = g_build_path(G_DIR_SEPARATOR_S, persistent_docs_target_dir, filename, NULL);
file_path_utf8 = utils_get_utf8_from_locale(locale_file_path);
doc = document_find_by_filename(file_path_utf8);
g_free(file_path_utf8);
if (doc == NULL)
doc = document_open_file(locale_file_path, FALSE, NULL, NULL);
g_free(locale_file_path);
/* we are closing (and thus deleting) empty documents here - mainly in order to avoid accumulation of
empty untitled document files, that happens when new tab with empty document is created at new (empty) session start.
Note: we cannot 'close' newly-created empty document from 'document-activate' callback,
so this is a perfect place for it */
if (doc != NULL && document_is_empty(doc))
document_close(doc);
}
}
g_dir_close(dir);
/* create new empty file/tab if this is a "fresh" session start without any opened files */
if (count_opened_notebook_tabs() == 0)
document_new_file(NULL, NULL, NULL);
}
static gboolean load_all_persistent_docs_idle(gpointer data)
{
load_all_persistent_docs_into_editor();
return FALSE;
}
static gboolean reload_persistent_docs_on_session_change_idle(gpointer data)
{
/* remember and re-open document from originaly focused tab
(after we mess selected tab with re-loaded untitled doc files) */
GeanyDocument *current_doc = document_get_current();
load_all_persistent_docs_into_editor();
if (current_doc != NULL && current_doc->real_path != NULL)
document_open_file(current_doc->real_path, FALSE, NULL, NULL);
return FALSE;
}
/* Save when focus out
*
* @param pointer ref to the current doc (struct GeanyDocument *)
*
* @return always FALSE = Just a one shot execution
*
*/
static gboolean save_on_focus_out_idle(gpointer p_cur_doc)
{
GeanyDocument *cur_doc = p_cur_doc;
if (DOC_VALID(cur_doc) && (cur_doc->file_name != NULL))
document_save_file(cur_doc, FALSE);
return FALSE;
}
/* Autosave the current file when the focus out of the _editor_
*
* Get the SCN_FOCUSOUT signal, and then ask plugin_idle_add()
* to save the current doc when idle
*
* @return always FALSE = Non block signals
*
*/
static gboolean on_editor_notify(GObject *object, GeanyEditor *editor,
SCNotification *nt, gpointer data)
{
GeanyDocument *doc = editor->document;
if (nt->nmhdr.code == SCN_FOCUSOUT
&& doc->file_name != NULL)
{
if (enable_autosave_losing_focus || (enable_persistent_docs
&& doc->real_path != NULL
&& is_persistent_doc_file_path(doc->file_name)))
{
plugin_idle_add(geany_plugin, save_on_focus_out_idle, doc);
}
}
else if (nt->nmhdr.code == SCN_MODIFIED &&
(nt->modificationType & (SC_MOD_INSERTTEXT | SC_MOD_DELETETEXT)))
{
/* Untitled non-empty documents modified for the first time - empty
* ones are handled by on_document_new() */
if (!doc->real_path && doc->changed)
{
if (enable_instantsave)
instantsave_document_new(doc);
if (enable_persistent_docs)
persistent_doc_new(doc);
}
}
return FALSE;
}
static void on_document_save(GObject *obj, GeanyDocument *doc, gpointer user_data)
{
if (enable_backupcopy)
backupcopy_document_save_cb(obj, doc, user_data);
if (enable_persistent_docs)
persistent_doc_save_cb(obj, doc, user_data);
}
static void on_document_new(GObject *obj, GeanyDocument *doc, gpointer user_data)
{
/* We are only interested in empty new documents here. Other documents
* such as templates are handled in on_editor_notify(). */
if (!document_is_empty(doc))
return;
if (enable_instantsave)
instantsave_document_new(doc);
if (enable_persistent_docs)
persistent_doc_new(doc);
}
static void on_project_open(G_GNUC_UNUSED GObject *obj, G_GNUC_UNUSED GKeyFile *config,
G_GNUC_UNUSED gpointer data)
{
session_is_changing = TRUE;
}
static void on_project_before_close(void)
{
session_is_changing = TRUE;
}
static void on_document_activate(G_GNUC_UNUSED GObject *obj, GeanyDocument *doc,
G_GNUC_UNUSED gpointer data)
{
if (session_is_changing)
{
session_is_changing = FALSE;
if (enable_persistent_docs)
plugin_idle_add(geany_plugin, reload_persistent_docs_on_session_change_idle, NULL);
}
}
static void persistent_doc_close_cb(GObject *obj, GeanyDocument *doc, gpointer user_data)
{
if (! enable_persistent_docs)
{
return;
}
if (! geany_is_closing_all_documents() && doc->real_path != NULL && is_persistent_doc_file_path(doc->file_name))
{
gchar *short_filename = document_get_basename_for_display(doc, -1);
if (! document_is_empty(doc))
{
show_dialog_for_persistent_doc_tab_closing(
doc,
short_filename
);
}
else
{
g_remove(doc->real_path);
msgwin_status_add(_("Empty untitled document file %s was deleted"), short_filename);
}
g_free(short_filename);
}
else if (geany_is_closing_all_documents() && count_opened_notebook_tabs() == 1)
{
/* if this is a last file being closed during 'close all' - reopen all untitled doc files to keep them in editor */
plugin_idle_add(geany_plugin, load_all_persistent_docs_idle, NULL);
}
}
PluginCallback plugin_callbacks[] =
{
{ "document-new", (GCallback) &on_document_new, FALSE, NULL },
{ "document-close", (GCallback) &persistent_doc_close_cb, FALSE, NULL },
{ "document-before-save-as", (GCallback) &persistent_doc_before_save_as_cb, FALSE, NULL },
{ "document-save", (GCallback) &on_document_save, FALSE, NULL },
{ "document-activate", (GCallback) &on_document_activate, FALSE, NULL },
{ "editor-notify", (GCallback) &on_editor_notify, FALSE, NULL },
{ "project-open", (GCallback) &on_project_open, FALSE, NULL },
{ "project-before-close", (GCallback) &on_project_before_close, FALSE, NULL },
{ NULL, NULL, FALSE, NULL }
};
static gboolean auto_save(gpointer data)
{
GeanyDocument *doc;
GeanyDocument *cur_doc = document_get_current();
gint i, max = count_opened_notebook_tabs();
gint saved_files = 0;
if (cur_doc == NULL)
return TRUE;
if (autosave_save_all)
{
for (i = 0; i < max; i++)
{
doc = document_get_from_page(i);
/* skip current file (save it last), skip files without name */
if (doc != cur_doc && doc->file_name != NULL)
if (document_save_file(doc, FALSE))
saved_files++;
}
}
/* finally save current file, do it after all other files to get correct window title and
* symbol list */
if (cur_doc->file_name != NULL)
if (document_save_file(cur_doc, FALSE))
saved_files++;
if (saved_files > 0 && autosave_print_msg)
ui_set_statusbar(FALSE, ngettext(
"Autosave: Saved %d file automatically.",
"Autosave: Saved %d files automatically.", saved_files),
saved_files);
return TRUE;
}
static void autosave_set_timeout(void)
{
if (autosave_src_id != 0)
g_source_remove(autosave_src_id);
if (! enable_autosave)
return;
autosave_src_id = g_timeout_add(autosave_interval * 1000, (GSourceFunc) auto_save, NULL);
}
static gboolean persistent_doc_files_update(gpointer data)
{
gint i, max = count_opened_notebook_tabs();
for (i = 0; i < max; i++)
{
GeanyDocument *doc = document_get_from_page(i);
if (doc->real_path != NULL && is_persistent_doc_file_path(doc->file_name))
{
document_save_file(doc, FALSE);
}
}
return TRUE;
}
static void persistent_doc_files_updater_set_timeout(void)
{
if (persistent_docs_updater_src_id != 0)
g_source_remove(persistent_docs_updater_src_id);
if (! enable_persistent_docs)
return;
persistent_docs_updater_src_id = g_timeout_add(
persistent_docs_updater_interval_ms,
(GSourceFunc) persistent_doc_files_update,
NULL
);
}
static void write_config_file_updates(GKeyFile *config)
{
gchar *str, *config_dir;
config_dir = g_path_get_dirname(config_file);
if (! g_file_test(config_dir, G_FILE_TEST_IS_DIR) && utils_mkdir(config_dir, TRUE) != 0)
{
dialogs_show_msgbox(GTK_MESSAGE_ERROR,
_("Plugin configuration directory could not be created."));
}
else
{
str = g_key_file_to_data(config, NULL, NULL);
utils_write_file(config_file, str);
g_free(str);
}
g_free(config_dir);
}
void plugin_init(GeanyData *data)
{
GKeyFile *config = g_key_file_new();
gchar *tmp;
config_file = g_strconcat(geany->app->configdir, G_DIR_SEPARATOR_S, "plugins",
G_DIR_SEPARATOR_S, "saveactions", G_DIR_SEPARATOR_S, "saveactions.conf", NULL);
g_key_file_load_from_file(config, config_file, G_KEY_FILE_NONE, NULL);
enable_autosave = utils_get_setting_boolean(
config, "saveactions", "enable_autosave", FALSE);
enable_autosave_losing_focus = utils_get_setting_boolean(
config, "saveactions", "enable_autosave_losing_focus", FALSE);
enable_backupcopy = utils_get_setting_boolean(
config, "saveactions", "enable_backupcopy", FALSE);
enable_instantsave = utils_get_setting_boolean(
config, "saveactions", "enable_instantsave", FALSE);
enable_persistent_docs = utils_get_setting_boolean(
config, "saveactions", "enable_persistent_untitled_documents", FALSE);
if (enable_instantsave && enable_persistent_docs)
{
enable_instantsave = FALSE;
}
autosave_src_id = 0; /* mark as invalid */
autosave_interval = utils_get_setting_integer(config, "autosave", "interval", 300);
autosave_print_msg = utils_get_setting_boolean(config, "autosave", "print_messages", FALSE);
autosave_save_all = utils_get_setting_boolean(config, "autosave", "save_all", FALSE);
autosave_set_timeout();
backupcopy_dir_levels = utils_get_setting_integer(config, "backupcopy", "dir_levels", 0);
backupcopy_time_fmt = utils_get_setting_string(
config, "backupcopy", "time_fmt", "%Y-%m-%d-%H-%M-%S");
tmp = utils_get_setting_string(config, "backupcopy", "backup_dir", g_get_tmp_dir());
store_target_directory(tmp, &backupcopy_backup_dir);
g_free(tmp);
untitled_doc_default_ft = utils_get_setting_string(config, "untitled_document_save", "default_ft",
filetypes[GEANY_FILETYPES_NONE]->name);
tmp = utils_get_setting_string(config, "instantsave", "target_dir", NULL);
store_target_directory(tmp, &instantsave_target_dir);
g_free(tmp);
/* START Persistent untitled documents */
if (utils_get_setting_string(config, "untitled_document_save", "persistent_untitled_documents_target_dir", NULL) == NULL)
{