emacs: Generalize buffer redisplaying.

* emacs/guix-base.el (guix-buffer-after-redisplay-hook): New variable.
  (guix-redisplay-buffer): Use it.  Remove all arguments and the code
  for moving point to the next button.  Rename to...
  (guix-buffer-redisplay): ... this.
  (guix-buffer-redisplay-goto-button): New procedure.
* emacs/guix-info.el (guix-package-info-show-source): Use it.  Adjust
  accordingly.
  (guix-package-info-redisplay-after-download): Likewise.
master
Alex Kost 2015-11-22 13:21:18 +03:00
parent e40b3c33f4
commit 819518d15a
2 changed files with 40 additions and 39 deletions

View File

@ -288,7 +288,7 @@ This alist is filled by `guix-buffer-define-interface' macro.")
(define-key map (kbd "l") 'guix-history-back) (define-key map (kbd "l") 'guix-history-back)
(define-key map (kbd "r") 'guix-history-forward) (define-key map (kbd "r") 'guix-history-forward)
(define-key map (kbd "g") 'revert-buffer) (define-key map (kbd "g") 'revert-buffer)
(define-key map (kbd "R") 'guix-redisplay-buffer) (define-key map (kbd "R") 'guix-buffer-redisplay)
map) map)
"Parent keymap for Guix buffer modes.") "Parent keymap for Guix buffer modes.")
@ -616,41 +616,39 @@ See `revert-buffer' for the meaning of NOCONFIRM."
(guix-set-buffer guix-profile entries guix-buffer-type guix-entry-type (guix-set-buffer guix-profile entries guix-buffer-type guix-entry-type
search-type search-vals t t)))) search-type search-vals t t))))
(cl-defun guix-redisplay-buffer (&key buffer profile entries buffer-type (defvar guix-buffer-after-redisplay-hook nil
entry-type search-type search-vals) "Hook run by `guix-buffer-redisplay'.
"Redisplay a Guix BUFFER. This hook is called before seting up a window position.")
Restore the point and window positions after redisplaying if possible.
This function will not update the information, use (defun guix-buffer-redisplay ()
\"\\[revert-buffer]\" if you want the full update. "Redisplay the current Guix buffer.
Restore the point and window positions after redisplaying.
If BUFFER is nil, use the current buffer. For the meaning of the This function does not update the buffer data, use
rest arguments, see `guix-set-buffer'." '\\[revert-buffer]' if you want the full update."
(interactive) (interactive)
(or buffer (setq buffer (current-buffer))) (let* ((old-point (point))
(with-current-buffer buffer ;; For simplicity, ignore an unlikely case when multiple
(or (derived-mode-p 'guix-info-mode 'guix-list-mode) ;; windows display the same buffer.
(error "%S is not a Guix buffer" buffer)) (window (car (get-buffer-window-list (current-buffer) nil t)))
(let* ((point (point)) (window-start (and window (window-start window))))
(was-at-button (button-at point)) (guix-set-buffer guix-profile guix-entries guix-buffer-type
;; For simplicity, ignore an unlikely case when multiple guix-entry-type guix-search-type guix-search-vals
;; windows display the same BUFFER. t t)
(window (car (get-buffer-window-list buffer nil t))) (goto-char old-point)
(window-start (and window (window-start window)))) (run-hooks 'guix-buffer-after-redisplay-hook)
(guix-set-buffer (or profile guix-profile) (when window
(or entries guix-entries) (set-window-point window (point))
(or buffer-type guix-buffer-type) (set-window-start window window-start))))
(or entry-type guix-entry-type)
(or search-type guix-search-type) (defun guix-buffer-redisplay-goto-button ()
(or search-vals guix-search-vals) "Redisplay the current buffer and go to the next button, if needed."
t t) (let ((guix-buffer-after-redisplay-hook
(goto-char point) (cons (lambda ()
(and was-at-button (unless (button-at (point))
(not (button-at (point))) (forward-button 1)))
(forward-button 1)) guix-buffer-after-redisplay-hook)))
(when window (guix-buffer-redisplay)))
(set-window-point window (point))
(set-window-start window window-start)))))
;;; Generations ;;; Generations

View File

@ -714,14 +714,16 @@ prompt depending on `guix-operation-confirm' variable)."
Find the file if needed (see `guix-package-info-auto-find-source'). Find the file if needed (see `guix-package-info-auto-find-source').
ENTRY-ID is an ID of the current entry (package or output). ENTRY-ID is an ID of the current entry (package or output).
PACKAGE-ID is an ID of the package which source to show." PACKAGE-ID is an ID of the package which source to show."
(let* ((entry (guix-entry-by-id entry-id guix-entries)) (let* ((entries guix-entries)
(file (guix-package-source-path package-id))) (entry (guix-entry-by-id entry-id guix-entries))
(file (guix-package-source-path package-id)))
(or file (or file
(error "Couldn't define file path of the package source")) (error "Couldn't define file name of the package source"))
(let* ((new-entry (cons (cons 'source-file file) (let* ((new-entry (cons (cons 'source-file file)
entry)) entry))
(entries (guix-replace-entry entry-id new-entry guix-entries))) (new-entries (guix-replace-entry entry-id new-entry entries)))
(guix-redisplay-buffer :entries entries) (setq guix-entries new-entries)
(guix-buffer-redisplay-goto-button)
(if (file-exists-p file) (if (file-exists-p file)
(if guix-package-info-auto-find-source (if guix-package-info-auto-find-source
(guix-find-file file) (guix-find-file file)
@ -770,7 +772,8 @@ SOURCE is a list of URLs."
"Redisplay an 'info' buffer after downloading the package source. "Redisplay an 'info' buffer after downloading the package source.
This function is used to hide a \"Download\" button if needed." This function is used to hide a \"Download\" button if needed."
(when (buffer-live-p guix-package-info-download-buffer) (when (buffer-live-p guix-package-info-download-buffer)
(guix-redisplay-buffer :buffer guix-package-info-download-buffer) (with-current-buffer guix-package-info-download-buffer
(guix-buffer-redisplay-goto-button))
(setq guix-package-info-download-buffer nil))) (setq guix-package-info-download-buffer nil)))
(add-hook 'guix-after-source-download-hook (add-hook 'guix-after-source-download-hook