aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMattias Andrée <maandree@operamail.com>2014-06-13 12:57:27 +0200
committerMattias Andrée <maandree@operamail.com>2014-06-13 12:57:27 +0200
commitbd2dfeee45a8a50031cc1889fdc7b9184da0b9b9 (patch)
tree1c1fe21e32cd69835f8f233e04b19be1298597b2
parentrcfile works (diff)
parentm (diff)
downloadpytagomacs-bd2dfeee45a8a50031cc1889fdc7b9184da0b9b9.tar.gz
pytagomacs-bd2dfeee45a8a50031cc1889fdc7b9184da0b9b9.tar.bz2
pytagomacs-bd2dfeee45a8a50031cc1889fdc7b9184da0b9b9.tar.xz
merge the two branches
Signed-off-by: Mattias Andrée <maandree@operamail.com>
-rw-r--r--COPYING2
-rw-r--r--src/common.py107
-rw-r--r--src/editor.py10
-rw-r--r--src/editring.py2
-rw-r--r--src/killring.py2
-rw-r--r--src/line.py2
6 files changed, 73 insertions, 52 deletions
diff --git a/COPYING b/COPYING
index 2a5cf61..65e240b 100644
--- a/COPYING
+++ b/COPYING
@@ -1,6 +1,6 @@
pytagomacs – An Emacs like key–value editor library for Python
-Copyright © 2013 Mattias Andrée (maandree@member.fsf.org)
+Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
diff --git a/src/common.py b/src/common.py
index 51549c8..476cd38 100644
--- a/src/common.py
+++ b/src/common.py
@@ -47,6 +47,16 @@ ALERT_COLOUR = None
:str? The colour of the alert message
'''
+KILLRING_LIMIT = 50
+'''
+:int The maximum size of the killring
+'''
+
+EDITRING_LIMIT = 100
+'''
+:int The maximum size of the editring
+'''
+
atleast = lambda x, minimum : (x is not None) and (x >= minimum)
'''
@@ -87,47 +97,60 @@ class Jump():
print(self.string, end = '')
-def parsefile(file):
- '''
- Parse a file name encoded with environment variables
-
- @param file The encoded file name
- @return The target file name, None if the environment variables are not declared
- '''
- if '$' in file:
- buf = ''
- esc = False
- var = None
- for c in file:
- if esc:
- buf += c
- esc = False
- elif var is not None:
- if c == '/':
- var = os.environ[var] if var in os.environ else ''
- if len(var) == 0:
- return None
- buf += var + c
- var = None
- else:
- var += c
- elif c == '$':
- var = ''
- elif c == '\\':
- esc = True
- else:
- buf += c
- return buf
- return file
-
-
-for file in ('$XDG_CONFIG_HOME/%/%rc', '$HOME/.config/%/%rc', '$HOME/.%rc', '/etc/%rc'):
- file = parsefile(file.replace('%', 'pytagomacs'))
- if (file is not None) and os.path.exists(file):
- with open(file, 'rb') as rcfile:
- code = rcfile.read().decode('utf8', 'replace') + '\n'
- env = os.environ
- code = compile(code, file, 'exec')
- exec(code, globals())
+## Load extension and configurations via pytagomacsrc.
+config_file = None
+# Possible auto-selected configuration scripts,
+# earlier ones have precedence, we can only select one.
+files = []
+def add_files(var, *ps, multi = False):
+ if var == '~':
+ try:
+ # Get the home (also known as initial) directory of the real user
+ import pwd
+ var = pwd.getpwuid(os.getuid()).pw_dir
+ except:
+ return
+ else:
+ # Resolve environment variable or use empty string if none is selected
+ if (var is None) or (var in os.environ) and (not os.environ[var] == ''):
+ var = '' if var is None else os.environ[var]
+ else:
+ return
+ paths = [var]
+ # Split environment variable value if it is a multi valeu variable
+ if multi and os.pathsep in var:
+ paths = [v for v in var.split(os.pathsep) if not v == '']
+ # Add files according to patterns
+ for p in ps:
+ p = p.replace('/', os.sep).replace('%', 'pytagomacs')
+ for v in paths:
+ files.append(v + p)
+add_files('XDG_CONFIG_HOME', '/%/%rc', '/%rc')
+add_files('HOME', '/.config/%/%rc', '/.config/%rc', '/.%rc')
+add_files('~', '/.config/%/%rc', '/.config/%rc', '/.%rc')
+add_files('XDG_CONFIG_DIRS', '/%rc', multi = True)
+add_files(None, '/etc/%rc')
+for file in files:
+ # If the file we exists,
+ if os.path.exists(file):
+ # select it,
+ config_file = file
+ # and stop trying files with lower precedence.
break
+if config_file is not None:
+ code = None
+ # Read configuration script file
+ with open(config_file, 'rb') as script:
+ code = script.read()
+ # Decode configurion script file and add a line break
+ # at the end to ensure that the last line is empty.
+ # If it is not, we will get errors.
+ code = code.decode('utf-8', 'error') + '\n'
+ # Compile the configuration script,
+ code = compile(code, config_file, 'exec')
+ # and run it, with it have the same
+ # globals as this module, so that it can
+ # not only use want we have defined, but
+ # also redefine it for us.
+ exec(code, globals())
diff --git a/src/editor.py b/src/editor.py
index c889cd7..bc73348 100644
--- a/src/editor.py
+++ b/src/editor.py
@@ -3,7 +3,7 @@
'''
pytagomacs – An Emacs like key–value editor library for Python
-Copyright © 2013 Mattias Andrée (maandree@member.fsf.org)
+Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -35,7 +35,6 @@ from line import *
## TODO widthless characters should be ignored when calculating the size a text
-
## TODO implement undo history
##
## Until the user has halted for 1 second (configurably) or has navigated using arrow keys or alternative key combinations,
@@ -80,7 +79,6 @@ def __override(self, insert, override = True):
Line.override = __override
-
class TextArea():
'''
GNU Emacs alike text area
@@ -105,7 +103,7 @@ class TextArea():
if height <= 0: height += int(screen_size[0]) - top + 1
self.fields, self.datamap, self.left, self.top, self.width, self.height = fields, datamap, left, top, width - 1, height
self.innerleft = len(max(self.fields, key = len)) + 3
- self.killring, self.editring = Killring(), Editring()
+ self.killring, self.editring = Killring(limit = KILLRING_LIMIT), Editring(limit = EDITRING_LIMIT)
data = lambda field : datamap[field] if field in datamap else ''
self.lines = [Line(self, self.fields[y], data(self.fields[y]), y) for y in range(len(self.fields))]
self.areawidth = self.width - self.innerleft
@@ -362,7 +360,7 @@ class TextArea():
elif d == ctrl('Y'): edit(lambda L : L.yank(), _('Killring is empty'))
elif d == ctrl('R'): self.editring.change_direction()
elif d in (ctrl('_'), ctrl('U')):
- ## TODO history break
+ ## TODO history break
if self.editring.is_empty():
self.alert(_('Nothing to undo'))
else:
@@ -488,7 +486,7 @@ if __name__ == '__main__': # For testing
return True
area = None
try:
- area = TextArea(('a be se de e eff ge hå i ji kå ell emm enn o pe ku ärr ess te u ve dubbel-ve eks y säta å ä ö').split(' '), {}, 6, 4, 40, 10)
+ area = TextArea(('a be se de e eff ge hå i ji kå ell emm enn o pe ku ärr ess te u ve dubbel-ve eks y säta å ä ö').split(' '), {}, 6, 4, -6, -4)
area.initialise(True)
area.run(phonysaver)
finally:
diff --git a/src/editring.py b/src/editring.py
index a71700c..3dcee08 100644
--- a/src/editring.py
+++ b/src/editring.py
@@ -3,7 +3,7 @@
'''
pytagomacs – An Emacs like key–value editor library for Python
-Copyright © 2013 Mattias Andrée (maandree@member.fsf.org)
+Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
diff --git a/src/killring.py b/src/killring.py
index b5275ac..eb80f27 100644
--- a/src/killring.py
+++ b/src/killring.py
@@ -3,7 +3,7 @@
'''
pytagomacs – An Emacs like key–value editor library for Python
-Copyright © 2013 Mattias Andrée (maandree@member.fsf.org)
+Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
diff --git a/src/line.py b/src/line.py
index 6fc619b..4ec4000 100644
--- a/src/line.py
+++ b/src/line.py
@@ -3,7 +3,7 @@
'''
pytagomacs – An Emacs like key–value editor library for Python
-Copyright © 2013 Mattias Andrée (maandree@member.fsf.org)
+Copyright © 2013, 2014 Mattias Andrée (maandree@member.fsf.org)
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by