For release.
[erlware-mode.git] / erlang.el
blob39450e3b5455cba788b052b8496fd1640a9f0710
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.18
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.18"
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\\|_monitor\\)\\|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)
944 ;; nil
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)))
1062 (defvar erlang-dollar-is-escape t
1063 "If non-nil, a dollar sign escapes everything.
1064 There is (to the best of this author's knowledge) no solution
1065 that fits all code. When this is set to t, a string ending with
1066 $ (e.g. \"foo$\") will be incorrectly highlighted (though
1067 \"foo\\$\" works); when nil, the character double-quote
1068 \(i.e. $\") will be incorrectly highlighted (though $\\\" works).
1069 Pick your poison.
1071 Changing this variable after you've opened an Erlang file doesn't
1072 do anything; use the function `erlang-toggle-dollar-is-escape'.")
1074 (defun erlang-toggle-dollar-is-escape ()
1075 "Toggle syntax class of the dollar sign in Erlang mode.
1076 See `erlang-dollar-is-escape' for details."
1077 (interactive)
1078 (setq erlang-dollar-is-escape (not erlang-dollar-is-escape))
1079 (setq erlang-mode-syntax-table nil)
1080 ;; change in all erlang mode buffers
1081 (dolist (buffer (buffer-list))
1082 (with-current-buffer buffer
1083 (when (eq major-mode 'erlang-mode)
1084 (erlang-mode)))))
1086 (defun erlang-syntax-table-init ()
1087 (if (null erlang-mode-syntax-table)
1088 (let ((table (make-syntax-table)))
1089 (modify-syntax-entry ?\n ">" table)
1090 (modify-syntax-entry ?\" "\"" table)
1091 (modify-syntax-entry ?# "." table)
1092 (if erlang-dollar-is-escape
1093 (modify-syntax-entry ?$ "\\" table)
1094 (modify-syntax-entry ?$ "'" table))
1095 (modify-syntax-entry ?% "<" table)
1096 (modify-syntax-entry ?& "." table)
1097 (modify-syntax-entry ?\' "w" table)
1098 (modify-syntax-entry ?* "." table)
1099 (modify-syntax-entry ?+ "." table)
1100 (modify-syntax-entry ?- "." table)
1101 (modify-syntax-entry ?/ "." table)
1102 (modify-syntax-entry ?: "." table)
1103 (modify-syntax-entry ?< "." table)
1104 (modify-syntax-entry ?= "." table)
1105 (modify-syntax-entry ?> "." table)
1106 (modify-syntax-entry ?\\ "\\" table)
1107 (modify-syntax-entry ?_ "_" table)
1108 (modify-syntax-entry ?| "." table)
1110 (setq erlang-mode-syntax-table table)))
1112 (set-syntax-table erlang-mode-syntax-table))
1115 (defun erlang-electric-init ()
1116 ;; Set up electric character functions to work with
1117 ;; delsel/pending-del mode. Also, set up text properties for bit
1118 ;; syntax handling.
1119 (mapcar #'(lambda (cmd)
1120 (put cmd 'delete-selection t) ;for delsel (Emacs)
1121 (put cmd 'pending-delete t)) ;for pending-del (XEmacs)
1122 '(erlang-electric-semicolon
1123 erlang-electric-comma
1124 erlang-electric-gt))
1126 (put 'bitsyntax-open-outer 'syntax-table '(4 . ?>))
1127 (put 'bitsyntax-open-outer 'rear-nonsticky '(category))
1128 (put 'bitsyntax-open-inner 'rear-nonsticky '(category))
1129 (put 'bitsyntax-close-inner 'rear-nonsticky '(category))
1130 (put 'bitsyntax-close-outer 'syntax-table '(5 . ?<))
1131 (put 'bitsyntax-close-outer 'rear-nonsticky '(category))
1132 (setq parse-sexp-lookup-properties 't))
1135 (defun erlang-mode-variables ()
1136 (or erlang-mode-abbrev-table
1137 (define-abbrev-table 'erlang-mode-abbrev-table ()))
1138 (setq local-abbrev-table erlang-mode-abbrev-table)
1139 (make-local-variable 'paragraph-start)
1140 (setq paragraph-start (concat "^$\\|" page-delimiter))
1141 (make-local-variable 'paragraph-separate)
1142 (setq paragraph-separate paragraph-start)
1143 (make-local-variable 'paragraph-ignore-fill-prefix)
1144 (setq paragraph-ignore-fill-prefix t)
1145 (make-local-variable 'require-final-newline)
1146 (setq require-final-newline t)
1147 (make-local-variable 'defun-prompt-regexp)
1148 (setq defun-prompt-regexp erlang-defun-prompt-regexp)
1149 (make-local-variable 'comment-start)
1150 (setq comment-start "%")
1151 (make-local-variable 'comment-start-skip)
1152 (setq comment-start-skip "%+\\s *")
1153 (make-local-variable 'indent-line-function)
1154 (setq indent-line-function 'erlang-indent-command)
1155 (make-local-variable 'indent-region-function)
1156 (setq indent-region-function 'erlang-indent-region)
1157 (set (make-local-variable 'comment-indent-function) 'erlang-comment-indent)
1158 (if (<= erlang-emacs-major-version 18)
1159 (set (make-local-variable 'comment-indent-hook) 'erlang-comment-indent))
1160 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1161 (set (make-local-variable 'dabbrev-case-fold-search) nil)
1162 (set (make-local-variable 'imenu-prev-index-position-function)
1163 'erlang-beginning-of-function)
1164 (set (make-local-variable 'imenu-extract-index-name-function)
1165 'erlang-get-function-name-and-arity)
1166 (set (make-local-variable 'tempo-match-finder)
1167 "[^-a-zA-Z0-9_]\\([-a-zA-Z0-9_]*\\)\\=")
1168 (set (make-local-variable 'beginning-of-defun-function)
1169 'erlang-beginning-of-function)
1170 (set (make-local-variable 'end-of-defun-function) 'erlang-end-of-function)
1171 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil)
1172 (set (make-local-variable 'fill-paragraph-function) 'erlang-fill-paragraph)
1173 (set (make-local-variable 'comment-add)
1174 (if erlang-oldstyle-comment-indent 1 0))
1175 (set (make-local-variable 'outline-regexp) "[[:lower:]0-9_]+ *(.*) *-> *$")
1176 (set (make-local-variable 'outline-level) (lambda () 1))
1177 (set (make-local-variable 'add-log-current-defun-function)
1178 'erlang-current-defun))
1181 ;; Compilation.
1183 ;; The following code is compatible with the standard package `compilation',
1184 ;; making it possible to go to errors using `erlang-next-error' (or just
1185 ;; `next-error' in Emacs 21).
1187 ;; The normal `compile' command works of course. For best result, please
1188 ;; execute `make' with the `-w' flag.
1190 ;; Please see the variables named `compiling-..' above.
1192 (defun erlang-add-compilation-alist (alist)
1193 (require 'compile)
1194 (cond ((boundp 'compilation-error-regexp-alist) ; Emacs 19
1195 (while alist
1196 (or (assoc (car (car alist)) compilation-error-regexp-alist)
1197 (setq compilation-error-regexp-alist
1198 (cons (car alist) compilation-error-regexp-alist)))
1199 (setq alist (cdr alist))))
1200 ((boundp 'compilation-error-regexp)
1201 ;; Emacs 18, Only one regexp is allowed.
1202 (funcall (symbol-function 'set)
1203 'compilation-error-regexp (car (car alist))))))
1205 (defun erlang-font-lock-init ()
1206 "Initialize Font Lock for Erlang mode."
1207 (or erlang-font-lock-syntax-table
1208 (setq erlang-font-lock-syntax-table
1209 (let ((table (copy-syntax-table erlang-mode-syntax-table)))
1210 (modify-syntax-entry ?_ "w" table)
1211 table)))
1212 (set (make-local-variable 'font-lock-syntax-table)
1213 erlang-font-lock-syntax-table)
1214 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1215 'erlang-beginning-of-clause)
1216 (make-local-variable 'font-lock-keywords)
1217 (let ((level (cond ((boundp 'font-lock-maximum-decoration)
1218 (symbol-value 'font-lock-maximum-decoration))
1219 ((boundp 'font-lock-use-maximal-decoration)
1220 (symbol-value 'font-lock-use-maximal-decoration))
1221 (t nil))))
1222 (if (consp level)
1223 (setq level (cdr-safe (or (assq 'erlang-mode level)
1224 (assq t level)))))
1225 ;; `level' can here be:
1226 ;; A number - The fontification level
1227 ;; nil - Use the default
1228 ;; t - Use maximum
1229 (cond ((eq level nil)
1230 (set 'font-lock-keywords erlang-font-lock-keywords))
1231 ((eq level 1)
1232 (set 'font-lock-keywords erlang-font-lock-keywords-1))
1233 ((eq level 2)
1234 (set 'font-lock-keywords erlang-font-lock-keywords-2))
1236 (set 'font-lock-keywords erlang-font-lock-keywords-3))))
1238 ;; Modern font-locks can handle the above much more elegantly:
1239 (set (make-local-variable 'font-lock-defaults)
1240 '((erlang-font-lock-keywords erlang-font-lock-keywords-1
1241 erlang-font-lock-keywords-2 erlang-font-lock-keywords-3)
1242 nil nil ((?_ . "w")) erlang-beginning-of-clause
1243 (font-lock-mark-block-function . erlang-mark-clause))))
1247 ;; Useful when defining your own keywords.
1248 (defun erlang-font-lock-set-face (ks &rest faces)
1249 "Replace the face components in a list of keywords.
1251 The first argument, KS, is a list of keywords. The rest of the
1252 arguments are expressions to replace the face information with. The
1253 first expression replaces the face of the first keyword, the second
1254 expression the second keyword etc.
1256 Should an expression be nil, the face of the corresponding keyword is
1257 not changed.
1259 Should fewer expressions than keywords be given, the last expression
1260 is used for all remaining keywords.
1262 Normally, the expressions are just atoms representing the new face.
1263 They could however be more complex, returning different faces in
1264 different situations.
1266 This function only handles keywords with elements on the forms:
1267 (REGEXP NUMBER FACE)
1268 (REGEXP NUMBER FACE OVERWRITE)
1270 This could be used when defining your own special font-lock setup, e.g:
1272 \(setq my-font-lock-keywords
1273 (append erlang-font-lock-keywords-func
1274 erlang-font-lock-keywords-dollar
1275 (erlang-font-lock-set-face
1276 erlang-font-lock-keywords-macros 'my-neon-green-face)
1277 (erlang-font-lock-set-face
1278 erlang-font-lock-keywords-lc 'my-deep-red 'my-light-red)
1279 erlang-font-lock-keywords-attr))
1281 For a more elaborate example, please see the beginning of the file
1282 `erlang.el'."
1283 (let ((res '()))
1284 (while ks
1285 (let* ((regexp (car (car ks)))
1286 (number (car (cdr (car ks))))
1287 (new-face (if (and faces (car faces))
1288 (car faces)
1289 (car (cdr (cdr (car ks))))))
1290 (overwrite (car (cdr (cdr (cdr (car ks))))))
1291 (new-keyword (list regexp number new-face)))
1292 (if overwrite (nconc new-keyword (list overwrite)))
1293 (setq res (cons new-keyword res))
1294 (setq ks (cdr ks))
1295 (if (and faces (cdr faces))
1296 (setq faces (cdr faces)))))
1297 (nreverse res)))
1300 (defun erlang-font-lock-level-0 ()
1301 ;; DocStringOrig: font-cmd
1302 "Unfontify current buffer."
1303 (interactive)
1304 (font-lock-mode 0))
1307 (defun erlang-font-lock-level-1 ()
1308 ;; DocStringCopy: font-cmd
1309 "Fontify current buffer at level 1.
1310 This highlights function headers, reserved keywords, strings and comments."
1311 (interactive)
1312 (require 'font-lock)
1313 (set 'font-lock-keywords erlang-font-lock-keywords-1)
1314 (font-lock-mode 1)
1315 (funcall (symbol-function 'font-lock-fontify-buffer)))
1318 (defun erlang-font-lock-level-2 ()
1319 ;; DocStringCopy: font-cmd
1320 "Fontify current buffer at level 2.
1321 This highlights level 1 features (see `erlang-font-lock-level-1')
1322 plus bifs, guards and `single quotes'."
1323 (interactive)
1324 (require 'font-lock)
1325 (set 'font-lock-keywords erlang-font-lock-keywords-2)
1326 (font-lock-mode 1)
1327 (funcall (symbol-function 'font-lock-fontify-buffer)))
1330 (defun erlang-font-lock-level-3 ()
1331 ;; DocStringCopy: font-cmd
1332 "Fontify current buffer at level 3.
1333 This highlights level 2 features (see `erlang-font-lock-level-2')
1334 plus variables, macros and records."
1335 (interactive)
1336 (require 'font-lock)
1337 (set 'font-lock-keywords erlang-font-lock-keywords-3)
1338 (font-lock-mode 1)
1339 (funcall (symbol-function 'font-lock-fontify-buffer)))
1342 (defun erlang-menu-init ()
1343 "Init menus for Erlang mode.
1345 The variable `erlang-menu-items' contain a description of the Erlang
1346 mode menu. Normally, the list contains atoms, representing variables
1347 bound to pieces of the menu.
1349 Personal extensions could be added to `erlang-menu-personal-items'.
1351 This function should be called if any variable describing the
1352 menu configuration is changed."
1353 (erlang-menu-install "Erlang" erlang-menu-items erlang-mode-map t))
1356 (defun erlang-menu-install (name items keymap &optional popup)
1357 "Install a menu in Emacs or XEmacs based on an abstract description.
1359 NAME is the name of the menu.
1361 ITEMS is a list. The elements are either nil representing a horizontal
1362 line or a list with two or three elements. The first is the name of
1363 the menu item, the second the function to call, or a submenu, on the
1364 same same form as ITEMS. The third optional element is an expression
1365 which is evaluated every time the menu is displayed. Should the
1366 expression evaluate to nil the menu item is ghosted.
1368 KEYMAP is the keymap to add to menu to. (When using XEmacs, the menu
1369 will only be visible when this menu is the global, the local, or an
1370 activate minor mode keymap.)
1372 If POPUP is non-nil, the menu is bound to the XEmacs `mode-popup-menu'
1373 variable, i.e. it will popup when pressing the right mouse button.
1375 Please see the variable `erlang-menu-base-items'."
1376 (cond (erlang-xemacs-p
1377 (let ((menu (erlang-menu-xemacs name items keymap)))
1378 ;; We add the menu to the global menubar.
1379 (funcall (symbol-function 'add-submenu) nil menu)
1380 (setcdr erlang-xemacs-popup-menu (cdr menu))
1381 (if (and popup (boundp 'mode-popup-menu))
1382 (funcall (symbol-function 'set)
1383 'mode-popup-menu erlang-xemacs-popup-menu))))
1384 ((>= erlang-emacs-major-version 19)
1385 (define-key keymap (vector 'menu-bar (intern name))
1386 (erlang-menu-make-keymap name items)))
1387 (t nil)))
1390 (defun erlang-menu-make-keymap (name items)
1391 "Build a menu for Emacs 19."
1392 (let ((menumap (funcall (symbol-function 'make-sparse-keymap)
1393 name))
1394 (count 0)
1395 id def first second third)
1396 (setq items (reverse items))
1397 (while items
1398 ;; Replace any occurrence of atoms by their value.
1399 (while (and items (atom (car items)) (not (null (car items))))
1400 (if (and (boundp (car items))
1401 (listp (symbol-value (car items))))
1402 (setq items (append (reverse (symbol-value (car items)))
1403 (cdr items)))
1404 (setq items (cdr items))))
1405 (setq first (car-safe (car items)))
1406 (setq second (car-safe (cdr-safe (car items))))
1407 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1408 (cond ((null first)
1409 (setq count (+ count 1))
1410 (setq id (intern (format "separator-%d" count)))
1411 (setq def '("--" . nil)))
1412 ((and (consp second) (eq (car second) 'lambda))
1413 (setq count (+ count 1))
1414 (setq id (intern (format "lambda-%d" count)))
1415 (setq def (cons first second)))
1416 ((symbolp second)
1417 (setq id second)
1418 (setq def (cons first second)))
1420 (setq count (+ count 1))
1421 (setq id (intern (format "submenu-%d" count)))
1422 (setq def (erlang-menu-make-keymap first second))))
1423 (define-key menumap (vector id) def)
1424 (if third
1425 (put id 'menu-enable third))
1426 (setq items (cdr items)))
1427 (cons name menumap)))
1430 (defun erlang-menu-xemacs (name items &optional keymap)
1431 "Build a menu for XEmacs."
1432 (let ((res '())
1433 first second third entry)
1434 (while items
1435 ;; Replace any occurrence of atoms by their value.
1436 (while (and items (atom (car items)) (not (null (car items))))
1437 (if (and (boundp (car items))
1438 (listp (symbol-value (car items))))
1439 (setq items (append (reverse (symbol-value (car items)))
1440 (cdr items)))
1441 (setq items (cdr items))))
1442 (setq first (car-safe (car items)))
1443 (setq second (car-safe (cdr-safe (car items))))
1444 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1445 (cond ((null first)
1446 (setq res (cons "------" res)))
1447 ((symbolp second)
1448 (setq res (cons (vector first second (or third t)) res)))
1449 ((and (consp second) (eq (car second) 'lambda))
1450 (setq res (cons (vector first (list 'call-interactively second)
1451 (or third t)) res)))
1453 (setq res (cons (cons first
1454 (cdr (erlang-menu-xemacs
1455 first second)))
1456 res))))
1457 (setq items (cdr items)))
1458 (setq res (reverse res))
1459 ;; When adding a menu to a minor-mode keymap under Emacs,
1460 ;; it disappears when the mode is disabled. The expression
1461 ;; generated below imitates this behaviour.
1462 ;; (This could be expressed much clearer using backquotes,
1463 ;; but I don't want to pull in every package.)
1464 (if keymap
1465 (let ((expr (list 'or
1466 (list 'eq keymap 'global-map)
1467 (list 'eq keymap (list 'current-local-map))
1468 (list 'symbol-value
1469 (list 'car-safe
1470 (list 'rassq
1471 keymap
1472 'minor-mode-map-alist))))))
1473 (setq res (cons ':included (cons expr res)))))
1474 (cons name res)))
1477 (defun erlang-menu-substitute (items alist)
1478 "Substitute functions in menu described by ITEMS.
1480 The menu ITEMS is updated destructively.
1482 ALIST is list of pairs where the car is the old function and cdr the new."
1483 (let (first second pair)
1484 (while items
1485 (setq first (car-safe (car items)))
1486 (setq second (car-safe (cdr-safe (car items))))
1487 (cond ((null first))
1488 ((symbolp second)
1489 (setq pair (and second (assq second alist)))
1490 (if pair
1491 (setcar (cdr (car items)) (cdr pair))))
1492 ((and (consp second) (eq (car second) 'lambda)))
1494 (erlang-menu-substitute second alist)))
1495 (setq items (cdr items)))))
1498 (defun erlang-menu-add-above (entry above items)
1499 "Add menu ENTRY above menu entry ABOVE in menu ITEMS.
1500 Do nothing if the items already should be in the menu.
1501 Should ABOVE not be in the list, the entry is added at
1502 the bottom of the menu.
1504 The new menu is returned. No guarantee is given that the original
1505 menu is left unchanged.
1507 The equality test is performed by `eq'.
1509 Example: (erlang-menu-add-above 'my-erlang-menu-items
1510 'erlang-menu-man-items)"
1511 (erlang-menu-add-below entry above items t))
1514 (defun erlang-menu-add-below (entry below items &optional above-p)
1515 "Add menu ENTRY below menu items BELOW in the Erlang menu.
1516 Do nothing if the items already should be in the menu.
1517 Should BELOW not be in the list, items is added at the bottom
1518 of the menu.
1520 The new menu is returned. No guarantee is given that the original
1521 menu is left unchanged.
1523 The equality test is performed by `eq'.
1525 Example:
1527 \(setq erlang-menu-items
1528 (erlang-menu-add-below 'my-erlang-menu-items
1529 'erlang-menu-base-items
1530 erlang-menu-items))"
1531 (if (memq entry items)
1532 items ; Return the original menu.
1533 (let ((head '())
1534 (done nil)
1535 res)
1536 (while (not done)
1537 (cond ((null items)
1538 (setq res (append head (list entry)))
1539 (setq done t))
1540 ((eq below (car items))
1541 (setq res
1542 (if above-p
1543 (append head (cons entry items))
1544 (append head (cons (car items)
1545 (cons entry (cdr items))))))
1546 (setq done t))
1548 (setq head (append head (list (car items))))
1549 (setq items (cdr items)))))
1550 res)))
1552 (defun erlang-menu-delete (entry items)
1553 "Delete ENTRY from menu ITEMS.
1555 The new menu is returned. No guarantee is given that the original
1556 menu is left unchanged."
1557 (delq entry items))
1559 ;; Man code:
1561 (defun erlang-man-init ()
1562 "Add menus containing the manual pages of the Erlang.
1564 The variable `erlang-man-dirs' contains entries describing
1565 the location of the manual pages."
1566 (interactive)
1567 (if erlang-man-inhibit
1569 (setq erlang-menu-man-items
1570 '(nil
1571 ("Man - Function" erlang-man-function)))
1572 (if erlang-man-dirs
1573 (setq erlang-menu-man-items
1574 (append erlang-menu-man-items
1575 (erlang-man-make-top-menu erlang-man-dirs))))
1576 (setq erlang-menu-items
1577 (erlang-menu-add-above 'erlang-menu-man-items
1578 'erlang-menu-version-items
1579 erlang-menu-items))
1580 (erlang-menu-init)))
1583 (defun erlang-man-uninstall ()
1584 "Remove the man pages from the Erlang mode."
1585 (interactive)
1586 (setq erlang-menu-items
1587 (erlang-menu-delete 'erlang-menu-man-items erlang-menu-items))
1588 (erlang-menu-init))
1591 ;; The man menu is a hierarchal structure, with the manual sections
1592 ;; at the top, described by `erlang-man-dirs'. The next level could
1593 ;; either be the manual pages if not to many, otherwise it is an index
1594 ;; menu whose submenus will contain up to `erlang-man-max-menu-size'
1595 ;; manual pages.
1597 (defun erlang-man-make-top-menu (dir-list)
1598 "Create one menu entry per element of DIR-LIST.
1599 The format is described in the documentation of `erlang-man-dirs'."
1600 (let ((menu '())
1601 dir)
1602 (while dir-list
1603 (setq dir (cond ((nth 2 (car dir-list))
1604 ;; Relative to `erlang-man-root-dir'.
1605 (and (stringp erlang-man-root-dir)
1606 (concat erlang-man-root-dir (nth 1 (car dir-list)))))
1608 ;; Absolute
1609 (nth 1 (car dir-list)))))
1610 (if (and dir
1611 (file-readable-p dir))
1612 (setq menu (cons (list (car (car dir-list))
1613 (erlang-man-make-middle-menu
1614 (erlang-man-get-files dir)))
1615 menu)))
1616 (setq dir-list (cdr dir-list)))
1617 ;; Should no menus be found, generate a menu item which
1618 ;; will display a help text, when selected.
1619 (if menu
1620 (nreverse menu)
1621 '(("Man Pages"
1622 (("Error! Why?" erlang-man-describe-error)))))))
1625 ;; Should the menu be to long, let's split it into a number of
1626 ;; smaller menus. Warning, this code contains beautiful
1627 ;; destructive operations!
1628 (defun erlang-man-make-middle-menu (filelist)
1629 "Create the second level menu from FILELIST.
1631 Should the list be longer than `erlang-man-max-menu-size', a tree of
1632 menus is created."
1633 (if (<= (length filelist) erlang-man-max-menu-size)
1634 (erlang-man-make-menu filelist)
1635 (let ((menu '())
1636 (filelist (copy-sequence filelist))
1637 segment submenu pair)
1638 (while filelist
1639 (setq pair (nthcdr (- erlang-man-max-menu-size 1) filelist))
1640 (setq segment filelist)
1641 (if (null pair)
1642 (setq filelist nil)
1643 (setq filelist (cdr pair))
1644 (setcdr pair nil))
1645 (setq submenu (erlang-man-make-menu segment))
1646 (setq menu (cons (list (concat (car (car submenu))
1647 " -- "
1648 (car (car (reverse submenu))))
1649 submenu)
1650 menu)))
1651 (nreverse menu))))
1654 (defun erlang-man-make-menu (filelist)
1655 "Make a leaf menu based on FILELIST."
1656 (let ((menu '())
1657 item)
1658 (while filelist
1659 (setq item (erlang-man-make-menu-item (car filelist)))
1660 (if item
1661 (setq menu (cons item menu)))
1662 (setq filelist (cdr filelist)))
1663 (nreverse menu)))
1666 (defun erlang-man-make-menu-item (file)
1667 "Create a menu item containing the name of the man page."
1668 (and (string-match ".*/\\([^/]+\\)\\.[^.]$" file)
1669 (let ((page (substring file (match-beginning 1) (match-end 1))))
1670 (list (capitalize page)
1671 (list 'lambda '()
1672 '(interactive)
1673 (list 'funcall 'erlang-man-display-function
1674 file))))))
1677 (defun erlang-man-get-files (dir)
1678 "Return files in directory DIR."
1679 (directory-files dir t ".*\\.[0-9]\\'"))
1682 (defun erlang-man-module (&optional module)
1683 "Find manual page for MODULE, defaults to module of function under point.
1684 This function is aware of imported functions."
1685 (interactive
1686 (list (let* ((mod (car-safe (erlang-get-function-under-point)))
1687 (input (read-string
1688 (format "Manual entry for module%s: "
1689 (if (or (null mod) (string= mod ""))
1691 (format " (default %s)" mod))))))
1692 (if (string= input "")
1694 input))))
1695 (or module (setq module (car (erlang-get-function-under-point))))
1696 (if (or (null module) (string= module ""))
1697 (error "No Erlang module name given"))
1698 (let ((dir-list erlang-man-dirs)
1699 (pat (concat "/" (regexp-quote module) "\\.[^.]$"))
1700 (file nil)
1701 file-list)
1702 (while (and dir-list (null file))
1703 (setq file-list (erlang-man-get-files
1704 (if (nth 2 (car dir-list))
1705 (concat erlang-man-root-dir (nth 1 (car dir-list)))
1706 (nth 1 (car dir-list)))))
1707 (while (and file-list (null file))
1708 (if (string-match pat (car file-list))
1709 (setq file (car file-list)))
1710 (setq file-list (cdr file-list)))
1711 (setq dir-list (cdr dir-list)))
1712 (if file
1713 (funcall erlang-man-display-function file)
1714 (error "No manual page for module %s found" module))))
1717 ;; Warning, the function `erlang-man-function' is a hack!
1718 ;; It links itself into the man code in a non-clean way. I have
1719 ;; chosen to keep it since it provides a very useful functionality
1720 ;; which is not possible to achieve using a clean approach.
1721 ;; / AndersL
1723 (defvar erlang-man-function-name nil
1724 "Name of function for last `erlang-man-function' call.
1725 Used for communication between `erlang-man-function' and the
1726 patch to `Man-notify-when-ready'.")
1728 (defun erlang-man-function (&optional name)
1729 "Find manual page for NAME, where NAME is module:function.
1730 The entry for `function' is displayed.
1732 This function is aware of imported functions."
1733 (interactive
1734 (list (let* ((mod-func (erlang-get-function-under-point))
1735 (mod (car-safe mod-func))
1736 (func (nth 1 mod-func))
1737 (input (read-string
1738 (format
1739 "Manual entry for `module:func' or `module'%s: "
1740 (if (or (null mod) (string= mod ""))
1742 (format " (default %s:%s)" mod func))))))
1743 (if (string= input "")
1744 (if (and mod func)
1745 (concat mod ":" func)
1746 mod)
1747 input))))
1748 ;; Emacs 18 doesn't provide `man'...
1749 (condition-case nil
1750 (require 'man)
1751 (error nil))
1752 (let ((modname nil)
1753 (funcname nil))
1754 (cond ((null name)
1755 (let ((mod-func (erlang-get-function-under-point)))
1756 (setq modname (car-safe mod-func))
1757 (setq funcname (nth 1 mod-func))))
1758 ((string-match ":" name)
1759 (setq modname (substring name 0 (match-beginning 0)))
1760 (setq funcname (substring name (match-end 0) nil)))
1761 ((stringp name)
1762 (setq modname name)))
1763 (if (or (null modname) (string= modname ""))
1764 (error "No Erlang module name given"))
1765 (cond ((fboundp 'Man-notify-when-ready)
1766 ;; Emacs 19: The man command could possibly start an
1767 ;; asynchronous process, i.e. we must hook ourselves into
1768 ;; the system to be activated when the man-process
1769 ;; terminates.
1770 (if (null funcname)
1772 (erlang-man-patch-notify)
1773 (setq erlang-man-function-name funcname))
1774 (condition-case nil
1775 (erlang-man-module modname)
1776 (error (setq erlang-man-function-name nil))))
1778 (erlang-man-module modname)
1779 (if funcname
1780 (erlang-man-find-function
1781 (or (get-buffer "*Manual Entry*") ; Emacs 18
1782 (current-buffer)) ; XEmacs
1783 funcname))))))
1786 ;; Should the defadvice be at the top level, the package `advice' would
1787 ;; be required. Now it is only required when this functionality
1788 ;; is used. (Emacs 19 specific.)
1789 (defun erlang-man-patch-notify ()
1790 "Patch the function `Man-notify-when-ready' to search for function.
1791 The variable `erlang-man-function-name' is assumed to be bound to
1792 the function name, or to nil.
1794 The reason for patching a function is that under Emacs 19, the man
1795 command is executed asynchronously."
1796 (condition-case nil
1797 (require 'advice)
1798 ;; This should never happened since this is only called when
1799 ;; running under Emacs 19.
1800 (error (error (concat "This command needs the package `advice', "
1801 "please upgrade your Emacs."))))
1802 (require 'man)
1803 (defadvice Man-notify-when-ready
1804 (after erlang-Man-notify-when-ready activate)
1805 "Set point at the documentation of the function name in
1806 `erlang-man-function-name' when the man page is displayed."
1807 (if erlang-man-function-name
1808 (erlang-man-find-function (ad-get-arg 0) erlang-man-function-name))
1809 (setq erlang-man-function-name nil)))
1812 (defun erlang-man-find-function (buf func)
1813 "Find manual page for function in `erlang-man-function-name' in buffer BUF."
1814 (if func
1815 (let ((win (get-buffer-window buf)))
1816 (if win
1817 (progn
1818 (set-buffer buf)
1819 (goto-char (point-min))
1820 (if (re-search-forward
1821 (concat "^[ \t]+" func " ?(")
1822 (point-max) t)
1823 (progn
1824 (forward-word -1)
1825 (set-window-point win (point)))
1826 (message "Could not find function `%s'" func)))))))
1829 (defun erlang-man-display (file)
1830 "Display FILE as a `man' file.
1831 This is the default manual page display function.
1832 The variables `erlang-man-display-function' contains the function
1833 to be used."
1834 ;; Emacs 18 doesn't `provide' man.
1835 (condition-case nil
1836 (require 'man)
1837 (error nil))
1838 (if file
1839 (let ((process-environment (copy-sequence process-environment)))
1840 (if (string-match "\\(.*\\)/man[^/]*/\\([^/]+\\)\\.[^.]$" file)
1841 (let ((dir (substring file (match-beginning 1) (match-end 1)))
1842 (page (substring file (match-beginning 2) (match-end 2))))
1843 (if (fboundp 'setenv)
1844 (setenv "MANPATH" dir)
1845 ;; Emacs 18
1846 (setq process-environment (cons (concat "MANPATH=" dir)
1847 process-environment)))
1848 (cond ((not (and (not erlang-xemacs-p)
1849 (= erlang-emacs-major-version 19)
1850 (< erlang-emacs-minor-version 29)))
1851 (manual-entry page))
1853 ;; Emacs 19.28 and earlier versions of 19:
1854 ;; The manual-entry command unconditionally prompts
1855 ;; the user :-(
1856 (funcall (symbol-function 'Man-getpage-in-background)
1857 page))))
1858 (error "Can't find man page for %s\n" file)))))
1861 (defun erlang-man-describe-error ()
1862 "Describe why the manual pages weren't found."
1863 (interactive)
1864 (with-output-to-temp-buffer "*Erlang Man Error*"
1865 (princ "Normally, this menu should contain Erlang manual pages.
1867 In order to find the manual pages, the variable `erlang-man-root-dir'
1868 should be bound to the name of the directory containing the Erlang
1869 man pages. The name should not include the final slash.
1871 Practically, you should add a line on the following form to
1872 your ~/.emacs, or ask your system administrator to add it to
1873 the site init file:
1875 (setq erlang-man-root-dir \"/usr/local/erlang\")
1877 After installing the line, kill and restart Emacs, or restart Erlang
1878 mode with the command `M-x erlang-mode RET'.")))
1880 ;; Indentation code:
1882 (defun erlang-indent-command (&optional whole-exp)
1883 "Indent current line as Erlang code.
1884 With argument, indent any additional lines of the same clause
1885 rigidly along with this one."
1886 (interactive "P")
1887 (if whole-exp
1888 ;; If arg, always indent this line as Erlang
1889 ;; and shift remaining lines of clause the same amount.
1890 (let ((shift-amt (erlang-indent-line))
1891 beg end)
1892 (save-excursion
1893 (if erlang-tab-always-indent
1894 (beginning-of-line))
1895 (setq beg (point))
1896 (erlang-end-of-clause 1)
1897 (setq end (point))
1898 (goto-char beg)
1899 (forward-line 1)
1900 (setq beg (point)))
1901 (if (> end beg)
1902 (indent-code-rigidly beg end shift-amt "\n")))
1903 (if (and (not erlang-tab-always-indent)
1904 (save-excursion
1905 (skip-chars-backward " \t")
1906 (not (bolp))))
1907 (insert-tab)
1908 (erlang-indent-line))))
1911 (defun erlang-indent-line ()
1912 "Indent current line as Erlang code.
1913 Return the amount the indentation changed by."
1914 (let ((pos (- (point-max) (point)))
1915 indent beg
1916 shift-amt)
1917 (beginning-of-line 1)
1918 (setq beg (point))
1919 (skip-chars-forward " \t")
1920 (cond ((looking-at "%")
1921 (setq indent (funcall comment-indent-function))
1922 (setq shift-amt (- indent (current-column))))
1924 (setq indent (erlang-calculate-indent))
1925 (cond ((null indent)
1926 (setq indent (current-indentation)))
1927 ((eq indent t)
1928 ;; This should never occur here.
1929 (error "Erlang mode error"))
1930 ((= (char-syntax (following-char)) ?\))
1931 (setq indent (1- indent))))
1932 (setq shift-amt (- indent (current-column)))))
1933 (if (zerop shift-amt)
1935 (delete-region beg (point))
1936 (indent-to indent))
1937 ;; If initial point was within line's indentation, position
1938 ;; after the indentation. Else stay at same point in text.
1939 (if (> (- (point-max) pos) (point))
1940 (goto-char (- (point-max) pos)))
1941 shift-amt))
1944 (defun erlang-indent-region (beg end)
1945 "Indent region of Erlang code.
1947 This is automagically called by the user level function `indent-region'."
1948 (interactive "r")
1949 (save-excursion
1950 (let ((case-fold-search nil)
1951 (continue t)
1952 (from-end (- (point-max) end))
1953 indent-point;; The beginning of the current line
1954 indent;; The indent amount
1955 state)
1956 (goto-char beg)
1957 (beginning-of-line)
1958 (setq indent-point (point))
1959 (erlang-beginning-of-clause)
1960 ;; Parse the Erlang code from the beginning of the clause to
1961 ;; the beginning of the region.
1962 (while (< (point) indent-point)
1963 (setq state (erlang-partial-parse (point) indent-point state)))
1964 ;; Indent every line in the region
1965 (while continue
1966 (goto-char indent-point)
1967 (skip-chars-forward " \t")
1968 (cond ((looking-at "%")
1969 ;; Do not use our stack to help the user to customize
1970 ;; comment indentation.
1971 (setq indent (funcall comment-indent-function)))
1972 ((looking-at "$")
1973 ;; Don't indent empty lines.
1974 (setq indent 0))
1976 (setq indent
1977 (save-excursion
1978 (erlang-calculate-stack-indent (point) state)))
1979 (cond ((null indent)
1980 (setq indent (current-indentation)))
1981 ((eq indent t)
1982 ;; This should never occur here.
1983 (error "Erlang mode error"))
1984 ((= (char-syntax (following-char)) ?\))
1985 (setq indent (1- indent))))))
1986 (if (zerop (- indent (current-column)))
1988 (delete-region indent-point (point))
1989 (indent-to indent))
1990 ;; Find the next line in the region
1991 (goto-char indent-point)
1992 (save-excursion
1993 (forward-line 1)
1994 (setq indent-point (point)))
1995 (if (>= from-end (- (point-max) indent-point))
1996 (setq continue nil)
1997 (while (< (point) indent-point)
1998 (setq state (erlang-partial-parse
1999 (point) indent-point state))))))))
2002 (defun erlang-indent-current-buffer ()
2003 "Indent current buffer as Erlang code."
2004 (interactive)
2005 (save-excursion
2006 (save-restriction
2007 (widen)
2008 (erlang-indent-region (point-min) (point-max)))))
2011 (defun erlang-indent-function ()
2012 "Indent current Erlang function."
2013 (interactive)
2014 (save-excursion
2015 (let ((end (progn (erlang-end-of-function 1) (point)))
2016 (beg (progn (erlang-beginning-of-function 1) (point))))
2017 (erlang-indent-region beg end))))
2020 (defun erlang-indent-clause ()
2021 "Indent current Erlang clause."
2022 (interactive)
2023 (save-excursion
2024 (let ((end (progn (erlang-end-of-clause 1) (point)))
2025 (beg (progn (erlang-beginning-of-clause 1) (point))))
2026 (erlang-indent-region beg end))))
2029 (defmacro erlang-push (x stack) (list 'setq stack (list 'cons x stack)))
2030 (defmacro erlang-pop (stack) (list 'setq stack (list 'cdr stack)))
2031 ;; Would much prefer to make caddr a macro but this clashes.
2032 (defun erlang-caddr (x) (car (cdr (cdr x))))
2035 (defun erlang-calculate-indent (&optional parse-start)
2036 "Compute appropriate indentation for current line as Erlang code.
2037 Return nil if line starts inside string, t if in a comment."
2038 (save-excursion
2039 (let ((indent-point (point))
2040 (case-fold-search nil)
2041 (state nil))
2042 (if parse-start
2043 (goto-char parse-start)
2044 (erlang-beginning-of-clause))
2045 (while (< (point) indent-point)
2046 (setq state (erlang-partial-parse (point) indent-point state)))
2047 (erlang-calculate-stack-indent indent-point state))))
2049 (defun erlang-show-syntactic-information ()
2050 "Show syntactic information for current line."
2052 (interactive)
2054 (save-excursion
2055 (let ((starting-point (point))
2056 (case-fold-search nil)
2057 (state nil))
2058 (erlang-beginning-of-clause)
2059 (while (< (point) starting-point)
2060 (setq state (erlang-partial-parse (point) starting-point state)))
2061 (message "%S" state))))
2064 (defun erlang-partial-parse (from to &optional state)
2065 "Parse Erlang syntax starting at FROM until TO, with an optional STATE.
2066 Value is list (stack token-start token-type in-what)."
2067 (goto-char from) ; Start at the beginning
2068 (erlang-skip-blank to)
2069 (let ((cs (char-syntax (following-char)))
2070 (stack (car state))
2071 (token (point))
2072 in-what)
2073 (cond
2075 ;; Done: Return previous state.
2076 ((>= token to)
2077 (setq token (nth 1 state))
2078 (setq cs (nth 2 state))
2079 (setq in-what (nth 3 state)))
2081 ;; Word constituent: check and handle keywords.
2082 ((= cs ?w)
2083 (cond ((looking-at "\\(end\\|after\\)[^_a-zA-Z0-9]")
2084 ;; Must pop top icr layer, `after' will push a new
2085 ;; layer next.
2086 (progn
2087 (while (and stack (eq (car (car stack)) '->))
2088 (erlang-pop stack))
2089 (if (and stack (memq (car (car stack)) '(icr begin)))
2090 (erlang-pop stack))))
2091 ((looking-at "catch[^,\n\\of]*\n")
2092 ;; Must pop top icr layer, `catch' in try/catch
2093 ;;will push a new layer next.
2094 (progn
2095 (while (and stack (eq (car (car stack)) '->))
2096 (erlang-pop stack))
2097 (if (and stack (memq (car (car stack)) '(icr begin)))
2098 (erlang-pop stack))))
2100 (cond ((looking-at "\\(if\\|case\\|receive\\|try\\)[^_a-zA-Z0-9]")
2101 ;; Must push a new icr (if/case/receive) layer.
2102 (erlang-push (list 'icr token (current-column)) stack))
2103 ((looking-at "\\(fun\\)[^_a-zA-Z0-9]")
2104 ;; Push a new icr layer if we are defining a `fun'
2105 ;; expression, not when we are refering an existing
2106 ;; function.
2107 (if (save-excursion
2108 (goto-char (match-end 1))
2109 (erlang-skip-blank to)
2110 (eq (following-char) ?\())
2111 (erlang-push (list 'icr token (current-column)) stack)))
2112 ((looking-at "\\(begin\\|query\\)[^_a-zA-Z0-9]")
2113 (erlang-push (list 'begin token (current-column)) stack))
2114 ;; In test suites you may want to do something like
2115 ;; ?match(Mem when integer(Mem), mnesia:table_info(Tab,
2116 ;; memory)), and then the following if/case/receive
2117 ;; statement will mess up the indentation by fooling the
2118 ;; erlang mode to think the 'when' in the argument is a
2119 ;; "real" when. The following three clauses will avoid
2120 ;; this problem.
2121 ((looking-at "when[^->\.]*if[^->\.]*->"))
2122 ((looking-at "when[^->\.]*case[^->\.]*->"))
2123 ((looking-at "when[^->\.]*receive[^->\.]*->"))
2124 ;; Normal when case
2125 ((looking-at "when [^->\.]*->")
2126 (erlang-push (list 'when token (current-column)) stack))
2127 ((looking-at "after[.]+->")
2128 (erlang-push (list 'icr token (current-column)) stack))
2129 ((looking-at "after[^_a-zA-Z0-9->]")
2130 (erlang-push (list 'icr token (current-column)) stack)
2131 (erlang-push (list '-> token (current-column)) stack))
2132 ((looking-at "catch[^,\n\\of]*\n")
2133 (erlang-push (list 'icr token (current-column)) stack))
2135 (forward-sexp 1))
2137 ;; String: Try to skip over it. (Catch error if not complete.)
2138 ((= cs ?\")
2139 (condition-case nil
2140 (progn
2141 (forward-sexp 1)
2142 (if (> (point) to)
2143 (progn
2144 (setq in-what 'string)
2145 (goto-char to))))
2146 (error
2147 (setq in-what 'string)
2148 (goto-char to))))
2150 ;; Symbol constituent or punctuation
2152 ((memq cs '(?. ?_))
2153 (cond
2155 ;; Clause end
2156 ((= (following-char) ?\;)
2157 (if (and stack (eq (car (car stack)) '->))
2158 (erlang-pop stack))
2159 (forward-char 1))
2161 ;; Function end
2162 ((looking-at "\\.\\(\\s \\|\n\\|\\s<\\)")
2163 (setq stack nil)
2164 (forward-char 1))
2166 ;; Function head
2167 ((looking-at "->\\|:-")
2168 (save-excursion
2169 (back-to-indentation)
2170 (cond ((looking-at "after[^_a-zA-Z0-9]")
2171 (erlang-pop stack))))
2172 (if (and stack (eq (car (car stack)) 'when))
2173 (erlang-pop stack))
2174 (erlang-push (list '-> token (current-column)) stack)
2175 (forward-char 2))
2177 ;; List-comprehension divider
2178 ((looking-at "||")
2179 (erlang-push (list '|| token (current-column)) stack)
2180 (forward-char 2))
2182 ;; Parameter separator
2183 ((looking-at ",")
2184 (forward-char 1))
2186 ;; Bit-syntax open paren
2187 ((looking-at "<<")
2188 (erlang-push (list '\( token (current-column)) stack)
2189 (forward-char 2))
2191 ;; Bbit-syntax close paren
2192 ((looking-at ">>")
2193 (while (memq (car (car stack)) '(|| ->))
2194 (erlang-pop stack))
2195 (cond ((eq (car (car stack)) '\()
2196 (erlang-pop stack))
2197 ((memq (car (car stack)) '(icr begin))
2198 (error "Missing `end'"))
2200 (error "Unbalanced parentheses")))
2201 (forward-char 2))
2203 ;; Macro
2204 ((= (following-char) ??)
2205 ;; Skip over the ?
2206 (forward-char 1)
2209 ;; Other punctuation: Skip over it and any following punctuation
2210 ((= cs ?.)
2211 ;; Skip over all characters in the operand.
2212 (skip-syntax-forward "."))
2214 ;; Other char: Skip over it.
2216 (forward-char 1))))
2218 ;; Open parenthesis
2219 ((= cs ?\()
2220 (erlang-push (list '\( token (current-column)) stack)
2221 (forward-char 1))
2223 ;; Close parenthesis
2224 ((= cs ?\))
2225 (while (memq (car (car stack)) '(|| ->))
2226 (erlang-pop stack))
2227 (cond ((eq (car (car stack)) '\()
2228 (erlang-pop stack))
2229 ((eq (car (car stack)) 'icr)
2230 (erlang-pop stack)
2231 ;; Normal catch not try-catch might have caused icr
2232 ;; and then incr should be removed and is not an error.
2233 (if (eq (car (car stack)) '\()
2234 (erlang-pop stack)
2235 (error "Missing `end'")))
2236 ((eq (car (car stack)) 'begin)
2237 (error "Missing `end'"))
2239 (error "Unbalanced parenthesis")))
2240 (forward-char 1))
2242 ;; Character quote: Skip it and the quoted char.
2243 ((= cs ?/)
2244 (forward-char 2))
2246 ;; Character escape: Skip it and the escape sequence.
2247 ((= cs ?\\)
2248 (forward-char 1)
2249 (skip-syntax-forward "w"))
2251 ;; Everything else
2253 (forward-char 1)))
2254 (list stack token cs in-what)))
2256 (defun erlang-calculate-stack-indent (indent-point state)
2257 "From the given last position and state (stack) calculate indentation.
2258 Return nil if inside string, t if in a comment."
2259 (let* ((stack (and state (car state)))
2260 (token (nth 1 state))
2261 (stack-top (and stack (car stack))))
2262 (cond ((null state) ;No state
2264 ((nth 3 state)
2265 ;; Return nil or t.
2266 (eq (nth 3 state) 'comment))
2267 ((null stack)
2268 (if (looking-at "when[^_a-zA-Z0-9]")
2269 erlang-indent-guard
2271 ((eq (car stack-top) '\()
2272 ;; Element of list, tuple or part of an expression,
2273 (if (null erlang-argument-indent)
2274 ;; indent to next column.
2275 (1+ (nth 2 stack-top))
2276 (goto-char (nth 1 stack-top))
2277 (cond ((looking-at "[({]\\s *\\($\\|%\\)")
2278 ;; Line ends with parenthesis.
2279 (+ (erlang-indent-find-preceding-expr)
2280 erlang-argument-indent))
2282 ;; Indent to the same column as the first
2283 ;; argument.
2284 (goto-char (1+ (nth 1 stack-top)))
2285 (skip-chars-forward " \t")
2286 (current-column)))))
2287 ((eq (car stack-top) 'icr)
2288 ;; The default indentation is the column of the option
2289 ;; directly following the keyword. (This does not apply to
2290 ;; `case'.) Should no option be on the same line, the
2291 ;; indentation is the indentation of the keyword +
2292 ;; `erlang-indent-level'.
2294 ;; `after' should be indented to the save level as the
2295 ;; corresponding receive.
2296 (if (looking-at "\\(after\\|catch\\)[^_a-zA-Z0-9]")
2297 (nth 2 stack-top)
2298 (save-excursion
2299 (goto-char (nth 1 stack-top))
2300 (if (looking-at "case[^_a-zA-Z0-9]")
2301 (+ (nth 2 stack-top) erlang-indent-level)
2302 (skip-chars-forward "a-z")
2303 (skip-chars-forward " \t")
2304 (if (memq (following-char) '(?% ?\n))
2305 (+ (nth 2 stack-top) erlang-indent-level)
2306 (current-column)))))
2308 ;; Real indentation, where operators create extra indentation etc.
2309 ((memq (car stack-top) '(-> || begin))
2310 (goto-char (nth 1 stack-top))
2311 ;; Check if there is more code after the '->' on the
2312 ;; same line. If so use this indentation as base, else
2313 ;; use parent indentation + 2 * level as base.
2314 (let ((off erlang-indent-level)
2315 (skip 2))
2316 (cond ((null (cdr stack))) ; Top level in function.
2317 ((eq (car stack-top) 'begin)
2318 (setq skip 5))
2319 ((eq (car stack-top) '->)
2320 (setq off (* 2 erlang-indent-level))))
2321 (let ((base (erlang-indent-find-base stack indent-point off skip)))
2322 ;; Look at last thing to see how we are to move relative
2323 ;; to the base.
2324 (goto-char token)
2325 (cond ((looking-at "||\\|,\\|->\\|:-")
2326 base)
2327 ((erlang-at-keyword)
2328 (+ (current-column) erlang-indent-level))
2329 ((or (= (char-syntax (following-char)) ?.)
2330 (erlang-at-operator))
2331 (+ base erlang-indent-level))
2333 (goto-char indent-point)
2334 (cond ((memq (following-char) '(?\( ?{))
2335 ;; Function application or record.
2336 (+ (erlang-indent-find-preceding-expr)
2337 erlang-argument-indent))
2338 ;; Empty line, or end; treat it as the end of
2339 ;; the block. (Here we have a choice: should
2340 ;; the user be forced to reindent continued
2341 ;; lines, or should the "end" be reindented?)
2342 ((looking-at "\\(end\\|after\\|catch\\)[^_a-zA-Z0-9]\\|$")
2343 (if (eq (car (car stack)) '->)
2344 (erlang-pop stack))
2345 (if stack
2346 (erlang-caddr (car stack))
2348 ;; Avoid treating comments a continued line.
2349 ((= (following-char) ?%)
2350 base)
2351 ;; Continued line (e.g. line beginning
2352 ;; with an operator.)
2353 (t (+ base erlang-indent-level)))))))
2355 ((eq (car stack-top) 'when)
2356 (goto-char (nth 1 stack-top))
2357 (if (looking-at "when\\s *\\($\\|%\\)")
2358 (progn
2359 (erlang-pop stack)
2360 (if (and stack (eq (nth 0 (car stack)) 'icr))
2361 (progn
2362 (goto-char (nth 1 (car stack)))
2363 (+ (nth 2 (car stack)) erlang-indent-guard
2364 ;; receive XYZ or receive
2365 ;; XYZ
2366 (if (looking-at "[a-z]+\\s *\\($\\|%\\)")
2367 erlang-indent-level
2368 (* 2 erlang-indent-level))))
2369 erlang-indent-guard))
2370 ;; "when" is followed by code, let's indent to the same
2371 ;; column.
2372 (forward-char 4) ; Skip "when"
2373 (skip-chars-forward " \t")
2374 (current-column))))))
2377 (defun erlang-indent-find-base (stack indent-point &optional offset skip)
2378 "Find the base column for current stack."
2379 (or skip (setq skip 2))
2380 (or offset (setq offset erlang-indent-level))
2381 (save-excursion
2382 (let* ((stack-top (car stack)))
2383 (goto-char (nth 1 stack-top))
2384 (forward-char skip)
2385 (if (looking-at "\\s *\\($\\|%\\)")
2386 (progn
2387 (if (memq (car stack-top) '(-> ||))
2388 (erlang-pop stack))
2389 ;; Take parent identation + offset,
2390 ;; else just erlang-indent-level if no parent
2391 (if stack
2392 (+ (erlang-caddr (car stack))
2393 offset)
2394 erlang-indent-level))
2395 (erlang-skip-blank indent-point)
2396 (current-column)))))
2399 ;; Does not handle `begin' .. `end'.
2400 (defun erlang-indent-find-preceding-expr ()
2401 "Return the first column of the preceding expression.
2402 This assumes that the preceding expression is either simple
2403 \(i.e. an atom) or parenthesized."
2404 (save-excursion
2405 (forward-sexp -1)
2406 (let ((col (current-column)))
2407 (skip-chars-backward " \t")
2408 ;; Needed to match the colon in "'foo':'bar'".
2409 (if (not (memq (preceding-char) '(?# ?:)))
2411 (backward-char 1)
2412 (forward-sexp -1)
2413 (current-column)))))
2416 (defun erlang-skip-blank (&optional lim)
2417 "Skip over whitespace and comments until limit reached."
2418 (or lim (setq lim (point-max)))
2419 (let (stop)
2420 (while (and (not stop) (< (point) lim))
2421 (cond ((= (following-char) ?%)
2422 (skip-chars-forward "^\n" lim))
2423 ((= (following-char) ?\n)
2424 (skip-chars-forward "\n" lim))
2425 ((looking-at "\\s ")
2426 (if (re-search-forward "\\S " lim 'move)
2427 (forward-char -1)))
2429 (setq stop t))))
2430 stop))
2432 (defun erlang-at-keyword ()
2433 "Are we looking at an Erlang keyword which will increase indentation?"
2434 (looking-at (concat "\\(when\\|if\\|fun\\|case\\|begin\\|query\\|"
2435 "of\\|receive\\|after\\|catch\\)[^_a-zA-Z0-9]")))
2437 (defun erlang-at-operator ()
2438 "Are we looking at an Erlang operator?"
2439 (looking-at
2440 "\\(bnot\\|div\\|mod\\|band\\|bor\\|bxor\\|bsl\\|bsr\\)[^_a-zA-Z0-9]"))
2442 (defun erlang-comment-indent ()
2443 "Compute Erlang comment indentation.
2445 Used both by `indent-for-comment' and the Erlang specific indentation
2446 commands."
2447 (if (not erlang-oldstyle-comment-indent)
2448 (cond ((looking-at "%%") 0)
2449 ((looking-at "%")
2450 (or (erlang-calculate-indent)
2451 (current-indentation))))
2452 (cond ((looking-at "%%%") 0)
2453 ((looking-at "%%")
2454 (or (erlang-calculate-indent)
2455 (current-indentation)))
2457 (save-excursion
2458 (skip-chars-backward " \t")
2459 (max (if (bolp) 0 (1+ (current-column)))
2460 comment-column))))))
2463 ;;; Erlang movement commands
2465 ;; All commands below work as movement commands. I.e. if the point is
2466 ;; at the end of the clause, and the command `erlang-end-of-clause' is
2467 ;; executed, the point is moved to the end of the NEXT clause. (This
2468 ;; mimics the behaviour of `end-of-defun'.)
2470 ;; Personally I would like to rewrite them to be "pure", and add a set
2471 ;; of movement functions, like `erlang-next-clause',
2472 ;; `erlang-previous-clause', and the same for functions.
2474 ;; The current implementation makes it hopeless to use the functions as
2475 ;; subroutines in more complex commands. /andersl
2477 (defun erlang-beginning-of-clause (&optional arg)
2478 "Move backward to previous start of clause.
2479 With argument, do this that many times.
2480 Return t unless search stops due to end of buffer."
2481 (interactive "p")
2482 (or arg (setq arg 1))
2483 (if (< arg 0)
2484 ;; Step back to the end of the previous line, unless we are at
2485 ;; the beginning of the buffer. The reason for this move is
2486 ;; that the regexp below includes the last character of the
2487 ;; previous line.
2488 (if (bobp)
2489 (or (looking-at "\n")
2490 (forward-char 1))
2491 (forward-char -1)
2492 (if (looking-at "\\`\n")
2493 (forward-char 1))))
2494 ;; The regexp matches a function header that isn't
2495 ;; included in a string.
2496 (and (re-search-forward "\\(\\`\\|\\`\n\\|[^\\]\n\\)\\([a-z]\\|'\\|-\\)"
2497 nil 'move (- arg))
2498 (let ((beg (match-beginning 2)))
2499 (and beg (goto-char beg))
2500 t)))
2502 (defun erlang-end-of-clause (&optional arg)
2503 "Move to the end of the current clause.
2504 With argument, do this that many times."
2505 (interactive "p")
2506 (or arg (setq arg 1))
2507 (while (and (looking-at "[ \t]*[%\n]")
2508 (zerop (forward-line 1))))
2509 ;; Move to the next clause.
2510 (erlang-beginning-of-clause (- arg))
2511 (beginning-of-line);; Just to be sure...
2512 (let ((continue t))
2513 (while (and (not (bobp)) continue)
2514 (forward-line -1)
2515 (skip-chars-forward " \t")
2516 (if (looking-at "[%\n]")
2518 (end-of-line)
2519 (setq continue nil)))))
2521 (defun erlang-mark-clause ()
2522 "Put mark at end of clause, point at beginning."
2523 (interactive)
2524 (push-mark (point))
2525 (erlang-end-of-clause 1)
2526 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2527 ;; the region.
2528 (condition-case nil
2529 (push-mark (point) nil t)
2530 (error (push-mark (point))))
2531 (erlang-beginning-of-clause 1)
2532 ;; The above function deactivates the mark.
2533 (if (boundp 'deactivate-mark)
2534 (funcall (symbol-function 'set) 'deactivate-mark nil)))
2536 (defun erlang-beginning-of-function (&optional arg)
2537 "Move backward to previous start of function.
2538 With positive argument, do this that many times.
2539 With negative argument, search forward.
2541 Return t unless search stops due to end of buffer."
2542 (interactive "p")
2543 (or arg (setq arg 1))
2544 (cond
2545 ;; Search backward
2546 ((> arg 0)
2547 (while (and (> arg 0)
2548 (and (erlang-beginning-of-clause 1)
2549 (let ((start (point))
2550 (name (erlang-name-of-function))
2551 (arity (erlang-get-function-arity)))
2552 ;; Note: "arity" is nil for e.g. "-import", hence
2553 ;; two "-import" clauses are not considered to
2554 ;; be part of the same function.
2555 (while (and (erlang-beginning-of-clause 1)
2556 (string-equal name
2557 (erlang-name-of-function))
2558 arity
2559 (equal arity
2560 (erlang-get-function-arity)))
2561 (setq start (point)))
2562 (goto-char start)
2563 t)))
2564 (setq arg (1- arg))))
2565 ;; Search forward
2566 ((< arg 0)
2567 (end-of-line)
2568 (erlang-beginning-of-clause 1)
2569 ;; Step -arg functions forward.
2570 (while (and (< arg 0)
2571 ;; Step one function forward, or stop if the end of
2572 ;; the buffer was reached. Return t if we found the
2573 ;; function.
2574 (let ((name (erlang-name-of-function))
2575 (arity (erlang-get-function-arity))
2576 (found (erlang-beginning-of-clause -1)))
2577 (while (and found
2578 (string-equal name (erlang-name-of-function))
2579 arity
2580 (equal arity
2581 (erlang-get-function-arity)))
2582 (setq found (erlang-beginning-of-clause -1)))
2583 found))
2584 (setq arg (1+ arg)))))
2585 (zerop arg))
2588 (defun erlang-end-of-function (&optional arg)
2589 "Move forward to next end of function.
2591 With argument, do this that many times.
2592 With negative argument go towards the beginning of the buffer."
2593 (interactive "p")
2594 (or arg (setq arg 1))
2595 (let ((first t))
2596 ;; Forward
2597 (while (and (> arg 0) (< (point) (point-max)))
2598 (let ((pos (point)))
2599 (while (progn
2600 (if (and first
2601 (progn
2602 (forward-char 1)
2603 (erlang-beginning-of-clause 1)))
2605 (or (bobp) (forward-char -1))
2606 (erlang-beginning-of-clause -1))
2607 (setq first nil)
2608 (erlang-pass-over-function)
2609 (skip-chars-forward " \t")
2610 (if (looking-at "[%\n]")
2611 (forward-line 1))
2612 (<= (point) pos))))
2613 (setq arg (1- arg)))
2614 ;; Backward
2615 (while (< arg 0)
2616 (let ((pos (point)))
2617 (erlang-beginning-of-clause 1)
2618 (erlang-pass-over-function)
2619 (forward-line 1)
2620 (if (>= (point) pos)
2621 (if (erlang-beginning-of-function 2)
2622 (progn
2623 (erlang-pass-over-function)
2624 (skip-chars-forward " \t")
2625 (if (looking-at "[%\n]")
2626 (forward-line 1)))
2627 (goto-char (point-min)))))
2628 (setq arg (1+ arg)))))
2630 (eval-and-compile
2631 (if (default-boundp 'beginning-of-defun-function)
2632 (defalias 'erlang-mark-function 'mark-defun)
2633 (defun erlang-mark-function ()
2634 "Put mark at end of function, point at beginning."
2635 (interactive)
2636 (push-mark (point))
2637 (erlang-end-of-function 1)
2638 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2639 ;; the region.
2640 (condition-case nil
2641 (push-mark (point) nil t)
2642 (error (push-mark (point))))
2643 (erlang-beginning-of-function 1)
2644 ;; The above function deactivates the mark.
2645 (if (boundp 'deactivate-mark)
2646 (funcall (symbol-function 'set) 'deactivate-mark nil)))))
2648 (defun erlang-pass-over-function ()
2649 (while (progn
2650 (erlang-skip-blank)
2651 (and (not (looking-at "\\.\\(\\s \\|\n\\|\\s<\\)"))
2652 (not (eobp))))
2653 (forward-sexp 1))
2654 (if (not (eobp))
2655 (forward-char 1)))
2657 (defun erlang-name-of-function ()
2658 (save-excursion
2659 ;; Skip over attribute leader.
2660 (if (looking-at "-[ \t]*")
2661 (re-search-forward "-[ \t]*" nil 'move))
2662 (let ((start (point)))
2663 (forward-sexp 1)
2664 (buffer-substring start (point)))))
2667 ;;; Miscellaneous
2669 (defun erlang-fill-paragraph (&optional justify)
2670 "Like \\[fill-paragraph], but handle Erlang comments.
2671 If any of the current line is a comment, fill the comment or the
2672 paragraph of it that point is in, preserving the comment's indentation
2673 and initial `%':s."
2674 (interactive "P")
2675 (let ((has-comment nil)
2676 ;; If has-comment, the appropriate fill-prefix for the comment.
2677 comment-fill-prefix)
2678 ;; Figure out what kind of comment we are looking at.
2679 (save-excursion
2680 (beginning-of-line)
2681 (cond
2682 ;; Find the command prefix.
2683 ((looking-at (concat "\\s *" comment-start-skip))
2684 (setq has-comment t)
2685 (setq comment-fill-prefix (buffer-substring (match-beginning 0)
2686 (match-end 0))))
2687 ;; A line with some code, followed by a comment? Remember that the
2688 ;; % which starts the comment shouldn't be part of a string or
2689 ;; character.
2690 ((progn
2691 (while (not (looking-at "%\\|$"))
2692 (skip-chars-forward "^%\n\"\\\\")
2693 (cond
2694 ((eq (char-after (point)) ?\\) (forward-char 2))
2695 ((eq (char-after (point)) ?\") (forward-sexp 1))))
2696 (looking-at comment-start-skip))
2697 (setq has-comment t)
2698 (setq comment-fill-prefix
2699 (concat (make-string (current-column) ? )
2700 (buffer-substring (match-beginning 0) (match-end 0)))))))
2701 (if (not has-comment)
2702 (fill-paragraph justify)
2703 ;; Narrow to include only the comment, and then fill the region.
2704 (save-restriction
2705 (narrow-to-region
2706 ;; Find the first line we should include in the region to fill.
2707 (save-excursion
2708 (while (and (zerop (forward-line -1))
2709 (looking-at "^\\s *%")))
2710 ;; We may have gone to far. Go forward again.
2711 (or (looking-at "^\\s *%")
2712 (forward-line 1))
2713 (point))
2714 ;; Find the beginning of the first line past the region to fill.
2715 (save-excursion
2716 (while (progn (forward-line 1)
2717 (looking-at "^\\s *%")))
2718 (point)))
2719 ;; Lines with only % on them can be paragraph boundaries.
2720 (let ((paragraph-start (concat paragraph-start "\\|^[ \t%]*$"))
2721 (paragraph-separate (concat paragraph-start "\\|^[ \t%]*$"))
2722 (fill-prefix comment-fill-prefix))
2723 (fill-paragraph justify))))))
2726 (defun erlang-uncomment-region (beg end)
2727 "Uncomment all commented lines in the region."
2728 (interactive "r")
2729 (comment-region beg end -1))
2732 (defun erlang-generate-new-clause ()
2733 "Create additional Erlang clause header.
2735 Parses the source file for the name of the current Erlang function.
2736 Create the header containing the name, A pair of parentheses,
2737 and an arrow. The space between the function name and the
2738 first parenthesis is preserved. The point is placed between
2739 the parentheses."
2740 (interactive)
2741 (let ((name (save-excursion
2742 (and (erlang-beginning-of-clause)
2743 (erlang-get-function-name t))))
2744 (arrow (save-excursion
2745 (and (erlang-beginning-of-clause)
2746 (erlang-get-function-arrow)))))
2747 (if (or (null arrow) (null name))
2748 (error "Can't find name of current Erlang function"))
2749 (if (and (bolp) (eolp))
2751 (end-of-line)
2752 (newline))
2753 (insert name)
2754 (save-excursion
2755 (insert ") " arrow))
2756 (if erlang-new-clause-with-arguments
2757 (erlang-clone-arguments))))
2760 (defun erlang-clone-arguments ()
2761 "Insert, at the point, the argument list of the previous clause.
2763 The mark is set at the beginning of the inserted text, the point
2764 at the end."
2765 (interactive)
2766 (let ((args (save-excursion
2767 (beginning-of-line)
2768 (and (erlang-beginning-of-clause)
2769 (erlang-get-function-arguments))))
2770 (p (point)))
2771 (if (null args)
2772 (error "Can't clone argument list"))
2773 (insert args)
2774 (set-mark p)))
2776 ;;; Information retrieval functions.
2778 (defun erlang-buffer-substring (beg end)
2779 "Like `buffer-substring-no-properties'.
2780 Although, this function works on all versions of Emacs."
2781 (if (fboundp 'buffer-substring-no-properties)
2782 (funcall (symbol-function 'buffer-substring-no-properties) beg end)
2783 (buffer-substring beg end)))
2786 (defun erlang-get-module ()
2787 "Return the name of the module as specified by `-module'.
2789 Return nil if file contains no `-module' attribute."
2790 (save-excursion
2791 (save-restriction
2792 (widen)
2793 (goto-char (point-min))
2794 (let ((md (match-data)))
2795 (unwind-protect
2796 (if (re-search-forward
2797 (eval-when-compile
2798 (concat "^-module\\s *(\\s *\\(\\("
2799 erlang-atom-regexp
2800 "\\)?\\)\\s *)\\s *\\."))
2801 (point-max) t)
2802 (erlang-remove-quotes
2803 (erlang-buffer-substring (match-beginning 1)
2804 (match-end 1)))
2805 nil)
2806 (store-match-data md))))))
2809 (defun erlang-get-module-from-file-name (&optional file)
2810 "Extract the module name from a file name.
2812 First, the directory part is removed. Second, the part of the file name
2813 matching `erlang-file-name-extension-regexp' is removed.
2815 Should the match fail, nil is returned.
2817 By modifying `erlang-file-name-extension-regexp' to match files other
2818 than Erlang source files, Erlang specific functions could be applied on
2819 non-Erlang files. Most notably; the support for Erlang modules in the
2820 tags system could be used by files written in other languages."
2821 (or file (setq file buffer-file-name))
2822 (if (null file)
2824 (setq file (file-name-nondirectory file))
2825 (if (string-match erlang-file-name-extension-regexp file)
2826 (substring file 0 (match-beginning 0))
2827 nil)))
2830 ;; Used by `erlang-get-export' and `erlang-get-import'.
2832 (defun erlang-get-function-arity-list ()
2833 "Parse list of `function/arity' as used by `-import' and `-export'.
2835 Point must be before the opening bracket. When the
2836 function returns the point will be placed after the closing bracket.
2838 The function does not return an error if the list is incorrectly
2839 formatted.
2841 Return list of (function . arity). The order of the returned list
2842 corresponds to the order of the parsed Erlang list."
2843 (let ((res '()))
2844 (erlang-skip-blank)
2845 (forward-char 1)
2846 (if (not (eq (preceding-char) ?\[))
2847 '() ; Not looking at an Erlang list.
2848 (while ; Note: `while' has no body.
2849 (progn
2850 (erlang-skip-blank)
2851 (and (looking-at (eval-when-compile
2852 (concat erlang-atom-regexp "/\\([0-9]+\\)\\>")))
2853 (progn
2854 (setq res (cons
2855 (cons
2856 (erlang-remove-quotes
2857 (erlang-buffer-substring
2858 (match-beginning 1) (match-end 1)))
2859 (string-to-int
2860 (erlang-buffer-substring
2861 (match-beginning
2862 (+ 1 erlang-atom-regexp-matches))
2863 (match-end
2864 (+ 1 erlang-atom-regexp-matches)))))
2865 res))
2866 (goto-char (match-end 0))
2867 (erlang-skip-blank)
2868 (forward-char 1)
2869 ;; Test if there are more exported functions.
2870 (eq (preceding-char) ?,))))))
2871 (nreverse res)))
2874 ;;; Note that `-export' and the open parenthesis must be written on
2875 ;;; the same line.
2877 (defun erlang-get-export ()
2878 "Return a list of `(function . arity)' as specified by `-export'."
2879 (save-excursion
2880 (goto-char (point-min))
2881 (let ((md (match-data))
2882 (res '()))
2883 (unwind-protect
2884 (progn
2885 (while (re-search-forward "^-export\\s *(" (point-max) t)
2886 (erlang-skip-blank)
2887 (setq res (nconc res (erlang-get-function-arity-list))))
2888 res)
2889 (store-match-data md)))))
2892 (defun erlang-get-import ()
2893 "Parse an Erlang source file for imported functions.
2895 Return an alist with module name as car part and list of conses containing
2896 function and arity as cdr part."
2897 (save-excursion
2898 (goto-char (point-min))
2899 (let ((md (match-data))
2900 (res '()))
2901 (unwind-protect
2902 (progn
2903 (while (re-search-forward "^-import\\s *(" (point-max) t)
2904 (erlang-skip-blank)
2905 (if (looking-at erlang-atom-regexp)
2906 (let ((module (erlang-remove-quotes
2907 (erlang-buffer-substring
2908 (match-beginning 0)
2909 (match-end 0)))))
2910 (goto-char (match-end 0))
2911 (erlang-skip-blank)
2912 (if (eq (following-char) ?,)
2913 (progn
2914 (forward-char 1)
2915 (erlang-skip-blank)
2916 (let ((funcs (erlang-get-function-arity-list))
2917 (pair (assoc module res)))
2918 (if pair
2919 (setcdr pair (nconc (cdr pair) funcs))
2920 (setq res (cons (cons module funcs)
2921 res)))))))))
2922 (nreverse res))
2923 (store-match-data md)))))
2926 (defun erlang-get-function-name (&optional arg)
2927 "Return name of current function, or nil.
2929 If optional argument is non-nil, everything up to and including
2930 the first `(' is returned.
2932 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2933 (save-excursion
2934 (if (not (eobp)) (forward-char 1))
2935 (and (erlang-beginning-of-clause)
2936 (erlang-get-function-name t)))"
2937 (let ((n (if arg 0 1)))
2938 (and (looking-at (eval-when-compile
2939 (concat "^" erlang-atom-regexp "\\s *(")))
2940 (erlang-buffer-substring (match-beginning n) (match-end n)))))
2942 (defun erlang-get-function-name-and-arity ()
2943 "Return name and arity of current function (e.g. \"foo/1\"), or nil."
2944 (when (looking-at (eval-when-compile
2945 (concat "^" erlang-atom-regexp "\\s *(")))
2946 (let ((name (erlang-buffer-substring (match-beginning 1) (match-end 1)))
2947 (arity 0))
2948 (save-excursion
2949 (goto-char (match-end 0))
2950 (while (ignore-errors
2951 (skip-syntax-forward "-")
2952 (forward-sexp 1)
2953 (skip-syntax-forward "-")
2955 (when (looking-at "[,)]")
2956 (incf arity))))
2957 (format "%s/%d" name arity))))
2959 (defun erlang-get-function-arrow ()
2960 "Return arrow of current function, could be \"->\", \":-\" or nil.
2962 The \":-\" arrow is used by mnesia queries.
2964 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2965 (save-excursion
2966 (if (not (eobp)) (forward-char 1))
2967 (and (erlang-beginning-of-clause)
2968 (erlang-get-function-arrow)))"
2969 (and
2970 (save-excursion
2971 (re-search-forward "[^-:]*-\\|:" (point-max) t)
2972 (erlang-buffer-substring (- (point) 1) (+ (point) 1)))))
2974 (defun erlang-get-function-arity ()
2975 "Return the number of arguments of function at point, or nil."
2976 (and (looking-at (eval-when-compile
2977 (concat "^" erlang-atom-regexp "\\s *(")))
2978 (save-excursion
2979 (goto-char (match-end 0))
2980 (condition-case nil
2981 (let ((res 0)
2982 (cont t))
2983 (while cont
2984 (cond ((eobp)
2985 (setq res nil)
2986 (setq cont nil))
2987 ((looking-at "\\s *)")
2988 (setq cont nil))
2989 ((looking-at "\\s *\\($\\|%\\)")
2990 (forward-line 1))
2991 ((looking-at "\\s *,")
2992 (setq res (+ 1 res))
2993 (goto-char (match-end 0)))
2995 (when (zerop res)
2996 (setq res (+ 1 res)))
2997 (forward-sexp 1))))
2998 res)
2999 (error nil)))))
3001 (defun erlang-get-function-arguments ()
3002 "Return arguments of current function, or nil."
3003 (if (not (looking-at (eval-when-compile
3004 (concat "^" erlang-atom-regexp "\\s *("))))
3006 (save-excursion
3007 (condition-case nil
3008 (let ((start (match-end 0)))
3009 (goto-char (- start 1))
3010 (forward-sexp)
3011 (erlang-buffer-substring start (- (point) 1)))
3012 (error nil)))))
3015 (defun erlang-get-function-under-point ()
3016 "Return the module and function under the point, or nil.
3018 Should no explicit module name be present at the point, the
3019 list of imported functions is searched.
3021 The following could be returned:
3022 (\"module\" \"function\") -- Both module and function name found.
3023 (nil \"function\") -- No module name was found.
3024 nil -- No function name found
3026 In the future the list may contain more elements."
3027 (save-excursion
3028 (let ((md (match-data))
3029 (res nil))
3030 (if (eq (char-syntax (following-char)) ? )
3031 (skip-chars-backward " \t"))
3032 (skip-chars-backward "a-zA-Z0-9_:'")
3033 (cond ((looking-at (eval-when-compile
3034 (concat erlang-atom-regexp ":" erlang-atom-regexp)))
3035 (setq res (list
3036 (erlang-remove-quotes
3037 (erlang-buffer-substring
3038 (match-beginning 1) (match-end 1)))
3039 (erlang-remove-quotes
3040 (erlang-buffer-substring
3041 (match-beginning (1+ erlang-atom-regexp-matches))
3042 (match-end (1+ erlang-atom-regexp-matches)))))))
3043 ((looking-at erlang-atom-regexp)
3044 (let ((fk (erlang-remove-quotes
3045 (erlang-buffer-substring
3046 (match-beginning 0) (match-end 0))))
3047 (mod nil)
3048 (imports (erlang-get-import)))
3049 (while (and imports (null mod))
3050 (if (assoc fk (cdr (car imports)))
3051 (setq mod (car (car imports)))
3052 (setq imports (cdr imports))))
3053 (setq res (list mod fk)))))
3054 (store-match-data md)
3055 res)))
3058 ;; TODO: Escape single quotes inside the string without
3059 ;; replace-regexp-in-string.
3060 (defun erlang-add-quotes-if-needed (str)
3061 "Return STR, possibly with quotes."
3062 (if (and (stringp str)
3063 (not (string-match (eval-when-compile
3064 (concat "\\`" erlang-atom-regexp "\\'")) str)))
3065 (progn (if (fboundp 'replace-regexp-in-string)
3066 (setq str (replace-regexp-in-string "'" "\\'" str t t )))
3067 (concat "'" str "'"))
3068 str))
3071 (defun erlang-remove-quotes (str)
3072 "Return STR without quotes, if present."
3073 (let ((md (match-data)))
3074 (prog1
3075 (if (string-match "\\`'\\(.*\\)'\\'" str)
3076 (substring str 1 -1)
3077 str)
3078 (store-match-data md))))
3081 ;;; Check module name
3083 ;; The function `write-file', bound to C-x C-w, calls
3084 ;; `set-visited-file-name' which clears the hook. :-(
3085 ;; To make sure that the hook always is present, we advise
3086 ;; `set-visited-file-name'.
3087 (defun erlang-check-module-name-init ()
3088 "Initialize the functionality to compare file and module names.
3090 Unless we have `before-save-hook', we redefine the function
3091 `set-visited-file-name' since it clears the variable
3092 `local-write-file-hooks'. The original function definition is
3093 stored in `erlang-orig-set-visited-file-name'."
3094 (if (boundp 'before-save-hook)
3095 ;; If we have that, `make-local-hook' is obsolete.
3096 (add-hook 'before-save-hook 'erlang-check-module-name nil t)
3097 (require 'advice)
3098 (unless (ad-advised-definition-p 'set-visited-file-name)
3099 (defadvice set-visited-file-name (after erlang-set-visited-file-name
3100 activate)
3101 (if (eq major-mode 'erlang-mode)
3102 (add-hook 'local-write-file-hooks 'erlang-check-module-name))))
3103 (add-hook 'local-write-file-hooks 'erlang-check-module-name)))
3106 (defun erlang-check-module-name ()
3107 "If the module name doesn't match file name, ask for permission to change.
3109 The variable `erlang-check-module-name' controls the behaviour of this
3110 function. It it is nil, this function does nothing. If it is t, the
3111 source is silently changed. If it is set to the atom `ask', the user
3112 is prompted.
3114 This function is normally placed in the hook `local-write-file-hooks'."
3115 (if erlang-check-module-name
3116 (let ((mn (erlang-get-module))
3117 (fn (erlang-get-module-from-file-name (buffer-file-name))))
3118 (if (and (stringp mn) (stringp fn))
3119 (or (string-equal mn fn)
3120 (if (or (eq erlang-check-module-name t)
3121 (y-or-n-p
3122 "Module does not match file name. Modify source? "))
3123 (save-excursion
3124 (save-restriction
3125 (widen)
3126 (goto-char (point-min))
3127 (if (re-search-forward
3128 (eval-when-compile
3129 (concat "^-module\\s *(\\s *\\(\\("
3130 erlang-atom-regexp
3131 "\\)?\\)\\s *)\\s *\\."))
3132 (point-max) t)
3133 (progn
3134 (goto-char (match-beginning 1))
3135 (delete-region (match-beginning 1)
3136 (match-end 1))
3137 (insert fn))))))))))
3138 ;; Must return nil since it is added to `local-write-file-hook'.
3139 nil)
3142 ;;; Electric functions.
3144 (defun erlang-electric-semicolon (&optional arg)
3145 "Insert a semicolon character and possibly a prototype for the next line.
3147 The variable `erlang-electric-semicolon-criteria' states a criterion,
3148 when fulfilled a newline is inserted, the next line is indented and a
3149 prototype for the next line is inserted. Normally the prototype
3150 consists of \" ->\". Should the semicolon end the clause a new clause
3151 header is generated.
3153 The variable `erlang-electric-semicolon-insert-blank-lines' controls
3154 the number of blank lines inserted between the current line and new
3155 function header.
3157 Behaves just like the normal semicolon when supplied with a
3158 numerical arg, point is inside string or comment, or when there are
3159 non-whitespace characters following the point on the current line."
3160 (interactive "P")
3161 (self-insert-command (prefix-numeric-value arg))
3162 (if (or arg
3163 (and (listp erlang-electric-commands)
3164 (not (memq 'erlang-electric-semicolon
3165 erlang-electric-commands)))
3166 (erlang-in-literal)
3167 (not (looking-at "\\s *\\(%.*\\)?$"))
3168 (null (erlang-test-criteria-list
3169 erlang-electric-semicolon-criteria)))
3170 (setq erlang-electric-newline-inhibit nil)
3171 (setq erlang-electric-newline-inhibit t)
3172 (undo-boundary)
3173 (end-of-line)
3174 (newline)
3175 (if (condition-case nil
3176 (progn (erlang-indent-line) t)
3177 (error (if (bolp) (delete-backward-char 1))))
3178 (if (not (bolp))
3179 (save-excursion
3180 (insert " ->"))
3181 (condition-case nil
3182 (progn
3183 (erlang-generate-new-clause)
3184 (if erlang-electric-semicolon-insert-blank-lines
3185 (save-excursion
3186 (beginning-of-line)
3187 (newline
3188 erlang-electric-semicolon-insert-blank-lines))))
3189 (error (if (bolp) (delete-backward-char 1))))))))
3192 (defun erlang-electric-comma (&optional arg)
3193 "Insert a comma character and possibly a new indented line.
3194 The variable `erlang-electric-comma-criteria' states a criterion,
3195 when fulfilled a newline is inserted and the next line is indented.
3197 Behaves just like the normal comma when supplied with a
3198 numerical arg, point is inside string or comment, or when there are
3199 non-whitespace characters following the point on the current line."
3200 (interactive "P")
3202 (self-insert-command (prefix-numeric-value arg))
3204 (if (or arg
3205 (and (listp erlang-electric-commands)
3206 (not (memq 'erlang-electric-comma erlang-electric-commands)))
3207 (erlang-in-literal)
3208 (not (looking-at "\\s *\\(%.*\\)?$"))
3209 (null (erlang-test-criteria-list
3210 erlang-electric-comma-criteria)))
3211 (setq erlang-electric-newline-inhibit nil)
3212 (setq erlang-electric-newline-inhibit t)
3213 (undo-boundary)
3214 (end-of-line)
3215 (newline)
3216 (condition-case nil
3217 (erlang-indent-line)
3218 (error (if (bolp) (delete-backward-char 1))))))
3220 (defun erlang-electric-lt (&optional arg)
3221 "Insert a less-than sign, and optionally mark it as an open paren."
3223 (interactive "p")
3225 (self-insert-command arg)
3227 ;; Was this the second char in bit-syntax open (`<<')?
3228 (unless (< (point) 2)
3229 (save-excursion
3230 (backward-char 2)
3231 (when (and (eq (char-after (point)) ?<)
3232 (not (eq (get-text-property (point) 'category)
3233 'bitsyntax-open-inner)))
3234 ;; Then mark the two chars...
3235 (put-text-property (point) (1+ (point))
3236 'category 'bitsyntax-open-outer)
3237 (forward-char 1)
3238 (put-text-property (point) (1+ (point))
3239 'category 'bitsyntax-open-inner)
3240 ;;...and unmark any subsequent less-than chars.
3241 (forward-char 1)
3242 (while (eq (char-after (point)) ?<)
3243 (remove-text-properties (point) (1+ (point))
3244 '(category nil))
3245 (forward-char 1))))))
3247 (defun erlang-after-bitsyntax-close ()
3248 "Return t if point is immediately after a bit-syntax close parenthesis (`>>')."
3249 (and (>= (point) 2)
3250 (save-excursion
3251 (backward-char 2)
3252 (and (eq (char-after (point)) ?>)
3253 (not (eq (get-text-property (point) 'category)
3254 'bitsyntax-close-outer))))))
3256 (defun erlang-after-arrow ()
3257 "Return true if point is immediately after a function arrow (`->')."
3258 (and (>= (point) 2)
3259 (and
3260 (save-excursion
3261 (backward-char)
3262 (eq (char-before (point)) ?-))
3263 (or (not (listp erlang-electric-commands))
3264 (memq 'erlang-electric-gt
3265 erlang-electric-commands))
3266 (not (erlang-in-literal))
3267 (looking-at "\\s *\\(%.*\\)?$")
3268 (erlang-test-criteria-list erlang-electric-arrow-criteria))))
3271 (defun erlang-electric-gt (&optional arg)
3272 "Insert a greater-than sign, and optionally mark it as a close paren."
3274 (interactive "p")
3276 (self-insert-command arg)
3278 (cond
3279 ;; Did we just write a bit-syntax close (`>>')?
3280 ((erlang-after-bitsyntax-close)
3281 (save-excursion
3282 ;; Then mark the two chars...
3283 (backward-char 2)
3284 (put-text-property (point) (1+ (point))
3285 'category 'bitsyntax-close-inner)
3286 (forward-char)
3287 (put-text-property (point) (1+ (point))
3288 'category 'bitsyntax-close-outer)
3289 ;;...and unmark any subsequent greater-than chars.
3290 (forward-char)
3291 (while (eq (char-after (point)) ?>)
3292 (remove-text-properties (point) (1+ (point))
3293 '(category nil))
3294 (forward-char))))
3296 ;; Did we just write a function arrow (`->')?
3297 ((erlang-after-arrow)
3298 (let ((erlang-electric-newline-inhibit t))
3299 (undo-boundary)
3300 (end-of-line)
3301 (newline)
3302 (condition-case nil
3303 (erlang-indent-line)
3304 (error (if (bolp) (delete-backward-char 1))))))
3306 ;; Then it's just a plain greater-than.
3308 nil)))
3311 (defun erlang-electric-arrow\ off (&optional arg)
3312 "Insert a '>'-sign and possibly a new indented line.
3314 This command is only `electric' when the `>' is part of an `->' arrow.
3315 The variable `erlang-electric-arrow-criteria' states a sequence of
3316 criteria, which decides when a newline should be inserted and the next
3317 line indented.
3319 It behaves just like the normal greater than sign when supplied with a
3320 numerical arg, point is inside string or comment, or when there are
3321 non-whitespace characters following the point on the current line.
3323 After being split/merged into `erlang-after-arrow' and
3324 `erlang-electric-gt', it is now unused and disabled."
3325 (interactive "P")
3326 (let ((prec (preceding-char)))
3327 (self-insert-command (prefix-numeric-value arg))
3328 (if (or arg
3329 (and (listp erlang-electric-commands)
3330 (not (memq 'erlang-electric-arrow
3331 erlang-electric-commands)))
3332 (not (eq prec ?-))
3333 (erlang-in-literal)
3334 (not (looking-at "\\s *\\(%.*\\)?$"))
3335 (null (erlang-test-criteria-list
3336 erlang-electric-arrow-criteria)))
3337 (setq erlang-electric-newline-inhibit nil)
3338 (setq erlang-electric-newline-inhibit t)
3339 (undo-boundary)
3340 (end-of-line)
3341 (newline)
3342 (condition-case nil
3343 (erlang-indent-line)
3344 (error (if (bolp) (delete-backward-char 1)))))))
3347 (defun erlang-electric-newline (&optional arg)
3348 "Break line at point and indent, continuing comment if within one.
3349 The variable `erlang-electric-newline-criteria' states a criterion,
3350 when fulfilled a newline is inserted and the next line is indented.
3352 Should the current line begin with a comment, and the variable
3353 `comment-multi-line' be non-nil, a new comment start is inserted.
3355 Should the previous command be another electric command we assume that
3356 the user pressed newline out of old habit, hence we will do nothing."
3357 (interactive "P")
3358 (cond ((and (not arg)
3359 erlang-electric-newline-inhibit
3360 (memq last-command erlang-electric-newline-inhibit-list))
3361 ()) ; Do nothing!
3362 ((or arg
3363 (and (listp erlang-electric-commands)
3364 (not (memq 'erlang-electric-newline
3365 erlang-electric-commands)))
3366 (null (erlang-test-criteria-list
3367 erlang-electric-newline-criteria)))
3368 (newline (prefix-numeric-value arg)))
3370 (if (and comment-multi-line
3371 (save-excursion
3372 (beginning-of-line)
3373 (looking-at (concat "\\s *" comment-start-skip))))
3374 (let ((str (buffer-substring
3375 (or (match-end 1) (match-beginning 0))
3376 (min (match-end 0) (point)))))
3377 (newline)
3378 (undo-boundary)
3379 (insert str))
3380 (newline-and-indent)))))
3383 (defun erlang-test-criteria-list (criteria)
3384 "Given a list of criterion functions, test if criteria are fulfilled.
3386 Each element in the criteria list can a function returning nil, t or
3387 the atom `stop'. t means that the criterion is fulfilled, `stop' means
3388 that it isn't fulfilled and that the search should stop,
3389 and nil means continue searching.
3391 Should the list contain the atom t the criterion is assumed to be
3392 fulfilled, unless preceded by a function returning `stop', of course.
3394 Should the argument be the atom t instead of a list, the criterion is
3395 assumed to be trivially true.
3397 Should all functions return nil, the criteria are assumed not to be
3398 fulfilled.
3400 Return t if criteria fulfilled, nil otherwise."
3401 (if (eq criteria t)
3403 (save-excursion
3404 (let ((answer nil))
3405 (while (and criteria (null answer))
3406 (if (eq (car criteria) t)
3407 (setq answer t)
3408 (setq answer (funcall (car criteria))))
3409 (setq criteria (cdr criteria)))
3410 (if (and answer (not (eq answer 'stop)))
3412 nil)))))
3415 (defun erlang-in-literal (&optional lim)
3416 "Test if point is in string, quoted atom or comment.
3418 Return one of the three atoms `atom', `string', and `comment'.
3419 Should the point be inside none of the above mentioned types of
3420 context, nil is returned."
3421 (save-excursion
3422 (let* ((lim (or lim (save-excursion
3423 (erlang-beginning-of-clause)
3424 (point))))
3425 (state (if (fboundp 'syntax-ppss) ; post Emacs 21.3
3426 (syntax-ppss)
3427 (parse-partial-sexp lim (point)))))
3428 (cond
3429 ((eq (nth 3 state) ?') 'atom)
3430 ((nth 3 state) 'string)
3431 ((nth 4 state) 'comment)
3432 (t nil)))))
3435 (defun erlang-at-end-of-function-p ()
3436 "Test if point is at end of an Erlang function.
3438 This function is designed to be a member of a criteria list."
3439 (eq (save-excursion (erlang-skip-blank) (point))
3440 (save-excursion
3441 (erlang-beginning-of-function -1) (point))))
3444 (defun erlang-at-end-of-clause-p ()
3445 "Test if point is at end of an Erlang clause.
3447 This function is designed to be a member of a criteria list."
3448 (eq (save-excursion (erlang-skip-blank) (point))
3449 (save-excursion
3450 (erlang-beginning-of-clause -1) (point))))
3453 (defun erlang-stop-when-inside-argument-list ()
3454 "Return `stop' if inside parenthesis list, nil otherwise.
3456 Knows about the list comprehension syntax. When the point is
3457 after `||', `stop' is not returned.
3459 This function is designed to be a member of a criteria list."
3460 (save-excursion
3461 (condition-case nil
3462 (let ((orig-point (point))
3463 (state nil))
3464 (up-list -1)
3465 (if (not (eq (following-char) ?\[))
3466 'stop
3467 ;; Do not return `stop' when inside a list comprehension
3468 ;; construction. (The point must be after `||').
3469 (while (< (point) orig-point)
3470 (setq state (erlang-partial-parse (point) orig-point state)))
3471 (if (and (car state) (eq (car (car (car state))) '||))
3473 'stop)))
3474 (error
3475 nil))))
3478 (defun erlang-stop-when-at-guard ()
3479 "Return `stop' when at function guards.
3481 This function is designed to be a member of a criteria list."
3482 (save-excursion
3483 (beginning-of-line)
3484 (if (and (looking-at (eval-when-compile
3485 (concat "^" erlang-atom-regexp "\\s *(")))
3486 (not (looking-at
3487 (eval-when-compile
3488 (concat "^" erlang-atom-regexp ".*\\(->\\|:-\\)")))))
3489 'stop
3490 nil)))
3493 (defun erlang-next-lines-empty-p ()
3494 "Return non-nil if next lines are empty.
3496 The variable `erlang-next-lines-empty-threshold' contains the number
3497 of lines required to be empty.
3499 A line containing only spaces and tabs is considered empty.
3501 This function is designed to be a member of a criteria list."
3502 (and erlang-next-lines-empty-threshold
3503 (save-excursion
3504 (let ((left erlang-next-lines-empty-threshold)
3505 (cont t))
3506 (while (and cont (> left 0))
3507 (forward-line 1)
3508 (setq cont (looking-at "\\s *$"))
3509 (setq left (- left 1)))
3510 cont))))
3513 (defun erlang-at-keyword-end-p ()
3514 "Test if next readable token is the keyword end.
3516 This function is designed to be a member of a criteria list."
3517 (save-excursion
3518 (erlang-skip-blank)
3519 (looking-at "end[^_a-zA-Z0-9]")))
3522 ;; Erlang tags support which is aware of erlang modules.
3524 ;; Not yet implemented under XEmacs. (Hint: The Emacs 19 etags
3525 ;; package works under XEmacs.)
3527 (eval-when-compile
3528 (if (or (featurep 'bytecomp)
3529 (featurep 'byte-compile))
3530 (progn
3531 (require 'etags))))
3534 ;; Variables:
3536 (defvar erlang-tags-function-alist
3537 '((find-tag . erlang-find-tag)
3538 (find-tag-other-window . erlang-find-tag-other-window)
3539 (find-tag-regexp . erlang-find-tag-regexp)
3540 (find-tag-other-frame . erlang-find-tag-other-frame))
3541 "Alist of old tags commands and the replacement functions.")
3543 (defvar erlang-tags-installed nil
3544 "Non-nil when the Erlang tags system is installed.")
3545 (defvar erlang-tags-file-list '()
3546 "List of files in tag list. Used when finding tag on form `module:'.")
3547 (defvar erlang-tags-completion-table nil
3548 "Like `tags-completion-table', this table contains `tag' and `module:tag'.")
3549 (defvar erlang-tags-buffer-installed-p nil
3550 "Non-nil when Erlang module recognising functions installed.")
3551 (defvar erlang-tags-buffer-list '()
3552 "Temporary list of buffers.")
3553 (defvar erlang-tags-orig-completion-table nil
3554 "Temporary storage for `tags-completion-table'.")
3555 (defvar erlang-tags-orig-tag-order nil
3556 "Temporary storage for `find-tag-tag-order'.")
3557 (defvar erlang-tags-orig-regexp-tag-order nil
3558 "Temporary storage for `find-tag-regexp-tag-order'.")
3559 (defvar erlang-tags-orig-search-function nil
3560 "Temporary storage for `find-tag-search-function'.")
3561 (defvar erlang-tags-orig-regexp-search-function nil
3562 "Temporary storage for `find-tag-regexp-search-function'.")
3563 (defvar erlang-tags-orig-format-hooks nil
3564 "Temporary storage for `tags-table-format-hooks'.") ;v19
3565 (defvar erlang-tags-orig-format-functions nil
3566 "Temporary storage for `tags-table-format-functions'.") ;v > 19
3568 (defun erlang-tags-init ()
3569 "Install an alternate version of tags, aware of Erlang modules.
3571 After calling this function, the tags functions are aware of
3572 Erlang modules. Tags can be entered on the for `module:tag' as well
3573 as on the old form `tag'.
3575 In the completion list, `module:tag' and `module:' shows up.
3577 Call this function from an appropriate init file, or add it to
3578 Erlang mode hook with the commands:
3579 (add-hook 'erlang-mode-hook 'erlang-tags-init)
3580 (add-hook 'erlang-shell-mode-hook 'erlang-tags-init)
3582 This function only works under Emacs 18 and Emacs 19. Currently, It
3583 is not implemented under XEmacs. (Hint: The Emacs 19 etags module
3584 works under XEmacs.)"
3585 (interactive)
3586 (cond ((= erlang-emacs-major-version 18)
3587 (require 'tags)
3588 (erlang-tags-define-keys (current-local-map))
3589 (setq erlang-tags-installed t))
3591 (require 'etags)
3592 ;; Test on a function available in the Emacs 19 version
3593 ;; of tags but not in the XEmacs version.
3594 (if (not (fboundp 'find-tag-noselect))
3596 (erlang-tags-define-keys (current-local-map))
3597 (setq erlang-tags-installed t)))))
3600 ;; Set all keys bound to `find-tag' et.al. in the global map and the
3601 ;; menu to `erlang-find-tag' et.al. in `map'.
3603 ;; The function `substitute-key-definition' does not work properly
3604 ;; in all version of Emacs.
3606 (defun erlang-tags-define-keys (map)
3607 "Bind tags commands to keymap MAP aware of Erlang modules."
3608 (let ((alist erlang-tags-function-alist))
3609 (while alist
3610 (let* ((old (car (car alist)))
3611 (new (cdr (car alist)))
3612 (keys (append (where-is-internal old global-map))))
3613 (while keys
3614 (define-key map (car keys) new)
3615 (setq keys (cdr keys))))
3616 (setq alist (cdr alist))))
3617 ;; Update the menu.
3618 (erlang-menu-substitute erlang-menu-base-items erlang-tags-function-alist)
3619 (erlang-menu-init))
3622 ;; There exists a variable `find-tag-default-function'. It is not used
3623 ;; since `complete-tag' uses it to get current word under point. In that
3624 ;; situation we don't want the module to be prepended.
3626 (defun erlang-find-tag-default ()
3627 "Return the default tag.
3628 Search `-import' list of imported functions.
3629 Single quotes are been stripped away."
3630 (let ((mod-func (erlang-get-function-under-point)))
3631 (cond ((null mod-func)
3632 nil)
3633 ((null (car mod-func))
3634 (nth 1 mod-func))
3636 (concat (car mod-func) ":" (nth 1 mod-func))))))
3639 ;; Return `t' since it is used inside `tags-loop-form'.
3640 ;;;###autoload
3641 (defun erlang-find-tag (modtagname &optional next-p regexp-p)
3642 "Like `find-tag'. Capable of retrieving Erlang modules.
3644 Tags can be given on the forms `tag', `module:', `module:tag'."
3645 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3646 (switch-to-buffer (erlang-find-tag-noselect modtagname next-p regexp-p))
3650 ;; Code mainly from `find-tag-other-window' in `etags.el'.
3651 ;;;###autoload
3652 (defun erlang-find-tag-other-window (tagname &optional next-p regexp-p)
3653 "Like `find-tag-other-window' but aware of Erlang modules."
3654 (interactive (erlang-tag-interactive
3655 "Find `module:tag' or `tag' other window: "))
3657 ;; This is to deal with the case where the tag is found in the
3658 ;; selected window's buffer; without this, point is moved in both
3659 ;; windows. To prevent this, we save the selected window's point
3660 ;; before doing find-tag-noselect, and restore it afterwards.
3661 (let* ((window-point (window-point (selected-window)))
3662 (tagbuf (erlang-find-tag-noselect tagname next-p regexp-p))
3663 (tagpoint (progn (set-buffer tagbuf) (point))))
3664 (set-window-point (prog1
3665 (selected-window)
3666 (switch-to-buffer-other-window tagbuf)
3667 ;; We have to set this new window's point; it
3668 ;; might already have been displaying a
3669 ;; different portion of tagbuf, in which case
3670 ;; switch-to-buffer-other-window doesn't set
3671 ;; the window's point from the buffer.
3672 (set-window-point (selected-window) tagpoint))
3673 window-point)))
3676 (defun erlang-find-tag-other-frame (tagname &optional next-p)
3677 "Like `find-tag-other-frame' but aware of Erlang modules."
3678 (interactive (erlang-tag-interactive
3679 "Find `module:tag' or `tag' other frame: "))
3680 (let ((pop-up-frames t))
3681 (erlang-find-tag-other-window tagname next-p)))
3684 (defun erlang-find-tag-regexp (regexp &optional next-p other-window)
3685 "Like `find-tag-regexp' but aware of Erlang modules."
3686 (interactive (if (fboundp 'find-tag-regexp)
3687 (erlang-tag-interactive
3688 "Find `module:regexp' or `regexp': ")
3689 (error "This version of Emacs can't find tags by regexps")))
3690 (funcall (if other-window
3691 'erlang-find-tag-other-window
3692 'erlang-find-tag)
3693 regexp next-p t))
3696 ;; Just like C-u M-. This could be added to the menu.
3697 (defun erlang-find-next-tag ()
3698 "Find next tag, like \\[find-tag] with prefix arg."
3699 (interactive)
3700 (let ((current-prefix-arg '(4)))
3701 (if erlang-tags-installed
3702 (call-interactively 'erlang-find-tag)
3703 (call-interactively 'find-tag))))
3706 ;; Mimics `find-tag-noselect' found in `etags.el', but uses `find-tag' to
3707 ;; be compatible with `tags.el'.
3709 ;; Handles three cases:
3710 ;; * `module:' Loop over all possible file names. Stop if a file-name
3711 ;; without extension and directory matches the module.
3713 ;; * `module:tag'
3714 ;; Emacs 19: Replace test functions with functions aware of
3715 ;; Erlang modules. Tricky because the etags system wasn't
3716 ;; built for these kind of operations...
3718 ;; Emacs 18: We loop over `find-tag' until we find a file
3719 ;; whose module matches the requested module. The
3720 ;; drawback is that a lot of files could be loaded into
3721 ;; Emacs.
3723 ;; * `tag' Just give it to `find-tag'.
3725 (defun erlang-find-tag-noselect (modtagname &optional next-p regexp-p)
3726 "Like `find-tag-noselect' but aware of Erlang modules."
3727 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3728 (or modtagname
3729 (setq modtagname (symbol-value 'last-tag)))
3730 (funcall (symbol-function 'set) 'last-tag modtagname)
3731 ;; `tags.el' uses this variable to record how M-, would
3732 ;; know where to restart a tags command.
3733 (if (boundp 'tags-loop-form)
3734 (funcall (symbol-function 'set)
3735 'tags-loop-form '(erlang-find-tag nil t)))
3736 (save-window-excursion
3737 (cond
3738 ((string-match ":$" modtagname)
3739 ;; Only the module name was given. Read all files whose file name
3740 ;; match.
3741 (let ((modname (substring modtagname 0 (match-beginning 0)))
3742 (file nil))
3743 (if (not next-p)
3744 (save-excursion
3745 (visit-tags-table-buffer)
3746 (setq erlang-tags-file-list
3747 (funcall (symbol-function 'tags-table-files)))))
3748 (while (null file)
3749 (or erlang-tags-file-list
3750 (save-excursion
3751 (if (and (featurep 'etags)
3752 (funcall
3753 (symbol-function 'visit-tags-table-buffer) 'same)
3754 (funcall
3755 (symbol-function 'visit-tags-table-buffer) t))
3756 (setq erlang-tags-file-list
3757 (funcall (symbol-function 'tags-table-files)))
3758 (error "No %stags containing %s" (if next-p "more " "")
3759 modtagname))))
3760 (if erlang-tags-file-list
3761 (let ((this-module (erlang-get-module-from-file-name
3762 (car erlang-tags-file-list))))
3763 (if (and (stringp this-module)
3764 (string= modname this-module))
3765 (setq file (car erlang-tags-file-list)))
3766 (setq erlang-tags-file-list (cdr erlang-tags-file-list)))))
3767 (set-buffer (or (get-file-buffer file)
3768 (find-file-noselect file)))))
3770 ((string-match ":" modtagname)
3771 (if (boundp 'find-tag-tag-order)
3772 ;; Method one: Add module-recognising functions to the
3773 ;; list of order functions. However, the tags system
3774 ;; from Emacs 18, and derives thereof (read: XEmacs)
3775 ;; hasn't got this feature.
3776 (progn
3777 (erlang-tags-install-module-check)
3778 (unwind-protect
3779 (funcall (symbol-function 'find-tag)
3780 modtagname next-p regexp-p)
3781 (erlang-tags-remove-module-check)))
3782 ;; Method two: Call the tags system until a file matching
3783 ;; the module is found. This could result in that many
3784 ;; files are read. (e.g. The tag "foo:file" will take a
3785 ;; while to process.)
3786 (let* ((modname (substring modtagname 0 (match-beginning 0)))
3787 (tagname (substring modtagname (match-end 0) nil))
3788 (last-tag tagname)
3789 file)
3790 (while
3791 (progn
3792 (funcall (symbol-function 'find-tag) tagname next-p regexp-p)
3793 (setq next-p t)
3794 ;; Determine the module form the file name. (The
3795 ;; alternative, to check `-module', would make this
3796 ;; code useless for non-Erlang programs.)
3797 (setq file (erlang-get-module-from-file-name buffer-file-name))
3798 (not (and (stringp file)
3799 (string= modname file))))))))
3801 (funcall (symbol-function 'find-tag) modtagname next-p regexp-p)))
3802 (current-buffer))) ; Return the new buffer.
3805 ;; Process interactive arguments for erlang-find-tag-*.
3807 ;; Negative arguments work only for `etags', not `tags'. This is not
3808 ;; a problem since negative arguments means step back into the
3809 ;; history list, a feature not implemented in `tags'.
3811 (defun erlang-tag-interactive (prompt)
3812 (condition-case nil
3813 (require 'etags)
3814 (error
3815 (require 'tags)))
3816 (if current-prefix-arg
3817 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
3820 (let* ((default (erlang-find-tag-default))
3821 (prompt (if default
3822 (format "%s(default %s) " prompt default)
3823 prompt))
3824 (spec (if (featurep 'etags)
3825 (completing-read prompt 'erlang-tags-complete-tag)
3826 (read-string prompt))))
3827 (list (if (equal spec "")
3828 (or default (error "There is no default tag"))
3829 spec)))))
3832 ;; Search tag functions which are aware of Erlang modules. The tactic
3833 ;; is to store new search functions into the local variables of the
3834 ;; TAGS buffers. The variables are restored directly after the
3835 ;; search. The situation is complicated by the fact that new TAGS
3836 ;; files can be loaded during the search.
3839 (defun erlang-tags-install-module-check ()
3840 "Install our own tag search functions."
3841 ;; Make sure our functions are installed in TAGS files loaded
3842 ;; into Emacs while searching.
3843 (cond
3844 ((>= erlang-emacs-major-version 20)
3845 (setq erlang-tags-orig-format-functions
3846 (symbol-value 'tags-table-format-functions))
3847 (funcall (symbol-function 'set) 'tags-table-format-functions
3848 (cons 'erlang-tags-recognize-tags-table
3849 erlang-tags-orig-format-functions))
3850 (setq erlang-tags-buffer-list '())
3853 (setq erlang-tags-orig-format-hooks
3854 (symbol-value 'tags-table-format-hooks)))
3855 (funcall (symbol-function 'set) 'tags-table-format-hooks
3856 (cons 'erlang-tags-recognize-tags-table
3857 erlang-tags-orig-format-hooks))
3858 (setq erlang-tags-buffer-list '())
3861 ;; Install our functions in the TAGS files already resident.
3862 (save-excursion
3863 (let ((files (symbol-value 'tags-table-computed-list)))
3864 (while files
3865 (if (stringp (car files))
3866 (if (get-file-buffer (car files))
3867 (progn
3868 (set-buffer (get-file-buffer (car files)))
3869 (erlang-tags-install-local))))
3870 (setq files (cdr files))))))
3873 (defun erlang-tags-install-local ()
3874 "Install our tag search functions in current buffer."
3875 (if erlang-tags-buffer-installed-p
3877 ;; Mark this buffer as "installed" and record.
3878 (set (make-local-variable 'erlang-tags-buffer-installed-p) t)
3879 (setq erlang-tags-buffer-list
3880 (cons (current-buffer) erlang-tags-buffer-list))
3882 ;; Save the original values.
3883 (set (make-local-variable 'erlang-tags-orig-tag-order)
3884 (symbol-value 'find-tag-tag-order))
3885 (set (make-local-variable 'erlang-tags-orig-regexp-tag-order)
3886 (symbol-value 'find-tag-regexp-tag-order))
3887 (set (make-local-variable 'erlang-tags-orig-search-function)
3888 (symbol-value 'find-tag-search-function))
3889 (set (make-local-variable 'erlang-tags-orig-regexp-search-function)
3890 (symbol-value 'find-tag-regexp-search-function))
3892 ;; Install our own functions.
3893 (set (make-local-variable 'find-tag-search-function)
3894 'erlang-tags-search-forward)
3895 (set (make-local-variable 'find-tag-regexp-search-function)
3896 'erlang-tags-regexp-search-forward)
3897 (set (make-local-variable 'find-tag-tag-order)
3898 '(erlang-tag-match-module-p))
3899 (set (make-local-variable 'find-tag-regexp-tag-order)
3900 '(erlang-tag-match-module-regexp-p))))
3903 (defun erlang-tags-remove-module-check ()
3904 "Remove our own tags search functions."
3905 (cond
3906 ((>= erlang-emacs-major-version 20)
3907 (funcall (symbol-function 'set)
3908 'tags-table-format-functions
3909 erlang-tags-orig-format-functions)
3912 (funcall (symbol-function 'set)
3913 'tags-table-format-hooks
3914 erlang-tags-orig-format-hooks)
3917 ;; Remove our functions from the TAGS files. (Note that
3918 ;; `tags-table-computed-list' need not be the same list as when
3919 ;; the search was started.)
3920 (save-excursion
3921 (let ((buffers erlang-tags-buffer-list))
3922 (while buffers
3923 (if (buffer-name (car buffers))
3924 (progn
3925 (set-buffer (car buffers))
3926 (erlang-tags-remove-local)))
3927 (setq buffers (cdr buffers))))))
3930 (defun erlang-tags-remove-local ()
3931 "Remove our tag search functions from current buffer."
3932 (if (null erlang-tags-buffer-installed-p)
3934 (funcall (symbol-function 'set) 'erlang-tags-buffer-installed-p nil)
3935 (funcall (symbol-function 'set)
3936 'find-tag-tag-order erlang-tags-orig-tag-order)
3937 (funcall (symbol-function 'set)
3938 'find-tag-regexp-tag-order erlang-tags-orig-regexp-tag-order)
3939 (funcall (symbol-function 'set)
3940 'find-tag-search-function erlang-tags-orig-search-function)
3941 (funcall (symbol-function 'set)
3942 'find-tag-regexp-search-function
3943 erlang-tags-orig-regexp-search-function)))
3946 (defun erlang-tags-recognize-tags-table ()
3947 "Install our functions in all loaded TAGS files.
3949 This function is added to `tags-table-format-hooks/functions' when searching
3950 for a tag on the form `module:tag'."
3951 (if (null (funcall (symbol-function 'etags-recognize-tags-table)))
3953 (erlang-tags-install-local)
3957 (defun erlang-tags-search-forward (tag &optional bound noerror count)
3958 "Forward search function, aware of Erlang module prefix."
3959 (if (string-match ":" tag)
3960 (setq tag (substring tag (match-end 0) nil)))
3961 ;; Avoid unintended recursion.
3962 (if (eq erlang-tags-orig-search-function 'erlang-tags-search-forward)
3963 (search-forward tag bound noerror count)
3964 (funcall erlang-tags-orig-search-function tag bound noerror count)))
3967 (defun erlang-tags-regexp-search-forward (tag &optional bound noerror count)
3968 "Forward regexp search function, aware of Erlang module prefix."
3969 (if (string-match ":" tag)
3970 (setq tag (substring tag (match-end 0) nil)))
3971 (if (eq erlang-tags-orig-regexp-search-function
3972 'erlang-tags-regexp-search-forward)
3973 (re-search-forward tag bound noerror count)
3974 (funcall erlang-tags-orig-regexp-search-function
3975 tag bound noerror count)))
3978 ;; t if point is at a tag line that matches TAG, containing
3979 ;; module information. Assumes that all other order functions
3980 ;; are stored in `erlang-tags-orig-[regex]-tag-order'.
3982 (defun erlang-tag-match-module-p (tag)
3983 (erlang-tag-match-module-common-p tag erlang-tags-orig-tag-order))
3985 (defun erlang-tag-match-module-regexp-p (tag)
3986 (erlang-tag-match-module-common-p tag erlang-tags-orig-regexp-tag-order))
3988 (defun erlang-tag-match-module-common-p (tag order)
3989 (let ((mod nil)
3990 (found nil))
3991 (if (string-match ":" tag)
3992 (progn
3993 (setq mod (substring tag 0 (match-beginning 0)))
3994 (setq tag (substring tag (match-end 0) nil))))
3995 (while (and order (not found))
3996 (setq found
3997 (and (not (memq (car order)
3998 '(erlang-tag-match-module-p
3999 erlang-tag-match-module-regexp-p)))
4000 (funcall (car order) tag)))
4001 (setq order (cdr order)))
4002 (and found
4003 (or (null mod)
4004 (string= mod (erlang-get-module-from-file-name
4005 (file-of-tag)))))))
4008 ;;; Tags completion, Emacs 19 `etags' specific.
4010 ;;; The basic idea is to create a second completion table `erlang-tags-
4011 ;;; completion-table' containing all normal tags plus tags on the form
4012 ;;; `module:tag'.
4015 (defun erlang-complete-tag ()
4016 "Perform tags completion on the text around point.
4017 Completes to the set of names listed in the current tags table.
4019 Should the Erlang tags system be installed this command knows
4020 about Erlang modules."
4021 (interactive)
4022 (condition-case nil
4023 (require 'etags)
4024 (error nil))
4025 (cond ((and erlang-tags-installed
4026 (fboundp 'complete-tag)) ; Emacs 19
4027 (let ((orig-tags-complete-tag (symbol-function 'tags-complete-tag)))
4028 (fset 'tags-complete-tag
4029 (symbol-function 'erlang-tags-complete-tag))
4030 (unwind-protect
4031 (funcall (symbol-function 'complete-tag))
4032 (fset 'tags-complete-tag orig-tags-complete-tag))))
4033 ((fboundp 'complete-tag) ; Emacs 19
4034 (funcall (symbol-function 'complete-tag)))
4035 ((fboundp 'tag-complete-symbol) ; XEmacs
4036 (funcall (symbol-function 'tag-complete-symbol)))
4038 (error "This version of Emacs can't complete tags"))))
4041 ;; Based on `tags-complete-tag', but this one uses
4042 ;; `erlang-tags-completion-table' instead of `tags-completion-table'.
4044 ;; This is the entry-point called by system function `completing-read'.
4045 (defun erlang-tags-complete-tag (string predicate what)
4046 (save-excursion
4047 ;; If we need to ask for the tag table, allow that.
4048 (let ((enable-recursive-minibuffers t))
4049 (visit-tags-table-buffer))
4050 (if (eq what t)
4051 (all-completions string (erlang-tags-completion-table) predicate)
4052 (try-completion string (erlang-tags-completion-table) predicate))))
4055 ;; `tags-completion-table' calls itself recursively, make it
4056 ;; call our own wedge instead. Note that the recursive call
4057 ;; is very rare; it only occurs when a tags-file contains
4058 ;; `include'-statements.
4059 (defun erlang-tags-completion-table ()
4060 "Build completion table. Tags on the form `tag' or `module:tag'."
4061 (setq erlang-tags-orig-completion-table
4062 (symbol-function 'tags-completion-table))
4063 (fset 'tags-completion-table
4064 (symbol-function 'erlang-tags-completion-table-1))
4065 (unwind-protect
4066 (erlang-tags-completion-table-1)
4067 (fset 'tags-completion-table
4068 erlang-tags-orig-completion-table)))
4071 (defun erlang-tags-completion-table-1 ()
4072 (make-local-variable 'erlang-tags-completion-table)
4073 (or erlang-tags-completion-table
4074 (let ((tags-completion-table nil)
4075 (tags-completion-table-function
4076 'erlang-etags-tags-completion-table))
4077 (funcall erlang-tags-orig-completion-table)
4078 (setq erlang-tags-completion-table tags-completion-table))))
4081 ;; Based on `etags-tags-completion-table'. The difference is that we
4082 ;; add three symbols to the vector, the tag, module: and module:tag.
4083 ;; The module is extracted from the file name of a tag. (This one
4084 ;; only works if we are looking at an `etags' file. However, this is
4085 ;; the only format supported by Emacs, so far.)
4086 (defun erlang-etags-tags-completion-table ()
4087 (let ((table (make-vector 511 0))
4088 (file nil))
4089 (save-excursion
4090 (goto-char (point-min))
4091 ;; This monster regexp matches an etags tag line.
4092 ;; \1 is the string to match;
4093 ;; \2 is not interesting;
4094 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
4095 ;; \4 is not interesting;
4096 ;; \5 is the explicitly-specified tag name.
4097 ;; \6 is the line to start searching at;
4098 ;; \7 is the char to start searching at.
4099 (while (progn
4100 (while (and
4101 (eq (following-char) ?\f)
4102 (looking-at "\f\n\\([^,\n]*\\),.*\n"))
4103 (setq file (buffer-substring
4104 (match-beginning 1) (match-end 1)))
4105 (goto-char (match-end 0)))
4106 (re-search-forward
4108 ^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
4109 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
4110 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
4111 nil t))
4112 (let ((tag (if (match-beginning 5)
4113 ;; There is an explicit tag name.
4114 (buffer-substring (match-beginning 5) (match-end 5))
4115 ;; No explicit tag name. Best guess.
4116 (buffer-substring (match-beginning 3) (match-end 3))))
4117 (module (and file
4118 (erlang-get-module-from-file-name file))))
4119 (intern tag table)
4120 (if (stringp module)
4121 (progn
4122 (intern (concat module ":" tag) table)
4123 ;; Only the first one will be stored in the table.
4124 (intern (concat module ":") table))))))
4125 table))
4128 ;;; Prepare for other methods to run an Erlang slave process.
4131 (defvar erlang-shell-function 'inferior-erlang
4132 "Command to execute start a new Erlang shell.
4134 Change this variable to use your favorite
4135 Erlang compilation package.")
4137 (defvar erlang-shell-display-function 'inferior-erlang-run-or-select
4138 "Command to execute to display Erlang shell.
4140 Change this variable to use your favorite
4141 Erlang compilation package.")
4143 (defvar erlang-compile-function 'inferior-erlang-compile
4144 "Command to execute to compile current buffer.
4146 Change this variable to use your favorite
4147 Erlang compilation package.")
4149 (defvar erlang-compile-display-function 'inferior-erlang-run-or-select
4150 "Command to execute to view last compilation.
4152 Change this variable to use your favorite
4153 Erlang compilation package.")
4155 (defvar erlang-next-error-function 'inferior-erlang-next-error
4156 "Command to execute to go to the next error.
4158 Change this variable to use your favorite Erlang compilation
4159 package. Not used in Emacs 21.")
4162 ;;;###autoload
4163 (defun erlang-shell ()
4164 "Start a new Erlang shell.
4166 The variable `erlang-shell-function' decides which method to use,
4167 default is to start a new Erlang host. It is possible that, in the
4168 future, a new shell on an already running host will be started."
4169 (interactive)
4170 (call-interactively erlang-shell-function))
4173 ;;;###autoload (autoload 'run-erlang "erlang" "Start a new Erlang shell." t)
4175 ;; It is customary for Emacs packages to supply a function on this
4176 ;; form, even though it violates the `erlang-*' name convention.
4177 (defalias 'run-erlang 'erlang-shell)
4180 (defun erlang-shell-display ()
4181 "Display an Erlang shell, or start a new."
4182 (interactive)
4183 (call-interactively erlang-shell-display-function))
4186 ;;;###autoload
4187 (defun erlang-compile ()
4188 "Compile Erlang module in current buffer."
4189 (interactive)
4190 (call-interactively erlang-compile-function))
4193 (defun erlang-compile-display ()
4194 "Display compilation output."
4195 (interactive)
4196 (call-interactively erlang-compile-display-function))
4199 (defun erlang-next-error ()
4200 "Display next error message from the latest compilation."
4201 (interactive)
4202 (call-interactively erlang-next-error-function))
4207 ;;; Erlang Shell Mode -- Major mode used for Erlang shells.
4210 ;; This mode is designed to be implementation independent,
4211 ;; e.g. it does not assume that we are running an inferior
4212 ;; Erlang, there exists a lot of other possibilities.
4215 (defvar erlang-shell-buffer-name "*erlang*"
4216 "The name of the Erlang link shell buffer.")
4219 (defvar erlang-shell-mode-map nil
4220 "Keymap used by Erlang shells.")
4223 (defvar erlang-shell-mode-hook nil
4224 "*User functions to run when an Erlang shell is started.
4226 This hook is used to change the behaviour of Erlang mode. It is
4227 normally used by the user to personalise the programming environment.
4228 When used in a site init file, it could be used to customise Erlang
4229 mode for all users on the system.
4231 The function added to this hook is run every time a new Erlang
4232 shell is started.
4234 See also `erlang-load-hook', a hook which is run once, when Erlang
4235 mode is loaded, and `erlang-mode-hook' which is run every time a new
4236 Erlang source file is loaded into Emacs.")
4239 (defvar erlang-input-ring-file-name "~/.erlang_history"
4240 "*When non-nil, file name used to store Erlang shell history information.")
4243 (defun erlang-shell-mode ()
4244 "Major mode for interacting with an Erlang shell.
4246 We assume that we already are in Comint mode.
4248 The following special commands are available:
4249 \\{erlang-shell-mode-map}"
4250 (interactive)
4251 (setq major-mode 'erlang-shell-mode)
4252 (setq mode-name "Erlang Shell")
4253 (erlang-mode-variables)
4254 (if erlang-shell-mode-map
4256 (setq erlang-shell-mode-map (copy-keymap comint-mode-map))
4257 (erlang-shell-mode-commands erlang-shell-mode-map))
4258 (use-local-map erlang-shell-mode-map)
4259 (unless inferior-erlang-use-cmm
4260 ;; This was originally not a marker, but it needs to be, at least
4261 ;; in Emacs 21, and should be backwards-compatible. Otherwise,
4262 ;; would need to test whether compilation-parsing-end is a marker
4263 ;; after requiring `compile'.
4264 (set (make-local-variable 'compilation-parsing-end) (copy-marker 1))
4265 (set (make-local-variable 'compilation-error-list) nil)
4266 (set (make-local-variable 'compilation-old-error-list) nil))
4267 ;; Needed when compiling directly from the Erlang shell.
4268 (setq compilation-last-buffer (current-buffer))
4269 (erlang-add-compilation-alist erlang-error-regexp-alist)
4270 (setq comint-prompt-regexp "^[^>=]*> *")
4271 (setq comint-eol-on-send t)
4272 (setq comint-input-ignoredups t)
4273 (setq comint-scroll-show-maximum-output t)
4274 (setq comint-scroll-to-bottom-on-output t)
4275 ;; In Emacs 19.30, `add-hook' has got a `local' flag, use it. If
4276 ;; the call fails, just call the normal `add-hook'.
4277 (condition-case nil
4278 (progn
4279 (make-local-hook 'comint-output-filter-functions) ; obsolete after Emacs 21.3
4280 (add-hook 'comint-output-filter-functions
4281 'inferior-erlang-strip-delete nil t)
4282 (add-hook 'comint-output-filter-functions
4283 'inferior-erlang-strip-ctrl-m nil t))
4284 (error
4285 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-delete)
4286 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-ctrl-m)))
4287 ;; Some older versions of comint don't have an input ring.
4288 (if (fboundp 'comint-read-input-ring)
4289 (progn
4290 (setq comint-input-ring-file-name erlang-input-ring-file-name)
4291 (comint-read-input-ring t)
4292 (make-local-variable 'kill-buffer-hook)
4293 (add-hook 'kill-buffer-hook 'comint-write-input-ring)))
4294 ;; At least in Emacs 21, we need to be in `compilation-minor-mode'
4295 ;; for `next-error' to work. We can avoid it clobbering the shell
4296 ;; keys thus.
4297 (when inferior-erlang-use-cmm
4298 (compilation-minor-mode 1)
4299 (set (make-local-variable 'minor-mode-overriding-map-alist)
4300 `((compilation-minor-mode
4301 . ,(let ((map (make-sparse-keymap)))
4302 ;; It would be useful to put keymap properties on the
4303 ;; error lines so that we could use RET and mouse-2
4304 ;; on them directly.
4305 (when (boundp 'compilation-skip-threshold) ; new compile.el
4306 (define-key map [mouse-2] #'erlang-mouse-2-command)
4307 (define-key map "\C-m" #'erlang-RET-command))
4308 (if (boundp 'compilation-menu-map)
4309 (define-key map [menu-bar compilation]
4310 (cons "Errors" compilation-menu-map)))
4311 map)))))
4312 (run-hooks 'erlang-shell-mode-hook))
4315 (defun erlang-mouse-2-command (event)
4316 "Command bound to `mouse-2' in inferior Erlang buffer.
4317 Selects Comint or Compilation mode command as appropriate."
4318 (interactive "e")
4319 (if (save-window-excursion
4320 (save-excursion
4321 (mouse-set-point event)
4322 (consp (get-text-property (line-beginning-position) 'message))))
4323 (call-interactively (lookup-key compilation-mode-map [mouse-2]))
4324 (call-interactively (lookup-key comint-mode-map [mouse-2]))))
4326 (defun erlang-RET-command ()
4327 "Command bound to `RET' in inferior Erlang buffer.
4328 Selects Comint or Compilation mode command as appropriate."
4329 (interactive)
4330 (if (consp (get-text-property (line-beginning-position) 'message))
4331 (call-interactively (lookup-key compilation-mode-map "\C-m"))
4332 (call-interactively (lookup-key comint-mode-map "\C-m"))))
4334 (defun erlang-shell-mode-commands (map)
4335 (define-key map "\M-\t" 'erlang-complete-tag)
4336 (define-key map "\C-a" 'comint-bol) ; Normally the other way around.
4337 (define-key map "\C-c\C-a" 'beginning-of-line)
4338 (define-key map "\C-d" nil) ; Was `comint-delchar-or-maybe-eof'
4339 (define-key map "\M-\C-m" 'compile-goto-error)
4340 (unless inferior-erlang-use-cmm
4341 (define-key map "\C-x`" 'erlang-next-error)))
4344 ;;; Inferior Erlang -- Run an Erlang shell as a subprocess.
4347 (defvar inferior-erlang-display-buffer-any-frame nil
4348 "*When nil, `inferior-erlang-display-buffer' use only selected frame.
4349 When t, all frames are searched. When 'raise, the frame is raised.")
4351 (defvar inferior-erlang-shell-type 'newshell
4352 "The type of Erlang shell to use.
4354 When this variable is set to the atom `oldshell', the old shell is used.
4355 When set to `newshell' the new shell is used. Should the variable be
4356 nil, the default shell is used.
4358 This variable influence the setting of other variables.")
4360 (defvar inferior-erlang-machine "erl"
4361 "*The name of the Erlang shell.")
4363 (defvar inferior-erlang-machine-options '()
4364 "*The options used when activating the Erlang shell.
4366 This must be a list of strings.")
4368 (defvar inferior-erlang-process-name "inferior-erlang"
4369 "The name of the inferior Erlang process.")
4371 (defvar inferior-erlang-buffer-name erlang-shell-buffer-name
4372 "The name of the inferior Erlang buffer.")
4374 (defvar inferior-erlang-prompt-timeout 60
4375 "*Number of seconds before `inferior-erlang-wait-prompt' timeouts.
4377 The time specified is waited after every output made by the inferior
4378 Erlang shell. When this variable is t, we assume that we always have
4379 a prompt. When nil, we will wait forever, or until \\[keyboard-quit].")
4381 (defvar inferior-erlang-process nil
4382 "Process of last invoked inferior Erlang, or nil.")
4384 (defvar inferior-erlang-buffer nil
4385 "Buffer of last invoked inferior Erlang, or nil.")
4387 ;;;###autoload
4388 (defun inferior-erlang (&optional command)
4389 "Run an inferior Erlang.
4390 With prefix command, prompt for command to start Erlang with.
4392 This is just like running Erlang in a normal shell, except that
4393 an Emacs buffer is used for input and output.
4394 \\<comint-mode-map>
4395 The command line history can be accessed with \\[comint-previous-input] and \\[comint-next-input].
4396 The history is saved between sessions.
4398 Entry to this mode calls the functions in the variables
4399 `comint-mode-hook' and `erlang-shell-mode-hook' with no arguments.
4401 The following commands imitate the usual Unix interrupt and
4402 editing control characters:
4403 \\{erlang-shell-mode-map}"
4404 (interactive
4405 (when current-prefix-arg
4406 (list (read-string "Erlang command: "))))
4407 (require 'comint)
4408 (let (cmd opts)
4409 (if command
4410 (setq cmd "sh"
4411 opts (list "-c" command))
4412 (setq cmd inferior-erlang-machine
4413 opts inferior-erlang-machine-options)
4414 (cond ((eq inferior-erlang-shell-type 'oldshell)
4415 (setq opts (cons "-oldshell" opts)))
4416 ((eq inferior-erlang-shell-type 'newshell)
4417 (setq opts (append '("-newshell" "-env" "TERM" "vt100") opts)))))
4419 (setq inferior-erlang-buffer
4420 (apply 'make-comint
4421 inferior-erlang-process-name cmd
4422 nil opts)))
4423 (setq inferior-erlang-process
4424 (get-buffer-process inferior-erlang-buffer))
4425 (process-kill-without-query inferior-erlang-process)
4426 (switch-to-buffer inferior-erlang-buffer)
4427 (if (and (not (eq system-type 'windows-nt))
4428 (eq inferior-erlang-shell-type 'newshell))
4429 (setq comint-process-echoes t))
4430 ;; `rename-buffer' takes only one argument in Emacs 18.
4431 (condition-case nil
4432 (rename-buffer inferior-erlang-buffer-name t)
4433 (error (rename-buffer inferior-erlang-buffer-name)))
4434 (erlang-shell-mode))
4437 (defun inferior-erlang-run-or-select ()
4438 "Switch to an inferior Erlang buffer, possibly starting new process."
4439 (interactive)
4440 (if (null (inferior-erlang-running-p))
4441 (inferior-erlang)
4442 (inferior-erlang-display-buffer t)))
4445 (defun inferior-erlang-display-buffer (&optional select)
4446 "Make the inferior Erlang process visible.
4447 The window is returned.
4449 Should `inferior-erlang-display-buffer-any-frame' be nil the buffer is
4450 displayed in the current frame. Should it be non-nil, and the buffer
4451 already is visible in any other frame, no new window will be created.
4452 Should it be the atom 'raise, the frame containing the window will
4453 be raised.
4455 Should the optional argument SELECT be non-nil, the window is
4456 selected. Should the window be in another frame, that frame is raised.
4458 Note, should the mouse pointer be places outside the raised frame, that
4459 frame will become deselected before the next command."
4460 (interactive)
4461 (or (inferior-erlang-running-p)
4462 (error "No inferior Erlang process is running"))
4463 (let ((win (inferior-erlang-window
4464 inferior-erlang-display-buffer-any-frame))
4465 (frames-p (fboundp 'selected-frame)))
4466 (if (null win)
4467 (let ((old-win (selected-window)))
4468 (save-excursion
4469 (switch-to-buffer-other-window inferior-erlang-buffer)
4470 (setq win (selected-window)))
4471 (select-window old-win))
4472 (if (and window-system
4473 frames-p
4474 (or select
4475 (eq inferior-erlang-display-buffer-any-frame 'raise))
4476 (not (eq (selected-frame) (window-frame win))))
4477 (raise-frame (window-frame win))))
4478 (if select
4479 (select-window win))
4480 (sit-for 0)
4481 win))
4484 (defun inferior-erlang-running-p ()
4485 "Non-nil when an inferior Erlang is running."
4486 (and inferior-erlang-process
4487 (memq (process-status inferior-erlang-process) '(run open))
4488 inferior-erlang-buffer
4489 (buffer-name inferior-erlang-buffer)))
4492 (defun inferior-erlang-window (&optional all-frames)
4493 "Return the window containing the inferior Erlang, or nil."
4494 (and (inferior-erlang-running-p)
4495 (if (and all-frames (>= erlang-emacs-major-version 19))
4496 (get-buffer-window inferior-erlang-buffer t)
4497 (get-buffer-window inferior-erlang-buffer))))
4500 (defun inferior-erlang-wait-prompt ()
4501 "Wait until the inferior Erlang shell prompt appears."
4502 (if (eq inferior-erlang-prompt-timeout t)
4504 (or (inferior-erlang-running-p)
4505 (error "No inferior Erlang shell is running"))
4506 (save-excursion
4507 (set-buffer inferior-erlang-buffer)
4508 (let ((msg nil))
4509 (while (save-excursion
4510 (goto-char (process-mark inferior-erlang-process))
4511 (forward-line 0)
4512 (not (looking-at comint-prompt-regexp)))
4513 (if msg
4515 (setq msg t)
4516 (message "Waiting for Erlang shell prompt (press C-g to abort)."))
4517 (or (accept-process-output inferior-erlang-process
4518 inferior-erlang-prompt-timeout)
4519 (error "No Erlang shell prompt before timeout")))
4520 (if msg (message ""))))))
4522 (autoload 'comint-send-input "comint")
4524 (defun inferior-erlang-send-command (cmd &optional hist)
4525 "Send command CMD to the inferior Erlang.
4527 The contents of the current command line (if any) will
4528 be placed at the next prompt.
4530 If optional second argument is non-nil the command is inserted into
4531 the history list.
4533 Return the position after the newly inserted command."
4534 (or (inferior-erlang-running-p)
4535 (error "No inferior Erlang process is running"))
4536 (let ((old-buffer (current-buffer))
4537 (insert-point (marker-position (process-mark inferior-erlang-process)))
4538 (insert-length (if comint-process-echoes
4540 (1+ (length cmd)))))
4541 (set-buffer inferior-erlang-buffer)
4542 (goto-char insert-point)
4543 (insert cmd)
4544 ;; Strange things happened if `comint-eol-on-send' is declared
4545 ;; in the `let' expression above, but setq:d here. The
4546 ;; `set-buffer' statement obviously makes the buffer local
4547 ;; instance of `comint-eol-on-send' shadow this one.
4548 ;; I'm considering this a bug in Elisp.
4550 ;; This was previously cautioned against in the Lisp manual. It
4551 ;; has been sorted out in Emacs 21. -- fx
4552 (let ((comint-eol-on-send nil)
4553 (comint-input-filter (if hist comint-input-filter 'ignore)))
4554 (comint-send-input))
4555 ;; Adjust all windows whose points are incorrect.
4556 (if (null comint-process-echoes)
4557 (walk-windows
4558 (function
4559 (lambda (window)
4560 (if (and (eq (window-buffer window) inferior-erlang-buffer)
4561 (= (window-point window) insert-point))
4562 (set-window-point window
4563 (+ insert-point insert-length)))))
4564 nil t))
4565 (set-buffer old-buffer)
4566 (+ insert-point insert-length)))
4569 (defun inferior-erlang-strip-delete (&optional s)
4570 "Remove `^H' (delete) and the characters it was supposed to remove."
4571 (interactive)
4572 (if (and (boundp 'comint-last-input-end)
4573 (boundp 'comint-last-output-start))
4574 (save-excursion
4575 (goto-char
4576 (if (interactive-p)
4577 (symbol-value 'comint-last-input-end)
4578 (symbol-value 'comint-last-output-start)))
4579 (while (progn (skip-chars-forward "^\C-h")
4580 (not (eq (point) (point-max))))
4581 (delete-char 1)
4582 (or (bolp)
4583 (backward-delete-char 1))))))
4586 ;; Basically `comint-strip-ctrl-m', with a few extra checks.
4587 (defun inferior-erlang-strip-ctrl-m (&optional string)
4588 "Strip trailing `^M' characters from the current output group."
4589 (interactive)
4590 (if (and (boundp 'comint-last-input-end)
4591 (boundp 'comint-last-output-start))
4592 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
4593 (save-excursion
4594 (goto-char
4595 (if (interactive-p)
4596 (symbol-value 'comint-last-input-end)
4597 (symbol-value 'comint-last-output-start)))
4598 (while (re-search-forward "\r+$" pmark t)
4599 (replace-match "" t t))))))
4602 (defun inferior-erlang-compile ()
4603 "Compile the file in the current buffer.
4605 Should Erlang return `{error, nofile}' it could not load the object
4606 module after completing the compilation. This is due to a bug in the
4607 compile command `c' when using the option `outdir'.
4609 There exists two workarounds for this bug:
4611 1) Place the directory in the Erlang load path.
4613 2) Set the Emacs variable `erlang-compile-use-outdir' to nil.
4614 To do so, place the following line in your `~/.emacs'-file:
4615 (setq erlang-compile-use-outdir nil)"
4616 (interactive)
4617 (save-some-buffers)
4618 (or (inferior-erlang-running-p)
4619 (save-excursion
4620 (inferior-erlang)))
4621 (or (inferior-erlang-running-p)
4622 (error "Error starting inferior Erlang shell"))
4623 (let ((dir (file-name-directory (buffer-file-name)))
4624 ;;; (file (file-name-nondirectory (buffer-file-name)))
4625 (noext (substring (buffer-file-name) 0 -4))
4626 ;; Hopefully, noone else will ever use these...
4627 (tmpvar "Tmp7236")
4628 (tmpvar2 "Tmp8742")
4629 end)
4630 (inferior-erlang-display-buffer)
4631 (inferior-erlang-wait-prompt)
4632 (setq end (inferior-erlang-send-command
4633 (if erlang-compile-use-outdir
4634 (format "c(\"%s\", [{outdir, \"%s\"}])." noext dir)
4635 (format
4636 (concat
4637 "f(%s), {ok, %s} = file:get_cwd(), "
4638 "file:set_cwd(\"%s\"), "
4639 "%s = c(\"%s\"), file:set_cwd(%s), f(%s), %s.")
4640 tmpvar2 tmpvar
4642 tmpvar2 noext tmpvar tmpvar tmpvar2))
4643 nil))
4644 (inferior-erlang-wait-prompt)
4645 (save-excursion
4646 (set-buffer inferior-erlang-buffer)
4647 (setq compilation-error-list nil)
4648 (set-marker compilation-parsing-end end))
4649 (setq compilation-last-buffer inferior-erlang-buffer)))
4652 ;; `next-error' only accepts buffers with major mode `compilation-mode'
4653 ;; or with the minor mode `compilation-minor-mode' activated.
4654 ;; (To activate the minor mode is out of the question, since it will
4655 ;; ruin the inferior Erlang keymap.)
4656 ;; This is done differently in Emacs 21.
4657 (defun inferior-erlang-next-error (&optional argp)
4658 "Just like `next-error'.
4659 Capable of finding error messages in an inferior Erlang buffer."
4660 (interactive "P")
4661 (let ((done nil)
4662 (buf (and (boundp 'compilation-last-buffer)
4663 compilation-last-buffer)))
4664 (if (and (bufferp buf)
4665 (save-excursion
4666 (set-buffer buf)
4667 (and (eq major-mode 'erlang-shell-mode)
4668 (setq major-mode 'compilation-mode))))
4669 (unwind-protect
4670 (progn
4671 (setq done t)
4672 (next-error argp))
4673 (save-excursion
4674 (set-buffer buf)
4675 (setq major-mode 'erlang-shell-mode))))
4676 (or done
4677 (next-error argp))))
4680 (defun inferior-erlang-change-directory (&optional dir)
4681 "Make the inferior Erlang change directory.
4682 The default is to go to the directory of the current buffer."
4683 (interactive)
4684 (or dir (setq dir (file-name-directory (buffer-file-name))))
4685 (or (inferior-erlang-running-p)
4686 (error "No inferior Erlang is running"))
4687 (inferior-erlang-display-buffer)
4688 (inferior-erlang-wait-prompt)
4689 (inferior-erlang-send-command (format "cd('%s')." dir) nil))
4691 (defun erlang-align-arrows (start end)
4692 "Align arrows (\"->\") in function clauses from START to END.
4693 When called interactively, aligns arrows after function clauses inside
4694 the region.
4696 With a prefix argument, aligns all arrows, not just those in function
4697 clauses.
4699 Example:
4701 sum(L) -> sum(L, 0).
4702 sum([H|T], Sum) -> sum(T, Sum + H);
4703 sum([], Sum) -> Sum.
4705 becomes:
4707 sum(L) -> sum(L, 0).
4708 sum([H|T], Sum) -> sum(T, Sum + H);
4709 sum([], Sum) -> Sum."
4710 (interactive "r")
4711 (save-excursion
4712 (let (;; regexp for matching arrows. without a prefix argument,
4713 ;; the regexp matches function heads. With a prefix, it
4714 ;; matches any arrow.
4715 (re (if current-prefix-arg
4716 "^.*\\(\\)->"
4717 (eval-when-compile
4718 (concat "^" erlang-atom-regexp ".*\\(\\)->"))))
4719 ;; part of regexp matching directly before the arrow
4720 (arrow-match-pos (if current-prefix-arg
4722 (1+ erlang-atom-regexp-matches)))
4723 ;; accumulator for positions where arrows are found, ordered
4724 ;; by buffer position (from greatest to smallest)
4725 (arrow-positions '())
4726 ;; accumulator for longest distance from start of line to arrow
4727 (most-indent 0)
4728 ;; marker to track the end of the region we're aligning
4729 (end-marker (progn (goto-char end)
4730 (point-marker))))
4731 ;; Pass 1: Find the arrow positions, adjust the whitespace
4732 ;; before each arrow to one space, and find the greatest
4733 ;; indentation level.
4734 (goto-char start)
4735 (while (re-search-forward re end-marker t)
4736 (goto-char (match-beginning arrow-match-pos))
4737 (just-one-space) ; adjust whitespace
4738 (setq arrow-positions (cons (point) arrow-positions))
4739 (setq most-indent (max most-indent (erlang-column-number))))
4740 (set-marker end-marker nil) ; free the marker
4741 ;; Pass 2: Insert extra padding so that all arrow indentation is
4742 ;; equal. This is done last-to-first by buffer position, so that
4743 ;; inserting spaces before one arrow doesn't change the
4744 ;; positions of the next ones.
4745 (mapcar (lambda (arrow-pos)
4746 (goto-char arrow-pos)
4747 (let* ((pad (- most-indent (erlang-column-number))))
4748 (when (> pad 0)
4749 (insert-char ?\ pad))))
4750 arrow-positions))))
4752 (defun erlang-column-number ()
4753 "Return the column number of the current position in the buffer.
4754 Tab characters are counted by their visual width."
4755 (string-width (buffer-substring (line-beginning-position) (point))))
4757 (defun erlang-current-defun ()
4758 "`add-log-current-defun-function' for Erlang."
4759 (save-excursion
4760 (erlang-beginning-of-function)
4761 (if (looking-at "[a-z0-9_]+")
4762 (match-string 0))))
4764 ;; Aliases for backward compatibility with older versions of Erlang Mode.
4766 ;; Unfortuantely, older versions of Emacs doesn't have `defalias' and
4767 ;; `make-obsolete' so we have to define our own `obsolete' function.
4769 (defun erlang-obsolete (sym newdef)
4770 "Make the obsolete function SYM refer to the defined function NEWDEF.
4772 Simplified version of a combination `defalias' and `make-obsolete',
4773 it assumes that NEWDEF is loaded."
4774 (defalias sym (symbol-function newdef))
4775 (if (fboundp 'make-obsolete)
4776 (make-obsolete sym newdef)))
4779 (erlang-obsolete 'calculate-erlang-indent 'erlang-calculate-indent)
4780 (erlang-obsolete 'calculate-erlang-stack-indent
4781 'erlang-calculate-stack-indent)
4782 (erlang-obsolete 'at-erlang-keyword 'erlang-at-keyword)
4783 (erlang-obsolete 'at-erlang-operator 'erlang-at-operator)
4784 (erlang-obsolete 'beginning-of-erlang-clause 'erlang-beginning-of-clause)
4785 (erlang-obsolete 'end-of-erlang-clause 'erlang-end-of-clause)
4786 (erlang-obsolete 'mark-erlang-clause 'erlang-mark-clause)
4787 (erlang-obsolete 'beginning-of-erlang-function 'erlang-beginning-of-function)
4788 (erlang-obsolete 'end-of-erlang-function 'erlang-end-of-function)
4789 (erlang-obsolete 'mark-erlang-function 'erlang-mark-function)
4790 (erlang-obsolete 'pass-over-erlang-clause 'erlang-pass-over-function)
4791 (erlang-obsolete 'name-of-erlang-function 'erlang-name-of-function)
4794 ;; Fixme: shouldn't redefine `set-visited-file-name' anyhow -- see above.
4795 (defconst erlang-unload-hook
4796 (list (lambda ()
4797 (defalias 'set-visited-file-name
4798 'erlang-orig-set-visited-file-name)
4799 (when (featurep 'advice)
4800 (ad-unadvise 'Man-notify-when-ready)
4801 (ad-unadvise 'set-visited-file-name)))))
4803 ;; The end...
4805 (provide 'erlang)
4807 (run-hooks 'erlang-load-hook)
4809 ;; Local variables:
4810 ;; coding: iso-8859-1
4811 ;; End:
4813 ;;; erlang.el ends here