From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Pascal Bourguignon Newsgroups: gmane.emacs.help Subject: Re: indirect assignement in lisp Date: Tue, 29 Aug 2006 17:46:45 +0200 Organization: Informatimago Message-ID: <87zmdnfst6.fsf@thalassa.informatimago.com> References: NNTP-Posting-Host: main.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: sea.gmane.org 1156869645 4668 80.91.229.2 (29 Aug 2006 16:40:45 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Tue, 29 Aug 2006 16:40:45 +0000 (UTC) Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Tue Aug 29 18:40:41 2006 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by ciao.gmane.org with esmtp (Exim 4.43) id 1GI6dP-0005Tq-BA for geh-help-gnu-emacs@m.gmane.org; Tue, 29 Aug 2006 18:40:19 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1GI6dO-0006nB-Rp for geh-help-gnu-emacs@m.gmane.org; Tue, 29 Aug 2006 12:40:18 -0400 Original-Path: shelby.stanford.edu!newsfeed.stanford.edu!newsserver.news.garr.it!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 143 Original-X-Trace: individual.net idZF775vCsTdyNRE/Zh8eAVCpbIiEbYZVHXbA+aiesE8zEIF+g Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en X-Disabled: X-No-Archive: no User-Agent: Gnus/5.11 (Gnus v5.11) Emacs/22.0.50 (gnu/linux) Cancel-Lock: sha1:G9arGfGYfwlA6lDfjUCkdQ1dolw= Original-Xref: shelby.stanford.edu gnu.emacs.help:141418 Original-To: help-gnu-emacs@gnu.org X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:37038 Archived-At: 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.