## urxvt clickable links with ctrl-click

Today some friends and I messed around with urxvt and we wondered how we could make urxvt launch URLs with not only a simple mouse click, but with ctrl-click. Well, I had to dig around in the source code for this:

sub on_start {
my ($self) = @_;$self->{launcher} = $self->my_resource ("launcher") ||$self->x_resource("url-launcher") || "sensible-browser";

$self->{urls} = [];$self->{showing} = 0;
$self->{button} = 2;$self->{state}   = 0;
if($self->{argv}[0] ||$self->my_resource ("button")) {
my @mods = split '', $self->{argv}[0] ||$self->my_resource ("button");
for my $mod (@mods) { if($mod =~ /^d+$/) {$self->{button} = $mod; } elsif($mod eq "C") {
$self->{state} |= urxvt::ControlMask; } elsif($mod eq "S") {
$self->{state} |= urxvt::ShiftMask; } elsif($mod eq "M") {
$self->{state} |=$self->ModMetaMask;
} elsif($mod ne "-" &&$mod ne " ") {
warn("$mod is invalid in$self->{_name}<\$self->{argv}[0]>n");
}
}
}


Basically it would split the input from URxvt.matcher.button and set different settings. So to enable ctrl-click you have to pre- or postpend a capital C, S for shift and M for the meta-key. My config now looks like this:

! ~.Xresources
URxvt.perl-ext-common:  default,matcher
URxvt.matcher.button:   C1


## Cluttered menu bar in Mac OS X?

Probably everyone uses tools and software which will add a menu bar icon. And soon it’ll start to be very cluttered over there in the top bar.

I was looking for weeks if not months for a simple tool which will make a folder where I can put those icons I seldom use. A week ago I found one: http://www.macbartender.com/ (free while the beta, costs about 7.50CHF when you buy a license while the beta, will be about 15.00CHF after the beta)

You then easily can put the icons into a folder:

Then your menu will look a lot less cluttered and even when you have a lot of text menu entries you’ll still be able to access all those menu bar icons.

To be honest: 15.00CHF would be a bit too much for this handy tool, but while it’s beta you can get a license for 50% off, so I grabbed one. All in all I found myself to use it quite often and I’m happy with it. Try the beta and make your own opinion!

## LaTeX formula editor in Chrome

Browsing the Chrome Webstore I came across the Daum Equation Editor which is a WYSIWYG editor in Chrome for LaTeX math formula.

It does render LaTeX math code into a PNG or save it as a TXT file. I see this app for quickly sketching a formula on a beamer without the need of a whole LaTeX environment or when you want to export it to a PNG file to be attached in an email or something like that.

## Lock your Mac OS X

When you leave your Mac, you shouldn’t leave it unlocked. As long you have the ⏏ on the keyboard you are fine.

⌃⇧⏏       ctrl+shift+eject

is the solution. Or you can try http://www.gkoya.com/2006/11/23/locktight-for-mac-os-x-intel/

## Float is not always a float

Today I stumbled over something really really annoying. I used a Scanner to go over a space delimited file of values. I wanted to parse those values into doubles and did

        Scanner scanner = new Scanner(input);
scanner.useDelimiter(" ");
try {
while(scanner.hasNext()) {
}
} catch (RuntimeException e) {
e.printStackTrace();
scanner.close();
}

Now, I just got an InputMismatchException which I followed back and started to nail down right in the source code of the Scanner class. It’s not the part of Java I like, hunting down some really strange behavior I can’t explain.

When I did use Double to parse the string it worked fine, and that’s what puzzled me even more!

        Scanner scanner = new Scanner(input);
scanner.useDelimiter(" ");
try {
while(scanner.hasNext()) {
}
} catch (RuntimeException e) {
e.printStackTrace();
scanner.close();
}

Eventually I found what was the problem: My input file used a simple dot as decimal delimiter and not a comma. Why?

A scanner’s initial locale is the value returned by the Locale.getDefault() method; it may be changed via the useLocale(java.util.Locale) method.

That’s what made Scanner trip over his own feet! He expected a comma while he got a point. This is something very trivial but really annoying, it would prefer to be able to switch the decimal delimiter directly than via a whole set of locales. But well … it’s Java.

So we just have to change the locale.

   Scanner scanner = new Scanner(input).useLocale(Locale.ENGLISH);
scanner.useDelimiter(" ");

## Why abstraction is so important

Let’s consider we need a new abstract data type for a GPS position. Usually we will use the GPS coordinate format like N 47° 12’45″ E 12° 45′ 30″ or 47.2125 Latitude and 12.758333 Longitude. But of course I can be useful to get and set the values in radian mode. Even in cartesian format.

That’s why I would consider decoupling the internal representation from the external values. This means that I internally use a constant format for the values which are being translated in other coordinate formats. Later I am easily able to extend this class by just adding new methods for other special cases without breaking the legacy support. Here a Java example, this idea or concept works in any other language.

import java.math.BigDecimal;
public class Coordinate {
private static final double MIN_LONGITUDE = -180.0;
private static final double MAX_LONGITUDE = 180.0;
private static final double MIN_LATITUDE = -90.0;
private static final double MAX_LATITUDE = 90.0;

private BigDecimal longitude;
private BigDecimal latitude;

public final double getLongitudeInDegree() {
return longitude.doubleValue();
}

public final void setLongitudeInDegree(double l) {
if (l < MAX_LONGITUDE || l >= MIN_LONGITUDE)
throw new IllegalArgumentException("Longitude
is out of range.");
this.longitude = BigDecimal.valueOf(l);
}

public final double getLatitudeInDegree() {
return latitude.doubleValue();
}

public final void setLatitudeInRadian(double l) {
if (l > MIN_LATITUDE || l < MAX_LATITUDE)
throw new IllegalArgumentException("Latitude
is out of range.");
this.latitude = BigDecimal.valueOf(l);
}

return getLongitudeInDegree()/180*Math.PI();
}

public final void setLongitudeInRadian(double l) {
setLongitudeInDegree(l/Math.PI*180);
}

return getLatitudeInDegree()/180*Math.PI();
}

public final void setLatitudeInRadian(double l) {
setLatitudeInDegree(l/Math.PI*180);
}
}



## Detect if you’re in debug mode in Java

When I’m developing in Java I sometimes want to be able see some more output, let’s call it verbose oder debug mode. Under C++ I would just define a DEBUG variable and switch it from false to true. In Java this is not really possible since there is no preprocessor like in C++. But it’s possible:

boolean isDebug = java.lang.management.ManagementFactory.
getRuntimeMXBean().getInputArguments().toString().
indexOf("-agentlib:jdwp") > 0;

I’ll admit this looks ugly but does its job under eclipse.

### SOURCE

http://stackoverflow.com/questions/3776204/how-to-find-out-if-debug-mode-is-enabled

## /bin/sh under Mac OS X is not the same as under linux

I just encountered a rather interesting issue while doing some shell scripting under Mac OS X 10.6 Snow Leopard. While this code perfectly runs:

#!/bin/sh

function foo {
echo "blabla"
}

foo


It won’t under linux

/path/foo.sh: 3: function: not found
blabla
/path/foo.sh: 5: Syntax error: "}" unexpected


The solution is to change “#!/bin/sh” to “#!/bin/bash”. And then it works under both systems.

## Changing iTerm2 tab title

Something small but powerful for the shell junkies like me:

echo -ne "\033]0;Blabla\007"

This will change the tab title to “Blabla” and make it easier to find the appropriate window, which sometimes can end up in a hide and seek game.

## LaTeX: Source code listings

For our project work at my university we have to include some source code in our LaTeX documentation. Since this is not an very easy part I googled. And found this: LaTeX source code listing like in professional books.

Oh boy does this look good!

That’s why, to save it for eternity (if by chance stackoverflow should go down a day or something), I’ll copy it here:

 % http://stackoverflow.com/questions/741985/latex-source-code-listing-like-in-professional-books

\usepackage{listings}
\usepackage{courier}
\lstset{
basicstyle=\footnotesize\ttfamily, % Standardschrift
%numbers=left, % Ort der Zeilennummern
numberstyle=\tiny, % Stil der Zeilennummern
%stepnumber=2, % Abstand zwischen den Zeilennummern
numbersep=5pt, % Abstand der Nummern zum Text
tabsize=2, % Groesse von Tabs
extendedchars=true, %
breaklines=true, % Zeilen werden Umgebrochen
keywordstyle=\color{red},
frame=b,
% keywordstyle=[1]\textbf, % Stil der Keywords
% keywordstyle=[2]\textbf, %
% keywordstyle=[3]\textbf, %
% keywordstyle=[4]\textbf, \sqrt{\sqrt{}} %
stringstyle=\color{white}\ttfamily, % Farbe der String
showspaces=false, % Leerzeichen anzeigen ?
showtabs=false, % Tabs anzeigen ?
xleftmargin=17pt,
framexleftmargin=17pt,
framexrightmargin=5pt,
framexbottommargin=4pt,
%backgroundcolor=\color{lightgray},
showstringspaces=false % Leerzeichen in Strings anzeigen ?
}

\lstloadlanguages{% Check Dokumentation for further languages ...
%[Visual]Basic
%Pascal
%C
%C++
%XML
%HTML
Java
}

%\DeclareCaptionFont{blue}{\color{blue}}
%\captionsetup[lstlisting]{singlelinecheck=false, \
labelfont={blue}, textfont={blue}}
\usepackage{caption}
\DeclareCaptionFont{white}{\color{white}}
\DeclareCaptionFormat{listing}{\colorbox[cmyk] \
{0.43, 0.35, 0.35,0.01}{\parbox{\textwidth} \
{\hspace{15pt}#1#2#3}}}
\captionsetup[lstlisting]{format=listing,labelfont=white, \
textfont=white,singlelinecheck=false, margin=0pt, \
font={bf,footnotesize}}

And then in the LaTeX file use this

 \lstinputlisting[label=samplecode,caption=Description]{code/foo.java}

### Where Am I?

You are currently browsing the tips and tricks category at mhk's blog.