Saturday, 12 March 2011

zsh tips

Invoke 'less' smartly

One of the nice little things about Git is that if the output of a command is long, then it is displayed in less. If output of a command is short, it is just displayed (there is no interactive Git involved). One can make Mercurial suck less by emulating this behavoiur with Pager extension.

Would be it nice to have this automatic less for everything, not just hg and git? Add this one line to your .zshrc:

AL() { $* 2>&1 | less -FSRX }

Now you can call commands like AL ls /tmp/mydir and enjoy having pager only when you need it. -R option makes less not mess up colour escapes.

Have easy access to newest download

I set chrome to automatically download everything to ~/downloads folder. I often want to do something with newest downloaded file (like open it or move somewhere else).

alias -g @@NDL='~/downloads/*(.om[1])'

You can use anything that is unlikely to collide with anything else instead of @@NDL (which stands for Newest DownLoad). Now you can use it like:

acroread @@NDL
mv @@NDL .

Parenthesis after glob is qualifier. Dot stands for file, om Ordered by Modification. One is there because om[0] always matches current directory.

Wednesday, 11 November 2009

64-bit Linux + Eclipse Galileo crash - fix

A short one.

I had this problem some time ago, and today I've seen someone else on #eclipse IRC channel to have it. If you are running 64-bit Linux (esp. Gentoo) and Eclipse Galileo and your eclipse keeps crashing with XPCOM error -2147467262, then fear no more. this is what you should do:

  1. turn off all Eclipse instances
  2. delete following files:
    • WORKSPACE/.metadata
    • ~/.mozilla/eclipse
    • ~/.eclipse
  3. add -Dorg.eclipse.swt.browser.XULRunnerPath=/usr/lib64/xulrunner-1.9.1/libxpcom.so to the end of your eclipse.ini file (actual path to your libxpcom.so may be different)
  4. you are done. You may use your Eclipse safely

This tip is based on comments 11 and 12 in this bug report.

Wednesday, 20 May 2009

Controlling roommate's music with ssh and dcop

This is rather old one, but I didn't publish it here before.

Both my roommates use Amarok as their music player. I'm OK with music they listen to for 90% of time, but their big, fat playlists hide some real monsters. And they both have a bad habit of leaving the room with Amaroks playing in shuffle mode.

This is why I created two little scripts to pause/change the song without leaving the comfort of my chair.

Here is one, for Paweł (username pavlo) and his widescreen laptop kuandatun (kuandatun it's supposed to mean "wide ass" in Chineese).


#!/bin/zsh
ssh kuandatun "/opt/kde3/bin/dcop --all-sessions --user pavlo amarok player $*"


I have kuandatun in /etc/hosts, my .ssh/config knows that it should log in as pavlo to his machine and I have my public key listed in his .ssh/authorized_keys. Now, changing shitty song on his computer is as easy as AmarokPawla next.

The less evil use of this script is getting title, lyrics or artist of currently playing song.

EDIT: this is going to be obsolete soon, when they switch to amarok2, which uses D-Bus instead of DCOP, but it's basically the same trick

Friday, 27 March 2009

Introducing Zmrok

It was neither sleep, nor science. It was pure art. Ladies and Gentleman, I present you the Zmrok colour scheme:



features:
  • it looks good
    • well, I like it ;-)
  • it's small:
    • it's hackable
    • it's easily portable to other editors


Source can be viewed and downloaded at my mercurial repo or vim.org.

Zmrok is Polish for twilight, check out TextMate's (or pastie.org's) twilight colour scheme if you still wonder why it's named this way ;)

Sunday, 14 December 2008

Open source is great

I'm proud and happy to proclaim my Russian PLansliterated vimscript dead. Some nice guy have taken a look at it and developed a superior solution, so Russian PLansliterated shared a destiny of my pygments' lexer for Scala.

An interesting meta-solution emerges:
  1. Realize that you have a quite general problem
  2. Write 15-30 min hack solution to it. It may work a little crappy, but it's good if it's easy to read and extend.
  3. Open source it
  4. Wait...
  5. Next time you'll have the same problem someone has improved your solution beyond the recognition, so you can dump the old one.


It seems that improving something that exists is psychologically easier that starting from scratch. My new slides about Scala (download them, browse the source or watch them online) were made using pygments with new shiny lexer developed by someone pissed off by quality of an old one. Open Source is great, isn't it?

Saturday, 15 November 2008

Programming language idea

Well, it's an idea about programming language without a programming language.

Think about writing a script that generates assembly. It's doable. Sometimes it's even *almost* sane thing to do, eg. when working on spellchecker I've had a crazy idea about generating code that does dictionary lookup for a hardcoded word set (It's no rocket science, just compare, jump, compare, jump...). But it's so low level and not cross-platform.

I've done some C generation from Python several times (eg. to make last year's april fool's joke - it's on this blog). The problem is that C is often not flexible enough (GCC would choke on 100MB file for dictionary; TCO, call/cc and other fancy stuff is hard...). Not C, not assembly...

There is LLVM - a perfect target.

Moving on: a scripting language that would generate all this mess.

It should be very, very meta one. Like lisp, but with syntax. A pluggable one.

My idea is to write a language that would have pluggable lexer (one lexer would switch to another one when it would encounter some sequence of characters - think of reader macros on steroids), then a huge layer of macros (tons of macros. Like Nemerle). Add a lispy semantics on top of it (few orthogonal concepts that combine together nicely) and my evil plan is 10% complete.

You would be able to create literal syntax for your new, shiny DSL inside your script, then semantics that would generate optimal machine code for it and boom!

It would enable some crazy stuff. Think of all super-duper assertions library creator would be able to enforce compile time with it! You like that D enables you have optional purity control? You can add it to your script.

It wouldn't be general purpose tool (it would probably be to wired and demanding to do everyday work), but I can see applications where you have to be extremely fast (OS kernels, games) or you are very domain-specific stuff (think of all those code-generating tools like lex, bison, antlr).

More or less related things to do/blog about in future:
- DBus + gvim - a possible step towards an headless IDE
- Actor-based OS

random stuff

Beep, beep! I'm still (more or less) alive.

University takes me lots of time, which is my primary excuse for not blogging
for so long time. Projects are like gas - they fill up all available space^H^H^H^H^H time.

Interesting (for geeky enough definition of "interesting") stuff I've done recently:

  • Spellchecker. It's a quite smart one -- written in python and C, it uses trie (ternary search tree) for storing dictionary. It's super-fast to load data (there are no pointers, so loading it is just one read of binary file, and you can use it).

    Then you can use TST to quickly (it's quicker than hashmap) check if a word belongs to vocabulary or retrieve (it's still bleeding fast) a list of words that are no further (in Levenstein's (edit) distance) than misspelled one.

    Then it uses longest common subsequence algorithm to get parts that don't match and compares those parts using knowledge about typical spelling errors in Polish. It can correct "grzegrzułka", "zomp" and "fzhut". In summary: it's cool.

  • I'm preparing a talk about scala. It's work in progress. You can see some slides here. I'll give this talk on 3th December as a part of BIWAK

  • Oh, yes, BIWAK. We (BIT science club) have started a series of talks called BIWAK.

  • Oh, yes, science club. I've done some work on platform game with cool physics, but there is nothing cool to show off yet.

  • I've published some of my .rc files

  • Hooray new swimming pool! Hooray hiking! Hooray birthdays and weddings. Hooray real life.

Monday, 22 September 2008

Back to Poland

<terminator-voice>I'm back!</terminator-voice>
10 weeks of my internship at Resolver Systems passed like ten days. It was amazing experience - I learned a lot and I had great time. Many thanks to all my co-workers, you are great!

I had a great opportunity to attend first day of PyCon UK - it was really cool. I got some ideas I'd like to transplant to SFI festival.

TODO:
- expand this post
- LLVM
- programming language ideas
- vim + python
- more

Saturday, 21 June 2008

LaTeX thoughts and lack of time

I haven't written here for quite a long time — it's because of current semester's ending. I had a lot of work to do with my projects (btw: it's one of them; another video). For every project one has to write a documentation. And I know only one way to write documents that doesn't suck hard. It's the way of TeX. I have written quite a lot of LaTeX documents last month. Here is what I learned:
  • \newcommand and \newenvironment are your friends
  • Making your comands and env names uppercase lets you avoid clashes with predefined ones.
  • Try to be semantic from very beginning if it's easy. For example: you mention electronical parts or Java classes all over your document. Add \newcommand{\Class}[1]{#1} in the beginning of your document and wrap all class names with \Class{}. It would be easy do make all class names typed with some special font later on.
  • If your documents take a lot of time to compile
    • break it intro few files and \input them. Comment out inputs you don't work on currently.
    • use draft option
  • Thinking about writing documents in TeX as of programming helps a lot!
    • Being a nazi with DRY principle makes your document sources look very semantic and the output is prettier. You do more tweaking if you can do it in one place instead of 20 places.
    • Do the simple thing that gives you desired result. If you repeat yourself ⇒ abstract out repetition intro new command(s) and/or environment(s).
    • Refactor!


There are some things I wanted to write about, but I haven't got time. Here is my public TODO list:
  • Why Eiffel standard library is so cheesy that it makes me want to write program in brainf*ck instead of Eiffel (which is quite a nice language), and what can we learn from that (in "I promise, I'll never code something similar" way of learning)
  • Why hovercrafts rock and how it is like to discharge 8 Duracell bateries in 20 minutes of playing with your homemade, bluetooth controlled one. (It's much more Witek's one than mine, but still)...
  • Hooray, yet another post about monads!
  • Hooray, yet another post about Scala!
  • Erlang, OOP and functional programming
  • I switched to zsh

Maybe one day one of those will turn intro full-blown post. Maybe.

Friday, 2 May 2008

organize your python imports in vim

My first script at vim.sf.net. You can grab it here. Source code of it and unittests (nose is great!) are on my mercurial repo.

Have fun!

Sunday, 20 April 2008

bash fun vol. 2

Forget previous post. Bash has functions: goj@abulafia ~ $ tail ~/.bashrc
function apply() {
cmd=$1
shift
for x in $*; do $cmd $x; done
}

function go() {
$@
cd ${!#}
}

Much better now. :)

Monday, 14 April 2008

bash fun

Quote by Terrence Parr:
My Motto: "Why program by hand in five days what you can spend five years of your life automating?"


We do a lot of actions that make a directory interesting for us and then we want to cd intro that directory. Consider:

hg init my_repo; cd my_repo
mkdir -p a/b/c/d; cd a/b/c/d
mv my_file /tmp; cd /tmp


Boring, repetitive and not DRY.

Lesson 1: You can do something like this:
mkdir -p a/b/c/d; cd !$

Better, but not good enough for me. I'd like to type
go hg init my_repo
go mkdir -p a/b/c/d
go mv my_file /tmp


So we need to create a short bash script, don't we?

How do we get last argument of the script (we'll need that). Provided that $1 is the first argument, $2 - second and $# - argument count and $@ all arguments as an array... yes, you guessed it. It's ${!#}

How nice and intuitive. Try those: $$# ${$#} ${${#}} $@[$#].

So do we have all that we need?
goj@abulafia tmp $ cat go
#!/bin/bash
$@
cd ${!#}


goj@abulafia tmp $ ./go echo ~
/home/goj
goj@abulafia tmp $


Epic FAIL. ./go script is executed in child shell. When it terminates it's parent's CWD is unaffected. We have to source it:

goj@abulafia tmp $ . ./go echo ~
/home/goj
goj@abulafia ~ $


But this syntax sucks. Luckily, alias is our friend:

goj@abulafia tmp $ echo 'alias go=". go-source-me"' >> ~/.bashrc
goj@abulafia tmp $ mv go ~/bin/go-source-me


You have to have ~/bin in your PATH.

goj@abulafia tmp $ go echo ~
/home/goj
goj@abulafia ~ $


Ta-dam!

Wednesday, 2 April 2008

April Fool's day

As last year, I managed to make my year's forum unreadable for April Fool's day. Awful, pink theme from last year was yesterday joined by truly horrible spelling and flipped avatars. :>

Here are the results:

Here is a little HOWTO. It may be useful for something practical, or for next year. Basically, you have to use mod_ext_filter see the sed example.

Polish spelling mistakes are different to English ones. They are (mostly) caused by fact, that there are (for historical & (maybe) other slavic languages "compatibility" reasons (is legacy a proper word?)) few sounds that have the two spellings, like ź-rz and u-ó, h-ch. It makes it very easy to inject typos to polish text.

We are talking about HTML pages. You cannot break html, eg <a chref="..."> would be BAD. So we don't touch anything inside html tags, comments and entities. This makes it hard to use regexps. If I were to write this joke's filter program once again I would use flex. But I wrote a short and slow (2,2 seconds to process a page - unacceptable) automata-based python script, and then (when it turned out how bad it performs) added lighting-fast (0,01 s/page) C-code generation to it. Generating code for automata is very easy.

code is here

Code generation itself is a bit over-engineered, too. I shouldn't have cared about beautiful indentation & proper newlines. I should have used GNU indent instead.

Apache configuration:
ExtFilterDefine ortozawal mode=output intype=text/html cmd="/usr/local/bin/ortozawal"
ExtFilterDefine ungzip-filter mode=output intype=text/html cmd="/bin/gunzip -"
ExtFilterDefine gzipme-filter mode=output intype=text/html cmd="/bin/gzip"
ExtFilterDefine flip-image mode=output cmd="/usr/bin/convert - -flip -"

<Location "/forum">
SetOutputFilter ungzip-filter;ortozawal;gzipme-filter
</Location>

<Location "/forum/images">
SetOutputFilter flip-image
</Location>

gunzip-filter-gzip is a filthy trick. You can probably avoid it by proper filter configuration. If you know how, please drop me a comment.

Saturday, 1 March 2008

small, pythonic lisp

Grab it here.

It's < 256 LOC toy lisp. Nothing super-fancy. If you don't count unit tests and Guido's mm.py it's even < 128 LOC.

It's quite pythonic. When you write eval in LISP it's just a big cond. It translates directly to if/elif/elif/... which doesn't look good. I used dict of functions instead, which looks better in my eyes. Uses as many Python features as possible. Python functions, python lists. etc.

Talking about Python and lisp lists:

class slist(object):
class iterator(object):
def __init__(self, lst):
self.lst = lst
def next(self):
if self.lst:
result = self.lst.car
self.lst = self.lst.cdr
return result
else: raise StopIteration

def __iter__(self):
return slist.iterator(self)

def __init__(self, a, b=None):
self.car = a
self.cdr = b

def __str__(self):
return '(%s)' % ' '.join(str(elem) for elem in self)

list_123 = slist(1, slist(2, slist(3)))
a, b, c = list_123

Yes, this is just a linked list, not a true scheme cons, since it doesn't support improper lists, I posted it just to show how Python iterators are neat.

Friday, 29 February 2008

Lap year's WTF

Well, we all know phpBB is evil. Html code it generates makes you sick. It's a great example of spaghetti design antipattern. And it's written in PHP.

Today (it's 29th February) many phpBB by Przemo forums have blown. It's because of lap year and their "whose birthday is it" function.

Warning. Clicking link below may even make you vomit.
I'm ugly, think again

Yes, they didn't use standard date("L"), yes they did their own calculations (using sqrt?!?), and yes, they f*ck*d it badly. Awful switch full of magic numbers (hello, PHP is a junk, but it has loops and arrays) is just a minor ugliness.

Saturday, 9 February 2008

int *(*(*(*b)())[10])();

I've just read Terence Parr's post titled "How To Read C Declarations". The quoted "Golden Rule" makes reading declarations really easy even for a drunk ape, but it's one of the kind I dislike. It gives you some "magical" steps to follow (here are the same rules stated more verbosely in an awful, BASIC-ish GOTO-step-N manner) with no explanation why this way, no another.

Here is the missing explanation:

Declaration reflects how you use declared expression (how you get the value of it), so in int tab[]; tab is an array (you index it) of integers. int (*tab)[]; is a pointer to an array (you dereference it, then index). int *(tab[]); is an array of pointers — you have to index it, then dereference.

How about int *tab[]? You have to know operator precedence

It's not that hard as in looks like. In our case the rule of thumb is: "Postfix binds stronger than prefix.", so you read int *tab[] as array of pointers. "Postfix binds stronger than prefix" is the reason why you look right, then left, in "Golden rule".

Easy, isn't it? Now you know this post's title reads pointer to the function returning pointer to an array of pointers to functions returning pointers to integers. 10 is redundant in this case. (It's no 5 from here, I'm so lazy...).

Of course you'll find out that the Golden rule is an obvious result of sentences above. It's convinient to read declarations that way, but IMO it's very bad to actually think about declarations only in terms of now jump out of the parenthesis.

Friday, 1 February 2008

Vim stuff

Vim is great, but quite hard to master. My experience is that there usually is a lot of "how did you do that?" when working with another vimmer. There goes a random list of some useful commands I taught (or learned) recently:

^rX in command and insert mode.
Puts the content of X register. Typical use case: select something with * and then :%s/^r//new stuff/g

punctuation: ; and ,
repeats last tTfF motion, like everyone's favourite dot repeats last command

rectangle visual mode
^v or quadruple1 click. Often easier and faster, than macros

macros
q. Everyone know them :help q

^o
Jumps intro normal mode for one command. Typical usecase: editing middle of the line ^oo.

^v any_key in insert and command mode
Puts the Vim representation for that key. Useful for editing .vimrc

J in normal mode
joins current line and next line

^a ^x in normal mode
increment/decrement a number

& and \1, \2...
in to part of substitution

"*
X11 clipboard

^p ^n and ^xlot_of_keys like ^xf
completions

autocmds in your .vimrc
see .vimrc examples below

not a command: Wombat
My text is brilliant
My vim is pure.
I saw a Wombat.
Of that I'm sure.
[...]

You're beautiful. You're beautiful.
You're beautiful, it's true.




Some macros from my .vimrc that I found very useful:

" done with current line
inoremap <c-cr> <c-o&ht;o

"XML, HTML, JSP
autocmd FileType xml,html,xhtml,jsp,htmldjango inoremap <C-b> <Esc>"tciw<<C-o>"tp><CR></<C-o>"tp><Esc>O
autocmd FileType xml,html,xhtml,jsp,htmldjango inoremap <C-a> <Esc>"tciw<></><Esc>"tPF>"tPla
autocmd FileType xml,html,xhtml,jsp,htmldjango inoremap <C-s> <ESC>"tciw</><ESC>h"tPa<Space><Space><Left>


Side note: python syntaxfile file (at least in vim 7.0 in my SuSE) seems to be a little outdated:
autocmd FileType python syn keyword Constant True False None
autocmd FileType python syn keyword Keyword with


Meta tip:
type :help one_or_two_random_keys from time to time. prefix with i_ for insert mode stuff.


1 - yes. QUAD-ruple. Click, click, click, click. Four clicks in a row %-| . Open Office does have quadruple click too. Who uses mouse, anyway...

Monday, 14 January 2008

DSL for CppUnit tests

It's amazing what activities can one invent, just to have something to do but learn...
I've just written an external DSL to ease writing CppUnit tests. It's not finished, but it works.

There is one thing I particularly hate about CppUnit: you have to name every test in fixture at least twice - once when it's created, and another time when it's added to the suite. OK, it's C++, we don't have reflection to do this stuff automatically, preprocessor is to dumb to fix it too... but it still sucks. It's repetitive and error prone. And you can end with tests that are never called, because you have forgotten to add them to suite.

Blah. I think I'm clear.

I wanted to have something simple - generated code should be very similar to our source - tests bodies themselves should be just copied intro right places and so on. No full-blown C++ parsers... no C++ parser at all. Just something that is a little more than preprocessor macros.

And here it goes:

@includeHeaders

@beginFixture TestSum
Sum *sum;
@setUp {
sum = new Sum();
}
@tearDown {
delete sum;
}
@test "empty sum of nothing should be zero" {
CPPUNIT_ASSERT_EQUAL(0, sum->getResult());
}
@test "simple sum of 1+2+3" {
sum->add(1);
sum->add(2);
sum->add(3);
CPPUNIT_ASSERT_EQUAL(6, sum->getResult());
}
@endFixture

translates to

#include <cppunit/TestCase.h>
#include <cppunit/extensions/HelperMacros.h>

class TestSum: public CppUnit::TestFixture {
CPPUNIT_TEST_SUITE( TestSum );
CPPUNIT_TEST( empty_sum_of_nothing_should_be_zero );
CPPUNIT_TEST( simple_sum_of_1_2_3 );
CPPUNIT_TEST_SUITE_END();

public:
Sum *sum;
void setUp() {
sum = new Sum();
}
void tearDown() {
delete sum;
}
void empty_sum_of_nothing_should_be_zero() {
CPPUNIT_ASSERT_EQUAL(0, sum->getResult());
}
void simple_sum_of_1_2_3() {
sum->add(1);
sum->add(2);
sum->add(3);
CPPUNIT_ASSERT_EQUAL(6, sum->getResult());
}
};

Simple as that.

Mercurial repo.

Time to finish this longish post before it took me more time to blog about it, than to actually write it...
cut!
My first post on my first blog is finished. Applause!