Browse Source

Move code around and make some public functions protected

tags/v0.6.0^2
Damien Cassou 9 months ago
parent
commit
b122d03720
Signed by: DamienCassou <damien@cassou.me> GPG Key ID: B68746238E59B548
1 changed files with 55 additions and 46 deletions
  1. +55
    -46
      navigel.el

+ 55
- 46
navigel.el View File

@@ -72,7 +72,7 @@ OPERATION and ARGS are defined by `tablist-operations-function'."
(cl-case operation
(supported-operations '(find-entry delete))
(find-entry (navigel-open (car args) nil))
(delete (navigel-delete (car args) #'navigel-revert-buffer))))
(delete (navigel-delete (car args) #'navigel--revert-buffer))))

(defun navigel--imenu-extract-index-name ()
"Return the name of entity at point for `imenu'.
@@ -195,7 +195,7 @@ If TARGET is non-nil and is in buffer, move point to it.

By default, list ENTITY's children in a tabulated list.
"
(navigel-list-children entity target))
(navigel--list-children entity target))

(cl-defgeneric navigel-parent-to-open (entity)
"Return an indication of what to open if asked to open the parent of entity at point.
@@ -229,6 +229,9 @@ If non-nil, call CALLBACK with no parameter when done."
If non-nil, call CALLBACK with no parameter when done."
(navigel-async-mapc #'navigel-delete entities callback))

;;; Public functions

(defun navigel-async-mapcar (mapfn list callback)
"Apply MAPFN to each element of LIST and pass result to CALLBACK.

@@ -270,7 +273,51 @@ computing for the all elements of LIST."
list
(lambda (_result) (funcall callback))))

(defun navigel-list-children (entity &optional target)
(defun navigel-open-parent (&optional entity)
"Open in a new buffer the parent of ENTITY, entity at point if nil."
(interactive (list (navigel-entity-at-point)))
(when entity
(pcase (navigel-parent-to-open entity)
(`(,parent . ,entity) (navigel-open parent entity))
(_ (message "No parent to go to")))))

(defun navigel-refresh (&optional target callback)
"Compute `navigel-entity' children and list those in the current buffer.

If TARGET is non-nil and is in buffer, move point to it.

If CALLBACK is non nil, execute it when the buffer has been
refreshed."
(let ((entity navigel-entity)
;; save navigel-app so we can rebind below
(app navigel-app))
(message (if (equal (point-min) (point-max))
"Populating…"
"Refreshing…"))
(navigel-children
entity
(lambda (children)
;; restore navigel-app
(let ((navigel-app app) state)
(with-current-buffer (get-buffer-create (navigel-entity-buffer entity))
(setq state (navigel--save-state))
(setq-local tabulated-list-entries
(mapcar
(lambda (child) (list child (navigel-entity-to-columns child)))
children))
(tabulated-list-print)
(navigel--restore-state state)
(when target
(navigel-go-to-entity target))
(run-hooks 'navigel-changed-hook)
(when callback
(funcall callback))
(message "Ready!")))))))

;;; Private functions

(defun navigel--list-children (entity &optional target)
"Open a new buffer showing ENTITY's children.

If TARGET is non-nil and is in buffer, move point to it.
@@ -291,7 +338,7 @@ is asked for a top level ENTITY."
(setq-local navigel-entity entity)

(setq-local tablist-operations-function #'navigel--tablist-operation-function)
(setq-local revert-buffer-function #'navigel-revert-buffer)
(setq-local revert-buffer-function #'navigel--revert-buffer)
(setq-local imenu-prev-index-position-function
#'navigel--imenu-prev-index-position)
(setq-local imenu-extract-index-name-function
@@ -305,14 +352,6 @@ is asked for a top level ENTITY."
(run-hooks 'navigel-init-done-hook))))
(switch-to-buffer buffer))))

(defun navigel-open-parent (&optional entity)
"Open in a new buffer the parent of ENTITY, entity at point if nil."
(interactive (list (navigel-entity-at-point)))
(when entity
(pcase (navigel-parent-to-open entity)
(`(,parent . ,entity) (navigel-open parent entity))
(_ (message "No parent to go to")))))

(defun navigel--save-state ()
"Return an object representing the state of the current buffer.
This should be restored with `navigel--restore-state'.
@@ -338,43 +377,13 @@ The state contains the entity at point, the column of point, and the marked enti
(when (navigel-go-to-entity entity)
(tablist-put-mark)))))))

(defun navigel-refresh (&optional target callback)
"Compute `navigel-entity' children and list those in the current buffer.

If TARGET is non-nil and is in buffer, move point to it.

If CALLBACK is non nil, execute it when the buffer has been
refreshed."
(let ((entity navigel-entity)
;; save navigel-app so we can rebind below
(app navigel-app))
(message (if (equal (point-min) (point-max))
"Populating…"
"Refreshing…"))
(navigel-children
entity
(lambda (children)
;; restore navigel-app
(let ((navigel-app app) state)
(with-current-buffer (get-buffer-create (navigel-entity-buffer entity))
(setq state (navigel--save-state))
(setq-local tabulated-list-entries
(mapcar
(lambda (child) (list child (navigel-entity-to-columns child)))
children))
(tabulated-list-print)
(navigel--restore-state state)
(when target
(navigel-go-to-entity target))
(run-hooks 'navigel-changed-hook)
(when callback
(funcall callback))
(message "Ready!")))))))

(defun navigel-revert-buffer (&rest _args)
(defun navigel--revert-buffer (&rest _args)
"Compute `navigel-entity' children and list those in the current buffer."
(navigel-refresh))

;;; Major mode

(defvar navigel-tablist-mode-map
(let ((map (make-sparse-keymap)))
(define-key map (kbd "^") #'navigel-open-parent)


Loading…
Cancel
Save