From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Should mode commands be idempotent? Date: Sat, 23 Sep 2017 16:37:08 -0400 Message-ID: References: <5358b04b-70cb-bbaf-1887-bd83613e9c2b@gmail.com> <00b0c4e4-a02f-8c9f-ef52-7ad5a65798d1@gmail.com> <81dcceb4-f939-1d63-0c31-0926a5dad823@gmail.com> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1506199051 20738 195.159.176.226 (23 Sep 2017 20:37:31 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Sat, 23 Sep 2017 20:37:31 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Sep 23 22:37:27 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1dvrAy-0004o2-7g for ged-emacs-devel@m.gmane.org; Sat, 23 Sep 2017 22:37:24 +0200 Original-Received: from localhost ([::1]:36039 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dvrB3-0000Xs-L8 for ged-emacs-devel@m.gmane.org; Sat, 23 Sep 2017 16:37:29 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:49097) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dvrAw-0000Xb-Un for emacs-devel@gnu.org; Sat, 23 Sep 2017 16:37:23 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dvrAt-0004bu-S1 for emacs-devel@gnu.org; Sat, 23 Sep 2017 16:37:22 -0400 Original-Received: from [195.159.176.226] (port=33420 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1dvrAt-0004Zs-Kz for emacs-devel@gnu.org; Sat, 23 Sep 2017 16:37:19 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1dvrAh-0003te-6G for emacs-devel@gnu.org; Sat, 23 Sep 2017 22:37:07 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 58 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:4uOFXYyIZnKSccVKeiDz6SrE4o0= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:218735 Archived-At: > the top of my todo list recently (close enough that I thought about it, > but not close enough to get usable code out of it). I finally found the the pseudo-code I wrote for it back then, in case you're interested (it was in my local nadvice.el hacks). See below (guaranteed 100% untested). Stefan ;;; Changing variables in general (not just function-valued ones) ;; TODO: Handle buffer-local settings! ;; TODO: Make it work for arbitrary gv-places? ;; TODO: Maybe use it or something similar to keep track of effects of ;; loading a file (where `id' would be the file name)? (defun advice--var-apply-op (val op v) (pcase-exhaustive op (:override v) (:around (funcall v val)))) (defun advice--var-compute-val (var) (let ((xs (get var 'advice-var-settings))) (if (not xs) (symbol-value var) (let ((val (car xs))) (pcase-dolist (`(,_id ,op ,v) (cdr xs)) (setq val (advice--var-apply-op val op v))) val)))) (defun advice--var-set (var id op v) ;TODO: add a `depth'? (let ((xs (get var 'advice-var-settings))) (unless xs (setq xs (list (symbol-value var))) (setf (get var 'advice-var-settings) xs)) (unless (equal (symbol-value var) (advice--var-compute-val var)) (message "Var %S changed outside of `advice-set'" var)) ;; FIXME: We could almost use ;; (setf (alist-get id (cdr xs)) (list op v)) ;; Except that we want to add new entries at the end! (let ((x (assoc id (cdr xs)))) (if x ;; There's already another setting for `id'. (setcdr x (list op v)) (setcdr (last xs) (list (list id op v))))) (set var (advice--var-compute-val var)))) (defun advice--var-unset (var id) (let* ((xs (get var 'advice-var-settings)) (x (assoc id (cdr xs)))) (when x (delq x xs) (set var (advice--var-compute-val var)) (unless (cdr xs) (setf (get var 'advice-var-settings) nil)))))