Next: , Previous: Editing, Up: Top


7 Completion

Completing function names, identifiers and PHP keywords is possible in PHP mode. In PHP mode, it is possible to complete identifiers defined by the PHP programming language, and to complete functions defined by you.

For PHP mode to know the functions of keywords of PHP, it must have access to the PHP manual. Download the HTML archive of the manual made of separate files for each function.1 Unpack the files to a directory, and define the variable php-manual-path to the location of these files. Configure this setting with either M-x customize-variable RET php-manual-path RET, or by setting its value in your Emacs initialization file (~/.emacs).

For functions and classes you define, you'll need to provide them to Emacs as a tag file.2 Creating a tag file (TAGS) from a PHP source code file is done by running the etags command on the file.3 The etags command can even generate tag files for providing completion across multiple files. For larger projects and PHP coded libraries, use a system like a Makefile to generate and maintain the tag tables.

Run,

     etags file

to create a TAGS file for file.

To generate a TAGS file for all the PHP files in the current directory, run

     etags *.php

To enable a tags file you need to use the following command.

C-u M-x visit-tags-table
Specify the TAGS file that PHP mode should use to find completion candidates for the current buffer.
M-x visit-tags-table
Specify the TAGS file that PHP mode should use to find completion candidates for all buffers, and even other modes other than PHP mode.

If you are interested in any one or more of the following consider using the variable php-completion-file.

  1. avoid regenerating the completion table from the PHP manual every Emacs session,
  2. generating a completion file from a facility external to Emacs tags,
  3. or provide more granular control over the completion rules for files;

Configure this setting with either M-x customize-variable RET php-completion-file RET, or by setting its value in your Emacs initialization file (~/.emacs).

To create a file that can be set for php-completion-file, create with the following shell commands with the HTML version of the PHP manual installed at /usr/share/doc/php-manual/en/html.

     $ cd /usr/share/doc/php-manual/en/html
     $ ls -1 function*.html \
       | sed -e 's/^function\.\([-a-zA-Z_0-9]*\)\.html/\1/' \
       | tr - _ \
       > ~/.emacs.d/php/php-completion-file

In larger projects or with other specific needs, conventional methods of setting these and other variables may be inadequate. As of 2008, third-party packages do exist for Emacs to configure Emacs settings on groups of files or for directory trees, including dirvars.el.

M-TAB
ESC TAB
C-[ TAB
Complete function name at point (php-complete-function).
C-.
Show in the minibuffer the argument list for the current function at point (php-show-arglist). The argument list is generated by finding and jumping to the respective function's location specified in the tag file (see above). The argument list is then read and printed to the minibuffer.

Footnotes

[1] http://www.php.net/download-docs.php

[2] See Tags Tables.

[3] See Creating Tags Tables.