Olical / aniseed

Neovim configuration and plugins in Fennel (Lisp compiled to Lua)
https://discord.gg/wXAMr8F
The Unlicense
606 stars 28 forks source link

Is it possible to get the current module name? #69

Closed gwerbin closed 2 years ago

gwerbin commented 2 years ago

I see that there is an aniseed/module entry in the table created by the module macro. Is this value accessible from inside the module?

For example:

(module foo.bar)

(defn print-module-name []
  "Prints the name of the currently loading module."
  ; Hypothetical variable name: _currentmodule_
  (print _currentmodule_))

In this case, I would expect foo.bar.print-module-name to print foo.bar.

My use case is to facilitate scripting Vim:

(module hello)

(defn hello []
  (vim.api.nvim_echo "Hello!!"))

(defn vimeval [source ...]
  "Kind of like luaeval(). Consider using %q, not %s, for strings."
  (vim.api.nvim-exec (string.format source ...)) true)

(defn setup []
  (vimeval
    "augroup Hello
       autocmd BufNewFile * lua require(%q).hello()
     augroup END"
    _currentmodule_))
bangedorrunt commented 2 years ago

@gwerbin yes, you can with special form *module-name*.

Quote from aniseed doc:

(comment
  *module* ;; => {...} the current Lua module table.
  *module-name* ;; => "my-module"
  *file* ;; => "relative/path/to/source/file/my-module.fnl"
  )