You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

commands.py 57KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877
  1. # Note that additional commands are automatically generated from the methods
  2. # of the class ranger.core.actions.Actions.
  3. # ===================================================================
  4. # Every class defined here which is a subclass of `Command' will be used as a
  5. # command in ranger. Several methods are defined to interface with ranger:
  6. # execute(): called when the command is executed.
  7. # cancel(): called when closing the console.
  8. # tab(tabnum): called when <TAB> is pressed.
  9. # quick(): called after each keypress.
  10. #
  11. # tab() argument tabnum is 1 for <TAB> and -1 for <S-TAB> by default
  12. #
  13. # The return values for tab() can be either:
  14. # None: There is no tab completion
  15. # A string: Change the console to this string
  16. # A list/tuple/generator: cycle through every item in it
  17. #
  18. # The return value for quick() can be:
  19. # False: Nothing happens
  20. # True: Execute the command afterwards
  21. #
  22. # The return value for execute() and cancel() doesn't matter.
  23. #
  24. # ===================================================================
  25. # Commands have certain attributes and methods that facilitate parsing of
  26. # the arguments:
  27. #
  28. # self.line: The whole line that was written in the console.
  29. # self.args: A list of all (space-separated) arguments to the command.
  30. # self.quantifier: If this command was mapped to the key "X" and
  31. # the user pressed 6X, self.quantifier will be 6.
  32. # self.arg(n): The n-th argument, or an empty string if it doesn't exist.
  33. # self.rest(n): The n-th argument plus everything that followed. For example,
  34. # if the command was "search foo bar a b c", rest(2) will be "bar a b c"
  35. # self.start(n): Anything before the n-th argument. For example, if the
  36. # command was "search foo bar a b c", start(2) will be "search foo"
  37. #
  38. # ===================================================================
  39. # And this is a little reference for common ranger functions and objects:
  40. #
  41. # self.fm: A reference to the "fm" object which contains most information
  42. # about ranger.
  43. # self.fm.notify(string): Print the given string on the screen.
  44. # self.fm.notify(string, bad=True): Print the given string in RED.
  45. # self.fm.reload_cwd(): Reload the current working directory.
  46. # self.fm.thisdir: The current working directory. (A File object.)
  47. # self.fm.thisfile: The current file. (A File object too.)
  48. # self.fm.thistab.get_selection(): A list of all selected files.
  49. # self.fm.execute_console(string): Execute the string as a ranger command.
  50. # self.fm.open_console(string): Open the console with the given string
  51. # already typed in for you.
  52. # self.fm.move(direction): Moves the cursor in the given direction, which
  53. # can be something like down=3, up=5, right=1, left=1, to=6, ...
  54. #
  55. # File objects (for example self.fm.thisfile) have these useful attributes and
  56. # methods:
  57. #
  58. # tfile.path: The path to the file.
  59. # tfile.basename: The base name only.
  60. # tfile.load_content(): Force a loading of the directories content (which
  61. # obviously works with directories only)
  62. # tfile.is_directory: True/False depending on whether it's a directory.
  63. #
  64. # For advanced commands it is unavoidable to dive a bit into the source code
  65. # of ranger.
  66. # ===================================================================
  67. from __future__ import (absolute_import, division, print_function)
  68. from collections import deque
  69. import os
  70. import re
  71. from ranger.api.commands import Command
  72. class alias(Command):
  73. """:alias <newcommand> <oldcommand>
  74. Copies the oldcommand as newcommand.
  75. """
  76. context = 'browser'
  77. resolve_macros = False
  78. def execute(self):
  79. if not self.arg(1) or not self.arg(2):
  80. self.fm.notify('Syntax: alias <newcommand> <oldcommand>', bad=True)
  81. return
  82. self.fm.commands.alias(self.arg(1), self.rest(2))
  83. class echo(Command):
  84. """:echo <text>
  85. Display the text in the statusbar.
  86. """
  87. def execute(self):
  88. self.fm.notify(self.rest(1))
  89. class cd(Command):
  90. """:cd [-r] <path>
  91. The cd command changes the directory.
  92. If the path is a file, selects that file.
  93. The command 'cd -' is equivalent to typing ``.
  94. Using the option "-r" will get you to the real path.
  95. """
  96. def execute(self):
  97. if self.arg(1) == '-r':
  98. self.shift()
  99. destination = os.path.realpath(self.rest(1))
  100. if os.path.isfile(destination):
  101. self.fm.select_file(destination)
  102. return
  103. else:
  104. destination = self.rest(1)
  105. if not destination:
  106. destination = '~'
  107. if destination == '-':
  108. self.fm.enter_bookmark('`')
  109. else:
  110. self.fm.cd(destination)
  111. def _tab_args(self):
  112. # dest must be rest because path could contain spaces
  113. if self.arg(1) == '-r':
  114. start = self.start(2)
  115. dest = self.rest(2)
  116. else:
  117. start = self.start(1)
  118. dest = self.rest(1)
  119. if dest:
  120. head, tail = os.path.split(os.path.expanduser(dest))
  121. if head:
  122. dest_exp = os.path.join(os.path.normpath(head), tail)
  123. else:
  124. dest_exp = tail
  125. else:
  126. dest_exp = ''
  127. return (start, dest_exp, os.path.join(self.fm.thisdir.path, dest_exp),
  128. dest.endswith(os.path.sep))
  129. @staticmethod
  130. def _tab_paths(dest, dest_abs, ends_with_sep):
  131. if not dest:
  132. try:
  133. return next(os.walk(dest_abs))[1], dest_abs
  134. except (OSError, StopIteration):
  135. return [], ''
  136. if ends_with_sep:
  137. try:
  138. return [os.path.join(dest, path) for path in next(os.walk(dest_abs))[1]], ''
  139. except (OSError, StopIteration):
  140. return [], ''
  141. return None, None
  142. def _tab_match(self, path_user, path_file):
  143. if self.fm.settings.cd_tab_case == 'insensitive':
  144. path_user = path_user.lower()
  145. path_file = path_file.lower()
  146. elif self.fm.settings.cd_tab_case == 'smart' and path_user.islower():
  147. path_file = path_file.lower()
  148. return path_file.startswith(path_user)
  149. def _tab_normal(self, dest, dest_abs):
  150. dest_dir = os.path.dirname(dest)
  151. dest_base = os.path.basename(dest)
  152. try:
  153. dirnames = next(os.walk(os.path.dirname(dest_abs)))[1]
  154. except (OSError, StopIteration):
  155. return [], ''
  156. return [os.path.join(dest_dir, d) for d in dirnames if self._tab_match(dest_base, d)], ''
  157. def _tab_fuzzy_match(self, basepath, tokens):
  158. """ Find directories matching tokens recursively """
  159. if not tokens:
  160. tokens = ['']
  161. paths = [basepath]
  162. while True:
  163. token = tokens.pop()
  164. matches = []
  165. for path in paths:
  166. try:
  167. directories = next(os.walk(path))[1]
  168. except (OSError, StopIteration):
  169. continue
  170. matches += [os.path.join(path, d) for d in directories
  171. if self._tab_match(token, d)]
  172. if not tokens or not matches:
  173. return matches
  174. paths = matches
  175. return None
  176. def _tab_fuzzy(self, dest, dest_abs):
  177. tokens = []
  178. basepath = dest_abs
  179. while True:
  180. basepath_old = basepath
  181. basepath, token = os.path.split(basepath)
  182. if basepath == basepath_old:
  183. break
  184. if os.path.isdir(basepath_old) and not token.startswith('.'):
  185. basepath = basepath_old
  186. break
  187. tokens.append(token)
  188. paths = self._tab_fuzzy_match(basepath, tokens)
  189. if not os.path.isabs(dest):
  190. paths_rel = basepath
  191. paths = [os.path.relpath(path, paths_rel) for path in paths]
  192. else:
  193. paths_rel = ''
  194. return paths, paths_rel
  195. def tab(self, tabnum):
  196. from os.path import sep
  197. start, dest, dest_abs, ends_with_sep = self._tab_args()
  198. paths, paths_rel = self._tab_paths(dest, dest_abs, ends_with_sep)
  199. if paths is None:
  200. if self.fm.settings.cd_tab_fuzzy:
  201. paths, paths_rel = self._tab_fuzzy(dest, dest_abs)
  202. else:
  203. paths, paths_rel = self._tab_normal(dest, dest_abs)
  204. paths.sort()
  205. if self.fm.settings.cd_bookmarks:
  206. paths[0:0] = [
  207. os.path.relpath(v.path, paths_rel) if paths_rel else v.path
  208. for v in self.fm.bookmarks.dct.values() for path in paths
  209. if v.path.startswith(os.path.join(paths_rel, path) + sep)
  210. ]
  211. if not paths:
  212. return None
  213. if len(paths) == 1:
  214. return start + paths[0] + sep
  215. return [start + dirname for dirname in paths]
  216. class chain(Command):
  217. """:chain <command1>; <command2>; ...
  218. Calls multiple commands at once, separated by semicolons.
  219. """
  220. def execute(self):
  221. if not self.rest(1).strip():
  222. self.fm.notify('Syntax: chain <command1>; <command2>; ...', bad=True)
  223. return
  224. for command in [s.strip() for s in self.rest(1).split(";")]:
  225. self.fm.execute_console(command)
  226. class shell(Command):
  227. escape_macros_for_shell = True
  228. def execute(self):
  229. if self.arg(1) and self.arg(1)[0] == '-':
  230. flags = self.arg(1)[1:]
  231. command = self.rest(2)
  232. else:
  233. flags = ''
  234. command = self.rest(1)
  235. if command:
  236. self.fm.execute_command(command, flags=flags)
  237. def tab(self, tabnum):
  238. from ranger.ext.get_executables import get_executables
  239. if self.arg(1) and self.arg(1)[0] == '-':
  240. command = self.rest(2)
  241. else:
  242. command = self.rest(1)
  243. start = self.line[0:len(self.line) - len(command)]
  244. try:
  245. position_of_last_space = command.rindex(" ")
  246. except ValueError:
  247. return (start + program + ' ' for program
  248. in get_executables() if program.startswith(command))
  249. if position_of_last_space == len(command) - 1:
  250. selection = self.fm.thistab.get_selection()
  251. if len(selection) == 1:
  252. return self.line + selection[0].shell_escaped_basename + ' '
  253. return self.line + '%s '
  254. before_word, start_of_word = self.line.rsplit(' ', 1)
  255. return (before_word + ' ' + file.shell_escaped_basename
  256. for file in self.fm.thisdir.files or []
  257. if file.shell_escaped_basename.startswith(start_of_word))
  258. class open_with(Command):
  259. def execute(self):
  260. app, flags, mode = self._get_app_flags_mode(self.rest(1))
  261. self.fm.execute_file(
  262. files=[f for f in self.fm.thistab.get_selection()],
  263. app=app,
  264. flags=flags,
  265. mode=mode)
  266. def tab(self, tabnum):
  267. return self._tab_through_executables()
  268. def _get_app_flags_mode(self, string): # pylint: disable=too-many-branches,too-many-statements
  269. """Extracts the application, flags and mode from a string.
  270. examples:
  271. "mplayer f 1" => ("mplayer", "f", 1)
  272. "atool 4" => ("atool", "", 4)
  273. "p" => ("", "p", 0)
  274. "" => None
  275. """
  276. app = ''
  277. flags = ''
  278. mode = 0
  279. split = string.split()
  280. if len(split) == 1:
  281. part = split[0]
  282. if self._is_app(part):
  283. app = part
  284. elif self._is_flags(part):
  285. flags = part
  286. elif self._is_mode(part):
  287. mode = part
  288. elif len(split) == 2:
  289. part0 = split[0]
  290. part1 = split[1]
  291. if self._is_app(part0):
  292. app = part0
  293. if self._is_flags(part1):
  294. flags = part1
  295. elif self._is_mode(part1):
  296. mode = part1
  297. elif self._is_flags(part0):
  298. flags = part0
  299. if self._is_mode(part1):
  300. mode = part1
  301. elif self._is_mode(part0):
  302. mode = part0
  303. if self._is_flags(part1):
  304. flags = part1
  305. elif len(split) >= 3:
  306. part0 = split[0]
  307. part1 = split[1]
  308. part2 = split[2]
  309. if self._is_app(part0):
  310. app = part0
  311. if self._is_flags(part1):
  312. flags = part1
  313. if self._is_mode(part2):
  314. mode = part2
  315. elif self._is_mode(part1):
  316. mode = part1
  317. if self._is_flags(part2):
  318. flags = part2
  319. elif self._is_flags(part0):
  320. flags = part0
  321. if self._is_mode(part1):
  322. mode = part1
  323. elif self._is_mode(part0):
  324. mode = part0
  325. if self._is_flags(part1):
  326. flags = part1
  327. return app, flags, int(mode)
  328. def _is_app(self, arg):
  329. return not self._is_flags(arg) and not arg.isdigit()
  330. @staticmethod
  331. def _is_flags(arg):
  332. from ranger.core.runner import ALLOWED_FLAGS
  333. return all(x in ALLOWED_FLAGS for x in arg)
  334. @staticmethod
  335. def _is_mode(arg):
  336. return all(x in '0123456789' for x in arg)
  337. class set_(Command):
  338. """:set <option name>=<python expression>
  339. Gives an option a new value.
  340. Use `:set <option>!` to toggle or cycle it, e.g. `:set flush_input!`
  341. """
  342. name = 'set' # don't override the builtin set class
  343. def execute(self):
  344. name = self.arg(1)
  345. name, value, _, toggle = self.parse_setting_line_v2()
  346. if toggle:
  347. self.fm.toggle_option(name)
  348. else:
  349. self.fm.set_option_from_string(name, value)
  350. def tab(self, tabnum): # pylint: disable=too-many-return-statements
  351. from ranger.gui.colorscheme import get_all_colorschemes
  352. name, value, name_done = self.parse_setting_line()
  353. settings = self.fm.settings
  354. if not name:
  355. return sorted(self.firstpart + setting for setting in settings)
  356. if not value and not name_done:
  357. return sorted(self.firstpart + setting for setting in settings
  358. if setting.startswith(name))
  359. if not value:
  360. value_completers = {
  361. "colorscheme":
  362. # Cycle through colorschemes when name, but no value is specified
  363. lambda: sorted(self.firstpart + colorscheme for colorscheme
  364. in get_all_colorschemes(self.fm)),
  365. "column_ratios":
  366. lambda: self.firstpart + ",".join(map(str, settings[name])),
  367. }
  368. def default_value_completer():
  369. return self.firstpart + str(settings[name])
  370. return value_completers.get(name, default_value_completer)()
  371. if bool in settings.types_of(name):
  372. if 'true'.startswith(value.lower()):
  373. return self.firstpart + 'True'
  374. if 'false'.startswith(value.lower()):
  375. return self.firstpart + 'False'
  376. # Tab complete colorscheme values if incomplete value is present
  377. if name == "colorscheme":
  378. return sorted(self.firstpart + colorscheme for colorscheme
  379. in get_all_colorschemes(self.fm) if colorscheme.startswith(value))
  380. return None
  381. class setlocal(set_):
  382. """:setlocal path=<regular expression> <option name>=<python expression>
  383. Gives an option a new value.
  384. """
  385. PATH_RE_DQUOTED = re.compile(r'^setlocal\s+path="(.*?)"')
  386. PATH_RE_SQUOTED = re.compile(r"^setlocal\s+path='(.*?)'")
  387. PATH_RE_UNQUOTED = re.compile(r'^path=(.*?)$')
  388. def _re_shift(self, match):
  389. if not match:
  390. return None
  391. path = os.path.expanduser(match.group(1))
  392. for _ in range(len(path.split())):
  393. self.shift()
  394. return path
  395. def execute(self):
  396. path = self._re_shift(self.PATH_RE_DQUOTED.match(self.line))
  397. if path is None:
  398. path = self._re_shift(self.PATH_RE_SQUOTED.match(self.line))
  399. if path is None:
  400. path = self._re_shift(self.PATH_RE_UNQUOTED.match(self.arg(1)))
  401. if path is None and self.fm.thisdir:
  402. path = self.fm.thisdir.path
  403. if not path:
  404. return
  405. name, value, _ = self.parse_setting_line()
  406. self.fm.set_option_from_string(name, value, localpath=path)
  407. class setintag(set_):
  408. """:setintag <tag or tags> <option name>=<option value>
  409. Sets an option for directories that are tagged with a specific tag.
  410. """
  411. def execute(self):
  412. tags = self.arg(1)
  413. self.shift()
  414. name, value, _ = self.parse_setting_line()
  415. self.fm.set_option_from_string(name, value, tags=tags)
  416. class default_linemode(Command):
  417. def execute(self):
  418. from ranger.container.fsobject import FileSystemObject
  419. if len(self.args) < 2:
  420. self.fm.notify(
  421. "Usage: default_linemode [path=<regexp> | tag=<tag(s)>] <linemode>", bad=True)
  422. # Extract options like "path=..." or "tag=..." from the command line
  423. arg1 = self.arg(1)
  424. method = "always"
  425. argument = None
  426. if arg1.startswith("path="):
  427. method = "path"
  428. argument = re.compile(arg1[5:])
  429. self.shift()
  430. elif arg1.startswith("tag="):
  431. method = "tag"
  432. argument = arg1[4:]
  433. self.shift()
  434. # Extract and validate the line mode from the command line
  435. lmode = self.rest(1)
  436. if lmode not in FileSystemObject.linemode_dict:
  437. self.fm.notify(
  438. "Invalid linemode: %s; should be %s" % (
  439. lmode, "/".join(FileSystemObject.linemode_dict)),
  440. bad=True,
  441. )
  442. # Add the prepared entry to the fm.default_linemodes
  443. entry = [method, argument, lmode]
  444. self.fm.default_linemodes.appendleft(entry)
  445. # Redraw the columns
  446. if self.fm.ui.browser:
  447. for col in self.fm.ui.browser.columns:
  448. col.need_redraw = True
  449. def tab(self, tabnum):
  450. return (self.arg(0) + " " + lmode
  451. for lmode in self.fm.thisfile.linemode_dict.keys()
  452. if lmode.startswith(self.arg(1)))
  453. class quit(Command): # pylint: disable=redefined-builtin
  454. """:quit
  455. Closes the current tab, if there's only one tab.
  456. Otherwise quits if there are no tasks in progress.
  457. """
  458. def _exit_no_work(self):
  459. if self.fm.loader.has_work():
  460. self.fm.notify('Not quitting: Tasks in progress: Use `quit!` to force quit')
  461. else:
  462. self.fm.exit()
  463. def execute(self):
  464. if len(self.fm.tabs) >= 2:
  465. self.fm.tab_close()
  466. else:
  467. self._exit_no_work()
  468. class quit_bang(Command):
  469. """:quit!
  470. Closes the current tab, if there's only one tab.
  471. Otherwise force quits immediately.
  472. """
  473. name = 'quit!'
  474. allow_abbrev = False
  475. def execute(self):
  476. if len(self.fm.tabs) >= 2:
  477. self.fm.tab_close()
  478. else:
  479. self.fm.exit()
  480. class quitall(Command):
  481. """:quitall
  482. Quits if there are no tasks in progress.
  483. """
  484. def _exit_no_work(self):
  485. if self.fm.loader.has_work():
  486. self.fm.notify('Not quitting: Tasks in progress: Use `quitall!` to force quit')
  487. else:
  488. self.fm.exit()
  489. def execute(self):
  490. self._exit_no_work()
  491. class quitall_bang(Command):
  492. """:quitall!
  493. Force quits immediately.
  494. """
  495. name = 'quitall!'
  496. allow_abbrev = False
  497. def execute(self):
  498. self.fm.exit()
  499. class terminal(Command):
  500. """:terminal
  501. Spawns an "x-terminal-emulator" starting in the current directory.
  502. """
  503. def execute(self):
  504. from ranger.ext.get_executables import get_term
  505. self.fm.run(get_term(), flags='f')
  506. class delete(Command):
  507. """:delete
  508. Tries to delete the selection or the files passed in arguments (if any).
  509. The arguments use a shell-like escaping.
  510. "Selection" is defined as all the "marked files" (by default, you
  511. can mark files with space or v). If there are no marked files,
  512. use the "current file" (where the cursor is)
  513. When attempting to delete non-empty directories or multiple
  514. marked files, it will require a confirmation.
  515. """
  516. allow_abbrev = False
  517. escape_macros_for_shell = True
  518. def execute(self):
  519. import shlex
  520. from functools import partial
  521. def is_directory_with_files(path):
  522. return os.path.isdir(path) and not os.path.islink(path) and len(os.listdir(path)) > 0
  523. if self.rest(1):
  524. files = shlex.split(self.rest(1))
  525. many_files = (len(files) > 1 or is_directory_with_files(files[0]))
  526. else:
  527. cwd = self.fm.thisdir
  528. tfile = self.fm.thisfile
  529. if not cwd or not tfile:
  530. self.fm.notify("Error: no file selected for deletion!", bad=True)
  531. return
  532. # relative_path used for a user-friendly output in the confirmation.
  533. files = [f.relative_path for f in self.fm.thistab.get_selection()]
  534. many_files = (cwd.marked_items or is_directory_with_files(tfile.path))
  535. confirm = self.fm.settings.confirm_on_delete
  536. if confirm != 'never' and (confirm != 'multiple' or many_files):
  537. self.fm.ui.console.ask(
  538. "Confirm deletion of: %s (y/N)" % ', '.join(files),
  539. partial(self._question_callback, files),
  540. ('n', 'N', 'y', 'Y'),
  541. )
  542. else:
  543. # no need for a confirmation, just delete
  544. self.fm.delete(files)
  545. def tab(self, tabnum):
  546. return self._tab_directory_content()
  547. def _question_callback(self, files, answer):
  548. if answer == 'y' or answer == 'Y':
  549. self.fm.delete(files)
  550. class jump_non(Command):
  551. """:jump_non [-FLAGS...]
  552. Jumps to first non-directory if highlighted file is a directory and vice versa.
  553. Flags:
  554. -r Jump in reverse order
  555. -w Wrap around if reaching end of filelist
  556. """
  557. def __init__(self, *args, **kwargs):
  558. super(jump_non, self).__init__(*args, **kwargs)
  559. flags, _ = self.parse_flags()
  560. self._flag_reverse = 'r' in flags
  561. self._flag_wrap = 'w' in flags
  562. @staticmethod
  563. def _non(fobj, is_directory):
  564. return fobj.is_directory if not is_directory else not fobj.is_directory
  565. def execute(self):
  566. tfile = self.fm.thisfile
  567. passed = False
  568. found_before = None
  569. found_after = None
  570. for fobj in self.fm.thisdir.files[::-1] if self._flag_reverse else self.fm.thisdir.files:
  571. if fobj.path == tfile.path:
  572. passed = True
  573. continue
  574. if passed:
  575. if self._non(fobj, tfile.is_directory):
  576. found_after = fobj.path
  577. break
  578. elif not found_before and self._non(fobj, tfile.is_directory):
  579. found_before = fobj.path
  580. if found_after:
  581. self.fm.select_file(found_after)
  582. elif self._flag_wrap and found_before:
  583. self.fm.select_file(found_before)
  584. class mark_tag(Command):
  585. """:mark_tag [<tags>]
  586. Mark all tags that are tagged with either of the given tags.
  587. When leaving out the tag argument, all tagged files are marked.
  588. """
  589. do_mark = True
  590. def execute(self):
  591. cwd = self.fm.thisdir
  592. tags = self.rest(1).replace(" ", "")
  593. if not self.fm.tags or not cwd.files:
  594. return
  595. for fileobj in cwd.files:
  596. try:
  597. tag = self.fm.tags.tags[fileobj.realpath]
  598. except KeyError:
  599. continue
  600. if not tags or tag in tags:
  601. cwd.mark_item(fileobj, val=self.do_mark)
  602. self.fm.ui.status.need_redraw = True
  603. self.fm.ui.need_redraw = True
  604. class console(Command):
  605. """:console <command>
  606. Open the console with the given command.
  607. """
  608. def execute(self):
  609. position = None
  610. if self.arg(1)[0:2] == '-p':
  611. try:
  612. position = int(self.arg(1)[2:])
  613. except ValueError:
  614. pass
  615. else:
  616. self.shift()
  617. self.fm.open_console(self.rest(1), position=position)
  618. class load_copy_buffer(Command):
  619. """:load_copy_buffer
  620. Load the copy buffer from datadir/copy_buffer
  621. """
  622. copy_buffer_filename = 'copy_buffer'
  623. def execute(self):
  624. import sys
  625. from ranger.container.file import File
  626. from os.path import exists
  627. fname = self.fm.datapath(self.copy_buffer_filename)
  628. unreadable = IOError if sys.version_info[0] < 3 else OSError
  629. try:
  630. fobj = open(fname, 'r')
  631. except unreadable:
  632. return self.fm.notify(
  633. "Cannot open %s" % (fname or self.copy_buffer_filename), bad=True)
  634. self.fm.copy_buffer = set(File(g)
  635. for g in fobj.read().split("\n") if exists(g))
  636. fobj.close()
  637. self.fm.ui.redraw_main_column()
  638. return None
  639. class save_copy_buffer(Command):
  640. """:save_copy_buffer
  641. Save the copy buffer to datadir/copy_buffer
  642. """
  643. copy_buffer_filename = 'copy_buffer'
  644. def execute(self):
  645. import sys
  646. fname = None
  647. fname = self.fm.datapath(self.copy_buffer_filename)
  648. unwritable = IOError if sys.version_info[0] < 3 else OSError
  649. try:
  650. fobj = open(fname, 'w')
  651. except unwritable:
  652. return self.fm.notify("Cannot open %s" %
  653. (fname or self.copy_buffer_filename), bad=True)
  654. fobj.write("\n".join(fobj.path for fobj in self.fm.copy_buffer))
  655. fobj.close()
  656. return None
  657. class unmark_tag(mark_tag):
  658. """:unmark_tag [<tags>]
  659. Unmark all tags that are tagged with either of the given tags.
  660. When leaving out the tag argument, all tagged files are unmarked.
  661. """
  662. do_mark = False
  663. class mkdir(Command):
  664. """:mkdir <dirname>
  665. Creates a directory with the name <dirname>.
  666. """
  667. def execute(self):
  668. from os.path import join, expanduser, lexists
  669. from os import makedirs
  670. dirname = join(self.fm.thisdir.path, expanduser(self.rest(1)))
  671. if not lexists(dirname):
  672. makedirs(dirname)
  673. else:
  674. self.fm.notify("file/directory exists!", bad=True)
  675. def tab(self, tabnum):
  676. return self._tab_directory_content()
  677. class touch(Command):
  678. """:touch <fname>
  679. Creates a file with the name <fname>.
  680. """
  681. def execute(self):
  682. from os.path import join, expanduser, lexists
  683. fname = join(self.fm.thisdir.path, expanduser(self.rest(1)))
  684. if not lexists(fname):
  685. open(fname, 'a').close()
  686. else:
  687. self.fm.notify("file/directory exists!", bad=True)
  688. def tab(self, tabnum):
  689. return self._tab_directory_content()
  690. class edit(Command):
  691. """:edit <filename>
  692. Opens the specified file in vim
  693. """
  694. def execute(self):
  695. if not self.arg(1):
  696. self.fm.edit_file(self.fm.thisfile.path)
  697. else:
  698. self.fm.edit_file(self.rest(1))
  699. def tab(self, tabnum):
  700. return self._tab_directory_content()
  701. class eval_(Command):
  702. """:eval [-q] <python code>
  703. Evaluates the python code.
  704. `fm' is a reference to the FM instance.
  705. To display text, use the function `p'.
  706. Examples:
  707. :eval fm
  708. :eval len(fm.directories)
  709. :eval p("Hello World!")
  710. """
  711. name = 'eval'
  712. resolve_macros = False
  713. def execute(self):
  714. # The import is needed so eval() can access the ranger module
  715. import ranger # NOQA pylint: disable=unused-import,unused-variable
  716. if self.arg(1) == '-q':
  717. code = self.rest(2)
  718. quiet = True
  719. else:
  720. code = self.rest(1)
  721. quiet = False
  722. global cmd, fm, p, quantifier # pylint: disable=invalid-name,global-variable-undefined
  723. fm = self.fm
  724. cmd = self.fm.execute_console
  725. p = fm.notify
  726. quantifier = self.quantifier
  727. try:
  728. try:
  729. result = eval(code) # pylint: disable=eval-used
  730. except SyntaxError:
  731. exec(code) # pylint: disable=exec-used
  732. else:
  733. if result and not quiet:
  734. p(result)
  735. except Exception as err: # pylint: disable=broad-except
  736. fm.notify("The error `%s` was caused by evaluating the "
  737. "following code: `%s`" % (err, code), bad=True)
  738. class rename(Command):
  739. """:rename <newname>
  740. Changes the name of the currently highlighted file to <newname>
  741. """
  742. def execute(self):
  743. from ranger.container.file import File
  744. from os import access
  745. new_name = self.rest(1)
  746. if not new_name:
  747. return self.fm.notify('Syntax: rename <newname>', bad=True)
  748. if new_name == self.fm.thisfile.relative_path:
  749. return None
  750. if access(new_name, os.F_OK):
  751. return self.fm.notify("Can't rename: file already exists!", bad=True)
  752. if self.fm.rename(self.fm.thisfile, new_name):
  753. file_new = File(new_name)
  754. self.fm.bookmarks.update_path(self.fm.thisfile.path, file_new)
  755. self.fm.tags.update_path(self.fm.thisfile.path, file_new.path)
  756. self.fm.thisdir.pointed_obj = file_new
  757. self.fm.thisfile = file_new
  758. return None
  759. def tab(self, tabnum):
  760. return self._tab_directory_content()
  761. class rename_append(Command):
  762. """:rename_append [-FLAGS...]
  763. Opens the console with ":rename <current file>" with the cursor positioned
  764. before the file extension.
  765. Flags:
  766. -a Position before all extensions
  767. -r Remove everything before extensions
  768. """
  769. def __init__(self, *args, **kwargs):
  770. super(rename_append, self).__init__(*args, **kwargs)
  771. flags, _ = self.parse_flags()
  772. self._flag_ext_all = 'a' in flags
  773. self._flag_remove = 'r' in flags
  774. def execute(self):
  775. from ranger import MACRO_DELIMITER, MACRO_DELIMITER_ESC
  776. tfile = self.fm.thisfile
  777. relpath = tfile.relative_path.replace(MACRO_DELIMITER, MACRO_DELIMITER_ESC)
  778. basename = tfile.basename.replace(MACRO_DELIMITER, MACRO_DELIMITER_ESC)
  779. if basename.find('.') <= 0:
  780. self.fm.open_console('rename ' + relpath)
  781. return
  782. if self._flag_ext_all:
  783. pos_ext = re.search(r'[^.]+', basename).end(0)
  784. else:
  785. pos_ext = basename.rindex('.')
  786. pos = len(relpath) - len(basename) + pos_ext
  787. if self._flag_remove:
  788. relpath = relpath[:-len(basename)] + basename[pos_ext:]
  789. pos -= pos_ext
  790. self.fm.open_console('rename ' + relpath, position=(7 + pos))
  791. class chmod(Command):
  792. """:chmod <octal number>
  793. Sets the permissions of the selection to the octal number.
  794. The octal number is between 0 and 777. The digits specify the
  795. permissions for the user, the group and others.
  796. A 1 permits execution, a 2 permits writing, a 4 permits reading.
  797. Add those numbers to combine them. So a 7 permits everything.
  798. """
  799. def execute(self):
  800. mode_str = self.rest(1)
  801. if not mode_str:
  802. if not self.quantifier:
  803. self.fm.notify("Syntax: chmod <octal number>", bad=True)
  804. return
  805. mode_str = str(self.quantifier)
  806. try:
  807. mode = int(mode_str, 8)
  808. if mode < 0 or mode > 0o777:
  809. raise ValueError
  810. except ValueError:
  811. self.fm.notify("Need an octal number between 0 and 777!", bad=True)
  812. return
  813. for fobj in self.fm.thistab.get_selection():
  814. try:
  815. os.chmod(fobj.path, mode)
  816. except OSError as ex:
  817. self.fm.notify(ex)
  818. # reloading directory. maybe its better to reload the selected
  819. # files only.
  820. self.fm.thisdir.content_outdated = True
  821. class bulkrename(Command):
  822. """:bulkrename
  823. This command opens a list of selected files in an external editor.
  824. After you edit and save the file, it will generate a shell script
  825. which does bulk renaming according to the changes you did in the file.
  826. This shell script is opened in an editor for you to review.
  827. After you close it, it will be executed.
  828. """
  829. def execute(self): # pylint: disable=too-many-locals,too-many-statements
  830. import sys
  831. import tempfile
  832. from ranger.container.file import File
  833. from ranger.ext.shell_escape import shell_escape as esc
  834. py3 = sys.version_info[0] >= 3
  835. # Create and edit the file list
  836. filenames = [f.relative_path for f in self.fm.thistab.get_selection()]
  837. listfile = tempfile.NamedTemporaryFile(delete=False)
  838. listpath = listfile.name
  839. if py3:
  840. listfile.write("\n".join(filenames).encode("utf-8"))
  841. else:
  842. listfile.write("\n".join(filenames))
  843. listfile.close()
  844. self.fm.execute_file([File(listpath)], app='editor')
  845. listfile = open(listpath, 'r')
  846. new_filenames = listfile.read().split("\n")
  847. listfile.close()
  848. os.unlink(listpath)
  849. if all(a == b for a, b in zip(filenames, new_filenames)):
  850. self.fm.notify("No renaming to be done!")
  851. return
  852. # Generate script
  853. cmdfile = tempfile.NamedTemporaryFile()
  854. script_lines = []
  855. script_lines.append("# This file will be executed when you close the editor.\n")
  856. script_lines.append("# Please double-check everything, clear the file to abort.\n")
  857. script_lines.extend("mv -vi -- %s %s\n" % (esc(old), esc(new))
  858. for old, new in zip(filenames, new_filenames) if old != new)
  859. script_content = "".join(script_lines)
  860. if py3:
  861. cmdfile.write(script_content.encode("utf-8"))
  862. else:
  863. cmdfile.write(script_content)
  864. cmdfile.flush()
  865. # Open the script and let the user review it, then check if the script
  866. # was modified by the user
  867. self.fm.execute_file([File(cmdfile.name)], app='editor')
  868. cmdfile.seek(0)
  869. script_was_edited = (script_content != cmdfile.read())
  870. # Do the renaming
  871. self.fm.run(['/bin/sh', cmdfile.name], flags='w')
  872. cmdfile.close()
  873. # Retag the files, but only if the script wasn't changed during review,
  874. # because only then we know which are the source and destination files.
  875. if not script_was_edited:
  876. tags_changed = False
  877. for old, new in zip(filenames, new_filenames):
  878. if old != new:
  879. oldpath = self.fm.thisdir.path + '/' + old
  880. newpath = self.fm.thisdir.path + '/' + new
  881. if oldpath in self.fm.tags:
  882. old_tag = self.fm.tags.tags[oldpath]
  883. self.fm.tags.remove(oldpath)
  884. self.fm.tags.tags[newpath] = old_tag
  885. tags_changed = True
  886. if tags_changed:
  887. self.fm.tags.dump()
  888. else:
  889. fm.notify("files have not been retagged")
  890. class relink(Command):
  891. """:relink <newpath>
  892. Changes the linked path of the currently highlighted symlink to <newpath>
  893. """
  894. def execute(self):
  895. new_path = self.rest(1)
  896. tfile = self.fm.thisfile
  897. if not new_path:
  898. return self.fm.notify('Syntax: relink <newpath>', bad=True)
  899. if not tfile.is_link:
  900. return self.fm.notify('%s is not a symlink!' % tfile.relative_path, bad=True)
  901. if new_path == os.readlink(tfile.path):
  902. return None
  903. try:
  904. os.remove(tfile.path)
  905. os.symlink(new_path, tfile.path)
  906. except OSError as err:
  907. self.fm.notify(err)
  908. self.fm.reset()
  909. self.fm.thisdir.pointed_obj = tfile
  910. self.fm.thisfile = tfile
  911. return None
  912. def tab(self, tabnum):
  913. if not self.rest(1):
  914. return self.line + os.readlink(self.fm.thisfile.path)
  915. return self._tab_directory_content()
  916. class help_(Command):
  917. """:help
  918. Display ranger's manual page.
  919. """
  920. name = 'help'
  921. def execute(self):
  922. def callback(answer):
  923. if answer == "q":
  924. return
  925. elif answer == "m":
  926. self.fm.display_help()
  927. elif answer == "c":
  928. self.fm.dump_commands()
  929. elif answer == "k":
  930. self.fm.dump_keybindings()
  931. elif answer == "s":
  932. self.fm.dump_settings()
  933. self.fm.ui.console.ask(
  934. "View [m]an page, [k]ey bindings, [c]ommands or [s]ettings? (press q to abort)",
  935. callback,
  936. list("mqkcs")
  937. )
  938. class copymap(Command):
  939. """:copymap <keys> <newkeys1> [<newkeys2>...]
  940. Copies a "browser" keybinding from <keys> to <newkeys>
  941. """
  942. context = 'browser'
  943. def execute(self):
  944. if not self.arg(1) or not self.arg(2):
  945. return self.fm.notify("Not enough arguments", bad=True)
  946. for arg in self.args[2:]:
  947. self.fm.ui.keymaps.copy(self.context, self.arg(1), arg)
  948. return None
  949. class copypmap(copymap):
  950. """:copypmap <keys> <newkeys1> [<newkeys2>...]
  951. Copies a "pager" keybinding from <keys> to <newkeys>
  952. """
  953. context = 'pager'
  954. class copycmap(copymap):
  955. """:copycmap <keys> <newkeys1> [<newkeys2>...]
  956. Copies a "console" keybinding from <keys> to <newkeys>
  957. """
  958. context = 'console'
  959. class copytmap(copymap):
  960. """:copycmap <keys> <newkeys1> [<newkeys2>...]
  961. Copies a "taskview" keybinding from <keys> to <newkeys>
  962. """
  963. context = 'taskview'
  964. class unmap(Command):
  965. """:unmap <keys> [<keys2>, ...]
  966. Remove the given "browser" mappings
  967. """
  968. context = 'browser'
  969. def execute(self):
  970. for arg in self.args[1:]:
  971. self.fm.ui.keymaps.unbind(self.context, arg)
  972. class cunmap(unmap):
  973. """:cunmap <keys> [<keys2>, ...]
  974. Remove the given "console" mappings
  975. """
  976. context = 'browser'
  977. class punmap(unmap):
  978. """:punmap <keys> [<keys2>, ...]
  979. Remove the given "pager" mappings
  980. """
  981. context = 'pager'
  982. class tunmap(unmap):
  983. """:tunmap <keys> [<keys2>, ...]
  984. Remove the given "taskview" mappings
  985. """
  986. context = 'taskview'
  987. class map_(Command):
  988. """:map <keysequence> <command>
  989. Maps a command to a keysequence in the "browser" context.
  990. Example:
  991. map j move down
  992. map J move down 10
  993. """
  994. name = 'map'
  995. context = 'browser'
  996. resolve_macros = False
  997. def execute(self):
  998. if not self.arg(1) or not self.arg(2):
  999. self.fm.notify("Syntax: {0} <keysequence> <command>".format(self.get_name()), bad=True)
  1000. return
  1001. self.fm.ui.keymaps.bind(self.context, self.arg(1), self.rest(2))
  1002. class cmap(map_):
  1003. """:cmap <keysequence> <command>
  1004. Maps a command to a keysequence in the "console" context.
  1005. Example:
  1006. cmap <ESC> console_close
  1007. cmap <C-x> console_type test
  1008. """
  1009. context = 'console'
  1010. class tmap(map_):
  1011. """:tmap <keysequence> <command>
  1012. Maps a command to a keysequence in the "taskview" context.
  1013. """
  1014. context = 'taskview'
  1015. class pmap(map_):
  1016. """:pmap <keysequence> <command>
  1017. Maps a command to a keysequence in the "pager" context.
  1018. """
  1019. context = 'pager'
  1020. class scout(Command):
  1021. """:scout [-FLAGS...] <pattern>
  1022. Swiss army knife command for searching, traveling and filtering files.
  1023. Flags:
  1024. -a Automatically open a file on unambiguous match
  1025. -e Open the selected file when pressing enter
  1026. -f Filter files that match the current search pattern
  1027. -g Interpret pattern as a glob pattern
  1028. -i Ignore the letter case of the files
  1029. -k Keep the console open when changing a directory with the command
  1030. -l Letter skipping; e.g. allow "rdme" to match the file "readme"
  1031. -m Mark the matching files after pressing enter
  1032. -M Unmark the matching files after pressing enter
  1033. -p Permanent filter: hide non-matching files after pressing enter
  1034. -r Interpret pattern as a regular expression pattern
  1035. -s Smart case; like -i unless pattern contains upper case letters
  1036. -t Apply filter and search pattern as you type
  1037. -v Inverts the match
  1038. Multiple flags can be combined. For example, ":scout -gpt" would create
  1039. a :filter-like command using globbing.
  1040. """
  1041. # pylint: disable=bad-whitespace
  1042. AUTO_OPEN = 'a'
  1043. OPEN_ON_ENTER = 'e'
  1044. FILTER = 'f'
  1045. SM_GLOB = 'g'
  1046. IGNORE_CASE = 'i'
  1047. KEEP_OPEN = 'k'
  1048. SM_LETTERSKIP = 'l'
  1049. MARK = 'm'
  1050. UNMARK = 'M'
  1051. PERM_FILTER = 'p'
  1052. SM_REGEX = 'r'
  1053. SMART_CASE = 's'
  1054. AS_YOU_TYPE = 't'
  1055. INVERT = 'v'
  1056. # pylint: enable=bad-whitespace
  1057. def __init__(self, *args, **kwargs):
  1058. super(scout, self).__init__(*args, **kwargs)
  1059. self._regex = None
  1060. self.flags, self.pattern = self.parse_flags()
  1061. def execute(self): # pylint: disable=too-many-branches
  1062. thisdir = self.fm.thisdir
  1063. flags = self.flags
  1064. pattern = self.pattern
  1065. regex = self._build_regex()
  1066. count = self._count(move=True)
  1067. self.fm.thistab.last_search = regex
  1068. self.fm.set_search_method(order="search")
  1069. if (self.MARK in flags or self.UNMARK in flags) and thisdir.files:
  1070. value = flags.find(self.MARK) > flags.find(self.UNMARK)
  1071. if self.FILTER in flags:
  1072. for fobj in thisdir.files:
  1073. thisdir.mark_item(fobj, value)
  1074. else:
  1075. for fobj in thisdir.files:
  1076. if regex.search(fobj.relative_path):
  1077. thisdir.mark_item(fobj, value)
  1078. if self.PERM_FILTER in flags:
  1079. thisdir.filter = regex if pattern else None
  1080. # clean up:
  1081. self.cancel()
  1082. if self.OPEN_ON_ENTER in flags or \
  1083. (self.AUTO_OPEN in flags and count == 1):
  1084. if pattern == '..':
  1085. self.fm.cd(pattern)
  1086. else:
  1087. self.fm.move(right=1)
  1088. if self.quickly_executed:
  1089. self.fm.block_input(0.5)
  1090. if self.KEEP_OPEN in flags and thisdir != self.fm.thisdir:
  1091. # reopen the console:
  1092. if not pattern:
  1093. self.fm.open_console(self.line)
  1094. else:
  1095. self.fm.open_console(self.line[0:-len(pattern)])
  1096. if self.quickly_executed and thisdir != self.fm.thisdir and pattern != "..":
  1097. self.fm.block_input(0.5)
  1098. def cancel(self):
  1099. self.fm.thisdir.temporary_filter = None
  1100. self.fm.thisdir.refilter()
  1101. def quick(self):
  1102. asyoutype = self.AS_YOU_TYPE in self.flags
  1103. if self.FILTER in self.flags:
  1104. self.fm.thisdir.temporary_filter = self._build_regex()
  1105. if self.PERM_FILTER in self.flags and asyoutype:
  1106. self.fm.thisdir.filter = self._build_regex()
  1107. if self.FILTER in self.flags or self.PERM_FILTER in self.flags:
  1108. self.fm.thisdir.refilter()
  1109. if self._count(move=asyoutype) == 1 and self.AUTO_OPEN in self.flags:
  1110. return True
  1111. return False
  1112. def tab(self, tabnum):
  1113. self._count(move=True, offset=tabnum)
  1114. def _build_regex(self):
  1115. if self._regex is not None:
  1116. return self._regex
  1117. frmat = "%s"
  1118. flags = self.flags
  1119. pattern = self.pattern
  1120. if pattern == ".":
  1121. return re.compile("")
  1122. # Handle carets at start and dollar signs at end separately
  1123. if pattern.startswith('^'):
  1124. pattern = pattern[1:]
  1125. frmat = "^" + frmat
  1126. if pattern.endswith('$'):
  1127. pattern = pattern[:-1]
  1128. frmat += "$"
  1129. # Apply one of the search methods
  1130. if self.SM_REGEX in flags:
  1131. regex = pattern
  1132. elif self.SM_GLOB in flags:
  1133. regex = re.escape(pattern).replace("\\*", ".*").replace("\\?", ".")
  1134. elif self.SM_LETTERSKIP in flags:
  1135. regex = ".*".join(re.escape(c) for c in pattern)
  1136. else:
  1137. regex = re.escape(pattern)
  1138. regex = frmat % regex
  1139. # Invert regular expression if necessary
  1140. if self.INVERT in flags:
  1141. regex = "^(?:(?!%s).)*$" % regex
  1142. # Compile Regular Expression
  1143. # pylint: disable=no-member
  1144. options = re.UNICODE
  1145. if self.IGNORE_CASE in flags or self.SMART_CASE in flags and \
  1146. pattern.islower():
  1147. options |= re.IGNORECASE
  1148. # pylint: enable=no-member
  1149. try:
  1150. self._regex = re.compile(regex, options)
  1151. except re.error:
  1152. self._regex = re.compile("")
  1153. return self._regex
  1154. def _count(self, move=False, offset=0):
  1155. count = 0
  1156. cwd = self.fm.thisdir
  1157. pattern = self.pattern
  1158. if not pattern or not cwd.files:
  1159. return 0
  1160. if pattern == '.':
  1161. return 0
  1162. if pattern == '..':
  1163. return 1
  1164. deq = deque(cwd.files)
  1165. deq.rotate(-cwd.pointer - offset)
  1166. i = offset
  1167. regex = self._build_regex()
  1168. for fsobj in deq:
  1169. if regex.search(fsobj.relative_path):
  1170. count += 1
  1171. if move and count == 1:
  1172. cwd.move(to=(cwd.pointer + i) % len(cwd.files))
  1173. self.fm.thisfile = cwd.pointed_obj
  1174. if count > 1:
  1175. return count
  1176. i += 1
  1177. return count == 1
  1178. class narrow(Command):
  1179. """
  1180. :narrow
  1181. Show only the files selected right now. If no files are selected,
  1182. disable narrowing.
  1183. """
  1184. def execute(self):
  1185. if self.fm.thisdir.marked_items:
  1186. selection = [f.basename for f in self.fm.thistab.get_selection()]
  1187. self.fm.thisdir.narrow_filter = selection
  1188. else:
  1189. self.fm.thisdir.narrow_filter = None
  1190. self.fm.thisdir.refilter()
  1191. class filter_inode_type(Command):
  1192. """
  1193. :filter_inode_type [dfl]
  1194. Displays only the files of specified inode type. Parameters
  1195. can be combined.
  1196. d display directories
  1197. f display files
  1198. l display links
  1199. """
  1200. def execute(self):
  1201. if not self.arg(1):
  1202. self.fm.thisdir.inode_type_filter = ""
  1203. else:
  1204. self.fm.thisdir.inode_type_filter = self.arg(1)
  1205. self.fm.thisdir.refilter()
  1206. class filter_stack(Command):
  1207. """
  1208. :filter_stack ...
  1209. Manages the filter stack.
  1210. filter_stack add FILTER_TYPE ARGS...
  1211. filter_stack pop
  1212. filter_stack decompose
  1213. filter_stack rotate [N=1]
  1214. filter_stack clear
  1215. filter_stack show
  1216. """
  1217. def execute(self):
  1218. from ranger.core.filter_stack import SIMPLE_FILTERS, FILTER_COMBINATORS
  1219. subcommand = self.arg(1)
  1220. if subcommand == "add":
  1221. try:
  1222. self.fm.thisdir.filter_stack.append(
  1223. SIMPLE_FILTERS[self.arg(2)](self.rest(3))
  1224. )
  1225. except KeyError:
  1226. FILTER_COMBINATORS[self.arg(2)](self.fm.thisdir.filter_stack)
  1227. elif subcommand == "pop":
  1228. self.fm.thisdir.filter_stack.pop()
  1229. elif subcommand == "decompose":
  1230. inner_filters = self.fm.thisdir.filter_stack.pop().decompose()
  1231. if inner_filters:
  1232. self.fm.thisdir.filter_stack.extend(inner_filters)
  1233. elif subcommand == "clear":
  1234. self.fm.thisdir.filter_stack = []
  1235. elif subcommand == "rotate":
  1236. rotate_by = int(self.arg(2) or 1)
  1237. self.fm.thisdir.filter_stack = (
  1238. self.fm.thisdir.filter_stack[-rotate_by:]
  1239. + self.fm.thisdir.filter_stack[:-rotate_by]
  1240. )
  1241. elif subcommand == "show":
  1242. stack = list(map(str, self.fm.thisdir.filter_stack))
  1243. pager = self.fm.ui.open_pager()
  1244. pager.set_source(["Filter stack: "] + stack)
  1245. pager.move(to=100, percentage=True)
  1246. return
  1247. else:
  1248. self.fm.notify(
  1249. "Unknown subcommand: {}".format(subcommand),
  1250. bad=True
  1251. )
  1252. return
  1253. self.fm.thisdir.refilter()
  1254. class grep(Command):
  1255. """:grep <string>
  1256. Looks for a string in all marked files or directories
  1257. """
  1258. def execute(self):
  1259. if self.rest(1):
  1260. action = ['grep', '--line-number']
  1261. action.extend(['-e', self.rest(1), '-r'])
  1262. action.extend(f.path for f in self.fm.thistab.get_selection())
  1263. self.fm.execute_command(action, flags='p')
  1264. class flat(Command):
  1265. """
  1266. :flat <level>
  1267. Flattens the directory view up to the specified level.
  1268. -1 fully flattened
  1269. 0 remove flattened view
  1270. """
  1271. def execute(self):
  1272. try:
  1273. level_str = self.rest(1)
  1274. level = int(level_str)
  1275. except ValueError:
  1276. level = self.quantifier
  1277. if level is None:
  1278. self.fm.notify("Syntax: flat <level>", bad=True)
  1279. return
  1280. if level < -1:
  1281. self.fm.notify("Need an integer number (-1, 0, 1, ...)", bad=True)
  1282. self.fm.thisdir.unload()
  1283. self.fm.thisdir.flat = level
  1284. self.fm.thisdir.load_content()
  1285. # Version control commands
  1286. # --------------------------------
  1287. class stage(Command):
  1288. """
  1289. :stage
  1290. Stage selected files for the corresponding version control system
  1291. """
  1292. def execute(self):
  1293. from ranger.ext.vcs import VcsError
  1294. if self.fm.thisdir.vcs and self.fm.thisdir.vcs.track:
  1295. filelist = [f.path for f in self.fm.thistab.get_selection()]
  1296. try:
  1297. self.fm.thisdir.vcs.action_add(filelist)
  1298. except VcsError as ex:
  1299. self.fm.notify('Unable to stage files: {0}'.format(ex))
  1300. self.fm.ui.vcsthread.process(self.fm.thisdir)
  1301. else:
  1302. self.fm.notify('Unable to stage files: Not in repository')
  1303. class unstage(Command):
  1304. """
  1305. :unstage
  1306. Unstage selected files for the corresponding version control system
  1307. """
  1308. def execute(self):
  1309. from ranger.ext.vcs import VcsError
  1310. if self.fm.thisdir.vcs and self.fm.thisdir.vcs.track:
  1311. filelist = [f.path for f in self.fm.thistab.get_selection()]
  1312. try:
  1313. self.fm.thisdir.vcs.action_reset(filelist)
  1314. except VcsError as ex:
  1315. self.fm.notify('Unable to unstage files: {0}'.format(ex))
  1316. self.fm.ui.vcsthread.process(self.fm.thisdir)
  1317. else:
  1318. self.fm.notify('Unable to unstage files: Not in repository')
  1319. # Metadata commands
  1320. # --------------------------------
  1321. class prompt_metadata(Command):
  1322. """
  1323. :prompt_metadata <key1> [<key2> [<key3> ...]]
  1324. Prompt the user to input metadata for multiple keys in a row.
  1325. """
  1326. _command_name = "meta"
  1327. _console_chain = None
  1328. def execute(self):
  1329. prompt_metadata._console_chain = self.args[1:]
  1330. self._process_command_stack()
  1331. def _process_command_stack(self):
  1332. if prompt_metadata._console_chain:
  1333. key = prompt_metadata._console_chain.pop()
  1334. self._fill_console(key)
  1335. else:
  1336. for col in self.fm.ui.browser.columns:
  1337. col.need_redraw = True
  1338. def _fill_console(self, key):
  1339. metadata = self.fm.metadata.get_metadata(self.fm.thisfile.path)
  1340. if key in metadata and metadata[key]:
  1341. existing_value = metadata[key]
  1342. else:
  1343. existing_value = ""
  1344. text = "%s %s %s" % (self._command_name, key, existing_value)
  1345. self.fm.open_console(text, position=len(text))
  1346. class meta(prompt_metadata):
  1347. """
  1348. :meta <key> [<value>]
  1349. Change metadata of a file. Deletes the key if value is empty.
  1350. """
  1351. def execute(self):
  1352. key = self.arg(1)
  1353. update_dict = dict()
  1354. update_dict[key] = self.rest(2)
  1355. selection = self.fm.thistab.get_selection()
  1356. for fobj in selection:
  1357. self.fm.metadata.set_metadata(fobj.path, update_dict)
  1358. self._process_command_stack()
  1359. def tab(self, tabnum):
  1360. key = self.arg(1)
  1361. metadata = self.fm.metadata.get_metadata(self.fm.thisfile.path)
  1362. if key in metadata and metadata[key]:
  1363. return [" ".join([self.arg(0), self.arg(1), metadata[key]])]
  1364. return [self.arg(0) + " " + k for k in sorted(metadata)
  1365. if k.startswith(self.arg(1))]
  1366. class linemode(default_linemode):
  1367. """
  1368. :linemode <mode>
  1369. Change what is displayed as a filename.
  1370. - "mode" may be any of the defined linemodes (see: ranger.core.linemode).
  1371. "normal" is mapped to "filename".
  1372. """
  1373. def execute(self):
  1374. mode = self.arg(1)
  1375. if mode == "normal":
  1376. from ranger.core.linemode import DEFAULT_LINEMODE
  1377. mode = DEFAULT_LINEMODE
  1378. if mode not in self.fm.thisfile.linemode_dict:
  1379. self.fm.notify("Unhandled linemode: `%s'" % mode, bad=True)
  1380. return
  1381. self.fm.thisdir.set_linemode_of_children(mode)
  1382. # Ask the browsercolumns to redraw
  1383. for col in self.fm.ui.browser.columns:
  1384. col.need_redraw = True
  1385. class yank(Command):
  1386. """:yank [name|dir|path]
  1387. Copies the file's name (default), directory or path into both the primary X
  1388. selection and the clipboard.
  1389. """
  1390. modes = {
  1391. '': 'basename',
  1392. 'name_without_extension': 'basename_without_extension',
  1393. 'name': 'basename',
  1394. 'dir': 'dirname',
  1395. 'path': 'path',
  1396. }
  1397. def execute(self):
  1398. import subprocess
  1399. def clipboards():
  1400. from ranger.ext.get_executables import get_executables
  1401. clipboard_managers = {
  1402. 'xclip': [
  1403. ['xclip'],
  1404. ['xclip', '-selection', 'clipboard'],
  1405. ],
  1406. 'xsel': [
  1407. ['xsel'],
  1408. ['xsel', '-b'],
  1409. ],
  1410. 'pbcopy': [
  1411. ['pbcopy'],
  1412. ],
  1413. }
  1414. ordered_managers = ['pbcopy', 'xclip', 'xsel']
  1415. executables = get_executables()
  1416. for manager in ordered_managers:
  1417. if manager in executables:
  1418. return clipboard_managers[manager]
  1419. return []
  1420. clipboard_commands = clipboards()
  1421. mode = self.modes[self.arg(1)]
  1422. selection = self.get_selection_attr(mode)
  1423. new_clipboard_contents = "\n".join(selection)
  1424. for command in clipboard_commands:
  1425. process = subprocess.Popen(command, universal_newlines=True,
  1426. stdin=subprocess.PIPE)
  1427. process.communicate(input=new_clipboard_contents)
  1428. def get_selection_attr(self, attr):
  1429. return [getattr(item, attr) for item in
  1430. self.fm.thistab.get_selection()]
  1431. def tab(self, tabnum):
  1432. return (
  1433. self.start(1) + mode for mode
  1434. in sorted(self.modes.keys())
  1435. if mode
  1436. )
  1437. fd_deq = deque()
  1438. class fd_search(Command):
  1439. """:fd_search [-d<depth>] <query>
  1440. Executes "fd -d<depth> <query>" in the current directory and focuses the
  1441. first match. <depth> defaults to 1, i.e. only the contents of the current
  1442. directory.
  1443. """
  1444. def execute(self):
  1445. import subprocess
  1446. from ranger.ext.get_executables import get_executables
  1447. if not 'fd' in get_executables():
  1448. self.fm.notify("Couldn't find fd on the PATH.", bad=True)
  1449. return
  1450. if self.arg(1):
  1451. if self.arg(1)[:2] == '-d':
  1452. depth = self.arg(1)
  1453. target = self.rest(2)
  1454. else:
  1455. depth = '-d1'
  1456. target = self.rest(1)
  1457. else:
  1458. self.fm.notify(":fd_search needs a query.", bad=True)
  1459. return
  1460. # For convenience, change which dict is used as result_sep to change
  1461. # fd's behavior from splitting results by \0, which allows for newlines
  1462. # in your filenames to splitting results by \n, which allows for \0 in
  1463. # filenames.
  1464. null_sep = {'arg': '-0', 'split': '\0'}
  1465. nl_sep = {'arg': '', 'split': '\n'}
  1466. result_sep = null_sep
  1467. process = subprocess.Popen(['fd', result_sep['arg'], depth, target],
  1468. universal_newlines=True, stdout=subprocess.PIPE)
  1469. (search_results, _err) = process.communicate()
  1470. global fd_deq
  1471. fd_deq = deque((self.fm.thisdir.path + os.sep + rel for rel in
  1472. sorted(search_results.split(result_sep['split']), key=str.lower)
  1473. if rel != ''))
  1474. if len(fd_deq) > 0:
  1475. self.fm.select_file(fd_deq[0])
  1476. class fd_next(Command):
  1477. """:fd_next
  1478. Selects the next match from the last :fd_search.
  1479. """
  1480. def execute(self):
  1481. if len(fd_deq) > 1:
  1482. fd_deq.rotate(-1) # rotate left
  1483. self.fm.select_file(fd_deq[0])
  1484. elif len(fd_deq) == 1:
  1485. self.fm.select_file(fd_deq[0])
  1486. class fd_prev(Command):
  1487. """:fd_prev
  1488. Selects the next match from the last :fd_search.
  1489. """
  1490. def execute(self):
  1491. if len(fd_deq) > 1:
  1492. fd_deq.rotate(1) # rotate right
  1493. self.fm.select_file(fd_deq[0])
  1494. elif len(fd_deq) == 1:
  1495. self.fm.select_file(fd_deq[0])