speling
[erlware-mode.git] / erlang.el
blob74ea45755494fc5db3894c7e8f090cf58f621754
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: 0.1.10
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 ;; The Erlware version of the mode has updated skeletons, syntax
32 ;; highlighting fixes, and indentation fixes. The latest Erlware mode
33 ;; can be obtained here:
35 ;; http://code.google.com/p/erlware-mode/downloads/list
37 ;;; Commentary:
39 ;; Introduction:
40 ;; ------------
42 ;; This package provides support for the programming language Erlang.
43 ;; The package provides an editing mode with lots of bells and
44 ;; whistles, compilation support, and it makes it possible for the
45 ;; user to start Erlang shells that run inside Emacs.
47 ;; See the Erlang distribution for full documentation of this package.
49 ;; Installation:
50 ;; ------------
52 ;; Place this file in Emacs load path, byte-compile it, and add the
53 ;; following line to the appropriate init file:
55 ;; (require 'erlang-start)
57 ;; The full documentation contains much more extensive description of
58 ;; the installation procedure.
60 ;; Reporting Bugs:
61 ;; --------------
63 ;; Please send bug reports to the following email address:
64 ;; erlang-bugs@erlang.org
65 ;; or if you have a patch suggestion to:
66 ;; erlang-patches@erlang.org
67 ;; Please state as exactly as possible:
68 ;; - Version number of Erlang Mode (see the menu), Emacs, Erlang,
69 ;; and of any other relevant software.
70 ;; - What the expected result was.
71 ;; - What you did, preferably in a repeatable step-by-step form.
72 ;; - A description of the unexpected result.
73 ;; - Relevant pieces of Erlang code causing the problem.
74 ;; - Personal Emacs customisations, if any.
76 ;; Should the Emacs generate an error, please set the Emacs variable
77 ;; `debug-on-error' to `t'. Repeat the error and enclose the debug
78 ;; information in your bug-report.
80 ;; To set the variable you can use the following command:
81 ;; M-x set-variable RET debug-on-error RET t RET
82 ;;; Code:
84 ;; Variables:
86 (defconst erlang-version "0.1.10"
87 "The version number of Erlware Erlang mode.")
89 (defvar erlang-man-root-dir nil
90 "The directory where the Erlang manual pages are installed.
91 The name should not contain the trailing slash.
93 Should this variable be nil, no manual pages will show up in the
94 Erlang mode menu.")
96 (defvar erlang-menu-items '(erlang-menu-base-items
97 erlang-menu-skel-items
98 erlang-menu-shell-items
99 erlang-menu-compile-items
100 erlang-menu-man-items
101 erlang-menu-personal-items
102 erlang-menu-version-items)
103 "*List of menu item list to combine to create Erlang mode menu.
105 External programs which temporarily add menu items to the Erlang mode
106 menu may use this variable. Please use the function `add-hook' to add
107 items.
109 Please call the function `erlang-menu-init' after every change to this
110 variable.")
112 (defvar erlang-menu-base-items
113 '(("Indent"
114 (("Indent Line" erlang-indent-command)
115 ("Indent Region " erlang-indent-region
116 (if erlang-xemacs-p (mark) mark-active))
117 ("Indent Clause" erlang-indent-clause)
118 ("Indent Function" erlang-indent-function)
119 ("Indent Buffer" erlang-indent-current-buffer)))
120 ("Edit"
121 (("Fill Comment" erlang-fill-paragraph)
122 ("Comment Region" comment-region
123 (if erlang-xemacs-p (mark) mark-active))
124 ("Uncomment Region" erlang-uncomment-region
125 (if erlang-xemacs-p (mark) mark-active))
127 ("Beginning of Function" erlang-beginning-of-function)
128 ("End of Function" erlang-end-of-function)
129 ("Mark Function" erlang-mark-function)
131 ("Beginning of Clause" erlang-beginning-of-clause)
132 ("End of Clause" erlang-end-of-clause)
133 ("Mark Clause" erlang-mark-clause)
135 ("New Clause" erlang-generate-new-clause)
136 ("Clone Arguments" erlang-clone-arguments)
138 ("Align Arrows" erlang-align-arrows)))
139 ("Syntax Highlighting"
140 (("Level 3" erlang-font-lock-level-3)
141 ("Level 2" erlang-font-lock-level-2)
142 ("Level 1" erlang-font-lock-level-1)
143 ("Off" erlang-font-lock-level-0)))
144 ("TAGS"
145 (("Find Tag" find-tag)
146 ("Find Next Tag" erlang-find-next-tag)
147 ("Complete Word" erlang-complete-tag)
148 ("Tags Apropos" tags-apropos)
149 ("Search Files" tags-search))))
150 "Description of menu used in Erlang mode.
152 This variable must be a list. The elements are either nil representing
153 a horizontal line or a list with two or three elements. The first is
154 the name of the menu item, the second is the function to call, or a
155 submenu, on the same same form as ITEMS. The third optional argument
156 is an expression which is evaluated every time the menu is displayed.
157 Should the expression evaluate to nil the menu item is ghosted.
159 Example:
160 '((\"Func1\" function-one)
161 (\"SubItem\"
162 ((\"Yellow\" function-yellow)
163 (\"Blue\" function-blue)))
165 (\"Region Function\" spook-function midnight-variable))
167 Call the function `erlang-menu-init' after modifying this variable.")
169 (defvar erlang-menu-shell-items
170 '(nil
171 ("Shell"
172 (("Start New Shell" erlang-shell)
173 ("Display Shell" erlang-shell-display))))
174 "Description of the Shell menu used by Erlang mode.
176 Please see the documentation of `erlang-menu-base-items'.")
178 (defvar erlang-menu-compile-items
179 '(("Compile"
180 (("Compile Buffer" erlang-compile)
181 ("Display Result" erlang-compile-display)
182 ("Next Error" erlang-next-error))))
183 "Description of the Compile menu used by Erlang mode.
185 Please see the documentation of `erlang-menu-base-items'.")
187 (defvar erlang-menu-version-items
188 '(nil
189 ("Version" erlang-version))
190 "Description of the version menu used in Erlang mode.")
192 (defvar erlang-menu-personal-items nil
193 "Description of personal menu items used in Erlang mode.
195 Please see the variable `erlang-menu-base-items' for a description
196 of the format.")
198 (defvar erlang-menu-man-items nil
199 "The menu containing man pages.
201 The format of the menu should be compatible with `erlang-menu-base-items'.
202 This variable is added to the list of Erlang menus stored in
203 `erlang-menu-items'.")
205 (defvar erlang-menu-skel-items '()
206 "Description of the menu containing the skeleton entries.
207 The menu is in the form described by the variable `erlang-menu-base-items'.")
209 (defvar erlang-mode-hook nil
210 "*Functions to run when Erlang mode is activated.
212 This hook is used to change the behaviour of Erlang mode. It is
213 normally used by the user to personalise the programming environment.
214 When used in a site init file, it could be used to customise Erlang
215 mode for all users on the system.
217 The functions added to this hook are run every time Erlang mode is
218 started. See also `erlang-load-hook', a hook which is run once,
219 when Erlang mode is loaded into Emacs, and `erlang-shell-mode-hook'
220 which is run every time a new inferior Erlang shell is started.
222 To use a hook, create an Emacs lisp function to perform your actions
223 and add the function to the hook by calling `add-hook'.
225 The following example binds the key sequence C-c C-c to the command
226 `erlang-compile' (normally bound to C-c C-k). The example also
227 activates Font Lock mode to fontify the buffer and adds a menu
228 containing all functions defined in the current buffer.
230 To use the example, copy the following lines to your `~/.emacs' file:
232 (add-hook 'erlang-mode-hook 'my-erlang-mode-hook)
234 (defun my-erlang-mode-hook ()
235 (local-set-key \"\\C-c\\C-c\" 'erlang-compile)
236 (if window-system
237 (progn
238 (setq font-lock-maximum-decoration t)
239 (font-lock-mode 1)))
240 (if (and window-system (fboundp 'imenu-add-to-menubar))
241 (imenu-add-to-menubar \"Imenu\")))")
243 (defvar erlang-load-hook nil
244 "*Functions to run when Erlang mode is loaded.
246 This hook is used to change the behaviour of Erlang mode. It is
247 normally used by the user to personalise the programming environment.
248 When used in a site init file, it could be used to customize Erlang
249 mode for all users on the system.
251 The difference between this hook and `erlang-mode-hook' and
252 `erlang-shell-mode-hook' is that the functions in this hook
253 is only called once, when the Erlang mode is loaded into Emacs
254 the first time.
256 Natural actions for the functions added to this hook are actions which
257 only should be performed once, and actions which should be performed
258 before starting Erlang mode. For example, a number of variables are
259 used by Erlang mode before `erlang-mode-hook' is run.
261 The following example sets the variable `erlang-man-root-dir' so that
262 the manual pages can be retrieved (note that you must set the value of
263 `erlang-man-root-dir' to match the location of the Erlang man pages
264 on your system):
266 (setq erlang-man-root-dir \"/usr/local/erlang\")")
268 (defvar erlang-new-file-hook nil
269 "Functions to run when a new Erlang source file is being edited.
271 A useful function is `tempo-template-erlang-normal-header'.
272 \(This function only exists when the `tempo' package is available.)")
274 (defvar erlang-check-module-name 'ask
275 "*Non-nil means check that module name and file name agrees when saving.
277 If the value of this variable is the atom `ask', the user is
278 prompted. If the value is t the source is silently changed.")
280 (defvar erlang-electric-commands
281 '(erlang-electric-comma
282 erlang-electric-semicolon
283 erlang-electric-newline
284 erlang-electric-gt)
285 "*List of activated electric commands.
287 The list should contain the electric commands which should be active.
288 Currently, the available electric commands are:
289 `erlang-electric-comma'
290 `erlang-electric-semicolon'
291 `erlang-electric-gt'
292 `erlang-electric-newline'
294 Should the variable be bound to t, all electric commands
295 are activated.
297 To deactivate all electric commands, set this variable to nil.")
299 (defvar erlang-electric-newline-inhibit t
300 "*Set to non-nil to inhibit newline after electric command.
302 This is useful since a lot of people press return after executing an
303 electric command.
305 In order to work, the command must also be in the
306 list `erlang-electric-newline-inhibit-list'.
308 Note that commands in this list are required to set the variable
309 `erlang-electric-newline-inhibit' to nil when the newline shouldn't be
310 inhibited.")
312 (defvar erlang-electric-newline-inhibit-list
313 '(erlang-electric-semicolon
314 erlang-electric-comma
315 erlang-electric-gt)
316 "*Commands which can inhibit the next newline.")
318 (defvar erlang-electric-semicolon-insert-blank-lines nil
319 "*Number of blank lines inserted before header, or nil.
321 This variable controls the behaviour of `erlang-electric-semicolon'
322 when a new function header is generated. When nil, no blank line is
323 inserted between the current line and the new header. When bound to a
324 number it represents the number of blank lines which should be
325 inserted.")
327 (defvar erlang-electric-semicolon-criteria
328 '(erlang-next-lines-empty-p
329 erlang-at-keyword-end-p
330 erlang-at-end-of-function-p)
331 "*List of functions controlling `erlang-electric-semicolon'.
332 The functions in this list are called, in order, whenever a semicolon
333 is typed. Each function in the list is called with no arguments,
334 and should return one of the following values:
336 nil -- no determination made, continue checking
337 'stop -- do not create prototype for next line
338 (anything else) -- insert prototype, and stop checking
340 If every function in the list is called with no determination made,
341 then no prototype is inserted.
343 The test is performed by the function `erlang-test-criteria-list'.")
345 (defvar erlang-electric-comma-criteria
346 '(erlang-stop-when-inside-argument-list
347 erlang-stop-when-at-guard
348 erlang-next-lines-empty-p
349 erlang-at-keyword-end-p
350 erlang-at-end-of-clause-p
351 erlang-at-end-of-function-p)
352 "*List of functions controlling `erlang-electric-comma'.
353 The functions in this list are called, in order, whenever a comma
354 is typed. Each function in the list is called with no arguments,
355 and should return one of the following values:
357 nil -- no determination made, continue checking
358 'stop -- do not create prototype for next line
359 (anything else) -- insert prototype, and stop checking
361 If every function in the list is called with no determination made,
362 then no prototype is inserted.
364 The test is performed by the function `erlang-test-criteria-list'.")
366 (defvar erlang-electric-arrow-criteria
367 '(erlang-next-lines-empty-p
368 erlang-at-end-of-function-p)
369 "*List of functions controlling the arrow aspect of `erlang-electric-gt'.
370 The functions in this list are called, in order, whenever a `>'
371 is typed. Each function in the list is called with no arguments,
372 and should return one of the following values:
374 nil -- no determination made, continue checking
375 'stop -- do not create prototype for next line
376 (anything else) -- insert prototype, and stop checking
378 If every function in the list is called with no determination made,
379 then no prototype is inserted.
381 The test is performed by the function `erlang-test-criteria-list'.")
383 (defvar erlang-electric-newline-criteria
384 '(t)
385 "*List of functions controlling `erlang-electric-newline'.
387 The electric newline commands indents the next line. Should the
388 current line begin with a comment the comment start is copied to
389 the newly created line.
391 The functions in this list are called, in order, whenever a comma
392 is typed. Each function in the list is called with no arguments,
393 and should return one of the following values:
395 nil -- no determination made, continue checking
396 'stop -- do not create prototype for next line
397 (anything else) -- trigger the electric command.
399 If every function in the list is called with no determination made,
400 then no prototype is inserted. Should the atom t be a member of the
401 list, it is treated as a function triggering the electric command.
403 The test is performed by the function `erlang-test-criteria-list'.")
405 (defvar erlang-next-lines-empty-threshold 2
406 "*Number of blank lines required to activate an electric command.
408 Actually, this value controls the behaviour of the function
409 `erlang-next-lines-empty-p' which normally is a member of the
410 criteria lists controlling the electric commands. (Please see
411 the variables `erlang-electric-semicolon-criteria' and
412 `erlang-electric-comma-criteria'.)
414 The variable is bound to a threshold value, a number, representing the
415 number of lines which must be empty.
417 Setting this variable to zero, electric commands will always be
418 triggered by `erlang-next-lines-empty-p', unless inhibited by other
419 rules.
421 Should this variable be nil, `erlang-next-lines-empty-p' will never
422 trigger an electric command. The same effect would be reached if the
423 function `erlang-next-lines-empty-p' would be removed from the criteria
424 lists.
426 Note that even if `erlang-next-lines-empty-p' should not trigger an
427 electric command, other functions in the criteria list could.")
429 (defvar erlang-new-clause-with-arguments nil
430 "*Non-nil means that the arguments are cloned when a clause is generated.
432 A new function header can be generated by calls to the function
433 `erlang-generate-new-clause' and by use of the electric semicolon.")
435 (defvar erlang-compile-use-outdir t
436 "*When nil, go to the directory containing source file when compiling.
438 This is a workaround for a bug in the `outdir' option of compile. If the
439 outdir is not in the current load path, Erlang doesn't load the object
440 module after it has been compiled.
442 To activate the workaround, place the following in your `~/.emacs' file:
443 (setq erlang-compile-use-outdir nil)")
445 (defvar erlang-indent-level 4
446 "*Indentation of Erlang calls/clauses within blocks.")
448 (defvar erlang-indent-guard 2
449 "*Indentation of Erlang guards.")
451 (defvar erlang-argument-indent 2
452 "*Indentation of the first argument in a function call.
453 When nil, indent to the column after the `(' of the
454 function.")
456 (defvar erlang-tab-always-indent t
457 "*Non-nil means TAB in Erlang mode should always re-indent the current line,
458 regardless of where in the line point is when the TAB command is used.")
460 (defvar erlang-error-regexp-alist
461 '(("^\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" . (1 2)))
462 "*Patterns for matching Erlang errors.")
464 (defvar erlang-man-inhibit (eq system-type 'windows-nt)
465 "Inhibit the creation of the Erlang Manual Pages menu.
467 The Windows distribution of Erlang does not include man pages, hence
468 there is no attempt to create the menu.")
470 (defvar erlang-man-dirs
471 '(("Man - Commands" "/man/man1" t)
472 ("Man - Modules" "/man/man3" t)
473 ("Man - Files" "/man/man4" t)
474 ("Man - Applications" "/man/man6" t))
475 "*The man directories displayed in the Erlang menu.
477 Each item in the list should be a list with three elements, the first
478 the name of the menu, the second the directory, and the last a flag.
479 Should the flag the nil, the directory is absolute, should it be non-nil
480 the directory is relative to the variable `erlang-man-root-dir'.")
482 (defvar erlang-man-max-menu-size 20
483 "*The maximum number of menu items in one menu allowed.")
485 (defvar erlang-man-display-function 'erlang-man-display
486 "*Function used to display man page.
488 The function is called with one argument, the name of the file
489 containing the man page. Use this variable when the default
490 function, `erlang-man-display', does not work on your system.")
492 (eval-and-compile
493 (defconst erlang-atom-regexp
494 "\\([a-z][A-Za-z0-9_]*\\|'\\(?:[^\\']?\\(?:\\\\'\\)?\\)*'\\)"
495 ;; "\\([a-z][a-zA-Z0-9_]*\\|'[^\n']*'\\)"
496 "Regexp which should match an Erlang atom.
498 The regexp must be surrounded with a pair of regexp parentheses."))
500 (defconst erlang-atom-regexp-matches 1
501 "Number of regexp parenthesis pairs in `erlang-atom-regexp'.
503 This is used to determine parenthesis matches in complex regexps which
504 contains `erlang-atom-regexp'.")
506 (defconst erlang-variable-regexp "\\([A-Z_][a-zA-Z0-9_]*\\)"
507 "Regexp which should match an Erlang variable.
509 The regexp must be surrounded with a pair of regexp parentheses.")
510 (defconst erlang-variable-regexp-matches 1
511 "Number of regexp parenthesis pairs in `erlang-variable-regexp'.
513 This is used to determine matches in complex regexps which contains
514 `erlang-variable-regexp'.")
516 (defvar erlang-defun-prompt-regexp (concat "^" erlang-atom-regexp "\\s *(")
517 "Regexp which should match beginning of a clause.")
519 (defvar erlang-file-name-extension-regexp "\\.[eh]rl$"
520 "*Regexp which should match an Erlang file name.
522 This regexp is used when an Erlang module name is extracted from the
523 name of an Erlang source file.
525 The regexp should only match the section of the file name which should
526 be excluded from the module name.
528 To match all files set this variable to \"\\\\(\\\\..*\\\\|\\\\)$\".
529 The matches all except the extension. This is useful if the Erlang
530 tags system should interpret tags on the form `module:tag' for
531 files written in other languages than Erlang.")
533 (defvar erlang-mode-map nil
534 "*Keymap used in Erlang mode.")
535 (defvar erlang-mode-abbrev-table nil
536 "Abbrev table in use in Erlang-mode buffers.")
537 (defvar erlang-mode-syntax-table nil
538 "Syntax table in use in Erlang-mode buffers.")
540 (defconst erlang-emacs-major-version
541 (if (boundp 'emacs-major-version)
542 emacs-major-version
543 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version)
544 (string-to-int (substring emacs-version
545 (match-beginning 1) (match-end 1))))
546 "Major version number of Emacs.")
548 (defconst erlang-emacs-minor-version
549 (if (boundp 'emacs-minor-version)
550 emacs-minor-version
551 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version)
552 (string-to-int (substring emacs-version
553 (match-beginning 2) (match-end 2))))
554 "Minor version number of Emacs.")
556 (defconst erlang-xemacs-p (string-match "Lucid\\|XEmacs" emacs-version)
557 "Non-nil when running under XEmacs or Lucid Emacs.")
559 (defvar erlang-xemacs-popup-menu '("Erlang Mode Commands" . nil)
560 "Common popup menu for all buffers in Erlang mode.
562 This variable is destructively modified every time the Erlang menu
563 is modified. The effect is that all changes take effect in all
564 buffers in Erlang mode, just like under GNU Emacs.
566 Never EVER set this variable!")
568 (defconst inferior-erlang-use-cmm (boundp 'minor-mode-overriding-map-alist)
569 "Non-nil means use `compilation-minor-mode' in Erlang shell.")
571 ;; Tempo skeleton templates:
572 (load "erlang-skels")
574 ;; Sinan commands:
575 (load "erlang-sinan")
578 ;; Font-lock variables
580 (defvar erlang-font-lock-modern-p
581 (cond ((>= erlang-emacs-major-version 20) t)
582 (erlang-xemacs-p (>= erlang-emacs-minor-version 14))
583 ((= erlang-emacs-major-version 19) (>= erlang-emacs-minor-version 29))
584 (t nil))
585 "Non-nil when this version of Emacs uses a modern version of Font Lock.
587 This is determined by checking the version of Emacs used, the actual
588 font-lock code is not loaded.")
591 ;; The next few variables define different Erlang font-lock patterns.
592 ;; They could be appended to form a custom font-lock appearance.
594 ;; The function `erlang-font-lock-set-face' could be used to change
595 ;; the face of a pattern.
597 ;; Note that Erlang strings and atoms are highlighted with using
598 ;; syntactic analysis.
600 (defvar erlang-font-lock-keywords-func
601 (list
602 (list (concat "^" erlang-atom-regexp "\\s *(")
603 1 'font-lock-function-name-face t))
604 "Font lock keyword highlighting a function header.")
606 (defvar erlang-font-lock-keywords-func-call
607 (list
608 (list (concat "\\<" erlang-atom-regexp "\\s-*(")
609 1 'font-lock-type-face))
610 "Font lock keyword highlighting an internal function call.")
612 (defvar erlang-font-lock-keywords-ext-func-call
613 (list
614 (list (concat "\\<\\(" erlang-atom-regexp "\\s-*:"
615 "\\s-*" erlang-atom-regexp "\\)\\s-*(")
616 1 'font-lock-type-face))
617 "Font lock keyword highlighting an external function call.")
619 (defvar erlang-font-lock-keywords-fn
620 (list
621 (list (concat "\\(" erlang-atom-regexp "/[0-9]+\\)")
622 1 'font-lock-function-name-face))
623 "Font lock keyword highlighting a F/N fun descriptor.")
625 (defvar erlang-font-lock-keywords-plusplus
626 (list
627 (list (concat "\\(\\+\\+\\)")
628 1 'font-lock-warning-face))
629 "Font lock keyword highlighting the `++' operator.")
631 (defvar erlang-font-lock-keywords-dollar
632 (list
633 (list "\\(\\$\\([^\\]\\|\\\\\\([^0-7^\n]\\|[0-7]+\\|\\^[a-zA-Z]\\)\\)\\)"
634 1 'font-lock-string-face))
635 "Font lock keyword highlighting numbers in ASCII form (e.g. $A).")
637 (defvar erlang-font-lock-keywords-lc
638 (list
639 (list "\\(<-\\)" 1 'font-lock-keyword-face)
640 (list "\\(||\\)" 1 'font-lock-keyword-face))
641 "Font lock keyword highlighting list comprehension operators.")
643 (defvar erlang-font-lock-keywords-keywords
644 (list
645 (list (concat "\\<\\(a\\(fter\\|ndalso\\)\\|begin\\|c\\(atch\\|ase\\)"
646 "\\|end\\|fun\\|if\\|o\\(f\\|relse\\)\\|receive\\|try\\|when"
647 "\\|query\\)\\([^a-zA-Z0-9_]\\|$\\)")
648 1 'font-lock-keyword-face))
649 "Font lock keyword highlighting Erlang keywords.")
651 (defvar erlang-font-lock-keywords-attr
652 (list
653 (list (concat "^\\(-" erlang-atom-regexp "\\)\\(\\s-\\|\\.\\|(\\)")
654 1 'font-lock-preprocessor-face))
655 "Font lock keyword highlighting attributes.")
657 (defvar erlang-font-lock-keywords-quotes
658 (list
659 (list "`\\([-+a-zA-Z0-9_:*][-+a-zA-Z0-9_:*]+\\)'"
660 1 'font-lock-keyword-face t))
661 "Font lock keyword highlighting words in single quotes in comments.
663 This is not the highlighting of Erlang strings and atoms, which
664 are highlighted by syntactic analysis.")
666 ;; Note: The deprecated guard `float' collides with the bif `float'.
667 (defvar erlang-font-lock-keywords-guards
668 (list
669 (list
670 (concat "[^:]\\<\\("
671 "\\(is_\\)?\\(atom\\|boolean\\|function\\|binary\\|constant"
672 "\\|integer\\|list\\|number\\|p\\(id\\|ort\\)\\|"
673 "re\\(ference\\|cord\\)\\|tuple"
674 "\\)\\)\\s *(")
676 (if erlang-font-lock-modern-p
677 'font-lock-builtin-face
678 'font-lock-keyword-face)))
679 "Font lock keyword highlighting guards.")
681 (defvar erlang-font-lock-keywords-bifs
682 (list
683 (list
684 (concat
685 "[^:]\\<\\("
686 "a\\(bs\\|live\\|pply\\|tom_to_list\\)\\|"
687 "binary_to_\\(list\\|term\\)\\|"
688 "concat_binary\\|d\\(ate\\|isconnect_node\\)\\|"
689 "e\\(lement\\|rase\\|xit\\)\\|"
690 "floa\\(t\\|t_to_list\\)\\|"
691 "g\\(arbage_collect\\|et\\(\\|_keys\\)\\|roup_leader\\)\\|"
692 "h\\(alt\\|d\\)\\|"
693 "i\\(nte\\(ger_to_list\\|rnal_bif\\)\\|s_alive\\)\\|"
694 "l\\(ength\\|i\\(nk\\|st_to_\\(atom\\|binary\\|float\\|integer"
695 "\\|pid\\|tuple\\)\\)\\)\\|"
696 "make_ref\\|no\\(de\\(\\|_\\(link\\|unlink\\)\\|s\\)\\|talive\\)\\|"
697 "open_port\\|"
698 "p\\(id_to_list\\|rocess\\(_\\(flag\\|info\\)\\|es\\)\\|ut\\)\\|"
699 "r\\(egister\\(\\|ed\\)\\|ound\\)\\|"
700 "s\\(e\\(lf\\|telement\\)\\|ize\\|"
701 "p\\(awn\\(\\|_link\\)\\|lit_binary\\)\\|tatistics\\)\\|"
702 "t\\(erm_to_binary\\|hrow\\|ime\\|l\\|"
703 "r\\(ace\\|unc\\)\\|uple_to_list\\)\\|"
704 "un\\(link\\|register\\)\\|whereis"
705 "\\)\\s *(")
707 'font-lock-builtin-face))
708 "Font lock keyword highlighting built in functions.")
710 (defvar erlang-font-lock-keywords-macros
711 (list
712 (list (concat "?\\s *\\(" erlang-atom-regexp
713 "\\|" erlang-variable-regexp "\\)\\>")
714 1 'font-lock-preprocessor-face)
715 (list (concat "^-\\(define\\|ifn?def\\)\\s *(\\s *\\(" erlang-atom-regexp
716 "\\|" erlang-variable-regexp "\\)\\>")
717 2 'font-lock-preprocessor-face))
718 "Font lock keyword highlighting macros.
719 This must be placed in front of `erlang-font-lock-keywords-vars'.")
721 (defvar erlang-font-lock-keywords-records
722 (list
723 (list (concat "#\\s *" erlang-atom-regexp "\\>")
724 1 'font-lock-preprocessor-face)
725 ;; Don't highlight numerical constants.
726 (list "\\<[0-9][0-9]?#\\([0-9a-fA_F]+\\)\\>"
727 1 nil t)
728 (list (concat "^-record(\\s *" erlang-atom-regexp "\\>")
729 1 'font-lock-preprocessor-face))
730 "Font lock keyword highlighting Erlang records.
731 This must be placed in front of `erlang-font-lock-keywords-vars'.")
733 (defvar erlang-font-lock-keywords-vars
734 (list
735 (list (concat "\\<" erlang-variable-regexp "\\>")
736 1 (if erlang-font-lock-modern-p
737 'font-lock-variable-name-face
738 'font-lock-type-face)))
739 "Font lock keyword highlighting Erlang variables.
740 Must be preceded by `erlang-font-lock-keywords-macros' and `-records'
741 to work properly.")
743 (defvar erlang-font-lock-keywords-atom
744 (list
745 (list (concat "\\<" erlang-atom-regexp "\\>")
746 1 'font-lock-constant-face))
747 "Font lock keyword highlighting Erlang atoms.")
749 (defvar erlang-font-lock-keywords-1
750 (append erlang-font-lock-keywords-func
751 erlang-font-lock-keywords-keywords)
752 ;; DocStringOrig: erlang-font-lock-keywords
753 "Font-lock keywords used by Erlang Mode.
755 There exists three levels of Font Lock keywords for Erlang:
756 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
757 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
758 `erlang-font-lock-keywords-3' - Variables, macros and records.
760 To use a specific level, please set the variable
761 `font-lock-maximum-decoration' to the appropriate level. Note that the
762 variable must be set before Erlang mode is activated.
764 Example:
765 (setq font-lock-maximum-decoration 2)")
768 (defvar erlang-font-lock-keywords-2
769 (append erlang-font-lock-keywords-1
770 erlang-font-lock-keywords-attr
771 erlang-font-lock-keywords-quotes
772 erlang-font-lock-keywords-guards
773 erlang-font-lock-keywords-bifs)
774 ;; DocStringCopy: erlang-font-lock-keywords
775 "Font-lock keywords used by Erlang Mode.
777 There exists three levels of Font Lock keywords for Erlang:
778 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
779 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
780 `erlang-font-lock-keywords-3' - Variables, macros and records.
782 To use a specific level, please set the variable
783 `font-lock-maximum-decoration' to the appropriate level. Note that the
784 variable must be set before Erlang mode is activated.
786 Example:
787 (setq font-lock-maximum-decoration 2)")
790 (defvar erlang-font-lock-keywords-3
791 (append erlang-font-lock-keywords-2
792 erlang-font-lock-keywords-macros
793 erlang-font-lock-keywords-records
794 erlang-font-lock-keywords-ext-func-call
795 erlang-font-lock-keywords-func-call
796 erlang-font-lock-keywords-fn
797 erlang-font-lock-keywords-plusplus
798 erlang-font-lock-keywords-lc
799 erlang-font-lock-keywords-dollar
800 erlang-font-lock-keywords-vars
801 erlang-font-lock-keywords-atom
803 ;; DocStringCopy: erlang-font-lock-keywords
804 "Font-lock keywords used by Erlang Mode.
806 There exists three levels of Font Lock keywords for Erlang:
807 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
808 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
809 `erlang-font-lock-keywords-3' - Variables, macros and records.
811 To use a specific level, please set the variable
812 `font-lock-maximum-decoration' to the appropriate level. Note that the
813 variable must be set before Erlang mode is activated.
815 Example:
816 (setq font-lock-maximum-decoration 2)")
819 (defvar erlang-font-lock-keywords erlang-font-lock-keywords-3
820 ;; DocStringCopy: erlang-font-lock-keywords
821 "Font-lock keywords used by Erlang Mode.
823 There exists three levels of Font Lock keywords for Erlang:
824 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
825 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
826 `erlang-font-lock-keywords-3' - Variables, macros and records.
828 To use a specific level, please set the variable
829 `font-lock-maximum-decoration' to the appropriate level. Note that the
830 variable must be set before Erlang mode is activated.
832 Example:
833 (setq font-lock-maximum-decoration 2)")
836 (defvar erlang-font-lock-syntax-table nil
837 "Syntax table used by Font Lock mode.
839 The difference between this and the standard Erlang Mode
840 syntax table is that `_' is treated as part of words by
841 this syntax table.
843 Unfortunately, XEmacs hasn't got support for a special Font
844 Lock syntax table. The effect is that `apply' in the atom
845 `foo_apply' will be highlighted as a bif.")
848 ;;; Avoid errors while compiling this file.
850 ;; `eval-when-compile' is not defined in Emacs 18. We define it as a
851 ;; no-op.
852 (or (fboundp 'eval-when-compile)
853 (defmacro eval-when-compile (&rest rest) nil))
855 ;; These umm...functions are new in Emacs 20. And, yes, until version
856 ;; 19.27 Emacs backquotes were this ugly.
858 (or (fboundp 'unless)
859 (defmacro unless (condition &rest body)
860 "(unless CONDITION BODY...): If CONDITION is false, do BODY, else return nil."
861 (` (if (, condition)
863 (,@ body)))))
865 (or (fboundp 'when)
866 (defmacro when (condition &rest body)
867 "(when CONDITION BODY...): If CONDITION is true, do BODY, else return nil."
868 (` (if (, condition)
869 (progn (,@ body))
870 nil))))
872 (or (fboundp 'char-before)
873 (defmacro char-before (&optional pos)
874 "Return the character in the current buffer just before POS."
875 (` (char-after (1- (or (, pos) (point)))))))
877 (eval-when-compile
878 (if (or (featurep 'bytecomp)
879 (featurep 'byte-compile))
880 (progn
881 (cond ((string-match "Lucid\\|XEmacs" emacs-version)
882 (put 'comment-indent-hook 'byte-obsolete-variable nil)
883 ;; Do not warn for unused variables
884 ;; when compiling under XEmacs.
885 (setq byte-compile-warnings
886 '(free-vars unresolved callargs redefine))))
887 (require 'comint)
888 (require 'compile))))
891 (defun erlang-version ()
892 "Return the current version of Erlang mode."
893 (interactive)
894 (if (interactive-p)
895 (message "Erlware Erlang mode version %s" erlang-version))
896 erlang-version)
899 ;;;###autoload
900 (defun erlang-mode ()
901 "Major mode for editing Erlang source files in Emacs.
902 It knows about syntax and comment, it can indent code, it is capable
903 of fontifying the source file, the TAGS commands are aware of Erlang
904 modules, and the Erlang man pages can be accessed.
906 Should this module, \"erlang.el\", be installed properly, Erlang mode
907 is activated whenever an Erlang source or header file is loaded into
908 Emacs. To indicate this, the mode line should contain the word
909 \"Erlang\".
911 The main feature of Erlang mode is indentation, press TAB and the
912 current line will be indented correctly.
914 Comments starting with one `%' are indented with the same indentation
915 as code. Comments starting with at least two `%':s are indented to
916 the first column.
918 However, Erlang mode contains much more, this is a list of the most
919 useful commands:
920 TAB - Indent the line.
921 C-c C-q - Indent current function.
922 M-; - Create a comment at the end of the line.
923 M-q - Fill a comment, i.e. wrap lines so that they (hopefully)
924 will look better.
925 M-a - Goto the beginning of an Erlang clause.
926 M-C-a - Ditto for function.
927 M-e - Goto the end of an Erlang clause.
928 M-C-e - Ditto for function.
929 M-h - Mark current Erlang clause.
930 M-C-h - Ditto for function.
931 C-c C-z - Start, or switch to, an inferior Erlang shell.
932 C-c C-k - Compile current file.
933 C-x ` - Next error.
934 , - Electric comma.
935 ; - Electric semicolon.
937 Erlang mode check the name of the file against the module name when
938 saving, whenever a mismatch occurs Erlang mode offers to modify the
939 source.
941 The variable `erlang-electric-commands' controls the electric
942 commands. To deactivate all of them, set it to nil.
944 There exists a large number of commands and variables in the Erlang
945 module. Please press `M-x apropos RET erlang RET' to see a complete
946 list. Press `C-h f name-of-function RET' and `C-h v name-of-variable
947 RET'to see the full description of functions and variables,
948 respectively.
950 On entry to this mode the contents of the hook `erlang-mode-hook' is
951 executed.
953 Please see the beginning of the file `erlang.el' for more information
954 and examples of hooks.
956 Other commands:
957 \\{erlang-mode-map}"
958 (interactive)
959 (kill-all-local-variables)
960 (setq major-mode 'erlang-mode)
961 (setq mode-name "Erlang")
962 (erlang-syntax-table-init)
963 (erlang-keymap-init)
964 (erlang-electric-init)
965 (erlang-menu-init)
966 (erlang-mode-variables)
967 (erlang-check-module-name-init)
968 (erlang-add-compilation-alist erlang-error-regexp-alist)
969 (erlang-man-init)
970 (erlang-tags-init)
971 (erlang-font-lock-init)
972 (erlang-skel-init)
973 (run-hooks 'erlang-mode-hook)
974 (if (zerop (buffer-size))
975 (run-hooks 'erlang-new-file-hook)))
978 (defun erlang-syntax-table-init ()
979 (if (null erlang-mode-syntax-table)
980 (let ((table (make-syntax-table)))
981 (modify-syntax-entry ?\n ">" table)
982 (modify-syntax-entry ?\" "\"" table)
983 (modify-syntax-entry ?# "." table)
984 (modify-syntax-entry ?$ "\\" table)
985 (modify-syntax-entry ?% "<" table)
986 (modify-syntax-entry ?& "." table)
987 (modify-syntax-entry ?\' "w" table)
988 (modify-syntax-entry ?* "." table)
989 (modify-syntax-entry ?+ "." table)
990 (modify-syntax-entry ?- "." table)
991 (modify-syntax-entry ?/ "." table)
992 (modify-syntax-entry ?: "." table)
993 (modify-syntax-entry ?< "." table)
994 (modify-syntax-entry ?= "." table)
995 (modify-syntax-entry ?> "." table)
996 (modify-syntax-entry ?\\ "\\" table)
997 (modify-syntax-entry ?_ "_" table)
998 (modify-syntax-entry ?| "." table)
1000 (setq erlang-mode-syntax-table table)))
1002 (set-syntax-table erlang-mode-syntax-table))
1005 (defun erlang-keymap-init ()
1006 (if erlang-mode-map
1008 (setq erlang-mode-map (make-sparse-keymap))
1009 (erlang-mode-commands erlang-mode-map))
1010 (use-local-map erlang-mode-map))
1013 (defun erlang-mode-commands (map)
1014 (unless (boundp 'indent-line-function)
1015 (define-key map "\t" 'erlang-indent-command))
1016 (define-key map ";" 'erlang-electric-semicolon)
1017 (define-key map "," 'erlang-electric-comma)
1018 (define-key map "<" 'erlang-electric-lt)
1019 (define-key map ">" 'erlang-electric-gt)
1020 (define-key map "\C-m" 'erlang-electric-newline)
1021 (if (not (boundp 'delete-key-deletes-forward))
1022 (define-key map "\177" 'backward-delete-char-untabify)
1023 (define-key map [backspace] 'backward-delete-char-untabify))
1024 (define-key map "\M-q" 'erlang-fill-paragraph)
1025 (unless (boundp 'beginning-of-defun-function)
1026 (define-key map "\M-\C-a" 'erlang-beginning-of-function)
1027 (define-key map "\M-\C-e" 'erlang-end-of-function)
1028 (define-key map "\M-\C-h" 'erlang-mark-function))
1029 (define-key map "\M-\t" 'erlang-complete-tag)
1030 (define-key map "\C-c\M-\t" 'tempo-complete-tag)
1031 (define-key map "\M-+" 'erlang-find-next-tag)
1032 (define-key map "\C-c\M-a" 'erlang-beginning-of-clause)
1033 (define-key map "\C-c\M-b" 'tempo-backward-mark)
1034 (define-key map "\C-c\M-e" 'erlang-end-of-clause)
1035 (define-key map "\C-c\M-f" 'tempo-forward-mark)
1036 (define-key map "\C-c\M-h" 'erlang-mark-clause)
1037 (define-key map "\C-c\C-c" 'comment-region)
1038 (define-key map "\C-c\C-j" 'erlang-generate-new-clause)
1039 (define-key map "\C-c\C-k" 'erlang-compile)
1040 (define-key map "\C-c\C-l" 'erlang-compile-display)
1041 (define-key map "\C-c\C-s" 'erlang-show-syntactic-information)
1042 (define-key map "\C-c\C-q" 'erlang-indent-function)
1043 (define-key map "\C-c\C-u" 'erlang-uncomment-region)
1044 (define-key map "\C-c\C-y" 'erlang-clone-arguments)
1045 (define-key map "\C-c\C-a" 'erlang-align-arrows)
1046 (define-key map "\C-c\C-z" 'erlang-shell-display)
1047 (unless inferior-erlang-use-cmm
1048 (define-key map "\C-x`" 'erlang-next-error)))
1051 (defun erlang-electric-init ()
1052 ;; Set up electric character functions to work with
1053 ;; delsel/pending-del mode. Also, set up text properties for bit
1054 ;; syntax handling.
1055 (mapcar #'(lambda (cmd)
1056 (put cmd 'delete-selection t) ;for delsel (Emacs)
1057 (put cmd 'pending-delete t)) ;for pending-del (XEmacs)
1058 '(erlang-electric-semicolon
1059 erlang-electric-comma
1060 erlang-electric-gt))
1062 (put 'bitsyntax-open-outer 'syntax-table '(4 . ?>))
1063 (put 'bitsyntax-open-outer 'rear-nonsticky '(category))
1064 (put 'bitsyntax-open-inner 'rear-nonsticky '(category))
1065 (put 'bitsyntax-close-inner 'rear-nonsticky '(category))
1066 (put 'bitsyntax-close-outer 'syntax-table '(5 . ?<))
1067 (put 'bitsyntax-close-outer 'rear-nonsticky '(category))
1068 (setq parse-sexp-lookup-properties 't))
1071 (defun erlang-mode-variables ()
1072 (or erlang-mode-abbrev-table
1073 (define-abbrev-table 'erlang-mode-abbrev-table ()))
1074 (setq local-abbrev-table erlang-mode-abbrev-table)
1075 (make-local-variable 'paragraph-start)
1076 (setq paragraph-start (concat "^$\\|" page-delimiter))
1077 (make-local-variable 'paragraph-separate)
1078 (setq paragraph-separate paragraph-start)
1079 (make-local-variable 'paragraph-ignore-fill-prefix)
1080 (setq paragraph-ignore-fill-prefix t)
1081 (make-local-variable 'require-final-newline)
1082 (setq require-final-newline t)
1083 (make-local-variable 'defun-prompt-regexp)
1084 (setq defun-prompt-regexp erlang-defun-prompt-regexp)
1085 (make-local-variable 'comment-start)
1086 (setq comment-start "%")
1087 (make-local-variable 'comment-start-skip)
1088 (setq comment-start-skip "%+\\s *")
1089 (make-local-variable 'indent-line-function)
1090 (setq indent-line-function 'erlang-indent-command)
1091 (make-local-variable 'indent-region-function)
1092 (setq indent-region-function 'erlang-indent-region)
1093 (set (make-local-variable 'comment-indent-function) 'erlang-comment-indent)
1094 (if (<= erlang-emacs-major-version 18)
1095 (set (make-local-variable 'comment-indent-hook) 'erlang-comment-indent))
1096 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1097 (set (make-local-variable 'dabbrev-case-fold-search) nil)
1098 (set (make-local-variable 'imenu-prev-index-position-function)
1099 'erlang-beginning-of-function)
1100 (set (make-local-variable 'imenu-extract-index-name-function)
1101 'erlang-get-function-name)
1102 (set (make-local-variable 'tempo-match-finder)
1103 "[^-a-zA-Z0-9_]\\([-a-zA-Z0-9_]*\\)\\=")
1104 (set (make-local-variable 'beginning-of-defun-function)
1105 'erlang-beginning-of-function)
1106 (set (make-local-variable 'end-of-defun-function) 'erlang-end-of-function)
1107 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil)
1108 (set (make-local-variable 'fill-paragraph-function) 'erlang-fill-paragraph)
1109 (set (make-local-variable 'comment-add) 1)
1110 (set (make-local-variable 'outline-regexp) "[[:lower:]0-9_]+ *(.*) *-> *$")
1111 (set (make-local-variable 'outline-level) (lambda () 1))
1112 (set (make-local-variable 'add-log-current-defun-function)
1113 'erlang-current-defun))
1116 ;; Compilation.
1118 ;; The following code is compatible with the standard package `compilation',
1119 ;; making it possible to go to errors using `erlang-next-error' (or just
1120 ;; `next-error' in Emacs 21).
1122 ;; The normal `compile' command works of course. For best result, please
1123 ;; execute `make' with the `-w' flag.
1125 ;; Please see the variables named `compiling-..' above.
1127 (defun erlang-add-compilation-alist (alist)
1128 (require 'compile)
1129 (cond ((boundp 'compilation-error-regexp-alist) ; Emacs 19
1130 (while alist
1131 (or (assoc (car (car alist)) compilation-error-regexp-alist)
1132 (setq compilation-error-regexp-alist
1133 (cons (car alist) compilation-error-regexp-alist)))
1134 (setq alist (cdr alist))))
1135 ((boundp 'compilation-error-regexp)
1136 ;; Emacs 18, Only one regexp is allowed.
1137 (funcall (symbol-function 'set)
1138 'compilation-error-regexp (car (car alist))))))
1140 (defun erlang-font-lock-init ()
1141 "Initialize Font Lock for Erlang mode."
1142 (or erlang-font-lock-syntax-table
1143 (setq erlang-font-lock-syntax-table
1144 (let ((table (copy-syntax-table erlang-mode-syntax-table)))
1145 (modify-syntax-entry ?_ "w" table)
1146 table)))
1147 (set (make-local-variable 'font-lock-syntax-table)
1148 erlang-font-lock-syntax-table)
1149 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1150 'erlang-beginning-of-clause)
1151 (make-local-variable 'font-lock-keywords)
1152 (let ((level (cond ((boundp 'font-lock-maximum-decoration)
1153 (symbol-value 'font-lock-maximum-decoration))
1154 ((boundp 'font-lock-use-maximal-decoration)
1155 (symbol-value 'font-lock-use-maximal-decoration))
1156 (t nil))))
1157 (if (consp level)
1158 (setq level (cdr-safe (or (assq 'erlang-mode level)
1159 (assq t level)))))
1160 ;; `level' can here be:
1161 ;; A number - The fontification level
1162 ;; nil - Use the default
1163 ;; t - Use maximum
1164 (cond ((eq level nil)
1165 (set 'font-lock-keywords erlang-font-lock-keywords))
1166 ((eq level 1)
1167 (set 'font-lock-keywords erlang-font-lock-keywords-1))
1168 ((eq level 2)
1169 (set 'font-lock-keywords erlang-font-lock-keywords-2))
1171 (set 'font-lock-keywords erlang-font-lock-keywords-3))))
1173 ;; Modern font-locks can handle the above much more elegantly:
1174 (set (make-local-variable 'font-lock-defaults)
1175 '((erlang-font-lock-keywords erlang-font-lock-keywords-1
1176 erlang-font-lock-keywords-2 erlang-font-lock-keywords-3)
1177 nil nil ((?_ . "w")) erlang-beginning-of-clause
1178 (font-lock-mark-block-function . erlang-mark-clause))))
1182 ;; Useful when defining your own keywords.
1183 (defun erlang-font-lock-set-face (ks &rest faces)
1184 "Replace the face components in a list of keywords.
1186 The first argument, KS, is a list of keywords. The rest of the
1187 arguments are expressions to replace the face information with. The
1188 first expression replaces the face of the first keyword, the second
1189 expression the second keyword etc.
1191 Should an expression be nil, the face of the corresponding keyword is
1192 not changed.
1194 Should fewer expressions than keywords be given, the last expression
1195 is used for all remaining keywords.
1197 Normally, the expressions are just atoms representing the new face.
1198 They could however be more complex, returning different faces in
1199 different situations.
1201 This function only handles keywords with elements on the forms:
1202 (REGEXP NUMBER FACE)
1203 (REGEXP NUMBER FACE OVERWRITE)
1205 This could be used when defining your own special font-lock setup, e.g:
1207 \(setq my-font-lock-keywords
1208 (append erlang-font-lock-keywords-func
1209 erlang-font-lock-keywords-dollar
1210 (erlang-font-lock-set-face
1211 erlang-font-lock-keywords-macros 'my-neon-green-face)
1212 (erlang-font-lock-set-face
1213 erlang-font-lock-keywords-lc 'my-deep-red 'my-light-red)
1214 erlang-font-lock-keywords-attr))
1216 For a more elaborate example, please see the beginning of the file
1217 `erlang.el'."
1218 (let ((res '()))
1219 (while ks
1220 (let* ((regexp (car (car ks)))
1221 (number (car (cdr (car ks))))
1222 (new-face (if (and faces (car faces))
1223 (car faces)
1224 (car (cdr (cdr (car ks))))))
1225 (overwrite (car (cdr (cdr (cdr (car ks))))))
1226 (new-keyword (list regexp number new-face)))
1227 (if overwrite (nconc new-keyword (list overwrite)))
1228 (setq res (cons new-keyword res))
1229 (setq ks (cdr ks))
1230 (if (and faces (cdr faces))
1231 (setq faces (cdr faces)))))
1232 (nreverse res)))
1235 (defun erlang-font-lock-level-0 ()
1236 ;; DocStringOrig: font-cmd
1237 "Unfontify current buffer."
1238 (interactive)
1239 (font-lock-mode 0))
1242 (defun erlang-font-lock-level-1 ()
1243 ;; DocStringCopy: font-cmd
1244 "Fontify current buffer at level 1.
1245 This highlights function headers, reserved keywords, strings and comments."
1246 (interactive)
1247 (require 'font-lock)
1248 (set 'font-lock-keywords erlang-font-lock-keywords-1)
1249 (font-lock-mode 1)
1250 (funcall (symbol-function 'font-lock-fontify-buffer)))
1253 (defun erlang-font-lock-level-2 ()
1254 ;; DocStringCopy: font-cmd
1255 "Fontify current buffer at level 2.
1256 This highlights level 1 features (see `erlang-font-lock-level-1')
1257 plus bifs, guards and `single quotes'."
1258 (interactive)
1259 (require 'font-lock)
1260 (set 'font-lock-keywords erlang-font-lock-keywords-2)
1261 (font-lock-mode 1)
1262 (funcall (symbol-function 'font-lock-fontify-buffer)))
1265 (defun erlang-font-lock-level-3 ()
1266 ;; DocStringCopy: font-cmd
1267 "Fontify current buffer at level 3.
1268 This highlights level 2 features (see `erlang-font-lock-level-2')
1269 plus variables, macros and records."
1270 (interactive)
1271 (require 'font-lock)
1272 (set 'font-lock-keywords erlang-font-lock-keywords-3)
1273 (font-lock-mode 1)
1274 (funcall (symbol-function 'font-lock-fontify-buffer)))
1277 (defun erlang-menu-init ()
1278 "Init menus for Erlang mode.
1280 The variable `erlang-menu-items' contain a description of the Erlang
1281 mode menu. Normally, the list contains atoms, representing variables
1282 bound to pieces of the menu.
1284 Personal extensions could be added to `erlang-menu-personal-items'.
1286 This function should be called if any variable describing the
1287 menu configuration is changed."
1288 (erlang-menu-install "Erlang" erlang-menu-items erlang-mode-map t))
1291 (defun erlang-menu-install (name items keymap &optional popup)
1292 "Install a menu in Emacs or XEmacs based on an abstract description.
1294 NAME is the name of the menu.
1296 ITEMS is a list. The elements are either nil representing a horizontal
1297 line or a list with two or three elements. The first is the name of
1298 the menu item, the second the function to call, or a submenu, on the
1299 same same form as ITEMS. The third optional element is an expression
1300 which is evaluated every time the menu is displayed. Should the
1301 expression evaluate to nil the menu item is ghosted.
1303 KEYMAP is the keymap to add to menu to. (When using XEmacs, the menu
1304 will only be visible when this menu is the global, the local, or an
1305 activate minor mode keymap.)
1307 If POPUP is non-nil, the menu is bound to the XEmacs `mode-popup-menu'
1308 variable, i.e. it will popup when pressing the right mouse button.
1310 Please see the variable `erlang-menu-base-items'."
1311 (cond (erlang-xemacs-p
1312 (let ((menu (erlang-menu-xemacs name items keymap)))
1313 ;; We add the menu to the global menubar.
1314 (funcall (symbol-function 'add-submenu) nil menu)
1315 (setcdr erlang-xemacs-popup-menu (cdr menu))
1316 (if (and popup (boundp 'mode-popup-menu))
1317 (funcall (symbol-function 'set)
1318 'mode-popup-menu erlang-xemacs-popup-menu))))
1319 ((>= erlang-emacs-major-version 19)
1320 (define-key keymap (vector 'menu-bar (intern name))
1321 (erlang-menu-make-keymap name items)))
1322 (t nil)))
1325 (defun erlang-menu-make-keymap (name items)
1326 "Build a menu for Emacs 19."
1327 (let ((menumap (funcall (symbol-function 'make-sparse-keymap)
1328 name))
1329 (count 0)
1330 id def first second third)
1331 (setq items (reverse items))
1332 (while items
1333 ;; Replace any occurrence of atoms by their value.
1334 (while (and items (atom (car items)) (not (null (car items))))
1335 (if (and (boundp (car items))
1336 (listp (symbol-value (car items))))
1337 (setq items (append (reverse (symbol-value (car items)))
1338 (cdr items)))
1339 (setq items (cdr items))))
1340 (setq first (car-safe (car items)))
1341 (setq second (car-safe (cdr-safe (car items))))
1342 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1343 (cond ((null first)
1344 (setq count (+ count 1))
1345 (setq id (intern (format "separator-%d" count)))
1346 (setq def '("--" . nil)))
1347 ((and (consp second) (eq (car second) 'lambda))
1348 (setq count (+ count 1))
1349 (setq id (intern (format "lambda-%d" count)))
1350 (setq def (cons first second)))
1351 ((symbolp second)
1352 (setq id second)
1353 (setq def (cons first second)))
1355 (setq count (+ count 1))
1356 (setq id (intern (format "submenu-%d" count)))
1357 (setq def (erlang-menu-make-keymap first second))))
1358 (define-key menumap (vector id) def)
1359 (if third
1360 (put id 'menu-enable third))
1361 (setq items (cdr items)))
1362 (cons name menumap)))
1365 (defun erlang-menu-xemacs (name items &optional keymap)
1366 "Build a menu for XEmacs."
1367 (let ((res '())
1368 first second third entry)
1369 (while items
1370 ;; Replace any occurrence of atoms by their value.
1371 (while (and items (atom (car items)) (not (null (car items))))
1372 (if (and (boundp (car items))
1373 (listp (symbol-value (car items))))
1374 (setq items (append (reverse (symbol-value (car items)))
1375 (cdr items)))
1376 (setq items (cdr items))))
1377 (setq first (car-safe (car items)))
1378 (setq second (car-safe (cdr-safe (car items))))
1379 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1380 (cond ((null first)
1381 (setq res (cons "------" res)))
1382 ((symbolp second)
1383 (setq res (cons (vector first second (or third t)) res)))
1384 ((and (consp second) (eq (car second) 'lambda))
1385 (setq res (cons (vector first (list 'call-interactively second)
1386 (or third t)) res)))
1388 (setq res (cons (cons first
1389 (cdr (erlang-menu-xemacs
1390 first second)))
1391 res))))
1392 (setq items (cdr items)))
1393 (setq res (reverse res))
1394 ;; When adding a menu to a minor-mode keymap under Emacs,
1395 ;; it disappears when the mode is disabled. The expression
1396 ;; generated below imitates this behaviour.
1397 ;; (This could be expressed much clearer using backquotes,
1398 ;; but I don't want to pull in every package.)
1399 (if keymap
1400 (let ((expr (list 'or
1401 (list 'eq keymap 'global-map)
1402 (list 'eq keymap (list 'current-local-map))
1403 (list 'symbol-value
1404 (list 'car-safe
1405 (list 'rassq
1406 keymap
1407 'minor-mode-map-alist))))))
1408 (setq res (cons ':included (cons expr res)))))
1409 (cons name res)))
1412 (defun erlang-menu-substitute (items alist)
1413 "Substitute functions in menu described by ITEMS.
1415 The menu ITEMS is updated destructively.
1417 ALIST is list of pairs where the car is the old function and cdr the new."
1418 (let (first second pair)
1419 (while items
1420 (setq first (car-safe (car items)))
1421 (setq second (car-safe (cdr-safe (car items))))
1422 (cond ((null first))
1423 ((symbolp second)
1424 (setq pair (and second (assq second alist)))
1425 (if pair
1426 (setcar (cdr (car items)) (cdr pair))))
1427 ((and (consp second) (eq (car second) 'lambda)))
1429 (erlang-menu-substitute second alist)))
1430 (setq items (cdr items)))))
1433 (defun erlang-menu-add-above (entry above items)
1434 "Add menu ENTRY above menu entry ABOVE in menu ITEMS.
1435 Do nothing if the items already should be in the menu.
1436 Should ABOVE not be in the list, the entry is added at
1437 the bottom of the menu.
1439 The new menu is returned. No guarantee is given that the original
1440 menu is left unchanged.
1442 The equality test is performed by `eq'.
1444 Example: (erlang-menu-add-above 'my-erlang-menu-items
1445 'erlang-menu-man-items)"
1446 (erlang-menu-add-below entry above items t))
1449 (defun erlang-menu-add-below (entry below items &optional above-p)
1450 "Add menu ENTRY below menu items BELOW in the Erlang menu.
1451 Do nothing if the items already should be in the menu.
1452 Should BELOW not be in the list, items is added at the bottom
1453 of the menu.
1455 The new menu is returned. No guarantee is given that the original
1456 menu is left unchanged.
1458 The equality test is performed by `eq'.
1460 Example:
1462 \(setq erlang-menu-items
1463 (erlang-menu-add-below 'my-erlang-menu-items
1464 'erlang-menu-base-items
1465 erlang-menu-items))"
1466 (if (memq entry items)
1467 items ; Return the original menu.
1468 (let ((head '())
1469 (done nil)
1470 res)
1471 (while (not done)
1472 (cond ((null items)
1473 (setq res (append head (list entry)))
1474 (setq done t))
1475 ((eq below (car items))
1476 (setq res
1477 (if above-p
1478 (append head (cons entry items))
1479 (append head (cons (car items)
1480 (cons entry (cdr items))))))
1481 (setq done t))
1483 (setq head (append head (list (car items))))
1484 (setq items (cdr items)))))
1485 res)))
1487 (defun erlang-menu-delete (entry items)
1488 "Delete ENTRY from menu ITEMS.
1490 The new menu is returned. No guarantee is given that the original
1491 menu is left unchanged."
1492 (delq entry items))
1494 ;; Man code:
1496 (defun erlang-man-init ()
1497 "Add menus containing the manual pages of the Erlang.
1499 The variable `erlang-man-dirs' contains entries describing
1500 the location of the manual pages."
1501 (interactive)
1502 (if erlang-man-inhibit
1504 (setq erlang-menu-man-items
1505 '(nil
1506 ("Man - Function" erlang-man-function)))
1507 (if erlang-man-dirs
1508 (setq erlang-menu-man-items
1509 (append erlang-menu-man-items
1510 (erlang-man-make-top-menu erlang-man-dirs))))
1511 (setq erlang-menu-items
1512 (erlang-menu-add-above 'erlang-menu-man-items
1513 'erlang-menu-version-items
1514 erlang-menu-items))
1515 (erlang-menu-init)))
1518 (defun erlang-man-uninstall ()
1519 "Remove the man pages from the Erlang mode."
1520 (interactive)
1521 (setq erlang-menu-items
1522 (erlang-menu-delete 'erlang-menu-man-items erlang-menu-items))
1523 (erlang-menu-init))
1526 ;; The man menu is a hierarchal structure, with the manual sections
1527 ;; at the top, described by `erlang-man-dirs'. The next level could
1528 ;; either be the manual pages if not to many, otherwise it is an index
1529 ;; menu whose submenus will contain up to `erlang-man-max-menu-size'
1530 ;; manual pages.
1532 (defun erlang-man-make-top-menu (dir-list)
1533 "Create one menu entry per element of DIR-LIST.
1534 The format is described in the documentation of `erlang-man-dirs'."
1535 (let ((menu '())
1536 dir)
1537 (while dir-list
1538 (setq dir (cond ((nth 2 (car dir-list))
1539 ;; Relative to `erlang-man-root-dir'.
1540 (and (stringp erlang-man-root-dir)
1541 (concat erlang-man-root-dir (nth 1 (car dir-list)))))
1543 ;; Absolute
1544 (nth 1 (car dir-list)))))
1545 (if (and dir
1546 (file-readable-p dir))
1547 (setq menu (cons (list (car (car dir-list))
1548 (erlang-man-make-middle-menu
1549 (erlang-man-get-files dir)))
1550 menu)))
1551 (setq dir-list (cdr dir-list)))
1552 ;; Should no menus be found, generate a menu item which
1553 ;; will display a help text, when selected.
1554 (if menu
1555 (nreverse menu)
1556 '(("Man Pages"
1557 (("Error! Why?" erlang-man-describe-error)))))))
1560 ;; Should the menu be to long, let's split it into a number of
1561 ;; smaller menus. Warning, this code contains beautiful
1562 ;; destructive operations!
1563 (defun erlang-man-make-middle-menu (filelist)
1564 "Create the second level menu from FILELIST.
1566 Should the list be longer than `erlang-man-max-menu-size', a tree of
1567 menus is created."
1568 (if (<= (length filelist) erlang-man-max-menu-size)
1569 (erlang-man-make-menu filelist)
1570 (let ((menu '())
1571 (filelist (copy-sequence filelist))
1572 segment submenu pair)
1573 (while filelist
1574 (setq pair (nthcdr (- erlang-man-max-menu-size 1) filelist))
1575 (setq segment filelist)
1576 (if (null pair)
1577 (setq filelist nil)
1578 (setq filelist (cdr pair))
1579 (setcdr pair nil))
1580 (setq submenu (erlang-man-make-menu segment))
1581 (setq menu (cons (list (concat (car (car submenu))
1582 " -- "
1583 (car (car (reverse submenu))))
1584 submenu)
1585 menu)))
1586 (nreverse menu))))
1589 (defun erlang-man-make-menu (filelist)
1590 "Make a leaf menu based on FILELIST."
1591 (let ((menu '())
1592 item)
1593 (while filelist
1594 (setq item (erlang-man-make-menu-item (car filelist)))
1595 (if item
1596 (setq menu (cons item menu)))
1597 (setq filelist (cdr filelist)))
1598 (nreverse menu)))
1601 (defun erlang-man-make-menu-item (file)
1602 "Create a menu item containing the name of the man page."
1603 (and (string-match ".*/\\([^/]+\\)\\.[^.]$" file)
1604 (let ((page (substring file (match-beginning 1) (match-end 1))))
1605 (list (capitalize page)
1606 (list 'lambda '()
1607 '(interactive)
1608 (list 'funcall 'erlang-man-display-function
1609 file))))))
1612 (defun erlang-man-get-files (dir)
1613 "Return files in directory DIR."
1614 (directory-files dir t ".*\\.[0-9]\\'"))
1617 (defun erlang-man-module (&optional module)
1618 "Find manual page for MODULE, defaults to module of function under point.
1619 This function is aware of imported functions."
1620 (interactive
1621 (list (let* ((mod (car-safe (erlang-get-function-under-point)))
1622 (input (read-string
1623 (format "Manual entry for module%s: "
1624 (if (or (null mod) (string= mod ""))
1626 (format " (default %s)" mod))))))
1627 (if (string= input "")
1629 input))))
1630 (or module (setq module (car (erlang-get-function-under-point))))
1631 (if (or (null module) (string= module ""))
1632 (error "No Erlang module name given"))
1633 (let ((dir-list erlang-man-dirs)
1634 (pat (concat "/" (regexp-quote module) "\\.[^.]$"))
1635 (file nil)
1636 file-list)
1637 (while (and dir-list (null file))
1638 (setq file-list (erlang-man-get-files
1639 (if (nth 2 (car dir-list))
1640 (concat erlang-man-root-dir (nth 1 (car dir-list)))
1641 (nth 1 (car dir-list)))))
1642 (while (and file-list (null file))
1643 (if (string-match pat (car file-list))
1644 (setq file (car file-list)))
1645 (setq file-list (cdr file-list)))
1646 (setq dir-list (cdr dir-list)))
1647 (if file
1648 (funcall erlang-man-display-function file)
1649 (error "No manual page for module %s found" module))))
1652 ;; Warning, the function `erlang-man-function' is a hack!
1653 ;; It links itself into the man code in a non-clean way. I have
1654 ;; chosen to keep it since it provides a very useful functionality
1655 ;; which is not possible to achieve using a clean approach.
1656 ;; / AndersL
1658 (defvar erlang-man-function-name nil
1659 "Name of function for last `erlang-man-function' call.
1660 Used for communication between `erlang-man-function' and the
1661 patch to `Man-notify-when-ready'.")
1663 (defun erlang-man-function (&optional name)
1664 "Find manual page for NAME, where NAME is module:function.
1665 The entry for `function' is displayed.
1667 This function is aware of imported functions."
1668 (interactive
1669 (list (let* ((mod-func (erlang-get-function-under-point))
1670 (mod (car-safe mod-func))
1671 (func (nth 1 mod-func))
1672 (input (read-string
1673 (format
1674 "Manual entry for `module:func' or `module'%s: "
1675 (if (or (null mod) (string= mod ""))
1677 (format " (default %s:%s)" mod func))))))
1678 (if (string= input "")
1679 (if (and mod func)
1680 (concat mod ":" func)
1681 mod)
1682 input))))
1683 ;; Emacs 18 doesn't provide `man'...
1684 (condition-case nil
1685 (require 'man)
1686 (error nil))
1687 (let ((modname nil)
1688 (funcname nil))
1689 (cond ((null name)
1690 (let ((mod-func (erlang-get-function-under-point)))
1691 (setq modname (car-safe mod-func))
1692 (setq funcname (nth 1 mod-func))))
1693 ((string-match ":" name)
1694 (setq modname (substring name 0 (match-beginning 0)))
1695 (setq funcname (substring name (match-end 0) nil)))
1696 ((stringp name)
1697 (setq modname name)))
1698 (if (or (null modname) (string= modname ""))
1699 (error "No Erlang module name given"))
1700 (cond ((fboundp 'Man-notify-when-ready)
1701 ;; Emacs 19: The man command could possibly start an
1702 ;; asynchronous process, i.e. we must hook ourselves into
1703 ;; the system to be activated when the man-process
1704 ;; terminates.
1705 (if (null funcname)
1707 (erlang-man-patch-notify)
1708 (setq erlang-man-function-name funcname))
1709 (condition-case nil
1710 (erlang-man-module modname)
1711 (error (setq erlang-man-function-name nil))))
1713 (erlang-man-module modname)
1714 (if funcname
1715 (erlang-man-find-function
1716 (or (get-buffer "*Manual Entry*") ; Emacs 18
1717 (current-buffer)) ; XEmacs
1718 funcname))))))
1721 ;; Should the defadvice be at the top level, the package `advice' would
1722 ;; be required. Now it is only required when this functionality
1723 ;; is used. (Emacs 19 specific.)
1724 (defun erlang-man-patch-notify ()
1725 "Patch the function `Man-notify-when-ready' to search for function.
1726 The variable `erlang-man-function-name' is assumed to be bound to
1727 the function name, or to nil.
1729 The reason for patching a function is that under Emacs 19, the man
1730 command is executed asynchronously."
1731 (condition-case nil
1732 (require 'advice)
1733 ;; This should never happened since this is only called when
1734 ;; running under Emacs 19.
1735 (error (error (concat "This command needs the package `advice', "
1736 "please upgrade your Emacs."))))
1737 (require 'man)
1738 (defadvice Man-notify-when-ready
1739 (after erlang-Man-notify-when-ready activate)
1740 "Set point at the documentation of the function name in
1741 `erlang-man-function-name' when the man page is displayed."
1742 (if erlang-man-function-name
1743 (erlang-man-find-function (ad-get-arg 0) erlang-man-function-name))
1744 (setq erlang-man-function-name nil)))
1747 (defun erlang-man-find-function (buf func)
1748 "Find manual page for function in `erlang-man-function-name' in buffer BUF."
1749 (if func
1750 (let ((win (get-buffer-window buf)))
1751 (if win
1752 (progn
1753 (set-buffer buf)
1754 (goto-char (point-min))
1755 (if (re-search-forward
1756 (concat "^[ \t]+" func " ?(")
1757 (point-max) t)
1758 (progn
1759 (forward-word -1)
1760 (set-window-point win (point)))
1761 (message "Could not find function `%s'" func)))))))
1764 (defun erlang-man-display (file)
1765 "Display FILE as a `man' file.
1766 This is the default manual page display function.
1767 The variables `erlang-man-display-function' contains the function
1768 to be used."
1769 ;; Emacs 18 doesn't `provide' man.
1770 (condition-case nil
1771 (require 'man)
1772 (error nil))
1773 (if file
1774 (let ((process-environment (copy-sequence process-environment)))
1775 (if (string-match "\\(.*\\)/man[^/]*/\\([^/]+\\)\\.[^.]$" file)
1776 (let ((dir (substring file (match-beginning 1) (match-end 1)))
1777 (page (substring file (match-beginning 2) (match-end 2))))
1778 (if (fboundp 'setenv)
1779 (setenv "MANPATH" dir)
1780 ;; Emacs 18
1781 (setq process-environment (cons (concat "MANPATH=" dir)
1782 process-environment)))
1783 (cond ((not (and (not erlang-xemacs-p)
1784 (= erlang-emacs-major-version 19)
1785 (< erlang-emacs-minor-version 29)))
1786 (manual-entry page))
1788 ;; Emacs 19.28 and earlier versions of 19:
1789 ;; The manual-entry command unconditionally prompts
1790 ;; the user :-(
1791 (funcall (symbol-function 'Man-getpage-in-background)
1792 page))))
1793 (error "Can't find man page for %s\n" file)))))
1796 (defun erlang-man-describe-error ()
1797 "Describe why the manual pages weren't found."
1798 (interactive)
1799 (with-output-to-temp-buffer "*Erlang Man Error*"
1800 (princ "Normally, this menu should contain Erlang manual pages.
1802 In order to find the manual pages, the variable `erlang-man-root-dir'
1803 should be bound to the name of the directory containing the Erlang
1804 man pages. The name should not include the final slash.
1806 Practically, you should add a line on the following form to
1807 your ~/.emacs, or ask your system administrator to add it to
1808 the site init file:
1810 (setq erlang-man-root-dir \"/usr/local/erlang\")
1812 After installing the line, kill and restart Emacs, or restart Erlang
1813 mode with the command `M-x erlang-mode RET'.")))
1815 ;; Indentation code:
1817 (defun erlang-indent-command (&optional whole-exp)
1818 "Indent current line as Erlang code.
1819 With argument, indent any additional lines of the same clause
1820 rigidly along with this one."
1821 (interactive "P")
1822 (if whole-exp
1823 ;; If arg, always indent this line as Erlang
1824 ;; and shift remaining lines of clause the same amount.
1825 (let ((shift-amt (erlang-indent-line))
1826 beg end)
1827 (save-excursion
1828 (if erlang-tab-always-indent
1829 (beginning-of-line))
1830 (setq beg (point))
1831 (erlang-end-of-clause 1)
1832 (setq end (point))
1833 (goto-char beg)
1834 (forward-line 1)
1835 (setq beg (point)))
1836 (if (> end beg)
1837 (indent-code-rigidly beg end shift-amt "\n")))
1838 (if (and (not erlang-tab-always-indent)
1839 (save-excursion
1840 (skip-chars-backward " \t")
1841 (not (bolp))))
1842 (insert-tab)
1843 (erlang-indent-line))))
1846 (defun erlang-indent-line ()
1847 "Indent current line as Erlang code.
1848 Return the amount the indentation changed by."
1849 (let ((pos (- (point-max) (point)))
1850 indent beg
1851 shift-amt)
1852 (beginning-of-line 1)
1853 (setq beg (point))
1854 (skip-chars-forward " \t")
1855 (cond ((looking-at "%")
1856 (setq indent (funcall comment-indent-function))
1857 (setq shift-amt (- indent (current-column))))
1859 (setq indent (erlang-calculate-indent))
1860 (cond ((null indent)
1861 (setq indent (current-indentation)))
1862 ((eq indent t)
1863 ;; This should never occur here.
1864 (error "Erlang mode error"))
1865 ((= (char-syntax (following-char)) ?\))
1866 (setq indent (1- indent))))
1867 (setq shift-amt (- indent (current-column)))))
1868 (if (zerop shift-amt)
1870 (delete-region beg (point))
1871 (indent-to indent))
1872 ;; If initial point was within line's indentation, position
1873 ;; after the indentation. Else stay at same point in text.
1874 (if (> (- (point-max) pos) (point))
1875 (goto-char (- (point-max) pos)))
1876 shift-amt))
1879 (defun erlang-indent-region (beg end)
1880 "Indent region of Erlang code.
1882 This is automagically called by the user level function `indent-region'."
1883 (interactive "r")
1884 (save-excursion
1885 (let ((case-fold-search nil)
1886 (continue t)
1887 (from-end (- (point-max) end))
1888 indent-point;; The beginning of the current line
1889 indent;; The indent amount
1890 state)
1891 (goto-char beg)
1892 (beginning-of-line)
1893 (setq indent-point (point))
1894 (erlang-beginning-of-clause)
1895 ;; Parse the Erlang code from the beginning of the clause to
1896 ;; the beginning of the region.
1897 (while (< (point) indent-point)
1898 (setq state (erlang-partial-parse (point) indent-point state)))
1899 ;; Indent every line in the region
1900 (while continue
1901 (goto-char indent-point)
1902 (skip-chars-forward " \t")
1903 (cond ((looking-at "%")
1904 ;; Do not use our stack to help the user to customize
1905 ;; comment indentation.
1906 (setq indent (funcall comment-indent-function)))
1907 ((looking-at "$")
1908 ;; Don't indent empty lines.
1909 (setq indent 0))
1911 (setq indent
1912 (save-excursion
1913 (erlang-calculate-stack-indent (point) state)))
1914 (cond ((null indent)
1915 (setq indent (current-indentation)))
1916 ((eq indent t)
1917 ;; This should never occur here.
1918 (error "Erlang mode error"))
1919 ((= (char-syntax (following-char)) ?\))
1920 (setq indent (1- indent))))))
1921 (if (zerop (- indent (current-column)))
1923 (delete-region indent-point (point))
1924 (indent-to indent))
1925 ;; Find the next line in the region
1926 (goto-char indent-point)
1927 (save-excursion
1928 (forward-line 1)
1929 (setq indent-point (point)))
1930 (if (>= from-end (- (point-max) indent-point))
1931 (setq continue nil)
1932 (while (< (point) indent-point)
1933 (setq state (erlang-partial-parse
1934 (point) indent-point state))))))))
1937 (defun erlang-indent-current-buffer ()
1938 "Indent current buffer as Erlang code."
1939 (interactive)
1940 (save-excursion
1941 (save-restriction
1942 (widen)
1943 (erlang-indent-region (point-min) (point-max)))))
1946 (defun erlang-indent-function ()
1947 "Indent current Erlang function."
1948 (interactive)
1949 (save-excursion
1950 (let ((end (progn (erlang-end-of-function 1) (point)))
1951 (beg (progn (erlang-beginning-of-function 1) (point))))
1952 (erlang-indent-region beg end))))
1955 (defun erlang-indent-clause ()
1956 "Indent current Erlang clause."
1957 (interactive)
1958 (save-excursion
1959 (let ((end (progn (erlang-end-of-clause 1) (point)))
1960 (beg (progn (erlang-beginning-of-clause 1) (point))))
1961 (erlang-indent-region beg end))))
1964 (defmacro erlang-push (x stack) (list 'setq stack (list 'cons x stack)))
1965 (defmacro erlang-pop (stack) (list 'setq stack (list 'cdr stack)))
1966 ;; Would much prefer to make caddr a macro but this clashes.
1967 (defun erlang-caddr (x) (car (cdr (cdr x))))
1970 (defun erlang-calculate-indent (&optional parse-start)
1971 "Compute appropriate indentation for current line as Erlang code.
1972 Return nil if line starts inside string, t if in a comment."
1973 (save-excursion
1974 (let ((indent-point (point))
1975 (case-fold-search nil)
1976 (state nil))
1977 (if parse-start
1978 (goto-char parse-start)
1979 (erlang-beginning-of-clause))
1980 (while (< (point) indent-point)
1981 (setq state (erlang-partial-parse (point) indent-point state)))
1982 (erlang-calculate-stack-indent indent-point state))))
1984 (defun erlang-show-syntactic-information ()
1985 "Show syntactic information for current line."
1987 (interactive)
1989 (save-excursion
1990 (let ((starting-point (point))
1991 (case-fold-search nil)
1992 (state nil))
1993 (erlang-beginning-of-clause)
1994 (while (< (point) starting-point)
1995 (setq state (erlang-partial-parse (point) starting-point state)))
1996 (message "%S" state))))
1999 (defun erlang-partial-parse (from to &optional state)
2000 "Parse Erlang syntax starting at FROM until TO, with an optional STATE.
2001 Value is list (stack token-start token-type in-what)."
2002 (goto-char from) ; Start at the beginning
2003 (erlang-skip-blank to)
2004 (let ((cs (char-syntax (following-char)))
2005 (stack (car state))
2006 (token (point))
2007 in-what)
2008 (cond
2010 ;; Done: Return previous state.
2011 ((>= token to)
2012 (setq token (nth 1 state))
2013 (setq cs (nth 2 state))
2014 (setq in-what (nth 3 state)))
2016 ;; Word constituent: check and handle keywords.
2017 ((= cs ?w)
2018 (cond ((looking-at "\\(end\\|after\\)[^_a-zA-Z0-9]")
2019 ;; Must pop top icr layer, `after' will push a new
2020 ;; layer next.
2021 (progn
2022 (while (and stack (eq (car (car stack)) '->))
2023 (erlang-pop stack))
2024 (if (and stack (memq (car (car stack)) '(icr begin)))
2025 (erlang-pop stack))))
2026 ((looking-at "catch[^,\n\\of]*\n")
2027 ;; Must pop top icr layer, `catch' in try/catch
2028 ;;will push a new layer next.
2029 (progn
2030 (while (and stack (eq (car (car stack)) '->))
2031 (erlang-pop stack))
2032 (if (and stack (memq (car (car stack)) '(icr begin)))
2033 (erlang-pop stack))))
2035 (cond ((looking-at "\\(if\\|case\\|receive\\|try\\)[^_a-zA-Z0-9]")
2036 ;; Must push a new icr (if/case/receive) layer.
2037 (erlang-push (list 'icr token (current-column)) stack))
2038 ((looking-at "\\(fun\\)[^_a-zA-Z0-9]")
2039 ;; Push a new icr layer if we are defining a `fun'
2040 ;; expression, not when we are refering an existing
2041 ;; function.
2042 (if (save-excursion
2043 (goto-char (match-end 1))
2044 (erlang-skip-blank to)
2045 (eq (following-char) ?\())
2046 (erlang-push (list 'icr token (current-column)) stack)))
2047 ((looking-at "\\(begin\\|query\\)[^_a-zA-Z0-9]")
2048 (erlang-push (list 'begin token (current-column)) stack))
2049 ;; In test suites you may want to do something like
2050 ;; ?match(Mem when integer(Mem), mnesia:table_info(Tab,
2051 ;; memory)), and then the following if/case/receive
2052 ;; statement will mess up the indentation by fooling the
2053 ;; erlang mode to think the 'when' in the argument is a
2054 ;; "real" when. The following three clauses will avoid
2055 ;; this problem.
2056 ((looking-at "when[^->\.]*if[^->\.]*->"))
2057 ((looking-at "when[^->\.]*case[^->\.]*->"))
2058 ((looking-at "when[^->\.]*receive[^->\.]*->"))
2059 ;; Normal when case
2060 ((looking-at "when [^->\.]*->")
2061 (erlang-push (list 'when token (current-column)) stack))
2062 ((looking-at "after[.]+->")
2063 (erlang-push (list 'icr token (current-column)) stack))
2064 ((looking-at "after[^_a-zA-Z0-9->]")
2065 (erlang-push (list 'icr token (current-column)) stack)
2066 (erlang-push (list '-> token (current-column)) stack))
2067 ((looking-at "catch[^,\n\\of]*\n")
2068 (erlang-push (list 'icr token (current-column)) stack))
2070 (forward-sexp 1))
2072 ;; String: Try to skip over it. (Catch error if not complete.)
2073 ((= cs ?\")
2074 (condition-case nil
2075 (progn
2076 (forward-sexp 1)
2077 (if (> (point) to)
2078 (progn
2079 (setq in-what 'string)
2080 (goto-char to))))
2081 (error
2082 (setq in-what 'string)
2083 (goto-char to))))
2085 ;; Symbol constituent or punctuation
2087 ((memq cs '(?. ?_))
2088 (cond
2090 ;; Clause end
2091 ((= (following-char) ?\;)
2092 (if (and stack (eq (car (car stack)) '->))
2093 (erlang-pop stack))
2094 (forward-char 1))
2096 ;; Function end
2097 ((looking-at "\\.\\(\\s \\|\n\\|\\s<\\)")
2098 (setq stack nil)
2099 (forward-char 1))
2101 ;; Function head
2102 ((looking-at "->\\|:-")
2103 (save-excursion
2104 (back-to-indentation)
2105 (cond ((looking-at "after[^_a-zA-Z0-9]")
2106 (erlang-pop stack))))
2107 (if (and stack (eq (car (car stack)) 'when))
2108 (erlang-pop stack))
2109 (erlang-push (list '-> token (current-column)) stack)
2110 (forward-char 2))
2112 ;; List-comprehension divider
2113 ((looking-at "||")
2114 (erlang-push (list '|| token (current-column)) stack)
2115 (forward-char 2))
2117 ;; Parameter separator
2118 ((looking-at ",")
2119 (forward-char 1))
2121 ;; Bit-syntax open paren
2122 ((looking-at "<<")
2123 (erlang-push (list '\( token (current-column)) stack)
2124 (forward-char 2))
2126 ;; Bbit-syntax close paren
2127 ((looking-at ">>")
2128 (while (memq (car (car stack)) '(|| ->))
2129 (erlang-pop stack))
2130 (cond ((eq (car (car stack)) '\()
2131 (erlang-pop stack))
2132 ((memq (car (car stack)) '(icr begin))
2133 (error "Missing `end'"))
2135 (error "Unbalanced parentheses")))
2136 (forward-char 2))
2138 ;; Macro
2139 ((= (following-char) ??)
2140 ;; Skip over the ?
2141 (forward-char 1)
2144 ;; Other punctuation: Skip over it and any following punctuation
2145 ((= cs ?.)
2146 ;; Skip over all characters in the operand.
2147 (skip-syntax-forward "."))
2149 ;; Other char: Skip over it.
2151 (forward-char 1))))
2153 ;; Open parenthesis
2154 ((= cs ?\()
2155 (erlang-push (list '\( token (current-column)) stack)
2156 (forward-char 1))
2158 ;; Close parenthesis
2159 ((= cs ?\))
2160 (while (memq (car (car stack)) '(|| ->))
2161 (erlang-pop stack))
2162 (cond ((eq (car (car stack)) '\()
2163 (erlang-pop stack))
2164 ((eq (car (car stack)) 'icr)
2165 (erlang-pop stack)
2166 ;; Normal catch not try-catch might have caused icr
2167 ;; and then incr should be removed and is not an error.
2168 (if (eq (car (car stack)) '\()
2169 (erlang-pop stack)
2170 (else
2171 (error "Missing `end'"))
2173 ((eq (car (car stack)) 'begin)
2174 (error "Missing `end'")
2176 (error "Unbalanced parenthesis"))
2178 (forward-char 1))
2180 ;; Character quote: Skip it and the quoted char.
2181 ((= cs ?/)
2182 (forward-char 2))
2184 ;; Character escape: Skip it and the escape sequence.
2185 ((= cs ?\\)
2186 (forward-char 1)
2187 (skip-syntax-forward "w"))
2189 ;; Everything else
2191 (forward-char 1)))
2192 (list stack token cs in-what)))
2194 (defun erlang-calculate-stack-indent (indent-point state)
2195 "From the given last position and state (stack) calculate indentation.
2196 Return nil if inside string, t if in a comment."
2197 (let* ((stack (and state (car state)))
2198 (token (nth 1 state))
2199 (stack-top (and stack (car stack))))
2200 (cond ((null state) ;No state
2202 ((nth 3 state)
2203 ;; Return nil or t.
2204 (eq (nth 3 state) 'comment))
2205 ((null stack)
2206 (if (looking-at "when[^_a-zA-Z0-9]")
2207 erlang-indent-guard
2209 ((eq (car stack-top) '\()
2210 ;; Element of list, tuple or part of an expression,
2211 (if (null erlang-argument-indent)
2212 ;; indent to next column.
2213 (1+ (nth 2 stack-top))
2214 (goto-char (nth 1 stack-top))
2215 (cond ((looking-at "[({]\\s *\\($\\|%\\)")
2216 ;; Line ends with parenthesis.
2217 (+ (erlang-indent-find-preceding-expr)
2218 erlang-argument-indent))
2220 ;; Indent to the same column as the first
2221 ;; argument.
2222 (goto-char (1+ (nth 1 stack-top)))
2223 (skip-chars-forward " \t")
2224 (current-column)))))
2225 ((eq (car stack-top) 'icr)
2226 ;; The default indentation is the column of the option
2227 ;; directly following the keyword. (This does not apply to
2228 ;; `case'.) Should no option be on the same line, the
2229 ;; indentation is the indentation of the keyword +
2230 ;; `erlang-indent-level'.
2232 ;; `after' should be indented to the save level as the
2233 ;; corresponding receive.
2234 (if (looking-at "after[^_a-zA-Z0-9]")
2235 (nth 2 stack-top)
2236 (save-excursion
2237 (goto-char (nth 1 stack-top))
2238 (if (looking-at "case[^_a-zA-Z0-9]")
2239 (+ (nth 2 stack-top) erlang-indent-level)
2240 (skip-chars-forward "a-z")
2241 (skip-chars-forward " \t")
2242 (if (memq (following-char) '(?% ?\n))
2243 (+ (nth 2 stack-top) erlang-indent-level)
2244 (current-column)))))
2245 (if (looking-at "catch[^_a-zA-Z0-9]")
2246 (nth 2 stack-top)
2247 (save-excursion
2248 (goto-char (nth 1 stack-top))
2249 (if (looking-at "case[^_a-zA-Z0-9]")
2250 (+ (nth 2 stack-top) erlang-indent-level)
2251 (skip-chars-forward "a-z")
2252 (skip-chars-forward " \t")
2253 (if (memq (following-char) '(?% ?\n))
2254 (+ (nth 2 stack-top) erlang-indent-level)
2255 (current-column)))))
2257 ;; Real indentation, where operators create extra indentation etc.
2258 ((memq (car stack-top) '(-> || begin))
2259 (goto-char (nth 1 stack-top))
2260 ;; Check if there is more code after the '->' on the
2261 ;; same line. If so use this indentation as base, else
2262 ;; use parent indentation + 2 * level as base.
2263 (let ((off erlang-indent-level)
2264 (skip 2))
2265 (cond ((null (cdr stack))) ; Top level in function.
2266 ((eq (car stack-top) 'begin)
2267 (setq skip 5))
2268 ((eq (car stack-top) '->)
2269 (setq off (* 2 erlang-indent-level))))
2270 (let ((base (erlang-indent-find-base stack indent-point off skip)))
2271 ;; Look at last thing to see how we are to move relative
2272 ;; to the base.
2273 (goto-char token)
2274 (cond ((looking-at "||\\|,\\|->\\|:-")
2275 base)
2276 ((erlang-at-keyword)
2277 (+ (current-column) erlang-indent-level))
2278 ((or (= (char-syntax (following-char)) ?.)
2279 (erlang-at-operator))
2280 (+ base erlang-indent-level))
2282 (goto-char indent-point)
2283 (cond ((memq (following-char) '(?\( ?{))
2284 ;; Function application or record.
2285 (+ (erlang-indent-find-preceding-expr)
2286 erlang-argument-indent))
2287 ;; Empty line, or end; treat it as the end of
2288 ;; the block. (Here we have a choice: should
2289 ;; the user be forced to reindent continued
2290 ;; lines, or should the "end" be reindented?)
2291 ((looking-at "\\(end\\|after\\|catch\\)[^_a-zA-Z0-9]\\|$")
2292 (if (eq (car (car stack)) '->)
2293 (erlang-pop stack))
2294 (if stack
2295 (erlang-caddr (car stack))
2297 ;; Avoid treating comments a continued line.
2298 ((= (following-char) ?%)
2299 base)
2300 ;; Continued line (e.g. line beginning
2301 ;; with an operator.)
2302 (t (+ base erlang-indent-level)))))))
2304 ((eq (car stack-top) 'when)
2305 (goto-char (nth 1 stack-top))
2306 (if (looking-at "when\\s *\\($\\|%\\)")
2307 (progn
2308 (erlang-pop stack)
2309 (if (and stack (eq (nth 0 (car stack)) 'icr))
2310 (progn
2311 (goto-char (nth 1 (car stack)))
2312 (+ (nth 2 (car stack)) erlang-indent-guard
2313 ;; receive XYZ or receive
2314 ;; XYZ
2315 (if (looking-at "[a-z]+\\s *\\($\\|%\\)")
2316 erlang-indent-level
2317 (* 2 erlang-indent-level))))
2318 erlang-indent-guard))
2319 ;; "when" is followed by code, let's indent to the same
2320 ;; column.
2321 (forward-char 4) ; Skip "when"
2322 (skip-chars-forward " \t")
2323 (current-column))))))
2326 (defun erlang-indent-find-base (stack indent-point &optional offset skip)
2327 "Find the base column for current stack."
2328 (or skip (setq skip 2))
2329 (or offset (setq offset erlang-indent-level))
2330 (save-excursion
2331 (let* ((stack-top (car stack)))
2332 (goto-char (nth 1 stack-top))
2333 (forward-char skip)
2334 (if (looking-at "\\s *\\($\\|%\\)")
2335 (progn
2336 (if (memq (car stack-top) '(-> ||))
2337 (erlang-pop stack))
2338 ;; Take parent identation + offset,
2339 ;; else just erlang-indent-level if no parent
2340 (if stack
2341 (+ (erlang-caddr (car stack))
2342 offset)
2343 erlang-indent-level))
2344 (erlang-skip-blank indent-point)
2345 (current-column)))))
2348 ;; Does not handle `begin' .. `end'.
2349 (defun erlang-indent-find-preceding-expr ()
2350 "Return the first column of the preceding expression.
2351 This assumes that the preceding expression is either simple
2352 \(i.e. an atom) or parenthesized."
2353 (save-excursion
2354 (forward-sexp -1)
2355 (let ((col (current-column)))
2356 (skip-chars-backward " \t")
2357 ;; Needed to match the colon in "'foo':'bar'".
2358 (if (not (memq (preceding-char) '(?# ?:)))
2360 (backward-char 1)
2361 (forward-sexp -1)
2362 (current-column)))))
2365 (defun erlang-skip-blank (&optional lim)
2366 "Skip over whitespace and comments until limit reached."
2367 (or lim (setq lim (point-max)))
2368 (let (stop)
2369 (while (and (not stop) (< (point) lim))
2370 (cond ((= (following-char) ?%)
2371 (skip-chars-forward "^\n" lim))
2372 ((= (following-char) ?\n)
2373 (skip-chars-forward "\n" lim))
2374 ((looking-at "\\s ")
2375 (if (re-search-forward "\\S " lim 'move)
2376 (forward-char -1)))
2378 (setq stop t))))
2379 stop))
2381 (defun erlang-at-keyword ()
2382 "Are we looking at an Erlang keyword which will increase indentation?"
2383 (looking-at (concat "\\(when\\|if\\|fun\\|case\\|begin\\|query\\|"
2384 "of\\|receive\\|after\\|catch\\)[^_a-zA-Z0-9]")))
2386 (defun erlang-at-operator ()
2387 "Are we looking at an Erlang operator?"
2388 (looking-at
2389 "\\(bnot\\|div\\|mod\\|band\\|bor\\|bxor\\|bsl\\|bsr\\)[^_a-zA-Z0-9]"))
2391 (defun erlang-comment-indent ()
2392 "Compute Erlang comment indentation.
2394 Used both by `indent-for-comment' and the Erlang specific indentation
2395 commands."
2396 (cond ((looking-at "%%") 0)
2397 ((looking-at "%")
2398 (or (erlang-calculate-indent)
2399 (current-indentation)))))
2402 ;;; Erlang movement commands
2404 ;; All commands below work as movement commands. I.e. if the point is
2405 ;; at the end of the clause, and the command `erlang-end-of-clause' is
2406 ;; executed, the point is moved to the end of the NEXT clause. (This
2407 ;; mimics the behaviour of `end-of-defun'.)
2409 ;; Personally I would like to rewrite them to be "pure", and add a set
2410 ;; of movement functions, like `erlang-next-clause',
2411 ;; `erlang-previous-clause', and the same for functions.
2413 ;; The current implementation makes it hopeless to use the functions as
2414 ;; subroutines in more complex commands. /andersl
2416 (defun erlang-beginning-of-clause (&optional arg)
2417 "Move backward to previous start of clause.
2418 With argument, do this that many times.
2419 Return t unless search stops due to end of buffer."
2420 (interactive "p")
2421 (or arg (setq arg 1))
2422 (if (< arg 0)
2423 ;; Step back to the end of the previous line, unless we are at
2424 ;; the beginning of the buffer. The reason for this move is
2425 ;; that the regexp below includes the last character of the
2426 ;; previous line.
2427 (if (bobp)
2428 (or (looking-at "\n")
2429 (forward-char 1))
2430 (forward-char -1)
2431 (if (looking-at "\\`\n")
2432 (forward-char 1))))
2433 ;; The regexp matches a function header that isn't
2434 ;; included in a string.
2435 (and (re-search-forward "\\(\\`\\|\\`\n\\|[^\\]\n\\)\\([a-z]\\|'\\|-\\)"
2436 nil 'move (- arg))
2437 (let ((beg (match-beginning 2)))
2438 (and beg (goto-char beg))
2439 t)))
2441 (defun erlang-end-of-clause (&optional arg)
2442 "Move to the end of the current clause.
2443 With argument, do this that many times."
2444 (interactive "p")
2445 (or arg (setq arg 1))
2446 (while (and (looking-at "[ \t]*[%\n]")
2447 (zerop (forward-line 1))))
2448 ;; Move to the next clause.
2449 (erlang-beginning-of-clause (- arg))
2450 (beginning-of-line);; Just to be sure...
2451 (let ((continue t))
2452 (while (and (not (bobp)) continue)
2453 (forward-line -1)
2454 (skip-chars-forward " \t")
2455 (if (looking-at "[%\n]")
2457 (end-of-line)
2458 (setq continue nil)))))
2460 (defun erlang-mark-clause ()
2461 "Put mark at end of clause, point at beginning."
2462 (interactive)
2463 (push-mark (point))
2464 (erlang-end-of-clause 1)
2465 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2466 ;; the region.
2467 (condition-case nil
2468 (push-mark (point) nil t)
2469 (error (push-mark (point))))
2470 (erlang-beginning-of-clause 1)
2471 ;; The above function deactivates the mark.
2472 (if (boundp 'deactivate-mark)
2473 (funcall (symbol-function 'set) 'deactivate-mark nil)))
2475 (defun erlang-beginning-of-function (&optional arg)
2476 "Move backward to previous start of function.
2477 With positive argument, do this that many times.
2478 With negative argument, search forward.
2480 Return t unless search stops due to end of buffer."
2481 (interactive "p")
2482 (or arg (setq arg 1))
2483 (cond
2484 ;; Search backward
2485 ((> arg 0)
2486 (while (and (> arg 0)
2487 (and (erlang-beginning-of-clause 1)
2488 (let ((start (point))
2489 (name (erlang-name-of-function))
2490 (arity (erlang-get-function-arity)))
2491 ;; Note: "arity" is nil for e.g. "-import", hence
2492 ;; two "-import" clauses are not considered to
2493 ;; be part of the same function.
2494 (while (and (erlang-beginning-of-clause 1)
2495 (string-equal name
2496 (erlang-name-of-function))
2497 arity
2498 (equal arity
2499 (erlang-get-function-arity)))
2500 (setq start (point)))
2501 (goto-char start)
2502 t)))
2503 (setq arg (1- arg))))
2504 ;; Search forward
2505 ((< arg 0)
2506 (end-of-line)
2507 (erlang-beginning-of-clause 1)
2508 ;; Step -arg functions forward.
2509 (while (and (< arg 0)
2510 ;; Step one function forward, or stop if the end of
2511 ;; the buffer was reached. Return t if we found the
2512 ;; function.
2513 (let ((name (erlang-name-of-function))
2514 (arity (erlang-get-function-arity))
2515 (found (erlang-beginning-of-clause -1)))
2516 (while (and found
2517 (string-equal name (erlang-name-of-function))
2518 arity
2519 (equal arity
2520 (erlang-get-function-arity)))
2521 (setq found (erlang-beginning-of-clause -1)))
2522 found))
2523 (setq arg (1+ arg)))))
2524 (zerop arg))
2527 (defun erlang-end-of-function (&optional arg)
2528 "Move forward to next end of function.
2530 With argument, do this that many times.
2531 With negative argument go towards the beginning of the buffer."
2532 (interactive "p")
2533 (or arg (setq arg 1))
2534 (let ((first t))
2535 ;; Forward
2536 (while (and (> arg 0) (< (point) (point-max)))
2537 (let ((pos (point)))
2538 (while (progn
2539 (if (and first
2540 (progn
2541 (forward-char 1)
2542 (erlang-beginning-of-clause 1)))
2544 (or (bobp) (forward-char -1))
2545 (erlang-beginning-of-clause -1))
2546 (setq first nil)
2547 (erlang-pass-over-function)
2548 (skip-chars-forward " \t")
2549 (if (looking-at "[%\n]")
2550 (forward-line 1))
2551 (<= (point) pos))))
2552 (setq arg (1- arg)))
2553 ;; Backward
2554 (while (< arg 0)
2555 (let ((pos (point)))
2556 (erlang-beginning-of-clause 1)
2557 (erlang-pass-over-function)
2558 (forward-line 1)
2559 (if (>= (point) pos)
2560 (if (erlang-beginning-of-function 2)
2561 (progn
2562 (erlang-pass-over-function)
2563 (skip-chars-forward " \t")
2564 (if (looking-at "[%\n]")
2565 (forward-line 1)))
2566 (goto-char (point-min)))))
2567 (setq arg (1+ arg)))))
2569 (eval-and-compile
2570 (if (default-boundp 'beginning-of-defun-function)
2571 (defalias 'erlang-mark-function 'mark-defun)
2572 (defun erlang-mark-function ()
2573 "Put mark at end of function, point at beginning."
2574 (interactive)
2575 (push-mark (point))
2576 (erlang-end-of-function 1)
2577 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2578 ;; the region.
2579 (condition-case nil
2580 (push-mark (point) nil t)
2581 (error (push-mark (point))))
2582 (erlang-beginning-of-function 1)
2583 ;; The above function deactivates the mark.
2584 (if (boundp 'deactivate-mark)
2585 (funcall (symbol-function 'set) 'deactivate-mark nil)))))
2587 (defun erlang-pass-over-function ()
2588 (while (progn
2589 (erlang-skip-blank)
2590 (and (not (looking-at "\\.\\(\\s \\|\n\\|\\s<\\)"))
2591 (not (eobp))))
2592 (forward-sexp 1))
2593 (if (not (eobp))
2594 (forward-char 1)))
2596 (defun erlang-name-of-function ()
2597 (save-excursion
2598 ;; Skip over attribute leader.
2599 (if (looking-at "-[ \t]*")
2600 (re-search-forward "-[ \t]*" nil 'move))
2601 (let ((start (point)))
2602 (forward-sexp 1)
2603 (buffer-substring start (point)))))
2606 ;;; Miscellaneous
2608 (defun erlang-fill-paragraph (&optional justify)
2609 "Like \\[fill-paragraph], but handle Erlang comments.
2610 If any of the current line is a comment, fill the comment or the
2611 paragraph of it that point is in, preserving the comment's indentation
2612 and initial `%':s."
2613 (interactive "P")
2614 (let ((has-comment nil)
2615 ;; If has-comment, the appropriate fill-prefix for the comment.
2616 comment-fill-prefix)
2617 ;; Figure out what kind of comment we are looking at.
2618 (save-excursion
2619 (beginning-of-line)
2620 (cond
2621 ;; Find the command prefix.
2622 ((looking-at (concat "\\s *" comment-start-skip))
2623 (setq has-comment t)
2624 (setq comment-fill-prefix (buffer-substring (match-beginning 0)
2625 (match-end 0))))
2626 ;; A line with some code, followed by a comment? Remember that the
2627 ;; % which starts the comment shouldn't be part of a string or
2628 ;; character.
2629 ((progn
2630 (while (not (looking-at "%\\|$"))
2631 (skip-chars-forward "^%\n\"\\\\")
2632 (cond
2633 ((eq (char-after (point)) ?\\) (forward-char 2))
2634 ((eq (char-after (point)) ?\") (forward-sexp 1))))
2635 (looking-at comment-start-skip))
2636 (setq has-comment t)
2637 (setq comment-fill-prefix
2638 (concat (make-string (current-column) ? )
2639 (buffer-substring (match-beginning 0) (match-end 0)))))))
2640 (if (not has-comment)
2641 (fill-paragraph justify)
2642 ;; Narrow to include only the comment, and then fill the region.
2643 (save-restriction
2644 (narrow-to-region
2645 ;; Find the first line we should include in the region to fill.
2646 (save-excursion
2647 (while (and (zerop (forward-line -1))
2648 (looking-at "^\\s *%")))
2649 ;; We may have gone to far. Go forward again.
2650 (or (looking-at "^\\s *%")
2651 (forward-line 1))
2652 (point))
2653 ;; Find the beginning of the first line past the region to fill.
2654 (save-excursion
2655 (while (progn (forward-line 1)
2656 (looking-at "^\\s *%")))
2657 (point)))
2658 ;; Lines with only % on them can be paragraph boundaries.
2659 (let ((paragraph-start (concat paragraph-start "\\|^[ \t%]*$"))
2660 (paragraph-separate (concat paragraph-start "\\|^[ \t%]*$"))
2661 (fill-prefix comment-fill-prefix))
2662 (fill-paragraph justify))))))
2665 (defun erlang-uncomment-region (beg end)
2666 "Uncomment all commented lines in the region."
2667 (interactive "r")
2668 (comment-region beg end -1))
2671 (defun erlang-generate-new-clause ()
2672 "Create additional Erlang clause header.
2674 Parses the source file for the name of the current Erlang function.
2675 Create the header containing the name, A pair of parentheses,
2676 and an arrow. The space between the function name and the
2677 first parenthesis is preserved. The point is placed between
2678 the parentheses."
2679 (interactive)
2680 (let ((name (save-excursion
2681 (and (erlang-beginning-of-clause)
2682 (erlang-get-function-name t))))
2683 (arrow (save-excursion
2684 (and (erlang-beginning-of-clause)
2685 (erlang-get-function-arrow)))))
2686 (if (or (null arrow) (null name))
2687 (error "Can't find name of current Erlang function"))
2688 (if (and (bolp) (eolp))
2690 (end-of-line)
2691 (newline))
2692 (insert name)
2693 (save-excursion
2694 (insert ") " arrow))
2695 (if erlang-new-clause-with-arguments
2696 (erlang-clone-arguments))))
2699 (defun erlang-clone-arguments ()
2700 "Insert, at the point, the argument list of the previous clause.
2702 The mark is set at the beginning of the inserted text, the point
2703 at the end."
2704 (interactive)
2705 (let ((args (save-excursion
2706 (beginning-of-line)
2707 (and (erlang-beginning-of-clause)
2708 (erlang-get-function-arguments))))
2709 (p (point)))
2710 (if (null args)
2711 (error "Can't clone argument list"))
2712 (insert args)
2713 (set-mark p)))
2715 ;;; Information retrieval functions.
2717 (defun erlang-buffer-substring (beg end)
2718 "Like `buffer-substring-no-properties'.
2719 Although, this function works on all versions of Emacs."
2720 (if (fboundp 'buffer-substring-no-properties)
2721 (funcall (symbol-function 'buffer-substring-no-properties) beg end)
2722 (buffer-substring beg end)))
2725 (defun erlang-get-module ()
2726 "Return the name of the module as specified by `-module'.
2728 Return nil if file contains no `-module' attribute."
2729 (save-excursion
2730 (save-restriction
2731 (widen)
2732 (goto-char (point-min))
2733 (let ((md (match-data)))
2734 (unwind-protect
2735 (if (re-search-forward
2736 (eval-when-compile
2737 (concat "^-module\\s *(\\s *\\(\\("
2738 erlang-atom-regexp
2739 "\\)?\\)\\s *)\\s *\\."))
2740 (point-max) t)
2741 (erlang-remove-quotes
2742 (erlang-buffer-substring (match-beginning 1)
2743 (match-end 1)))
2744 nil)
2745 (store-match-data md))))))
2748 (defun erlang-get-module-from-file-name (&optional file)
2749 "Extract the module name from a file name.
2751 First, the directory part is removed. Second, the part of the file name
2752 matching `erlang-file-name-extension-regexp' is removed.
2754 Should the match fail, nil is returned.
2756 By modifying `erlang-file-name-extension-regexp' to match files other
2757 than Erlang source files, Erlang specific functions could be applied on
2758 non-Erlang files. Most notably; the support for Erlang modules in the
2759 tags system could be used by files written in other languages."
2760 (or file (setq file buffer-file-name))
2761 (if (null file)
2763 (setq file (file-name-nondirectory file))
2764 (if (string-match erlang-file-name-extension-regexp file)
2765 (substring file 0 (match-beginning 0))
2766 nil)))
2769 ;; Used by `erlang-get-export' and `erlang-get-import'.
2771 (defun erlang-get-function-arity-list ()
2772 "Parse list of `function/arity' as used by `-import' and `-export'.
2774 Point must be before the opening bracket. When the
2775 function returns the point will be placed after the closing bracket.
2777 The function does not return an error if the list is incorrectly
2778 formatted.
2780 Return list of (function . arity). The order of the returned list
2781 corresponds to the order of the parsed Erlang list."
2782 (let ((res '()))
2783 (erlang-skip-blank)
2784 (forward-char 1)
2785 (if (not (eq (preceding-char) ?\[))
2786 '() ; Not looking at an Erlang list.
2787 (while ; Note: `while' has no body.
2788 (progn
2789 (erlang-skip-blank)
2790 (and (looking-at (eval-when-compile
2791 (concat erlang-atom-regexp "/\\([0-9]+\\)\\>")))
2792 (progn
2793 (setq res (cons
2794 (cons
2795 (erlang-remove-quotes
2796 (erlang-buffer-substring
2797 (match-beginning 1) (match-end 1)))
2798 (string-to-int
2799 (erlang-buffer-substring
2800 (match-beginning
2801 (+ 1 erlang-atom-regexp-matches))
2802 (match-end
2803 (+ 1 erlang-atom-regexp-matches)))))
2804 res))
2805 (goto-char (match-end 0))
2806 (erlang-skip-blank)
2807 (forward-char 1)
2808 ;; Test if there are more exported functions.
2809 (eq (preceding-char) ?,))))))
2810 (nreverse res)))
2813 ;;; Note that `-export' and the open parenthesis must be written on
2814 ;;; the same line.
2816 (defun erlang-get-export ()
2817 "Return a list of `(function . arity)' as specified by `-export'."
2818 (save-excursion
2819 (goto-char (point-min))
2820 (let ((md (match-data))
2821 (res '()))
2822 (unwind-protect
2823 (progn
2824 (while (re-search-forward "^-export\\s *(" (point-max) t)
2825 (erlang-skip-blank)
2826 (setq res (nconc res (erlang-get-function-arity-list))))
2827 res)
2828 (store-match-data md)))))
2831 (defun erlang-get-import ()
2832 "Parse an Erlang source file for imported functions.
2834 Return an alist with module name as car part and list of conses containing
2835 function and arity as cdr part."
2836 (save-excursion
2837 (goto-char (point-min))
2838 (let ((md (match-data))
2839 (res '()))
2840 (unwind-protect
2841 (progn
2842 (while (re-search-forward "^-import\\s *(" (point-max) t)
2843 (erlang-skip-blank)
2844 (if (looking-at erlang-atom-regexp)
2845 (let ((module (erlang-remove-quotes
2846 (erlang-buffer-substring
2847 (match-beginning 0)
2848 (match-end 0)))))
2849 (goto-char (match-end 0))
2850 (erlang-skip-blank)
2851 (if (eq (following-char) ?,)
2852 (progn
2853 (forward-char 1)
2854 (erlang-skip-blank)
2855 (let ((funcs (erlang-get-function-arity-list))
2856 (pair (assoc module res)))
2857 (if pair
2858 (setcdr pair (nconc (cdr pair) funcs))
2859 (setq res (cons (cons module funcs)
2860 res)))))))))
2861 (nreverse res))
2862 (store-match-data md)))))
2865 (defun erlang-get-function-name (&optional arg)
2866 "Return name of current function, or nil.
2868 If optional argument is non-nil, everything up to and including
2869 the first `(' is returned.
2871 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2872 (save-excursion
2873 (if (not (eobp)) (forward-char 1))
2874 (and (erlang-beginning-of-clause)
2875 (erlang-get-function-name t)))"
2876 (let ((n (if arg 0 1)))
2877 (and (looking-at (eval-when-compile
2878 (concat "^" erlang-atom-regexp "\\s *(")))
2879 (erlang-buffer-substring (match-beginning n) (match-end n)))))
2882 (defun erlang-get-function-arrow ()
2883 "Return arrow of current function, could be \"->\", \":-\" or nil.
2885 The \":-\" arrow is used by mnesia queries.
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-arrow)))"
2892 (and
2893 (save-excursion
2894 (re-search-forward "[^-:]*-\\|:" (point-max) t)
2895 (erlang-buffer-substring (- (point) 1) (+ (point) 1)))))
2897 (defun erlang-get-function-arity ()
2898 "Return the number of arguments of function at point, or nil."
2899 (and (looking-at (eval-when-compile
2900 (concat "^" erlang-atom-regexp "\\s *(")))
2901 (save-excursion
2902 (goto-char (match-end 0))
2903 (condition-case nil
2904 (let ((res 0)
2905 (cont t))
2906 (while cont
2907 (cond ((eobp)
2908 (setq res nil)
2909 (setq cont nil))
2910 ((looking-at "\\s *)")
2911 (setq cont nil))
2912 ((looking-at "\\s *\\($\\|%\\)")
2913 (forward-line 1))
2914 ((looking-at "\\s *,")
2915 (setq res (+ 1 res))
2916 (goto-char (match-end 0)))
2918 (when (zerop res)
2919 (setq res (+ 1 res)))
2920 (forward-sexp 1))))
2921 res)
2922 (error nil)))))
2924 (defun erlang-get-function-arguments ()
2925 "Return arguments of current function, or nil."
2926 (if (not (looking-at (eval-when-compile
2927 (concat "^" erlang-atom-regexp "\\s *("))))
2929 (save-excursion
2930 (condition-case nil
2931 (let ((start (match-end 0)))
2932 (goto-char (- start 1))
2933 (forward-sexp)
2934 (erlang-buffer-substring start (- (point) 1)))
2935 (error nil)))))
2938 (defun erlang-get-function-under-point ()
2939 "Return the module and function under the point, or nil.
2941 Should no explicit module name be present at the point, the
2942 list of imported functions is searched.
2944 The following could be returned:
2945 (\"module\" \"function\") -- Both module and function name found.
2946 (nil \"function\") -- No module name was found.
2947 nil -- No function name found
2949 In the future the list may contain more elements."
2950 (save-excursion
2951 (let ((md (match-data))
2952 (res nil))
2953 (if (eq (char-syntax (following-char)) ? )
2954 (skip-chars-backward " \t"))
2955 (skip-chars-backward "a-zA-Z0-9_:'")
2956 (cond ((looking-at (eval-when-compile
2957 (concat erlang-atom-regexp ":" erlang-atom-regexp)))
2958 (setq res (list
2959 (erlang-remove-quotes
2960 (erlang-buffer-substring
2961 (match-beginning 1) (match-end 1)))
2962 (erlang-remove-quotes
2963 (erlang-buffer-substring
2964 (match-beginning (1+ erlang-atom-regexp-matches))
2965 (match-end (1+ erlang-atom-regexp-matches)))))))
2966 ((looking-at erlang-atom-regexp)
2967 (let ((fk (erlang-remove-quotes
2968 (erlang-buffer-substring
2969 (match-beginning 0) (match-end 0))))
2970 (mod nil)
2971 (imports (erlang-get-import)))
2972 (while (and imports (null mod))
2973 (if (assoc fk (cdr (car imports)))
2974 (setq mod (car (car imports)))
2975 (setq imports (cdr imports))))
2976 (setq res (list mod fk)))))
2977 (store-match-data md)
2978 res)))
2981 ;; TODO: Escape single quotes inside the string without
2982 ;; replace-regexp-in-string.
2983 (defun erlang-add-quotes-if-needed (str)
2984 "Return STR, possibly with quotes."
2985 (if (and (stringp str)
2986 (not (string-match (eval-when-compile
2987 (concat "\\`" erlang-atom-regexp "\\'")) str)))
2988 (progn (if (fboundp 'replace-regexp-in-string)
2989 (setq str (replace-regexp-in-string "'" "\\'" str t t )))
2990 (concat "'" str "'"))
2991 str))
2994 (defun erlang-remove-quotes (str)
2995 "Return STR without quotes, if present."
2996 (let ((md (match-data)))
2997 (prog1
2998 (if (string-match "\\`'\\(.*\\)'\\'" str)
2999 (substring str 1 -1)
3000 str)
3001 (store-match-data md))))
3004 ;;; Check module name
3006 ;; The function `write-file', bound to C-x C-w, calls
3007 ;; `set-visited-file-name' which clears the hook. :-(
3008 ;; To make sure that the hook always is present, we advise
3009 ;; `set-visited-file-name'.
3010 (defun erlang-check-module-name-init ()
3011 "Initialize the functionality to compare file and module names.
3013 Unless we have `before-save-hook', we redefine the function
3014 `set-visited-file-name' since it clears the variable
3015 `local-write-file-hooks'. The original function definition is
3016 stored in `erlang-orig-set-visited-file-name'."
3017 (if (boundp 'before-save-hook)
3018 ;; If we have that, `make-local-hook' is obsolete.
3019 (add-hook 'before-save-hook 'erlang-check-module-name nil t)
3020 (require 'advice)
3021 (unless (ad-advised-definition-p 'set-visited-file-name)
3022 (defadvice set-visited-file-name (after erlang-set-visited-file-name
3023 activate)
3024 (if (eq major-mode 'erlang-mode)
3025 (add-hook 'local-write-file-hooks 'erlang-check-module-name))))
3026 (add-hook 'local-write-file-hooks 'erlang-check-module-name)))
3029 (defun erlang-check-module-name ()
3030 "If the module name doesn't match file name, ask for permission to change.
3032 The variable `erlang-check-module-name' controls the behaviour of this
3033 function. It it is nil, this function does nothing. If it is t, the
3034 source is silently changed. If it is set to the atom `ask', the user
3035 is prompted.
3037 This function is normally placed in the hook `local-write-file-hooks'."
3038 (if erlang-check-module-name
3039 (let ((mn (erlang-get-module))
3040 (fn (erlang-get-module-from-file-name (buffer-file-name))))
3041 (if (and (stringp mn) (stringp fn))
3042 (or (string-equal mn fn)
3043 (if (or (eq erlang-check-module-name t)
3044 (y-or-n-p
3045 "Module does not match file name. Modify source? "))
3046 (save-excursion
3047 (save-restriction
3048 (widen)
3049 (goto-char (point-min))
3050 (if (re-search-forward
3051 (eval-when-compile
3052 (concat "^-module\\s *(\\s *\\(\\("
3053 erlang-atom-regexp
3054 "\\)?\\)\\s *)\\s *\\."))
3055 (point-max) t)
3056 (progn
3057 (goto-char (match-beginning 1))
3058 (delete-region (match-beginning 1)
3059 (match-end 1))
3060 (insert fn))))))))))
3061 ;; Must return nil since it is added to `local-write-file-hook'.
3062 nil)
3065 ;;; Electric functions.
3067 (defun erlang-electric-semicolon (&optional arg)
3068 "Insert a semicolon character and possibly a prototype for the next line.
3070 The variable `erlang-electric-semicolon-criteria' states a criterion,
3071 when fulfilled a newline is inserted, the next line is indented and a
3072 prototype for the next line is inserted. Normally the prototype
3073 consists of \" ->\". Should the semicolon end the clause a new clause
3074 header is generated.
3076 The variable `erlang-electric-semicolon-insert-blank-lines' controls
3077 the number of blank lines inserted between the current line and new
3078 function header.
3080 Behaves just like the normal semicolon when supplied with a
3081 numerical arg, point is inside string or comment, or when there are
3082 non-whitespace characters following the point on the current line."
3083 (interactive "P")
3084 (self-insert-command (prefix-numeric-value arg))
3085 (if (or arg
3086 (and (listp erlang-electric-commands)
3087 (not (memq 'erlang-electric-semicolon
3088 erlang-electric-commands)))
3089 (erlang-in-literal)
3090 (not (looking-at "\\s *\\(%.*\\)?$"))
3091 (null (erlang-test-criteria-list
3092 erlang-electric-semicolon-criteria)))
3093 (setq erlang-electric-newline-inhibit nil)
3094 (setq erlang-electric-newline-inhibit t)
3095 (undo-boundary)
3096 (end-of-line)
3097 (newline)
3098 (if (condition-case nil
3099 (progn (erlang-indent-line) t)
3100 (error (if (bolp) (delete-backward-char 1))))
3101 (if (not (bolp))
3102 (save-excursion
3103 (insert " ->"))
3104 (condition-case nil
3105 (progn
3106 (erlang-generate-new-clause)
3107 (if erlang-electric-semicolon-insert-blank-lines
3108 (save-excursion
3109 (beginning-of-line)
3110 (newline
3111 erlang-electric-semicolon-insert-blank-lines))))
3112 (error (if (bolp) (delete-backward-char 1))))))))
3115 (defun erlang-electric-comma (&optional arg)
3116 "Insert a comma character and possibly a new indented line.
3117 The variable `erlang-electric-comma-criteria' states a criterion,
3118 when fulfilled a newline is inserted and the next line is indented.
3120 Behaves just like the normal comma when supplied with a
3121 numerical arg, point is inside string or comment, or when there are
3122 non-whitespace characters following the point on the current line."
3123 (interactive "P")
3125 (self-insert-command (prefix-numeric-value arg))
3127 (if (or arg
3128 (and (listp erlang-electric-commands)
3129 (not (memq 'erlang-electric-comma erlang-electric-commands)))
3130 (erlang-in-literal)
3131 (not (looking-at "\\s *\\(%.*\\)?$"))
3132 (null (erlang-test-criteria-list
3133 erlang-electric-comma-criteria)))
3134 (setq erlang-electric-newline-inhibit nil)
3135 (setq erlang-electric-newline-inhibit t)
3136 (undo-boundary)
3137 (end-of-line)
3138 (newline)
3139 (condition-case nil
3140 (erlang-indent-line)
3141 (error (if (bolp) (delete-backward-char 1))))))
3143 (defun erlang-electric-lt (&optional arg)
3144 "Insert a less-than sign, and optionally mark it as an open paren."
3146 (interactive "p")
3148 (self-insert-command arg)
3150 ;; Was this the second char in bit-syntax open (`<<')?
3151 (unless (< (point) 2)
3152 (save-excursion
3153 (backward-char 2)
3154 (when (and (eq (char-after (point)) ?<)
3155 (not (eq (get-text-property (point) 'category)
3156 'bitsyntax-open-inner)))
3157 ;; Then mark the two chars...
3158 (put-text-property (point) (1+ (point))
3159 'category 'bitsyntax-open-outer)
3160 (forward-char 1)
3161 (put-text-property (point) (1+ (point))
3162 'category 'bitsyntax-open-inner)
3163 ;;...and unmark any subsequent less-than chars.
3164 (forward-char 1)
3165 (while (eq (char-after (point)) ?<)
3166 (remove-text-properties (point) (1+ (point))
3167 '(category nil))
3168 (forward-char 1))))))
3170 (defun erlang-after-bitsyntax-close ()
3171 "Return t if point is immediately after a bit-syntax close parenthesis (`>>')."
3172 (and (>= (point) 2)
3173 (save-excursion
3174 (backward-char 2)
3175 (and (eq (char-after (point)) ?>)
3176 (not (eq (get-text-property (point) 'category)
3177 'bitsyntax-close-outer))))))
3179 (defun erlang-after-arrow ()
3180 "Return true if point is immediately after a function arrow (`->')."
3181 (and (>= (point) 2)
3182 (and
3183 (save-excursion
3184 (backward-char)
3185 (eq (char-before (point)) ?-))
3186 (or (not (listp erlang-electric-commands))
3187 (memq 'erlang-electric-gt
3188 erlang-electric-commands))
3189 (not (erlang-in-literal))
3190 (looking-at "\\s *\\(%.*\\)?$")
3191 (erlang-test-criteria-list erlang-electric-arrow-criteria))))
3194 (defun erlang-electric-gt (&optional arg)
3195 "Insert a greater-than sign, and optionally mark it as a close paren."
3197 (interactive "p")
3199 (self-insert-command arg)
3201 (cond
3202 ;; Did we just write a bit-syntax close (`>>')?
3203 ((erlang-after-bitsyntax-close)
3204 (save-excursion
3205 ;; Then mark the two chars...
3206 (backward-char 2)
3207 (put-text-property (point) (1+ (point))
3208 'category 'bitsyntax-close-inner)
3209 (forward-char)
3210 (put-text-property (point) (1+ (point))
3211 'category 'bitsyntax-close-outer)
3212 ;;...and unmark any subsequent greater-than chars.
3213 (forward-char)
3214 (while (eq (char-after (point)) ?>)
3215 (remove-text-properties (point) (1+ (point))
3216 '(category nil))
3217 (forward-char))))
3219 ;; Did we just write a function arrow (`->')?
3220 ((erlang-after-arrow)
3221 (let ((erlang-electric-newline-inhibit t))
3222 (undo-boundary)
3223 (end-of-line)
3224 (newline)
3225 (condition-case nil
3226 (erlang-indent-line)
3227 (error (if (bolp) (delete-backward-char 1))))))
3229 ;; Then it's just a plain greater-than.
3231 nil)))
3234 (defun erlang-electric-arrow\ off (&optional arg)
3235 "Insert a '>'-sign and possibly a new indented line.
3237 This command is only `electric' when the `>' is part of an `->' arrow.
3238 The variable `erlang-electric-arrow-criteria' states a sequence of
3239 criteria, which decides when a newline should be inserted and the next
3240 line indented.
3242 It behaves just like the normal greater than sign when supplied with a
3243 numerical arg, point is inside string or comment, or when there are
3244 non-whitespace characters following the point on the current line.
3246 After being split/merged into `erlang-after-arrow' and
3247 `erlang-electric-gt', it is now unused and disabled."
3248 (interactive "P")
3249 (let ((prec (preceding-char)))
3250 (self-insert-command (prefix-numeric-value arg))
3251 (if (or arg
3252 (and (listp erlang-electric-commands)
3253 (not (memq 'erlang-electric-arrow
3254 erlang-electric-commands)))
3255 (not (eq prec ?-))
3256 (erlang-in-literal)
3257 (not (looking-at "\\s *\\(%.*\\)?$"))
3258 (null (erlang-test-criteria-list
3259 erlang-electric-arrow-criteria)))
3260 (setq erlang-electric-newline-inhibit nil)
3261 (setq erlang-electric-newline-inhibit t)
3262 (undo-boundary)
3263 (end-of-line)
3264 (newline)
3265 (condition-case nil
3266 (erlang-indent-line)
3267 (error (if (bolp) (delete-backward-char 1)))))))
3270 (defun erlang-electric-newline (&optional arg)
3271 "Break line at point and indent, continuing comment if within one.
3272 The variable `erlang-electric-newline-criteria' states a criterion,
3273 when fulfilled a newline is inserted and the next line is indented.
3275 Should the current line begin with a comment, and the variable
3276 `comment-multi-line' be non-nil, a new comment start is inserted.
3278 Should the previous command be another electric command we assume that
3279 the user pressed newline out of old habit, hence we will do nothing."
3280 (interactive "P")
3281 (cond ((and (not arg)
3282 erlang-electric-newline-inhibit
3283 (memq last-command erlang-electric-newline-inhibit-list))
3284 ()) ; Do nothing!
3285 ((or arg
3286 (and (listp erlang-electric-commands)
3287 (not (memq 'erlang-electric-newline
3288 erlang-electric-commands)))
3289 (null (erlang-test-criteria-list
3290 erlang-electric-newline-criteria)))
3291 (newline (prefix-numeric-value arg)))
3293 (if (and comment-multi-line
3294 (save-excursion
3295 (beginning-of-line)
3296 (looking-at (concat "\\s *" comment-start-skip))))
3297 (let ((str (buffer-substring
3298 (or (match-end 1) (match-beginning 0))
3299 (min (match-end 0) (point)))))
3300 (newline)
3301 (undo-boundary)
3302 (insert str))
3303 (newline-and-indent)))))
3306 (defun erlang-test-criteria-list (criteria)
3307 "Given a list of criterion functions, test if criteria are fulfilled.
3309 Each element in the criteria list can a function returning nil, t or
3310 the atom `stop'. t means that the criterion is fulfilled, `stop' means
3311 that it isn't fulfilled and that the search should stop,
3312 and nil means continue searching.
3314 Should the list contain the atom t the criterion is assumed to be
3315 fulfilled, unless preceded by a function returning `stop', of course.
3317 Should the argument be the atom t instead of a list, the criterion is
3318 assumed to be trivially true.
3320 Should all functions return nil, the criteria are assumed not to be
3321 fulfilled.
3323 Return t if criteria fulfilled, nil otherwise."
3324 (if (eq criteria t)
3326 (save-excursion
3327 (let ((answer nil))
3328 (while (and criteria (null answer))
3329 (if (eq (car criteria) t)
3330 (setq answer t)
3331 (setq answer (funcall (car criteria))))
3332 (setq criteria (cdr criteria)))
3333 (if (and answer (not (eq answer 'stop)))
3335 nil)))))
3338 (defun erlang-in-literal (&optional lim)
3339 "Test if point is in string, quoted atom or comment.
3341 Return one of the three atoms `atom', `string', and `comment'.
3342 Should the point be inside none of the above mentioned types of
3343 context, nil is returned."
3344 (save-excursion
3345 (let* ((lim (or lim (save-excursion
3346 (erlang-beginning-of-clause)
3347 (point))))
3348 (state (if (fboundp 'syntax-ppss) ; post Emacs 21.3
3349 (syntax-ppss)
3350 (parse-partial-sexp lim (point)))))
3351 (cond
3352 ((eq (nth 3 state) ?') 'atom)
3353 ((nth 3 state) 'string)
3354 ((nth 4 state) 'comment)
3355 (t nil)))))
3358 (defun erlang-at-end-of-function-p ()
3359 "Test if point is at end of an Erlang function.
3361 This function is designed to be a member of a criteria list."
3362 (eq (save-excursion (erlang-skip-blank) (point))
3363 (save-excursion
3364 (erlang-beginning-of-function -1) (point))))
3367 (defun erlang-at-end-of-clause-p ()
3368 "Test if point is at end of an Erlang clause.
3370 This function is designed to be a member of a criteria list."
3371 (eq (save-excursion (erlang-skip-blank) (point))
3372 (save-excursion
3373 (erlang-beginning-of-clause -1) (point))))
3376 (defun erlang-stop-when-inside-argument-list ()
3377 "Return `stop' if inside parenthesis list, nil otherwise.
3379 Knows about the list comprehension syntax. When the point is
3380 after `||', `stop' is not returned.
3382 This function is designed to be a member of a criteria list."
3383 (save-excursion
3384 (condition-case nil
3385 (let ((orig-point (point))
3386 (state nil))
3387 (up-list -1)
3388 (if (not (eq (following-char) ?\[))
3389 'stop
3390 ;; Do not return `stop' when inside a list comprehension
3391 ;; construction. (The point must be after `||').
3392 (while (< (point) orig-point)
3393 (setq state (erlang-partial-parse (point) orig-point state)))
3394 (if (and (car state) (eq (car (car (car state))) '||))
3396 'stop)))
3397 (error
3398 nil))))
3401 (defun erlang-stop-when-at-guard ()
3402 "Return `stop' when at function guards.
3404 This function is designed to be a member of a criteria list."
3405 (save-excursion
3406 (beginning-of-line)
3407 (if (and (looking-at (eval-when-compile
3408 (concat "^" erlang-atom-regexp "\\s *(")))
3409 (not (looking-at
3410 (eval-when-compile
3411 (concat "^" erlang-atom-regexp ".*\\(->\\|:-\\)")))))
3412 'stop
3413 nil)))
3416 (defun erlang-next-lines-empty-p ()
3417 "Return non-nil if next lines are empty.
3419 The variable `erlang-next-lines-empty-threshold' contains the number
3420 of lines required to be empty.
3422 A line containing only spaces and tabs is considered empty.
3424 This function is designed to be a member of a criteria list."
3425 (and erlang-next-lines-empty-threshold
3426 (save-excursion
3427 (let ((left erlang-next-lines-empty-threshold)
3428 (cont t))
3429 (while (and cont (> left 0))
3430 (forward-line 1)
3431 (setq cont (looking-at "\\s *$"))
3432 (setq left (- left 1)))
3433 cont))))
3436 (defun erlang-at-keyword-end-p ()
3437 "Test if next readable token is the keyword end.
3439 This function is designed to be a member of a criteria list."
3440 (save-excursion
3441 (erlang-skip-blank)
3442 (looking-at "end[^_a-zA-Z0-9]")))
3445 ;; Erlang tags support which is aware of erlang modules.
3447 ;; Not yet implemented under XEmacs. (Hint: The Emacs 19 etags
3448 ;; package works under XEmacs.)
3450 (eval-when-compile
3451 (if (or (featurep 'bytecomp)
3452 (featurep 'byte-compile))
3453 (progn
3454 (require 'etags))))
3457 ;; Variables:
3459 (defvar erlang-tags-function-alist
3460 '((find-tag . erlang-find-tag)
3461 (find-tag-other-window . erlang-find-tag-other-window)
3462 (find-tag-regexp . erlang-find-tag-regexp)
3463 (find-tag-other-frame . erlang-find-tag-other-frame))
3464 "Alist of old tags commands and the replacement functions.")
3466 (defvar erlang-tags-installed nil
3467 "Non-nil when the Erlang tags system is installed.")
3468 (defvar erlang-tags-file-list '()
3469 "List of files in tag list. Used when finding tag on form `module:'.")
3470 (defvar erlang-tags-completion-table nil
3471 "Like `tags-completion-table', this table contains `tag' and `module:tag'.")
3472 (defvar erlang-tags-buffer-installed-p nil
3473 "Non-nil when Erlang module recognising functions installed.")
3474 (defvar erlang-tags-buffer-list '()
3475 "Temporary list of buffers.")
3476 (defvar erlang-tags-orig-completion-table nil
3477 "Temporary storage for `tags-completion-table'.")
3478 (defvar erlang-tags-orig-tag-order nil
3479 "Temporary storage for `find-tag-tag-order'.")
3480 (defvar erlang-tags-orig-regexp-tag-order nil
3481 "Temporary storage for `find-tag-regexp-tag-order'.")
3482 (defvar erlang-tags-orig-search-function nil
3483 "Temporary storage for `find-tag-search-function'.")
3484 (defvar erlang-tags-orig-regexp-search-function nil
3485 "Temporary storage for `find-tag-regexp-search-function'.")
3486 (defvar erlang-tags-orig-format-hooks nil
3487 "Temporary storage for `tags-table-format-hooks'.")
3489 (defun erlang-tags-init ()
3490 "Install an alternate version of tags, aware of Erlang modules.
3492 After calling this function, the tags functions are aware of
3493 Erlang modules. Tags can be entered on the for `module:tag' as well
3494 as on the old form `tag'.
3496 In the completion list, `module:tag' and `module:' shows up.
3498 Call this function from an appropriate init file, or add it to
3499 Erlang mode hook with the commands:
3500 (add-hook 'erlang-mode-hook 'erlang-tags-init)
3501 (add-hook 'erlang-shell-mode-hook 'erlang-tags-init)
3503 This function only works under Emacs 18 and Emacs 19. Currently, It
3504 is not implemented under XEmacs. (Hint: The Emacs 19 etags module
3505 works under XEmacs.)"
3506 (interactive)
3507 (cond ((= erlang-emacs-major-version 18)
3508 (require 'tags)
3509 (erlang-tags-define-keys (current-local-map))
3510 (setq erlang-tags-installed t))
3512 (require 'etags)
3513 ;; Test on a function available in the Emacs 19 version
3514 ;; of tags but not in the XEmacs version.
3515 (if (not (fboundp 'find-tag-noselect))
3517 (erlang-tags-define-keys (current-local-map))
3518 (setq erlang-tags-installed t)))))
3521 ;; Set all keys bound to `find-tag' et.al. in the global map and the
3522 ;; menu to `erlang-find-tag' et.al. in `map'.
3524 ;; The function `substitute-key-definition' does not work properly
3525 ;; in all version of Emacs.
3527 (defun erlang-tags-define-keys (map)
3528 "Bind tags commands to keymap MAP aware of Erlang modules."
3529 (let ((alist erlang-tags-function-alist))
3530 (while alist
3531 (let* ((old (car (car alist)))
3532 (new (cdr (car alist)))
3533 (keys (append (where-is-internal old global-map))))
3534 (while keys
3535 (define-key map (car keys) new)
3536 (setq keys (cdr keys))))
3537 (setq alist (cdr alist))))
3538 ;; Update the menu.
3539 (erlang-menu-substitute erlang-menu-base-items erlang-tags-function-alist)
3540 (erlang-menu-init))
3543 ;; There exists a variable `find-tag-default-function'. It is not used
3544 ;; since `complete-tag' uses it to get current word under point. In that
3545 ;; situation we don't want the module to be prepended.
3547 (defun erlang-find-tag-default ()
3548 "Return the default tag.
3549 Search `-import' list of imported functions.
3550 Single quotes are been stripped away."
3551 (let ((mod-func (erlang-get-function-under-point)))
3552 (cond ((null mod-func)
3553 nil)
3554 ((null (car mod-func))
3555 (nth 1 mod-func))
3557 (concat (car mod-func) ":" (nth 1 mod-func))))))
3560 ;; Return `t' since it is used inside `tags-loop-form'.
3561 ;;;###autoload
3562 (defun erlang-find-tag (modtagname &optional next-p regexp-p)
3563 "Like `find-tag'. Capable of retrieving Erlang modules.
3565 Tags can be given on the forms `tag', `module:', `module:tag'."
3566 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3567 (switch-to-buffer (erlang-find-tag-noselect modtagname next-p regexp-p))
3571 ;; Code mainly from `find-tag-other-window' in `etags.el'.
3572 ;;;###autoload
3573 (defun erlang-find-tag-other-window (tagname &optional next-p regexp-p)
3574 "Like `find-tag-other-window' but aware of Erlang modules."
3575 (interactive (erlang-tag-interactive
3576 "Find `module:tag' or `tag' other window: "))
3578 ;; This is to deal with the case where the tag is found in the
3579 ;; selected window's buffer; without this, point is moved in both
3580 ;; windows. To prevent this, we save the selected window's point
3581 ;; before doing find-tag-noselect, and restore it afterwards.
3582 (let* ((window-point (window-point (selected-window)))
3583 (tagbuf (erlang-find-tag-noselect tagname next-p regexp-p))
3584 (tagpoint (progn (set-buffer tagbuf) (point))))
3585 (set-window-point (prog1
3586 (selected-window)
3587 (switch-to-buffer-other-window tagbuf)
3588 ;; We have to set this new window's point; it
3589 ;; might already have been displaying a
3590 ;; different portion of tagbuf, in which case
3591 ;; switch-to-buffer-other-window doesn't set
3592 ;; the window's point from the buffer.
3593 (set-window-point (selected-window) tagpoint))
3594 window-point)))
3597 (defun erlang-find-tag-other-frame (tagname &optional next-p)
3598 "Like `find-tag-other-frame' but aware of Erlang modules."
3599 (interactive (erlang-tag-interactive
3600 "Find `module:tag' or `tag' other frame: "))
3601 (let ((pop-up-frames t))
3602 (erlang-find-tag-other-window tagname next-p)))
3605 (defun erlang-find-tag-regexp (regexp &optional next-p other-window)
3606 "Like `find-tag-regexp' but aware of Erlang modules."
3607 (interactive (if (fboundp 'find-tag-regexp)
3608 (erlang-tag-interactive
3609 "Find `module:regexp' or `regexp': ")
3610 (error "This version of Emacs can't find tags by regexps")))
3611 (funcall (if other-window
3612 'erlang-find-tag-other-window
3613 'erlang-find-tag)
3614 regexp next-p t))
3617 ;; Just like C-u M-. This could be added to the menu.
3618 (defun erlang-find-next-tag ()
3619 "Find next tag, like \\[find-tag] with prefix arg."
3620 (interactive)
3621 (let ((current-prefix-arg '(4)))
3622 (if erlang-tags-installed
3623 (call-interactively 'erlang-find-tag)
3624 (call-interactively 'find-tag))))
3627 ;; Mimics `find-tag-noselect' found in `etags.el', but uses `find-tag' to
3628 ;; be compatible with `tags.el'.
3630 ;; Handles three cases:
3631 ;; * `module:' Loop over all possible file names. Stop if a file-name
3632 ;; without extension and directory matches the module.
3634 ;; * `module:tag'
3635 ;; Emacs 19: Replace test functions with functions aware of
3636 ;; Erlang modules. Tricky because the etags system wasn't
3637 ;; built for these kind of operations...
3639 ;; Emacs 18: We loop over `find-tag' until we find a file
3640 ;; whose module matches the requested module. The
3641 ;; drawback is that a lot of files could be loaded into
3642 ;; Emacs.
3644 ;; * `tag' Just give it to `find-tag'.
3646 (defun erlang-find-tag-noselect (modtagname &optional next-p regexp-p)
3647 "Like `find-tag-noselect' but aware of Erlang modules."
3648 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3649 (or modtagname
3650 (setq modtagname (symbol-value 'last-tag)))
3651 (funcall (symbol-function 'set) 'last-tag modtagname)
3652 ;; `tags.el' uses this variable to record how M-, would
3653 ;; know where to restart a tags command.
3654 (if (boundp 'tags-loop-form)
3655 (funcall (symbol-function 'set)
3656 'tags-loop-form '(erlang-find-tag nil t)))
3657 (save-window-excursion
3658 (cond
3659 ((string-match ":$" modtagname)
3660 ;; Only the module name was given. Read all files whose file name
3661 ;; match.
3662 (let ((modname (substring modtagname 0 (match-beginning 0)))
3663 (file nil))
3664 (if (not next-p)
3665 (save-excursion
3666 (visit-tags-table-buffer)
3667 (setq erlang-tags-file-list
3668 (funcall (symbol-function 'tags-table-files)))))
3669 (while (null file)
3670 (or erlang-tags-file-list
3671 (save-excursion
3672 (if (and (featurep 'etags)
3673 (funcall
3674 (symbol-function 'visit-tags-table-buffer) 'same)
3675 (funcall
3676 (symbol-function 'visit-tags-table-buffer) t))
3677 (setq erlang-tags-file-list
3678 (funcall (symbol-function 'tags-table-files)))
3679 (error "No %stags containing %s" (if next-p "more " "")
3680 modtagname))))
3681 (if erlang-tags-file-list
3682 (let ((this-module (erlang-get-module-from-file-name
3683 (car erlang-tags-file-list))))
3684 (if (and (stringp this-module)
3685 (string= modname this-module))
3686 (setq file (car erlang-tags-file-list)))
3687 (setq erlang-tags-file-list (cdr erlang-tags-file-list)))))
3688 (set-buffer (or (get-file-buffer file)
3689 (find-file-noselect file)))))
3691 ((string-match ":" modtagname)
3692 (if (boundp 'find-tag-tag-order)
3693 ;; Method one: Add module-recognising functions to the
3694 ;; list of order functions. However, the tags system
3695 ;; from Emacs 18, and derives thereof (read: XEmacs)
3696 ;; hasn't got this feature.
3697 (progn
3698 (erlang-tags-install-module-check)
3699 (unwind-protect
3700 (funcall (symbol-function 'find-tag)
3701 modtagname next-p regexp-p)
3702 (erlang-tags-remove-module-check)))
3703 ;; Method two: Call the tags system until a file matching
3704 ;; the module is found. This could result in that many
3705 ;; files are read. (e.g. The tag "foo:file" will take a
3706 ;; while to process.)
3707 (let* ((modname (substring modtagname 0 (match-beginning 0)))
3708 (tagname (substring modtagname (match-end 0) nil))
3709 (last-tag tagname)
3710 file)
3711 (while
3712 (progn
3713 (funcall (symbol-function 'find-tag) tagname next-p regexp-p)
3714 (setq next-p t)
3715 ;; Determine the module form the file name. (The
3716 ;; alternative, to check `-module', would make this
3717 ;; code useless for non-Erlang programs.)
3718 (setq file (erlang-get-module-from-file-name buffer-file-name))
3719 (not (and (stringp file)
3720 (string= modname file))))))))
3722 (funcall (symbol-function 'find-tag) modtagname next-p regexp-p)))
3723 (current-buffer))) ; Return the new buffer.
3726 ;; Process interactive arguments for erlang-find-tag-*.
3728 ;; Negative arguments work only for `etags', not `tags'. This is not
3729 ;; a problem since negative arguments means step back into the
3730 ;; history list, a feature not implemented in `tags'.
3732 (defun erlang-tag-interactive (prompt)
3733 (condition-case nil
3734 (require 'etags)
3735 (error
3736 (require 'tags)))
3737 (if current-prefix-arg
3738 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
3741 (let* ((default (erlang-find-tag-default))
3742 (prompt (if default
3743 (format "%s(default %s) " prompt default)
3744 prompt))
3745 (spec (if (featurep 'etags)
3746 (completing-read prompt 'erlang-tags-complete-tag)
3747 (read-string prompt))))
3748 (list (if (equal spec "")
3749 (or default (error "There is no default tag"))
3750 spec)))))
3753 ;; Search tag functions which are aware of Erlang modules. The tactic
3754 ;; is to store new search functions into the local variables of the
3755 ;; TAGS buffers. The variables are restored directly after the
3756 ;; search. The situation is complicated by the fact that new TAGS
3757 ;; files can be loaded during the search.
3759 ;; This code is Emacs 19 `etags' specific.
3761 (defun erlang-tags-install-module-check ()
3762 "Install our own tag search functions."
3763 ;; Make sure our functions are installed in TAGS files loaded
3764 ;; into Emacs while searching.
3765 ;; ?? tags-table-format-hooks isn't in Emacs 21 or XEmacs etags.
3766 (setq erlang-tags-orig-format-hooks
3767 (symbol-value 'tags-table-format-hooks))
3768 (funcall (symbol-function 'set) 'tags-table-format-hooks
3769 (cons 'erlang-tags-recognize-tags-table
3770 erlang-tags-orig-format-hooks))
3771 (setq erlang-tags-buffer-list '())
3772 ;; Install our functions in the TAGS files already resident.
3773 (save-excursion
3774 (let ((files (symbol-value 'tags-table-computed-list)))
3775 (while files
3776 (if (stringp (car files))
3777 (if (get-file-buffer (car files))
3778 (progn
3779 (set-buffer (get-file-buffer (car files)))
3780 (erlang-tags-install-local))))
3781 (setq files (cdr files))))))
3784 (defun erlang-tags-install-local ()
3785 "Install our tag search functions in current buffer."
3786 (if erlang-tags-buffer-installed-p
3788 ;; Mark this buffer as "installed" and record.
3789 (set (make-local-variable 'erlang-tags-buffer-installed-p) t)
3790 (setq erlang-tags-buffer-list
3791 (cons (current-buffer) erlang-tags-buffer-list))
3793 ;; Save the original values.
3794 (set (make-local-variable 'erlang-tags-orig-tag-order)
3795 (symbol-value 'find-tag-tag-order))
3796 (set (make-local-variable 'erlang-tags-orig-regexp-tag-order)
3797 (symbol-value 'find-tag-regexp-tag-order))
3798 (set (make-local-variable 'erlang-tags-orig-search-function)
3799 (symbol-value 'find-tag-search-function))
3800 (set (make-local-variable 'erlang-tags-orig-regexp-search-function)
3801 (symbol-value 'find-tag-regexp-search-function))
3803 ;; Install our own functions.
3804 (set (make-local-variable 'find-tag-search-function)
3805 'erlang-tags-search-forward)
3806 (set (make-local-variable 'find-tag-regexp-search-function)
3807 'erlang-tags-regexp-search-forward)
3808 (set (make-local-variable 'find-tag-tag-order)
3809 '(erlang-tag-match-module-p))
3810 (set (make-local-variable 'find-tag-regexp-tag-order)
3811 '(erlang-tag-match-module-regexp-p))))
3814 (defun erlang-tags-remove-module-check ()
3815 "Remove our own tags search functions."
3816 (funcall (symbol-function 'set)
3817 'tags-table-format-hooks
3818 erlang-tags-orig-format-hooks)
3819 ;; Remove our functions from the TAGS files. (Note that
3820 ;; `tags-table-computed-list' need not be the same list as when
3821 ;; the search was started.)
3822 (save-excursion
3823 (let ((buffers erlang-tags-buffer-list))
3824 (while buffers
3825 (if (buffer-name (car buffers))
3826 (progn
3827 (set-buffer (car buffers))
3828 (erlang-tags-remove-local)))
3829 (setq buffers (cdr buffers))))))
3832 (defun erlang-tags-remove-local ()
3833 "Remove our tag search functions from current buffer."
3834 (if (null erlang-tags-buffer-installed-p)
3836 (funcall (symbol-function 'set) 'erlang-tags-buffer-installed-p nil)
3837 (funcall (symbol-function 'set)
3838 'find-tag-tag-order erlang-tags-orig-tag-order)
3839 (funcall (symbol-function 'set)
3840 'find-tag-regexp-tag-order erlang-tags-orig-regexp-tag-order)
3841 (funcall (symbol-function 'set)
3842 'find-tag-search-function erlang-tags-orig-search-function)
3843 (funcall (symbol-function 'set)
3844 'find-tag-regexp-search-function
3845 erlang-tags-orig-regexp-search-function)))
3848 (defun erlang-tags-recognize-tags-table ()
3849 "Install our functions in all loaded TAGS files.
3851 This function is added to `tags-table-format-hooks' when searching
3852 for a tag on the form `module:tag'."
3853 (if (null (funcall (symbol-function 'etags-recognize-tags-table)))
3855 (erlang-tags-install-local)
3859 (defun erlang-tags-search-forward (tag &optional bound noerror count)
3860 "Forward search function, aware of Erlang module prefix."
3861 (if (string-match ":" tag)
3862 (setq tag (substring tag (match-end 0) nil)))
3863 ;; Avoid unintended recursion.
3864 (if (eq erlang-tags-orig-search-function 'erlang-tags-search-forward)
3865 (search-forward tag bound noerror count)
3866 (funcall erlang-tags-orig-search-function tag bound noerror count)))
3869 (defun erlang-tags-regexp-search-forward (tag &optional bound noerror count)
3870 "Forward regexp search function, aware of Erlang module prefix."
3871 (if (string-match ":" tag)
3872 (setq tag (substring tag (match-end 0) nil)))
3873 (if (eq erlang-tags-orig-regexp-search-function
3874 'erlang-tags-regexp-search-forward)
3875 (re-search-forward tag bound noerror count)
3876 (funcall erlang-tags-orig-regexp-search-function
3877 tag bound noerror count)))
3880 ;; t if point is at a tag line that matches TAG, containing
3881 ;; module information. Assumes that all other order functions
3882 ;; are stored in `erlang-tags-orig-[regex]-tag-order'.
3884 (defun erlang-tag-match-module-p (tag)
3885 (erlang-tag-match-module-common-p tag erlang-tags-orig-tag-order))
3887 (defun erlang-tag-match-module-regexp-p (tag)
3888 (erlang-tag-match-module-common-p tag erlang-tags-orig-regexp-tag-order))
3890 (defun erlang-tag-match-module-common-p (tag order)
3891 (let ((mod nil)
3892 (found nil))
3893 (if (string-match ":" tag)
3894 (progn
3895 (setq mod (substring tag 0 (match-beginning 0)))
3896 (setq tag (substring tag (match-end 0) nil))))
3897 (while (and order (not found))
3898 (setq found
3899 (and (not (memq (car order)
3900 '(erlang-tag-match-module-p
3901 erlang-tag-match-module-regexp-p)))
3902 (funcall (car order) tag)))
3903 (setq order (cdr order)))
3904 (and found
3905 (or (null mod)
3906 (string= mod (erlang-get-module-from-file-name
3907 (file-of-tag)))))))
3910 ;;; Tags completion, Emacs 19 `etags' specific.
3912 ;;; The basic idea is to create a second completion table `erlang-tags-
3913 ;;; completion-table' containing all normal tags plus tags on the form
3914 ;;; `module:tag'.
3917 (defun erlang-complete-tag ()
3918 "Perform tags completion on the text around point.
3919 Completes to the set of names listed in the current tags table.
3921 Should the Erlang tags system be installed this command knows
3922 about Erlang modules."
3923 (interactive)
3924 (condition-case nil
3925 (require 'etags)
3926 (error nil))
3927 (cond ((and erlang-tags-installed
3928 (fboundp 'complete-tag)) ; Emacs 19
3929 (let ((orig-tags-complete-tag (symbol-function 'tags-complete-tag)))
3930 (fset 'tags-complete-tag
3931 (symbol-function 'erlang-tags-complete-tag))
3932 (unwind-protect
3933 (funcall (symbol-function 'complete-tag))
3934 (fset 'tags-complete-tag orig-tags-complete-tag))))
3935 ((fboundp 'complete-tag) ; Emacs 19
3936 (funcall (symbol-function 'complete-tag)))
3937 ((fboundp 'tag-complete-symbol) ; XEmacs
3938 (funcall (symbol-function 'tag-complete-symbol)))
3940 (error "This version of Emacs can't complete tags"))))
3943 ;; Based on `tags-complete-tag', but this one uses
3944 ;; `erlang-tags-completion-table' instead of `tags-completion-table'.
3946 ;; This is the entry-point called by system function `completing-read'.
3947 (defun erlang-tags-complete-tag (string predicate what)
3948 (save-excursion
3949 ;; If we need to ask for the tag table, allow that.
3950 (let ((enable-recursive-minibuffers t))
3951 (visit-tags-table-buffer))
3952 (if (eq what t)
3953 (all-completions string (erlang-tags-completion-table) predicate)
3954 (try-completion string (erlang-tags-completion-table) predicate))))
3957 ;; `tags-completion-table' calls itself recursively, make it
3958 ;; call our own wedge instead. Note that the recursive call
3959 ;; is very rare; it only occurs when a tags-file contains
3960 ;; `include'-statements.
3961 (defun erlang-tags-completion-table ()
3962 "Build completion table. Tags on the form `tag' or `module:tag'."
3963 (setq erlang-tags-orig-completion-table
3964 (symbol-function 'tags-completion-table))
3965 (fset 'tags-completion-table
3966 (symbol-function 'erlang-tags-completion-table-1))
3967 (unwind-protect
3968 (erlang-tags-completion-table-1)
3969 (fset 'tags-completion-table
3970 erlang-tags-orig-completion-table)))
3973 (defun erlang-tags-completion-table-1 ()
3974 (make-local-variable 'erlang-tags-completion-table)
3975 (or erlang-tags-completion-table
3976 (let ((tags-completion-table nil)
3977 (tags-completion-table-function
3978 'erlang-etags-tags-completion-table))
3979 (funcall erlang-tags-orig-completion-table)
3980 (setq erlang-tags-completion-table tags-completion-table))))
3983 ;; Based on `etags-tags-completion-table'. The difference is that we
3984 ;; add three symbols to the vector, the tag, module: and module:tag.
3985 ;; The module is extracted from the file name of a tag. (This one
3986 ;; only works if we are looking at an `etags' file. However, this is
3987 ;; the only format supported by Emacs, so far.)
3988 (defun erlang-etags-tags-completion-table ()
3989 (let ((table (make-vector 511 0))
3990 (file nil))
3991 (save-excursion
3992 (goto-char (point-min))
3993 ;; This monster regexp matches an etags tag line.
3994 ;; \1 is the string to match;
3995 ;; \2 is not interesting;
3996 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
3997 ;; \4 is not interesting;
3998 ;; \5 is the explicitly-specified tag name.
3999 ;; \6 is the line to start searching at;
4000 ;; \7 is the char to start searching at.
4001 (while (progn
4002 (while (and
4003 (eq (following-char) ?\f)
4004 (looking-at "\f\n\\([^,\n]*\\),.*\n"))
4005 (setq file (buffer-substring
4006 (match-beginning 1) (match-end 1)))
4007 (goto-char (match-end 0)))
4008 (re-search-forward
4010 ^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
4011 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
4012 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
4013 nil t))
4014 (let ((tag (if (match-beginning 5)
4015 ;; There is an explicit tag name.
4016 (buffer-substring (match-beginning 5) (match-end 5))
4017 ;; No explicit tag name. Best guess.
4018 (buffer-substring (match-beginning 3) (match-end 3))))
4019 (module (and file
4020 (erlang-get-module-from-file-name file))))
4021 (intern tag table)
4022 (if (stringp module)
4023 (progn
4024 (intern (concat module ":" tag) table)
4025 ;; Only the first one will be stored in the table.
4026 (intern (concat module ":") table))))))
4027 table))
4030 ;;; Prepare for other methods to run an Erlang slave process.
4033 (defvar erlang-shell-function 'inferior-erlang
4034 "Command to execute start a new Erlang shell.
4036 Change this variable to use your favorite
4037 Erlang compilation package.")
4039 (defvar erlang-shell-display-function 'inferior-erlang-run-or-select
4040 "Command to execute to display Erlang shell.
4042 Change this variable to use your favorite
4043 Erlang compilation package.")
4045 (defvar erlang-compile-function 'inferior-erlang-compile
4046 "Command to execute to compile current buffer.
4048 Change this variable to use your favorite
4049 Erlang compilation package.")
4051 (defvar erlang-compile-display-function 'inferior-erlang-run-or-select
4052 "Command to execute to view last compilation.
4054 Change this variable to use your favorite
4055 Erlang compilation package.")
4057 (defvar erlang-next-error-function 'inferior-erlang-next-error
4058 "Command to execute to go to the next error.
4060 Change this variable to use your favorite Erlang compilation
4061 package. Not used in Emacs 21.")
4064 ;;;###autoload
4065 (defun erlang-shell ()
4066 "Start a new Erlang shell.
4068 The variable `erlang-shell-function' decides which method to use,
4069 default is to start a new Erlang host. It is possible that, in the
4070 future, a new shell on an already running host will be started."
4071 (interactive)
4072 (call-interactively erlang-shell-function))
4075 ;;;###autoload (autoload 'run-erlang "erlang" "Start a new Erlang shell." t)
4077 ;; It is customary for Emacs packages to supply a function on this
4078 ;; form, even though it violates the `erlang-*' name convention.
4079 (defalias 'run-erlang 'erlang-shell)
4082 (defun erlang-shell-display ()
4083 "Display an Erlang shell, or start a new."
4084 (interactive)
4085 (call-interactively erlang-shell-display-function))
4088 ;;;###autoload
4089 (defun erlang-compile ()
4090 "Compile Erlang module in current buffer."
4091 (interactive)
4092 (call-interactively erlang-compile-function))
4095 (defun erlang-compile-display ()
4096 "Display compilation output."
4097 (interactive)
4098 (call-interactively erlang-compile-display-function))
4101 (defun erlang-next-error ()
4102 "Display next error message from the latest compilation."
4103 (interactive)
4104 (call-interactively erlang-next-error-function))
4109 ;;; Erlang Shell Mode -- Major mode used for Erlang shells.
4112 ;; This mode is designed to be implementation independent,
4113 ;; e.g. it does not assume that we are running an inferior
4114 ;; Erlang, there exists a lot of other possibilities.
4117 (defvar erlang-shell-buffer-name "*erlang*"
4118 "The name of the Erlang link shell buffer.")
4121 (defvar erlang-shell-mode-map nil
4122 "Keymap used by Erlang shells.")
4125 (defvar erlang-shell-mode-hook nil
4126 "*User functions to run when an Erlang shell is started.
4128 This hook is used to change the behaviour of Erlang mode. It is
4129 normally used by the user to personalise the programming environment.
4130 When used in a site init file, it could be used to customise Erlang
4131 mode for all users on the system.
4133 The function added to this hook is run every time a new Erlang
4134 shell is started.
4136 See also `erlang-load-hook', a hook which is run once, when Erlang
4137 mode is loaded, and `erlang-mode-hook' which is run every time a new
4138 Erlang source file is loaded into Emacs.")
4141 (defvar erlang-input-ring-file-name "~/.erlang_history"
4142 "*When non-nil, file name used to store Erlang shell history information.")
4145 (defun erlang-shell-mode ()
4146 "Major mode for interacting with an Erlang shell.
4148 We assume that we already are in Comint mode.
4150 The following special commands are available:
4151 \\{erlang-shell-mode-map}"
4152 (interactive)
4153 (setq major-mode 'erlang-shell-mode)
4154 (setq mode-name "Erlang Shell")
4155 (erlang-mode-variables)
4156 (if erlang-shell-mode-map
4158 (setq erlang-shell-mode-map (copy-keymap comint-mode-map))
4159 (erlang-shell-mode-commands erlang-shell-mode-map))
4160 (use-local-map erlang-shell-mode-map)
4161 (unless inferior-erlang-use-cmm
4162 ;; This was originally not a marker, but it needs to be, at least
4163 ;; in Emacs 21, and should be backwards-compatible. Otherwise,
4164 ;; would need to test whether compilation-parsing-end is a marker
4165 ;; after requiring `compile'.
4166 (set (make-local-variable 'compilation-parsing-end) (copy-marker 1))
4167 (set (make-local-variable 'compilation-error-list) nil)
4168 (set (make-local-variable 'compilation-old-error-list) nil))
4169 ;; Needed when compiling directly from the Erlang shell.
4170 (setq compilation-last-buffer (current-buffer))
4171 (erlang-add-compilation-alist erlang-error-regexp-alist)
4172 (setq comint-prompt-regexp "^[^>=]*> *")
4173 (setq comint-eol-on-send t)
4174 (setq comint-input-ignoredups t)
4175 (setq comint-scroll-show-maximum-output t)
4176 (setq comint-scroll-to-bottom-on-output t)
4177 ;; In Emacs 19.30, `add-hook' has got a `local' flag, use it. If
4178 ;; the call fails, just call the normal `add-hook'.
4179 (condition-case nil
4180 (progn
4181 (make-local-hook 'comint-output-filter-functions) ; obsolete after Emacs 21.3
4182 (add-hook 'comint-output-filter-functions
4183 'inferior-erlang-strip-delete nil t)
4184 (add-hook 'comint-output-filter-functions
4185 'inferior-erlang-strip-ctrl-m nil t))
4186 (error
4187 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-delete)
4188 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-ctrl-m)))
4189 ;; Some older versions of comint don't have an input ring.
4190 (if (fboundp 'comint-read-input-ring)
4191 (progn
4192 (setq comint-input-ring-file-name erlang-input-ring-file-name)
4193 (comint-read-input-ring t)
4194 (make-local-variable 'kill-buffer-hook)
4195 (add-hook 'kill-buffer-hook 'comint-write-input-ring)))
4196 ;; At least in Emacs 21, we need to be in `compilation-minor-mode'
4197 ;; for `next-error' to work. We can avoid it clobbering the shell
4198 ;; keys thus.
4199 (when inferior-erlang-use-cmm
4200 (compilation-minor-mode 1)
4201 (set (make-local-variable 'minor-mode-overriding-map-alist)
4202 `((compilation-minor-mode
4203 . ,(let ((map (make-sparse-keymap)))
4204 ;; It would be useful to put keymap properties on the
4205 ;; error lines so that we could use RET and mouse-2
4206 ;; on them directly.
4207 (when (boundp 'compilation-skip-threshold) ; new compile.el
4208 (define-key map [mouse-2] #'erlang-mouse-2-command)
4209 (define-key map "\C-m" #'erlang-RET-command))
4210 (if (boundp 'compilation-menu-map)
4211 (define-key map [menu-bar compilation]
4212 (cons "Errors" compilation-menu-map)))
4213 map)))))
4214 (run-hooks 'erlang-shell-mode-hook))
4217 (defun erlang-mouse-2-command (event)
4218 "Command bound to `mouse-2' in inferior Erlang buffer.
4219 Selects Comint or Compilation mode command as appropriate."
4220 (interactive "e")
4221 (if (save-window-excursion
4222 (save-excursion
4223 (mouse-set-point event)
4224 (consp (get-text-property (line-beginning-position) 'message))))
4225 (call-interactively (lookup-key compilation-mode-map [mouse-2]))
4226 (call-interactively (lookup-key comint-mode-map [mouse-2]))))
4228 (defun erlang-RET-command ()
4229 "Command bound to `RET' in inferior Erlang buffer.
4230 Selects Comint or Compilation mode command as appropriate."
4231 (interactive)
4232 (if (consp (get-text-property (line-beginning-position) 'message))
4233 (call-interactively (lookup-key compilation-mode-map "\C-m"))
4234 (call-interactively (lookup-key comint-mode-map "\C-m"))))
4236 (defun erlang-shell-mode-commands (map)
4237 (define-key map "\M-\t" 'erlang-complete-tag)
4238 (define-key map "\C-a" 'comint-bol) ; Normally the other way around.
4239 (define-key map "\C-c\C-a" 'beginning-of-line)
4240 (define-key map "\C-d" nil) ; Was `comint-delchar-or-maybe-eof'
4241 (define-key map "\M-\C-m" 'compile-goto-error)
4242 (unless inferior-erlang-use-cmm
4243 (define-key map "\C-x`" 'erlang-next-error)))
4246 ;;; Inferior Erlang -- Run an Erlang shell as a subprocess.
4249 (defvar inferior-erlang-display-buffer-any-frame nil
4250 "*When nil, `inferior-erlang-display-buffer' use only selected frame.
4251 When t, all frames are searched. When 'raise, the frame is raised.")
4253 (defvar inferior-erlang-shell-type 'newshell
4254 "The type of Erlang shell to use.
4256 When this variable is set to the atom `oldshell', the old shell is used.
4257 When set to `newshell' the new shell is used. Should the variable be
4258 nil, the default shell is used.
4260 This variable influence the setting of other variables.")
4262 (defvar inferior-erlang-machine "erl"
4263 "*The name of the Erlang shell.")
4265 (defvar inferior-erlang-machine-options '()
4266 "*The options used when activating the Erlang shell.
4268 This must be a list of strings.")
4270 (defvar inferior-erlang-process-name "inferior-erlang"
4271 "The name of the inferior Erlang process.")
4273 (defvar inferior-erlang-buffer-name erlang-shell-buffer-name
4274 "The name of the inferior Erlang buffer.")
4276 (defvar inferior-erlang-prompt-timeout 60
4277 "*Number of seconds before `inferior-erlang-wait-prompt' timeouts.
4279 The time specified is waited after every output made by the inferior
4280 Erlang shell. When this variable is t, we assume that we always have
4281 a prompt. When nil, we will wait forever, or until \\[keyboard-quit].")
4283 (defvar inferior-erlang-process nil
4284 "Process of last invoked inferior Erlang, or nil.")
4286 (defvar inferior-erlang-buffer nil
4287 "Buffer of last invoked inferior Erlang, or nil.")
4289 ;;;###autoload
4290 (defun inferior-erlang ()
4291 "Run an inferior Erlang.
4293 This is just like running Erlang in a normal shell, except that
4294 an Emacs buffer is used for input and output.
4295 \\<comint-mode-map>
4296 The command line history can be accessed with \\[comint-previous-input] and \\[comint-next-input].
4297 The history is saved between sessions.
4299 Entry to this mode calls the functions in the variables
4300 `comint-mode-hook' and `erlang-shell-mode-hook' with no arguments.
4302 The following commands imitate the usual Unix interrupt and
4303 editing control characters:
4304 \\{erlang-shell-mode-map}"
4305 (interactive)
4306 (require 'comint)
4307 (let ((opts inferior-erlang-machine-options))
4308 (cond ((eq inferior-erlang-shell-type 'oldshell)
4309 (setq opts (cons "-oldshell" opts)))
4310 ((eq inferior-erlang-shell-type 'newshell)
4311 (setq opts (append '("-newshell" "-env" "TERM" "vt100") opts))))
4312 (setq inferior-erlang-buffer
4313 (apply 'make-comint
4314 inferior-erlang-process-name inferior-erlang-machine
4315 nil opts)))
4316 (setq inferior-erlang-process
4317 (get-buffer-process inferior-erlang-buffer))
4318 (process-kill-without-query inferior-erlang-process)
4319 (switch-to-buffer inferior-erlang-buffer)
4320 (if (and (not (eq system-type 'windows-nt))
4321 (eq inferior-erlang-shell-type 'newshell))
4322 (setq comint-process-echoes t))
4323 ;; `rename-buffer' takes only one argument in Emacs 18.
4324 (condition-case nil
4325 (rename-buffer inferior-erlang-buffer-name t)
4326 (error (rename-buffer inferior-erlang-buffer-name)))
4327 (erlang-shell-mode))
4330 (defun inferior-erlang-run-or-select ()
4331 "Switch to an inferior Erlang buffer, possibly starting new process."
4332 (interactive)
4333 (if (null (inferior-erlang-running-p))
4334 (inferior-erlang)
4335 (inferior-erlang-display-buffer t)))
4338 (defun inferior-erlang-display-buffer (&optional select)
4339 "Make the inferior Erlang process visible.
4340 The window is returned.
4342 Should `inferior-erlang-display-buffer-any-frame' be nil the buffer is
4343 displayed in the current frame. Should it be non-nil, and the buffer
4344 already is visible in any other frame, no new window will be created.
4345 Should it be the atom 'raise, the frame containing the window will
4346 be raised.
4348 Should the optional argument SELECT be non-nil, the window is
4349 selected. Should the window be in another frame, that frame is raised.
4351 Note, should the mouse pointer be places outside the raised frame, that
4352 frame will become deselected before the next command."
4353 (interactive)
4354 (or (inferior-erlang-running-p)
4355 (error "No inferior Erlang process is running"))
4356 (let ((win (inferior-erlang-window
4357 inferior-erlang-display-buffer-any-frame))
4358 (frames-p (fboundp 'selected-frame)))
4359 (if (null win)
4360 (let ((old-win (selected-window)))
4361 (save-excursion
4362 (switch-to-buffer-other-window inferior-erlang-buffer)
4363 (setq win (selected-window)))
4364 (select-window old-win))
4365 (if (and window-system
4366 frames-p
4367 (or select
4368 (eq inferior-erlang-display-buffer-any-frame 'raise))
4369 (not (eq (selected-frame) (window-frame win))))
4370 (raise-frame (window-frame win))))
4371 (if select
4372 (select-window win))
4373 (sit-for 0)
4374 win))
4377 (defun inferior-erlang-running-p ()
4378 "Non-nil when an inferior Erlang is running."
4379 (and inferior-erlang-process
4380 (memq (process-status inferior-erlang-process) '(run open))
4381 inferior-erlang-buffer
4382 (buffer-name inferior-erlang-buffer)))
4385 (defun inferior-erlang-window (&optional all-frames)
4386 "Return the window containing the inferior Erlang, or nil."
4387 (and (inferior-erlang-running-p)
4388 (if (and all-frames (>= erlang-emacs-major-version 19))
4389 (get-buffer-window inferior-erlang-buffer t)
4390 (get-buffer-window inferior-erlang-buffer))))
4393 (defun inferior-erlang-wait-prompt ()
4394 "Wait until the inferior Erlang shell prompt appears."
4395 (if (eq inferior-erlang-prompt-timeout t)
4397 (or (inferior-erlang-running-p)
4398 (error "No inferior Erlang shell is running"))
4399 (save-excursion
4400 (set-buffer inferior-erlang-buffer)
4401 (let ((msg nil))
4402 (while (save-excursion
4403 (goto-char (process-mark inferior-erlang-process))
4404 (forward-line 0)
4405 (not (looking-at comint-prompt-regexp)))
4406 (if msg
4408 (setq msg t)
4409 (message "Waiting for Erlang shell prompt (press C-g to abort)."))
4410 (or (accept-process-output inferior-erlang-process
4411 inferior-erlang-prompt-timeout)
4412 (error "No Erlang shell prompt before timeout")))
4413 (if msg (message ""))))))
4415 (autoload 'comint-send-input "comint")
4417 (defun inferior-erlang-send-command (cmd &optional hist)
4418 "Send command CMD to the inferior Erlang.
4420 The contents of the current command line (if any) will
4421 be placed at the next prompt.
4423 If optional second argument is non-nil the command is inserted into
4424 the history list.
4426 Return the position after the newly inserted command."
4427 (or (inferior-erlang-running-p)
4428 (error "No inferior Erlang process is running"))
4429 (let ((old-buffer (current-buffer))
4430 (insert-point (marker-position (process-mark inferior-erlang-process)))
4431 (insert-length (if comint-process-echoes
4433 (1+ (length cmd)))))
4434 (set-buffer inferior-erlang-buffer)
4435 (goto-char insert-point)
4436 (insert cmd)
4437 ;; Strange things happened if `comint-eol-on-send' is declared
4438 ;; in the `let' expression above, but setq:d here. The
4439 ;; `set-buffer' statement obviously makes the buffer local
4440 ;; instance of `comint-eol-on-send' shadow this one.
4441 ;; I'm considering this a bug in Elisp.
4443 ;; This was previously cautioned against in the Lisp manual. It
4444 ;; has been sorted out in Emacs 21. -- fx
4445 (let ((comint-eol-on-send nil)
4446 (comint-input-filter (if hist comint-input-filter 'ignore)))
4447 (comint-send-input))
4448 ;; Adjust all windows whose points are incorrect.
4449 (if (null comint-process-echoes)
4450 (walk-windows
4451 (function
4452 (lambda (window)
4453 (if (and (eq (window-buffer window) inferior-erlang-buffer)
4454 (= (window-point window) insert-point))
4455 (set-window-point window
4456 (+ insert-point insert-length)))))
4457 nil t))
4458 (set-buffer old-buffer)
4459 (+ insert-point insert-length)))
4462 (defun inferior-erlang-strip-delete (&optional s)
4463 "Remove `^H' (delete) and the characters it was supposed to remove."
4464 (interactive)
4465 (if (and (boundp 'comint-last-input-end)
4466 (boundp 'comint-last-output-start))
4467 (save-excursion
4468 (goto-char
4469 (if (interactive-p)
4470 (symbol-value 'comint-last-input-end)
4471 (symbol-value 'comint-last-output-start)))
4472 (while (progn (skip-chars-forward "^\C-h")
4473 (not (eq (point) (point-max))))
4474 (delete-char 1)
4475 (or (bolp)
4476 (backward-delete-char 1))))))
4479 ;; Basically `comint-strip-ctrl-m', with a few extra checks.
4480 (defun inferior-erlang-strip-ctrl-m (&optional string)
4481 "Strip trailing `^M' characters from the current output group."
4482 (interactive)
4483 (if (and (boundp 'comint-last-input-end)
4484 (boundp 'comint-last-output-start))
4485 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
4486 (save-excursion
4487 (goto-char
4488 (if (interactive-p)
4489 (symbol-value 'comint-last-input-end)
4490 (symbol-value 'comint-last-output-start)))
4491 (while (re-search-forward "\r+$" pmark t)
4492 (replace-match "" t t))))))
4495 (defun inferior-erlang-compile ()
4496 "Compile the file in the current buffer.
4498 Should Erlang return `{error, nofile}' it could not load the object
4499 module after completing the compilation. This is due to a bug in the
4500 compile command `c' when using the option `outdir'.
4502 There exists two workarounds for this bug:
4504 1) Place the directory in the Erlang load path.
4506 2) Set the Emacs variable `erlang-compile-use-outdir' to nil.
4507 To do so, place the following line in your `~/.emacs'-file:
4508 (setq erlang-compile-use-outdir nil)"
4509 (interactive)
4510 (save-some-buffers)
4511 (or (inferior-erlang-running-p)
4512 (save-excursion
4513 (inferior-erlang)))
4514 (or (inferior-erlang-running-p)
4515 (error "Error starting inferior Erlang shell"))
4516 (let ((dir (file-name-directory (buffer-file-name)))
4517 ;;; (file (file-name-nondirectory (buffer-file-name)))
4518 (noext (substring (buffer-file-name) 0 -4))
4519 ;; Hopefully, noone else will ever use these...
4520 (tmpvar "Tmp7236")
4521 (tmpvar2 "Tmp8742")
4522 end)
4523 (inferior-erlang-display-buffer)
4524 (inferior-erlang-wait-prompt)
4525 (setq end (inferior-erlang-send-command
4526 (if erlang-compile-use-outdir
4527 (format "c(\"%s\", [{outdir, \"%s\"}])." noext dir)
4528 (format
4529 (concat
4530 "f(%s), {ok, %s} = file:get_cwd(), "
4531 "file:set_cwd(\"%s\"), "
4532 "%s = c(\"%s\"), file:set_cwd(%s), f(%s), %s.")
4533 tmpvar2 tmpvar
4535 tmpvar2 noext tmpvar tmpvar tmpvar2))
4536 nil))
4537 (inferior-erlang-wait-prompt)
4538 (save-excursion
4539 (set-buffer inferior-erlang-buffer)
4540 (setq compilation-error-list nil)
4541 (set-marker compilation-parsing-end end))
4542 (setq compilation-last-buffer inferior-erlang-buffer)))
4545 ;; `next-error' only accepts buffers with major mode `compilation-mode'
4546 ;; or with the minor mode `compilation-minor-mode' activated.
4547 ;; (To activate the minor mode is out of the question, since it will
4548 ;; ruin the inferior Erlang keymap.)
4549 ;; This is done differently in Emacs 21.
4550 (defun inferior-erlang-next-error (&optional argp)
4551 "Just like `next-error'.
4552 Capable of finding error messages in an inferior Erlang buffer."
4553 (interactive "P")
4554 (let ((done nil)
4555 (buf (and (boundp 'compilation-last-buffer)
4556 compilation-last-buffer)))
4557 (if (and (bufferp buf)
4558 (save-excursion
4559 (set-buffer buf)
4560 (and (eq major-mode 'erlang-shell-mode)
4561 (setq major-mode 'compilation-mode))))
4562 (unwind-protect
4563 (progn
4564 (setq done t)
4565 (next-error argp))
4566 (save-excursion
4567 (set-buffer buf)
4568 (setq major-mode 'erlang-shell-mode))))
4569 (or done
4570 (next-error argp))))
4573 (defun inferior-erlang-change-directory (&optional dir)
4574 "Make the inferior Erlang change directory.
4575 The default is to go to the directory of the current buffer."
4576 (interactive)
4577 (or dir (setq dir (file-name-directory (buffer-file-name))))
4578 (or (inferior-erlang-running-p)
4579 (error "No inferior Erlang is running"))
4580 (inferior-erlang-display-buffer)
4581 (inferior-erlang-wait-prompt)
4582 (inferior-erlang-send-command (format "cd('%s')." dir) nil))
4584 (defun erlang-align-arrows (start end)
4585 "Align arrows (\"->\") in function clauses from START to END.
4586 When called interactively, aligns arrows after function clauses inside
4587 the region.
4589 With a prefix argument, aligns all arrows, not just those in function
4590 clauses.
4592 Example:
4594 sum(L) -> sum(L, 0).
4595 sum([H|T], Sum) -> sum(T, Sum + H);
4596 sum([], Sum) -> Sum.
4598 becomes:
4600 sum(L) -> sum(L, 0).
4601 sum([H|T], Sum) -> sum(T, Sum + H);
4602 sum([], Sum) -> Sum."
4603 (interactive "r")
4604 (save-excursion
4605 (let (;; regexp for matching arrows. without a prefix argument,
4606 ;; the regexp matches function heads. With a prefix, it
4607 ;; matches any arrow.
4608 (re (if current-prefix-arg
4609 "^.*\\(\\)->"
4610 (eval-when-compile
4611 (concat "^" erlang-atom-regexp ".*\\(\\)->"))))
4612 ;; part of regexp matching directly before the arrow
4613 (arrow-match-pos (if current-prefix-arg
4615 (1+ erlang-atom-regexp-matches)))
4616 ;; accumulator for positions where arrows are found, ordered
4617 ;; by buffer position (from greatest to smallest)
4618 (arrow-positions '())
4619 ;; accumulator for longest distance from start of line to arrow
4620 (most-indent 0)
4621 ;; marker to track the end of the region we're aligning
4622 (end-marker (progn (goto-char end)
4623 (point-marker))))
4624 ;; Pass 1: Find the arrow positions, adjust the whitespace
4625 ;; before each arrow to one space, and find the greatest
4626 ;; indentation level.
4627 (goto-char start)
4628 (while (re-search-forward re end-marker t)
4629 (goto-char (match-beginning arrow-match-pos))
4630 (just-one-space) ; adjust whitespace
4631 (setq arrow-positions (cons (point) arrow-positions))
4632 (setq most-indent (max most-indent (erlang-column-number))))
4633 (set-marker end-marker nil) ; free the marker
4634 ;; Pass 2: Insert extra padding so that all arrow indentation is
4635 ;; equal. This is done last-to-first by buffer position, so that
4636 ;; inserting spaces before one arrow doesn't change the
4637 ;; positions of the next ones.
4638 (mapcar (lambda (arrow-pos)
4639 (goto-char arrow-pos)
4640 (let* ((pad (- most-indent (erlang-column-number))))
4641 (when (> pad 0)
4642 (insert-char ?\ pad))))
4643 arrow-positions))))
4645 (defun erlang-column-number ()
4646 "Return the column number of the current position in the buffer.
4647 Tab characters are counted by their visual width."
4648 (string-width (buffer-substring (line-beginning-position) (point))))
4650 (defun erlang-current-defun ()
4651 "`add-log-current-defun-function' for Erlang."
4652 (save-excursion
4653 (erlang-beginning-of-function)
4654 (if (looking-at "[a-z0-9_]+")
4655 (match-string 0))))
4657 ;; Aliases for backward compatibility with older versions of Erlang Mode.
4659 ;; Unfortuantely, older versions of Emacs doesn't have `defalias' and
4660 ;; `make-obsolete' so we have to define our own `obsolete' function.
4662 (defun erlang-obsolete (sym newdef)
4663 "Make the obsolete function SYM refer to the defined function NEWDEF.
4665 Simplified version of a combination `defalias' and `make-obsolete',
4666 it assumes that NEWDEF is loaded."
4667 (defalias sym (symbol-function newdef))
4668 (if (fboundp 'make-obsolete)
4669 (make-obsolete sym newdef)))
4672 (erlang-obsolete 'calculate-erlang-indent 'erlang-calculate-indent)
4673 (erlang-obsolete 'calculate-erlang-stack-indent
4674 'erlang-calculate-stack-indent)
4675 (erlang-obsolete 'at-erlang-keyword 'erlang-at-keyword)
4676 (erlang-obsolete 'at-erlang-operator 'erlang-at-operator)
4677 (erlang-obsolete 'beginning-of-erlang-clause 'erlang-beginning-of-clause)
4678 (erlang-obsolete 'end-of-erlang-clause 'erlang-end-of-clause)
4679 (erlang-obsolete 'mark-erlang-clause 'erlang-mark-clause)
4680 (erlang-obsolete 'beginning-of-erlang-function 'erlang-beginning-of-function)
4681 (erlang-obsolete 'end-of-erlang-function 'erlang-end-of-function)
4682 (erlang-obsolete 'mark-erlang-function 'erlang-mark-function)
4683 (erlang-obsolete 'pass-over-erlang-clause 'erlang-pass-over-function)
4684 (erlang-obsolete 'name-of-erlang-function 'erlang-name-of-function)
4687 ;; Fixme: shouldn't redefine `set-visited-file-name' anyhow -- see above.
4688 (defconst erlang-unload-hook
4689 (list (lambda ()
4690 (defalias 'set-visited-file-name
4691 'erlang-orig-set-visited-file-name)
4692 (when (featurep 'advice)
4693 (ad-unadvise 'Man-notify-when-ready)
4694 (ad-unadvise 'set-visited-file-name)))))
4696 ;; The end...
4698 (provide 'erlang)
4700 (run-hooks 'erlang-load-hook)
4702 ;; Local variables:
4703 ;; coding: iso-8859-1
4704 ;; End:
4706 ;;; erlang.el ends here