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 <lxsameer@gnu.org>
This commit is contained in:
Sameer Rahmani 2020-03-12 18:45:38 +00:00
parent 713468a5be
commit 950575a38f
2 changed files with 43 additions and 1 deletions

View File

@ -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

View File

@ -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