Merge branch 'legoscia' into next
[erlware-mode.git] / erlang.el
blob6ad03ea417ee0fd13a71bb2ecdba16a7df8db877
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.14
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.14"
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-indent-level 4
457 "*Indentation of Erlang calls/clauses within blocks.")
459 (defvar erlang-indent-guard 2
460 "*Indentation of Erlang guards.")
462 (defvar erlang-argument-indent 2
463 "*Indentation of the first argument in a function call.
464 When nil, indent to the column after the `(' of the
465 function.")
467 (defvar erlang-tab-always-indent t
468 "*Non-nil means TAB in Erlang mode should always re-indent the current line,
469 regardless of where in the line point is when the TAB command is used.")
471 (defvar erlang-error-regexp-alist
472 '(("^\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" . (1 2)))
473 "*Patterns for matching Erlang errors.")
475 (defvar erlang-man-inhibit (eq system-type 'windows-nt)
476 "Inhibit the creation of the Erlang Manual Pages menu.
478 The Windows distribution of Erlang does not include man pages, hence
479 there is no attempt to create the menu.")
481 (defvar erlang-man-dirs
482 '(("Man - Commands" "/man/man1" t)
483 ("Man - Modules" "/man/man3" t)
484 ("Man - Files" "/man/man4" t)
485 ("Man - Applications" "/man/man6" t))
486 "*The man directories displayed in the Erlang menu.
488 Each item in the list should be a list with three elements, the first
489 the name of the menu, the second the directory, and the last a flag.
490 Should the flag the nil, the directory is absolute, should it be non-nil
491 the directory is relative to the variable `erlang-man-root-dir'.")
493 (defvar erlang-man-max-menu-size 20
494 "*The maximum number of menu items in one menu allowed.")
496 (defvar erlang-man-display-function 'erlang-man-display
497 "*Function used to display man page.
499 The function is called with one argument, the name of the file
500 containing the man page. Use this variable when the default
501 function, `erlang-man-display', does not work on your system.")
503 (eval-and-compile
504 (defconst erlang-atom-quoted-regexp
505 "'\\(?:[^\\']?\\(?:\\\\'\\)?\\)*'"
506 "Regexp describing a single-quoted atom"))
508 (eval-and-compile
509 (defconst erlang-atom-regular-regexp
510 "\\(?:[a-z][A-Za-z0-9_]*\\)"
511 "Regexp describing a regular (non-quoted) atom"))
513 (eval-and-compile
514 (defconst erlang-atom-regexp
515 (concat"\\(" erlang-atom-quoted-regexp "\\|"
516 erlang-atom-regular-regexp "\\)")
517 "Regexp describing an Erlang atom."))
519 (defconst erlang-atom-regexp-matches 1
520 "Number of regexp parenthesis pairs in `erlang-atom-regexp'.
522 This is used to determine parenthesis matches in complex regexps which
523 contains `erlang-atom-regexp'.")
525 (defconst erlang-variable-regexp "\\([A-Z_][a-zA-Z0-9_]*\\)"
526 "Regexp which should match an Erlang variable.
528 The regexp must be surrounded with a pair of regexp parentheses.")
529 (defconst erlang-variable-regexp-matches 1
530 "Number of regexp parenthesis pairs in `erlang-variable-regexp'.
532 This is used to determine matches in complex regexps which contains
533 `erlang-variable-regexp'.")
535 (defconst erlang-bif-regexp
536 (concat
537 "\\("
538 "a\\(bs\\|live\\|pply\\|tom_to_list\\)\\|"
539 "binary_to_\\(list\\|term\\)\\|"
540 "concat_binary\\|"
541 "d\\(ate\\|isconnect_node\\)\\|"
542 "e\\(lement\\|rase\\|xit\\)\\|"
543 "floa\\(t\\|t_to_list\\)\\|"
544 "g\\(arbage_collect\\|et\\(\\|_keys\\)\\|roup_leader\\)\\|"
545 "h\\(alt\\|d\\)\\|"
546 "i\\(nte\\(ger_to_list\\|rnal_bif\\)\\|s_alive\\)\\|"
547 "l\\(ength\\|i\\(nk\\|st_to_\\(atom\\|binary\\|float\\|integer"
548 "\\|pid\\|tuple\\)\\)\\)\\|"
549 "make_ref\\|no\\(de\\(\\|_\\(link\\|unlink\\)\\|s\\)\\|talive\\)\\|"
550 "open_port\\|"
551 "p\\(id_to_list\\|rocess\\(_\\(flag\\|info\\)\\|es\\)\\|ut\\)\\|"
552 "r\\(egister\\(\\|ed\\)\\|ound\\)\\|"
553 "s\\(e\\(lf\\|telement\\)\\|ize"
554 "\\|p\\(awn\\(\\|_link\\)\\|lit_binary\\)\\|tatistics\\)\\|"
555 "t\\(erm_to_binary\\|hrow\\|ime\\|l\\|r\\(ace\\|unc\\)\\|uple_to_list\\)\\|"
556 "un\\(link\\|"
557 "register\\)\\|"
558 "whereis"
559 "\\)")
560 "Regexp matching an Erlang built-in function (BIF).")
562 (defvar erlang-defun-prompt-regexp (concat "^" erlang-atom-regexp "\\s *(")
563 "Regexp which should match beginning of a clause.")
565 (defvar erlang-file-name-extension-regexp "\\.[eh]rl$"
566 "*Regexp which should match an Erlang file name.
568 This regexp is used when an Erlang module name is extracted from the
569 name of an Erlang source file.
571 The regexp should only match the section of the file name which should
572 be excluded from the module name.
574 To match all files set this variable to \"\\\\(\\\\..*\\\\|\\\\)$\".
575 The matches all except the extension. This is useful if the Erlang
576 tags system should interpret tags on the form `module:tag' for
577 files written in other languages than Erlang.")
579 (defvar erlang-mode-map
580 (let ((map (make-sparse-keymap)))
581 (unless (boundp 'indent-line-function)
582 (define-key map "\t" 'erlang-indent-command))
583 (define-key map ";" 'erlang-electric-semicolon)
584 (define-key map "," 'erlang-electric-comma)
585 (define-key map "<" 'erlang-electric-lt)
586 (define-key map ">" 'erlang-electric-gt)
587 (define-key map "\C-m" 'erlang-electric-newline)
588 (if (not (boundp 'delete-key-deletes-forward))
589 (define-key map "\177" 'backward-delete-char-untabify)
590 (define-key map [backspace] 'backward-delete-char-untabify))
591 (define-key map "\M-q" 'erlang-fill-paragraph)
592 (unless (boundp 'beginning-of-defun-function)
593 (define-key map "\M-\C-a" 'erlang-beginning-of-function)
594 (define-key map "\M-\C-e" 'erlang-end-of-function)
595 (define-key map "\M-\C-h" 'erlang-mark-function))
596 (define-key map "\M-\t" 'erlang-complete-tag)
597 (define-key map "\C-c\M-\t" 'tempo-complete-tag)
598 (define-key map "\M-+" 'erlang-find-next-tag)
599 (define-key map "\C-c\M-a" 'erlang-beginning-of-clause)
600 (define-key map "\C-c\M-b" 'tempo-backward-mark)
601 (define-key map "\C-c\M-e" 'erlang-end-of-clause)
602 (define-key map "\C-c\M-f" 'tempo-forward-mark)
603 (define-key map "\C-c\M-h" 'erlang-mark-clause)
604 (define-key map "\C-c\C-c" 'comment-region)
605 (define-key map "\C-c\C-j" 'erlang-generate-new-clause)
606 (define-key map "\C-c\C-k" 'erlang-compile)
607 (define-key map "\C-c\C-l" 'erlang-compile-display)
608 (define-key map "\C-c\C-s" 'erlang-show-syntactic-information)
609 (define-key map "\C-c\C-q" 'erlang-indent-function)
610 (define-key map "\C-c\C-u" 'erlang-uncomment-region)
611 (define-key map "\C-c\C-y" 'erlang-clone-arguments)
612 (define-key map "\C-c\C-a" 'erlang-align-arrows)
613 (define-key map "\C-c\C-z" 'erlang-shell-display)
614 ;; This is only needed for Emacs < 21.
615 ;; (unless inferior-erlang-use-cmm
616 ;; (define-key map "\C-x`" 'erlang-next-error))
617 map)
618 "*Keymap used in Erlang mode.")
619 (defvar erlang-mode-abbrev-table nil
620 "Abbrev table in use in Erlang-mode buffers.")
621 (defvar erlang-mode-syntax-table nil
622 "Syntax table in use in Erlang-mode buffers.")
624 (defconst erlang-emacs-major-version
625 (if (boundp 'emacs-major-version)
626 emacs-major-version
627 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version)
628 (string-to-int (substring emacs-version
629 (match-beginning 1) (match-end 1))))
630 "Major version number of Emacs.")
632 (defconst erlang-emacs-minor-version
633 (if (boundp 'emacs-minor-version)
634 emacs-minor-version
635 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version)
636 (string-to-int (substring emacs-version
637 (match-beginning 2) (match-end 2))))
638 "Minor version number of Emacs.")
640 (defconst erlang-xemacs-p (string-match "Lucid\\|XEmacs" emacs-version)
641 "Non-nil when running under XEmacs or Lucid Emacs.")
643 (defvar erlang-xemacs-popup-menu '("Erlang Mode Commands" . nil)
644 "Common popup menu for all buffers in Erlang mode.
646 This variable is destructively modified every time the Erlang menu
647 is modified. The effect is that all changes take effect in all
648 buffers in Erlang mode, just like under GNU Emacs.
650 Never EVER set this variable!")
652 (defconst inferior-erlang-use-cmm (boundp 'minor-mode-overriding-map-alist)
653 "Non-nil means use `compilation-minor-mode' in Erlang shell.")
655 ;; Tempo skeleton templates:
656 (load "erlang-skels")
658 ;; Sinan commands:
659 (load "erlang-sinan")
661 ;; Flymake support
662 ;; M-x flymake-mode to start
663 (load "erlang-flymake")
665 ;; Font-lock variables
667 (defvar erlang-font-lock-modern-p
668 (cond ((>= erlang-emacs-major-version 20) t)
669 (erlang-xemacs-p (>= erlang-emacs-minor-version 14))
670 ((= erlang-emacs-major-version 19) (>= erlang-emacs-minor-version 29))
671 (t nil))
672 "Non-nil when this version of Emacs uses a modern version of Font Lock.
674 This is determined by checking the version of Emacs used, the actual
675 font-lock code is not loaded.")
678 ;; The next few variables define different Erlang font-lock patterns.
679 ;; They could be appended to form a custom font-lock appearance.
681 ;; The function `erlang-font-lock-set-face' could be used to change
682 ;; the face of a pattern.
684 ;; Note that Erlang strings and atoms are highlighted with using
685 ;; syntactic analysis.
687 (defvar erlang-font-lock-keywords-func
688 (list
689 (list (concat "^" erlang-atom-regexp "\\s-*(")
690 1 'font-lock-function-name-face t))
691 "Font lock keyword highlighting a function header.")
693 (defvar erlang-font-lock-keywords-int-func-call
694 (list
695 (list (concat "\\<" erlang-atom-regexp "\\s-*(")
696 1 'font-lock-type-face))
697 "Font lock keyword highlighting an internal function call.")
699 (defvar erlang-font-lock-keywords-ext-func-call
700 (list
701 (list (concat "\\<" erlang-atom-regexp "\\s-*:\\s-*"
702 erlang-atom-regexp "\\s-*(")
703 1 'font-lock-type-face)
704 (list (concat "\\<" erlang-atom-regexp "\\s-*:\\s-*"
705 erlang-atom-regexp "\\s-*(")
706 2 'font-lock-type-face))
707 "Font lock keyword highlighting an external function call.")
709 (defvar erlang-font-lock-keywords-ext-bif
710 (list
711 (list (concat "\\<\\(erlang\\)\\s-*:\\s-*\\<" erlang-bif-regexp "\\s-*(")
712 1 'font-lock-builtin-face)
713 (list (concat "\\<\\(erlang\\)\\s-*:\\s-*\\<" erlang-bif-regexp "\\s-*(")
714 2 'font-lock-builtin-face))
715 "Font lock keyword highlighting built in functions.")
717 (defvar erlang-font-lock-keywords-int-bif
718 (list
719 (list (concat "\\<" erlang-bif-regexp "\\s-*(")
720 1 'font-lock-builtin-face))
721 "Font lock keyword highlighting built in functions.")
723 (defvar erlang-font-lock-keywords-fn
724 (list
725 (list (concat "\\(" erlang-atom-regexp "/[0-9]+\\)")
726 1 'font-lock-function-name-face))
727 "Font lock keyword highlighting a F/N fun descriptor.")
729 (defvar erlang-font-lock-keywords-plusplus
730 (list
731 (list (concat "\\(\\+\\+\\)")
732 1 'font-lock-warning-face))
733 "Font lock keyword highlighting the `++' operator.")
735 (defvar erlang-font-lock-keywords-dollar
736 (list
737 (list "\\(\\$\\([^\\]\\|\\\\\\([^0-7^\n]\\|[0-7]+\\|\\^[a-zA-Z]\\)\\)\\)"
738 1 'font-lock-string-face))
739 "Font lock keyword highlighting numbers in ASCII form (e.g. $A).")
741 (defvar erlang-font-lock-keywords-lc
742 (list
743 (list "\\(<-\\)" 1 'font-lock-keyword-face)
744 (list "\\(||\\)" 1 'font-lock-keyword-face))
745 "Font lock keyword highlighting list comprehension operators.")
747 (defvar erlang-font-lock-keywords-keywords
748 (list
749 (list (concat "\\<\\(a\\(fter\\|ndalso\\)\\|begin\\|c\\(atch\\|ase\\)"
750 "\\|end\\|fun\\|if\\|o\\(f\\|relse\\)\\|receive\\|try\\|when"
751 "\\|query\\)\\([^a-zA-Z0-9_]\\|$\\)")
752 1 'font-lock-keyword-face))
753 "Font lock keyword highlighting Erlang keywords.")
755 (defvar erlang-font-lock-keywords-attr
756 (list
757 (list (concat "^\\(-" erlang-atom-regexp "\\)\\(\\s-\\|\\.\\|(\\)")
758 1 'font-lock-preprocessor-face))
759 "Font lock keyword highlighting attributes.")
761 (defvar erlang-font-lock-keywords-quotes
762 (list
763 (list "`\\([-+a-zA-Z0-9_:*][-+a-zA-Z0-9_:*]+\\)'"
764 1 'font-lock-keyword-face t))
765 "Font lock keyword highlighting words in single quotes in comments.
767 This is not the highlighting of Erlang strings and atoms, which
768 are highlighted by syntactic analysis.")
770 ;; Note: The deprecated guard `float' collides with the bif `float'.
771 (defvar erlang-font-lock-keywords-guards
772 (list
773 (list
774 (concat "[^:]\\<\\("
775 "\\(is_\\)?\\(atom\\|boolean\\|function\\|binary\\|constant"
776 "\\|integer\\|list\\|number\\|p\\(id\\|ort\\)\\|"
777 "re\\(ference\\|cord\\)\\|tuple"
778 "\\)\\)\\s *(")
780 (if erlang-font-lock-modern-p
781 'font-lock-builtin-face
782 'font-lock-keyword-face)))
783 "Font lock keyword highlighting guards.")
785 (defvar erlang-font-lock-keywords-macros
786 (list
787 (list (concat "?\\s *\\(" erlang-atom-regexp
788 "\\|" erlang-variable-regexp "\\)\\>")
789 1 'font-lock-preprocessor-face)
790 (list (concat "^-\\(define\\|ifn?def\\)\\s *(\\s *\\(" erlang-atom-regexp
791 "\\|" erlang-variable-regexp "\\)\\>")
792 2 'font-lock-preprocessor-face))
793 "Font lock keyword highlighting macros.
794 This must be placed in front of `erlang-font-lock-keywords-vars'.")
796 (defvar erlang-font-lock-keywords-records
797 (list
798 (list (concat "#\\s *" erlang-atom-regexp "\\>")
799 1 'font-lock-preprocessor-face)
800 ;; Don't highlight numerical constants.
801 (list "\\<[0-9][0-9]?#\\([0-9a-fA_F]+\\)\\>"
802 1 nil t)
803 (list (concat "^-record(\\s *" erlang-atom-regexp "\\>")
804 1 'font-lock-preprocessor-face))
805 "Font lock keyword highlighting Erlang records.
806 This must be placed in front of `erlang-font-lock-keywords-vars'.")
808 (defvar erlang-font-lock-keywords-vars
809 (list
810 (list (concat "\\<" erlang-variable-regexp "\\>")
811 1 (if erlang-font-lock-modern-p
812 'font-lock-variable-name-face
813 'font-lock-type-face)))
814 "Font lock keyword highlighting Erlang variables.
815 Must be preceded by `erlang-font-lock-keywords-macros' and `-records'
816 to work properly.")
818 (defvar erlang-font-lock-keywords-atom-regular
819 (list
820 (list (concat "\\<\\(" erlang-atom-regular-regexp "\\)\\>")
821 1 'font-lock-constant-face))
822 "Font lock keyword highlighting Erlang atoms.")
824 (defvar erlang-font-lock-keywords-atom-quoted
825 (list
826 (list (concat "\\<\\(" erlang-atom-quoted-regexp "\\)\\>")
827 1 'font-lock-constant-face))
828 "Font lock keyword highlighting Erlang atoms.")
830 (defvar erlang-font-lock-keywords-1
831 (append erlang-font-lock-keywords-func
832 erlang-font-lock-keywords-keywords)
833 ;; DocStringOrig: erlang-font-lock-keywords
834 "Font-lock keywords used by Erlang Mode.
836 There exists three levels of Font Lock keywords for Erlang:
837 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
838 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
839 `erlang-font-lock-keywords-3' - Variables, macros and records.
841 To use a specific level, please set the variable
842 `font-lock-maximum-decoration' to the appropriate level. Note that the
843 variable must be set before Erlang mode is activated.
845 Example:
846 (setq font-lock-maximum-decoration 2)")
848 (defvar erlang-font-lock-keywords-2
849 (append erlang-font-lock-keywords-1
850 erlang-font-lock-keywords-attr
851 erlang-font-lock-keywords-quotes
852 erlang-font-lock-keywords-guards)
853 ;; DocStringCopy: erlang-font-lock-keywords
854 "Font-lock keywords used by Erlang Mode.
856 There exists three levels of Font Lock keywords for Erlang:
857 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
858 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
859 `erlang-font-lock-keywords-3' - Variables, macros and records.
861 To use a specific level, please set the variable
862 `font-lock-maximum-decoration' to the appropriate level. Note that the
863 variable must be set before Erlang mode is activated.
865 Example:
866 (setq font-lock-maximum-decoration 2)")
869 (defvar erlang-font-lock-keywords-3
870 (append erlang-font-lock-keywords-2
871 erlang-font-lock-keywords-macros
872 erlang-font-lock-keywords-records
873 erlang-font-lock-keywords-ext-bif
874 erlang-font-lock-keywords-ext-func-call
875 erlang-font-lock-keywords-int-bif
876 erlang-font-lock-keywords-int-func-call
877 erlang-font-lock-keywords-fn
878 erlang-font-lock-keywords-plusplus
879 erlang-font-lock-keywords-lc
880 erlang-font-lock-keywords-atom-quoted
881 erlang-font-lock-keywords-dollar
882 erlang-font-lock-keywords-atom-regular
883 erlang-font-lock-keywords-vars
885 ;; DocStringCopy: erlang-font-lock-keywords
886 "Font-lock keywords used by Erlang Mode.
888 There exists three levels of Font Lock keywords for Erlang:
889 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
890 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
891 `erlang-font-lock-keywords-3' - Variables, macros and records.
893 To use a specific level, please set the variable
894 `font-lock-maximum-decoration' to the appropriate level. Note that the
895 variable must be set before Erlang mode is activated.
897 Example:
898 (setq font-lock-maximum-decoration 2)")
901 (defvar erlang-font-lock-keywords erlang-font-lock-keywords-3
902 ;; DocStringCopy: erlang-font-lock-keywords
903 "Font-lock keywords used by Erlang Mode.
905 There exists three levels of Font Lock keywords for Erlang:
906 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
907 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
908 `erlang-font-lock-keywords-3' - Variables, macros and records.
910 To use a specific level, please set the variable
911 `font-lock-maximum-decoration' to the appropriate level. Note that the
912 variable must be set before Erlang mode is activated.
914 Example:
915 (setq font-lock-maximum-decoration 2)")
918 (defvar erlang-font-lock-syntax-table nil
919 "Syntax table used by Font Lock mode.
921 The difference between this and the standard Erlang Mode
922 syntax table is that `_' is treated as part of words by
923 this syntax table.
925 Unfortunately, XEmacs hasn't got support for a special Font
926 Lock syntax table. The effect is that `apply' in the atom
927 `foo_apply' will be highlighted as a bif.")
930 ;;; Avoid errors while compiling this file.
932 ;; `eval-when-compile' is not defined in Emacs 18. We define it as a
933 ;; no-op.
934 (or (fboundp 'eval-when-compile)
935 (defmacro eval-when-compile (&rest rest) nil))
937 ;; These umm...functions are new in Emacs 20. And, yes, until version
938 ;; 19.27 Emacs backquotes were this ugly.
940 (or (fboundp 'unless)
941 (defmacro unless (condition &rest body)
942 "(unless CONDITION BODY...): If CONDITION is false, do BODY, else return nil."
943 (` (if (, condition)
945 (,@ body)))))
947 (or (fboundp 'when)
948 (defmacro when (condition &rest body)
949 "(when CONDITION BODY...): If CONDITION is true, do BODY, else return nil."
950 (` (if (, condition)
951 (progn (,@ body))
952 nil))))
954 (or (fboundp 'char-before)
955 (defmacro char-before (&optional pos)
956 "Return the character in the current buffer just before POS."
957 (` (char-after (1- (or (, pos) (point)))))))
959 (eval-when-compile
960 (if (or (featurep 'bytecomp)
961 (featurep 'byte-compile))
962 (progn
963 (cond ((string-match "Lucid\\|XEmacs" emacs-version)
964 (put 'comment-indent-hook 'byte-obsolete-variable nil)
965 ;; Do not warn for unused variables
966 ;; when compiling under XEmacs.
967 (setq byte-compile-warnings
968 '(free-vars unresolved callargs redefine))))
969 (require 'comint)
970 (require 'compile))))
973 (defun erlang-version ()
974 "Return the current version of Erlang mode."
975 (interactive)
976 (if (interactive-p)
977 (message "Erlware Erlang mode version %s" erlang-version))
978 erlang-version)
981 ;;;###autoload
982 (defun erlang-mode ()
983 "Major mode for editing Erlang source files in Emacs.
984 It knows about syntax and comment, it can indent code, it is capable
985 of fontifying the source file, the TAGS commands are aware of Erlang
986 modules, and the Erlang man pages can be accessed.
988 Should this module, \"erlang.el\", be installed properly, Erlang mode
989 is activated whenever an Erlang source or header file is loaded into
990 Emacs. To indicate this, the mode line should contain the word
991 \"Erlang\".
993 The main feature of Erlang mode is indentation, press TAB and the
994 current line will be indented correctly.
996 Comments starting with one `%' are indented with the same indentation
997 as code. Comments starting with at least two `%':s are indented to
998 the first column.
1000 However, Erlang mode contains much more, this is a list of the most
1001 useful commands:
1002 TAB - Indent the line.
1003 C-c C-q - Indent current function.
1004 M-; - Create a comment at the end of the line.
1005 M-q - Fill a comment, i.e. wrap lines so that they (hopefully)
1006 will look better.
1007 M-a - Goto the beginning of an Erlang clause.
1008 M-C-a - Ditto for function.
1009 M-e - Goto the end of an Erlang clause.
1010 M-C-e - Ditto for function.
1011 M-h - Mark current Erlang clause.
1012 M-C-h - Ditto for function.
1013 C-c C-z - Start, or switch to, an inferior Erlang shell.
1014 C-c C-k - Compile current file.
1015 C-x ` - Next error.
1016 , - Electric comma.
1017 ; - Electric semicolon.
1019 Erlang mode check the name of the file against the module name when
1020 saving, whenever a mismatch occurs Erlang mode offers to modify the
1021 source.
1023 The variable `erlang-electric-commands' controls the electric
1024 commands. To deactivate all of them, set it to nil.
1026 There exists a large number of commands and variables in the Erlang
1027 module. Please press `M-x apropos RET erlang RET' to see a complete
1028 list. Press `C-h f name-of-function RET' and `C-h v name-of-variable
1029 RET'to see the full description of functions and variables,
1030 respectively.
1032 On entry to this mode the contents of the hook `erlang-mode-hook' is
1033 executed.
1035 Please see the beginning of the file `erlang.el' for more information
1036 and examples of hooks.
1038 Other commands:
1039 \\{erlang-mode-map}"
1040 (interactive)
1041 (kill-all-local-variables)
1042 (setq major-mode 'erlang-mode)
1043 (setq mode-name "Erlang")
1044 (erlang-syntax-table-init)
1045 (use-local-map erlang-mode-map)
1046 (erlang-electric-init)
1047 (erlang-menu-init)
1048 (erlang-mode-variables)
1049 (erlang-check-module-name-init)
1050 (erlang-add-compilation-alist erlang-error-regexp-alist)
1051 (erlang-man-init)
1052 (erlang-tags-init)
1053 (erlang-font-lock-init)
1054 (erlang-skel-init)
1055 (run-hooks 'erlang-mode-hook)
1056 (if (zerop (buffer-size))
1057 (run-hooks 'erlang-new-file-hook))
1058 ;; Doesn't exist in Emacs v21.4; required by Emacs v23.
1059 (if (boundp 'after-change-major-mode-hook)
1060 (run-hooks 'after-change-major-mode-hook)))
1063 (defun erlang-syntax-table-init ()
1064 (if (null erlang-mode-syntax-table)
1065 (let ((table (make-syntax-table)))
1066 (modify-syntax-entry ?\n ">" table)
1067 (modify-syntax-entry ?\" "\"" table)
1068 (modify-syntax-entry ?# "." table)
1069 (modify-syntax-entry ?$ "\\" table)
1070 (modify-syntax-entry ?% "<" table)
1071 (modify-syntax-entry ?& "." table)
1072 (modify-syntax-entry ?\' "w" table)
1073 (modify-syntax-entry ?* "." table)
1074 (modify-syntax-entry ?+ "." table)
1075 (modify-syntax-entry ?- "." table)
1076 (modify-syntax-entry ?/ "." table)
1077 (modify-syntax-entry ?: "." table)
1078 (modify-syntax-entry ?< "." table)
1079 (modify-syntax-entry ?= "." table)
1080 (modify-syntax-entry ?> "." table)
1081 (modify-syntax-entry ?\\ "\\" table)
1082 (modify-syntax-entry ?_ "_" table)
1083 (modify-syntax-entry ?| "." table)
1085 (setq erlang-mode-syntax-table table)))
1087 (set-syntax-table erlang-mode-syntax-table))
1090 (defun erlang-electric-init ()
1091 ;; Set up electric character functions to work with
1092 ;; delsel/pending-del mode. Also, set up text properties for bit
1093 ;; syntax handling.
1094 (mapcar #'(lambda (cmd)
1095 (put cmd 'delete-selection t) ;for delsel (Emacs)
1096 (put cmd 'pending-delete t)) ;for pending-del (XEmacs)
1097 '(erlang-electric-semicolon
1098 erlang-electric-comma
1099 erlang-electric-gt))
1101 (put 'bitsyntax-open-outer 'syntax-table '(4 . ?>))
1102 (put 'bitsyntax-open-outer 'rear-nonsticky '(category))
1103 (put 'bitsyntax-open-inner 'rear-nonsticky '(category))
1104 (put 'bitsyntax-close-inner 'rear-nonsticky '(category))
1105 (put 'bitsyntax-close-outer 'syntax-table '(5 . ?<))
1106 (put 'bitsyntax-close-outer 'rear-nonsticky '(category))
1107 (setq parse-sexp-lookup-properties 't))
1110 (defun erlang-mode-variables ()
1111 (or erlang-mode-abbrev-table
1112 (define-abbrev-table 'erlang-mode-abbrev-table ()))
1113 (setq local-abbrev-table erlang-mode-abbrev-table)
1114 (make-local-variable 'paragraph-start)
1115 (setq paragraph-start (concat "^$\\|" page-delimiter))
1116 (make-local-variable 'paragraph-separate)
1117 (setq paragraph-separate paragraph-start)
1118 (make-local-variable 'paragraph-ignore-fill-prefix)
1119 (setq paragraph-ignore-fill-prefix t)
1120 (make-local-variable 'require-final-newline)
1121 (setq require-final-newline t)
1122 (make-local-variable 'defun-prompt-regexp)
1123 (setq defun-prompt-regexp erlang-defun-prompt-regexp)
1124 (make-local-variable 'comment-start)
1125 (setq comment-start "%")
1126 (make-local-variable 'comment-start-skip)
1127 (setq comment-start-skip "%+\\s *")
1128 (make-local-variable 'indent-line-function)
1129 (setq indent-line-function 'erlang-indent-command)
1130 (make-local-variable 'indent-region-function)
1131 (setq indent-region-function 'erlang-indent-region)
1132 (set (make-local-variable 'comment-indent-function) 'erlang-comment-indent)
1133 (if (<= erlang-emacs-major-version 18)
1134 (set (make-local-variable 'comment-indent-hook) 'erlang-comment-indent))
1135 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1136 (set (make-local-variable 'dabbrev-case-fold-search) nil)
1137 (set (make-local-variable 'imenu-prev-index-position-function)
1138 'erlang-beginning-of-function)
1139 (set (make-local-variable 'imenu-extract-index-name-function)
1140 'erlang-get-function-name-and-arity)
1141 (set (make-local-variable 'tempo-match-finder)
1142 "[^-a-zA-Z0-9_]\\([-a-zA-Z0-9_]*\\)\\=")
1143 (set (make-local-variable 'beginning-of-defun-function)
1144 'erlang-beginning-of-function)
1145 (set (make-local-variable 'end-of-defun-function) 'erlang-end-of-function)
1146 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil)
1147 (set (make-local-variable 'fill-paragraph-function) 'erlang-fill-paragraph)
1148 (set (make-local-variable 'comment-add)
1149 (if erlang-oldstyle-comment-indent 1 0))
1150 (set (make-local-variable 'outline-regexp) "[[:lower:]0-9_]+ *(.*) *-> *$")
1151 (set (make-local-variable 'outline-level) (lambda () 1))
1152 (set (make-local-variable 'add-log-current-defun-function)
1153 'erlang-current-defun))
1156 ;; Compilation.
1158 ;; The following code is compatible with the standard package `compilation',
1159 ;; making it possible to go to errors using `erlang-next-error' (or just
1160 ;; `next-error' in Emacs 21).
1162 ;; The normal `compile' command works of course. For best result, please
1163 ;; execute `make' with the `-w' flag.
1165 ;; Please see the variables named `compiling-..' above.
1167 (defun erlang-add-compilation-alist (alist)
1168 (require 'compile)
1169 (cond ((boundp 'compilation-error-regexp-alist) ; Emacs 19
1170 (while alist
1171 (or (assoc (car (car alist)) compilation-error-regexp-alist)
1172 (setq compilation-error-regexp-alist
1173 (cons (car alist) compilation-error-regexp-alist)))
1174 (setq alist (cdr alist))))
1175 ((boundp 'compilation-error-regexp)
1176 ;; Emacs 18, Only one regexp is allowed.
1177 (funcall (symbol-function 'set)
1178 'compilation-error-regexp (car (car alist))))))
1180 (defun erlang-font-lock-init ()
1181 "Initialize Font Lock for Erlang mode."
1182 (or erlang-font-lock-syntax-table
1183 (setq erlang-font-lock-syntax-table
1184 (let ((table (copy-syntax-table erlang-mode-syntax-table)))
1185 (modify-syntax-entry ?_ "w" table)
1186 table)))
1187 (set (make-local-variable 'font-lock-syntax-table)
1188 erlang-font-lock-syntax-table)
1189 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1190 'erlang-beginning-of-clause)
1191 (make-local-variable 'font-lock-keywords)
1192 (let ((level (cond ((boundp 'font-lock-maximum-decoration)
1193 (symbol-value 'font-lock-maximum-decoration))
1194 ((boundp 'font-lock-use-maximal-decoration)
1195 (symbol-value 'font-lock-use-maximal-decoration))
1196 (t nil))))
1197 (if (consp level)
1198 (setq level (cdr-safe (or (assq 'erlang-mode level)
1199 (assq t level)))))
1200 ;; `level' can here be:
1201 ;; A number - The fontification level
1202 ;; nil - Use the default
1203 ;; t - Use maximum
1204 (cond ((eq level nil)
1205 (set 'font-lock-keywords erlang-font-lock-keywords))
1206 ((eq level 1)
1207 (set 'font-lock-keywords erlang-font-lock-keywords-1))
1208 ((eq level 2)
1209 (set 'font-lock-keywords erlang-font-lock-keywords-2))
1211 (set 'font-lock-keywords erlang-font-lock-keywords-3))))
1213 ;; Modern font-locks can handle the above much more elegantly:
1214 (set (make-local-variable 'font-lock-defaults)
1215 '((erlang-font-lock-keywords erlang-font-lock-keywords-1
1216 erlang-font-lock-keywords-2 erlang-font-lock-keywords-3)
1217 nil nil ((?_ . "w")) erlang-beginning-of-clause
1218 (font-lock-mark-block-function . erlang-mark-clause))))
1222 ;; Useful when defining your own keywords.
1223 (defun erlang-font-lock-set-face (ks &rest faces)
1224 "Replace the face components in a list of keywords.
1226 The first argument, KS, is a list of keywords. The rest of the
1227 arguments are expressions to replace the face information with. The
1228 first expression replaces the face of the first keyword, the second
1229 expression the second keyword etc.
1231 Should an expression be nil, the face of the corresponding keyword is
1232 not changed.
1234 Should fewer expressions than keywords be given, the last expression
1235 is used for all remaining keywords.
1237 Normally, the expressions are just atoms representing the new face.
1238 They could however be more complex, returning different faces in
1239 different situations.
1241 This function only handles keywords with elements on the forms:
1242 (REGEXP NUMBER FACE)
1243 (REGEXP NUMBER FACE OVERWRITE)
1245 This could be used when defining your own special font-lock setup, e.g:
1247 \(setq my-font-lock-keywords
1248 (append erlang-font-lock-keywords-func
1249 erlang-font-lock-keywords-dollar
1250 (erlang-font-lock-set-face
1251 erlang-font-lock-keywords-macros 'my-neon-green-face)
1252 (erlang-font-lock-set-face
1253 erlang-font-lock-keywords-lc 'my-deep-red 'my-light-red)
1254 erlang-font-lock-keywords-attr))
1256 For a more elaborate example, please see the beginning of the file
1257 `erlang.el'."
1258 (let ((res '()))
1259 (while ks
1260 (let* ((regexp (car (car ks)))
1261 (number (car (cdr (car ks))))
1262 (new-face (if (and faces (car faces))
1263 (car faces)
1264 (car (cdr (cdr (car ks))))))
1265 (overwrite (car (cdr (cdr (cdr (car ks))))))
1266 (new-keyword (list regexp number new-face)))
1267 (if overwrite (nconc new-keyword (list overwrite)))
1268 (setq res (cons new-keyword res))
1269 (setq ks (cdr ks))
1270 (if (and faces (cdr faces))
1271 (setq faces (cdr faces)))))
1272 (nreverse res)))
1275 (defun erlang-font-lock-level-0 ()
1276 ;; DocStringOrig: font-cmd
1277 "Unfontify current buffer."
1278 (interactive)
1279 (font-lock-mode 0))
1282 (defun erlang-font-lock-level-1 ()
1283 ;; DocStringCopy: font-cmd
1284 "Fontify current buffer at level 1.
1285 This highlights function headers, reserved keywords, strings and comments."
1286 (interactive)
1287 (require 'font-lock)
1288 (set 'font-lock-keywords erlang-font-lock-keywords-1)
1289 (font-lock-mode 1)
1290 (funcall (symbol-function 'font-lock-fontify-buffer)))
1293 (defun erlang-font-lock-level-2 ()
1294 ;; DocStringCopy: font-cmd
1295 "Fontify current buffer at level 2.
1296 This highlights level 1 features (see `erlang-font-lock-level-1')
1297 plus bifs, guards and `single quotes'."
1298 (interactive)
1299 (require 'font-lock)
1300 (set 'font-lock-keywords erlang-font-lock-keywords-2)
1301 (font-lock-mode 1)
1302 (funcall (symbol-function 'font-lock-fontify-buffer)))
1305 (defun erlang-font-lock-level-3 ()
1306 ;; DocStringCopy: font-cmd
1307 "Fontify current buffer at level 3.
1308 This highlights level 2 features (see `erlang-font-lock-level-2')
1309 plus variables, macros and records."
1310 (interactive)
1311 (require 'font-lock)
1312 (set 'font-lock-keywords erlang-font-lock-keywords-3)
1313 (font-lock-mode 1)
1314 (funcall (symbol-function 'font-lock-fontify-buffer)))
1317 (defun erlang-menu-init ()
1318 "Init menus for Erlang mode.
1320 The variable `erlang-menu-items' contain a description of the Erlang
1321 mode menu. Normally, the list contains atoms, representing variables
1322 bound to pieces of the menu.
1324 Personal extensions could be added to `erlang-menu-personal-items'.
1326 This function should be called if any variable describing the
1327 menu configuration is changed."
1328 (erlang-menu-install "Erlang" erlang-menu-items erlang-mode-map t))
1331 (defun erlang-menu-install (name items keymap &optional popup)
1332 "Install a menu in Emacs or XEmacs based on an abstract description.
1334 NAME is the name of the menu.
1336 ITEMS is a list. The elements are either nil representing a horizontal
1337 line or a list with two or three elements. The first is the name of
1338 the menu item, the second the function to call, or a submenu, on the
1339 same same form as ITEMS. The third optional element is an expression
1340 which is evaluated every time the menu is displayed. Should the
1341 expression evaluate to nil the menu item is ghosted.
1343 KEYMAP is the keymap to add to menu to. (When using XEmacs, the menu
1344 will only be visible when this menu is the global, the local, or an
1345 activate minor mode keymap.)
1347 If POPUP is non-nil, the menu is bound to the XEmacs `mode-popup-menu'
1348 variable, i.e. it will popup when pressing the right mouse button.
1350 Please see the variable `erlang-menu-base-items'."
1351 (cond (erlang-xemacs-p
1352 (let ((menu (erlang-menu-xemacs name items keymap)))
1353 ;; We add the menu to the global menubar.
1354 (funcall (symbol-function 'add-submenu) nil menu)
1355 (setcdr erlang-xemacs-popup-menu (cdr menu))
1356 (if (and popup (boundp 'mode-popup-menu))
1357 (funcall (symbol-function 'set)
1358 'mode-popup-menu erlang-xemacs-popup-menu))))
1359 ((>= erlang-emacs-major-version 19)
1360 (define-key keymap (vector 'menu-bar (intern name))
1361 (erlang-menu-make-keymap name items)))
1362 (t nil)))
1365 (defun erlang-menu-make-keymap (name items)
1366 "Build a menu for Emacs 19."
1367 (let ((menumap (funcall (symbol-function 'make-sparse-keymap)
1368 name))
1369 (count 0)
1370 id def first second third)
1371 (setq items (reverse items))
1372 (while items
1373 ;; Replace any occurrence of atoms by their value.
1374 (while (and items (atom (car items)) (not (null (car items))))
1375 (if (and (boundp (car items))
1376 (listp (symbol-value (car items))))
1377 (setq items (append (reverse (symbol-value (car items)))
1378 (cdr items)))
1379 (setq items (cdr items))))
1380 (setq first (car-safe (car items)))
1381 (setq second (car-safe (cdr-safe (car items))))
1382 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1383 (cond ((null first)
1384 (setq count (+ count 1))
1385 (setq id (intern (format "separator-%d" count)))
1386 (setq def '("--" . nil)))
1387 ((and (consp second) (eq (car second) 'lambda))
1388 (setq count (+ count 1))
1389 (setq id (intern (format "lambda-%d" count)))
1390 (setq def (cons first second)))
1391 ((symbolp second)
1392 (setq id second)
1393 (setq def (cons first second)))
1395 (setq count (+ count 1))
1396 (setq id (intern (format "submenu-%d" count)))
1397 (setq def (erlang-menu-make-keymap first second))))
1398 (define-key menumap (vector id) def)
1399 (if third
1400 (put id 'menu-enable third))
1401 (setq items (cdr items)))
1402 (cons name menumap)))
1405 (defun erlang-menu-xemacs (name items &optional keymap)
1406 "Build a menu for XEmacs."
1407 (let ((res '())
1408 first second third entry)
1409 (while items
1410 ;; Replace any occurrence of atoms by their value.
1411 (while (and items (atom (car items)) (not (null (car items))))
1412 (if (and (boundp (car items))
1413 (listp (symbol-value (car items))))
1414 (setq items (append (reverse (symbol-value (car items)))
1415 (cdr items)))
1416 (setq items (cdr items))))
1417 (setq first (car-safe (car items)))
1418 (setq second (car-safe (cdr-safe (car items))))
1419 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1420 (cond ((null first)
1421 (setq res (cons "------" res)))
1422 ((symbolp second)
1423 (setq res (cons (vector first second (or third t)) res)))
1424 ((and (consp second) (eq (car second) 'lambda))
1425 (setq res (cons (vector first (list 'call-interactively second)
1426 (or third t)) res)))
1428 (setq res (cons (cons first
1429 (cdr (erlang-menu-xemacs
1430 first second)))
1431 res))))
1432 (setq items (cdr items)))
1433 (setq res (reverse res))
1434 ;; When adding a menu to a minor-mode keymap under Emacs,
1435 ;; it disappears when the mode is disabled. The expression
1436 ;; generated below imitates this behaviour.
1437 ;; (This could be expressed much clearer using backquotes,
1438 ;; but I don't want to pull in every package.)
1439 (if keymap
1440 (let ((expr (list 'or
1441 (list 'eq keymap 'global-map)
1442 (list 'eq keymap (list 'current-local-map))
1443 (list 'symbol-value
1444 (list 'car-safe
1445 (list 'rassq
1446 keymap
1447 'minor-mode-map-alist))))))
1448 (setq res (cons ':included (cons expr res)))))
1449 (cons name res)))
1452 (defun erlang-menu-substitute (items alist)
1453 "Substitute functions in menu described by ITEMS.
1455 The menu ITEMS is updated destructively.
1457 ALIST is list of pairs where the car is the old function and cdr the new."
1458 (let (first second pair)
1459 (while items
1460 (setq first (car-safe (car items)))
1461 (setq second (car-safe (cdr-safe (car items))))
1462 (cond ((null first))
1463 ((symbolp second)
1464 (setq pair (and second (assq second alist)))
1465 (if pair
1466 (setcar (cdr (car items)) (cdr pair))))
1467 ((and (consp second) (eq (car second) 'lambda)))
1469 (erlang-menu-substitute second alist)))
1470 (setq items (cdr items)))))
1473 (defun erlang-menu-add-above (entry above items)
1474 "Add menu ENTRY above menu entry ABOVE in menu ITEMS.
1475 Do nothing if the items already should be in the menu.
1476 Should ABOVE not be in the list, the entry is added at
1477 the bottom of the menu.
1479 The new menu is returned. No guarantee is given that the original
1480 menu is left unchanged.
1482 The equality test is performed by `eq'.
1484 Example: (erlang-menu-add-above 'my-erlang-menu-items
1485 'erlang-menu-man-items)"
1486 (erlang-menu-add-below entry above items t))
1489 (defun erlang-menu-add-below (entry below items &optional above-p)
1490 "Add menu ENTRY below menu items BELOW in the Erlang menu.
1491 Do nothing if the items already should be in the menu.
1492 Should BELOW not be in the list, items is added at the bottom
1493 of the menu.
1495 The new menu is returned. No guarantee is given that the original
1496 menu is left unchanged.
1498 The equality test is performed by `eq'.
1500 Example:
1502 \(setq erlang-menu-items
1503 (erlang-menu-add-below 'my-erlang-menu-items
1504 'erlang-menu-base-items
1505 erlang-menu-items))"
1506 (if (memq entry items)
1507 items ; Return the original menu.
1508 (let ((head '())
1509 (done nil)
1510 res)
1511 (while (not done)
1512 (cond ((null items)
1513 (setq res (append head (list entry)))
1514 (setq done t))
1515 ((eq below (car items))
1516 (setq res
1517 (if above-p
1518 (append head (cons entry items))
1519 (append head (cons (car items)
1520 (cons entry (cdr items))))))
1521 (setq done t))
1523 (setq head (append head (list (car items))))
1524 (setq items (cdr items)))))
1525 res)))
1527 (defun erlang-menu-delete (entry items)
1528 "Delete ENTRY from menu ITEMS.
1530 The new menu is returned. No guarantee is given that the original
1531 menu is left unchanged."
1532 (delq entry items))
1534 ;; Man code:
1536 (defun erlang-man-init ()
1537 "Add menus containing the manual pages of the Erlang.
1539 The variable `erlang-man-dirs' contains entries describing
1540 the location of the manual pages."
1541 (interactive)
1542 (if erlang-man-inhibit
1544 (setq erlang-menu-man-items
1545 '(nil
1546 ("Man - Function" erlang-man-function)))
1547 (if erlang-man-dirs
1548 (setq erlang-menu-man-items
1549 (append erlang-menu-man-items
1550 (erlang-man-make-top-menu erlang-man-dirs))))
1551 (setq erlang-menu-items
1552 (erlang-menu-add-above 'erlang-menu-man-items
1553 'erlang-menu-version-items
1554 erlang-menu-items))
1555 (erlang-menu-init)))
1558 (defun erlang-man-uninstall ()
1559 "Remove the man pages from the Erlang mode."
1560 (interactive)
1561 (setq erlang-menu-items
1562 (erlang-menu-delete 'erlang-menu-man-items erlang-menu-items))
1563 (erlang-menu-init))
1566 ;; The man menu is a hierarchal structure, with the manual sections
1567 ;; at the top, described by `erlang-man-dirs'. The next level could
1568 ;; either be the manual pages if not to many, otherwise it is an index
1569 ;; menu whose submenus will contain up to `erlang-man-max-menu-size'
1570 ;; manual pages.
1572 (defun erlang-man-make-top-menu (dir-list)
1573 "Create one menu entry per element of DIR-LIST.
1574 The format is described in the documentation of `erlang-man-dirs'."
1575 (let ((menu '())
1576 dir)
1577 (while dir-list
1578 (setq dir (cond ((nth 2 (car dir-list))
1579 ;; Relative to `erlang-man-root-dir'.
1580 (and (stringp erlang-man-root-dir)
1581 (concat erlang-man-root-dir (nth 1 (car dir-list)))))
1583 ;; Absolute
1584 (nth 1 (car dir-list)))))
1585 (if (and dir
1586 (file-readable-p dir))
1587 (setq menu (cons (list (car (car dir-list))
1588 (erlang-man-make-middle-menu
1589 (erlang-man-get-files dir)))
1590 menu)))
1591 (setq dir-list (cdr dir-list)))
1592 ;; Should no menus be found, generate a menu item which
1593 ;; will display a help text, when selected.
1594 (if menu
1595 (nreverse menu)
1596 '(("Man Pages"
1597 (("Error! Why?" erlang-man-describe-error)))))))
1600 ;; Should the menu be to long, let's split it into a number of
1601 ;; smaller menus. Warning, this code contains beautiful
1602 ;; destructive operations!
1603 (defun erlang-man-make-middle-menu (filelist)
1604 "Create the second level menu from FILELIST.
1606 Should the list be longer than `erlang-man-max-menu-size', a tree of
1607 menus is created."
1608 (if (<= (length filelist) erlang-man-max-menu-size)
1609 (erlang-man-make-menu filelist)
1610 (let ((menu '())
1611 (filelist (copy-sequence filelist))
1612 segment submenu pair)
1613 (while filelist
1614 (setq pair (nthcdr (- erlang-man-max-menu-size 1) filelist))
1615 (setq segment filelist)
1616 (if (null pair)
1617 (setq filelist nil)
1618 (setq filelist (cdr pair))
1619 (setcdr pair nil))
1620 (setq submenu (erlang-man-make-menu segment))
1621 (setq menu (cons (list (concat (car (car submenu))
1622 " -- "
1623 (car (car (reverse submenu))))
1624 submenu)
1625 menu)))
1626 (nreverse menu))))
1629 (defun erlang-man-make-menu (filelist)
1630 "Make a leaf menu based on FILELIST."
1631 (let ((menu '())
1632 item)
1633 (while filelist
1634 (setq item (erlang-man-make-menu-item (car filelist)))
1635 (if item
1636 (setq menu (cons item menu)))
1637 (setq filelist (cdr filelist)))
1638 (nreverse menu)))
1641 (defun erlang-man-make-menu-item (file)
1642 "Create a menu item containing the name of the man page."
1643 (and (string-match ".*/\\([^/]+\\)\\.[^.]$" file)
1644 (let ((page (substring file (match-beginning 1) (match-end 1))))
1645 (list (capitalize page)
1646 (list 'lambda '()
1647 '(interactive)
1648 (list 'funcall 'erlang-man-display-function
1649 file))))))
1652 (defun erlang-man-get-files (dir)
1653 "Return files in directory DIR."
1654 (directory-files dir t ".*\\.[0-9]\\'"))
1657 (defun erlang-man-module (&optional module)
1658 "Find manual page for MODULE, defaults to module of function under point.
1659 This function is aware of imported functions."
1660 (interactive
1661 (list (let* ((mod (car-safe (erlang-get-function-under-point)))
1662 (input (read-string
1663 (format "Manual entry for module%s: "
1664 (if (or (null mod) (string= mod ""))
1666 (format " (default %s)" mod))))))
1667 (if (string= input "")
1669 input))))
1670 (or module (setq module (car (erlang-get-function-under-point))))
1671 (if (or (null module) (string= module ""))
1672 (error "No Erlang module name given"))
1673 (let ((dir-list erlang-man-dirs)
1674 (pat (concat "/" (regexp-quote module) "\\.[^.]$"))
1675 (file nil)
1676 file-list)
1677 (while (and dir-list (null file))
1678 (setq file-list (erlang-man-get-files
1679 (if (nth 2 (car dir-list))
1680 (concat erlang-man-root-dir (nth 1 (car dir-list)))
1681 (nth 1 (car dir-list)))))
1682 (while (and file-list (null file))
1683 (if (string-match pat (car file-list))
1684 (setq file (car file-list)))
1685 (setq file-list (cdr file-list)))
1686 (setq dir-list (cdr dir-list)))
1687 (if file
1688 (funcall erlang-man-display-function file)
1689 (error "No manual page for module %s found" module))))
1692 ;; Warning, the function `erlang-man-function' is a hack!
1693 ;; It links itself into the man code in a non-clean way. I have
1694 ;; chosen to keep it since it provides a very useful functionality
1695 ;; which is not possible to achieve using a clean approach.
1696 ;; / AndersL
1698 (defvar erlang-man-function-name nil
1699 "Name of function for last `erlang-man-function' call.
1700 Used for communication between `erlang-man-function' and the
1701 patch to `Man-notify-when-ready'.")
1703 (defun erlang-man-function (&optional name)
1704 "Find manual page for NAME, where NAME is module:function.
1705 The entry for `function' is displayed.
1707 This function is aware of imported functions."
1708 (interactive
1709 (list (let* ((mod-func (erlang-get-function-under-point))
1710 (mod (car-safe mod-func))
1711 (func (nth 1 mod-func))
1712 (input (read-string
1713 (format
1714 "Manual entry for `module:func' or `module'%s: "
1715 (if (or (null mod) (string= mod ""))
1717 (format " (default %s:%s)" mod func))))))
1718 (if (string= input "")
1719 (if (and mod func)
1720 (concat mod ":" func)
1721 mod)
1722 input))))
1723 ;; Emacs 18 doesn't provide `man'...
1724 (condition-case nil
1725 (require 'man)
1726 (error nil))
1727 (let ((modname nil)
1728 (funcname nil))
1729 (cond ((null name)
1730 (let ((mod-func (erlang-get-function-under-point)))
1731 (setq modname (car-safe mod-func))
1732 (setq funcname (nth 1 mod-func))))
1733 ((string-match ":" name)
1734 (setq modname (substring name 0 (match-beginning 0)))
1735 (setq funcname (substring name (match-end 0) nil)))
1736 ((stringp name)
1737 (setq modname name)))
1738 (if (or (null modname) (string= modname ""))
1739 (error "No Erlang module name given"))
1740 (cond ((fboundp 'Man-notify-when-ready)
1741 ;; Emacs 19: The man command could possibly start an
1742 ;; asynchronous process, i.e. we must hook ourselves into
1743 ;; the system to be activated when the man-process
1744 ;; terminates.
1745 (if (null funcname)
1747 (erlang-man-patch-notify)
1748 (setq erlang-man-function-name funcname))
1749 (condition-case nil
1750 (erlang-man-module modname)
1751 (error (setq erlang-man-function-name nil))))
1753 (erlang-man-module modname)
1754 (if funcname
1755 (erlang-man-find-function
1756 (or (get-buffer "*Manual Entry*") ; Emacs 18
1757 (current-buffer)) ; XEmacs
1758 funcname))))))
1761 ;; Should the defadvice be at the top level, the package `advice' would
1762 ;; be required. Now it is only required when this functionality
1763 ;; is used. (Emacs 19 specific.)
1764 (defun erlang-man-patch-notify ()
1765 "Patch the function `Man-notify-when-ready' to search for function.
1766 The variable `erlang-man-function-name' is assumed to be bound to
1767 the function name, or to nil.
1769 The reason for patching a function is that under Emacs 19, the man
1770 command is executed asynchronously."
1771 (condition-case nil
1772 (require 'advice)
1773 ;; This should never happened since this is only called when
1774 ;; running under Emacs 19.
1775 (error (error (concat "This command needs the package `advice', "
1776 "please upgrade your Emacs."))))
1777 (require 'man)
1778 (defadvice Man-notify-when-ready
1779 (after erlang-Man-notify-when-ready activate)
1780 "Set point at the documentation of the function name in
1781 `erlang-man-function-name' when the man page is displayed."
1782 (if erlang-man-function-name
1783 (erlang-man-find-function (ad-get-arg 0) erlang-man-function-name))
1784 (setq erlang-man-function-name nil)))
1787 (defun erlang-man-find-function (buf func)
1788 "Find manual page for function in `erlang-man-function-name' in buffer BUF."
1789 (if func
1790 (let ((win (get-buffer-window buf)))
1791 (if win
1792 (progn
1793 (set-buffer buf)
1794 (goto-char (point-min))
1795 (if (re-search-forward
1796 (concat "^[ \t]+" func " ?(")
1797 (point-max) t)
1798 (progn
1799 (forward-word -1)
1800 (set-window-point win (point)))
1801 (message "Could not find function `%s'" func)))))))
1804 (defun erlang-man-display (file)
1805 "Display FILE as a `man' file.
1806 This is the default manual page display function.
1807 The variables `erlang-man-display-function' contains the function
1808 to be used."
1809 ;; Emacs 18 doesn't `provide' man.
1810 (condition-case nil
1811 (require 'man)
1812 (error nil))
1813 (if file
1814 (let ((process-environment (copy-sequence process-environment)))
1815 (if (string-match "\\(.*\\)/man[^/]*/\\([^/]+\\)\\.[^.]$" file)
1816 (let ((dir (substring file (match-beginning 1) (match-end 1)))
1817 (page (substring file (match-beginning 2) (match-end 2))))
1818 (if (fboundp 'setenv)
1819 (setenv "MANPATH" dir)
1820 ;; Emacs 18
1821 (setq process-environment (cons (concat "MANPATH=" dir)
1822 process-environment)))
1823 (cond ((not (and (not erlang-xemacs-p)
1824 (= erlang-emacs-major-version 19)
1825 (< erlang-emacs-minor-version 29)))
1826 (manual-entry page))
1828 ;; Emacs 19.28 and earlier versions of 19:
1829 ;; The manual-entry command unconditionally prompts
1830 ;; the user :-(
1831 (funcall (symbol-function 'Man-getpage-in-background)
1832 page))))
1833 (error "Can't find man page for %s\n" file)))))
1836 (defun erlang-man-describe-error ()
1837 "Describe why the manual pages weren't found."
1838 (interactive)
1839 (with-output-to-temp-buffer "*Erlang Man Error*"
1840 (princ "Normally, this menu should contain Erlang manual pages.
1842 In order to find the manual pages, the variable `erlang-man-root-dir'
1843 should be bound to the name of the directory containing the Erlang
1844 man pages. The name should not include the final slash.
1846 Practically, you should add a line on the following form to
1847 your ~/.emacs, or ask your system administrator to add it to
1848 the site init file:
1850 (setq erlang-man-root-dir \"/usr/local/erlang\")
1852 After installing the line, kill and restart Emacs, or restart Erlang
1853 mode with the command `M-x erlang-mode RET'.")))
1855 ;; Indentation code:
1857 (defun erlang-indent-command (&optional whole-exp)
1858 "Indent current line as Erlang code.
1859 With argument, indent any additional lines of the same clause
1860 rigidly along with this one."
1861 (interactive "P")
1862 (if whole-exp
1863 ;; If arg, always indent this line as Erlang
1864 ;; and shift remaining lines of clause the same amount.
1865 (let ((shift-amt (erlang-indent-line))
1866 beg end)
1867 (save-excursion
1868 (if erlang-tab-always-indent
1869 (beginning-of-line))
1870 (setq beg (point))
1871 (erlang-end-of-clause 1)
1872 (setq end (point))
1873 (goto-char beg)
1874 (forward-line 1)
1875 (setq beg (point)))
1876 (if (> end beg)
1877 (indent-code-rigidly beg end shift-amt "\n")))
1878 (if (and (not erlang-tab-always-indent)
1879 (save-excursion
1880 (skip-chars-backward " \t")
1881 (not (bolp))))
1882 (insert-tab)
1883 (erlang-indent-line))))
1886 (defun erlang-indent-line ()
1887 "Indent current line as Erlang code.
1888 Return the amount the indentation changed by."
1889 (let ((pos (- (point-max) (point)))
1890 indent beg
1891 shift-amt)
1892 (beginning-of-line 1)
1893 (setq beg (point))
1894 (skip-chars-forward " \t")
1895 (cond ((looking-at "%")
1896 (setq indent (funcall comment-indent-function))
1897 (setq shift-amt (- indent (current-column))))
1899 (setq indent (erlang-calculate-indent))
1900 (cond ((null indent)
1901 (setq indent (current-indentation)))
1902 ((eq indent t)
1903 ;; This should never occur here.
1904 (error "Erlang mode error"))
1905 ((= (char-syntax (following-char)) ?\))
1906 (setq indent (1- indent))))
1907 (setq shift-amt (- indent (current-column)))))
1908 (if (zerop shift-amt)
1910 (delete-region beg (point))
1911 (indent-to indent))
1912 ;; If initial point was within line's indentation, position
1913 ;; after the indentation. Else stay at same point in text.
1914 (if (> (- (point-max) pos) (point))
1915 (goto-char (- (point-max) pos)))
1916 shift-amt))
1919 (defun erlang-indent-region (beg end)
1920 "Indent region of Erlang code.
1922 This is automagically called by the user level function `indent-region'."
1923 (interactive "r")
1924 (save-excursion
1925 (let ((case-fold-search nil)
1926 (continue t)
1927 (from-end (- (point-max) end))
1928 indent-point;; The beginning of the current line
1929 indent;; The indent amount
1930 state)
1931 (goto-char beg)
1932 (beginning-of-line)
1933 (setq indent-point (point))
1934 (erlang-beginning-of-clause)
1935 ;; Parse the Erlang code from the beginning of the clause to
1936 ;; the beginning of the region.
1937 (while (< (point) indent-point)
1938 (setq state (erlang-partial-parse (point) indent-point state)))
1939 ;; Indent every line in the region
1940 (while continue
1941 (goto-char indent-point)
1942 (skip-chars-forward " \t")
1943 (cond ((looking-at "%")
1944 ;; Do not use our stack to help the user to customize
1945 ;; comment indentation.
1946 (setq indent (funcall comment-indent-function)))
1947 ((looking-at "$")
1948 ;; Don't indent empty lines.
1949 (setq indent 0))
1951 (setq indent
1952 (save-excursion
1953 (erlang-calculate-stack-indent (point) state)))
1954 (cond ((null indent)
1955 (setq indent (current-indentation)))
1956 ((eq indent t)
1957 ;; This should never occur here.
1958 (error "Erlang mode error"))
1959 ((= (char-syntax (following-char)) ?\))
1960 (setq indent (1- indent))))))
1961 (if (zerop (- indent (current-column)))
1963 (delete-region indent-point (point))
1964 (indent-to indent))
1965 ;; Find the next line in the region
1966 (goto-char indent-point)
1967 (save-excursion
1968 (forward-line 1)
1969 (setq indent-point (point)))
1970 (if (>= from-end (- (point-max) indent-point))
1971 (setq continue nil)
1972 (while (< (point) indent-point)
1973 (setq state (erlang-partial-parse
1974 (point) indent-point state))))))))
1977 (defun erlang-indent-current-buffer ()
1978 "Indent current buffer as Erlang code."
1979 (interactive)
1980 (save-excursion
1981 (save-restriction
1982 (widen)
1983 (erlang-indent-region (point-min) (point-max)))))
1986 (defun erlang-indent-function ()
1987 "Indent current Erlang function."
1988 (interactive)
1989 (save-excursion
1990 (let ((end (progn (erlang-end-of-function 1) (point)))
1991 (beg (progn (erlang-beginning-of-function 1) (point))))
1992 (erlang-indent-region beg end))))
1995 (defun erlang-indent-clause ()
1996 "Indent current Erlang clause."
1997 (interactive)
1998 (save-excursion
1999 (let ((end (progn (erlang-end-of-clause 1) (point)))
2000 (beg (progn (erlang-beginning-of-clause 1) (point))))
2001 (erlang-indent-region beg end))))
2004 (defmacro erlang-push (x stack) (list 'setq stack (list 'cons x stack)))
2005 (defmacro erlang-pop (stack) (list 'setq stack (list 'cdr stack)))
2006 ;; Would much prefer to make caddr a macro but this clashes.
2007 (defun erlang-caddr (x) (car (cdr (cdr x))))
2010 (defun erlang-calculate-indent (&optional parse-start)
2011 "Compute appropriate indentation for current line as Erlang code.
2012 Return nil if line starts inside string, t if in a comment."
2013 (save-excursion
2014 (let ((indent-point (point))
2015 (case-fold-search nil)
2016 (state nil))
2017 (if parse-start
2018 (goto-char parse-start)
2019 (erlang-beginning-of-clause))
2020 (while (< (point) indent-point)
2021 (setq state (erlang-partial-parse (point) indent-point state)))
2022 (erlang-calculate-stack-indent indent-point state))))
2024 (defun erlang-show-syntactic-information ()
2025 "Show syntactic information for current line."
2027 (interactive)
2029 (save-excursion
2030 (let ((starting-point (point))
2031 (case-fold-search nil)
2032 (state nil))
2033 (erlang-beginning-of-clause)
2034 (while (< (point) starting-point)
2035 (setq state (erlang-partial-parse (point) starting-point state)))
2036 (message "%S" state))))
2039 (defun erlang-partial-parse (from to &optional state)
2040 "Parse Erlang syntax starting at FROM until TO, with an optional STATE.
2041 Value is list (stack token-start token-type in-what)."
2042 (goto-char from) ; Start at the beginning
2043 (erlang-skip-blank to)
2044 (let ((cs (char-syntax (following-char)))
2045 (stack (car state))
2046 (token (point))
2047 in-what)
2048 (cond
2050 ;; Done: Return previous state.
2051 ((>= token to)
2052 (setq token (nth 1 state))
2053 (setq cs (nth 2 state))
2054 (setq in-what (nth 3 state)))
2056 ;; Word constituent: check and handle keywords.
2057 ((= cs ?w)
2058 (cond ((looking-at "\\(end\\|after\\)[^_a-zA-Z0-9]")
2059 ;; Must pop top icr layer, `after' will push a new
2060 ;; layer next.
2061 (progn
2062 (while (and stack (eq (car (car stack)) '->))
2063 (erlang-pop stack))
2064 (if (and stack (memq (car (car stack)) '(icr begin)))
2065 (erlang-pop stack))))
2066 ((looking-at "catch[^,\n\\of]*\n")
2067 ;; Must pop top icr layer, `catch' in try/catch
2068 ;;will push a new layer next.
2069 (progn
2070 (while (and stack (eq (car (car stack)) '->))
2071 (erlang-pop stack))
2072 (if (and stack (memq (car (car stack)) '(icr begin)))
2073 (erlang-pop stack))))
2075 (cond ((looking-at "\\(if\\|case\\|receive\\|try\\)[^_a-zA-Z0-9]")
2076 ;; Must push a new icr (if/case/receive) layer.
2077 (erlang-push (list 'icr token (current-column)) stack))
2078 ((looking-at "\\(fun\\)[^_a-zA-Z0-9]")
2079 ;; Push a new icr layer if we are defining a `fun'
2080 ;; expression, not when we are refering an existing
2081 ;; function.
2082 (if (save-excursion
2083 (goto-char (match-end 1))
2084 (erlang-skip-blank to)
2085 (eq (following-char) ?\())
2086 (erlang-push (list 'icr token (current-column)) stack)))
2087 ((looking-at "\\(begin\\|query\\)[^_a-zA-Z0-9]")
2088 (erlang-push (list 'begin token (current-column)) stack))
2089 ;; In test suites you may want to do something like
2090 ;; ?match(Mem when integer(Mem), mnesia:table_info(Tab,
2091 ;; memory)), and then the following if/case/receive
2092 ;; statement will mess up the indentation by fooling the
2093 ;; erlang mode to think the 'when' in the argument is a
2094 ;; "real" when. The following three clauses will avoid
2095 ;; this problem.
2096 ((looking-at "when[^->\.]*if[^->\.]*->"))
2097 ((looking-at "when[^->\.]*case[^->\.]*->"))
2098 ((looking-at "when[^->\.]*receive[^->\.]*->"))
2099 ;; Normal when case
2100 ((looking-at "when [^->\.]*->")
2101 (erlang-push (list 'when token (current-column)) stack))
2102 ((looking-at "after[.]+->")
2103 (erlang-push (list 'icr token (current-column)) stack))
2104 ((looking-at "after[^_a-zA-Z0-9->]")
2105 (erlang-push (list 'icr token (current-column)) stack)
2106 (erlang-push (list '-> token (current-column)) stack))
2107 ((looking-at "catch[^,\n\\of]*\n")
2108 (erlang-push (list 'icr token (current-column)) stack))
2110 (forward-sexp 1))
2112 ;; String: Try to skip over it. (Catch error if not complete.)
2113 ((= cs ?\")
2114 (condition-case nil
2115 (progn
2116 (forward-sexp 1)
2117 (if (> (point) to)
2118 (progn
2119 (setq in-what 'string)
2120 (goto-char to))))
2121 (error
2122 (setq in-what 'string)
2123 (goto-char to))))
2125 ;; Symbol constituent or punctuation
2127 ((memq cs '(?. ?_))
2128 (cond
2130 ;; Clause end
2131 ((= (following-char) ?\;)
2132 (if (and stack (eq (car (car stack)) '->))
2133 (erlang-pop stack))
2134 (forward-char 1))
2136 ;; Function end
2137 ((looking-at "\\.\\(\\s \\|\n\\|\\s<\\)")
2138 (setq stack nil)
2139 (forward-char 1))
2141 ;; Function head
2142 ((looking-at "->\\|:-")
2143 (save-excursion
2144 (back-to-indentation)
2145 (cond ((looking-at "after[^_a-zA-Z0-9]")
2146 (erlang-pop stack))))
2147 (if (and stack (eq (car (car stack)) 'when))
2148 (erlang-pop stack))
2149 (erlang-push (list '-> token (current-column)) stack)
2150 (forward-char 2))
2152 ;; List-comprehension divider
2153 ((looking-at "||")
2154 (erlang-push (list '|| token (current-column)) stack)
2155 (forward-char 2))
2157 ;; Parameter separator
2158 ((looking-at ",")
2159 (forward-char 1))
2161 ;; Bit-syntax open paren
2162 ((looking-at "<<")
2163 (erlang-push (list '\( token (current-column)) stack)
2164 (forward-char 2))
2166 ;; Bbit-syntax close paren
2167 ((looking-at ">>")
2168 (while (memq (car (car stack)) '(|| ->))
2169 (erlang-pop stack))
2170 (cond ((eq (car (car stack)) '\()
2171 (erlang-pop stack))
2172 ((memq (car (car stack)) '(icr begin))
2173 (error "Missing `end'"))
2175 (error "Unbalanced parentheses")))
2176 (forward-char 2))
2178 ;; Macro
2179 ((= (following-char) ??)
2180 ;; Skip over the ?
2181 (forward-char 1)
2184 ;; Other punctuation: Skip over it and any following punctuation
2185 ((= cs ?.)
2186 ;; Skip over all characters in the operand.
2187 (skip-syntax-forward "."))
2189 ;; Other char: Skip over it.
2191 (forward-char 1))))
2193 ;; Open parenthesis
2194 ((= cs ?\()
2195 (erlang-push (list '\( token (current-column)) stack)
2196 (forward-char 1))
2198 ;; Close parenthesis
2199 ((= cs ?\))
2200 (while (memq (car (car stack)) '(|| ->))
2201 (erlang-pop stack))
2202 (cond ((eq (car (car stack)) '\()
2203 (erlang-pop stack))
2204 ((eq (car (car stack)) 'icr)
2205 (erlang-pop stack)
2206 ;; Normal catch not try-catch might have caused icr
2207 ;; and then incr should be removed and is not an error.
2208 (if (eq (car (car stack)) '\()
2209 (erlang-pop stack)
2210 (else
2211 (error "Missing `end'"))
2213 ((eq (car (car stack)) 'begin)
2214 (error "Missing `end'")
2216 (error "Unbalanced parenthesis"))
2218 (forward-char 1))
2220 ;; Character quote: Skip it and the quoted char.
2221 ((= cs ?/)
2222 (forward-char 2))
2224 ;; Character escape: Skip it and the escape sequence.
2225 ((= cs ?\\)
2226 (forward-char 1)
2227 (skip-syntax-forward "w"))
2229 ;; Everything else
2231 (forward-char 1)))
2232 (list stack token cs in-what)))
2234 (defun erlang-calculate-stack-indent (indent-point state)
2235 "From the given last position and state (stack) calculate indentation.
2236 Return nil if inside string, t if in a comment."
2237 (let* ((stack (and state (car state)))
2238 (token (nth 1 state))
2239 (stack-top (and stack (car stack))))
2240 (cond ((null state) ;No state
2242 ((nth 3 state)
2243 ;; Return nil or t.
2244 (eq (nth 3 state) 'comment))
2245 ((null stack)
2246 (if (looking-at "when[^_a-zA-Z0-9]")
2247 erlang-indent-guard
2249 ((eq (car stack-top) '\()
2250 ;; Element of list, tuple or part of an expression,
2251 (if (null erlang-argument-indent)
2252 ;; indent to next column.
2253 (1+ (nth 2 stack-top))
2254 (goto-char (nth 1 stack-top))
2255 (cond ((looking-at "[({]\\s *\\($\\|%\\)")
2256 ;; Line ends with parenthesis.
2257 (+ (erlang-indent-find-preceding-expr)
2258 erlang-argument-indent))
2260 ;; Indent to the same column as the first
2261 ;; argument.
2262 (goto-char (1+ (nth 1 stack-top)))
2263 (skip-chars-forward " \t")
2264 (current-column)))))
2265 ((eq (car stack-top) 'icr)
2266 ;; The default indentation is the column of the option
2267 ;; directly following the keyword. (This does not apply to
2268 ;; `case'.) Should no option be on the same line, the
2269 ;; indentation is the indentation of the keyword +
2270 ;; `erlang-indent-level'.
2272 ;; `after' should be indented to the save level as the
2273 ;; corresponding receive.
2274 (if (looking-at "\\(after\\|catch\\)[^_a-zA-Z0-9]")
2275 (nth 2 stack-top)
2276 (save-excursion
2277 (goto-char (nth 1 stack-top))
2278 (if (looking-at "case[^_a-zA-Z0-9]")
2279 (+ (nth 2 stack-top) erlang-indent-level)
2280 (skip-chars-forward "a-z")
2281 (skip-chars-forward " \t")
2282 (if (memq (following-char) '(?% ?\n))
2283 (+ (nth 2 stack-top) erlang-indent-level)
2284 (current-column)))))
2286 ;; Real indentation, where operators create extra indentation etc.
2287 ((memq (car stack-top) '(-> || begin))
2288 (goto-char (nth 1 stack-top))
2289 ;; Check if there is more code after the '->' on the
2290 ;; same line. If so use this indentation as base, else
2291 ;; use parent indentation + 2 * level as base.
2292 (let ((off erlang-indent-level)
2293 (skip 2))
2294 (cond ((null (cdr stack))) ; Top level in function.
2295 ((eq (car stack-top) 'begin)
2296 (setq skip 5))
2297 ((eq (car stack-top) '->)
2298 (setq off (* 2 erlang-indent-level))))
2299 (let ((base (erlang-indent-find-base stack indent-point off skip)))
2300 ;; Look at last thing to see how we are to move relative
2301 ;; to the base.
2302 (goto-char token)
2303 (cond ((looking-at "||\\|,\\|->\\|:-")
2304 base)
2305 ((erlang-at-keyword)
2306 (+ (current-column) erlang-indent-level))
2307 ((or (= (char-syntax (following-char)) ?.)
2308 (erlang-at-operator))
2309 (+ base erlang-indent-level))
2311 (goto-char indent-point)
2312 (cond ((memq (following-char) '(?\( ?{))
2313 ;; Function application or record.
2314 (+ (erlang-indent-find-preceding-expr)
2315 erlang-argument-indent))
2316 ;; Empty line, or end; treat it as the end of
2317 ;; the block. (Here we have a choice: should
2318 ;; the user be forced to reindent continued
2319 ;; lines, or should the "end" be reindented?)
2320 ((looking-at "\\(end\\|after\\|catch\\)[^_a-zA-Z0-9]\\|$")
2321 (if (eq (car (car stack)) '->)
2322 (erlang-pop stack))
2323 (if stack
2324 (erlang-caddr (car stack))
2326 ;; Avoid treating comments a continued line.
2327 ((= (following-char) ?%)
2328 base)
2329 ;; Continued line (e.g. line beginning
2330 ;; with an operator.)
2331 (t (+ base erlang-indent-level)))))))
2333 ((eq (car stack-top) 'when)
2334 (goto-char (nth 1 stack-top))
2335 (if (looking-at "when\\s *\\($\\|%\\)")
2336 (progn
2337 (erlang-pop stack)
2338 (if (and stack (eq (nth 0 (car stack)) 'icr))
2339 (progn
2340 (goto-char (nth 1 (car stack)))
2341 (+ (nth 2 (car stack)) erlang-indent-guard
2342 ;; receive XYZ or receive
2343 ;; XYZ
2344 (if (looking-at "[a-z]+\\s *\\($\\|%\\)")
2345 erlang-indent-level
2346 (* 2 erlang-indent-level))))
2347 erlang-indent-guard))
2348 ;; "when" is followed by code, let's indent to the same
2349 ;; column.
2350 (forward-char 4) ; Skip "when"
2351 (skip-chars-forward " \t")
2352 (current-column))))))
2355 (defun erlang-indent-find-base (stack indent-point &optional offset skip)
2356 "Find the base column for current stack."
2357 (or skip (setq skip 2))
2358 (or offset (setq offset erlang-indent-level))
2359 (save-excursion
2360 (let* ((stack-top (car stack)))
2361 (goto-char (nth 1 stack-top))
2362 (forward-char skip)
2363 (if (looking-at "\\s *\\($\\|%\\)")
2364 (progn
2365 (if (memq (car stack-top) '(-> ||))
2366 (erlang-pop stack))
2367 ;; Take parent identation + offset,
2368 ;; else just erlang-indent-level if no parent
2369 (if stack
2370 (+ (erlang-caddr (car stack))
2371 offset)
2372 erlang-indent-level))
2373 (erlang-skip-blank indent-point)
2374 (current-column)))))
2377 ;; Does not handle `begin' .. `end'.
2378 (defun erlang-indent-find-preceding-expr ()
2379 "Return the first column of the preceding expression.
2380 This assumes that the preceding expression is either simple
2381 \(i.e. an atom) or parenthesized."
2382 (save-excursion
2383 (forward-sexp -1)
2384 (let ((col (current-column)))
2385 (skip-chars-backward " \t")
2386 ;; Needed to match the colon in "'foo':'bar'".
2387 (if (not (memq (preceding-char) '(?# ?:)))
2389 (backward-char 1)
2390 (forward-sexp -1)
2391 (current-column)))))
2394 (defun erlang-skip-blank (&optional lim)
2395 "Skip over whitespace and comments until limit reached."
2396 (or lim (setq lim (point-max)))
2397 (let (stop)
2398 (while (and (not stop) (< (point) lim))
2399 (cond ((= (following-char) ?%)
2400 (skip-chars-forward "^\n" lim))
2401 ((= (following-char) ?\n)
2402 (skip-chars-forward "\n" lim))
2403 ((looking-at "\\s ")
2404 (if (re-search-forward "\\S " lim 'move)
2405 (forward-char -1)))
2407 (setq stop t))))
2408 stop))
2410 (defun erlang-at-keyword ()
2411 "Are we looking at an Erlang keyword which will increase indentation?"
2412 (looking-at (concat "\\(when\\|if\\|fun\\|case\\|begin\\|query\\|"
2413 "of\\|receive\\|after\\|catch\\)[^_a-zA-Z0-9]")))
2415 (defun erlang-at-operator ()
2416 "Are we looking at an Erlang operator?"
2417 (looking-at
2418 "\\(bnot\\|div\\|mod\\|band\\|bor\\|bxor\\|bsl\\|bsr\\)[^_a-zA-Z0-9]"))
2420 (defun erlang-comment-indent ()
2421 "Compute Erlang comment indentation.
2423 Used both by `indent-for-comment' and the Erlang specific indentation
2424 commands."
2425 (if (not erlang-oldstyle-comment-indent)
2426 (cond ((looking-at "%%") 0)
2427 ((looking-at "%")
2428 (or (erlang-calculate-indent)
2429 (current-indentation))))
2430 (cond ((looking-at "%%%") 0)
2431 ((looking-at "%%")
2432 (or (erlang-calculate-indent)
2433 (current-indentation)))
2435 (save-excursion
2436 (skip-chars-backward " \t")
2437 (max (if (bolp) 0 (1+ (current-column)))
2438 comment-column))))))
2441 ;;; Erlang movement commands
2443 ;; All commands below work as movement commands. I.e. if the point is
2444 ;; at the end of the clause, and the command `erlang-end-of-clause' is
2445 ;; executed, the point is moved to the end of the NEXT clause. (This
2446 ;; mimics the behaviour of `end-of-defun'.)
2448 ;; Personally I would like to rewrite them to be "pure", and add a set
2449 ;; of movement functions, like `erlang-next-clause',
2450 ;; `erlang-previous-clause', and the same for functions.
2452 ;; The current implementation makes it hopeless to use the functions as
2453 ;; subroutines in more complex commands. /andersl
2455 (defun erlang-beginning-of-clause (&optional arg)
2456 "Move backward to previous start of clause.
2457 With argument, do this that many times.
2458 Return t unless search stops due to end of buffer."
2459 (interactive "p")
2460 (or arg (setq arg 1))
2461 (if (< arg 0)
2462 ;; Step back to the end of the previous line, unless we are at
2463 ;; the beginning of the buffer. The reason for this move is
2464 ;; that the regexp below includes the last character of the
2465 ;; previous line.
2466 (if (bobp)
2467 (or (looking-at "\n")
2468 (forward-char 1))
2469 (forward-char -1)
2470 (if (looking-at "\\`\n")
2471 (forward-char 1))))
2472 ;; The regexp matches a function header that isn't
2473 ;; included in a string.
2474 (and (re-search-forward "\\(\\`\\|\\`\n\\|[^\\]\n\\)\\([a-z]\\|'\\|-\\)"
2475 nil 'move (- arg))
2476 (let ((beg (match-beginning 2)))
2477 (and beg (goto-char beg))
2478 t)))
2480 (defun erlang-end-of-clause (&optional arg)
2481 "Move to the end of the current clause.
2482 With argument, do this that many times."
2483 (interactive "p")
2484 (or arg (setq arg 1))
2485 (while (and (looking-at "[ \t]*[%\n]")
2486 (zerop (forward-line 1))))
2487 ;; Move to the next clause.
2488 (erlang-beginning-of-clause (- arg))
2489 (beginning-of-line);; Just to be sure...
2490 (let ((continue t))
2491 (while (and (not (bobp)) continue)
2492 (forward-line -1)
2493 (skip-chars-forward " \t")
2494 (if (looking-at "[%\n]")
2496 (end-of-line)
2497 (setq continue nil)))))
2499 (defun erlang-mark-clause ()
2500 "Put mark at end of clause, point at beginning."
2501 (interactive)
2502 (push-mark (point))
2503 (erlang-end-of-clause 1)
2504 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2505 ;; the region.
2506 (condition-case nil
2507 (push-mark (point) nil t)
2508 (error (push-mark (point))))
2509 (erlang-beginning-of-clause 1)
2510 ;; The above function deactivates the mark.
2511 (if (boundp 'deactivate-mark)
2512 (funcall (symbol-function 'set) 'deactivate-mark nil)))
2514 (defun erlang-beginning-of-function (&optional arg)
2515 "Move backward to previous start of function.
2516 With positive argument, do this that many times.
2517 With negative argument, search forward.
2519 Return t unless search stops due to end of buffer."
2520 (interactive "p")
2521 (or arg (setq arg 1))
2522 (cond
2523 ;; Search backward
2524 ((> arg 0)
2525 (while (and (> arg 0)
2526 (and (erlang-beginning-of-clause 1)
2527 (let ((start (point))
2528 (name (erlang-name-of-function))
2529 (arity (erlang-get-function-arity)))
2530 ;; Note: "arity" is nil for e.g. "-import", hence
2531 ;; two "-import" clauses are not considered to
2532 ;; be part of the same function.
2533 (while (and (erlang-beginning-of-clause 1)
2534 (string-equal name
2535 (erlang-name-of-function))
2536 arity
2537 (equal arity
2538 (erlang-get-function-arity)))
2539 (setq start (point)))
2540 (goto-char start)
2541 t)))
2542 (setq arg (1- arg))))
2543 ;; Search forward
2544 ((< arg 0)
2545 (end-of-line)
2546 (erlang-beginning-of-clause 1)
2547 ;; Step -arg functions forward.
2548 (while (and (< arg 0)
2549 ;; Step one function forward, or stop if the end of
2550 ;; the buffer was reached. Return t if we found the
2551 ;; function.
2552 (let ((name (erlang-name-of-function))
2553 (arity (erlang-get-function-arity))
2554 (found (erlang-beginning-of-clause -1)))
2555 (while (and found
2556 (string-equal name (erlang-name-of-function))
2557 arity
2558 (equal arity
2559 (erlang-get-function-arity)))
2560 (setq found (erlang-beginning-of-clause -1)))
2561 found))
2562 (setq arg (1+ arg)))))
2563 (zerop arg))
2566 (defun erlang-end-of-function (&optional arg)
2567 "Move forward to next end of function.
2569 With argument, do this that many times.
2570 With negative argument go towards the beginning of the buffer."
2571 (interactive "p")
2572 (or arg (setq arg 1))
2573 (let ((first t))
2574 ;; Forward
2575 (while (and (> arg 0) (< (point) (point-max)))
2576 (let ((pos (point)))
2577 (while (progn
2578 (if (and first
2579 (progn
2580 (forward-char 1)
2581 (erlang-beginning-of-clause 1)))
2583 (or (bobp) (forward-char -1))
2584 (erlang-beginning-of-clause -1))
2585 (setq first nil)
2586 (erlang-pass-over-function)
2587 (skip-chars-forward " \t")
2588 (if (looking-at "[%\n]")
2589 (forward-line 1))
2590 (<= (point) pos))))
2591 (setq arg (1- arg)))
2592 ;; Backward
2593 (while (< arg 0)
2594 (let ((pos (point)))
2595 (erlang-beginning-of-clause 1)
2596 (erlang-pass-over-function)
2597 (forward-line 1)
2598 (if (>= (point) pos)
2599 (if (erlang-beginning-of-function 2)
2600 (progn
2601 (erlang-pass-over-function)
2602 (skip-chars-forward " \t")
2603 (if (looking-at "[%\n]")
2604 (forward-line 1)))
2605 (goto-char (point-min)))))
2606 (setq arg (1+ arg)))))
2608 (eval-and-compile
2609 (if (default-boundp 'beginning-of-defun-function)
2610 (defalias 'erlang-mark-function 'mark-defun)
2611 (defun erlang-mark-function ()
2612 "Put mark at end of function, point at beginning."
2613 (interactive)
2614 (push-mark (point))
2615 (erlang-end-of-function 1)
2616 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2617 ;; the region.
2618 (condition-case nil
2619 (push-mark (point) nil t)
2620 (error (push-mark (point))))
2621 (erlang-beginning-of-function 1)
2622 ;; The above function deactivates the mark.
2623 (if (boundp 'deactivate-mark)
2624 (funcall (symbol-function 'set) 'deactivate-mark nil)))))
2626 (defun erlang-pass-over-function ()
2627 (while (progn
2628 (erlang-skip-blank)
2629 (and (not (looking-at "\\.\\(\\s \\|\n\\|\\s<\\)"))
2630 (not (eobp))))
2631 (forward-sexp 1))
2632 (if (not (eobp))
2633 (forward-char 1)))
2635 (defun erlang-name-of-function ()
2636 (save-excursion
2637 ;; Skip over attribute leader.
2638 (if (looking-at "-[ \t]*")
2639 (re-search-forward "-[ \t]*" nil 'move))
2640 (let ((start (point)))
2641 (forward-sexp 1)
2642 (buffer-substring start (point)))))
2645 ;;; Miscellaneous
2647 (defun erlang-fill-paragraph (&optional justify)
2648 "Like \\[fill-paragraph], but handle Erlang comments.
2649 If any of the current line is a comment, fill the comment or the
2650 paragraph of it that point is in, preserving the comment's indentation
2651 and initial `%':s."
2652 (interactive "P")
2653 (let ((has-comment nil)
2654 ;; If has-comment, the appropriate fill-prefix for the comment.
2655 comment-fill-prefix)
2656 ;; Figure out what kind of comment we are looking at.
2657 (save-excursion
2658 (beginning-of-line)
2659 (cond
2660 ;; Find the command prefix.
2661 ((looking-at (concat "\\s *" comment-start-skip))
2662 (setq has-comment t)
2663 (setq comment-fill-prefix (buffer-substring (match-beginning 0)
2664 (match-end 0))))
2665 ;; A line with some code, followed by a comment? Remember that the
2666 ;; % which starts the comment shouldn't be part of a string or
2667 ;; character.
2668 ((progn
2669 (while (not (looking-at "%\\|$"))
2670 (skip-chars-forward "^%\n\"\\\\")
2671 (cond
2672 ((eq (char-after (point)) ?\\) (forward-char 2))
2673 ((eq (char-after (point)) ?\") (forward-sexp 1))))
2674 (looking-at comment-start-skip))
2675 (setq has-comment t)
2676 (setq comment-fill-prefix
2677 (concat (make-string (current-column) ? )
2678 (buffer-substring (match-beginning 0) (match-end 0)))))))
2679 (if (not has-comment)
2680 (fill-paragraph justify)
2681 ;; Narrow to include only the comment, and then fill the region.
2682 (save-restriction
2683 (narrow-to-region
2684 ;; Find the first line we should include in the region to fill.
2685 (save-excursion
2686 (while (and (zerop (forward-line -1))
2687 (looking-at "^\\s *%")))
2688 ;; We may have gone to far. Go forward again.
2689 (or (looking-at "^\\s *%")
2690 (forward-line 1))
2691 (point))
2692 ;; Find the beginning of the first line past the region to fill.
2693 (save-excursion
2694 (while (progn (forward-line 1)
2695 (looking-at "^\\s *%")))
2696 (point)))
2697 ;; Lines with only % on them can be paragraph boundaries.
2698 (let ((paragraph-start (concat paragraph-start "\\|^[ \t%]*$"))
2699 (paragraph-separate (concat paragraph-start "\\|^[ \t%]*$"))
2700 (fill-prefix comment-fill-prefix))
2701 (fill-paragraph justify))))))
2704 (defun erlang-uncomment-region (beg end)
2705 "Uncomment all commented lines in the region."
2706 (interactive "r")
2707 (comment-region beg end -1))
2710 (defun erlang-generate-new-clause ()
2711 "Create additional Erlang clause header.
2713 Parses the source file for the name of the current Erlang function.
2714 Create the header containing the name, A pair of parentheses,
2715 and an arrow. The space between the function name and the
2716 first parenthesis is preserved. The point is placed between
2717 the parentheses."
2718 (interactive)
2719 (let ((name (save-excursion
2720 (and (erlang-beginning-of-clause)
2721 (erlang-get-function-name t))))
2722 (arrow (save-excursion
2723 (and (erlang-beginning-of-clause)
2724 (erlang-get-function-arrow)))))
2725 (if (or (null arrow) (null name))
2726 (error "Can't find name of current Erlang function"))
2727 (if (and (bolp) (eolp))
2729 (end-of-line)
2730 (newline))
2731 (insert name)
2732 (save-excursion
2733 (insert ") " arrow))
2734 (if erlang-new-clause-with-arguments
2735 (erlang-clone-arguments))))
2738 (defun erlang-clone-arguments ()
2739 "Insert, at the point, the argument list of the previous clause.
2741 The mark is set at the beginning of the inserted text, the point
2742 at the end."
2743 (interactive)
2744 (let ((args (save-excursion
2745 (beginning-of-line)
2746 (and (erlang-beginning-of-clause)
2747 (erlang-get-function-arguments))))
2748 (p (point)))
2749 (if (null args)
2750 (error "Can't clone argument list"))
2751 (insert args)
2752 (set-mark p)))
2754 ;;; Information retrieval functions.
2756 (defun erlang-buffer-substring (beg end)
2757 "Like `buffer-substring-no-properties'.
2758 Although, this function works on all versions of Emacs."
2759 (if (fboundp 'buffer-substring-no-properties)
2760 (funcall (symbol-function 'buffer-substring-no-properties) beg end)
2761 (buffer-substring beg end)))
2764 (defun erlang-get-module ()
2765 "Return the name of the module as specified by `-module'.
2767 Return nil if file contains no `-module' attribute."
2768 (save-excursion
2769 (save-restriction
2770 (widen)
2771 (goto-char (point-min))
2772 (let ((md (match-data)))
2773 (unwind-protect
2774 (if (re-search-forward
2775 (eval-when-compile
2776 (concat "^-module\\s *(\\s *\\(\\("
2777 erlang-atom-regexp
2778 "\\)?\\)\\s *)\\s *\\."))
2779 (point-max) t)
2780 (erlang-remove-quotes
2781 (erlang-buffer-substring (match-beginning 1)
2782 (match-end 1)))
2783 nil)
2784 (store-match-data md))))))
2787 (defun erlang-get-module-from-file-name (&optional file)
2788 "Extract the module name from a file name.
2790 First, the directory part is removed. Second, the part of the file name
2791 matching `erlang-file-name-extension-regexp' is removed.
2793 Should the match fail, nil is returned.
2795 By modifying `erlang-file-name-extension-regexp' to match files other
2796 than Erlang source files, Erlang specific functions could be applied on
2797 non-Erlang files. Most notably; the support for Erlang modules in the
2798 tags system could be used by files written in other languages."
2799 (or file (setq file buffer-file-name))
2800 (if (null file)
2802 (setq file (file-name-nondirectory file))
2803 (if (string-match erlang-file-name-extension-regexp file)
2804 (substring file 0 (match-beginning 0))
2805 nil)))
2808 ;; Used by `erlang-get-export' and `erlang-get-import'.
2810 (defun erlang-get-function-arity-list ()
2811 "Parse list of `function/arity' as used by `-import' and `-export'.
2813 Point must be before the opening bracket. When the
2814 function returns the point will be placed after the closing bracket.
2816 The function does not return an error if the list is incorrectly
2817 formatted.
2819 Return list of (function . arity). The order of the returned list
2820 corresponds to the order of the parsed Erlang list."
2821 (let ((res '()))
2822 (erlang-skip-blank)
2823 (forward-char 1)
2824 (if (not (eq (preceding-char) ?\[))
2825 '() ; Not looking at an Erlang list.
2826 (while ; Note: `while' has no body.
2827 (progn
2828 (erlang-skip-blank)
2829 (and (looking-at (eval-when-compile
2830 (concat erlang-atom-regexp "/\\([0-9]+\\)\\>")))
2831 (progn
2832 (setq res (cons
2833 (cons
2834 (erlang-remove-quotes
2835 (erlang-buffer-substring
2836 (match-beginning 1) (match-end 1)))
2837 (string-to-int
2838 (erlang-buffer-substring
2839 (match-beginning
2840 (+ 1 erlang-atom-regexp-matches))
2841 (match-end
2842 (+ 1 erlang-atom-regexp-matches)))))
2843 res))
2844 (goto-char (match-end 0))
2845 (erlang-skip-blank)
2846 (forward-char 1)
2847 ;; Test if there are more exported functions.
2848 (eq (preceding-char) ?,))))))
2849 (nreverse res)))
2852 ;;; Note that `-export' and the open parenthesis must be written on
2853 ;;; the same line.
2855 (defun erlang-get-export ()
2856 "Return a list of `(function . arity)' as specified by `-export'."
2857 (save-excursion
2858 (goto-char (point-min))
2859 (let ((md (match-data))
2860 (res '()))
2861 (unwind-protect
2862 (progn
2863 (while (re-search-forward "^-export\\s *(" (point-max) t)
2864 (erlang-skip-blank)
2865 (setq res (nconc res (erlang-get-function-arity-list))))
2866 res)
2867 (store-match-data md)))))
2870 (defun erlang-get-import ()
2871 "Parse an Erlang source file for imported functions.
2873 Return an alist with module name as car part and list of conses containing
2874 function and arity as cdr part."
2875 (save-excursion
2876 (goto-char (point-min))
2877 (let ((md (match-data))
2878 (res '()))
2879 (unwind-protect
2880 (progn
2881 (while (re-search-forward "^-import\\s *(" (point-max) t)
2882 (erlang-skip-blank)
2883 (if (looking-at erlang-atom-regexp)
2884 (let ((module (erlang-remove-quotes
2885 (erlang-buffer-substring
2886 (match-beginning 0)
2887 (match-end 0)))))
2888 (goto-char (match-end 0))
2889 (erlang-skip-blank)
2890 (if (eq (following-char) ?,)
2891 (progn
2892 (forward-char 1)
2893 (erlang-skip-blank)
2894 (let ((funcs (erlang-get-function-arity-list))
2895 (pair (assoc module res)))
2896 (if pair
2897 (setcdr pair (nconc (cdr pair) funcs))
2898 (setq res (cons (cons module funcs)
2899 res)))))))))
2900 (nreverse res))
2901 (store-match-data md)))))
2904 (defun erlang-get-function-name (&optional arg)
2905 "Return name of current function, or nil.
2907 If optional argument is non-nil, everything up to and including
2908 the first `(' is returned.
2910 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2911 (save-excursion
2912 (if (not (eobp)) (forward-char 1))
2913 (and (erlang-beginning-of-clause)
2914 (erlang-get-function-name t)))"
2915 (let ((n (if arg 0 1)))
2916 (and (looking-at (eval-when-compile
2917 (concat "^" erlang-atom-regexp "\\s *(")))
2918 (erlang-buffer-substring (match-beginning n) (match-end n)))))
2920 (defun erlang-get-function-name-and-arity ()
2921 "Return name and arity of current function (e.g. \"foo/1\"), or nil."
2922 (when (looking-at (eval-when-compile
2923 (concat "^" erlang-atom-regexp "\\s *(")))
2924 (let ((name (erlang-buffer-substring (match-beginning 1) (match-end 1)))
2925 (arity 0))
2926 (save-excursion
2927 (goto-char (match-end 0))
2928 (while (ignore-errors
2929 (skip-syntax-forward "-")
2930 (forward-sexp 1)
2931 (skip-syntax-forward "-")
2933 (when (looking-at "[,)]")
2934 (incf arity))))
2935 (format "%s/%d" name arity))))
2937 (defun erlang-get-function-arrow ()
2938 "Return arrow of current function, could be \"->\", \":-\" or nil.
2940 The \":-\" arrow is used by mnesia queries.
2942 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2943 (save-excursion
2944 (if (not (eobp)) (forward-char 1))
2945 (and (erlang-beginning-of-clause)
2946 (erlang-get-function-arrow)))"
2947 (and
2948 (save-excursion
2949 (re-search-forward "[^-:]*-\\|:" (point-max) t)
2950 (erlang-buffer-substring (- (point) 1) (+ (point) 1)))))
2952 (defun erlang-get-function-arity ()
2953 "Return the number of arguments of function at point, or nil."
2954 (and (looking-at (eval-when-compile
2955 (concat "^" erlang-atom-regexp "\\s *(")))
2956 (save-excursion
2957 (goto-char (match-end 0))
2958 (condition-case nil
2959 (let ((res 0)
2960 (cont t))
2961 (while cont
2962 (cond ((eobp)
2963 (setq res nil)
2964 (setq cont nil))
2965 ((looking-at "\\s *)")
2966 (setq cont nil))
2967 ((looking-at "\\s *\\($\\|%\\)")
2968 (forward-line 1))
2969 ((looking-at "\\s *,")
2970 (setq res (+ 1 res))
2971 (goto-char (match-end 0)))
2973 (when (zerop res)
2974 (setq res (+ 1 res)))
2975 (forward-sexp 1))))
2976 res)
2977 (error nil)))))
2979 (defun erlang-get-function-arguments ()
2980 "Return arguments of current function, or nil."
2981 (if (not (looking-at (eval-when-compile
2982 (concat "^" erlang-atom-regexp "\\s *("))))
2984 (save-excursion
2985 (condition-case nil
2986 (let ((start (match-end 0)))
2987 (goto-char (- start 1))
2988 (forward-sexp)
2989 (erlang-buffer-substring start (- (point) 1)))
2990 (error nil)))))
2993 (defun erlang-get-function-under-point ()
2994 "Return the module and function under the point, or nil.
2996 Should no explicit module name be present at the point, the
2997 list of imported functions is searched.
2999 The following could be returned:
3000 (\"module\" \"function\") -- Both module and function name found.
3001 (nil \"function\") -- No module name was found.
3002 nil -- No function name found
3004 In the future the list may contain more elements."
3005 (save-excursion
3006 (let ((md (match-data))
3007 (res nil))
3008 (if (eq (char-syntax (following-char)) ? )
3009 (skip-chars-backward " \t"))
3010 (skip-chars-backward "a-zA-Z0-9_:'")
3011 (cond ((looking-at (eval-when-compile
3012 (concat erlang-atom-regexp ":" erlang-atom-regexp)))
3013 (setq res (list
3014 (erlang-remove-quotes
3015 (erlang-buffer-substring
3016 (match-beginning 1) (match-end 1)))
3017 (erlang-remove-quotes
3018 (erlang-buffer-substring
3019 (match-beginning (1+ erlang-atom-regexp-matches))
3020 (match-end (1+ erlang-atom-regexp-matches)))))))
3021 ((looking-at erlang-atom-regexp)
3022 (let ((fk (erlang-remove-quotes
3023 (erlang-buffer-substring
3024 (match-beginning 0) (match-end 0))))
3025 (mod nil)
3026 (imports (erlang-get-import)))
3027 (while (and imports (null mod))
3028 (if (assoc fk (cdr (car imports)))
3029 (setq mod (car (car imports)))
3030 (setq imports (cdr imports))))
3031 (setq res (list mod fk)))))
3032 (store-match-data md)
3033 res)))
3036 ;; TODO: Escape single quotes inside the string without
3037 ;; replace-regexp-in-string.
3038 (defun erlang-add-quotes-if-needed (str)
3039 "Return STR, possibly with quotes."
3040 (if (and (stringp str)
3041 (not (string-match (eval-when-compile
3042 (concat "\\`" erlang-atom-regexp "\\'")) str)))
3043 (progn (if (fboundp 'replace-regexp-in-string)
3044 (setq str (replace-regexp-in-string "'" "\\'" str t t )))
3045 (concat "'" str "'"))
3046 str))
3049 (defun erlang-remove-quotes (str)
3050 "Return STR without quotes, if present."
3051 (let ((md (match-data)))
3052 (prog1
3053 (if (string-match "\\`'\\(.*\\)'\\'" str)
3054 (substring str 1 -1)
3055 str)
3056 (store-match-data md))))
3059 ;;; Check module name
3061 ;; The function `write-file', bound to C-x C-w, calls
3062 ;; `set-visited-file-name' which clears the hook. :-(
3063 ;; To make sure that the hook always is present, we advise
3064 ;; `set-visited-file-name'.
3065 (defun erlang-check-module-name-init ()
3066 "Initialize the functionality to compare file and module names.
3068 Unless we have `before-save-hook', we redefine the function
3069 `set-visited-file-name' since it clears the variable
3070 `local-write-file-hooks'. The original function definition is
3071 stored in `erlang-orig-set-visited-file-name'."
3072 (if (boundp 'before-save-hook)
3073 ;; If we have that, `make-local-hook' is obsolete.
3074 (add-hook 'before-save-hook 'erlang-check-module-name nil t)
3075 (require 'advice)
3076 (unless (ad-advised-definition-p 'set-visited-file-name)
3077 (defadvice set-visited-file-name (after erlang-set-visited-file-name
3078 activate)
3079 (if (eq major-mode 'erlang-mode)
3080 (add-hook 'local-write-file-hooks 'erlang-check-module-name))))
3081 (add-hook 'local-write-file-hooks 'erlang-check-module-name)))
3084 (defun erlang-check-module-name ()
3085 "If the module name doesn't match file name, ask for permission to change.
3087 The variable `erlang-check-module-name' controls the behaviour of this
3088 function. It it is nil, this function does nothing. If it is t, the
3089 source is silently changed. If it is set to the atom `ask', the user
3090 is prompted.
3092 This function is normally placed in the hook `local-write-file-hooks'."
3093 (if erlang-check-module-name
3094 (let ((mn (erlang-get-module))
3095 (fn (erlang-get-module-from-file-name (buffer-file-name))))
3096 (if (and (stringp mn) (stringp fn))
3097 (or (string-equal mn fn)
3098 (if (or (eq erlang-check-module-name t)
3099 (y-or-n-p
3100 "Module does not match file name. Modify source? "))
3101 (save-excursion
3102 (save-restriction
3103 (widen)
3104 (goto-char (point-min))
3105 (if (re-search-forward
3106 (eval-when-compile
3107 (concat "^-module\\s *(\\s *\\(\\("
3108 erlang-atom-regexp
3109 "\\)?\\)\\s *)\\s *\\."))
3110 (point-max) t)
3111 (progn
3112 (goto-char (match-beginning 1))
3113 (delete-region (match-beginning 1)
3114 (match-end 1))
3115 (insert fn))))))))))
3116 ;; Must return nil since it is added to `local-write-file-hook'.
3117 nil)
3120 ;;; Electric functions.
3122 (defun erlang-electric-semicolon (&optional arg)
3123 "Insert a semicolon character and possibly a prototype for the next line.
3125 The variable `erlang-electric-semicolon-criteria' states a criterion,
3126 when fulfilled a newline is inserted, the next line is indented and a
3127 prototype for the next line is inserted. Normally the prototype
3128 consists of \" ->\". Should the semicolon end the clause a new clause
3129 header is generated.
3131 The variable `erlang-electric-semicolon-insert-blank-lines' controls
3132 the number of blank lines inserted between the current line and new
3133 function header.
3135 Behaves just like the normal semicolon when supplied with a
3136 numerical arg, point is inside string or comment, or when there are
3137 non-whitespace characters following the point on the current line."
3138 (interactive "P")
3139 (self-insert-command (prefix-numeric-value arg))
3140 (if (or arg
3141 (and (listp erlang-electric-commands)
3142 (not (memq 'erlang-electric-semicolon
3143 erlang-electric-commands)))
3144 (erlang-in-literal)
3145 (not (looking-at "\\s *\\(%.*\\)?$"))
3146 (null (erlang-test-criteria-list
3147 erlang-electric-semicolon-criteria)))
3148 (setq erlang-electric-newline-inhibit nil)
3149 (setq erlang-electric-newline-inhibit t)
3150 (undo-boundary)
3151 (end-of-line)
3152 (newline)
3153 (if (condition-case nil
3154 (progn (erlang-indent-line) t)
3155 (error (if (bolp) (delete-backward-char 1))))
3156 (if (not (bolp))
3157 (save-excursion
3158 (insert " ->"))
3159 (condition-case nil
3160 (progn
3161 (erlang-generate-new-clause)
3162 (if erlang-electric-semicolon-insert-blank-lines
3163 (save-excursion
3164 (beginning-of-line)
3165 (newline
3166 erlang-electric-semicolon-insert-blank-lines))))
3167 (error (if (bolp) (delete-backward-char 1))))))))
3170 (defun erlang-electric-comma (&optional arg)
3171 "Insert a comma character and possibly a new indented line.
3172 The variable `erlang-electric-comma-criteria' states a criterion,
3173 when fulfilled a newline is inserted and the next line is indented.
3175 Behaves just like the normal comma when supplied with a
3176 numerical arg, point is inside string or comment, or when there are
3177 non-whitespace characters following the point on the current line."
3178 (interactive "P")
3180 (self-insert-command (prefix-numeric-value arg))
3182 (if (or arg
3183 (and (listp erlang-electric-commands)
3184 (not (memq 'erlang-electric-comma erlang-electric-commands)))
3185 (erlang-in-literal)
3186 (not (looking-at "\\s *\\(%.*\\)?$"))
3187 (null (erlang-test-criteria-list
3188 erlang-electric-comma-criteria)))
3189 (setq erlang-electric-newline-inhibit nil)
3190 (setq erlang-electric-newline-inhibit t)
3191 (undo-boundary)
3192 (end-of-line)
3193 (newline)
3194 (condition-case nil
3195 (erlang-indent-line)
3196 (error (if (bolp) (delete-backward-char 1))))))
3198 (defun erlang-electric-lt (&optional arg)
3199 "Insert a less-than sign, and optionally mark it as an open paren."
3201 (interactive "p")
3203 (self-insert-command arg)
3205 ;; Was this the second char in bit-syntax open (`<<')?
3206 (unless (< (point) 2)
3207 (save-excursion
3208 (backward-char 2)
3209 (when (and (eq (char-after (point)) ?<)
3210 (not (eq (get-text-property (point) 'category)
3211 'bitsyntax-open-inner)))
3212 ;; Then mark the two chars...
3213 (put-text-property (point) (1+ (point))
3214 'category 'bitsyntax-open-outer)
3215 (forward-char 1)
3216 (put-text-property (point) (1+ (point))
3217 'category 'bitsyntax-open-inner)
3218 ;;...and unmark any subsequent less-than chars.
3219 (forward-char 1)
3220 (while (eq (char-after (point)) ?<)
3221 (remove-text-properties (point) (1+ (point))
3222 '(category nil))
3223 (forward-char 1))))))
3225 (defun erlang-after-bitsyntax-close ()
3226 "Return t if point is immediately after a bit-syntax close parenthesis (`>>')."
3227 (and (>= (point) 2)
3228 (save-excursion
3229 (backward-char 2)
3230 (and (eq (char-after (point)) ?>)
3231 (not (eq (get-text-property (point) 'category)
3232 'bitsyntax-close-outer))))))
3234 (defun erlang-after-arrow ()
3235 "Return true if point is immediately after a function arrow (`->')."
3236 (and (>= (point) 2)
3237 (and
3238 (save-excursion
3239 (backward-char)
3240 (eq (char-before (point)) ?-))
3241 (or (not (listp erlang-electric-commands))
3242 (memq 'erlang-electric-gt
3243 erlang-electric-commands))
3244 (not (erlang-in-literal))
3245 (looking-at "\\s *\\(%.*\\)?$")
3246 (erlang-test-criteria-list erlang-electric-arrow-criteria))))
3249 (defun erlang-electric-gt (&optional arg)
3250 "Insert a greater-than sign, and optionally mark it as a close paren."
3252 (interactive "p")
3254 (self-insert-command arg)
3256 (cond
3257 ;; Did we just write a bit-syntax close (`>>')?
3258 ((erlang-after-bitsyntax-close)
3259 (save-excursion
3260 ;; Then mark the two chars...
3261 (backward-char 2)
3262 (put-text-property (point) (1+ (point))
3263 'category 'bitsyntax-close-inner)
3264 (forward-char)
3265 (put-text-property (point) (1+ (point))
3266 'category 'bitsyntax-close-outer)
3267 ;;...and unmark any subsequent greater-than chars.
3268 (forward-char)
3269 (while (eq (char-after (point)) ?>)
3270 (remove-text-properties (point) (1+ (point))
3271 '(category nil))
3272 (forward-char))))
3274 ;; Did we just write a function arrow (`->')?
3275 ((erlang-after-arrow)
3276 (let ((erlang-electric-newline-inhibit t))
3277 (undo-boundary)
3278 (end-of-line)
3279 (newline)
3280 (condition-case nil
3281 (erlang-indent-line)
3282 (error (if (bolp) (delete-backward-char 1))))))
3284 ;; Then it's just a plain greater-than.
3286 nil)))
3289 (defun erlang-electric-arrow\ off (&optional arg)
3290 "Insert a '>'-sign and possibly a new indented line.
3292 This command is only `electric' when the `>' is part of an `->' arrow.
3293 The variable `erlang-electric-arrow-criteria' states a sequence of
3294 criteria, which decides when a newline should be inserted and the next
3295 line indented.
3297 It behaves just like the normal greater than sign when supplied with a
3298 numerical arg, point is inside string or comment, or when there are
3299 non-whitespace characters following the point on the current line.
3301 After being split/merged into `erlang-after-arrow' and
3302 `erlang-electric-gt', it is now unused and disabled."
3303 (interactive "P")
3304 (let ((prec (preceding-char)))
3305 (self-insert-command (prefix-numeric-value arg))
3306 (if (or arg
3307 (and (listp erlang-electric-commands)
3308 (not (memq 'erlang-electric-arrow
3309 erlang-electric-commands)))
3310 (not (eq prec ?-))
3311 (erlang-in-literal)
3312 (not (looking-at "\\s *\\(%.*\\)?$"))
3313 (null (erlang-test-criteria-list
3314 erlang-electric-arrow-criteria)))
3315 (setq erlang-electric-newline-inhibit nil)
3316 (setq erlang-electric-newline-inhibit t)
3317 (undo-boundary)
3318 (end-of-line)
3319 (newline)
3320 (condition-case nil
3321 (erlang-indent-line)
3322 (error (if (bolp) (delete-backward-char 1)))))))
3325 (defun erlang-electric-newline (&optional arg)
3326 "Break line at point and indent, continuing comment if within one.
3327 The variable `erlang-electric-newline-criteria' states a criterion,
3328 when fulfilled a newline is inserted and the next line is indented.
3330 Should the current line begin with a comment, and the variable
3331 `comment-multi-line' be non-nil, a new comment start is inserted.
3333 Should the previous command be another electric command we assume that
3334 the user pressed newline out of old habit, hence we will do nothing."
3335 (interactive "P")
3336 (cond ((and (not arg)
3337 erlang-electric-newline-inhibit
3338 (memq last-command erlang-electric-newline-inhibit-list))
3339 ()) ; Do nothing!
3340 ((or arg
3341 (and (listp erlang-electric-commands)
3342 (not (memq 'erlang-electric-newline
3343 erlang-electric-commands)))
3344 (null (erlang-test-criteria-list
3345 erlang-electric-newline-criteria)))
3346 (newline (prefix-numeric-value arg)))
3348 (if (and comment-multi-line
3349 (save-excursion
3350 (beginning-of-line)
3351 (looking-at (concat "\\s *" comment-start-skip))))
3352 (let ((str (buffer-substring
3353 (or (match-end 1) (match-beginning 0))
3354 (min (match-end 0) (point)))))
3355 (newline)
3356 (undo-boundary)
3357 (insert str))
3358 (newline-and-indent)))))
3361 (defun erlang-test-criteria-list (criteria)
3362 "Given a list of criterion functions, test if criteria are fulfilled.
3364 Each element in the criteria list can a function returning nil, t or
3365 the atom `stop'. t means that the criterion is fulfilled, `stop' means
3366 that it isn't fulfilled and that the search should stop,
3367 and nil means continue searching.
3369 Should the list contain the atom t the criterion is assumed to be
3370 fulfilled, unless preceded by a function returning `stop', of course.
3372 Should the argument be the atom t instead of a list, the criterion is
3373 assumed to be trivially true.
3375 Should all functions return nil, the criteria are assumed not to be
3376 fulfilled.
3378 Return t if criteria fulfilled, nil otherwise."
3379 (if (eq criteria t)
3381 (save-excursion
3382 (let ((answer nil))
3383 (while (and criteria (null answer))
3384 (if (eq (car criteria) t)
3385 (setq answer t)
3386 (setq answer (funcall (car criteria))))
3387 (setq criteria (cdr criteria)))
3388 (if (and answer (not (eq answer 'stop)))
3390 nil)))))
3393 (defun erlang-in-literal (&optional lim)
3394 "Test if point is in string, quoted atom or comment.
3396 Return one of the three atoms `atom', `string', and `comment'.
3397 Should the point be inside none of the above mentioned types of
3398 context, nil is returned."
3399 (save-excursion
3400 (let* ((lim (or lim (save-excursion
3401 (erlang-beginning-of-clause)
3402 (point))))
3403 (state (if (fboundp 'syntax-ppss) ; post Emacs 21.3
3404 (syntax-ppss)
3405 (parse-partial-sexp lim (point)))))
3406 (cond
3407 ((eq (nth 3 state) ?') 'atom)
3408 ((nth 3 state) 'string)
3409 ((nth 4 state) 'comment)
3410 (t nil)))))
3413 (defun erlang-at-end-of-function-p ()
3414 "Test if point is at end of an Erlang function.
3416 This function is designed to be a member of a criteria list."
3417 (eq (save-excursion (erlang-skip-blank) (point))
3418 (save-excursion
3419 (erlang-beginning-of-function -1) (point))))
3422 (defun erlang-at-end-of-clause-p ()
3423 "Test if point is at end of an Erlang clause.
3425 This function is designed to be a member of a criteria list."
3426 (eq (save-excursion (erlang-skip-blank) (point))
3427 (save-excursion
3428 (erlang-beginning-of-clause -1) (point))))
3431 (defun erlang-stop-when-inside-argument-list ()
3432 "Return `stop' if inside parenthesis list, nil otherwise.
3434 Knows about the list comprehension syntax. When the point is
3435 after `||', `stop' is not returned.
3437 This function is designed to be a member of a criteria list."
3438 (save-excursion
3439 (condition-case nil
3440 (let ((orig-point (point))
3441 (state nil))
3442 (up-list -1)
3443 (if (not (eq (following-char) ?\[))
3444 'stop
3445 ;; Do not return `stop' when inside a list comprehension
3446 ;; construction. (The point must be after `||').
3447 (while (< (point) orig-point)
3448 (setq state (erlang-partial-parse (point) orig-point state)))
3449 (if (and (car state) (eq (car (car (car state))) '||))
3451 'stop)))
3452 (error
3453 nil))))
3456 (defun erlang-stop-when-at-guard ()
3457 "Return `stop' when at function guards.
3459 This function is designed to be a member of a criteria list."
3460 (save-excursion
3461 (beginning-of-line)
3462 (if (and (looking-at (eval-when-compile
3463 (concat "^" erlang-atom-regexp "\\s *(")))
3464 (not (looking-at
3465 (eval-when-compile
3466 (concat "^" erlang-atom-regexp ".*\\(->\\|:-\\)")))))
3467 'stop
3468 nil)))
3471 (defun erlang-next-lines-empty-p ()
3472 "Return non-nil if next lines are empty.
3474 The variable `erlang-next-lines-empty-threshold' contains the number
3475 of lines required to be empty.
3477 A line containing only spaces and tabs is considered empty.
3479 This function is designed to be a member of a criteria list."
3480 (and erlang-next-lines-empty-threshold
3481 (save-excursion
3482 (let ((left erlang-next-lines-empty-threshold)
3483 (cont t))
3484 (while (and cont (> left 0))
3485 (forward-line 1)
3486 (setq cont (looking-at "\\s *$"))
3487 (setq left (- left 1)))
3488 cont))))
3491 (defun erlang-at-keyword-end-p ()
3492 "Test if next readable token is the keyword end.
3494 This function is designed to be a member of a criteria list."
3495 (save-excursion
3496 (erlang-skip-blank)
3497 (looking-at "end[^_a-zA-Z0-9]")))
3500 ;; Erlang tags support which is aware of erlang modules.
3502 ;; Not yet implemented under XEmacs. (Hint: The Emacs 19 etags
3503 ;; package works under XEmacs.)
3505 (eval-when-compile
3506 (if (or (featurep 'bytecomp)
3507 (featurep 'byte-compile))
3508 (progn
3509 (require 'etags))))
3512 ;; Variables:
3514 (defvar erlang-tags-function-alist
3515 '((find-tag . erlang-find-tag)
3516 (find-tag-other-window . erlang-find-tag-other-window)
3517 (find-tag-regexp . erlang-find-tag-regexp)
3518 (find-tag-other-frame . erlang-find-tag-other-frame))
3519 "Alist of old tags commands and the replacement functions.")
3521 (defvar erlang-tags-installed nil
3522 "Non-nil when the Erlang tags system is installed.")
3523 (defvar erlang-tags-file-list '()
3524 "List of files in tag list. Used when finding tag on form `module:'.")
3525 (defvar erlang-tags-completion-table nil
3526 "Like `tags-completion-table', this table contains `tag' and `module:tag'.")
3527 (defvar erlang-tags-buffer-installed-p nil
3528 "Non-nil when Erlang module recognising functions installed.")
3529 (defvar erlang-tags-buffer-list '()
3530 "Temporary list of buffers.")
3531 (defvar erlang-tags-orig-completion-table nil
3532 "Temporary storage for `tags-completion-table'.")
3533 (defvar erlang-tags-orig-tag-order nil
3534 "Temporary storage for `find-tag-tag-order'.")
3535 (defvar erlang-tags-orig-regexp-tag-order nil
3536 "Temporary storage for `find-tag-regexp-tag-order'.")
3537 (defvar erlang-tags-orig-search-function nil
3538 "Temporary storage for `find-tag-search-function'.")
3539 (defvar erlang-tags-orig-regexp-search-function nil
3540 "Temporary storage for `find-tag-regexp-search-function'.")
3541 (defvar erlang-tags-orig-format-hooks nil
3542 "Temporary storage for `tags-table-format-hooks'.") ;v19
3543 (defvar erlang-tags-orig-format-functions nil
3544 "Temporary storage for `tags-table-format-functions'.") ;v > 19
3546 (defun erlang-tags-init ()
3547 "Install an alternate version of tags, aware of Erlang modules.
3549 After calling this function, the tags functions are aware of
3550 Erlang modules. Tags can be entered on the for `module:tag' as well
3551 as on the old form `tag'.
3553 In the completion list, `module:tag' and `module:' shows up.
3555 Call this function from an appropriate init file, or add it to
3556 Erlang mode hook with the commands:
3557 (add-hook 'erlang-mode-hook 'erlang-tags-init)
3558 (add-hook 'erlang-shell-mode-hook 'erlang-tags-init)
3560 This function only works under Emacs 18 and Emacs 19. Currently, It
3561 is not implemented under XEmacs. (Hint: The Emacs 19 etags module
3562 works under XEmacs.)"
3563 (interactive)
3564 (cond ((= erlang-emacs-major-version 18)
3565 (require 'tags)
3566 (erlang-tags-define-keys (current-local-map))
3567 (setq erlang-tags-installed t))
3569 (require 'etags)
3570 ;; Test on a function available in the Emacs 19 version
3571 ;; of tags but not in the XEmacs version.
3572 (if (not (fboundp 'find-tag-noselect))
3574 (erlang-tags-define-keys (current-local-map))
3575 (setq erlang-tags-installed t)))))
3578 ;; Set all keys bound to `find-tag' et.al. in the global map and the
3579 ;; menu to `erlang-find-tag' et.al. in `map'.
3581 ;; The function `substitute-key-definition' does not work properly
3582 ;; in all version of Emacs.
3584 (defun erlang-tags-define-keys (map)
3585 "Bind tags commands to keymap MAP aware of Erlang modules."
3586 (let ((alist erlang-tags-function-alist))
3587 (while alist
3588 (let* ((old (car (car alist)))
3589 (new (cdr (car alist)))
3590 (keys (append (where-is-internal old global-map))))
3591 (while keys
3592 (define-key map (car keys) new)
3593 (setq keys (cdr keys))))
3594 (setq alist (cdr alist))))
3595 ;; Update the menu.
3596 (erlang-menu-substitute erlang-menu-base-items erlang-tags-function-alist)
3597 (erlang-menu-init))
3600 ;; There exists a variable `find-tag-default-function'. It is not used
3601 ;; since `complete-tag' uses it to get current word under point. In that
3602 ;; situation we don't want the module to be prepended.
3604 (defun erlang-find-tag-default ()
3605 "Return the default tag.
3606 Search `-import' list of imported functions.
3607 Single quotes are been stripped away."
3608 (let ((mod-func (erlang-get-function-under-point)))
3609 (cond ((null mod-func)
3610 nil)
3611 ((null (car mod-func))
3612 (nth 1 mod-func))
3614 (concat (car mod-func) ":" (nth 1 mod-func))))))
3617 ;; Return `t' since it is used inside `tags-loop-form'.
3618 ;;;###autoload
3619 (defun erlang-find-tag (modtagname &optional next-p regexp-p)
3620 "Like `find-tag'. Capable of retrieving Erlang modules.
3622 Tags can be given on the forms `tag', `module:', `module:tag'."
3623 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3624 (switch-to-buffer (erlang-find-tag-noselect modtagname next-p regexp-p))
3628 ;; Code mainly from `find-tag-other-window' in `etags.el'.
3629 ;;;###autoload
3630 (defun erlang-find-tag-other-window (tagname &optional next-p regexp-p)
3631 "Like `find-tag-other-window' but aware of Erlang modules."
3632 (interactive (erlang-tag-interactive
3633 "Find `module:tag' or `tag' other window: "))
3635 ;; This is to deal with the case where the tag is found in the
3636 ;; selected window's buffer; without this, point is moved in both
3637 ;; windows. To prevent this, we save the selected window's point
3638 ;; before doing find-tag-noselect, and restore it afterwards.
3639 (let* ((window-point (window-point (selected-window)))
3640 (tagbuf (erlang-find-tag-noselect tagname next-p regexp-p))
3641 (tagpoint (progn (set-buffer tagbuf) (point))))
3642 (set-window-point (prog1
3643 (selected-window)
3644 (switch-to-buffer-other-window tagbuf)
3645 ;; We have to set this new window's point; it
3646 ;; might already have been displaying a
3647 ;; different portion of tagbuf, in which case
3648 ;; switch-to-buffer-other-window doesn't set
3649 ;; the window's point from the buffer.
3650 (set-window-point (selected-window) tagpoint))
3651 window-point)))
3654 (defun erlang-find-tag-other-frame (tagname &optional next-p)
3655 "Like `find-tag-other-frame' but aware of Erlang modules."
3656 (interactive (erlang-tag-interactive
3657 "Find `module:tag' or `tag' other frame: "))
3658 (let ((pop-up-frames t))
3659 (erlang-find-tag-other-window tagname next-p)))
3662 (defun erlang-find-tag-regexp (regexp &optional next-p other-window)
3663 "Like `find-tag-regexp' but aware of Erlang modules."
3664 (interactive (if (fboundp 'find-tag-regexp)
3665 (erlang-tag-interactive
3666 "Find `module:regexp' or `regexp': ")
3667 (error "This version of Emacs can't find tags by regexps")))
3668 (funcall (if other-window
3669 'erlang-find-tag-other-window
3670 'erlang-find-tag)
3671 regexp next-p t))
3674 ;; Just like C-u M-. This could be added to the menu.
3675 (defun erlang-find-next-tag ()
3676 "Find next tag, like \\[find-tag] with prefix arg."
3677 (interactive)
3678 (let ((current-prefix-arg '(4)))
3679 (if erlang-tags-installed
3680 (call-interactively 'erlang-find-tag)
3681 (call-interactively 'find-tag))))
3684 ;; Mimics `find-tag-noselect' found in `etags.el', but uses `find-tag' to
3685 ;; be compatible with `tags.el'.
3687 ;; Handles three cases:
3688 ;; * `module:' Loop over all possible file names. Stop if a file-name
3689 ;; without extension and directory matches the module.
3691 ;; * `module:tag'
3692 ;; Emacs 19: Replace test functions with functions aware of
3693 ;; Erlang modules. Tricky because the etags system wasn't
3694 ;; built for these kind of operations...
3696 ;; Emacs 18: We loop over `find-tag' until we find a file
3697 ;; whose module matches the requested module. The
3698 ;; drawback is that a lot of files could be loaded into
3699 ;; Emacs.
3701 ;; * `tag' Just give it to `find-tag'.
3703 (defun erlang-find-tag-noselect (modtagname &optional next-p regexp-p)
3704 "Like `find-tag-noselect' but aware of Erlang modules."
3705 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3706 (or modtagname
3707 (setq modtagname (symbol-value 'last-tag)))
3708 (funcall (symbol-function 'set) 'last-tag modtagname)
3709 ;; `tags.el' uses this variable to record how M-, would
3710 ;; know where to restart a tags command.
3711 (if (boundp 'tags-loop-form)
3712 (funcall (symbol-function 'set)
3713 'tags-loop-form '(erlang-find-tag nil t)))
3714 (save-window-excursion
3715 (cond
3716 ((string-match ":$" modtagname)
3717 ;; Only the module name was given. Read all files whose file name
3718 ;; match.
3719 (let ((modname (substring modtagname 0 (match-beginning 0)))
3720 (file nil))
3721 (if (not next-p)
3722 (save-excursion
3723 (visit-tags-table-buffer)
3724 (setq erlang-tags-file-list
3725 (funcall (symbol-function 'tags-table-files)))))
3726 (while (null file)
3727 (or erlang-tags-file-list
3728 (save-excursion
3729 (if (and (featurep 'etags)
3730 (funcall
3731 (symbol-function 'visit-tags-table-buffer) 'same)
3732 (funcall
3733 (symbol-function 'visit-tags-table-buffer) t))
3734 (setq erlang-tags-file-list
3735 (funcall (symbol-function 'tags-table-files)))
3736 (error "No %stags containing %s" (if next-p "more " "")
3737 modtagname))))
3738 (if erlang-tags-file-list
3739 (let ((this-module (erlang-get-module-from-file-name
3740 (car erlang-tags-file-list))))
3741 (if (and (stringp this-module)
3742 (string= modname this-module))
3743 (setq file (car erlang-tags-file-list)))
3744 (setq erlang-tags-file-list (cdr erlang-tags-file-list)))))
3745 (set-buffer (or (get-file-buffer file)
3746 (find-file-noselect file)))))
3748 ((string-match ":" modtagname)
3749 (if (boundp 'find-tag-tag-order)
3750 ;; Method one: Add module-recognising functions to the
3751 ;; list of order functions. However, the tags system
3752 ;; from Emacs 18, and derives thereof (read: XEmacs)
3753 ;; hasn't got this feature.
3754 (progn
3755 (erlang-tags-install-module-check)
3756 (unwind-protect
3757 (funcall (symbol-function 'find-tag)
3758 modtagname next-p regexp-p)
3759 (erlang-tags-remove-module-check)))
3760 ;; Method two: Call the tags system until a file matching
3761 ;; the module is found. This could result in that many
3762 ;; files are read. (e.g. The tag "foo:file" will take a
3763 ;; while to process.)
3764 (let* ((modname (substring modtagname 0 (match-beginning 0)))
3765 (tagname (substring modtagname (match-end 0) nil))
3766 (last-tag tagname)
3767 file)
3768 (while
3769 (progn
3770 (funcall (symbol-function 'find-tag) tagname next-p regexp-p)
3771 (setq next-p t)
3772 ;; Determine the module form the file name. (The
3773 ;; alternative, to check `-module', would make this
3774 ;; code useless for non-Erlang programs.)
3775 (setq file (erlang-get-module-from-file-name buffer-file-name))
3776 (not (and (stringp file)
3777 (string= modname file))))))))
3779 (funcall (symbol-function 'find-tag) modtagname next-p regexp-p)))
3780 (current-buffer))) ; Return the new buffer.
3783 ;; Process interactive arguments for erlang-find-tag-*.
3785 ;; Negative arguments work only for `etags', not `tags'. This is not
3786 ;; a problem since negative arguments means step back into the
3787 ;; history list, a feature not implemented in `tags'.
3789 (defun erlang-tag-interactive (prompt)
3790 (condition-case nil
3791 (require 'etags)
3792 (error
3793 (require 'tags)))
3794 (if current-prefix-arg
3795 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
3798 (let* ((default (erlang-find-tag-default))
3799 (prompt (if default
3800 (format "%s(default %s) " prompt default)
3801 prompt))
3802 (spec (if (featurep 'etags)
3803 (completing-read prompt 'erlang-tags-complete-tag)
3804 (read-string prompt))))
3805 (list (if (equal spec "")
3806 (or default (error "There is no default tag"))
3807 spec)))))
3810 ;; Search tag functions which are aware of Erlang modules. The tactic
3811 ;; is to store new search functions into the local variables of the
3812 ;; TAGS buffers. The variables are restored directly after the
3813 ;; search. The situation is complicated by the fact that new TAGS
3814 ;; files can be loaded during the search.
3817 (defun erlang-tags-install-module-check ()
3818 "Install our own tag search functions."
3819 ;; Make sure our functions are installed in TAGS files loaded
3820 ;; into Emacs while searching.
3821 (cond
3822 ((>= erlang-emacs-major-version 20)
3823 (setq erlang-tags-orig-format-functions
3824 (symbol-value 'tags-table-format-functions))
3825 (funcall (symbol-function 'set) 'tags-table-format-functions
3826 (cons 'erlang-tags-recognize-tags-table
3827 erlang-tags-orig-format-functions))
3828 (setq erlang-tags-buffer-list '())
3831 (setq erlang-tags-orig-format-hooks
3832 (symbol-value 'tags-table-format-hooks)))
3833 (funcall (symbol-function 'set) 'tags-table-format-hooks
3834 (cons 'erlang-tags-recognize-tags-table
3835 erlang-tags-orig-format-hooks))
3836 (setq erlang-tags-buffer-list '())
3839 ;; Install our functions in the TAGS files already resident.
3840 (save-excursion
3841 (let ((files (symbol-value 'tags-table-computed-list)))
3842 (while files
3843 (if (stringp (car files))
3844 (if (get-file-buffer (car files))
3845 (progn
3846 (set-buffer (get-file-buffer (car files)))
3847 (erlang-tags-install-local))))
3848 (setq files (cdr files))))))
3851 (defun erlang-tags-install-local ()
3852 "Install our tag search functions in current buffer."
3853 (if erlang-tags-buffer-installed-p
3855 ;; Mark this buffer as "installed" and record.
3856 (set (make-local-variable 'erlang-tags-buffer-installed-p) t)
3857 (setq erlang-tags-buffer-list
3858 (cons (current-buffer) erlang-tags-buffer-list))
3860 ;; Save the original values.
3861 (set (make-local-variable 'erlang-tags-orig-tag-order)
3862 (symbol-value 'find-tag-tag-order))
3863 (set (make-local-variable 'erlang-tags-orig-regexp-tag-order)
3864 (symbol-value 'find-tag-regexp-tag-order))
3865 (set (make-local-variable 'erlang-tags-orig-search-function)
3866 (symbol-value 'find-tag-search-function))
3867 (set (make-local-variable 'erlang-tags-orig-regexp-search-function)
3868 (symbol-value 'find-tag-regexp-search-function))
3870 ;; Install our own functions.
3871 (set (make-local-variable 'find-tag-search-function)
3872 'erlang-tags-search-forward)
3873 (set (make-local-variable 'find-tag-regexp-search-function)
3874 'erlang-tags-regexp-search-forward)
3875 (set (make-local-variable 'find-tag-tag-order)
3876 '(erlang-tag-match-module-p))
3877 (set (make-local-variable 'find-tag-regexp-tag-order)
3878 '(erlang-tag-match-module-regexp-p))))
3881 (defun erlang-tags-remove-module-check ()
3882 "Remove our own tags search functions."
3883 (cond
3884 ((>= erlang-emacs-major-version 20)
3885 (funcall (symbol-function 'set)
3886 'tags-table-format-functions
3887 erlang-tags-orig-format-functions)
3890 (funcall (symbol-function 'set)
3891 'tags-table-format-hooks
3892 erlang-tags-orig-format-hooks)
3895 ;; Remove our functions from the TAGS files. (Note that
3896 ;; `tags-table-computed-list' need not be the same list as when
3897 ;; the search was started.)
3898 (save-excursion
3899 (let ((buffers erlang-tags-buffer-list))
3900 (while buffers
3901 (if (buffer-name (car buffers))
3902 (progn
3903 (set-buffer (car buffers))
3904 (erlang-tags-remove-local)))
3905 (setq buffers (cdr buffers))))))
3908 (defun erlang-tags-remove-local ()
3909 "Remove our tag search functions from current buffer."
3910 (if (null erlang-tags-buffer-installed-p)
3912 (funcall (symbol-function 'set) 'erlang-tags-buffer-installed-p nil)
3913 (funcall (symbol-function 'set)
3914 'find-tag-tag-order erlang-tags-orig-tag-order)
3915 (funcall (symbol-function 'set)
3916 'find-tag-regexp-tag-order erlang-tags-orig-regexp-tag-order)
3917 (funcall (symbol-function 'set)
3918 'find-tag-search-function erlang-tags-orig-search-function)
3919 (funcall (symbol-function 'set)
3920 'find-tag-regexp-search-function
3921 erlang-tags-orig-regexp-search-function)))
3924 (defun erlang-tags-recognize-tags-table ()
3925 "Install our functions in all loaded TAGS files.
3927 This function is added to `tags-table-format-hooks/functions' when searching
3928 for a tag on the form `module:tag'."
3929 (if (null (funcall (symbol-function 'etags-recognize-tags-table)))
3931 (erlang-tags-install-local)
3935 (defun erlang-tags-search-forward (tag &optional bound noerror count)
3936 "Forward search function, aware of Erlang module prefix."
3937 (if (string-match ":" tag)
3938 (setq tag (substring tag (match-end 0) nil)))
3939 ;; Avoid unintended recursion.
3940 (if (eq erlang-tags-orig-search-function 'erlang-tags-search-forward)
3941 (search-forward tag bound noerror count)
3942 (funcall erlang-tags-orig-search-function tag bound noerror count)))
3945 (defun erlang-tags-regexp-search-forward (tag &optional bound noerror count)
3946 "Forward regexp search function, aware of Erlang module prefix."
3947 (if (string-match ":" tag)
3948 (setq tag (substring tag (match-end 0) nil)))
3949 (if (eq erlang-tags-orig-regexp-search-function
3950 'erlang-tags-regexp-search-forward)
3951 (re-search-forward tag bound noerror count)
3952 (funcall erlang-tags-orig-regexp-search-function
3953 tag bound noerror count)))
3956 ;; t if point is at a tag line that matches TAG, containing
3957 ;; module information. Assumes that all other order functions
3958 ;; are stored in `erlang-tags-orig-[regex]-tag-order'.
3960 (defun erlang-tag-match-module-p (tag)
3961 (erlang-tag-match-module-common-p tag erlang-tags-orig-tag-order))
3963 (defun erlang-tag-match-module-regexp-p (tag)
3964 (erlang-tag-match-module-common-p tag erlang-tags-orig-regexp-tag-order))
3966 (defun erlang-tag-match-module-common-p (tag order)
3967 (let ((mod nil)
3968 (found nil))
3969 (if (string-match ":" tag)
3970 (progn
3971 (setq mod (substring tag 0 (match-beginning 0)))
3972 (setq tag (substring tag (match-end 0) nil))))
3973 (while (and order (not found))
3974 (setq found
3975 (and (not (memq (car order)
3976 '(erlang-tag-match-module-p
3977 erlang-tag-match-module-regexp-p)))
3978 (funcall (car order) tag)))
3979 (setq order (cdr order)))
3980 (and found
3981 (or (null mod)
3982 (string= mod (erlang-get-module-from-file-name
3983 (file-of-tag)))))))
3986 ;;; Tags completion, Emacs 19 `etags' specific.
3988 ;;; The basic idea is to create a second completion table `erlang-tags-
3989 ;;; completion-table' containing all normal tags plus tags on the form
3990 ;;; `module:tag'.
3993 (defun erlang-complete-tag ()
3994 "Perform tags completion on the text around point.
3995 Completes to the set of names listed in the current tags table.
3997 Should the Erlang tags system be installed this command knows
3998 about Erlang modules."
3999 (interactive)
4000 (condition-case nil
4001 (require 'etags)
4002 (error nil))
4003 (cond ((and erlang-tags-installed
4004 (fboundp 'complete-tag)) ; Emacs 19
4005 (let ((orig-tags-complete-tag (symbol-function 'tags-complete-tag)))
4006 (fset 'tags-complete-tag
4007 (symbol-function 'erlang-tags-complete-tag))
4008 (unwind-protect
4009 (funcall (symbol-function 'complete-tag))
4010 (fset 'tags-complete-tag orig-tags-complete-tag))))
4011 ((fboundp 'complete-tag) ; Emacs 19
4012 (funcall (symbol-function 'complete-tag)))
4013 ((fboundp 'tag-complete-symbol) ; XEmacs
4014 (funcall (symbol-function 'tag-complete-symbol)))
4016 (error "This version of Emacs can't complete tags"))))
4019 ;; Based on `tags-complete-tag', but this one uses
4020 ;; `erlang-tags-completion-table' instead of `tags-completion-table'.
4022 ;; This is the entry-point called by system function `completing-read'.
4023 (defun erlang-tags-complete-tag (string predicate what)
4024 (save-excursion
4025 ;; If we need to ask for the tag table, allow that.
4026 (let ((enable-recursive-minibuffers t))
4027 (visit-tags-table-buffer))
4028 (if (eq what t)
4029 (all-completions string (erlang-tags-completion-table) predicate)
4030 (try-completion string (erlang-tags-completion-table) predicate))))
4033 ;; `tags-completion-table' calls itself recursively, make it
4034 ;; call our own wedge instead. Note that the recursive call
4035 ;; is very rare; it only occurs when a tags-file contains
4036 ;; `include'-statements.
4037 (defun erlang-tags-completion-table ()
4038 "Build completion table. Tags on the form `tag' or `module:tag'."
4039 (setq erlang-tags-orig-completion-table
4040 (symbol-function 'tags-completion-table))
4041 (fset 'tags-completion-table
4042 (symbol-function 'erlang-tags-completion-table-1))
4043 (unwind-protect
4044 (erlang-tags-completion-table-1)
4045 (fset 'tags-completion-table
4046 erlang-tags-orig-completion-table)))
4049 (defun erlang-tags-completion-table-1 ()
4050 (make-local-variable 'erlang-tags-completion-table)
4051 (or erlang-tags-completion-table
4052 (let ((tags-completion-table nil)
4053 (tags-completion-table-function
4054 'erlang-etags-tags-completion-table))
4055 (funcall erlang-tags-orig-completion-table)
4056 (setq erlang-tags-completion-table tags-completion-table))))
4059 ;; Based on `etags-tags-completion-table'. The difference is that we
4060 ;; add three symbols to the vector, the tag, module: and module:tag.
4061 ;; The module is extracted from the file name of a tag. (This one
4062 ;; only works if we are looking at an `etags' file. However, this is
4063 ;; the only format supported by Emacs, so far.)
4064 (defun erlang-etags-tags-completion-table ()
4065 (let ((table (make-vector 511 0))
4066 (file nil))
4067 (save-excursion
4068 (goto-char (point-min))
4069 ;; This monster regexp matches an etags tag line.
4070 ;; \1 is the string to match;
4071 ;; \2 is not interesting;
4072 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
4073 ;; \4 is not interesting;
4074 ;; \5 is the explicitly-specified tag name.
4075 ;; \6 is the line to start searching at;
4076 ;; \7 is the char to start searching at.
4077 (while (progn
4078 (while (and
4079 (eq (following-char) ?\f)
4080 (looking-at "\f\n\\([^,\n]*\\),.*\n"))
4081 (setq file (buffer-substring
4082 (match-beginning 1) (match-end 1)))
4083 (goto-char (match-end 0)))
4084 (re-search-forward
4086 ^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
4087 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
4088 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
4089 nil t))
4090 (let ((tag (if (match-beginning 5)
4091 ;; There is an explicit tag name.
4092 (buffer-substring (match-beginning 5) (match-end 5))
4093 ;; No explicit tag name. Best guess.
4094 (buffer-substring (match-beginning 3) (match-end 3))))
4095 (module (and file
4096 (erlang-get-module-from-file-name file))))
4097 (intern tag table)
4098 (if (stringp module)
4099 (progn
4100 (intern (concat module ":" tag) table)
4101 ;; Only the first one will be stored in the table.
4102 (intern (concat module ":") table))))))
4103 table))
4106 ;;; Prepare for other methods to run an Erlang slave process.
4109 (defvar erlang-shell-function 'inferior-erlang
4110 "Command to execute start a new Erlang shell.
4112 Change this variable to use your favorite
4113 Erlang compilation package.")
4115 (defvar erlang-shell-display-function 'inferior-erlang-run-or-select
4116 "Command to execute to display Erlang shell.
4118 Change this variable to use your favorite
4119 Erlang compilation package.")
4121 (defvar erlang-compile-function 'inferior-erlang-compile
4122 "Command to execute to compile current buffer.
4124 Change this variable to use your favorite
4125 Erlang compilation package.")
4127 (defvar erlang-compile-display-function 'inferior-erlang-run-or-select
4128 "Command to execute to view last compilation.
4130 Change this variable to use your favorite
4131 Erlang compilation package.")
4133 (defvar erlang-next-error-function 'inferior-erlang-next-error
4134 "Command to execute to go to the next error.
4136 Change this variable to use your favorite Erlang compilation
4137 package. Not used in Emacs 21.")
4140 ;;;###autoload
4141 (defun erlang-shell ()
4142 "Start a new Erlang shell.
4144 The variable `erlang-shell-function' decides which method to use,
4145 default is to start a new Erlang host. It is possible that, in the
4146 future, a new shell on an already running host will be started."
4147 (interactive)
4148 (call-interactively erlang-shell-function))
4151 ;;;###autoload (autoload 'run-erlang "erlang" "Start a new Erlang shell." t)
4153 ;; It is customary for Emacs packages to supply a function on this
4154 ;; form, even though it violates the `erlang-*' name convention.
4155 (defalias 'run-erlang 'erlang-shell)
4158 (defun erlang-shell-display ()
4159 "Display an Erlang shell, or start a new."
4160 (interactive)
4161 (call-interactively erlang-shell-display-function))
4164 ;;;###autoload
4165 (defun erlang-compile ()
4166 "Compile Erlang module in current buffer."
4167 (interactive)
4168 (call-interactively erlang-compile-function))
4171 (defun erlang-compile-display ()
4172 "Display compilation output."
4173 (interactive)
4174 (call-interactively erlang-compile-display-function))
4177 (defun erlang-next-error ()
4178 "Display next error message from the latest compilation."
4179 (interactive)
4180 (call-interactively erlang-next-error-function))
4185 ;;; Erlang Shell Mode -- Major mode used for Erlang shells.
4188 ;; This mode is designed to be implementation independent,
4189 ;; e.g. it does not assume that we are running an inferior
4190 ;; Erlang, there exists a lot of other possibilities.
4193 (defvar erlang-shell-buffer-name "*erlang*"
4194 "The name of the Erlang link shell buffer.")
4197 (defvar erlang-shell-mode-map nil
4198 "Keymap used by Erlang shells.")
4201 (defvar erlang-shell-mode-hook nil
4202 "*User functions to run when an Erlang shell is started.
4204 This hook is used to change the behaviour of Erlang mode. It is
4205 normally used by the user to personalise the programming environment.
4206 When used in a site init file, it could be used to customise Erlang
4207 mode for all users on the system.
4209 The function added to this hook is run every time a new Erlang
4210 shell is started.
4212 See also `erlang-load-hook', a hook which is run once, when Erlang
4213 mode is loaded, and `erlang-mode-hook' which is run every time a new
4214 Erlang source file is loaded into Emacs.")
4217 (defvar erlang-input-ring-file-name "~/.erlang_history"
4218 "*When non-nil, file name used to store Erlang shell history information.")
4221 (defun erlang-shell-mode ()
4222 "Major mode for interacting with an Erlang shell.
4224 We assume that we already are in Comint mode.
4226 The following special commands are available:
4227 \\{erlang-shell-mode-map}"
4228 (interactive)
4229 (setq major-mode 'erlang-shell-mode)
4230 (setq mode-name "Erlang Shell")
4231 (erlang-mode-variables)
4232 (if erlang-shell-mode-map
4234 (setq erlang-shell-mode-map (copy-keymap comint-mode-map))
4235 (erlang-shell-mode-commands erlang-shell-mode-map))
4236 (use-local-map erlang-shell-mode-map)
4237 (unless inferior-erlang-use-cmm
4238 ;; This was originally not a marker, but it needs to be, at least
4239 ;; in Emacs 21, and should be backwards-compatible. Otherwise,
4240 ;; would need to test whether compilation-parsing-end is a marker
4241 ;; after requiring `compile'.
4242 (set (make-local-variable 'compilation-parsing-end) (copy-marker 1))
4243 (set (make-local-variable 'compilation-error-list) nil)
4244 (set (make-local-variable 'compilation-old-error-list) nil))
4245 ;; Needed when compiling directly from the Erlang shell.
4246 (setq compilation-last-buffer (current-buffer))
4247 (erlang-add-compilation-alist erlang-error-regexp-alist)
4248 (setq comint-prompt-regexp "^[^>=]*> *")
4249 (setq comint-eol-on-send t)
4250 (setq comint-input-ignoredups t)
4251 (setq comint-scroll-show-maximum-output t)
4252 (setq comint-scroll-to-bottom-on-output t)
4253 ;; In Emacs 19.30, `add-hook' has got a `local' flag, use it. If
4254 ;; the call fails, just call the normal `add-hook'.
4255 (condition-case nil
4256 (progn
4257 (make-local-hook 'comint-output-filter-functions) ; obsolete after Emacs 21.3
4258 (add-hook 'comint-output-filter-functions
4259 'inferior-erlang-strip-delete nil t)
4260 (add-hook 'comint-output-filter-functions
4261 'inferior-erlang-strip-ctrl-m nil t))
4262 (error
4263 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-delete)
4264 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-ctrl-m)))
4265 ;; Some older versions of comint don't have an input ring.
4266 (if (fboundp 'comint-read-input-ring)
4267 (progn
4268 (setq comint-input-ring-file-name erlang-input-ring-file-name)
4269 (comint-read-input-ring t)
4270 (make-local-variable 'kill-buffer-hook)
4271 (add-hook 'kill-buffer-hook 'comint-write-input-ring)))
4272 ;; At least in Emacs 21, we need to be in `compilation-minor-mode'
4273 ;; for `next-error' to work. We can avoid it clobbering the shell
4274 ;; keys thus.
4275 (when inferior-erlang-use-cmm
4276 (compilation-minor-mode 1)
4277 (set (make-local-variable 'minor-mode-overriding-map-alist)
4278 `((compilation-minor-mode
4279 . ,(let ((map (make-sparse-keymap)))
4280 ;; It would be useful to put keymap properties on the
4281 ;; error lines so that we could use RET and mouse-2
4282 ;; on them directly.
4283 (when (boundp 'compilation-skip-threshold) ; new compile.el
4284 (define-key map [mouse-2] #'erlang-mouse-2-command)
4285 (define-key map "\C-m" #'erlang-RET-command))
4286 (if (boundp 'compilation-menu-map)
4287 (define-key map [menu-bar compilation]
4288 (cons "Errors" compilation-menu-map)))
4289 map)))))
4290 (run-hooks 'erlang-shell-mode-hook))
4293 (defun erlang-mouse-2-command (event)
4294 "Command bound to `mouse-2' in inferior Erlang buffer.
4295 Selects Comint or Compilation mode command as appropriate."
4296 (interactive "e")
4297 (if (save-window-excursion
4298 (save-excursion
4299 (mouse-set-point event)
4300 (consp (get-text-property (line-beginning-position) 'message))))
4301 (call-interactively (lookup-key compilation-mode-map [mouse-2]))
4302 (call-interactively (lookup-key comint-mode-map [mouse-2]))))
4304 (defun erlang-RET-command ()
4305 "Command bound to `RET' in inferior Erlang buffer.
4306 Selects Comint or Compilation mode command as appropriate."
4307 (interactive)
4308 (if (consp (get-text-property (line-beginning-position) 'message))
4309 (call-interactively (lookup-key compilation-mode-map "\C-m"))
4310 (call-interactively (lookup-key comint-mode-map "\C-m"))))
4312 (defun erlang-shell-mode-commands (map)
4313 (define-key map "\M-\t" 'erlang-complete-tag)
4314 (define-key map "\C-a" 'comint-bol) ; Normally the other way around.
4315 (define-key map "\C-c\C-a" 'beginning-of-line)
4316 (define-key map "\C-d" nil) ; Was `comint-delchar-or-maybe-eof'
4317 (define-key map "\M-\C-m" 'compile-goto-error)
4318 (unless inferior-erlang-use-cmm
4319 (define-key map "\C-x`" 'erlang-next-error)))
4322 ;;; Inferior Erlang -- Run an Erlang shell as a subprocess.
4325 (defvar inferior-erlang-display-buffer-any-frame nil
4326 "*When nil, `inferior-erlang-display-buffer' use only selected frame.
4327 When t, all frames are searched. When 'raise, the frame is raised.")
4329 (defvar inferior-erlang-shell-type 'newshell
4330 "The type of Erlang shell to use.
4332 When this variable is set to the atom `oldshell', the old shell is used.
4333 When set to `newshell' the new shell is used. Should the variable be
4334 nil, the default shell is used.
4336 This variable influence the setting of other variables.")
4338 (defvar inferior-erlang-machine "erl"
4339 "*The name of the Erlang shell.")
4341 (defvar inferior-erlang-machine-options '()
4342 "*The options used when activating the Erlang shell.
4344 This must be a list of strings.")
4346 (defvar inferior-erlang-process-name "inferior-erlang"
4347 "The name of the inferior Erlang process.")
4349 (defvar inferior-erlang-buffer-name erlang-shell-buffer-name
4350 "The name of the inferior Erlang buffer.")
4352 (defvar inferior-erlang-prompt-timeout 60
4353 "*Number of seconds before `inferior-erlang-wait-prompt' timeouts.
4355 The time specified is waited after every output made by the inferior
4356 Erlang shell. When this variable is t, we assume that we always have
4357 a prompt. When nil, we will wait forever, or until \\[keyboard-quit].")
4359 (defvar inferior-erlang-process nil
4360 "Process of last invoked inferior Erlang, or nil.")
4362 (defvar inferior-erlang-buffer nil
4363 "Buffer of last invoked inferior Erlang, or nil.")
4365 ;;;###autoload
4366 (defun inferior-erlang (&optional command)
4367 "Run an inferior Erlang.
4368 With prefix command, prompt for command to start Erlang with.
4370 This is just like running Erlang in a normal shell, except that
4371 an Emacs buffer is used for input and output.
4372 \\<comint-mode-map>
4373 The command line history can be accessed with \\[comint-previous-input] and \\[comint-next-input].
4374 The history is saved between sessions.
4376 Entry to this mode calls the functions in the variables
4377 `comint-mode-hook' and `erlang-shell-mode-hook' with no arguments.
4379 The following commands imitate the usual Unix interrupt and
4380 editing control characters:
4381 \\{erlang-shell-mode-map}"
4382 (interactive
4383 (when current-prefix-arg
4384 (list (read-string "Erlang command: "))))
4385 (require 'comint)
4386 (let (cmd opts)
4387 (if command
4388 (setq cmd "sh"
4389 opts (list "-c" command))
4390 (setq cmd inferior-erlang-machine
4391 opts inferior-erlang-machine-options)
4392 (cond ((eq inferior-erlang-shell-type 'oldshell)
4393 (setq opts (cons "-oldshell" opts)))
4394 ((eq inferior-erlang-shell-type 'newshell)
4395 (setq opts (append '("-newshell" "-env" "TERM" "vt100") opts)))))
4397 (setq inferior-erlang-buffer
4398 (apply 'make-comint
4399 inferior-erlang-process-name cmd
4400 nil opts)))
4401 (setq inferior-erlang-process
4402 (get-buffer-process inferior-erlang-buffer))
4403 (process-kill-without-query inferior-erlang-process)
4404 (switch-to-buffer inferior-erlang-buffer)
4405 (if (and (not (eq system-type 'windows-nt))
4406 (eq inferior-erlang-shell-type 'newshell))
4407 (setq comint-process-echoes t))
4408 ;; `rename-buffer' takes only one argument in Emacs 18.
4409 (condition-case nil
4410 (rename-buffer inferior-erlang-buffer-name t)
4411 (error (rename-buffer inferior-erlang-buffer-name)))
4412 (erlang-shell-mode))
4415 (defun inferior-erlang-run-or-select ()
4416 "Switch to an inferior Erlang buffer, possibly starting new process."
4417 (interactive)
4418 (if (null (inferior-erlang-running-p))
4419 (inferior-erlang)
4420 (inferior-erlang-display-buffer t)))
4423 (defun inferior-erlang-display-buffer (&optional select)
4424 "Make the inferior Erlang process visible.
4425 The window is returned.
4427 Should `inferior-erlang-display-buffer-any-frame' be nil the buffer is
4428 displayed in the current frame. Should it be non-nil, and the buffer
4429 already is visible in any other frame, no new window will be created.
4430 Should it be the atom 'raise, the frame containing the window will
4431 be raised.
4433 Should the optional argument SELECT be non-nil, the window is
4434 selected. Should the window be in another frame, that frame is raised.
4436 Note, should the mouse pointer be places outside the raised frame, that
4437 frame will become deselected before the next command."
4438 (interactive)
4439 (or (inferior-erlang-running-p)
4440 (error "No inferior Erlang process is running"))
4441 (let ((win (inferior-erlang-window
4442 inferior-erlang-display-buffer-any-frame))
4443 (frames-p (fboundp 'selected-frame)))
4444 (if (null win)
4445 (let ((old-win (selected-window)))
4446 (save-excursion
4447 (switch-to-buffer-other-window inferior-erlang-buffer)
4448 (setq win (selected-window)))
4449 (select-window old-win))
4450 (if (and window-system
4451 frames-p
4452 (or select
4453 (eq inferior-erlang-display-buffer-any-frame 'raise))
4454 (not (eq (selected-frame) (window-frame win))))
4455 (raise-frame (window-frame win))))
4456 (if select
4457 (select-window win))
4458 (sit-for 0)
4459 win))
4462 (defun inferior-erlang-running-p ()
4463 "Non-nil when an inferior Erlang is running."
4464 (and inferior-erlang-process
4465 (memq (process-status inferior-erlang-process) '(run open))
4466 inferior-erlang-buffer
4467 (buffer-name inferior-erlang-buffer)))
4470 (defun inferior-erlang-window (&optional all-frames)
4471 "Return the window containing the inferior Erlang, or nil."
4472 (and (inferior-erlang-running-p)
4473 (if (and all-frames (>= erlang-emacs-major-version 19))
4474 (get-buffer-window inferior-erlang-buffer t)
4475 (get-buffer-window inferior-erlang-buffer))))
4478 (defun inferior-erlang-wait-prompt ()
4479 "Wait until the inferior Erlang shell prompt appears."
4480 (if (eq inferior-erlang-prompt-timeout t)
4482 (or (inferior-erlang-running-p)
4483 (error "No inferior Erlang shell is running"))
4484 (save-excursion
4485 (set-buffer inferior-erlang-buffer)
4486 (let ((msg nil))
4487 (while (save-excursion
4488 (goto-char (process-mark inferior-erlang-process))
4489 (forward-line 0)
4490 (not (looking-at comint-prompt-regexp)))
4491 (if msg
4493 (setq msg t)
4494 (message "Waiting for Erlang shell prompt (press C-g to abort)."))
4495 (or (accept-process-output inferior-erlang-process
4496 inferior-erlang-prompt-timeout)
4497 (error "No Erlang shell prompt before timeout")))
4498 (if msg (message ""))))))
4500 (autoload 'comint-send-input "comint")
4502 (defun inferior-erlang-send-command (cmd &optional hist)
4503 "Send command CMD to the inferior Erlang.
4505 The contents of the current command line (if any) will
4506 be placed at the next prompt.
4508 If optional second argument is non-nil the command is inserted into
4509 the history list.
4511 Return the position after the newly inserted command."
4512 (or (inferior-erlang-running-p)
4513 (error "No inferior Erlang process is running"))
4514 (let ((old-buffer (current-buffer))
4515 (insert-point (marker-position (process-mark inferior-erlang-process)))
4516 (insert-length (if comint-process-echoes
4518 (1+ (length cmd)))))
4519 (set-buffer inferior-erlang-buffer)
4520 (goto-char insert-point)
4521 (insert cmd)
4522 ;; Strange things happened if `comint-eol-on-send' is declared
4523 ;; in the `let' expression above, but setq:d here. The
4524 ;; `set-buffer' statement obviously makes the buffer local
4525 ;; instance of `comint-eol-on-send' shadow this one.
4526 ;; I'm considering this a bug in Elisp.
4528 ;; This was previously cautioned against in the Lisp manual. It
4529 ;; has been sorted out in Emacs 21. -- fx
4530 (let ((comint-eol-on-send nil)
4531 (comint-input-filter (if hist comint-input-filter 'ignore)))
4532 (comint-send-input))
4533 ;; Adjust all windows whose points are incorrect.
4534 (if (null comint-process-echoes)
4535 (walk-windows
4536 (function
4537 (lambda (window)
4538 (if (and (eq (window-buffer window) inferior-erlang-buffer)
4539 (= (window-point window) insert-point))
4540 (set-window-point window
4541 (+ insert-point insert-length)))))
4542 nil t))
4543 (set-buffer old-buffer)
4544 (+ insert-point insert-length)))
4547 (defun inferior-erlang-strip-delete (&optional s)
4548 "Remove `^H' (delete) and the characters it was supposed to remove."
4549 (interactive)
4550 (if (and (boundp 'comint-last-input-end)
4551 (boundp 'comint-last-output-start))
4552 (save-excursion
4553 (goto-char
4554 (if (interactive-p)
4555 (symbol-value 'comint-last-input-end)
4556 (symbol-value 'comint-last-output-start)))
4557 (while (progn (skip-chars-forward "^\C-h")
4558 (not (eq (point) (point-max))))
4559 (delete-char 1)
4560 (or (bolp)
4561 (backward-delete-char 1))))))
4564 ;; Basically `comint-strip-ctrl-m', with a few extra checks.
4565 (defun inferior-erlang-strip-ctrl-m (&optional string)
4566 "Strip trailing `^M' characters from the current output group."
4567 (interactive)
4568 (if (and (boundp 'comint-last-input-end)
4569 (boundp 'comint-last-output-start))
4570 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
4571 (save-excursion
4572 (goto-char
4573 (if (interactive-p)
4574 (symbol-value 'comint-last-input-end)
4575 (symbol-value 'comint-last-output-start)))
4576 (while (re-search-forward "\r+$" pmark t)
4577 (replace-match "" t t))))))
4580 (defun inferior-erlang-compile ()
4581 "Compile the file in the current buffer.
4583 Should Erlang return `{error, nofile}' it could not load the object
4584 module after completing the compilation. This is due to a bug in the
4585 compile command `c' when using the option `outdir'.
4587 There exists two workarounds for this bug:
4589 1) Place the directory in the Erlang load path.
4591 2) Set the Emacs variable `erlang-compile-use-outdir' to nil.
4592 To do so, place the following line in your `~/.emacs'-file:
4593 (setq erlang-compile-use-outdir nil)"
4594 (interactive)
4595 (save-some-buffers)
4596 (or (inferior-erlang-running-p)
4597 (save-excursion
4598 (inferior-erlang)))
4599 (or (inferior-erlang-running-p)
4600 (error "Error starting inferior Erlang shell"))
4601 (let ((dir (file-name-directory (buffer-file-name)))
4602 ;;; (file (file-name-nondirectory (buffer-file-name)))
4603 (noext (substring (buffer-file-name) 0 -4))
4604 ;; Hopefully, noone else will ever use these...
4605 (tmpvar "Tmp7236")
4606 (tmpvar2 "Tmp8742")
4607 end)
4608 (inferior-erlang-display-buffer)
4609 (inferior-erlang-wait-prompt)
4610 (setq end (inferior-erlang-send-command
4611 (if erlang-compile-use-outdir
4612 (format "c(\"%s\", [{outdir, \"%s\"}])." noext dir)
4613 (format
4614 (concat
4615 "f(%s), {ok, %s} = file:get_cwd(), "
4616 "file:set_cwd(\"%s\"), "
4617 "%s = c(\"%s\"), file:set_cwd(%s), f(%s), %s.")
4618 tmpvar2 tmpvar
4620 tmpvar2 noext tmpvar tmpvar tmpvar2))
4621 nil))
4622 (inferior-erlang-wait-prompt)
4623 (save-excursion
4624 (set-buffer inferior-erlang-buffer)
4625 (setq compilation-error-list nil)
4626 (set-marker compilation-parsing-end end))
4627 (setq compilation-last-buffer inferior-erlang-buffer)))
4630 ;; `next-error' only accepts buffers with major mode `compilation-mode'
4631 ;; or with the minor mode `compilation-minor-mode' activated.
4632 ;; (To activate the minor mode is out of the question, since it will
4633 ;; ruin the inferior Erlang keymap.)
4634 ;; This is done differently in Emacs 21.
4635 (defun inferior-erlang-next-error (&optional argp)
4636 "Just like `next-error'.
4637 Capable of finding error messages in an inferior Erlang buffer."
4638 (interactive "P")
4639 (let ((done nil)
4640 (buf (and (boundp 'compilation-last-buffer)
4641 compilation-last-buffer)))
4642 (if (and (bufferp buf)
4643 (save-excursion
4644 (set-buffer buf)
4645 (and (eq major-mode 'erlang-shell-mode)
4646 (setq major-mode 'compilation-mode))))
4647 (unwind-protect
4648 (progn
4649 (setq done t)
4650 (next-error argp))
4651 (save-excursion
4652 (set-buffer buf)
4653 (setq major-mode 'erlang-shell-mode))))
4654 (or done
4655 (next-error argp))))
4658 (defun inferior-erlang-change-directory (&optional dir)
4659 "Make the inferior Erlang change directory.
4660 The default is to go to the directory of the current buffer."
4661 (interactive)
4662 (or dir (setq dir (file-name-directory (buffer-file-name))))
4663 (or (inferior-erlang-running-p)
4664 (error "No inferior Erlang is running"))
4665 (inferior-erlang-display-buffer)
4666 (inferior-erlang-wait-prompt)
4667 (inferior-erlang-send-command (format "cd('%s')." dir) nil))
4669 (defun erlang-align-arrows (start end)
4670 "Align arrows (\"->\") in function clauses from START to END.
4671 When called interactively, aligns arrows after function clauses inside
4672 the region.
4674 With a prefix argument, aligns all arrows, not just those in function
4675 clauses.
4677 Example:
4679 sum(L) -> sum(L, 0).
4680 sum([H|T], Sum) -> sum(T, Sum + H);
4681 sum([], Sum) -> Sum.
4683 becomes:
4685 sum(L) -> sum(L, 0).
4686 sum([H|T], Sum) -> sum(T, Sum + H);
4687 sum([], Sum) -> Sum."
4688 (interactive "r")
4689 (save-excursion
4690 (let (;; regexp for matching arrows. without a prefix argument,
4691 ;; the regexp matches function heads. With a prefix, it
4692 ;; matches any arrow.
4693 (re (if current-prefix-arg
4694 "^.*\\(\\)->"
4695 (eval-when-compile
4696 (concat "^" erlang-atom-regexp ".*\\(\\)->"))))
4697 ;; part of regexp matching directly before the arrow
4698 (arrow-match-pos (if current-prefix-arg
4700 (1+ erlang-atom-regexp-matches)))
4701 ;; accumulator for positions where arrows are found, ordered
4702 ;; by buffer position (from greatest to smallest)
4703 (arrow-positions '())
4704 ;; accumulator for longest distance from start of line to arrow
4705 (most-indent 0)
4706 ;; marker to track the end of the region we're aligning
4707 (end-marker (progn (goto-char end)
4708 (point-marker))))
4709 ;; Pass 1: Find the arrow positions, adjust the whitespace
4710 ;; before each arrow to one space, and find the greatest
4711 ;; indentation level.
4712 (goto-char start)
4713 (while (re-search-forward re end-marker t)
4714 (goto-char (match-beginning arrow-match-pos))
4715 (just-one-space) ; adjust whitespace
4716 (setq arrow-positions (cons (point) arrow-positions))
4717 (setq most-indent (max most-indent (erlang-column-number))))
4718 (set-marker end-marker nil) ; free the marker
4719 ;; Pass 2: Insert extra padding so that all arrow indentation is
4720 ;; equal. This is done last-to-first by buffer position, so that
4721 ;; inserting spaces before one arrow doesn't change the
4722 ;; positions of the next ones.
4723 (mapcar (lambda (arrow-pos)
4724 (goto-char arrow-pos)
4725 (let* ((pad (- most-indent (erlang-column-number))))
4726 (when (> pad 0)
4727 (insert-char ?\ pad))))
4728 arrow-positions))))
4730 (defun erlang-column-number ()
4731 "Return the column number of the current position in the buffer.
4732 Tab characters are counted by their visual width."
4733 (string-width (buffer-substring (line-beginning-position) (point))))
4735 (defun erlang-current-defun ()
4736 "`add-log-current-defun-function' for Erlang."
4737 (save-excursion
4738 (erlang-beginning-of-function)
4739 (if (looking-at "[a-z0-9_]+")
4740 (match-string 0))))
4742 ;; Aliases for backward compatibility with older versions of Erlang Mode.
4744 ;; Unfortuantely, older versions of Emacs doesn't have `defalias' and
4745 ;; `make-obsolete' so we have to define our own `obsolete' function.
4747 (defun erlang-obsolete (sym newdef)
4748 "Make the obsolete function SYM refer to the defined function NEWDEF.
4750 Simplified version of a combination `defalias' and `make-obsolete',
4751 it assumes that NEWDEF is loaded."
4752 (defalias sym (symbol-function newdef))
4753 (if (fboundp 'make-obsolete)
4754 (make-obsolete sym newdef)))
4757 (erlang-obsolete 'calculate-erlang-indent 'erlang-calculate-indent)
4758 (erlang-obsolete 'calculate-erlang-stack-indent
4759 'erlang-calculate-stack-indent)
4760 (erlang-obsolete 'at-erlang-keyword 'erlang-at-keyword)
4761 (erlang-obsolete 'at-erlang-operator 'erlang-at-operator)
4762 (erlang-obsolete 'beginning-of-erlang-clause 'erlang-beginning-of-clause)
4763 (erlang-obsolete 'end-of-erlang-clause 'erlang-end-of-clause)
4764 (erlang-obsolete 'mark-erlang-clause 'erlang-mark-clause)
4765 (erlang-obsolete 'beginning-of-erlang-function 'erlang-beginning-of-function)
4766 (erlang-obsolete 'end-of-erlang-function 'erlang-end-of-function)
4767 (erlang-obsolete 'mark-erlang-function 'erlang-mark-function)
4768 (erlang-obsolete 'pass-over-erlang-clause 'erlang-pass-over-function)
4769 (erlang-obsolete 'name-of-erlang-function 'erlang-name-of-function)
4772 ;; Fixme: shouldn't redefine `set-visited-file-name' anyhow -- see above.
4773 (defconst erlang-unload-hook
4774 (list (lambda ()
4775 (defalias 'set-visited-file-name
4776 'erlang-orig-set-visited-file-name)
4777 (when (featurep 'advice)
4778 (ad-unadvise 'Man-notify-when-ready)
4779 (ad-unadvise 'set-visited-file-name)))))
4781 ;; The end...
4783 (provide 'erlang)
4785 (run-hooks 'erlang-load-hook)
4787 ;; Local variables:
4788 ;; coding: iso-8859-1
4789 ;; End:
4791 ;;; erlang.el ends here