edkolev / evil-lion

Evil align operator
131 stars 12 forks source link

consider incorporating some ideas from spacemacs alignment functions #7

Closed ninrod closed 7 years ago

ninrod commented 7 years ago

Spacemacs has some interesting functions regarding alignment right here.

Consider incorporating some the functionality into lion. the align-repeat seems to be the most useful. Here's the snippet:

;; BEGIN align functions

;; modified function from http://emacswiki.org/emacs/AlignCommands
(defun spacemacs/align-repeat (start end regexp &optional justify-right after)
  "Repeat alignment with respect to the given regular expression.
If JUSTIFY-RIGHT is non nil justify to the right instead of the
left. If AFTER is non-nil, add whitespace to the left instead of
the right."
  (interactive "r\nsAlign regexp: ")
  (let* ((ws-regexp (if (string-empty-p regexp)
                        "\\(\\s-+\\)"
                      "\\(\\s-*\\)"))
         (complete-regexp (if after
                              (concat regexp ws-regexp)
                            (concat ws-regexp regexp)))
         (group (if justify-right -1 1)))
    (message "%S" complete-regexp)
    (align-regexp start end complete-regexp group 1 t)))

;; Modified answer from http://emacs.stackexchange.com/questions/47/align-vertical-columns-of-numbers-on-the-decimal-point
(defun spacemacs/align-repeat-decimal (start end)
  "Align a table of numbers on decimal points and dollar signs (both optional)"
  (interactive "r")
  (require 'align)
  (align-region start end nil
                '((nil (regexp . "\\([\t ]*\\)\\$?\\([\t ]+[0-9]+\\)\\.?")
                       (repeat . t)
                       (group 1 2)
                       (spacing 1 1)
                       (justify nil t)))
                nil))

(defmacro spacemacs|create-align-repeat-x (name regexp &optional justify-right default-after)
  (let ((new-func (intern (concat "spacemacs/align-repeat-" name))))
    `(defun ,new-func (start end switch)
       (interactive "r\nP")
       (let ((after (not (eq (if switch t nil) (if ,default-after t nil)))))
         (spacemacs/align-repeat start end ,regexp ,justify-right after)))))

(spacemacs|create-align-repeat-x "comma" "," nil t)
(spacemacs|create-align-repeat-x "semicolon" ";" nil t)
(spacemacs|create-align-repeat-x "colon" ":" nil t)
(spacemacs|create-align-repeat-x "equal" "=")
(spacemacs|create-align-repeat-x "math-oper" "[+\\-*/]")
(spacemacs|create-align-repeat-x "ampersand" "&")
(spacemacs|create-align-repeat-x "bar" "|")
(spacemacs|create-align-repeat-x "left-paren" "(")
(spacemacs|create-align-repeat-x "right-paren" ")" t)
(spacemacs|create-align-repeat-x "backslash" "\\\\")

;; END align functions

Thanks again for the package.

edkolev commented 7 years ago

If I understand correctly, this snippet defines interactive commands align-repeat-comma, align-repeat-semicolon, etc. Plus it instructs align.el to repeat the alignment. Is this correct?

At this point, evil-lion also repeats the alignment, but it would be hice to have an option not to. For example, it should be possible to align on the first instance of , instead of all of them. I might add this in the future.

ninrod commented 7 years ago

If I understand correctly, this snippet defines interactive commands align-repeat-comma, align-repeat-semicolon, etc. Plus it instructs align.el to repeat the alignment. Is this correct?

Yes that is corret.

rlister commented 7 years ago

@edkolev If I understand this correctly, vim-lion uses count to repeat alignment, so that 1gLip, will align just the first comma.

edkolev commented 7 years ago

@rlister exactly.

I've added experimental support for COUNT 1 since it was simple to implement https://github.com/edkolev/evil-lion/commit/da324d54e539f042a85344d1c168b69106896b8b

So this should now work on the first occurrence of comma: 1 gL ip , At this point, only 1 is supported.

Refs #9

ninrod commented 7 years ago

Gentleman, after the last commit, I think we can safely close this.

This is shaping up to be a great package in the evil ecosystem.

edkolev commented 7 years ago

Thanks, and thank you for your valuable input!