Skip to content

Commit

Permalink
add installable editor field (default to $EDITOR)
Browse files Browse the repository at this point in the history
  • Loading branch information
perrette committed Apr 27, 2023
1 parent 01eb72b commit 75b16f3
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 2 deletions.
4 changes: 4 additions & 0 deletions papers/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,9 @@ def installcmd(parser, o, config):
config.local = o.local
config.absolute_paths = o.absolute_paths

if o.editor:
config.editor = o.editor

# create bibtex file if not existing
bibtex = Path(o.bibtex) if o.bibtex else None

Expand Down Expand Up @@ -787,6 +790,7 @@ def get_parser(config=None):
installp.add_argument('--git', '--backup', action='store_true', default=None, help="""Track bibtex files with git.""")
installp.add_argument('--git-lfs', '--backup-files', action='store_true', default=None, help="""Backup files with git-lfs (implies --git)""")
installp.add_argument('--gitdir', default=None, help=argparse.SUPPRESS)
installp.add_argument('--editor', help="""Set command to open text editor. Need to wait until closing ! E.g. vim or subl -w""")

grp = installp.add_argument_group('status')
# grp.add_argument('-l','--status', action='store_true')
Expand Down
15 changes: 15 additions & 0 deletions papers/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,13 @@ def __init__(self, file=None, data=DATA_DIR,
bibtex=None, filesdir=None,
keyformat=KEYFORMAT,
nameformat=NAMEFORMAT,
editor=None,
gitdir=None, git=False, gitlfs=False, local=None, absolute_paths=None, backup_files=False):
self.file = file
self.local = local
self.data = data
self.filesdir = filesdir
self.editor = editor
self.bibtex = bibtex
self.keyformat = keyformat
self.nameformat = nameformat
Expand All @@ -46,6 +48,17 @@ def __init__(self, file=None, data=DATA_DIR,
self.gitlfs = gitlfs
self.backup_files = backup_files


@property
def editor(self):
return self._editor

@editor.setter
def editor(self, value):
if value is not None:
os.environ['EDITOR'] = value
self._editor = value

def collections(self):
files = []
for root, dirs, files in os.walk(os.path.dirname(self.bibtex)):
Expand Down Expand Up @@ -91,6 +104,7 @@ def save(self):
"filesdir": self._relpath(self.filesdir),
"bibtex": self._relpath(self.bibtex),
"gitdir": self._relpath(self.gitdir),
"editor": self.editor,
"keyformat": self.keyformat.todict(),
"nameformat": self.nameformat.todict(),
"local": self.local,
Expand Down Expand Up @@ -146,6 +160,7 @@ def _fmt_path(p):
if self.git:
lines.append('* git-lfs tracked: '+str(self.gitlfs))
lines.append('* git directory : '+self.gitdir)
lines.append('* editor: '+str(self.editor))

if self.filesdir is None:
status = bcolors.WARNING+' (unset)'+bcolors.ENDC
Expand Down
4 changes: 2 additions & 2 deletions papers/duplicate.py
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ def choose_entry_interactive(entries, extra=[], msg='', select=False, best=None)



def edit_entries(entries, diff=False, ndiff=False):
def edit_entries(entries, diff=False, ndiff=False, editor=None):
'''edit entries and insert result in database
'''
# write the listed entries to temporary file
Expand All @@ -363,7 +363,7 @@ def edit_entries(entries, diff=False, ndiff=False):
with open(filename, 'w') as f:
f.write(entrystring)

res = os.system('{} {}'.format(os.getenv('EDITOR'), filename))
res = os.system('{} {}'.format(editor or os.getenv('EDITOR'), filename))

if res == 0:
logger.info('sucessfully edited file, insert edited entries')
Expand Down

0 comments on commit 75b16f3

Please sign in to comment.