2012-05-19 19:56:13 +00:00
|
|
|
|
"""
|
|
|
|
|
Alias plugin.
|
|
|
|
|
|
|
|
|
|
Allows the creation and the removal of personal aliases.
|
|
|
|
|
"""
|
|
|
|
|
|
2011-10-02 11:22:13 +00:00
|
|
|
|
from plugin import BasePlugin
|
2011-11-18 22:35:28 +00:00
|
|
|
|
import common
|
2013-03-08 18:39:34 +00:00
|
|
|
|
from common import shell_split
|
2011-10-02 11:22:13 +00:00
|
|
|
|
|
|
|
|
|
class Plugin(BasePlugin):
|
|
|
|
|
def init(self):
|
2013-03-08 21:53:35 +00:00
|
|
|
|
self.api.add_command('alias', self.command_alias,
|
2013-03-01 18:25:31 +00:00
|
|
|
|
usage='<alias> <command> [args]',
|
|
|
|
|
short='Create an alias command',
|
|
|
|
|
help='Create an alias for <command> with [args].')
|
2013-03-08 21:53:35 +00:00
|
|
|
|
self.api.add_command('unalias', self.command_unalias,
|
2013-03-01 18:25:31 +00:00
|
|
|
|
usage='<alias>',
|
|
|
|
|
help='Remove a previously created alias',
|
|
|
|
|
short='Remove an alias',
|
|
|
|
|
completion=self.completion_unalias)
|
2011-10-02 11:22:13 +00:00
|
|
|
|
self.commands = {}
|
|
|
|
|
|
|
|
|
|
def command_alias(self, line):
|
2012-05-19 19:56:13 +00:00
|
|
|
|
"""
|
|
|
|
|
/alias <alias> <command> [args]
|
|
|
|
|
"""
|
2011-11-18 22:35:28 +00:00
|
|
|
|
arg = common.shell_split(line)
|
2011-10-02 11:22:13 +00:00
|
|
|
|
if len(arg) < 2:
|
2013-03-08 18:39:34 +00:00
|
|
|
|
self.api.information('Alias: Not enough parameters', 'Error')
|
2011-10-02 11:22:13 +00:00
|
|
|
|
return
|
|
|
|
|
alias = arg[0]
|
2012-05-19 19:56:13 +00:00
|
|
|
|
command = arg[1]
|
|
|
|
|
tmp_args = arg[2] if len(arg) > 2 else ''
|
2011-10-02 11:22:13 +00:00
|
|
|
|
|
|
|
|
|
if alias in self.core.commands or alias in self.commands:
|
2013-03-08 18:39:34 +00:00
|
|
|
|
self.api.information('Alias: command already exists', 'Error')
|
2011-10-02 11:22:13 +00:00
|
|
|
|
return
|
2013-03-08 21:53:35 +00:00
|
|
|
|
|
|
|
|
|
self.commands[alias] = lambda arg: self.get_command(command)(tmp_args + arg)
|
|
|
|
|
self.api.add_command(alias, self.commands[alias], 'This command is an alias for /%s %s' %( command, tmp_args))
|
2013-03-08 18:39:34 +00:00
|
|
|
|
self.api.information('Alias /%s successfuly created' % alias, 'Info')
|
2011-10-02 11:22:13 +00:00
|
|
|
|
|
|
|
|
|
def command_unalias(self, alias):
|
2012-05-19 19:56:13 +00:00
|
|
|
|
"""
|
|
|
|
|
/unalias <existing alias>
|
|
|
|
|
"""
|
2011-10-02 11:22:13 +00:00
|
|
|
|
if alias in self.commands:
|
|
|
|
|
del self.commands[alias]
|
2013-03-08 21:53:35 +00:00
|
|
|
|
self.api.del_command(alias)
|
2013-03-08 18:39:34 +00:00
|
|
|
|
self.api.information('Alias /%s successfuly deleted' % alias, 'Info')
|
2011-10-02 11:22:13 +00:00
|
|
|
|
|
2013-03-01 18:25:31 +00:00
|
|
|
|
def completion_unalias(self, the_input):
|
|
|
|
|
aliases = [alias for alias in self.commands]
|
|
|
|
|
aliases.sort()
|
|
|
|
|
return the_input.auto_completion(aliases, '', quotify=False)
|
|
|
|
|
|
2011-10-02 11:22:13 +00:00
|
|
|
|
def get_command(self, name):
|
|
|
|
|
"""Returns the function associated with a command"""
|
|
|
|
|
def dummy(args):
|
|
|
|
|
"""Dummy function called if the command doesn’t exist"""
|
|
|
|
|
pass
|
|
|
|
|
if name in self.core.commands:
|
|
|
|
|
return self.core.commands[name][0]
|
2013-03-08 18:39:34 +00:00
|
|
|
|
elif name in self.api.current_tab().commands:
|
|
|
|
|
return self.api.current_tab().commands[name][0]
|
2011-10-02 11:22:13 +00:00
|
|
|
|
return dummy
|