Java Gripes

I, for my sins, am now programming in Java. By now I have a lot of experience programming with a pretty large number of programming languages. Java, C++, C, Basic, Visual Basic, SQL, Python, Perl, php, Matlab, etc.

While I have to admit to having a prejudice against Java as YAPL (Yet Another Proprietary Language), I knew that in all likelihood I’d be working in it for some time, and I did my best to reserve judgement and find what is good in the language.

By now though, I think I’ve gotten good enough in Java to have an informed opinion. While Java has its strengths, and it could certainly be worse (it could be visual basic for example), I’m generally unhappy with it. I think the big problem I have with Java is it marketed to be used in application domains where it simply isn’t right choice. I suspect that for medium sized projects, free projects, projects which require a heavy network integration or should run as an applet, it’s probably a good language. For certain problems I would even believe that it’s the best choice.

But for large, complex projects, in particular projects in which you want to sell a binary and not sell a source code, forget it, Java’s a nightmare. But I don’t want to get into an encyclopaedic discourse of the ills of Java. I actually just want to use this forum to vent my frustrations with the language, and keep a kind of running diary as problems cross my mind. I suspect it will be useful when I need to explain to someone why I don’t care for a language. Normally when I get involved in such conversations my frustration is to palpable for me to explain my concerns lucidly: I’m to busy trying to get my head above the frust.

For today, let me just gripe about how Java has too many advocates. I suspect that this ill, along with most of Java’s ills, stem from the fact that java is owned by a corporation which expends a lot of money and energy trying to brand the language, generate a community, and in general get people feeling all tribal about being a Java programmer. Consider this blurb on the back of “Killer Game Programming in Java” which has on it’s back cover blurb “As a result [of poor documentation] Java has become a second-class citizen to C, C++, and assebly language when it comes to hardcore game programming. This book changes all that…”

Ugh. That’s just typical of the Java world. The book jacket isn’t trying to sell a book, it’s selling Java to a problem domain. The fact is Java isn’t the right choice for a “hardcore” game. My understanding of the term “hardcore gaming” is pretty much, by definition, to be “resource intensive”. That means efficiency and complexity (both algorithmic and code-complexity) are an issue, and that’s where Java just isn’t a good choice. Not only that, but games tend to be commercial undertakings, which means selling binaries and license management etc, which means obfuscation, which means you can’t break your code down properly into modular units and libraries. Generally if it’s really computationally intensive, you want to manage your own memory…

Now, I have to use Java for a 3D application. I have no choice. So I’m happy to have the book and I’m sure it will be useful to me. I’m also sure that there are a number of applications for which Java’s 3d apps are a good choice (smart phones, web apps, free software projects), but trying to convince the reader that the only thing keeping Java from being as good a platform as C++ and assembly is the documentation, well that’s just disingenuous shilling. The Java community is like the P.T. Barnum of programming communities.

Atheism and the holidays

Well, Switzerland just passed an anti-minaret initiative, which forbids the construction of new minarets in Switzerland.  It was a very dissappointing result.  Preliminary polls indicated that only 36 percent of Swiss favored such a law, which leads  me to believe that the racist, fear-mongering, irrational and emotion-based ad campaign was successful.  Members of the EU made public statements to the effect that ‘some issues shouldn’t be up to the people”, in other words, Switzerland suffers from an excess of Democracy.

Well, sometimes Democracy leads to poor results, but so does every other form of government.  Who decides what gets to go to the people?  Would Switzerland be better off with a top-down psuedo-democracy like the United States, where the corporations reign so supreme we’re struggling to impose some oversight on health insurance providers?  Nuts…

Now me, I’m almost okay with the minaret ban.  The only real problem is, it’s discriminatory.  The SVP claim that there is a need to  ban minarets because they symbolise political power of a religion.  I actually agree with that completely.  However, to be fair, we need a ban on all new religious towers.  Band the goddamn church towers I say.  I can’t tell you how many people I know who get awoken by goddamn church bells every weekend.

Bettina was so upset she started talking about moving to the states.  Well, if we ever fix our health care system, that might be something I would consider.  Certainly I don’t think such a ban would stand up in America, but that’s largely because we have such an undemocratic system of government, where the laws scarcely reflect the will of the people.    On the other hand, religious tolerance is a deeply engrained ideal in America, so perhaps a similar vote would turn out differently there.  I’m not entirely sure.

America does idolize religious tolerance and freedom.  You have the freedom to believe whatever you like, as long as you choose to believe some irrational bullshit based on some book written by a lunatic or a charlatan at least a hundred years ago.  It’s perfectly acceptable to believe that god screwed some virgin 2000 years ago, that the earth is only 4000 years old, that jesus actually lived in the states at some point and that the indians are the lost 13’th tribe of Israel.  But god forbid you believe in reproducibility and the evidence of your senses.  God forbid you believe in rational thought.  If you conclude that the idea of a cosmic father figure is kind of silly, well, apparently you’re as bad as Hitler in America’s eyes.

Think I’m exagerating?  Maybe.  The NYT has a nice article about secular humanists, who are conducting a billboard campaign.

“We don’t intend to rain on anyone’s parade, but secular people celebrate the holidays, too, and we’re just trying to reach out to our people,” said Roy Speckhardt, the executive director of the American Humanist Association. “To the degree that we are reaching out to the godly, it’s just to say that you can be good without god. So their atheist neighbor down the street shouldn’t be vilified as though he is immoral.”

There have been some interesting responses:

The head of the Catholic League linked secular humanists to figures like Hitler and the serial killer Jeffrey Dahmer. The publisher of “Santa Claus Is Coming to Town” complained about the signs. In Cincinnati, a billboard that said “Don’t believe in God? You’re Not Alone” had to be moved after the owner of the billboard property said he had received threats. In Moscow, Idaho, a sign that said “Good without God. Millions of humanists are” was vandalized twice in three weeks.

Man, so secular humanists are like Hitler and Jeffrey Dahmer. I found the following bit particularly humorous:

“It is the ultimate Grinch to suggest there is no God during a holiday where millions of people around the world celebrate the birth of Jesus Christ,” said Mathew D. Staver, founder and chairman of the Liberty Counsel, a conservative religious law firm, and dean of Liberty University School of Law in Lynchburg, Va. “It is insensitive and mean.”

I just want to mock the guy for using the word “Grinch” in such a dumb way, and to further mock him for working for the “Liberty University”, and being chairman of the “Liberty Council”. How very newspeak.

You know, as an atheist I’m constantly subjected to religious propaganda, in advertisements, in television shows and movies, at people’s weddings… recently I was at a wedding where the priest recited that verse that goes “and the lord god gave us dominion over the earth and all the animals and all the fish and the sea… be fruitful an multiply…”. Is there a more harmful belief in this day and age, than to think that we should dominate the planet, that we need more children? What we need is a little more respect for our fellow living creatures, rather than more dogma saying we’re somehow special and it’s okay for us to do whatever we like to the little fishies, as they are ours to do with as we see fit.

On the other hand, I’m encouraged that the percentage of Americans identifying themselves as having no religion has more than doubled since 1990. I think we can thank the religious right for giving religion such a bad name, and encouraging people to start thinking for themselves.

An open letter to Antonio Maria Costa

Antonia Maria Costa, the UN “Drug Czar”, has written a letter to the guardian asking “How many lives would have ben lost if we didn’t have controls on drugs”. It riled me up enough that I had to shoot off a response:

One has to wonder if Mr. Costa believes the nonsense he is promulgating, or he is simply performing his function as a propaganda minister for the prohibition industry to the best of his abilities. Both could reasonably be the case.

He posts the question

How many lives would have been lost if we didn’t have controls on drugs?

and goes on to discuss the current policy of drug prohibition as though “control”, “prohibition”, and “regulation” were synonymous. They are, of course, not. We have systems of control in place for the regulation of of alcohol and tobacco, both of which are significantly more dangerous and more addictive than many drugs which are currently prohibited my most nations (for example Cannabis, LSD, Ecstasy). By comparing the effect of regulation of alcohol against the effect of prohibition of alcohol, we can easily see that we protect our citizenry better through regulation than we do through prohibition. Regulation results in less crime, less overdose, less underage use, in short more control than does prohibition.

The current prohibitionist policies and irrational assessments of drug harm result in a situation that is quite simply out of control, and this is what we in the “legalization chorus” object to. Or does Mr. Costa wish to make the claim that things are in control in, say, Mexico? Or the United States for that matter?

How many lives would have been saved if our children had been obtaining honest information about the relative harms of drugs, rather than dishonest propaganda? How many lives would have been saved if our addicts (I am writing as an American here) could have gotten treatment and counselling for their problems instead of getting labelled as a criminal and ostracised as a criminal? How many lives would have been saved if clean needles were freely available? How many lives have been lost in botched drug raids, in drug violence?

The facts, Mr. Costa, speak against you, and your rhetoric and word-play is too weak to obscure them. Of course we need controls on harmful substances. We in the chorus are not asking you to stop controlling potentially harmful substances. We are asking out governments to regulate them. We simply want sane and rational regulation, rather than jingoistic prohibition.

Exit strategies for the war on drugs, part1: Framing the discussion

I am gradually of the opinion that drug-policy reform is now a sure thing, and the discussion will need to shift to alternative policies.  This is the first in a multi-part series, in which I prattle on about what comes next after the war on drugs.  This post attempts to formulate a useful basis for the discussion of the subject.

The Guardian has an excellent article: Prohibition’s failed. Time for a new drugs policy. The first line sums it up perfectly “http://www.guardian.co.uk/commentisfree/2009/sep/06/editorial-drugs-policy-latin-america”.

It’s clear that the debate now needs to be about what comes next.   We’ve created a stupid war against the citizenry our own country.  It’s completely fucking up our civil liberties, and in fact the entire premise is completely unconstitutional. Argentina’s government has realized this, and if we lived in a healthier democracy, we would have figured out the same thing by now. The good news is we seem to be getting there, so the time for figuring out an exit strategy would seem to be now.

The issues aren’t simple. We have a monstrous police-state machinery in place. We have to pull out the troops and integrate them back into society, and provide them with counselling to reintegrate them into normal society. While this should be an easy sell, as there is a peace-dividend (reduced spending on law-enforcement and prisons, improved civil liberties, reduced crime…) the drug-warriors don’t want to give up sucking at the government teat, and form a powerful lobby. The most difficult question of course is “okay, prohibition doesn’t work, what now?”.

Unfortunately, the people who should be working on this are still too afraid to admit prohibition has failed.  While they get up to speed, the most productive discussions in this arena are taking place online, in in the periphery of other discussions. I’d like to discuss the issue more directly.

Goals:

So, let’s identify some (hopefully) uncontroversial goals, by which we can judge whether a drug policy is working or not.

  • minimize addiction rates.
  • minimize overdose deaths.
  • protect children and uninformed consumers.
  • minimize crime (e.g. junkies stealing to get their ‘fix’)

There are other effects which are more difficult to quantify, such as health impacts (cancer and such) and effects on productivity. While these are worth considering, I think it’s a reasonable approach to consider them second-order effects. Once we have a policy which optimizes the easily measured first-order effects, we can worry about the second order ones. The key thing to keep in mind here is prohibition is a nightmarish failure, regardless of which effects you consider. It doesn’t accomplish any of the desired effects. The results of prohibition are so disastrously bad, that complete deregulation might end up working just as well, without the enormous cost (socially and economically) of funding the war.

An error the drug warriors make is framing the discussion in terms of “zero-tolerance”.   They want to completely eliminate all drug use.  What the last 100 years has shown is that that won’t happen. You can keep spending more money, you can keep use the constitution as toilet paper after shitting on people’s civil rights, you can get more and more violent and intolerant, you can impose increasingly draconian laws, and people will still use drugs. The figures are there.  It takes enormous cognitive dissonance to deny them, so let’s stop doing

There remains of course the question of how much we are willing to pay to achieve those goals. I suspect that the people who are so willing to spend billions on the drug war, will be less willing to spend the same billions on counselling, care, rehabilitation, education, and maintenance programs. Fortunately, the drug war has been so damned expensive, anything we come up with likely be much more effective at a greatly reduced financial cost.  This will allow us to frame all such harm reduction spending in terms of savings over the prohibitionist approach.

Having identified a set of goals which I hope we can all agree on, let us consider what will be needed to implement a sane drug policy.  It’s my conviction that a good drug policy will involve the following components.

  1. Rational evaluation of drug harm.
  2. Honest drug education.
  3. Honest drug scheduling (a rational classification system).
  4. A sane handling of the respective classes of drugs.
  5. Reality based assessment of policy effects.
  6. More power to states and communities for deciding drug policies.

Each of these points is non-trivial, and will require some discussion.  Thus they will be the subject of future posts.

Some might disagree with necessity of a drug scheduling system at all, and would advocate regulating all drugs like we do alcohol.  While I see some merits to such an extremely libratarian approach,  I would argue against pursuing such a goal for the following reasons:  It’s unrealistic in today’s political climate, it’s too rapid and extreme a change, and I suspect such a policy might be nearly as harmful as the current policy.  If it’s not clear to me, it’s going to be extremely unpalatable for the average citizen.

Keeping the classification system allows to handle the approach in a more reasonable and rationed manner.  We can agree to pursue a policy that accomplished the stated goals, and analyse each drug case by case, based on a rational assessment of its relative harm, made by qualified medical researchers. It also allows us to separate the questions “do we need drug policy reform”, and “what is a good drug policy for drug X”.  The answer to the former question is simple, the answer to the latter is, in some cases, rather difficult.  For example, I am torn on what constitutes a good policy for Heroin or Crack (I do know that current American policies are the wrong answer, but I’m not sure heroin and crack bars are the right answer).

Conclusion and caveats:

To successfully advocate for drug policy reform, I think keeping the above goals in mind is extremely useful.  It provides a concrete, uncontroversial framework for evaluating the failure of current policy, and provides some useful indications for steps in a positive direction.  There may be additional goals which are useful to bring into the discussion, but in the terrible situation we currently find ourselves in, we should strive to work toward unifying, uncontroversial goals.  Once these are acheived, we can open up more controversial, difficult discussions, such as “what right does the government have telling me what I can put in my body anyway”, or the ethical merits of a drug-free lifestyle versus the spiritual benefits of psychotropic drugs.

Bing

Youngsters to the web might not remember a time before Google, where search engines basically sucked. They were based on simple and stupid algorithms which were easily gamed, and only occasionally got you to a site you wanted to visit. Google was so successful because their search engine worked vastly better than the competitions. Back then, if you were looking on information on a particular subject, you would go through a list of search engines, until you got a useful hit. There were even meta search engines which agglomerated the results of several search engines. These all died out quickly after google went up.

Since Google, pretty much everyone I know just uses Google. If a particular search is unsuccessful, we try experimenting with different terms. I don’t think I ever go to another search engine.

But I try to be open minded. So when I heard that Bing didn’t suck, I gave it a shot. I picked 5 searches I might be interested in and search in Bing and Google. I consider it effectively a tie. Sometimes Bing’s results were better, sometimes Google’s, but in neither of them did I get a bullshit result that was clearly inferior to another’s.

So chapeau Microsoft. You appear to have gotten your head out of your ass and produced a respectable product. If you’re as successful with Natal as you were with Bing, you might even get me to buy one of your products!

What the Sotomeyer confirmation hearings have to tell us about the drug war.

So, people who are well informed about drugs, and have bothered to look over the medical evidence, are perfectly aware that X (MDMH) shouldn’t be a schedule 1 drug.  This is of course no surprise, since our drug scheduling system looks as if it was chosen by throwing darts.

Why is it illegal though?  The brief history is that MDMA was created by a guy called Anton Koellisch, working for the pharmaceutical Merck in 1912.  Apparently they were trying to develop a substance to stop abnormal  bleeding.  MDMA was just an intermediary compound on the route to methylhydrastinine, and Merck wasn’t interested in its properties.

Now, the next part is fascinating.  Apparently recreational drug users first determined the drug was worth taking, around 1970.  This led to a guy called Alexander Shulgin (who would have been around 50 at the time, working (I believe) as a p0st doc at UCSF to play around with it.  Apparently he called it his “low-cal martini”.   He in turn metioned it to a psychotherapist named Leo Zeff.   MDMA is very useful for  enhancing communications, reducing psychological defenses, and increasing the capacity for therapeutic introspection.

In the early 80’s MDMA started to catch on.  All the kids who grew up playing pac-man started hanging around in dark-rooms, listening to repetitive electronic music, and popping pills.  One of those pills was MDMA, and the Reagan-Bush “Say no to drugs”, “Say no to condom  education in schools”, “say no to Foreign aid unless you teach that abstinence is the only way to prevent aids, and forbid teaching about safe-sex” administration decided to say no to young adults having a good time and feeling more empathic to each other.

MDMA is an empathogen.  I don’t know if this is the accepted medical phrasing for what MDMA does, but take it from me, MDMA is an empathogen.  It makes you care more about other people.  This can be negative.  For example, if I have a single sixteen year daughter, I would discourage her from takine X at  a party full of strangers.  I would however encourage her as an adult to take with her partner.  It makes relationships richer and stronger, I promise you.

I have an informal ranking, which I call a drugs “Scary Monster” level.  It’s how much fear a drug evokes in a particular audience or demographic.  X is one of the more maligned drugs out there.  I think the average, over 40 American, and possibly european, sees X as worse than Cocaine, for example, which certainly doesn’t fit its ranking in rational harm rating.  My point here is, beyond being illegal (which it course should not be), it is villified disproportionately to the harm it causes.

For example, when Professor David Nutt, head of Britain’s drug advisory board recommended that ecstasy be downgraded to a class B drug, he tried to explain the relative risk inherent in taking the drug as being equivalent to the risk you take in riding a horse.  The point was to get people to understand that drug harm can be equal to harms in other parts of life.  This is an importnat thing to discuss, since the debate on drugs is typically so emotional and irrational, particularly on the side of the prohibitionists.   He wrote an article titled “Equasy: an overlooked addiction with implications for the current debate on drug harms”.  Equasy being “Equine Addiciton Syndrome”, which has caused ten deaths, and causes more than 100 road traffic accidents a year.  He goes to explain that many other activities in life (like motorcycling) are much more dangerous than man illicit drugs.

The prohibitionists were up in arms, calling for his resignation.  They can’t attack his science or his reasoning, both of which are flawless.  They instead must make emotional attacks, claiming he is insensitive to the families of victims of drug abuse.  Sigh.  So victims of drug abuse suffer more than victims of horse riding?  Why?  The fundies react very badly to rational discourse on the relative risks of drugs.  Why is that?

I’ve long had the opinion that the problem is simply fundamentalism.  They see drug use as a threat against their culture.   There’s no question that this is the case for the drug policies of the Nixon, Reagan, Clinton and Bush administrations.   I also think this is part of the particularly irrational attitude towards X.  It makes you feel more empathy, and fundamentalists are anti-empathy.

Now I feel affirmed.  Warren Richey writes about the Sotomeyer Hearings that

Republicans Question Need for “Empathy”

Republicans repeatedly criticized President Obama’s stated goal of seeking judges with “empathy” for “people’s hopes and struggles.” They questioned how the concept of empathetic judges could coexist with the well-known portrayal of a blindfolded Lady Justice.

Finally I have proof that  the sector of our culture which represents religious fundamentalism and intolerance is genuinely anti-empathy.  I knew they were of course, as did a great many people.  But now they are going so far as to say it openly, which means I can make this without a burdensome process of proof.  It’s easy, they don’t deny it. We don’t need empathy in the justice system? That’s some fucked up shit right there.

I think the fundies are anti-X in part because they are anti-empathy, and X encourages empathy. Used under the proper conditions it does so in a lasting and meaningful way. Criminalizing it only encourages its use in harmful locations, and decreases its positive use.

If we consider our culture’s evolution, I think there’s a real possiblity that empathogens and psychadelics can play a meaningful role in hunting down and de-clawing harmful social memes. Thinking about it like that, religious intolerance and flexibility, which is so symbolized by the modern Republican party, is like a mind virus.  Lack of empathy in our culture can be treated effectively with MDMA (as part of a directed therapeutic process), and prohibition of MDMA is a defence mechanism of the anti-empathy meme.

When one goes further to consider that addicts and people who do abuse drugs are people with medical conditions, deserving of our empathy and support, it’s clear that entire concept of drug criminalization is a defense mechanism against empathy.

Micheal Jackson is Dead!

You know, thinking about MJ dying does make me sad. But the real tragedy isn’t his death, it was his life. All these people saying “we should remember him as the great entertainer he was” would be better off trying to learn a lesson from his life. What is that lesson? I don’t know, but I guess it has something to do with the trappings of fame and wealth, and the dangers of not looking inward.

Fedora 11 Post Install Configuration

Fedora 11 is out, so it’s time to update my notes on configuring my fedora install. In the examples I will use a default Fedora install (with development tools selected), on my 64 bit Sony vgn-s2750N laptop, which I will henceforth refer to as Sunny.

Here’s what I have to do to get Fedora 11 running like I want it:

  1. Fix the DNS lookup bug. On all the machines I administer, this manifests itself as massive dns lookup failures, with the effect that although you can ping an address, you don’t have any internet access (no web browser, no yum…).  This answers the question:  I have an internet connection, but I can’t use the web, WTF?
  2. Access to fusion
  3. Add MP3 support/get Amarok working.
  4. Get Flash working (people need their youtube).
  5. Graphics acceleration
  6. Make FAT partitions writeable by users, and add ntfs support.
  7. Disable physical file folders.
  8. Enable Ctl-Alt-Backspace

1. Fix the DNS bug
Apparently there is a known bug, which mucks up the domain name lookup with certain ISP’s, of which bluewin (my ISP) is one. In the bug description the complaint is that you get unreliable name lookups, but in the case of bluewin (my isp), you get no successful lookups.  A workaround is:

  1. Find out the network interfaces the machine has using the command “route -n”.
  2. Create a file:  /etc/dhclient-< your network interface name here >.conf consisting of the line
    prepend domain-name-servers 127.0.0.1;
  3. Start dnsmasq (‘service dnsmasq start’).
  4. tell dnsmasq to start every time the computer does (‘chkconfig dnsmasq on’)
  5. restart the network connection (‘service NetworkManager restart’)

So on Sunny the Sony I want to get my wireless LAN working right on Bluewin. Running ‘route -n’ tells me my network interface is ‘wlan0’ (which I could have guessed). So I do the following (as root of course):

echo 'prepend domain-name-servers 127.0.0.1;' >  /etc/dhclient-wlan0.conf
service dnsmasq start
chkconfig dnsmasq on
service NetworkManager restart

And presto, my internets work again. I don’t put it on this list, but at this point I run a ‘yum -y update’ to get the base install up to date.

2. Access to fusion:
Fusion is a merge of the largest existing addon repos, and means to be the extra repo for fedora, including (separate) free and non-free packages that Fedora is not able to ship of license or export regulations (see comment by ingvar).  Apparently it is now possible to add fusion support through a GUI, but I find it much more efficient to just do:

rpm -Uvh http://download1.rpmfusion.org/free/fedora/rpmfusion-free-release-stable.noarch.rpm http://download1.rpmfusion.org/nonfree/fedora/rpmfusion-nonfree-release-stable.noarch.rpm

This is a good time to do another yum update. You’ll have to accept a couple of no-key warnings.

3. mp3 support.
I still use Amarok, which I am still unsure about recommending. I found Amarok 1 vastly superior to the alternatives however, so I’m hoping Amarok 2 eventually becomes awesome. In addition to Amarok, I want lame for when I rip my CD’s for my car mp3 player, mp3 support for Totem, etc. So I do the following:

yum -y install amarok lame* gstreamer-plugins-ugly xine-lib-extras-freeworld

And things seem to be running all right.

4. Get Flash (i.e. Youtube) working This solution comes from here

rpm -ivh http://linuxdownload.adobe.com/adobe-release/adobe-release-i386-1.0-1.noarch.rpm
rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-adobe-linux
yum install nspluginwrapper.{i586,x86_64} alsa-plugins-pulseaudio.i586
yum -y install flash-plugin

Note that the third line is only required for 64 bit Fedora.

5. Enable your 3d hardware acceleration. In my case, I just run ‘yum install kmod-nvidia’ and restart X. If you have a radeon card, I suppose the solution is the obvious one.

6. Make the fat drive writeable, and add NTFS write support, so people can easily work with Windows.
For any fat partition, change the umask in fstab to 000. For NTFS support:

yum -y install ntfs-config.noarch

7. Disable “physical” file folders. This is the annoying behavior, default in gnome, that opens a new window for every folder that you open. Get rid of it by double clicking on a folder, and in the resulting window open edit->preferences->Behavior, and check the box for “Always open in browser windows”. There is a scriptable way to do this, so if someone wants to tell me, please do.

8. Enable ctl-alt-backspace.It always frustrates me when a distro moves away from supporting the power use to supporting the neophyte. I think there are plenty of neophyte oriented OS’s and distros around. I use Fedora instead of Ubuntu because Ubuntu aims too much at the dumb asses, and Fedora tends to support the people who want to learn and be efficient. Unfortunately the Fedora guys do make dumb-ass-friendly decisions, such as the decision to disable ctl-alt-backspace, which I find to be a very poor decision. Who hits this key sequence by accident? Anyway, to enable it in Fed 11 do System->Preferences->Keyboard, choose keyboard layout options and enable the checkbox for “key sequence to kill the x server”.

Exporting emacs org-mode html, ain’t sed grand.

I’ve recently begun using emacs org-mode, and I quite like it.  I do a lot of my writing, some of which might finally end up here on my blog, in org-mode these days.  For a lot of my applications it’s a perfect overlay on plain text documents.  I can export what I’ve written as html or latex, which is fantastic.

What’s less fantastic, is when I want to cut and paste the html output here to my WordPress blog.  Unfortunately org-mode puts in linefeeds between paragraph elements, and for some reason wordpress maintains these, resulting in incorrect word wrapping.  So I want a way to remove the the linefeeds between paragraph elements.

This was just a little bit beyond my capabilities with SED, and I’m often telling myself self, you should really learn how to use sed and regex terms better . So I thought bugger it, let’s figure out how to do this. So I whipped out the excellent book “sed&awk” from O’Reilly.

As someone who has only used sed for banal substitutions, I had to learn the following:

  • “:whatever” can be used to create a label.  There are two commands that allow you to utilize these lables: “b” creates a branch, while “t” jumps to a label if a successful substitution has been made on the currently addressed line.
  • “N” is needed to join two lines, since sed normally works on a one-line-at-a-time fashion.

With these two tidbits, and a basic understanding of how sed operates, we can construct the desired script.

:top
/

/ {:loop N s/\n/ / /<\/p>/{P;D;btop} bloop}

In one line the command looks like this:

 sed ':top;/<p>/{:loop;N;s/\n/ /;/<\/p>/{P;D;btop};bloop}'

If you’re like me, it’s not immediately clear what’s going on here, so let’s break it down:

  • First we create a label with “:top”.
  • “/<p>/” tells sed to look for the paragraph block tag.  The next ‘line’ of the script will be called after this tag is found.
  • The curly braces “{}” group a set of commands, so upon encountering the paragraph tag, it executes the contents of these brackets.   In the brackets:
    • Create a new label “:loop”.
    • “N” creates a multiline pattern space by reading the next line of input, and appending it to the contents of the pattern space.
    • “s/\n/ /”: substitute a space for the line feed.
    • “/<\/p>/{P;D;btop}”:  If sed encounters an end of paragraph tag, it executes “P;D;btop”, which (P) prints the contents of the multiline pattern space, (D) deletes it, and (btop) creates a branch(b) and goes to the label(top).   It’s a little like “if (<p>) goto top”.
    • “bloop” (b) branch and goto label(loop).

So as long as no closing tag (</p>) is found, we have a loop that keeps adding new lines to the multiline pattern buffer, and substituting spaces for linefeeds.  When the closing tag (</p>) is found, the loop goes back up to the “top” label.  That loop makes sure all of the  paragraph sections get handled.

So that’s it.  If anyone knows a more elegant solution to this, I’d be glad to here about it.