0

Here at 'dayjob I'm often sent paths from developers using Windows in Windows format:

D:\Some\Path

These paths can uniformly be converted to paths valid on at least some Linux machines at 'dayjob like:

/mnt/remote-d/Some/Path

I would like to write an interactive function to automate this conversion, so that I may simply tap something along the lines of C-t C-p to perform this conversion. The string transformation part of the function will probably be something along the lines of

(replace-regexp-in-string "\\\\" "/" str)

But then we also have to capture the string from the buffer. For that purpose thing-at-point looks promising, but I've thus far been unable to tame it. I have also found the following function which I think captures what I'm after with some modification: https://github.com/akicho8/string-inflection/blob/master/string-inflection.el#L184

3
  • I'm not sure I understand the question. If the difficulty is capturing a path around point, look at what ffap does. Commented May 5, 2020 at 10:27
  • I was not able to comprehend ffap, it was very confusing to me. But I was able to figure out string-inflection and wrap it to my will.
    – Rovanion
    Commented May 5, 2020 at 14:04
  • See emacs.stackexchange.com/tags/elisp/info.
    – Drew
    Commented May 5, 2020 at 18:26

2 Answers 2

2

I was able to construct a function based on the code in string-inflection.

(defconst path-characters "a-zA-Z0-9:\\\\_-")

(defun win-to-posix-posix-path ()
  "Rewrites a Windows formatted path to be of POSIX style."
  (interactive)
  (let* ((start (if mark-active
                    (region-end)
                  (progn
                    (skip-chars-forward path-characters)
                    (point))))
         (end (if mark-active
                  (region-beginning)
                (progn
                  (skip-chars-backward path-characters)
                  (point))))
         (str (buffer-substring start end))
         (slash-fix (replace-regexp-in-string "\\\\"  "/"       str))
         (m-fix     (replace-regexp-in-string "[Mm]:" "/mnt/m"  slash-fix t))
         (v-fix     (replace-regexp-in-string "[Vv]:" "/mnt/v"  mls-fix   t)))
    (delete-region start end)
    (insert v-fix)))
1
  • I was able to make this work by changing mls-fix to m-fix. I was then able to tailor it so it also supports changing network drive references to mounted drives that are mapped via Windows to the network drives. I also created a shortcut C-c C-t to execute the function. This is a big time saver. Thanks for this solution! Commented Jun 25 at 20:27
0

To get the file path you can try:

(let ((thing-at-point-file-name-chars
       (concat thing-at-point-file-name-chars "\\")))
  (thing-at-point 'filename))

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.