问题
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