/vim/ftplugin/latex-suite/smartspace.vim

https://bitbucket.org/vertespain/config · Vim Script · 102 lines · 61 code · 8 blank · 33 comment · 23 complexity · 5bbfd407ee08588e2b1ec4f364d7397c MD5 · raw file

  1. "=============================================================================
  2. " File: smartspace.vim
  3. " Author: Carl Muller
  4. " Created: Fri Dec 06 12:00 AM 2002 PST
  5. "
  6. " Description:
  7. " Maps the <space> key in insert mode so that mathematical formulaes are
  8. " always kept on the same line. i.e, $$'s dont get broken across multiple
  9. " lines.
  10. "=============================================================================
  11. " Avoid reinclusion or if the user doesn't want us.
  12. if exists('b:done_smartspace')
  13. \ || (exists('g:Tex_SmartKeySpace') && !g:Tex_SmartKeySpace)
  14. finish
  15. endif
  16. let b:done_smartspace = 1
  17. " Smart space relies on taking over vim's insertion of carriage returns in
  18. " order to keep $$'s on the same line. The only way to get vim not to break
  19. " lines is to set tw=0.
  20. "
  21. " NOTE: setting tw != 0 will break smartspace
  22. " the user's 'tw' setting is still respected in the insert mode.
  23. " However, normal mode actions which rely on 'tw' such as gqap will be
  24. " broken because of the faulty 'tw' setting.
  25. let b:tw = &l:tw
  26. setlocal tw=0
  27. inoremap <buffer> <silent> <Space> <Space><Esc>:call <SID>TexFill(b:tw)<CR>a
  28. " Do not redefine the function.
  29. if exists('*s:TexFill')
  30. finish
  31. endif
  32. " TexFormatLine: format line retaining $$'s on the same line. {{{
  33. function! s:TexFill(width)
  34. if a:width != 0 && col(".") > a:width
  35. " For future use, record the current line and the number of the current column
  36. let current_line = getline(".")
  37. let current_column = col(".")
  38. exe "normal! a##\<Esc>"
  39. call <SID>TexFormatLine(a:width,current_line,current_column)
  40. exe "normal! ?##\<CR>2s\<Esc>"
  41. " Remove ## from the search history.
  42. call histdel("/", -1)|let @/=histget("/", -1)
  43. endif
  44. endfunction
  45. " }}}
  46. function! s:TexFormatLine(width, current_line, current_column) " {{{
  47. " get the first non-blank character.
  48. let first = matchstr(getline('.'), '\S')
  49. normal! $
  50. let length = col('.')
  51. let go = 1
  52. while length > a:width+2 && go
  53. let between = 0
  54. let string = strpart(getline('.'), 0, a:width)
  55. " Count the dollar signs
  56. let number_of_dollars = 0
  57. let evendollars = 1
  58. let counter = 0
  59. while counter <= a:width-1
  60. " Pay attention to '$$'.
  61. if string[counter] == '$' && string[counter-1] != '$'
  62. let evendollars = 1 - evendollars
  63. let number_of_dollars = number_of_dollars + 1
  64. endif
  65. let counter = counter + 1
  66. endwhile
  67. " Get ready to split the line.
  68. exe 'normal! ' . (a:width + 1) . '|'
  69. if evendollars
  70. " Then you are not between dollars.
  71. exe "normal! ?\\$\\+\\| \<CR>W"
  72. else
  73. " Then you are between dollars.
  74. normal! F$
  75. if col(".") == 1 || getline('.')[col(".")-1] != "$"
  76. let go = 0
  77. endif
  78. endif
  79. if first == '$' && number_of_dollars == 1
  80. let go = 0
  81. else
  82. exe "normal! i\<CR>\<Esc>$"
  83. " get the first non-blank character.
  84. let first = matchstr(getline('.'), '\S')
  85. endif
  86. let length = col(".")
  87. endwhile
  88. if go == 0 && strpart(a:current_line, 0, a:current_column) =~ '.*\$.*\$.*'
  89. exe "normal! ^f$a\<CR>\<Esc>"
  90. call <SID>TexFormatLine(a:width, a:current_line, a:current_column)
  91. endif
  92. endfunction
  93. " }}}
  94. " vim:fdm=marker:ts=4:sw=4:noet