A series of notes that I learn Emacs hacking. Cask and pallet package for multi-version Emacs collaboration; Macro in emacs-lisp; use-package package introduction.
Prologue: Not too much for day 8. Previous video is 1 hour’s long, the author decrease the length of each video to 30 min for better performance. Therefore I put the content in two days’ video into single post.
There are not too many in this video.
Visit Melpa from China
cask, a package for project monitoring
- Clone from the Github repo, and install. Find more from the official site.
- If we have multiple Emacs installed, we can employ
caskto make them work simutaneously.
- If there is only one Emacs on the compter, there you are good to go.
- A package management system based on
M-x pallet-init. A
caskfile will be generated in the
(cask-initialize): a similar code snippet as
- Will help to synchronize packages between different version of Emcas.
- Add the following code before any of the package are required.
(require 'cask "~/.cask/cask.el") (cask-initialize) (require 'pallet) (pallet-mode t)
cask installin the terminal to merge the package directory.
Now we can get rid of the previous package initialization code.
- Now we can have an idea on what key is pressed in history.
- We can consider it as “code that generates code”.
- Analog as macro in C or template in C++.
- See an example below
(defmacro inc (var) (list 'setq var (list '1+ var)))
- Usage of the example
(setq my-var 1) (inc my-var) (macroexpand '(inc my-var))
- Write macro is similar as writing function in elisp.
- However, to achieve similar result we can use
defun, why bother? What’s the difference between function and macro?
- Evaluation: the macro arguments are the actual expressions appearing in the macro call. For functions, the value in the argument is first fetched or calculated then the function is executed. For macro, the expression is first expanded and then executed.
- Expansion: the value returned by the macro body is an alternate Lisp expresion, aka the expansion of the macro.
- Backquote matters. Emacs will automatically insert two backquotes all together. Use the following code to disable it.
(sp-local-pair 'emacs-lisp-mode "`" nil :actions nil)
,. It means we do not execute the program after the quote. However, the expression (program) after
,will be execute first, and then pass as argument.
ppto pretty print your
- Take a look at another example
(defun my-print (number) (message "%d" number)) (my-print 2) (my-print (+ 2 3)) (defmacro my-print-2 (number) `(message "%d" ,number)) (my-print-2 2) (my-print-2 (+ 2 3)) (defmacro inc (var) (list 'setq var (list '1+ var))) (setq my-var-2) (inc my-var) (defmacro inc2 (var1 var2 ) (list 'progn (list 'inc var1) (list 'inc var2))) (macroexpand '(inc2 my-var my-var)) ;; this will expand only one layer. (macroexpand-all '(inc2 my-var my-var))
- All the stuff can be found on its official page.
(use-package xxx): a better solution to substitute
(require 'xxx). If the ‘xxx’ is not in
elpa, we will encounter an error if we use
use-packagecan help you load packages safer.
- We can merge similar configuration together by using
:config. The difference is that,
:initwill be executed before requiring the package, while
:configwill be executed after loading the pacakge, when we can add more configuration to the package.
(use-package xxx :init (progn (setq var1 1) (setq var2 "s")) :config (setq var3 5))
requirewill load everything, which is too slow. We can use
autoloadinstead. Now we can integrate
:commandto do so.
:deferis equal to
(use-package xxx :commands (global-company-mode) :defer t
- Key binding:
(use-package color-moccur :commands (isearch-moccur iseach-all) :bind (("M-s O" . moccur) :map isearch-mode-map ("M-o" . isearch-moccur) ("M-O" . isearch-moccur-all)) :init (setq isearch-lazy-highlight t) :config (use-package moccur-edit) )
- In a word, it makes your code neat and robust.
- Spacemacs use