diff options
author | Karoly Lorentey <lorentey@elte.hu> | 2006-03-26 16:34:35 +0000 |
---|---|---|
committer | Karoly Lorentey <lorentey@elte.hu> | 2006-03-26 16:34:35 +0000 |
commit | 2828d5f9d4f097228e9b64542fd74de4c47135e5 (patch) | |
tree | db326e57114dedeb6b74f050dd485fd6fe1184d3 /lib-src/emacsclient.c | |
parent | 59b3194ca9708b904c87d52bb9e074e32b7d9089 (diff) | |
download | emacs-2828d5f9d4f097228e9b64542fd74de4c47135e5.tar.gz emacs-2828d5f9d4f097228e9b64542fd74de4c47135e5.tar.bz2 emacs-2828d5f9d4f097228e9b64542fd74de4c47135e5.zip |
Set `default-directory' in *scratch* to the current directory of emacsclient.
* lib-src/emacsclient.c (get_current_dir_name): New function, copied here
from sysdep.c.
(main): Use it to send over the current directory.
* lisp/server.el (server-process-filter): Accept `-dir' command. Set
`default-directory' of the *scratch* buffer on connect, if applicable.
git-archimport-id: lorentey@elte.hu--2004/emacs--multi-tty--0--patch-539
Diffstat (limited to 'lib-src/emacsclient.c')
-rw-r--r-- | lib-src/emacsclient.c | 91 |
1 files changed, 91 insertions, 0 deletions
diff --git a/lib-src/emacsclient.c b/lib-src/emacsclient.c index 42c90934b4b..b538b0093af 100644 --- a/lib-src/emacsclient.c +++ b/lib-src/emacsclient.c @@ -248,6 +248,83 @@ xstrdup (const char *s) return result; } +/* From sysdep.c */ +#if !defined (HAVE_GET_CURRENT_DIR_NAME) || defined (BROKEN_GET_CURRENT_DIR_NAME) + +/* Return the current working directory. Returns NULL on errors. + Any other returned value must be freed with free. This is used + only when get_current_dir_name is not defined on the system. */ +char* +get_current_dir_name () +{ + char *buf; + char *pwd; + struct stat dotstat, pwdstat; + /* If PWD is accurate, use it instead of calling getwd. PWD is + sometimes a nicer name, and using it may avoid a fatal error if a + parent directory is searchable but not readable. */ + if ((pwd = getenv ("PWD")) != 0 + && (IS_DIRECTORY_SEP (*pwd) || (*pwd && IS_DEVICE_SEP (pwd[1]))) + && stat (pwd, &pwdstat) == 0 + && stat (".", &dotstat) == 0 + && dotstat.st_ino == pwdstat.st_ino + && dotstat.st_dev == pwdstat.st_dev +#ifdef MAXPATHLEN + && strlen (pwd) < MAXPATHLEN +#endif + ) + { + buf = (char *) malloc (strlen (pwd) + 1); + if (!buf) + return NULL; + strcpy (buf, pwd); + } +#ifdef HAVE_GETCWD + else + { + size_t buf_size = 1024; + buf = (char *) malloc (buf_size); + if (!buf) + return NULL; + for (;;) + { + if (getcwd (buf, buf_size) == buf) + break; + if (errno != ERANGE) + { + int tmp_errno = errno; + free (buf); + errno = tmp_errno; + return NULL; + } + buf_size *= 2; + buf = (char *) realloc (buf, buf_size); + if (!buf) + return NULL; + } + } +#else + else + { + /* We need MAXPATHLEN here. */ + buf = (char *) malloc (MAXPATHLEN + 1); + if (!buf) + return NULL; + if (getwd (buf) == NULL) + { + int tmp_errno = errno; + free (buf); + errno = tmp_errno; + return NULL; + } + } +#endif + return buf; +} +#endif + + + /* In STR, insert a & before each &, each space, each newline, and any initial -. Change spaces to underscores, too, so that the return value never contains a space. @@ -709,6 +786,20 @@ To start the server in Emacs, type \"M-x server-start\".\n", } } + /* Send over our current directory. */ + if (!current_frame) + { + char *dir = get_current_dir_name (); + if (dir) + { + fprintf (out, "-dir "); + quote_argument (dir, out); + fprintf (out, "/"); + fprintf (out, " "); + free (dir); + } + } + retry: if (nowait) fprintf (out, "-nowait "); |