unofficial mirror of help-gnu-emacs@gnu.org
 help / color / mirror / Atom feed
* Set the default-directory dynamically according to the buffer-file-name.
@ 2021-09-29 14:31 Hongyi Zhao
  2021-09-29 14:50 ` Hongyi Zhao
                   ` (2 more replies)
  0 siblings, 3 replies; 12+ messages in thread
From: Hongyi Zhao @ 2021-09-29 14:31 UTC (permalink / raw)
  To: help-gnu-emacs

I wrote the following function to set the default-directory
dynamically according to the buffer-file-name:

(defun my/default-directory ()
  (let ((file (buffer-file-name)))
    (if (not file)
        (message "no file name here.")
        (setq default-directory (file-name-directory (buffer-file-name))))))

Then I apply this function to specific hooks, say, python-mode-hook.
This will make it easier for me to invoke files using the relative
path of each project. I want to know if this is a good practice.

Regards
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province



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

* Re: Set the default-directory dynamically according to the buffer-file-name.
  2021-09-29 14:31 Set the default-directory dynamically according to the buffer-file-name Hongyi Zhao
@ 2021-09-29 14:50 ` Hongyi Zhao
  2021-09-29 15:31 ` Tassilo Horn
  2021-09-29 15:47 ` [External] : " Drew Adams
  2 siblings, 0 replies; 12+ messages in thread
From: Hongyi Zhao @ 2021-09-29 14:50 UTC (permalink / raw)
  To: help-gnu-emacs

On Wed, Sep 29, 2021 at 10:31 PM Hongyi Zhao <hongyi.zhao@gmail.com> wrote:
>
> I wrote the following function to set the default-directory
> dynamically according to the buffer-file-name:
>
> (defun my/default-directory ()
>   (let ((file (buffer-file-name)))
>     (if (not file)
>         (message "no file name here.")
>         (setq default-directory (file-name-directory (buffer-file-name))))))
>
> Then I apply this function to specific hooks, say, python-mode-hook.
> This will make it easier for me to invoke files using the relative
> path of each project. I want to know if this is a good practice.

Maybe I have some wrong understanding. Please let me explain the
current situation carefully from the questions I describe below.

I've python script which located at
`/home/werner/Desktop/work/python', named as `pandas-excel.py', which
will read a xls file located in the same folder as it. See following
for the detailed content of this script:

#######
import pandas as pd

excel_file = '2021-2022-1-trimmed-top-3-lines.xls'

newpd = pd.read_excel(excel_file, sheet_name='Sheet1')

for i in newpd.index:
    if i > 1:
        for j in newpd.columns:
            if int(j.split()[1]) > 2:
                if not pd.isnull(newpd.loc[i][j]):
                    print(newpd.loc[i][j])
#######

Now I've added the above customized function to python-mode-hook. When
I open the above script within Emacs and  eval it with ipython, the
following error will be triggered:


/home/werner/2021-2022-1-trimmed-top-3-lines.xls
Traceback (most recent call last):
  File "/home/werner/Desktop/work/python/pandas-excel.py", line 14, in <module>
    newpd = pd.read_excel(excel_file, sheet_name='Sheet1')
  File "/home/werner/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-packages/pandas/util/_decorators.py",
line 311, in wrapper
    return func(*args, **kwargs)
  File "/home/werner/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-packages/pandas/io/excel/_base.py",
line 364, in read_excel
    io = ExcelFile(io, storage_options=storage_options, engine=engine)
  File "/home/werner/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-packages/pandas/io/excel/_base.py",
line 1191, in __init__
    ext = inspect_excel_format(
  File "/home/werner/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-packages/pandas/io/excel/_base.py",
line 1070, in inspect_excel_format
    with get_handle(
  File "/home/werner/.pyenv/versions/3.9.1/envs/datasci/lib/python3.9/site-packages/pandas/io/common.py",
line 710, in get_handle
    handle = open(handle, ioargs.mode)
FileNotFoundError: [Errno 2] No such file or directory:
'2021-2022-1-trimmed-top-3-lines.xls'


But `C-h o default-directory RET' will give me the following info:

default-directory is a variable defined in ‘C source code’.

Its value is "/home/werner/Desktop/work/python/"
Local in buffer pandas-excel.py; global value is nil


Therefore, I want to know why it does not locate the xls file from the
directory of the Python script.

Regards
-- 
Assoc. Prof. Hongyi Zhao <hongyi.zhao@gmail.com>
Theory and Simulation of Materials
Hebei Vocational University of Technology and Engineering
No. 473, Quannan West Street, Xindu District, Xingtai, Hebei province



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

* Re: Set the default-directory dynamically according to the buffer-file-name.
  2021-09-29 14:31 Set the default-directory dynamically according to the buffer-file-name Hongyi Zhao
  2021-09-29 14:50 ` Hongyi Zhao
@ 2021-09-29 15:31 ` Tassilo Horn
  2021-09-30  1:08   ` Hongyi Zhao
  2021-09-29 15:47 ` [External] : " Drew Adams
  2 siblings, 1 reply; 12+ messages in thread
From: Tassilo Horn @ 2021-09-29 15:31 UTC (permalink / raw)
  To: Hongyi Zhao; +Cc: help-gnu-emacs

Hongyi Zhao <hongyi.zhao@gmail.com> writes:

> I wrote the following function to set the default-directory
> dynamically according to the buffer-file-name:
>
> (defun my/default-directory ()
>   (let ((file (buffer-file-name)))
>     (if (not file)
>         (message "no file name here.")
>         (setq default-directory (file-name-directory (buffer-file-name))))))
>
> Then I apply this function to specific hooks, say, python-mode-hook.
> This will make it easier for me to invoke files using the relative
> path of each project. I want to know if this is a good practice.

Isn't that already the case that file-visiting buffers have

  (file-name-directory (buffer-file-name))

as `default-directory'?

Bye,
Tassilo



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

* RE: [External] : Set the default-directory dynamically according to the buffer-file-name.
  2021-09-29 14:31 Set the default-directory dynamically according to the buffer-file-name Hongyi Zhao
  2021-09-29 14:50 ` Hongyi Zhao
  2021-09-29 15:31 ` Tassilo Horn
@ 2021-09-29 15:47 ` Drew Adams
  2021-10-01  4:41   ` Hongyi Zhao
  2 siblings, 1 reply; 12+ messages in thread
From: Drew Adams @ 2021-09-29 15:47 UTC (permalink / raw)
  To: Hongyi Zhao, help-gnu-emacs

>     (if (not file)
>         (message "no file name here.")
>       (setq default-directory (file-name-directory
>                                 (buffer-file-name))))))

(if (not WHATEVER)
    (message "PROBLEM NOTIFICATION")
  (DO-STUFF))

If you want to stop everything and be sure the
message is seen, you might want to raise an error:

(unless WHATEVER (error "PROBLEM")) ; End it all
(DO-STUFF)

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

* Re: Set the default-directory dynamically according to the buffer-file-name.
  2021-09-29 15:31 ` Tassilo Horn
@ 2021-09-30  1:08   ` Hongyi Zhao
  2021-09-30  3:37     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: Hongyi Zhao @ 2021-09-30  1:08 UTC (permalink / raw)
  To: Tassilo Horn; +Cc: help-gnu-emacs

On Wed, Sep 29, 2021 at 11:34 PM Tassilo Horn <tsdh@gnu.org> wrote:
>
> Hongyi Zhao <hongyi.zhao@gmail.com> writes:
>
> > I wrote the following function to set the default-directory
> > dynamically according to the buffer-file-name:
> >
> > (defun my/default-directory ()
> >   (let ((file (buffer-file-name)))
> >     (if (not file)
> >         (message "no file name here.")
> >         (setq default-directory (file-name-directory (buffer-file-name))))))
> >
> > Then I apply this function to specific hooks, say, python-mode-hook.
> > This will make it easier for me to invoke files using the relative
> > path of each project. I want to know if this is a good practice.
>
> Isn't that already the case that file-visiting buffers have
>
>   (file-name-directory (buffer-file-name))
>
> as `default-directory'?

Yes, that's it. So, my code snippet is unnecessary. The problem I
posted here should be relevant to and handled by Projectile, as I've
noticed the following message:

Projectile is initializing cache for /home/werner/ ...

HZ



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

* Re: Set the default-directory dynamically according to the buffer-file-name.
  2021-09-30  1:08   ` Hongyi Zhao
@ 2021-09-30  3:37     ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-09-30  7:00       ` Hongyi Zhao
  0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-09-30  3:37 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: emacs-erc

Hongyi Zhao wrote:

> Yes, that's it. So, my code snippet is unnecessary.

I have 100+ Elisp files [1] and if everything unnecessary was
to be identified and dropped I think that would mean -25 files
or more :)

But I got an idea recently that I told you about already, but
I can tell you again, only I only implemented it to a very
small degree so far (if I'll ever complete the intention),
anyway it is a good idea, and simple as well, namely, keep all
configuration (within a field) in _one_ file, and all
extensions (within the same field) in other, separate files -
that way people can browse the extensions, and use it if they
like it, or tell me if it is, in your words, "unnecessary" -
I don't think they will, but chances are better since no one
wants to browse some other guy's mere options ...

Also with this configuration/extension split on can easily add
the extensions to a place like the EmacsWiki, just ship it
with no prior edit needed!

If only I had come up with this idea 10~15 years ago. Regrets,
I have but a few ... No, just kidding :) A detail of course.

I only did it for ERC so far, I think? Have a look

  https://dataswamp.org/~incal/emacs-init/erc/

[1] https://dataswamp.org/~incal/emacs-init/

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Set the default-directory dynamically according to the buffer-file-name.
  2021-09-30  3:37     ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-09-30  7:00       ` Hongyi Zhao
  2021-09-30  7:39         ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: Hongyi Zhao @ 2021-09-30  7:00 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs; +Cc: emacs-erc

On Thu, Sep 30, 2021 at 11:37 AM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
>
> Hongyi Zhao wrote:
>
> > Yes, that's it. So, my code snippet is unnecessary.
>
> I have 100+ Elisp files [1] and if everything unnecessary was
> to be identified and dropped I think that would mean -25 files
> or more :)
>
> But I got an idea recently that I told you about already, but
> I can tell you again, only I only implemented it to a very
> small degree so far (if I'll ever complete the intention),
> anyway it is a good idea, and simple as well, namely, keep all
> configuration (within a field) in _one_ file, and all
> extensions (within the same field) in other, separate files -

The most difficult thing, IMHO, is the classification criteria for the
different fields.

> that way people can browse the extensions, and use it if they
> like it, or tell me if it is, in your words, "unnecessary" -
> I don't think they will, but chances are better since no one
> wants to browse some other guy's mere options ...
>
> Also with this configuration/extension split on can easily add
> the extensions to a place like the EmacsWiki, just ship it
> with no prior edit needed!
>
> If only I had come up with this idea 10~15 years ago. Regrets,
> I have but a few ... No, just kidding :) A detail of course.
>
> I only did it for ERC so far, I think? Have a look
>
>   https://dataswamp.org/~incal/emacs-init/erc/

Which ones are configuration and Which ones are extensions in your terminology?

> [1] https://dataswamp.org/~incal/emacs-init/

HZ



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

* Re: Set the default-directory dynamically according to the buffer-file-name.
  2021-09-30  7:00       ` Hongyi Zhao
@ 2021-09-30  7:39         ` Emanuel Berg via Users list for the GNU Emacs text editor
  2021-09-30  8:54           ` Hongyi Zhao
  0 siblings, 1 reply; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-09-30  7:39 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: emacs-erc

Hongyi Zhao wrote:

>> But I got an idea recently that I told you about already,
>> but I can tell you again, only I only implemented it to
>> a very small degree so far (if I'll ever complete the
>> intention), anyway it is a good idea, and simple as well,
>> namely, keep all configuration (within a field) in _one_
>> file, and all extensions (within the same field) in other,
>> separate files -
>
> The most difficult thing, IMHO, is the classification
> criteria for the different fields.

Here "field" refers either to an Elisp program, e.g. Gnus,
Emacs-w3m and ERC, or a activity or other e.g. "audio" or
"math". That's just something I wrote, maybe it should be
called something else.

Examples:

  https://dataswamp.org/~incal/emacs-init/erc/
  https://dataswamp.org/~incal/emacs-init/audio.el
  https://dataswamp.org/~incal/emacs-init/math.el

> Which ones are configuration and Which ones are extensions
> in your terminology?

Configuration example:
  https://dataswamp.org/~incal/emacs-init/erc/erc-incal.el

Extensions are all other files in that directory,
https://dataswamp.org/~incal/emacs-init/erc/ , namely

  erc-connect.el
  erc-iterate.el
  erc-kill.el
  erc-misc.el
  erc-scroll.el
  erc-spell.el

I'm sure some of that is, in your words, "unnecessary".

Sometimes it is good to have even tho it is unnecessary
because the version already there isn't exactly to your
liking. Sometimes it isn't but can be configured, that's
preferable if so. Sometimes you are just doing what someone
else has already added to Emacs and you don't know that.
Then you should just drop it when you find out and re-wire to
the official version ...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: Set the default-directory dynamically according to the buffer-file-name.
  2021-09-30  7:39         ` Emanuel Berg via Users list for the GNU Emacs text editor
@ 2021-09-30  8:54           ` Hongyi Zhao
  2021-09-30  9:58             ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: Hongyi Zhao @ 2021-09-30  8:54 UTC (permalink / raw)
  To: Emanuel Berg, help-gnu-emacs; +Cc: emacs-erc

On Thu, Sep 30, 2021 at 3:39 PM Emanuel Berg via Users list for the
GNU Emacs text editor <help-gnu-emacs@gnu.org> wrote:
> Configuration example:
>   https://dataswamp.org/~incal/emacs-init/erc/erc-incal.el

Got it. All files ending with "-incal.el" belong to this category.

HZ



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

* Re: Set the default-directory dynamically according to the buffer-file-name.
  2021-09-30  8:54           ` Hongyi Zhao
@ 2021-09-30  9:58             ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-09-30  9:58 UTC (permalink / raw)
  To: help-gnu-emacs; +Cc: emacs-erc

Hongyi Zhao wrote:

>> Configuration example:
>>   https://dataswamp.org/~incal/emacs-init/erc/erc-incal.el
>
> Got it. All files ending with "-incal.el" belong to
> this category.

Ah, sorry, no. Sometimes that means the first word wasn't
available, e.g.

  https://dataswamp.org/~incal/emacs-init/align-incal.el

because there was/is an `align' already!

Well, good that you said this! If I'm going to continue to
praise this method I should come up with a convention for file
names ...

-- 
underground experts united
https://dataswamp.org/~incal




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

* Re: [External] : Set the default-directory dynamically according to the buffer-file-name.
  2021-09-29 15:47 ` [External] : " Drew Adams
@ 2021-10-01  4:41   ` Hongyi Zhao
  2021-10-01  7:09     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 1 reply; 12+ messages in thread
From: Hongyi Zhao @ 2021-10-01  4:41 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

On Wed, Sep 29, 2021 at 11:47 PM Drew Adams <drew.adams@oracle.com> wrote:
>
> >     (if (not file)
> >         (message "no file name here.")
> >       (setq default-directory (file-name-directory
> >                                 (buffer-file-name))))))
>
> (if (not WHATEVER)
>     (message "PROBLEM NOTIFICATION")
>   (DO-STUFF))
>
> If you want to stop everything and be sure the
> message is seen, you might want to raise an error:
>
> (unless WHATEVER (error "PROBLEM")) ; End it all
> (DO-STUFF)

Do you mean this is a good practice for usage scenarios in production
environments?

HZ



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

* Re: [External] : Set the default-directory dynamically according to the buffer-file-name.
  2021-10-01  4:41   ` Hongyi Zhao
@ 2021-10-01  7:09     ` Emanuel Berg via Users list for the GNU Emacs text editor
  0 siblings, 0 replies; 12+ messages in thread
From: Emanuel Berg via Users list for the GNU Emacs text editor @ 2021-10-01  7:09 UTC (permalink / raw)
  To: help-gnu-emacs

Hongyi Zhao wrote:

>> (if (not WHATEVER)
>>     (message "PROBLEM NOTIFICATION")
>>   (DO-STUFF))
>>
>> If you want to stop everything and be sure the
>> message is seen, you might want to raise an error:
>>
>> (unless WHATEVER (error "PROBLEM")) ; End it all
>> (DO-STUFF)
>
> Do you mean this is a good practice for usage scenarios in
> production environments?

Well, yes, but also in every other environment where your
aspiration is to write good, idiomatic Lisp.

He is showing the syntax and how it is put together for
two scenarios.

But the more tricky question is another, namely when should
one use the first one (i.e., `message' the situation, then
branch out) and when is the situation in fact an `error', much
like division by zero?

(/ 1337 0) ; Arithmetic error

-- 
underground experts united
https://dataswamp.org/~incal




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

end of thread, other threads:[~2021-10-01  7:09 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2021-09-29 14:31 Set the default-directory dynamically according to the buffer-file-name Hongyi Zhao
2021-09-29 14:50 ` Hongyi Zhao
2021-09-29 15:31 ` Tassilo Horn
2021-09-30  1:08   ` Hongyi Zhao
2021-09-30  3:37     ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-30  7:00       ` Hongyi Zhao
2021-09-30  7:39         ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-30  8:54           ` Hongyi Zhao
2021-09-30  9:58             ` Emanuel Berg via Users list for the GNU Emacs text editor
2021-09-29 15:47 ` [External] : " Drew Adams
2021-10-01  4:41   ` Hongyi Zhao
2021-10-01  7:09     ` Emanuel Berg via Users list for the GNU Emacs text editor

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).