Skip to content

Instantly share code, notes, and snippets.

@knjname
Last active January 1, 2016 21:39
Show Gist options
  • Select an option

  • Save knjname/8204631 to your computer and use it in GitHub Desktop.

Select an option

Save knjname/8204631 to your computer and use it in GitHub Desktop.
(defun parent-directory (dir)
(unless (equal "/" dir)
(file-name-directory (directory-file-name dir))))
(defun find-nearest-file (dir-path filename)
(let ((file (concat dir-path filename))
(parent (parent-directory (expand-file-name dir-path))))
(if (file-exists-p file)
file
(when parent
(find-nearest-file parent filename)))))
(defun current-buffer-project-clj ()
(find-nearest-file (file-name-directory (buffer-file-name)) "project.clj"))
(defmacro with-clj-buffer (filepath &rest body)
`(with-temp-buffer
(insert-file-contents ,filepath)
,@body))
(defmacro let-if (varname condition-expr true-part &optional false-part)
`(let
((,varname ,condition-expr))
(if ,varname
,true-part
,false-part)))
(defun guess-ring-nrepl-port (filepath)
(with-clj-buffer filepath
(beginning-of-buffer)
(if (and
(re-search-forward ":nrepl" nil t)
(re-search-forward ":port" nil t))
(progn
(forward-word)
(thing-at-point 'word)))))
(defun open-current-clj-project-ring-nrepl () (interactive)
(let-if project-file-path
(current-buffer-project-clj)
(let-if ring-nrepl-port
(guess-ring-nrepl-port project-file-path)
(nrepl "127.0.0.1" ring-nrepl-port)
(error "Port specification is not found."))
(error "project.clj is not found.")))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment