unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
From: "Pascal J. Bourguignon" <pjb@informatimago.com>
To: help-gnu-emacs@gnu.org
Subject: Re: How to stop from loading a script?
Date: Wed, 21 Oct 2015 15:11:59 +0200	[thread overview]
Message-ID: <87oafs8ixs.fsf@kuiper.lan.informatimago.com> (raw)
In-Reply-To: d8pdrfFsit9U1@mid.individual.net

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	other threads:[~2015-10-21 13:11 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-10-21 11:35 How to stop from loading a script? Shiyao Ma
2015-10-21 12:13 ` Joost Kremers
2015-10-21 13:11   ` Pascal J. Bourguignon [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87oafs8ixs.fsf@kuiper.lan.informatimago.com \
    --to=pjb@informatimago.com \
    --cc=help-gnu-emacs@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).