OSDN Git Service

Load JOY_HOME/definitions.txt
authorSimon Forman <sforman@hushmail.com>
Sun, 22 Jul 2018 18:50:34 +0000 (11:50 -0700)
committerSimon Forman <sforman@hushmail.com>
Sun, 22 Jul 2018 18:50:34 +0000 (11:50 -0700)
You still can't edit other text files from within the UI, but at least
now you have a place to persist your own definitions over restarts.

I thought about having a [definitions] section in the config INI file,
but for some reason I prefer a separate definitions.txt file.  I dunno.
Might change it in future.

joy/gui/main.py
joy/library.py

index 150028c..eb84be4 100755 (executable)
@@ -29,7 +29,7 @@ _log.info('Starting with JOY_HOME=%s', JOY_HOME)
 
 from joy.gui.textwidget import TextViewerWidget, tk, get_font
 from joy.gui.world import StackDisplayWorld
-from joy.library import initialize
+from joy.library import initialize, DefinitionWrapper
 from joy.utils.stack import stack_to_string
 
 
@@ -48,7 +48,6 @@ def repo_relative_path(path):
     os.path.commonprefix((repo.controldir(), path))
     )
 
-
 def commands():
   # pylint: disable=unused-variable
 
@@ -112,6 +111,7 @@ JOY_FN = os.path.join(JOY_HOME, 'scratch.txt')
 LOG_FN = os.path.join(JOY_HOME, 'log.txt')
 D = initialize()
 D.update(commands())
+DefinitionWrapper.load_definitions(os.path.join(JOY_HOME, 'definitions.txt'), D)
 world = StackDisplayWorld(repo, STACK_FN, REL_STACK_FN, dictionary=D)
 defaults = dict(width=80, height=25)
 t = TextViewerWidget(world, **defaults)
index e760ddb..9e55b7a 100644 (file)
@@ -380,6 +380,13 @@ class DefinitionWrapper(object):
     _log.info('Adding definition %s := %s', F.name, expression_to_string(F.body))
     dictionary[F.name] = F
 
+  @classmethod
+  def load_definitions(class_, filename, dictionary):
+    with open(filename) as f:
+      lines = [line for line in f if '==' in line]
+    for line in lines:
+      class_.add_def(line, dictionary)
+
 
 def _text_to_defs(text):
   return (line.strip() for line in text.splitlines() if '==' in line)