1
0

vimsupport.py 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. #!/usr/bin/env python
  2. #
  3. # Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
  4. #
  5. # This file is part of YouCompleteMe.
  6. #
  7. # YouCompleteMe is free software: you can redistribute it and/or modify
  8. # it under the terms of the GNU General Public License as published by
  9. # the Free Software Foundation, either version 3 of the License, or
  10. # (at your option) any later version.
  11. #
  12. # YouCompleteMe is distributed in the hope that it will be useful,
  13. # but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. # GNU General Public License for more details.
  16. #
  17. # You should have received a copy of the GNU General Public License
  18. # along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
  19. import vim
  20. def CurrentLineAndColumn():
  21. """Returns the 0-based current line and 0-based current column."""
  22. # See the comment in CurrentColumn about the calculation for the line and
  23. # column number
  24. line, column = vim.current.window.cursor
  25. line -= 1
  26. return line, column
  27. def CurrentColumn():
  28. """Returns the 0-based current column. Do NOT access the CurrentColumn in
  29. vim.current.line. It doesn't exist yet. Only the chars before the current
  30. column exist in vim.current.line."""
  31. # vim's columns are 1-based while vim.current.line columns are 0-based
  32. # ... but vim.current.window.cursor (which returns a (line, column) tuple)
  33. # columns are 0-based, while the line from that same tuple is 1-based.
  34. # vim.buffers buffer objects OTOH have 0-based lines and columns.
  35. # Pigs have wings and I'm a loopy purple duck. Everything makes sense now.
  36. return vim.current.window.cursor[ 1 ]
  37. def GetUnsavedBuffers():
  38. def BufferModified( buffer_number ):
  39. to_eval = 'getbufvar({0}, "&mod")'.format( buffer_number )
  40. return GetBoolValue( to_eval )
  41. return ( x for x in vim.buffers if BufferModified( x.number ) )
  42. # Both |line| and |column| need to be 1-based
  43. def JumpToLocation( filename, line, column ):
  44. # Add an entry to the jumplist
  45. vim.command( "normal! m'" )
  46. if filename != vim.current.buffer.name:
  47. # We prefix the command with 'keepjumps' so that opening the file is not
  48. # recorded in the jumplist. So when we open the file and move the cursor to
  49. # a location in it, the user can use CTRL-O to jump back to the original
  50. # location, not to the start of the newly opened file.
  51. # Sadly this fails on random occasions and the undesired jump remains in the
  52. # jumplist.
  53. vim.command( 'keepjumps edit {0}'.format( filename ) )
  54. vim.current.window.cursor = ( line, column - 1 )
  55. # Center the screen on the jumped-to location
  56. vim.command( 'normal! zz' )
  57. def NumLinesInBuffer( buffer ):
  58. # This is actually less than obvious, that's why it's wrapped in a function
  59. return len( buffer )
  60. def PostVimMessage( message ):
  61. vim.command( "echohl WarningMsg | echomsg '{0}' | echohl None"
  62. .format( EscapeForVim( message ) ) )
  63. def PresentDialog( message, choices, default_choice_index = 0 ):
  64. """Presents the user with a dialog where a choice can be made.
  65. This will be a dialog for gvim users or a question in the message buffer
  66. for vim users or if `set guioptions+=c` was used.
  67. choices is list of alternatives.
  68. default_choice_index is the 0-based index of the default element
  69. that will get choosen if the user hits <CR>. Use -1 for no default.
  70. PresentDialog will return a 0-based index into the list
  71. or -1 if the dialog was dismissed by using <Esc>, Ctrl-C, etc.
  72. See also:
  73. :help confirm() in vim (Note that vim uses 1-based indexes)
  74. Example call:
  75. PresentDialog("Is this a nice example?", ["Yes", "No", "May&be"])
  76. Is this a nice example?
  77. [Y]es, (N)o, May(b)e:"""
  78. to_eval = "confirm('{0}', '{1}', {2})".format( EscapeForVim( message ),
  79. EscapeForVim( "\n" .join( choices ) ), default_choice_index + 1 )
  80. return int( vim.eval( to_eval ) ) - 1
  81. def Confirm( message ):
  82. return bool( PresentDialog( message, [ "Ok", "Cancel" ] ) == 0 )
  83. def EchoText( text ):
  84. def EchoLine( text ):
  85. vim.command( "echom '{0}'".format( EscapeForVim( text ) ) )
  86. for line in text.split( '\n' ):
  87. EchoLine( line )
  88. def EscapeForVim( text ):
  89. return text.replace( "'", "''" )
  90. def CurrentFiletypes():
  91. ft_string = vim.eval( "&filetype" )
  92. return ft_string.split( '.' )
  93. def FiletypesForBuffer( buffer_object ):
  94. # NOTE: Getting &ft for other buffers only works when the buffer has been
  95. # visited by the user at least once, which is true for modified buffers
  96. ft_string = vim.eval( 'getbufvar({0}, "&ft")'.format( buffer_object.number ) )
  97. return ft_string.split( '.' )
  98. def GetVariableValue( variable ):
  99. return vim.eval( variable )
  100. def GetBoolValue( variable ):
  101. return bool( int( vim.eval( variable ) ) )