Lemur Pro nixos dotfiles
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.

1993 lines
61 KiB

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