A new tree-buffer - ECB - the Emacs Code Browser
Description: ECB - the Emacs Code Browser

Next: A new tree-node, Previous: Introduction, Up: tree-buffer

#### 11.3.2 How to create a new tree-buffer

The creator-function for a new tree-buffer depends on the fact if the new tree-buffer should be used as an ECB-window or not. For a new ECB-window the macro `defecb-tree-buffer-creator` has to be used, otherwise the function `tree-buffer-create`. In the end both methods use `tree-buffer-create` because the BODY-argument of `defecb-tree-buffer-creator` must contain a call to this function!.

This section describes all arguments of `tree-buffer-create`.

Except the first argument NAME all arguments are key-arguments of the form :arg-name arg-value, so for example a call looks like

       (tree-buffer-create <buffer-name> :frame <frame-object> ...).

These key-arguments (all except the first argument `NAME`) can be arranged in any arbitrary order but all of them are not-optional! The key-arg-name is always a : followed by the lowercase version of the mentioned argument below (e.g. `FRAME` –> :frame, `MOUSE-ACTION-TRIGGER` –> :mouse-action-trigger).

Here is a description of the arguments of `tree-buffer-create` - also available as docstring for this function (via `C-h f`). The description below contains also some examples for complex-arguments!

`NAME`
  Buffername of the new tree-buffer.
FRAME
  Frame in which the tree-buffer is displayed and valid. All key-bindings and interactive functions of the tree-buffer work only if called in FRAME otherwise nothing is done!
MOUSE-ACTION-TRIGGER
  When a mouse-action is triggered. Allowed values: `button-release` and `button-press`.
IS-CLICK-VALID-FN
  `tree-buffer-create` rebinds `mouse-1`, `mouse-2`, `RET` (and `TAB`) and also in combination with shift and control (not with `TAB`). IS-CLICK-VALID-FN is called first if a node or an expand-symbol is clicked. This function is called with five arguments:
  - mouse-button: The clicked mouse-button or RET or TAB (0 = RET or TAB, 1 = mouse-1, 2 = mouse 2)
  - shift-pressed: Non nil if the SHIFT-key was pressed during mouse-click or RET.
  - control-pressed: Non nil if the CONTROL-key was pressed during mouse-click or RET.
  - meta-pressed: Non nil if the META-key was pressed during mouse-click or RET.
  - tree-buffer-name: The buffer-name of the tree-buffer where the node has been clicked.

  The function must return not nil iff exactly this click/hit is accepted. If the function returns nil then really nothing is done by the tree-buffer after this click/hit!

  Here is an example (call `C-h f` to see what it does) for this callback-function:

              (defun ecb-interpret-mouse-click (mouse-button
                                                shift-pressed
                                                control-pressed
                                                meta-pressed
                                                tree-buffer-name)
                (if (eq mouse-button 0)
                    (list (if control-pressed 2 1) shift-pressed meta-pressed
                          'keyboard)
                  (if (and (not (eq mouse-button 1)) (not (eq mouse-button 2)))
              	nil
                    (case ecb-primary-secondary-mouse-buttons
                      (mouse-1--mouse-2
                       (if control-pressed
                           nil
                         (list mouse-button shift-pressed meta-pressed 'mouse)))
                      (mouse-1--C-mouse-1
                       (if (not (eq mouse-button 1))
                           nil
                         (list (if control-pressed 2 1) shift-pressed meta-pressed
                               'mouse)))
                      (mouse-2--C-mouse-2
                       (if (not (eq mouse-button 2))
                           nil
                         (list (if control-pressed 2 1) shift-pressed meta-pressed
                               'mouse)))
                      (otherwise nil)))))

  This example would be passed as parameter as follows:

              (tree-buffer-create "myname"
                                  :is-click-valid-fn 'ecb-interpret-mouse-click
                                  ...)

NODE-SELECTED-FN
  Function to call if a node has been selected. This function is called with the following parameters:
  - node: The selected node
  - mouse-button (0 = RET, 1 = mouse-1, 2 = mouse 2)
  - shift-pressed
  - control-pressed
  - meta-pressed
  - tree-buffer-name

  For the last four arguments see the description above. This function has to ensure that the expandable- and expanded-state of the selected node is correct after returning.

  Here is an example (call `C-h f` to see what it does) for this callback-function:

              (defun ecb-tree-buffer-node-select-callback (node
              					     mouse-button
              					     shift-pressed
              					     control-pressed
                                                           meta-pressed
              					     tree-buffer-name)
                (let* ((ecb-button-list (ecb-interpret-mouse-click mouse-button
              						     shift-pressed
              						     control-pressed
                                                                   meta-pressed
              						     tree-buffer-name))
              	 (ecb-button (nth 0 ecb-button-list))
              	 (shift-mode (nth 1 ecb-button-list))
                       (meta-mode (nth 2 ecb-button-list))
                       (keyboard-p (equal (nth 3 ecb-button-list) 'keyboard))
                       (maximized-p (ecb-buffer-is-maximized-p tree-buffer-name)))
                  ;; now we dispatch to the right action
                  (when ecb-button-list
                    (cond ((ecb-string= tree-buffer-name ecb-directories-buffer-name)
              	     (ecb-directory-clicked node ecb-button nil shift-mode
                                                  meta-mode))
              	    ((ecb-string= tree-buffer-name ecb-sources-buffer-name)
              	     (ecb-source-clicked node ecb-button nil shift-mode
                                               meta-mode))
              	    ((ecb-string= tree-buffer-name ecb-history-buffer-name)
              	     (ecb-history-clicked node ecb-button nil shift-mode
                                                meta-mode))
              	    ((ecb-string= tree-buffer-name ecb-methods-buffer-name)
              	     (ecb-method-clicked node ecb-button nil shift-mode
                                               meta-mode))
              	    ((ecb-string= tree-buffer-name ecb-analyse-buffer-name)
              	     (ecb-analyse-node-clicked node ecb-button nil shift-mode
                                                     meta-mode))
              	    (t nil)))))

  This example would be passed as parameter as follows:

              (tree-buffer-create "myname"
                                  :node-selected-fn 'ecb-tree-buffer-node-select-callback
                                  ...)

  IMPORTANT: This callback must not modify the slot EXPANDED of the passed node because this is done automatically by the tree-buffer-library!

NODE-EXPANDED-FN
  Function to call if a node is expandable, point stays onto the expand-symbol and node is not already expanded. This function is called with the following parameters:
  - node: The selected node
  - mouse-button (0 = TAB, 1 = mouse-1, 2 = mouse 2)
  - shift-pressed
  - control-pressed
  - meta-pressed
  - tree-buffer-name

  This function should add all children nodes to this node if not already done (if possible). This function has to ensure that the expandable- and expanded state of the selected node is correct after returning!

  IMPORTANT: This callback must not modify the slot EXPANDED of the passed node because this is done automatically by the tree-buffer-library!

NODE-COLLAPSED-FN
  Function to call if a node is expandable, point stays onto the expand-symbol and node is already expanded. This function is called with the following parameters:
  - node: The selected node
  - mouse-button (0 = TAB, 1 = mouse-1, 2 = mouse 2)
  - shift-pressed
  - control-pressed
  - meta-pressed
  - tree-buffer-name

  This function is only a callback to inform the owner/user of this tree-buffer that this node has been collapsed. This function must not modify the expandable- or expanded state of the selected node!

  Often a sensefull value for this parameter is the function `ignore`.

  IMPORTANT: This callback must not modify the slot EXPANDED of the passed node because this is done automatically by the tree-buffer-library!

NODE-MOUSE-OVER-FN
  Function to call when the mouse is moved over a node. This function is called with three arguments: NODE, WINDOW, NO-PRINT, each of them related to the current tree-buffer. If NO-PRINT is nil then the function must print the text itself in any manner. This function must always return the text which either is printed by the function itself or by the caller (if NO-PRINT is not nil). The current buffer for this function is the tree-buffer itself. With XEmacs this function is only called if the tree-buffer track-mouse mechanism is activated (see the function `tree-buffer-activate-follow-mouse`). With GNU Emacs >= 21 this function is called by the `help-echo` property added to each node.

  Here is an example (call `C-h f` to see what it does) for this callback-function:

              (defun ecb-mouse-over-analyse-node (node &optional window no-message
                                                       click-force)
                (let ((str (when (or click-force
                                     (ecb-show-minibuffer-info
                                      node window
                                      (car ecb-analyse-show-node-info)))
                             (if (equal (cdr ecb-analyse-show-node-info)
                                        'full-info)
                                 (ecb-analyse-gen-tag-info
                                  (car (tree-node->data node)))
                               (tree-node->name node)))))
                  (prog1 str
                    (unless no-message
                      (ecb-nolog-message str)))))

MOUSE-HIGHLIGHT-FN
  If nil then in this tree-buffer no node is highlighted when the mouse moves over it. If t then each node is highlighted when the mouse moves over it. If a function then it is called with the node as argument and if it returns not nil then the node will be highlighted when the mouse moves over it - otherwise no highlighting takes place.
NODE-DATA-EQUAL-FN
  Function used by the tree-buffer to test if the data of two tree-nodes are equal. The function is called with two args: The DATA-slots of the two tree-nodes (see A new tree-node for details about the data-slots).

  Here is an example (call `C-h f` to see what it does) for this callback-function:

              (defun ecb-analyse-compare-node-data (left right)
                "Return not nil when LEFT and RIGHT are identical node-datas."
                (and (equal (nth 2 left) (nth 2 right))
                     (ecb-compare-methods-buffer-node-data (car left)
                                                           (car right))))

  Often a suitable value for this parameter is `equal`.

MAYBE-EMPTY-NODE-TYPES
  Nil or a list of node-types (a node-type is an integer which must be set with `tree-node-new`). Nodes with one of these types are treated as empty if they are not expandable (i.e. they have no children) and will be displayed with the empty-symbol ([x]); for other nodes see next argument.
LEAF-NODE-TYPES
  Nil or a list of node-types (see above). Nodes with one of these types are treated as leafs and will be displayed with the leaf-symbol (*).

  Summary for MAYBE-EMPTY-NODE-TYPES and LEAF-NODE-TYPES:

  - Expandable nodes will always be displayed either with the open- or with the close-symbol.
  - Not-expandable nodes with a node-type contained in MAYBE-EMPTY-NODE-TYPES will be displayed with the empty-symbol.
  - Not-expandable nodes with a node-type contained in LEAF-NODE-TYPES will be displayed with the leaf-symbol.
  - All other nodes will be displayed with no symbol just with correct indentation.
MENU-CREATOR
  Nil or function which has to return nil or a list of conses, each cons for a known node-type of this tree-buffer (the node-type of a node is an integer). Example: ((0 . menu-for-type-0) (1 . menu-for-type-1)). The cdr of a cons must be a menu in the same format `tree-buffer-create-menu` expects as argument - see the documentation of this function for details. This function gets two arguments: The name of the tree-buffer and the node for which a popup-menu should be opened.

  Here is an example for such a menu-creator-callback:

              (defconst ecb-analyse-nodedata-tag-with-pos 0)
              (defconst ecb-analyse-nodedata-tag-without-pos 1)
              (defconst ecb-analyse-nodedata-no-tag 2)

              (defconst ecb-analyse-nodetype-bucket 0)
              (defconst ecb-analyse-nodetype-context 1)
              (defconst ecb-analyse-nodetype-arguments 2)
              (defconst ecb-analyse-nodetype-completions 3)
              (defconst ecb-analyse-nodetype-localvars 4)
              (defconst ecb-analyse-nodetype-prefix 5)
              (defconst ecb-analyse-nodetype-assignee 6)
              (defconst ecb-analyse-nodetype-function 7)
              (defconst ecb-analyse-nodetype-function-arg 8)

              (defun ecb-analyse-create-menu (node)
                "Return a popup-menu suitable for NODE."
                (let* ((data (tree-node->data node))
                       (tag-p (not (equal (nth 1 data) ecb-analyse-nodedata-no-tag)))
                       (tag-with-pos-p (equal (nth 1 data)
                                              ecb-analyse-nodedata-tag-with-pos))
                       (nodetype (nth 2 data)))
                  (delq nil (list (if (equal nodetype ecb-analyse-nodetype-completions)
                                      '(ecb-analyse-complete "Complete"))
                                  (if tag-p
                                      '(ecb-analyse-show-tag-info "Show tag info"))
                                  (if tag-with-pos-p
                                      '(ecb-analyse-jump-to-tag "Jump to tag"))))))

              (defun ecb-analyse-menu-creator (tree-buffer-name node)
                "Creates the popup-menus for the analyse-buffer."
                (let ((nodetype (tree-node->type node)))
                  (unless (equal nodetype ecb-analyse-nodetype-bucket)
                    (mapcar (function (lambda (type)
                                        (cons type (ecb-analyse-create-menu node))))
                            `(,ecb-analyse-nodetype-context
                              ,ecb-analyse-nodetype-arguments
                              ,ecb-analyse-nodetype-completions
                              ,ecb-analyse-nodetype-localvars
                              ,ecb-analyse-nodetype-prefix
                              ,ecb-analyse-nodetype-assignee
                              ,ecb-analyse-nodetype-function
                              ,ecb-analyse-nodetype-function-arg)))))

  This example would be passed as parameter as follows:

              (tree-buffer-create "myname"
                                  :menu-creator 'ecb-analyse-menu-creator
                                  ...)

MENU-TITLES
  Nil or a list conses, each cons for a node-type. See MENU-CREATOR. The cdr of a cons must be either a string or a function which will be called with current node under point and must return a string which is displayed as the menu-title.
MODELINE-MENU-CREATOR
  Nil or a function which has to return nil or a menu in the same format `tree-buffer-create-menu` expects as argument - see the documentation of this function for details. This function gets one argument: The name of the tree-buffer. If the function returns a menu then this menu will be displayed when the user clicks with mouse-button 3 at the modeline of the tree-buffer. The menu-title will be “Tree-buffer modeline-menu”.
sticky-parent-p
  If not nil then with GNU Emacs >= 21 the tree-buffer displays in its header-line the unvisible parent-node if there is any. sticky-parent-fn is used to get this parent node.
sticky-indent-string
  String used for indendation of the sticky node in the header-line so it matches the tree-display.
sticky-parent-fn
  Function used to get that parent node which should be sticky. This function gets as argument a node and should either return nil (if there is not suitable parent node) or a node. This node will be displayed as sticky in the header-line of the tree-buffer. If nil is returned and sticky-parent-p is not nil then just the node under the header-line is displayed. If this argument is nil and sticky-parent-p is not nil then always the next unvisible parent node will be displayed in the header-line.
TRUNC-LINES
  Should lines in this tree buffer be truncated (not nil).
READ-ONLY
  Should the treebuffer be read-only (not nil).
TREE-INDENT
  Spaces subnodes should be indented. Ignored if TREE-STYLE is `image` (see below).
INCR-SEARCH-P
  Should the incremental search be enabled in the tree-buffer. Three choices: `prefix`, `substring`, `nil`. See the command `tree-buffer-incremental-node-search`.
INCR-SEARCH-ADDITIONAL-PATTERN
  Every search-pattern is prefixed with a regexp to jump over not important stuff of a displayed node-name at incr. search.. This is per default: beginning spaces and guide characters (|`-) and all expand/collapse-buttons [+], [x], rsp. [-]!

  If this argument is not nil then it must be a cons-cell where car is a string which should be a regexp-pattern which is added to the basic-prefix pattern (see above) and both of them prefix the incr-search-pattern. The cdr is the number of subexpressions in this additoonal pattern.

ARROW-NAVIGATION
  If not nil then a smart navigation with arrow keys is offered:
  - Left-arrow: If node is expanded then it will be collapsed otherwise point jumps to the next "higher" node in the hierarchical tree (higher means the next higher tree-level or - if no higher level available - the next higher node on the same level).
  - Right-arrow: If node is not expanded then it will be expanded.

    Onto a not expandable node the horizontal arrow-keys go one character in the senseful correct direction.

  - Up- and down-key: Point jumps to the first character of the previous (up) rsp. next node (down). “First” character means either the first character of the expand-symbol (in case EXPAND-SYMBOL-BEFORE-P is not nil) or of the displayed node-name. Or with other words: The first non-indentation and non-guide-line (see TREE-STYLE) character of a node.
HOR-SCROLL-STEP
  Number of columns a hor. scroll in the tree-buffer should scroll. If not nil then `M-mouse-1` and `M-mouse-2` scroll left and right and also `M-<left-arrow>` and `M-<right-arrow>`. Ignored with XEmacs.
DEFAULT-IMAGES-DIR
  Full path where the default images for the tree-buffer can be found. It should contain an image for every name of `tree-buffer-tree-image-names`.
ADDITIONAL-IMAGES-DIR
  Additional image-dir which should be searched first for images needed for current tree-buffer. If the image can not be found in this directory then DEFAULT-IMAGES-DIR is searched. If the image can't even found here the related ascii-symbol is used.
IMAGE-FILE-PREFIX
  Common prefix for all image-files for this tree-buffer, e.g. “ecb-”.
TREE-STYLE
  There are three different styles available: Image-style (value `image`): Very nice and modern because image-icons are used to display the tree-buffer. For this style the arguments TREE-INDENT and EXPAND-SYMBOL-BEFORE-P have no effect.

  Ascii-style with guide-lines (value `ascii-guides`) and ascii-style without guidelines (value `ascii-no-guides`. See Tree-buffer styles for details about the tree-styles.

  Both ascii-styles are affected by the args TREE-INDENT and EXPAND-SYMBOL-BEFORE-P.

ASCII-GUIDE-FACE
  If TREE-STYLE is `ascii-guides` then this defines the face the guides should be displayed with.
TYPE-FACER`:`
  Nil or a list of one or more conses, each cons for a node-type (a node-type is an integer which must be set with `tree-node-new`). The cdr of a cons can be:
  - a face-symbol
  - a function-symbol which gets two arguments (see `tree-buffer-insert-text`). This function can do anything, but normally it should face a tree-node.
  - the symbol t. Then the tree-buffer assumes that the node-text is already faced and therefore it does not face the node, means it does nothing then inserting the node-text, if the tree-buffer is updated.
EXPAND-SYMBOL-BEFORE-P
  If not nil then the expand-symbol is displayed before the node-text. Ignored when TREE-STYLE is `image` and Emacs can display images.
HIGHLIGHT-NODE-FACE
  Face used for highlighting current selected node in this tree-buffer.
GENERAL-FACE
  General face in which the whole tree-buffer should be displayed.
AFTER-CREATE-HOOK`:`
  A function or a list of functions (with no arguments) called directly after creating the tree-buffer and defining it's local keymap. For example such a function can add additional key-bindings for this tree-buffer local keymap (use `local-set-key` for this).

  Here is an example for such a hook:

              (defun ecb-common-after-tree-buffer-create-actions ()
                "Things which should be performed after creating a tree-buffer.
              The tree-buffer is the current buffer."
                (local-set-key (kbd "C-t")
                               'ecb-toggle-do-not-leave-window-after-select)
                (if ecb-running-xemacs
                    (define-key modeline-map
                      '(button2up)
                      'ecb-toggle-maximize-ecb-window-with-mouse)
                  (local-set-key [mode-line mouse-2]
                                 'ecb-toggle-maximize-ecb-window-with-mouse)))

AFTER-UPDATE-HOOK`:`
  A function or a list of functions (with no arguments) called each time after the tree-buffer has been updated via `tree-buffer-update`.

Here is an example how to create a tree-buffer (if you want a tree-buffer not for ECB then just strip off the `defecb-tree-buffer-creator` and just call `tree-buffer-create`):

       (defecb-tree-buffer-creator ecb-create-analyse-tree-buffer
           ecb-analyse-buffer-name
         "Create the tree-buffer for analyse-display."
         (tree-buffer-create
          ecb-analyse-buffer-name
          :frame ecb-frame
          :mouse-action-trigger ecb-tree-mouse-action-trigger
          :is-click-valid-fn 'ecb-interpret-mouse-click
          :node-selected-fn 'ecb-tree-buffer-node-select-callback
          :node-expanded-fn 'ecb-tree-buffer-node-expand-callback
          :node-collapsed-fn 'ecb-tree-buffer-node-collapsed-callback
          :node-mouse-over-fn 'ecb-mouse-over-analyse-node
          :mouse-highlight-fn 'ecb-analyse-node-mouse-highlighted-p
          :node-data-equal-fn 'ecb-analyse-compare-node-data
          :maybe-empty-node-types nil
          :leaf-node-types nil
          :menu-creator 'ecb-analyse-menu-creator
          :menu-titles (ecb-analyse-gen-menu-title-creator)
          :modeline-menu-creator 'ecb-common-tree-buffer-modeline-menu-creator
          :trunc-lines (ecb-member-of-symbol/value-list
                        ecb-analyse-buffer-name
                        ecb-tree-truncate-lines)
          :read-only t
          :tree-indent ecb-tree-indent
          :incr-search-p t
          :incr-search-additional-pattern nil
          :arrow-navigation ecb-tree-navigation-by-arrow
          :hor-scroll-step ecb-tree-easy-hor-scroll
          :default-images-dir (car ecb-tree-image-icons-directories)
          :additional-images-dir (ecb-member-of-symbol/value-list
                                  ecb-analyse-buffer-name
                                  (cdr ecb-tree-image-icons-directories)
                                  'car 'cdr)
          :image-file-prefix "ecb-"
          :tree-style ecb-tree-buffer-style
          :ascii-guide-face ecb-tree-guide-line-face
          :type-facer nil
          :expand-symbol-before-p ecb-tree-expand-symbol-before
          :highlight-node-face ecb-analyse-face
          :general-face ecb-analyse-general-face
          :after-create-hook (append
                              (list (lambda ()
                                      (ecb-common-after-create-actions)))
                              ecb-common-tree-buffer-after-create-hook
                              ecb-analyse-buffer-after-create-hook)
          :after-update-hook nil))