Skip to content

Commit

Permalink
Per #4: Add implicit parenthesized expression
Browse files Browse the repository at this point in the history
  • Loading branch information
mkcms committed Dec 2, 2018
1 parent 2504a9e commit e1308c8
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@
** ~ialign-initial-repeat~
Default state of ~repeat~ argument passed to ~align-regexp~.

The default value is ~nil~.
** ~ialign-implicit-regexp~
String to prepend to the regexp, if the regex doesn't have a subgroup.

Set this to ~\\(\\s-*\\)~ to implicitly prepend that string to
the regexp, if the regexp doesn't contain a subgroup expression.

The default value is ~nil~.
* License
This program is free software: you can redistribute it and/or modify
Expand Down
32 changes: 27 additions & 5 deletions ialign.el
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,20 @@ or equal to this, otherwise do not update."
:group 'ialign
:type 'boolean)

(defcustom ialign-implicit-regexp nil
"String to prepend to the regexp, if the regexp doesn't have a subgroup.
`align-regexp' expects the align regexp to contain a
parenthesized subexpression whose characters are replaced.
However, it adds such an expression automatically to the regexp
if necessary. This option allows you to specify the string to
implicitly prepend to the string in case there's no group
subexpression."
:group 'ialign
:type '(choice (const :tag "Don't add implicit group" nil)
(const :tag "Whitespace" "\\(\\s-*\\)")
string))

(defvaralias 'ialign-initial-spacing 'ialign-default-spacing)

(defvar ialign--buffer nil)
Expand Down Expand Up @@ -193,11 +207,19 @@ help"))))
(defun ialign--align ()
"Revert the current region, then align it."
(ialign--revert)
(ialign--with-region-narrowed
(let ((case-fold-search ialign--case-fold-search)
(indent-tabs-mode (ialign--enable-tabs-p)))
(align-regexp (point-min) (point-max) ialign--regexp
ialign--group ialign--spacing ialign--repeat))))
(let ((reg ialign--regexp))
(when (and (null (string-match-p (regexp-quote "\\(") reg))
(stringp ialign-implicit-regexp)
(= 1 ialign--group))
(setq reg (concat ialign-implicit-regexp reg))
(setq ialign--error "Using implicit regexp")
(when (minibufferp)
(ialign--update-minibuffer-prompt)))
(ialign--with-region-narrowed
(let ((case-fold-search ialign--case-fold-search)
(indent-tabs-mode (ialign--enable-tabs-p)))
(align-regexp (point-min) (point-max) reg
ialign--group ialign--spacing ialign--repeat)))))

(defun ialign--undo (beg end orig)
"Delete region BEG END and insert ORIG at BEG.
Expand Down

0 comments on commit e1308c8

Please sign in to comment.