" Vimball Archiver by Charles E. Campbell, Jr., Ph.D. UseVimball finish plugin/twitvim.vim [[[1 383 " ============================================================== " TwitVim - Post to Twitter from Vim " Based on Twitter Vim script by Travis Jeffery " " Version: 0.2.1 " License: Vim license. See :help license " Language: Vim script " Maintainer: Po Shan Cheah " Created: March 28, 2008 " Last updated: April 10, 2008 " " GetLatestVimScripts: 2204 1 twitvim.vim " ============================================================== " Load this module only once. if exists('loaded_twitvim') finish endif let loaded_twitvim = 1 " Avoid side-effects from cpoptions setting. let s:save_cpo = &cpo set cpo&vim let s:proxy = "" let s:login = "" " The extended character limit is 246. Twitter will display a tweet longer than " 140 characters in truncated form with a link to the full tweet. If that is " undesirable, set s:char_limit to 140. let s:char_limit = 246 let s:twupdate = "http://twitter.com/statuses/update.xml?source=vim" function! s:get_config_proxy() " Get proxy setting from twitvim_proxy in .vimrc or _vimrc. " Format is proxysite:proxyport let s:proxy = exists('g:twitvim_proxy') ? "-x " . g:twitvim_proxy : "" endfunction " Get user-config variables twitvim_proxy and twitvim_login. function! s:get_config() call s:get_config_proxy() " Get Twitter login info from twitvim_login in .vimrc or _vimrc. " Format is username:password if exists('g:twitvim_login') && g:twitvim_login != '' let s:login = "-u " . g:twitvim_login else " Beep and error-highlight execute "normal \" echohl ErrorMsg echomsg 'Twitter login not set.' \ 'Please add to .vimrc: let twitvim_login="USER:PASS"' echohl None return -1 endif return 0 endfunction " Add update to Twitter buffer if public, friends, or user timeline. function! s:add_update(output) if s:twit_buftype == "public" || s:twit_buftype == "friends" || s:twit_buftype == "user" " Parse the output from the Twitter update call. let matchres = matchlist(a:output, '\(.\{-}\).\{-}\(.\{-}\).\{-}\(.\{-}\)', -1, 1) if matchres == [] return endif let twit_bufnr = bufwinnr('^'.s:twit_winname.'$') if twit_bufnr > 0 execute twit_bufnr . "wincmd w" call append(2, matchres[3].': '.s:convert_entity(matchres[2]).' |'.matchres[1].'|') wincmd p endif endif endfunction " Common code to post a message to Twitter. function! s:post_twitter(mesg) " Get user-config variables twitvim_proxy and twitvim_login. " We get these variables every time before posting to Twitter so that the " user can change them on the fly. let rc = s:get_config() if rc < 0 return -1 endif let mesg = a:mesg " Remove trailing newline. You see that when you visual-select an entire " line. Don't let it count towards the tweet length. let mesg = substitute(mesg, '\n$', '', "") " Convert internal newlines to spaces. let mesg = substitute(mesg, '\n', ' ', "g") " Check tweet length. Note that the tweet length should be checked before " URL-encoding the special characters because URL-encoding increases the " string length. if strlen(mesg) > s:char_limit echohl WarningMsg echo "Your tweet has" strlen(mesg) - s:char_limit \ "too many characters. It was not sent." echohl None elseif strlen(mesg) < 1 echohl WarningMsg echo "Your tweet was empty. It was not sent." echohl None else " URL-encode some special characters so they show up verbatim. let mesg = substitute(mesg, '%', '%25', "g") let mesg = substitute(mesg, '"', '%22', "g") let mesg = substitute(mesg, '&', '%26', "g") let output = system("curl -s ".s:proxy." ".s:login.' -d status="'. \mesg.'" '.s:twupdate) if v:shell_error != 0 echohl ErrorMsg echomsg "Error posting your tweet. Result code: ".v:shell_error echomsg "Output:" echomsg output echohl None else call s:add_update(output) echo "Your tweet was sent. You used" strlen(mesg) "characters." endif endif endfunction function! s:CmdLine_Twitter() " Do this here too to check for twitvim_login. This is to avoid having the " user type in the message only to be told that his configuration is " incomplete. let rc = s:get_config() if rc < 0 return -1 endif call inputsave() let mesg = input("Your Twitter: ") call inputrestore() call s:post_twitter(mesg) endfunction " Prompt user for tweet. if !exists(":PosttoTwitter") command PosttoTwitter :call CmdLine_Twitter() endif " Post current line to Twitter. if !exists(":CPosttoTwitter") command CPosttoTwitter :call post_twitter(getline('.')) endif " Post entire buffer to Twitter. if !exists(":BPosttoTwitter") command BPosttoTwitter :call post_twitter(join(getline(1, "$"))) endif " Post visual selection to Twitter. noremap Visual y:call post_twitter(@") noremap