Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

narrow mode fixes #15

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 64 additions & 26 deletions org-ol-tree.el
Original file line number Diff line number Diff line change
Expand Up @@ -1009,6 +1009,9 @@ The argument EVENT, is the same event received by the
(goto-char (posn-point (cadr event)))
(goto-char (point-at-bol))

(when (eq (org-ol-tree--util-line-length) 0)
(keyboard-quit))

(when (region-active-p)
(keyboard-quit))

Expand Down Expand Up @@ -1038,6 +1041,8 @@ This function cancels any timer call from `org-ol-tree-action--leftclick'."
(goto-char (point-at-bol))
(when (region-active-p)
(keyboard-quit))
(when (eq (org-ol-tree--util-line-length) 0)
(keyboard-quit))
(org-ol-tree-action--visit)))


Expand Down Expand Up @@ -1078,30 +1083,40 @@ state is collapsed, the parent not will be selected."
If the cursor is not on top of an expanded section, calling this function has no
effect."
(interactive)
(when (eq (org-ol-tree--util-line-length) 0)
(keyboard-quit))

(pcase (org-ol-tree-core--node-get :state)
('treemacs-org-ol-doc-open-state (treemacs-collapse-org-ol-doc))
('treemacs-org-ol-parent-section-open-state (treemacs-collapse-org-ol-parent-section))))



(defun org-ol-tree-action--expand ()
"Expand an expandable section that is currently collapsed.

If the cursor is not on top of a collapsed section, calling this function has no
effect."
(interactive)
(when (eq (org-ol-tree--util-line-length) 0)
(keyboard-quit))

(pcase (org-ol-tree-core--node-get :state)
('treemacs-org-ol-doc-closed-state (treemacs-expand-org-ol-doc))
('treemacs-org-ol-parent-section-closed-state (treemacs-expand-org-ol-parent-section))))



(defun org-ol-tree-action--move-to (target-point)
"Move the cursor to TARGET-POINT and scroll point to top.

If the buffer is narrowed, it will get widen as a side effect of this function."
(widen)
(goto-char target-point)
(org-reveal)
(+org/toggle-fold)
(org-show-entry)
(org-show-children)
(recenter (min (max 0 scroll-margin) (truncate (/ (window-body-height) 4.0))) t))


Expand Down Expand Up @@ -1246,19 +1261,24 @@ The file watched is always `org-ol-tree--org-buffer'.

For more information on EVENT, check the documentation of
`file-notify-add-watch'."
(cl-multiple-value-bind (descriptor action file) event
(when (member action '(renamed changed))
(let ((ol-buffer (ht-get org-ol-tree-action--watcher-buffers descriptor))
(current-window (selected-window)))
(if (and ol-buffer (buffer-live-p ol-buffer))
(progn
(when (eq (org-ol-tree-ui--visibility) 'visible)
(select-window (get-buffer-window ol-buffer))
(org-ol-tree-action--refresh)
(select-window current-window)))
(ht-remove! org-ol-tree-action--watcher-buffers descriptor)
(ht-remove! org-ol-tree-action--buffer-watchers ol-buffer)
(file-notify-rm-watch descriptor))))))

(save-restriction
(widen)
(save-excursion
(cl-multiple-value-bind (descriptor action file) event
(when (member action '(renamed changed))
(let ((ol-buffer (ht-get org-ol-tree-action--watcher-buffers descriptor))
(current-window (selected-window)))
(if (and ol-buffer (buffer-live-p ol-buffer))
(progn
(when (eq (org-ol-tree-ui--visibility) 'visible)
(select-window (get-buffer-window ol-buffer))
(org-ol-tree-action--refresh)
(select-window current-window)))
(ht-remove! org-ol-tree-action--watcher-buffers descriptor)
(ht-remove! org-ol-tree-action--buffer-watchers ol-buffer)
(file-notify-rm-watch descriptor))))))))



(defun org-ol-tree-action--start-watching-buffer ()
Expand Down Expand Up @@ -1469,6 +1489,21 @@ With a prefix ARG call `org-ol-tree-ui--kill-buffer' instead."

;;;; --- Commands

;;;; --- Utils

(defun org-ol-tree--util-line-length (&optional n)
"Length of the Nth line."
(if (not n)
(setq n (line-number-at-pos)))

(save-excursion
(goto-char (point-min))
(if (zerop (forward-line (1- n)))
(setq line-length-of-entry (- (line-end-position)
(line-beginning-position)))))
(princ line-length-of-entry))


;;;###autoload
(defun org-ol-tree ()
"Initialise or toggle org-ol-tree.
Expand All @@ -1480,19 +1515,22 @@ With a prefix ARG call `org-ol-tree-ui--kill-buffer' instead."
- If no org-ol-tree buffer exists for the current Org-file buffer create and
show it."
(interactive)
(unless (or org-ol-tree--buffer-p (buffer-live-p org-ol-tree--buffer) (eq major-mode 'org-mode))
(user-error "Org Outline Tree can only be used with Org buffers"))
(pcase (org-ol-tree-ui--visibility)
('visible
(if org-ol-tree--buffer-p
(delete-window (org-ol-tree-ui--get-window))
(org-ol-tree-ui--setup-window nil)))
('exists
(org-ol-tree-ui--setup-buffer)
(org-ol-tree-ui--setup-window t)
(org-ol-tree-action--refresh))
('none
(org-ol-tree-action--init))))
(save-restriction
(widen)
(save-excursion
(unless (or org-ol-tree--buffer-p (buffer-live-p org-ol-tree--buffer) (eq major-mode 'org-mode))
(user-error "Org Outline Tree can only be used with Org buffers"))
(pcase (org-ol-tree-ui--visibility)
('visible
(if org-ol-tree--buffer-p
(delete-window (org-ol-tree-ui--get-window))
(org-ol-tree-ui--setup-window nil)))
('exists
(org-ol-tree-ui--setup-buffer)
(org-ol-tree-ui--setup-window t)
(org-ol-tree-action--refresh))
('none
(org-ol-tree-action--init))))))



Expand Down