lua-mode icon indicating copy to clipboard operation
lua-mode copied to clipboard

Auto-Enclosing Blocks

Open ghost opened this issue 7 years ago • 2 comments

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)

ghost avatar Feb 03 '18 21:02 ghost

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 avatar Apr 02 '18 09:04 immerrr

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).

immerrr avatar Apr 02 '18 09:04 immerrr