forked from aziz/PlainTasks
-
Notifications
You must be signed in to change notification settings - Fork 0
/
PlainTasks.py
932 lines (814 loc) · 41.1 KB
/
PlainTasks.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sublime, sublime_plugin
import os
import re
import webbrowser
import itertools
from datetime import datetime, tzinfo, timedelta
platform = sublime.platform()
ST3 = int(sublime.version()) >= 3000
if ST3:
from .APlainTasksCommon import PlainTasksBase, PlainTasksFold, get_all_projects_and_separators
else:
from APlainTasksCommon import PlainTasksBase, PlainTasksFold, get_all_projects_and_separators
# io is not operable in ST2 on Linux, but in all other cases io is better
# https://github.com/SublimeTextIssues/Core/issues/254
if not ST3 and platform == 'linux':
import codecs as io
else:
import io
NT = platform == 'windows'
if NT:
import subprocess
if ST3:
from datetime import timezone
else:
class timezone(tzinfo):
__slots__ = ("_offset", "_name")
def __init__(self, offset, name=None):
if not isinstance(offset, timedelta):
raise TypeError("offset must be a timedelta")
self._offset = offset
self._name = name
def utcoffset(self, dt):
return self._offset
def tzname(self, dt):
return self._name
def dst(self, dt):
return timedelta(0)
def tznow():
return datetime.now().replace(
tzinfo=timezone(
datetime.fromtimestamp(0) - datetime.utcfromtimestamp(0)
)
)
def check_parentheses(date_format, regex_group, is_date=False):
if is_date:
try:
parentheses = regex_group if datetime.strptime(regex_group.strip(), date_format) else ''
except ValueError:
parentheses = ''
else:
try:
parentheses = '' if datetime.strptime(regex_group.strip(), date_format) else regex_group
except ValueError:
parentheses = regex_group
return parentheses
class PlainTasksNewCommand(PlainTasksBase):
def runCommand(self, edit):
# list for ST3 support;
# reversed because with multiple selections regions would be messed up after first iteration
regions = itertools.chain(*(reversed(self.view.lines(region)) for region in reversed(list(self.view.sel()))))
header_to_task = self.view.settings().get('header_to_task', False)
# ST3 (3080) moves sel when call view.replace only by delta between original and
# new regions, so if sel is not in eol and we replace line with two lines,
# then cursor won’t be on next line as it should
sels = self.view.sel()
eol = None
for i, line in enumerate(regions):
line_contents = self.view.substr(line).rstrip()
not_empty_line = re.match('^(\s*)(\S.*)$', self.view.substr(line))
empty_line = re.match('^(\s+)$', self.view.substr(line))
current_scope = self.view.scope_name(line.a)
eol = line.b # need for ST3 when new content has line break
if 'item' in current_scope:
grps = not_empty_line.groups()
line_contents = self.view.substr(line) + '\n' + grps[0] + self.open_tasks_bullet + self.tasks_bullet_space
elif 'header' in current_scope and line_contents and not header_to_task:
grps = not_empty_line.groups()
line_contents = self.view.substr(line) + '\n' + grps[0] + self.before_tasks_bullet_spaces + self.open_tasks_bullet + self.tasks_bullet_space
elif 'separator' in current_scope:
grps = not_empty_line.groups()
line_contents = self.view.substr(line) + '\n' + grps[0] + self.before_tasks_bullet_spaces + self.open_tasks_bullet + self.tasks_bullet_space
elif not ('header' and 'separator') in current_scope or header_to_task:
eol = None
if not_empty_line:
grps = not_empty_line.groups()
line_contents = (grps[0] if len(grps[0]) > 0 else self.before_tasks_bullet_spaces) + self.open_tasks_bullet + self.tasks_bullet_space + grps[1]
elif empty_line: # only whitespaces
grps = empty_line.groups()
line_contents = grps[0] + self.open_tasks_bullet + self.tasks_bullet_space
else: # completely empty, no whitespaces
line_contents = self.before_tasks_bullet_spaces + self.open_tasks_bullet + self.tasks_bullet_space
else:
print('oops, need to improve PlainTasksNewCommand')
if eol:
# move cursor to eol of original line, workaround for ST3
sels.subtract(sels[~i])
sels.add(sublime.Region(eol, eol))
self.view.replace(edit, line, line_contents)
# convert each selection to single cursor, ready to type
new_selections = []
for sel in list(self.view.sel()):
eol = self.view.line(sel).b
new_selections.append(sublime.Region(eol, eol))
self.view.sel().clear()
for sel in new_selections:
self.view.sel().add(sel)
PlainTasksStatsStatus.set_stats(self.view)
self.view.run_command('plain_tasks_toggle_highlight_past_due')
class PlainTasksNewWithDateCommand(PlainTasksBase):
def runCommand(self, edit):
self.view.run_command('plain_tasks_new')
sels = list(self.view.sel())
suffix = ' @created%s' % tznow().strftime(self.date_format)
points = []
for s in reversed(sels):
if self.view.substr(sublime.Region(s.b - 2, s.b)) == ' ':
point = s.b - 2 # keep double whitespace at eol
else:
point = s.b
self.view.insert(edit, point, suffix)
points.append(point)
self.view.sel().clear()
offset = len(suffix)
for i, sel in enumerate(sels):
self.view.sel().add(sublime.Region(points[~i] + i*offset, points[~i] + i*offset))
class PlainTasksCompleteCommand(PlainTasksBase):
def runCommand(self, edit):
original = [r for r in self.view.sel()]
done_line_end, now = self.format_line_end(self.done_tag, tznow())
offset = len(done_line_end)
rom = r'^(\s*)(\[\s\]|.)(\s*.*)$'
rdm = r'''
(?x)^(\s*)(\[x\]|.) # 0,1 indent & bullet
(\s*[^\b]*?(?:[^\@]|(?<!\s)\@|\@(?=\s))*?\s*) # 2 very task
(?=
((?:\s@done|@project|@[wl]asted|$).*) # 3 ending either w/ done or w/o it & no date
| # or
(?:[ \t](\([^()]*\))\s*([^@]*|(?:@project|@[wl]asted).*))?$ # 4 date & possible project tag after
)
''' # rcm is the same, except bullet & ending
rcm = r'^(\s*)(\[\-\]|.)(\s*[^\b]*?(?:[^\@]|(?<!\s)\@|\@(?=\s))*?\s*)(?=((?:\s@cancelled|@project|@[wl]asted|$).*)|(?:[ \t](\([^()]*\))\s*([^@]*|(?:@project|@[wl]asted).*))?$)'
started = r'^\s*[^\b]*?\s*@started(\([\d\w,\.:\-\/ @]*\)).*$'
toggle = r'@toggle(\([\d\w,\.:\-\/ @]*\))'
regions = itertools.chain(*(reversed(self.view.lines(region)) for region in reversed(list(self.view.sel()))))
for line in regions:
line_contents = self.view.substr(line)
open_matches = re.match(rom, line_contents, re.U)
done_matches = re.match(rdm, line_contents, re.U)
canc_matches = re.match(rcm, line_contents, re.U)
started_matches = re.findall(started, line_contents, re.U)
toggle_matches = re.findall(toggle, line_contents, re.U)
done_line_end = done_line_end.rstrip()
if line_contents.endswith(' '):
done_line_end += ' ' # keep double whitespace at eol
dblspc = ' '
else:
dblspc = ''
current_scope = self.view.scope_name(line.a)
if 'pending' in current_scope:
grps = open_matches.groups()
eol = self.view.insert(edit, line.end(), done_line_end)
replacement = u'%s%s%s' % (grps[0], self.done_tasks_bullet, grps[2])
self.view.replace(edit, line, replacement.rstrip())
self.view.run_command(
'plain_tasks_calculate_time_for_task', {
'started_matches': started_matches,
'toggle_matches': toggle_matches,
'now': now,
'eol': line.end() + eol - (len(grps[1]) - len(self.done_tasks_bullet))}
)
elif 'header' in current_scope:
eol = self.view.insert(edit, line.end(), done_line_end)
self.view.run_command(
'plain_tasks_calculate_time_for_task', {
'started_matches': started_matches,
'toggle_matches': toggle_matches,
'now': now,
'eol': line.end() + eol}
)
indent = re.match('^(\s*)\S', line_contents, re.U)
self.view.insert(edit, line.begin() + len(indent.group(1)), '%s ' % self.done_tasks_bullet)
self.view.run_command('plain_tasks_calculate_total_time_for_project', {'start': line.a})
elif 'completed' in current_scope:
grps = done_matches.groups()
parentheses = check_parentheses(self.date_format, grps[4] or '')
replacement = u'%s%s%s%s' % (grps[0], self.open_tasks_bullet, grps[2], parentheses)
self.view.replace(edit, line, replacement.rstrip() + dblspc)
offset = -offset
elif 'cancelled' in current_scope:
grps = canc_matches.groups()
self.view.insert(edit, line.end(), done_line_end)
parentheses = check_parentheses(self.date_format, grps[4] or '')
replacement = u'%s%s%s%s' % (grps[0], self.done_tasks_bullet, grps[2], parentheses)
self.view.replace(edit, line, replacement.rstrip())
offset = -offset
self.view.run_command(
'plain_tasks_calculate_time_for_task', {
'started_matches': started_matches,
'toggle_matches': toggle_matches,
'now': now,
'eol': self.view.line(line.begin()).end()}
)
self.view.sel().clear()
for ind, pt in enumerate(original):
ofs = ind * offset
new_pt = sublime.Region(pt.a + ofs, pt.b + ofs)
self.view.sel().add(new_pt)
PlainTasksStatsStatus.set_stats(self.view)
self.view.run_command('plain_tasks_toggle_highlight_past_due')
class PlainTasksCancelCommand(PlainTasksBase):
def runCommand(self, edit):
original = [r for r in self.view.sel()]
canc_line_end, now = self.format_line_end(self.canc_tag, tznow())
offset = len(canc_line_end)
rom = r'^(\s*)(\[\s\]|.)(\s*.*)$'
rdm = r'^(\s*)(\[x\]|.)(\s*[^\b]*?(?:[^\@]|(?<!\s)\@|\@(?=\s))*?\s*)(?=((?:\s@done|@project|@[wl]asted|$).*)|(?:[ \t](\([^()]*\))\s*([^@]*|(?:@project|@[wl]asted).*))?$)'
rcm = r'^(\s*)(\[\-\]|.)(\s*[^\b]*?(?:[^\@]|(?<!\s)\@|\@(?=\s))*?\s*)(?=((?:\s@cancelled|@project|@[wl]asted|$).*)|(?:[ \t](\([^()]*\))\s*([^@]*|(?:@project|@[wl]asted).*))?$)'
started = r'^\s*[^\b]*?\s*@started(\([\d\w,\.:\-\/ @]*\)).*$'
toggle = r'@toggle(\([\d\w,\.:\-\/ @]*\))'
regions = itertools.chain(*(reversed(self.view.lines(region)) for region in reversed(list(self.view.sel()))))
for line in regions:
line_contents = self.view.substr(line)
open_matches = re.match(rom, line_contents, re.U)
done_matches = re.match(rdm, line_contents, re.U)
canc_matches = re.match(rcm, line_contents, re.U)
started_matches = re.findall(started, line_contents, re.U)
toggle_matches = re.findall(toggle, line_contents, re.U)
canc_line_end = canc_line_end.rstrip()
if line_contents.endswith(' '):
canc_line_end += ' ' # keep double whitespace at eol
dblspc = ' '
else:
dblspc = ''
current_scope = self.view.scope_name(line.a)
if 'pending' in current_scope:
grps = open_matches.groups()
eol = self.view.insert(edit, line.end(), canc_line_end)
replacement = u'%s%s%s' % (grps[0], self.canc_tasks_bullet, grps[2])
self.view.replace(edit, line, replacement.rstrip())
self.view.run_command(
'plain_tasks_calculate_time_for_task', {
'started_matches': started_matches,
'toggle_matches': toggle_matches,
'now': now,
'eol': line.end() + eol - (len(grps[1]) - len(self.canc_tasks_bullet)),
'tag': 'wasted'}
)
elif 'header' in current_scope:
eol = self.view.insert(edit, line.end(), canc_line_end)
self.view.run_command(
'plain_tasks_calculate_time_for_task', {
'started_matches': started_matches,
'toggle_matches': toggle_matches,
'now': now,
'eol': line.end() + eol,
'tag': 'wasted'}
)
indent = re.match('^(\s*)\S', line_contents, re.U)
self.view.insert(edit, line.begin() + len(indent.group(1)), '%s ' % self.canc_tasks_bullet)
self.view.run_command('plain_tasks_calculate_total_time_for_project', {'start': line.a})
elif 'completed' in current_scope:
sublime.status_message('You cannot cancel what have been done, can you?')
# grps = done_matches.groups()
# parentheses = check_parentheses(self.date_format, grps[4] or '')
# replacement = u'%s%s%s%s' % (grps[0], self.canc_tasks_bullet, grps[2], parentheses)
# self.view.replace(edit, line, replacement.rstrip())
# offset = -offset
elif 'cancelled' in current_scope:
grps = canc_matches.groups()
parentheses = check_parentheses(self.date_format, grps[4] or '')
replacement = u'%s%s%s%s' % (grps[0], self.open_tasks_bullet, grps[2], parentheses)
self.view.replace(edit, line, replacement.rstrip() + dblspc)
offset = -offset
self.view.sel().clear()
for ind, pt in enumerate(original):
ofs = ind * offset
new_pt = sublime.Region(pt.a + ofs, pt.b + ofs)
self.view.sel().add(new_pt)
PlainTasksStatsStatus.set_stats(self.view)
self.view.run_command('plain_tasks_toggle_highlight_past_due')
class PlainTasksArchiveCommand(PlainTasksBase):
def runCommand(self, edit, partial=False):
rds = 'meta.item.todo.completed'
rcs = 'meta.item.todo.cancelled'
# finding archive section
archive_pos = self.view.find(self.archive_name, 0, sublime.LITERAL)
if partial:
all_tasks = self.get_archivable_tasks_within_selections()
else:
all_tasks = self.get_all_archivable_tasks(archive_pos, rds, rcs)
if not all_tasks:
sublime.status_message('Nothing to archive')
else:
if archive_pos and archive_pos.a > 0:
line = self.view.full_line(archive_pos).end()
else:
create_archive = u'\n\n___________________\n%s\n' % self.archive_name
self.view.insert(edit, self.view.size(), create_archive)
line = self.view.size()
projects = get_all_projects_and_separators(self.view)
# adding tasks to archive section
for task in all_tasks:
line_content = self.view.substr(task)
match_task = re.match(r'^\s*(\[[x-]\]|.)(\s+.*$)', line_content, re.U)
current_scope = self.view.scope_name(task.a)
if rds in current_scope or rcs in current_scope:
pr = self.get_task_project(task, projects)
if self.project_postfix:
eol = u'{0}{1}{2}{3}\n'.format(
self.before_tasks_bullet_spaces,
line_content.strip(),
(u' @project(%s)' % pr) if pr else '',
' ' if line_content.endswith(' ') else '')
else:
eol = u'{0}{1}{2}{3}\n'.format(
self.before_tasks_bullet_spaces,
match_task.group(1), # bullet
(u'%s%s:' % (self.tasks_bullet_space, pr)) if pr else '',
match_task.group(2)) # very task
else:
eol = u'{0}{1}\n'.format(self.before_tasks_bullet_spaces * 2, line_content.lstrip())
line += self.view.insert(edit, line, eol)
# remove moved tasks (starting from the last one otherwise it screw up regions after the first delete)
for task in reversed(all_tasks):
self.view.erase(edit, self.view.full_line(task))
self.view.run_command('plain_tasks_sort_by_date')
def get_task_project(self, task, projects):
index = -1
for ind, pr in enumerate(projects):
if task < pr:
if ind > 0:
index = ind-1
break
#if there is no projects for task - return empty string
if index == -1:
return ''
prog = re.compile(r'^\n*(\s*)(.+):(?=\s|$)\s*(\@[^\s]+(\(.*?\))?\s*)*')
hierarhProject = ''
if index >= 0:
depth = re.match(r"\s*", self.view.substr(self.view.line(task))).group()
while index >= 0:
strProject = self.view.substr(projects[index])
if prog.match(strProject):
spaces = prog.match(strProject).group(1)
if len(spaces) < len(depth):
hierarhProject = prog.match(strProject).group(2) + ((" / " + hierarhProject) if hierarhProject else '')
depth = spaces
if len(depth) == 0:
break
else:
sep = re.compile(r'(^\s*)---.{3,5}---+$')
spaces = sep.match(strProject).group(1)
if len(spaces) < len(depth):
depth = spaces
if len(depth) == 0:
break
index -= 1
if not hierarhProject:
return ''
else:
return hierarhProject
def get_task_note(self, task, tasks):
note_line = task.end() + 1
while self.view.scope_name(note_line) == 'text.todo notes.todo ':
note = self.view.line(note_line)
if note not in tasks:
tasks.append(note)
note_line = self.view.line(note_line).end() + 1
def get_all_archivable_tasks(self, archive_pos, rds, rcs):
done_tasks = [i for i in self.view.find_by_selector(rds) if i.a < (archive_pos.a if archive_pos and archive_pos.a > 0 else self.view.size())]
for i in done_tasks:
self.get_task_note(i, done_tasks)
canc_tasks = [i for i in self.view.find_by_selector(rcs) if i.a < (archive_pos.a if archive_pos and archive_pos.a > 0 else self.view.size())]
for i in canc_tasks:
self.get_task_note(i, canc_tasks)
all_tasks = done_tasks + canc_tasks
all_tasks.sort()
return all_tasks
def get_archivable_tasks_within_selections(self):
all_tasks = []
for region in self.view.sel():
for l in self.view.lines(region):
line = self.view.line(l)
if ('completed' in self.view.scope_name(line.a)) or ('cancelled' in self.view.scope_name(line.a)):
all_tasks.append(line)
self.get_task_note(line, all_tasks)
return all_tasks
class PlainTasksNewTaskDocCommand(sublime_plugin.WindowCommand):
def run(self):
view = self.window.new_file()
view.settings().add_on_change('color_scheme', lambda: self.set_proper_scheme(view))
view.set_syntax_file('Packages/PlainTasks/PlainTasks.sublime-syntax' if ST3 else
'Packages/PlainTasks/PlainTasks.tmLanguage')
def set_proper_scheme(self, view):
if view.id() != sublime.active_window().active_view().id():
return
pts = sublime.load_settings('PlainTasks.sublime-settings')
if view.settings().get('color_scheme') == pts.get('color_scheme'):
return
# Since we cannot create file with syntax, there is moment when view has no settings,
# but it is activated, so some plugins (e.g. Color Highlighter) set wrong color scheme
view.settings().set('color_scheme', pts.get('color_scheme'))
class PlainTasksOpenUrlCommand(sublime_plugin.TextCommand):
#It is horrible regex but it works perfectly
URL_REGEX = r"""(?i)\b((?:https?://|www\d{0,3}[.]|[a-z0-9.\-]+[.][a-z]{2,4}/)(?:[^\s()<>]+|\(([^\s()<>]+|(\([^\s()<>]+\)))*\))
+(?:\(([^\s()<>]+|(\([^\s()<>]+\)))*\)|[^\s`!()\[\]{};:'".,<>?«»“”‘’]))"""
def run(self, edit):
s = self.view.sel()[0]
start, end = s.a, s.b
if 'url' in self.view.scope_name(start):
while self.view.substr(start) != '<': start -= 1
while self.view.substr(end) != '>': end += 1
rgn = sublime.Region(start + 1, end)
# optional select URL
self.view.sel().add(rgn)
url = self.view.substr(rgn)
if NT and all([ST3, ':' in url]):
# webbrowser uses os.startfile() under the hood, and it is not reliable in py3;
# thus call start command for url with scheme (eg skype:nick) and full path (eg c:\b)
subprocess.Popen(['start', url], shell=True)
else:
webbrowser.open_new_tab(url)
else:
self.search_bare_weblink_and_open(start, end)
def search_bare_weblink_and_open(self, start, end):
# expand selection to nearest stopSymbols
view_size = self.view.size()
stopSymbols = ['\t', ' ', '\"', '\'', '>', '<', ',']
# move the selection back to the start of the url
while (start > 0
and not self.view.substr(start - 1) in stopSymbols
and self.view.classify(start) & sublime.CLASS_LINE_START == 0):
start -= 1
# move end of selection forward to the end of the url
while (end < view_size
and not self.view.substr(end) in stopSymbols
and self.view.classify(end) & sublime.CLASS_LINE_END == 0):
end += 1
# grab the URL
url = self.view.substr(sublime.Region(start, end))
# optional select URL
self.view.sel().add(sublime.Region(start, end))
exp = re.search(self.URL_REGEX, url, re.X)
if exp and exp.group(0):
strUrl = exp.group(0)
if strUrl.find("://") == -1:
strUrl = "http://" + strUrl
webbrowser.open_new_tab(strUrl)
else:
sublime.status_message("Looks like there is nothing to open")
class PlainTasksOpenLinkCommand(sublime_plugin.TextCommand):
LINK_PATTERN = re.compile( # simple ./path/
r'''(?ixu)(?:^|[ \t])\.[\\/]
(?P<fn>
(?:[a-z]\:[\\/])? # special case for Windows full path
(?:[^\\/:">]+[\\/]?)+) # the very path (single filename/relative/full)
(?=[\\/:">]) # stop matching path
# options:
(>(?P<sym>\w+))?(\:(?P<line>\d+))?(\:(?P<col>\d+))?(\"(?P<text>[^\n]*)\")?
''')
MD_LINK = re.compile( # markdown [](path)
r'''(?ixu)\][ \t]*\(\<?(?:file\:///?)?
(?P<fn>.*?((\\\))?.*?)*)
(?:\>?[ \t]*
\"((\:(?P<line>\d+))?(\:(?P<col>\d+))?|(\>(?P<sym>\w+))?|(?P<text>[^\n]*))
\")?
\)
''')
WIKI_LINK = re.compile( # ORGMODE, NV, and all similar formats [[link][opt-desc]]
r'''(?ixu)\[\[(?:file(?:\+(?:sys|emacs))?\:)?(?:\.[\\/])?
(?P<fn>.*?((\\\])?.*?)*)
(?# options for orgmode link [[path::option]])
(?:\:\:(((?P<line>\d+))?(\:(?P<col>\d+))?|(\*(?P<sym>\w+))?|(?P<text>.*?((\\\])?.*?)*)))?
\](?:\[(.*?)\])?
\]
(?# options for NV [[path]] "option" — NV not support it, but PT should support so it wont break NV)
(?:[ \t]*
\"((\:(?P<linen>\d+))?(\:(?P<coln>\d+))?|(\>(?P<symn>\w+))?|(?P<textn>[^\n]*))
\")?
''')
def _format_res(self, res):
if res[3] == 'f':
return [res[0], "line: %d column: %d" % (int(res[1]), int(res[2]))]
elif res[3] == 'd':
return [res[0], 'Add folder to project' if ST3 else 'Folders are supported only in Sublime 3']
def _on_panel_selection(self, selection):
if selection >= 0:
res = self._current_res[selection]
win = sublime.active_window()
if not ST3 and res[3] == "d":
return sublime.status_message('Folders are supported only in Sublime 3')
elif res[3] == "d":
data = win.project_data()
if not data:
data = {}
if "folders" not in data:
data["folders"] = []
data["folders"].append({'follow_symlinks': True,
'path': res[0]})
win.set_project_data(data)
else:
self.opened_file = win.open_file('%s:%s:%s' % res[:3],
sublime.ENCODED_POSITION)
def show_panel_or_open(self, fn, sym, line, col, text):
win = sublime.active_window()
self._current_res = list()
if sym:
for name, _, pos in win.lookup_symbol_in_index(sym):
if name.endswith(fn):
line, col = pos
self._current_res.append((name, line, col, "f"))
else:
fn = fn.replace('/', os.sep)
all_folders = win.folders() + [os.path.dirname(v.file_name()) for v in win.views() if v.file_name()]
for folder in set(all_folders):
for root, _, _ in os.walk(folder):
name = os.path.abspath(os.path.join(root, fn))
if os.path.isfile(name):
self._current_res.append((name, line or 0, col or 0, "f"))
if os.path.isdir(name):
self._current_res.append((name, 0, 0, "d"))
if os.path.isfile(fn): # check for full path
self._current_res.append((fn, line or 0, col or 0, "f"))
elif os.path.isdir(fn):
self._current_res.append((fn, 0, 0, "d"))
self._current_res = list(set(self._current_res))
if not self._current_res:
sublime.error_message('File was not found\n\n\t%s' % fn)
if len(self._current_res) == 1:
self._on_panel_selection(0)
else:
entries = [self._format_res(res) for res in self._current_res]
win.show_quick_panel(entries, self._on_panel_selection)
def run(self, edit, fn=None):
point = self.view.sel()[0].begin()
line = self.view.substr(self.view.line(point))
match_link = self.LINK_PATTERN.search(line)
match_md = self.MD_LINK.search(line)
match_wiki = self.WIKI_LINK.search(line)
if match_link:
fn, sym, line, col, text = match_link.group('fn', 'sym', 'line', 'col', 'text')
elif match_md:
fn, sym, line, col, text = match_md.group('fn', 'sym', 'line', 'col', 'text')
# unescape some chars
fn = (fn.replace('\\(', '(').replace('\\)', ')'))
elif match_wiki:
fn = match_wiki.group('fn')
sym = match_wiki.group('sym') or match_wiki.group('symn')
line = match_wiki.group('line') or match_wiki.group('linen')
col = match_wiki.group('col') or match_wiki.group('coln')
text = match_wiki.group('text') or match_wiki.group('textn')
# unescape some chars
fn = (fn.replace('\\[', '[').replace('\\]', ']'))
if text:
text = (text.replace('\\[', '[').replace('\\]', ']'))
if fn:
self.show_panel_or_open(fn, sym, line, col, text)
if text:
sublime.set_timeout(lambda: self.find_text(self.opened_file, text, line), 300)
else:
sublime.status_message('Line does not contain a valid link to file')
def find_text(self, view, text, line):
result = view.find(text, view.sel()[0].a if line else 0, sublime.LITERAL)
view.sel().clear()
view.sel().add(result.a)
view.set_viewport_position(view.text_to_layout(view.size()), False)
view.show_at_center(result)
class PlainTasksSortByDate(PlainTasksBase):
def runCommand(self, edit):
if not re.search(r'(?su)%[Yy][-./ ]*%m[-./ ]*%d\s*%H.*%M', self.date_format):
# TODO: sort with dateutil so we wont depend on specific date_format
return
archive_pos = self.view.find(self.archive_name, 0, sublime.LITERAL)
if archive_pos:
have_date = r'(^\s*[^\n]*?\s\@(?:done|cancelled)\s*(\([\d\w,\.:\-\/ ]*\))[^\n]*$)'
all_tasks_prefixed_date = []
all_tasks = self.view.find_all(have_date, 0, u"\\2\\1", all_tasks_prefixed_date)
tasks_prefixed_date = []
tasks = []
for ind, task in enumerate(all_tasks):
if task.a > archive_pos.b:
tasks.append(task)
tasks_prefixed_date.append(all_tasks_prefixed_date[ind])
notes = []
for ind, task in enumerate(tasks):
note_line = task.end() + 1
while self.view.scope_name(note_line) == 'text.todo notes.todo ':
note = self.view.line(note_line)
notes.append(note)
tasks_prefixed_date[ind] += u'\n' + self.view.substr(note)
note_line = note.end() + 1
to_remove = tasks+notes
to_remove.sort()
for i in reversed(to_remove):
self.view.erase(edit, self.view.full_line(i))
tasks_prefixed_date.sort(reverse=self.view.settings().get('new_on_top', True))
eol = archive_pos.end()
for a in tasks_prefixed_date:
eol += self.view.insert(edit, eol, u'\n' + re.sub(r'^\([\d\w,\.:\-\/ ]*\)([^\b]*$)', u'\\1', a))
else:
sublime.status_message("Nothing to sort")
class PlainTasksRemoveBold(sublime_plugin.TextCommand):
def run(self, edit):
for s in reversed(list(self.view.sel())):
a, b = s.begin(), s.end()
for r in sublime.Region(b + 2, b), sublime.Region(a - 2, a):
self.view.erase(edit, r)
class PlainTasksStatsStatus(sublime_plugin.EventListener):
def on_activated(self, view):
if not view.score_selector(0, "text.todo") > 0:
return
self.set_stats(view)
def on_post_save(self, view):
self.on_activated(view)
@staticmethod
def set_stats(view):
view.set_status('PlainTasks', PlainTasksStatsStatus.get_stats(view))
@staticmethod
def get_stats(view):
msgf = view.settings().get('stats_format', '$n/$a done ($percent%) $progress Last task @done $last')
special_interest = re.findall(r'{{.*?}}', msgf)
for i in special_interest:
matches = view.find_all(i.strip('{}'))
pend, done, canc = [], [], []
for t in matches:
# one task may contain same tag/word several times—we count amount of tasks, not tags
t = view.line(t).a
scope = view.scope_name(t)
if 'pending' in scope and t not in pend:
pend.append(t)
elif 'completed' in scope and t not in done:
done.append(t)
elif 'cancelled' in scope and t not in canc:
canc.append(t)
msgf = msgf.replace(i, '%d/%d/%d'%(len(pend), len(done), len(canc)))
ignore_archive = view.settings().get('stats_ignore_archive', False)
if ignore_archive:
archive_pos = view.find(view.settings().get('archive_name', 'Archive:'), 0, sublime.LITERAL)
pend = len([i for i in view.find_by_selector('meta.item.todo.pending') if i.a < (archive_pos.a if archive_pos and archive_pos.a > 0 else view.size())])
done = len([i for i in view.find_by_selector('meta.item.todo.completed') if i.a < (archive_pos.a if archive_pos and archive_pos.a > 0 else view.size())])
canc = len([i for i in view.find_by_selector('meta.item.todo.cancelled') if i.a < (archive_pos.a if archive_pos and archive_pos.a > 0 else view.size())])
else:
pend = len(view.find_by_selector('meta.item.todo.pending'))
done = len(view.find_by_selector('meta.item.todo.completed'))
canc = len(view.find_by_selector('meta.item.todo.cancelled'))
allt = pend + done + canc
percent = ((done+canc)/float(allt))*100 if allt else 0
factor = int(round(percent/10)) if percent<90 else int(percent/10)
barfull = view.settings().get('bar_full', u'■')
barempty = view.settings().get('bar_empty', u'□')
progress = '%s%s' % (barfull*factor, barempty*(10-factor)) if factor else ''
tasks_dates = []
view.find_all('(^\s*[^\n]*?\s\@(?:done)\s*(\([\d\w,\.:\-\/ ]*\))[^\n]*$)', 0, "\\2", tasks_dates)
date_format = view.settings().get('date_format', '(%y-%m-%d %H:%M)')
tasks_dates = [check_parentheses(date_format, t, is_date=True) for t in tasks_dates]
tasks_dates.sort(reverse=True)
last = tasks_dates[0] if tasks_dates else '(UNKNOWN)'
msg = (msgf.replace('$o', str(pend))
.replace('$d', str(done))
.replace('$c', str(canc))
.replace('$n', str(done+canc))
.replace('$a', str(allt))
.replace('$percent', str(int(percent)))
.replace('$progress', progress)
.replace('$last', last)
)
return msg
class PlainTasksCopyStats(sublime_plugin.TextCommand):
def is_enabled(self):
return self.view.score_selector(0, "text.todo") > 0
def run(self, edit):
msg = self.view.get_status('PlainTasks')
replacements = self.view.settings().get('replace_stats_chars', [])
if replacements:
for o, r in replacements:
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(u'Archiving tree to {0}'.format(filename))
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(u"--- ✄ -----------------------\n")
fh.write(u"Archived {0}:\n".format(tznow().strftime(
self.date_format)))
# And, finally, write our data
fh.write(u"{0}\n".format(data))
return True
except Exception as e:
sublime.error_message(u"Error:\n\nUnable to append to {0}\n{1}".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(u"Error:\n\nInvalid filemask:{0}\nUsing default: {1}".format(
self.archive_org_filemask, self.archive_org_default_filemask))
return archive_filename
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)
if region.contains(line.b):
# there is no subtree
return sublime.Region(-1, -1)
if not region.empty():
region = sublime.Region(line.a, region.b)
return region
class PlainTasksFoldToTags(PlainTasksFold):
TAG = r'(?u)@\w+'
def run(self, edit):
tag_sels = [s for s in list(self.view.sel()) if 'tag.todo' in self.view.scope_name(s.a)]
if not tag_sels:
sublime.status_message('Cursor(s) must be placed on tag(s)')
return
tags = self.extract_tags(tag_sels)
tasks = [self.view.line(f) for f in self.view.find_all(r'[ \t](%s)' % '|'.join(tags)) if 'pending' in self.view.scope_name(f.a)]
if not tasks:
sublime.status_message('Pending tasks with given tags are not found')
print(tags, tag_sels)
return
self.exec_folding(self.add_projects_and_notes(tasks))
def extract_tags(self, tag_sels):
tags = []
for s in tag_sels:
start = end = s.a
limit = self.view.size()
while all(self.view.substr(start) != c for c in '@ \n'):
start -= 1
if start == 0:
break
while all(self.view.substr(end) != c for c in '( @\n'):
end += 1
if end == limit:
break
match = re.match(self.TAG, self.view.substr(sublime.Region(start, end)))
tag = match.group(0) if match else False
if tag and tag not in tags:
tags.append(tag)
return tags
class PlainTasksAddGutterIconsForTags(sublime_plugin.EventListener):
def on_activated(self, view):
if not view.score_selector(0, "text.todo") > 0:
return
view.erase_regions('critical')
view.erase_regions('high')
view.erase_regions('low')
view.erase_regions('today')
icon_critical = view.settings().get('icon_critical', '')
icon_high = view.settings().get('icon_high', '')
icon_low = view.settings().get('icon_low', '')
icon_today = view.settings().get('icon_today', '')
if not any((icon_critical, icon_high, icon_low, icon_today)):
return
critical = 'string.other.tag.todo.critical'
high = 'string.other.tag.todo.high'
low = 'string.other.tag.todo.low'
today = 'string.other.tag.todo.today'
r_critical = view.find_by_selector(critical)
r_high = view.find_by_selector(high)
r_low = view.find_by_selector(low)
r_today = view.find_by_selector(today)
if not any((r_critical, r_high, r_low, r_today)):
return
view.add_regions('critical', r_critical, critical, icon_critical, sublime.HIDDEN)
view.add_regions('high', r_high, high, icon_high, sublime.HIDDEN)
view.add_regions('low', r_low, low, icon_low, sublime.HIDDEN)
view.add_regions('today', r_today, today, icon_today, sublime.HIDDEN)
def on_post_save(self, view):
self.on_activated(view)
def on_load(self, view):
self.on_activated(view)