Tuesday, October 13, 2015

How to customize x11/sterm on FreeBSD

Looking for a more lightwieght and minimal replacement for rxvt-unicode, one that supported more unicode than urxvt, I stumbled upon Termite. I was trying to get Termite to install, but had 20 show stopping errors at the last part and decided to check around for something else. I stumbled upon st from suckless. I'm already using dmenu, and I liked their philosophy, so if they made st, why not give it a shot.

As a side note, I was trying to figure out how to get st installed on FreeBSD 10.2 and almost gave up, until by complete accident, I saw x11/sterm, and decided to give it a try, and lo and behold, checking it out on portmon it said the projects home page was..dun dun dunnn..st.suckless.org. x11/sterm was FreeBSD's port of st! Install. Now.

Okay great, I installed and got the famous termcap error. Solution was to apply the TERM="xterm=256color" fix. Great, done. It's installed and working, but urxvt is still looking sexier.

st isn't picking up my .Xdefaults file. Okay, so how do I configure it? I have to create my own config.h, edit it to suit my needs and recompile it in...whuh?

I won't bore (or amuse) you with my trials and error. This is for others out there who were lost like me, and for my future self to save me time.

This is how to generate your own config.h:

  1. cd /usr/ports/x11/sterm && make extract
  2. This is my preference, you can get it this way or get it another way by cloning st from github. I prefer to get it from the extract method as my way of ensuring I'm up to date with the FreeBSD port. Copying the extracted directory also has an added bonus as we'll see later on. cp work/st-0.6 /home/Jason/.config/ You can rename it st or keep it as st-0.6, you can also copy the folder wherever you like, I just prefer .config to keep things sane.
  3. Include the newly copied st directory into your dotfiles or git repo. That way if you have to upgrade, or move to a new computer, you will always have your personal settings saved.
  4. cd into the new st directory and cp config.def.h config.h
  5. Modify config.h to your hearts content.
  6. Then when you're finished, go back to /usr/ports/x11/sterm.
  7. Important, if sterm is already installed, please deinstall it. make deinstall clean Also note that whenever clean is run, you lose the work directory.
  8. make ST_CONF=/home/Jason/.config/st-0.6/config.h install clean
  9. Test out st and witness your changes.

So, I already applied my newly found bestest and mostest eye pleasing preferred font of Hack size 14 with antialiasing set to true. I already applied the FreeBSD fix of setting the TERM to xterm-256color.

Which was simple enough as all I had to do was reinstall with the ST_CONF option.

Looking for a more lightwieght and minimal replacement for rxvt-unicode, one that supported more unicode than urxvt, I stumbled upon Termite. I was trying to get Termite to install, but had 20 show stopping errors at the last part and decided to check around for something else. I stumbled upon st from suckless. I'm already using dmenu, and I liked their philosophy, so if they made st, why not give it a shot.

As a side note, I was trying to figure out how to get st installed on FreeBSD 10.2 and almost gave up, until by complete accident, I saw x11/sterm, and decided to give it a try, and lo and behold, checking it out on portmon it said the projects home page was..dun dun dunnn..st.suckless.org. x11/sterm was FreeBSD's port of st! Install. Now.

Okay great, I installed and got the famous termcap error. Solution was to apply the TERM="xterm=256color" fix. Great, done. It's installed and working, but urxvt is still looking sexier.

st isn't picking up my .Xdefaults file. Okay, so how do I configure it? I have to create my own config.h, edit it to suit my needs and recompile it in...whuh?

I won't bore (or amuse) you with my trials and error. This is for others out there who were lost like me, and for my future self to save me time.

This is how to generate your own config.h:

  1. cd /usr/ports/x11/sterm && make extract
  2. This is my preference, you can get it this way or get it another way by cloning st from github. I prefer to get it from the extract method as my way of ensuring I'm up to date with the FreeBSD port. Copying the extracted directory also has an added bonus as we'll see later on. cp work/st-0.6 /home/Jason/.config/ You can rename it st or keep it as st-0.6, you can also copy the folder wherever you like, I just prefer .config to keep things sane.
  3. Include the newly copied st directory into your dotfiles or git repo. That way if you have to upgrade, or move to a new computer, you will always have your personal settings saved.
  4. cd into the new st directory and cp config.def.h config.h
  5. Modify config.h to your hearts content.
  6. Then when you're finished, go back to /usr/ports/x11/sterm.
  7. Important, if sterm is already installed, please deinstall it. make deinstall clean Also note that whenever clean is run, you lose the work directory.
  8. make ST_CONF=/home/Jason/.config/st-0.6/config.h install clean
  9. Test out st and witness your changes.

So, I already applied my preferred font of Dejavu Sans Mono size 12 with antialiasing set to true. I already applied the FreeBSD fix of setting the TERM to xterm-256color.

Which was simple enough as all I had to do was reinstall with the ST_CONF option.

However, wanting to completely replace rxvt-unicode, I needed transparency. I am in luck, as st provides the ARGBBG patch. However my so called luck quickly dissipated like morning mist, as I realised I had no clue how to apply the patch. The more advanced users can now sit back in amusement at my ignorance.

This is how in my newbness I applied suckless patches to sterm.

In my local copy of st-0.6, I made the changes from the diff file...by hand. Yes, I painstakingly checked the diffs and applied the changes to the individual files.

Afterwords...

  1. # cd /usr/ports/x11/sterm
  2. # make deinstall clean
  3. # make ST_CONF=/home/Jason/.config/st-0.6/config.h extract
  4. # cd work/st-0.6
  5. # rm config.mk st.c
  6. # cp /home/Jason/.config/st-0.6/config.mk .
  7. # cp /home/Jason/.config/st-0.6/st.c .
  8. # cd ../..
  9. # make ST_CONF=/home/Jason/.config/st-0.6/config.h install clean
  10. Enjoy new transparent st.
  11. Find out that there is a better way as discussed in the Porter's Handbook.

If anyone has done it more efficiently, I implore you, kindly share it with us, so we can spend more time enjoying what st has to offer us and less time in the configs. Thanks in advance.

Sunday, August 23, 2015

Vim: Convert Markdown to HTML

This is pretty obvious, but I just started blogging, and I decided to dust off my old blogger account and use it for my posts, because, why not?

However, what has been bothering me is blogspots WYSIWYG editor, and while I can edit my posts in HTML, I am a bit too lazy to fight the editor. My process was to save rough drafts in Apache OpenOffice's native ODT format, then copy and paste the draft into blogspot's editor and 'polish' off the blog, switching between the Compose and HTML screens.

Then a thought hit me, why not blog in Markdown and convert to HTML, and post the HTML instead. Great idea, but how?

For almost a month now, I had made the switch from Emacs to Vim as my default editor and "IDE" of choice, so I did a search for available plugins and came across this awesome article, Vim Essential Plugin: Markdown to HTML

So I downloaded Markdown 1.0.1, unzipped it and placed it in my /usr/local/binand tested it out...

/usr/local/bin/Markdown.pl: not found

Whuh? Not found? Oh yes, I needed to ensure the file could find my perl, so I edited the first line in the file to reflect it's location.

$ which perl
/usr/local/bin/perl

I edited the first line of Markdown.pl so it looks like this #!/usr/local/bin/perl and I tested it from my terminal this time.

Markdown.pl --html4tags README.md

It's output was splendid, clean HTML. So I assigned my vim shortcut key, like so:

" Markdown to HTML
nnoremap <leader>m2h :%!/usr/local/bin/Markdown.pl --html4tags <CR>

I also downloaded the html2text.py script from Aaron Swartz html2text page.

That way I can now convert between the two formats within vim. Just follow the same steps as we did for Markdown.

As a sign off note, this is my first blog written entirely in Markdown, converted to HTML and published to Blogger!

Saturday, August 22, 2015

UZBL LastPass Bookmarklet

In my attempt to be as minimalist as possible, I came across a wonderful browser I never knew existed before a few days ago, UZBL. From their site:

Uzbl follows the UNIX philosophy - "Write programs that do one thing and do it well. Write programs to work together. Write programs to handle text streams, because that is a universal interface."

Uzbl comes with uzbl-core, uzbl-browser and uzbl-tabbed. For everyday web browsing, I use uzbl-tabbed, which in my dmenu setup, I created a script so that it shows up as just 'uzbl'. Less typing. Uzbl has modes, and keyboard bindings similar to Vi/Vim, so if you are already familiar with that editor, you are almost there.

Now in order to replace Opera, which was my default browser, and was very lightwieght, I needed a few things in place:

  1. Could it render the pages that I visited often and were important to me. CHECK[1]
  2. Does it support LastPass...not in the usual way other browsers do, via extensions.

After much searching on google, I came across an email thread where someone else had accomplished this. Okay, so it can be done, but how? Breaking down the email, I needed to understand two things, how to get the javascript code from LastPass and how to create a bookmarklet for uzbl.


A bookmarklet as it turns out is just javascript, well, that's a simplified explanation, but for this article, that's all that is relevant, and LastPass already has bookmarklets, all I had to do was log into LastPass and I was greeted with this message:

**You are using a browser that currently does not have a LastPass plug-in available.
This may result in reduced functionality. We recommend you use Firefox so you can use the plugin.
Install the bookmarklet if that's not an option.
Clicking the bookmarklet link will show you three options and their explanations. I chose to grab Fill and Fill Forms. Once downloaded, the next step is an adventure, how to actually create an uzbl bookmarklet, luckily someone already created a wiki page on how to do it.

Unfortunately, I could not understand it at first, and missed a few things, so it took me a bit longer to figure out. This is what I did to get it working for me:
  1. Saved LastPass Fill Forms as lastpass_bookmarklet to my home directory.
  2. Run the command from the wiki:

    $ cat lastpass_bookmarklet | sed 's/javascript://' \
    | python -c 'import urllib, sys; print urllib.unquote(sys.stdin.read())' \
    > lastpass_autofill.js

  3. $ mv lastpass_autofill.js ~/.local/share/uzbl/scripts/
  4. $ chmod +x ~/.local/share/uzbl/scripts/lastpass_autofill.js
  5. Edit ~/.config/uzbl/config and add the following line
    @cbind !lpaf = script .local/share/uzbl/scripts/lastpass_autofill.js

That's it, so whenever you need to autofill your login details, just go to Cmd mode and type the keybind you had set in step 5.

1 - As a bonus, I was also able to view sites that opera 12.16 blocked due to certification errors.

Friday, July 24, 2015

Vim: Rebind Caps Lock And Escape

In this article I discuss how I managed to bind Escape to my Caps Lock key and Caps Lock to Escape, not only for my OpenBox environment, but also for my Virtual Terminal/Console.

I recently decided to finally give Vi/Vim a try. Previously, I thought it was too arcane and tried Emacs instead. I was an avid Emacs user and it became my editor and IDE of choice. So why did I decide to switch?
My decision came down to three (3) things:
1. Why not?
2. Striving to be more productive (i.e. lazier...i.e. less keystrokes)
3. Vi is installed by default on Unix, so an advanced editor is always ready. So are ee, ed and a whole range of others, but I like the challenge and wanted to see what the rage was all about. Yes Emacs came with Mac OS X by default albeit an older version, so I still had to install the latest. On FreeBSD? pkg install emacs is your friend.
While using Emacs on OS X, I could easily remap the keybindings for Caps Lock to Control, in fact, it was a builtin feature of the OS. It's a bit more work to get it done on FreeBSD, so here goes.

OpenBox setup:

This was taken from the vim wikia:

  1. vi ~/.vimkeyswapper – you can name the file anything you want, I just wanted the easy reminder everytime I looked at it. This was easier to understand at a glance over '.xmodmaprc'.
  2. Edit the file to include the following contents:
    ! Swap CAPS LOCK and ESC
    remove Lock = Caps_Lock
    keysym Escape = Caps_Lock
    keysym Caps_Lock = Escape
    add Lock = Caps_Lock
  3. Edit either your ~/.config/openbox/autostart if you are using a login manager or your ~/.xinitrc if, like me you only need to start X when you really need to
    xmodmap ~/.vimkeyswapper
  4. Restart X, or reboot just to make sure it takes effect.

Console setup:

Alright, be careful here. We're going into the file system, which requires root privileges. Ironically, I found the original solution here.
  1. We need to decide which keyboard mapping we are using. So you'll need to install misc/kbdscan if you don't already have it installed.
    $ cd /usr/ports/misc/kbdscan && sudo make install clean
  2. Please note, you have to be in the console to be using this, so you can't be logged into X and use your terminals. So ALT+F1 or ALT+F(n) to whichever console you like and login.
    $ kbdscan
    Press and release Caps Lock and make a note of which number appears, for me it was 58
    Press and release Esc, the number I got was 1.
  3. Okay, you may have to do some digging around to find which files match the numbers you got.
    $ cd /usr/share/syscons/keymaps && ls
    Based on your locale, language or keyboard setup, it may be different for you. The file that matched my bindings were us.iso.kbd. [1]
    $ sudo cp us.iso.kbd us.vim.kbd
  4. Edit the file, so it should look something like this:
    # $FreeBSD: releng/10.1/share/syscons/keymaps/us.iso.kbd 74119 2001-03-11 23:41:19Z ache $
    # alt
    # scan cntrl alt alt cntrl lock
    # code base shift cntrl shift alt shift cntrl shift state
    # ------------------------------------------------------------------
    000 nop nop nop nop nop nop nop nop O
    001 clock clock clock clock clock clock debug clock O
    ...
    058 esc esc esc esc esc esc esc esc O
    [2]


    I don't know if it made a difference or not, but I realigned the text before saving the file.
  5. Add this to your /etc/rc.conf file: keymap="us.vim.kbd"
  6. Reboot: # reboot OR $ shutdown -r now
  7. And there you have it, if done correctly, your Caps Lock will now be your new Esc key. Happy coding![3]
[1] – On a side note, they already have us.emacs.kbd, no fair to us vim users :-)
[2] – Ellipses denotes more content, I just did this to highlight the relevant changes. Please don't remove anything from this file.
[3] – I am not sure if this affects console only or the X environment as well, making the xmodmap command unneccessary.

Tuesday, July 21, 2015

FreeBSD 10.1: Cabal Update Version Issues

Sometimes there is an issue with % cabal update, where after updating and testing the update with % cabal --version, you will still see the previous version. I had this issue when I was running Mac OS X 10.9 and 10.10 but they were easily resolved by just pointing to the correct PATH, e.g: export PATH="$HOME/Library/Haskell/ghc-7.8.3-x86_64/lib/cabal-install-1.20.0.3/bin:$PATH"

On FreeBSD, it's also an easy fix, again by ensuring you set your $PATH correctly, but this time ensuring it's ~/.cabal/bin:/usr/local/bin: . An issue I found with this, is that during some cabal installs, I'd see an error message gcc -not found even though whereis gcc reports /usr/local/bin/gcc48. To resolve this, I set my $PATH thus: /usr/local/bin:~/.cabal/bin:, which lead to 'cabal --version' not displaying the correct version number.

On a side note, I could just use the --with-gcc=/usr/local/bin/gcc48 flag, but I'm too lazy to type all that and too forgetful to remember.

This is how I chose to resolve it.

% sudo mv /usr/local/bin/cabal /usr/local/bin/cabal-1.18.0.5
% sudo ln -s ~/.cabal/bin/cabal /usr/local/bin/cabal

Effectively, I renamed the original cabal to include it's version number, in case I may ever need it later, then I created a symbolic link which pointed to my user copy of cabal, which will always be the updated version. So, I can now do cabal updates and cabal installs knowing that gcc will always be found and I'll always be using the most up to date cabal version.

Monday, July 13, 2015

Installing Adeskbar on FreeBSD

Why?

On not so powerful hardware, squeezing every last ounce of juice from your limited resources are vital especially when you're a developer. Time is very important, and so waiting for a program to compile or watching your IDE slow to a crawl just trying to auto-complete the name of a function, or even watching your mouse cursor that you're used to blinking be a constant pixel on your screen, makes your soul scream as precious seconds waste away before your eyes.

So when I got a chance to finally install FreeBSD on my own hardware, I jumped on it. Unfortunately, said hardware isn't as powerful as I want it to be right now for my daily development, it's an Acer E 15 Start [1], but it's mine. So as much as I could, my X environment has to be as minimal as possible.

I know there are tons of Desktop Environments (DE) and Window Managers (WM) to choose from, I thought XFCE4 was lighweight enough as I had installed that on a previous PC-BSD setup I had, but that was on much better hardware, so it was blazingly fast, but on my current hardware, the Application Launcher menu took ~3 seconds to load on average [2], yes, the menu to launch my applications took a while to appear before I could launch my applications. Now what's the big deal with 3 seconds? I'm dual booting with Windows 8.1 [3] and Windows 8.1 on the same hardware is surpisingly and pleasantly responsive, enviously so, and I can't have my main OS FreeBSD be shown up like that. So I did a lot of reading and did a lot of testing and although I thought LXDE would have been faster, and at least not let me feel bad about jumping from XFCE4, which honestly, I do enjoy and would install it on anyones computer, LXDE for some reason just wasn't the same. BUT it was faster, and making this possible was OpenBox. OpenBox? What is that, I've never heard of it before...I did more reading and realized something. What if I just ran OpenBox by itself, how much more responsive could my desktop be? 

# cd /usr/ports/x11/lxde-meta && make rmconfig-recursive deinstall
# cd /usr/ports/x11-wm/openbox && make config-recursive install clean

I now had OpenBox and a gray background...Simple, minimal, divine. In the first time using an OS, I realised, everything else was unneccessary. However, after shaking myself out of it, I soon realised, there were somethings that were necessary for me to be and feel productive, especially if I wanted to replace Windows 8.1 and get over the feeling of missing the MacBook Pro I had been using for over three (3) years. So I moved on to see what others were doing with their FreeBSD + OpenBox setup and stumbled across this forum explaining how to have a minimal FreeBSD desktop. How I wish I saw this first, and how much more I wanted to do a full reinstall of FreeBSD, but after 2 weeks of struggling, and not wanting to waste any more time, I decided to make the best of it, so I opted for Tint2 and Wbar.

Wbar - minimal, lightwieght, easy to configure. I quickly found out it's limitation, and it was the one thing I needed, it doesn't support SVG, and doesn't plan to anytime soon, unless imlib2 decides to. I found this out from here.

I thought cairo-dock aka GLX-dock would have been the answer, and would give me that OS X feel, but after # make config-recursive install clean I realized how wrong I was. One hour of installation, and to top it off, my hardware doesn't support OpenGL, which means space on my HDD is being taken up for eye-candy that I can't see or utilize. I tried making the best of it and it worked after a fashion, but the bulkiness and underutilizatoin of it was getting to me. So back to google. I found this wonderful article which confirmed a lot for me and also introduced me to Adeskbar, which was created by David Art. The caveat, Adeskbar isn't ported to FreeBSD, which means a manual install. Google turned up nothing helpful on how to get it done, so I went through the README and following are my steps to get Adeskbar up and running in my environment, I had to have it.

What?

These are the steps I had initially taken.

1. Downloaded adeskbar tar.bz2 from official site
2. $ tar yxf adeskbar-0.4.3.tar.bz2
3. $ mv adeskbar-0.4.3 ~/adeskbar-0.4.3 && cd adeskbar-0.4.3
4. $ sudo ./install.sh
5. $ adeskbar
6. annnddd...Nothing.
7. $ cat install.sh (Reveals where the files were moved, and adeskbar.desktop was renamed to applications...hmm, ok)
8. $ sudo mv /usr/share/applications /usr/local/share/applications/adeskbar.desktop
9. $ sudo ln -s /usr/local/share/applications/adeskbar.desktop /usr/share/applications
10. copied default.conf to ~/.config/adeskbar/
11. $ adeskbar default …. repeat step 6
12. $ cat ~/.config/adeskbar/output.log
> /usr/bin/adeskbar: /usr/share/adeskbar/main.py: not found
13. $ ls /usr/bin/adeskbar
> main.py (listed) ??? I found this article Still Stupid (http://stillstup.blogspot.com/2013/03/new-adeskbarinstall.html)
14. $ whereis python : /usr/local/bin/python
15. $ sudo emacs main.py (updated path based on whereis: #!/usr/local/bin/python)
16. Repeat step 5, 6 and 11.
17. This time /usr/share/adeskbar/ui.py complains about not finding gmenu. I have gnome-menus installed.

After all of that, I realised I would never get gmenu installed. Soo, I found and downloaded adeskbar-5.0, changed all the paths in install.sh, adeskbar.sh and uninstall.sh to reflect /usr/local/share instead of /usr/share.

Then:
1. $ adeskbar &
2. and …. IT'S ALIVE
3. edited ~/.config/openbox/autostart to include (sleep 4 && adeskbar) &

Tweaks...

Okay, it works but not quite. I had to do a few more things. First I followed the advice in the comments section and did
$ ln -s /usr/local/etc/xdg/menus/gnome-applications.menu /usr/local/etc/xdg/menus/applications.menu
I also installed py27-dbus, not sure if I needed to or not.

You can use ~/.config/adeskbar/output.log to guide you, but based on my errors, this is what I did.

1. NameError: name 'pyinotify' is not defined.
  • Pyinotify is listed as optional, and I couldn't find it in the pkg or ports repos, so
  • Go to /usr/local/share/adeskbar/plugins/menu.py or wherever you installed it on your system and comment the entire class definition for class EventHandler(pyinotify.ProcessEvent)
  • Restart adeskbar, for me I right-clicked adeskbar, chose quit and ran $ adeskbar default &
2. IOError: [Errno 2] No such file or directory /etc/xdg/menus/applications.menu
  • Easy fix, based on output.log, control left adeskar at this point: File "/usr/local/share/adeskbar/adesk/core.py", line 216
  • Go to line 216 and update the path to the symbolic link created earlier /usr/local/etc/xdg/menus/
3. File "/usr/local/share/adeskbar/adesk/check.py", line 76, in check_modules
check['VTE'][0] = False
  • Line 76 isn't the problem, it's actually at line 19. if you look closely you'll see 'VTE ', notice the extra space. Edit the line to remove the space so it looks like 'VTE'.
  • Restart Adeskbar, right-click and choose Preferences and it should finally appear

1. Intel Celeron Processor N2840 @ 2.14GHz / Intel HD Graphics / 4 GB RAM / 223Gb Storage
2. I counted in my head 1-1000-2-1000-3-1000, not scientific but I didn't care enough to really measure it down to the milliseconds
3. It took me longer than 3 days to figure out how to dual boot Windows 8.1 / FreeBSD on a GPT + UEFI setup and I still don't know how to