nim-lang / nim-mode

An emacs major mode for the Nim programming language
137 stars 46 forks source link

How to set my own comamnd line options for `nim-compile` #231

Open halloleo opened 3 years ago

halloleo commented 3 years ago

I try to set my own command line options for nim-compile as file local variables (like ("js" "-r" "--verbosity:0" "--hint[Processing]:off" "--excessiveStackTrace:on").

Only way I found to do this is setting nim-compile-default-command - despite the fact that this variable has the suffix -default-command, but the variable contains neither the full command nor is it the default any more.

Do I miss something? Or is this the recommended way?


I use Linux, emacs 26.2, nim-mode-20191219.847 and nim 1.2.6.

halloleo commented 3 years ago

Also, I think because nim-compile-default-command is not designed as a file-local variable I cannot add values to the safe-local-variable-values when opening a nim file.

krux02 commented 3 years ago

This is my configuration:

(defun arne--get-nim-compile-command ()
  "Calculate a good default compile command for Nim."
  (let ((koch-dir (locate-dominating-file buffer-file-name "koch.nim"))
        (nimble-dir (arne--locate-dominating-nimble-file)))
    (cond
     (koch-dir ;; in the compiler
      (concat
       "cd "
       (shell-quote-argument
        (file-relative-name koch-dir))
       (cond ;; is test file in the test folder?
        ((string-match "^tests/.*/t[^/]*.nims?$" (file-relative-name buffer-file-name koch-dir))
         "; testament/testament run ")
        ((string-match "nimsuggest/tests/t[^/]*.nim$" (file-relative-name buffer-file-name koch-dir))
         "; nimsuggest/tester ")
        (t
         "; nim c -r "))
       (shell-quote-argument
        (file-relative-name buffer-file-name koch-dir))))
     ((string-match "\\.nims$" buffer-file-name)
      (concat "nim e "
              (shell-quote-argument
               (file-relative-name buffer-file-name))))
     (t
      (concat "nim c -r "
              (shell-quote-argument
               (file-relative-name buffer-file-name)))))))

(defun arne--init-nim-mode ()
  "Define some settings for the nim mode.  Some ``imenu'' integration."
  (when buffer-file-name
    (setq-local compile-command (arne--get-nim-compile-command)))

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

The idea here is: arne--get-nim-compile-command implements some logic to determine a good default compile command. Then for compilation I am just using compile, not nim-compile. I never really understood the point of a distinct mode nim-compile when compile with a good default command works just fine. It also allows me to map compile and recompile to some keys globally and have this workflow work across programming languages.

The logic in arne--get-nim-compile-command is not perfect. But it doesn't need to be, it is just a guess that should do what I need.

halloleo commented 3 years ago

Thanks @krux02 for chiming in! :-) Will try your custom compile-command.

Normally I use compile and recompile for all sorts of stuff (like invoke tasks), but I thought if nim-mode has its own compile I might use it...