[Bold Daemon]


Dynamic calendar location in Emacs

[2014-11-13 Thu 15:26]

Why would you want your location set in Emacs? Well it is an OS, isn't it? But seriously, I have found at least two valid reasons to make Emacs aware of my location.

  1. Dynamic lite/dark themes (moe-theme in this case).
  2. Knowing when the sun is going to rise/set.

Knowledge of sundown time is helpful if you are traveling (ever needed to beat traffic home?) or if your government insists on following stupid DST traditions.

I still wake up at 5:30 or 6 AM every morning. /me shakes his fist!

By default Emacs lets you set calendar-longitude and calendar-latitude with.. you guessed it, longitude and latitude for your location! But what if we set it for Colorado, and head on down to South Africa?

We need it to be dynamic!

Fortunately someone has already done the hard work. We only need to abstract it out with an externally accessible service (HTTP in this case) and we need to tell Emacs how to query said service.

First the HTTP server:

var http = require('http'),
geo = require('geoip'),
City = geo.City,
city = new City("/var/db/GeoLiteCity.dat"),
server = http.createServer(function(req, res) {
    var from = req.connection.remoteAddress || req.headers['x-forwarded-for'] || 'unknown';
    if (from === '') {
        from = req.headers['x-forwarded-for'];
    var cobj = city.lookup(from, function(err, data) {
        if (from === 'unknown' || err) {
        if (req.url.match('json')) {
        } else {
            res.end(data.latitude + ", " + data.longitude);

The only dependency for running the above server is GeoIP. Which can be installed using 'npm install geoip'

Next location.el:

Now that we have a way of getting our location based off our external IP address. We can move on to telling Emacs how to snag it!

(defun set-location (str)
  "Set Longitude and Latitude for calendar"
  (setq calendar-latitude (string-to-number (car str)))
  (setq calendar-longitude (string-to-number (car (cdr str)))))

(defun set-location-by-ip ()
  "Try to get Longitude and Latitude from GeoIP"
  (let ((url-request-method "GET"))
    (url-retrieve "http://geo.qbit.io/"
                  (lambda (status) ()
                    (set-location (split-string (buffer-substring (point-max) 81) ", "))))))

(provide 'location)

Once you have included location.el in your Emacs config, you can use it by using the following:

(require 'location)

Verify it's set by running M-x sunrise-sunset!

Blogging from Emacs

[2014-11-03 Mon 16:20]

My name is Aaron and I am addicted to Emacs. It's quickly taking over every application I use!

The latest area Emacs has assimilated is the publishing of my blog. I am tired of running a database.. a webserver AND a middleware app of some kind (RoR, Node.. etc). With Emacs I can do it all with a webserver and org-mode!

With a few blurbs to tell org what to do, I can create a new blog post and publish it with just a a few key strokes.

First Blurb:

(setq org-capture-templates
      ("b" "BoldDaemon Entry"
       entry (file+headline "~/org/websites/bolddaemon/index.org" "Posts")
       "* %?\n %U\n %i\n"
       :empty-lines 1
       :prepend 1)

This tells org to open our index.org file when we call org-capture. The options we are passing in (:empty-lines and :prepend) tell capture to add 1 line before and after the new entry and to prepend the entry to any existing entries.

The string with '%?', '%U', '%i' tells org to place the cursor where '%?' is after the template is processed, add an inactive time stamp and '%i' tells org where to place the content.

More docs for org-capture and template expansion

Second Blurb:

(setq org-publish-project-alist
       :base-directory "~/org/websites/bolddaemon/"
       :base-extension "css\\|js\\|png\\|jpg\\|gif"
       :publishing-directory "/ssh:akb.io:/var/www/bolddaemon/"
       :recursive t
       :publishing-function org-publish-attachment
       :base-directory "~/org/websites/bolddaemon/"
       :base-extension "org"
       :publishing-directory "/ssh:akb.io:/var/www/bolddaemon/"
       :recursive t
       :publishing-function org-html-publish-to-html
       :auto-preamble t
      ("bolddaemon" :components ("bold-static" "bold"))))

Now when ever I feel like creating an entry in my blog I simply M-x org-capture RET and select b. Type to your hearts content and hit M-x org-publish RET bolddaemon

You can see that publishing to another directory is as easy as adding an entry to the org-publish-project-alist var. Making it super easy to publish to a different environment for testing!

Adding your own Style:

The above is all fine and dandy, but what if you want it to look better!?

Simply add a line similar to this to your index.org file and BAM!

#+HTML_HEAD_EXTRA: <link rel="stylesheet" type="text/css" href="css/style.css" />

More docs for org-publish.

Finishing up

<3 you Emacs!

Author: Aaron Bieber

Created: 2017-06-02 Fri 10:16