From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: Setting global variables Date: Fri, 25 Feb 2022 13:01:51 -0500 Message-ID: References: <20220220040515.zum3iodtpscj23j3.ref@Ergus> <20220220040515.zum3iodtpscj23j3@Ergus> <87wnhp4wu3.fsf@posteo.net> <20220220132708.lvuzvg2fyfpp6k76@Ergus> <87y224v767.fsf@posteo.net> Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21152"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux) Cc: philipk@posteo.net, spacibba@aol.com, juri@linkov.net, emacs-devel@gnu.org To: Richard Stallman Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Feb 25 19:32:48 2022 Return-path: Envelope-to: ged-emacs-devel@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nNfOZ-0005By-J4 for ged-emacs-devel@m.gmane-mx.org; Fri, 25 Feb 2022 19:32:47 +0100 Original-Received: from localhost ([::1]:32972 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1nNfOX-0003FZ-UH for ged-emacs-devel@m.gmane-mx.org; Fri, 25 Feb 2022 13:32:45 -0500 Original-Received: from eggs.gnu.org ([209.51.188.92]:51238) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nNeum-0007ry-NN for emacs-devel@gnu.org; Fri, 25 Feb 2022 13:02:00 -0500 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:64235) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1nNeuh-00026O-R8; Fri, 25 Feb 2022 13:01:57 -0500 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 3272580530; Fri, 25 Feb 2022 13:01:54 -0500 (EST) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 6445E80263; Fri, 25 Feb 2022 13:01:52 -0500 (EST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1645812112; bh=AngQbWzM+rlWMi07SmzqkZNPIpzjRFVKtKET2PfuPc4=; h=From:To:Cc:Subject:References:Date:In-Reply-To:From; b=ZxxjG8RzxgnVEkvanCuW2hMHAl1E2R4j9wxqlpDCKCQkcVZpP3ypewexD39t9zylj FwkCSzcrsZl5979sEwbP2QKn6zj2Ojta7x+OupGX23vZnwNpd8XW89U4fI/uLRUgpA XAcbGINdkqQaxhaVPh16QEnmYqA+rLURpxZzRF3i16MbbHTrppBlxBlRfeSwKPBcLU 0dGGTiPtuAe8+en55PvAsYHIR3nhdgsRNXXD7xLLbuCAZ5DVqhl8CUYEGx4RUPNy63 Qut7yEd728hT8GvnTWzR8QaCZiVUPp02bouxTv6szSE4EHy9JQ71temc8LQiG+CulL ucDOagX7SXaTg== Original-Received: from alfajor (lechon.iro.umontreal.ca [132.204.27.242]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 34ACB1208AC; Fri, 25 Feb 2022 13:01:52 -0500 (EST) In-Reply-To: (Richard Stallman's message of "Wed, 23 Feb 2022 23:50:16 -0500") Received-SPF: pass client-ip=132.204.25.50; envelope-from=monnier@iro.umontreal.ca; helo=mailscanner.iro.umontreal.ca X-Spam_score_int: -42 X-Spam_score: -4.3 X-Spam_bar: ---- X-Spam_report: (-4.3 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, RCVD_IN_DNSWL_MED=-2.3, SPF_HELO_NONE=0.001, SPF_PASS=-0.001, T_SCC_BODY_TEXT_LINE=-0.01 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 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-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:286689 Archived-At: > > The idea is very much like advice-add/remove but for variables instead > > of functions. > How would it work? However we decide it should. > is your ides to advise each function that uses `bar'? I don't really know. Last time I took a more serious look at it, I ended up with the code below. Stefan ;;; Changing variables in general (not just function-valued ones) ;; 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--sym (var) (or (get var 'advice--var) (let ((sym (make-symbol "fun"))) (put var 'advice--var sym) ;; FIXME: We store the ground value in the function cell so that it can ;; be easily accessed/changed without having to search for it hidden in ;; the middle of a function. (fset sym (default-value var)) (set sym (lambda () (symbol-function sym))) sym))) (defun advice-var--make-local (var sym) (when (and (not (local-variable-p sym)) (local-variable-p var)) (add-function :override (local sym) ;; FIXME: Should we store this value in a more accessible ;; place, like we do for the global default? (let ((lv (symbol-value var))) (lambda () lv))))) (defun advice-var--refresh (var sym) ;; (cl-assert (eq sym (advice-var--sym var))) (if (local-variable-p sym) (make-local-variable var) (kill-local-variable var)) (set var (funcall (symbol-value sym)))) (defun advice-var-add (var f &optional id local) (let ((sym (advice-var--sym var)) ;; FIXME: This small function which calls two other functions, combined ;; with the advice object in which it'll be placed, results in "many" ;; small functions calling other small functions. ;; This is not handled efficiently by our ELisp engine, neither in ;; terms of space nor in terms of time. (advice (lambda (orig-fun) (funcall f (funcall orig-fun)))) (props (if id `((name . ,id))))) (when local (advice-var--make-local var sym)) (if local (add-function :around (local sym) advice props) (add-function :around (symbol-value sym) advice props)) (advice-var--refresh var sym))) (defun advice-var-set (var val &optional id local) (let ((sym (advice-var--sym var)) (props (if id `((name . ,id))))) (when local (advice-var--make-local var sym)) (if local (add-function :override (local sym) (lambda () val) props) (add-function :override (symbol-value sym) (lambda () val) props)) (advice-var--refresh var sym))) (defun advice-var-unset (var f-or-id &optional local) (let ((sym (advice-var--sym var))) (if local (remove-function (local sym) f-or-id) (remove-function (symbol-value sym) f-or-id)) (advice-var--refresh var sym)))