Automatically load my own functions on startup

Messages in english

Modérateur : xcasadmin

niccolo
Messages : 26
Inscription : dim. mars 02, 2014 2:13 pm

Automatically load my own functions on startup

Message par niccolo » dim. mai 26, 2019 12:42 pm

Is it possible to define my own functions in some file, and have it loaded automatically each time 'giac' or 'xcas' start?

parisse
Messages : 5734
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: Automatically load my own functions on startup

Message par parisse » dim. mai 26, 2019 12:53 pm

Yes, add your commands in ~/.xcasrc Linux/Mac or in xcas.rc Windows (note that the file will be overwritten if you change and save your setup).

niccolo
Messages : 26
Inscription : dim. mars 02, 2014 2:13 pm

Re: Automatically load my own functions on startup

Message par niccolo » dim. mai 26, 2019 3:13 pm

Thanks for replying.

I checked your solution. It works for 'giac' only (the console app), not for 'xcas' (the GUI app).

For example, I added ";myfunc(x):=x*666;" to ~/.xcasrc. 'giac' then recognizes "myfunc", but 'xcas' doesn't.

Is there a solution that works for 'xcas' as well?

For reference, here's the content of my ~/.xcasrc (note the last line):

Code : Tout sélectionner

widget_size([0,14,40,10],1,25,1278,977,1,2,0,[56,255,60,255,58,255,216,255,56,255,49],"/usr/bin/firefox",0,0,"","");
cas_setup(0,0,0,1,0,[1e-12,1e-15],12,[2,100,0,25],0,1,0,1),xcas_mode(0);
xyztrange([-10.0,10.0,-10.0,10.0,-10.0,10.0,-10.0,10.0,-10.0,10.0,-1.4,1.1,1,0.0,1.0,3]);
autosimplify(regroup);
myfunc(x):=x*666;
(I'm using xcas 1.2.3, which comes with Ubuntu 18.04.)

parisse
Messages : 5734
Inscription : mar. déc. 20, 2005 4:02 pm
Contact :

Re: Automatically load my own functions on startup

Message par parisse » dim. mai 26, 2019 4:12 pm

Indeed, the xcasrc file is executed in the null context, not in the session context.
The best is perhaps to run read("script_filename") in the first level of your xcas session when you start a new file. When you load an existing worksheet, all variables are restored, you don't need to re-parse them.

XcasEngGuy
Messages : 145
Inscription : mer. janv. 13, 2010 3:20 pm

Re: Automatically load my own functions on startup

Message par XcasEngGuy » lun. juin 03, 2019 10:41 pm

I did this by using the following:

I modified runxcas.en to:

#! /bin/bash
export LANG=en
export XCAS_ROOT='/cygdrive/C/Programs_OS/xcas'
# export XCAS_HOME='/cygdrive/p'
# export XCAS_AUTOSAVEFOLDER='/cygdrive/p'
export XCAS_LOCALE="$XCAS_ROOT/locale/"
export XCAS_HELP="$XCAS_ROOT/aide_cas"
cp /cygdrive/C/Programs_OS/xcas/eq_funcs_template.xws /cygdrive/C/Programs_OS/xcas/eq_funcs.xws
if [ "$1" == "" ]
then
$XCAS_ROOT/xcas.exe /cygdrive/C/Programs_OS/xcas/eq_funcs.xws
else
$XCAS_ROOT/xcas.exe /cygdrive/C/Programs_OS/xcas/eq_funcs.xws "$1"
fi

Note, I created a template called eq_funcs_template.xws with all my functions loaded in it. That file is in C:\Programs_OS\xcas. That template gets copied into eq_funcs.xws and opened in the last if statement. If I double click a xws file it opens it and the "template file". That way I always have a fresh file to work on that has all my functions.

I also modified xcasen.bat to include C:\Programs_OS\xcas in the PATH:

set PATH=C:\Programs_OS\xcas;%PATH%
bash.exe '/cygdrive/C/Programs_OS/xcas/runxcas.en' %1

hope it works for you. Since you are running Linux, maybe xcasen.bat isn't necessary.

niccolo
Messages : 26
Inscription : dim. mars 02, 2014 2:13 pm

Re: Automatically load my own functions on startup

Message par niccolo » mer. juin 12, 2019 6:44 pm

XcasEngGuy a écrit :
lun. juin 03, 2019 10:41 pm
I did this by using the following:

I modified runxcas.en to:
Thanks. I considered this solution, but it's flawed for me: I usually launch xcas from the directory I work in, as I want xcas to save the file to the diretcory I'm in, without me navigating there. But if I load it with a template file, it will default to the template file's direcotry. Besides, I wouldn't be able to save the file just by pressing Control-S but will have to use the mouse (for "save as").

BTW, in your script you can replace the 'if' statement with:

$XCAS_ROOT/xcas.exe /cygdrive/C/Programs_OS/xcas/eq_funcs.xws "$@"

(I.e., use $@ instead of $1 or null).

XcasEngGuy
Messages : 145
Inscription : mer. janv. 13, 2010 3:20 pm

Re: Automatically load my own functions on startup

Message par XcasEngGuy » ven. juin 14, 2019 11:53 pm

add:

cd "$(dirname "$@")"
cp /cygdrive/C/Programs_OS/xcas/eq_funcs_template.xws eq_funcs.xws
$XCAS_ROOT/xcas.exe eq_funcs.xws "$@"

puts the template in the same directory as the file you open.

Répondre