Jul 16 2009

VIM as a syntax highlighting pager

Éverton Arruda

I’ve been looking for a way to use less with syntax highlight and ended up in a page of Ubuntu Tutorials[0] website with a very good tip: Using VIM as a syntax highlighting pager[1].

All you’ll need is the package vim-full, só, in Debian-like systems, you’ll need to execute:

1
# aptitude install vim-full

Then, you’ll have to execute:

1
$ vim -u /usr/share/vim/vim72/macros/less.vim <FILE>

And you have it working!
NOTE: Depending on the version of your VIM, the path to less.vim might be different.

You can make this command simpler by creating an alias and adding it to your .bashrc file:

1
$ echo "alias vless=\"vim -u /usr/share/vim/vim72/macros/less.vim\"" >> ~/.bashrc

Now the system will need to re-read your .bashrc file, so the changes may be applied:

1
$ . ~/.bashrc

And after this you can test vless:

1
$ vless <FILE>

[0] http://ubuntu-tutorials.com
[1] http://ubuntu-tutorials.com/2008/07/14/use-vim-as-a-syntax-highlighting-pager/

:wq


Jul 14 2009

Colored Man Pages

Éverton Arruda

I found an interesting tip about coloring man pages in Jônatas’ blog (http://tupinigeek.blogspot.com/2009/07/man-pages-coloridas.html <- in Portugese). This is how you do it:

1 – Install most (http://www.jedsoft.org/most/index.html):
You can download most here: ftp://space.mit.edu/pub/davis/most/
or by executing the line bellow in debian-like systems:

1
# aptitude install most

2 – Export MANPAGER variable:
After this you’ll have to execute the line bellow:

1
$ export MANPAGER="/usr/bin/most -s"

Now you should have your man pages like this:

If you don’t want to export that variable everytime you restart your computer, you can add that line to your .bashrc file:

1
$ echo "export MANPAGER=\"/usr/bin/most -s\"" >> ~/.bashrc


Jun 24 2009

[JavaScript+CSS+PHP] Changing style of actual menu link

Éverton Arruda

I was looking for a way to change the style of the actual menu link (the last clicked link in the menu), so that the user could know what page he is seeing, or the last link he clicked. My first attempt was to use only CSS, but i didn’t find anything. Then i read somewhere that it was not able to what i wanted only with CSS, that i needed JavaScript to do it. Well, i’ve done it and here i’ll teach how to do it:

The first thing we have to do is to create the menu and it’s style:
Style:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
#menu {
    border-top: 2px solid black;
    border-bottom: 2px solid black;
    padding: 10px;
    text-align: center;
    width: 300px;
}

#menu a {
    padding: 10px;
    text-decoration: none;
    margin: 0px 5px;
    color: black;
    background-color: "white";
}

#menu a:hover {
    text-decoration: underline;
}

Note that i didn’t set the a:visited style.

Menu:

1
2
3
4
5
<div id="menu">
    <a id="link1" href="<?php $_SERVER['php_self'] ?>?p=link1">link1</a>
    <a id="link2" href="<?php $_SERVER['php_self'] ?>?p=link2">link2</a>
    <a id="link3" href="<?php $_SERVER['php_self'] ?>?p=link3">link3</a>
</div>

Now we have our menu, but it’s still not showing what is the actual menu link, so we will need a function that will change the style of the actual menu link:

Javascript Function:

1
2
3
4
5
function setActualLink(item)
{
    item.style.backgroundColor = "black";
    item.style.color = "white";
}

Now that we have the function it’s necessary to make it be executed after the menu is loaded, adding this line:

1
<script>setActualLink(<?php echo $p; ?>);</script>

You could also create a class for the actual menu link and assign the class to the actual menu link, instead of setting the styles directly inside the javascript function, as shown below:

Actual Menu Link Class:

1
2
3
4
#menu a.actualLink:visited {
    background-color: black;
    color: white;
}

And the javascript function would change to:
JavaScript Function:

1
2
3
4
function setActualLink(item)
{
    item.className = "actualLink";
}

You can see the examples in the links below:
Using class
Not using class
The php codes are commented in the source.

Remeber that the pages must be .php .

:wq


May 16 2009

Qt Study

Éverton Arruda

Soon I’ll be studying Qt, a cross-platform application and UI framework, owned by Nokia.
With Qt, you can write applications once and deploy them across many desktop and embedded operating systems without rewriting the source code.

Here is the source from where I’ll beggin my studies:
http://doc.trolltech.com/4.4/tutorials-tutorial.html

See more about Qt in http://www.qtsoftware.com/

:wq


Apr 16 2009

Autocompletion on VIM

Éverton Arruda

Some time ago i read, in Renato Carvalho’s Blog (http://www.renatocarvalho.net/autocompletando-no-vim-dicionario-php.html), a post about autocompletion on VIM.

Autocompletion is a method which consists of completing a word when you write part of it, for example you write lin and press a key, on the keyboard, which is programmed to execute the autocompletion and it completes the word for you, giving you linux. For me it’s very useful.

If you want it too, all you have to do is:

1 – Create plugin and doc directories in ~/.vim:

1
$ mkdir -p ~/.vim/doc ~/.vim/plugin

2 – Download and install SuperTab plugin for VIM:

This plugin enables <tab> to be used for completion.
SuperTab Link:  http://www.vim.org/scripts/script.php?script_id=1643

3 – Download functions list of a language and save it to ~/.vim/doc:

Using google you should be able to find the function list of a language.
For this tutorial i’ll use a PHP functions list, you can get it at: http://lerdorf.com/funclist.txt, save this file in ~/.vim/doc as php-funclist.txt

4 – Add a few lines inside /etc/vim/vimrc:

Edit /etc/vim/vimrc and add the following lines:

1
2
3
if has("autocmd")
    autocmd FileType php  set complete-=k/home/$USER/.vim/doc/php-funclist.txt complete+=k/home/$USER/.vim/doc/php-funclist.txt
endif

These lines tell VIM to read the php-funclist.txt file and load it for autocompletion.

You could use another language functions list, all you have to do is change the name of the functions list file from the lines above.

Now you can test it, open vim and write part of a php function, then press the <tab> key and see what happens.

This tutorial was written by Rodrigo Carvalho, i made some modifications and, with his permission, i posted here.


Apr 15 2009

Creating a simple repository for .deb packages

Éverton Arruda

A few days ago i had to create an internal repository for some deb packages. As there weren’t so many packages, i didn’t need to create a huge repository, like the official ones, so, after a little research, i managed to get it working, and that is what i will explain here, how to create a simple repository for deb packages.

We’ll need this packages before we can continue:
- apt-utils
- dpkg-dev
In debian-like systems you can install it by executing:

1
# aptitude install apt-utils dpkg-dev

And now the steps:

1 – Select the directory in which the repository will be:
I recommend you to use a direcotory in /var/www , like /var/www/repository, making it possible for everyone to see and access it without the need change apache configurations, but for this you’ll have to have apache installed.

For this tutorial we will take /var/www/repository as our repository directory.

1
# mkdir /var/www/repository

2 – Move packages to the repository directory:
Move all deb packages you want to be in the repository to the repository directory, in our case: /var/www/repository.

1
# mv *.deb /var/www/repository

3 – Create index files (Packages and Sources):
These files tell apt wich packages are available in the repository, their version, author and other informations. These files must be in the repository directory.

To create them you’ll have to run:

1
2
3
4
# dpkg-scanpackages /var/www/repository > /var/www/repository/Packages
# dpkg-scansources /var/www/repository > /var/www/repository/Sources
# dpkg-scanpackages /var/www/repository /dev/null | gzip -9c > /var/www/repository/Packages.gz
# dpkg-scansources /var/www/repository /dev/null | gzip -9c > /var/www/repository/Sources.gz

What there command do:
The first two commands scan the given directory for, repectively, .deb files and .dsc files and save their informations in, respectively, Packages and Sources.
The two last commands do the same thing, but instead of saving the files’ informations in a text file, it compresses the files’ informations and save it to a .gz file, which apt will also look for.
For more information about these commands check manpages for each one.

4 – Add repository uri to /var/apt/sources.list:
Now that you have a repository running you’ll, probably, want to access it, right?! To do that you’ll have to edit /etc/apt/sources.list .
If you want to access the repository from the same machine you’ll need to add the following lines:

1
2
3
4
# For binary files repository:
deb file:///var/www/ repository/
# For source files repository:
deb-src file:///var/www/ repository/

If you want other machines to access it you’ll have to have those machines in the same network and add the following lines in their /var/apt/sources.list file:

1
2
3
4
# For binary files repository:
deb http://repository's.machine.ip/ repository/
# For source files repository:
deb-src http://repository's.machine.ip/ repository/

5 – Check if the repository is up:
Now run:

1
# aptitude update

and see if everything is ok.

And that’s it, you should have your repository running.

TIP:
You can make a script to automatize the creation of index files, all you have to do is create a script in the /usr/bin/ directory and add the command lines for the dpkg-scanpackages and  dpkg-scansources commands:

1
2
3
4
5
6
7
# cd /usr/bin
# echo "#!/bin/bash \
dpkg-scanpackages . /dev/null | gzip -9c > Packages.gz \
dpkg-scansources . /dev/null | gzip -9c > Sources.gz  \
dpkg-scanpackages . > Packages \
dpkg-scansources . > Sources" > createrepo
# chmod +x createrepo

The script above will scan for packages and source files in the directory it’s going to be executed and you’ll have to run it as root, you could add ‘sudo’ before each dpkg-scan command so you could run it as a normal user, but it would ask you for the sudo password as well.

This tutorial was based on:
- http://ubuntuforums.org/showthread.php?t=42862
- http://www.debian.org/doc/manuals/repository-howto/repository-howto.en.html

:wq!