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