| 1 |
= EmacsDefUseMode |
| 2 |
|
| 3 |
MLton provides an <<CompileTimeOptions#,option>>, |
| 4 |
``-show-def-use __file__``, to output precise (giving exact source |
| 5 |
locations) and accurate (including all uses and no false data) |
| 6 |
whole-program def-use information to a file. Unlike typical tags |
| 7 |
facilities, the information includes local variables and distinguishes |
| 8 |
between different definitions even when they have the same name. The |
| 9 |
def-use Emacs mode uses the information to provide navigation support, |
| 10 |
which can be particularly useful while reading SML programs compiled |
| 11 |
with MLton (such as the MLton compiler itself). |
| 12 |
|
| 13 |
|
| 14 |
== Screen Capture |
| 15 |
|
| 16 |
Note the highlighting and the type displayed in the minibuffer. |
| 17 |
|
| 18 |
image::EmacsDefUseMode.attachments/def-use-capture.png[align="center"] |
| 19 |
|
| 20 |
|
| 21 |
== Features |
| 22 |
|
| 23 |
* Highlights definitions and uses. Different colors for definitions, unused definitions, and uses. |
| 24 |
* Shows types (with highlighting) of variable definitions in the minibuffer. |
| 25 |
* Navigation: `jump-to-def`, `jump-to-next`, and `jump-to-prev`. These work precisely (no searching involved). |
| 26 |
* Can list, visit and mark all references to a definition (within a program). |
| 27 |
* Automatically reloads updated def-use files. |
| 28 |
* Automatically loads previously used def-use files at startup. |
| 29 |
* Supports both http://www.gnu.org/software/emacs/[Gnu Emacs] and http://www.xemacs.org[XEmacs]. |
| 30 |
|
| 31 |
|
| 32 |
== Download |
| 33 |
|
| 34 |
There is no separate package for the def-use mode although the mode |
| 35 |
has been relatively stable for some time already. To install the mode |
| 36 |
you need to get the Emacs Lisp, `*.el`, files from MLton's repository: |
| 37 |
https://github.com/MLton/mlton/tree/master/ide/emacs[`emacs`]. The easiest way to get the files |
| 38 |
is to use <<Git#>> to access MLton's <<Sources#,sources>>. |
| 39 |
|
| 40 |
///// |
| 41 |
If you only want the Emacs lisp files, you can use the following |
| 42 |
command: |
| 43 |
---- |
| 44 |
svn co svn://mlton.org/mlton/trunk/ide/emacs mlton-emacs-ide |
| 45 |
---- |
| 46 |
///// |
| 47 |
|
| 48 |
== Setup |
| 49 |
|
| 50 |
The easiest way to load def-use mode is to first tell Emacs where to |
| 51 |
find the files. For example, add |
| 52 |
|
| 53 |
[source,cl] |
| 54 |
---- |
| 55 |
(add-to-list 'load-path (file-truename "path-to-the-el-files")) |
| 56 |
---- |
| 57 |
|
| 58 |
to your `~/.emacs` or `~/.xemacs/init.el`. You'll probably |
| 59 |
also want to start `def-use-mode` automatically by adding |
| 60 |
|
| 61 |
[source,cl] |
| 62 |
---- |
| 63 |
(require 'esml-du-mlton) |
| 64 |
(def-use-mode) |
| 65 |
---- |
| 66 |
|
| 67 |
to your Emacs init file. Once the def-use mode is activated, you |
| 68 |
should see the `DU` indicator on the mode line. |
| 69 |
|
| 70 |
== Usage |
| 71 |
|
| 72 |
To use def-use mode one typically first sets up the program's makefile |
| 73 |
or build script so that the def-use information is saved each time the |
| 74 |
program is compiled. In addition to the ``-show-def-use __file__`` |
| 75 |
option, the ``-prefer-abs-paths true`` expert option is required. |
| 76 |
Note that the time it takes to save the information is small (compared |
| 77 |
to type-checking), so it is recommended to simply add the options to |
| 78 |
the MLton invocation that compiles the program. However, it is only |
| 79 |
necessary to type check the program (or library), so one can specify |
| 80 |
the ``-stop tc`` option. For example, suppose you have a program |
| 81 |
defined by an MLB file named `my-prg.mlb`, you can save the def-use |
| 82 |
information to the file `my-prg.du` by invoking MLton as: |
| 83 |
|
| 84 |
---- |
| 85 |
mlton -prefer-abs-paths true -show-def-use my-prg.du -stop tc my-prg.mlb |
| 86 |
---- |
| 87 |
|
| 88 |
Finally, one needs to tell the mode where to find the def-use |
| 89 |
information. This is done with the `esml-du-mlton` command. For |
| 90 |
example, to load the `my-prg.du` file, one would type: |
| 91 |
|
| 92 |
---- |
| 93 |
M-x esml-du-mlton my-prg.du |
| 94 |
---- |
| 95 |
|
| 96 |
After doing all of the above, find an SML file covered by the |
| 97 |
previously saved and loaded def-use information, and place the cursor |
| 98 |
at some variable (definition or use, it doesn't matter). You should |
| 99 |
see the variable being highlighted. (Note that specifications in |
| 100 |
signatures do not define variables.) |
| 101 |
|
| 102 |
You might also want to setup and use the |
| 103 |
<<EmacsBgBuildMode#,Bg-Build mode>> to start builds automatically. |
| 104 |
|
| 105 |
|
| 106 |
== Types |
| 107 |
|
| 108 |
`-show-def-use` output was extended to include types of variable |
| 109 |
definitions in revision https://github.com/MLton/mlton/search?q=SVN+r6333&type=commits[`r6333`]. To get good type names, the |
| 110 |
types must be in scope at the end of the program. If you are using the |
| 111 |
<<MLBasis#,ML Basis>> system, this means that the root MLB-file for your |
| 112 |
application should not wrap the libraries used in the application inside |
| 113 |
`local ... in ... end`, because that would remove them from the scope before |
| 114 |
the end of the program. |