【问题标题】:How to align certain text in emacs vhdl-mode如何在 emacs vhdl-mode 中对齐某些文本
【发布时间】:2013-05-09 03:26:00
【问题描述】:

在 Emacs 中我如何对齐一组文本:

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);

看起来像这样

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);

基本上我想正确证明“downto”之前的数字;另一个副作用是文本右对齐(分号对齐)。

我将如何实现这一目标?我玩过M-x alignM-x align-regexp 但是我似乎无法得到我想要的东西。

我也在使用 vhdl 模式,所以也许它内置了一些东西?

【问题讨论】:

  • 你试过VHDL模式美化吗?它在 VHDL->beautify 菜单下。
  • @Thomas 谢谢我最终实现了自己的功能,如下所示:(defun my-align-downto (beg end) (interactive "r") (align-regexp beg end "\\([0-9]* downto*\\)" -1 0 t))
  • 所以我所做的就是突出显示所需的代码并通过M-x my-align-downto 调用我的函数,这似乎可以正常工作。 @pknodle 这个功能在vhdl-mode afk中没有实现,你的能用吗?

标签: emacs elisp vhdl emacs24


【解决方案1】:

我不认为在 vhdl 模式中有这样的事情,因为你需要在文本中间有空格。如果它只是右对齐,您可能会玩tab 行为。在这种情况下,您可以使用宏。

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

【讨论】:

    【解决方案2】:

    在第一次运行中,检测到填充的最大值,然后在给定填充的情况下递归调用。

    如果需要不同的东西,请在 let 中编辑 `this-regexp'。 也许 concat 形式的硬编码开头“(”也需要编辑。

    (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))))
    

    【讨论】:

    • 不幸的是我不能让它工作,我可能需要研究一下我的 Lisp 来弄清楚它是如何工作的。如果这个函数适用于整个缓冲区,那么我就不用像上面那样选择区域和装配align-regexp了。
    • @mrbean beg 和 end 是有条件地设置的。如果不存在区域并且未将 beg/end 作为参数给出,则它适用于整个缓冲区。
    【解决方案3】:

    这是我目前正在使用的(效果很好):

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

    我想我会更进一步,根据本页最底部的信息创建自己的“对齐钩子”: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)))))
    

    我没有得到任何结果。 :(

    vhdl-mode 是否会覆盖这些对齐规则......或者我只是没有正确地做某事?

    【讨论】:

    • (modes . '(vhdl-mode)) 修复了吗?
    • 我认为确实如此,但现在我收到一个错误...“Args 超出范围:-1, 0”。它似乎不喜欢我的“群组”设置?
    • 啊,您对align-regexp 如何触发理由感到困惑。你想要(group . 1) (justify . t)。查看align-rules-list 文档。
    • 完美,成功了!非常感谢!现在说得通了。
    猜你喜欢
    • 2021-11-19
    • 1970-01-01
    • 2012-06-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-23
    • 1970-01-01
    相关资源
    最近更新 更多