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

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

Re: [h-e-w] Off Topic: Align mode and aligning comments


From: Stephen Leake
Subject: Re: [h-e-w] Off Topic: Align mode and aligning comments
Date: 26 Sep 2002 09:02:39 -0400
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.2

Paul Whitfield <address@hidden> writes:

> Hi All,
> 
> Here is non-windows question for pondering
> 
> I would like to force align.el to align my comments in C code
> for example if I run align over the following code
> 
> int i; /* This is a comment */
> struct t_a a;  /*Another comment */
> 
> I would like it to be aligned as
> 
> int    i;     /* This is a comment */
> struct t_a a; /* Another comment */
> 
> Currently align refuses to align the comments, and
> 
> I am afraid that the align customise options are
> beyond my ken :-(

Here's my C align settup; it does what you want (and probably more :).
I copied c-align-rules-list from align.el and changed it. It's just
regexp, not rocket science :).

;; alignment
(require 'align)
(defcustom c-align-rules-list
  `((c-comment-one-line
     (regexp . "[^- \t]\\(\\s-*\\)/\\*.*\\*/$")
     (group  . 1)
     (repeat . nil))

    (c-macro-definition
     (regexp   . "^\\s-*#\\s-*define\\s-+\\S-+\\(\\s-+\\)"))

    (c-macro-line-continuation
     (regexp   . "\\(\\s-*\\)\\\\$")
     (column   . c-backslash-column))

    (c-variable-declaration
     (regexp   . ,(concat "[*&0-9A-Za-z_]>?[&*]*\\(\\s-+[*&]*\\)"
                          "[A-Za-z_][0-9A-Za-z:_]*\\s-*\\(\\()\\|"
                          "=[^=\n].*\\|(.*)\\|\\(\\[.*\\]\\)*\\)?"
                          "\\s-*[;,]\\|)\\s-*$\\)"))
     (group    . 1)
     (justify  . t)
     (valid
      . ,(function
          (lambda ()
            (not (or (save-excursion
                       (goto-char (match-beginning 1))
                       (backward-word 1)
                       (looking-at
                        "\\(goto\\|return\\|new\\|delete\\|throw\\)"))
                     (if (and (boundp 'font-lock-mode) font-lock-mode)
                         (eq (cadr (memq 'face (text-properties-at (point))))
                             'font-lock-comment-face)
                       (eq (caar (c-guess-basic-syntax)) 'c))))))))

    (c-assignment
     (regexp   . ,(concat "[^-=!^&*+<>/| \t\n]\\(\\s-*[-=!^&*+<>/|]*\\)"
                          "=\\(\\s-*\\)\\([^= \t\n]\\|$\\)"))
     (group    . (1 2))
     (justify  . t)
     (tab-stop . nil))

    (c-chain-logic
     (regexp   . "\\(\\s-*\\)\\(&&\\|||\\|\\<and\\>\\|\\<or\\>\\)")
     (modes    . align-c++-modes)
     (valid    . ,(function
                   (lambda ()
                     (save-excursion
                       (goto-char (match-end 2))
                       (looking-at "\\s-*\\(/[*/]\\|$\\)"))))))
    )
  "*List of C alignment rules. See 'align-rules-list'."
  :type align-rules-list-type
  :group 'c)

(add-hook 'c-mode-hook (lambda () (setq align-mode-rules-list 
c-align-rules-list)))

-- 
-- Stephe





reply via email to

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