All pastes #987191 Raw Edit

Someone

public text v1 · immutable
·published 2008-04-16 09:24 UTC
rendered paste body
" sweettabs.vim
" @Author:      Alessandro Decina <alessandro@nnva.org>
" @License:     GPL
" @Created:     11-Nov-2007.
" @Last Change: 11-Nov-2007.
" @Revision:    0.1.1
"

" reload if the config has changed or g:sweettabs_reload is set (useful for
" development)
if exists('s:sweettabs') &&
    \ s:config == s:parseConfig() &&
    \ (!exists('g:sweettabs_reload') || g:sweettabs_reload == 0)
    finish
endif

let s:sweettabs = 'VIM Tabs Done Right'

"do NOT change the values here, put let g:sweettabs_$keyname = yourvalue in your
"vimrc file to override them
let s:config_defaults = {'enable_guitablabel': 0,
  \ 'enable_shortcuts': 1, 
  \ 'move_modifier_format': '<A-%s>',
  \ 'move_tab_modifier_format': '<S-A-%s>',
  \ 'guitablabel_fixed_length': 20}

"load global configuration variables
function s:parseConfig()
  let retval = copy(s:config_defaults)

  for key in keys(s:config_defaults)
    let global_option = 'g:sweettabs_' . key
    if exists(global_option)
      retval[key] = eval(global_option)
    endif
  endfor

  return retval
endfunction

"utility function to map keys to cmd in nvoi states
function s:sweetTabsMap(modifier_fmt, keys, cmd)
  let modifier = printf(a:modifier_fmt, a:keys)

  execute 'map <silent> ' . modifier . ' ' . a:cmd . '<CR>'
  execute 'imap <silent> ' . modifier . ' <Esc>' . a:cmd . '<CR>'
endfunction

function SweetTabsMoveLeft()
  let pos = tabpagenr()
  
  if pos == 1
    return
  endif

  execute 'tabmove ' pos - 2
endfunction

function SweetTabsMoveRight()
  let pos = tabpagenr()
  execute 'tabmove ' pos
endfunction

function s:addShortcuts()
  let modifier = s:config['move_modifier_format']

  "<modifier-Left> goes to the previous tab
  call s:sweetTabsMap(modifier, 'Left', ':tabp')
  "<modifier-Left> goes to the next tab
  call s:sweetTabsMap(modifier, 'Right', ':tabn')
  
  "map <modifier-[1-9]> to :tabn [1-9]
  for i in range(1, 9)
    call s:sweetTabsMap(modifier, i, ':tabn ' . i)
  endfor

  "move tab shortcuts
  let modifier = s:config['move_tab_modifier_format']

  "<modifier-Left> moves the current tab one position left
  call s:sweetTabsMap(modifier, 'Left', ':call SweetTabsMoveLeft()')
  "<modifier-Right> moves the current tab one position right
  call s:sweetTabsMap(modifier, 'Right', ':call SweetTabsMoveRight()')
endfunction

function SweetTabsGuiTabLabel()
  let label = ''

  " prepend '+' if the tab has modified buffers, like the default label
  let buf_list = tabpagebuflist(v:lnum)
  for buf_nr in buf_list
    if getbufvar(buf_nr, '&modified')
      let label = label . '*'
      break
    endif
  endfor

  let label = label . ' ' . bufname(buf_list[tabpagewinnr(v:lnum) - 1]) 

  return label
endfunction

function s:setGuiTabLabel()
  set guitablabel=%{SweetTabsGuiTabLabel()}
endfunction

function s:Main()
  "FIXME: unmap previously mapped keys when reloading
  let s:config = s:parseConfig()
  
  "shortcuts
  if s:config['enable_shortcuts']
    call s:addShortcuts()
  endif

  "guitablabel
  if s:config['enable_guitablabel']
    call s:setGuiTabLabel()
  endif
endfunction

call s:Main()