Debian Codegraph @

ide/rocqide/fileOps.ml

raw symbols SCIP
1 (************************************************************************)
2 (* * The Rocq Prover / The Rocq Development Team *)
3 (* v * Copyright INRIA, CNRS and contributors *)
4 (* <O___,, * (see version control and CREDITS file for authors & dates) *)
5 (* \VV/ **************************************************************)
6 (* // * This file is distributed under the terms of the *)
7 (* * GNU Lesser General Public License Version 2.1 *)
8 (* * (see LICENSE file for the text of the license) *)
9 (************************************************************************)
10
11 open Ideutils
12
13 let reload_timer = mktimer ()
14 let autosave_timer = mktimer ()
15
16 class type ops =
17 object
18 method filename : string option
19 method update_stats : unit
20 method changed_on_disk : bool
21 method reload : ?parent:GWindow.window -> unit -> unit
22 method auto_save : unit
23 method save : string -> bool
24 method saveas : ?parent:GWindow.window -> string -> bool
25 end
26
27 class fileops (buffer:GText.buffer) _fn (reset_handler:unit->unit) =
28 object(self)
29
30 val mutable filename = _fn
31 val mutable last_stats = NoSuchFile
32 val mutable last_modification_time = 0.
33 val mutable last_auto_save_time = 0.
34
35 method filename = filename
36
37 method update_stats = match filename with
38 |Some f -> last_stats <- Ideutils.stat f
39 |_ -> ()
40
41 method changed_on_disk = match filename with
42 |None -> false
43 |Some f -> match Ideutils.stat f, last_stats with
44 |MTime cur_mt, MTime last_mt -> cur_mt > last_mt
45 |MTime _, (NoSuchFile|OtherError) -> true
46 |NoSuchFile, MTime _ ->
47 flash_info ("Warning, file not on disk anymore : "^f);
48 false
49 |_ -> false
50
51 method reload ?parent () =
52 let do_reload f =
53 push_info "Reloading buffer";
54 try
55 reset_handler ();
56 let b = Buffer.create 1024 in
57 Ideutils.read_file f b;
58 let s = try_convert (Buffer.contents b) in
59 buffer#set_text s;
60 self#update_stats;
61 buffer#place_cursor ~where:buffer#start_iter;
62 buffer#set_modified false;
63 pop_info ();
64 flash_info "Buffer reloaded";
65 Sentence.tag_all buffer;
66 with _ ->
67 pop_info ();
68 flash_info "Warning: could not reload buffer";
69 in
70 match filename with
71 | None -> ()
72 | Some f ->
73 if not buffer#modified then do_reload f
74 else
75 let answ = Ui_dialogs.question_box
76 ~title:"Modified buffer changed on disk"
77 ~buttons:[ButtonWithLabel "Reload from file";
78 ButtonWithLabel "Overwrite file";
79 ButtonWithLabel "Disable Auto Reload"]
80 ~default:0
81 ~icon:(stock_to_widget `DIALOG_WARNING)
82 ?parent
83 "Some unsaved buffers changed on disk"
84 in
85 match answ with
86 | 1 -> do_reload f
87 | 2 -> if self#save f then flash_info "Overwritten" else
88 flash_info "Could not overwrite file"
89 | _ ->
90 Minilib.log "Auto reload set to false";
91 Preferences.global_auto_reload#set false;
92 reload_timer.kill ()
93
94 method save f =
95 if try_export f (buffer#get_text ()) then begin
96 filename <- Some f;
97 self#update_stats;
98 buffer#set_modified false;
99 (match self#auto_save_name with
100 | None -> ()
101 | Some fn -> try Sys.remove fn with _ -> ());
102 true
103 end
104 else false
105
106 method saveas ?parent f =
107 if not (Sys.file_exists f) then self#save f
108 else
109 (* The dialog's content may be determined by GTK, but we have to provide placeholders
110 or the dialog may not appear at all *)
111 let answ = Ui_dialogs.question_box ~title:"File exists on disk"
112 ~buttons:[ButtonWithLabel "Overwrite"; ButtonWithStock `CANCEL]
113 ~default:1
114 ~icon:(warn_image ())#coerce
115 ?parent
116 ("File "^f^" already exists")
117 in
118 match answ with
119 | 1 -> self#save f
120 | _ -> false
121
122 method private auto_save_name =
123 match filename with
124 | None -> None
125 | Some f ->
126 let dir = Filename.dirname f in
127 let base = (fst Preferences.auto_save_name#get) ^
128 (Filename.basename f) ^
129 (snd Preferences.auto_save_name#get)
130 in Some (Filename.concat dir base)
131
132 method private need_auto_save =
133 buffer#modified &&
134 last_modification_time > last_auto_save_time
135
136 method auto_save =
137 if self#need_auto_save then begin
138 match self#auto_save_name with
139 | None -> ()
140 | Some fn ->
141 try
142 last_auto_save_time <- Unix.time();
143 Minilib.log ("Autosave time: "^(string_of_float (Unix.time())));
144 if try_export fn (buffer#get_text ()) then begin
145 flash_info ~delay:1000 "Autosaved"
146 end
147 else warning
148 ("Autosave failed (check if " ^ fn ^ " is writable)")
149 with _ ->
150 warning ("Autosave: unexpected error while writing "^fn)
151 end
152
153 initializer
154 let _ = buffer#connect#end_user_action
155 ~callback:(fun () -> last_modification_time <- Unix.time ())
156 in ()
157
158 end