Haskell autocompletion in Emacs using haskell-mode - haskell

I installed haskel-mode in emacs. Then I write in my .emacs:
(load "~/.emacs.d/haskell-mode/haskell-site-file")
(add-hook 'haskell-mode-hook 'turn-on-haskell-doc-mode)
(add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
(add-hook 'haskell-mode-hook 'haskell-font-lock-symbols t)
(put 'downcase-region 'disabled nil)
What must I add in my conf file that emacs could autocomplete for Haskell? Or Haskell mode there is no such possibility?

When there is no language-specific support, you can use tags. This is a generic completion mechanism.
Generate a TAGS file, which contains a list of identifiers and where they are defined. Emacs comes with the etags program to do this in many languages, but not Haskell; ghc comes with hasktags.
Load the TAGS file with M-x visit-tags-table.
Tags are not context-dependent, so they'll indiscriminately suggest types, values, constructors, etc everywhere. They also won't provide advanced features such as easily showing the type of a value. The most important tags commands are:
M-TAB (complete-symbol) completes an identifier according to the loaded list of tags.
M-. (find-tag) goes to the place where the identifier at point is defined, opening the containing file if necessary.
M-* (pop-tag-mark) goes back where you were before M-..
M-x tags-apropos shows a list of identifiers matching a regexp.
For more information, look under "Tags" in the Emacs manual.
For an even cruder, but fully automatic mechanism, there is the dynamic abbrev feature. C-M-/ (dabbrev-completion) looks in most open buffers for a completion; this is completely language-independent, so it'll even find words in strings, comments, whatever. M-/ (dabbrev-expand) is similar, but directly completes to the nearest match before point.

ghc-mod provides some completion for Haskell within Emacs, as well as checking with hlint and ghc. In combination with M-/, it's good enough for me.

haskell-mode currently provides no such possibility. There is some work on implementation of haskell parser for CEDET - in this case, users will get autocompletion features automatically. But this work had started not so much time ago...

My setup is a little more complicated. It uses the auto-complete infrastructure which
shows a dropdown list of candidates automatically similar to traditional IDEs. I downloaded this script that hardcodes all the keywords. In addition to that, I use ghc-mod to generate module names.

As a "cheap and cheerful" autocompletion mechanism, don't overlook M-/. It's completely heuristic and language-independent, but surprisingly effective.

Besides autocompletion for your own code, you can also get autocompletion (with apidocs even) for the standard library, import names, and pragma names using company-ghc. I found this guide to be very helpful. Note, I didn't get it to work fully for myself yet, but I can feel I'm close :-)

Related

Haskell Guards and SublimeText 3

I switched over to Sublime Text 3 but now that I was coding some Haskell in ST3 I noticed something quite odd, which is the syntax highlighting logic for guards.
As you can see, when I write it this way, it highlights the first guard in white colour and the different sign in a mix of white/magenta:
Only when I use this wrong syntax (with an equal sign after the argument) it displays correctly.
Does anyone know how to fix this?
You're probably using the default Haskell syntax highlighting. I would recommend disabling the Haskell package and installing SublimeHaskell. Its syntax highlighting is much better, and it recognizes things like otherwise as being a "built-in" (it's mainly Prelude functions that are considered built-in).
If you're using the built-in Haskell highlighting, you can check that it's buggy by using the CtrlAltShiftP shortcut. Highlight each guard pipe individually and then hit this shortcut. In the status bar it'll briefly show the syntax scope names associated with the region. For the first pipe, you'll get source.haskell meta.function.type-declaration.haskell, and for the second you'll get source.haskell keyword.operator.haskell. Using SublimeHaskell's syntax you'll get source.haskell keyword.operator.haskell for both pipes. I won't say that SublimeHaskell's is perfect (try indenting an entire file after module Name where), but it's definitely better. Since the syntaxes have the same name and because SublimeHaskell comes with snippets and whatnot that cover everything that the built-in does, I recommend disabling the Haskell plugin and only leaving SublimeHaskell's syntax selectable.
(NOT SURE!!!)
I now believe this is not a bug, instead I believe this is actually ST3's way of telling you you have non-exaustive patterns in that function.
Non-exaustive: http://i.imgur.com/74o4sgp.png
Exaustive: http://i.imgur.com/M9a4TTL.png

Code navigation in vim and emacs

The IDE feature that I miss the most in emacs and vim are code navigation and find usages. Both of these editor have the following similar features for them:
Tags - identifiers from specified files are indexed and when you press shortcut on a word which is an identifier, you will be navigated there
CScope - it allows you to navigate to "usages" of an identifier
As far as I understand, both of these systems are very imprecise. If we have similar identifiers with the same name both tags and scope might mix them up. Are there any better alternatives and how precise they really are?
I use cscope and semantic in Emacs. It is just enough for me.
In cscope the two function I heavily used are cscope-find-global-definition and cscope-find-this-symbol. The previous function is quite precise.
C-c s s Find symbol.
C-c s d Find global definition.
As for semantic(dynamic index, do not need to generate TAGS).
(global-set-key [f8] 'semantic-ia-fast-jump) ;; jump to definition.
(global-set-key [S-f8] ;; jump back
(lambda ()
(interactive)
(if (ring-empty-p (oref semantic-mru-bookmark-ring ring))
(error "Semantic Bookmark ring is currently empty"))
(let* ((ring (oref semantic-mru-bookmark-ring ring))
(alist (semantic-mrub-ring-to-assoc-list ring))
(first (cdr (car alist))))
(if (semantic-equivalent-tag-p (oref first tag)
(semantic-current-tag))
(setq first (cdr (car (cdr alist)))))
(semantic-mrub-switch-tags first))))
GNU Global, for example, allows duplicate identifiers, and you'll be able to select needed.
There are many programs that output ctags-compatible tags files. They are often language-specific because of some low level limitations in ctags but neither they nor ctags help you when you have multiple methods with the same name because these tools only do the indexing part of the job. Searching through the index is actually Vim's job and, because it is a freaking text editor and not an IDE it has zero mean to decide which method declaration is the right one.
Fortunately, Vim shows an actionable list when there are multiple hits. But that's how far you can go.
Cscope is a little smarter than ctags and, when used from Vim, does the searching as well as the indexing. But, like ctags, cscope is still a code indexer.
It's actually possible to use both at the same time with set cscopetags but it won't help with your naming issue.
You could try GNU Global. But it doesn't support JavaScript so I've never really used it long enough to make an opinion.
IDEs usually do their magic through language-specific parsers/static analysis tools that run in the background against your code. Vim has at least one limitation and one feature that make it hard to even imagine it ever reaching the level of code awareness that you seem to be looking for:
Vim is not multithreaded. So it can't run a static analysis tool in the background.
Vim supports hundreds of languages. Providing that kind of feature for that many languages would be impossible for any organization of any size.
But none of this is a problem for me, because Vim is not an IDE.
And because I don't have multiple methods with the same name in my projects.
<C-]>, <C-w>}, :tag /foo<Tab> and :cs f c bar are enough for my humble needs.
See eclim which has a very usable emacs interface which supports the same level of code navigation for java as Eclipse.
Eclim also supoprts C/C++ among others, though support for this is not yet implemented in emacs-eclim, but if you know Elisp then it is pretty trivial to implement, because for java all the necessary infrastructure is already there, so you only need to add the implementation for the c++ calls.

Using an external syntax highlighter in vim

I have access to a syntax highlighting program for an internal-only language. Is there any way in which I could get vim to call this program to perform syntax highlighting? I figure that it's worth seeing if this is possible before I write my own syntax file, since it's quite complex (not to mention prone to change). The program in question could be quite easily customized to output in a new intermediate format (it's only currently outputting HTML), if that would make things easier.
I would suggest to write a quick throwaway script in the language of
your choice to convert the output of the program in question to the Vim
highlighting syntax. You mentioned the program is prone to change, but
its output format can be easily customized: using your own conversion
script (which parses the programs output format) you could easily stay
up-to-date with the latest changes (just run your script again). And as
others have mentioned: creating a new syntax file is really easy in Vim,
so it's up to the complexity of the programs output how hard this actually is.
Yes, you can use vim job/channels to communicate with external process and highlight text using textprops.
Type :help channel and :help textprop in vim to get more info.
This method is used to implement treesitter highlighting in vim-treesitter plugin

VimScript or VimL?

What is the correct name of the Vim scripting language? I see it being called VimScript, Vim script and even VimL.
VimL is even listed on GitHub as the 10th most popular programming language! What is the history behind the VimL name? Why are the Git folks calling it VimL?
After grepping through official documentation, it seems that "Vim script" is the "most" official name since nowhere is "VimScript" mentioned in regular sentences. GetLatestVimScripts is often mentioned, but this is a script name where no underlines are allowed.
I'd guess VimL is just an abbreviation for Vim Language (Vim script). It was probably coined some time ago and became used at few places, but it's not official.
This explains all terms used:
Vim’s built-in scripting language, VimL. This language is also known as Vimscript. Depending on how you look at it, either VimL is an alternate name for Vimscript or Vimscript is an alternate name for VimL.
Actually, there’s no real official name for the language; the closest seems to be the two-word “Vim script.” To better follow English naming conventions, this is usually altered to “Vimscript,” or more rarely, “Vim Script”—but all of this can be confusing, since the files which store code in this language are themselves called “Vim scripts.”
The relatively new name “VimL” (“Vim Language”) has been gaining in popularity in rough correlation with the growth of the code-sharing site GitHub. Its use is a matter of preference, but I do find it more easily distinguishable from mentions of Vim scripts or of writing generic scripts using Vim (in search results, for instance).
Read more - https://pragprog.com/book/bkviml/the-viml-primer
:h usr_41.txt says Vim script language for the language, and Vim script(s) for ..., well, scripts.
The abbreviation VimL sounds more correct to me.
Notice, that most of the opinions here suggesting VimL come from personal preference, observations of community behavior or references from good but still unofficial books. The truth is, whether we like it or not, and even when grammatical incorrect, the official name should be the one picked and used by Vim's original author, Bram Moolenaar; and he seems to always use Vim script.
For e.g. look at it being referred to as "Vim script" in the readme for his Vim9 repo —
https://github.com/brammool/vim9

Which editors out of Emacs, Vim and JEdit support multiple simultaneous text insertion points?

Background: JEdit (and some other text editors as well) support a feature called Multiple simultaneous text insertion points. (at least that's what I'm calling it here).
To understand what this means, take a look at the link.
Out of all the features in use in modern text editors, initial research seems to indicate that this is one feature that both Emacs and Vim do not actually support. If correct, this would be pretty exceptional since it's quite difficult to find a text editor feature that has not made its way into at least one of these two old-school editors.
Question: Has anyone ever seen or implemented this feature in either Emacs, Vim, or both? If so, please point me to a link, script, reference or summary that explains the details.
If you know an alternate way to do the same (or similar) thing, please let me know.
The vim way to do this is the . command which repeats the last change. So, for instance, if I change a pointer to a reference and I have a bunch of
obj->func
that I want to change to
obj.func
then I search for obj->, do 2cw to change the obj-> to obj., then do n.n.n. until all the instances are changed.
Perhaps not a flexible as what you're talking about, but it works frequently and is very intuitive and fast when it does.
moccur-edit.el almost does what you want. All the locations matching the regexp are displayed, and the editing the matches makes changes in the corresponding source. However, the editing is done on a single instance of the occurrence.
I imagine it'd be straight forward to extend it to allow you to edit them all simultaneously (at least in the simple case).
There is a demo of it found here.
Turns out, the newest versions of moccur-edit don't apply changes in real-time - you must apply the changes. The changes are also now undoable (nice win).
In EMACS, you could/would do it with M-x find-grep and a macro. If you really insist that it be fully automatic, then you'd include the find-next in the macro.
But honestly, this strikes me as a sort of Microsoft-feature: yes, it adds to the feature list, but why bother? And would you remember it existed in six months, when you want to use it again?
For emacs, multiple-cursors does exactly that.
Have a look at emacsrocks episode 13, by the author of the module.
I don't think this feature has a direct analogue in either Emacs or Vim, which is not to say that everything achievable with this feature is not possible in some fashion with the two 'old-school' editors. And like most things Emacs and Vim, power-users would probably be able to achieve such a task exceedingly quickly, even if mere mortals like myself could spend five minutes figuring out the correct grep search and replace with appropriate back-references, for example.
YASnippet package for Emacs uses it. See 2:13 and 2:44 in the screencast.
Another slight similarity: In Emacs, the rectangle editing features provided by cua-selection-mode (or cua-mode) automatically gives you multiple insertion points down the left or right edge of the marked rectangle, so that you can type a common prefix or suffix to all of those lines.
e.g.:
M-x cua-selection-mode RET (enable the global minor mode, if you don't already use this or cua-mode)
C-RET down down down (marks a 1x3 character rectangle)
type prefix here
C-RET (unmark the rectangle to return to normal editing)
It should be something like this in vim:
%s/paint.\((.*),/\1.paint(/
Or something like that, I am really bad at "mock" regular expressions.
The idea is substitute the pattern:
/paint(object,/
with
/object.paint(/
So, yes, it is "supported"
It seemed simple to do a basic version of this in Emacs lisp. This is for when you just want two places to insert text in parallel:
(defun cjw-multi-insert (text)
"insert text at both point and mark"
(interactive "sText:")
(insert-before-markers text)
(save-excursion
(exchange-point-and-mark)
(insert-before-markers text)))
When you run it, it prompts for text and inserts it at both point (current position) and mark. You can set the mark with C-SPC. This could be easily extended for N different positions. A function like set-insert-point would record current position (stored as an Emacs marker) into a list and then when you run the multi-insert command, it just iterates through the list adding text at each.
I'm not sure about what would a simple way to handle a more general "multi-editing" feature.
Nope. This would be quite difficult to do with a primarily console-based UI.
That said, there is similar features in vim (and emacs, although I've not used it nearly as much) - search and replace, as people have said, and more similarly, column insert mode: http://pivotallabs.com/users/brian/blog/articles/350-column-edit-mode-in-vi

Resources