@@ -161,6 +161,320 @@ snippet gen_server
161161 code_change(_OldVsn, State, _Extra) ->
162162 {ok, State}.
163163
164+ %%%===================================================================
165+ %%% Internal functions
166+ %%%===================================================================
167+ # OTP gen_fsm
168+ snippet gen_fsm
169+ -module(${0: `vim_snippets#Filename('', 'my')` } ).
170+
171+ -behaviour(gen_fsm).
172+
173+ %% API
174+ -export([start_link/0]).
175+
176+ %% gen_fsm callbacks
177+ -export([init/1,
178+ state_name/2,
179+ state_name/3,
180+ handle_event/3,
181+ handle_sync_event/4,
182+ handle_info/3,
183+ terminate/3,
184+ code_change/4]).
185+
186+ -record(state, {}).
187+
188+ %%%===================================================================
189+ %%% API
190+ %%%===================================================================
191+
192+ %%--------------------------------------------------------------------
193+ %% @doc
194+ %% Creates a gen_fsm process which calls Module:init/1 to
195+ %% initialize. To ensure a synchronized start-up procedure, this
196+ %% function does not return until Module:init/1 has returned.
197+ %%
198+ %% @spec start_link() -> {ok, Pid} | ignore | {error, Error}
199+ %% @end
200+ %%--------------------------------------------------------------------
201+ start_link() ->
202+ gen_fsm:start_link({local, ?MODULE}, ?MODULE, [], []).
203+
204+ %%%===================================================================
205+ %%% gen_fsm callbacks
206+ %%%===================================================================
207+
208+ %%--------------------------------------------------------------------
209+ %% @private
210+ %% @doc
211+ %% Whenever a gen_fsm is started using gen_fsm:start/[3,4] or
212+ %% gen_fsm:start_link/[3,4], this function is called by the new
213+ %% process to initialize.
214+ %%
215+ %% @spec init(Args) -> {ok, StateName, State} |
216+ %% {ok, StateName, State, Timeout} |
217+ %% ignore |
218+ %% {stop, StopReason}
219+ %% @end
220+ %%--------------------------------------------------------------------
221+ init([]) ->
222+ {ok, state_name, #state{}}.
223+
224+ %%--------------------------------------------------------------------
225+ %% @private
226+ %% @doc
227+ %% There should be one instance of this function for each possible
228+ %% state name. Whenever a gen_fsm receives an event sent using
229+ %% gen_fsm:send_event/2, the instance of this function with the same
230+ %% name as the current state name StateName is called to handle
231+ %% the event. It is also called if a timeout occurs.
232+ %%
233+ %% @spec state_name(Event, State) ->
234+ %% {next_state, NextStateName, NextState} |
235+ %% {next_state, NextStateName, NextState, Timeout} |
236+ %% {stop, Reason, NewState}
237+ %% @end
238+ %%--------------------------------------------------------------------
239+ state_name(_Event, State) ->
240+ {next_state, state_name, State}.
241+
242+ %%--------------------------------------------------------------------
243+ %% @private
244+ %% @doc
245+ %% There should be one instance of this function for each possible
246+ %% state name. Whenever a gen_fsm receives an event sent using
247+ %% gen_fsm:sync_send_event/[2,3], the instance of this function with
248+ %% the same name as the current state name StateName is called to
249+ %% handle the event.
250+ %%
251+ %% @spec state_name(Event, From, State) ->
252+ %% {next_state, NextStateName, NextState} |
253+ %% {next_state, NextStateName, NextState, Timeout} |
254+ %% {reply, Reply, NextStateName, NextState} |
255+ %% {reply, Reply, NextStateName, NextState, Timeout} |
256+ %% {stop, Reason, NewState} |
257+ %% {stop, Reason, Reply, NewState}
258+ %% @end
259+ %%--------------------------------------------------------------------
260+ state_name(_Event, _From, State) ->
261+ Reply = ok,
262+ {reply, Reply, state_name, State}.
263+
264+ %%--------------------------------------------------------------------
265+ %% @private
266+ %% @doc
267+ %% Whenever a gen_fsm receives an event sent using
268+ %% gen_fsm:send_all_state_event/2, this function is called to handle
269+ %% the event.
270+ %%
271+ %% @spec handle_event(Event, StateName, State) ->
272+ %% {next_state, NextStateName, NextState} |
273+ %% {next_state, NextStateName, NextState, Timeout} |
274+ %% {stop, Reason, NewState}
275+ %% @end
276+ %%--------------------------------------------------------------------
277+ handle_event(_Event, StateName, State) ->
278+ {next_state, StateName, State}.
279+
280+ %%--------------------------------------------------------------------
281+ %% @private
282+ %% @doc
283+ %% Whenever a gen_fsm receives an event sent using
284+ %% gen_fsm:sync_send_all_state_event/[2,3], this function is called
285+ %% to handle the event.
286+ %%
287+ %% @spec handle_sync_event(Event, From, StateName, State) ->
288+ %% {next_state, NextStateName, NextState} |
289+ %% {next_state, NextStateName, NextState, Timeout} |
290+ %% {reply, Reply, NextStateName, NextState} |
291+ %% {reply, Reply, NextStateName, NextState, Timeout} |
292+ %% {stop, Reason, NewState} |
293+ %% {stop, Reason, Reply, NewState}
294+ %% @end
295+ %%--------------------------------------------------------------------
296+ handle_sync_event(_Event, _From, StateName, State) ->
297+ Reply = ok,
298+ {reply, Reply, StateName, State}.
299+
300+ %%--------------------------------------------------------------------
301+ %% @private
302+ %% @doc
303+ %% This function is called by a gen_fsm when it receives any
304+ %% message other than a synchronous or asynchronous event
305+ %% (or a system message).
306+ %%
307+ %% @spec handle_info(Info,StateName,State)->
308+ %% {next_state, NextStateName, NextState} |
309+ %% {next_state, NextStateName, NextState, Timeout} |
310+ %% {stop, Reason, NewState}
311+ %% @end
312+ %%--------------------------------------------------------------------
313+ handle_info(_Info, StateName, State) ->
314+ {next_state, StateName, State}.
315+
316+ %%--------------------------------------------------------------------
317+ %% @private
318+ %% @doc
319+ %% This function is called by a gen_fsm when it is about to
320+ %% terminate. It should be the opposite of Module:init/1 and do any
321+ %% necessary cleaning up. When it returns, the gen_fsm terminates with
322+ %% Reason. The return value is ignored.
323+ %%
324+ %% @spec terminate(Reason, StateName, State) -> void()
325+ %% @end
326+ %%--------------------------------------------------------------------
327+ terminate(_Reason, _StateName, _State) ->
328+ ok.
329+
330+ %%--------------------------------------------------------------------
331+ %% @private
332+ %% @doc
333+ %% Convert process state when code is changed
334+ %%
335+ %% @spec code_change(OldVsn, StateName, State, Extra) ->
336+ %% {ok, StateName, NewState}
337+ %% @end
338+ %%--------------------------------------------------------------------
339+ code_change(_OldVsn, StateName, State, _Extra) ->
340+ {ok, StateName, State}.
341+
342+ %%%===================================================================
343+ %%% Internal functions
344+ %%%===================================================================
345+ # OTP gen_event
346+ snippet gen_event
347+ -module(${0: `vim_snippets#Filename('', 'my')` } ).
348+
349+ -behaviour(gen_event).
350+
351+ %% API
352+ -export([start_link/0,
353+ add_handler/2]).
354+
355+ %% gen_event callbacks
356+ -export([init/1,
357+ handle_event/2,
358+ handle_call/2,
359+ handle_info/2,
360+ terminate/2,
361+ code_change/3]).
362+
363+ -record(state, {}).
364+
365+ %%%===================================================================
366+ %%% gen_event callbacks
367+ %%%===================================================================
368+
369+ %%--------------------------------------------------------------------
370+ %% @doc
371+ %% Creates an event manager
372+ %%
373+ %% @spec start_link() -> {ok, Pid} | {error, Error}
374+ %% @end
375+ %%--------------------------------------------------------------------
376+ start_link() ->
377+ gen_event:start_link({local, ?MODULE}).
378+
379+ %%--------------------------------------------------------------------
380+ %% @doc
381+ %% Adds an event handler
382+ %%
383+ %% @spec add_handler(Handler, Args) -> ok | {'EXIT', Reason} | term()
384+ %% @end
385+ %%--------------------------------------------------------------------
386+ add_handler(Handler, Args) ->
387+ gen_event:add_handler(?MODULE, Handler, Args).
388+
389+ %%%===================================================================
390+ %%% gen_event callbacks
391+ %%%===================================================================
392+
393+ %%--------------------------------------------------------------------
394+ %% @private
395+ %% @doc
396+ %% Whenever a new event handler is added to an event manager,
397+ %% this function is called to initialize the event handler.
398+ %%
399+ %% @spec init(Args) -> {ok, State}
400+ %% @end
401+ %%--------------------------------------------------------------------
402+ init([]) ->
403+ {ok, #state{}}.
404+
405+ %%--------------------------------------------------------------------
406+ %% @private
407+ %% @doc
408+ %% Whenever an event manager receives an event sent using
409+ %% gen_event:notify/2 or gen_event:sync_notify/2, this function is
410+ %% called for each installed event handler to handle the event.
411+ %%
412+ %% @spec handle_event(Event, State) ->
413+ %% {ok, State} |
414+ %% {swap_handler, Args1, State1, Mod2, Args2} |
415+ %% remove_handler
416+ %% @end
417+ %%--------------------------------------------------------------------
418+ handle_event(_Event, State) ->
419+ {ok, State}.
420+
421+ %%--------------------------------------------------------------------
422+ %% @private
423+ %% @doc
424+ %% Whenever an event manager receives a request sent using
425+ %% gen_event:call/3,4, this function is called for the specified
426+ %% event handler to handle the request.
427+ %%
428+ %% @spec handle_call(Request, State) ->
429+ %% {ok, Reply, State} |
430+ %% {swap_handler, Reply, Args1, State1, Mod2, Args2} |
431+ %% {remove_handler, Reply}
432+ %% @end
433+ %%--------------------------------------------------------------------
434+ handle_call(_Request, State) ->
435+ Reply = ok,
436+ {ok, Reply, State}.
437+
438+ %%--------------------------------------------------------------------
439+ %% @private
440+ %% @doc
441+ %% This function is called for each installed event handler when
442+ %% an event manager receives any other message than an event or a
443+ %% synchronous request (or a system message).
444+ %%
445+ %% @spec handle_info(Info, State) ->
446+ %% {ok, State} |
447+ %% {swap_handler, Args1, State1, Mod2, Args2} |
448+ %% remove_handler
449+ %% @end
450+ %%--------------------------------------------------------------------
451+ handle_info(_Info, State) ->
452+ {ok, State}.
453+
454+ %%--------------------------------------------------------------------
455+ %% @private
456+ %% @doc
457+ %% Whenever an event handler is deleted from an event manager, this
458+ %% function is called. It should be the opposite of Module:init/1 and
459+ %% do any necessary cleaning up.
460+ %%
461+ %% @spec terminate(Reason, State) -> void()
462+ %% @end
463+ %%--------------------------------------------------------------------
464+ terminate(_Reason, _State) ->
465+ ok.
466+
467+ %%--------------------------------------------------------------------
468+ %% @private
469+ %% @doc
470+ %% Convert process state when code is changed
471+ %%
472+ %% @spec code_change(OldVsn, State, Extra) -> {ok, NewState}
473+ %% @end
474+ %%--------------------------------------------------------------------
475+ code_change(_OldVsn, State, _Extra) ->
476+ {ok, State}.
477+
164478 %%%===================================================================
165479 %%% Internal functions
166480 %%%===================================================================
0 commit comments