Reading a sequence of bytes as one integer
[elbb.git] / code / elbb.el
blobc9696d8e032fc8687e56518558f4fd3042065cf7
1 * Reading a sequence of bytes as one integer
3 Date: Sat, 13 Mar 2010 20:49:28 +0200
4 From: Eli Zaretskii <eliz@gnu.org>
5 To: help-gnu-emacs@gnu.org
6 Subject: Re: Emacs Lisp - Reading a sequence of bytes as one integer
8 > Date: Sat, 13 Mar 2010 11:56:17 -0500 (EST)
9 > From: Jeff Clough <jeff@chaosphere.com>
11 > I have a (mostly) binary file I want to inspect. There are several
12 > places in this file where I want to read a series of bytes (in this
13 > case, three consecutive bytes) and treat those bytes as a single
14 > integer. I can grab them as a buffer-substring obviously, but I'm at
15 > a loss for how to make that into the integer I need, nor can I find
16 > anything obvious that would be easier.
18 Take a look at bindat.el (it's bundled with Emacs).
20 * how to access a large datastructure efficiently?
22 To: help-gnu-emacs@gnu.org
23 From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
24 Date: Thu, 04 Mar 2010 17:09:35 +0100
25 Subject: Re: how to access a large datastructure efficiently?
27 Andreas Röhler <andreas.roehler@easy-emacs.de> writes:
29 > Thierry Volpiatto wrote:
30 >> Andreas Röhler <andreas.roehler@easy-emacs.de> writes:
31 >>
32 >>> Thierry Volpiatto wrote:
33 >>>> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
34 >>>>
35 >>>>> Hi,
36 >>>>>
37 >>>>> Christian Wittern <cwittern@gmail.com> writes:
38 >>>>>
39 >>>>>> Hi there,
40 >>>>>>
41 >>>>>> Here is the problem I am trying to solve:
42 >>>>>>
43 >>>>>> I have a large list of items which I want to access. The items are in
44 >>>>>> sequential order, but many are missing in between, like:
45 >>>>>>
46 >>>>>> (1 8 17 23 25 34 45 47 50) [in reality, there is a value associated
47 >>>>>> with this, but I took it out for simplicity]
48 >>>>>>
49 >>>>>> Now when I am trying to access with a key that is not in the list, I
50 >>>>>> want to have the one with the closest smaller key returned, so for 6
51 >>>>>> and 7 this would be 1, but for 8 and 9 this would be 8.
52 >>>>>>
53 >>>>>> Since the list will have thousands of elements, I do not want to simply
54 >>>>>> loop through it but am looking for better ways to do this in Emacs lisp.
55 >>>>>> Any ideas how to achieve this?
56 >>>>> ,----
57 >>>>> | (defun closest-elm-in-seq (n seq)
58 >>>>> | (let ((pair (loop with elm = n with last-elm
59 >>>>> | for i in seq
60 >>>>> | if (and last-elm (< last-elm elm) (> i elm)) return (list last-elm i)
61 >>>>> | do (setq last-elm i))))
62 >>>>> | (if (< (- n (car pair)) (- (cadr pair) n))
63 >>>>> | (car pair) (cadr pair))))
64 >>>>> `----
65 >>>>>
66 >>>>> That return the closest, but not the smaller closest, but it should be
67 >>>>> easy to adapt.
68 >>>> Case where your element is member of list, return it:
69 >>>>
70 >>>> ,----
71 >>>> | (defun closest-elm-in-seq (n seq)
72 >>>> | (let ((pair (loop with elm = n with last-elm
73 >>>> | for i in seq
74 >>>> | if (eq i elm) return (list i)
75 >>>> | else if (and last-elm (< last-elm elm) (> i elm)) return (list last-elm i)
76 >>>> | do (setq last-elm i))))
77 >>>> | (if (> (length pair) 1)
78 >>>> | (if (< (- n (car pair)) (- (cadr pair) n))
79 >>>> | (car pair) (cadr pair))
80 >>>> | (car pair))))
81 >>>> `----
82 >>>> For the smallest just return the car...
83 >>>>
84 >>> if n is member of the seq, maybe equal-operator too
85 >>>
86 >>> (<= last-elm elm)
87 >>>
88 >>> is correct?
89 >>
90 >> No, in this case:
91 >>
92 >> if (eq i elm) return (list i) ==> (i) ; which is n
93 >>
94 >> and finally (car pair) ==> n
95 >>
97 > Hmm, sorry being the imprecise,
98 > aimed at the first form, whose result equals the the second form once implemented this "="
100 Ok, i understand, yes, we can do what you say and it's more elegant, i
101 just notice also i forget to remove a unuseful else:
103 ,----
104 | (defun closest-elm-in-seq (n seq)
105 | (let ((pair (loop with elm = n with last-elm
106 | for i in seq
107 | if (and last-elm (<= last-elm elm) (> i elm)) return (list last-elm i)
108 | do (setq last-elm i))))
109 | (if (< (- n (car pair)) (- (cadr pair) n))
110 | (car pair) (cadr pair))))
111 `----
113 That should work the same.
114 Thanks. ;-)
117 Thierry Volpiatto
118 Gpg key: http://pgp.mit.edu/
120 To: help-gnu-emacs@gnu.org
121 From: Andreas Politz <politza@fh-trier.de>
122 Date: Thu, 04 Mar 2010 17:49:54 +0100
123 Subject: Re: how to access a large datastructure efficiently?
125 [ ... ]
127 I don't know how hash-table could help in this case, but maybe you want
128 to consider binary trees, as implemented in the avl-tree.el package.
129 Though, there is no function for finding the closest member with respect
130 to some data and a distance-function, but see below.
132 Finding a (closest) member should be constraint in logarithmic time.
134 (require 'avl-tree)
136 (setq avl (avl-tree-create '<))
138 (dotimes (i 2000)
139 (when (= 0 (% i 4))
140 (avl-tree-enter avl i)))
142 (avl-tree-member avl 80)
143 => 80
144 (avl-tree-member avl 70)
145 => nil
147 (defun avl-tree-closest-member (tree data delta-fn)
148 ;; delta-fn : data x data -> Z
149 (flet ((comp-delta (node)
150 (funcall delta-fn data
151 (avl-tree--node-data node))))
152 (let* ((node (avl-tree--root tree))
153 closest
154 (delta most-positive-fixnum)
155 (compare-function (avl-tree--cmpfun tree))
156 found)
157 (while (and node
158 (not found))
159 (when (< (comp-delta node) delta)
160 (setq delta (comp-delta node)
161 closest node))
162 (cond
163 ((funcall compare-function data (avl-tree--node-data node))
164 (setq node (avl-tree--node-left node)))
165 ((funcall compare-function (avl-tree--node-data node) data)
166 (setq node (avl-tree--node-right node)))
168 (setq found t))))
169 (if closest
170 (avl-tree--node-data closest)
171 nil))))
173 (mapcar
174 (lambda (data)
175 (avl-tree-closest-member
176 avl data (lambda (n1 n2)
177 (abs (- n1 n2)))))
178 '(1001 1002 1003 1004))
180 => (1000 1004 1004 1004)
184 * cedet and auto-complete
186 To: help-gnu-emacs@gnu.org
187 From: Richard Riley <rileyrgdev@gmail.com>
188 Date: Tue, 02 Mar 2010 08:26:20 +0100
189 Subject: Re: cedet and auto-complete
191 Richard Riley <rileyrgdev@gmail.com> writes:
193 > Just another quick poll to see if anyone has got the new cedet working
194 > with auto-complete. cedet has developed its own completion UIs but
195 > ideally I would like to use auto-complete as I do in all other
196 > modes. Has anyone a workaround or instructions for allowing this to
197 > work? Unfortunately if you only enable the minimum features which then
198 > turns off the cedet completion mechs, it also turns off the semantic
199 > navigation features which kind of detracts from its usefulness.
201 > Any help or pointer appreciated,
203 > r.
206 OK, user errror to a degree - I have now moved back to company-mode
207 (newer version is available) and made sure it was on the load path
208 before the version that came with nxhtml.
210 Works well.
212 Here is my simple setup which turns on ispell support in text-mode and
213 so works in gnus message modes.
215 (add-to-list 'load-path "~/.emacs.d/company-mode")
216 (require 'company)
217 (add-hook 'text-mode-hook (lambda()(add-to-list 'company-backends 'company-ispell)))
218 (require 'company-ispell)
219 (global-company-mode)
221 This is with cedet 1.0pre7 and company-mode 0.5
223 * hide-show squash-minor-modes
225 From: Thien-Thi Nguyen <ttn@gnuvola.org>
226 To: Andreas Roehler <andreas.roehler@online.de>
227 Subject: Re: hideshow condensed view
228 Date: Thu, 25 Feb 2010 16:21:27 +0100
230 () Andreas Roehler <andreas.roehler@online.de>
231 () Thu, 25 Feb 2010 15:24:24 +0100
233 thanks a lot maintaining hideshow.el.
235 In the last few years, i can't take any credit for its improvement.
236 See Emacs' ChangeLog for those who actually merit the appreciation...
238 Use it as default outline-mode now.
240 Cool.
242 Just one thing remains so far: hs displays an empty
243 line between headers. Would prefer a more condensed
244 view.
246 You must have good eyes (still); i use hideshow not
247 for density but for spaciousness.
249 Might it be possible to introduce a function
250 `hide-all-empty-lines', inclusive a variable which may
251 be customized?
253 Yeah, it would be possible, but i tend to think of empty lines outside
254 the (top-level) blocks as also outside the scope of hideshow. It's like
255 in the book GEB, there is the tree and there is the stuff outside the
256 tree. Get it?
258 I am already troubled by hideshow straying from its conceptual (both
259 meanings) simplicity -- a block is between matching parens, no more no
260 less -- but that's just me...
262 All this is to say, you probably should write a "squash minor mode",
263 which puts the invisible property on an overlay for text that matches
264 the empty line regexp (which of course, could be made customizable), at
265 which point my advice would be to add a hook to `hs-hide-all' to enable
266 this mode.
268 Here's a quick (but tested (lightly)) sketch:
270 (defvar empty-lines-rx "^\\s-*\\(\n\\s-*\\)+")
272 (defvar empty-lines nil
273 "List of empty lines (overlays).")
275 (defvar squash-minor-mode nil)
277 (defun squash-minor-mode ()
278 (interactive)
279 (setq squash-minor-mode (not squash-minor-mode))
280 (if squash-minor-mode
281 (save-excursion
282 (goto-char (point-min))
283 (while (re-search-forward empty-lines-rx nil t)
284 (let ((ov (make-overlay (match-beginning 0) (match-end 0))))
285 (overlay-put ov 'display "")
286 (push ov empty-lines))))
287 (mapc 'delete-overlay empty-lines)
288 (setq empty-lines nil))
289 (message "empty-lines squashing %s" (if squash-minor-mode 'on 'off)))
291 Have fun!
295 * questioning let
297 Andreas Roehler <andreas.roehler@online.de> writes:
299 > Hi,
301 > behaviour of the example code below puzzles me. Would
302 > expect setting of arg by external function, but inside
303 > `let', recognised. But remains `1'.
305 > (defun arg-setting ()
306 > (interactive)
307 > (let ((arg 1))
308 > (message "%s" arg)
309 > (arg-extern arg)
310 > (message "%s" arg)))
312 > (defun arg-extern (arg)
313 > (setq arg (1- arg)))
315 > Any help?
317 From: David Kastrup <dak@gnu.org>
318 Date: Wed, 24 Feb 2010 19:10:38 +0100
319 To: help-gnu-emacs@gnu.org
320 Subject: Re: questioning let
322 >> The argument binding in arg-extern is the innermost one and consequently
323 >> the only affected one. If you make the function argument-less, it will
324 >> likely work as expected by you, affecting the binding in arg-setting.
326 > That works, thanks a lot!
327 > However, stored in some eil.el, get a compiler warning than:
330 > In arg-extern:
331 > eil.el:9:9:Warning: reference to free variable `arg'
332 > eil.el:9:17:Warning: assignment to free variable `arg'
334 > Would think a useless warning, as the compiler should know being inside a let (?)
336 The warning is completely accurate since arg-extern can be called from
337 outside arg-setting, in which case it will assign to a global variable
338 called "arg".
340 Whether or not some let-binding might be effective at the point of
341 calling arg-extern is unknown to the compiler.
343 In a Lisp variant with lexical binding (like Common Lisp or Scheme),
344 arg-extern has no way to fiddle with the let-binding of arg-setting: it
345 is completely inaccessible by name outside of arg-setting itself.
348 David Kastrup
350 From: pjb@informatimago.com (Pascal J. Bourguignon)
351 Date: Wed, 24 Feb 2010 12:59:22 +0100
352 To: help-gnu-emacs@gnu.org
353 Subject: Re: questioning let
355 let is equivalent to lambda:
357 (let ((a 1) (b 2)) (list a b)) <=> ((lambda (a b) (list a b)) 1 2)
359 defun is binding a lambda to a function cell:
361 (defun f (a b) (list a b))
362 <=> (setf (symbol-function 'f) (lambda (a b) (list a b)))
364 Therefore you can see that calling a function defined by defun is a let
365 in disguise.
367 If you transformed your code following these equivalences, you would
368 notice that you have actually TWO variables named arg, one as parameter
369 of the function arg-extern, and one as variable in the let in
370 arg-setting.
372 The setq in arg-extern will modify only the variable parameter of
373 arg-extern. Because they have the same name, this variable hides the
374 one defined in the let of arg-setting. There's no way to access it from
375 within arg-extern.
377 If they had a different name, you could modify a variable from an outer
378 dynamic scope from an inner dynamic scope, because in emacs all the
379 variables are dynamic. But it is considered very bad form to do so:
380 this is a big side effect, and what's more, one that depends on the call
381 chain. You should avoid side effects, to increase the readability and
382 debugability of your code. Therefore you should avoid setq and setf.
383 Try to write pure function, never try to modify a variable.
385 One way to write your code would be:
387 (defun do-what-you-need-to-do-with (arg)
390 (defun arg-binding ()
391 (interactive)
392 (let ((arg 1))
393 (message "before arg = %s" arg)
394 (let ((arg (arg-extern arg)))
395 (message "after arg = %s" arg)
396 (do-what-you-need-to-do-with arg))
397 (message "original arg = %s" arg)))
399 (defun arg-extern (arg)
400 (message "arg-extern before arg = %s" arg)
401 (message "arg-extern returns = %s" (1- arg))
402 (1- arg))
404 before arg = 1
405 arg-extern before arg = 1
406 arg-extern returns = 0
407 after arg = 0
408 original arg = 1
410 If you need a global variable (perhaps because you need to keep some
411 data across command invocations), the I would advise to distringuish it
412 from the other by giving it a name surrounded by stars: *var*. Then, it
413 will have a different name, and won't be shadowed (inadvertantly) by
414 inner lets, defuns or lambdas.
416 (defvar *var* 42)
418 (defun arg-extern (arg)
419 (message "arg-extern before arg = %s" arg)
420 (setf *var* (1- arg))
421 (message "arg-extern returns = %s" *var*)
422 *var*)
424 (arg-binding)
425 var* --> 0
428 __Pascal Bourguignon__
430 * real tab completion in shell-mode
432 To: help-gnu-emacs@gnu.org
433 From: Andreas Politz <politza@fh-trier.de>
434 Date: Tue, 23 Feb 2010 19:43:58 +0100
435 Subject: Re: real tab completion in shell-mode
437 Nathaniel Flath <flat0103@gmail.com> writes:
439 > Hello,
440 > Is there any way to get shell mode to use the background shell's
441 > (zsh or bash) tab completion as opposed to just tab-completing
442 > filenames?  I know about ansi-term, but I hate how it steals a lot
443 > of my keybindings and so I would prefer it if I could get this to
444 > work in shell-mode.
446 There is not really a clean way to implement this for different reasons.
449 > Thanks,
450 > Nathaniel Flath
452 A while ago, I did it anyway and wrote a special mode for bash-shells
453 which features
455 + emulation of bash completion
456 + dir-tracking (local and remote)
457 + tracking of history files
458 + displaying dir stack in header line
460 I took the opportunity to comment the code somewhat, you may give it
461 a try, if you dare:
463 http://www.fh-trier.de/~politza/emacs/bash-mode-1_0.tgz
465 Extract it somewhere in your load-path and read the commentary, or just
466 M-x bash RET
470 * reduce repeated backslashes
472 From: Teemu Likonen <tlikonen@iki.fi>
473 Date: Mon, 15 Feb 2010 10:51:28 +0200
474 To: help-gnu-emacs@gnu.org
475 Subject: Re: reduce repeated backslashes
477 2010-02-15 09:40 (+0100), Andreas Roehler wrote:
479 > don't know how to replace/reduce repeated backslashes from inside a
480 > string.
482 > Let's assume "abcdef\\\\"
484 What is the end result you want? I guess:
486 (replace-regexp-in-string "\\\\\\\\" "\\" "abcdef\\\\" nil t)
487 => "abcdef\\"
489 There are 8 backslashes in the regexp string because backslash is a meta
490 character in Lisp strings and also in regexps. 8 backslashes in a regexp
491 Lisp string means 2 literal backslashes. In the resulting string there
492 is only one backslash but it is displayed as two "\\" because it's a
493 printed representation of Lisp string.
495 * Why can't I use xargs emacs?
497 From: pjb@informatimago.com (Pascal J. Bourguignon)
498 Date: Tue, 02 Feb 2010 23:40:30 +0100
499 To: help-gnu-emacs@gnu.org
500 Subject: Re: Why can't I use xargs emacs?
502 Adam Funk <a24061@ducksburg.com> writes:
504 > The emacs command can take a list of filename arguments, so why can't
505 > I get xargs to work with it?
507 > $ find -name '*.txt' |xargs emacs -nw
508 > emacs: standard input is not a tty
510 > $ grep -rl 'foo' some/path |xargs emacs -nw
511 > emacs: standard input is not a tty
513 emacs is an interactive program. It expects its stdin and stdout to
514 be hooked to the terminal, where it can display a character matrix,
515 and from which it can read user input.
517 When you use a pipe to send paths to xargs, you disconnect the
518 terminal from the stdin, and replace it with a pipe. When xargs forks
519 and exec emacs, emacs inherit this pipe as stdin, and cannot get user
520 input, but will get instead further path from grep.
522 % echo hello > file ; ( echo -b ; echo file ) | xargs -L 1 cat
523 hello
525 To open several files in emacs, you could either use emacsclient, or
526 an emacs lisp script.
528 Launch emacs in a separate terminal: xterm -e emacs -nw &
529 In emacs, start the server: M-x server-start RET
530 In a shell, you can then type: find -name '*.txt' | xargs emacsclient -n
532 Simplier would be to just open the file in emacs:
534 Launch emacs: emacs -nw
535 Then type: C-x C-f *.txt RET
537 For the second case, you could type:
538 M-: (map nil 'find-file (split-string (shell-command-to-string "grep -rl 'foo' some/path") "\n")) RET
541 __Pascal Bourguignon__
543 From: Harald Hanche-Olsen <hanche@math.ntnu.no>
544 Date: Tue, 02 Feb 2010 18:51:39 -0500
545 To: help-gnu-emacs@gnu.org
546 Subject: Re: Why can't I use xargs emacs?
548 + Adam Funk <a24061@ducksburg.com>:
550 > The emacs command can take a list of filename arguments, so why can't
551 > I get xargs to work with it?
553 > $ find -name '*.txt' |xargs emacs -nw
554 > emacs: standard input is not a tty
556 $ find -name '*.txt' |xargs sh -c 'emacs -nw "$@" </dev/tty' -
558 (untested)
561 Harald Hanche-Olsen <URL:http://www.math.ntnu.no/~hanche/>
562 - It is undesirable to believe a proposition
563 when there is no ground whatsoever for supposing it is true.
564 -- Bertrand Russell
566 To: help-gnu-emacs@gnu.org
567 From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
568 Date: Wed, 03 Feb 2010 08:23:52 +0100
569 Subject: Re: Why can't I use xargs emacs?
571 Why not a simple:
573 emacs -nw -Q $(find . -name '*.txt')
575 Why do you want to use xargs?
577 Bill Marcum <marcumbill@bellsouth.net> writes:
579 > ["Followup-To:" header set to comp.unix.shell.]
580 > On 2010-02-02, Adam Funk <a24061@ducksburg.com> wrote:
581 >> The emacs command can take a list of filename arguments, so why can't
582 >> I get xargs to work with it?
584 >> $ find -name '*.txt' |xargs emacs -nw
585 >> emacs: standard input is not a tty
587 >> $ grep -rl 'foo' some/path |xargs emacs -nw
588 >> emacs: standard input is not a tty
590 > It says: standard input is not a tty. I don't normally use emacs, so there
591 > may be a better way to do this, but you could write a function:
592 > my_emacs () { emacs "$@" </dev/tty >&0 2>&0 ; }
597 Thierry Volpiatto
599 From: Adam Funk <a24061@ducksburg.com>
600 Date: Wed, 03 Feb 2010 14:18:58 +0000
601 To: help-gnu-emacs@gnu.org
602 Subject: Re: Why can't I use xargs emacs?
604 On 2010-02-02, Bit Twister wrote:
606 > On Tue, 02 Feb 2010 20:22:17 +0000, Adam Funk wrote:
607 >> The emacs command can take a list of filename arguments, so why can't
608 >> I get xargs to work with it?
610 >> $ find -name '*.txt' |xargs emacs -nw
611 >> emacs: standard input is not a tty
613 >> $ grep -rl 'foo' some/path |xargs emacs -nw
614 >> emacs: standard input is not a tty
617 > Maybe it's the -nw switch. Try
618 > find -name '*.txt' |xargs emacs
620 Yes, it works without -nw, but I'm often logged into an ssh server so
621 it's faster to open emacs in the xterm.
623 Anyway, the solution (posted by Thierry in gnu.emacs.help) turns out
624 to be this:
626 emacs -nw $(find . -name '*.txt')
628 Thanks.
631 ..the reason why so many professional artists drink a lot is not
632 necessarily very much to do with the artistic temperament, etc. It is
633 simply that they can afford to, because they can normally take a large
634 part of a day off to deal with the ravages. [Amis _On Drink_]
636 * getting emacs from launchpad
638 From: Lennart Borgman <lennart.borgman@gmail.com>
639 Date: Tue, 26 Jan 2010 22:46:47 +0100
640 To: henry atting <nsmp_01@online.de>
641 Subject: Re: bazaar question
643 On Tue, Jan 26, 2010 at 10:31 PM, henry atting <nsmp_01@online.de> wrote:
644 > I find it annoying that I have to grapple with bazaar if I
645 > want to go on building emacs from source. Do I really have to pull
646 > the whole emacs tree starting from the first published sources in the
647 > Roman Empire?
649 > Tried it with `bazaar branch http://bzr.savannah.gnu.org/r/emacs/trunk/'
650 > but stopped, unpatiently, after 20 minutes.
651 > Is this the only way to get the latest source?
653 You can get them from Launchpad too. That is faster since they have
654 installed the bazaar fast server (or what it is called).
656 bzr branch lp:emacs trunk
658 After that updates are quick (in the trunk subdir):
660 bzr update
662 * debug modification of a variable
664 From: Tassilo Horn <tassilo@member.fsf.org>
665 To: emacs-devel@gnu.org
666 Date: Tue, 26 Jan 2010 21:26:49 +0100
667 Subject: Re: How to debug modification to a variable value?
669 "alin.s" <alinsoar@voila.fr> writes:
671 > I suggest you to use the WATCH in gdb for modifications of the
672 > variable, AWATCH for reading of the variable, etc.
674 > To detect the location of the variable is easy: install a breakpoint
675 > in make-variable-buffer-local that stops when exactly the variable you
676 > are interested about is set, then see there the location of
677 > tg-schema-alist. Afterward you can use the x* functions from .gdbinit
678 > of emacs to debug your problem...
680 Thanks for that explanation. I'll try that out as soon as I find some
681 time. And also thanks for volunteering on implementing a debug facility
682 for cases like that.
684 Bye,
685 Tassilo
687 * strip mail head to fit for elbb
689 (defun mail2elbb (&optional beg end)
691 (interactive "*")
692 (let ((beg (cond (beg beg)
693 ((region-active-p)
694 (region-beginning))
695 (t (point-min))))
696 (end (cond (end end)
697 ((region-active-p)
698 (copy-marker (region-end)))
699 (t (point-max)))))
700 (save-restriction
701 (narrow-to-region beg end)
702 (goto-char beg)
703 (mail2elbb-intern beg end)
704 (widen))))
706 (defun mail2elbb-intern (beg end)
707 (while (search-forward "=A0" nil t 1)
708 (replace-match ""))
709 (goto-char beg)
710 (let ((end (progn (while
711 (re-search-forward "^[[:alpha:]-]+To:" nil t 1) 't) (copy-marker (line-end-position)))))
712 (goto-char beg)
713 (beginning-of-line)
714 (while
715 (< (point) end)
716 (if (looking-at "Subject:\\|From:\\|Date:\\|To:")
717 (forward-line 1)
718 (delete-region (line-beginning-position) (1+ (line-end-position)))))))
720 * testing configuration
722 From: pjb@informatimago.com (Pascal J. Bourguignon)
723 Date: Sun, 27 Dec 2009 18:53:08 +0100
724 To: help-gnu-emacs@gnu.org
725 Subject: Re: testing configuration
727 andrea <andrea.crotti.0@gmail.com> writes:
729 > I was wondering if it would be possible to automatically check if my
730 > emacs configuration is correct.
731 > This can be then put in a post-commit hook to check that whenever I add
732 > some new features or change I don't mess up somethin else
734 > I would only need to:
735 > - load my whole configuration
736 > - exit and returns 0 if everything goes fine, returns another number
737 > otherwise.
739 > Any ideas?
741 > I can already launch a new emacs with certain conf, but how can I get a
742 > return value?
744 What about:
746 emacs -q --eval '(condition-case err (progn (load "~/.emacs") (kill-emacs 0)) (error (kill-emacs 1)))'
751 __Pascal Bourguignon__ http://www.informatimago.com/
753 * self-typing
755 From: pjb@informatimago.com (Pascal J. Bourguignon)
756 Date: Sat, 26 Dec 2009 14:32:16 +0100
757 To: help-gnu-emacs@gnu.org
758 Subject: Re: what is `self-typing' ?
760 waterloo <waterloo2005@gmail.com> writes:
762 > I can not understand a para in Elisp manual :
764 > Lisp is unlike many other languages in that its objects are
765 > "self-typing": the primitive type of each object is implicit in the
766 > object itself.  For example, if an object is a vector, nothing can
767 > treat it as a number; Lisp knows it is a vector, not a number.
769 > What is `self-typing' ? thanks
771 As mentionned by Eli. But this require some more explaination.
773 Some languages are defined so that variables can hold only objects of
774 a given type (you declare the type of the variable, or it is infered
775 automatically at compilation time). For example, C or Haskell.
777 Since all the types of the objects are known at compilation time from
778 the variables they're stored in, the compiler doesn't need to generate
779 code to store the type along with the object, or along with the
780 variable: the type is implicity. In C, typeof(42) or int a=42; typeof(a)
781 is computed at compilation time.
783 Some languages are defined so that variables can hold objects of
784 different types, at different times. In that case, the type is not
785 attached to the variable, but must be attached to the objects
786 themselves. For example, Lisp or Smalltalk.
788 Since it is rarely possible to know what type of object a variable
789 will hold (some type inference can still be applied to optimize out
790 some function, but there remains a lot of functions where type
791 inference doesn't restrict much the possible types, the more so when
792 global analysis is not practical or possible), then the type of each
793 object has to be kept with the object. In Lisp, (type-of 42) or (let
794 ((a 42)) (type-of 42)) is computed at run-time. Notably, you can
795 write: (type-of (read)) and depending on what you enter at run-time,
796 will get back one type or another:
798 M-x ielm RET
799 *** Welcome to IELM *** Type (describe-mode) for help.
800 ELISP> (type-of (read))
801 Lisp expression: 42
802 integer
803 ELISP> (type-of (read))
804 Lisp expression: "fourty-two"
805 string
806 ELISP> (type-of (read))
807 Lisp expression: fourty-two
808 symbol
809 ELISP> (type-of (read))
810 Lisp expression: (fourty two)
811 cons
812 ELISP>
814 There are also languages where both things are more or less possible,
815 you can have variables with pre-defined types holding only one type of
816 object, and variables which can hold objects of various types, but of
817 a same root class. For example, C++ or Java.
819 These languages usually provide so called "Plain Old Data" types which
820 correspond to the type of objects that can only be stored in variables
821 with predefined types. These objects of these POD types usually
822 don't have the type attached to them, these objects can only be stored
823 in variables with pre-defined type. When you want to store such an
824 object in a variable-type variable, you have to wrap it in another
825 object, a typed object, instance of a class. In Java, you have a POD
826 type int and an object class Integer.
829 __Pascal Bourguignon__ http://www.informatimago.com/
831 * Emacs uptime
833 From: pjb@informatimago.com (Pascal J. Bourguignon)
834 Date: Wed, 23 Dec 2009 20:16:03 +0100
835 To: help-gnu-emacs@gnu.org
836 Subject: Re: Some functions I hope are useful for others to
838 [ ... ]
840 For example, here is my emacs-uptime:
842 (defvar com.informatimago.time/*emacs-start-time* (current-time)
843 "For (emacs-uptime)")
845 (defun com.informatimago.time/emacs-uptime ()
846 "Gives Emacs' uptime, based on global var `com.informatimago.time/*emacs-start-time*'."
847 (interactive)
848 (let* ((st com.informatimago.time/*emacs-start-time*)
849 (cur (current-time))
850 (hi-diff (- (car cur) (car st)))
851 (tot-sec (+ (ash hi-diff 16) (- (cadr cur) (cadr st))))
852 (days (/ tot-sec (* 60 60 24)))
853 (hrs (/ (- tot-sec (* days 60 60 24)) (* 60 60)))
854 (mins (/ (- tot-sec (* days 60 60 24) (* hrs 60 60)) 60))
855 (secs (/ (- tot-sec (* days 60 60 24) (* hrs 60 60) (* mins 60)) 1)))
856 (message "Up %dd %dh %dm %ds (%s), %d buffers, %d files"
857 days hrs mins secs
858 (format-time-string "%a %Y-%m-%d %T" st)
859 (length (buffer-list))
860 (count t (buffer-list)
861 :test-not
862 (lambda (ignore buf)
863 (null (cdr (assoc 'buffer-file-truename
864 (buffer-local-variables buf)))))))))
866 (defalias 'emacs-uptime 'com.informatimago.time/emacs-uptime)
868 [ .... ]
870 * Using Emacs Lisp for script writing
872 To: help-gnu-emacs@gnu.org
873 From: David Engster <deng@randomsample.de>
874 Date: Sat, 19 Dec 2009 11:02:22 +0100
875 Subject: Re: Using Emacs Lisp for script writing
877 Andreas Politz <politza@fh-trier.de> writes:
878 > Cecil Westerhof <Cecil@decebal.nl> writes:
880 >> I already use 'emacs -batch' for scripting where no user input is used,
881 >> but I would like to use it also for interactive scripting. Until now I
882 >> did not find any usable information about this. Anybody using Emacs for
883 >> interactive scripts?
885 > No, but it should be noted, that this is not very difficult :
887 > $ emacs -Q -batch -eval '(yes-or-no-p "Want some cookies ?")'
889 You can also use the '--script' option in a shebang line:
891 ----------------- test.sh -----------------
892 #!/usr/bin/emacs --script
894 (if (yes-or-no-p "Choose ")
895 (message "You said yes")
896 (message "You said no"))
897 -------------------------------------------
899 I use emacs regularly for writing scripts, since with its thousands of
900 packages you have a huge library readily available.
902 -David
904 * until found
906 From: Helmut Eller
907 Subject: Re: until-found
908 Date: Fri, 11 Dec 2009 17:10:38 +0100
909 User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)
911 * Andreas Roehler [2009-12-10 13:50+0100] writes:
913 > And here my implementation so far:
915 > (defun until-found (search-string liste)
916 > (let ((liste liste) element)
917 > (while liste
918 > (if (member search-string (car liste))
919 > (setq element (car liste) liste nil))
920 > (setq liste (cdr liste)))
921 > element))
923 This seems to be the same as:
925 (car (member (lambda (element) (member search-string element)) liste))
929 (find-if (lambda (element) (member search-string element)) liste)
933 (find search-string liste :test #'member)
937 (loop for e in liste if (member search-string e) return e)
939 Helmut
941 * favourite directories implementation
943 Author: Florent Georges, source:
944 http://fgeorges.blogspot.com/2008/01/emacs-favourite-directories.html
946 Today, I have finally taken a look at one of the
947 simple features I always missed in Emacs: the
948 ability to define a set of "favourite directories."
949 That is, a set of named directories that one can use
950 in the minibuffer when prompted for instance to open
951 a file. Given a set of such dirs:
953 emacs-src -> /enter/your/path/to/emacs/sources
954 projects -> /path/to/some/company/projects
955 now -> @projects/the/project/I/am/working/on
957 one can use the following path in the minibuffer to open a file,
958 for instance using C-x C-f:
960 @emacs-src/lisp/files.el
961 @emacs-src/src/alloc.c
962 @projects/great/README
963 @now/src/some/stuff.txt
965 Doing so, completion is available for both directory names and
966 files under their target directories. For instance, to open the
967 third file above, you only have to type:
969 C-x C-f @ p <tab> g <tab> R <tab> <enter>
971 The implementation I have just written is really simple, but
972 useful yet. It implements all described above (including
973 recursive defined directories, as the '@now' above.) Thanks to
974 Emacs, I am still suprised by the facility to implement such a
975 feature!
977 The code was written on GNU Emacs 22.1 on Windows, but should
978 work on any platform, and I think on Emacs 21 as well.
980 TODO: Make a custom variable.
981 (defvar drkm-fav:favourite-directories-alist
982 '(("saxon-src" . "y:/Saxon/saxon-resources9-0-0-1/source/net/sf/saxon")
983 ("kernow-src" . "~/xslt/kernow/svn-2007-09-29/kernow/trunk/src/net/sf/kernow"))
984 "See `drkm-fav:handler'.")
986 (defvar drkm-fav::fav-dirs-re
987 ;; TODO: Is tehre really no other way (than mapcar) to get the list
988 ;; of the keys of an alist?!?
989 (concat
990 "^@"
991 (regexp-opt
992 (mapcar 'car drkm-fav:favourite-directories-alist)
994 "Internal variable that stores a regex computed from
995 `drkm-fav:favourite-directories-alist'. WARNING: This is not
996 updated automatically if the later variable is changed.")
998 (defun drkm-fav:handler (primitive &rest args)
999 "Magic handler for favourite directories.
1001 With this handler installed into `file-name-handler-alist', it is
1002 possible to use shortcuts for often used directories. It uses
1003 the mapping in the alist `drkm-fav:favourite-directories-alist'.
1005 Once installed, say you have the following alist in the mapping
1006 variable:
1008 ((\"dir-1\" . \"~/some/real/dir\")
1009 (\"dir-2\" . \"c:/other/dir/for/windows/users\"))
1011 You can now use \"@dir-1\" while opening a file with C-x C-f for
1012 instance, with completion for the abbreviation names themselves
1013 as well as for files under the target directory."
1014 (cond
1015 ;; expand-file-name
1016 ((and (eq primitive 'expand-file-name)
1017 (string-match drkm-fav::fav-dirs-re (car args)))
1018 (replace-match
1019 (cdr (assoc (match-string 1 (car args))
1020 drkm-fav:favourite-directories-alist))
1021 t t (car args)))
1022 ;; file-name-completion
1023 ((and (eq primitive 'file-name-completion)
1024 (string-match "^@\\([^/]*\\)$" (car args)))
1025 (let ((compl (try-completion
1026 (match-string 1 (car args))
1027 drkm-fav:favourite-directories-alist)))
1028 (cond ((eq t compl)
1029 (concat "@" (match-string 1 (car args)) "/"))
1030 ((not compl)
1031 nil)
1033 (concat "@" compl)))))
1034 ;; file-name-all-completions
1035 ((and (eq primitive 'file-name-all-completions)
1036 (string-match "^@\\([^/]*\\)$" (car args)))
1037 (all-completions
1038 (match-string 1 (car args))
1039 drkm-fav:favourite-directories-alist))
1040 ;; Handle any primitive we don't know about (from the info node
1041 ;; (info "(elisp)Magic File Names")).
1042 (t (let ((inhibit-file-name-handlers
1043 (cons 'drkm-fav:handler
1044 (and (eq inhibit-file-name-operation primitive)
1045 inhibit-file-name-handlers)))
1046 (inhibit-file-name-operation primitive))
1047 (apply primitive args)))))
1049 ;; Actually plug the feature into Emacs.
1050 (push '("\\`@" . drkm-fav:handler) file-name-handler-alist)
1052 * lisp interface to ispell
1054 From: Teemu Likonen <tlikonen@iki.fi>
1055 Date: Fri, 06 Nov 2009 22:05:53 +0200
1056 To: help-gnu-emacs@gnu.org
1057 Subject: Re: lisp interface to ispell ?
1059 On 2009-11-06 20:39 (+0100), Andreas Politz wrote:
1061 > Does someone have a hack, or know a different package, in order to allow
1062 > elisp access to spelling functions ? E.g. like
1064 > (spell word language)
1066 > which at least returns t or nil.
1068 Something like this?
1070 (defun my-ispell-string (word lang)
1071 (with-temp-buffer
1072 (insert word)
1073 (call-process-region (point-min) (point-max)
1074 "ispell" t t nil "-l" "-d" lang)
1075 (if (= (point-min) (point-max))
1076 t)))
1078 * Python workflow
1080 From: Simon <bbbscarter@gmail.com>
1081 Date: Fri, 6 Nov 2009 03:42:44 -0800 (PST)
1082 To: help-gnu-emacs@gnu.org
1083 Subject: Python workflow
1085 Hi, apologies in advance for a potentially numpty post.
1087 I've been using Emacs for a little while now, but I've yet to settle
1088 on a satisfactory python edit-run-debug cycle, and I was wondering
1089 what wiser minds than mine have settled upon. So far I've tried:
1091 - Edit code and run with emacs PDB. After fiddling the lisp code to
1092 automatically pick up the current buffer as the default run candidate,
1093 this is nearly okay. The main issue is that, after editing code,
1094 there's no easy way to rerun the code, so I end up killing the gud
1095 buffer every time. As such, entering and leaving the debugger is quite
1096 a few key presses and accidentally leaving it running is also easy.
1098 - Tried Pydb to similar effect.
1100 - Run everything in a seperate shell. And debug by hand. This is a
1101 little too low-fi, even for me.
1103 - Use the 'import/reload file' and 'eval def/class' functions, and run
1104 everything from the emacs python shell, using pdbtrack to help with
1105 debugging. Problems so far:
1106 - It's very easy to forget which modules you've modified and fail to
1107 reload them; because the state is carried over I continually find
1108 myself running the old versions of code I've just edited, especially
1109 if it's across several files.
1110 - More than that, sometimes the stuff I expect to reload simply
1111 doesn't, and I have no indication as to why. For example, if pdb has
1112 module open and you stick a deliberate error in the code and reload
1113 it, the minibuffer tells you the module has been loaded, even though
1114 it clearly can't have been.
1115 - I have to run pdb.pm() to debug the exception. If I run *anything*
1116 else by accident, I lose the exception context. This can be annoying
1117 if you're incompetent enough to keep making typos (I am).
1119 Does anyone have any tips on their workflow?
1121 Many thanks!
1123 Simon
1125 * strip out UTF-8 BOMs
1126 From: "Edward O'Connor" <hober0@gmail.com>
1127 Date: Thu, 5 Nov 2009 16:13:27 -0800
1128 To: emacs-devel@gnu.org
1129 Subject: find-file-literally-at-point
1133 I recently found myself in need of such a function (I was going through
1134 a bunch of files to strip out their UTF-8 BOMs, if you're curious), and
1135 it was quick enough to put together:
1137 (autoload 'ffap-guesser "ffap")
1138 (defun find-file-literally-at-point ()
1139 "Open the file at point (like `ffap') with `find-file-literally'."
1140 (interactive)
1141 (find-file-literally (ffap-guesser)))
1143 * xml and n3
1145 From: "Eric Schulte" <schulte.eric@gmail.com>
1146 Subject: Re: [Orgmode] org-babel-tangle xml text
1147 Date: Tue, 03 Nov 2009 09:18:34 -0700
1149 "Martin G. Skjæveland" <martige@ifi.uio.no> writes:
1151 > Is there a way I can add xml and n3 to the list of supported
1152 > languages? These languages does not need interpretation, so I'm
1153 > thinking it should be quite easy to add. I have fumblingly tried
1155 > (add-to-list 'org-babel-tangle-langs '("xml"))
1157 > and
1159 > (add-to-list 'org-babel-tangle-langs '("css" "xml"))
1161 > but it as no effect.
1164 Hi Martin,
1166 The attached org-mode file contains instructions for adding xml and n3
1167 to org-babel and org-babel-tangle. Note that there may be another step
1168 if the major mode for n3 is not n3-mode. Best -- Eric
1170 introduce org-babel to =xml= and =n3=
1172 #+begin_src emacs-lisp :results silent
1173 (org-babel-add-interpreter "xml")
1174 (org-babel-add-interpreter "n3")
1175 #+end_src
1177 if say =n3= should be edited using =xml-mode=, then evaluate the
1178 following adding this pair to =org-src-lang-modes=
1180 #+begin_src emacs-lisp :results silent
1181 (add-to-list 'org-src-lang-modes '("n3" . xml))
1182 #+end_src
1184 ;; inform org-babel-tangle of their existence and file extensions
1185 #+begin_src emacs-lisp :results silent
1186 (add-to-list 'org-babel-tangle-langs '("xml" "xml" nil t))
1187 (add-to-list 'org-babel-tangle-langs '("n3" "n3" nil t))
1188 #+end_src
1190 #+begin_src xml :tangle example
1191 <first>
1192 </first>
1193 #+end_src
1195 #+begin_src n3 :tangle example
1196 n3 stuff
1197 #+end_src
1199 * How to check regexp for syntax-errors?
1201 From: Kevin Rodgers <kevin.d.rodgers@gmail.com>
1202 Date: Tue, 20 Oct 2009 01:54:56 -0600
1203 Subject: Re: How to check regexp for syntax-errors?
1204 David Combs wrote:
1205 > Isn't there some .el that that will try to parse a regexp, and tell
1206 > me where it got confused, and what to look for for errors?
1208 (defun valid-regexp-p (regexp)
1209 (interactive "sRegexp: ")
1210 (with-temp-buffer
1211 (condition-case error-data
1212 (progn
1213 (re-search-forward regexp nil t)
1215 (invalid-regexp
1216 (when (interactive-p) (message "Invalid regexp: %s" (cdr error-data)))
1217 nil))))
1220 Kevin Rodgers
1221 Denver, Colorado, USA
1223 * prefer cond over case?
1225 From: David Kastrup <dak@gnu.org>
1226 Date: Mon, 12 Oct 2009 10:03:39 +0200
1227 To: help-gnu-emacs@gnu.org
1228 Subject: Re: Perferr cond over case?
1230 pjb@informatimago.com (Pascal J. Bourguignon) writes:
1232 > Nordlöw <per.nordlow@gmail.com> writes:
1234 >> Does the use of the cl macro case() incurr some loss of performance
1235 >> compare to using cond() instead?
1237 > (macroexpand '(case (* 2 2 2 2 3)
1238 > (10 'one)
1239 > ((24 42) 'two)
1240 > ((3 33) 'three)
1241 > (otherwise 'unknown)))
1242 > -->
1243 > (let ((--cl-var-- (* 2 2 2 2 3)))
1244 > (cond ((eql --cl-var-- (quote 10)) (quote one))
1245 > ((member* --cl-var-- (quote (24 42))) (quote two))
1246 > ((member* --cl-var-- (quote (3 33))) (quote three))
1247 > (t (quote unknown))))
1249 > What do you think?
1251 Before or after byte compilation?
1254 David Kastrup
1256 * batch-mode
1258 From: Decebal <cldwesterhof@gmail.com>
1259 Newsgroups: gnu.emacs.help
1260 Date: Sat, 10 Oct 2009 11:33:17 -0700 (PDT)
1261 To: help-gnu-emacs@gnu.org
1262 Envelope-To: andreas.roehler@easy-emacs.de
1264 In a Bash script I changed:
1266 local i
1267 local length=${#1}
1269 for i in $(seq ${1}) ; do
1270 printf " Regel %${length}d voor de test\n" ${i}
1271 done >${2}
1274 emacs -batch -nw --eval='
1275 (let (
1277 (nr-of-lines '${1}')
1278 (nr-of-lines-length)
1279 (output-file "'"${2}"'"))
1280 (setq nr-of-lines-length (length (number-to-string nr-of-lines)))
1281 (dotimes (i nr-of-lines t)
1282 (insert (format (format " Regel %%%dd voor de test\n" nr-of-lines-length) (1+ i))))
1283 (write-file output-file))
1284 ' 2>/dev/null
1286 The Bash version took 293 seconds and the Emacs Lisp version 59
1287 seconds. So it is about 5 times as fast.
1288 The Emacs batch gives output like:
1289 Saving file /home/cecil/temp/inputEmacs...
1290 Wrote /home/cecil/temp/inputEmacs
1291 That is why I use the 2>/dev/null.
1292 Is there a way to circumvent the generation of the above output?
1293 Because when there is an error it is also thrown away and that is not
1294 nice.
1296 From: Vassil Nikolov <vnikolov@pobox.com>
1297 Newsgroups: gnu.emacs.help
1298 Date: Sat, 10 Oct 2009 16:55:31 -0400
1299 To: help-gnu-emacs@gnu.org
1300 Envelope-To: andreas.roehler@easy-emacs.de
1302 On Sat, 10 Oct 2009 11:33:17 -0700 (PDT), Decebal <cldwesterhof@gmail.com> said:
1303 > ...
1304 > local i
1305 > local length=${#1}
1306 > for i in $(seq ${1}) ; do
1307 > printf " Regel %${length}d voor de test\n" ${i}
1308 > done >${2}
1310 translates to
1312 emacs -Q -batch -eval '
1313 (dotimes (i '"${1}"')
1314 (princ (format " Regel %'"${#1}"'d voor de test\n" (1+ i))))
1315 ' > "${2}"
1317 ---Vassil.
1319 From: Decebal <cldwesterhof@gmail.com>
1320 Newsgroups: gnu.emacs.help
1321 Date: Sat, 10 Oct 2009 13:57:07 -0700 (PDT)
1322 To: help-gnu-emacs@gnu.org
1323 Envelope-To: andreas.roehler@easy-emacs.de
1325 On Oct 10, 10:06pm, Andreas R=F6hler <andreas.roeh...@easy-emacs.de>
1326 wrote:
1327 > > The Emacs batch gives output like:
1328 > > Saving file /home/cecil/temp/inputEmacs...
1330 > it's in files.el, save-buffer, AFAIS
1332 > (if (and modp (buffer-file-name))
1333 > (message "Saving file %s..." (buffer-file-name)))
1335 > commenting out these lines should cancel the message
1337 The problem with that is that it only works for me. But I found a way.
1338 I replaced:
1340 (write-file output-file))
1342 with:
1344 (set-visited-file-name output-file)
1345 (basic-save-buffer))
1347 But maybe there should be more consideration for the possibility that
1348 Emacs is used as a batch program.
1350 > > Wrote /home/cecil/temp/inputEmacs
1352 I still have to find something for this.
1354 That is not possible I am afraid. In the C-source there is a call to
1355 message_with_string.
1357 * vectors and lists
1359 From: pjb@informatimago.com (Pascal J. Bourguignon)
1360 Newsgroups: gnu.emacs.help
1361 Date: Fri, 09 Oct 2009 19:19:49 +0200
1362 To: help-gnu-emacs@gnu.org
1363 Envelope-To: andreas.roehler@easy-emacs.de
1365 Nordlöw <per.nordlow@gmail.com> writes:
1367 > If I have an association list say,
1369 > '(
1370 > ("key" sym1 val1 num1)
1371 > ("key2" sym2 val2 num2)
1374 > , where each entry is a fixed sequence of various objects.
1376 If this is an a-list, then you could write it as:
1378 (("key1" . (sym1 val1 num1))
1379 ("key2" . (sym2 val2 numb2)))
1381 to show that it is a list of cons cells.
1383 (a . (b c d)) <=> (a b c d), but the first notation shows that you
1384 consider it as a list of cons, and notably that you don't expect nil
1385 ie. () to be in the toplevel of the a-list.
1387 Also, if we write the a-list properly like this, we can better answer
1388 the following question:
1390 > I might
1391 > aswell use a vector to represent an entry in this alist, right?
1393 You cannot use a vector instead of the cons cells of the a-list, but
1394 you can use a vector as a value of an a-list entry. Values can be of
1395 any type. In the case of emacs lisp, you could also easily use
1396 vectors (or any other type) as keys in an a-list, since it uses equal
1397 to compare keys.
1399 (("key1" . [sym1 val1 num1])
1400 ("key2" . [sym2 val2 num2])
1401 ([?k ?e ?y ?3] . [sym3 val3 num3]))
1403 > In this case, what do I gain by using a vector instead of list?
1405 In general, vectors take half the space of lists, and access to the
1406 nth element is done in O(1) instead of O(n) with lists. However,
1407 adding or removing an element in a vector is O(n) while in the case of
1408 lists, it may be O(1) (prepending an element or removing the first
1409 element or one of the few firsts elements) or O(n) (inserting,
1410 appending an element or removing the nth element).
1412 > What about performance?: aref() faster than nth() only for large
1413 > vectors?
1415 aref has to compute a multiplication and a sum, before doing one
1416 memory load to get the element. In the case of emacs lisp, the
1417 multiplication is always by the same fixed factor AFAIK.
1419 nth has to do n memory loads to get the element.
1421 So indeed, aref will probably be faster than nth, even for indices as
1422 small as 1 or 0.
1424 > Is there vector-variant of assoc()?
1426 No. Unless you count hash-tables as a vector variant.
1428 > If not, why?
1430 Because there's no point. The advantage of using a list for a-list,
1431 apart from the historical simplicity, is that you can easily prepend
1432 the a-list with new associations, and therefore use the a-list in a
1433 purely functional way.
1435 (defun f (bindings)
1436 (let ((val (cdr (assoc 'x bindings))))
1437 (if (zerop val)
1438 (list val)
1439 (cons val (f (cons (cons 'x (1- val)) bindings))))))
1441 (let ((bindings '((y . 0) (x . 1))))
1442 (list (f (cons (cons 'x 2) bindings))
1443 (cdr (assoc 'x bindings))))
1444 ;; --> ((2 1 0) 1)
1446 Note: you could use (require 'cl) (acons key value a-list)
1447 instead of (cons (cons key value) a-list).
1449 > Has any one already written such a function?
1451 Not AFAIK, but you can write it. However, the semantics of assoc
1452 require a sequential search of the keys in the list, so there would be
1453 no gain. On the contrary, we would now have O(n) complexity to
1454 prepend a new entry to the a-vector.
1457 __Pascal Bourguignon__