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
** 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,11 +1952,11 @@ 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 ()
@ -1957,21 +1965,89 @@ when inside a source block. Otherwise, keep the normal behavior for ctrl-ent.
"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-babel-next-src-block))
(+org--insert-item 'below)))
#+end_src
#+begin_src emacs-lisp
(map! :after evil-org
:map evil-org-mode-map
:n "<C-return>" #'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-org-babel-execute-matlab-background (&optional arg info params)
(interactive)
(let* ((org-babel-current-src-block-location
(or org-babel-current-src-block-location
(nth 5 info)
(org-babel-where-is-src-block-head)))
(info (if info (copy-tree info) (org-babel-get-src-block-info))))
;; Merge PARAMS with INFO before considering source block
;; evaluation since both could disagree.
(cl-callf org-babel-merge-params (nth 2 info) params)
(when (org-babel-check-evaluate info)
(cl-callf org-babel-process-params (nth 2 info))
(let* ((params (nth 2 info))
(cache (let ((c (cdr (assq :cache params))))
(and (not arg) c (string= "yes" c))))
(new-hash (and cache (org-babel-sha1-hash info :eval)))
(old-hash (and cache (org-babel-current-result-hash)))
(current-cache (and new-hash (equal new-hash old-hash))))
(cond
(current-cache
(save-excursion ;Return cached result.
(goto-char (org-babel-where-is-src-block-result nil info))
(forward-line)
(skip-chars-forward " \t")
(let ((result (org-babel-read-result)))
(message (replace-regexp-in-string "%" "%%" (format "%S" result)))
result)))
((org-babel-confirm-evaluate info)
(let* ((lang (nth 0 info))
(result-params (cdr (assq :result-params params)))
;; Expand noweb references in BODY and remove any
;; coderef.
(body
(let ((coderef (nth 6 info))
(expand
(if (org-babel-noweb-p params :eval)
(org-babel-expand-noweb-references info)
(nth 1 info))))
(if (not coderef) expand
(replace-regexp-in-string
(org-src-coderef-regexp coderef) "" expand nil nil 1))))
(dir (cdr (assq :dir params)))
(mkdirp (cdr (assq :mkdirp params)))
(default-directory
(cond
((not dir) default-directory)
((member mkdirp '("no" "nil" nil))
(file-name-as-directory (expand-file-name dir)))
(t
(let ((d (file-name-as-directory (expand-file-name dir))))
(make-directory d 'parents)
d))))
(cmd (intern (concat "org-babel-execute:" lang)))
result)
(process-send-string "*MATLAB*" (concat body "\n"))
result))
)))
)
)
#+end_src
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-ctrl-shit-ret ()
(interactive)
@ -1980,17 +2056,24 @@ when inside a source block. Otherwise, keep the normal behavior for ctrl-ent.
(string= "src" (org-in-block-p '("src"))))
(if (in-src-block-p)
(tdh-org-babel-execute-goto-next)
(+org/insert-item-above)))
(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
#+begin_src emacs-lisp
(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
@ -2064,7 +2147,7 @@ https://emacs.stackexchange.com/questions/22430/rebind-org-babel-execute-src-blo
"Execute selected text in the *MATLAB* buffer"
(interactive "r")
(let ((regionp (buffer-substring start end)))
(process-send-string "*MATLAB*" regionp)))
(process-send-string "*MATLAB*" (concat regionp "\n"))))
#+end_src
*** Specify a Matlab command to run
@ -2075,80 +2158,16 @@ https://emacs.stackexchange.com/questions/22430/rebind-org-babel-execute-src-blo
(process-send-string "*MATLAB*" (concat (read-string "Matlab Command: ") "\n")))
#+end_src
*** Execute the current source block with no output
*** Specify a Matlab command to run and show output in minibuffer
#+begin_src emacs-lisp
(defun tdh-org-babel-execute (&optional arg info params)
(defun tdh-matlab-run-command-show-output ()
"Prompt user to enter a matlab command"
(interactive)
(let* ((org-babel-current-src-block-location
(or org-babel-current-src-block-location
(nth 5 info)
(org-babel-where-is-src-block-head)))
(info (if info (copy-tree info) (org-babel-get-src-block-info))))
;; Merge PARAMS with INFO before considering source block
;; evaluation since both could disagree.
(cl-callf org-babel-merge-params (nth 2 info) params)
(when (org-babel-check-evaluate info)
(cl-callf org-babel-process-params (nth 2 info))
(let* ((params (nth 2 info))
(cache (let ((c (cdr (assq :cache params))))
(and (not arg) c (string= "yes" c))))
(new-hash (and cache (org-babel-sha1-hash info :eval)))
(old-hash (and cache (org-babel-current-result-hash)))
(current-cache (and new-hash (equal new-hash old-hash))))
(cond
(current-cache
(save-excursion ;Return cached result.
(goto-char (org-babel-where-is-src-block-result nil info))
(forward-line)
(skip-chars-forward " \t")
(let ((result (org-babel-read-result)))
(message (replace-regexp-in-string "%" "%%" (format "%S" result)))
result)))
((org-babel-confirm-evaluate info)
(let* ((lang (nth 0 info))
(result-params (cdr (assq :result-params params)))
;; Expand noweb references in BODY and remove any
;; coderef.
(body
(let ((coderef (nth 6 info))
(expand
(if (org-babel-noweb-p params :eval)
(org-babel-expand-noweb-references info)
(nth 1 info))))
(if (not coderef) expand
(replace-regexp-in-string
(org-src-coderef-regexp coderef) "" expand nil nil 1))))
(dir (cdr (assq :dir params)))
(mkdirp (cdr (assq :mkdirp params)))
(default-directory
(cond
((not dir) default-directory)
((member mkdirp '("no" "nil" nil))
(file-name-as-directory (expand-file-name dir)))
(t
(let ((d (file-name-as-directory (expand-file-name dir))))
(make-directory d 'parents)
d))))
(cmd (intern (concat "org-babel-execute:" lang)))
result)
;; (message "%s" body)
(process-send-string "*MATLAB*" (concat body "\n"))
result))
)))
)
)
(process-send-string "*MATLAB*" (concat "evalEmacs('" (read-string "Matlab Command: ") "')\n")))
#+end_src
*** Execute the source block and go to the next one
#+begin_src emacs-lisp
(defun tdh-org-babel-execute-goto-next (&optional arg info params)
(interactive)
(tdh-org-babel-execute arg info params)
(org-babel-next-src-block)
)
#+end_src
*** Org-Babel Tangle Subtree
** 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