-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsl-path.el
134 lines (110 loc) · 4.68 KB
/
wsl-path.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
;;; wsl-path.el -- Teach EMACS about windows subsystem for linux styles paths
;; Copyright (C) 2009 Victor Ren
;; Copyright (C) 2019-08-29 Zach Kost-Smith
;; 2023-11-02 Todd Goldfinger
;; Author: Victor Ren <[email protected]>
;; Modified for WSL by: Zach Kost-Smith <[email protected]>
;; Keywords: windows, WSL, mount, path
;; This file is *NOT* (yet?) part of GNU Emacs.
;;
;; This program 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.
;;
;; This program 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:
;; This package lets you use windows-style filenames like "c:/path/file" or
;; "c:\path\file" in Emacs when running in the Windows Subsystem for Linux.
;;; Installation:
;; Put in your .emacs or site-start.el file the following lines:
;; (require 'wsl-path)
;; (wsl-path-activate)
;;; Compatibility
;; How it works: Push some functions onto file-name-handler-alist. which detect
;; filenames expressed in Windows style, and translate those names into the WSL
;; equivalent.
;;; Code:
(require 'cl-lib)
(defconst wsl-path-version "0.2")
(defconst wsl-path-style1-regexp "\\`\\(.*/\\)?\\([a-zA-Z]:\\|[.]\\)\\\\")
(defconst wsl-path-style2-regexp "\\`\\(.*/\\)?\\([a-zA-Z]:\\)/")
(defvar wsl-path-activated nil)
(defgroup wsl-path nil
"Proper handling of windows filenames."
:prefix "wsl-path-"
:group 'files)
(defvar wslpath-exec "wslpath"
"The wslpath executable location.")
(defun wsl-path-run-real-handler (operation args)
"Run OPERATION with ARGS."
(let ((inhibit-file-name-handlers
(append '(wsl-path-map-drive-hook-function)
(and (eq inhibit-file-name-operation operation)
inhibit-file-name-handlers)))
(inhibit-file-name-operation operation))
(apply operation args)))
(defun wsl-path-convert-to-linux (name)
"Convert file NAME to WSL style (`x:/' to `/mnt/x/')."
(if (or (string-match wsl-path-style1-regexp name)
(string-match wsl-path-style2-regexp name))
(unwind-protect
;; inhibit only works for one operation, but there are multiple that happen in call-process
(with-temp-buffer
(wsl-path-deactivate)
(call-process wslpath-exec nil t nil "-u" name)
(s-trim (buffer-string)))
(wsl-path-activate))
name))
(defun wsl-path-convert-to-win (name)
"Convert file NAME to WSL style (`/mnt/x/' to `x:/')."
(with-temp-buffer
(call-process wslpath-exec nil t nil "-m" name)
(s-trim (buffer-string))))
(defun wsl-path-map-drive-hook-function (operation name &rest args)
"Run OPERATION on WSL NAME with ARGS.
Map Windows sytle name to the WSL-style \"/[A-Za-z]/\" and call
OPERATION with the mapped filename\(s). NAME must have the format looks like
\"^/[A-Za-z]:/\" or \"^[A-Za-z]:\\\" here. Note that at least the first
element of ARGS could be a filename too \(then it must have the same syntax
like NAME!) which must be converted \(e.g. `expand-file-name' can be called
with two filenames)."
(wsl-path-run-real-handler
operation
(cons (wsl-path-convert-to-linux name)
(if (stringp (car args))
(cons (wsl-path-convert-to-linux (car args))
(cdr args))
args))))
(defun wsl-path-activate ()
"Activate wsl-path-style-handling."
(interactive)
(unless wsl-path-activated
(add-to-list 'file-name-handler-alist
(cons wsl-path-style1-regexp
'wsl-path-map-drive-hook-function))
(add-to-list 'file-name-handler-alist
(cons wsl-path-style2-regexp
'wsl-path-map-drive-hook-function))
(setq wsl-path-activated t)))
(defun wsl-path-deactivate ()
"Deactivate windows-style-path handling."
(interactive)
(unless (not wsl-path-activated)
(setq file-name-handler-alist
(delete (assoc wsl-path-style1-regexp file-name-handler-alist)
file-name-handler-alist))
(setq file-name-handler-alist
(delete (assoc wsl-path-style2-regexp file-name-handler-alist)
file-name-handler-alist))
(setq wsl-path-activated nil)))
(provide 'wsl-path)
;;; wsl-path.el ends here