Emacs + php-mode in windows

19 11 2009
My development machine is having Ubuntu and I love to use Emacs than any other editor. Recently I need to work on windows.  I wrote a blog post about how to make Linux like environment in windows. I wanted to use Emacs in windows. To install Emacs in windows I followed the instructions as it is from http://www.claremontmckenna.edu/math/alee/emacs/emacs.html

I want to work on PHP, so I need a php-mode for Emacs, otherwise it will be very difficult. I tried a lot to install php-mode in windows as specified at https://prajwalaa.wordpress.com/2009/03/19/using-emacs-to-edit-php-files/ but I was getting “error: `c-lang-defconst’ must be used in a file” error message. Finally after searching for long time I figured out how to enable php-mode.

I downloaded php-mode and extracted and placed in my C:\emacs directory. And added following lines in my C:\emacs\.emacs.d\init.el file


(add-to-list 'load-path "C:\emacs\php-mode")
;;(load 'php-mode)
(autoload 'php-mode "php-mode" "Major mode for editing php code." t)
(defun clean-php-mode ()
(interactive)
(php-mode)
(setq c-basic-offset 2) ; 2 tabs indenting
(setq indent-tabs-mode nil)
(setq fill-column 78)
(c-set-offset 'case-label '+)
(c-set-offset 'arglist-close 'c-lineup-arglist-operators))
(c-set-offset 'arglist-intro '+) ; for FAPI arrays and DBTNG
(c-set-offset 'arglist-cont-nonempty 'c-lineup-math) ; for DBTNG fields and values
(add-to-list 'auto-mode-alist '("\\.php$" . php-mode))
(add-to-list 'auto-mode-alist '("\\.inc$" . php-mode))

After adding above lines and save. I restarted emacs. Then Php-mode is working fine. Hope this will help others to install Emacs and enable php-mode.





Linux like development environment in Windows

6 11 2009

I am a fan of Linux based OS. I love Ubuntu a lot. In fact I started my programming in Redhat. So from the beginning I am comfortable using Linux than Windows. The only reason I use windows is to check IE browser compatability.

Recently I attended Drupal Sprint India 2009. I want to attend the sprint, so I need one Laptop to go there and work. My company provided me Laptop with Windows. So I need to work on drupal core issues in windows. Hmm it looks very hard for me to work in Windows. I want to have following things in my Windows machine.

  1. Cygwin to get the linux like terminal, so that I can see paths with forward slash.
  2. Xampp Lite to get Apache, Mysql, PHP.
  3. cvs, I am going to work on drupal core so I need it
  4. I should able to create patches, apply patches.
  5. I want to use Drupal Drush module, which provides some command line utilities
  6. Emacs. I am more comfortable with this editor. It is very hard to work with other editors once we are comfortable with Emacs

First I installed the things I know like Cygwin, Xampp Lite.

Installing Emacs

I Installed Emacs by following instructions at http://www.claremontmckenna.edu/math/ALee/emacs/emacs.html .

Installing UnxUtils

To be able create patches and apply patches I installed unxutils from http://sourceforge.net/projects/unxutils/ . With this unxutils I got most of the Linux commands. The commands like ls, mkdir, wget, diff, patch, tar, zip etc… are working. I am very happy after installing unixutils.
Installing UnxUtils is nothing but setting the path variable in Windows. I extracted the unixutils directory in C folder and Add C:\unixutils\usr\local\wbin path to the Path variable. In Windows XP we can set the Path variable by going to control panel > System > Advanced > Environment Variables. Select the Path and click Edit and add your path to the existing path.

Installing Drush

There is a documentation about installing Drush in Windows http://drupal.org/node/594744 . This document explain Drush installation by using GnuWin32. But I already installed UnxUtils so I don’t require GnuWin32. So first 3 steps in the http://drupal.org/node/594744 can be ignored. I extracted drush in C folder. As specified in the step 4 of http://drupal.org/node/594744 I edited drush.bat with the path of php.exe and path of drush.php. I added following statement in drush.bat file
@E:\xampplite\php\php.exe C:\drush\drush.php %1 %2 %3 %4 %5 %6 %7 %8 %9

Because I used xampplite to install php, php is there at path E:\xampplite\php\php.exe.Now I executed drush command from the windows terminal by specifying -r and -l attributes.
After that I got an idea why can’t I keep this php, drush paths also in path so that I can directly drush.bat file as it is. Yes I added php, drush, mysql paths in Windows Path variable. The everything works file.

Installing cvs

I downloaded TortoiseCVS from http://www.tortoisecvs.org/download.shtml. This is a exe file, I can simple double click and execute.

That’s it, Now I installed all the things necessary to work on drupal with a linux like environment. Now I am happy to work on windows.





Using Emacs to edit PHP files

19 03 2009

When we are programming on any language a good editor(emacs :)), lint (which gives syntax errors) and interactive debugger helps us to code fast.

I want to use emacs to edit php files. There are other IDEs which supports lint and debugger but I feel much comfort with emacs. So my idea is to find good php-mode for emacs and I want to find sytax errors in emacs itself. And I want to find good interactive debugger like python debugger.

I found php-mode and compiler for php but I could not find interactive debugger in the command line for php.

PHP-mode for emacs:

http://sourceforge.net/projects/php-mode/

Download php-mode from the above location. Extract the compressed file and copy php-mode.el to emacs lisp path.
Refer this link for emacs lisp path http://www.gnu.org/software/emacs/elisp/html_node/Library-Search.html.
or you can put this php-mode.el file in some directory and you can add this directory to lisp path. To add this new directory to lisp path add this line in the .emacs file.

(add-to-list 'load-path "~/path/to/new/directory/")

And add following lines to .emacs file to enable php-mode

(require 'php-mode)
;; To use abbrev-mode, add lines like this:
(add-hook 'php-mode-hook
'(lambda () (define-abbrev php-mode-abbrev-table "ex" "extends")))

Enable Tab indentation to 2:

The php-mode indentation for arrays is not good. It will indent like this

$a = array(
           'foo' => 'bar',
           'gaz' => 'gazonk',
          ) ;

If the associated array is having another associative array. then it will be like this

 

$a = array(
           'foo' => array(
                          'bar' => 'bar info'),
           'gaz' => 'gazonk',
          ) ;

If we are having more values like that then the code does not look good. It looks good if it will be like this.

$a = array(
  'foo' => array(
    'bar' => 'bar info'),
  'gaz' => 'gazonk',
) ;

To get view looks like that add following code in .emacs file

(defun clean-php-mode ()
(interactive)
(php-mode)
(setq c-basic-offset 2) ; 2 tabs indenting
(setq indent-tabs-mode nil)
(setq fill-column 78)
(c-set-offset 'case-label '+)
(c-set-offset 'arglist-close 'c-lineup-arglist-operators))
(c-set-offset 'arglist-intro '+) ; for FAPI arrays and DBTNG
(c-set-offset 'arglist-cont-nonempty 'c-lineup-math) ; for DBTNG fields and values

Enable php mode for drupal module files:

To enable php-mode for drupal module file add these lines to .emacs file

(add-to-list 'auto-mode-alist '("/drupal.*\.\(php\|module\|inc\|test\|install\)$" . php-mode))
(add-to-list 'auto-mode-alist '("/drupal.*\.info" . conf-windows-mode))

In place of /drupal you can put your drupal code base path. I put my drupal project at /var/www/ directory I replaced /drupal.* with /var/www/

PHP lint:

Hardly no body use php in the command line, so to use we need to install php-cli (php command line interface). Using php at the command line will make us to find errors easily and fast. We can find syntax error with the following command.
php -l <php file name>

To find more about php-cli option follow this link
http://www.php-cli.com/php-cli-options.shtml

I am using emacs so I can directly check syntax errors in emacs without running the above command on the command prompt. To do that add the following lines in the .emacs file.

;; run php lint when press f8 key
;; php lint
(defun phplint-thisfile ()
(interactive)
(compile (format "php -l %s" (buffer-file-name))))
(add-hook 'php-mode-hook
'(lambda ()
(local-set-key [f8] 'phplint-thisfile)))
;; end of php lint

To check syntax errors press “F8” function key on emacs editor, then currently opened php file will complie and show the syntax errors if any.

PHP Debugger: I tried to find good interactive debugger like python debugger. But I could not find it. So I might be developing one in the future.