2012-08-04 20:46:54 -04:00
|
|
|
#!/usr/bin/env python
|
|
|
|
#
|
|
|
|
# Copyright (C) 2011, 2012 Strahinja Val Markovic <val@markovic.io>
|
|
|
|
#
|
|
|
|
# This file is part of YouCompleteMe.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# YouCompleteMe is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
import vim
|
2013-10-03 13:14:31 -04:00
|
|
|
import os
|
2013-10-04 15:23:33 -04:00
|
|
|
import json
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
def CurrentLineAndColumn():
|
2012-09-24 22:20:33 -04:00
|
|
|
"""Returns the 0-based current line and 0-based current column."""
|
2012-08-04 20:46:54 -04:00
|
|
|
# See the comment in CurrentColumn about the calculation for the line and
|
|
|
|
# column number
|
|
|
|
line, column = vim.current.window.cursor
|
|
|
|
line -= 1
|
|
|
|
return line, column
|
|
|
|
|
|
|
|
|
|
|
|
def CurrentColumn():
|
2012-08-15 22:39:03 -04:00
|
|
|
"""Returns the 0-based current column. Do NOT access the CurrentColumn in
|
2013-06-09 20:31:17 -04:00
|
|
|
vim.current.line. It doesn't exist yet when the cursor is at the end of the
|
|
|
|
line. Only the chars before the current column exist in vim.current.line."""
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
# vim's columns are 1-based while vim.current.line columns are 0-based
|
|
|
|
# ... but vim.current.window.cursor (which returns a (line, column) tuple)
|
|
|
|
# columns are 0-based, while the line from that same tuple is 1-based.
|
|
|
|
# vim.buffers buffer objects OTOH have 0-based lines and columns.
|
|
|
|
# Pigs have wings and I'm a loopy purple duck. Everything makes sense now.
|
|
|
|
return vim.current.window.cursor[ 1 ]
|
|
|
|
|
|
|
|
|
2013-06-09 22:00:49 -04:00
|
|
|
def TextAfterCursor():
|
|
|
|
"""Returns the text after CurrentColumn."""
|
|
|
|
return vim.current.line[ CurrentColumn(): ]
|
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
# Note the difference between buffer OPTIONS and VARIABLES; the two are not
|
|
|
|
# the same.
|
|
|
|
def GetBufferOption( buffer_object, option ):
|
|
|
|
# The 'options' property is only available in recent (7.4+) Vim builds
|
|
|
|
if hasattr( buffer_object, 'options' ):
|
|
|
|
return buffer_object.options[ option ]
|
2012-08-04 20:46:54 -04:00
|
|
|
|
2013-09-16 16:55:41 -04:00
|
|
|
to_eval = 'getbufvar({0}, "&{1}")'.format( buffer_object.number, option )
|
2013-09-06 02:43:14 -04:00
|
|
|
return GetVariableValue( to_eval )
|
|
|
|
|
|
|
|
|
|
|
|
def GetUnsavedAndCurrentBufferData():
|
|
|
|
def BufferModified( buffer_object ):
|
|
|
|
return bool( int( GetBufferOption( buffer_object, 'mod' ) ) )
|
|
|
|
|
|
|
|
buffers_data = {}
|
|
|
|
for buffer_object in vim.buffers:
|
|
|
|
if not ( BufferModified( buffer_object ) or
|
|
|
|
buffer_object == vim.current.buffer ):
|
|
|
|
continue
|
|
|
|
|
|
|
|
buffers_data[ buffer_object.name ] = {
|
|
|
|
'contents': '\n'.join( buffer_object ),
|
|
|
|
'filetypes': FiletypesForBuffer( buffer_object )
|
|
|
|
}
|
|
|
|
|
|
|
|
return buffers_data
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2013-10-03 13:14:31 -04:00
|
|
|
def GetBufferNumberForFilename( filename, open_file_if_needed = True ):
|
|
|
|
return int( vim.eval( "bufnr('{0}', {1})".format(
|
|
|
|
os.path.realpath( filename ),
|
|
|
|
int( open_file_if_needed ) ) ) )
|
|
|
|
|
|
|
|
|
2013-10-04 15:23:33 -04:00
|
|
|
# Given a dict like {'a': 1}, loads it into Vim as if you ran 'let g:a = 1'
|
|
|
|
# When |overwrite| is True, overwrites the existing value in Vim.
|
|
|
|
def LoadDictIntoVimGlobals( new_globals, overwrite = True ):
|
|
|
|
extend_option = '"force"' if overwrite else '"keep"'
|
|
|
|
|
|
|
|
# We need to use json.dumps because that won't use the 'u' prefix on strings
|
|
|
|
# which Vim would bork on.
|
2013-10-04 18:44:16 -04:00
|
|
|
vim.eval( 'extend( g:, {0}, {1})'.format( json.dumps( new_globals ),
|
|
|
|
extend_option ) )
|
2013-10-04 15:23:33 -04:00
|
|
|
|
|
|
|
|
|
|
|
# Changing the returned dict will NOT change the value in Vim.
|
|
|
|
def GetReadOnlyVimGlobals():
|
|
|
|
try:
|
|
|
|
# vim.vars is fairly new so it might not exist
|
|
|
|
return vim.vars
|
|
|
|
except:
|
|
|
|
return vim.eval( 'g:' )
|
|
|
|
|
|
|
|
|
2013-03-31 23:38:29 -04:00
|
|
|
# Both |line| and |column| need to be 1-based
|
|
|
|
def JumpToLocation( filename, line, column ):
|
|
|
|
# Add an entry to the jumplist
|
2013-04-01 00:01:38 -04:00
|
|
|
vim.command( "normal! m'" )
|
2013-03-31 23:38:29 -04:00
|
|
|
|
|
|
|
if filename != vim.current.buffer.name:
|
|
|
|
# We prefix the command with 'keepjumps' so that opening the file is not
|
|
|
|
# recorded in the jumplist. So when we open the file and move the cursor to
|
|
|
|
# a location in it, the user can use CTRL-O to jump back to the original
|
|
|
|
# location, not to the start of the newly opened file.
|
|
|
|
# Sadly this fails on random occasions and the undesired jump remains in the
|
|
|
|
# jumplist.
|
|
|
|
vim.command( 'keepjumps edit {0}'.format( filename ) )
|
|
|
|
vim.current.window.cursor = ( line, column - 1 )
|
|
|
|
|
|
|
|
# Center the screen on the jumped-to location
|
|
|
|
vim.command( 'normal! zz' )
|
|
|
|
|
|
|
|
|
2013-09-06 02:43:14 -04:00
|
|
|
def NumLinesInBuffer( buffer_object ):
|
2012-08-04 20:46:54 -04:00
|
|
|
# This is actually less than obvious, that's why it's wrapped in a function
|
2013-09-06 02:43:14 -04:00
|
|
|
return len( buffer_object )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2013-10-01 13:41:34 -04:00
|
|
|
# Calling this function from the non-GUI thread will sometimes crash Vim. At the
|
|
|
|
# time of writing, YCM only uses the GUI thread inside Vim (this used to not be
|
|
|
|
# the case).
|
2012-08-04 20:46:54 -04:00
|
|
|
def PostVimMessage( message ):
|
2013-02-10 22:55:05 -05:00
|
|
|
vim.command( "echohl WarningMsg | echomsg '{0}' | echohl None"
|
2013-09-27 16:52:04 -04:00
|
|
|
.format( EscapeForVim( str( message ) ) ) )
|
2012-08-04 20:46:54 -04:00
|
|
|
|
|
|
|
|
2013-02-25 04:49:51 -05:00
|
|
|
def PresentDialog( message, choices, default_choice_index = 0 ):
|
|
|
|
"""Presents the user with a dialog where a choice can be made.
|
|
|
|
This will be a dialog for gvim users or a question in the message buffer
|
|
|
|
for vim users or if `set guioptions+=c` was used.
|
|
|
|
|
|
|
|
choices is list of alternatives.
|
|
|
|
default_choice_index is the 0-based index of the default element
|
|
|
|
that will get choosen if the user hits <CR>. Use -1 for no default.
|
|
|
|
|
|
|
|
PresentDialog will return a 0-based index into the list
|
|
|
|
or -1 if the dialog was dismissed by using <Esc>, Ctrl-C, etc.
|
|
|
|
|
|
|
|
See also:
|
|
|
|
:help confirm() in vim (Note that vim uses 1-based indexes)
|
|
|
|
|
|
|
|
Example call:
|
|
|
|
PresentDialog("Is this a nice example?", ["Yes", "No", "May&be"])
|
|
|
|
Is this a nice example?
|
|
|
|
[Y]es, (N)o, May(b)e:"""
|
|
|
|
to_eval = "confirm('{0}', '{1}', {2})".format( EscapeForVim( message ),
|
|
|
|
EscapeForVim( "\n" .join( choices ) ), default_choice_index + 1 )
|
|
|
|
return int( vim.eval( to_eval ) ) - 1
|
|
|
|
|
|
|
|
|
|
|
|
def Confirm( message ):
|
|
|
|
return bool( PresentDialog( message, [ "Ok", "Cancel" ] ) == 0 )
|
|
|
|
|
|
|
|
|
2012-08-15 22:39:03 -04:00
|
|
|
def EchoText( text ):
|
2013-02-21 13:50:38 -05:00
|
|
|
def EchoLine( text ):
|
|
|
|
vim.command( "echom '{0}'".format( EscapeForVim( text ) ) )
|
|
|
|
|
|
|
|
for line in text.split( '\n' ):
|
|
|
|
EchoLine( line )
|
2012-08-15 22:39:03 -04:00
|
|
|
|
|
|
|
|
2012-08-04 20:46:54 -04:00
|
|
|
def EscapeForVim( text ):
|
|
|
|
return text.replace( "'", "''" )
|
|
|
|
|
|
|
|
|
2013-01-31 19:19:56 -05:00
|
|
|
def CurrentFiletypes():
|
2013-09-06 02:43:14 -04:00
|
|
|
return vim.eval( "&filetype" ).split( '.' )
|
2012-08-05 16:12:10 -04:00
|
|
|
|
|
|
|
|
2013-03-03 13:48:34 -05:00
|
|
|
def FiletypesForBuffer( buffer_object ):
|
|
|
|
# NOTE: Getting &ft for other buffers only works when the buffer has been
|
|
|
|
# visited by the user at least once, which is true for modified buffers
|
2013-09-06 02:43:14 -04:00
|
|
|
return GetBufferOption( buffer_object, 'ft' ).split( '.' )
|
2013-03-03 13:48:34 -05:00
|
|
|
|
|
|
|
|
2012-08-05 16:12:10 -04:00
|
|
|
def GetVariableValue( variable ):
|
|
|
|
return vim.eval( variable )
|
2013-02-25 04:49:17 -05:00
|
|
|
|
|
|
|
|
|
|
|
def GetBoolValue( variable ):
|
|
|
|
return bool( int( vim.eval( variable ) ) )
|
2013-09-02 17:45:53 -04:00
|
|
|
|
|
|
|
|
|
|
|
def GetIntValue( variable ):
|
|
|
|
return int( vim.eval( variable ) )
|