12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115open!Coreopen!Importopen!Async_kernelopenValue_intfincludeValue0moduletypeFuncall=Funcallwithtypevalue:=tmoduletypeMake_subtype_arg=Make_subtype_argwithtypevalue:=tmoduleEmacs_value=structtypenonrect=tendtypevalue=t[@@derivingsexp_of]moduleProcess_input=structtypet=|Continue|Quit[@@derivingvariants]let()=assert(Variants.continue.rank=0);assert(Variants.quit.rank=1);;endexternalprocess_input:unit->Process_input.t="ecaml_process_input"(* [Funcall_exit.t] values are only constructed by [ecaml_non_local_exit_get_and_clear] in
[ecaml_stubs.c]. *)moduleFuncall_exit=structtypet=|Return|SignalofEmacs_value.t*Emacs_value.t|ThrowofEmacs_value.t*Emacs_value.t[@@derivingvariants]let()=assert(Variants.return.rank=0);assert(Variants.signal.rank=1);assert(Variants.throw.rank=2);;endexternalhave_active_env:unit->bool="ecaml_have_active_env"externalnon_local_exit_get_and_clear:unit->Funcall_exit.t="ecaml_non_local_exit_get_and_clear"exceptionElisp_signalof{symbol:t;data:t}exceptionElisp_throwof{tag:t;value:t}letraise_if_emacs_signaled()=matchnon_local_exit_get_and_clear()with|Return->()|Signal(symbol,data)->raise(Elisp_signal{symbol;data})|Throw(tag,value)->raise(Elisp_throw{tag;value});;moduleMust_check_exit=structexternalintern:string->t="ecaml_intern"externalfuncall_array:t->tarray->bool->t="ecaml_funcall_array"externalfuncall0:t->bool->t="ecaml_funcall0"externalfuncall1:t->t->bool->t="ecaml_funcall1"externalfuncall2:t->t->t->bool->t="ecaml_funcall2"externalfuncall3:t->t->t->t->bool->t="ecaml_funcall3"externalfuncall4:t->t->t->t->t->bool->t="ecaml_funcall4_byte""ecaml_funcall4"externalfuncall5:t->t->t->t->t->t->bool->t="ecaml_funcall5_byte""ecaml_funcall5"externalfuncall_int_int_value_unit:t->int->int->t->unit="ecaml_funcall_int_int_value_unit"externalfuncall_int_int_value_value_unit:t->int->int->t->t->unit="ecaml_funcall_int_int_value_value_unit"externaltype_of:t->t="ecaml_type_of"externalis_not_nil:t->bool="ecaml_is_not_nil"externaleq:t->t->bool="ecaml_eq"externalof_float:float->t="ecaml_of_float"externalto_float_exn:t->float="ecaml_to_float"externalmultibyte_of_string:string->t="ecaml_multibyte_of_string"externalunibyte_of_string:string->t="ecaml_unibyte_of_string"externalto_utf8_bytes_exn:t->string="ecaml_to_string"externalof_time_ns:Time_ns.t->t="ecaml_of_time_ns"externalto_time_ns_exn:t->Time_ns.t="ecaml_to_time_ns"externalvec_get:t->int->t="ecaml_vec_get"externalvec_set:t->int->t->unit="ecaml_vec_set"externalvec_size:t->int="ecaml_vec_size"externalof_buffer_contents:Buffer.t->t="ecaml_unibyte_of_buffer"end[@@alertmust_check_exit"The caller must check for a non-local exit immediately after calling any function \
in this module."]moduleHave_checked_exit=structletwrap_raise1fa=letr=fainraise_if_emacs_signaled();r;;letwrap_raise2fa1a2=letr=fa1a2inraise_if_emacs_signaled();r;;letwrap_raise3fa1a2a3=letr=fa1a2a3inraise_if_emacs_signaled();r;;letwrap_raise4fa1a2a3a4=letr=fa1a2a3a4inraise_if_emacs_signaled();r;;letwrap_raise5fa1a2a3a4a5=letr=fa1a2a3a4a5inraise_if_emacs_signaled();r;;letwrap_raise6fa1a2a3a4a5a6=letr=fa1a2a3a4a5a6inraise_if_emacs_signaled();r;;letwrap_raise7fa1a2a3a4a5a6a7=letr=fa1a2a3a4a5a6a7inraise_if_emacs_signaled();r;;openMust_check_exit[@@alert"-must_check_exit"]letintern=wrap_raise1internletfuncall_array=wrap_raise3funcall_arrayletfuncall0=wrap_raise2funcall0letfuncall1=wrap_raise3funcall1letfuncall2=wrap_raise4funcall2letfuncall3=wrap_raise5funcall3letfuncall4=wrap_raise6funcall4letfuncall5=wrap_raise7funcall5letfuncall_int_int_value_unit=wrap_raise4funcall_int_int_value_unitletfuncall_int_int_value_value_unit=wrap_raise5funcall_int_int_value_value_unitlettype_of=wrap_raise1type_ofletis_not_nil=wrap_raise1is_not_nilleteq=wrap_raise2eqletof_float=wrap_raise1of_floatletto_float_exn=wrap_raise1to_float_exnletmultibyte_of_string=wrap_raise1multibyte_of_stringletunibyte_of_string=wrap_raise1unibyte_of_stringletto_utf8_bytes_exn=wrap_raise1to_utf8_bytes_exnletof_time_ns=wrap_raise1of_time_nsletto_time_ns_exn=wrap_raise1to_time_ns_exnletvec_get=wrap_raise2vec_getletvec_set=wrap_raise3vec_setletvec_size=wrap_raise1vec_sizeletof_buffer_contents=wrap_raise1of_buffer_contentsendincludeHave_checked_exitletinterned_symbols=String.Hash_set.create()letall_interned_symbols()=List.sort(Hash_set.to_listinterned_symbols)~compare:[%compare:string];;letinternstring=ifam_running_testthenHash_set.addinterned_symbolsstring;internstring;;moduleQ=structletarrayp="arrayp"|>internletbufferp="bufferp"|>internletcar="car"|>internletcadr="cadr"|>internletcdr="cdr"|>internletcommandp="commandp"|>internletcons="cons"|>internletconsp="consp"|>internletdebug_on_error="debug-on-error"|>internletequal="equal"|>internletequal_including_properties="equal-including-properties"|>internleterror="error"|>internleterror_conditions="error-conditions"|>internleteventp="eventp"|>internletfloatp="floatp"|>internletfontp="fontp"|>internletframep="framep"|>internletfunctionp="functionp"|>internletget="get"|>internlethash_table_p="hash-table-p"|>internletkeymapp="keymapp"|>internletlength="length"|>internletlist="list"|>internletmarkerp="markerp"|>internletmemq="memq"|>internletmessage="message"|>internletnil="nil"|>internletprin1_to_string="prin1-to-string"|>internletprint_length="print-length"|>internletprint_level="print-level"|>internletprocessp="processp"|>internletset="set"|>internletstring_as_multibyte="string-as-multibyte"|>internletstringp="stringp"|>internletsubstring_no_properties="substring-no-properties"|>internletsymbol_value="symbol-value"|>internletsymbolp="symbolp"|>internlett="t"|>internlettimerp="timerp"|>internletuser_error="user-error"|>internletvector="vector"|>internletvectorp="vectorp"|>internletwindow_configuration_p="window-configuration-p"|>internletwindowp="windowp"|>internendmoduleBlock_on_async=structtypet={f:'a.here:[%call_pos]->?context:Sexp.tLazy.t->(unit->'aDeferred.t)->'a}letset_once:tSet_once.t=Set_once.create()endmoduleEnqueue_foreground_block_on_async=structtypet={f:here:[%call_pos]->?context:Sexp.tLazy.t->?raise_exceptions_to_monitor:Monitor.t->(unit->unitDeferred.t)->unit}letset_once:tSet_once.t=Set_once.create()endmoduleRun_outside_async=structtypet={f:'a.here:[%call_pos]->?allowed_in_background:bool->(unit->'a)->'aDeferred.t}letset_once:tSet_once.t=Set_once.create()endletmaybe_profile~should_profilecontextf=ifOption.valueshould_profile~default:truethenprofileSynccontextfelsef();;letfuncall_array?should_profiletts~should_return_result=maybe_profile~should_profile[%lazy_sexp(t::(ts|>Array.to_list):tlist)](fun()->funcall_arrayttsshould_return_result);;letfuncallN_array?should_profiletts=funcall_array?should_profiletts~should_return_result:true;;letfuncallN_array_i?should_profiletts=ignore(funcall_array?should_profiletts~should_return_result:false:t);;letfuncallN?should_profiletts=funcallN_array?should_profilet(ts|>Array.of_list)letfuncallN_i?should_profiletts=funcallN_array_i?should_profilet(ts|>Array.of_list);;letfuncall0f~should_profile~should_return_result=maybe_profile~should_profile[%lazy_sexp([f]:tlist)](fun()->funcall0fshould_return_result);;letfuncall1fa1~should_profile~should_return_result=maybe_profile~should_profile[%lazy_sexp([f;a1]:tlist)](fun()->funcall1fa1should_return_result);;letfuncall2fa1a2~should_profile~should_return_result=maybe_profile~should_profile[%lazy_sexp([f;a1;a2]:tlist)](fun()->funcall2fa1a2should_return_result);;letfuncall3fa1a2a3~should_profile~should_return_result=maybe_profile~should_profile[%lazy_sexp([f;a1;a2;a3]:tlist)](fun()->funcall3fa1a2a3should_return_result);;letfuncall4fa1a2a3a4~should_profile~should_return_result=maybe_profile~should_profile[%lazy_sexp([f;a1;a2;a3;a4]:tlist)](fun()->funcall4fa1a2a3a4should_return_result);;letfuncall5fa1a2a3a4a5~should_profile~should_return_result=maybe_profile~should_profile[%lazy_sexp([f;a1;a2;a3;a4;a5]:tlist)](fun()->funcall5fa1a2a3a4a5should_return_result);;letfuncall0_i?should_profilef=ignore(funcall0f~should_profile~should_return_result:false:t);;letfuncall0?should_profilef=funcall0f~should_profile~should_return_result:trueletfuncall1_i?should_profilefa=ignore(funcall1fa~should_profile~should_return_result:false:t);;letfuncall1?should_profilefa=funcall1fa~should_profile~should_return_result:trueletfuncall2_i?should_profilefa1a2=ignore(funcall2fa1a2~should_profile~should_return_result:false:t);;letfuncall2?should_profilefa1a2=funcall2fa1a2~should_profile~should_return_result:true;;letfuncall3_i?should_profilefa1a2a3=ignore(funcall3fa1a2a3~should_profile~should_return_result:false:t);;letfuncall3?should_profilefa1a2a3=funcall3fa1a2a3~should_profile~should_return_result:true;;letfuncall4_i?should_profilefa1a2a3a4=ignore(funcall4fa1a2a3a4~should_profile~should_return_result:false:t);;letfuncall4?should_profilefa1a2a3a4=funcall4fa1a2a3a4~should_profile~should_return_result:true;;letfuncall5_i?should_profilefa1a2a3a4a5=ignore(funcall5fa1a2a3a4a5~should_profile~should_return_result:false:t);;letfuncall5?should_profilefa1a2a3a4a5=funcall5fa1a2a3a4a5~should_profile~should_return_result:true;;letfuncall_int_int_value_unit?should_profilefa1a2a3=maybe_profile~should_profile[%lazy_sexp(f:t),(a1:int),(a2:int),(a3:t)](fun()->funcall_int_int_value_unitfa1a2a3);;letfuncall_int_int_value_value_unit?should_profilefa1a2a3a4=maybe_profile~should_profile[%lazy_sexp(f:t),(a1:int),(a2:int),(a3:t),(a4:t)](fun()->funcall_int_int_value_value_unitfa1a2a3a4);;let[@inlinealways]is_integer(t:t)=Obj.is_int(Obj.reprt)let[@inlinealways]to_int_exn(t:t)=ifnot(is_integert)thenraise_s[%sexp"wrong-type-argument",("integerp",(t:t))]else(Obj.magict:int);;letget_varsymbol=funcall1Q.symbol_valuesymbolletset_varsymbolvalue=funcall2_iQ.setsymbolvalueletget_int_varstring=get_var(string|>intern)|>to_int_exnletdebug_on_error()=is_not_nil(funcall1Q.symbol_valueQ.debug_on_error)letemacs_min_int=get_int_var"most-negative-fixnum"letemacs_max_int=get_int_var"most-positive-fixnum"letof_int_exn:int->t=letcheck_bounds=Int.validate_bound~min:(Inclemacs_min_int)~max:(Inclemacs_max_int)infunn->Validate.maybe_raise(Validate.name"overflow-error"(check_boundsn));(Obj.magicn:t);;(* multibyte strings contain UTF-8 *)let_multibyte_of_string=multibyte_of_string(* unibyte strings contain arbitrary bytes *)letunibyte_of_string=unibyte_of_string(* Unfortunately, there are lots of places where we can call of_utf8_bytes on things which
are not in fact UTF-8. If we naively use multibyte_of_string here, we'll break Emacs by
creating broken multibyte strings. So we want to try to parse the input string as
UTF-8, but if there's any non-UTF-8 bytes, we want to escape them somehow.
That's what string-as-multibyte does when passed a unibyte string.
*)letof_utf8_bytess=funcall1Q.string_as_multibyte(unibyte_of_strings)letof_buffer_contentsbuf=of_buffer_contentsbufletof_utf8_bytes_cached=Memo.general~hashable:String.hashableof_utf8_bytesletof_utf8_bytes_replacing_invalidstr=letbuffer=Buffer.create(String.lengthstr)inletfirst_malformed=Uutf.String.fold_utf_8(funfirst_malformedpos->function|`Malformedbad_string->Uutf.Buffer.add_utf_8bufferUutf.u_rep;Option.first_somefirst_malformed(Some(pos,bad_string))|`Ucharuchar->Uutf.Buffer.add_utf_8bufferuchar;first_malformed)Nonestrin(funcall1Q.string_as_multibyte(of_buffer_contentsbuffer),`First_malformedfirst_malformed);;letpercent_s=of_utf8_bytes"%s"letmessages=funcall2_iQ.messagepercent_s(of_utf8_bytess)letmessage_zero_alloct=ignore((Must_check_exit.funcall1[@alert"-must_check_exit"])Q.messagetfalse:t);raise_if_emacs_signaled();;letmessage_tt=funcall2_iQ.messagepercent_stletmessageffmt=ksprintfmessagefmtletmessage_s:Sexp.t->unit=function|Atomstring->messagestring|sexp->messagef"%s"(sexp|>Sexp.to_string_hum);;letnil=Q.nillett=Q.tletto_bool=is_not_nilletof_boolb=ifbthentelsenilletlistts=funcallNQ.listtsletis_nilt=eqtnilletis_arrayt=funcall1Q.arraypt|>to_boolletis_buffert=funcall1Q.bufferpt|>to_boolletis_commandt=funcall1Q.commandpt|>to_boolletis_eventt=funcall1Q.eventpt|>to_boolletis_floatt=funcall1Q.floatpt|>to_boolletis_fontt=funcall1Q.fontpt|>to_boolletis_framet=funcall1Q.framept|>to_boolletis_functiont=funcall1Q.functionpt|>to_boolletis_hash_tablet=funcall1Q.hash_table_pt|>to_boolletis_keymapt=funcall1Q.keymappt|>to_boolletis_markert=funcall1Q.markerpt|>to_boolletis_processt=funcall1Q.processpt|>to_boolletis_stringt=funcall1Q.stringpt|>to_boolletis_symbolt=funcall1Q.symbolpt|>to_boolletis_timert=funcall1Q.timerpt|>to_boolletis_vectort=funcall1Q.vectorpt|>to_boolletis_windowt=funcall1Q.windowpt|>to_boolletis_window_configurationt=funcall1Q.window_configuration_pt|>to_boolletequalt1t2=funcall2Q.equalt1t2|>to_boolletconst1t2=funcall2Q.const1t2letcar_exnt=funcall1Q.cartletcdr_exnt=funcall1Q.cdrtletcadr_exnt=funcall1Q.cadrtletis_cons?car?cdrt=funcall1Q.conspt|>to_bool&&(matchcarwith|None->true|Someis_car->is_car(car_exnt))&&matchcdrwith|None->true|Someis_cdr->is_cdr(cdr_exnt);;letto_list_exn(t:t)~f=letreclooptac=ifis_niltthenList.revacelseifis_constthenloop(cdr_exnt)(f(car_exnt)::ac)elseraise_s[%message"[Value.to_list] got strange value"~_:(t:t)]inloopt[];;letlist_to_array_exnt~f=letlength=funcall1Q.lengtht|>to_int_exniniflength=0thenArray.of_list[]else(letelt0=f(car_exnt)inletarray=Array.create~len:lengthelt0inletrecfill_array_loopti=ifis_niltthen()else(array.(i)<-f(car_exnt);fill_array_loop(cdr_exnt)(i+1))infill_array_loop(cdr_exnt)1;array);;letto_array_exn(t:t)~f=letlength=vec_sizetinArray.initlength~f:(funi->f(vec_getti));;letvectorarr=funcallN_arrayQ.vectorarr(* Sadly, error messages' text properties do not affect what is displayed in the echo
area, so there's no point trying to make this a [Text.t]. *)exceptionUser_errorofstringletsymbol_get_propertysymbolproperty=funcall2Q.getsymbolpropertyleterror_conditionssignal=symbol_get_propertysignalQ.error_conditionsletmemqeltlist=funcall2Q.memqeltlistletnon_local_exit_signalexn=letmoduleM=struct(** [non_local_exit_signal] sets a [pending_error] flag in the Emacs environment that
causes it to, after our C code returns to it, signal instead of returning a value. *)externalnon_local_exit_signal:t->t->unit="ecaml_non_local_exit_signal"(** [non_local_exit_throw] works like [non_local_exit_signal], except that it throws
instead of signaling when our code returns to emacs. *)externalnon_local_exit_throw:t->t->unit="ecaml_non_local_exit_throw"endinletdebug_on_error=debug_on_error()inmatchexnwith|Elisp_throw{tag;value}->M.non_local_exit_throwtagvalue|Elisp_signal{symbol;data}->M.non_local_exit_signalsymboldata|_->(* Exceptions generated from OCaml *)leterror_symbol,message=matchexnwith|User_errormessage->(* User errors are treated specially in a few ways (debug-ignored-errors,
error-message-string, command-error-function, etc.) so make some effort to
distinguish them. The message is just a string, to encourage callers to format
them human-readably. *)Q.user_error,message|_->(Q.error,letbacktrace=(* In our tests, [Expect_test_helpers_base] normally elides backtraces for
exceptions that get raised up to [require_does_raise], but that only
happens once the exception actually makes it all the way back to the expect
test machinery; since we're explicitly rendering and adding the backtrace
so that it can be preserved as we go back through Emacs stack frames, we
need to do it ourselves. *)ifdebug_on_error&¬am_running_testthenSome(Backtrace.Exn.most_recent())elseNoneinletmessage=[%message.omit_nil""~_:(exn:exn)(backtrace:Backtrace.toption)]inletmessage=matchmessagewith|Atomstring->string|List_assexp->Sexp.to_string_humsexpinmessage)in(* For the [error] symbol, the error data should be a list whose car is a string. See
[(Info-goto-node "(elisp)Signaling Errors")]. *)M.non_local_exit_signalerror_symbol(list[message|>of_utf8_bytes]);;letprin1_to_stringt=funcall1Q.prin1_to_stringt|>to_utf8_bytes_exnlettext_has_propertiest=is_nil(funcall2Q.equal_including_propertiest(funcall1Q.substring_no_propertiest));;letmight_be_a_sexpstring=letstring=string|>String.stripinletn=String.lengthstringinn>=2&&Char.equalstring.[0]'('&&Char.equalstring.[n-1]')';;letwith_print_config~print_length~print_level~f=letold_print_length=get_varQ.print_lengthinletold_print_level=get_varQ.print_levelinset_varQ.print_length(print_length|>of_int_exn);set_varQ.print_level(print_level|>of_int_exn);protect~f~finally:(fun()->set_varQ.print_lengthold_print_length;set_varQ.print_levelold_print_level);;letrecsexp_of_t_internalt~print_length~print_level:Sexp.t=ifprint_length<=0||print_level<=0then[%message"..."]elseifis_stringt&¬(text_has_propertiest)then(letstring=t|>to_utf8_bytes_exninifnot(might_be_a_sexpstring)thenAtomstringelse(matchstring|>Sexp.of_stringwith|x->x|exception_->Atomstring))elseifis_constthen(letcar=sexp_of_t_internal(car_exnt)~print_length~print_level:(print_level-1)inletcdr=sexp_of_t_internal(cdr_exnt)~print_length:(print_length-1)~print_levelinmatchcdrwith|Atom"nil"->List[car]|Atom_->List[car;Atom".";cdr]|Listsexps->List(car::sexps))else(letsexp_string=with_print_config~print_length~print_level~f:(fun()->prin1_to_stringt)inletsexp_string=(* Emacs prefixes some values (like buffers, markers, etc) with [#], which then
makes the sexp unparseable. So in this case we strip the [#]. *)ifString.is_prefixsexp_string~prefix:"#("thenString.chop_prefix_exnsexp_string~prefix:"#"elsesexp_stringinmatchSexp.of_stringsexp_stringwith|sexp->sexp|exception_->Atomsexp_string);;letecaml_profile_print_length=refNoneletecaml_profile_print_level=refNoneletsexp_of_tt=letprint_length,print_level=ifProfile.am_forcing_message()then!ecaml_profile_print_length,!ecaml_profile_print_levelelse(letint_or_nilsymbol=letvalue=get_varsymbolinifis_nilvaluethenNoneelseSome(value|>to_int_exn)inint_or_nilQ.print_length,int_or_nilQ.print_level)inletprint_length=Option.valueprint_length~default:emacs_max_intinletprint_level=Option.valueprint_level~default:emacs_max_intinsexp_of_t_internalt~print_length~print_level;;let()=Sexplib.Conv.Exn_converter.add[%extension_constructorElisp_signal](Obj.magic_portable(function|Elisp_signal{symbol;data}->(* Omit the symbol for plain [error]s, but keep it for more specific error
conditions like [wrong-type-argument]. *)ifeqsymbolQ.errorthen[%sexp(data:t)]elseifis_nildatathen[%sexp(symbol:t)]else[%message""~_:(symbol:t)~_:(data:t)]|_->(* Reaching this branch indicates a bug in sexplib. *)assertfalse));sexp_of_t_ref:=sexp_of_t;;moduleType=structtypeasync[@@derivingsexp_of]typesync[@@derivingsexp_of]type('a,'m)t_={id:'aType_equal.Id.t;of_value_exn:value->'a;to_value:'a->value}[@@derivingfields~getters,sexp_of]type'at=('a,sync)t_[@@derivingsexp_of]moduletypeEnum=Enum.SmoduletypeS=Typewithtypevalue:=valuewithtype'at:='atletnamet=Sexp.of_string(Type_equal.Id.namet.id)letto_sexpt=Type_equal.Id.to_sexpt.idletsexp_of_t_t=nametletcreatenamesexp_of_tof_value_exnto_value={id=Type_equal.Id.create~name:(Sexp.to_stringname)sexp_of_t;of_value_exn=(funvalue->tryof_value_exnvaluewith|exn->raise_s[%message"unable to convert Elisp value to OCaml value"~type_:(name:Sexp.t)(value:value)(exn:exn)]);to_value};;letwith_of_value_exntof_value_exn={twithof_value_exn}letenum(typea)name(moduleM:Enum.Swithtypet=a)to_value=letvalid_values=List.mapM.all~f:(funm->to_valuem,m)inletof_value_exnvalue=matchList.Assoc.findvalid_valuesvalue~equalwith|None->raise_s[%message(valid_values:(value*M.t)list)]|Somem->mincreatename[%sexp_of:M.t]of_value_exnto_value;;letenum_symbolnamem=enumnamem(funa->Enum.to_string_humma|>intern)letbool=create[%message"bool"][%sexp_of:bool]to_boolof_boolletfloat=create[%message"float"][%sexp_of:float]to_float_exnof_floatletignored=create[%message"ignored"][%sexp_of:unit]ignore(constnil)letint=create[%message"int"][%sexp_of:int]to_int_exnof_int_exnletstring=create[%message"string"][%sexp_of:string]to_utf8_bytes_exnof_utf8_bytes;;letstring_cached=create[%message"string"][%sexp_of:string]to_utf8_bytes_exnof_utf8_bytes_cached;;lettime_ns=create[%message"time"][%sexp_of:Time_ns.t]to_time_ns_exnof_time_nsletunit=create[%message"unit"][%sexp_of:unit](funvalue->assert(is_nilvalue))(constnil);;letvalue=create[%message"value"][%sexp_of:value]Fn.idFn.idletalistt1t2=create[%message"alist"~_:(namet1:Sexp.t)~_:(namet2:Sexp.t)](sexp_of_list(Tuple2.sexp_of_t(to_sexpt1)(to_sexpt2)))(to_list_exn~f:(funcons_cell->of_value_exnt1(car_exncons_cell),of_value_exnt2(cdr_exncons_cell)))(funl->list(List.mapl~f:(fun(a,b)->cons(to_valuet1a)(to_valuet2b))));;letlist_or_singletont=create[%message"list_or_singleton"~_:(namet:Sexp.t)](sexp_of_list(to_sexpt))(funv->tryto_list_exn~f:(of_value_exnt)vwith|_->of_value_exntv|>List.singleton)(function|[elem]->to_valuetelem|l->list(List.mapl~f:(to_valuet)));;letlistt=create[%message"list"~_:(namet:Sexp.t)](sexp_of_list(to_sexpt))(to_list_exn~f:(of_value_exnt))(funl->list(List.mapl~f:(to_valuet)));;letarray_as_listt=create[%message"array-as-list"~_:(namet:Sexp.t)](sexp_of_array(to_sexpt))(list_to_array_exn~f:(of_value_exnt))(Array.fold_right~init:nil~f:(funeltelisp_list->cons(to_valuetelt)elisp_list));;letvectort=create[%message"vector"~_:(namet:Sexp.t)](sexp_of_array(to_sexpt))(to_array_exn~f:(of_value_exnt))(funa->vector(Array.mapa~f:(to_valuet)));;lettuplet1t2=create[%sexp(namet1:Sexp.t),(namet2:Sexp.t)](Tuple2.sexp_of_t(to_sexpt1)(to_sexpt2))(funv->of_value_exnt1(car_exnv),of_value_exnt2(cdr_exnv))(fun(a1,a2)->cons(to_valuet1a1)(to_valuet2a2));;lettuple2_as_listt1t2=create[%sexp(namet1:Sexp.t),(namet2:Sexp.t)](Tuple2.sexp_of_t(to_sexpt1)(to_sexpt2))(funv->of_value_exnt1(car_exnv),of_value_exnt2(cadr_exnv))(fun(a1,a2)->cons(to_valuet1a1)(cons(to_valuet2a2)nil));;lettuple3_as_listt1t2t3=create[%sexp(namet1:Sexp.t),(namet2:Sexp.t),(namet3:Sexp.t)](fun(v1,v2,v3)->Sexp.List[to_sexpt1v1;to_sexpt2v2;to_sexpt3v3])(funcons->letv1,cons=of_value_exnt1(car_exncons),cdr_exnconsinletv2,cons=of_value_exnt2(car_exncons),cdr_exnconsinletv3,_ons=of_value_exnt3(car_exncons),cdr_exnconsinv1,v2,v3)(fun(a1,a2,a3)->nil|>cons(to_valuet3a3)|>cons(to_valuet2a2)|>cons(to_valuet1a1));;lettuple4_as_listt1t2t3t4=create[%sexp(namet1:Sexp.t),(namet2:Sexp.t),(namet3:Sexp.t),(namet4:Sexp.t)](fun(v1,v2,v3,v4)->Sexp.List[to_sexpt1v1;to_sexpt2v2;to_sexpt3v3;to_sexpt4v4])(funcons->letv1,cons=of_value_exnt1(car_exncons),cdr_exnconsinletv2,cons=of_value_exnt2(car_exncons),cdr_exnconsinletv3,cons=of_value_exnt3(car_exncons),cdr_exnconsinletv4,_ons=of_value_exnt4(car_exncons),cdr_exnconsinv1,v2,v3,v4)(fun(a1,a2,a3,a4)->nil|>cons(to_valuet4a4)|>cons(to_valuet3a3)|>cons(to_valuet2a2)|>cons(to_valuet1a1));;lettuple5_as_listt1t2t3t4t5=create[%sexp(namet1:Sexp.t),(namet2:Sexp.t),(namet3:Sexp.t),(namet4:Sexp.t),(namet5:Sexp.t)](fun(v1,v2,v3,v4,v5)->Sexp.List[to_sexpt1v1;to_sexpt2v2;to_sexpt3v3;to_sexpt4v4;to_sexpt5v5])(funcons->letv1,cons=of_value_exnt1(car_exncons),cdr_exnconsinletv2,cons=of_value_exnt2(car_exncons),cdr_exnconsinletv3,cons=of_value_exnt3(car_exncons),cdr_exnconsinletv4,cons=of_value_exnt4(car_exncons),cdr_exnconsinletv5,_ons=of_value_exnt5(car_exncons),cdr_exnconsinv1,v2,v3,v4,v5)(fun(a1,a2,a3,a4,a5)->nil|>cons(to_valuet5a5)|>cons(to_valuet4a4)|>cons(to_valuet3a3)|>cons(to_valuet2a2)|>cons(to_valuet1a1));;letmapt~name~of_~to_=createname(Fn.compose(to_sexpt)to_)(Fn.composeof_(of_value_exnt))(Fn.compose(to_valuet)to_);;letmap_idtname=mapt~name~of_:Fn.id~to_:Fn.idletnil_ort=create[%message"nil_or"~_:(namet:Sexp.t)](function|None->Atom"nil"|Somev->to_sexptv)(funv->ifis_nilvthenNoneelseSome(v|>of_value_exnt))(function|None->nil|Somev->(to_valuet)v);;letoptiont=create[%message"option"~_:(namet:Sexp.t)](sexp_of_option(to_sexpt))(funv->ifis_nilvthenNoneelseSome(car_exnv|>of_value_exnt))(function|None->nil|Somev->cons(to_valuetv)nil);;letstringable(typea)name(moduleM:Stringable.Swithtypet=a)=mapstring~name~of_:M.of_string~to_:M.to_string;;(* embed ocaml values as strings which are sexp representations *)letsexpable(typea)(moduleA:Sexpablewithtypet=a)~name=create[%message"sexpable"~_:(name:Sexp.t)][%sexp_of:A.t](funv->A.t_of_sexp(Sexp.of_string(of_value_exnstringv)))(funa->A.sexp_of_ta|>Sexp.to_string_mach|>to_valuestring);;letpath_list=map(nil_orstring)~name:[%message"path-list-element"]~of_:(Option.value~default:".")~to_:Option.return|>list;;letspan_ns=maptime_ns~name:[%message"time_span"]~of_:Time_ns.to_span_since_epoch~to_:Time_ns.of_span_since_epoch;;endmoduletypeSubtype=Subtypewithtypevalue:=twithtype'atype_:='aType.tmoduleMake_subtype(M:Make_subtype_arg)=structopenMtypenonrect=t[@@derivingsexp_of]letis_in_subtype=is_in_subtypeletto_valuet=tletof_value_exnt=ifnot(is_in_subtypet)thenraise_s[%message(concat["[";name;"]'s [of_value_exn] got value not in subtype"])~_:(t:t)];t;;lettype_=Type.create[%messagename][%sexp_of:t]of_value_exnto_valuelett=type_leteq(t1:t)t2=eqt1t2endmoduleStat=structtypet={free_performed:int;free_scheduled:int;root_allocated:int}[@@derivingsexp_of]externalnum_emacs_free_performed:unit->int="ecaml_num_emacs_free_performed"externalnum_emacs_free_scheduled:unit->int="ecaml_num_emacs_free_scheduled"externalnum_emacs_root_allocated:unit->int="ecaml_num_emacs_root_allocated"letnow()={free_performed=num_emacs_free_performed();free_scheduled=num_emacs_free_scheduled();root_allocated=num_emacs_root_allocated()};;letdifft2t1={free_performed=t2.free_performed-t1.free_performed;free_scheduled=t2.free_scheduled-t1.free_scheduled;root_allocated=t2.root_allocated-t1.root_allocated};;endmoduleExpert=structmoduleProcess_input=Process_inputexceptionUser_error=User_errorletprocess_input=process_inputletraise_if_emacs_signaled=raise_if_emacs_signaledlethave_active_env=have_active_envletnon_local_exit_signal=non_local_exit_signallethas_error_conditionexncondition=matchexnwith|Elisp_signal{symbol;data=_}whenis_not_nil(memqcondition(error_conditionssymbol))->true|_->false;;endmoduleFor_testing=structletall_interned_symbols=all_interned_symbolsexceptionElisp_signal=Elisp_signalexceptionElisp_throw=Elisp_throwletmap_elisp_signalg~f=matchg()with|a->a|exceptionElisp_signal{symbol;data}->Nothing.unreachable_code(f~symbol~data~reraise:(fun~symbol~data->raise(Elisp_signal{symbol;data})));;letmap_elisp_signal_omit_dataf=map_elisp_signalf~f:(fun~symbol~data:_~reraise->reraise~symbol~data:nil);;endmodulePrivate=structmoduleBlock_on_async=Block_on_asyncmoduleEnqueue_foreground_block_on_async=Enqueue_foreground_block_on_asyncmoduleRun_outside_async=Run_outside_asyncletblock_on_async~(here:[%call_pos])?contextf=(Set_once.get_exnBlock_on_async.set_once~here).f~here?contextf;;letenqueue_foreground_block_on_async~(here:[%call_pos])?context?raise_exceptions_to_monitorf=(Set_once.get_exnEnqueue_foreground_block_on_async.set_once~here).f~here?context?raise_exceptions_to_monitorf;;letrun_outside_async~(here:[%call_pos])?allowed_in_backgroundf=(Set_once.get_exnRun_outside_async.set_once~here).f~here?allowed_in_backgroundf;;letecaml_profile_print_length=ecaml_profile_print_lengthletecaml_profile_print_level=ecaml_profile_print_levelletmessage_zero_alloc=message_zero_allocletmessage_t=message_tend