Add cdp shell function #39

Merged
merged 2 commits into from Dec 7, 2012

Projects

None yet

3 participants

@tkf
Contributor
tkf commented Dec 4, 2012

This pull request adds cdp (cd to project) shell function. In your terminal, you can type this command to go to the root of the project you are opening now. Needs emacsclient.

@thomasf
Contributor
thomasf commented Dec 5, 2012

When no emacs server is running I get the following error message and one new emacs instance with lots of file buffers named after source provided to emacsclient instead:

emacsclient: can't find socket; have you started the server?
To start the server in Emacs, type "M-x server-start".
/home/a00001/.bin/editor: line 13: [: too many arguments
chdir to

Maybe it's worth fixing

@thomasf
Contributor
thomasf commented Dec 6, 2012

Ive modified it a bit

  • does not print the directory it will cd to (it's a very non unixey modus operandi)
  • handles when emacs server is runnig setting by useing /bin/false as an alternative editor.
  • hides any errors coming from emacs and returns with non 0 exit status if something goes wrong
  • set EMACS_CWP as a function local variable which is supported by at least bash, dash. ksh and zsh
  • check if the returned directory is a valid directory according to the shell.

Now it looks like this::

# Go to currently active project root in Emacs
cdp() {
    local EMACS_CWP=$(emacsclient -a false -e "
  (let ((current-buffer
         (nth 1 (assoc 'buffer-list
                       (nth 1 (nth 1 (current-frame-configuration)))))))
    (or (ignore-errors (eproject-root current-buffer))
        (with-current-buffer current-buffer
          (let ((filename (buffer-file-name)))
            (if filename
                (file-name-directory filename)
              default-directory)))))
    " 2>/dev/null | sed 's/^"\(.*\)"$/\1/')
    if [ -d "$EMACS_CWP" ]; then
        cd "$EMACS_CWP"
    else
        return 1
    fi
}
@tkf
Contributor
tkf commented Dec 6, 2012

Thanks for the review! I agree with your points except for hiding errors from Emacs. Why not just show them?

Can you send a pull request to my shell-utils branch? That way your change will show up here. I just don't want to take your credit. :)

@thomasf
Contributor
thomasf commented Dec 6, 2012

ok., lets show the errors. i'll make the pull req.

@tkf
Contributor
tkf commented Dec 6, 2012

Merged tkf#1. Thanks.

@jrockway
Owner
jrockway commented Dec 7, 2012

This is clever and will probably prove quite useful.

I'd prefer if that Lisp code was in a .el file, however, so it's easier to edit and tweak. The shell script should then look like: emacsclient -a false -e "(eproject-print-current-project-working-directory)" or something like that.

I'll merge this for now, however, as it's not a really big problem 😄

@jrockway jrockway merged commit 906bb29 into jrockway:master Dec 7, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment