Skip to content
This repository

Add cdp shell function #39

Merged
merged 2 commits into from over 1 year ago

3 participants

Takafumi Arakaki Thomas Frössman Jonathan Rockway
Takafumi Arakaki

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.

Thomas Frössman

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

Thomas Frössman

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
}
Takafumi Arakaki

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

Thomas Frössman

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

Takafumi Arakaki

Merged tkf#1. Thanks.

Jonathan Rockway
Owner

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 :smile:

Jonathan Rockway jrockway merged commit 906bb29 into from December 06, 2012
Jonathan Rockway jrockway closed this December 06, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 2 unique commits by 2 authors.

Dec 04, 2012
Takafumi Arakaki Add cdp shell function 5cc022a
Dec 06, 2012
Thomas Frössman make cdp alias more fault tolerant. 6555f9a
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 46 additions and 0 deletions. Show diff stats Hide diff stats

  1. 46  contrib/eproject.sh
46  contrib/eproject.sh
... ...
@@ -0,0 +1,46 @@
  1
+### eproject.sh --- eproject shell helper functions
  2
+
  3
+# Copyright (C) 2012 Takafumi Arakaki
  4
+
  5
+# Author: Takafumi Arakaki <aka.tkf at gmail.com>
  6
+# Keywords: eproject
  7
+
  8
+# This program is free software; you can redistribute it and/or modify
  9
+# it under the terms of the GNU General Public License as published by
  10
+# the Free Software Foundation, either version 3 of the License, or
  11
+# (at your option) any later version.
  12
+
  13
+# This program is distributed in the hope that it will be useful,
  14
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
  15
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  16
+# GNU General Public License for more details.
  17
+
  18
+# You should have received a copy of the GNU General Public License
  19
+# along with this program.  If not, see <http://www.gnu.org/licenses/>.
  20
+
  21
+### Commentary:
  22
+
  23
+# To use this file, add the following line in your shell setting:
  24
+#   source PATH/TO/eproject.sh
  25
+
  26
+### Code:
  27
+
  28
+# Go to currently active project root in Emacs
  29
+cdp() {
  30
+    local EMACS_CWP=$(emacsclient -a false -e "
  31
+  (let ((current-buffer
  32
+         (nth 1 (assoc 'buffer-list
  33
+                       (nth 1 (nth 1 (current-frame-configuration)))))))
  34
+    (or (ignore-errors (eproject-root current-buffer))
  35
+        (with-current-buffer current-buffer
  36
+          (let ((filename (buffer-file-name)))
  37
+            (if filename
  38
+                (file-name-directory filename)
  39
+              default-directory)))))
  40
+    " | sed 's/^"\(.*\)"$/\1/')
  41
+    if [ -d "$EMACS_CWP" ]; then
  42
+        cd "$EMACS_CWP"
  43
+    else
  44
+        return 1
  45
+    fi
  46
+}
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.