This works by accessing the USPS web site, so one needs to be online for this to work. Since the code parses the HTML code, this breaks whenever the web site code is reorganized, which seems to happen every other year or so, but can be fixed by adjusting the regexp (which may take a bit of fiddling). The code is in TCL, so one needs to use a TCL-enabled version of Vim. The following should go in your gvimrc file: and this into utilities.tcl:
Attributes | Values |
---|
rdfs:label
| - Lookup the city and state of a given US Zip code using TCL
|
rdfs:comment
| - This works by accessing the USPS web site, so one needs to be online for this to work. Since the code parses the HTML code, this breaks whenever the web site code is reorganized, which seems to happen every other year or so, but can be fixed by adjusting the regexp (which may take a bit of fiddling). The code is in TCL, so one needs to use a TCL-enabled version of Vim. The following should go in your gvimrc file: and this into utilities.tcl:
|
Version
| |
dbkwik:vim/property/wikiPageUsesTemplate
| |
Previous
| |
Author
| |
Complexity
| |
Created
| |
ID
| |
NEXT
| |
Rating
| |
abstract
| - This works by accessing the USPS web site, so one needs to be online for this to work. Since the code parses the HTML code, this breaks whenever the web site code is reorganized, which seems to happen every other year or so, but can be fixed by adjusting the regexp (which may take a bit of fiddling). The code is in TCL, so one needs to use a TCL-enabled version of Vim. The following should go in your gvimrc file: if has("tcl") tclfile utilities.tcl " change the menu name and the menu item name as you see fit menu &Utilities.&ZipLookup :call ZIPLookup(expand("")) function ZIPLookup (word) tcl puts [ZipLookup [::vim::expr a:word]] endfunction endif and this into utilities.tcl: proc ZipLookup zipcode { package require http # some websites, not the usps necessarily, care what kind of browser is used. ::http::config -useragent "Mozilla/5.0 (X11; U; Linux 2.4.22; rv:1.7.5) Gecko/20041108 Firefox/1.0" set url "http://zip4.usps.com/zip4/zip_responseA.jsp"; while { [string length $zipcode] < 5 } { set zipcode "0$zipcode" } # the http man page is a good place to read up on these commands set query [::http::formatQuery zipcode $zipcode] set http [::http::geturl $url -query $query] set html [::http::data $http] # we use a regular expression pattern to extract the text we are looking for if {[regexp {row1 header1[^>]*>[^<]*]*>\s*([^<]+).*row1 header2.*([A-Z][A-Z]).*row1 header3} $html => city state]} { return "$zipcode ==> $city, $state" } else { return "$zipcode ==> not found" } }
|