Merge branch 'ct/autoconf-htmldir'
[git.git] / git-gui / lib / tools_dlg.tcl
1 # git-gui Tools menu dialogs
2
3 class tools_add {
4
5 field w              ; # widget path
6 field w_name         ; # new remote name widget
7 field w_cmd          ; # new remote location widget
8
9 field name         {}; # name of the tool
10 field command      {}; # command to execute
11 field add_global    0; # add to the --global config
12 field no_console    0; # disable using the console
13 field needs_file    0; # ensure filename is set
14 field confirm       0; # ask for confirmation
15 field ask_branch    0; # ask for a revision
16 field ask_args      0; # ask for additional args
17
18 constructor dialog {} {
19         global repo_config use_ttk NS
20
21         make_dialog top w
22         wm title $top [append "[appname] ([reponame]): " [mc "Add Tool"]]
23         if {$top ne {.}} {
24                 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
25                 wm transient $top .
26         }
27
28         ${NS}::label $w.header -text [mc "Add New Tool Command"] \
29                 -font font_uibold -anchor center
30         pack $w.header -side top -fill x
31
32         ${NS}::frame $w.buttons
33         ${NS}::checkbutton $w.buttons.global \
34                 -text [mc "Add globally"] \
35                 -variable @add_global
36         pack $w.buttons.global -side left -padx 5
37         ${NS}::button $w.buttons.create -text [mc Add] \
38                 -default active \
39                 -command [cb _add]
40         pack $w.buttons.create -side right
41         ${NS}::button $w.buttons.cancel -text [mc Cancel] \
42                 -command [list destroy $w]
43         pack $w.buttons.cancel -side right -padx 5
44         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
45
46         ${NS}::labelframe $w.desc -text [mc "Tool Details"]
47
48         ${NS}::label $w.desc.name_cmnt -anchor w\
49                 -text [mc "Use '/' separators to create a submenu tree:"]
50         grid x $w.desc.name_cmnt -sticky we -padx {0 5} -pady {0 2}
51         ${NS}::label $w.desc.name_l -text [mc "Name:"]
52         set w_name $w.desc.name_t
53         ${NS}::entry $w_name \
54                 -width 40 \
55                 -textvariable @name \
56                 -validate key \
57                 -validatecommand [cb _validate_name %d %S]
58         grid $w.desc.name_l $w_name -sticky we -padx {0 5}
59
60         ${NS}::label $w.desc.cmd_l -text [mc "Command:"]
61         set w_cmd $w.desc.cmd_t
62         ${NS}::entry $w_cmd \
63                 -width 40 \
64                 -textvariable @command
65         grid $w.desc.cmd_l $w_cmd -sticky we -padx {0 5} -pady {0 3}
66
67         grid columnconfigure $w.desc 1 -weight 1
68         pack $w.desc -anchor nw -fill x -pady 5 -padx 5
69
70         ${NS}::checkbutton $w.confirm \
71                 -text [mc "Show a dialog before running"] \
72                 -variable @confirm -command [cb _check_enable_dlg]
73
74         ${NS}::labelframe $w.dlg -labelwidget $w.confirm
75
76         ${NS}::checkbutton $w.dlg.askbranch \
77                 -text [mc "Ask the user to select a revision (sets \$REVISION)"] \
78                 -variable @ask_branch -state disabled
79         pack $w.dlg.askbranch -anchor w -padx 15
80
81         ${NS}::checkbutton $w.dlg.askargs \
82                 -text [mc "Ask the user for additional arguments (sets \$ARGS)"] \
83                 -variable @ask_args -state disabled
84         pack $w.dlg.askargs -anchor w -padx 15
85
86         pack $w.dlg -anchor nw -fill x -pady {0 8} -padx 5
87
88         ${NS}::checkbutton $w.noconsole \
89                 -text [mc "Don't show the command output window"] \
90                 -variable @no_console
91         pack $w.noconsole -anchor w -padx 5
92
93         ${NS}::checkbutton $w.needsfile \
94                 -text [mc "Run only if a diff is selected (\$FILENAME not empty)"] \
95                 -variable @needs_file
96         pack $w.needsfile -anchor w -padx 5
97
98         bind $w <Visibility> [cb _visible]
99         bind $w <Key-Escape> [list destroy $w]
100         bind $w <Key-Return> [cb _add]\;break
101         tkwait window $w
102 }
103
104 method _check_enable_dlg {} {
105         if {$confirm} {
106                 $w.dlg.askbranch configure -state normal
107                 $w.dlg.askargs configure -state normal
108         } else {
109                 $w.dlg.askbranch configure -state disabled
110                 $w.dlg.askargs configure -state disabled
111         }
112 }
113
114 method _add {} {
115         global repo_config
116
117         if {$name eq {}} {
118                 error_popup [mc "Please supply a name for the tool."]
119                 focus $w_name
120                 return
121         }
122
123         set item "guitool.$name.cmd"
124
125         if {[info exists repo_config($item)]} {
126                 error_popup [mc "Tool '%s' already exists." $name]
127                 focus $w_name
128                 return
129         }
130
131         set cmd [list git config]
132         if {$add_global} { lappend cmd --global }
133         set items {}
134         if {$no_console} { lappend items "guitool.$name.noconsole" }
135         if {$needs_file} { lappend items "guitool.$name.needsfile" }
136         if {$confirm} {
137                 if {$ask_args}   { lappend items "guitool.$name.argprompt" }
138                 if {$ask_branch} { lappend items "guitool.$name.revprompt" }
139                 if {!$ask_args && !$ask_branch} {
140                         lappend items "guitool.$name.confirm"
141                 }
142         }
143
144         if {[catch {
145                 eval $cmd [list $item $command]
146                 foreach citem $items { eval $cmd [list $citem yes] }
147             } err]} {
148                 error_popup [mc "Could not add tool:\n%s" $err]
149         } else {
150                 set repo_config($item) $command
151                 foreach citem $items { set repo_config($citem) yes }
152
153                 tools_populate_all
154         }
155
156         destroy $w
157 }
158
159 method _validate_name {d S} {
160         if {$d == 1} {
161                 if {[regexp {[~?*&\[\0\"\\\{]} $S]} {
162                         return 0
163                 }
164         }
165         return 1
166 }
167
168 method _visible {} {
169         grab $w
170         $w_name icursor end
171         focus $w_name
172 }
173
174 }
175
176 class tools_remove {
177
178 field w              ; # widget path
179 field w_names        ; # name list
180
181 constructor dialog {} {
182         global repo_config global_config system_config use_ttk NS
183
184         load_config 1
185
186         make_dialog top w
187         wm title $top [append "[appname] ([reponame]): " [mc "Remove Tool"]]
188         if {$top ne {.}} {
189                 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
190                 wm transient $top .
191         }
192
193         ${NS}::label $w.header -text [mc "Remove Tool Commands"] \
194                 -font font_uibold -anchor center
195         pack $w.header -side top -fill x
196
197         ${NS}::frame $w.buttons
198         ${NS}::button $w.buttons.create -text [mc Remove] \
199                 -default active \
200                 -command [cb _remove]
201         pack $w.buttons.create -side right
202         ${NS}::button $w.buttons.cancel -text [mc Cancel] \
203                 -command [list destroy $w]
204         pack $w.buttons.cancel -side right -padx 5
205         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
206
207         ${NS}::frame $w.list
208         set w_names $w.list.l
209         slistbox $w_names \
210                 -height 10 \
211                 -width 30 \
212                 -selectmode extended \
213                 -exportselection false
214         pack $w.list.l -side left -fill both -expand 1
215         pack $w.list -fill both -expand 1 -pady 5 -padx 5
216
217         set local_cnt 0
218         foreach fullname [tools_list] {
219                 # Cannot delete system tools
220                 if {[info exists system_config(guitool.$fullname.cmd)]} continue
221
222                 $w_names insert end $fullname
223                 if {![info exists global_config(guitool.$fullname.cmd)]} {
224                         $w_names itemconfigure end -foreground blue
225                         incr local_cnt
226                 }
227         }
228
229         if {$local_cnt > 0} {
230                 ${NS}::label $w.colorlbl -foreground blue \
231                         -text [mc "(Blue denotes repository-local tools)"]
232                 pack $w.colorlbl -fill x -pady 5 -padx 5
233         }
234
235         bind $w <Visibility> [cb _visible]
236         bind $w <Key-Escape> [list destroy $w]
237         bind $w <Key-Return> [cb _remove]\;break
238         tkwait window $w
239 }
240
241 method _remove {} {
242         foreach i [$w_names curselection] {
243                 set name [$w_names get $i]
244
245                 catch { git config --remove-section guitool.$name }
246                 catch { git config --global --remove-section guitool.$name }
247         }
248
249         load_config 0
250         tools_populate_all
251
252         destroy $w
253 }
254
255 method _visible {} {
256         grab $w
257         focus $w_names
258 }
259
260 }
261
262 class tools_askdlg {
263
264 field w              ; # widget path
265 field w_rev        {}; # revision browser
266 field w_args       {}; # arguments
267
268 field is_ask_args   0; # has arguments field
269 field is_ask_revs   0; # has revision browser
270
271 field is_ok         0; # ok to start
272 field argstr       {}; # arguments
273
274 constructor dialog {fullname} {
275         global M1B use_ttk NS
276
277         set title [get_config "guitool.$fullname.title"]
278         if {$title eq {}} {
279                 regsub {/} $fullname { / } title
280         }
281
282         make_dialog top w -autodelete 0
283         wm title $top [append "[appname] ([reponame]): " $title]
284         if {$top ne {.}} {
285                 wm geometry $top "+[winfo rootx .]+[winfo rooty .]"
286                 wm transient $top .
287         }
288
289         set prompt [get_config "guitool.$fullname.prompt"]
290         if {$prompt eq {}} {
291                 set command [get_config "guitool.$fullname.cmd"]
292                 set prompt [mc "Run Command: %s" $command]
293         }
294
295         ${NS}::label $w.header -text $prompt -font font_uibold -anchor center
296         pack $w.header -side top -fill x
297
298         set argprompt [get_config "guitool.$fullname.argprompt"]
299         set revprompt [get_config "guitool.$fullname.revprompt"]
300
301         set is_ask_args [expr {$argprompt ne {}}]
302         set is_ask_revs [expr {$revprompt ne {}}]
303
304         if {$is_ask_args} {
305                 if {$argprompt eq {yes} || $argprompt eq {true} || $argprompt eq {1}} {
306                         set argprompt [mc "Arguments"]
307                 }
308
309                 ${NS}::labelframe $w.arg -text $argprompt
310
311                 set w_args $w.arg.txt
312                 ${NS}::entry $w_args \
313                         -width 40 \
314                         -textvariable @argstr
315                 pack $w_args -padx 5 -pady 5 -fill both
316                 pack $w.arg -anchor nw -fill both -pady 5 -padx 5
317         }
318
319         if {$is_ask_revs} {
320                 if {$revprompt eq {yes} || $revprompt eq {true} || $revprompt eq {1}} {
321                         set revprompt [mc "Revision"]
322                 }
323
324                 if {[is_config_true "guitool.$fullname.revunmerged"]} {
325                         set w_rev [::choose_rev::new_unmerged $w.rev $revprompt]
326                 } else {
327                         set w_rev [::choose_rev::new $w.rev $revprompt]
328                 }
329
330                 pack $w.rev -anchor nw -fill both -expand 1 -pady 5 -padx 5
331         }
332
333         ${NS}::frame $w.buttons
334         if {$is_ask_revs} {
335                 ${NS}::button $w.buttons.visualize \
336                         -text [mc Visualize] \
337                         -command [cb _visualize]
338                 pack $w.buttons.visualize -side left
339         }
340         ${NS}::button $w.buttons.ok \
341                 -text [mc OK] \
342                 -command [cb _start]
343         pack $w.buttons.ok -side right
344         ${NS}::button $w.buttons.cancel \
345                 -text [mc "Cancel"] \
346                 -command [cb _cancel]
347         pack $w.buttons.cancel -side right -padx 5
348         pack $w.buttons -side bottom -fill x -pady 10 -padx 10
349
350         bind $w <$M1B-Key-Return> [cb _start]
351         bind $w <Key-Return> [cb _start]
352         bind $w <Key-Escape> [cb _cancel]
353         wm protocol $w WM_DELETE_WINDOW [cb _cancel]
354
355         bind $w <Visibility> [cb _visible]
356         return $this
357 }
358
359 method execute {} {
360         tkwait window $w
361         set rv $is_ok
362         delete_this
363         return $rv
364 }
365
366 method _visible {} {
367         grab $w
368         if {$is_ask_args} {
369                 focus $w_args
370         } elseif {$is_ask_revs} {
371                 $w_rev focus_filter
372         }
373 }
374
375 method _cancel {} {
376         wm protocol $w WM_DELETE_WINDOW {}
377         destroy $w
378 }
379
380 method _rev {} {
381         if {[catch {$w_rev commit_or_die}]} {
382                 return {}
383         }
384         return [$w_rev get]
385 }
386
387 method _visualize {} {
388         global current_branch
389         set rev [_rev $this]
390         if {$rev ne {}} {
391                 do_gitk [list --left-right "$current_branch...$rev"]
392         }
393 }
394
395 method _start {} {
396         global env
397
398         if {$is_ask_revs} {
399                 set name [_rev $this]
400                 if {$name eq {}} {
401                         return
402                 }
403                 set env(REVISION) $name
404         }
405
406         if {$is_ask_args} {
407                 set env(ARGS) $argstr
408         }
409
410         set is_ok 1
411         _cancel $this
412 }
413
414 }