all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* indirect assignement in lisp
@ 2006-08-29  6:39 help-gnu-emacs
  0 siblings, 0 replies; 4+ messages in thread
From: help-gnu-emacs @ 2006-08-29  6:39 UTC (permalink / raw)
  Cc: vb


Hello all,

say I want to assign a variable a value.

The name of the variable being assigned should depend on the key pressed by the user. Say, if user hits key "M-1" the variable to assign should be `variable-1', and if the user hits "M-2", the variable to assign should be `variable-2'.

I just don't seem to be able to figure out how to make this indirect assignment (other than using a switch statement, of course!). This is a sample of what I've been trying to do:

====================================================
(defvar marked-point-1 nil )
(defvar marked-point-2 nil )

(defun set-mark-point ()
    (interactive)
    ( let ( point-name ( cmd-key last-command-char ) )
        ( if ( or ( < cmd-key 49 ) ( > cmd-key 50 ) )
            ( message "the key is %d how did you get here?" cmd-key )
            ( progn
              ( setq point-name ( concat "marked-point-" (number-to-string (- cmd-key 48 ))))
;              ( set (eval point-name) (list (current-buffer) (point) ) )
            )
        )
    )
)
===============================================================

as one can see, point-name gets assigned to the variable name depending on the key pressed, but how do I get that variable (marked-point-1 or marked-point-2 in this case) assigned a value. I tried many variants of the commented out statement, all to no avail.

Any help would be greatly appreciated,

regards,
Vb

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: indirect assignement in lisp
       [not found] <mailman.5877.1156833550.9609.help-gnu-emacs@gnu.org>
@ 2006-08-29 10:51 ` wenbinye
  2006-08-29 15:46 ` Pascal Bourguignon
  1 sibling, 0 replies; 4+ messages in thread
From: wenbinye @ 2006-08-29 10:51 UTC (permalink / raw)


you can get a symbol value from symbol name like this:
(symbol-value (intern-soft (format "marked-point-%d" (- cmd-key 48))))

For a detail documents, see Info elisp 8.3 Creating and Interning
Symbols

help-gnu-emacs@vsbe.com wrote:
> Hello all,
>
> say I want to assign a variable a value.
>
> The name of the variable being assigned should depend on the key pressed by the user. Say, if user hits key "M-1" the variable to assign should be `variable-1', and if the user hits "M-2", the variable to assign should be `variable-2'.
>
> I just don't seem to be able to figure out how to make this indirect assignment (other than using a switch statement, of course!). This is a sample of what I've been trying to do:
>
> ====================================================
> (defvar marked-point-1 nil )
> (defvar marked-point-2 nil )
>
> (defun set-mark-point ()
>     (interactive)
>     ( let ( point-name ( cmd-key last-command-char ) )
>         ( if ( or ( < cmd-key 49 ) ( > cmd-key 50 ) )
>             ( message "the key is %d how did you get here?" cmd-key )
>             ( progn
>               ( setq point-name ( concat "marked-point-" (number-to-string (- cmd-key 48 ))))
> ;              ( set (eval point-name) (list (current-buffer) (point) ) )
>             )
>         )
>     )
> )
> ===============================================================
>
> as one can see, point-name gets assigned to the variable name depending on the key pressed, but how do I get that variable (marked-point-1 or marked-point-2 in this case) assigned a value. I tried many variants of the commented out statement, all to no avail.
> 
> Any help would be greatly appreciated,
> 
> regards,
> Vb

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: indirect assignement in lisp
       [not found] <mailman.5877.1156833550.9609.help-gnu-emacs@gnu.org>
  2006-08-29 10:51 ` indirect assignement in lisp wenbinye
@ 2006-08-29 15:46 ` Pascal Bourguignon
  1 sibling, 0 replies; 4+ messages in thread
From: Pascal Bourguignon @ 2006-08-29 15:46 UTC (permalink / raw)


help-gnu-emacs@vsbe.com writes:

> Hello all,
>
> say I want to assign a variable a value.

Yes, but you need also to learn some more lisp.

> ====================================================
> (defvar marked-point-1 nil )
> (defvar marked-point-2 nil )
>
> (defun set-mark-point ()
>     (interactive)
>     ( let ( point-name ( cmd-key last-command-char ) )
>         ( if ( or ( < cmd-key 49 ) ( > cmd-key 50 ) )
>             ( message "the key is %d how did you get here?" cmd-key )
>             ( progn
>               ( setq point-name ( concat "marked-point-" (number-to-string (- cmd-key 48 ))))
> ;              ( set (eval point-name) (list (current-buffer) (point) ) )
>             )
>         )
>     )
> )
> ===============================================================

First, indenting:

(defun set-mark-point ()
  (interactive)
  (let (point-name (cmd-key last-command-char))
    (if (or (< cmd-key 49) (> cmd-key 50))
        (message "the key is %d how did you get here?" cmd-key)
        (progn
          (setq point-name (concat "marked-point-"
                                   (number-to-string (- cmd-key 48))))
          (set (eval point-name) (list (current-buffer) (point)))))))

Your let is confusing, we might think that you made an error. If you
put each variable on it's line and don't mix the two forms, it'll be
clearer.  Also, I usually put non-initialized variables after the
initialized ones:

(defun set-mark-point ()
  (interactive)
  (let ((cmd-key last-command-char)
        (point-name))
    (if (or (< cmd-key 49) (> cmd-key 50))
        (message "the key is %d how did you get here?" cmd-key)
        (progn
          (setq point-name (concat "marked-point-"
                                   (number-to-string (- cmd-key 48))))
          (set (eval point-name) (list (current-buffer) (point)))))))


It is better for readability to always use the only one dirrection for
comparizon operators. Use only < <= , never > >=.

        (or (< cmd-key 49) (< 50 cmd-key))

is easier to read than:

        (or (< cmd-key 49) (> cmd-key 50))

and:
        (and (<= 49 cmd-key) (<= cmd-key 50))

[ in Common Lisp one could even write: (<= 49 cmd-key 50) ]

of course the above and is the negated form of the above or, so we
have to change the order of the if branches:



(defun set-mark-point ()
  (interactive)
  (let ((cmd-key last-command-char)
        (point-name))
    (if (and (<= 49 cmd-key) (<= cmd-key 50))
        (progn
          (setq point-name (concat "marked-point-"
                                   (number-to-string (- cmd-key 48))))
          (set (eval point-name) (list (current-buffer) (point))))
        (message "the key is %d how did you get here?" cmd-key))))


Since cmd-key is a character, you should use character literals
instead of codes ?a ?b ?1 ?2, etc.
Instead of concat + number-to-string, it's easier to use format.
Variable names are symbols thought, so you must intern the variable
name (a string) to get a symbol.
Finally, you can get at the value of the symbol with symbol-value:


(defun set-mark-point ()
  (interactive)
  (let ((cmd-key last-command-char)
        (point-name))
    (if (and (<= ?1 cmd-key) (<= cmd-key ?2))
        (progn
          (setq point-name (intern (format "marked-point-%c" cmd-key)))
          (setf (symbol-value point-name) (list (current-buffer) (point))))
        (message "the key is %c how did you get here?" cmd-key))))


You can also move bind point-name in a let* since its expression use
the value of the previously set cmd-key, and use error instead of message
to break out of the function in case of error.

(defun set-mark-point ()
  (interactive)
  (let* ((cmd-key last-command-char)
         (point-name (if (and (<= ?1 cmd-key) (<= cmd-key ?2))
                       (intern (format "marked-point-%c" cmd-key))
                       (error "the key is %c how did you get here?" cmd-key))))
      (setf (symbol-value point-name) (list (current-buffer) (point)))
      (message "%S = %S" point-name  (symbol-value point-name))))




Another solution would be to use something like:

(defvar marked-points (make-hash-table))
(defvar valid-keys "12")

(defun set-mark-point ()
  (interactive)
  (let ((cmd-key last-command-char))
     (unless (position cmd-key valid-keys)
        (error "the key is %c how did you get here?" cmd-key))
     (setf (gethash cmd-key marked-points) (list (current-buffer) (point)))
     (message "marked-point %S = %S" cmd-key (gethash cmd-key marked-points))))

with the advantage that you can easily add new marked-points.


-- 
__Pascal Bourguignon__                     http://www.informatimago.com/

ATTENTION: Despite any other listing of product contents found
herein, the consumer is advised that, in actuality, this product
consists of 99.9999999999% empty space.

^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: indirect assignement in lisp
@ 2006-08-31  4:22 help-gnu-emacs
  0 siblings, 0 replies; 4+ messages in thread
From: help-gnu-emacs @ 2006-08-31  4:22 UTC (permalink / raw)
  Cc: wenbinye


Thank you, this helped a lot, I made it work after reading the man mage you refering to and trying a few things.

vb

<wenbinye@gmail.com> wrote in message news:1156848695.348934.164620@p79g2000cwp.googlegroups.com...
> you can get a symbol value from symbol name like this:
> (symbol-value (intern-soft (format "marked-point-%d" (- cmd-key 48))))
> 
> For a detail documents, see Info elisp 8.3 Creating and Interning
> Symbols

^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2006-08-31  4:22 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
     [not found] <mailman.5877.1156833550.9609.help-gnu-emacs@gnu.org>
2006-08-29 10:51 ` indirect assignement in lisp wenbinye
2006-08-29 15:46 ` Pascal Bourguignon
2006-08-31  4:22 help-gnu-emacs
  -- strict thread matches above, loose matches on Subject: below --
2006-08-29  6:39 help-gnu-emacs

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.