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

Indent and align at the same time #599

Closed
antoineB opened this issue Dec 17, 2019 · 1 comment
Closed

Indent and align at the same time #599

antoineB opened this issue Dec 17, 2019 · 1 comment

Comments

@antoineB
Copy link
Contributor

It could be good to easily indent this:

$abc = 1;
$abcd = 2;
$abcde = 3;

['abc' => 1,
 'abcd' => 2,
 'abcde' => 3];

Into:

$abc   = 1;
$abcd  = 2;
$abcde = 3;

['abc'   => 1,
 'abcd'  => 2,
 'abcde' => 3];

The following code does this if the caret is before = or => and leverage align-regexp, maybe there is another option using cc-engine indentation mechanism that will work with indent-region.

(defun php--indent-align ()
  (interactive)
  (cond ((looking-at "[ \t]*=>")
         (let* ((begin (cadr (syntax-ppss)))
                (end (when begin (condition-case nil (scan-sexps begin 1) ('error nil)))))
           (when end
             (align-regexp begin (scan-sexps begin 1) "\\(\\s-*\\)=>"))))
        ((looking-at "[ \t]*=[$ \ta-zA-Z0-9_'\"\\\\]")
         (let* ((ss (cadr (syntax-ppss)))
                (sp (save-excursion (backward-paragraph 1) (point)))
                (begin (if ss (max ss sp) sp))
                (es (when ss (condition-case nil (scan-sexps ss 1) ('error nil))))
                (ep (save-excursion (forward-paragraph 1) (point)))
                (end (if es (min es ep) ep)))
           (align-regexp begin end "\\(\\s-*\\)=")))
        ((indent-for-tab-command))))
@zonuexe
Copy link
Member

zonuexe commented Dec 22, 2019

@antoineB
Thanks for the suggestion.
We have no plans to integrate this code into php.el, but have posted it on the Wiki Tips page.

@zonuexe zonuexe closed this as completed Dec 22, 2019
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

2 participants