-
Notifications
You must be signed in to change notification settings - Fork 55
/
jdee-cygwin.el
143 lines (127 loc) · 5.75 KB
/
jdee-cygwin.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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
;;; jdee-cygwin.el --- Cygwin helper functions
;; Maintainer: jdee-devel
;; GNU Emacs 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 2, or (at your option)
;; any later version.
;; GNU Emacs 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 GNU Emacs; see the file COPYING. If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
;;; Commentary:
;;; Code:
(defun jdee-cygpath (path &optional direction)
"If `system-type' is cygwin or cygwin32, converts PATH from cygwin
to DOS form (if DIRECTION is nil) or to cygwin form (if DIRECTION is
nonnil). The converion requires that cygpath be in your path. If the
`system-type' is not cygwin or cygwin32, returns PATH unchanged."
(interactive "sPath: ")
(if (member system-type '(cygwin32 cygwin))
(if (executable-find "cygpath")
(save-excursion
(let ((buf-name "*cygwin-output*")
(output-type (if direction "-u" "-w")))
(shell-command
(concat "cygpath " output-type " -p '" path "'") buf-name)
(set-buffer buf-name)
(let ((output (buffer-substring (point-min) (point-max))))
(kill-buffer buf-name)
(cl-substitute ?\/ ?\\ (cl-remove ?\n output)))))
(error "Cannot find cygpath executable"))
path))
(defvar jdee-cygwin-root-cache nil
"Cache of converted cygwin root directory paths.")
(defun jdee-cygwin-path-converter-cygpath (path)
(interactive "sPath: ")
(if (string-match "^[a-zA-Z]:" path)
path
(cond
((string-match "^/\\(cygdrive\\)?/\\([a-zA-Z]\\)/" path)
(concat
(substring
path
(match-beginning 2)
(match-end 2))
":/"
(substring path (match-end 0))))
((string-match "^/[^/]*" path)
(let* ((root (substring
path (match-beginning 0) (match-end 0)))
(rest (substring path (match-end 0)))
(converted-root (cdr (assoc root jdee-cygwin-root-cache))))
(if (not converted-root)
(progn
(setq converted-root (jdee-cygpath root))
(if converted-root
(add-to-list 'jdee-cygwin-root-cache
(cons root converted-root))
(error "Cannot convert %s" path))))
(if (string= rest "")
converted-root
(concat converted-root rest))))
(t
(error "Cannot convert %s" path)))))
(defun jdee-cygwin-path-converter-internal (path)
"Convert cygwin style `PATH' to a form acceptable to java vm.
Basically converts paths of the form: '//C/dir/file' or '/cygdrive/c/dir/file'
to 'c:/dir/file'. This function will not modify standard unix style paths
unless they begin with '//[a-z]/' or '/cygdrive/[a-z]/'."
(interactive "sPath: ")
(if (fboundp 'mswindows-cygwin-to-win32-path)
(cl-substitute ?/ ?\\ (mswindows-cygwin-to-win32-path path))
(let* ((path-re "/\\(cygdrive\\)?/\\([a-zA-Z]\\)/")
(subexpr 2)
(index1 (* 2 subexpr))
(index2 (1+ index1)))
(if (string-match (concat "^" path-re) path)
(let ((new-path
(concat (substring path
(nth index1 (match-data))
(nth index2 (match-data)))
":/"
(substring path (match-end 0)))))
(while (string-match (concat ":" path-re) new-path)
(setq new-path
(concat
(substring new-path 0 (match-beginning 0))
";"
(substring new-path
(nth index1 (match-data))
(nth index2 (match-data)))
":/"
(substring new-path (match-end 0)))))
(cl-substitute ?\\ ?\/ new-path))
path))))
(defcustom jdee-cygwin-path-converter '(jdee-cygwin-path-converter-internal)
"Function to use to convert cygwin paths to DOS paths.
Choose jdee-cygwin-path-converter-internal, jdee-cygwin-path-converter-cygpath,
or \"custom-function.\" jdee-cygwin-path-converter-cygpath handles all
cygwin-style paths, including mount points, e.g.,/bin.
jdee-cygwin-path-converter-internal does not handle mount
paths. However, it is much faster as it does not require running a
subprocess every time the JDEE needs to convert a path. Choose
\"custom-function\" if you want the JDEE to use a function that you
supply. Replace \"custom-function\" with the name of the function that
you want to use."
:group 'jdee-project
:type '(list
(radio-button-choice :format "%t \n%v"
:tag "Converter: "
:entry-format " %b %v"
(const jdee-cygwin-path-converter-internal)
(const jdee-cygwin-path-converter-cygpath)
(function custom-function))))
(defun jdee-convert-cygwin-path (path &optional separator)
"Convert cygwin style `PATH' to a form acceptable to java vm, using
the conversion function specified by `jdee-cygwin-path-converter'."
(interactive "sPath: ")
(funcall (car jdee-cygwin-path-converter)
(if separator
(cl-substitute ?\: (string-to-char separator) path)
path)))
(provide 'jdee-cygwin)
;;; jdee-cygwin.el ends here