Emacs hangs in haskell-mode with inferior-haskell-load-file call - haskell

When in a Haskell file, I use C-c C-l to run the command inferior-haskell-load-file which is intended to load the current file into the GHCI interpreter but Emacs just hangs until I hit C-g. Anyone know how I can get this to work?
GNU Emacs 23.3.1 (x86_64-pc-linux-gnu, GTK+ Version 2.24.5) of 2011-08-14 on allspice, modified by Debian
Using haskell-mode version v2.7.0

inferior-haskell-mode does some parsing based upon the expected ghci prompt. As such, if you change the prompt in a .ghci file, then it can't detect it.
For more information, see where haskell-ghci.el sets the comint-prompt-regexp value to determine what a prompt is.
;; GHCi prompt should be of the form `ModuleName> '.
(setq comint-prompt-regexp
"^\\*?[[:upper:]][\\._[:alnum:]]*\\( \\*?[[:upper:]][\\._[:alnum:]]*\\)*> ")
If you want to keep the setting in your .ghci file, then it may be possible to customise this settings.

Old question, but as I just ran into this today, I wanted to share how to actually customize comint-prompt-regexp since I had to figure it out.
This customization will recognize λ> prompts, or actually any single character before >), but it doesn't break the existing regex. In your .emacs:
(load-library "inf-haskell")
(defun my-inf-haskell-hook ()
(setq comint-prompt-regexp
(concat comint-prompt-regexp "\\|^.> ")))
(add-to-list 'inferior-haskell-mode-hook 'my-inf-haskell-hook)
You can add more dots to "\\|^.> " to recognize a longer prompt, but I wanted to keep it fixed-length for simplicity.

I had a similar problem caused by GHCi reporting some kind of error on startup, causing the Emacs haskell mode to wait for the GHCi prompt indefinitely (GHCi didn't show the standard prompt (Prelude>), but rather just showed >). You can try running GHCi externally and see if it reports any errors.

I got the same error message when I tried to use stack with emacs. For me, adding this line to my .emacs/init.el resolved the problem:
(setq haskell-program-name "stack ghci")

Related

tryhaskell.org does not seem to support GHCi commands

When I use GHCi commands (i.e. any command starting with ":" for example :set +t) on https://tryhaskell.org/ I get an error:
:1:1: parse error on input ‘:’
According to the documentation of GHCi this should work, but I cannot find much information about it.
I've tried a few other online "repls" as well.
That repl probably doesn't try to provide all of GHCI's functionality. It probably wants to be stateless, so it just evaluates expressions one at a time. No variable definitions, no interacting with GHCI any other way, e.g. through :t. If you install GHCI yourself, you will have a functioning repl. Or, if you want an online repl, https://replit.com/languages/haskell seems to work fine for me.

How to switch to Haskell Interactive and reload last line in emacs?

Is there a command available to load the the current buffer to the haskell process and execute the previous line I introduced in the ghci?
I am trying to make a function for this, but I get "Unexpected response from haskell process." because of this line: haskell-interactive-mode-history-previous
(defun reload-haskell-last-line () (interactive)
"blablabla"
(haskell-process-load-file)
(haskell-interactive-switch)
(haskell-interactive-mode-history-previous)
(haskell-interactive-mode-return)
)
This can be super handy for developing.
If this is not the right way, what do you usually use?
Atom haskell-ide repl has a similar feature, that I would like to replicate in emacs

Haskell does not return from getLine

For some reason, Haskell on my machine does never return from any getLine call. For instance, I tried to run the following code straight from Learn You a Haskell for Great Good:
main = do
putStrLn "Hello, what's your name?"
name <- getLine
putStrLn ("Hey " ++ name ++ ", you rock!")
When I run it, the first line is printed, and I see my input when I type a name, however when I press Enter the program just blocks there and never prints the final line.
How should I fix this?
edit: I am running it from the Sublime IDE, maybe that has something to do with it
After doing a quick search on how Sublime runs programs, I found a youtube video (edit: and this SO post) which says that Sublime's "run program" functionality can only show output and isn't capable of reading input.
So it looks like you'll have to run your program from the command line or from within GHCi using :main. The latter might be the most convenient as Sublime actually supports a GHCi tab, so you can still do everything from within Sublime.
This seems to be a limitation in Sublime's Build command (assuming that this is what you're using).
Sublime executes the script using runhaskell, but apparently, it doesn't capture STDIN (which makes kind of sense - build results are usually read-only and not an interactive session).
Workaround: run your script from the command line with
runhaskell script.hs
and everything works as expected

Prevent gVim from returning control to command line (when called from Stata)

When I call gVim from Stata with shell (or equivalently with !) Stata doesn't wait for the command to finish and continues on with the .do file. I usually specify a short sleep and everything works great (discussed on the Stata mailing list here).
But sometimes the gVim call is lengthy and the length is unknown a priori. For example. I use gVim's argdo to remove headers from a folder of text files.
!gvim -c "argdo 1,3d | update" *sheet*.txt
Is there any way that I can force gVim to not return control to Stata? Or are my best options to complete this step outside the .do file or with a pause/lengthy sleep? Thanks!
Oh, I'm on Win 8 (64 bit) with gVim 7.3.
I think you would need to make this call a Stata command or the equivalent thereof.
That is, try running this separately from a do-file editor window or as wrapped up in a separate do-file.
I realise that is not an attractive solution, but in principle it seems the only one.
(sleep solutions I dislike as fudges, but I guess no one likes them on principle.)

How to open files in web browsers (e.g Firefox) within editors like vim or emacs?

How to open files in browsers (e.g Firefox) within editors like vim or emacs? Notepad++ open files in browsers by pressing Ctrl+Shift+Alt+X (Firefox). Is there a way to do this in gVim or Emacs?
browse-url-of-file is an interactive compiled Lisp function in
`browse-url.el'.
It is bound to <menu-bar> <HTML> <Load this Buffer in Browser>, C-c
C-z v.
(browse-url-of-file &optional file)
Ask a WWW browser to display file.
Display the current buffer's file if file is nil or if called
interactively. Turn the filename into a URL with function
browse-url-file-url. Pass the URL to a browser using the
browse-url function then run browse-url-of-file-hook.
In emacs I don't think this is built in, I may be wrong, but if not here is a function to do it:
(defun open-in-browser()
(interactive)
(let ((filename (buffer-file-name)))
(browse-url (concat "file://" filename))))
For whatever reason, my EmacsW32 on WinXP install kept sending browse-url directives to shell with "open file:// alone, and that didn't work so well*. Cutting it off at the knees, and modifying justin's original as below worked for me:
(defun open-in-browser()
"open buffer in browser, unless it is not a file. Then fail silently (ouch)."
(interactive)
(if (buffer-file-name)
(let ((filename (buffer-file-name)))
(shell-command (concat "start firefox.exe \"file://" filename "\"")))))
Needs some improvement. As well as replacement of your favorite browser. d**n you, hard-coding.
* I think the problem was the system-type check in browse-url-default-windows-browser, but not positive.
In gVim:
:!start cmd /c "C:\Users\pierre\AppData\Local\Google\Chrome\Application\chrome.exe" file:///"%:p""
You need the file:// URI to indicate that it is from the file system, this will work with all browsers. %:p produces the full file path for the current file. The quotes are necessary.
Simply map that to whatever you choose. You may need to do set shell=cmd.exe if you've set your shell to bash or something else.
In emacs (quoting justinhj):
(defun open-in-browser()
(interactive)
(let ((filename (buffer-file-name)))
(browse-url (concat "file://" filename))))
You mean you'd like to open the file currently being edited in a web browser?
In Vim, use something like :!firefox %.
Edit: You could, in fact, use nmap <silent> <C-M-X> :!firefox %<CR> to cause Vim to act very much like Notepad++ (though this mapping won't care whether you press shift or not).
Note that not every browser will actually render the file's contents when given the filename on the command line; e.g. Google Chrome will open a "save as" dialogue instead, as if you were downloading the file in question. Look up your browser's docs if in doubt. Firefox will 'just work', though.
Depending what you want to do with this, you might consider Emacs + MozRepl, which basically lets you send javascript commands to Firefox via telnet. Unfortunately I can't seem to write the elisp to make this work, but a related trick for reloading webpages from within emacs is shown by Sard in What's in your .emacs?. More information on integrating emacs and mozrepl from the original source and also here for a cool trick that updates the page in the browser as you type in the emacs buffer - it's pretty nice for getting instant feedback when working with html.
I reckon the same thing would work with vim, but I've only used it in emacs.
I do it an Elisp function using shell command xdg-open.
Then I define a key in html-mode to call the function.
You've gotta be comfortable adding stuff to your .emacs file.
(defun open-html()
"Get the HTML file path & open it"
(interactive)
(let (html-file-path)
(setq html-file-path (buffer-file-name))
(shell-command (format "xdg-open '%s'" html-file-path)))
)
This answer is based on Emacs 26.2
Emacs has the functions for opening a file in a browser built in but the behavior on different platforms may be different. Looking into the source code and documentation of browse-url-of-file by entering C-h f browse-url-of-file you'll see that the variable browse-url-browse-function determines which browser is used. You can then customize this variable to use, say, Chrome, by choosing browse-url-chrome and then apply and save the change. To access the customization page either entering C-h f browse-url-browser-function and then selecting the customize hyperlink, or M-x customize then searching for browse-url-browser-function.

Resources