1

In Emacs how would I align a group of text:

signal slv4   : std_logic_vector(3 downto 0);
signal slv16  : std_logic_vector(15 downto 0);
signal slv8   : std_logic_vector(7 downto 0);
signal slv32  : std_logic_vector(32 downto 0);

so it looks like this

signal slv4   : std_logic_vector( 3 downto 0);
signal slv16  : std_logic_vector(15 downto 0);
signal slv8   : std_logic_vector( 7 downto 0);
signal slv32  : std_logic_vector(32 downto 0);

Basically I want to right justify the numbers before "downto"; another side effect is that the text is right justified (the semi-colons line up).

How would I achieve this? I have played around with M-x align and M-x align-regexp however I can't seem to get what I'm looking for.

Also I'm using vhdl-mode so maybe it has something built in?

mrbean
  • 547
  • 5
  • 14
  • 1
    Check out this question and answer: http://stackoverflow.com/questions/10895930/right-align-text-in-emacs – Thomas May 09 '13 at 04:40
  • Have you tried the VHDL mode beautify? It's under the VHDL->beautify menu. – pknodle May 09 '13 at 20:45
  • @Thomas Thanks I ended up implementing my own function as follows: `(defun my-align-downto (beg end) (interactive "r") (align-regexp beg end "\\([0-9]* downto*\\)" -1 0 t))` – mrbean May 10 '13 at 02:25
  • So all I do is highlight the desired code and call my function by `M-x my-align-downto` Which seems to work out OK. @ pknodle This feature is not implemented in vhdl-mode afk, does it work on yours? – mrbean May 10 '13 at 02:31

3 Answers3

0

I don't think there is such a thing in vhdl mode because you need spaces in the middle of the text. If it were just about right aligning, you could possibly play around with tab behaviour. In this case, you could use a macro.

http://www.emacswiki.org/emacs/KeyboardMacros

shrm
  • 1,112
  • 2
  • 8
  • 20
0

In a first run the maximum of padding is detected, than called recursively with padding given.

Edit `this-regexp' inside let if something different is needed. Maybe the hard-coded opening "(" in concat-form needs edit than too.

(defun my-numbers-padded (&optional beg end len)
  "Pad numbers as given in example string "
  (interactive "*")
  (let* ((beg (cond (beg)
                    ((use-region-p)
                     (region-beginning))
                    (t (point-min))))
         (end (cond (end (copy-marker end))
                    ((use-region-p)
                     (copy-marker (region-end)))
                    (t (copy-marker (point-max)))))
         (this-regexp "^\\([^(]+\\)\(\\([0-9]+\\)\\(.+\\)$")
         (len len)
         lenmax
         (var (and len (concat "(format \"%" (number-to-string len) "s\" (match-string-no-properties 2))"))))
    (goto-char beg)
    (if len
        (while (re-search-forward this-regexp  nil t 1)
          (setq erg (eval (car (read-from-string var))))
          (replace-match
           (concat (match-string-no-properties 1) "\("   erg (match-string-no-properties 3))))
      (while (re-search-forward this-regexp  nil t 1)
        (setq current-length (length (match-string-no-properties 2)))
        (if (and lenmax (< lenmax current-length))
            (setq lenmax current-length)
          (unless lenmax (setq lenmax current-length))))
      (my-padding-numbers beg end lenmax))))
Andreas Röhler
  • 4,804
  • 14
  • 18
  • Unfortunately I couldn't get this to work, I may need to study my Lisp a bit to figure out how this works. If this function works on the entire buffer then that would save me the trouble of selecting regions and rigging up a `align-regexp` as I did above. – mrbean May 10 '13 at 02:36
  • @mrbean beg and end are set conditionally. If no region exists and beg/end not given as arguments, it works at the whole buffer. – Andreas Röhler May 11 '13 at 05:29
0

Here is what I'm currently using (which works fine):

(defun my-align-downto (beg end)
    (interactive "r")
    (align-regexp beg end "\\([0-9]* downto*\\)" -1 0 t))

I thought I would take it one step further and create my own 'align hook' based on the information at the very bottom of this page: http://www.emacswiki.org/emacs/AlignCommands#toc8

(add-hook 'align-load-hook (lambda ()
    (add-to-list 'align-rules-list
      '(downto-align
        (regexp  . "\\([0-9]* downto*\\)")
        (group   . -1)
        (spacing . 0)
        (modes   . vhdl-mode)
        (repeat  . t)))))

With which I get no results. :(

Does vhdl-mode override these alignment rules.. or am I just not doing something correctly?

mrbean
  • 547
  • 5
  • 14
  • Does `(modes . '(vhdl-mode))` fix it? – phils May 10 '13 at 06:32
  • I think it did but now I get an error... "Args out of range: -1, 0". It doesn't seem to like my 'Group' setting? – mrbean May 10 '13 at 14:43
  • Ah, you've gotten confused by how `align-regexp` triggers justification. You want `(group . 1) (justify . t)`. Check the `align-rules-list` documentation. – phils May 10 '13 at 14:58