emacs daemon.. but quietly
[elbb.git] / elbb.org
blob0ac20b45233cfbc322b6eca7b3efd932baf2597e
1 #+TITLE:    ELBB - itself
2 #+STARTUP:  odd hidestars unfold
3 #+AUTHOR:   Emacs Community
4 #+EMAIL:    bzg AT gnu DOT org
5 #+LANGUAGE: en
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
13 ;; David Combs wrote:
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: ")
20    (with-temp-buffer
21      (condition-case error-data
22         (progn
23           (re-search-forward regexp nil t)
24           t)
25        (invalid-regexp
26         (when (interactive-p) (message "Invalid regexp: %s" (cdr error-data)))
27         nil))))
29 ;; -- 
30 ;; Kevin Rodgers
31 ;; Denver, Colorado, USA
32 #+END_SRC
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:
43 ;; >
44 ;; >> Does the use of the cl macro case() incurr some loss of performance
45 ;; >> compare to using cond() instead?
46 ;; >
47 ;; > (macroexpand '(case (* 2 2 2 2 3)
48 ;; >                 (10      'one)
49 ;; >                 ((24 42) 'two)
50 ;; >                 ((3 33)  'three)
51 ;; >                 (otherwise 'unknown)))
52 ;; > -->
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))))
58 ;; >
59 ;; > What do you think?
61 ;; Before or after byte compilation?
63 ;; -- 
64 ;; David Kastrup
67 #+END_SRC
68 * batch-mode
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:
77        local i
78        local length=${#1}
80        for i in $(seq ${1}) ; do
81          printf "       Regel %${length}d voor de test\n" ${i}
82        done  >${2}
83  ;; to:
84        emacs -batch -nw --eval='
85         (let (
86               (i)
87               (nr-of-lines '${1}')
88               (nr-of-lines-length)
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))
94        ' 2>/dev/null
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
104 ;; nice.
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:
114 ;; > ...
115 ;; >       local i
116 ;; >       local length=${#1}
117 ;; >       for i in $(seq ${1}) ; do
118 ;; >         printf "       Regel %${length}d voor de test\n" ${i}
119 ;; >       done  >${2}
121   ;; translates to
123     ;; emacs -Q -batch -eval '
124       (dotimes (i '"${1}"')
125         (princ (format "       Regel %'"${#1}"'d voor de test\n" (1+ i))))
126     ;; ' > "${2}"
128   ;; ---Vassil.
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>
138 ;; wrote:
139 ;; > > The Emacs batch gives output like:
140 ;; > >   Saving file /home/cecil/temp/inputEmacs...
141 ;; >
142 ;; > it's in files.el, save-buffer, AFAIS
143 ;; >
144 ;; >   (if (and modp (buffer-file-name))
145 ;; >     (message "Saving file %s..." (buffer-file-name)))
146 ;; >
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.
150 ;; I replaced:
151     (write-file output-file))
152 ;; with:
153     (set-visited-file-name output-file)
154     (basic-save-buffer))
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.
167 #+END_SRC
168 * vectors and lists
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,
179 ;; >
180 ;; > '(
181 ;; >   ("key" sym1 val1 num1)
182 ;; >   ("key2" sym2 val2 num2)
183 ;; > )
184 ;; >
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:
201 ;; > I might
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
208 ;; to compare keys.  
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
224 ;; > vectors?
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
233 ;; small as 1 or 0.
235 ;; > Is there vector-variant of assoc()? 
237 ;; No.  Unless you count hash-tables as a vector variant.
239 ;; > If not, why? 
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.
246    (defun f (bindings)
247       (let ((val (cdr (assoc 'x bindings))))
248          (if (zerop val)
249              (list val)
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))))
255    ;; --> ((2 1 0) 1)
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.
267 ;; -- 
268 ;; __Pascal Bourguignon__
269 #+END_SRC