My dotfiles for my Linux rice managed with stow and make
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_full.py 45KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486
  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 ~/.config/ranger/commands_full.py,
  7. # then it will NOT be loaded by ranger, and only serve as a reference.
  8. #
  9. # ===================================================================
  10. # This file contains ranger's commands.
  11. # It's all in python; lines beginning with # are comments.
  12. #
  13. # Note that additional commands are automatically generated from the methods
  14. # of the class ranger.core.actions.Actions.
  15. #
  16. # You can customize commands in the file ~/.config/ranger/commands.py.
  17. # It has the same syntax as this file. In fact, you can just copy this
  18. # file there with `ranger --copy-config=commands' and make your modifications.
  19. # But make sure you update your configs when you update ranger.
  20. #
  21. # ===================================================================
  22. # Every class defined here which is a subclass of `Command' will be used as a
  23. # command in ranger. Several methods are defined to interface with ranger:
  24. # execute(): called when the command is executed.
  25. # cancel(): called when closing the console.
  26. # tab(tabnum): called when <TAB> is pressed.
  27. # quick(): called after each keypress.
  28. #
  29. # tab() argument tabnum is 1 for <TAB> and -1 for <S-TAB> by default
  30. #
  31. # The return values for tab() can be either:
  32. # None: There is no tab completion
  33. # A string: Change the console to this string
  34. # A list/tuple/generator: cycle through every item in it
  35. #
  36. # The return value for quick() can be:
  37. # False: Nothing happens
  38. # True: Execute the command afterwards
  39. #
  40. # The return value for execute() and cancel() doesn't matter.
  41. #
  42. # ===================================================================
  43. # Commands have certain attributes and methods that facilitate parsing of
  44. # the arguments:
  45. #
  46. # self.line: The whole line that was written in the console.
  47. # self.args: A list of all (space-separated) arguments to the command.
  48. # self.quantifier: If this command was mapped to the key "X" and
  49. # the user pressed 6X, self.quantifier will be 6.
  50. # self.arg(n): The n-th argument, or an empty string if it doesn't exist.
  51. # self.rest(n): The n-th argument plus everything that followed. For example,
  52. # if the command was "search foo bar a b c", rest(2) will be "bar a b c"
  53. # self.start(n): Anything before the n-th argument. For example, if the
  54. # command was "search foo bar a b c", start(2) will be "search foo"
  55. #
  56. # ===================================================================
  57. # And this is a little reference for common ranger functions and objects:
  58. #
  59. # self.fm: A reference to the "fm" object which contains most information
  60. # about ranger.
  61. # self.fm.notify(string): Print the given string on the screen.
  62. # self.fm.notify(string, bad=True): Print the given string in RED.
  63. # self.fm.reload_cwd(): Reload the current working directory.
  64. # self.fm.thisdir: The current working directory. (A File object.)
  65. # self.fm.thisfile: The current file. (A File object too.)
  66. # self.fm.thistab.get_selection(): A list of all selected files.
  67. # self.fm.execute_console(string): Execute the string as a ranger command.
  68. # self.fm.open_console(string): Open the console with the given string
  69. # already typed in for you.
  70. # self.fm.move(direction): Moves the cursor in the given direction, which
  71. # can be something like down=3, up=5, right=1, left=1, to=6, ...
  72. #
  73. # File objects (for example self.fm.thisfile) have these useful attributes and
  74. # methods:
  75. #
  76. # cf.path: The path to the file.
  77. # cf.basename: The base name only.
  78. # cf.load_content(): Force a loading of the directories content (which
  79. # obviously works with directories only)
  80. # cf.is_directory: True/False depending on whether it's a directory.
  81. #
  82. # For advanced commands it is unavoidable to dive a bit into the source code
  83. # of ranger.
  84. # ===================================================================
  85. from ranger.api.commands import *
  86. class alias(Command):
  87. """:alias <newcommand> <oldcommand>
  88. Copies the oldcommand as newcommand.
  89. """
  90. context = 'browser'
  91. resolve_macros = False
  92. def execute(self):
  93. if not self.arg(1) or not self.arg(2):
  94. self.fm.notify('Syntax: alias <newcommand> <oldcommand>', bad=True)
  95. else:
  96. self.fm.commands.alias(self.arg(1), self.rest(2))
  97. class echo(Command):
  98. """:echo <text>
  99. Display the text in the statusbar.
  100. """
  101. def execute(self):
  102. self.fm.notify(self.rest(1))
  103. class cd(Command):
  104. """:cd [-r] <dirname>
  105. The cd command changes the directory.
  106. The command 'cd -' is equivalent to typing ``.
  107. Using the option "-r" will get you to the real path.
  108. """
  109. def execute(self):
  110. import os.path
  111. if self.arg(1) == '-r':
  112. self.shift()
  113. destination = os.path.realpath(self.rest(1))
  114. if os.path.isfile(destination):
  115. self.fm.select_file(destination)
  116. return
  117. else:
  118. destination = self.rest(1)
  119. if not destination:
  120. destination = '~'
  121. if destination == '-':
  122. self.fm.enter_bookmark('`')
  123. else:
  124. self.fm.cd(destination)
  125. def tab(self, tabnum):
  126. import os
  127. from os.path import dirname, basename, expanduser, join
  128. cwd = self.fm.thisdir.path
  129. rel_dest = self.rest(1)
  130. bookmarks = [v.path for v in self.fm.bookmarks.dct.values()
  131. if rel_dest in v.path]
  132. # expand the tilde into the user directory
  133. if rel_dest.startswith('~'):
  134. rel_dest = expanduser(rel_dest)
  135. # define some shortcuts
  136. abs_dest = join(cwd, rel_dest)
  137. abs_dirname = dirname(abs_dest)
  138. rel_basename = basename(rel_dest)
  139. rel_dirname = dirname(rel_dest)
  140. try:
  141. # are we at the end of a directory?
  142. if rel_dest.endswith('/') or rel_dest == '':
  143. _, dirnames, _ = next(os.walk(abs_dest))
  144. # are we in the middle of the filename?
  145. else:
  146. _, dirnames, _ = next(os.walk(abs_dirname))
  147. dirnames = [dn for dn in dirnames
  148. if dn.startswith(rel_basename)]
  149. except (OSError, StopIteration):
  150. # os.walk found nothing
  151. pass
  152. else:
  153. dirnames.sort()
  154. if self.fm.settings.cd_bookmarks:
  155. dirnames = bookmarks + dirnames
  156. # no results, return None
  157. if len(dirnames) == 0:
  158. return
  159. # one result. since it must be a directory, append a slash.
  160. if len(dirnames) == 1:
  161. return self.start(1) + join(rel_dirname, dirnames[0]) + '/'
  162. # more than one result. append no slash, so the user can
  163. # manually type in the slash to advance into that directory
  164. return (self.start(1) + join(rel_dirname, dirname) for dirname in dirnames)
  165. class chain(Command):
  166. """:chain <command1>; <command2>; ...
  167. Calls multiple commands at once, separated by semicolons.
  168. """
  169. def execute(self):
  170. for command in [s.strip() for s in self.rest(1).split(";")]:
  171. self.fm.execute_console(command)
  172. class shell(Command):
  173. escape_macros_for_shell = True
  174. def execute(self):
  175. if self.arg(1) and self.arg(1)[0] == '-':
  176. flags = self.arg(1)[1:]
  177. command = self.rest(2)
  178. else:
  179. flags = ''
  180. command = self.rest(1)
  181. if command:
  182. self.fm.execute_command(command, flags=flags)
  183. def tab(self, tabnum):
  184. from ranger.ext.get_executables import get_executables
  185. if self.arg(1) and self.arg(1)[0] == '-':
  186. command = self.rest(2)
  187. else:
  188. command = self.rest(1)
  189. start = self.line[0:len(self.line) - len(command)]
  190. try:
  191. position_of_last_space = command.rindex(" ")
  192. except ValueError:
  193. return (start + program + ' ' for program
  194. in get_executables() if program.startswith(command))
  195. if position_of_last_space == len(command) - 1:
  196. selection = self.fm.thistab.get_selection()
  197. if len(selection) == 1:
  198. return self.line + selection[0].shell_escaped_basename + ' '
  199. else:
  200. return self.line + '%s '
  201. else:
  202. before_word, start_of_word = self.line.rsplit(' ', 1)
  203. return (before_word + ' ' + file.shell_escaped_basename
  204. for file in self.fm.thisdir.files or []
  205. if file.shell_escaped_basename.startswith(start_of_word))
  206. class open_with(Command):
  207. def execute(self):
  208. app, flags, mode = self._get_app_flags_mode(self.rest(1))
  209. self.fm.execute_file(
  210. files=[f for f in self.fm.thistab.get_selection()],
  211. app=app,
  212. flags=flags,
  213. mode=mode)
  214. def tab(self, tabnum):
  215. return self._tab_through_executables()
  216. def _get_app_flags_mode(self, string):
  217. """Extracts the application, flags and mode from a string.
  218. examples:
  219. "mplayer f 1" => ("mplayer", "f", 1)
  220. "aunpack 4" => ("aunpack", "", 4)
  221. "p" => ("", "p", 0)
  222. "" => None
  223. """
  224. app = ''
  225. flags = ''
  226. mode = 0
  227. split = string.split()
  228. if len(split) == 0:
  229. pass
  230. elif len(split) == 1:
  231. part = split[0]
  232. if self._is_app(part):
  233. app = part
  234. elif self._is_flags(part):
  235. flags = part
  236. elif self._is_mode(part):
  237. mode = part
  238. elif len(split) == 2:
  239. part0 = split[0]
  240. part1 = split[1]
  241. if self._is_app(part0):
  242. app = part0
  243. if self._is_flags(part1):
  244. flags = part1
  245. elif self._is_mode(part1):
  246. mode = part1
  247. elif self._is_flags(part0):
  248. flags = part0
  249. if self._is_mode(part1):
  250. mode = part1
  251. elif self._is_mode(part0):
  252. mode = part0
  253. if self._is_flags(part1):
  254. flags = part1
  255. elif len(split) >= 3:
  256. part0 = split[0]
  257. part1 = split[1]
  258. part2 = split[2]
  259. if self._is_app(part0):
  260. app = part0
  261. if self._is_flags(part1):
  262. flags = part1
  263. if self._is_mode(part2):
  264. mode = part2
  265. elif self._is_mode(part1):
  266. mode = part1
  267. if self._is_flags(part2):
  268. flags = part2
  269. elif self._is_flags(part0):
  270. flags = part0
  271. if self._is_mode(part1):
  272. mode = part1
  273. elif self._is_mode(part0):
  274. mode = part0
  275. if self._is_flags(part1):
  276. flags = part1
  277. return app, flags, int(mode)
  278. def _is_app(self, arg):
  279. return not self._is_flags(arg) and not arg.isdigit()
  280. def _is_flags(self, arg):
  281. from ranger.core.runner import ALLOWED_FLAGS
  282. return all(x in ALLOWED_FLAGS for x in arg)
  283. def _is_mode(self, arg):
  284. return all(x in '0123456789' for x in arg)
  285. class set_(Command):
  286. """:set <option name>=<python expression>
  287. Gives an option a new value.
  288. Use `:set <option>!` to toggle or cycle it, e.g. `:set flush_input!`
  289. """
  290. name = 'set' # don't override the builtin set class
  291. def execute(self):
  292. name = self.arg(1)
  293. name, value, _, toggle = self.parse_setting_line_v2()
  294. if toggle:
  295. self.fm.toggle_option(name)
  296. else:
  297. self.fm.set_option_from_string(name, value)
  298. def tab(self, tabnum):
  299. from ranger.gui.colorscheme import get_all_colorschemes
  300. name, value, name_done = self.parse_setting_line()
  301. settings = self.fm.settings
  302. if not name:
  303. return sorted(self.firstpart + setting for setting in settings)
  304. if not value and not name_done:
  305. return sorted(self.firstpart + setting for setting in settings
  306. if setting.startswith(name))
  307. if not value:
  308. # Cycle through colorschemes when name, but no value is specified
  309. if name == "colorscheme":
  310. return sorted(self.firstpart + colorscheme for colorscheme
  311. in get_all_colorschemes())
  312. return self.firstpart + str(settings[name])
  313. if bool in settings.types_of(name):
  314. if 'true'.startswith(value.lower()):
  315. return self.firstpart + 'True'
  316. if 'false'.startswith(value.lower()):
  317. return self.firstpart + 'False'
  318. # Tab complete colorscheme values if incomplete value is present
  319. if name == "colorscheme":
  320. return sorted(self.firstpart + colorscheme for colorscheme
  321. in get_all_colorschemes() if colorscheme.startswith(value))
  322. class setlocal(set_):
  323. """:setlocal path=<regular expression> <option name>=<python expression>
  324. Gives an option a new value.
  325. """
  326. PATH_RE = re.compile(r'^\s*path="?(.*?)"?\s*$')
  327. def execute(self):
  328. import os.path
  329. match = self.PATH_RE.match(self.arg(1))
  330. if match:
  331. path = os.path.normpath(os.path.expanduser(match.group(1)))
  332. self.shift()
  333. elif self.fm.thisdir:
  334. path = self.fm.thisdir.path
  335. else:
  336. path = None
  337. if path:
  338. name = self.arg(1)
  339. name, value, _ = self.parse_setting_line()
  340. self.fm.set_option_from_string(name, value, localpath=path)
  341. class setintag(setlocal):
  342. """:setintag <tag or tags> <option name>=<option value>
  343. Sets an option for directories that are tagged with a specific tag.
  344. """
  345. def execute(self):
  346. tags = self.arg(1)
  347. self.shift()
  348. name, value, _ = self.parse_setting_line()
  349. self.fm.set_option_from_string(name, value, tags=tags)
  350. class default_linemode(Command):
  351. def execute(self):
  352. import re
  353. from ranger.container.fsobject import FileSystemObject
  354. if len(self.args) < 2:
  355. self.fm.notify("Usage: default_linemode [path=<regexp> | tag=<tag(s)>] <linemode>", bad=True)
  356. # Extract options like "path=..." or "tag=..." from the command line
  357. arg1 = self.arg(1)
  358. method = "always"
  359. argument = None
  360. if arg1.startswith("path="):
  361. method = "path"
  362. argument = re.compile(arg1[5:])
  363. self.shift()
  364. elif arg1.startswith("tag="):
  365. method = "tag"
  366. argument = arg1[4:]
  367. self.shift()
  368. # Extract and validate the line mode from the command line
  369. linemode = self.rest(1)
  370. if linemode not in FileSystemObject.linemode_dict:
  371. self.fm.notify("Invalid linemode: %s; should be %s" %
  372. (linemode, "/".join(FileSystemObject.linemode_dict)), bad=True)
  373. # Add the prepared entry to the fm.default_linemodes
  374. entry = [method, argument, linemode]
  375. self.fm.default_linemodes.appendleft(entry)
  376. # Redraw the columns
  377. if hasattr(self.fm.ui, "browser"):
  378. for col in self.fm.ui.browser.columns:
  379. col.need_redraw = True
  380. def tab(self, tabnum):
  381. mode = self.arg(1)
  382. return (self.arg(0) + " " + linemode
  383. for linemode in self.fm.thisfile.linemode_dict.keys()
  384. if linemode.startswith(self.arg(1)))
  385. class quit(Command):
  386. """:quit
  387. Closes the current tab. If there is only one tab, quit the program.
  388. """
  389. def execute(self):
  390. if len(self.fm.tabs) <= 1:
  391. self.fm.exit()
  392. self.fm.tab_close()
  393. class quitall(Command):
  394. """:quitall
  395. Quits the program immediately.
  396. """
  397. def execute(self):
  398. self.fm.exit()
  399. class quit_bang(quitall):
  400. """:quit!
  401. Quits the program immediately.
  402. """
  403. name = 'quit!'
  404. allow_abbrev = False
  405. class terminal(Command):
  406. """:terminal
  407. Spawns an "x-terminal-emulator" starting in the current directory.
  408. """
  409. def execute(self):
  410. from ranger.ext.get_executables import get_term
  411. self.fm.run(get_term(), flags='f')
  412. class delete(Command):
  413. """:delete
  414. Tries to delete the selection or the files passed in arguments (if any).
  415. The arguments use a shell-like escaping.
  416. "Selection" is defined as all the "marked files" (by default, you
  417. can mark files with space or v). If there are no marked files,
  418. use the "current file" (where the cursor is)
  419. When attempting to delete non-empty directories or multiple
  420. marked files, it will require a confirmation.
  421. """
  422. allow_abbrev = False
  423. escape_macros_for_shell = True
  424. def execute(self):
  425. import os
  426. import shlex
  427. from functools import partial
  428. from ranger.container.file import File
  429. def is_directory_with_files(f):
  430. import os.path
  431. return (os.path.isdir(f) and not os.path.islink(f)
  432. and len(os.listdir(f)) > 0)
  433. if self.rest(1):
  434. files = shlex.split(self.rest(1))
  435. many_files = (len(files) > 1 or is_directory_with_files(files[0]))
  436. else:
  437. cwd = self.fm.thisdir
  438. cf = self.fm.thisfile
  439. if not cwd or not cf:
  440. self.fm.notify("Error: no file selected for deletion!", bad=True)
  441. return
  442. # relative_path used for a user-friendly output in the confirmation.
  443. files = [f.relative_path for f in self.fm.thistab.get_selection()]
  444. many_files = (cwd.marked_items or is_directory_with_files(cf.path))
  445. confirm = self.fm.settings.confirm_on_delete
  446. if confirm != 'never' and (confirm != 'multiple' or many_files):
  447. filename_list = files
  448. self.fm.ui.console.ask("Confirm deletion of: %s (y/N)" %
  449. ', '.join(files),
  450. partial(self._question_callback, files), ('n', 'N', 'y', 'Y'))
  451. else:
  452. # no need for a confirmation, just delete
  453. self.fm.delete(files)
  454. def tab(self, tabnum):
  455. return self._tab_directory_content()
  456. def _question_callback(self, files, answer):
  457. if answer == 'y' or answer == 'Y':
  458. self.fm.delete(files)
  459. class mark_tag(Command):
  460. """:mark_tag [<tags>]
  461. Mark all tags that are tagged with either of the given tags.
  462. When leaving out the tag argument, all tagged files are marked.
  463. """
  464. do_mark = True
  465. def execute(self):
  466. cwd = self.fm.thisdir
  467. tags = self.rest(1).replace(" ", "")
  468. if not self.fm.tags or not cwd.files:
  469. return
  470. for fileobj in cwd.files:
  471. try:
  472. tag = self.fm.tags.tags[fileobj.realpath]
  473. except KeyError:
  474. continue
  475. if not tags or tag in tags:
  476. cwd.mark_item(fileobj, val=self.do_mark)
  477. self.fm.ui.status.need_redraw = True
  478. self.fm.ui.need_redraw = True
  479. class console(Command):
  480. """:console <command>
  481. Open the console with the given command.
  482. """
  483. def execute(self):
  484. position = None
  485. if self.arg(1)[0:2] == '-p':
  486. try:
  487. position = int(self.arg(1)[2:])
  488. self.shift()
  489. except Exception:
  490. pass
  491. self.fm.open_console(self.rest(1), position=position)
  492. class load_copy_buffer(Command):
  493. """:load_copy_buffer
  494. Load the copy buffer from confdir/copy_buffer
  495. """
  496. copy_buffer_filename = 'copy_buffer'
  497. def execute(self):
  498. from ranger.container.file import File
  499. from os.path import exists
  500. try:
  501. fname = self.fm.confpath(self.copy_buffer_filename)
  502. f = open(fname, 'r')
  503. except Exception:
  504. return self.fm.notify("Cannot open %s" %
  505. (fname or self.copy_buffer_filename), bad=True)
  506. self.fm.copy_buffer = set(File(g)
  507. for g in f.read().split("\n") if exists(g))
  508. f.close()
  509. self.fm.ui.redraw_main_column()
  510. class save_copy_buffer(Command):
  511. """:save_copy_buffer
  512. Save the copy buffer to confdir/copy_buffer
  513. """
  514. copy_buffer_filename = 'copy_buffer'
  515. def execute(self):
  516. fname = None
  517. try:
  518. fname = self.fm.confpath(self.copy_buffer_filename)
  519. f = open(fname, 'w')
  520. except Exception:
  521. return self.fm.notify("Cannot open %s" %
  522. (fname or self.copy_buffer_filename), bad=True)
  523. f.write("\n".join(f.path for f in self.fm.copy_buffer))
  524. f.close()
  525. class unmark_tag(mark_tag):
  526. """:unmark_tag [<tags>]
  527. Unmark all tags that are tagged with either of the given tags.
  528. When leaving out the tag argument, all tagged files are unmarked.
  529. """
  530. do_mark = False
  531. class mkdir(Command):
  532. """:mkdir <dirname>
  533. Creates a directory with the name <dirname>.
  534. """
  535. def execute(self):
  536. from os.path import join, expanduser, lexists
  537. from os import makedirs
  538. dirname = join(self.fm.thisdir.path, expanduser(self.rest(1)))
  539. if not lexists(dirname):
  540. makedirs(dirname)
  541. else:
  542. self.fm.notify("file/directory exists!", bad=True)
  543. def tab(self, tabnum):
  544. return self._tab_directory_content()
  545. class touch(Command):
  546. """:touch <fname>
  547. Creates a file with the name <fname>.
  548. """
  549. def execute(self):
  550. from os.path import join, expanduser, lexists
  551. fname = join(self.fm.thisdir.path, expanduser(self.rest(1)))
  552. if not lexists(fname):
  553. open(fname, 'a').close()
  554. else:
  555. self.fm.notify("file/directory exists!", bad=True)
  556. def tab(self, tabnum):
  557. return self._tab_directory_content()
  558. class edit(Command):
  559. """:edit <filename>
  560. Opens the specified file in vim
  561. """
  562. def execute(self):
  563. if not self.arg(1):
  564. self.fm.edit_file(self.fm.thisfile.path)
  565. else:
  566. self.fm.edit_file(self.rest(1))
  567. def tab(self, tabnum):
  568. return self._tab_directory_content()
  569. class eval_(Command):
  570. """:eval [-q] <python code>
  571. Evaluates the python code.
  572. `fm' is a reference to the FM instance.
  573. To display text, use the function `p'.
  574. Examples:
  575. :eval fm
  576. :eval len(fm.directories)
  577. :eval p("Hello World!")
  578. """
  579. name = 'eval'
  580. resolve_macros = False
  581. def execute(self):
  582. if self.arg(1) == '-q':
  583. code = self.rest(2)
  584. quiet = True
  585. else:
  586. code = self.rest(1)
  587. quiet = False
  588. import ranger
  589. global cmd, fm, p, quantifier
  590. fm = self.fm
  591. cmd = self.fm.execute_console
  592. p = fm.notify
  593. quantifier = self.quantifier
  594. try:
  595. try:
  596. result = eval(code)
  597. except SyntaxError:
  598. exec(code)
  599. else:
  600. if result and not quiet:
  601. p(result)
  602. except Exception as err:
  603. p(err)
  604. class rename(Command):
  605. """:rename <newname>
  606. Changes the name of the currently highlighted file to <newname>
  607. """
  608. def execute(self):
  609. from ranger.container.file import File
  610. from os import access
  611. new_name = self.rest(1)
  612. tagged = {}
  613. old_name = self.fm.thisfile.relative_path
  614. for f in self.fm.tags.tags:
  615. if str(f).startswith(self.fm.thisfile.path):
  616. tagged[f] = self.fm.tags.tags[f]
  617. self.fm.tags.remove(f)
  618. if not new_name:
  619. return self.fm.notify('Syntax: rename <newname>', bad=True)
  620. if new_name == old_name:
  621. return
  622. if access(new_name, os.F_OK):
  623. return self.fm.notify("Can't rename: file already exists!", bad=True)
  624. if self.fm.rename(self.fm.thisfile, new_name):
  625. f = File(new_name)
  626. # Update bookmarks that were pointing on the previous name
  627. obsoletebookmarks = [b for b in self.fm.bookmarks
  628. if b[1].path == self.fm.thisfile]
  629. if obsoletebookmarks:
  630. for key, _ in obsoletebookmarks:
  631. self.fm.bookmarks[key] = f
  632. self.fm.bookmarks.update_if_outdated()
  633. self.fm.thisdir.pointed_obj = f
  634. self.fm.thisfile = f
  635. for t in tagged:
  636. self.fm.tags.tags[t.replace(old_name, new_name)] = tagged[t]
  637. self.fm.tags.dump()
  638. def tab(self, tabnum):
  639. return self._tab_directory_content()
  640. class rename_append(Command):
  641. """:rename_append
  642. Creates an open_console for the rename command, automatically placing the cursor before the file extension.
  643. """
  644. def execute(self):
  645. cf = self.fm.thisfile
  646. path = cf.relative_path.replace("%", "%%")
  647. if path.find('.') != 0 and path.rfind('.') != -1 and not cf.is_directory:
  648. self.fm.open_console('rename ' + path, position=(7 + path.rfind('.')))
  649. else:
  650. self.fm.open_console('rename ' + path)
  651. class chmod(Command):
  652. """:chmod <octal number>
  653. Sets the permissions of the selection to the octal number.
  654. The octal number is between 0 and 777. The digits specify the
  655. permissions for the user, the group and others.
  656. A 1 permits execution, a 2 permits writing, a 4 permits reading.
  657. Add those numbers to combine them. So a 7 permits everything.
  658. """
  659. def execute(self):
  660. mode = self.rest(1)
  661. if not mode:
  662. mode = str(self.quantifier)
  663. try:
  664. mode = int(mode, 8)
  665. if mode < 0 or mode > 0o777:
  666. raise ValueError
  667. except ValueError:
  668. self.fm.notify("Need an octal number between 0 and 777!", bad=True)
  669. return
  670. for file in self.fm.thistab.get_selection():
  671. try:
  672. os.chmod(file.path, mode)
  673. except Exception as ex:
  674. self.fm.notify(ex)
  675. try:
  676. # reloading directory. maybe its better to reload the selected
  677. # files only.
  678. self.fm.thisdir.load_content()
  679. except Exception:
  680. pass
  681. class bulkrename(Command):
  682. """:bulkrename
  683. This command opens a list of selected files in an external editor.
  684. After you edit and save the file, it will generate a shell script
  685. which does bulk renaming according to the changes you did in the file.
  686. This shell script is opened in an editor for you to review.
  687. After you close it, it will be executed.
  688. """
  689. def execute(self):
  690. import sys
  691. import tempfile
  692. from ranger.container.file import File
  693. from ranger.ext.shell_escape import shell_escape as esc
  694. py3 = sys.version_info[0] >= 3
  695. # Create and edit the file list
  696. filenames = [f.relative_path for f in self.fm.thistab.get_selection()]
  697. listfile = tempfile.NamedTemporaryFile(delete=False)
  698. listpath = listfile.name
  699. if py3:
  700. listfile.write("\n".join(filenames).encode("utf-8"))
  701. else:
  702. listfile.write("\n".join(filenames))
  703. listfile.close()
  704. self.fm.execute_file([File(listpath)], app='editor')
  705. listfile = open(listpath, 'r')
  706. new_filenames = listfile.read().split("\n")
  707. listfile.close()
  708. os.unlink(listpath)
  709. if all(a == b for a, b in zip(filenames, new_filenames)):
  710. self.fm.notify("No renaming to be done!")
  711. return
  712. # Generate script
  713. cmdfile = tempfile.NamedTemporaryFile()
  714. script_lines = []
  715. script_lines.append("# This file will be executed when you close the editor.\n")
  716. script_lines.append("# Please double-check everything, clear the file to abort.\n")
  717. script_lines.extend("mv -vi -- %s %s\n" % (esc(old), esc(new))
  718. for old, new in zip(filenames, new_filenames) if old != new)
  719. script_content = "".join(script_lines)
  720. if py3:
  721. cmdfile.write(script_content.encode("utf-8"))
  722. else:
  723. cmdfile.write(script_content)
  724. cmdfile.flush()
  725. # Open the script and let the user review it, then check if the script
  726. # was modified by the user
  727. self.fm.execute_file([File(cmdfile.name)], app='editor')
  728. cmdfile.seek(0)
  729. script_was_edited = (script_content != cmdfile.read())
  730. # Do the renaming
  731. self.fm.run(['/bin/sh', cmdfile.name], flags='w')
  732. cmdfile.close()
  733. # Retag the files, but only if the script wasn't changed during review,
  734. # because only then we know which are the source and destination files.
  735. if not script_was_edited:
  736. tags_changed = False
  737. for old, new in zip(filenames, new_filenames):
  738. if old != new:
  739. oldpath = self.fm.thisdir.path + '/' + old
  740. newpath = self.fm.thisdir.path + '/' + new
  741. if oldpath in self.fm.tags:
  742. old_tag = self.fm.tags.tags[oldpath]
  743. self.fm.tags.remove(oldpath)
  744. self.fm.tags.tags[newpath] = old_tag
  745. tags_changed = True
  746. if tags_changed:
  747. self.fm.tags.dump()
  748. else:
  749. fm.notify("files have not been retagged")
  750. class relink(Command):
  751. """:relink <newpath>
  752. Changes the linked path of the currently highlighted symlink to <newpath>
  753. """
  754. def execute(self):
  755. from ranger.container.file import File
  756. new_path = self.rest(1)
  757. cf = self.fm.thisfile
  758. if not new_path:
  759. return self.fm.notify('Syntax: relink <newpath>', bad=True)
  760. if not cf.is_link:
  761. return self.fm.notify('%s is not a symlink!' % cf.relative_path, bad=True)
  762. if new_path == os.readlink(cf.path):
  763. return
  764. try:
  765. os.remove(cf.path)
  766. os.symlink(new_path, cf.path)
  767. except OSError as err:
  768. self.fm.notify(err)
  769. self.fm.reset()
  770. self.fm.thisdir.pointed_obj = cf
  771. self.fm.thisfile = cf
  772. def tab(self, tabnum):
  773. if not self.rest(1):
  774. return self.line + os.readlink(self.fm.thisfile.path)
  775. else:
  776. return self._tab_directory_content()
  777. class help_(Command):
  778. """:help
  779. Display ranger's manual page.
  780. """
  781. name = 'help'
  782. def execute(self):
  783. def callback(answer):
  784. if answer == "q":
  785. return
  786. elif answer == "m":
  787. self.fm.display_help()
  788. elif answer == "c":
  789. self.fm.dump_commands()
  790. elif answer == "k":
  791. self.fm.dump_keybindings()
  792. elif answer == "s":
  793. self.fm.dump_settings()
  794. c = self.fm.ui.console.ask("View [m]an page, [k]ey bindings,"
  795. " [c]ommands or [s]ettings? (press q to abort)", callback, list("mkcsq") + [chr(27)])
  796. class copymap(Command):
  797. """:copymap <keys> <newkeys1> [<newkeys2>...]
  798. Copies a "browser" keybinding from <keys> to <newkeys>
  799. """
  800. context = 'browser'
  801. def execute(self):
  802. if not self.arg(1) or not self.arg(2):
  803. return self.fm.notify("Not enough arguments", bad=True)
  804. for arg in self.args[2:]:
  805. self.fm.ui.keymaps.copy(self.context, self.arg(1), arg)
  806. class copypmap(copymap):
  807. """:copypmap <keys> <newkeys1> [<newkeys2>...]
  808. Copies a "pager" keybinding from <keys> to <newkeys>
  809. """
  810. context = 'pager'
  811. class copycmap(copymap):
  812. """:copycmap <keys> <newkeys1> [<newkeys2>...]
  813. Copies a "console" keybinding from <keys> to <newkeys>
  814. """
  815. context = 'console'
  816. class copytmap(copymap):
  817. """:copycmap <keys> <newkeys1> [<newkeys2>...]
  818. Copies a "taskview" keybinding from <keys> to <newkeys>
  819. """
  820. context = 'taskview'
  821. class unmap(Command):
  822. """:unmap <keys> [<keys2>, ...]
  823. Remove the given "browser" mappings
  824. """
  825. context = 'browser'
  826. def execute(self):
  827. for arg in self.args[1:]:
  828. self.fm.ui.keymaps.unbind(self.context, arg)
  829. class cunmap(unmap):
  830. """:cunmap <keys> [<keys2>, ...]
  831. Remove the given "console" mappings
  832. """
  833. context = 'browser'
  834. class punmap(unmap):
  835. """:punmap <keys> [<keys2>, ...]
  836. Remove the given "pager" mappings
  837. """
  838. context = 'pager'
  839. class tunmap(unmap):
  840. """:tunmap <keys> [<keys2>, ...]
  841. Remove the given "taskview" mappings
  842. """
  843. context = 'taskview'
  844. class map_(Command):
  845. """:map <keysequence> <command>
  846. Maps a command to a keysequence in the "browser" context.
  847. Example:
  848. map j move down
  849. map J move down 10
  850. """
  851. name = 'map'
  852. context = 'browser'
  853. resolve_macros = False
  854. def execute(self):
  855. if not self.arg(1) or not self.arg(2):
  856. return self.fm.notify("Not enough arguments", bad=True)
  857. self.fm.ui.keymaps.bind(self.context, self.arg(1), self.rest(2))
  858. class cmap(map_):
  859. """:cmap <keysequence> <command>
  860. Maps a command to a keysequence in the "console" context.
  861. Example:
  862. cmap <ESC> console_close
  863. cmap <C-x> console_type test
  864. """
  865. context = 'console'
  866. class tmap(map_):
  867. """:tmap <keysequence> <command>
  868. Maps a command to a keysequence in the "taskview" context.
  869. """
  870. context = 'taskview'
  871. class pmap(map_):
  872. """:pmap <keysequence> <command>
  873. Maps a command to a keysequence in the "pager" context.
  874. """
  875. context = 'pager'
  876. class scout(Command):
  877. """:scout [-FLAGS] <pattern>
  878. Swiss army knife command for searching, traveling and filtering files.
  879. The command takes various flags as arguments which can be used to
  880. influence its behaviour:
  881. -a = automatically open a file on unambiguous match
  882. -e = open the selected file when pressing enter
  883. -f = filter files that match the current search pattern
  884. -g = interpret pattern as a glob pattern
  885. -i = ignore the letter case of the files
  886. -k = keep the console open when changing a directory with the command
  887. -l = letter skipping; e.g. allow "rdme" to match the file "readme"
  888. -m = mark the matching files after pressing enter
  889. -M = unmark the matching files after pressing enter
  890. -p = permanent filter: hide non-matching files after pressing enter
  891. -r = interpret pattern as a regular expression pattern
  892. -s = smart case; like -i unless pattern contains upper case letters
  893. -t = apply filter and search pattern as you type
  894. -v = inverts the match
  895. Multiple flags can be combined. For example, ":scout -gpt" would create
  896. a :filter-like command using globbing.
  897. """
  898. AUTO_OPEN = 'a'
  899. OPEN_ON_ENTER = 'e'
  900. FILTER = 'f'
  901. SM_GLOB = 'g'
  902. IGNORE_CASE = 'i'
  903. KEEP_OPEN = 'k'
  904. SM_LETTERSKIP = 'l'
  905. MARK = 'm'
  906. UNMARK = 'M'
  907. PERM_FILTER = 'p'
  908. SM_REGEX = 'r'
  909. SMART_CASE = 's'
  910. AS_YOU_TYPE = 't'
  911. INVERT = 'v'
  912. def __init__(self, *args, **kws):
  913. Command.__init__(self, *args, **kws)
  914. self._regex = None
  915. self.flags, self.pattern = self.parse_flags()
  916. def execute(self):
  917. thisdir = self.fm.thisdir
  918. flags = self.flags
  919. pattern = self.pattern
  920. regex = self._build_regex()
  921. count = self._count(move=True)
  922. self.fm.thistab.last_search = regex
  923. self.fm.set_search_method(order="search")
  924. if (self.MARK in flags or self.UNMARK in flags) and thisdir.files:
  925. value = flags.find(self.MARK) > flags.find(self.UNMARK)
  926. if self.FILTER in flags:
  927. for f in thisdir.files:
  928. thisdir.mark_item(f, value)
  929. else:
  930. for f in thisdir.files:
  931. if regex.search(f.relative_path):
  932. thisdir.mark_item(f, value)
  933. if self.PERM_FILTER in flags:
  934. thisdir.filter = regex if pattern else None
  935. # clean up:
  936. self.cancel()
  937. if self.OPEN_ON_ENTER in flags or \
  938. self.AUTO_OPEN in flags and count == 1:
  939. if os.path.exists(pattern):
  940. self.fm.cd(pattern)
  941. else:
  942. self.fm.move(right=1)
  943. if self.KEEP_OPEN in flags and thisdir != self.fm.thisdir:
  944. # reopen the console:
  945. if not pattern:
  946. self.fm.open_console(self.line)
  947. else:
  948. self.fm.open_console(self.line[0:-len(pattern)])
  949. if self.quickly_executed and thisdir != self.fm.thisdir and pattern != "..":
  950. self.fm.block_input(0.5)
  951. def cancel(self):
  952. self.fm.thisdir.temporary_filter = None
  953. self.fm.thisdir.refilter()
  954. def quick(self):
  955. asyoutype = self.AS_YOU_TYPE in self.flags
  956. if self.FILTER in self.flags:
  957. self.fm.thisdir.temporary_filter = self._build_regex()
  958. if self.PERM_FILTER in self.flags and asyoutype:
  959. self.fm.thisdir.filter = self._build_regex()
  960. if self.FILTER in self.flags or self.PERM_FILTER in self.flags:
  961. self.fm.thisdir.refilter()
  962. if self._count(move=asyoutype) == 1 and self.AUTO_OPEN in self.flags:
  963. return True
  964. return False
  965. def tab(self, tabnum):
  966. self._count(move=True, offset=tabnum)
  967. def _build_regex(self):
  968. if self._regex is not None:
  969. return self._regex
  970. frmat = "%s"
  971. flags = self.flags
  972. pattern = self.pattern
  973. if pattern == ".":
  974. return re.compile("")
  975. # Handle carets at start and dollar signs at end separately
  976. if pattern.startswith('^'):
  977. pattern = pattern[1:]
  978. frmat = "^" + frmat
  979. if pattern.endswith('$'):
  980. pattern = pattern[:-1]
  981. frmat += "$"
  982. # Apply one of the search methods
  983. if self.SM_REGEX in flags:
  984. regex = pattern
  985. elif self.SM_GLOB in flags:
  986. regex = re.escape(pattern).replace("\\*", ".*").replace("\\?", ".")
  987. elif self.SM_LETTERSKIP in flags:
  988. regex = ".*".join(re.escape(c) for c in pattern)
  989. else:
  990. regex = re.escape(pattern)
  991. regex = frmat % regex
  992. # Invert regular expression if necessary
  993. if self.INVERT in flags:
  994. regex = "^(?:(?!%s).)*$" % regex
  995. # Compile Regular Expression
  996. options = re.UNICODE
  997. if self.IGNORE_CASE in flags or self.SMART_CASE in flags and \
  998. pattern.islower():
  999. options |= re.IGNORECASE
  1000. try:
  1001. self._regex = re.compile(regex, options)
  1002. except Exception:
  1003. self._regex = re.compile("")
  1004. return self._regex
  1005. def _count(self, move=False, offset=0):
  1006. count = 0
  1007. cwd = self.fm.thisdir
  1008. pattern = self.pattern
  1009. if not pattern or not cwd.files:
  1010. return 0
  1011. if pattern == '.':
  1012. return 0
  1013. if pattern == '..':
  1014. return 1
  1015. deq = deque(cwd.files)
  1016. deq.rotate(-cwd.pointer - offset)
  1017. i = offset
  1018. regex = self._build_regex()
  1019. for fsobj in deq:
  1020. if regex.search(fsobj.relative_path):
  1021. count += 1
  1022. if move and count == 1:
  1023. cwd.move(to=(cwd.pointer + i) % len(cwd.files))
  1024. self.fm.thisfile = cwd.pointed_obj
  1025. if count > 1:
  1026. return count
  1027. i += 1
  1028. return count == 1
  1029. class filter_inode_type(Command):
  1030. """
  1031. :filter_inode_type [dfl]
  1032. Displays only the files of specified inode type. Parameters
  1033. can be combined.
  1034. d display directories
  1035. f display files
  1036. l display links
  1037. """
  1038. FILTER_DIRS = 'd'
  1039. FILTER_FILES = 'f'
  1040. FILTER_LINKS = 'l'
  1041. def execute(self):
  1042. if not self.arg(1):
  1043. self.fm.thisdir.inode_type_filter = None
  1044. else:
  1045. self.fm.thisdir.inode_type_filter = lambda file: (
  1046. True if ((self.FILTER_DIRS in self.arg(1) and file.is_directory) or
  1047. (self.FILTER_FILES in self.arg(1) and file.is_file and not file.is_link) or
  1048. (self.FILTER_LINKS in self.arg(1) and file.is_link)) else False)
  1049. self.fm.thisdir.refilter()
  1050. class grep(Command):
  1051. """:grep <string>
  1052. Looks for a string in all marked files or directories
  1053. """
  1054. def execute(self):
  1055. if self.rest(1):
  1056. action = ['grep', '--line-number']
  1057. action.extend(['-e', self.rest(1), '-r'])
  1058. action.extend(f.path for f in self.fm.thistab.get_selection())
  1059. self.fm.execute_command(action, flags='p')
  1060. class flat(Command):
  1061. """
  1062. :flat <level>
  1063. Flattens the directory view up to the specified level.
  1064. -1 fully flattened
  1065. 0 remove flattened view
  1066. """
  1067. def execute(self):
  1068. try:
  1069. level = self.rest(1)
  1070. level = int(level)
  1071. except ValueError:
  1072. level = self.quantifier
  1073. if level < -1:
  1074. self.fm.notify("Need an integer number (-1, 0, 1, ...)", bad=True)
  1075. self.fm.thisdir.unload()
  1076. self.fm.thisdir.flat = level
  1077. self.fm.thisdir.load_content()
  1078. # Version control commands
  1079. # --------------------------------
  1080. class stage(Command):
  1081. """
  1082. :stage
  1083. Stage selected files for the corresponding version control system
  1084. """
  1085. def execute(self):
  1086. from ranger.ext.vcs import VcsError
  1087. if self.fm.thisdir.vcs and self.fm.thisdir.vcs.track:
  1088. filelist = [f.path for f in self.fm.thistab.get_selection()]
  1089. try:
  1090. self.fm.thisdir.vcs.action_add(filelist)
  1091. except VcsError as error:
  1092. self.fm.notify('Unable to stage files: {0:s}'.format(str(error)))
  1093. self.fm.ui.vcsthread.process(self.fm.thisdir)
  1094. else:
  1095. self.fm.notify('Unable to stage files: Not in repository')
  1096. class unstage(Command):
  1097. """
  1098. :unstage
  1099. Unstage selected files for the corresponding version control system
  1100. """
  1101. def execute(self):
  1102. from ranger.ext.vcs import VcsError
  1103. if self.fm.thisdir.vcs and self.fm.thisdir.vcs.track:
  1104. filelist = [f.path for f in self.fm.thistab.get_selection()]
  1105. try:
  1106. self.fm.thisdir.vcs.action_reset(filelist)
  1107. except VcsError as error:
  1108. self.fm.notify('Unable to unstage files: {0:s}'.format(str(error)))
  1109. self.fm.ui.vcsthread.process(self.fm.thisdir)
  1110. else:
  1111. self.fm.notify('Unable to unstage files: Not in repository')
  1112. # Metadata commands
  1113. # --------------------------------
  1114. class prompt_metadata(Command):
  1115. """
  1116. :prompt_metadata <key1> [<key2> [<key3> ...]]
  1117. Prompt the user to input metadata for multiple keys in a row.
  1118. """
  1119. _command_name = "meta"
  1120. _console_chain = None
  1121. def execute(self):
  1122. prompt_metadata._console_chain = self.args[1:]
  1123. self._process_command_stack()
  1124. def _process_command_stack(self):
  1125. if prompt_metadata._console_chain:
  1126. key = prompt_metadata._console_chain.pop()
  1127. self._fill_console(key)
  1128. else:
  1129. for col in self.fm.ui.browser.columns:
  1130. col.need_redraw = True
  1131. def _fill_console(self, key):
  1132. metadata = self.fm.metadata.get_metadata(self.fm.thisfile.path)
  1133. if key in metadata and metadata[key]:
  1134. existing_value = metadata[key]
  1135. else:
  1136. existing_value = ""
  1137. text = "%s %s %s" % (self._command_name, key, existing_value)
  1138. self.fm.open_console(text, position=len(text))
  1139. class meta(prompt_metadata):
  1140. """
  1141. :meta <key> [<value>]
  1142. Change metadata of a file. Deletes the key if value is empty.
  1143. """
  1144. def execute(self):
  1145. key = self.arg(1)
  1146. value = self.rest(1)
  1147. update_dict = dict()
  1148. update_dict[key] = self.rest(2)
  1149. selection = self.fm.thistab.get_selection()
  1150. for f in selection:
  1151. self.fm.metadata.set_metadata(f.path, update_dict)
  1152. self._process_command_stack()
  1153. def tab(self, tabnum):
  1154. key = self.arg(1)
  1155. metadata = self.fm.metadata.get_metadata(self.fm.thisfile.path)
  1156. if key in metadata and metadata[key]:
  1157. return [" ".join([self.arg(0), self.arg(1), metadata[key]])]
  1158. else:
  1159. return [self.arg(0) + " " + key for key in sorted(metadata)
  1160. if key.startswith(self.arg(1))]
  1161. class linemode(default_linemode):
  1162. """
  1163. :linemode <mode>
  1164. Change what is displayed as a filename.
  1165. - "mode" may be any of the defined linemodes (see: ranger.core.linemode).
  1166. "normal" is mapped to "filename".
  1167. """
  1168. def execute(self):
  1169. mode = self.arg(1)
  1170. if mode == "normal":
  1171. mode = DEFAULT_LINEMODE
  1172. if mode not in self.fm.thisfile.linemode_dict:
  1173. self.fm.notify("Unhandled linemode: `%s'" % mode, bad=True)
  1174. return
  1175. self.fm.thisdir._set_linemode_of_children(mode)
  1176. # Ask the browsercolumns to redraw
  1177. for col in self.fm.ui.browser.columns:
  1178. col.need_redraw = True