Mime is a scripting tool for text processing, inspired by Emacs.
Mime provides an editor-like abstraction for manipulating text files, but in a scripting environment, without an editor. This enables very sophisticated transformations that are easy to do through tools like Emacs Keyboard Macros, but hard to do in code.
- Documentation: https://shsms.github.io/mime
For providing its functionalities, Mime depends on these amazing libraries:
- ChaiScript for the scripting language.
- Immer for data representation.
- cxxopts for parsing of cli arguments.
- googletest for unit tests.
(You don’t have to install them separately, they are added as submodules to this repository, so just following the build steps in the Getting started page is enough.)
var b = buffer("main.go");
var doc_c = b.new_cursor();
var nav_c = b.new_cursor();
b.use_cursor(nav_c);
while(b.find("func ") >= 0) {
b.set_mark();
b.find("(");
b.backward();
var fname = b.copy();
b.use_cursor(doc_c);
b.paste("// FuncAlert: " + fname + "\n");
b.use_cursor(nav_c);
}
b.use_cursor(doc_c);
b.paste("\n");
b.save_as("mimeout.go");
If there is a file “main.go” in the same directory with the below contents,
package main
import "fmt"
func main() {
fmt.Print(hello(), world())
}
func world() string {
return "world!"
}
func hello() string {
return "Hello "
}
Then running the above mime script with:
mime gofunc.mime
would generate a new file “mimeout.go” with below contents:
// FuncAlert: main
// FuncAlert: world
// FuncAlert: hello
package main
import "fmt"
func main() {
fmt.Print(hello(), world())
}
func world() string {
return "world!"
}
func hello() string {
return "Hello "
}
Here’s what the script does:
- open file “main.go”
- create two cursors - one for adding documentation at the top, and one for navigating the file.
- switch to the navigation cursor.
- find and goto next occurence of the string “func “. If found:
- set mark (emacs parlance for “start selecting”)
- find next occurence of “(” (assuming we are operating on a valid go program, we don’t check if “(” was found, we assume it is there.)
- go back one character, we don’t want to copy the “(”
- copy the text between mark (where we started selecting in 4.1), and (current cursor) point, and store in a var called “fname”.
- switch to the documentation cursor, which is still at the top
- paste the function name in “fname” with a comment prefix and a newline at the end.
- switch to navigation cursor
- goto step 4.
- switch to doc cursor to insert a final newline to introduce a gap between inserted text and original program.
- save file under a new name.
- ulysses-annotated: a project to automatically generate an annotated ebook version of Ulysses. Read more in its github page or on joyceproject.com
If you’d like to contribute a feature or a bug fix, feel free to send a pull request!