Public part of my 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.

46 lines
1.6 KiB

  1. import rlcompleter
  2. import sys
  3. import os
  4. # From https://github.com/python/cpython/blob/v3.7.0b5/Lib/site.py#L436
  5. # Changing the history file
  6. def register_readline():
  7. import atexit
  8. try:
  9. import readline
  10. import rlcompleter
  11. except ImportError:
  12. return
  13. # Reading the initialization (config) file may not be enough to set a
  14. # completion key, so we set one first and then read the file.
  15. readline_doc = getattr(readline, '__doc__', '')
  16. if readline_doc is not None and 'libedit' in readline_doc:
  17. readline.parse_and_bind('bind ^I rl_complete')
  18. else:
  19. readline.parse_and_bind('tab: complete')
  20. try:
  21. readline.read_init_file()
  22. except OSError:
  23. # An OSError here could have many causes, but the most likely one
  24. # is that there's no .inputrc file (or .editrc file in the case of
  25. # Mac OS X + libedit) in the expected location. In that case, we
  26. # want to ignore the exception.
  27. pass
  28. if readline.get_current_history_length() == 0:
  29. # If no history was loaded, default to .python_history.
  30. # The guard is necessary to avoid doubling history size at
  31. # each interpreter exit when readline was already configured
  32. # through a PYTHONSTARTUP hook, see:
  33. # http://bugs.python.org/issue5845#msg198636
  34. history = os.path.join(os.path.expanduser('~'),
  35. '.cache/python_history')
  36. try:
  37. readline.read_history_file(history)
  38. except OSError:
  39. pass
  40. atexit.register(readline.write_history_file, history)
  41. sys.__interactivehook__ = register_readline