/
mac_list_sublist_functions.el
653 lines (520 loc) · 16.7 KB
/
mac_list_sublist_functions.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
;; regexps and functions for creating lists and todo items
;; in my preferred format
;; -----
(defvar non-blank-line-regex "\\(^[^[:space:]\n]+\\)"
"regex for a non-blank line; used to demarcate list boundaries")
(defvar blank-line-regex "^ *\n"
"regex for a blank line; used to remove blank lines or add as needed")
(defvar list-regex "^ + - \\[[[:digit:]xX ]*\\] *\\(.*:: \\)\\{0,1\\}"
"regex for a list element; used to mark where sublists should go")
(defvar numbered-list-regex "^ + - \\[[[:digit:]]+\\] *\\(.*:: \\)\\{0,1\\}"
"regex for a numbered list element; used to mark where sublists should go")
(defvar todo-regex "^ + - \\[[xX ]*\\]\\(.*:: \\)\\{0,1\\}"
"regex for a todo list element; used to mark where sublists should go")
(defvar sublist-column-regex "^ +[+*]"
"regex used to determine the column to insert the next '+' char")
(defvar sublist-regex "\\(\n\\)\\(^ +[+*].*\\)"
"regex for a sublist element; used to mark where sublists should go")
;; -----
(defun fwd-bound ()
"Set forward-bound for list/todo list builder functions.
Setting these boundaries helps w/ positioning and renumbering."
(let ((fwd-bound (save-excursion
(re-search-forward non-blank-line-regex nil t))))
(when (not fwd-bound)
(setq fwd-bound (point-max)))
fwd-bound))
(defun rev-bound ()
"Set forward-bound for list/todo list builder functions.
Setting these boundaries helps w/ positioning and renumbering."
(let ((rev-bound (save-excursion
(re-search-backward non-blank-line-regex nil t))))
(when (not rev-bound)
(setq rev-bound (point-min)))
rev-bound))
;; -----
(defun list-line-adder ()
"Add newlines between lines of my lists where needed."
(interactive)
(let* ((rev-bound (rev-bound))
(fwd-bound (fwd-bound))
(next-elt))
;; start at list end; when no blank line after a list item, add one
(goto-char fwd-bound)
(beginning-of-line)
(when
(not
(re-search-backward
blank-line-regex
(save-excursion (previous-line 1) (line-beginning-position)) t))
(open-line 1))
;; search backwards for list elements
(setq next-elt
(save-excursion
(re-search-backward list-regex rev-bound t)))
;; insert needed newlines
(while (and
next-elt
(>= (point) rev-bound))
(progn
(re-search-backward list-regex rev-bound t)
(when
(not
(re-search-backward blank-line-regex
(save-excursion (previous-line 1) (line-beginning-position)) t))
(open-line 1))
(setq next-elt (save-excursion (re-search-backward list-regex rev-bound t)))
))
))
;; -----
;; (list-elt-regex "\\(^ - \\[[[:digit:]x]*\\]\\).*\n\\(^ +\\+.*\n\\)*\\(^ .*\n\\)*")
(defun list-line-remover ()
"Remove extraneous newlines from my lists."
(interactive)
(let* ((rev-bound (rev-bound))
(fwd-bound (fwd-bound))
(next-elt))
(goto-char fwd-bound)
(setq next-elt
(re-search-backward list-regex rev-bound t))
(when (not next-elt)
(setq next-elt rev-bound))
;; insert needed newlines
(while
(and
(save-excursion
(re-search-backward blank-line-regex rev-bound t))
(>= (point) rev-bound))
(progn
(re-search-backward blank-line-regex rev-bound t)
(unless
(looking-at
(concat "^ *\n" (substring list-regex 1)))
(delete-blank-lines))
(unless
(looking-at
(concat "^ *\n" (substring list-regex 1)))
(delete-blank-lines)))
))
)
;; -----
(defun list-line-renumber ()
"Automatically re-number lists and return point to its present location."
(interactive)
(let* ((current-point (point))
(list-n 1)
(rev-bound (rev-bound))
(fwd-bound (fwd-bound)))
(goto-char rev-bound)
(unless (eobp)
(forward-char 1))
(while
(<= (point) (1- fwd-bound))
(if ;; different regex here, b/c it has to contain a number
(re-search-forward "^ - \\[\\([[:digit:]]+\\)\\]" fwd-bound t)
(progn
(goto-char (match-beginning 1))
(re-search-forward "[[:digit:]]+" fwd-bound t)
(replace-match (format "%s" list-n))
(end-of-line)
(setq list-n (1+ list-n)))
(if (eobp)
(progn
(end-of-line)
(insert "\n"))
(forward-char 1)))
)
(goto-char current-point)
))
;; -----
(defun list-single-spacer ()
"Create a single-spaced list."
(interactive)
(let ((rev-bound (rev-bound))
(fwd-bound (fwd-bound))
(first-list-elt))
(goto-char rev-bound)
(unless
(not (re-search-forward list-regex fwd-bound t))
(while
(and
(or
(save-excursion (re-search-forward list-regex fwd-bound t))
(save-excursion (re-search-forward sublist-regex fwd-bound t)))
(re-search-forward blank-line-regex fwd-bound t))
(progn
(goto-char (match-beginning 0))
(delete-blank-lines)))
)
(goto-char fwd-bound)
(beginning-of-line)
(when
(not
(re-search-backward
blank-line-regex
(save-excursion (previous-line 1) (line-beginning-position)) t))
(open-line 1))
))
;; -----
(defun mac-org-numbered-list ()
"Function to insert numbered lists in my preferred format of \'- [1] first point.\'"
(interactive)
(end-of-line)
(let* ((current-point (point))
(current-number 1)
(rev-bound (rev-bound))
(fwd-bound (fwd-bound))
(next-list-elt
(save-excursion
(re-search-forward list-regex fwd-bound t)))
(prev-list-elt
(save-excursion
(re-search-backward list-regex rev-bound t))))
(when prev-list-elt
(progn
(goto-char prev-list-elt)
(re-search-forward "[[:digit:]]" (line-end-position) t)
(goto-char (match-beginning 0))
(setq current-number (1+ (number-at-point)))
(goto-char current-point)))
(when next-list-elt
(progn
(goto-char next-list-elt)
(beginning-of-line)
(open-line 1)))
(when (not next-list-elt)
(re-search-forward "\\(^ +\\+.*\n\\)+\\(^ .*\n\\)*" fwd-bound t))
(insert (concat "\n - [" (format "%s" current-number) "] "))
(list-line-renumber)
(re-search-backward "^ - " (line-beginning-position) t)
(re-search-forward "\\[" (line-end-position) t)
(setq current-number (format "%s" (number-at-point))
fwd-bound (fwd-bound))
(goto-char fwd-bound)
(beginning-of-line)
(list-line-remover)
(setq fwd-bound (fwd-bound))
(goto-char fwd-bound)
(beginning-of-line)
(list-line-adder)
(list-single-spacer)
(setq fwd-bound (fwd-bound))
(goto-char fwd-bound)
(re-search-backward (concat "^ - \\[" current-number) rev-bound t)
(end-of-line)
(message current-number)
))
;; -----
(defun mac-org-todo-list ()
"Insert to-do lists in my preferred format."
(interactive)
(end-of-line)
(let* ((current-point (point))
(rev-bound (rev-bound))
(fwd-bound (fwd-bound))
(next-list-elt
(save-excursion
(re-search-forward todo-regex fwd-bound t)))
(prev-list-elt
(save-excursion
(re-search-backward todo-regex rev-bound t))))
(when next-list-elt
(progn
(goto-char next-list-elt)
(beginning-of-line)
(open-line 1)))
(when (not next-list-elt)
;; regex to get through sublists
(re-search-forward "\\(^ +\\+.*\n\\)+\\(^ .*\n\\)*" fwd-bound t))
(insert (concat "\n - [ ] ~~\n"))
(setq fwd-bound (fwd-bound))
(goto-char fwd-bound)
(beginning-of-line)
(list-line-remover)
(setq fwd-bound (fwd-bound))
(goto-char fwd-bound)
(beginning-of-line)
(list-single-spacer)
(list-line-adder)
(list-single-spacer)
(end-of-buffer)
(re-search-backward "~~" rev-bound t)
(replace-match "")
))
;; -----
(defun mac-org-numbered-or-todo (arg)
"Insert a todo or numbered list item depending on context."
(interactive "P")
(let* ((rev-bound (rev-bound))
(fwd-bound (fwd-bound))
(next-list-elt
(save-excursion
(re-search-forward todo-regex fwd-bound t)))
(prev-list-elt
(save-excursion
(re-search-backward todo-regex rev-bound t))))
(cond
;; in a todo list already
((or
(save-excursion
(re-search-forward todo-regex fwd-bound t))
(save-excursion
(re-search-backward todo-regex rev-bound t)))
(mac-org-todo-list))
;; in a numbered list already
((or
(save-excursion
(re-search-forward numbered-list-regex fwd-bound t))
(save-excursion
(re-search-backward numbered-list-regex rev-bound t)))
(mac-org-numbered-list))
;; no prefix arg
((= (prefix-numeric-value arg) 1)
(mac-org-numbered-list))
;; all other instances
(t
(mac-org-todo-list)))
;; add spacing to the list w/ prefix arg = 0
(when (eq arg 0)
(progn
(list-line-adder)
(re-search-forward list-regex fwd-bound nil)
(end-of-line))
))
)
;; -----
(defun l1-sublist ()
"Helper function 'mac-sublist-indent'.
Simplest use scenario for when you call this while on a line w/ a list elt."
(let ((cp (point))
(curr-col))
(when
(save-excursion
(beginning-of-line)
(re-search-forward list-regex (line-end-position) t))
(progn
(beginning-of-line)
(re-search-forward list-regex (line-end-position) t)
(setq curr-col (current-column))))
(if curr-col
(progn
(end-of-line)
(insert (concat "\n" (make-string curr-col #x20) "+ ")))
(goto-char cp))))
;; -----
(defun l2-sublist ()
"Helper function for 'mac-sublist-indent'.
For when calling on a sublist line w/ no wrapped text following."
(let* ((cp (point))
(curr-col)
(fwd-bound (fwd-bound))
(list-bound))
(setq list-bound (save-excursion (re-search-forward list-regex fwd-bound t)))
(when (not list-bound)
(setq list-bound fwd-bound))
(when
(save-excursion
(beginning-of-line)
(re-search-forward sublist-column-regex (line-end-position) t))
(progn
(beginning-of-line)
(re-search-forward sublist-column-regex (line-end-position) t)
(setq curr-col (1- (current-column)))))
(if (and
curr-col
(save-excursion (re-search-forward sublist-regex list-bound t)))
(progn
(re-search-forward sublist-regex)
(goto-char (match-beginning 0))
(insert (concat "\n" (make-string curr-col #x20) "+ ")))
(goto-char cp))))
;; -----
(defun l3-sublist ()
"Helper function for 'mac-sublist-indent'.
For when calling at the end of a wrapped text sublist line w/ sublists."
(let ((cp (point))
(fwd-bound
(save-excursion
(re-search-forward list-regex nil t)))
(rev-bound
(save-excursion
(re-search-backward list-regex nil t))))
(when (not fwd-bound)
(setq fwd-bound
(save-excursion
(re-search-forward non-blank-line-regex nil t))))
(when (not fwd-bound)
(setq fwd-bound (point-max)))
(when (not rev-bound)
(setq rev-bound
(save-excursion
(re-search-backward non-blank-line-regex nil t))))
(when (not rev-bound)
(setq rev-bound (point-max)))
(let ((curr-col)
(next-sublist)
(next-list-elt)
(sublist-point))
(when
(save-excursion
(re-search-forward "\n +[+*]" fwd-bound t))
(progn
(setq next-sublist (save-excursion
(re-search-forward "\n +[+*].*$" fwd-bound t)
(point)))))
(cond
(next-sublist
(progn
(setq curr-col
(save-excursion
(re-search-forward sublist-column-regex next-sublist)
(1- (current-column))))
(re-search-forward "\n +[+*].*$" fwd-bound t)
(goto-char (match-beginning 0))
(insert (concat "\n" (make-string curr-col #x20) "+ "))))
((looking-at (concat "\n" (substring list-regex 1)))
(progn
(setq curr-col
(save-excursion
(re-search-backward sublist-column-regex rev-bound t)
(goto-char (match-end 0))
(1- (current-column))))
(unless (not curr-col)
(insert (concat "\n" (make-string curr-col #x20) "+ ")))))
(t
(goto-char cp)))
)))
;; -----
(defun l4-sublist ()
"Helper function for 'mac-sublist-indent'.
Inserts sub-lists at the end of sublists."
(let* ((rev-bound (rev-bound))
(fwd-bound (fwd-bound))
(next-list-elt)
(prev-list-elt)
(line-end (line-end-position))
(curr-col))
(setq next-list-elt
(save-excursion
(re-search-forward list-regex fwd-bound t)))
(when (not next-list-elt)
(setq next-list-elt fwd-bound))
(setq prev-list-elt
(save-excursion
(re-search-backward list-regex rev-bound t)))
(unless (not prev-list-elt)
(goto-char prev-list-elt))
(beginning-of-line)
(re-search-forward list-regex (line-end-position) t)
(setq curr-col (current-column))
(while
(save-excursion
(re-search-forward "[+*]" line-end t))
(re-search-forward "[+*]" line-end t))
;; look for a blank line to insert after
(if (save-excursion
(re-search-forward "\\(\n[[:space:]]*\\)\n" next-list-elt t))
(progn
(re-search-forward "\\(\n[[:space:]]*\\)\n" next-list-elt t)
(goto-char (match-beginning 0)))
(end-of-line))
(insert (concat "\n" (make-string curr-col #x20) "+ "))
(when (not (looking-at "\\(\n[[:space:]]*\\)\n"))
(open-line 1))
))
;; -----
(defun mac-sublist-indent ()
"Insert a sublist at point using my preferred format.
Uses helper functions together w/ a cond function to decide
how to insert the sublist."
(interactive)
(end-of-line)
(cond
((looking-back (concat list-regex ".*") (line-beginning-position))
(progn
(l1-sublist)
(message "l1")))
((and
(looking-at (concat sublist-regex ".*"))
(looking-back (concat sublist-regex ".*") (line-beginning-position)))
(progn
(l2-sublist)
(message "l2")))
((looking-at "\\(\n +.*\\)+ +\\+")
(progn
(l3-sublist)
(message "l3")))
((save-excursion
(re-search-backward "\\(^ +\\+\\)*" nil t))
(progn
(l4-sublist)
(message "l4")))
(t
(message "not in a list, dummy!"))
)
(insert "~~")
(list-line-remover)
(list-single-spacer)
;; (list-line-adder)
(re-search-backward "~~" nil t)
(replace-match "")
)
;; -----
(defun mac-alt-sublist-indent ()
"Insert an alternative char '*' to denote separate sublist points."
(interactive)
(mac-sublist-indent)
(re-search-backward "\\+" (line-beginning-position) t)
(replace-match "* ")
(fixup-whitespace)
(insert " "))
;; -----
(defun mac-sub-sub-list ()
"Insert a 'level 2' item beneath a sub-list point."
(interactive)
(let ((fwd-bound (fwd-bound))
(rev-bound (rev-bound))
(column))
(end-of-line)
;; contingency for no sublist item
(unless
(not
(save-excursion
(re-search-backward " +\\([+*]\\) " rev-bound t)))
(progn
(save-excursion
(re-search-backward "^ +\\([+*]\\) " rev-bound t)
(goto-char (match-end 0))
(setq column (current-column)))
(end-of-line)
(insert (concat "\n" (make-string column #x20) "- "))))))
;; -----
(defun mac-sub-sub-sub-list ()
"Insert a 'level 2' item beneath a sub-list point."
(interactive)
(let ((fwd-bound (fwd-bound))
(rev-bound (rev-bound))
(column))
(end-of-line)
(setq rev-bound (save-excursion (re-search-backward list-regex rev-bound t) (end-of-line) (point)))
;; contingency for no sublist item
(unless
(not
(save-excursion
(re-search-backward "^ +- " rev-bound t)))
(progn
(save-excursion
(re-search-backward "^ +- " rev-bound t)
(goto-char (match-end 0))
(setq column (current-column)))
(end-of-line)
(insert (concat "\n" (make-string column #x20) "> "))))))
;; -----
;; open up org-mode bindings for my use
(define-key org-mode-map (kbd "C-c C-y") nil)
(define-key org-mode-map (kbd "C-c C-k") nil)
(global-set-key (kbd "C-c M-RET") 'mac-org-numbered-or-todo)
(global-set-key (kbd "C-c C-j") 'mac-sublist-indent)
(global-set-key (kbd "C-c C-k") 'mac-alt-sublist-indent)
(global-set-key (kbd "C-c C-y") 'mac-sub-sub-list)
(global-set-key (kbd "C-c C-7") 'mac-sub-sub-sub-list)