-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalb-org-behavior.el
More file actions
1479 lines (1293 loc) · 56.4 KB
/
alb-org-behavior.el
File metadata and controls
1479 lines (1293 loc) · 56.4 KB
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
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;
;; alb-org-mode/alb-org-behavior.el
;;
;; Copyright (C) 2010-2016 Andrew Lincoln Burrow
;;
;; This library is free software; you can redistribute it and/or
;; modify it under the terms of the GNU General Public License as
;; published by the Free Software Foundation; either version 2 of
;; the License, or (at your option) any later version.
;;
;; This library is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public
;; License along with this library; if not, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston,
;; MA 02111-1307, USA.
;;
;; - Functions to configure Org-Mode
;;
;;
;;
;; PROVIDED FEATURE
;; ---------------------------------------------------------------------
;;
(provide 'alb-org-behavior)
;;
;;
;; REQUIRED FEATURES
;; ---------------------------------------------------------------------
;;
(require 'org)
;;
;;
;; CONSTANT DECLARATIONS
;; ---------------------------------------------------------------------
;;
(defconst alb-org-heading-incoming
"01-Think"
"Title of the Org-Mode heading designated as the tree
containing incoming tasks.")
(defconst alb-re-org-date
(concat "[0-9]\\{4\\}-[0-9]\\{2\\}-[0-9]\\{2\\}"
" \\(?:Mon\\|Tue\\|Wed\\|Thu\\|Fri\\|Sat\\|Sun\\)"
"\\(?: [0-9]\\{2\\}:[0-9]\\{2\\}\\)?"
"\\(?: \\(\+\\|\+\+\\|\.\+\\)[0-9]+[hdwmy]\\)?"
"\\(?: -[0-9]+[hdwmy]\\)?"
)
"Regexp matching an Org-Mode timestamp including repeaters. It
consumes 0 subexpression.")
(defconst alb-re-org-todo
"\\(DUTY\\|TODO\\|WAIT\\|HOLD\\|STOP\\|DONE\\)"
"Regexp matching an Org-Mode todo state. It consumes 1
subexpression.")
(defconst alb-re-org-priority
"\\(\\[#[A-C]\\]\\)"
"Regexp matching an Org-Mode priority. It consumes 1
subexpression.")
(defconst alb-re-org-heading
(concat "^"
"\\(\\*+\\)? *"
alb-re-org-todo "? *"
alb-re-org-priority "? *"
"\\(.*?\\) *"
"\\(:[a-z:@_]+:\\)? *"
"$")
"Regexp matching an Org-Mode heading with anchoring to the
start and end of line. It consumes 5 subexpressions as follows.
1. (optional) Asterisks
2. (optional) TODO keyword
3. (optional) Priority
4. (mandatory) Heading text including (optional) statistics and
trailing whitespace
5. (optional) Tags")
(defconst alb-re-org-heading-incoming
(concat "^"
"\\(\\*+\\)? *"
alb-re-org-priority "? *"
alb-org-heading-incoming " *"
"\\(:[a-z:@_]+:\\)? *"
"$")
"Regexp matching the Org-Mode heading designated as the tree
containing incoming tasks. The regexp is anchored to the start
and end of line, and consumes 3 subexpressions as follows.
1. (optional) Asterisks
2. (optional) Priority
3. (optional) Tags")
(defconst alb-re-org-metadata
(concat "\\(?:\n[ \t]*"
"\\(?:"
"\\(?:DEADLINE\\|SCHEDULED\\):[ \t]+<" alb-re-org-date ">"
"\\(?:[ \t]+\\(?:DEADLINE\\|SCHEDULED\\):[ \t]+<"
alb-re-org-date ">\\)?"
"\\|"
":\\(?:LOGBOOK\\|PROPERTIES\\):[ \t]*\\(?:\n.*\\)*?\n[ \t]*:END:"
"\\)[ \t]*"
"\\)*")
"Regexp matching the possibly-empty properties metadata for a
heading. The match includes the newline terminating the headline,
but excludes the newline terminating the metadata. It consumes 0
subexpressions.")
;;
;;
;; CUSTOMISATION
;; ---------------------------------------------------------------------
;;
(defgroup alb-org nil
"Customise aspects of Org-Mode for albcorp"
:group 'org)
(defcustom alb-org-abbrev-separtr-re
"[-_~ \t\n\r,;]+"
"Regexp used to split the string-to-abbreviate into words"
:group 'alb-org
:type 'regexp)
(defcustom alb-org-abbrev-illegal-re
"[^-_a-zA-Z0-9+]"
"Regexp used to recognise illegal characters in the string-to-abbreviate"
:group 'alb-org
:type 'regexp)
(defcustom alb-org-abbrev-ignore-words
'("the" "on" "in" "off" "a" "for" "by" "of" "and" "is" "to" "with")
"List of words to be removed from the string-to-abbreviate"
:group 'alb-org
:type '(repeat string))
(defcustom alb-org-abbrev-words-max
5
"Maximum number of words to use"
:group 'alb-org
:type 'integer)
(defcustom alb-org-abbrev-chars-max
30
"Maximum number of characters in the abbreviated string"
:group 'alb-org
:type 'integer)
(defcustom alb-org-abbrev-separator
"_"
"String separating different words in the abbreviated string")
(defcustom alb-org-project-dirname-patterns
'(("^Work on =\\([A-Z]+-[0-9]+\\)=: \\*\\([^*]+\\)\\*\\s-:foc_\\([^:]*\\):*.*$"
. "~/Desktop/10-Files/\\3/\\1")
("^Work on =\\([a-z_-]+\\)= issue \\([0-9]+\\)\\: \\*\\([^*]+\\)\\*\\s-:foc_\\([^:]*\\):*.*$"
. "~/Desktop/10-Files/\\4/\\1/\\2")
("^\\(.*\\)\\s-+:foc_\\([^:]*\\):.*$"
. "~/Desktop/10-Files/\\2/<<\\1>>")
("^.*$"
. "~/Desktop/10-Files"))
"Pattern-replacement pairs to transform headings to project directory names
Each pair comprises a regular expression and a replacement
string. These are passed to `replace-regexp-in-string`, and then
each string enclosed inside double angle brackets is transformed
using `alb-org-abbrev`. The input text is the concatenation of
the heading without its TODO keywords and tags, a space, and all
inherited tags as returned by `org-entry-properties`. The
patterns are tested in turn, and the first pattern that matches
the input is transformed into the project directory filename.
You should include a pair that matches all inputs, and outputs
the default directory."
:group 'alb-org
:type '(repeat (cons regexp string)))
;;
;;
;; FACE DECLARATIONS
;; ---------------------------------------------------------------------
;;
(defface alb-org-keyword-duty
'((t :foreground "CornflowerBlue" :weight bold))
"Face used for DUTY keyword."
:group 'org-faces)
(defface alb-org-keyword-todo
'((t :foreground "PaleGreen" :weight bold))
"Face used for TODO keyword."
:group 'org-faces)
(defface alb-org-keyword-wait
'((t :foreground "LightGoldenrod" :weight bold))
"Face used for WAIT keyword."
:group 'org-faces)
(defface alb-org-keyword-hold
'((t :foreground "grey" :weight bold))
"Face used for HOLD keyword."
:group 'org-faces)
(defface alb-org-keyword-stop
'((t :foreground "grey30" :weight bold))
"Face used for STOP keyword."
:group 'org-faces)
(defface alb-org-keyword-done
'((t :foreground "IndianRed" :weight bold))
"Face used for DONE keyword."
:group 'org-faces)
;;
;;
;; FUNCTION DEFINITIONS
;; ---------------------------------------------------------------------
;;
;;
;; String functions
;;
(defun alb-org-chomp (str)
"Chomp leading and tailing whitespace from STR."
(replace-regexp-in-string (rx (or (: bos (* (any " \t\n")))
(: (* (any " \t\n")) eos)))
""
str))
(defun alb-org-abbrev (input)
"Convert INPUT (a sentence) to a suitable abbreviation. In
particular, an abbreviation for the basename of a file.
See `alb-org-abbrev-separtr-re`, `alb-org-abbrev-illegal-re`,
`alb-org-abbrev-ignore-words`, `alb-org-abbrev-words-max`,
`alb-org-abbrev-chars-max`, and `alb-org-abbrev-separator` for
the customisable parameters.
Copied in spirit from \"reftex.el\"."
(let ((init-words (split-string input alb-org-abbrev-separtr-re))
final-words
word
(abbrev-re (concat
"\\`\\("
(make-string 4 ?.) ; Minimum chars to include.
"[^aeiou]*" ; Chars before abbrev point in word.
"\\)"
"[aeiou]" ; Chars after abbrev point in word.
(make-string 1 ?.) ; Minimum chars to exclude.
)))
;; Remove words from the ignore list or with funny characters.
(while (setq word (pop init-words))
(setq word (downcase word))
(cond
((member word alb-org-abbrev-ignore-words)
)
((string-match alb-org-abbrev-illegal-re word)
(setq word (replace-match "" nil nil word))
(while (string-match alb-org-abbrev-illegal-re word)
(setq word (replace-match "" nil nil word)))
(push word final-words))
(t
(push word final-words))))
(setq final-words (nreverse final-words))
;; Restrict number of words.
(if (> (length final-words) alb-org-abbrev-words-max)
(setcdr (nthcdr (1- alb-org-abbrev-words-max) final-words) nil))
;; Abbreviate words.
(setq final-words
(mapcar
(function
(lambda (w) (if (string-match abbrev-re w)
(match-string 1 w)
w)))
final-words))
;; Construct string with alb-org-abbrev-separators.
(setq input
(mapconcat 'identity final-words alb-org-abbrev-separator))
;; Shorten if still too long.
(setq input
(if (> (length input) alb-org-abbrev-chars-max)
(substring input 0 alb-org-abbrev-chars-max)
input))))
;;
;; Whitespace cleanup
;;
(defun alb-org-whitespace-cleanup ()
"Clean buffer of Org-Mode specific whitespace issues
Clean buffer content. Return nil, so that the function can be
safely added to the `write-contents-functions` hook."
(save-excursion
(goto-char (point-min))
(while (re-search-forward "^\n\\{2,\\}" nil t)
(replace-match "\n" nil nil))
(goto-char (point-min))
(while (re-search-forward (concat "\\([^[:space:]]\\)\n\\("
org-outline-regexp "\\)")
nil t)
(replace-match "\\1\n\n\\2" nil nil))
(goto-char (point-min))
(while (re-search-forward org-outline-regexp-bol nil t)
(org-set-tags nil t)
(end-of-line 1))
nil))
(defun alb-add-org-whitespace-cleanup ()
"Add buffer local hook to clean buffer of Org-Mode specific whitespace issues
Add `alb-org-whitespace-cleanup` to the buffer local
`write-contents-functions`."
(add-hook 'write-contents-functions 'alb-org-whitespace-cleanup))
;;
;; Structure navigation
;;
(defun alb-org-entry-structure-visit (past-pos)
"Recursively collect the entry structure upto PAST-POS
Collect the preamble and list at point and advance point to the
end of the list. Recur on the space remaining before PAST-POS.
See `alb-org-entry-structure` for further details."
(let* ((text-pos (if (looking-at "[[:space:]]*")
(match-end 0)))
(item-pos (if (re-search-forward (org-item-beginning-re) past-pos t)
(match-beginning 0))))
(cond
((<= past-pos text-pos)
nil)
((not item-pos)
(list (cons text-pos nil)))
(t
(let ((struct (progn (goto-char item-pos)
(org-list-struct))))
(cons (cons text-pos struct)
(progn (goto-char (org-list-get-bottom-point struct))
(alb-org-entry-structure-visit past-pos))))))))
(defun alb-org-entry-structure (text-pos past-pos)
"Parse the entry structure between TEXT-POS and PAST-POS
Define an *entry* as the text between the end of a heading's
meta-data, and the start of the next heading or the end of
buffer. Define a *preamble* as the possibly empty text before
each top-level list in the entry. Let TEXT-POS be the position
of the first non-whitespace character in an entry, and PAST-POS
be the first position after the entry. Parse the entry into a
sequence of preamble-list pairs represented by an alist that maps
the position of the first non-whitespace character to the alist
returned by `org-list-struct`. An preamble is empty when the
position of the preamble is the position of the top of the list.
This function customises Org-Mode."
(save-excursion (goto-char text-pos)
(beginning-of-line)
(alb-org-entry-structure-visit past-pos)))
(defun alb-org-visible-pos (pos)
"Return POS only if it is visible
This function customises Org-Mode."
(save-excursion (goto-char pos)
(and (not (outline-invisible-p))
(point))))
(defun alb-org-visible-preamble-pos (text-pos star-pos)
"Return TEXT-POS only if its distinct from STAR-POS and visible
This function customises Org-Mode."
(if (< text-pos star-pos)
(save-excursion (goto-char text-pos)
(and (not (outline-invisible-p))
(point)))))
(defun alb-org-head-pos (curr-pos)
"Return position of first non-whitespace character in the heading
Let CURR-POS be a position in the heading or its entry. Return
the position of the start the enclosing heading. This function
customises Org-Mode."
(save-excursion (goto-char curr-pos)
(outline-back-to-heading t)
(point)))
(defun alb-org-text-pos (head-pos)
"Return position of first non-whitespace character in the entry
Let HEAD-POS be the position of a heading. Return the position
of the first non-whitespace character after the heading and
associated metadata. This function customises Org-Mode."
(save-excursion (goto-char head-pos)
(looking-at alb-re-org-heading)
(goto-char (match-end 0))
(looking-at alb-re-org-metadata)
(goto-char (match-end 0))
(looking-at "[[:space:]]*")
(match-end 0)))
(defun alb-org-past-pos (head-pos)
"Return position of first non-whitespace character after the entry
Let HEAD-POS be the position of a heading. Return the position
of the first non-whitespace character after the entry of the
heading. This function customises Org-Mode."
(save-excursion (goto-char head-pos)
(if (outline-next-heading)
(point)
(point-max))))
(defun alb-org-star-pos (item-pos)
"Return position of bullet in list item
Let ITEM-POS be the start of the line of a list item. Return the
position of bullet. This function customises Org-Mode."
(save-excursion (goto-char item-pos)
(looking-at org-list-full-item-re)
(match-beginning 1)))
(defun alb-org-up-item-pos (curr-pos item-pos struct parents)
"Find first visible ancestor of CURR-POS in STRUCT
Let CURR-POS be the position of point, ITEM-POS be the start of
an enclosing list item, STRUCT be the enclosing list structure as
returned by `org-list-struct`, and PARENTS be the alist returned
by `org-list-parents-alist`. Recur on the parents of ITEM-POS
searching for a visible ancestor. If found, return the position
of the bullet of the ancestor list item. Do not move point.
This function customises Org-Mode."
(if item-pos
(let ((star-pos (alb-org-star-pos item-pos))
(next-item-pos (org-list-get-parent item-pos struct parents)))
(or (and (< star-pos curr-pos)
(alb-org-visible-pos star-pos))
(alb-org-up-item-pos curr-pos next-item-pos struct parents)))))
(defun alb-org-up-text-pos (curr-pos entry-struct)
"Find first visible ancestor of CURR-POS in ENTRY-STRUCT
Let CURR-POS be the position of point, and ENTRY-STRUCT be the
alist returned by `alb-org-entry-structure`. Recur on
ENTRY-STRUCT searching for a containing preamble or list. In
turn, visit the bullet of each enclosing list item, and the first
non-whitespace character of the immediately preceeding preamble
until a visible character is located. If found, return the
position. Do not move point. This function customises
Org-Mode."
(if entry-struct
(let ((text-pos (caar entry-struct))
(struct (cdar entry-struct)))
(cond
((<= curr-pos text-pos)
nil)
(struct
(let ((star-pos (alb-org-star-pos (org-list-get-top-point struct)))
(past-pos (org-list-get-bottom-point struct)))
(cond
((<= curr-pos star-pos)
(alb-org-visible-preamble-pos text-pos star-pos))
((<= curr-pos past-pos)
(or (alb-org-up-item-pos curr-pos (org-in-item-p) struct
(org-list-parents-alist struct))
(alb-org-visible-preamble-pos text-pos star-pos)))
(t
(alb-org-up-text-pos curr-pos (cdr entry-struct))))))
(t
(alb-org-visible-pos text-pos))))))
(defun alb-org-up-head-pos (curr-pos head-pos)
"Find first visible ancestor of CURR-POS
Let CURR-POS be the position of point, and HEAD-POS be the start
of the enclosing heading. Assume HEAD-POS < CURR-POS. In turn,
visit the bullet of each enclosing list item, the first
non-whitespace character of the immediately preceeding preamble,
and the first character in the heading until a visible character
is located. If found, return the position. Do not move point.
This function customises Org-Mode."
(let ((text-pos (alb-org-text-pos head-pos))
(past-pos (alb-org-past-pos head-pos)))
(or (and (< text-pos curr-pos)
(alb-org-up-text-pos curr-pos (alb-org-entry-structure text-pos
past-pos)))
(alb-org-visible-pos head-pos))))
(defun alb-org-up-heading-pos (head-pos)
"Find first visible ancestor heading of HEAD-POS
Let HEAD-POS be the start of the enclosing heading. Search
upward for a visible ancestor heading. If found, return the
position. Do not move point. This function customises
Org-Mode."
(save-excursion (goto-char head-pos)
(org-up-heading-safe)
(and (outline-on-heading-p)
(point))))
(defun alb-org-up-structure ()
"Move backward out one level of Org-Mode structure
Search backward for the first visible ancestor list item,
preamble, or heading. Place point at the bullet of a list item,
or first character of a preamble or heading. This function is
intended to be the analogue of `backward-up-list` for the case of
navigating Org-Mode structures. It navigates headings and plain
lists, with text outside of a list being a special case. Text
preceeding a list is treated as a list item (a preamble) of a
virtual enclosing list. This function customises Org-Mode."
(interactive)
(let* ((curr-pos (point))
(head-pos (alb-org-head-pos curr-pos))
(next-pos (if (= curr-pos head-pos)
(alb-org-up-heading-pos head-pos)
(or (alb-org-up-head-pos curr-pos head-pos)
(alb-org-up-heading-pos head-pos)))))
(if next-pos
(goto-char next-pos))))
(defun alb-org-down-item-pos (curr-pos item-pos struct parents)
"Find first visible descendant of CURR-POS in STRUCT
Let CURR-POS be the position of point, ITEM-POS be the start of
an enclosing list item, STRUCT be the enclosing list structure as
returned by `org-list-struct`, and PARENTS be the alist returned
by `org-list-parents-alist`. Test for a visible child of
ITEM-POS. If found, return the position of the bullet of the
child list item. Do not move point. This function customises
Org-Mode."
(if item-pos
(let ((star-pos (alb-org-star-pos item-pos))
(next-item-pos (car (org-list-get-children item-pos struct
parents))))
(if (< curr-pos star-pos)
(alb-org-visible-pos star-pos)
(alb-org-down-item-pos curr-pos next-item-pos struct parents)))))
(defun alb-org-down-text-pos (curr-pos entry-struct)
"Find first visible descendant of CURR-POS in ENTRY-STRUCT
Let CURR-POS be the position of point, and ENTRY-STRUCT be the
alist returned by `alb-org-entry-structure`. Recur on
ENTRY-STRUCT searching for a containing preamble or list. If
CURR-POS falls before a preamble, stop at the first character of
the preamble. If CURR-POS falls before a list, stop at the first
bullet. If CURR-POS falls within a list item, stop at the first
bullet of any nested list. If found and visible, return the
position. Do not move point. This function customises
Org-Mode."
(if entry-struct
(let ((text-pos (caar entry-struct))
(struct (cdar entry-struct)))
(cond
((< curr-pos text-pos)
(alb-org-visible-pos text-pos))
(struct
(let ((star-pos (alb-org-star-pos (org-list-get-top-point struct)))
(past-pos (org-list-get-bottom-point struct)))
(cond
((< curr-pos star-pos)
(alb-org-visible-pos star-pos))
((< curr-pos past-pos)
(alb-org-down-item-pos curr-pos (org-in-item-p) struct
(org-list-parents-alist struct)))
(t
(alb-org-down-text-pos curr-pos (cdr entry-struct))))))))))
(defun alb-org-down-heading-pos (head-pos)
"Find first visible descendant heading of HEAD-POS
Let HEAD-POS be the start of the enclosing heading. Visit the
next visible heading. If found at a deeper level than the
current heading, return the position. Do not move point. This
function customises Org-Mode."
(save-excursion (goto-char head-pos)
(let ((curr-level (org-current-level)))
(outline-next-visible-heading 1)
(and (outline-on-heading-p)
(< curr-level (org-current-level))
(point)))))
(defun alb-org-down-structure ()
"Move down one level of Org-Mode structure
Search forward for the first visible descendant list item,
preamble, or heading. Place point at the bullet of a list item,
or first character of a preamble or heading. This function is
intended to be the analogue of `down-list` for the case of
navigating Org-Mode structures. It navigates headings and plain
lists, with text outside of a list being a special case. Text
preceeding a list is treated as a list item (a preamble) of a
virtual enclosing list. This function customises Org-Mode."
(interactive)
(if (outline-invisible-p)
(alb-org-up-structure))
(let* ((curr-pos (point))
(head-pos (alb-org-head-pos curr-pos))
(text-pos (alb-org-text-pos head-pos))
(past-pos (alb-org-past-pos head-pos))
(entry-struct (alb-org-entry-structure text-pos past-pos))
(next-pos (cond ((= text-pos past-pos)
(alb-org-down-heading-pos head-pos))
((< curr-pos text-pos)
(or (alb-org-down-text-pos curr-pos entry-struct)
(alb-org-down-heading-pos head-pos)))
(t
(alb-org-down-text-pos curr-pos entry-struct)))))
(if next-pos
(goto-char next-pos))))
(defun alb-org-backward-item-pos (curr-pos item-pos struct prevs parents)
"Find first visible predecessor of CURR-POS in STRUCT
Let CURR-POS be the position of point, ITEM-POS be the start of
an enclosing list item, STRUCT be the enclosing list structure as
returned by `org-list-struct`, PREVS be the alist returned by
`org-list-prevs-alist`, and PARENTS be the alist returned by
`org-list-parents-alist`. Recur on the preceding siblings and
parent of ITEM-POS searching for a visible predecessor. If
found, return the position of the bullet of the predecessor list
item. Do not move point. This function customises Org-Mode."
(if item-pos
(let ((star-pos (alb-org-star-pos item-pos))
(next-item-pos (or (org-list-get-prev-item item-pos struct prevs)
(org-list-get-parent item-pos struct parents))))
(or (and (< star-pos curr-pos)
(alb-org-visible-pos star-pos))
(alb-org-backward-item-pos curr-pos next-item-pos
struct prevs parents)))))
(defun alb-org-backward-text-pos (curr-pos entry-struct)
"Find first visible predecessor of CURR-POS in ENTRY-STRUCT
Let CURR-POS be the position of point, and ENTRY-STRUCT be the
alist returned by `alb-org-entry-structure`. Recur on
ENTRY-STRUCT searching for a containing preamble or list. In
turn, visit the bullet of each preceding list item, and the first
non-whitespace character of the immediately preceeding preamble
until a visible character is located. If found, return the
position. Do not move point. This function customises
Org-Mode."
(if entry-struct
(let ((text-pos (caar entry-struct))
(struct (cdar entry-struct)))
(cond
((<= curr-pos text-pos)
nil)
(struct
(let ((star-pos (alb-org-star-pos (org-list-get-top-point struct)))
(past-pos (org-list-get-bottom-point struct)))
(cond
((<= curr-pos star-pos)
(alb-org-visible-preamble-pos text-pos star-pos))
((<= curr-pos past-pos)
(or (alb-org-backward-item-pos curr-pos (org-in-item-p) struct
(org-list-prevs-alist struct)
(org-list-parents-alist struct))
(alb-org-visible-preamble-pos text-pos star-pos)))
(t
(or (alb-org-backward-text-pos curr-pos (cdr entry-struct))
(alb-org-visible-pos (alb-org-star-pos
(org-list-get-last-item
(caar struct) struct
(org-list-prevs-alist struct))))
(alb-org-visible-preamble-pos text-pos star-pos))))))
(t
(alb-org-visible-pos text-pos))))))
(defun alb-org-backward-head-pos (curr-pos head-pos)
"Find first visible predecessor of CURR-POS
Let CURR-POS be the position of point, and HEAD-POS be the start
of the enclosing heading. Assume HEAD-POS < CURR-POS. In turn,
visit the bullet of each preceding list item, the first
non-whitespace character of the immediately preceeding preamble,
and the first character in the heading until a visible character
is located. If found, return the position. Do not move point.
This function customises Org-Mode."
(let ((text-pos (alb-org-text-pos head-pos))
(past-pos (alb-org-past-pos head-pos)))
(or (and (< text-pos curr-pos)
(alb-org-backward-text-pos curr-pos (alb-org-entry-structure
text-pos past-pos)))
(alb-org-visible-pos head-pos))))
(defun alb-org-backward-heading-pos (head-pos)
"Find first visible predecessor heading of HEAD-POS
Let HEAD-POS be the position of the enclosing heading. Search
backward for a visible predecessor heading. If found, return the
position. Do not move point. This function customises
Org-Mode."
(save-excursion (goto-char head-pos)
(org-backward-heading-same-level 1)
(and (outline-on-heading-p)
(point))))
(defun alb-org-backward-structure ()
"Move backward in the Org-Mode structure
Search backward for the first visible predecessor list item,
preamble, or heading at the same level in the structure. Place
point at the bullet of a list item, or first character of a
preamble or heading. This function is intended to be the
analogue of `backward-sexp` for the case of navigating Org-Mode
structures. It navigates headings and plain lists, with text
outside of a list being a special case. Text preceeding a list
is treated as a list item (a preamble) of a virtual enclosing
list. This function customises Org-Mode."
(interactive)
(let* ((curr-pos (point))
(head-pos (alb-org-head-pos curr-pos))
(next-pos (if (= curr-pos head-pos)
(alb-org-backward-heading-pos head-pos)
(or (alb-org-backward-head-pos curr-pos head-pos)
(alb-org-backward-heading-pos head-pos)))))
(if next-pos
(goto-char next-pos))))
(defun alb-org-forward-item-pos (curr-pos item-pos struct prevs parents)
"Find first visible successor of CURR-POS in STRUCT
Let CURR-POS be the position of point, ITEM-POS be the start of
an enclosing list item, STRUCT be the enclosing list structure as
returned by `org-list-struct`, PREVS be the alist returned by
`org-list-prevs-alist`, and PARENTS be the alist returned by
`org-list-parents-alist`. Recur on the succeeding siblings and
suceeding sibling of the parent of ITEM-POS searching for a
visible successor. If found, return the position of the bullet
of the ancestor list item. Do not move point. This function
customises Org-Mode."
(if item-pos
(let ((star-pos (alb-org-star-pos item-pos))
(next-item-pos (org-list-get-next-item item-pos struct prevs))
(parent-item-pos (org-list-get-parent item-pos struct parents)))
(or (and (< curr-pos star-pos)
(alb-org-visible-pos star-pos))
(alb-org-forward-item-pos curr-pos next-item-pos
struct prevs parents)
(alb-org-forward-item-pos curr-pos parent-item-pos
struct prevs parents)))))
(defun alb-org-forward-text-pos (curr-pos entry-struct)
"Find first visible successor of CURR-POS in ENTRY-STRUCT
Let CURR-POS be the position of point, and ENTRY-STRUCT be the
alist returned by `alb-org-entry-structure`. Recur on
ENTRY-STRUCT searching for a containing preamble or list. In
turn, visit the bullet of each succeeding list item, the first
non-whitespace character of the immediately suceeding preamble, and the
until a visible character is located. If found, return the
position. Do not move point. This function customises
Org-Mode."
(if entry-struct
(let ((text-pos (caar entry-struct))
(struct (cdar entry-struct)))
(cond
((< curr-pos text-pos)
(alb-org-visible-pos text-pos))
(struct
(let ((star-pos (alb-org-star-pos (org-list-get-top-point struct)))
(past-pos (org-list-get-bottom-point struct)))
(cond
((< curr-pos star-pos )
(or (alb-org-visible-pos star-pos)
(alb-org-forward-text-pos curr-pos (cdr entry-struct))))
((< curr-pos past-pos)
(or (alb-org-forward-item-pos curr-pos (org-in-item-p) struct
(org-list-prevs-alist struct)
(org-list-parents-alist struct))
(alb-org-forward-text-pos curr-pos (cdr entry-struct))))
(t
(alb-org-forward-text-pos curr-pos (cdr entry-struct))))))))))
(defun alb-org-forward-head-pos (curr-pos head-pos)
"Find first visible successor of CURR-POS
Let CURR-POS be the position of point, and HEAD-POS be the start
of the enclosing heading. Assume HEAD-POS < CURR-POS. In turn,
visit the bullet of each suceeding list item, the first
non-whitespace character of the immediately succeeding preamble,
and the first character outside of the entry. If found, return
the position. Do not move point. This function customises
Org-Mode."
(let ((text-pos (alb-org-text-pos head-pos))
(past-pos (alb-org-past-pos head-pos)))
(if (< curr-pos text-pos)
(alb-org-visible-pos text-pos)
(or (alb-org-forward-text-pos curr-pos (alb-org-entry-structure
text-pos past-pos))
(alb-org-visible-pos past-pos)))))
(defun alb-org-forward-heading-pos (head-pos)
"Find first visible successor heading of HEAD-POS
Let HEAD-POS be the position of the enclosing heading. Search
forward for a visible successor heading. If found, return the
position. Do not move point. This function customises
Org-Mode."
(save-excursion (goto-char head-pos)
(org-forward-heading-same-level 1)
(and (outline-on-heading-p)
(point))))
(defun alb-org-forward-structure ()
"Move forward in the Org-Mode structure
Search forward for the first visible successor list item,
preamble, or heading at the same level in the structure. Place
point at the bullet of a list item, or first character of a
preamble or heading. This function is intended to be the
analogue of `forward-sexp` for the case of navigating Org-Mode
structures. It navigates headings and plain lists, with text
outside of a list being a special case. Text preceeding a list
is treated as a list item (a preamble) of a virtual enclosing
list. This function customises Org-Mode."
(interactive)
(let* ((curr-pos (point))
(head-pos (alb-org-head-pos curr-pos))
(next-pos (if (= curr-pos head-pos)
(alb-org-forward-heading-pos head-pos)
(or (alb-org-forward-head-pos curr-pos head-pos)
(alb-org-forward-heading-pos head-pos)))))
(if next-pos
(goto-char next-pos))))
(defun alb-org-prev-item-pos (curr-pos item-pos struct prevs parents)
"Find first visible list item before CURR-POS in STRUCT
Let CURR-POS be the position of point, ITEM-POS be the start of a
list item, STRUCT be the enclosing list structure as returned by
`org-list-struct`, PREVS be the alist returned by
`org-list-prevs-alist`, and PARENTS be the alist returned by
`org-list-parents-alist`. Recur on the succeeding sibling and
first child of ITEM-POS, searching for the visible list item that
is the closest predecessor of CURR-POS. If found, return the
position of the bullet of the list item. Do not move point.
This function customises Org-Mode."
(if item-pos
(let ((star-pos (alb-org-star-pos item-pos))
(past-pos (org-list-get-item-end item-pos struct))
(sibling-pos (org-list-get-next-item item-pos struct prevs))
(child-pos (car (org-list-get-children item-pos struct parents))))
(or (and (< past-pos curr-pos)
(alb-org-prev-item-pos curr-pos sibling-pos struct
prevs parents))
(alb-org-prev-item-pos curr-pos child-pos struct
prevs parents)
(and (< star-pos curr-pos)
(alb-org-visible-pos star-pos))))))
(defun alb-org-prev-text-pos (curr-pos entry-struct)
"Find first visible preamble or list item before CURR-POS in ENTRY-STRUCT
Let CURR-POS be the position of point, and ENTRY-STRUCT be the
alist returned by `alb-org-entry-structure`. Recur on
ENTRY-STRUCT searching for a containing preamble or list. Search
backward from CURR-POS: visit the bullet of each list item, and
the first non-whitespace character of each preamble. If a
visible position is found, return it. Do not move point. This
function customises Org-Mode."
(if entry-struct
(let ((text-pos (caar entry-struct))
(struct (cdar entry-struct)))
(if struct
(let ((item-pos (org-list-get-top-point struct))
(past-pos (org-list-get-bottom-point struct)))
(or (and (< past-pos curr-pos)
(alb-org-prev-text-pos curr-pos (cdr entry-struct)))
(alb-org-prev-item-pos curr-pos item-pos struct
(org-list-prevs-alist struct)
(org-list-parents-alist struct))
(and (< text-pos curr-pos)
(alb-org-visible-pos text-pos))))
(and (< text-pos curr-pos)
(alb-org-visible-pos text-pos))))))
(defun alb-org-prev-head-pos (curr-pos head-pos)
"Find first visible preamble or list item before CURR-POS
Let CURR-POS be the position of point, and HEAD-POS be the start
of the enclosing heading. Assume HEAD-POS < CURR-POS. Search
backward from CURR-POS: visit the bullet of each list item, the
first non-whitespace character of each preamble, and the first
character of the heading. If a visible position is found, return
it. Do not move point. This function customises Org-Mode."
(let ((text-pos (alb-org-text-pos head-pos))
(past-pos (alb-org-past-pos head-pos)))
(or (and (< text-pos curr-pos )
(alb-org-prev-text-pos curr-pos
(alb-org-entry-structure text-pos
past-pos)))
(alb-org-visible-pos head-pos))))
(defun alb-org-prev-heading-pos (head-pos)
"Find first visible heading before HEAD-POS
Let HEAD-POS be the position of the enclosing heading. Search
backward for a visible heading. If found, return the position.
Do not move point. This function customises Org-Mode."
(save-excursion (goto-char head-pos)
(outline-previous-visible-heading 1)
(and (outline-on-heading-p)
(point))))
(defun alb-org-prev-structure ()
"Move to the previous element in the Org-Mode structure
Search backward for the first visible list item, preamble, or
heading regardless of level in the structure. Place point at the
bullet of a list item, or first character of a preamble or
heading. This function navigates headings and plain lists, with
text outside of a list being a special case. Text preceeding a
list is treated as a list item (a preamble) of a virtual
enclosing list. This function customises Org-Mode."
(interactive)
(let* ((curr-pos (point))
(head-pos (alb-org-head-pos curr-pos))
(prev-head-pos (alb-org-prev-heading-pos head-pos))
(next-pos (or (and (< head-pos curr-pos)
(alb-org-prev-head-pos curr-pos head-pos))
(and prev-head-pos
(alb-org-prev-head-pos curr-pos prev-head-pos)))))
(if next-pos
(goto-char next-pos))))
(defun alb-org-next-item-pos (curr-pos item-pos struct prevs parents)
"Find first visible list item after CURR-POS in STRUCT
Let CURR-POS be the position of point, ITEM-POS be the start of a
list item, STRUCT be the enclosing list structure as returned by
`org-list-struct`, PREVS be the alist returned by
`org-list-prevs-alist`, and PARENTS be the alist returned by
`org-list-parents-alist`. Recur on the first child and
succeeding sibling of ITEM-POS searching for the visible list
item that is the closest succesor of CURR-POS. If found, return
the position of the bullet of the list item. Do not move point.
This function customises Org-Mode."
(if item-pos
(let ((star-pos (alb-org-star-pos item-pos))
(child-pos (car (org-list-get-children item-pos struct parents)))
(sibling-pos (org-list-get-next-item item-pos struct prevs)))
(or (and (< curr-pos star-pos)
(alb-org-visible-pos star-pos))
(alb-org-next-item-pos curr-pos child-pos struct
prevs parents)
(alb-org-next-item-pos curr-pos sibling-pos struct
prevs parents)))))
(defun alb-org-next-text-pos (curr-pos entry-struct)
"Find first visible preamble or list iteam after CURR-POS in ENTRY-STRUCT
Let CURR-POS be the position of point, and ENTRY-STRUCT be the
alist returned by `alb-org-entry-structure`. Recur on
ENTRY-STRUCT searching for a containing preamble or list. Search
forward from CURR-POS: visit the bullet of each list item, and
the first non-whitespace character of each preamble. If a
visible position is found, return it. Do not move point. This
function customises Org-Mode."
(if entry-struct
(let ((text-pos (caar entry-struct))
(struct (cdar entry-struct)))
(if struct
(let ((item-pos (org-list-get-top-point struct))
(past-pos (org-list-get-bottom-point struct)))
(or (and (< curr-pos text-pos)
(alb-org-visible-pos text-pos))
(and (< curr-pos past-pos)
(alb-org-next-item-pos curr-pos item-pos struct
(org-list-prevs-alist struct)
(org-list-parents-alist struct)))
(alb-org-next-text-pos curr-pos (cdr entry-struct))))
(and (< curr-pos text-pos)
(alb-org-visible-pos text-pos))))))
(defun alb-org-next-head-pos (curr-pos head-pos)
"Find first visible preamble or list item after CURR-POS
Let CURR-POS be the position of point, and HEAD-POS be the start
of the enclosing heading. Assume HEAD-POS < CURR-POS. Search
forward from CURR-POS: visit the bullet of each list item, the
first non-whitespace character of each preamble, and the first
character outside of the entry. If a visible position is found,
return it. Do not move point. This function customises
Org-Mode."
(let ((text-pos (alb-org-text-pos head-pos))