Skip to content

Commit

Permalink
More intelligent project-name guessing.
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisdone committed Mar 24, 2012
1 parent 9b96ddb commit c8a57f1
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 2 deletions.
25 changes: 23 additions & 2 deletions haskell-session.el
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
;;; Code:

(require 'haskell-cabal)
(require 'haskell-string)

;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Globals
Expand All @@ -46,8 +47,28 @@
(defun haskell-session ()
"Get the Haskell session, prompt if there isn't one or fail."
(or (haskell-session-maybe)
(haskell-session-assign (haskell-session-choose))
(haskell-session-assign (haskell-session-new))))
(haskell-session-assign
(or (haskell-session-from-buffer)
(haskell-session-new-assume-from-cabal)
(haskell-session-choose)
(haskell-session-new)))))

(defun haskell-session-new-assume-from-cabal ()
"Prompt to create a new project based on a guess from the nearest Cabal file."
(when (y-or-n-p (format "Start a new project named “%s”? "
(haskell-session-default-name)))
(haskell-session-make (haskell-session-default-name))))

(defun haskell-session-from-buffer ()
"Get the session based on the buffer."
(let ((sessions (remove-if-not (lambda (session)
(haskell-is-prefix-of (file-name-directory (buffer-file-name))
(haskell-session-cabal-dir session)))
haskell-sessions)))
(sort sessions (lambda (a b) (< (length (haskell-session-cabal-dir a))
(length (haskell-session-cabal-dir b)))))
(when (consp sessions)
(car sessions))))

(defun haskell-session-new ()
"Make a new session."
Expand Down
21 changes: 21 additions & 0 deletions haskell-string.el
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
(defun haskell-trim (string)
(replace-regexp-in-string
"^[ \t\n]+" ""
(replace-regexp-in-string
"[ \t\n]+$" ""
string)))

(defun haskell-string-take (string n)
"Take n chars from string."
(substring string
0
(min (length string) n)))

(defun haskell-is-prefix-of (x y)
"Is x string a prefix of y string?"
(string= (substring x 0 (min (length x) (length y)))
(substring y 0 (min (length x) (length y)))))

(defun haskell-string ())

(provide 'haskell-string)

0 comments on commit c8a57f1

Please sign in to comment.