Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
2016-10-15 - Insert Relative Path
(require 'f)
(defconst fn/relative-same-node "."  
"Represents the same node on a path.")
(defconst fn/relative-parent-node ".."  
"Represents the parent node on a path.")
(defun fn/relative-parent-node-thunk (&rest args)
"Always returns `fn/relative-parent-node'."
(defun fn/relative-path (source-path target-path)
"An attempt to compute the relative path from SOURCE-PATH to TARGET-PATH."
(letrec ((recurser
(lambda (upward-nodes downward-nodes parent-node)
((null (car upward-nodes))
(append (list fn/relative-same-node)
((null (car downward-nodes))
(append (mapcar #'fn/relative-parent-node-thunk upward-nodes)
(list parent-node))
((string-equal (car upward-nodes)
(car downward-nodes))
(funcall recurser
(cdr upward-nodes)
(cdr downward-nodes)
(car upward-nodes)))
(lexical-let* ((up-nodes (or (mapcar #'fn/relative-parent-node-thunk (cdr upward-nodes))
(list fn/relative-same-node)))
(down-nodes downward-nodes))
(append up-nodes down-nodes)
(funcall recurser (f-split source-path) (f-split target-path) fn/relative-same-node)))
(defun fn/insert-relative-file-path (arg)
"Insert the relative file path of a source file to a target file.
If prefix ARG is present, select the source file."
(interactive "P")
(let* ((source-path
(car (find-file-read-args "Find source file: " t)))
(t (error "Source file does not have a file name."))))      
(target-path (car (find-file-read-args "Find target file: " t))))
(insert (fn/relative-path source-path target-path))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment