New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Org archive #216
Org archive #216
Changes from 7 commits
4b8b509
9880df4
c15b59d
20f99a6
2d2d3e5
be88e90
ad09a00
46c0f9c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
#!/usr/bin/python | ||
# -*- coding: utf-8 -*- | ||
|
||
import os | ||
import os,io | ||
import re | ||
import sublime | ||
import sublime_plugin | ||
|
@@ -38,6 +38,10 @@ def run(self, edit): | |
self.sys_enc = locale.getpreferredencoding() | ||
self.project_postfix = self.view.settings().get('project_tag', True) | ||
self.archive_name = self.view.settings().get('archive_name', 'Archive:') | ||
# org-mode style archive stuff | ||
self.archive_org_default_filemask = "{dir}{sep}{base}_archive{ext}" | ||
self.archive_org_filemask = self.view.settings().get( | ||
'archive_org_filemask', self.archive_org_default_filemask) | ||
self.runCommand(edit) | ||
|
||
|
||
|
@@ -709,7 +713,7 @@ def runCommand(self, edit): | |
html_doc.append(ht) | ||
|
||
# create file | ||
import tempfile, io | ||
import tempfile | ||
tmp_html = tempfile.NamedTemporaryFile(delete=False, suffix='.html') | ||
with io.open('%s/PlainTasks/templates/template.html' % sublime.packages_path(), 'r', encoding='utf8') as template: | ||
title = os.path.basename(self.view.file_name()) if self.view.file_name() else 'Export' | ||
|
@@ -833,3 +837,101 @@ def run(self, edit): | |
msg = msg.replace(o, r) | ||
|
||
sublime.set_clipboard(msg) | ||
|
||
class PlainTasksArchiveOrgCommand(PlainTasksBase): | ||
def runCommand(self, edit): | ||
# Archive the curent subtree to our archive file, not just completed tasks. | ||
# For now, it's mapped to ctrl-shift-o or super-shift-o | ||
|
||
# TODO: Mark any tasks found as complete, or maybe warn. | ||
|
||
# Get our archive filename | ||
archive_filename = self.__createArchiveFilename() | ||
|
||
# Figure out our subtree | ||
region = self.__findCurrentSubtree() | ||
if region.empty(): | ||
# How can we get here? | ||
sublime.error_message("Error:\n\nCould not find a tree to archive.") | ||
return | ||
|
||
# Write our region or our archive file | ||
success = self.__writeArchive(archive_filename, region) | ||
|
||
# only erase our region if the write was successful | ||
if success: | ||
self.view.erase(edit,region) | ||
|
||
return | ||
|
||
def __writeArchive(self, filename, region): | ||
# Write out the given region | ||
|
||
sublime.status_message('Archiving tree to {}'.format(filename)) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAVER - http://www.naver.com/stockerman7@naver.com 님께 보내신 메일 <Re: [PlainTasks] Org archive (#216)> 이 다음과 같은 이유로 전송 실패했습니다. 받는 사람이 회원님의 메일을 수신차단 하였습니다. |
||
try: | ||
# Have to use io.open because windows doesn't like writing | ||
# utf8 to regular filehandles | ||
with io.open(filename, 'a', encoding='utf8') as fh: | ||
data = self.view.substr(region) | ||
# Is there a way to read this in? | ||
fh.write("--- ✄ -----------------------\n") | ||
fh.write("Archived {}:\n".format(datetime.now().strftime( | ||
self.date_format))) | ||
# And, finally, write our data | ||
fh.write("{}\n".format(data)) | ||
return True | ||
|
||
except Exception as e: | ||
sublime.error_message("Error:\n\nUnable to append to {}\n{}".format( | ||
filename, str(e))) | ||
return False | ||
|
||
def __createArchiveFilename(self): | ||
# Create our archive filename, from the mask in our settings. | ||
|
||
# Split filename int dir, base, and extension, then apply our mask | ||
path_base, extension=os.path.splitext(self.view.file_name()) | ||
dir=os.path.dirname(path_base) | ||
base=os.path.basename(path_base) | ||
sep=os.sep | ||
|
||
# Now build our new filename | ||
try: | ||
# This could fail, if someone messed up the mask in the | ||
# settings. So, if it did fail, use our default. | ||
|
||
archive_filename=self.archive_org_filemask.format( | ||
dir=dir, base=base, ext=extension, sep=sep) | ||
except: | ||
# Use our default mask | ||
archive_filename=self.archive_org_default_filemask.format( | ||
dir=dir, base=base, ext=extension, sep=sep) | ||
|
||
# Display error, letting the user know | ||
sublime.error_message("Error:\n\nInvalid filemask:{}\nUsing default: {}".format( | ||
self.archive_org_filemask, self.archive_org_default_filemask)) | ||
|
||
return archive_filename | ||
|
||
def __regionIndentLen(self, region): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. unused method |
||
# Compute how many spaces a given region is indented | ||
# This is used to determine when we've left our subtree | ||
|
||
line_contents = self.view.substr(region).rstrip() | ||
indent = re.match('^(\s*)\S', line_contents, re.U) | ||
if indent is None or indent.group(1) is None: | ||
return 0 | ||
return len(indent.group(1)) | ||
|
||
def __findCurrentSubtree(self): | ||
# Return the region that starts at the cursor, or starts at | ||
# the beginning of the selection | ||
|
||
line = self.view.line(self.view.sel()[0].begin()) | ||
# Start finding the region at the beginning of the next line | ||
region = self.view.indented_region(line.b+2) | ||
|
||
# It missed our current line, so, back up one line. | ||
region.a=line.a | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. if not region.empty():
region = sublime.Region(line.a, region.b) |
||
|
||
return region |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
u
too