help-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Changing buffer-local variables to cycle emacs options


From: steve-humphreys
Subject: Re: Changing buffer-local variables to cycle emacs options
Date: Tue, 29 Dec 2020 07:28:51 +0100

;;(defvar gstate 0)

(defun gstate-cycle ()
  "Toggle Display-Line-Numbers mode in all buffers."
  (interactive)

  (let ((n gstate))

    (pcase n
      (1 (setq-local gstate 2)
         (message "%s" "gstate 1"))

      (2 (setq-local gstate 3)
         (message "%s" "gstate 2"))

      (_ (setq-local gstate 1)
         (message "%s" "gstate _")) )))

 (global-set-key (kbd "H-1") #'gstate-cycle)




> Sent: Tuesday, December 29, 2020 at 10:43 AM
> From: steve-humphreys@gmx.com
> To: "Help Gnu Emacs" <help-gnu-emacs@gnu.org>
> Subject: Changing buffer-local variables to cycle emacs options
>
> I am getting the following Lisp error
>
> Debugger entered--Lisp error: (void-variable gstate)
>   (let ((n gstate)) (cond ((eql n 1) (set (make-local-variable 'gstate) 2) 
> (message "%s" "gstate 1")) ((eql n 2) (set (make-local-variable 'gstate) 3) 
> (message "%s" "gstate 2")) (t (set (make-local-variable 'gstate) 1) (message 
> "%s" "gstate _"))))
>   gstate-cycle()
>   funcall-interactively(gstate-cycle)
>   call-interactively(gstate-cycle nil nil)
>   command-execute(gstate-cycle)
>
> I am using the function below.  The problem occurs when I comment out
> "(defvar gstate 0)".  When I introduce the variable, the Lisp Error is
> not reported.
>
> One confusion that exists is that I want gstate buffer-local.  But I think
> that using defvar is a mistake.  Another think is to avoid defining gstate
> before calling the function.  My plan was to have "pcase" do to the "(_"
> part if the buffer-local variable "gstate" is not defined, and then define
> it using "(setq-local gstate 1)".
>
> How can I code this properly?
>
> -------- code --------
>
> ;;(defvar gstate 0)
>
> (defun gstate-cycle ()
>   (interactive)
>
>   (let ((n gstate))
>
>     (pcase n
>       (1 (setq-local gstate 2)
>        (message "%s" "gstate 1"))
>
>       (2 (setq-local gstate 3)
>        (message "%s" "gstate 2"))
>
>       (_ (setq-local gstate 1)
>        (message "%s" "gstate _")) )))
>
>  (global-set-key (kbd "H-1") #'gstate-cycle)
>
>
>
>
>
>



reply via email to

[Prev in Thread] Current Thread [Next in Thread]