MAGICIEL is an Emacs Lisp implementation of the file and
magic file content and type guessing machinery available on UN*X
systems.
There are two reasons for its existence.
- Installing the C
dllon Windows is a P.I.T.A. for my standards. - An Emacs Lisp (and, next, a Common Lisp) version is useful per se.
- Why not doing it? It is just one of the many rabbit holes I fall into.
- You do not want to know which rabbit hole led to this one.
- Did I say there were two reasons?
The final version (available "very soon now" (tm)) will allow you to get information about a file (and MIME information) directly from Emacs Lisp.
Also, the library will allow you to load and compile magic(5) files
in a modular way.
To use the package, just install the folder magiciel in your Emacs
setup and ensure that the file magiciel.el is loaded. Eventually, it
will get into melpa.
The main command (and function) to use is magiciel-divine, described
below.
(magiciel-divine FILENAME &optional (CONTEXT :normal)) The type of FILENAME will be divined by the call.
CONTEXT can be either :normal or !:normal, in which case the
function will return a description of the file content (as per the
simple use of the UN*X file command; or CONTEXT can be !:mime
in which case it will return the MIME type of the file (or
"application/octet-stream" or "data" as last resort). Finally,
CONTEXT can be !:ext, in which case the function will return the
most likely extension for FILENAME, by querying the magic spells
(i.e., not by looking at the filename extension).
If called interactively, the command will default to :normal
CONTEXT, unless invoked with a prefix (use the ?d choice for
:normal operation in that case).
- Eventually, it'd be nice to have a sexpy
magicformat. - Integration with
diredet similia, is also a desirable goal.
The flow is essentially the following.
- A
magic(5)file is read line by line. - A
lineis parsed in amagiciel--magic-recordby the functionmagiciel--parse-magic-record(in fileparse-magic.el). - A
magiciel--magic-recordis "compiled" in amagiciel--instructionby the functionmagiciel--compile-record, which is actually called bymagiciel-compile-magic; the last function does also other things like organizing the test tree implied in themagic(5)specifications (in filecompile-magic.el). - The function
magiciel--compile-recordis just a wrapper aroundmagiciel--compile-instrwhich generatesmagiciel--instructions. - The
magiciel-instructions generated are then emitted bymagiciel--emit(always in filecompile-magic.el), which is called bymagiciel--group-emitfrommagiciel--magic-emit.
Of course you are free to fork the project subject to the current licensing scheme. However, before you do so, I ask you to consider plain old "cooperation" by asking me to become a developer. It helps keeping the entropy level at an acceptable level.
Enjoy
Marco Antoniotti, Milan, Italy, (c) 2024