help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Operate on each line in region


From: Jacob Gerlach
Subject: Re: Operate on each line in region
Date: Wed, 23 Apr 2014 16:23:20 -0700 (PDT)
User-agent: G2/1.0

I'm having more trouble figuring this out that I expected.

I tried building a function around align-regexp:

(defun my-justify-equals (start end)
  "Indents current region to justify equals signs"
  (interactive 'r')
  (align-regexp start end "\\([ \t]*\\)\\(.*\\)=" 1 nil nil))

But when I attempt to evaluate the defun I get 

    Invalid read syntax: ")"

In any case, I still can't get the right result using  align-regexp 
interactively.

I tried "\([ \t]*\)\(.*\)=" as my regexp, with 1 for 'group' and 'spacing.' 
This inserts a tab character at the beginning of the line...

After reviewing the documentation more closely, perhaps align regexp doesn't do 
what I need. The description for group states:

The "alignment character" is always the first character immediately following 
this parenthesis group.

However, I want a different behavior, where the alignment character is later in 
the string, so it seems that what I want to do isn't actually possible with 
align-regexp.

So I tried something more in line with my original idea, except working from 
the end to the beginning as Stefan suggested:

(defun my-justify-equals (start end)
  "Indents current region to justify equals signs"
  (interactive 'r')
  (save-excursion
    (goto-char end)
    (let (left-length)
      (save-excursion
        (while (> (point) start)
          (if (re-search-forward "^[ \t]*\\(.*\\)=" (line-end-position) t)
              (when (> (length (match-string-no-properties 1)) left-length)
                (setq left-length (length (match-string-no-properties 1)))))
          (forward-line -1)))
      (goto-char end)
      (while (> (point) start)
        (if (re-search-forward "^[ \t]*\\(.*\\)=" (line-end-position) t)
            (indent-line-to (+ tab-width (length (match-string-no-properties 
1))))
          (indent-line-to tab-width))
        (forward-line -1)))))

However, I can't test this. Attempting to evaluate the defun gives "Invalid 
read syntax ")". I followed the instructions at 
http://www.gnu.org/software/emacs/manual/html_node/elisp/Syntax-Errors.html, 
but was unable to identify an error.

What am I missing?

Thanks,
Jake


reply via email to

[Prev in Thread] Current Thread [Next in Thread]