Skip to content

Auto-Enclosing Blocks #142

Open
Open

Description

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)

Activity

  1. immerrr commented on Apr 2, 2018

    @immerrr
    Owner

    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.

  2. immerrr commented on Apr 2, 2018

    @immerrr
    Owner

    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

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions