1

I am using pine-script-mode for TradingView Pine script.

When I follow https://emacs.stackexchange.com/a/17565/18414 I have applied:

(defun my-custom-settings-fn ()
  (setq indent-tabs-mode nil)  
  (setq tab-width 4)
  (setq indent-line-function 'insert-tab))

(add-hook 'pine-script-mode-hook 'my-custom-settings-fn)

Hence still TABs make 3 space inden. How can I force pine-script-mode to do indention as 4 space?

I have in the following code when cursor is at the beginning of SLEEP_BAR := 20 and tab applied 3 space is added :

example.pine file:

if timeframe.isseconds
SLEEP_BAR := 20  // when I applied TAB at the beginning of the line 3 spaces added
  • In the example.pine buffer, C-h v tab-width says 4
6
  • "when I applied tab here 3 spaces added": where is here exactly? At the beginning of the line, four spaces are added; if at the end of the line, two spaces are added because that's how many are needed to bring you to the next "tab stop". You have to describe the problem more precisely.
    – NickD
    Sep 16, 2022 at 17:07
  • 1
    In particular, in the example.pine buffer, what does C-h v tab-width say?
    – NickD
    Sep 16, 2022 at 18:51
  • @NickD tab-width says 4
    – alper
    Sep 17, 2022 at 12:07
  • Yes here == At the beginning of the line, but no matter what 3 spaces added instead of 4 :-(
    – alper
    Sep 17, 2022 at 12:09
  • Just out of curiosity, what's the value of indent-line-function? If you're getting 3 spaces instead of 4, it seems like indent-relative is still being used instead of insert-tab.
    – g-gundam
    Sep 17, 2022 at 14:41

1 Answer 1

1
+50

One way to completely sidestep emacs' indentation madness is to use .editorconfig files. In the root of the project where you have your PineScript files, create a .editorconfig file with the following content.

[*.pine]
indent_style = space
indent_size = 4

Then install the EditorConfig plugin for Emacs. The next time you open up a PineScript file in that project, indentation should work as you intended.


PS: What you originally had almost worked for me. I'm not sure what you're doing wrong, because when I used your config and pressed TAB after a line that started an if-block, it indented the next line using 4 spaces as intended. The only thing I'm doing differently is adding insert-tab to indent-line-ignored-functions, but that was to solve a different problem involving new lines.

;; https://emacs.stackexchange.com/q/73620/37580
(defun c/pinescript-indent ()
  "Just do dumb indentation rather than emacs' relative indentation."
  (setq indent-tabs-mode nil)
  (setq tab-width 4)  ; this can be omitted if standard-indent is 4
  (setq indent-line-function 'insert-tab))

(add-to-list 'indent-line-ignored-functions 'insert-tab)
(add-hook 'pine-script-mode-hook 'c/pinescript-indent)

Just to be clear, if you use EditorConfig, you don't need any of the above elisp.

3
  • Thanks it works! Can also indention be smart like if it is already indented inside a if block and I press TAB can we prevent additional indent to be added?
    – alper
    Sep 17, 2022 at 12:28
  • @alper - I'm not aware of any options to enable 'smart' indentation like you described.
    – g-gundam
    Sep 17, 2022 at 14:23
  • As I remember it was emacswiki.org/emacs/SmartTabs , which works on other languages like python. But on pine with your solution tab keeps adding tabs rather than if the alignment is correct shouldn't add indent just toggle without indent setup, like python-indent-line-function does it
    – alper
    Sep 17, 2022 at 15:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.