• Stars
    star
    138
  • Rank 256,940 (Top 6 %)
  • Language
    Emacs Lisp
  • Created over 11 years ago
  • Updated 3 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

An emacs major mode for the Nim programming language

nim-mode A major mode for editing Nim source code

Travis CI MELPA MELPA Stable

This package provides (and requires Emacs 24.4 or higher version):

  • Syntax highlighting for *.nim, *.nims, *.nimble and nim.cfg files
  • nim-compile command (C-c C-c), with error matcher for the compile buffer
  • Nimsuggest (alpha):
    • on the fly linter using flycheck, or flymake (from Emacs 26)
    • auto-completion with company-mode ("C-M-i" for manual completion)
    • jump-to-definition (M-., and M-, keys)
    • find-references (M-? key)
    • eldoc or help on hover in term of LSP
  • Automatic indentation and line breaking (alpha)
  • Outline by procedures (hs-hide-all, hs-show-all etc.)

Installation

  • ensure packages from MELPA can be installed
  • Install nim-mode (e.g. M-x package-install RET nim-mode RET)

nimsuggest (alpha)

At the time of writing this it should be mentioned that both nimsuggest and nimsuggest-mode have problems that could cause emacs to be much less responsive, or even freeze. Apart from that is is non trivial to configure nimsuggest with the right parameters so that you also get correct results. So you have been warned.

Nimsuggest is the compilation server for Nim, it runs in its own process, pretty much independent of emacs. nimsuggest-mode is an emacs minor mode that comes with nim-mode. It is responsible to create the nimsuggest instance and connect emacs with it. nimsuggest-mode doesn't do anything visual in emacs yet. There are other minor modes such as flycheck,flymake (linting) and company (completion) that are responsible for editor integration.

  • flycheck and flymake are two alternative linting engines. Before emacs version 26.1 flymake was pretty much outdated and the recommended linting engine was the external flycheck. But from version 26.1 onward, flymake is a good linting engine that comes with emacs. But you should not use both at the same time.
  • flycheck-nimsuggest is a backend for flycheck. It builds the bridge to nimsuggest-mode so that flycheck can visualize the linting information that nimsuggest provides.
  • flycheck-nim is an alternative backend for flycheck that does not interact with nimsuggest at all. Instead it uses the nim check command and parses the output of that command.
  • flymake-nimsuggest is the backend for flymake to build the bridge to nimsuggest-mode. It comes with nim-mode, and it is activated automatically in nim files, when flymake-mode is on.
  • company-mode is a minor mode for auto completion (company - complete anything)
  • company-nimsuggest is the backend for company-mode that builds the bridge to nimsuggest-mode.
  • eldoc-mode is a minor mode for emacs that is responsible to show the documentation of emacs lisp symbols at point, hence the name. But eldoc-mode has been extended to work for other programming languages as well. nimsuggest-mode has integration for eldoc-mode so you can see documentation of nim symbols at point when nimsuggest-mode is active.

For nimsuggest-mode to work, emacs needs to be able to find the nimsuggest binary, when it is on the path, it should just work, if not you can customize nimsuggest-path. Since it is completely optional to use nimsuggest, you have to activate nimsuggest-mode manually.

Install nimsuggest

  1. Use stable version: See official download instruction at "Installation based on generated C code" section.

  2. Use latest version: This way may or may not work (depending on Nim or nimsuggest's state and we can't support all the way), so use above way if you prefer stable.

    #  assuming you already installed Nim
    cd /path/to/Nim_repository
    ./koch tools

Keyboard Shortcuts (with nimsuggest)

  1. Completion feature -- C-M-i and M-TAB keys and auto-complete feature if you install company-mode
  2. Jump to Definition -- M-. to find the definition for symbol at poisition and M-, to go back.
  3. Show Doc -- C-c C-d Show documentation of symbol at current position in the dedicated *nim-doc* buffer.
  4. Show Short Doc -- (automatically) Shows the short documentation of the symbol at point in the minibuffer

Grammar and Indentation

In nim-smie.el there are nim grammar rules for smie (Simple Minded Indentation Engine). These rules give emacs a basic understanding of the Nim grammar. They are used to calculate a "correct" indentation level for code, and to fill (distribute line endings at margin) comments, multiline strings and other parts of the code.

electric-indent-mode, a global minor mode that is turned on by default, uses the rules from nim-smie.el to automatically reindent the current line, before a new line is started on RET. The rules sometimes can really make the emacs behave sluggish up to freezing for several seconds. The problem is most noticeable when the grammar gets confused with incomplete statements or the grammar becomes very uncommon through the usage of untyped macros for embedded domain language. Just as an example writing patterns for the nim library ast-pattern-matching really confuses smie and you might have to manually fix a lot of indentation that electric-indent-mode breaks automatically.

My recommendation is to turn off electric indentation for Nim files. This can be done locally with (electric-indent-local-mode 0), or globally (not just Nim files) with (electric-indent-mode 0). Nim has semantic whitespace, therefore it might be better if the indentation is something that is inserted manually.

auto-fill-mode, a minor mode, uses the rules to break lines automatically. At the moment it is also not recommend to enable auto-fill-mode for Nim files. But using fill-paragraph (M-q) on comments does work reliably and it is very useful.

Example Configuration

You can copy and adjust the following configuration into your local init.el file.

;; The `nimsuggest-path' will be set to the value of
;; (executable-find "nimsuggest"), automatically.
(setq nimsuggest-path "path/to/nimsuggest")

(defun my--init-nim-mode ()
  "Local init function for `nim-mode'."

  ;; Just an example, by default these functions are
  ;; already mapped to "C-c <" and "C-c >".
  (local-set-key (kbd "M->") 'nim-indent-shift-right)
  (local-set-key (kbd "M-<") 'nim-indent-shift-left)

  ;; Make files in the nimble folder read only by default.
  ;; This can prevent to edit them by accident.
  (when (string-match "/\.nimble/" (or (buffer-file-name) "")) (read-only-mode 1))

  ;; If you want to experiment, you can enable the following modes by
  ;; uncommenting their line.
  ;; (nimsuggest-mode 1)
  ;; Remember: Only enable either `flycheck-mode' or `flymake-mode' at the same time.
  ;; (flycheck-mode 1)
  ;; (flymake-mode 1)

  ;; The following modes are disabled for Nim files just for the case
  ;; that they are enabled globally.
  ;; Anything that is based on smie can cause problems.
  (auto-fill-mode 0)
  (electric-indent-local-mode 0)
)

(add-hook 'nim-mode-hook 'my--init-nim-mode)

Other convenience packages for editing Nim source code

Those packages are convenience packages and can be installed same way as nim-mode (M-x list-packages ...)

  • indent-guide: show visible indent levels
  • quickrun: emacs port of vim's quickrun
  • company-mode: auto-complete feature
  • ob-nim: org-mode integration focused on Nim
  • wgrep: Writable grep buffer and apply the changes to files (maybe convenient for refactor stuff)
  • suggestion-box-el: show argument info on the cursor

Other editors/IDEs

You can also find other editor/IDE plugins for Nim language here

More Repositories

1

Nim

Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula. Its design focuses on efficiency, expressiveness, and elegance (in that order of priority).
Nim
16,144
star
2

nimble

Package manager for the Nim programming language.
Nim
1,225
star
3

nimforum

Lightweight alternative to Discourse written in Nim
Nim
744
star
4

c2nim

c2nim is a tool to translate Ansi C code to Nim. The output is human-readable Nim code that is meant to be tweaked by hand before and after the translation process.
Nim
489
star
5

packages

List of packages for Nimble
Nim
438
star
6

ui

Beginnings of what might become Nim's official UI library.
Nim
237
star
7

sdl2

Nim wrapper for SDL 2.x
Nim
222
star
8

langserver

The Nim language server implementation (based on nimsuggest)
Nim
177
star
9

RFCs

A repository for your Nim proposals.
134
star
10

fusion

Fusion is for now an idea about how to grow Nim's ecosystem without the pain points of more traditional approaches.
Nim
129
star
11

NimLime

Super Nim Plugin for Sublime Text 2/3
Nim
128
star
12

redis

Official redis wrapper for Nim.
Nim
126
star
13

bigints

BigInts for Nim
Nim
117
star
14

needed-libraries

This repository contains a list a needed libraries.
111
star
15

website

Code for the official Nim programming language website
HTML
109
star
16

opengl

An OpenGL wrapper for Nim
Nim
106
star
17

atlas

The Atlas Package cloner. It manages an isolated workspace that contains projects and dependencies.
Nim
91
star
18

nim-zmq

Nim ZMQ wrapper
Nim
62
star
19

threading

New atomics, thread primitives, atomic refcounting for --gc:arc/orc.
Nim
60
star
20

csources

The pre-generated C sources of the Nim compiler which aid in bootstrapping. This repository is archived because it's frozen, HEAD of csources can build Nim version 1 and any later version.
C
51
star
21

zip

zip wrapper for Nim
C
50
star
22

vscode-nim

A VS Code plugin for the Nim language
Nim
50
star
23

x11

x11 wrapper for Nim
Nim
47
star
24

iup

iup wrapper for Nim. Used to be part of the stdlib, now a Nimble package.
Nim
46
star
25

nightlies

Separate repository to trigger installer builds.
Shell
42
star
26

nimsuggest

idetools for the nim language
39
star
27

lua

Nim Wrapper to interface with the Lua interpreter
Nim
37
star
28

python

Nim wrapper for the Python 2 programming language
Nim
32
star
29

cairo

Nim Cairo wrapper.
Nim
31
star
30

gtk2

gtk2 wrapper for Nim
Nim
30
star
31

opencl

Low-level OpenCL wrapper for Nim
Nim
23
star
32

virus_checker

A virus checker for nim binaries
Nim
21
star
33

db_connector

Unified db connector in Nim
Nim
17
star
34

tcl

Nim Wrapper for the TCL programming language
Nim
16
star
35

csources_v1

CSources compiled from Nim version 1. Supports more CPU/OS combinations than the older csources repository.
C
15
star
36

niminst

EDIT: now archived, see https://github.com/nim-lang/Nim/issues/15946. niminst is a tool to generate an installer for a Nim program. Currently it can create an installer for Windows as well as installation/deinstallation scripts for UNIX. Some support for Linux' package management systems is also included.
Nim
15
star
37

assets

14
star
38

graphics

Graphics module for Nim. Currently based on SDL v1.2.
Nim
13
star
39

compilerdev

This repository contains a collection of documents about how to change/refactor the Nim compiler in order to make it faster, easier to maintain and have fewer bugs by a superior architecture and design. However, no every idea here will work out.
Nim
12
star
40

sat

A SAT solver written in Nim
Nim
12
star
41

punycode

Implements a representation of Unicode with the limited ASCII character subset in Nim.
Nim
12
star
42

wiki

11
star
43

checksums

Hash algorithms in Nim
Nim
11
star
44

standardjs

Wrappers for standardized JS modules.
Nim
10
star
45

ci_bench

A simple performance dashboard for the Nim language
Nim
10
star
46

smtp

SMTP client implementation, adapted from the Nim standard library
Nim
10
star
47

nimbot

The friendly, slightly sentient, Nim IRC bot.
Nim
10
star
48

pas2nim

pas2nim is a tool to translate Delphi/Pascal wrappers to Nim code.
Nim
10
star
49

mongo

MongoDB wrapper for Nim
Nim
9
star
50

dlls

Prebuilt DLLs and lib*.so files for Nim. Every DLL added here will also have at least a description about how we built it.
9
star
51

cgi

Helper procs for CGI applications in Nim.
Nim
8
star
52

dialogs

This module implements portable dialogs for Nim; the implementation builds on the GTK interface. On Windows, native dialogs are shown instead.
Nim
8
star
53

sdl1

SDL v1.2 wrapper for Nim.
Nim
7
star
54

oldwinapi

Old Win API wrapper for Nim.
Nim
7
star
55

csources_v2

CSources compiled from Nim version 2.
C
7
star
56

edutainment

Tasks related to making Nim more widely known such as video creation.
7
star
57

community_map

6
star
58

htmlparser

Parse a HTML document in Nim
Nim
6
star
59

graveyard

non-deprecated modules that have been removed from the Nim stdlib
Nim
5
star
60

security

Embargoed security issues that will be made public after a fix is made available. Use https://github.com/nim-lang/security/security
5
star
61

testspec

in progress
Nim
5
star
62

basic2d

Deprecated package from stdlib.
Nim
4
star
63

asyncftpclient

FTP client implementation, adapted from the Nim standard library
Nim
4
star
64

backport

backport requests
4
star
65

kickstart

Automated bootstrapping scripts for the Nim programming language compiler.
Shell
4
star
66

aporia-bin

Holds binaries for Aporia (mostly DLLs)
Shell
4
star
67

expat

Expat wrapper for Nim.
Nim
3
star
68

basic3d

Nim
3
star
69

joyent_http_parser

Nim
2
star
70

nim-buildbot

Buildbot Configuration and Script Files for Nim
Python
2
star
71

forum.nim-lang.org

Styles for forum.nim-lang.org. Powered by nimforum.
SCSS
2
star
72

libsvm_legacy

Nim
1
star