Wikia

Vim Tips Wiki

Changes: Lookup the city and state of a given US Zip code using TCL

Edit

Back to page

(Move categories to tip template)
(Remove html character entities)
 
Line 14: Line 14:
 
|category2=
 
|category2=
 
}}
 
}}
The following code allows a lookup of a US ZIP code without leaving Vim. Place the cursor over a 5-digit number (the ZIP code), and choose ZipLookup from the Utilities menu. The result is then displayed at the bottom of the screen. For example, placing the cursor over "02169" should result in "02169 ==> QUINCY, MA" being displayed.
+
The following code allows a lookup of a US ZIP code without leaving Vim. Place the cursor over a 5-digit number (the ZIP code), and choose ZipLookup from the Utilities menu. The result is then displayed at the bottom of the screen. For example, placing the cursor over "02169" should result in "02169 ==> QUINCY, MA" being displayed.
   
 
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.
 
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.
Line 24: Line 24:
 
tclfile utilities.tcl
 
tclfile utilities.tcl
 
" change the menu name and the menu item name as you see fit
 
" change the menu name and the menu item name as you see fit
menu &Utilities.&ZipLookup :call ZIPLookup(expand("<cword>")) <CR>
+
menu &Utilities.&ZipLookup :call ZIPLookup(expand("<cword>")) <CR>
 
function ZIPLookup (word)
 
function ZIPLookup (word)
 
tcl puts [ZipLookup [::vim::expr a:word]]
 
tcl puts [ZipLookup [::vim::expr a:word]]
Line 39: Line 39:
 
::http::config -useragent "Mozilla/5.0 (X11; U; Linux 2.4.22; rv:1.7.5) Gecko/20041108 Firefox/1.0"
 
::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";
 
set url "http://zip4.usps.com/zip4/zip_responseA.jsp";
while { [string length $zipcode] &lt; 5 } { set zipcode "0$zipcode" }
+
while { [string length $zipcode] < 5 } { set zipcode "0$zipcode" }
 
# the http man page is a good place to read up on these commands
 
# the http man page is a good place to read up on these commands
 
set query [::http::formatQuery zipcode $zipcode]
 
set query [::http::formatQuery zipcode $zipcode]
Line 45: Line 45:
 
set html [::http::data $http]
 
set html [::http::data $http]
 
# we use a regular expression pattern to extract the text we are looking for
 
# we use a regular expression pattern to extract the text we are looking for
if {[regexp {row1 header1[^&gt;]*&gt;[^&lt;]*&lt;font[^&gt;]*&gt;\s*([^&lt;]+).*row1 header2.*([A-Z][A-Z]).*row1 header3} $html =&gt; city state]} {
+
if {[regexp {row1 header1[^>]*>[^<]*<font[^>]*>\s*([^<]+).*row1 header2.*([A-Z][A-Z]).*row1 header3} $html => city state]} {
return "$zipcode ==&gt; $city, $state"
+
return "$zipcode ==> $city, $state"
 
} else {
 
} else {
return "$zipcode ==&gt; not found"
+
return "$zipcode ==> not found"
 
}
 
}
 
}
 
}
Line 57: Line 57:
   
 
<pre>
 
<pre>
:map \zc :!lynx [http://zip4.usps.com/zip4/zip_responseA.jsp?zipcode=<C-R><C-W><CR> http://zip4.usps.com/zip4/zip_responseA.jsp?zipcode=&lt;C-R&gt;&lt;C-W&gt;&lt;CR&gt;];
+
:map \zc :!lynx [http://zip4.usps.com/zip4/zip_responseA.jsp?zipcode=<C-R><C-W><CR> http://zip4.usps.com/zip4/zip_responseA.jsp?zipcode=<C-R><C-W><CR>];
 
</pre>
 
</pre>
   

Latest revision as of 23:27, September 29, 2008

Tip 896 Printable Monobook Previous Next

created March 15, 2005 · complexity basic · author Ulf Dittmer · version 5.7


The following code allows a lookup of a US ZIP code without leaving Vim. Place the cursor over a 5-digit number (the ZIP code), and choose ZipLookup from the Utilities menu. The result is then displayed at the bottom of the screen. For example, placing the cursor over "02169" should result in "02169 ==> QUINCY, MA" being displayed.

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("<cword>")) <CR>
  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[^>]*>[^<]*<font[^>]*>\s*([^<]+).*row1 header2.*([A-Z][A-Z]).*row1 header3} $html => city state]} {
    return "$zipcode ==> $city, $state"
  } else {
    return "$zipcode ==> not found"
  }
}

CommentsEdit

If you don't have tcl you could try something like this.

:map \zc :!lynx [http://zip4.usps.com/zip4/zip_responseA.jsp?zipcode=<C-R><C-W><CR> http://zip4.usps.com/zip4/zip_responseA.jsp?zipcode=<C-R><C-W><CR>];

Also on my bsd system I can do

$ vim /usr/share/misc/zipcodes

but this seems to be out of date.


Around Wikia's network

Random Wiki