1 ;; erlang.el --- Major modes for editing and running Erlang
3 ;; Copyright (C) 1995-1998,2000 Ericsson Telecom AB
4 ;; Copyright (C) 2004 Free Software Foundation, Inc.
5 ;; Author: Anders Lindgren
7 ;; Keywords: erlang, languages, processes
10 ;; The contents of this file are subject to the Erlang Public License,
11 ;; Version 1.1, (the "License"); you may not use this file except in
12 ;; compliance with the License. You should have received a copy of the
13 ;; Erlang Public License along with this software. If not, it can be
14 ;; retrieved via the world wide web at http://www.erlang.org/.
16 ;; Software distributed under the License is distributed on an "AS IS"
17 ;; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
18 ;; the License for the specific language governing rights and limitations
21 ;; The Initial Developer of the Original Code is Ericsson Utvecklings AB.
22 ;; All Rights Reserved.
24 ;; Lars Thorsén's modifications of 2000-06-07 included.
26 ;; The original version of this package was written by Robert Virding.
28 ;; Most skeletons have been written at Ericsson Telecom by
29 ;; magnus@erix.ericsson.se and janne@erix.ericsson.se
31 ;; The Erlware version of the mode has updated skeletons, syntax
32 ;; highlighting fixes, and indentation fixes. The latest Erlware mode
33 ;; can be obtained here:
35 ;; http://code.google.com/p/erlware-mode/downloads/list
42 ;; This package provides support for the programming language Erlang.
43 ;; The package provides an editing mode with lots of bells and
44 ;; whistles, compilation support, and it makes it possible for the
45 ;; user to start Erlang shells that run inside Emacs.
47 ;; See the Erlang distribution for full documentation of this package.
52 ;; Place this file in Emacs load path, byte-compile it, and add the
53 ;; following line to the appropriate init file:
55 ;; (require 'erlang-start)
57 ;; The full documentation contains much more extensive description of
58 ;; the installation procedure.
63 ;; Please send bug reports to the following email address:
64 ;; erlang-bugs@erlang.org
65 ;; or if you have a patch suggestion to:
66 ;; erlang-patches@erlang.org
67 ;; Please state as exactly as possible:
68 ;; - Version number of Erlang Mode (see the menu), Emacs, Erlang,
69 ;; and of any other relevant software.
70 ;; - What the expected result was.
71 ;; - What you did, preferably in a repeatable step-by-step form.
72 ;; - A description of the unexpected result.
73 ;; - Relevant pieces of Erlang code causing the problem.
74 ;; - Personal Emacs customisations, if any.
76 ;; Should the Emacs generate an error, please set the Emacs variable
77 ;; `debug-on-error' to `t'. Repeat the error and enclose the debug
78 ;; information in your bug-report.
80 ;; To set the variable you can use the following command:
81 ;; M-x set-variable RET debug-on-error RET t RET
86 (defconst erlang-version
"0.1.12"
87 "The version number of Erlware Erlang mode.")
89 (defvar erlang-man-root-dir nil
90 "The directory where the Erlang manual pages are installed.
91 The name should not contain the trailing slash.
93 Should this variable be nil, no manual pages will show up in the
96 (defvar erlang-menu-items
'(erlang-menu-base-items
97 erlang-menu-skel-items
98 erlang-menu-shell-items
99 erlang-menu-compile-items
100 erlang-menu-man-items
101 erlang-menu-personal-items
102 erlang-menu-version-items
)
103 "*List of menu item list to combine to create Erlang mode menu.
105 External programs which temporarily add menu items to the Erlang mode
106 menu may use this variable. Please use the function `add-hook' to add
109 Please call the function `erlang-menu-init' after every change to this
112 (defvar erlang-menu-base-items
114 (("Indent Line" erlang-indent-command
)
115 ("Indent Region " erlang-indent-region
116 (if erlang-xemacs-p
(mark) mark-active
))
117 ("Indent Clause" erlang-indent-clause
)
118 ("Indent Function" erlang-indent-function
)
119 ("Indent Buffer" erlang-indent-current-buffer
)))
121 (("Fill Comment" erlang-fill-paragraph
)
122 ("Comment Region" comment-region
123 (if erlang-xemacs-p
(mark) mark-active
))
124 ("Uncomment Region" erlang-uncomment-region
125 (if erlang-xemacs-p
(mark) mark-active
))
127 ("Beginning of Function" erlang-beginning-of-function
)
128 ("End of Function" erlang-end-of-function
)
129 ("Mark Function" erlang-mark-function
)
131 ("Beginning of Clause" erlang-beginning-of-clause
)
132 ("End of Clause" erlang-end-of-clause
)
133 ("Mark Clause" erlang-mark-clause
)
135 ("New Clause" erlang-generate-new-clause
)
136 ("Clone Arguments" erlang-clone-arguments
)
138 ("Align Arrows" erlang-align-arrows
)))
139 ("Syntax Highlighting"
140 (("Level 3" erlang-font-lock-level-3
)
141 ("Level 2" erlang-font-lock-level-2
)
142 ("Level 1" erlang-font-lock-level-1
)
143 ("Off" erlang-font-lock-level-0
)))
145 (("Find Tag" find-tag
)
146 ("Find Next Tag" erlang-find-next-tag
)
147 ("Complete Word" erlang-complete-tag
)
148 ("Tags Apropos" tags-apropos
)
149 ("Search Files" tags-search
))))
150 "Description of menu used in Erlang mode.
152 This variable must be a list. The elements are either nil representing
153 a horizontal line or a list with two or three elements. The first is
154 the name of the menu item, the second is the function to call, or a
155 submenu, on the same same form as ITEMS. The third optional argument
156 is an expression which is evaluated every time the menu is displayed.
157 Should the expression evaluate to nil the menu item is ghosted.
160 '((\"Func1\" function-one)
162 ((\"Yellow\" function-yellow)
163 (\"Blue\" function-blue)))
165 (\"Region Function\" spook-function midnight-variable))
167 Call the function `erlang-menu-init' after modifying this variable.")
169 (defvar erlang-menu-shell-items
172 (("Start New Shell" erlang-shell
)
173 ("Display Shell" erlang-shell-display
))))
174 "Description of the Shell menu used by Erlang mode.
176 Please see the documentation of `erlang-menu-base-items'.")
178 (defvar erlang-menu-compile-items
180 (("Compile Buffer" erlang-compile
)
181 ("Display Result" erlang-compile-display
)
182 ("Next Error" erlang-next-error
))))
183 "Description of the Compile menu used by Erlang mode.
185 Please see the documentation of `erlang-menu-base-items'.")
187 (defvar erlang-menu-version-items
189 ("Version" erlang-version
))
190 "Description of the version menu used in Erlang mode.")
192 (defvar erlang-menu-personal-items nil
193 "Description of personal menu items used in Erlang mode.
195 Please see the variable `erlang-menu-base-items' for a description
198 (defvar erlang-menu-man-items nil
199 "The menu containing man pages.
201 The format of the menu should be compatible with `erlang-menu-base-items'.
202 This variable is added to the list of Erlang menus stored in
203 `erlang-menu-items'.")
205 (defvar erlang-menu-skel-items
'()
206 "Description of the menu containing the skeleton entries.
207 The menu is in the form described by the variable `erlang-menu-base-items'.")
209 (defvar erlang-mode-hook nil
210 "*Functions to run when Erlang mode is activated.
212 This hook is used to change the behaviour of Erlang mode. It is
213 normally used by the user to personalise the programming environment.
214 When used in a site init file, it could be used to customise Erlang
215 mode for all users on the system.
217 The functions added to this hook are run every time Erlang mode is
218 started. See also `erlang-load-hook', a hook which is run once,
219 when Erlang mode is loaded into Emacs, and `erlang-shell-mode-hook'
220 which is run every time a new inferior Erlang shell is started.
222 To use a hook, create an Emacs lisp function to perform your actions
223 and add the function to the hook by calling `add-hook'.
225 The following example binds the key sequence C-c C-c to the command
226 `erlang-compile' (normally bound to C-c C-k). The example also
227 activates Font Lock mode to fontify the buffer and adds a menu
228 containing all functions defined in the current buffer.
230 To use the example, copy the following lines to your `~/.emacs' file:
232 (add-hook 'erlang-mode-hook 'my-erlang-mode-hook)
234 (defun my-erlang-mode-hook ()
235 (local-set-key \"\\C-c\\C-c\" 'erlang-compile)
238 (setq font-lock-maximum-decoration t)
240 (if (and window-system (fboundp 'imenu-add-to-menubar))
241 (imenu-add-to-menubar \"Imenu\")))")
243 (defvar erlang-load-hook nil
244 "*Functions to run when Erlang mode is loaded.
246 This hook is used to change the behaviour of Erlang mode. It is
247 normally used by the user to personalise the programming environment.
248 When used in a site init file, it could be used to customize Erlang
249 mode for all users on the system.
251 The difference between this hook and `erlang-mode-hook' and
252 `erlang-shell-mode-hook' is that the functions in this hook
253 is only called once, when the Erlang mode is loaded into Emacs
256 Natural actions for the functions added to this hook are actions which
257 only should be performed once, and actions which should be performed
258 before starting Erlang mode. For example, a number of variables are
259 used by Erlang mode before `erlang-mode-hook' is run.
261 The following example sets the variable `erlang-man-root-dir' so that
262 the manual pages can be retrieved (note that you must set the value of
263 `erlang-man-root-dir' to match the location of the Erlang man pages
266 (setq erlang-man-root-dir \"/usr/local/erlang\")")
268 (defvar erlang-new-file-hook nil
269 "Functions to run when a new Erlang source file is being edited.
271 A useful function is `tempo-template-erlang-normal-header'.
272 \(This function only exists when the `tempo' package is available.)")
274 (defvar erlang-check-module-name
'ask
275 "*Non-nil means check that module name and file name agrees when saving.
277 If the value of this variable is the atom `ask', the user is
278 prompted. If the value is t the source is silently changed.")
280 (defvar erlang-electric-commands
281 '(erlang-electric-comma
282 erlang-electric-semicolon
283 erlang-electric-newline
285 "*List of activated electric commands.
287 The list should contain the electric commands which should be active.
288 Currently, the available electric commands are:
289 `erlang-electric-comma'
290 `erlang-electric-semicolon'
292 `erlang-electric-newline'
294 Should the variable be bound to t, all electric commands
297 To deactivate all electric commands, set this variable to nil.")
299 (defvar erlang-electric-newline-inhibit t
300 "*Set to non-nil to inhibit newline after electric command.
302 This is useful since a lot of people press return after executing an
305 In order to work, the command must also be in the
306 list `erlang-electric-newline-inhibit-list'.
308 Note that commands in this list are required to set the variable
309 `erlang-electric-newline-inhibit' to nil when the newline shouldn't be
312 (defvar erlang-electric-newline-inhibit-list
313 '(erlang-electric-semicolon
314 erlang-electric-comma
316 "*Commands which can inhibit the next newline.")
318 (defvar erlang-electric-semicolon-insert-blank-lines nil
319 "*Number of blank lines inserted before header, or nil.
321 This variable controls the behaviour of `erlang-electric-semicolon'
322 when a new function header is generated. When nil, no blank line is
323 inserted between the current line and the new header. When bound to a
324 number it represents the number of blank lines which should be
327 (defvar erlang-electric-semicolon-criteria
328 '(erlang-next-lines-empty-p
329 erlang-at-keyword-end-p
330 erlang-at-end-of-function-p
)
331 "*List of functions controlling `erlang-electric-semicolon'.
332 The functions in this list are called, in order, whenever a semicolon
333 is typed. Each function in the list is called with no arguments,
334 and should return one of the following values:
336 nil -- no determination made, continue checking
337 'stop -- do not create prototype for next line
338 (anything else) -- insert prototype, and stop checking
340 If every function in the list is called with no determination made,
341 then no prototype is inserted.
343 The test is performed by the function `erlang-test-criteria-list'.")
345 (defvar erlang-electric-comma-criteria
346 '(erlang-stop-when-inside-argument-list
347 erlang-stop-when-at-guard
348 erlang-next-lines-empty-p
349 erlang-at-keyword-end-p
350 erlang-at-end-of-clause-p
351 erlang-at-end-of-function-p
)
352 "*List of functions controlling `erlang-electric-comma'.
353 The functions in this list are called, in order, whenever a comma
354 is typed. Each function in the list is called with no arguments,
355 and should return one of the following values:
357 nil -- no determination made, continue checking
358 'stop -- do not create prototype for next line
359 (anything else) -- insert prototype, and stop checking
361 If every function in the list is called with no determination made,
362 then no prototype is inserted.
364 The test is performed by the function `erlang-test-criteria-list'.")
366 (defvar erlang-electric-arrow-criteria
367 '(erlang-next-lines-empty-p
368 erlang-at-end-of-function-p
)
369 "*List of functions controlling the arrow aspect of `erlang-electric-gt'.
370 The functions in this list are called, in order, whenever a `>'
371 is typed. Each function in the list is called with no arguments,
372 and should return one of the following values:
374 nil -- no determination made, continue checking
375 'stop -- do not create prototype for next line
376 (anything else) -- insert prototype, and stop checking
378 If every function in the list is called with no determination made,
379 then no prototype is inserted.
381 The test is performed by the function `erlang-test-criteria-list'.")
383 (defvar erlang-electric-newline-criteria
385 "*List of functions controlling `erlang-electric-newline'.
387 The electric newline commands indents the next line. Should the
388 current line begin with a comment the comment start is copied to
389 the newly created line.
391 The functions in this list are called, in order, whenever a comma
392 is typed. Each function in the list is called with no arguments,
393 and should return one of the following values:
395 nil -- no determination made, continue checking
396 'stop -- do not create prototype for next line
397 (anything else) -- trigger the electric command.
399 If every function in the list is called with no determination made,
400 then no prototype is inserted. Should the atom t be a member of the
401 list, it is treated as a function triggering the electric command.
403 The test is performed by the function `erlang-test-criteria-list'.")
405 (defvar erlang-next-lines-empty-threshold
2
406 "*Number of blank lines required to activate an electric command.
408 Actually, this value controls the behaviour of the function
409 `erlang-next-lines-empty-p' which normally is a member of the
410 criteria lists controlling the electric commands. (Please see
411 the variables `erlang-electric-semicolon-criteria' and
412 `erlang-electric-comma-criteria'.)
414 The variable is bound to a threshold value, a number, representing the
415 number of lines which must be empty.
417 Setting this variable to zero, electric commands will always be
418 triggered by `erlang-next-lines-empty-p', unless inhibited by other
421 Should this variable be nil, `erlang-next-lines-empty-p' will never
422 trigger an electric command. The same effect would be reached if the
423 function `erlang-next-lines-empty-p' would be removed from the criteria
426 Note that even if `erlang-next-lines-empty-p' should not trigger an
427 electric command, other functions in the criteria list could.")
429 (defvar erlang-new-clause-with-arguments nil
430 "*Non-nil means that the arguments are cloned when a clause is generated.
432 A new function header can be generated by calls to the function
433 `erlang-generate-new-clause' and by use of the electric semicolon.")
435 (defvar erlang-compile-use-outdir t
436 "*When nil, go to the directory containing source file when compiling.
438 This is a workaround for a bug in the `outdir' option of compile. If the
439 outdir is not in the current load path, Erlang doesn't load the object
440 module after it has been compiled.
442 To activate the workaround, place the following in your `~/.emacs' file:
443 (setq erlang-compile-use-outdir nil)")
445 (defvar erlang-indent-level
4
446 "*Indentation of Erlang calls/clauses within blocks.")
448 (defvar erlang-indent-guard
2
449 "*Indentation of Erlang guards.")
451 (defvar erlang-argument-indent
2
452 "*Indentation of the first argument in a function call.
453 When nil, indent to the column after the `(' of the
456 (defvar erlang-tab-always-indent t
457 "*Non-nil means TAB in Erlang mode should always re-indent the current line,
458 regardless of where in the line point is when the TAB command is used.")
460 (defvar erlang-error-regexp-alist
461 '(("^\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" .
(1 2)))
462 "*Patterns for matching Erlang errors.")
464 (defvar erlang-man-inhibit
(eq system-type
'windows-nt
)
465 "Inhibit the creation of the Erlang Manual Pages menu.
467 The Windows distribution of Erlang does not include man pages, hence
468 there is no attempt to create the menu.")
470 (defvar erlang-man-dirs
471 '(("Man - Commands" "/man/man1" t
)
472 ("Man - Modules" "/man/man3" t
)
473 ("Man - Files" "/man/man4" t
)
474 ("Man - Applications" "/man/man6" t
))
475 "*The man directories displayed in the Erlang menu.
477 Each item in the list should be a list with three elements, the first
478 the name of the menu, the second the directory, and the last a flag.
479 Should the flag the nil, the directory is absolute, should it be non-nil
480 the directory is relative to the variable `erlang-man-root-dir'.")
482 (defvar erlang-man-max-menu-size
20
483 "*The maximum number of menu items in one menu allowed.")
485 (defvar erlang-man-display-function
'erlang-man-display
486 "*Function used to display man page.
488 The function is called with one argument, the name of the file
489 containing the man page. Use this variable when the default
490 function, `erlang-man-display', does not work on your system.")
493 (defconst erlang-atom-quoted-regexp
494 "'\\(?:[^\\']?\\(?:\\\\'\\)?\\)*'"
495 "Regexp describing a single-quoted atom"))
498 (defconst erlang-atom-regular-regexp
499 "\\(?:[a-z][A-Za-z0-9_]*\\)"
500 "Regexp describing a regular (non-quoted) atom"))
503 (defconst erlang-atom-regexp
504 (concat"\\(" erlang-atom-quoted-regexp
"\\|"
505 erlang-atom-regular-regexp
"\\)")
506 "Regexp describing an Erlang atom."))
508 (defconst erlang-atom-regexp-matches
1
509 "Number of regexp parenthesis pairs in `erlang-atom-regexp'.
511 This is used to determine parenthesis matches in complex regexps which
512 contains `erlang-atom-regexp'.")
514 (defconst erlang-variable-regexp
"\\([A-Z_][a-zA-Z0-9_]*\\)"
515 "Regexp which should match an Erlang variable.
517 The regexp must be surrounded with a pair of regexp parentheses.")
518 (defconst erlang-variable-regexp-matches
1
519 "Number of regexp parenthesis pairs in `erlang-variable-regexp'.
521 This is used to determine matches in complex regexps which contains
522 `erlang-variable-regexp'.")
524 (defconst erlang-bif-regexp
527 "a\\(bs\\|live\\|pply\\|tom_to_list\\)\\|"
528 "binary_to_\\(list\\|term\\)\\|"
530 "d\\(ate\\|isconnect_node\\)\\|"
531 "e\\(lement\\|rase\\|xit\\)\\|"
532 "floa\\(t\\|t_to_list\\)\\|"
533 "g\\(arbage_collect\\|et\\(\\|_keys\\)\\|roup_leader\\)\\|"
535 "i\\(nte\\(ger_to_list\\|rnal_bif\\)\\|s_alive\\)\\|"
536 "l\\(ength\\|i\\(nk\\|st_to_\\(atom\\|binary\\|float\\|integer"
537 "\\|pid\\|tuple\\)\\)\\)\\|"
538 "make_ref\\|no\\(de\\(\\|_\\(link\\|unlink\\)\\|s\\)\\|talive\\)\\|"
540 "p\\(id_to_list\\|rocess\\(_\\(flag\\|info\\)\\|es\\)\\|ut\\)\\|"
541 "r\\(egister\\(\\|ed\\)\\|ound\\)\\|"
542 "s\\(e\\(lf\\|telement\\)\\|ize"
543 "\\|p\\(awn\\(\\|_link\\)\\|lit_binary\\)\\|tatistics\\)\\|"
544 "t\\(erm_to_binary\\|hrow\\|ime\\|l\\|r\\(ace\\|unc\\)\\|uple_to_list\\)\\|"
549 "Regexp matching an Erlang built-in function (BIF).")
551 (defvar erlang-defun-prompt-regexp
(concat "^" erlang-atom-regexp
"\\s *(")
552 "Regexp which should match beginning of a clause.")
554 (defvar erlang-file-name-extension-regexp
"\\.[eh]rl$"
555 "*Regexp which should match an Erlang file name.
557 This regexp is used when an Erlang module name is extracted from the
558 name of an Erlang source file.
560 The regexp should only match the section of the file name which should
561 be excluded from the module name.
563 To match all files set this variable to \"\\\\(\\\\..*\\\\|\\\\)$\".
564 The matches all except the extension. This is useful if the Erlang
565 tags system should interpret tags on the form `module:tag' for
566 files written in other languages than Erlang.")
568 (defvar erlang-mode-map nil
569 "*Keymap used in Erlang mode.")
570 (defvar erlang-mode-abbrev-table nil
571 "Abbrev table in use in Erlang-mode buffers.")
572 (defvar erlang-mode-syntax-table nil
573 "Syntax table in use in Erlang-mode buffers.")
575 (defconst erlang-emacs-major-version
576 (if (boundp 'emacs-major-version
)
578 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version
)
579 (string-to-int (substring emacs-version
580 (match-beginning 1) (match-end 1))))
581 "Major version number of Emacs.")
583 (defconst erlang-emacs-minor-version
584 (if (boundp 'emacs-minor-version
)
586 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version
)
587 (string-to-int (substring emacs-version
588 (match-beginning 2) (match-end 2))))
589 "Minor version number of Emacs.")
591 (defconst erlang-xemacs-p
(string-match "Lucid\\|XEmacs" emacs-version
)
592 "Non-nil when running under XEmacs or Lucid Emacs.")
594 (defvar erlang-xemacs-popup-menu
'("Erlang Mode Commands" . nil
)
595 "Common popup menu for all buffers in Erlang mode.
597 This variable is destructively modified every time the Erlang menu
598 is modified. The effect is that all changes take effect in all
599 buffers in Erlang mode, just like under GNU Emacs.
601 Never EVER set this variable!")
603 (defconst inferior-erlang-use-cmm
(boundp 'minor-mode-overriding-map-alist
)
604 "Non-nil means use `compilation-minor-mode' in Erlang shell.")
606 ;; Tempo skeleton templates:
607 (load "erlang-skels")
610 (load "erlang-sinan")
613 ;; Font-lock variables
615 (defvar erlang-font-lock-modern-p
616 (cond ((>= erlang-emacs-major-version
20) t
)
617 (erlang-xemacs-p (>= erlang-emacs-minor-version
14))
618 ((= erlang-emacs-major-version
19) (>= erlang-emacs-minor-version
29))
620 "Non-nil when this version of Emacs uses a modern version of Font Lock.
622 This is determined by checking the version of Emacs used, the actual
623 font-lock code is not loaded.")
626 ;; The next few variables define different Erlang font-lock patterns.
627 ;; They could be appended to form a custom font-lock appearance.
629 ;; The function `erlang-font-lock-set-face' could be used to change
630 ;; the face of a pattern.
632 ;; Note that Erlang strings and atoms are highlighted with using
633 ;; syntactic analysis.
635 (defvar erlang-font-lock-keywords-func
637 (list (concat "^" erlang-atom-regexp
"\\s-*(")
638 1 'font-lock-function-name-face t
))
639 "Font lock keyword highlighting a function header.")
641 (defvar erlang-font-lock-keywords-int-func-call
643 (list (concat "\\<" erlang-atom-regexp
"\\s-*(")
644 1 'font-lock-type-face
))
645 "Font lock keyword highlighting an internal function call.")
647 (defvar erlang-font-lock-keywords-ext-func-call
649 (list (concat "\\<" erlang-atom-regexp
"\\s-*:\\s-*"
650 erlang-atom-regexp
"\\s-*(")
651 1 'font-lock-type-face
)
652 (list (concat "\\<" erlang-atom-regexp
"\\s-*:\\s-*"
653 erlang-atom-regexp
"\\s-*(")
654 2 'font-lock-type-face
))
655 "Font lock keyword highlighting an external function call.")
657 (defvar erlang-font-lock-keywords-ext-bif
659 (list (concat "\\<\\(erlang\\)\\s-*:\\s-*\\<" erlang-bif-regexp
"\\s-*(")
660 1 'font-lock-builtin-face
)
661 (list (concat "\\<\\(erlang\\)\\s-*:\\s-*\\<" erlang-bif-regexp
"\\s-*(")
662 2 'font-lock-builtin-face
))
663 "Font lock keyword highlighting built in functions.")
665 (defvar erlang-font-lock-keywords-int-bif
667 (list (concat "\\<" erlang-bif-regexp
"\\s-*(")
668 1 'font-lock-builtin-face
))
669 "Font lock keyword highlighting built in functions.")
671 (defvar erlang-font-lock-keywords-fn
673 (list (concat "\\(" erlang-atom-regexp
"/[0-9]+\\)")
674 1 'font-lock-function-name-face
))
675 "Font lock keyword highlighting a F/N fun descriptor.")
677 (defvar erlang-font-lock-keywords-plusplus
679 (list (concat "\\(\\+\\+\\)")
680 1 'font-lock-warning-face
))
681 "Font lock keyword highlighting the `++' operator.")
683 (defvar erlang-font-lock-keywords-dollar
685 (list "\\(\\$\\([^\\]\\|\\\\\\([^0-7^\n]\\|[0-7]+\\|\\^[a-zA-Z]\\)\\)\\)"
686 1 'font-lock-string-face
))
687 "Font lock keyword highlighting numbers in ASCII form (e.g. $A).")
689 (defvar erlang-font-lock-keywords-lc
691 (list "\\(<-\\)" 1 'font-lock-keyword-face
)
692 (list "\\(||\\)" 1 'font-lock-keyword-face
))
693 "Font lock keyword highlighting list comprehension operators.")
695 (defvar erlang-font-lock-keywords-keywords
697 (list (concat "\\<\\(a\\(fter\\|ndalso\\)\\|begin\\|c\\(atch\\|ase\\)"
698 "\\|end\\|fun\\|if\\|o\\(f\\|relse\\)\\|receive\\|try\\|when"
699 "\\|query\\)\\([^a-zA-Z0-9_]\\|$\\)")
700 1 'font-lock-keyword-face
))
701 "Font lock keyword highlighting Erlang keywords.")
703 (defvar erlang-font-lock-keywords-attr
705 (list (concat "^\\(-" erlang-atom-regexp
"\\)\\(\\s-\\|\\.\\|(\\)")
706 1 'font-lock-preprocessor-face
))
707 "Font lock keyword highlighting attributes.")
709 (defvar erlang-font-lock-keywords-quotes
711 (list "`\\([-+a-zA-Z0-9_:*][-+a-zA-Z0-9_:*]+\\)'"
712 1 'font-lock-keyword-face t
))
713 "Font lock keyword highlighting words in single quotes in comments.
715 This is not the highlighting of Erlang strings and atoms, which
716 are highlighted by syntactic analysis.")
718 ;; Note: The deprecated guard `float' collides with the bif `float'.
719 (defvar erlang-font-lock-keywords-guards
723 "\\(is_\\)?\\(atom\\|boolean\\|function\\|binary\\|constant"
724 "\\|integer\\|list\\|number\\|p\\(id\\|ort\\)\\|"
725 "re\\(ference\\|cord\\)\\|tuple"
728 (if erlang-font-lock-modern-p
729 'font-lock-builtin-face
730 'font-lock-keyword-face
)))
731 "Font lock keyword highlighting guards.")
733 (defvar erlang-font-lock-keywords-macros
735 (list (concat "?\\s *\\(" erlang-atom-regexp
736 "\\|" erlang-variable-regexp
"\\)\\>")
737 1 'font-lock-preprocessor-face
)
738 (list (concat "^-\\(define\\|ifn?def\\)\\s *(\\s *\\(" erlang-atom-regexp
739 "\\|" erlang-variable-regexp
"\\)\\>")
740 2 'font-lock-preprocessor-face
))
741 "Font lock keyword highlighting macros.
742 This must be placed in front of `erlang-font-lock-keywords-vars'.")
744 (defvar erlang-font-lock-keywords-records
746 (list (concat "#\\s *" erlang-atom-regexp
"\\>")
747 1 'font-lock-preprocessor-face
)
748 ;; Don't highlight numerical constants.
749 (list "\\<[0-9][0-9]?#\\([0-9a-fA_F]+\\)\\>"
751 (list (concat "^-record(\\s *" erlang-atom-regexp
"\\>")
752 1 'font-lock-preprocessor-face
))
753 "Font lock keyword highlighting Erlang records.
754 This must be placed in front of `erlang-font-lock-keywords-vars'.")
756 (defvar erlang-font-lock-keywords-vars
758 (list (concat "\\<" erlang-variable-regexp
"\\>")
759 1 (if erlang-font-lock-modern-p
760 'font-lock-variable-name-face
761 'font-lock-type-face
)))
762 "Font lock keyword highlighting Erlang variables.
763 Must be preceded by `erlang-font-lock-keywords-macros' and `-records'
766 (defvar erlang-font-lock-keywords-atom-regular
768 (list (concat "\\<\\(" erlang-atom-regular-regexp
"\\)\\>")
769 1 'font-lock-constant-face
))
770 "Font lock keyword highlighting Erlang atoms.")
772 (defvar erlang-font-lock-keywords-atom-quoted
774 (list (concat "\\<\\(" erlang-atom-quoted-regexp
"\\)\\>")
775 1 'font-lock-constant-face
))
776 "Font lock keyword highlighting Erlang atoms.")
778 (defvar erlang-font-lock-keywords-1
779 (append erlang-font-lock-keywords-func
780 erlang-font-lock-keywords-keywords
)
781 ;; DocStringOrig: erlang-font-lock-keywords
782 "Font-lock keywords used by Erlang Mode.
784 There exists three levels of Font Lock keywords for Erlang:
785 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
786 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
787 `erlang-font-lock-keywords-3' - Variables, macros and records.
789 To use a specific level, please set the variable
790 `font-lock-maximum-decoration' to the appropriate level. Note that the
791 variable must be set before Erlang mode is activated.
794 (setq font-lock-maximum-decoration 2)")
796 (defvar erlang-font-lock-keywords-2
797 (append erlang-font-lock-keywords-1
798 erlang-font-lock-keywords-attr
799 erlang-font-lock-keywords-quotes
800 erlang-font-lock-keywords-guards
)
801 ;; DocStringCopy: erlang-font-lock-keywords
802 "Font-lock keywords used by Erlang Mode.
804 There exists three levels of Font Lock keywords for Erlang:
805 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
806 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
807 `erlang-font-lock-keywords-3' - Variables, macros and records.
809 To use a specific level, please set the variable
810 `font-lock-maximum-decoration' to the appropriate level. Note that the
811 variable must be set before Erlang mode is activated.
814 (setq font-lock-maximum-decoration 2)")
817 (defvar erlang-font-lock-keywords-3
818 (append erlang-font-lock-keywords-2
819 erlang-font-lock-keywords-macros
820 erlang-font-lock-keywords-records
821 erlang-font-lock-keywords-ext-bif
822 erlang-font-lock-keywords-ext-func-call
823 erlang-font-lock-keywords-int-bif
824 erlang-font-lock-keywords-int-func-call
825 erlang-font-lock-keywords-fn
826 erlang-font-lock-keywords-plusplus
827 erlang-font-lock-keywords-lc
828 erlang-font-lock-keywords-atom-quoted
829 erlang-font-lock-keywords-dollar
830 erlang-font-lock-keywords-atom-regular
831 erlang-font-lock-keywords-vars
833 ;; DocStringCopy: erlang-font-lock-keywords
834 "Font-lock keywords used by Erlang Mode.
836 There exists three levels of Font Lock keywords for Erlang:
837 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
838 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
839 `erlang-font-lock-keywords-3' - Variables, macros and records.
841 To use a specific level, please set the variable
842 `font-lock-maximum-decoration' to the appropriate level. Note that the
843 variable must be set before Erlang mode is activated.
846 (setq font-lock-maximum-decoration 2)")
849 (defvar erlang-font-lock-keywords erlang-font-lock-keywords-3
850 ;; DocStringCopy: erlang-font-lock-keywords
851 "Font-lock keywords used by Erlang Mode.
853 There exists three levels of Font Lock keywords for Erlang:
854 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
855 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
856 `erlang-font-lock-keywords-3' - Variables, macros and records.
858 To use a specific level, please set the variable
859 `font-lock-maximum-decoration' to the appropriate level. Note that the
860 variable must be set before Erlang mode is activated.
863 (setq font-lock-maximum-decoration 2)")
866 (defvar erlang-font-lock-syntax-table nil
867 "Syntax table used by Font Lock mode.
869 The difference between this and the standard Erlang Mode
870 syntax table is that `_' is treated as part of words by
873 Unfortunately, XEmacs hasn't got support for a special Font
874 Lock syntax table. The effect is that `apply' in the atom
875 `foo_apply' will be highlighted as a bif.")
878 ;;; Avoid errors while compiling this file.
880 ;; `eval-when-compile' is not defined in Emacs 18. We define it as a
882 (or (fboundp 'eval-when-compile
)
883 (defmacro eval-when-compile
(&rest rest
) nil
))
885 ;; These umm...functions are new in Emacs 20. And, yes, until version
886 ;; 19.27 Emacs backquotes were this ugly.
888 (or (fboundp 'unless
)
889 (defmacro unless
(condition &rest body
)
890 "(unless CONDITION BODY...): If CONDITION is false, do BODY, else return nil."
896 (defmacro when
(condition &rest body
)
897 "(when CONDITION BODY...): If CONDITION is true, do BODY, else return nil."
902 (or (fboundp 'char-before
)
903 (defmacro char-before
(&optional pos
)
904 "Return the character in the current buffer just before POS."
905 (` (char-after (1- (or (, pos
) (point)))))))
908 (if (or (featurep 'bytecomp
)
909 (featurep 'byte-compile
))
911 (cond ((string-match "Lucid\\|XEmacs" emacs-version
)
912 (put 'comment-indent-hook
'byte-obsolete-variable nil
)
913 ;; Do not warn for unused variables
914 ;; when compiling under XEmacs.
915 (setq byte-compile-warnings
916 '(free-vars unresolved callargs redefine
))))
918 (require 'compile
))))
921 (defun erlang-version ()
922 "Return the current version of Erlang mode."
925 (message "Erlware Erlang mode version %s" erlang-version
))
930 (defun erlang-mode ()
931 "Major mode for editing Erlang source files in Emacs.
932 It knows about syntax and comment, it can indent code, it is capable
933 of fontifying the source file, the TAGS commands are aware of Erlang
934 modules, and the Erlang man pages can be accessed.
936 Should this module, \"erlang.el\", be installed properly, Erlang mode
937 is activated whenever an Erlang source or header file is loaded into
938 Emacs. To indicate this, the mode line should contain the word
941 The main feature of Erlang mode is indentation, press TAB and the
942 current line will be indented correctly.
944 Comments starting with one `%' are indented with the same indentation
945 as code. Comments starting with at least two `%':s are indented to
948 However, Erlang mode contains much more, this is a list of the most
950 TAB - Indent the line.
951 C-c C-q - Indent current function.
952 M-; - Create a comment at the end of the line.
953 M-q - Fill a comment, i.e. wrap lines so that they (hopefully)
955 M-a - Goto the beginning of an Erlang clause.
956 M-C-a - Ditto for function.
957 M-e - Goto the end of an Erlang clause.
958 M-C-e - Ditto for function.
959 M-h - Mark current Erlang clause.
960 M-C-h - Ditto for function.
961 C-c C-z - Start, or switch to, an inferior Erlang shell.
962 C-c C-k - Compile current file.
965 ; - Electric semicolon.
967 Erlang mode check the name of the file against the module name when
968 saving, whenever a mismatch occurs Erlang mode offers to modify the
971 The variable `erlang-electric-commands' controls the electric
972 commands. To deactivate all of them, set it to nil.
974 There exists a large number of commands and variables in the Erlang
975 module. Please press `M-x apropos RET erlang RET' to see a complete
976 list. Press `C-h f name-of-function RET' and `C-h v name-of-variable
977 RET'to see the full description of functions and variables,
980 On entry to this mode the contents of the hook `erlang-mode-hook' is
983 Please see the beginning of the file `erlang.el' for more information
984 and examples of hooks.
989 (kill-all-local-variables)
990 (setq major-mode
'erlang-mode
)
991 (setq mode-name
"Erlang")
992 (erlang-syntax-table-init)
994 (erlang-electric-init)
996 (erlang-mode-variables)
997 (erlang-check-module-name-init)
998 (erlang-add-compilation-alist erlang-error-regexp-alist
)
1001 (erlang-font-lock-init)
1003 (run-hooks 'erlang-mode-hook
)
1004 (if (zerop (buffer-size))
1005 (run-hooks 'erlang-new-file-hook
))
1006 ;; Doesn't exist in Emacs v21.4; required by Emacs v23.
1007 (if (boundp 'after-change-major-mode-hook
)
1008 (run-hooks 'after-change-major-mode-hook
)))
1011 (defun erlang-syntax-table-init ()
1012 (if (null erlang-mode-syntax-table
)
1013 (let ((table (make-syntax-table)))
1014 (modify-syntax-entry ?
\n ">" table
)
1015 (modify-syntax-entry ?
\" "\"" table
)
1016 (modify-syntax-entry ?
# "." table
)
1017 (modify-syntax-entry ?$
"\\" table
)
1018 (modify-syntax-entry ?%
"<" table
)
1019 (modify-syntax-entry ?
& "." table
)
1020 (modify-syntax-entry ?
\' "w" table
)
1021 (modify-syntax-entry ?
* "." table
)
1022 (modify-syntax-entry ?
+ "." table
)
1023 (modify-syntax-entry ?-
"." table
)
1024 (modify-syntax-entry ?
/ "." table
)
1025 (modify-syntax-entry ?
: "." table
)
1026 (modify-syntax-entry ?
< "." table
)
1027 (modify-syntax-entry ?
= "." table
)
1028 (modify-syntax-entry ?
> "." table
)
1029 (modify-syntax-entry ?
\\ "\\" table
)
1030 (modify-syntax-entry ?_
"_" table
)
1031 (modify-syntax-entry ?|
"." table
)
1033 (setq erlang-mode-syntax-table table
)))
1035 (set-syntax-table erlang-mode-syntax-table
))
1038 (defun erlang-keymap-init ()
1041 (setq erlang-mode-map
(make-sparse-keymap))
1042 (erlang-mode-commands erlang-mode-map
))
1043 (use-local-map erlang-mode-map
))
1046 (defun erlang-mode-commands (map)
1047 (unless (boundp 'indent-line-function
)
1048 (define-key map
"\t" 'erlang-indent-command
))
1049 (define-key map
";" 'erlang-electric-semicolon
)
1050 (define-key map
"," 'erlang-electric-comma
)
1051 (define-key map
"<" 'erlang-electric-lt
)
1052 (define-key map
">" 'erlang-electric-gt
)
1053 (define-key map
"\C-m" 'erlang-electric-newline
)
1054 (if (not (boundp 'delete-key-deletes-forward
))
1055 (define-key map
"\177" 'backward-delete-char-untabify
)
1056 (define-key map
[backspace] 'backward-delete-char-untabify))
1057 (define-key map "\M-q" 'erlang-fill-paragraph)
1058 (unless (boundp 'beginning-of-defun-function)
1059 (define-key map "\M-\C-a" 'erlang-beginning-of-function)
1060 (define-key map "\M-\C-e" 'erlang-end-of-function)
1061 (define-key map "\M-\C-h" 'erlang-mark-function))
1062 (define-key map "\M-\t" 'erlang-complete-tag)
1063 (define-key map "\C-c\M-\t" 'tempo-complete-tag)
1064 (define-key map "\M-+" 'erlang-find-next-tag)
1065 (define-key map "\C-c\M-a" 'erlang-beginning-of-clause)
1066 (define-key map "\C-c\M-b" 'tempo-backward-mark)
1067 (define-key map "\C-c\M-e" 'erlang-end-of-clause)
1068 (define-key map "\C-c\M-f" 'tempo-forward-mark)
1069 (define-key map "\C-c\M-h" 'erlang-mark-clause)
1070 (define-key map "\C-c\C-c" 'comment-region)
1071 (define-key map "\C-c\C-j" 'erlang-generate-new-clause)
1072 (define-key map "\C-c\C-k" 'erlang-compile)
1073 (define-key map "\C-c\C-l" 'erlang-compile-display)
1074 (define-key map "\C-c\C-s" 'erlang-show-syntactic-information)
1075 (define-key map "\C-c\C-q" 'erlang-indent-function)
1076 (define-key map "\C-c\C-u" 'erlang-uncomment-region)
1077 (define-key map "\C-c\C-y" 'erlang-clone-arguments)
1078 (define-key map "\C-c\C-a" 'erlang-align-arrows)
1079 (define-key map "\C-c\C-z" 'erlang-shell-display)
1080 (unless inferior-erlang-use-cmm
1081 (define-key map "\C-x`" 'erlang-next-error)))
1084 (defun erlang-electric-init ()
1085 ;; Set up electric character functions to work with
1086 ;; delsel/pending-del mode. Also, set up text properties for bit
1088 (mapcar #'(lambda (cmd)
1089 (put cmd 'delete-selection t) ;for delsel (Emacs)
1090 (put cmd 'pending-delete t)) ;for pending-del (XEmacs)
1091 '(erlang-electric-semicolon
1092 erlang-electric-comma
1093 erlang-electric-gt))
1095 (put 'bitsyntax-open-outer 'syntax-table '(4 . ?>))
1096 (put 'bitsyntax-open-outer 'rear-nonsticky '(category))
1097 (put 'bitsyntax-open-inner 'rear-nonsticky '(category))
1098 (put 'bitsyntax-close-inner 'rear-nonsticky '(category))
1099 (put 'bitsyntax-close-outer 'syntax-table '(5 . ?<))
1100 (put 'bitsyntax-close-outer 'rear-nonsticky '(category))
1101 (setq parse-sexp-lookup-properties 't))
1104 (defun erlang-mode-variables ()
1105 (or erlang-mode-abbrev-table
1106 (define-abbrev-table 'erlang-mode-abbrev-table ()))
1107 (setq local-abbrev-table erlang-mode-abbrev-table)
1108 (make-local-variable 'paragraph-start)
1109 (setq paragraph-start (concat "^$\\|" page-delimiter))
1110 (make-local-variable 'paragraph-separate)
1111 (setq paragraph-separate paragraph-start)
1112 (make-local-variable 'paragraph-ignore-fill-prefix)
1113 (setq paragraph-ignore-fill-prefix t)
1114 (make-local-variable 'require-final-newline)
1115 (setq require-final-newline t)
1116 (make-local-variable 'defun-prompt-regexp)
1117 (setq defun-prompt-regexp erlang-defun-prompt-regexp)
1118 (make-local-variable 'comment-start)
1119 (setq comment-start "%")
1120 (make-local-variable 'comment-start-skip)
1121 (setq comment-start-skip "%+\\s *")
1122 (make-local-variable 'indent-line-function)
1123 (setq indent-line-function 'erlang-indent-command)
1124 (make-local-variable 'indent-region-function)
1125 (setq indent-region-function 'erlang-indent-region)
1126 (set (make-local-variable 'comment-indent-function) 'erlang-comment-indent)
1127 (if (<= erlang-emacs-major-version 18)
1128 (set (make-local-variable 'comment-indent-hook) 'erlang-comment-indent))
1129 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1130 (set (make-local-variable 'dabbrev-case-fold-search) nil)
1131 (set (make-local-variable 'imenu-prev-index-position-function)
1132 'erlang-beginning-of-function)
1133 (set (make-local-variable 'imenu-extract-index-name-function)
1134 'erlang-get-function-name)
1135 (set (make-local-variable 'tempo-match-finder)
1136 "[^-a-zA-Z0-9_]\\([-a-zA-Z0-9_]*\\)\\=")
1137 (set (make-local-variable 'beginning-of-defun-function)
1138 'erlang-beginning-of-function)
1139 (set (make-local-variable 'end-of-defun-function) 'erlang-end-of-function)
1140 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil)
1141 (set (make-local-variable 'fill-paragraph-function) 'erlang-fill-paragraph)
1142 (set (make-local-variable 'comment-add) 1)
1143 (set (make-local-variable 'outline-regexp) "[[:lower:]0-9_]+ *(.*) *-> *$")
1144 (set (make-local-variable 'outline-level) (lambda () 1))
1145 (set (make-local-variable 'add-log-current-defun-function)
1146 'erlang-current-defun))
1151 ;; The following code is compatible with the standard package `compilation',
1152 ;; making it possible to go to errors using `erlang-next-error' (or just
1153 ;; `next-error' in Emacs 21).
1155 ;; The normal `compile' command works of course. For best result, please
1156 ;; execute `make' with the `-w' flag.
1158 ;; Please see the variables named `compiling-..' above.
1160 (defun erlang-add-compilation-alist (alist)
1162 (cond ((boundp 'compilation-error-regexp-alist) ; Emacs 19
1164 (or (assoc (car (car alist)) compilation-error-regexp-alist)
1165 (setq compilation-error-regexp-alist
1166 (cons (car alist) compilation-error-regexp-alist)))
1167 (setq alist (cdr alist))))
1168 ((boundp 'compilation-error-regexp)
1169 ;; Emacs 18, Only one regexp is allowed.
1170 (funcall (symbol-function 'set)
1171 'compilation-error-regexp (car (car alist))))))
1173 (defun erlang-font-lock-init ()
1174 "Initialize Font Lock for Erlang mode."
1175 (or erlang-font-lock-syntax-table
1176 (setq erlang-font-lock-syntax-table
1177 (let ((table (copy-syntax-table erlang-mode-syntax-table)))
1178 (modify-syntax-entry ?_ "w" table)
1180 (set (make-local-variable 'font-lock-syntax-table)
1181 erlang-font-lock-syntax-table)
1182 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1183 'erlang-beginning-of-clause)
1184 (make-local-variable 'font-lock-keywords)
1185 (let ((level (cond ((boundp 'font-lock-maximum-decoration)
1186 (symbol-value 'font-lock-maximum-decoration))
1187 ((boundp 'font-lock-use-maximal-decoration)
1188 (symbol-value 'font-lock-use-maximal-decoration))
1191 (setq level (cdr-safe (or (assq 'erlang-mode level)
1193 ;; `level' can here be:
1194 ;; A number - The fontification level
1195 ;; nil - Use the default
1197 (cond ((eq level nil)
1198 (set 'font-lock-keywords erlang-font-lock-keywords))
1200 (set 'font-lock-keywords erlang-font-lock-keywords-1))
1202 (set 'font-lock-keywords erlang-font-lock-keywords-2))
1204 (set 'font-lock-keywords erlang-font-lock-keywords-3))))
1206 ;; Modern font-locks can handle the above much more elegantly:
1207 (set (make-local-variable 'font-lock-defaults)
1208 '((erlang-font-lock-keywords erlang-font-lock-keywords-1
1209 erlang-font-lock-keywords-2 erlang-font-lock-keywords-3)
1210 nil nil ((?_ . "w")) erlang-beginning-of-clause
1211 (font-lock-mark-block-function . erlang-mark-clause))))
1215 ;; Useful when defining your own keywords.
1216 (defun erlang-font-lock-set-face (ks &rest faces)
1217 "Replace the face components in a list of keywords.
1219 The first argument, KS, is a list of keywords. The rest of the
1220 arguments are expressions to replace the face information with. The
1221 first expression replaces the face of the first keyword, the second
1222 expression the second keyword etc.
1224 Should an expression be nil, the face of the corresponding keyword is
1227 Should fewer expressions than keywords be given, the last expression
1228 is used for all remaining keywords.
1230 Normally, the expressions are just atoms representing the new face.
1231 They could however be more complex, returning different faces in
1232 different situations.
1234 This function only handles keywords with elements on the forms:
1235 (REGEXP NUMBER FACE)
1236 (REGEXP NUMBER FACE OVERWRITE)
1238 This could be used when defining your own special font-lock setup, e.g:
1240 \(setq my-font-lock-keywords
1241 (append erlang-font-lock-keywords-func
1242 erlang-font-lock-keywords-dollar
1243 (erlang-font-lock-set-face
1244 erlang-font-lock-keywords-macros 'my-neon-green-face)
1245 (erlang-font-lock-set-face
1246 erlang-font-lock-keywords-lc 'my-deep-red 'my-light-red)
1247 erlang-font-lock-keywords-attr))
1249 For a more elaborate example, please see the beginning of the file
1253 (let* ((regexp (car (car ks)))
1254 (number (car (cdr (car ks))))
1255 (new-face (if (and faces (car faces))
1257 (car (cdr (cdr (car ks))))))
1258 (overwrite (car (cdr (cdr (cdr (car ks))))))
1259 (new-keyword (list regexp number new-face)))
1260 (if overwrite (nconc new-keyword (list overwrite)))
1261 (setq res (cons new-keyword res))
1263 (if (and faces (cdr faces))
1264 (setq faces (cdr faces)))))
1268 (defun erlang-font-lock-level-0 ()
1269 ;; DocStringOrig: font-cmd
1270 "Unfontify current buffer."
1275 (defun erlang-font-lock-level-1 ()
1276 ;; DocStringCopy: font-cmd
1277 "Fontify current buffer at level 1.
1278 This highlights function headers, reserved keywords, strings and comments."
1280 (require 'font-lock)
1281 (set 'font-lock-keywords erlang-font-lock-keywords-1)
1283 (funcall (symbol-function 'font-lock-fontify-buffer)))
1286 (defun erlang-font-lock-level-2 ()
1287 ;; DocStringCopy: font-cmd
1288 "Fontify current buffer at level 2.
1289 This highlights level 1 features (see `erlang-font-lock-level-1')
1290 plus bifs, guards and `single quotes'."
1292 (require 'font-lock)
1293 (set 'font-lock-keywords erlang-font-lock-keywords-2)
1295 (funcall (symbol-function 'font-lock-fontify-buffer)))
1298 (defun erlang-font-lock-level-3 ()
1299 ;; DocStringCopy: font-cmd
1300 "Fontify current buffer at level 3.
1301 This highlights level 2 features (see `erlang-font-lock-level-2')
1302 plus variables, macros and records."
1304 (require 'font-lock)
1305 (set 'font-lock-keywords erlang-font-lock-keywords-3)
1307 (funcall (symbol-function 'font-lock-fontify-buffer)))
1310 (defun erlang-menu-init ()
1311 "Init menus for Erlang mode.
1313 The variable `erlang-menu-items' contain a description of the Erlang
1314 mode menu. Normally, the list contains atoms, representing variables
1315 bound to pieces of the menu.
1317 Personal extensions could be added to `erlang-menu-personal-items'.
1319 This function should be called if any variable describing the
1320 menu configuration is changed."
1321 (erlang-menu-install "Erlang" erlang-menu-items erlang-mode-map t))
1324 (defun erlang-menu-install (name items keymap &optional popup)
1325 "Install a menu in Emacs or XEmacs based on an abstract description.
1327 NAME is the name of the menu.
1329 ITEMS is a list. The elements are either nil representing a horizontal
1330 line or a list with two or three elements. The first is the name of
1331 the menu item, the second the function to call, or a submenu, on the
1332 same same form as ITEMS. The third optional element is an expression
1333 which is evaluated every time the menu is displayed. Should the
1334 expression evaluate to nil the menu item is ghosted.
1336 KEYMAP is the keymap to add to menu to. (When using XEmacs, the menu
1337 will only be visible when this menu is the global, the local, or an
1338 activate minor mode keymap.)
1340 If POPUP is non-nil, the menu is bound to the XEmacs `mode-popup-menu'
1341 variable, i.e. it will popup when pressing the right mouse button.
1343 Please see the variable `erlang-menu-base-items'."
1344 (cond (erlang-xemacs-p
1345 (let ((menu (erlang-menu-xemacs name items keymap)))
1346 ;; We add the menu to the global menubar.
1347 (funcall (symbol-function 'add-submenu) nil menu)
1348 (setcdr erlang-xemacs-popup-menu (cdr menu))
1349 (if (and popup (boundp 'mode-popup-menu))
1350 (funcall (symbol-function 'set)
1351 'mode-popup-menu erlang-xemacs-popup-menu))))
1352 ((>= erlang-emacs-major-version 19)
1353 (define-key keymap (vector 'menu-bar (intern name))
1354 (erlang-menu-make-keymap name items)))
1358 (defun erlang-menu-make-keymap (name items)
1359 "Build a menu for Emacs 19."
1360 (let ((menumap (funcall (symbol-function 'make-sparse-keymap)
1363 id def first second third)
1364 (setq items (reverse items))
1366 ;; Replace any occurrence of atoms by their value.
1367 (while (and items (atom (car items)) (not (null (car items))))
1368 (if (and (boundp (car items))
1369 (listp (symbol-value (car items))))
1370 (setq items (append (reverse (symbol-value (car items)))
1372 (setq items (cdr items))))
1373 (setq first (car-safe (car items)))
1374 (setq second (car-safe (cdr-safe (car items))))
1375 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1377 (setq count (+ count 1))
1378 (setq id (intern (format "separator-%d" count)))
1379 (setq def '("--" . nil)))
1380 ((and (consp second) (eq (car second) 'lambda))
1381 (setq count (+ count 1))
1382 (setq id (intern (format "lambda-%d" count)))
1383 (setq def (cons first second)))
1386 (setq def (cons first second)))
1388 (setq count (+ count 1))
1389 (setq id (intern (format "submenu-%d" count)))
1390 (setq def (erlang-menu-make-keymap first second))))
1391 (define-key menumap (vector id) def)
1393 (put id 'menu-enable third))
1394 (setq items (cdr items)))
1395 (cons name menumap)))
1398 (defun erlang-menu-xemacs (name items &optional keymap)
1399 "Build a menu for XEmacs."
1401 first second third entry)
1403 ;; Replace any occurrence of atoms by their value.
1404 (while (and items (atom (car items)) (not (null (car items))))
1405 (if (and (boundp (car items))
1406 (listp (symbol-value (car items))))
1407 (setq items (append (reverse (symbol-value (car items)))
1409 (setq items (cdr items))))
1410 (setq first (car-safe (car items)))
1411 (setq second (car-safe (cdr-safe (car items))))
1412 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1414 (setq res (cons "------" res)))
1416 (setq res (cons (vector first second (or third t)) res)))
1417 ((and (consp second) (eq (car second) 'lambda))
1418 (setq res (cons (vector first (list 'call-interactively second)
1419 (or third t)) res)))
1421 (setq res (cons (cons first
1422 (cdr (erlang-menu-xemacs
1425 (setq items (cdr items)))
1426 (setq res (reverse res))
1427 ;; When adding a menu to a minor-mode keymap under Emacs,
1428 ;; it disappears when the mode is disabled. The expression
1429 ;; generated below imitates this behaviour.
1430 ;; (This could be expressed much clearer using backquotes,
1431 ;; but I don't want to pull in every package.)
1433 (let ((expr (list 'or
1434 (list 'eq keymap 'global-map)
1435 (list 'eq keymap (list 'current-local-map))
1440 'minor-mode-map-alist))))))
1441 (setq res (cons ':included (cons expr res)))))
1445 (defun erlang-menu-substitute (items alist)
1446 "Substitute functions in menu described by ITEMS.
1448 The menu ITEMS is updated destructively.
1450 ALIST is list of pairs where the car is the old function and cdr the new."
1451 (let (first second pair)
1453 (setq first (car-safe (car items)))
1454 (setq second (car-safe (cdr-safe (car items))))
1455 (cond ((null first))
1457 (setq pair (and second (assq second alist)))
1459 (setcar (cdr (car items)) (cdr pair))))
1460 ((and (consp second) (eq (car second) 'lambda)))
1462 (erlang-menu-substitute second alist)))
1463 (setq items (cdr items)))))
1466 (defun erlang-menu-add-above (entry above items)
1467 "Add menu ENTRY above menu entry ABOVE in menu ITEMS.
1468 Do nothing if the items already should be in the menu.
1469 Should ABOVE not be in the list, the entry is added at
1470 the bottom of the menu.
1472 The new menu is returned. No guarantee is given that the original
1473 menu is left unchanged.
1475 The equality test is performed by `eq'.
1477 Example: (erlang-menu-add-above 'my-erlang-menu-items
1478 'erlang-menu-man-items)"
1479 (erlang-menu-add-below entry above items t))
1482 (defun erlang-menu-add-below (entry below items &optional above-p)
1483 "Add menu ENTRY below menu items BELOW in the Erlang menu.
1484 Do nothing if the items already should be in the menu.
1485 Should BELOW not be in the list, items is added at the bottom
1488 The new menu is returned. No guarantee is given that the original
1489 menu is left unchanged.
1491 The equality test is performed by `eq'.
1495 \(setq erlang-menu-items
1496 (erlang-menu-add-below 'my-erlang-menu-items
1497 'erlang-menu-base-items
1498 erlang-menu-items))"
1499 (if (memq entry items)
1500 items ; Return the original menu.
1506 (setq res (append head (list entry)))
1508 ((eq below (car items))
1511 (append head (cons entry items))
1512 (append head (cons (car items)
1513 (cons entry (cdr items))))))
1516 (setq head (append head (list (car items))))
1517 (setq items (cdr items)))))
1520 (defun erlang-menu-delete (entry items)
1521 "Delete ENTRY from menu ITEMS.
1523 The new menu is returned. No guarantee is given that the original
1524 menu is left unchanged."
1529 (defun erlang-man-init ()
1530 "Add menus containing the manual pages of the Erlang.
1532 The variable `erlang-man-dirs' contains entries describing
1533 the location of the manual pages."
1535 (if erlang-man-inhibit
1537 (setq erlang-menu-man-items
1539 ("Man - Function" erlang-man-function)))
1541 (setq erlang-menu-man-items
1542 (append erlang-menu-man-items
1543 (erlang-man-make-top-menu erlang-man-dirs))))
1544 (setq erlang-menu-items
1545 (erlang-menu-add-above 'erlang-menu-man-items
1546 'erlang-menu-version-items
1548 (erlang-menu-init)))
1551 (defun erlang-man-uninstall ()
1552 "Remove the man pages from the Erlang mode."
1554 (setq erlang-menu-items
1555 (erlang-menu-delete 'erlang-menu-man-items erlang-menu-items))
1559 ;; The man menu is a hierarchal structure, with the manual sections
1560 ;; at the top, described by `erlang-man-dirs'. The next level could
1561 ;; either be the manual pages if not to many, otherwise it is an index
1562 ;; menu whose submenus will contain up to `erlang-man-max-menu-size'
1565 (defun erlang-man-make-top-menu (dir-list)
1566 "Create one menu entry per element of DIR-LIST.
1567 The format is described in the documentation of `erlang-man-dirs'."
1571 (setq dir (cond ((nth 2 (car dir-list))
1572 ;; Relative to `erlang-man-root-dir'.
1573 (and (stringp erlang-man-root-dir)
1574 (concat erlang-man-root-dir (nth 1 (car dir-list)))))
1577 (nth 1 (car dir-list)))))
1579 (file-readable-p dir))
1580 (setq menu (cons (list (car (car dir-list))
1581 (erlang-man-make-middle-menu
1582 (erlang-man-get-files dir)))
1584 (setq dir-list (cdr dir-list)))
1585 ;; Should no menus be found, generate a menu item which
1586 ;; will display a help text, when selected.
1590 (("Error! Why?" erlang-man-describe-error)))))))
1593 ;; Should the menu be to long, let's split it into a number of
1594 ;; smaller menus. Warning, this code contains beautiful
1595 ;; destructive operations!
1596 (defun erlang-man-make-middle-menu (filelist)
1597 "Create the second level menu from FILELIST.
1599 Should the list be longer than `erlang-man-max-menu-size', a tree of
1601 (if (<= (length filelist) erlang-man-max-menu-size)
1602 (erlang-man-make-menu filelist)
1604 (filelist (copy-sequence filelist))
1605 segment submenu pair)
1607 (setq pair (nthcdr (- erlang-man-max-menu-size 1) filelist))
1608 (setq segment filelist)
1611 (setq filelist (cdr pair))
1613 (setq submenu (erlang-man-make-menu segment))
1614 (setq menu (cons (list (concat (car (car submenu))
1616 (car (car (reverse submenu))))
1622 (defun erlang-man-make-menu (filelist)
1623 "Make a leaf menu based on FILELIST."
1627 (setq item (erlang-man-make-menu-item (car filelist)))
1629 (setq menu (cons item menu)))
1630 (setq filelist (cdr filelist)))
1634 (defun erlang-man-make-menu-item (file)
1635 "Create a menu item containing the name of the man page."
1636 (and (string-match ".*/\\([^/]+\\)\\.[^.]$" file)
1637 (let ((page (substring file (match-beginning 1) (match-end 1))))
1638 (list (capitalize page)
1641 (list 'funcall 'erlang-man-display-function
1645 (defun erlang-man-get-files (dir)
1646 "Return files in directory DIR."
1647 (directory-files dir t ".*\\.[0-9]\\'"))
1650 (defun erlang-man-module (&optional module)
1651 "Find manual page for MODULE, defaults to module of function under point.
1652 This function is aware of imported functions."
1654 (list (let* ((mod (car-safe (erlang-get-function-under-point)))
1656 (format "Manual entry for module%s: "
1657 (if (or (null mod) (string= mod ""))
1659 (format " (default %s)" mod))))))
1660 (if (string= input "")
1663 (or module (setq module (car (erlang-get-function-under-point))))
1664 (if (or (null module) (string= module ""))
1665 (error "No Erlang module name given"))
1666 (let ((dir-list erlang-man-dirs)
1667 (pat (concat "/" (regexp-quote module) "\\.[^.]$"))
1670 (while (and dir-list (null file))
1671 (setq file-list (erlang-man-get-files
1672 (if (nth 2 (car dir-list))
1673 (concat erlang-man-root-dir (nth 1 (car dir-list)))
1674 (nth 1 (car dir-list)))))
1675 (while (and file-list (null file))
1676 (if (string-match pat (car file-list))
1677 (setq file (car file-list)))
1678 (setq file-list (cdr file-list)))
1679 (setq dir-list (cdr dir-list)))
1681 (funcall erlang-man-display-function file)
1682 (error "No manual page for module %s found" module))))
1685 ;; Warning, the function `erlang-man-function' is a hack!
1686 ;; It links itself into the man code in a non-clean way. I have
1687 ;; chosen to keep it since it provides a very useful functionality
1688 ;; which is not possible to achieve using a clean approach.
1691 (defvar erlang-man-function-name nil
1692 "Name of function for last `erlang-man-function' call.
1693 Used for communication between `erlang-man-function' and the
1694 patch to `Man-notify-when-ready'.")
1696 (defun erlang-man-function (&optional name)
1697 "Find manual page for NAME, where NAME is module:function.
1698 The entry for `function' is displayed.
1700 This function is aware of imported functions."
1702 (list (let* ((mod-func (erlang-get-function-under-point))
1703 (mod (car-safe mod-func))
1704 (func (nth 1 mod-func))
1707 "Manual entry for `module:func' or `module'%s: "
1708 (if (or (null mod) (string= mod ""))
1710 (format " (default %s:%s)" mod func))))))
1711 (if (string= input "")
1713 (concat mod ":" func)
1716 ;; Emacs 18 doesn't provide `man'...
1723 (let ((mod-func (erlang-get-function-under-point)))
1724 (setq modname (car-safe mod-func))
1725 (setq funcname (nth 1 mod-func))))
1726 ((string-match ":" name)
1727 (setq modname (substring name 0 (match-beginning 0)))
1728 (setq funcname (substring name (match-end 0) nil)))
1730 (setq modname name)))
1731 (if (or (null modname) (string= modname ""))
1732 (error "No Erlang module name given"))
1733 (cond ((fboundp 'Man-notify-when-ready)
1734 ;; Emacs 19: The man command could possibly start an
1735 ;; asynchronous process, i.e. we must hook ourselves into
1736 ;; the system to be activated when the man-process
1740 (erlang-man-patch-notify)
1741 (setq erlang-man-function-name funcname))
1743 (erlang-man-module modname)
1744 (error (setq erlang-man-function-name nil))))
1746 (erlang-man-module modname)
1748 (erlang-man-find-function
1749 (or (get-buffer "*Manual Entry*") ; Emacs 18
1750 (current-buffer)) ; XEmacs
1754 ;; Should the defadvice be at the top level, the package `advice' would
1755 ;; be required. Now it is only required when this functionality
1756 ;; is used. (Emacs 19 specific.)
1757 (defun erlang-man-patch-notify ()
1758 "Patch the function `Man-notify-when-ready' to search for function.
1759 The variable `erlang-man-function-name' is assumed to be bound to
1760 the function name, or to nil.
1762 The reason for patching a function is that under Emacs 19, the man
1763 command is executed asynchronously."
1766 ;; This should never happened since this is only called when
1767 ;; running under Emacs 19.
1768 (error (error (concat "This command needs the package `advice', "
1769 "please upgrade your Emacs."))))
1771 (defadvice Man-notify-when-ready
1772 (after erlang-Man-notify-when-ready activate)
1773 "Set point at the documentation of the function name in
1774 `erlang-man-function-name' when the man page is displayed."
1775 (if erlang-man-function-name
1776 (erlang-man-find-function (ad-get-arg 0) erlang-man-function-name))
1777 (setq erlang-man-function-name nil)))
1780 (defun erlang-man-find-function (buf func)
1781 "Find manual page for function in `erlang-man-function-name' in buffer BUF."
1783 (let ((win (get-buffer-window buf)))
1787 (goto-char (point-min))
1788 (if (re-search-forward
1789 (concat "^[ \t]+" func " ?(")
1793 (set-window-point win (point)))
1794 (message "Could not find function `%s'" func)))))))
1797 (defun erlang-man-display (file)
1798 "Display FILE as a `man' file.
1799 This is the default manual page display function.
1800 The variables `erlang-man-display-function' contains the function
1802 ;; Emacs 18 doesn't `provide' man.
1807 (let ((process-environment (copy-sequence process-environment)))
1808 (if (string-match "\\(.*\\)/man[^/]*/\\([^/]+\\)\\.[^.]$" file)
1809 (let ((dir (substring file (match-beginning 1) (match-end 1)))
1810 (page (substring file (match-beginning 2) (match-end 2))))
1811 (if (fboundp 'setenv)
1812 (setenv "MANPATH" dir)
1814 (setq process-environment (cons (concat "MANPATH=" dir)
1815 process-environment)))
1816 (cond ((not (and (not erlang-xemacs-p)
1817 (= erlang-emacs-major-version 19)
1818 (< erlang-emacs-minor-version 29)))
1819 (manual-entry page))
1821 ;; Emacs 19.28 and earlier versions of 19:
1822 ;; The manual-entry command unconditionally prompts
1824 (funcall (symbol-function 'Man-getpage-in-background)
1826 (error "Can't find man page for %s\n" file)))))
1829 (defun erlang-man-describe-error ()
1830 "Describe why the manual pages weren't found."
1832 (with-output-to-temp-buffer "*Erlang Man Error*"
1833 (princ "Normally, this menu should contain Erlang manual pages.
1835 In order to find the manual pages, the variable `erlang-man-root-dir'
1836 should be bound to the name of the directory containing the Erlang
1837 man pages. The name should not include the final slash.
1839 Practically, you should add a line on the following form to
1840 your ~/.emacs, or ask your system administrator to add it to
1843 (setq erlang-man-root-dir \"/usr/local/erlang\")
1845 After installing the line, kill and restart Emacs, or restart Erlang
1846 mode with the command `M-x erlang-mode RET'.")))
1848 ;; Indentation code:
1850 (defun erlang-indent-command (&optional whole-exp)
1851 "Indent current line as Erlang code.
1852 With argument, indent any additional lines of the same clause
1853 rigidly along with this one."
1856 ;; If arg, always indent this line as Erlang
1857 ;; and shift remaining lines of clause the same amount.
1858 (let ((shift-amt (erlang-indent-line))
1861 (if erlang-tab-always-indent
1862 (beginning-of-line))
1864 (erlang-end-of-clause 1)
1870 (indent-code-rigidly beg end shift-amt "\n")))
1871 (if (and (not erlang-tab-always-indent)
1873 (skip-chars-backward " \t")
1876 (erlang-indent-line))))
1879 (defun erlang-indent-line ()
1880 "Indent current line as Erlang code.
1881 Return the amount the indentation changed by."
1882 (let ((pos (- (point-max) (point)))
1885 (beginning-of-line 1)
1887 (skip-chars-forward " \t")
1888 (cond ((looking-at "%")
1889 (setq indent (funcall comment-indent-function))
1890 (setq shift-amt (- indent (current-column))))
1892 (setq indent (erlang-calculate-indent))
1893 (cond ((null indent)
1894 (setq indent (current-indentation)))
1896 ;; This should never occur here.
1897 (error "Erlang mode error"))
1898 ((= (char-syntax (following-char)) ?\))
1899 (setq indent (1- indent))))
1900 (setq shift-amt (- indent (current-column)))))
1901 (if (zerop shift-amt)
1903 (delete-region beg (point))
1905 ;; If initial point was within line's indentation, position
1906 ;; after the indentation. Else stay at same point in text.
1907 (if (> (- (point-max) pos) (point))
1908 (goto-char (- (point-max) pos)))
1912 (defun erlang-indent-region (beg end)
1913 "Indent region of Erlang code.
1915 This is automagically called by the user level function `indent-region'."
1918 (let ((case-fold-search nil)
1920 (from-end (- (point-max) end))
1921 indent-point;; The beginning of the current line
1922 indent;; The indent amount
1926 (setq indent-point (point))
1927 (erlang-beginning-of-clause)
1928 ;; Parse the Erlang code from the beginning of the clause to
1929 ;; the beginning of the region.
1930 (while (< (point) indent-point)
1931 (setq state (erlang-partial-parse (point) indent-point state)))
1932 ;; Indent every line in the region
1934 (goto-char indent-point)
1935 (skip-chars-forward " \t")
1936 (cond ((looking-at "%")
1937 ;; Do not use our stack to help the user to customize
1938 ;; comment indentation.
1939 (setq indent (funcall comment-indent-function)))
1941 ;; Don't indent empty lines.
1946 (erlang-calculate-stack-indent (point) state)))
1947 (cond ((null indent)
1948 (setq indent (current-indentation)))
1950 ;; This should never occur here.
1951 (error "Erlang mode error"))
1952 ((= (char-syntax (following-char)) ?\))
1953 (setq indent (1- indent))))))
1954 (if (zerop (- indent (current-column)))
1956 (delete-region indent-point (point))
1958 ;; Find the next line in the region
1959 (goto-char indent-point)
1962 (setq indent-point (point)))
1963 (if (>= from-end (- (point-max) indent-point))
1965 (while (< (point) indent-point)
1966 (setq state (erlang-partial-parse
1967 (point) indent-point state))))))))
1970 (defun erlang-indent-current-buffer ()
1971 "Indent current buffer as Erlang code."
1976 (erlang-indent-region (point-min) (point-max)))))
1979 (defun erlang-indent-function ()
1980 "Indent current Erlang function."
1983 (let ((end (progn (erlang-end-of-function 1) (point)))
1984 (beg (progn (erlang-beginning-of-function 1) (point))))
1985 (erlang-indent-region beg end))))
1988 (defun erlang-indent-clause ()
1989 "Indent current Erlang clause."
1992 (let ((end (progn (erlang-end-of-clause 1) (point)))
1993 (beg (progn (erlang-beginning-of-clause 1) (point))))
1994 (erlang-indent-region beg end))))
1997 (defmacro erlang-push (x stack) (list 'setq stack (list 'cons x stack)))
1998 (defmacro erlang-pop (stack) (list 'setq stack (list 'cdr stack)))
1999 ;; Would much prefer to make caddr a macro but this clashes.
2000 (defun erlang-caddr (x) (car (cdr (cdr x))))
2003 (defun erlang-calculate-indent (&optional parse-start)
2004 "Compute appropriate indentation for current line as Erlang code.
2005 Return nil if line starts inside string, t if in a comment."
2007 (let ((indent-point (point))
2008 (case-fold-search nil)
2011 (goto-char parse-start)
2012 (erlang-beginning-of-clause))
2013 (while (< (point) indent-point)
2014 (setq state (erlang-partial-parse (point) indent-point state)))
2015 (erlang-calculate-stack-indent indent-point state))))
2017 (defun erlang-show-syntactic-information ()
2018 "Show syntactic information for current line."
2023 (let ((starting-point (point))
2024 (case-fold-search nil)
2026 (erlang-beginning-of-clause)
2027 (while (< (point) starting-point)
2028 (setq state (erlang-partial-parse (point) starting-point state)))
2029 (message "%S" state))))
2032 (defun erlang-partial-parse (from to &optional state)
2033 "Parse Erlang syntax starting at FROM until TO, with an optional STATE.
2034 Value is list (stack token-start token-type in-what)."
2035 (goto-char from) ; Start at the beginning
2036 (erlang-skip-blank to)
2037 (let ((cs (char-syntax (following-char)))
2043 ;; Done: Return previous state.
2045 (setq token (nth 1 state))
2046 (setq cs (nth 2 state))
2047 (setq in-what (nth 3 state)))
2049 ;; Word constituent: check and handle keywords.
2051 (cond ((looking-at "\\(end\\|after\\)[^_a-zA-Z0-9]")
2052 ;; Must pop top icr layer, `after' will push a new
2055 (while (and stack (eq (car (car stack)) '->))
2057 (if (and stack (memq (car (car stack)) '(icr begin)))
2058 (erlang-pop stack))))
2059 ((looking-at "catch[^,\n\\of]*\n")
2060 ;; Must pop top icr layer, `catch' in try/catch
2061 ;;will push a new layer next.
2063 (while (and stack (eq (car (car stack)) '->))
2065 (if (and stack (memq (car (car stack)) '(icr begin)))
2066 (erlang-pop stack))))
2068 (cond ((looking-at "\\(if\\|case\\|receive\\|try\\)[^_a-zA-Z0-9]")
2069 ;; Must push a new icr (if/case/receive) layer.
2070 (erlang-push (list 'icr token (current-column)) stack))
2071 ((looking-at "\\(fun\\)[^_a-zA-Z0-9]")
2072 ;; Push a new icr layer if we are defining a `fun'
2073 ;; expression, not when we are refering an existing
2076 (goto-char (match-end 1))
2077 (erlang-skip-blank to)
2078 (eq (following-char) ?\())
2079 (erlang-push (list 'icr token (current-column)) stack)))
2080 ((looking-at "\\(begin\\|query\\)[^_a-zA-Z0-9]")
2081 (erlang-push (list 'begin token (current-column)) stack))
2082 ;; In test suites you may want to do something like
2083 ;; ?match(Mem when integer(Mem), mnesia:table_info(Tab,
2084 ;; memory)), and then the following if/case/receive
2085 ;; statement will mess up the indentation by fooling the
2086 ;; erlang mode to think the 'when' in the argument is a
2087 ;; "real" when. The following three clauses will avoid
2089 ((looking-at "when[^->\.]*if[^->\.]*->"))
2090 ((looking-at "when[^->\.]*case[^->\.]*->"))
2091 ((looking-at "when[^->\.]*receive[^->\.]*->"))
2093 ((looking-at "when [^->\.]*->")
2094 (erlang-push (list 'when token (current-column)) stack))
2095 ((looking-at "after[.]+->")
2096 (erlang-push (list 'icr token (current-column)) stack))
2097 ((looking-at "after[^_a-zA-Z0-9->]")
2098 (erlang-push (list 'icr token (current-column)) stack)
2099 (erlang-push (list '-> token (current-column)) stack))
2100 ((looking-at "catch[^,\n\\of]*\n")
2101 (erlang-push (list 'icr token (current-column)) stack))
2105 ;; String: Try to skip over it. (Catch error if not complete.)
2112 (setq in-what 'string)
2115 (setq in-what 'string)
2118 ;; Symbol constituent or punctuation
2124 ((= (following-char) ?\;)
2125 (if (and stack (eq (car (car stack)) '->))
2130 ((looking-at "\\.\\(\\s \\|\n\\|\\s<\\)")
2135 ((looking-at "->\\|:-")
2137 (back-to-indentation)
2138 (cond ((looking-at "after[^_a-zA-Z0-9]")
2139 (erlang-pop stack))))
2140 (if (and stack (eq (car (car stack)) 'when))
2142 (erlang-push (list '-> token (current-column)) stack)
2145 ;; List-comprehension divider
2147 (erlang-push (list '|| token (current-column)) stack)
2150 ;; Parameter separator
2154 ;; Bit-syntax open paren
2156 (erlang-push (list '\( token (current-column)) stack)
2159 ;; Bbit-syntax close paren
2161 (while (memq (car (car stack)) '(|| ->))
2163 (cond ((eq (car (car stack)) '\()
2165 ((memq (car (car stack)) '(icr begin))
2166 (error "Missing `end'"))
2168 (error "Unbalanced parentheses")))
2172 ((= (following-char) ??)
2177 ;; Other punctuation: Skip over it and any following punctuation
2179 ;; Skip over all characters in the operand.
2180 (skip-syntax-forward "."))
2182 ;; Other char: Skip over it.
2188 (erlang-push (list '\( token (current-column)) stack)
2191 ;; Close parenthesis
2193 (while (memq (car (car stack)) '(|| ->))
2195 (cond ((eq (car (car stack)) '\()
2197 ((eq (car (car stack)) 'icr)
2199 ;; Normal catch not try-catch might have caused icr
2200 ;; and then incr should be removed and is not an error.
2201 (if (eq (car (car stack)) '\()
2204 (error "Missing `end'"))
2206 ((eq (car (car stack)) 'begin)
2207 (error "Missing `end'")
2209 (error "Unbalanced parenthesis"))
2213 ;; Character quote: Skip it and the quoted char.
2217 ;; Character escape: Skip it and the escape sequence.
2220 (skip-syntax-forward "w"))
2225 (list stack token cs in-what)))
2227 (defun erlang-calculate-stack-indent (indent-point state)
2228 "From the given last position and state (stack) calculate indentation.
2229 Return nil if inside string, t if in a comment."
2230 (let* ((stack (and state (car state)))
2231 (token (nth 1 state))
2232 (stack-top (and stack (car stack))))
2233 (cond ((null state) ;No state
2237 (eq (nth 3 state) 'comment))
2239 (if (looking-at "when[^_a-zA-Z0-9]")
2242 ((eq (car stack-top) '\()
2243 ;; Element of list, tuple or part of an expression,
2244 (if (null erlang-argument-indent)
2245 ;; indent to next column.
2246 (1+ (nth 2 stack-top))
2247 (goto-char (nth 1 stack-top))
2248 (cond ((looking-at "[({]\\s *\\($\\|%\\)")
2249 ;; Line ends with parenthesis.
2250 (+ (erlang-indent-find-preceding-expr)
2251 erlang-argument-indent))
2253 ;; Indent to the same column as the first
2255 (goto-char (1+ (nth 1 stack-top)))
2256 (skip-chars-forward " \t")
2257 (current-column)))))
2258 ((eq (car stack-top) 'icr)
2259 ;; The default indentation is the column of the option
2260 ;; directly following the keyword. (This does not apply to
2261 ;; `case'.) Should no option be on the same line, the
2262 ;; indentation is the indentation of the keyword +
2263 ;; `erlang-indent-level'.
2265 ;; `after' should be indented to the save level as the
2266 ;; corresponding receive.
2267 (if (looking-at "after[^_a-zA-Z0-9]")
2270 (goto-char (nth 1 stack-top))
2271 (if (looking-at "case[^_a-zA-Z0-9]")
2272 (+ (nth 2 stack-top) erlang-indent-level)
2273 (skip-chars-forward "a-z")
2274 (skip-chars-forward " \t")
2275 (if (memq (following-char) '(?% ?\n))
2276 (+ (nth 2 stack-top) erlang-indent-level)
2277 (current-column)))))
2278 (if (looking-at "catch[^_a-zA-Z0-9]")
2281 (goto-char (nth 1 stack-top))
2282 (if (looking-at "case[^_a-zA-Z0-9]")
2283 (+ (nth 2 stack-top) erlang-indent-level)
2284 (skip-chars-forward "a-z")
2285 (skip-chars-forward " \t")
2286 (if (memq (following-char) '(?% ?\n))
2287 (+ (nth 2 stack-top) erlang-indent-level)
2288 (current-column)))))
2290 ;; Real indentation, where operators create extra indentation etc.
2291 ((memq (car stack-top) '(-> || begin))
2292 (goto-char (nth 1 stack-top))
2293 ;; Check if there is more code after the '->' on the
2294 ;; same line. If so use this indentation as base, else
2295 ;; use parent indentation + 2 * level as base.
2296 (let ((off erlang-indent-level)
2298 (cond ((null (cdr stack))) ; Top level in function.
2299 ((eq (car stack-top) 'begin)
2301 ((eq (car stack-top) '->)
2302 (setq off (* 2 erlang-indent-level))))
2303 (let ((base (erlang-indent-find-base stack indent-point off skip)))
2304 ;; Look at last thing to see how we are to move relative
2307 (cond ((looking-at "||\\|,\\|->\\|:-")
2309 ((erlang-at-keyword)
2310 (+ (current-column) erlang-indent-level))
2311 ((or (= (char-syntax (following-char)) ?.)
2312 (erlang-at-operator))
2313 (+ base erlang-indent-level))
2315 (goto-char indent-point)
2316 (cond ((memq (following-char) '(?\( ?{))
2317 ;; Function application or record.
2318 (+ (erlang-indent-find-preceding-expr)
2319 erlang-argument-indent))
2320 ;; Empty line, or end; treat it as the end of
2321 ;; the block. (Here we have a choice: should
2322 ;; the user be forced to reindent continued
2323 ;; lines, or should the "end" be reindented?)
2324 ((looking-at "\\(end\\|after\\|catch\\)[^_a-zA-Z0-9]\\|$")
2325 (if (eq (car (car stack)) '->)
2328 (erlang-caddr (car stack))
2330 ;; Avoid treating comments a continued line.
2331 ((= (following-char) ?%)
2333 ;; Continued line (e.g. line beginning
2334 ;; with an operator.)
2335 (t (+ base erlang-indent-level)))))))
2337 ((eq (car stack-top) 'when)
2338 (goto-char (nth 1 stack-top))
2339 (if (looking-at "when\\s *\\($\\|%\\)")
2342 (if (and stack (eq (nth 0 (car stack)) 'icr))
2344 (goto-char (nth 1 (car stack)))
2345 (+ (nth 2 (car stack)) erlang-indent-guard
2346 ;; receive XYZ or receive
2348 (if (looking-at "[a-z]+\\s *\\($\\|%\\)")
2350 (* 2 erlang-indent-level))))
2351 erlang-indent-guard))
2352 ;; "when" is followed by code, let's indent to the same
2354 (forward-char 4) ; Skip "when"
2355 (skip-chars-forward " \t")
2356 (current-column))))))
2359 (defun erlang-indent-find-base (stack indent-point &optional offset skip)
2360 "Find the base column for current stack."
2361 (or skip (setq skip 2))
2362 (or offset (setq offset erlang-indent-level))
2364 (let* ((stack-top (car stack)))
2365 (goto-char (nth 1 stack-top))
2367 (if (looking-at "\\s *\\($\\|%\\)")
2369 (if (memq (car stack-top) '(-> ||))
2371 ;; Take parent identation + offset,
2372 ;; else just erlang-indent-level if no parent
2374 (+ (erlang-caddr (car stack))
2376 erlang-indent-level))
2377 (erlang-skip-blank indent-point)
2378 (current-column)))))
2381 ;; Does not handle `begin' .. `end'.
2382 (defun erlang-indent-find-preceding-expr ()
2383 "Return the first column of the preceding expression.
2384 This assumes that the preceding expression is either simple
2385 \(i.e. an atom) or parenthesized."
2388 (let ((col (current-column)))
2389 (skip-chars-backward " \t")
2390 ;; Needed to match the colon in "'foo':'bar'".
2391 (if (not (memq (preceding-char) '(?# ?:)))
2395 (current-column)))))
2398 (defun erlang-skip-blank (&optional lim)
2399 "Skip over whitespace and comments until limit reached."
2400 (or lim (setq lim (point-max)))
2402 (while (and (not stop) (< (point) lim))
2403 (cond ((= (following-char) ?%)
2404 (skip-chars-forward "^\n" lim))
2405 ((= (following-char) ?\n)
2406 (skip-chars-forward "\n" lim))
2407 ((looking-at "\\s ")
2408 (if (re-search-forward "\\S " lim 'move)
2414 (defun erlang-at-keyword ()
2415 "Are we looking at an Erlang keyword which will increase indentation?"
2416 (looking-at (concat "\\(when\\|if\\|fun\\|case\\|begin\\|query\\|"
2417 "of\\|receive\\|after\\|catch\\)[^_a-zA-Z0-9]")))
2419 (defun erlang-at-operator ()
2420 "Are we looking at an Erlang operator?"
2422 "\\(bnot\\|div\\|mod\\|band\\|bor\\|bxor\\|bsl\\|bsr\\)[^_a-zA-Z0-9]"))
2424 (defun erlang-comment-indent ()
2425 "Compute Erlang comment indentation.
2427 Used both by `indent-for-comment' and the Erlang specific indentation
2429 (cond ((looking-at "%%") 0)
2431 (or (erlang-calculate-indent)
2432 (current-indentation)))))
2435 ;;; Erlang movement commands
2437 ;; All commands below work as movement commands. I.e. if the point is
2438 ;; at the end of the clause, and the command `erlang-end-of-clause' is
2439 ;; executed, the point is moved to the end of the NEXT clause. (This
2440 ;; mimics the behaviour of `end-of-defun'.)
2442 ;; Personally I would like to rewrite them to be "pure", and add a set
2443 ;; of movement functions, like `erlang-next-clause',
2444 ;; `erlang-previous-clause', and the same for functions.
2446 ;; The current implementation makes it hopeless to use the functions as
2447 ;; subroutines in more complex commands. /andersl
2449 (defun erlang-beginning-of-clause (&optional arg)
2450 "Move backward to previous start of clause.
2451 With argument, do this that many times.
2452 Return t unless search stops due to end of buffer."
2454 (or arg (setq arg 1))
2456 ;; Step back to the end of the previous line, unless we are at
2457 ;; the beginning of the buffer. The reason for this move is
2458 ;; that the regexp below includes the last character of the
2461 (or (looking-at "\n")
2464 (if (looking-at "\\`\n")
2466 ;; The regexp matches a function header that isn't
2467 ;; included in a string.
2468 (and (re-search-forward "\\(\\`\\|\\`\n\\|[^\\]\n\\)\\([a-z]\\|'\\|-\\)"
2470 (let ((beg (match-beginning 2)))
2471 (and beg (goto-char beg))
2474 (defun erlang-end-of-clause (&optional arg)
2475 "Move to the end of the current clause.
2476 With argument, do this that many times."
2478 (or arg (setq arg 1))
2479 (while (and (looking-at "[ \t]*[%\n]")
2480 (zerop (forward-line 1))))
2481 ;; Move to the next clause.
2482 (erlang-beginning-of-clause (- arg))
2483 (beginning-of-line);; Just to be sure...
2485 (while (and (not (bobp)) continue)
2487 (skip-chars-forward " \t")
2488 (if (looking-at "[%\n]")
2491 (setq continue nil)))))
2493 (defun erlang-mark-clause ()
2494 "Put mark at end of clause, point at beginning."
2497 (erlang-end-of-clause 1)
2498 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2501 (push-mark (point) nil t)
2502 (error (push-mark (point))))
2503 (erlang-beginning-of-clause 1)
2504 ;; The above function deactivates the mark.
2505 (if (boundp 'deactivate-mark)
2506 (funcall (symbol-function 'set) 'deactivate-mark nil)))
2508 (defun erlang-beginning-of-function (&optional arg)
2509 "Move backward to previous start of function.
2510 With positive argument, do this that many times.
2511 With negative argument, search forward.
2513 Return t unless search stops due to end of buffer."
2515 (or arg (setq arg 1))
2519 (while (and (> arg 0)
2520 (and (erlang-beginning-of-clause 1)
2521 (let ((start (point))
2522 (name (erlang-name-of-function))
2523 (arity (erlang-get-function-arity)))
2524 ;; Note: "arity" is nil for e.g. "-import", hence
2525 ;; two "-import" clauses are not considered to
2526 ;; be part of the same function.
2527 (while (and (erlang-beginning-of-clause 1)
2529 (erlang-name-of-function))
2532 (erlang-get-function-arity)))
2533 (setq start (point)))
2536 (setq arg (1- arg))))
2540 (erlang-beginning-of-clause 1)
2541 ;; Step -arg functions forward.
2542 (while (and (< arg 0)
2543 ;; Step one function forward, or stop if the end of
2544 ;; the buffer was reached. Return t if we found the
2546 (let ((name (erlang-name-of-function))
2547 (arity (erlang-get-function-arity))
2548 (found (erlang-beginning-of-clause -1)))
2550 (string-equal name (erlang-name-of-function))
2553 (erlang-get-function-arity)))
2554 (setq found (erlang-beginning-of-clause -1)))
2556 (setq arg (1+ arg)))))
2560 (defun erlang-end-of-function (&optional arg)
2561 "Move forward to next end of function.
2563 With argument, do this that many times.
2564 With negative argument go towards the beginning of the buffer."
2566 (or arg (setq arg 1))
2569 (while (and (> arg 0) (< (point) (point-max)))
2570 (let ((pos (point)))
2575 (erlang-beginning-of-clause 1)))
2577 (or (bobp) (forward-char -1))
2578 (erlang-beginning-of-clause -1))
2580 (erlang-pass-over-function)
2581 (skip-chars-forward " \t")
2582 (if (looking-at "[%\n]")
2585 (setq arg (1- arg)))
2588 (let ((pos (point)))
2589 (erlang-beginning-of-clause 1)
2590 (erlang-pass-over-function)
2592 (if (>= (point) pos)
2593 (if (erlang-beginning-of-function 2)
2595 (erlang-pass-over-function)
2596 (skip-chars-forward " \t")
2597 (if (looking-at "[%\n]")
2599 (goto-char (point-min)))))
2600 (setq arg (1+ arg)))))
2603 (if (default-boundp 'beginning-of-defun-function)
2604 (defalias 'erlang-mark-function 'mark-defun)
2605 (defun erlang-mark-function ()
2606 "Put mark at end of function, point at beginning."
2609 (erlang-end-of-function 1)
2610 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2613 (push-mark (point) nil t)
2614 (error (push-mark (point))))
2615 (erlang-beginning-of-function 1)
2616 ;; The above function deactivates the mark.
2617 (if (boundp 'deactivate-mark)
2618 (funcall (symbol-function 'set) 'deactivate-mark nil)))))
2620 (defun erlang-pass-over-function ()
2623 (and (not (looking-at "\\.\\(\\s \\|\n\\|\\s<\\)"))
2629 (defun erlang-name-of-function ()
2631 ;; Skip over attribute leader.
2632 (if (looking-at "-[ \t]*")
2633 (re-search-forward "-[ \t]*" nil 'move))
2634 (let ((start (point)))
2636 (buffer-substring start (point)))))
2641 (defun erlang-fill-paragraph (&optional justify)
2642 "Like \\[fill-paragraph], but handle Erlang comments.
2643 If any of the current line is a comment, fill the comment or the
2644 paragraph of it that point is in, preserving the comment's indentation
2647 (let ((has-comment nil)
2648 ;; If has-comment, the appropriate fill-prefix for the comment.
2649 comment-fill-prefix)
2650 ;; Figure out what kind of comment we are looking at.
2654 ;; Find the command prefix.
2655 ((looking-at (concat "\\s *" comment-start-skip))
2656 (setq has-comment t)
2657 (setq comment-fill-prefix (buffer-substring (match-beginning 0)
2659 ;; A line with some code, followed by a comment? Remember that the
2660 ;; % which starts the comment shouldn't be part of a string or
2663 (while (not (looking-at "%\\|$"))
2664 (skip-chars-forward "^%\n\"\\\\")
2666 ((eq (char-after (point)) ?\\) (forward-char 2))
2667 ((eq (char-after (point)) ?\") (forward-sexp 1))))
2668 (looking-at comment-start-skip))
2669 (setq has-comment t)
2670 (setq comment-fill-prefix
2671 (concat (make-string (current-column) ? )
2672 (buffer-substring (match-beginning 0) (match-end 0)))))))
2673 (if (not has-comment)
2674 (fill-paragraph justify)
2675 ;; Narrow to include only the comment, and then fill the region.
2678 ;; Find the first line we should include in the region to fill.
2680 (while (and (zerop (forward-line -1))
2681 (looking-at "^\\s *%")))
2682 ;; We may have gone to far. Go forward again.
2683 (or (looking-at "^\\s *%")
2686 ;; Find the beginning of the first line past the region to fill.
2688 (while (progn (forward-line 1)
2689 (looking-at "^\\s *%")))
2691 ;; Lines with only % on them can be paragraph boundaries.
2692 (let ((paragraph-start (concat paragraph-start "\\|^[ \t%]*$"))
2693 (paragraph-separate (concat paragraph-start "\\|^[ \t%]*$"))
2694 (fill-prefix comment-fill-prefix))
2695 (fill-paragraph justify))))))
2698 (defun erlang-uncomment-region (beg end)
2699 "Uncomment all commented lines in the region."
2701 (comment-region beg end -1))
2704 (defun erlang-generate-new-clause ()
2705 "Create additional Erlang clause header.
2707 Parses the source file for the name of the current Erlang function.
2708 Create the header containing the name, A pair of parentheses,
2709 and an arrow. The space between the function name and the
2710 first parenthesis is preserved. The point is placed between
2713 (let ((name (save-excursion
2714 (and (erlang-beginning-of-clause)
2715 (erlang-get-function-name t))))
2716 (arrow (save-excursion
2717 (and (erlang-beginning-of-clause)
2718 (erlang-get-function-arrow)))))
2719 (if (or (null arrow) (null name))
2720 (error "Can't find name of current Erlang function"))
2721 (if (and (bolp) (eolp))
2727 (insert ") " arrow))
2728 (if erlang-new-clause-with-arguments
2729 (erlang-clone-arguments))))
2732 (defun erlang-clone-arguments ()
2733 "Insert, at the point, the argument list of the previous clause.
2735 The mark is set at the beginning of the inserted text, the point
2738 (let ((args (save-excursion
2740 (and (erlang-beginning-of-clause)
2741 (erlang-get-function-arguments))))
2744 (error "Can't clone argument list"))
2748 ;;; Information retrieval functions.
2750 (defun erlang-buffer-substring (beg end)
2751 "Like `buffer-substring-no-properties'.
2752 Although, this function works on all versions of Emacs."
2753 (if (fboundp 'buffer-substring-no-properties)
2754 (funcall (symbol-function 'buffer-substring-no-properties) beg end)
2755 (buffer-substring beg end)))
2758 (defun erlang-get-module ()
2759 "Return the name of the module as specified by `-module'.
2761 Return nil if file contains no `-module' attribute."
2765 (goto-char (point-min))
2766 (let ((md (match-data)))
2768 (if (re-search-forward
2770 (concat "^-module\\s *(\\s *\\(\\("
2772 "\\)?\\)\\s *)\\s *\\."))
2774 (erlang-remove-quotes
2775 (erlang-buffer-substring (match-beginning 1)
2778 (store-match-data md))))))
2781 (defun erlang-get-module-from-file-name (&optional file)
2782 "Extract the module name from a file name.
2784 First, the directory part is removed. Second, the part of the file name
2785 matching `erlang-file-name-extension-regexp' is removed.
2787 Should the match fail, nil is returned.
2789 By modifying `erlang-file-name-extension-regexp' to match files other
2790 than Erlang source files, Erlang specific functions could be applied on
2791 non-Erlang files. Most notably; the support for Erlang modules in the
2792 tags system could be used by files written in other languages."
2793 (or file (setq file buffer-file-name))
2796 (setq file (file-name-nondirectory file))
2797 (if (string-match erlang-file-name-extension-regexp file)
2798 (substring file 0 (match-beginning 0))
2802 ;; Used by `erlang-get-export' and `erlang-get-import'.
2804 (defun erlang-get-function-arity-list ()
2805 "Parse list of `function/arity' as used by `-import' and `-export'.
2807 Point must be before the opening bracket. When the
2808 function returns the point will be placed after the closing bracket.
2810 The function does not return an error if the list is incorrectly
2813 Return list of (function . arity). The order of the returned list
2814 corresponds to the order of the parsed Erlang list."
2818 (if (not (eq (preceding-char) ?\[))
2819 '() ; Not looking at an Erlang list.
2820 (while ; Note: `while' has no body.
2823 (and (looking-at (eval-when-compile
2824 (concat erlang-atom-regexp "/\\([0-9]+\\)\\>")))
2828 (erlang-remove-quotes
2829 (erlang-buffer-substring
2830 (match-beginning 1) (match-end 1)))
2832 (erlang-buffer-substring
2834 (+ 1 erlang-atom-regexp-matches))
2836 (+ 1 erlang-atom-regexp-matches)))))
2838 (goto-char (match-end 0))
2841 ;; Test if there are more exported functions.
2842 (eq (preceding-char) ?,))))))
2846 ;;; Note that `-export' and the open parenthesis must be written on
2849 (defun erlang-get-export ()
2850 "Return a list of `(function . arity)' as specified by `-export'."
2852 (goto-char (point-min))
2853 (let ((md (match-data))
2857 (while (re-search-forward "^-export\\s *(" (point-max) t)
2859 (setq res (nconc res (erlang-get-function-arity-list))))
2861 (store-match-data md)))))
2864 (defun erlang-get-import ()
2865 "Parse an Erlang source file for imported functions.
2867 Return an alist with module name as car part and list of conses containing
2868 function and arity as cdr part."
2870 (goto-char (point-min))
2871 (let ((md (match-data))
2875 (while (re-search-forward "^-import\\s *(" (point-max) t)
2877 (if (looking-at erlang-atom-regexp)
2878 (let ((module (erlang-remove-quotes
2879 (erlang-buffer-substring
2882 (goto-char (match-end 0))
2884 (if (eq (following-char) ?,)
2888 (let ((funcs (erlang-get-function-arity-list))
2889 (pair (assoc module res)))
2891 (setcdr pair (nconc (cdr pair) funcs))
2892 (setq res (cons (cons module funcs)
2895 (store-match-data md)))))
2898 (defun erlang-get-function-name (&optional arg)
2899 "Return name of current function, or nil.
2901 If optional argument is non-nil, everything up to and including
2902 the first `(' is returned.
2904 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2906 (if (not (eobp)) (forward-char 1))
2907 (and (erlang-beginning-of-clause)
2908 (erlang-get-function-name t)))"
2909 (let ((n (if arg 0 1)))
2910 (and (looking-at (eval-when-compile
2911 (concat "^" erlang-atom-regexp "\\s *(")))
2912 (erlang-buffer-substring (match-beginning n) (match-end n)))))
2915 (defun erlang-get-function-arrow ()
2916 "Return arrow of current function, could be \"->\", \":-\" or nil.
2918 The \":-\" arrow is used by mnesia queries.
2920 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2922 (if (not (eobp)) (forward-char 1))
2923 (and (erlang-beginning-of-clause)
2924 (erlang-get-function-arrow)))"
2927 (re-search-forward "[^-:]*-\\|:" (point-max) t)
2928 (erlang-buffer-substring (- (point) 1) (+ (point) 1)))))
2930 (defun erlang-get-function-arity ()
2931 "Return the number of arguments of function at point, or nil."
2932 (and (looking-at (eval-when-compile
2933 (concat "^" erlang-atom-regexp "\\s *(")))
2935 (goto-char (match-end 0))
2943 ((looking-at "\\s *)")
2945 ((looking-at "\\s *\\($\\|%\\)")
2947 ((looking-at "\\s *,")
2948 (setq res (+ 1 res))
2949 (goto-char (match-end 0)))
2952 (setq res (+ 1 res)))
2957 (defun erlang-get-function-arguments ()
2958 "Return arguments of current function, or nil."
2959 (if (not (looking-at (eval-when-compile
2960 (concat "^" erlang-atom-regexp "\\s *("))))
2964 (let ((start (match-end 0)))
2965 (goto-char (- start 1))
2967 (erlang-buffer-substring start (- (point) 1)))
2971 (defun erlang-get-function-under-point ()
2972 "Return the module and function under the point, or nil.
2974 Should no explicit module name be present at the point, the
2975 list of imported functions is searched.
2977 The following could be returned:
2978 (\"module\" \"function\") -- Both module and function name found.
2979 (nil \"function\") -- No module name was found.
2980 nil -- No function name found
2982 In the future the list may contain more elements."
2984 (let ((md (match-data))
2986 (if (eq (char-syntax (following-char)) ? )
2987 (skip-chars-backward " \t"))
2988 (skip-chars-backward "a-zA-Z0-9_:'")
2989 (cond ((looking-at (eval-when-compile
2990 (concat erlang-atom-regexp ":" erlang-atom-regexp)))
2992 (erlang-remove-quotes
2993 (erlang-buffer-substring
2994 (match-beginning 1) (match-end 1)))
2995 (erlang-remove-quotes
2996 (erlang-buffer-substring
2997 (match-beginning (1+ erlang-atom-regexp-matches))
2998 (match-end (1+ erlang-atom-regexp-matches)))))))
2999 ((looking-at erlang-atom-regexp)
3000 (let ((fk (erlang-remove-quotes
3001 (erlang-buffer-substring
3002 (match-beginning 0) (match-end 0))))
3004 (imports (erlang-get-import)))
3005 (while (and imports (null mod))
3006 (if (assoc fk (cdr (car imports)))
3007 (setq mod (car (car imports)))
3008 (setq imports (cdr imports))))
3009 (setq res (list mod fk)))))
3010 (store-match-data md)
3014 ;; TODO: Escape single quotes inside the string without
3015 ;; replace-regexp-in-string.
3016 (defun erlang-add-quotes-if-needed (str)
3017 "Return STR, possibly with quotes."
3018 (if (and (stringp str)
3019 (not (string-match (eval-when-compile
3020 (concat "\\`" erlang-atom-regexp "\\'")) str)))
3021 (progn (if (fboundp 'replace-regexp-in-string)
3022 (setq str (replace-regexp-in-string "'" "\\'" str t t )))
3023 (concat "'" str "'"))
3027 (defun erlang-remove-quotes (str)
3028 "Return STR without quotes, if present."
3029 (let ((md (match-data)))
3031 (if (string-match "\\`'\\(.*\\)'\\'" str)
3032 (substring str 1 -1)
3034 (store-match-data md))))
3037 ;;; Check module name
3039 ;; The function `write-file', bound to C-x C-w, calls
3040 ;; `set-visited-file-name' which clears the hook. :-(
3041 ;; To make sure that the hook always is present, we advise
3042 ;; `set-visited-file-name'.
3043 (defun erlang-check-module-name-init ()
3044 "Initialize the functionality to compare file and module names.
3046 Unless we have `before-save-hook', we redefine the function
3047 `set-visited-file-name' since it clears the variable
3048 `local-write-file-hooks'. The original function definition is
3049 stored in `erlang-orig-set-visited-file-name'."
3050 (if (boundp 'before-save-hook)
3051 ;; If we have that, `make-local-hook' is obsolete.
3052 (add-hook 'before-save-hook 'erlang-check-module-name nil t)
3054 (unless (ad-advised-definition-p 'set-visited-file-name)
3055 (defadvice set-visited-file-name (after erlang-set-visited-file-name
3057 (if (eq major-mode 'erlang-mode)
3058 (add-hook 'local-write-file-hooks 'erlang-check-module-name))))
3059 (add-hook 'local-write-file-hooks 'erlang-check-module-name)))
3062 (defun erlang-check-module-name ()
3063 "If the module name doesn't match file name, ask for permission to change.
3065 The variable `erlang-check-module-name' controls the behaviour of this
3066 function. It it is nil, this function does nothing. If it is t, the
3067 source is silently changed. If it is set to the atom `ask', the user
3070 This function is normally placed in the hook `local-write-file-hooks'."
3071 (if erlang-check-module-name
3072 (let ((mn (erlang-get-module))
3073 (fn (erlang-get-module-from-file-name (buffer-file-name))))
3074 (if (and (stringp mn) (stringp fn))
3075 (or (string-equal mn fn)
3076 (if (or (eq erlang-check-module-name t)
3078 "Module does not match file name. Modify source? "))
3082 (goto-char (point-min))
3083 (if (re-search-forward
3085 (concat "^-module\\s *(\\s *\\(\\("
3087 "\\)?\\)\\s *)\\s *\\."))
3090 (goto-char (match-beginning 1))
3091 (delete-region (match-beginning 1)
3093 (insert fn))))))))))
3094 ;; Must return nil since it is added to `local-write-file-hook'.
3098 ;;; Electric functions.
3100 (defun erlang-electric-semicolon (&optional arg)
3101 "Insert a semicolon character and possibly a prototype for the next line.
3103 The variable `erlang-electric-semicolon-criteria' states a criterion,
3104 when fulfilled a newline is inserted, the next line is indented and a
3105 prototype for the next line is inserted. Normally the prototype
3106 consists of \" ->\". Should the semicolon end the clause a new clause
3107 header is generated.
3109 The variable `erlang-electric-semicolon-insert-blank-lines' controls
3110 the number of blank lines inserted between the current line and new
3113 Behaves just like the normal semicolon when supplied with a
3114 numerical arg, point is inside string or comment, or when there are
3115 non-whitespace characters following the point on the current line."
3117 (self-insert-command (prefix-numeric-value arg))
3119 (and (listp erlang-electric-commands)
3120 (not (memq 'erlang-electric-semicolon
3121 erlang-electric-commands)))
3123 (not (looking-at "\\s *\\(%.*\\)?$"))
3124 (null (erlang-test-criteria-list
3125 erlang-electric-semicolon-criteria)))
3126 (setq erlang-electric-newline-inhibit nil)
3127 (setq erlang-electric-newline-inhibit t)
3131 (if (condition-case nil
3132 (progn (erlang-indent-line) t)
3133 (error (if (bolp) (delete-backward-char 1))))
3139 (erlang-generate-new-clause)
3140 (if erlang-electric-semicolon-insert-blank-lines
3144 erlang-electric-semicolon-insert-blank-lines))))
3145 (error (if (bolp) (delete-backward-char 1))))))))
3148 (defun erlang-electric-comma (&optional arg)
3149 "Insert a comma character and possibly a new indented line.
3150 The variable `erlang-electric-comma-criteria' states a criterion,
3151 when fulfilled a newline is inserted and the next line is indented.
3153 Behaves just like the normal comma when supplied with a
3154 numerical arg, point is inside string or comment, or when there are
3155 non-whitespace characters following the point on the current line."
3158 (self-insert-command (prefix-numeric-value arg))
3161 (and (listp erlang-electric-commands)
3162 (not (memq 'erlang-electric-comma erlang-electric-commands)))
3164 (not (looking-at "\\s *\\(%.*\\)?$"))
3165 (null (erlang-test-criteria-list
3166 erlang-electric-comma-criteria)))
3167 (setq erlang-electric-newline-inhibit nil)
3168 (setq erlang-electric-newline-inhibit t)
3173 (erlang-indent-line)
3174 (error (if (bolp) (delete-backward-char 1))))))
3176 (defun erlang-electric-lt (&optional arg)
3177 "Insert a less-than sign, and optionally mark it as an open paren."
3181 (self-insert-command arg)
3183 ;; Was this the second char in bit-syntax open (`<<')?
3184 (unless (< (point) 2)
3187 (when (and (eq (char-after (point)) ?<)
3188 (not (eq (get-text-property (point) 'category)
3189 'bitsyntax-open-inner)))
3190 ;; Then mark the two chars...
3191 (put-text-property (point) (1+ (point))
3192 'category 'bitsyntax-open-outer)
3194 (put-text-property (point) (1+ (point))
3195 'category 'bitsyntax-open-inner)
3196 ;;...and unmark any subsequent less-than chars.
3198 (while (eq (char-after (point)) ?<)
3199 (remove-text-properties (point) (1+ (point))
3201 (forward-char 1))))))
3203 (defun erlang-after-bitsyntax-close ()
3204 "Return t if point is immediately after a bit-syntax close parenthesis (`>>')."
3208 (and (eq (char-after (point)) ?>)
3209 (not (eq (get-text-property (point) 'category)
3210 'bitsyntax-close-outer))))))
3212 (defun erlang-after-arrow ()
3213 "Return true if point is immediately after a function arrow (`->')."
3218 (eq (char-before (point)) ?-))
3219 (or (not (listp erlang-electric-commands))
3220 (memq 'erlang-electric-gt
3221 erlang-electric-commands))
3222 (not (erlang-in-literal))
3223 (looking-at "\\s *\\(%.*\\)?$")
3224 (erlang-test-criteria-list erlang-electric-arrow-criteria))))
3227 (defun erlang-electric-gt (&optional arg)
3228 "Insert a greater-than sign, and optionally mark it as a close paren."
3232 (self-insert-command arg)
3235 ;; Did we just write a bit-syntax close (`>>')?
3236 ((erlang-after-bitsyntax-close)
3238 ;; Then mark the two chars...
3240 (put-text-property (point) (1+ (point))
3241 'category 'bitsyntax-close-inner)
3243 (put-text-property (point) (1+ (point))
3244 'category 'bitsyntax-close-outer)
3245 ;;...and unmark any subsequent greater-than chars.
3247 (while (eq (char-after (point)) ?>)
3248 (remove-text-properties (point) (1+ (point))
3252 ;; Did we just write a function arrow (`->')?
3253 ((erlang-after-arrow)
3254 (let ((erlang-electric-newline-inhibit t))
3259 (erlang-indent-line)
3260 (error (if (bolp) (delete-backward-char 1))))))
3262 ;; Then it's just a plain greater-than.
3267 (defun erlang-electric-arrow\ off (&optional arg)
3268 "Insert a '>'-sign and possibly a new indented line.
3270 This command is only `electric' when the `>' is part of an `->' arrow.
3271 The variable `erlang-electric-arrow-criteria' states a sequence of
3272 criteria, which decides when a newline should be inserted and the next
3275 It behaves just like the normal greater than sign when supplied with a
3276 numerical arg, point is inside string or comment, or when there are
3277 non-whitespace characters following the point on the current line.
3279 After being split/merged into `erlang-after-arrow' and
3280 `erlang-electric-gt', it is now unused and disabled."
3282 (let ((prec (preceding-char)))
3283 (self-insert-command (prefix-numeric-value arg))
3285 (and (listp erlang-electric-commands)
3286 (not (memq 'erlang-electric-arrow
3287 erlang-electric-commands)))
3290 (not (looking-at "\\s *\\(%.*\\)?$"))
3291 (null (erlang-test-criteria-list
3292 erlang-electric-arrow-criteria)))
3293 (setq erlang-electric-newline-inhibit nil)
3294 (setq erlang-electric-newline-inhibit t)
3299 (erlang-indent-line)
3300 (error (if (bolp) (delete-backward-char 1)))))))
3303 (defun erlang-electric-newline (&optional arg)
3304 "Break line at point and indent, continuing comment if within one.
3305 The variable `erlang-electric-newline-criteria' states a criterion,
3306 when fulfilled a newline is inserted and the next line is indented.
3308 Should the current line begin with a comment, and the variable
3309 `comment-multi-line' be non-nil, a new comment start is inserted.
3311 Should the previous command be another electric command we assume that
3312 the user pressed newline out of old habit, hence we will do nothing."
3314 (cond ((and (not arg)
3315 erlang-electric-newline-inhibit
3316 (memq last-command erlang-electric-newline-inhibit-list))
3319 (and (listp erlang-electric-commands)
3320 (not (memq 'erlang-electric-newline
3321 erlang-electric-commands)))
3322 (null (erlang-test-criteria-list
3323 erlang-electric-newline-criteria)))
3324 (newline (prefix-numeric-value arg)))
3326 (if (and comment-multi-line
3329 (looking-at (concat "\\s *" comment-start-skip))))
3330 (let ((str (buffer-substring
3331 (or (match-end 1) (match-beginning 0))
3332 (min (match-end 0) (point)))))
3336 (newline-and-indent)))))
3339 (defun erlang-test-criteria-list (criteria)
3340 "Given a list of criterion functions, test if criteria are fulfilled.
3342 Each element in the criteria list can a function returning nil, t or
3343 the atom `stop'. t means that the criterion is fulfilled, `stop' means
3344 that it isn't fulfilled and that the search should stop,
3345 and nil means continue searching.
3347 Should the list contain the atom t the criterion is assumed to be
3348 fulfilled, unless preceded by a function returning `stop', of course.
3350 Should the argument be the atom t instead of a list, the criterion is
3351 assumed to be trivially true.
3353 Should all functions return nil, the criteria are assumed not to be
3356 Return t if criteria fulfilled, nil otherwise."
3361 (while (and criteria (null answer))
3362 (if (eq (car criteria) t)
3364 (setq answer (funcall (car criteria))))
3365 (setq criteria (cdr criteria)))
3366 (if (and answer (not (eq answer 'stop)))
3371 (defun erlang-in-literal (&optional lim)
3372 "Test if point is in string, quoted atom or comment.
3374 Return one of the three atoms `atom', `string', and `comment'.
3375 Should the point be inside none of the above mentioned types of
3376 context, nil is returned."
3378 (let* ((lim (or lim (save-excursion
3379 (erlang-beginning-of-clause)
3381 (state (if (fboundp 'syntax-ppss) ; post Emacs 21.3
3383 (parse-partial-sexp lim (point)))))
3385 ((eq (nth 3 state) ?') 'atom)
3386 ((nth 3 state) 'string)
3387 ((nth 4 state) 'comment)
3391 (defun erlang-at-end-of-function-p ()
3392 "Test if point is at end of an Erlang function.
3394 This function is designed to be a member of a criteria list."
3395 (eq (save-excursion (erlang-skip-blank) (point))
3397 (erlang-beginning-of-function -1) (point))))
3400 (defun erlang-at-end-of-clause-p ()
3401 "Test if point is at end of an Erlang clause.
3403 This function is designed to be a member of a criteria list."
3404 (eq (save-excursion (erlang-skip-blank) (point))
3406 (erlang-beginning-of-clause -1) (point))))
3409 (defun erlang-stop-when-inside-argument-list ()
3410 "Return `stop' if inside parenthesis list, nil otherwise.
3412 Knows about the list comprehension syntax. When the point is
3413 after `||', `stop' is not returned.
3415 This function is designed to be a member of a criteria list."
3418 (let ((orig-point (point))
3421 (if (not (eq (following-char) ?\[))
3423 ;; Do not return `stop' when inside a list comprehension
3424 ;; construction. (The point must be after `||').
3425 (while (< (point) orig-point)
3426 (setq state (erlang-partial-parse (point) orig-point state)))
3427 (if (and (car state) (eq (car (car (car state))) '||))
3434 (defun erlang-stop-when-at-guard ()
3435 "Return `stop' when at function guards.
3437 This function is designed to be a member of a criteria list."
3440 (if (and (looking-at (eval-when-compile
3441 (concat "^" erlang-atom-regexp "\\s *(")))
3444 (concat "^" erlang-atom-regexp ".*\\(->\\|:-\\)")))))
3449 (defun erlang-next-lines-empty-p ()
3450 "Return non-nil if next lines are empty.
3452 The variable `erlang-next-lines-empty-threshold' contains the number
3453 of lines required to be empty.
3455 A line containing only spaces and tabs is considered empty.
3457 This function is designed to be a member of a criteria list."
3458 (and erlang-next-lines-empty-threshold
3460 (let ((left erlang-next-lines-empty-threshold)
3462 (while (and cont (> left 0))
3464 (setq cont (looking-at "\\s *$"))
3465 (setq left (- left 1)))
3469 (defun erlang-at-keyword-end-p ()
3470 "Test if next readable token is the keyword end.
3472 This function is designed to be a member of a criteria list."
3475 (looking-at "end[^_a-zA-Z0-9]")))
3478 ;; Erlang tags support which is aware of erlang modules.
3480 ;; Not yet implemented under XEmacs. (Hint: The Emacs 19 etags
3481 ;; package works under XEmacs.)
3484 (if (or (featurep 'bytecomp)
3485 (featurep 'byte-compile))
3492 (defvar erlang-tags-function-alist
3493 '((find-tag . erlang-find-tag)
3494 (find-tag-other-window . erlang-find-tag-other-window)
3495 (find-tag-regexp . erlang-find-tag-regexp)
3496 (find-tag-other-frame . erlang-find-tag-other-frame))
3497 "Alist of old tags commands and the replacement functions.")
3499 (defvar erlang-tags-installed nil
3500 "Non-nil when the Erlang tags system is installed.")
3501 (defvar erlang-tags-file-list '()
3502 "List of files in tag list. Used when finding tag on form `module:'.")
3503 (defvar erlang-tags-completion-table nil
3504 "Like `tags-completion-table', this table contains `tag' and `module:tag'.")
3505 (defvar erlang-tags-buffer-installed-p nil
3506 "Non-nil when Erlang module recognising functions installed.")
3507 (defvar erlang-tags-buffer-list '()
3508 "Temporary list of buffers.")
3509 (defvar erlang-tags-orig-completion-table nil
3510 "Temporary storage for `tags-completion-table'.")
3511 (defvar erlang-tags-orig-tag-order nil
3512 "Temporary storage for `find-tag-tag-order'.")
3513 (defvar erlang-tags-orig-regexp-tag-order nil
3514 "Temporary storage for `find-tag-regexp-tag-order'.")
3515 (defvar erlang-tags-orig-search-function nil
3516 "Temporary storage for `find-tag-search-function'.")
3517 (defvar erlang-tags-orig-regexp-search-function nil
3518 "Temporary storage for `find-tag-regexp-search-function'.")
3519 (defvar erlang-tags-orig-format-hooks nil
3520 "Temporary storage for `tags-table-format-hooks'.") ;v19
3521 (defvar erlang-tags-orig-format-functions nil
3522 "Temporary storage for `tags-table-format-functions'.") ;v > 19
3524 (defun erlang-tags-init ()
3525 "Install an alternate version of tags, aware of Erlang modules.
3527 After calling this function, the tags functions are aware of
3528 Erlang modules. Tags can be entered on the for `module:tag' as well
3529 as on the old form `tag'.
3531 In the completion list, `module:tag' and `module:' shows up.
3533 Call this function from an appropriate init file, or add it to
3534 Erlang mode hook with the commands:
3535 (add-hook 'erlang-mode-hook 'erlang-tags-init)
3536 (add-hook 'erlang-shell-mode-hook 'erlang-tags-init)
3538 This function only works under Emacs 18 and Emacs 19. Currently, It
3539 is not implemented under XEmacs. (Hint: The Emacs 19 etags module
3540 works under XEmacs.)"
3542 (cond ((= erlang-emacs-major-version 18)
3544 (erlang-tags-define-keys (current-local-map))
3545 (setq erlang-tags-installed t))
3548 ;; Test on a function available in the Emacs 19 version
3549 ;; of tags but not in the XEmacs version.
3550 (if (not (fboundp 'find-tag-noselect))
3552 (erlang-tags-define-keys (current-local-map))
3553 (setq erlang-tags-installed t)))))
3556 ;; Set all keys bound to `find-tag' et.al. in the global map and the
3557 ;; menu to `erlang-find-tag' et.al. in `map'.
3559 ;; The function `substitute-key-definition' does not work properly
3560 ;; in all version of Emacs.
3562 (defun erlang-tags-define-keys (map)
3563 "Bind tags commands to keymap MAP aware of Erlang modules."
3564 (let ((alist erlang-tags-function-alist))
3566 (let* ((old (car (car alist)))
3567 (new (cdr (car alist)))
3568 (keys (append (where-is-internal old global-map))))
3570 (define-key map (car keys) new)
3571 (setq keys (cdr keys))))
3572 (setq alist (cdr alist))))
3574 (erlang-menu-substitute erlang-menu-base-items erlang-tags-function-alist)
3578 ;; There exists a variable `find-tag-default-function'. It is not used
3579 ;; since `complete-tag' uses it to get current word under point. In that
3580 ;; situation we don't want the module to be prepended.
3582 (defun erlang-find-tag-default ()
3583 "Return the default tag.
3584 Search `-import' list of imported functions.
3585 Single quotes are been stripped away."
3586 (let ((mod-func (erlang-get-function-under-point)))
3587 (cond ((null mod-func)
3589 ((null (car mod-func))
3592 (concat (car mod-func) ":" (nth 1 mod-func))))))
3595 ;; Return `t' since it is used inside `tags-loop-form'.
3597 (defun erlang-find-tag (modtagname &optional next-p regexp-p)
3598 "Like `find-tag'. Capable of retrieving Erlang modules.
3600 Tags can be given on the forms `tag', `module:', `module:tag'."
3601 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3602 (switch-to-buffer (erlang-find-tag-noselect modtagname next-p regexp-p))
3606 ;; Code mainly from `find-tag-other-window' in `etags.el'.
3608 (defun erlang-find-tag-other-window (tagname &optional next-p regexp-p)
3609 "Like `find-tag-other-window' but aware of Erlang modules."
3610 (interactive (erlang-tag-interactive
3611 "Find `module:tag' or `tag' other window: "))
3613 ;; This is to deal with the case where the tag is found in the
3614 ;; selected window's buffer; without this, point is moved in both
3615 ;; windows. To prevent this, we save the selected window's point
3616 ;; before doing find-tag-noselect, and restore it afterwards.
3617 (let* ((window-point (window-point (selected-window)))
3618 (tagbuf (erlang-find-tag-noselect tagname next-p regexp-p))
3619 (tagpoint (progn (set-buffer tagbuf) (point))))
3620 (set-window-point (prog1
3622 (switch-to-buffer-other-window tagbuf)
3623 ;; We have to set this new window's point; it
3624 ;; might already have been displaying a
3625 ;; different portion of tagbuf, in which case
3626 ;; switch-to-buffer-other-window doesn't set
3627 ;; the window's point from the buffer.
3628 (set-window-point (selected-window) tagpoint))
3632 (defun erlang-find-tag-other-frame (tagname &optional next-p)
3633 "Like `find-tag-other-frame' but aware of Erlang modules."
3634 (interactive (erlang-tag-interactive
3635 "Find `module:tag' or `tag' other frame: "))
3636 (let ((pop-up-frames t))
3637 (erlang-find-tag-other-window tagname next-p)))
3640 (defun erlang-find-tag-regexp (regexp &optional next-p other-window)
3641 "Like `find-tag-regexp' but aware of Erlang modules."
3642 (interactive (if (fboundp 'find-tag-regexp)
3643 (erlang-tag-interactive
3644 "Find `module:regexp' or `regexp': ")
3645 (error "This version of Emacs can't find tags by regexps")))
3646 (funcall (if other-window
3647 'erlang-find-tag-other-window
3652 ;; Just like C-u M-. This could be added to the menu.
3653 (defun erlang-find-next-tag ()
3654 "Find next tag, like \\[find-tag] with prefix arg."
3656 (let ((current-prefix-arg '(4)))
3657 (if erlang-tags-installed
3658 (call-interactively 'erlang-find-tag)
3659 (call-interactively 'find-tag))))
3662 ;; Mimics `find-tag-noselect' found in `etags.el', but uses `find-tag' to
3663 ;; be compatible with `tags.el'.
3665 ;; Handles three cases:
3666 ;; * `module:' Loop over all possible file names. Stop if a file-name
3667 ;; without extension and directory matches the module.
3670 ;; Emacs 19: Replace test functions with functions aware of
3671 ;; Erlang modules. Tricky because the etags system wasn't
3672 ;; built for these kind of operations...
3674 ;; Emacs 18: We loop over `find-tag' until we find a file
3675 ;; whose module matches the requested module. The
3676 ;; drawback is that a lot of files could be loaded into
3679 ;; * `tag' Just give it to `find-tag'.
3681 (defun erlang-find-tag-noselect (modtagname &optional next-p regexp-p)
3682 "Like `find-tag-noselect' but aware of Erlang modules."
3683 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3685 (setq modtagname (symbol-value 'last-tag)))
3686 (funcall (symbol-function 'set) 'last-tag modtagname)
3687 ;; `tags.el' uses this variable to record how M-, would
3688 ;; know where to restart a tags command.
3689 (if (boundp 'tags-loop-form)
3690 (funcall (symbol-function 'set)
3691 'tags-loop-form '(erlang-find-tag nil t)))
3692 (save-window-excursion
3694 ((string-match ":$" modtagname)
3695 ;; Only the module name was given. Read all files whose file name
3697 (let ((modname (substring modtagname 0 (match-beginning 0)))
3701 (visit-tags-table-buffer)
3702 (setq erlang-tags-file-list
3703 (funcall (symbol-function 'tags-table-files)))))
3705 (or erlang-tags-file-list
3707 (if (and (featurep 'etags)
3709 (symbol-function 'visit-tags-table-buffer) 'same)
3711 (symbol-function 'visit-tags-table-buffer) t))
3712 (setq erlang-tags-file-list
3713 (funcall (symbol-function 'tags-table-files)))
3714 (error "No %stags containing %s" (if next-p "more " "")
3716 (if erlang-tags-file-list
3717 (let ((this-module (erlang-get-module-from-file-name
3718 (car erlang-tags-file-list))))
3719 (if (and (stringp this-module)
3720 (string= modname this-module))
3721 (setq file (car erlang-tags-file-list)))
3722 (setq erlang-tags-file-list (cdr erlang-tags-file-list)))))
3723 (set-buffer (or (get-file-buffer file)
3724 (find-file-noselect file)))))
3726 ((string-match ":" modtagname)
3727 (if (boundp 'find-tag-tag-order)
3728 ;; Method one: Add module-recognising functions to the
3729 ;; list of order functions. However, the tags system
3730 ;; from Emacs 18, and derives thereof (read: XEmacs)
3731 ;; hasn't got this feature.
3733 (erlang-tags-install-module-check)
3735 (funcall (symbol-function 'find-tag)
3736 modtagname next-p regexp-p)
3737 (erlang-tags-remove-module-check)))
3738 ;; Method two: Call the tags system until a file matching
3739 ;; the module is found. This could result in that many
3740 ;; files are read. (e.g. The tag "foo:file" will take a
3741 ;; while to process.)
3742 (let* ((modname (substring modtagname 0 (match-beginning 0)))
3743 (tagname (substring modtagname (match-end 0) nil))
3748 (funcall (symbol-function 'find-tag) tagname next-p regexp-p)
3750 ;; Determine the module form the file name. (The
3751 ;; alternative, to check `-module', would make this
3752 ;; code useless for non-Erlang programs.)
3753 (setq file (erlang-get-module-from-file-name buffer-file-name))
3754 (not (and (stringp file)
3755 (string= modname file))))))))
3757 (funcall (symbol-function 'find-tag) modtagname next-p regexp-p)))
3758 (current-buffer))) ; Return the new buffer.
3761 ;; Process interactive arguments for erlang-find-tag-*.
3763 ;; Negative arguments work only for `etags', not `tags'. This is not
3764 ;; a problem since negative arguments means step back into the
3765 ;; history list, a feature not implemented in `tags'.
3767 (defun erlang-tag-interactive (prompt)
3772 (if current-prefix-arg
3773 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
3776 (let* ((default (erlang-find-tag-default))
3778 (format "%s(default %s) " prompt default)
3780 (spec (if (featurep 'etags)
3781 (completing-read prompt 'erlang-tags-complete-tag)
3782 (read-string prompt))))
3783 (list (if (equal spec "")
3784 (or default (error "There is no default tag"))
3788 ;; Search tag functions which are aware of Erlang modules. The tactic
3789 ;; is to store new search functions into the local variables of the
3790 ;; TAGS buffers. The variables are restored directly after the
3791 ;; search. The situation is complicated by the fact that new TAGS
3792 ;; files can be loaded during the search.
3795 (defun erlang-tags-install-module-check ()
3796 "Install our own tag search functions."
3797 ;; Make sure our functions are installed in TAGS files loaded
3798 ;; into Emacs while searching.
3800 ((>= erlang-emacs-major-version 20)
3801 (setq erlang-tags-orig-format-functions
3802 (symbol-value 'tags-table-format-functions))
3803 (funcall (symbol-function 'set) 'tags-table-format-functions
3804 (cons 'erlang-tags-recognize-tags-table
3805 erlang-tags-orig-format-functions))
3806 (setq erlang-tags-buffer-list '())
3809 (setq erlang-tags-orig-format-hooks
3810 (symbol-value 'tags-table-format-hooks)))
3811 (funcall (symbol-function 'set) 'tags-table-format-hooks
3812 (cons 'erlang-tags-recognize-tags-table
3813 erlang-tags-orig-format-hooks))
3814 (setq erlang-tags-buffer-list '())
3817 ;; Install our functions in the TAGS files already resident.
3819 (let ((files (symbol-value 'tags-table-computed-list)))
3821 (if (stringp (car files))
3822 (if (get-file-buffer (car files))
3824 (set-buffer (get-file-buffer (car files)))
3825 (erlang-tags-install-local))))
3826 (setq files (cdr files))))))
3829 (defun erlang-tags-install-local ()
3830 "Install our tag search functions in current buffer."
3831 (if erlang-tags-buffer-installed-p
3833 ;; Mark this buffer as "installed" and record.
3834 (set (make-local-variable 'erlang-tags-buffer-installed-p) t)
3835 (setq erlang-tags-buffer-list
3836 (cons (current-buffer) erlang-tags-buffer-list))
3838 ;; Save the original values.
3839 (set (make-local-variable 'erlang-tags-orig-tag-order)
3840 (symbol-value 'find-tag-tag-order))
3841 (set (make-local-variable 'erlang-tags-orig-regexp-tag-order)
3842 (symbol-value 'find-tag-regexp-tag-order))
3843 (set (make-local-variable 'erlang-tags-orig-search-function)
3844 (symbol-value 'find-tag-search-function))
3845 (set (make-local-variable 'erlang-tags-orig-regexp-search-function)
3846 (symbol-value 'find-tag-regexp-search-function))
3848 ;; Install our own functions.
3849 (set (make-local-variable 'find-tag-search-function)
3850 'erlang-tags-search-forward)
3851 (set (make-local-variable 'find-tag-regexp-search-function)
3852 'erlang-tags-regexp-search-forward)
3853 (set (make-local-variable 'find-tag-tag-order)
3854 '(erlang-tag-match-module-p))
3855 (set (make-local-variable 'find-tag-regexp-tag-order)
3856 '(erlang-tag-match-module-regexp-p))))
3859 (defun erlang-tags-remove-module-check ()
3860 "Remove our own tags search functions."
3862 ((>= erlang-emacs-major-version 20)
3863 (funcall (symbol-function 'set)
3864 'tags-table-format-functions
3865 erlang-tags-orig-format-functions)
3868 (funcall (symbol-function 'set)
3869 'tags-table-format-hooks
3870 erlang-tags-orig-format-hooks)
3873 ;; Remove our functions from the TAGS files. (Note that
3874 ;; `tags-table-computed-list' need not be the same list as when
3875 ;; the search was started.)
3877 (let ((buffers erlang-tags-buffer-list))
3879 (if (buffer-name (car buffers))
3881 (set-buffer (car buffers))
3882 (erlang-tags-remove-local)))
3883 (setq buffers (cdr buffers))))))
3886 (defun erlang-tags-remove-local ()
3887 "Remove our tag search functions from current buffer."
3888 (if (null erlang-tags-buffer-installed-p)
3890 (funcall (symbol-function 'set) 'erlang-tags-buffer-installed-p nil)
3891 (funcall (symbol-function 'set)
3892 'find-tag-tag-order erlang-tags-orig-tag-order)
3893 (funcall (symbol-function 'set)
3894 'find-tag-regexp-tag-order erlang-tags-orig-regexp-tag-order)
3895 (funcall (symbol-function 'set)
3896 'find-tag-search-function erlang-tags-orig-search-function)
3897 (funcall (symbol-function 'set)
3898 'find-tag-regexp-search-function
3899 erlang-tags-orig-regexp-search-function)))
3902 (defun erlang-tags-recognize-tags-table ()
3903 "Install our functions in all loaded TAGS files.
3905 This function is added to `tags-table-format-hooks/functions' when searching
3906 for a tag on the form `module:tag'."
3907 (if (null (funcall (symbol-function 'etags-recognize-tags-table)))
3909 (erlang-tags-install-local)
3913 (defun erlang-tags-search-forward (tag &optional bound noerror count)
3914 "Forward search function, aware of Erlang module prefix."
3915 (if (string-match ":" tag)
3916 (setq tag (substring tag (match-end 0) nil)))
3917 ;; Avoid unintended recursion.
3918 (if (eq erlang-tags-orig-search-function 'erlang-tags-search-forward)
3919 (search-forward tag bound noerror count)
3920 (funcall erlang-tags-orig-search-function tag bound noerror count)))
3923 (defun erlang-tags-regexp-search-forward (tag &optional bound noerror count)
3924 "Forward regexp search function, aware of Erlang module prefix."
3925 (if (string-match ":" tag)
3926 (setq tag (substring tag (match-end 0) nil)))
3927 (if (eq erlang-tags-orig-regexp-search-function
3928 'erlang-tags-regexp-search-forward)
3929 (re-search-forward tag bound noerror count)
3930 (funcall erlang-tags-orig-regexp-search-function
3931 tag bound noerror count)))
3934 ;; t if point is at a tag line that matches TAG, containing
3935 ;; module information. Assumes that all other order functions
3936 ;; are stored in `erlang-tags-orig-[regex]-tag-order'.
3938 (defun erlang-tag-match-module-p (tag)
3939 (erlang-tag-match-module-common-p tag erlang-tags-orig-tag-order))
3941 (defun erlang-tag-match-module-regexp-p (tag)
3942 (erlang-tag-match-module-common-p tag erlang-tags-orig-regexp-tag-order))
3944 (defun erlang-tag-match-module-common-p (tag order)
3947 (if (string-match ":" tag)
3949 (setq mod (substring tag 0 (match-beginning 0)))
3950 (setq tag (substring tag (match-end 0) nil))))
3951 (while (and order (not found))
3953 (and (not (memq (car order)
3954 '(erlang-tag-match-module-p
3955 erlang-tag-match-module-regexp-p)))
3956 (funcall (car order) tag)))
3957 (setq order (cdr order)))
3960 (string= mod (erlang-get-module-from-file-name
3964 ;;; Tags completion, Emacs 19 `etags' specific.
3966 ;;; The basic idea is to create a second completion table `erlang-tags-
3967 ;;; completion-table' containing all normal tags plus tags on the form
3971 (defun erlang-complete-tag ()
3972 "Perform tags completion on the text around point.
3973 Completes to the set of names listed in the current tags table.
3975 Should the Erlang tags system be installed this command knows
3976 about Erlang modules."
3981 (cond ((and erlang-tags-installed
3982 (fboundp 'complete-tag)) ; Emacs 19
3983 (let ((orig-tags-complete-tag (symbol-function 'tags-complete-tag)))
3984 (fset 'tags-complete-tag
3985 (symbol-function 'erlang-tags-complete-tag))
3987 (funcall (symbol-function 'complete-tag))
3988 (fset 'tags-complete-tag orig-tags-complete-tag))))
3989 ((fboundp 'complete-tag) ; Emacs 19
3990 (funcall (symbol-function 'complete-tag)))
3991 ((fboundp 'tag-complete-symbol) ; XEmacs
3992 (funcall (symbol-function 'tag-complete-symbol)))
3994 (error "This version of Emacs can't complete tags"))))
3997 ;; Based on `tags-complete-tag', but this one uses
3998 ;; `erlang-tags-completion-table' instead of `tags-completion-table'.
4000 ;; This is the entry-point called by system function `completing-read'.
4001 (defun erlang-tags-complete-tag (string predicate what)
4003 ;; If we need to ask for the tag table, allow that.
4004 (let ((enable-recursive-minibuffers t))
4005 (visit-tags-table-buffer))
4007 (all-completions string (erlang-tags-completion-table) predicate)
4008 (try-completion string (erlang-tags-completion-table) predicate))))
4011 ;; `tags-completion-table' calls itself recursively, make it
4012 ;; call our own wedge instead. Note that the recursive call
4013 ;; is very rare; it only occurs when a tags-file contains
4014 ;; `include'-statements.
4015 (defun erlang-tags-completion-table ()
4016 "Build completion table. Tags on the form `tag' or `module:tag'."
4017 (setq erlang-tags-orig-completion-table
4018 (symbol-function 'tags-completion-table))
4019 (fset 'tags-completion-table
4020 (symbol-function 'erlang-tags-completion-table-1))
4022 (erlang-tags-completion-table-1)
4023 (fset 'tags-completion-table
4024 erlang-tags-orig-completion-table)))
4027 (defun erlang-tags-completion-table-1 ()
4028 (make-local-variable 'erlang-tags-completion-table)
4029 (or erlang-tags-completion-table
4030 (let ((tags-completion-table nil)
4031 (tags-completion-table-function
4032 'erlang-etags-tags-completion-table))
4033 (funcall erlang-tags-orig-completion-table)
4034 (setq erlang-tags-completion-table tags-completion-table))))
4037 ;; Based on `etags-tags-completion-table'. The difference is that we
4038 ;; add three symbols to the vector, the tag, module: and module:tag.
4039 ;; The module is extracted from the file name of a tag. (This one
4040 ;; only works if we are looking at an `etags' file. However, this is
4041 ;; the only format supported by Emacs, so far.)
4042 (defun erlang-etags-tags-completion-table ()
4043 (let ((table (make-vector 511 0))
4046 (goto-char (point-min))
4047 ;; This monster regexp matches an etags tag line.
4048 ;; \1 is the string to match;
4049 ;; \2 is not interesting;
4050 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
4051 ;; \4 is not interesting;
4052 ;; \5 is the explicitly-specified tag name.
4053 ;; \6 is the line to start searching at;
4054 ;; \7 is the char to start searching at.
4057 (eq (following-char) ?\f)
4058 (looking-at "\f\n\\([^,\n]*\\),.*\n"))
4059 (setq file (buffer-substring
4060 (match-beginning 1) (match-end 1)))
4061 (goto-char (match-end 0)))
4064 ^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
4065 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
4066 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
4068 (let ((tag (if (match-beginning 5)
4069 ;; There is an explicit tag name.
4070 (buffer-substring (match-beginning 5) (match-end 5))
4071 ;; No explicit tag name. Best guess.
4072 (buffer-substring (match-beginning 3) (match-end 3))))
4074 (erlang-get-module-from-file-name file))))
4076 (if (stringp module)
4078 (intern (concat module ":" tag) table)
4079 ;; Only the first one will be stored in the table.
4080 (intern (concat module ":") table))))))
4084 ;;; Prepare for other methods to run an Erlang slave process.
4087 (defvar erlang-shell-function 'inferior-erlang
4088 "Command to execute start a new Erlang shell.
4090 Change this variable to use your favorite
4091 Erlang compilation package.")
4093 (defvar erlang-shell-display-function 'inferior-erlang-run-or-select
4094 "Command to execute to display Erlang shell.
4096 Change this variable to use your favorite
4097 Erlang compilation package.")
4099 (defvar erlang-compile-function 'inferior-erlang-compile
4100 "Command to execute to compile current buffer.
4102 Change this variable to use your favorite
4103 Erlang compilation package.")
4105 (defvar erlang-compile-display-function 'inferior-erlang-run-or-select
4106 "Command to execute to view last compilation.
4108 Change this variable to use your favorite
4109 Erlang compilation package.")
4111 (defvar erlang-next-error-function 'inferior-erlang-next-error
4112 "Command to execute to go to the next error.
4114 Change this variable to use your favorite Erlang compilation
4115 package. Not used in Emacs 21.")
4119 (defun erlang-shell ()
4120 "Start a new Erlang shell.
4122 The variable `erlang-shell-function' decides which method to use,
4123 default is to start a new Erlang host. It is possible that, in the
4124 future, a new shell on an already running host will be started."
4126 (call-interactively erlang-shell-function))
4129 ;;;###autoload (autoload 'run-erlang "erlang" "Start a new Erlang shell." t)
4131 ;; It is customary for Emacs packages to supply a function on this
4132 ;; form, even though it violates the `erlang-*' name convention.
4133 (defalias 'run-erlang 'erlang-shell)
4136 (defun erlang-shell-display ()
4137 "Display an Erlang shell, or start a new."
4139 (call-interactively erlang-shell-display-function))
4143 (defun erlang-compile ()
4144 "Compile Erlang module in current buffer."
4146 (call-interactively erlang-compile-function))
4149 (defun erlang-compile-display ()
4150 "Display compilation output."
4152 (call-interactively erlang-compile-display-function))
4155 (defun erlang-next-error ()
4156 "Display next error message from the latest compilation."
4158 (call-interactively erlang-next-error-function))
4163 ;;; Erlang Shell Mode -- Major mode used for Erlang shells.
4166 ;; This mode is designed to be implementation independent,
4167 ;; e.g. it does not assume that we are running an inferior
4168 ;; Erlang, there exists a lot of other possibilities.
4171 (defvar erlang-shell-buffer-name "*erlang*"
4172 "The name of the Erlang link shell buffer.")
4175 (defvar erlang-shell-mode-map nil
4176 "Keymap used by Erlang shells.")
4179 (defvar erlang-shell-mode-hook nil
4180 "*User functions to run when an Erlang shell is started.
4182 This hook is used to change the behaviour of Erlang mode. It is
4183 normally used by the user to personalise the programming environment.
4184 When used in a site init file, it could be used to customise Erlang
4185 mode for all users on the system.
4187 The function added to this hook is run every time a new Erlang
4190 See also `erlang-load-hook', a hook which is run once, when Erlang
4191 mode is loaded, and `erlang-mode-hook' which is run every time a new
4192 Erlang source file is loaded into Emacs.")
4195 (defvar erlang-input-ring-file-name "~/.erlang_history"
4196 "*When non-nil, file name used to store Erlang shell history information.")
4199 (defun erlang-shell-mode ()
4200 "Major mode for interacting with an Erlang shell.
4202 We assume that we already are in Comint mode.
4204 The following special commands are available:
4205 \\{erlang-shell-mode-map}"
4207 (setq major-mode 'erlang-shell-mode)
4208 (setq mode-name "Erlang Shell")
4209 (erlang-mode-variables)
4210 (if erlang-shell-mode-map
4212 (setq erlang-shell-mode-map (copy-keymap comint-mode-map))
4213 (erlang-shell-mode-commands erlang-shell-mode-map))
4214 (use-local-map erlang-shell-mode-map)
4215 (unless inferior-erlang-use-cmm
4216 ;; This was originally not a marker, but it needs to be, at least
4217 ;; in Emacs 21, and should be backwards-compatible. Otherwise,
4218 ;; would need to test whether compilation-parsing-end is a marker
4219 ;; after requiring `compile'.
4220 (set (make-local-variable 'compilation-parsing-end) (copy-marker 1))
4221 (set (make-local-variable 'compilation-error-list) nil)
4222 (set (make-local-variable 'compilation-old-error-list) nil))
4223 ;; Needed when compiling directly from the Erlang shell.
4224 (setq compilation-last-buffer (current-buffer))
4225 (erlang-add-compilation-alist erlang-error-regexp-alist)
4226 (setq comint-prompt-regexp "^[^>=]*> *")
4227 (setq comint-eol-on-send t)
4228 (setq comint-input-ignoredups t)
4229 (setq comint-scroll-show-maximum-output t)
4230 (setq comint-scroll-to-bottom-on-output t)
4231 ;; In Emacs 19.30, `add-hook' has got a `local' flag, use it. If
4232 ;; the call fails, just call the normal `add-hook'.
4235 (make-local-hook 'comint-output-filter-functions) ; obsolete after Emacs 21.3
4236 (add-hook 'comint-output-filter-functions
4237 'inferior-erlang-strip-delete nil t)
4238 (add-hook 'comint-output-filter-functions
4239 'inferior-erlang-strip-ctrl-m nil t))
4241 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-delete)
4242 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-ctrl-m)))
4243 ;; Some older versions of comint don't have an input ring.
4244 (if (fboundp 'comint-read-input-ring)
4246 (setq comint-input-ring-file-name erlang-input-ring-file-name)
4247 (comint-read-input-ring t)
4248 (make-local-variable 'kill-buffer-hook)
4249 (add-hook 'kill-buffer-hook 'comint-write-input-ring)))
4250 ;; At least in Emacs 21, we need to be in `compilation-minor-mode'
4251 ;; for `next-error' to work. We can avoid it clobbering the shell
4253 (when inferior-erlang-use-cmm
4254 (compilation-minor-mode 1)
4255 (set (make-local-variable 'minor-mode-overriding-map-alist)
4256 `((compilation-minor-mode
4257 . ,(let ((map (make-sparse-keymap)))
4258 ;; It would be useful to put keymap properties on the
4259 ;; error lines so that we could use RET and mouse-2
4260 ;; on them directly.
4261 (when (boundp 'compilation-skip-threshold) ; new compile.el
4262 (define-key map [mouse-2] #'erlang-mouse-2-command)
4263 (define-key map "\C-m" #'erlang-RET-command))
4264 (if (boundp 'compilation-menu-map)
4265 (define-key map [menu-bar compilation]
4266 (cons "Errors" compilation-menu-map)))
4268 (run-hooks 'erlang-shell-mode-hook))
4271 (defun erlang-mouse-2-command (event)
4272 "Command bound to `mouse-2' in inferior Erlang buffer.
4273 Selects Comint or Compilation mode command as appropriate."
4275 (if (save-window-excursion
4277 (mouse-set-point event)
4278 (consp (get-text-property (line-beginning-position) 'message))))
4279 (call-interactively (lookup-key compilation-mode-map [mouse-2]))
4280 (call-interactively (lookup-key comint-mode-map [mouse-2]))))
4282 (defun erlang-RET-command ()
4283 "Command bound to `RET' in inferior Erlang buffer.
4284 Selects Comint or Compilation mode command as appropriate."
4286 (if (consp (get-text-property (line-beginning-position) 'message))
4287 (call-interactively (lookup-key compilation-mode-map "\C-m"))
4288 (call-interactively (lookup-key comint-mode-map "\C-m"))))
4290 (defun erlang-shell-mode-commands (map)
4291 (define-key map "\M-\t" 'erlang-complete-tag)
4292 (define-key map "\C-a" 'comint-bol) ; Normally the other way around.
4293 (define-key map "\C-c\C-a" 'beginning-of-line)
4294 (define-key map "\C-d" nil) ; Was `comint-delchar-or-maybe-eof'
4295 (define-key map "\M-\C-m" 'compile-goto-error)
4296 (unless inferior-erlang-use-cmm
4297 (define-key map "\C-x`" 'erlang-next-error)))
4300 ;;; Inferior Erlang -- Run an Erlang shell as a subprocess.
4303 (defvar inferior-erlang-display-buffer-any-frame nil
4304 "*When nil, `inferior-erlang-display-buffer' use only selected frame.
4305 When t, all frames are searched. When 'raise, the frame is raised.")
4307 (defvar inferior-erlang-shell-type 'newshell
4308 "The type of Erlang shell to use.
4310 When this variable is set to the atom `oldshell', the old shell is used.
4311 When set to `newshell' the new shell is used. Should the variable be
4312 nil, the default shell is used.
4314 This variable influence the setting of other variables.")
4316 (defvar inferior-erlang-machine "erl"
4317 "*The name of the Erlang shell.")
4319 (defvar inferior-erlang-machine-options '()
4320 "*The options used when activating the Erlang shell.
4322 This must be a list of strings.")
4324 (defvar inferior-erlang-process-name "inferior-erlang"
4325 "The name of the inferior Erlang process.")
4327 (defvar inferior-erlang-buffer-name erlang-shell-buffer-name
4328 "The name of the inferior Erlang buffer.")
4330 (defvar inferior-erlang-prompt-timeout 60
4331 "*Number of seconds before `inferior-erlang-wait-prompt' timeouts.
4333 The time specified is waited after every output made by the inferior
4334 Erlang shell. When this variable is t, we assume that we always have
4335 a prompt. When nil, we will wait forever, or until \\[keyboard-quit].")
4337 (defvar inferior-erlang-process nil
4338 "Process of last invoked inferior Erlang, or nil.")
4340 (defvar inferior-erlang-buffer nil
4341 "Buffer of last invoked inferior Erlang, or nil.")
4344 (defun inferior-erlang ()
4345 "Run an inferior Erlang.
4347 This is just like running Erlang in a normal shell, except that
4348 an Emacs buffer is used for input and output.
4350 The command line history can be accessed with \\[comint-previous-input] and \\[comint-next-input].
4351 The history is saved between sessions.
4353 Entry to this mode calls the functions in the variables
4354 `comint-mode-hook' and `erlang-shell-mode-hook' with no arguments.
4356 The following commands imitate the usual Unix interrupt and
4357 editing control characters:
4358 \\{erlang-shell-mode-map}"
4361 (let ((opts inferior-erlang-machine-options))
4362 (cond ((eq inferior-erlang-shell-type 'oldshell)
4363 (setq opts (cons "-oldshell" opts)))
4364 ((eq inferior-erlang-shell-type 'newshell)
4365 (setq opts (append '("-newshell" "-env" "TERM" "vt100") opts))))
4366 (setq inferior-erlang-buffer
4368 inferior-erlang-process-name inferior-erlang-machine
4370 (setq inferior-erlang-process
4371 (get-buffer-process inferior-erlang-buffer))
4372 (process-kill-without-query inferior-erlang-process)
4373 (switch-to-buffer inferior-erlang-buffer)
4374 (if (and (not (eq system-type 'windows-nt))
4375 (eq inferior-erlang-shell-type 'newshell))
4376 (setq comint-process-echoes t))
4377 ;; `rename-buffer' takes only one argument in Emacs 18.
4379 (rename-buffer inferior-erlang-buffer-name t)
4380 (error (rename-buffer inferior-erlang-buffer-name)))
4381 (erlang-shell-mode))
4384 (defun inferior-erlang-run-or-select ()
4385 "Switch to an inferior Erlang buffer, possibly starting new process."
4387 (if (null (inferior-erlang-running-p))
4389 (inferior-erlang-display-buffer t)))
4392 (defun inferior-erlang-display-buffer (&optional select)
4393 "Make the inferior Erlang process visible.
4394 The window is returned.
4396 Should `inferior-erlang-display-buffer-any-frame' be nil the buffer is
4397 displayed in the current frame. Should it be non-nil, and the buffer
4398 already is visible in any other frame, no new window will be created.
4399 Should it be the atom 'raise, the frame containing the window will
4402 Should the optional argument SELECT be non-nil, the window is
4403 selected. Should the window be in another frame, that frame is raised.
4405 Note, should the mouse pointer be places outside the raised frame, that
4406 frame will become deselected before the next command."
4408 (or (inferior-erlang-running-p)
4409 (error "No inferior Erlang process is running"))
4410 (let ((win (inferior-erlang-window
4411 inferior-erlang-display-buffer-any-frame))
4412 (frames-p (fboundp 'selected-frame)))
4414 (let ((old-win (selected-window)))
4416 (switch-to-buffer-other-window inferior-erlang-buffer)
4417 (setq win (selected-window)))
4418 (select-window old-win))
4419 (if (and window-system
4422 (eq inferior-erlang-display-buffer-any-frame 'raise))
4423 (not (eq (selected-frame) (window-frame win))))
4424 (raise-frame (window-frame win))))
4426 (select-window win))
4431 (defun inferior-erlang-running-p ()
4432 "Non-nil when an inferior Erlang is running."
4433 (and inferior-erlang-process
4434 (memq (process-status inferior-erlang-process) '(run open))
4435 inferior-erlang-buffer
4436 (buffer-name inferior-erlang-buffer)))
4439 (defun inferior-erlang-window (&optional all-frames)
4440 "Return the window containing the inferior Erlang, or nil."
4441 (and (inferior-erlang-running-p)
4442 (if (and all-frames (>= erlang-emacs-major-version 19))
4443 (get-buffer-window inferior-erlang-buffer t)
4444 (get-buffer-window inferior-erlang-buffer))))
4447 (defun inferior-erlang-wait-prompt ()
4448 "Wait until the inferior Erlang shell prompt appears."
4449 (if (eq inferior-erlang-prompt-timeout t)
4451 (or (inferior-erlang-running-p)
4452 (error "No inferior Erlang shell is running"))
4454 (set-buffer inferior-erlang-buffer)
4456 (while (save-excursion
4457 (goto-char (process-mark inferior-erlang-process))
4459 (not (looking-at comint-prompt-regexp)))
4463 (message "Waiting for Erlang shell prompt (press C-g to abort)."))
4464 (or (accept-process-output inferior-erlang-process
4465 inferior-erlang-prompt-timeout)
4466 (error "No Erlang shell prompt before timeout")))
4467 (if msg (message ""))))))
4469 (autoload 'comint-send-input "comint")
4471 (defun inferior-erlang-send-command (cmd &optional hist)
4472 "Send command CMD to the inferior Erlang.
4474 The contents of the current command line (if any) will
4475 be placed at the next prompt.
4477 If optional second argument is non-nil the command is inserted into
4480 Return the position after the newly inserted command."
4481 (or (inferior-erlang-running-p)
4482 (error "No inferior Erlang process is running"))
4483 (let ((old-buffer (current-buffer))
4484 (insert-point (marker-position (process-mark inferior-erlang-process)))
4485 (insert-length (if comint-process-echoes
4487 (1+ (length cmd)))))
4488 (set-buffer inferior-erlang-buffer)
4489 (goto-char insert-point)
4491 ;; Strange things happened if `comint-eol-on-send' is declared
4492 ;; in the `let' expression above, but setq:d here. The
4493 ;; `set-buffer' statement obviously makes the buffer local
4494 ;; instance of `comint-eol-on-send' shadow this one.
4495 ;; I'm considering this a bug in Elisp.
4497 ;; This was previously cautioned against in the Lisp manual. It
4498 ;; has been sorted out in Emacs 21. -- fx
4499 (let ((comint-eol-on-send nil)
4500 (comint-input-filter (if hist comint-input-filter 'ignore)))
4501 (comint-send-input))
4502 ;; Adjust all windows whose points are incorrect.
4503 (if (null comint-process-echoes)
4507 (if (and (eq (window-buffer window) inferior-erlang-buffer)
4508 (= (window-point window) insert-point))
4509 (set-window-point window
4510 (+ insert-point insert-length)))))
4512 (set-buffer old-buffer)
4513 (+ insert-point insert-length)))
4516 (defun inferior-erlang-strip-delete (&optional s)
4517 "Remove `^H' (delete) and the characters it was supposed to remove."
4519 (if (and (boundp 'comint-last-input-end)
4520 (boundp 'comint-last-output-start))
4524 (symbol-value 'comint-last-input-end)
4525 (symbol-value 'comint-last-output-start)))
4526 (while (progn (skip-chars-forward "^\C-h")
4527 (not (eq (point) (point-max))))
4530 (backward-delete-char 1))))))
4533 ;; Basically `comint-strip-ctrl-m', with a few extra checks.
4534 (defun inferior-erlang-strip-ctrl-m (&optional string)
4535 "Strip trailing `^M' characters from the current output group."
4537 (if (and (boundp 'comint-last-input-end)
4538 (boundp 'comint-last-output-start))
4539 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
4543 (symbol-value 'comint-last-input-end)
4544 (symbol-value 'comint-last-output-start)))
4545 (while (re-search-forward "\r+$" pmark t)
4546 (replace-match "" t t))))))
4549 (defun inferior-erlang-compile ()
4550 "Compile the file in the current buffer.
4552 Should Erlang return `{error, nofile}' it could not load the object
4553 module after completing the compilation. This is due to a bug in the
4554 compile command `c' when using the option `outdir'.
4556 There exists two workarounds for this bug:
4558 1) Place the directory in the Erlang load path.
4560 2) Set the Emacs variable `erlang-compile-use-outdir' to nil.
4561 To do so, place the following line in your `~/.emacs'-file:
4562 (setq erlang-compile-use-outdir nil)"
4565 (or (inferior-erlang-running-p)
4568 (or (inferior-erlang-running-p)
4569 (error "Error starting inferior Erlang shell"))
4570 (let ((dir (file-name-directory (buffer-file-name)))
4571 ;;; (file (file-name-nondirectory (buffer-file-name)))
4572 (noext (substring (buffer-file-name) 0 -4))
4573 ;; Hopefully, noone else will ever use these...
4577 (inferior-erlang-display-buffer)
4578 (inferior-erlang-wait-prompt)
4579 (setq end (inferior-erlang-send-command
4580 (if erlang-compile-use-outdir
4581 (format "c(\"%s\", [{outdir, \"%s\"}])." noext dir)
4584 "f(%s), {ok, %s} = file:get_cwd(), "
4585 "file:set_cwd(\"%s\"), "
4586 "%s = c(\"%s\"), file:set_cwd(%s), f(%s), %s.")
4589 tmpvar2 noext tmpvar tmpvar tmpvar2))
4591 (inferior-erlang-wait-prompt)
4593 (set-buffer inferior-erlang-buffer)
4594 (setq compilation-error-list nil)
4595 (set-marker compilation-parsing-end end))
4596 (setq compilation-last-buffer inferior-erlang-buffer)))
4599 ;; `next-error' only accepts buffers with major mode `compilation-mode'
4600 ;; or with the minor mode `compilation-minor-mode' activated.
4601 ;; (To activate the minor mode is out of the question, since it will
4602 ;; ruin the inferior Erlang keymap.)
4603 ;; This is done differently in Emacs 21.
4604 (defun inferior-erlang-next-error (&optional argp)
4605 "Just like `next-error'.
4606 Capable of finding error messages in an inferior Erlang buffer."
4609 (buf (and (boundp 'compilation-last-buffer)
4610 compilation-last-buffer)))
4611 (if (and (bufferp buf)
4614 (and (eq major-mode 'erlang-shell-mode)
4615 (setq major-mode 'compilation-mode))))
4622 (setq major-mode 'erlang-shell-mode))))
4624 (next-error argp))))
4627 (defun inferior-erlang-change-directory (&optional dir)
4628 "Make the inferior Erlang change directory.
4629 The default is to go to the directory of the current buffer."
4631 (or dir (setq dir (file-name-directory (buffer-file-name))))
4632 (or (inferior-erlang-running-p)
4633 (error "No inferior Erlang is running"))
4634 (inferior-erlang-display-buffer)
4635 (inferior-erlang-wait-prompt)
4636 (inferior-erlang-send-command (format "cd('%s')." dir) nil))
4638 (defun erlang-align-arrows (start end)
4639 "Align arrows (\"->\") in function clauses from START to END.
4640 When called interactively, aligns arrows after function clauses inside
4643 With a prefix argument, aligns all arrows, not just those in function
4648 sum(L) -> sum(L, 0).
4649 sum([H|T], Sum) -> sum(T, Sum + H);
4650 sum([], Sum) -> Sum.
4654 sum(L) -> sum(L, 0).
4655 sum([H|T], Sum) -> sum(T, Sum + H);
4656 sum([], Sum) -> Sum."
4659 (let (;; regexp for matching arrows. without a prefix argument,
4660 ;; the regexp matches function heads. With a prefix, it
4661 ;; matches any arrow.
4662 (re (if current-prefix-arg
4665 (concat "^" erlang-atom-regexp ".*\\(\\)->"))))
4666 ;; part of regexp matching directly before the arrow
4667 (arrow-match-pos (if current-prefix-arg
4669 (1+ erlang-atom-regexp-matches)))
4670 ;; accumulator for positions where arrows are found, ordered
4671 ;; by buffer position (from greatest to smallest)
4672 (arrow-positions '())
4673 ;; accumulator for longest distance from start of line to arrow
4675 ;; marker to track the end of the region we're aligning
4676 (end-marker (progn (goto-char end)
4678 ;; Pass 1: Find the arrow positions, adjust the whitespace
4679 ;; before each arrow to one space, and find the greatest
4680 ;; indentation level.
4682 (while (re-search-forward re end-marker t)
4683 (goto-char (match-beginning arrow-match-pos))
4684 (just-one-space) ; adjust whitespace
4685 (setq arrow-positions (cons (point) arrow-positions))
4686 (setq most-indent (max most-indent (erlang-column-number))))
4687 (set-marker end-marker nil) ; free the marker
4688 ;; Pass 2: Insert extra padding so that all arrow indentation is
4689 ;; equal. This is done last-to-first by buffer position, so that
4690 ;; inserting spaces before one arrow doesn't change the
4691 ;; positions of the next ones.
4692 (mapcar (lambda (arrow-pos)
4693 (goto-char arrow-pos)
4694 (let* ((pad (- most-indent (erlang-column-number))))
4696 (insert-char ?\ pad))))
4699 (defun erlang-column-number ()
4700 "Return the column number of the current position in the buffer.
4701 Tab characters are counted by their visual width."
4702 (string-width (buffer-substring (line-beginning-position) (point))))
4704 (defun erlang-current-defun ()
4705 "`add-log-current-defun-function' for Erlang."
4707 (erlang-beginning-of-function)
4708 (if (looking-at "[a-z0-9_]+")
4711 ;; Aliases for backward compatibility with older versions of Erlang Mode.
4713 ;; Unfortuantely, older versions of Emacs doesn't have `defalias' and
4714 ;; `make-obsolete' so we have to define our own `obsolete' function.
4716 (defun erlang-obsolete (sym newdef)
4717 "Make the obsolete function SYM refer to the defined function NEWDEF.
4719 Simplified version of a combination `defalias' and `make-obsolete',
4720 it assumes that NEWDEF is loaded."
4721 (defalias sym (symbol-function newdef))
4722 (if (fboundp 'make-obsolete)
4723 (make-obsolete sym newdef)))
4726 (erlang-obsolete 'calculate-erlang-indent 'erlang-calculate-indent)
4727 (erlang-obsolete 'calculate-erlang-stack-indent
4728 'erlang-calculate-stack-indent)
4729 (erlang-obsolete 'at-erlang-keyword 'erlang-at-keyword)
4730 (erlang-obsolete 'at-erlang-operator 'erlang-at-operator)
4731 (erlang-obsolete 'beginning-of-erlang-clause 'erlang-beginning-of-clause)
4732 (erlang-obsolete 'end-of-erlang-clause 'erlang-end-of-clause)
4733 (erlang-obsolete 'mark-erlang-clause 'erlang-mark-clause)
4734 (erlang-obsolete 'beginning-of-erlang-function 'erlang-beginning-of-function)
4735 (erlang-obsolete 'end-of-erlang-function 'erlang-end-of-function)
4736 (erlang-obsolete 'mark-erlang-function 'erlang-mark-function)
4737 (erlang-obsolete 'pass-over-erlang-clause 'erlang-pass-over-function)
4738 (erlang-obsolete 'name-of-erlang-function 'erlang-name-of-function)
4741 ;; Fixme: shouldn't redefine `set-visited-file-name' anyhow -- see above.
4742 (defconst erlang-unload-hook
4744 (defalias 'set-visited-file-name
4745 'erlang-orig-set-visited-file-name)
4746 (when (featurep 'advice)
4747 (ad-unadvise 'Man-notify-when-ready)
4748 (ad-unadvise 'set-visited-file-name)))))
4754 (run-hooks 'erlang-load-hook)
4757 ;; coding: iso-8859-1
4760 ;;; erlang.el ends here