Update ChangeLog, AUTHORS.
[erlware-mode.git] / erlang.el
blob98aa015c48e5e685905a38b1db3f9d28153b48e4
1 ;; erlang.el --- Major modes for editing and running Erlang
3 ;; Copyright (C) 1995-1998,2000 Ericsson Telecom AB
4 ;; Copyright (C) 2004 Free Software Foundation, Inc.
5 ;; Author: Anders Lindgren
6 ;; Version: 0.1.19
7 ;; Keywords: erlang, languages, processes
8 ;; Date: 2000-09-11
10 ;; The contents of this file are subject to the Erlang Public License,
11 ;; Version 1.1, (the "License"); you may not use this file except in
12 ;; compliance with the License. You should have received a copy of the
13 ;; Erlang Public License along with this software. If not, it can be
14 ;; retrieved via the world wide web at http://www.erlang.org/.
16 ;; Software distributed under the License is distributed on an "AS IS"
17 ;; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
18 ;; the License for the specific language governing rights and limitations
19 ;; under the License.
21 ;; The Initial Developer of the Original Code is Ericsson Utvecklings AB.
22 ;; All Rights Reserved.
24 ;; Lars Thorsén's modifications of 2000-06-07 included.
26 ;; The original version of this package was written by Robert Virding.
28 ;; Most skeletons have been written at Ericsson Telecom by
29 ;; magnus@erix.ericsson.se and janne@erix.ericsson.se
31 ;; The Erlware version of the mode has updated skeletons, syntax
32 ;; highlighting fixes, and indentation fixes. The latest Erlware mode
33 ;; can be obtained here:
35 ;; http://code.google.com/p/erlware-mode/downloads/list
37 ;;; Commentary:
39 ;; Introduction:
40 ;; ------------
42 ;; This package provides support for the programming language Erlang.
43 ;; The package provides an editing mode with lots of bells and
44 ;; whistles, compilation support, and it makes it possible for the
45 ;; user to start Erlang shells that run inside Emacs.
47 ;; See the Erlang distribution for full documentation of this package.
49 ;; Installation:
50 ;; ------------
52 ;; Place this file in Emacs load path, byte-compile it, and add the
53 ;; following line to the appropriate init file:
55 ;; (require 'erlang-start)
57 ;; The full documentation contains much more extensive description of
58 ;; the installation procedure.
60 ;; Reporting Bugs:
61 ;; --------------
63 ;; Please send bug reports to the following email address:
64 ;; erlang-bugs@erlang.org
65 ;; or if you have a patch suggestion to:
66 ;; erlang-patches@erlang.org
67 ;; Please state as exactly as possible:
68 ;; - Version number of Erlang Mode (see the menu), Emacs, Erlang,
69 ;; and of any other relevant software.
70 ;; - What the expected result was.
71 ;; - What you did, preferably in a repeatable step-by-step form.
72 ;; - A description of the unexpected result.
73 ;; - Relevant pieces of Erlang code causing the problem.
74 ;; - Personal Emacs customisations, if any.
76 ;; Should the Emacs generate an error, please set the Emacs variable
77 ;; `debug-on-error' to `t'. Repeat the error and enclose the debug
78 ;; information in your bug-report.
80 ;; To set the variable you can use the following command:
81 ;; M-x set-variable RET debug-on-error RET t RET
82 ;;; Code:
84 ;; Variables:
86 (defconst erlang-version "0.1.19"
87 "The version number of Erlware Erlang mode.")
89 (defvar erlang-man-root-dir nil
90 "The directory where the Erlang manual pages are installed.
91 The name should not contain the trailing slash.
93 Should this variable be nil, no manual pages will show up in the
94 Erlang mode menu.")
96 (defvar erlang-menu-items '(erlang-menu-base-items
97 erlang-menu-skel-items
98 erlang-menu-shell-items
99 erlang-menu-compile-items
100 erlang-menu-man-items
101 erlang-menu-personal-items
102 erlang-menu-version-items)
103 "*List of menu item list to combine to create Erlang mode menu.
105 External programs which temporarily add menu items to the Erlang mode
106 menu may use this variable. Please use the function `add-hook' to add
107 items.
109 Please call the function `erlang-menu-init' after every change to this
110 variable.")
112 (defvar erlang-menu-base-items
113 '(("Indent"
114 (("Indent Line" erlang-indent-command)
115 ("Indent Region " erlang-indent-region
116 (if erlang-xemacs-p (mark) mark-active))
117 ("Indent Clause" erlang-indent-clause)
118 ("Indent Function" erlang-indent-function)
119 ("Indent Buffer" erlang-indent-current-buffer)))
120 ("Edit"
121 (("Fill Comment" erlang-fill-paragraph)
122 ("Comment Region" comment-region
123 (if erlang-xemacs-p (mark) mark-active))
124 ("Uncomment Region" erlang-uncomment-region
125 (if erlang-xemacs-p (mark) mark-active))
127 ("Beginning of Function" erlang-beginning-of-function)
128 ("End of Function" erlang-end-of-function)
129 ("Mark Function" erlang-mark-function)
131 ("Beginning of Clause" erlang-beginning-of-clause)
132 ("End of Clause" erlang-end-of-clause)
133 ("Mark Clause" erlang-mark-clause)
135 ("New Clause" erlang-generate-new-clause)
136 ("Clone Arguments" erlang-clone-arguments)
138 ("Align Arrows" erlang-align-arrows)))
139 ("Syntax Highlighting"
140 (("Level 3" erlang-font-lock-level-3)
141 ("Level 2" erlang-font-lock-level-2)
142 ("Level 1" erlang-font-lock-level-1)
143 ("Off" erlang-font-lock-level-0)))
144 ("TAGS"
145 (("Find Tag" find-tag)
146 ("Find Next Tag" erlang-find-next-tag)
147 ("Complete Word" erlang-complete-tag)
148 ("Tags Apropos" tags-apropos)
149 ("Search Files" tags-search))))
150 "Description of menu used in Erlang mode.
152 This variable must be a list. The elements are either nil representing
153 a horizontal line or a list with two or three elements. The first is
154 the name of the menu item, the second is the function to call, or a
155 submenu, on the same same form as ITEMS. The third optional argument
156 is an expression which is evaluated every time the menu is displayed.
157 Should the expression evaluate to nil the menu item is ghosted.
159 Example:
160 '((\"Func1\" function-one)
161 (\"SubItem\"
162 ((\"Yellow\" function-yellow)
163 (\"Blue\" function-blue)))
165 (\"Region Function\" spook-function midnight-variable))
167 Call the function `erlang-menu-init' after modifying this variable.")
169 (defvar erlang-menu-shell-items
170 '(nil
171 ("Shell"
172 (("Start New Shell" erlang-shell)
173 ("Display Shell" erlang-shell-display))))
174 "Description of the Shell menu used by Erlang mode.
176 Please see the documentation of `erlang-menu-base-items'.")
178 (defvar erlang-menu-compile-items
179 '(("Compile"
180 (("Compile Buffer" erlang-compile)
181 ("Display Result" erlang-compile-display)
182 ("Next Error" erlang-next-error))))
183 "Description of the Compile menu used by Erlang mode.
185 Please see the documentation of `erlang-menu-base-items'.")
187 (defvar erlang-menu-version-items
188 '(nil
189 ("Version" erlang-version))
190 "Description of the version menu used in Erlang mode.")
192 (defvar erlang-menu-personal-items nil
193 "Description of personal menu items used in Erlang mode.
195 Please see the variable `erlang-menu-base-items' for a description
196 of the format.")
198 (defvar erlang-menu-man-items nil
199 "The menu containing man pages.
201 The format of the menu should be compatible with `erlang-menu-base-items'.
202 This variable is added to the list of Erlang menus stored in
203 `erlang-menu-items'.")
205 (defvar erlang-menu-skel-items '()
206 "Description of the menu containing the skeleton entries.
207 The menu is in the form described by the variable `erlang-menu-base-items'.")
209 (defvar erlang-mode-hook nil
210 "*Functions to run when Erlang mode is activated.
212 This hook is used to change the behaviour of Erlang mode. It is
213 normally used by the user to personalise the programming environment.
214 When used in a site init file, it could be used to customise Erlang
215 mode for all users on the system.
217 The functions added to this hook are run every time Erlang mode is
218 started. See also `erlang-load-hook', a hook which is run once,
219 when Erlang mode is loaded into Emacs, and `erlang-shell-mode-hook'
220 which is run every time a new inferior Erlang shell is started.
222 To use a hook, create an Emacs lisp function to perform your actions
223 and add the function to the hook by calling `add-hook'.
225 The following example binds the key sequence C-c C-c to the command
226 `erlang-compile' (normally bound to C-c C-k). The example also
227 activates Font Lock mode to fontify the buffer and adds a menu
228 containing all functions defined in the current buffer.
230 To use the example, copy the following lines to your `~/.emacs' file:
232 (add-hook 'erlang-mode-hook 'my-erlang-mode-hook)
234 (defun my-erlang-mode-hook ()
235 (local-set-key \"\\C-c\\C-c\" 'erlang-compile)
236 (if window-system
237 (progn
238 (setq font-lock-maximum-decoration t)
239 (font-lock-mode 1)))
240 (if (and window-system (fboundp 'imenu-add-to-menubar))
241 (imenu-add-to-menubar \"Imenu\")))")
243 (defvar erlang-load-hook nil
244 "*Functions to run when Erlang mode is loaded.
246 This hook is used to change the behaviour of Erlang mode. It is
247 normally used by the user to personalise the programming environment.
248 When used in a site init file, it could be used to customize Erlang
249 mode for all users on the system.
251 The difference between this hook and `erlang-mode-hook' and
252 `erlang-shell-mode-hook' is that the functions in this hook
253 is only called once, when the Erlang mode is loaded into Emacs
254 the first time.
256 Natural actions for the functions added to this hook are actions which
257 only should be performed once, and actions which should be performed
258 before starting Erlang mode. For example, a number of variables are
259 used by Erlang mode before `erlang-mode-hook' is run.
261 The following example sets the variable `erlang-man-root-dir' so that
262 the manual pages can be retrieved (note that you must set the value of
263 `erlang-man-root-dir' to match the location of the Erlang man pages
264 on your system):
266 (setq erlang-man-root-dir \"/usr/local/erlang\")")
268 (defvar erlang-new-file-hook nil
269 "Functions to run when a new Erlang source file is being edited.
271 A useful function is `tempo-template-erlang-normal-header'.
272 \(This function only exists when the `tempo' package is available.)")
274 (defvar erlang-check-module-name 'ask
275 "*Non-nil means check that module name and file name agrees when saving.
277 If the value of this variable is the atom `ask', the user is
278 prompted. If the value is t the source is silently changed.")
280 (defvar erlang-electric-commands
281 '(erlang-electric-comma
282 erlang-electric-semicolon
283 erlang-electric-newline
284 erlang-electric-gt)
285 "*List of activated electric commands.
287 The list should contain the electric commands which should be active.
288 Currently, the available electric commands are:
289 `erlang-electric-comma'
290 `erlang-electric-semicolon'
291 `erlang-electric-gt'
292 `erlang-electric-newline'
294 Should the variable be bound to t, all electric commands
295 are activated.
297 To deactivate all electric commands, set this variable to nil.")
299 (defvar erlang-electric-newline-inhibit t
300 "*Set to non-nil to inhibit newline after electric command.
302 This is useful since a lot of people press return after executing an
303 electric command.
305 In order to work, the command must also be in the
306 list `erlang-electric-newline-inhibit-list'.
308 Note that commands in this list are required to set the variable
309 `erlang-electric-newline-inhibit' to nil when the newline shouldn't be
310 inhibited.")
312 (defvar erlang-electric-newline-inhibit-list
313 '(erlang-electric-semicolon
314 erlang-electric-comma
315 erlang-electric-gt)
316 "*Commands which can inhibit the next newline.")
318 (defvar erlang-oldstyle-comment-indent nil
319 "*if non-nil, use old-style indent rules.
321 Old-style is to indent comments starting with `%' far right,
322 those starting with `%%' with same indent as code, and thos
323 starting with `%%%' far left.
325 New-style is to indent comments starting with `%' with same
326 indent as code, and those starting with at least two `%' far
327 left.")
329 (defvar erlang-electric-semicolon-insert-blank-lines nil
330 "*Number of blank lines inserted before header, or nil.
332 This variable controls the behaviour of `erlang-electric-semicolon'
333 when a new function header is generated. When nil, no blank line is
334 inserted between the current line and the new header. When bound to a
335 number it represents the number of blank lines which should be
336 inserted.")
338 (defvar erlang-electric-semicolon-criteria
339 '(erlang-next-lines-empty-p
340 erlang-at-keyword-end-p
341 erlang-at-end-of-function-p)
342 "*List of functions controlling `erlang-electric-semicolon'.
343 The functions in this list are called, in order, whenever a semicolon
344 is typed. Each function in the list is called with no arguments,
345 and should return one of the following values:
347 nil -- no determination made, continue checking
348 'stop -- do not create prototype for next line
349 (anything else) -- insert prototype, and stop checking
351 If every function in the list is called with no determination made,
352 then no prototype is inserted.
354 The test is performed by the function `erlang-test-criteria-list'.")
356 (defvar erlang-electric-comma-criteria
357 '(erlang-stop-when-inside-argument-list
358 erlang-stop-when-at-guard
359 erlang-next-lines-empty-p
360 erlang-at-keyword-end-p
361 erlang-at-end-of-clause-p
362 erlang-at-end-of-function-p)
363 "*List of functions controlling `erlang-electric-comma'.
364 The functions in this list are called, in order, whenever a comma
365 is typed. Each function in the list is called with no arguments,
366 and should return one of the following values:
368 nil -- no determination made, continue checking
369 'stop -- do not create prototype for next line
370 (anything else) -- insert prototype, and stop checking
372 If every function in the list is called with no determination made,
373 then no prototype is inserted.
375 The test is performed by the function `erlang-test-criteria-list'.")
377 (defvar erlang-electric-arrow-criteria
378 '(erlang-next-lines-empty-p
379 erlang-at-end-of-function-p)
380 "*List of functions controlling the arrow aspect of `erlang-electric-gt'.
381 The functions in this list are called, in order, whenever a `>'
382 is typed. Each function in the list is called with no arguments,
383 and should return one of the following values:
385 nil -- no determination made, continue checking
386 'stop -- do not create prototype for next line
387 (anything else) -- insert prototype, and stop checking
389 If every function in the list is called with no determination made,
390 then no prototype is inserted.
392 The test is performed by the function `erlang-test-criteria-list'.")
394 (defvar erlang-electric-newline-criteria
395 '(t)
396 "*List of functions controlling `erlang-electric-newline'.
398 The electric newline commands indents the next line. Should the
399 current line begin with a comment the comment start is copied to
400 the newly created line.
402 The functions in this list are called, in order, whenever a comma
403 is typed. Each function in the list is called with no arguments,
404 and should return one of the following values:
406 nil -- no determination made, continue checking
407 'stop -- do not create prototype for next line
408 (anything else) -- trigger the electric command.
410 If every function in the list is called with no determination made,
411 then no prototype is inserted. Should the atom t be a member of the
412 list, it is treated as a function triggering the electric command.
414 The test is performed by the function `erlang-test-criteria-list'.")
416 (defvar erlang-next-lines-empty-threshold 2
417 "*Number of blank lines required to activate an electric command.
419 Actually, this value controls the behaviour of the function
420 `erlang-next-lines-empty-p' which normally is a member of the
421 criteria lists controlling the electric commands. (Please see
422 the variables `erlang-electric-semicolon-criteria' and
423 `erlang-electric-comma-criteria'.)
425 The variable is bound to a threshold value, a number, representing the
426 number of lines which must be empty.
428 Setting this variable to zero, electric commands will always be
429 triggered by `erlang-next-lines-empty-p', unless inhibited by other
430 rules.
432 Should this variable be nil, `erlang-next-lines-empty-p' will never
433 trigger an electric command. The same effect would be reached if the
434 function `erlang-next-lines-empty-p' would be removed from the criteria
435 lists.
437 Note that even if `erlang-next-lines-empty-p' should not trigger an
438 electric command, other functions in the criteria list could.")
440 (defvar erlang-new-clause-with-arguments nil
441 "*Non-nil means that the arguments are cloned when a clause is generated.
443 A new function header can be generated by calls to the function
444 `erlang-generate-new-clause' and by use of the electric semicolon.")
446 (defvar erlang-compile-use-outdir t
447 "*When nil, go to the directory containing source file when compiling.
449 This is a workaround for a bug in the `outdir' option of compile. If the
450 outdir is not in the current load path, Erlang doesn't load the object
451 module after it has been compiled.
453 To activate the workaround, place the following in your `~/.emacs' file:
454 (setq erlang-compile-use-outdir nil)")
456 (defvar erlang-compile-outdir ""
457 "*This value is concat'ed to outdir while compiling.
459 If you have your erl files in /some/path/src and desire the beams to
460 be placed in ../ebin set in your '~/.emacs' file:
461 (setq erlang-compile-outdir \"../ebin\"")
463 (defvar erlang-indent-level 4
464 "*Indentation of Erlang calls/clauses within blocks.")
466 (defvar erlang-indent-guard 2
467 "*Indentation of Erlang guards.")
469 (defvar erlang-argument-indent 2
470 "*Indentation of the first argument in a function call.
471 When nil, indent to the column after the `(' of the
472 function.")
474 (defvar erlang-tab-always-indent t
475 "*Non-nil means TAB in Erlang mode should always re-indent the current line,
476 regardless of where in the line point is when the TAB command is used.")
478 (defvar erlang-error-regexp-alist
479 '(("^\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" . (1 2)))
480 "*Patterns for matching Erlang errors.")
482 (defvar erlang-man-inhibit (eq system-type 'windows-nt)
483 "Inhibit the creation of the Erlang Manual Pages menu.
485 The Windows distribution of Erlang does not include man pages, hence
486 there is no attempt to create the menu.")
488 (defvar erlang-man-dirs
489 '(("Man - Commands" "/man/man1" t)
490 ("Man - Modules" "/man/man3" t)
491 ("Man - Files" "/man/man4" t)
492 ("Man - Applications" "/man/man6" t))
493 "*The man directories displayed in the Erlang menu.
495 Each item in the list should be a list with three elements, the first
496 the name of the menu, the second the directory, and the last a flag.
497 Should the flag the nil, the directory is absolute, should it be non-nil
498 the directory is relative to the variable `erlang-man-root-dir'.")
500 (defvar erlang-man-max-menu-size 20
501 "*The maximum number of menu items in one menu allowed.")
503 (defvar erlang-man-display-function 'erlang-man-display
504 "*Function used to display man page.
506 The function is called with one argument, the name of the file
507 containing the man page. Use this variable when the default
508 function, `erlang-man-display', does not work on your system.")
510 (eval-and-compile
511 (defconst erlang-atom-quoted-regexp
512 "'\\(?:[^\\']?\\(?:\\\\'\\)?\\)*'"
513 "Regexp describing a single-quoted atom"))
515 (eval-and-compile
516 (defconst erlang-atom-regular-regexp
517 "\\(?:[a-z][A-Za-z0-9_]*\\)"
518 "Regexp describing a regular (non-quoted) atom"))
520 (eval-and-compile
521 (defconst erlang-atom-regexp
522 (concat"\\(" erlang-atom-quoted-regexp "\\|"
523 erlang-atom-regular-regexp "\\)")
524 "Regexp describing an Erlang atom."))
526 (defconst erlang-atom-regexp-matches 1
527 "Number of regexp parenthesis pairs in `erlang-atom-regexp'.
529 This is used to determine parenthesis matches in complex regexps which
530 contains `erlang-atom-regexp'.")
532 (defconst erlang-variable-regexp "\\([A-Z_][a-zA-Z0-9_]*\\)"
533 "Regexp which should match an Erlang variable.
535 The regexp must be surrounded with a pair of regexp parentheses.")
536 (defconst erlang-variable-regexp-matches 1
537 "Number of regexp parenthesis pairs in `erlang-variable-regexp'.
539 This is used to determine matches in complex regexps which contains
540 `erlang-variable-regexp'.")
542 (defconst erlang-bif-regexp
543 (concat
544 "\\("
545 "a\\(bs\\|live\\|pply\\|tom_to_list\\)\\|"
546 "binary_to_\\(list\\|term\\)\\|"
547 "concat_binary\\|"
548 "d\\(ate\\|isconnect_node\\)\\|"
549 "e\\(lement\\|rase\\|xit\\)\\|"
550 "floa\\(t\\|t_to_list\\)\\|"
551 "g\\(arbage_collect\\|et\\(\\|_keys\\)\\|roup_leader\\)\\|"
552 "h\\(alt\\|d\\)\\|"
553 "i\\(nte\\(ger_to_list\\|rnal_bif\\)\\|s_alive\\)\\|"
554 "l\\(ength\\|i\\(nk\\|st_to_\\(atom\\|binary\\|float\\|integer"
555 "\\|pid\\|tuple\\)\\)\\)\\|"
556 "make_ref\\|no\\(de\\(\\|_\\(link\\|unlink\\)\\|s\\)\\|talive\\)\\|"
557 "open_port\\|"
558 "p\\(id_to_list\\|rocess\\(_\\(flag\\|info\\)\\|es\\)\\|ut\\)\\|"
559 "r\\(egister\\(\\|ed\\)\\|ound\\)\\|"
560 "s\\(e\\(lf\\|telement\\)\\|ize"
561 "\\|p\\(awn\\(\\|_link\\|_monitor\\)\\|lit_binary\\)\\|tatistics\\)\\|"
562 "t\\(erm_to_binary\\|hrow\\|ime\\|l\\|r\\(ace\\|unc\\)\\|uple_to_list\\)\\|"
563 "un\\(link\\|"
564 "register\\)\\|"
565 "whereis"
566 "\\)")
567 "Regexp matching an Erlang built-in function (BIF).")
569 (defvar erlang-defun-prompt-regexp (concat "^" erlang-atom-regexp "\\s *(")
570 "Regexp which should match beginning of a clause.")
572 (defvar erlang-file-name-extension-regexp "\\.[eh]rl$"
573 "*Regexp which should match an Erlang file name.
575 This regexp is used when an Erlang module name is extracted from the
576 name of an Erlang source file.
578 The regexp should only match the section of the file name which should
579 be excluded from the module name.
581 To match all files set this variable to \"\\\\(\\\\..*\\\\|\\\\)$\".
582 The matches all except the extension. This is useful if the Erlang
583 tags system should interpret tags on the form `module:tag' for
584 files written in other languages than Erlang.")
586 (defvar erlang-mode-map
587 (let ((map (make-sparse-keymap)))
588 (unless (boundp 'indent-line-function)
589 (define-key map "\t" 'erlang-indent-command))
590 (define-key map ";" 'erlang-electric-semicolon)
591 (define-key map "," 'erlang-electric-comma)
592 (define-key map "<" 'erlang-electric-lt)
593 (define-key map ">" 'erlang-electric-gt)
594 (define-key map "\C-m" 'erlang-electric-newline)
595 (if (not (boundp 'delete-key-deletes-forward))
596 (define-key map "\177" 'backward-delete-char-untabify)
597 (define-key map [backspace] 'backward-delete-char-untabify))
598 (define-key map "\M-q" 'erlang-fill-paragraph)
599 (unless (boundp 'beginning-of-defun-function)
600 (define-key map "\M-\C-a" 'erlang-beginning-of-function)
601 (define-key map "\M-\C-e" 'erlang-end-of-function)
602 (define-key map "\M-\C-h" 'erlang-mark-function))
603 (define-key map "\M-\t" 'erlang-complete-tag)
604 (define-key map "\C-c\M-\t" 'tempo-complete-tag)
605 (define-key map "\M-+" 'erlang-find-next-tag)
606 (define-key map "\C-c\M-a" 'erlang-beginning-of-clause)
607 (define-key map "\C-c\M-b" 'tempo-backward-mark)
608 (define-key map "\C-c\M-e" 'erlang-end-of-clause)
609 (define-key map "\C-c\M-f" 'tempo-forward-mark)
610 (define-key map "\C-c\M-h" 'erlang-mark-clause)
611 (define-key map "\C-c\C-c" 'comment-region)
612 (define-key map "\C-c\C-j" 'erlang-generate-new-clause)
613 (define-key map "\C-c\C-k" 'erlang-compile)
614 (define-key map "\C-c\C-l" 'erlang-compile-display)
615 (define-key map "\C-c\C-s" 'erlang-show-syntactic-information)
616 (define-key map "\C-c\C-q" 'erlang-indent-function)
617 (define-key map "\C-c\C-u" 'erlang-uncomment-region)
618 (define-key map "\C-c\C-y" 'erlang-clone-arguments)
619 (define-key map "\C-c\C-a" 'erlang-align-arrows)
620 (define-key map "\C-c\C-z" 'erlang-shell-display)
621 ;; This is only needed for Emacs < 21.
622 ;; (unless inferior-erlang-use-cmm
623 ;; (define-key map "\C-x`" 'erlang-next-error))
624 map)
625 "*Keymap used in Erlang mode.")
626 (defvar erlang-mode-abbrev-table nil
627 "Abbrev table in use in Erlang-mode buffers.")
628 (defvar erlang-mode-syntax-table nil
629 "Syntax table in use in Erlang-mode buffers.")
631 (defconst erlang-emacs-major-version
632 (if (boundp 'emacs-major-version)
633 emacs-major-version
634 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version)
635 (string-to-int (substring emacs-version
636 (match-beginning 1) (match-end 1))))
637 "Major version number of Emacs.")
639 (defconst erlang-emacs-minor-version
640 (if (boundp 'emacs-minor-version)
641 emacs-minor-version
642 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version)
643 (string-to-int (substring emacs-version
644 (match-beginning 2) (match-end 2))))
645 "Minor version number of Emacs.")
647 (defconst erlang-xemacs-p (string-match "Lucid\\|XEmacs" emacs-version)
648 "Non-nil when running under XEmacs or Lucid Emacs.")
650 (defvar erlang-xemacs-popup-menu '("Erlang Mode Commands" . nil)
651 "Common popup menu for all buffers in Erlang mode.
653 This variable is destructively modified every time the Erlang menu
654 is modified. The effect is that all changes take effect in all
655 buffers in Erlang mode, just like under GNU Emacs.
657 Never EVER set this variable!")
659 (defconst inferior-erlang-use-cmm (boundp 'minor-mode-overriding-map-alist)
660 "Non-nil means use `compilation-minor-mode' in Erlang shell.")
662 ;; Tempo skeleton templates:
663 (load "erlang-skels")
665 ;; Sinan commands:
666 (load "erlang-sinan")
668 ;; Flymake support
669 ;; M-x flymake-mode to start
670 (load "erlang-flymake")
672 ;; Font-lock variables
674 (defvar erlang-font-lock-modern-p
675 (cond ((>= erlang-emacs-major-version 20) t)
676 (erlang-xemacs-p (>= erlang-emacs-minor-version 14))
677 ((= erlang-emacs-major-version 19) (>= erlang-emacs-minor-version 29))
678 (t nil))
679 "Non-nil when this version of Emacs uses a modern version of Font Lock.
681 This is determined by checking the version of Emacs used, the actual
682 font-lock code is not loaded.")
685 ;; The next few variables define different Erlang font-lock patterns.
686 ;; They could be appended to form a custom font-lock appearance.
688 ;; The function `erlang-font-lock-set-face' could be used to change
689 ;; the face of a pattern.
691 ;; Note that Erlang strings and atoms are highlighted with using
692 ;; syntactic analysis.
694 (defvar erlang-font-lock-keywords-func
695 (list
696 (list (concat "^" erlang-atom-regexp "\\s-*(")
697 1 'font-lock-function-name-face t))
698 "Font lock keyword highlighting a function header.")
700 (defvar erlang-font-lock-keywords-int-func-call
701 (list
702 (list (concat "\\<" erlang-atom-regexp "\\s-*(")
703 1 'font-lock-type-face))
704 "Font lock keyword highlighting an internal function call.")
706 (defvar erlang-font-lock-keywords-ext-func-call
707 (list
708 (list (concat "\\<" erlang-atom-regexp "\\s-*:\\s-*"
709 erlang-atom-regexp "\\s-*(")
710 1 'font-lock-type-face)
711 (list (concat "\\<" erlang-atom-regexp "\\s-*:\\s-*"
712 erlang-atom-regexp "\\s-*(")
713 2 'font-lock-type-face))
714 "Font lock keyword highlighting an external function call.")
716 (defvar erlang-font-lock-keywords-ext-bif
717 (list
718 (list (concat "\\<\\(erlang\\)\\s-*:\\s-*\\<" erlang-bif-regexp "\\s-*(")
719 1 'font-lock-builtin-face)
720 (list (concat "\\<\\(erlang\\)\\s-*:\\s-*\\<" erlang-bif-regexp "\\s-*(")
721 2 'font-lock-builtin-face))
722 "Font lock keyword highlighting built in functions.")
724 (defvar erlang-font-lock-keywords-int-bif
725 (list
726 (list (concat "\\<" erlang-bif-regexp "\\s-*(")
727 1 'font-lock-builtin-face))
728 "Font lock keyword highlighting built in functions.")
730 (defvar erlang-font-lock-keywords-fn
731 (list
732 (list (concat "\\(" erlang-atom-regexp "/[0-9]+\\)")
733 1 'font-lock-function-name-face))
734 "Font lock keyword highlighting a F/N fun descriptor.")
736 (defvar erlang-font-lock-keywords-plusplus
737 (list
738 (list (concat "\\(\\+\\+\\)")
739 1 'font-lock-warning-face))
740 "Font lock keyword highlighting the `++' operator.")
742 (defvar erlang-font-lock-keywords-dollar
743 (list
744 (list "\\(\\$\\([^\\]\\|\\\\\\([^0-7^\n]\\|[0-7]+\\|\\^[a-zA-Z]\\)\\)\\)"
745 1 'font-lock-string-face))
746 "Font lock keyword highlighting numbers in ASCII form (e.g. $A).")
748 (defvar erlang-font-lock-keywords-lc
749 (list
750 (list "\\(<-\\)" 1 'font-lock-keyword-face)
751 (list "\\(||\\)" 1 'font-lock-keyword-face))
752 "Font lock keyword highlighting list comprehension operators.")
754 (defvar erlang-font-lock-keywords-keywords
755 (list
756 (list (concat "\\<\\(a\\(fter\\|ndalso\\)\\|begin\\|c\\(atch\\|ase\\)"
757 "\\|end\\|fun\\|if\\|o\\(f\\|relse\\)\\|receive\\|try\\|when"
758 "\\|query\\)\\([^a-zA-Z0-9_]\\|$\\)")
759 1 'font-lock-keyword-face))
760 "Font lock keyword highlighting Erlang keywords.")
762 (defvar erlang-font-lock-keywords-attr
763 (list
764 (list (concat "^\\(-" erlang-atom-regexp "\\)\\(\\s-\\|\\.\\|(\\)")
765 1 'font-lock-preprocessor-face))
766 "Font lock keyword highlighting attributes.")
768 (defvar erlang-font-lock-keywords-quotes
769 (list
770 (list "`\\([-+a-zA-Z0-9_:*][-+a-zA-Z0-9_:*]+\\)'"
771 1 'font-lock-keyword-face t))
772 "Font lock keyword highlighting words in single quotes in comments.
774 This is not the highlighting of Erlang strings and atoms, which
775 are highlighted by syntactic analysis.")
777 ;; Note: The deprecated guard `float' collides with the bif `float'.
778 (defvar erlang-font-lock-keywords-guards
779 (list
780 (list
781 (concat "[^:]\\<\\("
782 "\\(is_\\)?\\(atom\\|boolean\\|function\\|binary\\|constant"
783 "\\|integer\\|list\\|number\\|p\\(id\\|ort\\)\\|"
784 "re\\(ference\\|cord\\)\\|tuple"
785 "\\)\\)\\s *(")
787 (if erlang-font-lock-modern-p
788 'font-lock-builtin-face
789 'font-lock-keyword-face)))
790 "Font lock keyword highlighting guards.")
792 (defvar erlang-font-lock-keywords-macros
793 (list
794 (list (concat "?\\s *\\(" erlang-atom-regexp
795 "\\|" erlang-variable-regexp "\\)\\>")
796 1 'font-lock-preprocessor-face)
797 (list (concat "^-\\(define\\|ifn?def\\)\\s *(\\s *\\(" erlang-atom-regexp
798 "\\|" erlang-variable-regexp "\\)\\>")
799 2 'font-lock-preprocessor-face))
800 "Font lock keyword highlighting macros.
801 This must be placed in front of `erlang-font-lock-keywords-vars'.")
803 (defvar erlang-font-lock-keywords-records
804 (list
805 (list (concat "#\\s *" erlang-atom-regexp "\\>")
806 1 'font-lock-preprocessor-face)
807 ;; Don't highlight numerical constants.
808 (list "\\<[0-9][0-9]?#\\([0-9a-fA_F]+\\)\\>"
809 1 nil t)
810 (list (concat "^-record(\\s *" erlang-atom-regexp "\\>")
811 1 'font-lock-preprocessor-face))
812 "Font lock keyword highlighting Erlang records.
813 This must be placed in front of `erlang-font-lock-keywords-vars'.")
815 (defvar erlang-font-lock-keywords-vars
816 (list
817 (list (concat "\\<" erlang-variable-regexp "\\>")
818 1 (if erlang-font-lock-modern-p
819 'font-lock-variable-name-face
820 'font-lock-type-face)))
821 "Font lock keyword highlighting Erlang variables.
822 Must be preceded by `erlang-font-lock-keywords-macros' and `-records'
823 to work properly.")
825 (defvar erlang-font-lock-keywords-atom-regular
826 (list
827 (list (concat "\\<\\(" erlang-atom-regular-regexp "\\)\\>")
828 1 'font-lock-constant-face))
829 "Font lock keyword highlighting Erlang atoms.")
831 (defvar erlang-font-lock-keywords-atom-quoted
832 (list
833 (list (concat "\\<\\(" erlang-atom-quoted-regexp "\\)\\>")
834 1 'font-lock-constant-face))
835 "Font lock keyword highlighting Erlang atoms.")
837 (defvar erlang-font-lock-keywords-1
838 (append erlang-font-lock-keywords-func
839 erlang-font-lock-keywords-keywords)
840 ;; DocStringOrig: erlang-font-lock-keywords
841 "Font-lock keywords used by Erlang Mode.
843 There exists three levels of Font Lock keywords for Erlang:
844 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
845 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
846 `erlang-font-lock-keywords-3' - Variables, macros and records.
848 To use a specific level, please set the variable
849 `font-lock-maximum-decoration' to the appropriate level. Note that the
850 variable must be set before Erlang mode is activated.
852 Example:
853 (setq font-lock-maximum-decoration 2)")
855 (defvar erlang-font-lock-keywords-2
856 (append erlang-font-lock-keywords-1
857 erlang-font-lock-keywords-attr
858 erlang-font-lock-keywords-quotes
859 erlang-font-lock-keywords-guards)
860 ;; DocStringCopy: erlang-font-lock-keywords
861 "Font-lock keywords used by Erlang Mode.
863 There exists three levels of Font Lock keywords for Erlang:
864 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
865 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
866 `erlang-font-lock-keywords-3' - Variables, macros and records.
868 To use a specific level, please set the variable
869 `font-lock-maximum-decoration' to the appropriate level. Note that the
870 variable must be set before Erlang mode is activated.
872 Example:
873 (setq font-lock-maximum-decoration 2)")
876 (defvar erlang-font-lock-keywords-3
877 (append erlang-font-lock-keywords-2
878 erlang-font-lock-keywords-macros
879 erlang-font-lock-keywords-records
880 erlang-font-lock-keywords-ext-bif
881 erlang-font-lock-keywords-ext-func-call
882 erlang-font-lock-keywords-int-bif
883 erlang-font-lock-keywords-int-func-call
884 erlang-font-lock-keywords-fn
885 erlang-font-lock-keywords-plusplus
886 erlang-font-lock-keywords-lc
887 erlang-font-lock-keywords-atom-quoted
888 erlang-font-lock-keywords-dollar
889 erlang-font-lock-keywords-atom-regular
890 erlang-font-lock-keywords-vars
892 ;; DocStringCopy: erlang-font-lock-keywords
893 "Font-lock keywords used by Erlang Mode.
895 There exists three levels of Font Lock keywords for Erlang:
896 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
897 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
898 `erlang-font-lock-keywords-3' - Variables, macros and records.
900 To use a specific level, please set the variable
901 `font-lock-maximum-decoration' to the appropriate level. Note that the
902 variable must be set before Erlang mode is activated.
904 Example:
905 (setq font-lock-maximum-decoration 2)")
908 (defvar erlang-font-lock-keywords erlang-font-lock-keywords-3
909 ;; DocStringCopy: erlang-font-lock-keywords
910 "Font-lock keywords used by Erlang Mode.
912 There exists three levels of Font Lock keywords for Erlang:
913 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
914 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
915 `erlang-font-lock-keywords-3' - Variables, macros and records.
917 To use a specific level, please set the variable
918 `font-lock-maximum-decoration' to the appropriate level. Note that the
919 variable must be set before Erlang mode is activated.
921 Example:
922 (setq font-lock-maximum-decoration 2)")
925 (defvar erlang-font-lock-syntax-table nil
926 "Syntax table used by Font Lock mode.
928 The difference between this and the standard Erlang Mode
929 syntax table is that `_' is treated as part of words by
930 this syntax table.
932 Unfortunately, XEmacs hasn't got support for a special Font
933 Lock syntax table. The effect is that `apply' in the atom
934 `foo_apply' will be highlighted as a bif.")
937 ;;; Avoid errors while compiling this file.
939 ;; `eval-when-compile' is not defined in Emacs 18. We define it as a
940 ;; no-op.
941 ;; (or (fboundp 'eval-when-compile)
942 ;; (defmacro eval-when-compile (&rest rest) nil))
944 ;; ;; These umm...functions are new in Emacs 20. And, yes, until version
945 ;; ;; 19.27 Emacs backquotes were this ugly.
947 ;; (or (fboundp 'unless)
948 ;; (defmacro unless (condition &rest body)
949 ;; "(unless CONDITION BODY...): If CONDITION is false, do BODY, else return nil."
950 ;; (` (if (, condition)
951 ;; nil
952 ;; (,@ body)))))
954 ;; (or (fboundp 'when)
955 ;; (defmacro when (condition &rest body)
956 ;; "(when CONDITION BODY...): If CONDITION is true, do BODY, else return nil."
957 ;; (` (if (, condition)
958 ;; (progn (,@ body))
959 ;; nil))))
961 ;; (or (fboundp 'char-before)
962 ;; (defmacro char-before (&optional pos)
963 ;; "Return the character in the current buffer just before POS."
964 ;; (` (char-after (1- (or (, pos) (point)))))))
966 (eval-when-compile
967 (if (or (featurep 'bytecomp)
968 (featurep 'byte-compile))
969 (progn
970 (cond ((string-match "Lucid\\|XEmacs" emacs-version)
971 (put 'comment-indent-hook 'byte-obsolete-variable nil)
972 ;; Do not warn for unused variables
973 ;; when compiling under XEmacs.
974 (setq byte-compile-warnings
975 '(free-vars unresolved callargs redefine))))
976 (require 'comint)
977 (require 'compile))))
980 (defun erlang-version ()
981 "Return the current version of Erlang mode."
982 (interactive)
983 (if (interactive-p)
984 (message "Erlware Erlang mode version %s" erlang-version))
985 erlang-version)
988 ;;;###autoload
989 (defun erlang-mode ()
990 "Major mode for editing Erlang source files in Emacs.
991 It knows about syntax and comment, it can indent code, it is capable
992 of fontifying the source file, the TAGS commands are aware of Erlang
993 modules, and the Erlang man pages can be accessed.
995 Should this module, \"erlang.el\", be installed properly, Erlang mode
996 is activated whenever an Erlang source or header file is loaded into
997 Emacs. To indicate this, the mode line should contain the word
998 \"Erlang\".
1000 The main feature of Erlang mode is indentation, press TAB and the
1001 current line will be indented correctly.
1003 Comments starting with one `%' are indented with the same indentation
1004 as code. Comments starting with at least two `%':s are indented to
1005 the first column.
1007 However, Erlang mode contains much more, this is a list of the most
1008 useful commands:
1009 TAB - Indent the line.
1010 C-c C-q - Indent current function.
1011 M-; - Create a comment at the end of the line.
1012 M-q - Fill a comment, i.e. wrap lines so that they (hopefully)
1013 will look better.
1014 M-a - Goto the beginning of an Erlang clause.
1015 M-C-a - Ditto for function.
1016 M-e - Goto the end of an Erlang clause.
1017 M-C-e - Ditto for function.
1018 M-h - Mark current Erlang clause.
1019 M-C-h - Ditto for function.
1020 C-c C-z - Start, or switch to, an inferior Erlang shell.
1021 C-c C-k - Compile current file.
1022 C-x ` - Next error.
1023 , - Electric comma.
1024 ; - Electric semicolon.
1026 Erlang mode check the name of the file against the module name when
1027 saving, whenever a mismatch occurs Erlang mode offers to modify the
1028 source.
1030 The variable `erlang-electric-commands' controls the electric
1031 commands. To deactivate all of them, set it to nil.
1033 There exists a large number of commands and variables in the Erlang
1034 module. Please press `M-x apropos RET erlang RET' to see a complete
1035 list. Press `C-h f name-of-function RET' and `C-h v name-of-variable
1036 RET'to see the full description of functions and variables,
1037 respectively.
1039 On entry to this mode the contents of the hook `erlang-mode-hook' is
1040 executed.
1042 Please see the beginning of the file `erlang.el' for more information
1043 and examples of hooks.
1045 Other commands:
1046 \\{erlang-mode-map}"
1047 (interactive)
1048 (kill-all-local-variables)
1049 (setq major-mode 'erlang-mode)
1050 (setq mode-name "Erlang")
1051 (erlang-syntax-table-init)
1052 (use-local-map erlang-mode-map)
1053 (erlang-electric-init)
1054 (erlang-menu-init)
1055 (erlang-mode-variables)
1056 (erlang-check-module-name-init)
1057 (erlang-add-compilation-alist erlang-error-regexp-alist)
1058 (erlang-man-init)
1059 (erlang-tags-init)
1060 (erlang-font-lock-init)
1061 (erlang-skel-init)
1062 (run-hooks 'erlang-mode-hook)
1063 (if (zerop (buffer-size))
1064 (run-hooks 'erlang-new-file-hook))
1065 ;; Doesn't exist in Emacs v21.4; required by Emacs v23.
1066 (if (boundp 'after-change-major-mode-hook)
1067 (run-hooks 'after-change-major-mode-hook)))
1069 (defvar erlang-dollar-is-escape t
1070 "If non-nil, a dollar sign escapes everything.
1071 There is (to the best of this author's knowledge) no solution
1072 that fits all code. When this is set to t, a string ending with
1073 $ (e.g. \"foo$\") will be incorrectly highlighted (though
1074 \"foo\\$\" works); when nil, the character double-quote
1075 \(i.e. $\") will be incorrectly highlighted (though $\\\" works).
1076 Pick your poison.
1078 Changing this variable after you've opened an Erlang file doesn't
1079 do anything; use the function `erlang-toggle-dollar-is-escape'.")
1081 (defun erlang-toggle-dollar-is-escape ()
1082 "Toggle syntax class of the dollar sign in Erlang mode.
1083 See `erlang-dollar-is-escape' for details."
1084 (interactive)
1085 (setq erlang-dollar-is-escape (not erlang-dollar-is-escape))
1086 (setq erlang-mode-syntax-table nil)
1087 ;; change in all erlang mode buffers
1088 (dolist (buffer (buffer-list))
1089 (with-current-buffer buffer
1090 (when (eq major-mode 'erlang-mode)
1091 (erlang-mode)))))
1093 (defun erlang-syntax-table-init ()
1094 (if (null erlang-mode-syntax-table)
1095 (let ((table (make-syntax-table)))
1096 (modify-syntax-entry ?\n ">" table)
1097 (modify-syntax-entry ?\" "\"" table)
1098 (modify-syntax-entry ?# "." table)
1099 (if erlang-dollar-is-escape
1100 (modify-syntax-entry ?$ "\\" table)
1101 (modify-syntax-entry ?$ "'" table))
1102 (modify-syntax-entry ?% "<" table)
1103 (modify-syntax-entry ?& "." table)
1104 (modify-syntax-entry ?\' "w" table)
1105 (modify-syntax-entry ?* "." table)
1106 (modify-syntax-entry ?+ "." table)
1107 (modify-syntax-entry ?- "." table)
1108 (modify-syntax-entry ?/ "." table)
1109 (modify-syntax-entry ?: "." table)
1110 (modify-syntax-entry ?< "." table)
1111 (modify-syntax-entry ?= "." table)
1112 (modify-syntax-entry ?> "." table)
1113 (modify-syntax-entry ?\\ "\\" table)
1114 (modify-syntax-entry ?_ "_" table)
1115 (modify-syntax-entry ?| "." table)
1117 (setq erlang-mode-syntax-table table)))
1119 (set-syntax-table erlang-mode-syntax-table))
1122 (defun erlang-electric-init ()
1123 ;; Set up electric character functions to work with
1124 ;; delsel/pending-del mode. Also, set up text properties for bit
1125 ;; syntax handling.
1126 (mapcar #'(lambda (cmd)
1127 (put cmd 'delete-selection t) ;for delsel (Emacs)
1128 (put cmd 'pending-delete t)) ;for pending-del (XEmacs)
1129 '(erlang-electric-semicolon
1130 erlang-electric-comma
1131 erlang-electric-gt))
1133 (put 'bitsyntax-open-outer 'syntax-table '(4 . ?>))
1134 (put 'bitsyntax-open-outer 'rear-nonsticky '(category))
1135 (put 'bitsyntax-open-inner 'rear-nonsticky '(category))
1136 (put 'bitsyntax-close-inner 'rear-nonsticky '(category))
1137 (put 'bitsyntax-close-outer 'syntax-table '(5 . ?<))
1138 (put 'bitsyntax-close-outer 'rear-nonsticky '(category))
1139 (setq parse-sexp-lookup-properties 't))
1142 (defun erlang-mode-variables ()
1143 (or erlang-mode-abbrev-table
1144 (define-abbrev-table 'erlang-mode-abbrev-table ()))
1145 (setq local-abbrev-table erlang-mode-abbrev-table)
1146 (make-local-variable 'paragraph-start)
1147 (setq paragraph-start (concat "^$\\|" page-delimiter))
1148 (make-local-variable 'paragraph-separate)
1149 (setq paragraph-separate paragraph-start)
1150 (make-local-variable 'paragraph-ignore-fill-prefix)
1151 (setq paragraph-ignore-fill-prefix t)
1152 (make-local-variable 'require-final-newline)
1153 (setq require-final-newline t)
1154 (make-local-variable 'defun-prompt-regexp)
1155 (setq defun-prompt-regexp erlang-defun-prompt-regexp)
1156 (make-local-variable 'comment-start)
1157 (setq comment-start "%")
1158 (make-local-variable 'comment-start-skip)
1159 (setq comment-start-skip "%+\\s *")
1160 (make-local-variable 'indent-line-function)
1161 (setq indent-line-function 'erlang-indent-command)
1162 (make-local-variable 'indent-region-function)
1163 (setq indent-region-function 'erlang-indent-region)
1164 (set (make-local-variable 'comment-indent-function) 'erlang-comment-indent)
1165 (if (<= erlang-emacs-major-version 18)
1166 (set (make-local-variable 'comment-indent-hook) 'erlang-comment-indent))
1167 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1168 (set (make-local-variable 'dabbrev-case-fold-search) nil)
1169 (set (make-local-variable 'imenu-prev-index-position-function)
1170 'erlang-beginning-of-function)
1171 (set (make-local-variable 'imenu-extract-index-name-function)
1172 'erlang-get-function-name-and-arity)
1173 (set (make-local-variable 'tempo-match-finder)
1174 "[^-a-zA-Z0-9_]\\([-a-zA-Z0-9_]*\\)\\=")
1175 (set (make-local-variable 'beginning-of-defun-function)
1176 'erlang-beginning-of-function)
1177 (set (make-local-variable 'end-of-defun-function) 'erlang-end-of-function)
1178 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil)
1179 (set (make-local-variable 'fill-paragraph-function) 'erlang-fill-paragraph)
1180 (set (make-local-variable 'comment-add)
1181 (if erlang-oldstyle-comment-indent 1 0))
1182 (set (make-local-variable 'outline-regexp) "[[:lower:]0-9_]+ *(.*) *-> *$")
1183 (set (make-local-variable 'outline-level) (lambda () 1))
1184 (set (make-local-variable 'add-log-current-defun-function)
1185 'erlang-current-defun))
1188 ;; Compilation.
1190 ;; The following code is compatible with the standard package `compilation',
1191 ;; making it possible to go to errors using `erlang-next-error' (or just
1192 ;; `next-error' in Emacs 21).
1194 ;; The normal `compile' command works of course. For best result, please
1195 ;; execute `make' with the `-w' flag.
1197 ;; Please see the variables named `compiling-..' above.
1199 (defun erlang-add-compilation-alist (alist)
1200 (require 'compile)
1201 (cond ((boundp 'compilation-error-regexp-alist) ; Emacs 19
1202 (while alist
1203 (or (assoc (car (car alist)) compilation-error-regexp-alist)
1204 (setq compilation-error-regexp-alist
1205 (cons (car alist) compilation-error-regexp-alist)))
1206 (setq alist (cdr alist))))
1207 ((boundp 'compilation-error-regexp)
1208 ;; Emacs 18, Only one regexp is allowed.
1209 (funcall (symbol-function 'set)
1210 'compilation-error-regexp (car (car alist))))))
1212 (defun erlang-font-lock-init ()
1213 "Initialize Font Lock for Erlang mode."
1214 (or erlang-font-lock-syntax-table
1215 (setq erlang-font-lock-syntax-table
1216 (let ((table (copy-syntax-table erlang-mode-syntax-table)))
1217 (modify-syntax-entry ?_ "w" table)
1218 table)))
1219 (set (make-local-variable 'font-lock-syntax-table)
1220 erlang-font-lock-syntax-table)
1221 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1222 'erlang-beginning-of-clause)
1223 (make-local-variable 'font-lock-keywords)
1224 (let ((level (cond ((boundp 'font-lock-maximum-decoration)
1225 (symbol-value 'font-lock-maximum-decoration))
1226 ((boundp 'font-lock-use-maximal-decoration)
1227 (symbol-value 'font-lock-use-maximal-decoration))
1228 (t nil))))
1229 (if (consp level)
1230 (setq level (cdr-safe (or (assq 'erlang-mode level)
1231 (assq t level)))))
1232 ;; `level' can here be:
1233 ;; A number - The fontification level
1234 ;; nil - Use the default
1235 ;; t - Use maximum
1236 (cond ((eq level nil)
1237 (set 'font-lock-keywords erlang-font-lock-keywords))
1238 ((eq level 1)
1239 (set 'font-lock-keywords erlang-font-lock-keywords-1))
1240 ((eq level 2)
1241 (set 'font-lock-keywords erlang-font-lock-keywords-2))
1243 (set 'font-lock-keywords erlang-font-lock-keywords-3))))
1245 ;; Modern font-locks can handle the above much more elegantly:
1246 (set (make-local-variable 'font-lock-defaults)
1247 '((erlang-font-lock-keywords erlang-font-lock-keywords-1
1248 erlang-font-lock-keywords-2 erlang-font-lock-keywords-3)
1249 nil nil ((?_ . "w")) erlang-beginning-of-clause
1250 (font-lock-mark-block-function . erlang-mark-clause))))
1254 ;; Useful when defining your own keywords.
1255 (defun erlang-font-lock-set-face (ks &rest faces)
1256 "Replace the face components in a list of keywords.
1258 The first argument, KS, is a list of keywords. The rest of the
1259 arguments are expressions to replace the face information with. The
1260 first expression replaces the face of the first keyword, the second
1261 expression the second keyword etc.
1263 Should an expression be nil, the face of the corresponding keyword is
1264 not changed.
1266 Should fewer expressions than keywords be given, the last expression
1267 is used for all remaining keywords.
1269 Normally, the expressions are just atoms representing the new face.
1270 They could however be more complex, returning different faces in
1271 different situations.
1273 This function only handles keywords with elements on the forms:
1274 (REGEXP NUMBER FACE)
1275 (REGEXP NUMBER FACE OVERWRITE)
1277 This could be used when defining your own special font-lock setup, e.g:
1279 \(setq my-font-lock-keywords
1280 (append erlang-font-lock-keywords-func
1281 erlang-font-lock-keywords-dollar
1282 (erlang-font-lock-set-face
1283 erlang-font-lock-keywords-macros 'my-neon-green-face)
1284 (erlang-font-lock-set-face
1285 erlang-font-lock-keywords-lc 'my-deep-red 'my-light-red)
1286 erlang-font-lock-keywords-attr))
1288 For a more elaborate example, please see the beginning of the file
1289 `erlang.el'."
1290 (let ((res '()))
1291 (while ks
1292 (let* ((regexp (car (car ks)))
1293 (number (car (cdr (car ks))))
1294 (new-face (if (and faces (car faces))
1295 (car faces)
1296 (car (cdr (cdr (car ks))))))
1297 (overwrite (car (cdr (cdr (cdr (car ks))))))
1298 (new-keyword (list regexp number new-face)))
1299 (if overwrite (nconc new-keyword (list overwrite)))
1300 (setq res (cons new-keyword res))
1301 (setq ks (cdr ks))
1302 (if (and faces (cdr faces))
1303 (setq faces (cdr faces)))))
1304 (nreverse res)))
1307 (defun erlang-font-lock-level-0 ()
1308 ;; DocStringOrig: font-cmd
1309 "Unfontify current buffer."
1310 (interactive)
1311 (font-lock-mode 0))
1314 (defun erlang-font-lock-level-1 ()
1315 ;; DocStringCopy: font-cmd
1316 "Fontify current buffer at level 1.
1317 This highlights function headers, reserved keywords, strings and comments."
1318 (interactive)
1319 (require 'font-lock)
1320 (set 'font-lock-keywords erlang-font-lock-keywords-1)
1321 (font-lock-mode 1)
1322 (funcall (symbol-function 'font-lock-fontify-buffer)))
1325 (defun erlang-font-lock-level-2 ()
1326 ;; DocStringCopy: font-cmd
1327 "Fontify current buffer at level 2.
1328 This highlights level 1 features (see `erlang-font-lock-level-1')
1329 plus bifs, guards and `single quotes'."
1330 (interactive)
1331 (require 'font-lock)
1332 (set 'font-lock-keywords erlang-font-lock-keywords-2)
1333 (font-lock-mode 1)
1334 (funcall (symbol-function 'font-lock-fontify-buffer)))
1337 (defun erlang-font-lock-level-3 ()
1338 ;; DocStringCopy: font-cmd
1339 "Fontify current buffer at level 3.
1340 This highlights level 2 features (see `erlang-font-lock-level-2')
1341 plus variables, macros and records."
1342 (interactive)
1343 (require 'font-lock)
1344 (set 'font-lock-keywords erlang-font-lock-keywords-3)
1345 (font-lock-mode 1)
1346 (funcall (symbol-function 'font-lock-fontify-buffer)))
1349 (defun erlang-menu-init ()
1350 "Init menus for Erlang mode.
1352 The variable `erlang-menu-items' contain a description of the Erlang
1353 mode menu. Normally, the list contains atoms, representing variables
1354 bound to pieces of the menu.
1356 Personal extensions could be added to `erlang-menu-personal-items'.
1358 This function should be called if any variable describing the
1359 menu configuration is changed."
1360 (erlang-menu-install "Erlang" erlang-menu-items erlang-mode-map t))
1363 (defun erlang-menu-install (name items keymap &optional popup)
1364 "Install a menu in Emacs or XEmacs based on an abstract description.
1366 NAME is the name of the menu.
1368 ITEMS is a list. The elements are either nil representing a horizontal
1369 line or a list with two or three elements. The first is the name of
1370 the menu item, the second the function to call, or a submenu, on the
1371 same same form as ITEMS. The third optional element is an expression
1372 which is evaluated every time the menu is displayed. Should the
1373 expression evaluate to nil the menu item is ghosted.
1375 KEYMAP is the keymap to add to menu to. (When using XEmacs, the menu
1376 will only be visible when this menu is the global, the local, or an
1377 activate minor mode keymap.)
1379 If POPUP is non-nil, the menu is bound to the XEmacs `mode-popup-menu'
1380 variable, i.e. it will popup when pressing the right mouse button.
1382 Please see the variable `erlang-menu-base-items'."
1383 (cond (erlang-xemacs-p
1384 (let ((menu (erlang-menu-xemacs name items keymap)))
1385 ;; We add the menu to the global menubar.
1386 (funcall (symbol-function 'add-submenu) nil menu)
1387 (setcdr erlang-xemacs-popup-menu (cdr menu))
1388 (if (and popup (boundp 'mode-popup-menu))
1389 (funcall (symbol-function 'set)
1390 'mode-popup-menu erlang-xemacs-popup-menu))))
1391 ((>= erlang-emacs-major-version 19)
1392 (define-key keymap (vector 'menu-bar (intern name))
1393 (erlang-menu-make-keymap name items)))
1394 (t nil)))
1397 (defun erlang-menu-make-keymap (name items)
1398 "Build a menu for Emacs 19."
1399 (let ((menumap (funcall (symbol-function 'make-sparse-keymap)
1400 name))
1401 (count 0)
1402 id def first second third)
1403 (setq items (reverse items))
1404 (while items
1405 ;; Replace any occurrence of atoms by their value.
1406 (while (and items (atom (car items)) (not (null (car items))))
1407 (if (and (boundp (car items))
1408 (listp (symbol-value (car items))))
1409 (setq items (append (reverse (symbol-value (car items)))
1410 (cdr items)))
1411 (setq items (cdr items))))
1412 (setq first (car-safe (car items)))
1413 (setq second (car-safe (cdr-safe (car items))))
1414 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1415 (cond ((null first)
1416 (setq count (+ count 1))
1417 (setq id (intern (format "separator-%d" count)))
1418 (setq def '("--" . nil)))
1419 ((and (consp second) (eq (car second) 'lambda))
1420 (setq count (+ count 1))
1421 (setq id (intern (format "lambda-%d" count)))
1422 (setq def (cons first second)))
1423 ((symbolp second)
1424 (setq id second)
1425 (setq def (cons first second)))
1427 (setq count (+ count 1))
1428 (setq id (intern (format "submenu-%d" count)))
1429 (setq def (erlang-menu-make-keymap first second))))
1430 (define-key menumap (vector id) def)
1431 (if third
1432 (put id 'menu-enable third))
1433 (setq items (cdr items)))
1434 (cons name menumap)))
1437 (defun erlang-menu-xemacs (name items &optional keymap)
1438 "Build a menu for XEmacs."
1439 (let ((res '())
1440 first second third entry)
1441 (while items
1442 ;; Replace any occurrence of atoms by their value.
1443 (while (and items (atom (car items)) (not (null (car items))))
1444 (if (and (boundp (car items))
1445 (listp (symbol-value (car items))))
1446 (setq items (append (reverse (symbol-value (car items)))
1447 (cdr items)))
1448 (setq items (cdr items))))
1449 (setq first (car-safe (car items)))
1450 (setq second (car-safe (cdr-safe (car items))))
1451 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1452 (cond ((null first)
1453 (setq res (cons "------" res)))
1454 ((symbolp second)
1455 (setq res (cons (vector first second (or third t)) res)))
1456 ((and (consp second) (eq (car second) 'lambda))
1457 (setq res (cons (vector first (list 'call-interactively second)
1458 (or third t)) res)))
1460 (setq res (cons (cons first
1461 (cdr (erlang-menu-xemacs
1462 first second)))
1463 res))))
1464 (setq items (cdr items)))
1465 (setq res (reverse res))
1466 ;; When adding a menu to a minor-mode keymap under Emacs,
1467 ;; it disappears when the mode is disabled. The expression
1468 ;; generated below imitates this behaviour.
1469 ;; (This could be expressed much clearer using backquotes,
1470 ;; but I don't want to pull in every package.)
1471 (if keymap
1472 (let ((expr (list 'or
1473 (list 'eq keymap 'global-map)
1474 (list 'eq keymap (list 'current-local-map))
1475 (list 'symbol-value
1476 (list 'car-safe
1477 (list 'rassq
1478 keymap
1479 'minor-mode-map-alist))))))
1480 (setq res (cons ':included (cons expr res)))))
1481 (cons name res)))
1484 (defun erlang-menu-substitute (items alist)
1485 "Substitute functions in menu described by ITEMS.
1487 The menu ITEMS is updated destructively.
1489 ALIST is list of pairs where the car is the old function and cdr the new."
1490 (let (first second pair)
1491 (while items
1492 (setq first (car-safe (car items)))
1493 (setq second (car-safe (cdr-safe (car items))))
1494 (cond ((null first))
1495 ((symbolp second)
1496 (setq pair (and second (assq second alist)))
1497 (if pair
1498 (setcar (cdr (car items)) (cdr pair))))
1499 ((and (consp second) (eq (car second) 'lambda)))
1501 (erlang-menu-substitute second alist)))
1502 (setq items (cdr items)))))
1505 (defun erlang-menu-add-above (entry above items)
1506 "Add menu ENTRY above menu entry ABOVE in menu ITEMS.
1507 Do nothing if the items already should be in the menu.
1508 Should ABOVE not be in the list, the entry is added at
1509 the bottom of the menu.
1511 The new menu is returned. No guarantee is given that the original
1512 menu is left unchanged.
1514 The equality test is performed by `eq'.
1516 Example: (erlang-menu-add-above 'my-erlang-menu-items
1517 'erlang-menu-man-items)"
1518 (erlang-menu-add-below entry above items t))
1521 (defun erlang-menu-add-below (entry below items &optional above-p)
1522 "Add menu ENTRY below menu items BELOW in the Erlang menu.
1523 Do nothing if the items already should be in the menu.
1524 Should BELOW not be in the list, items is added at the bottom
1525 of the menu.
1527 The new menu is returned. No guarantee is given that the original
1528 menu is left unchanged.
1530 The equality test is performed by `eq'.
1532 Example:
1534 \(setq erlang-menu-items
1535 (erlang-menu-add-below 'my-erlang-menu-items
1536 'erlang-menu-base-items
1537 erlang-menu-items))"
1538 (if (memq entry items)
1539 items ; Return the original menu.
1540 (let ((head '())
1541 (done nil)
1542 res)
1543 (while (not done)
1544 (cond ((null items)
1545 (setq res (append head (list entry)))
1546 (setq done t))
1547 ((eq below (car items))
1548 (setq res
1549 (if above-p
1550 (append head (cons entry items))
1551 (append head (cons (car items)
1552 (cons entry (cdr items))))))
1553 (setq done t))
1555 (setq head (append head (list (car items))))
1556 (setq items (cdr items)))))
1557 res)))
1559 (defun erlang-menu-delete (entry items)
1560 "Delete ENTRY from menu ITEMS.
1562 The new menu is returned. No guarantee is given that the original
1563 menu is left unchanged."
1564 (delq entry items))
1566 ;; Man code:
1568 (defun erlang-man-init ()
1569 "Add menus containing the manual pages of the Erlang.
1571 The variable `erlang-man-dirs' contains entries describing
1572 the location of the manual pages."
1573 (interactive)
1574 (if erlang-man-inhibit
1576 (setq erlang-menu-man-items
1577 '(nil
1578 ("Man - Function" erlang-man-function)))
1579 (if erlang-man-dirs
1580 (setq erlang-menu-man-items
1581 (append erlang-menu-man-items
1582 (erlang-man-make-top-menu erlang-man-dirs))))
1583 (setq erlang-menu-items
1584 (erlang-menu-add-above 'erlang-menu-man-items
1585 'erlang-menu-version-items
1586 erlang-menu-items))
1587 (erlang-menu-init)))
1590 (defun erlang-man-uninstall ()
1591 "Remove the man pages from the Erlang mode."
1592 (interactive)
1593 (setq erlang-menu-items
1594 (erlang-menu-delete 'erlang-menu-man-items erlang-menu-items))
1595 (erlang-menu-init))
1598 ;; The man menu is a hierarchal structure, with the manual sections
1599 ;; at the top, described by `erlang-man-dirs'. The next level could
1600 ;; either be the manual pages if not to many, otherwise it is an index
1601 ;; menu whose submenus will contain up to `erlang-man-max-menu-size'
1602 ;; manual pages.
1604 (defun erlang-man-make-top-menu (dir-list)
1605 "Create one menu entry per element of DIR-LIST.
1606 The format is described in the documentation of `erlang-man-dirs'."
1607 (let ((menu '())
1608 dir)
1609 (while dir-list
1610 (setq dir (cond ((nth 2 (car dir-list))
1611 ;; Relative to `erlang-man-root-dir'.
1612 (and (stringp erlang-man-root-dir)
1613 (concat erlang-man-root-dir (nth 1 (car dir-list)))))
1615 ;; Absolute
1616 (nth 1 (car dir-list)))))
1617 (if (and dir
1618 (file-readable-p dir))
1619 (setq menu (cons (list (car (car dir-list))
1620 (erlang-man-make-middle-menu
1621 (erlang-man-get-files dir)))
1622 menu)))
1623 (setq dir-list (cdr dir-list)))
1624 ;; Should no menus be found, generate a menu item which
1625 ;; will display a help text, when selected.
1626 (if menu
1627 (nreverse menu)
1628 '(("Man Pages"
1629 (("Error! Why?" erlang-man-describe-error)))))))
1632 ;; Should the menu be to long, let's split it into a number of
1633 ;; smaller menus. Warning, this code contains beautiful
1634 ;; destructive operations!
1635 (defun erlang-man-make-middle-menu (filelist)
1636 "Create the second level menu from FILELIST.
1638 Should the list be longer than `erlang-man-max-menu-size', a tree of
1639 menus is created."
1640 (if (<= (length filelist) erlang-man-max-menu-size)
1641 (erlang-man-make-menu filelist)
1642 (let ((menu '())
1643 (filelist (copy-sequence filelist))
1644 segment submenu pair)
1645 (while filelist
1646 (setq pair (nthcdr (- erlang-man-max-menu-size 1) filelist))
1647 (setq segment filelist)
1648 (if (null pair)
1649 (setq filelist nil)
1650 (setq filelist (cdr pair))
1651 (setcdr pair nil))
1652 (setq submenu (erlang-man-make-menu segment))
1653 (setq menu (cons (list (concat (car (car submenu))
1654 " -- "
1655 (car (car (reverse submenu))))
1656 submenu)
1657 menu)))
1658 (nreverse menu))))
1661 (defun erlang-man-make-menu (filelist)
1662 "Make a leaf menu based on FILELIST."
1663 (let ((menu '())
1664 item)
1665 (while filelist
1666 (setq item (erlang-man-make-menu-item (car filelist)))
1667 (if item
1668 (setq menu (cons item menu)))
1669 (setq filelist (cdr filelist)))
1670 (nreverse menu)))
1673 (defun erlang-man-make-menu-item (file)
1674 "Create a menu item containing the name of the man page."
1675 (and (string-match ".*/\\([^/]+\\)\\.[^.]$" file)
1676 (let ((page (substring file (match-beginning 1) (match-end 1))))
1677 (list (capitalize page)
1678 (list 'lambda '()
1679 '(interactive)
1680 (list 'funcall 'erlang-man-display-function
1681 file))))))
1684 (defun erlang-man-get-files (dir)
1685 "Return files in directory DIR."
1686 (directory-files dir t ".*\\.[0-9]\\'"))
1689 (defun erlang-man-module (&optional module)
1690 "Find manual page for MODULE, defaults to module of function under point.
1691 This function is aware of imported functions."
1692 (interactive
1693 (list (let* ((mod (car-safe (erlang-get-function-under-point)))
1694 (input (read-string
1695 (format "Manual entry for module%s: "
1696 (if (or (null mod) (string= mod ""))
1698 (format " (default %s)" mod))))))
1699 (if (string= input "")
1701 input))))
1702 (or module (setq module (car (erlang-get-function-under-point))))
1703 (if (or (null module) (string= module ""))
1704 (error "No Erlang module name given"))
1705 (let ((dir-list erlang-man-dirs)
1706 (pat (concat "/" (regexp-quote module) "\\.[^.]$"))
1707 (file nil)
1708 file-list)
1709 (while (and dir-list (null file))
1710 (setq file-list (erlang-man-get-files
1711 (if (nth 2 (car dir-list))
1712 (concat erlang-man-root-dir (nth 1 (car dir-list)))
1713 (nth 1 (car dir-list)))))
1714 (while (and file-list (null file))
1715 (if (string-match pat (car file-list))
1716 (setq file (car file-list)))
1717 (setq file-list (cdr file-list)))
1718 (setq dir-list (cdr dir-list)))
1719 (if file
1720 (funcall erlang-man-display-function file)
1721 (error "No manual page for module %s found" module))))
1724 ;; Warning, the function `erlang-man-function' is a hack!
1725 ;; It links itself into the man code in a non-clean way. I have
1726 ;; chosen to keep it since it provides a very useful functionality
1727 ;; which is not possible to achieve using a clean approach.
1728 ;; / AndersL
1730 (defvar erlang-man-function-name nil
1731 "Name of function for last `erlang-man-function' call.
1732 Used for communication between `erlang-man-function' and the
1733 patch to `Man-notify-when-ready'.")
1735 (defun erlang-man-function (&optional name)
1736 "Find manual page for NAME, where NAME is module:function.
1737 The entry for `function' is displayed.
1739 This function is aware of imported functions."
1740 (interactive
1741 (list (let* ((mod-func (erlang-get-function-under-point))
1742 (mod (car-safe mod-func))
1743 (func (nth 1 mod-func))
1744 (input (read-string
1745 (format
1746 "Manual entry for `module:func' or `module'%s: "
1747 (if (or (null mod) (string= mod ""))
1749 (format " (default %s:%s)" mod func))))))
1750 (if (string= input "")
1751 (if (and mod func)
1752 (concat mod ":" func)
1753 mod)
1754 input))))
1755 ;; Emacs 18 doesn't provide `man'...
1756 (condition-case nil
1757 (require 'man)
1758 (error nil))
1759 (let ((modname nil)
1760 (funcname nil))
1761 (cond ((null name)
1762 (let ((mod-func (erlang-get-function-under-point)))
1763 (setq modname (car-safe mod-func))
1764 (setq funcname (nth 1 mod-func))))
1765 ((string-match ":" name)
1766 (setq modname (substring name 0 (match-beginning 0)))
1767 (setq funcname (substring name (match-end 0) nil)))
1768 ((stringp name)
1769 (setq modname name)))
1770 (if (or (null modname) (string= modname ""))
1771 (error "No Erlang module name given"))
1772 (cond ((fboundp 'Man-notify-when-ready)
1773 ;; Emacs 19: The man command could possibly start an
1774 ;; asynchronous process, i.e. we must hook ourselves into
1775 ;; the system to be activated when the man-process
1776 ;; terminates.
1777 (if (null funcname)
1779 (erlang-man-patch-notify)
1780 (setq erlang-man-function-name funcname))
1781 (condition-case nil
1782 (erlang-man-module modname)
1783 (error (setq erlang-man-function-name nil))))
1785 (erlang-man-module modname)
1786 (if funcname
1787 (erlang-man-find-function
1788 (or (get-buffer "*Manual Entry*") ; Emacs 18
1789 (current-buffer)) ; XEmacs
1790 funcname))))))
1793 ;; Should the defadvice be at the top level, the package `advice' would
1794 ;; be required. Now it is only required when this functionality
1795 ;; is used. (Emacs 19 specific.)
1796 (defun erlang-man-patch-notify ()
1797 "Patch the function `Man-notify-when-ready' to search for function.
1798 The variable `erlang-man-function-name' is assumed to be bound to
1799 the function name, or to nil.
1801 The reason for patching a function is that under Emacs 19, the man
1802 command is executed asynchronously."
1803 (condition-case nil
1804 (require 'advice)
1805 ;; This should never happened since this is only called when
1806 ;; running under Emacs 19.
1807 (error (error (concat "This command needs the package `advice', "
1808 "please upgrade your Emacs."))))
1809 (require 'man)
1810 (defadvice Man-notify-when-ready
1811 (after erlang-Man-notify-when-ready activate)
1812 "Set point at the documentation of the function name in
1813 `erlang-man-function-name' when the man page is displayed."
1814 (if erlang-man-function-name
1815 (erlang-man-find-function (ad-get-arg 0) erlang-man-function-name))
1816 (setq erlang-man-function-name nil)))
1819 (defun erlang-man-find-function (buf func)
1820 "Find manual page for function in `erlang-man-function-name' in buffer BUF."
1821 (if func
1822 (let ((win (get-buffer-window buf)))
1823 (if win
1824 (progn
1825 (set-buffer buf)
1826 (goto-char (point-min))
1827 (if (re-search-forward
1828 (concat "^[ \t]+" func " ?(")
1829 (point-max) t)
1830 (progn
1831 (forward-word -1)
1832 (set-window-point win (point)))
1833 (message "Could not find function `%s'" func)))))))
1836 (defun erlang-man-display (file)
1837 "Display FILE as a `man' file.
1838 This is the default manual page display function.
1839 The variables `erlang-man-display-function' contains the function
1840 to be used."
1841 ;; Emacs 18 doesn't `provide' man.
1842 (condition-case nil
1843 (require 'man)
1844 (error nil))
1845 (if file
1846 (let ((process-environment (copy-sequence process-environment)))
1847 (if (string-match "\\(.*\\)/man[^/]*/\\([^/]+\\)\\.[^.]$" file)
1848 (let ((dir (substring file (match-beginning 1) (match-end 1)))
1849 (page (substring file (match-beginning 2) (match-end 2))))
1850 (if (fboundp 'setenv)
1851 (setenv "MANPATH" dir)
1852 ;; Emacs 18
1853 (setq process-environment (cons (concat "MANPATH=" dir)
1854 process-environment)))
1855 (cond ((not (and (not erlang-xemacs-p)
1856 (= erlang-emacs-major-version 19)
1857 (< erlang-emacs-minor-version 29)))
1858 (manual-entry page))
1860 ;; Emacs 19.28 and earlier versions of 19:
1861 ;; The manual-entry command unconditionally prompts
1862 ;; the user :-(
1863 (funcall (symbol-function 'Man-getpage-in-background)
1864 page))))
1865 (error "Can't find man page for %s\n" file)))))
1868 (defun erlang-man-describe-error ()
1869 "Describe why the manual pages weren't found."
1870 (interactive)
1871 (with-output-to-temp-buffer "*Erlang Man Error*"
1872 (princ "Normally, this menu should contain Erlang manual pages.
1874 In order to find the manual pages, the variable `erlang-man-root-dir'
1875 should be bound to the name of the directory containing the Erlang
1876 man pages. The name should not include the final slash.
1878 Practically, you should add a line on the following form to
1879 your ~/.emacs, or ask your system administrator to add it to
1880 the site init file:
1882 (setq erlang-man-root-dir \"/usr/local/erlang\")
1884 After installing the line, kill and restart Emacs, or restart Erlang
1885 mode with the command `M-x erlang-mode RET'.")))
1887 ;; Indentation code:
1889 (defun erlang-indent-command (&optional whole-exp)
1890 "Indent current line as Erlang code.
1891 With argument, indent any additional lines of the same clause
1892 rigidly along with this one."
1893 (interactive "P")
1894 (if whole-exp
1895 ;; If arg, always indent this line as Erlang
1896 ;; and shift remaining lines of clause the same amount.
1897 (let ((shift-amt (erlang-indent-line))
1898 beg end)
1899 (save-excursion
1900 (if erlang-tab-always-indent
1901 (beginning-of-line))
1902 (setq beg (point))
1903 (erlang-end-of-clause 1)
1904 (setq end (point))
1905 (goto-char beg)
1906 (forward-line 1)
1907 (setq beg (point)))
1908 (if (> end beg)
1909 (indent-code-rigidly beg end shift-amt "\n")))
1910 (if (and (not erlang-tab-always-indent)
1911 (save-excursion
1912 (skip-chars-backward " \t")
1913 (not (bolp))))
1914 (insert-tab)
1915 (erlang-indent-line))))
1918 (defun erlang-indent-line ()
1919 "Indent current line as Erlang code.
1920 Return the amount the indentation changed by."
1921 (let ((pos (- (point-max) (point)))
1922 indent beg
1923 shift-amt)
1924 (beginning-of-line 1)
1925 (setq beg (point))
1926 (skip-chars-forward " \t")
1927 (cond ((looking-at "%")
1928 (setq indent (funcall comment-indent-function))
1929 (setq shift-amt (- indent (current-column))))
1931 (setq indent (erlang-calculate-indent))
1932 (cond ((null indent)
1933 (setq indent (current-indentation)))
1934 ((eq indent t)
1935 ;; This should never occur here.
1936 (error "Erlang mode error"))
1937 ((= (char-syntax (following-char)) ?\))
1938 (setq indent (1- indent))))
1939 (setq shift-amt (- indent (current-column)))))
1940 (if (zerop shift-amt)
1942 (delete-region beg (point))
1943 (indent-to indent))
1944 ;; If initial point was within line's indentation, position
1945 ;; after the indentation. Else stay at same point in text.
1946 (if (> (- (point-max) pos) (point))
1947 (goto-char (- (point-max) pos)))
1948 shift-amt))
1951 (defun erlang-indent-region (beg end)
1952 "Indent region of Erlang code.
1954 This is automagically called by the user level function `indent-region'."
1955 (interactive "r")
1956 (save-excursion
1957 (let ((case-fold-search nil)
1958 (continue t)
1959 (from-end (- (point-max) end))
1960 indent-point;; The beginning of the current line
1961 indent;; The indent amount
1962 state)
1963 (goto-char beg)
1964 (beginning-of-line)
1965 (setq indent-point (point))
1966 (erlang-beginning-of-clause)
1967 ;; Parse the Erlang code from the beginning of the clause to
1968 ;; the beginning of the region.
1969 (while (< (point) indent-point)
1970 (setq state (erlang-partial-parse (point) indent-point state)))
1971 ;; Indent every line in the region
1972 (while continue
1973 (goto-char indent-point)
1974 (skip-chars-forward " \t")
1975 (cond ((looking-at "%")
1976 ;; Do not use our stack to help the user to customize
1977 ;; comment indentation.
1978 (setq indent (funcall comment-indent-function)))
1979 ((looking-at "$")
1980 ;; Don't indent empty lines.
1981 (setq indent 0))
1983 (setq indent
1984 (save-excursion
1985 (erlang-calculate-stack-indent (point) state)))
1986 (cond ((null indent)
1987 (setq indent (current-indentation)))
1988 ((eq indent t)
1989 ;; This should never occur here.
1990 (error "Erlang mode error"))
1991 ((= (char-syntax (following-char)) ?\))
1992 (setq indent (1- indent))))))
1993 (if (zerop (- indent (current-column)))
1995 (delete-region indent-point (point))
1996 (indent-to indent))
1997 ;; Find the next line in the region
1998 (goto-char indent-point)
1999 (save-excursion
2000 (forward-line 1)
2001 (setq indent-point (point)))
2002 (if (>= from-end (- (point-max) indent-point))
2003 (setq continue nil)
2004 (while (< (point) indent-point)
2005 (setq state (erlang-partial-parse
2006 (point) indent-point state))))))))
2009 (defun erlang-indent-current-buffer ()
2010 "Indent current buffer as Erlang code."
2011 (interactive)
2012 (save-excursion
2013 (save-restriction
2014 (widen)
2015 (erlang-indent-region (point-min) (point-max)))))
2018 (defun erlang-indent-function ()
2019 "Indent current Erlang function."
2020 (interactive)
2021 (save-excursion
2022 (let ((end (progn (erlang-end-of-function 1) (point)))
2023 (beg (progn (erlang-beginning-of-function 1) (point))))
2024 (erlang-indent-region beg end))))
2027 (defun erlang-indent-clause ()
2028 "Indent current Erlang clause."
2029 (interactive)
2030 (save-excursion
2031 (let ((end (progn (erlang-end-of-clause 1) (point)))
2032 (beg (progn (erlang-beginning-of-clause 1) (point))))
2033 (erlang-indent-region beg end))))
2036 (defmacro erlang-push (x stack) (list 'setq stack (list 'cons x stack)))
2037 (defmacro erlang-pop (stack) (list 'setq stack (list 'cdr stack)))
2038 ;; Would much prefer to make caddr a macro but this clashes.
2039 (defun erlang-caddr (x) (car (cdr (cdr x))))
2042 (defun erlang-calculate-indent (&optional parse-start)
2043 "Compute appropriate indentation for current line as Erlang code.
2044 Return nil if line starts inside string, t if in a comment."
2045 (save-excursion
2046 (let ((indent-point (point))
2047 (case-fold-search nil)
2048 (state nil))
2049 (if parse-start
2050 (goto-char parse-start)
2051 (erlang-beginning-of-clause))
2052 (while (< (point) indent-point)
2053 (setq state (erlang-partial-parse (point) indent-point state)))
2054 (erlang-calculate-stack-indent indent-point state))))
2056 (defun erlang-show-syntactic-information ()
2057 "Show syntactic information for current line."
2059 (interactive)
2061 (save-excursion
2062 (let ((starting-point (point))
2063 (case-fold-search nil)
2064 (state nil))
2065 (erlang-beginning-of-clause)
2066 (while (< (point) starting-point)
2067 (setq state (erlang-partial-parse (point) starting-point state)))
2068 (message "%S" state))))
2071 (defun erlang-partial-parse (from to &optional state)
2072 "Parse Erlang syntax starting at FROM until TO, with an optional STATE.
2073 Value is list (stack token-start token-type in-what)."
2074 (goto-char from) ; Start at the beginning
2075 (erlang-skip-blank to)
2076 (let ((cs (char-syntax (following-char)))
2077 (stack (car state))
2078 (token (point))
2079 in-what)
2080 (cond
2082 ;; Done: Return previous state.
2083 ((>= token to)
2084 (setq token (nth 1 state))
2085 (setq cs (nth 2 state))
2086 (setq in-what (nth 3 state)))
2088 ;; Word constituent: check and handle keywords.
2089 ((= cs ?w)
2090 (cond ((looking-at "\\(end\\|after\\)[^_a-zA-Z0-9]")
2091 ;; Must pop top icr layer, `after' will push a new
2092 ;; layer next.
2093 (progn
2094 (while (and stack (eq (car (car stack)) '->))
2095 (erlang-pop stack))
2096 (if (and stack (memq (car (car stack)) '(icr begin)))
2097 (erlang-pop stack))))
2098 ((looking-at "catch[^,\n\\of]*\n")
2099 ;; Must pop top icr layer, `catch' in try/catch
2100 ;;will push a new layer next.
2101 (progn
2102 (while (and stack (eq (car (car stack)) '->))
2103 (erlang-pop stack))
2104 (if (and stack (memq (car (car stack)) '(icr begin)))
2105 (erlang-pop stack))))
2107 (cond ((looking-at "\\(if\\|case\\|receive\\|try\\)[^_a-zA-Z0-9]")
2108 ;; Must push a new icr (if/case/receive) layer.
2109 (erlang-push (list 'icr token (current-column)) stack))
2110 ((looking-at "\\(fun\\)[^_a-zA-Z0-9]")
2111 ;; Push a new icr layer if we are defining a `fun'
2112 ;; expression, not when we are refering an existing
2113 ;; function.
2114 (if (save-excursion
2115 (goto-char (match-end 1))
2116 (erlang-skip-blank to)
2117 (eq (following-char) ?\())
2118 (erlang-push (list 'icr token (current-column)) stack)))
2119 ((looking-at "\\(begin\\|query\\)[^_a-zA-Z0-9]")
2120 (erlang-push (list 'begin token (current-column)) stack))
2121 ;; In test suites you may want to do something like
2122 ;; ?match(Mem when integer(Mem), mnesia:table_info(Tab,
2123 ;; memory)), and then the following if/case/receive
2124 ;; statement will mess up the indentation by fooling the
2125 ;; erlang mode to think the 'when' in the argument is a
2126 ;; "real" when. The following three clauses will avoid
2127 ;; this problem.
2128 ((looking-at "when[^->\.]*if[^->\.]*->"))
2129 ((looking-at "when[^->\.]*case[^->\.]*->"))
2130 ((looking-at "when[^->\.]*receive[^->\.]*->"))
2131 ;; Normal when case
2132 ((looking-at "when [^->\.]*->")
2133 (erlang-push (list 'when token (current-column)) stack))
2134 ((looking-at "after[.]+->")
2135 (erlang-push (list 'icr token (current-column)) stack))
2136 ((looking-at "after[^_a-zA-Z0-9->]")
2137 (erlang-push (list 'icr token (current-column)) stack)
2138 (erlang-push (list '-> token (current-column)) stack))
2139 ((looking-at "catch[^,\n\\of]*\n")
2140 (erlang-push (list 'icr token (current-column)) stack))
2142 (forward-sexp 1))
2144 ;; String: Try to skip over it. (Catch error if not complete.)
2145 ((= cs ?\")
2146 (condition-case nil
2147 (progn
2148 (forward-sexp 1)
2149 (if (> (point) to)
2150 (progn
2151 (setq in-what 'string)
2152 (goto-char to))))
2153 (error
2154 (setq in-what 'string)
2155 (goto-char to))))
2157 ;; Symbol constituent or punctuation
2159 ((memq cs '(?. ?_))
2160 (cond
2162 ;; Clause end
2163 ((= (following-char) ?\;)
2164 (if (and stack (eq (car (car stack)) '->))
2165 (erlang-pop stack))
2166 (forward-char 1))
2168 ;; Function end
2169 ((looking-at "\\.\\(\\s \\|\n\\|\\s<\\)")
2170 (setq stack nil)
2171 (forward-char 1))
2173 ;; Function head
2174 ((looking-at "->\\|:-")
2175 (save-excursion
2176 (back-to-indentation)
2177 (cond ((looking-at "after[^_a-zA-Z0-9]")
2178 (erlang-pop stack))))
2179 (if (and stack (eq (car (car stack)) 'when))
2180 (erlang-pop stack))
2181 (erlang-push (list '-> token (current-column)) stack)
2182 (forward-char 2))
2184 ;; List-comprehension divider
2185 ((looking-at "||")
2186 (erlang-push (list '|| token (current-column)) stack)
2187 (forward-char 2))
2189 ;; Parameter separator
2190 ((looking-at ",")
2191 (forward-char 1))
2193 ;; Bit-syntax open paren
2194 ((looking-at "<<")
2195 (erlang-push (list '\( token (current-column)) stack)
2196 (forward-char 2))
2198 ;; Bbit-syntax close paren
2199 ((looking-at ">>")
2200 (while (memq (car (car stack)) '(|| ->))
2201 (erlang-pop stack))
2202 (cond ((eq (car (car stack)) '\()
2203 (erlang-pop stack))
2204 ((memq (car (car stack)) '(icr begin))
2205 (error "Missing `end'"))
2207 (error "Unbalanced parentheses")))
2208 (forward-char 2))
2210 ;; Macro
2211 ((= (following-char) ??)
2212 ;; Skip over the ?
2213 (forward-char 1)
2216 ;; Other punctuation: Skip over it and any following punctuation
2217 ((= cs ?.)
2218 ;; Skip over all characters in the operand.
2219 (skip-syntax-forward "."))
2221 ;; Other char: Skip over it.
2223 (forward-char 1))))
2225 ;; Open parenthesis
2226 ((= cs ?\()
2227 (erlang-push (list '\( token (current-column)) stack)
2228 (forward-char 1))
2230 ;; Close parenthesis
2231 ((= cs ?\))
2232 (while (memq (car (car stack)) '(|| ->))
2233 (erlang-pop stack))
2234 (cond ((eq (car (car stack)) '\()
2235 (erlang-pop stack))
2236 ((eq (car (car stack)) 'icr)
2237 (erlang-pop stack)
2238 ;; Normal catch not try-catch might have caused icr
2239 ;; and then incr should be removed and is not an error.
2240 (if (eq (car (car stack)) '\()
2241 (erlang-pop stack)
2242 (error "Missing `end'")))
2243 ((eq (car (car stack)) 'begin)
2244 (error "Missing `end'"))
2246 (error "Unbalanced parenthesis")))
2247 (forward-char 1))
2249 ;; Character quote: Skip it and the quoted char.
2250 ((= cs ?/)
2251 (forward-char 2))
2253 ;; Character escape: Skip it and the escape sequence.
2254 ((= cs ?\\)
2255 (forward-char 1)
2256 (skip-syntax-forward "w"))
2258 ;; Everything else
2260 (forward-char 1)))
2261 (list stack token cs in-what)))
2263 (defun erlang-calculate-stack-indent (indent-point state)
2264 "From the given last position and state (stack) calculate indentation.
2265 Return nil if inside string, t if in a comment."
2266 (let* ((stack (and state (car state)))
2267 (token (nth 1 state))
2268 (stack-top (and stack (car stack))))
2269 (cond ((null state) ;No state
2271 ((nth 3 state)
2272 ;; Return nil or t.
2273 (eq (nth 3 state) 'comment))
2274 ((null stack)
2275 (if (looking-at "when[^_a-zA-Z0-9]")
2276 erlang-indent-guard
2278 ((eq (car stack-top) '\()
2279 ;; Element of list, tuple or part of an expression,
2280 (if (null erlang-argument-indent)
2281 ;; indent to next column.
2282 (1+ (nth 2 stack-top))
2283 (goto-char (nth 1 stack-top))
2284 (cond ((looking-at "[({]\\s *\\($\\|%\\)")
2285 ;; Line ends with parenthesis.
2286 (+ (erlang-indent-find-preceding-expr)
2287 erlang-argument-indent))
2289 ;; Indent to the same column as the first
2290 ;; argument.
2291 (goto-char (1+ (nth 1 stack-top)))
2292 (skip-chars-forward " \t")
2293 (current-column)))))
2294 ((eq (car stack-top) 'icr)
2295 ;; The default indentation is the column of the option
2296 ;; directly following the keyword. (This does not apply to
2297 ;; `case'.) Should no option be on the same line, the
2298 ;; indentation is the indentation of the keyword +
2299 ;; `erlang-indent-level'.
2301 ;; `after' should be indented to the save level as the
2302 ;; corresponding receive.
2303 (if (looking-at "\\(after\\|catch\\)[^_a-zA-Z0-9]")
2304 (nth 2 stack-top)
2305 (save-excursion
2306 (goto-char (nth 1 stack-top))
2307 (if (looking-at "case[^_a-zA-Z0-9]")
2308 (+ (nth 2 stack-top) erlang-indent-level)
2309 (skip-chars-forward "a-z")
2310 (skip-chars-forward " \t")
2311 (if (memq (following-char) '(?% ?\n))
2312 (+ (nth 2 stack-top) erlang-indent-level)
2313 (current-column)))))
2315 ;; Real indentation, where operators create extra indentation etc.
2316 ((memq (car stack-top) '(-> || begin))
2317 (goto-char (nth 1 stack-top))
2318 ;; Check if there is more code after the '->' on the
2319 ;; same line. If so use this indentation as base, else
2320 ;; use parent indentation + 2 * level as base.
2321 (let ((off erlang-indent-level)
2322 (skip 2))
2323 (cond ((null (cdr stack))) ; Top level in function.
2324 ((eq (car stack-top) 'begin)
2325 (setq skip 5))
2326 ((eq (car stack-top) '->)
2327 (setq off (* 2 erlang-indent-level))))
2328 (let ((base (erlang-indent-find-base stack indent-point off skip)))
2329 ;; Look at last thing to see how we are to move relative
2330 ;; to the base.
2331 (goto-char token)
2332 (cond ((looking-at "||\\|,\\|->\\|:-")
2333 base)
2334 ((erlang-at-keyword)
2335 (+ (current-column) erlang-indent-level))
2336 ((or (= (char-syntax (following-char)) ?.)
2337 (erlang-at-operator))
2338 (+ base erlang-indent-level))
2340 (goto-char indent-point)
2341 (cond ((memq (following-char) '(?\( ?{))
2342 ;; Function application or record.
2343 (+ (erlang-indent-find-preceding-expr)
2344 erlang-argument-indent))
2345 ;; Empty line, or end; treat it as the end of
2346 ;; the block. (Here we have a choice: should
2347 ;; the user be forced to reindent continued
2348 ;; lines, or should the "end" be reindented?)
2349 ((looking-at "\\(end\\|after\\|catch\\)[^_a-zA-Z0-9]\\|$")
2350 (if (eq (car (car stack)) '->)
2351 (erlang-pop stack))
2352 (if stack
2353 (erlang-caddr (car stack))
2355 ;; Avoid treating comments a continued line.
2356 ((= (following-char) ?%)
2357 base)
2358 ;; Continued line (e.g. line beginning
2359 ;; with an operator.)
2360 (t (+ base erlang-indent-level)))))))
2362 ((eq (car stack-top) 'when)
2363 (goto-char (nth 1 stack-top))
2364 (if (looking-at "when\\s *\\($\\|%\\)")
2365 (progn
2366 (erlang-pop stack)
2367 (if (and stack (eq (nth 0 (car stack)) 'icr))
2368 (progn
2369 (goto-char (nth 1 (car stack)))
2370 (+ (nth 2 (car stack)) erlang-indent-guard
2371 ;; receive XYZ or receive
2372 ;; XYZ
2373 (if (looking-at "[a-z]+\\s *\\($\\|%\\)")
2374 erlang-indent-level
2375 (* 2 erlang-indent-level))))
2376 erlang-indent-guard))
2377 ;; "when" is followed by code, let's indent to the same
2378 ;; column.
2379 (forward-char 4) ; Skip "when"
2380 (skip-chars-forward " \t")
2381 (current-column))))))
2384 (defun erlang-indent-find-base (stack indent-point &optional offset skip)
2385 "Find the base column for current stack."
2386 (or skip (setq skip 2))
2387 (or offset (setq offset erlang-indent-level))
2388 (save-excursion
2389 (let* ((stack-top (car stack)))
2390 (goto-char (nth 1 stack-top))
2391 (forward-char skip)
2392 (if (looking-at "\\s *\\($\\|%\\)")
2393 (progn
2394 (if (memq (car stack-top) '(-> ||))
2395 (erlang-pop stack))
2396 ;; Take parent identation + offset,
2397 ;; else just erlang-indent-level if no parent
2398 (if stack
2399 (+ (erlang-caddr (car stack))
2400 offset)
2401 erlang-indent-level))
2402 (erlang-skip-blank indent-point)
2403 (current-column)))))
2406 ;; Does not handle `begin' .. `end'.
2407 (defun erlang-indent-find-preceding-expr ()
2408 "Return the first column of the preceding expression.
2409 This assumes that the preceding expression is either simple
2410 \(i.e. an atom) or parenthesized."
2411 (save-excursion
2412 (forward-sexp -1)
2413 (let ((col (current-column)))
2414 (skip-chars-backward " \t")
2415 ;; Needed to match the colon in "'foo':'bar'".
2416 (if (not (memq (preceding-char) '(?# ?:)))
2418 (backward-char 1)
2419 (forward-sexp -1)
2420 (current-column)))))
2423 (defun erlang-skip-blank (&optional lim)
2424 "Skip over whitespace and comments until limit reached."
2425 (or lim (setq lim (point-max)))
2426 (let (stop)
2427 (while (and (not stop) (< (point) lim))
2428 (cond ((= (following-char) ?%)
2429 (skip-chars-forward "^\n" lim))
2430 ((= (following-char) ?\n)
2431 (skip-chars-forward "\n" lim))
2432 ((looking-at "\\s ")
2433 (if (re-search-forward "\\S " lim 'move)
2434 (forward-char -1)))
2436 (setq stop t))))
2437 stop))
2439 (defun erlang-at-keyword ()
2440 "Are we looking at an Erlang keyword which will increase indentation?"
2441 (looking-at (concat "\\(when\\|if\\|fun\\|case\\|begin\\|query\\|"
2442 "of\\|receive\\|after\\|catch\\)[^_a-zA-Z0-9]")))
2444 (defun erlang-at-operator ()
2445 "Are we looking at an Erlang operator?"
2446 (looking-at
2447 "\\(bnot\\|div\\|mod\\|band\\|bor\\|bxor\\|bsl\\|bsr\\)[^_a-zA-Z0-9]"))
2449 (defun erlang-comment-indent ()
2450 "Compute Erlang comment indentation.
2452 Used both by `indent-for-comment' and the Erlang specific indentation
2453 commands."
2454 (if (not erlang-oldstyle-comment-indent)
2455 (cond ((looking-at "%%") 0)
2456 ((looking-at "%")
2457 (or (erlang-calculate-indent)
2458 (current-indentation))))
2459 (cond ((looking-at "%%%") 0)
2460 ((looking-at "%%")
2461 (or (erlang-calculate-indent)
2462 (current-indentation)))
2464 (save-excursion
2465 (skip-chars-backward " \t")
2466 (max (if (bolp) 0 (1+ (current-column)))
2467 comment-column))))))
2470 ;;; Erlang movement commands
2472 ;; All commands below work as movement commands. I.e. if the point is
2473 ;; at the end of the clause, and the command `erlang-end-of-clause' is
2474 ;; executed, the point is moved to the end of the NEXT clause. (This
2475 ;; mimics the behaviour of `end-of-defun'.)
2477 ;; Personally I would like to rewrite them to be "pure", and add a set
2478 ;; of movement functions, like `erlang-next-clause',
2479 ;; `erlang-previous-clause', and the same for functions.
2481 ;; The current implementation makes it hopeless to use the functions as
2482 ;; subroutines in more complex commands. /andersl
2484 (defun erlang-beginning-of-clause (&optional arg)
2485 "Move backward to previous start of clause.
2486 With argument, do this that many times.
2487 Return t unless search stops due to end of buffer."
2488 (interactive "p")
2489 (or arg (setq arg 1))
2490 (if (< arg 0)
2491 ;; Step back to the end of the previous line, unless we are at
2492 ;; the beginning of the buffer. The reason for this move is
2493 ;; that the regexp below includes the last character of the
2494 ;; previous line.
2495 (if (bobp)
2496 (or (looking-at "\n")
2497 (forward-char 1))
2498 (forward-char -1)
2499 (if (looking-at "\\`\n")
2500 (forward-char 1))))
2501 ;; The regexp matches a function header that isn't
2502 ;; included in a string.
2503 (and (re-search-forward "\\(\\`\\|\\`\n\\|[^\\]\n\\)\\([a-z]\\|'\\|-\\)"
2504 nil 'move (- arg))
2505 (let ((beg (match-beginning 2)))
2506 (and beg (goto-char beg))
2507 t)))
2509 (defun erlang-end-of-clause (&optional arg)
2510 "Move to the end of the current clause.
2511 With argument, do this that many times."
2512 (interactive "p")
2513 (or arg (setq arg 1))
2514 (while (and (looking-at "[ \t]*[%\n]")
2515 (zerop (forward-line 1))))
2516 ;; Move to the next clause.
2517 (erlang-beginning-of-clause (- arg))
2518 (beginning-of-line);; Just to be sure...
2519 (let ((continue t))
2520 (while (and (not (bobp)) continue)
2521 (forward-line -1)
2522 (skip-chars-forward " \t")
2523 (if (looking-at "[%\n]")
2525 (end-of-line)
2526 (setq continue nil)))))
2528 (defun erlang-mark-clause ()
2529 "Put mark at end of clause, point at beginning."
2530 (interactive)
2531 (push-mark (point))
2532 (erlang-end-of-clause 1)
2533 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2534 ;; the region.
2535 (condition-case nil
2536 (push-mark (point) nil t)
2537 (error (push-mark (point))))
2538 (erlang-beginning-of-clause 1)
2539 ;; The above function deactivates the mark.
2540 (if (boundp 'deactivate-mark)
2541 (funcall (symbol-function 'set) 'deactivate-mark nil)))
2543 (defun erlang-beginning-of-function (&optional arg)
2544 "Move backward to previous start of function.
2545 With positive argument, do this that many times.
2546 With negative argument, search forward.
2548 Return t unless search stops due to end of buffer."
2549 (interactive "p")
2550 (or arg (setq arg 1))
2551 (cond
2552 ;; Search backward
2553 ((> arg 0)
2554 (while (and (> arg 0)
2555 (and (erlang-beginning-of-clause 1)
2556 (let ((start (point))
2557 (name (erlang-name-of-function))
2558 (arity (erlang-get-function-arity)))
2559 ;; Note: "arity" is nil for e.g. "-import", hence
2560 ;; two "-import" clauses are not considered to
2561 ;; be part of the same function.
2562 (while (and (erlang-beginning-of-clause 1)
2563 (string-equal name
2564 (erlang-name-of-function))
2565 arity
2566 (equal arity
2567 (erlang-get-function-arity)))
2568 (setq start (point)))
2569 (goto-char start)
2570 t)))
2571 (setq arg (1- arg))))
2572 ;; Search forward
2573 ((< arg 0)
2574 (end-of-line)
2575 (erlang-beginning-of-clause 1)
2576 ;; Step -arg functions forward.
2577 (while (and (< arg 0)
2578 ;; Step one function forward, or stop if the end of
2579 ;; the buffer was reached. Return t if we found the
2580 ;; function.
2581 (let ((name (erlang-name-of-function))
2582 (arity (erlang-get-function-arity))
2583 (found (erlang-beginning-of-clause -1)))
2584 (while (and found
2585 (string-equal name (erlang-name-of-function))
2586 arity
2587 (equal arity
2588 (erlang-get-function-arity)))
2589 (setq found (erlang-beginning-of-clause -1)))
2590 found))
2591 (setq arg (1+ arg)))))
2592 (zerop arg))
2595 (defun erlang-end-of-function (&optional arg)
2596 "Move forward to next end of function.
2598 With argument, do this that many times.
2599 With negative argument go towards the beginning of the buffer."
2600 (interactive "p")
2601 (or arg (setq arg 1))
2602 (let ((first t))
2603 ;; Forward
2604 (while (and (> arg 0) (< (point) (point-max)))
2605 (let ((pos (point)))
2606 (while (progn
2607 (if (and first
2608 (progn
2609 (forward-char 1)
2610 (erlang-beginning-of-clause 1)))
2612 (or (bobp) (forward-char -1))
2613 (erlang-beginning-of-clause -1))
2614 (setq first nil)
2615 (erlang-pass-over-function)
2616 (skip-chars-forward " \t")
2617 (if (looking-at "[%\n]")
2618 (forward-line 1))
2619 (<= (point) pos))))
2620 (setq arg (1- arg)))
2621 ;; Backward
2622 (while (< arg 0)
2623 (let ((pos (point)))
2624 (erlang-beginning-of-clause 1)
2625 (erlang-pass-over-function)
2626 (forward-line 1)
2627 (if (>= (point) pos)
2628 (if (erlang-beginning-of-function 2)
2629 (progn
2630 (erlang-pass-over-function)
2631 (skip-chars-forward " \t")
2632 (if (looking-at "[%\n]")
2633 (forward-line 1)))
2634 (goto-char (point-min)))))
2635 (setq arg (1+ arg)))))
2637 (eval-and-compile
2638 (if (default-boundp 'beginning-of-defun-function)
2639 (defalias 'erlang-mark-function 'mark-defun)
2640 (defun erlang-mark-function ()
2641 "Put mark at end of function, point at beginning."
2642 (interactive)
2643 (push-mark (point))
2644 (erlang-end-of-function 1)
2645 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2646 ;; the region.
2647 (condition-case nil
2648 (push-mark (point) nil t)
2649 (error (push-mark (point))))
2650 (erlang-beginning-of-function 1)
2651 ;; The above function deactivates the mark.
2652 (if (boundp 'deactivate-mark)
2653 (funcall (symbol-function 'set) 'deactivate-mark nil)))))
2655 (defun erlang-pass-over-function ()
2656 (while (progn
2657 (erlang-skip-blank)
2658 (and (not (looking-at "\\.\\(\\s \\|\n\\|\\s<\\)"))
2659 (not (eobp))))
2660 (forward-sexp 1))
2661 (if (not (eobp))
2662 (forward-char 1)))
2664 (defun erlang-name-of-function ()
2665 (save-excursion
2666 ;; Skip over attribute leader.
2667 (if (looking-at "-[ \t]*")
2668 (re-search-forward "-[ \t]*" nil 'move))
2669 (let ((start (point)))
2670 (forward-sexp 1)
2671 (buffer-substring start (point)))))
2674 ;;; Miscellaneous
2676 (defun erlang-fill-paragraph (&optional justify)
2677 "Like \\[fill-paragraph], but handle Erlang comments.
2678 If any of the current line is a comment, fill the comment or the
2679 paragraph of it that point is in, preserving the comment's indentation
2680 and initial `%':s."
2681 (interactive "P")
2682 (let ((has-comment nil)
2683 ;; If has-comment, the appropriate fill-prefix for the comment.
2684 comment-fill-prefix)
2685 ;; Figure out what kind of comment we are looking at.
2686 (save-excursion
2687 (beginning-of-line)
2688 (cond
2689 ;; Find the command prefix.
2690 ((looking-at (concat "\\s *" comment-start-skip))
2691 (setq has-comment t)
2692 (setq comment-fill-prefix (buffer-substring (match-beginning 0)
2693 (match-end 0))))
2694 ;; A line with some code, followed by a comment? Remember that the
2695 ;; % which starts the comment shouldn't be part of a string or
2696 ;; character.
2697 ((progn
2698 (while (not (looking-at "%\\|$"))
2699 (skip-chars-forward "^%\n\"\\\\")
2700 (cond
2701 ((eq (char-after (point)) ?\\) (forward-char 2))
2702 ((eq (char-after (point)) ?\") (forward-sexp 1))))
2703 (looking-at comment-start-skip))
2704 (setq has-comment t)
2705 (setq comment-fill-prefix
2706 (concat (make-string (current-column) ? )
2707 (buffer-substring (match-beginning 0) (match-end 0)))))))
2708 (if (not has-comment)
2709 (fill-paragraph justify)
2710 ;; Narrow to include only the comment, and then fill the region.
2711 (save-restriction
2712 (narrow-to-region
2713 ;; Find the first line we should include in the region to fill.
2714 (save-excursion
2715 (while (and (zerop (forward-line -1))
2716 (looking-at "^\\s *%")))
2717 ;; We may have gone to far. Go forward again.
2718 (or (looking-at "^\\s *%")
2719 (forward-line 1))
2720 (point))
2721 ;; Find the beginning of the first line past the region to fill.
2722 (save-excursion
2723 (while (progn (forward-line 1)
2724 (looking-at "^\\s *%")))
2725 (point)))
2726 ;; Lines with only % on them can be paragraph boundaries.
2727 (let ((paragraph-start (concat paragraph-start "\\|^[ \t%]*$"))
2728 (paragraph-separate (concat paragraph-start "\\|^[ \t%]*$"))
2729 (fill-prefix comment-fill-prefix))
2730 (fill-paragraph justify))))))
2733 (defun erlang-uncomment-region (beg end)
2734 "Uncomment all commented lines in the region."
2735 (interactive "r")
2736 (comment-region beg end -1))
2739 (defun erlang-generate-new-clause ()
2740 "Create additional Erlang clause header.
2742 Parses the source file for the name of the current Erlang function.
2743 Create the header containing the name, A pair of parentheses,
2744 and an arrow. The space between the function name and the
2745 first parenthesis is preserved. The point is placed between
2746 the parentheses."
2747 (interactive)
2748 (let ((name (save-excursion
2749 (and (erlang-beginning-of-clause)
2750 (erlang-get-function-name t))))
2751 (arrow (save-excursion
2752 (and (erlang-beginning-of-clause)
2753 (erlang-get-function-arrow)))))
2754 (if (or (null arrow) (null name))
2755 (error "Can't find name of current Erlang function"))
2756 (if (and (bolp) (eolp))
2758 (end-of-line)
2759 (newline))
2760 (insert name)
2761 (save-excursion
2762 (insert ") " arrow))
2763 (if erlang-new-clause-with-arguments
2764 (erlang-clone-arguments))))
2767 (defun erlang-clone-arguments ()
2768 "Insert, at the point, the argument list of the previous clause.
2770 The mark is set at the beginning of the inserted text, the point
2771 at the end."
2772 (interactive)
2773 (let ((args (save-excursion
2774 (beginning-of-line)
2775 (and (erlang-beginning-of-clause)
2776 (erlang-get-function-arguments))))
2777 (p (point)))
2778 (if (null args)
2779 (error "Can't clone argument list"))
2780 (insert args)
2781 (set-mark p)))
2783 ;;; Information retrieval functions.
2785 (defun erlang-buffer-substring (beg end)
2786 "Like `buffer-substring-no-properties'.
2787 Although, this function works on all versions of Emacs."
2788 (if (fboundp 'buffer-substring-no-properties)
2789 (funcall (symbol-function 'buffer-substring-no-properties) beg end)
2790 (buffer-substring beg end)))
2793 (defun erlang-get-module ()
2794 "Return the name of the module as specified by `-module'.
2796 Return nil if file contains no `-module' attribute."
2797 (save-excursion
2798 (save-restriction
2799 (widen)
2800 (goto-char (point-min))
2801 (let ((md (match-data)))
2802 (unwind-protect
2803 (if (re-search-forward
2804 (eval-when-compile
2805 (concat "^-module\\s *(\\s *\\(\\("
2806 erlang-atom-regexp
2807 "\\)?\\)\\s *)\\s *\\."))
2808 (point-max) t)
2809 (erlang-remove-quotes
2810 (erlang-buffer-substring (match-beginning 1)
2811 (match-end 1)))
2812 nil)
2813 (store-match-data md))))))
2816 (defun erlang-get-module-from-file-name (&optional file)
2817 "Extract the module name from a file name.
2819 First, the directory part is removed. Second, the part of the file name
2820 matching `erlang-file-name-extension-regexp' is removed.
2822 Should the match fail, nil is returned.
2824 By modifying `erlang-file-name-extension-regexp' to match files other
2825 than Erlang source files, Erlang specific functions could be applied on
2826 non-Erlang files. Most notably; the support for Erlang modules in the
2827 tags system could be used by files written in other languages."
2828 (or file (setq file buffer-file-name))
2829 (if (null file)
2831 (setq file (file-name-nondirectory file))
2832 (if (string-match erlang-file-name-extension-regexp file)
2833 (substring file 0 (match-beginning 0))
2834 nil)))
2837 ;; Used by `erlang-get-export' and `erlang-get-import'.
2839 (defun erlang-get-function-arity-list ()
2840 "Parse list of `function/arity' as used by `-import' and `-export'.
2842 Point must be before the opening bracket. When the
2843 function returns the point will be placed after the closing bracket.
2845 The function does not return an error if the list is incorrectly
2846 formatted.
2848 Return list of (function . arity). The order of the returned list
2849 corresponds to the order of the parsed Erlang list."
2850 (let ((res '()))
2851 (erlang-skip-blank)
2852 (forward-char 1)
2853 (if (not (eq (preceding-char) ?\[))
2854 '() ; Not looking at an Erlang list.
2855 (while ; Note: `while' has no body.
2856 (progn
2857 (erlang-skip-blank)
2858 (and (looking-at (eval-when-compile
2859 (concat erlang-atom-regexp "/\\([0-9]+\\)\\>")))
2860 (progn
2861 (setq res (cons
2862 (cons
2863 (erlang-remove-quotes
2864 (erlang-buffer-substring
2865 (match-beginning 1) (match-end 1)))
2866 (string-to-int
2867 (erlang-buffer-substring
2868 (match-beginning
2869 (+ 1 erlang-atom-regexp-matches))
2870 (match-end
2871 (+ 1 erlang-atom-regexp-matches)))))
2872 res))
2873 (goto-char (match-end 0))
2874 (erlang-skip-blank)
2875 (forward-char 1)
2876 ;; Test if there are more exported functions.
2877 (eq (preceding-char) ?,))))))
2878 (nreverse res)))
2881 ;;; Note that `-export' and the open parenthesis must be written on
2882 ;;; the same line.
2884 (defun erlang-get-export ()
2885 "Return a list of `(function . arity)' as specified by `-export'."
2886 (save-excursion
2887 (goto-char (point-min))
2888 (let ((md (match-data))
2889 (res '()))
2890 (unwind-protect
2891 (progn
2892 (while (re-search-forward "^-export\\s *(" (point-max) t)
2893 (erlang-skip-blank)
2894 (setq res (nconc res (erlang-get-function-arity-list))))
2895 res)
2896 (store-match-data md)))))
2899 (defun erlang-get-import ()
2900 "Parse an Erlang source file for imported functions.
2902 Return an alist with module name as car part and list of conses containing
2903 function and arity as cdr part."
2904 (save-excursion
2905 (goto-char (point-min))
2906 (let ((md (match-data))
2907 (res '()))
2908 (unwind-protect
2909 (progn
2910 (while (re-search-forward "^-import\\s *(" (point-max) t)
2911 (erlang-skip-blank)
2912 (if (looking-at erlang-atom-regexp)
2913 (let ((module (erlang-remove-quotes
2914 (erlang-buffer-substring
2915 (match-beginning 0)
2916 (match-end 0)))))
2917 (goto-char (match-end 0))
2918 (erlang-skip-blank)
2919 (if (eq (following-char) ?,)
2920 (progn
2921 (forward-char 1)
2922 (erlang-skip-blank)
2923 (let ((funcs (erlang-get-function-arity-list))
2924 (pair (assoc module res)))
2925 (if pair
2926 (setcdr pair (nconc (cdr pair) funcs))
2927 (setq res (cons (cons module funcs)
2928 res)))))))))
2929 (nreverse res))
2930 (store-match-data md)))))
2933 (defun erlang-get-function-name (&optional arg)
2934 "Return name of current function, or nil.
2936 If optional argument is non-nil, everything up to and including
2937 the first `(' is returned.
2939 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2940 (save-excursion
2941 (if (not (eobp)) (forward-char 1))
2942 (and (erlang-beginning-of-clause)
2943 (erlang-get-function-name t)))"
2944 (let ((n (if arg 0 1)))
2945 (and (looking-at (eval-when-compile
2946 (concat "^" erlang-atom-regexp "\\s *(")))
2947 (erlang-buffer-substring (match-beginning n) (match-end n)))))
2949 (defun erlang-get-function-name-and-arity ()
2950 "Return name and arity of current function (e.g. \"foo/1\"), or nil."
2951 (when (looking-at (eval-when-compile
2952 (concat "^" erlang-atom-regexp "\\s *(")))
2953 (let ((name (erlang-buffer-substring (match-beginning 1) (match-end 1)))
2954 (arity 0))
2955 (save-excursion
2956 (goto-char (match-end 0))
2957 (while (ignore-errors
2958 (skip-syntax-forward "-")
2959 (forward-sexp 1)
2960 (skip-syntax-forward "-")
2962 (when (looking-at "[,)]")
2963 (incf arity))))
2964 (format "%s/%d" name arity))))
2966 (defun erlang-get-function-arrow ()
2967 "Return arrow of current function, could be \"->\", \":-\" or nil.
2969 The \":-\" arrow is used by mnesia queries.
2971 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2972 (save-excursion
2973 (if (not (eobp)) (forward-char 1))
2974 (and (erlang-beginning-of-clause)
2975 (erlang-get-function-arrow)))"
2976 (and
2977 (save-excursion
2978 (re-search-forward "[^-:]*-\\|:" (point-max) t)
2979 (erlang-buffer-substring (- (point) 1) (+ (point) 1)))))
2981 (defun erlang-get-function-arity ()
2982 "Return the number of arguments of function at point, or nil."
2983 (and (looking-at (eval-when-compile
2984 (concat "^" erlang-atom-regexp "\\s *(")))
2985 (save-excursion
2986 (goto-char (match-end 0))
2987 (condition-case nil
2988 (let ((res 0)
2989 (cont t))
2990 (while cont
2991 (cond ((eobp)
2992 (setq res nil)
2993 (setq cont nil))
2994 ((looking-at "\\s *)")
2995 (setq cont nil))
2996 ((looking-at "\\s *\\($\\|%\\)")
2997 (forward-line 1))
2998 ((looking-at "\\s *,")
2999 (setq res (+ 1 res))
3000 (goto-char (match-end 0)))
3002 (when (zerop res)
3003 (setq res (+ 1 res)))
3004 (forward-sexp 1))))
3005 res)
3006 (error nil)))))
3008 (defun erlang-get-function-arguments ()
3009 "Return arguments of current function, or nil."
3010 (if (not (looking-at (eval-when-compile
3011 (concat "^" erlang-atom-regexp "\\s *("))))
3013 (save-excursion
3014 (condition-case nil
3015 (let ((start (match-end 0)))
3016 (goto-char (- start 1))
3017 (forward-sexp)
3018 (erlang-buffer-substring start (- (point) 1)))
3019 (error nil)))))
3022 (defun erlang-get-function-under-point ()
3023 "Return the module and function under the point, or nil.
3025 Should no explicit module name be present at the point, the
3026 list of imported functions is searched.
3028 The following could be returned:
3029 (\"module\" \"function\") -- Both module and function name found.
3030 (nil \"function\") -- No module name was found.
3031 nil -- No function name found
3033 In the future the list may contain more elements."
3034 (save-excursion
3035 (let ((md (match-data))
3036 (res nil))
3037 (if (eq (char-syntax (following-char)) ? )
3038 (skip-chars-backward " \t"))
3039 (skip-chars-backward "a-zA-Z0-9_:'")
3040 (cond ((looking-at (eval-when-compile
3041 (concat erlang-atom-regexp ":" erlang-atom-regexp)))
3042 (setq res (list
3043 (erlang-remove-quotes
3044 (erlang-buffer-substring
3045 (match-beginning 1) (match-end 1)))
3046 (erlang-remove-quotes
3047 (erlang-buffer-substring
3048 (match-beginning (1+ erlang-atom-regexp-matches))
3049 (match-end (1+ erlang-atom-regexp-matches)))))))
3050 ((looking-at erlang-atom-regexp)
3051 (let ((fk (erlang-remove-quotes
3052 (erlang-buffer-substring
3053 (match-beginning 0) (match-end 0))))
3054 (mod nil)
3055 (imports (erlang-get-import)))
3056 (while (and imports (null mod))
3057 (if (assoc fk (cdr (car imports)))
3058 (setq mod (car (car imports)))
3059 (setq imports (cdr imports))))
3060 (setq res (list mod fk)))))
3061 (store-match-data md)
3062 res)))
3065 ;; TODO: Escape single quotes inside the string without
3066 ;; replace-regexp-in-string.
3067 (defun erlang-add-quotes-if-needed (str)
3068 "Return STR, possibly with quotes."
3069 (if (and (stringp str)
3070 (not (string-match (eval-when-compile
3071 (concat "\\`" erlang-atom-regexp "\\'")) str)))
3072 (progn (if (fboundp 'replace-regexp-in-string)
3073 (setq str (replace-regexp-in-string "'" "\\'" str t t )))
3074 (concat "'" str "'"))
3075 str))
3078 (defun erlang-remove-quotes (str)
3079 "Return STR without quotes, if present."
3080 (let ((md (match-data)))
3081 (prog1
3082 (if (string-match "\\`'\\(.*\\)'\\'" str)
3083 (substring str 1 -1)
3084 str)
3085 (store-match-data md))))
3088 ;;; Check module name
3090 ;; The function `write-file', bound to C-x C-w, calls
3091 ;; `set-visited-file-name' which clears the hook. :-(
3092 ;; To make sure that the hook always is present, we advise
3093 ;; `set-visited-file-name'.
3094 (defun erlang-check-module-name-init ()
3095 "Initialize the functionality to compare file and module names.
3097 Unless we have `before-save-hook', we redefine the function
3098 `set-visited-file-name' since it clears the variable
3099 `local-write-file-hooks'. The original function definition is
3100 stored in `erlang-orig-set-visited-file-name'."
3101 (if (boundp 'before-save-hook)
3102 ;; If we have that, `make-local-hook' is obsolete.
3103 (add-hook 'before-save-hook 'erlang-check-module-name nil t)
3104 (require 'advice)
3105 (unless (ad-advised-definition-p 'set-visited-file-name)
3106 (defadvice set-visited-file-name (after erlang-set-visited-file-name
3107 activate)
3108 (if (eq major-mode 'erlang-mode)
3109 (add-hook 'local-write-file-hooks 'erlang-check-module-name))))
3110 (add-hook 'local-write-file-hooks 'erlang-check-module-name)))
3113 (defun erlang-check-module-name ()
3114 "If the module name doesn't match file name, ask for permission to change.
3116 The variable `erlang-check-module-name' controls the behaviour of this
3117 function. It it is nil, this function does nothing. If it is t, the
3118 source is silently changed. If it is set to the atom `ask', the user
3119 is prompted.
3121 This function is normally placed in the hook `local-write-file-hooks'."
3122 (if erlang-check-module-name
3123 (let ((mn (erlang-get-module))
3124 (fn (erlang-get-module-from-file-name (buffer-file-name))))
3125 (if (and (stringp mn) (stringp fn))
3126 (or (string-equal mn fn)
3127 (if (or (eq erlang-check-module-name t)
3128 (y-or-n-p
3129 "Module does not match file name. Modify source? "))
3130 (save-excursion
3131 (save-restriction
3132 (widen)
3133 (goto-char (point-min))
3134 (if (re-search-forward
3135 (eval-when-compile
3136 (concat "^-module\\s *(\\s *\\(\\("
3137 erlang-atom-regexp
3138 "\\)?\\)\\s *)\\s *\\."))
3139 (point-max) t)
3140 (progn
3141 (goto-char (match-beginning 1))
3142 (delete-region (match-beginning 1)
3143 (match-end 1))
3144 (insert fn))))))))))
3145 ;; Must return nil since it is added to `local-write-file-hook'.
3146 nil)
3149 ;;; Electric functions.
3151 (defun erlang-electric-semicolon (&optional arg)
3152 "Insert a semicolon character and possibly a prototype for the next line.
3154 The variable `erlang-electric-semicolon-criteria' states a criterion,
3155 when fulfilled a newline is inserted, the next line is indented and a
3156 prototype for the next line is inserted. Normally the prototype
3157 consists of \" ->\". Should the semicolon end the clause a new clause
3158 header is generated.
3160 The variable `erlang-electric-semicolon-insert-blank-lines' controls
3161 the number of blank lines inserted between the current line and new
3162 function header.
3164 Behaves just like the normal semicolon when supplied with a
3165 numerical arg, point is inside string or comment, or when there are
3166 non-whitespace characters following the point on the current line."
3167 (interactive "P")
3168 (self-insert-command (prefix-numeric-value arg))
3169 (if (or arg
3170 (and (listp erlang-electric-commands)
3171 (not (memq 'erlang-electric-semicolon
3172 erlang-electric-commands)))
3173 (erlang-in-literal)
3174 (not (looking-at "\\s *\\(%.*\\)?$"))
3175 (null (erlang-test-criteria-list
3176 erlang-electric-semicolon-criteria)))
3177 (setq erlang-electric-newline-inhibit nil)
3178 (setq erlang-electric-newline-inhibit t)
3179 (undo-boundary)
3180 (end-of-line)
3181 (newline)
3182 (if (condition-case nil
3183 (progn (erlang-indent-line) t)
3184 (error (if (bolp) (delete-backward-char 1))))
3185 (if (not (bolp))
3186 (save-excursion
3187 (insert " ->"))
3188 (condition-case nil
3189 (progn
3190 (erlang-generate-new-clause)
3191 (if erlang-electric-semicolon-insert-blank-lines
3192 (save-excursion
3193 (beginning-of-line)
3194 (newline
3195 erlang-electric-semicolon-insert-blank-lines))))
3196 (error (if (bolp) (delete-backward-char 1))))))))
3199 (defun erlang-electric-comma (&optional arg)
3200 "Insert a comma character and possibly a new indented line.
3201 The variable `erlang-electric-comma-criteria' states a criterion,
3202 when fulfilled a newline is inserted and the next line is indented.
3204 Behaves just like the normal comma when supplied with a
3205 numerical arg, point is inside string or comment, or when there are
3206 non-whitespace characters following the point on the current line."
3207 (interactive "P")
3209 (self-insert-command (prefix-numeric-value arg))
3211 (if (or arg
3212 (and (listp erlang-electric-commands)
3213 (not (memq 'erlang-electric-comma erlang-electric-commands)))
3214 (erlang-in-literal)
3215 (not (looking-at "\\s *\\(%.*\\)?$"))
3216 (null (erlang-test-criteria-list
3217 erlang-electric-comma-criteria)))
3218 (setq erlang-electric-newline-inhibit nil)
3219 (setq erlang-electric-newline-inhibit t)
3220 (undo-boundary)
3221 (end-of-line)
3222 (newline)
3223 (condition-case nil
3224 (erlang-indent-line)
3225 (error (if (bolp) (delete-backward-char 1))))))
3227 (defun erlang-electric-lt (&optional arg)
3228 "Insert a less-than sign, and optionally mark it as an open paren."
3230 (interactive "p")
3232 (self-insert-command arg)
3234 ;; Was this the second char in bit-syntax open (`<<')?
3235 (unless (< (point) 2)
3236 (save-excursion
3237 (backward-char 2)
3238 (when (and (eq (char-after (point)) ?<)
3239 (not (eq (get-text-property (point) 'category)
3240 'bitsyntax-open-inner)))
3241 ;; Then mark the two chars...
3242 (put-text-property (point) (1+ (point))
3243 'category 'bitsyntax-open-outer)
3244 (forward-char 1)
3245 (put-text-property (point) (1+ (point))
3246 'category 'bitsyntax-open-inner)
3247 ;;...and unmark any subsequent less-than chars.
3248 (forward-char 1)
3249 (while (eq (char-after (point)) ?<)
3250 (remove-text-properties (point) (1+ (point))
3251 '(category nil))
3252 (forward-char 1))))))
3254 (defun erlang-after-bitsyntax-close ()
3255 "Return t if point is immediately after a bit-syntax close parenthesis (`>>')."
3256 (and (>= (point) 2)
3257 (save-excursion
3258 (backward-char 2)
3259 (and (eq (char-after (point)) ?>)
3260 (not (eq (get-text-property (point) 'category)
3261 'bitsyntax-close-outer))))))
3263 (defun erlang-after-arrow ()
3264 "Return true if point is immediately after a function arrow (`->')."
3265 (and (>= (point) 2)
3266 (and
3267 (save-excursion
3268 (backward-char)
3269 (eq (char-before (point)) ?-))
3270 (or (not (listp erlang-electric-commands))
3271 (memq 'erlang-electric-gt
3272 erlang-electric-commands))
3273 (not (erlang-in-literal))
3274 (looking-at "\\s *\\(%.*\\)?$")
3275 (erlang-test-criteria-list erlang-electric-arrow-criteria))))
3278 (defun erlang-electric-gt (&optional arg)
3279 "Insert a greater-than sign, and optionally mark it as a close paren."
3281 (interactive "p")
3283 (self-insert-command arg)
3285 (cond
3286 ;; Did we just write a bit-syntax close (`>>')?
3287 ((erlang-after-bitsyntax-close)
3288 (save-excursion
3289 ;; Then mark the two chars...
3290 (backward-char 2)
3291 (put-text-property (point) (1+ (point))
3292 'category 'bitsyntax-close-inner)
3293 (forward-char)
3294 (put-text-property (point) (1+ (point))
3295 'category 'bitsyntax-close-outer)
3296 ;;...and unmark any subsequent greater-than chars.
3297 (forward-char)
3298 (while (eq (char-after (point)) ?>)
3299 (remove-text-properties (point) (1+ (point))
3300 '(category nil))
3301 (forward-char))))
3303 ;; Did we just write a function arrow (`->')?
3304 ((erlang-after-arrow)
3305 (let ((erlang-electric-newline-inhibit t))
3306 (undo-boundary)
3307 (end-of-line)
3308 (newline)
3309 (condition-case nil
3310 (erlang-indent-line)
3311 (error (if (bolp) (delete-backward-char 1))))))
3313 ;; Then it's just a plain greater-than.
3315 nil)))
3318 (defun erlang-electric-arrow\ off (&optional arg)
3319 "Insert a '>'-sign and possibly a new indented line.
3321 This command is only `electric' when the `>' is part of an `->' arrow.
3322 The variable `erlang-electric-arrow-criteria' states a sequence of
3323 criteria, which decides when a newline should be inserted and the next
3324 line indented.
3326 It behaves just like the normal greater than sign when supplied with a
3327 numerical arg, point is inside string or comment, or when there are
3328 non-whitespace characters following the point on the current line.
3330 After being split/merged into `erlang-after-arrow' and
3331 `erlang-electric-gt', it is now unused and disabled."
3332 (interactive "P")
3333 (let ((prec (preceding-char)))
3334 (self-insert-command (prefix-numeric-value arg))
3335 (if (or arg
3336 (and (listp erlang-electric-commands)
3337 (not (memq 'erlang-electric-arrow
3338 erlang-electric-commands)))
3339 (not (eq prec ?-))
3340 (erlang-in-literal)
3341 (not (looking-at "\\s *\\(%.*\\)?$"))
3342 (null (erlang-test-criteria-list
3343 erlang-electric-arrow-criteria)))
3344 (setq erlang-electric-newline-inhibit nil)
3345 (setq erlang-electric-newline-inhibit t)
3346 (undo-boundary)
3347 (end-of-line)
3348 (newline)
3349 (condition-case nil
3350 (erlang-indent-line)
3351 (error (if (bolp) (delete-backward-char 1)))))))
3354 (defun erlang-electric-newline (&optional arg)
3355 "Break line at point and indent, continuing comment if within one.
3356 The variable `erlang-electric-newline-criteria' states a criterion,
3357 when fulfilled a newline is inserted and the next line is indented.
3359 Should the current line begin with a comment, and the variable
3360 `comment-multi-line' be non-nil, a new comment start is inserted.
3362 Should the previous command be another electric command we assume that
3363 the user pressed newline out of old habit, hence we will do nothing."
3364 (interactive "P")
3365 (cond ((and (not arg)
3366 erlang-electric-newline-inhibit
3367 (memq last-command erlang-electric-newline-inhibit-list))
3368 ()) ; Do nothing!
3369 ((or arg
3370 (and (listp erlang-electric-commands)
3371 (not (memq 'erlang-electric-newline
3372 erlang-electric-commands)))
3373 (null (erlang-test-criteria-list
3374 erlang-electric-newline-criteria)))
3375 (newline (prefix-numeric-value arg)))
3377 (if (and comment-multi-line
3378 (save-excursion
3379 (beginning-of-line)
3380 (looking-at (concat "\\s *" comment-start-skip))))
3381 (let ((str (buffer-substring
3382 (or (match-end 1) (match-beginning 0))
3383 (min (match-end 0) (point)))))
3384 (newline)
3385 (undo-boundary)
3386 (insert str))
3387 (newline-and-indent)))))
3390 (defun erlang-test-criteria-list (criteria)
3391 "Given a list of criterion functions, test if criteria are fulfilled.
3393 Each element in the criteria list can a function returning nil, t or
3394 the atom `stop'. t means that the criterion is fulfilled, `stop' means
3395 that it isn't fulfilled and that the search should stop,
3396 and nil means continue searching.
3398 Should the list contain the atom t the criterion is assumed to be
3399 fulfilled, unless preceded by a function returning `stop', of course.
3401 Should the argument be the atom t instead of a list, the criterion is
3402 assumed to be trivially true.
3404 Should all functions return nil, the criteria are assumed not to be
3405 fulfilled.
3407 Return t if criteria fulfilled, nil otherwise."
3408 (if (eq criteria t)
3410 (save-excursion
3411 (let ((answer nil))
3412 (while (and criteria (null answer))
3413 (if (eq (car criteria) t)
3414 (setq answer t)
3415 (setq answer (funcall (car criteria))))
3416 (setq criteria (cdr criteria)))
3417 (if (and answer (not (eq answer 'stop)))
3419 nil)))))
3422 (defun erlang-in-literal (&optional lim)
3423 "Test if point is in string, quoted atom or comment.
3425 Return one of the three atoms `atom', `string', and `comment'.
3426 Should the point be inside none of the above mentioned types of
3427 context, nil is returned."
3428 (save-excursion
3429 (let* ((lim (or lim (save-excursion
3430 (erlang-beginning-of-clause)
3431 (point))))
3432 (state (if (fboundp 'syntax-ppss) ; post Emacs 21.3
3433 (syntax-ppss)
3434 (parse-partial-sexp lim (point)))))
3435 (cond
3436 ((eq (nth 3 state) ?') 'atom)
3437 ((nth 3 state) 'string)
3438 ((nth 4 state) 'comment)
3439 (t nil)))))
3442 (defun erlang-at-end-of-function-p ()
3443 "Test if point is at end of an Erlang function.
3445 This function is designed to be a member of a criteria list."
3446 (eq (save-excursion (erlang-skip-blank) (point))
3447 (save-excursion
3448 (erlang-beginning-of-function -1) (point))))
3451 (defun erlang-at-end-of-clause-p ()
3452 "Test if point is at end of an Erlang clause.
3454 This function is designed to be a member of a criteria list."
3455 (eq (save-excursion (erlang-skip-blank) (point))
3456 (save-excursion
3457 (erlang-beginning-of-clause -1) (point))))
3460 (defun erlang-stop-when-inside-argument-list ()
3461 "Return `stop' if inside parenthesis list, nil otherwise.
3463 Knows about the list comprehension syntax. When the point is
3464 after `||', `stop' is not returned.
3466 This function is designed to be a member of a criteria list."
3467 (save-excursion
3468 (condition-case nil
3469 (let ((orig-point (point))
3470 (state nil))
3471 (up-list -1)
3472 (if (not (eq (following-char) ?\[))
3473 'stop
3474 ;; Do not return `stop' when inside a list comprehension
3475 ;; construction. (The point must be after `||').
3476 (while (< (point) orig-point)
3477 (setq state (erlang-partial-parse (point) orig-point state)))
3478 (if (and (car state) (eq (car (car (car state))) '||))
3480 'stop)))
3481 (error
3482 nil))))
3485 (defun erlang-stop-when-at-guard ()
3486 "Return `stop' when at function guards.
3488 This function is designed to be a member of a criteria list."
3489 (save-excursion
3490 (beginning-of-line)
3491 (if (and (looking-at (eval-when-compile
3492 (concat "^" erlang-atom-regexp "\\s *(")))
3493 (not (looking-at
3494 (eval-when-compile
3495 (concat "^" erlang-atom-regexp ".*\\(->\\|:-\\)")))))
3496 'stop
3497 nil)))
3500 (defun erlang-next-lines-empty-p ()
3501 "Return non-nil if next lines are empty.
3503 The variable `erlang-next-lines-empty-threshold' contains the number
3504 of lines required to be empty.
3506 A line containing only spaces and tabs is considered empty.
3508 This function is designed to be a member of a criteria list."
3509 (and erlang-next-lines-empty-threshold
3510 (save-excursion
3511 (let ((left erlang-next-lines-empty-threshold)
3512 (cont t))
3513 (while (and cont (> left 0))
3514 (forward-line 1)
3515 (setq cont (looking-at "\\s *$"))
3516 (setq left (- left 1)))
3517 cont))))
3520 (defun erlang-at-keyword-end-p ()
3521 "Test if next readable token is the keyword end.
3523 This function is designed to be a member of a criteria list."
3524 (save-excursion
3525 (erlang-skip-blank)
3526 (looking-at "end[^_a-zA-Z0-9]")))
3529 ;; Erlang tags support which is aware of erlang modules.
3531 ;; Not yet implemented under XEmacs. (Hint: The Emacs 19 etags
3532 ;; package works under XEmacs.)
3534 (eval-when-compile
3535 (if (or (featurep 'bytecomp)
3536 (featurep 'byte-compile))
3537 (progn
3538 (require 'etags))))
3541 ;; Variables:
3543 (defvar erlang-tags-function-alist
3544 '((find-tag . erlang-find-tag)
3545 (find-tag-other-window . erlang-find-tag-other-window)
3546 (find-tag-regexp . erlang-find-tag-regexp)
3547 (find-tag-other-frame . erlang-find-tag-other-frame))
3548 "Alist of old tags commands and the replacement functions.")
3550 (defvar erlang-tags-installed nil
3551 "Non-nil when the Erlang tags system is installed.")
3552 (defvar erlang-tags-file-list '()
3553 "List of files in tag list. Used when finding tag on form `module:'.")
3554 (defvar erlang-tags-completion-table nil
3555 "Like `tags-completion-table', this table contains `tag' and `module:tag'.")
3556 (defvar erlang-tags-buffer-installed-p nil
3557 "Non-nil when Erlang module recognising functions installed.")
3558 (defvar erlang-tags-buffer-list '()
3559 "Temporary list of buffers.")
3560 (defvar erlang-tags-orig-completion-table nil
3561 "Temporary storage for `tags-completion-table'.")
3562 (defvar erlang-tags-orig-tag-order nil
3563 "Temporary storage for `find-tag-tag-order'.")
3564 (defvar erlang-tags-orig-regexp-tag-order nil
3565 "Temporary storage for `find-tag-regexp-tag-order'.")
3566 (defvar erlang-tags-orig-search-function nil
3567 "Temporary storage for `find-tag-search-function'.")
3568 (defvar erlang-tags-orig-regexp-search-function nil
3569 "Temporary storage for `find-tag-regexp-search-function'.")
3570 (defvar erlang-tags-orig-format-hooks nil
3571 "Temporary storage for `tags-table-format-hooks'.") ;v19
3572 (defvar erlang-tags-orig-format-functions nil
3573 "Temporary storage for `tags-table-format-functions'.") ;v > 19
3575 (defun erlang-tags-init ()
3576 "Install an alternate version of tags, aware of Erlang modules.
3578 After calling this function, the tags functions are aware of
3579 Erlang modules. Tags can be entered on the for `module:tag' as well
3580 as on the old form `tag'.
3582 In the completion list, `module:tag' and `module:' shows up.
3584 Call this function from an appropriate init file, or add it to
3585 Erlang mode hook with the commands:
3586 (add-hook 'erlang-mode-hook 'erlang-tags-init)
3587 (add-hook 'erlang-shell-mode-hook 'erlang-tags-init)
3589 This function only works under Emacs 18 and Emacs 19. Currently, It
3590 is not implemented under XEmacs. (Hint: The Emacs 19 etags module
3591 works under XEmacs.)"
3592 (interactive)
3593 (cond ((= erlang-emacs-major-version 18)
3594 (require 'tags)
3595 (erlang-tags-define-keys (current-local-map))
3596 (setq erlang-tags-installed t))
3598 (require 'etags)
3599 ;; Test on a function available in the Emacs 19 version
3600 ;; of tags but not in the XEmacs version.
3601 (if (not (fboundp 'find-tag-noselect))
3603 (erlang-tags-define-keys (current-local-map))
3604 (setq erlang-tags-installed t)))))
3607 ;; Set all keys bound to `find-tag' et.al. in the global map and the
3608 ;; menu to `erlang-find-tag' et.al. in `map'.
3610 ;; The function `substitute-key-definition' does not work properly
3611 ;; in all version of Emacs.
3613 (defun erlang-tags-define-keys (map)
3614 "Bind tags commands to keymap MAP aware of Erlang modules."
3615 (let ((alist erlang-tags-function-alist))
3616 (while alist
3617 (let* ((old (car (car alist)))
3618 (new (cdr (car alist)))
3619 (keys (append (where-is-internal old global-map))))
3620 (while keys
3621 (define-key map (car keys) new)
3622 (setq keys (cdr keys))))
3623 (setq alist (cdr alist))))
3624 ;; Update the menu.
3625 (erlang-menu-substitute erlang-menu-base-items erlang-tags-function-alist)
3626 (erlang-menu-init))
3629 ;; There exists a variable `find-tag-default-function'. It is not used
3630 ;; since `complete-tag' uses it to get current word under point. In that
3631 ;; situation we don't want the module to be prepended.
3633 (defun erlang-find-tag-default ()
3634 "Return the default tag.
3635 Search `-import' list of imported functions.
3636 Single quotes are been stripped away."
3637 (let ((mod-func (erlang-get-function-under-point)))
3638 (cond ((null mod-func)
3639 nil)
3640 ((null (car mod-func))
3641 (nth 1 mod-func))
3643 (concat (car mod-func) ":" (nth 1 mod-func))))))
3646 ;; Return `t' since it is used inside `tags-loop-form'.
3647 ;;;###autoload
3648 (defun erlang-find-tag (modtagname &optional next-p regexp-p)
3649 "Like `find-tag'. Capable of retrieving Erlang modules.
3651 Tags can be given on the forms `tag', `module:', `module:tag'."
3652 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3653 (switch-to-buffer (erlang-find-tag-noselect modtagname next-p regexp-p))
3657 ;; Code mainly from `find-tag-other-window' in `etags.el'.
3658 ;;;###autoload
3659 (defun erlang-find-tag-other-window (tagname &optional next-p regexp-p)
3660 "Like `find-tag-other-window' but aware of Erlang modules."
3661 (interactive (erlang-tag-interactive
3662 "Find `module:tag' or `tag' other window: "))
3664 ;; This is to deal with the case where the tag is found in the
3665 ;; selected window's buffer; without this, point is moved in both
3666 ;; windows. To prevent this, we save the selected window's point
3667 ;; before doing find-tag-noselect, and restore it afterwards.
3668 (let* ((window-point (window-point (selected-window)))
3669 (tagbuf (erlang-find-tag-noselect tagname next-p regexp-p))
3670 (tagpoint (progn (set-buffer tagbuf) (point))))
3671 (set-window-point (prog1
3672 (selected-window)
3673 (switch-to-buffer-other-window tagbuf)
3674 ;; We have to set this new window's point; it
3675 ;; might already have been displaying a
3676 ;; different portion of tagbuf, in which case
3677 ;; switch-to-buffer-other-window doesn't set
3678 ;; the window's point from the buffer.
3679 (set-window-point (selected-window) tagpoint))
3680 window-point)))
3683 (defun erlang-find-tag-other-frame (tagname &optional next-p)
3684 "Like `find-tag-other-frame' but aware of Erlang modules."
3685 (interactive (erlang-tag-interactive
3686 "Find `module:tag' or `tag' other frame: "))
3687 (let ((pop-up-frames t))
3688 (erlang-find-tag-other-window tagname next-p)))
3691 (defun erlang-find-tag-regexp (regexp &optional next-p other-window)
3692 "Like `find-tag-regexp' but aware of Erlang modules."
3693 (interactive (if (fboundp 'find-tag-regexp)
3694 (erlang-tag-interactive
3695 "Find `module:regexp' or `regexp': ")
3696 (error "This version of Emacs can't find tags by regexps")))
3697 (funcall (if other-window
3698 'erlang-find-tag-other-window
3699 'erlang-find-tag)
3700 regexp next-p t))
3703 ;; Just like C-u M-. This could be added to the menu.
3704 (defun erlang-find-next-tag ()
3705 "Find next tag, like \\[find-tag] with prefix arg."
3706 (interactive)
3707 (let ((current-prefix-arg '(4)))
3708 (if erlang-tags-installed
3709 (call-interactively 'erlang-find-tag)
3710 (call-interactively 'find-tag))))
3713 ;; Mimics `find-tag-noselect' found in `etags.el', but uses `find-tag' to
3714 ;; be compatible with `tags.el'.
3716 ;; Handles three cases:
3717 ;; * `module:' Loop over all possible file names. Stop if a file-name
3718 ;; without extension and directory matches the module.
3720 ;; * `module:tag'
3721 ;; Emacs 19: Replace test functions with functions aware of
3722 ;; Erlang modules. Tricky because the etags system wasn't
3723 ;; built for these kind of operations...
3725 ;; Emacs 18: We loop over `find-tag' until we find a file
3726 ;; whose module matches the requested module. The
3727 ;; drawback is that a lot of files could be loaded into
3728 ;; Emacs.
3730 ;; * `tag' Just give it to `find-tag'.
3732 (defun erlang-find-tag-noselect (modtagname &optional next-p regexp-p)
3733 "Like `find-tag-noselect' but aware of Erlang modules."
3734 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3735 (or modtagname
3736 (setq modtagname (symbol-value 'last-tag)))
3737 (funcall (symbol-function 'set) 'last-tag modtagname)
3738 ;; `tags.el' uses this variable to record how M-, would
3739 ;; know where to restart a tags command.
3740 (if (boundp 'tags-loop-form)
3741 (funcall (symbol-function 'set)
3742 'tags-loop-form '(erlang-find-tag nil t)))
3743 (save-window-excursion
3744 (cond
3745 ((string-match ":$" modtagname)
3746 ;; Only the module name was given. Read all files whose file name
3747 ;; match.
3748 (let ((modname (substring modtagname 0 (match-beginning 0)))
3749 (file nil))
3750 (if (not next-p)
3751 (save-excursion
3752 (visit-tags-table-buffer)
3753 (setq erlang-tags-file-list
3754 (funcall (symbol-function 'tags-table-files)))))
3755 (while (null file)
3756 (or erlang-tags-file-list
3757 (save-excursion
3758 (if (and (featurep 'etags)
3759 (funcall
3760 (symbol-function 'visit-tags-table-buffer) 'same)
3761 (funcall
3762 (symbol-function 'visit-tags-table-buffer) t))
3763 (setq erlang-tags-file-list
3764 (funcall (symbol-function 'tags-table-files)))
3765 (error "No %stags containing %s" (if next-p "more " "")
3766 modtagname))))
3767 (if erlang-tags-file-list
3768 (let ((this-module (erlang-get-module-from-file-name
3769 (car erlang-tags-file-list))))
3770 (if (and (stringp this-module)
3771 (string= modname this-module))
3772 (setq file (car erlang-tags-file-list)))
3773 (setq erlang-tags-file-list (cdr erlang-tags-file-list)))))
3774 (set-buffer (or (get-file-buffer file)
3775 (find-file-noselect file)))))
3777 ((string-match ":" modtagname)
3778 (if (boundp 'find-tag-tag-order)
3779 ;; Method one: Add module-recognising functions to the
3780 ;; list of order functions. However, the tags system
3781 ;; from Emacs 18, and derives thereof (read: XEmacs)
3782 ;; hasn't got this feature.
3783 (progn
3784 (erlang-tags-install-module-check)
3785 (unwind-protect
3786 (funcall (symbol-function 'find-tag)
3787 modtagname next-p regexp-p)
3788 (erlang-tags-remove-module-check)))
3789 ;; Method two: Call the tags system until a file matching
3790 ;; the module is found. This could result in that many
3791 ;; files are read. (e.g. The tag "foo:file" will take a
3792 ;; while to process.)
3793 (let* ((modname (substring modtagname 0 (match-beginning 0)))
3794 (tagname (substring modtagname (match-end 0) nil))
3795 (last-tag tagname)
3796 file)
3797 (while
3798 (progn
3799 (funcall (symbol-function 'find-tag) tagname next-p regexp-p)
3800 (setq next-p t)
3801 ;; Determine the module form the file name. (The
3802 ;; alternative, to check `-module', would make this
3803 ;; code useless for non-Erlang programs.)
3804 (setq file (erlang-get-module-from-file-name buffer-file-name))
3805 (not (and (stringp file)
3806 (string= modname file))))))))
3808 (funcall (symbol-function 'find-tag) modtagname next-p regexp-p)))
3809 (current-buffer))) ; Return the new buffer.
3812 ;; Process interactive arguments for erlang-find-tag-*.
3814 ;; Negative arguments work only for `etags', not `tags'. This is not
3815 ;; a problem since negative arguments means step back into the
3816 ;; history list, a feature not implemented in `tags'.
3818 (defun erlang-tag-interactive (prompt)
3819 (condition-case nil
3820 (require 'etags)
3821 (error
3822 (require 'tags)))
3823 (if current-prefix-arg
3824 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
3827 (let* ((default (erlang-find-tag-default))
3828 (prompt (if default
3829 (format "%s(default %s) " prompt default)
3830 prompt))
3831 (spec (if (featurep 'etags)
3832 (completing-read prompt 'erlang-tags-complete-tag)
3833 (read-string prompt))))
3834 (list (if (equal spec "")
3835 (or default (error "There is no default tag"))
3836 spec)))))
3839 ;; Search tag functions which are aware of Erlang modules. The tactic
3840 ;; is to store new search functions into the local variables of the
3841 ;; TAGS buffers. The variables are restored directly after the
3842 ;; search. The situation is complicated by the fact that new TAGS
3843 ;; files can be loaded during the search.
3846 (defun erlang-tags-install-module-check ()
3847 "Install our own tag search functions."
3848 ;; Make sure our functions are installed in TAGS files loaded
3849 ;; into Emacs while searching.
3850 (cond
3851 ((>= erlang-emacs-major-version 20)
3852 (setq erlang-tags-orig-format-functions
3853 (symbol-value 'tags-table-format-functions))
3854 (funcall (symbol-function 'set) 'tags-table-format-functions
3855 (cons 'erlang-tags-recognize-tags-table
3856 erlang-tags-orig-format-functions))
3857 (setq erlang-tags-buffer-list '())
3860 (setq erlang-tags-orig-format-hooks
3861 (symbol-value 'tags-table-format-hooks)))
3862 (funcall (symbol-function 'set) 'tags-table-format-hooks
3863 (cons 'erlang-tags-recognize-tags-table
3864 erlang-tags-orig-format-hooks))
3865 (setq erlang-tags-buffer-list '())
3868 ;; Install our functions in the TAGS files already resident.
3869 (save-excursion
3870 (let ((files (symbol-value 'tags-table-computed-list)))
3871 (while files
3872 (if (stringp (car files))
3873 (if (get-file-buffer (car files))
3874 (progn
3875 (set-buffer (get-file-buffer (car files)))
3876 (erlang-tags-install-local))))
3877 (setq files (cdr files))))))
3880 (defun erlang-tags-install-local ()
3881 "Install our tag search functions in current buffer."
3882 (if erlang-tags-buffer-installed-p
3884 ;; Mark this buffer as "installed" and record.
3885 (set (make-local-variable 'erlang-tags-buffer-installed-p) t)
3886 (setq erlang-tags-buffer-list
3887 (cons (current-buffer) erlang-tags-buffer-list))
3889 ;; Save the original values.
3890 (set (make-local-variable 'erlang-tags-orig-tag-order)
3891 (symbol-value 'find-tag-tag-order))
3892 (set (make-local-variable 'erlang-tags-orig-regexp-tag-order)
3893 (symbol-value 'find-tag-regexp-tag-order))
3894 (set (make-local-variable 'erlang-tags-orig-search-function)
3895 (symbol-value 'find-tag-search-function))
3896 (set (make-local-variable 'erlang-tags-orig-regexp-search-function)
3897 (symbol-value 'find-tag-regexp-search-function))
3899 ;; Install our own functions.
3900 (set (make-local-variable 'find-tag-search-function)
3901 'erlang-tags-search-forward)
3902 (set (make-local-variable 'find-tag-regexp-search-function)
3903 'erlang-tags-regexp-search-forward)
3904 (set (make-local-variable 'find-tag-tag-order)
3905 '(erlang-tag-match-module-p))
3906 (set (make-local-variable 'find-tag-regexp-tag-order)
3907 '(erlang-tag-match-module-regexp-p))))
3910 (defun erlang-tags-remove-module-check ()
3911 "Remove our own tags search functions."
3912 (cond
3913 ((>= erlang-emacs-major-version 20)
3914 (funcall (symbol-function 'set)
3915 'tags-table-format-functions
3916 erlang-tags-orig-format-functions)
3919 (funcall (symbol-function 'set)
3920 'tags-table-format-hooks
3921 erlang-tags-orig-format-hooks)
3924 ;; Remove our functions from the TAGS files. (Note that
3925 ;; `tags-table-computed-list' need not be the same list as when
3926 ;; the search was started.)
3927 (save-excursion
3928 (let ((buffers erlang-tags-buffer-list))
3929 (while buffers
3930 (if (buffer-name (car buffers))
3931 (progn
3932 (set-buffer (car buffers))
3933 (erlang-tags-remove-local)))
3934 (setq buffers (cdr buffers))))))
3937 (defun erlang-tags-remove-local ()
3938 "Remove our tag search functions from current buffer."
3939 (if (null erlang-tags-buffer-installed-p)
3941 (funcall (symbol-function 'set) 'erlang-tags-buffer-installed-p nil)
3942 (funcall (symbol-function 'set)
3943 'find-tag-tag-order erlang-tags-orig-tag-order)
3944 (funcall (symbol-function 'set)
3945 'find-tag-regexp-tag-order erlang-tags-orig-regexp-tag-order)
3946 (funcall (symbol-function 'set)
3947 'find-tag-search-function erlang-tags-orig-search-function)
3948 (funcall (symbol-function 'set)
3949 'find-tag-regexp-search-function
3950 erlang-tags-orig-regexp-search-function)))
3953 (defun erlang-tags-recognize-tags-table ()
3954 "Install our functions in all loaded TAGS files.
3956 This function is added to `tags-table-format-hooks/functions' when searching
3957 for a tag on the form `module:tag'."
3958 (if (null (funcall (symbol-function 'etags-recognize-tags-table)))
3960 (erlang-tags-install-local)
3964 (defun erlang-tags-search-forward (tag &optional bound noerror count)
3965 "Forward search function, aware of Erlang module prefix."
3966 (if (string-match ":" tag)
3967 (setq tag (substring tag (match-end 0) nil)))
3968 ;; Avoid unintended recursion.
3969 (if (eq erlang-tags-orig-search-function 'erlang-tags-search-forward)
3970 (search-forward tag bound noerror count)
3971 (funcall erlang-tags-orig-search-function tag bound noerror count)))
3974 (defun erlang-tags-regexp-search-forward (tag &optional bound noerror count)
3975 "Forward regexp search function, aware of Erlang module prefix."
3976 (if (string-match ":" tag)
3977 (setq tag (substring tag (match-end 0) nil)))
3978 (if (eq erlang-tags-orig-regexp-search-function
3979 'erlang-tags-regexp-search-forward)
3980 (re-search-forward tag bound noerror count)
3981 (funcall erlang-tags-orig-regexp-search-function
3982 tag bound noerror count)))
3985 ;; t if point is at a tag line that matches TAG, containing
3986 ;; module information. Assumes that all other order functions
3987 ;; are stored in `erlang-tags-orig-[regex]-tag-order'.
3989 (defun erlang-tag-match-module-p (tag)
3990 (erlang-tag-match-module-common-p tag erlang-tags-orig-tag-order))
3992 (defun erlang-tag-match-module-regexp-p (tag)
3993 (erlang-tag-match-module-common-p tag erlang-tags-orig-regexp-tag-order))
3995 (defun erlang-tag-match-module-common-p (tag order)
3996 (let ((mod nil)
3997 (found nil))
3998 (if (string-match ":" tag)
3999 (progn
4000 (setq mod (substring tag 0 (match-beginning 0)))
4001 (setq tag (substring tag (match-end 0) nil))))
4002 (while (and order (not found))
4003 (setq found
4004 (and (not (memq (car order)
4005 '(erlang-tag-match-module-p
4006 erlang-tag-match-module-regexp-p)))
4007 (funcall (car order) tag)))
4008 (setq order (cdr order)))
4009 (and found
4010 (or (null mod)
4011 (string= mod (erlang-get-module-from-file-name
4012 (file-of-tag)))))))
4015 ;;; Tags completion, Emacs 19 `etags' specific.
4017 ;;; The basic idea is to create a second completion table `erlang-tags-
4018 ;;; completion-table' containing all normal tags plus tags on the form
4019 ;;; `module:tag'.
4022 (defun erlang-complete-tag ()
4023 "Perform tags completion on the text around point.
4024 Completes to the set of names listed in the current tags table.
4026 Should the Erlang tags system be installed this command knows
4027 about Erlang modules."
4028 (interactive)
4029 (condition-case nil
4030 (require 'etags)
4031 (error nil))
4032 (cond ((and erlang-tags-installed
4033 (fboundp 'complete-tag)) ; Emacs 19
4034 (let ((orig-tags-complete-tag (symbol-function 'tags-complete-tag)))
4035 (fset 'tags-complete-tag
4036 (symbol-function 'erlang-tags-complete-tag))
4037 (unwind-protect
4038 (funcall (symbol-function 'complete-tag))
4039 (fset 'tags-complete-tag orig-tags-complete-tag))))
4040 ((fboundp 'complete-tag) ; Emacs 19
4041 (funcall (symbol-function 'complete-tag)))
4042 ((fboundp 'tag-complete-symbol) ; XEmacs
4043 (funcall (symbol-function 'tag-complete-symbol)))
4045 (error "This version of Emacs can't complete tags"))))
4048 ;; Based on `tags-complete-tag', but this one uses
4049 ;; `erlang-tags-completion-table' instead of `tags-completion-table'.
4051 ;; This is the entry-point called by system function `completing-read'.
4052 (defun erlang-tags-complete-tag (string predicate what)
4053 (save-excursion
4054 ;; If we need to ask for the tag table, allow that.
4055 (let ((enable-recursive-minibuffers t))
4056 (visit-tags-table-buffer))
4057 (if (eq what t)
4058 (all-completions string (erlang-tags-completion-table) predicate)
4059 (try-completion string (erlang-tags-completion-table) predicate))))
4062 ;; `tags-completion-table' calls itself recursively, make it
4063 ;; call our own wedge instead. Note that the recursive call
4064 ;; is very rare; it only occurs when a tags-file contains
4065 ;; `include'-statements.
4066 (defun erlang-tags-completion-table ()
4067 "Build completion table. Tags on the form `tag' or `module:tag'."
4068 (setq erlang-tags-orig-completion-table
4069 (symbol-function 'tags-completion-table))
4070 (fset 'tags-completion-table
4071 (symbol-function 'erlang-tags-completion-table-1))
4072 (unwind-protect
4073 (erlang-tags-completion-table-1)
4074 (fset 'tags-completion-table
4075 erlang-tags-orig-completion-table)))
4078 (defun erlang-tags-completion-table-1 ()
4079 (make-local-variable 'erlang-tags-completion-table)
4080 (or erlang-tags-completion-table
4081 (let ((tags-completion-table nil)
4082 (tags-completion-table-function
4083 'erlang-etags-tags-completion-table))
4084 (funcall erlang-tags-orig-completion-table)
4085 (setq erlang-tags-completion-table tags-completion-table))))
4088 ;; Based on `etags-tags-completion-table'. The difference is that we
4089 ;; add three symbols to the vector, the tag, module: and module:tag.
4090 ;; The module is extracted from the file name of a tag. (This one
4091 ;; only works if we are looking at an `etags' file. However, this is
4092 ;; the only format supported by Emacs, so far.)
4093 (defun erlang-etags-tags-completion-table ()
4094 (let ((table (make-vector 511 0))
4095 (file nil))
4096 (save-excursion
4097 (goto-char (point-min))
4098 ;; This monster regexp matches an etags tag line.
4099 ;; \1 is the string to match;
4100 ;; \2 is not interesting;
4101 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
4102 ;; \4 is not interesting;
4103 ;; \5 is the explicitly-specified tag name.
4104 ;; \6 is the line to start searching at;
4105 ;; \7 is the char to start searching at.
4106 (while (progn
4107 (while (and
4108 (eq (following-char) ?\f)
4109 (looking-at "\f\n\\([^,\n]*\\),.*\n"))
4110 (setq file (buffer-substring
4111 (match-beginning 1) (match-end 1)))
4112 (goto-char (match-end 0)))
4113 (re-search-forward
4115 ^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
4116 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
4117 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
4118 nil t))
4119 (let ((tag (if (match-beginning 5)
4120 ;; There is an explicit tag name.
4121 (buffer-substring (match-beginning 5) (match-end 5))
4122 ;; No explicit tag name. Best guess.
4123 (buffer-substring (match-beginning 3) (match-end 3))))
4124 (module (and file
4125 (erlang-get-module-from-file-name file))))
4126 (intern tag table)
4127 (if (stringp module)
4128 (progn
4129 (intern (concat module ":" tag) table)
4130 ;; Only the first one will be stored in the table.
4131 (intern (concat module ":") table))))))
4132 table))
4135 ;;; Prepare for other methods to run an Erlang slave process.
4138 (defvar erlang-shell-function 'inferior-erlang
4139 "Command to execute start a new Erlang shell.
4141 Change this variable to use your favorite
4142 Erlang compilation package.")
4144 (defvar erlang-shell-display-function 'inferior-erlang-run-or-select
4145 "Command to execute to display Erlang shell.
4147 Change this variable to use your favorite
4148 Erlang compilation package.")
4150 (defvar erlang-compile-function 'inferior-erlang-compile
4151 "Command to execute to compile current buffer.
4153 Change this variable to use your favorite
4154 Erlang compilation package.")
4156 (defvar erlang-compile-display-function 'inferior-erlang-run-or-select
4157 "Command to execute to view last compilation.
4159 Change this variable to use your favorite
4160 Erlang compilation package.")
4162 (defvar erlang-next-error-function 'inferior-erlang-next-error
4163 "Command to execute to go to the next error.
4165 Change this variable to use your favorite Erlang compilation
4166 package. Not used in Emacs 21.")
4169 ;;;###autoload
4170 (defun erlang-shell ()
4171 "Start a new Erlang shell.
4173 The variable `erlang-shell-function' decides which method to use,
4174 default is to start a new Erlang host. It is possible that, in the
4175 future, a new shell on an already running host will be started."
4176 (interactive)
4177 (call-interactively erlang-shell-function))
4180 ;;;###autoload (autoload 'run-erlang "erlang" "Start a new Erlang shell." t)
4182 ;; It is customary for Emacs packages to supply a function on this
4183 ;; form, even though it violates the `erlang-*' name convention.
4184 (defalias 'run-erlang 'erlang-shell)
4187 (defun erlang-shell-display ()
4188 "Display an Erlang shell, or start a new."
4189 (interactive)
4190 (call-interactively erlang-shell-display-function))
4193 ;;;###autoload
4194 (defun erlang-compile ()
4195 "Compile Erlang module in current buffer."
4196 (interactive)
4197 (call-interactively erlang-compile-function))
4200 (defun erlang-compile-display ()
4201 "Display compilation output."
4202 (interactive)
4203 (call-interactively erlang-compile-display-function))
4206 (defun erlang-next-error ()
4207 "Display next error message from the latest compilation."
4208 (interactive)
4209 (call-interactively erlang-next-error-function))
4214 ;;; Erlang Shell Mode -- Major mode used for Erlang shells.
4217 ;; This mode is designed to be implementation independent,
4218 ;; e.g. it does not assume that we are running an inferior
4219 ;; Erlang, there exists a lot of other possibilities.
4222 (defvar erlang-shell-buffer-name "*erlang*"
4223 "The name of the Erlang link shell buffer.")
4226 (defvar erlang-shell-mode-map nil
4227 "Keymap used by Erlang shells.")
4230 (defvar erlang-shell-mode-hook nil
4231 "*User functions to run when an Erlang shell is started.
4233 This hook is used to change the behaviour of Erlang mode. It is
4234 normally used by the user to personalise the programming environment.
4235 When used in a site init file, it could be used to customise Erlang
4236 mode for all users on the system.
4238 The function added to this hook is run every time a new Erlang
4239 shell is started.
4241 See also `erlang-load-hook', a hook which is run once, when Erlang
4242 mode is loaded, and `erlang-mode-hook' which is run every time a new
4243 Erlang source file is loaded into Emacs.")
4246 (defvar erlang-input-ring-file-name "~/.erlang_history"
4247 "*When non-nil, file name used to store Erlang shell history information.")
4250 (defun erlang-shell-mode ()
4251 "Major mode for interacting with an Erlang shell.
4253 We assume that we already are in Comint mode.
4255 The following special commands are available:
4256 \\{erlang-shell-mode-map}"
4257 (interactive)
4258 (setq major-mode 'erlang-shell-mode)
4259 (setq mode-name "Erlang Shell")
4260 (erlang-mode-variables)
4261 (if erlang-shell-mode-map
4263 (setq erlang-shell-mode-map (copy-keymap comint-mode-map))
4264 (erlang-shell-mode-commands erlang-shell-mode-map))
4265 (use-local-map erlang-shell-mode-map)
4266 (unless inferior-erlang-use-cmm
4267 ;; This was originally not a marker, but it needs to be, at least
4268 ;; in Emacs 21, and should be backwards-compatible. Otherwise,
4269 ;; would need to test whether compilation-parsing-end is a marker
4270 ;; after requiring `compile'.
4271 (set (make-local-variable 'compilation-parsing-end) (copy-marker 1))
4272 (set (make-local-variable 'compilation-error-list) nil)
4273 (set (make-local-variable 'compilation-old-error-list) nil))
4274 ;; Needed when compiling directly from the Erlang shell.
4275 (setq compilation-last-buffer (current-buffer))
4276 (erlang-add-compilation-alist erlang-error-regexp-alist)
4277 (setq comint-prompt-regexp "^[^>=]*> *")
4278 (setq comint-eol-on-send t)
4279 (setq comint-input-ignoredups t)
4280 (setq comint-scroll-show-maximum-output t)
4281 (setq comint-scroll-to-bottom-on-output t)
4282 ;; In Emacs 19.30, `add-hook' has got a `local' flag, use it. If
4283 ;; the call fails, just call the normal `add-hook'.
4284 (condition-case nil
4285 (progn
4286 (make-local-hook 'comint-output-filter-functions) ; obsolete after Emacs 21.3
4287 (add-hook 'comint-output-filter-functions
4288 'inferior-erlang-strip-delete nil t)
4289 (add-hook 'comint-output-filter-functions
4290 'inferior-erlang-strip-ctrl-m nil t))
4291 (error
4292 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-delete)
4293 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-ctrl-m)))
4294 ;; Some older versions of comint don't have an input ring.
4295 (if (fboundp 'comint-read-input-ring)
4296 (progn
4297 (setq comint-input-ring-file-name erlang-input-ring-file-name)
4298 (comint-read-input-ring t)
4299 (make-local-variable 'kill-buffer-hook)
4300 (add-hook 'kill-buffer-hook 'comint-write-input-ring)))
4301 ;; At least in Emacs 21, we need to be in `compilation-minor-mode'
4302 ;; for `next-error' to work. We can avoid it clobbering the shell
4303 ;; keys thus.
4304 (when inferior-erlang-use-cmm
4305 (compilation-minor-mode 1)
4306 (set (make-local-variable 'minor-mode-overriding-map-alist)
4307 `((compilation-minor-mode
4308 . ,(let ((map (make-sparse-keymap)))
4309 ;; It would be useful to put keymap properties on the
4310 ;; error lines so that we could use RET and mouse-2
4311 ;; on them directly.
4312 (when (boundp 'compilation-skip-threshold) ; new compile.el
4313 (define-key map [mouse-2] #'erlang-mouse-2-command)
4314 (define-key map "\C-m" #'erlang-RET-command))
4315 (if (boundp 'compilation-menu-map)
4316 (define-key map [menu-bar compilation]
4317 (cons "Errors" compilation-menu-map)))
4318 map)))))
4319 (run-hooks 'erlang-shell-mode-hook))
4322 (defun erlang-mouse-2-command (event)
4323 "Command bound to `mouse-2' in inferior Erlang buffer.
4324 Selects Comint or Compilation mode command as appropriate."
4325 (interactive "e")
4326 (if (save-window-excursion
4327 (save-excursion
4328 (mouse-set-point event)
4329 (consp (get-text-property (line-beginning-position) 'message))))
4330 (call-interactively (lookup-key compilation-mode-map [mouse-2]))
4331 (call-interactively (lookup-key comint-mode-map [mouse-2]))))
4333 (defun erlang-RET-command ()
4334 "Command bound to `RET' in inferior Erlang buffer.
4335 Selects Comint or Compilation mode command as appropriate."
4336 (interactive)
4337 (if (consp (get-text-property (line-beginning-position) 'message))
4338 (call-interactively (lookup-key compilation-mode-map "\C-m"))
4339 (call-interactively (lookup-key comint-mode-map "\C-m"))))
4341 (defun erlang-shell-mode-commands (map)
4342 (define-key map "\M-\t" 'erlang-complete-tag)
4343 (define-key map "\C-a" 'comint-bol) ; Normally the other way around.
4344 (define-key map "\C-c\C-a" 'beginning-of-line)
4345 (define-key map "\C-d" nil) ; Was `comint-delchar-or-maybe-eof'
4346 (define-key map "\M-\C-m" 'compile-goto-error)
4347 (unless inferior-erlang-use-cmm
4348 (define-key map "\C-x`" 'erlang-next-error)))
4351 ;;; Inferior Erlang -- Run an Erlang shell as a subprocess.
4354 (defvar inferior-erlang-display-buffer-any-frame nil
4355 "*When nil, `inferior-erlang-display-buffer' use only selected frame.
4356 When t, all frames are searched. When 'raise, the frame is raised.")
4358 (defvar inferior-erlang-shell-type 'newshell
4359 "The type of Erlang shell to use.
4361 When this variable is set to the atom `oldshell', the old shell is used.
4362 When set to `newshell' the new shell is used. Should the variable be
4363 nil, the default shell is used.
4365 This variable influence the setting of other variables.")
4367 (defvar inferior-erlang-machine "erl"
4368 "*The name of the Erlang shell.")
4370 (defvar inferior-erlang-machine-options '()
4371 "*The options used when activating the Erlang shell.
4373 This must be a list of strings.")
4375 (defvar inferior-erlang-process-name "inferior-erlang"
4376 "The name of the inferior Erlang process.")
4378 (defvar inferior-erlang-buffer-name erlang-shell-buffer-name
4379 "The name of the inferior Erlang buffer.")
4381 (defvar inferior-erlang-prompt-timeout 60
4382 "*Number of seconds before `inferior-erlang-wait-prompt' timeouts.
4384 The time specified is waited after every output made by the inferior
4385 Erlang shell. When this variable is t, we assume that we always have
4386 a prompt. When nil, we will wait forever, or until \\[keyboard-quit].")
4388 (defvar inferior-erlang-process nil
4389 "Process of last invoked inferior Erlang, or nil.")
4391 (defvar inferior-erlang-buffer nil
4392 "Buffer of last invoked inferior Erlang, or nil.")
4394 ;;;###autoload
4395 (defun inferior-erlang (&optional command)
4396 "Run an inferior Erlang.
4397 With prefix command, prompt for command to start Erlang with.
4399 This is just like running Erlang in a normal shell, except that
4400 an Emacs buffer is used for input and output.
4401 \\<comint-mode-map>
4402 The command line history can be accessed with \\[comint-previous-input] and \\[comint-next-input].
4403 The history is saved between sessions.
4405 Entry to this mode calls the functions in the variables
4406 `comint-mode-hook' and `erlang-shell-mode-hook' with no arguments.
4408 The following commands imitate the usual Unix interrupt and
4409 editing control characters:
4410 \\{erlang-shell-mode-map}"
4411 (interactive
4412 (when current-prefix-arg
4413 (list (read-string "Erlang command: "))))
4414 (require 'comint)
4415 (let (cmd opts)
4416 (if command
4417 (setq cmd "sh"
4418 opts (list "-c" command))
4419 (setq cmd inferior-erlang-machine
4420 opts inferior-erlang-machine-options)
4421 (cond ((eq inferior-erlang-shell-type 'oldshell)
4422 (setq opts (cons "-oldshell" opts)))
4423 ((eq inferior-erlang-shell-type 'newshell)
4424 (setq opts (append '("-newshell" "-env" "TERM" "vt100") opts)))))
4426 (setq inferior-erlang-buffer
4427 (apply 'make-comint
4428 inferior-erlang-process-name cmd
4429 nil opts)))
4430 (setq inferior-erlang-process
4431 (get-buffer-process inferior-erlang-buffer))
4432 (process-kill-without-query inferior-erlang-process)
4433 (switch-to-buffer inferior-erlang-buffer)
4434 (if (and (not (eq system-type 'windows-nt))
4435 (eq inferior-erlang-shell-type 'newshell))
4436 (setq comint-process-echoes t))
4437 ;; `rename-buffer' takes only one argument in Emacs 18.
4438 (condition-case nil
4439 (rename-buffer inferior-erlang-buffer-name t)
4440 (error (rename-buffer inferior-erlang-buffer-name)))
4441 (erlang-shell-mode))
4444 (defun inferior-erlang-run-or-select ()
4445 "Switch to an inferior Erlang buffer, possibly starting new process."
4446 (interactive)
4447 (if (null (inferior-erlang-running-p))
4448 (inferior-erlang)
4449 (inferior-erlang-display-buffer t)))
4452 (defun inferior-erlang-display-buffer (&optional select)
4453 "Make the inferior Erlang process visible.
4454 The window is returned.
4456 Should `inferior-erlang-display-buffer-any-frame' be nil the buffer is
4457 displayed in the current frame. Should it be non-nil, and the buffer
4458 already is visible in any other frame, no new window will be created.
4459 Should it be the atom 'raise, the frame containing the window will
4460 be raised.
4462 Should the optional argument SELECT be non-nil, the window is
4463 selected. Should the window be in another frame, that frame is raised.
4465 Note, should the mouse pointer be places outside the raised frame, that
4466 frame will become deselected before the next command."
4467 (interactive)
4468 (or (inferior-erlang-running-p)
4469 (error "No inferior Erlang process is running"))
4470 (let ((win (inferior-erlang-window
4471 inferior-erlang-display-buffer-any-frame))
4472 (frames-p (fboundp 'selected-frame)))
4473 (if (null win)
4474 (let ((old-win (selected-window)))
4475 (save-excursion
4476 (switch-to-buffer-other-window inferior-erlang-buffer)
4477 (setq win (selected-window)))
4478 (select-window old-win))
4479 (if (and window-system
4480 frames-p
4481 (or select
4482 (eq inferior-erlang-display-buffer-any-frame 'raise))
4483 (not (eq (selected-frame) (window-frame win))))
4484 (raise-frame (window-frame win))))
4485 (if select
4486 (select-window win))
4487 (sit-for 0)
4488 win))
4491 (defun inferior-erlang-running-p ()
4492 "Non-nil when an inferior Erlang is running."
4493 (and inferior-erlang-process
4494 (memq (process-status inferior-erlang-process) '(run open))
4495 inferior-erlang-buffer
4496 (buffer-name inferior-erlang-buffer)))
4499 (defun inferior-erlang-window (&optional all-frames)
4500 "Return the window containing the inferior Erlang, or nil."
4501 (and (inferior-erlang-running-p)
4502 (if (and all-frames (>= erlang-emacs-major-version 19))
4503 (get-buffer-window inferior-erlang-buffer t)
4504 (get-buffer-window inferior-erlang-buffer))))
4507 (defun inferior-erlang-wait-prompt ()
4508 "Wait until the inferior Erlang shell prompt appears."
4509 (if (eq inferior-erlang-prompt-timeout t)
4511 (or (inferior-erlang-running-p)
4512 (error "No inferior Erlang shell is running"))
4513 (save-excursion
4514 (set-buffer inferior-erlang-buffer)
4515 (let ((msg nil))
4516 (while (save-excursion
4517 (goto-char (process-mark inferior-erlang-process))
4518 (forward-line 0)
4519 (not (looking-at comint-prompt-regexp)))
4520 (if msg
4522 (setq msg t)
4523 (message "Waiting for Erlang shell prompt (press C-g to abort)."))
4524 (or (accept-process-output inferior-erlang-process
4525 inferior-erlang-prompt-timeout)
4526 (error "No Erlang shell prompt before timeout")))
4527 (if msg (message ""))))))
4529 (autoload 'comint-send-input "comint")
4531 (defun inferior-erlang-send-command (cmd &optional hist)
4532 "Send command CMD to the inferior Erlang.
4534 The contents of the current command line (if any) will
4535 be placed at the next prompt.
4537 If optional second argument is non-nil the command is inserted into
4538 the history list.
4540 Return the position after the newly inserted command."
4541 (or (inferior-erlang-running-p)
4542 (error "No inferior Erlang process is running"))
4543 (let ((old-buffer (current-buffer))
4544 (insert-point (marker-position (process-mark inferior-erlang-process)))
4545 (insert-length (if comint-process-echoes
4547 (1+ (length cmd)))))
4548 (set-buffer inferior-erlang-buffer)
4549 (goto-char insert-point)
4550 (insert cmd)
4551 ;; Strange things happened if `comint-eol-on-send' is declared
4552 ;; in the `let' expression above, but setq:d here. The
4553 ;; `set-buffer' statement obviously makes the buffer local
4554 ;; instance of `comint-eol-on-send' shadow this one.
4555 ;; I'm considering this a bug in Elisp.
4557 ;; This was previously cautioned against in the Lisp manual. It
4558 ;; has been sorted out in Emacs 21. -- fx
4559 (let ((comint-eol-on-send nil)
4560 (comint-input-filter (if hist comint-input-filter 'ignore)))
4561 (comint-send-input))
4562 ;; Adjust all windows whose points are incorrect.
4563 (if (null comint-process-echoes)
4564 (walk-windows
4565 (function
4566 (lambda (window)
4567 (if (and (eq (window-buffer window) inferior-erlang-buffer)
4568 (= (window-point window) insert-point))
4569 (set-window-point window
4570 (+ insert-point insert-length)))))
4571 nil t))
4572 (set-buffer old-buffer)
4573 (+ insert-point insert-length)))
4576 (defun inferior-erlang-strip-delete (&optional s)
4577 "Remove `^H' (delete) and the characters it was supposed to remove."
4578 (interactive)
4579 (if (and (boundp 'comint-last-input-end)
4580 (boundp 'comint-last-output-start))
4581 (save-excursion
4582 (goto-char
4583 (if (interactive-p)
4584 (symbol-value 'comint-last-input-end)
4585 (symbol-value 'comint-last-output-start)))
4586 (while (progn (skip-chars-forward "^\C-h")
4587 (not (eq (point) (point-max))))
4588 (delete-char 1)
4589 (or (bolp)
4590 (backward-delete-char 1))))))
4593 ;; Basically `comint-strip-ctrl-m', with a few extra checks.
4594 (defun inferior-erlang-strip-ctrl-m (&optional string)
4595 "Strip trailing `^M' characters from the current output group."
4596 (interactive)
4597 (if (and (boundp 'comint-last-input-end)
4598 (boundp 'comint-last-output-start))
4599 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
4600 (save-excursion
4601 (goto-char
4602 (if (interactive-p)
4603 (symbol-value 'comint-last-input-end)
4604 (symbol-value 'comint-last-output-start)))
4605 (while (re-search-forward "\r+$" pmark t)
4606 (replace-match "" t t))))))
4609 (defun inferior-erlang-compile ()
4610 "Compile the file in the current buffer.
4612 Should Erlang return `{error, nofile}' it could not load the object
4613 module after completing the compilation. This is due to a bug in the
4614 compile command `c' when using the option `outdir'.
4616 There exists two workarounds for this bug:
4618 1) Place the directory in the Erlang load path.
4620 2) Set the Emacs variable `erlang-compile-use-outdir' to nil.
4621 To do so, place the following line in your `~/.emacs'-file:
4622 (setq erlang-compile-use-outdir nil)"
4623 (interactive)
4624 (save-some-buffers)
4625 (or (inferior-erlang-running-p)
4626 (save-excursion
4627 (inferior-erlang)))
4628 (or (inferior-erlang-running-p)
4629 (error "Error starting inferior Erlang shell"))
4630 (let ((outdir (concat (file-name-directory (buffer-file-name)) erlang-compile-outdir))
4631 ;;; (file (file-name-nondirectory (buffer-file-name)))
4632 (noext (substring (buffer-file-name) 0 -4))
4633 ;; Hopefully, noone else will ever use these...
4634 (tmpvar "Tmp7236")
4635 (tmpvar2 "Tmp8742")
4636 end)
4637 (inferior-erlang-display-buffer)
4638 (inferior-erlang-wait-prompt)
4639 (setq end (inferior-erlang-send-command
4640 (if erlang-compile-use-outdir
4641 (format "c(\"%s\", [{outdir, \"%s\"}])." noext outdir)
4642 (format
4643 (concat
4644 "f(%s), {ok, %s} = file:get_cwd(), "
4645 "file:set_cwd(\"%s\"), "
4646 "%s = c(\"%s\"), file:set_cwd(%s), f(%s), %s.")
4647 tmpvar2 tmpvar
4648 outdir
4649 tmpvar2 noext tmpvar tmpvar tmpvar2))
4650 nil))
4651 (inferior-erlang-wait-prompt)
4652 (save-excursion
4653 (set-buffer inferior-erlang-buffer)
4654 (setq compilation-error-list nil)
4655 (set-marker compilation-parsing-end end))
4656 (setq compilation-last-buffer inferior-erlang-buffer)))
4659 ;; `next-error' only accepts buffers with major mode `compilation-mode'
4660 ;; or with the minor mode `compilation-minor-mode' activated.
4661 ;; (To activate the minor mode is out of the question, since it will
4662 ;; ruin the inferior Erlang keymap.)
4663 ;; This is done differently in Emacs 21.
4664 (defun inferior-erlang-next-error (&optional argp)
4665 "Just like `next-error'.
4666 Capable of finding error messages in an inferior Erlang buffer."
4667 (interactive "P")
4668 (let ((done nil)
4669 (buf (and (boundp 'compilation-last-buffer)
4670 compilation-last-buffer)))
4671 (if (and (bufferp buf)
4672 (save-excursion
4673 (set-buffer buf)
4674 (and (eq major-mode 'erlang-shell-mode)
4675 (setq major-mode 'compilation-mode))))
4676 (unwind-protect
4677 (progn
4678 (setq done t)
4679 (next-error argp))
4680 (save-excursion
4681 (set-buffer buf)
4682 (setq major-mode 'erlang-shell-mode))))
4683 (or done
4684 (next-error argp))))
4687 (defun inferior-erlang-change-directory (&optional dir)
4688 "Make the inferior Erlang change directory.
4689 The default is to go to the directory of the current buffer."
4690 (interactive)
4691 (or dir (setq dir (file-name-directory (buffer-file-name))))
4692 (or (inferior-erlang-running-p)
4693 (error "No inferior Erlang is running"))
4694 (inferior-erlang-display-buffer)
4695 (inferior-erlang-wait-prompt)
4696 (inferior-erlang-send-command (format "cd('%s')." dir) nil))
4698 (defun erlang-align-arrows (start end)
4699 "Align arrows (\"->\") in function clauses from START to END.
4700 When called interactively, aligns arrows after function clauses inside
4701 the region.
4703 With a prefix argument, aligns all arrows, not just those in function
4704 clauses.
4706 Example:
4708 sum(L) -> sum(L, 0).
4709 sum([H|T], Sum) -> sum(T, Sum + H);
4710 sum([], Sum) -> Sum.
4712 becomes:
4714 sum(L) -> sum(L, 0).
4715 sum([H|T], Sum) -> sum(T, Sum + H);
4716 sum([], Sum) -> Sum."
4717 (interactive "r")
4718 (save-excursion
4719 (let (;; regexp for matching arrows. without a prefix argument,
4720 ;; the regexp matches function heads. With a prefix, it
4721 ;; matches any arrow.
4722 (re (if current-prefix-arg
4723 "^.*\\(\\)->"
4724 (eval-when-compile
4725 (concat "^" erlang-atom-regexp ".*\\(\\)->"))))
4726 ;; part of regexp matching directly before the arrow
4727 (arrow-match-pos (if current-prefix-arg
4729 (1+ erlang-atom-regexp-matches)))
4730 ;; accumulator for positions where arrows are found, ordered
4731 ;; by buffer position (from greatest to smallest)
4732 (arrow-positions '())
4733 ;; accumulator for longest distance from start of line to arrow
4734 (most-indent 0)
4735 ;; marker to track the end of the region we're aligning
4736 (end-marker (progn (goto-char end)
4737 (point-marker))))
4738 ;; Pass 1: Find the arrow positions, adjust the whitespace
4739 ;; before each arrow to one space, and find the greatest
4740 ;; indentation level.
4741 (goto-char start)
4742 (while (re-search-forward re end-marker t)
4743 (goto-char (match-beginning arrow-match-pos))
4744 (just-one-space) ; adjust whitespace
4745 (setq arrow-positions (cons (point) arrow-positions))
4746 (setq most-indent (max most-indent (erlang-column-number))))
4747 (set-marker end-marker nil) ; free the marker
4748 ;; Pass 2: Insert extra padding so that all arrow indentation is
4749 ;; equal. This is done last-to-first by buffer position, so that
4750 ;; inserting spaces before one arrow doesn't change the
4751 ;; positions of the next ones.
4752 (mapcar (lambda (arrow-pos)
4753 (goto-char arrow-pos)
4754 (let* ((pad (- most-indent (erlang-column-number))))
4755 (when (> pad 0)
4756 (insert-char ?\ pad))))
4757 arrow-positions))))
4759 (defun erlang-column-number ()
4760 "Return the column number of the current position in the buffer.
4761 Tab characters are counted by their visual width."
4762 (string-width (buffer-substring (line-beginning-position) (point))))
4764 (defun erlang-current-defun ()
4765 "`add-log-current-defun-function' for Erlang."
4766 (save-excursion
4767 (erlang-beginning-of-function)
4768 (if (looking-at "[a-z0-9_]+")
4769 (match-string 0))))
4771 ;; Aliases for backward compatibility with older versions of Erlang Mode.
4773 ;; Unfortuantely, older versions of Emacs doesn't have `defalias' and
4774 ;; `make-obsolete' so we have to define our own `obsolete' function.
4776 (defun erlang-obsolete (sym newdef)
4777 "Make the obsolete function SYM refer to the defined function NEWDEF.
4779 Simplified version of a combination `defalias' and `make-obsolete',
4780 it assumes that NEWDEF is loaded."
4781 (defalias sym (symbol-function newdef))
4782 (if (fboundp 'make-obsolete)
4783 (make-obsolete sym newdef)))
4786 (erlang-obsolete 'calculate-erlang-indent 'erlang-calculate-indent)
4787 (erlang-obsolete 'calculate-erlang-stack-indent
4788 'erlang-calculate-stack-indent)
4789 (erlang-obsolete 'at-erlang-keyword 'erlang-at-keyword)
4790 (erlang-obsolete 'at-erlang-operator 'erlang-at-operator)
4791 (erlang-obsolete 'beginning-of-erlang-clause 'erlang-beginning-of-clause)
4792 (erlang-obsolete 'end-of-erlang-clause 'erlang-end-of-clause)
4793 (erlang-obsolete 'mark-erlang-clause 'erlang-mark-clause)
4794 (erlang-obsolete 'beginning-of-erlang-function 'erlang-beginning-of-function)
4795 (erlang-obsolete 'end-of-erlang-function 'erlang-end-of-function)
4796 (erlang-obsolete 'mark-erlang-function 'erlang-mark-function)
4797 (erlang-obsolete 'pass-over-erlang-clause 'erlang-pass-over-function)
4798 (erlang-obsolete 'name-of-erlang-function 'erlang-name-of-function)
4801 ;; Fixme: shouldn't redefine `set-visited-file-name' anyhow -- see above.
4802 (defconst erlang-unload-hook
4803 (list (lambda ()
4804 (defalias 'set-visited-file-name
4805 'erlang-orig-set-visited-file-name)
4806 (when (featurep 'advice)
4807 (ad-unadvise 'Man-notify-when-ready)
4808 (ad-unadvise 'set-visited-file-name)))))
4810 ;; The end...
4812 (provide 'erlang)
4814 (run-hooks 'erlang-load-hook)
4816 ;; Local variables:
4817 ;; coding: iso-8859-1
4818 ;; End:
4820 ;;; erlang.el ends here