How can I best do source code browsing inside Emacs? - emacs

I have a workflow where I use grep and other tools in a shell for searching in different projects even though my main editor is emacs. I usually work on bug fixing and minor development of source code that is often unknown to me so searching the code is important. The langages I mostly work in are php, ruby, java, perl and sometimes python.
Is there some common IDE extension in emacs that would enable me to have functionality like "goto definition" from multiple files that span all these languages? Are there some other modules that could be useful, either code browsing or indexed search?

If you want to work with lots of different languages, ctags is pretty flexible. See the EmacsWiki for instructions on integration of ctags.

Code browsing can mean a bunch of different things. The mode you mention of using grep implies to me you are looking for function definitions, or perhaps looking for uses of a particular function.
While CEDET supports all the languages you list (some with the ctags parser), starting fresh in a new source code area is something CEDEt isn't too good at until it has indexed the entire project for those tasks you listed. You can, however, use GNU Global or idutils. I think idutils supports more languages.
In CEDET, the `semantic-symref' and related commands will do a grep-like operation with grep, or global, or idutils (depending on what type of tag table you created.) Unlike grep style output, the symref output buffer shows which functions are using the command in question. You can then execute macros on the hits of the symbol you care about to do large refactoring operations.
CEDET also supports a tags like jump to function, though if you are already using something like ctags that works fine too. CEDET is better when it comes to handling polymorphism in some cases.
With CEDET, you can also get structured browsing via imenu, speedbar, and ECB (the emacs code browser). ECB is particularly good in that the methods buffer allows you to quickly navigate to different parts of a class. Particularly handy for classes where the pieces are spread around, like in C++. CEDET can even create cute UML diagrams of class inheritance structures which are connected to your code.
CEDET does take some learning, and some of the languages you list are not fully supported for all the tools, though the basic browsing discussed above should be ok.

etags-select (which you can get from ELPA) makes browsing your TAGS file nicer, in my experience, if you bind M-. to etags-select-find-tag.

As well as etags/ctags try running grep with M-x grep rather than in a shell, to get clickable links in Emacs rather than having to manually find the file and line matching the grep output.

For code browsing, you can also use M-x imenu-tree

Tags browsing, with dynamic completion, filtering (substring, regexp, fuzzy), cycling:
http://www.emacswiki.org/emacs/Icicles_-_Emacs_Tags_Enhancements

Related

what are the code completetion systems available for emacs core (23.3 or 24)?

I use auto-completion mode which is not inbuilt. good but not the best IMO. So I was wondering is there inbuilt generic plugins for completion in emacs like you see in IDEs.
to name,
dabbrev
hippie expansion
I think these two are different from auto-completion mode kind which I am looking for.
EDIT:
I like to extend one of any completion system to support a scripting language which I use regularly for scientific data visualization.
There is CEDET package in GNU Emacs starting from version 23, but it slightly outdated, and it's better to use CEDET from its repository. I have an article on setting and use CEDET from repository.
Why don't you want to use external packages? With new packaging system in GNU Emacs (package.el) and with things, like el-get, it's very easy to install new packages into GNU Emacs...
CEDET's autocompletion is the only drop-down completion package provided in core Emacs. The core in-place completion packages are hippie-expand and dabbrev.
The primary non-core packages which provide drop-down completion are auto-complete and company; the former appears to be the most popular these days.
There's also anything, which is an interactive completion package. While it doesn't provide drop-down completion at the cursor position, the way it works is really rather nice, so it's worth investigating.
With a bit of tweaking, dabbrev and hippie-expand can be a lot more effective than you might expect, but if you want something more visual and interactive, my advice is to look beyond the core packages and go for auto-complete or perhaps anything.
(For what it's worth, I use both hippie-expand and auto-complete myself.)
As a C programmer I tried http://cx4a.org/software/gccsense.
I read an interview where the CEDET author said that this is actually better because it hooks directly into gcc. However its quite a hassle to install gccsense because I had to compile a modified gcc. Once this package is included in distributions I would use it.
You may also want to try out predictive mode. While not exactly a code completion package, it comes close to it by predicting your frequently used words, which in a coding platform is definitely a limited vocabulary.
http://www.dr-qubit.org/emacs.php
The emacs wiki has a good explanation too.
http://www.emacswiki.org/emacs/PredictiveMode

Emacs for Erlang with vi like keybindings and handy short references?

I have come accross vi-style keybindings for Emacs, but my past experience in mixing Lisp based config in .emacs file for enabling various modes etc., hasn't been very pleasant so far.
Read several articles on Emacs + Erlang, but is there something that might be slightly easier for folks familiar mostly with vi (and who are unfamiliar with Lisp)?
The entire set of possibilities (keybindings) is quite overwhelming. Is there a condensed key-map/shortcuts reference specially relevant for Erlang development?
Just use vi and a command line. There are numerous people doing that and it seems to work just fine for them. Even though I use Emacs, it is not the Emacs-erlang interaction I use. Rather I usually just have a separate Erlang shell and then I load modules in that shell with the l() command for interaction.
Answering your questions one by one:
Debugging in Erlang is funny. Firstly, if your program is kept functional you will find that you need much less debugging to figure out what the program does incorrectly because you can simply write small tests for each of the numerous functions to test its correctness. And you can add those into unit-test frameworks.
As soon as you add multiple processes to the mix you will find that traditional debugging doesn't work anymore anyway. The trick is then to trace and assert, and you will need to learn a way to read programs without running them and see where they go with a debugger.
That said, try to execute debugger:start() in an Erlang shell :) There are also the tracing systems and redbug, a 3rd party tool built on top of them (part of the eper suite).
Profiling can be done with one of the 3-4 profilers: cprof, eprof and fprof are all slightly different in scope and in how much impact they have on your program and if they can be run on a production system or not. I tend to use eprof and I have a knob in my program which will spawn an eprof and then attach it at program start.
Try appmon:start() in a shell.
The standard vi-way of browsing large code bases is to create tags-files so you can jump to the definition point of the thing-under-the-cursor. Emacs can do the same. I have a make tags target to create these files so I can easily make jumps around in the source code. When jumping you have a stack of the former jumps so you can return to the point you jumped from later on.
Finally there are tools like xref which can be worked to create call graphs and find odd things in the code. It will require some coding on your part, but it does provide you with the necessary tooling.
Viper mode provides Vi-style keybindings. That's probably the easiest way to start. Enabling a new mode in your .emacs is a pretty fundamental part of using Emacs, so it's a learning hurdle best cleared early!
The Emacs manual contains more detailed information about making individual changes to your keybindings.
You may find Xah's keybinding tutorial helpful, as it provides many examples.

Maintaining Emacs autoload files for user-installed elisp?

Emacs has this seemingly very nice facility for building autoload files based on magic source code comments ("autoload cookies") of the form ;;;###autoload, which are to be placed on lines by themselves immediately above each definition to be autoloaded; see (elisp)Autoload.
This would seem to be an ideal tool for maintaining autoloads for those little one-file packages that Emacs users inevitably end up installing in their profiles. There's just one small problem: this facility (in GNU Emacs, anyway) appears to be almost entirely focused on generating the loaddefs.el file for Emacs itself, with very little (if any) concession to other uses.
This doesn't stop largish packages from using the autoloads.el machinery to build their own autoload files, but those that I've looked at have a fair bit of rather hairy code devoted to making it do what is needed, though some of the hairiness might be due to GNU Emacs/XEmacs divergence.
(I think XEmacs is a bit better on this front, probably due at least in part to the fact that its official package system uses this machinery to make separate autoload files for each package. Hopefully GNU Emacs' inclusion of the ELPA package system, which also uses this machinery, leads to similar improvements on their side.)
So, my question for you is:
How should I maintain an autoload file for all .el files in a directory, assuming they have all the necessary ;;;###autoload comments (autoload cookies) in place already?
[Hmm. block quotes look a lot coolor on tex.SE...]
I'm currently using GNU Emacs 23.2.1, though the farther back an answer works, the better. (For that matter, it'd be nice if it would also work with XEmacs.)
I'm on Windows, but have MSYS installed alongside Emacs so sh/bash scripts will probably be fine as long as they don't call anything terribly exotic.
[I'm not entirely certain this doesn't belong on superuser rather than on SO. If there is already a package that can take care of this with only a small amount of configuration, it probably does; on the other hand if (as I suspect) there are only fairly rough snippets of code that might require a good deal of direct changes, I think it probably belongs here on SO.]
Check out this answer to "emacs23 / elisp: how to properly autoload this library?".
In summary, you dump all the packages in a particular directory, and create a package of your own update-auto-loads.el, which builds the loaddefs.el and provides a function you can regularly run to rebuild the loaddefs.el file whenever you want.
In 2017, you don't have to maintain it yourself. Instead, you can use a modern package manager such as straight.el which is specifically designed to take care of this (as well as many other things) for you.
You could use package.el instead of straight.el, but package.el has a number of serious problems, and a particularly relevant one is that it's very awkward to get package.el to load a local package you've written yourself, and in particular package.el has absolutely no support for making modifications to a package once it's been installed.
You could use other package managers, as well. I wrote an extensive section comparing straight.el to other package managers.

Eclipse Style Function Completions in Emacs for C, C++ and JAVA?

How Do I Get Eclipse Style Function Completions in Emacs for C, C++ and JAVA?
I love the power of the Emacs text editor but the lack of an "intellisense" feature
leaves me using Eclipse.
M-/ is a quick and dirty autocomplete based on the contents of your current buffer. It won't give you everything you get in Eclipse but is surprisingly powerful.
I can only answer your question as one who has not used Eclipse much. But! What if there was a really nice fast heuristic analysis of everything you typed or looked at in your emacs buffers, and you got smart completion over all that everywhere, not just in code?
M-x load-library completion
M-x global-set-key C-RET complete RET
When I was doing java development I used to use the:
Java Development Environment for Emacs (JDEE)
The JDEE will provide method name completion when you explicitly invoke a jdee provided function. It has a keyboard binding for this functionality in the jdee-mode.
The CEDET package provides completion for C/C++ & Java (and for some other languages). To initial customization you can take my config that i use to work with C++ projects
Right now, I'm using Auto Complete for Emacs. As a current Visual Studio and ex-Eclipse user, I can say that it rivals both applications quite well. It's still not as good as Microsoft's IntelliSense for C#, but some would say that C++ is notoriously difficult to parse.
It leverages the power of (I believe) the Semantic package from Cedet, and I find it feels nicer to use when compared to Smart Complete. It completes C++ members, local variables, etc. It's pretty good. However, it falls down on not being able to complete overloaded methods (it only shows the function once with no parameters, but thats a limitation of Cedet I believe), and other various things. It may improve in future though!
By the way, I could be wrong here, but I think you need an EDE project set up for the class member completion to work (just like you would normally with Semantic). I've only ever used it while having an EDE project, so I assume this is true.
Searching the web I find http://www.emacswiki.org/cgi-bin/wiki/EmacsTags#toc7 describing complete-tab in etags. It is bound to M-Tab by default. This binding may be a problem for you
Also, etags has some limits, which may annoy you...
The link also points to CEDET as having better symbol completion support.
Have you tried the emacs plugin for eclipse?
http://people.csail.mit.edu/adonovan/hacks/eclipse-emacs.html
I've written a C++-specific package on top of CEDET that might provide
what you want. It provides an Eclipse-like function arguments hint.
Overloaded functions are supported both for function arguments hint
and for completion.
Package is located here:
https://github.com/abo-abo/function-args
Make sure to check out the nice screenshot:
https://raw.github.com/abo-abo/function-args/master/doc/screenshot-1.png
auto-complete-clang is what you want. Can't go wrong with using an actual C++ compiler for completions.
The only problem it has is there's no way to know what -I and -D flags to pass to the compiler. There are packages for emacs that let you declare projects and then you can use that.
Personally, I use CMake for all C and C++ work so I wrote some CMake code to pass that information to emacs through directory-local variables. It works, but I'm thinking of writing a package that calls cmake from emacs so there's less intrusion.

Writing Emacs extensions in languages other than Lisp

I'd like to take an existing application (written in OCaml) and create an Emacs "interface" for it (like, for example, the Emacs GDB mode). I would prefer to do this without writing a ton of Lisp code. In MVC terms, I'd like for the View to be Emacs, but for the Model and Controller to remain (primarily) OCaml.
Does anybody know of a way to write Emacs extensions in a language other than Lisp? This could either take the form of bindings to the Emacs extension API in some other language (e.g., making OCaml a first-class Emacs extension language) or an Emacs interaction mode where, for example, the extension has a pipe into which it can write Emacs Lisp expressions and read out result values.
http://www.emacswiki.org/cgi-bin/emacs-en?CategoryExtensionLanguage is a list of all non-Elisp extension languages you can use.
It does appear to be dynamic language centric.
http://common-lisp.net/project/slime/ is missing from that list, as it is not quite an extension language, but an Elisp-Common Lisp bridge. Its source code would show how to communicate back and forth over sockets.
A similar IDE for Erlang is Distel, at http://fresh.homeunix.net/~luke/distel/ (currently down) and https://github.com/massemanet/distel.
Good luck!
I don't know if this will work for your particular problem, but I have been doing something similar using the shell-command-to-string function:
(shell-command-to-string
"bash -c \"script-to-exec args\"")
So for example, we have existing scripts written in python which will mangle a file, so the above lets me invoke the script via emacs lisp.
A quick google search found this page describing a system to write extensions in Python, so it seems feasible to do what you want... you will just have to see if anyone has written a similar framework for OCaml.
Some Extension Api is now possible with the incoming emacs 25.1 and dynamic modules
A Library, emacs-ffi offer a foreign function interface based on libffi.
Check out complete documentation on the README.
Try PyMacs, which allows extending Emacs in Python.
edit: updated link.
From the statically typed languages side, there is something that looks quite performant and well featured for Haskell:
https://github.com/knupfer/haskell-emacs
there is also probably something useful for Scala to be reused from the Ensime project (has a bridge for both Emacs and Vim):
https://github.com/ensime/ensime-server
Furthermore, a quick google search revealed another potential candidate for extending Emacs with a classic FP language, OCaml; the project has a lot of .ml source files so there's got to be an Emacs-OCaml bridge somewhere:
https://github.com/the-lambda-church/merlin
There is no "Extension API". Emacs Lisp is way in there, and it ain't moving.
You can run Emacs commands from your other process. Have a look at Gnuserv.
There are plenty of applications where Emacs is the View for a Model/Controller in a separate process. The Emacs GDB interface is a good example. I'm not sure of a simpler example, maybe sql-postgresql?