Reformat @doc sections to be consistent.
[erlware-mode.git] / erlang-skels.el
blobff9ff646a9a5520cf362a859b0b6982f5ecb328c
1 ;; erlang.el --- Major modes for editing and running Erlang
2 ;; The contents of this file are subject to the Erlang Public License,
3 ;; Version 1.1, (the "License"); you may not use this file except in
4 ;; compliance with the License. You should have received a copy of the
5 ;; Erlang Public License along with this software. If not, it can be
6 ;; retrieved via the world wide web at http://www.erlang.org/.
8 ;; Software distributed under the License is distributed on an "AS IS"
9 ;; basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
10 ;; the License for the specific language governing rights and limitations
11 ;; under the License.
13 ;; The Initial Developer of the Original Code is Ericsson Utvecklings AB.
14 ;; All Rights Reserved.
18 (defvar erlang-skel
19 '(("If" "if" erlang-skel-if)
20 ("Case" "case" erlang-skel-case)
21 ("Receive" "receive" erlang-skel-receive)
22 ("Receive After" "after" erlang-skel-receive-after)
23 ("Receive Loop" "loop" erlang-skel-receive-loop)
24 ("Module" "module" erlang-skel-module)
25 ("Author" "author" erlang-skel-author)
26 ("Function" "function" erlang-skel-function)
28 ("Small Header" "small-header"
29 erlang-skel-small-header erlang-skel-header)
30 ("Normal Header" "normal-header"
31 erlang-skel-normal-header erlang-skel-header)
32 ("Large Header" "large-header"
33 erlang-skel-large-header erlang-skel-header)
35 ("Small Server" "small-server"
36 erlang-skel-small-server erlang-skel-header)
38 ("Application" "application"
39 erlang-skel-application erlang-skel-header)
40 ("Supervisor" "supervisor"
41 erlang-skel-supervisor erlang-skel-header)
42 ("supervisor_bridge" "supervisor-bridge"
43 erlang-skel-supervisor-bridge erlang-skel-header)
44 ("gen_server" "generic-server"
45 erlang-skel-generic-server erlang-skel-header)
46 ("gen_event" "gen-event"
47 erlang-skel-gen-event erlang-skel-header)
48 ("gen_fsm" "gen-fsm"
49 erlang-skel-gen-fsm erlang-skel-header)
50 ("Library module" "gen-lib"
51 erlang-skel-lib erlang-skel-header)
52 ("Corba callback" "gen-corba-cb"
53 erlang-skel-corba-callback erlang-skel-header)
54 ("Erlang test suite TS frontend" "ts-test-suite"
55 erlang-skel-ts-test-suite erlang-skel-header)
56 ("Erlang test suite CT frontend" "ct-test-suite"
57 erlang-skel-ct-test-suite erlang-skel-header)
59 "*Description of all skeleton templates.
60 Both functions and menu entries will be created.
62 Each entry in `erlang-skel' should be a list with three or four
63 elements, or the empty list.
65 The first element is the name which shows up in the menu. The second
66 is the `tempo' identifier (The string \"erlang-\" will be added in
67 front of it). The third is the skeleton descriptor, a variable
68 containing `tempo' attributes as described in the function
69 `tempo-define-template'. The optional fourth elements denotes a
70 function which should be called when the menu is selected.
72 Functions corresponding to every template will be created. The name
73 of the function will be `tempo-template-erlang-X' where `X' is the
74 tempo identifier as specified in the second argument of the elements
75 in this list.
77 A list with zero elements means that the a horizontal line should
78 be placed in the menu.")
80 ;; In XEmacs `user-mail-address' returns "x@y.z (Foo Bar)" ARGH!
81 ;; What's wrong with that? RFC 822 says it's legal. [sverkerw]
82 ;; This needs to use the customized value. If that's not sane, things like
83 ;; add-log will lose anyhow. Avoid it if there _is_ a paren.
84 (defvar erlang-skel-mail-address
85 (if (or (not user-mail-address) (string-match "(" user-mail-address))
86 (concat (user-login-name) "@"
87 (or (and (boundp 'mail-host-address)
88 mail-host-address)
89 (system-name)))
90 user-mail-address)
91 "Mail address of the user.")
93 ;; Expression templates:
94 (defvar erlang-skel-case
95 '((erlang-skel-skip-blank) o >
96 "case " p " of" n> p "_ ->" n> p "ok" n> "end" p)
97 "*The skeleton of a `case' expression.
98 Please see the function `tempo-define-template'.")
100 (defvar erlang-skel-if
101 '((erlang-skel-skip-blank) o >
102 "if" n> p " ->" n> p "ok" n> "end" p)
103 "The skeleton of an `if' expression.
104 Please see the function `tempo-define-template'.")
106 (defvar erlang-skel-receive
107 '((erlang-skel-skip-blank) o >
108 "receive" n> p "_ ->" n> p "ok" n> "end" p)
109 "*The skeleton of a `receive' expression.
110 Please see the function `tempo-define-template'.")
112 (defvar erlang-skel-receive-after
113 '((erlang-skel-skip-blank) o >
114 "receive" n> p "_ ->" n> p "ok" n> "after " p "T ->" n>
115 p "ok" n> "end" p)
116 "*The skeleton of a `receive' expression with an `after' clause.
117 Please see the function `tempo-define-template'.")
119 (defvar erlang-skel-receive-loop
120 '(& o "loop(" p ") ->" n> "receive" n> p "_ ->" n>
121 "loop(" p ")" n> "end.")
122 "*The skeleton of a simple `receive' loop.
123 Please see the function `tempo-define-template'.")
126 (defvar erlang-skel-function
127 '((erlang-skel-separator 2)
128 "%% @doc" n
129 "%% @spec" n
130 "%% @end" n
131 (erlang-skel-separator 2))
132 "*The template of a function skeleton.
133 Please see the function `tempo-define-template'.")
136 ;; Attribute templates
138 (defvar erlang-skel-module
139 '(& "-module("
140 (erlang-add-quotes-if-needed (erlang-get-module-from-file-name))
141 ")." n)
142 "*The skeleton of a `module' attribute.
143 Please see the function `tempo-define-template'.")
145 (defvar erlang-skel-author
146 '(& "-author('" erlang-skel-mail-address "')." n)
147 "*The skeleton of a `author' attribute.
148 Please see the function `tempo-define-template'.")
150 (defvar erlang-skel-vc nil
151 "*The skeleton template to generate a version control attribute.
152 The default is to insert nothing. Example of usage:
154 (setq erlang-skel-vc '(& \"-rcs(\\\"$\Id: $ \\\").\") n)
156 Please see the function `tempo-define-template'.")
158 (defvar erlang-skel-export
159 '(& "-export([" n> "])." n)
160 "*The skeleton of an `export' attribute.
161 Please see the function `tempo-define-template'.")
163 (defvar erlang-skel-import
164 '(& "%%-import(Module, [Function/Arity, ...])." n)
165 "*The skeleton of an `import' attribute.
166 Please see the function `tempo-define-template'.")
168 (defvar erlang-skel-compile nil
169 ;; '(& "%%-compile(export_all)." n)
170 "*The skeleton of a `compile' attribute.
171 Please see the function `tempo-define-template'.")
174 ;; Comment templates.
176 (defvar erlang-skel-date-function 'erlang-skel-dd-mmm-yyyy
177 "*Function which returns date string.
178 Look in the module `time-stamp' for a battery of functions.")
180 (defvar erlang-skel-copyright-comment
181 (if (boundp '*copyright-organization*)
182 '(& "%%% @copyright (C) " (format-time-string "%Y") ", "
183 *copyright-organization* n)
184 '(& "%%% @copyright (C) " (format-time-string "%Y") ", "
185 (user-full-name) n))
186 "*The template for a copyright line in the header, normally empty.
187 This variable should be bound to a `tempo' template, for example:
188 '(& \"%%% Copyright (C) 2000, Yoyodyne, Inc.\" n)
189 Please see the function `tempo-define-template'.")
191 (defvar erlang-skel-created-comment
192 '(& "%%% Created : " (funcall erlang-skel-date-function) " by "
193 (user-full-name) " <" erlang-skel-mail-address ">" n)
194 "*The template for the \"Created:\" comment line.")
196 (defvar erlang-skel-author-comment
197 '(& "%%% @author " (user-full-name) " <" erlang-skel-mail-address ">" n)
198 "*The template for creating the \"Author:\" line in the header.
199 Please see the function `tempo-define-template'.")
201 (defvar erlang-skel-small-header
202 '(o (erlang-skel-include erlang-skel-module)
204 (erlang-skel-include erlang-skel-compile erlang-skel-vc))
205 "*The template of a small header without any comments.
206 Please see the function `tempo-define-template'.")
208 (defvar erlang-skel-normal-header
209 '(o (erlang-skel-include erlang-skel-author-comment)
210 (erlang-skel-include erlang-skel-copyright-comment)
211 "%%% @doc" n
212 "%%%" p n
213 "%%% @end" n
214 (erlang-skel-include erlang-skel-created-comment) n
215 (erlang-skel-include erlang-skel-small-header) n)
216 "*The template of a normal header.
217 Please see the function `tempo-define-template'.")
219 (defvar erlang-skel-large-header
220 '(o (erlang-skel-separator)
221 (erlang-skel-include erlang-skel-author-comment)
222 (erlang-skel-include erlang-skel-copyright-comment)
223 "%%% @doc" n
224 "%%%" p n
225 "%%% @end" n
226 (erlang-skel-include erlang-skel-created-comment)
227 (erlang-skel-separator)
228 (erlang-skel-include erlang-skel-small-header) )
229 "*The template of a large header.
230 Please see the function `tempo-define-template'.")
233 ;; Server templates.
234 (defvar erlang-skel-small-server
235 '((erlang-skel-include erlang-skel-large-header)
236 "-export([start/0,init/1])." n n n
237 "start() ->" n> "spawn(" (erlang-get-module-from-file-name)
238 ", init, [self()])." n n
239 "init(From) ->" n>
240 "loop(From)." n n
241 "loop(From) ->" n>
242 "receive" n>
243 p "_ ->" n>
244 "loop(From)" n>
245 "end." n
247 "*Template of a small server.
248 Please see the function `tempo-define-template'.")
250 ;; Behaviour templates.
251 (defvar erlang-skel-application
252 '((erlang-skel-include erlang-skel-large-header)
253 "-behaviour(application)." n n
254 "%% Application callbacks" n
255 "-export([start/2, stop/1])." n n
256 (erlang-skel-double-separator 2)
257 "%% Application callbacks" n
258 (erlang-skel-double-separator 2)
259 (erlang-skel-separator 2)
260 "%% @private" n
261 "%% @doc" n
262 "%% This function is called whenever an application is started using" n
263 "%% application:start/[1,2], and should start the processes of the" n
264 "%% application. If the application is structured according to the OTP" n
265 "%% design principles as a supervision tree, this means starting the" n
266 "%% top supervisor of the tree." n
267 "%%" n
268 "%% @spec start(Type, StartArgs) -> {ok, Pid} |" n
269 "%% {ok, Pid, State} |" n
270 "%% {error, Reason}" n
271 "%% @end" n
272 (erlang-skel-separator 2)
273 "start(_Type, StartArgs) ->" n>
274 "case 'TopSupervisor':start_link(StartArgs) of" n>
275 "{ok, Pid} ->" n>
276 "{ok, Pid};" n>
277 "Error ->" n>
278 "Error" n>
279 "end." n
281 (erlang-skel-separator 2)
282 "%% @private" n
283 "%% @doc" n
284 "%% This function is called whenever an application has stopped. It" n
285 "%% is intended to be the opposite of Module:start/2 and should do" n
286 "%% any necessary cleaning up. The return value is ignored." n
287 "%%" n
288 "%% @spec stop(State) -> void()" n
289 "%% @end" n
290 (erlang-skel-separator 2)
291 "stop(_State) ->" n>
292 "ok." n
294 (erlang-skel-double-separator 2)
295 "%% Internal functions" n
296 (erlang-skel-double-separator 2)
298 "*The template of an application behaviour.
299 Please see the function `tempo-define-template'.")
301 (defvar erlang-skel-supervisor
302 '((erlang-skel-include erlang-skel-large-header)
303 "-behaviour(supervisor)." n n
305 "%% API" n
306 "-export([start_link/0])." n n
308 "%% Supervisor callbacks" n
309 "-export([init/1])." n n
311 "-define(SERVER, ?MODULE)." n n
313 (erlang-skel-double-separator 2)
314 "%% API functions" n
315 (erlang-skel-double-separator 2)
316 (erlang-skel-separator 2)
317 "%% @doc" n
318 "%% Starts the supervisor" n
319 "%%" n
320 "%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}" n
321 "%% @end" n
322 (erlang-skel-separator 2)
323 "start_link() ->" n>
324 "supervisor:start_link({local, ?SERVER}, ?MODULE, [])." n
326 (erlang-skel-double-separator 2)
327 "%% Supervisor callbacks" n
328 (erlang-skel-double-separator 2)
329 (erlang-skel-separator 2)
330 "%% @private" n
331 "%% @doc" n
332 "%% Whenever a supervisor is started using supervisor:start_link/[2,3]," n
333 "%% this function is called by the new process to find out about" n
334 "%% restart strategy, maximum restart frequency and child" n
335 "%% specifications." n
336 "%%" n
337 "%% @spec init(Args) -> {ok, {SupFlags, [ChildSpec]}} |" n
338 "%% ignore |" n
339 "%% {error, Reason}" n
340 "%% @end" n
341 (erlang-skel-separator 2)
342 "init([]) ->" n>
343 "AChild = {'AName', {'AModule', start_link, []}," n>
344 "permanent, 2000, worker, ['AModule']}," n>
345 "{ok, {{one_for_all, 0, 1}, [AChild]}}." n
347 (erlang-skel-double-separator 2)
348 "%% Internal functions" n
349 (erlang-skel-double-separator 2)
351 "*The template of an supervisor behaviour.
352 Please see the function `tempo-define-template'.")
354 (defvar erlang-skel-supervisor-bridge
355 '((erlang-skel-include erlang-skel-large-header)
356 "-behaviour(supervisor_bridge)." n n
358 "%% API" n
359 "-export([start_link/0])." n n
361 "%% supervisor_bridge callbacks" n
362 "-export([init/1, terminate/2])." n n
364 "-define(SERVER, ?MODULE)." n n
366 "-record(state, {})." n n
368 (erlang-skel-double-separator 2)
369 "%% API" n
370 (erlang-skel-double-separator 2)
371 (erlang-skel-separator 2)
372 "%% @doc" n
373 "%% Starts the supervisor bridge" n
374 "%%" n
375 "%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}" n
376 "%% @end" n
377 (erlang-skel-separator 2)
378 "start_link() ->" n>
379 "supervisor_bridge:start_link({local, ?SERVER}, ?MODULE, [])." n
381 (erlang-skel-double-separator 2)
382 "%% supervisor_bridge callbacks" n
383 (erlang-skel-double-separator 2)
384 (erlang-skel-separator 2)
385 "%% @private" n
386 "%% @doc" n
387 "%% Creates a supervisor_bridge process, linked to the calling process," n
388 "%% which calls Module:init/1 to start the subsystem. To ensure a" n
389 "%% synchronized start-up procedure, this function does not return" n
390 "%% until Module:init/1 has returned." n
391 "%%" n
392 "%% @spec init(Args) -> {ok, Pid, State} |" n
393 "%% ignore |" n
394 "%% {error, Reason}" n
395 "%% @end" n
396 (erlang-skel-separator 2)
397 "init([]) ->" n>
398 "case 'AModule':start_link() of" n>
399 "{ok, Pid} ->" n>
400 "{ok, Pid, #state{}};" n>
401 "Error ->" n>
402 "Error" n>
403 "end." n
405 (erlang-skel-separator 2)
406 "%% @private" n
407 "%% @doc" n
408 "%% This function is called by the supervisor_bridge when it is about" n
409 "%% to terminate. It should be the opposite of Module:init/1 and stop" n
410 "%% the subsystem and do any necessary cleaning up.The return value is" n
411 "%% ignored." n
412 "%%" n
413 "%% @spec terminate(Reason, State) -> void()" n
414 "%% @end" n
415 (erlang-skel-separator 2)
416 "terminate(Reason, State) ->" n>
417 "'AModule':stop()," n>
418 "ok." n
420 (erlang-skel-double-separator 2)
421 "%% Internal functions" n
422 (erlang-skel-double-separator 2)
424 "*The template of an supervisor_bridge behaviour.
425 Please see the function `tempo-define-template'.")
427 (defvar erlang-skel-generic-server
428 '((erlang-skel-include erlang-skel-large-header)
429 "-behaviour(gen_server)." n n
431 "%% API" n
432 "-export([start_link/0])." n n
434 "%% gen_server callbacks" n
435 "-export([init/1, handle_call/3, handle_cast/2, "
436 "handle_info/2," n>
437 "terminate/2, code_change/3])." n n
439 "-define(SERVER, ?MODULE). " n n
441 "-record(state, {})." n n
443 (erlang-skel-double-separator 2)
444 "%% API" n
445 (erlang-skel-double-separator 2)
446 (erlang-skel-separator 2)
447 "%% @doc" n
448 "%% Starts the server" n
449 "%%" n
450 "%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}" n
451 "%% @end" n
452 (erlang-skel-separator 2)
453 "start_link() ->" n>
454 "gen_server:start_link({local, ?SERVER}, ?MODULE, [], [])." n
456 (erlang-skel-double-separator 2)
457 "%% gen_server callbacks" n
458 (erlang-skel-double-separator 2)
460 (erlang-skel-separator 2)
461 "%% @private" n
462 "%% @doc" n
463 "%% Initiates the server" n
464 "%%" n
465 "%% @spec init(Args) -> {ok, State} |" n
466 "%% {ok, State, Timeout} |" n
467 "%% ignore |" n
468 "%% {stop, Reason}" n
469 "%% @end" n
470 (erlang-skel-separator 2)
471 "init([]) ->" n>
472 "{ok, #state{}}." n
474 (erlang-skel-separator 2)
475 "%% @private" n
476 "%% @doc" n
477 "%% Handling call messages" n
478 "%%" n
479 "%% @spec handle_call(Request, From, State) ->" n
480 "%% {reply, Reply, State} |" n
481 "%% {reply, Reply, State, Timeout} |" n
482 "%% {noreply, State} |" n
483 "%% {noreply, State, Timeout} |" n
484 "%% {stop, Reason, Reply, State} |" n
485 "%% {stop, Reason, State}" n
486 "%% @end" n
487 (erlang-skel-separator 2)
488 "handle_call(_Request, _From, State) ->" n>
489 "Reply = ok," n>
490 "{reply, Reply, State}." n
492 (erlang-skel-separator 2)
493 "%% @private" n
494 "%% @doc" n
495 "%% Handling cast messages" n
496 "%%" n
497 "%% @spec handle_cast(Msg, State) -> {noreply, State} |" n
498 "%% {noreply, State, Timeout} |" n
499 "%% {stop, Reason, State}" n
500 "%% @end" n
502 (erlang-skel-separator 2)
503 "handle_cast(_Msg, State) ->" n>
504 "{noreply, State}." n
506 (erlang-skel-separator 2)
507 "%% @private" n
508 "%% @doc" n
509 "%% Handling all non call/cast messages" n
510 "%%" n
511 "%% @spec handle_info(Info, State) -> {noreply, State} |" n
512 "%% {noreply, State, Timeout} |" n
513 "%% {stop, Reason, State}" n
514 "%% @end" n
515 (erlang-skel-separator 2)
516 "handle_info(_Info, State) ->" n>
517 "{noreply, State}." n
519 (erlang-skel-separator 2)
520 "%% @private" n
521 "%% @doc" n
522 "%% This function is called by a gen_server when it is about to" n
523 "%% terminate. It should be the opposite of Module:init/1 and do any" n
524 "%% necessary cleaning up. When it returns, the gen_server terminates" n
525 "%% with Reason. The return value is ignored." n
526 "%%" n
527 "%% @spec terminate(Reason, State) -> void()" n
528 "%% @end" n
529 (erlang-skel-separator 2)
530 "terminate(_Reason, _State) ->" n>
531 "ok." n
533 (erlang-skel-separator 2)
534 "%% @private" n
535 "%% @doc" n
536 "%% Convert process state when code is changed" n
537 "%%" n
538 "%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}" n
539 "%% @end" n
540 (erlang-skel-separator 2)
541 "code_change(_OldVsn, State, _Extra) ->" n>
542 "{ok, State}." n
544 (erlang-skel-double-separator 2)
545 "%%% Internal functions" n
546 (erlang-skel-double-separator 2)
548 "*The template of a generic server.
549 Please see the function `tempo-define-template'.")
551 (defvar erlang-skel-gen-event
552 '((erlang-skel-include erlang-skel-large-header)
553 "-behaviour(gen_event)." n
555 "%% API" n
556 "-export([start_link/0, add_handler/0])." n n
558 "%% gen_event callbacks" n
559 "-export([init/1, handle_event/2, handle_call/2, " n>
560 "handle_info/2, terminate/2, code_change/3])." n n
562 "-record(state, {})." n n
564 (erlang-skel-double-separator 2)
565 "%% gen_event callbacks" n
566 (erlang-skel-double-separator 2)
567 (erlang-skel-separator 2)
568 "%% @doc" n
569 "%% Creates an event manager" n
570 "%%" n
571 "%% @spec start_link() -> {ok, Pid} | {error, Error}" n
572 "%% @end" n
573 (erlang-skel-separator 2)
574 "start_link() ->" n>
575 "gen_event:start_link({local, ?SERVER})." n
577 (erlang-skel-separator 2)
578 "%% @doc" n
579 "%% Adds an event handler" n
580 "%%" n
581 "%% @spec add_handler() -> ok | {'EXIT', Reason} | term()" n
582 "%% @end" n
583 (erlang-skel-separator 2)
584 "add_handler() ->" n>
585 "gen_event:add_handler(?SERVER, ?MODULE, [])." n
587 (erlang-skel-double-separator 2)
588 "%% gen_event callbacks" n
589 (erlang-skel-double-separator 2)
590 (erlang-skel-separator 2)
591 "%% @private" n
592 "%% @doc" n
593 "%% Whenever a new event handler is added to an event manager," n
594 "%% this function is called to initialize the event handler." n
595 "%%" n
596 "%% @spec init(Args) -> {ok, State}" n
597 "%% @end" n
598 (erlang-skel-separator 2)
599 "init([]) ->" n>
600 "{ok, #state{}}." n
602 (erlang-skel-separator 2)
603 "%% @private" n
604 "%% @doc" n
605 "%% Whenever an event manager receives an event sent using" n
606 "%% gen_event:notify/2 or gen_event:sync_notify/2, this function is" n
607 "%% called for each installed event handler to handle the event." n
608 "%%" n
609 "%% @spec handle_event(Event, State) ->" n
610 "%% {ok, State} |" n
611 "%% {swap_handler, Args1, State1, Mod2, Args2} |"n
612 "%% remove_handler" n
613 "%% @end" n
614 (erlang-skel-separator 2)
615 "handle_event(_Event, State) ->" n>
616 "{ok, State}." n
618 (erlang-skel-separator 2)
619 "%% @private" n
620 "%% @doc" n
621 "%% Whenever an event manager receives a request sent using" n
622 "%% gen_event:call/3,4, this function is called for the specified" n
623 "%% event handler to handle the request." n
624 "%%" n
625 "%% @spec handle_call(Request, State) ->" n
626 "%% {ok, Reply, State} |" n
627 "%% {swap_handler, Reply, Args1, State1, Mod2, Args2} |" n
628 "%% {remove_handler, Reply}" n
629 "%% @end" n
630 (erlang-skel-separator 2)
631 "handle_call(_Request, State) ->" n>
632 "Reply = ok," n>
633 "{ok, Reply, State}." n
635 (erlang-skel-separator 2)
636 "%% @private" n
637 "%% @doc" n
638 "%% This function is called for each installed event handler when" n
639 "%% an event manager receives any other message than an event or a" n
640 "%% synchronous request (or a system message)." n
641 "%%" n
642 "%% @spec handle_info(Info, State) ->" n
643 "%% {ok, State} |" n
644 "%% {swap_handler, Args1, State1, Mod2, Args2} |" n
645 "%% remove_handler" n
646 "%% @end" n
647 (erlang-skel-separator 2)
648 "handle_info(_Info, State) ->" n>
649 "{ok, State}." n
651 (erlang-skel-separator 2)
652 "%% @private" n
653 "%% @doc" n
654 "%% Whenever an event handler is deleted from an event manager, this" n
655 "%% function is called. It should be the opposite of Module:init/1 and" n
656 "%% do any necessary cleaning up." n
657 "%%" n
658 "%% @spec terminate(Reason, State) -> void()" n
659 "%% @end" n
660 (erlang-skel-separator 2)
661 "terminate(_Reason, _State) ->" n>
662 "ok." n
664 (erlang-skel-separator 2)
665 "%% @private" n
666 "%% @doc" n
667 "%% Convert process state when code is changed" n
668 "%%" n
669 "%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}" n
670 "%% @end" n
671 (erlang-skel-separator 2)
672 "code_change(_OldVsn, State, _Extra) ->" n>
673 "{ok, State}." n
675 (erlang-skel-double-separator 2)
676 "%%% Internal functions" n
677 (erlang-skel-double-separator 2)
679 "*The template of a gen_event.
680 Please see the function `tempo-define-template'.")
682 (defvar erlang-skel-gen-fsm
683 '((erlang-skel-include erlang-skel-large-header)
684 "-behaviour(gen_fsm)." n n
686 "%% API" n
687 "-export([start_link/0])." n n
689 "%% gen_fsm callbacks" n
690 "-export([init/1, state_name/2, state_name/3, handle_event/3," n>
691 "handle_sync_event/4, handle_info/3, terminate/3, code_change/4])." n n
693 "-record(state, {})." n n
695 (erlang-skel-double-separator 2)
696 "%% API" n
697 (erlang-skel-double-separator 2)
698 (erlang-skel-separator 2)
699 "%% @doc" n
700 "%% Creates a gen_fsm process which calls Module:init/1 to" n
701 "%% initialize. To ensure a synchronized start-up procedure, this" n
702 "%% function does not return until Module:init/1 has returned." n
703 "%%" n
704 "%% @spec start_link() -> {ok, Pid} | ignore | {error, Error}" n
705 "%% @end" n
706 (erlang-skel-separator 2)
707 "start_link() ->" n>
708 "gen_fsm:start_link({local, ?SERVER}, ?MODULE, [], [])." n
710 (erlang-skel-double-separator 2)
711 "%% gen_fsm callbacks" n
712 (erlang-skel-double-separator 2)
713 (erlang-skel-separator 2)
714 "%% @private" n
715 "%% @doc" n
716 "%% Whenever a gen_fsm is started using gen_fsm:start/[3,4] or" n
717 "%% gen_fsm:start_link/[3,4], this function is called by the new" n
718 "%% process to initialize." n
719 "%%" n
720 "%% @spec init(Args) -> {ok, StateName, State} |" n
721 "%% {ok, StateName, State, Timeout} |" n
722 "%% ignore |" n
723 "%% {stop, StopReason}" n
724 "%% @end" n
725 (erlang-skel-separator 2)
726 "init([]) ->" n>
727 "{ok, state_name, #state{}}." n
729 (erlang-skel-separator 2)
730 "%% @private" n
731 "%% @doc" n
732 "%% There should be one instance of this function for each possible" n
733 "%% state name. Whenever a gen_fsm receives an event sent using" n
734 "%% gen_fsm:send_event/2, the instance of this function with the same" n
735 "%% name as the current state name StateName is called to handle" n
736 "%% the event. It is also called if a timeout occurs." n
737 "%%" n
738 "%% @spec state_name(Event, State) ->" n
739 "%% {next_state, NextStateName, NextState} |" n
740 "%% {next_state, NextStateName, NextState, Timeout} |" n
741 "%% {stop, Reason, NewState}" n
742 "%% @end" n
743 (erlang-skel-separator 2)
744 "state_name(_Event, State) ->" n>
745 "{next_state, state_name, State}." n
747 (erlang-skel-separator 2)
748 "%% @private" n
749 "%% @doc" n
750 "%% There should be one instance of this function for each possible" n
751 "%% state name. Whenever a gen_fsm receives an event sent using" n
752 "%% gen_fsm:sync_send_event/[2,3], the instance of this function with" n
753 "%% the same name as the current state name StateName is called to" n
754 "%% handle the event." n
755 "%%" n
756 "%% @spec state_name(Event, From, State) ->" n
757 "%% {next_state, NextStateName, NextState} |"n
758 "%% {next_state, NextStateName, NextState, Timeout} |" n
759 "%% {reply, Reply, NextStateName, NextState} |" n
760 "%% {reply, Reply, NextStateName, NextState, Timeout} |" n
761 "%% {stop, Reason, NewState} |" n
762 "%% {stop, Reason, Reply, NewState}" n
763 "%% @end" n
764 (erlang-skel-separator 2)
765 "state_name(_Event, _From, State) ->" n>
766 "Reply = ok," n>
767 "{reply, Reply, state_name, State}." n
769 (erlang-skel-separator 2)
770 "%% @private" n
771 "%% @doc" n
772 "%% Whenever a gen_fsm receives an event sent using" n
773 "%% gen_fsm:send_all_state_event/2, this function is called to handle" n
774 "%% the event." n
775 "%%" n
776 "%% @spec handle_event(Event, StateName, State) ->" n
777 "%% {next_state, NextStateName, NextState} |" n
778 "%% {next_state, NextStateName, NextState, Timeout} |" n
779 "%% {stop, Reason, NewState}" n
780 "%% @end" n
781 (erlang-skel-separator 2)
782 "handle_event(_Event, StateName, State) ->" n>
783 "{next_state, StateName, State}." n
785 (erlang-skel-separator 2)
786 "%% @private" n
787 "%% @doc" n
788 "%% Whenever a gen_fsm receives an event sent using" n
789 "%% gen_fsm:sync_send_all_state_event/[2,3], this function is called" n
790 "%% to handle the event." n
791 "%%" n
792 "%% @spec handle_sync_event(Event, From, StateName, State) ->" n
793 "%% {next_state, NextStateName, NextState} |" n
794 "%% {next_state, NextStateName, NextState, Timeout} |" n
795 "%% {reply, Reply, NextStateName, NextState} |" n
796 "%% {reply, Reply, NextStateName, NextState, Timeout} |" n
797 "%% {stop, Reason, NewState} |" n
798 "%% {stop, Reason, Reply, NewState}" n
799 "%% @end" n
800 (erlang-skel-separator 2)
801 "handle_sync_event(Event, From, StateName, State) ->" n>
802 "Reply = ok," n>
803 "{reply, Reply, StateName, State}." n
805 (erlang-skel-separator 2)
806 "%% @private" n
807 "%% @doc" n
808 "%% This function is called by a gen_fsm when it receives any" n
809 "%% message other than a synchronous or asynchronous event" n
810 "%% (or a system message)." n
811 "%%" n
812 "%% @spec handle_info(Info,StateName,State)->" n
813 "%% {next_state, NextStateName, NextState} |" n
814 "%% {next_state, NextStateName, NextState, Timeout} |" n
815 "%% {stop, Reason, NewState}" n
816 "%% @end" n
817 (erlang-skel-separator 2)
818 "handle_info(_Info, StateName, State) ->" n>
819 "{next_state, StateName, State}." n
821 (erlang-skel-separator 2)
822 "%% @private" n
823 "%% @doc" n
824 "%% This function is called by a gen_fsm when it is about to" n
825 "%% terminate. It should be the opposite of Module:init/1 and do any" n
826 "%% necessary cleaning up. When it returns, the gen_fsm terminates with" n
827 "%% Reason. The return value is ignored." n
828 "%%" n
829 "%% @spec terminate(Reason, StateName, State) -> void()" n
830 "%% @end" n
831 (erlang-skel-separator 2)
832 "terminate(_Reason, _StateName, _State) ->" n>
833 "ok." n
835 (erlang-skel-separator 2)
836 "%% @private" n
837 "%% @doc" n
838 "%% Convert process state when code is changed" n
839 "%%" n
840 "%% @spec code_change(OldVsn, StateName, State, Extra) ->" n
841 "%% {ok, StateName, NewState}" n
842 "%% @end" n
843 (erlang-skel-separator 2)
844 "code_change(_OldVsn, StateName, State, _Extra) ->" n>
845 "{ok, StateName, State}." n
847 (erlang-skel-double-separator 2)
848 "%%% Internal functions" n
849 (erlang-skel-double-separator 2)
851 "*The template of a gen_fsm.
852 Please see the function `tempo-define-template'.")
854 (defvar erlang-skel-lib
855 '((erlang-skel-include erlang-skel-large-header)
857 "%% API" n
858 "-export([])." n n
860 (erlang-skel-double-separator 2)
861 "%% API" n
862 (erlang-skel-double-separator 2)
863 (erlang-skel-separator 2)
864 "%% @doc" n
865 "%% @spec" n
866 "%% @end" n
867 (erlang-skel-separator 2)
869 (erlang-skel-double-separator 2)
870 "%% Internal functions" n
871 (erlang-skel-double-separator 2)
873 "*The template of a library module.
874 Please see the function `tempo-define-template'.")
876 (defvar erlang-skel-corba-callback
877 '((erlang-skel-include erlang-skel-large-header)
878 "%% Include files" n n
880 "%% API" n
881 "-export([])." n n
883 "%% Corba callbacks" n
884 "-export([init/1, terminate/2, code_change/3])." n n
886 "-record(state, {})." n n
888 (erlang-skel-double-separator 2)
889 "%% Corba callbacks" n
890 (erlang-skel-double-separator 2)
891 (erlang-skel-separator 2)
892 "%% @private" n
893 "%% @doc" n
894 "%% Initiates the server" n
895 "%%" n
896 "%% @spec init(Args) -> {ok, State} |" n
897 "%% {ok, State, Timeout} |" n
898 "%% ignore |" n
899 "%% {stop, Reason}" n
900 "%% @end" n
901 (erlang-skel-separator 2)
902 "init([]) ->" n>
903 "{ok, #state{}}." n
905 (erlang-skel-separator 2)
906 "%% @private" n
907 "%% @doc" n
908 "%% Shutdown the server" n
909 "%%" n
910 "%% @spec terminate(Reason, State) -> void()" n
911 "%% @end" n
912 (erlang-skel-separator 2)
913 "terminate(_Reason, _State) ->" n>
914 "ok." n
916 (erlang-skel-separator 2)
917 "%% @private" n
918 "%% @doc" n
919 "%% Convert process state when code is changed" n
920 "%%" n
921 "%% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}" n
922 "%% @end" n
923 (erlang-skel-separator 2)
924 "code_change(_OldVsn, State, _Extra) ->" n>
925 "{ok, State}." n
927 (erlang-skel-double-separator 2)
928 "%% Internal functions" n
929 (erlang-skel-double-separator 2)
931 "*The template of a library module.
932 Please see the function `tempo-define-template'.")
934 (defvar erlang-skel-ts-test-suite
935 '((erlang-skel-include erlang-skel-large-header)
936 "%% Note: This directive should only be used in test suites." n
937 "-compile(export_all)." n n
939 "-include(\"test_server.hrl\")." n n
941 "%% Test server callback functions" n
942 (erlang-skel-separator 2)
943 "%% @doc" n
944 "%% Config - [tuple()]" n
945 "%% A list of key/value pairs, holding the test case configuration." n
946 "%%" n
947 "%% Initiation before the whole suite" n
948 "%%" n
949 "%% Note: This function is free to add any key/value pairs to the Config" n
950 "%% variable, but should NOT alter/remove any existing entries." n
951 "%%" n
952 "%% @spec init_per_suite(Config) -> Config" n
953 "%% @end" n
954 (erlang-skel-separator 2)
955 "init_per_suite(Config) ->" n >
956 "Config." n n
958 (erlang-skel-separator 2)
959 "%% @doc" n
960 "%% Config - [tuple()]" n
961 "%% A list of key/value pairs, holding the test case configuration." n
962 "%%" n
963 "%% Cleanup after the whole suite" n
964 "%%" n
965 "%% @spec end_per_suite(Config) -> _" n
966 "%% @end" n
967 (erlang-skel-separator 2)
968 "end_per_suite(_Config) ->" n >
969 "ok." n n
971 (erlang-skel-separator 2)
972 "%% @doc" n
973 "%% Case - atom()" n
974 "%% Name of the test case that is about to be run." n
975 "%% Config - [tuple()]" n
976 "%% A list of key/value pairs, holding the test case configuration." n
977 "%%" n
978 "%% Initiation before each test case" n
979 "%%" n
980 "%% Note: This function is free to add any key/value pairs to the Config" n
981 "%% variable, but should NOT alter/remove any existing entries." n
982 "%%" n
983 "%% @spec init_per_testcase(TestCase, Config) -> Config" n
984 "%% @end" n
985 (erlang-skel-separator 2)
986 "init_per_testcase(_TestCase, Config) ->" n >
987 "Config." n n
989 (erlang-skel-separator 2)
990 "%% @doc" n
991 "%% Case - atom()" n
992 "%% Name of the test case that is about to be run." n
993 "%% Config - [tuple()]" n
994 "%% A list of key/value pairs, holding the test case configuration." n
995 "%%" n
996 "%% Cleanup after each test case" n
997 "%%" n
998 "%% @spec end_per_testcase(TestCase, Config) -> _" n
999 "%% @end" n
1000 (erlang-skel-separator 2)
1001 "end_per_testcase(_TestCase, _Config) ->" n >
1002 "ok."n n
1004 (erlang-skel-separator 2)
1005 "%% @doc" n
1006 "%% Clause - atom() - suite | doc" n
1007 "%% TestCases - [Case]" n
1008 "%% Case - atom()" n
1009 "%% Name of a test case." n
1010 "%%" n
1011 "%% Returns a list of all test cases in this test suite" n
1012 "%%" n
1013 "%% @spec all(Clause) -> TestCases" n
1014 "%% @end" n
1015 (erlang-skel-separator 2)
1016 "all(doc) ->" n >
1017 "[\"Describe the main purpose of this suite\"];" n n
1018 "all(suite) ->" n >
1019 "[]." n n
1021 "%% Test cases starts here." n
1022 (erlang-skel-separator 2)
1023 "test_case(doc) ->" n >
1024 "[\"Describe the main purpose of test case\"];" n n
1025 "test_case(suite) ->" n >
1026 "[];" n n
1027 "test_case(Config) when is_list(Config) ->" n >
1028 "ok." n
1030 "*The template of a library module.
1031 Please see the function `tempo-define-template'.")
1033 (defvar erlang-skel-ct-test-suite
1034 '((erlang-skel-include erlang-skel-large-header)
1035 "-suite_defaults([{timetrap, {minutes, 10}}])." n n
1037 "%% Note: This directive should only be used in test suites." n
1038 "-compile(export_all)." n n
1040 "-include(\"ct.hrl\")." n n
1042 "%% Test server callback functions" n
1043 (erlang-skel-separator 2)
1044 "%% @doc" n
1045 "%% Config - [tuple()]" n
1046 "%% A list of key/value pairs, holding the test case configuration." n
1047 "%%" n
1048 "%% Initiation before the whole suite" n
1049 "%%" n
1050 "%% Note: This function is free to add any key/value pairs to the Config" n
1051 "%% variable, but should NOT alter/remove any existing entries." n
1052 "%%" n
1053 "%% @spec init_per_suite(Config) -> Config" n
1054 "%% @end" n
1055 (erlang-skel-separator 2)
1056 "init_per_suite(Config) ->" n >
1057 "Config." n n
1059 (erlang-skel-separator 2)
1060 "%% @doc" n
1061 "%% Config - [tuple()]" n
1062 "%% A list of key/value pairs, holding the test case configuration." n
1063 "%%" n
1064 "%% Cleanup after the whole suite" n
1065 "%%" n
1066 "%% @spec end_per_suite(Config) -> _" n
1067 "%% @end" n
1068 (erlang-skel-separator 2)
1069 "end_per_suite(_Config) ->" n >
1070 "ok." n n
1072 (erlang-skel-separator 2)
1073 "%% @doc" n
1074 "%% Case - atom()" n
1075 "%% Name of the test case that is about to be run." n
1076 "%% Config - [tuple()]" n
1077 "%% A list of key/value pairs, holding the test case configuration." n
1078 "%%" n
1079 "%% Initiation before each test case" n
1080 "%%" n
1081 "%% Note: This function is free to add any key/value pairs to the Config" n
1082 "%% variable, but should NOT alter/remove any existing entries." n
1083 "%% Initiation before each test case" n
1084 "%%" n
1085 "%% @spec init_per_testcase(TestCase, Config) -> Config" n
1086 "%% @end" n
1087 (erlang-skel-separator 2)
1088 "init_per_testcase(_TestCase, Config) ->" n >
1089 "Config." n n
1091 (erlang-skel-separator 2)
1092 "%% @doc" n
1093 "%% Case - atom()" n
1094 "%% Name of the test case that is about to be run." n
1095 "%% Config - [tuple()]" n
1096 "%% A list of key/value pairs, holding the test case configuration." n
1097 "%%" n
1098 "%% Cleanup after each test case" n
1099 "%%" n
1100 "%% @spec end_per_testcase(TestCase, Config) -> _" n
1101 "%% @end" n
1102 (erlang-skel-separator 2)
1103 "end_per_testcase(_TestCase, _Config) ->" n >
1104 "ok."n n
1106 (erlang-skel-separator 2)
1107 "%% @doc" n
1108 "%% TestCases - [Case]" n
1109 "%% Case - atom()" n
1110 "%% Name of a test case." n
1111 "%%" n
1112 "%% Returns a list of all test cases in this test suite" n
1113 "%%" n
1114 "%% @spec all() -> TestCases" n
1115 "%% @end" n
1116 (erlang-skel-separator 2)
1117 "all() ->" n >
1118 "[]." n n
1120 "%% Test cases starts here." n
1121 (erlang-skel-separator 2)
1122 "test_case() ->" n >
1123 "[{doc, \"Describe the main purpose of this test case\"}]." n n
1124 "test_case(Config) when is_list(Config) ->" n >
1125 "ok." n
1127 "*The template of a library module.
1128 Please see the function `tempo-define-template'.")
1130 ;; Skeleton code:
1132 ;; This code is based on the package `tempo' which is part of modern
1133 ;; Emacsen. (GNU Emacs 19.25 (?) and XEmacs 19.14.)
1135 (defun erlang-skel-init ()
1136 "Generate the skeleton functions and menu items.
1137 The variable `erlang-skel' contains the name and descriptions of
1138 all skeletons.
1140 The skeleton routines are based on the `tempo' package. Should this
1141 package not be present, this function does nothing."
1142 (interactive)
1143 (condition-case nil
1144 (require 'tempo)
1145 (error t))
1146 (if (featurep 'tempo)
1147 (let ((skel erlang-skel)
1148 (menu '()))
1149 (while skel
1150 (cond ((null (car skel))
1151 (setq menu (cons nil menu)))
1153 (funcall (symbol-function 'tempo-define-template)
1154 (concat "erlang-" (nth 1 (car skel)))
1155 ;; The tempo template used contains an `include'
1156 ;; function call only, hence changes to the
1157 ;; variables describing the templates take effect
1158 ;; immdiately.
1159 (list (list 'erlang-skel-include (nth 2 (car skel))))
1160 (nth 1 (car skel)))
1161 (setq menu (cons (erlang-skel-make-menu-item
1162 (car skel)) menu))))
1163 (setq skel (cdr skel)))
1164 (setq erlang-menu-skel-items
1165 (list nil (list "Skeletons" (nreverse menu))))
1166 (setq erlang-menu-items
1167 (erlang-menu-add-above 'erlang-menu-skel-items
1168 'erlang-menu-version-items
1169 erlang-menu-items))
1170 (erlang-menu-init))))
1172 (defun erlang-skel-make-menu-item (skel)
1173 (let ((func (intern (concat "tempo-template-erlang-" (nth 1 skel)))))
1174 (cond ((null (nth 3 skel))
1175 (list (car skel) func))
1177 (list (car skel)
1178 (list 'lambda '()
1179 '(interactive)
1180 (list 'funcall
1181 (list 'quote (nth 3 skel))
1182 (list 'quote func))))))))
1184 ;; Functions designed to be added to the skeleton menu.
1185 ;; (Not normally used)
1186 (defun erlang-skel-insert (func)
1187 "Insert skeleton generated by FUNC and goto first tempo mark."
1188 (save-excursion (funcall func))
1189 (funcall (symbol-function 'tempo-forward-mark)))
1191 (defun erlang-skel-header (func)
1192 "Insert the header generated by FUNC at the beginning of the buffer."
1193 (goto-char (point-min))
1194 (save-excursion (funcall func))
1195 (funcall (symbol-function 'tempo-forward-mark)))
1198 ;; Functions used inside the skeleton descriptions.
1199 (defun erlang-skel-skip-blank ()
1200 (skip-chars-backward " \t")
1201 nil)
1203 (defun erlang-skel-include (&rest args)
1204 "Include a template inside another template.
1206 Example of use, assuming that `erlang-skel-func' is defined:
1208 (defvar foo-skeleton '(\"%%% New function:\"
1209 (erlang-skel-include erlang-skel-func)))
1211 Technically, this function returns the `tempo' attribute`(l ...)' which
1212 can contain other `tempo' attributes. Please see the function
1213 `tempo-define-template' for a description of the `(l ...)' attribute."
1214 (let ((res '())
1215 entry)
1216 (while args
1217 (setq entry (car args))
1218 (while entry
1219 (setq res (cons (car entry) res))
1220 (setq entry (cdr entry)))
1221 (setq args (cdr args)))
1222 (cons 'l (nreverse res))))
1224 (defun erlang-skel-separator (&optional percent)
1225 "Return a comment separator."
1226 (let ((percent (or percent 3)))
1227 (concat (make-string percent ?%)
1228 (make-string (- 70 percent) ?-)
1229 "\n")))
1231 (defun erlang-skel-double-separator (&optional percent)
1232 "Return a comment separator."
1233 (let ((percent (or percent 3)))
1234 (concat (make-string percent ?%)
1235 (make-string (- 70 percent) ?=)
1236 "\n")))
1238 (defun erlang-skel-dd-mmm-yyyy ()
1239 "Return the current date as a string in \"DD Mon YYYY\" form.
1240 The first character of DD is space if the value is less than 10."
1241 (let ((date (current-time-string)))
1242 (format "%2d %s %s"
1243 (string-to-int (substring date 8 10))
1244 (substring date 4 7)
1245 (substring date -4))))
1247 ;; Local variables:
1248 ;; coding: iso-8859-1
1249 ;; End:
1251 ;;; erlang.el ends here