# Copyright (C) 2009, 2010 Roman Zimbelmann # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program. If not, see . ''' This is the default file for command definitions. Each command is a subclass of `Command'. Several methods are defined to interface with the console: execute: call this method when the command is executed. tab: call this method when tab is pressed. quick: call this method after each keypress in the QuickCommandConsole. The return values for tab() can be either: None: There is no tab completion A string: Change the console to this string A list/tuple/generator: cycle through every item in it The return value for quick() can be: False: Nothing happens True: Execute the command afterwards The return value for execute() doesn't matter. If you want to add custom commands, you can create a file ~/.ranger/commands.py, add the line: from ranger.api.commands import * and write some command definitions, for example: class tabnew(Command): def execute(self): self.fm.tab_new() class tabgo(Command): """ :tabgo Go to the nth tab. """ def execute(self): num = self.line.split()[1] self.fm.tab_open(int(num)) For a list of all actions, check /ranger/core/actions.py. ''' from ranger.api.commands import * alias('e', 'edit') alias('q', 'quit') alias('q!', 'quit!') alias('qall', 'quit!') class cd(Command): """ :cd The cd command changes the directory. The command 'cd -' is equivalent to typing ``. In the quick console, the directory will be entered without the need to press enter, as soon as there is one unambiguous match. """ def execute(self): line = parse(self.line) try: destination = line.rest(1) except IndexError: destination = '~' if destination == '-': self.fm.enter_bookmark('`') else: self.fm.cd(destination) def tab(self): return self._tab_only_directories() def quick(self): from os.path import isdir, join, normpath line = parse(self.line) cwd = self.fm.env.cwd.path rel_dest = line.rest(1) if not rel_dest: return False abs_dest = normpath(join(cwd, rel_dest)) return rel_dest != '.' and isdir(abs_dest) class find(Command): """ :find The find command will attempt to find a partial, case insensitive match in the filenames of the current directory. In the quick command console, once there is one unambiguous match, the file will be run automatically. """ count = 0 tab = Command._tab_directory_content def execute(self): if self.mode != cmode.COMMAND_QUICK: self._search() import re search = parse(self.line).rest(1) search = re.escape(search) self.fm.env.last_search = re.compile(search, re.IGNORECASE) self.fm.search_method = 'search' if self.count == 1: self.fm.move(right=1) self.fm.block_input(0.5) def quick(self): self._search() if self.count == 1: return True def _search(self): self.count = 0 line = parse(self.line) cwd = self.fm.env.cwd try: arg = line.rest(1) except IndexError: return False deq = deque(cwd.files) deq.rotate(-cwd.pointer) i = 0 for fsobj in deq: filename = fsobj.basename_lower if arg in filename: self.count += 1 if self.count == 1: cwd.move(to=(cwd.pointer + i) % len(cwd.files)) self.fm.env.cf = cwd.pointed_obj if self.count > 1: return False i += 1 return self.count == 1 class set(Command): """ :set