" Vimball Archiver by Charles E. Campbell, Jr., Ph.D. UseVimball finish plugin\twitvim.vim [[[1 656 " ============================================================== " TwitVim - Post to Twitter from Vim " Based on Twitter Vim script by Travis Jeffery " " Version: 0.2.8 " License: Vim license. See :help license " Language: Vim script " Maintainer: Po Shan Cheah " Created: March 28, 2008 " Last updated: April 15, 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 = "" " If true, disable the Perl code that simplifies and localizes Twitter " timestamps. if !exists('g:twitvim_disable_simple_time') let g:twitvim_disable_simple_time = 0 endif " 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=twitvim" 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 " === XML helper functions === " Get the content of the n'th element in a series of elements. function! s:xml_get_nth(xmlstr, elem, n) let matchres = matchlist(a:xmlstr, '<'.a:elem.'>\(.\{-}\)', -1, a:n) return matchres == [] ? "" : matchres[1] endfunction " Get the content of the specified element. function! s:xml_get_element(xmlstr, elem) return s:xml_get_nth(a:xmlstr, a:elem, 1) endfunction " Remove any number of the specified element from the string. Used for removing " sub-elements so that you can parse the remaining elements safely. function! s:xml_remove_elements(xmlstr, elem) return substitute(a:xmlstr, '<'.a:elem.'>.\{-}', '', "g") endfunction " === XML helper functions === " === Perl time string parser === if has('perl') && !g:twitvim_disable_simple_time function s:def_perl_time_funcs() perl < 0 execute twit_bufnr . "wincmd w" call append(2, name.': '.s:convert_entity(text).' |'.date.'|') wincmd p endif endif endfunction " URL-encode a string. function! s:url_encode(str) let str = a:str let str = substitute(str, '%', '%25', "g") let str = substitute(str, '"', '%22', "g") let str = substitute(str, '&', '%26', "g") let str = substitute(str, '+', '%2B', "g") let str = substitute(str, '?', '%3F', "g") return str 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 = s:url_encode(mesg) 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 " Prompt user for tweet and then post it. " If initstr is given, use that as the initial input. function! s:CmdLine_Twitter(initstr) " 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: ", a:initstr) call inputrestore() call s:post_twitter(mesg) endfunction " Extract the user name from a line in the timeline. function! s:get_user_name(line) let matchres = matchlist(a:line, '^\(\w\+\):') return matchres != [] ? matchres[1] : "" endfunction " This is for a local mapping in the timeline. Start an @-reply on the command " line to the author of the tweet on the current line. function! s:Quick_Reply() let username = s:get_user_name(getline('.')) if username != "" call s:CmdLine_Twitter('@'.username.' ') endif endfunction " This is for a local mapping in the timeline. Start a direct message on the " command line to the author of the tweet on the current line. function! s:Quick_DM() let username = s:get_user_name(getline('.')) if username != "" call s:CmdLine_Twitter('d '.username.' ') endif 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