Showing posts tagged emacs

Saying an editor has an “Emacs Mode” because it mimics some of its keybindings is like saying a language has “A Lisp” because it has parentheses. It’s so far off base that it’s not even wrong.

Applescripted Lunch and Learn Org File FTW

I whipped together a small applescript today to wrap around my lunch and learn Org file. I haven’t actually written about my lunch and learn file before but I’ll try to soon. In short, it’s really important to sharpen your saw and if you’re anything like me, you need a plan to do that.

I’m not great at Org. I’ve never really used it before a few months ago, but it’s been on my list of things to look into for years. People talk about it like it’s the air they breathe, so it must be good, right? :)

Anyway, Org supports todo lists natively. A todo header is simply a line that starts with some number of * characters followed by the keyword TODO (in the case of an incomplete item), or DONE (in the case of, you guessed it, a complete item).

Org also supports hyperlinks that can point to an incredible variety of sources. I use this so I can mix direct links to a video or paper online and to local files that I’ve cached for offline usage. A link is just a piece of text with the following format: [[link][description]].

I found myself, very often, popping Emacs open, and copying the current Chrome tab’s URL and title into that file such that I generated a new line of the format: [[current tab url][current tab title]].

But hey, I know applescript yo. Why am I doing this manually like an animal?

What came out of that was this little script. If you want to use it, you’ll have to edit it. There are still several hard coded paths and such in there. I’ve been developing a whole suite of applescript all centered around managing interactions with your tabs from anywhere in your system across bash/applescript/𝑄uıcĸsılⅴεʀ and I really want to open source them at some point because they’re pretty cool. Right now, sadly, they’re not even on Github.

Also, to use that you’ll have to download the write_to_file subroutine and save it somewhere as a .scpt file so that Applescript can load it. I’ve heard rumors that Applescript subroutine management got a lot better recently so I need to dig into that soon because right now it sucks.

Enjoy!

Aww, init.el… You’re so sensitive!

I got tired of re-inventing the wheel every time I wanted to set a variable with sensitive data in my .emacs file, so I published a little package that aims to make it dead simple to lazily set variables for modes that require that sort of thing, called sensitive.el.

The way it works is by interpreting a directory tree of directories named after modes and files named after variables whose contents is what the variable should be set to, and causing an eval-after-load corresponding to each file form to be evaled when emacs starts up.

For instance. If the contents of sensitive-root (by default ~/sensitive) are as follows:

{sensitive-root}
├── erc
│   └── erc-password -> "password as a string"
└── pivotal-tracker
    └── pivotal-api-token -> "api token as a string"

sensitive will cause the forms

(eval-after-load 'erc
  '(setq 'erc-password "password as a string"))

(eval-after-load 'pivotal-api-token
  '(setq pivotal-api-token "api token as string"))

to be evaled.

It’s important to note that the contents of the variable files should be exactly what you would have used if you were to write the eval-after-load form yourself. In other words, there is no defaulting to strings or something; the form is actually read via the elisp reader and inserted into the eval-after-load form.

To install sensitive, just use package.el and point it at MELPA.

For further usage or to file bugs, please visit the sensitive.el repo.

If you find this useful, let me know in the comments!

sequences.el

I hacked together a very simple port of two functions that I find very useful to have around in Clojure: tree-seq and file-seq.

Because elisp doesn’t support namespace, they’ve been renamed to sequences-tree-seq and sequences-file-seq respectively.

To get them, install the sequences package via package.el. It’s available from MELPA.

sequences-tree-seq takes 3 arguments: branch?, children, and root.

branch? is a function which should return true if a node may contain children.

children is a function which returns the children of a node that might contain children.

root is the root of the tree structure.

This is quite a powerful abstraction as it can take any tree-like object and give you leaves in a depth first traversal given simple functions.

sequences-file-seq is a demonstration of why this can be useful. Given a directory name, calling sequnces-tree-seq like so:

(sequences-tree-seq 'file-directory-p
                    (lambda (directory)
                      (directory-files directory t))
                    (expand-file-name root))

will return every file under the directory in a depth-first manner.

The mess with the lambda is only necesarry because I wanted it to return absolute paths rather than relative. If I had been ok with relative, I could have simply passed 'directory-files and it would have done the job.

I’m using these functions in another feature I just wrote that I’ll be blogging about shortly. Until then, I hope they’re useful to you.

I am hoping to have a pull request out to Magnar's dash library soon which would obsolete this library quite nicely.

Until then, enjoy!

google-this.el Just Got Lucky

I had hacked together a little implementation of the Google Lucky Search Services a little while ago and when I went to release it I figured it’d be good to check if there were any other libraries out there that already did it.

Lucky for me, there weren’t any. But there was a nice package called google-this that already had a bunch of nice features for searching for things via Google, things being thing-at-point or region or word or line etc. I had hoped to add that stuff into my functions later on so instead I retrofitted google-this with lucky searching capabilities.

I think it came out quite nicely!

To use the new functions, use package.el to update or install google-this to at least version 20130908.1342.

The main use cases for the lucky search operations are to replace text you are pretty sure will return the right URL with that URL or to actually perform the lucky search in your browser. I use both a ton, and have the corresponding services, in general, bound to ⌘⌥^-^ and ⌘⌥^-&, in general. The services, however, don’t work in Emacs for me because iTerm doesn’t provide the right text services or something.

The new entry points in google-this are google-lucky-search and google-lucky-and-insert-url. Both can be called interactively or bound to keys. By default they are bound to C-c / g and C-c / i, respectively.

google-lucky-search attempts to intelligently deduce which among region, thing-at-point 'symbol, thing-at-point 'word, and line you would like to search for, in that order, and then prompts you to be sure it’s searching for the right thing, allowing you to enter something else. Once you’ve selected your term, it pops open a new browser window using browse-url and you’re good to go.

google-lucky-and-insert-url, on the other hand, chooses between an active region and the current line and then performs an asynchronous retrieval of the URL and replaces the line or the region with the retrieved URL. With just a bit more hacking I think it could easily be extended to also do thing-at-point 'symbol and thing-at-point 'word.

Anyway, I hope you find it useful. If you do, let me know in the comments!

Automatically Put Extracted Archive Files Into Read-Only Mode

If you’re editing Clojure code in Emacs a lot, you’re probably using ⌥.. If you’re not, you should be. It’s a fantastic way to navigate around your code.

That being said, Emacs comes with this awesome feature where if you try to ‘edit’ an archive file of some kind, it’ll simply open it and allow you to edit it and then save the file back to the original archive for you. It’s all pretty magical.

Except when you’ve ⌥.ed into a jar file and find yourself editing it by mistake.

So I hacked together this very simple bit of Elisp. All it does is make a buffer produced by archive extraction read-only by default, which I’ll be thanking myself for later.

Enjoy!

Calc was originally started as a two-week project to occupy a lull in the author’s schedule. Basically, a friend asked if I remembered the value of ‘2^32’. I didn’t offhand, but I said, “that’s easy, just call up an xcalc.” Xcalc duly reported that the answer to our question was ‘4.294967e 09’—with no way to see the full ten digits even though we knew they were there in the program’s memory! I was so annoyed, I vowed to write a calculator of my own, once and for all.

I chose Emacs Lisp, a) because I had always been curious about it and b) because, being only a text editor extension language after all, Emacs Lisp would surely reach its limits long before the project got too far out of hand.

To make a long story short, Emacs Lisp turned out to be a distressingly solid implementation of Lisp, and the humble task of calculating turned out to be more open-ended than one might have expected.

Calc is a monument to what can be done in ELisp…

GNU Emacs Calc Manual