A thought came to me that directory tracking in shell-mode can be done
in very simple and robust way. One just needs to call emacsclient from
prompt string in order to set value of default-directory of the shell
buffer. Name of the shell buffer can be transferred to the shell process
using environment. So I have the following solution.

In init.el:

(advice-add 'comint-exec :around
            #'(lambda (f buffer &rest args)
                (let ((process-environment (cons (concat "EMACS_BUFFER=" 
(buffer-name buffer))
                                                 process-environment)))
                  (apply f buffer args)))
            '((name . "envbuf")))

At the end of ~/.bashrc:

if [ -n "$EMACS_BUFFER" ]; then
    set_emacs_dir() {
        >/dev/null 2>&1 emacsclient --eval \
            '(with-current-buffer "'"$EMACS_BUFFER"'" (setq default-directory 
"'"$PWD"'"))'
    }
    PS1='`set_emacs_dir`'"$PS1"
fi

With regards
Dmitri Paduchikh

_______________________________________________
gnu-emacs-sources mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/gnu-emacs-sources

Reply via email to