all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* how to load code conditional on uid
@ 2011-11-11 13:33 Harry Putnam
  2011-11-11 14:44 ` Drew Adams
  0 siblings, 1 reply; 9+ messages in thread
From: Harry Putnam @ 2011-11-11 13:33 UTC (permalink / raw
  To: help-gnu-emacs

Can anyone shoe an example making the loading of specific code,
conditional on the users ID?

For an example, maybe something like a list of load path additions,
perhaps the equivalent of what you would do with shell type coding,
only in elisp:

if you are specific uid; then ## load theses paths

  (add-to-list 'load-path  "/usr/local/src/vcs/cvs/bbdb") 
  (add-to-list 'load-path  "/usr/local/src/vcs/cvs/bbdb/lisp") 

fi




^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: how to load code conditional on uid
  2011-11-11 13:33 Harry Putnam
@ 2011-11-11 14:44 ` Drew Adams
  2011-11-11 15:26   ` Harry Putnam
  0 siblings, 1 reply; 9+ messages in thread
From: Drew Adams @ 2011-11-11 14:44 UTC (permalink / raw
  To: 'Harry Putnam', help-gnu-emacs

> Can anyone shoe an example making the loading of specific code,
> conditional on the users ID?
> 
> if you are specific uid; then ## load theses paths
>   (add-to-list 'load-path  "/usr/local/src/vcs/cvs/bbdb") 
>   (add-to-list 'load-path  "/usr/local/src/vcs/cvs/bbdb/lisp") 

(when (= me (user-uid)) ; ME = your UID
  (add-to-list 'load-path ...)
  (add-to-list 'load-path ...))

See (elisp) `User Identification'.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how to load code conditional on uid
  2011-11-11 14:44 ` Drew Adams
@ 2011-11-11 15:26   ` Harry Putnam
  2011-11-11 15:45     ` Drew Adams
  0 siblings, 1 reply; 9+ messages in thread
From: Harry Putnam @ 2011-11-11 15:26 UTC (permalink / raw
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:

>> Can anyone shoe an example making the loading of specific code,
>> conditional on the users ID?
>> 
>> if you are specific uid; then ## load theses paths
>>   (add-to-list 'load-path  "/usr/local/src/vcs/cvs/bbdb") 
>>   (add-to-list 'load-path  "/usr/local/src/vcs/cvs/bbdb/lisp") 
>
> (when (= me (user-uid)) ; ME = your UID
>   (add-to-list 'load-path ...)
>   (add-to-list 'load-path ...))
>
> See (elisp) `User Identification'.

Thank you.  Yes I see the User Identification section and read it from
your pointer... thanks for that too.

To add a further complication... If you wanted to pass a list of uids.

I get thoroughly confused where the parens go.

I tried a few possibilities.

  (when (= 1000 1001 (user-uid)) 
  (when (= (1000 1001) (user-uid)) 
  (when ((= 1000 1001) (user-uid))

As you can see, I'm just kind of shotgunning in the dark.

Looking at the section on lists in emacs-elisp-intro... doesn't really
help.  Seems to get quite complicated right away and if I use the
first basic example:

     '(rose
       violet
       daisy
       buttercup)

Like this:

  (when (= '(1000 1001) (user-uid)) 

or even another example from emacs-lisp-intro

 `'(this list has (a list inside of it))'

  (when (= '(1000 (1001)) (user-uid)) 

Well, those are wrong too.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: how to load code conditional on uid
  2011-11-11 15:26   ` Harry Putnam
@ 2011-11-11 15:45     ` Drew Adams
  2011-11-11 22:46       ` Harry Putnam
  2011-11-12 11:19       ` Harry Putnam
  0 siblings, 2 replies; 9+ messages in thread
From: Drew Adams @ 2011-11-11 15:45 UTC (permalink / raw
  To: 'Harry Putnam', help-gnu-emacs

> To add a further complication... If you wanted to pass a list of uids.

What do you want to test?  Whether the current `user-uid' is a member of that
list?  If so, use function `member' - do not use function `=' which tests
whether two numbers are equal.

> I tried a few possibilities.
>   (when (= 1000 1001 (user-uid)) 
>   (when (= (1000 1001) (user-uid)) 
>   (when ((= 1000 1001) (user-uid))

1. `C-h f RET user-uid' tells you that `user-uid' returns a number.

2. `C-h f =' tells you that `=' is a binary function: two (numeric) args.

3. `C-h f RET when' tells you that the first arg to `when' is a sexp that is
evaluated and its value tested as a boolean, and that there can be additional
args that are evaluated in order (if the first arg is true).

#2 tells you that your first try cannot be correct: it passes 3 args to `='.

#1 tells you that your second try cannot be correct: it passes a list, not a
number, to `='.

#3 passes `when' _only_ a test, no sexps to eval if the test is true.  And the
test is an invalid sexp (in Emacs Lisp): ((= X Y) Z).  That test sexp has no
valid function to pass arg Z to.  A function in Emacs Lisp is a symbol defined
as a function (e.g. (foo Z)) or it is a lambda expression (e.g. (lambda (arg)
(foo arg))).  But the function position of the sexp ((= X Y) Z) has only (= X
Y), which is neither (is not a valid function).

> As you can see, I'm just kind of shotgunning in the dark.

Yes.  It would help you to read the `Emacs Lisp Intro', available via `C-h i'.
It guides you in a reasonable, pedagogic order. 

> Looking at the section on lists in emacs-elisp-intro... doesn't really
> help.  Seems to get quite complicated right away and if I use the
> first basic example: '(rose violet daisy buttercup) Like this:
>   (when (= '(1000 1001) (user-uid)) 
> or even another example from emacs-lisp-intro
>  `'(this list has (a list inside of it))'
>   (when (= '(1000 (1001)) (user-uid)) 
> Well, those are wrong too.

Read the section again (and again).  Read first about evaluation.

(user-uid) is a sexp that is evaluated.  It returns a number, not a list.

There are no lists here, after arguments are evaluated.  The sexp (= ...) must
evaluate to a boolean value, not a non-empty list.  The sexp (user-uid) must
evaluate to a number, not a list.

What you're missing, besides some background on evaluation and lists, is that to
test membership in a list you use function `member', not function `='.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how to load code conditional on uid
  2011-11-11 15:45     ` Drew Adams
@ 2011-11-11 22:46       ` Harry Putnam
  2011-11-12 11:19       ` Harry Putnam
  1 sibling, 0 replies; 9+ messages in thread
From: Harry Putnam @ 2011-11-11 22:46 UTC (permalink / raw
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:

> What do you want to test?  Whether the current `user-uid' is a member of that
> list?  If so, use function `member' - do not use function `=' which tests
> whether two numbers are equal.

First, its a single user machine but I invent users from time to time
to test various things.

Over the years, being a single user all along... I've developed the
bad habit of misusing site-start.el and putting quite a lot of stuff
in there that really should be in users' ~/.emacs or the like.

So to do some testing, kind of suddenly, rather than go thru and really
clean up site-start.el from top to bottom, which would take me a while
I thought I might just put conditionals where I didn't want a newly
invented user to evaluate stuff and thereby still be able to test
whatever right now.  (Putting off the inevitable clean up of
site-start.el till later)

The general aim was to have a conditional ahead of some elisp code so
that only 1 user's (me) emacs instance would evaluate it.  And in fact
that is about all I really needed to do, so, I started out just
wanting to test for a number like your first example does.

After posting about that and seeing your nifty answer, it occurred to
me that there may be occasion where I'd want several, but not all,
newly invented users to be allowed to eval stuff.

My programming background is so thin that I did not recognize what
should have been obvious (given the equals (=) sign) and got the
foolish notion I might be able to pass several uid to that same kind
of conditional.

I'm very delighted with your detailed explanations, you've made a
number of things clear[er] and provided a nice jumpstart for my weak
adventures with elisp.

Consequently, I've already learned how to allow one user to eval
something, and now how to allow a list of users to do the same thing.

Clearly I have a lot to catch up on and will have to take a lot more
baby steps with close attention to the lisp-intro as I go.

Thanks again for your patient hand holding.




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how to load code conditional on uid
       [not found] <mailman.258.1321018489.798.help-gnu-emacs@gnu.org>
@ 2011-11-12  2:40 ` Jason Rumney
  0 siblings, 0 replies; 9+ messages in thread
From: Jason Rumney @ 2011-11-12  2:40 UTC (permalink / raw
  To: gnu.emacs.help; +Cc: help-gnu-emacs

The usual way is via ~/.emacs (or ~/.emacs.d/init.el)



^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how to load code conditional on uid
  2011-11-11 15:45     ` Drew Adams
  2011-11-11 22:46       ` Harry Putnam
@ 2011-11-12 11:19       ` Harry Putnam
  2011-11-12 14:36         ` Drew Adams
  1 sibling, 1 reply; 9+ messages in thread
From: Harry Putnam @ 2011-11-12 11:19 UTC (permalink / raw
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:

> What do you want to test?  Whether the current `user-uid' is a
> member of that list?  If so, use function `member' - do not use
> function `=' which tests whether two numbers are equal.

C-h f member <RET> shows something that for a lightweight like me is
your basic greek.

,----
| member is a built-in function in `C source code'.
| 
| (member ELT LIST)
| 
| Return non-nil if ELT is an element of LIST.  Comparison done with `equal'.
| The value is actually the tail of LIST whose car is ELT.
`----

Doing an index search in `emacs-lisp-intro' on `member' shows no hits so
apparently it is not used or explained in that document.

Not sure how I would find out what ELT means.  A quick index search in
Elisp manaual on ELT shows:

,----
| -- Function: elt sequence index
|      This function returns the element of SEQUENCE indexed by INDEX.
|      Legitimate values of INDEX are integers ranging from 0 up to one
|      less than the length of SEQUENCE.  If SEQUENCE is a list,
|      out-of-range values behave as for `nth'.  *Note Definition of
|      nth::.  Otherwise, out-of-range values trigger an
|      `args-out-of-range' error.
|       [...]
`----

Well, that didn't help (me) much.  Other than to indicate ELT is short
for element.

But at a glance it sounds as if it could be a uid like 1000 or maybe
(user-uid).

So a couple of clumsy experiments show I've got it all wrong.

(when (member 1000 (1000 1001 1002))
 (add-to-list 'load-path ...)
  (add-to-list 'load-path ...))

or maybe

(when (member (user-uid) (1000 1001 1002)) 
  (add-to-list 'load-path ...)
  (add-to-list 'load-path ...))




^ permalink raw reply	[flat|nested] 9+ messages in thread

* RE: how to load code conditional on uid
  2011-11-12 11:19       ` Harry Putnam
@ 2011-11-12 14:36         ` Drew Adams
  2011-11-13 23:14           ` Harry Putnam
  0 siblings, 1 reply; 9+ messages in thread
From: Drew Adams @ 2011-11-12 14:36 UTC (permalink / raw
  To: 'Harry Putnam', help-gnu-emacs

> C-h f member <RET> shows something that for a lightweight like me is
> your basic greek.
> 
> (member ELT LIST)
> Return non-nil if ELT is an element of LIST.  Comparison 
> done with `equal'.

(member ELT LIST) shows the calling sequence of function `member'.
ELT is the first parameter, LIST is the second.

See (Elisp manual) `Conventions' for documentation conventions.
Use `g' to go to a section of the manual such as `Conventions'.

> The value is actually the tail of LIST whose car is ELT.

This completes the description of the function signature: its parameters are ELT
and LIST, and its return value is the (longest) tail of LIST whose first element
is ELT.  That is, it returns nil if element ELT is not a member of LIST, and if
it is a member then it returns a sublist (a tail) whose first element is ELT.

> Doing an index search in `emacs-lisp-intro' on `member' shows 
> no hits so apparently it is not used or explained in that document.

It is an pedagogical intro, similar to a tutorial.  The reference manual for
Emacs Lisp is the Elisp manual.  There, `i member' gives you more info about the
function.

> Not sure how I would find out what ELT means.

Read the doc conventions section mentioned above.

> Elisp manaual on ELT shows: Function: elt sequence index

That's a function named `elt'.  It has nothing to do with the `member'
function's parameter referred to as ELT in its calling-sequence description.

But if you looked up `member' in the Elisp manual you would find this:

Function: member object list
 The function `member' tests to see whether OBJECT is a member of
 LIST, comparing members with OBJECT using `equal'.  If OBJECT is a
 member, `member' returns a list starting with its first occurrence
 in LIST.  Otherwise, it returns `nil'.
...

ELT and OBJECT are just names - ways to refer to the first parameter.

> But at a glance it sounds as if it could be a uid like 1000 or maybe
> (user-uid).
> (when (member 1000 (1000 1001 1002))...

You correctly understood that the first parameter is tested to see if it is an
element of the second parameter (a list).  The only thing you didn't get was
that `member', as an ordinary function, evaluates its arguments, so if you want
to pass it the constant list (1000 1001 1002) then you need to quote that sexp:

 (when (member 1000 '(1000 1001 1002))... 

Otherwise, Lisp tries to evaluate the sexp (1000 1001 1002).  In doing that, it
expects the first element, 1000, to be a _function_ and tries to apply that
function to the other elements - error.

If you add the quote mark then the sexp to be evaluated is (quote (1000 1001
1002)), which evaluates to the list (1000 1001 1002).

> or maybe (when (member (user-uid) (1000 1001 1002)) 

Again, correct, except for the missing quote mark.

You just need to spend a little more time reading the Lisp intro and then some
of the Elisp manual.  IOW, do a little more homework.  This too will help:

http://www.emacswiki.org/emacs/LearnEmacsLisp




^ permalink raw reply	[flat|nested] 9+ messages in thread

* Re: how to load code conditional on uid
  2011-11-12 14:36         ` Drew Adams
@ 2011-11-13 23:14           ` Harry Putnam
  0 siblings, 0 replies; 9+ messages in thread
From: Harry Putnam @ 2011-11-13 23:14 UTC (permalink / raw
  To: help-gnu-emacs

"Drew Adams" <drew.adams@oracle.com> writes:

> You just need to spend a little more time reading the Lisp intro and
> then some of the Elisp manual.  IOW, do a little more homework.
> This too will help:

Well said, and again thanks so much for a nice push start.




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2011-11-13 23:14 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.258.1321018489.798.help-gnu-emacs@gnu.org>
2011-11-12  2:40 ` how to load code conditional on uid Jason Rumney
2011-11-11 13:33 Harry Putnam
2011-11-11 14:44 ` Drew Adams
2011-11-11 15:26   ` Harry Putnam
2011-11-11 15:45     ` Drew Adams
2011-11-11 22:46       ` Harry Putnam
2011-11-12 11:19       ` Harry Putnam
2011-11-12 14:36         ` Drew Adams
2011-11-13 23:14           ` Harry Putnam

Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.