diff --git a/module2/ressources/rr_org/init.el b/module2/ressources/rr_org/init.el index 5ce0517e27f2555d960833f79c65b2b3b7da026b..a81549de88782edcf0a757bdc4dd2f6765f1b1d3 100644 --- a/module2/ressources/rr_org/init.el +++ b/module2/ressources/rr_org/init.el @@ -227,51 +227,64 @@ Entered on %U )) (setq org-src-preserve-indentation t) -(require 'org-tempo nil t) +(setq rrmooc/new-org-templates (version<= "9.2" (org-version))) +(when rrmooc/new-org-templates + (require 'org-tempo)) -(add-to-list 'org-structure-template-alist - '("s" "#+begin_src ?\n\n#+end_src" "\n\n")) +(require 'subr-x) +(defun rrmooc/add-org-template (old-style-template) + (add-to-list 'org-structure-template-alist + (if rrmooc/new-org-templates + (cons + (first old-style-template) + (string-trim-right (substring (second old-style-template) 8 -9))) + old-style-template))) + +(unless rrmooc/new-org-templates + ;; this template is predefined in the new templating system + (rrmooc/add-org-template + '("s" "#+begin_src ?\n\n#+end_src" "\n\n"))) -(add-to-list 'org-structure-template-alist - '("m" "#+begin_src emacs-lisp :tangle init.el\n\n#+end_src" "\n\n")) +(rrmooc/add-org-template + '("m" "#+begin_src emacs-lisp\n\n#+end_src" "\n\n")) -(add-to-list 'org-structure-template-alist - '("r" "#+begin_src R :results output :session *R* :exports both\n\n#+end_src" "\n\n")) +(rrmooc/add-org-template + '("r" "#+begin_src R :results output :session *R* :exports both\n\n#+end_src" "\n\n")) -(add-to-list 'org-structure-template-alist - '("R" "#+begin_src R :results output graphics :file (org-babel-temp-file \"figure\" \".png\") :exports both :width 600 :height 400 :session *R* \n\n#+end_src" "\n\n")) +(rrmooc/add-org-template + '("R" "#+begin_src R :results output graphics :file (org-babel-temp-file \"figure\" \".png\") :exports both :width 600 :height 400 :session *R* \n\n#+end_src" "\n\n")) -(add-to-list 'org-structure-template-alist - '("RR" "#+begin_src R :results output graphics :file (org-babel-temp-file (concat (file-name-directory (or load-file-name buffer-file-name)) \"figure-\") \".png\") :exports both :width 600 :height 400 :session *R* \n\n#+end_src" "\n\n")) +(rrmooc/add-org-template + '("RR" "#+begin_src R :results output graphics :file (org-babel-temp-file (concat (file-name-directory (or load-file-name buffer-file-name)) \"figure-\") \".png\") :exports both :width 600 :height 400 :session *R* \n\n#+end_src" "\n\n")) -(add-to-list 'org-structure-template-alist - '("p" "#+begin_src python :results output :exports both\n\n#+end_src" "\n\n")) +(rrmooc/add-org-template + '("p" "#+begin_src python :results output :exports both\n\n#+end_src" "\n\n")) -(add-to-list 'org-structure-template-alist - '("P" "#+begin_src python :results output :session :exports both\n\n#+end_src" "\n\n")) +(rrmooc/add-org-template + '("P" "#+begin_src python :results output :session :exports both\n\n#+end_src" "\n\n")) -(add-to-list 'org-structure-template-alist - '("PP" "#+begin_src python :results file :session :var matplot_lib_filename=(org-babel-temp-file \"figure\" \".png\") :exports both\nimport matplotlib.pyplot as plt\n\nimport numpy\nx=numpy.linspace(-15,15)\nplt.figure(figsize=(10,5))\nplt.plot(x,numpy.cos(x)/x)\nplt.tight_layout()\n\nplt.savefig(matplot_lib_filename)\nmatplot_lib_filename\n#+end_src" "\n\n")) +(rrmooc/add-org-template + '("PP" "#+begin_src python :results file :session :var matplot_lib_filename=(org-babel-temp-file \"figure\" \".png\") :exports both\nimport matplotlib.pyplot as plt\n\nimport numpy\nx=numpy.linspace(-15,15)\nplt.figure(figsize=(10,5))\nplt.plot(x,numpy.cos(x)/x)\nplt.tight_layout()\n\nplt.savefig(matplot_lib_filename)\nmatplot_lib_filename\n#+end_src" "\n\n")) (if (memq system-type '(windows-nt ms-dos)) ;; Non-session shell execution does not seem to work under Windows, so we use ;; a named session just like for B. - (add-to-list 'org-structure-template-alist - '("b" "#+begin_src shell :session session :results output :exports both\n\n#+end_src" "\n\n")) - (add-to-list 'org-structure-template-alist - '("b" "#+begin_src shell :results output :exports both\n\n#+end_src" "\n\n"))) - -(add-to-list 'org-structure-template-alist - '("B" "#+begin_src shell :session *shell* :results output :exports both \n\n#+end_src" "\n\n")) - -(add-to-list 'org-structure-template-alist - '("g" "#+begin_src dot :results output graphics :file \"/tmp/graph.pdf\" :exports both - digraph G { - node [color=black,fillcolor=white,shape=rectangle,style=filled,fontname=\"Helvetica\"]; - A[label=\"A\"] - B[label=\"B\"] - A->B - }\n#+end_src" "\n\n")) + (rrmooc/add-org-template + '("b" "#+begin_src shell :session session :results output :exports both\n\n#+end_src" "\n\n")) + (rrmooc/add-org-template + '("b" "#+begin_src shell :results output :exports both\n\n#+end_src" "\n\n"))) + +(rrmooc/add-org-template + '("B" "#+begin_src shell :session *shell* :results output :exports both \n\n#+end_src" "\n\n")) + +(rrmooc/add-org-template + '("g" "#+begin_src dot :results output graphics :file \"/tmp/graph.pdf\" :exports both +digraph G { +node [color=black,fillcolor=white,shape=rectangle,style=filled,fontname=\"Helvetica\"]; +A[label=\"A\"] +B[label=\"B\"] +A->B +}\n#+end_src" "\n\n")) (global-set-key (kbd "C-c S-t") 'org-babel-execute-subtree) diff --git a/module2/ressources/rr_org/init.org b/module2/ressources/rr_org/init.org index d6e20cbf16c23f9ab6bac5411fba30e260da4769..b4b1fec88434dba22493dda35ed9c720ddeccb19 100644 --- a/module2/ressources/rr_org/init.org +++ b/module2/ressources/rr_org/init.org @@ -83,7 +83,7 @@ document, simply =M-x org-babel-tangle=. ("melpa-stable" . 10))) #+END_SRC * Install missing packages -#+begin_src emacs-lisp +#+begin_src emacs-lisp :tangle init.el (dolist (pkg '(ess auctex htmlize @@ -92,7 +92,7 @@ document, simply =M-x org-babel-tangle=. (package-install pkg))) #+end_src * Import environment variables -#+begin_src emacs-lisp +#+begin_src emacs-lisp :tangle init.el (exec-path-from-shell-initialize) (exec-path-from-shell-copy-env "PYTHONPATH") #+end_src @@ -366,57 +366,72 @@ Additional shortcuts for navigating through org-mode documents: ** Adding source code blocks: :LUKA: *** Enable the old template system with Orgmode 9.2 and later #+begin_src emacs-lisp :tangle init.el -(require 'org-tempo nil t) +(setq rrmooc/new-org-templates (version<= "9.2" (org-version))) +(when rrmooc/new-org-templates + (require 'org-tempo)) +#+end_src +*** Template definitions that work with the old and new templating systems +#+begin_src emacs-lisp :tangle init.el +(require 'subr-x) +(defun rrmooc/add-org-template (old-style-template) + (add-to-list 'org-structure-template-alist + (if rrmooc/new-org-templates + (cons + (first old-style-template) + (string-trim-right (substring (second old-style-template) 8 -9))) + old-style-template))) #+end_src *** With capital letters: To use this type \n\n")) +(unless rrmooc/new-org-templates + ;; this template is predefined in the new templating system + (rrmooc/add-org-template + '("s" "#+begin_src ?\n\n#+end_src" "\n\n"))) #+end_src *** Emacs-elisp code: To use this type \n\n")) +(rrmooc/add-org-template + '("m" "#+begin_src emacs-lisp\n\n#+end_src" "\n\n")) #+end_src *** R code: To use this type \n\n")) +(rrmooc/add-org-template + '("r" "#+begin_src R :results output :session *R* :exports both\n\n#+end_src" "\n\n")) #+end_src To use this type \n\n")) +(rrmooc/add-org-template + '("R" "#+begin_src R :results output graphics :file (org-babel-temp-file \"figure\" \".png\") :exports both :width 600 :height 400 :session *R* \n\n#+end_src" "\n\n")) #+end_src To use this type \n\n")) +(rrmooc/add-org-template + '("RR" "#+begin_src R :results output graphics :file (org-babel-temp-file (concat (file-name-directory (or load-file-name buffer-file-name)) \"figure-\") \".png\") :exports both :width 600 :height 400 :session *R* \n\n#+end_src" "\n\n")) #+end_src *** Python code To use this type

\n\n")) +(rrmooc/add-org-template + '("p" "#+begin_src python :results output :exports both\n\n#+end_src" "\n\n")) #+end_src #+begin_src emacs-lisp :tangle init.el -(add-to-list 'org-structure-template-alist - '("P" "#+begin_src python :results output :session :exports both\n\n#+end_src" "\n\n")) +(rrmooc/add-org-template + '("P" "#+begin_src python :results output :session :exports both\n\n#+end_src" "\n\n")) #+end_src #+begin_src emacs-lisp :tangle init.el -(add-to-list 'org-structure-template-alist - '("PP" "#+begin_src python :results file :session :var matplot_lib_filename=(org-babel-temp-file \"figure\" \".png\") :exports both\nimport matplotlib.pyplot as plt\n\nimport numpy\nx=numpy.linspace(-15,15)\nplt.figure(figsize=(10,5))\nplt.plot(x,numpy.cos(x)/x)\nplt.tight_layout()\n\nplt.savefig(matplot_lib_filename)\nmatplot_lib_filename\n#+end_src" "\n\n")) +(rrmooc/add-org-template + '("PP" "#+begin_src python :results file :session :var matplot_lib_filename=(org-babel-temp-file \"figure\" \".png\") :exports both\nimport matplotlib.pyplot as plt\n\nimport numpy\nx=numpy.linspace(-15,15)\nplt.figure(figsize=(10,5))\nplt.plot(x,numpy.cos(x)/x)\nplt.tight_layout()\n\nplt.savefig(matplot_lib_filename)\nmatplot_lib_filename\n#+end_src" "\n\n")) #+end_src *** Bash "sh" code: To use this type \n\n")) - (add-to-list 'org-structure-template-alist - '("b" "#+begin_src shell :results output :exports both\n\n#+end_src" "\n\n"))) + (rrmooc/add-org-template + '("b" "#+begin_src shell :session session :results output :exports both\n\n#+end_src" "\n\n")) + (rrmooc/add-org-template + '("b" "#+begin_src shell :results output :exports both\n\n#+end_src" "\n\n"))) #+end_src To use this type \n\n")) +(rrmooc/add-org-template + '("B" "#+begin_src shell :session *shell* :results output :exports both \n\n#+end_src" "\n\n")) #+end_src *** Graphviz #+begin_src emacs-lisp :tangle init.el -(add-to-list 'org-structure-template-alist - '("g" "#+begin_src dot :results output graphics :file \"/tmp/graph.pdf\" :exports both - digraph G { - node [color=black,fillcolor=white,shape=rectangle,style=filled,fontname=\"Helvetica\"]; - A[label=\"A\"] - B[label=\"B\"] - A->B - }\n#+end_src" "\n\n")) +(rrmooc/add-org-template + '("g" "#+begin_src dot :results output graphics :file \"/tmp/graph.pdf\" :exports both +digraph G { +node [color=black,fillcolor=white,shape=rectangle,style=filled,fontname=\"Helvetica\"]; +A[label=\"A\"] +B[label=\"B\"] +A->B +}\n#+end_src" "\n\n")) #+end_src ** Evaluating whole subtree: :LUKA: #+begin_src emacs-lisp :tangle init.el