SpaceEmacs Rock Day9 学习笔记
SpaceEmacs Rock Day9 学习笔记
Table of Contents
1 SpaceEmacs Rock Day9 学习笔记
Topic: Macro and the-package
1.1 What is macro?
Code which generate code?
(defmacro inc (var) (list 'setq var (list '1+ var))) (setq my-var 1) (setq my-var (+ 1 my-var))
See the macro expand
(setq my-var 1)
(macroexpand '(inc my-var))
Write macro is almost the same as writing function in elisp.
2 What's the different between function and macro?
- Evaluation: the macro arguments are the actual expressions apperaring in the macro call.
- Expansion: the value returned by the macro body is an alternate Lisp expression, also known as
2.1 back quote matters
(with-eval-after-load 'smartparens (sp-local-pair 'emacs-lisp-mode "`" nil :actions nil))
example,quote and back quote
(defun my-print (number) (message "This is a number: %d" number)) (my-print 2) (my-print (+ 2 3)) ;; return a expression (quote (+ 1 1)) (defmacro my-print-2 (number) `(message "This is a number: %d", number)) (my-print-2 2) (my-print-2 (+ 2 3)) (defmacro inc2 (var1 var2) (list 'progn (list 'inc var1) (list 'inc var2))) (macroexpand '(inc2 my-var my-var)) (macroexpand-all '(inc2 my-var my-var))
3 Why Macro?
3.1 A more useful example of Elisp macro
(defun prelude-search (query-url prompt) "Open the search url constructed with the QUERY URL, PROMPT set the `read-string prompt." (browse-url (concat query-url (url-hexify-string (if mark-active (buffer-substring (region-beginning) (region-end)) (read-string prompt)))))) (defmacro prelude-install-search-engine (search-engine-name search-engine-url search-engine-prompt) "Given some information regarding a search engine, install the interactive command to search through them" `(defun ,(intern (format "prelude-%s" search-engine-name)) () ,(format "Search %s with a query or region if any." search-engine-name) (interactive) (prelude-search ,search-engine-url, search-engine-prompt))) (prelude-install-search-engine "google" "http://google.com/seearch?q=" "Google: ") (prelude-install-search-engine "youtube" "http://youtube.com/results?search_query=" "Search YouTube: ") (prelude-install-search-engine "github" "https://github.com/search?q=" "Search GitHub:") (prelude-install-search-engine "duckduckgo" "https://duckduckgo.com/?t=lm&q=" "Search Duckduckgo: ") (macroexpand '(prelude-install-search-engine "google" "http://google.com/seearch?q=" "Google: "))
4 Use package and basic usage
- a more safe require
- group config into one place
- autoload and bind keys easily
- make your config local faster
- and more? SpaceEmacs use it a lot
(use-package color-moccur :commands (isearch-moccur isearch-all) :bind (("M-s O" . moccur) :map isearch-mode-map ("M-s o" . isearch-moccur) ("M-s o" . isearch-moccur-all)) :init (setq isearch-lazy-highlight t) :config (use-package moccur-edit))
5 brain Training
(defmacro defsynonym (old-name new-name) `(defmacro ,new-name (&rest args) `(, ',old-name ,@args)))
more discussion: https://emacs-china.org/t/lisp/357
6 Reference
Readme more about macro: https://www.gnu.org/software/emacs/manual/html_node/elisp/Macros.html