From 4b8ceb723e5afc8f3e0035955e37fb7022b448b5 Mon Sep 17 00:00:00 2001 From: Thomas Dehaeze Date: Sun, 22 Mar 2020 13:08:54 +0100 Subject: [PATCH] Reworked to org-babel / matlab configuration Add nice shortcuts, especially the C-S-ret one! --- dotfiles/doom.org | 284 +++++++++++++++++++++++++--------------------- 1 file changed, 152 insertions(+), 132 deletions(-) diff --git a/dotfiles/doom.org b/dotfiles/doom.org index 656b5e0..90e156a 100644 --- a/dotfiles/doom.org +++ b/dotfiles/doom.org @@ -183,6 +183,14 @@ Turn off auto-fill mode that add line breaks. )) #+end_src +** Remap =jump-forward= key binding +#+begin_src emacs-lisp + (with-eval-after-load 'better-jumper + (map! + :desc "Jump Forward" + "C-i" #'better-jumper-jump-forward)) +#+end_src + * Magit #+begin_src emacs-lisp (setenv "GIT_ASKPASS" "git-gui--askpass") @@ -1944,140 +1952,35 @@ Add all named source blocks to =org-babel-library-of-babel=. #+end_src -** Special Shortcuts +** Remap =ctrl-ret= used to execute the src block and go to the next one https://emacs.stackexchange.com/questions/13869/how-to-toggle-org-mode-source-code-block-eval-no-status Remap =ctrl-ret= to execute the source block and go to the next source block -when inside a source block. Otherwise, keep the normal behavior for ctrl-ent. +when inside a source block. Otherwise, keep the normal behavior for =ctrl-ret=. #+begin_src emacs-lisp (defun tdh-ctrl-ret () - (interactive) - (defun in-src-block-p () - "Returns t when the point is inside a source code block" - (string= "src" (org-in-block-p '("src")))) + (interactive) + (defun in-src-block-p () + "Returns t when the point is inside a source code block" + (string= "src" (org-in-block-p '("src")))) - (defun in-src-block-function () - "Function to do when inside the src block" + (if (in-src-block-p) (progn - (org-babel-execute-src-block) - (org-babel-next-src-block))) - - (if (in-src-block-p) - (in-src-block-function) - (+org--insert-item 'below))) + (org-babel-execute-src-block) + (org-babel-next-src-block)) + (+org--insert-item 'below))) +#+end_src +#+begin_src emacs-lisp (map! :after evil-org :map evil-org-mode-map :n "" #'tdh-ctrl-ret) #+end_src +** Remap =ctrl-shift-ret= used to execute the (matlab) src block in the background and go to the next one #+begin_src emacs-lisp - (defun tdh-ctrl-shit-ret () - (interactive) - (defun in-src-block-p () - "Returns t when the point is inside a source code block" - (string= "src" (org-in-block-p '("src")))) - - (if (in-src-block-p) - (tdh-org-babel-execute-goto-next) - (+org/insert-item-above))) - - (map! :after evil-org - :map evil-org-mode-map - :n "" #'tdh-ctrl-shit-ret) -#+end_src - -Remap =C-c C-c=: -https://emacs.stackexchange.com/questions/22430/rebind-org-babel-execute-src-block-maybe-to-c-c-c-c/22530#22530 - -** Org-Babel Matlab -#+begin_src emacs-lisp - (after! org - (setq org-babel-matlab-shell-command "/home/thomas/bin/matlab -nodesktop -nosplash") - - (setq org-babel-matlab-emacs-link-wrapper-method - "%s - if ischar(ans); - echo('test'); - fid = fopen('%s', 'w'); - fprintf(fid, '%s', ans); - fclose(fid); - else; - save -ascii %s ans; - end - delete('%s'); - ") - ) -#+end_src - -** Default options for Matlab code -#+begin_src emacs-lisp - (after! org - (setq org-babel-default-header-args:matlab - '((:results . "none") - (:session . "*MATLAB*") - (:comments . "org") - (:exports . "both") - (:cache . "no") - (:noweb . "no") - (:hlines . "no") - (:tangle . "no") - (:mkdir . "yes") - (:eval . "no-export"))) - ) -#+end_src - -** Indentation -#+begin_src emacs-lisp - (after! org - (setq org-edit-src-content-indentation 2 - org-src-tab-acts-natively nil - org-src-preserve-indentation nil) - ) -#+end_src - -** Some functions for using Matlab with Org Babel -*** =whos= matlab function -#+begin_src emacs-lisp - (defun tdh-matlab-whos (&optional start end) - "Get what is in the Matlab workspace" - (interactive) - (if (use-region-p) - (let ((regionp (buffer-substring (region-beginning) (region-end)))) - (process-send-string "*MATLAB*" (concat "whosEmacs " regionp "\n"))) - (process-send-string "*MATLAB*" (concat "whosEmacs" "\n")))) -#+end_src - -*** =help= matlab function -#+begin_src emacs-lisp - (defun tdh-matlab-help (start end) - "Get help on the selected function" - (interactive "r") - (let ((regionp (buffer-substring start end))) - (process-send-string "*MATLAB*" (concat "help " regionp "\n")))) -#+end_src - -*** Execute selected text -#+begin_src emacs-lisp - (defun tdh-matlab-execute-selected (start end) - "Execute selected text in the *MATLAB* buffer" - (interactive "r") - (let ((regionp (buffer-substring start end))) - (process-send-string "*MATLAB*" regionp))) -#+end_src - -*** Specify a Matlab command to run -#+begin_src emacs-lisp -(defun tdh-matlab-run-command () - "Prompt user to enter a matlab command" - (interactive) - (process-send-string "*MATLAB*" (concat (read-string "Matlab Command: ") "\n"))) -#+end_src - -*** Execute the current source block with no output -#+begin_src emacs-lisp - (defun tdh-org-babel-execute (&optional arg info params) + (defun tdh-org-babel-execute-matlab-background (&optional arg info params) (interactive) (let* ((org-babel-current-src-block-location (or org-babel-current-src-block-location @@ -2131,7 +2034,6 @@ https://emacs.stackexchange.com/questions/22430/rebind-org-babel-execute-src-blo d)))) (cmd (intern (concat "org-babel-execute:" lang))) result) - ;; (message "%s" body) (process-send-string "*MATLAB*" (concat body "\n")) result)) ))) @@ -2139,16 +2041,133 @@ https://emacs.stackexchange.com/questions/22430/rebind-org-babel-execute-src-blo ) #+end_src -*** Execute the source block and go to the next one +This function: +- first check if inside a source block, if not does nothing +- when check if the language is =matlab= + - if it is not, it just runs the code and go to the next source block + - if it is in a =matlab= block, it first check if a region if selected, if so it just runs the selected region. + if no region is selected, it runs all the code blocks and goes to the next block + #+begin_src emacs-lisp - (defun tdh-org-babel-execute-goto-next (&optional arg info params) + (defun tdh-ctrl-shit-ret () (interactive) - (tdh-org-babel-execute arg info params) - (org-babel-next-src-block) + (defun in-src-block-p () + "Returns t when the point is inside a source code block" + (string= "src" (org-in-block-p '("src")))) + + (if (in-src-block-p) + (let ((lang (nth 0 (org-babel-get-src-block-info)))) + (if (string= lang "matlab") + (if (region-active-p) + (tdh-matlab-execute-selected (region-beginning) (region-end)) + (progn (tdh-org-babel-execute-matlab-background) + (org-babel-next-src-block))) + (tdh-ctrl-ret)) + ) + ) ) #+end_src -*** Org-Babel Tangle Subtree +#+begin_src emacs-lisp + (map! :after evil-org + :map evil-org-mode-map + :n "" #'tdh-ctrl-shit-ret) +#+end_src + +** Org-Babel Matlab +#+begin_src emacs-lisp + (after! org + (setq org-babel-matlab-shell-command "/home/thomas/bin/matlab -nodesktop -nosplash") + + (setq org-babel-matlab-emacs-link-wrapper-method + "%s + if ischar(ans); + echo('test'); + fid = fopen('%s', 'w'); + fprintf(fid, '%s', ans); + fclose(fid); + else; + save -ascii %s ans; + end + delete('%s'); + ") + ) +#+end_src + +** Default options for Matlab code +#+begin_src emacs-lisp + (after! org + (setq org-babel-default-header-args:matlab + '((:results . "none") + (:session . "*MATLAB*") + (:comments . "org") + (:exports . "both") + (:cache . "no") + (:noweb . "no") + (:hlines . "no") + (:tangle . "no") + (:mkdir . "yes") + (:eval . "no-export"))) + ) +#+end_src + +** Indentation +#+begin_src emacs-lisp + (after! org + (setq org-edit-src-content-indentation 2 + org-src-tab-acts-natively nil + org-src-preserve-indentation nil) + ) +#+end_src + +** Some functions for using Matlab with Org Babel +*** =whos= matlab function +#+begin_src emacs-lisp + (defun tdh-matlab-whos (&optional start end) + "Get what is in the Matlab workspace" + (interactive) + (if (use-region-p) + (let ((regionp (buffer-substring (region-beginning) (region-end)))) + (process-send-string "*MATLAB*" (concat "whosEmacs " regionp "\n"))) + (process-send-string "*MATLAB*" (concat "whosEmacs" "\n")))) +#+end_src + +*** =help= matlab function +#+begin_src emacs-lisp + (defun tdh-matlab-help (start end) + "Get help on the selected function" + (interactive "r") + (let ((regionp (buffer-substring start end))) + (process-send-string "*MATLAB*" (concat "help " regionp "\n")))) +#+end_src + +*** Execute selected text +#+begin_src emacs-lisp + (defun tdh-matlab-execute-selected (start end) + "Execute selected text in the *MATLAB* buffer" + (interactive "r") + (let ((regionp (buffer-substring start end))) + (process-send-string "*MATLAB*" (concat regionp "\n")))) +#+end_src + +*** Specify a Matlab command to run +#+begin_src emacs-lisp +(defun tdh-matlab-run-command () + "Prompt user to enter a matlab command" + (interactive) + (process-send-string "*MATLAB*" (concat (read-string "Matlab Command: ") "\n"))) +#+end_src + +*** Specify a Matlab command to run and show output in minibuffer +#+begin_src emacs-lisp + (defun tdh-matlab-run-command-show-output () + "Prompt user to enter a matlab command" + (interactive) + (process-send-string "*MATLAB*" (concat "evalEmacs('" (read-string "Matlab Command: ") "')\n"))) +#+end_src + +** Helping Functions - Tangling +*** Org-Babel Tangle Sub-tree #+begin_src emacs-lisp (defun tdh-org-babel-tangle-subtree () "Tangle the current subtree" @@ -2158,9 +2177,6 @@ https://emacs.stackexchange.com/questions/22430/rebind-org-babel-execute-src-blo (org-babel-tangle) (widen)) ) - - (after! org - (map! :map org-mode-map :n ",bT" 'tdh-org-babel-tangle-subtree)) #+end_src *** Org-Babel Jump to Tangle File @@ -2180,9 +2196,6 @@ Actually this tangle the file and then go to the file. Maybe I would like to ign (if (file-readable-p file) (find-file file) (error "Cannot open tangle file %S" file))))) - - (after! org - (map! :map org-mode-map :n ",bF" 'tdh-org-babel-jump-to-tangle-file)) #+end_src *** Org-Babel Tangle File and Execute @@ -2202,6 +2215,13 @@ Actually this tangle the file and then go to the file. Maybe I would like to ign (error "Cannot open tangle file %S" file))))) #+end_src +*** Map Functions +#+begin_src emacs-lisp + (after! org + (map! :map org-mode-map :n ",bF" 'tdh-org-babel-jump-to-tangle-file) + (map! :map org-mode-map :n ",bT" 'tdh-org-babel-tangle-subtree)) +#+end_src + * LaTeX - https://tex.stackexchange.com/questions/52179/what-is-your-favorite-emacs-and-or-auctex-command-trick - https://tex.stackexchange.com/questions/20843/useful-shortcuts-or-key-bindings-or-predefined-commands-for-emacsauctex @@ -2226,7 +2246,7 @@ Actually this tangle the file and then go to the file. Maybe I would like to ign ** Face Attributes #+begin_src emacs-lisp (with-eval-after-load 'font-latex - (set-face-attribute 'font-latex-math-face nil :foreground (face-foreground 'default)) + (set-face-attribute 'font-latex-math-face nil :foreground (face-foreground 'org-meta-line)) ) #+end_src