Friday, February 25, 2011

One Liner: Pretty Print, Syntax Highlight and Page Online XML Data

Useful when leaching a bunch of video clips from YouTube:
curl -s "http://gdata.youtube.com/feeds/api/playlists/916175AB005C0FB6" | xml_pp | pygmentize -l xml | less -R
(required packages: curl, xml-twig-tools, python-pygments)

Actually, xml-pp isn't really needed with YouTube - it can do the pretty-printing for you:
curl -s "http://gdata.youtube.com/feeds/api/playlists/916175AB005C0FB6?prettyprint=true" | pygmentize -l xml | less -R

Friday, February 18, 2011

Firefox Crash Recovery

Just the other day, I was left with no option other than to power-cycle my Ubuntu workstation at work. After this, Firefox, which was open when my workstation died on me, would not start anymore. It just insisted that "Firefox is already running, but is not responding. To open a new window, you must first close the existing Firefox process, or restart your system." - a useless piece of crap advice.

This happened to me before, but I never bothered to document the recovery process. I learned my lesson, thank you very much:
  1. step #0: find the location of the default Firefox user profile, and then cd to it:
    cd ~/.mozilla/firefox/pefasakk.default/
  2. step #1: remove the previous session's lock file:
    rm .parentlock
    you may be lucky, and all may just work now. I wasn't, and it didn't.
  3. step #2: if Firefox complains that bookmarks and history have been disabled, then you should move away the places database and try again:
    mv places.sqlite places.sqlite.backup
    mv places.sqlite-journal places.sqlite-journal.backup
    the good news is that Firefox will most likely start with your bookmarks intact, the bad news is that you've just reset your browsing history...
  4. step #3: I thought I was out of the woods - everything looked normal enough, until I restarted Firefox - a few hours later - only to discover that Firefox stopped saving cookies, so that I had to re-login to every web site that required it (so many!). I fixed this by moving away the cookies database:
    mv cookies.sqlite cookies.sqlite.backup
    mv cookies.sqlite-journal cookies.sqlite-journal.backup
Now back to our regularly scheduled programming (pun intended).

Friday, February 11, 2011

How To Disconnect from a Network Share on Windows

My wife's employer issued her an Asus Eee PC 1005P Netbook. She rarely needs to use it, so that it's usually turned off. But, whenever it is turned on, I encounter new system administration problems. The latest problem was with network shares.

Windows 7 Starter, which is the OS installed on this netbook, doesn't seem to allow access to multiple network shares, with different credentials. One has to disconnect from the currently connected network share before connecting to another one.

Disconnecting from a network share that's been mapped as a network drive is easy enough (the menu item to look for starts with 'Disconnect' ...) - but there's no obvious way to disconnect from a network share that has not been mapped as a network drive.

I found a solution on this forum thread - run the following command in a command window:
net use \\MACHINE\share /d
where you should replace \\MACHINE\share with the path of the network share you want to disconnect from.

Friday, February 4, 2011

Highlight #if 0 ... #else ... #endif in Emacs

This seems to be a FAQ, and a rather annoying missing feature in Emacs. Especially once you see it working in Vim. I guess it's the closest I can get to penis envy.

Well, envy no more - just add the code below to your .emacs and you're good to go.

It'll highlight, as comments, bits of C/C++ code that are disabled with #if 0 ... #else ... #endif and #if 1 ... #else ... #endif. This is done by configuring cpp-highlight-buffer to recognize "0" as an undefined C preprocessor label and "1" as defined, and then causing cpp-highlight-buffer to be called every time the buffer is highlighted.

If you feel adventurous, I suggest you remove the first few lines - those that start with (setq ... - and use, instead, the Emacs customization interface to configure cpp-highlight-buffer:
M-x customize-group RET cpp RET
It should be pretty easy to recreate the setup below, and then you may want to experiment:
  1. handle any #ifdef ... #else ... #endif blocks, by adding more pre-defined labels (e.g. HAVE_CONFIG_H)
  2. make disabled code read-only
  3. make disabled code completely invisible!
  4. use (background-color . "gray") instead of font-lock-comment-face, so that disabled code is still syntax highlighted as usual, only grayed-out
  5. ...
(don't forget to save your settings once done)

To be fair with Vim, this is a (potentially CPU intensive) hack, but then again, what isn't?

(setq cpp-known-face 'default)
(setq cpp-unknown-face 'default)
(setq cpp-known-writable 't)
(setq cpp-unknown-writable 't)
(setq cpp-edit-list '(("0" font-lock-comment-face default both)
                      ("1" default font-lock-comment-face both)))

(defun my-c-mode-font-lock-if0 (limit)
  (cpp-highlight-buffer t)
  nil)

(defun my-c-mode-common-hook ()
  (font-lock-add-keywords
   nil
   '((my-c-mode-font-lock-if0 (0 font-lock-comment-face prepend)))
   'add-to-end))