Esempio n. 1
0
func (ch *commandHandler) Register(name string, cmd interface{}) error {
	var r = false
	log.Finest("Want to register %s", name)
	if ac, ok := cmd.(ApplicationCommand); ok {
		if _, ok := ch.ApplicationCommands[name]; ok {
			return fmt.Errorf("%s is already a registered command", name)
		}
		r = true
		ch.ApplicationCommands[name] = ac
	}
	if wc, ok := cmd.(WindowCommand); ok {
		if _, ok := ch.WindowCommands[name]; ok {
			return fmt.Errorf("%s is already a registered command", name)
		}
		r = true
		ch.WindowCommands[name] = wc
	}
	if tc, ok := cmd.(TextCommand); ok {
		if _, ok := ch.TextCommands[name]; ok {
			return fmt.Errorf("%s is already a registered command", name)
		}
		r = true
		ch.TextCommands[name] = tc
	}
	if !r {
		return fmt.Errorf("Command wasn't registered in any list: %s", name)
	} else if ch.verbose {
		log.Finest("Successfully registered command %s", name)
	}
	return nil
}
Esempio n. 2
0
// Returns packet file data if any error occurred
// on reading file we will return nil
func (p *Packet) Get() interface{} {
	e := []byte(`{}`)
	if p.group() == "keymap" {
		e = []byte(`[]`)
	}

	if _, err := os.Stat(p.path); os.IsNotExist(err) {
		log.Finest("%s doesn't exist yet", p.path)
		return e
	}

	d, err := ioutil.ReadFile(p.path)
	if err != nil {
		log.Errorf("Couldn't read file: %s", err)
		return e
	}
	return d
}
Esempio n. 3
0
func (v *View) FileChanged(filename string) {
	log.Finest("Reloading %s", filename)

	if saving, ok := v.Settings().Get("lime.saving", false).(bool); ok && saving {
		// This reload was triggered by ourselves saving to this file, so don't reload it
		return
	}
	if !GetEditor().Frontend().OkCancelDialog("File was changed by another program, reload?", "reload") {
		return
	}

	if d, err := ioutil.ReadFile(filename); err != nil {
		log.Error("Could not read file: %s\n. Error was: %v", filename, err)
	} else {
		edit := v.BeginEdit()
		end := v.buffer.Size()
		v.Replace(edit, Region{0, end}, string(d))
		v.EndEdit(edit)
	}
}
Esempio n. 4
0
func (w *Watcher) UnWatch(name string, cb interface{}) error {
	log.Finest("UnWatch(%s)", name)
	w.lock.Lock()
	defer w.lock.Unlock()
	if cb == nil {
		return w.unWatch(name)
	}
	for i, c := range w.watched[name] {
		if c == cb {
			w.watched[name][i] = w.watched[name][len(w.watched[name])-1]
			w.watched[name][len(w.watched[name])-1] = nil
			w.watched[name] = w.watched[name][:len(w.watched[name])-1]
			break
		}
	}
	if len(w.watched[name]) == 0 {
		w.unWatch(name)
	}
	return nil
}
Esempio n. 5
0
func (w *Watcher) Watch(name string, cb interface{}) error {
	log.Finest("Watch(%s)", name)
	fi, err := os.Stat(name)
	isDir := err == nil && fi.IsDir()
	// If the file doesn't exist currently we will add watcher for file
	// directory and look for create event inside the directory
	if os.IsNotExist(err) {
		log.Fine("%s doesn't exist, Watching parent directory", name)
		if err := w.Watch(filepath.Dir(name), nil); err != nil {
			return err
		}
	}
	w.lock.Lock()
	defer w.lock.Unlock()
	if err := w.add(name, cb); err != nil {
		if !isDir {
			return err
		}
		if exist(w.dirs, name) {
			log.Debug("%s is watched already", name)
			return nil
		}
	}
	// If exists in watchers we are already watching the path
	// Or
	// If the file is under one of watched dirs
	//
	// no need to create watcher
	if exist(w.watchers, name) || (!isDir && exist(w.dirs, filepath.Dir(name))) {
		return nil
	}
	if err := w.watch(name); err != nil {
		return err
	}
	if isDir {
		w.flushDir(name)
	}
	return nil
}
Esempio n. 6
0
// Trigger this ViewEvent by calling all the registered callbacks in order of registration.
func (ve *ViewEvent) Call(v *View) {
	log.Finest("%s(%v)", evNames[ve], v.Id())
	for _, ev := range *ve {
		ev(v)
	}
}
Esempio n. 7
0
// Trigger this WindowEvent by calling all the registered callbacks in order of registration.
func (we *WindowEvent) Call(w *Window) {
	log.Finest("%s(%v)", wevNames[we], w.Id())
	for _, ev := range *we {
		ev(w)
	}
}
Esempio n. 8
0
func (c *TextCommandGlue) Run(v *backend.View, e *backend.Edit) error {
	l := py.NewLock()
	defer l.Unlock()

	p0 := util.Prof.Enter("tc.run")
	defer p0.Exit()
	var (
		pyv, pye, pyargs, obj py.Object
		err                   error
	)
	if pyv, err = toPython(v); err != nil {
		return pyError(err)
	}
	defer pyv.Decref()

	if pye, err = toPython(e); err != nil {
		return pyError(err)
	}
	defer pye.Decref()

	if pyargs, err = c.CreatePyArgs(c.args); err != nil {
		return pyError(err)
	}
	defer pyargs.Decref()

	init := util.Prof.Enter("tc.init")
	if obj, err = c.inner.Base().CallFunctionObjArgs(pyv); err != nil {
		return pyError(err)
	}
	defer obj.Decref()
	init.Exit()
	// interrupt := true
	// defer func() { interrupt = false }()
	// go func() {
	// 	<-time.After(time.Second * 5)
	// 	if interrupt {
	// 		py.SetInterrupt()
	// 	}
	// }()
	exec := util.Prof.Enter("tc.exec")
	defer exec.Exit()
	if obj.Base().HasAttrString("run_") {
		// The plugin is probably trying to bypass the undostack...
		old := v.IsScratch()
		v.SetScratch(true)
		log.Finest("Discarded: %s", e)
		v.EndEdit(e)
		v.SetScratch(old)
		ret, err := obj.Base().CallMethodObjArgs("run_", pye, pyargs)
		if ret != nil {
			ret.Decref()
		}
		if err != nil {
			return pyError(err)
		}
		return nil
	}
	ret, err := obj.Base().CallMethodObjArgs("run__", pye, pyargs)
	if ret != nil {
		ret.Decref()
	}
	if err != nil {
		return pyError(err)
	}
	return nil
}
Esempio n. 9
0
func (e *Editor) inputthread() {
	pc := 0
	var lastBindings keys.KeyBindings
	doinput := func(kp keys.KeyPress) {
		defer func() {
			if r := recover(); r != nil {
				log.Error("Panic in inputthread: %v\n%s", r, string(debug.Stack()))
				if pc > 0 {
					panic(r)
				}
				pc++
			}
		}()
		p := Prof.Enter("hi")
		defer p.Exit()

		lvl := log.FINE
		if e.logInput {
			lvl++
		}
		log.Logf(lvl, "Key: %v", kp)
		if lastBindings.SeqIndex() == 0 {
			lastBindings = *e.KeyBindings()
		}
	try_again:
		possible_actions := lastBindings.Filter(kp)
		lastBindings = possible_actions

		// TODO?
		var (
			wnd *Window
			v   *View
		)
		if wnd = e.ActiveWindow(); wnd != nil {
			v = wnd.ActiveView()
		}

		qc := func(key string, operator Op, operand interface{}, match_all bool) bool {
			return OnQueryContext.Call(v, key, operator, operand, match_all) == True
		}

		if action := possible_actions.Action(qc); action != nil {
			p2 := Prof.Enter("hi.perform")
			e.RunCommand(action.Command, action.Args)
			p2.Exit()
		} else if possible_actions.SeqIndex() > 1 {
			// TODO: this disables having keyBindings with more than 2 key sequence
			lastBindings = *e.KeyBindings()
			goto try_again
		} else if kp.IsCharacter() {
			p2 := Prof.Enter("hi.character")
			log.Finest("[editor.inputthread] kp: |%s|, pos: %v", kp.Text, possible_actions)
			if err := e.CommandHandler().RunTextCommand(v, "insert", Args{"characters": kp.Text}); err != nil {
				log.Debug("Couldn't run textcommand: %s", err)
			}
			p2.Exit()
		}
	}
	for kp := range e.keyInput {
		doinput(kp)
	}
}