-
Notifications
You must be signed in to change notification settings - Fork 1
/
funcs.el
897 lines (802 loc) · 30.9 KB
/
funcs.el
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
;;; funcs.el --- JW Layer functions File for Spacemacs
;;
;; Copyright (c) 2012-2014 Sylvain Benner
;; Copyright (c) 2014-2015 Sylvain Benner & Contributors
;;
;; Author: Sylvain Benner <[email protected]>
;; URL: https://github.com/syl20bnr/spacemacs
;;
;; This file is not part of GNU Emacs.
;;
;;; License: GPLv3
(defun jw/hide-other ()
(interactive)
(save-excursion
(org-back-to-heading 'invisible-ok)
(hide-other)
(org-cycle)
(org-cycle)
(org-cycle)))
(defun jw/set-truncate-lines ()
"Toggle value of truncate-lines and refresh window display."
(interactive)
(setq truncate-lines (not truncate-lines))
;; now refresh window display (an idiom from simple.el):
(save-excursion
(set-window-start (selected-window)
(window-start (selected-window)))))
(defun jw/make-org-scratch ()
(interactive)
(find-file "/tmp/publish/scratch.org")
(gnus-make-directory "/tmp/publish"))
(defun jw/switch-to-scratch ()
(interactive)
(switch-to-buffer "*scratch*"))
;; Remove empty LOGBOOK drawers on clock out
(defun jw/remove-empty-drawer-on-clock-out ()
(interactive)
(save-excursion
(beginning-of-line 0)
(org-remove-empty-drawer-at (point))))
;; Exclude DONE state tasks from refile targets
(defun jw/verify-refile-target ()
"Exclude todo keywords with a done state from refile targets"
(not (member (nth 2 (org-heading-components)) org-done-keywords)))
(defun jw/org-auto-exclude-function (tag)
"Automatic task exclusion in the agenda with / RET"
(and (cond
((string= tag "hold")
t)
((string= tag "lab")
t))
(concat "-" tag)))
(defun jw/clock-in-to-next (kw)
"Switch a task from TODO to NEXT when clocking in.
Skips capture tasks, projects, and subprojects.
Switch projects and subprojects from NEXT back to TODO"
(when (not (and (boundp 'org-capture-mode) org-capture-mode))
(cond
((and (member (org-get-todo-state) (list "TODO"))
(jw/is-task-p))
"NEXT")
((and (member (org-get-todo-state) (list "NEXT"))
(jw/is-project-p))
"TODO"))))
(defun jw/find-project-task ()
"Move point to the parent (project) task if any"
(save-restriction
(widen)
(let ((parent-task (save-excursion (org-back-to-heading 'invisible-ok) (point))))
(while (org-up-heading-safe)
(when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
(setq parent-task (point))))
(goto-char parent-task)
parent-task)))
(defun jw/punch-in (arg)
"Start continuous clocking and set the default task to the
selected task. If no task is selected set the Organization task
as the default task."
(interactive "p")
(setq jw/keep-clock-running t)
(if (equal major-mode 'org-agenda-mode)
;;
;; We're in the agenda
;;
(let* ((marker (org-get-at-bol 'org-hd-marker))
(tags (org-with-point-at marker (org-get-tags-at))))
(if (and (eq arg 4) tags)
(org-agenda-clock-in '(16))
(jw/clock-in-organization-task-as-default)))
;;
;; We are not in the agenda
;;
(save-restriction
(widen)
;; Find the tags on the current task
(if (and (equal major-mode 'org-mode) (not (org-before-first-heading-p)) (eq arg 4))
(org-clock-in '(16))
(jw/clock-in-organization-task-as-default)))))
(defun jw/punch-out ()
(interactive)
(setq jw/keep-clock-running nil)
(when (org-clock-is-active)
(org-clock-out))
(org-agenda-remove-restriction-lock))
(defun jw/clock-in-default-task ()
(save-excursion
(org-with-point-at org-clock-default-task
(org-clock-in))))
(defun jw/clock-in-parent-task ()
"Move point to the parent (project) task if any and clock in"
(let ((parent-task))
(save-excursion
(save-restriction
(widen)
(while (and (not parent-task) (org-up-heading-safe))
(when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
(setq parent-task (point))))
(if parent-task
(org-with-point-at parent-task
(org-clock-in))
(when jw/keep-clock-running
(jw/clock-in-default-task)))))))
(defun jw/clock-in-organization-task-as-default ()
(interactive)
(org-with-point-at (org-id-find jw/organization-task-id 'marker)
(org-clock-in '(16))))
(defun jw/clock-out-maybe ()
(when (and jw/keep-clock-running
(not org-clock-clocking-in)
(marker-buffer org-clock-default-task)
(not org-clock-resolving-clocks-due-to-idleness))
(jw/clock-in-parent-task)))
(defun jw/clock-in-task-by-id (id)
"Clock in a task by id"
(org-with-point-at (org-id-find id 'marker)
(org-clock-in nil)))
(defun jw/clock-in-last-task (arg)
"Clock in the interrupted task if there is one
Skip the default task and get the next one.
A prefix arg forces clock in of the default task."
(interactive "p")
(let ((clock-in-to-task
(cond
((eq arg 4) org-clock-default-task)
((and (org-clock-is-active)
(equal org-clock-default-task (cadr org-clock-history)))
(caddr org-clock-history))
((org-clock-is-active) (cadr org-clock-history))
((equal org-clock-default-task (car org-clock-history)) (cadr org-clock-history))
(t (car org-clock-history)))))
(widen)
(org-with-point-at clock-in-to-task
(org-clock-in nil))))
(defun jw/init-cdlatex ()
"Initialize cdlatex"
(use-package cdlatex
:init (add-hook 'org-mode-hook 'turn-on-org-cdlatex)))
;; Phone capture template handling with BBDB lookup
;; Adapted from code by Gregory J. Grubbs
(defun jw/phone-call ()
"Return name and company info for caller from bbdb lookup"
(interactive)
(let* (name rec caller)
(setq name (completing-read "Who is calling? "
(bbdb-hashtable)
'bbdb-completion-predicate
'confirm))
(when (> (length name) 0)
;; Something was supplied - look it up in bbdb
(setq rec
(or (first
(or (bbdb-search (bbdb-records) name nil nil)
(bbdb-search (bbdb-records) nil name nil)))
name)))
;; Build the bbdb link if we have a bbdb record, otherwise just return the name
(setq caller (cond ((and rec (vectorp rec))
(let ((name (bbdb-record-name rec))
(company (bbdb-record-company rec)))
(concat "[[bbdb:"
name "]["
name "]]"
(when company
(concat " - " company)))))
(rec)
(t "NameOfCaller")))
(insert caller)))
(defun jw/is-project-p ()
"Any task with a todo keyword subtask"
(save-restriction
(widen)
(let ((has-subtask)
(subtree-end (save-excursion (org-end-of-subtree t)))
(is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
(save-excursion
(forward-line 1)
(while (and (not has-subtask)
(< (point) subtree-end)
(re-search-forward "^\*+ " subtree-end t))
(when (member (org-get-todo-state) org-todo-keywords-1)
(setq has-subtask t))))
(and is-a-task has-subtask))))
(defun jw/is-project-subtree-p ()
"Any task with a todo keyword that is in a project subtree.
Callers of this function already widen the buffer view."
(let ((task (save-excursion (org-back-to-heading 'invisible-ok)
(point))))
(save-excursion
(jw/find-project-task)
(if (equal (point) task)
nil
t))))
(defun jw/is-task-p ()
"Any task with a todo keyword and no subtask"
(save-restriction
(widen)
(let ((has-subtask)
(subtree-end (save-excursion (org-end-of-subtree t)))
(is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
(save-excursion
(forward-line 1)
(while (and (not has-subtask)
(< (point) subtree-end)
(re-search-forward "^\*+ " subtree-end t))
(when (member (org-get-todo-state) org-todo-keywords-1)
(setq has-subtask t))))
(and is-a-task (not has-subtask)))))
(defun jw/is-subproject-p ()
"Any task which is a subtask of another project"
(let ((is-subproject)
(is-a-task (member (nth 2 (org-heading-components)) org-todo-keywords-1)))
(save-excursion
(while (and (not is-subproject) (org-up-heading-safe))
(when (member (nth 2 (org-heading-components)) org-todo-keywords-1)
(setq is-subproject t))))
(and is-a-task is-subproject)))
(defun jw/list-sublevels-for-projects-indented ()
"Set org-tags-match-list-sublevels so when restricted to a subtree we list all subtasks.
This is normally used by skipping functions where this variable is already local to the agenda."
(if (marker-buffer org-agenda-restrict-begin)
(setq org-tags-match-list-sublevels 'indented)
(setq org-tags-match-list-sublevels nil))
nil)
(defun jw/list-sublevels-for-projects ()
"Set org-tags-match-list-sublevels so when restricted to a subtree we list all subtasks.
This is normally used by skipping functions where this variable is already local to the agenda."
(if (marker-buffer org-agenda-restrict-begin)
(setq org-tags-match-list-sublevels t)
(setq org-tags-match-list-sublevels nil))
nil)
(defun jw/toggle-next-task-display ()
(interactive)
(setq jw/hide-scheduled-and-waiting-next-tasks (not jw/hide-scheduled-and-waiting-next-tasks))
(when (equal major-mode 'org-agenda-mode)
(org-agenda-redo))
(message "%s WAITING and SCHEDULED NEXT Tasks" (if jw/hide-scheduled-and-waiting-next-tasks "Hide" "Show")))
(defun jw/skip-stuck-projects ()
"Skip trees that are not stuck projects"
(save-restriction
(widen)
(let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
(if (jw/is-project-p)
(let* ((subtree-end (save-excursion (org-end-of-subtree t)))
(has-next ))
(save-excursion
(forward-line 1)
(while (and (not has-next) (< (point) subtree-end) (re-search-forward "^\\*+ NEXT " subtree-end t))
(unless (member "WAITING" (org-get-tags-at))
(setq has-next t))))
(if has-next
nil
next-headline)) ; a stuck project, has subtasks but no next task
nil))))
(defun jw/skip-non-stuck-projects ()
"Skip trees that are not stuck projects"
;; (jw/list-sublevels-for-projects-indented)
(save-restriction
(widen)
(let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
(if (jw/is-project-p)
(let* ((subtree-end (save-excursion (org-end-of-subtree t)))
(has-next ))
(save-excursion
(forward-line 1)
(while (and (not has-next) (< (point) subtree-end) (re-search-forward "^\\*+ NEXT " subtree-end t))
(unless (member "WAITING" (org-get-tags-at))
(setq has-next t))))
(if has-next
next-headline
nil)) ; a stuck project, has subtasks but no next task
next-headline))))
(defun jw/skip-non-projects ()
"Skip trees that are not projects"
;; (jw/list-sublevels-for-projects-indented)
(if (save-excursion (jw/skip-non-stuck-projects))
(save-restriction
(widen)
(let ((subtree-end (save-excursion (org-end-of-subtree t))))
(cond
((jw/is-project-p)
nil)
((and (jw/is-project-subtree-p) (not (jw/is-task-p)))
nil)
(t
subtree-end))))
(save-excursion (org-end-of-subtree t))))
(defun jw/skip-project-trees-and-habits ()
"Skip trees that are projects"
(save-restriction
(widen)
(let ((subtree-end (save-excursion (org-end-of-subtree t))))
(cond
((jw/is-project-p)
subtree-end)
((org-is-habit-p)
subtree-end)
(t
nil)))))
(defun jw/skip-projects-and-habits-and-single-tasks ()
"Skip trees that are projects, tasks that are habits, single non-project tasks"
(save-restriction
(widen)
(let ((next-headline (save-excursion (or (outline-next-heading) (point-max)))))
(cond
((org-is-habit-p)
next-headline)
((and jw/hide-scheduled-and-waiting-next-tasks
(member "WAITING" (org-get-tags-at)))
next-headline)
((jw/is-project-p)
next-headline)
((and (jw/is-task-p) (not (jw/is-project-subtree-p)))
next-headline)
(t
nil)))))
(defun jw/skip-project-tasks-maybe ()
"Show tasks related to the current restriction.
When restricted to a project, skip project and sub project tasks, habits, NEXT tasks, and loose tasks.
When not restricted, skip project and sub-project tasks, habits, and project related tasks."
(save-restriction
(widen)
(let* ((subtree-end (save-excursion (org-end-of-subtree t)))
(next-headline (save-excursion (or (outline-next-heading) (point-max))))
(limit-to-project (marker-buffer org-agenda-restrict-begin)))
(cond
((jw/is-project-p)
next-headline)
((org-is-habit-p)
subtree-end)
((and (not limit-to-project)
(jw/is-project-subtree-p))
subtree-end)
((and limit-to-project
(jw/is-project-subtree-p)
(member (org-get-todo-state) (list "NEXT")))
subtree-end)
(t
nil)))))
(defun jw/skip-project-tasks ()
"Show non-project tasks.
Skip project and sub-project tasks, habits, and project related tasks."
(save-restriction
(widen)
(let* ((subtree-end (save-excursion (org-end-of-subtree t))))
(cond
((jw/is-project-p)
subtree-end)
((org-is-habit-p)
subtree-end)
((jw/is-project-subtree-p)
subtree-end)
(t
nil)))))
(defun jw/skip-non-project-tasks ()
"Show project tasks.
Skip project and sub-project tasks, habits, and loose non-project tasks."
(save-restriction
(widen)
(let* ((subtree-end (save-excursion (org-end-of-subtree t)))
(next-headline (save-excursion (or (outline-next-heading) (point-max)))))
(cond
((jw/is-project-p)
next-headline)
((org-is-habit-p)
subtree-end)
((and (jw/is-project-subtree-p)
(member (org-get-todo-state) (list "NEXT")))
subtree-end)
((not (jw/is-project-subtree-p))
subtree-end)
(t
nil)))))
(defun jw/skip-projects-and-habits ()
"Skip trees that are projects and tasks that are habits"
(save-restriction
(widen)
(let ((subtree-end (save-excursion (org-end-of-subtree t))))
(cond
((jw/is-project-p)
subtree-end)
((org-is-habit-p)
subtree-end)
(t
nil)))))
(defun jw/skip-non-subprojects ()
"Skip trees that are not projects"
(let ((next-headline (save-excursion (outline-next-heading))))
(if (jw/is-subproject-p)
nil
next-headline)))
(defun jw/skip-non-archivable-tasks ()
"Skip trees that are not available for archiving"
(save-restriction
(widen)
;; Consider only tasks with done todo headings as archivable candidates
(let ((next-headline (save-excursion (or (outline-next-heading) (point-max))))
(subtree-end (save-excursion (org-end-of-subtree t))))
(if (member (org-get-todo-state) org-todo-keywords-1)
(if (member (org-get-todo-state) org-done-keywords)
(let* ((daynr (string-to-int (format-time-string "%d" (current-time))))
(a-month-ago (* 60 60 24 (+ daynr 1)))
(last-month (format-time-string "%Y-%m-" (time-subtract (current-time) (seconds-to-time a-month-ago))))
(this-month (format-time-string "%Y-%m-" (current-time)))
(subtree-is-current (save-excursion
(forward-line 1)
(and (< (point) subtree-end)
(re-search-forward (concat last-month "\\|" this-month) subtree-end t)))))
(if subtree-is-current
subtree-end ; Has a date in this month or last month, skip it
nil)) ; available to archive
(or subtree-end (point-max)))
next-headline))))
(defun jw/display-inline-images ()
(condition-case nil
(org-display-inline-images)
(error nil)))
;; I'm lazy and don't want to remember the name of the project to publish when I modify
;; a file that is part of a project. So this function saves the file, and publishes
;; the project that includes this file
;; It's bound to C-S-F12 so I just edit and hit C-S-F12 when I'm done and move on to the next thing
(defun jw/save-then-publish (&optional force)
(interactive "P")
(save-buffer)
(org-save-all-org-buffers)
(let ((org-html-head-extra)
(org-html-validation-link "<a href=\"http://validator.w3.org/check?uri=referer\">Validate XHTML 1.0</a>"))
(org-publish-current-project force)))
;; Erase all reminders and rebuilt reminders for today from the agenda
(defun jw/org-agenda-to-appt ()
(interactive)
(setq appt-time-msg-list nil)
(org-agenda-to-appt))
(defun jw/plantuml-if ()
(incf jw/plantuml-if-count)
(number-to-string jw/plantuml-if-count))
(defun jw/plantuml-loop ()
(incf jw/plantuml-loop-count)
(number-to-string jw/plantuml-loop-count))
(defun jw/plantuml-reset-counters ()
(setq jw/plantuml-if-count 0
jw/plantuml-loop-count 0)
"")
(defun jw/org-todo (arg)
(interactive "p")
(if (equal arg 4)
(save-restriction
(jw/narrow-to-org-subtree)
(org-show-todo-tree nil))
(jw/narrow-to-org-subtree)
(org-show-todo-tree nil)))
(defun jw/widen ()
(interactive)
(if (equal major-mode 'org-agenda-mode)
(progn
(org-agenda-remove-restriction-lock)
(when org-agenda-sticky
(org-agenda-redo)))
(widen)))
(defun jw/restrict-to-file-or-follow (arg)
"Set agenda restriction to 'file or with argument invoke follow mode.
I don't use follow mode very often but I restrict to file all the time
so change the default 'F' binding in the agenda to allow both"
(interactive "p")
(if (equal arg 4)
(org-agenda-follow-mode)
(widen)
(jw/set-agenda-restriction-lock 4)
(org-agenda-redo)
(beginning-of-buffer)))
(defun jw/narrow-to-org-subtree ()
(widen)
(org-narrow-to-subtree)
(save-restriction
(org-agenda-set-restriction-lock)))
(defun jw/narrow-to-subtree ()
(interactive)
(if (equal major-mode 'org-agenda-mode)
(progn
(org-with-point-at (org-get-at-bol 'org-hd-marker)
(jw/narrow-to-org-subtree))
(when org-agenda-sticky
(org-agenda-redo)))
(jw/narrow-to-org-subtree)))
(defun jw/narrow-up-one-org-level ()
(widen)
(save-excursion
(outline-up-heading 1 'invisible-ok)
(jw/narrow-to-org-subtree)))
(defun jw/get-pom-from-agenda-restriction-or-point ()
(or (and (marker-position org-agenda-restrict-begin) org-agenda-restrict-begin)
(org-get-at-bol 'org-hd-marker)
(and (equal major-mode 'org-mode) (point))
org-clock-marker))
(defun jw/narrow-up-one-level ()
(interactive)
(if (equal major-mode 'org-agenda-mode)
(progn
(org-with-point-at (jw/get-pom-from-agenda-restriction-or-point)
(jw/narrow-up-one-org-level))
(org-agenda-redo))
(jw/narrow-up-one-org-level)))
(defun jw/narrow-to-org-project ()
(widen)
(save-excursion
(jw/find-project-task)
(jw/narrow-to-org-subtree)))
(defun jw/narrow-to-project ()
(interactive)
(if (equal major-mode 'org-agenda-mode)
(progn
(org-with-point-at (jw/get-pom-from-agenda-restriction-or-point)
(jw/narrow-to-org-project)
(save-excursion
(jw/find-project-task)
(org-agenda-set-restriction-lock)))
(org-agenda-redo)
(beginning-of-buffer))
(jw/narrow-to-org-project)
(save-restriction
(org-agenda-set-restriction-lock))))
(defun jw/view-next-project ()
(interactive)
(let (num-project-left current-project)
(unless (marker-position org-agenda-restrict-begin)
(goto-char (point-min))
;; Clear all of the existing markers on the list
(while jw/project-list
(set-marker (pop jw/project-list) nil))
(re-search-forward "Tasks to Refile")
(forward-visible-line 1))
;; Build a new project marker list
(unless jw/project-list
(while (< (point) (point-max))
(while (and (< (point) (point-max))
(or (not (org-get-at-bol 'org-hd-marker))
(org-with-point-at (org-get-at-bol 'org-hd-marker)
(or (not (jw/is-project-p))
(jw/is-project-subtree-p)))))
(forward-visible-line 1))
(when (< (point) (point-max))
(add-to-list 'jw/project-list (copy-marker (org-get-at-bol 'org-hd-marker)) 'append))
(forward-visible-line 1)))
;; Pop off the first marker on the list and display
(setq current-project (pop jw/project-list))
(when current-project
(org-with-point-at current-project
(setq jw/hide-scheduled-and-waiting-next-tasks nil)
(jw/narrow-to-project))
;; Remove the marker
(setq current-project nil)
(org-agenda-redo)
(beginning-of-buffer)
(setq num-projects-left (length jw/project-list))
(if (> num-projects-left 0)
(message "%s projects left to view" num-projects-left)
(beginning-of-buffer)
(setq jw/hide-scheduled-and-waiting-next-tasks t)
(error "All projects viewed.")))))
(defun jw/set-agenda-restriction-lock (arg)
"Set restriction lock to current task subtree or file if prefix is specified"
(interactive "p")
(let* ((pom (jw/get-pom-from-agenda-restriction-or-point))
(tags (org-with-point-at pom (org-get-tags-at))))
(let ((restriction-type (if (equal arg 4) 'file 'subtree)))
(save-restriction
(cond
((and (equal major-mode 'org-agenda-mode) pom)
(org-with-point-at pom
(org-agenda-set-restriction-lock restriction-type))
(org-agenda-redo))
((and (equal major-mode 'org-mode) (org-before-first-heading-p))
(org-agenda-set-restriction-lock 'file))
(pom
(org-with-point-at pom
(org-agenda-set-restriction-lock restriction-type))))))))
(defun jw/agenda-sort (a b)
"Sorting strategy for agenda items.
Late deadlines first, then scheduled, then non-late deadlines"
(let (result num-a num-b)
(cond
;; time specific items are already sorted first by org-agenda-sorting-strategy
;; non-deadline and non-scheduled items next
((jw/agenda-sort-test 'jw/is-not-scheduled-or-deadline a b))
;; deadlines for today next
((jw/agenda-sort-test 'jw/is-due-deadline a b))
;; late deadlines next
((jw/agenda-sort-test-num 'jw/is-late-deadline '> a b))
;; scheduled items for today next
((jw/agenda-sort-test 'jw/is-scheduled-today a b))
;; late scheduled items next
((jw/agenda-sort-test-num 'jw/is-scheduled-late '> a b))
;; pending deadlines last
((jw/agenda-sort-test-num 'jw/is-pending-deadline '< a b))
;; finally default to unsorted
(t (setq result nil)))
result))
(defun jw/is-not-scheduled-or-deadline (date-str)
(and (not (jw/is-deadline date-str))
(not (jw/is-scheduled date-str))))
(defun jw/is-due-deadline (date-str)
(string-match "Deadline:" date-str))
(defun jw/is-late-deadline (date-str)
(string-match "\\([0-9]*\\) d\. ago:" date-str))
(defun jw/is-pending-deadline (date-str)
(string-match "In \\([^-]*\\)d\.:" date-str))
(defun jw/is-deadline (date-str)
(or (jw/is-due-deadline date-str)
(jw/is-late-deadline date-str)
(jw/is-pending-deadline date-str)))
(defun jw/is-scheduled (date-str)
(or (jw/is-scheduled-today date-str)
(jw/is-scheduled-late date-str)))
(defun jw/is-scheduled-today (date-str)
(string-match "Scheduled:" date-str))
(defun jw/is-scheduled-late (date-str)
(string-match "Sched\.\\(.*\\)x:" date-str))
(defun jw/show-org-agenda ()
(interactive)
(if org-agenda-sticky
(switch-to-buffer "*Org Agenda( )*")
(switch-to-buffer "*Org Agenda*"))
(delete-other-windows))
(defun jw/toggle-insert-inactive-timestamp ()
(interactive)
(setq jw/insert-inactive-timestamp (not jw/insert-inactive-timestamp))
(message "Heading timestamps are %s" (if jw/insert-inactive-timestamp "ON" "OFF")))
(defun jw/insert-inactive-timestamp ()
(interactive)
(org-insert-time-stamp nil t t nil nil nil))
(defun jw/insert-heading-inactive-timestamp ()
(save-excursion
(when jw/insert-inactive-timestamp
(org-return)
(org-cycle)
(jw/insert-inactive-timestamp))))
(defun jw/prepare-meeting-notes ()
"Prepare meeting notes for email
Take selected region and convert tabs to spaces, mark TODOs with leading >>>, and copy to kill ring for pasting"
(interactive)
(let (prefix)
(save-excursion
(save-restriction
(narrow-to-region (region-beginning) (region-end))
(untabify (point-min) (point-max))
(goto-char (point-min))
(while (re-search-forward "^\\( *-\\\) \\(TODO\\|DONE\\): " (point-max) t)
(replace-match (concat (make-string (length (match-string 1)) ?>) " " (match-string 2) ": ")))
(goto-char (point-min))
(kill-ring-save (point-min) (point-max))))))
(defun jw/mark-next-parent-tasks-todo ()
"Visit each parent task and change NEXT states to TODO"
(let ((mystate (or (and (fboundp 'org-state)
state)
(nth 2 (org-heading-components)))))
(when mystate
(save-excursion
(while (org-up-heading-safe)
(when (member (nth 2 (org-heading-components)) (list "NEXT"))
(org-todo "TODO")))))))
(defun jw/mail-subtree ()
(interactive)
(org-mark-subtree)
(org-mime-subtree))
(defun jw/auto-tex-cmd (latex)
"When exporting from .org with latex, automatically run latex,
pdflatex, or xelatex as appropriate, using latexmk."
(let ((texcmd))
;; default command: oldstyle latex via dvi
(setq texcmd '("latexmk -dvi -pdfps -quiet %f"
"latexmk -c"))
;; pdflatex -> .pdf
(if (string-match "LATEX_CMD: pdflatex" (buffer-string))
(setq texcmd '("latexmk -pdf -quiet %f"
"latexmk -c")))
;; xelatex -> .pdf
(if (string-match "LATEX_CMD: xelatex" (buffer-string))
(setq texcmd '("latexmk -pdflatex=xelatex -pdf -quiet %f"
"latexmk -c")))
;; xelatex -> .pdf (beamer)
(if (string-match "STARTUP: beamer" (buffer-string))
(setq texcmd '("latexmk -pdflatex=xelatex -pdf -quiet %f"
"latexmk -c")))
;; LaTeX compilation command
(setq org-latex-pdf-process texcmd)))
(defun jw/auto-tex-parameters (latex)
"Automatically select the tex packages to include."
;; default packages for ordinary latex or pdflatex export
(setq org-latex-default-packages-alist
'(("AUTO" "inputenc" t)
("T1" "fontenc" t)
("" "fixltx2e" nil)
("" "wrapfig" nil)
("" "soul" t)
("" "textcomp" t)
("" "marvosym" t)
("" "wasysym" t)
("" "latexsym" t)
("" "amssymb" t)
("" "lmodern" t)
("" "hyperref" nil)))
;; Packages to include when xelatex is used
(if (string-match "LATEX_CMD: xelatex" (buffer-string))
(setq org-latex-default-packages-alist
'(
("" "rotating" t)
("" "soul" t)
("" "url" t)
("" "zhfontcfg" t)
("" "zhparcfg" t)
("xetex" "hyperref" nil)
)))
(if (string-match "LATEX_CMD: xelatex" (buffer-string))
(setq org-latex-classes
(cons '("cnbook"
("\\part{%s}" . "\\part*{%s}")
("\\chapter{%s}" . "\\chapter*{%s}")
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}"))
org-latex-classes)))
(if (string-match "STARTUP: beamer" (buffer-string))
(setq org-latex-default-packages-alist
'(
("" "beamerthemesplit" nil)
("" "zhfontcfg" t)))))
(defun jw/read-date ()
"Parse date for capturing ledger entries via org mode"
(replace-regexp-in-string "-" "/" (org-read-date)))
(defun jw/shell-command-on-buffer()
"Asks for a command and executes it in inferior shell with current buffer
as input."
(interactive)
(shell-command-on-region
(point-min) (point-max)
(read-shell-command "Shell command on buffer: ")))
(defun jw/run-current-file ()
"Execute the current file.
For example, if the current buffer is the file xx.py, then it'll call 「python xx.py」 in a shell.
The file can be php, perl, python, ruby, javascript, bash, ocaml, vb, elisp.
File suffix is used to determine what program to run.
If the file is modified, ask if you want to save first.
URL `http://ergoemacs.org/emacs/elisp_run_current_file.html'
version 2014-10-28"
(interactive)
(let* (
(suffixMap
;; (‹extension› . ‹shell program name›)
`(
;; ("php" . "php")
;; ("pl" . "perl")
;; ("py" . "python")
;; ("py3" . ,(if (string-equal system-type "windows-nt") "c:/Python32/python.exe" "python3"))
;; ("rb" . "ruby")
;; ("js" . "node") ; node.js
;; ("sh" . "bash")
;; ("clj" . "java -cp /home/xah/apps/clojure-1.6.0/clojure-1.6.0.jar clojure.main")
;; ("ml" . "ocaml")
;; ("vbs" . "cscript")
;; ("pov" . "/usr/local/bin/povray +R2 +A0.1 +J1.2 +Am2 +Q9 +H480 +W640")
("dat" . "pfc3d")
("fis" . "pfc3d")
("FIS" . "pfc3d")
("dvr" . "pfc3d")
("DVR" . "pfc3d")
))
(fName (if (eq system-type 'windows-nt)
(replace-regexp-in-string "/" "\\" (buffer-file-name) t t)
(buffer-file-name)))
(fSuffix (file-name-extension fName))
(progName (cdr (assoc fSuffix suffixMap)))
;; (cmdStr (concat progName " " fName))
)
(when (buffer-modified-p)
(when (y-or-n-p "Buffer modified. Do you want to save first?")
(save-buffer)))
(if (string-equal fSuffix "el") ; special case for emacs lisp
(load fName)
(if progName
(progn
(message "Running…")
;; (shell-command cmdStr "*jw/run-current-file output*" )
(start-process "jw/process" "*jw/run-current-file output*" progName fName))
(message "No recognized program file suffix for this file.")))))