Compare commits

...

2 commits

Author SHA1 Message Date
Howard Abrams
36ea4b904e Appendix for Yasnippets 2024-08-04 22:11:00 -07:00
Howard Abrams
accf51fcc1 Editing blocks section 2024-08-04 22:10:44 -07:00

View file

@ -169,6 +169,30 @@ Note: If you are reading this in an Emacs buffer, you can also place your cursor
At this point, you can begin a line with =<s= and hit ~TAB~ to have a src block expanded, with the cursor left at the end of first line, allowing you to type =emacs-lisp=.
This is Emacs, you probably have your favorite template expansion, like [[https://elpa.gnu.org/packages/doc/tempel/tempel.html][TempEL]] or [[https://www.emacswiki.org/emacs/Yasnippet][Yasnippet]], as any system that can generate your text works fine.The magic isnt hidden in markers, but shines plainly in the text itself.
** Editing src Blocks
I find editing prose in an Org file quite nice…editing code? Not so much. Many techniques you expect to use, like jumping to code definitions with the [[info:emacs#Looking Up Identifiers][Xref system]] or manipulating s-expressions in a Lisp with =paredit= or =smart-parens=, arent available.
With your cursor anywhere, the header line, anywhere in the body or one the =end_src= line, type ~C-c '~ to narrow to contents of that block with a mode based on the language on the header line.
Whew, now you can edit your code in a manner you expect. Type ~C-c '~ again to save and return to your full org buffer, or ~C-c C-k~ to cancel your changes.
Lets add more [[info:org#Languages][languages]] to what is available, by running the following code:
#+begin_src emacs-lisp
(org-babel-do-load-languages
'org-babel-load-languages
'((emacs-lisp . t)
(python . t)))
#+end_src
Feel free to substitute the =python=, for your favorite language, or add more. Use =sh= for shell scripts, and =js= for Javascript. See [[https://orgmode.org/worg/org-contrib/babel/languages/index.html][Babel]] for details on languages that can be supported.
*Note:* If you dont see you languages pretty colors for its syntax, run this code (and put it in your init file):
#+begin_src emacs-lisp
(setq org-src-fontify-natively t)
#+end_src
* Working with Python
* Calling out to the Shell
@ -177,3 +201,50 @@ Can we do both Bash, Fish and Powershell?
** Graphviz
** PlantUML
** Pikchr
* Tips and Tricks
This final chapter contains optional information you might find useful.
Oh, and please let me know if you have a tip or trick that you think should be included.
** Yasnippet Templates
If you use [[https://www.emacswiki.org/emacs/Yasnippet][Yasnippet]], and include the [[https://github.com/AndreaCrotti/yasnippet-snippets][yasnippet-snippets project]], you can type =<s= in an Org buffer to expand into a src block, and type =def= in an Emacs Lisp buffer to get a function definition, but why not have both?
In an Org file, type ~M-x yas-new-snippet~ and replace the default text with:
#+begin_example
# key: #slf
# name: emacs-lisp-defun
# --
,#+BEGIN_SRC emacs-lisp
(defun ${1:fun} (${2:args})
"${3:docstring}"
${4:(interactive${5: "${6:P}"})}
$0)
,#+END_SRC
#+end_example
Type ~C-c C-c~ to save and install the snippet with Org.
Lets make one for variables:
#+begin_example
# key: slv
# name: emacs-lisp-defvar
# --
,#+BEGIN_SRC emacs-lisp
(defvar ${1:symbol} ${2:initvalue} "${3:docstring}")
,#+END_SRC
#+end_example
And maybe another one for defining unit tests?
#+begin_example
# -*- mode: snippet -*-
# name: ert-deftest
# key: edt
# --
,#+BEGIN_SRC emacs-lisp :tangle no
(ert-deftest $1-test ()
(should (= $0)))
,#+END_SRC
#+end_example