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

Avoid unnecessary work to improve performance #1830

Merged
merged 14 commits into from
Apr 29, 2015
Merged
Show file tree
Hide file tree
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
2 changes: 1 addition & 1 deletion magit-commit.el
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ an error while using those is harder to recover from."
;;; Code

;;;###autoload (autoload 'magit-commit-popup "magit-commit" nil t)
(with-no-warnings ; quiet 24.4 byte-compiler
(with-no-warnings ; quiet byte-compiler
(magit-define-popup magit-commit-popup
"Popup console for commit commands."
'magit-commands
Expand Down
268 changes: 155 additions & 113 deletions magit-diff.el

Large diffs are not rendered by default.

51 changes: 45 additions & 6 deletions magit-mode.el
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,18 @@
:group 'magit-modes
:type 'string)

(defcustom magit-region-highlight-hook
'(magit-section-update-region magit-diff-update-hunk-region)
"Functions used to highlight the region.
Each function is run with the current section as only argument
until one of them returns non-nil. When multiple sections are
selected, then this hook does not run and the region is not
displayed. Otherwise fall back to regular region highlighting."
:package-version '(magit . "2.1.0")
:group 'magit-modes
:type 'hook
:options '(magit-section-update-region magit-diff-update-hunk-region))

(define-minor-mode magit-auto-revert-mode
"Toggle global Magit-Auto-Revert mode.
With prefix ARG, enable Magit-Auto-Revert mode if ARG is positive;
Expand Down Expand Up @@ -258,15 +270,30 @@ Magit is documented in info node `(magit)'."
(push (cons 'keymap t) text-property-default-nonsticky)
(push (cons 'invisible t) text-property-default-nonsticky)
(add-hook 'post-command-hook #'magit-post-command-adjust-point t t)
(add-hook 'post-command-hook #'magit-section-update-highlight t t))
(add-hook 'post-command-hook #'magit-section-update-highlight t t)
(setq-local redisplay-highlight-region-function 'magit-highlight-region)
(setq-local redisplay-unhighlight-region-function 'magit-unhighlight-region))

(defun magit-post-command-adjust-point ()
(when (and (get-text-property (point) 'invisible)
(not (if (fboundp 'get-pos-property) ; since 24.4, see #1671
(get-pos-property (point) 'invisible)
(get-text-property (1+ (point)) 'invisible))))
(not (get-pos-property (point) 'invisible)))
(goto-char (next-single-char-property-change (point) 'invisible))))

(defvar-local magit-region-overlays nil)

(defun magit-highlight-region (start end window rol)
(mapc #'delete-overlay magit-region-overlays)
(if (run-hook-with-args-until-success 'magit-region-highlight-hook
(magit-current-section))
(funcall (default-value 'redisplay-unhighlight-region-function) rol)
(funcall (default-value 'redisplay-highlight-region-function)
start end window rol)))

(defun magit-unhighlight-region (rol)
(setq magit-section-highlighted-section nil)
(mapc #'delete-overlay magit-region-overlays)
(funcall (default-value 'redisplay-unhighlight-region-function) rol))

(defvar-local magit-refresh-function nil)
(put 'magit-refresh-function 'permanent-local t)

Expand Down Expand Up @@ -441,10 +468,16 @@ tracked in the current repository."
(defvar magit-refresh-buffer-hook nil
"Hook run after refreshing a file-visiting buffer.")

(defvar magit-refresh-verbose nil)
(defvar-local magit-refresh-start-time nil)

(defun magit-refresh-buffer ()
"Refresh the current Magit buffer.
Uses the buffer-local `magit-refresh-function'."
(setq magit-refresh-start-time (current-time))
(when magit-refresh-function
(when magit-refresh-verbose
(message "Refreshing %s..." (current-buffer)))
(let* ((buffer (current-buffer))
(windows
(--mapcat (with-selected-window it
Expand All @@ -459,7 +492,9 @@ Uses the buffer-local `magit-refresh-function'."
(list (selected-window))))))
(deactivate-mark)
(setq magit-section-highlight-overlays nil
magit-section-highlighted-sections nil)
magit-section-highlighted-section nil
magit-section-highlighted-sections nil
magit-section-unhighlight-sections nil)
(let ((inhibit-read-only t))
(erase-buffer)
(save-excursion
Expand All @@ -471,7 +506,11 @@ Uses the buffer-local `magit-refresh-function'."
(apply #'magit-section-goto-successor (cdr window)))))
(run-hooks 'magit-refresh-buffer-hook)
(magit-section-update-highlight)
(set-buffer-modified-p nil))))
(set-buffer-modified-p nil))
(when magit-refresh-verbose
(message "Refreshing %s...done (%.3fs)" (current-buffer)
(float-time (time-subtract (current-time)
magit-refresh-start-time))))))

(defun magit-revert-buffers (&optional force)
"Revert unmodified file-visiting buffers of the current repository.
Expand Down
2 changes: 1 addition & 1 deletion magit-pkg.el
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
(define-package "magit" "2.0.50"
"Control Git from Emacs"
'((emacs "24.2")
'((emacs "24.4")
(dash "2.10.0")))
2 changes: 1 addition & 1 deletion magit-popup.el
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
;; Author: Jonas Bernoulli <jonas@bernoul.li>
;; Maintainer: Jonas Bernoulli <jonas@bernoul.li>

;; Package-Requires: ((emacs "24") (cl-lib "0.5") (dash "2.10.0"))
;; Package-Requires: ((emacs "24.4") (cl-lib "0.5") (dash "2.10.0"))
;; Homepage: https://github.com/magit/magit

;; Magit is free software; you can redistribute it and/or modify it
Expand Down
87 changes: 60 additions & 27 deletions magit-section.el
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,18 @@ diff-related sections being the only exception."
:type 'hook
:options '(magit-diff-unhighlight))

(defcustom magit-section-set-visibility-hook
'(magit-diff-expansion-threshold magit-revision-set-visibility)
"Hook used to set the initial visibility of a section.
Stop at the first function that returns non-nil. The value
should be `show' or `hide'. 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.1.0")
:group 'magit-section
:type 'hook
:options '(magit-diff-expansion-threshold magit-revision-set-visibility))

(defface magit-section-highlight
'((((class color) (background light)) :background "grey85")
(((class color) (background dark)) :background "grey20"))
Expand Down Expand Up @@ -259,10 +271,12 @@ With a prefix argument also expand it." title)
(let ((inhibit-read-only t)
(magit-insert-section--parent section))
(save-excursion
(goto-char (magit-section-end section))
(setf (magit-section-content section) (point-marker))
(funcall washer)
(setf (magit-section-end section) (point-marker))))
(if (magit-section-content section)
(funcall washer section) ; already partially washed (hunk)
(goto-char (magit-section-end section))
(setf (magit-section-content section) (point-marker))
(funcall washer)
(setf (magit-section-end section) (point-marker)))))
(magit-section-update-highlight))
(-when-let (beg (magit-section-content section))
(let ((inhibit-read-only t))
Expand Down Expand Up @@ -396,6 +410,14 @@ hidden."
(and (magit-section-content section)
(magit-section-hidden section))))

(defun magit-section-invisible-p (section)
"Return t if the SECTION's body is invisible.
When the body of an ancestor of SECTION is collapsed then
SECTION's body (and heading) obviously cannot be visible."
(or (magit-section-hidden section)
(--when-let (magit-section-parent section)
(magit-section-invisible-p it))))

(defun magit-section-show-level (level)
"Show surrounding sections up to LEVEL.
If LEVEL is negative show up to the absolute value.
Expand Down Expand Up @@ -554,12 +576,6 @@ at point."
(defvar magit-insert-section--parent nil "For internal use only.")
(defvar magit-insert-section--oldroot nil "For internal use only.")

(defvar magit-section-set-visibility-hook '(magit-diff-set-visibility)
"Hook used to set the initial visibility of a section.
Stop at the first function that returns non-nil. The value
should be `show' or `hide'. If no function returns non-nil
determine the visibility as usual (see `magit-insert-section').")

(defmacro magit-insert-section (&rest args)
"Insert a section at point.

Expand Down Expand Up @@ -767,33 +783,41 @@ found in STRING."
;;; Update

(defvar-local magit-section-highlight-overlays nil)
(defvar-local magit-section-highlighted-section nil)
(defvar-local magit-section-highlighted-sections nil)
(defvar-local magit-section-unhighlight-sections nil)

(defun magit-section-update-region (_)
;; Don't show complete region. Highlighting emphasizes headings.
(magit-region-sections))

(defun magit-section-update-highlight ()
(let ((inhibit-read-only t)
(deactivate-mark nil)
(section (magit-current-section)))
(mapc 'delete-overlay magit-section-highlight-overlays)
(setq magit-section-unhighlight-sections
magit-section-highlighted-sections
magit-section-highlighted-sections nil)
(magit-face-remap-set-base 'region)
(unless (eq section magit-root-section)
(run-hook-with-args-until-success
'magit-section-highlight-hook section (magit-region-sections)))
(mapc (apply-partially 'run-hook-with-args-until-success
'magit-section-unhighlight-hook)
magit-section-unhighlight-sections)
(restore-buffer-modified-p nil)))
(let ((section (magit-current-section)))
(unless (eq section magit-section-highlighted-section)
(let ((inhibit-read-only t)
(deactivate-mark nil)
(selection (magit-region-sections)))
(mapc #'delete-overlay magit-section-highlight-overlays)
(setq magit-section-unhighlight-sections
magit-section-highlighted-sections
magit-section-highlighted-sections nil)
(unless (eq section magit-root-section)
(run-hook-with-args-until-success
'magit-section-highlight-hook section selection))
(--each magit-section-unhighlight-sections
(run-hook-with-args-until-success
'magit-section-unhighlight-hook it selection))
(restore-buffer-modified-p nil)
(unless (eq magit-section-highlighted-section section)
(setq magit-section-highlighted-section
(unless (magit-section-hidden section) section)))))))

(defun magit-section-highlight (section siblings)
"Highlight SECTION and if non-nil all SIBLINGS.
This function works for any section but produces undesirable
effects for diff related sections, which by default are
highlighted using `magit-diff-highlight'."
(cond (siblings
(magit-face-remap-set-base 'region 'face-override-spec)
(magit-section-make-overlay (magit-section-start (car siblings))
(magit-section-end (car (last siblings)))
'magit-section-highlight)
Expand All @@ -808,7 +832,8 @@ highlighted using `magit-diff-highlight'."
'magit-section-highlight))))

(defun magit-section-make-overlay (start end face)
(let ((ov (magit-put-face-property start end face)))
(let ((ov (make-overlay start end nil t)))
(overlay-put ov 'face face)
(overlay-put ov 'evaporate t)
(push ov magit-section-highlight-overlays)
ov))
Expand Down Expand Up @@ -852,6 +877,14 @@ highlighted using `magit-diff-highlight'."

;;; Utilities

(cl-defun magit-section-selected-p (section &optional (selection nil sselection))
(or (eq section (magit-current-section))
(memq section (if sselection
selection
(setq selection (magit-region-sections))))
(--when-let (magit-section-parent section)
(magit-section-selected-p it selection))))

(defun magit-section-parent-value (section)
(setq section (magit-section-parent section))
(when section (magit-section-value section)))
Expand Down
76 changes: 0 additions & 76 deletions magit-utils.el
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@
;; libraries which cannot depend on one another, they are defined here
;; too.

;; And finally, some users insist on using ancient Emacsen, making
;; backward compatibility definitions necessary. They too are placed
;; here.

;;; Code:

(require 'cl-lib)
Expand Down Expand Up @@ -171,64 +167,6 @@ Global settings:
:group 'magit
:type 'character)

;;; Compatibility

(eval-and-compile

;; Added in Emacs 24.3
(unless (fboundp 'user-error)
(defalias 'user-error 'error))

;; Added in Emacs 24.3 (mirrors/emacs@b335efc3).
(unless (fboundp 'setq-local)
(defmacro setq-local (var val)
"Set variable VAR to value VAL in current buffer."
(list 'set (list 'make-local-variable (list 'quote var)) val)))

;; Added in Emacs 24.3 (mirrors/emacs@b335efc3).
(unless (fboundp 'defvar-local)
(defmacro defvar-local (var val &optional docstring)
"Define VAR as a buffer-local variable with default value VAL.
Like `defvar' but additionally marks the variable as being automatically
buffer-local wherever it is set."
(declare (debug defvar) (doc-string 3))
(list 'progn (list 'defvar var val docstring)
(list 'make-variable-buffer-local (list 'quote var)))))

;; Added in Emacs 24.4
(unless (fboundp 'with-current-buffer-window)
(defmacro with-current-buffer-window (buffer-or-name action quit-function &rest body)
"Evaluate BODY with a buffer BUFFER-OR-NAME current and show that buffer.
This construct is like `with-temp-buffer-window' but unlike that
makes the buffer specified by BUFFER-OR-NAME current for running
BODY."
(declare (debug t))
(let ((buffer (make-symbol "buffer"))
(window (make-symbol "window"))
(value (make-symbol "value")))
`(let* ((,buffer (temp-buffer-window-setup ,buffer-or-name))
(standard-output ,buffer)
,window ,value)
(with-current-buffer ,buffer
(setq ,value (progn ,@body))
(setq ,window (temp-buffer-window-show ,buffer ,action)))

(if (functionp ,quit-function)
(funcall ,quit-function ,window ,value)
,value)))))

;; Added in Emacs 24.4
(unless (fboundp 'string-suffix-p)
(defun string-suffix-p (suffix string &optional ignore-case)
"Return non-nil if SUFFIX is a suffix of STRING.
If IGNORE-CASE is non-nil, the comparison is done without paying
attention to case differences."
(let ((start-pos (- (length string) (length suffix))))
(and (>= start-pos 0)
(eq t (compare-strings suffix nil nil
string start-pos nil ignore-case))))))
)

;;; User Input

(defun magit-completing-read
Expand Down Expand Up @@ -415,20 +353,6 @@ Unless optional argument KEEP-EMPTY-LINES is t, trim all empty lines."
(insert-file-contents file)
(split-string (buffer-string) "\n" (not keep-empty-lines)))))

(defun magit-face-remap-set-base (face &optional base)
"Like `face-remap-set-base' but without the bug.
Also lacks a few features we don't need, including the
always-raise-an-error feature."
(make-local-variable 'face-remapping-alist)
(--if-let (assq face face-remapping-alist)
(if base
(setcar (last it) base)
(if (cddr it)
(setcar (last it) face)
(setq face-remapping-alist (remq it face-remapping-alist))))
(when base
(push (list face base) face-remapping-alist))))

;;; magit-utils.el ends soon
(provide 'magit-utils)
;; Local Variables:
Expand Down
Loading