Skip to content

Few doc improvements and a bit of refactoring #1205

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Mar 13, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 34 additions & 23 deletions haskell-cabal.el
Original file line number Diff line number Diff line change
Expand Up @@ -105,14 +105,23 @@
"List of Cabal buffers.")

(defun haskell-cabal-buffers-clean (&optional buffer)
"Refresh list of known cabal buffers.

Check each buffer in variable `haskell-cabal-buffers' and remove
it from list if one of the following conditions are hold:
+ buffer is killed;
+ buffer's mode is not derived from `haskell-cabal-mode';
+ buffer is a BUFFER (if given)."
(let ((bufs ()))
(dolist (buf haskell-cabal-buffers)
(if (and (buffer-live-p buf) (not (eq buf buffer))
(if (and (buffer-live-p buf)
(not (eq buf buffer))
(with-current-buffer buf (derived-mode-p 'haskell-cabal-mode)))
(push buf bufs)))
(setq haskell-cabal-buffers bufs)))

(defun haskell-cabal-unregister-buffer ()
"Exclude current buffer from global list of known cabal buffers."
(haskell-cabal-buffers-clean (current-buffer)))

;;;###autoload
Expand Down Expand Up @@ -150,7 +159,8 @@
(setq indent-tabs-mode nil)
)

(defun haskell-cabal-get-setting (name)
(defun haskell-cabal--get-field (name)
"Try to read value of field with NAME from current buffer."
(save-excursion
(let ((case-fold-search t))
(goto-char (point-min))
Expand All @@ -174,8 +184,8 @@
val)))))

;;;###autoload
(defun haskell-cabal-guess-setting (name)
"Guess the specified setting of this project.
(defun haskell-cabal-get-field (name)
"Read the value of field with NAME from project's cabal file.
If there is no valid .cabal file to get the setting from (or
there is no corresponding setting with that name in the .cabal
file), then this function returns nil."
Expand All @@ -185,7 +195,7 @@ file), then this function returns nil."
(when (and cabal-file (file-readable-p cabal-file))
(with-temp-buffer
(insert-file-contents cabal-file)
(haskell-cabal-get-setting name))))))
(haskell-cabal--get-field name))))))

;;;###autoload
(defun haskell-cabal-get-dir (&optional use-defaults)
Expand Down Expand Up @@ -442,23 +452,24 @@ OTHER-WINDOW use `find-file-other-window'."
(let ((cabal-file (haskell-cabal-find-file)))
(when (and cabal-file (file-readable-p cabal-file))
(with-temp-buffer
(insert-file-contents cabal-file)
(haskell-cabal-mode)
(goto-char (point-min))
(let ((matches)
(projectName (haskell-cabal-get-setting "name")))
(haskell-cabal-next-section)
(while (not (eobp))
(if (haskell-cabal-source-section-p (haskell-cabal-section))
(let ((val (car (split-string
(haskell-cabal-section-value (haskell-cabal-section))))))
(if (or (string= val "")
(string= val "{")
(not val))
(push projectName matches)
(push val matches))))
(haskell-cabal-next-section))
(reverse matches))))))
(insert-file-contents cabal-file)
(haskell-cabal-mode)
(goto-char (point-min))
(let ((matches)
(projectName (haskell-cabal--get-field "name")))
(haskell-cabal-next-section)
(while (not (eobp))
(if (haskell-cabal-source-section-p (haskell-cabal-section))
(let ((val (car (split-string
(haskell-cabal-section-value
(haskell-cabal-section))))))
(if (or (string= val "")
(string= val "{")
(not val))
(push projectName matches)
(push val matches))))
(haskell-cabal-next-section))
(reverse matches))))))

(defmacro haskell-cabal-with-subsection (subsection replace &rest funs)
"Copy subsection data into a temporary buffer, save indentation
Expand Down Expand Up @@ -1036,7 +1047,7 @@ source-section."
(concat package
(if version (concat " >= " version) ""))))))
(haskell-cabal-add-build-dependency entry sort silent)
(when (or silent (y-or-n-p "Save cabal file?"))
(when (or silent (y-or-n-p "Save cabal file? "))
(save-buffer))))
;; unwind
(haskell-mode-toggle-interactive-prompt-state t)))
Expand Down
2 changes: 1 addition & 1 deletion inf-haskell.el
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ The process PROC should be associated to a comint buffer."
)
(or (when cabal
(with-current-buffer cabal
(let ((hsd (haskell-cabal-get-setting "hs-source-dirs")))
(let ((hsd (haskell-cabal--get-field "hs-source-dirs")))
(if (null hsd)
;; If there's a Cabal file with no Hs-Source-Dirs, then
;; just use the Cabal file's directory.
Expand Down
10 changes: 5 additions & 5 deletions tests/haskell-cabal-tests.el
Original file line number Diff line number Diff line change
Expand Up @@ -16,20 +16,20 @@
(should (equal '("Test" "test-1" "bench-1" "bin-1")
(haskell-cabal-enum-targets)))))

(ert-deftest haskell-cabal-guess-setting-1 ()
(ert-deftest haskell-cabal-get-field-1 ()
(with-temp-buffer
(let ((scriptDir
(file-name-directory
(or (symbol-file 'haskell-cabal-guess-setting-1)
(or (symbol-file 'haskell-cabal-get-field-1)
(buffer-file-name)))))
(set-visited-file-name (expand-file-name "test-data/Source.hs" scriptDir) t t))
(should (equal "Simple"
(haskell-cabal-guess-setting "build-type")))))
(haskell-cabal-get-field "build-type")))))

(ert-deftest haskell-cabal-compute-checksum-1 ()
(let ((scriptDir
(file-name-directory
(or (symbol-file 'haskell-cabal-guess-setting-1)
(or (symbol-file 'haskell-cabal-get-field-1)
(buffer-file-name)))))

(should (equal "263e67082326a27585639420f4d42c8b"
Expand All @@ -38,7 +38,7 @@
(ert-deftest haskell-cabal-compute-next-prev-section-1 ()
(let ((scriptDir
(file-name-directory
(or (symbol-file 'haskell-cabal-guess-setting-1)
(or (symbol-file 'haskell-cabal-get-field-1)
(buffer-file-name)))))

(with-temp-buffer
Expand Down