Sublime Forum

Modified package's hotkeys not working

#1

okay. for years i use the package AppendSemicolon from MauriceZ. love it. i am now doing a ton of JS work and wanted to modify it to append commas instead. the Python code is very straightforward, made the changes, and updated the keymaps to suit. zipped and renamed package appropriately and dropped it into the Installed Packages path to test.

nothing works. i’ve literally made three changes to the base python code, changed the keymap from ; to , and it flat out won’t fire… what could i be missing? you can peep the code following MauriceZ’s link above, all i did was replace semicolon with comma accordingly (a no-brainer, i thought…). even in the keymaps. so why won’t this fire?

when i open ST again, it populates the menu but when i go to check the default keymap, it is blank… WTH?

0 Likes

#2

Do you see any error messages in the Sublime console?

0 Likes

#3

nope. no errors at all. not when loading, not when firing. it’s really odd… :confused:

0 Likes

#4

Can you run me through the steps you used in order to make the changes you’re trying to make? There may be something related to that which is kicking you in the butt, but exactly what is hard to say exactly.

0 Likes

#5

frustrating trip to hospital later…

here’s what i did:

  1. change extension and unzip package
  2. rename similarly named files and folder
  3. open all files in ST and replace names in each to reflect the new functionality:
    3.1 every instance of semicolon replaced with comma
  4. update keymap file to reflect intended use: CTRL+,
  5. zip folder and change extension
  6. quit and reload SublimeText
  7. see that plugin has loaded
  8. attempt to use hotkey and FAIL
  9. check console: no errors.
  10. stew in frustration

if you need more details, let me know, i can get as granular as you’d like.

0 Likes

#6

The only time it’s ever safe to take the step of renaming a sublime-package to a zip file, extract it, modify contents and put it back where you got it is if you also manually created it and put it in place initially yourself.

Or, put another way, directly modifying a sublime-package file that either ships with Sublime or is installed by Package Control is a slow cooking recipe for having all your changes summarily discarded and lost forever without any prompt or warning (because you’re not supposed to do that).

That may or may not relate to your issue here; the appropriate way to do something like this would be to create an override on the package resource, which safely applies the change in a way that won’t get wiped without warning.

Easiest steps to do what you want would be the following (you may want to use Package Control to remove and then replace the AppendSemiColon package to make sure everything is pristine).:

  1. Install OverrideAudit
  2. OverrideAudit: Create Override from the command palette; choose AppendSemiColon as the package and the appropriate keymap for your OS from the list.
  3. Modify the keymap to use , in place of ; in the two key bindings, then save the file
  4. Repeat step 2, but choose AppendSemiColon.py as the file
  5. Modify lines 7 and 10 to replace the ; with , instead, then save the file

Now the key inserts commas at the ends of lines; no need to quit or restart or anything like that:

com-gif-maker

0 Likes

#7

before i do all that, it’s important to understand that i still want and need to use the original package. it seems to me that the Override will REPLACE it, rather than allow it to run side-by-side. although if the override is applied ONLY at the time of the keystrikes, then, yeah, that would do what i need.

also. i didn’t install AppendSemiColon from Package Control. i downloaded and installed it manually, since you are allowed to manually install your own stuff (and keep it from the update wipe) in the User folder. so your first paragraph has be a bit befuddled…

and finally, a big Thank You for working with me on this. :slight_smile:

0 Likes

#8

When you say User folder here, do you mean the configuration area of Sublime in general or your User package in particular (Preferences: Browse Packages, and it’s a folder that appears in the list).

sublime-package files aren’t loaded from that location, only from inside of Installed Packages or a subdirectory inside of it; perhaps that was your problem initially?

In any case, indeed the override alters the package entirely; if your goal is to do both then you need a different solution. Combining that with your desire to not have it be automatically updated (which, given the last update date was 7 years ago, is probably not forthcoming anyway) then the most expedient solution would probably be to just use the plugin directly and avoid using it as a package.

The package mentality is more for a “I want this piece of functionality and I don’t want to have to install it and keep it updated myself” kind of workflow, which is not really what you’re after here. You want a version of this particular functionality custom tuned to your own liking, for which you’re willing to install it manually and perform updates manually.

In that case:

import sublime_plugin


class AppendCharacterCommand(sublime_plugin.TextCommand):
    """
    Append the provided character to the end of the current line if it's not
    already there, possibly also entering a newline as well.
    """
    def run(self, edit, character=';', enter_new_line=False):
        def insert_character(point):
            self.view.insert(edit, point, character)

        def is_character(point):
            return self.view.substr(point) == character

        for region in self.view.sel():
            line = self.view.line(region)
            line_begin = line.begin()
            line_end = line.end()

            # go to the first character from the end that isn't whitespace
            while(self.view.substr(line_end - 1).isspace() and line_end != line_begin):
                line_end -= 1

            if line_end == line_begin:
                continue

            if self.view.match_selector(line_end - 1, 'comment'):
                point = self.view.extract_scope(line_end - 1).a - 1

                if point < line_begin:
                    continue

                # go to the first character before the comment that isn't whitespace
                while(self.view.substr(point).isspace() and point != line_begin):
                    point -= 1

                if not self.view.substr(point).isspace() and not is_character(point):
                    insert_character(point + 1)

            elif not is_character(line_end - 1):
                insert_character(line_end)

        if enter_new_line == True:
            self.view.run_command("run_macro_file", {"file": "Packages/Default/Add Line.sublime-macro"})

Store that in your User package (how you get there outlined above) as any .py file you like, then add some custom key bindings:

  { "keys": ["ctrl+;"], "command": "append_character", "args": {"character": ";"} }, 
  { "keys": ["ctrl+shift+;"], "command": "append_character", "args": {"character": ";", "enter_new_line": true} },

  { "keys": ["ctrl+,"], "command": "append_character", "args": {"character": ","} }, 
  { "keys": ["ctrl+shift+,"], "command": "append_character", "args": {"character": ",", "enter_new_line": true} },	

This is based on the version from the package, but cleaned up a bit (hence the name is also changed to make sure that older key bindings won’t trigger it). It takes as an argument the character to be added, so you can use it with any characters you like.

2 Likes

#9

brilliant. thank you for working through this with me. i thought this would be a simple thing… nope! :slight_smile:

have a wonderful week. i’ll tool this up and give it a go. i’ll post results here in a couple days.

0 Likes