Thursday, July 16, 2009

Source-highlight-qt: A library for highlighting Qt documents using GNU Source-highlight

A few days ago I released a new software that uses the new library provided by GNU Source-highlight: Source-highlight-qt, "a library for performing syntax highlighting in Qt documents by relying on GNU Source-Highlight library".

This library provides an implementation of the qt abstract class QSyntaxHighlighter class, and it deals both with Qt3 and Qt4 (you will need to build a separate version of the library for the two different qt frameworks). Please note, the Qt3 version has less features and it is there only for old qt applications; furthermore, QSyntaxHighlighter class in Qt3 has some design problems which make it quite inefficient to use.

The library can be downloaded from the sourceforge site using this link: http://sourceforge.net/projects/srchiliteqt/files.

For standard usage, it's just a matter of executing this code (you can pass the language syntax, i.e., the source-highlight language definition file, to the init method):

QTextEdit *editor = new QTextEdit;
srchiliteqt::Qt4SyntaxHighlighter *highlighter =
new srchiliteqt::Qt4SyntaxHighlighter(editor->document());
highlighter->init("java.lang");
We also suggest to take a look at the example program qeditexample, for some use cases of the library. Please notice that this qeditexample program is not intended to be used as an editor for production, it's just to show how to use source-highlight-qt library (and it uses the qt4 version).

This is a screenshot of the program qeditexample, editing a C file; basically most of the code is taken directly from the library. For instance, it provides the combo boxes for selecting the highlighting language and the output style (and they be connected to the editor, so that all the changes are automatically applied):


This is another screenshot, where we're editing a C++ file, with a different style


The library also provides a dialog to customize the output style:


Happy Qt Highlighting! :-)

Thursday, June 11, 2009

Php-source-highlight

Just after one week I released version 3.0 of source-highlight with the C++ library, I got an email from Pascal Bleser, telling me that he created a php extension using the library, for highlighting code from php without relying on CGI scripts! You can find its plugin extension here: http://code.google.com/p/php-source-highlight. I took a quick look at his code and it's really simple and clean, hopefully this also means that the library of source-highlight can be smooth to use :-).

You can also might want to take a look at his blog post!

Saturday, June 06, 2009

Tutorial: using Autotools with Eclipse (and Linux Tools Project)

As a GNU maintainer, I've always been using autotools (i.e., automake, autoconf, etc.), and I must admit I still haven't found anything such powerful. Now, I started using the plugins of the Linux Tool Project which seem to have improved a lot! They provide plugins for ChangeLog, Valgrind, and in particular for autotools based projects. Since I haven't seen any tutorial for using the autotools plugin, I thought I could write one; well, it's not that difficult to use the autotools plugin, but I hope it can help :-)

Of course, first of all, you need to install the autotools plugin, and I personally prefer the update site method, using http://download.eclipse.org/technology/linuxtools/update. Remember that the linux tools are based on CDT plugin.

For this tutorial, we will use an already autotools based project (for the moment, the autotools plugin can create an autotools based project, but without any templates). We will use the GNU Hello program, so you can download the last version from the web site. Then you can untar the contents in a directory (say tmp).

Now, it's time to create a new project in eclipse, in particular, a GNU C Autotools Project (or a C++ project if you're project uses C++):


Let's call it "hello" (and then you can select your location and accept the other defaults). You will now have an empty autotools based C project.

Now, let's import the hello package from the directory where we had untarred it, by using "import file system":


Now, the hello project is populated with the package of GNU Hello; notice that configure.ac, Makefile.am and .m4 files are handled by the autotools plugins with syntax highlighting, outline, mouse over functionalities, etc.:


Now, BEFORE building the project for the first time, let's take a look at the properties of the project, where you can see we now have an autotools part, where you can customize the autotools versions you might want to use (I take the defaults):


In particular, from "C/C++ Build - Settings" you can configure the arguments passed to the tools, and the build directory (it is often advised to build a package in a different directory, so that you can configure different builds; by default, the build directory is "build", and I'd leave this that way):


Now, let's change some options! For instance, the configure's --prefix option:


The suffix of the installed program


Since we build the program for development and for debugging, let's make sure to turn off C compiler optimizations (which make debugging a pain):


You can then check that debugging is already turned on, as also '-Wall' option (to report all warnings).

Since we downloaded the source package of hello, produced by 'make dist', this provides all the scripts generated by the autotools in order to run 'configure && make'. However, if you get a program from the CVS, it is likely that you need to bootstrap autotools. Now, the autotools plugin relies on an 'autogen.sh' script which calls all the autotools in the right order, which must be provided by the package itself; however, this is kind of an old method, since now, to bootstrap autotools, you can rely on the program 'autoreconf' which is part of autotools; so, in case you need to bootstrap the autotools, you can simply call 'autoreconf -i' (this will install required files from autotools) from a shell, in the directory of the project.

Now, you can build the project, by right clicking on the project and by choosing "Build Project"! Since this is the first time we build it, the build directory will be created, the configure script will be called with all the features we selected, and the files are compiled!


Hey, but wait a minute! We could have used ccache to compile the sources! And we should have also enable maintainer mode specific configuration settings! Well, we can still do that by going back to the settings of the project, enable the maintainer setting check box, and specify the gcc command as 'ccache gcc':


Now, let's clean the project (this will issue a 'make distclean', but you can customize this from the setting), and build it again, so that it will be reconfigured with the new settings.

Now, let's create another build configuration (e.g., for the release version of the package, with optimized compiler options, with a different prefix, removing the suffix '-debug' and without the maintainer-mode option enabled). We just need to go to "Manage Configurations...", create a new one, starting from the previous default one:


and now we can set the optimization level for the compiler, disable maintainer mode and use another --prefix option (e.g., '/usr/local/'). It is also important to specify another build directory, say 'build_release' (but when creating a new configuration, the build directory has already been changed to something different from the previous one, i.e., 'build1'):


Now, we can switch between the two different build configurations:


Well, that's all for now; I hope you find this tutorial useful :-)

Thursday, May 14, 2009

GNU Source-higlight 3.0: now with a Library

I finally made it to release the new major version of GNU Source-highlight 3.0, where the main novelty is that it now includes a C++ library that can be used in other programs to highlight text (not necessarily to generated highlighted text as source-highlight itself does, but for instance, to highlight text in a text editor). This was the first time I implemented a C++ library, so I hope I got it right, mostly at least ;-)

Source-highlight now includes also the program source-highlight-settings, which can be used to check whether source-highlight will be able find its language definition files, and other configuration files, and in case, to store the correct settings in a configuration file, in the user home directory. In particular, the stored configuration file will be called source-highlight.conf and stored in $HOME/.source-highlight/. For the moment, this file only stores the default value for the --data-dir option.

Another new feature is the command line option --regex-range, which, similarly to the option --line-range, allows to specify only a part of the input source to be highlighted (of course, as for --line-range option, environments or states, such as, e.g., comments and strings, are handled correctly even though they cross the range). Ranges can be expressed using regular expressions, with the command line option --regex-range. In this case the beginning of the range will be detected by a line containing (in any point) a string matching the specified regular expression; the end will be detected by a line containing a string matching the same regular expression that started the range. This feature is very useful when we want to document some code (e.g., in this very manual) by showing only specific parts, that are delimited in a ad-hoc way in the source code (e.g., with specific comment patterns).

For instance, consider the following listing:

01: /*
02: This is a classical Hello program
03: to test source-highlight with Java programs.
04:
05: to have an html translation type
06:
07: source-highlight -s java -f html --input Hello.java --output Hello.html
08: source-highlight -s java -f html < Hello.java > Hello.html
09:
10: or type source-highlight --help for the list of options
11:
12: written by
13: Lorenzo Bettini
14: http://www.lorenzobettini.it
15: http://www.gnu.org/software/src-highlite
16: */
17:
18: package hello;
19:
20: import java.io.* ;
21:
22: /**
23: * <p>
24: * A simple Hello World class, used to demonstrate some
25: * features of Java source highlighting.
26: * </p>
27: * TODO: nothing, just to show an highlighted TODO or FIXME
28: *
29: * @author Lorenzo Bettini
30: * @version 2.0
31: */ /// class
32: public class Hello {
33: int foo = 1998 ;
34: int hex_foo = 0xCAFEBABE;
35: boolean b = false;
36: Integer i = null ;
37: char c = '\'', d = 'n', e = '\\' ;
38: String xml = "<tag attr=\"value\">&auml;</tag>", foo2 = "\\" ;
39:
40: /* mymethod */
41: public void mymethod(int i) {
42: // just a foo method
43: }
44: /* mymethod */
45:
46: /* main */
47: public static void main( String args[] ) {
48: // just some greetings ;-) /*
49: System.out.println( "Hello from java2html :-)" ) ;
50: System.out.println( "\tby Lorenzo Bettini" ) ;
51: System.out.println( "\thttp://www.lorenzobettini.it" ) ;
52: if (argc > 0)
53: String param = argc[0];
54: //System.out.println( "bye bye... :-D" ) ; // see you soon
55: }
56: /* main */
57: }
58: /// class
59:
60: // end of file test.java

If we invoke source-highlight with the following command line option:
--regex-range="/// [[:alpha:]]+"
we will get this output (thus, only the part in the range delimited by "/// class", which matches the specified regular expression:

32: public class Hello {
33: int foo = 1998 ;
34: int hex_foo = 0xCAFEBABE;
35: boolean b = false;
36: Integer i = null ;
37: char c = '\'', d = 'n', e = '\\' ;
38: String xml = "<tag attr=\"value\">&auml;</tag>", foo2 = "\\" ;
39:
40: /* mymethod */
41: public void mymethod(int i) {
42: // just a foo method
43: }
44: /* mymethod */
45:
46: /* main */
47: public static void main( String args[] ) {
48: // just some greetings ;-) /*
49: System.out.println( "Hello from java2html :-)" ) ;
50: System.out.println( "\tby Lorenzo Bettini" ) ;
51: System.out.println( "\thttp://www.lorenzobettini.it" ) ;
52: if (argc > 0)
53: String param = argc[0];
54: //System.out.println( "bye bye... :-D" ) ; // see you soon
55: }
56: /* main */
57: }

if we invoke it with
--regex-range="/\* main \*/"
we get the part concerning main:

47:     public static void main( String args[] ) {
48: // just some greetings ;-) /*
49: System.out.println( "Hello from java2html :-)" ) ;
50: System.out.println( "\tby Lorenzo Bettini" ) ;
51: System.out.println( "\thttp://www.lorenzobettini.it" ) ;
52: if (argc > 0)
53: String param = argc[0];
54: //System.out.println( "bye bye... :-D" ) ; // see you soon
55: }

Finally, with
--regex-range="/\* [[:alpha:]]+ \*/"
you'll get only the parts concerning methods:

41:     public void mymethod(int i) {
42: // just a foo method
43: }
47: public static void main( String args[] ) {
48: // just some greetings ;-) /*
49: System.out.println( "Hello from java2html :-)" ) ;
50: System.out.println( "\tby Lorenzo Bettini" ) ;
51: System.out.println( "\thttp://www.lorenzobettini.it" ) ;
52: if (argc > 0)
53: String param = argc[0];
54: //System.out.println( "bye bye... :-D" ) ; // see you soon
55: }

This is quite useful for inserting some parts of input files in manuals (e.g., I used it myself to insert some parts of files in the source-highlight manuals).

Another added functionality is that source-highlight also uses the filename (not only its extension) to detect the source language (for instance, if you pass as input ChangeLog, it will use the changelog.lang language definition).

Finally, two more input languages were added: the one for generic configuration files (including, e.g., xorg.conf, qmake.conf, etc.), and the one for pkg-config files; here are some examples

configuration files

# test file for some configuration files, such as
# xorg, qmake.conf and generic conf files (based on properties)

# xorg.conf (xorg X Window System server configuration file)
#
# This file was generated by dexconf, the Debian X Configuration tool, using
# values from the debconf database.
#

Section "Files"
EndSection

Section "InputDevice"
Identifier "Synaptics Touchpad"
Driver "synaptics"
Option "SendCoreEvents" "true"
Option "Device" "/dev/psaux"
Option "Protocol" "auto-dev"
Option "SHMConfig" "on"
Option "HorizEdgeScroll" "0"
Option "MinSpeed" "0.5" # touchpad speed when moving slowly
Option "MaxSpeed" "2.0" # touchpad speed when moving fast
Option "AccelFactor" "0.10"
Option "CircularScrolling" "on"
Option "EdgeMotionMinSpeed" "0.5"
EndSection

#
# qmake configuration for win32-g++
#
# Written for MinGW
#

MAKEFILE_GENERATOR = MINGW
TEMPLATE = app
CONFIG += qt warn_on release link_prl copy_dir_files debug_and_release debug_and_release_target precompile_header

QMAKE_INCDIR =
QMAKE_INCDIR_QT = $$[QT_INSTALL_HEADERS]
QMAKE_LIBDIR_QT = $$[QT_INSTALL_LIBS]

QMAKE_RUN_CC = $(CC) -c $(CFLAGS) $(INCPATH) -o $obj $src
QMAKE_RUN_CC_IMP = $(CC) -c $(CFLAGS) $(INCPATH) -o $@ $<


# settings for source-highlight

datadir = "/home/bettini/usr/local/share/source-highlight"

# other conf file
[PlaylistColumnsLayout]
ColumnNames=Filename,Title,Artist,AlbumArtist,Composer,Year,Album,DiscNumber,Track,BPM,Genre,Comment,Directory,Type,Length,Bitrate,SampleRate,Score,Rating,PlayCount,LastPlayed,Mood,Filesize
ColumnOrder=0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22
ColumnWidths=0,184,92,0,0,100,92,0,0,0,0,0,73,0,80,0,0,0,0,0,0,0,0
ColumnsVersion=1
SortAscending=true
SortColumn=200

pkg-config files

prefix=/home/bettini/usr/local
exec_prefix=${prefix}
libdir=${exec_prefix}/lib
includedir=${prefix}/include

Name: libsource-highlight
Description: GNU Source-highlight library.
URL: http://www.gnu.org/software/src-highlite/
Version: 3.0
Libs: -L${libdir} -lsource-highlight -L/usr/lib -lboost_regex-gcc43-1_35
Cflags: -I${includedir} -I${libdir}/include -I/usr/include



Hope to have another program using source-highlight library ready soon :-)

Friday, May 08, 2009

Goodbye ATI, welcome NVIDIA

I recently bought a new desktop, a slim one: the s3530.it HP Pavilion slimline, which I've really enjoyed since the very first moment! It is very small, compact, silent, and it provides basically everything!

When I bought it I noticed that it was endorsed with an ATI graphical card, but I said to myself "ATI? Never owned one... but it is well supported in Linux as far as I remember..." Gosh I was quite wrong!

If supported means that you need to get stuck with 2d or experience very sloooow motions in 3d then, it is right, it is supported, otherwise... :'(

I had always had nice experiences with Nvidia, and I didn't want to give up compiz special effects (after all I have a powerful desktop!), so after a quite hard research to find someone selling an Nvidia lowprofile graphic card, I substituted that crap of ATI with an Nvidia GE-FORCE 8400 GS graphical card, and now everything works like magic, with a very fluent graphic!

Of course, Ubuntu provides the driver for that! Well it did it also for ATI, but that driver really sucks!

I'll never buy another computer again if endorsed with an ATI, this is for sure! :-)

Saturday, April 11, 2009

GPRS connection in Ubuntu Jaunty

I've been using this beta version of Ubuntu Jaunty for a few days now, and today I wanted to establish a GPRS/GSM connection with my Nokia, using a USB cable. I had done this in my other Kubuntu distribution, and I only needed to set the parameters right in kppp.

This time I had a very pleasant surprise: as soon as I connected the USB cable, a pop window showed up:


after pressing Forward:


it already had all the parameters I needed, I just had to select the right voices in the Country and Provider, and I'm on-line!

Well done developers :-)

Sunday, April 05, 2009

Ubuntu Jaunty 9.04 beta on a Dell Latitude D630

I've been using this laptop, a Dell Latitude D630 for more than one year now, and I've always used Kubuntu (but always up to 8.04). Today, I decided to give a try to Ubuntu Jaunty 9.04 (still in beta); I preferred Ubuntu instead of Kubuntu because the same version of Kubuntu has a bug at the end of the installation process which does not let you decide whether to install the boot loaded on a specific partition instead of the MBR.

I tested the system live first, and experienced no problems. Thus, I installed it on the hard disk (on another partition, since I'm still keeping the kubuntu 8.04 installation).


The first nice thing I noticed was that now it's much easier to select your time zone, and the Region and City lists are neater (you can select the time zone with a few clicks now).


Also the suggestion of the keyboard layout works fine!

Then we come to the partitioning step:


where I preferred to do a manual partitioning (having already some existing partitions I needed to keep):


I also specified to mount my other linux installation in a specific directory



The installation also offers the possibility of importing existing accounts (note that it detected windows and other Linux installations), but I preferred not to.


Then, before starting the installation...


remember to click on the advanced button, to setup the boot loader as you prefer, otherwise it will be installed on the MBR.


Now, since I already have grub installed (from my main kubuntu 8.04 installation), and want to keep that that way, I chose to install the ubuntu boot loader on the first sector of the installation partition (in my case (hd0,5) or /dev/sda6):


Now, I'm read to start the installation:


Of course, I had to change the grub menu.lst file of my main linux kubuntu 8.04 installation in order to add a menu item to boot this new Linux installation:
title        Ubuntu Jaunty
root (hd0,5)
chainloader +1
By selecting this grub menu item, I can boot the new Jaunty installation.

The first impression was really good! Everything seems to work fine: ethernet (Broadcom Corporation NetXtreme BCM5755M Gigabit Ethernet PCI Express (rev 02)), wireless (Network controller: Intel Corporation PRO/Wireless 3945ABG [Golan] Network Connection (rev 02)), bluetooth, graphic card (Intel Corporation Mobile GM965/GL960 Integrated Graphics Controller (rev 0c)) with special effects on!

It also looks like the infamous bug concerning too many load/unload cycles is not there anymore!

Stay tuned, as I'll keep this post up to date with new information :-)