Reworked to org-babel / matlab configuration

Add nice shortcuts, especially the C-S-ret one!
This commit is contained in:
Thomas Dehaeze 2020-03-22 13:08:54 +01:00
parent 5db4def7f0
commit 4b8ceb723e

View File

@ -183,6 +183,14 @@ Turn off auto-fill mode that add line breaks.
)) ))
#+end_src #+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 * Magit
#+begin_src emacs-lisp #+begin_src emacs-lisp
(setenv "GIT_ASKPASS" "git-gui--askpass") (setenv "GIT_ASKPASS" "git-gui--askpass")
@ -1944,140 +1952,35 @@ Add all named source blocks to =org-babel-library-of-babel=.
#+end_src #+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 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 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 #+begin_src emacs-lisp
(defun tdh-ctrl-ret () (defun tdh-ctrl-ret ()
(interactive) (interactive)
(defun in-src-block-p () (defun in-src-block-p ()
"Returns t when the point is inside a source code block" "Returns t when the point is inside a source code block"
(string= "src" (org-in-block-p '("src")))) (string= "src" (org-in-block-p '("src"))))
(defun in-src-block-function () (if (in-src-block-p)
"Function to do when inside the src block"
(progn (progn
(org-babel-execute-src-block) (org-babel-execute-src-block)
(org-babel-next-src-block))) (org-babel-next-src-block))
(+org--insert-item 'below)))
(if (in-src-block-p) #+end_src
(in-src-block-function)
(+org--insert-item 'below)))
#+begin_src emacs-lisp
(map! :after evil-org (map! :after evil-org
:map evil-org-mode-map :map evil-org-mode-map
:n "<C-return>" #'tdh-ctrl-ret) :n "<C-return>" #'tdh-ctrl-ret)
#+end_src #+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 #+begin_src emacs-lisp
(defun tdh-ctrl-shit-ret () (defun tdh-org-babel-execute-matlab-background (&optional arg info params)
(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 "<C-S-return>" #'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)
(interactive) (interactive)
(let* ((org-babel-current-src-block-location (let* ((org-babel-current-src-block-location
(or 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)))) d))))
(cmd (intern (concat "org-babel-execute:" lang))) (cmd (intern (concat "org-babel-execute:" lang)))
result) result)
;; (message "%s" body)
(process-send-string "*MATLAB*" (concat body "\n")) (process-send-string "*MATLAB*" (concat body "\n"))
result)) result))
))) )))
@ -2139,16 +2041,133 @@ https://emacs.stackexchange.com/questions/22430/rebind-org-babel-execute-src-blo
) )
#+end_src #+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 #+begin_src emacs-lisp
(defun tdh-org-babel-execute-goto-next (&optional arg info params) (defun tdh-ctrl-shit-ret ()
(interactive) (interactive)
(tdh-org-babel-execute arg info params) (defun in-src-block-p ()
(org-babel-next-src-block) "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 #+end_src
*** Org-Babel Tangle Subtree #+begin_src emacs-lisp
(map! :after evil-org
:map evil-org-mode-map
:n "<C-S-return>" #'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 #+begin_src emacs-lisp
(defun tdh-org-babel-tangle-subtree () (defun tdh-org-babel-tangle-subtree ()
"Tangle the current subtree" "Tangle the current subtree"
@ -2158,9 +2177,6 @@ https://emacs.stackexchange.com/questions/22430/rebind-org-babel-execute-src-blo
(org-babel-tangle) (org-babel-tangle)
(widen)) (widen))
) )
(after! org
(map! :map org-mode-map :n ",bT" 'tdh-org-babel-tangle-subtree))
#+end_src #+end_src
*** Org-Babel Jump to Tangle File *** 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) (if (file-readable-p file)
(find-file file) (find-file file)
(error "Cannot open tangle file %S" 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 #+end_src
*** Org-Babel Tangle File and Execute *** 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))))) (error "Cannot open tangle file %S" file)))))
#+end_src #+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 * LaTeX
- https://tex.stackexchange.com/questions/52179/what-is-your-favorite-emacs-and-or-auctex-command-trick - 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 - 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 ** Face Attributes
#+begin_src emacs-lisp #+begin_src emacs-lisp
(with-eval-after-load 'font-latex (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 #+end_src