From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Pascal J. Bourguignon" Newsgroups: gmane.emacs.help Subject: Re: never use `eval' Date: Thu, 16 Jul 2015 02:22:23 +0200 Organization: Informatimago Message-ID: <87k2u1kl34.fsf@kuiper.lan.informatimago.com> References: <87io9lmb4z.fsf@mbork.pl> <87oajdkqc7.fsf@kuiper.lan.informatimago.com> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Trace: ger.gmane.org 1437006321 891 80.91.229.3 (16 Jul 2015 00:25:21 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Thu, 16 Jul 2015 00:25:21 +0000 (UTC) To: help-gnu-emacs@gnu.org Original-X-From: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Thu Jul 16 02:25:18 2015 Return-path: Envelope-to: geh-help-gnu-emacs@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1ZFWzG-0001Cn-9I for geh-help-gnu-emacs@m.gmane.org; Thu, 16 Jul 2015 02:25:18 +0200 Original-Received: from localhost ([::1]:37908 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1ZFWzF-00025Q-L6 for geh-help-gnu-emacs@m.gmane.org; Wed, 15 Jul 2015 20:25:17 -0400 Original-Path: usenet.stanford.edu!fu-berlin.de!uni-berlin.de!individual.net!not-for-mail Original-Newsgroups: gnu.emacs.help Original-Lines: 131 Original-X-Trace: individual.net cEIO0m4EAf+kJTNdwldeOwOTpz2NwQHwcvR1rwrvyoxdZO9TQw Cancel-Lock: sha1:NTc4ZDI4ZDdlNDFiNDMzMGIyNDc2NDE4ZDEwZjZiYjc4ZWJhMGViNg== sha1:kCQi8c+HS7KujqXx6xCcjz7XBIs= Face: iVBORw0KGgoAAAANSUhEUgAAADAAAAAwAQMAAABtzGvEAAAABlBMVEUAAAD///+l2Z/dAAAA oElEQVR4nK3OsRHCMAwF0O8YQufUNIQRGIAja9CxSA55AxZgFO4coMgYrEDDQZWPIlNAjwq9 033pbOBPtbXuB6PKNBn5gZkhGa86Z4x2wE67O+06WxGD/HCOGR0deY3f9Ijwwt7rNGNf6Oac l/GuZTF1wFGKiYYHKSFAkjIo1b6sCYS1sVmFhhhahKQssRjRT90ITWUk6vvK3RsPGs+M1RuR mV+hO/VvFAAAAABJRU5ErkJggg== X-Accept-Language: fr, es, en User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) Original-Xref: usenet.stanford.edu gnu.emacs.help:213470 X-BeenThere: help-gnu-emacs@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: Users list for the GNU Emacs text editor List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Original-Sender: help-gnu-emacs-bounces+geh-help-gnu-emacs=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.help:105756 Archived-At: Emanuel Berg writes: > "Pascal J. Bourguignon" > writes: > >> It's always a bad idea to use eval, because eval >> evalutes in the nil environment, not in the local >> lexical envrionment. > > I have used `eval' eight times in my current setup of > 75 files. > > Tho some of this code I wrote several years ago, I'd > be happy to correct it. Any hints - general or > specific - are appreciated. > > ;; From: http://user.it.uu.se/~embe8573/conf/emacs-init/faces.el > > (defun set-color-face (name front &optional bold back) > (eval `(defvar ,(make-symbol name))) > (eval `(setq ,(intern name) > `((t (:foreground ,front > :background ,back > :bold ,bold) ))))) As mentionned, eval setq can be replaced by (setf symbol-value) or even set, if you like archaic forms. For defvar, and in general for operations like this set-color-face that _defines_ a symbol to bind it to some kind of object, what you should do is to write a define macro instead. Also, your code is higly suspicious: why do you use make-symbol? This will create a _different_ symbol! (eq (make-symbol "x") (intern "x")) --> nil therefore your defvar will be totally useless! Instead, you can just give a symbol to the macro. (defmacro define-color-face (name front &optional bold back) `(progn (defvar ,name) (setq ,name '((t (:foreground ,front :background ,back :bold ,bold)))))) (define-color-face cf-black "black") cf-black --> ((t (:foreground "black" :background nil :bold nil))) > (defun do-repeat-complex-command () > (interactive) > (eval (car command-history) )) This use is acceptable. > ;; From: http://user.it.uu.se/~embe8573/conf/emacs-init/isbn.el > > (defun book-page-to-bibtex () > (interactive) > (save-excursion > (goto-char (point-min)) > (let ((title (get-title)) > (data (mapcar 'key-value > '("authors?" "publisher" "published" "isbn-10")) )) > (eval `(create-book nil ,title ,@data) )))) ; don't INSERT Just use apply! (apply (function create-book) nil title data) > ;; From: http://user.it.uu.se/~embe8573/conf/emacs-init/match-data-format.el > > (defun match-data-format (data match format-str) > (save-match-data > (string-match match data) > (eval `(message format-str ,@(make-match-list 1 data) )))) Use apply. > ;; From: http://user.it.uu.se/~embe8573/conf/emacs-init/wrap-search.el > > (defun wrap-search-again (prefix) > "Search again for the most recent search string of `wrap-search'. > Use \\[universal-argument] \(to set the PREFIX\) to toggle case sensitiveness." > (interactive "p") > (let ((cmd (cl-dolist (cmd command-history) > (if (and (eq (car cmd) 'wrap-search) > (not (string= (cl-caddr cmd) "")) ) > (cl-return cmd) )))) > (if cmd > (if (eq prefix 4) > (let*((old-prefix (cadr cmd)) > (search-str (cl-caddr cmd)) > (new-prefix (if (eq old-prefix 4) 1 4)) > (final-cmd `(wrap-search ,new-prefix ,search-str)) ) > (setq command-history (cons final-cmd command-history)) > (eval final-cmd) ) Use apply. > (eval cmd) ) Ok for eval. > (message " No previous search.") ))) Notice: command-history is a variable defined in `C source code'. Its value is shown below. Documentation: List of recent commands that read arguments from terminal. Each command is represented as a form to evaluate. So clearly, (eval (first command-history)) is vetted. -- __Pascal Bourguignon__ http://www.informatimago.com/ “The factory of the future will have only two employees, a man and a dog. The man will be there to feed the dog. The dog will be there to keep the man from touching the equipment.” -- Carl Bass CEO Autodesk