-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy patheshell-up.el
123 lines (106 loc) · 4.49 KB
/
eshell-up.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
;;; eshell-up.el --- Quickly go to a specific parent directory in eshell -*- lexical-binding: t; -*-
;; Copyright (C) 2016 Peter W. V. Tran-Jørgensen
;; Author: Peter W. V. Tran-Jørgensen <[email protected]>
;; Maintainer: Peter W. V. Tran-Jørgensen <[email protected]>
;; URL: https://github.com/peterwvj/eshell-up
;; Created: 14th October 2016
;; Version: 0.0.4
;; Package-Requires: ((emacs "24"))
;; Keywords: eshell
;; This file is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published
;; by the Free Software Foundation, either version 3 of the License,
;; or (at your option) any later version.
;; This file is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
;; General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this file. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; Package for quickly navigating to a specific parent directory in
;; eshell without having to repeatedly typing 'cd ..'. This is
;; achieved using the 'eshell-up' function, which can be bound to an
;; eshell alias such as 'up'. As an example, assume that the current
;; working directory is:
;;
;; /home/user/first/second/third/fourth/fifth $
;;
;; Now, in order to quickly go to (say) the directory named 'first' one
;; simply executes:
;;
;; /home/user/first/second/third/fourth/fifth $ up fi
;; /home/user/first $
;;
;; This command searches the current working directory from right to
;; left (while skipping the current directory, 'fifth') for a
;; directory that matches the user's input ('fi' in this case). If a
;; match is found then eshell changes to that directory, otherwise it
;; does nothing.
;;
;; It is recommended to invoke 'eshell-up' using an alias as done in
;; the example above. To do that, add the following to your
;; .eshell.aliases file:
;;
;; alias up eshell-up $1
;;
;; The complete description of eshell-up, including other features, is
;; available at: https://github.com/peterwvj/eshell-up
;;
;; This package is inspired by 'bd', which uses bash to implement
;; similar functionality.
;;
;; See: https://github.com/vigneshwaranr/bd
;;; Code:
;; User-definable variables
(defvar eshell-up-ignore-case t "Non-nil if searches must ignore case.")
(defvar eshell-up-print-parent-dir nil "Non-nil if the parent directory must be printed before ‘eshell-up’ changes to it.")
(defun eshell-up-closest-parent-dir (file)
"Find the closest parent directory of a file.
Argument FILE the file to find the closest parent directory for."
(file-name-directory
(directory-file-name
(expand-file-name file))))
(defun eshell-up-find-parent-dir (path &optional match)
"Find the parent directory based on the user's input.
Argument PATH the source directory to search from.
Argument MATCH a string that identifies the parent directory to search for."
(let ((closest-parent (eshell-up-closest-parent-dir path)))
(if match
(let ((case-fold-search eshell-up-ignore-case))
(locate-dominating-file closest-parent
(lambda (parent)
(let ((dir (file-name-nondirectory
(expand-file-name
(directory-file-name parent)))))
(if (string-match match dir)
dir
nil)))))
closest-parent)))
;;;###autoload
(defun eshell-up (&optional match)
"Go to a specific parent directory in eshell.
Argument MATCH a string that identifies the parent directory to go
to."
(interactive)
(let* ((path default-directory)
(parent-dir (eshell-up-find-parent-dir path match)))
(progn
(when parent-dir
(eshell/cd parent-dir))
(when eshell-up-print-parent-dir
(if parent-dir
(eshell/echo parent-dir)
(eshell/echo path))))))
;;;###autoload
(defun eshell-up-peek (&optional match)
"Find a specific parent directory in eshell.
Argument MATCH a string that identifies the parent directory to find"
(interactive)
(let* ((path default-directory)
(parent-dir (eshell-up-find-parent-dir path match)))
(if parent-dir
parent-dir
path)))
(provide 'eshell-up)
;;; eshell-up.el ends here