2 #+STARTUP: odd hidestars unfold
3 #+AUTHOR: Emacs Community
4 #+EMAIL: bzg AT gnu DOT org
6 #+OPTIONS: skip:nil toc:nil
7 * How to check regexp for syntax-errors?
9 ;; From: Kevin Rodgers <kevin.d.rodgers@gmail.com>
10 ;; Date: Tue, 20 Oct 2009 01:54:56 -0600
11 ;; Subject: Re: How to check regexp for syntax-errors? (There HAS to be SOME
14 ;; > Isn't there some .el that that will try to parse a regexp, and tell
15 ;; > me where it got confused, and what to look for for errors?
17 #+BEGIN_SRC emacs-lisp
18 (defun valid-regexp-p (regexp)
19 (interactive "sRegexp: ")
21 (condition-case error-data
23 (re-search-forward regexp nil t)
26 (when (interactive-p) (message "Invalid regexp: %s" (cdr error-data)))
31 ;; Denver, Colorado, USA
33 * prefer cond over case?
34 #+BEGIN_SRC emacs-lisp
35 ;; From: David Kastrup <dak@gnu.org>
36 ;; Date: Mon, 12 Oct 2009 10:03:39 +0200
37 ;; To: help-gnu-emacs@gnu.org
38 ;; Subject: Re: Perferr cond over case?
40 ;; pjb@informatimago.com (Pascal J. Bourguignon) writes:
42 ;; > Nordlöw <per.nordlow@gmail.com> writes:
44 ;; >> Does the use of the cl macro case() incurr some loss of performance
45 ;; >> compare to using cond() instead?
47 ;; > (macroexpand '(case (* 2 2 2 2 3)
51 ;; > (otherwise 'unknown)))
53 ;; > (let ((--cl-var-- (* 2 2 2 2 3)))
54 ;; > (cond ((eql --cl-var-- (quote 10)) (quote one))
55 ;; > ((member* --cl-var-- (quote (24 42))) (quote two))
56 ;; > ((member* --cl-var-- (quote (3 33))) (quote three))
57 ;; > (t (quote unknown))))
59 ;; > What do you think?
61 ;; Before or after byte compilation?
69 #+BEGIN_SRC emacs-lisp
70 ;; From: Decebal <cldwesterhof@gmail.com>
71 ;; Newsgroups: gnu.emacs.help
72 ;; Date: Sat, 10 Oct 2009 11:33:17 -0700 (PDT)
73 ;; To: help-gnu-emacs@gnu.org
74 ;; Envelope-To: andreas.roehler@easy-emacs.de
76 ;; In a Bash script I changed:
80 for i in $(seq ${1}) ; do
81 printf " Regel %${length}d voor de test\n" ${i}
84 emacs -batch -nw --eval='
89 (output-file "'"${2}"'"))
90 (setq nr-of-lines-length (length (number-to-string nr-of-lines)))
91 (dotimes (i nr-of-lines t)
92 (insert (format (format " Regel %%%dd voor de test\n" nr-of-lines-length) (1+ i))))
93 (write-file output-file))
96 ;; The Bash version took 293 seconds and the Emacs Lisp version 59
97 ;; seconds. So it is about 5 times as fast.
98 ;; The Emacs batch gives output like:
99 ;; Saving file /home/cecil/temp/inputEmacs...
100 ;; Wrote /home/cecil/temp/inputEmacs
101 ;; That is why I use the 2>/dev/null.
102 ;; Is there a way to circumvent the generation of the above output?
103 ;; Because when there is an error it is also thrown away and that is not
106 ;; From: Vassil Nikolov <vnikolov@pobox.com>
107 ;; Newsgroups: gnu.emacs.help
108 ;; Date: Sat, 10 Oct 2009 16:55:31 -0400
109 ;; To: help-gnu-emacs@gnu.org
110 ;; Envelope-To: andreas.roehler@easy-emacs.de
113 ;; On Sat, 10 Oct 2009 11:33:17 -0700 (PDT), Decebal <cldwesterhof@gmail.com> said:
116 ;; > local length=${#1}
117 ;; > for i in $(seq ${1}) ; do
118 ;; > printf " Regel %${length}d voor de test\n" ${i}
123 ;; emacs -Q -batch -eval '
124 (dotimes (i '"${1}"')
125 (princ (format " Regel %'"${#1}"'d voor de test\n" (1+ i))))
131 ;; From: Decebal <cldwesterhof@gmail.com>
132 ;; Newsgroups: gnu.emacs.help
133 ;; Date: Sat, 10 Oct 2009 13:57:07 -0700 (PDT)
134 ;; To: help-gnu-emacs@gnu.org
135 ;; Envelope-To: andreas.roehler@easy-emacs.de
137 ;; On Oct 10, 10:06pm, Andreas R=F6hler <andreas.roeh...@easy-emacs.de>
139 ;; > > The Emacs batch gives output like:
140 ;; > > Saving file /home/cecil/temp/inputEmacs...
142 ;; > it's in files.el, save-buffer, AFAIS
144 ;; > (if (and modp (buffer-file-name))
145 ;; > (message "Saving file %s..." (buffer-file-name)))
147 ;; > commenting out these lines should cancel the message
149 ;; The problem with that is that it only works for me. But I found a way.
151 (write-file output-file))
153 (set-visited-file-name output-file)
156 ;; But maybe there should be more consideration for the possibility that
157 ;; Emacs is used as a batch program.
160 ;; > > Wrote /home/cecil/temp/inputEmacs
162 ;; I still have to find something for this.
164 ;; That is not possible I am afraid. In the C-source there is a call to
165 ;; message_with_string.
169 #+BEGIN_SRC emacs-lisp
170 ;; From: pjb@informatimago.com (Pascal J. Bourguignon)
171 ;; Newsgroups: gnu.emacs.help
172 ;; Date: Fri, 09 Oct 2009 19:19:49 +0200
173 ;; To: help-gnu-emacs@gnu.org
174 ;; Envelope-To: andreas.roehler@easy-emacs.de
176 ;; Nordlöw <per.nordlow@gmail.com> writes:
178 ;; > If I have an association list say,
181 ;; > ("key" sym1 val1 num1)
182 ;; > ("key2" sym2 val2 num2)
185 ;; > , where each entry is a fixed sequence of various objects.
187 ;; If this is an a-list, then you could write it as:
189 (("key1" . (sym1 val1 num1))
190 ("key2" . (sym2 val2 numb2)))
192 ;; to show that it is a list of cons cells.
194 ;; (a . (b c d)) <=> (a b c d), but the first notation shows that you
195 ;; consider it as a list of cons, and notably that you don't expect nil
196 ;; ie. () to be in the toplevel of the a-list.
198 ;; Also, if we write the a-list properly like this, we can better answer
199 ;; the following question:
202 ;; > aswell use a vector to represent an entry in this alist, right?
204 ;; You cannot use a vector instead of the cons cells of the a-list, but
205 ;; you can use a vector as a value of an a-list entry. Values can be of
206 ;; any type. In the case of emacs lisp, you could also easily use
207 ;; vectors (or any other type) as keys in an a-list, since it uses equal
210 (("key1" . [sym1 val1 num1])
211 ("key2" . [sym2 val2 num2])
212 ([?k ?e ?y ?3] . [sym3 val3 num3]))
214 ;; > In this case, what do I gain by using a vector instead of list?
216 ;; In general, vectors take half the space of lists, and access to the
217 ;; nth element is done in O(1) instead of O(n) with lists. However,
218 ;; adding or removing an element in a vector is O(n) while in the case of
219 ;; lists, it may be O(1) (prepending an element or removing the first
220 ;; element or one of the few firsts elements) or O(n) (inserting,
221 ;; appending an element or removing the nth element).
223 ;; > What about performance?: aref() faster than nth() only for large
226 ;; aref has to compute a multiplication and a sum, before doing one
227 ;; memory load to get the element. In the case of emacs lisp, the
228 ;; multiplication is always by the same fixed factor AFAIK.
230 ;; nth has to do n memory loads to get the element.
232 ;; So indeed, aref will probably be faster than nth, even for indices as
235 ;; > Is there vector-variant of assoc()?
237 ;; No. Unless you count hash-tables as a vector variant.
241 ;; Because there's no point. The advantage of using a list for a-list,
242 ;; apart from the historical simplicity, is that you can easily prepend
243 ;; the a-list with new associations, and therefore use the a-list in a
244 ;; purely functional way.
247 (let ((val (cdr (assoc 'x bindings))))
250 (cons val (f (cons (cons 'x (1- val)) bindings))))))
252 (let ((bindings '((y . 0) (x . 1))))
253 (list (f (cons (cons 'x 2) bindings))
254 (cdr (assoc 'x bindings))))
257 ;; Note: you could use (require 'cl) (acons key value a-list)
258 ;; instead of (cons (cons key value) a-list).
260 ;; > Has any one already written such a function?
262 ;; Not AFAIK, but you can write it. However, the semantics of assoc
263 ;; require a sequential search of the keys in the list, so there would be
264 ;; no gain. On the contrary, we would now have O(n) complexity to
265 ;; prepend a new entry to the a-vector.
268 ;; __Pascal Bourguignon__