~ubuntu-branches/ubuntu/wily/coq-doc/wily

« back to all changes in this revision

Viewing changes to ide/undo.ml

  • Committer: Bazaar Package Importer
  • Author(s): Stéphane Glondu, Stéphane Glondu, Samuel Mimram
  • Date: 2010-01-07 22:50:39 UTC
  • mfrom: (1.2.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20100107225039-n3cq82589u0qt0s2
Tags: 8.2pl1-1
[ Stéphane Glondu ]
* New upstream release (Closes: #563669)
  - remove patches
* Packaging overhaul:
  - use git, advertise it in Vcs-* fields of debian/control
  - use debhelper 7 and dh with override
  - use source format 3.0 (quilt)
* debian/control:
  - set Maintainer to d-o-m, set Uploaders to Sam and myself
  - add Homepage field
  - bump Standards-Version to 3.8.3
* Register PDF documentation into doc-base
* Add debian/watch
* Update debian/copyright

[ Samuel Mimram ]
* Change coq-doc's description to mention that it provides documentation in
  pdf format, not postscript, closes: #543545.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
(************************************************************************)
 
2
(*  v      *   The Coq Proof Assistant  /  The Coq Development Team     *)
 
3
(* <O___,, * CNRS-Ecole Polytechnique-INRIA Futurs-Universite Paris Sud *)
 
4
(*   \VV/  **************************************************************)
 
5
(*    //   *      This file is distributed under the terms of the       *)
 
6
(*         *       GNU Lesser General Public License Version 2.1        *)
 
7
(************************************************************************)
 
8
 
 
9
(* $Id: undo.ml 7603 2005-11-23 17:21:53Z barras $ *)
 
10
 
 
11
open GText
 
12
open Ideutils
 
13
type action = 
 
14
  | Insert of string * int * int (* content*pos*length *) 
 
15
  | Delete of string * int * int (* content*pos*length *) 
 
16
 
 
17
let neg act = match act with
 
18
  | Insert (s,i,l) -> Delete (s,i,l)
 
19
  | Delete (s,i,l) -> Insert (s,i,l)
 
20
 
 
21
class undoable_view (tv:[>Gtk.text_view] Gtk.obj) =
 
22
  let undo_lock = ref true in 
 
23
object(self)
 
24
  inherit GText.view tv as super
 
25
  val history = (Stack.create () : action Stack.t)
 
26
  val redo = (Queue.create () : action Queue.t)
 
27
  val nredo = (Stack.create () : action Stack.t)
 
28
 
 
29
  method private dump_debug =
 
30
    if false (* !debug *) then begin
 
31
      prerr_endline "==========Stack top=============";
 
32
      Stack.iter 
 
33
        (fun e -> match e with
 
34
           | Insert(s,p,l) ->
 
35
               Printf.eprintf "Insert of '%s' at %d (length %d)\n" s p l
 
36
           | Delete(s,p,l) ->                      
 
37
               Printf.eprintf "Delete '%s' from %d (length %d)\n" s p l)
 
38
        history;
 
39
      Printf.eprintf "Stack size %d\n" (Stack.length history);
 
40
      prerr_endline "==========Stack Bottom==========";
 
41
      prerr_endline "==========Queue start=============";
 
42
      Queue.iter 
 
43
        (fun e -> match e with
 
44
           | Insert(s,p,l) ->
 
45
               Printf.eprintf "Insert of '%s' at %d (length %d)\n" s p l
 
46
           | Delete(s,p,l) ->                      
 
47
               Printf.eprintf "Delete '%s' from %d (length %d)\n" s p l)
 
48
        redo;
 
49
      Printf.eprintf "Stack size %d\n" (Queue.length redo);
 
50
      prerr_endline "==========Queue End==========" 
 
51
 
 
52
    end
 
53
 
 
54
  method clear_undo = Stack.clear history; Stack.clear nredo; Queue.clear redo
 
55
 
 
56
  method undo = if !undo_lock then begin
 
57
    undo_lock := false;
 
58
    prerr_endline "UNDO";
 
59
    try begin
 
60
      let r = 
 
61
        match Stack.pop history with
 
62
          | Insert(s,p,l) as act -> 
 
63
              let start = self#buffer#get_iter_at_char p in
 
64
              (self#buffer#delete_interactive 
 
65
                 ~start
 
66
                 ~stop:(start#forward_chars l)
 
67
                 ()) or
 
68
              (Stack.push act history; false)
 
69
          | Delete(s,p,l) as act -> 
 
70
              let iter = self#buffer#get_iter_at_char p in
 
71
              (self#buffer#insert_interactive ~iter s) or
 
72
              (Stack.push act history; false)
 
73
      in if r then begin
 
74
        let act = Stack.pop history in
 
75
        Queue.push act redo;
 
76
        Stack.push act nredo
 
77
      end;
 
78
      undo_lock := true; 
 
79
      r
 
80
    end
 
81
    with Stack.Empty -> 
 
82
      undo_lock := true; 
 
83
      false
 
84
  end else
 
85
    (prerr_endline "UNDO DISCARDED"; true)
 
86
 
 
87
  method redo = prerr_endline "REDO"; true
 
88
  initializer
 
89
(* INCORRECT: is called even while undoing...
 
90
   ignore (self#buffer#connect#mark_set
 
91
              ~callback:
 
92
              (fun it tm -> if !undo_lock && not (Queue.is_empty redo) then begin
 
93
                 Stack.iter (fun e -> Stack.push (neg e) history) nredo;
 
94
                 Stack.clear nredo;
 
95
                 Queue.iter (fun e -> Stack.push e history) redo;
 
96
                 Queue.clear redo;
 
97
               end)
 
98
           );
 
99
*)
 
100
    ignore (self#buffer#connect#insert_text 
 
101
              ~callback:
 
102
              (fun it s ->
 
103
                 if !undo_lock && not (Queue.is_empty redo) then begin
 
104
                   Stack.iter (fun e -> Stack.push (neg e) history) nredo;
 
105
                   Stack.clear nredo;
 
106
                   Queue.iter (fun e -> Stack.push e history) redo;
 
107
                   Queue.clear redo;
 
108
                 end;
 
109
(*               let pos = it#offset in
 
110
                 if Stack.is_empty history or 
 
111
                   s=" " or s="\t" or s="\n" or
 
112
                                        (match Stack.top history with 
 
113
                                           | Insert(old,opos,olen) -> 
 
114
                                               opos + olen <> pos
 
115
                                           | _ -> true)
 
116
                 then *)
 
117
                 Stack.push (Insert(s,it#offset,Glib.Utf8.length s)) history
 
118
                 (*else begin
 
119
                   match Stack.pop history with
 
120
                     | Insert(olds,offset,len) -> 
 
121
                         Stack.push 
 
122
                         (Insert(olds^s,
 
123
                                 offset,
 
124
                                 len+(Glib.Utf8.length s)))
 
125
                         history
 
126
                     | _ -> assert false
 
127
                   end*);
 
128
                 self#dump_debug
 
129
              ));
 
130
    ignore (self#buffer#connect#delete_range
 
131
              ~callback:
 
132
              (fun ~start ~stop -> 
 
133
                 if !undo_lock && not (Queue.is_empty redo) then begin
 
134
                   Queue.iter (fun e -> Stack.push e history) redo;
 
135
                   Queue.clear redo;
 
136
                 end;
 
137
                 let start_offset = start#offset in
 
138
                 let stop_offset = stop#offset in
 
139
                 let s = self#buffer#get_text ~start ~stop () in
 
140
(*               if Stack.is_empty history or (match Stack.top history with
 
141
                                                 | Delete(old,opos,olen) -> 
 
142
                                                     olen=1 or opos <> start_offset
 
143
                                                 | _ -> true
 
144
                                              )
 
145
                 then
 
146
*)                 Stack.push 
 
147
                     (Delete(s,
 
148
                             start_offset,
 
149
                             stop_offset - start_offset
 
150
                            ))
 
151
                     history
 
152
        (*       else begin
 
153
                   match Stack.pop history with
 
154
                     | Delete(olds,offset,len) -> 
 
155
                         Stack.push 
 
156
                         (Delete(olds^s,
 
157
                                 offset,
 
158
                                 len+(Glib.Utf8.length s)))
 
159
                         history
 
160
                     | _ -> assert false
 
161
                         
 
162
                 end*);
 
163
                 self#dump_debug
 
164
              ))
 
165
end
 
166
 
 
167
let undoable_view ?(buffer:GText.buffer option) =
 
168
  GtkText.View.make_params [] 
 
169
    ~cont:(GContainer.pack_container 
 
170
             ~create:
 
171
             (fun pl -> let w = match buffer with 
 
172
              | None -> GtkText.View.create []
 
173
              | Some b -> GtkText.View.create_with_buffer b#as_buffer
 
174
              in
 
175
              Gobject.set_params w pl; ((new undoable_view w):undoable_view)))
 
176
    
 
177