1 ;; erlang.el --- Major modes for editing and running Erlang
3 ;; Copyright (C) 1995-1998,2000 Ericsson Telecom AB
4 ;; Copyright (C) 2004 Free Software Foundation, Inc.
5 ;; Author: Anders Lindgren
7 ;; Keywords: erlang, languages, processes
10 ;; The contents of this file are subject to the Erlang Public License,
11 ;; Version 1.1, (the "License"); you may not use this file except in
12 ;; compliance with the License. You should have received a copy of the
13 ;; Erlang Public License along with this software. If not, it can be
14 ;; retrieved via the world wide web at http://www.erlang.org/.
16 ;; Software distributed under the License is distributed on an "AS IS"
17 ;; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
18 ;; the License for the specific language governing rights and limitations
21 ;; The Initial Developer of the Original Code is Ericsson Utvecklings AB.
22 ;; All Rights Reserved.
24 ;; Lars Thorsén's modifications of 2000-06-07 included.
26 ;; The original version of this package was written by Robert Virding.
28 ;; Most skeletons have been written at Ericsson Telecom by
29 ;; magnus@erix.ericsson.se and janne@erix.ericsson.se
36 ;; This package provides support for the programming language Erlang.
37 ;; The package provides an editing mode with lots of bells and
38 ;; whistles, compilation support, and it makes it possible for the
39 ;; user to start Erlang shells that run inside Emacs.
41 ;; See the Erlang distribution for full documentation of this package.
46 ;; Place this file in Emacs load path, byte-compile it, and add the
47 ;; following line to the appropriate init file:
49 ;; (require 'erlang-start)
51 ;; The full documentation contains much more extensive description of
52 ;; the installation procedure.
57 ;; Please send bug reports to the following email address:
58 ;; erlang-bugs@erlang.org
59 ;; or if you have a patch suggestion to:
60 ;; erlang-patches@erlang.org
61 ;; Please state as exactly as possible:
62 ;; - Version number of Erlang Mode (see the menu), Emacs, Erlang,
63 ;; and of any other relevant software.
64 ;; - What the expected result was.
65 ;; - What you did, preferably in a repeatable step-by-step form.
66 ;; - A description of the unexpected result.
67 ;; - Relevant pieces of Erlang code causing the problem.
68 ;; - Personal Emacs customisations, if any.
70 ;; Should the Emacs generate an error, please set the Emacs variable
71 ;; `debug-on-error' to `t'. Repeat the error and enclose the debug
72 ;; information in your bug-report.
74 ;; To set the variable you can use the following command:
75 ;; M-x set-variable RET debug-on-error RET t RET
80 (defconst erlang-version
"2.5.2"
81 "The version number of Erlang mode.")
83 (defvar erlang-root-dir nil
84 "The directory where the Erlang system is installed.
85 The name should not contain the trailing slash.
87 Should this variable be nil, no manual pages will show up in the
90 (defvar erlang-menu-items
'(erlang-menu-base-items
91 erlang-menu-skel-items
92 erlang-menu-shell-items
93 erlang-menu-compile-items
95 erlang-menu-personal-items
96 erlang-menu-version-items
)
97 "*List of menu item list to combine to create Erlang mode menu.
99 External programs which temporarily add menu items to the Erlang mode
100 menu may use this variable. Please use the function `add-hook' to add
103 Please call the function `erlang-menu-init' after every change to this
106 (defvar erlang-menu-base-items
108 (("Indent Line" erlang-indent-command
)
109 ("Indent Region " erlang-indent-region
110 (if erlang-xemacs-p
(mark) mark-active
))
111 ("Indent Clause" erlang-indent-clause
)
112 ("Indent Function" erlang-indent-function
)
113 ("Indent Buffer" erlang-indent-current-buffer
)))
115 (("Fill Comment" erlang-fill-paragraph
)
116 ("Comment Region" comment-region
117 (if erlang-xemacs-p
(mark) mark-active
))
118 ("Uncomment Region" erlang-uncomment-region
119 (if erlang-xemacs-p
(mark) mark-active
))
121 ("Beginning of Function" erlang-beginning-of-function
)
122 ("End of Function" erlang-end-of-function
)
123 ("Mark Function" erlang-mark-function
)
125 ("Beginning of Clause" erlang-beginning-of-clause
)
126 ("End of Clause" erlang-end-of-clause
)
127 ("Mark Clause" erlang-mark-clause
)
129 ("New Clause" erlang-generate-new-clause
)
130 ("Clone Arguments" erlang-clone-arguments
)
132 ("Align Arrows" erlang-align-arrows
)))
133 ("Syntax Highlighting"
134 (("Level 3" erlang-font-lock-level-3
)
135 ("Level 2" erlang-font-lock-level-2
)
136 ("Level 1" erlang-font-lock-level-1
)
137 ("Off" erlang-font-lock-level-0
)))
139 (("Find Tag" find-tag
)
140 ("Find Next Tag" erlang-find-next-tag
)
141 ;("Find Regexp" find-tag-regexp)
142 ("Complete Word" erlang-complete-tag
)
143 ("Tags Apropos" tags-apropos
)
144 ("Search Files" tags-search
))))
145 "Description of menu used in Erlang mode.
147 This variable must be a list. The elements are either nil representing
148 a horizontal line or a list with two or three elements. The first is
149 the name of the menu item, the second is the function to call, or a
150 submenu, on the same same form as ITEMS. The third optional argument
151 is an expression which is evaluated every time the menu is displayed.
152 Should the expression evaluate to nil the menu item is ghosted.
155 '((\"Func1\" function-one)
157 ((\"Yellow\" function-yellow)
158 (\"Blue\" function-blue)))
160 (\"Region Function\" spook-function midnight-variable))
162 Call the function `erlang-menu-init' after modifying this variable.")
164 (defvar erlang-menu-shell-items
167 (("Start New Shell" erlang-shell
)
168 ("Display Shell" erlang-shell-display
))))
169 "Description of the Shell menu used by Erlang mode.
171 Please see the documentation of `erlang-menu-base-items'.")
173 (defvar erlang-menu-compile-items
175 (("Compile Buffer" erlang-compile
)
176 ("Display Result" erlang-compile-display
)
177 ("Next Error" erlang-next-error
))))
178 "Description of the Compile menu used by Erlang mode.
180 Please see the documentation of `erlang-menu-base-items'.")
182 (defvar erlang-menu-version-items
184 ("Version" erlang-version
))
185 "Description of the version menu used in Erlang mode.")
187 (defvar erlang-menu-personal-items nil
188 "Description of personal menu items used in Erlang mode.
190 Please see the variable `erlang-menu-base-items' for a description
193 (defvar erlang-menu-man-items nil
194 "The menu containing man pages.
196 The format of the menu should be compatible with `erlang-menu-base-items'.
197 This variable is added to the list of Erlang menus stored in
198 `erlang-menu-items'.")
200 (defvar erlang-menu-skel-items
'()
201 "Description of the menu containing the skeleton entries.
202 The menu is in the form described by the variable `erlang-menu-base-items'.")
204 (defvar erlang-mode-hook nil
205 "*Functions to run when Erlang mode is activated.
207 This hook is used to change the behaviour of Erlang mode. It is
208 normally used by the user to personalise the programming environment.
209 When used in a site init file, it could be used to customise Erlang
210 mode for all users on the system.
212 The functions added to this hook are run every time Erlang mode is
213 started. See also `erlang-load-hook', a hook which is run once,
214 when Erlang mode is loaded into Emacs, and `erlang-shell-mode-hook'
215 which is run every time a new inferior Erlang shell is started.
217 To use a hook, create an Emacs lisp function to perform your actions
218 and add the function to the hook by calling `add-hook'.
220 The following example binds the key sequence C-c C-c to the command
221 `erlang-compile' (normally bound to C-c C-k). The example also
222 activates Font Lock mode to fontify the buffer and adds a menu
223 containing all functions defined in the current buffer.
225 To use the example, copy the following lines to your `~/.emacs' file:
227 (add-hook 'erlang-mode-hook 'my-erlang-mode-hook)
229 (defun my-erlang-mode-hook ()
230 (local-set-key \"\\C-c\\C-c\" 'erlang-compile)
233 (setq font-lock-maximum-decoration t)
235 (if (and window-system (fboundp 'imenu-add-to-menubar))
236 (imenu-add-to-menubar \"Imenu\")))")
238 (defvar erlang-load-hook nil
239 "*Functions to run when Erlang mode is loaded.
241 This hook is used to change the behaviour of Erlang mode. It is
242 normally used by the user to personalise the programming environment.
243 When used in a site init file, it could be used to customize Erlang
244 mode for all users on the system.
246 The difference between this hook and `erlang-mode-hook' and
247 `erlang-shell-mode-hook' is that the functions in this hook
248 is only called once, when the Erlang mode is loaded into Emacs
251 Natural actions for the functions added to this hook are actions which
252 only should be performed once, and actions which should be performed
253 before starting Erlang mode. For example, a number of variables are
254 used by Erlang mode before `erlang-mode-hook' is run.
256 The following example sets the variable `erlang-root-dir' so that the
257 manual pages can be retrieved (note that you must set the value of
258 `erlang-root-dir' to match the location of Erlang on your system):
260 (add-hook 'erlang-load-hook 'my-erlang-load-hook)
262 (defun my-erlang-load-hook ()
263 (setq erlang-root-dir \"/usr/local/erlang\"))")
265 (defvar erlang-new-file-hook nil
266 "Functions to run when a new Erlang source file is being edited.
268 A useful function is `tempo-template-erlang-normal-header'.
269 \(This function only exists when the `tempo' package is available.)")
271 (defvar erlang-check-module-name
'ask
272 "*Non-nil means check that module name and file name agrees when saving.
274 If the value of this variable is the atom `ask', the user is
275 prompted. If the value is t the source is silently changed.")
277 (defvar erlang-electric-commands
278 '(erlang-electric-comma
279 erlang-electric-semicolon
281 "*List of activated electric commands.
283 The list should contain the electric commands which should be active.
284 Currently, the available electric commands are:
285 `erlang-electric-comma'
286 `erlang-electric-semicolon'
288 `erlang-electric-newline'
290 Should the variable be bound to t, all electric commands
293 To deactivate all electric commands, set this variable to nil.")
295 (defvar erlang-electric-newline-inhibit t
296 "*Set to non-nil to inhibit newline after electric command.
298 This is useful since a lot of people press return after executing an
301 In order to work, the command must also be in the
302 list `erlang-electric-newline-inhibit-list'.
304 Note that commands in this list are required to set the variable
305 `erlang-electric-newline-inhibit' to nil when the newline shouldn't be
308 (defvar erlang-electric-newline-inhibit-list
309 '(erlang-electric-semicolon
310 erlang-electric-comma
312 "*Commands which can inhibit the next newline.")
314 (defvar erlang-electric-semicolon-insert-blank-lines nil
315 "*Number of blank lines inserted before header, or nil.
317 This variable controls the behaviour of `erlang-electric-semicolon'
318 when a new function header is generated. When nil, no blank line is
319 inserted between the current line and the new header. When bound to a
320 number it represents the number of blank lines which should be
323 (defvar erlang-electric-semicolon-criteria
324 '(erlang-next-lines-empty-p
325 erlang-at-keyword-end-p
326 erlang-at-end-of-function-p
)
327 "*List of functions controlling `erlang-electric-semicolon'.
328 The functions in this list are called, in order, whenever a semicolon
329 is typed. Each function in the list is called with no arguments,
330 and should return one of the following values:
332 nil -- no determination made, continue checking
333 'stop -- do not create prototype for next line
334 (anything else) -- insert prototype, and stop checking
336 If every function in the list is called with no determination made,
337 then no prototype is inserted.
339 The test is performed by the function `erlang-test-criteria-list'.")
341 (defvar erlang-electric-comma-criteria
342 '(erlang-stop-when-inside-argument-list
343 erlang-stop-when-at-guard
344 erlang-next-lines-empty-p
345 erlang-at-keyword-end-p
346 erlang-at-end-of-function-p
)
347 "*List of functions controlling `erlang-electric-comma'.
348 The functions in this list are called, in order, whenever a comma
349 is typed. Each function in the list is called with no arguments,
350 and should return one of the following values:
352 nil -- no determination made, continue checking
353 'stop -- do not create prototype for next line
354 (anything else) -- insert prototype, and stop checking
356 If every function in the list is called with no determination made,
357 then no prototype is inserted.
359 The test is performed by the function `erlang-test-criteria-list'.")
361 (defvar erlang-electric-arrow-criteria
362 '(erlang-next-lines-empty-p
363 erlang-at-end-of-function-p
)
364 "*List of functions controlling the arrow aspect of `erlang-electric-gt'.
365 The functions in this list are called, in order, whenever a `>'
366 is typed. Each function in the list is called with no arguments,
367 and should return one of the following values:
369 nil -- no determination made, continue checking
370 'stop -- do not create prototype for next line
371 (anything else) -- insert prototype, and stop checking
373 If every function in the list is called with no determination made,
374 then no prototype is inserted.
376 The test is performed by the function `erlang-test-criteria-list'.")
378 (defvar erlang-electric-newline-criteria
380 "*List of functions controlling `erlang-electric-newline'.
382 The electric newline commands indents the next line. Should the
383 current line begin with a comment the comment start is copied to
384 the newly created line.
386 The functions in this list are called, in order, whenever a comma
387 is typed. Each function in the list is called with no arguments,
388 and should return one of the following values:
390 nil -- no determination made, continue checking
391 'stop -- do not create prototype for next line
392 (anything else) -- trigger the electric command.
394 If every function in the list is called with no determination made,
395 then no prototype is inserted. Should the atom t be a member of the
396 list, it is treated as a function triggering the electric command.
398 The test is performed by the function `erlang-test-criteria-list'.")
400 (defvar erlang-next-lines-empty-threshold
2
401 "*Number of blank lines required to activate an electric command.
403 Actually, this value controls the behaviour of the function
404 `erlang-next-lines-empty-p' which normally is a member of the
405 criteria lists controlling the electric commands. (Please see
406 the variables `erlang-electric-semicolon-criteria' and
407 `erlang-electric-comma-criteria'.)
409 The variable is bound to a threshold value, a number, representing the
410 number of lines which must be empty.
412 Setting this variable to zero, electric commands will always be
413 triggered by `erlang-next-lines-empty-p', unless inhibited by other
416 Should this variable be nil, `erlang-next-lines-empty-p' will never
417 trigger an electric command. The same effect would be reached if the
418 function `erlang-next-lines-empty-p' would be removed from the criteria
421 Note that even if `erlang-next-lines-empty-p' should not trigger an
422 electric command, other functions in the criteria list could.")
424 (defvar erlang-new-clause-with-arguments nil
425 "*Non-nil means that the arguments are cloned when a clause is generated.
427 A new function header can be generated by calls to the function
428 `erlang-generate-new-clause' and by use of the electric semicolon.")
430 (defvar erlang-compile-use-outdir t
431 "*When nil, go to the directory containing source file when compiling.
433 This is a workaround for a bug in the `outdir' option of compile. If the
434 outdir is not in the current load path, Erlang doesn't load the object
435 module after it has been compiled.
437 To activate the workaround, place the following in your `~/.emacs' file:
438 (setq erlang-compile-use-outdir nil)")
440 (defvar erlang-indent-level
4
441 "*Indentation of Erlang calls/clauses within blocks.")
443 (defvar erlang-indent-guard
2
444 "*Indentation of Erlang guards.")
446 (defvar erlang-argument-indent
2
447 "*Indentation of the first argument in a function call.
448 When nil, indent to the column after the `(' of the
451 (defvar erlang-tab-always-indent t
452 "*Non-nil means TAB in Erlang mode should always re-indent the current line,
453 regardless of where in the line point is when the TAB command is used.")
455 (defvar erlang-error-regexp-alist
456 '(("^\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" .
(1 2)))
457 "*Patterns for matching Erlang errors.")
459 (defvar erlang-man-inhibit
(eq system-type
'windows-nt
)
460 "Inhibit the creation of the Erlang Manual Pages menu.
462 The Windows distribution of Erlang does not include man pages, hence
463 there is no attempt to create the menu.")
465 (defvar erlang-man-dirs
466 '(("Man - Commands" "/man/man1" t
)
467 ("Man - Modules" "/man/man3" t
)
468 ("Man - Files" "/man/man4" t
)
469 ("Man - Applications" "/man/man6" t
))
470 "*The man directories displayed in the Erlang menu.
472 Each item in the list should be a list with three elements, the first
473 the name of the menu, the second the directory, and the last a flag.
474 Should the flag the nil, the directory is absolute, should it be non-nil
475 the directory is relative to the variable `erlang-root-dir'.")
477 (defvar erlang-man-max-menu-size
20
478 "*The maximum number of menu items in one menu allowed.")
480 (defvar erlang-man-display-function
'erlang-man-display
481 "*Function used to display man page.
483 The function is called with one argument, the name of the file
484 containing the man page. Use this variable when the default
485 function, `erlang-man-display', does not work on your system.")
488 (defconst erlang-atom-regexp
"\\([a-z][a-zA-Z0-9_]*\\|'[^\n']*[^\\]'\\)"
489 "Regexp which should match an Erlang atom.
491 The regexp must be surrounded with a pair of regexp parentheses."))
492 (defconst erlang-atom-regexp-matches
1
493 "Number of regexp parenthesis pairs in `erlang-atom-regexp'.
495 This is used to determine parenthesis matches in complex regexps which
496 contains `erlang-atom-regexp'.")
498 (defconst erlang-variable-regexp
"\\([A-Z_][a-zA-Z0-9_]*\\)"
499 "Regexp which should match an Erlang variable.
501 The regexp must be surrounded with a pair of regexp parentheses.")
502 (defconst erlang-variable-regexp-matches
1
503 "Number of regexp parenthesis pairs in `erlang-variable-regexp'.
505 This is used to determine matches in complex regexps which contains
506 `erlang-variable-regexp'.")
508 (defvar erlang-defun-prompt-regexp
(concat "^" erlang-atom-regexp
"\\s *(")
509 "Regexp which should match beginning of a clause.")
511 (defvar erlang-file-name-extension-regexp
"\\.[eh]rl$"
512 "*Regexp which should match an Erlang file name.
514 This regexp is used when an Erlang module name is extracted from the
515 name of an Erlang source file.
517 The regexp should only match the section of the file name which should
518 be excluded from the module name.
520 To match all files set this variable to \"\\\\(\\\\..*\\\\|\\\\)$\".
521 The matches all except the extension. This is useful if the Erlang
522 tags system should interpret tags on the form `module:tag' for
523 files written in other languages than Erlang.")
525 (defvar erlang-mode-map nil
526 "*Keymap used in Erlang mode.")
527 (defvar erlang-mode-abbrev-table nil
528 "Abbrev table in use in Erlang-mode buffers.")
529 (defvar erlang-mode-syntax-table nil
530 "Syntax table in use in Erlang-mode buffers.")
532 (defconst erlang-emacs-major-version
533 (if (boundp 'emacs-major-version
)
535 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version
)
536 (string-to-int (substring emacs-version
537 (match-beginning 1) (match-end 1))))
538 "Major version number of Emacs.")
540 (defconst erlang-emacs-minor-version
541 (if (boundp 'emacs-minor-version
)
543 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version
)
544 (string-to-int (substring emacs-version
545 (match-beginning 2) (match-end 2))))
546 "Minor version number of Emacs.")
548 (defconst erlang-xemacs-p
(string-match "Lucid\\|XEmacs" emacs-version
)
549 "Non-nil when running under XEmacs or Lucid Emacs.")
551 (defvar erlang-xemacs-popup-menu
'("Erlang Mode Commands" . nil
)
552 "Common popup menu for all buffers in Erlang mode.
554 This variable is destructively modified every time the Erlang menu
555 is modified. The effect is that all changes take effect in all
556 buffers in Erlang mode, just like under GNU Emacs.
558 Never EVER set this variable!")
560 (defconst inferior-erlang-use-cmm
(boundp 'minor-mode-overriding-map-alist
)
561 "Non-nil means use `compilation-minor-mode' in Erlang shell.")
563 ;; Tempo skeleton templates:
564 (load "erlang-skels")
566 ;; Font-lock variables
568 (defvar erlang-font-lock-modern-p
569 (cond ((>= erlang-emacs-major-version
20) t
)
570 (erlang-xemacs-p (>= erlang-emacs-minor-version
14))
571 ((= erlang-emacs-major-version
19) (>= erlang-emacs-minor-version
29))
573 "Non-nil when this version of Emacs uses a modern version of Font Lock.
575 This is determined by checking the version of Emacs used, the actual
576 font-lock code is not loaded.")
579 ;; The next few variables define different Erlang font-lock patterns.
580 ;; They could be appended to form a custom font-lock appearance.
582 ;; The function `erlang-font-lock-set-face' could be used to change
583 ;; the face of a pattern.
585 ;; Note that Erlang strings and atoms are highlighted with using
586 ;; syntactic analysis.
588 (defvar erlang-font-lock-keywords-func
590 (list (concat "^" erlang-atom-regexp
"\\s *(")
591 1 'font-lock-function-name-face t
))
592 "Font lock keyword highlighting a function header.")
594 (defvar erlang-font-lock-keywords-dollar
596 (list "\\(\\$\\([^\\]\\|\\\\\\([^0-7^\n]\\|[0-7]+\\|\\^[a-zA-Z]\\)\\)\\)"
597 1 'font-lock-string-face
))
598 "Font lock keyword highlighting numbers in ASCII form (e.g. $A).")
600 (defvar erlang-font-lock-keywords-arrow
602 (list "\\(->\\|:-\\)\\(\\s \\|$\\)" 2 'font-lock-function-name-face
))
603 "Font lock keyword highlighting clause arrow.")
605 (defvar erlang-font-lock-keywords-lc
607 (list "\\(<-\\)\\(\\s \\|$\\)" 1 'font-lock-keyword-face
)
608 (list "\\(||\\)\\(\\s \\|$\\)" 1 'font-lock-keyword-face
))
609 "Font lock keyword highlighting list comprehension operators.")
611 (defvar erlang-font-lock-keywords-keywords
613 (list (concat "\\<\\(a\\(fter\\|ndalso\\)\\|begin\\|c\\(atch\\|ase\\)"
614 "\\|end\\|fun\\|if\\|o\\(f\\|relse\\)\\|receive\\|try\\|when"
615 "\\|query\\)\\([^a-zA-Z0-9_]\\|$\\)")
616 1 'font-lock-keyword-face
))
617 "Font lock keyword highlighting Erlang keywords.")
619 (defvar erlang-font-lock-keywords-attr
621 (list (concat "^\\(-" erlang-atom-regexp
"\\)\\s *\\(\\.\\|(\\)")
622 1 'font-lock-function-name-face
))
623 "Font lock keyword highlighting attributes.")
625 (defvar erlang-font-lock-keywords-quotes
627 (list "`\\([-+a-zA-Z0-9_:*][-+a-zA-Z0-9_:*]+\\)'"
629 (if erlang-font-lock-modern-p
630 'font-lock-reference-face
631 'font-lock-keyword-face
)
633 "Font lock keyword highlighting words in single quotes in comments.
635 This is not the highlighting of Erlang strings and atoms, which
636 are highlighted by syntactic analysis.")
638 ;; Note: The guard `float' collides with the bif `float'.
639 (defvar erlang-font-lock-keywords-guards
643 "\\(is_\\)*\\(atom\\|boolean\\|function\\|binary\\|constant"
644 "\\|float\\|integer\\|list\\|number\\|p\\(id\\|ort\\)\\|"
645 "re\\(ference\\|cord\\)\\|tuple"
649 (if erlang-font-lock-modern-p
650 'font-lock-reference-face
651 'font-lock-keyword-face
)))
652 "Font lock keyword highlighting guards.")
654 (defvar erlang-font-lock-keywords-bifs
659 "a\\(bs\\|live\\|pply\\|tom_to_list\\)\\|"
660 "binary_to_\\(list\\|term\\)\\|"
661 "concat_binary\\|d\\(ate\\|isconnect_node\\)\\|"
662 "e\\(lement\\|rase\\|xit\\)\\|"
663 "float\\(\\|_to_list\\)\\|"
664 "g\\(arbage_collect\\|et\\(\\|_keys\\)\\|roup_leader\\)\\|"
666 "i\\(nte\\(ger_to_list\\|rnal_bif\\)\\|s_alive\\)\\|"
667 "l\\(ength\\|i\\(nk\\|st_to_\\(atom\\|binary\\|float\\|integer"
668 "\\|pid\\|tuple\\)\\)\\)\\|"
669 "make_ref\\|no\\(de\\(\\|_\\(link\\|unlink\\)\\|s\\)\\|talive\\)\\|"
671 "p\\(id_to_list\\|rocess\\(_\\(flag\\|info\\)\\|es\\)\\|ut\\)\\|"
672 "r\\(egister\\(\\|ed\\)\\|ound\\)\\|"
673 "s\\(e\\(lf\\|telement\\)\\|ize\\|"
674 "p\\(awn\\(\\|_link\\)\\|lit_binary\\)\\|tatistics\\)\\|"
675 "t\\(erm_to_binary\\|hrow\\|ime\\|l\\|"
676 "r\\(ace\\|unc\\)\\|uple_to_list\\)\\|"
677 "un\\(link\\|register\\)\\|whereis"
680 'font-lock-keyword-face
))
681 "Font lock keyword highlighting built in functions.")
683 (defvar erlang-font-lock-keywords-macros
685 (list (concat "?\\s *\\(" erlang-atom-regexp
686 "\\|" erlang-variable-regexp
"\\)\\>")
687 1 (if erlang-font-lock-modern-p
688 'font-lock-reference-face
689 'font-lock-type-face
))
690 (list (concat "^-\\(define\\|ifn?def\\)\\s *(\\s *\\(" erlang-atom-regexp
691 "\\|" erlang-variable-regexp
"\\)\\>")
692 2 (if erlang-font-lock-modern-p
693 'font-lock-reference-face
694 'font-lock-type-face
)))
695 "Font lock keyword highlighting macros.
696 This must be placed in front of `erlang-font-lock-keywords-vars'.")
698 (defvar erlang-font-lock-keywords-records
700 (list (concat "#\\s *" erlang-atom-regexp
"\\>")
701 1 'font-lock-type-face
)
702 ;; Don't highlight numerical constants.
703 (list "\\<[0-9][0-9]?#\\([0-9a-fA_F]+\\)\\>"
705 (list (concat "^-record(\\s *" erlang-atom-regexp
"\\>")
706 1 'font-lock-type-face
))
707 "Font lock keyword highlighting Erlang records.
708 This must be placed in front of `erlang-font-lock-keywords-vars'.")
710 (defvar erlang-font-lock-keywords-vars
712 (list (concat "\\<" erlang-variable-regexp
"\\>")
713 1 (if erlang-font-lock-modern-p
714 'font-lock-variable-name-face
715 'font-lock-type-face
)))
716 "Font lock keyword highlighting Erlang variables.
717 Must be preceded by `erlang-font-lock-keywords-macros' and `-records'
721 (defvar erlang-font-lock-keywords-1
722 (append erlang-font-lock-keywords-func
723 erlang-font-lock-keywords-dollar
724 erlang-font-lock-keywords-arrow
725 erlang-font-lock-keywords-keywords
)
726 ;; DocStringOrig: erlang-font-lock-keywords
727 "Font-lock keywords used by Erlang Mode.
729 There exists three levels of Font Lock keywords for Erlang:
730 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
731 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
732 `erlang-font-lock-keywords-3' - Variables, macros and records.
734 To use a specific level, please set the variable
735 `font-lock-maximum-decoration' to the appropriate level. Note that the
736 variable must be set before Erlang mode is activated.
739 (setq font-lock-maximum-decoration 2)")
742 (defvar erlang-font-lock-keywords-2
743 (append erlang-font-lock-keywords-1
744 erlang-font-lock-keywords-attr
745 erlang-font-lock-keywords-quotes
746 erlang-font-lock-keywords-guards
747 erlang-font-lock-keywords-bifs
)
748 ;; DocStringCopy: erlang-font-lock-keywords
749 "Font-lock keywords used by Erlang Mode.
751 There exists three levels of Font Lock keywords for Erlang:
752 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
753 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
754 `erlang-font-lock-keywords-3' - Variables, macros and records.
756 To use a specific level, please set the variable
757 `font-lock-maximum-decoration' to the appropriate level. Note that the
758 variable must be set before Erlang mode is activated.
761 (setq font-lock-maximum-decoration 2)")
764 (defvar erlang-font-lock-keywords-3
765 (append erlang-font-lock-keywords-2
766 erlang-font-lock-keywords-macros
767 erlang-font-lock-keywords-records
768 erlang-font-lock-keywords-vars
)
769 ;; DocStringCopy: erlang-font-lock-keywords
770 "Font-lock keywords used by Erlang Mode.
772 There exists three levels of Font Lock keywords for Erlang:
773 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
774 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
775 `erlang-font-lock-keywords-3' - Variables, macros and records.
777 To use a specific level, please set the variable
778 `font-lock-maximum-decoration' to the appropriate level. Note that the
779 variable must be set before Erlang mode is activated.
782 (setq font-lock-maximum-decoration 2)")
785 (defvar erlang-font-lock-keywords erlang-font-lock-keywords-3
786 ;; DocStringCopy: erlang-font-lock-keywords
787 "Font-lock keywords used by Erlang Mode.
789 There exists three levels of Font Lock keywords for Erlang:
790 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
791 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
792 `erlang-font-lock-keywords-3' - Variables, macros and records.
794 To use a specific level, please set the variable
795 `font-lock-maximum-decoration' to the appropriate level. Note that the
796 variable must be set before Erlang mode is activated.
799 (setq font-lock-maximum-decoration 2)")
802 (defvar erlang-font-lock-syntax-table nil
803 "Syntax table used by Font Lock mode.
805 The difference between this and the standard Erlang Mode
806 syntax table is that `_' is treated as part of words by
809 Unfortunately, XEmacs hasn't got support for a special Font
810 Lock syntax table. The effect is that `apply' in the atom
811 `foo_apply' will be highlighted as a bif.")
814 ;;; Avoid errors while compiling this file.
816 ;; `eval-when-compile' is not defined in Emacs 18. We define it as a
818 (or (fboundp 'eval-when-compile
)
819 (defmacro eval-when-compile
(&rest rest
) nil
))
821 ;; These umm...functions are new in Emacs 20. And, yes, until version
822 ;; 19.27 Emacs backquotes were this ugly.
824 (or (fboundp 'unless
)
825 (defmacro unless
(condition &rest body
)
826 "(unless CONDITION BODY...): If CONDITION is false, do BODY, else return nil."
832 (defmacro when
(condition &rest body
)
833 "(when CONDITION BODY...): If CONDITION is true, do BODY, else return nil."
838 (or (fboundp 'char-before
)
839 (defmacro char-before
(&optional pos
)
840 "Return the character in the current buffer just before POS."
841 (` (char-after (1- (or (, pos
) (point)))))))
844 (if (or (featurep 'bytecomp
)
845 (featurep 'byte-compile
))
847 (cond ((string-match "Lucid\\|XEmacs" emacs-version
)
848 (put 'comment-indent-hook
'byte-obsolete-variable nil
)
849 ;; Do not warn for unused variables
850 ;; when compiling under XEmacs.
851 (setq byte-compile-warnings
852 '(free-vars unresolved callargs redefine
))))
854 (require 'compile
))))
857 (defun erlang-version ()
858 "Return the current version of Erlang mode."
861 (message "Erlang mode version %s, written by Anders Lindgren"
867 (defun erlang-mode ()
868 "Major mode for editing Erlang source files in Emacs.
869 It knows about syntax and comment, it can indent code, it is capable
870 of fontifying the source file, the TAGS commands are aware of Erlang
871 modules, and the Erlang man pages can be accessed.
873 Should this module, \"erlang.el\", be installed properly, Erlang mode
874 is activated whenever an Erlang source or header file is loaded into
875 Emacs. To indicate this, the mode line should contain the word
878 The main feature of Erlang mode is indentation, press TAB and the
879 current line will be indented correctly.
881 Comments starting with only one `%' are indented to the column stored
882 in the variable `comment-column'. Comments starting with two `%':s
883 are indented with the same indentation as code. Comments starting
884 with at least three `%':s are indented to the first column.
886 However, Erlang mode contains much more, this is a list of the most
888 TAB - Indent the line.
889 C-c C-q - Indent current function.
890 M-; - Create a comment at the end of the line.
891 M-q - Fill a comment, i.e. wrap lines so that they (hopefully)
893 M-a - Goto the beginning of an Erlang clause.
894 M-C-a - Ditto for function.
895 M-e - Goto the end of an Erlang clause.
896 M-C-e - Ditto for function.
897 M-h - Mark current Erlang clause.
898 M-C-h - Ditto for function.
899 C-c C-z - Start, or switch to, an inferior Erlang shell.
900 C-c C-k - Compile current file.
903 ; - Electric semicolon.
905 Erlang mode check the name of the file against the module name when
906 saving, whenever a mismatch occurs Erlang mode offers to modify the
909 The variable `erlang-electric-commands' controls the electric
910 commands. To deactivate all of them, set it to nil.
912 There exists a large number of commands and variables in the Erlang
913 module. Please press `M-x apropos RET erlang RET' to see a complete
914 list. Press `C-h f name-of-function RET' and `C-h v name-of-variable
915 RET'to see the full description of functions and variables,
918 On entry to this mode the contents of the hook `erlang-mode-hook' is
921 Please see the beginning of the file `erlang.el' for more information
922 and examples of hooks.
927 (kill-all-local-variables)
928 (setq major-mode
'erlang-mode
)
929 (setq mode-name
"Erlang")
930 (erlang-syntax-table-init)
932 (erlang-electric-init)
934 (erlang-mode-variables)
935 (erlang-check-module-name-init)
936 (erlang-add-compilation-alist erlang-error-regexp-alist
)
939 (erlang-font-lock-init)
941 (run-hooks 'erlang-mode-hook
)
942 (if (zerop (buffer-size))
943 (run-hooks 'erlang-new-file-hook
)))
946 (defun erlang-syntax-table-init ()
947 (if (null erlang-mode-syntax-table
)
948 (let ((table (make-syntax-table)))
949 (modify-syntax-entry ?
\n ">" table
)
950 (modify-syntax-entry ?
\" "\"" table
)
951 (modify-syntax-entry ?
# "." table
)
952 (modify-syntax-entry ?$
"'" 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
)
969 ;; Pseudo bit-syntax: Latin1 double angle quotes as parens.
970 ;;(modify-syntax-entry ?\253 "(?\273" table)
971 ;;(modify-syntax-entry ?\273 ")?\253" table)
973 (setq erlang-mode-syntax-table table
)))
975 (set-syntax-table erlang-mode-syntax-table
))
978 (defun erlang-keymap-init ()
981 (setq erlang-mode-map
(make-sparse-keymap))
982 (erlang-mode-commands erlang-mode-map
))
983 (use-local-map erlang-mode-map
))
986 (defun erlang-mode-commands (map)
987 (unless (boundp 'indent-line-function
)
988 (define-key map
"\t" 'erlang-indent-command
))
989 (define-key map
";" 'erlang-electric-semicolon
)
990 (define-key map
"," 'erlang-electric-comma
)
991 (define-key map
"<" 'erlang-electric-lt
)
992 (define-key map
">" 'erlang-electric-gt
)
993 (define-key map
"\C-m" 'erlang-electric-newline
)
994 (define-key map
"\177" 'backward-delete-char-untabify
)
995 ;;(unless (boundp 'fill-paragraph-function)
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 'set-buffer-menubar)
1289 ;; (symbol-value 'current-menubar))
1290 (funcall (symbol-function 'add-submenu
) nil menu
)
1291 (setcdr erlang-xemacs-popup-menu
(cdr menu
))
1292 (if (and popup
(boundp 'mode-popup-menu
))
1293 (funcall (symbol-function 'set
)
1294 'mode-popup-menu erlang-xemacs-popup-menu
))))
1295 ((>= erlang-emacs-major-version
19)
1296 (define-key keymap
(vector 'menu-bar
(intern name
))
1297 (erlang-menu-make-keymap name items
)))
1301 (defun erlang-menu-make-keymap (name items
)
1302 "Build a menu for Emacs 19."
1303 (let ((menumap (funcall (symbol-function 'make-sparse-keymap
)
1306 id def first second third
)
1307 (setq items
(reverse items
))
1309 ;; Replace any occurrence of atoms by their value.
1310 (while (and items
(atom (car items
)) (not (null (car items
))))
1311 (if (and (boundp (car items
))
1312 (listp (symbol-value (car items
))))
1313 (setq items
(append (reverse (symbol-value (car items
)))
1315 (setq items
(cdr items
))))
1316 (setq first
(car-safe (car items
)))
1317 (setq second
(car-safe (cdr-safe (car items
))))
1318 (setq third
(car-safe (cdr-safe (cdr-safe (car items
)))))
1320 (setq count
(+ count
1))
1321 (setq id
(intern (format "separator-%d" count
)))
1322 (setq def
'("--" . nil
)))
1323 ((and (consp second
) (eq (car second
) 'lambda
))
1324 (setq count
(+ count
1))
1325 (setq id
(intern (format "lambda-%d" count
)))
1326 (setq def
(cons first second
)))
1329 (setq def
(cons first second
)))
1331 (setq count
(+ count
1))
1332 (setq id
(intern (format "submenu-%d" count
)))
1333 (setq def
(erlang-menu-make-keymap first second
))))
1334 (define-key menumap
(vector id
) def
)
1336 (put id
'menu-enable third
))
1337 (setq items
(cdr items
)))
1338 (cons name menumap
)))
1341 (defun erlang-menu-xemacs (name items
&optional keymap
)
1342 "Build a menu for XEmacs."
1344 first second third entry
)
1346 ;; Replace any occurrence of atoms by their value.
1347 (while (and items
(atom (car items
)) (not (null (car items
))))
1348 (if (and (boundp (car items
))
1349 (listp (symbol-value (car items
))))
1350 (setq items
(append (reverse (symbol-value (car items
)))
1352 (setq items
(cdr items
))))
1353 (setq first
(car-safe (car items
)))
1354 (setq second
(car-safe (cdr-safe (car items
))))
1355 (setq third
(car-safe (cdr-safe (cdr-safe (car items
)))))
1357 (setq res
(cons "------" res
)))
1359 (setq res
(cons (vector first second
(or third t
)) res
)))
1360 ((and (consp second
) (eq (car second
) 'lambda
))
1361 (setq res
(cons (vector first
(list 'call-interactively second
)
1362 (or third t
)) res
)))
1364 (setq res
(cons (cons first
1365 (cdr (erlang-menu-xemacs
1368 (setq items
(cdr items
)))
1369 (setq res
(reverse res
))
1370 ;; When adding a menu to a minor-mode keymap under Emacs,
1371 ;; it disappears when the mode is disabled. The expression
1372 ;; generated below imitates this behaviour.
1373 ;; (This could be expressed much clearer using backquotes,
1374 ;; but I don't want to pull in every package.)
1376 (let ((expr (list 'or
1377 (list 'eq keymap
'global-map
)
1378 (list 'eq keymap
(list 'current-local-map
))
1383 'minor-mode-map-alist
))))))
1384 (setq res
(cons ':included
(cons expr res
)))))
1388 (defun erlang-menu-substitute (items alist
)
1389 "Substitute functions in menu described by ITEMS.
1391 The menu ITEMS is updated destructively.
1393 ALIST is list of pairs where the car is the old function and cdr the new."
1394 (let (first second pair
)
1396 (setq first
(car-safe (car items
)))
1397 (setq second
(car-safe (cdr-safe (car items
))))
1398 (cond ((null first
))
1400 (setq pair
(and second
(assq second alist
)))
1402 (setcar (cdr (car items
)) (cdr pair
))))
1403 ((and (consp second
) (eq (car second
) 'lambda
)))
1405 (erlang-menu-substitute second alist
)))
1406 (setq items
(cdr items
)))))
1409 (defun erlang-menu-add-above (entry above items
)
1410 "Add menu ENTRY above menu entry ABOVE in menu ITEMS.
1411 Do nothing if the items already should be in the menu.
1412 Should ABOVE not be in the list, the entry is added at
1413 the bottom of the menu.
1415 The new menu is returned. No guarantee is given that the original
1416 menu is left unchanged.
1418 The equality test is performed by `eq'.
1420 Example: (erlang-menu-add-above 'my-erlang-menu-items
1421 'erlang-menu-man-items)"
1422 (erlang-menu-add-below entry above items t
))
1425 (defun erlang-menu-add-below (entry below items
&optional above-p
)
1426 "Add menu ENTRY below menu items BELOW in the Erlang menu.
1427 Do nothing if the items already should be in the menu.
1428 Should BELOW not be in the list, items is added at the bottom
1431 The new menu is returned. No guarantee is given that the original
1432 menu is left unchanged.
1434 The equality test is performed by `eq'.
1438 \(setq erlang-menu-items
1439 (erlang-menu-add-below 'my-erlang-menu-items
1440 'erlang-menu-base-items
1441 erlang-menu-items))"
1442 (if (memq entry items
)
1443 items
; Return the original menu.
1449 (setq res
(append head
(list entry
)))
1451 ((eq below
(car items
))
1454 (append head
(cons entry items
))
1455 (append head
(cons (car items
)
1456 (cons entry
(cdr items
))))))
1459 (setq head
(append head
(list (car items
))))
1460 (setq items
(cdr items
)))))
1463 (defun erlang-menu-delete (entry items
)
1464 "Delete ENTRY from menu ITEMS.
1466 The new menu is returned. No guarantee is given that the original
1467 menu is left unchanged."
1472 (defun erlang-man-init ()
1473 "Add menus containing the manual pages of the Erlang.
1475 The variable `erlang-man-dirs' contains entries describing
1476 the location of the manual pages."
1478 (if erlang-man-inhibit
1480 (setq erlang-menu-man-items
1482 ("Man - Function" erlang-man-function
)))
1484 (setq erlang-menu-man-items
1485 (append erlang-menu-man-items
1486 (erlang-man-make-top-menu erlang-man-dirs
))))
1487 (setq erlang-menu-items
1488 (erlang-menu-add-above 'erlang-menu-man-items
1489 'erlang-menu-version-items
1491 (erlang-menu-init)))
1494 (defun erlang-man-uninstall ()
1495 "Remove the man pages from the Erlang mode."
1497 (setq erlang-menu-items
1498 (erlang-menu-delete 'erlang-menu-man-items erlang-menu-items
))
1502 ;; The man menu is a hierarchal structure, with the manual sections
1503 ;; at the top, described by `erlang-man-dirs'. The next level could
1504 ;; either be the manual pages if not to many, otherwise it is an index
1505 ;; menu whose submenus will contain up to `erlang-man-max-menu-size'
1508 (defun erlang-man-make-top-menu (dir-list)
1509 "Create one menu entry per element of DIR-LIST.
1510 The format is described in the documentation of `erlang-man-dirs'."
1514 (setq dir
(cond ((nth 2 (car dir-list
))
1515 ;; Relative to `erlang-root-dir'.
1516 (and (stringp erlang-root-dir
)
1517 (concat erlang-root-dir
(nth 1 (car dir-list
)))))
1520 (nth 1 (car dir-list
)))))
1522 (file-readable-p dir
))
1523 (setq menu
(cons (list (car (car dir-list
))
1524 (erlang-man-make-middle-menu
1525 (erlang-man-get-files dir
)))
1527 (setq dir-list
(cdr dir-list
)))
1528 ;; Should no menus be found, generate a menu item which
1529 ;; will display a help text, when selected.
1533 (("Error! Why?" erlang-man-describe-error
)))))))
1536 ;; Should the menu be to long, let's split it into a number of
1537 ;; smaller menus. Warning, this code contains beautiful
1538 ;; destructive operations!
1539 (defun erlang-man-make-middle-menu (filelist)
1540 "Create the second level menu from FILELIST.
1542 Should the list be longer than `erlang-man-max-menu-size', a tree of
1544 (if (<= (length filelist
) erlang-man-max-menu-size
)
1545 (erlang-man-make-menu filelist
)
1547 (filelist (copy-sequence filelist
))
1548 segment submenu pair
)
1550 (setq pair
(nthcdr (- erlang-man-max-menu-size
1) filelist
))
1551 (setq segment filelist
)
1554 (setq filelist
(cdr pair
))
1556 (setq submenu
(erlang-man-make-menu segment
))
1557 (setq menu
(cons (list (concat (car (car submenu
))
1559 (car (car (reverse submenu
))))
1565 (defun erlang-man-make-menu (filelist)
1566 "Make a leaf menu based on FILELIST."
1570 (setq item
(erlang-man-make-menu-item (car filelist
)))
1572 (setq menu
(cons item menu
)))
1573 (setq filelist
(cdr filelist
)))
1577 (defun erlang-man-make-menu-item (file)
1578 "Create a menu item containing the name of the man page."
1579 (and (string-match ".*/\\([^/]+\\)\\.[^.]$" file
)
1580 (let ((page (substring file
(match-beginning 1) (match-end 1))))
1581 (list (capitalize page
)
1584 (list 'funcall
'erlang-man-display-function
1588 (defun erlang-man-get-files (dir)
1589 "Return files in directory DIR."
1590 (directory-files dir t
".*\\.[0-9]\\'"))
1593 (defun erlang-man-module (&optional module
)
1594 "Find manual page for MODULE, defaults to module of function under point.
1595 This function is aware of imported functions."
1597 (list (let* ((mod (car-safe (erlang-get-function-under-point)))
1599 (format "Manual entry for module%s: "
1600 (if (or (null mod
) (string= mod
""))
1602 (format " (default %s)" mod
))))))
1603 (if (string= input
"")
1606 (or module
(setq module
(car (erlang-get-function-under-point))))
1607 (if (or (null module
) (string= module
""))
1608 (error "No Erlang module name given"))
1609 (let ((dir-list erlang-man-dirs
)
1610 (pat (concat "/" (regexp-quote module
) "\\.[^.]$"))
1613 (while (and dir-list
(null file
))
1614 (setq file-list
(erlang-man-get-files
1615 (if (nth 2 (car dir-list
))
1616 (concat erlang-root-dir
(nth 1 (car dir-list
)))
1617 (nth 1 (car dir-list
)))))
1618 (while (and file-list
(null file
))
1619 (if (string-match pat
(car file-list
))
1620 (setq file
(car file-list
)))
1621 (setq file-list
(cdr file-list
)))
1622 (setq dir-list
(cdr dir-list
)))
1624 (funcall erlang-man-display-function file
)
1625 (error "No manual page for module %s found" module
))))
1628 ;; Warning, the function `erlang-man-function' is a hack!
1629 ;; It links itself into the man code in a non-clean way. I have
1630 ;; chosen to keep it since it provides a very useful functionality
1631 ;; which is not possible to achieve using a clean approach.
1634 (defvar erlang-man-function-name nil
1635 "Name of function for last `erlang-man-function' call.
1636 Used for communication between `erlang-man-function' and the
1637 patch to `Man-notify-when-ready'.")
1639 (defun erlang-man-function (&optional name
)
1640 "Find manual page for NAME, where NAME is module:function.
1641 The entry for `function' is displayed.
1643 This function is aware of imported functions."
1645 (list (let* ((mod-func (erlang-get-function-under-point))
1646 (mod (car-safe mod-func
))
1647 (func (nth 1 mod-func
))
1650 "Manual entry for `module:func' or `module'%s: "
1651 (if (or (null mod
) (string= mod
""))
1653 (format " (default %s:%s)" mod func
))))))
1654 (if (string= input
"")
1656 (concat mod
":" func
)
1659 ;; Emacs 18 doesn't provide `man'...
1666 (let ((mod-func (erlang-get-function-under-point)))
1667 (setq modname
(car-safe mod-func
))
1668 (setq funcname
(nth 1 mod-func
))))
1669 ((string-match ":" name
)
1670 (setq modname
(substring name
0 (match-beginning 0)))
1671 (setq funcname
(substring name
(match-end 0) nil
)))
1673 (setq modname name
)))
1674 (if (or (null modname
) (string= modname
""))
1675 (error "No Erlang module name given"))
1676 (cond ((fboundp 'Man-notify-when-ready
)
1677 ;; Emacs 19: The man command could possibly start an
1678 ;; asynchronous process, i.e. we must hook ourselves into
1679 ;; the system to be activated when the man-process
1683 (erlang-man-patch-notify)
1684 (setq erlang-man-function-name funcname
))
1686 (erlang-man-module modname
)
1687 (error (setq erlang-man-function-name nil
))))
1689 (erlang-man-module modname
)
1691 (erlang-man-find-function
1692 (or (get-buffer "*Manual Entry*") ; Emacs 18
1693 (current-buffer)) ; XEmacs
1697 ;; Should the defadvice be at the top level, the package `advice' would
1698 ;; be required. Now it is only required when this functionality
1699 ;; is used. (Emacs 19 specific.)
1700 (defun erlang-man-patch-notify ()
1701 "Patch the function `Man-notify-when-ready' to search for function.
1702 The variable `erlang-man-function-name' is assumed to be bound to
1703 the function name, or to nil.
1705 The reason for patching a function is that under Emacs 19, the man
1706 command is executed asynchronously."
1709 ;; This should never happened since this is only called when
1710 ;; running under Emacs 19.
1711 (error (error (concat "This command needs the package `advice', "
1712 "please upgrade your Emacs."))))
1714 (defadvice Man-notify-when-ready
1715 (after erlang-Man-notify-when-ready activate
)
1716 "Set point at the documentation of the function name in
1717 `erlang-man-function-name' when the man page is displayed."
1718 (if erlang-man-function-name
1719 (erlang-man-find-function (ad-get-arg 0) erlang-man-function-name
))
1720 (setq erlang-man-function-name nil
)))
1723 (defun erlang-man-find-function (buf func
)
1724 "Find manual page for function in `erlang-man-function-name' in buffer BUF."
1726 (let ((win (get-buffer-window buf
)))
1730 (goto-char (point-min))
1731 (if (re-search-forward
1732 (concat "^[ \t]+" func
" ?(")
1736 (set-window-point win
(point)))
1737 (message "Could not find function `%s'" func
)))))))
1740 (defun erlang-man-display (file)
1741 "Display FILE as a `man' file.
1742 This is the default manual page display function.
1743 The variables `erlang-man-display-function' contains the function
1745 ;; Emacs 18 doesn't `provide' man.
1750 (let ((process-environment (copy-sequence process-environment
)))
1751 (if (string-match "\\(.*\\)/man[^/]*/\\([^/]+\\)\\.[^.]$" file
)
1752 (let ((dir (substring file
(match-beginning 1) (match-end 1)))
1753 (page (substring file
(match-beginning 2) (match-end 2))))
1754 (if (fboundp 'setenv
)
1755 (setenv "MANPATH" dir
)
1757 (setq process-environment
(cons (concat "MANPATH=" dir
)
1758 process-environment
)))
1759 (cond ((not (and (not erlang-xemacs-p
)
1760 (= erlang-emacs-major-version
19)
1761 (< erlang-emacs-minor-version
29)))
1762 (manual-entry page
))
1764 ;; Emacs 19.28 and earlier versions of 19:
1765 ;; The manual-entry command unconditionally prompts
1767 (funcall (symbol-function 'Man-getpage-in-background
)
1769 (error "Can't find man page for %s\n" file
)))))
1772 (defun erlang-man-describe-error ()
1773 "Describe why the manual pages weren't found."
1775 (with-output-to-temp-buffer "*Erlang Man Error*"
1776 (princ "Normally, this menu should contain Erlang manual pages.
1778 In order to find the manual pages, the variable `erlang-root-dir'
1779 should be bound to the name of the directory containing the Erlang
1780 installation. The name should not include the final slash.
1782 Practically, you should add a line on the following form to
1783 your ~/.emacs, or ask your system administrator to add it to
1785 (setq erlang-root-dir \"/the/erlang/root/dir/goes/here\")
1788 (setq erlang-root-dir \"/usr/local/erlang\")
1790 After installing the line, kill and restart Emacs, or restart Erlang
1791 mode with the command `M-x erlang-mode RET'.")))
1793 ;; Indentation code:
1795 (defun erlang-indent-command (&optional whole-exp
)
1796 "Indent current line as Erlang code.
1797 With argument, indent any additional lines of the same clause
1798 rigidly along with this one."
1801 ;; If arg, always indent this line as Erlang
1802 ;; and shift remaining lines of clause the same amount.
1803 (let ((shift-amt (erlang-indent-line))
1806 (if erlang-tab-always-indent
1807 (beginning-of-line))
1809 (erlang-end-of-clause 1)
1815 (indent-code-rigidly beg end shift-amt
"\n")))
1816 (if (and (not erlang-tab-always-indent
)
1818 (skip-chars-backward " \t")
1821 (erlang-indent-line))))
1824 (defun erlang-indent-line ()
1825 "Indent current line as Erlang code.
1826 Return the amount the indentation changed by."
1827 (let ((pos (- (point-max) (point)))
1830 (beginning-of-line 1)
1832 (skip-chars-forward " \t")
1833 (cond ((looking-at "%")
1834 (setq indent
(funcall comment-indent-function
))
1835 (setq shift-amt
(- indent
(current-column))))
1837 (setq indent
(erlang-calculate-indent))
1838 (cond ((null indent
)
1839 (setq indent
(current-indentation)))
1841 ;; This should never occur here.
1842 (error "Erlang mode error"))
1843 ((= (char-syntax (following-char)) ?\
))
1844 (setq indent
(1- indent
))))
1845 (setq shift-amt
(- indent
(current-column)))))
1846 (if (zerop shift-amt
)
1848 (delete-region beg
(point))
1850 ;; If initial point was within line's indentation, position
1851 ;; after the indentation. Else stay at same point in text.
1852 (if (> (- (point-max) pos
) (point))
1853 (goto-char (- (point-max) pos
)))
1857 (defun erlang-indent-region (beg end
)
1858 "Indent region of Erlang code.
1860 This is automagically called by the user level function `indent-region'."
1863 (let ((case-fold-search nil
)
1865 (from-end (- (point-max) end
))
1866 indent-point
;; The beginning of the current line
1867 indent
;; The indent amount
1871 (setq indent-point
(point))
1872 (erlang-beginning-of-clause)
1873 ;; Parse the Erlang code from the beginning of the clause to
1874 ;; the beginning of the region.
1875 (while (< (point) indent-point
)
1876 (setq state
(erlang-partial-parse (point) indent-point state
)))
1877 ;; Indent every line in the region
1879 (goto-char indent-point
)
1880 (skip-chars-forward " \t")
1881 (cond ((looking-at "%")
1882 ;; Do not use our stack to help the user to customize
1883 ;; comment indentation.
1884 (setq indent
(funcall comment-indent-function
)))
1886 ;; Don't indent empty lines.
1891 (erlang-calculate-stack-indent (point) state
)))
1892 (cond ((null indent
)
1893 (setq indent
(current-indentation)))
1895 ;; This should never occur here.
1896 (error "Erlang mode error"))
1897 ((= (char-syntax (following-char)) ?\
))
1898 (setq indent
(1- indent
))))))
1899 (if (zerop (- indent
(current-column)))
1901 (delete-region indent-point
(point))
1903 ;; Find the next line in the region
1904 (goto-char indent-point
)
1907 (setq indent-point
(point)))
1908 (if (>= from-end
(- (point-max) indent-point
))
1910 (while (< (point) indent-point
)
1911 (setq state
(erlang-partial-parse
1912 (point) indent-point state
))))))))
1915 (defun erlang-indent-current-buffer ()
1916 "Indent current buffer as Erlang code."
1921 (erlang-indent-region (point-min) (point-max)))))
1924 (defun erlang-indent-function ()
1925 "Indent current Erlang function."
1928 (let ((end (progn (erlang-end-of-function 1) (point)))
1929 (beg (progn (erlang-beginning-of-function 1) (point))))
1930 (erlang-indent-region beg end
))))
1933 (defun erlang-indent-clause ()
1934 "Indent current Erlang clause."
1937 (let ((end (progn (erlang-end-of-clause 1) (point)))
1938 (beg (progn (erlang-beginning-of-clause 1) (point))))
1939 (erlang-indent-region beg end
))))
1942 (defmacro erlang-push
(x stack
) (list 'setq stack
(list 'cons x stack
)))
1943 (defmacro erlang-pop
(stack) (list 'setq stack
(list 'cdr stack
)))
1944 ;; Would much prefer to make caddr a macro but this clashes.
1945 (defun erlang-caddr (x) (car (cdr (cdr x
))))
1948 (defun erlang-calculate-indent (&optional parse-start
)
1949 "Compute appropriate indentation for current line as Erlang code.
1950 Return nil if line starts inside string, t if in a comment."
1952 (let ((indent-point (point))
1953 (case-fold-search nil
)
1956 (goto-char parse-start
)
1957 (erlang-beginning-of-clause))
1958 (while (< (point) indent-point
)
1959 (setq state
(erlang-partial-parse (point) indent-point state
)))
1960 (erlang-calculate-stack-indent indent-point state
))))
1962 (defun erlang-show-syntactic-information ()
1963 "Show syntactic information for current line."
1968 (let ((starting-point (point))
1969 (case-fold-search nil
)
1971 (erlang-beginning-of-clause)
1972 (while (< (point) starting-point
)
1973 (setq state
(erlang-partial-parse (point) starting-point state
)))
1974 (message "%S" state
))))
1977 (defun erlang-partial-parse (from to
&optional state
)
1978 "Parse Erlang syntax starting at FROM until TO, with an optional STATE.
1979 Value is list (stack token-start token-type in-what)."
1980 (goto-char from
) ; Start at the beginning
1981 (erlang-skip-blank to
)
1982 (let ((cs (char-syntax (following-char)))
1988 ;; Done: Return previous state.
1990 (setq token
(nth 1 state
))
1991 (setq cs
(nth 2 state
))
1992 (setq in-what
(nth 3 state
)))
1994 ;; Word constituent: check and handle keywords.
1996 (cond ((looking-at "\\(end\\|after\\)[^_a-zA-Z0-9]")
1997 ;; Must pop top icr layer, `after' will push a new
2000 (while (and stack
(eq (car (car stack
)) '-
>))
2002 (if (and stack
(memq (car (car stack
)) '(icr begin
)))
2003 (erlang-pop stack
))))
2004 ((looking-at "catch[^,\n\\of]*\n")
2005 ;; Must pop top icr layer, `catch' in try/catch
2006 ;;will push a new layer next.
2008 (while (and stack
(eq (car (car stack
)) '-
>))
2010 (if (and stack
(memq (car (car stack
)) '(icr begin
)))
2011 (erlang-pop stack
))))
2012 ;;((looking-at "^of$")
2013 ;; Must pop top icr layer, `of' in try/catch
2014 ;;will push a new layer next.
2016 ;; (while (and stack (eq (car (car stack)) '->))
2017 ;; (erlang-pop stack))
2018 ;; (if (and stack (memq (car (car stack)) '(icr begin)))
2019 ;; (erlang-pop stack))))
2021 (cond ((looking-at "\\(if\\|case\\|receive\\|try\\)[^_a-zA-Z0-9]")
2022 ;; Must push a new icr (if/case/receive) layer.
2023 (erlang-push (list 'icr token
(current-column)) stack
))
2024 ((looking-at "\\(fun\\)[^_a-zA-Z0-9]")
2025 ;; Push a new icr layer if we are defining a `fun'
2026 ;; expression, not when we are refering an existing
2029 (goto-char (match-end 1))
2030 (erlang-skip-blank to
)
2031 (eq (following-char) ?\
())
2032 (erlang-push (list 'icr token
(current-column)) stack
)))
2033 ((looking-at "\\(begin\\|query\\)[^_a-zA-Z0-9]")
2034 (erlang-push (list 'begin token
(current-column)) stack
))
2035 ;; In test suites you may want to do something like
2036 ;; ?match(Mem when integer(Mem), mnesia:table_info(Tab,
2037 ;; memory)), and then the following if/case/receive
2038 ;; statement will mess up the indentation by fooling the
2039 ;; erlang mode to think the 'when' in the argument is a
2040 ;; "real" when. The following three clauses will avoid
2042 ((looking-at "when[^->\.]*if[^->\.]*->"))
2043 ((looking-at "when[^->\.]*case[^->\.]*->"))
2044 ((looking-at "when[^->\.]*receive[^->\.]*->"))
2046 ((looking-at "when [^->\.]*->")
2047 (erlang-push (list 'when token
(current-column)) stack
))
2048 ((looking-at "after[.]+->")
2049 (erlang-push (list 'icr token
(current-column)) stack
))
2050 ((looking-at "after[^_a-zA-Z0-9->]")
2051 ;; Probably in try-statment, fake "->" to get right
2052 ;; indentation in erlang-calculate-stack-indent. If it
2053 ;; was an ordinary catch without try, these entries will
2054 ;; be popped of the stack at a later ocaccion.
2055 (erlang-push (list 'icr token
(current-column)) stack
)
2056 (erlang-push (list '-
> token
(current-column)) stack
))
2057 ((looking-at "catch[^,\n\\of]*\n")
2058 (erlang-push (list 'icr token
(current-column)) stack
)
2059 (erlang-push (list '-
> token
(current-column)) stack
))
2060 ;;((looking-at "^of$")
2061 ;; (erlang-push (list 'icr token (current-column)) stack)
2062 ;;(erlang-push (list '-> token (current-column)) stack))
2065 ;; String: Try to skip over it. (Catch error if not complete.)
2072 (setq in-what
'string
)
2075 (setq in-what
'string
)
2078 ;; Expression prefix e.i. $ or ^ (Note ^ can be in the character
2079 ;; literal $^ or part of string and $ outside of a string denotes
2080 ;; a character literal)
2083 ((= (following-char) ?
\") ;; $ or ^ was the last char in a string
2086 ;; Maybe a character literal, quote the next char to avoid
2087 ;; situations as $" being seen as the begining of a string.
2088 ;; Note the quoting something in the middle of a string is harmless.
2089 (quote (following-char))
2092 ;; Symbol constituent or punctuation
2098 ((= (following-char) ?\
;)
2099 (if (and stack
(eq (car (car stack
)) '-
>))
2104 ((looking-at "\\.\\(\\s \\|\n\\|\\s<\\)")
2109 ((looking-at "->\\|:-")
2111 (back-to-indentation)
2112 (cond ((looking-at "after[^_a-zA-Z0-9]")
2113 (erlang-pop stack
))))
2114 (if (and stack
(eq (car (car stack
)) 'when
))
2116 (erlang-push (list '-
> token
(current-column)) stack
)
2119 ;; List-comprehension divider
2121 (erlang-push (list '|| token
(current-column)) stack
)
2124 ;;((looking-at ",$")
2125 ;; Normal catch not try-catch have caused icr
2126 ;; and then incr and faked "->" should be removed
2128 ;; (back-to-indentation)
2129 ;; (cond ((looking-at "catch[^_a-zA-Z0-9]")
2130 ;; (erlang-pop stack)
2131 ;; (erlang-pop stack))))
2134 ;; Parameter separator
2138 ;; Bit-syntax open paren
2140 (erlang-push (list '\
( token
(current-column)) stack
)
2143 ;; Bbit-syntax close paren
2145 (while (memq (car (car stack
)) '(|| -
>))
2147 (cond ((eq (car (car stack
)) '\
()
2149 ((memq (car (car stack
)) '(icr begin
))
2150 (error "Missing `end'"))
2152 (error "Unbalanced parentheses")))
2156 ((= (following-char) ??
)
2161 ;; Other punctuation: Skip over it and any following punctuation
2163 ;; Skip over all characters in the operand.
2164 (skip-syntax-forward "."))
2166 ;; Other char: Skip over it.
2172 (erlang-push (list '\
( token
(current-column)) stack
)
2175 ;; Close parenthesis
2177 (while (memq (car (car stack
)) '(|| -
>))
2179 (cond ((eq (car (car stack
)) '\
()
2181 ((eq (car (car stack
)) 'icr
)
2183 ;; Normal catch not try-catch might have caused icr
2184 ;; and then incr should be removed and is not an error.
2185 (if (eq (car (car stack
)) '\
()
2188 (error "Missing `end'"))
2190 ((eq (car (car stack
)) 'begin
)
2191 (error "Missing `end'")
2193 (error "Unbalanced parenthesis"))
2197 ;; Character quote: Skip it and the quoted char.
2201 ;; Character escape: Skip it and the escape sequence.
2204 (skip-syntax-forward "w"))
2209 (list stack token cs in-what
)))
2211 (defun erlang-calculate-stack-indent (indent-point state
)
2212 "From the given last position and state (stack) calculate indentation.
2213 Return nil if inside string, t if in a comment."
2214 (let* ((stack (and state
(car state
)))
2215 (token (nth 1 state
))
2216 (stack-top (and stack
(car stack
))))
2217 (cond ((null state
) ;No state
2221 (eq (nth 3 state
) 'comment
))
2223 (if (looking-at "when[^_a-zA-Z0-9]")
2226 ((eq (car stack-top
) '\
()
2227 ;; Element of list, tuple or part of an expression,
2228 (if (null erlang-argument-indent
)
2229 ;; indent to next column.
2230 (1+ (nth 2 stack-top
))
2231 (goto-char (nth 1 stack-top
))
2232 (cond ((looking-at "[({]\\s *\\($\\|%\\)")
2233 ;; Line ends with parenthesis.
2234 (+ (erlang-indent-find-preceding-expr)
2235 erlang-argument-indent
))
2237 ;; Indent to the same column as the first
2239 (goto-char (1+ (nth 1 stack-top
)))
2240 (skip-chars-forward " \t")
2241 (current-column)))))
2242 ((eq (car stack-top
) 'icr
)
2243 ;; The default indentation is the column of the option
2244 ;; directly following the keyword. (This does not apply to
2245 ;; `case'.) Should no option be on the same line, the
2246 ;; indentation is the indentation of the keyword +
2247 ;; `erlang-indent-level'.
2249 ;; `after' should be indented to the save level as the
2250 ;; corresponding receive.
2251 (if (looking-at "after[^_a-zA-Z0-9]")
2254 (goto-char (nth 1 stack-top
))
2255 (if (looking-at "case[^_a-zA-Z0-9]")
2256 (+ (nth 2 stack-top
) erlang-indent-level
)
2257 (skip-chars-forward "a-z")
2258 (skip-chars-forward " \t")
2259 (if (memq (following-char) '(?% ?
\n))
2260 (+ (nth 2 stack-top
) erlang-indent-level
)
2261 (current-column)))))
2262 (if (looking-at "catch[^_a-zA-Z0-9]")
2265 (goto-char (nth 1 stack-top
))
2266 (if (looking-at "case[^_a-zA-Z0-9]")
2267 (+ (nth 2 stack-top
) erlang-indent-level
)
2268 (skip-chars-forward "a-z")
2269 (skip-chars-forward " \t")
2270 (if (memq (following-char) '(?% ?
\n))
2271 (+ (nth 2 stack-top
) erlang-indent-level
)
2272 (current-column)))))
2274 ;; Real indentation, where operators create extra indentation etc.
2275 ((memq (car stack-top
) '(-> || begin
))
2276 (goto-char (nth 1 stack-top
))
2277 ;; Check if there is more code after the '->' on the
2278 ;; same line. If so use this indentation as base, else
2279 ;; use parent indentation + 2 * level as base.
2280 (let ((off erlang-indent-level
)
2282 (cond ((null (cdr stack
))) ; Top level in function.
2283 ((eq (car stack-top
) 'begin
)
2285 ((eq (car stack-top
) '-
>)
2286 (setq off
(* 2 erlang-indent-level
))))
2287 (let ((base (erlang-indent-find-base stack indent-point off skip
)))
2288 ;; Look at last thing to see how we are to move relative
2291 (cond ((looking-at "||\\|,\\|->\\|:-")
2293 ((erlang-at-keyword)
2294 (+ (current-column) erlang-indent-level
))
2295 ((or (= (char-syntax (following-char)) ?.
)
2296 (erlang-at-operator))
2297 (+ base erlang-indent-level
))
2299 (goto-char indent-point
)
2300 (cond ((memq (following-char) '(?\
( ?
{))
2301 ;; Function application or record.
2302 (+ (erlang-indent-find-preceding-expr)
2303 erlang-argument-indent
))
2304 ;; Empty line, or end; treat it as the end of
2305 ;; the block. (Here we have a choice: should
2306 ;; the user be forced to reindent continued
2307 ;; lines, or should the "end" be reindented?)
2308 ((looking-at "\\(end\\|after\\|catch\\)[^_a-zA-Z0-9]\\|$")
2309 (if (eq (car (car stack
)) '-
>)
2312 (erlang-caddr (car stack
))
2314 ;; Avoid treating comments a continued line.
2315 ((= (following-char) ?%
)
2317 ;; Continued line (e.g. line beginning
2318 ;; with an operator.)
2319 (t (+ base erlang-indent-level
)))))))
2321 ((eq (car stack-top
) 'when
)
2322 (goto-char (nth 1 stack-top
))
2323 (if (looking-at "when\\s *\\($\\|%\\)")
2326 (if (and stack
(eq (nth 0 (car stack
)) 'icr
))
2328 (goto-char (nth 1 (car stack
)))
2329 (+ (nth 2 (car stack
)) erlang-indent-guard
2330 ;; receive XYZ or receive
2332 (if (looking-at "[a-z]+\\s *\\($\\|%\\)")
2334 (* 2 erlang-indent-level
))))
2335 erlang-indent-guard
))
2336 ;; "when" is followed by code, let's indent to the same
2338 (forward-char 4) ; Skip "when"
2339 (skip-chars-forward " \t")
2340 (current-column))))))
2343 (defun erlang-indent-find-base (stack indent-point
&optional offset skip
)
2344 "Find the base column for current stack."
2345 (or skip
(setq skip
2))
2346 (or offset
(setq offset erlang-indent-level
))
2348 (let* ((stack-top (car stack
)))
2349 (goto-char (nth 1 stack-top
))
2351 (if (looking-at "\\s *\\($\\|%\\)")
2353 (if (memq (car stack-top
) '(-> ||
))
2355 ;; Take parent identation + offset,
2356 ;; else just erlang-indent-level if no parent
2358 (+ (erlang-caddr (car stack
))
2360 erlang-indent-level
))
2361 (erlang-skip-blank indent-point
)
2362 (current-column)))))
2365 ;; Does not handle `begin' .. `end'.
2366 (defun erlang-indent-find-preceding-expr ()
2367 "Return the first column of the preceding expression.
2368 This assumes that the preceding expression is either simple
2369 \(i.e. an atom) or parenthesized."
2372 (let ((col (current-column)))
2373 (skip-chars-backward " \t")
2374 ;; Needed to match the colon in "'foo':'bar'".
2375 (if (not (memq (preceding-char) '(?
# ?
:)))
2379 (current-column)))))
2382 (defun erlang-skip-blank (&optional lim
)
2383 "Skip over whitespace and comments until limit reached."
2384 (or lim
(setq lim
(point-max)))
2386 (while (and (not stop
) (< (point) lim
))
2387 (cond ((= (following-char) ?%
)
2388 (skip-chars-forward "^\n" lim
))
2389 ((= (following-char) ?
\n)
2390 (skip-chars-forward "\n" lim
))
2391 ((looking-at "\\s ")
2392 (if (re-search-forward "\\S " lim
'move
)
2398 (defun erlang-at-keyword ()
2399 "Are we looking at an Erlang keyword which will increase indentation?"
2400 (looking-at (concat "\\(when\\|if\\|fun\\|case\\|begin\\|query\\|"
2401 "of\\|receive\\|after\\|catch\\)[^_a-zA-Z0-9]")))
2403 (defun erlang-at-operator ()
2404 "Are we looking at an Erlang operator?"
2406 "\\(bnot\\|div\\|mod\\|band\\|bor\\|bxor\\|bsl\\|bsr\\)[^_a-zA-Z0-9]"))
2408 (defun erlang-comment-indent ()
2409 "Compute Erlang comment indentation.
2411 Used both by `indent-for-comment' and the Erlang specific indentation
2413 (cond ((looking-at "%%%") 0)
2415 (or (erlang-calculate-indent)
2416 (current-indentation)))
2419 (skip-chars-backward " \t")
2420 (max (if (bolp) 0 (1+ (current-column)))
2423 ;;; Erlang movement commands
2425 ;; All commands below work as movement commands. I.e. if the point is
2426 ;; at the end of the clause, and the command `erlang-end-of-clause' is
2427 ;; executed, the point is moved to the end of the NEXT clause. (This
2428 ;; mimics the behaviour of `end-of-defun'.)
2430 ;; Personally I would like to rewrite them to be "pure", and add a set
2431 ;; of movement functions, like `erlang-next-clause',
2432 ;; `erlang-previous-clause', and the same for functions.
2434 ;; The current implementation makes it hopeless to use the functions as
2435 ;; subroutines in more complex commands. /andersl
2437 (defun erlang-beginning-of-clause (&optional arg
)
2438 "Move backward to previous start of clause.
2439 With argument, do this that many times.
2440 Return t unless search stops due to end of buffer."
2442 (or arg
(setq arg
1))
2444 ;; Step back to the end of the previous line, unless we are at
2445 ;; the beginning of the buffer. The reason for this move is
2446 ;; that the regexp below includes the last character of the
2449 (or (looking-at "\n")
2452 (if (looking-at "\\`\n")
2454 ;; The regexp matches a function header that isn't
2455 ;; included in a string.
2456 (and (re-search-forward "\\(\\`\\|\\`\n\\|[^\\]\n\\)\\([a-z]\\|'\\|-\\)"
2458 (let ((beg (match-beginning 2)))
2459 (and beg
(goto-char beg
))
2462 (defun erlang-end-of-clause (&optional arg
)
2463 "Move to the end of the current clause.
2464 With argument, do this that many times."
2466 (or arg
(setq arg
1))
2467 (while (and (looking-at "[ \t]*[%\n]")
2468 (zerop (forward-line 1))))
2469 ;; Move to the next clause.
2470 (erlang-beginning-of-clause (- arg
))
2471 (beginning-of-line);; Just to be sure...
2473 (while (and (not (bobp)) continue
)
2475 (skip-chars-forward " \t")
2476 (if (looking-at "[%\n]")
2479 (setq continue nil
)))))
2481 (defun erlang-mark-clause ()
2482 "Put mark at end of clause, point at beginning."
2485 (erlang-end-of-clause 1)
2486 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2489 (push-mark (point) nil t
)
2490 (error (push-mark (point))))
2491 (erlang-beginning-of-clause 1)
2492 ;; The above function deactivates the mark.
2493 (if (boundp 'deactivate-mark
)
2494 (funcall (symbol-function 'set
) 'deactivate-mark nil
)))
2496 (defun erlang-beginning-of-function (&optional arg
)
2497 "Move backward to previous start of function.
2498 With positive argument, do this that many times.
2499 With negative argument, search forward.
2501 Return t unless search stops due to end of buffer."
2503 (or arg
(setq arg
1))
2507 (while (and (> arg
0)
2508 (and (erlang-beginning-of-clause 1)
2509 (let ((start (point))
2510 (name (erlang-name-of-function))
2511 (arity (erlang-get-function-arity)))
2512 ;; Note: "arity" is nil for e.g. "-import", hence
2513 ;; two "-import" clauses are not considered to
2514 ;; be part of the same function.
2515 (while (and (erlang-beginning-of-clause 1)
2517 (erlang-name-of-function))
2520 (erlang-get-function-arity)))
2521 (setq start
(point)))
2524 (setq arg
(1- arg
))))
2528 (erlang-beginning-of-clause 1)
2529 ;; Step -arg functions forward.
2530 (while (and (< arg
0)
2531 ;; Step one function forward, or stop if the end of
2532 ;; the buffer was reached. Return t if we found the
2534 (let ((name (erlang-name-of-function))
2535 (arity (erlang-get-function-arity))
2536 (found (erlang-beginning-of-clause -
1)))
2538 (string-equal name
(erlang-name-of-function))
2541 (erlang-get-function-arity)))
2542 (setq found
(erlang-beginning-of-clause -
1)))
2544 (setq arg
(1+ arg
)))))
2548 (defun erlang-end-of-function (&optional arg
)
2549 "Move forward to next end of function.
2551 With argument, do this that many times.
2552 With negative argument go towards the beginning of the buffer."
2554 (or arg
(setq arg
1))
2557 (while (and (> arg
0) (< (point) (point-max)))
2558 (let ((pos (point)))
2563 (erlang-beginning-of-clause 1)))
2565 (or (bobp) (forward-char -
1))
2566 (erlang-beginning-of-clause -
1))
2568 (erlang-pass-over-function)
2569 (skip-chars-forward " \t")
2570 (if (looking-at "[%\n]")
2573 (setq arg
(1- arg
)))
2576 (let ((pos (point)))
2577 (erlang-beginning-of-clause 1)
2578 (erlang-pass-over-function)
2580 (if (>= (point) pos
)
2581 (if (erlang-beginning-of-function 2)
2583 (erlang-pass-over-function)
2584 (skip-chars-forward " \t")
2585 (if (looking-at "[%\n]")
2587 (goto-char (point-min)))))
2588 (setq arg
(1+ arg
)))))
2591 (if (default-boundp 'beginning-of-defun-function
)
2592 (defalias 'erlang-mark-function
'mark-defun
)
2593 (defun erlang-mark-function ()
2594 "Put mark at end of function, point at beginning."
2597 (erlang-end-of-function 1)
2598 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2601 (push-mark (point) nil t
)
2602 (error (push-mark (point))))
2603 (erlang-beginning-of-function 1)
2604 ;; The above function deactivates the mark.
2605 (if (boundp 'deactivate-mark
)
2606 (funcall (symbol-function 'set
) 'deactivate-mark nil
)))))
2608 (defun erlang-pass-over-function ()
2611 (and (not (looking-at "\\.\\(\\s \\|\n\\|\\s<\\)"))
2617 (defun erlang-name-of-function ()
2619 ;; Skip over attribute leader.
2620 (if (looking-at "-[ \t]*")
2621 (re-search-forward "-[ \t]*" nil
'move
))
2622 (let ((start (point)))
2624 (buffer-substring start
(point)))))
2629 (defun erlang-fill-paragraph (&optional justify
)
2630 "Like \\[fill-paragraph], but handle Erlang comments.
2631 If any of the current line is a comment, fill the comment or the
2632 paragraph of it that point is in, preserving the comment's indentation
2635 (let ((has-comment nil
)
2636 ;; If has-comment, the appropriate fill-prefix for the comment.
2637 comment-fill-prefix
)
2638 ;; Figure out what kind of comment we are looking at.
2642 ;; Find the command prefix.
2643 ((looking-at (concat "\\s *" comment-start-skip
))
2644 (setq has-comment t
)
2645 (setq comment-fill-prefix
(buffer-substring (match-beginning 0)
2647 ;; A line with some code, followed by a comment? Remember that the
2648 ;; % which starts the comment shouldn't be part of a string or
2651 (while (not (looking-at "%\\|$"))
2652 (skip-chars-forward "^%\n\"\\\\")
2654 ((eq (char-after (point)) ?
\\) (forward-char 2))
2655 ((eq (char-after (point)) ?
\") (forward-sexp 1))))
2656 (looking-at comment-start-skip
))
2657 (setq has-comment t
)
2658 (setq comment-fill-prefix
2659 (concat (make-string (current-column) ?
)
2660 (buffer-substring (match-beginning 0) (match-end 0)))))))
2661 (if (not has-comment
)
2662 (fill-paragraph justify
)
2663 ;; Narrow to include only the comment, and then fill the region.
2666 ;; Find the first line we should include in the region to fill.
2668 (while (and (zerop (forward-line -
1))
2669 (looking-at "^\\s *%")))
2670 ;; We may have gone to far. Go forward again.
2671 (or (looking-at "^\\s *%")
2674 ;; Find the beginning of the first line past the region to fill.
2676 (while (progn (forward-line 1)
2677 (looking-at "^\\s *%")))
2679 ;; Lines with only % on them can be paragraph boundaries.
2680 (let ((paragraph-start (concat paragraph-start
"\\|^[ \t%]*$"))
2681 (paragraph-separate (concat paragraph-start
"\\|^[ \t%]*$"))
2682 (fill-prefix comment-fill-prefix
))
2683 (fill-paragraph justify
))))))
2686 (defun erlang-uncomment-region (beg end
)
2687 "Uncomment all commented lines in the region."
2689 (comment-region beg end -
1))
2692 (defun erlang-generate-new-clause ()
2693 "Create additional Erlang clause header.
2695 Parses the source file for the name of the current Erlang function.
2696 Create the header containing the name, A pair of parentheses,
2697 and an arrow. The space between the function name and the
2698 first parenthesis is preserved. The point is placed between
2701 (let ((name (save-excursion
2702 (and (erlang-beginning-of-clause)
2703 (erlang-get-function-name t
))))
2704 (arrow (save-excursion
2705 (and (erlang-beginning-of-clause)
2706 (erlang-get-function-arrow)))))
2707 (if (or (null arrow
) (null name
))
2708 (error "Can't find name of current Erlang function"))
2709 (if (and (bolp) (eolp))
2715 (insert ") " arrow
))
2716 (if erlang-new-clause-with-arguments
2717 (erlang-clone-arguments))))
2720 (defun erlang-clone-arguments ()
2721 "Insert, at the point, the argument list of the previous clause.
2723 The mark is set at the beginning of the inserted text, the point
2726 (let ((args (save-excursion
2728 (and (erlang-beginning-of-clause)
2729 (erlang-get-function-arguments))))
2732 (error "Can't clone argument list"))
2736 ;;; Information retrieval functions.
2738 (defun erlang-buffer-substring (beg end
)
2739 "Like `buffer-substring-no-properties'.
2740 Although, this function works on all versions of Emacs."
2741 (if (fboundp 'buffer-substring-no-properties
)
2742 (funcall (symbol-function 'buffer-substring-no-properties
) beg end
)
2743 (buffer-substring beg end
)))
2746 (defun erlang-get-module ()
2747 "Return the name of the module as specified by `-module'.
2749 Return nil if file contains no `-module' attribute."
2753 (goto-char (point-min))
2754 (let ((md (match-data)))
2756 (if (re-search-forward
2758 (concat "^-module\\s *(\\s *\\(\\("
2760 "\\)?\\)\\s *)\\s *\\."))
2762 (erlang-remove-quotes
2763 (erlang-buffer-substring (match-beginning 1)
2766 (store-match-data md
))))))
2769 (defun erlang-get-module-from-file-name (&optional file
)
2770 "Extract the module name from a file name.
2772 First, the directory part is removed. Second, the part of the file name
2773 matching `erlang-file-name-extension-regexp' is removed.
2775 Should the match fail, nil is returned.
2777 By modifying `erlang-file-name-extension-regexp' to match files other
2778 than Erlang source files, Erlang specific functions could be applied on
2779 non-Erlang files. Most notably; the support for Erlang modules in the
2780 tags system could be used by files written in other languages."
2781 (or file
(setq file buffer-file-name
))
2784 (setq file
(file-name-nondirectory file
))
2785 (if (string-match erlang-file-name-extension-regexp file
)
2786 (substring file
0 (match-beginning 0))
2790 ;; Used by `erlang-get-export' and `erlang-get-import'.
2792 (defun erlang-get-function-arity-list ()
2793 "Parse list of `function/arity' as used by `-import' and `-export'.
2795 Point must be before the opening bracket. When the
2796 function returns the point will be placed after the closing bracket.
2798 The function does not return an error if the list is incorrectly
2801 Return list of (function . arity). The order of the returned list
2802 corresponds to the order of the parsed Erlang list."
2806 (if (not (eq (preceding-char) ?\
[))
2807 '() ; Not looking at an Erlang list.
2808 (while ; Note: `while' has no body.
2811 (and (looking-at (eval-when-compile
2812 (concat erlang-atom-regexp
"/\\([0-9]+\\)\\>")))
2816 (erlang-remove-quotes
2817 (erlang-buffer-substring
2818 (match-beginning 1) (match-end 1)))
2820 (erlang-buffer-substring
2822 (+ 1 erlang-atom-regexp-matches
))
2824 (+ 1 erlang-atom-regexp-matches
)))))
2826 (goto-char (match-end 0))
2829 ;; Test if there are more exported functions.
2830 (eq (preceding-char) ?
,))))))
2834 ;;; Note that `-export' and the open parenthesis must be written on
2837 (defun erlang-get-export ()
2838 "Return a list of `(function . arity)' as specified by `-export'."
2840 (goto-char (point-min))
2841 (let ((md (match-data))
2845 (while (re-search-forward "^-export\\s *(" (point-max) t
)
2847 (setq res
(nconc res
(erlang-get-function-arity-list))))
2849 (store-match-data md
)))))
2852 (defun erlang-get-import ()
2853 "Parse an Erlang source file for imported functions.
2855 Return an alist with module name as car part and list of conses containing
2856 function and arity as cdr part."
2858 (goto-char (point-min))
2859 (let ((md (match-data))
2863 (while (re-search-forward "^-import\\s *(" (point-max) t
)
2865 (if (looking-at erlang-atom-regexp
)
2866 (let ((module (erlang-remove-quotes
2867 (erlang-buffer-substring
2870 (goto-char (match-end 0))
2872 (if (eq (following-char) ?
,)
2876 (let ((funcs (erlang-get-function-arity-list))
2877 (pair (assoc module res
)))
2879 (setcdr pair
(nconc (cdr pair
) funcs
))
2880 (setq res
(cons (cons module funcs
)
2883 (store-match-data md
)))))
2886 (defun erlang-get-function-name (&optional arg
)
2887 "Return name of current function, or nil.
2889 If optional argument is non-nil, everything up to and including
2890 the first `(' is returned.
2892 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2894 (if (not (eobp)) (forward-char 1))
2895 (and (erlang-beginning-of-clause)
2896 (erlang-get-function-name t)))"
2897 (let ((n (if arg
0 1)))
2898 (and (looking-at (eval-when-compile
2899 (concat "^" erlang-atom-regexp
"\\s *(")))
2900 (erlang-buffer-substring (match-beginning n
) (match-end n
)))))
2903 (defun erlang-get-function-arrow ()
2904 "Return arrow of current function, could be \"->\", \":-\" or nil.
2906 The \":-\" arrow is used by mnesia queries.
2908 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2910 (if (not (eobp)) (forward-char 1))
2911 (and (erlang-beginning-of-clause)
2912 (erlang-get-function-arrow)))"
2915 (re-search-forward "[^-:]*-\\|:" (point-max) t
)
2916 (erlang-buffer-substring (- (point) 1) (+ (point) 1)))))
2918 (defun erlang-get-function-arity ()
2919 "Return the number of arguments of function at point, or nil."
2920 (and (looking-at (eval-when-compile
2921 (concat "^" erlang-atom-regexp
"\\s *(")))
2923 (goto-char (match-end 0))
2931 ((looking-at "\\s *)")
2933 ((looking-at "\\s *\\($\\|%\\)")
2935 ((looking-at "\\s *,")
2936 (setq res
(+ 1 res
))
2937 (goto-char (match-end 0)))
2940 (setq res
(+ 1 res
)))
2945 (defun erlang-get-function-arguments ()
2946 "Return arguments of current function, or nil."
2947 (if (not (looking-at (eval-when-compile
2948 (concat "^" erlang-atom-regexp
"\\s *("))))
2952 (let ((start (match-end 0)))
2953 (goto-char (- start
1))
2955 (erlang-buffer-substring start
(- (point) 1)))
2959 (defun erlang-get-function-under-point ()
2960 "Return the module and function under the point, or nil.
2962 Should no explicit module name be present at the point, the
2963 list of imported functions is searched.
2965 The following could be returned:
2966 (\"module\" \"function\") -- Both module and function name found.
2967 (nil \"function\") -- No module name was found.
2968 nil -- No function name found
2970 In the future the list may contain more elements."
2972 (let ((md (match-data))
2974 (if (eq (char-syntax (following-char)) ?
)
2975 (skip-chars-backward " \t"))
2976 (skip-chars-backward "a-zA-Z0-9_:'")
2977 (cond ((looking-at (eval-when-compile
2978 (concat erlang-atom-regexp
":" erlang-atom-regexp
)))
2980 (erlang-remove-quotes
2981 (erlang-buffer-substring
2982 (match-beginning 1) (match-end 1)))
2983 (erlang-remove-quotes
2984 (erlang-buffer-substring
2985 (match-beginning (1+ erlang-atom-regexp-matches
))
2986 (match-end (1+ erlang-atom-regexp-matches
)))))))
2987 ((looking-at erlang-atom-regexp
)
2988 (let ((fk (erlang-remove-quotes
2989 (erlang-buffer-substring
2990 (match-beginning 0) (match-end 0))))
2992 (imports (erlang-get-import)))
2993 (while (and imports
(null mod
))
2994 (if (assoc fk
(cdr (car imports
)))
2995 (setq mod
(car (car imports
)))
2996 (setq imports
(cdr imports
))))
2997 (setq res
(list mod fk
)))))
2998 (store-match-data md
)
3002 ;; TODO: Escape single quotes inside the string without
3003 ;; replace-regexp-in-string.
3004 (defun erlang-add-quotes-if-needed (str)
3005 "Return STR, possibly with quotes."
3006 (if (and (stringp str
)
3007 (not (string-match (eval-when-compile
3008 (concat "\\`" erlang-atom-regexp
"\\'")) str
)))
3009 (progn (if (fboundp 'replace-regexp-in-string
)
3010 (setq str
(replace-regexp-in-string "'" "\\'" str t t
)))
3011 (concat "'" str
"'"))
3015 (defun erlang-remove-quotes (str)
3016 "Return STR without quotes, if present."
3017 (let ((md (match-data)))
3019 (if (string-match "\\`'\\(.*\\)'\\'" str
)
3020 (substring str
1 -
1)
3022 (store-match-data md
))))
3025 ;;; Check module name
3027 ;; The function `write-file', bound to C-x C-w, calls
3028 ;; `set-visited-file-name' which clears the hook. :-(
3029 ;; To make sure that the hook always is present, we advise
3030 ;; `set-visited-file-name'.
3031 (defun erlang-check-module-name-init ()
3032 "Initialize the functionality to compare file and module names.
3034 Unless we have `before-save-hook', we redefine the function
3035 `set-visited-file-name' since it clears the variable
3036 `local-write-file-hooks'. The original function definition is
3037 stored in `erlang-orig-set-visited-file-name'."
3038 (if (boundp 'before-save-hook
)
3039 ;; If we have that, `make-local-hook' is obsolete.
3040 (add-hook 'before-save-hook
'erlang-check-module-name nil t
)
3042 (unless (ad-advised-definition-p 'set-visited-file-name
)
3043 (defadvice set-visited-file-name
(after erlang-set-visited-file-name
3045 (if (eq major-mode
'erlang-mode
)
3046 (add-hook 'local-write-file-hooks
'erlang-check-module-name
))))
3047 (add-hook 'local-write-file-hooks
'erlang-check-module-name
)))
3050 (defun erlang-check-module-name ()
3051 "If the module name doesn't match file name, ask for permission to change.
3053 The variable `erlang-check-module-name' controls the behaviour of this
3054 function. It it is nil, this function does nothing. If it is t, the
3055 source is silently changed. If it is set to the atom `ask', the user
3058 This function is normally placed in the hook `local-write-file-hooks'."
3059 (if erlang-check-module-name
3060 (let ((mn (erlang-get-module))
3061 (fn (erlang-get-module-from-file-name (buffer-file-name))))
3062 (if (and (stringp mn
) (stringp fn
))
3063 (or (string-equal mn fn
)
3064 (if (or (eq erlang-check-module-name t
)
3066 "Module does not match file name. Modify source? "))
3070 (goto-char (point-min))
3071 (if (re-search-forward
3073 (concat "^-module\\s *(\\s *\\(\\("
3075 "\\)?\\)\\s *)\\s *\\."))
3078 (goto-char (match-beginning 1))
3079 (delete-region (match-beginning 1)
3081 (insert fn
))))))))))
3082 ;; Must return nil since it is added to `local-write-file-hook'.
3086 ;;; Electric functions.
3088 (defun erlang-electric-semicolon (&optional arg
)
3089 "Insert a semicolon character and possibly a prototype for the next line.
3091 The variable `erlang-electric-semicolon-criteria' states a criterion,
3092 when fulfilled a newline is inserted, the next line is indented and a
3093 prototype for the next line is inserted. Normally the prototype
3094 consists of \" ->\". Should the semicolon end the clause a new clause
3095 header is generated.
3097 The variable `erlang-electric-semicolon-insert-blank-lines' controls
3098 the number of blank lines inserted between the current line and new
3101 Behaves just like the normal semicolon when supplied with a
3102 numerical arg, point is inside string or comment, or when there are
3103 non-whitespace characters following the point on the current line."
3105 (self-insert-command (prefix-numeric-value arg
))
3107 (and (listp erlang-electric-commands
)
3108 (not (memq 'erlang-electric-semicolon
3109 erlang-electric-commands
)))
3111 (not (looking-at "\\s *\\(%.*\\)?$"))
3112 (null (erlang-test-criteria-list
3113 erlang-electric-semicolon-criteria
)))
3114 (setq erlang-electric-newline-inhibit nil
)
3115 (setq erlang-electric-newline-inhibit t
)
3119 (if (condition-case nil
3120 (progn (erlang-indent-line) t
)
3121 (error (if (bolp) (delete-backward-char 1))))
3127 (erlang-generate-new-clause)
3128 (if erlang-electric-semicolon-insert-blank-lines
3132 erlang-electric-semicolon-insert-blank-lines
))))
3133 (error (if (bolp) (delete-backward-char 1))))))))
3136 (defun erlang-electric-comma (&optional arg
)
3137 "Insert a comma character and possibly a new indented line.
3138 The variable `erlang-electric-comma-criteria' states a criterion,
3139 when fulfilled a newline is inserted and the next line is indented.
3141 Behaves just like the normal comma when supplied with a
3142 numerical arg, point is inside string or comment, or when there are
3143 non-whitespace characters following the point on the current line."
3146 (self-insert-command (prefix-numeric-value arg
))
3149 (and (listp erlang-electric-commands
)
3150 (not (memq 'erlang-electric-comma erlang-electric-commands
)))
3152 (not (looking-at "\\s *\\(%.*\\)?$"))
3153 (null (erlang-test-criteria-list
3154 erlang-electric-comma-criteria
)))
3155 (setq erlang-electric-newline-inhibit nil
)
3156 (setq erlang-electric-newline-inhibit t
)
3161 (erlang-indent-line)
3162 (error (if (bolp) (delete-backward-char 1))))))
3164 (defun erlang-electric-lt (&optional arg
)
3165 "Insert a less-than sign, and optionally mark it as an open paren."
3169 (self-insert-command arg
)
3171 ;; Was this the second char in bit-syntax open (`<<')?
3172 (unless (< (point) 2)
3175 (when (and (eq (char-after (point)) ?
<)
3176 (not (eq (get-text-property (point) 'category
)
3177 'bitsyntax-open-inner
)))
3178 ;; Then mark the two chars...
3179 (put-text-property (point) (1+ (point))
3180 'category
'bitsyntax-open-outer
)
3182 (put-text-property (point) (1+ (point))
3183 'category
'bitsyntax-open-inner
)
3184 ;;...and unmark any subsequent less-than chars.
3186 (while (eq (char-after (point)) ?
<)
3187 (remove-text-properties (point) (1+ (point))
3189 (forward-char 1))))))
3191 (defun erlang-after-bitsyntax-close ()
3192 "Return t if point is immediately after a bit-syntax close parenthesis (`>>')."
3196 (and (eq (char-after (point)) ?
>)
3197 (not (eq (get-text-property (point) 'category
)
3198 'bitsyntax-close-outer
))))))
3200 (defun erlang-after-arrow ()
3201 "Return true if point is immediately after a function arrow (`->')."
3206 (eq (char-before (point)) ?-
))
3207 (or (not (listp erlang-electric-commands
))
3208 (memq 'erlang-electric-gt
3209 erlang-electric-commands
))
3210 (not (erlang-in-literal))
3211 (looking-at "\\s *\\(%.*\\)?$")
3212 (erlang-test-criteria-list erlang-electric-arrow-criteria
))))
3215 (defun erlang-electric-gt (&optional arg
)
3216 "Insert a greater-than sign, and optionally mark it as a close paren."
3220 (self-insert-command arg
)
3223 ;; Did we just write a bit-syntax close (`>>')?
3224 ((erlang-after-bitsyntax-close)
3226 ;; Then mark the two chars...
3228 (put-text-property (point) (1+ (point))
3229 'category
'bitsyntax-close-inner
)
3231 (put-text-property (point) (1+ (point))
3232 'category
'bitsyntax-close-outer
)
3233 ;;...and unmark any subsequent greater-than chars.
3235 (while (eq (char-after (point)) ?
>)
3236 (remove-text-properties (point) (1+ (point))
3240 ;; Did we just write a function arrow (`->')?
3241 ((erlang-after-arrow)
3242 (let ((erlang-electric-newline-inhibit t
))
3247 (erlang-indent-line)
3248 (error (if (bolp) (delete-backward-char 1))))))
3250 ;; Then it's just a plain greater-than.
3255 (defun erlang-electric-arrow\ off
(&optional arg
)
3256 "Insert a '>'-sign and possibly a new indented line.
3258 This command is only `electric' when the `>' is part of an `->' arrow.
3259 The variable `erlang-electric-arrow-criteria' states a sequence of
3260 criteria, which decides when a newline should be inserted and the next
3263 It behaves just like the normal greater than sign when supplied with a
3264 numerical arg, point is inside string or comment, or when there are
3265 non-whitespace characters following the point on the current line.
3267 After being split/merged into `erlang-after-arrow' and
3268 `erlang-electric-gt', it is now unused and disabled."
3270 (let ((prec (preceding-char)))
3271 (self-insert-command (prefix-numeric-value arg
))
3273 (and (listp erlang-electric-commands
)
3274 (not (memq 'erlang-electric-arrow
3275 erlang-electric-commands
)))
3278 (not (looking-at "\\s *\\(%.*\\)?$"))
3279 (null (erlang-test-criteria-list
3280 erlang-electric-arrow-criteria
)))
3281 (setq erlang-electric-newline-inhibit nil
)
3282 (setq erlang-electric-newline-inhibit t
)
3287 (erlang-indent-line)
3288 (error (if (bolp) (delete-backward-char 1)))))))
3291 (defun erlang-electric-newline (&optional arg
)
3292 "Break line at point and indent, continuing comment if within one.
3293 The variable `erlang-electric-newline-criteria' states a criterion,
3294 when fulfilled a newline is inserted and the next line is indented.
3296 Should the current line begin with a comment, and the variable
3297 `comment-multi-line' be non-nil, a new comment start is inserted.
3299 Should the previous command be another electric command we assume that
3300 the user pressed newline out of old habit, hence we will do nothing."
3302 (cond ((and (not arg
)
3303 erlang-electric-newline-inhibit
3304 (memq last-command erlang-electric-newline-inhibit-list
))
3307 (and (listp erlang-electric-commands
)
3308 (not (memq 'erlang-electric-newline
3309 erlang-electric-commands
)))
3310 (null (erlang-test-criteria-list
3311 erlang-electric-newline-criteria
)))
3312 (newline (prefix-numeric-value arg
)))
3314 (if (and comment-multi-line
3317 (looking-at (concat "\\s *" comment-start-skip
))))
3318 (let ((str (buffer-substring
3319 (or (match-end 1) (match-beginning 0))
3320 (min (match-end 0) (point)))))
3326 (indent-according-to-mode)))))
3329 (defun erlang-test-criteria-list (criteria)
3330 "Given a list of criterion functions, test if criteria are fulfilled.
3332 Each element in the criteria list can a function returning nil, t or
3333 the atom `stop'. t means that the criterion is fulfilled, `stop' means
3334 that it isn't fulfilled and that the search should stop,
3335 and nil means continue searching.
3337 Should the list contain the atom t the criterion is assumed to be
3338 fulfilled, unless preceded by a function returning `stop', of course.
3340 Should the argument be the atom t instead of a list, the criterion is
3341 assumed to be trivially true.
3343 Should all functions return nil, the criteria are assumed not to be
3346 Return t if criteria fulfilled, nil otherwise."
3351 (while (and criteria
(null answer
))
3352 (if (eq (car criteria
) t
)
3354 (setq answer
(funcall (car criteria
))))
3355 (setq criteria
(cdr criteria
)))
3356 (if (and answer
(not (eq answer
'stop
)))
3361 (defun erlang-in-literal (&optional lim
)
3362 "Test if point is in string, quoted atom or comment.
3364 Return one of the three atoms `atom', `string', and `comment'.
3365 Should the point be inside none of the above mentioned types of
3366 context, nil is returned."
3368 (let* ((lim (or lim
(save-excursion
3369 (erlang-beginning-of-clause)
3371 (state (if (fboundp 'syntax-ppss
) ; post Emacs 21.3
3373 (parse-partial-sexp lim
(point)))))
3375 ((eq (nth 3 state
) ?
') 'atom
)
3376 ((nth 3 state
) 'string
)
3377 ((nth 4 state
) 'comment
)
3381 (defun erlang-at-end-of-function-p ()
3382 "Test if point is at end of an Erlang function.
3384 This function is designed to be a member of a criteria list."
3385 (eq (save-excursion (erlang-skip-blank) (point))
3387 (erlang-beginning-of-function -
1) (point))))
3390 (defun erlang-stop-when-inside-argument-list ()
3391 "Return `stop' if inside parenthesis list, nil otherwise.
3393 Knows about the list comprehension syntax. When the point is
3394 after `||', `stop' is not returned.
3396 This function is designed to be a member of a criteria list."
3399 (let ((orig-point (point))
3402 (if (not (eq (following-char) ?\
[))
3404 ;; Do not return `stop' when inside a list comprehension
3405 ;; construction. (The point must be after `||').
3406 (while (< (point) orig-point
)
3407 (setq state
(erlang-partial-parse (point) orig-point state
)))
3408 (if (and (car state
) (eq (car (car (car state
))) '||
))
3415 (defun erlang-stop-when-at-guard ()
3416 "Return `stop' when at function guards.
3418 This function is designed to be a member of a criteria list."
3421 (if (and (looking-at (eval-when-compile
3422 (concat "^" erlang-atom-regexp
"\\s *(")))
3425 (concat "^" erlang-atom-regexp
".*\\(->\\|:-\\)")))))
3430 (defun erlang-next-lines-empty-p ()
3431 "Return non-nil if next lines are empty.
3433 The variable `erlang-next-lines-empty-threshold' contains the number
3434 of lines required to be empty.
3436 A line containing only spaces and tabs is considered empty.
3438 This function is designed to be a member of a criteria list."
3439 (and erlang-next-lines-empty-threshold
3441 (let ((left erlang-next-lines-empty-threshold
)
3443 (while (and cont
(> left
0))
3445 (setq cont
(looking-at "\\s *$"))
3446 (setq left
(- left
1)))
3450 (defun erlang-at-keyword-end-p ()
3451 "Test if next readable token is the keyword end.
3453 This function is designed to be a member of a criteria list."
3456 (looking-at "end[^_a-zA-Z0-9]")))
3459 ;; Erlang tags support which is aware of erlang modules.
3461 ;; Not yet implemented under XEmacs. (Hint: The Emacs 19 etags
3462 ;; package works under XEmacs.)
3465 (if (or (featurep 'bytecomp
)
3466 (featurep 'byte-compile
))
3473 (defvar erlang-tags-function-alist
3474 '((find-tag . erlang-find-tag
)
3475 (find-tag-other-window . erlang-find-tag-other-window
)
3476 (find-tag-regexp . erlang-find-tag-regexp
)
3477 (find-tag-other-frame . erlang-find-tag-other-frame
))
3478 "Alist of old tags commands and the replacement functions.")
3480 (defvar erlang-tags-installed nil
3481 "Non-nil when the Erlang tags system is installed.")
3482 (defvar erlang-tags-file-list
'()
3483 "List of files in tag list. Used when finding tag on form `module:'.")
3484 (defvar erlang-tags-completion-table nil
3485 "Like `tags-completion-table', this table contains `tag' and `module:tag'.")
3486 (defvar erlang-tags-buffer-installed-p nil
3487 "Non-nil when Erlang module recognising functions installed.")
3488 (defvar erlang-tags-buffer-list
'()
3489 "Temporary list of buffers.")
3490 (defvar erlang-tags-orig-completion-table nil
3491 "Temporary storage for `tags-completion-table'.")
3492 (defvar erlang-tags-orig-tag-order nil
3493 "Temporary storage for `find-tag-tag-order'.")
3494 (defvar erlang-tags-orig-regexp-tag-order nil
3495 "Temporary storage for `find-tag-regexp-tag-order'.")
3496 (defvar erlang-tags-orig-search-function nil
3497 "Temporary storage for `find-tag-search-function'.")
3498 (defvar erlang-tags-orig-regexp-search-function nil
3499 "Temporary storage for `find-tag-regexp-search-function'.")
3500 (defvar erlang-tags-orig-format-hooks nil
3501 "Temporary storage for `tags-table-format-hooks'.")
3503 (defun erlang-tags-init ()
3504 "Install an alternate version of tags, aware of Erlang modules.
3506 After calling this function, the tags functions are aware of
3507 Erlang modules. Tags can be entered on the for `module:tag' as well
3508 as on the old form `tag'.
3510 In the completion list, `module:tag' and `module:' shows up.
3512 Call this function from an appropriate init file, or add it to
3513 Erlang mode hook with the commands:
3514 (add-hook 'erlang-mode-hook 'erlang-tags-init)
3515 (add-hook 'erlang-shell-mode-hook 'erlang-tags-init)
3517 This function only works under Emacs 18 and Emacs 19. Currently, It
3518 is not implemented under XEmacs. (Hint: The Emacs 19 etags module
3519 works under XEmacs.)"
3521 (cond ((= erlang-emacs-major-version
18)
3523 (erlang-tags-define-keys (current-local-map))
3524 (setq erlang-tags-installed t
))
3527 ;; Test on a function available in the Emacs 19 version
3528 ;; of tags but not in the XEmacs version.
3529 (if (not (fboundp 'find-tag-noselect
))
3531 (erlang-tags-define-keys (current-local-map))
3532 (setq erlang-tags-installed t
)))))
3535 ;; Set all keys bound to `find-tag' et.al. in the global map and the
3536 ;; menu to `erlang-find-tag' et.al. in `map'.
3538 ;; The function `substitute-key-definition' does not work properly
3539 ;; in all version of Emacs.
3541 (defun erlang-tags-define-keys (map)
3542 "Bind tags commands to keymap MAP aware of Erlang modules."
3543 (let ((alist erlang-tags-function-alist
))
3545 (let* ((old (car (car alist
)))
3546 (new (cdr (car alist
)))
3547 (keys (append (where-is-internal old global-map
))))
3549 (define-key map
(car keys
) new
)
3550 (setq keys
(cdr keys
))))
3551 (setq alist
(cdr alist
))))
3553 (erlang-menu-substitute erlang-menu-base-items erlang-tags-function-alist
)
3557 ;; There exists a variable `find-tag-default-function'. It is not used
3558 ;; since `complete-tag' uses it to get current word under point. In that
3559 ;; situation we don't want the module to be prepended.
3561 (defun erlang-find-tag-default ()
3562 "Return the default tag.
3563 Search `-import' list of imported functions.
3564 Single quotes are been stripped away."
3565 (let ((mod-func (erlang-get-function-under-point)))
3566 (cond ((null mod-func
)
3568 ((null (car mod-func
))
3571 (concat (car mod-func
) ":" (nth 1 mod-func
))))))
3574 ;; Return `t' since it is used inside `tags-loop-form'.
3576 (defun erlang-find-tag (modtagname &optional next-p regexp-p
)
3577 "Like `find-tag'. Capable of retrieving Erlang modules.
3579 Tags can be given on the forms `tag', `module:', `module:tag'."
3580 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3581 (switch-to-buffer (erlang-find-tag-noselect modtagname next-p regexp-p
))
3585 ;; Code mainly from `find-tag-other-window' in `etags.el'.
3587 (defun erlang-find-tag-other-window (tagname &optional next-p regexp-p
)
3588 "Like `find-tag-other-window' but aware of Erlang modules."
3589 (interactive (erlang-tag-interactive
3590 "Find `module:tag' or `tag' other window: "))
3592 ;; This is to deal with the case where the tag is found in the
3593 ;; selected window's buffer; without this, point is moved in both
3594 ;; windows. To prevent this, we save the selected window's point
3595 ;; before doing find-tag-noselect, and restore it afterwards.
3596 (let* ((window-point (window-point (selected-window)))
3597 (tagbuf (erlang-find-tag-noselect tagname next-p regexp-p
))
3598 (tagpoint (progn (set-buffer tagbuf
) (point))))
3599 (set-window-point (prog1
3601 (switch-to-buffer-other-window tagbuf
)
3602 ;; We have to set this new window's point; it
3603 ;; might already have been displaying a
3604 ;; different portion of tagbuf, in which case
3605 ;; switch-to-buffer-other-window doesn't set
3606 ;; the window's point from the buffer.
3607 (set-window-point (selected-window) tagpoint
))
3611 (defun erlang-find-tag-other-frame (tagname &optional next-p
)
3612 "Like `find-tag-other-frame' but aware of Erlang modules."
3613 (interactive (erlang-tag-interactive
3614 "Find `module:tag' or `tag' other frame: "))
3615 (let ((pop-up-frames t
))
3616 (erlang-find-tag-other-window tagname next-p
)))
3619 (defun erlang-find-tag-regexp (regexp &optional next-p other-window
)
3620 "Like `find-tag-regexp' but aware of Erlang modules."
3621 (interactive (if (fboundp 'find-tag-regexp
)
3622 (erlang-tag-interactive
3623 "Find `module:regexp' or `regexp': ")
3624 (error "This version of Emacs can't find tags by regexps")))
3625 (funcall (if other-window
3626 'erlang-find-tag-other-window
3631 ;; Just like C-u M-. This could be added to the menu.
3632 (defun erlang-find-next-tag ()
3633 "Find next tag, like \\[find-tag] with prefix arg."
3635 (let ((current-prefix-arg '(4)))
3636 (if erlang-tags-installed
3637 (call-interactively 'erlang-find-tag
)
3638 (call-interactively 'find-tag
))))
3641 ;; Mimics `find-tag-noselect' found in `etags.el', but uses `find-tag' to
3642 ;; be compatible with `tags.el'.
3644 ;; Handles three cases:
3645 ;; * `module:' Loop over all possible file names. Stop if a file-name
3646 ;; without extension and directory matches the module.
3649 ;; Emacs 19: Replace test functions with functions aware of
3650 ;; Erlang modules. Tricky because the etags system wasn't
3651 ;; built for these kind of operations...
3653 ;; Emacs 18: We loop over `find-tag' until we find a file
3654 ;; whose module matches the requested module. The
3655 ;; drawback is that a lot of files could be loaded into
3658 ;; * `tag' Just give it to `find-tag'.
3660 (defun erlang-find-tag-noselect (modtagname &optional next-p regexp-p
)
3661 "Like `find-tag-noselect' but aware of Erlang modules."
3662 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3664 (setq modtagname
(symbol-value 'last-tag
)))
3665 (funcall (symbol-function 'set
) 'last-tag modtagname
)
3666 ;; `tags.el' uses this variable to record how M-, would
3667 ;; know where to restart a tags command.
3668 (if (boundp 'tags-loop-form
)
3669 (funcall (symbol-function 'set
)
3670 'tags-loop-form
'(erlang-find-tag nil t
)))
3671 (save-window-excursion
3673 ((string-match ":$" modtagname
)
3674 ;; Only the module name was given. Read all files whose file name
3676 (let ((modname (substring modtagname
0 (match-beginning 0)))
3680 (visit-tags-table-buffer)
3681 (setq erlang-tags-file-list
3682 (funcall (symbol-function 'tags-table-files
)))))
3684 (or erlang-tags-file-list
3686 (if (and (featurep 'etags
)
3688 (symbol-function 'visit-tags-table-buffer
) 'same
)
3690 (symbol-function 'visit-tags-table-buffer
) t
))
3691 (setq erlang-tags-file-list
3692 (funcall (symbol-function 'tags-table-files
)))
3693 (error "No %stags containing %s" (if next-p
"more " "")
3695 (if erlang-tags-file-list
3696 (let ((this-module (erlang-get-module-from-file-name
3697 (car erlang-tags-file-list
))))
3698 (if (and (stringp this-module
)
3699 (string= modname this-module
))
3700 (setq file
(car erlang-tags-file-list
)))
3701 (setq erlang-tags-file-list
(cdr erlang-tags-file-list
)))))
3702 (set-buffer (or (get-file-buffer file
)
3703 (find-file-noselect file
)))))
3705 ((string-match ":" modtagname
)
3706 (if (boundp 'find-tag-tag-order
)
3707 ;; Method one: Add module-recognising functions to the
3708 ;; list of order functions. However, the tags system
3709 ;; from Emacs 18, and derives thereof (read: XEmacs)
3710 ;; hasn't got this feature.
3712 (erlang-tags-install-module-check)
3714 (funcall (symbol-function 'find-tag
)
3715 modtagname next-p regexp-p
)
3716 (erlang-tags-remove-module-check)))
3717 ;; Method two: Call the tags system until a file matching
3718 ;; the module is found. This could result in that many
3719 ;; files are read. (e.g. The tag "foo:file" will take a
3720 ;; while to process.)
3721 (let* ((modname (substring modtagname
0 (match-beginning 0)))
3722 (tagname (substring modtagname
(match-end 0) nil
))
3727 (funcall (symbol-function 'find-tag
) tagname next-p regexp-p
)
3729 ;; Determine the module form the file name. (The
3730 ;; alternative, to check `-module', would make this
3731 ;; code useless for non-Erlang programs.)
3732 (setq file
(erlang-get-module-from-file-name buffer-file-name
))
3733 (not (and (stringp file
)
3734 (string= modname file
))))))))
3736 (funcall (symbol-function 'find-tag
) modtagname next-p regexp-p
)))
3737 (current-buffer))) ; Return the new buffer.
3740 ;; Process interactive arguments for erlang-find-tag-*.
3742 ;; Negative arguments work only for `etags', not `tags'. This is not
3743 ;; a problem since negative arguments means step back into the
3744 ;; history list, a feature not implemented in `tags'.
3746 (defun erlang-tag-interactive (prompt)
3751 (if current-prefix-arg
3752 (list nil
(if (< (prefix-numeric-value current-prefix-arg
) 0)
3755 (let* ((default (erlang-find-tag-default))
3757 (format "%s(default %s) " prompt default
)
3759 (spec (if (featurep 'etags
)
3760 (completing-read prompt
'erlang-tags-complete-tag
)
3761 (read-string prompt
))))
3762 (list (if (equal spec
"")
3763 (or default
(error "There is no default tag"))
3767 ;; Search tag functions which are aware of Erlang modules. The tactic
3768 ;; is to store new search functions into the local variables of the
3769 ;; TAGS buffers. The variables are restored directly after the
3770 ;; search. The situation is complicated by the fact that new TAGS
3771 ;; files can be loaded during the search.
3773 ;; This code is Emacs 19 `etags' specific.
3775 (defun erlang-tags-install-module-check ()
3776 "Install our own tag search functions."
3777 ;; Make sure our functions are installed in TAGS files loaded
3778 ;; into Emacs while searching.
3779 ;; ?? tags-table-format-hooks isn't in Emacs 21 or XEmacs etags.
3780 (setq erlang-tags-orig-format-hooks
3781 (symbol-value 'tags-table-format-hooks
))
3782 (funcall (symbol-function 'set
) 'tags-table-format-hooks
3783 (cons 'erlang-tags-recognize-tags-table
3784 erlang-tags-orig-format-hooks
))
3785 (setq erlang-tags-buffer-list
'())
3786 ;; Install our functions in the TAGS files already resident.
3788 (let ((files (symbol-value 'tags-table-computed-list
)))
3790 (if (stringp (car files
))
3791 (if (get-file-buffer (car files
))
3793 (set-buffer (get-file-buffer (car files
)))
3794 (erlang-tags-install-local))))
3795 (setq files
(cdr files
))))))
3798 (defun erlang-tags-install-local ()
3799 "Install our tag search functions in current buffer."
3800 (if erlang-tags-buffer-installed-p
3802 ;; Mark this buffer as "installed" and record.
3803 (set (make-local-variable 'erlang-tags-buffer-installed-p
) t
)
3804 (setq erlang-tags-buffer-list
3805 (cons (current-buffer) erlang-tags-buffer-list
))
3807 ;; Save the original values.
3808 (set (make-local-variable 'erlang-tags-orig-tag-order
)
3809 (symbol-value 'find-tag-tag-order
))
3810 (set (make-local-variable 'erlang-tags-orig-regexp-tag-order
)
3811 (symbol-value 'find-tag-regexp-tag-order
))
3812 (set (make-local-variable 'erlang-tags-orig-search-function
)
3813 (symbol-value 'find-tag-search-function
))
3814 (set (make-local-variable 'erlang-tags-orig-regexp-search-function
)
3815 (symbol-value 'find-tag-regexp-search-function
))
3817 ;; Install our own functions.
3818 (set (make-local-variable 'find-tag-search-function
)
3819 'erlang-tags-search-forward
)
3820 (set (make-local-variable 'find-tag-regexp-search-function
)
3821 'erlang-tags-regexp-search-forward
)
3822 (set (make-local-variable 'find-tag-tag-order
)
3823 '(erlang-tag-match-module-p))
3824 (set (make-local-variable 'find-tag-regexp-tag-order
)
3825 '(erlang-tag-match-module-regexp-p))))
3828 (defun erlang-tags-remove-module-check ()
3829 "Remove our own tags search functions."
3830 (funcall (symbol-function 'set
)
3831 'tags-table-format-hooks
3832 erlang-tags-orig-format-hooks
)
3833 ;; Remove our functions from the TAGS files. (Note that
3834 ;; `tags-table-computed-list' need not be the same list as when
3835 ;; the search was started.)
3837 (let ((buffers erlang-tags-buffer-list
))
3839 (if (buffer-name (car buffers
))
3841 (set-buffer (car buffers
))
3842 (erlang-tags-remove-local)))
3843 (setq buffers
(cdr buffers
))))))
3846 (defun erlang-tags-remove-local ()
3847 "Remove our tag search functions from current buffer."
3848 (if (null erlang-tags-buffer-installed-p
)
3850 (funcall (symbol-function 'set
) 'erlang-tags-buffer-installed-p nil
)
3851 (funcall (symbol-function 'set
)
3852 'find-tag-tag-order erlang-tags-orig-tag-order
)
3853 (funcall (symbol-function 'set
)
3854 'find-tag-regexp-tag-order erlang-tags-orig-regexp-tag-order
)
3855 (funcall (symbol-function 'set
)
3856 'find-tag-search-function erlang-tags-orig-search-function
)
3857 (funcall (symbol-function 'set
)
3858 'find-tag-regexp-search-function
3859 erlang-tags-orig-regexp-search-function
)))
3862 (defun erlang-tags-recognize-tags-table ()
3863 "Install our functions in all loaded TAGS files.
3865 This function is added to `tags-table-format-hooks' when searching
3866 for a tag on the form `module:tag'."
3867 (if (null (funcall (symbol-function 'etags-recognize-tags-table
)))
3869 (erlang-tags-install-local)
3873 (defun erlang-tags-search-forward (tag &optional bound noerror count
)
3874 "Forward search function, aware of Erlang module prefix."
3875 (if (string-match ":" tag
)
3876 (setq tag
(substring tag
(match-end 0) nil
)))
3877 ;; Avoid unintended recursion.
3878 (if (eq erlang-tags-orig-search-function
'erlang-tags-search-forward
)
3879 (search-forward tag bound noerror count
)
3880 (funcall erlang-tags-orig-search-function tag bound noerror count
)))
3883 (defun erlang-tags-regexp-search-forward (tag &optional bound noerror count
)
3884 "Forward regexp search function, aware of Erlang module prefix."
3885 (if (string-match ":" tag
)
3886 (setq tag
(substring tag
(match-end 0) nil
)))
3887 (if (eq erlang-tags-orig-regexp-search-function
3888 'erlang-tags-regexp-search-forward
)
3889 (re-search-forward tag bound noerror count
)
3890 (funcall erlang-tags-orig-regexp-search-function
3891 tag bound noerror count
)))
3894 ;; t if point is at a tag line that matches TAG, containing
3895 ;; module information. Assumes that all other order functions
3896 ;; are stored in `erlang-tags-orig-[regex]-tag-order'.
3898 (defun erlang-tag-match-module-p (tag)
3899 (erlang-tag-match-module-common-p tag erlang-tags-orig-tag-order
))
3901 (defun erlang-tag-match-module-regexp-p (tag)
3902 (erlang-tag-match-module-common-p tag erlang-tags-orig-regexp-tag-order
))
3904 (defun erlang-tag-match-module-common-p (tag order
)
3907 (if (string-match ":" tag
)
3909 (setq mod
(substring tag
0 (match-beginning 0)))
3910 (setq tag
(substring tag
(match-end 0) nil
))))
3911 (while (and order
(not found
))
3913 (and (not (memq (car order
)
3914 '(erlang-tag-match-module-p
3915 erlang-tag-match-module-regexp-p
)))
3916 (funcall (car order
) tag
)))
3917 (setq order
(cdr order
)))
3920 (string= mod
(erlang-get-module-from-file-name
3924 ;;; Tags completion, Emacs 19 `etags' specific.
3926 ;;; The basic idea is to create a second completion table `erlang-tags-
3927 ;;; completion-table' containing all normal tags plus tags on the form
3931 (defun erlang-complete-tag ()
3932 "Perform tags completion on the text around point.
3933 Completes to the set of names listed in the current tags table.
3935 Should the Erlang tags system be installed this command knows
3936 about Erlang modules."
3941 (cond ((and erlang-tags-installed
3942 (fboundp 'complete-tag
)) ; Emacs 19
3943 (let ((orig-tags-complete-tag (symbol-function 'tags-complete-tag
)))
3944 (fset 'tags-complete-tag
3945 (symbol-function 'erlang-tags-complete-tag
))
3947 (funcall (symbol-function 'complete-tag
))
3948 (fset 'tags-complete-tag orig-tags-complete-tag
))))
3949 ((fboundp 'complete-tag
) ; Emacs 19
3950 (funcall (symbol-function 'complete-tag
)))
3951 ((fboundp 'tag-complete-symbol
) ; XEmacs
3952 (funcall (symbol-function 'tag-complete-symbol
)))
3954 (error "This version of Emacs can't complete tags"))))
3957 ;; Based on `tags-complete-tag', but this one uses
3958 ;; `erlang-tags-completion-table' instead of `tags-completion-table'.
3960 ;; This is the entry-point called by system function `completing-read'.
3961 (defun erlang-tags-complete-tag (string predicate what
)
3963 ;; If we need to ask for the tag table, allow that.
3964 (let ((enable-recursive-minibuffers t
))
3965 (visit-tags-table-buffer))
3967 (all-completions string
(erlang-tags-completion-table) predicate
)
3968 (try-completion string
(erlang-tags-completion-table) predicate
))))
3971 ;; `tags-completion-table' calls itself recursively, make it
3972 ;; call our own wedge instead. Note that the recursive call
3973 ;; is very rare; it only occurs when a tags-file contains
3974 ;; `include'-statements.
3975 (defun erlang-tags-completion-table ()
3976 "Build completion table. Tags on the form `tag' or `module:tag'."
3977 (setq erlang-tags-orig-completion-table
3978 (symbol-function 'tags-completion-table
))
3979 (fset 'tags-completion-table
3980 (symbol-function 'erlang-tags-completion-table-1
))
3982 (erlang-tags-completion-table-1)
3983 (fset 'tags-completion-table
3984 erlang-tags-orig-completion-table
)))
3987 (defun erlang-tags-completion-table-1 ()
3988 (make-local-variable 'erlang-tags-completion-table
)
3989 (or erlang-tags-completion-table
3990 (let ((tags-completion-table nil
)
3991 (tags-completion-table-function
3992 'erlang-etags-tags-completion-table
))
3993 (funcall erlang-tags-orig-completion-table
)
3994 (setq erlang-tags-completion-table tags-completion-table
))))
3997 ;; Based on `etags-tags-completion-table'. The difference is that we
3998 ;; add three symbols to the vector, the tag, module: and module:tag.
3999 ;; The module is extracted from the file name of a tag. (This one
4000 ;; only works if we are looking at an `etags' file. However, this is
4001 ;; the only format supported by Emacs, so far.)
4002 (defun erlang-etags-tags-completion-table ()
4003 (let ((table (make-vector 511 0))
4006 (goto-char (point-min))
4007 ;; This monster regexp matches an etags tag line.
4008 ;; \1 is the string to match;
4009 ;; \2 is not interesting;
4010 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
4011 ;; \4 is not interesting;
4012 ;; \5 is the explicitly-specified tag name.
4013 ;; \6 is the line to start searching at;
4014 ;; \7 is the char to start searching at.
4017 (eq (following-char) ?
\f)
4018 (looking-at "\f\n\\([^,\n]*\\),.*\n"))
4019 (setq file
(buffer-substring
4020 (match-beginning 1) (match-end 1)))
4021 (goto-char (match-end 0)))
4024 ^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
4025 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
4026 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
4028 (let ((tag (if (match-beginning 5)
4029 ;; There is an explicit tag name.
4030 (buffer-substring (match-beginning 5) (match-end 5))
4031 ;; No explicit tag name. Best guess.
4032 (buffer-substring (match-beginning 3) (match-end 3))))
4034 (erlang-get-module-from-file-name file
))))
4036 (if (stringp module
)
4038 (intern (concat module
":" tag
) table
)
4039 ;; Only the first one will be stored in the table.
4040 (intern (concat module
":") table
))))))
4044 ;;; Prepare for other methods to run an Erlang slave process.
4047 (defvar erlang-shell-function
'inferior-erlang
4048 "Command to execute start a new Erlang shell.
4050 Change this variable to use your favorite
4051 Erlang compilation package.")
4053 (defvar erlang-shell-display-function
'inferior-erlang-run-or-select
4054 "Command to execute to display Erlang shell.
4056 Change this variable to use your favorite
4057 Erlang compilation package.")
4059 (defvar erlang-compile-function
'inferior-erlang-compile
4060 "Command to execute to compile current buffer.
4062 Change this variable to use your favorite
4063 Erlang compilation package.")
4065 (defvar erlang-compile-display-function
'inferior-erlang-run-or-select
4066 "Command to execute to view last compilation.
4068 Change this variable to use your favorite
4069 Erlang compilation package.")
4071 (defvar erlang-next-error-function
'inferior-erlang-next-error
4072 "Command to execute to go to the next error.
4074 Change this variable to use your favorite Erlang compilation
4075 package. Not used in Emacs 21.")
4079 (defun erlang-shell ()
4080 "Start a new Erlang shell.
4082 The variable `erlang-shell-function' decides which method to use,
4083 default is to start a new Erlang host. It is possible that, in the
4084 future, a new shell on an already running host will be started."
4086 (call-interactively erlang-shell-function
))
4089 ;;;###autoload (autoload 'run-erlang "erlang" "Start a new Erlang shell." t)
4091 ;; It is customary for Emacs packages to supply a function on this
4092 ;; form, even though it violates the `erlang-*' name convention.
4093 (defalias 'run-erlang
'erlang-shell
)
4096 (defun erlang-shell-display ()
4097 "Display an Erlang shell, or start a new."
4099 (call-interactively erlang-shell-display-function
))
4103 (defun erlang-compile ()
4104 "Compile Erlang module in current buffer."
4106 (call-interactively erlang-compile-function
))
4109 (defun erlang-compile-display ()
4110 "Display compilation output."
4112 (call-interactively erlang-compile-display-function
))
4115 (defun erlang-next-error ()
4116 "Display next error message from the latest compilation."
4118 (call-interactively erlang-next-error-function
))
4123 ;;; Erlang Shell Mode -- Major mode used for Erlang shells.
4126 ;; This mode is designed to be implementation independent,
4127 ;; e.g. it does not assume that we are running an inferior
4128 ;; Erlang, there exists a lot of other possibilities.
4131 (defvar erlang-shell-buffer-name
"*erlang*"
4132 "The name of the Erlang link shell buffer.")
4134 ;;(when (boundp 'same-window-buffer-names)
4135 ;; (unless (member "*Python*" same-window-buffer-names)
4136 ;; (push "*Python*" same-window-buffer-names)))
4139 (defvar erlang-shell-mode-map nil
4140 "Keymap used by Erlang shells.")
4143 (defvar erlang-shell-mode-hook nil
4144 "*User functions to run when an Erlang shell is started.
4146 This hook is used to change the behaviour of Erlang mode. It is
4147 normally used by the user to personalise the programming environment.
4148 When used in a site init file, it could be used to customise Erlang
4149 mode for all users on the system.
4151 The function added to this hook is run every time a new Erlang
4154 See also `erlang-load-hook', a hook which is run once, when Erlang
4155 mode is loaded, and `erlang-mode-hook' which is run every time a new
4156 Erlang source file is loaded into Emacs.")
4159 (defvar erlang-input-ring-file-name
"~/.erlang_history"
4160 "*When non-nil, file name used to store Erlang shell history information.")
4163 (defun erlang-shell-mode ()
4164 "Major mode for interacting with an Erlang shell.
4166 We assume that we already are in Comint mode.
4168 The following special commands are available:
4169 \\{erlang-shell-mode-map}"
4171 (setq major-mode
'erlang-shell-mode
)
4172 (setq mode-name
"Erlang Shell")
4173 (erlang-mode-variables)
4174 (if erlang-shell-mode-map
4176 (setq erlang-shell-mode-map
(copy-keymap comint-mode-map
))
4177 (erlang-shell-mode-commands erlang-shell-mode-map
))
4178 (use-local-map erlang-shell-mode-map
)
4179 (unless inferior-erlang-use-cmm
4180 ;; This was originally not a marker, but it needs to be, at least
4181 ;; in Emacs 21, and should be backwards-compatible. Otherwise,
4182 ;; would need to test whether compilation-parsing-end is a marker
4183 ;; after requiring `compile'.
4184 (set (make-local-variable 'compilation-parsing-end
) (copy-marker 1))
4185 (set (make-local-variable 'compilation-error-list
) nil
)
4186 (set (make-local-variable 'compilation-old-error-list
) nil
))
4187 ;; Needed when compiling directly from the Erlang shell.
4188 (setq compilation-last-buffer
(current-buffer))
4189 (erlang-add-compilation-alist erlang-error-regexp-alist
)
4190 (setq comint-prompt-regexp
"^[^>=]*> *")
4191 (setq comint-eol-on-send t
)
4192 (setq comint-input-ignoredups t
)
4193 (setq comint-scroll-show-maximum-output t
)
4194 (setq comint-scroll-to-bottom-on-output t
)
4195 ;; In Emacs 19.30, `add-hook' has got a `local' flag, use it. If
4196 ;; the call fails, just call the normal `add-hook'.
4199 (make-local-hook 'comint-output-filter-functions
) ; obsolete after Emacs 21.3
4200 (add-hook 'comint-output-filter-functions
4201 'inferior-erlang-strip-delete nil t
)
4202 (add-hook 'comint-output-filter-functions
4203 'inferior-erlang-strip-ctrl-m nil t
))
4205 (add-hook 'comint-output-filter-functions
'inferior-erlang-strip-delete
)
4206 (add-hook 'comint-output-filter-functions
'inferior-erlang-strip-ctrl-m
)))
4207 ;; Some older versions of comint don't have an input ring.
4208 (if (fboundp 'comint-read-input-ring
)
4210 (setq comint-input-ring-file-name erlang-input-ring-file-name
)
4211 (comint-read-input-ring t
)
4212 (make-local-variable 'kill-buffer-hook
)
4213 (add-hook 'kill-buffer-hook
'comint-write-input-ring
)))
4214 ;; At least in Emacs 21, we need to be in `compilation-minor-mode'
4215 ;; for `next-error' to work. We can avoid it clobbering the shell
4217 (when inferior-erlang-use-cmm
4218 (compilation-minor-mode 1)
4219 (set (make-local-variable 'minor-mode-overriding-map-alist
)
4220 `((compilation-minor-mode
4221 .
,(let ((map (make-sparse-keymap)))
4222 ;; It would be useful to put keymap properties on the
4223 ;; error lines so that we could use RET and mouse-2
4224 ;; on them directly.
4225 (when (boundp 'compilation-skip-threshold
) ; new compile.el
4226 (define-key map
[mouse-2
] #'erlang-mouse-2-command
)
4227 (define-key map
"\C-m" #'erlang-RET-command
))
4228 (if (boundp 'compilation-menu-map
)
4229 (define-key map
[menu-bar compilation
]
4230 (cons "Errors" compilation-menu-map
)))
4232 (run-hooks 'erlang-shell-mode-hook
))
4235 (defun erlang-mouse-2-command (event)
4236 "Command bound to `mouse-2' in inferior Erlang buffer.
4237 Selects Comint or Compilation mode command as appropriate."
4239 (if (save-window-excursion
4241 (mouse-set-point event
)
4242 (consp (get-text-property (line-beginning-position) 'message
))))
4243 (call-interactively (lookup-key compilation-mode-map
[mouse-2
]))
4244 (call-interactively (lookup-key comint-mode-map
[mouse-2
]))))
4246 (defun erlang-RET-command ()
4247 "Command bound to `RET' in inferior Erlang buffer.
4248 Selects Comint or Compilation mode command as appropriate."
4250 (if (consp (get-text-property (line-beginning-position) 'message
))
4251 (call-interactively (lookup-key compilation-mode-map
"\C-m"))
4252 (call-interactively (lookup-key comint-mode-map
"\C-m"))))
4254 (defun erlang-shell-mode-commands (map)
4255 (define-key map
"\M-\t" 'erlang-complete-tag
)
4256 (define-key map
"\C-a" 'comint-bol
) ; Normally the other way around.
4257 (define-key map
"\C-c\C-a" 'beginning-of-line
)
4258 (define-key map
"\C-d" nil
) ; Was `comint-delchar-or-maybe-eof'
4259 (define-key map
"\M-\C-m" 'compile-goto-error
)
4260 (unless inferior-erlang-use-cmm
4261 (define-key map
"\C-x`" 'erlang-next-error
)))
4264 ;;; Inferior Erlang -- Run an Erlang shell as a subprocess.
4267 (defvar inferior-erlang-display-buffer-any-frame nil
4268 "*When nil, `inferior-erlang-display-buffer' use only selected frame.
4269 When t, all frames are searched. When 'raise, the frame is raised.")
4271 (defvar inferior-erlang-shell-type
'newshell
4272 "The type of Erlang shell to use.
4274 When this variable is set to the atom `oldshell', the old shell is used.
4275 When set to `newshell' the new shell is used. Should the variable be
4276 nil, the default shell is used.
4278 This variable influence the setting of other variables.")
4280 (defvar inferior-erlang-machine
"erl"
4281 "*The name of the Erlang shell.")
4283 (defvar inferior-erlang-machine-options
'()
4284 "*The options used when activating the Erlang shell.
4286 This must be a list of strings.")
4288 (defvar inferior-erlang-process-name
"inferior-erlang"
4289 "The name of the inferior Erlang process.")
4291 (defvar inferior-erlang-buffer-name erlang-shell-buffer-name
4292 "The name of the inferior Erlang buffer.")
4294 (defvar inferior-erlang-prompt-timeout
60
4295 "*Number of seconds before `inferior-erlang-wait-prompt' timeouts.
4297 The time specified is waited after every output made by the inferior
4298 Erlang shell. When this variable is t, we assume that we always have
4299 a prompt. When nil, we will wait forever, or until \\[keyboard-quit].")
4301 (defvar inferior-erlang-process nil
4302 "Process of last invoked inferior Erlang, or nil.")
4304 (defvar inferior-erlang-buffer nil
4305 "Buffer of last invoked inferior Erlang, or nil.")
4308 (defun inferior-erlang ()
4309 "Run an inferior Erlang.
4311 This is just like running Erlang in a normal shell, except that
4312 an Emacs buffer is used for input and output.
4314 The command line history can be accessed with \\[comint-previous-input] and \\[comint-next-input].
4315 The history is saved between sessions.
4317 Entry to this mode calls the functions in the variables
4318 `comint-mode-hook' and `erlang-shell-mode-hook' with no arguments.
4320 The following commands imitate the usual Unix interrupt and
4321 editing control characters:
4322 \\{erlang-shell-mode-map}"
4325 (let ((opts inferior-erlang-machine-options
))
4326 (cond ((eq inferior-erlang-shell-type
'oldshell
)
4327 (setq opts
(cons "-oldshell" opts
)))
4328 ((eq inferior-erlang-shell-type
'newshell
)
4329 (setq opts
(append '("-newshell" "-env" "TERM" "vt100") opts
))))
4330 (setq inferior-erlang-buffer
4332 inferior-erlang-process-name inferior-erlang-machine
4334 (setq inferior-erlang-process
4335 (get-buffer-process inferior-erlang-buffer
))
4336 (process-kill-without-query inferior-erlang-process
)
4337 (switch-to-buffer inferior-erlang-buffer
)
4338 (if (and (not (eq system-type
'windows-nt
))
4339 (eq inferior-erlang-shell-type
'newshell
))
4340 (setq comint-process-echoes t
))
4341 ;; `rename-buffer' takes only one argument in Emacs 18.
4343 (rename-buffer inferior-erlang-buffer-name t
)
4344 (error (rename-buffer inferior-erlang-buffer-name
)))
4345 (erlang-shell-mode))
4348 (defun inferior-erlang-run-or-select ()
4349 "Switch to an inferior Erlang buffer, possibly starting new process."
4351 (if (null (inferior-erlang-running-p))
4353 (inferior-erlang-display-buffer t
)))
4356 (defun inferior-erlang-display-buffer (&optional select
)
4357 "Make the inferior Erlang process visible.
4358 The window is returned.
4360 Should `inferior-erlang-display-buffer-any-frame' be nil the buffer is
4361 displayed in the current frame. Should it be non-nil, and the buffer
4362 already is visible in any other frame, no new window will be created.
4363 Should it be the atom 'raise, the frame containing the window will
4366 Should the optional argument SELECT be non-nil, the window is
4367 selected. Should the window be in another frame, that frame is raised.
4369 Note, should the mouse pointer be places outside the raised frame, that
4370 frame will become deselected before the next command."
4372 (or (inferior-erlang-running-p)
4373 (error "No inferior Erlang process is running"))
4374 (let ((win (inferior-erlang-window
4375 inferior-erlang-display-buffer-any-frame
))
4376 (frames-p (fboundp 'selected-frame
)))
4378 (let ((old-win (selected-window)))
4380 (switch-to-buffer-other-window inferior-erlang-buffer
)
4381 (setq win
(selected-window)))
4382 (select-window old-win
))
4383 (if (and window-system
4386 (eq inferior-erlang-display-buffer-any-frame
'raise
))
4387 (not (eq (selected-frame) (window-frame win
))))
4388 (raise-frame (window-frame win
))))
4390 (select-window win
))
4395 (defun inferior-erlang-running-p ()
4396 "Non-nil when an inferior Erlang is running."
4397 (and inferior-erlang-process
4398 (memq (process-status inferior-erlang-process
) '(run open
))
4399 inferior-erlang-buffer
4400 (buffer-name inferior-erlang-buffer
)))
4403 (defun inferior-erlang-window (&optional all-frames
)
4404 "Return the window containing the inferior Erlang, or nil."
4405 (and (inferior-erlang-running-p)
4406 (if (and all-frames
(>= erlang-emacs-major-version
19))
4407 (get-buffer-window inferior-erlang-buffer t
)
4408 (get-buffer-window inferior-erlang-buffer
))))
4411 (defun inferior-erlang-wait-prompt ()
4412 "Wait until the inferior Erlang shell prompt appears."
4413 (if (eq inferior-erlang-prompt-timeout t
)
4415 (or (inferior-erlang-running-p)
4416 (error "No inferior Erlang shell is running"))
4418 (set-buffer inferior-erlang-buffer
)
4420 (while (save-excursion
4421 (goto-char (process-mark inferior-erlang-process
))
4423 (not (looking-at comint-prompt-regexp
)))
4427 (message "Waiting for Erlang shell prompt (press C-g to abort)."))
4428 (or (accept-process-output inferior-erlang-process
4429 inferior-erlang-prompt-timeout
)
4430 (error "No Erlang shell prompt before timeout")))
4431 (if msg
(message ""))))))
4433 (autoload 'comint-send-input
"comint")
4435 (defun inferior-erlang-send-command (cmd &optional hist
)
4436 "Send command CMD to the inferior Erlang.
4438 The contents of the current command line (if any) will
4439 be placed at the next prompt.
4441 If optional second argument is non-nil the command is inserted into
4444 Return the position after the newly inserted command."
4445 (or (inferior-erlang-running-p)
4446 (error "No inferior Erlang process is running"))
4447 (let ((old-buffer (current-buffer))
4448 (insert-point (marker-position (process-mark inferior-erlang-process
)))
4449 (insert-length (if comint-process-echoes
4451 (1+ (length cmd
)))))
4452 (set-buffer inferior-erlang-buffer
)
4453 (goto-char insert-point
)
4455 ;; Strange things happened if `comint-eol-on-send' is declared
4456 ;; in the `let' expression above, but setq:d here. The
4457 ;; `set-buffer' statement obviously makes the buffer local
4458 ;; instance of `comint-eol-on-send' shadow this one.
4459 ;; I'm considering this a bug in Elisp.
4461 ;; This was previously cautioned against in the Lisp manual. It
4462 ;; has been sorted out in Emacs 21. -- fx
4463 (let ((comint-eol-on-send nil
)
4464 (comint-input-filter (if hist comint-input-filter
'ignore
)))
4465 (comint-send-input))
4466 ;; Adjust all windows whose points are incorrect.
4467 (if (null comint-process-echoes
)
4471 (if (and (eq (window-buffer window
) inferior-erlang-buffer
)
4472 (= (window-point window
) insert-point
))
4473 (set-window-point window
4474 (+ insert-point insert-length
)))))
4476 (set-buffer old-buffer
)
4477 (+ insert-point insert-length
)))
4480 (defun inferior-erlang-strip-delete (&optional s
)
4481 "Remove `^H' (delete) and the characters it was supposed to remove."
4483 (if (and (boundp 'comint-last-input-end
)
4484 (boundp 'comint-last-output-start
))
4488 (symbol-value 'comint-last-input-end
)
4489 (symbol-value 'comint-last-output-start
)))
4490 (while (progn (skip-chars-forward "^\C-h")
4491 (not (eq (point) (point-max))))
4494 (backward-delete-char 1))))))
4497 ;; Basically `comint-strip-ctrl-m', with a few extra checks.
4498 (defun inferior-erlang-strip-ctrl-m (&optional string
)
4499 "Strip trailing `^M' characters from the current output group."
4501 (if (and (boundp 'comint-last-input-end
)
4502 (boundp 'comint-last-output-start
))
4503 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
4507 (symbol-value 'comint-last-input-end
)
4508 (symbol-value 'comint-last-output-start
)))
4509 (while (re-search-forward "\r+$" pmark t
)
4510 (replace-match "" t t
))))))
4513 (defun inferior-erlang-compile ()
4514 "Compile the file in the current buffer.
4516 Should Erlang return `{error, nofile}' it could not load the object
4517 module after completing the compilation. This is due to a bug in the
4518 compile command `c' when using the option `outdir'.
4520 There exists two workarounds for this bug:
4522 1) Place the directory in the Erlang load path.
4524 2) Set the Emacs variable `erlang-compile-use-outdir' to nil.
4525 To do so, place the following line in your `~/.emacs'-file:
4526 (setq erlang-compile-use-outdir nil)"
4529 (or (inferior-erlang-running-p)
4532 (or (inferior-erlang-running-p)
4533 (error "Error starting inferior Erlang shell"))
4534 (let ((dir (file-name-directory (buffer-file-name)))
4535 ;;; (file (file-name-nondirectory (buffer-file-name)))
4536 (noext (substring (buffer-file-name) 0 -
4))
4537 ;; Hopefully, noone else will ever use these...
4541 (inferior-erlang-display-buffer)
4542 (inferior-erlang-wait-prompt)
4543 (setq end
(inferior-erlang-send-command
4544 (if erlang-compile-use-outdir
4545 (format "c(\"%s\", [{outdir, \"%s\"}])." noext dir
)
4548 "f(%s), {ok, %s} = file:get_cwd(), "
4549 "file:set_cwd(\"%s\"), "
4550 "%s = c(\"%s\"), file:set_cwd(%s), f(%s), %s.")
4553 tmpvar2 noext tmpvar tmpvar tmpvar2
))
4555 (inferior-erlang-wait-prompt)
4557 (set-buffer inferior-erlang-buffer
)
4558 (setq compilation-error-list nil
)
4559 (set-marker compilation-parsing-end end
))
4560 (setq compilation-last-buffer inferior-erlang-buffer
)))
4563 ;; `next-error' only accepts buffers with major mode `compilation-mode'
4564 ;; or with the minor mode `compilation-minor-mode' activated.
4565 ;; (To activate the minor mode is out of the question, since it will
4566 ;; ruin the inferior Erlang keymap.)
4567 ;; This is done differently in Emacs 21.
4568 (defun inferior-erlang-next-error (&optional argp
)
4569 "Just like `next-error'.
4570 Capable of finding error messages in an inferior Erlang buffer."
4573 (buf (and (boundp 'compilation-last-buffer
)
4574 compilation-last-buffer
)))
4575 (if (and (bufferp buf
)
4578 (and (eq major-mode
'erlang-shell-mode
)
4579 (setq major-mode
'compilation-mode
))))
4586 (setq major-mode
'erlang-shell-mode
))))
4588 (next-error argp
))))
4591 (defun inferior-erlang-change-directory (&optional dir
)
4592 "Make the inferior Erlang change directory.
4593 The default is to go to the directory of the current buffer."
4595 (or dir
(setq dir
(file-name-directory (buffer-file-name))))
4596 (or (inferior-erlang-running-p)
4597 (error "No inferior Erlang is running"))
4598 (inferior-erlang-display-buffer)
4599 (inferior-erlang-wait-prompt)
4600 (inferior-erlang-send-command (format "cd('%s')." dir
) nil
))
4602 (defun erlang-align-arrows (start end
)
4603 "Align arrows (\"->\") in function clauses from START to END.
4604 When called interactively, aligns arrows after function clauses inside
4607 With a prefix argument, aligns all arrows, not just those in function
4612 sum(L) -> sum(L, 0).
4613 sum([H|T], Sum) -> sum(T, Sum + H);
4614 sum([], Sum) -> Sum.
4618 sum(L) -> sum(L, 0).
4619 sum([H|T], Sum) -> sum(T, Sum + H);
4620 sum([], Sum) -> Sum."
4623 (let (;; regexp for matching arrows. without a prefix argument,
4624 ;; the regexp matches function heads. With a prefix, it
4625 ;; matches any arrow.
4626 (re (if current-prefix-arg
4629 (concat "^" erlang-atom-regexp
".*\\(\\)->"))))
4630 ;; part of regexp matching directly before the arrow
4631 (arrow-match-pos (if current-prefix-arg
4633 (1+ erlang-atom-regexp-matches
)))
4634 ;; accumulator for positions where arrows are found, ordered
4635 ;; by buffer position (from greatest to smallest)
4636 (arrow-positions '())
4637 ;; accumulator for longest distance from start of line to arrow
4639 ;; marker to track the end of the region we're aligning
4640 (end-marker (progn (goto-char end
)
4642 ;; Pass 1: Find the arrow positions, adjust the whitespace
4643 ;; before each arrow to one space, and find the greatest
4644 ;; indentation level.
4646 (while (re-search-forward re end-marker t
)
4647 (goto-char (match-beginning arrow-match-pos
))
4648 (just-one-space) ; adjust whitespace
4649 (setq arrow-positions
(cons (point) arrow-positions
))
4650 (setq most-indent
(max most-indent
(erlang-column-number))))
4651 (set-marker end-marker nil
) ; free the marker
4652 ;; Pass 2: Insert extra padding so that all arrow indentation is
4653 ;; equal. This is done last-to-first by buffer position, so that
4654 ;; inserting spaces before one arrow doesn't change the
4655 ;; positions of the next ones.
4656 (mapcar (lambda (arrow-pos)
4657 (goto-char arrow-pos
)
4658 (let* ((pad (- most-indent
(erlang-column-number))))
4660 (insert-char ?\ pad
))))
4663 (defun erlang-column-number ()
4664 "Return the column number of the current position in the buffer.
4665 Tab characters are counted by their visual width."
4666 (string-width (buffer-substring (line-beginning-position) (point))))
4668 (defun erlang-current-defun ()
4669 "`add-log-current-defun-function' for Erlang."
4671 (erlang-beginning-of-function)
4672 (if (looking-at "[a-z0-9_]+")
4675 ;; Aliases for backward compatibility with older versions of Erlang Mode.
4677 ;; Unfortuantely, older versions of Emacs doesn't have `defalias' and
4678 ;; `make-obsolete' so we have to define our own `obsolete' function.
4680 (defun erlang-obsolete (sym newdef
)
4681 "Make the obsolete function SYM refer to the defined function NEWDEF.
4683 Simplified version of a combination `defalias' and `make-obsolete',
4684 it assumes that NEWDEF is loaded."
4685 (defalias sym
(symbol-function newdef
))
4686 (if (fboundp 'make-obsolete
)
4687 (make-obsolete sym newdef
)))
4690 (erlang-obsolete 'calculate-erlang-indent
'erlang-calculate-indent
)
4691 (erlang-obsolete 'calculate-erlang-stack-indent
4692 'erlang-calculate-stack-indent
)
4693 (erlang-obsolete 'at-erlang-keyword
'erlang-at-keyword
)
4694 (erlang-obsolete 'at-erlang-operator
'erlang-at-operator
)
4695 (erlang-obsolete 'beginning-of-erlang-clause
'erlang-beginning-of-clause
)
4696 (erlang-obsolete 'end-of-erlang-clause
'erlang-end-of-clause
)
4697 (erlang-obsolete 'mark-erlang-clause
'erlang-mark-clause
)
4698 (erlang-obsolete 'beginning-of-erlang-function
'erlang-beginning-of-function
)
4699 (erlang-obsolete 'end-of-erlang-function
'erlang-end-of-function
)
4700 (erlang-obsolete 'mark-erlang-function
'erlang-mark-function
)
4701 (erlang-obsolete 'pass-over-erlang-clause
'erlang-pass-over-function
)
4702 (erlang-obsolete 'name-of-erlang-function
'erlang-name-of-function
)
4705 ;; Fixme: shouldn't redefine `set-visited-file-name' anyhow -- see above.
4706 (defconst erlang-unload-hook
4708 (defalias 'set-visited-file-name
4709 'erlang-orig-set-visited-file-name
)
4710 (when (featurep 'advice
)
4711 (ad-unadvise 'Man-notify-when-ready
)
4712 (ad-unadvise 'set-visited-file-name
)))))
4718 (run-hooks 'erlang-load-hook
)
4721 ;; coding: iso-8859-1
4724 ;;; erlang.el ends here