1

I currently use emacs for UVM testbench environment development. I use verilog-mode with solarized theme. I like my current setup, however I would like to improve the functionality of emacs.

I would like to add fold-unfold functionality for block of code / comment.

for e.g. I've a class as follows

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

If I'm working with 2nd (bottom) begin-end block, I would like to fold 1st one (top) Anything followed by // is a comment and can be replaced with actual code. Can I achieve this with emacs?

4

2 回答 2

1

这是一个示例,它使用\begin{anything}\end{anything}作为函数使用的开始和结束正则表达式toggle-block来确定使用哪两个点来折叠这两个点之间的所有内容。原始海报可以修改beg-flag-regexp为类似的东西function1\\|function2\\|function3\\|function4,而结尾的正则表达式将是类似的东西endfunction。起点可能应该就在之后,function1以便function1在折叠代码块时仍然可见。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."))))

第一步beg-flag-regexp:将变量和函数的整个代码粘贴toggle-block.emacs文件中,然后重新启动 Emacs。

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

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

\begin{wisemonkey}Lorem ipsum dolor sit amet,consectetur adipiscing elit。Vivamus convallis sodales sapien, ut vulputate tellus mollis vel。Aenean accumsan leo est,nec euismod sem imperdiet blandit。在 sed consequat mi 中,在 convallis arcu 中。前庭 ultrices lectus nec nisi malesuada hendrerit。Proin molestie placerat rutrum。Donec gravida, ligula sit amet luctus interdum, nisl quam tempus sem, eget varius lacus orci ac sapien。在 metus non neque condimentum blandit 中的 Nulla。Phasellus sed est atellus balloonum viverra vulputate non dui。整数 vel tempor nisi。Phasellus turpis lectus, gravida sit amet ornare faucibus, sollicitudin quis metus。

Pellentesque 居民 morbi tristique senectus et netus et malesuada 名声 ac turpis egestas。Sed hendrerit commodo consectetur。Mauris sollicitudin ut sapien a interdum。Maecenas ac nibh sodales,adipiscing leo a,dictum turpis。Sed 前庭 odio id pulvinar rhoncus。Ut fringilla lacus 在 egestas semper。Nulla aliquet eleifend quam, nec varius tortor tincidunt 在。Donec non vehicula tortor, eu congue augue。Nulla a nisi hendrerit,tincidunt sapien eget,accumsan mauris。Cum sociis natoque penatibus et magnis dis parturient montes, nascetur ridiculus mus.\end{wisemonkey}

\begin{lawlist} Nunc sapien elit,felis non 中的 gravida,volutpat lacinia quam。Nulla ut sagittis turpis。Curabitur faucibus ullamcorper felis,eget hendrerit dolor。Donec volutpat, nisi at condimentum posuere, nulla justo blandit tortor, rhoncus odio augue id dolor。Praesent 坐amet aliquam risus。Praesent odio velit,dignissim eget tincidunt nec,ultricies ut est. Morbi sit amet pellentesque ipsum。Ut vitae laoreet mauris。Donec lobortis auctor tortor,id faucibus urna luctus quis。Praesent vel sollicitudin mi。Sed neque mi, ultrices nec erat ut, congue convallis ante。Sed eu ipsum blandit、porttitor libero ac、volutpat est。

Phasellus gravida urna 在 ante pretium,sed interdum sem elementum。Maecenas congue purus nec aliquam lobortis。在 eleifend 的 Praesent lacinia ac libero。Mauris nec blandit ante。整数 accumsan et elit vitae scelerisque。Maecenas hendrerit, elit quis facilisis rhoncus, mi justo adipiscing erat, vitae pellentesque dui purus eu magna。Vestibulum tincidunt, lacus vitae molestie hendrerit, quam justo feugiat odio, eu ullamcorper risus sem vitae lacus。前庭坐在 amet nunc 在 dolor placerat congue hendrerit vel augue。Vivamus ac odio ut purus malesuada gravida eget a dolor。Nullam 坐 amet nisi congue, lobortis lacus eu, varius ante。Donec vehicula lacinia elit ac condimentum。Aliquam egestas adipiscing orci in pharetra。Curabitur pulvinar tincidunt neque,非 tempus nisi volutpat eget。Nulla tincidunt egestas libero,pharetra orci ullamcorper 发酵菌。Nulla nec nunc nec urna semper tristique。Proin est dolor, sodales ut pretium eu, luctus a magna。\结束{法律清单}

第四步:要折叠文本,将光标放在包含\begin{wisemonkey}or的代码行上的任意位置,\begin{lawlist}然后键入M-x toggle-block

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


例子
(来源:lawlist.com

例子
(来源:lawlist.com

例子
(来源: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."))))
于 2014-03-31T21:16:03.073 回答
0

我在我的配置文件中得到了这个,你可以试试:

(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))
于 2014-04-03T02:00:18.127 回答