Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Auto-Enclosing Blocks #142

Open
ghost opened this issue Feb 3, 2018 · 2 comments
Open

Auto-Enclosing Blocks #142

ghost opened this issue Feb 3, 2018 · 2 comments

Comments

@ghost
Copy link

ghost commented Feb 3, 2018

Is there such a thing in lua-mode already, i couldn't find it anyway and i think it is nice if the blocks get an end inserted automatically, so if i type function or while and hit enter or space-bar the end gets inserted. I added this to my init.el file which does it for me:

 (defun put-block-end ()
  "put ends of blocks"
  (interactive)
  (when (and
     ;; in lua mode
     (derived-mode-p 'lua-mode)
     ;; enter or space
     (member (char-before) '(10 32)))    
    (when
    (save-excursion (backward-word)
		    (member (thing-at-point 'word)
			    '("do" "if" "for" "while" "function")))
      (if (= (char-before) 10)
      (progn			;enter
        (insert "end")
        (forward-line -1)
        (forward-word)
        (insert " "))
    (progn				;space
      (insert " " "end")
      (backward-char 4))))))

And then:

    (defun my-lua-mode-hook ()
      (add-hook 'post-self-insert-hook  'put-block-end))
    (add-hook 'lua-mode-hook 'my-lua-mode-hook)
@immerrr
Copy link
Owner

immerrr commented Apr 2, 2018

Hi, please check https://github.com/Fuco1/smartparens and https://github.com/joaotavora/autopair, IIRC some of those provided that functionality. Up to the point when some people complained about it being too eager, eg #135 and #143.

@immerrr
Copy link
Owner

immerrr commented Apr 2, 2018

If you'd like to have it implemented separately regardless, please keep the mentioned issues in mind to avoid inconveniences when editing comments or string literals (there's a lua-comment-or-string-p function to help with this).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant