randomfox (randomfox) wrote,
randomfox
randomfox

Vim: Convert waypoint-name pair into geocaching link

This Vim macro converts a line of the form "GCxxxx cache-title" into a HTML link to the geocache page.


" convert waypoint to ID number
" returns -1 if invalid waypoint
function! GCtoID(waypt)
    let waypt = toupper(a:waypt)
    let WPTCHARS = "0123456789ABCDEFGHJKMNPQRTVWXYZ"

    if match(waypt, '^GC[0-9A-F]\{1,4}$') >= 0
	return "0x" . strpart(waypt, 2) + 0
    " elseif match(waypt, '^GC[GHJ-KM-NP-RTV-Z][0-9A-HJ-KM-NP-RTV-Z]\{3}$') >= 0
    elseif match(waypt, '^GC[' . strpart(WPTCHARS, 16) . '][' . WPTCHARS . ']\{3}$') >= 0
	let accum = 0
	let i = 2
	while i < 6
	    " echo waypt[i]
	    let accum = accum * 31 + match(WPTCHARS, waypt[i])
	    if i == 2
		let accum = accum - 16
	    endif
	    let i = i + 1
	endwhile
	return accum + 65536
    else
	return -1
    endif
endfunction

" ,c Convert a line of the form
" waypt cache-title
" into a HTML geocache link.
function! ConvertGC()
    let mx = '^\s*\(\S\+\)\s\+\(.*\)$'
    let waypt = substitute(getline("."), mx, '\1', '')
    let title = substitute(getline("."), mx, '\2', '')

    " echo waypt
    " echo title

    let id = GCtoID(waypt)
    if id >= 0
	call setline(".", "<p><a href=\"http://www.geocaching.com/seek/cache_details.aspx?pf=y&ID=" . id . "&log=y&decrypt=y\">" . title . "</a>")
    endif
endfunction

nmap ,c :call ConvertGC()<cr>
vmap ,c :call ConvertGC()<cr>

Subscribe
  • Post a new comment

    Error

    Anonymous comments are disabled in this journal

    default userpic

    Your reply will be screened

    Your IP address will be recorded 

  • 0 comments