help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: How to stop from loading a script?


From: Pascal J. Bourguignon
Subject: Re: How to stop from loading a script?
Date: Wed, 21 Oct 2015 15:11:59 +0200
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

Joost Kremers <joost.m.kremers@gmail.com> writes:

> Shiyao Ma wrote:
>> Hi,
>>
>> Say the caller will *always* load a script.
>>
>> But inside the script, I'd like to *directly* return to the caller if some 
>> conditions are met.
>>
>> A workaround is to wrap all the stuff in side a when/unless clause.
>>
>> e.g.,
>>
>>
>> (unless (COND)
>>
>> do-my-stuff)
>>
>>
>> However, the do-my-stuff is too lengthy and I am in search of the following 
>> form:
>
> Why is it too lengthy?
>
>> (when (COND)
>>
>> EARLY_RETURN_TO_THE_LOADER)
>>
>> (do-my-stuff).
>>
>> So my question is, what should I fill in the EARLY_RETURN_TO_THE_LOADER slot?
>
> There is nothing that really fits the bill. You could exit the function
> with `error`, but that won't pass control back to the calling function
> unless the call was wrapped in a `condition-case` or `ignore-errors`.
> Alternatively, you could use `catch` and `throw`, but that also requires
> that the call to your function is wrapped in a special form. See the
> Elisp manual, section on nonlocal exits for both options.
>
> But in either case, you're making the code more complex for unnecessary
> reasons. Using `unless` the way you describe is not a workaround IMHO
> but the best way to do it. The fact that the body of `unless` is long is
> not an issue, the only "disadvantage" is that there are two additional
> spaces for indentation.

I wonder if in emacs lisp there'd also be consequences for some forms
not to be top-level forms. In the case of Common Lisp this certainly
could be a problem.



For example, in CL:

  (unless (find-package "SOME-PACKAGE")
    (ql:quickload :some-system)
    (some-package:some-function))

won't work, because the form will be read before the package named
SOME-PACKAGE (assumedly loaded from the system named "some-system" by
quickload) is defined.

The semantics of EVAL-WHEN are also changed (and therefore any macro
that expand to an EVAL-WHEN form.



In either case, if indentation or toplevelness is a problem, you can put
the optional part in another file that can be compiled and loaded
separately:

    -----------(file1.el)--------------
    (unless (condition)
      (load "file1optional"))
    -----------------------------------


-- 
__Pascal Bourguignon__                 http://www.informatimago.com/
“The factory of the future will have only two employees, a man and a
dog. The man will be there to feed the dog. The dog will be there to
keep the man from touching the equipment.” -- Carl Bass CEO Autodesk


reply via email to

[Prev in Thread] Current Thread [Next in Thread]