How to easily outline arbitrary (data) structures in text?

亡梦爱人 提交于 2019-12-13 08:29:38

问题


So I came across this:

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
        urls.py
        wsgi.py

and this:

root --+---> child1
       +---> child2 --+--> subchild1
       |              +--> subchild2
       +---> child3

and was wondering what tool(editor packages/bundle etc) does one use in order to outline and edit such structures easily/programmatically.

PS: My preference is towards emacs but for the sake of completeness I will appreciate if other editors/tools were included in the answer.


回答1:


The second example is clearly more complex, and I'm not going to address it, but outline-mode/minor-mode is going to work nicely for the indentation-based tree, so here's an approach for that, with outline headings based on an arbitrary repeated prefix string (four spaces in this example, but configurable as required).

If you load/evaluate the code below, and then load the data file (with the local variables included), then you can hide/show branches with SHIFT+TAB and promote/demote branches with SHIFT+LEFT and SHIFT+RIGHT (cursor keys). All the other outline-minor-mode functionality is also available, of course.

mysite/
    manage.py
    mysite/
        __init__.py
        settings.py
            urls.py
        wsgi.py

;;; Local Variables:
;;; my-outline-prefix: "    "
;;; eval: (my-outline-minor-mode 1)
;;; End:
(global-set-key (kbd "<S-tab>") 'outline-toggle-children)
(global-set-key (kbd "<S-left>") 'outline-promote)
(global-set-key (kbd "<S-right>") 'outline-demote)

(defvar my-outline-prefix "    "
  "Prefix string denoting a single outline level for `my-outline-minor-mode'.")

(defvar my-outline-max-level 20
  "Maximum number of levels for `my-outline-minor-mode'.")

(define-minor-mode my-outline-minor-mode
  "Outline levels based on repetitions of `my-outline-prefix'."
  0 nil nil
  (outline-minor-mode 1)
  (setq-local outline-level 'my-outline-level)
  (setq-local outline-regexp (format "\\(%s\\)*" 
                                     (regexp-quote my-outline-prefix)))
  (setq outline-heading-alist '())
  (let ((level 0)
        (level-prefix ""))
    (while (< level my-outline-max-level)
      (setq outline-heading-alist (cons (cons level-prefix level) 
                                        outline-heading-alist)
            level (1+ level)
            level-prefix (concat level-prefix my-outline-prefix)))
    (setq outline-heading-alist (nreverse outline-heading-alist))))

(defun my-outline-level ()
  "Counts how many times `my-outline-prefix' appears at the start of the line."
  (let* ((data (match-data))
         (start (car data))
         (end (cadr data))
         (indent (- end start)))
    (/ indent (length my-outline-prefix))))



回答2:


As for the second example, with Emacs you could jump from node to node using M-x search-forward-regexp RET \(-*\)\(\+\)\(-+>\)

Than use something like narrow-to-region. Beside narrowing Emacs provides commands to hide stuff.

With some lisp based on this node-detection, you may read in a list with data found, etc.



来源:https://stackoverflow.com/questions/16479425/how-to-easily-outline-arbitrary-data-structures-in-text

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!