# ########################################################### # # created: 2010-07-14 # developer: JohnLBevan (http://developer42.wordpress.com) # description: Pulls task comments out of code (just //todo:... or #todo:... for now; but easily customisable) # # modified: 2010-07-04 # by: JohnLBevan import pn import scintilla import re, string from pypn.decorators import script todo = re.compile(r"((?:\/{2}|#)todo:.*$)",re.M) offset = 0 @script("Task List", "Productivity") def GetTaskList(): """Pulls tasks from the doc (or selected text) and outputs to a new window""" data = GetSelectedText(scintilla.Scintilla(pn.CurrentDoc())) data = InsertLineNos(data) lines = todo.findall(data) #NOfEachTuple(todo.findall(data),1) (required before I figured out ?:) data = "\n".join(lines) UpdateDocument(scintilla.Scintilla(pn.NewDocument(None)),data) def GetSelectedText(editor): """pulls the selected text from the editor window, or the whole docs content if nothings selected""" start = editor.SelectionStart end = editor.SelectionEnd if (start == end): start = 0 end = editor.Length else: GetOffset(editor) return editor.GetTextRange(start, end) def GetOffset(editor): """works out what line number the selection starts at & stores it in a global variable""" global offset end = editor.SelectionStart text = editor.GetTextRange(0, end) offset = text.count("\n") #assumes there are no docs with just \r as a line break def UpdateDocument(editor, content): """puts the content into the editor window""" editor.BeginUndoAction() editor.ClearAll() editor.AddText(len(content), content) editor.EndUndoAction() def InsertLineNos(data): """puts in line numbers""" data = data.replace("\r\n","\r") + '\r' data = data.replace("\n","\r") global offset i = offset while '\r' in data: i = i + 1 data = data.replace("\r"," :: at line " + str(i)+"\n",1) return data def NOfEachTuple(tuples, n): """takes an array of tuples and returns a list containing just the nth item of each tuple""" result = [0] result.pop() for t in tuples: result.append(t[n]) return result