From c29511a3cc3f7832b6c1add8b08748d3fa01250c Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Mon, 12 Sep 2022 15:24:31 +0100 Subject: [PATCH] Finish up ep 10 on macros --- core/fg42/cube.el | 8 ++++---- core/fg42/flags.el | 24 +++++++++++++----------- docs/videos.org | 32 +++++++++++++++++++++++++++++--- scripts/install_files.sh | 1 + 4 files changed, 47 insertions(+), 18 deletions(-) diff --git a/core/fg42/cube.el b/core/fg42/cube.el index 57976f8..049962c 100644 --- a/core/fg42/cube.el +++ b/core/fg42/cube.el @@ -29,12 +29,12 @@ (require 'fg42/utils) (defvar fg42/after-cubes-setup-hook nil - "A hook that will be run after all the active cubes got setup. This hook -is dedicated for the codes that needs to do stuff based on other cubes -presence. With this hook we eliminate the need for cube ordering. + "A hook that will be run after all the active cubes got setup. +This hook is dedicated for the codes that needs to do stuff based on other cubes +presence. With this hook we eliminate the need for cube ordering. It will be called in the `fg42-config' and the proper way to use -it is to use `fg42/after-cubes' macro. ") +it is to use `fg42/after-cubes' macro.") (defvar fg42/available-cubes '() diff --git a/core/fg42/flags.el b/core/fg42/flags.el index d9813aa..7caf4a2 100644 --- a/core/fg42/flags.el +++ b/core/fg42/flags.el @@ -90,9 +90,12 @@ For example, `(fg42/merge-flags (list f1 f2 f3) f4 -f2)' will return `(f1 f3 f4) (defmacro defflag (flag-name docstring &optional default-value) "Define a new flag FLAG-NAME with the given DOCSTRING. If the DEFAULT-VALUE is a non nil value, then the flag will be -added to the activa flags list." - (let ((var-name (intern (format "fg42/-flag-%s" flag-name))) - (set-default (if default-value `((add-to-list 'fg42/flags ',flag-name)) ()))) +added to the active flags list." + (declare (doc-string 2) (indent defun)) + (let ((var-name (intern (format "fg42/-flag-%s" (symbol-name flag-name)))) + (set-default (when default-value + `((add-to-list 'fg42/flags ',flag-name))))) + `(if (boundp ',var-name) (warn (format "Flag name `%s' already defined" ,(format "%s" flag-name))) (progn @@ -106,12 +109,11 @@ added to the activa flags list." (declare (indent defun)) ;; The `cube-local-flags' variable here is going to be ;; defined in cubes to hold the local flags for each cube - (if (and (boundp 'cube-local-flags) - (member flag cube-local-flags)) - `,@body - `(if (member ',flag fg42/flags) - ,@body - nil))) + (if (not (and (boundp 'cube-local-flags) + (member flag cube-local-flags))) + `(progn ,@body) + `(when (member ',flag fg42/flags) + ,@body))) (defmacro if-flag (flag then else) @@ -121,8 +123,8 @@ added to the activa flags list." ;; defined in cubes to hold the local flags for each cube (if (and (boundp 'cube-local-flags) (member flag cube-local-flags)) - `,@then - `(if (member ',flag fg42/flags) + then + `(if (member ',flag fg42/flags) ,then ,else))) diff --git a/docs/videos.org b/docs/videos.org index 92a193e..dbf2b65 100644 --- a/docs/videos.org +++ b/docs/videos.org @@ -556,7 +556,8 @@ A collection of handy functions that operate on ~sequences~. #+END_SRC - ~seq-min~, ~seq-max~ -* Episode 9 - Introduction to Macros +* DONE Episode 9 - Introduction to Macros +CLOSED: [2022-09-11 Sun 11:08] ** What is a macro? - A macro is defined much like a function - It works on compile time @@ -598,7 +599,32 @@ A collection of handy functions that operate on ~sequences~. - [[https://twitter.com/lxsameer][@lxsameer]] on Twitter * Episode 10 - More on Macros -** Quasiquotes aka back quote +** Quasiquote aka backquote (`) +- Backquote constructs allow us to quote an expression, but selectively evaluate sub-expressions +- Similar to a template engine for lisp expressions +- It can be used on its own but it will shine in macro definitions +- On the simplest form it is identical to ~quote~ +- Special markers ~,~ and ~,@~ can be used to mark expressions for evaluation +- ~,~ will replace the return value of the evaluation with the expression +- ~,@~ will replace and splice the return value of the evaluation +*** Examples + +#+BEGIN_SRC emacs-lisp + `3 + `(1 2 3 4) + `(1 2 3 ,(+ 5 6)) + `(1 2 3 ,(list 4 5 6)) + `(1 2 3 ,@(list 4 5 6)) + + `(setq ,(intern (upcase "some_var")) 3) +#+END_SRC + ** ~declare~ form +- It's a special macro which can be used to add meta properties to a function or macro + + ~doc-string~ + + ~indent~ + +** Real Examples +Let's discuss ~defflag~ and ~when-flag~ macros in [[file:../core/fg42/flags.el]] + * Episode 11 - Common pitfalls of Macros -* diff --git a/scripts/install_files.sh b/scripts/install_files.sh index 28d5187..2d9faa2 100644 --- a/scripts/install_files.sh +++ b/scripts/install_files.sh @@ -75,6 +75,7 @@ install_runners() { info "Copying share files..." sudo mkdir -p /usr/share/fg42/ + sudo mkdir -p /usr/local/share/applications sudo cp $fg42_home/share/applications/fg42.desktop /usr/local/share/applications sudo cp -r $fg42_home/share/icons/hicolor/ /usr/local/share/icons sudo cp -r $fg42_home/share/* /usr/share/fg42/