mylisp/cygwin-winpath.el
author Oleksandr Gavenko <gavenkoa@gmail.com>
Wed, 02 Jun 2021 00:34:11 +0300
changeset 1731 fec5d1fffe8c
permissions -rw-r--r--
Added mode for recognition of Windows paths in Cygwin Emacs.
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1731
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     1
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     2
(defvar cygwin-winpath-cygdrive-prefix "/cygdrive")
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     3
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     4
(defconst cygwin-winpath-regex-forward-slash "\\`\\([a-zA-Z]\\):/")
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     5
(defconst cygwin-winpath-regex-backward-slash "\\`\\([a-zA-Z]\\):\\\\")
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     6
(defconst cygwin-winpath-regex-list (list cygwin-winpath-regex-forward-slash cygwin-winpath-regex-backward-slash))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     7
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     8
(defun cygwin-winpath-cygwin-file-name (winpath)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
     9
  (cond
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    10
   ((string-match cygwin-winpath-regex-forward-slash winpath)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    11
    (concat cygwin-winpath-cygdrive-prefix "/" (downcase (match-string 1 winpath)) "/" (substring winpath (match-end 0))))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    12
   ((string-match cygwin-winpath-regex-backward-slash winpath)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    13
    (concat cygwin-winpath-cygdrive-prefix "/" (downcase (match-string 1 winpath)) "/" (replace-regexp-in-string "\\\\" "/" (substring winpath (match-end 0)))))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    14
   (t winpath)))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    15
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    16
;; (cygwin-winpath-cygwin-file-name "c:/home/my.txt")
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    17
;; (cygwin-winpath-cygwin-file-name "c:\\home\\my.txt")
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    18
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    19
(defun cygwin-winpath--mapping-function (op winname &rest args)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    20
  (let ((inhibit-file-name-handlers (cons #'cygwin-winpath--mapping-function inhibit-file-name-handlers))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    21
        (inhibit-file-name-operation op))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    22
    (apply op (cygwin-winpath-cygwin-file-name winname) args)))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    23
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    24
(defvar cygwin-winpath-activated nil)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    25
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    26
(defun cygwin-winpath--fail-if-not-cygwin ()
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    27
  (unless (eq system-type 'cygwin)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    28
    (error "Mode is suppose to work only inside Cygwin")))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    29
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    30
;;;###autoload
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    31
(defun cygwin-winpath-activate ()
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    32
  "Start interpret Windows paths inside Cygwin Emacs."
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    33
  (interactive)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    34
  (cygwin-winpath--fail-if-not-cygwin)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    35
  (unless cygwin-winpath-activated
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    36
    (mapc (lambda (regex)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    37
            (add-to-list 'file-name-handler-alist (cons regex #'cygwin-winpath--mapping-function)))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    38
          cygwin-winpath-regex-list)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    39
    (setq cygwin-winpath-activated t)))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    40
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    41
;;;###autoload
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    42
(defun cygwin-winpath-deactivate ()
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    43
  "Stop interpret Windows paths inside Cygwin Emacs."
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    44
  (interactive)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    45
  (cygwin-winpath--fail-if-not-cygwin)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    46
  (when cygwin-winpath-activated
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    47
    (mapc (lambda (regex)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    48
            (setf file-name-handler-alist (cl-delete regex file-name-handler-alist :key #'car :test #'equal)))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    49
          cygwin-winpath-regex-list)
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    50
    (setq cygwin-winpath-activated t)))
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    51
fec5d1fffe8c Added mode for recognition of Windows paths in Cygwin Emacs.
Oleksandr Gavenko <gavenkoa@gmail.com>
parents:
diff changeset
    52
(provide 'cygwin-winpath)