Klimi's new dotfiles with stow.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

117 lines
5.0 KiB

пре 4 година
  1. ;;; haskell-modules.el --- -*- lexical-binding: t -*-
  2. ;; Copyright (c) 2014 Chris Done. All rights reserved.
  3. ;; This file is free software; you can redistribute it and/or modify
  4. ;; it under the terms of the GNU General Public License as published by
  5. ;; the Free Software Foundation; either version 3, or (at your option)
  6. ;; any later version.
  7. ;; This file is distributed in the hope that it will be useful,
  8. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. ;; GNU General Public License for more details.
  11. ;; You should have received a copy of the GNU General Public License
  12. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  13. ;;; Code:
  14. (require 'haskell-sort-imports)
  15. (require 'haskell-align-imports)
  16. (require 'haskell-session)
  17. (require 'haskell-navigate-imports)
  18. (require 'haskell-complete-module)
  19. (require 'haskell-sandbox)
  20. (require 'haskell-customize)
  21. (defun haskell-add-import (&optional module)
  22. "Add an import to the import list. Sorts and aligns imports,
  23. unless `haskell-stylish-on-save' is set, in which case we defer
  24. to stylish-haskell."
  25. (interactive)
  26. (save-excursion
  27. (goto-char (point-max))
  28. (haskell-navigate-imports)
  29. (insert (haskell-import-for-module
  30. (or module
  31. (haskell-complete-module-read
  32. "Module: "
  33. (haskell-session-all-modules (haskell-modules-session))))))
  34. (unless haskell-stylish-on-save (haskell-sort-imports)
  35. (haskell-align-imports))))
  36. (defun haskell-import-for-module (module)
  37. "Get import statements for the given module."
  38. (let ((mapping (assoc module haskell-import-mapping)))
  39. (if mapping
  40. (cdr mapping)
  41. (concat (read-from-minibuffer "Import line: "
  42. (format "import %s" module))
  43. "\n"))))
  44. ;;;###autoload
  45. (defun haskell-session-installed-modules (_session &optional _dontcreate)
  46. "Get the modules installed in the current package set."
  47. ;; TODO: Again, this makes HEAVY use of unix utilities. It'll work
  48. ;; fine in Linux, probably okay on OS X, and probably not at all on
  49. ;; Windows. Again, if someone wants to test on Windows and come up
  50. ;; with alternatives that's OK.
  51. ;;
  52. ;; Ideally all these package queries can be provided by a Haskell
  53. ;; program based on the Cabal API. Possibly as a nice service. Such
  54. ;; a service could cache and do nice things like that. For now, this
  55. ;; simple shell script takes us far.
  56. ;;
  57. ;; Probably also we can take the code from inferior-haskell-mode.
  58. ;;
  59. ;; Ugliness aside, if it saves us time to type it's a winner.
  60. ;;
  61. ;; FIXME/TODO: add support for (eq 'cabal-repl (haskell-process-type))
  62. (let ((session (haskell-session-maybe)))
  63. (when session
  64. (let ((modules (shell-command-to-string
  65. (format "%s 2> /dev/null | %s | %s"
  66. (cond
  67. ((haskell-sandbox-exists-p session)
  68. (concat "ghc-pkg dump -f "
  69. (shell-quote-argument (haskell-sandbox-pkgdb session))))
  70. (t "ghc-pkg dump"))
  71. "egrep '^(exposed-modules: | )[A-Z]'"
  72. "cut -c18-"))))
  73. (split-string modules)))))
  74. ;;;###autoload
  75. (defun haskell-session-all-modules (session &optional dontcreate)
  76. "Get all modules -- installed or in the current project.
  77. If DONTCREATE is non-nil don't create a new session."
  78. (append (haskell-session-installed-modules session dontcreate)
  79. (haskell-session-project-modules session dontcreate)))
  80. ;;;###autoload
  81. (defun haskell-session-project-modules (session &optional dontcreate)
  82. "Get the modules of the current project.
  83. If DONTCREATE is non-nil don't create a new session."
  84. (if (or (not dontcreate) (haskell-session-maybe))
  85. (let* ((modules
  86. (shell-command-to-string
  87. (format "%s && %s"
  88. (format "cd %s" (haskell-session-cabal-dir session))
  89. ;; TODO: Use a different, better source. Possibly hasktags or some such.
  90. ;; TODO: At least make it cross-platform. Linux
  91. ;; (and possibly OS X) have egrep, Windows
  92. ;; doesn't -- or does it via Cygwin or MinGW?
  93. ;; This also doesn't handle module\nName. But those gits can just cut it out!
  94. "egrep '^module[\t\r ]+[^(\t\r ]+' . -r -I --include='*.*hs' --include='*.hsc' -s -o -h | sed 's/^module[\t\r ]*//' | sort | uniq"))))
  95. (split-string modules))))
  96. (defun haskell-modules-session ()
  97. "Get the `haskell-session', throw an error if it's not
  98. available."
  99. (or (haskell-session-maybe)
  100. (haskell-session-assign
  101. (or (haskell-session-from-buffer)
  102. (haskell-session-choose)
  103. (error "No session associated with this buffer. Try M-x haskell-session-change or report this as a bug.")))))
  104. (provide 'haskell-modules)