How to migrate align-regexp for a specific regex in emacs

I often use align-regexp with the regular expression "[^] + _" in the scope. So I decided that I would define a function for it so that I could bind it to a key:

(defun align-members () (interactive) (align-regexp " [^ ]+_")) 

But emacs complains that align-regexp accepts three parameters. Looking at the documents, I see that he takes BEG and END. I'm not sure how (interactive) stuff works in emacs, but by reading the documentation, I'm going to do this:

 (defun align-members (BEG END) (interactive "r") (align-regexp BEG END " [^ ]+_")) 

But emacs then complains somewhere deep in the align-regexp call stack that it expected integer-or-marker-p and instead received zero. What am I doing wrong?

+4
source share
4 answers

You should write the following

 (defun align-members (BEG END) (interactive "r") (align-regexp BEG END (concat "\\(\\s-*\\)" " [^ ]+_") 1 1)) 

or a little easier

 (defun align-members (BEG END) (interactive "r") (align-regexp BEG END "\\(\\s-*\\) [^ ]+_" 1 1)) 

To understand this, look at the source align-regexp , there is a part of it here.

 (interactive (append (list (region-beginning) (region-end)) (if current-prefix-arg (list (read-string "Complex align using regexp: " "\\(\\s-*\\)") (string-to-number (read-string "Parenthesis group to modify (justify if negative): " "1")) (string-to-number (read-string "Amount of spacing (or column if negative): " (number-to-string align-default-spacing))) (y-or-np "Repeat throughout line? ")) (list (concat "\\(\\s-*\\)" (read-string "Align regexp: ")) 1 align-default-spacing nil)))) 

As you can see:

  • it adds the string "\\(\\s-*\\)" to your regular expression
  • it sets 1 and align-default-spacing to optional parameters
+4
source

Oleg answer is good.

The reason align-regexp fills the details for you when invoked interactively is because you should be able to do the most useful things with as little as possible. If you need more control, use Cu .

This is a common design theme for Emacs teams.

Now, when you call align-regexp programmatically, there is no need to offer you briefly, as, apparently, the programmer wants to control completely by default.

In the future, when you convert a task to an interactive defun , use Cx esc esc to see how the team received your input after they went through the interactive forms.

In this case, you should see something like:

 (align-regexp 185 320 "\\(\\s-*\\)" [^ ]+_" 1 1 nil) 
+2
source

The documentation for align-regexp states

If the arg prefix is ​​specified, a full regex with parenthesized spaces must be provided

so I copied the space in your regex and seems to run it:

 (defun align-members (beg end) (interactive "r") (align-regexp beg end "\\( \\)[^ ]+_")) 
0
source

From my point of view, I cannot find group in your regular expression. The lack of a test template from you, I'll just give an example.

Try aligning the code:

 bit [4:0] a; bit [16:0] b; 

To:

 bit [ 4:0] a; bit [ 16:0] b; 

I have such a working function with group :

 (defun try-align () (interactive) (let ((BEG (region-beginning)) (END (region-end))) (align-regexp BEG END "\\[\\(.*:\\)" -1 1 0))) 

If you remove group as shown below, this will not work:

 (defun try-align () (interactive) (let ((BEG (region-beginning)) (END (region-end))) (align-regexp BEG END "\\[.*:" -1 1 0))) 
0
source

Source: https://habr.com/ru/post/1396469/


All Articles