summaryrefslogtreecommitdiff
path: root/support/tcltexed/lib/file.tcl
blob: c683b5c0579e400f101277775abe7a1192a3ed13 (plain)
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
# Copyright (C) 1999 - Martin Strauss - under terms of GPL
proc NewFile {} {
 global FocusEditor
 global FilenameArray UpToDateArray
 global EDITORNUMMER
 global etcpath
 set E [EDITOR]
 $E.te.edit1 delete 1.0 end
 set FilenameArray($E) ""
 set UpToDateArray($E) 0
 wm title $E "no name $EDITORNUMMER"
 if [file exists "$etcpath/default.tex"] then {
  set f [open "$etcpath/default.tex" r]
  set x [read $f]
  $E.te.edit1 insert 1.0 $x
  set t [close $f]
 } else {
  $E.te.edit1 insert end "\\documentclass\[12pt,a4paper\]{report}\n"
#  $E.te.edit1 insert end "\\usepackage\[german\]{babel}\n"
#  $E.te.edit1 insert end "\\usepackage{umlaut}\n"
  $E.te.edit1 insert end "\\begin{document}\n\n"
  $E.te.edit1 insert end "\\end{document}\n"
 }
 Syntax $E.te.edit1 1.0 end
 set ksp [$E.te.edit1 search "\\begin{document}" 1.0]
 $E.te.edit1 mark set insert "$ksp + 1 lines"
 update
 event generate $E.te.edit1 <<Position_tail>> -when tail
# after 250 "event generate $E.te.edit1 <<Position_tail>> -when tail"
}
proc NewFileArg {} {
 global FocusEditor
 global FilenameArray UpToDateArray
 global EDITORNUMMER
 global etcpath
 set E [EDITOR]
 $E.te.edit1 delete 1.0 end
 set FilenameArray($E) ""
 set UpToDateArray($E) 0
 wm title $E "no name $EDITORNUMMER"
 if [file exists "$etcpath/default.tex"] then {
  set f [open "$etcpath/default.tex" r]
  set x [read $f]
  $E.te.edit1 insert 1.0 $x
  set t [close $f]
 } else {
  $E.te.edit1 insert end "\\documentclass\[12pt,a4paper\]{report}\n"
#  $E.te.edit1 insert end "\\usepackage\[german\]{babel}\n"
#  $E.te.edit1 insert end "\\usepackage{umlaut}\n"
  $E.te.edit1 insert end "\\begin{document}\n\n"
  $E.te.edit1 insert end "\\end{document}\n"
 }
 Syntax $E.te.edit1 1.0 end
 set ksp [$E.te.edit1 search "\\begin{document}" 1.0]
 $E.te.edit1 mark set insert "$ksp + 1 lines"
 update
 event generate $E.te.edit1 <<Position_tail>> -when tail
# after 250 "event generate $E.te.edit1 <<Position_tail>> -when tail"
}


proc LoadFile {} {
 global FilenameArray ModifiedArray directory
 set types {
    {{latex Files} {.tex} }
    {{plain Text} {.txt}}
    {{All Files}        *}
 }
 set filename [tk_getOpenFile -filetypes $types -initialdir $directory]
 LoadFileArg $filename 0
}

proc LoadFileArg {filename line} {
 global FilenameArray UpToDateArray ModifiedArray directory
 if {[file exists $filename]} then {
  set directory [file dirname $filename]
  set E [EDITOR]
  set f [open $filename r]
  set x [read $f]
  $E.te.edit1 insert 1.0 $x
  set t [close $f]
  #Set global filename
  set FilenameArray($E) $filename
  set UpToDateArray($E) [file mtime "$filename"]
  wm title $E "$filename"
  Syntax $E.te.edit1 1.0 end
  $E.te.edit1 mark set insert "1.0 + $line lines"
  event generate $E.te.edit1 <<Modified>>
  update
  event generate $E.te.edit1 <<Position_tail>> -when tail
  update
  focus $E.te.edit1
#  after 500 "event generate $E.te.edit1 <<Position_tail>> -when tail"
#  after 500 "focus $E.te.edit1"
 }
}
proc ReLoadFileArg {E filename} {
 global FilenameArray UpToDateArray ModifiedArray directory
 if {[file exists $filename]} then {
  set directory [file dirname $filename]
  set f [open $filename r]
  set x [read $f]
  set pos [$E.te.edit1 index insert]
  $E.te.edit1 delete 1.0 end 
  $E.te.edit1 insert 1.0 $x
  $E.te.edit1 mark set insert $pos
  set t [close $f]
  #Set global filename
  set FilenameArray($E) $filename
  set UpToDateArray($E) [file mtime "$filename"]
  Syntax $E.te.edit1 1.0 end
  focus $E.te.edit1
#  after 500 "event generate $E.te.edit1 <<Position_tail>> -when tail"
#  after 500 "focus $E.te.edit1"
 }
}

proc AppendFile {E} {
 global directory
 set types {
    {{latex Files} {.tex} }
    {{plain Text} {.txt}}
    {{All Files}        *}
 }
 set filename [tk_getOpenFile -filetypes $types -initialdir $directory]
 if {$filename != ""} then {
  set directory [file dirname $filename]
  set f [open $filename r]
  set x [read $f]
  #Append file at end of text
  $E.te.edit1 insert insert $x
  #Close source file
  set t [close $f]
  Syntax $E.te.edit1 1.0 end
  event generate $E.te.edit1 <<Modified>>
}}

proc UpToDateFile {E} {
 global FilenameArray UpToDateArray T
 if [file exists $FilenameArray($E)] {
  if { [file mtime $FilenameArray($E)] > $UpToDateArray($E) } {
   switch [tk_messageBox -parent $E -icon warning -message $T(FileUpToDate) -default yes -type yesno] yes {
    ReLoadFileArg $E $FilenameArray($E)
   } no {
     set UpToDateArray($E) [file mtime $FilenameArray($E)]
   }
  }
 }
}

proc SaveFile {E} {
 global FilenameArray UpToDateArray ModifiedArray
 if {$FilenameArray($E)!= ""} then {
  UpToDateFile $E
  if { ![file exists $FilenameArray($E)] || [file mtime $FilenameArray($E)] <= $UpToDateArray($E) } {
   set f [open $FilenameArray($E) w]
   #Get text to save
   set x [$E.te.edit1 get 1.0 end]
   #Save it
   puts $f $x
   #and close file
   set t [close $f]
   set ModifiedArray($E.te.edit1) 0
   set ModifiedArrayl($E.te.edit1) [string length $x]
   set UpToDateArray($E) [file mtime $FilenameArray($E)]
  }
 } else {SaveAsFile $E}
}

proc SaveAsFile {E} {
 global FilenameArray UpToDateArray ModifiedArray directory
 set types {
    {{latex Files} {.tex} }
    {{plain Text} {.txt}}
    {{All Files}        *}
 }
 set filename [tk_getSaveFile -filetypes $types -initialfile $FilenameArray($E) -defaultextension ".tex" -initialdir $directory]
 if {$filename != ""} then {
  set directory [file dirname $filename]
  set f [open $filename w]
  #Get text to save
  set x [$E.te.edit1 get 1.0 end]
  #Save it
  puts $f $x
  #and close file
  set t [close $f]
  set FilenameArray($E) $filename
  wm title $E "$FilenameArray($E)"
  set ModifiedArray($E.te.edit1) 0
  set UpToDateArray($E) [file mtime $FilenameArray($E)]
}}