From f1930bd7c2361a8b02062ea3d411fe2d62e0e1a9 Mon Sep 17 00:00:00 2001 From: Ivan Lazar Miljenovic Date: Sun, 15 Mar 2015 20:28:30 +1100 Subject: [PATCH 1/2] Ensure directory exists before searching for a .cabal file --- haskell-cabal.el | 32 +++++++++++++++++--------------- 1 file changed, 17 insertions(+), 15 deletions(-) diff --git a/haskell-cabal.el b/haskell-cabal.el index ef6175d35..d168de60a 100644 --- a/haskell-cabal.el +++ b/haskell-cabal.el @@ -211,21 +211,23 @@ Return nil if no Cabal description file could be located via If DIR is nil, `default-directory' is used as starting point for directory traversal. Upward traversal is aborted if file owner changes. Uses`haskell-cabal-find-pkg-desc' internally." - (catch 'found - (let ((user (nth 2 (file-attributes (or dir default-directory)))) - ;; Abbreviate, so as to stop when we cross ~/. - (root (abbreviate-file-name (or dir default-directory)))) - ;; traverse current dir up to root as long as file owner doesn't change - (while (and root (equal user (nth 2 (file-attributes root)))) - (let ((cabal-file (haskell-cabal-find-pkg-desc root))) - (when cabal-file - (throw 'found cabal-file))) - - (let ((proot (file-name-directory (directory-file-name root)))) - (if (equal proot root) ;; fix-point reached? - (throw 'found nil) - (setq root proot)))) - nil))) + (let ((use-dir (or dir default-directory))) + (when (file-directory-p use-dir) + (catch 'found + (let ((user (nth 2 (file-attributes use-dir))) + ;; Abbreviate, so as to stop when we cross ~/. + (root (abbreviate-file-name use-dir))) + ;; traverse current dir up to root as long as file owner doesn't change + (while (and root (equal user (nth 2 (file-attributes root)))) + (let ((cabal-file (haskell-cabal-find-pkg-desc root))) + (when cabal-file + (throw 'found cabal-file))) + + (let ((proot (file-name-directory (directory-file-name root)))) + (if (equal proot root) ;; fix-point reached? + (throw 'found nil) + (setq root proot)))) + nil))))) (defun haskell-cabal-find-pkg-desc (dir &optional allow-multiple) "Find a package description file in the directory DIR. From 4cdecb9940585bf9b879ce137e7215acea8bfc1b Mon Sep 17 00:00:00 2001 From: Ivan Lazar Miljenovic Date: Tue, 17 Mar 2015 16:15:05 +1100 Subject: [PATCH 2/2] Find Cabal file even when directory does not yet exist If you have a Cabalised project in an existing directory "/path/to/project/" and you want to add a new module into an as-yet nonexistent directory "Foo", you still might want to be able to find the .cabal file for that project (e.g. to use fields from the .cabal file to populate a skeleton for the new module). This patch keeps stripping off directories from the end of the path until an existing directory is found (or the directory is found to be nonsensical). --- haskell-cabal.el | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/haskell-cabal.el b/haskell-cabal.el index d168de60a..134bffbba 100644 --- a/haskell-cabal.el +++ b/haskell-cabal.el @@ -212,7 +212,9 @@ If DIR is nil, `default-directory' is used as starting point for directory traversal. Upward traversal is aborted if file owner changes. Uses`haskell-cabal-find-pkg-desc' internally." (let ((use-dir (or dir default-directory))) - (when (file-directory-p use-dir) + (while (and use-dir (not (file-directory-p use-dir))) + (setq use-dir (file-name-directory (directory-file-name use-dir)))) + (when use-dir (catch 'found (let ((user (nth 2 (file-attributes use-dir))) ;; Abbreviate, so as to stop when we cross ~/.