vimsupport.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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 when the cursor is at the end of the
  30. line. Only the chars before the current 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 TextAfterCursor():
  38. """Returns the text after CurrentColumn."""
  39. return vim.current.line[ CurrentColumn(): ]
  40. def GetUnsavedBuffers():
  41. def BufferModified( buffer_number ):
  42. to_eval = 'getbufvar({0}, "&mod")'.format( buffer_number )
  43. return GetBoolValue( to_eval )
  44. return ( x for x in vim.buffers if BufferModified( x.number ) )
  45. # Both |line| and |column| need to be 1-based
  46. def JumpToLocation( filename, line, column ):
  47. # Add an entry to the jumplist
  48. vim.command( "normal! m'" )
  49. if filename != vim.current.buffer.name:
  50. # We prefix the command with 'keepjumps' so that opening the file is not
  51. # recorded in the jumplist. So when we open the file and move the cursor to
  52. # a location in it, the user can use CTRL-O to jump back to the original
  53. # location, not to the start of the newly opened file.
  54. # Sadly this fails on random occasions and the undesired jump remains in the
  55. # jumplist.
  56. vim.command( 'keepjumps edit {0}'.format( filename ) )
  57. vim.current.window.cursor = ( line, column - 1 )
  58. # Center the screen on the jumped-to location
  59. vim.command( 'normal! zz' )
  60. def NumLinesInBuffer( buffer ):
  61. # This is actually less than obvious, that's why it's wrapped in a function
  62. return len( buffer )
  63. def PostVimMessage( message ):
  64. # TODO: Check are we on the main thread or not, and if not, force a crash
  65. # here. This should make it impossible to accidentally call this from a
  66. # non-GUI thread which *sometimes* crashes Vim because Vim is not thread-safe.
  67. # A consistent crash should force us to notice the error.
  68. vim.command( "echohl WarningMsg | echomsg '{0}' | echohl None"
  69. .format( EscapeForVim( message ) ) )
  70. def PresentDialog( message, choices, default_choice_index = 0 ):
  71. """Presents the user with a dialog where a choice can be made.
  72. This will be a dialog for gvim users or a question in the message buffer
  73. for vim users or if `set guioptions+=c` was used.
  74. choices is list of alternatives.
  75. default_choice_index is the 0-based index of the default element
  76. that will get choosen if the user hits <CR>. Use -1 for no default.
  77. PresentDialog will return a 0-based index into the list
  78. or -1 if the dialog was dismissed by using <Esc>, Ctrl-C, etc.
  79. See also:
  80. :help confirm() in vim (Note that vim uses 1-based indexes)
  81. Example call:
  82. PresentDialog("Is this a nice example?", ["Yes", "No", "May&be"])
  83. Is this a nice example?
  84. [Y]es, (N)o, May(b)e:"""
  85. to_eval = "confirm('{0}', '{1}', {2})".format( EscapeForVim( message ),
  86. EscapeForVim( "\n" .join( choices ) ), default_choice_index + 1 )
  87. return int( vim.eval( to_eval ) ) - 1
  88. def Confirm( message ):
  89. return bool( PresentDialog( message, [ "Ok", "Cancel" ] ) == 0 )
  90. def EchoText( text ):
  91. def EchoLine( text ):
  92. vim.command( "echom '{0}'".format( EscapeForVim( text ) ) )
  93. for line in text.split( '\n' ):
  94. EchoLine( line )
  95. def EscapeForVim( text ):
  96. return text.replace( "'", "''" )
  97. def CurrentFiletypes():
  98. ft_string = vim.eval( "&filetype" )
  99. return ft_string.split( '.' )
  100. def FiletypesForBuffer( buffer_object ):
  101. # NOTE: Getting &ft for other buffers only works when the buffer has been
  102. # visited by the user at least once, which is true for modified buffers
  103. ft_string = vim.eval( 'getbufvar({0}, "&ft")'.format( buffer_object.number ) )
  104. return ft_string.split( '.' )
  105. def GetVariableValue( variable ):
  106. return vim.eval( variable )
  107. def GetBoolValue( variable ):
  108. return bool( int( vim.eval( variable ) ) )