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
36 ;; This package provides support for the programming language Erlang.
37 ;; The package provides an editing mode with lots of bells and
38 ;; whistles, compilation support, and it makes it possible for the
39 ;; user to start Erlang shells that run inside Emacs.
41 ;; See the Erlang distribution for full documentation of this package.
46 ;; Place this file in Emacs load path, byte-compile it, and add the
47 ;; following line to the appropriate init file:
49 ;; (require 'erlang-start)
51 ;; The full documentation contains much more extensive description of
52 ;; the installation procedure.
57 ;; Please send bug reports to the following email address:
58 ;; erlang-bugs@erlang.org
59 ;; or if you have a patch suggestion to:
60 ;; erlang-patches@erlang.org
61 ;; Please state as exactly as possible:
62 ;; - Version number of Erlang Mode (see the menu), Emacs, Erlang,
63 ;; and of any other relevant software.
64 ;; - What the expected result was.
65 ;; - What you did, preferably in a repeatable step-by-step form.
66 ;; - A description of the unexpected result.
67 ;; - Relevant pieces of Erlang code causing the problem.
68 ;; - Personal Emacs customisations, if any.
70 ;; Should the Emacs generate an error, please set the Emacs variable
71 ;; `debug-on-error' to `t'. Repeat the error and enclose the debug
72 ;; information in your bug-report.
74 ;; To set the variable you can use the following command:
75 ;; M-x set-variable RET debug-on-error RET t RET
80 (defconst erlang-version
"2.5.2"
81 "The version number of Erlang mode.")
83 (defvar erlang-root-dir nil
84 "The directory where the Erlang system is installed.
85 The name should not contain the trailing slash.
87 Should this variable be nil, no manual pages will show up in the
90 (defvar erlang-menu-items
'(erlang-menu-base-items
91 erlang-menu-skel-items
92 erlang-menu-shell-items
93 erlang-menu-compile-items
95 erlang-menu-personal-items
96 erlang-menu-version-items
)
97 "*List of menu item list to combine to create Erlang mode menu.
99 External programs which temporarily add menu items to the Erlang mode
100 menu may use this variable. Please use the function `add-hook' to add
103 Please call the function `erlang-menu-init' after every change to this
106 (defvar erlang-menu-base-items
108 (("Indent Line" erlang-indent-command
)
109 ("Indent Region " erlang-indent-region
110 (if erlang-xemacs-p
(mark) mark-active
))
111 ("Indent Clause" erlang-indent-clause
)
112 ("Indent Function" erlang-indent-function
)
113 ("Indent Buffer" erlang-indent-current-buffer
)))
115 (("Fill Comment" erlang-fill-paragraph
)
116 ("Comment Region" comment-region
117 (if erlang-xemacs-p
(mark) mark-active
))
118 ("Uncomment Region" erlang-uncomment-region
119 (if erlang-xemacs-p
(mark) mark-active
))
121 ("Beginning of Function" erlang-beginning-of-function
)
122 ("End of Function" erlang-end-of-function
)
123 ("Mark Function" erlang-mark-function
)
125 ("Beginning of Clause" erlang-beginning-of-clause
)
126 ("End of Clause" erlang-end-of-clause
)
127 ("Mark Clause" erlang-mark-clause
)
129 ("New Clause" erlang-generate-new-clause
)
130 ("Clone Arguments" erlang-clone-arguments
)
132 ("Align Arrows" erlang-align-arrows
)))
133 ("Syntax Highlighting"
134 (("Level 3" erlang-font-lock-level-3
)
135 ("Level 2" erlang-font-lock-level-2
)
136 ("Level 1" erlang-font-lock-level-1
)
137 ("Off" erlang-font-lock-level-0
)))
139 (("Find Tag" find-tag
)
140 ("Find Next Tag" erlang-find-next-tag
)
141 ;("Find Regexp" find-tag-regexp)
142 ("Complete Word" erlang-complete-tag
)
143 ("Tags Apropos" tags-apropos
)
144 ("Search Files" tags-search
))))
145 "Description of menu used in Erlang mode.
147 This variable must be a list. The elements are either nil representing
148 a horizontal line or a list with two or three elements. The first is
149 the name of the menu item, the second is the function to call, or a
150 submenu, on the same same form as ITEMS. The third optional argument
151 is an expression which is evaluated every time the menu is displayed.
152 Should the expression evaluate to nil the menu item is ghosted.
155 '((\"Func1\" function-one)
157 ((\"Yellow\" function-yellow)
158 (\"Blue\" function-blue)))
160 (\"Region Function\" spook-function midnight-variable))
162 Call the function `erlang-menu-init' after modifying this variable.")
164 (defvar erlang-menu-shell-items
167 (("Start New Shell" erlang-shell
)
168 ("Display Shell" erlang-shell-display
))))
169 "Description of the Shell menu used by Erlang mode.
171 Please see the documentation of `erlang-menu-base-items'.")
173 (defvar erlang-menu-compile-items
175 (("Compile Buffer" erlang-compile
)
176 ("Display Result" erlang-compile-display
)
177 ("Next Error" erlang-next-error
))))
178 "Description of the Compile menu used by Erlang mode.
180 Please see the documentation of `erlang-menu-base-items'.")
182 (defvar erlang-menu-version-items
184 ("Version" erlang-version
))
185 "Description of the version menu used in Erlang mode.")
187 (defvar erlang-menu-personal-items nil
188 "Description of personal menu items used in Erlang mode.
190 Please see the variable `erlang-menu-base-items' for a description
193 (defvar erlang-menu-man-items nil
194 "The menu containing man pages.
196 The format of the menu should be compatible with `erlang-menu-base-items'.
197 This variable is added to the list of Erlang menus stored in
198 `erlang-menu-items'.")
200 (defvar erlang-menu-skel-items
'()
201 "Description of the menu containing the skeleton entries.
202 The menu is in the form described by the variable `erlang-menu-base-items'.")
204 (defvar erlang-mode-hook nil
205 "*Functions to run when Erlang mode is activated.
207 This hook is used to change the behaviour of Erlang mode. It is
208 normally used by the user to personalise the programming environment.
209 When used in a site init file, it could be used to customise Erlang
210 mode for all users on the system.
212 The functions added to this hook are run every time Erlang mode is
213 started. See also `erlang-load-hook', a hook which is run once,
214 when Erlang mode is loaded into Emacs, and `erlang-shell-mode-hook'
215 which is run every time a new inferior Erlang shell is started.
217 To use a hook, create an Emacs lisp function to perform your actions
218 and add the function to the hook by calling `add-hook'.
220 The following example binds the key sequence C-c C-c to the command
221 `erlang-compile' (normally bound to C-c C-k). The example also
222 activates Font Lock mode to fontify the buffer and adds a menu
223 containing all functions defined in the current buffer.
225 To use the example, copy the following lines to your `~/.emacs' file:
227 (add-hook 'erlang-mode-hook 'my-erlang-mode-hook)
229 (defun my-erlang-mode-hook ()
230 (local-set-key \"\\C-c\\C-c\" 'erlang-compile)
233 (setq font-lock-maximum-decoration t)
235 (if (and window-system (fboundp 'imenu-add-to-menubar))
236 (imenu-add-to-menubar \"Imenu\")))")
238 (defvar erlang-load-hook nil
239 "*Functions to run when Erlang mode is loaded.
241 This hook is used to change the behaviour of Erlang mode. It is
242 normally used by the user to personalise the programming environment.
243 When used in a site init file, it could be used to customize Erlang
244 mode for all users on the system.
246 The difference between this hook and `erlang-mode-hook' and
247 `erlang-shell-mode-hook' is that the functions in this hook
248 is only called once, when the Erlang mode is loaded into Emacs
251 Natural actions for the functions added to this hook are actions which
252 only should be performed once, and actions which should be performed
253 before starting Erlang mode. For example, a number of variables are
254 used by Erlang mode before `erlang-mode-hook' is run.
256 The following example sets the variable `erlang-root-dir' so that the
257 manual pages can be retrieved (note that you must set the value of
258 `erlang-root-dir' to match the location of Erlang on your system):
260 (add-hook 'erlang-load-hook 'my-erlang-load-hook)
262 (defun my-erlang-load-hook ()
263 (setq erlang-root-dir \"/usr/local/erlang\"))")
265 (defvar erlang-new-file-hook nil
266 "Functions to run when a new Erlang source file is being edited.
268 A useful function is `tempo-template-erlang-normal-header'.
269 \(This function only exists when the `tempo' package is available.)")
271 (defvar erlang-check-module-name
'ask
272 "*Non-nil means check that module name and file name agrees when saving.
274 If the value of this variable is the atom `ask', the user is
275 prompted. If the value is t the source is silently changed.")
277 (defvar erlang-electric-commands
278 '(erlang-electric-comma
279 erlang-electric-semicolon
281 "*List of activated electric commands.
283 The list should contain the electric commands which should be active.
284 Currently, the available electric commands are:
285 `erlang-electric-comma'
286 `erlang-electric-semicolon'
288 `erlang-electric-newline'
290 Should the variable be bound to t, all electric commands
293 To deactivate all electric commands, set this variable to nil.")
295 (defvar erlang-electric-newline-inhibit t
296 "*Set to non-nil to inhibit newline after electric command.
298 This is useful since a lot of people press return after executing an
301 In order to work, the command must also be in the
302 list `erlang-electric-newline-inhibit-list'.
304 Note that commands in this list are required to set the variable
305 `erlang-electric-newline-inhibit' to nil when the newline shouldn't be
308 (defvar erlang-electric-newline-inhibit-list
309 '(erlang-electric-semicolon
310 erlang-electric-comma
312 "*Commands which can inhibit the next newline.")
314 (defvar erlang-electric-semicolon-insert-blank-lines nil
315 "*Number of blank lines inserted before header, or nil.
317 This variable controls the behaviour of `erlang-electric-semicolon'
318 when a new function header is generated. When nil, no blank line is
319 inserted between the current line and the new header. When bound to a
320 number it represents the number of blank lines which should be
323 (defvar erlang-electric-semicolon-criteria
324 '(erlang-next-lines-empty-p
325 erlang-at-keyword-end-p
326 erlang-at-end-of-function-p
)
327 "*List of functions controlling `erlang-electric-semicolon'.
328 The functions in this list are called, in order, whenever a semicolon
329 is typed. Each function in the list is called with no arguments,
330 and should return one of the following values:
332 nil -- no determination made, continue checking
333 'stop -- do not create prototype for next line
334 (anything else) -- insert prototype, and stop checking
336 If every function in the list is called with no determination made,
337 then no prototype is inserted.
339 The test is performed by the function `erlang-test-criteria-list'.")
341 (defvar erlang-electric-comma-criteria
342 '(erlang-stop-when-inside-argument-list
343 erlang-stop-when-at-guard
344 erlang-next-lines-empty-p
345 erlang-at-keyword-end-p
346 erlang-at-end-of-function-p
)
347 "*List of functions controlling `erlang-electric-comma'.
348 The functions in this list are called, in order, whenever a comma
349 is typed. Each function in the list is called with no arguments,
350 and should return one of the following values:
352 nil -- no determination made, continue checking
353 'stop -- do not create prototype for next line
354 (anything else) -- insert prototype, and stop checking
356 If every function in the list is called with no determination made,
357 then no prototype is inserted.
359 The test is performed by the function `erlang-test-criteria-list'.")
361 (defvar erlang-electric-arrow-criteria
362 '(erlang-next-lines-empty-p
363 erlang-at-end-of-function-p
)
364 "*List of functions controlling the arrow aspect of `erlang-electric-gt'.
365 The functions in this list are called, in order, whenever a `>'
366 is typed. Each function in the list is called with no arguments,
367 and should return one of the following values:
369 nil -- no determination made, continue checking
370 'stop -- do not create prototype for next line
371 (anything else) -- insert prototype, and stop checking
373 If every function in the list is called with no determination made,
374 then no prototype is inserted.
376 The test is performed by the function `erlang-test-criteria-list'.")
378 (defvar erlang-electric-newline-criteria
380 "*List of functions controlling `erlang-electric-newline'.
382 The electric newline commands indents the next line. Should the
383 current line begin with a comment the comment start is copied to
384 the newly created line.
386 The functions in this list are called, in order, whenever a comma
387 is typed. Each function in the list is called with no arguments,
388 and should return one of the following values:
390 nil -- no determination made, continue checking
391 'stop -- do not create prototype for next line
392 (anything else) -- trigger the electric command.
394 If every function in the list is called with no determination made,
395 then no prototype is inserted. Should the atom t be a member of the
396 list, it is treated as a function triggering the electric command.
398 The test is performed by the function `erlang-test-criteria-list'.")
400 (defvar erlang-next-lines-empty-threshold
2
401 "*Number of blank lines required to activate an electric command.
403 Actually, this value controls the behaviour of the function
404 `erlang-next-lines-empty-p' which normally is a member of the
405 criteria lists controlling the electric commands. (Please see
406 the variables `erlang-electric-semicolon-criteria' and
407 `erlang-electric-comma-criteria'.)
409 The variable is bound to a threshold value, a number, representing the
410 number of lines which must be empty.
412 Setting this variable to zero, electric commands will always be
413 triggered by `erlang-next-lines-empty-p', unless inhibited by other
416 Should this variable be nil, `erlang-next-lines-empty-p' will never
417 trigger an electric command. The same effect would be reached if the
418 function `erlang-next-lines-empty-p' would be removed from the criteria
421 Note that even if `erlang-next-lines-empty-p' should not trigger an
422 electric command, other functions in the criteria list could.")
424 (defvar erlang-new-clause-with-arguments nil
425 "*Non-nil means that the arguments are cloned when a clause is generated.
427 A new function header can be generated by calls to the function
428 `erlang-generate-new-clause' and by use of the electric semicolon.")
430 (defvar erlang-compile-use-outdir t
431 "*When nil, go to the directory containing source file when compiling.
433 This is a workaround for a bug in the `outdir' option of compile. If the
434 outdir is not in the current load path, Erlang doesn't load the object
435 module after it has been compiled.
437 To activate the workaround, place the following in your `~/.emacs' file:
438 (setq erlang-compile-use-outdir nil)")
440 (defvar erlang-indent-level
4
441 "*Indentation of Erlang calls/clauses within blocks.")
443 (defvar erlang-indent-guard
2
444 "*Indentation of Erlang guards.")
446 (defvar erlang-argument-indent
2
447 "*Indentation of the first argument in a function call.
448 When nil, indent to the column after the `(' of the
451 (defvar erlang-tab-always-indent t
452 "*Non-nil means TAB in Erlang mode should always re-indent the current line,
453 regardless of where in the line point is when the TAB command is used.")
455 (defvar erlang-error-regexp-alist
456 '(("^\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" .
(1 2)))
457 "*Patterns for matching Erlang errors.")
459 (defvar erlang-man-inhibit
(eq system-type
'windows-nt
)
460 "Inhibit the creation of the Erlang Manual Pages menu.
462 The Windows distribution of Erlang does not include man pages, hence
463 there is no attempt to create the menu.")
465 (defvar erlang-man-dirs
466 '(("Man - Commands" "/man/man1" t
)
467 ("Man - Modules" "/man/man3" t
)
468 ("Man - Files" "/man/man4" t
)
469 ("Man - Applications" "/man/man6" t
))
470 "*The man directories displayed in the Erlang menu.
472 Each item in the list should be a list with three elements, the first
473 the name of the menu, the second the directory, and the last a flag.
474 Should the flag the nil, the directory is absolute, should it be non-nil
475 the directory is relative to the variable `erlang-root-dir'.")
477 (defvar erlang-man-max-menu-size
20
478 "*The maximum number of menu items in one menu allowed.")
480 (defvar erlang-man-display-function
'erlang-man-display
481 "*Function used to display man page.
483 The function is called with one argument, the name of the file
484 containing the man page. Use this variable when the default
485 function, `erlang-man-display', does not work on your system.")
488 (defconst erlang-atom-regexp
"\\([a-z][a-zA-Z0-9_]*\\|'[^\n']*[^\\]'\\)"
489 "Regexp which should match an Erlang atom.
491 The regexp must be surrounded with a pair of regexp parentheses."))
492 (defconst erlang-atom-regexp-matches
1
493 "Number of regexp parenthesis pairs in `erlang-atom-regexp'.
495 This is used to determine parenthesis matches in complex regexps which
496 contains `erlang-atom-regexp'.")
498 (defconst erlang-variable-regexp
"\\([A-Z_][a-zA-Z0-9_]*\\)"
499 "Regexp which should match an Erlang variable.
501 The regexp must be surrounded with a pair of regexp parentheses.")
502 (defconst erlang-variable-regexp-matches
1
503 "Number of regexp parenthesis pairs in `erlang-variable-regexp'.
505 This is used to determine matches in complex regexps which contains
506 `erlang-variable-regexp'.")
508 (defvar erlang-defun-prompt-regexp
(concat "^" erlang-atom-regexp
"\\s *(")
509 "Regexp which should match beginning of a clause.")
511 (defvar erlang-file-name-extension-regexp
"\\.[eh]rl$"
512 "*Regexp which should match an Erlang file name.
514 This regexp is used when an Erlang module name is extracted from the
515 name of an Erlang source file.
517 The regexp should only match the section of the file name which should
518 be excluded from the module name.
520 To match all files set this variable to \"\\\\(\\\\..*\\\\|\\\\)$\".
521 The matches all except the extension. This is useful if the Erlang
522 tags system should interpret tags on the form `module:tag' for
523 files written in other languages than Erlang.")
525 (defvar erlang-mode-map nil
526 "*Keymap used in Erlang mode.")
527 (defvar erlang-mode-abbrev-table nil
528 "Abbrev table in use in Erlang-mode buffers.")
529 (defvar erlang-mode-syntax-table nil
530 "Syntax table in use in Erlang-mode buffers.")
532 (defconst erlang-emacs-major-version
533 (if (boundp 'emacs-major-version
)
535 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version
)
536 (string-to-int (substring emacs-version
537 (match-beginning 1) (match-end 1))))
538 "Major version number of Emacs.")
540 (defconst erlang-emacs-minor-version
541 (if (boundp 'emacs-minor-version
)
543 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version
)
544 (string-to-int (substring emacs-version
545 (match-beginning 2) (match-end 2))))
546 "Minor version number of Emacs.")
548 (defconst erlang-xemacs-p
(string-match "Lucid\\|XEmacs" emacs-version
)
549 "Non-nil when running under XEmacs or Lucid Emacs.")
551 (defvar erlang-xemacs-popup-menu
'("Erlang Mode Commands" . nil
)
552 "Common popup menu for all buffers in Erlang mode.
554 This variable is destructively modified every time the Erlang menu
555 is modified. The effect is that all changes take effect in all
556 buffers in Erlang mode, just like under GNU Emacs.
558 Never EVER set this variable!")
560 (defconst inferior-erlang-use-cmm
(boundp 'minor-mode-overriding-map-alist
)
561 "Non-nil means use `compilation-minor-mode' in Erlang shell.")
563 ;; Tempo skeleton templates:
564 (load "erlang-skels")
567 (load "erlang-sinan")
570 ;; Font-lock variables
572 (defvar erlang-font-lock-modern-p
573 (cond ((>= erlang-emacs-major-version
20) t
)
574 (erlang-xemacs-p (>= erlang-emacs-minor-version
14))
575 ((= erlang-emacs-major-version
19) (>= erlang-emacs-minor-version
29))
577 "Non-nil when this version of Emacs uses a modern version of Font Lock.
579 This is determined by checking the version of Emacs used, the actual
580 font-lock code is not loaded.")
583 ;; The next few variables define different Erlang font-lock patterns.
584 ;; They could be appended to form a custom font-lock appearance.
586 ;; The function `erlang-font-lock-set-face' could be used to change
587 ;; the face of a pattern.
589 ;; Note that Erlang strings and atoms are highlighted with using
590 ;; syntactic analysis.
592 (defvar erlang-font-lock-keywords-func
594 (list (concat "^" erlang-atom-regexp
"\\s *(")
595 1 'font-lock-function-name-face t
))
596 "Font lock keyword highlighting a function header.")
598 (defvar erlang-font-lock-keywords-dollar
600 (list "\\(\\$\\([^\\]\\|\\\\\\([^0-7^\n]\\|[0-7]+\\|\\^[a-zA-Z]\\)\\)\\)"
601 1 'font-lock-string-face
))
602 "Font lock keyword highlighting numbers in ASCII form (e.g. $A).")
604 (defvar erlang-font-lock-keywords-arrow
606 (list "\\(->\\|:-\\)\\(\\s \\|$\\)" 2 'font-lock-function-name-face
))
607 "Font lock keyword highlighting clause arrow.")
609 (defvar erlang-font-lock-keywords-lc
611 (list "\\(<-\\)\\(\\s \\|$\\)" 1 'font-lock-keyword-face
)
612 (list "\\(||\\)\\(\\s \\|$\\)" 1 'font-lock-keyword-face
))
613 "Font lock keyword highlighting list comprehension operators.")
615 (defvar erlang-font-lock-keywords-keywords
617 (list (concat "\\<\\(a\\(fter\\|ndalso\\)\\|begin\\|c\\(atch\\|ase\\)"
618 "\\|end\\|fun\\|if\\|o\\(f\\|relse\\)\\|receive\\|try\\|when"
619 "\\|query\\)\\([^a-zA-Z0-9_]\\|$\\)")
620 1 'font-lock-keyword-face
))
621 "Font lock keyword highlighting Erlang keywords.")
623 (defvar erlang-font-lock-keywords-attr
625 (list (concat "^\\(-" erlang-atom-regexp
"\\)\\s *\\(\\.\\|(\\)")
626 1 'font-lock-function-name-face
))
627 "Font lock keyword highlighting attributes.")
629 (defvar erlang-font-lock-keywords-quotes
631 (list "`\\([-+a-zA-Z0-9_:*][-+a-zA-Z0-9_:*]+\\)'"
633 (if erlang-font-lock-modern-p
634 'font-lock-reference-face
635 'font-lock-keyword-face
)
637 "Font lock keyword highlighting words in single quotes in comments.
639 This is not the highlighting of Erlang strings and atoms, which
640 are highlighted by syntactic analysis.")
642 ;; Note: The guard `float' collides with the bif `float'.
643 (defvar erlang-font-lock-keywords-guards
647 "\\(is_\\)*\\(atom\\|boolean\\|function\\|binary\\|constant"
648 "\\|float\\|integer\\|list\\|number\\|p\\(id\\|ort\\)\\|"
649 "re\\(ference\\|cord\\)\\|tuple"
653 (if erlang-font-lock-modern-p
654 'font-lock-reference-face
655 'font-lock-keyword-face
)))
656 "Font lock keyword highlighting guards.")
658 (defvar erlang-font-lock-keywords-bifs
663 "a\\(bs\\|live\\|pply\\|tom_to_list\\)\\|"
664 "binary_to_\\(list\\|term\\)\\|"
665 "concat_binary\\|d\\(ate\\|isconnect_node\\)\\|"
666 "e\\(lement\\|rase\\|xit\\)\\|"
667 "float\\(\\|_to_list\\)\\|"
668 "g\\(arbage_collect\\|et\\(\\|_keys\\)\\|roup_leader\\)\\|"
670 "i\\(nte\\(ger_to_list\\|rnal_bif\\)\\|s_alive\\)\\|"
671 "l\\(ength\\|i\\(nk\\|st_to_\\(atom\\|binary\\|float\\|integer"
672 "\\|pid\\|tuple\\)\\)\\)\\|"
673 "make_ref\\|no\\(de\\(\\|_\\(link\\|unlink\\)\\|s\\)\\|talive\\)\\|"
675 "p\\(id_to_list\\|rocess\\(_\\(flag\\|info\\)\\|es\\)\\|ut\\)\\|"
676 "r\\(egister\\(\\|ed\\)\\|ound\\)\\|"
677 "s\\(e\\(lf\\|telement\\)\\|ize\\|"
678 "p\\(awn\\(\\|_link\\)\\|lit_binary\\)\\|tatistics\\)\\|"
679 "t\\(erm_to_binary\\|hrow\\|ime\\|l\\|"
680 "r\\(ace\\|unc\\)\\|uple_to_list\\)\\|"
681 "un\\(link\\|register\\)\\|whereis"
684 'font-lock-keyword-face
))
685 "Font lock keyword highlighting built in functions.")
687 (defvar erlang-font-lock-keywords-macros
689 (list (concat "?\\s *\\(" erlang-atom-regexp
690 "\\|" erlang-variable-regexp
"\\)\\>")
691 1 (if erlang-font-lock-modern-p
692 'font-lock-reference-face
693 'font-lock-type-face
))
694 (list (concat "^-\\(define\\|ifn?def\\)\\s *(\\s *\\(" erlang-atom-regexp
695 "\\|" erlang-variable-regexp
"\\)\\>")
696 2 (if erlang-font-lock-modern-p
697 'font-lock-reference-face
698 'font-lock-type-face
)))
699 "Font lock keyword highlighting macros.
700 This must be placed in front of `erlang-font-lock-keywords-vars'.")
702 (defvar erlang-font-lock-keywords-records
704 (list (concat "#\\s *" erlang-atom-regexp
"\\>")
705 1 'font-lock-type-face
)
706 ;; Don't highlight numerical constants.
707 (list "\\<[0-9][0-9]?#\\([0-9a-fA_F]+\\)\\>"
709 (list (concat "^-record(\\s *" erlang-atom-regexp
"\\>")
710 1 'font-lock-type-face
))
711 "Font lock keyword highlighting Erlang records.
712 This must be placed in front of `erlang-font-lock-keywords-vars'.")
714 (defvar erlang-font-lock-keywords-vars
716 (list (concat "\\<" erlang-variable-regexp
"\\>")
717 1 (if erlang-font-lock-modern-p
718 'font-lock-variable-name-face
719 'font-lock-type-face
)))
720 "Font lock keyword highlighting Erlang variables.
721 Must be preceded by `erlang-font-lock-keywords-macros' and `-records'
725 (defvar erlang-font-lock-keywords-1
726 (append erlang-font-lock-keywords-func
727 erlang-font-lock-keywords-dollar
728 erlang-font-lock-keywords-arrow
729 erlang-font-lock-keywords-keywords
)
730 ;; DocStringOrig: erlang-font-lock-keywords
731 "Font-lock keywords used by Erlang Mode.
733 There exists three levels of Font Lock keywords for Erlang:
734 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
735 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
736 `erlang-font-lock-keywords-3' - Variables, macros and records.
738 To use a specific level, please set the variable
739 `font-lock-maximum-decoration' to the appropriate level. Note that the
740 variable must be set before Erlang mode is activated.
743 (setq font-lock-maximum-decoration 2)")
746 (defvar erlang-font-lock-keywords-2
747 (append erlang-font-lock-keywords-1
748 erlang-font-lock-keywords-attr
749 erlang-font-lock-keywords-quotes
750 erlang-font-lock-keywords-guards
751 erlang-font-lock-keywords-bifs
)
752 ;; DocStringCopy: erlang-font-lock-keywords
753 "Font-lock keywords used by Erlang Mode.
755 There exists three levels of Font Lock keywords for Erlang:
756 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
757 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
758 `erlang-font-lock-keywords-3' - Variables, macros and records.
760 To use a specific level, please set the variable
761 `font-lock-maximum-decoration' to the appropriate level. Note that the
762 variable must be set before Erlang mode is activated.
765 (setq font-lock-maximum-decoration 2)")
768 (defvar erlang-font-lock-keywords-3
769 (append erlang-font-lock-keywords-2
770 erlang-font-lock-keywords-macros
771 erlang-font-lock-keywords-records
772 erlang-font-lock-keywords-vars
)
773 ;; DocStringCopy: erlang-font-lock-keywords
774 "Font-lock keywords used by Erlang Mode.
776 There exists three levels of Font Lock keywords for Erlang:
777 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
778 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
779 `erlang-font-lock-keywords-3' - Variables, macros and records.
781 To use a specific level, please set the variable
782 `font-lock-maximum-decoration' to the appropriate level. Note that the
783 variable must be set before Erlang mode is activated.
786 (setq font-lock-maximum-decoration 2)")
789 (defvar erlang-font-lock-keywords erlang-font-lock-keywords-3
790 ;; DocStringCopy: erlang-font-lock-keywords
791 "Font-lock keywords used by Erlang Mode.
793 There exists three levels of Font Lock keywords for Erlang:
794 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
795 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
796 `erlang-font-lock-keywords-3' - Variables, macros and records.
798 To use a specific level, please set the variable
799 `font-lock-maximum-decoration' to the appropriate level. Note that the
800 variable must be set before Erlang mode is activated.
803 (setq font-lock-maximum-decoration 2)")
806 (defvar erlang-font-lock-syntax-table nil
807 "Syntax table used by Font Lock mode.
809 The difference between this and the standard Erlang Mode
810 syntax table is that `_' is treated as part of words by
813 Unfortunately, XEmacs hasn't got support for a special Font
814 Lock syntax table. The effect is that `apply' in the atom
815 `foo_apply' will be highlighted as a bif.")
818 ;;; Avoid errors while compiling this file.
820 ;; `eval-when-compile' is not defined in Emacs 18. We define it as a
822 (or (fboundp 'eval-when-compile
)
823 (defmacro eval-when-compile
(&rest rest
) nil
))
825 ;; These umm...functions are new in Emacs 20. And, yes, until version
826 ;; 19.27 Emacs backquotes were this ugly.
828 (or (fboundp 'unless
)
829 (defmacro unless
(condition &rest body
)
830 "(unless CONDITION BODY...): If CONDITION is false, do BODY, else return nil."
836 (defmacro when
(condition &rest body
)
837 "(when CONDITION BODY...): If CONDITION is true, do BODY, else return nil."
842 (or (fboundp 'char-before
)
843 (defmacro char-before
(&optional pos
)
844 "Return the character in the current buffer just before POS."
845 (` (char-after (1- (or (, pos
) (point)))))))
848 (if (or (featurep 'bytecomp
)
849 (featurep 'byte-compile
))
851 (cond ((string-match "Lucid\\|XEmacs" emacs-version
)
852 (put 'comment-indent-hook
'byte-obsolete-variable nil
)
853 ;; Do not warn for unused variables
854 ;; when compiling under XEmacs.
855 (setq byte-compile-warnings
856 '(free-vars unresolved callargs redefine
))))
858 (require 'compile
))))
861 (defun erlang-version ()
862 "Return the current version of Erlang mode."
865 (message "Erlang mode version %s, written by Anders Lindgren"
871 (defun erlang-mode ()
872 "Major mode for editing Erlang source files in Emacs.
873 It knows about syntax and comment, it can indent code, it is capable
874 of fontifying the source file, the TAGS commands are aware of Erlang
875 modules, and the Erlang man pages can be accessed.
877 Should this module, \"erlang.el\", be installed properly, Erlang mode
878 is activated whenever an Erlang source or header file is loaded into
879 Emacs. To indicate this, the mode line should contain the word
882 The main feature of Erlang mode is indentation, press TAB and the
883 current line will be indented correctly.
885 Comments starting with only one `%' are indented to the column stored
886 in the variable `comment-column'. Comments starting with two `%':s
887 are indented with the same indentation as code. Comments starting
888 with at least three `%':s are indented to the first column.
890 However, Erlang mode contains much more, this is a list of the most
892 TAB - Indent the line.
893 C-c C-q - Indent current function.
894 M-; - Create a comment at the end of the line.
895 M-q - Fill a comment, i.e. wrap lines so that they (hopefully)
897 M-a - Goto the beginning of an Erlang clause.
898 M-C-a - Ditto for function.
899 M-e - Goto the end of an Erlang clause.
900 M-C-e - Ditto for function.
901 M-h - Mark current Erlang clause.
902 M-C-h - Ditto for function.
903 C-c C-z - Start, or switch to, an inferior Erlang shell.
904 C-c C-k - Compile current file.
907 ; - Electric semicolon.
909 Erlang mode check the name of the file against the module name when
910 saving, whenever a mismatch occurs Erlang mode offers to modify the
913 The variable `erlang-electric-commands' controls the electric
914 commands. To deactivate all of them, set it to nil.
916 There exists a large number of commands and variables in the Erlang
917 module. Please press `M-x apropos RET erlang RET' to see a complete
918 list. Press `C-h f name-of-function RET' and `C-h v name-of-variable
919 RET'to see the full description of functions and variables,
922 On entry to this mode the contents of the hook `erlang-mode-hook' is
925 Please see the beginning of the file `erlang.el' for more information
926 and examples of hooks.
931 (kill-all-local-variables)
932 (setq major-mode
'erlang-mode
)
933 (setq mode-name
"Erlang")
934 (erlang-syntax-table-init)
936 (erlang-electric-init)
938 (erlang-mode-variables)
939 (erlang-check-module-name-init)
940 (erlang-add-compilation-alist erlang-error-regexp-alist
)
943 (erlang-font-lock-init)
945 (run-hooks 'erlang-mode-hook
)
946 (if (zerop (buffer-size))
947 (run-hooks 'erlang-new-file-hook
)))
950 (defun erlang-syntax-table-init ()
951 (if (null erlang-mode-syntax-table
)
952 (let ((table (make-syntax-table)))
953 (modify-syntax-entry ?
\n ">" table
)
954 (modify-syntax-entry ?
\" "\"" table
)
955 (modify-syntax-entry ?
# "." table
)
956 (modify-syntax-entry ?$
"'" table
)
957 (modify-syntax-entry ?%
"<" table
)
958 (modify-syntax-entry ?
& "." table
)
959 (modify-syntax-entry ?
\' "\"" table
)
960 (modify-syntax-entry ?
* "." table
)
961 (modify-syntax-entry ?
+ "." table
)
962 (modify-syntax-entry ?-
"." table
)
963 (modify-syntax-entry ?
/ "." table
)
964 (modify-syntax-entry ?
: "." table
)
965 (modify-syntax-entry ?
< "." table
)
966 (modify-syntax-entry ?
= "." table
)
967 (modify-syntax-entry ?
> "." table
)
968 (modify-syntax-entry ?
\\ "\\" table
)
969 (modify-syntax-entry ?_
"_" table
)
970 (modify-syntax-entry ?|
"." table
)
971 (modify-syntax-entry ?^
"'" table
)
973 (setq erlang-mode-syntax-table table
)))
975 (set-syntax-table erlang-mode-syntax-table
))
978 (defun erlang-keymap-init ()
981 (setq erlang-mode-map
(make-sparse-keymap))
982 (erlang-mode-commands erlang-mode-map
))
983 (use-local-map erlang-mode-map
))
986 (defun erlang-mode-commands (map)
987 (unless (boundp 'indent-line-function
)
988 (define-key map
"\t" 'erlang-indent-command
))
989 (define-key map
";" 'erlang-electric-semicolon
)
990 (define-key map
"," 'erlang-electric-comma
)
991 (define-key map
"<" 'erlang-electric-lt
)
992 (define-key map
">" 'erlang-electric-gt
)
993 (define-key map
"\C-m" 'erlang-electric-newline
)
994 (if (not (boundp 'delete-key-deletes-forward
))
995 (define-key map
"\177" 'backward-delete-char-untabify
)
996 (define-key map
[backspace] 'backward-delete-char-untabify))
997 (define-key map "\M-q" 'erlang-fill-paragraph)
998 (unless (boundp 'beginning-of-defun-function)
999 (define-key map "\M-\C-a" 'erlang-beginning-of-function)
1000 (define-key map "\M-\C-e" 'erlang-end-of-function)
1001 (define-key map "\M-\C-h" 'erlang-mark-function))
1002 (define-key map "\M-\t" 'erlang-complete-tag)
1003 (define-key map "\C-c\M-\t" 'tempo-complete-tag)
1004 (define-key map "\M-+" 'erlang-find-next-tag)
1005 (define-key map "\C-c\M-a" 'erlang-beginning-of-clause)
1006 (define-key map "\C-c\M-b" 'tempo-backward-mark)
1007 (define-key map "\C-c\M-e" 'erlang-end-of-clause)
1008 (define-key map "\C-c\M-f" 'tempo-forward-mark)
1009 (define-key map "\C-c\M-h" 'erlang-mark-clause)
1010 (define-key map "\C-c\C-c" 'comment-region)
1011 (define-key map "\C-c\C-j" 'erlang-generate-new-clause)
1012 (define-key map "\C-c\C-k" 'erlang-compile)
1013 (define-key map "\C-c\C-l" 'erlang-compile-display)
1014 (define-key map "\C-c\C-s" 'erlang-show-syntactic-information)
1015 (define-key map "\C-c\C-q" 'erlang-indent-function)
1016 (define-key map "\C-c\C-u" 'erlang-uncomment-region)
1017 (define-key map "\C-c\C-y" 'erlang-clone-arguments)
1018 (define-key map "\C-c\C-a" 'erlang-align-arrows)
1019 (define-key map "\C-c\C-z" 'erlang-shell-display)
1020 (unless inferior-erlang-use-cmm
1021 (define-key map "\C-x`" 'erlang-next-error)))
1024 (defun erlang-electric-init ()
1025 ;; Set up electric character functions to work with
1026 ;; delsel/pending-del mode. Also, set up text properties for bit
1028 (mapcar #'(lambda (cmd)
1029 (put cmd 'delete-selection t) ;for delsel (Emacs)
1030 (put cmd 'pending-delete t)) ;for pending-del (XEmacs)
1031 '(erlang-electric-semicolon
1032 erlang-electric-comma
1033 erlang-electric-gt))
1035 (put 'bitsyntax-open-outer 'syntax-table '(4 . ?>))
1036 (put 'bitsyntax-open-outer 'rear-nonsticky '(category))
1037 (put 'bitsyntax-open-inner 'rear-nonsticky '(category))
1038 (put 'bitsyntax-close-inner 'rear-nonsticky '(category))
1039 (put 'bitsyntax-close-outer 'syntax-table '(5 . ?<))
1040 (put 'bitsyntax-close-outer 'rear-nonsticky '(category))
1041 (setq parse-sexp-lookup-properties 't))
1044 (defun erlang-mode-variables ()
1045 (or erlang-mode-abbrev-table
1046 (define-abbrev-table 'erlang-mode-abbrev-table ()))
1047 (setq local-abbrev-table erlang-mode-abbrev-table)
1048 (make-local-variable 'paragraph-start)
1049 (setq paragraph-start (concat "^$\\|" page-delimiter))
1050 (make-local-variable 'paragraph-separate)
1051 (setq paragraph-separate paragraph-start)
1052 (make-local-variable 'paragraph-ignore-fill-prefix)
1053 (setq paragraph-ignore-fill-prefix t)
1054 (make-local-variable 'require-final-newline)
1055 (setq require-final-newline t)
1056 (make-local-variable 'defun-prompt-regexp)
1057 (setq defun-prompt-regexp erlang-defun-prompt-regexp)
1058 (make-local-variable 'comment-start)
1059 (setq comment-start "%")
1060 (make-local-variable 'comment-start-skip)
1061 (setq comment-start-skip "%+\\s *")
1062 (make-local-variable 'comment-column)
1063 (setq comment-column 48)
1064 (make-local-variable 'indent-line-function)
1065 (setq indent-line-function 'erlang-indent-command)
1066 (make-local-variable 'indent-region-function)
1067 (setq indent-region-function 'erlang-indent-region)
1068 (set (make-local-variable 'comment-indent-function) 'erlang-comment-indent)
1069 (if (<= erlang-emacs-major-version 18)
1070 (set (make-local-variable 'comment-indent-hook) 'erlang-comment-indent))
1071 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1072 (set (make-local-variable 'dabbrev-case-fold-search) nil)
1073 (set (make-local-variable 'imenu-prev-index-position-function)
1074 'erlang-beginning-of-function)
1075 (set (make-local-variable 'imenu-extract-index-name-function)
1076 'erlang-get-function-name)
1077 (set (make-local-variable 'tempo-match-finder)
1078 "[^-a-zA-Z0-9_]\\([-a-zA-Z0-9_]*\\)\\=")
1079 (set (make-local-variable 'beginning-of-defun-function)
1080 'erlang-beginning-of-function)
1081 (set (make-local-variable 'end-of-defun-function) 'erlang-end-of-function)
1082 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil)
1083 (set (make-local-variable 'fill-paragraph-function) 'erlang-fill-paragraph)
1084 (set (make-local-variable 'comment-add) 1)
1085 (set (make-local-variable 'outline-regexp) "[[:lower:]0-9_]+ *(.*) *-> *$")
1086 (set (make-local-variable 'outline-level) (lambda () 1))
1087 (set (make-local-variable 'add-log-current-defun-function)
1088 'erlang-current-defun))
1093 ;; The following code is compatible with the standard package `compilation',
1094 ;; making it possible to go to errors using `erlang-next-error' (or just
1095 ;; `next-error' in Emacs 21).
1097 ;; The normal `compile' command works of course. For best result, please
1098 ;; execute `make' with the `-w' flag.
1100 ;; Please see the variables named `compiling-..' above.
1102 (defun erlang-add-compilation-alist (alist)
1104 (cond ((boundp 'compilation-error-regexp-alist) ; Emacs 19
1106 (or (assoc (car (car alist)) compilation-error-regexp-alist)
1107 (setq compilation-error-regexp-alist
1108 (cons (car alist) compilation-error-regexp-alist)))
1109 (setq alist (cdr alist))))
1110 ((boundp 'compilation-error-regexp)
1111 ;; Emacs 18, Only one regexp is allowed.
1112 (funcall (symbol-function 'set)
1113 'compilation-error-regexp (car (car alist))))))
1115 (defun erlang-font-lock-init ()
1116 "Initialize Font Lock for Erlang mode."
1117 (or erlang-font-lock-syntax-table
1118 (setq erlang-font-lock-syntax-table
1119 (let ((table (copy-syntax-table erlang-mode-syntax-table)))
1120 (modify-syntax-entry ?_ "w" table)
1122 (set (make-local-variable 'font-lock-syntax-table)
1123 erlang-font-lock-syntax-table)
1124 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1125 'erlang-beginning-of-clause)
1126 (make-local-variable 'font-lock-keywords)
1127 (let ((level (cond ((boundp 'font-lock-maximum-decoration)
1128 (symbol-value 'font-lock-maximum-decoration))
1129 ((boundp 'font-lock-use-maximal-decoration)
1130 (symbol-value 'font-lock-use-maximal-decoration))
1133 (setq level (cdr-safe (or (assq 'erlang-mode level)
1135 ;; `level' can here be:
1136 ;; A number - The fontification level
1137 ;; nil - Use the default
1139 (cond ((eq level nil)
1140 (set 'font-lock-keywords erlang-font-lock-keywords))
1142 (set 'font-lock-keywords erlang-font-lock-keywords-1))
1144 (set 'font-lock-keywords erlang-font-lock-keywords-2))
1146 (set 'font-lock-keywords erlang-font-lock-keywords-3))))
1148 ;; Modern font-locks can handle the above much more elegantly:
1149 (set (make-local-variable 'font-lock-defaults)
1150 '((erlang-font-lock-keywords erlang-font-lock-keywords-1
1151 erlang-font-lock-keywords-2 erlang-font-lock-keywords-3)
1152 nil nil ((?_ . "w")) erlang-beginning-of-clause
1153 (font-lock-mark-block-function . erlang-mark-clause))))
1157 ;; Useful when defining your own keywords.
1158 (defun erlang-font-lock-set-face (ks &rest faces)
1159 "Replace the face components in a list of keywords.
1161 The first argument, KS, is a list of keywords. The rest of the
1162 arguments are expressions to replace the face information with. The
1163 first expression replaces the face of the first keyword, the second
1164 expression the second keyword etc.
1166 Should an expression be nil, the face of the corresponding keyword is
1169 Should fewer expressions than keywords be given, the last expression
1170 is used for all remaining keywords.
1172 Normally, the expressions are just atoms representing the new face.
1173 They could however be more complex, returning different faces in
1174 different situations.
1176 This function only handles keywords with elements on the forms:
1177 (REGEXP NUMBER FACE)
1178 (REGEXP NUMBER FACE OVERWRITE)
1180 This could be used when defining your own special font-lock setup, e.g:
1182 \(setq my-font-lock-keywords
1183 (append erlang-font-lock-keywords-func
1184 erlang-font-lock-keywords-dollar
1185 (erlang-font-lock-set-face
1186 erlang-font-lock-keywords-macros 'my-neon-green-face)
1187 (erlang-font-lock-set-face
1188 erlang-font-lock-keywords-lc 'my-deep-red 'my-light-red)
1189 erlang-font-lock-keywords-attr))
1191 For a more elaborate example, please see the beginning of the file
1195 (let* ((regexp (car (car ks)))
1196 (number (car (cdr (car ks))))
1197 (new-face (if (and faces (car faces))
1199 (car (cdr (cdr (car ks))))))
1200 (overwrite (car (cdr (cdr (cdr (car ks))))))
1201 (new-keyword (list regexp number new-face)))
1202 (if overwrite (nconc new-keyword (list overwrite)))
1203 (setq res (cons new-keyword res))
1205 (if (and faces (cdr faces))
1206 (setq faces (cdr faces)))))
1210 (defun erlang-font-lock-level-0 ()
1211 ;; DocStringOrig: font-cmd
1212 "Unfontify current buffer."
1217 (defun erlang-font-lock-level-1 ()
1218 ;; DocStringCopy: font-cmd
1219 "Fontify current buffer at level 1.
1220 This highlights function headers, reserved keywords, strings and comments."
1222 (require 'font-lock)
1223 (set 'font-lock-keywords erlang-font-lock-keywords-1)
1225 (funcall (symbol-function 'font-lock-fontify-buffer)))
1228 (defun erlang-font-lock-level-2 ()
1229 ;; DocStringCopy: font-cmd
1230 "Fontify current buffer at level 2.
1231 This highlights level 1 features (see `erlang-font-lock-level-1')
1232 plus bifs, guards and `single quotes'."
1234 (require 'font-lock)
1235 (set 'font-lock-keywords erlang-font-lock-keywords-2)
1237 (funcall (symbol-function 'font-lock-fontify-buffer)))
1240 (defun erlang-font-lock-level-3 ()
1241 ;; DocStringCopy: font-cmd
1242 "Fontify current buffer at level 3.
1243 This highlights level 2 features (see `erlang-font-lock-level-2')
1244 plus variables, macros and records."
1246 (require 'font-lock)
1247 (set 'font-lock-keywords erlang-font-lock-keywords-3)
1249 (funcall (symbol-function 'font-lock-fontify-buffer)))
1252 (defun erlang-menu-init ()
1253 "Init menus for Erlang mode.
1255 The variable `erlang-menu-items' contain a description of the Erlang
1256 mode menu. Normally, the list contains atoms, representing variables
1257 bound to pieces of the menu.
1259 Personal extensions could be added to `erlang-menu-personal-items'.
1261 This function should be called if any variable describing the
1262 menu configuration is changed."
1263 (erlang-menu-install "Erlang" erlang-menu-items erlang-mode-map t))
1266 (defun erlang-menu-install (name items keymap &optional popup)
1267 "Install a menu in Emacs or XEmacs based on an abstract description.
1269 NAME is the name of the menu.
1271 ITEMS is a list. The elements are either nil representing a horizontal
1272 line or a list with two or three elements. The first is the name of
1273 the menu item, the second the function to call, or a submenu, on the
1274 same same form as ITEMS. The third optional element is an expression
1275 which is evaluated every time the menu is displayed. Should the
1276 expression evaluate to nil the menu item is ghosted.
1278 KEYMAP is the keymap to add to menu to. (When using XEmacs, the menu
1279 will only be visible when this menu is the global, the local, or an
1280 activate minor mode keymap.)
1282 If POPUP is non-nil, the menu is bound to the XEmacs `mode-popup-menu'
1283 variable, i.e. it will popup when pressing the right mouse button.
1285 Please see the variable `erlang-menu-base-items'."
1286 (cond (erlang-xemacs-p
1287 (let ((menu (erlang-menu-xemacs name items keymap)))
1288 ;; We add the menu to the global menubar.
1289 (funcall (symbol-function 'add-submenu) nil menu)
1290 (setcdr erlang-xemacs-popup-menu (cdr menu))
1291 (if (and popup (boundp 'mode-popup-menu))
1292 (funcall (symbol-function 'set)
1293 'mode-popup-menu erlang-xemacs-popup-menu))))
1294 ((>= erlang-emacs-major-version 19)
1295 (define-key keymap (vector 'menu-bar (intern name))
1296 (erlang-menu-make-keymap name items)))
1300 (defun erlang-menu-make-keymap (name items)
1301 "Build a menu for Emacs 19."
1302 (let ((menumap (funcall (symbol-function 'make-sparse-keymap)
1305 id def first second third)
1306 (setq items (reverse items))
1308 ;; Replace any occurrence of atoms by their value.
1309 (while (and items (atom (car items)) (not (null (car items))))
1310 (if (and (boundp (car items))
1311 (listp (symbol-value (car items))))
1312 (setq items (append (reverse (symbol-value (car items)))
1314 (setq items (cdr items))))
1315 (setq first (car-safe (car items)))
1316 (setq second (car-safe (cdr-safe (car items))))
1317 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1319 (setq count (+ count 1))
1320 (setq id (intern (format "separator-%d" count)))
1321 (setq def '("--" . nil)))
1322 ((and (consp second) (eq (car second) 'lambda))
1323 (setq count (+ count 1))
1324 (setq id (intern (format "lambda-%d" count)))
1325 (setq def (cons first second)))
1328 (setq def (cons first second)))
1330 (setq count (+ count 1))
1331 (setq id (intern (format "submenu-%d" count)))
1332 (setq def (erlang-menu-make-keymap first second))))
1333 (define-key menumap (vector id) def)
1335 (put id 'menu-enable third))
1336 (setq items (cdr items)))
1337 (cons name menumap)))
1340 (defun erlang-menu-xemacs (name items &optional keymap)
1341 "Build a menu for XEmacs."
1343 first second third entry)
1345 ;; Replace any occurrence of atoms by their value.
1346 (while (and items (atom (car items)) (not (null (car items))))
1347 (if (and (boundp (car items))
1348 (listp (symbol-value (car items))))
1349 (setq items (append (reverse (symbol-value (car items)))
1351 (setq items (cdr items))))
1352 (setq first (car-safe (car items)))
1353 (setq second (car-safe (cdr-safe (car items))))
1354 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1356 (setq res (cons "------" res)))
1358 (setq res (cons (vector first second (or third t)) res)))
1359 ((and (consp second) (eq (car second) 'lambda))
1360 (setq res (cons (vector first (list 'call-interactively second)
1361 (or third t)) res)))
1363 (setq res (cons (cons first
1364 (cdr (erlang-menu-xemacs
1367 (setq items (cdr items)))
1368 (setq res (reverse res))
1369 ;; When adding a menu to a minor-mode keymap under Emacs,
1370 ;; it disappears when the mode is disabled. The expression
1371 ;; generated below imitates this behaviour.
1372 ;; (This could be expressed much clearer using backquotes,
1373 ;; but I don't want to pull in every package.)
1375 (let ((expr (list 'or
1376 (list 'eq keymap 'global-map)
1377 (list 'eq keymap (list 'current-local-map))
1382 'minor-mode-map-alist))))))
1383 (setq res (cons ':included (cons expr res)))))
1387 (defun erlang-menu-substitute (items alist)
1388 "Substitute functions in menu described by ITEMS.
1390 The menu ITEMS is updated destructively.
1392 ALIST is list of pairs where the car is the old function and cdr the new."
1393 (let (first second pair)
1395 (setq first (car-safe (car items)))
1396 (setq second (car-safe (cdr-safe (car items))))
1397 (cond ((null first))
1399 (setq pair (and second (assq second alist)))
1401 (setcar (cdr (car items)) (cdr pair))))
1402 ((and (consp second) (eq (car second) 'lambda)))
1404 (erlang-menu-substitute second alist)))
1405 (setq items (cdr items)))))
1408 (defun erlang-menu-add-above (entry above items)
1409 "Add menu ENTRY above menu entry ABOVE in menu ITEMS.
1410 Do nothing if the items already should be in the menu.
1411 Should ABOVE not be in the list, the entry is added at
1412 the bottom of the menu.
1414 The new menu is returned. No guarantee is given that the original
1415 menu is left unchanged.
1417 The equality test is performed by `eq'.
1419 Example: (erlang-menu-add-above 'my-erlang-menu-items
1420 'erlang-menu-man-items)"
1421 (erlang-menu-add-below entry above items t))
1424 (defun erlang-menu-add-below (entry below items &optional above-p)
1425 "Add menu ENTRY below menu items BELOW in the Erlang menu.
1426 Do nothing if the items already should be in the menu.
1427 Should BELOW not be in the list, items is added at the bottom
1430 The new menu is returned. No guarantee is given that the original
1431 menu is left unchanged.
1433 The equality test is performed by `eq'.
1437 \(setq erlang-menu-items
1438 (erlang-menu-add-below 'my-erlang-menu-items
1439 'erlang-menu-base-items
1440 erlang-menu-items))"
1441 (if (memq entry items)
1442 items ; Return the original menu.
1448 (setq res (append head (list entry)))
1450 ((eq below (car items))
1453 (append head (cons entry items))
1454 (append head (cons (car items)
1455 (cons entry (cdr items))))))
1458 (setq head (append head (list (car items))))
1459 (setq items (cdr items)))))
1462 (defun erlang-menu-delete (entry items)
1463 "Delete ENTRY from menu ITEMS.
1465 The new menu is returned. No guarantee is given that the original
1466 menu is left unchanged."
1471 (defun erlang-man-init ()
1472 "Add menus containing the manual pages of the Erlang.
1474 The variable `erlang-man-dirs' contains entries describing
1475 the location of the manual pages."
1477 (if erlang-man-inhibit
1479 (setq erlang-menu-man-items
1481 ("Man - Function" erlang-man-function)))
1483 (setq erlang-menu-man-items
1484 (append erlang-menu-man-items
1485 (erlang-man-make-top-menu erlang-man-dirs))))
1486 (setq erlang-menu-items
1487 (erlang-menu-add-above 'erlang-menu-man-items
1488 'erlang-menu-version-items
1490 (erlang-menu-init)))
1493 (defun erlang-man-uninstall ()
1494 "Remove the man pages from the Erlang mode."
1496 (setq erlang-menu-items
1497 (erlang-menu-delete 'erlang-menu-man-items erlang-menu-items))
1501 ;; The man menu is a hierarchal structure, with the manual sections
1502 ;; at the top, described by `erlang-man-dirs'. The next level could
1503 ;; either be the manual pages if not to many, otherwise it is an index
1504 ;; menu whose submenus will contain up to `erlang-man-max-menu-size'
1507 (defun erlang-man-make-top-menu (dir-list)
1508 "Create one menu entry per element of DIR-LIST.
1509 The format is described in the documentation of `erlang-man-dirs'."
1513 (setq dir (cond ((nth 2 (car dir-list))
1514 ;; Relative to `erlang-root-dir'.
1515 (and (stringp erlang-root-dir)
1516 (concat erlang-root-dir (nth 1 (car dir-list)))))
1519 (nth 1 (car dir-list)))))
1521 (file-readable-p dir))
1522 (setq menu (cons (list (car (car dir-list))
1523 (erlang-man-make-middle-menu
1524 (erlang-man-get-files dir)))
1526 (setq dir-list (cdr dir-list)))
1527 ;; Should no menus be found, generate a menu item which
1528 ;; will display a help text, when selected.
1532 (("Error! Why?" erlang-man-describe-error)))))))
1535 ;; Should the menu be to long, let's split it into a number of
1536 ;; smaller menus. Warning, this code contains beautiful
1537 ;; destructive operations!
1538 (defun erlang-man-make-middle-menu (filelist)
1539 "Create the second level menu from FILELIST.
1541 Should the list be longer than `erlang-man-max-menu-size', a tree of
1543 (if (<= (length filelist) erlang-man-max-menu-size)
1544 (erlang-man-make-menu filelist)
1546 (filelist (copy-sequence filelist))
1547 segment submenu pair)
1549 (setq pair (nthcdr (- erlang-man-max-menu-size 1) filelist))
1550 (setq segment filelist)
1553 (setq filelist (cdr pair))
1555 (setq submenu (erlang-man-make-menu segment))
1556 (setq menu (cons (list (concat (car (car submenu))
1558 (car (car (reverse submenu))))
1564 (defun erlang-man-make-menu (filelist)
1565 "Make a leaf menu based on FILELIST."
1569 (setq item (erlang-man-make-menu-item (car filelist)))
1571 (setq menu (cons item menu)))
1572 (setq filelist (cdr filelist)))
1576 (defun erlang-man-make-menu-item (file)
1577 "Create a menu item containing the name of the man page."
1578 (and (string-match ".*/\\([^/]+\\)\\.[^.]$" file)
1579 (let ((page (substring file (match-beginning 1) (match-end 1))))
1580 (list (capitalize page)
1583 (list 'funcall 'erlang-man-display-function
1587 (defun erlang-man-get-files (dir)
1588 "Return files in directory DIR."
1589 (directory-files dir t ".*\\.[0-9]\\'"))
1592 (defun erlang-man-module (&optional module)
1593 "Find manual page for MODULE, defaults to module of function under point.
1594 This function is aware of imported functions."
1596 (list (let* ((mod (car-safe (erlang-get-function-under-point)))
1598 (format "Manual entry for module%s: "
1599 (if (or (null mod) (string= mod ""))
1601 (format " (default %s)" mod))))))
1602 (if (string= input "")
1605 (or module (setq module (car (erlang-get-function-under-point))))
1606 (if (or (null module) (string= module ""))
1607 (error "No Erlang module name given"))
1608 (let ((dir-list erlang-man-dirs)
1609 (pat (concat "/" (regexp-quote module) "\\.[^.]$"))
1612 (while (and dir-list (null file))
1613 (setq file-list (erlang-man-get-files
1614 (if (nth 2 (car dir-list))
1615 (concat erlang-root-dir (nth 1 (car dir-list)))
1616 (nth 1 (car dir-list)))))
1617 (while (and file-list (null file))
1618 (if (string-match pat (car file-list))
1619 (setq file (car file-list)))
1620 (setq file-list (cdr file-list)))
1621 (setq dir-list (cdr dir-list)))
1623 (funcall erlang-man-display-function file)
1624 (error "No manual page for module %s found" module))))
1627 ;; Warning, the function `erlang-man-function' is a hack!
1628 ;; It links itself into the man code in a non-clean way. I have
1629 ;; chosen to keep it since it provides a very useful functionality
1630 ;; which is not possible to achieve using a clean approach.
1633 (defvar erlang-man-function-name nil
1634 "Name of function for last `erlang-man-function' call.
1635 Used for communication between `erlang-man-function' and the
1636 patch to `Man-notify-when-ready'.")
1638 (defun erlang-man-function (&optional name)
1639 "Find manual page for NAME, where NAME is module:function.
1640 The entry for `function' is displayed.
1642 This function is aware of imported functions."
1644 (list (let* ((mod-func (erlang-get-function-under-point))
1645 (mod (car-safe mod-func))
1646 (func (nth 1 mod-func))
1649 "Manual entry for `module:func' or `module'%s: "
1650 (if (or (null mod) (string= mod ""))
1652 (format " (default %s:%s)" mod func))))))
1653 (if (string= input "")
1655 (concat mod ":" func)
1658 ;; Emacs 18 doesn't provide `man'...
1665 (let ((mod-func (erlang-get-function-under-point)))
1666 (setq modname (car-safe mod-func))
1667 (setq funcname (nth 1 mod-func))))
1668 ((string-match ":" name)
1669 (setq modname (substring name 0 (match-beginning 0)))
1670 (setq funcname (substring name (match-end 0) nil)))
1672 (setq modname name)))
1673 (if (or (null modname) (string= modname ""))
1674 (error "No Erlang module name given"))
1675 (cond ((fboundp 'Man-notify-when-ready)
1676 ;; Emacs 19: The man command could possibly start an
1677 ;; asynchronous process, i.e. we must hook ourselves into
1678 ;; the system to be activated when the man-process
1682 (erlang-man-patch-notify)
1683 (setq erlang-man-function-name funcname))
1685 (erlang-man-module modname)
1686 (error (setq erlang-man-function-name nil))))
1688 (erlang-man-module modname)
1690 (erlang-man-find-function
1691 (or (get-buffer "*Manual Entry*") ; Emacs 18
1692 (current-buffer)) ; XEmacs
1696 ;; Should the defadvice be at the top level, the package `advice' would
1697 ;; be required. Now it is only required when this functionality
1698 ;; is used. (Emacs 19 specific.)
1699 (defun erlang-man-patch-notify ()
1700 "Patch the function `Man-notify-when-ready' to search for function.
1701 The variable `erlang-man-function-name' is assumed to be bound to
1702 the function name, or to nil.
1704 The reason for patching a function is that under Emacs 19, the man
1705 command is executed asynchronously."
1708 ;; This should never happened since this is only called when
1709 ;; running under Emacs 19.
1710 (error (error (concat "This command needs the package `advice', "
1711 "please upgrade your Emacs."))))
1713 (defadvice Man-notify-when-ready
1714 (after erlang-Man-notify-when-ready activate)
1715 "Set point at the documentation of the function name in
1716 `erlang-man-function-name' when the man page is displayed."
1717 (if erlang-man-function-name
1718 (erlang-man-find-function (ad-get-arg 0) erlang-man-function-name))
1719 (setq erlang-man-function-name nil)))
1722 (defun erlang-man-find-function (buf func)
1723 "Find manual page for function in `erlang-man-function-name' in buffer BUF."
1725 (let ((win (get-buffer-window buf)))
1729 (goto-char (point-min))
1730 (if (re-search-forward
1731 (concat "^[ \t]+" func " ?(")
1735 (set-window-point win (point)))
1736 (message "Could not find function `%s'" func)))))))
1739 (defun erlang-man-display (file)
1740 "Display FILE as a `man' file.
1741 This is the default manual page display function.
1742 The variables `erlang-man-display-function' contains the function
1744 ;; Emacs 18 doesn't `provide' man.
1749 (let ((process-environment (copy-sequence process-environment)))
1750 (if (string-match "\\(.*\\)/man[^/]*/\\([^/]+\\)\\.[^.]$" file)
1751 (let ((dir (substring file (match-beginning 1) (match-end 1)))
1752 (page (substring file (match-beginning 2) (match-end 2))))
1753 (if (fboundp 'setenv)
1754 (setenv "MANPATH" dir)
1756 (setq process-environment (cons (concat "MANPATH=" dir)
1757 process-environment)))
1758 (cond ((not (and (not erlang-xemacs-p)
1759 (= erlang-emacs-major-version 19)
1760 (< erlang-emacs-minor-version 29)))
1761 (manual-entry page))
1763 ;; Emacs 19.28 and earlier versions of 19:
1764 ;; The manual-entry command unconditionally prompts
1766 (funcall (symbol-function 'Man-getpage-in-background)
1768 (error "Can't find man page for %s\n" file)))))
1771 (defun erlang-man-describe-error ()
1772 "Describe why the manual pages weren't found."
1774 (with-output-to-temp-buffer "*Erlang Man Error*"
1775 (princ "Normally, this menu should contain Erlang manual pages.
1777 In order to find the manual pages, the variable `erlang-root-dir'
1778 should be bound to the name of the directory containing the Erlang
1779 installation. The name should not include the final slash.
1781 Practically, you should add a line on the following form to
1782 your ~/.emacs, or ask your system administrator to add it to
1784 (setq erlang-root-dir \"/the/erlang/root/dir/goes/here\")
1787 (setq erlang-root-dir \"/usr/local/erlang\")
1789 After installing the line, kill and restart Emacs, or restart Erlang
1790 mode with the command `M-x erlang-mode RET'.")))
1792 ;; Indentation code:
1794 (defun erlang-indent-command (&optional whole-exp)
1795 "Indent current line as Erlang code.
1796 With argument, indent any additional lines of the same clause
1797 rigidly along with this one."
1800 ;; If arg, always indent this line as Erlang
1801 ;; and shift remaining lines of clause the same amount.
1802 (let ((shift-amt (erlang-indent-line))
1805 (if erlang-tab-always-indent
1806 (beginning-of-line))
1808 (erlang-end-of-clause 1)
1814 (indent-code-rigidly beg end shift-amt "\n")))
1815 (if (and (not erlang-tab-always-indent)
1817 (skip-chars-backward " \t")
1820 (erlang-indent-line))))
1823 (defun erlang-indent-line ()
1824 "Indent current line as Erlang code.
1825 Return the amount the indentation changed by."
1826 (let ((pos (- (point-max) (point)))
1829 (beginning-of-line 1)
1831 (skip-chars-forward " \t")
1832 (cond ((looking-at "%")
1833 (setq indent (funcall comment-indent-function))
1834 (setq shift-amt (- indent (current-column))))
1836 (setq indent (erlang-calculate-indent))
1837 (cond ((null indent)
1838 (setq indent (current-indentation)))
1840 ;; This should never occur here.
1841 (error "Erlang mode error"))
1842 ((= (char-syntax (following-char)) ?\))
1843 (setq indent (1- indent))))
1844 (setq shift-amt (- indent (current-column)))))
1845 (if (zerop shift-amt)
1847 (delete-region beg (point))
1849 ;; If initial point was within line's indentation, position
1850 ;; after the indentation. Else stay at same point in text.
1851 (if (> (- (point-max) pos) (point))
1852 (goto-char (- (point-max) pos)))
1856 (defun erlang-indent-region (beg end)
1857 "Indent region of Erlang code.
1859 This is automagically called by the user level function `indent-region'."
1862 (let ((case-fold-search nil)
1864 (from-end (- (point-max) end))
1865 indent-point;; The beginning of the current line
1866 indent;; The indent amount
1870 (setq indent-point (point))
1871 (erlang-beginning-of-clause)
1872 ;; Parse the Erlang code from the beginning of the clause to
1873 ;; the beginning of the region.
1874 (while (< (point) indent-point)
1875 (setq state (erlang-partial-parse (point) indent-point state)))
1876 ;; Indent every line in the region
1878 (goto-char indent-point)
1879 (skip-chars-forward " \t")
1880 (cond ((looking-at "%")
1881 ;; Do not use our stack to help the user to customize
1882 ;; comment indentation.
1883 (setq indent (funcall comment-indent-function)))
1885 ;; Don't indent empty lines.
1890 (erlang-calculate-stack-indent (point) state)))
1891 (cond ((null indent)
1892 (setq indent (current-indentation)))
1894 ;; This should never occur here.
1895 (error "Erlang mode error"))
1896 ((= (char-syntax (following-char)) ?\))
1897 (setq indent (1- indent))))))
1898 (if (zerop (- indent (current-column)))
1900 (delete-region indent-point (point))
1902 ;; Find the next line in the region
1903 (goto-char indent-point)
1906 (setq indent-point (point)))
1907 (if (>= from-end (- (point-max) indent-point))
1909 (while (< (point) indent-point)
1910 (setq state (erlang-partial-parse
1911 (point) indent-point state))))))))
1914 (defun erlang-indent-current-buffer ()
1915 "Indent current buffer as Erlang code."
1920 (erlang-indent-region (point-min) (point-max)))))
1923 (defun erlang-indent-function ()
1924 "Indent current Erlang function."
1927 (let ((end (progn (erlang-end-of-function 1) (point)))
1928 (beg (progn (erlang-beginning-of-function 1) (point))))
1929 (erlang-indent-region beg end))))
1932 (defun erlang-indent-clause ()
1933 "Indent current Erlang clause."
1936 (let ((end (progn (erlang-end-of-clause 1) (point)))
1937 (beg (progn (erlang-beginning-of-clause 1) (point))))
1938 (erlang-indent-region beg end))))
1941 (defmacro erlang-push (x stack) (list 'setq stack (list 'cons x stack)))
1942 (defmacro erlang-pop (stack) (list 'setq stack (list 'cdr stack)))
1943 ;; Would much prefer to make caddr a macro but this clashes.
1944 (defun erlang-caddr (x) (car (cdr (cdr x))))
1947 (defun erlang-calculate-indent (&optional parse-start)
1948 "Compute appropriate indentation for current line as Erlang code.
1949 Return nil if line starts inside string, t if in a comment."
1951 (let ((indent-point (point))
1952 (case-fold-search nil)
1955 (goto-char parse-start)
1956 (erlang-beginning-of-clause))
1957 (while (< (point) indent-point)
1958 (setq state (erlang-partial-parse (point) indent-point state)))
1959 (erlang-calculate-stack-indent indent-point state))))
1961 (defun erlang-show-syntactic-information ()
1962 "Show syntactic information for current line."
1967 (let ((starting-point (point))
1968 (case-fold-search nil)
1970 (erlang-beginning-of-clause)
1971 (while (< (point) starting-point)
1972 (setq state (erlang-partial-parse (point) starting-point state)))
1973 (message "%S" state))))
1976 (defun erlang-partial-parse (from to &optional state)
1977 "Parse Erlang syntax starting at FROM until TO, with an optional STATE.
1978 Value is list (stack token-start token-type in-what)."
1979 (goto-char from) ; Start at the beginning
1980 (erlang-skip-blank to)
1981 (let ((cs (char-syntax (following-char)))
1987 ;; Done: Return previous state.
1989 (setq token (nth 1 state))
1990 (setq cs (nth 2 state))
1991 (setq in-what (nth 3 state)))
1993 ;; Word constituent: check and handle keywords.
1995 (cond ((looking-at "\\(end\\|after\\)[^_a-zA-Z0-9]")
1996 ;; Must pop top icr layer, `after' will push a new
1999 (while (and stack (eq (car (car stack)) '->))
2001 (if (and stack (memq (car (car stack)) '(icr begin)))
2002 (erlang-pop stack))))
2003 ((looking-at "catch[^,\n\\of]*\n")
2004 ;; Must pop top icr layer, `catch' in try/catch
2005 ;;will push a new layer next.
2007 (while (and stack (eq (car (car stack)) '->))
2009 (if (and stack (memq (car (car stack)) '(icr begin)))
2010 (erlang-pop stack))))
2011 ;;((looking-at "^of$")
2012 ;; Must pop top icr layer, `of' in try/catch
2013 ;;will push a new layer next.
2015 ;; (while (and stack (eq (car (car stack)) '->))
2016 ;; (erlang-pop stack))
2017 ;; (if (and stack (memq (car (car stack)) '(icr begin)))
2018 ;; (erlang-pop stack))))
2020 (cond ((looking-at "\\(if\\|case\\|receive\\|try\\)[^_a-zA-Z0-9]")
2021 ;; Must push a new icr (if/case/receive) layer.
2022 (erlang-push (list 'icr token (current-column)) stack))
2023 ((looking-at "\\(fun\\)[^_a-zA-Z0-9]")
2024 ;; Push a new icr layer if we are defining a `fun'
2025 ;; expression, not when we are refering an existing
2028 (goto-char (match-end 1))
2029 (erlang-skip-blank to)
2030 (eq (following-char) ?\())
2031 (erlang-push (list 'icr token (current-column)) stack)))
2032 ((looking-at "\\(begin\\|query\\)[^_a-zA-Z0-9]")
2033 (erlang-push (list 'begin token (current-column)) stack))
2034 ;; In test suites you may want to do something like
2035 ;; ?match(Mem when integer(Mem), mnesia:table_info(Tab,
2036 ;; memory)), and then the following if/case/receive
2037 ;; statement will mess up the indentation by fooling the
2038 ;; erlang mode to think the 'when' in the argument is a
2039 ;; "real" when. The following three clauses will avoid
2041 ((looking-at "when[^->\.]*if[^->\.]*->"))
2042 ((looking-at "when[^->\.]*case[^->\.]*->"))
2043 ((looking-at "when[^->\.]*receive[^->\.]*->"))
2045 ((looking-at "when [^->\.]*->")
2046 (erlang-push (list 'when token (current-column)) stack))
2047 ((looking-at "after[.]+->")
2048 (erlang-push (list 'icr token (current-column)) stack))
2049 ((looking-at "after[^_a-zA-Z0-9->]")
2050 ;; Probably in try-statment, fake "->" to get right
2051 ;; indentation in erlang-calculate-stack-indent. If it
2052 ;; was an ordinary catch without try, these entries will
2053 ;; be popped of the stack at a later ocaccion.
2054 (erlang-push (list 'icr token (current-column)) stack)
2055 (erlang-push (list '-> token (current-column)) stack))
2056 ((looking-at "catch[^,\n\\of]*\n")
2057 (erlang-push (list 'icr token (current-column)) stack)
2058 (erlang-push (list '-> token (current-column)) stack))
2059 ;;((looking-at "^of$")
2060 ;; (erlang-push (list 'icr token (current-column)) stack)
2061 ;;(erlang-push (list '-> token (current-column)) stack))
2064 ;; String: Try to skip over it. (Catch error if not complete.)
2071 (setq in-what 'string)
2074 (setq in-what 'string)
2077 ;; Expression prefix e.i. $ or ^ (Note ^ can be in the character
2078 ;; literal $^ or part of string and $ outside of a string denotes
2079 ;; a character literal)
2082 ((= (following-char) ?\") ;; $ or ^ was the last char in a string
2085 ;; Maybe a character literal, quote the next char to avoid
2086 ;; situations as $" being seen as the begining of a string.
2087 ;; Note the quoting something in the middle of a string is harmless.
2088 (quote (following-char))
2091 ;; Symbol constituent or punctuation
2097 ((= (following-char) ?\;)
2098 (if (and stack (eq (car (car stack)) '->))
2103 ((looking-at "\\.\\(\\s \\|\n\\|\\s<\\)")
2108 ((looking-at "->\\|:-")
2110 (back-to-indentation)
2111 (cond ((looking-at "after[^_a-zA-Z0-9]")
2112 (erlang-pop stack))))
2113 (if (and stack (eq (car (car stack)) 'when))
2115 (erlang-push (list '-> token (current-column)) stack)
2118 ;; List-comprehension divider
2120 (erlang-push (list '|| token (current-column)) stack)
2123 ;;((looking-at ",$")
2124 ;; Normal catch not try-catch have caused icr
2125 ;; and then incr and faked "->" should be removed
2127 ;; (back-to-indentation)
2128 ;; (cond ((looking-at "catch[^_a-zA-Z0-9]")
2129 ;; (erlang-pop stack)
2130 ;; (erlang-pop stack))))
2133 ;; Parameter separator
2137 ;; Bit-syntax open paren
2139 (erlang-push (list '\( token (current-column)) stack)
2142 ;; Bbit-syntax close paren
2144 (while (memq (car (car stack)) '(|| ->))
2146 (cond ((eq (car (car stack)) '\()
2148 ((memq (car (car stack)) '(icr begin))
2149 (error "Missing `end'"))
2151 (error "Unbalanced parentheses")))
2155 ((= (following-char) ??)
2160 ;; Other punctuation: Skip over it and any following punctuation
2162 ;; Skip over all characters in the operand.
2163 (skip-syntax-forward "."))
2165 ;; Other char: Skip over it.
2171 (erlang-push (list '\( token (current-column)) stack)
2174 ;; Close parenthesis
2176 (while (memq (car (car stack)) '(|| ->))
2178 (cond ((eq (car (car stack)) '\()
2180 ((eq (car (car stack)) 'icr)
2182 ;; Normal catch not try-catch might have caused icr
2183 ;; and then incr should be removed and is not an error.
2184 (if (eq (car (car stack)) '\()
2187 (error "Missing `end'"))
2189 ((eq (car (car stack)) 'begin)
2190 (error "Missing `end'")
2192 (error "Unbalanced parenthesis"))
2196 ;; Character quote: Skip it and the quoted char.
2200 ;; Character escape: Skip it and the escape sequence.
2203 (skip-syntax-forward "w"))
2208 (list stack token cs in-what)))
2210 (defun erlang-calculate-stack-indent (indent-point state)
2211 "From the given last position and state (stack) calculate indentation.
2212 Return nil if inside string, t if in a comment."
2213 (let* ((stack (and state (car state)))
2214 (token (nth 1 state))
2215 (stack-top (and stack (car stack))))
2216 (cond ((null state) ;No state
2220 (eq (nth 3 state) 'comment))
2222 (if (looking-at "when[^_a-zA-Z0-9]")
2225 ((eq (car stack-top) '\()
2226 ;; Element of list, tuple or part of an expression,
2227 (if (null erlang-argument-indent)
2228 ;; indent to next column.
2229 (1+ (nth 2 stack-top))
2230 (goto-char (nth 1 stack-top))
2231 (cond ((looking-at "[({]\\s *\\($\\|%\\)")
2232 ;; Line ends with parenthesis.
2233 (+ (erlang-indent-find-preceding-expr)
2234 erlang-argument-indent))
2236 ;; Indent to the same column as the first
2238 (goto-char (1+ (nth 1 stack-top)))
2239 (skip-chars-forward " \t")
2240 (current-column)))))
2241 ((eq (car stack-top) 'icr)
2242 ;; The default indentation is the column of the option
2243 ;; directly following the keyword. (This does not apply to
2244 ;; `case'.) Should no option be on the same line, the
2245 ;; indentation is the indentation of the keyword +
2246 ;; `erlang-indent-level'.
2248 ;; `after' should be indented to the save level as the
2249 ;; corresponding receive.
2250 (if (looking-at "after[^_a-zA-Z0-9]")
2253 (goto-char (nth 1 stack-top))
2254 (if (looking-at "case[^_a-zA-Z0-9]")
2255 (+ (nth 2 stack-top) erlang-indent-level)
2256 (skip-chars-forward "a-z")
2257 (skip-chars-forward " \t")
2258 (if (memq (following-char) '(?% ?\n))
2259 (+ (nth 2 stack-top) erlang-indent-level)
2260 (current-column)))))
2261 (if (looking-at "catch[^_a-zA-Z0-9]")
2264 (goto-char (nth 1 stack-top))
2265 (if (looking-at "case[^_a-zA-Z0-9]")
2266 (+ (nth 2 stack-top) erlang-indent-level)
2267 (skip-chars-forward "a-z")
2268 (skip-chars-forward " \t")
2269 (if (memq (following-char) '(?% ?\n))
2270 (+ (nth 2 stack-top) erlang-indent-level)
2271 (current-column)))))
2273 ;; Real indentation, where operators create extra indentation etc.
2274 ((memq (car stack-top) '(-> || begin))
2275 (goto-char (nth 1 stack-top))
2276 ;; Check if there is more code after the '->' on the
2277 ;; same line. If so use this indentation as base, else
2278 ;; use parent indentation + 2 * level as base.
2279 (let ((off erlang-indent-level)
2281 (cond ((null (cdr stack))) ; Top level in function.
2282 ((eq (car stack-top) 'begin)
2284 ((eq (car stack-top) '->)
2285 (setq off (* 2 erlang-indent-level))))
2286 (let ((base (erlang-indent-find-base stack indent-point off skip)))
2287 ;; Look at last thing to see how we are to move relative
2290 (cond ((looking-at "||\\|,\\|->\\|:-")
2292 ((erlang-at-keyword)
2293 (+ (current-column) erlang-indent-level))
2294 ((or (= (char-syntax (following-char)) ?.)
2295 (erlang-at-operator))
2296 (+ base erlang-indent-level))
2298 (goto-char indent-point)
2299 (cond ((memq (following-char) '(?\( ?{))
2300 ;; Function application or record.
2301 (+ (erlang-indent-find-preceding-expr)
2302 erlang-argument-indent))
2303 ;; Empty line, or end; treat it as the end of
2304 ;; the block. (Here we have a choice: should
2305 ;; the user be forced to reindent continued
2306 ;; lines, or should the "end" be reindented?)
2307 ((looking-at "\\(end\\|after\\|catch\\)[^_a-zA-Z0-9]\\|$")
2308 (if (eq (car (car stack)) '->)
2311 (erlang-caddr (car stack))
2313 ;; Avoid treating comments a continued line.
2314 ((= (following-char) ?%)
2316 ;; Continued line (e.g. line beginning
2317 ;; with an operator.)
2318 (t (+ base erlang-indent-level)))))))
2320 ((eq (car stack-top) 'when)
2321 (goto-char (nth 1 stack-top))
2322 (if (looking-at "when\\s *\\($\\|%\\)")
2325 (if (and stack (eq (nth 0 (car stack)) 'icr))
2327 (goto-char (nth 1 (car stack)))
2328 (+ (nth 2 (car stack)) erlang-indent-guard
2329 ;; receive XYZ or receive
2331 (if (looking-at "[a-z]+\\s *\\($\\|%\\)")
2333 (* 2 erlang-indent-level))))
2334 erlang-indent-guard))
2335 ;; "when" is followed by code, let's indent to the same
2337 (forward-char 4) ; Skip "when"
2338 (skip-chars-forward " \t")
2339 (current-column))))))
2342 (defun erlang-indent-find-base (stack indent-point &optional offset skip)
2343 "Find the base column for current stack."
2344 (or skip (setq skip 2))
2345 (or offset (setq offset erlang-indent-level))
2347 (let* ((stack-top (car stack)))
2348 (goto-char (nth 1 stack-top))
2350 (if (looking-at "\\s *\\($\\|%\\)")
2352 (if (memq (car stack-top) '(-> ||))
2354 ;; Take parent identation + offset,
2355 ;; else just erlang-indent-level if no parent
2357 (+ (erlang-caddr (car stack))
2359 erlang-indent-level))
2360 (erlang-skip-blank indent-point)
2361 (current-column)))))
2364 ;; Does not handle `begin' .. `end'.
2365 (defun erlang-indent-find-preceding-expr ()
2366 "Return the first column of the preceding expression.
2367 This assumes that the preceding expression is either simple
2368 \(i.e. an atom) or parenthesized."
2371 (let ((col (current-column)))
2372 (skip-chars-backward " \t")
2373 ;; Needed to match the colon in "'foo':'bar'".
2374 (if (not (memq (preceding-char) '(?# ?:)))
2378 (current-column)))))
2381 (defun erlang-skip-blank (&optional lim)
2382 "Skip over whitespace and comments until limit reached."
2383 (or lim (setq lim (point-max)))
2385 (while (and (not stop) (< (point) lim))
2386 (cond ((= (following-char) ?%)
2387 (skip-chars-forward "^\n" lim))
2388 ((= (following-char) ?\n)
2389 (skip-chars-forward "\n" lim))
2390 ((looking-at "\\s ")
2391 (if (re-search-forward "\\S " lim 'move)
2397 (defun erlang-at-keyword ()
2398 "Are we looking at an Erlang keyword which will increase indentation?"
2399 (looking-at (concat "\\(when\\|if\\|fun\\|case\\|begin\\|query\\|"
2400 "of\\|receive\\|after\\|catch\\)[^_a-zA-Z0-9]")))
2402 (defun erlang-at-operator ()
2403 "Are we looking at an Erlang operator?"
2405 "\\(bnot\\|div\\|mod\\|band\\|bor\\|bxor\\|bsl\\|bsr\\)[^_a-zA-Z0-9]"))
2407 (defun erlang-comment-indent ()
2408 "Compute Erlang comment indentation.
2410 Used both by `indent-for-comment' and the Erlang specific indentation
2412 (cond ((looking-at "%%%") 0)
2414 (or (erlang-calculate-indent)
2415 (current-indentation)))
2418 (skip-chars-backward " \t")
2419 (max (if (bolp) 0 (1+ (current-column)))
2422 ;;; Erlang movement commands
2424 ;; All commands below work as movement commands. I.e. if the point is
2425 ;; at the end of the clause, and the command `erlang-end-of-clause' is
2426 ;; executed, the point is moved to the end of the NEXT clause. (This
2427 ;; mimics the behaviour of `end-of-defun'.)
2429 ;; Personally I would like to rewrite them to be "pure", and add a set
2430 ;; of movement functions, like `erlang-next-clause',
2431 ;; `erlang-previous-clause', and the same for functions.
2433 ;; The current implementation makes it hopeless to use the functions as
2434 ;; subroutines in more complex commands. /andersl
2436 (defun erlang-beginning-of-clause (&optional arg)
2437 "Move backward to previous start of clause.
2438 With argument, do this that many times.
2439 Return t unless search stops due to end of buffer."
2441 (or arg (setq arg 1))
2443 ;; Step back to the end of the previous line, unless we are at
2444 ;; the beginning of the buffer. The reason for this move is
2445 ;; that the regexp below includes the last character of the
2448 (or (looking-at "\n")
2451 (if (looking-at "\\`\n")
2453 ;; The regexp matches a function header that isn't
2454 ;; included in a string.
2455 (and (re-search-forward "\\(\\`\\|\\`\n\\|[^\\]\n\\)\\([a-z]\\|'\\|-\\)"
2457 (let ((beg (match-beginning 2)))
2458 (and beg (goto-char beg))
2461 (defun erlang-end-of-clause (&optional arg)
2462 "Move to the end of the current clause.
2463 With argument, do this that many times."
2465 (or arg (setq arg 1))
2466 (while (and (looking-at "[ \t]*[%\n]")
2467 (zerop (forward-line 1))))
2468 ;; Move to the next clause.
2469 (erlang-beginning-of-clause (- arg))
2470 (beginning-of-line);; Just to be sure...
2472 (while (and (not (bobp)) continue)
2474 (skip-chars-forward " \t")
2475 (if (looking-at "[%\n]")
2478 (setq continue nil)))))
2480 (defun erlang-mark-clause ()
2481 "Put mark at end of clause, point at beginning."
2484 (erlang-end-of-clause 1)
2485 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2488 (push-mark (point) nil t)
2489 (error (push-mark (point))))
2490 (erlang-beginning-of-clause 1)
2491 ;; The above function deactivates the mark.
2492 (if (boundp 'deactivate-mark)
2493 (funcall (symbol-function 'set) 'deactivate-mark nil)))
2495 (defun erlang-beginning-of-function (&optional arg)
2496 "Move backward to previous start of function.
2497 With positive argument, do this that many times.
2498 With negative argument, search forward.
2500 Return t unless search stops due to end of buffer."
2502 (or arg (setq arg 1))
2506 (while (and (> arg 0)
2507 (and (erlang-beginning-of-clause 1)
2508 (let ((start (point))
2509 (name (erlang-name-of-function))
2510 (arity (erlang-get-function-arity)))
2511 ;; Note: "arity" is nil for e.g. "-import", hence
2512 ;; two "-import" clauses are not considered to
2513 ;; be part of the same function.
2514 (while (and (erlang-beginning-of-clause 1)
2516 (erlang-name-of-function))
2519 (erlang-get-function-arity)))
2520 (setq start (point)))
2523 (setq arg (1- arg))))
2527 (erlang-beginning-of-clause 1)
2528 ;; Step -arg functions forward.
2529 (while (and (< arg 0)
2530 ;; Step one function forward, or stop if the end of
2531 ;; the buffer was reached. Return t if we found the
2533 (let ((name (erlang-name-of-function))
2534 (arity (erlang-get-function-arity))
2535 (found (erlang-beginning-of-clause -1)))
2537 (string-equal name (erlang-name-of-function))
2540 (erlang-get-function-arity)))
2541 (setq found (erlang-beginning-of-clause -1)))
2543 (setq arg (1+ arg)))))
2547 (defun erlang-end-of-function (&optional arg)
2548 "Move forward to next end of function.
2550 With argument, do this that many times.
2551 With negative argument go towards the beginning of the buffer."
2553 (or arg (setq arg 1))
2556 (while (and (> arg 0) (< (point) (point-max)))
2557 (let ((pos (point)))
2562 (erlang-beginning-of-clause 1)))
2564 (or (bobp) (forward-char -1))
2565 (erlang-beginning-of-clause -1))
2567 (erlang-pass-over-function)
2568 (skip-chars-forward " \t")
2569 (if (looking-at "[%\n]")
2572 (setq arg (1- arg)))
2575 (let ((pos (point)))
2576 (erlang-beginning-of-clause 1)
2577 (erlang-pass-over-function)
2579 (if (>= (point) pos)
2580 (if (erlang-beginning-of-function 2)
2582 (erlang-pass-over-function)
2583 (skip-chars-forward " \t")
2584 (if (looking-at "[%\n]")
2586 (goto-char (point-min)))))
2587 (setq arg (1+ arg)))))
2590 (if (default-boundp 'beginning-of-defun-function)
2591 (defalias 'erlang-mark-function 'mark-defun)
2592 (defun erlang-mark-function ()
2593 "Put mark at end of function, point at beginning."
2596 (erlang-end-of-function 1)
2597 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2600 (push-mark (point) nil t)
2601 (error (push-mark (point))))
2602 (erlang-beginning-of-function 1)
2603 ;; The above function deactivates the mark.
2604 (if (boundp 'deactivate-mark)
2605 (funcall (symbol-function 'set) 'deactivate-mark nil)))))
2607 (defun erlang-pass-over-function ()
2610 (and (not (looking-at "\\.\\(\\s \\|\n\\|\\s<\\)"))
2616 (defun erlang-name-of-function ()
2618 ;; Skip over attribute leader.
2619 (if (looking-at "-[ \t]*")
2620 (re-search-forward "-[ \t]*" nil 'move))
2621 (let ((start (point)))
2623 (buffer-substring start (point)))))
2628 (defun erlang-fill-paragraph (&optional justify)
2629 "Like \\[fill-paragraph], but handle Erlang comments.
2630 If any of the current line is a comment, fill the comment or the
2631 paragraph of it that point is in, preserving the comment's indentation
2634 (let ((has-comment nil)
2635 ;; If has-comment, the appropriate fill-prefix for the comment.
2636 comment-fill-prefix)
2637 ;; Figure out what kind of comment we are looking at.
2641 ;; Find the command prefix.
2642 ((looking-at (concat "\\s *" comment-start-skip))
2643 (setq has-comment t)
2644 (setq comment-fill-prefix (buffer-substring (match-beginning 0)
2646 ;; A line with some code, followed by a comment? Remember that the
2647 ;; % which starts the comment shouldn't be part of a string or
2650 (while (not (looking-at "%\\|$"))
2651 (skip-chars-forward "^%\n\"\\\\")
2653 ((eq (char-after (point)) ?\\) (forward-char 2))
2654 ((eq (char-after (point)) ?\") (forward-sexp 1))))
2655 (looking-at comment-start-skip))
2656 (setq has-comment t)
2657 (setq comment-fill-prefix
2658 (concat (make-string (current-column) ? )
2659 (buffer-substring (match-beginning 0) (match-end 0)))))))
2660 (if (not has-comment)
2661 (fill-paragraph justify)
2662 ;; Narrow to include only the comment, and then fill the region.
2665 ;; Find the first line we should include in the region to fill.
2667 (while (and (zerop (forward-line -1))
2668 (looking-at "^\\s *%")))
2669 ;; We may have gone to far. Go forward again.
2670 (or (looking-at "^\\s *%")
2673 ;; Find the beginning of the first line past the region to fill.
2675 (while (progn (forward-line 1)
2676 (looking-at "^\\s *%")))
2678 ;; Lines with only % on them can be paragraph boundaries.
2679 (let ((paragraph-start (concat paragraph-start "\\|^[ \t%]*$"))
2680 (paragraph-separate (concat paragraph-start "\\|^[ \t%]*$"))
2681 (fill-prefix comment-fill-prefix))
2682 (fill-paragraph justify))))))
2685 (defun erlang-uncomment-region (beg end)
2686 "Uncomment all commented lines in the region."
2688 (comment-region beg end -1))
2691 (defun erlang-generate-new-clause ()
2692 "Create additional Erlang clause header.
2694 Parses the source file for the name of the current Erlang function.
2695 Create the header containing the name, A pair of parentheses,
2696 and an arrow. The space between the function name and the
2697 first parenthesis is preserved. The point is placed between
2700 (let ((name (save-excursion
2701 (and (erlang-beginning-of-clause)
2702 (erlang-get-function-name t))))
2703 (arrow (save-excursion
2704 (and (erlang-beginning-of-clause)
2705 (erlang-get-function-arrow)))))
2706 (if (or (null arrow) (null name))
2707 (error "Can't find name of current Erlang function"))
2708 (if (and (bolp) (eolp))
2714 (insert ") " arrow))
2715 (if erlang-new-clause-with-arguments
2716 (erlang-clone-arguments))))
2719 (defun erlang-clone-arguments ()
2720 "Insert, at the point, the argument list of the previous clause.
2722 The mark is set at the beginning of the inserted text, the point
2725 (let ((args (save-excursion
2727 (and (erlang-beginning-of-clause)
2728 (erlang-get-function-arguments))))
2731 (error "Can't clone argument list"))
2735 ;;; Information retrieval functions.
2737 (defun erlang-buffer-substring (beg end)
2738 "Like `buffer-substring-no-properties'.
2739 Although, this function works on all versions of Emacs."
2740 (if (fboundp 'buffer-substring-no-properties)
2741 (funcall (symbol-function 'buffer-substring-no-properties) beg end)
2742 (buffer-substring beg end)))
2745 (defun erlang-get-module ()
2746 "Return the name of the module as specified by `-module'.
2748 Return nil if file contains no `-module' attribute."
2752 (goto-char (point-min))
2753 (let ((md (match-data)))
2755 (if (re-search-forward
2757 (concat "^-module\\s *(\\s *\\(\\("
2759 "\\)?\\)\\s *)\\s *\\."))
2761 (erlang-remove-quotes
2762 (erlang-buffer-substring (match-beginning 1)
2765 (store-match-data md))))))
2768 (defun erlang-get-module-from-file-name (&optional file)
2769 "Extract the module name from a file name.
2771 First, the directory part is removed. Second, the part of the file name
2772 matching `erlang-file-name-extension-regexp' is removed.
2774 Should the match fail, nil is returned.
2776 By modifying `erlang-file-name-extension-regexp' to match files other
2777 than Erlang source files, Erlang specific functions could be applied on
2778 non-Erlang files. Most notably; the support for Erlang modules in the
2779 tags system could be used by files written in other languages."
2780 (or file (setq file buffer-file-name))
2783 (setq file (file-name-nondirectory file))
2784 (if (string-match erlang-file-name-extension-regexp file)
2785 (substring file 0 (match-beginning 0))
2789 ;; Used by `erlang-get-export' and `erlang-get-import'.
2791 (defun erlang-get-function-arity-list ()
2792 "Parse list of `function/arity' as used by `-import' and `-export'.
2794 Point must be before the opening bracket. When the
2795 function returns the point will be placed after the closing bracket.
2797 The function does not return an error if the list is incorrectly
2800 Return list of (function . arity). The order of the returned list
2801 corresponds to the order of the parsed Erlang list."
2805 (if (not (eq (preceding-char) ?\[))
2806 '() ; Not looking at an Erlang list.
2807 (while ; Note: `while' has no body.
2810 (and (looking-at (eval-when-compile
2811 (concat erlang-atom-regexp "/\\([0-9]+\\)\\>")))
2815 (erlang-remove-quotes
2816 (erlang-buffer-substring
2817 (match-beginning 1) (match-end 1)))
2819 (erlang-buffer-substring
2821 (+ 1 erlang-atom-regexp-matches))
2823 (+ 1 erlang-atom-regexp-matches)))))
2825 (goto-char (match-end 0))
2828 ;; Test if there are more exported functions.
2829 (eq (preceding-char) ?,))))))
2833 ;;; Note that `-export' and the open parenthesis must be written on
2836 (defun erlang-get-export ()
2837 "Return a list of `(function . arity)' as specified by `-export'."
2839 (goto-char (point-min))
2840 (let ((md (match-data))
2844 (while (re-search-forward "^-export\\s *(" (point-max) t)
2846 (setq res (nconc res (erlang-get-function-arity-list))))
2848 (store-match-data md)))))
2851 (defun erlang-get-import ()
2852 "Parse an Erlang source file for imported functions.
2854 Return an alist with module name as car part and list of conses containing
2855 function and arity as cdr part."
2857 (goto-char (point-min))
2858 (let ((md (match-data))
2862 (while (re-search-forward "^-import\\s *(" (point-max) t)
2864 (if (looking-at erlang-atom-regexp)
2865 (let ((module (erlang-remove-quotes
2866 (erlang-buffer-substring
2869 (goto-char (match-end 0))
2871 (if (eq (following-char) ?,)
2875 (let ((funcs (erlang-get-function-arity-list))
2876 (pair (assoc module res)))
2878 (setcdr pair (nconc (cdr pair) funcs))
2879 (setq res (cons (cons module funcs)
2882 (store-match-data md)))))
2885 (defun erlang-get-function-name (&optional arg)
2886 "Return name of current function, or nil.
2888 If optional argument is non-nil, everything up to and including
2889 the first `(' is returned.
2891 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2893 (if (not (eobp)) (forward-char 1))
2894 (and (erlang-beginning-of-clause)
2895 (erlang-get-function-name t)))"
2896 (let ((n (if arg 0 1)))
2897 (and (looking-at (eval-when-compile
2898 (concat "^" erlang-atom-regexp "\\s *(")))
2899 (erlang-buffer-substring (match-beginning n) (match-end n)))))
2902 (defun erlang-get-function-arrow ()
2903 "Return arrow of current function, could be \"->\", \":-\" or nil.
2905 The \":-\" arrow is used by mnesia queries.
2907 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2909 (if (not (eobp)) (forward-char 1))
2910 (and (erlang-beginning-of-clause)
2911 (erlang-get-function-arrow)))"
2914 (re-search-forward "[^-:]*-\\|:" (point-max) t)
2915 (erlang-buffer-substring (- (point) 1) (+ (point) 1)))))
2917 (defun erlang-get-function-arity ()
2918 "Return the number of arguments of function at point, or nil."
2919 (and (looking-at (eval-when-compile
2920 (concat "^" erlang-atom-regexp "\\s *(")))
2922 (goto-char (match-end 0))
2930 ((looking-at "\\s *)")
2932 ((looking-at "\\s *\\($\\|%\\)")
2934 ((looking-at "\\s *,")
2935 (setq res (+ 1 res))
2936 (goto-char (match-end 0)))
2939 (setq res (+ 1 res)))
2944 (defun erlang-get-function-arguments ()
2945 "Return arguments of current function, or nil."
2946 (if (not (looking-at (eval-when-compile
2947 (concat "^" erlang-atom-regexp "\\s *("))))
2951 (let ((start (match-end 0)))
2952 (goto-char (- start 1))
2954 (erlang-buffer-substring start (- (point) 1)))
2958 (defun erlang-get-function-under-point ()
2959 "Return the module and function under the point, or nil.
2961 Should no explicit module name be present at the point, the
2962 list of imported functions is searched.
2964 The following could be returned:
2965 (\"module\" \"function\") -- Both module and function name found.
2966 (nil \"function\") -- No module name was found.
2967 nil -- No function name found
2969 In the future the list may contain more elements."
2971 (let ((md (match-data))
2973 (if (eq (char-syntax (following-char)) ? )
2974 (skip-chars-backward " \t"))
2975 (skip-chars-backward "a-zA-Z0-9_:'")
2976 (cond ((looking-at (eval-when-compile
2977 (concat erlang-atom-regexp ":" erlang-atom-regexp)))
2979 (erlang-remove-quotes
2980 (erlang-buffer-substring
2981 (match-beginning 1) (match-end 1)))
2982 (erlang-remove-quotes
2983 (erlang-buffer-substring
2984 (match-beginning (1+ erlang-atom-regexp-matches))
2985 (match-end (1+ erlang-atom-regexp-matches)))))))
2986 ((looking-at erlang-atom-regexp)
2987 (let ((fk (erlang-remove-quotes
2988 (erlang-buffer-substring
2989 (match-beginning 0) (match-end 0))))
2991 (imports (erlang-get-import)))
2992 (while (and imports (null mod))
2993 (if (assoc fk (cdr (car imports)))
2994 (setq mod (car (car imports)))
2995 (setq imports (cdr imports))))
2996 (setq res (list mod fk)))))
2997 (store-match-data md)
3001 ;; TODO: Escape single quotes inside the string without
3002 ;; replace-regexp-in-string.
3003 (defun erlang-add-quotes-if-needed (str)
3004 "Return STR, possibly with quotes."
3005 (if (and (stringp str)
3006 (not (string-match (eval-when-compile
3007 (concat "\\`" erlang-atom-regexp "\\'")) str)))
3008 (progn (if (fboundp 'replace-regexp-in-string)
3009 (setq str (replace-regexp-in-string "'" "\\'" str t t )))
3010 (concat "'" str "'"))
3014 (defun erlang-remove-quotes (str)
3015 "Return STR without quotes, if present."
3016 (let ((md (match-data)))
3018 (if (string-match "\\`'\\(.*\\)'\\'" str)
3019 (substring str 1 -1)
3021 (store-match-data md))))
3024 ;;; Check module name
3026 ;; The function `write-file', bound to C-x C-w, calls
3027 ;; `set-visited-file-name' which clears the hook. :-(
3028 ;; To make sure that the hook always is present, we advise
3029 ;; `set-visited-file-name'.
3030 (defun erlang-check-module-name-init ()
3031 "Initialize the functionality to compare file and module names.
3033 Unless we have `before-save-hook', we redefine the function
3034 `set-visited-file-name' since it clears the variable
3035 `local-write-file-hooks'. The original function definition is
3036 stored in `erlang-orig-set-visited-file-name'."
3037 (if (boundp 'before-save-hook)
3038 ;; If we have that, `make-local-hook' is obsolete.
3039 (add-hook 'before-save-hook 'erlang-check-module-name nil t)
3041 (unless (ad-advised-definition-p 'set-visited-file-name)
3042 (defadvice set-visited-file-name (after erlang-set-visited-file-name
3044 (if (eq major-mode 'erlang-mode)
3045 (add-hook 'local-write-file-hooks 'erlang-check-module-name))))
3046 (add-hook 'local-write-file-hooks 'erlang-check-module-name)))
3049 (defun erlang-check-module-name ()
3050 "If the module name doesn't match file name, ask for permission to change.
3052 The variable `erlang-check-module-name' controls the behaviour of this
3053 function. It it is nil, this function does nothing. If it is t, the
3054 source is silently changed. If it is set to the atom `ask', the user
3057 This function is normally placed in the hook `local-write-file-hooks'."
3058 (if erlang-check-module-name
3059 (let ((mn (erlang-get-module))
3060 (fn (erlang-get-module-from-file-name (buffer-file-name))))
3061 (if (and (stringp mn) (stringp fn))
3062 (or (string-equal mn fn)
3063 (if (or (eq erlang-check-module-name t)
3065 "Module does not match file name. Modify source? "))
3069 (goto-char (point-min))
3070 (if (re-search-forward
3072 (concat "^-module\\s *(\\s *\\(\\("
3074 "\\)?\\)\\s *)\\s *\\."))
3077 (goto-char (match-beginning 1))
3078 (delete-region (match-beginning 1)
3080 (insert fn))))))))))
3081 ;; Must return nil since it is added to `local-write-file-hook'.
3085 ;;; Electric functions.
3087 (defun erlang-electric-semicolon (&optional arg)
3088 "Insert a semicolon character and possibly a prototype for the next line.
3090 The variable `erlang-electric-semicolon-criteria' states a criterion,
3091 when fulfilled a newline is inserted, the next line is indented and a
3092 prototype for the next line is inserted. Normally the prototype
3093 consists of \" ->\". Should the semicolon end the clause a new clause
3094 header is generated.
3096 The variable `erlang-electric-semicolon-insert-blank-lines' controls
3097 the number of blank lines inserted between the current line and new
3100 Behaves just like the normal semicolon when supplied with a
3101 numerical arg, point is inside string or comment, or when there are
3102 non-whitespace characters following the point on the current line."
3104 (self-insert-command (prefix-numeric-value arg))
3106 (and (listp erlang-electric-commands)
3107 (not (memq 'erlang-electric-semicolon
3108 erlang-electric-commands)))
3110 (not (looking-at "\\s *\\(%.*\\)?$"))
3111 (null (erlang-test-criteria-list
3112 erlang-electric-semicolon-criteria)))
3113 (setq erlang-electric-newline-inhibit nil)
3114 (setq erlang-electric-newline-inhibit t)
3118 (if (condition-case nil
3119 (progn (erlang-indent-line) t)
3120 (error (if (bolp) (delete-backward-char 1))))
3126 (erlang-generate-new-clause)
3127 (if erlang-electric-semicolon-insert-blank-lines
3131 erlang-electric-semicolon-insert-blank-lines))))
3132 (error (if (bolp) (delete-backward-char 1))))))))
3135 (defun erlang-electric-comma (&optional arg)
3136 "Insert a comma character and possibly a new indented line.
3137 The variable `erlang-electric-comma-criteria' states a criterion,
3138 when fulfilled a newline is inserted and the next line is indented.
3140 Behaves just like the normal comma when supplied with a
3141 numerical arg, point is inside string or comment, or when there are
3142 non-whitespace characters following the point on the current line."
3145 (self-insert-command (prefix-numeric-value arg))
3148 (and (listp erlang-electric-commands)
3149 (not (memq 'erlang-electric-comma erlang-electric-commands)))
3151 (not (looking-at "\\s *\\(%.*\\)?$"))
3152 (null (erlang-test-criteria-list
3153 erlang-electric-comma-criteria)))
3154 (setq erlang-electric-newline-inhibit nil)
3155 (setq erlang-electric-newline-inhibit t)
3160 (erlang-indent-line)
3161 (error (if (bolp) (delete-backward-char 1))))))
3163 (defun erlang-electric-lt (&optional arg)
3164 "Insert a less-than sign, and optionally mark it as an open paren."
3168 (self-insert-command arg)
3170 ;; Was this the second char in bit-syntax open (`<<')?
3171 (unless (< (point) 2)
3174 (when (and (eq (char-after (point)) ?<)
3175 (not (eq (get-text-property (point) 'category)
3176 'bitsyntax-open-inner)))
3177 ;; Then mark the two chars...
3178 (put-text-property (point) (1+ (point))
3179 'category 'bitsyntax-open-outer)
3181 (put-text-property (point) (1+ (point))
3182 'category 'bitsyntax-open-inner)
3183 ;;...and unmark any subsequent less-than chars.
3185 (while (eq (char-after (point)) ?<)
3186 (remove-text-properties (point) (1+ (point))
3188 (forward-char 1))))))
3190 (defun erlang-after-bitsyntax-close ()
3191 "Return t if point is immediately after a bit-syntax close parenthesis (`>>')."
3195 (and (eq (char-after (point)) ?>)
3196 (not (eq (get-text-property (point) 'category)
3197 'bitsyntax-close-outer))))))
3199 (defun erlang-after-arrow ()
3200 "Return true if point is immediately after a function arrow (`->')."
3205 (eq (char-before (point)) ?-))
3206 (or (not (listp erlang-electric-commands))
3207 (memq 'erlang-electric-gt
3208 erlang-electric-commands))
3209 (not (erlang-in-literal))
3210 (looking-at "\\s *\\(%.*\\)?$")
3211 (erlang-test-criteria-list erlang-electric-arrow-criteria))))
3214 (defun erlang-electric-gt (&optional arg)
3215 "Insert a greater-than sign, and optionally mark it as a close paren."
3219 (self-insert-command arg)
3222 ;; Did we just write a bit-syntax close (`>>')?
3223 ((erlang-after-bitsyntax-close)
3225 ;; Then mark the two chars...
3227 (put-text-property (point) (1+ (point))
3228 'category 'bitsyntax-close-inner)
3230 (put-text-property (point) (1+ (point))
3231 'category 'bitsyntax-close-outer)
3232 ;;...and unmark any subsequent greater-than chars.
3234 (while (eq (char-after (point)) ?>)
3235 (remove-text-properties (point) (1+ (point))
3239 ;; Did we just write a function arrow (`->')?
3240 ((erlang-after-arrow)
3241 (let ((erlang-electric-newline-inhibit t))
3246 (erlang-indent-line)
3247 (error (if (bolp) (delete-backward-char 1))))))
3249 ;; Then it's just a plain greater-than.
3254 (defun erlang-electric-arrow\ off (&optional arg)
3255 "Insert a '>'-sign and possibly a new indented line.
3257 This command is only `electric' when the `>' is part of an `->' arrow.
3258 The variable `erlang-electric-arrow-criteria' states a sequence of
3259 criteria, which decides when a newline should be inserted and the next
3262 It behaves just like the normal greater than sign when supplied with a
3263 numerical arg, point is inside string or comment, or when there are
3264 non-whitespace characters following the point on the current line.
3266 After being split/merged into `erlang-after-arrow' and
3267 `erlang-electric-gt', it is now unused and disabled."
3269 (let ((prec (preceding-char)))
3270 (self-insert-command (prefix-numeric-value arg))
3272 (and (listp erlang-electric-commands)
3273 (not (memq 'erlang-electric-arrow
3274 erlang-electric-commands)))
3277 (not (looking-at "\\s *\\(%.*\\)?$"))
3278 (null (erlang-test-criteria-list
3279 erlang-electric-arrow-criteria)))
3280 (setq erlang-electric-newline-inhibit nil)
3281 (setq erlang-electric-newline-inhibit t)
3286 (erlang-indent-line)
3287 (error (if (bolp) (delete-backward-char 1)))))))
3290 (defun erlang-electric-newline (&optional arg)
3291 "Break line at point and indent, continuing comment if within one.
3292 The variable `erlang-electric-newline-criteria' states a criterion,
3293 when fulfilled a newline is inserted and the next line is indented.
3295 Should the current line begin with a comment, and the variable
3296 `comment-multi-line' be non-nil, a new comment start is inserted.
3298 Should the previous command be another electric command we assume that
3299 the user pressed newline out of old habit, hence we will do nothing."
3301 (cond ((and (not arg)
3302 erlang-electric-newline-inhibit
3303 (memq last-command erlang-electric-newline-inhibit-list))
3306 (and (listp erlang-electric-commands)
3307 (not (memq 'erlang-electric-newline
3308 erlang-electric-commands)))
3309 (null (erlang-test-criteria-list
3310 erlang-electric-newline-criteria)))
3311 (newline (prefix-numeric-value arg)))
3313 (if (and comment-multi-line
3316 (looking-at (concat "\\s *" comment-start-skip))))
3317 (let ((str (buffer-substring
3318 (or (match-end 1) (match-beginning 0))
3319 (min (match-end 0) (point)))))
3325 (indent-according-to-mode)))))
3328 (defun erlang-test-criteria-list (criteria)
3329 "Given a list of criterion functions, test if criteria are fulfilled.
3331 Each element in the criteria list can a function returning nil, t or
3332 the atom `stop'. t means that the criterion is fulfilled, `stop' means
3333 that it isn't fulfilled and that the search should stop,
3334 and nil means continue searching.
3336 Should the list contain the atom t the criterion is assumed to be
3337 fulfilled, unless preceded by a function returning `stop', of course.
3339 Should the argument be the atom t instead of a list, the criterion is
3340 assumed to be trivially true.
3342 Should all functions return nil, the criteria are assumed not to be
3345 Return t if criteria fulfilled, nil otherwise."
3350 (while (and criteria (null answer))
3351 (if (eq (car criteria) t)
3353 (setq answer (funcall (car criteria))))
3354 (setq criteria (cdr criteria)))
3355 (if (and answer (not (eq answer 'stop)))
3360 (defun erlang-in-literal (&optional lim)
3361 "Test if point is in string, quoted atom or comment.
3363 Return one of the three atoms `atom', `string', and `comment'.
3364 Should the point be inside none of the above mentioned types of
3365 context, nil is returned."
3367 (let* ((lim (or lim (save-excursion
3368 (erlang-beginning-of-clause)
3370 (state (if (fboundp 'syntax-ppss) ; post Emacs 21.3
3372 (parse-partial-sexp lim (point)))))
3374 ((eq (nth 3 state) ?') 'atom)
3375 ((nth 3 state) 'string)
3376 ((nth 4 state) 'comment)
3380 (defun erlang-at-end-of-function-p ()
3381 "Test if point is at end of an Erlang function.
3383 This function is designed to be a member of a criteria list."
3384 (eq (save-excursion (erlang-skip-blank) (point))
3386 (erlang-beginning-of-function -1) (point))))
3389 (defun erlang-stop-when-inside-argument-list ()
3390 "Return `stop' if inside parenthesis list, nil otherwise.
3392 Knows about the list comprehension syntax. When the point is
3393 after `||', `stop' is not returned.
3395 This function is designed to be a member of a criteria list."
3398 (let ((orig-point (point))
3401 (if (not (eq (following-char) ?\[))
3403 ;; Do not return `stop' when inside a list comprehension
3404 ;; construction. (The point must be after `||').
3405 (while (< (point) orig-point)
3406 (setq state (erlang-partial-parse (point) orig-point state)))
3407 (if (and (car state) (eq (car (car (car state))) '||))
3414 (defun erlang-stop-when-at-guard ()
3415 "Return `stop' when at function guards.
3417 This function is designed to be a member of a criteria list."
3420 (if (and (looking-at (eval-when-compile
3421 (concat "^" erlang-atom-regexp "\\s *(")))
3424 (concat "^" erlang-atom-regexp ".*\\(->\\|:-\\)")))))
3429 (defun erlang-next-lines-empty-p ()
3430 "Return non-nil if next lines are empty.
3432 The variable `erlang-next-lines-empty-threshold' contains the number
3433 of lines required to be empty.
3435 A line containing only spaces and tabs is considered empty.
3437 This function is designed to be a member of a criteria list."
3438 (and erlang-next-lines-empty-threshold
3440 (let ((left erlang-next-lines-empty-threshold)
3442 (while (and cont (> left 0))
3444 (setq cont (looking-at "\\s *$"))
3445 (setq left (- left 1)))
3449 (defun erlang-at-keyword-end-p ()
3450 "Test if next readable token is the keyword end.
3452 This function is designed to be a member of a criteria list."
3455 (looking-at "end[^_a-zA-Z0-9]")))
3458 ;; Erlang tags support which is aware of erlang modules.
3460 ;; Not yet implemented under XEmacs. (Hint: The Emacs 19 etags
3461 ;; package works under XEmacs.)
3464 (if (or (featurep 'bytecomp)
3465 (featurep 'byte-compile))
3472 (defvar erlang-tags-function-alist
3473 '((find-tag . erlang-find-tag)
3474 (find-tag-other-window . erlang-find-tag-other-window)
3475 (find-tag-regexp . erlang-find-tag-regexp)
3476 (find-tag-other-frame . erlang-find-tag-other-frame))
3477 "Alist of old tags commands and the replacement functions.")
3479 (defvar erlang-tags-installed nil
3480 "Non-nil when the Erlang tags system is installed.")
3481 (defvar erlang-tags-file-list '()
3482 "List of files in tag list. Used when finding tag on form `module:'.")
3483 (defvar erlang-tags-completion-table nil
3484 "Like `tags-completion-table', this table contains `tag' and `module:tag'.")
3485 (defvar erlang-tags-buffer-installed-p nil
3486 "Non-nil when Erlang module recognising functions installed.")
3487 (defvar erlang-tags-buffer-list '()
3488 "Temporary list of buffers.")
3489 (defvar erlang-tags-orig-completion-table nil
3490 "Temporary storage for `tags-completion-table'.")
3491 (defvar erlang-tags-orig-tag-order nil
3492 "Temporary storage for `find-tag-tag-order'.")
3493 (defvar erlang-tags-orig-regexp-tag-order nil
3494 "Temporary storage for `find-tag-regexp-tag-order'.")
3495 (defvar erlang-tags-orig-search-function nil
3496 "Temporary storage for `find-tag-search-function'.")
3497 (defvar erlang-tags-orig-regexp-search-function nil
3498 "Temporary storage for `find-tag-regexp-search-function'.")
3499 (defvar erlang-tags-orig-format-hooks nil
3500 "Temporary storage for `tags-table-format-hooks'.")
3502 (defun erlang-tags-init ()
3503 "Install an alternate version of tags, aware of Erlang modules.
3505 After calling this function, the tags functions are aware of
3506 Erlang modules. Tags can be entered on the for `module:tag' as well
3507 as on the old form `tag'.
3509 In the completion list, `module:tag' and `module:' shows up.
3511 Call this function from an appropriate init file, or add it to
3512 Erlang mode hook with the commands:
3513 (add-hook 'erlang-mode-hook 'erlang-tags-init)
3514 (add-hook 'erlang-shell-mode-hook 'erlang-tags-init)
3516 This function only works under Emacs 18 and Emacs 19. Currently, It
3517 is not implemented under XEmacs. (Hint: The Emacs 19 etags module
3518 works under XEmacs.)"
3520 (cond ((= erlang-emacs-major-version 18)
3522 (erlang-tags-define-keys (current-local-map))
3523 (setq erlang-tags-installed t))
3526 ;; Test on a function available in the Emacs 19 version
3527 ;; of tags but not in the XEmacs version.
3528 (if (not (fboundp 'find-tag-noselect))
3530 (erlang-tags-define-keys (current-local-map))
3531 (setq erlang-tags-installed t)))))
3534 ;; Set all keys bound to `find-tag' et.al. in the global map and the
3535 ;; menu to `erlang-find-tag' et.al. in `map'.
3537 ;; The function `substitute-key-definition' does not work properly
3538 ;; in all version of Emacs.
3540 (defun erlang-tags-define-keys (map)
3541 "Bind tags commands to keymap MAP aware of Erlang modules."
3542 (let ((alist erlang-tags-function-alist))
3544 (let* ((old (car (car alist)))
3545 (new (cdr (car alist)))
3546 (keys (append (where-is-internal old global-map))))
3548 (define-key map (car keys) new)
3549 (setq keys (cdr keys))))
3550 (setq alist (cdr alist))))
3552 (erlang-menu-substitute erlang-menu-base-items erlang-tags-function-alist)
3556 ;; There exists a variable `find-tag-default-function'. It is not used
3557 ;; since `complete-tag' uses it to get current word under point. In that
3558 ;; situation we don't want the module to be prepended.
3560 (defun erlang-find-tag-default ()
3561 "Return the default tag.
3562 Search `-import' list of imported functions.
3563 Single quotes are been stripped away."
3564 (let ((mod-func (erlang-get-function-under-point)))
3565 (cond ((null mod-func)
3567 ((null (car mod-func))
3570 (concat (car mod-func) ":" (nth 1 mod-func))))))
3573 ;; Return `t' since it is used inside `tags-loop-form'.
3575 (defun erlang-find-tag (modtagname &optional next-p regexp-p)
3576 "Like `find-tag'. Capable of retrieving Erlang modules.
3578 Tags can be given on the forms `tag', `module:', `module:tag'."
3579 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3580 (switch-to-buffer (erlang-find-tag-noselect modtagname next-p regexp-p))
3584 ;; Code mainly from `find-tag-other-window' in `etags.el'.
3586 (defun erlang-find-tag-other-window (tagname &optional next-p regexp-p)
3587 "Like `find-tag-other-window' but aware of Erlang modules."
3588 (interactive (erlang-tag-interactive
3589 "Find `module:tag' or `tag' other window: "))
3591 ;; This is to deal with the case where the tag is found in the
3592 ;; selected window's buffer; without this, point is moved in both
3593 ;; windows. To prevent this, we save the selected window's point
3594 ;; before doing find-tag-noselect, and restore it afterwards.
3595 (let* ((window-point (window-point (selected-window)))
3596 (tagbuf (erlang-find-tag-noselect tagname next-p regexp-p))
3597 (tagpoint (progn (set-buffer tagbuf) (point))))
3598 (set-window-point (prog1
3600 (switch-to-buffer-other-window tagbuf)
3601 ;; We have to set this new window's point; it
3602 ;; might already have been displaying a
3603 ;; different portion of tagbuf, in which case
3604 ;; switch-to-buffer-other-window doesn't set
3605 ;; the window's point from the buffer.
3606 (set-window-point (selected-window) tagpoint))
3610 (defun erlang-find-tag-other-frame (tagname &optional next-p)
3611 "Like `find-tag-other-frame' but aware of Erlang modules."
3612 (interactive (erlang-tag-interactive
3613 "Find `module:tag' or `tag' other frame: "))
3614 (let ((pop-up-frames t))
3615 (erlang-find-tag-other-window tagname next-p)))
3618 (defun erlang-find-tag-regexp (regexp &optional next-p other-window)
3619 "Like `find-tag-regexp' but aware of Erlang modules."
3620 (interactive (if (fboundp 'find-tag-regexp)
3621 (erlang-tag-interactive
3622 "Find `module:regexp' or `regexp': ")
3623 (error "This version of Emacs can't find tags by regexps")))
3624 (funcall (if other-window
3625 'erlang-find-tag-other-window
3630 ;; Just like C-u M-. This could be added to the menu.
3631 (defun erlang-find-next-tag ()
3632 "Find next tag, like \\[find-tag] with prefix arg."
3634 (let ((current-prefix-arg '(4)))
3635 (if erlang-tags-installed
3636 (call-interactively 'erlang-find-tag)
3637 (call-interactively 'find-tag))))
3640 ;; Mimics `find-tag-noselect' found in `etags.el', but uses `find-tag' to
3641 ;; be compatible with `tags.el'.
3643 ;; Handles three cases:
3644 ;; * `module:' Loop over all possible file names. Stop if a file-name
3645 ;; without extension and directory matches the module.
3648 ;; Emacs 19: Replace test functions with functions aware of
3649 ;; Erlang modules. Tricky because the etags system wasn't
3650 ;; built for these kind of operations...
3652 ;; Emacs 18: We loop over `find-tag' until we find a file
3653 ;; whose module matches the requested module. The
3654 ;; drawback is that a lot of files could be loaded into
3657 ;; * `tag' Just give it to `find-tag'.
3659 (defun erlang-find-tag-noselect (modtagname &optional next-p regexp-p)
3660 "Like `find-tag-noselect' but aware of Erlang modules."
3661 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3663 (setq modtagname (symbol-value 'last-tag)))
3664 (funcall (symbol-function 'set) 'last-tag modtagname)
3665 ;; `tags.el' uses this variable to record how M-, would
3666 ;; know where to restart a tags command.
3667 (if (boundp 'tags-loop-form)
3668 (funcall (symbol-function 'set)
3669 'tags-loop-form '(erlang-find-tag nil t)))
3670 (save-window-excursion
3672 ((string-match ":$" modtagname)
3673 ;; Only the module name was given. Read all files whose file name
3675 (let ((modname (substring modtagname 0 (match-beginning 0)))
3679 (visit-tags-table-buffer)
3680 (setq erlang-tags-file-list
3681 (funcall (symbol-function 'tags-table-files)))))
3683 (or erlang-tags-file-list
3685 (if (and (featurep 'etags)
3687 (symbol-function 'visit-tags-table-buffer) 'same)
3689 (symbol-function 'visit-tags-table-buffer) t))
3690 (setq erlang-tags-file-list
3691 (funcall (symbol-function 'tags-table-files)))
3692 (error "No %stags containing %s" (if next-p "more " "")
3694 (if erlang-tags-file-list
3695 (let ((this-module (erlang-get-module-from-file-name
3696 (car erlang-tags-file-list))))
3697 (if (and (stringp this-module)
3698 (string= modname this-module))
3699 (setq file (car erlang-tags-file-list)))
3700 (setq erlang-tags-file-list (cdr erlang-tags-file-list)))))
3701 (set-buffer (or (get-file-buffer file)
3702 (find-file-noselect file)))))
3704 ((string-match ":" modtagname)
3705 (if (boundp 'find-tag-tag-order)
3706 ;; Method one: Add module-recognising functions to the
3707 ;; list of order functions. However, the tags system
3708 ;; from Emacs 18, and derives thereof (read: XEmacs)
3709 ;; hasn't got this feature.
3711 (erlang-tags-install-module-check)
3713 (funcall (symbol-function 'find-tag)
3714 modtagname next-p regexp-p)
3715 (erlang-tags-remove-module-check)))
3716 ;; Method two: Call the tags system until a file matching
3717 ;; the module is found. This could result in that many
3718 ;; files are read. (e.g. The tag "foo:file" will take a
3719 ;; while to process.)
3720 (let* ((modname (substring modtagname 0 (match-beginning 0)))
3721 (tagname (substring modtagname (match-end 0) nil))
3726 (funcall (symbol-function 'find-tag) tagname next-p regexp-p)
3728 ;; Determine the module form the file name. (The
3729 ;; alternative, to check `-module', would make this
3730 ;; code useless for non-Erlang programs.)
3731 (setq file (erlang-get-module-from-file-name buffer-file-name))
3732 (not (and (stringp file)
3733 (string= modname file))))))))
3735 (funcall (symbol-function 'find-tag) modtagname next-p regexp-p)))
3736 (current-buffer))) ; Return the new buffer.
3739 ;; Process interactive arguments for erlang-find-tag-*.
3741 ;; Negative arguments work only for `etags', not `tags'. This is not
3742 ;; a problem since negative arguments means step back into the
3743 ;; history list, a feature not implemented in `tags'.
3745 (defun erlang-tag-interactive (prompt)
3750 (if current-prefix-arg
3751 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
3754 (let* ((default (erlang-find-tag-default))
3756 (format "%s(default %s) " prompt default)
3758 (spec (if (featurep 'etags)
3759 (completing-read prompt 'erlang-tags-complete-tag)
3760 (read-string prompt))))
3761 (list (if (equal spec "")
3762 (or default (error "There is no default tag"))
3766 ;; Search tag functions which are aware of Erlang modules. The tactic
3767 ;; is to store new search functions into the local variables of the
3768 ;; TAGS buffers. The variables are restored directly after the
3769 ;; search. The situation is complicated by the fact that new TAGS
3770 ;; files can be loaded during the search.
3772 ;; This code is Emacs 19 `etags' specific.
3774 (defun erlang-tags-install-module-check ()
3775 "Install our own tag search functions."
3776 ;; Make sure our functions are installed in TAGS files loaded
3777 ;; into Emacs while searching.
3778 ;; ?? tags-table-format-hooks isn't in Emacs 21 or XEmacs etags.
3779 (setq erlang-tags-orig-format-hooks
3780 (symbol-value 'tags-table-format-hooks))
3781 (funcall (symbol-function 'set) 'tags-table-format-hooks
3782 (cons 'erlang-tags-recognize-tags-table
3783 erlang-tags-orig-format-hooks))
3784 (setq erlang-tags-buffer-list '())
3785 ;; Install our functions in the TAGS files already resident.
3787 (let ((files (symbol-value 'tags-table-computed-list)))
3789 (if (stringp (car files))
3790 (if (get-file-buffer (car files))
3792 (set-buffer (get-file-buffer (car files)))
3793 (erlang-tags-install-local))))
3794 (setq files (cdr files))))))
3797 (defun erlang-tags-install-local ()
3798 "Install our tag search functions in current buffer."
3799 (if erlang-tags-buffer-installed-p
3801 ;; Mark this buffer as "installed" and record.
3802 (set (make-local-variable 'erlang-tags-buffer-installed-p) t)
3803 (setq erlang-tags-buffer-list
3804 (cons (current-buffer) erlang-tags-buffer-list))
3806 ;; Save the original values.
3807 (set (make-local-variable 'erlang-tags-orig-tag-order)
3808 (symbol-value 'find-tag-tag-order))
3809 (set (make-local-variable 'erlang-tags-orig-regexp-tag-order)
3810 (symbol-value 'find-tag-regexp-tag-order))
3811 (set (make-local-variable 'erlang-tags-orig-search-function)
3812 (symbol-value 'find-tag-search-function))
3813 (set (make-local-variable 'erlang-tags-orig-regexp-search-function)
3814 (symbol-value 'find-tag-regexp-search-function))
3816 ;; Install our own functions.
3817 (set (make-local-variable 'find-tag-search-function)
3818 'erlang-tags-search-forward)
3819 (set (make-local-variable 'find-tag-regexp-search-function)
3820 'erlang-tags-regexp-search-forward)
3821 (set (make-local-variable 'find-tag-tag-order)
3822 '(erlang-tag-match-module-p))
3823 (set (make-local-variable 'find-tag-regexp-tag-order)
3824 '(erlang-tag-match-module-regexp-p))))
3827 (defun erlang-tags-remove-module-check ()
3828 "Remove our own tags search functions."
3829 (funcall (symbol-function 'set)
3830 'tags-table-format-hooks
3831 erlang-tags-orig-format-hooks)
3832 ;; Remove our functions from the TAGS files. (Note that
3833 ;; `tags-table-computed-list' need not be the same list as when
3834 ;; the search was started.)
3836 (let ((buffers erlang-tags-buffer-list))
3838 (if (buffer-name (car buffers))
3840 (set-buffer (car buffers))
3841 (erlang-tags-remove-local)))
3842 (setq buffers (cdr buffers))))))
3845 (defun erlang-tags-remove-local ()
3846 "Remove our tag search functions from current buffer."
3847 (if (null erlang-tags-buffer-installed-p)
3849 (funcall (symbol-function 'set) 'erlang-tags-buffer-installed-p nil)
3850 (funcall (symbol-function 'set)
3851 'find-tag-tag-order erlang-tags-orig-tag-order)
3852 (funcall (symbol-function 'set)
3853 'find-tag-regexp-tag-order erlang-tags-orig-regexp-tag-order)
3854 (funcall (symbol-function 'set)
3855 'find-tag-search-function erlang-tags-orig-search-function)
3856 (funcall (symbol-function 'set)
3857 'find-tag-regexp-search-function
3858 erlang-tags-orig-regexp-search-function)))
3861 (defun erlang-tags-recognize-tags-table ()
3862 "Install our functions in all loaded TAGS files.
3864 This function is added to `tags-table-format-hooks' when searching
3865 for a tag on the form `module:tag'."
3866 (if (null (funcall (symbol-function 'etags-recognize-tags-table)))
3868 (erlang-tags-install-local)
3872 (defun erlang-tags-search-forward (tag &optional bound noerror count)
3873 "Forward search function, aware of Erlang module prefix."
3874 (if (string-match ":" tag)
3875 (setq tag (substring tag (match-end 0) nil)))
3876 ;; Avoid unintended recursion.
3877 (if (eq erlang-tags-orig-search-function 'erlang-tags-search-forward)
3878 (search-forward tag bound noerror count)
3879 (funcall erlang-tags-orig-search-function tag bound noerror count)))
3882 (defun erlang-tags-regexp-search-forward (tag &optional bound noerror count)
3883 "Forward regexp search function, aware of Erlang module prefix."
3884 (if (string-match ":" tag)
3885 (setq tag (substring tag (match-end 0) nil)))
3886 (if (eq erlang-tags-orig-regexp-search-function
3887 'erlang-tags-regexp-search-forward)
3888 (re-search-forward tag bound noerror count)
3889 (funcall erlang-tags-orig-regexp-search-function
3890 tag bound noerror count)))
3893 ;; t if point is at a tag line that matches TAG, containing
3894 ;; module information. Assumes that all other order functions
3895 ;; are stored in `erlang-tags-orig-[regex]-tag-order'.
3897 (defun erlang-tag-match-module-p (tag)
3898 (erlang-tag-match-module-common-p tag erlang-tags-orig-tag-order))
3900 (defun erlang-tag-match-module-regexp-p (tag)
3901 (erlang-tag-match-module-common-p tag erlang-tags-orig-regexp-tag-order))
3903 (defun erlang-tag-match-module-common-p (tag order)
3906 (if (string-match ":" tag)
3908 (setq mod (substring tag 0 (match-beginning 0)))
3909 (setq tag (substring tag (match-end 0) nil))))
3910 (while (and order (not found))
3912 (and (not (memq (car order)
3913 '(erlang-tag-match-module-p
3914 erlang-tag-match-module-regexp-p)))
3915 (funcall (car order) tag)))
3916 (setq order (cdr order)))
3919 (string= mod (erlang-get-module-from-file-name
3923 ;;; Tags completion, Emacs 19 `etags' specific.
3925 ;;; The basic idea is to create a second completion table `erlang-tags-
3926 ;;; completion-table' containing all normal tags plus tags on the form
3930 (defun erlang-complete-tag ()
3931 "Perform tags completion on the text around point.
3932 Completes to the set of names listed in the current tags table.
3934 Should the Erlang tags system be installed this command knows
3935 about Erlang modules."
3940 (cond ((and erlang-tags-installed
3941 (fboundp 'complete-tag)) ; Emacs 19
3942 (let ((orig-tags-complete-tag (symbol-function 'tags-complete-tag)))
3943 (fset 'tags-complete-tag
3944 (symbol-function 'erlang-tags-complete-tag))
3946 (funcall (symbol-function 'complete-tag))
3947 (fset 'tags-complete-tag orig-tags-complete-tag))))
3948 ((fboundp 'complete-tag) ; Emacs 19
3949 (funcall (symbol-function 'complete-tag)))
3950 ((fboundp 'tag-complete-symbol) ; XEmacs
3951 (funcall (symbol-function 'tag-complete-symbol)))
3953 (error "This version of Emacs can't complete tags"))))
3956 ;; Based on `tags-complete-tag', but this one uses
3957 ;; `erlang-tags-completion-table' instead of `tags-completion-table'.
3959 ;; This is the entry-point called by system function `completing-read'.
3960 (defun erlang-tags-complete-tag (string predicate what)
3962 ;; If we need to ask for the tag table, allow that.
3963 (let ((enable-recursive-minibuffers t))
3964 (visit-tags-table-buffer))
3966 (all-completions string (erlang-tags-completion-table) predicate)
3967 (try-completion string (erlang-tags-completion-table) predicate))))
3970 ;; `tags-completion-table' calls itself recursively, make it
3971 ;; call our own wedge instead. Note that the recursive call
3972 ;; is very rare; it only occurs when a tags-file contains
3973 ;; `include'-statements.
3974 (defun erlang-tags-completion-table ()
3975 "Build completion table. Tags on the form `tag' or `module:tag'."
3976 (setq erlang-tags-orig-completion-table
3977 (symbol-function 'tags-completion-table))
3978 (fset 'tags-completion-table
3979 (symbol-function 'erlang-tags-completion-table-1))
3981 (erlang-tags-completion-table-1)
3982 (fset 'tags-completion-table
3983 erlang-tags-orig-completion-table)))
3986 (defun erlang-tags-completion-table-1 ()
3987 (make-local-variable 'erlang-tags-completion-table)
3988 (or erlang-tags-completion-table
3989 (let ((tags-completion-table nil)
3990 (tags-completion-table-function
3991 'erlang-etags-tags-completion-table))
3992 (funcall erlang-tags-orig-completion-table)
3993 (setq erlang-tags-completion-table tags-completion-table))))
3996 ;; Based on `etags-tags-completion-table'. The difference is that we
3997 ;; add three symbols to the vector, the tag, module: and module:tag.
3998 ;; The module is extracted from the file name of a tag. (This one
3999 ;; only works if we are looking at an `etags' file. However, this is
4000 ;; the only format supported by Emacs, so far.)
4001 (defun erlang-etags-tags-completion-table ()
4002 (let ((table (make-vector 511 0))
4005 (goto-char (point-min))
4006 ;; This monster regexp matches an etags tag line.
4007 ;; \1 is the string to match;
4008 ;; \2 is not interesting;
4009 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
4010 ;; \4 is not interesting;
4011 ;; \5 is the explicitly-specified tag name.
4012 ;; \6 is the line to start searching at;
4013 ;; \7 is the char to start searching at.
4016 (eq (following-char) ?\f)
4017 (looking-at "\f\n\\([^,\n]*\\),.*\n"))
4018 (setq file (buffer-substring
4019 (match-beginning 1) (match-end 1)))
4020 (goto-char (match-end 0)))
4023 ^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
4024 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
4025 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
4027 (let ((tag (if (match-beginning 5)
4028 ;; There is an explicit tag name.
4029 (buffer-substring (match-beginning 5) (match-end 5))
4030 ;; No explicit tag name. Best guess.
4031 (buffer-substring (match-beginning 3) (match-end 3))))
4033 (erlang-get-module-from-file-name file))))
4035 (if (stringp module)
4037 (intern (concat module ":" tag) table)
4038 ;; Only the first one will be stored in the table.
4039 (intern (concat module ":") table))))))
4043 ;;; Prepare for other methods to run an Erlang slave process.
4046 (defvar erlang-shell-function 'inferior-erlang
4047 "Command to execute start a new Erlang shell.
4049 Change this variable to use your favorite
4050 Erlang compilation package.")
4052 (defvar erlang-shell-display-function 'inferior-erlang-run-or-select
4053 "Command to execute to display Erlang shell.
4055 Change this variable to use your favorite
4056 Erlang compilation package.")
4058 (defvar erlang-compile-function 'inferior-erlang-compile
4059 "Command to execute to compile current buffer.
4061 Change this variable to use your favorite
4062 Erlang compilation package.")
4064 (defvar erlang-compile-display-function 'inferior-erlang-run-or-select
4065 "Command to execute to view last compilation.
4067 Change this variable to use your favorite
4068 Erlang compilation package.")
4070 (defvar erlang-next-error-function 'inferior-erlang-next-error
4071 "Command to execute to go to the next error.
4073 Change this variable to use your favorite Erlang compilation
4074 package. Not used in Emacs 21.")
4078 (defun erlang-shell ()
4079 "Start a new Erlang shell.
4081 The variable `erlang-shell-function' decides which method to use,
4082 default is to start a new Erlang host. It is possible that, in the
4083 future, a new shell on an already running host will be started."
4085 (call-interactively erlang-shell-function))
4088 ;;;###autoload (autoload 'run-erlang "erlang" "Start a new Erlang shell." t)
4090 ;; It is customary for Emacs packages to supply a function on this
4091 ;; form, even though it violates the `erlang-*' name convention.
4092 (defalias 'run-erlang 'erlang-shell)
4095 (defun erlang-shell-display ()
4096 "Display an Erlang shell, or start a new."
4098 (call-interactively erlang-shell-display-function))
4102 (defun erlang-compile ()
4103 "Compile Erlang module in current buffer."
4105 (call-interactively erlang-compile-function))
4108 (defun erlang-compile-display ()
4109 "Display compilation output."
4111 (call-interactively erlang-compile-display-function))
4114 (defun erlang-next-error ()
4115 "Display next error message from the latest compilation."
4117 (call-interactively erlang-next-error-function))
4122 ;;; Erlang Shell Mode -- Major mode used for Erlang shells.
4125 ;; This mode is designed to be implementation independent,
4126 ;; e.g. it does not assume that we are running an inferior
4127 ;; Erlang, there exists a lot of other possibilities.
4130 (defvar erlang-shell-buffer-name "*erlang*"
4131 "The name of the Erlang link shell buffer.")
4134 (defvar erlang-shell-mode-map nil
4135 "Keymap used by Erlang shells.")
4138 (defvar erlang-shell-mode-hook nil
4139 "*User functions to run when an Erlang shell is started.
4141 This hook is used to change the behaviour of Erlang mode. It is
4142 normally used by the user to personalise the programming environment.
4143 When used in a site init file, it could be used to customise Erlang
4144 mode for all users on the system.
4146 The function added to this hook is run every time a new Erlang
4149 See also `erlang-load-hook', a hook which is run once, when Erlang
4150 mode is loaded, and `erlang-mode-hook' which is run every time a new
4151 Erlang source file is loaded into Emacs.")
4154 (defvar erlang-input-ring-file-name "~/.erlang_history"
4155 "*When non-nil, file name used to store Erlang shell history information.")
4158 (defun erlang-shell-mode ()
4159 "Major mode for interacting with an Erlang shell.
4161 We assume that we already are in Comint mode.
4163 The following special commands are available:
4164 \\{erlang-shell-mode-map}"
4166 (setq major-mode 'erlang-shell-mode)
4167 (setq mode-name "Erlang Shell")
4168 (erlang-mode-variables)
4169 (if erlang-shell-mode-map
4171 (setq erlang-shell-mode-map (copy-keymap comint-mode-map))
4172 (erlang-shell-mode-commands erlang-shell-mode-map))
4173 (use-local-map erlang-shell-mode-map)
4174 (unless inferior-erlang-use-cmm
4175 ;; This was originally not a marker, but it needs to be, at least
4176 ;; in Emacs 21, and should be backwards-compatible. Otherwise,
4177 ;; would need to test whether compilation-parsing-end is a marker
4178 ;; after requiring `compile'.
4179 (set (make-local-variable 'compilation-parsing-end) (copy-marker 1))
4180 (set (make-local-variable 'compilation-error-list) nil)
4181 (set (make-local-variable 'compilation-old-error-list) nil))
4182 ;; Needed when compiling directly from the Erlang shell.
4183 (setq compilation-last-buffer (current-buffer))
4184 (erlang-add-compilation-alist erlang-error-regexp-alist)
4185 (setq comint-prompt-regexp "^[^>=]*> *")
4186 (setq comint-eol-on-send t)
4187 (setq comint-input-ignoredups t)
4188 (setq comint-scroll-show-maximum-output t)
4189 (setq comint-scroll-to-bottom-on-output t)
4190 ;; In Emacs 19.30, `add-hook' has got a `local' flag, use it. If
4191 ;; the call fails, just call the normal `add-hook'.
4194 (make-local-hook 'comint-output-filter-functions) ; obsolete after Emacs 21.3
4195 (add-hook 'comint-output-filter-functions
4196 'inferior-erlang-strip-delete nil t)
4197 (add-hook 'comint-output-filter-functions
4198 'inferior-erlang-strip-ctrl-m nil t))
4200 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-delete)
4201 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-ctrl-m)))
4202 ;; Some older versions of comint don't have an input ring.
4203 (if (fboundp 'comint-read-input-ring)
4205 (setq comint-input-ring-file-name erlang-input-ring-file-name)
4206 (comint-read-input-ring t)
4207 (make-local-variable 'kill-buffer-hook)
4208 (add-hook 'kill-buffer-hook 'comint-write-input-ring)))
4209 ;; At least in Emacs 21, we need to be in `compilation-minor-mode'
4210 ;; for `next-error' to work. We can avoid it clobbering the shell
4212 (when inferior-erlang-use-cmm
4213 (compilation-minor-mode 1)
4214 (set (make-local-variable 'minor-mode-overriding-map-alist)
4215 `((compilation-minor-mode
4216 . ,(let ((map (make-sparse-keymap)))
4217 ;; It would be useful to put keymap properties on the
4218 ;; error lines so that we could use RET and mouse-2
4219 ;; on them directly.
4220 (when (boundp 'compilation-skip-threshold) ; new compile.el
4221 (define-key map [mouse-2] #'erlang-mouse-2-command)
4222 (define-key map "\C-m" #'erlang-RET-command))
4223 (if (boundp 'compilation-menu-map)
4224 (define-key map [menu-bar compilation]
4225 (cons "Errors" compilation-menu-map)))
4227 (run-hooks 'erlang-shell-mode-hook))
4230 (defun erlang-mouse-2-command (event)
4231 "Command bound to `mouse-2' in inferior Erlang buffer.
4232 Selects Comint or Compilation mode command as appropriate."
4234 (if (save-window-excursion
4236 (mouse-set-point event)
4237 (consp (get-text-property (line-beginning-position) 'message))))
4238 (call-interactively (lookup-key compilation-mode-map [mouse-2]))
4239 (call-interactively (lookup-key comint-mode-map [mouse-2]))))
4241 (defun erlang-RET-command ()
4242 "Command bound to `RET' in inferior Erlang buffer.
4243 Selects Comint or Compilation mode command as appropriate."
4245 (if (consp (get-text-property (line-beginning-position) 'message))
4246 (call-interactively (lookup-key compilation-mode-map "\C-m"))
4247 (call-interactively (lookup-key comint-mode-map "\C-m"))))
4249 (defun erlang-shell-mode-commands (map)
4250 (define-key map "\M-\t" 'erlang-complete-tag)
4251 (define-key map "\C-a" 'comint-bol) ; Normally the other way around.
4252 (define-key map "\C-c\C-a" 'beginning-of-line)
4253 (define-key map "\C-d" nil) ; Was `comint-delchar-or-maybe-eof'
4254 (define-key map "\M-\C-m" 'compile-goto-error)
4255 (unless inferior-erlang-use-cmm
4256 (define-key map "\C-x`" 'erlang-next-error)))
4259 ;;; Inferior Erlang -- Run an Erlang shell as a subprocess.
4262 (defvar inferior-erlang-display-buffer-any-frame nil
4263 "*When nil, `inferior-erlang-display-buffer' use only selected frame.
4264 When t, all frames are searched. When 'raise, the frame is raised.")
4266 (defvar inferior-erlang-shell-type 'newshell
4267 "The type of Erlang shell to use.
4269 When this variable is set to the atom `oldshell', the old shell is used.
4270 When set to `newshell' the new shell is used. Should the variable be
4271 nil, the default shell is used.
4273 This variable influence the setting of other variables.")
4275 (defvar inferior-erlang-machine "erl"
4276 "*The name of the Erlang shell.")
4278 (defvar inferior-erlang-machine-options '()
4279 "*The options used when activating the Erlang shell.
4281 This must be a list of strings.")
4283 (defvar inferior-erlang-process-name "inferior-erlang"
4284 "The name of the inferior Erlang process.")
4286 (defvar inferior-erlang-buffer-name erlang-shell-buffer-name
4287 "The name of the inferior Erlang buffer.")
4289 (defvar inferior-erlang-prompt-timeout 60
4290 "*Number of seconds before `inferior-erlang-wait-prompt' timeouts.
4292 The time specified is waited after every output made by the inferior
4293 Erlang shell. When this variable is t, we assume that we always have
4294 a prompt. When nil, we will wait forever, or until \\[keyboard-quit].")
4296 (defvar inferior-erlang-process nil
4297 "Process of last invoked inferior Erlang, or nil.")
4299 (defvar inferior-erlang-buffer nil
4300 "Buffer of last invoked inferior Erlang, or nil.")
4303 (defun inferior-erlang ()
4304 "Run an inferior Erlang.
4306 This is just like running Erlang in a normal shell, except that
4307 an Emacs buffer is used for input and output.
4309 The command line history can be accessed with \\[comint-previous-input] and \\[comint-next-input].
4310 The history is saved between sessions.
4312 Entry to this mode calls the functions in the variables
4313 `comint-mode-hook' and `erlang-shell-mode-hook' with no arguments.
4315 The following commands imitate the usual Unix interrupt and
4316 editing control characters:
4317 \\{erlang-shell-mode-map}"
4320 (let ((opts inferior-erlang-machine-options))
4321 (cond ((eq inferior-erlang-shell-type 'oldshell)
4322 (setq opts (cons "-oldshell" opts)))
4323 ((eq inferior-erlang-shell-type 'newshell)
4324 (setq opts (append '("-newshell" "-env" "TERM" "vt100") opts))))
4325 (setq inferior-erlang-buffer
4327 inferior-erlang-process-name inferior-erlang-machine
4329 (setq inferior-erlang-process
4330 (get-buffer-process inferior-erlang-buffer))
4331 (process-kill-without-query inferior-erlang-process)
4332 (switch-to-buffer inferior-erlang-buffer)
4333 (if (and (not (eq system-type 'windows-nt))
4334 (eq inferior-erlang-shell-type 'newshell))
4335 (setq comint-process-echoes t))
4336 ;; `rename-buffer' takes only one argument in Emacs 18.
4338 (rename-buffer inferior-erlang-buffer-name t)
4339 (error (rename-buffer inferior-erlang-buffer-name)))
4340 (erlang-shell-mode))
4343 (defun inferior-erlang-run-or-select ()
4344 "Switch to an inferior Erlang buffer, possibly starting new process."
4346 (if (null (inferior-erlang-running-p))
4348 (inferior-erlang-display-buffer t)))
4351 (defun inferior-erlang-display-buffer (&optional select)
4352 "Make the inferior Erlang process visible.
4353 The window is returned.
4355 Should `inferior-erlang-display-buffer-any-frame' be nil the buffer is
4356 displayed in the current frame. Should it be non-nil, and the buffer
4357 already is visible in any other frame, no new window will be created.
4358 Should it be the atom 'raise, the frame containing the window will
4361 Should the optional argument SELECT be non-nil, the window is
4362 selected. Should the window be in another frame, that frame is raised.
4364 Note, should the mouse pointer be places outside the raised frame, that
4365 frame will become deselected before the next command."
4367 (or (inferior-erlang-running-p)
4368 (error "No inferior Erlang process is running"))
4369 (let ((win (inferior-erlang-window
4370 inferior-erlang-display-buffer-any-frame))
4371 (frames-p (fboundp 'selected-frame)))
4373 (let ((old-win (selected-window)))
4375 (switch-to-buffer-other-window inferior-erlang-buffer)
4376 (setq win (selected-window)))
4377 (select-window old-win))
4378 (if (and window-system
4381 (eq inferior-erlang-display-buffer-any-frame 'raise))
4382 (not (eq (selected-frame) (window-frame win))))
4383 (raise-frame (window-frame win))))
4385 (select-window win))
4390 (defun inferior-erlang-running-p ()
4391 "Non-nil when an inferior Erlang is running."
4392 (and inferior-erlang-process
4393 (memq (process-status inferior-erlang-process) '(run open))
4394 inferior-erlang-buffer
4395 (buffer-name inferior-erlang-buffer)))
4398 (defun inferior-erlang-window (&optional all-frames)
4399 "Return the window containing the inferior Erlang, or nil."
4400 (and (inferior-erlang-running-p)
4401 (if (and all-frames (>= erlang-emacs-major-version 19))
4402 (get-buffer-window inferior-erlang-buffer t)
4403 (get-buffer-window inferior-erlang-buffer))))
4406 (defun inferior-erlang-wait-prompt ()
4407 "Wait until the inferior Erlang shell prompt appears."
4408 (if (eq inferior-erlang-prompt-timeout t)
4410 (or (inferior-erlang-running-p)
4411 (error "No inferior Erlang shell is running"))
4413 (set-buffer inferior-erlang-buffer)
4415 (while (save-excursion
4416 (goto-char (process-mark inferior-erlang-process))
4418 (not (looking-at comint-prompt-regexp)))
4422 (message "Waiting for Erlang shell prompt (press C-g to abort)."))
4423 (or (accept-process-output inferior-erlang-process
4424 inferior-erlang-prompt-timeout)
4425 (error "No Erlang shell prompt before timeout")))
4426 (if msg (message ""))))))
4428 (autoload 'comint-send-input "comint")
4430 (defun inferior-erlang-send-command (cmd &optional hist)
4431 "Send command CMD to the inferior Erlang.
4433 The contents of the current command line (if any) will
4434 be placed at the next prompt.
4436 If optional second argument is non-nil the command is inserted into
4439 Return the position after the newly inserted command."
4440 (or (inferior-erlang-running-p)
4441 (error "No inferior Erlang process is running"))
4442 (let ((old-buffer (current-buffer))
4443 (insert-point (marker-position (process-mark inferior-erlang-process)))
4444 (insert-length (if comint-process-echoes
4446 (1+ (length cmd)))))
4447 (set-buffer inferior-erlang-buffer)
4448 (goto-char insert-point)
4450 ;; Strange things happened if `comint-eol-on-send' is declared
4451 ;; in the `let' expression above, but setq:d here. The
4452 ;; `set-buffer' statement obviously makes the buffer local
4453 ;; instance of `comint-eol-on-send' shadow this one.
4454 ;; I'm considering this a bug in Elisp.
4456 ;; This was previously cautioned against in the Lisp manual. It
4457 ;; has been sorted out in Emacs 21. -- fx
4458 (let ((comint-eol-on-send nil)
4459 (comint-input-filter (if hist comint-input-filter 'ignore)))
4460 (comint-send-input))
4461 ;; Adjust all windows whose points are incorrect.
4462 (if (null comint-process-echoes)
4466 (if (and (eq (window-buffer window) inferior-erlang-buffer)
4467 (= (window-point window) insert-point))
4468 (set-window-point window
4469 (+ insert-point insert-length)))))
4471 (set-buffer old-buffer)
4472 (+ insert-point insert-length)))
4475 (defun inferior-erlang-strip-delete (&optional s)
4476 "Remove `^H' (delete) and the characters it was supposed to remove."
4478 (if (and (boundp 'comint-last-input-end)
4479 (boundp 'comint-last-output-start))
4483 (symbol-value 'comint-last-input-end)
4484 (symbol-value 'comint-last-output-start)))
4485 (while (progn (skip-chars-forward "^\C-h")
4486 (not (eq (point) (point-max))))
4489 (backward-delete-char 1))))))
4492 ;; Basically `comint-strip-ctrl-m', with a few extra checks.
4493 (defun inferior-erlang-strip-ctrl-m (&optional string)
4494 "Strip trailing `^M' characters from the current output group."
4496 (if (and (boundp 'comint-last-input-end)
4497 (boundp 'comint-last-output-start))
4498 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
4502 (symbol-value 'comint-last-input-end)
4503 (symbol-value 'comint-last-output-start)))
4504 (while (re-search-forward "\r+$" pmark t)
4505 (replace-match "" t t))))))
4508 (defun inferior-erlang-compile ()
4509 "Compile the file in the current buffer.
4511 Should Erlang return `{error, nofile}' it could not load the object
4512 module after completing the compilation. This is due to a bug in the
4513 compile command `c' when using the option `outdir'.
4515 There exists two workarounds for this bug:
4517 1) Place the directory in the Erlang load path.
4519 2) Set the Emacs variable `erlang-compile-use-outdir' to nil.
4520 To do so, place the following line in your `~/.emacs'-file:
4521 (setq erlang-compile-use-outdir nil)"
4524 (or (inferior-erlang-running-p)
4527 (or (inferior-erlang-running-p)
4528 (error "Error starting inferior Erlang shell"))
4529 (let ((dir (file-name-directory (buffer-file-name)))
4530 ;;; (file (file-name-nondirectory (buffer-file-name)))
4531 (noext (substring (buffer-file-name) 0 -4))
4532 ;; Hopefully, noone else will ever use these...
4536 (inferior-erlang-display-buffer)
4537 (inferior-erlang-wait-prompt)
4538 (setq end (inferior-erlang-send-command
4539 (if erlang-compile-use-outdir
4540 (format "c(\"%s\", [{outdir, \"%s\"}])." noext dir)
4543 "f(%s), {ok, %s} = file:get_cwd(), "
4544 "file:set_cwd(\"%s\"), "
4545 "%s = c(\"%s\"), file:set_cwd(%s), f(%s), %s.")
4548 tmpvar2 noext tmpvar tmpvar tmpvar2))
4550 (inferior-erlang-wait-prompt)
4552 (set-buffer inferior-erlang-buffer)
4553 (setq compilation-error-list nil)
4554 (set-marker compilation-parsing-end end))
4555 (setq compilation-last-buffer inferior-erlang-buffer)))
4558 ;; `next-error' only accepts buffers with major mode `compilation-mode'
4559 ;; or with the minor mode `compilation-minor-mode' activated.
4560 ;; (To activate the minor mode is out of the question, since it will
4561 ;; ruin the inferior Erlang keymap.)
4562 ;; This is done differently in Emacs 21.
4563 (defun inferior-erlang-next-error (&optional argp)
4564 "Just like `next-error'.
4565 Capable of finding error messages in an inferior Erlang buffer."
4568 (buf (and (boundp 'compilation-last-buffer)
4569 compilation-last-buffer)))
4570 (if (and (bufferp buf)
4573 (and (eq major-mode 'erlang-shell-mode)
4574 (setq major-mode 'compilation-mode))))
4581 (setq major-mode 'erlang-shell-mode))))
4583 (next-error argp))))
4586 (defun inferior-erlang-change-directory (&optional dir)
4587 "Make the inferior Erlang change directory.
4588 The default is to go to the directory of the current buffer."
4590 (or dir (setq dir (file-name-directory (buffer-file-name))))
4591 (or (inferior-erlang-running-p)
4592 (error "No inferior Erlang is running"))
4593 (inferior-erlang-display-buffer)
4594 (inferior-erlang-wait-prompt)
4595 (inferior-erlang-send-command (format "cd('%s')." dir) nil))
4597 (defun erlang-align-arrows (start end)
4598 "Align arrows (\"->\") in function clauses from START to END.
4599 When called interactively, aligns arrows after function clauses inside
4602 With a prefix argument, aligns all arrows, not just those in function
4607 sum(L) -> sum(L, 0).
4608 sum([H|T], Sum) -> sum(T, Sum + H);
4609 sum([], Sum) -> Sum.
4613 sum(L) -> sum(L, 0).
4614 sum([H|T], Sum) -> sum(T, Sum + H);
4615 sum([], Sum) -> Sum."
4618 (let (;; regexp for matching arrows. without a prefix argument,
4619 ;; the regexp matches function heads. With a prefix, it
4620 ;; matches any arrow.
4621 (re (if current-prefix-arg
4624 (concat "^" erlang-atom-regexp ".*\\(\\)->"))))
4625 ;; part of regexp matching directly before the arrow
4626 (arrow-match-pos (if current-prefix-arg
4628 (1+ erlang-atom-regexp-matches)))
4629 ;; accumulator for positions where arrows are found, ordered
4630 ;; by buffer position (from greatest to smallest)
4631 (arrow-positions '())
4632 ;; accumulator for longest distance from start of line to arrow
4634 ;; marker to track the end of the region we're aligning
4635 (end-marker (progn (goto-char end)
4637 ;; Pass 1: Find the arrow positions, adjust the whitespace
4638 ;; before each arrow to one space, and find the greatest
4639 ;; indentation level.
4641 (while (re-search-forward re end-marker t)
4642 (goto-char (match-beginning arrow-match-pos))
4643 (just-one-space) ; adjust whitespace
4644 (setq arrow-positions (cons (point) arrow-positions))
4645 (setq most-indent (max most-indent (erlang-column-number))))
4646 (set-marker end-marker nil) ; free the marker
4647 ;; Pass 2: Insert extra padding so that all arrow indentation is
4648 ;; equal. This is done last-to-first by buffer position, so that
4649 ;; inserting spaces before one arrow doesn't change the
4650 ;; positions of the next ones.
4651 (mapcar (lambda (arrow-pos)
4652 (goto-char arrow-pos)
4653 (let* ((pad (- most-indent (erlang-column-number))))
4655 (insert-char ?\ pad))))
4658 (defun erlang-column-number ()
4659 "Return the column number of the current position in the buffer.
4660 Tab characters are counted by their visual width."
4661 (string-width (buffer-substring (line-beginning-position) (point))))
4663 (defun erlang-current-defun ()
4664 "`add-log-current-defun-function' for Erlang."
4666 (erlang-beginning-of-function)
4667 (if (looking-at "[a-z0-9_]+")
4670 ;; Aliases for backward compatibility with older versions of Erlang Mode.
4672 ;; Unfortuantely, older versions of Emacs doesn't have `defalias' and
4673 ;; `make-obsolete' so we have to define our own `obsolete' function.
4675 (defun erlang-obsolete (sym newdef)
4676 "Make the obsolete function SYM refer to the defined function NEWDEF.
4678 Simplified version of a combination `defalias' and `make-obsolete',
4679 it assumes that NEWDEF is loaded."
4680 (defalias sym (symbol-function newdef))
4681 (if (fboundp 'make-obsolete)
4682 (make-obsolete sym newdef)))
4685 (erlang-obsolete 'calculate-erlang-indent 'erlang-calculate-indent)
4686 (erlang-obsolete 'calculate-erlang-stack-indent
4687 'erlang-calculate-stack-indent)
4688 (erlang-obsolete 'at-erlang-keyword 'erlang-at-keyword)
4689 (erlang-obsolete 'at-erlang-operator 'erlang-at-operator)
4690 (erlang-obsolete 'beginning-of-erlang-clause 'erlang-beginning-of-clause)
4691 (erlang-obsolete 'end-of-erlang-clause 'erlang-end-of-clause)
4692 (erlang-obsolete 'mark-erlang-clause 'erlang-mark-clause)
4693 (erlang-obsolete 'beginning-of-erlang-function 'erlang-beginning-of-function)
4694 (erlang-obsolete 'end-of-erlang-function 'erlang-end-of-function)
4695 (erlang-obsolete 'mark-erlang-function 'erlang-mark-function)
4696 (erlang-obsolete 'pass-over-erlang-clause 'erlang-pass-over-function)
4697 (erlang-obsolete 'name-of-erlang-function 'erlang-name-of-function)
4700 ;; Fixme: shouldn't redefine `set-visited-file-name' anyhow -- see above.
4701 (defconst erlang-unload-hook
4703 (defalias 'set-visited-file-name
4704 'erlang-orig-set-visited-file-name)
4705 (when (featurep 'advice)
4706 (ad-unadvise 'Man-notify-when-ready)
4707 (ad-unadvise 'set-visited-file-name)))))
4713 (run-hooks 'erlang-load-hook)
4716 ;; coding: iso-8859-1
4719 ;;; erlang.el ends here