Saturday, January 4, 2014


Eventhough there are more than one way to compile/execute programs in Vim/Gvim, the normal method to compile and execute C,C++,Java,Python programs are:

C:

 !gcc % -o %:r
 !./%:r

C++:

!g++ % -o %:r
!./%:r

Python:

!python %

Java:

!javac %
!java %:r

To do all these things you've to type the commands repeatedly everytime you want to compile and execute. To prove the laziness of a programmer, there's a way to do all these things by pressing a single key in Vim/Gvim.

First of all check if you have ~/.vimrc, if you don't have create one using this command :
nano ~/.vimrc

Now after creating this, put the following commands in your .vimrc :

autocmd filetype python nnoremap <F4> :w <bar> exec '!python '.shellescape('%')<CR>
autocmd filetype c nnoremap <F5> :w <bar> exec '!gcc '.shellescape('%').' -o '.shellescape('%:r').' && ./'.shellescape('%:r')<CR>
autocmd filetype cpp nnoremap <F6> :w <bar> exec '!g++ '.shellescape('%').' -o '.shellescape('%:r').' && ./'.shellescape('%:r')<CR>
autocmd filetype java nnoremap <F7> :w <bar> exec '!javac '.shellescape('%').' && java '.shellescape('%:r')<CR>

Here's a screenshot of compiling a program in Vim/Gvim :



If you want to know the execution time of your program too, then use the following code instead of the previous one :


autocmd filetype python nnoremap <F4> :w <bar> exec '!time python '.shellescape('%')<CR>
autocmd filetype c nnoremap <F5> :w <bar> exec '!gcc '.shellescape('%').' -o '.shellescape('%:r').' && time ./'.shellescape('%:r')<CR>
autocmd filetype cpp nnoremap <F6> :w <bar> exec '!g++ '.shellescape('%').' -o '.shellescape('%:r').' && time ./'.shellescape('%:r')<CR>
autocmd filetype java nnoremap <F7> :w <bar> exec '!javac '.shellescape('%').' && time java '.shellescape('%:r')<CR>

To compile and execute the programs, press:
F4 for Python
F5 for C
F6 for C++
F7 for Java



You can change the default key-mappings, just make sure that it didn't conflict with the original Vim/Gvim key-mappings.
That's it enjoy your programming in Vim.

Sunday, December 29, 2013

GVim
GVim
The default syntax highlighting VIM Editor is not that much upto the expectation of the programmers. It's also one of my favourite text editor. So, i've modified the original source code of the syntax highlighting files of the VIm  and able to improved it.

I'm going to share the improved syntax highlighting  files. So, please download the files from my GIT repository. Languages for syntax highlighting  that i've modified are Python, C , C++, JAVA.
and i would like to encourage you to contribute in it which is hosted in GITHUB.

Please clone the following GIT repository :


or download the zip file from here.

Screenshots after using the changed original files:

Python:



C++ :

      


C :



JAVA:








Git is a distributed revision control and source code management (SCM) system with an emphasis on speed. Git was initially designed and developed by Linus Torvalds for Linux kernel development in 2005.

Installing GIT:


GIT can be installed in many  ways. The two major ones are: to install from source and to install from an existing 

package for your platform i.e apt based (Ubuntu, Debian) or rpm based ( Fedora ).

Installing from Source:


For rpm based systems(Fedora,Mandrake,...etc):
# yum install curl-devel expat-devel gettext-devel   openssl-devel zlib-devel
For apt based systems(Ubuntu,Debian,...etc):
# sudo apt-get install libcurl4-gnutls-dev libexpat1-dev gettext libz-dev libssl-dev


When you have all the necessary dependencies, you can go ahead and grab the latest snapshot from the Git web site:
http://git-scm.com/download
or you can download the package using wget:
# wget https://git-core.googlecode.com/files/git-1.8.4.2.tar.gz
Then, compile and install:
# tar xf git-1.7.2.2.tar.gz
# cd git-1.7.2.2
# make prefix=/usr/local all
# sudo make prefix=/usr/local install

Installing on Linux

If you want to install Git on Linux via a binary installer, you can generally do so through the basic package-management tool that comes with your distribution. If you’re on Fedora, you can use yum:
# yum install git-core
    
Or if you’re on a Debian-based distribution like Ubuntu, try apt-get:
# sudo apt-get install git
That's it.
The first thing you should do when you install Git is to set your user name and e-mail address. This is important because every Git commit uses this information, and it’s immutably baked into the commits you pass around:
# git config --global user.name "Unix Roots"
# git config --global user.email unixroots@example.com

If you want to check your settings, you can use the git config --list command to list all the settings Git can find at that point:

# git config --list
    user.name=Unix Roots
    user.email=unixroots@example.com                                                                                                         ...
Now check that if it's working using the command git clone and using any git repository:
Here's a screenshot of cloning from GITHUB:


If you are going to use GIT from a proxy server then check out my post on Using GIT behind proxy server. 

Friday, December 27, 2013

Brackets
Brackets
Brackets is an open source code editor for the web. It's entirely developed using JavaScript, HTML and CSS by web developers like you. It's one of my favourite code editors besides WebStorm,PyCharm,Bluefish,etc..

As Brackets didn't provide the GUI interface for setting PROXY and PORT, you need to hack around a little bit  in the SOURCE CODE of the Brackets. I've already hacked it so this article provides you the details on setting the PROXY for Brackets. You can download the source code from GitHub.

you can use the command :

                 git clone https://github.com/adobe/brackets

or simply download the zip file from there.

Brackets does't work on Debian-7(wheezy) due to glibc version incompatibility, so to use Brackets in Wheezy, please download this test build Brackets-sprint-34. This release solves the bug  #4816 for glibc version incompatibility.

Let's set the proxy in Brackets.
Brackets is installed in /opt/brackets in Linux
So goto  /opt/brackets/www/extensibility/node/ExtensionManagerDomain.js

or by using the command in terminal :

               gedit /opt/brackets/www/extensibility/node/ExtensionManagerDomain.js


If you are using Brackets-sprint-34, then goto line number 382 and look for the function _cmdDownloadFile(downloadId, url, callback)   and 

change the line :
var req = request.get({
        url: url,
        encoding: null
    },

to

var req = request.get({
        proxy: 'http://proxy.adrress:port',
        url: url,
        encoding: null
    },

Here's a Screenshot :



if your proxy requires authentication then it should be like this :

var req = request.get({
        proxy: 'http://username:password@proxy.adrress:port',
        url: url,
        encoding: null
    },


for example :
var req = request.get({
        proxy: 'http://127.0.0.1:8080',
        url: url,
        encoding: null
    },

That's it, save the file and restart the Brackets. Now, you'll be able to install plugins.
If you have got any problem just throw a comment, i'll try to help you.  







Wednesday, October 23, 2013

super(AssetDelegate, self).__init__(parent)
The initializer is typical of most delegate subclasses, simply calling the
base class.
def paint(self, painter, option, index):
myoption = QStyleOptionViewItem(option)
if index.column() == ROOM:
myoption.displayAlignment |= Qt.AlignRight|Qt.AlignVCenter
QSqlRelationalDelegate.paint(self, painter, myoption, index)
We have reimplemented the paint() method only to right-align the room
numbers. We do this by changing the QStyleOptionViewItem, and we leave the
painting itself to be done by the base class.
def createEditor(self, parent, option, index):
if index.column() == ROOM:
editor = QLineEdit(parent)
regex = QRegExp(r"(?:0[1-9]|1[0124-9]|2[0-7])"
r"(?:0[1-9]|[1-5][0-9]|6[012])")
validator = QRegExpValidator(regex, parent)
editor.setValidator(validator)
editor.setInputMask("9999")
editor.setAlignment(Qt.AlignRight|Qt.AlignVCenter)
return editor
else:
return QSqlRelationalDelegate.createEditor(self, parent,
option, index)
The heart of the createEditor() method is the code that sets up the QLineEdit
for entering room numbers. Room numbers are four digits long, made up of
a floor number, in the range 01–27 (but excluding 13), and a room number on
the floor in the range 01–62. For example, 0231 is floor 2, room 31, but 0364 is
invalid. The regular expression is sufficient for specifying valid room numbers,
but it cannot set a minimum number of digits, since one, two, or three digits
may be a valid prefix for a valid four digit room number. We have solved this
by using an input mask that requires exactly four digits to be entered. For the
other fields, we pass the work on to the base class.
def setEditorData(self, editor, index):
if index.column() == ROOM:
text = index.model().data(index, Qt.DisplayRole).toString()
editor.setText(text)
else:
QSqlRelationalDelegate.setEditorData(self, editor, index)462
Chapter 15. Databases
Once the editor has been created, the view will call setEditorData() so that it
can be populated with the current value. In this case, we care only about the
room column, passing on the work for the other fields to the base class.
def setModelData(self, editor, model, index):
if index.column() == ROOM:
model.setData(index, QVariant(editor.text()))
else:
QSqlRelationalDelegate.setModelData(self, editor, model,
index)
We have taken a similar approach to the previous method, handling the room
field and leaving the others to be handled by the base class. As a matter of
fact, we could have omitted reimplementing this method, and PyQt would have
been smart enough to retrieve the value from our QLineEdit. However, it is a
better practice to take full responsibility for our own customizations.
We have now finished the detour and can return to the MainForm.__init__()
method, beginning with the bottom table that shows the log records that are
applicable to the current asset.
self.logModel = QSqlRelationalTableModel(self)
self.logModel.setTable("logs")
self.logModel.setRelation(ACTIONID,
QSqlRelation("actions", "id", "name"))
self.logModel.setSort(DATE, Qt.AscendingOrder)
self.logModel.setHeaderData(DATE, Qt.Horizontal,
QVariant("Date"))
self.logModel.setHeaderData(ACTIONID, Qt.Horizontal,
QVariant("Action"))
self.logModel.select()
The code for creating the log model is almost the same as the code we used for
the asset model. We use a QSqlRelationalTableModel because we have a foreign
key field, and we provide our own column titles.
self.logView = QTableView()

self.logView.setModel(self.logModel)
self.logView.setItemDelegate(LogDelegate(self))
self.logView.setSelectionMode(QTableView.SingleSelection)
self.logView.setSelectionBehavior(QTableView.SelectRows)
self.logView.setColumnHidden(ID, True)
self.logView.setColumnHidden(ASSETID, True)
self.logView.resizeColumnsToContents()
self.logView.horizontalHeader().setStretchLastSection(True)

This code is also similar to what we did for the assets table, but with three dif-
ferences. Here we have used a custom LogDelegate class—we won’t review itUsing Database Table Views
463
because it is structurally very similar to the AssetDelegate. It provides custom
editing of the date field. We also hide both the log record’s ID field and the
assetid foreign key—there’s no need to show which asset the log records are for
because we are using master–detail, so the only log records that are visible are
those that apply to the current asset. (We will see how the master–detail rela-
tionship is coded shortly.) The last difference is that we have set the last col-
umn to stretch to fill all the available space. The QTableView.horizontalHeader()
method returns a QHeaderView, and this is what controls some aspects of the
table view’s columns, including their widths.
self.connect(self.assetView.selectionModel(),
SIGNAL("currentRowChanged(QModelIndex,QModelIndex)"),
self.assetChanged)
self.connect(addAssetButton, SIGNAL("clicked()"),
self.addAsset)
If the user navigates to a different row we must update the log view to show
the log records for the right asset. This is achieved by the first connection in
conjunction with the assetChanged() method that we will review in a moment.
Every view has at least one selection model that is used to keep track of
which items in the view’s model (if any) are selected. We connect the view’s
selection model’s currentRowChanged() signal so that we can update the log view
depending on the current asset.
All the other connections are button-clicked connections like the second one
shown here. We will cover all the methods the buttons connect to as we
progress through this section.
self.assetChanged(self.assetView.currentIndex())
self.setMinimumWidth(650)
self.setWindowTitle("Asset Manager")
The initializer ends by calling the assetChanged() method with the asset view’s
current model index—this will result in the log view showing the relevant
asset’s records.
def assetChanged(self, index):
if index.isValid():
record = self.assetModel.record(index.row())
id = record.value("id").toInt()[0]
self.logModel.setFilter(QString("assetid = %1").arg(



class MainForm(QDialog):
def __init__(self):
super(MainForm, self).__init__()
self.assetModel = QSqlRelationalTableModel(self)
self.assetModel.setTable("assets")
self.assetModel.setRelation(CATEGORYID,
QSqlRelation("categories", "id", "name"))
self.assetModel.setSort(ROOM, Qt.AscendingOrder)
self.assetModel.setHeaderData(ID, Qt.Horizontal,
QVariant("ID"))
self.assetModel.setHeaderData(NAME, Qt.Horizontal,
QVariant("Name"))
self.assetModel.setHeaderData(CATEGORYID, Qt.Horizontal,
QVariant("Category"))
self.assetModel.setHeaderData(ROOM, Qt.Horizontal,
QVariant("Room"))
self.assetModel.select()
The model is created in much the same way as we saw in the preceding sec-
tion. The ID, NAME, and others are integer column indexes set up earlier in the
assetmanager.pyw file. What’s different from using a QDIalogWidget
The initializer is typical of most delegate subclasses, simply calling the
base class.
def paint(self, painter, option, index):
myoption = QStyleOptionViewItem(option)
if index.column() == ROOM:
myoption.displayAlignment |= Qt.AlignRight|Qt.AlignVCenter
QSqlRelationalDelegate.paint(self, painter, myoption, index)
We have reimplemented the paint() method only to right-align the room
numbers. We do this by changing the QStyleOptionViewItem, and we leave the
painting itself to be done by the base class.
def createEditor(self, parent, option, index):
if index.column() == ROOM:
editor = QLineEdit(parent)
regex = QRegExp(r"(?:0[1-9]|1[0124-9]|2[0-7])"
r"(?:0[1-9]|[1-5][0-9]|6[012])")
validator = QRegExpValidator(regex, par