代码之家  ›  专栏  ›  技术社区  ›  wisemonkey

在Emacs for UVM/SystemVerilog中折叠展开代码/注释部分块

  •  1
  • wisemonkey  · 技术社区  · 10 年前

    我目前使用emacs进行UVM测试台环境开发。我使用带有日光主题的verilog模式。我喜欢我当前的设置,但是我想改进emacs的功能。

    我想为代码/注释块添加折叠展开功能。

    例如。 我的课程如下

    class myClass ;
       function myFunction
         // variable define
         // more variable define
    
         begin
        //line 1
        //line 2
        //line 3
         end
    
          begin
         //line1
         //line2
         //line3
          end
    
       endfunction //
    endclass // myClass
    

    如果我使用第二个(底部)开始-结束块,我想折叠第一个(顶部) //后面的任何内容都是注释,可以用实际代码替换。 我可以用emacs实现这一点吗?

    2 回复  |  直到 10 年前
        1
  •  1
  •   Glorfindel Doug L.    5 年前

    下面是一个使用 \begin{anything} \end{anything} 用于函数使用的开始和结束regexp toggle-block 确定使用哪两个点来折叠这两个点之间的所有内容。原始海报可以修改 beg-flag-regexp 像这样 function1\\|function2\\|function3\\|function4 结尾的regexp类似于 endfunction 。开始点应该正好在 function1 因此 功能1 当代码块折叠时仍然可见。需要对该功能进行额外的调整/修改,以满足原始海报的精确需求,但本示例应有助于演示使用 outline-flag-region 来自 outline 图书馆

    (defvar beg-flag-regexp (concat
      "\\(\\\\begin\{\\)\\("
      "[^}]*"
      "\\)\\(\}\\)" )
    "Regexp matching the beginning of the folded region.")
    
    (defun toggle-block ()
    "When FLAG is non-nil, hide the region.  Otherwise make it visible.  For this
    function to work, the cursor must be on the same line as the beginning regexp."
    (interactive)
      (require 'outline)
      (cond
        ((or
            ;; sweet-spot
            (looking-at beg-flag-regexp)
            ;; point could be between backslash and before the letter n
            (let ((line-begin (save-excursion (beginning-of-line 1) (point))))
              (save-excursion
                (re-search-backward "\\\\" line-begin t)
                (looking-at beg-flag-regexp)))
            ;; point could be to the right of \begin
            (let ((line-begin (save-excursion (beginning-of-line 1) (point))))
              (save-excursion
                (re-search-backward "\\\\begin" line-begin t)
                (looking-at beg-flag-regexp)))
            ;; point could be to the left of \begin
            (let ((line-end (save-excursion (end-of-line 1) (point))))
              (save-excursion
                (re-search-forward "\\\\begin" line-end t)
                (backward-char 6)
                (looking-at beg-flag-regexp))))
          (let* (
              (flag (not (get-char-property (match-end 0) 'invisible)))
              (beg (match-end 0))
              end
              (base-flag-match (regexp-quote
                (buffer-substring-no-properties (match-beginning 2) (match-end 2))))
              (end-flag-match (concat "\\\\end\{" base-flag-match "\}"))
              (go-fish (concat "\\begin\{" base-flag-match "\}"))  )
            (save-excursion
              (if (re-search-forward end-flag-match nil t)
                (progn
                  (setq end (point))
                  (outline-flag-region beg end flag)
                  (cond
                    (flag
                      (overlay-put (make-overlay beg end) 'display "\u25be"))
                    (t
                      (mapc 'delete-overlay (overlays-in beg end)))))
                (user-error "Error locating an ending match for:  %s." go-fish)))
            (if (> (point) beg)
              (goto-char beg)) ))
        (t
          (message "Sorry, you are not on a line containing the beginning regexp."))))
    

    第一步 :粘贴变量的整个代码 乞求标志正则表达式 和函数 肘节块 进入 .emacs 文件,然后重新启动Emacs。

    第二步 :打开一个空缓冲区,然后键入 M-x text-mode RET

    第三步 :将以下示例复制并粘贴到刚刚创建的缓冲区中:

    \开始{智慧猴子}Lorem ipsum dolor坐amet,consectetur adipiscing elit。智人万岁(Vivamus convallis sodales sapien),智人万岁。埃涅安·埃姆桑·利奥是一位伟大的政治家,也是一位杰出的政治家。在康瓦利斯arcu中。ultrices lectus nec nisi malesuada hendrerit。Proin调戏rutrum砂矿。Donec gravida,舌坐amet luctus interdum,nisl quam tempus sem,eget varius lacus orci ac sapien。在metus非neque调味品blandit中的Nulla。Phasellus sed is a tellus fermentum viverra vulputate non dui。nisi的整数级别。Phasellus turpis lectus,gravida sit amet or are faucibus,sollicitudin quis metus。

    Pellent型居住者morbi tristique senectus et netus et malesuada fames ac turpis egestas。Sed hendrerit commodo consectetur。Mauris sollicitudin ut sapien a interdum。Maecenas ac nibh sodales,adipiscing leo a,dictum turpis。侧前庭。埃格斯塔斯农场的乌特·弗林吉拉-拉库斯。Nulla aliquet eleifend quam,nec varius tortor tincidunt at.Donec non vehicula tortor,eu congue augus。Nulla a nisi hendrerit、tincidunt sapien eget、accumsan mauris。在社会上,纳托克·佩纳提布斯和马格尼斯是产妇,而不是嘲笑母亲。\结束{wismonkey}

    \开始{法律列表}Nunc sapien elit,felis non的孕妇,volutpat lacinia quam。Nulla ut sagitis turpis。Curabitur faucibus ullamcorper felis,eget hendrerit dolor。Donec volutpat,调味品posuere的nisi,nulla justo blandit tortor,rhoncus odio auge id dolor。Praesent坐在aliquam risus旁边。Praesent odio velit,dignissim eget tincidunt nec,ultricies ut est。Morbi坐在一个叫pellentesque ipsum的地方。乌特·劳里特·毛里斯。Donec lobortis auctor tortor,id faucibus urna luctus quis。Praesent vel sollicitudin mi。Sed neque mi,ultrices nec erat ut,congue convalis ante。Sed eu ipsum blandit,porttitor libero ac,volutpat est。

    怀孕前阶段,使用扫描电镜元素。刚果人民共和国总统。普莱森特·拉奇尼娅·阿克利罗(Praesent lacinia ac libero)在终点。Mauris nec blandit ante。整数accumsan et elit vitae scelerisque。梅塞纳斯·亨德里特,埃利特·奎斯·法利西斯·朗库斯,我只是阿迪皮西奇·埃拉特,我是欧盟巨头。Vestibulum tincidunt,lacus vitae lessiie hendrerit,quam justo feugiat odio,eu ullamcorper risus sem vitae lacus。前庭位于刚果亨德雷里特维尔奥古斯特的dolor砂矿中。怀孕时的猫是一种悲哀的动物。Nullam坐在尼斯刚果,洛博尔蒂斯拉库斯欧盟,varius ante。Donec vehicula lacinia elit ac调味品。法雷特拉的Aliquam egestas adipiscing orci。Curabitur pulvinar tincidunt neque,non-tempus nisi volutpat eget。Nulla tincidunt egestas libero,一种半兽人鱼。Nulla nec nunc nec urna semper tristique(三分之一)。Proin est dolor,sodales ut pretium eu,luctus a magna。\结束{lawlist}

    第四步 :要折叠文本,请将光标放在包含 \begin{wisemonkey} \begin{lawlist} 和类型 M-x toggle-block

    第五步 :要展开文本,只需重复 第四步 .


    Example
    (来源: lawlist.com )

    Example
    (来源: lawlist.com )

    Example
    (来源: lawlist.com )


    示例#2

    (defun wisemonkey ()
    "When FLAG is non-nil, hide the region.  Otherwise make it visible.  For this
    function to work, the cursor must be on the same line as the beginning regexp."
    (interactive)
      (require 'outline)
      (cond
        ((let ((line-end (save-excursion (end-of-line 1) (point))))
          (save-excursion
            (beginning-of-line 1)
            (re-search-forward "function" line-end t)))
          (let* (
              (beg (save-excursion (end-of-line 1) (point)))
              (flag (not (get-char-property beg 'invisible)))
              end
              (end-flag-match "endfunction"))
            (save-excursion
              (if (re-search-forward end-flag-match nil t)
                (progn
                  (setq end (point))
                  (outline-flag-region beg end flag)
                  (cond
                    (flag
                      (overlay-put (make-overlay beg end) 'display "\u25be"))
                    (t
                      (mapc 'delete-overlay (overlays-in beg end)))))
                (user-error "Error locating an ending match for:  `function`.")))
            (if (> (point) beg)
              (goto-char beg)) ))
        (t
          (message "Sorry, you are not on a line containing the beginning regexp."))))
    
        2
  •  0
  •   Enze Chi    10 年前

    我的配置文件中有这个,你可以试试:

    (setq hs-special-modes-alist
          (cons '(verilog-mode "\\<begin\\>\\|\\<task\\>\\|\\<function\\>\\|\\<class\\>\\|\\<module\\>\\|\\<package\\>"
                               "\\<end\\>\\|\\<endtask\\>\\|\\<endfunction\\>\\|\\<endclass\\>\\|\\<endmodule\\>\\|\\<endpackage\\>"
                               nil
                               verilog-forward-sexp-function)
                hs-special-modes-alist))