Esempio n. 1
1
def get_sublime_exe():
    '''
    Utility function to get the full path to the currently executing
    Sublime instance.
    '''
    processes = ['subl', 'sublime_text']

    def check_processes(st2_dir=None):
        if st2_dir is None or os.path.exists(st2_dir):
            for process in processes:
                try:
                    if st2_dir is not None:
                        process = os.path.join(st2_dir, process)

                    p = subprocess.Popen(
                        [process, '-v'],
                        stdout=subprocess.PIPE,
                        startupinfo=startupinfo,
                        shell=shell,
                        env=os.environ
                    )
                except:
                    pass
                else:
                    stdout, _ = p.communicate()

                    if p.returncode == 0:
                        m = SUBLIME_VERSION.search(stdout.decode('utf8'))
                        if m and m.group(1) == version:
                            return process
        return None

    platform = sublime.platform()

    plat_settings = get_setting(platform, {})
    sublime_executable = plat_settings.get('sublime_executable', None)

    if sublime_executable:
        return sublime_executable

    # we cache the results of the other checks, if possible
    if hasattr(get_sublime_exe, 'result'):
        return get_sublime_exe.result

    # are we on ST3
    if hasattr(sublime, 'executable_path'):
        get_sublime_exe.result = sublime.executable_path()

        # on osx, the executable does not function the same as subl
        if platform== 'osx':
            get_sublime_exe.result = os.path.normpath(
                os.path.join(
                    os.path.dirname(get_sublime_exe.result),
                    '..',
                    'SharedSupport',
                    'bin',
                    'subl'
                )
            )
    # in ST2 on Windows the Python executable is actually "sublime_text"
    elif platform == 'windows' and sys.executable != 'python' and \
            os.path.isabs(sys.executable):
        get_sublime_exe.result = sys.executable
        return get_sublime_exe.result

    # guess-work for ST2
    version = sublime.version()

    startupinfo = None
    shell = False
    if platform == 'windows':
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        shell = sublime.version() >= '3000'

    # hope its on the path
    result = check_processes()
    if result is not None:
        get_sublime_exe.result = result
        return result

    # guess the default location
    if platform == 'windows':
        st2_dir = os.path.expandvars('%PROGRAMFILES%\\Sublime Text 2')
        result = check_processes(st2_dir)
        if result is not None:
            get_sublime_exe.result = result
            return result
    elif platform == 'linux':
        for path in [
            '$HOME/bin',
            '$HOME/sublime_text_2',
            '$HOME/sublime_text',
            '/opt/sublime_text_2',
            '/opt/sublime_text',
            '/usr/local/bin',
            '/usr/bin'
        ]:
            st2_dir = os.path.expandvars(path)
            result = check_processes(st2_dir)
            if result is not None:
                get_sublime_exe.result = result
                return result
    else:
        st2_dir = '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin'
        result = check_processes(st2_dir)
        if result is not None:
            get_sublime_exe.result = result
            return result
        try:
            p = subprocess.Popen(
                ['mdfind', '"kMDItemCFBundleIdentifier == com.sublimetext.2"'],
                stdout=subprocess.PIPE,
                env=os.environ
            )
        except:
            pass
        else:
            stdout, _ = p.communicate()
            if p.returncode == 0:
                st2_dir = os.path.join(
                    stdout.decode('utf8'),
                    'Contents',
                    'SharedSupport',
                    'bin'
                )
                result = check_processes(st2_dir)
                if result is not None:
                    get_sublime_exe.result = result
                    return result


    print(
        'Cannot determine the path to your Sublime installation. Please ' +
        'set the "sublime_executable" setting in your settings for your platform.'
    )

    return None
Esempio n. 2
0
def focus_st():
    if get_setting('disable_focus_hack', False):
        return

    sublime_command = get_sublime_exe()

    if sublime_command is not None:
        platform = sublime.platform()
        plat_settings = get_setting(platform, {})
        wait_time = plat_settings.get('keep_focus_delay', 0.5)

        # osx is a special snowflake
        if platform == 'osx':
            # sublime_command should be /path/to/Sublime Text.app/Contents/...
            sublime_app = sublime_command.split('/Contents/')[0]

            def keep_focus():
                external_command([
                    'osascript', '-e',
                    'tell application "{0}" to activate'.format(sublime_app)
                ],
                                 use_texpath=False)
        else:

            def keep_focus():
                external_command([sublime_command], use_texpath=False)

        if hasattr(sublime, 'set_async_timeout'):
            sublime.set_async_timeout(keep_focus, int(wait_time * 1000))
        else:
            sublime.set_timeout(keep_focus, int(wait_time * 1000))
Esempio n. 3
0
def focus_st():
    if get_setting('disable_focus_hack', False):
        return

    sublime_command = get_sublime_exe()

    if sublime_command is not None:
        platform = sublime.platform()
        plat_settings = get_setting(platform, {})
        wait_time = plat_settings.get('keep_focus_delay', 0.5)

        # osx is a special snowflake
        if platform == 'osx':
            # sublime_command should be /path/to/Sublime Text.app/Contents/...
            sublime_app = sublime_command.split('/Contents/')[0]

            def keep_focus():
                external_command(
                    [
                        'osascript', '-e',
                        'tell application "{0}" to activate'.format(
                            sublime_app
                        )
                    ],
                    use_texpath=False
                )
        else:
            def keep_focus():
                external_command([sublime_command], use_texpath=False)

        if hasattr(sublime, 'set_async_timeout'):
            sublime.set_async_timeout(keep_focus, int(wait_time * 1000))
        else:
            sublime.set_timeout(keep_focus, int(wait_time * 1000))
Esempio n. 4
0
    def _get_texpath():
        try:
            texpath = get_setting(sublime.platform(), {}).get('texpath')
        except AttributeError:
            # hack to reload this module in case the calling module was
            # reloaded
            exc_info = sys.exc_info
            try:
                reload(sys.modules[get_texpath.__module__])
                texpath = get_setting(sublime.platform(), {}).get('texpath')
            except:
                reraise(*exc_info)

        return expand_vars(texpath) if texpath is not None else None
Esempio n. 5
0
    def _get_texpath():
        try:
            texpath = get_setting(sublime.platform(), {}).get('texpath')
        except AttributeError:
            # hack to reload this module in case the calling module was
            # reloaded
            exc_info = sys.exc_info
            try:
                reload(sys.modules[get_texpath.__module__])
                texpath = get_setting(sublime.platform(), {}).get('texpath')
            except:
                reraise(*exc_info)

        return expand_vars(texpath) if texpath is not None else None
Esempio n. 6
0
def update_dict_language(view, extract_from_root):
    loc = (_get_locale(view)
           or extract_from_root and _get_locale_from_tex_root(view))
    if not loc:
        return  # no spellcheck directive found

    try:
        user_sc = settings.get_setting("tex_spellcheck_paths", {})
        dict_path = user_sc.get(loc) or get_dict_path(loc)
    except DictMissing:
        print("dict definition missing for locale '{0}'".format(loc))
        return  # no dict defined for locale
    current_dict = view.settings().get("dictionary")
    if current_dict == dict_path:
        return  # the locale is already set
    view.settings().set("dictionary", dict_path)
    print("Changed dictionary to '{0}'".format(dict_path))
Esempio n. 7
0
def update_dict_language(view, extract_from_root):
    loc = (_get_locale(view) or
           extract_from_root and _get_locale_from_tex_root(view))
    if not loc:
        return  # no spellcheck directive found

    try:
        user_sc = settings.get_setting("tex_spellcheck_paths", {})
        dict_path = user_sc.get(loc) or get_dict_path(loc)
    except DictMissing:
        print("dict definition missing for locale '{0}'"
              .format(loc))
        return  # no dict defined for locale
    current_dict = view.settings().get("dictionary")
    if current_dict == dict_path:
        return  # the locale is already set
    view.settings().set("dictionary", dict_path)
    print("Changed dictionary to '{0}'".format(dict_path))
Esempio n. 8
0
def get_sublime_exe():
    '''
    Utility function to get the full path to the currently executing
    Sublime instance.
    '''
    processes = ['subl', 'sublime_text']

    def check_processes(st2_dir=None):
        if st2_dir is None or os.path.exists(st2_dir):
            for process in processes:
                try:
                    if st2_dir is not None:
                        process = os.path.join(st2_dir, process)

                    m = SUBLIME_VERSION.search(
                        check_output([process, '-v'], use_texpath=False))
                    if m and m.group(1) == version:
                        return process
                except:
                    pass

        return None

    platform = sublime.platform()

    plat_settings = get_setting(platform, {})
    sublime_executable = plat_settings.get('sublime_executable', None)

    if sublime_executable:
        return sublime_executable

    # we cache the results of the other checks, if possible
    if hasattr(get_sublime_exe, 'result'):
        return get_sublime_exe.result

    # are we on ST3
    if hasattr(sublime, 'executable_path'):
        get_sublime_exe.result = sublime.executable_path()
        # on osx, the executable does not function the same as subl
        if platform == 'osx':
            get_sublime_exe.result = os.path.normpath(
                os.path.join(os.path.dirname(get_sublime_exe.result), '..',
                             'SharedSupport', 'bin', 'subl'))
        # on linux, it is preferable to use subl if it points to the
        # correct version see issue #710 for a case where this is useful
        elif (platform == 'linux'
              and not get_sublime_exe.result.endswith('subl')):
            subl = which('subl')
            if subl is not None:
                try:
                    m = SUBLIME_VERSION.search(
                        check_output([subl, '-v'], use_texpath=False))

                    if m and m.group(1) == sublime.version():
                        get_sublime_exe.result = subl
                except:
                    pass

        return get_sublime_exe.result
    # in ST2 on Windows the Python executable is actually "sublime_text"
    elif platform == 'windows' and sys.executable != 'python' and \
            os.path.isabs(sys.executable):
        get_sublime_exe.result = sys.executable
        return get_sublime_exe.result

    # guess-work for ST2
    version = sublime.version()

    # hope its on the path
    result = check_processes()
    if result is not None:
        get_sublime_exe.result = result
        return result

    # guess the default location
    if platform == 'windows':
        st2_dir = os.path.expandvars('%PROGRAMFILES%\\Sublime Text 2')
        result = check_processes(st2_dir)
        if result is not None:
            get_sublime_exe.result = result
            return result
    elif platform == 'linux':
        for path in [
                '$HOME/bin', '$HOME/sublime_text_2', '$HOME/sublime_text',
                '/opt/sublime_text_2', '/opt/sublime_text', '/usr/local/bin',
                '/usr/bin'
        ]:
            st2_dir = os.path.expandvars(path)
            result = check_processes(st2_dir)
            if result is not None:
                get_sublime_exe.result = result
                return result
    else:
        st2_dir = '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin'
        result = check_processes(st2_dir)
        if result is not None:
            get_sublime_exe.result = result
            return result
        try:
            folder = check_output(
                ['mdfind', '"kMDItemCFBundleIdentifier == com.sublimetext.2"'],
                use_texpath=False)

            st2_dir = os.path.join(folder, 'Contents', 'SharedSupport', 'bin')
            result = check_processes(st2_dir)
            if result is not None:
                get_sublime_exe.result = result
                return result
        except:
            pass

    print('Cannot determine the path to your Sublime installation. Please '
          'set the "sublime_executable" setting in your settings for your '
          'platform.')

    return None
Esempio n. 9
0
def get_sublime_exe():
    '''
    Utility function to get the full path to the currently executing
    Sublime instance.
    '''
    processes = ['subl', 'sublime_text']

    def check_processes(st2_dir=None):
        if st2_dir is None or os.path.exists(st2_dir):
            for process in processes:
                try:
                    if st2_dir is not None:
                        process = os.path.join(st2_dir, process)

                    p = subprocess.Popen([process, '-v'],
                                         stdout=subprocess.PIPE,
                                         startupinfo=startupinfo,
                                         shell=shell,
                                         env=os.environ)
                except:
                    pass
                else:
                    stdout, _ = p.communicate()

                    if p.returncode == 0:
                        m = SUBLIME_VERSION.search(stdout.decode('utf8'))
                        if m and m.group(1) == version:
                            return process
        return None

    platform = sublime.platform()

    plat_settings = get_setting(platform, {})
    sublime_executable = plat_settings.get('sublime_executable', None)

    if sublime_executable:
        return sublime_executable

    # we cache the results of the other checks, if possible
    if hasattr(get_sublime_exe, 'result'):
        return get_sublime_exe.result

    # are we on ST3
    if hasattr(sublime, 'executable_path'):
        get_sublime_exe.result = sublime.executable_path()

        # on osx, the executable does not function the same as subl
        if platform == 'osx':
            get_sublime_exe.result = os.path.normpath(
                os.path.join(os.path.dirname(get_sublime_exe.result), '..',
                             'SharedSupport', 'bin', 'subl'))

        return get_sublime_exe.result
    # in ST2 on Windows the Python executable is actually "sublime_text"
    elif platform == 'windows' and sys.executable != 'python' and \
            os.path.isabs(sys.executable):
        get_sublime_exe.result = sys.executable
        return get_sublime_exe.result

    # guess-work for ST2
    version = sublime.version()

    startupinfo = None
    shell = False
    if platform == 'windows':
        startupinfo = subprocess.STARTUPINFO()
        startupinfo.dwFlags |= subprocess.STARTF_USESHOWWINDOW
        shell = sublime.version() >= '3000'

    # hope its on the path
    result = check_processes()
    if result is not None:
        get_sublime_exe.result = result
        return result

    # guess the default location
    if platform == 'windows':
        st2_dir = os.path.expandvars('%PROGRAMFILES%\\Sublime Text 2')
        result = check_processes(st2_dir)
        if result is not None:
            get_sublime_exe.result = result
            return result
    elif platform == 'linux':
        for path in [
                '$HOME/bin', '$HOME/sublime_text_2', '$HOME/sublime_text',
                '/opt/sublime_text_2', '/opt/sublime_text', '/usr/local/bin',
                '/usr/bin'
        ]:
            st2_dir = os.path.expandvars(path)
            result = check_processes(st2_dir)
            if result is not None:
                get_sublime_exe.result = result
                return result
    else:
        st2_dir = '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin'
        result = check_processes(st2_dir)
        if result is not None:
            get_sublime_exe.result = result
            return result
        try:
            p = subprocess.Popen(
                ['mdfind', '"kMDItemCFBundleIdentifier == com.sublimetext.2"'],
                stdout=subprocess.PIPE,
                env=os.environ)
        except:
            pass
        else:
            stdout, _ = p.communicate()
            if p.returncode == 0:
                st2_dir = os.path.join(stdout.decode('utf8'), 'Contents',
                                       'SharedSupport', 'bin')
                result = check_processes(st2_dir)
                if result is not None:
                    get_sublime_exe.result = result
                    return result

    print(
        'Cannot determine the path to your Sublime installation. Please ' +
        'set the "sublime_executable" setting in your settings for your platform.'
    )

    return None
Esempio n. 10
0
def get_sublime_exe():
    '''
    Utility function to get the full path to the currently executing
    Sublime instance.
    '''
    processes = ['subl', 'sublime_text']

    def check_processes(st2_dir=None):
        if st2_dir is None or os.path.exists(st2_dir):
            for process in processes:
                try:
                    if st2_dir is not None:
                        process = os.path.join(st2_dir, process)

                    m = SUBLIME_VERSION.search(check_output(
                        [process, '-v'],
                        use_texpath=False
                    ))
                    if m and m.group(1) == version:
                        return process
                except:
                    pass

        return None

    platform = sublime.platform()

    plat_settings = get_setting(platform, {})
    sublime_executable = plat_settings.get('sublime_executable', None)

    if sublime_executable:
        return sublime_executable

    # we cache the results of the other checks, if possible
    if hasattr(get_sublime_exe, 'result'):
        return get_sublime_exe.result

    # are we on ST3
    if hasattr(sublime, 'executable_path'):
        get_sublime_exe.result = sublime.executable_path()
        # on osx, the executable does not function the same as subl
        if platform == 'osx':
            get_sublime_exe.result = os.path.normpath(
                os.path.join(
                    os.path.dirname(get_sublime_exe.result),
                    '..',
                    'SharedSupport',
                    'bin',
                    'subl'
                )
            )
        # on linux, it is preferable to use subl if it points to the
        # correct version see issue #710 for a case where this is useful
        elif (
            platform == 'linux' and
            not get_sublime_exe.result.endswith('subl')
        ):
            subl = which('subl')
            if subl is not None:
                try:
                    m = SUBLIME_VERSION.search(check_output(
                        [subl, '-v'],
                        use_texpath=False
                    ))

                    if m and m.group(1) == sublime.version():
                        get_sublime_exe.result = subl
                except:
                    pass

        return get_sublime_exe.result
    # in ST2 on Windows the Python executable is actually "sublime_text"
    elif platform == 'windows' and sys.executable != 'python' and \
            os.path.isabs(sys.executable):
        get_sublime_exe.result = sys.executable
        return get_sublime_exe.result

    # guess-work for ST2
    version = sublime.version()

    # hope its on the path
    result = check_processes()
    if result is not None:
        get_sublime_exe.result = result
        return result

    # guess the default location
    if platform == 'windows':
        st2_dir = os.path.expandvars('%PROGRAMFILES%\\Sublime Text 2')
        result = check_processes(st2_dir)
        if result is not None:
            get_sublime_exe.result = result
            return result
    elif platform == 'linux':
        for path in [
            '$HOME/bin',
            '$HOME/sublime_text_2',
            '$HOME/sublime_text',
            '/opt/sublime_text_2',
            '/opt/sublime_text',
            '/usr/local/bin',
            '/usr/bin'
        ]:
            st2_dir = os.path.expandvars(path)
            result = check_processes(st2_dir)
            if result is not None:
                get_sublime_exe.result = result
                return result
    else:
        st2_dir = '/Applications/Sublime Text 2.app/Contents/SharedSupport/bin'
        result = check_processes(st2_dir)
        if result is not None:
            get_sublime_exe.result = result
            return result
        try:
            folder = check_output(
                ['mdfind', '"kMDItemCFBundleIdentifier == com.sublimetext.2"'],
                use_texpath=False
            )

            st2_dir = os.path.join(folder, 'Contents', 'SharedSupport', 'bin')
            result = check_processes(st2_dir)
            if result is not None:
                    get_sublime_exe.result = result
                    return result
        except:
            pass

    print(
        'Cannot determine the path to your Sublime installation. Please '
        'set the "sublime_executable" setting in your settings for your '
        'platform.'
    )

    return None