Remove unused code.
[erlware-mode.git] / erlang.el
blob75857b9fbdb6b7a0dbbbef4f3864baa0c123fd8a
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
6 ;; Version: 2.5.2
7 ;; Keywords: erlang, languages, processes
8 ;; Date: 2000-09-11
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
19 ;; under the License.
21 ;; The Initial Developer of the Original Code is Ericsson Utvecklings AB.
22 ;; All Rights Reserved.
24 ;; Lars Thorsén's modifications of 2000-06-07 included.
26 ;; The original version of this package was written by Robert Virding.
28 ;; Most skeletons have been written at Ericsson Telecom by
29 ;; magnus@erix.ericsson.se and janne@erix.ericsson.se
31 ;;; Commentary:
33 ;; Introduction:
34 ;; ------------
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.
43 ;; Installation:
44 ;; ------------
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.
54 ;; Reporting Bugs:
55 ;; --------------
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
76 ;;; Code:
78 ;; Variables:
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
88 Erlang mode menu.")
90 (defvar erlang-menu-items '(erlang-menu-base-items
91 erlang-menu-skel-items
92 erlang-menu-shell-items
93 erlang-menu-compile-items
94 erlang-menu-man-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
101 items.
103 Please call the function `erlang-menu-init' after every change to this
104 variable.")
106 (defvar erlang-menu-base-items
107 '(("Indent"
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)))
114 ("Edit"
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)))
138 ("TAGS"
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.
154 Example:
155 '((\"Func1\" function-one)
156 (\"SubItem\"
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
165 '(nil
166 ("Shell"
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
174 '(("Compile"
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
183 '(nil
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
191 of the format.")
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)
231 (if window-system
232 (progn
233 (setq font-lock-maximum-decoration t)
234 (font-lock-mode 1)))
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
249 the first time.
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
280 erlang-electric-gt)
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'
287 `erlang-electric-gt'
288 `erlang-electric-newline'
290 Should the variable be bound to t, all electric commands
291 are activated.
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
299 electric command.
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
306 inhibited.")
308 (defvar erlang-electric-newline-inhibit-list
309 '(erlang-electric-semicolon
310 erlang-electric-comma
311 erlang-electric-gt)
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
321 inserted.")
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
379 '(t)
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
414 rules.
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
419 lists.
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
449 function.")
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.")
487 (eval-and-compile
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)
534 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)
542 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))
572 (t nil))
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
589 (list
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
595 (list
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
601 (list
602 (list "\\(->\\|:-\\)\\(\\s \\|$\\)" 2 'font-lock-function-name-face))
603 "Font lock keyword highlighting clause arrow.")
605 (defvar erlang-font-lock-keywords-lc
606 (list
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
612 (list
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
620 (list
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
626 (list
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
640 (list
641 (list
642 (concat "\\<\\("
643 "\\(is_\\)*\\(atom\\|boolean\\|function\\|binary\\|constant"
644 "\\|float\\|integer\\|list\\|number\\|p\\(id\\|ort\\)\\|"
645 "re\\(ference\\|cord\\)\\|tuple"
646 "\\)\\)\\s *(")
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
655 (list
656 (list
657 (concat
658 "\\<\\("
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\\)\\|"
665 "h\\(alt\\|d\\)\\|"
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\\)\\|"
670 "open_port\\|"
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"
678 "\\)\\s *(")
680 'font-lock-keyword-face))
681 "Font lock keyword highlighting built in functions.")
683 (defvar erlang-font-lock-keywords-macros
684 (list
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
699 (list
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]+\\)\\>"
704 1 nil t)
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
711 (list
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'
718 to work properly.")
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.
738 Example:
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.
760 Example:
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.
781 Example:
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.
798 Example:
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
807 this syntax table.
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
817 ;; no-op.
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."
827 (` (if (, condition)
829 (,@ body)))))
831 (or (fboundp 'when)
832 (defmacro when (condition &rest body)
833 "(when CONDITION BODY...): If CONDITION is true, do BODY, else return nil."
834 (` (if (, condition)
835 (progn (,@ body))
836 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)))))))
843 (eval-when-compile
844 (if (or (featurep 'bytecomp)
845 (featurep 'byte-compile))
846 (progn
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))))
853 (require 'comint)
854 (require 'compile))))
857 (defun erlang-version ()
858 "Return the current version of Erlang mode."
859 (interactive)
860 (if (interactive-p)
861 (message "Erlang mode version %s, written by Anders Lindgren"
862 erlang-version))
863 erlang-version)
866 ;;;###autoload
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
876 \"Erlang\".
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
887 useful commands:
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)
892 will look better.
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.
901 C-x ` - Next error.
902 , - Electric comma.
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
907 source.
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,
916 respectively.
918 On entry to this mode the contents of the hook `erlang-mode-hook' is
919 executed.
921 Please see the beginning of the file `erlang.el' for more information
922 and examples of hooks.
924 Other commands:
925 \\{erlang-mode-map}"
926 (interactive)
927 (kill-all-local-variables)
928 (setq major-mode 'erlang-mode)
929 (setq mode-name "Erlang")
930 (erlang-syntax-table-init)
931 (erlang-keymap-init)
932 (erlang-electric-init)
933 (erlang-menu-init)
934 (erlang-mode-variables)
935 (erlang-check-module-name-init)
936 (erlang-add-compilation-alist erlang-error-regexp-alist)
937 (erlang-man-init)
938 (erlang-tags-init)
939 (erlang-font-lock-init)
940 (erlang-skel-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 (setq erlang-mode-syntax-table table)))
971 (set-syntax-table erlang-mode-syntax-table))
974 (defun erlang-keymap-init ()
975 (if erlang-mode-map
977 (setq erlang-mode-map (make-sparse-keymap))
978 (erlang-mode-commands erlang-mode-map))
979 (use-local-map erlang-mode-map))
982 (defun erlang-mode-commands (map)
983 (unless (boundp 'indent-line-function)
984 (define-key map "\t" 'erlang-indent-command))
985 (define-key map ";" 'erlang-electric-semicolon)
986 (define-key map "," 'erlang-electric-comma)
987 (define-key map "<" 'erlang-electric-lt)
988 (define-key map ">" 'erlang-electric-gt)
989 (define-key map "\C-m" 'erlang-electric-newline)
990 (if (not (boundp 'delete-key-deletes-forward))
991 (define-key map "\177" 'backward-delete-char-untabify)
992 (define-key map [backspace] 'backward-delete-char-untabify))
993 (define-key map "\M-q" 'erlang-fill-paragraph)
994 (unless (boundp 'beginning-of-defun-function)
995 (define-key map "\M-\C-a" 'erlang-beginning-of-function)
996 (define-key map "\M-\C-e" 'erlang-end-of-function)
997 (define-key map "\M-\C-h" 'erlang-mark-function))
998 (define-key map "\M-\t" 'erlang-complete-tag)
999 (define-key map "\C-c\M-\t" 'tempo-complete-tag)
1000 (define-key map "\M-+" 'erlang-find-next-tag)
1001 (define-key map "\C-c\M-a" 'erlang-beginning-of-clause)
1002 (define-key map "\C-c\M-b" 'tempo-backward-mark)
1003 (define-key map "\C-c\M-e" 'erlang-end-of-clause)
1004 (define-key map "\C-c\M-f" 'tempo-forward-mark)
1005 (define-key map "\C-c\M-h" 'erlang-mark-clause)
1006 (define-key map "\C-c\C-c" 'comment-region)
1007 (define-key map "\C-c\C-j" 'erlang-generate-new-clause)
1008 (define-key map "\C-c\C-k" 'erlang-compile)
1009 (define-key map "\C-c\C-l" 'erlang-compile-display)
1010 (define-key map "\C-c\C-s" 'erlang-show-syntactic-information)
1011 (define-key map "\C-c\C-q" 'erlang-indent-function)
1012 (define-key map "\C-c\C-u" 'erlang-uncomment-region)
1013 (define-key map "\C-c\C-y" 'erlang-clone-arguments)
1014 (define-key map "\C-c\C-a" 'erlang-align-arrows)
1015 (define-key map "\C-c\C-z" 'erlang-shell-display)
1016 (unless inferior-erlang-use-cmm
1017 (define-key map "\C-x`" 'erlang-next-error)))
1020 (defun erlang-electric-init ()
1021 ;; Set up electric character functions to work with
1022 ;; delsel/pending-del mode. Also, set up text properties for bit
1023 ;; syntax handling.
1024 (mapcar #'(lambda (cmd)
1025 (put cmd 'delete-selection t) ;for delsel (Emacs)
1026 (put cmd 'pending-delete t)) ;for pending-del (XEmacs)
1027 '(erlang-electric-semicolon
1028 erlang-electric-comma
1029 erlang-electric-gt))
1031 (put 'bitsyntax-open-outer 'syntax-table '(4 . ?>))
1032 (put 'bitsyntax-open-outer 'rear-nonsticky '(category))
1033 (put 'bitsyntax-open-inner 'rear-nonsticky '(category))
1034 (put 'bitsyntax-close-inner 'rear-nonsticky '(category))
1035 (put 'bitsyntax-close-outer 'syntax-table '(5 . ?<))
1036 (put 'bitsyntax-close-outer 'rear-nonsticky '(category))
1037 (setq parse-sexp-lookup-properties 't))
1040 (defun erlang-mode-variables ()
1041 (or erlang-mode-abbrev-table
1042 (define-abbrev-table 'erlang-mode-abbrev-table ()))
1043 (setq local-abbrev-table erlang-mode-abbrev-table)
1044 (make-local-variable 'paragraph-start)
1045 (setq paragraph-start (concat "^$\\|" page-delimiter))
1046 (make-local-variable 'paragraph-separate)
1047 (setq paragraph-separate paragraph-start)
1048 (make-local-variable 'paragraph-ignore-fill-prefix)
1049 (setq paragraph-ignore-fill-prefix t)
1050 (make-local-variable 'require-final-newline)
1051 (setq require-final-newline t)
1052 (make-local-variable 'defun-prompt-regexp)
1053 (setq defun-prompt-regexp erlang-defun-prompt-regexp)
1054 (make-local-variable 'comment-start)
1055 (setq comment-start "%")
1056 (make-local-variable 'comment-start-skip)
1057 (setq comment-start-skip "%+\\s *")
1058 (make-local-variable 'comment-column)
1059 (setq comment-column 48)
1060 (make-local-variable 'indent-line-function)
1061 (setq indent-line-function 'erlang-indent-command)
1062 (make-local-variable 'indent-region-function)
1063 (setq indent-region-function 'erlang-indent-region)
1064 (set (make-local-variable 'comment-indent-function) 'erlang-comment-indent)
1065 (if (<= erlang-emacs-major-version 18)
1066 (set (make-local-variable 'comment-indent-hook) 'erlang-comment-indent))
1067 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1068 (set (make-local-variable 'dabbrev-case-fold-search) nil)
1069 (set (make-local-variable 'imenu-prev-index-position-function)
1070 'erlang-beginning-of-function)
1071 (set (make-local-variable 'imenu-extract-index-name-function)
1072 'erlang-get-function-name)
1073 (set (make-local-variable 'tempo-match-finder)
1074 "[^-a-zA-Z0-9_]\\([-a-zA-Z0-9_]*\\)\\=")
1075 (set (make-local-variable 'beginning-of-defun-function)
1076 'erlang-beginning-of-function)
1077 (set (make-local-variable 'end-of-defun-function) 'erlang-end-of-function)
1078 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil)
1079 (set (make-local-variable 'fill-paragraph-function) 'erlang-fill-paragraph)
1080 (set (make-local-variable 'comment-add) 1)
1081 (set (make-local-variable 'outline-regexp) "[[:lower:]0-9_]+ *(.*) *-> *$")
1082 (set (make-local-variable 'outline-level) (lambda () 1))
1083 (set (make-local-variable 'add-log-current-defun-function)
1084 'erlang-current-defun))
1087 ;; Compilation.
1089 ;; The following code is compatible with the standard package `compilation',
1090 ;; making it possible to go to errors using `erlang-next-error' (or just
1091 ;; `next-error' in Emacs 21).
1093 ;; The normal `compile' command works of course. For best result, please
1094 ;; execute `make' with the `-w' flag.
1096 ;; Please see the variables named `compiling-..' above.
1098 (defun erlang-add-compilation-alist (alist)
1099 (require 'compile)
1100 (cond ((boundp 'compilation-error-regexp-alist) ; Emacs 19
1101 (while alist
1102 (or (assoc (car (car alist)) compilation-error-regexp-alist)
1103 (setq compilation-error-regexp-alist
1104 (cons (car alist) compilation-error-regexp-alist)))
1105 (setq alist (cdr alist))))
1106 ((boundp 'compilation-error-regexp)
1107 ;; Emacs 18, Only one regexp is allowed.
1108 (funcall (symbol-function 'set)
1109 'compilation-error-regexp (car (car alist))))))
1111 (defun erlang-font-lock-init ()
1112 "Initialize Font Lock for Erlang mode."
1113 (or erlang-font-lock-syntax-table
1114 (setq erlang-font-lock-syntax-table
1115 (let ((table (copy-syntax-table erlang-mode-syntax-table)))
1116 (modify-syntax-entry ?_ "w" table)
1117 table)))
1118 (set (make-local-variable 'font-lock-syntax-table)
1119 erlang-font-lock-syntax-table)
1120 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1121 'erlang-beginning-of-clause)
1122 (make-local-variable 'font-lock-keywords)
1123 (let ((level (cond ((boundp 'font-lock-maximum-decoration)
1124 (symbol-value 'font-lock-maximum-decoration))
1125 ((boundp 'font-lock-use-maximal-decoration)
1126 (symbol-value 'font-lock-use-maximal-decoration))
1127 (t nil))))
1128 (if (consp level)
1129 (setq level (cdr-safe (or (assq 'erlang-mode level)
1130 (assq t level)))))
1131 ;; `level' can here be:
1132 ;; A number - The fontification level
1133 ;; nil - Use the default
1134 ;; t - Use maximum
1135 (cond ((eq level nil)
1136 (set 'font-lock-keywords erlang-font-lock-keywords))
1137 ((eq level 1)
1138 (set 'font-lock-keywords erlang-font-lock-keywords-1))
1139 ((eq level 2)
1140 (set 'font-lock-keywords erlang-font-lock-keywords-2))
1142 (set 'font-lock-keywords erlang-font-lock-keywords-3))))
1144 ;; Modern font-locks can handle the above much more elegantly:
1145 (set (make-local-variable 'font-lock-defaults)
1146 '((erlang-font-lock-keywords erlang-font-lock-keywords-1
1147 erlang-font-lock-keywords-2 erlang-font-lock-keywords-3)
1148 nil nil ((?_ . "w")) erlang-beginning-of-clause
1149 (font-lock-mark-block-function . erlang-mark-clause))))
1153 ;; Useful when defining your own keywords.
1154 (defun erlang-font-lock-set-face (ks &rest faces)
1155 "Replace the face components in a list of keywords.
1157 The first argument, KS, is a list of keywords. The rest of the
1158 arguments are expressions to replace the face information with. The
1159 first expression replaces the face of the first keyword, the second
1160 expression the second keyword etc.
1162 Should an expression be nil, the face of the corresponding keyword is
1163 not changed.
1165 Should fewer expressions than keywords be given, the last expression
1166 is used for all remaining keywords.
1168 Normally, the expressions are just atoms representing the new face.
1169 They could however be more complex, returning different faces in
1170 different situations.
1172 This function only handles keywords with elements on the forms:
1173 (REGEXP NUMBER FACE)
1174 (REGEXP NUMBER FACE OVERWRITE)
1176 This could be used when defining your own special font-lock setup, e.g:
1178 \(setq my-font-lock-keywords
1179 (append erlang-font-lock-keywords-func
1180 erlang-font-lock-keywords-dollar
1181 (erlang-font-lock-set-face
1182 erlang-font-lock-keywords-macros 'my-neon-green-face)
1183 (erlang-font-lock-set-face
1184 erlang-font-lock-keywords-lc 'my-deep-red 'my-light-red)
1185 erlang-font-lock-keywords-attr))
1187 For a more elaborate example, please see the beginning of the file
1188 `erlang.el'."
1189 (let ((res '()))
1190 (while ks
1191 (let* ((regexp (car (car ks)))
1192 (number (car (cdr (car ks))))
1193 (new-face (if (and faces (car faces))
1194 (car faces)
1195 (car (cdr (cdr (car ks))))))
1196 (overwrite (car (cdr (cdr (cdr (car ks))))))
1197 (new-keyword (list regexp number new-face)))
1198 (if overwrite (nconc new-keyword (list overwrite)))
1199 (setq res (cons new-keyword res))
1200 (setq ks (cdr ks))
1201 (if (and faces (cdr faces))
1202 (setq faces (cdr faces)))))
1203 (nreverse res)))
1206 (defun erlang-font-lock-level-0 ()
1207 ;; DocStringOrig: font-cmd
1208 "Unfontify current buffer."
1209 (interactive)
1210 (font-lock-mode 0))
1213 (defun erlang-font-lock-level-1 ()
1214 ;; DocStringCopy: font-cmd
1215 "Fontify current buffer at level 1.
1216 This highlights function headers, reserved keywords, strings and comments."
1217 (interactive)
1218 (require 'font-lock)
1219 (set 'font-lock-keywords erlang-font-lock-keywords-1)
1220 (font-lock-mode 1)
1221 (funcall (symbol-function 'font-lock-fontify-buffer)))
1224 (defun erlang-font-lock-level-2 ()
1225 ;; DocStringCopy: font-cmd
1226 "Fontify current buffer at level 2.
1227 This highlights level 1 features (see `erlang-font-lock-level-1')
1228 plus bifs, guards and `single quotes'."
1229 (interactive)
1230 (require 'font-lock)
1231 (set 'font-lock-keywords erlang-font-lock-keywords-2)
1232 (font-lock-mode 1)
1233 (funcall (symbol-function 'font-lock-fontify-buffer)))
1236 (defun erlang-font-lock-level-3 ()
1237 ;; DocStringCopy: font-cmd
1238 "Fontify current buffer at level 3.
1239 This highlights level 2 features (see `erlang-font-lock-level-2')
1240 plus variables, macros and records."
1241 (interactive)
1242 (require 'font-lock)
1243 (set 'font-lock-keywords erlang-font-lock-keywords-3)
1244 (font-lock-mode 1)
1245 (funcall (symbol-function 'font-lock-fontify-buffer)))
1248 (defun erlang-menu-init ()
1249 "Init menus for Erlang mode.
1251 The variable `erlang-menu-items' contain a description of the Erlang
1252 mode menu. Normally, the list contains atoms, representing variables
1253 bound to pieces of the menu.
1255 Personal extensions could be added to `erlang-menu-personal-items'.
1257 This function should be called if any variable describing the
1258 menu configuration is changed."
1259 (erlang-menu-install "Erlang" erlang-menu-items erlang-mode-map t))
1262 (defun erlang-menu-install (name items keymap &optional popup)
1263 "Install a menu in Emacs or XEmacs based on an abstract description.
1265 NAME is the name of the menu.
1267 ITEMS is a list. The elements are either nil representing a horizontal
1268 line or a list with two or three elements. The first is the name of
1269 the menu item, the second the function to call, or a submenu, on the
1270 same same form as ITEMS. The third optional element is an expression
1271 which is evaluated every time the menu is displayed. Should the
1272 expression evaluate to nil the menu item is ghosted.
1274 KEYMAP is the keymap to add to menu to. (When using XEmacs, the menu
1275 will only be visible when this menu is the global, the local, or an
1276 activate minor mode keymap.)
1278 If POPUP is non-nil, the menu is bound to the XEmacs `mode-popup-menu'
1279 variable, i.e. it will popup when pressing the right mouse button.
1281 Please see the variable `erlang-menu-base-items'."
1282 (cond (erlang-xemacs-p
1283 (let ((menu (erlang-menu-xemacs name items keymap)))
1284 ;; We add the menu to the global menubar.
1285 (funcall (symbol-function 'add-submenu) nil menu)
1286 (setcdr erlang-xemacs-popup-menu (cdr menu))
1287 (if (and popup (boundp 'mode-popup-menu))
1288 (funcall (symbol-function 'set)
1289 'mode-popup-menu erlang-xemacs-popup-menu))))
1290 ((>= erlang-emacs-major-version 19)
1291 (define-key keymap (vector 'menu-bar (intern name))
1292 (erlang-menu-make-keymap name items)))
1293 (t nil)))
1296 (defun erlang-menu-make-keymap (name items)
1297 "Build a menu for Emacs 19."
1298 (let ((menumap (funcall (symbol-function 'make-sparse-keymap)
1299 name))
1300 (count 0)
1301 id def first second third)
1302 (setq items (reverse items))
1303 (while items
1304 ;; Replace any occurrence of atoms by their value.
1305 (while (and items (atom (car items)) (not (null (car items))))
1306 (if (and (boundp (car items))
1307 (listp (symbol-value (car items))))
1308 (setq items (append (reverse (symbol-value (car items)))
1309 (cdr items)))
1310 (setq items (cdr items))))
1311 (setq first (car-safe (car items)))
1312 (setq second (car-safe (cdr-safe (car items))))
1313 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1314 (cond ((null first)
1315 (setq count (+ count 1))
1316 (setq id (intern (format "separator-%d" count)))
1317 (setq def '("--" . nil)))
1318 ((and (consp second) (eq (car second) 'lambda))
1319 (setq count (+ count 1))
1320 (setq id (intern (format "lambda-%d" count)))
1321 (setq def (cons first second)))
1322 ((symbolp second)
1323 (setq id second)
1324 (setq def (cons first second)))
1326 (setq count (+ count 1))
1327 (setq id (intern (format "submenu-%d" count)))
1328 (setq def (erlang-menu-make-keymap first second))))
1329 (define-key menumap (vector id) def)
1330 (if third
1331 (put id 'menu-enable third))
1332 (setq items (cdr items)))
1333 (cons name menumap)))
1336 (defun erlang-menu-xemacs (name items &optional keymap)
1337 "Build a menu for XEmacs."
1338 (let ((res '())
1339 first second third entry)
1340 (while items
1341 ;; Replace any occurrence of atoms by their value.
1342 (while (and items (atom (car items)) (not (null (car items))))
1343 (if (and (boundp (car items))
1344 (listp (symbol-value (car items))))
1345 (setq items (append (reverse (symbol-value (car items)))
1346 (cdr items)))
1347 (setq items (cdr items))))
1348 (setq first (car-safe (car items)))
1349 (setq second (car-safe (cdr-safe (car items))))
1350 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1351 (cond ((null first)
1352 (setq res (cons "------" res)))
1353 ((symbolp second)
1354 (setq res (cons (vector first second (or third t)) res)))
1355 ((and (consp second) (eq (car second) 'lambda))
1356 (setq res (cons (vector first (list 'call-interactively second)
1357 (or third t)) res)))
1359 (setq res (cons (cons first
1360 (cdr (erlang-menu-xemacs
1361 first second)))
1362 res))))
1363 (setq items (cdr items)))
1364 (setq res (reverse res))
1365 ;; When adding a menu to a minor-mode keymap under Emacs,
1366 ;; it disappears when the mode is disabled. The expression
1367 ;; generated below imitates this behaviour.
1368 ;; (This could be expressed much clearer using backquotes,
1369 ;; but I don't want to pull in every package.)
1370 (if keymap
1371 (let ((expr (list 'or
1372 (list 'eq keymap 'global-map)
1373 (list 'eq keymap (list 'current-local-map))
1374 (list 'symbol-value
1375 (list 'car-safe
1376 (list 'rassq
1377 keymap
1378 'minor-mode-map-alist))))))
1379 (setq res (cons ':included (cons expr res)))))
1380 (cons name res)))
1383 (defun erlang-menu-substitute (items alist)
1384 "Substitute functions in menu described by ITEMS.
1386 The menu ITEMS is updated destructively.
1388 ALIST is list of pairs where the car is the old function and cdr the new."
1389 (let (first second pair)
1390 (while items
1391 (setq first (car-safe (car items)))
1392 (setq second (car-safe (cdr-safe (car items))))
1393 (cond ((null first))
1394 ((symbolp second)
1395 (setq pair (and second (assq second alist)))
1396 (if pair
1397 (setcar (cdr (car items)) (cdr pair))))
1398 ((and (consp second) (eq (car second) 'lambda)))
1400 (erlang-menu-substitute second alist)))
1401 (setq items (cdr items)))))
1404 (defun erlang-menu-add-above (entry above items)
1405 "Add menu ENTRY above menu entry ABOVE in menu ITEMS.
1406 Do nothing if the items already should be in the menu.
1407 Should ABOVE not be in the list, the entry is added at
1408 the bottom of the menu.
1410 The new menu is returned. No guarantee is given that the original
1411 menu is left unchanged.
1413 The equality test is performed by `eq'.
1415 Example: (erlang-menu-add-above 'my-erlang-menu-items
1416 'erlang-menu-man-items)"
1417 (erlang-menu-add-below entry above items t))
1420 (defun erlang-menu-add-below (entry below items &optional above-p)
1421 "Add menu ENTRY below menu items BELOW in the Erlang menu.
1422 Do nothing if the items already should be in the menu.
1423 Should BELOW not be in the list, items is added at the bottom
1424 of the menu.
1426 The new menu is returned. No guarantee is given that the original
1427 menu is left unchanged.
1429 The equality test is performed by `eq'.
1431 Example:
1433 \(setq erlang-menu-items
1434 (erlang-menu-add-below 'my-erlang-menu-items
1435 'erlang-menu-base-items
1436 erlang-menu-items))"
1437 (if (memq entry items)
1438 items ; Return the original menu.
1439 (let ((head '())
1440 (done nil)
1441 res)
1442 (while (not done)
1443 (cond ((null items)
1444 (setq res (append head (list entry)))
1445 (setq done t))
1446 ((eq below (car items))
1447 (setq res
1448 (if above-p
1449 (append head (cons entry items))
1450 (append head (cons (car items)
1451 (cons entry (cdr items))))))
1452 (setq done t))
1454 (setq head (append head (list (car items))))
1455 (setq items (cdr items)))))
1456 res)))
1458 (defun erlang-menu-delete (entry items)
1459 "Delete ENTRY from menu ITEMS.
1461 The new menu is returned. No guarantee is given that the original
1462 menu is left unchanged."
1463 (delq entry items))
1465 ;; Man code:
1467 (defun erlang-man-init ()
1468 "Add menus containing the manual pages of the Erlang.
1470 The variable `erlang-man-dirs' contains entries describing
1471 the location of the manual pages."
1472 (interactive)
1473 (if erlang-man-inhibit
1475 (setq erlang-menu-man-items
1476 '(nil
1477 ("Man - Function" erlang-man-function)))
1478 (if erlang-man-dirs
1479 (setq erlang-menu-man-items
1480 (append erlang-menu-man-items
1481 (erlang-man-make-top-menu erlang-man-dirs))))
1482 (setq erlang-menu-items
1483 (erlang-menu-add-above 'erlang-menu-man-items
1484 'erlang-menu-version-items
1485 erlang-menu-items))
1486 (erlang-menu-init)))
1489 (defun erlang-man-uninstall ()
1490 "Remove the man pages from the Erlang mode."
1491 (interactive)
1492 (setq erlang-menu-items
1493 (erlang-menu-delete 'erlang-menu-man-items erlang-menu-items))
1494 (erlang-menu-init))
1497 ;; The man menu is a hierarchal structure, with the manual sections
1498 ;; at the top, described by `erlang-man-dirs'. The next level could
1499 ;; either be the manual pages if not to many, otherwise it is an index
1500 ;; menu whose submenus will contain up to `erlang-man-max-menu-size'
1501 ;; manual pages.
1503 (defun erlang-man-make-top-menu (dir-list)
1504 "Create one menu entry per element of DIR-LIST.
1505 The format is described in the documentation of `erlang-man-dirs'."
1506 (let ((menu '())
1507 dir)
1508 (while dir-list
1509 (setq dir (cond ((nth 2 (car dir-list))
1510 ;; Relative to `erlang-root-dir'.
1511 (and (stringp erlang-root-dir)
1512 (concat erlang-root-dir (nth 1 (car dir-list)))))
1514 ;; Absolute
1515 (nth 1 (car dir-list)))))
1516 (if (and dir
1517 (file-readable-p dir))
1518 (setq menu (cons (list (car (car dir-list))
1519 (erlang-man-make-middle-menu
1520 (erlang-man-get-files dir)))
1521 menu)))
1522 (setq dir-list (cdr dir-list)))
1523 ;; Should no menus be found, generate a menu item which
1524 ;; will display a help text, when selected.
1525 (if menu
1526 (nreverse menu)
1527 '(("Man Pages"
1528 (("Error! Why?" erlang-man-describe-error)))))))
1531 ;; Should the menu be to long, let's split it into a number of
1532 ;; smaller menus. Warning, this code contains beautiful
1533 ;; destructive operations!
1534 (defun erlang-man-make-middle-menu (filelist)
1535 "Create the second level menu from FILELIST.
1537 Should the list be longer than `erlang-man-max-menu-size', a tree of
1538 menus is created."
1539 (if (<= (length filelist) erlang-man-max-menu-size)
1540 (erlang-man-make-menu filelist)
1541 (let ((menu '())
1542 (filelist (copy-sequence filelist))
1543 segment submenu pair)
1544 (while filelist
1545 (setq pair (nthcdr (- erlang-man-max-menu-size 1) filelist))
1546 (setq segment filelist)
1547 (if (null pair)
1548 (setq filelist nil)
1549 (setq filelist (cdr pair))
1550 (setcdr pair nil))
1551 (setq submenu (erlang-man-make-menu segment))
1552 (setq menu (cons (list (concat (car (car submenu))
1553 " -- "
1554 (car (car (reverse submenu))))
1555 submenu)
1556 menu)))
1557 (nreverse menu))))
1560 (defun erlang-man-make-menu (filelist)
1561 "Make a leaf menu based on FILELIST."
1562 (let ((menu '())
1563 item)
1564 (while filelist
1565 (setq item (erlang-man-make-menu-item (car filelist)))
1566 (if item
1567 (setq menu (cons item menu)))
1568 (setq filelist (cdr filelist)))
1569 (nreverse menu)))
1572 (defun erlang-man-make-menu-item (file)
1573 "Create a menu item containing the name of the man page."
1574 (and (string-match ".*/\\([^/]+\\)\\.[^.]$" file)
1575 (let ((page (substring file (match-beginning 1) (match-end 1))))
1576 (list (capitalize page)
1577 (list 'lambda '()
1578 '(interactive)
1579 (list 'funcall 'erlang-man-display-function
1580 file))))))
1583 (defun erlang-man-get-files (dir)
1584 "Return files in directory DIR."
1585 (directory-files dir t ".*\\.[0-9]\\'"))
1588 (defun erlang-man-module (&optional module)
1589 "Find manual page for MODULE, defaults to module of function under point.
1590 This function is aware of imported functions."
1591 (interactive
1592 (list (let* ((mod (car-safe (erlang-get-function-under-point)))
1593 (input (read-string
1594 (format "Manual entry for module%s: "
1595 (if (or (null mod) (string= mod ""))
1597 (format " (default %s)" mod))))))
1598 (if (string= input "")
1600 input))))
1601 (or module (setq module (car (erlang-get-function-under-point))))
1602 (if (or (null module) (string= module ""))
1603 (error "No Erlang module name given"))
1604 (let ((dir-list erlang-man-dirs)
1605 (pat (concat "/" (regexp-quote module) "\\.[^.]$"))
1606 (file nil)
1607 file-list)
1608 (while (and dir-list (null file))
1609 (setq file-list (erlang-man-get-files
1610 (if (nth 2 (car dir-list))
1611 (concat erlang-root-dir (nth 1 (car dir-list)))
1612 (nth 1 (car dir-list)))))
1613 (while (and file-list (null file))
1614 (if (string-match pat (car file-list))
1615 (setq file (car file-list)))
1616 (setq file-list (cdr file-list)))
1617 (setq dir-list (cdr dir-list)))
1618 (if file
1619 (funcall erlang-man-display-function file)
1620 (error "No manual page for module %s found" module))))
1623 ;; Warning, the function `erlang-man-function' is a hack!
1624 ;; It links itself into the man code in a non-clean way. I have
1625 ;; chosen to keep it since it provides a very useful functionality
1626 ;; which is not possible to achieve using a clean approach.
1627 ;; / AndersL
1629 (defvar erlang-man-function-name nil
1630 "Name of function for last `erlang-man-function' call.
1631 Used for communication between `erlang-man-function' and the
1632 patch to `Man-notify-when-ready'.")
1634 (defun erlang-man-function (&optional name)
1635 "Find manual page for NAME, where NAME is module:function.
1636 The entry for `function' is displayed.
1638 This function is aware of imported functions."
1639 (interactive
1640 (list (let* ((mod-func (erlang-get-function-under-point))
1641 (mod (car-safe mod-func))
1642 (func (nth 1 mod-func))
1643 (input (read-string
1644 (format
1645 "Manual entry for `module:func' or `module'%s: "
1646 (if (or (null mod) (string= mod ""))
1648 (format " (default %s:%s)" mod func))))))
1649 (if (string= input "")
1650 (if (and mod func)
1651 (concat mod ":" func)
1652 mod)
1653 input))))
1654 ;; Emacs 18 doesn't provide `man'...
1655 (condition-case nil
1656 (require 'man)
1657 (error nil))
1658 (let ((modname nil)
1659 (funcname nil))
1660 (cond ((null name)
1661 (let ((mod-func (erlang-get-function-under-point)))
1662 (setq modname (car-safe mod-func))
1663 (setq funcname (nth 1 mod-func))))
1664 ((string-match ":" name)
1665 (setq modname (substring name 0 (match-beginning 0)))
1666 (setq funcname (substring name (match-end 0) nil)))
1667 ((stringp name)
1668 (setq modname name)))
1669 (if (or (null modname) (string= modname ""))
1670 (error "No Erlang module name given"))
1671 (cond ((fboundp 'Man-notify-when-ready)
1672 ;; Emacs 19: The man command could possibly start an
1673 ;; asynchronous process, i.e. we must hook ourselves into
1674 ;; the system to be activated when the man-process
1675 ;; terminates.
1676 (if (null funcname)
1678 (erlang-man-patch-notify)
1679 (setq erlang-man-function-name funcname))
1680 (condition-case nil
1681 (erlang-man-module modname)
1682 (error (setq erlang-man-function-name nil))))
1684 (erlang-man-module modname)
1685 (if funcname
1686 (erlang-man-find-function
1687 (or (get-buffer "*Manual Entry*") ; Emacs 18
1688 (current-buffer)) ; XEmacs
1689 funcname))))))
1692 ;; Should the defadvice be at the top level, the package `advice' would
1693 ;; be required. Now it is only required when this functionality
1694 ;; is used. (Emacs 19 specific.)
1695 (defun erlang-man-patch-notify ()
1696 "Patch the function `Man-notify-when-ready' to search for function.
1697 The variable `erlang-man-function-name' is assumed to be bound to
1698 the function name, or to nil.
1700 The reason for patching a function is that under Emacs 19, the man
1701 command is executed asynchronously."
1702 (condition-case nil
1703 (require 'advice)
1704 ;; This should never happened since this is only called when
1705 ;; running under Emacs 19.
1706 (error (error (concat "This command needs the package `advice', "
1707 "please upgrade your Emacs."))))
1708 (require 'man)
1709 (defadvice Man-notify-when-ready
1710 (after erlang-Man-notify-when-ready activate)
1711 "Set point at the documentation of the function name in
1712 `erlang-man-function-name' when the man page is displayed."
1713 (if erlang-man-function-name
1714 (erlang-man-find-function (ad-get-arg 0) erlang-man-function-name))
1715 (setq erlang-man-function-name nil)))
1718 (defun erlang-man-find-function (buf func)
1719 "Find manual page for function in `erlang-man-function-name' in buffer BUF."
1720 (if func
1721 (let ((win (get-buffer-window buf)))
1722 (if win
1723 (progn
1724 (set-buffer buf)
1725 (goto-char (point-min))
1726 (if (re-search-forward
1727 (concat "^[ \t]+" func " ?(")
1728 (point-max) t)
1729 (progn
1730 (forward-word -1)
1731 (set-window-point win (point)))
1732 (message "Could not find function `%s'" func)))))))
1735 (defun erlang-man-display (file)
1736 "Display FILE as a `man' file.
1737 This is the default manual page display function.
1738 The variables `erlang-man-display-function' contains the function
1739 to be used."
1740 ;; Emacs 18 doesn't `provide' man.
1741 (condition-case nil
1742 (require 'man)
1743 (error nil))
1744 (if file
1745 (let ((process-environment (copy-sequence process-environment)))
1746 (if (string-match "\\(.*\\)/man[^/]*/\\([^/]+\\)\\.[^.]$" file)
1747 (let ((dir (substring file (match-beginning 1) (match-end 1)))
1748 (page (substring file (match-beginning 2) (match-end 2))))
1749 (if (fboundp 'setenv)
1750 (setenv "MANPATH" dir)
1751 ;; Emacs 18
1752 (setq process-environment (cons (concat "MANPATH=" dir)
1753 process-environment)))
1754 (cond ((not (and (not erlang-xemacs-p)
1755 (= erlang-emacs-major-version 19)
1756 (< erlang-emacs-minor-version 29)))
1757 (manual-entry page))
1759 ;; Emacs 19.28 and earlier versions of 19:
1760 ;; The manual-entry command unconditionally prompts
1761 ;; the user :-(
1762 (funcall (symbol-function 'Man-getpage-in-background)
1763 page))))
1764 (error "Can't find man page for %s\n" file)))))
1767 (defun erlang-man-describe-error ()
1768 "Describe why the manual pages weren't found."
1769 (interactive)
1770 (with-output-to-temp-buffer "*Erlang Man Error*"
1771 (princ "Normally, this menu should contain Erlang manual pages.
1773 In order to find the manual pages, the variable `erlang-root-dir'
1774 should be bound to the name of the directory containing the Erlang
1775 installation. The name should not include the final slash.
1777 Practically, you should add a line on the following form to
1778 your ~/.emacs, or ask your system administrator to add it to
1779 the site init file:
1780 (setq erlang-root-dir \"/the/erlang/root/dir/goes/here\")
1782 For example:
1783 (setq erlang-root-dir \"/usr/local/erlang\")
1785 After installing the line, kill and restart Emacs, or restart Erlang
1786 mode with the command `M-x erlang-mode RET'.")))
1788 ;; Indentation code:
1790 (defun erlang-indent-command (&optional whole-exp)
1791 "Indent current line as Erlang code.
1792 With argument, indent any additional lines of the same clause
1793 rigidly along with this one."
1794 (interactive "P")
1795 (if whole-exp
1796 ;; If arg, always indent this line as Erlang
1797 ;; and shift remaining lines of clause the same amount.
1798 (let ((shift-amt (erlang-indent-line))
1799 beg end)
1800 (save-excursion
1801 (if erlang-tab-always-indent
1802 (beginning-of-line))
1803 (setq beg (point))
1804 (erlang-end-of-clause 1)
1805 (setq end (point))
1806 (goto-char beg)
1807 (forward-line 1)
1808 (setq beg (point)))
1809 (if (> end beg)
1810 (indent-code-rigidly beg end shift-amt "\n")))
1811 (if (and (not erlang-tab-always-indent)
1812 (save-excursion
1813 (skip-chars-backward " \t")
1814 (not (bolp))))
1815 (insert-tab)
1816 (erlang-indent-line))))
1819 (defun erlang-indent-line ()
1820 "Indent current line as Erlang code.
1821 Return the amount the indentation changed by."
1822 (let ((pos (- (point-max) (point)))
1823 indent beg
1824 shift-amt)
1825 (beginning-of-line 1)
1826 (setq beg (point))
1827 (skip-chars-forward " \t")
1828 (cond ((looking-at "%")
1829 (setq indent (funcall comment-indent-function))
1830 (setq shift-amt (- indent (current-column))))
1832 (setq indent (erlang-calculate-indent))
1833 (cond ((null indent)
1834 (setq indent (current-indentation)))
1835 ((eq indent t)
1836 ;; This should never occur here.
1837 (error "Erlang mode error"))
1838 ((= (char-syntax (following-char)) ?\))
1839 (setq indent (1- indent))))
1840 (setq shift-amt (- indent (current-column)))))
1841 (if (zerop shift-amt)
1843 (delete-region beg (point))
1844 (indent-to indent))
1845 ;; If initial point was within line's indentation, position
1846 ;; after the indentation. Else stay at same point in text.
1847 (if (> (- (point-max) pos) (point))
1848 (goto-char (- (point-max) pos)))
1849 shift-amt))
1852 (defun erlang-indent-region (beg end)
1853 "Indent region of Erlang code.
1855 This is automagically called by the user level function `indent-region'."
1856 (interactive "r")
1857 (save-excursion
1858 (let ((case-fold-search nil)
1859 (continue t)
1860 (from-end (- (point-max) end))
1861 indent-point;; The beginning of the current line
1862 indent;; The indent amount
1863 state)
1864 (goto-char beg)
1865 (beginning-of-line)
1866 (setq indent-point (point))
1867 (erlang-beginning-of-clause)
1868 ;; Parse the Erlang code from the beginning of the clause to
1869 ;; the beginning of the region.
1870 (while (< (point) indent-point)
1871 (setq state (erlang-partial-parse (point) indent-point state)))
1872 ;; Indent every line in the region
1873 (while continue
1874 (goto-char indent-point)
1875 (skip-chars-forward " \t")
1876 (cond ((looking-at "%")
1877 ;; Do not use our stack to help the user to customize
1878 ;; comment indentation.
1879 (setq indent (funcall comment-indent-function)))
1880 ((looking-at "$")
1881 ;; Don't indent empty lines.
1882 (setq indent 0))
1884 (setq indent
1885 (save-excursion
1886 (erlang-calculate-stack-indent (point) state)))
1887 (cond ((null indent)
1888 (setq indent (current-indentation)))
1889 ((eq indent t)
1890 ;; This should never occur here.
1891 (error "Erlang mode error"))
1892 ((= (char-syntax (following-char)) ?\))
1893 (setq indent (1- indent))))))
1894 (if (zerop (- indent (current-column)))
1896 (delete-region indent-point (point))
1897 (indent-to indent))
1898 ;; Find the next line in the region
1899 (goto-char indent-point)
1900 (save-excursion
1901 (forward-line 1)
1902 (setq indent-point (point)))
1903 (if (>= from-end (- (point-max) indent-point))
1904 (setq continue nil)
1905 (while (< (point) indent-point)
1906 (setq state (erlang-partial-parse
1907 (point) indent-point state))))))))
1910 (defun erlang-indent-current-buffer ()
1911 "Indent current buffer as Erlang code."
1912 (interactive)
1913 (save-excursion
1914 (save-restriction
1915 (widen)
1916 (erlang-indent-region (point-min) (point-max)))))
1919 (defun erlang-indent-function ()
1920 "Indent current Erlang function."
1921 (interactive)
1922 (save-excursion
1923 (let ((end (progn (erlang-end-of-function 1) (point)))
1924 (beg (progn (erlang-beginning-of-function 1) (point))))
1925 (erlang-indent-region beg end))))
1928 (defun erlang-indent-clause ()
1929 "Indent current Erlang clause."
1930 (interactive)
1931 (save-excursion
1932 (let ((end (progn (erlang-end-of-clause 1) (point)))
1933 (beg (progn (erlang-beginning-of-clause 1) (point))))
1934 (erlang-indent-region beg end))))
1937 (defmacro erlang-push (x stack) (list 'setq stack (list 'cons x stack)))
1938 (defmacro erlang-pop (stack) (list 'setq stack (list 'cdr stack)))
1939 ;; Would much prefer to make caddr a macro but this clashes.
1940 (defun erlang-caddr (x) (car (cdr (cdr x))))
1943 (defun erlang-calculate-indent (&optional parse-start)
1944 "Compute appropriate indentation for current line as Erlang code.
1945 Return nil if line starts inside string, t if in a comment."
1946 (save-excursion
1947 (let ((indent-point (point))
1948 (case-fold-search nil)
1949 (state nil))
1950 (if parse-start
1951 (goto-char parse-start)
1952 (erlang-beginning-of-clause))
1953 (while (< (point) indent-point)
1954 (setq state (erlang-partial-parse (point) indent-point state)))
1955 (erlang-calculate-stack-indent indent-point state))))
1957 (defun erlang-show-syntactic-information ()
1958 "Show syntactic information for current line."
1960 (interactive)
1962 (save-excursion
1963 (let ((starting-point (point))
1964 (case-fold-search nil)
1965 (state nil))
1966 (erlang-beginning-of-clause)
1967 (while (< (point) starting-point)
1968 (setq state (erlang-partial-parse (point) starting-point state)))
1969 (message "%S" state))))
1972 (defun erlang-partial-parse (from to &optional state)
1973 "Parse Erlang syntax starting at FROM until TO, with an optional STATE.
1974 Value is list (stack token-start token-type in-what)."
1975 (goto-char from) ; Start at the beginning
1976 (erlang-skip-blank to)
1977 (let ((cs (char-syntax (following-char)))
1978 (stack (car state))
1979 (token (point))
1980 in-what)
1981 (cond
1983 ;; Done: Return previous state.
1984 ((>= token to)
1985 (setq token (nth 1 state))
1986 (setq cs (nth 2 state))
1987 (setq in-what (nth 3 state)))
1989 ;; Word constituent: check and handle keywords.
1990 ((= cs ?w)
1991 (cond ((looking-at "\\(end\\|after\\)[^_a-zA-Z0-9]")
1992 ;; Must pop top icr layer, `after' will push a new
1993 ;; layer next.
1994 (progn
1995 (while (and stack (eq (car (car stack)) '->))
1996 (erlang-pop stack))
1997 (if (and stack (memq (car (car stack)) '(icr begin)))
1998 (erlang-pop stack))))
1999 ((looking-at "catch[^,\n\\of]*\n")
2000 ;; Must pop top icr layer, `catch' in try/catch
2001 ;;will push a new layer next.
2002 (progn
2003 (while (and stack (eq (car (car stack)) '->))
2004 (erlang-pop stack))
2005 (if (and stack (memq (car (car stack)) '(icr begin)))
2006 (erlang-pop stack))))
2007 ;;((looking-at "^of$")
2008 ;; Must pop top icr layer, `of' in try/catch
2009 ;;will push a new layer next.
2010 ;; (progn
2011 ;; (while (and stack (eq (car (car stack)) '->))
2012 ;; (erlang-pop stack))
2013 ;; (if (and stack (memq (car (car stack)) '(icr begin)))
2014 ;; (erlang-pop stack))))
2016 (cond ((looking-at "\\(if\\|case\\|receive\\|try\\)[^_a-zA-Z0-9]")
2017 ;; Must push a new icr (if/case/receive) layer.
2018 (erlang-push (list 'icr token (current-column)) stack))
2019 ((looking-at "\\(fun\\)[^_a-zA-Z0-9]")
2020 ;; Push a new icr layer if we are defining a `fun'
2021 ;; expression, not when we are refering an existing
2022 ;; function.
2023 (if (save-excursion
2024 (goto-char (match-end 1))
2025 (erlang-skip-blank to)
2026 (eq (following-char) ?\())
2027 (erlang-push (list 'icr token (current-column)) stack)))
2028 ((looking-at "\\(begin\\|query\\)[^_a-zA-Z0-9]")
2029 (erlang-push (list 'begin token (current-column)) stack))
2030 ;; In test suites you may want to do something like
2031 ;; ?match(Mem when integer(Mem), mnesia:table_info(Tab,
2032 ;; memory)), and then the following if/case/receive
2033 ;; statement will mess up the indentation by fooling the
2034 ;; erlang mode to think the 'when' in the argument is a
2035 ;; "real" when. The following three clauses will avoid
2036 ;; this problem.
2037 ((looking-at "when[^->\.]*if[^->\.]*->"))
2038 ((looking-at "when[^->\.]*case[^->\.]*->"))
2039 ((looking-at "when[^->\.]*receive[^->\.]*->"))
2040 ;; Normal when case
2041 ((looking-at "when [^->\.]*->")
2042 (erlang-push (list 'when token (current-column)) stack))
2043 ((looking-at "after[.]+->")
2044 (erlang-push (list 'icr token (current-column)) stack))
2045 ((looking-at "after[^_a-zA-Z0-9->]")
2046 ;; Probably in try-statment, fake "->" to get right
2047 ;; indentation in erlang-calculate-stack-indent. If it
2048 ;; was an ordinary catch without try, these entries will
2049 ;; be popped of the stack at a later ocaccion.
2050 (erlang-push (list 'icr token (current-column)) stack)
2051 (erlang-push (list '-> token (current-column)) stack))
2052 ((looking-at "catch[^,\n\\of]*\n")
2053 (erlang-push (list 'icr token (current-column)) stack)
2054 (erlang-push (list '-> token (current-column)) stack))
2055 ;;((looking-at "^of$")
2056 ;; (erlang-push (list 'icr token (current-column)) stack)
2057 ;;(erlang-push (list '-> token (current-column)) stack))
2059 (forward-sexp 1))
2060 ;; String: Try to skip over it. (Catch error if not complete.)
2061 ((= cs ?\")
2062 (condition-case nil
2063 (progn
2064 (forward-sexp 1)
2065 (if (> (point) to)
2066 (progn
2067 (setq in-what 'string)
2068 (goto-char to))))
2069 (error
2070 (setq in-what 'string)
2071 (goto-char to))))
2073 ;; Expression prefix e.i. $ or ^ (Note ^ can be in the character
2074 ;; literal $^ or part of string and $ outside of a string denotes
2075 ;; a character literal)
2076 ((= cs ?')
2077 (cond
2078 ((= (following-char) ?\") ;; $ or ^ was the last char in a string
2079 (forward-char 1))
2081 ;; Maybe a character literal, quote the next char to avoid
2082 ;; situations as $" being seen as the begining of a string.
2083 ;; Note the quoting something in the middle of a string is harmless.
2084 (quote (following-char))
2085 (forward-char 1))))
2087 ;; Symbol constituent or punctuation
2089 ((memq cs '(?. ?_))
2090 (cond
2092 ;; Clause end
2093 ((= (following-char) ?\;)
2094 (if (and stack (eq (car (car stack)) '->))
2095 (erlang-pop stack))
2096 (forward-char 1))
2098 ;; Function end
2099 ((looking-at "\\.\\(\\s \\|\n\\|\\s<\\)")
2100 (setq stack nil)
2101 (forward-char 1))
2103 ;; Function head
2104 ((looking-at "->\\|:-")
2105 (save-excursion
2106 (back-to-indentation)
2107 (cond ((looking-at "after[^_a-zA-Z0-9]")
2108 (erlang-pop stack))))
2109 (if (and stack (eq (car (car stack)) 'when))
2110 (erlang-pop stack))
2111 (erlang-push (list '-> token (current-column)) stack)
2112 (forward-char 2))
2114 ;; List-comprehension divider
2115 ((looking-at "||")
2116 (erlang-push (list '|| token (current-column)) stack)
2117 (forward-char 2))
2119 ;;((looking-at ",$")
2120 ;; Normal catch not try-catch have caused icr
2121 ;; and then incr and faked "->" should be removed
2122 ;; (save-excursion
2123 ;; (back-to-indentation)
2124 ;; (cond ((looking-at "catch[^_a-zA-Z0-9]")
2125 ;; (erlang-pop stack)
2126 ;; (erlang-pop stack))))
2127 ;;(forward-char 1))
2129 ;; Parameter separator
2130 ((looking-at ",")
2131 (forward-char 1))
2133 ;; Bit-syntax open paren
2134 ((looking-at "<<")
2135 (erlang-push (list '\( token (current-column)) stack)
2136 (forward-char 2))
2138 ;; Bbit-syntax close paren
2139 ((looking-at ">>")
2140 (while (memq (car (car stack)) '(|| ->))
2141 (erlang-pop stack))
2142 (cond ((eq (car (car stack)) '\()
2143 (erlang-pop stack))
2144 ((memq (car (car stack)) '(icr begin))
2145 (error "Missing `end'"))
2147 (error "Unbalanced parentheses")))
2148 (forward-char 2))
2150 ;; Macro
2151 ((= (following-char) ??)
2152 ;; Skip over the ?
2153 (forward-char 1)
2156 ;; Other punctuation: Skip over it and any following punctuation
2157 ((= cs ?.)
2158 ;; Skip over all characters in the operand.
2159 (skip-syntax-forward "."))
2161 ;; Other char: Skip over it.
2163 (forward-char 1))))
2165 ;; Open parenthesis
2166 ((= cs ?\()
2167 (erlang-push (list '\( token (current-column)) stack)
2168 (forward-char 1))
2170 ;; Close parenthesis
2171 ((= cs ?\))
2172 (while (memq (car (car stack)) '(|| ->))
2173 (erlang-pop stack))
2174 (cond ((eq (car (car stack)) '\()
2175 (erlang-pop stack))
2176 ((eq (car (car stack)) 'icr)
2177 (erlang-pop stack)
2178 ;; Normal catch not try-catch might have caused icr
2179 ;; and then incr should be removed and is not an error.
2180 (if (eq (car (car stack)) '\()
2181 (erlang-pop stack)
2182 (else
2183 (error "Missing `end'"))
2185 ((eq (car (car stack)) 'begin)
2186 (error "Missing `end'")
2188 (error "Unbalanced parenthesis"))
2190 (forward-char 1))
2192 ;; Character quote: Skip it and the quoted char.
2193 ((= cs ?/)
2194 (forward-char 2))
2196 ;; Character escape: Skip it and the escape sequence.
2197 ((= cs ?\\)
2198 (forward-char 1)
2199 (skip-syntax-forward "w"))
2201 ;; Everything else
2203 (forward-char 1)))
2204 (list stack token cs in-what)))
2206 (defun erlang-calculate-stack-indent (indent-point state)
2207 "From the given last position and state (stack) calculate indentation.
2208 Return nil if inside string, t if in a comment."
2209 (let* ((stack (and state (car state)))
2210 (token (nth 1 state))
2211 (stack-top (and stack (car stack))))
2212 (cond ((null state) ;No state
2214 ((nth 3 state)
2215 ;; Return nil or t.
2216 (eq (nth 3 state) 'comment))
2217 ((null stack)
2218 (if (looking-at "when[^_a-zA-Z0-9]")
2219 erlang-indent-guard
2221 ((eq (car stack-top) '\()
2222 ;; Element of list, tuple or part of an expression,
2223 (if (null erlang-argument-indent)
2224 ;; indent to next column.
2225 (1+ (nth 2 stack-top))
2226 (goto-char (nth 1 stack-top))
2227 (cond ((looking-at "[({]\\s *\\($\\|%\\)")
2228 ;; Line ends with parenthesis.
2229 (+ (erlang-indent-find-preceding-expr)
2230 erlang-argument-indent))
2232 ;; Indent to the same column as the first
2233 ;; argument.
2234 (goto-char (1+ (nth 1 stack-top)))
2235 (skip-chars-forward " \t")
2236 (current-column)))))
2237 ((eq (car stack-top) 'icr)
2238 ;; The default indentation is the column of the option
2239 ;; directly following the keyword. (This does not apply to
2240 ;; `case'.) Should no option be on the same line, the
2241 ;; indentation is the indentation of the keyword +
2242 ;; `erlang-indent-level'.
2244 ;; `after' should be indented to the save level as the
2245 ;; corresponding receive.
2246 (if (looking-at "after[^_a-zA-Z0-9]")
2247 (nth 2 stack-top)
2248 (save-excursion
2249 (goto-char (nth 1 stack-top))
2250 (if (looking-at "case[^_a-zA-Z0-9]")
2251 (+ (nth 2 stack-top) erlang-indent-level)
2252 (skip-chars-forward "a-z")
2253 (skip-chars-forward " \t")
2254 (if (memq (following-char) '(?% ?\n))
2255 (+ (nth 2 stack-top) erlang-indent-level)
2256 (current-column)))))
2257 (if (looking-at "catch[^_a-zA-Z0-9]")
2258 (nth 2 stack-top)
2259 (save-excursion
2260 (goto-char (nth 1 stack-top))
2261 (if (looking-at "case[^_a-zA-Z0-9]")
2262 (+ (nth 2 stack-top) erlang-indent-level)
2263 (skip-chars-forward "a-z")
2264 (skip-chars-forward " \t")
2265 (if (memq (following-char) '(?% ?\n))
2266 (+ (nth 2 stack-top) erlang-indent-level)
2267 (current-column)))))
2269 ;; Real indentation, where operators create extra indentation etc.
2270 ((memq (car stack-top) '(-> || begin))
2271 (goto-char (nth 1 stack-top))
2272 ;; Check if there is more code after the '->' on the
2273 ;; same line. If so use this indentation as base, else
2274 ;; use parent indentation + 2 * level as base.
2275 (let ((off erlang-indent-level)
2276 (skip 2))
2277 (cond ((null (cdr stack))) ; Top level in function.
2278 ((eq (car stack-top) 'begin)
2279 (setq skip 5))
2280 ((eq (car stack-top) '->)
2281 (setq off (* 2 erlang-indent-level))))
2282 (let ((base (erlang-indent-find-base stack indent-point off skip)))
2283 ;; Look at last thing to see how we are to move relative
2284 ;; to the base.
2285 (goto-char token)
2286 (cond ((looking-at "||\\|,\\|->\\|:-")
2287 base)
2288 ((erlang-at-keyword)
2289 (+ (current-column) erlang-indent-level))
2290 ((or (= (char-syntax (following-char)) ?.)
2291 (erlang-at-operator))
2292 (+ base erlang-indent-level))
2294 (goto-char indent-point)
2295 (cond ((memq (following-char) '(?\( ?{))
2296 ;; Function application or record.
2297 (+ (erlang-indent-find-preceding-expr)
2298 erlang-argument-indent))
2299 ;; Empty line, or end; treat it as the end of
2300 ;; the block. (Here we have a choice: should
2301 ;; the user be forced to reindent continued
2302 ;; lines, or should the "end" be reindented?)
2303 ((looking-at "\\(end\\|after\\|catch\\)[^_a-zA-Z0-9]\\|$")
2304 (if (eq (car (car stack)) '->)
2305 (erlang-pop stack))
2306 (if stack
2307 (erlang-caddr (car stack))
2309 ;; Avoid treating comments a continued line.
2310 ((= (following-char) ?%)
2311 base)
2312 ;; Continued line (e.g. line beginning
2313 ;; with an operator.)
2314 (t (+ base erlang-indent-level)))))))
2316 ((eq (car stack-top) 'when)
2317 (goto-char (nth 1 stack-top))
2318 (if (looking-at "when\\s *\\($\\|%\\)")
2319 (progn
2320 (erlang-pop stack)
2321 (if (and stack (eq (nth 0 (car stack)) 'icr))
2322 (progn
2323 (goto-char (nth 1 (car stack)))
2324 (+ (nth 2 (car stack)) erlang-indent-guard
2325 ;; receive XYZ or receive
2326 ;; XYZ
2327 (if (looking-at "[a-z]+\\s *\\($\\|%\\)")
2328 erlang-indent-level
2329 (* 2 erlang-indent-level))))
2330 erlang-indent-guard))
2331 ;; "when" is followed by code, let's indent to the same
2332 ;; column.
2333 (forward-char 4) ; Skip "when"
2334 (skip-chars-forward " \t")
2335 (current-column))))))
2338 (defun erlang-indent-find-base (stack indent-point &optional offset skip)
2339 "Find the base column for current stack."
2340 (or skip (setq skip 2))
2341 (or offset (setq offset erlang-indent-level))
2342 (save-excursion
2343 (let* ((stack-top (car stack)))
2344 (goto-char (nth 1 stack-top))
2345 (forward-char skip)
2346 (if (looking-at "\\s *\\($\\|%\\)")
2347 (progn
2348 (if (memq (car stack-top) '(-> ||))
2349 (erlang-pop stack))
2350 ;; Take parent identation + offset,
2351 ;; else just erlang-indent-level if no parent
2352 (if stack
2353 (+ (erlang-caddr (car stack))
2354 offset)
2355 erlang-indent-level))
2356 (erlang-skip-blank indent-point)
2357 (current-column)))))
2360 ;; Does not handle `begin' .. `end'.
2361 (defun erlang-indent-find-preceding-expr ()
2362 "Return the first column of the preceding expression.
2363 This assumes that the preceding expression is either simple
2364 \(i.e. an atom) or parenthesized."
2365 (save-excursion
2366 (forward-sexp -1)
2367 (let ((col (current-column)))
2368 (skip-chars-backward " \t")
2369 ;; Needed to match the colon in "'foo':'bar'".
2370 (if (not (memq (preceding-char) '(?# ?:)))
2372 (backward-char 1)
2373 (forward-sexp -1)
2374 (current-column)))))
2377 (defun erlang-skip-blank (&optional lim)
2378 "Skip over whitespace and comments until limit reached."
2379 (or lim (setq lim (point-max)))
2380 (let (stop)
2381 (while (and (not stop) (< (point) lim))
2382 (cond ((= (following-char) ?%)
2383 (skip-chars-forward "^\n" lim))
2384 ((= (following-char) ?\n)
2385 (skip-chars-forward "\n" lim))
2386 ((looking-at "\\s ")
2387 (if (re-search-forward "\\S " lim 'move)
2388 (forward-char -1)))
2390 (setq stop t))))
2391 stop))
2393 (defun erlang-at-keyword ()
2394 "Are we looking at an Erlang keyword which will increase indentation?"
2395 (looking-at (concat "\\(when\\|if\\|fun\\|case\\|begin\\|query\\|"
2396 "of\\|receive\\|after\\|catch\\)[^_a-zA-Z0-9]")))
2398 (defun erlang-at-operator ()
2399 "Are we looking at an Erlang operator?"
2400 (looking-at
2401 "\\(bnot\\|div\\|mod\\|band\\|bor\\|bxor\\|bsl\\|bsr\\)[^_a-zA-Z0-9]"))
2403 (defun erlang-comment-indent ()
2404 "Compute Erlang comment indentation.
2406 Used both by `indent-for-comment' and the Erlang specific indentation
2407 commands."
2408 (cond ((looking-at "%%%") 0)
2409 ((looking-at "%%")
2410 (or (erlang-calculate-indent)
2411 (current-indentation)))
2413 (save-excursion
2414 (skip-chars-backward " \t")
2415 (max (if (bolp) 0 (1+ (current-column)))
2416 comment-column)))))
2418 ;;; Erlang movement commands
2420 ;; All commands below work as movement commands. I.e. if the point is
2421 ;; at the end of the clause, and the command `erlang-end-of-clause' is
2422 ;; executed, the point is moved to the end of the NEXT clause. (This
2423 ;; mimics the behaviour of `end-of-defun'.)
2425 ;; Personally I would like to rewrite them to be "pure", and add a set
2426 ;; of movement functions, like `erlang-next-clause',
2427 ;; `erlang-previous-clause', and the same for functions.
2429 ;; The current implementation makes it hopeless to use the functions as
2430 ;; subroutines in more complex commands. /andersl
2432 (defun erlang-beginning-of-clause (&optional arg)
2433 "Move backward to previous start of clause.
2434 With argument, do this that many times.
2435 Return t unless search stops due to end of buffer."
2436 (interactive "p")
2437 (or arg (setq arg 1))
2438 (if (< arg 0)
2439 ;; Step back to the end of the previous line, unless we are at
2440 ;; the beginning of the buffer. The reason for this move is
2441 ;; that the regexp below includes the last character of the
2442 ;; previous line.
2443 (if (bobp)
2444 (or (looking-at "\n")
2445 (forward-char 1))
2446 (forward-char -1)
2447 (if (looking-at "\\`\n")
2448 (forward-char 1))))
2449 ;; The regexp matches a function header that isn't
2450 ;; included in a string.
2451 (and (re-search-forward "\\(\\`\\|\\`\n\\|[^\\]\n\\)\\([a-z]\\|'\\|-\\)"
2452 nil 'move (- arg))
2453 (let ((beg (match-beginning 2)))
2454 (and beg (goto-char beg))
2455 t)))
2457 (defun erlang-end-of-clause (&optional arg)
2458 "Move to the end of the current clause.
2459 With argument, do this that many times."
2460 (interactive "p")
2461 (or arg (setq arg 1))
2462 (while (and (looking-at "[ \t]*[%\n]")
2463 (zerop (forward-line 1))))
2464 ;; Move to the next clause.
2465 (erlang-beginning-of-clause (- arg))
2466 (beginning-of-line);; Just to be sure...
2467 (let ((continue t))
2468 (while (and (not (bobp)) continue)
2469 (forward-line -1)
2470 (skip-chars-forward " \t")
2471 (if (looking-at "[%\n]")
2473 (end-of-line)
2474 (setq continue nil)))))
2476 (defun erlang-mark-clause ()
2477 "Put mark at end of clause, point at beginning."
2478 (interactive)
2479 (push-mark (point))
2480 (erlang-end-of-clause 1)
2481 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2482 ;; the region.
2483 (condition-case nil
2484 (push-mark (point) nil t)
2485 (error (push-mark (point))))
2486 (erlang-beginning-of-clause 1)
2487 ;; The above function deactivates the mark.
2488 (if (boundp 'deactivate-mark)
2489 (funcall (symbol-function 'set) 'deactivate-mark nil)))
2491 (defun erlang-beginning-of-function (&optional arg)
2492 "Move backward to previous start of function.
2493 With positive argument, do this that many times.
2494 With negative argument, search forward.
2496 Return t unless search stops due to end of buffer."
2497 (interactive "p")
2498 (or arg (setq arg 1))
2499 (cond
2500 ;; Search backward
2501 ((> arg 0)
2502 (while (and (> arg 0)
2503 (and (erlang-beginning-of-clause 1)
2504 (let ((start (point))
2505 (name (erlang-name-of-function))
2506 (arity (erlang-get-function-arity)))
2507 ;; Note: "arity" is nil for e.g. "-import", hence
2508 ;; two "-import" clauses are not considered to
2509 ;; be part of the same function.
2510 (while (and (erlang-beginning-of-clause 1)
2511 (string-equal name
2512 (erlang-name-of-function))
2513 arity
2514 (equal arity
2515 (erlang-get-function-arity)))
2516 (setq start (point)))
2517 (goto-char start)
2518 t)))
2519 (setq arg (1- arg))))
2520 ;; Search forward
2521 ((< arg 0)
2522 (end-of-line)
2523 (erlang-beginning-of-clause 1)
2524 ;; Step -arg functions forward.
2525 (while (and (< arg 0)
2526 ;; Step one function forward, or stop if the end of
2527 ;; the buffer was reached. Return t if we found the
2528 ;; function.
2529 (let ((name (erlang-name-of-function))
2530 (arity (erlang-get-function-arity))
2531 (found (erlang-beginning-of-clause -1)))
2532 (while (and found
2533 (string-equal name (erlang-name-of-function))
2534 arity
2535 (equal arity
2536 (erlang-get-function-arity)))
2537 (setq found (erlang-beginning-of-clause -1)))
2538 found))
2539 (setq arg (1+ arg)))))
2540 (zerop arg))
2543 (defun erlang-end-of-function (&optional arg)
2544 "Move forward to next end of function.
2546 With argument, do this that many times.
2547 With negative argument go towards the beginning of the buffer."
2548 (interactive "p")
2549 (or arg (setq arg 1))
2550 (let ((first t))
2551 ;; Forward
2552 (while (and (> arg 0) (< (point) (point-max)))
2553 (let ((pos (point)))
2554 (while (progn
2555 (if (and first
2556 (progn
2557 (forward-char 1)
2558 (erlang-beginning-of-clause 1)))
2560 (or (bobp) (forward-char -1))
2561 (erlang-beginning-of-clause -1))
2562 (setq first nil)
2563 (erlang-pass-over-function)
2564 (skip-chars-forward " \t")
2565 (if (looking-at "[%\n]")
2566 (forward-line 1))
2567 (<= (point) pos))))
2568 (setq arg (1- arg)))
2569 ;; Backward
2570 (while (< arg 0)
2571 (let ((pos (point)))
2572 (erlang-beginning-of-clause 1)
2573 (erlang-pass-over-function)
2574 (forward-line 1)
2575 (if (>= (point) pos)
2576 (if (erlang-beginning-of-function 2)
2577 (progn
2578 (erlang-pass-over-function)
2579 (skip-chars-forward " \t")
2580 (if (looking-at "[%\n]")
2581 (forward-line 1)))
2582 (goto-char (point-min)))))
2583 (setq arg (1+ arg)))))
2585 (eval-and-compile
2586 (if (default-boundp 'beginning-of-defun-function)
2587 (defalias 'erlang-mark-function 'mark-defun)
2588 (defun erlang-mark-function ()
2589 "Put mark at end of function, point at beginning."
2590 (interactive)
2591 (push-mark (point))
2592 (erlang-end-of-function 1)
2593 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2594 ;; the region.
2595 (condition-case nil
2596 (push-mark (point) nil t)
2597 (error (push-mark (point))))
2598 (erlang-beginning-of-function 1)
2599 ;; The above function deactivates the mark.
2600 (if (boundp 'deactivate-mark)
2601 (funcall (symbol-function 'set) 'deactivate-mark nil)))))
2603 (defun erlang-pass-over-function ()
2604 (while (progn
2605 (erlang-skip-blank)
2606 (and (not (looking-at "\\.\\(\\s \\|\n\\|\\s<\\)"))
2607 (not (eobp))))
2608 (forward-sexp 1))
2609 (if (not (eobp))
2610 (forward-char 1)))
2612 (defun erlang-name-of-function ()
2613 (save-excursion
2614 ;; Skip over attribute leader.
2615 (if (looking-at "-[ \t]*")
2616 (re-search-forward "-[ \t]*" nil 'move))
2617 (let ((start (point)))
2618 (forward-sexp 1)
2619 (buffer-substring start (point)))))
2622 ;;; Miscellaneous
2624 (defun erlang-fill-paragraph (&optional justify)
2625 "Like \\[fill-paragraph], but handle Erlang comments.
2626 If any of the current line is a comment, fill the comment or the
2627 paragraph of it that point is in, preserving the comment's indentation
2628 and initial `%':s."
2629 (interactive "P")
2630 (let ((has-comment nil)
2631 ;; If has-comment, the appropriate fill-prefix for the comment.
2632 comment-fill-prefix)
2633 ;; Figure out what kind of comment we are looking at.
2634 (save-excursion
2635 (beginning-of-line)
2636 (cond
2637 ;; Find the command prefix.
2638 ((looking-at (concat "\\s *" comment-start-skip))
2639 (setq has-comment t)
2640 (setq comment-fill-prefix (buffer-substring (match-beginning 0)
2641 (match-end 0))))
2642 ;; A line with some code, followed by a comment? Remember that the
2643 ;; % which starts the comment shouldn't be part of a string or
2644 ;; character.
2645 ((progn
2646 (while (not (looking-at "%\\|$"))
2647 (skip-chars-forward "^%\n\"\\\\")
2648 (cond
2649 ((eq (char-after (point)) ?\\) (forward-char 2))
2650 ((eq (char-after (point)) ?\") (forward-sexp 1))))
2651 (looking-at comment-start-skip))
2652 (setq has-comment t)
2653 (setq comment-fill-prefix
2654 (concat (make-string (current-column) ? )
2655 (buffer-substring (match-beginning 0) (match-end 0)))))))
2656 (if (not has-comment)
2657 (fill-paragraph justify)
2658 ;; Narrow to include only the comment, and then fill the region.
2659 (save-restriction
2660 (narrow-to-region
2661 ;; Find the first line we should include in the region to fill.
2662 (save-excursion
2663 (while (and (zerop (forward-line -1))
2664 (looking-at "^\\s *%")))
2665 ;; We may have gone to far. Go forward again.
2666 (or (looking-at "^\\s *%")
2667 (forward-line 1))
2668 (point))
2669 ;; Find the beginning of the first line past the region to fill.
2670 (save-excursion
2671 (while (progn (forward-line 1)
2672 (looking-at "^\\s *%")))
2673 (point)))
2674 ;; Lines with only % on them can be paragraph boundaries.
2675 (let ((paragraph-start (concat paragraph-start "\\|^[ \t%]*$"))
2676 (paragraph-separate (concat paragraph-start "\\|^[ \t%]*$"))
2677 (fill-prefix comment-fill-prefix))
2678 (fill-paragraph justify))))))
2681 (defun erlang-uncomment-region (beg end)
2682 "Uncomment all commented lines in the region."
2683 (interactive "r")
2684 (comment-region beg end -1))
2687 (defun erlang-generate-new-clause ()
2688 "Create additional Erlang clause header.
2690 Parses the source file for the name of the current Erlang function.
2691 Create the header containing the name, A pair of parentheses,
2692 and an arrow. The space between the function name and the
2693 first parenthesis is preserved. The point is placed between
2694 the parentheses."
2695 (interactive)
2696 (let ((name (save-excursion
2697 (and (erlang-beginning-of-clause)
2698 (erlang-get-function-name t))))
2699 (arrow (save-excursion
2700 (and (erlang-beginning-of-clause)
2701 (erlang-get-function-arrow)))))
2702 (if (or (null arrow) (null name))
2703 (error "Can't find name of current Erlang function"))
2704 (if (and (bolp) (eolp))
2706 (end-of-line)
2707 (newline))
2708 (insert name)
2709 (save-excursion
2710 (insert ") " arrow))
2711 (if erlang-new-clause-with-arguments
2712 (erlang-clone-arguments))))
2715 (defun erlang-clone-arguments ()
2716 "Insert, at the point, the argument list of the previous clause.
2718 The mark is set at the beginning of the inserted text, the point
2719 at the end."
2720 (interactive)
2721 (let ((args (save-excursion
2722 (beginning-of-line)
2723 (and (erlang-beginning-of-clause)
2724 (erlang-get-function-arguments))))
2725 (p (point)))
2726 (if (null args)
2727 (error "Can't clone argument list"))
2728 (insert args)
2729 (set-mark p)))
2731 ;;; Information retrieval functions.
2733 (defun erlang-buffer-substring (beg end)
2734 "Like `buffer-substring-no-properties'.
2735 Although, this function works on all versions of Emacs."
2736 (if (fboundp 'buffer-substring-no-properties)
2737 (funcall (symbol-function 'buffer-substring-no-properties) beg end)
2738 (buffer-substring beg end)))
2741 (defun erlang-get-module ()
2742 "Return the name of the module as specified by `-module'.
2744 Return nil if file contains no `-module' attribute."
2745 (save-excursion
2746 (save-restriction
2747 (widen)
2748 (goto-char (point-min))
2749 (let ((md (match-data)))
2750 (unwind-protect
2751 (if (re-search-forward
2752 (eval-when-compile
2753 (concat "^-module\\s *(\\s *\\(\\("
2754 erlang-atom-regexp
2755 "\\)?\\)\\s *)\\s *\\."))
2756 (point-max) t)
2757 (erlang-remove-quotes
2758 (erlang-buffer-substring (match-beginning 1)
2759 (match-end 1)))
2760 nil)
2761 (store-match-data md))))))
2764 (defun erlang-get-module-from-file-name (&optional file)
2765 "Extract the module name from a file name.
2767 First, the directory part is removed. Second, the part of the file name
2768 matching `erlang-file-name-extension-regexp' is removed.
2770 Should the match fail, nil is returned.
2772 By modifying `erlang-file-name-extension-regexp' to match files other
2773 than Erlang source files, Erlang specific functions could be applied on
2774 non-Erlang files. Most notably; the support for Erlang modules in the
2775 tags system could be used by files written in other languages."
2776 (or file (setq file buffer-file-name))
2777 (if (null file)
2779 (setq file (file-name-nondirectory file))
2780 (if (string-match erlang-file-name-extension-regexp file)
2781 (substring file 0 (match-beginning 0))
2782 nil)))
2785 ;; Used by `erlang-get-export' and `erlang-get-import'.
2787 (defun erlang-get-function-arity-list ()
2788 "Parse list of `function/arity' as used by `-import' and `-export'.
2790 Point must be before the opening bracket. When the
2791 function returns the point will be placed after the closing bracket.
2793 The function does not return an error if the list is incorrectly
2794 formatted.
2796 Return list of (function . arity). The order of the returned list
2797 corresponds to the order of the parsed Erlang list."
2798 (let ((res '()))
2799 (erlang-skip-blank)
2800 (forward-char 1)
2801 (if (not (eq (preceding-char) ?\[))
2802 '() ; Not looking at an Erlang list.
2803 (while ; Note: `while' has no body.
2804 (progn
2805 (erlang-skip-blank)
2806 (and (looking-at (eval-when-compile
2807 (concat erlang-atom-regexp "/\\([0-9]+\\)\\>")))
2808 (progn
2809 (setq res (cons
2810 (cons
2811 (erlang-remove-quotes
2812 (erlang-buffer-substring
2813 (match-beginning 1) (match-end 1)))
2814 (string-to-int
2815 (erlang-buffer-substring
2816 (match-beginning
2817 (+ 1 erlang-atom-regexp-matches))
2818 (match-end
2819 (+ 1 erlang-atom-regexp-matches)))))
2820 res))
2821 (goto-char (match-end 0))
2822 (erlang-skip-blank)
2823 (forward-char 1)
2824 ;; Test if there are more exported functions.
2825 (eq (preceding-char) ?,))))))
2826 (nreverse res)))
2829 ;;; Note that `-export' and the open parenthesis must be written on
2830 ;;; the same line.
2832 (defun erlang-get-export ()
2833 "Return a list of `(function . arity)' as specified by `-export'."
2834 (save-excursion
2835 (goto-char (point-min))
2836 (let ((md (match-data))
2837 (res '()))
2838 (unwind-protect
2839 (progn
2840 (while (re-search-forward "^-export\\s *(" (point-max) t)
2841 (erlang-skip-blank)
2842 (setq res (nconc res (erlang-get-function-arity-list))))
2843 res)
2844 (store-match-data md)))))
2847 (defun erlang-get-import ()
2848 "Parse an Erlang source file for imported functions.
2850 Return an alist with module name as car part and list of conses containing
2851 function and arity as cdr part."
2852 (save-excursion
2853 (goto-char (point-min))
2854 (let ((md (match-data))
2855 (res '()))
2856 (unwind-protect
2857 (progn
2858 (while (re-search-forward "^-import\\s *(" (point-max) t)
2859 (erlang-skip-blank)
2860 (if (looking-at erlang-atom-regexp)
2861 (let ((module (erlang-remove-quotes
2862 (erlang-buffer-substring
2863 (match-beginning 0)
2864 (match-end 0)))))
2865 (goto-char (match-end 0))
2866 (erlang-skip-blank)
2867 (if (eq (following-char) ?,)
2868 (progn
2869 (forward-char 1)
2870 (erlang-skip-blank)
2871 (let ((funcs (erlang-get-function-arity-list))
2872 (pair (assoc module res)))
2873 (if pair
2874 (setcdr pair (nconc (cdr pair) funcs))
2875 (setq res (cons (cons module funcs)
2876 res)))))))))
2877 (nreverse res))
2878 (store-match-data md)))))
2881 (defun erlang-get-function-name (&optional arg)
2882 "Return name of current function, or nil.
2884 If optional argument is non-nil, everything up to and including
2885 the first `(' is returned.
2887 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2888 (save-excursion
2889 (if (not (eobp)) (forward-char 1))
2890 (and (erlang-beginning-of-clause)
2891 (erlang-get-function-name t)))"
2892 (let ((n (if arg 0 1)))
2893 (and (looking-at (eval-when-compile
2894 (concat "^" erlang-atom-regexp "\\s *(")))
2895 (erlang-buffer-substring (match-beginning n) (match-end n)))))
2898 (defun erlang-get-function-arrow ()
2899 "Return arrow of current function, could be \"->\", \":-\" or nil.
2901 The \":-\" arrow is used by mnesia queries.
2903 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2904 (save-excursion
2905 (if (not (eobp)) (forward-char 1))
2906 (and (erlang-beginning-of-clause)
2907 (erlang-get-function-arrow)))"
2908 (and
2909 (save-excursion
2910 (re-search-forward "[^-:]*-\\|:" (point-max) t)
2911 (erlang-buffer-substring (- (point) 1) (+ (point) 1)))))
2913 (defun erlang-get-function-arity ()
2914 "Return the number of arguments of function at point, or nil."
2915 (and (looking-at (eval-when-compile
2916 (concat "^" erlang-atom-regexp "\\s *(")))
2917 (save-excursion
2918 (goto-char (match-end 0))
2919 (condition-case nil
2920 (let ((res 0)
2921 (cont t))
2922 (while cont
2923 (cond ((eobp)
2924 (setq res nil)
2925 (setq cont nil))
2926 ((looking-at "\\s *)")
2927 (setq cont nil))
2928 ((looking-at "\\s *\\($\\|%\\)")
2929 (forward-line 1))
2930 ((looking-at "\\s *,")
2931 (setq res (+ 1 res))
2932 (goto-char (match-end 0)))
2934 (when (zerop res)
2935 (setq res (+ 1 res)))
2936 (forward-sexp 1))))
2937 res)
2938 (error nil)))))
2940 (defun erlang-get-function-arguments ()
2941 "Return arguments of current function, or nil."
2942 (if (not (looking-at (eval-when-compile
2943 (concat "^" erlang-atom-regexp "\\s *("))))
2945 (save-excursion
2946 (condition-case nil
2947 (let ((start (match-end 0)))
2948 (goto-char (- start 1))
2949 (forward-sexp)
2950 (erlang-buffer-substring start (- (point) 1)))
2951 (error nil)))))
2954 (defun erlang-get-function-under-point ()
2955 "Return the module and function under the point, or nil.
2957 Should no explicit module name be present at the point, the
2958 list of imported functions is searched.
2960 The following could be returned:
2961 (\"module\" \"function\") -- Both module and function name found.
2962 (nil \"function\") -- No module name was found.
2963 nil -- No function name found
2965 In the future the list may contain more elements."
2966 (save-excursion
2967 (let ((md (match-data))
2968 (res nil))
2969 (if (eq (char-syntax (following-char)) ? )
2970 (skip-chars-backward " \t"))
2971 (skip-chars-backward "a-zA-Z0-9_:'")
2972 (cond ((looking-at (eval-when-compile
2973 (concat erlang-atom-regexp ":" erlang-atom-regexp)))
2974 (setq res (list
2975 (erlang-remove-quotes
2976 (erlang-buffer-substring
2977 (match-beginning 1) (match-end 1)))
2978 (erlang-remove-quotes
2979 (erlang-buffer-substring
2980 (match-beginning (1+ erlang-atom-regexp-matches))
2981 (match-end (1+ erlang-atom-regexp-matches)))))))
2982 ((looking-at erlang-atom-regexp)
2983 (let ((fk (erlang-remove-quotes
2984 (erlang-buffer-substring
2985 (match-beginning 0) (match-end 0))))
2986 (mod nil)
2987 (imports (erlang-get-import)))
2988 (while (and imports (null mod))
2989 (if (assoc fk (cdr (car imports)))
2990 (setq mod (car (car imports)))
2991 (setq imports (cdr imports))))
2992 (setq res (list mod fk)))))
2993 (store-match-data md)
2994 res)))
2997 ;; TODO: Escape single quotes inside the string without
2998 ;; replace-regexp-in-string.
2999 (defun erlang-add-quotes-if-needed (str)
3000 "Return STR, possibly with quotes."
3001 (if (and (stringp str)
3002 (not (string-match (eval-when-compile
3003 (concat "\\`" erlang-atom-regexp "\\'")) str)))
3004 (progn (if (fboundp 'replace-regexp-in-string)
3005 (setq str (replace-regexp-in-string "'" "\\'" str t t )))
3006 (concat "'" str "'"))
3007 str))
3010 (defun erlang-remove-quotes (str)
3011 "Return STR without quotes, if present."
3012 (let ((md (match-data)))
3013 (prog1
3014 (if (string-match "\\`'\\(.*\\)'\\'" str)
3015 (substring str 1 -1)
3016 str)
3017 (store-match-data md))))
3020 ;;; Check module name
3022 ;; The function `write-file', bound to C-x C-w, calls
3023 ;; `set-visited-file-name' which clears the hook. :-(
3024 ;; To make sure that the hook always is present, we advise
3025 ;; `set-visited-file-name'.
3026 (defun erlang-check-module-name-init ()
3027 "Initialize the functionality to compare file and module names.
3029 Unless we have `before-save-hook', we redefine the function
3030 `set-visited-file-name' since it clears the variable
3031 `local-write-file-hooks'. The original function definition is
3032 stored in `erlang-orig-set-visited-file-name'."
3033 (if (boundp 'before-save-hook)
3034 ;; If we have that, `make-local-hook' is obsolete.
3035 (add-hook 'before-save-hook 'erlang-check-module-name nil t)
3036 (require 'advice)
3037 (unless (ad-advised-definition-p 'set-visited-file-name)
3038 (defadvice set-visited-file-name (after erlang-set-visited-file-name
3039 activate)
3040 (if (eq major-mode 'erlang-mode)
3041 (add-hook 'local-write-file-hooks 'erlang-check-module-name))))
3042 (add-hook 'local-write-file-hooks 'erlang-check-module-name)))
3045 (defun erlang-check-module-name ()
3046 "If the module name doesn't match file name, ask for permission to change.
3048 The variable `erlang-check-module-name' controls the behaviour of this
3049 function. It it is nil, this function does nothing. If it is t, the
3050 source is silently changed. If it is set to the atom `ask', the user
3051 is prompted.
3053 This function is normally placed in the hook `local-write-file-hooks'."
3054 (if erlang-check-module-name
3055 (let ((mn (erlang-get-module))
3056 (fn (erlang-get-module-from-file-name (buffer-file-name))))
3057 (if (and (stringp mn) (stringp fn))
3058 (or (string-equal mn fn)
3059 (if (or (eq erlang-check-module-name t)
3060 (y-or-n-p
3061 "Module does not match file name. Modify source? "))
3062 (save-excursion
3063 (save-restriction
3064 (widen)
3065 (goto-char (point-min))
3066 (if (re-search-forward
3067 (eval-when-compile
3068 (concat "^-module\\s *(\\s *\\(\\("
3069 erlang-atom-regexp
3070 "\\)?\\)\\s *)\\s *\\."))
3071 (point-max) t)
3072 (progn
3073 (goto-char (match-beginning 1))
3074 (delete-region (match-beginning 1)
3075 (match-end 1))
3076 (insert fn))))))))))
3077 ;; Must return nil since it is added to `local-write-file-hook'.
3078 nil)
3081 ;;; Electric functions.
3083 (defun erlang-electric-semicolon (&optional arg)
3084 "Insert a semicolon character and possibly a prototype for the next line.
3086 The variable `erlang-electric-semicolon-criteria' states a criterion,
3087 when fulfilled a newline is inserted, the next line is indented and a
3088 prototype for the next line is inserted. Normally the prototype
3089 consists of \" ->\". Should the semicolon end the clause a new clause
3090 header is generated.
3092 The variable `erlang-electric-semicolon-insert-blank-lines' controls
3093 the number of blank lines inserted between the current line and new
3094 function header.
3096 Behaves just like the normal semicolon when supplied with a
3097 numerical arg, point is inside string or comment, or when there are
3098 non-whitespace characters following the point on the current line."
3099 (interactive "P")
3100 (self-insert-command (prefix-numeric-value arg))
3101 (if (or arg
3102 (and (listp erlang-electric-commands)
3103 (not (memq 'erlang-electric-semicolon
3104 erlang-electric-commands)))
3105 (erlang-in-literal)
3106 (not (looking-at "\\s *\\(%.*\\)?$"))
3107 (null (erlang-test-criteria-list
3108 erlang-electric-semicolon-criteria)))
3109 (setq erlang-electric-newline-inhibit nil)
3110 (setq erlang-electric-newline-inhibit t)
3111 (undo-boundary)
3112 (end-of-line)
3113 (newline)
3114 (if (condition-case nil
3115 (progn (erlang-indent-line) t)
3116 (error (if (bolp) (delete-backward-char 1))))
3117 (if (not (bolp))
3118 (save-excursion
3119 (insert " ->"))
3120 (condition-case nil
3121 (progn
3122 (erlang-generate-new-clause)
3123 (if erlang-electric-semicolon-insert-blank-lines
3124 (save-excursion
3125 (beginning-of-line)
3126 (newline
3127 erlang-electric-semicolon-insert-blank-lines))))
3128 (error (if (bolp) (delete-backward-char 1))))))))
3131 (defun erlang-electric-comma (&optional arg)
3132 "Insert a comma character and possibly a new indented line.
3133 The variable `erlang-electric-comma-criteria' states a criterion,
3134 when fulfilled a newline is inserted and the next line is indented.
3136 Behaves just like the normal comma when supplied with a
3137 numerical arg, point is inside string or comment, or when there are
3138 non-whitespace characters following the point on the current line."
3139 (interactive "P")
3141 (self-insert-command (prefix-numeric-value arg))
3143 (if (or arg
3144 (and (listp erlang-electric-commands)
3145 (not (memq 'erlang-electric-comma erlang-electric-commands)))
3146 (erlang-in-literal)
3147 (not (looking-at "\\s *\\(%.*\\)?$"))
3148 (null (erlang-test-criteria-list
3149 erlang-electric-comma-criteria)))
3150 (setq erlang-electric-newline-inhibit nil)
3151 (setq erlang-electric-newline-inhibit t)
3152 (undo-boundary)
3153 (end-of-line)
3154 (newline)
3155 (condition-case nil
3156 (erlang-indent-line)
3157 (error (if (bolp) (delete-backward-char 1))))))
3159 (defun erlang-electric-lt (&optional arg)
3160 "Insert a less-than sign, and optionally mark it as an open paren."
3162 (interactive "p")
3164 (self-insert-command arg)
3166 ;; Was this the second char in bit-syntax open (`<<')?
3167 (unless (< (point) 2)
3168 (save-excursion
3169 (backward-char 2)
3170 (when (and (eq (char-after (point)) ?<)
3171 (not (eq (get-text-property (point) 'category)
3172 'bitsyntax-open-inner)))
3173 ;; Then mark the two chars...
3174 (put-text-property (point) (1+ (point))
3175 'category 'bitsyntax-open-outer)
3176 (forward-char 1)
3177 (put-text-property (point) (1+ (point))
3178 'category 'bitsyntax-open-inner)
3179 ;;...and unmark any subsequent less-than chars.
3180 (forward-char 1)
3181 (while (eq (char-after (point)) ?<)
3182 (remove-text-properties (point) (1+ (point))
3183 '(category nil))
3184 (forward-char 1))))))
3186 (defun erlang-after-bitsyntax-close ()
3187 "Return t if point is immediately after a bit-syntax close parenthesis (`>>')."
3188 (and (>= (point) 2)
3189 (save-excursion
3190 (backward-char 2)
3191 (and (eq (char-after (point)) ?>)
3192 (not (eq (get-text-property (point) 'category)
3193 'bitsyntax-close-outer))))))
3195 (defun erlang-after-arrow ()
3196 "Return true if point is immediately after a function arrow (`->')."
3197 (and (>= (point) 2)
3198 (and
3199 (save-excursion
3200 (backward-char)
3201 (eq (char-before (point)) ?-))
3202 (or (not (listp erlang-electric-commands))
3203 (memq 'erlang-electric-gt
3204 erlang-electric-commands))
3205 (not (erlang-in-literal))
3206 (looking-at "\\s *\\(%.*\\)?$")
3207 (erlang-test-criteria-list erlang-electric-arrow-criteria))))
3210 (defun erlang-electric-gt (&optional arg)
3211 "Insert a greater-than sign, and optionally mark it as a close paren."
3213 (interactive "p")
3215 (self-insert-command arg)
3217 (cond
3218 ;; Did we just write a bit-syntax close (`>>')?
3219 ((erlang-after-bitsyntax-close)
3220 (save-excursion
3221 ;; Then mark the two chars...
3222 (backward-char 2)
3223 (put-text-property (point) (1+ (point))
3224 'category 'bitsyntax-close-inner)
3225 (forward-char)
3226 (put-text-property (point) (1+ (point))
3227 'category 'bitsyntax-close-outer)
3228 ;;...and unmark any subsequent greater-than chars.
3229 (forward-char)
3230 (while (eq (char-after (point)) ?>)
3231 (remove-text-properties (point) (1+ (point))
3232 '(category nil))
3233 (forward-char))))
3235 ;; Did we just write a function arrow (`->')?
3236 ((erlang-after-arrow)
3237 (let ((erlang-electric-newline-inhibit t))
3238 (undo-boundary)
3239 (end-of-line)
3240 (newline)
3241 (condition-case nil
3242 (erlang-indent-line)
3243 (error (if (bolp) (delete-backward-char 1))))))
3245 ;; Then it's just a plain greater-than.
3247 nil)))
3250 (defun erlang-electric-arrow\ off (&optional arg)
3251 "Insert a '>'-sign and possibly a new indented line.
3253 This command is only `electric' when the `>' is part of an `->' arrow.
3254 The variable `erlang-electric-arrow-criteria' states a sequence of
3255 criteria, which decides when a newline should be inserted and the next
3256 line indented.
3258 It behaves just like the normal greater than sign when supplied with a
3259 numerical arg, point is inside string or comment, or when there are
3260 non-whitespace characters following the point on the current line.
3262 After being split/merged into `erlang-after-arrow' and
3263 `erlang-electric-gt', it is now unused and disabled."
3264 (interactive "P")
3265 (let ((prec (preceding-char)))
3266 (self-insert-command (prefix-numeric-value arg))
3267 (if (or arg
3268 (and (listp erlang-electric-commands)
3269 (not (memq 'erlang-electric-arrow
3270 erlang-electric-commands)))
3271 (not (eq prec ?-))
3272 (erlang-in-literal)
3273 (not (looking-at "\\s *\\(%.*\\)?$"))
3274 (null (erlang-test-criteria-list
3275 erlang-electric-arrow-criteria)))
3276 (setq erlang-electric-newline-inhibit nil)
3277 (setq erlang-electric-newline-inhibit t)
3278 (undo-boundary)
3279 (end-of-line)
3280 (newline)
3281 (condition-case nil
3282 (erlang-indent-line)
3283 (error (if (bolp) (delete-backward-char 1)))))))
3286 (defun erlang-electric-newline (&optional arg)
3287 "Break line at point and indent, continuing comment if within one.
3288 The variable `erlang-electric-newline-criteria' states a criterion,
3289 when fulfilled a newline is inserted and the next line is indented.
3291 Should the current line begin with a comment, and the variable
3292 `comment-multi-line' be non-nil, a new comment start is inserted.
3294 Should the previous command be another electric command we assume that
3295 the user pressed newline out of old habit, hence we will do nothing."
3296 (interactive "P")
3297 (cond ((and (not arg)
3298 erlang-electric-newline-inhibit
3299 (memq last-command erlang-electric-newline-inhibit-list))
3300 ()) ; Do nothing!
3301 ((or arg
3302 (and (listp erlang-electric-commands)
3303 (not (memq 'erlang-electric-newline
3304 erlang-electric-commands)))
3305 (null (erlang-test-criteria-list
3306 erlang-electric-newline-criteria)))
3307 (newline (prefix-numeric-value arg)))
3309 (if (and comment-multi-line
3310 (save-excursion
3311 (beginning-of-line)
3312 (looking-at (concat "\\s *" comment-start-skip))))
3313 (let ((str (buffer-substring
3314 (or (match-end 1) (match-beginning 0))
3315 (min (match-end 0) (point)))))
3316 (newline)
3317 (undo-boundary)
3318 (insert str))
3319 (newline)
3320 (undo-boundary)
3321 (indent-according-to-mode)))))
3324 (defun erlang-test-criteria-list (criteria)
3325 "Given a list of criterion functions, test if criteria are fulfilled.
3327 Each element in the criteria list can a function returning nil, t or
3328 the atom `stop'. t means that the criterion is fulfilled, `stop' means
3329 that it isn't fulfilled and that the search should stop,
3330 and nil means continue searching.
3332 Should the list contain the atom t the criterion is assumed to be
3333 fulfilled, unless preceded by a function returning `stop', of course.
3335 Should the argument be the atom t instead of a list, the criterion is
3336 assumed to be trivially true.
3338 Should all functions return nil, the criteria are assumed not to be
3339 fulfilled.
3341 Return t if criteria fulfilled, nil otherwise."
3342 (if (eq criteria t)
3344 (save-excursion
3345 (let ((answer nil))
3346 (while (and criteria (null answer))
3347 (if (eq (car criteria) t)
3348 (setq answer t)
3349 (setq answer (funcall (car criteria))))
3350 (setq criteria (cdr criteria)))
3351 (if (and answer (not (eq answer 'stop)))
3353 nil)))))
3356 (defun erlang-in-literal (&optional lim)
3357 "Test if point is in string, quoted atom or comment.
3359 Return one of the three atoms `atom', `string', and `comment'.
3360 Should the point be inside none of the above mentioned types of
3361 context, nil is returned."
3362 (save-excursion
3363 (let* ((lim (or lim (save-excursion
3364 (erlang-beginning-of-clause)
3365 (point))))
3366 (state (if (fboundp 'syntax-ppss) ; post Emacs 21.3
3367 (syntax-ppss)
3368 (parse-partial-sexp lim (point)))))
3369 (cond
3370 ((eq (nth 3 state) ?') 'atom)
3371 ((nth 3 state) 'string)
3372 ((nth 4 state) 'comment)
3373 (t nil)))))
3376 (defun erlang-at-end-of-function-p ()
3377 "Test if point is at end of an Erlang function.
3379 This function is designed to be a member of a criteria list."
3380 (eq (save-excursion (erlang-skip-blank) (point))
3381 (save-excursion
3382 (erlang-beginning-of-function -1) (point))))
3385 (defun erlang-stop-when-inside-argument-list ()
3386 "Return `stop' if inside parenthesis list, nil otherwise.
3388 Knows about the list comprehension syntax. When the point is
3389 after `||', `stop' is not returned.
3391 This function is designed to be a member of a criteria list."
3392 (save-excursion
3393 (condition-case nil
3394 (let ((orig-point (point))
3395 (state nil))
3396 (up-list -1)
3397 (if (not (eq (following-char) ?\[))
3398 'stop
3399 ;; Do not return `stop' when inside a list comprehension
3400 ;; construction. (The point must be after `||').
3401 (while (< (point) orig-point)
3402 (setq state (erlang-partial-parse (point) orig-point state)))
3403 (if (and (car state) (eq (car (car (car state))) '||))
3405 'stop)))
3406 (error
3407 nil))))
3410 (defun erlang-stop-when-at-guard ()
3411 "Return `stop' when at function guards.
3413 This function is designed to be a member of a criteria list."
3414 (save-excursion
3415 (beginning-of-line)
3416 (if (and (looking-at (eval-when-compile
3417 (concat "^" erlang-atom-regexp "\\s *(")))
3418 (not (looking-at
3419 (eval-when-compile
3420 (concat "^" erlang-atom-regexp ".*\\(->\\|:-\\)")))))
3421 'stop
3422 nil)))
3425 (defun erlang-next-lines-empty-p ()
3426 "Return non-nil if next lines are empty.
3428 The variable `erlang-next-lines-empty-threshold' contains the number
3429 of lines required to be empty.
3431 A line containing only spaces and tabs is considered empty.
3433 This function is designed to be a member of a criteria list."
3434 (and erlang-next-lines-empty-threshold
3435 (save-excursion
3436 (let ((left erlang-next-lines-empty-threshold)
3437 (cont t))
3438 (while (and cont (> left 0))
3439 (forward-line 1)
3440 (setq cont (looking-at "\\s *$"))
3441 (setq left (- left 1)))
3442 cont))))
3445 (defun erlang-at-keyword-end-p ()
3446 "Test if next readable token is the keyword end.
3448 This function is designed to be a member of a criteria list."
3449 (save-excursion
3450 (erlang-skip-blank)
3451 (looking-at "end[^_a-zA-Z0-9]")))
3454 ;; Erlang tags support which is aware of erlang modules.
3456 ;; Not yet implemented under XEmacs. (Hint: The Emacs 19 etags
3457 ;; package works under XEmacs.)
3459 (eval-when-compile
3460 (if (or (featurep 'bytecomp)
3461 (featurep 'byte-compile))
3462 (progn
3463 (require 'etags))))
3466 ;; Variables:
3468 (defvar erlang-tags-function-alist
3469 '((find-tag . erlang-find-tag)
3470 (find-tag-other-window . erlang-find-tag-other-window)
3471 (find-tag-regexp . erlang-find-tag-regexp)
3472 (find-tag-other-frame . erlang-find-tag-other-frame))
3473 "Alist of old tags commands and the replacement functions.")
3475 (defvar erlang-tags-installed nil
3476 "Non-nil when the Erlang tags system is installed.")
3477 (defvar erlang-tags-file-list '()
3478 "List of files in tag list. Used when finding tag on form `module:'.")
3479 (defvar erlang-tags-completion-table nil
3480 "Like `tags-completion-table', this table contains `tag' and `module:tag'.")
3481 (defvar erlang-tags-buffer-installed-p nil
3482 "Non-nil when Erlang module recognising functions installed.")
3483 (defvar erlang-tags-buffer-list '()
3484 "Temporary list of buffers.")
3485 (defvar erlang-tags-orig-completion-table nil
3486 "Temporary storage for `tags-completion-table'.")
3487 (defvar erlang-tags-orig-tag-order nil
3488 "Temporary storage for `find-tag-tag-order'.")
3489 (defvar erlang-tags-orig-regexp-tag-order nil
3490 "Temporary storage for `find-tag-regexp-tag-order'.")
3491 (defvar erlang-tags-orig-search-function nil
3492 "Temporary storage for `find-tag-search-function'.")
3493 (defvar erlang-tags-orig-regexp-search-function nil
3494 "Temporary storage for `find-tag-regexp-search-function'.")
3495 (defvar erlang-tags-orig-format-hooks nil
3496 "Temporary storage for `tags-table-format-hooks'.")
3498 (defun erlang-tags-init ()
3499 "Install an alternate version of tags, aware of Erlang modules.
3501 After calling this function, the tags functions are aware of
3502 Erlang modules. Tags can be entered on the for `module:tag' as well
3503 as on the old form `tag'.
3505 In the completion list, `module:tag' and `module:' shows up.
3507 Call this function from an appropriate init file, or add it to
3508 Erlang mode hook with the commands:
3509 (add-hook 'erlang-mode-hook 'erlang-tags-init)
3510 (add-hook 'erlang-shell-mode-hook 'erlang-tags-init)
3512 This function only works under Emacs 18 and Emacs 19. Currently, It
3513 is not implemented under XEmacs. (Hint: The Emacs 19 etags module
3514 works under XEmacs.)"
3515 (interactive)
3516 (cond ((= erlang-emacs-major-version 18)
3517 (require 'tags)
3518 (erlang-tags-define-keys (current-local-map))
3519 (setq erlang-tags-installed t))
3521 (require 'etags)
3522 ;; Test on a function available in the Emacs 19 version
3523 ;; of tags but not in the XEmacs version.
3524 (if (not (fboundp 'find-tag-noselect))
3526 (erlang-tags-define-keys (current-local-map))
3527 (setq erlang-tags-installed t)))))
3530 ;; Set all keys bound to `find-tag' et.al. in the global map and the
3531 ;; menu to `erlang-find-tag' et.al. in `map'.
3533 ;; The function `substitute-key-definition' does not work properly
3534 ;; in all version of Emacs.
3536 (defun erlang-tags-define-keys (map)
3537 "Bind tags commands to keymap MAP aware of Erlang modules."
3538 (let ((alist erlang-tags-function-alist))
3539 (while alist
3540 (let* ((old (car (car alist)))
3541 (new (cdr (car alist)))
3542 (keys (append (where-is-internal old global-map))))
3543 (while keys
3544 (define-key map (car keys) new)
3545 (setq keys (cdr keys))))
3546 (setq alist (cdr alist))))
3547 ;; Update the menu.
3548 (erlang-menu-substitute erlang-menu-base-items erlang-tags-function-alist)
3549 (erlang-menu-init))
3552 ;; There exists a variable `find-tag-default-function'. It is not used
3553 ;; since `complete-tag' uses it to get current word under point. In that
3554 ;; situation we don't want the module to be prepended.
3556 (defun erlang-find-tag-default ()
3557 "Return the default tag.
3558 Search `-import' list of imported functions.
3559 Single quotes are been stripped away."
3560 (let ((mod-func (erlang-get-function-under-point)))
3561 (cond ((null mod-func)
3562 nil)
3563 ((null (car mod-func))
3564 (nth 1 mod-func))
3566 (concat (car mod-func) ":" (nth 1 mod-func))))))
3569 ;; Return `t' since it is used inside `tags-loop-form'.
3570 ;;;###autoload
3571 (defun erlang-find-tag (modtagname &optional next-p regexp-p)
3572 "Like `find-tag'. Capable of retrieving Erlang modules.
3574 Tags can be given on the forms `tag', `module:', `module:tag'."
3575 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3576 (switch-to-buffer (erlang-find-tag-noselect modtagname next-p regexp-p))
3580 ;; Code mainly from `find-tag-other-window' in `etags.el'.
3581 ;;;###autoload
3582 (defun erlang-find-tag-other-window (tagname &optional next-p regexp-p)
3583 "Like `find-tag-other-window' but aware of Erlang modules."
3584 (interactive (erlang-tag-interactive
3585 "Find `module:tag' or `tag' other window: "))
3587 ;; This is to deal with the case where the tag is found in the
3588 ;; selected window's buffer; without this, point is moved in both
3589 ;; windows. To prevent this, we save the selected window's point
3590 ;; before doing find-tag-noselect, and restore it afterwards.
3591 (let* ((window-point (window-point (selected-window)))
3592 (tagbuf (erlang-find-tag-noselect tagname next-p regexp-p))
3593 (tagpoint (progn (set-buffer tagbuf) (point))))
3594 (set-window-point (prog1
3595 (selected-window)
3596 (switch-to-buffer-other-window tagbuf)
3597 ;; We have to set this new window's point; it
3598 ;; might already have been displaying a
3599 ;; different portion of tagbuf, in which case
3600 ;; switch-to-buffer-other-window doesn't set
3601 ;; the window's point from the buffer.
3602 (set-window-point (selected-window) tagpoint))
3603 window-point)))
3606 (defun erlang-find-tag-other-frame (tagname &optional next-p)
3607 "Like `find-tag-other-frame' but aware of Erlang modules."
3608 (interactive (erlang-tag-interactive
3609 "Find `module:tag' or `tag' other frame: "))
3610 (let ((pop-up-frames t))
3611 (erlang-find-tag-other-window tagname next-p)))
3614 (defun erlang-find-tag-regexp (regexp &optional next-p other-window)
3615 "Like `find-tag-regexp' but aware of Erlang modules."
3616 (interactive (if (fboundp 'find-tag-regexp)
3617 (erlang-tag-interactive
3618 "Find `module:regexp' or `regexp': ")
3619 (error "This version of Emacs can't find tags by regexps")))
3620 (funcall (if other-window
3621 'erlang-find-tag-other-window
3622 'erlang-find-tag)
3623 regexp next-p t))
3626 ;; Just like C-u M-. This could be added to the menu.
3627 (defun erlang-find-next-tag ()
3628 "Find next tag, like \\[find-tag] with prefix arg."
3629 (interactive)
3630 (let ((current-prefix-arg '(4)))
3631 (if erlang-tags-installed
3632 (call-interactively 'erlang-find-tag)
3633 (call-interactively 'find-tag))))
3636 ;; Mimics `find-tag-noselect' found in `etags.el', but uses `find-tag' to
3637 ;; be compatible with `tags.el'.
3639 ;; Handles three cases:
3640 ;; * `module:' Loop over all possible file names. Stop if a file-name
3641 ;; without extension and directory matches the module.
3643 ;; * `module:tag'
3644 ;; Emacs 19: Replace test functions with functions aware of
3645 ;; Erlang modules. Tricky because the etags system wasn't
3646 ;; built for these kind of operations...
3648 ;; Emacs 18: We loop over `find-tag' until we find a file
3649 ;; whose module matches the requested module. The
3650 ;; drawback is that a lot of files could be loaded into
3651 ;; Emacs.
3653 ;; * `tag' Just give it to `find-tag'.
3655 (defun erlang-find-tag-noselect (modtagname &optional next-p regexp-p)
3656 "Like `find-tag-noselect' but aware of Erlang modules."
3657 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3658 (or modtagname
3659 (setq modtagname (symbol-value 'last-tag)))
3660 (funcall (symbol-function 'set) 'last-tag modtagname)
3661 ;; `tags.el' uses this variable to record how M-, would
3662 ;; know where to restart a tags command.
3663 (if (boundp 'tags-loop-form)
3664 (funcall (symbol-function 'set)
3665 'tags-loop-form '(erlang-find-tag nil t)))
3666 (save-window-excursion
3667 (cond
3668 ((string-match ":$" modtagname)
3669 ;; Only the module name was given. Read all files whose file name
3670 ;; match.
3671 (let ((modname (substring modtagname 0 (match-beginning 0)))
3672 (file nil))
3673 (if (not next-p)
3674 (save-excursion
3675 (visit-tags-table-buffer)
3676 (setq erlang-tags-file-list
3677 (funcall (symbol-function 'tags-table-files)))))
3678 (while (null file)
3679 (or erlang-tags-file-list
3680 (save-excursion
3681 (if (and (featurep 'etags)
3682 (funcall
3683 (symbol-function 'visit-tags-table-buffer) 'same)
3684 (funcall
3685 (symbol-function 'visit-tags-table-buffer) t))
3686 (setq erlang-tags-file-list
3687 (funcall (symbol-function 'tags-table-files)))
3688 (error "No %stags containing %s" (if next-p "more " "")
3689 modtagname))))
3690 (if erlang-tags-file-list
3691 (let ((this-module (erlang-get-module-from-file-name
3692 (car erlang-tags-file-list))))
3693 (if (and (stringp this-module)
3694 (string= modname this-module))
3695 (setq file (car erlang-tags-file-list)))
3696 (setq erlang-tags-file-list (cdr erlang-tags-file-list)))))
3697 (set-buffer (or (get-file-buffer file)
3698 (find-file-noselect file)))))
3700 ((string-match ":" modtagname)
3701 (if (boundp 'find-tag-tag-order)
3702 ;; Method one: Add module-recognising functions to the
3703 ;; list of order functions. However, the tags system
3704 ;; from Emacs 18, and derives thereof (read: XEmacs)
3705 ;; hasn't got this feature.
3706 (progn
3707 (erlang-tags-install-module-check)
3708 (unwind-protect
3709 (funcall (symbol-function 'find-tag)
3710 modtagname next-p regexp-p)
3711 (erlang-tags-remove-module-check)))
3712 ;; Method two: Call the tags system until a file matching
3713 ;; the module is found. This could result in that many
3714 ;; files are read. (e.g. The tag "foo:file" will take a
3715 ;; while to process.)
3716 (let* ((modname (substring modtagname 0 (match-beginning 0)))
3717 (tagname (substring modtagname (match-end 0) nil))
3718 (last-tag tagname)
3719 file)
3720 (while
3721 (progn
3722 (funcall (symbol-function 'find-tag) tagname next-p regexp-p)
3723 (setq next-p t)
3724 ;; Determine the module form the file name. (The
3725 ;; alternative, to check `-module', would make this
3726 ;; code useless for non-Erlang programs.)
3727 (setq file (erlang-get-module-from-file-name buffer-file-name))
3728 (not (and (stringp file)
3729 (string= modname file))))))))
3731 (funcall (symbol-function 'find-tag) modtagname next-p regexp-p)))
3732 (current-buffer))) ; Return the new buffer.
3735 ;; Process interactive arguments for erlang-find-tag-*.
3737 ;; Negative arguments work only for `etags', not `tags'. This is not
3738 ;; a problem since negative arguments means step back into the
3739 ;; history list, a feature not implemented in `tags'.
3741 (defun erlang-tag-interactive (prompt)
3742 (condition-case nil
3743 (require 'etags)
3744 (error
3745 (require 'tags)))
3746 (if current-prefix-arg
3747 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
3750 (let* ((default (erlang-find-tag-default))
3751 (prompt (if default
3752 (format "%s(default %s) " prompt default)
3753 prompt))
3754 (spec (if (featurep 'etags)
3755 (completing-read prompt 'erlang-tags-complete-tag)
3756 (read-string prompt))))
3757 (list (if (equal spec "")
3758 (or default (error "There is no default tag"))
3759 spec)))))
3762 ;; Search tag functions which are aware of Erlang modules. The tactic
3763 ;; is to store new search functions into the local variables of the
3764 ;; TAGS buffers. The variables are restored directly after the
3765 ;; search. The situation is complicated by the fact that new TAGS
3766 ;; files can be loaded during the search.
3768 ;; This code is Emacs 19 `etags' specific.
3770 (defun erlang-tags-install-module-check ()
3771 "Install our own tag search functions."
3772 ;; Make sure our functions are installed in TAGS files loaded
3773 ;; into Emacs while searching.
3774 ;; ?? tags-table-format-hooks isn't in Emacs 21 or XEmacs etags.
3775 (setq erlang-tags-orig-format-hooks
3776 (symbol-value 'tags-table-format-hooks))
3777 (funcall (symbol-function 'set) 'tags-table-format-hooks
3778 (cons 'erlang-tags-recognize-tags-table
3779 erlang-tags-orig-format-hooks))
3780 (setq erlang-tags-buffer-list '())
3781 ;; Install our functions in the TAGS files already resident.
3782 (save-excursion
3783 (let ((files (symbol-value 'tags-table-computed-list)))
3784 (while files
3785 (if (stringp (car files))
3786 (if (get-file-buffer (car files))
3787 (progn
3788 (set-buffer (get-file-buffer (car files)))
3789 (erlang-tags-install-local))))
3790 (setq files (cdr files))))))
3793 (defun erlang-tags-install-local ()
3794 "Install our tag search functions in current buffer."
3795 (if erlang-tags-buffer-installed-p
3797 ;; Mark this buffer as "installed" and record.
3798 (set (make-local-variable 'erlang-tags-buffer-installed-p) t)
3799 (setq erlang-tags-buffer-list
3800 (cons (current-buffer) erlang-tags-buffer-list))
3802 ;; Save the original values.
3803 (set (make-local-variable 'erlang-tags-orig-tag-order)
3804 (symbol-value 'find-tag-tag-order))
3805 (set (make-local-variable 'erlang-tags-orig-regexp-tag-order)
3806 (symbol-value 'find-tag-regexp-tag-order))
3807 (set (make-local-variable 'erlang-tags-orig-search-function)
3808 (symbol-value 'find-tag-search-function))
3809 (set (make-local-variable 'erlang-tags-orig-regexp-search-function)
3810 (symbol-value 'find-tag-regexp-search-function))
3812 ;; Install our own functions.
3813 (set (make-local-variable 'find-tag-search-function)
3814 'erlang-tags-search-forward)
3815 (set (make-local-variable 'find-tag-regexp-search-function)
3816 'erlang-tags-regexp-search-forward)
3817 (set (make-local-variable 'find-tag-tag-order)
3818 '(erlang-tag-match-module-p))
3819 (set (make-local-variable 'find-tag-regexp-tag-order)
3820 '(erlang-tag-match-module-regexp-p))))
3823 (defun erlang-tags-remove-module-check ()
3824 "Remove our own tags search functions."
3825 (funcall (symbol-function 'set)
3826 'tags-table-format-hooks
3827 erlang-tags-orig-format-hooks)
3828 ;; Remove our functions from the TAGS files. (Note that
3829 ;; `tags-table-computed-list' need not be the same list as when
3830 ;; the search was started.)
3831 (save-excursion
3832 (let ((buffers erlang-tags-buffer-list))
3833 (while buffers
3834 (if (buffer-name (car buffers))
3835 (progn
3836 (set-buffer (car buffers))
3837 (erlang-tags-remove-local)))
3838 (setq buffers (cdr buffers))))))
3841 (defun erlang-tags-remove-local ()
3842 "Remove our tag search functions from current buffer."
3843 (if (null erlang-tags-buffer-installed-p)
3845 (funcall (symbol-function 'set) 'erlang-tags-buffer-installed-p nil)
3846 (funcall (symbol-function 'set)
3847 'find-tag-tag-order erlang-tags-orig-tag-order)
3848 (funcall (symbol-function 'set)
3849 'find-tag-regexp-tag-order erlang-tags-orig-regexp-tag-order)
3850 (funcall (symbol-function 'set)
3851 'find-tag-search-function erlang-tags-orig-search-function)
3852 (funcall (symbol-function 'set)
3853 'find-tag-regexp-search-function
3854 erlang-tags-orig-regexp-search-function)))
3857 (defun erlang-tags-recognize-tags-table ()
3858 "Install our functions in all loaded TAGS files.
3860 This function is added to `tags-table-format-hooks' when searching
3861 for a tag on the form `module:tag'."
3862 (if (null (funcall (symbol-function 'etags-recognize-tags-table)))
3864 (erlang-tags-install-local)
3868 (defun erlang-tags-search-forward (tag &optional bound noerror count)
3869 "Forward search function, aware of Erlang module prefix."
3870 (if (string-match ":" tag)
3871 (setq tag (substring tag (match-end 0) nil)))
3872 ;; Avoid unintended recursion.
3873 (if (eq erlang-tags-orig-search-function 'erlang-tags-search-forward)
3874 (search-forward tag bound noerror count)
3875 (funcall erlang-tags-orig-search-function tag bound noerror count)))
3878 (defun erlang-tags-regexp-search-forward (tag &optional bound noerror count)
3879 "Forward regexp search function, aware of Erlang module prefix."
3880 (if (string-match ":" tag)
3881 (setq tag (substring tag (match-end 0) nil)))
3882 (if (eq erlang-tags-orig-regexp-search-function
3883 'erlang-tags-regexp-search-forward)
3884 (re-search-forward tag bound noerror count)
3885 (funcall erlang-tags-orig-regexp-search-function
3886 tag bound noerror count)))
3889 ;; t if point is at a tag line that matches TAG, containing
3890 ;; module information. Assumes that all other order functions
3891 ;; are stored in `erlang-tags-orig-[regex]-tag-order'.
3893 (defun erlang-tag-match-module-p (tag)
3894 (erlang-tag-match-module-common-p tag erlang-tags-orig-tag-order))
3896 (defun erlang-tag-match-module-regexp-p (tag)
3897 (erlang-tag-match-module-common-p tag erlang-tags-orig-regexp-tag-order))
3899 (defun erlang-tag-match-module-common-p (tag order)
3900 (let ((mod nil)
3901 (found nil))
3902 (if (string-match ":" tag)
3903 (progn
3904 (setq mod (substring tag 0 (match-beginning 0)))
3905 (setq tag (substring tag (match-end 0) nil))))
3906 (while (and order (not found))
3907 (setq found
3908 (and (not (memq (car order)
3909 '(erlang-tag-match-module-p
3910 erlang-tag-match-module-regexp-p)))
3911 (funcall (car order) tag)))
3912 (setq order (cdr order)))
3913 (and found
3914 (or (null mod)
3915 (string= mod (erlang-get-module-from-file-name
3916 (file-of-tag)))))))
3919 ;;; Tags completion, Emacs 19 `etags' specific.
3921 ;;; The basic idea is to create a second completion table `erlang-tags-
3922 ;;; completion-table' containing all normal tags plus tags on the form
3923 ;;; `module:tag'.
3926 (defun erlang-complete-tag ()
3927 "Perform tags completion on the text around point.
3928 Completes to the set of names listed in the current tags table.
3930 Should the Erlang tags system be installed this command knows
3931 about Erlang modules."
3932 (interactive)
3933 (condition-case nil
3934 (require 'etags)
3935 (error nil))
3936 (cond ((and erlang-tags-installed
3937 (fboundp 'complete-tag)) ; Emacs 19
3938 (let ((orig-tags-complete-tag (symbol-function 'tags-complete-tag)))
3939 (fset 'tags-complete-tag
3940 (symbol-function 'erlang-tags-complete-tag))
3941 (unwind-protect
3942 (funcall (symbol-function 'complete-tag))
3943 (fset 'tags-complete-tag orig-tags-complete-tag))))
3944 ((fboundp 'complete-tag) ; Emacs 19
3945 (funcall (symbol-function 'complete-tag)))
3946 ((fboundp 'tag-complete-symbol) ; XEmacs
3947 (funcall (symbol-function 'tag-complete-symbol)))
3949 (error "This version of Emacs can't complete tags"))))
3952 ;; Based on `tags-complete-tag', but this one uses
3953 ;; `erlang-tags-completion-table' instead of `tags-completion-table'.
3955 ;; This is the entry-point called by system function `completing-read'.
3956 (defun erlang-tags-complete-tag (string predicate what)
3957 (save-excursion
3958 ;; If we need to ask for the tag table, allow that.
3959 (let ((enable-recursive-minibuffers t))
3960 (visit-tags-table-buffer))
3961 (if (eq what t)
3962 (all-completions string (erlang-tags-completion-table) predicate)
3963 (try-completion string (erlang-tags-completion-table) predicate))))
3966 ;; `tags-completion-table' calls itself recursively, make it
3967 ;; call our own wedge instead. Note that the recursive call
3968 ;; is very rare; it only occurs when a tags-file contains
3969 ;; `include'-statements.
3970 (defun erlang-tags-completion-table ()
3971 "Build completion table. Tags on the form `tag' or `module:tag'."
3972 (setq erlang-tags-orig-completion-table
3973 (symbol-function 'tags-completion-table))
3974 (fset 'tags-completion-table
3975 (symbol-function 'erlang-tags-completion-table-1))
3976 (unwind-protect
3977 (erlang-tags-completion-table-1)
3978 (fset 'tags-completion-table
3979 erlang-tags-orig-completion-table)))
3982 (defun erlang-tags-completion-table-1 ()
3983 (make-local-variable 'erlang-tags-completion-table)
3984 (or erlang-tags-completion-table
3985 (let ((tags-completion-table nil)
3986 (tags-completion-table-function
3987 'erlang-etags-tags-completion-table))
3988 (funcall erlang-tags-orig-completion-table)
3989 (setq erlang-tags-completion-table tags-completion-table))))
3992 ;; Based on `etags-tags-completion-table'. The difference is that we
3993 ;; add three symbols to the vector, the tag, module: and module:tag.
3994 ;; The module is extracted from the file name of a tag. (This one
3995 ;; only works if we are looking at an `etags' file. However, this is
3996 ;; the only format supported by Emacs, so far.)
3997 (defun erlang-etags-tags-completion-table ()
3998 (let ((table (make-vector 511 0))
3999 (file nil))
4000 (save-excursion
4001 (goto-char (point-min))
4002 ;; This monster regexp matches an etags tag line.
4003 ;; \1 is the string to match;
4004 ;; \2 is not interesting;
4005 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
4006 ;; \4 is not interesting;
4007 ;; \5 is the explicitly-specified tag name.
4008 ;; \6 is the line to start searching at;
4009 ;; \7 is the char to start searching at.
4010 (while (progn
4011 (while (and
4012 (eq (following-char) ?\f)
4013 (looking-at "\f\n\\([^,\n]*\\),.*\n"))
4014 (setq file (buffer-substring
4015 (match-beginning 1) (match-end 1)))
4016 (goto-char (match-end 0)))
4017 (re-search-forward
4019 ^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
4020 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
4021 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
4022 nil t))
4023 (let ((tag (if (match-beginning 5)
4024 ;; There is an explicit tag name.
4025 (buffer-substring (match-beginning 5) (match-end 5))
4026 ;; No explicit tag name. Best guess.
4027 (buffer-substring (match-beginning 3) (match-end 3))))
4028 (module (and file
4029 (erlang-get-module-from-file-name file))))
4030 (intern tag table)
4031 (if (stringp module)
4032 (progn
4033 (intern (concat module ":" tag) table)
4034 ;; Only the first one will be stored in the table.
4035 (intern (concat module ":") table))))))
4036 table))
4039 ;;; Prepare for other methods to run an Erlang slave process.
4042 (defvar erlang-shell-function 'inferior-erlang
4043 "Command to execute start a new Erlang shell.
4045 Change this variable to use your favorite
4046 Erlang compilation package.")
4048 (defvar erlang-shell-display-function 'inferior-erlang-run-or-select
4049 "Command to execute to display Erlang shell.
4051 Change this variable to use your favorite
4052 Erlang compilation package.")
4054 (defvar erlang-compile-function 'inferior-erlang-compile
4055 "Command to execute to compile current buffer.
4057 Change this variable to use your favorite
4058 Erlang compilation package.")
4060 (defvar erlang-compile-display-function 'inferior-erlang-run-or-select
4061 "Command to execute to view last compilation.
4063 Change this variable to use your favorite
4064 Erlang compilation package.")
4066 (defvar erlang-next-error-function 'inferior-erlang-next-error
4067 "Command to execute to go to the next error.
4069 Change this variable to use your favorite Erlang compilation
4070 package. Not used in Emacs 21.")
4073 ;;;###autoload
4074 (defun erlang-shell ()
4075 "Start a new Erlang shell.
4077 The variable `erlang-shell-function' decides which method to use,
4078 default is to start a new Erlang host. It is possible that, in the
4079 future, a new shell on an already running host will be started."
4080 (interactive)
4081 (call-interactively erlang-shell-function))
4084 ;;;###autoload (autoload 'run-erlang "erlang" "Start a new Erlang shell." t)
4086 ;; It is customary for Emacs packages to supply a function on this
4087 ;; form, even though it violates the `erlang-*' name convention.
4088 (defalias 'run-erlang 'erlang-shell)
4091 (defun erlang-shell-display ()
4092 "Display an Erlang shell, or start a new."
4093 (interactive)
4094 (call-interactively erlang-shell-display-function))
4097 ;;;###autoload
4098 (defun erlang-compile ()
4099 "Compile Erlang module in current buffer."
4100 (interactive)
4101 (call-interactively erlang-compile-function))
4104 (defun erlang-compile-display ()
4105 "Display compilation output."
4106 (interactive)
4107 (call-interactively erlang-compile-display-function))
4110 (defun erlang-next-error ()
4111 "Display next error message from the latest compilation."
4112 (interactive)
4113 (call-interactively erlang-next-error-function))
4118 ;;; Erlang Shell Mode -- Major mode used for Erlang shells.
4121 ;; This mode is designed to be implementation independent,
4122 ;; e.g. it does not assume that we are running an inferior
4123 ;; Erlang, there exists a lot of other possibilities.
4126 (defvar erlang-shell-buffer-name "*erlang*"
4127 "The name of the Erlang link shell buffer.")
4129 ;;(when (boundp 'same-window-buffer-names)
4130 ;; (unless (member "*Python*" same-window-buffer-names)
4131 ;; (push "*Python*" same-window-buffer-names)))
4134 (defvar erlang-shell-mode-map nil
4135 "Keymap used by Erlang shells.")
4138 (defvar erlang-shell-mode-hook nil
4139 "*User functions to run when an Erlang shell is started.
4141 This hook is used to change the behaviour of Erlang mode. It is
4142 normally used by the user to personalise the programming environment.
4143 When used in a site init file, it could be used to customise Erlang
4144 mode for all users on the system.
4146 The function added to this hook is run every time a new Erlang
4147 shell is started.
4149 See also `erlang-load-hook', a hook which is run once, when Erlang
4150 mode is loaded, and `erlang-mode-hook' which is run every time a new
4151 Erlang source file is loaded into Emacs.")
4154 (defvar erlang-input-ring-file-name "~/.erlang_history"
4155 "*When non-nil, file name used to store Erlang shell history information.")
4158 (defun erlang-shell-mode ()
4159 "Major mode for interacting with an Erlang shell.
4161 We assume that we already are in Comint mode.
4163 The following special commands are available:
4164 \\{erlang-shell-mode-map}"
4165 (interactive)
4166 (setq major-mode 'erlang-shell-mode)
4167 (setq mode-name "Erlang Shell")
4168 (erlang-mode-variables)
4169 (if erlang-shell-mode-map
4171 (setq erlang-shell-mode-map (copy-keymap comint-mode-map))
4172 (erlang-shell-mode-commands erlang-shell-mode-map))
4173 (use-local-map erlang-shell-mode-map)
4174 (unless inferior-erlang-use-cmm
4175 ;; This was originally not a marker, but it needs to be, at least
4176 ;; in Emacs 21, and should be backwards-compatible. Otherwise,
4177 ;; would need to test whether compilation-parsing-end is a marker
4178 ;; after requiring `compile'.
4179 (set (make-local-variable 'compilation-parsing-end) (copy-marker 1))
4180 (set (make-local-variable 'compilation-error-list) nil)
4181 (set (make-local-variable 'compilation-old-error-list) nil))
4182 ;; Needed when compiling directly from the Erlang shell.
4183 (setq compilation-last-buffer (current-buffer))
4184 (erlang-add-compilation-alist erlang-error-regexp-alist)
4185 (setq comint-prompt-regexp "^[^>=]*> *")
4186 (setq comint-eol-on-send t)
4187 (setq comint-input-ignoredups t)
4188 (setq comint-scroll-show-maximum-output t)
4189 (setq comint-scroll-to-bottom-on-output t)
4190 ;; In Emacs 19.30, `add-hook' has got a `local' flag, use it. If
4191 ;; the call fails, just call the normal `add-hook'.
4192 (condition-case nil
4193 (progn
4194 (make-local-hook 'comint-output-filter-functions) ; obsolete after Emacs 21.3
4195 (add-hook 'comint-output-filter-functions
4196 'inferior-erlang-strip-delete nil t)
4197 (add-hook 'comint-output-filter-functions
4198 'inferior-erlang-strip-ctrl-m nil t))
4199 (error
4200 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-delete)
4201 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-ctrl-m)))
4202 ;; Some older versions of comint don't have an input ring.
4203 (if (fboundp 'comint-read-input-ring)
4204 (progn
4205 (setq comint-input-ring-file-name erlang-input-ring-file-name)
4206 (comint-read-input-ring t)
4207 (make-local-variable 'kill-buffer-hook)
4208 (add-hook 'kill-buffer-hook 'comint-write-input-ring)))
4209 ;; At least in Emacs 21, we need to be in `compilation-minor-mode'
4210 ;; for `next-error' to work. We can avoid it clobbering the shell
4211 ;; keys thus.
4212 (when inferior-erlang-use-cmm
4213 (compilation-minor-mode 1)
4214 (set (make-local-variable 'minor-mode-overriding-map-alist)
4215 `((compilation-minor-mode
4216 . ,(let ((map (make-sparse-keymap)))
4217 ;; It would be useful to put keymap properties on the
4218 ;; error lines so that we could use RET and mouse-2
4219 ;; on them directly.
4220 (when (boundp 'compilation-skip-threshold) ; new compile.el
4221 (define-key map [mouse-2] #'erlang-mouse-2-command)
4222 (define-key map "\C-m" #'erlang-RET-command))
4223 (if (boundp 'compilation-menu-map)
4224 (define-key map [menu-bar compilation]
4225 (cons "Errors" compilation-menu-map)))
4226 map)))))
4227 (run-hooks 'erlang-shell-mode-hook))
4230 (defun erlang-mouse-2-command (event)
4231 "Command bound to `mouse-2' in inferior Erlang buffer.
4232 Selects Comint or Compilation mode command as appropriate."
4233 (interactive "e")
4234 (if (save-window-excursion
4235 (save-excursion
4236 (mouse-set-point event)
4237 (consp (get-text-property (line-beginning-position) 'message))))
4238 (call-interactively (lookup-key compilation-mode-map [mouse-2]))
4239 (call-interactively (lookup-key comint-mode-map [mouse-2]))))
4241 (defun erlang-RET-command ()
4242 "Command bound to `RET' in inferior Erlang buffer.
4243 Selects Comint or Compilation mode command as appropriate."
4244 (interactive)
4245 (if (consp (get-text-property (line-beginning-position) 'message))
4246 (call-interactively (lookup-key compilation-mode-map "\C-m"))
4247 (call-interactively (lookup-key comint-mode-map "\C-m"))))
4249 (defun erlang-shell-mode-commands (map)
4250 (define-key map "\M-\t" 'erlang-complete-tag)
4251 (define-key map "\C-a" 'comint-bol) ; Normally the other way around.
4252 (define-key map "\C-c\C-a" 'beginning-of-line)
4253 (define-key map "\C-d" nil) ; Was `comint-delchar-or-maybe-eof'
4254 (define-key map "\M-\C-m" 'compile-goto-error)
4255 (unless inferior-erlang-use-cmm
4256 (define-key map "\C-x`" 'erlang-next-error)))
4259 ;;; Inferior Erlang -- Run an Erlang shell as a subprocess.
4262 (defvar inferior-erlang-display-buffer-any-frame nil
4263 "*When nil, `inferior-erlang-display-buffer' use only selected frame.
4264 When t, all frames are searched. When 'raise, the frame is raised.")
4266 (defvar inferior-erlang-shell-type 'newshell
4267 "The type of Erlang shell to use.
4269 When this variable is set to the atom `oldshell', the old shell is used.
4270 When set to `newshell' the new shell is used. Should the variable be
4271 nil, the default shell is used.
4273 This variable influence the setting of other variables.")
4275 (defvar inferior-erlang-machine "erl"
4276 "*The name of the Erlang shell.")
4278 (defvar inferior-erlang-machine-options '()
4279 "*The options used when activating the Erlang shell.
4281 This must be a list of strings.")
4283 (defvar inferior-erlang-process-name "inferior-erlang"
4284 "The name of the inferior Erlang process.")
4286 (defvar inferior-erlang-buffer-name erlang-shell-buffer-name
4287 "The name of the inferior Erlang buffer.")
4289 (defvar inferior-erlang-prompt-timeout 60
4290 "*Number of seconds before `inferior-erlang-wait-prompt' timeouts.
4292 The time specified is waited after every output made by the inferior
4293 Erlang shell. When this variable is t, we assume that we always have
4294 a prompt. When nil, we will wait forever, or until \\[keyboard-quit].")
4296 (defvar inferior-erlang-process nil
4297 "Process of last invoked inferior Erlang, or nil.")
4299 (defvar inferior-erlang-buffer nil
4300 "Buffer of last invoked inferior Erlang, or nil.")
4302 ;;;###autoload
4303 (defun inferior-erlang ()
4304 "Run an inferior Erlang.
4306 This is just like running Erlang in a normal shell, except that
4307 an Emacs buffer is used for input and output.
4308 \\<comint-mode-map>
4309 The command line history can be accessed with \\[comint-previous-input] and \\[comint-next-input].
4310 The history is saved between sessions.
4312 Entry to this mode calls the functions in the variables
4313 `comint-mode-hook' and `erlang-shell-mode-hook' with no arguments.
4315 The following commands imitate the usual Unix interrupt and
4316 editing control characters:
4317 \\{erlang-shell-mode-map}"
4318 (interactive)
4319 (require 'comint)
4320 (let ((opts inferior-erlang-machine-options))
4321 (cond ((eq inferior-erlang-shell-type 'oldshell)
4322 (setq opts (cons "-oldshell" opts)))
4323 ((eq inferior-erlang-shell-type 'newshell)
4324 (setq opts (append '("-newshell" "-env" "TERM" "vt100") opts))))
4325 (setq inferior-erlang-buffer
4326 (apply 'make-comint
4327 inferior-erlang-process-name inferior-erlang-machine
4328 nil opts)))
4329 (setq inferior-erlang-process
4330 (get-buffer-process inferior-erlang-buffer))
4331 (process-kill-without-query inferior-erlang-process)
4332 (switch-to-buffer inferior-erlang-buffer)
4333 (if (and (not (eq system-type 'windows-nt))
4334 (eq inferior-erlang-shell-type 'newshell))
4335 (setq comint-process-echoes t))
4336 ;; `rename-buffer' takes only one argument in Emacs 18.
4337 (condition-case nil
4338 (rename-buffer inferior-erlang-buffer-name t)
4339 (error (rename-buffer inferior-erlang-buffer-name)))
4340 (erlang-shell-mode))
4343 (defun inferior-erlang-run-or-select ()
4344 "Switch to an inferior Erlang buffer, possibly starting new process."
4345 (interactive)
4346 (if (null (inferior-erlang-running-p))
4347 (inferior-erlang)
4348 (inferior-erlang-display-buffer t)))
4351 (defun inferior-erlang-display-buffer (&optional select)
4352 "Make the inferior Erlang process visible.
4353 The window is returned.
4355 Should `inferior-erlang-display-buffer-any-frame' be nil the buffer is
4356 displayed in the current frame. Should it be non-nil, and the buffer
4357 already is visible in any other frame, no new window will be created.
4358 Should it be the atom 'raise, the frame containing the window will
4359 be raised.
4361 Should the optional argument SELECT be non-nil, the window is
4362 selected. Should the window be in another frame, that frame is raised.
4364 Note, should the mouse pointer be places outside the raised frame, that
4365 frame will become deselected before the next command."
4366 (interactive)
4367 (or (inferior-erlang-running-p)
4368 (error "No inferior Erlang process is running"))
4369 (let ((win (inferior-erlang-window
4370 inferior-erlang-display-buffer-any-frame))
4371 (frames-p (fboundp 'selected-frame)))
4372 (if (null win)
4373 (let ((old-win (selected-window)))
4374 (save-excursion
4375 (switch-to-buffer-other-window inferior-erlang-buffer)
4376 (setq win (selected-window)))
4377 (select-window old-win))
4378 (if (and window-system
4379 frames-p
4380 (or select
4381 (eq inferior-erlang-display-buffer-any-frame 'raise))
4382 (not (eq (selected-frame) (window-frame win))))
4383 (raise-frame (window-frame win))))
4384 (if select
4385 (select-window win))
4386 (sit-for 0)
4387 win))
4390 (defun inferior-erlang-running-p ()
4391 "Non-nil when an inferior Erlang is running."
4392 (and inferior-erlang-process
4393 (memq (process-status inferior-erlang-process) '(run open))
4394 inferior-erlang-buffer
4395 (buffer-name inferior-erlang-buffer)))
4398 (defun inferior-erlang-window (&optional all-frames)
4399 "Return the window containing the inferior Erlang, or nil."
4400 (and (inferior-erlang-running-p)
4401 (if (and all-frames (>= erlang-emacs-major-version 19))
4402 (get-buffer-window inferior-erlang-buffer t)
4403 (get-buffer-window inferior-erlang-buffer))))
4406 (defun inferior-erlang-wait-prompt ()
4407 "Wait until the inferior Erlang shell prompt appears."
4408 (if (eq inferior-erlang-prompt-timeout t)
4410 (or (inferior-erlang-running-p)
4411 (error "No inferior Erlang shell is running"))
4412 (save-excursion
4413 (set-buffer inferior-erlang-buffer)
4414 (let ((msg nil))
4415 (while (save-excursion
4416 (goto-char (process-mark inferior-erlang-process))
4417 (forward-line 0)
4418 (not (looking-at comint-prompt-regexp)))
4419 (if msg
4421 (setq msg t)
4422 (message "Waiting for Erlang shell prompt (press C-g to abort)."))
4423 (or (accept-process-output inferior-erlang-process
4424 inferior-erlang-prompt-timeout)
4425 (error "No Erlang shell prompt before timeout")))
4426 (if msg (message ""))))))
4428 (autoload 'comint-send-input "comint")
4430 (defun inferior-erlang-send-command (cmd &optional hist)
4431 "Send command CMD to the inferior Erlang.
4433 The contents of the current command line (if any) will
4434 be placed at the next prompt.
4436 If optional second argument is non-nil the command is inserted into
4437 the history list.
4439 Return the position after the newly inserted command."
4440 (or (inferior-erlang-running-p)
4441 (error "No inferior Erlang process is running"))
4442 (let ((old-buffer (current-buffer))
4443 (insert-point (marker-position (process-mark inferior-erlang-process)))
4444 (insert-length (if comint-process-echoes
4446 (1+ (length cmd)))))
4447 (set-buffer inferior-erlang-buffer)
4448 (goto-char insert-point)
4449 (insert cmd)
4450 ;; Strange things happened if `comint-eol-on-send' is declared
4451 ;; in the `let' expression above, but setq:d here. The
4452 ;; `set-buffer' statement obviously makes the buffer local
4453 ;; instance of `comint-eol-on-send' shadow this one.
4454 ;; I'm considering this a bug in Elisp.
4456 ;; This was previously cautioned against in the Lisp manual. It
4457 ;; has been sorted out in Emacs 21. -- fx
4458 (let ((comint-eol-on-send nil)
4459 (comint-input-filter (if hist comint-input-filter 'ignore)))
4460 (comint-send-input))
4461 ;; Adjust all windows whose points are incorrect.
4462 (if (null comint-process-echoes)
4463 (walk-windows
4464 (function
4465 (lambda (window)
4466 (if (and (eq (window-buffer window) inferior-erlang-buffer)
4467 (= (window-point window) insert-point))
4468 (set-window-point window
4469 (+ insert-point insert-length)))))
4470 nil t))
4471 (set-buffer old-buffer)
4472 (+ insert-point insert-length)))
4475 (defun inferior-erlang-strip-delete (&optional s)
4476 "Remove `^H' (delete) and the characters it was supposed to remove."
4477 (interactive)
4478 (if (and (boundp 'comint-last-input-end)
4479 (boundp 'comint-last-output-start))
4480 (save-excursion
4481 (goto-char
4482 (if (interactive-p)
4483 (symbol-value 'comint-last-input-end)
4484 (symbol-value 'comint-last-output-start)))
4485 (while (progn (skip-chars-forward "^\C-h")
4486 (not (eq (point) (point-max))))
4487 (delete-char 1)
4488 (or (bolp)
4489 (backward-delete-char 1))))))
4492 ;; Basically `comint-strip-ctrl-m', with a few extra checks.
4493 (defun inferior-erlang-strip-ctrl-m (&optional string)
4494 "Strip trailing `^M' characters from the current output group."
4495 (interactive)
4496 (if (and (boundp 'comint-last-input-end)
4497 (boundp 'comint-last-output-start))
4498 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
4499 (save-excursion
4500 (goto-char
4501 (if (interactive-p)
4502 (symbol-value 'comint-last-input-end)
4503 (symbol-value 'comint-last-output-start)))
4504 (while (re-search-forward "\r+$" pmark t)
4505 (replace-match "" t t))))))
4508 (defun inferior-erlang-compile ()
4509 "Compile the file in the current buffer.
4511 Should Erlang return `{error, nofile}' it could not load the object
4512 module after completing the compilation. This is due to a bug in the
4513 compile command `c' when using the option `outdir'.
4515 There exists two workarounds for this bug:
4517 1) Place the directory in the Erlang load path.
4519 2) Set the Emacs variable `erlang-compile-use-outdir' to nil.
4520 To do so, place the following line in your `~/.emacs'-file:
4521 (setq erlang-compile-use-outdir nil)"
4522 (interactive)
4523 (save-some-buffers)
4524 (or (inferior-erlang-running-p)
4525 (save-excursion
4526 (inferior-erlang)))
4527 (or (inferior-erlang-running-p)
4528 (error "Error starting inferior Erlang shell"))
4529 (let ((dir (file-name-directory (buffer-file-name)))
4530 ;;; (file (file-name-nondirectory (buffer-file-name)))
4531 (noext (substring (buffer-file-name) 0 -4))
4532 ;; Hopefully, noone else will ever use these...
4533 (tmpvar "Tmp7236")
4534 (tmpvar2 "Tmp8742")
4535 end)
4536 (inferior-erlang-display-buffer)
4537 (inferior-erlang-wait-prompt)
4538 (setq end (inferior-erlang-send-command
4539 (if erlang-compile-use-outdir
4540 (format "c(\"%s\", [{outdir, \"%s\"}])." noext dir)
4541 (format
4542 (concat
4543 "f(%s), {ok, %s} = file:get_cwd(), "
4544 "file:set_cwd(\"%s\"), "
4545 "%s = c(\"%s\"), file:set_cwd(%s), f(%s), %s.")
4546 tmpvar2 tmpvar
4548 tmpvar2 noext tmpvar tmpvar tmpvar2))
4549 nil))
4550 (inferior-erlang-wait-prompt)
4551 (save-excursion
4552 (set-buffer inferior-erlang-buffer)
4553 (setq compilation-error-list nil)
4554 (set-marker compilation-parsing-end end))
4555 (setq compilation-last-buffer inferior-erlang-buffer)))
4558 ;; `next-error' only accepts buffers with major mode `compilation-mode'
4559 ;; or with the minor mode `compilation-minor-mode' activated.
4560 ;; (To activate the minor mode is out of the question, since it will
4561 ;; ruin the inferior Erlang keymap.)
4562 ;; This is done differently in Emacs 21.
4563 (defun inferior-erlang-next-error (&optional argp)
4564 "Just like `next-error'.
4565 Capable of finding error messages in an inferior Erlang buffer."
4566 (interactive "P")
4567 (let ((done nil)
4568 (buf (and (boundp 'compilation-last-buffer)
4569 compilation-last-buffer)))
4570 (if (and (bufferp buf)
4571 (save-excursion
4572 (set-buffer buf)
4573 (and (eq major-mode 'erlang-shell-mode)
4574 (setq major-mode 'compilation-mode))))
4575 (unwind-protect
4576 (progn
4577 (setq done t)
4578 (next-error argp))
4579 (save-excursion
4580 (set-buffer buf)
4581 (setq major-mode 'erlang-shell-mode))))
4582 (or done
4583 (next-error argp))))
4586 (defun inferior-erlang-change-directory (&optional dir)
4587 "Make the inferior Erlang change directory.
4588 The default is to go to the directory of the current buffer."
4589 (interactive)
4590 (or dir (setq dir (file-name-directory (buffer-file-name))))
4591 (or (inferior-erlang-running-p)
4592 (error "No inferior Erlang is running"))
4593 (inferior-erlang-display-buffer)
4594 (inferior-erlang-wait-prompt)
4595 (inferior-erlang-send-command (format "cd('%s')." dir) nil))
4597 (defun erlang-align-arrows (start end)
4598 "Align arrows (\"->\") in function clauses from START to END.
4599 When called interactively, aligns arrows after function clauses inside
4600 the region.
4602 With a prefix argument, aligns all arrows, not just those in function
4603 clauses.
4605 Example:
4607 sum(L) -> sum(L, 0).
4608 sum([H|T], Sum) -> sum(T, Sum + H);
4609 sum([], Sum) -> Sum.
4611 becomes:
4613 sum(L) -> sum(L, 0).
4614 sum([H|T], Sum) -> sum(T, Sum + H);
4615 sum([], Sum) -> Sum."
4616 (interactive "r")
4617 (save-excursion
4618 (let (;; regexp for matching arrows. without a prefix argument,
4619 ;; the regexp matches function heads. With a prefix, it
4620 ;; matches any arrow.
4621 (re (if current-prefix-arg
4622 "^.*\\(\\)->"
4623 (eval-when-compile
4624 (concat "^" erlang-atom-regexp ".*\\(\\)->"))))
4625 ;; part of regexp matching directly before the arrow
4626 (arrow-match-pos (if current-prefix-arg
4628 (1+ erlang-atom-regexp-matches)))
4629 ;; accumulator for positions where arrows are found, ordered
4630 ;; by buffer position (from greatest to smallest)
4631 (arrow-positions '())
4632 ;; accumulator for longest distance from start of line to arrow
4633 (most-indent 0)
4634 ;; marker to track the end of the region we're aligning
4635 (end-marker (progn (goto-char end)
4636 (point-marker))))
4637 ;; Pass 1: Find the arrow positions, adjust the whitespace
4638 ;; before each arrow to one space, and find the greatest
4639 ;; indentation level.
4640 (goto-char start)
4641 (while (re-search-forward re end-marker t)
4642 (goto-char (match-beginning arrow-match-pos))
4643 (just-one-space) ; adjust whitespace
4644 (setq arrow-positions (cons (point) arrow-positions))
4645 (setq most-indent (max most-indent (erlang-column-number))))
4646 (set-marker end-marker nil) ; free the marker
4647 ;; Pass 2: Insert extra padding so that all arrow indentation is
4648 ;; equal. This is done last-to-first by buffer position, so that
4649 ;; inserting spaces before one arrow doesn't change the
4650 ;; positions of the next ones.
4651 (mapcar (lambda (arrow-pos)
4652 (goto-char arrow-pos)
4653 (let* ((pad (- most-indent (erlang-column-number))))
4654 (when (> pad 0)
4655 (insert-char ?\ pad))))
4656 arrow-positions))))
4658 (defun erlang-column-number ()
4659 "Return the column number of the current position in the buffer.
4660 Tab characters are counted by their visual width."
4661 (string-width (buffer-substring (line-beginning-position) (point))))
4663 (defun erlang-current-defun ()
4664 "`add-log-current-defun-function' for Erlang."
4665 (save-excursion
4666 (erlang-beginning-of-function)
4667 (if (looking-at "[a-z0-9_]+")
4668 (match-string 0))))
4670 ;; Aliases for backward compatibility with older versions of Erlang Mode.
4672 ;; Unfortuantely, older versions of Emacs doesn't have `defalias' and
4673 ;; `make-obsolete' so we have to define our own `obsolete' function.
4675 (defun erlang-obsolete (sym newdef)
4676 "Make the obsolete function SYM refer to the defined function NEWDEF.
4678 Simplified version of a combination `defalias' and `make-obsolete',
4679 it assumes that NEWDEF is loaded."
4680 (defalias sym (symbol-function newdef))
4681 (if (fboundp 'make-obsolete)
4682 (make-obsolete sym newdef)))
4685 (erlang-obsolete 'calculate-erlang-indent 'erlang-calculate-indent)
4686 (erlang-obsolete 'calculate-erlang-stack-indent
4687 'erlang-calculate-stack-indent)
4688 (erlang-obsolete 'at-erlang-keyword 'erlang-at-keyword)
4689 (erlang-obsolete 'at-erlang-operator 'erlang-at-operator)
4690 (erlang-obsolete 'beginning-of-erlang-clause 'erlang-beginning-of-clause)
4691 (erlang-obsolete 'end-of-erlang-clause 'erlang-end-of-clause)
4692 (erlang-obsolete 'mark-erlang-clause 'erlang-mark-clause)
4693 (erlang-obsolete 'beginning-of-erlang-function 'erlang-beginning-of-function)
4694 (erlang-obsolete 'end-of-erlang-function 'erlang-end-of-function)
4695 (erlang-obsolete 'mark-erlang-function 'erlang-mark-function)
4696 (erlang-obsolete 'pass-over-erlang-clause 'erlang-pass-over-function)
4697 (erlang-obsolete 'name-of-erlang-function 'erlang-name-of-function)
4700 ;; Fixme: shouldn't redefine `set-visited-file-name' anyhow -- see above.
4701 (defconst erlang-unload-hook
4702 (list (lambda ()
4703 (defalias 'set-visited-file-name
4704 'erlang-orig-set-visited-file-name)
4705 (when (featurep 'advice)
4706 (ad-unadvise 'Man-notify-when-ready)
4707 (ad-unadvise 'set-visited-file-name)))))
4709 ;; The end...
4711 (provide 'erlang)
4713 (run-hooks 'erlang-load-hook)
4715 ;; Local variables:
4716 ;; coding: iso-8859-1
4717 ;; End:
4719 ;;; erlang.el ends here