-
Notifications
You must be signed in to change notification settings - Fork 59
/
jmax-org.el
1179 lines (1002 loc) · 35.7 KB
/
jmax-org.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
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
;;; jmax-org.el --- jmax customization for org-mode
;;; Commentary:
;;
(require 'org)
(require 'ox-beamer)
(require 'ox-texinfo)
(require 'ox-org)
(require 'ox-ascii)
; (require 'ox-odt)
(require 'org-inlinetask)
(require 'org-mouse)
(require 'org-contacts)
(require 'emacs-keybinding-command-tooltip-mode)
(require 'org-editmarks)
;;; Code:
;;* Basic variables
(add-to-list 'auto-mode-alist '("\\.org\\'" . org-mode))
(global-set-key "\C-cl" 'org-store-link)
(global-set-key "\C-ca" 'org-agenda)
(global-set-key "\C-cb" 'org-iswitchb) ; convenient switching between open org-buffers
(global-set-key "\C-e" 'end-of-line); overwrites org-mode \C-e definition
(global-set-key "\C-cL" 'org-insert-link-global)
(global-set-key "\C-co" 'org-open-at-point-global)
;; I like to press enter to follow a link. mouse clicks also work.
(setq org-return-follows-link t)
;; Setup the frame configuration for following links.
(setq org-link-frame-setup (quote ((gnus . org-gnus-no-new-news)
(file . find-file))))
; Use the current window for C-c ' source editing
(setq org-src-window-setup 'current-window)
;; use this code in emacs-lisp for folding code.
(global-set-key (kbd "C-M-]") (lambda () (interactive) (org-cycle t)))
(global-set-key (kbd "M-]") (lambda ()
(interactive)
(ignore-errors
(end-of-defun)
(beginning-of-defun))
(org-cycle)))
;; use ido completion wherever possible
(setq org-completion-use-ido t)
;; I do not like this mode
(auto-fill-mode -1)
;; turn off auto-fill in org-mode. It is not enough to turn it off
;; everywhere.
(remove-hook 'text-mode-hook #'turn-on-auto-fill)
;; allow lists with letters in them.
(setq org-list-allow-alphabetical t)
;; capture key binding
(define-key global-map "\C-cc" 'org-capture)
;; setup archive location in archive directory in current folder
(setq org-archive-location "archive/%s_archive::")
;;* Org-id
(setq org-id-link-to-org-use-id 'create-if-interactive)
(setq org-link-search-must-match-exact-headline 'query-to-create)
(setq org-id-locations-file
(expand-file-name "user/.org-id-locations" starter-kit-dir))
(require 'org-id)
;;* Speed commands
;; activate single letter commands at beginning of a headline.
;; User-defined Speed commands
;; ===========================
;; Built-in Speed commands
;; =======================
;; Outline Navigation
;; ------------------
;; n (org-speed-move-safe (quote outline-next-visible-heading))
;; p (org-speed-move-safe (quote outline-previous-visible-heading))
;; f (org-speed-move-safe (quote org-forward-heading-same-level))
;; b (org-speed-move-safe (quote org-backward-heading-same-level))
;; F org-next-block
;; B org-previous-block
;; u (org-speed-move-safe (quote outline-up-heading))
;; j org-goto
;; g (org-refile t)
;; Outline Visibility
;; ------------------
;; c org-cycle
;; C org-shifttab
;; org-display-outline-path
;; s org-narrow-to-subtree
;; = org-columns
;; Outline Structure Editing
;; -------------------------
;; U org-shiftmetaup
;; D org-shiftmetadown
;; r org-metaright
;; l org-metaleft
;; R org-shiftmetaright
;; L org-shiftmetaleft
;; i (progn (forward-char 1) (call-interactively (quote org-insert-heading-respect-content)))
;; ^ org-sort
;; w org-refile
;; a org-archive-subtree-default-with-confirmation
;; @ org-mark-subtree
;; # org-toggle-comment
;; Clock Commands
;; --------------
;; I org-clock-in
;; O org-clock-out
;; Meta Data Editing
;; -----------------
;; t org-todo
;; , (org-priority)
;; 0 (org-priority 32)
;; 1 (org-priority 65)
;; 2 (org-priority 66)
;; 3 (org-priority 67)
;; : org-set-tags-command
;; e org-set-effort
;; E org-inc-effort
;; W (lambda (m) (interactive "sMinutes before warning: ") (org-entry-put (point) "APPT_WARNTIME" m))
;; Agenda Views etc
;; ----------------
;; v org-agenda
;; / org-sparse-tree
;; Misc
;; ----
;; o org-open-at-point
;; ? org-speed-command-help
;; < (org-agenda-set-restriction-lock (quote subtree))
;; > (org-agenda-remove-restriction-lock)
;;* New speed bindings
(defun org-teleport (&optional arg)
"Teleport the current heading to after a headline selected with avy.
With a prefix ARG move the headline to before the selected
headline. With a numeric prefix, set the headline level. If ARG
is positive, move after, and if negative, move before."
(interactive "P")
;; Kill current headline
(org-mark-subtree)
(kill-region (region-beginning) (region-end))
;; Jump to a visible headline
(avy-with avy-goto-line (avy--generic-jump "^\\*+" nil avy-style))
(cond
;; Move before and change headline level
((and (numberp arg) (> 0 arg))
(save-excursion
(yank))
;; arg is what we want, second is what we have
;; if n is positive, we need to demote (increase level)
(let ((n (- (abs arg) (car (org-heading-components)))))
(cl-loop for i from 1 to (abs n)
do
(if (> 0 n)
(org-promote-subtree)
(org-demote-subtree)))))
;; Move after and change level
((and (numberp arg) (< 0 arg))
(org-mark-subtree)
(goto-char (region-end))
(when (eobp) (insert "\n"))
(save-excursion
(yank))
;; n is what we want and second is what we have
;; if n is positive, we need to demote
(let ((n (- (abs arg) (car (org-heading-components)))))
(cl-loop for i from 1 to (abs n)
do
(if (> 0 n) (org-promote-subtree)
(org-demote-subtree)))))
;; move to before selection
((equal arg '(4))
(save-excursion
(yank)))
;; move to after selection
(t
(org-mark-subtree)
(goto-char (region-end))
(when (eobp) (insert "\n"))
(save-excursion
(yank))))
(outline-hide-leaves))
(add-to-list 'org-speed-commands-user (cons "m" 'org-mark-subtree))
(add-to-list 'org-speed-commands-user (cons "S" 'widen))
(add-to-list 'org-speed-commands-user (cons "k" (lambda ()
(org-mark-subtree)
(kill-region
(region-beginning)
(region-end)))))
(add-to-list 'org-speed-commands-user
(cons "q" (lambda ()
(avy-with avy-goto-line
(avy--generic-jump "^\\*+" nil avy-style)))))
(add-to-list 'org-speed-commands-user (cons "T" 'org-teleport))
(setq org-use-speed-commands t)
;;* Misc org settings
;; renumber footnotes when new ones are inserted
(setq org-footnote-auto-adjust t)
;; default with images open
(setq org-startup-with-inline-images "inlineimages")
;; clocking setup http://www.gnu.org/software/emacs/manual/html_node/org/Clocking-work-time.html
(setq org-clock-persist 'history)
(org-clock-persistence-insinuate)
;; store clock entries in a drawer
(setq org-clock-into-drawer t)
;;* Expansions for blocks
(require 'ob-ipython)
(add-to-list 'org-structure-template-alist
'("ip" "#+BEGIN_SRC ipython :session\n?\n#+END_SRC" "<src lang=\"python\">\n?\n</src>"))
;; add <p for python expansion
(add-to-list 'org-structure-template-alist
'("p" "#+BEGIN_SRC python\n?\n#+END_SRC" "<src lang=\"python\">\n?\n</src>"))
;; add <por for python expansion with raw output
(add-to-list 'org-structure-template-alist
'("por" "#+BEGIN_SRC python :results output raw\n?\n#+END_SRC" "<src lang=\"python\">\n?\n</src>"))
;; add <pv for python expansion with value
(add-to-list 'org-structure-template-alist
'("pv" "#+BEGIN_SRC python :results value\n?\n#+END_SRC" "<src lang=\"python\">\n?\n</src>"))
;; add <el for emacs-lisp expansion
(add-to-list 'org-structure-template-alist
'("el" "#+BEGIN_SRC emacs-lisp\n?\n#+END_SRC" "<src lang=\"emacs-lisp\">\n?\n</src>"))
;; add <sh for shell
(add-to-list 'org-structure-template-alist
'("sh" "#+BEGIN_SRC sh\n?\n#+END_SRC" "<src lang=\"shell\">\n?\n</src>"))
(add-to-list 'org-structure-template-alist
'("lh" "#+latex_header: " ""))
(add-to-list 'org-structure-template-alist
'("lc" "#+latex_class: " ""))
(add-to-list 'org-structure-template-alist
'("lco" "#+latex_class_options: " ""))
(add-to-list 'org-structure-template-alist
'("ao" "#+attr_org: " ""))
(add-to-list 'org-structure-template-alist
'("al" "#+attr_latex: " ""))
(add-to-list 'org-structure-template-alist
'("ca" "#+caption: " ""))
(add-to-list 'org-structure-template-alist
'("tn" "#+tblname: " ""))
(add-to-list 'org-structure-template-alist
'("n" "#+name: " ""))
;; table expansions
(loop for i from 1 to 6
do
(let ((template (make-string i ?t))
(expansion (concat "|"
(mapconcat
'identity
(loop for j to i collect " ")
"|"))))
(setf (substring expansion 2 3) "?")
(add-to-list 'org-structure-template-alist
`(,template ,expansion ""))))
;;* Babel settings
;; do not evaluate code on export by default
(setq org-export-babel-evaluate nil)
;; enable prompt-free code running
(setq org-confirm-babel-evaluate nil
org-confirm-elisp-link-function nil
org-confirm-shell-link-function nil)
;; register languages in org-mode
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(python . t)
(sh . t)
(matlab . t)
(sqlite . t)
(ruby . t)
(perl . t)
(org . t)
(dot . t)
(plantuml . t)
(R . t)
(C . t)))
;; no extra indentation in the source blocks
(setq org-src-preserve-indentation t)
;; use syntax highlighting in org-file code blocks
(setq org-src-fontify-natively t)
;; language specific headers. I think this comes before the defaults
;; for emacs-lisp I want results to be value
(setq org-babel-default-header-args:emacs-lisp
(cons '(:results . "value replace")
(assq-delete-all :results org-babel-default-header-args)))
;; for everything else set default :results to output
(setq org-babel-default-header-args
(cons '(:results . "output replace")
(assq-delete-all :results org-babel-default-header-args)))
;; set default exports to both code and results
(setq org-babel-default-header-args
(cons '(:exports . "both")
(assq-delete-all :exports org-babel-default-header-args)))
;; Interpret "_" and "^" for export when braces are used.
(setq org-export-with-sub-superscripts '{})
;;* Agenda setup
; I don't want to see things that are done. turn that off here.
; http://orgmode.org/manual/Global-TODO-list.html#Global-TODO-list
(setq org-agenda-skip-scheduled-if-done t)
(setq org-agenda-skip-deadline-if-done t)
(setq org-agenda-skip-timestamp-if-done t)
(setq org-agenda-todo-ignore-scheduled t)
(setq org-agenda-todo-ignore-deadlines t)
(setq org-agenda-todo-ignore-timestamp t)
(setq org-agenda-todo-ignore-with-date t)
(setq org-agenda-start-on-weekday nil) ;; start on current day
(setq org-upcoming-deadline '(:foreground "blue" :weight bold))
;; give me some warning of upcoming deadlines
(setq org-deadline-warning-days 0)
;; record time I finished a task when I change it to DONE
(setq org-log-done 'time)
(add-to-list
'org-agenda-custom-commands
'("w" "Weekly Review"
( ;; deadlines
(tags-todo "+DEADLINE<=\"<today>\""
((org-agenda-overriding-header "Late Deadlines")))
;; scheduled past due
(tags-todo "+SCHEDULED<=\"<today>\""
((org-agenda-overriding-header "Late Scheduled")))
;; now the agenda
(agenda ""
((org-agenda-overriding-header "weekly agenda")
(org-agenda-ndays 7)
(org-agenda-tags-todo-honor-ignore-options t)
(org-agenda-todo-ignore-scheduled nil)
(org-agenda-todo-ignore-deadlines nil)
(org-deadline-warning-days 0)))
;; and last a global todo list
(todo "TODO"))))
;;* New org-links
;; support for links to microsoft docx,pptx,xlsx files
;; standard org-mode opens these as zip-files
;; http://orgmode.org/manual/Adding-hyperlink-types.html
(defun org-msx-open (path)
"Visit the msx file on PATH.
uses the dos command:
start empty title path"
(cond
((string= "windows-nt" system-type)
(shell-command
(concat "start \"title\" " (shell-quote-argument path)) t))
((string= "darwin" system-type)
(shell-command
(concat "open " (shell-quote-argument path)) t))))
(org-add-link-type
"msx"
'org-msx-open)
(org-add-link-type
"attachfile"
(lambda (link-string) (org-open-file link-string))
;; formatting
(lambda (keyword desc format)
(cond
((eq format 'html) (format "")); no output for html
((eq format 'latex)
;; write out the latex command
(format "\\attachfile{%s}" keyword)))))
;;* Export settings
(setq org-latex-default-packages-alist
'(("AUTO" "inputenc" t)
("" "lmodern" nil)
("T1" "fontenc" t)
("" "fixltx2e" nil)
; ("" "charter" nil) ;; a decent font
; ("expert" "mathdesign" nil)
("" "graphicx" t)
("" "longtable" nil)
("" "float" nil)
("" "wrapfig" nil)
("" "rotating" nil)
("normalem" "ulem" t)
("" "amsmath" t)
("" "textcomp" t)
("" "marvosym" t)
("" "wasysym" t)
("" "amssymb" t)
("" "amsmath" t)
("version=3" "mhchem" t)
("numbers,super,sort&compress" "natbib" nil)
("" "natmove" nil)
("" "url" nil)
("" "minted" nil)
("" "underscore" nil)
("linktocpage,pdfstartview=FitH,colorlinks,
linkcolor=blue,anchorcolor=blue,
citecolor=blue,filecolor=blue,menucolor=blue,urlcolor=blue"
"hyperref" nil)
("" "attachfile" nil)))
;; do not put in \hypersetup use your own
;; \hypersetup{pdfkeywords={%s},\n pdfsubject={%s},\n pdfcreator={%s}
(setq org-latex-with-hyperref nil)
;; this is for code syntax highlighting in export
(setq org-latex-listings 'minted)
(setq org-latex-minted-options
'(("frame" "lines")
("fontsize" "\\scriptsize")
("linenos" "")))
;; for minted you must run latex with -shell-escape because it calls pygmentize as an external program
;; (setq org-latex-pdf-process
;; '("pdflatex -shell-escape -interaction nonstopmode -output-directory %o %b"
;; "bibtex %b"
;; "makeindex %b"
;; "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %b"
;; "pdflatex -shell-escape -interaction nonstopmode -output-directory %o %b"))
;; I have not had good luck with this on windows
;(setq org-latex-to-pdf-process '("texi2dvi --pdf --clean --verbose --batch"))
;; avoid getting \maketitle right after begin{document}
;; you should put \maketitle if and where you want it.
(setq org-latex-title-command "")
;; customized article. better margins
(add-to-list 'org-latex-classes
'("cmu-article" ;class-name
"\\documentclass{article}
\\usepackage[top=1in, bottom=1.in, left=1in, right=1in]{geometry}
[PACKAGES]
[EXTRA]" ;;header-string
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*a{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
(add-to-list 'org-latex-classes
'("article-no-defaults" ;class-name
"\\documentclass{article}
[NO-DEFAULT-PACKAGES]
[PACKAGES]
[EXTRA]" ;;header-string
("\\section{%s}" . "\\section*{%s}")
("\\subsection{%s}" . "\\subsection*a{%s}")
("\\subsubsection{%s}" . "\\subsubsection*{%s}")
("\\paragraph{%s}" . "\\paragraph*{%s}")
("\\subparagraph{%s}" . "\\subparagraph*{%s}")))
(require 'ox-cmu-qualifier)
(require 'ox-cmu-ms-report)
(require 'ox-cmu-dissertation)
(require 'ox-manuscript)
(require 'ox-archive)
;;* org-require
;; org path for loadable org-files
;; (defvar org-load-path
;; (list (file-name-as-directory
;; (expand-file-name "org" starter-kit-dir)))
;; "List of directories to find org-files that `org-babel-load-file' can load code from.")
;; (defun org-require (feature)
;; "Load a FEATURE from an org-file.
;; FEATURE is a symbol, and it is loaded from an org-file by the name of FEATURE.org, that is in the `org-load-path'. The FEATURE is loaded from `org-babel-load-file'."
;; (let ((org-file (concat (symbol-name feature) ".org"))
;; (path))
;; ;; find the org-file
;; (catch 'result
;; (loop for dir in org-load-path do
;; (when (file-exists-p
;; (setq path
;; (expand-file-name
;; org-file
;; dir)))
;; (throw 'result path))))
;; (let ((default-directory (file-name-directory path)))
;; (org-babel-load-file path))))
(add-to-list 'load-path
(expand-file-name "org" starter-kit-dir))
(require 'org-show)
;;* cm-mode
(add-to-list 'load-path
(expand-file-name "criticmarkup-emacs" starter-kit-dir))
(require 'cm-mode)
(require 'cm-mods)
;;* https://github.com/jkitchin/org-ref
(add-to-list 'load-path
(expand-file-name "org-ref" starter-kit-dir))
(require 'org-ref)
(require 'doi-utils)
(require 'org-ref-isbn)
(require 'org-ref-pubmed)
(require 'org-ref-arxiv)
(require 'org-ref-bibtex)
(require 'org-ref-pdf)
(require 'org-ref-url-utils)
;; variables that control bibtex key format for auto-generation
;; I want firstauthor-year-title-words
;; this usually makes a legitimate filename to store pdfs under.
(setq bibtex-autokey-year-length 4
bibtex-autokey-name-year-separator "-"
bibtex-autokey-year-title-separator "-"
bibtex-autokey-titleword-separator "-"
bibtex-autokey-titlewords 2
bibtex-autokey-titlewords-stretch 1
bibtex-autokey-titleword-length 5)
;;* Images in org-mode - default width
(setq org-image-actual-width '(600))
;; refresh images after running a block
(defun org-refresh-images ()
"Refreshes images displayed inline."
(interactive)
(org-remove-inline-images)
(org-display-inline-images))
(add-hook 'org-babel-after-execute-hook
(lambda () (org-refresh-images)))
;;* font-lock on LaTeX fragments
(add-hook 'org-mode-hook
(lambda ()
(font-lock-add-keywords
nil
`((,(mapconcat (lambda (x)
(nth 1 x))
org-latex-regexps
"\\|") 0 '(:foreground "blue"))
("@@latex:[^@]*@@" 0 '(:foreground "blue"))))))
;;* Insert org figures and tables
(defun jmax-insert-figure (fname width params)
"Insert FNAME as a figure in org-mode.
WIDTH specifies how wide it should be, e.g. 300, 3.25in, or 4cm.
PARAMS is a string like \":placement [H]\"."
(interactive (list
(ido-read-file-name "File: ")
(read-input "Org width: " "200")
(read-input "Parameters: ")))
;; parse width
(let ((dpi 72)
org-width latex-width)
;; calculate width
(cond
((string= "" width)
(setq width nil
org-width nil))
((string-match "\\([0-9]\\.?[0-9]*\\)in" width)
(setq org-width (* dpi (string-to-number (string-match 1 width)))
latex-width width))
((string-match "\\([0-9]\\.?[0-9]*\\)cm" width)
(setq org-width (* dpi (string-to-number (string-match 1 width)) 2.54)
latex-width width))
((string-match "\\([0-9]*\\)" width)
(setq org-width (string-to-number width)
latex-width (/ org-width dpi))))
(when width
(insert (format "#+attr_latex: :width %sin %s\n" latex-width params))
(insert (format "#+attr_org: :width %s\n" org-width)))
(insert "#+caption: \n")
(insert (format "[[./%s]]\n" fname))
(forward-line -2)
(end-of-line)
(org-redisplay-inline-images)))
;; make it possible to insert from helm.
(defun helm-insert-org-figure (target)
(jmax-insert-figure
(file-relative-name target)
(read-input "Width: ")
(read-input "Parameters: ")))
(add-hook 'helm-find-files-before-init-hook
(lambda ()
(helm-add-action-to-source
"Insert as org-mode figure"
'helm-insert-org-figure
helm-source-find-files)))
(defun jmax-insert-table (ncols tblname attributes)
"Insert a table with NCOLS and named TBLNAME.
If you enter ATTRIBUTES they are inserted as LaTeX attributes."
(interactive "nColumns: \nsName: \nsAttributes: ")
(when (not (string= "" tblname))
(insert (format "#+tblname: %s\n" tblname)))
(when (not (string= "" attributes))
(insert (format "#+attr_latex: %s\n" attributes)))
(insert "#+caption: \n")
(loop
initially (insert "|")
repeat ncols do (insert " |" )
finally (insert "\n"))
(forward-line -2)
(end-of-line))
;;* Adapting C-c C-c for Latex overlays
;; Define a new toggling function for equations.
(defun org-toggle-latex-overlays (arg)
"Toggle LaTeX fragments. The prefix ARG is passed to `org-preview-latex-fragment'."
(interactive "P")
(if org-latex-fragment-image-overlays
(org-remove-latex-fragment-image-overlays)
(org-preview-latex-fragment arg)))
(org-defkey org-mode-map "\C-c\C-x\C-l" 'org-toggle-latex-overlays)
(defadvice org-ctrl-c-ctrl-c (around latex-overlays nil activate)
"Ignore latex overlays in C-cC-c."
(let ((org-latex-fragment-image-overlays nil))
ad-do-it))
(require 'ore)
;;* Python sessions
;; define a better org-mode tab
(define-key org-mode-map (kbd "<tab>")
'(menu-item "org-mode-tab" nil
:filter (lambda (&optional _)
(when
(and
(org-in-src-block-p t)
(string= "python"
(org-element-property
:language
(org-element-context))))
(org-babel-do-key-sequence-in-edit-buffer
(kbd "<tab>"))))))
;; Make shift-tab remove spaces backwards. Jake wanted this.
(define-key org-mode-map (kbd "S-<tab>")
'(menu-item "org-mode-tab" nil
:filter (lambda (&optional _)
(when
(and
(org-in-src-block-p t)
(string= "python"
(org-element-property
:language
(org-element-context))))
(let ((p (point)))
(skip-chars-backward " ")
(setf (buffer-substring p (point)) ""))))))
(defun org-babel-python-strip-session-chars ()
"Remove >>> and ... from a Python session output."
(when (and (org-element-property :parameters (org-element-at-point))
(string=
"python"
(org-element-property :language (org-element-at-point)))
(string-match
":session"
(org-element-property :parameters (org-element-at-point))))
(save-excursion
(when (org-babel-where-is-src-block-result)
(goto-char (org-babel-where-is-src-block-result))
(end-of-line 1)
;(while (looking-at "[\n\r\t\f ]") (forward-char 1))
(while (re-search-forward
"\\(>>> \\|\\.\\.\\. \\|: $\\|: >>>$\\)"
(org-element-property :end (org-element-at-point))
t)
(replace-match "")
;; this enables us to get rid of blank lines and blank : >>>
(beginning-of-line)
(when (looking-at "^$")
(kill-line)))))))
(add-hook 'org-babel-after-execute-hook 'org-babel-python-strip-session-chars)
;;** Asynchronous Python
(defun org-babel-async-execute:python ()
"Execute the python src-block at point asynchronously.
:var headers are supported.
:results output is all that is supported for output.
A new window will pop up showing you the output as it appears,
and the output in that window will be put in the RESULTS section
of the code block."
(interactive)
(let* ((current-file (buffer-file-name))
(uuid (org-id-uuid))
(code (org-element-property :value (org-element-context)))
(temporary-file-directory ".")
(tempfile (make-temp-file "py-"))
(pbuffer (format "*%s*" uuid))
(varcmds (org-babel-variable-assignments:python
(nth 2 (org-babel-get-src-block-info))))
process)
;; get rid of old results, and put a place-holder for the new results to
;; come.
(org-babel-remove-result)
(save-excursion
(re-search-forward "#\\+END_SRC")
(insert (format
"\n\n#+RESULTS: %s\n: %s"
(or (org-element-property :name (org-element-context))
"")
uuid)))
;; open the results buffer to see the results in.
(switch-to-buffer-other-window pbuffer)
;; Create temp file containing the code.
(with-temp-file tempfile
;; if there are :var headers insert them.
(dolist (cmd varcmds)
(insert cmd)
(insert "\n"))
(insert code))
;; run the code
(setq process (start-process
uuid
pbuffer
"python"
tempfile))
;; when the process is done, run this code to put the results in the
;; org-mode buffer.
(set-process-sentinel
process
`(lambda (process event)
(delete-file ,tempfile)
(unwind-protect
(save-window-excursion
(save-excursion
(save-restriction
(with-current-buffer (find-file-noselect ,current-file)
(goto-char (point-min))
(when (re-search-forward ,uuid nil t)
(beginning-of-line)
(kill-line)
(when (with-current-buffer
,pbuffer
(buffer-string)))
(insert
(mapconcat
(lambda (x)
(format ": %s" x))
(butlast (split-string
(with-current-buffer
,pbuffer
(buffer-string))
"\n"))
"\n")))))))
;; delete the results buffer then delete the tempfile.
;; finally, delete the process.
(when (get-buffer ,pbuffer)
(kill-buffer ,pbuffer)
(delete-window))
(delete-process process))))))
(defun org-babel-kill-async ()
"Kill the current async process.
Run this in the code block that is running."
(interactive)
(goto-char (org-babel-where-is-src-block-result))
(forward-line)
(forward-char)
(interrupt-process
(s-trim (buffer-substring (point) (line-end-position)))))
;;** Asynchronous shell commands
;; (defun org-babel-async-execute:sh ()
;; "Execute the shell src-block at point asynchronously.
;; :var headers are supported.
;; :results output is all that is supported for output.
;; A new window will pop up showing you the output as it appears,
;; and the output in that window will be put in the RESULTS section
;; of the code block."
;; (interactive)
;; (let* ((current-file (buffer-file-name))
;; (uuid (org-id-uuid))
;; (code (org-element-property :value (org-element-context)))
;; (temporary-file-directory ".")
;; (tempfile (make-temp-file "sh-"))
;; (pbuffer (format "*%s*" uuid))
;; (varcmds (org-babel-variable-assignments:sh
;; (nth 2 (org-babel-get-src-block-info))))
;; process)
;; ;; get rid of old results, and put a place-holder for the new results to
;; ;; come.
;; (org-babel-remove-result)
;; (save-excursion
;; (re-search-forward "#\\+END_SRC")
;; (insert (format
;; "\n\n#+RESULTS: %s\n: %s"
;; (or (org-element-property :name (org-element-context))
;; "")
;; uuid)))
;; ;; open the results buffer to see the results in.
;; (switch-to-buffer-other-window pbuffer)
;; ;; Create temp file containing the code.
;; (with-temp-file tempfile
;; ;; if there are :var headers insert them.
;; (dolist (cmd varcmds)
;; (insert cmd)
;; (insert "\n"))
;; (insert code))
;; ;; run the code
;; (setq process (start-process
;; uuid
;; pbuffer
;; "bash"
;; tempfile))
;; ;; when the process is done, run this code to put the results in the
;; ;; org-mode buffer.
;; (set-process-sentinel
;; process
;; `(lambda (process event)
;; (save-window-excursion
;; (save-excursion
;; (save-restriction
;; (with-current-buffer (find-file-noselect ,current-file)
;; (goto-char (point-min))
;; (re-search-forward ,uuid)
;; (beginning-of-line)
;; (kill-line)
;; (insert
;; (mapconcat
;; (lambda (x)
;; (format ": %s" x))
;; (butlast (split-string
;; (with-current-buffer
;; ,pbuffer
;; (buffer-string))
;; "\n"))
;; "\n"))))))
;; ;; delete the results buffer then delete the tempfile.
;; ;; finally, delete the process.
;; (when (get-buffer ,pbuffer)
;; (kill-buffer ,pbuffer)
;; (delete-window))
;; (delete-file ,tempfile)
;; (delete-process process)))))
;; (defun org-babel-execute:sh (body params)
;; "Execute a block of Shell commands with Babel.
;; Adds :async to the header vars
;; This function is called by `org-babel-execute-src-block'."
;; (let* ((session (org-babel-sh-initiate-session
;; (cdr (assoc :session params))))
;; (async (assoc :async params))
;; (stdin (let ((stdin (cdr (assoc :stdin params))))
;; (when stdin (org-babel-sh-var-to-string
;; (org-babel-ref-resolve stdin)))))
;; (full-body (org-babel-expand-body:generic
;; body params (org-babel-variable-assignments:sh params))))
;; (if async
;; ;; run asynchronously
;; (org-babel-async-execute:sh)
;; ;; else run regularly
;; (org-babel-reassemble-table
;; (org-babel-sh-evaluate session full-body params stdin)
;; (org-babel-pick-name
;; (cdr (assoc :colname-names params)) (cdr (assoc :colnames params)))
;; (org-babel-pick-name
;; (cdr (assoc :rowname-names params)) (cdr (assoc :rownames params)))))))
;;** Restarting an org-babel session
(defun src-block-in-session-p (&optional name)
"Return if src-block is in a session of NAME.
NAME may be nil for unnamed sessions."
(let* ((info (org-babel-get-src-block-info))
(lang (nth 0 info))
(body (nth 1 info))
(params (nth 2 info))
(session (cdr (assoc :session params))))
(cond
;; unnamed session, both name and session are nil
((and (null session)
(null name))
t)
;; Matching name and session
((and
(stringp name)
(stringp session)
(string= name session))
t)
;; no match
(t nil))))
(defun org-babel-restart-session-to-point (&optional arg)
"Restart session up to the src-block in the current point.
Goes to beginning of buffer and executes each code block with
`org-babel-execute-src-block' that has the same language and
session as the current block. ARG has same meaning as in
`org-babel-execute-src-block'."
(interactive "P")
(unless (org-in-src-block-p)
(error "You must be in a src-block to run this command"))
(org-babel-kill-session)
(let* ((current-point (point-marker))
(info (org-babel-get-src-block-info))
(lang (nth 0 info))
(params (nth 2 info))
(session (cdr (assoc :session params))))
(save-excursion
(goto-char (point-min))
(while (re-search-forward org-babel-src-block-regexp nil t)
;; goto start of block
(goto-char (match-beginning 0))
(let* ((this-info (org-babel-get-src-block-info))
(this-lang (nth 0 this-info))
(this-params (nth 2 this-info))
(this-session (cdr (assoc :session this-params))))
(when