Work at SourceForge, help us to make it a better place! We have an immediate need for a Support Technician in our San Francisco or Denver office.

Close

MS Word-style case change (Shift+F3 feature): change the case of selected text from upper to lower to title

Abbas
2013-08-11
2013-08-20
  • Abbas
    Abbas
    2013-08-11

    :::python

    # MS Word has the option of changing the case of selected text by pressing Shift+F3
    # The case changes in the order: UPPER CASE, lower case, Title Case
    # It was something I was missing in Notepad++ so I wrote the following script to add the change case feature
    # Remember to associate this script with Shift+F3 key combination in the Shortcut Mapper
    
    text = editor.getSelText()
    
    if text: # Do this only if some text has been selected
        checkingFuncs = [lambda s: s.isupper(), lambda s: s.islower(),  lambda s: s.istitle(),  lambda s: True]
        alteringFuncs = [lambda s: s.lower(),   lambda s: s.title(),        lambda s: s.upper(),        lambda s: s.upper()]
        numOfFuncs = len(checkingFuncs)
    
        for i in range(0, numOfFuncs): # Iterate through the list of checking functions
            if checkingFuncs[i](text): # Check the existing case of the text
                text = alteringFuncs[i](text) # Based on the result of checking function, change the case in this order: upper->lower, lower->title, title->upper, anything else->upper
                break
    
        start = editor.getSelectionStart() # Record the start and end position of the selection
        end =   editor.getSelectionEnd()
    
        editor.replaceSel(text) # Replace the originally selected text with altered-case text, this will end the selection as well
    
        editor.setSel(start, end) # Select the newly altered text