/usr/share/emacs/site-lisp/haskell-mode/haskell-session.el is in haskell-mode 13.10-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | ;;; haskell-session.el --- Haskell sessions
;; Copyright (C) 2011-2012 Chris Done
;; Author: Chris Done <chrisdone@gmail.com>
;; This file is not part of GNU Emacs.
;; This file is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 3, or (at your option)
;; any later version.
;; This file is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING. If not, write to
;; the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
;; Boston, MA 02110-1301, USA.
;;; Commentary:
;;; Todo:
;;; Code:
(require 'haskell-cabal)
(require 'haskell-string)
(with-no-warnings (require 'cl))
(declare-function haskell-interactive-mode "haskell-interactive-mode" ())
(declare-function haskell-kill-session-process "haskell-process" (&optional session))
(declare-function haskell-process-start "haskell-process" (session))
(declare-function haskell-process-cd "haskell-process" (&optional not-interactive))
;; Dynamically scoped variables.
(defvar haskell-process-type)
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Globals
(defvar haskell-sessions (list)
"All Haskell sessions in the Emacs session.")
(defun haskell-session-tags-filename (session)
"Get the filename for the TAGS file."
(concat (haskell-session-cabal-dir session) "/TAGS"))
;;;###autoload
(defun haskell-session-all-modules (&optional dontcreate)
"Get all modules -- installed or in the current project.
If DONTCREATE is non-nil don't create a new session."
(append (haskell-session-installed-modules dontcreate)
(haskell-session-project-modules dontcreate)))
;;;###autoload
(defun haskell-session-installed-modules (&optional dontcreate)
"Get the modules installed in the current package set.
If DONTCREATE is non-nil don't create a new session."
;; TODO: Again, this makes HEAVY use of unix utilities. It'll work
;; fine in Linux, probably okay on OS X, and probably not at all on
;; Windows. Again, if someone wants to test on Windows and come up
;; with alternatives that's OK.
;;
;; Ideally all these package queries can be provided by a Haskell
;; program based on the Cabal API. Possibly as a nice service. Such
;; a service could cache and do nice things like that. For now, this
;; simple shell script takes us far.
;;
;; Probably also we can take the code from inferior-haskell-mode.
;;
;; Ugliness aside, if it saves us time to type it's a winner.
;;
;; FIXME/TODO: add support for (eq 'cabal-repl haskell-process-type)
(require 'haskell-process) ; hack for accessing haskell-process-type
(let ((modules (shell-command-to-string
(format "%s | %s | %s"
(if (eq 'cabal-dev haskell-process-type)
(if (or (not dontcreate) (haskell-session-maybe))
(format "cabal-dev -s %s/cabal-dev ghc-pkg dump"
(haskell-session-cabal-dir (haskell-session)))
"echo ''")
"ghc-pkg dump")
"egrep '^(exposed-modules: | )[A-Z]'"
"cut -c18-"))))
(split-string modules)))
(defun haskell-session-project-modules (&optional dontcreate)
"Get the modules of the current project.
If DONTCREATE is non-nil don't create a new session."
(if (or (not dontcreate) (haskell-session-maybe))
(let* ((session (haskell-session))
(modules
(shell-command-to-string
(format "%s && %s"
(format "cd %s" (haskell-session-cabal-dir session))
;; TODO: Use a different, better source. Possibly hasktags or some such.
;; TODO: At least make it cross-platform. Linux
;; (and possibly OS X) have egrep, Windows
;; doesn't -- or does it via Cygwin or MinGW?
;; This also doesn't handle module\nName. But those gits can just cut it out!
"egrep '^module[\t\r ]+[^(\t\r ]+' . -r -I --include='*.*hs' --include='*.hsc' -s -o -h | sed 's/^module[\t\r ]*//' | sort | uniq"))))
(split-string modules))))
(defun haskell-session-kill (&optional leave-interactive-buffer)
"Kill the session process and buffer, delete the session.
0. Prompt to kill all associated buffers.
1. Kill the process.
2. Kill the interactive buffer.
3. Walk through all the related buffers and set their haskell-session to nil.
4. Remove the session from the sessions list."
(interactive)
(let* ((session (haskell-session))
(name (haskell-session-name session))
(also-kill-buffers (y-or-n-p (format "Killing `%s'. Also kill all associated buffers?" name))))
(haskell-kill-session-process session)
(unless leave-interactive-buffer
(kill-buffer (haskell-session-interactive-buffer session)))
(loop for buffer in (buffer-list)
do (with-current-buffer buffer
(when (and (boundp 'haskell-session)
(string= (haskell-session-name haskell-session) name))
(setq haskell-session nil)
(when also-kill-buffers
(kill-buffer)))))
(setq haskell-sessions
(remove-if (lambda (session)
(string= (haskell-session-name session)
name))
haskell-sessions))))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Finding/clearing the session
;; Used internally
(defvar haskell-session)
;;;###autoload
(defun haskell-session-maybe ()
"Maybe get the Haskell session, return nil if there isn't one."
(if (default-boundp 'haskell-session)
haskell-session
(setq haskell-session nil)))
;;;###autoload
(defun haskell-session ()
"Get the Haskell session, prompt if there isn't one or fail."
(or (haskell-session-maybe)
(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."
(when (and (buffer-file-name)
(consp haskell-sessions))
(reduce (lambda (acc a)
(if (haskell-is-prefix-of (haskell-session-cabal-dir a)
(file-name-directory (buffer-file-name)))
(if acc
(if (and
(> (length (haskell-session-cabal-dir a))
(length (haskell-session-cabal-dir acc))))
a
acc)
a)
acc))
haskell-sessions
:initial-value nil)))
(defun haskell-session-new ()
"Make a new session."
(let ((name (read-from-minibuffer "Project name: " (haskell-session-default-name))))
(when (not (string= name ""))
(haskell-session-make name))))
(defun haskell-session-default-name ()
"Generate a default project name for the new project prompt."
(let ((file (haskell-cabal-find-file)))
(or (when file
(downcase (file-name-sans-extension
(file-name-nondirectory file))))
"haskell")))
(defun haskell-session-assign (session)
"Set the current session."
(set (make-local-variable 'haskell-session) session))
(defun haskell-session-choose ()
"Find a session by choosing from a list of the current sessions."
(when haskell-sessions
(let* ((session-name (ido-completing-read
"Choose Haskell session: "
(mapcar 'haskell-session-name haskell-sessions)))
(session (find-if (lambda (session)
(string= (haskell-session-name session)
session-name))
haskell-sessions)))
session)))
(defun haskell-session-clear ()
"Clear the buffer of any Haskell session choice."
(set (make-local-variable 'haskell-session) nil))
(defun haskell-session-change ()
"Change the session for the current buffer."
(interactive)
(haskell-session-clear)
(haskell-session-assign (or (haskell-session-new-assume-from-cabal)
(haskell-session-choose)
(haskell-session-new))))
(defun haskell-session-strip-dir (session file)
"Strip the load dir from the file path."
(let ((cur-dir (haskell-session-current-dir session)))
(if (> (length file) (length cur-dir))
(if (string= (substring file 0 (length cur-dir))
cur-dir)
(replace-regexp-in-string
"^[/\\]" ""
(substring file
(length cur-dir)))
file)
file)))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Building the session
(defun haskell-session-make (name)
"Make a Haskell session."
(let ((session (set (make-local-variable 'haskell-session)
(list (cons 'name name)))))
(add-to-list 'haskell-sessions session)
(haskell-process-start session)
session))
;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
;; Accessing the session
(defun haskell-session-name (s)
"Get the session name."
(haskell-session-get s 'name))
(defun haskell-session-interactive-buffer (s)
"Get the session interactive buffer."
(let ((buffer (haskell-session-get s 'interactive-buffer)))
(if (and buffer (buffer-live-p buffer))
buffer
(let ((buffer (get-buffer-create (format "*%s*" (haskell-session-name s)))))
(haskell-session-set-interactive-buffer s buffer)
(with-current-buffer buffer
(haskell-interactive-mode)
(haskell-session-assign s))
(switch-to-buffer-other-window buffer)
buffer))))
(defun haskell-session-set-interactive-buffer (s v)
"Set the session interactive buffer."
(haskell-session-set s 'interactive-buffer v))
(defun haskell-session-set-process (s v)
"Set the session process."
(haskell-session-set s 'process v))
;;;###autoload
(defun haskell-session-process (s)
"Get the session process."
(haskell-session-get s 'process))
(defun haskell-session-set-cabal-dir (s v)
"Set the session cabal-dir."
(let ((true-path (file-truename v)))
(haskell-session-set s 'cabal-dir true-path)
(haskell-session-set-cabal-checksum s true-path)))
(defun haskell-session-set-current-dir (s v)
"Set the session current directory."
(let ((true-path (file-truename v)))
(haskell-session-set s 'current-dir true-path)))
(defun haskell-session-set-cabal-checksum (s cabal-dir)
"Set the session checksum of .cabal files"
(haskell-session-set s 'cabal-checksum
(haskell-cabal-compute-checksum cabal-dir)))
(defun haskell-session-current-dir (s)
"Get the session current directory."
(let ((dir (haskell-session-get s 'current-dir)))
(or dir
(haskell-process-cd t))))
(defun haskell-session-cabal-dir (s)
"Get the session cabal-dir."
(let ((dir (haskell-session-get s 'cabal-dir)))
(if dir
dir
(let ((set-dir (haskell-cabal-get-dir)))
(if set-dir
(progn (haskell-session-set-cabal-dir s set-dir)
set-dir)
(haskell-session-cabal-dir s))))))
(defun haskell-session-get (session key)
"Get the SESSION's KEY value.
Returns nil if KEY not set."
(cdr (assq key session)))
(defun haskell-session-set (session key value)
"Set the SESSION's KEY to VALUE.
Returns newly set VALUE."
(let ((cell (assq key session)))
(if cell
(setcdr cell value) ; modify cell in-place
(setcdr session (cons (cons key value) (cdr session))) ; new cell
value)))
(provide 'haskell-session)
;; Local Variables:
;; byte-compile-warnings: (not cl-functions)
;; End:
;;; haskell-session.el ends here
|