Skip to content
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

Closed
wants to merge 8 commits into from
Closed
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Default (Linux).sublime-keymap
Expand Up @@ -4,6 +4,7 @@
{ "keys": ["ctrl+enter"], "command": "plain_tasks_new","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["ctrl+i"], "command": "plain_tasks_new","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["ctrl+shift+a"], "command": "plain_tasks_archive","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["ctrl+shift+o"], "command": "plain_tasks_archive_org","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["ctrl+shift+u"], "command": "plain_tasks_open_url","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["alt+o"], "command": "plain_tasks_open_link","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["enter"], "command": "insert", "args": {"characters": "\n\t"}, "context":
Expand Down
1 change: 1 addition & 0 deletions Default (OSX).sublime-keymap
Expand Up @@ -4,6 +4,7 @@
{ "keys": ["super+enter"], "command": "plain_tasks_new","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["super+i"], "command": "plain_tasks_new","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["super+shift+a"], "command": "plain_tasks_archive","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["super+shift+o"], "command": "plain_tasks_archive_org","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["super+shift+u"], "command": "plain_tasks_open_url","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["ctrl+o"], "command": "plain_tasks_open_link","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["enter"], "command": "insert", "args": {"characters": "\n\t"}, "context":
Expand Down
1 change: 1 addition & 0 deletions Default (Windows).sublime-keymap
Expand Up @@ -4,6 +4,7 @@
{ "keys": ["ctrl+enter"], "command": "plain_tasks_new","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["ctrl+i"], "command": "plain_tasks_new","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["ctrl+shift+a"], "command": "plain_tasks_archive","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["ctrl+shift+o"], "command": "plain_tasks_archive_org","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["ctrl+shift+u"], "command": "plain_tasks_open_url","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["alt+o"], "command": "plain_tasks_open_link","context": [{ "key": "selector", "operator": "equal", "operand": "text.todo" }] },
{ "keys": ["enter"], "command": "insert", "args": {"characters": "\n\t"}, "context":
Expand Down
1 change: 1 addition & 0 deletions Default.sublime-commands
Expand Up @@ -4,6 +4,7 @@
{ "caption": "Tasks: Cancel", "command": "plain_tasks_cancel" },
{ "caption": "Tasks: New", "command": "plain_tasks_new" },
{ "caption": "Tasks: Archive", "command": "plain_tasks_archive" },
{ "caption": "Tasks: Archive (Org-Mode Style)", "command": "plain_tasks_org_archive" },
{ "caption": "Tasks: Open URL", "command": "plain_tasks_open_url" },
{ "caption": "Tasks: Open Link", "command": "plain_tasks_open_link" },
{ "caption": "Tasks: View as HTML", "command": "plain_tasks_convert_to_html" },
Expand Down
106 changes: 104 additions & 2 deletions PlainTasks.py
@@ -1,7 +1,7 @@
#!/usr/bin/python
# -*- coding: utf-8 -*-

import os
import os,io
import re
import sublime
import sublime_plugin
Expand Down Expand Up @@ -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}"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

u too

self.archive_org_filemask = self.view.settings().get(
'archive_org_filemask', self.archive_org_default_filemask)
self.runCommand(edit)


Expand Down Expand Up @@ -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'
Expand Down Expand Up @@ -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))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sublime.status_message(u'Archiving tree to {0}'.format(filename)) to support ST2, and all other strings should start with u and contain index for format.

Choose a reason for hiding this comment

The 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):
Copy link
Collaborator

Choose a reason for hiding this comment

The 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
Copy link
Collaborator

Choose a reason for hiding this comment

The 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