all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* elisp: isn't default-directory always the dir of current file??
@ 2009-03-02  6:54 Xah Lee
  2009-03-02  7:27 ` Xah Lee
  2009-03-02 13:12 ` Alan Mackenzie
  0 siblings, 2 replies; 10+ messages in thread
From: Xah Lee @ 2009-03-02  6:54 UTC (permalink / raw
  To: help-gnu-emacs

elisp question.

it seems to me, that the var default-directory is not updated when
using load-file.

For example, i have in my .emacs this line and nothing else:

(load-file "web/emacs/xah_emacs_init.el")

Then, in the file xah_emacs_init.el
has this line

(load-file "emacs2/init.el")

and nothing else.

However, it is a error because it cannot find init.el.

When adding (message default-directory) to the xah_emacs_init.el, it
still thinks that the dir is "~/".

The elisp doc on default-directory and load-file indicates that this
should work.

 Xah



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

* Re: elisp: isn't default-directory always the dir of current file??
  2009-03-02  6:54 elisp: isn't default-directory always the dir of current file?? Xah Lee
@ 2009-03-02  7:27 ` Xah Lee
  2009-03-02  7:46   ` Xah Lee
  2009-03-02 13:12 ` Alan Mackenzie
  1 sibling, 1 reply; 10+ messages in thread
From: Xah Lee @ 2009-03-02  7:27 UTC (permalink / raw
  To: help-gnu-emacs

On Mar 1, 10:54 pm, Xah Lee <xah...@gmail.com> wrote:
> elisp question.
>
> it seems to me, that the var default-directory is not updated when
> using load-file.
>
> For example, i have in my .emacs this line and nothing else:
>
> (load-file "web/emacs/xah_emacs_init.el")
>
> Then, in the file xah_emacs_init.el
> has this line
>
> (load-file "emacs2/init.el")
>
> and nothing else.
>
> However, it is a error because it cannot find init.el.
>
> When adding (message default-directory) to the xah_emacs_init.el, it
> still thinks that the dir is "~/".
>
> The elisp doc on default-directory and load-file indicates that this
> should work.
>
>  Xah

What i wanted to do was that when a file is loaded, put that file's
dir to the load-path.

I expected this to work:
(add-to-list 'load-path default-directory )

but that didn't. The following works though:
(add-to-list 'load-path (file-name-directory load-file-name))

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: elisp: isn't default-directory always the dir of current file??
  2009-03-02  7:27 ` Xah Lee
@ 2009-03-02  7:46   ` Xah Lee
  2009-03-02 10:32     ` Lennart Borgman
       [not found]     ` <mailman.2198.1235989932.31690.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 10+ messages in thread
From: Xah Lee @ 2009-03-02  7:46 UTC (permalink / raw
  To: help-gnu-emacs

On Mar 1, 11:27 pm, Xah Lee <xah...@gmail.com> wrote:
> On Mar 1, 10:54 pm, Xah Lee <xah...@gmail.com> wrote:
>
>
>
> > elisp question.
>
> > it seems to me, that the var default-directory is not updated when
> > using load-file.
>
> > For example, i have in my .emacs this line and nothing else:
>
> > (load-file "web/emacs/xah_emacs_init.el")
>
> > Then, in the file xah_emacs_init.el
> > has this line
>
> > (load-file "emacs2/init.el")
>
> > and nothing else.
>
> > However, it is a error because it cannot find init.el.
>
> > When adding (message default-directory) to the xah_emacs_init.el, it
> > still thinks that the dir is "~/".
>
> > The elisp doc on default-directory and load-file indicates that this
> > should work.
>
> >  Xah
>
> What i wanted to do was that when a file is loaded, put that file's
> dir to the load-path.
>
> I expected this to work:
> (add-to-list 'load-path default-directory )
>
> but that didn't. The following works though:
> (add-to-list 'load-path (file-name-directory load-file-name))

Ugh, this is quite painful. Spend 2+ hours on this.

If i use this:
(add-to-list 'load-path (file-name-directory load-file-name))

it works if the file is loaded by elisp. But doesn't work if the file
is run with eval-buffer.

The following works with eval-buffer but not when the file is loaded.
(add-to-list 'load-path default-directory )

What can one do?

I want a method to add the current file's dir to load-path, and it
should work either the file is loaded by elisp or run by hand with
eval-buffer.

Thanks.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: elisp: isn't default-directory always the dir of current file??
  2009-03-02  7:46   ` Xah Lee
@ 2009-03-02 10:32     ` Lennart Borgman
       [not found]     ` <mailman.2198.1235989932.31690.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 10+ messages in thread
From: Lennart Borgman @ 2009-03-02 10:32 UTC (permalink / raw
  To: Xah Lee; +Cc: help-gnu-emacs

On Mon, Mar 2, 2009 at 8:46 AM, Xah Lee <xahlee@gmail.com> wrote:
>> but that didn't. The following works though:
>> (add-to-list 'load-path (file-name-directory load-file-name))
>
> Ugh, this is quite painful. Spend 2+ hours on this.
>
> If i use this:
> (add-to-list 'load-path (file-name-directory load-file-name))
>
> it works if the file is loaded by elisp. But doesn't work if the file
> is run with eval-buffer.

Use buffer-file-name if load-file-name is nil.




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

* Re: elisp: isn't default-directory always the dir of current file??
  2009-03-02  6:54 elisp: isn't default-directory always the dir of current file?? Xah Lee
  2009-03-02  7:27 ` Xah Lee
@ 2009-03-02 13:12 ` Alan Mackenzie
  2009-03-02 20:38   ` Xah Lee
  1 sibling, 1 reply; 10+ messages in thread
From: Alan Mackenzie @ 2009-03-02 13:12 UTC (permalink / raw
  To: help-gnu-emacs

Hi, Xah!

In comp.emacs Xah Lee <xahlee@gmail.com> wrote:
> elisp question.

> it seems to me, that the var default-directory is not updated when
> using load-file.

It isn't.  Why should it be?  default-directory is associated with the
current _buffer_.  If you use this directory as the base for a relative
file name to do something wierd (like loading lisp code), why should that
change default-directory?

> For example, i have in my .emacs this line and nothing else:

> (load-file "web/emacs/xah_emacs_init.el")

> Then, in the file xah_emacs_init.el
> has this line

> (load-file "emacs2/init.el")

> and nothing else.

> However, it is a error because it cannot find init.el.

> When adding (message default-directory) to the xah_emacs_init.el, it
> still thinks that the dir is "~/".

> The elisp doc on default-directory and load-file indicates that this
> should work.

Really?  I've had a look for this and haven't found it.  Care to quote
from the page?

> Xah

-- 
Alan Mackenzie (Nuremberg, Germany).



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

* Re: elisp: isn't default-directory always the dir of current file??
       [not found]     ` <mailman.2198.1235989932.31690.help-gnu-emacs@gnu.org>
@ 2009-03-02 20:32       ` Xah Lee
  2009-03-03 16:45         ` Mike Mattie
       [not found]         ` <mailman.2298.1236115585.31690.help-gnu-emacs@gnu.org>
  0 siblings, 2 replies; 10+ messages in thread
From: Xah Lee @ 2009-03-02 20:32 UTC (permalink / raw
  To: help-gnu-emacs

On Mar 2, 2:32 am, Lennart Borgman <lennart.borg...@gmail.com> wrote:
> On Mon, Mar 2, 2009 at 8:46 AM,XahLee<xah...@gmail.com> wrote:
> >> but that didn't. The following works though:
> >> (add-to-list 'load-path (file-name-directory load-file-name))
>
> > Ugh, this is quite painful. Spend 2+ hours on this.
>
> > If i use this:
> > (add-to-list 'load-path (file-name-directory load-file-name))
>
> > it works if the file is loaded by elisp. But doesn't work if the file
> > is run with eval-buffer.
>
> Use buffer-file-name if load-file-name is nil.

Thanks.

---------------

For completeness or whoever may be reading this later, here's the code
to put the dir into load path:

(add-to-list 'load-path
 (file-name-directory (if load-file-name load-file-name buffer-file-
name)))


This is useful, because suppose you have a elisp library of many files
in a dir named xyz. And in that dir there's one file that init them
all, named “init.el”.

In your install instruction, you can tell user to open init.el and
eval-buffer and everything is installed. And if user wants it
automatically loaded when emacs starts, just put a single line such
as:

(load-file "/Users/xah/web/emacs/emacs2/init.el")

in “.emacs”.

The advantage of this is that:

• user can just put a single load-file line to init your library. They
don't need to put extra line to add path to load-path.

• user can install your library in a very simple way of opening the
file and M-x eval-buffer. (which is simple and especially when user
want to see what your lib do first).

... there are probably other easy alternatives for installation
approach...

For example, 2 installation instruction from existing package i find
very good are:

; from nxml
(load-file "~/Documents/emacs/nxml-mode-20041004/rng-auto.el")

; from js2
(autoload 'js2-mode "js2" nil t)

Very excellent!

Often, emacs packages does not have good install instruction. Some,
simply say “put dir in your load path”, assuming that the user knows
about elisp. Many users have no idea about load path or the concept
particular to elisp.

some rambling thoughts about emacs library system below.
----------------------------------

Also, emacs itself doesn't have a simple, mechanical, uniform
installation system. It would be nice if it does. Something enforced
by elisp... so that all packages are forced to have one single
installation method, such as say “(install filename)”.

... elisp's concept of module, lib, package etc are also sucky. Here's
something i wrote in my tutorial:

What's Library, Package, Feature?

Here's a bit technical detail about some elisp issues related to
library. Knowing it helps you a bit.

Emacs lisp the language does not have name spaces. Everything is
global. Also, emacs has a concept of “package”, “library”, but these
terms are used losely in the documentation. They do not have technical
meaning in emacs lisp. Emacs also has a concepts of “feature”. A
“feature” does have technical meaning in elisp. It is basically a
elisp symbol, that can be added to the list variable “features”. The
sole purpose is for emacs to know which “feature” has been loaded
already. A emacs “feature” has no relation to the file name the lisp
symbol is in.

In general, the terms used by emacs: “package”, “library”, “feature”,
are all fuzzily defined and confusing, and none enforces a relation to
a file's name. This means, you could have a file named “xyz.el”, which
provides a feature named “abc”, while it really just provide a mode to
user with the command name “opq-mode”, and “opq-mode” might have
display name “opq” or “OPQ”, “OPQ mode”, or “yoyo”. And, this file can
be considered as a package as well as library

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: elisp: isn't default-directory always the dir of current file??
  2009-03-02 13:12 ` Alan Mackenzie
@ 2009-03-02 20:38   ` Xah Lee
  2009-03-03  4:06     ` Kevin Rodgers
  0 siblings, 1 reply; 10+ messages in thread
From: Xah Lee @ 2009-03-02 20:38 UTC (permalink / raw
  To: help-gnu-emacs

On Mar 2, 5:12 am, Alan Mackenzie <a...@muc.de> wrote:
> Hi, Xah!
>
> In comp.emacs Xah Lee <xah...@gmail.com> wrote:
>
> > elisp question.
> > it seems to me, that the var default-directory is not updated when
> > using load-file.
>
> It isn't.  Why should it be?  default-directory is associated with the
> current _buffer_.  If you use this directory as the base for a relative
> file name to do something wierd (like loading lisp code), why should that
> change default-directory?

good point.

i misunderstood default-directory's purpose.

What i sought is load-file-name or buffer-file-name.
(file-name-directory (if load-file-name load-file-name buffer-file-
name))

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 10+ messages in thread

* Re: elisp: isn't default-directory always the dir of current file??
  2009-03-02 20:38   ` Xah Lee
@ 2009-03-03  4:06     ` Kevin Rodgers
  0 siblings, 0 replies; 10+ messages in thread
From: Kevin Rodgers @ 2009-03-03  4:06 UTC (permalink / raw
  To: help-gnu-emacs

Xah Lee wrote:
> What i sought is load-file-name or buffer-file-name.
> (file-name-directory (if load-file-name load-file-name buffer-file-
> name))

(file-name-directory (or load-file-name buffer-file-name))

is the idiomatic way to express that.

-- 
Kevin Rodgers
Denver, Colorado, USA





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

* Re: elisp: isn't default-directory always the dir of current file??
  2009-03-02 20:32       ` Xah Lee
@ 2009-03-03 16:45         ` Mike Mattie
       [not found]         ` <mailman.2298.1236115585.31690.help-gnu-emacs@gnu.org>
  1 sibling, 0 replies; 10+ messages in thread
From: Mike Mattie @ 2009-03-03 16:45 UTC (permalink / raw
  To: help-gnu-emacs

On Mon, 2 Mar 2009 12:32:58 -0800 (PST)
Xah Lee <xahlee@gmail.com> wrote:

> On Mar 2, 2:32 am, Lennart Borgman <lennart.borg...@gmail.com> wrote:
> > On Mon, Mar 2, 2009 at 8:46 AM,XahLee<xah...@gmail.com> wrote:
> > >> but that didn't. The following works though:
> > >> (add-to-list 'load-path (file-name-directory load-file-name))
> >
> > > Ugh, this is quite painful. Spend 2+ hours on this.
> >
> > > If i use this:
> > > (add-to-list 'load-path (file-name-directory load-file-name))
> >
> > > it works if the file is loaded by elisp. But doesn't work if the
> > > file is run with eval-buffer.
> >
> > Use buffer-file-name if load-file-name is nil.
> 
> Thanks.
> 
> ---------------
> 
> For completeness or whoever may be reading this later, here's the code
> to put the dir into load path:
> 
> (add-to-list 'load-path
>  (file-name-directory (if load-file-name load-file-name buffer-file-
> name)))
> 
> 
> This is useful, because suppose you have a elisp library of many files
> in a dir named xyz. And in that dir there's one file that init them
> all, named “init.el”.
> 
> In your install instruction, you can tell user to open init.el and
> eval-buffer and everything is installed. And if user wants it
> automatically loaded when emacs starts, just put a single line such
> as:
> 
> (load-file "/Users/xah/web/emacs/emacs2/init.el")
> 
> in “.emacs”.
> 
> The advantage of this is that:
> 
> • user can just put a single load-file line to init your library. They
> don't need to put extra line to add path to load-path.
> 
> • user can install your library in a very simple way of opening the
> file and M-x eval-buffer. (which is simple and especially when user
> want to see what your lib do first).
> 
> ... there are probably other easy alternatives for installation
> approach...
> 
> For example, 2 installation instruction from existing package i find
> very good are:
> 
> ; from nxml
> (load-file "~/Documents/emacs/nxml-mode-20041004/rng-auto.el")
> 
> ; from js2
> (autoload 'js2-mode "js2" nil t)
> 
> Very excellent!
> 
> Often, emacs packages does not have good install instruction. Some,
> simply say “put dir in your load path”, assuming that the user knows
> about elisp. Many users have no idea about load path or the concept
> particular to elisp.
> 
> some rambling thoughts about emacs library system below.
> ----------------------------------
> 
> Also, emacs itself doesn't have a simple, mechanical, uniform
> installation system. It would be nice if it does. Something enforced
> by elisp... so that all packages are forced to have one single
> installation method, such as say “(install filename)”.
> 
> ... elisp's concept of module, lib, package etc are also sucky. Here's
> something i wrote in my tutorial:
> 
> What's Library, Package, Feature?
> 
> Here's a bit technical detail about some elisp issues related to
> library. Knowing it helps you a bit.
> 
> Emacs lisp the language does not have name spaces. Everything is
> global. Also, emacs has a concept of “package”, “library”, but these
> terms are used losely in the documentation. They do not have technical
> meaning in emacs lisp. Emacs also has a concepts of “feature”. A
> “feature” does have technical meaning in elisp. It is basically a
> elisp symbol, that can be added to the list variable “features”. The
> sole purpose is for emacs to know which “feature” has been loaded
> already. A emacs “feature” has no relation to the file name the lisp
> symbol is in.
> 
> In general, the terms used by emacs: “package”, “library”, “feature”,
> are all fuzzily defined and confusing, and none enforces a relation to
> a file's name. This means, you could have a file named “xyz.el”, which
> provides a feature named “abc”, while it really just provide a mode to
> user with the command name “opq-mode”, and “opq-mode” might have
> display name “opq” or “OPQ”, “OPQ mode”, or “yoyo”. And, this file can
> be considered as a package as well as library
> 
>   Xah
> ∑ http://xahlee.org/
> 
> ☄

I am not sure if it is 100% on target to your goals but there is a uniform system. It is Tromney's ELPA
package management system. http://tromey.com/elpa/

It is quite sophisticated addressing the issues of package management comprehensively. As for load-path
there are endless variations on tweaking load-path. I decided to ditch the ad-hoc approach and go with
something systematic. To that end I created GRAIL, a configuration loader that aims to put an end
to the endless ad-hoc load-path tweaking. It works well for my purposes, but I have published it so that
other people may use it, for reading, or direct use.

GRAIL: http://www.emacswiki.org/emacs/Grail

Cheers,
Mike Mattie




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

* Re: elisp: isn't default-directory always the dir of current file??
       [not found]         ` <mailman.2298.1236115585.31690.help-gnu-emacs@gnu.org>
@ 2009-03-03 22:49           ` Xah Lee
  0 siblings, 0 replies; 10+ messages in thread
From: Xah Lee @ 2009-03-03 22:49 UTC (permalink / raw
  To: help-gnu-emacs

Mike Mattie wrote:
> I am not sure if it is 100% on target to your goals but there is a uniform system. It is Tromney's ELPA
> ...
> http://tromey.com/elpa/
> GRAIL: http://www.emacswiki.org/emacs/Grail

Thanks. Gosh, more hours to be spend. I'll try to digest them and
maybe use them and submit my elisp packages in elpa form.

Wouldn't it be nice if it's all build-in in emacs? But then there's
the paper signing, endless philosophies, “emacs way” debate, FSF
politics ... egads.

----------------
PS improved on my tutorial on emacs add-on installation a bit... hope
its useful to someone:

• How To Install Emacs Packages
  http://xahlee.org/emacs/emacs_installing_packages.html

Excerpt:
--------------------

What's Library, Package, Feature?

Here's a bit technical detail about some elisp issues related to
library. Knowing it helps you a bit when installing packages.

No Namespace

Emacs lisp the language does not have name spaces. Everything is
global, with dynamic scope, with some shadowing mechanism. So, don't
expect library or module to be language defined name space constructs
that somewhat enforce name space and file name relation, as in Perl,
Python, Java.

Package And Library Are Not Managed

The terms “package” and “library”, are used losely in emacs/elisp
manual to refer to any useful elisp file. They are not technical
definitions in elisp. A “library” is just some elisp files that do
something useful. A “package” usually refers to a particular one. The
term “module” is not used by emacs.

Emacs's Concept of “Feature”

The term “feature” has some meaning in elisp, but is not mechanical. A
“feature” is a elisp symbol, that is intended to represent the
functionality provided by a emacs package. This elisp symbol, can be
placed on the predefined global variable named “features”, which is a
list of symbols. For example, here's part of the value of “features”
when i do “Ctrl+h v Enter features”:

ibuffer etags ring cc-mode cc-fonts cc-menus cc-cmds cc-styles
cc-align cc-engine cc-vars cc-defs xlsl-mode encoded-kb speck
sgml-mode dired info newcomment desktop recentf tree-widget wid-edit
advice help-fns ...

A elisp file can call “(provide ‹some symbol›)” near the end, which
adds that symbol to the “features” list. The purpose of features and
the “features” variable is to provide a way for emacs to know if
something is already loaded.

There is no absolute relation between any concept of package/library/
module/feature/autoload facilities and the file name. By convention,
if a elisp file name is “xyz-mode.el”, it usually provides a lisp
symbol “xyz-mode” as emacs feature, and the function is often “xyz-
mode”. Sometimes the “mode” part is dropped in the file name, feature
symbol, or function name. This is only a lose convention. There are a
lot exceptions in many bundled emacs packages. For example, the file
“lisp-mode.el” provides the symbol “lisp-mode” as feature, and is
invoked by emacs-lisp-mode. The “cua-base.el” file provides symbols
“cua-base” and “cua” as features, and is invoked by cua-mode. The
“text-mode.el” file does not provide any symbol for feature, but is a
quite useful mode invoked by text-mode. The file “desktop.el” provides
“desktop” as feature, and is invoked by desktop-save-mode.

Summary

Emacs's library/module/package is a primitive system, centered on
loading file, with some slightly high level things such as its
“features” to determine if something is already loaded, and “autoload”
to search thru file name variations in different paths.

All the above means, you could have a file named “xyz.el”, which
provides a feature named “abc”, while it really just provide a mode to
user with the command name “opq-mode” or sometimes just “opq”, and it
might be displayed in mode line as “OPQ”, “OPQ helper”, or anything
else. And, this file can be considered as a package as well as
library.

  Xah
∑ http://xahlee.org/^ permalink raw reply	[flat|nested] 10+ messages in thread

end of thread, other threads:[~2009-03-03 22:49 UTC | newest]

Thread overview: 10+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-03-02  6:54 elisp: isn't default-directory always the dir of current file?? Xah Lee
2009-03-02  7:27 ` Xah Lee
2009-03-02  7:46   ` Xah Lee
2009-03-02 10:32     ` Lennart Borgman
     [not found]     ` <mailman.2198.1235989932.31690.help-gnu-emacs@gnu.org>
2009-03-02 20:32       ` Xah Lee
2009-03-03 16:45         ` Mike Mattie
     [not found]         ` <mailman.2298.1236115585.31690.help-gnu-emacs@gnu.org>
2009-03-03 22:49           ` Xah Lee
2009-03-02 13:12 ` Alan Mackenzie
2009-03-02 20:38   ` Xah Lee
2009-03-03  4:06     ` Kevin Rodgers

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.