Skip to content
Permalink
Browse files

Update all defadvice to use new-style advice instead

  • Loading branch information...
purcell committed Oct 20, 2018
1 parent e034eb6 commit 6b169e0e73ec93f31d05cd15426657afc0525dbe
Showing with 37 additions and 35 deletions.
  1. +4 −2 lisp/init-benchmarking.el
  2. +9 −8 lisp/init-compile.el
  3. +9 −8 lisp/init-lisp.el
  4. +1 −3 lisp/init-org.el
  5. +12 −11 lisp/init-sessions.el
  6. +2 −3 lisp/init-sql.el
@@ -6,18 +6,20 @@
"A list of (FEATURE LOAD-START-TIME LOAD-DURATION).
LOAD-DURATION is the time taken in milliseconds to load FEATURE.")

(defadvice require (around sanityinc/build-require-times (feature &optional filename noerror) activate)
(defun sanityinc/require-times-wrapper (orig feature &rest args)
"Note in `sanityinc/require-times' the time taken to require each feature."
(let* ((already-loaded (memq feature features))
(require-start-time (and (not already-loaded) (current-time))))
(prog1
ad-do-it
(apply orig feature args)
(when (and (not already-loaded) (memq feature features))
(let ((time (sanityinc/time-subtract-millis (current-time) require-start-time)))
(add-to-list 'sanityinc/require-times
(list feature require-start-time time)
t))))))

(advice-add 'require :around 'sanityinc/require-times-wrapper)


(define-derived-mode sanityinc/require-times-mode tabulated-list-mode "Require-Times"
"Show times taken to `require' packages."
@@ -24,30 +24,31 @@
"The last buffer in which compilation took place.")

(after-load 'compile
(defadvice compilation-start (after sanityinc/save-compilation-buffer activate)
(defun sanityinc/save-compilation-buffer ()
"Save the compilation buffer to find it later."
(setq sanityinc/last-compilation-buffer next-error-last-buffer))
(advice-add 'compilation-start :after 'sanityinc/save-compilation-buffer)

(defadvice recompile (around sanityinc/find-prev-compilation (&optional edit-command) activate)
(defun sanityinc/find-prev-compilation (orig &rest args)
"Find the previous compilation buffer, if present, and recompile there."
(if (and (null edit-command)
(not (derived-mode-p 'compilation-mode))
sanityinc/last-compilation-buffer
(buffer-live-p (get-buffer sanityinc/last-compilation-buffer)))
(with-current-buffer sanityinc/last-compilation-buffer
ad-do-it)
ad-do-it)))
(apply orig args))
(apply orig args)))
(advice-add 'recompile :around 'sanityinc/find-prev-compilation))

(global-set-key [f6] 'recompile)

(defadvice shell-command-on-region
(after sanityinc/shell-command-in-view-mode
(start end command &optional output-buffer replace &rest other-args)
activate)

(defun sanityinc/shell-command-in-view-mode (start end command &optional output-buffer replace &rest other-args)
"Put \"*Shell Command Output*\" buffers into view-mode."
(unless (or output-buffer replace)
(with-current-buffer "*Shell Command Output*"
(view-mode 1))))
(advice-add 'shell-command-on-region :after 'sanityinc/shell-command-in-view-mode)


(after-load 'compile
@@ -26,15 +26,16 @@
(add-hook 'after-init-hook 'ipretty-mode))


(defadvice pp-display-expression (after sanityinc/make-read-only (expression out-buffer-name) activate)
(defun sanityinc/make-read-only (expression out-buffer-name)
"Enable `view-mode' in the output buffer - if any - so it can be closed with `\"q\"."
(when (get-buffer out-buffer-name)
(with-current-buffer out-buffer-name
(view-mode 1))))
(advice-add 'pp-display-expression :after 'sanityinc/make-read-only)



(defun sanityinc/maybe-set-bundled-elisp-readonly ()
(defun sanityinc/maybe-set-bundled-elisp-readonly (&rest _)
"If this elisp appears to be part of Emacs, then disallow editing."
(when (and (buffer-file-name)
(string-match-p "\\.el\\.gz\\'" (buffer-file-name)))
@@ -188,7 +189,7 @@
(defvar sanityinc/vc-reverting nil
"Whether or not VC or Magit is currently reverting buffers.")

(defadvice revert-buffer (after sanityinc/maybe-remove-elc activate)
(defun sanityinc/maybe-remove-elc (&rest _)
"If reverting from VC, delete any .elc file that will now be out of sync."
(when sanityinc/vc-reverting
(when (and (eq 'emacs-lisp-mode major-mode)
@@ -198,13 +199,13 @@
(when (file-exists-p elc)
(message "Removing out-of-sync elc file %s" (file-name-nondirectory elc))
(delete-file elc))))))
(advice-add 'revert-buffer :after 'sanityinc/maybe-remove-elc)

(defadvice magit-revert-buffers (around sanityinc/reverting activate)
(defun sanityinc/reverting (orig &rest args)
(let ((sanityinc/vc-reverting t))
ad-do-it))
(defadvice vc-revert-buffer-internal (around sanityinc/reverting activate)
(let ((sanityinc/vc-reverting t))
ad-do-it))
(apply orig args)))
(advice-add 'magit-revert-buffers :around 'sanityinc/reverting)
(advice-add 'vc-revert-buffer-internal :around 'sanityinc/reverting)



@@ -128,9 +128,7 @@ typical word processor."
(after-load 'org-agenda
(add-to-list 'org-agenda-after-show-hook 'org-show-entry))

(defadvice org-refile (after sanityinc/save-all-after-refile activate)
"Save all org buffers after each refile operation."
(org-save-all-org-buffers))
(advice-add 'org-refile :after (lambda (&rest _) (org-save-all-org-buffers)))

;; Exclude DONE state tasks from refile targets
(defun sanityinc/verify-refile-target ()
@@ -3,24 +3,25 @@
desktop-auto-save-timeout 600)
(desktop-save-mode 1)

(defadvice desktop-read (around time-restore activate)
(let ((start-time (current-time)))
(prog1
ad-do-it
(message "Desktop restored in %.2fms"
(sanityinc/time-subtract-millis (current-time)
start-time)))))
(defun sanityinc/desktop-time-restore (orig &rest args)
(let ((start-time (current-time)))
(prog1
(apply orig args)
(message "Desktop restored in %.2fms"
(sanityinc/time-subtract-millis (current-time)
start-time)))))
(advice-add 'desktop-read :around 'sanityinc/desktop-time-restore)

(defadvice desktop-create-buffer (around time-create activate)
(let ((start-time (current-time))
(filename (ad-get-arg 1)))
(defun sanityinc/desktop-time-buffer-create (orig ver filename &rest args)
(let ((start-time (current-time)))
(prog1
ad-do-it
(apply orig ver filename args)
(message "Desktop: %.2fms to restore %s"
(sanityinc/time-subtract-millis (current-time)
start-time)
(when filename
(abbreviate-file-name filename))))))
(advice-add 'desktop-create-buffer :around 'sanityinc/desktop-time-buffer-create)

;;----------------------------------------------------------------------------
;; Restore histories and registers after saving
@@ -25,14 +25,13 @@ Fix for the above hasn't been released as of Emacs 25.2."
(after-load 'sql
(define-key sql-mode-map (kbd "C-c C-z") 'sanityinc/pop-to-sqli-buffer)
(when (package-installed-p 'dash-at-point)
(defun sanityinc/maybe-set-dash-db-docset ()
(defun sanityinc/maybe-set-dash-db-docset (&rest _)
(when (eq sql-product 'postgres)
(set (make-local-variable 'dash-at-point-docset) "psql")))

(add-hook 'sql-mode-hook 'sanityinc/maybe-set-dash-db-docset)
(add-hook 'sql-interactive-mode-hook 'sanityinc/maybe-set-dash-db-docset)
(defadvice sql-set-product (after set-dash-docset activate)
(sanityinc/maybe-set-dash-db-docset))))
(advice-add 'sql-set-product :after 'sanityinc/maybe-set-dash-db-docset)))

(setq-default sql-input-ring-file-name
(expand-file-name ".sqli_history" user-emacs-directory))

3 comments on commit 6b169e0

@yuzumx

This comment has been minimized.

Copy link
Contributor

replied Oct 20, 2018

I would like to ask, what is the advantage of this compared to the original defadvice?

Thanks.

@purcell

This comment has been minimized.

Copy link
Owner Author

replied Oct 20, 2018

It's a little more direct now that the defadvice machinery is quite a complicated wrapper around nadvice. And the code is less magical IMO, with no special symbols like ad-do-it.

@yuzumx

This comment has been minimized.

Copy link
Contributor

replied Oct 20, 2018

Thank you.

Please sign in to comment.
You can’t perform that action at this time.