Skip to content

Commit

Permalink
No longer define magit-section's hook variables as custom options
Browse files Browse the repository at this point in the history
Instead define them as regular variables and add the default hook
functions using `add-hook'.

We need to do this so that we can distribute `magit-section' as a
standalone package, which cannot use function that are defined in
`magit's on its hooks--they might not be defined since it is now
possible to load `magit-section' without loading `magit'.
  • Loading branch information
tarsius committed Jan 23, 2020
1 parent 1f79b3d commit 89c1394
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 60 deletions.
30 changes: 22 additions & 8 deletions Documentation/magit.org
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
#+TEXINFO_DIR_CATEGORY: Emacs
#+TEXINFO_DIR_TITLE: Magit: (magit).
#+TEXINFO_DIR_DESC: Using Git from Emacs with Magit.
#+SUBTITLE: for version 2.90.1 (v2.90.1-880-gf63077242+1)
#+SUBTITLE: for version 2.90.1 (v2.90.1-895-g1f79b3d77+1)

#+TEXINFO_DEFFN: t
#+OPTIONS: H:4 num:3 toc:2
Expand All @@ -25,7 +25,7 @@ directly from within Emacs. While many fine Git clients exist, only
Magit and Git itself deserve to be called porcelains.

#+TEXINFO: @noindent
This manual is for Magit version 2.90.1 (v2.90.1-880-gf63077242+1).
This manual is for Magit version 2.90.1 (v2.90.1-895-g1f79b3d77+1).

#+BEGIN_QUOTE
Copyright (C) 2015-2020 Jonas Bernoulli <[email protected]>
Expand Down Expand Up @@ -921,6 +921,16 @@ the following commands.
The above commands all call the hook ~magit-section-movement-hook~.
Any of the functions listed below can be used as members of this hook.

You might want to remove some of the functions that Magit adds using
~add-hook~. In doing so you have to make sure you do not attempt to
remove function that haven't even been added yet, for example:

#+BEGIN_SRC emacs-lisp
(with-eval-after-load 'magit-diff
(remove-hook 'magit-section-movement-hook
'magit-hunk-set-window-start))
#+END_SRC

- Variable: magit-section-movement-hook

This hook is run by all of the above movement commands, after
Expand All @@ -930,7 +940,9 @@ Any of the functions listed below can be used as members of this hook.

This hook function ensures that the beginning of the current section
is visible, provided it is a ~hunk~ section. Otherwise, it does
nothing. This function is a member of the hook's default value.
nothing.

Loading ~magit-diff~ adds this function to the hook.

- Function: magit-section-set-window-start

Expand All @@ -943,15 +955,17 @@ Any of the functions listed below can be used as members of this hook.

This hook function only has an effect in log buffers, and ~point~ is
on the "show more" section. If that is the case, then it doubles
the number of commits that are being shown. This function is a
member of the hook's default value.
the number of commits that are being shown.

Loading ~magit-log~ adds this function to the hook.

- Function: magit-log-maybe-update-revision-buffer

When moving inside a log buffer, then this function updates the
revision buffer, provided it is already being displayed in another
window of the same frame. This function is a member of the hook's
default value.
window of the same frame.

Loading ~magit-log~ adds this function to the hook.

- Function: magit-log-maybe-update-blob-buffer

Expand Down Expand Up @@ -1067,7 +1081,7 @@ useful. But your mileage may vary.

When a buffer is first created then some sections are shown expanded
while others are not. This is hard coded. When a buffer is refreshed
then the previous visibility is preserved. The initial visibility of
then the previous visibility is preserved. The initial visibility of
certain sections can also be overwritten using the hook
~magit-section-set-visibility-hook~.

Expand Down
30 changes: 22 additions & 8 deletions Documentation/magit.texi
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ General Public License for more details.
@finalout
@titlepage
@title Magit User Manual
@subtitle for version 2.90.1 (v2.90.1-880-gf63077242+1)
@subtitle for version 2.90.1 (v2.90.1-895-g1f79b3d77+1)
@author Jonas Bernoulli
@page
@vskip 0pt plus 1filll
Expand All @@ -53,7 +53,7 @@ directly from within Emacs. While many fine Git clients exist, only
Magit and Git itself deserve to be called porcelains.

@noindent
This manual is for Magit version 2.90.1 (v2.90.1-880-gf63077242+1).
This manual is for Magit version 2.90.1 (v2.90.1-895-g1f79b3d77+1).

@quotation
Copyright (C) 2015-2020 Jonas Bernoulli <jonas@@bernoul.li>
Expand Down Expand Up @@ -1358,6 +1358,16 @@ Move to the beginning of the parent of the current section.
The above commands all call the hook @code{magit-section-movement-hook}.
Any of the functions listed below can be used as members of this hook.

You might want to remove some of the functions that Magit adds using
@code{add-hook}. In doing so you have to make sure you do not attempt to
remove function that haven't even been added yet, for example:

@lisp
(with-eval-after-load 'magit-diff
(remove-hook 'magit-section-movement-hook
'magit-hunk-set-window-start))
@end lisp

@defvar magit-section-movement-hook

This hook is run by all of the above movement commands, after
Expand All @@ -1368,7 +1378,9 @@ arriving at the destination.

This hook function ensures that the beginning of the current section
is visible, provided it is a @code{hunk} section. Otherwise, it does
nothing. This function is a member of the hook's default value.
nothing.

Loading @code{magit-diff} adds this function to the hook.
@end defun

@defun magit-section-set-window-start
Expand All @@ -1383,16 +1395,18 @@ variant in turn.

This hook function only has an effect in log buffers, and @code{point} is
on the "show more" section. If that is the case, then it doubles
the number of commits that are being shown. This function is a
member of the hook's default value.
the number of commits that are being shown.

Loading @code{magit-log} adds this function to the hook.
@end defun

@defun magit-log-maybe-update-revision-buffer

When moving inside a log buffer, then this function updates the
revision buffer, provided it is already being displayed in another
window of the same frame. This function is a member of the hook's
default value.
window of the same frame.

Loading @code{magit-log} adds this function to the hook.
@end defun

@defun magit-log-maybe-update-blob-buffer
Expand Down Expand Up @@ -1554,7 +1568,7 @@ Toggle visibility of bodies of children of the current section.

When a buffer is first created then some sections are shown expanded
while others are not. This is hard coded. When a buffer is refreshed
then the previous visibility is preserved. The initial visibility of
then the previous visibility is preserved. The initial visibility of
certain sections can also be overwritten using the hook
@code{magit-section-set-visibility-hook}.

Expand Down
5 changes: 5 additions & 0 deletions lisp/magit-diff.el
Original file line number Diff line number Diff line change
Expand Up @@ -2244,6 +2244,8 @@ section or a child thereof."
magit-diff-expansion-threshold)
'hide))

(add-hook 'magit-section-set-visibility-hook #'magit-diff-expansion-threshold)

;;; Revision Mode

(define-derived-mode magit-revision-mode magit-diff-mode "Magit Rev"
Expand Down Expand Up @@ -2768,6 +2770,9 @@ actually a `diff' but a `diffstat' section."

;;; Diff Highlight

(add-hook 'magit-section-unhighlight-hook #'magit-diff-unhighlight)
(add-hook 'magit-section-highlight-hook #'magit-diff-highlight)

(defun magit-diff-unhighlight (section selection)
"Remove the highlighting of the diff-related SECTION."
(when (magit-hunk-section-p section)
Expand Down
4 changes: 4 additions & 0 deletions lisp/magit-log.el
Original file line number Diff line number Diff line change
Expand Up @@ -1289,6 +1289,8 @@ exists mostly for backward compatibility reasons."
(forward-line -1)
(magit-section-forward)))

(add-hook 'magit-section-movement-hook #'magit-log-maybe-show-more-commits)

(defvar magit--update-revision-buffer nil)

(defun magit-log-maybe-update-revision-buffer (&optional _)
Expand All @@ -1297,6 +1299,8 @@ If there is no revision buffer in the same frame, then do nothing."
(when (derived-mode-p 'magit-log-mode 'magit-cherry-mode 'magit-reflog-mode)
(magit--maybe-update-revision-buffer)))

(add-hook 'magit-section-movement-hook #'magit-log-maybe-update-revision-buffer)

(defun magit--maybe-update-revision-buffer ()
(when-let ((commit (magit-section-value-if 'commit))
(buffer (magit-get-mode-buffer 'magit-revision-mode nil t)))
Expand Down
58 changes: 14 additions & 44 deletions lisp/magit-section.el
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@
(defgroup magit-section nil
"Expandable sections."
:link '(info-link "(magit)Sections")
:group 'magit)
:group 'extensions)

(defcustom magit-section-show-child-count t
"Whether to append the number of children to section headings.
Expand All @@ -52,63 +52,31 @@ This only applies to sections for which doing so makes sense."
:group 'magit-section
:type 'boolean)

(defcustom magit-section-movement-hook
'(magit-hunk-set-window-start
magit-log-maybe-update-revision-buffer
magit-log-maybe-show-more-commits)
(defvar magit-section-movement-hook nil
"Hook run by `magit-section-goto'.
That function in turn is used by all section movement commands."
:package-version '(magit . "2.3.0")
:group 'magit-section
:type 'hook
:options '(magit-hunk-set-window-start
magit-status-maybe-update-revision-buffer
magit-status-maybe-update-stash-buffer
magit-status-maybe-update-blob-buffer
magit-log-maybe-update-revision-buffer
magit-log-maybe-update-blob-buffer
magit-log-maybe-show-more-commits
magit-stashes-maybe-update-stash-buffer))

(defcustom magit-section-highlight-hook
'(magit-diff-highlight
magit-section-highlight
That function in turn is used by all section movement commands.")

(defvar magit-section-highlight-hook
'(magit-section-highlight
magit-section-highlight-selection)
"Functions used to highlight the current section.
Each function is run with the current section as only argument
until one of them returns non-nil."
:package-version '(magit . "2.1.0")
:group 'magit-section
:type 'hook
:options '(magit-diff-highlight
magit-section-highlight
magit-section-highlight-selection))
until one of them returns non-nil.")

(defcustom magit-section-unhighlight-hook
'(magit-diff-unhighlight)
(defvar magit-section-unhighlight-hook nil
"Functions used to unhighlight the previously current section.
Each function is run with the current section as only argument
until one of them returns non-nil. Most sections are properly
unhighlighted without requiring a specialized unhighlighter,
diff-related sections being the only exception."
:package-version '(magit . "2.1.0")
:group 'magit-section
:type 'hook
:options '(magit-diff-unhighlight))
diff-related sections being the only exception.")

(defcustom magit-section-set-visibility-hook
'(magit-diff-expansion-threshold
magit-section-cached-visibility)
(defvar magit-section-set-visibility-hook
'(magit-section-cached-visibility)
"Hook used to set the initial visibility of a section.
Stop at the first function that returns non-nil. The returned
value should be `show', `hide' or nil. If no function returns
non-nil, determine the visibility as usual, i.e. use the
hardcoded section specific default (see `magit-insert-section')."
:package-version '(magit . "2.4.0")
:group 'magit-section
:type 'hook
:options '(magit-diff-expansion-threshold
magit-section-cached-visibility))
hardcoded section specific default (see `magit-insert-section').")

(defcustom magit-section-cache-visibility t
"Whether to cache visibility of sections.
Expand Down Expand Up @@ -591,6 +559,8 @@ It the SECTION has a different type, then do nothing."
(when (magit-hunk-section-p section)
(magit-section-set-window-start section)))

(add-hook 'magit-section-movement-hook #'magit-hunk-set-window-start)

(defmacro magit-define-section-jumper (name heading type &optional value)
"Define an interactive function to go some section.
Together TYPE and VALUE identify the section.
Expand Down

0 comments on commit 89c1394

Please sign in to comment.