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

  • I'm not sure I understand the question. If the difficulty is capturing a path around point, look at what ffap does. – Gilles 'SO- stop being evil' May 5 '20 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 May 5 '20 at 14:04
  • See emacs.stackexchange.com/tags/elisp/info. – Drew May 5 '20 at 18:26
1

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)))
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, privacy policy and cookie policy

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