top | item 6909463

Why Emacs is a better editor – case study for JavaScript developer

139 points| lelf | 12 years ago |blog.binchen.org

104 comments

order
[+] mickeyp|12 years ago|reply
Js2-mode is an interesting case of an author (Steve Yegge) going against the fold of how Emacs is supposed to interact with programming modes -- by writing his own parser and syntax highlighter and eschewing the built-in way, which is to use Emacs's font locking, which itself is driven by regular expressions and the odd lisp function.

It's a shame Emacs lacks the facility to properly capture the more complex requirements of languages today - especially files with mixed content, such as HTML, Javascript and templating tools. The few that do support this do it using a variety of clever hacks to work around this problem; and still, using regular expressions to capture the nuances and complexities of some languages make it an error-prone way of writing major modes in Emacs. Not to mention you lose out on the many advantages of having an AST in memory for code jumping, refactoring, syntax checking and what have you.

Emacs could do with a shake-up in this space. The proposed solution (Semantic Mode / CEDET) has never really caught on, even though it adds much of the same functionality to Emacs; my suspicion is that this is because Semantic/CEDET is ridiculously complex and lacking in documentation.

[+] klibertp|12 years ago|reply
> Semantic/CEDET is ridiculously complex and lacking in documentation

So, sooo much this. And it's a real shame, because semantic is quite a usable tool once you get to know it.

One thing I really wanted to have was a semantic index, a database of semantic symbols inside a project. I wanted to be able to write "Form" and have it display all the class names containing "Form". Grepping (even ag) is just much too slow and building my own index was too much work. I then saw "semanticdb" module.

A week, I'm not kidding, a week - instead of half an hour - later I finally managed to preload a custom tag database and to query it. And that was with Python code, which has Semantic parser already, which is well integrated into the rest of CEDET.

Now I'm fighting with CoffeeScript/LiveScript support. They both expose their parsers - I can get their AST with one line of code. After another week I still don't have working Semantic plugin for them.

When I last checked, the activity on a CEDET and Semantic mailgroup was almost non-existent. The home page for CEDET looks like it comes from last century and even info pages are not a big help.

It's a friggin shame, because when Semantic works, it does work well.

[+] brudgers|12 years ago|reply
Is the difficulty in accessing AST's in EMACS a valid criticism of EMACS when so few programming language specifications specify such access and few of the communities around languages that don't build such tools into open source implementations? It took Steve Yegge to do it for the ubiquitous JavaScript.

What Yegge's project shows is: that EMACS does not lack the facility to do what is needed [LISP is it's command language after all], and that implementing the task is non-trivial even for skilled programmers.

[+] rjsen|12 years ago|reply
Have you seen MuMaMo? It does a pretty good job handling mixed language files.

I'd also point to Ensime as an example of a language plugin that uses an external parser to check code.

[+] marijn|12 years ago|reply
Emacs Lisp is such a horrible language, and the Emacs ecosystem is so firmly stuck in the past (mutable state and magic hidden side effects and interactions everywhere) that writing extensions for it is much harder than it should be. It has enough history and a smart enough user base to have a lot of cool functionality, but the idea that it is somehow exceptionally wonderful just doesn't hold.

(I use Emacs for all my coding, and Common Lisp is my favorite programming language, so, if anything, my biases would be in favor of Emacs. Sadly, it doesn't really deliver -- not that other editors get their interface right. Don't get me started on vim. ST is somewhat better, but doesn't expose powerful enough tools. If I may toot my own horn, I believe my own project, http://codemirror.net , does a lot of things right on the editor API level that popular editors get tragically wrong.)

[+] dilap|12 years ago|reply
Fellow emacs user/addict. Emacs is, to steal a phrase from the politics, the "least bad form of editor" IMO. Your criticism of elisp, dynamic scoping, and mutable-state everywhere is spot-on. To use emacs is to constantly put up with things in a state of "almost works, kind of broken". Partly the language and design, and partly just the culture of the editor, I think.

(For me personally being able to run in a terminal is a huge feature, which means I'll probably be stuck w/ emacs forever -- no one making new editors seems to care about terminal support.)

[+] barrkel|12 years ago|reply
Elisp isn't really horrible. It sits on top of a local maximum in the fitness landscape. So often, little tweaks are needed, and dynamic scope lets you tweak more than a more engineering-oriented language would. It gets adapted to the task at hand in a worse-is-better, almost Darwinian way. The ability to redefine and tweak functions called by other functions, eval'd live in a buffer, is key. Without that, it would be a tenth the editor it is.

JS would not be a better language. Guile could be.

I used to think a more semantically aware setup would be better. But the costs of semantic awareness are enormous, because a single mismatch between what your editor understands, and what the underlying language can represent, breaks everything. This makes things fragile; parser error recovery is not an easy thing to get right. Some artfully written regexes can go further, in a practical sense.

[+] mickeyp|12 years ago|reply
It's stuck in the past because of its architecture and no doubt a lack of people to actually carry out large, wholesale changes.

Part of what Emacs a great editor and Elisp a great tool for mutating -- if one can call it that -- is exactly the same things that make it "bad." Yes, it has dynamic scoping, and yes you can change variables outside your scope -- which is exactly the level of granularity your average Emacs hacker wants. He's not looking to write rock-solid code for the mars rover; he's trying to make M-x compile a bit less crap or a bit more specific to what he's doing.

Say what you want about side effects and advising and all the other "terrible" practices that, in any sane software project, would never pass peer review -- in Emacs it enables you to hot plug, duct tape and jury rig Emacs to get it to do what you want.

The last 2-3 years has seen a massive resurgence in Emacs packages and there's a lot of buzz in the community, I feel, in trying to push things in the right direction. But it takes time. And people. And we need both.

[+] jrockway|12 years ago|reply
Writing editor extensions is a totally different set of programming compared to writing applications that will run unattended across terabytes of data for years. A programmer is always sitting in front of a programming environment while your code runs. Therefore, it makes sense to allow things like deep access to the internals (dynamic scope, etc.), because if something goes wrong, it's only because the person currently in front of the computer told it to go wrong. And she's a programmer, so she can fix it.

I guarantee you that if Emacs used a "real" extension language, with lots of OO and state hiding, you'd be yelling obscenities into the computer the first time you wanted to tweak some extension you had. Have you ever seen an Eclipse user tweak one of his extensions? Didn't think so.

[+] javajosh|12 years ago|reply
Hi Marijn! Love your work, especially Eloquent JavaScript[1] which is my goto link for when people ask me "where should i start if I want to learn how to code". And CodeMirror is really an amazing piece of kit, of course.

So out of curiosity, how far away do you think CodeMirror is from being usable as a real local editor - and by that I mean how far away would you be from using it instead of Emacs? It seems like it would be straightforward to embed in something like node-webkit[2]. What else then remains?

[1] http://eloquentjavascript.net/ [2] https://github.com/rogerwang/node-webkit

[+] brotheryuca|12 years ago|reply
While I would not say that Elisp is the greatest programming language every created, I would hardly call it horrible, nor would I call it entrenched in the past. The Emacs designers seem to put real effort into improving the language over time, even to the degree of breaking backwards compatibility. That's pretty hard to for an editor that greatly depends on the functionality of user-written extensions. Also, consider the recent addition of lexical scoping. That's a brobdingnagian change to a programming language, editing language or not.
[+] peterashford|12 years ago|reply
I disagree. I've written code to modify how emacs runs for me. I've never customised any other editor or IDE with the same ease of emacs - and I say this as an IDE slut - I'll use everything and anything.

That said, an emacs rewrite in Clojure is a wet-dream of mine (emacs like goodness with the ability to use modern GUI libraries, ANTLR parsers etc... and speed!)

[+] freyrs3|12 years ago|reply
Elisp is bad, but not nearly as bad as vimscript.
[+] aaronem|12 years ago|reply
I'm seeing in-editor browsers more and more lately. I'm not sure why. Given the difficulties with arbitrary key binding, and with filesystem access -- both sine qua non of a programmer's editor -- it seems like any such tool starts out half-broken by design. Don't get me wrong; yours is quite nice, as far as they go. I just don't see how they go far enough to have been getting the consideration they have lately.
[+] foobarqux|12 years ago|reply
Emacs isn't good enough and there really aren't any radical improvements happening. The problem is the alternatives aren't better.
[+] raganwald|12 years ago|reply
To put it another way, "Correlation between certain tools and smart people does not imply causality." Or exclusivity, for that matter.
[+] davidw|12 years ago|reply
Elisp has its wart, but... all told, for something created in the 1980ies, it's not that bad.
[+] Sssnake|12 years ago|reply
>Emacs Lisp is such a horrible language

Beats the hell out of javascript.

[+] bjourne|12 years ago|reply
The point is that js2-mode interprets javascript which enables it to offer much more exact syntax highlighting and navigation than purely regexp based approaches. FWIW, js2-mode is an exception and most of emacs highlighting comes from regexps via font-lock mode. The author could have made the same point about Visual Studio vs other editors as the former parses syntax which most others don't.
[+] mickeyp|12 years ago|reply
Indeed. But it's still a nice thing to be able to do; this is, after all, the Emacs way -- for good or ill!

One useful technique to make Emacs appear cleverer than it is vis-a-vis code searching and navigation is to navigate by syntax highlighting - most of which is assigned one of several types (constant, keyword, statement, etc.) -- and simply use that. It's hardly perfect, but it's good enough for 90% of the basic navigation you need to do.

[+] johncoltrane|12 years ago|reply
I kind of feel the hacker spirit in what you did but… what about fixing your JavaScript instead?

Sublime Text: http://imgur.com/p27i5L7

Vim: http://imgur.com/OlVbSdO

[+] klibertp|12 years ago|reply
No.

The tool should support even the most hideous constructs; saying that "but what you wrote could be written better" is completely orthogonal to "the tool couldn't handle it".

[+] imissmyjuno|12 years ago|reply
Which Vim plugin uses `Btag` ?
[+] spullara|12 years ago|reply
If you are going to argue for a tool that understands the semantics of the language you are editing, you are doing yourself a real disservice not using a JetBrains editor if they support the language. They do support Javascript and their support is quite great.
[+] Prefinem|12 years ago|reply
An editor with a plugin with code I modified makes this the best editor...

Because anything you put time into to makes it automatically better for your use case is actually better.

[+] h1fra|12 years ago|reply
This is quite stupid. Saying emacs is better but showing that emacs do actually the same sublime text mistake.... Emacs is extensible; ST too.
[+] mickeyp|12 years ago|reply
I think you're misunderstanding it. JS2-mode is a few years old and a generic javascript mode with a built-in AST and all the goodness that comes from that.

It does not inherently understand require.js. Nor does ST. With Emacs's JS2-mode, thanks to its AST, it is possible to augment the library so that it does understand require.js -- and the author does so without resorting to piles-upon-piles of regexp hacks.

[+] caipre|12 years ago|reply
AFAIK, ST2 doesn't have a plugin for a Javascript interpreter (though I haven't used ST2 for over a year now). You could extend the javascript.sublime-language file, but that only supports regexes so you won't have the same context as with the AST.

Your main point stands though: all major editors have the ability to add plugins to achieve this same functionality.

[+] platz|12 years ago|reply
Fwiw, visual studio evaluates your JavaScript similar to this providing you pretty good js intellisense/autocomplete
[+] podperson|12 years ago|reply
The key thing is that javascript is built into emacs, which is a good argument for javascript to be built into any serious programmer's editor (since (a) chances are good the programmer knows javascript, (b) you can get a good javascript engine for cheap, (c) many programmers will actually be using your editor to write javascript as a bonus).

I was playing around with another interesting editor (Chocolat) which actually uses a Javascript engine to identify symbols in Javascript source files; but it does this far worse that can be done easily with regex (and in fact makes terrible errors doing so).

Indeed, better results than that shown in the article are achieved by fancy new editors (like Espresso) and crufty old editors (like BBEdit) using regex and brute force. Fundamentally you've got an anonymous function or a function with a common name, and you want some outer context to further identify it -- regex can do that.

[+] watson|12 years ago|reply
I wonder why he obfuscated the "validOptusUsername" key in the screenshots
[+] pulmo|12 years ago|reply
Serious question: Did anyone ever chose an editor because of some "Why X is better than Y" article?
[+] skylan_q|12 years ago|reply
"Why X is better than Y" would be too clean-cut for my case, but it's not far off.

I wanted to start learning lisp and was told emacs was the best choice for it. I read that Richard Stallman made it and Linus Torvalds uses it. That was enough to get me started and it's now my editor of choice.

But that's not to say that I don't know how to edit, save, search and (force) quit vi. :)

[+] justinhj|12 years ago|reply
I think the more important question is how many otherwise interesting articles get buried because they don't have a title like "Why X is better than Y"?
[+] groups|12 years ago|reply
>Did anyone...

Over months I read many articles describing cool things you can do with emacs you can't do with vim, and eventually I switched.

So: one article, no. Many, yes.

[+] Derbasti|12 years ago|reply
No, but one could imagine choosing an editor because of some "look what cool thing I can do in X" article.
[+] csmuk|12 years ago|reply
Kevin Flynn uses vi - 'nuff said:

http://i.imgur.com/rMdKYde.jpg

(this is in jest but no doubt I'll be downvoted...)

[+] pault|12 years ago|reply
Where is vi? I only see two shell sessions and top.
[+] SimHacker|12 years ago|reply
The wrong people have been drunkenly driving Emacs into the ditch for the past decade or so, and they're really screwed it up in so many ways, usually trying to be way too clever and solve problems that really aren't worth solving.

They made "line-move-visual" the default, which in the service of making Emacs behave more like Microsoft Word, it makes ^N and ^P across wrapped lines stay on the same line and move to the part of the same line that is wrapped above or below the cursor, totally breaking type-ahead predictability and keyboard macros. That totally pissed of the late and long time Emacs user Marc Crispin, and he explains why it's such a terrible idea that breaks keyboard macros: http://compgroups.net/comp.emacs/line-move-visual/274792

It totally breaks keyboard macros, one of the most important things about Emacs. It definitely should not have been made the default, since keyboard macros are a way of life for real emacs hackers. I totally agree with Marc's take on the problem, and I am terribly disappointed that RMS let somebody get away with changing it that way, and making that horrible mode the default.

They just can't stop trying to make it more like Microsoft Word, while failing to actually achieve that goal, and only succeeding in inflicting half-assed solutions with harmful unpredictable side-effects on the users.

Another example is how the region highlighting and the weird pending delete behavior terrorizes me that sometimes but not all the time according to rules I just can't figure out or tell by looking at the screen, when I type something that I intend to insert, a huge chunk of my buffer might just disappear, but sometimes it doesn't. So now Emacs has become unpredictable and malicious. I have to do a dance of "insert character, undo" to know that I have canceled the pending delete mode. You can't tell if you are in pending delete mode just by looking at the screen and seeing the obnoxious highlighting, because sometimes it highlights the region and isn't in pending delete mode, and sometimes it highlights the region and is in pending delete mode. I just wish it would stop highlighting the region, because any time I set a mark and move around, it highlights half of the screen at no use to me, and that just gives me a headache, and I have to insert a character an undo it to cancel the highlighting, and until I cancel the highlighting I am living in terror that my next keystroke will delete a huge unseen portion of the buffer.

And the thing that REALLY pisses me off is the lame-assed attempt to make ^A and ^E ignore the prompt in shell mode. There is an extremely simple reliable solution to the problem of separating the prompt from your input in shell mode so you can always get to the beginning of the line with ^A, and that is to have a newline at the end of your prompt, so every line you type in is on the whole line and does not have a prompt prefix, therefore no half-assed magic is necessary.

But the half-asses magic is terribly implemented and has bizarre side-effects that screw me all the time: I get these fucking invisible force fields inserted into the line between my characters whenever I yank some text onto the line that has a prompt or a command line on it, and I can't move past them with ^A or ^E (but sometimes I can -- they're not predictable which is even worse)! And I can get any number of these fucking invisible force fields on my line, so in order to get to the beginning of the line I have to go ^A, look at the screen to see if I made it, type ^B ^A if I didn't, again and again, until I drill past all the invisible force fields that are trying to make my day happier.

And then I have to mark the beginning of the line I want to edit and repeat, go to the end of line by drilling back some number of opposite facing invisible force fields (which may be in different places than the ones going the other direction), and then kill the region, go to the end of the buffer, then yank the entire line to get a copy of it without any fucking invisible force fields in it.

But if there is an invisible force field in the line, or I just want to edit a few characters of the line and that adds some invisible force fields that were not even there before, and then I hit return in the vain hope of re-entering the entire line but not the prompt, it just enters the tail of the line after the last invisible force field, making an error in the shell, or sometimes even executing a totally different command than I intended and totally fucking me over.

I would really like somebody to explain what the fuck the idea behind the invisible force fields are, and give me the address of whoever thought is was such a brilliant idea, so I can send a robot drone to firebomb their house, or worse yet send RMS to live with them for a few months. Why did they do something so terrible, that totally fucks up such common important operations, to solve a trivial problem that has a much better solution?

Emacs used to be totally predictable. I learned it at 300 baud over the ARPANET on ITS, and I could type ahead hundreds of characters to do all kinds of stuff, and then go take a piss or get a drink or take a bong hit, and come back later, and it would be in exactly the state I meant for it to end up in. Now, I can't do that even on a local display. And for some fucking reason, it waits for up to five seconds sometime before updating the display when I'm running it in a terminal (especially when I start an incremental search or query replace). What the fuck is that about??! Not only is it totally unpredictable and forces me to stop what I'm doing and wait for it to catch up so I can figure out what state it guessed its way into before going on, but it won't paint the screen for five seconds when running on a modern top-of-the-line computer!

Emacs has become a shrine to outrageous violations of the principle of least astonishment. I just can't figure out how they could have come up with so many ways to corrupt it, when it used to work so well. It's totally unpredictable now, and I can't type ahead any more, so I have to type a few characters, look at the screen to see how it misinterpreted my intentions, and then try to work around the misunderstanding.

[+] nichochar|12 years ago|reply
vim.
[+] Symmetry|12 years ago|reply
Could you elaborate? Have you seen someone intagrate a javascript interpreter the same way with vim, or are you saying that modal editing is enough of a productivity boost to make up for it's lack?