Replacing words with greek letters in SublimeText

空扰寡人 提交于 2019-12-25 10:53:08

问题


I'm trying to create a snippet in SublimeText which replaces words I type with greek letters after pressing ctrl+g.

e.g.: sigma -> ctrl+g -> σ

I could make a snippet for each letter, but I have the feeling it should be easier. I don't want to scan through the whole document, only the word the cursor is currently at.


回答1:


I was looking for a similar thing and just stumbled over the Package "Greek Letters" by Arne Ludwig (for ST2/ST3):

https://packagecontrol.io/packages/Greek%20Letters

It works by autocompleting LaTeX names for Greek letters. Autocompletion is usually configured with the tab-key, e.g:

tau<tab>
varphi<tab>
phi<tab> 



回答2:


You could do it with a plugin. Something like the following will work for a single cursor position.

import sublime_plugin


class GreekSubstitution(sublime_plugin.TextCommand):
    greek_map = {}
    greek_map["alpha"] = "α"
    greek_map["beta"] = "ß"
    greek_map["gamma"] = "Γ"

    def run(self, edit):
        view = self.view
        cursors = view.sel()
        cursor = cursors[0]

        word_region = view.word(cursor)
        word = view.substr(word_region)
        if word in self.greek_map:
            view.replace(edit, word_region, self.greek_map[word])

The command you would bind to is greek_substitution. Obviously you would need to expand the list beyond alpha, beta, and gamma, but this should get you started in the right direction.




回答3:


I can't comment so I'll just post another reply:

Isn't self.greek_map(word) a syntax error? I'm assuming it's a simple typo.

I'm not sure if it's in the spirit of this forum, but I took the liberty of submitting a complete solution, based on the preceding reply.

import sublime, sublime_plugin
import unicodedata

# NOTE: Unicode uses the spelling 'lamda'

class InsertSpecialSymbol(sublime_plugin.TextCommand):

    # Configuration
    DEBUG = True

    # Symbol sets
    Greek = { unicodedata.name(L).split()[-1].lower() : L for L in map(chr, range(945, 970)) }
    Math = { 'multiply': '×', 'forall': '∀', 'element': '∈', 'angle': '∠', 'proportional': '∝', 'le': '≤', 'ge': '≥' }
    replacements = Greek.copy()
    replacements.update(Math)


    def debug(self, string):
        if InsertSpecialSymbol.DEBUG: print(string)

    def run(self, edit):
        ''' Replaces the selected word with an appropriate symbol '''
        view = self.view
        for cursor in view.sel():
            wordRegion = view.word(cursor)
            word = view.substr(wordRegion)
            symbol = self.replacements.get(word.lower(), None)

            if symbol is not None:
                view.replace(edit, wordRegion, symbol if word[0].islower() else symbol.upper())
            else:
                self.debug('Substitution not available for \'%s\'.' % word)

Just as an aside, since creating the map manually would've been such a pain, I'd like to point out that it was done automatically (cf. unicodedata.name) with a short Python-snippet. I'll post it upon request.

EDIT The example assumes you're using Sublime Text 3, whose API is not backwards compatible. If your editor relies on Python 2, I recommend James' answer below.



来源:https://stackoverflow.com/questions/21290849/replacing-words-with-greek-letters-in-sublimetext

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!