-
Notifications
You must be signed in to change notification settings - Fork 0
/
.emacs
1497 lines (1343 loc) · 53.1 KB
/
.emacs
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
;; Added by Package.el. This must come before configurations of
;; installed packages. Don't delete this line. If you don't want it,
;; just comment it out by adding a semicolon to the start of the line.
;; You may delete these explanatory comments.
(setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3")
(when (>= emacs-major-version 24)
(require 'package)
(add-to-list
'package-archives
'("melpa" . "http://melpa.org/packages/")
t))
(package-initialize)
(require 'use-package)
(setq package-enable-at-startup nil)
;; (setq org-cite-export-processors
;; '((latex biblatex)
;; (t csl)))
(defvar bootstrap-version)
(let ((bootstrap-file
(expand-file-name "straight/repos/straight.el/bootstrap.el" user-emacs-directory))
(bootstrap-version 5))
(unless (file-exists-p bootstrap-file)
(with-current-buffer
(url-retrieve-synchronously
"https://raw.githubusercontent.com/raxod502/straight.el/develop/install.el"
'silent 'inhibit-cookies)
(goto-char (point-max))
(eval-print-last-sexp)))
(load bootstrap-file nil 'nomessage))
(setq straight-use-package-by-default nil)
(use-package emacs-conflict
:straight (emacs-conflict :type git :host github :repo "ibizaman/emacs-conflict" :branch "master"))
;; (use-package eva
;; :straight (eva :type git :host github :repo "meedstrom/eva"
;; :files (:defaults "assets" "renv" "*.R" "*.gnuplot")))
;; (straight-use-package
;; :custom
;; '(eva :type git :host github :repo "meedstrom/eva"
;; :files (:defaults "assets" "renv" "*.R" "*.gnuplot")))
;; (setq eva-items
;; (list (eva-item-create :fn #'eva-query-sleep
;; :dataset "~/self-data/sleep.tsv"
;; :min-hours-wait 5
;; :lookup-posted-time t)
;; (eva-item-create :fn #'eva-query-weight
;; :dataset "~/self-data/weight.tsv"
;; :max-entries-per-day 1)
;; (eva-item-create :fn #'eva-query-mood
;; :dataset "~/self-data/mood.tsv")))
;; (use-package eva
;; ;; Things that should be set before load
;; :custom
;; (eva-va-name "Wynona")
;; (eva-user-name "Thea")
;; (eva-user-short-title "darling") ;; don't like titles? put in your name again
;; (eva-idle-log-path "~/self-data/idle.tsv")
;; (eva-buffer-focus-log-path "~/self-data/buffer-focus.tsv")
;; (eva-buffer-info-path "~/self-data/Self_data/buffer-info.tsv")
;; (eva-main-ledger-path "~/finances.ledger")
;; (eva-main-datetree-path "~/org/diary.org")
;; (ess-ask-for-ess-directory nil) ;; Prevent annoying ESS startup prompt.
;; :config
;; (require 'eva-builtin)
;; ;; These are looked up by `eva-present-diary', but org-journal is not needed.
;; (setq org-journal-dir "~/org/journal/")
;; (setq org-journal-file-format "%F.org")
;; (add-hook 'eva-after-load-vars-hook #'eva-check-dangling-clock)
;; (add-hook 'eva-after-load-vars-hook #'eva-check-org-vars)
;; ;; HINT: Though not likely you'll want to, you can use the same object
;; ;; multiple times in the queue, you'll just have to assign the output of
;; ;; an (eva-item-create) to an external variable and refer to it.
;; (setq eva-items
;; (list
;; (eva-item-create :fn #'eva-greet
;; :min-hours-wait 1)
;; (eva-item-create :fn #'eva-query-mood
;; :dataset "~/self-data/mood.tsv"
;; :min-hours-wait 1)
;; (eva-item-create :fn #'eva-present-diary
;; :max-successes-per-day 1)
;; (eva-item-create :fn #'eva-query-sleep
;; :dataset "~/self-data/sleep.tsv"
;; :min-hours-wait 5
;; :lookup-posted-time t)
;; (eva-item-create :fn #'eva-present-ledger-report)
;; ;; May be slow
;; ;; (eva-item-create :fn #'eva-present-org-agenda-log-archive)
;; (eva-item-create :fn #'eva-present-org-agenda-log)
;; (eva-item-create :fn #'eva-query-ingredients
;; :dataset "~/self-data/ingredients.tsv"
;; :min-hours-wait 5)
;; (eva-item-create :fn #'eva-query-cold-shower
;; :dataset "~/self-data/cold.tsv"
;; :max-entries-per-day 1)
;; (eva-item-create :fn #'eva-query-activity
;; :dataset "~/self-data/activities.tsv"
;; :min-hours-wait 1)
;; ;; you can inline define the functions too
;; (eva-item-create
;; :fn (eva-defun my-bye ()
;; (message (eva-emit "All done for now."))
;; (bury-buffer (eva-buffer-chat)))
;; :min-hours-wait 0)))
;; ;; Hotkeys in the chat buffer
;; (transient-replace-suffix 'eva-dispatch '(0)
;; '["General actions"
;; ("q" "Quit the chat" bury-buffer)
;; ("l" "View Ledger report" eva-present-ledger-report)
;; ("f" "View Ledger file" eva-present-ledger-file)
;; ("a" "View Org agenda" org-agenda-list)])
;; (define-key eva-chat-mode-map (kbd "l") #'eva-present-ledger-report)
;; (define-key eva-chat-mode-map (kbd "f") #'eva-present-ledger-file)
;; (define-key eva-chat-mode-map (kbd "a") #'org-agenda-list)
;; ;; Activities (for `eva-query-activity'). These are cl objects for forward
;; ;; compatibility; right now only :name is used, to fill out completion
;; ;; candidates.
;; (setq eva-activity-list
;; (list (eva-activity-create :name "sleep")
;; (eva-activity-create :name "studying")
;; (eva-activity-create :name "coding")
;; (eva-activity-create :name "unknown")))
;; (eva-mode))
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(ansi-color-faces-vector
[default default default italic underline success warning error])
'(ansi-color-names-vector
["#242424" "#e5786d" "#95e454" "#cae682" "#8ac6f2" "#333366" "#ccaa8f" "#f6f3e8"])
'(custom-enabled-themes '(tsdh-dark))
'(org-agenda-files nil)
'(package-selected-packages
'(muse mediawiki wiki-nav vagrant-tramp phps-mode php-refactor-mode php-mode elpy buffer-move auctex datetime highlight git-gutter csv-mode rjsx-mode prettier-js csv magit web-mode clang-format smarty-mode eyebrowse auto-complete grip-mode flymd markdown-preview-mode markdown-mode imenu-list flycheck org)))
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(org-table ((t (:inherit fixed-pitch :foreground "white")))))
;; load emacs 24's package system. Add unstable MELPA repository.
;; (setq gnutls-algorithm-priority "NORMAL:-VERS-TLS1.3")
;; (when (>= emacs-major-version 24)
;; (require 'package)
;; (add-to-list
;; 'package-archives
;; '("melpa" . "http://melpa.org/packages/")
;; t))
;(package-initialize)
;;initialize mapcar* function
(defun mapcar* (function &rest args)
"Apply FUNCTION to successive cars of all ARGS.
Return the list of results."
;; If no list is exhausted,
(if (not (memq nil args))
;; apply function to CARs.
(cons (apply function (mapcar 'car args))
(apply 'mapcar* function
;; Recurse for rest of elements.
(mapcar 'cdr args)))))
;; Show parens
(show-paren-mode 1)
;; Ignore insecure downloads. What could go wrong
(setq package-check-signature nil)
;; Automatically install packages, for distributing this config
(setq package-list
`(eyebrowse auto-complete grip-mode flymd markdown-preview-mode markdown-mode imenu-list flycheck org))
(unless package-archive-contents
(package-refresh-contents))
; install the missing packages
(dolist (package package-list)
(unless (package-installed-p package)
(package-install package)))
;; Eyebrowse Mode
;Everything starts with C-c C-w
(eyebrowse-mode t)
;; Keybindings help
(defun binding-help()
(interactive)
(describe-mode `eyebrowse-mode)
)
;; Color hex codes
(defun hex-color ()
(interactive)
(font-lock-add-keywords
nil
'(("#[[:xdigit:]]\\{3\\}"
(0 (put-text-property
(match-beginning 0)
(match-end 0)
'face (list :background
(let* (
(ms (match-string-no-properties 0))
(r (substring ms 1 2))
(g (substring ms 2 3))
(b (substring ms 3 4)))
(concat "#" r r g g b b))))))
("#[[:xdigit:]]\\{6\\}"
(0 (put-text-property
(match-beginning 0)
(match-end 0)
'face (list :background (match-string-no-properties 0)))))))
(font-lock-flush))
;; A function that adds a hook to the major programming languages only
(defun add-minor-mode-to-languages (m)
(add-hook `c-mode-common-hook m)
(add-hook `php-mode-hook m)
(add-hook 'emacs-lisp-mode-hook m)
(add-hook 'java-mode-hook m)
(add-hook 'lisp-mode-hook m)
(add-hook 'perl-mode-hook m)
(add-hook 'sh-mode-hook m)
(add-hook 'python-mode-hook m)
(add-hook 'javascript-mode-hook m)
(add-hook 'jsx-mode-hook m)
(add-hook 'js-jsx-mode-hook m)
(add-hook 'mhtml-mode-hook m)
(add-hook 'web-mode-hook m))
;; Occur
(global-set-key (kbd "C-M-s") `occur)
;; Imenu stuff
(global-set-key (kbd "C-c i") `imenu-list-smart-toggle)
;; Finds all <p> instances and formats their contents to fit in the desired length.
(defun format-paragraphs (col)
(interactive (list (read-number "Input fill column (Default is 100): " 100)))
(progn
(set-fill-column col)
(with-current-buffer (current-buffer)
(let (last-p) (setq last-p (point))
(goto-char 0)
(while (re-search-forward "<p[^>]*?>" nil t)
(goto-char (match-beginning 0))
(fill-paragraph)
(goto-char (match-end 0)))
(goto-char last-p)))))
(global-set-key (kbd "C-c p") 'format-paragraphs)
;; Keybinding to evaluate current buffer
;;(global-set-key (kbd "C-c e") `eval-buffer)
(defun re-init ()
(interactive)
(load-file "~/.emacs"))
;; SSH functions
(defun ssh (l)
(interactive "MConnect to host: ")
(dired (format "/ssh:%s:" l)))
;; Autocomplete
(add-minor-mode-to-languages `auto-complete-mode)
;;IDO!
;;(add-minor-mode-to-languages `ido-mode)
(setq ido-enable-flex-matching t)
(setq ido-everywhere t)
(ido-mode 1)
(global-set-key
"\M-x"
(lambda ()
(interactive)
(call-interactively
(intern
(ido-completing-read
"M-x "
(all-completions "" obarray 'commandp))))))
;;Add linum to code
(add-minor-mode-to-languages `(lambda () (linum-mode 1)))
;; Hideshow and yafolding stuff
(load-library "hideshow")
(defun toggle-selective-display (column)
(interactive "P")
(set-selective-display
(or column
(unless selective-display
(1+ (current-column))))))
(defun toggle-hiding (column)
(interactive "P")
(if hs-minor-mode
(if (condition-case nil
(hs-toggle-hiding)
(error t))
(hs-show-all))
(toggle-selective-display column)))
(global-set-key (kbd "C-c h") `hs-toggle-hiding)
(global-set-key (kbd "C-c -") 'hs-hide-all)
(global-set-key (kbd "C-c +") 'hs-show-all)
(global-set-key (kbd "C-c ,") 'hs-hide-level)
;; hs mhtml-forward functions
(defun mhtml-forward (arg)
(interactive "P")
(pcase (get-text-property (point) 'mhtml-submode)
('nil (sgml-skip-tag-forward 1))
(submode (forward-sexp))))
(add-to-list 'hs-special-modes-alist
'(mhtml-mode
"{\\|<[^/>]+?"
"}\\|</[^/>]*[^/]>"
"<!--"
mhtml-forward
nil))
(add-to-list 'hs-special-modes-alist
'(web-mode
"{\\|<[^/>]+?"
"}\\|</[^/>]*[^/]>"
"<!--"
mhtml-forward
nil))
(add-minor-mode-to-languages 'hs-minor-mode)
;; Yafolding for ruby
(require 'yafolding)
(add-hook 'ruby-mode-hook 'yafolding-mode)
(define-key yafolding-mode-map (kbd "<C-S-return>") nil)
(define-key yafolding-mode-map (kbd "<C-M-return>") nil)
(define-key yafolding-mode-map (kbd "<C-return>") nil)
(global-set-key (kbd "C-c h") 'yafolding-toggle-element)
(global-set-key (kbd "C-c -") 'yafolding-hide-all)
(global-set-key (kbd "C-c +") 'yafolding-show-all)
;; Flyspell settings
(add-hook 'flyspell-mode-hook
(lambda ()
(progn
(local-set-key (kbd "M-s") (lambda () (interactive)
(flyspell-goto-next-error)
(ispell-word))))))
;; Code folding and spelling checks for latex
(add-hook 'TeX-mode-hook
(lambda ()
(progn
; Code Folding for latex, in the same style as hs-minor-mode
(tex-fold-mode 1)
(local-set-key (kbd "C-c h") #'TeX-fold-dwim)
(local-set-key (kbd "C-c -") #'TeX-fold-region)
(local-set-key (kbd "C-c +") #'TeX-fold-clearout-region)
; Turn on flyspell and set a keybinding for searching with it
(flyspell-mode 1))))
(defun display-code-line-counts (ov)
(when (eq 'code (overlay-get ov 'hs))
(overlay-put ov 'help-echo
(buffer-substring (overlay-start ov)
(overlay-end ov)))))
(setq hs-set-up-overlay 'display-code-line-counts)
(defadvice goto-line (after expand-after-goto-line
activate compile)
"hideshow-expand affected block when using goto-line in a collapsed buffer"
(save-excursion
(hs-show-block)))
;; Desktops
(require 'desktop)
(desktop-save-mode 1)
;(desktop-save-in-desktop-dir)
(setq desktop-path (list "."))
(defun my-desktop-save ()
(interactive)
;; Don't call desktop-save-in-desktop-dir, as it prints a message.
(if (eq (desktop-owner) (emacs-pid))
(desktop-save desktop-dirname)))
(add-hook 'auto-save-hook 'my-desktop-save)
(setq desktop-restore-forces-onscreen nil)
(add-hook 'desktop-after-read-hook
(lambda ()
(frameset-restore
desktop-saved-frameset
:reuse-frames (eq desktop-restore-reuses-frames t)
:cleanup-frames (not (eq desktop-restore-reuses-frames 'keep))
:force-display desktop-restore-in-current-display
:force-onscreen desktop-restore-forces-onscreen)))
;;; desktop-override-stale-locks.el begins here
(defun emacs-process-p (pid)
"If pid is the process ID of an emacs process, return t, else nil.
Also returns nil if pid is nil."
(when pid
(let* ((cmdline-file (concat "/proc/" (int-to-string pid) "/cmdline")))
(when (file-exists-p cmdline-file)
(with-temp-buffer
(insert-file-contents-literally cmdline-file)
(goto-char (point-min))
(search-forward "emacs" nil t)
pid)))))
(defadvice desktop-owner (after pry-from-cold-dead-hands activate)
"Don't allow dead emacsen to own the desktop file."
(when (not (emacs-process-p ad-return-value))
(setq ad-return-value nil)))
;;; desktop-override-stale-locks.el ends here
;; Nice switch indentation
(c-set-offset 'case-label '+)
;; Nicer compilation output
(add-hook 'compilation-mode-hook
(lambda () (setq truncate-lines t)))
;; Use mhtl-mode for html files
;;(add-to-list 'auto-mode-alist '("\\.html\\'" . mhtml-mode))
(add-to-list 'auto-mode-alist '("\\.php\\'" . php-mode))
(add-to-list 'auto-mode-alist '("\\.tsx\\'" . js-jsx-mode))
(add-to-list 'auto-mode-alist '("\\.ts\\'" . js-jsx-mode))
(defvar my-menu-bar-menu (make-sparse-keymap "Mine"))
(define-key global-map [menu-bar my-menu] (cons "Mine" my-menu-bar-menu))
(require `notifications)
;;
;;
;;
;;
;; Syllabus organizing tools
;;
;;
;;
;
;; Copies the line the user is currently on
(defun buffer-contains-substring (string)
(save-excursion
(save-match-data
(goto-char (point-min))
(search-forward string nil t))))
;; Gets the position of the nth occurence of a substring. 0 if 0 provided, string len if substring dne
(defun get-nth-match (string substring n start)
(let ((pos 0)
(match (string-match substring string (+ 1 start))))
(if (null match)
(length string)
(if (= n 1)
match
(if (= n 0)
0
(get-nth-match string substring (- n 1) match))))))
;; Highlight the current line with the given color. If progress is passed, show partial completion of ";" seperated tasks
(defun color-line (color &optional progress)
(if progress
(hlt-highlight-region
(line-beginning-position) (line-end-position) `hlt-regexp-level-6)
(setq progress 0))
(if color
(hlt-highlight-region
(line-beginning-position)
(if (> progress 0)
(+ (line-beginning-position)
(get-nth-match
(buffer-substring-no-properties (line-beginning-position) (line-end-position)) ";" progress 0))
(line-end-position))
color))
t)
(setq upcoming-work-faces
(mapcar*
(lambda (color days-away)
(custom-declare-face
(intern (concat "syllabus-face-" (number-to-string days-away)))
`((((background dark)) (:background ,color))
(t (:background "#FA6CC847FFFF")))
(concat "Due in " (number-to-string days-away) " days")
:group 'highlight :group 'faces))
`("dark red" "#b36200" "dark orange" "yellow" "gold")
`(0 1 2 3 4)))
(setq complete-work `hlt-regexp-level-1)
(setq important-work `hlt-regexp-level-6)
(setq incomplete-work `hlt-regexp-level-4)
; If the date is in less that 5 days, return the automatic countdown highlight color for that date
(defun upcoming-work-face (cur-date n)
(if (< n 5)
(if (string< cur-date (date-in-n-days n))
`(,(nth (- n 1) upcoming-work-faces))
(upcoming-work-face cur-date (+ n 1)))
`(nil)
))
;; (defun marking-face (commands else-command)
;; (if commands
;; (if (string-match (concat "\\[" (caar commands) "\\]")
;; (buffer-substring-no-properties (line-beginning-position) (line-end-position)))
;; (cadar commands)
;; (marking-face (cdr commands) else-command))
;; else-command))
; Given a list of form `((symbol, command), ...) and a command to run if there is no symbol present, return the command that is applied to that line
(defun marking-face (commands else-command)
(if commands
(if (string-match (concat "\\[" (caar commands) "\\]")
(buffer-substring-no-properties
(line-beginning-position) (line-end-position)))
(cdar commands)
(marking-face (cdr commands) else-command))
else-command))
;; Returns the datestring for the date in n days
(defun date-in-n-days (n)
(format-time-string
"<%Y-%m-%d %a>"
(apply `encode-time
(progn
(setq l (decode-time))
(setcar (nthcdr 3 l)
(+ (nth 3 l) n))
l))))
(defvar todays-notifications (list)
"A list containing the notifications generated from the schedule")
;; Color the syllabus based on the symbols
(defun colorize-schedule ()
(and
(buffer-contains-substring "syllabus t")
;; remove pre-existing notifications
(or (mapcar `(lambda (notif-id)
(notifications-close-notification notif-id))
todays-notifications)
(setq todays-notifications (list))
t)
(save-excursion
(save-match-data
;; Jump to beginning, unhighlight all
(goto-char 0)
(hlt-unhighlight-region (point-min) (point-max))
;; Go to the first entry in the schedule, start looping over each
(re-search-forward "<.*>" nil t)
;; (goto-char (line-start-position))
;; (do-times (i 2)
;; (forward-line -1)
;; (color-line))
;; (let* ((anchor (point))
;; (len (- (line-end-position) (line-start-position)))
;; (start-point (- anchor (* 2 len))))
;; (color-line))
;; (goto-char (line-end-position))
(while
(let* (;; Positions of date elements
(date-start (- (point) 16))
(time-start (re-search-forward ":(" (line-end-position) t))
(time-end
(if time-start
(+ time-start 5)
nil))
; Selected timestamp in format <YYYY-MM-DD>
(selected-date
(buffer-substring-no-properties date-start (+ date-start 16)))
; Selected date in format HH:MM AKA military time
(selected-time
(if time-start
(buffer-substring-no-properties time-start time-end)
"08:00"))
; Split up columns into list, strip links
(boxes
(split-string
(replace-regexp-in-string "\\[\\[\\(.*?\\)\\]\\[\\(.*?\\)\\]\\]"
"\\2"
(buffer-substring-no-properties (line-beginning-position) (line-end-position)))
"|" t))
(current-date
(format-time-string "<%Y-%m-%d %a>"))
(hours-away 0)
;; Maps symbol to highlight face
(marking-faces
(append
; X => Done; ! => Very Important; - => NA
`(("X" hlt-regexp-level-1)
("!" hlt-regexp-level-6)
("-" hlt-regexp-level-4))
; Highest Priority (A) to lowest (E)
(mapcar
`(lambda (n) `(,(nth n '("A" "B" "C" "D" "E"))
,(nth n upcoming-work-faces)))
(number-sequence 0 4))
; Progress completion
(mapcar
`(lambda (n) `(,(number-to-string n)
hlt-regexp-level-1
,n))
(number-sequence 0 6)))))
; For incomplete work due today, add a notif
(if (and (string= current-date selected-date) (not (string= (nth 0 boxes) " [X] " )))
(setq todays-notifications
(append
(list (notifications-notify
:title (nth 2 boxes)
:app-name "Schedule"
:body (concat "<b>" (nth 0 boxes) "</b>" "<b>" selected-time "</b> - "
(if (string-match "[0-9]" (nth 0 boxes))
(nth (string-to-number (substring (nth 0 boxes) 2 3)) (split-string (nth 3 boxes) ";"))
(nth 3 boxes)))
:timeout 0
))
todays-notifications)))
;; Color the current line with the color as matched by symbol or by days away
(apply `color-line
(marking-face marking-faces
(upcoming-work-face selected-date 0)))
(goto-char (line-end-position))
(re-search-forward "<.*>" nil t)))
;; Go back and search for a timestamp
(goto-char 0)
(make-weekly-schedule)
;(re-search-forward "<.*>:(.*?)")
))))
(defvar sched-date (format-time-string "%Y-%m-%d")
"Which date the scheduler is currently looking at")
(defvar sched-period (format-time-string "all")
"Which period the scheduler is currently looking at ('day', 'week', 'all')")
(defvar sched-subject nil
"The current highlighted subject")
(defvar sched-dates '()
"List of dates present in the schedule buffer")
(defvar sched-subjects '()
"List of subjects present in the schedule buffer")
(defun any (pred list)
"Checks if any item in the list matches the predicate"
(while (and list (not (funcall pred (car list))))
(pop list))
(car list))
(defun sched-show-all ()
"Show all items in schedule"
(setq sched-period "all")
(setq sched-subject nil)
(setq sched-date (format-time-string "%Y-%m-%d"))
(loccur nil)
(colorize-schedule))
(defun sched-show-urgent ()
(setq sched-period "urgent")
(loccur nil)
(loccur "\\[A\\]")
(colorize-schedule))
(defun sched-show-day (day)
"Show items for given day"
(setq sched-period "day")
(loccur nil)
(loccur day)
(colorize-schedule))
(defun sched-show-week (week-regexp)
"Show items for given week"
(setq sched-period "week")
(loccur nil)
(loccur week-regexp)
(colorize-schedule))
(defun sched-show-subject (subject)
"Show items for given subject"
(setq sched-period "subject")
(loccur nil)
(loccur (concat "\|\s*" subject "\s*\|"))
(colorize-schedule))
(defun get-matches-in-buffer (regex)
"Returns a list of all matches in a buffer with duplicates removed"
(let ((dates '())
(buffer-text (buffer-substring-no-properties (point-min) (point-max))))
(with-temp-buffer
(insert buffer-text)
(goto-char 0)
(while (re-search-forward regex (point-max) t)
(push (match-string 1) dates)))
(reverse (delete-dups dates))))
(defun sched-get-dates ()
" Returns the list of all dates present in the schedule buffer with duplicates removed"
(get-matches-in-buffer "<\\([0-9|\-]*\\).*>"))
(defun sched-get-subjects ()
" Returns the list of all subject areas present in the schedule buffer with duplicates removed"
(cl-sort
(cdr (get-matches-in-buffer "^|.*?|.*?|\s*\\(.*?\\)\s*|"))
'string-lessp
:key 'downcase))
(defun sched-date-increment (datestring n)
"Increments the datestring by n days"
(org-read-date nil nil (concat (if (> n 0) "++" "--") (number-to-string (abs n))) nil (org-read-date nil t datestring)))
(defun sched-date-increment-day (inc)
"Shifts to the next or previous day, provide -1 or 1 for past or future."
(let ((new-date-index (+ inc (cl-position sched-date sched-dates :test 'string=))))
;; Set sched-date to next date in list if index in bounds
(if (and (>= new-date-index 0) (< new-date-index (length sched-dates)))
(progn (setq sched-date (nth new-date-index sched-dates))
(sched-show-day sched-date)))))
(defun get-week-regex (date)
"Returns a regex matching pattern for the week the date resides in"
(mapconcat
(lambda (x)
(org-read-date nil nil
(concat "++"
(number-to-string x)) nil
;;(org-read-date nil t "-sun")
(org-read-date nil t "--sat" nil (org-time-string-to-time date))))
(number-sequence 1 7) "\\|"))
(defun sched-date-increment-week (inc)
"Shifts to the next or previous week, provide -1 or 1 for past or future."
(let* ((lower-bound (sched-date-increment (car sched-dates) -7))
(upper-bound (sched-date-increment (car (last sched-dates)) 7))
(next-week (sched-date-increment sched-date (* 7 inc)))
(next-week-dates (get-week-regex next-week))
(new-sched-date nil))
(while
(and (string< next-week upper-bound) (string> next-week lower-bound)
(not (any `(lambda (date) (string-match next-week-dates date)) sched-dates)))
(setq next-week (sched-date-increment next-week (* 7 inc)))
(setq next-week-dates (get-week-regex next-week)))
(setq new-sched-date (any `(lambda (date) (string-match next-week-dates date)) sched-dates))
(if new-sched-date
(progn (setq sched-date new-sched-date)
(sched-show-week next-week-dates)))))
(defun sched-increment-subject (inc)
" Navigates subject views "
(message "trying to increment")
(let ((new-subject-index (if sched-subject
(+ inc (cl-position sched-subject sched-subjects :test 'string=))
0)))
(message "%s" new-subject-index)
(message sched-subject)
(if (and (>= new-subject-index 0) (< new-subject-index (length sched-subjects)))
(progn (setq sched-subject (nth new-subject-index sched-subjects))
(sched-show-subject sched-subject)))))
(add-to-list 'display-buffer-alist '("*Async Shell Command*" display-buffer-no-window (nil)))
(defun make-yearly-schedule ()
(interactive)
(let* ((start-date (format-time-string "%Y-01-01"))
(iter-dates (mapcar (lambda (x) (org-read-date nil nil (format "++%sw" x) nil (org-time-string-to-time start-date)) )
(number-sequence 0 48)
)))
(dolist (d iter-dates)
(and d (make-weekly-schedule d)))
(make-weekly-schedule)
(call-process-shell-command "make -B build-year" nil 0)
))
;; Generates an input latex file containing the events in a given week
(defun make-weekly-schedule (&optional the-day)
(interactive)
(let* ((current-date (or the-day (format-time-string "%Y-%m-%d")))
(weekly-regexp (get-week-regex current-date))
(events '())
(weekly-sched-out '("\\begin{weeklySchedule}"))
(weekdays (mapcar
(lambda (x)
(org-read-date nil nil
(concat "++"
(number-to-string x)) nil
;;(org-read-date nil t "-sun")
(org-read-date nil t "--sat" nil (org-time-string-to-time current-date))))
(number-sequence 1 7)))
(sched-name (format "%s-to-%s" (car weekdays) (car (last weekdays))))
)
(message "processing: %s -> %s" current-date sched-name)
;; (dolist (l weekly-sched-out)
;; (message "%s" l))
(add-to-list 'weekly-sched-out (format "{\\weekdays%s}" (mapconcat (lambda (x) (format "{%s}" x)) weekdays "")) t)
(dolist (row
(cdddr (let ((all-tables nil))
(org-table-map-tables
(lambda () (setq all-tables (cons all-tables (org-table-to-lisp (buffer-substring-no-properties (org-table-begin) (org-table-end)))))))
all-tables)))
(if (string-match weekly-regexp (nth 1 row))
(let* ((priority (substring (nth 0 row) 1 2))
(loc-date (substring (nth 1 row) 1 11))
(loc-day-of-week (-elem-index loc-date weekdays))
(time-parts (split-string (substring (nth 1 row) 18 29) ":\\|-"))
(start-time (max 8 (+ (string-to-number (nth 0 time-parts)) (/ (string-to-number (nth 1 time-parts)) 60.0))))
(end-time (+ (string-to-number (nth 2 time-parts)) (/ (string-to-number (nth 3 time-parts)) 60.0)))
(subj (nth 2 row))
(details (replace-regexp-in-string "^\\*\\([^\*]+\\)\\*" "\\\\textbf{\\1}" (nth 3 row) t))
(details (replace-regexp-in-string ";" "\\\\\\\\" details t))
)
(if (string= priority "-") (setq priority "N"))
(if (string= priority "1") (setq priority "X1"))
(if (string= priority "2") (setq priority "X2"))
(if (string= priority "3") (setq priority "X3"))
(if (string= priority "4") (setq priority "X4"))
(if (string= priority "5") (setq priority "X5"))
(if (string= priority "6") (setq priority "X6"))
(if (string= priority "7") (setq priority "X7"))
(add-to-list 'weekly-sched-out (format " \\event{%s}{%s}{%s}{%s}{%s}{%s}{%s};" priority loc-day-of-week start-time end-time subj (substring (nth 1 row) 18 29) details) t)
)
)
)
(add-to-list 'weekly-sched-out "\\end{weeklySchedule}" t)
(write-region (mapconcat (lambda (x) (format "%s\n" x)) weekly-sched-out "")
nil "./weekly_schedule_input.tex")
;(async-shell-command "pdflatex weekly_schedule.tex")
(message "%s.pdf" sched-name)
(if (not the-day)
(call-process-shell-command (format "make -B build-schedule-complete SCHEDNAME=%s" sched-name) nil 0)
(call-process-shell-command (format "make -B build-schedule-for-week SCHEDNAME=%s" sched-name))
)
(dolist (l weekly-sched-out)
(message "%s" l)
;(write-region l nil "./test-input.tex")
)
;; (dolist (event events)
;; (message "row is %s" event))
;; (dolist (w weekdays)
;; (message "d is (%s)" w))
))
(define-minor-mode schedule-minor-mode
" An org extension for organizing schedules"
:lighter " sched"
:keymap (let ((map (make-sparse-keymap)))
;; Sort by current column
(define-key map (kbd "C-c s") `(lambda ()
(interactive)
(org-table-sort-lines nil ?a)
(colorize-schedule)))
;; Sort by current column in reverse order
(define-key map (kbd "C-c r") `(lambda ()
(interactive)
(org-table-sort-lines nil ?A)
(colorize-schedule)))
;; Add entry to schedule
(define-key map (kbd "C-c e")
`(lambda ()
(interactive)
(let* ((p (read-string "Priority " nil nil " "))
(d (org-read-date))
(tm (read-string "Time " nil nil " "))
(c (read-string "Category " nil nil " "))
(w (read-string "Work " nil nil " ")))
(save-excursion
(goto-char (point-max))
(insert (format "| [%s] |" p))
(org-insert-time-stamp (org-read-date nil t) nil)
(insert (format ":(%s) | %s | %s |" tm c w))
(goto-char (line-beginning-position))
(execute-kbd-macro [?\t])
(goto-char (+ (line-beginning-position) 15))
(org-table-sort-lines nil ?a)
(colorize-schedule)))))
;; Show all
(define-key map (kbd "C-c a")
`(lambda ()
(interactive)
(if (string= "all" sched-period)
nil
(sched-show-all))))
;; Show urgent
(define-key map (kbd "C-c u")
`(lambda ()
(interactive)
(sched-show-urgent)))
;; Show today
(define-key map (kbd "C-c t")
`(lambda ()
(interactive)
(if (string= "day" sched-period)
(sched-show-all)
(sched-show-day sched-date))))
;; Show Week
(define-key map (kbd "C-c w")
`(lambda ()
(interactive)
(if (string= "week" sched-period)
(sched-show-all)
(progn
(setq sched-period "week")
(loccur (get-week-regex sched-date))
(colorize-schedule)))))
;; Show next day/week
(define-key map (kbd "C-c >")
`(lambda ()
(interactive)
(cond ((string= "day" sched-period) (sched-date-increment-day 1))
((string= "week" sched-period) (sched-date-increment-week 1))
((or (string= "all" sched-period) (string= "subject" sched-period))
(sched-increment-subject 1)))))
;; Show previous day/week
(define-key map (kbd "C-c <")
`(lambda ()
(interactive)
(cond ((string= "day" sched-period) (sched-date-increment-day -1))
((string= "week" sched-period) (sched-date-increment-week -1))
((or (string= "all" sched-period) (string= "subject" sched-period)) (sched-increment-subject -1)))))
;; toggle completion, anything else to X, X to nothing
(define-key map (kbd "C-c SPC")
`(lambda ()
(interactive)
(let ((line (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
(replace-region-contents
(line-beginning-position) (line-end-position)
`(lambda () (if (string-match "\\[X\\]" line)
(replace-regexp-in-string "\\[X\\]" "[ ]" line)
(replace-regexp-in-string "\\[.\\]" "[X]" line)))
)
(colorize-schedule)
)))
map)
(make-local-variable 'sched-date)
(make-local-variable 'sched-period)
(make-local-variable 'sched-dates)
(make-local-variable 'sched-subject)
(setq sched-dates (sched-get-dates))
(setq sched-subjects (sched-get-subjects))
(colorize-schedule)
(add-hook 'after-save-hook `(lambda ()
(interactive)
(colorize-schedule)
(setq sched-dates (sched-get-dates))
(setq sched-subjects (sched-get-subjects)))
nil 'make-it-local)
(setq loccur-highlight-matching-regexp nil)
t)
(require 'org)