Skip to main content

Posts about code

Say Howdy with Emacs!

Staying in touch with people is something I'm not very good at. Since I am not on popular (among my friends/family) networks – FB and Whatsapp – I don't even see random updates from people, to get some sense of being in touch.

I recently read some old posts by Sacha Chua and was inspired by how much code she had for contact management in her old blog posts. I was inspired by this post in particular to try and be more meticulous about how I stay in touch with people. Michael Fogleman blogged about his contact management work-flow using keepintouch. It seemed to do most of what I wanted, but I wanted this to be integrated with my org-contacts-db and I felt having native elisp code would make it easier to hook up email, chat, etc. to this.

I ended up writing a small utility called howdy to help me keep in touch with people. It currently has only a couple of features:

  • M-x howdy lets me update the last contacted timestamp for a contact.
  • Shows me contacts that I'm out of touch in the agenda, once I add the following snippet to an agenda file.
    * Howdy
      %%(howdy-howdy)
    

I also have a few hooks to hook up jabber messages and email to update the db. I've added them to howdy-hooks.el in case anybody else wants to use them. They can also be used as examples to write other hooks. Feel free to contribute other hooks or suggest improvements. The library also ships with a modest test suite, that will hopefully make it easier for others to contribute.

I'm looking forward to experimenting with this over the next few weeks and improving it. Hopefully, it'll help me keep in touch, better than I do now.

Martin Fowler on Refactoring @ RubyRogues

I stumbled on a Ruby Rogues podcast yesterday, which had Martin Fowler as the guest. I really enjoyed the discussion on Refactoring (the noun, the verb and the book!)

Martin clarified in the podcast that refactoring (the verb/process) is a sequence of very small refactorings, while you keep making sure that you can run the test suite always. A refactoring (noun) is a change where you change the structure of the code without any externally observable changes, with the intent of making it easier to understand and cheaper to change in future.

I also really liked the metaphor of a 'healthy code base'. Refactoring is, then, the process of keeping healthy – exercise, speaking metaphorically. You can stack up all the exercise you need to do, until you get really unfit. Refactoring, similarly, needs to be done regularly, to keep the code base healthy. This lets you go faster, in the future.

I also really liked the advise about trying to push back the mental contexts you build, while trying to debug/understand some code that is not very clear, by refactoring the code to make it clearer. Code needn't be one big chunk of cryptographic text. Code is writing. It should be clear and understandable. Or, at least we should strive to make it so!

The discussion, as always on this podcast, was very lively, pleasant and enjoyable! Enjoy!

Simple org-clock and gnome/pidgin integration

See update below

I have been trying to get back to using org-mode and its clocking functionality, more often than not. I used to use it a lot, a few years ago, and haven't been using it, since I had been in my last job.

To help with it, I decided to integrate clocking in and out with changing status on Pidgin, and turning notifications on and off in Gnome. Here's a few lines of code that does this for me.

(defadvice org-clock-in (after pc/org-clock-in (&optional select start-time))
  "Turn gnome notifications off."
  (dbus-send-signal
   :session
   "org.gnome.SessionManager"
   "/org/gnome/SessionManager/Presence"
   "org.gnome.SessionManager.Presence"
   "SetStatus" 2)
  (shell-command "purple-remote setstatus?status=unavailable"))

(defadvice org-clock-out (after pc/org-clock-out (&optional switch-to-state fail-quietly at-time))
  "Turn gnome notifications back on."
  (dbus-send-signal
   :session
   "org.gnome.SessionManager"
   "/org/gnome/SessionManager/Presence"
   "org.gnome.SessionManager.Presence"
   "SetStatus" 0)
  (shell-command "purple-remote setstatus?status=available"))

Update <2014-11-01 Sat>

@baali tried to use my code, and it turns out it didn't work for him, because I forgot to mention that (ad-activate 'org-clock-in) needs to be run, after the defadvice code. I have no idea how it worked for me, without doing that. May be because I have defadvice for other functions?

Also, while debugging this, I found that defadvice is a deprecated way of doing this, and add-function is the way to go now. But, instead of advising the function, I decided to make use of org-clock-in-hook.

Here is the new code.

(defun pc/turn-off-notifications ()
  "Turn gnome notifications off."
  (dbus-send-signal
   :session
   "org.gnome.SessionManager"
   "/org/gnome/SessionManager/Presence"
   "org.gnome.SessionManager.Presence"
   "SetStatus" 2)
  (shell-command "purple-remote setstatus?status=unavailable"))

(defun pc/turn-on-notifications ()
  "Turn gnome notifications back on."
  (dbus-send-signal
   :session
   "org.gnome.SessionManager"
   "/org/gnome/SessionManager/Presence"
   "org.gnome.SessionManager.Presence"
   "SetStatus" 0)
  (shell-command "purple-remote setstatus?status=available"))

(add-hook 'org-clock-in-hook 'pc/turn-off-notifications)
(add-hook 'org-clock-out-hook 'pc/turn-on-notifications)

Recurse Center, 2014-07-06

I took it easy during the weekend, and went on a nice camp with my a cousin's family to TobyHanna State Park for a day or so, and spent the remaining (long) weekend at their place in PA. It was a pretty nice break!

Yesterday evening, I talked to Madhu about how ugly my code looked while I was trying to use libclang, and he told me that he found a lot of wrapped C++/C libraries end up being that way, and I also realized that using the Python AST library seemed much cleaner, because it just contained Python objects, but representing C/C++ objects in an AST and interacting with them in Python, isn't that clean.

I cleaned up my code, and removed all the ugly regexes to use libclang to do the parsing of the libs, and get the source code. I basically got all the tests passing, but they were taking an order of magnitude longer to run. So, I guess the next step is to cache things somehow.

I also added support for viewing the sources of built-in types. It was pretty simple, once I had everything working with libclang.

A smarter 404 page.

Voodoo found a broken link referring to a one of my posts, on Quora. Given that I have changed my site generator a bunch of times now, it is quite possible that there are other broken links at various other places. So, I implemented a smart 404 page for the site, yesterday. It is very similar to something I came across on brettterpstra's site. I just hooked up fuse.js with Nikola's tipue-search plugin's output file. My glue code essentially, just figures out the search term from the URL, performs a Fuse search on the json data created by Nikola's search plugin, and returns a list of top 5 results. It just took me about an hour to write, but is pretty useful, I think.

Bookmarks and Quotes plugin

I used to have a separate page for bookmarks and quotes on the old blog. They will now be shared as regular posts with 5 or more bookmarks/quotes. I use a bookmarklet in my browser to capture links to an org-file using org-capture protocol and then I have a small plugin to Nikola, that looks at such captured bookmarks and quotes and makes new posts out of them. This workflow is inspired by Brett Trepestra's web excursions plugin.

The plugin is currently a part of my blog source, and not published to Nikola's plugin repository since it is very specific to my setup. But if anybody is interested, I can publish the code somewhere public.

If you are interested to get these links and bookmarks into your feed reader, you can subscribe to their feed: [quotes], bookmarks. These posts will also make it to the main feed, though.

UPDATE [2015-11-15 Sun 20:45]

  • The quotes feed no longer exists!
  • The bookmarks feed doesn't really get too many updates. I use pinboard for my bookmarks, now.

GitHub Cue: Recommendations for GitHub Repos

If you aren't already aware of it, I'm one of those people who goes around saying, "GitHub is my Facebook". I spend quite a lot of time on GitHub, browsing the work of various people, looking at loads of interesting stuff that people built. I keep jumping between people pages and projects using the Watchers/Watching & Followers/Following pages. This way, I do come across interesting people and projects, but the SNR is too low. I wanted a better way to be able to see stuff, that I find interesting. That's how the idea for this Chromium app – GitHub Cue – was born.

@baali and I hacked on this, during the last few days and got it working. It works as follows, (from the README) —

  1. Scrapes all the descriptions of the repositories being watched by the user.
  2. Key terms are extracted from this description text using the Yahoo Term Extractor.
  3. A list of languages is obtained, based on the languages of the repositories, the user if watching.
  4. GitHub searches are performed for a combination of 3 randomly chosen languages and 5 random key terms.
  5. 10 random repositories out of all these, are shown.

This is a very simplistic algorithm, but works decently for my purposes. Ideally, I would've liked to use a Collaborative Filtering algorithm, but I found the data to be too sparse, and the amount of computation to be too much to be done on the fly. I wasn't really interested in pre-computing stuff and putting it onto my server. I settled down to the next best thing I could think of.

I would appreciate any further ideas and suggestions. Thanks!

GetHub: Chrome Notifications for Github updates

Over the last two days, I hacked up my first Chrome extension. I've been using Chrome only for the past couple of weeks or so and I begin to like it, though some of the extensions aren't as mature as I would've liked.

The original idea was floated by my friend, Madhu, and Lee helped me quite a bit, while I was working on it.

What does it do?

It is a simple extension, that shows pop-ups, whenever there is an update in your GitHub "Wall" (yes, this is a Facebook world) or News Feed as they call it.

After installation, you will need to save your username and token for the extension to work.

Where to get it?

Presently, you will need to get it from GitHub.

I might add it to the Chrome Web Store, once I see more people using it. I couldn't justify, to myself, paying the initial one-time verification fee that Google asks for.

UPDATE: [2011-03-04 Fri] Added GetHub to the store

Comments and Feedback

  • Feel free to write to me at punchagan+gethub@jeemail.com
  • Or file issues at GitHub.

Happy GitHubbing!

Git powered wikis improved: My bit

I accidentally came across the blog-post Git-powered wikis improved - GitHub. And this particular paragraph caught my eye:

The new editor has the capability to support every markup language that GitHub Wikis support. If you're a fan of Markdown, Textile, pod or RDoc, the function bar buttons (e.g. bold, italic, underline, etc.) will now work. We've even written brand new inline help for many of GitHub Wiki's supported markup languages.

The language definitions the editor uses are JSON-based and easy to edit. If you'd like us to support a markup language that we don't currently support, Gollum, GitHub's wiki software, is entirely open source – fork our code and send us a pull request with changes that support your choice language.

I sent a quick patch for org-mode in the function bar, and it has been accepted. :)

If not anything more, I hope, at least a couple of curious people will explore org-mode and find it useful.

Raise windows (on current desktop) xdotool & bash

I posted a snippet 1 of python code that used xdotool to raise windows. I got one patch from dusual 2 that enabled raising of windows in the current workspace only.

More importantly, Jordan gave a one-liner 3 in bash, that could do the same thing as my long and elaborate python snippet. I had a feeling that this would be much shorter in bash, but I don't feel comfortable writing bash scripts. :P

Below is a bash snippet that tries to incorporates both the changes. :)

found=0

for win in `xdotool search --class $1`;
do
if [ `xdotool get_desktop_for_window $win` -eq `xdotool get_desktop` ];
then found=1; break;
fi;
done

if [ $found -eq 1 ]; then xdotool windowactivate $win; else $1; fi

Note: When you've visual effects enabled (the default option on Ubuntu), getting the current desktop/workspace doesn't work, probably because Root Window properties are being messed around by it.

Note2: search takes an option --screen. It'd be nice if it also had an option --desktop. This task could then be done in a one-liner.

Footnotes:

1

My python snippet

2

dusual on twitter

3

Jordan's one liner