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: cliphist.tcl 00020 # Author: Trevor Williams (trevorw@sgi.com) 00021 # Date: 05/21/2013 00022 # Brief: Handles clipboard history. 00023 ###################################################################### 00024 00025 namespace eval cliphist { 00026 00027 variable cliphist_file [file join $::tke_home cliphist.dat] 00028 variable hist {} 00029 00030 ###################################################################### 00031 # Load the contents of the saved clipboard history. 00032 proc load {} { 00033 00034 variable cliphist_file 00035 variable hist 00036 00037 if {![catch { open $cliphist_file r } rc]} { 00038 00039 # Read the contents of the file 00040 set contents [read $rc] 00041 close $rc 00042 00043 # Parse the file 00044 set in_history 0 00045 foreach line [split $contents \n] { 00046 if {$line eq "clipping:"} { 00047 set in_history 1 00048 set clipping "" 00049 } elseif {$in_history && [regexp {^\t(.*)$} $line -> str]} { 00050 append clipping "$str\n" 00051 } elseif {$in_history} { 00052 set clipping [string range $clipping 0 end-1] 00053 set in_history 0 00054 lappend hist $clipping 00055 } 00056 } 00057 00058 } 00059 00060 # Handle any changes to the clipboard history depth 00061 trace variable preferences::prefs(Editor/ClipboardHistoryDepth) w cliphist::handle_cliphist_depth 00062 00063 } 00064 00065 ###################################################################### 00066 # Saves the state of the clipboard history to a file. This is called 00067 # prior to exiting the application. 00068 proc save {} { 00069 00070 variable cliphist_file 00071 variable hist 00072 00073 if {![catch { open $cliphist_file w } rc]} { 00074 foreach clipping $hist { 00075 puts $rc "clipping:" 00076 foreach line [split $clipping \n] { 00077 puts $rc "\t$line" 00078 } 00079 puts $rc "" 00080 } 00081 close $rc 00082 } 00083 00084 } 00085 00086 ###################################################################### 00087 # Adds the current clipboard contents to the clipboard history list. 00088 proc add_from_clipboard {} { 00089 00090 variable hist 00091 00092 # Get the clipboard content 00093 set str [string map {\{ \\\{} [clipboard get]] 00094 00095 if {[string trim $str] ne ""} { 00096 00097 # If the string doesn't exist in history, add it 00098 if {[set index [lsearch -exact $hist $str]] == -1} { 00099 00100 # Append the string to the history file 00101 lappend hist $str 00102 00103 # Trim the history to meet the maxsize requirement, if necessary 00104 if {[llength $hist] > [preferences::get Editor/ClipboardHistoryDepth]} { 00105 set hist [lrange $hist 1 end] 00106 } 00107 00108 # Otherwise, move the current string to the beginning of the history 00109 } else { 00110 00111 set hist [linsert [lreplace $hist $index $index] end $str] 00112 00113 } 00114 00115 } 00116 00117 } 00118 00119 ###################################################################### 00120 # Adds the given string to the text widget. 00121 proc add_detail {str txt} { 00122 00123 $txt insert end $str 00124 00125 } 00126 00127 ###################################################################### 00128 # Adds the current string from the clipboard history list to the clipboard 00129 # and immediately adds the string to the current text widget, formatting 00130 # the text. 00131 proc add_to_clipboard {str} { 00132 00133 # Add the string to the clipboard 00134 clipboard clear 00135 clipboard append [string map {\\\{ \{} $str] 00136 00137 # We need to make sure that this string is updated within the clipboard 00138 add_from_clipboard 00139 00140 # Insert the string in the current text widget 00141 gui::paste_and_format 00142 00143 } 00144 00145 ###################################################################### 00146 # Returns the clipboard history as a list of string pairs where the 00147 # first item is the value to use in the listbox while the second pair 00148 # should be used in the full detail. 00149 proc get_history {} { 00150 00151 variable hist 00152 00153 set items [list] 00154 00155 foreach item [lreverse $hist] { 00156 set lines [split $item \n] 00157 set short [lindex $lines 0] 00158 if {[llength $lines] > 1} { 00159 append short " ..." 00160 } 00161 lappend items [list [string map {\\\{ \{} $short] [string map {\\\{ \{} $item]] 00162 } 00163 00164 return $items 00165 00166 } 00167 00168 ###################################################################### 00169 # Creates a launcher window that contains clipboard history with a preview. 00170 proc show_cliphist {} { 00171 00172 variable hist 00173 00174 # Add temporary registries to launcher 00175 set i 0 00176 foreach strs [get_history] { 00177 lassign $strs name str 00178 launcher::register_temp "`CLIPHIST:$name" [list cliphist::add_to_clipboard $str] $name $i [list cliphist::add_detail $str] 00179 incr i 00180 } 00181 00182 # Display the launcher in CLIPHIST: mode 00183 launcher::launch "`CLIPHIST:" 1 00184 00185 } 00186 00187 ###################################################################### 00188 # Debugging procedure. 00189 proc printable_hist {} { 00190 00191 variable hist 00192 00193 return [format { -%s } [join $hist "\n -"]] 00194 00195 } 00196 00197 ###################################################################### 00198 # Handles any changes to the clipboard history depth preference value. 00199 proc handle_cliphist_depth {name1 name2 op} { 00200 00201 variable hist 00202 00203 if {[set diff [expr [llength $hist] - [preferences::get Editor/ClipboardHistoryDepth]]] > 0} { 00204 set hist [lrange $hist $diff end] 00205 } 00206 00207 } 00208 00209 } 00210