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-man-root-dir nil
84 "The directory where the Erlang manual pages are 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-man-root-dir' so that
257 the manual pages can be retrieved (note that you must set the value of
258 `erlang-man-root-dir' to match the location of the Erlang man pages
261 (setq erlang-man-root-dir \"/usr/local/erlang\")")
263 (defvar erlang-new-file-hook nil
264 "Functions to run when a new Erlang source file is being edited.
266 A useful function is `tempo-template-erlang-normal-header'.
267 \(This function only exists when the `tempo' package is available.)")
269 (defvar erlang-check-module-name
'ask
270 "*Non-nil means check that module name and file name agrees when saving.
272 If the value of this variable is the atom `ask', the user is
273 prompted. If the value is t the source is silently changed.")
275 (defvar erlang-electric-commands
276 '(erlang-electric-comma
277 erlang-electric-semicolon
278 erlang-electric-newline
280 "*List of activated electric commands.
282 The list should contain the electric commands which should be active.
283 Currently, the available electric commands are:
284 `erlang-electric-comma'
285 `erlang-electric-semicolon'
287 `erlang-electric-newline'
289 Should the variable be bound to t, all electric commands
292 To deactivate all electric commands, set this variable to nil.")
294 (defvar erlang-electric-newline-inhibit t
295 "*Set to non-nil to inhibit newline after electric command.
297 This is useful since a lot of people press return after executing an
300 In order to work, the command must also be in the
301 list `erlang-electric-newline-inhibit-list'.
303 Note that commands in this list are required to set the variable
304 `erlang-electric-newline-inhibit' to nil when the newline shouldn't be
307 (defvar erlang-electric-newline-inhibit-list
308 '(erlang-electric-semicolon
309 erlang-electric-comma
311 "*Commands which can inhibit the next newline.")
313 (defvar erlang-electric-semicolon-insert-blank-lines nil
314 "*Number of blank lines inserted before header, or nil.
316 This variable controls the behaviour of `erlang-electric-semicolon'
317 when a new function header is generated. When nil, no blank line is
318 inserted between the current line and the new header. When bound to a
319 number it represents the number of blank lines which should be
322 (defvar erlang-electric-semicolon-criteria
323 '(erlang-next-lines-empty-p
324 erlang-at-keyword-end-p
325 erlang-at-end-of-function-p
)
326 "*List of functions controlling `erlang-electric-semicolon'.
327 The functions in this list are called, in order, whenever a semicolon
328 is typed. Each function in the list is called with no arguments,
329 and should return one of the following values:
331 nil -- no determination made, continue checking
332 'stop -- do not create prototype for next line
333 (anything else) -- insert prototype, and stop checking
335 If every function in the list is called with no determination made,
336 then no prototype is inserted.
338 The test is performed by the function `erlang-test-criteria-list'.")
340 (defvar erlang-electric-comma-criteria
341 '(erlang-stop-when-inside-argument-list
342 erlang-stop-when-at-guard
343 erlang-next-lines-empty-p
344 erlang-at-keyword-end-p
345 erlang-at-end-of-function-p
)
346 "*List of functions controlling `erlang-electric-comma'.
347 The functions in this list are called, in order, whenever a comma
348 is typed. Each function in the list is called with no arguments,
349 and should return one of the following values:
351 nil -- no determination made, continue checking
352 'stop -- do not create prototype for next line
353 (anything else) -- insert prototype, and stop checking
355 If every function in the list is called with no determination made,
356 then no prototype is inserted.
358 The test is performed by the function `erlang-test-criteria-list'.")
360 (defvar erlang-electric-arrow-criteria
361 '(erlang-next-lines-empty-p
362 erlang-at-end-of-function-p
)
363 "*List of functions controlling the arrow aspect of `erlang-electric-gt'.
364 The functions in this list are called, in order, whenever a `>'
365 is typed. Each function in the list is called with no arguments,
366 and should return one of the following values:
368 nil -- no determination made, continue checking
369 'stop -- do not create prototype for next line
370 (anything else) -- insert prototype, and stop checking
372 If every function in the list is called with no determination made,
373 then no prototype is inserted.
375 The test is performed by the function `erlang-test-criteria-list'.")
377 (defvar erlang-electric-newline-criteria
379 "*List of functions controlling `erlang-electric-newline'.
381 The electric newline commands indents the next line. Should the
382 current line begin with a comment the comment start is copied to
383 the newly created line.
385 The functions in this list are called, in order, whenever a comma
386 is typed. Each function in the list is called with no arguments,
387 and should return one of the following values:
389 nil -- no determination made, continue checking
390 'stop -- do not create prototype for next line
391 (anything else) -- trigger the electric command.
393 If every function in the list is called with no determination made,
394 then no prototype is inserted. Should the atom t be a member of the
395 list, it is treated as a function triggering the electric command.
397 The test is performed by the function `erlang-test-criteria-list'.")
399 (defvar erlang-next-lines-empty-threshold
2
400 "*Number of blank lines required to activate an electric command.
402 Actually, this value controls the behaviour of the function
403 `erlang-next-lines-empty-p' which normally is a member of the
404 criteria lists controlling the electric commands. (Please see
405 the variables `erlang-electric-semicolon-criteria' and
406 `erlang-electric-comma-criteria'.)
408 The variable is bound to a threshold value, a number, representing the
409 number of lines which must be empty.
411 Setting this variable to zero, electric commands will always be
412 triggered by `erlang-next-lines-empty-p', unless inhibited by other
415 Should this variable be nil, `erlang-next-lines-empty-p' will never
416 trigger an electric command. The same effect would be reached if the
417 function `erlang-next-lines-empty-p' would be removed from the criteria
420 Note that even if `erlang-next-lines-empty-p' should not trigger an
421 electric command, other functions in the criteria list could.")
423 (defvar erlang-new-clause-with-arguments nil
424 "*Non-nil means that the arguments are cloned when a clause is generated.
426 A new function header can be generated by calls to the function
427 `erlang-generate-new-clause' and by use of the electric semicolon.")
429 (defvar erlang-compile-use-outdir t
430 "*When nil, go to the directory containing source file when compiling.
432 This is a workaround for a bug in the `outdir' option of compile. If the
433 outdir is not in the current load path, Erlang doesn't load the object
434 module after it has been compiled.
436 To activate the workaround, place the following in your `~/.emacs' file:
437 (setq erlang-compile-use-outdir nil)")
439 (defvar erlang-indent-level
4
440 "*Indentation of Erlang calls/clauses within blocks.")
442 (defvar erlang-indent-guard
2
443 "*Indentation of Erlang guards.")
445 (defvar erlang-argument-indent
2
446 "*Indentation of the first argument in a function call.
447 When nil, indent to the column after the `(' of the
450 (defvar erlang-tab-always-indent t
451 "*Non-nil means TAB in Erlang mode should always re-indent the current line,
452 regardless of where in the line point is when the TAB command is used.")
454 (defvar erlang-error-regexp-alist
455 '(("^\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" .
(1 2)))
456 "*Patterns for matching Erlang errors.")
458 (defvar erlang-man-inhibit
(eq system-type
'windows-nt
)
459 "Inhibit the creation of the Erlang Manual Pages menu.
461 The Windows distribution of Erlang does not include man pages, hence
462 there is no attempt to create the menu.")
464 (defvar erlang-man-dirs
465 '(("Man - Commands" "/man/man1" t
)
466 ("Man - Modules" "/man/man3" t
)
467 ("Man - Files" "/man/man4" t
)
468 ("Man - Applications" "/man/man6" t
))
469 "*The man directories displayed in the Erlang menu.
471 Each item in the list should be a list with three elements, the first
472 the name of the menu, the second the directory, and the last a flag.
473 Should the flag the nil, the directory is absolute, should it be non-nil
474 the directory is relative to the variable `erlang-man-root-dir'.")
476 (defvar erlang-man-max-menu-size
20
477 "*The maximum number of menu items in one menu allowed.")
479 (defvar erlang-man-display-function
'erlang-man-display
480 "*Function used to display man page.
482 The function is called with one argument, the name of the file
483 containing the man page. Use this variable when the default
484 function, `erlang-man-display', does not work on your system.")
487 (defconst erlang-atom-regexp
"\\([a-z][a-zA-Z0-9_]*\\|'[^\n']*[^\\]'\\)"
488 "Regexp which should match an Erlang atom.
490 The regexp must be surrounded with a pair of regexp parentheses."))
491 (defconst erlang-atom-regexp-matches
1
492 "Number of regexp parenthesis pairs in `erlang-atom-regexp'.
494 This is used to determine parenthesis matches in complex regexps which
495 contains `erlang-atom-regexp'.")
497 (defconst erlang-variable-regexp
"\\([A-Z_][a-zA-Z0-9_]*\\)"
498 "Regexp which should match an Erlang variable.
500 The regexp must be surrounded with a pair of regexp parentheses.")
501 (defconst erlang-variable-regexp-matches
1
502 "Number of regexp parenthesis pairs in `erlang-variable-regexp'.
504 This is used to determine matches in complex regexps which contains
505 `erlang-variable-regexp'.")
507 (defvar erlang-defun-prompt-regexp
(concat "^" erlang-atom-regexp
"\\s *(")
508 "Regexp which should match beginning of a clause.")
510 (defvar erlang-file-name-extension-regexp
"\\.[eh]rl$"
511 "*Regexp which should match an Erlang file name.
513 This regexp is used when an Erlang module name is extracted from the
514 name of an Erlang source file.
516 The regexp should only match the section of the file name which should
517 be excluded from the module name.
519 To match all files set this variable to \"\\\\(\\\\..*\\\\|\\\\)$\".
520 The matches all except the extension. This is useful if the Erlang
521 tags system should interpret tags on the form `module:tag' for
522 files written in other languages than Erlang.")
524 (defvar erlang-mode-map nil
525 "*Keymap used in Erlang mode.")
526 (defvar erlang-mode-abbrev-table nil
527 "Abbrev table in use in Erlang-mode buffers.")
528 (defvar erlang-mode-syntax-table nil
529 "Syntax table in use in Erlang-mode buffers.")
531 (defconst erlang-emacs-major-version
532 (if (boundp 'emacs-major-version
)
534 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version
)
535 (string-to-int (substring emacs-version
536 (match-beginning 1) (match-end 1))))
537 "Major version number of Emacs.")
539 (defconst erlang-emacs-minor-version
540 (if (boundp 'emacs-minor-version
)
542 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version
)
543 (string-to-int (substring emacs-version
544 (match-beginning 2) (match-end 2))))
545 "Minor version number of Emacs.")
547 (defconst erlang-xemacs-p
(string-match "Lucid\\|XEmacs" emacs-version
)
548 "Non-nil when running under XEmacs or Lucid Emacs.")
550 (defvar erlang-xemacs-popup-menu
'("Erlang Mode Commands" . nil
)
551 "Common popup menu for all buffers in Erlang mode.
553 This variable is destructively modified every time the Erlang menu
554 is modified. The effect is that all changes take effect in all
555 buffers in Erlang mode, just like under GNU Emacs.
557 Never EVER set this variable!")
559 (defconst inferior-erlang-use-cmm
(boundp 'minor-mode-overriding-map-alist
)
560 "Non-nil means use `compilation-minor-mode' in Erlang shell.")
562 ;; Tempo skeleton templates:
563 (load "erlang-skels")
566 (load "erlang-sinan")
569 ;; Font-lock variables
571 (defvar erlang-font-lock-modern-p
572 (cond ((>= erlang-emacs-major-version
20) t
)
573 (erlang-xemacs-p (>= erlang-emacs-minor-version
14))
574 ((= erlang-emacs-major-version
19) (>= erlang-emacs-minor-version
29))
576 "Non-nil when this version of Emacs uses a modern version of Font Lock.
578 This is determined by checking the version of Emacs used, the actual
579 font-lock code is not loaded.")
582 ;; The next few variables define different Erlang font-lock patterns.
583 ;; They could be appended to form a custom font-lock appearance.
585 ;; The function `erlang-font-lock-set-face' could be used to change
586 ;; the face of a pattern.
588 ;; Note that Erlang strings and atoms are highlighted with using
589 ;; syntactic analysis.
591 (defvar erlang-font-lock-keywords-func
593 (list (concat "^" erlang-atom-regexp
"\\s *(")
594 1 'font-lock-function-name-face t
))
595 "Font lock keyword highlighting a function header.")
597 (defvar erlang-font-lock-keywords-dollar
599 (list "\\(\\$\\([^\\]\\|\\\\\\([^0-7^\n]\\|[0-7]+\\|\\^[a-zA-Z]\\)\\)\\)"
600 1 'font-lock-string-face
))
601 "Font lock keyword highlighting numbers in ASCII form (e.g. $A).")
603 (defvar erlang-font-lock-keywords-arrow
605 (list "\\(->\\|:-\\)\\(\\s \\|$\\)" 2 'font-lock-function-name-face
))
606 "Font lock keyword highlighting clause arrow.")
608 (defvar erlang-font-lock-keywords-lc
610 (list "\\(<-\\)\\(\\s \\|$\\)" 1 'font-lock-keyword-face
)
611 (list "\\(||\\)\\(\\s \\|$\\)" 1 'font-lock-keyword-face
))
612 "Font lock keyword highlighting list comprehension operators.")
614 (defvar erlang-font-lock-keywords-keywords
616 (list (concat "\\<\\(a\\(fter\\|ndalso\\)\\|begin\\|c\\(atch\\|ase\\)"
617 "\\|end\\|fun\\|if\\|o\\(f\\|relse\\)\\|receive\\|try\\|when"
618 "\\|query\\)\\([^a-zA-Z0-9_]\\|$\\)")
619 1 'font-lock-keyword-face
))
620 "Font lock keyword highlighting Erlang keywords.")
622 (defvar erlang-font-lock-keywords-attr
624 (list (concat "^\\(-" erlang-atom-regexp
"\\)\\s *\\(\\.\\|(\\)")
625 1 'font-lock-function-name-face
))
626 "Font lock keyword highlighting attributes.")
628 (defvar erlang-font-lock-keywords-quotes
630 (list "`\\([-+a-zA-Z0-9_:*][-+a-zA-Z0-9_:*]+\\)'"
632 (if erlang-font-lock-modern-p
633 'font-lock-reference-face
634 'font-lock-keyword-face
)
636 "Font lock keyword highlighting words in single quotes in comments.
638 This is not the highlighting of Erlang strings and atoms, which
639 are highlighted by syntactic analysis.")
641 ;; Note: The guard `float' collides with the bif `float'.
642 (defvar erlang-font-lock-keywords-guards
646 "\\(is_\\)*\\(atom\\|boolean\\|function\\|binary\\|constant"
647 "\\|float\\|integer\\|list\\|number\\|p\\(id\\|ort\\)\\|"
648 "re\\(ference\\|cord\\)\\|tuple"
652 (if erlang-font-lock-modern-p
653 'font-lock-reference-face
654 'font-lock-keyword-face
)))
655 "Font lock keyword highlighting guards.")
657 (defvar erlang-font-lock-keywords-bifs
662 "a\\(bs\\|live\\|pply\\|tom_to_list\\)\\|"
663 "binary_to_\\(list\\|term\\)\\|"
664 "concat_binary\\|d\\(ate\\|isconnect_node\\)\\|"
665 "e\\(lement\\|rase\\|xit\\)\\|"
666 "float\\(\\|_to_list\\)\\|"
667 "g\\(arbage_collect\\|et\\(\\|_keys\\)\\|roup_leader\\)\\|"
669 "i\\(nte\\(ger_to_list\\|rnal_bif\\)\\|s_alive\\)\\|"
670 "l\\(ength\\|i\\(nk\\|st_to_\\(atom\\|binary\\|float\\|integer"
671 "\\|pid\\|tuple\\)\\)\\)\\|"
672 "make_ref\\|no\\(de\\(\\|_\\(link\\|unlink\\)\\|s\\)\\|talive\\)\\|"
674 "p\\(id_to_list\\|rocess\\(_\\(flag\\|info\\)\\|es\\)\\|ut\\)\\|"
675 "r\\(egister\\(\\|ed\\)\\|ound\\)\\|"
676 "s\\(e\\(lf\\|telement\\)\\|ize\\|"
677 "p\\(awn\\(\\|_link\\)\\|lit_binary\\)\\|tatistics\\)\\|"
678 "t\\(erm_to_binary\\|hrow\\|ime\\|l\\|"
679 "r\\(ace\\|unc\\)\\|uple_to_list\\)\\|"
680 "un\\(link\\|register\\)\\|whereis"
683 'font-lock-keyword-face
))
684 "Font lock keyword highlighting built in functions.")
686 (defvar erlang-font-lock-keywords-macros
688 (list (concat "?\\s *\\(" erlang-atom-regexp
689 "\\|" erlang-variable-regexp
"\\)\\>")
690 1 (if erlang-font-lock-modern-p
691 'font-lock-reference-face
692 'font-lock-type-face
))
693 (list (concat "^-\\(define\\|ifn?def\\)\\s *(\\s *\\(" erlang-atom-regexp
694 "\\|" erlang-variable-regexp
"\\)\\>")
695 2 (if erlang-font-lock-modern-p
696 'font-lock-reference-face
697 'font-lock-type-face
)))
698 "Font lock keyword highlighting macros.
699 This must be placed in front of `erlang-font-lock-keywords-vars'.")
701 (defvar erlang-font-lock-keywords-records
703 (list (concat "#\\s *" erlang-atom-regexp
"\\>")
704 1 'font-lock-type-face
)
705 ;; Don't highlight numerical constants.
706 (list "\\<[0-9][0-9]?#\\([0-9a-fA_F]+\\)\\>"
708 (list (concat "^-record(\\s *" erlang-atom-regexp
"\\>")
709 1 'font-lock-type-face
))
710 "Font lock keyword highlighting Erlang records.
711 This must be placed in front of `erlang-font-lock-keywords-vars'.")
713 (defvar erlang-font-lock-keywords-vars
715 (list (concat "\\<" erlang-variable-regexp
"\\>")
716 1 (if erlang-font-lock-modern-p
717 'font-lock-variable-name-face
718 'font-lock-type-face
)))
719 "Font lock keyword highlighting Erlang variables.
720 Must be preceded by `erlang-font-lock-keywords-macros' and `-records'
724 (defvar erlang-font-lock-keywords-1
725 (append erlang-font-lock-keywords-func
726 erlang-font-lock-keywords-dollar
727 erlang-font-lock-keywords-arrow
728 erlang-font-lock-keywords-keywords
)
729 ;; DocStringOrig: erlang-font-lock-keywords
730 "Font-lock keywords used by Erlang Mode.
732 There exists three levels of Font Lock keywords for Erlang:
733 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
734 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
735 `erlang-font-lock-keywords-3' - Variables, macros and records.
737 To use a specific level, please set the variable
738 `font-lock-maximum-decoration' to the appropriate level. Note that the
739 variable must be set before Erlang mode is activated.
742 (setq font-lock-maximum-decoration 2)")
745 (defvar erlang-font-lock-keywords-2
746 (append erlang-font-lock-keywords-1
747 erlang-font-lock-keywords-attr
748 erlang-font-lock-keywords-quotes
749 erlang-font-lock-keywords-guards
750 erlang-font-lock-keywords-bifs
)
751 ;; DocStringCopy: erlang-font-lock-keywords
752 "Font-lock keywords used by Erlang Mode.
754 There exists three levels of Font Lock keywords for Erlang:
755 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
756 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
757 `erlang-font-lock-keywords-3' - Variables, macros and records.
759 To use a specific level, please set the variable
760 `font-lock-maximum-decoration' to the appropriate level. Note that the
761 variable must be set before Erlang mode is activated.
764 (setq font-lock-maximum-decoration 2)")
767 (defvar erlang-font-lock-keywords-3
768 (append erlang-font-lock-keywords-2
769 erlang-font-lock-keywords-macros
770 erlang-font-lock-keywords-records
771 erlang-font-lock-keywords-vars
)
772 ;; DocStringCopy: erlang-font-lock-keywords
773 "Font-lock keywords used by Erlang Mode.
775 There exists three levels of Font Lock keywords for Erlang:
776 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
777 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
778 `erlang-font-lock-keywords-3' - Variables, macros and records.
780 To use a specific level, please set the variable
781 `font-lock-maximum-decoration' to the appropriate level. Note that the
782 variable must be set before Erlang mode is activated.
785 (setq font-lock-maximum-decoration 2)")
788 (defvar erlang-font-lock-keywords erlang-font-lock-keywords-3
789 ;; DocStringCopy: erlang-font-lock-keywords
790 "Font-lock keywords used by Erlang Mode.
792 There exists three levels of Font Lock keywords for Erlang:
793 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
794 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
795 `erlang-font-lock-keywords-3' - Variables, macros and records.
797 To use a specific level, please set the variable
798 `font-lock-maximum-decoration' to the appropriate level. Note that the
799 variable must be set before Erlang mode is activated.
802 (setq font-lock-maximum-decoration 2)")
805 (defvar erlang-font-lock-syntax-table nil
806 "Syntax table used by Font Lock mode.
808 The difference between this and the standard Erlang Mode
809 syntax table is that `_' is treated as part of words by
812 Unfortunately, XEmacs hasn't got support for a special Font
813 Lock syntax table. The effect is that `apply' in the atom
814 `foo_apply' will be highlighted as a bif.")
817 ;;; Avoid errors while compiling this file.
819 ;; `eval-when-compile' is not defined in Emacs 18. We define it as a
821 (or (fboundp 'eval-when-compile
)
822 (defmacro eval-when-compile
(&rest rest
) nil
))
824 ;; These umm...functions are new in Emacs 20. And, yes, until version
825 ;; 19.27 Emacs backquotes were this ugly.
827 (or (fboundp 'unless
)
828 (defmacro unless
(condition &rest body
)
829 "(unless CONDITION BODY...): If CONDITION is false, do BODY, else return nil."
835 (defmacro when
(condition &rest body
)
836 "(when CONDITION BODY...): If CONDITION is true, do BODY, else return nil."
841 (or (fboundp 'char-before
)
842 (defmacro char-before
(&optional pos
)
843 "Return the character in the current buffer just before POS."
844 (` (char-after (1- (or (, pos
) (point)))))))
847 (if (or (featurep 'bytecomp
)
848 (featurep 'byte-compile
))
850 (cond ((string-match "Lucid\\|XEmacs" emacs-version
)
851 (put 'comment-indent-hook
'byte-obsolete-variable nil
)
852 ;; Do not warn for unused variables
853 ;; when compiling under XEmacs.
854 (setq byte-compile-warnings
855 '(free-vars unresolved callargs redefine
))))
857 (require 'compile
))))
860 (defun erlang-version ()
861 "Return the current version of Erlang mode."
864 (message "Erlang mode version %s, written by Anders Lindgren"
870 (defun erlang-mode ()
871 "Major mode for editing Erlang source files in Emacs.
872 It knows about syntax and comment, it can indent code, it is capable
873 of fontifying the source file, the TAGS commands are aware of Erlang
874 modules, and the Erlang man pages can be accessed.
876 Should this module, \"erlang.el\", be installed properly, Erlang mode
877 is activated whenever an Erlang source or header file is loaded into
878 Emacs. To indicate this, the mode line should contain the word
881 The main feature of Erlang mode is indentation, press TAB and the
882 current line will be indented correctly.
884 Comments starting with only one `%' are indented to the column stored
885 in the variable `comment-column'. Comments starting with two `%':s
886 are indented with the same indentation as code. Comments starting
887 with at least three `%':s are indented to the first column.
889 However, Erlang mode contains much more, this is a list of the most
891 TAB - Indent the line.
892 C-c C-q - Indent current function.
893 M-; - Create a comment at the end of the line.
894 M-q - Fill a comment, i.e. wrap lines so that they (hopefully)
896 M-a - Goto the beginning of an Erlang clause.
897 M-C-a - Ditto for function.
898 M-e - Goto the end of an Erlang clause.
899 M-C-e - Ditto for function.
900 M-h - Mark current Erlang clause.
901 M-C-h - Ditto for function.
902 C-c C-z - Start, or switch to, an inferior Erlang shell.
903 C-c C-k - Compile current file.
906 ; - Electric semicolon.
908 Erlang mode check the name of the file against the module name when
909 saving, whenever a mismatch occurs Erlang mode offers to modify the
912 The variable `erlang-electric-commands' controls the electric
913 commands. To deactivate all of them, set it to nil.
915 There exists a large number of commands and variables in the Erlang
916 module. Please press `M-x apropos RET erlang RET' to see a complete
917 list. Press `C-h f name-of-function RET' and `C-h v name-of-variable
918 RET'to see the full description of functions and variables,
921 On entry to this mode the contents of the hook `erlang-mode-hook' is
924 Please see the beginning of the file `erlang.el' for more information
925 and examples of hooks.
930 (kill-all-local-variables)
931 (setq major-mode
'erlang-mode
)
932 (setq mode-name
"Erlang")
933 (erlang-syntax-table-init)
935 (erlang-electric-init)
937 (erlang-mode-variables)
938 (erlang-check-module-name-init)
939 (erlang-add-compilation-alist erlang-error-regexp-alist
)
942 (erlang-font-lock-init)
944 (run-hooks 'erlang-mode-hook
)
945 (if (zerop (buffer-size))
946 (run-hooks 'erlang-new-file-hook
)))
949 (defun erlang-syntax-table-init ()
950 (if (null erlang-mode-syntax-table
)
951 (let ((table (make-syntax-table)))
952 (modify-syntax-entry ?
\n ">" table
)
953 (modify-syntax-entry ?
\" "\"" 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
)
972 (setq erlang-mode-syntax-table table
)))
974 (set-syntax-table erlang-mode-syntax-table
))
977 (defun erlang-keymap-init ()
980 (setq erlang-mode-map
(make-sparse-keymap))
981 (erlang-mode-commands erlang-mode-map
))
982 (use-local-map erlang-mode-map
))
985 (defun erlang-mode-commands (map)
986 (unless (boundp 'indent-line-function
)
987 (define-key map
"\t" 'erlang-indent-command
))
988 (define-key map
";" 'erlang-electric-semicolon
)
989 (define-key map
"," 'erlang-electric-comma
)
990 (define-key map
"<" 'erlang-electric-lt
)
991 (define-key map
">" 'erlang-electric-gt
)
992 (define-key map
"\C-m" 'erlang-electric-newline
)
993 (if (not (boundp 'delete-key-deletes-forward
))
994 (define-key map
"\177" 'backward-delete-char-untabify
)
995 (define-key map
[backspace] 'backward-delete-char-untabify))
996 (define-key map "\M-q" 'erlang-fill-paragraph)
997 (unless (boundp 'beginning-of-defun-function)
998 (define-key map "\M-\C-a" 'erlang-beginning-of-function)
999 (define-key map "\M-\C-e" 'erlang-end-of-function)
1000 (define-key map "\M-\C-h" 'erlang-mark-function))
1001 (define-key map "\M-\t" 'erlang-complete-tag)
1002 (define-key map "\C-c\M-\t" 'tempo-complete-tag)
1003 (define-key map "\M-+" 'erlang-find-next-tag)
1004 (define-key map "\C-c\M-a" 'erlang-beginning-of-clause)
1005 (define-key map "\C-c\M-b" 'tempo-backward-mark)
1006 (define-key map "\C-c\M-e" 'erlang-end-of-clause)
1007 (define-key map "\C-c\M-f" 'tempo-forward-mark)
1008 (define-key map "\C-c\M-h" 'erlang-mark-clause)
1009 (define-key map "\C-c\C-c" 'comment-region)
1010 (define-key map "\C-c\C-j" 'erlang-generate-new-clause)
1011 (define-key map "\C-c\C-k" 'erlang-compile)
1012 (define-key map "\C-c\C-l" 'erlang-compile-display)
1013 (define-key map "\C-c\C-s" 'erlang-show-syntactic-information)
1014 (define-key map "\C-c\C-q" 'erlang-indent-function)
1015 (define-key map "\C-c\C-u" 'erlang-uncomment-region)
1016 (define-key map "\C-c\C-y" 'erlang-clone-arguments)
1017 (define-key map "\C-c\C-a" 'erlang-align-arrows)
1018 (define-key map "\C-c\C-z" 'erlang-shell-display)
1019 (unless inferior-erlang-use-cmm
1020 (define-key map "\C-x`" 'erlang-next-error)))
1023 (defun erlang-electric-init ()
1024 ;; Set up electric character functions to work with
1025 ;; delsel/pending-del mode. Also, set up text properties for bit
1027 (mapcar #'(lambda (cmd)
1028 (put cmd 'delete-selection t) ;for delsel (Emacs)
1029 (put cmd 'pending-delete t)) ;for pending-del (XEmacs)
1030 '(erlang-electric-semicolon
1031 erlang-electric-comma
1032 erlang-electric-gt))
1034 (put 'bitsyntax-open-outer 'syntax-table '(4 . ?>))
1035 (put 'bitsyntax-open-outer 'rear-nonsticky '(category))
1036 (put 'bitsyntax-open-inner 'rear-nonsticky '(category))
1037 (put 'bitsyntax-close-inner 'rear-nonsticky '(category))
1038 (put 'bitsyntax-close-outer 'syntax-table '(5 . ?<))
1039 (put 'bitsyntax-close-outer 'rear-nonsticky '(category))
1040 (setq parse-sexp-lookup-properties 't))
1043 (defun erlang-mode-variables ()
1044 (or erlang-mode-abbrev-table
1045 (define-abbrev-table 'erlang-mode-abbrev-table ()))
1046 (setq local-abbrev-table erlang-mode-abbrev-table)
1047 (make-local-variable 'paragraph-start)
1048 (setq paragraph-start (concat "^$\\|" page-delimiter))
1049 (make-local-variable 'paragraph-separate)
1050 (setq paragraph-separate paragraph-start)
1051 (make-local-variable 'paragraph-ignore-fill-prefix)
1052 (setq paragraph-ignore-fill-prefix t)
1053 (make-local-variable 'require-final-newline)
1054 (setq require-final-newline t)
1055 (make-local-variable 'defun-prompt-regexp)
1056 (setq defun-prompt-regexp erlang-defun-prompt-regexp)
1057 (make-local-variable 'comment-start)
1058 (setq comment-start "%")
1059 (make-local-variable 'comment-start-skip)
1060 (setq comment-start-skip "%+\\s *")
1061 (make-local-variable 'comment-column)
1062 (setq comment-column 48)
1063 (make-local-variable 'indent-line-function)
1064 (setq indent-line-function 'erlang-indent-command)
1065 (make-local-variable 'indent-region-function)
1066 (setq indent-region-function 'erlang-indent-region)
1067 (set (make-local-variable 'comment-indent-function) 'erlang-comment-indent)
1068 (if (<= erlang-emacs-major-version 18)
1069 (set (make-local-variable 'comment-indent-hook) 'erlang-comment-indent))
1070 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1071 (set (make-local-variable 'dabbrev-case-fold-search) nil)
1072 (set (make-local-variable 'imenu-prev-index-position-function)
1073 'erlang-beginning-of-function)
1074 (set (make-local-variable 'imenu-extract-index-name-function)
1075 'erlang-get-function-name)
1076 (set (make-local-variable 'tempo-match-finder)
1077 "[^-a-zA-Z0-9_]\\([-a-zA-Z0-9_]*\\)\\=")
1078 (set (make-local-variable 'beginning-of-defun-function)
1079 'erlang-beginning-of-function)
1080 (set (make-local-variable 'end-of-defun-function) 'erlang-end-of-function)
1081 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil)
1082 (set (make-local-variable 'fill-paragraph-function) 'erlang-fill-paragraph)
1083 (set (make-local-variable 'comment-add) 1)
1084 (set (make-local-variable 'outline-regexp) "[[:lower:]0-9_]+ *(.*) *-> *$")
1085 (set (make-local-variable 'outline-level) (lambda () 1))
1086 (set (make-local-variable 'add-log-current-defun-function)
1087 'erlang-current-defun))
1092 ;; The following code is compatible with the standard package `compilation',
1093 ;; making it possible to go to errors using `erlang-next-error' (or just
1094 ;; `next-error' in Emacs 21).
1096 ;; The normal `compile' command works of course. For best result, please
1097 ;; execute `make' with the `-w' flag.
1099 ;; Please see the variables named `compiling-..' above.
1101 (defun erlang-add-compilation-alist (alist)
1103 (cond ((boundp 'compilation-error-regexp-alist) ; Emacs 19
1105 (or (assoc (car (car alist)) compilation-error-regexp-alist)
1106 (setq compilation-error-regexp-alist
1107 (cons (car alist) compilation-error-regexp-alist)))
1108 (setq alist (cdr alist))))
1109 ((boundp 'compilation-error-regexp)
1110 ;; Emacs 18, Only one regexp is allowed.
1111 (funcall (symbol-function 'set)
1112 'compilation-error-regexp (car (car alist))))))
1114 (defun erlang-font-lock-init ()
1115 "Initialize Font Lock for Erlang mode."
1116 (or erlang-font-lock-syntax-table
1117 (setq erlang-font-lock-syntax-table
1118 (let ((table (copy-syntax-table erlang-mode-syntax-table)))
1119 (modify-syntax-entry ?_ "w" table)
1121 (set (make-local-variable 'font-lock-syntax-table)
1122 erlang-font-lock-syntax-table)
1123 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1124 'erlang-beginning-of-clause)
1125 (make-local-variable 'font-lock-keywords)
1126 (let ((level (cond ((boundp 'font-lock-maximum-decoration)
1127 (symbol-value 'font-lock-maximum-decoration))
1128 ((boundp 'font-lock-use-maximal-decoration)
1129 (symbol-value 'font-lock-use-maximal-decoration))
1132 (setq level (cdr-safe (or (assq 'erlang-mode level)
1134 ;; `level' can here be:
1135 ;; A number - The fontification level
1136 ;; nil - Use the default
1138 (cond ((eq level nil)
1139 (set 'font-lock-keywords erlang-font-lock-keywords))
1141 (set 'font-lock-keywords erlang-font-lock-keywords-1))
1143 (set 'font-lock-keywords erlang-font-lock-keywords-2))
1145 (set 'font-lock-keywords erlang-font-lock-keywords-3))))
1147 ;; Modern font-locks can handle the above much more elegantly:
1148 (set (make-local-variable 'font-lock-defaults)
1149 '((erlang-font-lock-keywords erlang-font-lock-keywords-1
1150 erlang-font-lock-keywords-2 erlang-font-lock-keywords-3)
1151 nil nil ((?_ . "w")) erlang-beginning-of-clause
1152 (font-lock-mark-block-function . erlang-mark-clause))))
1156 ;; Useful when defining your own keywords.
1157 (defun erlang-font-lock-set-face (ks &rest faces)
1158 "Replace the face components in a list of keywords.
1160 The first argument, KS, is a list of keywords. The rest of the
1161 arguments are expressions to replace the face information with. The
1162 first expression replaces the face of the first keyword, the second
1163 expression the second keyword etc.
1165 Should an expression be nil, the face of the corresponding keyword is
1168 Should fewer expressions than keywords be given, the last expression
1169 is used for all remaining keywords.
1171 Normally, the expressions are just atoms representing the new face.
1172 They could however be more complex, returning different faces in
1173 different situations.
1175 This function only handles keywords with elements on the forms:
1176 (REGEXP NUMBER FACE)
1177 (REGEXP NUMBER FACE OVERWRITE)
1179 This could be used when defining your own special font-lock setup, e.g:
1181 \(setq my-font-lock-keywords
1182 (append erlang-font-lock-keywords-func
1183 erlang-font-lock-keywords-dollar
1184 (erlang-font-lock-set-face
1185 erlang-font-lock-keywords-macros 'my-neon-green-face)
1186 (erlang-font-lock-set-face
1187 erlang-font-lock-keywords-lc 'my-deep-red 'my-light-red)
1188 erlang-font-lock-keywords-attr))
1190 For a more elaborate example, please see the beginning of the file
1194 (let* ((regexp (car (car ks)))
1195 (number (car (cdr (car ks))))
1196 (new-face (if (and faces (car faces))
1198 (car (cdr (cdr (car ks))))))
1199 (overwrite (car (cdr (cdr (cdr (car ks))))))
1200 (new-keyword (list regexp number new-face)))
1201 (if overwrite (nconc new-keyword (list overwrite)))
1202 (setq res (cons new-keyword res))
1204 (if (and faces (cdr faces))
1205 (setq faces (cdr faces)))))
1209 (defun erlang-font-lock-level-0 ()
1210 ;; DocStringOrig: font-cmd
1211 "Unfontify current buffer."
1216 (defun erlang-font-lock-level-1 ()
1217 ;; DocStringCopy: font-cmd
1218 "Fontify current buffer at level 1.
1219 This highlights function headers, reserved keywords, strings and comments."
1221 (require 'font-lock)
1222 (set 'font-lock-keywords erlang-font-lock-keywords-1)
1224 (funcall (symbol-function 'font-lock-fontify-buffer)))
1227 (defun erlang-font-lock-level-2 ()
1228 ;; DocStringCopy: font-cmd
1229 "Fontify current buffer at level 2.
1230 This highlights level 1 features (see `erlang-font-lock-level-1')
1231 plus bifs, guards and `single quotes'."
1233 (require 'font-lock)
1234 (set 'font-lock-keywords erlang-font-lock-keywords-2)
1236 (funcall (symbol-function 'font-lock-fontify-buffer)))
1239 (defun erlang-font-lock-level-3 ()
1240 ;; DocStringCopy: font-cmd
1241 "Fontify current buffer at level 3.
1242 This highlights level 2 features (see `erlang-font-lock-level-2')
1243 plus variables, macros and records."
1245 (require 'font-lock)
1246 (set 'font-lock-keywords erlang-font-lock-keywords-3)
1248 (funcall (symbol-function 'font-lock-fontify-buffer)))
1251 (defun erlang-menu-init ()
1252 "Init menus for Erlang mode.
1254 The variable `erlang-menu-items' contain a description of the Erlang
1255 mode menu. Normally, the list contains atoms, representing variables
1256 bound to pieces of the menu.
1258 Personal extensions could be added to `erlang-menu-personal-items'.
1260 This function should be called if any variable describing the
1261 menu configuration is changed."
1262 (erlang-menu-install "Erlang" erlang-menu-items erlang-mode-map t))
1265 (defun erlang-menu-install (name items keymap &optional popup)
1266 "Install a menu in Emacs or XEmacs based on an abstract description.
1268 NAME is the name of the menu.
1270 ITEMS is a list. The elements are either nil representing a horizontal
1271 line or a list with two or three elements. The first is the name of
1272 the menu item, the second the function to call, or a submenu, on the
1273 same same form as ITEMS. The third optional element is an expression
1274 which is evaluated every time the menu is displayed. Should the
1275 expression evaluate to nil the menu item is ghosted.
1277 KEYMAP is the keymap to add to menu to. (When using XEmacs, the menu
1278 will only be visible when this menu is the global, the local, or an
1279 activate minor mode keymap.)
1281 If POPUP is non-nil, the menu is bound to the XEmacs `mode-popup-menu'
1282 variable, i.e. it will popup when pressing the right mouse button.
1284 Please see the variable `erlang-menu-base-items'."
1285 (cond (erlang-xemacs-p
1286 (let ((menu (erlang-menu-xemacs name items keymap)))
1287 ;; We add the menu to the global menubar.
1288 (funcall (symbol-function 'add-submenu) nil menu)
1289 (setcdr erlang-xemacs-popup-menu (cdr menu))
1290 (if (and popup (boundp 'mode-popup-menu))
1291 (funcall (symbol-function 'set)
1292 'mode-popup-menu erlang-xemacs-popup-menu))))
1293 ((>= erlang-emacs-major-version 19)
1294 (define-key keymap (vector 'menu-bar (intern name))
1295 (erlang-menu-make-keymap name items)))
1299 (defun erlang-menu-make-keymap (name items)
1300 "Build a menu for Emacs 19."
1301 (let ((menumap (funcall (symbol-function 'make-sparse-keymap)
1304 id def first second third)
1305 (setq items (reverse items))
1307 ;; Replace any occurrence of atoms by their value.
1308 (while (and items (atom (car items)) (not (null (car items))))
1309 (if (and (boundp (car items))
1310 (listp (symbol-value (car items))))
1311 (setq items (append (reverse (symbol-value (car items)))
1313 (setq items (cdr items))))
1314 (setq first (car-safe (car items)))
1315 (setq second (car-safe (cdr-safe (car items))))
1316 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1318 (setq count (+ count 1))
1319 (setq id (intern (format "separator-%d" count)))
1320 (setq def '("--" . nil)))
1321 ((and (consp second) (eq (car second) 'lambda))
1322 (setq count (+ count 1))
1323 (setq id (intern (format "lambda-%d" count)))
1324 (setq def (cons first second)))
1327 (setq def (cons first second)))
1329 (setq count (+ count 1))
1330 (setq id (intern (format "submenu-%d" count)))
1331 (setq def (erlang-menu-make-keymap first second))))
1332 (define-key menumap (vector id) def)
1334 (put id 'menu-enable third))
1335 (setq items (cdr items)))
1336 (cons name menumap)))
1339 (defun erlang-menu-xemacs (name items &optional keymap)
1340 "Build a menu for XEmacs."
1342 first second third entry)
1344 ;; Replace any occurrence of atoms by their value.
1345 (while (and items (atom (car items)) (not (null (car items))))
1346 (if (and (boundp (car items))
1347 (listp (symbol-value (car items))))
1348 (setq items (append (reverse (symbol-value (car items)))
1350 (setq items (cdr items))))
1351 (setq first (car-safe (car items)))
1352 (setq second (car-safe (cdr-safe (car items))))
1353 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1355 (setq res (cons "------" res)))
1357 (setq res (cons (vector first second (or third t)) res)))
1358 ((and (consp second) (eq (car second) 'lambda))
1359 (setq res (cons (vector first (list 'call-interactively second)
1360 (or third t)) res)))
1362 (setq res (cons (cons first
1363 (cdr (erlang-menu-xemacs
1366 (setq items (cdr items)))
1367 (setq res (reverse res))
1368 ;; When adding a menu to a minor-mode keymap under Emacs,
1369 ;; it disappears when the mode is disabled. The expression
1370 ;; generated below imitates this behaviour.
1371 ;; (This could be expressed much clearer using backquotes,
1372 ;; but I don't want to pull in every package.)
1374 (let ((expr (list 'or
1375 (list 'eq keymap 'global-map)
1376 (list 'eq keymap (list 'current-local-map))
1381 'minor-mode-map-alist))))))
1382 (setq res (cons ':included (cons expr res)))))
1386 (defun erlang-menu-substitute (items alist)
1387 "Substitute functions in menu described by ITEMS.
1389 The menu ITEMS is updated destructively.
1391 ALIST is list of pairs where the car is the old function and cdr the new."
1392 (let (first second pair)
1394 (setq first (car-safe (car items)))
1395 (setq second (car-safe (cdr-safe (car items))))
1396 (cond ((null first))
1398 (setq pair (and second (assq second alist)))
1400 (setcar (cdr (car items)) (cdr pair))))
1401 ((and (consp second) (eq (car second) 'lambda)))
1403 (erlang-menu-substitute second alist)))
1404 (setq items (cdr items)))))
1407 (defun erlang-menu-add-above (entry above items)
1408 "Add menu ENTRY above menu entry ABOVE in menu ITEMS.
1409 Do nothing if the items already should be in the menu.
1410 Should ABOVE not be in the list, the entry is added at
1411 the bottom of the menu.
1413 The new menu is returned. No guarantee is given that the original
1414 menu is left unchanged.
1416 The equality test is performed by `eq'.
1418 Example: (erlang-menu-add-above 'my-erlang-menu-items
1419 'erlang-menu-man-items)"
1420 (erlang-menu-add-below entry above items t))
1423 (defun erlang-menu-add-below (entry below items &optional above-p)
1424 "Add menu ENTRY below menu items BELOW in the Erlang menu.
1425 Do nothing if the items already should be in the menu.
1426 Should BELOW not be in the list, items is added at the bottom
1429 The new menu is returned. No guarantee is given that the original
1430 menu is left unchanged.
1432 The equality test is performed by `eq'.
1436 \(setq erlang-menu-items
1437 (erlang-menu-add-below 'my-erlang-menu-items
1438 'erlang-menu-base-items
1439 erlang-menu-items))"
1440 (if (memq entry items)
1441 items ; Return the original menu.
1447 (setq res (append head (list entry)))
1449 ((eq below (car items))
1452 (append head (cons entry items))
1453 (append head (cons (car items)
1454 (cons entry (cdr items))))))
1457 (setq head (append head (list (car items))))
1458 (setq items (cdr items)))))
1461 (defun erlang-menu-delete (entry items)
1462 "Delete ENTRY from menu ITEMS.
1464 The new menu is returned. No guarantee is given that the original
1465 menu is left unchanged."
1470 (defun erlang-man-init ()
1471 "Add menus containing the manual pages of the Erlang.
1473 The variable `erlang-man-dirs' contains entries describing
1474 the location of the manual pages."
1476 (if erlang-man-inhibit
1478 (setq erlang-menu-man-items
1480 ("Man - Function" erlang-man-function)))
1482 (setq erlang-menu-man-items
1483 (append erlang-menu-man-items
1484 (erlang-man-make-top-menu erlang-man-dirs))))
1485 (setq erlang-menu-items
1486 (erlang-menu-add-above 'erlang-menu-man-items
1487 'erlang-menu-version-items
1489 (erlang-menu-init)))
1492 (defun erlang-man-uninstall ()
1493 "Remove the man pages from the Erlang mode."
1495 (setq erlang-menu-items
1496 (erlang-menu-delete 'erlang-menu-man-items erlang-menu-items))
1500 ;; The man menu is a hierarchal structure, with the manual sections
1501 ;; at the top, described by `erlang-man-dirs'. The next level could
1502 ;; either be the manual pages if not to many, otherwise it is an index
1503 ;; menu whose submenus will contain up to `erlang-man-max-menu-size'
1506 (defun erlang-man-make-top-menu (dir-list)
1507 "Create one menu entry per element of DIR-LIST.
1508 The format is described in the documentation of `erlang-man-dirs'."
1512 (setq dir (cond ((nth 2 (car dir-list))
1513 ;; Relative to `erlang-man-root-dir'.
1514 (and (stringp erlang-man-root-dir)
1515 (concat erlang-man-root-dir (nth 1 (car dir-list)))))
1518 (nth 1 (car dir-list)))))
1520 (file-readable-p dir))
1521 (setq menu (cons (list (car (car dir-list))
1522 (erlang-man-make-middle-menu
1523 (erlang-man-get-files dir)))
1525 (setq dir-list (cdr dir-list)))
1526 ;; Should no menus be found, generate a menu item which
1527 ;; will display a help text, when selected.
1531 (("Error! Why?" erlang-man-describe-error)))))))
1534 ;; Should the menu be to long, let's split it into a number of
1535 ;; smaller menus. Warning, this code contains beautiful
1536 ;; destructive operations!
1537 (defun erlang-man-make-middle-menu (filelist)
1538 "Create the second level menu from FILELIST.
1540 Should the list be longer than `erlang-man-max-menu-size', a tree of
1542 (if (<= (length filelist) erlang-man-max-menu-size)
1543 (erlang-man-make-menu filelist)
1545 (filelist (copy-sequence filelist))
1546 segment submenu pair)
1548 (setq pair (nthcdr (- erlang-man-max-menu-size 1) filelist))
1549 (setq segment filelist)
1552 (setq filelist (cdr pair))
1554 (setq submenu (erlang-man-make-menu segment))
1555 (setq menu (cons (list (concat (car (car submenu))
1557 (car (car (reverse submenu))))
1563 (defun erlang-man-make-menu (filelist)
1564 "Make a leaf menu based on FILELIST."
1568 (setq item (erlang-man-make-menu-item (car filelist)))
1570 (setq menu (cons item menu)))
1571 (setq filelist (cdr filelist)))
1575 (defun erlang-man-make-menu-item (file)
1576 "Create a menu item containing the name of the man page."
1577 (and (string-match ".*/\\([^/]+\\)\\.[^.]$" file)
1578 (let ((page (substring file (match-beginning 1) (match-end 1))))
1579 (list (capitalize page)
1582 (list 'funcall 'erlang-man-display-function
1586 (defun erlang-man-get-files (dir)
1587 "Return files in directory DIR."
1588 (directory-files dir t ".*\\.[0-9]\\'"))
1591 (defun erlang-man-module (&optional module)
1592 "Find manual page for MODULE, defaults to module of function under point.
1593 This function is aware of imported functions."
1595 (list (let* ((mod (car-safe (erlang-get-function-under-point)))
1597 (format "Manual entry for module%s: "
1598 (if (or (null mod) (string= mod ""))
1600 (format " (default %s)" mod))))))
1601 (if (string= input "")
1604 (or module (setq module (car (erlang-get-function-under-point))))
1605 (if (or (null module) (string= module ""))
1606 (error "No Erlang module name given"))
1607 (let ((dir-list erlang-man-dirs)
1608 (pat (concat "/" (regexp-quote module) "\\.[^.]$"))
1611 (while (and dir-list (null file))
1612 (setq file-list (erlang-man-get-files
1613 (if (nth 2 (car dir-list))
1614 (concat erlang-man-root-dir (nth 1 (car dir-list)))
1615 (nth 1 (car dir-list)))))
1616 (while (and file-list (null file))
1617 (if (string-match pat (car file-list))
1618 (setq file (car file-list)))
1619 (setq file-list (cdr file-list)))
1620 (setq dir-list (cdr dir-list)))
1622 (funcall erlang-man-display-function file)
1623 (error "No manual page for module %s found" module))))
1626 ;; Warning, the function `erlang-man-function' is a hack!
1627 ;; It links itself into the man code in a non-clean way. I have
1628 ;; chosen to keep it since it provides a very useful functionality
1629 ;; which is not possible to achieve using a clean approach.
1632 (defvar erlang-man-function-name nil
1633 "Name of function for last `erlang-man-function' call.
1634 Used for communication between `erlang-man-function' and the
1635 patch to `Man-notify-when-ready'.")
1637 (defun erlang-man-function (&optional name)
1638 "Find manual page for NAME, where NAME is module:function.
1639 The entry for `function' is displayed.
1641 This function is aware of imported functions."
1643 (list (let* ((mod-func (erlang-get-function-under-point))
1644 (mod (car-safe mod-func))
1645 (func (nth 1 mod-func))
1648 "Manual entry for `module:func' or `module'%s: "
1649 (if (or (null mod) (string= mod ""))
1651 (format " (default %s:%s)" mod func))))))
1652 (if (string= input "")
1654 (concat mod ":" func)
1657 ;; Emacs 18 doesn't provide `man'...
1664 (let ((mod-func (erlang-get-function-under-point)))
1665 (setq modname (car-safe mod-func))
1666 (setq funcname (nth 1 mod-func))))
1667 ((string-match ":" name)
1668 (setq modname (substring name 0 (match-beginning 0)))
1669 (setq funcname (substring name (match-end 0) nil)))
1671 (setq modname name)))
1672 (if (or (null modname) (string= modname ""))
1673 (error "No Erlang module name given"))
1674 (cond ((fboundp 'Man-notify-when-ready)
1675 ;; Emacs 19: The man command could possibly start an
1676 ;; asynchronous process, i.e. we must hook ourselves into
1677 ;; the system to be activated when the man-process
1681 (erlang-man-patch-notify)
1682 (setq erlang-man-function-name funcname))
1684 (erlang-man-module modname)
1685 (error (setq erlang-man-function-name nil))))
1687 (erlang-man-module modname)
1689 (erlang-man-find-function
1690 (or (get-buffer "*Manual Entry*") ; Emacs 18
1691 (current-buffer)) ; XEmacs
1695 ;; Should the defadvice be at the top level, the package `advice' would
1696 ;; be required. Now it is only required when this functionality
1697 ;; is used. (Emacs 19 specific.)
1698 (defun erlang-man-patch-notify ()
1699 "Patch the function `Man-notify-when-ready' to search for function.
1700 The variable `erlang-man-function-name' is assumed to be bound to
1701 the function name, or to nil.
1703 The reason for patching a function is that under Emacs 19, the man
1704 command is executed asynchronously."
1707 ;; This should never happened since this is only called when
1708 ;; running under Emacs 19.
1709 (error (error (concat "This command needs the package `advice', "
1710 "please upgrade your Emacs."))))
1712 (defadvice Man-notify-when-ready
1713 (after erlang-Man-notify-when-ready activate)
1714 "Set point at the documentation of the function name in
1715 `erlang-man-function-name' when the man page is displayed."
1716 (if erlang-man-function-name
1717 (erlang-man-find-function (ad-get-arg 0) erlang-man-function-name))
1718 (setq erlang-man-function-name nil)))
1721 (defun erlang-man-find-function (buf func)
1722 "Find manual page for function in `erlang-man-function-name' in buffer BUF."
1724 (let ((win (get-buffer-window buf)))
1728 (goto-char (point-min))
1729 (if (re-search-forward
1730 (concat "^[ \t]+" func " ?(")
1734 (set-window-point win (point)))
1735 (message "Could not find function `%s'" func)))))))
1738 (defun erlang-man-display (file)
1739 "Display FILE as a `man' file.
1740 This is the default manual page display function.
1741 The variables `erlang-man-display-function' contains the function
1743 ;; Emacs 18 doesn't `provide' man.
1748 (let ((process-environment (copy-sequence process-environment)))
1749 (if (string-match "\\(.*\\)/man[^/]*/\\([^/]+\\)\\.[^.]$" file)
1750 (let ((dir (substring file (match-beginning 1) (match-end 1)))
1751 (page (substring file (match-beginning 2) (match-end 2))))
1752 (if (fboundp 'setenv)
1753 (setenv "MANPATH" dir)
1755 (setq process-environment (cons (concat "MANPATH=" dir)
1756 process-environment)))
1757 (cond ((not (and (not erlang-xemacs-p)
1758 (= erlang-emacs-major-version 19)
1759 (< erlang-emacs-minor-version 29)))
1760 (manual-entry page))
1762 ;; Emacs 19.28 and earlier versions of 19:
1763 ;; The manual-entry command unconditionally prompts
1765 (funcall (symbol-function 'Man-getpage-in-background)
1767 (error "Can't find man page for %s\n" file)))))
1770 (defun erlang-man-describe-error ()
1771 "Describe why the manual pages weren't found."
1773 (with-output-to-temp-buffer "*Erlang Man Error*"
1774 (princ "Normally, this menu should contain Erlang manual pages.
1776 In order to find the manual pages, the variable `erlang-man-root-dir'
1777 should be bound to the name of the directory containing the Erlang
1778 man pages. The name should not include the final slash.
1780 Practically, you should add a line on the following form to
1781 your ~/.emacs, or ask your system administrator to add it to
1784 (setq erlang-man-root-dir \"/usr/local/erlang\")
1786 After installing the line, kill and restart Emacs, or restart Erlang
1787 mode with the command `M-x erlang-mode RET'.")))
1789 ;; Indentation code:
1791 (defun erlang-indent-command (&optional whole-exp)
1792 "Indent current line as Erlang code.
1793 With argument, indent any additional lines of the same clause
1794 rigidly along with this one."
1797 ;; If arg, always indent this line as Erlang
1798 ;; and shift remaining lines of clause the same amount.
1799 (let ((shift-amt (erlang-indent-line))
1802 (if erlang-tab-always-indent
1803 (beginning-of-line))
1805 (erlang-end-of-clause 1)
1811 (indent-code-rigidly beg end shift-amt "\n")))
1812 (if (and (not erlang-tab-always-indent)
1814 (skip-chars-backward " \t")
1817 (erlang-indent-line))))
1820 (defun erlang-indent-line ()
1821 "Indent current line as Erlang code.
1822 Return the amount the indentation changed by."
1823 (let ((pos (- (point-max) (point)))
1826 (beginning-of-line 1)
1828 (skip-chars-forward " \t")
1829 (cond ((looking-at "%")
1830 (setq indent (funcall comment-indent-function))
1831 (setq shift-amt (- indent (current-column))))
1833 (setq indent (erlang-calculate-indent))
1834 (cond ((null indent)
1835 (setq indent (current-indentation)))
1837 ;; This should never occur here.
1838 (error "Erlang mode error"))
1839 ((= (char-syntax (following-char)) ?\))
1840 (setq indent (1- indent))))
1841 (setq shift-amt (- indent (current-column)))))
1842 (if (zerop shift-amt)
1844 (delete-region beg (point))
1846 ;; If initial point was within line's indentation, position
1847 ;; after the indentation. Else stay at same point in text.
1848 (if (> (- (point-max) pos) (point))
1849 (goto-char (- (point-max) pos)))
1853 (defun erlang-indent-region (beg end)
1854 "Indent region of Erlang code.
1856 This is automagically called by the user level function `indent-region'."
1859 (let ((case-fold-search nil)
1861 (from-end (- (point-max) end))
1862 indent-point;; The beginning of the current line
1863 indent;; The indent amount
1867 (setq indent-point (point))
1868 (erlang-beginning-of-clause)
1869 ;; Parse the Erlang code from the beginning of the clause to
1870 ;; the beginning of the region.
1871 (while (< (point) indent-point)
1872 (setq state (erlang-partial-parse (point) indent-point state)))
1873 ;; Indent every line in the region
1875 (goto-char indent-point)
1876 (skip-chars-forward " \t")
1877 (cond ((looking-at "%")
1878 ;; Do not use our stack to help the user to customize
1879 ;; comment indentation.
1880 (setq indent (funcall comment-indent-function)))
1882 ;; Don't indent empty lines.
1887 (erlang-calculate-stack-indent (point) state)))
1888 (cond ((null indent)
1889 (setq indent (current-indentation)))
1891 ;; This should never occur here.
1892 (error "Erlang mode error"))
1893 ((= (char-syntax (following-char)) ?\))
1894 (setq indent (1- indent))))))
1895 (if (zerop (- indent (current-column)))
1897 (delete-region indent-point (point))
1899 ;; Find the next line in the region
1900 (goto-char indent-point)
1903 (setq indent-point (point)))
1904 (if (>= from-end (- (point-max) indent-point))
1906 (while (< (point) indent-point)
1907 (setq state (erlang-partial-parse
1908 (point) indent-point state))))))))
1911 (defun erlang-indent-current-buffer ()
1912 "Indent current buffer as Erlang code."
1917 (erlang-indent-region (point-min) (point-max)))))
1920 (defun erlang-indent-function ()
1921 "Indent current Erlang function."
1924 (let ((end (progn (erlang-end-of-function 1) (point)))
1925 (beg (progn (erlang-beginning-of-function 1) (point))))
1926 (erlang-indent-region beg end))))
1929 (defun erlang-indent-clause ()
1930 "Indent current Erlang clause."
1933 (let ((end (progn (erlang-end-of-clause 1) (point)))
1934 (beg (progn (erlang-beginning-of-clause 1) (point))))
1935 (erlang-indent-region beg end))))
1938 (defmacro erlang-push (x stack) (list 'setq stack (list 'cons x stack)))
1939 (defmacro erlang-pop (stack) (list 'setq stack (list 'cdr stack)))
1940 ;; Would much prefer to make caddr a macro but this clashes.
1941 (defun erlang-caddr (x) (car (cdr (cdr x))))
1944 (defun erlang-calculate-indent (&optional parse-start)
1945 "Compute appropriate indentation for current line as Erlang code.
1946 Return nil if line starts inside string, t if in a comment."
1948 (let ((indent-point (point))
1949 (case-fold-search nil)
1952 (goto-char parse-start)
1953 (erlang-beginning-of-clause))
1954 (while (< (point) indent-point)
1955 (setq state (erlang-partial-parse (point) indent-point state)))
1956 (erlang-calculate-stack-indent indent-point state))))
1958 (defun erlang-show-syntactic-information ()
1959 "Show syntactic information for current line."
1964 (let ((starting-point (point))
1965 (case-fold-search nil)
1967 (erlang-beginning-of-clause)
1968 (while (< (point) starting-point)
1969 (setq state (erlang-partial-parse (point) starting-point state)))
1970 (message "%S" state))))
1973 (defun erlang-partial-parse (from to &optional state)
1974 "Parse Erlang syntax starting at FROM until TO, with an optional STATE.
1975 Value is list (stack token-start token-type in-what)."
1976 (goto-char from) ; Start at the beginning
1977 (erlang-skip-blank to)
1978 (let ((cs (char-syntax (following-char)))
1984 ;; Done: Return previous state.
1986 (setq token (nth 1 state))
1987 (setq cs (nth 2 state))
1988 (setq in-what (nth 3 state)))
1990 ;; Word constituent: check and handle keywords.
1992 (cond ((looking-at "\\(end\\|after\\)[^_a-zA-Z0-9]")
1993 ;; Must pop top icr layer, `after' will push a new
1996 (while (and stack (eq (car (car stack)) '->))
1998 (if (and stack (memq (car (car stack)) '(icr begin)))
1999 (erlang-pop stack))))
2000 ((looking-at "catch[^,\n\\of]*\n")
2001 ;; Must pop top icr layer, `catch' in try/catch
2002 ;;will push a new layer next.
2004 (while (and stack (eq (car (car stack)) '->))
2006 (if (and stack (memq (car (car stack)) '(icr begin)))
2007 (erlang-pop stack))))
2008 ;;((looking-at "^of$")
2009 ;; Must pop top icr layer, `of' in try/catch
2010 ;;will push a new layer next.
2012 ;; (while (and stack (eq (car (car stack)) '->))
2013 ;; (erlang-pop stack))
2014 ;; (if (and stack (memq (car (car stack)) '(icr begin)))
2015 ;; (erlang-pop stack))))
2017 (cond ((looking-at "\\(if\\|case\\|receive\\|try\\)[^_a-zA-Z0-9]")
2018 ;; Must push a new icr (if/case/receive) layer.
2019 (erlang-push (list 'icr token (current-column)) stack))
2020 ((looking-at "\\(fun\\)[^_a-zA-Z0-9]")
2021 ;; Push a new icr layer if we are defining a `fun'
2022 ;; expression, not when we are refering an existing
2025 (goto-char (match-end 1))
2026 (erlang-skip-blank to)
2027 (eq (following-char) ?\())
2028 (erlang-push (list 'icr token (current-column)) stack)))
2029 ((looking-at "\\(begin\\|query\\)[^_a-zA-Z0-9]")
2030 (erlang-push (list 'begin token (current-column)) stack))
2031 ;; In test suites you may want to do something like
2032 ;; ?match(Mem when integer(Mem), mnesia:table_info(Tab,
2033 ;; memory)), and then the following if/case/receive
2034 ;; statement will mess up the indentation by fooling the
2035 ;; erlang mode to think the 'when' in the argument is a
2036 ;; "real" when. The following three clauses will avoid
2038 ((looking-at "when[^->\.]*if[^->\.]*->"))
2039 ((looking-at "when[^->\.]*case[^->\.]*->"))
2040 ((looking-at "when[^->\.]*receive[^->\.]*->"))
2042 ((looking-at "when [^->\.]*->")
2043 (erlang-push (list 'when token (current-column)) stack))
2044 ((looking-at "after[.]+->")
2045 (erlang-push (list 'icr token (current-column)) stack))
2046 ((looking-at "after[^_a-zA-Z0-9->]")
2047 ;; Probably in try-statment, fake "->" to get right
2048 ;; indentation in erlang-calculate-stack-indent. If it
2049 ;; was an ordinary catch without try, these entries will
2050 ;; be popped of the stack at a later ocaccion.
2051 (erlang-push (list 'icr token (current-column)) stack)
2052 (erlang-push (list '-> token (current-column)) stack))
2053 ((looking-at "catch[^,\n\\of]*\n")
2054 (erlang-push (list 'icr token (current-column)) stack)
2055 (erlang-push (list '-> token (current-column)) stack))
2056 ;;((looking-at "^of$")
2057 ;; (erlang-push (list 'icr token (current-column)) stack)
2058 ;;(erlang-push (list '-> token (current-column)) stack))
2061 ;; String: Try to skip over it. (Catch error if not complete.)
2068 (setq in-what 'string)
2071 (setq in-what 'string)
2074 ;; Expression prefix e.i. $ or ^ (Note ^ can be in the character
2075 ;; literal $^ or part of string and $ outside of a string denotes
2076 ;; a character literal)
2079 ((= (following-char) ?\") ;; $ or ^ was the last char in a string
2082 ;; Maybe a character literal, quote the next char to avoid
2083 ;; situations as $" being seen as the begining of a string.
2084 ;; Note the quoting something in the middle of a string is harmless.
2085 (quote (following-char))
2088 ;; Symbol constituent or punctuation
2094 ((= (following-char) ?\;)
2095 (if (and stack (eq (car (car stack)) '->))
2100 ((looking-at "\\.\\(\\s \\|\n\\|\\s<\\)")
2105 ((looking-at "->\\|:-")
2107 (back-to-indentation)
2108 (cond ((looking-at "after[^_a-zA-Z0-9]")
2109 (erlang-pop stack))))
2110 (if (and stack (eq (car (car stack)) 'when))
2112 (erlang-push (list '-> token (current-column)) stack)
2115 ;; List-comprehension divider
2117 (erlang-push (list '|| token (current-column)) stack)
2120 ;;((looking-at ",$")
2121 ;; Normal catch not try-catch have caused icr
2122 ;; and then incr and faked "->" should be removed
2124 ;; (back-to-indentation)
2125 ;; (cond ((looking-at "catch[^_a-zA-Z0-9]")
2126 ;; (erlang-pop stack)
2127 ;; (erlang-pop stack))))
2130 ;; Parameter separator
2134 ;; Bit-syntax open paren
2136 (erlang-push (list '\( token (current-column)) stack)
2139 ;; Bbit-syntax close paren
2141 (while (memq (car (car stack)) '(|| ->))
2143 (cond ((eq (car (car stack)) '\()
2145 ((memq (car (car stack)) '(icr begin))
2146 (error "Missing `end'"))
2148 (error "Unbalanced parentheses")))
2152 ((= (following-char) ??)
2157 ;; Other punctuation: Skip over it and any following punctuation
2159 ;; Skip over all characters in the operand.
2160 (skip-syntax-forward "."))
2162 ;; Other char: Skip over it.
2168 (erlang-push (list '\( token (current-column)) stack)
2171 ;; Close parenthesis
2173 (while (memq (car (car stack)) '(|| ->))
2175 (cond ((eq (car (car stack)) '\()
2177 ((eq (car (car stack)) 'icr)
2179 ;; Normal catch not try-catch might have caused icr
2180 ;; and then incr should be removed and is not an error.
2181 (if (eq (car (car stack)) '\()
2184 (error "Missing `end'"))
2186 ((eq (car (car stack)) 'begin)
2187 (error "Missing `end'")
2189 (error "Unbalanced parenthesis"))
2193 ;; Character quote: Skip it and the quoted char.
2197 ;; Character escape: Skip it and the escape sequence.
2200 (skip-syntax-forward "w"))
2205 (list stack token cs in-what)))
2207 (defun erlang-calculate-stack-indent (indent-point state)
2208 "From the given last position and state (stack) calculate indentation.
2209 Return nil if inside string, t if in a comment."
2210 (let* ((stack (and state (car state)))
2211 (token (nth 1 state))
2212 (stack-top (and stack (car stack))))
2213 (cond ((null state) ;No state
2217 (eq (nth 3 state) 'comment))
2219 (if (looking-at "when[^_a-zA-Z0-9]")
2222 ((eq (car stack-top) '\()
2223 ;; Element of list, tuple or part of an expression,
2224 (if (null erlang-argument-indent)
2225 ;; indent to next column.
2226 (1+ (nth 2 stack-top))
2227 (goto-char (nth 1 stack-top))
2228 (cond ((looking-at "[({]\\s *\\($\\|%\\)")
2229 ;; Line ends with parenthesis.
2230 (+ (erlang-indent-find-preceding-expr)
2231 erlang-argument-indent))
2233 ;; Indent to the same column as the first
2235 (goto-char (1+ (nth 1 stack-top)))
2236 (skip-chars-forward " \t")
2237 (current-column)))))
2238 ((eq (car stack-top) 'icr)
2239 ;; The default indentation is the column of the option
2240 ;; directly following the keyword. (This does not apply to
2241 ;; `case'.) Should no option be on the same line, the
2242 ;; indentation is the indentation of the keyword +
2243 ;; `erlang-indent-level'.
2245 ;; `after' should be indented to the save level as the
2246 ;; corresponding receive.
2247 (if (looking-at "after[^_a-zA-Z0-9]")
2250 (goto-char (nth 1 stack-top))
2251 (if (looking-at "case[^_a-zA-Z0-9]")
2252 (+ (nth 2 stack-top) erlang-indent-level)
2253 (skip-chars-forward "a-z")
2254 (skip-chars-forward " \t")
2255 (if (memq (following-char) '(?% ?\n))
2256 (+ (nth 2 stack-top) erlang-indent-level)
2257 (current-column)))))
2258 (if (looking-at "catch[^_a-zA-Z0-9]")
2261 (goto-char (nth 1 stack-top))
2262 (if (looking-at "case[^_a-zA-Z0-9]")
2263 (+ (nth 2 stack-top) erlang-indent-level)
2264 (skip-chars-forward "a-z")
2265 (skip-chars-forward " \t")
2266 (if (memq (following-char) '(?% ?\n))
2267 (+ (nth 2 stack-top) erlang-indent-level)
2268 (current-column)))))
2270 ;; Real indentation, where operators create extra indentation etc.
2271 ((memq (car stack-top) '(-> || begin))
2272 (goto-char (nth 1 stack-top))
2273 ;; Check if there is more code after the '->' on the
2274 ;; same line. If so use this indentation as base, else
2275 ;; use parent indentation + 2 * level as base.
2276 (let ((off erlang-indent-level)
2278 (cond ((null (cdr stack))) ; Top level in function.
2279 ((eq (car stack-top) 'begin)
2281 ((eq (car stack-top) '->)
2282 (setq off (* 2 erlang-indent-level))))
2283 (let ((base (erlang-indent-find-base stack indent-point off skip)))
2284 ;; Look at last thing to see how we are to move relative
2287 (cond ((looking-at "||\\|,\\|->\\|:-")
2289 ((erlang-at-keyword)
2290 (+ (current-column) erlang-indent-level))
2291 ((or (= (char-syntax (following-char)) ?.)
2292 (erlang-at-operator))
2293 (+ base erlang-indent-level))
2295 (goto-char indent-point)
2296 (cond ((memq (following-char) '(?\( ?{))
2297 ;; Function application or record.
2298 (+ (erlang-indent-find-preceding-expr)
2299 erlang-argument-indent))
2300 ;; Empty line, or end; treat it as the end of
2301 ;; the block. (Here we have a choice: should
2302 ;; the user be forced to reindent continued
2303 ;; lines, or should the "end" be reindented?)
2304 ((looking-at "\\(end\\|after\\|catch\\)[^_a-zA-Z0-9]\\|$")
2305 (if (eq (car (car stack)) '->)
2308 (erlang-caddr (car stack))
2310 ;; Avoid treating comments a continued line.
2311 ((= (following-char) ?%)
2313 ;; Continued line (e.g. line beginning
2314 ;; with an operator.)
2315 (t (+ base erlang-indent-level)))))))
2317 ((eq (car stack-top) 'when)
2318 (goto-char (nth 1 stack-top))
2319 (if (looking-at "when\\s *\\($\\|%\\)")
2322 (if (and stack (eq (nth 0 (car stack)) 'icr))
2324 (goto-char (nth 1 (car stack)))
2325 (+ (nth 2 (car stack)) erlang-indent-guard
2326 ;; receive XYZ or receive
2328 (if (looking-at "[a-z]+\\s *\\($\\|%\\)")
2330 (* 2 erlang-indent-level))))
2331 erlang-indent-guard))
2332 ;; "when" is followed by code, let's indent to the same
2334 (forward-char 4) ; Skip "when"
2335 (skip-chars-forward " \t")
2336 (current-column))))))
2339 (defun erlang-indent-find-base (stack indent-point &optional offset skip)
2340 "Find the base column for current stack."
2341 (or skip (setq skip 2))
2342 (or offset (setq offset erlang-indent-level))
2344 (let* ((stack-top (car stack)))
2345 (goto-char (nth 1 stack-top))
2347 (if (looking-at "\\s *\\($\\|%\\)")
2349 (if (memq (car stack-top) '(-> ||))
2351 ;; Take parent identation + offset,
2352 ;; else just erlang-indent-level if no parent
2354 (+ (erlang-caddr (car stack))
2356 erlang-indent-level))
2357 (erlang-skip-blank indent-point)
2358 (current-column)))))
2361 ;; Does not handle `begin' .. `end'.
2362 (defun erlang-indent-find-preceding-expr ()
2363 "Return the first column of the preceding expression.
2364 This assumes that the preceding expression is either simple
2365 \(i.e. an atom) or parenthesized."
2368 (let ((col (current-column)))
2369 (skip-chars-backward " \t")
2370 ;; Needed to match the colon in "'foo':'bar'".
2371 (if (not (memq (preceding-char) '(?# ?:)))
2375 (current-column)))))
2378 (defun erlang-skip-blank (&optional lim)
2379 "Skip over whitespace and comments until limit reached."
2380 (or lim (setq lim (point-max)))
2382 (while (and (not stop) (< (point) lim))
2383 (cond ((= (following-char) ?%)
2384 (skip-chars-forward "^\n" lim))
2385 ((= (following-char) ?\n)
2386 (skip-chars-forward "\n" lim))
2387 ((looking-at "\\s ")
2388 (if (re-search-forward "\\S " lim 'move)
2394 (defun erlang-at-keyword ()
2395 "Are we looking at an Erlang keyword which will increase indentation?"
2396 (looking-at (concat "\\(when\\|if\\|fun\\|case\\|begin\\|query\\|"
2397 "of\\|receive\\|after\\|catch\\)[^_a-zA-Z0-9]")))
2399 (defun erlang-at-operator ()
2400 "Are we looking at an Erlang operator?"
2402 "\\(bnot\\|div\\|mod\\|band\\|bor\\|bxor\\|bsl\\|bsr\\)[^_a-zA-Z0-9]"))
2404 (defun erlang-comment-indent ()
2405 "Compute Erlang comment indentation.
2407 Used both by `indent-for-comment' and the Erlang specific indentation
2409 (cond ((looking-at "%%%") 0)
2411 (or (erlang-calculate-indent)
2412 (current-indentation)))
2415 (skip-chars-backward " \t")
2416 (max (if (bolp) 0 (1+ (current-column)))
2419 ;;; Erlang movement commands
2421 ;; All commands below work as movement commands. I.e. if the point is
2422 ;; at the end of the clause, and the command `erlang-end-of-clause' is
2423 ;; executed, the point is moved to the end of the NEXT clause. (This
2424 ;; mimics the behaviour of `end-of-defun'.)
2426 ;; Personally I would like to rewrite them to be "pure", and add a set
2427 ;; of movement functions, like `erlang-next-clause',
2428 ;; `erlang-previous-clause', and the same for functions.
2430 ;; The current implementation makes it hopeless to use the functions as
2431 ;; subroutines in more complex commands. /andersl
2433 (defun erlang-beginning-of-clause (&optional arg)
2434 "Move backward to previous start of clause.
2435 With argument, do this that many times.
2436 Return t unless search stops due to end of buffer."
2438 (or arg (setq arg 1))
2440 ;; Step back to the end of the previous line, unless we are at
2441 ;; the beginning of the buffer. The reason for this move is
2442 ;; that the regexp below includes the last character of the
2445 (or (looking-at "\n")
2448 (if (looking-at "\\`\n")
2450 ;; The regexp matches a function header that isn't
2451 ;; included in a string.
2452 (and (re-search-forward "\\(\\`\\|\\`\n\\|[^\\]\n\\)\\([a-z]\\|'\\|-\\)"
2454 (let ((beg (match-beginning 2)))
2455 (and beg (goto-char beg))
2458 (defun erlang-end-of-clause (&optional arg)
2459 "Move to the end of the current clause.
2460 With argument, do this that many times."
2462 (or arg (setq arg 1))
2463 (while (and (looking-at "[ \t]*[%\n]")
2464 (zerop (forward-line 1))))
2465 ;; Move to the next clause.
2466 (erlang-beginning-of-clause (- arg))
2467 (beginning-of-line);; Just to be sure...
2469 (while (and (not (bobp)) continue)
2471 (skip-chars-forward " \t")
2472 (if (looking-at "[%\n]")
2475 (setq continue nil)))))
2477 (defun erlang-mark-clause ()
2478 "Put mark at end of clause, point at beginning."
2481 (erlang-end-of-clause 1)
2482 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2485 (push-mark (point) nil t)
2486 (error (push-mark (point))))
2487 (erlang-beginning-of-clause 1)
2488 ;; The above function deactivates the mark.
2489 (if (boundp 'deactivate-mark)
2490 (funcall (symbol-function 'set) 'deactivate-mark nil)))
2492 (defun erlang-beginning-of-function (&optional arg)
2493 "Move backward to previous start of function.
2494 With positive argument, do this that many times.
2495 With negative argument, search forward.
2497 Return t unless search stops due to end of buffer."
2499 (or arg (setq arg 1))
2503 (while (and (> arg 0)
2504 (and (erlang-beginning-of-clause 1)
2505 (let ((start (point))
2506 (name (erlang-name-of-function))
2507 (arity (erlang-get-function-arity)))
2508 ;; Note: "arity" is nil for e.g. "-import", hence
2509 ;; two "-import" clauses are not considered to
2510 ;; be part of the same function.
2511 (while (and (erlang-beginning-of-clause 1)
2513 (erlang-name-of-function))
2516 (erlang-get-function-arity)))
2517 (setq start (point)))
2520 (setq arg (1- arg))))
2524 (erlang-beginning-of-clause 1)
2525 ;; Step -arg functions forward.
2526 (while (and (< arg 0)
2527 ;; Step one function forward, or stop if the end of
2528 ;; the buffer was reached. Return t if we found the
2530 (let ((name (erlang-name-of-function))
2531 (arity (erlang-get-function-arity))
2532 (found (erlang-beginning-of-clause -1)))
2534 (string-equal name (erlang-name-of-function))
2537 (erlang-get-function-arity)))
2538 (setq found (erlang-beginning-of-clause -1)))
2540 (setq arg (1+ arg)))))
2544 (defun erlang-end-of-function (&optional arg)
2545 "Move forward to next end of function.
2547 With argument, do this that many times.
2548 With negative argument go towards the beginning of the buffer."
2550 (or arg (setq arg 1))
2553 (while (and (> arg 0) (< (point) (point-max)))
2554 (let ((pos (point)))
2559 (erlang-beginning-of-clause 1)))
2561 (or (bobp) (forward-char -1))
2562 (erlang-beginning-of-clause -1))
2564 (erlang-pass-over-function)
2565 (skip-chars-forward " \t")
2566 (if (looking-at "[%\n]")
2569 (setq arg (1- arg)))
2572 (let ((pos (point)))
2573 (erlang-beginning-of-clause 1)
2574 (erlang-pass-over-function)
2576 (if (>= (point) pos)
2577 (if (erlang-beginning-of-function 2)
2579 (erlang-pass-over-function)
2580 (skip-chars-forward " \t")
2581 (if (looking-at "[%\n]")
2583 (goto-char (point-min)))))
2584 (setq arg (1+ arg)))))
2587 (if (default-boundp 'beginning-of-defun-function)
2588 (defalias 'erlang-mark-function 'mark-defun)
2589 (defun erlang-mark-function ()
2590 "Put mark at end of function, point at beginning."
2593 (erlang-end-of-function 1)
2594 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2597 (push-mark (point) nil t)
2598 (error (push-mark (point))))
2599 (erlang-beginning-of-function 1)
2600 ;; The above function deactivates the mark.
2601 (if (boundp 'deactivate-mark)
2602 (funcall (symbol-function 'set) 'deactivate-mark nil)))))
2604 (defun erlang-pass-over-function ()
2607 (and (not (looking-at "\\.\\(\\s \\|\n\\|\\s<\\)"))
2613 (defun erlang-name-of-function ()
2615 ;; Skip over attribute leader.
2616 (if (looking-at "-[ \t]*")
2617 (re-search-forward "-[ \t]*" nil 'move))
2618 (let ((start (point)))
2620 (buffer-substring start (point)))))
2625 (defun erlang-fill-paragraph (&optional justify)
2626 "Like \\[fill-paragraph], but handle Erlang comments.
2627 If any of the current line is a comment, fill the comment or the
2628 paragraph of it that point is in, preserving the comment's indentation
2631 (let ((has-comment nil)
2632 ;; If has-comment, the appropriate fill-prefix for the comment.
2633 comment-fill-prefix)
2634 ;; Figure out what kind of comment we are looking at.
2638 ;; Find the command prefix.
2639 ((looking-at (concat "\\s *" comment-start-skip))
2640 (setq has-comment t)
2641 (setq comment-fill-prefix (buffer-substring (match-beginning 0)
2643 ;; A line with some code, followed by a comment? Remember that the
2644 ;; % which starts the comment shouldn't be part of a string or
2647 (while (not (looking-at "%\\|$"))
2648 (skip-chars-forward "^%\n\"\\\\")
2650 ((eq (char-after (point)) ?\\) (forward-char 2))
2651 ((eq (char-after (point)) ?\") (forward-sexp 1))))
2652 (looking-at comment-start-skip))
2653 (setq has-comment t)
2654 (setq comment-fill-prefix
2655 (concat (make-string (current-column) ? )
2656 (buffer-substring (match-beginning 0) (match-end 0)))))))
2657 (if (not has-comment)
2658 (fill-paragraph justify)
2659 ;; Narrow to include only the comment, and then fill the region.
2662 ;; Find the first line we should include in the region to fill.
2664 (while (and (zerop (forward-line -1))
2665 (looking-at "^\\s *%")))
2666 ;; We may have gone to far. Go forward again.
2667 (or (looking-at "^\\s *%")
2670 ;; Find the beginning of the first line past the region to fill.
2672 (while (progn (forward-line 1)
2673 (looking-at "^\\s *%")))
2675 ;; Lines with only % on them can be paragraph boundaries.
2676 (let ((paragraph-start (concat paragraph-start "\\|^[ \t%]*$"))
2677 (paragraph-separate (concat paragraph-start "\\|^[ \t%]*$"))
2678 (fill-prefix comment-fill-prefix))
2679 (fill-paragraph justify))))))
2682 (defun erlang-uncomment-region (beg end)
2683 "Uncomment all commented lines in the region."
2685 (comment-region beg end -1))
2688 (defun erlang-generate-new-clause ()
2689 "Create additional Erlang clause header.
2691 Parses the source file for the name of the current Erlang function.
2692 Create the header containing the name, A pair of parentheses,
2693 and an arrow. The space between the function name and the
2694 first parenthesis is preserved. The point is placed between
2697 (let ((name (save-excursion
2698 (and (erlang-beginning-of-clause)
2699 (erlang-get-function-name t))))
2700 (arrow (save-excursion
2701 (and (erlang-beginning-of-clause)
2702 (erlang-get-function-arrow)))))
2703 (if (or (null arrow) (null name))
2704 (error "Can't find name of current Erlang function"))
2705 (if (and (bolp) (eolp))
2711 (insert ") " arrow))
2712 (if erlang-new-clause-with-arguments
2713 (erlang-clone-arguments))))
2716 (defun erlang-clone-arguments ()
2717 "Insert, at the point, the argument list of the previous clause.
2719 The mark is set at the beginning of the inserted text, the point
2722 (let ((args (save-excursion
2724 (and (erlang-beginning-of-clause)
2725 (erlang-get-function-arguments))))
2728 (error "Can't clone argument list"))
2732 ;;; Information retrieval functions.
2734 (defun erlang-buffer-substring (beg end)
2735 "Like `buffer-substring-no-properties'.
2736 Although, this function works on all versions of Emacs."
2737 (if (fboundp 'buffer-substring-no-properties)
2738 (funcall (symbol-function 'buffer-substring-no-properties) beg end)
2739 (buffer-substring beg end)))
2742 (defun erlang-get-module ()
2743 "Return the name of the module as specified by `-module'.
2745 Return nil if file contains no `-module' attribute."
2749 (goto-char (point-min))
2750 (let ((md (match-data)))
2752 (if (re-search-forward
2754 (concat "^-module\\s *(\\s *\\(\\("
2756 "\\)?\\)\\s *)\\s *\\."))
2758 (erlang-remove-quotes
2759 (erlang-buffer-substring (match-beginning 1)
2762 (store-match-data md))))))
2765 (defun erlang-get-module-from-file-name (&optional file)
2766 "Extract the module name from a file name.
2768 First, the directory part is removed. Second, the part of the file name
2769 matching `erlang-file-name-extension-regexp' is removed.
2771 Should the match fail, nil is returned.
2773 By modifying `erlang-file-name-extension-regexp' to match files other
2774 than Erlang source files, Erlang specific functions could be applied on
2775 non-Erlang files. Most notably; the support for Erlang modules in the
2776 tags system could be used by files written in other languages."
2777 (or file (setq file buffer-file-name))
2780 (setq file (file-name-nondirectory file))
2781 (if (string-match erlang-file-name-extension-regexp file)
2782 (substring file 0 (match-beginning 0))
2786 ;; Used by `erlang-get-export' and `erlang-get-import'.
2788 (defun erlang-get-function-arity-list ()
2789 "Parse list of `function/arity' as used by `-import' and `-export'.
2791 Point must be before the opening bracket. When the
2792 function returns the point will be placed after the closing bracket.
2794 The function does not return an error if the list is incorrectly
2797 Return list of (function . arity). The order of the returned list
2798 corresponds to the order of the parsed Erlang list."
2802 (if (not (eq (preceding-char) ?\[))
2803 '() ; Not looking at an Erlang list.
2804 (while ; Note: `while' has no body.
2807 (and (looking-at (eval-when-compile
2808 (concat erlang-atom-regexp "/\\([0-9]+\\)\\>")))
2812 (erlang-remove-quotes
2813 (erlang-buffer-substring
2814 (match-beginning 1) (match-end 1)))
2816 (erlang-buffer-substring
2818 (+ 1 erlang-atom-regexp-matches))
2820 (+ 1 erlang-atom-regexp-matches)))))
2822 (goto-char (match-end 0))
2825 ;; Test if there are more exported functions.
2826 (eq (preceding-char) ?,))))))
2830 ;;; Note that `-export' and the open parenthesis must be written on
2833 (defun erlang-get-export ()
2834 "Return a list of `(function . arity)' as specified by `-export'."
2836 (goto-char (point-min))
2837 (let ((md (match-data))
2841 (while (re-search-forward "^-export\\s *(" (point-max) t)
2843 (setq res (nconc res (erlang-get-function-arity-list))))
2845 (store-match-data md)))))
2848 (defun erlang-get-import ()
2849 "Parse an Erlang source file for imported functions.
2851 Return an alist with module name as car part and list of conses containing
2852 function and arity as cdr part."
2854 (goto-char (point-min))
2855 (let ((md (match-data))
2859 (while (re-search-forward "^-import\\s *(" (point-max) t)
2861 (if (looking-at erlang-atom-regexp)
2862 (let ((module (erlang-remove-quotes
2863 (erlang-buffer-substring
2866 (goto-char (match-end 0))
2868 (if (eq (following-char) ?,)
2872 (let ((funcs (erlang-get-function-arity-list))
2873 (pair (assoc module res)))
2875 (setcdr pair (nconc (cdr pair) funcs))
2876 (setq res (cons (cons module funcs)
2879 (store-match-data md)))))
2882 (defun erlang-get-function-name (&optional arg)
2883 "Return name of current function, or nil.
2885 If optional argument is non-nil, everything up to and including
2886 the first `(' is returned.
2888 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2890 (if (not (eobp)) (forward-char 1))
2891 (and (erlang-beginning-of-clause)
2892 (erlang-get-function-name t)))"
2893 (let ((n (if arg 0 1)))
2894 (and (looking-at (eval-when-compile
2895 (concat "^" erlang-atom-regexp "\\s *(")))
2896 (erlang-buffer-substring (match-beginning n) (match-end n)))))
2899 (defun erlang-get-function-arrow ()
2900 "Return arrow of current function, could be \"->\", \":-\" or nil.
2902 The \":-\" arrow is used by mnesia queries.
2904 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2906 (if (not (eobp)) (forward-char 1))
2907 (and (erlang-beginning-of-clause)
2908 (erlang-get-function-arrow)))"
2911 (re-search-forward "[^-:]*-\\|:" (point-max) t)
2912 (erlang-buffer-substring (- (point) 1) (+ (point) 1)))))
2914 (defun erlang-get-function-arity ()
2915 "Return the number of arguments of function at point, or nil."
2916 (and (looking-at (eval-when-compile
2917 (concat "^" erlang-atom-regexp "\\s *(")))
2919 (goto-char (match-end 0))
2927 ((looking-at "\\s *)")
2929 ((looking-at "\\s *\\($\\|%\\)")
2931 ((looking-at "\\s *,")
2932 (setq res (+ 1 res))
2933 (goto-char (match-end 0)))
2936 (setq res (+ 1 res)))
2941 (defun erlang-get-function-arguments ()
2942 "Return arguments of current function, or nil."
2943 (if (not (looking-at (eval-when-compile
2944 (concat "^" erlang-atom-regexp "\\s *("))))
2948 (let ((start (match-end 0)))
2949 (goto-char (- start 1))
2951 (erlang-buffer-substring start (- (point) 1)))
2955 (defun erlang-get-function-under-point ()
2956 "Return the module and function under the point, or nil.
2958 Should no explicit module name be present at the point, the
2959 list of imported functions is searched.
2961 The following could be returned:
2962 (\"module\" \"function\") -- Both module and function name found.
2963 (nil \"function\") -- No module name was found.
2964 nil -- No function name found
2966 In the future the list may contain more elements."
2968 (let ((md (match-data))
2970 (if (eq (char-syntax (following-char)) ? )
2971 (skip-chars-backward " \t"))
2972 (skip-chars-backward "a-zA-Z0-9_:'")
2973 (cond ((looking-at (eval-when-compile
2974 (concat erlang-atom-regexp ":" erlang-atom-regexp)))
2976 (erlang-remove-quotes
2977 (erlang-buffer-substring
2978 (match-beginning 1) (match-end 1)))
2979 (erlang-remove-quotes
2980 (erlang-buffer-substring
2981 (match-beginning (1+ erlang-atom-regexp-matches))
2982 (match-end (1+ erlang-atom-regexp-matches)))))))
2983 ((looking-at erlang-atom-regexp)
2984 (let ((fk (erlang-remove-quotes
2985 (erlang-buffer-substring
2986 (match-beginning 0) (match-end 0))))
2988 (imports (erlang-get-import)))
2989 (while (and imports (null mod))
2990 (if (assoc fk (cdr (car imports)))
2991 (setq mod (car (car imports)))
2992 (setq imports (cdr imports))))
2993 (setq res (list mod fk)))))
2994 (store-match-data md)
2998 ;; TODO: Escape single quotes inside the string without
2999 ;; replace-regexp-in-string.
3000 (defun erlang-add-quotes-if-needed (str)
3001 "Return STR, possibly with quotes."
3002 (if (and (stringp str)
3003 (not (string-match (eval-when-compile
3004 (concat "\\`" erlang-atom-regexp "\\'")) str)))
3005 (progn (if (fboundp 'replace-regexp-in-string)
3006 (setq str (replace-regexp-in-string "'" "\\'" str t t )))
3007 (concat "'" str "'"))
3011 (defun erlang-remove-quotes (str)
3012 "Return STR without quotes, if present."
3013 (let ((md (match-data)))
3015 (if (string-match "\\`'\\(.*\\)'\\'" str)
3016 (substring str 1 -1)
3018 (store-match-data md))))
3021 ;;; Check module name
3023 ;; The function `write-file', bound to C-x C-w, calls
3024 ;; `set-visited-file-name' which clears the hook. :-(
3025 ;; To make sure that the hook always is present, we advise
3026 ;; `set-visited-file-name'.
3027 (defun erlang-check-module-name-init ()
3028 "Initialize the functionality to compare file and module names.
3030 Unless we have `before-save-hook', we redefine the function
3031 `set-visited-file-name' since it clears the variable
3032 `local-write-file-hooks'. The original function definition is
3033 stored in `erlang-orig-set-visited-file-name'."
3034 (if (boundp 'before-save-hook)
3035 ;; If we have that, `make-local-hook' is obsolete.
3036 (add-hook 'before-save-hook 'erlang-check-module-name nil t)
3038 (unless (ad-advised-definition-p 'set-visited-file-name)
3039 (defadvice set-visited-file-name (after erlang-set-visited-file-name
3041 (if (eq major-mode 'erlang-mode)
3042 (add-hook 'local-write-file-hooks 'erlang-check-module-name))))
3043 (add-hook 'local-write-file-hooks 'erlang-check-module-name)))
3046 (defun erlang-check-module-name ()
3047 "If the module name doesn't match file name, ask for permission to change.
3049 The variable `erlang-check-module-name' controls the behaviour of this
3050 function. It it is nil, this function does nothing. If it is t, the
3051 source is silently changed. If it is set to the atom `ask', the user
3054 This function is normally placed in the hook `local-write-file-hooks'."
3055 (if erlang-check-module-name
3056 (let ((mn (erlang-get-module))
3057 (fn (erlang-get-module-from-file-name (buffer-file-name))))
3058 (if (and (stringp mn) (stringp fn))
3059 (or (string-equal mn fn)
3060 (if (or (eq erlang-check-module-name t)
3062 "Module does not match file name. Modify source? "))
3066 (goto-char (point-min))
3067 (if (re-search-forward
3069 (concat "^-module\\s *(\\s *\\(\\("
3071 "\\)?\\)\\s *)\\s *\\."))
3074 (goto-char (match-beginning 1))
3075 (delete-region (match-beginning 1)
3077 (insert fn))))))))))
3078 ;; Must return nil since it is added to `local-write-file-hook'.
3082 ;;; Electric functions.
3084 (defun erlang-electric-semicolon (&optional arg)
3085 "Insert a semicolon character and possibly a prototype for the next line.
3087 The variable `erlang-electric-semicolon-criteria' states a criterion,
3088 when fulfilled a newline is inserted, the next line is indented and a
3089 prototype for the next line is inserted. Normally the prototype
3090 consists of \" ->\". Should the semicolon end the clause a new clause
3091 header is generated.
3093 The variable `erlang-electric-semicolon-insert-blank-lines' controls
3094 the number of blank lines inserted between the current line and new
3097 Behaves just like the normal semicolon when supplied with a
3098 numerical arg, point is inside string or comment, or when there are
3099 non-whitespace characters following the point on the current line."
3101 (self-insert-command (prefix-numeric-value arg))
3103 (and (listp erlang-electric-commands)
3104 (not (memq 'erlang-electric-semicolon
3105 erlang-electric-commands)))
3107 (not (looking-at "\\s *\\(%.*\\)?$"))
3108 (null (erlang-test-criteria-list
3109 erlang-electric-semicolon-criteria)))
3110 (setq erlang-electric-newline-inhibit nil)
3111 (setq erlang-electric-newline-inhibit t)
3115 (if (condition-case nil
3116 (progn (erlang-indent-line) t)
3117 (error (if (bolp) (delete-backward-char 1))))
3123 (erlang-generate-new-clause)
3124 (if erlang-electric-semicolon-insert-blank-lines
3128 erlang-electric-semicolon-insert-blank-lines))))
3129 (error (if (bolp) (delete-backward-char 1))))))))
3132 (defun erlang-electric-comma (&optional arg)
3133 "Insert a comma character and possibly a new indented line.
3134 The variable `erlang-electric-comma-criteria' states a criterion,
3135 when fulfilled a newline is inserted and the next line is indented.
3137 Behaves just like the normal comma when supplied with a
3138 numerical arg, point is inside string or comment, or when there are
3139 non-whitespace characters following the point on the current line."
3142 (self-insert-command (prefix-numeric-value arg))
3145 (and (listp erlang-electric-commands)
3146 (not (memq 'erlang-electric-comma erlang-electric-commands)))
3148 (not (looking-at "\\s *\\(%.*\\)?$"))
3149 (null (erlang-test-criteria-list
3150 erlang-electric-comma-criteria)))
3151 (setq erlang-electric-newline-inhibit nil)
3152 (setq erlang-electric-newline-inhibit t)
3157 (erlang-indent-line)
3158 (error (if (bolp) (delete-backward-char 1))))))
3160 (defun erlang-electric-lt (&optional arg)
3161 "Insert a less-than sign, and optionally mark it as an open paren."
3165 (self-insert-command arg)
3167 ;; Was this the second char in bit-syntax open (`<<')?
3168 (unless (< (point) 2)
3171 (when (and (eq (char-after (point)) ?<)
3172 (not (eq (get-text-property (point) 'category)
3173 'bitsyntax-open-inner)))
3174 ;; Then mark the two chars...
3175 (put-text-property (point) (1+ (point))
3176 'category 'bitsyntax-open-outer)
3178 (put-text-property (point) (1+ (point))
3179 'category 'bitsyntax-open-inner)
3180 ;;...and unmark any subsequent less-than chars.
3182 (while (eq (char-after (point)) ?<)
3183 (remove-text-properties (point) (1+ (point))
3185 (forward-char 1))))))
3187 (defun erlang-after-bitsyntax-close ()
3188 "Return t if point is immediately after a bit-syntax close parenthesis (`>>')."
3192 (and (eq (char-after (point)) ?>)
3193 (not (eq (get-text-property (point) 'category)
3194 'bitsyntax-close-outer))))))
3196 (defun erlang-after-arrow ()
3197 "Return true if point is immediately after a function arrow (`->')."
3202 (eq (char-before (point)) ?-))
3203 (or (not (listp erlang-electric-commands))
3204 (memq 'erlang-electric-gt
3205 erlang-electric-commands))
3206 (not (erlang-in-literal))
3207 (looking-at "\\s *\\(%.*\\)?$")
3208 (erlang-test-criteria-list erlang-electric-arrow-criteria))))
3211 (defun erlang-electric-gt (&optional arg)
3212 "Insert a greater-than sign, and optionally mark it as a close paren."
3216 (self-insert-command arg)
3219 ;; Did we just write a bit-syntax close (`>>')?
3220 ((erlang-after-bitsyntax-close)
3222 ;; Then mark the two chars...
3224 (put-text-property (point) (1+ (point))
3225 'category 'bitsyntax-close-inner)
3227 (put-text-property (point) (1+ (point))
3228 'category 'bitsyntax-close-outer)
3229 ;;...and unmark any subsequent greater-than chars.
3231 (while (eq (char-after (point)) ?>)
3232 (remove-text-properties (point) (1+ (point))
3236 ;; Did we just write a function arrow (`->')?
3237 ((erlang-after-arrow)
3238 (let ((erlang-electric-newline-inhibit t))
3243 (erlang-indent-line)
3244 (error (if (bolp) (delete-backward-char 1))))))
3246 ;; Then it's just a plain greater-than.
3251 (defun erlang-electric-arrow\ off (&optional arg)
3252 "Insert a '>'-sign and possibly a new indented line.
3254 This command is only `electric' when the `>' is part of an `->' arrow.
3255 The variable `erlang-electric-arrow-criteria' states a sequence of
3256 criteria, which decides when a newline should be inserted and the next
3259 It behaves just like the normal greater than sign when supplied with a
3260 numerical arg, point is inside string or comment, or when there are
3261 non-whitespace characters following the point on the current line.
3263 After being split/merged into `erlang-after-arrow' and
3264 `erlang-electric-gt', it is now unused and disabled."
3266 (let ((prec (preceding-char)))
3267 (self-insert-command (prefix-numeric-value arg))
3269 (and (listp erlang-electric-commands)
3270 (not (memq 'erlang-electric-arrow
3271 erlang-electric-commands)))
3274 (not (looking-at "\\s *\\(%.*\\)?$"))
3275 (null (erlang-test-criteria-list
3276 erlang-electric-arrow-criteria)))
3277 (setq erlang-electric-newline-inhibit nil)
3278 (setq erlang-electric-newline-inhibit t)
3283 (erlang-indent-line)
3284 (error (if (bolp) (delete-backward-char 1)))))))
3287 (defun erlang-electric-newline (&optional arg)
3288 "Break line at point and indent, continuing comment if within one.
3289 The variable `erlang-electric-newline-criteria' states a criterion,
3290 when fulfilled a newline is inserted and the next line is indented.
3292 Should the current line begin with a comment, and the variable
3293 `comment-multi-line' be non-nil, a new comment start is inserted.
3295 Should the previous command be another electric command we assume that
3296 the user pressed newline out of old habit, hence we will do nothing."
3298 (cond ((and (not arg)
3299 erlang-electric-newline-inhibit
3300 (memq last-command erlang-electric-newline-inhibit-list))
3303 (and (listp erlang-electric-commands)
3304 (not (memq 'erlang-electric-newline
3305 erlang-electric-commands)))
3306 (null (erlang-test-criteria-list
3307 erlang-electric-newline-criteria)))
3308 (newline (prefix-numeric-value arg)))
3310 (if (and comment-multi-line
3313 (looking-at (concat "\\s *" comment-start-skip))))
3314 (let ((str (buffer-substring
3315 (or (match-end 1) (match-beginning 0))
3316 (min (match-end 0) (point)))))
3317 (newline-and-indent)
3320 (newline-and-indent)))))
3323 (defun erlang-test-criteria-list (criteria)
3324 "Given a list of criterion functions, test if criteria are fulfilled.
3326 Each element in the criteria list can a function returning nil, t or
3327 the atom `stop'. t means that the criterion is fulfilled, `stop' means
3328 that it isn't fulfilled and that the search should stop,
3329 and nil means continue searching.
3331 Should the list contain the atom t the criterion is assumed to be
3332 fulfilled, unless preceded by a function returning `stop', of course.
3334 Should the argument be the atom t instead of a list, the criterion is
3335 assumed to be trivially true.
3337 Should all functions return nil, the criteria are assumed not to be
3340 Return t if criteria fulfilled, nil otherwise."
3345 (while (and criteria (null answer))
3346 (if (eq (car criteria) t)
3348 (setq answer (funcall (car criteria))))
3349 (setq criteria (cdr criteria)))
3350 (if (and answer (not (eq answer 'stop)))
3355 (defun erlang-in-literal (&optional lim)
3356 "Test if point is in string, quoted atom or comment.
3358 Return one of the three atoms `atom', `string', and `comment'.
3359 Should the point be inside none of the above mentioned types of
3360 context, nil is returned."
3362 (let* ((lim (or lim (save-excursion
3363 (erlang-beginning-of-clause)
3365 (state (if (fboundp 'syntax-ppss) ; post Emacs 21.3
3367 (parse-partial-sexp lim (point)))))
3369 ((eq (nth 3 state) ?') 'atom)
3370 ((nth 3 state) 'string)
3371 ((nth 4 state) 'comment)
3375 (defun erlang-at-end-of-function-p ()
3376 "Test if point is at end of an Erlang function.
3378 This function is designed to be a member of a criteria list."
3379 (eq (save-excursion (erlang-skip-blank) (point))
3381 (erlang-beginning-of-function -1) (point))))
3384 (defun erlang-stop-when-inside-argument-list ()
3385 "Return `stop' if inside parenthesis list, nil otherwise.
3387 Knows about the list comprehension syntax. When the point is
3388 after `||', `stop' is not returned.
3390 This function is designed to be a member of a criteria list."
3393 (let ((orig-point (point))
3396 (if (not (eq (following-char) ?\[))
3398 ;; Do not return `stop' when inside a list comprehension
3399 ;; construction. (The point must be after `||').
3400 (while (< (point) orig-point)
3401 (setq state (erlang-partial-parse (point) orig-point state)))
3402 (if (and (car state) (eq (car (car (car state))) '||))
3409 (defun erlang-stop-when-at-guard ()
3410 "Return `stop' when at function guards.
3412 This function is designed to be a member of a criteria list."
3415 (if (and (looking-at (eval-when-compile
3416 (concat "^" erlang-atom-regexp "\\s *(")))
3419 (concat "^" erlang-atom-regexp ".*\\(->\\|:-\\)")))))
3424 (defun erlang-next-lines-empty-p ()
3425 "Return non-nil if next lines are empty.
3427 The variable `erlang-next-lines-empty-threshold' contains the number
3428 of lines required to be empty.
3430 A line containing only spaces and tabs is considered empty.
3432 This function is designed to be a member of a criteria list."
3433 (and erlang-next-lines-empty-threshold
3435 (let ((left erlang-next-lines-empty-threshold)
3437 (while (and cont (> left 0))
3439 (setq cont (looking-at "\\s *$"))
3440 (setq left (- left 1)))
3444 (defun erlang-at-keyword-end-p ()
3445 "Test if next readable token is the keyword end.
3447 This function is designed to be a member of a criteria list."
3450 (looking-at "end[^_a-zA-Z0-9]")))
3453 ;; Erlang tags support which is aware of erlang modules.
3455 ;; Not yet implemented under XEmacs. (Hint: The Emacs 19 etags
3456 ;; package works under XEmacs.)
3459 (if (or (featurep 'bytecomp)
3460 (featurep 'byte-compile))
3467 (defvar erlang-tags-function-alist
3468 '((find-tag . erlang-find-tag)
3469 (find-tag-other-window . erlang-find-tag-other-window)
3470 (find-tag-regexp . erlang-find-tag-regexp)
3471 (find-tag-other-frame . erlang-find-tag-other-frame))
3472 "Alist of old tags commands and the replacement functions.")
3474 (defvar erlang-tags-installed nil
3475 "Non-nil when the Erlang tags system is installed.")
3476 (defvar erlang-tags-file-list '()
3477 "List of files in tag list. Used when finding tag on form `module:'.")
3478 (defvar erlang-tags-completion-table nil
3479 "Like `tags-completion-table', this table contains `tag' and `module:tag'.")
3480 (defvar erlang-tags-buffer-installed-p nil
3481 "Non-nil when Erlang module recognising functions installed.")
3482 (defvar erlang-tags-buffer-list '()
3483 "Temporary list of buffers.")
3484 (defvar erlang-tags-orig-completion-table nil
3485 "Temporary storage for `tags-completion-table'.")
3486 (defvar erlang-tags-orig-tag-order nil
3487 "Temporary storage for `find-tag-tag-order'.")
3488 (defvar erlang-tags-orig-regexp-tag-order nil
3489 "Temporary storage for `find-tag-regexp-tag-order'.")
3490 (defvar erlang-tags-orig-search-function nil
3491 "Temporary storage for `find-tag-search-function'.")
3492 (defvar erlang-tags-orig-regexp-search-function nil
3493 "Temporary storage for `find-tag-regexp-search-function'.")
3494 (defvar erlang-tags-orig-format-hooks nil
3495 "Temporary storage for `tags-table-format-hooks'.")
3497 (defun erlang-tags-init ()
3498 "Install an alternate version of tags, aware of Erlang modules.
3500 After calling this function, the tags functions are aware of
3501 Erlang modules. Tags can be entered on the for `module:tag' as well
3502 as on the old form `tag'.
3504 In the completion list, `module:tag' and `module:' shows up.
3506 Call this function from an appropriate init file, or add it to
3507 Erlang mode hook with the commands:
3508 (add-hook 'erlang-mode-hook 'erlang-tags-init)
3509 (add-hook 'erlang-shell-mode-hook 'erlang-tags-init)
3511 This function only works under Emacs 18 and Emacs 19. Currently, It
3512 is not implemented under XEmacs. (Hint: The Emacs 19 etags module
3513 works under XEmacs.)"
3515 (cond ((= erlang-emacs-major-version 18)
3517 (erlang-tags-define-keys (current-local-map))
3518 (setq erlang-tags-installed t))
3521 ;; Test on a function available in the Emacs 19 version
3522 ;; of tags but not in the XEmacs version.
3523 (if (not (fboundp 'find-tag-noselect))
3525 (erlang-tags-define-keys (current-local-map))
3526 (setq erlang-tags-installed t)))))
3529 ;; Set all keys bound to `find-tag' et.al. in the global map and the
3530 ;; menu to `erlang-find-tag' et.al. in `map'.
3532 ;; The function `substitute-key-definition' does not work properly
3533 ;; in all version of Emacs.
3535 (defun erlang-tags-define-keys (map)
3536 "Bind tags commands to keymap MAP aware of Erlang modules."
3537 (let ((alist erlang-tags-function-alist))
3539 (let* ((old (car (car alist)))
3540 (new (cdr (car alist)))
3541 (keys (append (where-is-internal old global-map))))
3543 (define-key map (car keys) new)
3544 (setq keys (cdr keys))))
3545 (setq alist (cdr alist))))
3547 (erlang-menu-substitute erlang-menu-base-items erlang-tags-function-alist)
3551 ;; There exists a variable `find-tag-default-function'. It is not used
3552 ;; since `complete-tag' uses it to get current word under point. In that
3553 ;; situation we don't want the module to be prepended.
3555 (defun erlang-find-tag-default ()
3556 "Return the default tag.
3557 Search `-import' list of imported functions.
3558 Single quotes are been stripped away."
3559 (let ((mod-func (erlang-get-function-under-point)))
3560 (cond ((null mod-func)
3562 ((null (car mod-func))
3565 (concat (car mod-func) ":" (nth 1 mod-func))))))
3568 ;; Return `t' since it is used inside `tags-loop-form'.
3570 (defun erlang-find-tag (modtagname &optional next-p regexp-p)
3571 "Like `find-tag'. Capable of retrieving Erlang modules.
3573 Tags can be given on the forms `tag', `module:', `module:tag'."
3574 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3575 (switch-to-buffer (erlang-find-tag-noselect modtagname next-p regexp-p))
3579 ;; Code mainly from `find-tag-other-window' in `etags.el'.
3581 (defun erlang-find-tag-other-window (tagname &optional next-p regexp-p)
3582 "Like `find-tag-other-window' but aware of Erlang modules."
3583 (interactive (erlang-tag-interactive
3584 "Find `module:tag' or `tag' other window: "))
3586 ;; This is to deal with the case where the tag is found in the
3587 ;; selected window's buffer; without this, point is moved in both
3588 ;; windows. To prevent this, we save the selected window's point
3589 ;; before doing find-tag-noselect, and restore it afterwards.
3590 (let* ((window-point (window-point (selected-window)))
3591 (tagbuf (erlang-find-tag-noselect tagname next-p regexp-p))
3592 (tagpoint (progn (set-buffer tagbuf) (point))))
3593 (set-window-point (prog1
3595 (switch-to-buffer-other-window tagbuf)
3596 ;; We have to set this new window's point; it
3597 ;; might already have been displaying a
3598 ;; different portion of tagbuf, in which case
3599 ;; switch-to-buffer-other-window doesn't set
3600 ;; the window's point from the buffer.
3601 (set-window-point (selected-window) tagpoint))
3605 (defun erlang-find-tag-other-frame (tagname &optional next-p)
3606 "Like `find-tag-other-frame' but aware of Erlang modules."
3607 (interactive (erlang-tag-interactive
3608 "Find `module:tag' or `tag' other frame: "))
3609 (let ((pop-up-frames t))
3610 (erlang-find-tag-other-window tagname next-p)))
3613 (defun erlang-find-tag-regexp (regexp &optional next-p other-window)
3614 "Like `find-tag-regexp' but aware of Erlang modules."
3615 (interactive (if (fboundp 'find-tag-regexp)
3616 (erlang-tag-interactive
3617 "Find `module:regexp' or `regexp': ")
3618 (error "This version of Emacs can't find tags by regexps")))
3619 (funcall (if other-window
3620 'erlang-find-tag-other-window
3625 ;; Just like C-u M-. This could be added to the menu.
3626 (defun erlang-find-next-tag ()
3627 "Find next tag, like \\[find-tag] with prefix arg."
3629 (let ((current-prefix-arg '(4)))
3630 (if erlang-tags-installed
3631 (call-interactively 'erlang-find-tag)
3632 (call-interactively 'find-tag))))
3635 ;; Mimics `find-tag-noselect' found in `etags.el', but uses `find-tag' to
3636 ;; be compatible with `tags.el'.
3638 ;; Handles three cases:
3639 ;; * `module:' Loop over all possible file names. Stop if a file-name
3640 ;; without extension and directory matches the module.
3643 ;; Emacs 19: Replace test functions with functions aware of
3644 ;; Erlang modules. Tricky because the etags system wasn't
3645 ;; built for these kind of operations...
3647 ;; Emacs 18: We loop over `find-tag' until we find a file
3648 ;; whose module matches the requested module. The
3649 ;; drawback is that a lot of files could be loaded into
3652 ;; * `tag' Just give it to `find-tag'.
3654 (defun erlang-find-tag-noselect (modtagname &optional next-p regexp-p)
3655 "Like `find-tag-noselect' but aware of Erlang modules."
3656 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3658 (setq modtagname (symbol-value 'last-tag)))
3659 (funcall (symbol-function 'set) 'last-tag modtagname)
3660 ;; `tags.el' uses this variable to record how M-, would
3661 ;; know where to restart a tags command.
3662 (if (boundp 'tags-loop-form)
3663 (funcall (symbol-function 'set)
3664 'tags-loop-form '(erlang-find-tag nil t)))
3665 (save-window-excursion
3667 ((string-match ":$" modtagname)
3668 ;; Only the module name was given. Read all files whose file name
3670 (let ((modname (substring modtagname 0 (match-beginning 0)))
3674 (visit-tags-table-buffer)
3675 (setq erlang-tags-file-list
3676 (funcall (symbol-function 'tags-table-files)))))
3678 (or erlang-tags-file-list
3680 (if (and (featurep 'etags)
3682 (symbol-function 'visit-tags-table-buffer) 'same)
3684 (symbol-function 'visit-tags-table-buffer) t))
3685 (setq erlang-tags-file-list
3686 (funcall (symbol-function 'tags-table-files)))
3687 (error "No %stags containing %s" (if next-p "more " "")
3689 (if erlang-tags-file-list
3690 (let ((this-module (erlang-get-module-from-file-name
3691 (car erlang-tags-file-list))))
3692 (if (and (stringp this-module)
3693 (string= modname this-module))
3694 (setq file (car erlang-tags-file-list)))
3695 (setq erlang-tags-file-list (cdr erlang-tags-file-list)))))
3696 (set-buffer (or (get-file-buffer file)
3697 (find-file-noselect file)))))
3699 ((string-match ":" modtagname)
3700 (if (boundp 'find-tag-tag-order)
3701 ;; Method one: Add module-recognising functions to the
3702 ;; list of order functions. However, the tags system
3703 ;; from Emacs 18, and derives thereof (read: XEmacs)
3704 ;; hasn't got this feature.
3706 (erlang-tags-install-module-check)
3708 (funcall (symbol-function 'find-tag)
3709 modtagname next-p regexp-p)
3710 (erlang-tags-remove-module-check)))
3711 ;; Method two: Call the tags system until a file matching
3712 ;; the module is found. This could result in that many
3713 ;; files are read. (e.g. The tag "foo:file" will take a
3714 ;; while to process.)
3715 (let* ((modname (substring modtagname 0 (match-beginning 0)))
3716 (tagname (substring modtagname (match-end 0) nil))
3721 (funcall (symbol-function 'find-tag) tagname next-p regexp-p)
3723 ;; Determine the module form the file name. (The
3724 ;; alternative, to check `-module', would make this
3725 ;; code useless for non-Erlang programs.)
3726 (setq file (erlang-get-module-from-file-name buffer-file-name))
3727 (not (and (stringp file)
3728 (string= modname file))))))))
3730 (funcall (symbol-function 'find-tag) modtagname next-p regexp-p)))
3731 (current-buffer))) ; Return the new buffer.
3734 ;; Process interactive arguments for erlang-find-tag-*.
3736 ;; Negative arguments work only for `etags', not `tags'. This is not
3737 ;; a problem since negative arguments means step back into the
3738 ;; history list, a feature not implemented in `tags'.
3740 (defun erlang-tag-interactive (prompt)
3745 (if current-prefix-arg
3746 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
3749 (let* ((default (erlang-find-tag-default))
3751 (format "%s(default %s) " prompt default)
3753 (spec (if (featurep 'etags)
3754 (completing-read prompt 'erlang-tags-complete-tag)
3755 (read-string prompt))))
3756 (list (if (equal spec "")
3757 (or default (error "There is no default tag"))
3761 ;; Search tag functions which are aware of Erlang modules. The tactic
3762 ;; is to store new search functions into the local variables of the
3763 ;; TAGS buffers. The variables are restored directly after the
3764 ;; search. The situation is complicated by the fact that new TAGS
3765 ;; files can be loaded during the search.
3767 ;; This code is Emacs 19 `etags' specific.
3769 (defun erlang-tags-install-module-check ()
3770 "Install our own tag search functions."
3771 ;; Make sure our functions are installed in TAGS files loaded
3772 ;; into Emacs while searching.
3773 ;; ?? tags-table-format-hooks isn't in Emacs 21 or XEmacs etags.
3774 (setq erlang-tags-orig-format-hooks
3775 (symbol-value 'tags-table-format-hooks))
3776 (funcall (symbol-function 'set) 'tags-table-format-hooks
3777 (cons 'erlang-tags-recognize-tags-table
3778 erlang-tags-orig-format-hooks))
3779 (setq erlang-tags-buffer-list '())
3780 ;; Install our functions in the TAGS files already resident.
3782 (let ((files (symbol-value 'tags-table-computed-list)))
3784 (if (stringp (car files))
3785 (if (get-file-buffer (car files))
3787 (set-buffer (get-file-buffer (car files)))
3788 (erlang-tags-install-local))))
3789 (setq files (cdr files))))))
3792 (defun erlang-tags-install-local ()
3793 "Install our tag search functions in current buffer."
3794 (if erlang-tags-buffer-installed-p
3796 ;; Mark this buffer as "installed" and record.
3797 (set (make-local-variable 'erlang-tags-buffer-installed-p) t)
3798 (setq erlang-tags-buffer-list
3799 (cons (current-buffer) erlang-tags-buffer-list))
3801 ;; Save the original values.
3802 (set (make-local-variable 'erlang-tags-orig-tag-order)
3803 (symbol-value 'find-tag-tag-order))
3804 (set (make-local-variable 'erlang-tags-orig-regexp-tag-order)
3805 (symbol-value 'find-tag-regexp-tag-order))
3806 (set (make-local-variable 'erlang-tags-orig-search-function)
3807 (symbol-value 'find-tag-search-function))
3808 (set (make-local-variable 'erlang-tags-orig-regexp-search-function)
3809 (symbol-value 'find-tag-regexp-search-function))
3811 ;; Install our own functions.
3812 (set (make-local-variable 'find-tag-search-function)
3813 'erlang-tags-search-forward)
3814 (set (make-local-variable 'find-tag-regexp-search-function)
3815 'erlang-tags-regexp-search-forward)
3816 (set (make-local-variable 'find-tag-tag-order)
3817 '(erlang-tag-match-module-p))
3818 (set (make-local-variable 'find-tag-regexp-tag-order)
3819 '(erlang-tag-match-module-regexp-p))))
3822 (defun erlang-tags-remove-module-check ()
3823 "Remove our own tags search functions."
3824 (funcall (symbol-function 'set)
3825 'tags-table-format-hooks
3826 erlang-tags-orig-format-hooks)
3827 ;; Remove our functions from the TAGS files. (Note that
3828 ;; `tags-table-computed-list' need not be the same list as when
3829 ;; the search was started.)
3831 (let ((buffers erlang-tags-buffer-list))
3833 (if (buffer-name (car buffers))
3835 (set-buffer (car buffers))
3836 (erlang-tags-remove-local)))
3837 (setq buffers (cdr buffers))))))
3840 (defun erlang-tags-remove-local ()
3841 "Remove our tag search functions from current buffer."
3842 (if (null erlang-tags-buffer-installed-p)
3844 (funcall (symbol-function 'set) 'erlang-tags-buffer-installed-p nil)
3845 (funcall (symbol-function 'set)
3846 'find-tag-tag-order erlang-tags-orig-tag-order)
3847 (funcall (symbol-function 'set)
3848 'find-tag-regexp-tag-order erlang-tags-orig-regexp-tag-order)
3849 (funcall (symbol-function 'set)
3850 'find-tag-search-function erlang-tags-orig-search-function)
3851 (funcall (symbol-function 'set)
3852 'find-tag-regexp-search-function
3853 erlang-tags-orig-regexp-search-function)))
3856 (defun erlang-tags-recognize-tags-table ()
3857 "Install our functions in all loaded TAGS files.
3859 This function is added to `tags-table-format-hooks' when searching
3860 for a tag on the form `module:tag'."
3861 (if (null (funcall (symbol-function 'etags-recognize-tags-table)))
3863 (erlang-tags-install-local)
3867 (defun erlang-tags-search-forward (tag &optional bound noerror count)
3868 "Forward search function, aware of Erlang module prefix."
3869 (if (string-match ":" tag)
3870 (setq tag (substring tag (match-end 0) nil)))
3871 ;; Avoid unintended recursion.
3872 (if (eq erlang-tags-orig-search-function 'erlang-tags-search-forward)
3873 (search-forward tag bound noerror count)
3874 (funcall erlang-tags-orig-search-function tag bound noerror count)))
3877 (defun erlang-tags-regexp-search-forward (tag &optional bound noerror count)
3878 "Forward regexp search function, aware of Erlang module prefix."
3879 (if (string-match ":" tag)
3880 (setq tag (substring tag (match-end 0) nil)))
3881 (if (eq erlang-tags-orig-regexp-search-function
3882 'erlang-tags-regexp-search-forward)
3883 (re-search-forward tag bound noerror count)
3884 (funcall erlang-tags-orig-regexp-search-function
3885 tag bound noerror count)))
3888 ;; t if point is at a tag line that matches TAG, containing
3889 ;; module information. Assumes that all other order functions
3890 ;; are stored in `erlang-tags-orig-[regex]-tag-order'.
3892 (defun erlang-tag-match-module-p (tag)
3893 (erlang-tag-match-module-common-p tag erlang-tags-orig-tag-order))
3895 (defun erlang-tag-match-module-regexp-p (tag)
3896 (erlang-tag-match-module-common-p tag erlang-tags-orig-regexp-tag-order))
3898 (defun erlang-tag-match-module-common-p (tag order)
3901 (if (string-match ":" tag)
3903 (setq mod (substring tag 0 (match-beginning 0)))
3904 (setq tag (substring tag (match-end 0) nil))))
3905 (while (and order (not found))
3907 (and (not (memq (car order)
3908 '(erlang-tag-match-module-p
3909 erlang-tag-match-module-regexp-p)))
3910 (funcall (car order) tag)))
3911 (setq order (cdr order)))
3914 (string= mod (erlang-get-module-from-file-name
3918 ;;; Tags completion, Emacs 19 `etags' specific.
3920 ;;; The basic idea is to create a second completion table `erlang-tags-
3921 ;;; completion-table' containing all normal tags plus tags on the form
3925 (defun erlang-complete-tag ()
3926 "Perform tags completion on the text around point.
3927 Completes to the set of names listed in the current tags table.
3929 Should the Erlang tags system be installed this command knows
3930 about Erlang modules."
3935 (cond ((and erlang-tags-installed
3936 (fboundp 'complete-tag)) ; Emacs 19
3937 (let ((orig-tags-complete-tag (symbol-function 'tags-complete-tag)))
3938 (fset 'tags-complete-tag
3939 (symbol-function 'erlang-tags-complete-tag))
3941 (funcall (symbol-function 'complete-tag))
3942 (fset 'tags-complete-tag orig-tags-complete-tag))))
3943 ((fboundp 'complete-tag) ; Emacs 19
3944 (funcall (symbol-function 'complete-tag)))
3945 ((fboundp 'tag-complete-symbol) ; XEmacs
3946 (funcall (symbol-function 'tag-complete-symbol)))
3948 (error "This version of Emacs can't complete tags"))))
3951 ;; Based on `tags-complete-tag', but this one uses
3952 ;; `erlang-tags-completion-table' instead of `tags-completion-table'.
3954 ;; This is the entry-point called by system function `completing-read'.
3955 (defun erlang-tags-complete-tag (string predicate what)
3957 ;; If we need to ask for the tag table, allow that.
3958 (let ((enable-recursive-minibuffers t))
3959 (visit-tags-table-buffer))
3961 (all-completions string (erlang-tags-completion-table) predicate)
3962 (try-completion string (erlang-tags-completion-table) predicate))))
3965 ;; `tags-completion-table' calls itself recursively, make it
3966 ;; call our own wedge instead. Note that the recursive call
3967 ;; is very rare; it only occurs when a tags-file contains
3968 ;; `include'-statements.
3969 (defun erlang-tags-completion-table ()
3970 "Build completion table. Tags on the form `tag' or `module:tag'."
3971 (setq erlang-tags-orig-completion-table
3972 (symbol-function 'tags-completion-table))
3973 (fset 'tags-completion-table
3974 (symbol-function 'erlang-tags-completion-table-1))
3976 (erlang-tags-completion-table-1)
3977 (fset 'tags-completion-table
3978 erlang-tags-orig-completion-table)))
3981 (defun erlang-tags-completion-table-1 ()
3982 (make-local-variable 'erlang-tags-completion-table)
3983 (or erlang-tags-completion-table
3984 (let ((tags-completion-table nil)
3985 (tags-completion-table-function
3986 'erlang-etags-tags-completion-table))
3987 (funcall erlang-tags-orig-completion-table)
3988 (setq erlang-tags-completion-table tags-completion-table))))
3991 ;; Based on `etags-tags-completion-table'. The difference is that we
3992 ;; add three symbols to the vector, the tag, module: and module:tag.
3993 ;; The module is extracted from the file name of a tag. (This one
3994 ;; only works if we are looking at an `etags' file. However, this is
3995 ;; the only format supported by Emacs, so far.)
3996 (defun erlang-etags-tags-completion-table ()
3997 (let ((table (make-vector 511 0))
4000 (goto-char (point-min))
4001 ;; This monster regexp matches an etags tag line.
4002 ;; \1 is the string to match;
4003 ;; \2 is not interesting;
4004 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
4005 ;; \4 is not interesting;
4006 ;; \5 is the explicitly-specified tag name.
4007 ;; \6 is the line to start searching at;
4008 ;; \7 is the char to start searching at.
4011 (eq (following-char) ?\f)
4012 (looking-at "\f\n\\([^,\n]*\\),.*\n"))
4013 (setq file (buffer-substring
4014 (match-beginning 1) (match-end 1)))
4015 (goto-char (match-end 0)))
4018 ^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
4019 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
4020 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
4022 (let ((tag (if (match-beginning 5)
4023 ;; There is an explicit tag name.
4024 (buffer-substring (match-beginning 5) (match-end 5))
4025 ;; No explicit tag name. Best guess.
4026 (buffer-substring (match-beginning 3) (match-end 3))))
4028 (erlang-get-module-from-file-name file))))
4030 (if (stringp module)
4032 (intern (concat module ":" tag) table)
4033 ;; Only the first one will be stored in the table.
4034 (intern (concat module ":") table))))))
4038 ;;; Prepare for other methods to run an Erlang slave process.
4041 (defvar erlang-shell-function 'inferior-erlang
4042 "Command to execute start a new Erlang shell.
4044 Change this variable to use your favorite
4045 Erlang compilation package.")
4047 (defvar erlang-shell-display-function 'inferior-erlang-run-or-select
4048 "Command to execute to display Erlang shell.
4050 Change this variable to use your favorite
4051 Erlang compilation package.")
4053 (defvar erlang-compile-function 'inferior-erlang-compile
4054 "Command to execute to compile current buffer.
4056 Change this variable to use your favorite
4057 Erlang compilation package.")
4059 (defvar erlang-compile-display-function 'inferior-erlang-run-or-select
4060 "Command to execute to view last compilation.
4062 Change this variable to use your favorite
4063 Erlang compilation package.")
4065 (defvar erlang-next-error-function 'inferior-erlang-next-error
4066 "Command to execute to go to the next error.
4068 Change this variable to use your favorite Erlang compilation
4069 package. Not used in Emacs 21.")
4073 (defun erlang-shell ()
4074 "Start a new Erlang shell.
4076 The variable `erlang-shell-function' decides which method to use,
4077 default is to start a new Erlang host. It is possible that, in the
4078 future, a new shell on an already running host will be started."
4080 (call-interactively erlang-shell-function))
4083 ;;;###autoload (autoload 'run-erlang "erlang" "Start a new Erlang shell." t)
4085 ;; It is customary for Emacs packages to supply a function on this
4086 ;; form, even though it violates the `erlang-*' name convention.
4087 (defalias 'run-erlang 'erlang-shell)
4090 (defun erlang-shell-display ()
4091 "Display an Erlang shell, or start a new."
4093 (call-interactively erlang-shell-display-function))
4097 (defun erlang-compile ()
4098 "Compile Erlang module in current buffer."
4100 (call-interactively erlang-compile-function))
4103 (defun erlang-compile-display ()
4104 "Display compilation output."
4106 (call-interactively erlang-compile-display-function))
4109 (defun erlang-next-error ()
4110 "Display next error message from the latest compilation."
4112 (call-interactively erlang-next-error-function))
4117 ;;; Erlang Shell Mode -- Major mode used for Erlang shells.
4120 ;; This mode is designed to be implementation independent,
4121 ;; e.g. it does not assume that we are running an inferior
4122 ;; Erlang, there exists a lot of other possibilities.
4125 (defvar erlang-shell-buffer-name "*erlang*"
4126 "The name of the Erlang link shell buffer.")
4129 (defvar erlang-shell-mode-map nil
4130 "Keymap used by Erlang shells.")
4133 (defvar erlang-shell-mode-hook nil
4134 "*User functions to run when an Erlang shell is started.
4136 This hook is used to change the behaviour of Erlang mode. It is
4137 normally used by the user to personalise the programming environment.
4138 When used in a site init file, it could be used to customise Erlang
4139 mode for all users on the system.
4141 The function added to this hook is run every time a new Erlang
4144 See also `erlang-load-hook', a hook which is run once, when Erlang
4145 mode is loaded, and `erlang-mode-hook' which is run every time a new
4146 Erlang source file is loaded into Emacs.")
4149 (defvar erlang-input-ring-file-name "~/.erlang_history"
4150 "*When non-nil, file name used to store Erlang shell history information.")
4153 (defun erlang-shell-mode ()
4154 "Major mode for interacting with an Erlang shell.
4156 We assume that we already are in Comint mode.
4158 The following special commands are available:
4159 \\{erlang-shell-mode-map}"
4161 (setq major-mode 'erlang-shell-mode)
4162 (setq mode-name "Erlang Shell")
4163 (erlang-mode-variables)
4164 (if erlang-shell-mode-map
4166 (setq erlang-shell-mode-map (copy-keymap comint-mode-map))
4167 (erlang-shell-mode-commands erlang-shell-mode-map))
4168 (use-local-map erlang-shell-mode-map)
4169 (unless inferior-erlang-use-cmm
4170 ;; This was originally not a marker, but it needs to be, at least
4171 ;; in Emacs 21, and should be backwards-compatible. Otherwise,
4172 ;; would need to test whether compilation-parsing-end is a marker
4173 ;; after requiring `compile'.
4174 (set (make-local-variable 'compilation-parsing-end) (copy-marker 1))
4175 (set (make-local-variable 'compilation-error-list) nil)
4176 (set (make-local-variable 'compilation-old-error-list) nil))
4177 ;; Needed when compiling directly from the Erlang shell.
4178 (setq compilation-last-buffer (current-buffer))
4179 (erlang-add-compilation-alist erlang-error-regexp-alist)
4180 (setq comint-prompt-regexp "^[^>=]*> *")
4181 (setq comint-eol-on-send t)
4182 (setq comint-input-ignoredups t)
4183 (setq comint-scroll-show-maximum-output t)
4184 (setq comint-scroll-to-bottom-on-output t)
4185 ;; In Emacs 19.30, `add-hook' has got a `local' flag, use it. If
4186 ;; the call fails, just call the normal `add-hook'.
4189 (make-local-hook 'comint-output-filter-functions) ; obsolete after Emacs 21.3
4190 (add-hook 'comint-output-filter-functions
4191 'inferior-erlang-strip-delete nil t)
4192 (add-hook 'comint-output-filter-functions
4193 'inferior-erlang-strip-ctrl-m nil t))
4195 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-delete)
4196 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-ctrl-m)))
4197 ;; Some older versions of comint don't have an input ring.
4198 (if (fboundp 'comint-read-input-ring)
4200 (setq comint-input-ring-file-name erlang-input-ring-file-name)
4201 (comint-read-input-ring t)
4202 (make-local-variable 'kill-buffer-hook)
4203 (add-hook 'kill-buffer-hook 'comint-write-input-ring)))
4204 ;; At least in Emacs 21, we need to be in `compilation-minor-mode'
4205 ;; for `next-error' to work. We can avoid it clobbering the shell
4207 (when inferior-erlang-use-cmm
4208 (compilation-minor-mode 1)
4209 (set (make-local-variable 'minor-mode-overriding-map-alist)
4210 `((compilation-minor-mode
4211 . ,(let ((map (make-sparse-keymap)))
4212 ;; It would be useful to put keymap properties on the
4213 ;; error lines so that we could use RET and mouse-2
4214 ;; on them directly.
4215 (when (boundp 'compilation-skip-threshold) ; new compile.el
4216 (define-key map [mouse-2] #'erlang-mouse-2-command)
4217 (define-key map "\C-m" #'erlang-RET-command))
4218 (if (boundp 'compilation-menu-map)
4219 (define-key map [menu-bar compilation]
4220 (cons "Errors" compilation-menu-map)))
4222 (run-hooks 'erlang-shell-mode-hook))
4225 (defun erlang-mouse-2-command (event)
4226 "Command bound to `mouse-2' in inferior Erlang buffer.
4227 Selects Comint or Compilation mode command as appropriate."
4229 (if (save-window-excursion
4231 (mouse-set-point event)
4232 (consp (get-text-property (line-beginning-position) 'message))))
4233 (call-interactively (lookup-key compilation-mode-map [mouse-2]))
4234 (call-interactively (lookup-key comint-mode-map [mouse-2]))))
4236 (defun erlang-RET-command ()
4237 "Command bound to `RET' in inferior Erlang buffer.
4238 Selects Comint or Compilation mode command as appropriate."
4240 (if (consp (get-text-property (line-beginning-position) 'message))
4241 (call-interactively (lookup-key compilation-mode-map "\C-m"))
4242 (call-interactively (lookup-key comint-mode-map "\C-m"))))
4244 (defun erlang-shell-mode-commands (map)
4245 (define-key map "\M-\t" 'erlang-complete-tag)
4246 (define-key map "\C-a" 'comint-bol) ; Normally the other way around.
4247 (define-key map "\C-c\C-a" 'beginning-of-line)
4248 (define-key map "\C-d" nil) ; Was `comint-delchar-or-maybe-eof'
4249 (define-key map "\M-\C-m" 'compile-goto-error)
4250 (unless inferior-erlang-use-cmm
4251 (define-key map "\C-x`" 'erlang-next-error)))
4254 ;;; Inferior Erlang -- Run an Erlang shell as a subprocess.
4257 (defvar inferior-erlang-display-buffer-any-frame nil
4258 "*When nil, `inferior-erlang-display-buffer' use only selected frame.
4259 When t, all frames are searched. When 'raise, the frame is raised.")
4261 (defvar inferior-erlang-shell-type 'newshell
4262 "The type of Erlang shell to use.
4264 When this variable is set to the atom `oldshell', the old shell is used.
4265 When set to `newshell' the new shell is used. Should the variable be
4266 nil, the default shell is used.
4268 This variable influence the setting of other variables.")
4270 (defvar inferior-erlang-machine "erl"
4271 "*The name of the Erlang shell.")
4273 (defvar inferior-erlang-machine-options '()
4274 "*The options used when activating the Erlang shell.
4276 This must be a list of strings.")
4278 (defvar inferior-erlang-process-name "inferior-erlang"
4279 "The name of the inferior Erlang process.")
4281 (defvar inferior-erlang-buffer-name erlang-shell-buffer-name
4282 "The name of the inferior Erlang buffer.")
4284 (defvar inferior-erlang-prompt-timeout 60
4285 "*Number of seconds before `inferior-erlang-wait-prompt' timeouts.
4287 The time specified is waited after every output made by the inferior
4288 Erlang shell. When this variable is t, we assume that we always have
4289 a prompt. When nil, we will wait forever, or until \\[keyboard-quit].")
4291 (defvar inferior-erlang-process nil
4292 "Process of last invoked inferior Erlang, or nil.")
4294 (defvar inferior-erlang-buffer nil
4295 "Buffer of last invoked inferior Erlang, or nil.")
4298 (defun inferior-erlang ()
4299 "Run an inferior Erlang.
4301 This is just like running Erlang in a normal shell, except that
4302 an Emacs buffer is used for input and output.
4304 The command line history can be accessed with \\[comint-previous-input] and \\[comint-next-input].
4305 The history is saved between sessions.
4307 Entry to this mode calls the functions in the variables
4308 `comint-mode-hook' and `erlang-shell-mode-hook' with no arguments.
4310 The following commands imitate the usual Unix interrupt and
4311 editing control characters:
4312 \\{erlang-shell-mode-map}"
4315 (let ((opts inferior-erlang-machine-options))
4316 (cond ((eq inferior-erlang-shell-type 'oldshell)
4317 (setq opts (cons "-oldshell" opts)))
4318 ((eq inferior-erlang-shell-type 'newshell)
4319 (setq opts (append '("-newshell" "-env" "TERM" "vt100") opts))))
4320 (setq inferior-erlang-buffer
4322 inferior-erlang-process-name inferior-erlang-machine
4324 (setq inferior-erlang-process
4325 (get-buffer-process inferior-erlang-buffer))
4326 (process-kill-without-query inferior-erlang-process)
4327 (switch-to-buffer inferior-erlang-buffer)
4328 (if (and (not (eq system-type 'windows-nt))
4329 (eq inferior-erlang-shell-type 'newshell))
4330 (setq comint-process-echoes t))
4331 ;; `rename-buffer' takes only one argument in Emacs 18.
4333 (rename-buffer inferior-erlang-buffer-name t)
4334 (error (rename-buffer inferior-erlang-buffer-name)))
4335 (erlang-shell-mode))
4338 (defun inferior-erlang-run-or-select ()
4339 "Switch to an inferior Erlang buffer, possibly starting new process."
4341 (if (null (inferior-erlang-running-p))
4343 (inferior-erlang-display-buffer t)))
4346 (defun inferior-erlang-display-buffer (&optional select)
4347 "Make the inferior Erlang process visible.
4348 The window is returned.
4350 Should `inferior-erlang-display-buffer-any-frame' be nil the buffer is
4351 displayed in the current frame. Should it be non-nil, and the buffer
4352 already is visible in any other frame, no new window will be created.
4353 Should it be the atom 'raise, the frame containing the window will
4356 Should the optional argument SELECT be non-nil, the window is
4357 selected. Should the window be in another frame, that frame is raised.
4359 Note, should the mouse pointer be places outside the raised frame, that
4360 frame will become deselected before the next command."
4362 (or (inferior-erlang-running-p)
4363 (error "No inferior Erlang process is running"))
4364 (let ((win (inferior-erlang-window
4365 inferior-erlang-display-buffer-any-frame))
4366 (frames-p (fboundp 'selected-frame)))
4368 (let ((old-win (selected-window)))
4370 (switch-to-buffer-other-window inferior-erlang-buffer)
4371 (setq win (selected-window)))
4372 (select-window old-win))
4373 (if (and window-system
4376 (eq inferior-erlang-display-buffer-any-frame 'raise))
4377 (not (eq (selected-frame) (window-frame win))))
4378 (raise-frame (window-frame win))))
4380 (select-window win))
4385 (defun inferior-erlang-running-p ()
4386 "Non-nil when an inferior Erlang is running."
4387 (and inferior-erlang-process
4388 (memq (process-status inferior-erlang-process) '(run open))
4389 inferior-erlang-buffer
4390 (buffer-name inferior-erlang-buffer)))
4393 (defun inferior-erlang-window (&optional all-frames)
4394 "Return the window containing the inferior Erlang, or nil."
4395 (and (inferior-erlang-running-p)
4396 (if (and all-frames (>= erlang-emacs-major-version 19))
4397 (get-buffer-window inferior-erlang-buffer t)
4398 (get-buffer-window inferior-erlang-buffer))))
4401 (defun inferior-erlang-wait-prompt ()
4402 "Wait until the inferior Erlang shell prompt appears."
4403 (if (eq inferior-erlang-prompt-timeout t)
4405 (or (inferior-erlang-running-p)
4406 (error "No inferior Erlang shell is running"))
4408 (set-buffer inferior-erlang-buffer)
4410 (while (save-excursion
4411 (goto-char (process-mark inferior-erlang-process))
4413 (not (looking-at comint-prompt-regexp)))
4417 (message "Waiting for Erlang shell prompt (press C-g to abort)."))
4418 (or (accept-process-output inferior-erlang-process
4419 inferior-erlang-prompt-timeout)
4420 (error "No Erlang shell prompt before timeout")))
4421 (if msg (message ""))))))
4423 (autoload 'comint-send-input "comint")
4425 (defun inferior-erlang-send-command (cmd &optional hist)
4426 "Send command CMD to the inferior Erlang.
4428 The contents of the current command line (if any) will
4429 be placed at the next prompt.
4431 If optional second argument is non-nil the command is inserted into
4434 Return the position after the newly inserted command."
4435 (or (inferior-erlang-running-p)
4436 (error "No inferior Erlang process is running"))
4437 (let ((old-buffer (current-buffer))
4438 (insert-point (marker-position (process-mark inferior-erlang-process)))
4439 (insert-length (if comint-process-echoes
4441 (1+ (length cmd)))))
4442 (set-buffer inferior-erlang-buffer)
4443 (goto-char insert-point)
4445 ;; Strange things happened if `comint-eol-on-send' is declared
4446 ;; in the `let' expression above, but setq:d here. The
4447 ;; `set-buffer' statement obviously makes the buffer local
4448 ;; instance of `comint-eol-on-send' shadow this one.
4449 ;; I'm considering this a bug in Elisp.
4451 ;; This was previously cautioned against in the Lisp manual. It
4452 ;; has been sorted out in Emacs 21. -- fx
4453 (let ((comint-eol-on-send nil)
4454 (comint-input-filter (if hist comint-input-filter 'ignore)))
4455 (comint-send-input))
4456 ;; Adjust all windows whose points are incorrect.
4457 (if (null comint-process-echoes)
4461 (if (and (eq (window-buffer window) inferior-erlang-buffer)
4462 (= (window-point window) insert-point))
4463 (set-window-point window
4464 (+ insert-point insert-length)))))
4466 (set-buffer old-buffer)
4467 (+ insert-point insert-length)))
4470 (defun inferior-erlang-strip-delete (&optional s)
4471 "Remove `^H' (delete) and the characters it was supposed to remove."
4473 (if (and (boundp 'comint-last-input-end)
4474 (boundp 'comint-last-output-start))
4478 (symbol-value 'comint-last-input-end)
4479 (symbol-value 'comint-last-output-start)))
4480 (while (progn (skip-chars-forward "^\C-h")
4481 (not (eq (point) (point-max))))
4484 (backward-delete-char 1))))))
4487 ;; Basically `comint-strip-ctrl-m', with a few extra checks.
4488 (defun inferior-erlang-strip-ctrl-m (&optional string)
4489 "Strip trailing `^M' characters from the current output group."
4491 (if (and (boundp 'comint-last-input-end)
4492 (boundp 'comint-last-output-start))
4493 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
4497 (symbol-value 'comint-last-input-end)
4498 (symbol-value 'comint-last-output-start)))
4499 (while (re-search-forward "\r+$" pmark t)
4500 (replace-match "" t t))))))
4503 (defun inferior-erlang-compile ()
4504 "Compile the file in the current buffer.
4506 Should Erlang return `{error, nofile}' it could not load the object
4507 module after completing the compilation. This is due to a bug in the
4508 compile command `c' when using the option `outdir'.
4510 There exists two workarounds for this bug:
4512 1) Place the directory in the Erlang load path.
4514 2) Set the Emacs variable `erlang-compile-use-outdir' to nil.
4515 To do so, place the following line in your `~/.emacs'-file:
4516 (setq erlang-compile-use-outdir nil)"
4519 (or (inferior-erlang-running-p)
4522 (or (inferior-erlang-running-p)
4523 (error "Error starting inferior Erlang shell"))
4524 (let ((dir (file-name-directory (buffer-file-name)))
4525 ;;; (file (file-name-nondirectory (buffer-file-name)))
4526 (noext (substring (buffer-file-name) 0 -4))
4527 ;; Hopefully, noone else will ever use these...
4531 (inferior-erlang-display-buffer)
4532 (inferior-erlang-wait-prompt)
4533 (setq end (inferior-erlang-send-command
4534 (if erlang-compile-use-outdir
4535 (format "c(\"%s\", [{outdir, \"%s\"}])." noext dir)
4538 "f(%s), {ok, %s} = file:get_cwd(), "
4539 "file:set_cwd(\"%s\"), "
4540 "%s = c(\"%s\"), file:set_cwd(%s), f(%s), %s.")
4543 tmpvar2 noext tmpvar tmpvar tmpvar2))
4545 (inferior-erlang-wait-prompt)
4547 (set-buffer inferior-erlang-buffer)
4548 (setq compilation-error-list nil)
4549 (set-marker compilation-parsing-end end))
4550 (setq compilation-last-buffer inferior-erlang-buffer)))
4553 ;; `next-error' only accepts buffers with major mode `compilation-mode'
4554 ;; or with the minor mode `compilation-minor-mode' activated.
4555 ;; (To activate the minor mode is out of the question, since it will
4556 ;; ruin the inferior Erlang keymap.)
4557 ;; This is done differently in Emacs 21.
4558 (defun inferior-erlang-next-error (&optional argp)
4559 "Just like `next-error'.
4560 Capable of finding error messages in an inferior Erlang buffer."
4563 (buf (and (boundp 'compilation-last-buffer)
4564 compilation-last-buffer)))
4565 (if (and (bufferp buf)
4568 (and (eq major-mode 'erlang-shell-mode)
4569 (setq major-mode 'compilation-mode))))
4576 (setq major-mode 'erlang-shell-mode))))
4578 (next-error argp))))
4581 (defun inferior-erlang-change-directory (&optional dir)
4582 "Make the inferior Erlang change directory.
4583 The default is to go to the directory of the current buffer."
4585 (or dir (setq dir (file-name-directory (buffer-file-name))))
4586 (or (inferior-erlang-running-p)
4587 (error "No inferior Erlang is running"))
4588 (inferior-erlang-display-buffer)
4589 (inferior-erlang-wait-prompt)
4590 (inferior-erlang-send-command (format "cd('%s')." dir) nil))
4592 (defun erlang-align-arrows (start end)
4593 "Align arrows (\"->\") in function clauses from START to END.
4594 When called interactively, aligns arrows after function clauses inside
4597 With a prefix argument, aligns all arrows, not just those in function
4602 sum(L) -> sum(L, 0).
4603 sum([H|T], Sum) -> sum(T, Sum + H);
4604 sum([], Sum) -> Sum.
4608 sum(L) -> sum(L, 0).
4609 sum([H|T], Sum) -> sum(T, Sum + H);
4610 sum([], Sum) -> Sum."
4613 (let (;; regexp for matching arrows. without a prefix argument,
4614 ;; the regexp matches function heads. With a prefix, it
4615 ;; matches any arrow.
4616 (re (if current-prefix-arg
4619 (concat "^" erlang-atom-regexp ".*\\(\\)->"))))
4620 ;; part of regexp matching directly before the arrow
4621 (arrow-match-pos (if current-prefix-arg
4623 (1+ erlang-atom-regexp-matches)))
4624 ;; accumulator for positions where arrows are found, ordered
4625 ;; by buffer position (from greatest to smallest)
4626 (arrow-positions '())
4627 ;; accumulator for longest distance from start of line to arrow
4629 ;; marker to track the end of the region we're aligning
4630 (end-marker (progn (goto-char end)
4632 ;; Pass 1: Find the arrow positions, adjust the whitespace
4633 ;; before each arrow to one space, and find the greatest
4634 ;; indentation level.
4636 (while (re-search-forward re end-marker t)
4637 (goto-char (match-beginning arrow-match-pos))
4638 (just-one-space) ; adjust whitespace
4639 (setq arrow-positions (cons (point) arrow-positions))
4640 (setq most-indent (max most-indent (erlang-column-number))))
4641 (set-marker end-marker nil) ; free the marker
4642 ;; Pass 2: Insert extra padding so that all arrow indentation is
4643 ;; equal. This is done last-to-first by buffer position, so that
4644 ;; inserting spaces before one arrow doesn't change the
4645 ;; positions of the next ones.
4646 (mapcar (lambda (arrow-pos)
4647 (goto-char arrow-pos)
4648 (let* ((pad (- most-indent (erlang-column-number))))
4650 (insert-char ?\ pad))))
4653 (defun erlang-column-number ()
4654 "Return the column number of the current position in the buffer.
4655 Tab characters are counted by their visual width."
4656 (string-width (buffer-substring (line-beginning-position) (point))))
4658 (defun erlang-current-defun ()
4659 "`add-log-current-defun-function' for Erlang."
4661 (erlang-beginning-of-function)
4662 (if (looking-at "[a-z0-9_]+")
4665 ;; Aliases for backward compatibility with older versions of Erlang Mode.
4667 ;; Unfortuantely, older versions of Emacs doesn't have `defalias' and
4668 ;; `make-obsolete' so we have to define our own `obsolete' function.
4670 (defun erlang-obsolete (sym newdef)
4671 "Make the obsolete function SYM refer to the defined function NEWDEF.
4673 Simplified version of a combination `defalias' and `make-obsolete',
4674 it assumes that NEWDEF is loaded."
4675 (defalias sym (symbol-function newdef))
4676 (if (fboundp 'make-obsolete)
4677 (make-obsolete sym newdef)))
4680 (erlang-obsolete 'calculate-erlang-indent 'erlang-calculate-indent)
4681 (erlang-obsolete 'calculate-erlang-stack-indent
4682 'erlang-calculate-stack-indent)
4683 (erlang-obsolete 'at-erlang-keyword 'erlang-at-keyword)
4684 (erlang-obsolete 'at-erlang-operator 'erlang-at-operator)
4685 (erlang-obsolete 'beginning-of-erlang-clause 'erlang-beginning-of-clause)
4686 (erlang-obsolete 'end-of-erlang-clause 'erlang-end-of-clause)
4687 (erlang-obsolete 'mark-erlang-clause 'erlang-mark-clause)
4688 (erlang-obsolete 'beginning-of-erlang-function 'erlang-beginning-of-function)
4689 (erlang-obsolete 'end-of-erlang-function 'erlang-end-of-function)
4690 (erlang-obsolete 'mark-erlang-function 'erlang-mark-function)
4691 (erlang-obsolete 'pass-over-erlang-clause 'erlang-pass-over-function)
4692 (erlang-obsolete 'name-of-erlang-function 'erlang-name-of-function)
4695 ;; Fixme: shouldn't redefine `set-visited-file-name' anyhow -- see above.
4696 (defconst erlang-unload-hook
4698 (defalias 'set-visited-file-name
4699 'erlang-orig-set-visited-file-name)
4700 (when (featurep 'advice)
4701 (ad-unadvise 'Man-notify-when-ready)
4702 (ad-unadvise 'set-visited-file-name)))))
4708 (run-hooks 'erlang-load-hook)
4711 ;; coding: iso-8859-1
4714 ;;; erlang.el ends here