Sublime Forum

Events are not triggered

#1

Hi there,

I am playing with events, created the following simple plugin:

import sublime, sublime_plugin

class EventDump(sublime_plugin.EventListener):
    def onNew(view):
        print "new file"
        view.insert(edit, 0, "Hello, World!")
	def on_activated(view):
		print "got focus"
	def on_selection_modified(view):
		print "on_selection_modified"

Saved it here ~/Library/Application Support/Sublime Text 2/Packages/EventDump/eventDump.py

But I don’t see any relevant output on the console. Looks like events are not triggered.

0 Likes

#2

I see that you are new to Python, and you are new to the ST2 API. Hopefully this will clear some things up for you.

This works:

[code]import sublime, sublime_plugin

class EventDump(sublime_plugin.EventListener):
def on_new(self, view):
print “new file”
edit = view.begin_edit()
view.insert(edit, 0, “Hello, World!”)
view.end_edit(edit)

def on_activated(self, view):
    print "got focus"

def on_selection_modified(self, view):
    print "on_selection_modified"[/code]
0 Likes

#3

Both true :smile:

Following your advise, I changed my code into:

import sublime, sublime_plugin

class EventDump(sublime_plugin.EventListener):
    def onNew(self, view):
        print "new file"
        edit = view.begin_edit()
        view.insert(edit, 0, "Hello, World!")
        view.end_edit(edit)

By clicking ctrl+N (new buffer) - I should see “Hello, World!”, right? But I don’t see anything.
Should the “print” actually print something to the console?

0 Likes

#4

There are some subtleties you are missing.

The event is called on_new not onNew. If you copy exactly what I posted, you will see my code works.

0 Likes

#5

Sublime Text 2 api
sublimetext.com/docs/2/api_reference.html

0 Likes