PHP Emacs
PHP Emacs
More
Ben's Journal
A random collection of thoughts, comments and photos from Ben Simon.
Then, yesterday, my possible convert came to me with a simple question: what mode do you use to edit PHP
Considering that most of the code I write is PHP, you'd think I would be ready to deliver a solid answer. Instead, I
mumbled something about having a hacked bit of code I relied on, but really wouldn't recommend it for the general
use. Yeah, not cool. I closed out the conversation with a promise: I'd see what the latest PHP options were and report
back.
PHP is actually a fairly tricky language to build an editor for. That's because depending on the style it's written in, it
can range from disciplined C like code to a gobbly gook C like code with HTML, CSS and JavaScript all mixed in. Add
to that the classic emacs problem of having too many 85% solutions, and there's definitely room for frustration. Check
out the emacs wiki to see what I mean. You'll find lots of promising options, but no one definitive solution.
After reading up on my choices and trying out some options, I do believe I have a new recipe for PHP + emacs
success. Here goes.
Melpa is a code repository that emacs can magically pull in packages from. Back in the day, adding packages to
emacs meant downloading, untarring, and running make. Like all things emacs, the process has been both
streamlined, and of course, is fully executable from within emacs. To add Melpa, you'll need to follow the instructions
here. Assuming you have a modern version of emacs, this probably just means adding the following to your
(add-to-list 'package-archives
'("melpa" . "https://melpa.org/packages/"))
Emacs comes with a PHP mode out of the box, but it seems as though this one (also named php-mode) is more
modern. I love that the README talks about PHP 7, showing just how up to date this mode is with respect to the
latest language constructs.
Installing this mode, assuming Melpa is cooperating, couldn't be easier. Just run: M-x package-install and enter
php-mode.
1 of 10 8/4/18, 8:45 PM
Ben's Journal: Emacs + PHP - A Modern and (F... http://www.blogbyben.com/2016/08/emacs-php...
My preference for indentation is 2 spaces and no insertion of tab characters. Two other tweaks I was after was to turn
off HTML support from within PHP and enable subword-mode. All these tweaks are stored in a function and attached
to the php-mode-hook. This is all standard .emacs stuff:
(defun bs-php-mode-hook ()
(setq indent-tabs-mode nil)
(setq c-basic-offset 2)
(setq php-template-compatibility nil)
(subword-mode 1))
php-mode above is ideal for code centric PHP files. But what about those pesky layout files that contain HTML, CSS
and JavaScript? For that, web-mode.el looks especially promising. Installation and configuration mirrors php-mode
Here's the code I use to customize it:
(defun bs-web-mode-hook ()
(local-set-key '[backtab] 'indent-relative)
(setq indent-tabs-mode nil)
(setq web-mode-markup-indent-offset 2
web-mode-css-indent-offset 2
web-mode-code-indent-offset 2))
Web-mode.el is quite impressive and who knows, it may actually fulfill all my PHP needs. If that's the case, I may
choose to stop using the php-mode. However, for now, I like the idea of being able to switch between the two modes.
Which brings me to the next step...
Inspired by this tip on the EmacsWiki, I went ahead and setup a quick toggle between php-mode and web-mode
Here's that code:
(defun toggle-php-flavor-mode ()
(interactive)
"Toggle mode between PHP & Web-Mode Helper modes"
(cond ((string= mode-name "PHP")
(web-mode))
((string= mode-name "Web")
(php-mode))))
Now I'm only an F5 keystroke away from two different editor strategies.
2 of 10 8/4/18, 8:45 PM
Ben's Journal: Emacs + PHP - A Modern and (F... http://www.blogbyben.com/2016/08/emacs-php...
When I have a say in the matter, I tend to be pretty particular about which files in my source tree are pure code and
which are pure markup. At some point, I could codify this so that files in the snippets directory, for example, always
open in web-mode, whereas files loaded from under lib start off in php-mode. Such is the joy of having a fully
programmable editor at your fingertips. You make up the rules!
For bonus points, I decided to play with ac-php, a library that supports auto completion of function and class names. I
followed the install here, updated my .emacs file as noted below, created the empty file named .ac-php-
conf.json in my project's root and then ran M-x ac-php-remake-tags-all. Once that's done, emacs now
shouts completions like crazy at me:
It's slick, I'll give you that. I think I may have to see if I can turn down the volume, a bit. Here's what my .emacs
looks like to configure php-mode: now looks like:
(defun bs-php-mode-hook ()
(auto-complete-mode t) ;; «
(require 'ac-php) ;; «
(setq ac-sources '(ac-source-php )) ;; «
(yas-global-mode 1) ;; «
(setq indent-tabs-mode nil)
(setq php-template-compatibility nil)
(setq c-basic-offset 2))
Bye-bye hacked PHP code. Hello modern, feature filled, super easy to install, mega powerful code.
Update: updated the web-mode hook I'm using to make sure all code on the page, not just markup code, is indented
2 steps.
3 of 10 8/4/18, 8:45 PM
Ben's Journal: Emacs + PHP - A Modern and (F... http://www.blogbyben.com/2016/08/emacs-php...
Linkwithin
3 comments:
Reply
Anonymous 3:39 PM
One quick note - the MAJOR-MODE buffer-local variable identifies the currently selected major mode
perhaps more reliably than a string match, so it might be worth considering its use in your mode-toggle
binding - (eq major-mode 'php-mode), et al.
Reply
Fuco 5:20 PM
I've been working on and off on https://github.com/Fuco1/php-refactor ... a simple-minded refactoring, I wrote
my own very very incomplete php parser (but it's fast! only parsing the state I care about). So far it can't do
much but already it helps me a lot. Feature requests or code welcome :)
The installation is also a bit of a PITA, that could be worked out too.
Reply
4 of 10 8/4/18, 8:45 PM
Ben's Journal: Emacs + PHP - A Modern and (F... http://www.blogbyben.com/2016/08/emacs-php...
Create a Link
LinkWithin
5 of 10 8/4/18, 8:45 PM
Ben's Journal: Emacs + PHP - A Modern and (F... http://www.blogbyben.com/2016/08/emacs-php...
actionscript (1)
android (9)
arlington (660)
biking (31)
blogging (172)
business (332)
carry (152)
chutzpah (173)
commutecast (4)
cooking (11)
definition (10)
design (138)
development (423)
discoveries (34)
6 of 10 8/4/18, 8:45 PM
Ben's Journal: Emacs + PHP - A Modern and (F... http://www.blogbyben.com/2016/08/emacs-php...
drawing (11)
education (97)
eduction (11)
emacs (25)
family (322)
firsts (130)
fishing (10)
food (64)
forth (10)
friends (207)
fun (1204)
funny (1)
gardening (31)
gifts (13)
gotcha (196)
hacking (930)
hardare (1)
hardware (390)
i2x (105)
idea (29)
jacksonville (4)
jewish (170)
links (6)
linux (54)
macOS (6)
7 of 10 8/4/18, 8:45 PM
Ben's Journal: Emacs + PHP - A Modern and (F... http://www.blogbyben.com/2016/08/emacs-php...
miva (3)
mobile (286)
model100 (4)
music (134)
named (46)
on-the-cheap (42)
outdoor (4)
outdoors (442)
outside (5)
Panama (8)
parenting (206)
paris (10)
photograph (1)
photography (413)
php (19)
Plymouth (1)
pma (176)
politics (196)
programmer (1)
programming (590)
questions (2)
recommendations (448)
recoommendations (1)
religion (2)
review (236)
8 of 10 8/4/18, 8:45 PM
Ben's Journal: Emacs + PHP - A Modern and (F... http://www.blogbyben.com/2016/08/emacs-php...
reviews (66)
run (2)
running (124)
runnning (1)
scheme (129)
sdr (3)
shopping (11)
simcha (54)
sql (1)
tasker (40)
tenspotting (2)
tools (416)
toys (2)
travel (472)
twins (83)
unix (78)
us (384)
US Open (6)
venice (4)
video (23)
wearable (7)
whiteboard (21)
9 of 10 8/4/18, 8:45 PM
Ben's Journal: Emacs + PHP - A Modern and (F... http://www.blogbyben.com/2016/08/emacs-php...
windows (119)
writing (21)
Search
Report Abuse
Contributors
Ben Simon
Shira
10 of 10 8/4/18, 8:45 PM