From e1308c8f6aea05ad6dbcaa33b9bee4eb7e05ee39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Krzywkowski?= Date: Sun, 2 Dec 2018 12:07:06 +0100 Subject: [PATCH] Per #4: Add implicit parenthesized expression --- README.org | 7 +++++++ ialign.el | 32 +++++++++++++++++++++++++++----- 2 files changed, 34 insertions(+), 5 deletions(-) diff --git a/README.org b/README.org index cd4f684..22d0286 100644 --- a/README.org +++ b/README.org @@ -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 diff --git a/ialign.el b/ialign.el index 543992f..8652cc5 100644 --- a/ialign.el +++ b/ialign.el @@ -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) @@ -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.