* Changing buffer-local variables to cycle emacs options
@ 2020-12-29 5:13 steve-humphreys
2020-12-29 6:28 ` steve-humphreys
2020-12-29 14:17 ` Yuri Khan
0 siblings, 2 replies; 3+ messages in thread
From: steve-humphreys @ 2020-12-29 5:13 UTC (permalink / raw)
To: Help Gnu Emacs
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)
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Changing buffer-local variables to cycle emacs options
2020-12-29 5:13 Changing buffer-local variables to cycle emacs options steve-humphreys
@ 2020-12-29 6:28 ` steve-humphreys
2020-12-29 14:17 ` Yuri Khan
1 sibling, 0 replies; 3+ messages in thread
From: steve-humphreys @ 2020-12-29 6:28 UTC (permalink / raw)
To: steve-humphreys; +Cc: Help Gnu Emacs
;;(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)
>
>
>
>
>
>
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Changing buffer-local variables to cycle emacs options
2020-12-29 5:13 Changing buffer-local variables to cycle emacs options steve-humphreys
2020-12-29 6:28 ` steve-humphreys
@ 2020-12-29 14:17 ` Yuri Khan
1 sibling, 0 replies; 3+ messages in thread
From: Yuri Khan @ 2020-12-29 14:17 UTC (permalink / raw)
To: steve-humphreys; +Cc: Help Gnu Emacs
On Tue, 29 Dec 2020 at 12:14, <steve-humphreys@gmx.com> wrote:
>
> I am getting the following Lisp error
>
> Debugger entered--Lisp error: (void-variable gstate)
> 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)".
Defining your variables before use is the right way.
If you know you always want a variable to be buffer-local, you can use
(defvar-local gstate 0).
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2020-12-29 14:17 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2020-12-29 5:13 Changing buffer-local variables to cycle emacs options steve-humphreys
2020-12-29 6:28 ` steve-humphreys
2020-12-29 14:17 ` Yuri Khan
Code repositories for project(s) associated with this external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.