00001 # TKE - Advanced Programmer's Editor 00002 # Copyright (C) 2014-2019 Trevor Williams (phase1geo@gmail.com) 00003 # 00004 # This program is free software; you can redistribute it and/or modify 00005 # it under the terms of the GNU General Public License as published by 00006 # the Free Software Foundation; either version 2 of the License, or 00007 # (at your option) any later version. 00008 # 00009 # This program is distributed in the hope that it will be useful, 00010 # but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 # GNU General Public License for more details. 00013 # 00014 # You should have received a copy of the GNU General Public License along 00015 # with this program; if not, write to the Free Software Foundation, Inc., 00016 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 00017 00018 ###################################################################### 00019 # Name: templates.tcl 00020 # Author: Trevor Williams (phase1geo@gmail.com) 00021 # Date: 12/24/2015 00022 # Brief: Namespace handling file templates. 00023 ###################################################################### 00024 00025 namespace eval templates { 00026 00027 array set data {} 00028 00029 set data(templates_dir) [file join $::tke_home templates] 00030 00031 ###################################################################### 00032 # Loads the contents of the templates directory. 00033 proc preload {} { 00034 00035 variable data 00036 00037 # Create the template directory 00038 set data(templates) [glob -nocomplain -tails -directory $data(templates_dir) *] 00039 00040 } 00041 00042 ###################################################################### 00043 # Loads the contents of the specified template into a new buffer and 00044 # perform the snippet insertion. 00045 proc load {name fname args} { 00046 00047 variable data 00048 00049 # Open the template file for reading 00050 if {[catch { open [get_pathname $name] r } rc]} { 00051 return -code error "Unable to read template $name" 00052 } 00053 00054 # Get the template contents 00055 set contents [read $rc] 00056 close $rc 00057 00058 # Add the buffer 00059 gui::get_info [gui::add_new_file end -name $fname -sidebar 1 {*}$args] tab txt 00060 00061 # Insert the content as a snippet 00062 snippets::insert_snippet $txt.t $contents 00063 00064 # Take the extension of the template file (if there is one) and set the 00065 # current syntax highlighting to it 00066 syntax::set_language $txt [syntax::get_default_language $name] 00067 00068 } 00069 00070 ###################################################################### 00071 # Opens a TK save dialog box to specify the filename to save. 00072 proc load_abs {name args} { 00073 00074 # Get the browse directory 00075 set dirname [gui::get_browse_directory] 00076 00077 # Get the filename from the user 00078 if {[set fname [tk_getSaveFile -parent . -initialdir $dirname -confirmoverwrite 1 -title "New Filepath"]] ne ""} { 00079 load $name $fname {*}$args 00080 } 00081 00082 } 00083 00084 ###################################################################### 00085 # Displays the user input field to get the basename of the file to 00086 # create. 00087 proc load_rel {name args} { 00088 00089 set fname "" 00090 00091 if {[gui::get_user_response "File Name:" fname -allow_vars 1]} { 00092 00093 # Normalize the pathname 00094 set fname [file join [lindex $args 0] [file tail $fname]] 00095 00096 # Load the template 00097 load $name $fname {*}[lrange $args 1 end] 00098 00099 } 00100 00101 } 00102 00103 ###################################################################### 00104 # Allows the user to edit the template. 00105 proc edit {name args} { 00106 00107 variable data 00108 00109 # Add the file for editing (but don't display the other themes in the sidebar 00110 gui::add_file end [get_pathname $name] -sidebar 0 00111 00112 } 00113 00114 ###################################################################### 00115 # Allows the user to specify the name of the template to save. 00116 proc save_as {} { 00117 00118 variable data 00119 00120 set name "" 00121 00122 # Get the template name from the user 00123 if {[gui::get_user_response [format "%s:" [msgcat::mc "Template Name"]] name]} { 00124 00125 # Create the templates directory if it does not exist 00126 file mkdir $data(templates_dir) 00127 00128 # Open the file for writing 00129 if {[catch { open [get_pathname $name] w } rc]} { 00130 return -code error "Unable to open template $name for writing" 00131 } 00132 00133 # Write the file contents 00134 puts $rc [gui::scrub_text [gui::current_txt]] 00135 close $rc 00136 00137 # Add the file to our list if it does not already exist 00138 if {[lsearch $data(templates) $name] == -1} { 00139 lappend data(templates) $name 00140 } 00141 00142 # Specify that the file was saved in the information bar 00143 gui::set_info_message [format "%s $name %s" [msgcat::mc "Template"] [msgcat::mc "saved"]] 00144 00145 } 00146 00147 } 00148 00149 ###################################################################### 00150 # Deletes the given template. 00151 proc delete {name args} { 00152 00153 variable data 00154 00155 # Confirm the deletion 00156 set answer [tk_messageBox -parent . -icon question -message [msgcat::mc "Delete template?"] \ 00157 -detail [format "%s %s" $name [msgcat::mc "will be permanently deleted"]] -type yesno -default yes] 00158 00159 # If we are told not to delete, exit this procedure now 00160 if {$answer eq "no"} { 00161 return 00162 } 00163 00164 # Delete the file 00165 if {[catch { file delete -force [get_pathname $name] } rc]} { 00166 delete -code error "Unable to delete template" 00167 } 00168 00169 # Remove the template from the list 00170 if {[set index [lsearch $data(templates) $name]] != -1} { 00171 set data(templates) [lreplace $data(templates) $index $index] 00172 } 00173 00174 # Specify that the file was deleted in the information bar 00175 gui::set_info_message [format "%s $name %s" [msgcat::mc "Template"] [msgcat::mc "deleted"]] 00176 00177 } 00178 00179 ###################################################################### 00180 # Returns true if we have at least one template available. 00181 proc valid {} { 00182 00183 variable data 00184 00185 return [expr [llength $data(templates)] > 0] 00186 00187 } 00188 00189 ###################################################################### 00190 # Returns the full pathname of the given template name. 00191 proc get_pathname {name} { 00192 00193 variable data 00194 00195 return [file join $data(templates_dir) $name] 00196 00197 } 00198 00199 ###################################################################### 00200 # Displays the templates in the command launcher. If one is selected, 00201 # performs the specified command based on type. 00202 # 00203 # Legal values for cmd_type are: 00204 # - load_abs 00205 # - load_rel 00206 # - edit 00207 # - delete 00208 proc show_templates {cmd_type args} { 00209 00210 variable data 00211 00212 # Add temporary registries to launcher 00213 set i 0 00214 foreach name [lsort $data(templates)] { 00215 launcher::register_temp "`TEMPLATE:$name" [list templates::$cmd_type $name {*}$args] $name $i [list templates::add_detail $name] 00216 incr i 00217 } 00218 00219 # Display the launcher in SNIPPET: mode 00220 launcher::launch "`TEMPLATE:" 1 00221 00222 } 00223 00224 ###################################################################### 00225 # Returns the contents of the given file. 00226 proc add_detail {name txt} { 00227 00228 if {[catch { open [get_pathname $name] r } rc]} { 00229 return "" 00230 } 00231 00232 $txt insert end [read $rc] 00233 close $rc 00234 00235 } 00236 00237 ###################################################################### 00238 # Returns the list of files/directories used by the template engine 00239 # for the purposes of importing/exporting. 00240 proc get_share_items {dir} { 00241 00242 return [list templates] 00243 00244 } 00245 00246 ###################################################################### 00247 # Called when the share directory has changed. 00248 proc share_changed {dir} { 00249 00250 variable data 00251 00252 # Create the template directory 00253 set data(templates_dir) [file join $dir templates] 00254 set data(templates) [glob -nocomplain -tails -directory $data(templates_dir) *] 00255 00256 } 00257 00258 }