about summary refs log tree commit diff stats
Commit message (Expand)AuthorAgeFilesLines
* main: fixed --cleanhut2010-05-141-5/+7
* widget.browsercolumn: don't run files when scrolling in right columnhut2010-05-141-1/+1
* fsobject.fsobject: corrected typo and optimized loophut2010-05-141-2/+2
* fsobject.fsobject: applied patcheshut2010-05-143-115/+110
* core/environment: removed useless codehut2010-05-141-4/+0
* TODO: updatehut2010-05-141-0/+1
* fsobject.fsobject: Removed old/unused codehut2010-05-141-7/+1
* gui.ui: don't flush input when console is openhut2010-05-141-2/+2
* fixed #90hut2010-05-132-2/+2
* Added ranger.ext.spawn which runs programs and returns their stdouthut2010-05-132-3/+29
* main: restructured importshut2010-05-131-18/+21
* main: minor change, use relpath_confhut2010-05-131-1/+1
* main: strip off leading "file://" in filenameshut2010-05-131-0/+2
* main: more user friendly crash messageshut2010-05-131-6/+23
* TODO: updatedhut2010-05-121-1/+5
* defaults.options: increased default "scroll_offset" from 2 to 8hut2010-05-101-1/+1
* TODO: updatedhut2010-05-101-3/+13
* main: added commenthut2010-05-101-2/+3
* HACKING: updatehut2010-05-101-8/+3
* all_benchmarks: improvedhut2010-05-102-10/+20
* Makefile: updatedhut2010-05-101-9/+12
* Run python with flag "-O" by defaulthut2010-05-101-1/+1
* fixed "zc" keyhut2010-05-101-10/+10
* defaults.commands: edit current file with ":edit"hut2010-05-101-1/+4
* api.commands: fixed :ehut2010-05-101-3/+4
* close console with <C-D>hut2010-05-101-1/+1
* added <C-P> and <C-N> keys to consolehut2010-05-101-2/+2
* defaults.keys: added hint for "cw"hut2010-05-091-1/+1
* defaults.keys: fixed delete keyhut2010-05-091-1/+1
* fsobject.directory: bugfixhut2010-05-061-1/+1
* ext.human_readable: round(number, 2)hut2010-05-061-1/+1
* fsobject: bugfixhut2010-05-061-3/+3
* fsobject: don't unnecessarily use abspath() in __init__hut2010-05-063-9/+12
* fsobject.fsobject: lazily load mimetypehut2010-05-061-11/+26
* shared.mimetype: optimizationhut2010-05-061-5/+3
* fsobject.fsobject: optimizationshut2010-05-061-30/+44
* fsobject: a couple of optimizationshut2010-05-062-29/+44
* cleanupshut2010-05-059-30/+21
* Added license header to a few fileshut2010-05-044-0/+60
* bm_loader: stuffhut2010-05-031-1/+35
* fsobject: removed obsolete fsobject.type attribute + constantshut2010-05-032-13/+1
* Makefile: added `make bm` to run all benchmarks BMCOUNT timeshut2010-05-031-2/+5
* test: added TODO, skip broken testshut2010-05-034-5/+25
* Makefile: cleaned up, added automatic uninstallhut2010-05-031-58/+57
* ext.accumulator: added commenthut2010-05-021-0/+1
* defaults.commands: fixed :quit, added :quitallhut2010-05-021-5/+14
* help.movement: updated 1.6: there is no "tab 0" anymorehut2010-05-021-1/+1
* ranger.help: fixed topicshut2010-05-023-3/+5
* widgets.browsercolumn: call move_parent when scrolling in second columnhut2010-05-023-8/+19
* bugfix: scroll faster with ctrlhut2010-05-021-1/+1
hlight .gs { font-weight: bold } /* Generic.Strong */ .highlight .gu { color: #666666 } /* Generic.Subheading */ .highlight .gt { color: #aa0000 } /* Generic.Traceback */ .highlight .kc { color: #008800; font-weight: bold } /* Keyword.Constant */ .highlight .kd { color: #008800; font-weight: bold } /* Keyword.Declaration */ .highlight .kn { color: #008800; font-weight: bold } /* Keyword.Namespace */ .highlight .kp { color: #008800 } /* Keyword.Pseudo */ .highlight .kr { color: #008800; font-weight: bold } /* Keyword.Reserved */ .highlight .kt { color: #888888; font-weight: bold } /* Keyword.Type */ .highlight .m { color: #0000DD; font-weight: bold } /* Literal.Number */ .highlight .s { color: #dd2200; background-color: #fff0f0 } /* Literal.String */ .highlight .na { color: #336699 } /* Name.Attribute */ .highlight .nb { color: #003388 } /* Name.Builtin */ .highlight .nc { color: #bb0066; font-weight: bold } /* Name.Class */ .highlight .no { color: #003366; font-weight: bold } /* Name.Constant */ .highlight .nd { color: #555555 } /* Name.Decorator */ .highlight .ne { color: #bb0066; font-weight: bold } /* Name.Exception */ .highlight .nf { color: #0066bb; font-weight: bold } /* Name.Function */ .highlight .nl { color: #336699; font-style: italic } /* Name.Label */ .highlight .nn { color: #bb0066; font-weight: bold } /* Name.Namespace */ .highlight .py { color: #336699; font-weight: bold } /* Name.Property */ .highlight .nt { color: #bb0066; font-weight: bold } /* Name.Tag */ .highlight .nv { color: #336699 } /* Name.Variable */ .highlight .ow { color: #008800 } /* Operator.Word */ .highlight .w { color: #bbbbbb } /* Text.Whitespace */ .highlight .mb { color: #0000DD; font-weight: bold } /* Literal.Number.Bin */ .highlight .mf { color: #0000DD; font-weight: bold } /* Literal.Number.Float */ .highlight .mh { color: #0000DD; font-weight: bold } /* Literal.Number.Hex */ .highlight .mi { color: #0000DD; font-weight: bold } /* Literal.Number.Integer */ .highlight .mo { color: #0000DD; font-weight: bold } /* Literal.Number.Oct */ .highlight .sa { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Affix */ .highlight .sb { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Backtick */ .highlight .sc { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Char */ .highlight .dl { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Delimiter */ .highlight .sd { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Doc */ .highlight .s2 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Double */ .highlight .se { color: #0044dd; background-color: #fff0f0 } /* Literal.String.Escape */ .highlight .sh { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Heredoc */ .highlight .si { color: #3333bb; background-color: #fff0f0 } /* Literal.String.Interpol */ .highlight .sx { color: #22bb22; background-color: #f0fff0 } /* Literal.String.Other */ .highlight .sr { color: #008800; background-color: #fff0ff } /* Literal.String.Regex */ .highlight .s1 { color: #dd2200; background-color: #fff0f0 } /* Literal.String.Single */ .highlight .ss { color: #aa6600; background-color: #fff0f0 } /* Literal.String.Symbol */ .highlight .bp { color: #003388 } /* Name.Builtin.Pseudo */ .highlight .fm { color: #0066bb; font-weight: bold } /* Name.Function.Magic */ .highlight .vc { color: #336699 } /* Name.Variable.Class */ .highlight .vg { color: #dd7700 } /* Name.Variable.Global */ .highlight .vi { color: #3333bb } /* Name.Variable.Instance */ .highlight .vm { color: #336699 } /* Name.Variable.Magic */ .highlight .il { color: #0000DD; font-weight: bold } /* Literal.Number.Integer.Long */
# Copyright (C) 2009, 2010  Roman Zimbelmann <romanz@lavabit.com>
#
# 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 <http://www.gnu.org/licenses/>.

'''
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 <n>

		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 <dirname>

	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 <string>

	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 <option name>=<python expression>

	Gives an option a new value.
	"""
	def execute(self):
		line = parse(self.line)
		name = line.chunk(1)
		name, value, _ = line.parse_setting_line()
		if name and value:
			try:
				value = eval(value)
			except:
				pass
			self.fm.settings[name] = value

	def tab(self):
		line = parse(self.line)
		from ranger import log
		log(line.parse_setting_line())
		name, value, name_done = line.parse_setting_line()
		settings = self.fm.settings
		if not name:
			return (line + setting for setting in settings)
		if not value and not name_done:
			return (line + setting for setting in settings \
					if setting.startswith(name))
		if not value:
			return line + repr(settings[name])
		if bool in settings.types_of(name):
			if 'true'.startswith(value.lower()):
				return line + 'True'
			if 'false'.startswith(value.lower()):
				return line + 'False'


class quit(Command):
	"""
	:quit

	Closes the current tab.  If there is only one tab, quit the program.
	"""

	def execute(self):
		if len(self.fm.tabs) <= 1:
			self.fm.exit()
		self.fm.tab_close()


class quit_now(Command):
	"""
	:quit!

	Quits the program immediately.
	"""
	name = 'quit!'

	def execute(self):
		self.fm.exit()


class delete(Command):
	"""
	:delete

	Tries to delete the selection.

	"Selection" is defined as all the "marked files" (by default, you
	can mark files with space or v). If there are no marked files,
	use the "current file" (where the cursor is)

	When attempting to delete non-empty directories or multiple
	marked files, it will require a confirmation: The last word in
	the line has to start with a 'y'.  This may look like:
	:delete yes
	:delete seriously? yeah!
	"""

	allow_abbrev = False
	WARNING = 'delete seriously? '

	def execute(self):
		line = parse(self.line)
		lastword = line.chunk(-1)

		if lastword.startswith('y'):
			# user confirmed deletion!
			return self.fm.delete()
		elif self.line.startswith(delete.WARNING):
			# user did not confirm deletion
			return

		cwd = self.fm.env.cwd
		cf = self.fm.env.cf

		if cwd.marked_items or (cf.is_directory and not cf.is_link \
				and len(os.listdir(cf.path)) > 0):
			# better ask for a confirmation, when attempting to
			# delete multiple files or a non-empty directory.
			return self.fm.open_console(self.mode, delete.WARNING)

		# no need for a confirmation, just delete
		self.fm.delete()

class mkdir(Command):
	"""
	:mkdir <dirname>

	Creates a directory with the name <dirname>.
	"""

	def execute(self):
		from os.path import join, expanduser, lexists
		from os import mkdir

		line = parse(self.line)
		dirname = join(self.fm.env.cwd.path, expanduser(line.rest(1)))
		if not lexists(dirname):
			mkdir(dirname)
		else:
			self.fm.notify("file/directory exists!", bad=True)


class touch(Command):
	"""
	:touch <fname>

	Creates a file with the name <fname>.
	"""

	def execute(self):
		from os.path import join, expanduser, lexists
		from os import mkdir

		line = parse(self.line)
		fname = join(self.fm.env.cwd.path, expanduser(line.rest(1)))
		if not lexists(fname):
			open(fname, 'a')
		else:
			self.fm.notify("file/directory exists!", bad=True)


class edit(Command):
	"""
	:edit <filename>

	Opens the specified file in vim
	"""

	def execute(self):
		line = parse(self.line)
		self.fm.edit_file(line.rest(1))

	def tab(self):
		return self._tab_directory_content()


class eval_(Command):
	"""
	:eval <python code>

	Evaluates the python code.
	`fm' is a reference to the FM instance.
	To display text, use the function `p'.

	Examples:
	:eval fm
	:eval len(fm.env.directories)
	:eval p("Hello World!")
	"""
	name = 'eval'

	def execute(self):
		code = parse(self.line).rest(1)
		fm = self.fm
		p = fm.notify
		try:
			try:
				result = eval(code)
			except SyntaxError:
				exec(code)
			else:
				if result:
					p(result)
		except Exception as err:
			p(err)


class rename(Command):
	"""
	:rename <newname>

	Changes the name of the currently highlighted file to <newname>
	"""

	def execute(self):
		from ranger.fsobject.file import File
		line = parse(self.line)
		if not line.rest(1):
			return self.fm.notify('Syntax: rename <newname>', bad=True)
		self.fm.rename(self.fm.env.cf, line.rest(1))
		f = File(line.rest(1))
		self.fm.env.cwd.pointed_obj = f
		self.fm.env.cf = f

	def tab(self):
		return self._tab_directory_content()


class chmod(Command):
	"""
	:chmod <octal number>

	Sets the permissions of the selection to the octal number.

	The octal number is between 0 and 777. The digits specify the
	permissions for the user, the group and others.

	A 1 permits execution, a 2 permits writing, a 4 permits reading.
	Add those numbers to combine them. So a 7 permits everything.
	"""

	def execute(self):
		line = parse(self.line)
		mode = line.rest(1)

		try:
			mode = int(mode, 8)
			if mode < 0 or mode > 511:
				raise ValueError
		except ValueError:
			self.fm.notify("Need an octal number between 0 and 777!", bad=True)
			return

		for file in self.fm.env.get_selection():
			try:
				os.chmod(file.path, mode)
			except Exception as ex:
				self.fm.notify(ex)

		try:
			# reloading directory.  maybe its better to reload the selected
			# files only.
			self.fm.env.cwd.load_content()
		except:
			pass


class filter(Command):
	"""
	:filter <string>

	Displays only the files which contain <string> in their basename.
	"""

	def execute(self):
		line = parse(self.line)
		self.fm.set_filter(line.rest(1))


class grep(Command):
	"""
	:grep <string>

	Looks for a string in all marked files or directories
	"""

	def execute(self):
		line = parse(self.line)
		if line.rest(1):
			action = ['grep', '--color=always', '--line-number']
			action.extend(['-e', line.rest(1), '-r'])
			action.extend(f.path for f in self.fm.env.get_selection())
			self.fm.execute_command(action, flags='p')