Update ChangeLog.
[erlware-mode.git] / erlang.el
blob1fb0d4ced7b917022d0a9eb8cccdb415e4a07d1a
1 ;; erlang.el --- Major modes for editing and running Erlang
3 ;; Copyright (C) 1995-1998,2000 Ericsson Telecom AB
4 ;; Copyright (C) 2004 Free Software Foundation, Inc.
5 ;; Author: Anders Lindgren
6 ;; Version: 2.5.2
7 ;; Keywords: erlang, languages, processes
8 ;; Date: 2000-09-11
10 ;; The contents of this file are subject to the Erlang Public License,
11 ;; Version 1.1, (the "License"); you may not use this file except in
12 ;; compliance with the License. You should have received a copy of the
13 ;; Erlang Public License along with this software. If not, it can be
14 ;; retrieved via the world wide web at http://www.erlang.org/.
16 ;; Software distributed under the License is distributed on an "AS IS"
17 ;; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
18 ;; the License for the specific language governing rights and limitations
19 ;; under the License.
21 ;; The Initial Developer of the Original Code is Ericsson Utvecklings AB.
22 ;; All Rights Reserved.
24 ;; Lars Thorsén's modifications of 2000-06-07 included.
26 ;; The original version of this package was written by Robert Virding.
28 ;; Most skeletons have been written at Ericsson Telecom by
29 ;; magnus@erix.ericsson.se and janne@erix.ericsson.se
31 ;;; Commentary:
33 ;; Introduction:
34 ;; ------------
36 ;; This package provides support for the programming language Erlang.
37 ;; The package provides an editing mode with lots of bells and
38 ;; whistles, compilation support, and it makes it possible for the
39 ;; user to start Erlang shells that run inside Emacs.
41 ;; See the Erlang distribution for full documentation of this package.
43 ;; Installation:
44 ;; ------------
46 ;; Place this file in Emacs load path, byte-compile it, and add the
47 ;; following line to the appropriate init file:
49 ;; (require 'erlang-start)
51 ;; The full documentation contains much more extensive description of
52 ;; the installation procedure.
54 ;; Reporting Bugs:
55 ;; --------------
57 ;; Please send bug reports to the following email address:
58 ;; erlang-bugs@erlang.org
59 ;; or if you have a patch suggestion to:
60 ;; erlang-patches@erlang.org
61 ;; Please state as exactly as possible:
62 ;; - Version number of Erlang Mode (see the menu), Emacs, Erlang,
63 ;; and of any other relevant software.
64 ;; - What the expected result was.
65 ;; - What you did, preferably in a repeatable step-by-step form.
66 ;; - A description of the unexpected result.
67 ;; - Relevant pieces of Erlang code causing the problem.
68 ;; - Personal Emacs customisations, if any.
70 ;; Should the Emacs generate an error, please set the Emacs variable
71 ;; `debug-on-error' to `t'. Repeat the error and enclose the debug
72 ;; information in your bug-report.
74 ;; To set the variable you can use the following command:
75 ;; M-x set-variable RET debug-on-error RET t RET
76 ;;; Code:
78 ;; Variables:
80 (defconst erlang-version "2.5.2"
81 "The version number of Erlang mode.")
83 (defvar erlang-man-root-dir nil
84 "The directory where the Erlang manual pages are installed.
85 The name should not contain the trailing slash.
87 Should this variable be nil, no manual pages will show up in the
88 Erlang mode menu.")
90 (defvar erlang-menu-items '(erlang-menu-base-items
91 erlang-menu-skel-items
92 erlang-menu-shell-items
93 erlang-menu-compile-items
94 erlang-menu-man-items
95 erlang-menu-personal-items
96 erlang-menu-version-items)
97 "*List of menu item list to combine to create Erlang mode menu.
99 External programs which temporarily add menu items to the Erlang mode
100 menu may use this variable. Please use the function `add-hook' to add
101 items.
103 Please call the function `erlang-menu-init' after every change to this
104 variable.")
106 (defvar erlang-menu-base-items
107 '(("Indent"
108 (("Indent Line" erlang-indent-command)
109 ("Indent Region " erlang-indent-region
110 (if erlang-xemacs-p (mark) mark-active))
111 ("Indent Clause" erlang-indent-clause)
112 ("Indent Function" erlang-indent-function)
113 ("Indent Buffer" erlang-indent-current-buffer)))
114 ("Edit"
115 (("Fill Comment" erlang-fill-paragraph)
116 ("Comment Region" comment-region
117 (if erlang-xemacs-p (mark) mark-active))
118 ("Uncomment Region" erlang-uncomment-region
119 (if erlang-xemacs-p (mark) mark-active))
121 ("Beginning of Function" erlang-beginning-of-function)
122 ("End of Function" erlang-end-of-function)
123 ("Mark Function" erlang-mark-function)
125 ("Beginning of Clause" erlang-beginning-of-clause)
126 ("End of Clause" erlang-end-of-clause)
127 ("Mark Clause" erlang-mark-clause)
129 ("New Clause" erlang-generate-new-clause)
130 ("Clone Arguments" erlang-clone-arguments)
132 ("Align Arrows" erlang-align-arrows)))
133 ("Syntax Highlighting"
134 (("Level 3" erlang-font-lock-level-3)
135 ("Level 2" erlang-font-lock-level-2)
136 ("Level 1" erlang-font-lock-level-1)
137 ("Off" erlang-font-lock-level-0)))
138 ("TAGS"
139 (("Find Tag" find-tag)
140 ("Find Next Tag" erlang-find-next-tag)
141 ("Complete Word" erlang-complete-tag)
142 ("Tags Apropos" tags-apropos)
143 ("Search Files" tags-search))))
144 "Description of menu used in Erlang mode.
146 This variable must be a list. The elements are either nil representing
147 a horizontal line or a list with two or three elements. The first is
148 the name of the menu item, the second is the function to call, or a
149 submenu, on the same same form as ITEMS. The third optional argument
150 is an expression which is evaluated every time the menu is displayed.
151 Should the expression evaluate to nil the menu item is ghosted.
153 Example:
154 '((\"Func1\" function-one)
155 (\"SubItem\"
156 ((\"Yellow\" function-yellow)
157 (\"Blue\" function-blue)))
159 (\"Region Function\" spook-function midnight-variable))
161 Call the function `erlang-menu-init' after modifying this variable.")
163 (defvar erlang-menu-shell-items
164 '(nil
165 ("Shell"
166 (("Start New Shell" erlang-shell)
167 ("Display Shell" erlang-shell-display))))
168 "Description of the Shell menu used by Erlang mode.
170 Please see the documentation of `erlang-menu-base-items'.")
172 (defvar erlang-menu-compile-items
173 '(("Compile"
174 (("Compile Buffer" erlang-compile)
175 ("Display Result" erlang-compile-display)
176 ("Next Error" erlang-next-error))))
177 "Description of the Compile menu used by Erlang mode.
179 Please see the documentation of `erlang-menu-base-items'.")
181 (defvar erlang-menu-version-items
182 '(nil
183 ("Version" erlang-version))
184 "Description of the version menu used in Erlang mode.")
186 (defvar erlang-menu-personal-items nil
187 "Description of personal menu items used in Erlang mode.
189 Please see the variable `erlang-menu-base-items' for a description
190 of the format.")
192 (defvar erlang-menu-man-items nil
193 "The menu containing man pages.
195 The format of the menu should be compatible with `erlang-menu-base-items'.
196 This variable is added to the list of Erlang menus stored in
197 `erlang-menu-items'.")
199 (defvar erlang-menu-skel-items '()
200 "Description of the menu containing the skeleton entries.
201 The menu is in the form described by the variable `erlang-menu-base-items'.")
203 (defvar erlang-mode-hook nil
204 "*Functions to run when Erlang mode is activated.
206 This hook is used to change the behaviour of Erlang mode. It is
207 normally used by the user to personalise the programming environment.
208 When used in a site init file, it could be used to customise Erlang
209 mode for all users on the system.
211 The functions added to this hook are run every time Erlang mode is
212 started. See also `erlang-load-hook', a hook which is run once,
213 when Erlang mode is loaded into Emacs, and `erlang-shell-mode-hook'
214 which is run every time a new inferior Erlang shell is started.
216 To use a hook, create an Emacs lisp function to perform your actions
217 and add the function to the hook by calling `add-hook'.
219 The following example binds the key sequence C-c C-c to the command
220 `erlang-compile' (normally bound to C-c C-k). The example also
221 activates Font Lock mode to fontify the buffer and adds a menu
222 containing all functions defined in the current buffer.
224 To use the example, copy the following lines to your `~/.emacs' file:
226 (add-hook 'erlang-mode-hook 'my-erlang-mode-hook)
228 (defun my-erlang-mode-hook ()
229 (local-set-key \"\\C-c\\C-c\" 'erlang-compile)
230 (if window-system
231 (progn
232 (setq font-lock-maximum-decoration t)
233 (font-lock-mode 1)))
234 (if (and window-system (fboundp 'imenu-add-to-menubar))
235 (imenu-add-to-menubar \"Imenu\")))")
237 (defvar erlang-load-hook nil
238 "*Functions to run when Erlang mode is loaded.
240 This hook is used to change the behaviour of Erlang mode. It is
241 normally used by the user to personalise the programming environment.
242 When used in a site init file, it could be used to customize Erlang
243 mode for all users on the system.
245 The difference between this hook and `erlang-mode-hook' and
246 `erlang-shell-mode-hook' is that the functions in this hook
247 is only called once, when the Erlang mode is loaded into Emacs
248 the first time.
250 Natural actions for the functions added to this hook are actions which
251 only should be performed once, and actions which should be performed
252 before starting Erlang mode. For example, a number of variables are
253 used by Erlang mode before `erlang-mode-hook' is run.
255 The following example sets the variable `erlang-man-root-dir' so that
256 the manual pages can be retrieved (note that you must set the value of
257 `erlang-man-root-dir' to match the location of the Erlang man pages
258 on your system):
260 (setq erlang-man-root-dir \"/usr/local/erlang\")")
262 (defvar erlang-new-file-hook nil
263 "Functions to run when a new Erlang source file is being edited.
265 A useful function is `tempo-template-erlang-normal-header'.
266 \(This function only exists when the `tempo' package is available.)")
268 (defvar erlang-check-module-name 'ask
269 "*Non-nil means check that module name and file name agrees when saving.
271 If the value of this variable is the atom `ask', the user is
272 prompted. If the value is t the source is silently changed.")
274 (defvar erlang-electric-commands
275 '(erlang-electric-comma
276 erlang-electric-semicolon
277 erlang-electric-newline
278 erlang-electric-gt)
279 "*List of activated electric commands.
281 The list should contain the electric commands which should be active.
282 Currently, the available electric commands are:
283 `erlang-electric-comma'
284 `erlang-electric-semicolon'
285 `erlang-electric-gt'
286 `erlang-electric-newline'
288 Should the variable be bound to t, all electric commands
289 are activated.
291 To deactivate all electric commands, set this variable to nil.")
293 (defvar erlang-electric-newline-inhibit t
294 "*Set to non-nil to inhibit newline after electric command.
296 This is useful since a lot of people press return after executing an
297 electric command.
299 In order to work, the command must also be in the
300 list `erlang-electric-newline-inhibit-list'.
302 Note that commands in this list are required to set the variable
303 `erlang-electric-newline-inhibit' to nil when the newline shouldn't be
304 inhibited.")
306 (defvar erlang-electric-newline-inhibit-list
307 '(erlang-electric-semicolon
308 erlang-electric-comma
309 erlang-electric-gt)
310 "*Commands which can inhibit the next newline.")
312 (defvar erlang-electric-semicolon-insert-blank-lines nil
313 "*Number of blank lines inserted before header, or nil.
315 This variable controls the behaviour of `erlang-electric-semicolon'
316 when a new function header is generated. When nil, no blank line is
317 inserted between the current line and the new header. When bound to a
318 number it represents the number of blank lines which should be
319 inserted.")
321 (defvar erlang-electric-semicolon-criteria
322 '(erlang-next-lines-empty-p
323 erlang-at-keyword-end-p
324 erlang-at-end-of-function-p)
325 "*List of functions controlling `erlang-electric-semicolon'.
326 The functions in this list are called, in order, whenever a semicolon
327 is typed. Each function in the list is called with no arguments,
328 and should return one of the following values:
330 nil -- no determination made, continue checking
331 'stop -- do not create prototype for next line
332 (anything else) -- insert prototype, and stop checking
334 If every function in the list is called with no determination made,
335 then no prototype is inserted.
337 The test is performed by the function `erlang-test-criteria-list'.")
339 (defvar erlang-electric-comma-criteria
340 '(erlang-stop-when-inside-argument-list
341 erlang-stop-when-at-guard
342 erlang-next-lines-empty-p
343 erlang-at-keyword-end-p
344 erlang-at-end-of-clause-p
345 erlang-at-end-of-function-p)
346 "*List of functions controlling `erlang-electric-comma'.
347 The functions in this list are called, in order, whenever a comma
348 is typed. Each function in the list is called with no arguments,
349 and should return one of the following values:
351 nil -- no determination made, continue checking
352 'stop -- do not create prototype for next line
353 (anything else) -- insert prototype, and stop checking
355 If every function in the list is called with no determination made,
356 then no prototype is inserted.
358 The test is performed by the function `erlang-test-criteria-list'.")
360 (defvar erlang-electric-arrow-criteria
361 '(erlang-next-lines-empty-p
362 erlang-at-end-of-function-p)
363 "*List of functions controlling the arrow aspect of `erlang-electric-gt'.
364 The functions in this list are called, in order, whenever a `>'
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-newline-criteria
378 '(t)
379 "*List of functions controlling `erlang-electric-newline'.
381 The electric newline commands indents the next line. Should the
382 current line begin with a comment the comment start is copied to
383 the newly created line.
385 The functions in this list are called, in order, whenever a comma
386 is typed. Each function in the list is called with no arguments,
387 and should return one of the following values:
389 nil -- no determination made, continue checking
390 'stop -- do not create prototype for next line
391 (anything else) -- trigger the electric command.
393 If every function in the list is called with no determination made,
394 then no prototype is inserted. Should the atom t be a member of the
395 list, it is treated as a function triggering the electric command.
397 The test is performed by the function `erlang-test-criteria-list'.")
399 (defvar erlang-next-lines-empty-threshold 2
400 "*Number of blank lines required to activate an electric command.
402 Actually, this value controls the behaviour of the function
403 `erlang-next-lines-empty-p' which normally is a member of the
404 criteria lists controlling the electric commands. (Please see
405 the variables `erlang-electric-semicolon-criteria' and
406 `erlang-electric-comma-criteria'.)
408 The variable is bound to a threshold value, a number, representing the
409 number of lines which must be empty.
411 Setting this variable to zero, electric commands will always be
412 triggered by `erlang-next-lines-empty-p', unless inhibited by other
413 rules.
415 Should this variable be nil, `erlang-next-lines-empty-p' will never
416 trigger an electric command. The same effect would be reached if the
417 function `erlang-next-lines-empty-p' would be removed from the criteria
418 lists.
420 Note that even if `erlang-next-lines-empty-p' should not trigger an
421 electric command, other functions in the criteria list could.")
423 (defvar erlang-new-clause-with-arguments nil
424 "*Non-nil means that the arguments are cloned when a clause is generated.
426 A new function header can be generated by calls to the function
427 `erlang-generate-new-clause' and by use of the electric semicolon.")
429 (defvar erlang-compile-use-outdir t
430 "*When nil, go to the directory containing source file when compiling.
432 This is a workaround for a bug in the `outdir' option of compile. If the
433 outdir is not in the current load path, Erlang doesn't load the object
434 module after it has been compiled.
436 To activate the workaround, place the following in your `~/.emacs' file:
437 (setq erlang-compile-use-outdir nil)")
439 (defvar erlang-indent-level 4
440 "*Indentation of Erlang calls/clauses within blocks.")
442 (defvar erlang-indent-guard 2
443 "*Indentation of Erlang guards.")
445 (defvar erlang-argument-indent 2
446 "*Indentation of the first argument in a function call.
447 When nil, indent to the column after the `(' of the
448 function.")
450 (defvar erlang-tab-always-indent t
451 "*Non-nil means TAB in Erlang mode should always re-indent the current line,
452 regardless of where in the line point is when the TAB command is used.")
454 (defvar erlang-error-regexp-alist
455 '(("^\\([^:( \t\n]+\\)[:(][ \t]*\\([0-9]+\\)[:) \t]" . (1 2)))
456 "*Patterns for matching Erlang errors.")
458 (defvar erlang-man-inhibit (eq system-type 'windows-nt)
459 "Inhibit the creation of the Erlang Manual Pages menu.
461 The Windows distribution of Erlang does not include man pages, hence
462 there is no attempt to create the menu.")
464 (defvar erlang-man-dirs
465 '(("Man - Commands" "/man/man1" t)
466 ("Man - Modules" "/man/man3" t)
467 ("Man - Files" "/man/man4" t)
468 ("Man - Applications" "/man/man6" t))
469 "*The man directories displayed in the Erlang menu.
471 Each item in the list should be a list with three elements, the first
472 the name of the menu, the second the directory, and the last a flag.
473 Should the flag the nil, the directory is absolute, should it be non-nil
474 the directory is relative to the variable `erlang-man-root-dir'.")
476 (defvar erlang-man-max-menu-size 20
477 "*The maximum number of menu items in one menu allowed.")
479 (defvar erlang-man-display-function 'erlang-man-display
480 "*Function used to display man page.
482 The function is called with one argument, the name of the file
483 containing the man page. Use this variable when the default
484 function, `erlang-man-display', does not work on your system.")
486 (eval-and-compile
487 (defconst erlang-atom-regexp "\\([a-z][a-zA-Z0-9_]*\\|'[^\n']*[^\\]'\\)"
488 "Regexp which should match an Erlang atom.
490 The regexp must be surrounded with a pair of regexp parentheses."))
491 (defconst erlang-atom-regexp-matches 1
492 "Number of regexp parenthesis pairs in `erlang-atom-regexp'.
494 This is used to determine parenthesis matches in complex regexps which
495 contains `erlang-atom-regexp'.")
497 (defconst erlang-variable-regexp "\\([A-Z_][a-zA-Z0-9_]*\\)"
498 "Regexp which should match an Erlang variable.
500 The regexp must be surrounded with a pair of regexp parentheses.")
501 (defconst erlang-variable-regexp-matches 1
502 "Number of regexp parenthesis pairs in `erlang-variable-regexp'.
504 This is used to determine matches in complex regexps which contains
505 `erlang-variable-regexp'.")
507 (defvar erlang-defun-prompt-regexp (concat "^" erlang-atom-regexp "\\s *(")
508 "Regexp which should match beginning of a clause.")
510 (defvar erlang-file-name-extension-regexp "\\.[eh]rl$"
511 "*Regexp which should match an Erlang file name.
513 This regexp is used when an Erlang module name is extracted from the
514 name of an Erlang source file.
516 The regexp should only match the section of the file name which should
517 be excluded from the module name.
519 To match all files set this variable to \"\\\\(\\\\..*\\\\|\\\\)$\".
520 The matches all except the extension. This is useful if the Erlang
521 tags system should interpret tags on the form `module:tag' for
522 files written in other languages than Erlang.")
524 (defvar erlang-mode-map nil
525 "*Keymap used in Erlang mode.")
526 (defvar erlang-mode-abbrev-table nil
527 "Abbrev table in use in Erlang-mode buffers.")
528 (defvar erlang-mode-syntax-table nil
529 "Syntax table in use in Erlang-mode buffers.")
531 (defconst erlang-emacs-major-version
532 (if (boundp 'emacs-major-version)
533 emacs-major-version
534 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version)
535 (string-to-int (substring emacs-version
536 (match-beginning 1) (match-end 1))))
537 "Major version number of Emacs.")
539 (defconst erlang-emacs-minor-version
540 (if (boundp 'emacs-minor-version)
541 emacs-minor-version
542 (string-match "\\([0-9]+\\)\\.\\([0-9]+\\)" emacs-version)
543 (string-to-int (substring emacs-version
544 (match-beginning 2) (match-end 2))))
545 "Minor version number of Emacs.")
547 (defconst erlang-xemacs-p (string-match "Lucid\\|XEmacs" emacs-version)
548 "Non-nil when running under XEmacs or Lucid Emacs.")
550 (defvar erlang-xemacs-popup-menu '("Erlang Mode Commands" . nil)
551 "Common popup menu for all buffers in Erlang mode.
553 This variable is destructively modified every time the Erlang menu
554 is modified. The effect is that all changes take effect in all
555 buffers in Erlang mode, just like under GNU Emacs.
557 Never EVER set this variable!")
559 (defconst inferior-erlang-use-cmm (boundp 'minor-mode-overriding-map-alist)
560 "Non-nil means use `compilation-minor-mode' in Erlang shell.")
562 ;; Tempo skeleton templates:
563 (load "erlang-skels")
565 ;; Sinan commands:
566 (load "erlang-sinan")
569 ;; Font-lock variables
571 (defvar erlang-font-lock-modern-p
572 (cond ((>= erlang-emacs-major-version 20) t)
573 (erlang-xemacs-p (>= erlang-emacs-minor-version 14))
574 ((= erlang-emacs-major-version 19) (>= erlang-emacs-minor-version 29))
575 (t nil))
576 "Non-nil when this version of Emacs uses a modern version of Font Lock.
578 This is determined by checking the version of Emacs used, the actual
579 font-lock code is not loaded.")
582 ;; The next few variables define different Erlang font-lock patterns.
583 ;; They could be appended to form a custom font-lock appearance.
585 ;; The function `erlang-font-lock-set-face' could be used to change
586 ;; the face of a pattern.
588 ;; Note that Erlang strings and atoms are highlighted with using
589 ;; syntactic analysis.
591 (defvar erlang-font-lock-keywords-func
592 (list
593 (list (concat "^" erlang-atom-regexp "\\s *(")
594 1 'font-lock-function-name-face t))
595 "Font lock keyword highlighting a function header.")
597 (defvar erlang-font-lock-keywords-dollar
598 (list
599 (list "\\(\\$\\([^\\]\\|\\\\\\([^0-7^\n]\\|[0-7]+\\|\\^[a-zA-Z]\\)\\)\\)"
600 1 'font-lock-string-face))
601 "Font lock keyword highlighting numbers in ASCII form (e.g. $A).")
603 (defvar erlang-font-lock-keywords-lc
604 (list
605 (list "\\(<-\\)\\(\\s \\|$\\)" 1 'font-lock-keyword-face)
606 (list "\\(||\\)\\(\\s \\|$\\)" 1 'font-lock-keyword-face))
607 "Font lock keyword highlighting list comprehension operators.")
609 (defvar erlang-font-lock-keywords-keywords
610 (list
611 (list (concat "\\<\\(a\\(fter\\|ndalso\\)\\|begin\\|c\\(atch\\|ase\\)"
612 "\\|end\\|fun\\|if\\|o\\(f\\|relse\\)\\|receive\\|try\\|when"
613 "\\|query\\)\\([^a-zA-Z0-9_]\\|$\\)")
614 1 'font-lock-keyword-face))
615 "Font lock keyword highlighting Erlang keywords.")
617 (defvar erlang-font-lock-keywords-attr
618 (list
619 (list (concat "^\\(-" erlang-atom-regexp "\\)\\s *\\(\\.\\|(\\)")
620 1 'font-lock-function-name-face))
621 "Font lock keyword highlighting attributes.")
623 (defvar erlang-font-lock-keywords-quotes
624 (list
625 (list "`\\([-+a-zA-Z0-9_:*][-+a-zA-Z0-9_:*]+\\)'"
627 (if erlang-font-lock-modern-p
628 'font-lock-reference-face
629 'font-lock-keyword-face)
631 "Font lock keyword highlighting words in single quotes in comments.
633 This is not the highlighting of Erlang strings and atoms, which
634 are highlighted by syntactic analysis.")
636 ;; Note: The guard `float' collides with the bif `float'.
637 (defvar erlang-font-lock-keywords-guards
638 (list
639 (list
640 (concat "\\<\\("
641 "\\(is_\\)*\\(atom\\|boolean\\|function\\|binary\\|constant"
642 "\\|float\\|integer\\|list\\|number\\|p\\(id\\|ort\\)\\|"
643 "re\\(ference\\|cord\\)\\|tuple"
644 "\\)\\)\\s *(")
647 (if erlang-font-lock-modern-p
648 'font-lock-reference-face
649 'font-lock-keyword-face)))
650 "Font lock keyword highlighting guards.")
652 (defvar erlang-font-lock-keywords-bifs
653 (list
654 (list
655 (concat
656 "\\<\\("
657 "a\\(bs\\|live\\|pply\\|tom_to_list\\)\\|"
658 "binary_to_\\(list\\|term\\)\\|"
659 "concat_binary\\|d\\(ate\\|isconnect_node\\)\\|"
660 "e\\(lement\\|rase\\|xit\\)\\|"
661 "float\\(\\|_to_list\\)\\|"
662 "g\\(arbage_collect\\|et\\(\\|_keys\\)\\|roup_leader\\)\\|"
663 "h\\(alt\\|d\\)\\|"
664 "i\\(nte\\(ger_to_list\\|rnal_bif\\)\\|s_alive\\)\\|"
665 "l\\(ength\\|i\\(nk\\|st_to_\\(atom\\|binary\\|float\\|integer"
666 "\\|pid\\|tuple\\)\\)\\)\\|"
667 "make_ref\\|no\\(de\\(\\|_\\(link\\|unlink\\)\\|s\\)\\|talive\\)\\|"
668 "open_port\\|"
669 "p\\(id_to_list\\|rocess\\(_\\(flag\\|info\\)\\|es\\)\\|ut\\)\\|"
670 "r\\(egister\\(\\|ed\\)\\|ound\\)\\|"
671 "s\\(e\\(lf\\|telement\\)\\|ize\\|"
672 "p\\(awn\\(\\|_link\\)\\|lit_binary\\)\\|tatistics\\)\\|"
673 "t\\(erm_to_binary\\|hrow\\|ime\\|l\\|"
674 "r\\(ace\\|unc\\)\\|uple_to_list\\)\\|"
675 "un\\(link\\|register\\)\\|whereis"
676 "\\)\\s *(")
678 'font-lock-keyword-face))
679 "Font lock keyword highlighting built in functions.")
681 (defvar erlang-font-lock-keywords-macros
682 (list
683 (list (concat "?\\s *\\(" erlang-atom-regexp
684 "\\|" erlang-variable-regexp "\\)\\>")
685 1 (if erlang-font-lock-modern-p
686 'font-lock-reference-face
687 'font-lock-type-face))
688 (list (concat "^-\\(define\\|ifn?def\\)\\s *(\\s *\\(" erlang-atom-regexp
689 "\\|" erlang-variable-regexp "\\)\\>")
690 2 (if erlang-font-lock-modern-p
691 'font-lock-reference-face
692 'font-lock-type-face)))
693 "Font lock keyword highlighting macros.
694 This must be placed in front of `erlang-font-lock-keywords-vars'.")
696 (defvar erlang-font-lock-keywords-records
697 (list
698 (list (concat "#\\s *" erlang-atom-regexp "\\>")
699 1 'font-lock-type-face)
700 ;; Don't highlight numerical constants.
701 (list "\\<[0-9][0-9]?#\\([0-9a-fA_F]+\\)\\>"
702 1 nil t)
703 (list (concat "^-record(\\s *" erlang-atom-regexp "\\>")
704 1 'font-lock-type-face))
705 "Font lock keyword highlighting Erlang records.
706 This must be placed in front of `erlang-font-lock-keywords-vars'.")
708 (defvar erlang-font-lock-keywords-vars
709 (list
710 (list (concat "\\<" erlang-variable-regexp "\\>")
711 1 (if erlang-font-lock-modern-p
712 'font-lock-variable-name-face
713 'font-lock-type-face)))
714 "Font lock keyword highlighting Erlang variables.
715 Must be preceded by `erlang-font-lock-keywords-macros' and `-records'
716 to work properly.")
719 (defvar erlang-font-lock-keywords-1
720 (append erlang-font-lock-keywords-func
721 erlang-font-lock-keywords-dollar
722 erlang-font-lock-keywords-keywords)
723 ;; DocStringOrig: erlang-font-lock-keywords
724 "Font-lock keywords used by Erlang Mode.
726 There exists three levels of Font Lock keywords for Erlang:
727 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
728 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
729 `erlang-font-lock-keywords-3' - Variables, macros and records.
731 To use a specific level, please set the variable
732 `font-lock-maximum-decoration' to the appropriate level. Note that the
733 variable must be set before Erlang mode is activated.
735 Example:
736 (setq font-lock-maximum-decoration 2)")
739 (defvar erlang-font-lock-keywords-2
740 (append erlang-font-lock-keywords-1
741 erlang-font-lock-keywords-attr
742 erlang-font-lock-keywords-quotes
743 erlang-font-lock-keywords-guards
744 erlang-font-lock-keywords-bifs)
745 ;; DocStringCopy: erlang-font-lock-keywords
746 "Font-lock keywords used by Erlang Mode.
748 There exists three levels of Font Lock keywords for Erlang:
749 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
750 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
751 `erlang-font-lock-keywords-3' - Variables, macros and records.
753 To use a specific level, please set the variable
754 `font-lock-maximum-decoration' to the appropriate level. Note that the
755 variable must be set before Erlang mode is activated.
757 Example:
758 (setq font-lock-maximum-decoration 2)")
761 (defvar erlang-font-lock-keywords-3
762 (append erlang-font-lock-keywords-2
763 erlang-font-lock-keywords-macros
764 erlang-font-lock-keywords-records
765 erlang-font-lock-keywords-vars)
766 ;; DocStringCopy: erlang-font-lock-keywords
767 "Font-lock keywords used by Erlang Mode.
769 There exists three levels of Font Lock keywords for Erlang:
770 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
771 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
772 `erlang-font-lock-keywords-3' - Variables, macros and records.
774 To use a specific level, please set the variable
775 `font-lock-maximum-decoration' to the appropriate level. Note that the
776 variable must be set before Erlang mode is activated.
778 Example:
779 (setq font-lock-maximum-decoration 2)")
782 (defvar erlang-font-lock-keywords erlang-font-lock-keywords-3
783 ;; DocStringCopy: erlang-font-lock-keywords
784 "Font-lock keywords used by Erlang Mode.
786 There exists three levels of Font Lock keywords for Erlang:
787 `erlang-font-lock-keywords-1' - Function headers and reserved keywords.
788 `erlang-font-lock-keywords-2' - Bifs, guards and `single quotes'.
789 `erlang-font-lock-keywords-3' - Variables, macros and records.
791 To use a specific level, please set the variable
792 `font-lock-maximum-decoration' to the appropriate level. Note that the
793 variable must be set before Erlang mode is activated.
795 Example:
796 (setq font-lock-maximum-decoration 2)")
799 (defvar erlang-font-lock-syntax-table nil
800 "Syntax table used by Font Lock mode.
802 The difference between this and the standard Erlang Mode
803 syntax table is that `_' is treated as part of words by
804 this syntax table.
806 Unfortunately, XEmacs hasn't got support for a special Font
807 Lock syntax table. The effect is that `apply' in the atom
808 `foo_apply' will be highlighted as a bif.")
811 ;;; Avoid errors while compiling this file.
813 ;; `eval-when-compile' is not defined in Emacs 18. We define it as a
814 ;; no-op.
815 (or (fboundp 'eval-when-compile)
816 (defmacro eval-when-compile (&rest rest) nil))
818 ;; These umm...functions are new in Emacs 20. And, yes, until version
819 ;; 19.27 Emacs backquotes were this ugly.
821 (or (fboundp 'unless)
822 (defmacro unless (condition &rest body)
823 "(unless CONDITION BODY...): If CONDITION is false, do BODY, else return nil."
824 (` (if (, condition)
826 (,@ body)))))
828 (or (fboundp 'when)
829 (defmacro when (condition &rest body)
830 "(when CONDITION BODY...): If CONDITION is true, do BODY, else return nil."
831 (` (if (, condition)
832 (progn (,@ body))
833 nil))))
835 (or (fboundp 'char-before)
836 (defmacro char-before (&optional pos)
837 "Return the character in the current buffer just before POS."
838 (` (char-after (1- (or (, pos) (point)))))))
840 (eval-when-compile
841 (if (or (featurep 'bytecomp)
842 (featurep 'byte-compile))
843 (progn
844 (cond ((string-match "Lucid\\|XEmacs" emacs-version)
845 (put 'comment-indent-hook 'byte-obsolete-variable nil)
846 ;; Do not warn for unused variables
847 ;; when compiling under XEmacs.
848 (setq byte-compile-warnings
849 '(free-vars unresolved callargs redefine))))
850 (require 'comint)
851 (require 'compile))))
854 (defun erlang-version ()
855 "Return the current version of Erlang mode."
856 (interactive)
857 (if (interactive-p)
858 (message "Erlang mode version %s, written by Anders Lindgren"
859 erlang-version))
860 erlang-version)
863 ;;;###autoload
864 (defun erlang-mode ()
865 "Major mode for editing Erlang source files in Emacs.
866 It knows about syntax and comment, it can indent code, it is capable
867 of fontifying the source file, the TAGS commands are aware of Erlang
868 modules, and the Erlang man pages can be accessed.
870 Should this module, \"erlang.el\", be installed properly, Erlang mode
871 is activated whenever an Erlang source or header file is loaded into
872 Emacs. To indicate this, the mode line should contain the word
873 \"Erlang\".
875 The main feature of Erlang mode is indentation, press TAB and the
876 current line will be indented correctly.
878 Comments starting with one `%' are indented with the same indentation
879 as code. Comments starting with at least two `%':s are indented to
880 the first column.
882 However, Erlang mode contains much more, this is a list of the most
883 useful commands:
884 TAB - Indent the line.
885 C-c C-q - Indent current function.
886 M-; - Create a comment at the end of the line.
887 M-q - Fill a comment, i.e. wrap lines so that they (hopefully)
888 will look better.
889 M-a - Goto the beginning of an Erlang clause.
890 M-C-a - Ditto for function.
891 M-e - Goto the end of an Erlang clause.
892 M-C-e - Ditto for function.
893 M-h - Mark current Erlang clause.
894 M-C-h - Ditto for function.
895 C-c C-z - Start, or switch to, an inferior Erlang shell.
896 C-c C-k - Compile current file.
897 C-x ` - Next error.
898 , - Electric comma.
899 ; - Electric semicolon.
901 Erlang mode check the name of the file against the module name when
902 saving, whenever a mismatch occurs Erlang mode offers to modify the
903 source.
905 The variable `erlang-electric-commands' controls the electric
906 commands. To deactivate all of them, set it to nil.
908 There exists a large number of commands and variables in the Erlang
909 module. Please press `M-x apropos RET erlang RET' to see a complete
910 list. Press `C-h f name-of-function RET' and `C-h v name-of-variable
911 RET'to see the full description of functions and variables,
912 respectively.
914 On entry to this mode the contents of the hook `erlang-mode-hook' is
915 executed.
917 Please see the beginning of the file `erlang.el' for more information
918 and examples of hooks.
920 Other commands:
921 \\{erlang-mode-map}"
922 (interactive)
923 (kill-all-local-variables)
924 (setq major-mode 'erlang-mode)
925 (setq mode-name "Erlang")
926 (erlang-syntax-table-init)
927 (erlang-keymap-init)
928 (erlang-electric-init)
929 (erlang-menu-init)
930 (erlang-mode-variables)
931 (erlang-check-module-name-init)
932 (erlang-add-compilation-alist erlang-error-regexp-alist)
933 (erlang-man-init)
934 (erlang-tags-init)
935 (erlang-font-lock-init)
936 (erlang-skel-init)
937 (run-hooks 'erlang-mode-hook)
938 (if (zerop (buffer-size))
939 (run-hooks 'erlang-new-file-hook)))
942 (defun erlang-syntax-table-init ()
943 (if (null erlang-mode-syntax-table)
944 (let ((table (make-syntax-table)))
945 (modify-syntax-entry ?\n ">" table)
946 (modify-syntax-entry ?\" "\"" table)
947 (modify-syntax-entry ?# "." table)
948 (modify-syntax-entry ?$ "/" table)
949 (modify-syntax-entry ?% "<" table)
950 (modify-syntax-entry ?& "." table)
951 (modify-syntax-entry ?\' "\"" table)
952 (modify-syntax-entry ?* "." table)
953 (modify-syntax-entry ?+ "." table)
954 (modify-syntax-entry ?- "." table)
955 (modify-syntax-entry ?/ "." table)
956 (modify-syntax-entry ?: "." table)
957 (modify-syntax-entry ?< "." table)
958 (modify-syntax-entry ?= "." table)
959 (modify-syntax-entry ?> "." table)
960 (modify-syntax-entry ?\\ "\\" table)
961 (modify-syntax-entry ?_ "_" table)
962 (modify-syntax-entry ?| "." table)
964 (setq erlang-mode-syntax-table table)))
966 (set-syntax-table erlang-mode-syntax-table))
969 (defun erlang-keymap-init ()
970 (if erlang-mode-map
972 (setq erlang-mode-map (make-sparse-keymap))
973 (erlang-mode-commands erlang-mode-map))
974 (use-local-map erlang-mode-map))
977 (defun erlang-mode-commands (map)
978 (unless (boundp 'indent-line-function)
979 (define-key map "\t" 'erlang-indent-command))
980 (define-key map ";" 'erlang-electric-semicolon)
981 (define-key map "," 'erlang-electric-comma)
982 (define-key map "<" 'erlang-electric-lt)
983 (define-key map ">" 'erlang-electric-gt)
984 (define-key map "\C-m" 'erlang-electric-newline)
985 (if (not (boundp 'delete-key-deletes-forward))
986 (define-key map "\177" 'backward-delete-char-untabify)
987 (define-key map [backspace] 'backward-delete-char-untabify))
988 (define-key map "\M-q" 'erlang-fill-paragraph)
989 (unless (boundp 'beginning-of-defun-function)
990 (define-key map "\M-\C-a" 'erlang-beginning-of-function)
991 (define-key map "\M-\C-e" 'erlang-end-of-function)
992 (define-key map "\M-\C-h" 'erlang-mark-function))
993 (define-key map "\M-\t" 'erlang-complete-tag)
994 (define-key map "\C-c\M-\t" 'tempo-complete-tag)
995 (define-key map "\M-+" 'erlang-find-next-tag)
996 (define-key map "\C-c\M-a" 'erlang-beginning-of-clause)
997 (define-key map "\C-c\M-b" 'tempo-backward-mark)
998 (define-key map "\C-c\M-e" 'erlang-end-of-clause)
999 (define-key map "\C-c\M-f" 'tempo-forward-mark)
1000 (define-key map "\C-c\M-h" 'erlang-mark-clause)
1001 (define-key map "\C-c\C-c" 'comment-region)
1002 (define-key map "\C-c\C-j" 'erlang-generate-new-clause)
1003 (define-key map "\C-c\C-k" 'erlang-compile)
1004 (define-key map "\C-c\C-l" 'erlang-compile-display)
1005 (define-key map "\C-c\C-s" 'erlang-show-syntactic-information)
1006 (define-key map "\C-c\C-q" 'erlang-indent-function)
1007 (define-key map "\C-c\C-u" 'erlang-uncomment-region)
1008 (define-key map "\C-c\C-y" 'erlang-clone-arguments)
1009 (define-key map "\C-c\C-a" 'erlang-align-arrows)
1010 (define-key map "\C-c\C-z" 'erlang-shell-display)
1011 (unless inferior-erlang-use-cmm
1012 (define-key map "\C-x`" 'erlang-next-error)))
1015 (defun erlang-electric-init ()
1016 ;; Set up electric character functions to work with
1017 ;; delsel/pending-del mode. Also, set up text properties for bit
1018 ;; syntax handling.
1019 (mapcar #'(lambda (cmd)
1020 (put cmd 'delete-selection t) ;for delsel (Emacs)
1021 (put cmd 'pending-delete t)) ;for pending-del (XEmacs)
1022 '(erlang-electric-semicolon
1023 erlang-electric-comma
1024 erlang-electric-gt))
1026 (put 'bitsyntax-open-outer 'syntax-table '(4 . ?>))
1027 (put 'bitsyntax-open-outer 'rear-nonsticky '(category))
1028 (put 'bitsyntax-open-inner 'rear-nonsticky '(category))
1029 (put 'bitsyntax-close-inner 'rear-nonsticky '(category))
1030 (put 'bitsyntax-close-outer 'syntax-table '(5 . ?<))
1031 (put 'bitsyntax-close-outer 'rear-nonsticky '(category))
1032 (setq parse-sexp-lookup-properties 't))
1035 (defun erlang-mode-variables ()
1036 (or erlang-mode-abbrev-table
1037 (define-abbrev-table 'erlang-mode-abbrev-table ()))
1038 (setq local-abbrev-table erlang-mode-abbrev-table)
1039 (make-local-variable 'paragraph-start)
1040 (setq paragraph-start (concat "^$\\|" page-delimiter))
1041 (make-local-variable 'paragraph-separate)
1042 (setq paragraph-separate paragraph-start)
1043 (make-local-variable 'paragraph-ignore-fill-prefix)
1044 (setq paragraph-ignore-fill-prefix t)
1045 (make-local-variable 'require-final-newline)
1046 (setq require-final-newline t)
1047 (make-local-variable 'defun-prompt-regexp)
1048 (setq defun-prompt-regexp erlang-defun-prompt-regexp)
1049 (make-local-variable 'comment-start)
1050 (setq comment-start "%")
1051 (make-local-variable 'comment-start-skip)
1052 (setq comment-start-skip "%+\\s *")
1053 (make-local-variable 'indent-line-function)
1054 (setq indent-line-function 'erlang-indent-command)
1055 (make-local-variable 'indent-region-function)
1056 (setq indent-region-function 'erlang-indent-region)
1057 (set (make-local-variable 'comment-indent-function) 'erlang-comment-indent)
1058 (if (<= erlang-emacs-major-version 18)
1059 (set (make-local-variable 'comment-indent-hook) 'erlang-comment-indent))
1060 (set (make-local-variable 'parse-sexp-ignore-comments) t)
1061 (set (make-local-variable 'dabbrev-case-fold-search) nil)
1062 (set (make-local-variable 'imenu-prev-index-position-function)
1063 'erlang-beginning-of-function)
1064 (set (make-local-variable 'imenu-extract-index-name-function)
1065 'erlang-get-function-name)
1066 (set (make-local-variable 'tempo-match-finder)
1067 "[^-a-zA-Z0-9_]\\([-a-zA-Z0-9_]*\\)\\=")
1068 (set (make-local-variable 'beginning-of-defun-function)
1069 'erlang-beginning-of-function)
1070 (set (make-local-variable 'end-of-defun-function) 'erlang-end-of-function)
1071 (set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil)
1072 (set (make-local-variable 'fill-paragraph-function) 'erlang-fill-paragraph)
1073 (set (make-local-variable 'comment-add) 1)
1074 (set (make-local-variable 'outline-regexp) "[[:lower:]0-9_]+ *(.*) *-> *$")
1075 (set (make-local-variable 'outline-level) (lambda () 1))
1076 (set (make-local-variable 'add-log-current-defun-function)
1077 'erlang-current-defun))
1080 ;; Compilation.
1082 ;; The following code is compatible with the standard package `compilation',
1083 ;; making it possible to go to errors using `erlang-next-error' (or just
1084 ;; `next-error' in Emacs 21).
1086 ;; The normal `compile' command works of course. For best result, please
1087 ;; execute `make' with the `-w' flag.
1089 ;; Please see the variables named `compiling-..' above.
1091 (defun erlang-add-compilation-alist (alist)
1092 (require 'compile)
1093 (cond ((boundp 'compilation-error-regexp-alist) ; Emacs 19
1094 (while alist
1095 (or (assoc (car (car alist)) compilation-error-regexp-alist)
1096 (setq compilation-error-regexp-alist
1097 (cons (car alist) compilation-error-regexp-alist)))
1098 (setq alist (cdr alist))))
1099 ((boundp 'compilation-error-regexp)
1100 ;; Emacs 18, Only one regexp is allowed.
1101 (funcall (symbol-function 'set)
1102 'compilation-error-regexp (car (car alist))))))
1104 (defun erlang-font-lock-init ()
1105 "Initialize Font Lock for Erlang mode."
1106 (or erlang-font-lock-syntax-table
1107 (setq erlang-font-lock-syntax-table
1108 (let ((table (copy-syntax-table erlang-mode-syntax-table)))
1109 (modify-syntax-entry ?_ "w" table)
1110 table)))
1111 (set (make-local-variable 'font-lock-syntax-table)
1112 erlang-font-lock-syntax-table)
1113 (set (make-local-variable 'font-lock-beginning-of-syntax-function)
1114 'erlang-beginning-of-clause)
1115 (make-local-variable 'font-lock-keywords)
1116 (let ((level (cond ((boundp 'font-lock-maximum-decoration)
1117 (symbol-value 'font-lock-maximum-decoration))
1118 ((boundp 'font-lock-use-maximal-decoration)
1119 (symbol-value 'font-lock-use-maximal-decoration))
1120 (t nil))))
1121 (if (consp level)
1122 (setq level (cdr-safe (or (assq 'erlang-mode level)
1123 (assq t level)))))
1124 ;; `level' can here be:
1125 ;; A number - The fontification level
1126 ;; nil - Use the default
1127 ;; t - Use maximum
1128 (cond ((eq level nil)
1129 (set 'font-lock-keywords erlang-font-lock-keywords))
1130 ((eq level 1)
1131 (set 'font-lock-keywords erlang-font-lock-keywords-1))
1132 ((eq level 2)
1133 (set 'font-lock-keywords erlang-font-lock-keywords-2))
1135 (set 'font-lock-keywords erlang-font-lock-keywords-3))))
1137 ;; Modern font-locks can handle the above much more elegantly:
1138 (set (make-local-variable 'font-lock-defaults)
1139 '((erlang-font-lock-keywords erlang-font-lock-keywords-1
1140 erlang-font-lock-keywords-2 erlang-font-lock-keywords-3)
1141 nil nil ((?_ . "w")) erlang-beginning-of-clause
1142 (font-lock-mark-block-function . erlang-mark-clause))))
1146 ;; Useful when defining your own keywords.
1147 (defun erlang-font-lock-set-face (ks &rest faces)
1148 "Replace the face components in a list of keywords.
1150 The first argument, KS, is a list of keywords. The rest of the
1151 arguments are expressions to replace the face information with. The
1152 first expression replaces the face of the first keyword, the second
1153 expression the second keyword etc.
1155 Should an expression be nil, the face of the corresponding keyword is
1156 not changed.
1158 Should fewer expressions than keywords be given, the last expression
1159 is used for all remaining keywords.
1161 Normally, the expressions are just atoms representing the new face.
1162 They could however be more complex, returning different faces in
1163 different situations.
1165 This function only handles keywords with elements on the forms:
1166 (REGEXP NUMBER FACE)
1167 (REGEXP NUMBER FACE OVERWRITE)
1169 This could be used when defining your own special font-lock setup, e.g:
1171 \(setq my-font-lock-keywords
1172 (append erlang-font-lock-keywords-func
1173 erlang-font-lock-keywords-dollar
1174 (erlang-font-lock-set-face
1175 erlang-font-lock-keywords-macros 'my-neon-green-face)
1176 (erlang-font-lock-set-face
1177 erlang-font-lock-keywords-lc 'my-deep-red 'my-light-red)
1178 erlang-font-lock-keywords-attr))
1180 For a more elaborate example, please see the beginning of the file
1181 `erlang.el'."
1182 (let ((res '()))
1183 (while ks
1184 (let* ((regexp (car (car ks)))
1185 (number (car (cdr (car ks))))
1186 (new-face (if (and faces (car faces))
1187 (car faces)
1188 (car (cdr (cdr (car ks))))))
1189 (overwrite (car (cdr (cdr (cdr (car ks))))))
1190 (new-keyword (list regexp number new-face)))
1191 (if overwrite (nconc new-keyword (list overwrite)))
1192 (setq res (cons new-keyword res))
1193 (setq ks (cdr ks))
1194 (if (and faces (cdr faces))
1195 (setq faces (cdr faces)))))
1196 (nreverse res)))
1199 (defun erlang-font-lock-level-0 ()
1200 ;; DocStringOrig: font-cmd
1201 "Unfontify current buffer."
1202 (interactive)
1203 (font-lock-mode 0))
1206 (defun erlang-font-lock-level-1 ()
1207 ;; DocStringCopy: font-cmd
1208 "Fontify current buffer at level 1.
1209 This highlights function headers, reserved keywords, strings and comments."
1210 (interactive)
1211 (require 'font-lock)
1212 (set 'font-lock-keywords erlang-font-lock-keywords-1)
1213 (font-lock-mode 1)
1214 (funcall (symbol-function 'font-lock-fontify-buffer)))
1217 (defun erlang-font-lock-level-2 ()
1218 ;; DocStringCopy: font-cmd
1219 "Fontify current buffer at level 2.
1220 This highlights level 1 features (see `erlang-font-lock-level-1')
1221 plus bifs, guards and `single quotes'."
1222 (interactive)
1223 (require 'font-lock)
1224 (set 'font-lock-keywords erlang-font-lock-keywords-2)
1225 (font-lock-mode 1)
1226 (funcall (symbol-function 'font-lock-fontify-buffer)))
1229 (defun erlang-font-lock-level-3 ()
1230 ;; DocStringCopy: font-cmd
1231 "Fontify current buffer at level 3.
1232 This highlights level 2 features (see `erlang-font-lock-level-2')
1233 plus variables, macros and records."
1234 (interactive)
1235 (require 'font-lock)
1236 (set 'font-lock-keywords erlang-font-lock-keywords-3)
1237 (font-lock-mode 1)
1238 (funcall (symbol-function 'font-lock-fontify-buffer)))
1241 (defun erlang-menu-init ()
1242 "Init menus for Erlang mode.
1244 The variable `erlang-menu-items' contain a description of the Erlang
1245 mode menu. Normally, the list contains atoms, representing variables
1246 bound to pieces of the menu.
1248 Personal extensions could be added to `erlang-menu-personal-items'.
1250 This function should be called if any variable describing the
1251 menu configuration is changed."
1252 (erlang-menu-install "Erlang" erlang-menu-items erlang-mode-map t))
1255 (defun erlang-menu-install (name items keymap &optional popup)
1256 "Install a menu in Emacs or XEmacs based on an abstract description.
1258 NAME is the name of the menu.
1260 ITEMS is a list. The elements are either nil representing a horizontal
1261 line or a list with two or three elements. The first is the name of
1262 the menu item, the second the function to call, or a submenu, on the
1263 same same form as ITEMS. The third optional element is an expression
1264 which is evaluated every time the menu is displayed. Should the
1265 expression evaluate to nil the menu item is ghosted.
1267 KEYMAP is the keymap to add to menu to. (When using XEmacs, the menu
1268 will only be visible when this menu is the global, the local, or an
1269 activate minor mode keymap.)
1271 If POPUP is non-nil, the menu is bound to the XEmacs `mode-popup-menu'
1272 variable, i.e. it will popup when pressing the right mouse button.
1274 Please see the variable `erlang-menu-base-items'."
1275 (cond (erlang-xemacs-p
1276 (let ((menu (erlang-menu-xemacs name items keymap)))
1277 ;; We add the menu to the global menubar.
1278 (funcall (symbol-function 'add-submenu) nil menu)
1279 (setcdr erlang-xemacs-popup-menu (cdr menu))
1280 (if (and popup (boundp 'mode-popup-menu))
1281 (funcall (symbol-function 'set)
1282 'mode-popup-menu erlang-xemacs-popup-menu))))
1283 ((>= erlang-emacs-major-version 19)
1284 (define-key keymap (vector 'menu-bar (intern name))
1285 (erlang-menu-make-keymap name items)))
1286 (t nil)))
1289 (defun erlang-menu-make-keymap (name items)
1290 "Build a menu for Emacs 19."
1291 (let ((menumap (funcall (symbol-function 'make-sparse-keymap)
1292 name))
1293 (count 0)
1294 id def first second third)
1295 (setq items (reverse items))
1296 (while items
1297 ;; Replace any occurrence of atoms by their value.
1298 (while (and items (atom (car items)) (not (null (car items))))
1299 (if (and (boundp (car items))
1300 (listp (symbol-value (car items))))
1301 (setq items (append (reverse (symbol-value (car items)))
1302 (cdr items)))
1303 (setq items (cdr items))))
1304 (setq first (car-safe (car items)))
1305 (setq second (car-safe (cdr-safe (car items))))
1306 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1307 (cond ((null first)
1308 (setq count (+ count 1))
1309 (setq id (intern (format "separator-%d" count)))
1310 (setq def '("--" . nil)))
1311 ((and (consp second) (eq (car second) 'lambda))
1312 (setq count (+ count 1))
1313 (setq id (intern (format "lambda-%d" count)))
1314 (setq def (cons first second)))
1315 ((symbolp second)
1316 (setq id second)
1317 (setq def (cons first second)))
1319 (setq count (+ count 1))
1320 (setq id (intern (format "submenu-%d" count)))
1321 (setq def (erlang-menu-make-keymap first second))))
1322 (define-key menumap (vector id) def)
1323 (if third
1324 (put id 'menu-enable third))
1325 (setq items (cdr items)))
1326 (cons name menumap)))
1329 (defun erlang-menu-xemacs (name items &optional keymap)
1330 "Build a menu for XEmacs."
1331 (let ((res '())
1332 first second third entry)
1333 (while items
1334 ;; Replace any occurrence of atoms by their value.
1335 (while (and items (atom (car items)) (not (null (car items))))
1336 (if (and (boundp (car items))
1337 (listp (symbol-value (car items))))
1338 (setq items (append (reverse (symbol-value (car items)))
1339 (cdr items)))
1340 (setq items (cdr items))))
1341 (setq first (car-safe (car items)))
1342 (setq second (car-safe (cdr-safe (car items))))
1343 (setq third (car-safe (cdr-safe (cdr-safe (car items)))))
1344 (cond ((null first)
1345 (setq res (cons "------" res)))
1346 ((symbolp second)
1347 (setq res (cons (vector first second (or third t)) res)))
1348 ((and (consp second) (eq (car second) 'lambda))
1349 (setq res (cons (vector first (list 'call-interactively second)
1350 (or third t)) res)))
1352 (setq res (cons (cons first
1353 (cdr (erlang-menu-xemacs
1354 first second)))
1355 res))))
1356 (setq items (cdr items)))
1357 (setq res (reverse res))
1358 ;; When adding a menu to a minor-mode keymap under Emacs,
1359 ;; it disappears when the mode is disabled. The expression
1360 ;; generated below imitates this behaviour.
1361 ;; (This could be expressed much clearer using backquotes,
1362 ;; but I don't want to pull in every package.)
1363 (if keymap
1364 (let ((expr (list 'or
1365 (list 'eq keymap 'global-map)
1366 (list 'eq keymap (list 'current-local-map))
1367 (list 'symbol-value
1368 (list 'car-safe
1369 (list 'rassq
1370 keymap
1371 'minor-mode-map-alist))))))
1372 (setq res (cons ':included (cons expr res)))))
1373 (cons name res)))
1376 (defun erlang-menu-substitute (items alist)
1377 "Substitute functions in menu described by ITEMS.
1379 The menu ITEMS is updated destructively.
1381 ALIST is list of pairs where the car is the old function and cdr the new."
1382 (let (first second pair)
1383 (while items
1384 (setq first (car-safe (car items)))
1385 (setq second (car-safe (cdr-safe (car items))))
1386 (cond ((null first))
1387 ((symbolp second)
1388 (setq pair (and second (assq second alist)))
1389 (if pair
1390 (setcar (cdr (car items)) (cdr pair))))
1391 ((and (consp second) (eq (car second) 'lambda)))
1393 (erlang-menu-substitute second alist)))
1394 (setq items (cdr items)))))
1397 (defun erlang-menu-add-above (entry above items)
1398 "Add menu ENTRY above menu entry ABOVE in menu ITEMS.
1399 Do nothing if the items already should be in the menu.
1400 Should ABOVE not be in the list, the entry is added at
1401 the bottom of the menu.
1403 The new menu is returned. No guarantee is given that the original
1404 menu is left unchanged.
1406 The equality test is performed by `eq'.
1408 Example: (erlang-menu-add-above 'my-erlang-menu-items
1409 'erlang-menu-man-items)"
1410 (erlang-menu-add-below entry above items t))
1413 (defun erlang-menu-add-below (entry below items &optional above-p)
1414 "Add menu ENTRY below menu items BELOW in the Erlang menu.
1415 Do nothing if the items already should be in the menu.
1416 Should BELOW not be in the list, items is added at the bottom
1417 of the menu.
1419 The new menu is returned. No guarantee is given that the original
1420 menu is left unchanged.
1422 The equality test is performed by `eq'.
1424 Example:
1426 \(setq erlang-menu-items
1427 (erlang-menu-add-below 'my-erlang-menu-items
1428 'erlang-menu-base-items
1429 erlang-menu-items))"
1430 (if (memq entry items)
1431 items ; Return the original menu.
1432 (let ((head '())
1433 (done nil)
1434 res)
1435 (while (not done)
1436 (cond ((null items)
1437 (setq res (append head (list entry)))
1438 (setq done t))
1439 ((eq below (car items))
1440 (setq res
1441 (if above-p
1442 (append head (cons entry items))
1443 (append head (cons (car items)
1444 (cons entry (cdr items))))))
1445 (setq done t))
1447 (setq head (append head (list (car items))))
1448 (setq items (cdr items)))))
1449 res)))
1451 (defun erlang-menu-delete (entry items)
1452 "Delete ENTRY from menu ITEMS.
1454 The new menu is returned. No guarantee is given that the original
1455 menu is left unchanged."
1456 (delq entry items))
1458 ;; Man code:
1460 (defun erlang-man-init ()
1461 "Add menus containing the manual pages of the Erlang.
1463 The variable `erlang-man-dirs' contains entries describing
1464 the location of the manual pages."
1465 (interactive)
1466 (if erlang-man-inhibit
1468 (setq erlang-menu-man-items
1469 '(nil
1470 ("Man - Function" erlang-man-function)))
1471 (if erlang-man-dirs
1472 (setq erlang-menu-man-items
1473 (append erlang-menu-man-items
1474 (erlang-man-make-top-menu erlang-man-dirs))))
1475 (setq erlang-menu-items
1476 (erlang-menu-add-above 'erlang-menu-man-items
1477 'erlang-menu-version-items
1478 erlang-menu-items))
1479 (erlang-menu-init)))
1482 (defun erlang-man-uninstall ()
1483 "Remove the man pages from the Erlang mode."
1484 (interactive)
1485 (setq erlang-menu-items
1486 (erlang-menu-delete 'erlang-menu-man-items erlang-menu-items))
1487 (erlang-menu-init))
1490 ;; The man menu is a hierarchal structure, with the manual sections
1491 ;; at the top, described by `erlang-man-dirs'. The next level could
1492 ;; either be the manual pages if not to many, otherwise it is an index
1493 ;; menu whose submenus will contain up to `erlang-man-max-menu-size'
1494 ;; manual pages.
1496 (defun erlang-man-make-top-menu (dir-list)
1497 "Create one menu entry per element of DIR-LIST.
1498 The format is described in the documentation of `erlang-man-dirs'."
1499 (let ((menu '())
1500 dir)
1501 (while dir-list
1502 (setq dir (cond ((nth 2 (car dir-list))
1503 ;; Relative to `erlang-man-root-dir'.
1504 (and (stringp erlang-man-root-dir)
1505 (concat erlang-man-root-dir (nth 1 (car dir-list)))))
1507 ;; Absolute
1508 (nth 1 (car dir-list)))))
1509 (if (and dir
1510 (file-readable-p dir))
1511 (setq menu (cons (list (car (car dir-list))
1512 (erlang-man-make-middle-menu
1513 (erlang-man-get-files dir)))
1514 menu)))
1515 (setq dir-list (cdr dir-list)))
1516 ;; Should no menus be found, generate a menu item which
1517 ;; will display a help text, when selected.
1518 (if menu
1519 (nreverse menu)
1520 '(("Man Pages"
1521 (("Error! Why?" erlang-man-describe-error)))))))
1524 ;; Should the menu be to long, let's split it into a number of
1525 ;; smaller menus. Warning, this code contains beautiful
1526 ;; destructive operations!
1527 (defun erlang-man-make-middle-menu (filelist)
1528 "Create the second level menu from FILELIST.
1530 Should the list be longer than `erlang-man-max-menu-size', a tree of
1531 menus is created."
1532 (if (<= (length filelist) erlang-man-max-menu-size)
1533 (erlang-man-make-menu filelist)
1534 (let ((menu '())
1535 (filelist (copy-sequence filelist))
1536 segment submenu pair)
1537 (while filelist
1538 (setq pair (nthcdr (- erlang-man-max-menu-size 1) filelist))
1539 (setq segment filelist)
1540 (if (null pair)
1541 (setq filelist nil)
1542 (setq filelist (cdr pair))
1543 (setcdr pair nil))
1544 (setq submenu (erlang-man-make-menu segment))
1545 (setq menu (cons (list (concat (car (car submenu))
1546 " -- "
1547 (car (car (reverse submenu))))
1548 submenu)
1549 menu)))
1550 (nreverse menu))))
1553 (defun erlang-man-make-menu (filelist)
1554 "Make a leaf menu based on FILELIST."
1555 (let ((menu '())
1556 item)
1557 (while filelist
1558 (setq item (erlang-man-make-menu-item (car filelist)))
1559 (if item
1560 (setq menu (cons item menu)))
1561 (setq filelist (cdr filelist)))
1562 (nreverse menu)))
1565 (defun erlang-man-make-menu-item (file)
1566 "Create a menu item containing the name of the man page."
1567 (and (string-match ".*/\\([^/]+\\)\\.[^.]$" file)
1568 (let ((page (substring file (match-beginning 1) (match-end 1))))
1569 (list (capitalize page)
1570 (list 'lambda '()
1571 '(interactive)
1572 (list 'funcall 'erlang-man-display-function
1573 file))))))
1576 (defun erlang-man-get-files (dir)
1577 "Return files in directory DIR."
1578 (directory-files dir t ".*\\.[0-9]\\'"))
1581 (defun erlang-man-module (&optional module)
1582 "Find manual page for MODULE, defaults to module of function under point.
1583 This function is aware of imported functions."
1584 (interactive
1585 (list (let* ((mod (car-safe (erlang-get-function-under-point)))
1586 (input (read-string
1587 (format "Manual entry for module%s: "
1588 (if (or (null mod) (string= mod ""))
1590 (format " (default %s)" mod))))))
1591 (if (string= input "")
1593 input))))
1594 (or module (setq module (car (erlang-get-function-under-point))))
1595 (if (or (null module) (string= module ""))
1596 (error "No Erlang module name given"))
1597 (let ((dir-list erlang-man-dirs)
1598 (pat (concat "/" (regexp-quote module) "\\.[^.]$"))
1599 (file nil)
1600 file-list)
1601 (while (and dir-list (null file))
1602 (setq file-list (erlang-man-get-files
1603 (if (nth 2 (car dir-list))
1604 (concat erlang-man-root-dir (nth 1 (car dir-list)))
1605 (nth 1 (car dir-list)))))
1606 (while (and file-list (null file))
1607 (if (string-match pat (car file-list))
1608 (setq file (car file-list)))
1609 (setq file-list (cdr file-list)))
1610 (setq dir-list (cdr dir-list)))
1611 (if file
1612 (funcall erlang-man-display-function file)
1613 (error "No manual page for module %s found" module))))
1616 ;; Warning, the function `erlang-man-function' is a hack!
1617 ;; It links itself into the man code in a non-clean way. I have
1618 ;; chosen to keep it since it provides a very useful functionality
1619 ;; which is not possible to achieve using a clean approach.
1620 ;; / AndersL
1622 (defvar erlang-man-function-name nil
1623 "Name of function for last `erlang-man-function' call.
1624 Used for communication between `erlang-man-function' and the
1625 patch to `Man-notify-when-ready'.")
1627 (defun erlang-man-function (&optional name)
1628 "Find manual page for NAME, where NAME is module:function.
1629 The entry for `function' is displayed.
1631 This function is aware of imported functions."
1632 (interactive
1633 (list (let* ((mod-func (erlang-get-function-under-point))
1634 (mod (car-safe mod-func))
1635 (func (nth 1 mod-func))
1636 (input (read-string
1637 (format
1638 "Manual entry for `module:func' or `module'%s: "
1639 (if (or (null mod) (string= mod ""))
1641 (format " (default %s:%s)" mod func))))))
1642 (if (string= input "")
1643 (if (and mod func)
1644 (concat mod ":" func)
1645 mod)
1646 input))))
1647 ;; Emacs 18 doesn't provide `man'...
1648 (condition-case nil
1649 (require 'man)
1650 (error nil))
1651 (let ((modname nil)
1652 (funcname nil))
1653 (cond ((null name)
1654 (let ((mod-func (erlang-get-function-under-point)))
1655 (setq modname (car-safe mod-func))
1656 (setq funcname (nth 1 mod-func))))
1657 ((string-match ":" name)
1658 (setq modname (substring name 0 (match-beginning 0)))
1659 (setq funcname (substring name (match-end 0) nil)))
1660 ((stringp name)
1661 (setq modname name)))
1662 (if (or (null modname) (string= modname ""))
1663 (error "No Erlang module name given"))
1664 (cond ((fboundp 'Man-notify-when-ready)
1665 ;; Emacs 19: The man command could possibly start an
1666 ;; asynchronous process, i.e. we must hook ourselves into
1667 ;; the system to be activated when the man-process
1668 ;; terminates.
1669 (if (null funcname)
1671 (erlang-man-patch-notify)
1672 (setq erlang-man-function-name funcname))
1673 (condition-case nil
1674 (erlang-man-module modname)
1675 (error (setq erlang-man-function-name nil))))
1677 (erlang-man-module modname)
1678 (if funcname
1679 (erlang-man-find-function
1680 (or (get-buffer "*Manual Entry*") ; Emacs 18
1681 (current-buffer)) ; XEmacs
1682 funcname))))))
1685 ;; Should the defadvice be at the top level, the package `advice' would
1686 ;; be required. Now it is only required when this functionality
1687 ;; is used. (Emacs 19 specific.)
1688 (defun erlang-man-patch-notify ()
1689 "Patch the function `Man-notify-when-ready' to search for function.
1690 The variable `erlang-man-function-name' is assumed to be bound to
1691 the function name, or to nil.
1693 The reason for patching a function is that under Emacs 19, the man
1694 command is executed asynchronously."
1695 (condition-case nil
1696 (require 'advice)
1697 ;; This should never happened since this is only called when
1698 ;; running under Emacs 19.
1699 (error (error (concat "This command needs the package `advice', "
1700 "please upgrade your Emacs."))))
1701 (require 'man)
1702 (defadvice Man-notify-when-ready
1703 (after erlang-Man-notify-when-ready activate)
1704 "Set point at the documentation of the function name in
1705 `erlang-man-function-name' when the man page is displayed."
1706 (if erlang-man-function-name
1707 (erlang-man-find-function (ad-get-arg 0) erlang-man-function-name))
1708 (setq erlang-man-function-name nil)))
1711 (defun erlang-man-find-function (buf func)
1712 "Find manual page for function in `erlang-man-function-name' in buffer BUF."
1713 (if func
1714 (let ((win (get-buffer-window buf)))
1715 (if win
1716 (progn
1717 (set-buffer buf)
1718 (goto-char (point-min))
1719 (if (re-search-forward
1720 (concat "^[ \t]+" func " ?(")
1721 (point-max) t)
1722 (progn
1723 (forward-word -1)
1724 (set-window-point win (point)))
1725 (message "Could not find function `%s'" func)))))))
1728 (defun erlang-man-display (file)
1729 "Display FILE as a `man' file.
1730 This is the default manual page display function.
1731 The variables `erlang-man-display-function' contains the function
1732 to be used."
1733 ;; Emacs 18 doesn't `provide' man.
1734 (condition-case nil
1735 (require 'man)
1736 (error nil))
1737 (if file
1738 (let ((process-environment (copy-sequence process-environment)))
1739 (if (string-match "\\(.*\\)/man[^/]*/\\([^/]+\\)\\.[^.]$" file)
1740 (let ((dir (substring file (match-beginning 1) (match-end 1)))
1741 (page (substring file (match-beginning 2) (match-end 2))))
1742 (if (fboundp 'setenv)
1743 (setenv "MANPATH" dir)
1744 ;; Emacs 18
1745 (setq process-environment (cons (concat "MANPATH=" dir)
1746 process-environment)))
1747 (cond ((not (and (not erlang-xemacs-p)
1748 (= erlang-emacs-major-version 19)
1749 (< erlang-emacs-minor-version 29)))
1750 (manual-entry page))
1752 ;; Emacs 19.28 and earlier versions of 19:
1753 ;; The manual-entry command unconditionally prompts
1754 ;; the user :-(
1755 (funcall (symbol-function 'Man-getpage-in-background)
1756 page))))
1757 (error "Can't find man page for %s\n" file)))))
1760 (defun erlang-man-describe-error ()
1761 "Describe why the manual pages weren't found."
1762 (interactive)
1763 (with-output-to-temp-buffer "*Erlang Man Error*"
1764 (princ "Normally, this menu should contain Erlang manual pages.
1766 In order to find the manual pages, the variable `erlang-man-root-dir'
1767 should be bound to the name of the directory containing the Erlang
1768 man pages. The name should not include the final slash.
1770 Practically, you should add a line on the following form to
1771 your ~/.emacs, or ask your system administrator to add it to
1772 the site init file:
1774 (setq erlang-man-root-dir \"/usr/local/erlang\")
1776 After installing the line, kill and restart Emacs, or restart Erlang
1777 mode with the command `M-x erlang-mode RET'.")))
1779 ;; Indentation code:
1781 (defun erlang-indent-command (&optional whole-exp)
1782 "Indent current line as Erlang code.
1783 With argument, indent any additional lines of the same clause
1784 rigidly along with this one."
1785 (interactive "P")
1786 (if whole-exp
1787 ;; If arg, always indent this line as Erlang
1788 ;; and shift remaining lines of clause the same amount.
1789 (let ((shift-amt (erlang-indent-line))
1790 beg end)
1791 (save-excursion
1792 (if erlang-tab-always-indent
1793 (beginning-of-line))
1794 (setq beg (point))
1795 (erlang-end-of-clause 1)
1796 (setq end (point))
1797 (goto-char beg)
1798 (forward-line 1)
1799 (setq beg (point)))
1800 (if (> end beg)
1801 (indent-code-rigidly beg end shift-amt "\n")))
1802 (if (and (not erlang-tab-always-indent)
1803 (save-excursion
1804 (skip-chars-backward " \t")
1805 (not (bolp))))
1806 (insert-tab)
1807 (erlang-indent-line))))
1810 (defun erlang-indent-line ()
1811 "Indent current line as Erlang code.
1812 Return the amount the indentation changed by."
1813 (let ((pos (- (point-max) (point)))
1814 indent beg
1815 shift-amt)
1816 (beginning-of-line 1)
1817 (setq beg (point))
1818 (skip-chars-forward " \t")
1819 (cond ((looking-at "%")
1820 (setq indent (funcall comment-indent-function))
1821 (setq shift-amt (- indent (current-column))))
1823 (setq indent (erlang-calculate-indent))
1824 (cond ((null indent)
1825 (setq indent (current-indentation)))
1826 ((eq indent t)
1827 ;; This should never occur here.
1828 (error "Erlang mode error"))
1829 ((= (char-syntax (following-char)) ?\))
1830 (setq indent (1- indent))))
1831 (setq shift-amt (- indent (current-column)))))
1832 (if (zerop shift-amt)
1834 (delete-region beg (point))
1835 (indent-to indent))
1836 ;; If initial point was within line's indentation, position
1837 ;; after the indentation. Else stay at same point in text.
1838 (if (> (- (point-max) pos) (point))
1839 (goto-char (- (point-max) pos)))
1840 shift-amt))
1843 (defun erlang-indent-region (beg end)
1844 "Indent region of Erlang code.
1846 This is automagically called by the user level function `indent-region'."
1847 (interactive "r")
1848 (save-excursion
1849 (let ((case-fold-search nil)
1850 (continue t)
1851 (from-end (- (point-max) end))
1852 indent-point;; The beginning of the current line
1853 indent;; The indent amount
1854 state)
1855 (goto-char beg)
1856 (beginning-of-line)
1857 (setq indent-point (point))
1858 (erlang-beginning-of-clause)
1859 ;; Parse the Erlang code from the beginning of the clause to
1860 ;; the beginning of the region.
1861 (while (< (point) indent-point)
1862 (setq state (erlang-partial-parse (point) indent-point state)))
1863 ;; Indent every line in the region
1864 (while continue
1865 (goto-char indent-point)
1866 (skip-chars-forward " \t")
1867 (cond ((looking-at "%")
1868 ;; Do not use our stack to help the user to customize
1869 ;; comment indentation.
1870 (setq indent (funcall comment-indent-function)))
1871 ((looking-at "$")
1872 ;; Don't indent empty lines.
1873 (setq indent 0))
1875 (setq indent
1876 (save-excursion
1877 (erlang-calculate-stack-indent (point) state)))
1878 (cond ((null indent)
1879 (setq indent (current-indentation)))
1880 ((eq indent t)
1881 ;; This should never occur here.
1882 (error "Erlang mode error"))
1883 ((= (char-syntax (following-char)) ?\))
1884 (setq indent (1- indent))))))
1885 (if (zerop (- indent (current-column)))
1887 (delete-region indent-point (point))
1888 (indent-to indent))
1889 ;; Find the next line in the region
1890 (goto-char indent-point)
1891 (save-excursion
1892 (forward-line 1)
1893 (setq indent-point (point)))
1894 (if (>= from-end (- (point-max) indent-point))
1895 (setq continue nil)
1896 (while (< (point) indent-point)
1897 (setq state (erlang-partial-parse
1898 (point) indent-point state))))))))
1901 (defun erlang-indent-current-buffer ()
1902 "Indent current buffer as Erlang code."
1903 (interactive)
1904 (save-excursion
1905 (save-restriction
1906 (widen)
1907 (erlang-indent-region (point-min) (point-max)))))
1910 (defun erlang-indent-function ()
1911 "Indent current Erlang function."
1912 (interactive)
1913 (save-excursion
1914 (let ((end (progn (erlang-end-of-function 1) (point)))
1915 (beg (progn (erlang-beginning-of-function 1) (point))))
1916 (erlang-indent-region beg end))))
1919 (defun erlang-indent-clause ()
1920 "Indent current Erlang clause."
1921 (interactive)
1922 (save-excursion
1923 (let ((end (progn (erlang-end-of-clause 1) (point)))
1924 (beg (progn (erlang-beginning-of-clause 1) (point))))
1925 (erlang-indent-region beg end))))
1928 (defmacro erlang-push (x stack) (list 'setq stack (list 'cons x stack)))
1929 (defmacro erlang-pop (stack) (list 'setq stack (list 'cdr stack)))
1930 ;; Would much prefer to make caddr a macro but this clashes.
1931 (defun erlang-caddr (x) (car (cdr (cdr x))))
1934 (defun erlang-calculate-indent (&optional parse-start)
1935 "Compute appropriate indentation for current line as Erlang code.
1936 Return nil if line starts inside string, t if in a comment."
1937 (save-excursion
1938 (let ((indent-point (point))
1939 (case-fold-search nil)
1940 (state nil))
1941 (if parse-start
1942 (goto-char parse-start)
1943 (erlang-beginning-of-clause))
1944 (while (< (point) indent-point)
1945 (setq state (erlang-partial-parse (point) indent-point state)))
1946 (erlang-calculate-stack-indent indent-point state))))
1948 (defun erlang-show-syntactic-information ()
1949 "Show syntactic information for current line."
1951 (interactive)
1953 (save-excursion
1954 (let ((starting-point (point))
1955 (case-fold-search nil)
1956 (state nil))
1957 (erlang-beginning-of-clause)
1958 (while (< (point) starting-point)
1959 (setq state (erlang-partial-parse (point) starting-point state)))
1960 (message "%S" state))))
1963 (defun erlang-partial-parse (from to &optional state)
1964 "Parse Erlang syntax starting at FROM until TO, with an optional STATE.
1965 Value is list (stack token-start token-type in-what)."
1966 (goto-char from) ; Start at the beginning
1967 (erlang-skip-blank to)
1968 (let ((cs (char-syntax (following-char)))
1969 (stack (car state))
1970 (token (point))
1971 in-what)
1972 (cond
1974 ;; Done: Return previous state.
1975 ((>= token to)
1976 (setq token (nth 1 state))
1977 (setq cs (nth 2 state))
1978 (setq in-what (nth 3 state)))
1980 ;; Word constituent: check and handle keywords.
1981 ((= cs ?w)
1982 (cond ((looking-at "\\(end\\|after\\)[^_a-zA-Z0-9]")
1983 ;; Must pop top icr layer, `after' will push a new
1984 ;; layer next.
1985 (progn
1986 (while (and stack (eq (car (car stack)) '->))
1987 (erlang-pop stack))
1988 (if (and stack (memq (car (car stack)) '(icr begin)))
1989 (erlang-pop stack))))
1990 ((looking-at "catch[^,\n\\of]*\n")
1991 ;; Must pop top icr layer, `catch' in try/catch
1992 ;;will push a new layer next.
1993 (progn
1994 (while (and stack (eq (car (car stack)) '->))
1995 (erlang-pop stack))
1996 (if (and stack (memq (car (car stack)) '(icr begin)))
1997 (erlang-pop stack))))
1999 (cond ((looking-at "\\(if\\|case\\|receive\\|try\\)[^_a-zA-Z0-9]")
2000 ;; Must push a new icr (if/case/receive) layer.
2001 (erlang-push (list 'icr token (current-column)) stack))
2002 ((looking-at "\\(fun\\)[^_a-zA-Z0-9]")
2003 ;; Push a new icr layer if we are defining a `fun'
2004 ;; expression, not when we are refering an existing
2005 ;; function.
2006 (if (save-excursion
2007 (goto-char (match-end 1))
2008 (erlang-skip-blank to)
2009 (eq (following-char) ?\())
2010 (erlang-push (list 'icr token (current-column)) stack)))
2011 ((looking-at "\\(begin\\|query\\)[^_a-zA-Z0-9]")
2012 (erlang-push (list 'begin token (current-column)) stack))
2013 ;; In test suites you may want to do something like
2014 ;; ?match(Mem when integer(Mem), mnesia:table_info(Tab,
2015 ;; memory)), and then the following if/case/receive
2016 ;; statement will mess up the indentation by fooling the
2017 ;; erlang mode to think the 'when' in the argument is a
2018 ;; "real" when. The following three clauses will avoid
2019 ;; this problem.
2020 ((looking-at "when[^->\.]*if[^->\.]*->"))
2021 ((looking-at "when[^->\.]*case[^->\.]*->"))
2022 ((looking-at "when[^->\.]*receive[^->\.]*->"))
2023 ;; Normal when case
2024 ((looking-at "when [^->\.]*->")
2025 (erlang-push (list 'when token (current-column)) stack))
2026 ((looking-at "after[.]+->")
2027 (erlang-push (list 'icr token (current-column)) stack))
2028 ((looking-at "after[^_a-zA-Z0-9->]")
2029 (erlang-push (list 'icr token (current-column)) stack)
2030 (erlang-push (list '-> token (current-column)) stack))
2031 ((looking-at "catch[^,\n\\of]*\n")
2032 (erlang-push (list 'icr token (current-column)) stack))
2034 (forward-sexp 1))
2035 ;; String: Try to skip over it. (Catch error if not complete.)
2036 ((= cs ?\")
2037 (condition-case nil
2038 (progn
2039 (forward-sexp 1)
2040 (if (> (point) to)
2041 (progn
2042 (setq in-what 'string)
2043 (goto-char to))))
2044 (error
2045 (setq in-what 'string)
2046 (goto-char to))))
2048 ;; Symbol constituent or punctuation
2050 ((memq cs '(?. ?_))
2051 (cond
2053 ;; Clause end
2054 ((= (following-char) ?\;)
2055 (if (and stack (eq (car (car stack)) '->))
2056 (erlang-pop stack))
2057 (forward-char 1))
2059 ;; Function end
2060 ((looking-at "\\.\\(\\s \\|\n\\|\\s<\\)")
2061 (setq stack nil)
2062 (forward-char 1))
2064 ;; Function head
2065 ((looking-at "->\\|:-")
2066 (save-excursion
2067 (back-to-indentation)
2068 (cond ((looking-at "after[^_a-zA-Z0-9]")
2069 (erlang-pop stack))))
2070 (if (and stack (eq (car (car stack)) 'when))
2071 (erlang-pop stack))
2072 (erlang-push (list '-> token (current-column)) stack)
2073 (forward-char 2))
2075 ;; List-comprehension divider
2076 ((looking-at "||")
2077 (erlang-push (list '|| token (current-column)) stack)
2078 (forward-char 2))
2080 ;; Parameter separator
2081 ((looking-at ",")
2082 (forward-char 1))
2084 ;; Bit-syntax open paren
2085 ((looking-at "<<")
2086 (erlang-push (list '\( token (current-column)) stack)
2087 (forward-char 2))
2089 ;; Bbit-syntax close paren
2090 ((looking-at ">>")
2091 (while (memq (car (car stack)) '(|| ->))
2092 (erlang-pop stack))
2093 (cond ((eq (car (car stack)) '\()
2094 (erlang-pop stack))
2095 ((memq (car (car stack)) '(icr begin))
2096 (error "Missing `end'"))
2098 (error "Unbalanced parentheses")))
2099 (forward-char 2))
2101 ;; Macro
2102 ((= (following-char) ??)
2103 ;; Skip over the ?
2104 (forward-char 1)
2107 ;; Other punctuation: Skip over it and any following punctuation
2108 ((= cs ?.)
2109 ;; Skip over all characters in the operand.
2110 (skip-syntax-forward "."))
2112 ;; Other char: Skip over it.
2114 (forward-char 1))))
2116 ;; Open parenthesis
2117 ((= cs ?\()
2118 (erlang-push (list '\( token (current-column)) stack)
2119 (forward-char 1))
2121 ;; Close parenthesis
2122 ((= cs ?\))
2123 (while (memq (car (car stack)) '(|| ->))
2124 (erlang-pop stack))
2125 (cond ((eq (car (car stack)) '\()
2126 (erlang-pop stack))
2127 ((eq (car (car stack)) 'icr)
2128 (erlang-pop stack)
2129 ;; Normal catch not try-catch might have caused icr
2130 ;; and then incr should be removed and is not an error.
2131 (if (eq (car (car stack)) '\()
2132 (erlang-pop stack)
2133 (else
2134 (error "Missing `end'"))
2136 ((eq (car (car stack)) 'begin)
2137 (error "Missing `end'")
2139 (error "Unbalanced parenthesis"))
2141 (forward-char 1))
2143 ;; Character quote: Skip it and the quoted char.
2144 ((= cs ?/)
2145 (forward-char 2))
2147 ;; Character escape: Skip it and the escape sequence.
2148 ((= cs ?\\)
2149 (forward-char 1)
2150 (skip-syntax-forward "w"))
2152 ;; Everything else
2154 (forward-char 1)))
2155 (list stack token cs in-what)))
2157 (defun erlang-calculate-stack-indent (indent-point state)
2158 "From the given last position and state (stack) calculate indentation.
2159 Return nil if inside string, t if in a comment."
2160 (let* ((stack (and state (car state)))
2161 (token (nth 1 state))
2162 (stack-top (and stack (car stack))))
2163 (cond ((null state) ;No state
2165 ((nth 3 state)
2166 ;; Return nil or t.
2167 (eq (nth 3 state) 'comment))
2168 ((null stack)
2169 (if (looking-at "when[^_a-zA-Z0-9]")
2170 erlang-indent-guard
2172 ((eq (car stack-top) '\()
2173 ;; Element of list, tuple or part of an expression,
2174 (if (null erlang-argument-indent)
2175 ;; indent to next column.
2176 (1+ (nth 2 stack-top))
2177 (goto-char (nth 1 stack-top))
2178 (cond ((looking-at "[({]\\s *\\($\\|%\\)")
2179 ;; Line ends with parenthesis.
2180 (+ (erlang-indent-find-preceding-expr)
2181 erlang-argument-indent))
2183 ;; Indent to the same column as the first
2184 ;; argument.
2185 (goto-char (1+ (nth 1 stack-top)))
2186 (skip-chars-forward " \t")
2187 (current-column)))))
2188 ((eq (car stack-top) 'icr)
2189 ;; The default indentation is the column of the option
2190 ;; directly following the keyword. (This does not apply to
2191 ;; `case'.) Should no option be on the same line, the
2192 ;; indentation is the indentation of the keyword +
2193 ;; `erlang-indent-level'.
2195 ;; `after' should be indented to the save level as the
2196 ;; corresponding receive.
2197 (if (looking-at "after[^_a-zA-Z0-9]")
2198 (nth 2 stack-top)
2199 (save-excursion
2200 (goto-char (nth 1 stack-top))
2201 (if (looking-at "case[^_a-zA-Z0-9]")
2202 (+ (nth 2 stack-top) erlang-indent-level)
2203 (skip-chars-forward "a-z")
2204 (skip-chars-forward " \t")
2205 (if (memq (following-char) '(?% ?\n))
2206 (+ (nth 2 stack-top) erlang-indent-level)
2207 (current-column)))))
2208 (if (looking-at "catch[^_a-zA-Z0-9]")
2209 (nth 2 stack-top)
2210 (save-excursion
2211 (goto-char (nth 1 stack-top))
2212 (if (looking-at "case[^_a-zA-Z0-9]")
2213 (+ (nth 2 stack-top) erlang-indent-level)
2214 (skip-chars-forward "a-z")
2215 (skip-chars-forward " \t")
2216 (if (memq (following-char) '(?% ?\n))
2217 (+ (nth 2 stack-top) erlang-indent-level)
2218 (current-column)))))
2220 ;; Real indentation, where operators create extra indentation etc.
2221 ((memq (car stack-top) '(-> || begin))
2222 (goto-char (nth 1 stack-top))
2223 ;; Check if there is more code after the '->' on the
2224 ;; same line. If so use this indentation as base, else
2225 ;; use parent indentation + 2 * level as base.
2226 (let ((off erlang-indent-level)
2227 (skip 2))
2228 (cond ((null (cdr stack))) ; Top level in function.
2229 ((eq (car stack-top) 'begin)
2230 (setq skip 5))
2231 ((eq (car stack-top) '->)
2232 (setq off (* 2 erlang-indent-level))))
2233 (let ((base (erlang-indent-find-base stack indent-point off skip)))
2234 ;; Look at last thing to see how we are to move relative
2235 ;; to the base.
2236 (goto-char token)
2237 (cond ((looking-at "||\\|,\\|->\\|:-")
2238 base)
2239 ((erlang-at-keyword)
2240 (+ (current-column) erlang-indent-level))
2241 ((or (= (char-syntax (following-char)) ?.)
2242 (erlang-at-operator))
2243 (+ base erlang-indent-level))
2245 (goto-char indent-point)
2246 (cond ((memq (following-char) '(?\( ?{))
2247 ;; Function application or record.
2248 (+ (erlang-indent-find-preceding-expr)
2249 erlang-argument-indent))
2250 ;; Empty line, or end; treat it as the end of
2251 ;; the block. (Here we have a choice: should
2252 ;; the user be forced to reindent continued
2253 ;; lines, or should the "end" be reindented?)
2254 ((looking-at "\\(end\\|after\\|catch\\)[^_a-zA-Z0-9]\\|$")
2255 (if (eq (car (car stack)) '->)
2256 (erlang-pop stack))
2257 (if stack
2258 (erlang-caddr (car stack))
2260 ;; Avoid treating comments a continued line.
2261 ((= (following-char) ?%)
2262 base)
2263 ;; Continued line (e.g. line beginning
2264 ;; with an operator.)
2265 (t (+ base erlang-indent-level)))))))
2267 ((eq (car stack-top) 'when)
2268 (goto-char (nth 1 stack-top))
2269 (if (looking-at "when\\s *\\($\\|%\\)")
2270 (progn
2271 (erlang-pop stack)
2272 (if (and stack (eq (nth 0 (car stack)) 'icr))
2273 (progn
2274 (goto-char (nth 1 (car stack)))
2275 (+ (nth 2 (car stack)) erlang-indent-guard
2276 ;; receive XYZ or receive
2277 ;; XYZ
2278 (if (looking-at "[a-z]+\\s *\\($\\|%\\)")
2279 erlang-indent-level
2280 (* 2 erlang-indent-level))))
2281 erlang-indent-guard))
2282 ;; "when" is followed by code, let's indent to the same
2283 ;; column.
2284 (forward-char 4) ; Skip "when"
2285 (skip-chars-forward " \t")
2286 (current-column))))))
2289 (defun erlang-indent-find-base (stack indent-point &optional offset skip)
2290 "Find the base column for current stack."
2291 (or skip (setq skip 2))
2292 (or offset (setq offset erlang-indent-level))
2293 (save-excursion
2294 (let* ((stack-top (car stack)))
2295 (goto-char (nth 1 stack-top))
2296 (forward-char skip)
2297 (if (looking-at "\\s *\\($\\|%\\)")
2298 (progn
2299 (if (memq (car stack-top) '(-> ||))
2300 (erlang-pop stack))
2301 ;; Take parent identation + offset,
2302 ;; else just erlang-indent-level if no parent
2303 (if stack
2304 (+ (erlang-caddr (car stack))
2305 offset)
2306 erlang-indent-level))
2307 (erlang-skip-blank indent-point)
2308 (current-column)))))
2311 ;; Does not handle `begin' .. `end'.
2312 (defun erlang-indent-find-preceding-expr ()
2313 "Return the first column of the preceding expression.
2314 This assumes that the preceding expression is either simple
2315 \(i.e. an atom) or parenthesized."
2316 (save-excursion
2317 (forward-sexp -1)
2318 (let ((col (current-column)))
2319 (skip-chars-backward " \t")
2320 ;; Needed to match the colon in "'foo':'bar'".
2321 (if (not (memq (preceding-char) '(?# ?:)))
2323 (backward-char 1)
2324 (forward-sexp -1)
2325 (current-column)))))
2328 (defun erlang-skip-blank (&optional lim)
2329 "Skip over whitespace and comments until limit reached."
2330 (or lim (setq lim (point-max)))
2331 (let (stop)
2332 (while (and (not stop) (< (point) lim))
2333 (cond ((= (following-char) ?%)
2334 (skip-chars-forward "^\n" lim))
2335 ((= (following-char) ?\n)
2336 (skip-chars-forward "\n" lim))
2337 ((looking-at "\\s ")
2338 (if (re-search-forward "\\S " lim 'move)
2339 (forward-char -1)))
2341 (setq stop t))))
2342 stop))
2344 (defun erlang-at-keyword ()
2345 "Are we looking at an Erlang keyword which will increase indentation?"
2346 (looking-at (concat "\\(when\\|if\\|fun\\|case\\|begin\\|query\\|"
2347 "of\\|receive\\|after\\|catch\\)[^_a-zA-Z0-9]")))
2349 (defun erlang-at-operator ()
2350 "Are we looking at an Erlang operator?"
2351 (looking-at
2352 "\\(bnot\\|div\\|mod\\|band\\|bor\\|bxor\\|bsl\\|bsr\\)[^_a-zA-Z0-9]"))
2354 (defun erlang-comment-indent ()
2355 "Compute Erlang comment indentation.
2357 Used both by `indent-for-comment' and the Erlang specific indentation
2358 commands."
2359 (cond ((looking-at "%%") 0)
2360 ((looking-at "%")
2361 (or (erlang-calculate-indent)
2362 (current-indentation)))))
2365 ;;; Erlang movement commands
2367 ;; All commands below work as movement commands. I.e. if the point is
2368 ;; at the end of the clause, and the command `erlang-end-of-clause' is
2369 ;; executed, the point is moved to the end of the NEXT clause. (This
2370 ;; mimics the behaviour of `end-of-defun'.)
2372 ;; Personally I would like to rewrite them to be "pure", and add a set
2373 ;; of movement functions, like `erlang-next-clause',
2374 ;; `erlang-previous-clause', and the same for functions.
2376 ;; The current implementation makes it hopeless to use the functions as
2377 ;; subroutines in more complex commands. /andersl
2379 (defun erlang-beginning-of-clause (&optional arg)
2380 "Move backward to previous start of clause.
2381 With argument, do this that many times.
2382 Return t unless search stops due to end of buffer."
2383 (interactive "p")
2384 (or arg (setq arg 1))
2385 (if (< arg 0)
2386 ;; Step back to the end of the previous line, unless we are at
2387 ;; the beginning of the buffer. The reason for this move is
2388 ;; that the regexp below includes the last character of the
2389 ;; previous line.
2390 (if (bobp)
2391 (or (looking-at "\n")
2392 (forward-char 1))
2393 (forward-char -1)
2394 (if (looking-at "\\`\n")
2395 (forward-char 1))))
2396 ;; The regexp matches a function header that isn't
2397 ;; included in a string.
2398 (and (re-search-forward "\\(\\`\\|\\`\n\\|[^\\]\n\\)\\([a-z]\\|'\\|-\\)"
2399 nil 'move (- arg))
2400 (let ((beg (match-beginning 2)))
2401 (and beg (goto-char beg))
2402 t)))
2404 (defun erlang-end-of-clause (&optional arg)
2405 "Move to the end of the current clause.
2406 With argument, do this that many times."
2407 (interactive "p")
2408 (or arg (setq arg 1))
2409 (while (and (looking-at "[ \t]*[%\n]")
2410 (zerop (forward-line 1))))
2411 ;; Move to the next clause.
2412 (erlang-beginning-of-clause (- arg))
2413 (beginning-of-line);; Just to be sure...
2414 (let ((continue t))
2415 (while (and (not (bobp)) continue)
2416 (forward-line -1)
2417 (skip-chars-forward " \t")
2418 (if (looking-at "[%\n]")
2420 (end-of-line)
2421 (setq continue nil)))))
2423 (defun erlang-mark-clause ()
2424 "Put mark at end of clause, point at beginning."
2425 (interactive)
2426 (push-mark (point))
2427 (erlang-end-of-clause 1)
2428 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2429 ;; the region.
2430 (condition-case nil
2431 (push-mark (point) nil t)
2432 (error (push-mark (point))))
2433 (erlang-beginning-of-clause 1)
2434 ;; The above function deactivates the mark.
2435 (if (boundp 'deactivate-mark)
2436 (funcall (symbol-function 'set) 'deactivate-mark nil)))
2438 (defun erlang-beginning-of-function (&optional arg)
2439 "Move backward to previous start of function.
2440 With positive argument, do this that many times.
2441 With negative argument, search forward.
2443 Return t unless search stops due to end of buffer."
2444 (interactive "p")
2445 (or arg (setq arg 1))
2446 (cond
2447 ;; Search backward
2448 ((> arg 0)
2449 (while (and (> arg 0)
2450 (and (erlang-beginning-of-clause 1)
2451 (let ((start (point))
2452 (name (erlang-name-of-function))
2453 (arity (erlang-get-function-arity)))
2454 ;; Note: "arity" is nil for e.g. "-import", hence
2455 ;; two "-import" clauses are not considered to
2456 ;; be part of the same function.
2457 (while (and (erlang-beginning-of-clause 1)
2458 (string-equal name
2459 (erlang-name-of-function))
2460 arity
2461 (equal arity
2462 (erlang-get-function-arity)))
2463 (setq start (point)))
2464 (goto-char start)
2465 t)))
2466 (setq arg (1- arg))))
2467 ;; Search forward
2468 ((< arg 0)
2469 (end-of-line)
2470 (erlang-beginning-of-clause 1)
2471 ;; Step -arg functions forward.
2472 (while (and (< arg 0)
2473 ;; Step one function forward, or stop if the end of
2474 ;; the buffer was reached. Return t if we found the
2475 ;; function.
2476 (let ((name (erlang-name-of-function))
2477 (arity (erlang-get-function-arity))
2478 (found (erlang-beginning-of-clause -1)))
2479 (while (and found
2480 (string-equal name (erlang-name-of-function))
2481 arity
2482 (equal arity
2483 (erlang-get-function-arity)))
2484 (setq found (erlang-beginning-of-clause -1)))
2485 found))
2486 (setq arg (1+ arg)))))
2487 (zerop arg))
2490 (defun erlang-end-of-function (&optional arg)
2491 "Move forward to next end of function.
2493 With argument, do this that many times.
2494 With negative argument go towards the beginning of the buffer."
2495 (interactive "p")
2496 (or arg (setq arg 1))
2497 (let ((first t))
2498 ;; Forward
2499 (while (and (> arg 0) (< (point) (point-max)))
2500 (let ((pos (point)))
2501 (while (progn
2502 (if (and first
2503 (progn
2504 (forward-char 1)
2505 (erlang-beginning-of-clause 1)))
2507 (or (bobp) (forward-char -1))
2508 (erlang-beginning-of-clause -1))
2509 (setq first nil)
2510 (erlang-pass-over-function)
2511 (skip-chars-forward " \t")
2512 (if (looking-at "[%\n]")
2513 (forward-line 1))
2514 (<= (point) pos))))
2515 (setq arg (1- arg)))
2516 ;; Backward
2517 (while (< arg 0)
2518 (let ((pos (point)))
2519 (erlang-beginning-of-clause 1)
2520 (erlang-pass-over-function)
2521 (forward-line 1)
2522 (if (>= (point) pos)
2523 (if (erlang-beginning-of-function 2)
2524 (progn
2525 (erlang-pass-over-function)
2526 (skip-chars-forward " \t")
2527 (if (looking-at "[%\n]")
2528 (forward-line 1)))
2529 (goto-char (point-min)))))
2530 (setq arg (1+ arg)))))
2532 (eval-and-compile
2533 (if (default-boundp 'beginning-of-defun-function)
2534 (defalias 'erlang-mark-function 'mark-defun)
2535 (defun erlang-mark-function ()
2536 "Put mark at end of function, point at beginning."
2537 (interactive)
2538 (push-mark (point))
2539 (erlang-end-of-function 1)
2540 ;; Sets the region. In Emacs 19 and XEmacs, we want to activate
2541 ;; the region.
2542 (condition-case nil
2543 (push-mark (point) nil t)
2544 (error (push-mark (point))))
2545 (erlang-beginning-of-function 1)
2546 ;; The above function deactivates the mark.
2547 (if (boundp 'deactivate-mark)
2548 (funcall (symbol-function 'set) 'deactivate-mark nil)))))
2550 (defun erlang-pass-over-function ()
2551 (while (progn
2552 (erlang-skip-blank)
2553 (and (not (looking-at "\\.\\(\\s \\|\n\\|\\s<\\)"))
2554 (not (eobp))))
2555 (forward-sexp 1))
2556 (if (not (eobp))
2557 (forward-char 1)))
2559 (defun erlang-name-of-function ()
2560 (save-excursion
2561 ;; Skip over attribute leader.
2562 (if (looking-at "-[ \t]*")
2563 (re-search-forward "-[ \t]*" nil 'move))
2564 (let ((start (point)))
2565 (forward-sexp 1)
2566 (buffer-substring start (point)))))
2569 ;;; Miscellaneous
2571 (defun erlang-fill-paragraph (&optional justify)
2572 "Like \\[fill-paragraph], but handle Erlang comments.
2573 If any of the current line is a comment, fill the comment or the
2574 paragraph of it that point is in, preserving the comment's indentation
2575 and initial `%':s."
2576 (interactive "P")
2577 (let ((has-comment nil)
2578 ;; If has-comment, the appropriate fill-prefix for the comment.
2579 comment-fill-prefix)
2580 ;; Figure out what kind of comment we are looking at.
2581 (save-excursion
2582 (beginning-of-line)
2583 (cond
2584 ;; Find the command prefix.
2585 ((looking-at (concat "\\s *" comment-start-skip))
2586 (setq has-comment t)
2587 (setq comment-fill-prefix (buffer-substring (match-beginning 0)
2588 (match-end 0))))
2589 ;; A line with some code, followed by a comment? Remember that the
2590 ;; % which starts the comment shouldn't be part of a string or
2591 ;; character.
2592 ((progn
2593 (while (not (looking-at "%\\|$"))
2594 (skip-chars-forward "^%\n\"\\\\")
2595 (cond
2596 ((eq (char-after (point)) ?\\) (forward-char 2))
2597 ((eq (char-after (point)) ?\") (forward-sexp 1))))
2598 (looking-at comment-start-skip))
2599 (setq has-comment t)
2600 (setq comment-fill-prefix
2601 (concat (make-string (current-column) ? )
2602 (buffer-substring (match-beginning 0) (match-end 0)))))))
2603 (if (not has-comment)
2604 (fill-paragraph justify)
2605 ;; Narrow to include only the comment, and then fill the region.
2606 (save-restriction
2607 (narrow-to-region
2608 ;; Find the first line we should include in the region to fill.
2609 (save-excursion
2610 (while (and (zerop (forward-line -1))
2611 (looking-at "^\\s *%")))
2612 ;; We may have gone to far. Go forward again.
2613 (or (looking-at "^\\s *%")
2614 (forward-line 1))
2615 (point))
2616 ;; Find the beginning of the first line past the region to fill.
2617 (save-excursion
2618 (while (progn (forward-line 1)
2619 (looking-at "^\\s *%")))
2620 (point)))
2621 ;; Lines with only % on them can be paragraph boundaries.
2622 (let ((paragraph-start (concat paragraph-start "\\|^[ \t%]*$"))
2623 (paragraph-separate (concat paragraph-start "\\|^[ \t%]*$"))
2624 (fill-prefix comment-fill-prefix))
2625 (fill-paragraph justify))))))
2628 (defun erlang-uncomment-region (beg end)
2629 "Uncomment all commented lines in the region."
2630 (interactive "r")
2631 (comment-region beg end -1))
2634 (defun erlang-generate-new-clause ()
2635 "Create additional Erlang clause header.
2637 Parses the source file for the name of the current Erlang function.
2638 Create the header containing the name, A pair of parentheses,
2639 and an arrow. The space between the function name and the
2640 first parenthesis is preserved. The point is placed between
2641 the parentheses."
2642 (interactive)
2643 (let ((name (save-excursion
2644 (and (erlang-beginning-of-clause)
2645 (erlang-get-function-name t))))
2646 (arrow (save-excursion
2647 (and (erlang-beginning-of-clause)
2648 (erlang-get-function-arrow)))))
2649 (if (or (null arrow) (null name))
2650 (error "Can't find name of current Erlang function"))
2651 (if (and (bolp) (eolp))
2653 (end-of-line)
2654 (newline))
2655 (insert name)
2656 (save-excursion
2657 (insert ") " arrow))
2658 (if erlang-new-clause-with-arguments
2659 (erlang-clone-arguments))))
2662 (defun erlang-clone-arguments ()
2663 "Insert, at the point, the argument list of the previous clause.
2665 The mark is set at the beginning of the inserted text, the point
2666 at the end."
2667 (interactive)
2668 (let ((args (save-excursion
2669 (beginning-of-line)
2670 (and (erlang-beginning-of-clause)
2671 (erlang-get-function-arguments))))
2672 (p (point)))
2673 (if (null args)
2674 (error "Can't clone argument list"))
2675 (insert args)
2676 (set-mark p)))
2678 ;;; Information retrieval functions.
2680 (defun erlang-buffer-substring (beg end)
2681 "Like `buffer-substring-no-properties'.
2682 Although, this function works on all versions of Emacs."
2683 (if (fboundp 'buffer-substring-no-properties)
2684 (funcall (symbol-function 'buffer-substring-no-properties) beg end)
2685 (buffer-substring beg end)))
2688 (defun erlang-get-module ()
2689 "Return the name of the module as specified by `-module'.
2691 Return nil if file contains no `-module' attribute."
2692 (save-excursion
2693 (save-restriction
2694 (widen)
2695 (goto-char (point-min))
2696 (let ((md (match-data)))
2697 (unwind-protect
2698 (if (re-search-forward
2699 (eval-when-compile
2700 (concat "^-module\\s *(\\s *\\(\\("
2701 erlang-atom-regexp
2702 "\\)?\\)\\s *)\\s *\\."))
2703 (point-max) t)
2704 (erlang-remove-quotes
2705 (erlang-buffer-substring (match-beginning 1)
2706 (match-end 1)))
2707 nil)
2708 (store-match-data md))))))
2711 (defun erlang-get-module-from-file-name (&optional file)
2712 "Extract the module name from a file name.
2714 First, the directory part is removed. Second, the part of the file name
2715 matching `erlang-file-name-extension-regexp' is removed.
2717 Should the match fail, nil is returned.
2719 By modifying `erlang-file-name-extension-regexp' to match files other
2720 than Erlang source files, Erlang specific functions could be applied on
2721 non-Erlang files. Most notably; the support for Erlang modules in the
2722 tags system could be used by files written in other languages."
2723 (or file (setq file buffer-file-name))
2724 (if (null file)
2726 (setq file (file-name-nondirectory file))
2727 (if (string-match erlang-file-name-extension-regexp file)
2728 (substring file 0 (match-beginning 0))
2729 nil)))
2732 ;; Used by `erlang-get-export' and `erlang-get-import'.
2734 (defun erlang-get-function-arity-list ()
2735 "Parse list of `function/arity' as used by `-import' and `-export'.
2737 Point must be before the opening bracket. When the
2738 function returns the point will be placed after the closing bracket.
2740 The function does not return an error if the list is incorrectly
2741 formatted.
2743 Return list of (function . arity). The order of the returned list
2744 corresponds to the order of the parsed Erlang list."
2745 (let ((res '()))
2746 (erlang-skip-blank)
2747 (forward-char 1)
2748 (if (not (eq (preceding-char) ?\[))
2749 '() ; Not looking at an Erlang list.
2750 (while ; Note: `while' has no body.
2751 (progn
2752 (erlang-skip-blank)
2753 (and (looking-at (eval-when-compile
2754 (concat erlang-atom-regexp "/\\([0-9]+\\)\\>")))
2755 (progn
2756 (setq res (cons
2757 (cons
2758 (erlang-remove-quotes
2759 (erlang-buffer-substring
2760 (match-beginning 1) (match-end 1)))
2761 (string-to-int
2762 (erlang-buffer-substring
2763 (match-beginning
2764 (+ 1 erlang-atom-regexp-matches))
2765 (match-end
2766 (+ 1 erlang-atom-regexp-matches)))))
2767 res))
2768 (goto-char (match-end 0))
2769 (erlang-skip-blank)
2770 (forward-char 1)
2771 ;; Test if there are more exported functions.
2772 (eq (preceding-char) ?,))))))
2773 (nreverse res)))
2776 ;;; Note that `-export' and the open parenthesis must be written on
2777 ;;; the same line.
2779 (defun erlang-get-export ()
2780 "Return a list of `(function . arity)' as specified by `-export'."
2781 (save-excursion
2782 (goto-char (point-min))
2783 (let ((md (match-data))
2784 (res '()))
2785 (unwind-protect
2786 (progn
2787 (while (re-search-forward "^-export\\s *(" (point-max) t)
2788 (erlang-skip-blank)
2789 (setq res (nconc res (erlang-get-function-arity-list))))
2790 res)
2791 (store-match-data md)))))
2794 (defun erlang-get-import ()
2795 "Parse an Erlang source file for imported functions.
2797 Return an alist with module name as car part and list of conses containing
2798 function and arity as cdr part."
2799 (save-excursion
2800 (goto-char (point-min))
2801 (let ((md (match-data))
2802 (res '()))
2803 (unwind-protect
2804 (progn
2805 (while (re-search-forward "^-import\\s *(" (point-max) t)
2806 (erlang-skip-blank)
2807 (if (looking-at erlang-atom-regexp)
2808 (let ((module (erlang-remove-quotes
2809 (erlang-buffer-substring
2810 (match-beginning 0)
2811 (match-end 0)))))
2812 (goto-char (match-end 0))
2813 (erlang-skip-blank)
2814 (if (eq (following-char) ?,)
2815 (progn
2816 (forward-char 1)
2817 (erlang-skip-blank)
2818 (let ((funcs (erlang-get-function-arity-list))
2819 (pair (assoc module res)))
2820 (if pair
2821 (setcdr pair (nconc (cdr pair) funcs))
2822 (setq res (cons (cons module funcs)
2823 res)))))))))
2824 (nreverse res))
2825 (store-match-data md)))))
2828 (defun erlang-get-function-name (&optional arg)
2829 "Return name of current function, or nil.
2831 If optional argument is non-nil, everything up to and including
2832 the first `(' is returned.
2834 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2835 (save-excursion
2836 (if (not (eobp)) (forward-char 1))
2837 (and (erlang-beginning-of-clause)
2838 (erlang-get-function-name t)))"
2839 (let ((n (if arg 0 1)))
2840 (and (looking-at (eval-when-compile
2841 (concat "^" erlang-atom-regexp "\\s *(")))
2842 (erlang-buffer-substring (match-beginning n) (match-end n)))))
2845 (defun erlang-get-function-arrow ()
2846 "Return arrow of current function, could be \"->\", \":-\" or nil.
2848 The \":-\" arrow is used by mnesia queries.
2850 Normally used in conjunction with `erlang-beginning-of-clause', e.g.:
2851 (save-excursion
2852 (if (not (eobp)) (forward-char 1))
2853 (and (erlang-beginning-of-clause)
2854 (erlang-get-function-arrow)))"
2855 (and
2856 (save-excursion
2857 (re-search-forward "[^-:]*-\\|:" (point-max) t)
2858 (erlang-buffer-substring (- (point) 1) (+ (point) 1)))))
2860 (defun erlang-get-function-arity ()
2861 "Return the number of arguments of function at point, or nil."
2862 (and (looking-at (eval-when-compile
2863 (concat "^" erlang-atom-regexp "\\s *(")))
2864 (save-excursion
2865 (goto-char (match-end 0))
2866 (condition-case nil
2867 (let ((res 0)
2868 (cont t))
2869 (while cont
2870 (cond ((eobp)
2871 (setq res nil)
2872 (setq cont nil))
2873 ((looking-at "\\s *)")
2874 (setq cont nil))
2875 ((looking-at "\\s *\\($\\|%\\)")
2876 (forward-line 1))
2877 ((looking-at "\\s *,")
2878 (setq res (+ 1 res))
2879 (goto-char (match-end 0)))
2881 (when (zerop res)
2882 (setq res (+ 1 res)))
2883 (forward-sexp 1))))
2884 res)
2885 (error nil)))))
2887 (defun erlang-get-function-arguments ()
2888 "Return arguments of current function, or nil."
2889 (if (not (looking-at (eval-when-compile
2890 (concat "^" erlang-atom-regexp "\\s *("))))
2892 (save-excursion
2893 (condition-case nil
2894 (let ((start (match-end 0)))
2895 (goto-char (- start 1))
2896 (forward-sexp)
2897 (erlang-buffer-substring start (- (point) 1)))
2898 (error nil)))))
2901 (defun erlang-get-function-under-point ()
2902 "Return the module and function under the point, or nil.
2904 Should no explicit module name be present at the point, the
2905 list of imported functions is searched.
2907 The following could be returned:
2908 (\"module\" \"function\") -- Both module and function name found.
2909 (nil \"function\") -- No module name was found.
2910 nil -- No function name found
2912 In the future the list may contain more elements."
2913 (save-excursion
2914 (let ((md (match-data))
2915 (res nil))
2916 (if (eq (char-syntax (following-char)) ? )
2917 (skip-chars-backward " \t"))
2918 (skip-chars-backward "a-zA-Z0-9_:'")
2919 (cond ((looking-at (eval-when-compile
2920 (concat erlang-atom-regexp ":" erlang-atom-regexp)))
2921 (setq res (list
2922 (erlang-remove-quotes
2923 (erlang-buffer-substring
2924 (match-beginning 1) (match-end 1)))
2925 (erlang-remove-quotes
2926 (erlang-buffer-substring
2927 (match-beginning (1+ erlang-atom-regexp-matches))
2928 (match-end (1+ erlang-atom-regexp-matches)))))))
2929 ((looking-at erlang-atom-regexp)
2930 (let ((fk (erlang-remove-quotes
2931 (erlang-buffer-substring
2932 (match-beginning 0) (match-end 0))))
2933 (mod nil)
2934 (imports (erlang-get-import)))
2935 (while (and imports (null mod))
2936 (if (assoc fk (cdr (car imports)))
2937 (setq mod (car (car imports)))
2938 (setq imports (cdr imports))))
2939 (setq res (list mod fk)))))
2940 (store-match-data md)
2941 res)))
2944 ;; TODO: Escape single quotes inside the string without
2945 ;; replace-regexp-in-string.
2946 (defun erlang-add-quotes-if-needed (str)
2947 "Return STR, possibly with quotes."
2948 (if (and (stringp str)
2949 (not (string-match (eval-when-compile
2950 (concat "\\`" erlang-atom-regexp "\\'")) str)))
2951 (progn (if (fboundp 'replace-regexp-in-string)
2952 (setq str (replace-regexp-in-string "'" "\\'" str t t )))
2953 (concat "'" str "'"))
2954 str))
2957 (defun erlang-remove-quotes (str)
2958 "Return STR without quotes, if present."
2959 (let ((md (match-data)))
2960 (prog1
2961 (if (string-match "\\`'\\(.*\\)'\\'" str)
2962 (substring str 1 -1)
2963 str)
2964 (store-match-data md))))
2967 ;;; Check module name
2969 ;; The function `write-file', bound to C-x C-w, calls
2970 ;; `set-visited-file-name' which clears the hook. :-(
2971 ;; To make sure that the hook always is present, we advise
2972 ;; `set-visited-file-name'.
2973 (defun erlang-check-module-name-init ()
2974 "Initialize the functionality to compare file and module names.
2976 Unless we have `before-save-hook', we redefine the function
2977 `set-visited-file-name' since it clears the variable
2978 `local-write-file-hooks'. The original function definition is
2979 stored in `erlang-orig-set-visited-file-name'."
2980 (if (boundp 'before-save-hook)
2981 ;; If we have that, `make-local-hook' is obsolete.
2982 (add-hook 'before-save-hook 'erlang-check-module-name nil t)
2983 (require 'advice)
2984 (unless (ad-advised-definition-p 'set-visited-file-name)
2985 (defadvice set-visited-file-name (after erlang-set-visited-file-name
2986 activate)
2987 (if (eq major-mode 'erlang-mode)
2988 (add-hook 'local-write-file-hooks 'erlang-check-module-name))))
2989 (add-hook 'local-write-file-hooks 'erlang-check-module-name)))
2992 (defun erlang-check-module-name ()
2993 "If the module name doesn't match file name, ask for permission to change.
2995 The variable `erlang-check-module-name' controls the behaviour of this
2996 function. It it is nil, this function does nothing. If it is t, the
2997 source is silently changed. If it is set to the atom `ask', the user
2998 is prompted.
3000 This function is normally placed in the hook `local-write-file-hooks'."
3001 (if erlang-check-module-name
3002 (let ((mn (erlang-get-module))
3003 (fn (erlang-get-module-from-file-name (buffer-file-name))))
3004 (if (and (stringp mn) (stringp fn))
3005 (or (string-equal mn fn)
3006 (if (or (eq erlang-check-module-name t)
3007 (y-or-n-p
3008 "Module does not match file name. Modify source? "))
3009 (save-excursion
3010 (save-restriction
3011 (widen)
3012 (goto-char (point-min))
3013 (if (re-search-forward
3014 (eval-when-compile
3015 (concat "^-module\\s *(\\s *\\(\\("
3016 erlang-atom-regexp
3017 "\\)?\\)\\s *)\\s *\\."))
3018 (point-max) t)
3019 (progn
3020 (goto-char (match-beginning 1))
3021 (delete-region (match-beginning 1)
3022 (match-end 1))
3023 (insert fn))))))))))
3024 ;; Must return nil since it is added to `local-write-file-hook'.
3025 nil)
3028 ;;; Electric functions.
3030 (defun erlang-electric-semicolon (&optional arg)
3031 "Insert a semicolon character and possibly a prototype for the next line.
3033 The variable `erlang-electric-semicolon-criteria' states a criterion,
3034 when fulfilled a newline is inserted, the next line is indented and a
3035 prototype for the next line is inserted. Normally the prototype
3036 consists of \" ->\". Should the semicolon end the clause a new clause
3037 header is generated.
3039 The variable `erlang-electric-semicolon-insert-blank-lines' controls
3040 the number of blank lines inserted between the current line and new
3041 function header.
3043 Behaves just like the normal semicolon when supplied with a
3044 numerical arg, point is inside string or comment, or when there are
3045 non-whitespace characters following the point on the current line."
3046 (interactive "P")
3047 (self-insert-command (prefix-numeric-value arg))
3048 (if (or arg
3049 (and (listp erlang-electric-commands)
3050 (not (memq 'erlang-electric-semicolon
3051 erlang-electric-commands)))
3052 (erlang-in-literal)
3053 (not (looking-at "\\s *\\(%.*\\)?$"))
3054 (null (erlang-test-criteria-list
3055 erlang-electric-semicolon-criteria)))
3056 (setq erlang-electric-newline-inhibit nil)
3057 (setq erlang-electric-newline-inhibit t)
3058 (undo-boundary)
3059 (end-of-line)
3060 (newline)
3061 (if (condition-case nil
3062 (progn (erlang-indent-line) t)
3063 (error (if (bolp) (delete-backward-char 1))))
3064 (if (not (bolp))
3065 (save-excursion
3066 (insert " ->"))
3067 (condition-case nil
3068 (progn
3069 (erlang-generate-new-clause)
3070 (if erlang-electric-semicolon-insert-blank-lines
3071 (save-excursion
3072 (beginning-of-line)
3073 (newline
3074 erlang-electric-semicolon-insert-blank-lines))))
3075 (error (if (bolp) (delete-backward-char 1))))))))
3078 (defun erlang-electric-comma (&optional arg)
3079 "Insert a comma character and possibly a new indented line.
3080 The variable `erlang-electric-comma-criteria' states a criterion,
3081 when fulfilled a newline is inserted and the next line is indented.
3083 Behaves just like the normal comma when supplied with a
3084 numerical arg, point is inside string or comment, or when there are
3085 non-whitespace characters following the point on the current line."
3086 (interactive "P")
3088 (self-insert-command (prefix-numeric-value arg))
3090 (if (or arg
3091 (and (listp erlang-electric-commands)
3092 (not (memq 'erlang-electric-comma erlang-electric-commands)))
3093 (erlang-in-literal)
3094 (not (looking-at "\\s *\\(%.*\\)?$"))
3095 (null (erlang-test-criteria-list
3096 erlang-electric-comma-criteria)))
3097 (setq erlang-electric-newline-inhibit nil)
3098 (setq erlang-electric-newline-inhibit t)
3099 (undo-boundary)
3100 (end-of-line)
3101 (newline)
3102 (condition-case nil
3103 (erlang-indent-line)
3104 (error (if (bolp) (delete-backward-char 1))))))
3106 (defun erlang-electric-lt (&optional arg)
3107 "Insert a less-than sign, and optionally mark it as an open paren."
3109 (interactive "p")
3111 (self-insert-command arg)
3113 ;; Was this the second char in bit-syntax open (`<<')?
3114 (unless (< (point) 2)
3115 (save-excursion
3116 (backward-char 2)
3117 (when (and (eq (char-after (point)) ?<)
3118 (not (eq (get-text-property (point) 'category)
3119 'bitsyntax-open-inner)))
3120 ;; Then mark the two chars...
3121 (put-text-property (point) (1+ (point))
3122 'category 'bitsyntax-open-outer)
3123 (forward-char 1)
3124 (put-text-property (point) (1+ (point))
3125 'category 'bitsyntax-open-inner)
3126 ;;...and unmark any subsequent less-than chars.
3127 (forward-char 1)
3128 (while (eq (char-after (point)) ?<)
3129 (remove-text-properties (point) (1+ (point))
3130 '(category nil))
3131 (forward-char 1))))))
3133 (defun erlang-after-bitsyntax-close ()
3134 "Return t if point is immediately after a bit-syntax close parenthesis (`>>')."
3135 (and (>= (point) 2)
3136 (save-excursion
3137 (backward-char 2)
3138 (and (eq (char-after (point)) ?>)
3139 (not (eq (get-text-property (point) 'category)
3140 'bitsyntax-close-outer))))))
3142 (defun erlang-after-arrow ()
3143 "Return true if point is immediately after a function arrow (`->')."
3144 (and (>= (point) 2)
3145 (and
3146 (save-excursion
3147 (backward-char)
3148 (eq (char-before (point)) ?-))
3149 (or (not (listp erlang-electric-commands))
3150 (memq 'erlang-electric-gt
3151 erlang-electric-commands))
3152 (not (erlang-in-literal))
3153 (looking-at "\\s *\\(%.*\\)?$")
3154 (erlang-test-criteria-list erlang-electric-arrow-criteria))))
3157 (defun erlang-electric-gt (&optional arg)
3158 "Insert a greater-than sign, and optionally mark it as a close paren."
3160 (interactive "p")
3162 (self-insert-command arg)
3164 (cond
3165 ;; Did we just write a bit-syntax close (`>>')?
3166 ((erlang-after-bitsyntax-close)
3167 (save-excursion
3168 ;; Then mark the two chars...
3169 (backward-char 2)
3170 (put-text-property (point) (1+ (point))
3171 'category 'bitsyntax-close-inner)
3172 (forward-char)
3173 (put-text-property (point) (1+ (point))
3174 'category 'bitsyntax-close-outer)
3175 ;;...and unmark any subsequent greater-than chars.
3176 (forward-char)
3177 (while (eq (char-after (point)) ?>)
3178 (remove-text-properties (point) (1+ (point))
3179 '(category nil))
3180 (forward-char))))
3182 ;; Did we just write a function arrow (`->')?
3183 ((erlang-after-arrow)
3184 (let ((erlang-electric-newline-inhibit t))
3185 (undo-boundary)
3186 (end-of-line)
3187 (newline)
3188 (condition-case nil
3189 (erlang-indent-line)
3190 (error (if (bolp) (delete-backward-char 1))))))
3192 ;; Then it's just a plain greater-than.
3194 nil)))
3197 (defun erlang-electric-arrow\ off (&optional arg)
3198 "Insert a '>'-sign and possibly a new indented line.
3200 This command is only `electric' when the `>' is part of an `->' arrow.
3201 The variable `erlang-electric-arrow-criteria' states a sequence of
3202 criteria, which decides when a newline should be inserted and the next
3203 line indented.
3205 It behaves just like the normal greater than sign when supplied with a
3206 numerical arg, point is inside string or comment, or when there are
3207 non-whitespace characters following the point on the current line.
3209 After being split/merged into `erlang-after-arrow' and
3210 `erlang-electric-gt', it is now unused and disabled."
3211 (interactive "P")
3212 (let ((prec (preceding-char)))
3213 (self-insert-command (prefix-numeric-value arg))
3214 (if (or arg
3215 (and (listp erlang-electric-commands)
3216 (not (memq 'erlang-electric-arrow
3217 erlang-electric-commands)))
3218 (not (eq prec ?-))
3219 (erlang-in-literal)
3220 (not (looking-at "\\s *\\(%.*\\)?$"))
3221 (null (erlang-test-criteria-list
3222 erlang-electric-arrow-criteria)))
3223 (setq erlang-electric-newline-inhibit nil)
3224 (setq erlang-electric-newline-inhibit t)
3225 (undo-boundary)
3226 (end-of-line)
3227 (newline)
3228 (condition-case nil
3229 (erlang-indent-line)
3230 (error (if (bolp) (delete-backward-char 1)))))))
3233 (defun erlang-electric-newline (&optional arg)
3234 "Break line at point and indent, continuing comment if within one.
3235 The variable `erlang-electric-newline-criteria' states a criterion,
3236 when fulfilled a newline is inserted and the next line is indented.
3238 Should the current line begin with a comment, and the variable
3239 `comment-multi-line' be non-nil, a new comment start is inserted.
3241 Should the previous command be another electric command we assume that
3242 the user pressed newline out of old habit, hence we will do nothing."
3243 (interactive "P")
3244 (cond ((and (not arg)
3245 erlang-electric-newline-inhibit
3246 (memq last-command erlang-electric-newline-inhibit-list))
3247 ()) ; Do nothing!
3248 ((or arg
3249 (and (listp erlang-electric-commands)
3250 (not (memq 'erlang-electric-newline
3251 erlang-electric-commands)))
3252 (null (erlang-test-criteria-list
3253 erlang-electric-newline-criteria)))
3254 (newline (prefix-numeric-value arg)))
3256 (if (and comment-multi-line
3257 (save-excursion
3258 (beginning-of-line)
3259 (looking-at (concat "\\s *" comment-start-skip))))
3260 (let ((str (buffer-substring
3261 (or (match-end 1) (match-beginning 0))
3262 (min (match-end 0) (point)))))
3263 (newline)
3264 (undo-boundary)
3265 (insert str))
3266 (newline-and-indent)))))
3269 (defun erlang-test-criteria-list (criteria)
3270 "Given a list of criterion functions, test if criteria are fulfilled.
3272 Each element in the criteria list can a function returning nil, t or
3273 the atom `stop'. t means that the criterion is fulfilled, `stop' means
3274 that it isn't fulfilled and that the search should stop,
3275 and nil means continue searching.
3277 Should the list contain the atom t the criterion is assumed to be
3278 fulfilled, unless preceded by a function returning `stop', of course.
3280 Should the argument be the atom t instead of a list, the criterion is
3281 assumed to be trivially true.
3283 Should all functions return nil, the criteria are assumed not to be
3284 fulfilled.
3286 Return t if criteria fulfilled, nil otherwise."
3287 (if (eq criteria t)
3289 (save-excursion
3290 (let ((answer nil))
3291 (while (and criteria (null answer))
3292 (if (eq (car criteria) t)
3293 (setq answer t)
3294 (setq answer (funcall (car criteria))))
3295 (setq criteria (cdr criteria)))
3296 (if (and answer (not (eq answer 'stop)))
3298 nil)))))
3301 (defun erlang-in-literal (&optional lim)
3302 "Test if point is in string, quoted atom or comment.
3304 Return one of the three atoms `atom', `string', and `comment'.
3305 Should the point be inside none of the above mentioned types of
3306 context, nil is returned."
3307 (save-excursion
3308 (let* ((lim (or lim (save-excursion
3309 (erlang-beginning-of-clause)
3310 (point))))
3311 (state (if (fboundp 'syntax-ppss) ; post Emacs 21.3
3312 (syntax-ppss)
3313 (parse-partial-sexp lim (point)))))
3314 (cond
3315 ((eq (nth 3 state) ?') 'atom)
3316 ((nth 3 state) 'string)
3317 ((nth 4 state) 'comment)
3318 (t nil)))))
3321 (defun erlang-at-end-of-function-p ()
3322 "Test if point is at end of an Erlang function.
3324 This function is designed to be a member of a criteria list."
3325 (eq (save-excursion (erlang-skip-blank) (point))
3326 (save-excursion
3327 (erlang-beginning-of-function -1) (point))))
3330 (defun erlang-at-end-of-clause-p ()
3331 "Test if point is at end of an Erlang clause.
3333 This function is designed to be a member of a criteria list."
3334 (eq (save-excursion (erlang-skip-blank) (point))
3335 (save-excursion
3336 (erlang-beginning-of-clause -1) (point))))
3339 (defun erlang-stop-when-inside-argument-list ()
3340 "Return `stop' if inside parenthesis list, nil otherwise.
3342 Knows about the list comprehension syntax. When the point is
3343 after `||', `stop' is not returned.
3345 This function is designed to be a member of a criteria list."
3346 (save-excursion
3347 (condition-case nil
3348 (let ((orig-point (point))
3349 (state nil))
3350 (up-list -1)
3351 (if (not (eq (following-char) ?\[))
3352 'stop
3353 ;; Do not return `stop' when inside a list comprehension
3354 ;; construction. (The point must be after `||').
3355 (while (< (point) orig-point)
3356 (setq state (erlang-partial-parse (point) orig-point state)))
3357 (if (and (car state) (eq (car (car (car state))) '||))
3359 'stop)))
3360 (error
3361 nil))))
3364 (defun erlang-stop-when-at-guard ()
3365 "Return `stop' when at function guards.
3367 This function is designed to be a member of a criteria list."
3368 (save-excursion
3369 (beginning-of-line)
3370 (if (and (looking-at (eval-when-compile
3371 (concat "^" erlang-atom-regexp "\\s *(")))
3372 (not (looking-at
3373 (eval-when-compile
3374 (concat "^" erlang-atom-regexp ".*\\(->\\|:-\\)")))))
3375 'stop
3376 nil)))
3379 (defun erlang-next-lines-empty-p ()
3380 "Return non-nil if next lines are empty.
3382 The variable `erlang-next-lines-empty-threshold' contains the number
3383 of lines required to be empty.
3385 A line containing only spaces and tabs is considered empty.
3387 This function is designed to be a member of a criteria list."
3388 (and erlang-next-lines-empty-threshold
3389 (save-excursion
3390 (let ((left erlang-next-lines-empty-threshold)
3391 (cont t))
3392 (while (and cont (> left 0))
3393 (forward-line 1)
3394 (setq cont (looking-at "\\s *$"))
3395 (setq left (- left 1)))
3396 cont))))
3399 (defun erlang-at-keyword-end-p ()
3400 "Test if next readable token is the keyword end.
3402 This function is designed to be a member of a criteria list."
3403 (save-excursion
3404 (erlang-skip-blank)
3405 (looking-at "end[^_a-zA-Z0-9]")))
3408 ;; Erlang tags support which is aware of erlang modules.
3410 ;; Not yet implemented under XEmacs. (Hint: The Emacs 19 etags
3411 ;; package works under XEmacs.)
3413 (eval-when-compile
3414 (if (or (featurep 'bytecomp)
3415 (featurep 'byte-compile))
3416 (progn
3417 (require 'etags))))
3420 ;; Variables:
3422 (defvar erlang-tags-function-alist
3423 '((find-tag . erlang-find-tag)
3424 (find-tag-other-window . erlang-find-tag-other-window)
3425 (find-tag-regexp . erlang-find-tag-regexp)
3426 (find-tag-other-frame . erlang-find-tag-other-frame))
3427 "Alist of old tags commands and the replacement functions.")
3429 (defvar erlang-tags-installed nil
3430 "Non-nil when the Erlang tags system is installed.")
3431 (defvar erlang-tags-file-list '()
3432 "List of files in tag list. Used when finding tag on form `module:'.")
3433 (defvar erlang-tags-completion-table nil
3434 "Like `tags-completion-table', this table contains `tag' and `module:tag'.")
3435 (defvar erlang-tags-buffer-installed-p nil
3436 "Non-nil when Erlang module recognising functions installed.")
3437 (defvar erlang-tags-buffer-list '()
3438 "Temporary list of buffers.")
3439 (defvar erlang-tags-orig-completion-table nil
3440 "Temporary storage for `tags-completion-table'.")
3441 (defvar erlang-tags-orig-tag-order nil
3442 "Temporary storage for `find-tag-tag-order'.")
3443 (defvar erlang-tags-orig-regexp-tag-order nil
3444 "Temporary storage for `find-tag-regexp-tag-order'.")
3445 (defvar erlang-tags-orig-search-function nil
3446 "Temporary storage for `find-tag-search-function'.")
3447 (defvar erlang-tags-orig-regexp-search-function nil
3448 "Temporary storage for `find-tag-regexp-search-function'.")
3449 (defvar erlang-tags-orig-format-hooks nil
3450 "Temporary storage for `tags-table-format-hooks'.")
3452 (defun erlang-tags-init ()
3453 "Install an alternate version of tags, aware of Erlang modules.
3455 After calling this function, the tags functions are aware of
3456 Erlang modules. Tags can be entered on the for `module:tag' as well
3457 as on the old form `tag'.
3459 In the completion list, `module:tag' and `module:' shows up.
3461 Call this function from an appropriate init file, or add it to
3462 Erlang mode hook with the commands:
3463 (add-hook 'erlang-mode-hook 'erlang-tags-init)
3464 (add-hook 'erlang-shell-mode-hook 'erlang-tags-init)
3466 This function only works under Emacs 18 and Emacs 19. Currently, It
3467 is not implemented under XEmacs. (Hint: The Emacs 19 etags module
3468 works under XEmacs.)"
3469 (interactive)
3470 (cond ((= erlang-emacs-major-version 18)
3471 (require 'tags)
3472 (erlang-tags-define-keys (current-local-map))
3473 (setq erlang-tags-installed t))
3475 (require 'etags)
3476 ;; Test on a function available in the Emacs 19 version
3477 ;; of tags but not in the XEmacs version.
3478 (if (not (fboundp 'find-tag-noselect))
3480 (erlang-tags-define-keys (current-local-map))
3481 (setq erlang-tags-installed t)))))
3484 ;; Set all keys bound to `find-tag' et.al. in the global map and the
3485 ;; menu to `erlang-find-tag' et.al. in `map'.
3487 ;; The function `substitute-key-definition' does not work properly
3488 ;; in all version of Emacs.
3490 (defun erlang-tags-define-keys (map)
3491 "Bind tags commands to keymap MAP aware of Erlang modules."
3492 (let ((alist erlang-tags-function-alist))
3493 (while alist
3494 (let* ((old (car (car alist)))
3495 (new (cdr (car alist)))
3496 (keys (append (where-is-internal old global-map))))
3497 (while keys
3498 (define-key map (car keys) new)
3499 (setq keys (cdr keys))))
3500 (setq alist (cdr alist))))
3501 ;; Update the menu.
3502 (erlang-menu-substitute erlang-menu-base-items erlang-tags-function-alist)
3503 (erlang-menu-init))
3506 ;; There exists a variable `find-tag-default-function'. It is not used
3507 ;; since `complete-tag' uses it to get current word under point. In that
3508 ;; situation we don't want the module to be prepended.
3510 (defun erlang-find-tag-default ()
3511 "Return the default tag.
3512 Search `-import' list of imported functions.
3513 Single quotes are been stripped away."
3514 (let ((mod-func (erlang-get-function-under-point)))
3515 (cond ((null mod-func)
3516 nil)
3517 ((null (car mod-func))
3518 (nth 1 mod-func))
3520 (concat (car mod-func) ":" (nth 1 mod-func))))))
3523 ;; Return `t' since it is used inside `tags-loop-form'.
3524 ;;;###autoload
3525 (defun erlang-find-tag (modtagname &optional next-p regexp-p)
3526 "Like `find-tag'. Capable of retrieving Erlang modules.
3528 Tags can be given on the forms `tag', `module:', `module:tag'."
3529 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3530 (switch-to-buffer (erlang-find-tag-noselect modtagname next-p regexp-p))
3534 ;; Code mainly from `find-tag-other-window' in `etags.el'.
3535 ;;;###autoload
3536 (defun erlang-find-tag-other-window (tagname &optional next-p regexp-p)
3537 "Like `find-tag-other-window' but aware of Erlang modules."
3538 (interactive (erlang-tag-interactive
3539 "Find `module:tag' or `tag' other window: "))
3541 ;; This is to deal with the case where the tag is found in the
3542 ;; selected window's buffer; without this, point is moved in both
3543 ;; windows. To prevent this, we save the selected window's point
3544 ;; before doing find-tag-noselect, and restore it afterwards.
3545 (let* ((window-point (window-point (selected-window)))
3546 (tagbuf (erlang-find-tag-noselect tagname next-p regexp-p))
3547 (tagpoint (progn (set-buffer tagbuf) (point))))
3548 (set-window-point (prog1
3549 (selected-window)
3550 (switch-to-buffer-other-window tagbuf)
3551 ;; We have to set this new window's point; it
3552 ;; might already have been displaying a
3553 ;; different portion of tagbuf, in which case
3554 ;; switch-to-buffer-other-window doesn't set
3555 ;; the window's point from the buffer.
3556 (set-window-point (selected-window) tagpoint))
3557 window-point)))
3560 (defun erlang-find-tag-other-frame (tagname &optional next-p)
3561 "Like `find-tag-other-frame' but aware of Erlang modules."
3562 (interactive (erlang-tag-interactive
3563 "Find `module:tag' or `tag' other frame: "))
3564 (let ((pop-up-frames t))
3565 (erlang-find-tag-other-window tagname next-p)))
3568 (defun erlang-find-tag-regexp (regexp &optional next-p other-window)
3569 "Like `find-tag-regexp' but aware of Erlang modules."
3570 (interactive (if (fboundp 'find-tag-regexp)
3571 (erlang-tag-interactive
3572 "Find `module:regexp' or `regexp': ")
3573 (error "This version of Emacs can't find tags by regexps")))
3574 (funcall (if other-window
3575 'erlang-find-tag-other-window
3576 'erlang-find-tag)
3577 regexp next-p t))
3580 ;; Just like C-u M-. This could be added to the menu.
3581 (defun erlang-find-next-tag ()
3582 "Find next tag, like \\[find-tag] with prefix arg."
3583 (interactive)
3584 (let ((current-prefix-arg '(4)))
3585 (if erlang-tags-installed
3586 (call-interactively 'erlang-find-tag)
3587 (call-interactively 'find-tag))))
3590 ;; Mimics `find-tag-noselect' found in `etags.el', but uses `find-tag' to
3591 ;; be compatible with `tags.el'.
3593 ;; Handles three cases:
3594 ;; * `module:' Loop over all possible file names. Stop if a file-name
3595 ;; without extension and directory matches the module.
3597 ;; * `module:tag'
3598 ;; Emacs 19: Replace test functions with functions aware of
3599 ;; Erlang modules. Tricky because the etags system wasn't
3600 ;; built for these kind of operations...
3602 ;; Emacs 18: We loop over `find-tag' until we find a file
3603 ;; whose module matches the requested module. The
3604 ;; drawback is that a lot of files could be loaded into
3605 ;; Emacs.
3607 ;; * `tag' Just give it to `find-tag'.
3609 (defun erlang-find-tag-noselect (modtagname &optional next-p regexp-p)
3610 "Like `find-tag-noselect' but aware of Erlang modules."
3611 (interactive (erlang-tag-interactive "Find `module:tag' or `tag': "))
3612 (or modtagname
3613 (setq modtagname (symbol-value 'last-tag)))
3614 (funcall (symbol-function 'set) 'last-tag modtagname)
3615 ;; `tags.el' uses this variable to record how M-, would
3616 ;; know where to restart a tags command.
3617 (if (boundp 'tags-loop-form)
3618 (funcall (symbol-function 'set)
3619 'tags-loop-form '(erlang-find-tag nil t)))
3620 (save-window-excursion
3621 (cond
3622 ((string-match ":$" modtagname)
3623 ;; Only the module name was given. Read all files whose file name
3624 ;; match.
3625 (let ((modname (substring modtagname 0 (match-beginning 0)))
3626 (file nil))
3627 (if (not next-p)
3628 (save-excursion
3629 (visit-tags-table-buffer)
3630 (setq erlang-tags-file-list
3631 (funcall (symbol-function 'tags-table-files)))))
3632 (while (null file)
3633 (or erlang-tags-file-list
3634 (save-excursion
3635 (if (and (featurep 'etags)
3636 (funcall
3637 (symbol-function 'visit-tags-table-buffer) 'same)
3638 (funcall
3639 (symbol-function 'visit-tags-table-buffer) t))
3640 (setq erlang-tags-file-list
3641 (funcall (symbol-function 'tags-table-files)))
3642 (error "No %stags containing %s" (if next-p "more " "")
3643 modtagname))))
3644 (if erlang-tags-file-list
3645 (let ((this-module (erlang-get-module-from-file-name
3646 (car erlang-tags-file-list))))
3647 (if (and (stringp this-module)
3648 (string= modname this-module))
3649 (setq file (car erlang-tags-file-list)))
3650 (setq erlang-tags-file-list (cdr erlang-tags-file-list)))))
3651 (set-buffer (or (get-file-buffer file)
3652 (find-file-noselect file)))))
3654 ((string-match ":" modtagname)
3655 (if (boundp 'find-tag-tag-order)
3656 ;; Method one: Add module-recognising functions to the
3657 ;; list of order functions. However, the tags system
3658 ;; from Emacs 18, and derives thereof (read: XEmacs)
3659 ;; hasn't got this feature.
3660 (progn
3661 (erlang-tags-install-module-check)
3662 (unwind-protect
3663 (funcall (symbol-function 'find-tag)
3664 modtagname next-p regexp-p)
3665 (erlang-tags-remove-module-check)))
3666 ;; Method two: Call the tags system until a file matching
3667 ;; the module is found. This could result in that many
3668 ;; files are read. (e.g. The tag "foo:file" will take a
3669 ;; while to process.)
3670 (let* ((modname (substring modtagname 0 (match-beginning 0)))
3671 (tagname (substring modtagname (match-end 0) nil))
3672 (last-tag tagname)
3673 file)
3674 (while
3675 (progn
3676 (funcall (symbol-function 'find-tag) tagname next-p regexp-p)
3677 (setq next-p t)
3678 ;; Determine the module form the file name. (The
3679 ;; alternative, to check `-module', would make this
3680 ;; code useless for non-Erlang programs.)
3681 (setq file (erlang-get-module-from-file-name buffer-file-name))
3682 (not (and (stringp file)
3683 (string= modname file))))))))
3685 (funcall (symbol-function 'find-tag) modtagname next-p regexp-p)))
3686 (current-buffer))) ; Return the new buffer.
3689 ;; Process interactive arguments for erlang-find-tag-*.
3691 ;; Negative arguments work only for `etags', not `tags'. This is not
3692 ;; a problem since negative arguments means step back into the
3693 ;; history list, a feature not implemented in `tags'.
3695 (defun erlang-tag-interactive (prompt)
3696 (condition-case nil
3697 (require 'etags)
3698 (error
3699 (require 'tags)))
3700 (if current-prefix-arg
3701 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
3704 (let* ((default (erlang-find-tag-default))
3705 (prompt (if default
3706 (format "%s(default %s) " prompt default)
3707 prompt))
3708 (spec (if (featurep 'etags)
3709 (completing-read prompt 'erlang-tags-complete-tag)
3710 (read-string prompt))))
3711 (list (if (equal spec "")
3712 (or default (error "There is no default tag"))
3713 spec)))))
3716 ;; Search tag functions which are aware of Erlang modules. The tactic
3717 ;; is to store new search functions into the local variables of the
3718 ;; TAGS buffers. The variables are restored directly after the
3719 ;; search. The situation is complicated by the fact that new TAGS
3720 ;; files can be loaded during the search.
3722 ;; This code is Emacs 19 `etags' specific.
3724 (defun erlang-tags-install-module-check ()
3725 "Install our own tag search functions."
3726 ;; Make sure our functions are installed in TAGS files loaded
3727 ;; into Emacs while searching.
3728 ;; ?? tags-table-format-hooks isn't in Emacs 21 or XEmacs etags.
3729 (setq erlang-tags-orig-format-hooks
3730 (symbol-value 'tags-table-format-hooks))
3731 (funcall (symbol-function 'set) 'tags-table-format-hooks
3732 (cons 'erlang-tags-recognize-tags-table
3733 erlang-tags-orig-format-hooks))
3734 (setq erlang-tags-buffer-list '())
3735 ;; Install our functions in the TAGS files already resident.
3736 (save-excursion
3737 (let ((files (symbol-value 'tags-table-computed-list)))
3738 (while files
3739 (if (stringp (car files))
3740 (if (get-file-buffer (car files))
3741 (progn
3742 (set-buffer (get-file-buffer (car files)))
3743 (erlang-tags-install-local))))
3744 (setq files (cdr files))))))
3747 (defun erlang-tags-install-local ()
3748 "Install our tag search functions in current buffer."
3749 (if erlang-tags-buffer-installed-p
3751 ;; Mark this buffer as "installed" and record.
3752 (set (make-local-variable 'erlang-tags-buffer-installed-p) t)
3753 (setq erlang-tags-buffer-list
3754 (cons (current-buffer) erlang-tags-buffer-list))
3756 ;; Save the original values.
3757 (set (make-local-variable 'erlang-tags-orig-tag-order)
3758 (symbol-value 'find-tag-tag-order))
3759 (set (make-local-variable 'erlang-tags-orig-regexp-tag-order)
3760 (symbol-value 'find-tag-regexp-tag-order))
3761 (set (make-local-variable 'erlang-tags-orig-search-function)
3762 (symbol-value 'find-tag-search-function))
3763 (set (make-local-variable 'erlang-tags-orig-regexp-search-function)
3764 (symbol-value 'find-tag-regexp-search-function))
3766 ;; Install our own functions.
3767 (set (make-local-variable 'find-tag-search-function)
3768 'erlang-tags-search-forward)
3769 (set (make-local-variable 'find-tag-regexp-search-function)
3770 'erlang-tags-regexp-search-forward)
3771 (set (make-local-variable 'find-tag-tag-order)
3772 '(erlang-tag-match-module-p))
3773 (set (make-local-variable 'find-tag-regexp-tag-order)
3774 '(erlang-tag-match-module-regexp-p))))
3777 (defun erlang-tags-remove-module-check ()
3778 "Remove our own tags search functions."
3779 (funcall (symbol-function 'set)
3780 'tags-table-format-hooks
3781 erlang-tags-orig-format-hooks)
3782 ;; Remove our functions from the TAGS files. (Note that
3783 ;; `tags-table-computed-list' need not be the same list as when
3784 ;; the search was started.)
3785 (save-excursion
3786 (let ((buffers erlang-tags-buffer-list))
3787 (while buffers
3788 (if (buffer-name (car buffers))
3789 (progn
3790 (set-buffer (car buffers))
3791 (erlang-tags-remove-local)))
3792 (setq buffers (cdr buffers))))))
3795 (defun erlang-tags-remove-local ()
3796 "Remove our tag search functions from current buffer."
3797 (if (null erlang-tags-buffer-installed-p)
3799 (funcall (symbol-function 'set) 'erlang-tags-buffer-installed-p nil)
3800 (funcall (symbol-function 'set)
3801 'find-tag-tag-order erlang-tags-orig-tag-order)
3802 (funcall (symbol-function 'set)
3803 'find-tag-regexp-tag-order erlang-tags-orig-regexp-tag-order)
3804 (funcall (symbol-function 'set)
3805 'find-tag-search-function erlang-tags-orig-search-function)
3806 (funcall (symbol-function 'set)
3807 'find-tag-regexp-search-function
3808 erlang-tags-orig-regexp-search-function)))
3811 (defun erlang-tags-recognize-tags-table ()
3812 "Install our functions in all loaded TAGS files.
3814 This function is added to `tags-table-format-hooks' when searching
3815 for a tag on the form `module:tag'."
3816 (if (null (funcall (symbol-function 'etags-recognize-tags-table)))
3818 (erlang-tags-install-local)
3822 (defun erlang-tags-search-forward (tag &optional bound noerror count)
3823 "Forward search function, aware of Erlang module prefix."
3824 (if (string-match ":" tag)
3825 (setq tag (substring tag (match-end 0) nil)))
3826 ;; Avoid unintended recursion.
3827 (if (eq erlang-tags-orig-search-function 'erlang-tags-search-forward)
3828 (search-forward tag bound noerror count)
3829 (funcall erlang-tags-orig-search-function tag bound noerror count)))
3832 (defun erlang-tags-regexp-search-forward (tag &optional bound noerror count)
3833 "Forward regexp search function, aware of Erlang module prefix."
3834 (if (string-match ":" tag)
3835 (setq tag (substring tag (match-end 0) nil)))
3836 (if (eq erlang-tags-orig-regexp-search-function
3837 'erlang-tags-regexp-search-forward)
3838 (re-search-forward tag bound noerror count)
3839 (funcall erlang-tags-orig-regexp-search-function
3840 tag bound noerror count)))
3843 ;; t if point is at a tag line that matches TAG, containing
3844 ;; module information. Assumes that all other order functions
3845 ;; are stored in `erlang-tags-orig-[regex]-tag-order'.
3847 (defun erlang-tag-match-module-p (tag)
3848 (erlang-tag-match-module-common-p tag erlang-tags-orig-tag-order))
3850 (defun erlang-tag-match-module-regexp-p (tag)
3851 (erlang-tag-match-module-common-p tag erlang-tags-orig-regexp-tag-order))
3853 (defun erlang-tag-match-module-common-p (tag order)
3854 (let ((mod nil)
3855 (found nil))
3856 (if (string-match ":" tag)
3857 (progn
3858 (setq mod (substring tag 0 (match-beginning 0)))
3859 (setq tag (substring tag (match-end 0) nil))))
3860 (while (and order (not found))
3861 (setq found
3862 (and (not (memq (car order)
3863 '(erlang-tag-match-module-p
3864 erlang-tag-match-module-regexp-p)))
3865 (funcall (car order) tag)))
3866 (setq order (cdr order)))
3867 (and found
3868 (or (null mod)
3869 (string= mod (erlang-get-module-from-file-name
3870 (file-of-tag)))))))
3873 ;;; Tags completion, Emacs 19 `etags' specific.
3875 ;;; The basic idea is to create a second completion table `erlang-tags-
3876 ;;; completion-table' containing all normal tags plus tags on the form
3877 ;;; `module:tag'.
3880 (defun erlang-complete-tag ()
3881 "Perform tags completion on the text around point.
3882 Completes to the set of names listed in the current tags table.
3884 Should the Erlang tags system be installed this command knows
3885 about Erlang modules."
3886 (interactive)
3887 (condition-case nil
3888 (require 'etags)
3889 (error nil))
3890 (cond ((and erlang-tags-installed
3891 (fboundp 'complete-tag)) ; Emacs 19
3892 (let ((orig-tags-complete-tag (symbol-function 'tags-complete-tag)))
3893 (fset 'tags-complete-tag
3894 (symbol-function 'erlang-tags-complete-tag))
3895 (unwind-protect
3896 (funcall (symbol-function 'complete-tag))
3897 (fset 'tags-complete-tag orig-tags-complete-tag))))
3898 ((fboundp 'complete-tag) ; Emacs 19
3899 (funcall (symbol-function 'complete-tag)))
3900 ((fboundp 'tag-complete-symbol) ; XEmacs
3901 (funcall (symbol-function 'tag-complete-symbol)))
3903 (error "This version of Emacs can't complete tags"))))
3906 ;; Based on `tags-complete-tag', but this one uses
3907 ;; `erlang-tags-completion-table' instead of `tags-completion-table'.
3909 ;; This is the entry-point called by system function `completing-read'.
3910 (defun erlang-tags-complete-tag (string predicate what)
3911 (save-excursion
3912 ;; If we need to ask for the tag table, allow that.
3913 (let ((enable-recursive-minibuffers t))
3914 (visit-tags-table-buffer))
3915 (if (eq what t)
3916 (all-completions string (erlang-tags-completion-table) predicate)
3917 (try-completion string (erlang-tags-completion-table) predicate))))
3920 ;; `tags-completion-table' calls itself recursively, make it
3921 ;; call our own wedge instead. Note that the recursive call
3922 ;; is very rare; it only occurs when a tags-file contains
3923 ;; `include'-statements.
3924 (defun erlang-tags-completion-table ()
3925 "Build completion table. Tags on the form `tag' or `module:tag'."
3926 (setq erlang-tags-orig-completion-table
3927 (symbol-function 'tags-completion-table))
3928 (fset 'tags-completion-table
3929 (symbol-function 'erlang-tags-completion-table-1))
3930 (unwind-protect
3931 (erlang-tags-completion-table-1)
3932 (fset 'tags-completion-table
3933 erlang-tags-orig-completion-table)))
3936 (defun erlang-tags-completion-table-1 ()
3937 (make-local-variable 'erlang-tags-completion-table)
3938 (or erlang-tags-completion-table
3939 (let ((tags-completion-table nil)
3940 (tags-completion-table-function
3941 'erlang-etags-tags-completion-table))
3942 (funcall erlang-tags-orig-completion-table)
3943 (setq erlang-tags-completion-table tags-completion-table))))
3946 ;; Based on `etags-tags-completion-table'. The difference is that we
3947 ;; add three symbols to the vector, the tag, module: and module:tag.
3948 ;; The module is extracted from the file name of a tag. (This one
3949 ;; only works if we are looking at an `etags' file. However, this is
3950 ;; the only format supported by Emacs, so far.)
3951 (defun erlang-etags-tags-completion-table ()
3952 (let ((table (make-vector 511 0))
3953 (file nil))
3954 (save-excursion
3955 (goto-char (point-min))
3956 ;; This monster regexp matches an etags tag line.
3957 ;; \1 is the string to match;
3958 ;; \2 is not interesting;
3959 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
3960 ;; \4 is not interesting;
3961 ;; \5 is the explicitly-specified tag name.
3962 ;; \6 is the line to start searching at;
3963 ;; \7 is the char to start searching at.
3964 (while (progn
3965 (while (and
3966 (eq (following-char) ?\f)
3967 (looking-at "\f\n\\([^,\n]*\\),.*\n"))
3968 (setq file (buffer-substring
3969 (match-beginning 1) (match-end 1)))
3970 (goto-char (match-end 0)))
3971 (re-search-forward
3973 ^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
3974 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
3975 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
3976 nil t))
3977 (let ((tag (if (match-beginning 5)
3978 ;; There is an explicit tag name.
3979 (buffer-substring (match-beginning 5) (match-end 5))
3980 ;; No explicit tag name. Best guess.
3981 (buffer-substring (match-beginning 3) (match-end 3))))
3982 (module (and file
3983 (erlang-get-module-from-file-name file))))
3984 (intern tag table)
3985 (if (stringp module)
3986 (progn
3987 (intern (concat module ":" tag) table)
3988 ;; Only the first one will be stored in the table.
3989 (intern (concat module ":") table))))))
3990 table))
3993 ;;; Prepare for other methods to run an Erlang slave process.
3996 (defvar erlang-shell-function 'inferior-erlang
3997 "Command to execute start a new Erlang shell.
3999 Change this variable to use your favorite
4000 Erlang compilation package.")
4002 (defvar erlang-shell-display-function 'inferior-erlang-run-or-select
4003 "Command to execute to display Erlang shell.
4005 Change this variable to use your favorite
4006 Erlang compilation package.")
4008 (defvar erlang-compile-function 'inferior-erlang-compile
4009 "Command to execute to compile current buffer.
4011 Change this variable to use your favorite
4012 Erlang compilation package.")
4014 (defvar erlang-compile-display-function 'inferior-erlang-run-or-select
4015 "Command to execute to view last compilation.
4017 Change this variable to use your favorite
4018 Erlang compilation package.")
4020 (defvar erlang-next-error-function 'inferior-erlang-next-error
4021 "Command to execute to go to the next error.
4023 Change this variable to use your favorite Erlang compilation
4024 package. Not used in Emacs 21.")
4027 ;;;###autoload
4028 (defun erlang-shell ()
4029 "Start a new Erlang shell.
4031 The variable `erlang-shell-function' decides which method to use,
4032 default is to start a new Erlang host. It is possible that, in the
4033 future, a new shell on an already running host will be started."
4034 (interactive)
4035 (call-interactively erlang-shell-function))
4038 ;;;###autoload (autoload 'run-erlang "erlang" "Start a new Erlang shell." t)
4040 ;; It is customary for Emacs packages to supply a function on this
4041 ;; form, even though it violates the `erlang-*' name convention.
4042 (defalias 'run-erlang 'erlang-shell)
4045 (defun erlang-shell-display ()
4046 "Display an Erlang shell, or start a new."
4047 (interactive)
4048 (call-interactively erlang-shell-display-function))
4051 ;;;###autoload
4052 (defun erlang-compile ()
4053 "Compile Erlang module in current buffer."
4054 (interactive)
4055 (call-interactively erlang-compile-function))
4058 (defun erlang-compile-display ()
4059 "Display compilation output."
4060 (interactive)
4061 (call-interactively erlang-compile-display-function))
4064 (defun erlang-next-error ()
4065 "Display next error message from the latest compilation."
4066 (interactive)
4067 (call-interactively erlang-next-error-function))
4072 ;;; Erlang Shell Mode -- Major mode used for Erlang shells.
4075 ;; This mode is designed to be implementation independent,
4076 ;; e.g. it does not assume that we are running an inferior
4077 ;; Erlang, there exists a lot of other possibilities.
4080 (defvar erlang-shell-buffer-name "*erlang*"
4081 "The name of the Erlang link shell buffer.")
4084 (defvar erlang-shell-mode-map nil
4085 "Keymap used by Erlang shells.")
4088 (defvar erlang-shell-mode-hook nil
4089 "*User functions to run when an Erlang shell is started.
4091 This hook is used to change the behaviour of Erlang mode. It is
4092 normally used by the user to personalise the programming environment.
4093 When used in a site init file, it could be used to customise Erlang
4094 mode for all users on the system.
4096 The function added to this hook is run every time a new Erlang
4097 shell is started.
4099 See also `erlang-load-hook', a hook which is run once, when Erlang
4100 mode is loaded, and `erlang-mode-hook' which is run every time a new
4101 Erlang source file is loaded into Emacs.")
4104 (defvar erlang-input-ring-file-name "~/.erlang_history"
4105 "*When non-nil, file name used to store Erlang shell history information.")
4108 (defun erlang-shell-mode ()
4109 "Major mode for interacting with an Erlang shell.
4111 We assume that we already are in Comint mode.
4113 The following special commands are available:
4114 \\{erlang-shell-mode-map}"
4115 (interactive)
4116 (setq major-mode 'erlang-shell-mode)
4117 (setq mode-name "Erlang Shell")
4118 (erlang-mode-variables)
4119 (if erlang-shell-mode-map
4121 (setq erlang-shell-mode-map (copy-keymap comint-mode-map))
4122 (erlang-shell-mode-commands erlang-shell-mode-map))
4123 (use-local-map erlang-shell-mode-map)
4124 (unless inferior-erlang-use-cmm
4125 ;; This was originally not a marker, but it needs to be, at least
4126 ;; in Emacs 21, and should be backwards-compatible. Otherwise,
4127 ;; would need to test whether compilation-parsing-end is a marker
4128 ;; after requiring `compile'.
4129 (set (make-local-variable 'compilation-parsing-end) (copy-marker 1))
4130 (set (make-local-variable 'compilation-error-list) nil)
4131 (set (make-local-variable 'compilation-old-error-list) nil))
4132 ;; Needed when compiling directly from the Erlang shell.
4133 (setq compilation-last-buffer (current-buffer))
4134 (erlang-add-compilation-alist erlang-error-regexp-alist)
4135 (setq comint-prompt-regexp "^[^>=]*> *")
4136 (setq comint-eol-on-send t)
4137 (setq comint-input-ignoredups t)
4138 (setq comint-scroll-show-maximum-output t)
4139 (setq comint-scroll-to-bottom-on-output t)
4140 ;; In Emacs 19.30, `add-hook' has got a `local' flag, use it. If
4141 ;; the call fails, just call the normal `add-hook'.
4142 (condition-case nil
4143 (progn
4144 (make-local-hook 'comint-output-filter-functions) ; obsolete after Emacs 21.3
4145 (add-hook 'comint-output-filter-functions
4146 'inferior-erlang-strip-delete nil t)
4147 (add-hook 'comint-output-filter-functions
4148 'inferior-erlang-strip-ctrl-m nil t))
4149 (error
4150 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-delete)
4151 (add-hook 'comint-output-filter-functions 'inferior-erlang-strip-ctrl-m)))
4152 ;; Some older versions of comint don't have an input ring.
4153 (if (fboundp 'comint-read-input-ring)
4154 (progn
4155 (setq comint-input-ring-file-name erlang-input-ring-file-name)
4156 (comint-read-input-ring t)
4157 (make-local-variable 'kill-buffer-hook)
4158 (add-hook 'kill-buffer-hook 'comint-write-input-ring)))
4159 ;; At least in Emacs 21, we need to be in `compilation-minor-mode'
4160 ;; for `next-error' to work. We can avoid it clobbering the shell
4161 ;; keys thus.
4162 (when inferior-erlang-use-cmm
4163 (compilation-minor-mode 1)
4164 (set (make-local-variable 'minor-mode-overriding-map-alist)
4165 `((compilation-minor-mode
4166 . ,(let ((map (make-sparse-keymap)))
4167 ;; It would be useful to put keymap properties on the
4168 ;; error lines so that we could use RET and mouse-2
4169 ;; on them directly.
4170 (when (boundp 'compilation-skip-threshold) ; new compile.el
4171 (define-key map [mouse-2] #'erlang-mouse-2-command)
4172 (define-key map "\C-m" #'erlang-RET-command))
4173 (if (boundp 'compilation-menu-map)
4174 (define-key map [menu-bar compilation]
4175 (cons "Errors" compilation-menu-map)))
4176 map)))))
4177 (run-hooks 'erlang-shell-mode-hook))
4180 (defun erlang-mouse-2-command (event)
4181 "Command bound to `mouse-2' in inferior Erlang buffer.
4182 Selects Comint or Compilation mode command as appropriate."
4183 (interactive "e")
4184 (if (save-window-excursion
4185 (save-excursion
4186 (mouse-set-point event)
4187 (consp (get-text-property (line-beginning-position) 'message))))
4188 (call-interactively (lookup-key compilation-mode-map [mouse-2]))
4189 (call-interactively (lookup-key comint-mode-map [mouse-2]))))
4191 (defun erlang-RET-command ()
4192 "Command bound to `RET' in inferior Erlang buffer.
4193 Selects Comint or Compilation mode command as appropriate."
4194 (interactive)
4195 (if (consp (get-text-property (line-beginning-position) 'message))
4196 (call-interactively (lookup-key compilation-mode-map "\C-m"))
4197 (call-interactively (lookup-key comint-mode-map "\C-m"))))
4199 (defun erlang-shell-mode-commands (map)
4200 (define-key map "\M-\t" 'erlang-complete-tag)
4201 (define-key map "\C-a" 'comint-bol) ; Normally the other way around.
4202 (define-key map "\C-c\C-a" 'beginning-of-line)
4203 (define-key map "\C-d" nil) ; Was `comint-delchar-or-maybe-eof'
4204 (define-key map "\M-\C-m" 'compile-goto-error)
4205 (unless inferior-erlang-use-cmm
4206 (define-key map "\C-x`" 'erlang-next-error)))
4209 ;;; Inferior Erlang -- Run an Erlang shell as a subprocess.
4212 (defvar inferior-erlang-display-buffer-any-frame nil
4213 "*When nil, `inferior-erlang-display-buffer' use only selected frame.
4214 When t, all frames are searched. When 'raise, the frame is raised.")
4216 (defvar inferior-erlang-shell-type 'newshell
4217 "The type of Erlang shell to use.
4219 When this variable is set to the atom `oldshell', the old shell is used.
4220 When set to `newshell' the new shell is used. Should the variable be
4221 nil, the default shell is used.
4223 This variable influence the setting of other variables.")
4225 (defvar inferior-erlang-machine "erl"
4226 "*The name of the Erlang shell.")
4228 (defvar inferior-erlang-machine-options '()
4229 "*The options used when activating the Erlang shell.
4231 This must be a list of strings.")
4233 (defvar inferior-erlang-process-name "inferior-erlang"
4234 "The name of the inferior Erlang process.")
4236 (defvar inferior-erlang-buffer-name erlang-shell-buffer-name
4237 "The name of the inferior Erlang buffer.")
4239 (defvar inferior-erlang-prompt-timeout 60
4240 "*Number of seconds before `inferior-erlang-wait-prompt' timeouts.
4242 The time specified is waited after every output made by the inferior
4243 Erlang shell. When this variable is t, we assume that we always have
4244 a prompt. When nil, we will wait forever, or until \\[keyboard-quit].")
4246 (defvar inferior-erlang-process nil
4247 "Process of last invoked inferior Erlang, or nil.")
4249 (defvar inferior-erlang-buffer nil
4250 "Buffer of last invoked inferior Erlang, or nil.")
4252 ;;;###autoload
4253 (defun inferior-erlang ()
4254 "Run an inferior Erlang.
4256 This is just like running Erlang in a normal shell, except that
4257 an Emacs buffer is used for input and output.
4258 \\<comint-mode-map>
4259 The command line history can be accessed with \\[comint-previous-input] and \\[comint-next-input].
4260 The history is saved between sessions.
4262 Entry to this mode calls the functions in the variables
4263 `comint-mode-hook' and `erlang-shell-mode-hook' with no arguments.
4265 The following commands imitate the usual Unix interrupt and
4266 editing control characters:
4267 \\{erlang-shell-mode-map}"
4268 (interactive)
4269 (require 'comint)
4270 (let ((opts inferior-erlang-machine-options))
4271 (cond ((eq inferior-erlang-shell-type 'oldshell)
4272 (setq opts (cons "-oldshell" opts)))
4273 ((eq inferior-erlang-shell-type 'newshell)
4274 (setq opts (append '("-newshell" "-env" "TERM" "vt100") opts))))
4275 (setq inferior-erlang-buffer
4276 (apply 'make-comint
4277 inferior-erlang-process-name inferior-erlang-machine
4278 nil opts)))
4279 (setq inferior-erlang-process
4280 (get-buffer-process inferior-erlang-buffer))
4281 (process-kill-without-query inferior-erlang-process)
4282 (switch-to-buffer inferior-erlang-buffer)
4283 (if (and (not (eq system-type 'windows-nt))
4284 (eq inferior-erlang-shell-type 'newshell))
4285 (setq comint-process-echoes t))
4286 ;; `rename-buffer' takes only one argument in Emacs 18.
4287 (condition-case nil
4288 (rename-buffer inferior-erlang-buffer-name t)
4289 (error (rename-buffer inferior-erlang-buffer-name)))
4290 (erlang-shell-mode))
4293 (defun inferior-erlang-run-or-select ()
4294 "Switch to an inferior Erlang buffer, possibly starting new process."
4295 (interactive)
4296 (if (null (inferior-erlang-running-p))
4297 (inferior-erlang)
4298 (inferior-erlang-display-buffer t)))
4301 (defun inferior-erlang-display-buffer (&optional select)
4302 "Make the inferior Erlang process visible.
4303 The window is returned.
4305 Should `inferior-erlang-display-buffer-any-frame' be nil the buffer is
4306 displayed in the current frame. Should it be non-nil, and the buffer
4307 already is visible in any other frame, no new window will be created.
4308 Should it be the atom 'raise, the frame containing the window will
4309 be raised.
4311 Should the optional argument SELECT be non-nil, the window is
4312 selected. Should the window be in another frame, that frame is raised.
4314 Note, should the mouse pointer be places outside the raised frame, that
4315 frame will become deselected before the next command."
4316 (interactive)
4317 (or (inferior-erlang-running-p)
4318 (error "No inferior Erlang process is running"))
4319 (let ((win (inferior-erlang-window
4320 inferior-erlang-display-buffer-any-frame))
4321 (frames-p (fboundp 'selected-frame)))
4322 (if (null win)
4323 (let ((old-win (selected-window)))
4324 (save-excursion
4325 (switch-to-buffer-other-window inferior-erlang-buffer)
4326 (setq win (selected-window)))
4327 (select-window old-win))
4328 (if (and window-system
4329 frames-p
4330 (or select
4331 (eq inferior-erlang-display-buffer-any-frame 'raise))
4332 (not (eq (selected-frame) (window-frame win))))
4333 (raise-frame (window-frame win))))
4334 (if select
4335 (select-window win))
4336 (sit-for 0)
4337 win))
4340 (defun inferior-erlang-running-p ()
4341 "Non-nil when an inferior Erlang is running."
4342 (and inferior-erlang-process
4343 (memq (process-status inferior-erlang-process) '(run open))
4344 inferior-erlang-buffer
4345 (buffer-name inferior-erlang-buffer)))
4348 (defun inferior-erlang-window (&optional all-frames)
4349 "Return the window containing the inferior Erlang, or nil."
4350 (and (inferior-erlang-running-p)
4351 (if (and all-frames (>= erlang-emacs-major-version 19))
4352 (get-buffer-window inferior-erlang-buffer t)
4353 (get-buffer-window inferior-erlang-buffer))))
4356 (defun inferior-erlang-wait-prompt ()
4357 "Wait until the inferior Erlang shell prompt appears."
4358 (if (eq inferior-erlang-prompt-timeout t)
4360 (or (inferior-erlang-running-p)
4361 (error "No inferior Erlang shell is running"))
4362 (save-excursion
4363 (set-buffer inferior-erlang-buffer)
4364 (let ((msg nil))
4365 (while (save-excursion
4366 (goto-char (process-mark inferior-erlang-process))
4367 (forward-line 0)
4368 (not (looking-at comint-prompt-regexp)))
4369 (if msg
4371 (setq msg t)
4372 (message "Waiting for Erlang shell prompt (press C-g to abort)."))
4373 (or (accept-process-output inferior-erlang-process
4374 inferior-erlang-prompt-timeout)
4375 (error "No Erlang shell prompt before timeout")))
4376 (if msg (message ""))))))
4378 (autoload 'comint-send-input "comint")
4380 (defun inferior-erlang-send-command (cmd &optional hist)
4381 "Send command CMD to the inferior Erlang.
4383 The contents of the current command line (if any) will
4384 be placed at the next prompt.
4386 If optional second argument is non-nil the command is inserted into
4387 the history list.
4389 Return the position after the newly inserted command."
4390 (or (inferior-erlang-running-p)
4391 (error "No inferior Erlang process is running"))
4392 (let ((old-buffer (current-buffer))
4393 (insert-point (marker-position (process-mark inferior-erlang-process)))
4394 (insert-length (if comint-process-echoes
4396 (1+ (length cmd)))))
4397 (set-buffer inferior-erlang-buffer)
4398 (goto-char insert-point)
4399 (insert cmd)
4400 ;; Strange things happened if `comint-eol-on-send' is declared
4401 ;; in the `let' expression above, but setq:d here. The
4402 ;; `set-buffer' statement obviously makes the buffer local
4403 ;; instance of `comint-eol-on-send' shadow this one.
4404 ;; I'm considering this a bug in Elisp.
4406 ;; This was previously cautioned against in the Lisp manual. It
4407 ;; has been sorted out in Emacs 21. -- fx
4408 (let ((comint-eol-on-send nil)
4409 (comint-input-filter (if hist comint-input-filter 'ignore)))
4410 (comint-send-input))
4411 ;; Adjust all windows whose points are incorrect.
4412 (if (null comint-process-echoes)
4413 (walk-windows
4414 (function
4415 (lambda (window)
4416 (if (and (eq (window-buffer window) inferior-erlang-buffer)
4417 (= (window-point window) insert-point))
4418 (set-window-point window
4419 (+ insert-point insert-length)))))
4420 nil t))
4421 (set-buffer old-buffer)
4422 (+ insert-point insert-length)))
4425 (defun inferior-erlang-strip-delete (&optional s)
4426 "Remove `^H' (delete) and the characters it was supposed to remove."
4427 (interactive)
4428 (if (and (boundp 'comint-last-input-end)
4429 (boundp 'comint-last-output-start))
4430 (save-excursion
4431 (goto-char
4432 (if (interactive-p)
4433 (symbol-value 'comint-last-input-end)
4434 (symbol-value 'comint-last-output-start)))
4435 (while (progn (skip-chars-forward "^\C-h")
4436 (not (eq (point) (point-max))))
4437 (delete-char 1)
4438 (or (bolp)
4439 (backward-delete-char 1))))))
4442 ;; Basically `comint-strip-ctrl-m', with a few extra checks.
4443 (defun inferior-erlang-strip-ctrl-m (&optional string)
4444 "Strip trailing `^M' characters from the current output group."
4445 (interactive)
4446 (if (and (boundp 'comint-last-input-end)
4447 (boundp 'comint-last-output-start))
4448 (let ((pmark (process-mark (get-buffer-process (current-buffer)))))
4449 (save-excursion
4450 (goto-char
4451 (if (interactive-p)
4452 (symbol-value 'comint-last-input-end)
4453 (symbol-value 'comint-last-output-start)))
4454 (while (re-search-forward "\r+$" pmark t)
4455 (replace-match "" t t))))))
4458 (defun inferior-erlang-compile ()
4459 "Compile the file in the current buffer.
4461 Should Erlang return `{error, nofile}' it could not load the object
4462 module after completing the compilation. This is due to a bug in the
4463 compile command `c' when using the option `outdir'.
4465 There exists two workarounds for this bug:
4467 1) Place the directory in the Erlang load path.
4469 2) Set the Emacs variable `erlang-compile-use-outdir' to nil.
4470 To do so, place the following line in your `~/.emacs'-file:
4471 (setq erlang-compile-use-outdir nil)"
4472 (interactive)
4473 (save-some-buffers)
4474 (or (inferior-erlang-running-p)
4475 (save-excursion
4476 (inferior-erlang)))
4477 (or (inferior-erlang-running-p)
4478 (error "Error starting inferior Erlang shell"))
4479 (let ((dir (file-name-directory (buffer-file-name)))
4480 ;;; (file (file-name-nondirectory (buffer-file-name)))
4481 (noext (substring (buffer-file-name) 0 -4))
4482 ;; Hopefully, noone else will ever use these...
4483 (tmpvar "Tmp7236")
4484 (tmpvar2 "Tmp8742")
4485 end)
4486 (inferior-erlang-display-buffer)
4487 (inferior-erlang-wait-prompt)
4488 (setq end (inferior-erlang-send-command
4489 (if erlang-compile-use-outdir
4490 (format "c(\"%s\", [{outdir, \"%s\"}])." noext dir)
4491 (format
4492 (concat
4493 "f(%s), {ok, %s} = file:get_cwd(), "
4494 "file:set_cwd(\"%s\"), "
4495 "%s = c(\"%s\"), file:set_cwd(%s), f(%s), %s.")
4496 tmpvar2 tmpvar
4498 tmpvar2 noext tmpvar tmpvar tmpvar2))
4499 nil))
4500 (inferior-erlang-wait-prompt)
4501 (save-excursion
4502 (set-buffer inferior-erlang-buffer)
4503 (setq compilation-error-list nil)
4504 (set-marker compilation-parsing-end end))
4505 (setq compilation-last-buffer inferior-erlang-buffer)))
4508 ;; `next-error' only accepts buffers with major mode `compilation-mode'
4509 ;; or with the minor mode `compilation-minor-mode' activated.
4510 ;; (To activate the minor mode is out of the question, since it will
4511 ;; ruin the inferior Erlang keymap.)
4512 ;; This is done differently in Emacs 21.
4513 (defun inferior-erlang-next-error (&optional argp)
4514 "Just like `next-error'.
4515 Capable of finding error messages in an inferior Erlang buffer."
4516 (interactive "P")
4517 (let ((done nil)
4518 (buf (and (boundp 'compilation-last-buffer)
4519 compilation-last-buffer)))
4520 (if (and (bufferp buf)
4521 (save-excursion
4522 (set-buffer buf)
4523 (and (eq major-mode 'erlang-shell-mode)
4524 (setq major-mode 'compilation-mode))))
4525 (unwind-protect
4526 (progn
4527 (setq done t)
4528 (next-error argp))
4529 (save-excursion
4530 (set-buffer buf)
4531 (setq major-mode 'erlang-shell-mode))))
4532 (or done
4533 (next-error argp))))
4536 (defun inferior-erlang-change-directory (&optional dir)
4537 "Make the inferior Erlang change directory.
4538 The default is to go to the directory of the current buffer."
4539 (interactive)
4540 (or dir (setq dir (file-name-directory (buffer-file-name))))
4541 (or (inferior-erlang-running-p)
4542 (error "No inferior Erlang is running"))
4543 (inferior-erlang-display-buffer)
4544 (inferior-erlang-wait-prompt)
4545 (inferior-erlang-send-command (format "cd('%s')." dir) nil))
4547 (defun erlang-align-arrows (start end)
4548 "Align arrows (\"->\") in function clauses from START to END.
4549 When called interactively, aligns arrows after function clauses inside
4550 the region.
4552 With a prefix argument, aligns all arrows, not just those in function
4553 clauses.
4555 Example:
4557 sum(L) -> sum(L, 0).
4558 sum([H|T], Sum) -> sum(T, Sum + H);
4559 sum([], Sum) -> Sum.
4561 becomes:
4563 sum(L) -> sum(L, 0).
4564 sum([H|T], Sum) -> sum(T, Sum + H);
4565 sum([], Sum) -> Sum."
4566 (interactive "r")
4567 (save-excursion
4568 (let (;; regexp for matching arrows. without a prefix argument,
4569 ;; the regexp matches function heads. With a prefix, it
4570 ;; matches any arrow.
4571 (re (if current-prefix-arg
4572 "^.*\\(\\)->"
4573 (eval-when-compile
4574 (concat "^" erlang-atom-regexp ".*\\(\\)->"))))
4575 ;; part of regexp matching directly before the arrow
4576 (arrow-match-pos (if current-prefix-arg
4578 (1+ erlang-atom-regexp-matches)))
4579 ;; accumulator for positions where arrows are found, ordered
4580 ;; by buffer position (from greatest to smallest)
4581 (arrow-positions '())
4582 ;; accumulator for longest distance from start of line to arrow
4583 (most-indent 0)
4584 ;; marker to track the end of the region we're aligning
4585 (end-marker (progn (goto-char end)
4586 (point-marker))))
4587 ;; Pass 1: Find the arrow positions, adjust the whitespace
4588 ;; before each arrow to one space, and find the greatest
4589 ;; indentation level.
4590 (goto-char start)
4591 (while (re-search-forward re end-marker t)
4592 (goto-char (match-beginning arrow-match-pos))
4593 (just-one-space) ; adjust whitespace
4594 (setq arrow-positions (cons (point) arrow-positions))
4595 (setq most-indent (max most-indent (erlang-column-number))))
4596 (set-marker end-marker nil) ; free the marker
4597 ;; Pass 2: Insert extra padding so that all arrow indentation is
4598 ;; equal. This is done last-to-first by buffer position, so that
4599 ;; inserting spaces before one arrow doesn't change the
4600 ;; positions of the next ones.
4601 (mapcar (lambda (arrow-pos)
4602 (goto-char arrow-pos)
4603 (let* ((pad (- most-indent (erlang-column-number))))
4604 (when (> pad 0)
4605 (insert-char ?\ pad))))
4606 arrow-positions))))
4608 (defun erlang-column-number ()
4609 "Return the column number of the current position in the buffer.
4610 Tab characters are counted by their visual width."
4611 (string-width (buffer-substring (line-beginning-position) (point))))
4613 (defun erlang-current-defun ()
4614 "`add-log-current-defun-function' for Erlang."
4615 (save-excursion
4616 (erlang-beginning-of-function)
4617 (if (looking-at "[a-z0-9_]+")
4618 (match-string 0))))
4620 ;; Aliases for backward compatibility with older versions of Erlang Mode.
4622 ;; Unfortuantely, older versions of Emacs doesn't have `defalias' and
4623 ;; `make-obsolete' so we have to define our own `obsolete' function.
4625 (defun erlang-obsolete (sym newdef)
4626 "Make the obsolete function SYM refer to the defined function NEWDEF.
4628 Simplified version of a combination `defalias' and `make-obsolete',
4629 it assumes that NEWDEF is loaded."
4630 (defalias sym (symbol-function newdef))
4631 (if (fboundp 'make-obsolete)
4632 (make-obsolete sym newdef)))
4635 (erlang-obsolete 'calculate-erlang-indent 'erlang-calculate-indent)
4636 (erlang-obsolete 'calculate-erlang-stack-indent
4637 'erlang-calculate-stack-indent)
4638 (erlang-obsolete 'at-erlang-keyword 'erlang-at-keyword)
4639 (erlang-obsolete 'at-erlang-operator 'erlang-at-operator)
4640 (erlang-obsolete 'beginning-of-erlang-clause 'erlang-beginning-of-clause)
4641 (erlang-obsolete 'end-of-erlang-clause 'erlang-end-of-clause)
4642 (erlang-obsolete 'mark-erlang-clause 'erlang-mark-clause)
4643 (erlang-obsolete 'beginning-of-erlang-function 'erlang-beginning-of-function)
4644 (erlang-obsolete 'end-of-erlang-function 'erlang-end-of-function)
4645 (erlang-obsolete 'mark-erlang-function 'erlang-mark-function)
4646 (erlang-obsolete 'pass-over-erlang-clause 'erlang-pass-over-function)
4647 (erlang-obsolete 'name-of-erlang-function 'erlang-name-of-function)
4650 ;; Fixme: shouldn't redefine `set-visited-file-name' anyhow -- see above.
4651 (defconst erlang-unload-hook
4652 (list (lambda ()
4653 (defalias 'set-visited-file-name
4654 'erlang-orig-set-visited-file-name)
4655 (when (featurep 'advice)
4656 (ad-unadvise 'Man-notify-when-ready)
4657 (ad-unadvise 'set-visited-file-name)))))
4659 ;; The end...
4661 (provide 'erlang)
4663 (run-hooks 'erlang-load-hook)
4665 ;; Local variables:
4666 ;; coding: iso-8859-1
4667 ;; End:
4669 ;;; erlang.el ends here