From 950575a38f9a69e96818c4fbeeadc48acb6a6262 Mon Sep 17 00:00:00 2001 From: Sameer Rahmani Date: Thu, 12 Mar 2020 18:45:38 +0000 Subject: [PATCH] defextension has been added alongside with load-extension function. `defextension` is a new macro to replace the old `extension` macro to define a new extension. `load-extension` function take an extension which is defined using `defextension` and load it by loading the file addressed in extension's `:path` field or if that field i `nil` simply try to construct a path in FG42's extension directory. Signed-off-by: Sameer Rahmani --- lib/fg42/extension.el | 37 ++++++++++++++++++++++++++++++++++++- lib/fg42/utils.el | 7 +++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/lib/fg42/extension.el b/lib/fg42/extension.el index c54ac92..7811156 100644 --- a/lib/fg42/extension.el +++ b/lib/fg42/extension.el @@ -28,6 +28,7 @@ ;; This library provides some basic means to create a new FG42 extensions (require 'cl-lib) +(require 'fg42/utils) ;; Variables ----------------------------- (defvar activated-extensions () @@ -56,7 +57,7 @@ ;; An instance of fg42-actions structure that describe the ;; different actions of the given extension (actions nil) - + (path nil) ;; Callbacks (on-initialize nil) (on-load) @@ -74,6 +75,21 @@ (puthash abl t disabled-abilities))) +(defun extension-path (extension) + "Return the path to the given EXTENSION." + (or (fg42-extension-path extension) + ;; TODO: should we extract variables such as `fg42-home' to their + ;; dedicated ns in order to avoid the warning ? + (concat fg42-home "lib/extensions/" (fg42-extension-name extension)))) + + +(defun load-extension (ext) + "Load the given EXT which is a `fg42-extension' instance." + (load-file (extension-path ext)) + (when-let ((init-fn (eval (fg42-extension-on-initialize ext)))) + (apply (symbol-function init-fn) '(ext)))) + + ;; Macros --------------------------------- (defmacro ability (name deps &rest body) "Define an ability with the given NAME, DEPS, and BODY. @@ -94,6 +110,16 @@ to them. `(setq ,name (apply 'make-fg42-extension :name ,(symbol-name name) (quote ,args)))) +(defmacro defextension (name &optional docstring &rest args) + "A simple DSL to define new fg42 extension by given NAME, DOCSTRING and ARGS." + (declare (doc-string 2) (indent 1)) + ;; TODO: Inject the docstring to the current `system' in order + ;; to collect it later for `describe-extension' function. + `(defvar name (apply 'make-fg42-extension + :name ,(symbol-name name) + (quote ,args)))) + + (defmacro with-ability (name &rest body) "If the ability with the given NAME is not disabled, Run the BODY." `(when (active-ability? (intern ,(symbol-name name))) @@ -112,5 +138,14 @@ to them. (org-mode))) +(comment + (defextension example-extension + "Very simple extention as a test" + :path "~/example-extension.el" + :on-initialize 'example-extention-init) + + (load-extension example-extension)) + + (provide 'fg42/extension) ;;; extension ends here diff --git a/lib/fg42/utils.el b/lib/fg42/utils.el index c28c165..f4a4d54 100644 --- a/lib/fg42/utils.el +++ b/lib/fg42/utils.el @@ -69,5 +69,12 @@ with is the buffer." "Apply the given FACE-SYMBOL to the given TEXT." (put-text-property 0 (length text) 'face face-symbol text)) + +(defmacro comment (&rest body) + "A macro similar to Clojure's comment macro that ignore the BODY." + (declare (indent 1)) + `nil) + + (provide 'fg42/utils) ;;; utils.el ends here