simple useful functions from Tak Ota
[elbb.git] / code / elbb.el
blobefb55749c8d42ba139cbd6a01ec020db7bc998c9
1 * Simple useful functions
3 Date: Thu, 28 Oct 2010 11:56:15 -0700
4 From: Tak Ota <Takaaki.Ota@am.sony.com>
5 To: <emacs-devel@gnu.org>
6 Subject: simple useful functions
7 Envelope-To: andreas.roehler@online.de
9 If you think the following two functions are universally useful please
10 consider incorporating them in simple.el or any appropriate package.
11 If not disregard.
13 -Tak
15 (defun collect-string (regexp &optional num)
16 "Collect strings of REGEXP (or optional NUM paren) from the
17 current buffer into a collection buffer."
18 (interactive "sCollect string (regexp): \nP")
19 (let ((collection-buffer
20 (get-buffer-create (format "*Collection of \"%s\" *" regexp))))
21 (with-current-buffer collection-buffer (erase-buffer))
22 (save-excursion
23 (goto-char (point-min))
24 (while (re-search-forward regexp nil t)
25 (let ((str (match-string (or num 0))))
26 (if str
27 (with-current-buffer collection-buffer
28 (insert str)
29 (or (zerop (current-column))
30 (insert "\n")))))))
31 (pop-to-buffer collection-buffer)
32 (goto-char (point-min))))
34 (defun source (script &optional shell keep-current-directory)
35 "Source the specified shell script.
36 Source the shell SCRIPT and import the environment into this
37 emacs. The optional SHELL specifies the shell other than the
38 default `shell-file-name'. When KEEP-CURRENT-DIRECTORY is nil,
39 which is the default, the current directory is temporarily
40 changed to the directory where the script resides while sourcing
41 the script."
42 (interactive "fscript file: ")
43 (if (null shell)
44 (setq shell shell-file-name))
45 (with-temp-buffer
46 (unless keep-current-directory
47 (setq default-directory (file-name-directory script)))
48 (call-process shell nil t nil "-c" (concat "source " script "; printenv"))
49 (while (re-search-backward "^\\([^=]+\\)=\\(.*\\)$" nil t)
50 (setenv (match-string 1) (match-string 2)))))
52 * How highlight 16 or more occurrences of same character?
54 To: help-gnu-emacs@gnu.org
55 From: Oleksandr Gavenko <gavenko@bifit.com.ua>
56 Date: Mon, 13 Sep 2010 14:20:21 +0300
57 Subject: Re: How highlight 16 or more occurrences of same character?
58 Envelope-To: andreas.roehler@easy-emacs.de
60 On 13.09.2010 11:55, Deniz Dogan wrote:
61 > 2010/9/13 Oleksandr Gavenko<gavenko@bifit.com.ua>:
62 >> I use
64 >> (font-lock-add-keywords
65 >> 'c-mode
66 >> '(
67 >> ("\\(.\\)\\1\\1\\1\\1\\1\\1\\1\\1\\1\\1\\1\\1\\1\\1\\1\\1" 0
68 >> 'font-lock-warning-face t)
69 >> ))
71 >> but don't know how make variable back links count.
73 > Haven't tested this, but it should be similar to my reply to your
74 > other thread, with the added comma in {15,}...
76 > (font-lock-add-keywords 'c-mode '(("\\(.\\)\\1\\{15,\\}" 0
77 > 'font-lock-warning-face t)))
79 Thanks for tip. It work!
81 * define-key KEYMAP...
83 From: Miles Bader <miles@gnu.org>
84 To: Stefan Monnier <monnier@IRO.UMontreal.CA>
85 Date: Fri, 23 Jul 2010 18:32:55 +0900
86 Subject: Re: substitute-key-definition vs. define-key MAP [remap ...]
88 Stefan Monnier <monnier@IRO.UMontreal.CA> writes:
89 > The `remap' thingy operates at the level of `key-binding'. I.e. after
90 > the command loop reads a key-sequence, it looks it up in the keymaps to
91 > turn the key-sequence into a command, and then it looks this up in the
92 > `remap' sub-keymap(s) to see if it got remapped.
94 And it's _extremely_ handy for some uses... e.g., one of my favorite
95 tricks is little minor-modes that bind [remap self-insert-command]...
97 E.g.:
99 ;;; caps-lock-mode, Miles Bader <miles@gnu.org>
101 (defvar caps-lock-mode-map
102 (let ((map (make-sparse-keymap)))
103 (define-key map [remap self-insert-command] 'self-insert-upcased)
104 map))
106 (define-minor-mode caps-lock-mode
107 "When enabled, convert all self-inserting characters to uppercase."
108 :lighter " CapsLock")
110 (defun self-insert-upcased (arg)
111 (interactive "p")
112 (setq last-command-char (upcase last-command-char))
113 (self-insert-command arg))
115 -miles
118 Politics, n. A strife of interests masquerading as a contest of
119 principles. The conduct of public affairs for private advantage.
121 * LISP-3
123 To: emacs-devel@gnu.org
124 From: David Kastrup <dak@gnu.org>
125 Date: Fri, 23 Jul 2010 12:03:55 +0200
126 Subject: Re: substitute-key-definition vs. define-key MAP [remap ...]
128 Stefan Monnier <monnier@IRO.UMontreal.CA> writes:
130 >> If I understand you correctly, that won't get into XEmacs any time
131 >> soon. Do you really mean that `define-key' is allowed to effectively
132 >> change the command binding of a symbol globally, so that its function
133 >> definition is ignored in the context of interpreting keystrokes? Ie,
134 >> `define-key' now turns Emacs into what is effectively a LISP-3?
136 > I don't know what "ignored in the context of interpreting keystrokes"
137 > means, nor what LISP-3 means.
139 I think a LISP-2 has both function and value cells associated with a
140 symbol (never mind print name and property list in that kind of
141 accounting), making Scheme a LISP-1.
144 David Kastrup
146 * search-whitespace-regexp
148 To: help-gnu-emacs@gnu.org
149 From: Daniel Pittman <daniel@rimspace.net>
150 Date: Sat, 03 Jul 2010 23:14:59 +1000
151 Subject: Re: Why does this happen in regexp isearch?
153 Deniz Dogan <deniz.a.m.dogan@gmail.com> writes:
155 > I open cus-edit.el.gz in Emacs and do a regexp isearch for:
157 > ^ ;;
159 > (Beginning of line, then two spaces, then two semicolons.)
161 > For some reason that I don't understand, it seems to match "beginning
162 > of line, any number of spaces OR tabs, then two semicolons". When I
163 > search for:
165 > ^ \{2\};;
167 > ...it finds what I'm looking for. Why is this?
169 (defcustom search-whitespace-regexp (purecopy "\\s-+")
170 "If non-nil, regular expression to match a sequence of whitespace chars.
171 This applies to regular expression incremental search.
172 When you put a space or spaces in the incremental regexp, it stands for
173 this, unless it is inside of a regexp construct such as [...] or *, + or ?.
174 You might want to use something like \"[ \\t\\r\\n]+\" instead.
175 In the Customization buffer, that is `[' followed by a space,
176 a tab, a carriage return (control-M), a newline, and `]+'.
178 When this is nil, each space you type matches literally, against one space."
179 :type '(choice (const :tag "Find Spaces Literally" nil)
180 regexp)
181 :group 'isearch)
183 Regards,
184 Daniel
186 ✣ Daniel Pittman ✉ daniel@rimspace.net ☎ +61 401 155 707
187 ♽ made with 100 percent post-consumer electrons
189 * tabs instead of spaces in C mode
191 Date: Sun, 2 May 2010 22:13:04 -0400
192 From: Steve Revilak <steve@srevilak.net>
193 To: help-gnu-emacs@gnu.org
194 Subject: Re: Emacs behavior
196 >From: "VICTOR TARABOLA CORTIANO"=20
198 >I would like to change the default behavior of Emacs in C mode, I
199 >use tabs instead of spaces for editing, so I changed my .emacs[1]
200 >to behave the way I want.
202 >For instance, I want:
204 >function()
206 > commands;
209 >But Emacs automatically modify the text to:
211 >function()
213 > commands;
216 >It works the way I want in text-mode, but I want syntax highligting.
218 >I would like Emacs to behave like mg or vi in C mode.
220 I've been working on a C project where most of the source code was
221 indented using tabs, and I wanted my edits to follow the same
222 formatting. A solution that worked for me was
225 * Local Variables:
226 * c-basic-offset: 8
227 * indent-tabs-mode: t
228 * End:
231 I added this Local Variables block at the bottom of each .c file I
232 needed to edit. (I didn't want to change the behavior of C mode
233 globally; instead, I merely wanted to change it in a few specific
234 files.)
236 Putting something like
238 (setq c-basic-offset 8
239 indent-tabs-mode t)
241 into ~/.emacs may give some of the behavior you're looking for.
243 You can also try
245 (setq c-indentation-style "linux")
247 For a description of cc-mode styles, these are good places to start:
249 http://www.gnu.org/software/emacs/manual/html_node/ccmode/Choosing-a-Sty=
250 le.html#Choosing-a-Style
251 http://www.gnu.org/software/emacs/manual/html_node/ccmode/Built_002din-S=
252 tyles.html#Built_002din-Styles
254 Finally, c-insert-tab-function might also be useful to you.
256 http://www.gnu.org/software/emacs/manual/html_node/ccmode/Indentation-C=
257 ommands.html#index-TAB-17
259 Steve
261 * Using Git to manage your Emacs changes
263 To: emacs-devel@gnu.org
264 From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
265 Date: Fri, 09 Apr 2010 08:04:16 +0200
266 Subject: Re: Using Git to manage your Emacs changes
268 John Wiegley <jwiegley@gmail.com> writes:
270 > On Apr 7, 2010, at 4:37 PM, Thierry Volpiatto wrote:
272 >> Why not using a stack of patchs like stgit or mercurial qpatchs.
273 >> You can then apply these patchs to bzr repo.
275 > I don't see how stgit improves anything. Also, I'm using git-bzr
276 > because I need to fetch the mirrored commits back into Git immediately
277 > after pushing, and I'm not sure how often the GitHub emacs mirror
278 > updates itself.
280 I use http://repo.or.cz/w/emacs.git
282 This repo is converted to a hg repo locally.
283 I have cloned this hg repo to another hg repo that handle qpatchs.
284 So i have three repos:
286 git, hg, hg qpatch.
288 1) on git repo: git pull
290 2) on hg repo: hg convert <last git revision>
291 (when the repo exists, hg convert is as fast as a pull)
293 3) on hg qpatch repo: hg pull
295 4) make some new patchs on hg qpatch repo (i use DVC and
296 anything-mercurial.el)
298 5) Then you can send patchs to Emacs or apply these patchs to bzr repo
299 directly.(your patchs have to be in git format)
301 The same can be done with stg.(with only 2 repo)
304 Thierry Volpiatto
305 Gpg key: http://pgp.mit.edu/
307 * Guile in Emacs (was: integer overflow)
309 From: Thomas Lord <lord@emf.net>
310 To: rms@gnu.org
311 Date: Sun, 11 Apr 2010 16:33:57 -0700
312 Subject: Re: Guile in Emacs (was: integer overflow)
314 I'd like to correct some accounts of history.
316 On Mon, 2010-03-08 at 22:19 -0500, Richard Stallman wrote:
317 > RS> If it only supports something like Emacs Lisp then it is not much of
318 > RS> an advance. Guile supports Scheme and Javascript as well as Emacs Lisp.
320 > It also supports multithreading, which IMHO is a big deal as well,
321 > perhaps more important than bignums.
323 > I think the support for multiple languages is the biggest advance.
324 > I hope support for other popular extension languages will be
325 > implemented
326 > eventually -- that was the original goal of Guile.
328 Your recollection there seems to me to be mistaken. It
329 was a long time ago but I distinctly remember things
330 differently. As I recall:
332 The original GNU Manifesto described a unix-like foundation
333 with a heavy emphasis on a Lisp-centric user-space.
335 In the early 1990s, when I worked at the FSF, several
336 of the hackers there (including me) understood the GNU
337 vision to imply that many interactive programs - not just
338 Emacs - would be extensible in a dialect of Lisp. We
339 mostly shared the opinion that Scheme was an appropriate
340 dialect.
342 Before I got to the FSF people had already started on a
343 GNU Extension Language Library - an embeddable Scheme
344 interpreter. As the (bad) "joke" goes: they code-named the
345 project "revoc" but upon reflection that's just a "cover".
346 (Get it? Spooky, ain't it?)
348 Revoc was (at least back then) going nowhere fast. Later,
349 while working on a spreadsheet program, I attempted to start
350 a new attempt at a scheme-based extension language library.
351 You were (rightly, I suppose) upset that there were higher
352 priorities. You also pointed out that I was probably wasting
353 time by starting from scratch and that I ought to have started
354 from some existing Scheme implementation. I don't recall
355 if you specifically suggested SCM but I think you might have.
356 So there were two attempts at a Scheme-based GNU extension
357 library down - and one to go.
359 A year or two later I went to work for Cygnus and we, there,
360 initially agreed to invest in making a scheme-based extension
361 language library for GNU programs. This was originally named
362 "GEL" (for GNU Extension Language) until the company lawyers
363 warned that "GEL" was a trademark for another program. It
364 was renamed Guile (a cognitive relative of "scheme" and a
365 pun for "Guy L."). You were informed of this work and were
366 encouraging. I forget at which stage of this process you
367 declared Guile to be a "GNU project" but I'm sure the extant
368 public record has it somewhere.
370 Around that time, Sun was beginning to announce and promote
371 Java. They also hired John Ousterhout and began declaring
372 "Tcl is to be the scripting language for the Internet!" Back
373 then, Sun was a particularly influential company. On a
374 technical level, Tcl was a horribly regressive language -
375 a giant step backwards in many subtle but important ways.
376 Its license was a free software license but it was uncomforable
377 at the time to have Sun pushing it so heavily because of
378 the technical problems and because of doubts about Sun's
379 motives and control over the software.
381 A faction arose within Cygnus that argued that Cygnus should
382 divest from the Guile project and adopt Tcl instead. I made
383 you aware of that and of Sun's swagger, regarding Tcl.
385 Around the same time, two of Prof. Ousterhout's graduate
386 students, John Blow and Adam Sah, were proposing that
387 the TCL language be *altered* in some small but significant
388 ways that would facilitate its translation into reasonable
389 efficient Scheme code. Their altered version of Tcl was
390 largely but not completely upward compatible with standard
391 Tcl. By applying a Scheme->C compiler to the generated
392 Scheme code, they were able to benchmark far better than
393 standard Tcl in many cases.
395 And around that same time I was independently proposing
396 similar things. Mssrs. Blow, Sah, and I compared notes
397 and so forth.
399 Around that time, also, Python was beginning to gain
400 recognition in the US (it already had a foothold in Europe).
401 Blow, Sah, and I had dinner with Von Rossum and tried to
402 persuade him to add LAMBDA and proper tail calls and perhaps
403 continuations to the language so that it could be usefully
404 translated to Scheme. He did not agree. Back at Cygnus,
405 the oppositional factions had fun lampooning lisp syntax
406 and pointing to examples like Python of what was better.
408 Some of my notes to you mysteriously transmogrified into
409 a USENET post which the archives show as having you in the
410 "From:" line and with my signature at the bottom of the
411 message. This sparked what people, to this day,
412 still call "The TCL war".
416 The "original goal" of Guile was most definitely *not*
417 to support multiple languages. Rather, that was a goal
418 that was established as a tactic in response to early
419 competition with Guile.
421 Moreover, it was *never*, in the early days, a goal
422 that Guile support other popular extension languages.
423 That was something that you added and that I (regrettably)
424 agreed to in response to the perceived threat of Tcl
425 and to a lesser extent Python.
427 And: it was *never* in those years a goal to support
428 any of those other languages exactly. It was *always*
429 a goal to have a Tcl-like syntax, a Python-like syntax,
430 and a C-like syntax for Guile Scheme. But tight
431 compatibility with those languages was *not* the goal.
432 The notion wasn't so much to "support Tcl" (or any other
433 language) as to have a hyper-flexible syntax and to
434 support, via libraries, environments with all of the
435 convenience features of a shell-like language like Tcl.
437 Early on after we adopted that tactic you and I and
438 a few others had some lengthy discussions about adding
439 Emacs Lisp to the list of languages that Guile could
440 run. We ran into some severe issues such as the
441 difference between 'NIL in Emacs lisp and '() and Scheme.
442 Perhaps not to you but to several other of us it became
443 fairly clear, back then, that a strictly compatible
444 Emacs lisp would never sit comfortably alongside a
445 proper Scheme environment. Just as we would need to make
446 a "Tcl-like" language that would break some Tcl code,
447 we would need to break some Emacs lisp code, if indeed
448 we ultimately wanted to bother trying to support any.
449 (That is part of why, at Cygnus, I built a from-scratch
450 multi-buffer, self-documenting, extensible text editor
451 in Scheme with multi-font and proportionally-spaced
452 font capabilities that, at the time, GNU Emacs wasn't
453 close to achieving. The notion was to see how far I could
454 get just leapfrogging over GNU Emacs instead of trying
455 to retrofit it.)
457 Now, years have passed. In recent years, I gather,
458 the implementation of Guile has been fairly radically
459 altered so that it is now more of a bytecode VM with
460 primitive LISP-ish types. In some sense, it has begun
461 to drift away from being primarily a Scheme to being
462 more in the category of JVM or the Mono VM. It is easier
463 to target multiple languages to such a VM but no less
464 easy to make them interoperate cleanly in a way one would
465 want to live with for the long run.
467 So Guile's goals have shifted. It was once (originally)
468 to be a tight, clean, fun Scheme environment with some
469 alternative syntaxes and specialized environments -- and
470 only later did it become the kind of heterogenous language
471 environment we see it moving towards today.
473 So, again:
475 > I hope support for other popular extension languages will be
476 > implemented eventually -- that was the original goal of Guile.
478 Nah. The heck it was. That's just not true.
480 Now, there is a separate question: is that goal to support
481 other popular extension languages a better idea or a worse
482 idea (or not importantly different) than the original goal
483 of a nice, tight, fun Scheme-based system?
485 I have my opinion ("far worse!") but it's not my intent
486 to argue for that opinion here. Just to set history straight.
488 That multi-lingual stuff was *not* the original goal of Guile.
489 On the contrary, the multi-lingual goals only came up at all
490 because of a perceived crisis sparked by Sun's announcement
491 that Tcl was to become the ubiquitous scripting language of
492 the Internet.
496 * emacs daemon.. but quietly
498 To: help-gnu-emacs@gnu.org
499 From: Richard Riley <rileyrgdev@gmail.com>
500 Date: Mon, 22 Mar 2010 20:02:22 +0100
501 Subject: Re: emacs daemon.. but quietly
503 tomas@tuxteam.de writes:
505 > On Fri, Mar 05, 2010 at 02:24:44PM +0100, Gary . wrote:
506 >> Is there any way to stop emacs, run with --daemon, printing out
507 >> details about all of the config files it is loading? At the moment I
508 >> see
510 > If I understand you correctly, you want to suppress stdout/stderr output
511 > of emacs --daemon?
513 > You might just redirect that to /dev/null like so:
515 > emacs --daemon > /dev/null 2>&1
517 > (or did I misunderstand you completely?)
519 >> ("emacs" "--quiet")
520 >> Loading charset...
521 >> Loading charset...done
522 >> (etc.) which is ugly since I want to start the server, when
523 >> appropriate, when I start my login shell by doing something like
525 >> function serverExists {
526 >> TMPDIR=${TMPDIR-/tmp};
527 >> TMPFILE="${TMPDIR}/ps-output.$$";
529 >> ps > ${TMPFILE}
530 >> grep -q 'emacs-X11' ${TMPFILE}
531 >> SERVER_STARTED=$?;
532 >> rm ${TMPFILE}
534 >> return $SERVER_STARTED;
535 >> }
537 >> if serverExists ; then
538 >> export EMACS_SERVER="emacs already started"
539 >> else
540 >> emacs --daemon --quiet
541 >> export EMACS_SERVER="emacs started here"
542 >> fi
543 >> echo $EMACS_SERVER
545 >> in my .bashrc.
547 > Hm. I don't quite understand this part. Besides, it seems a roundabout
548 > way. What are you trying to achieve?
550 > Regards
551 > -- tomás
554 As is emacs --daemon IMO.
556 In emacs 23 using the alternate editor setting. My "edit" script is:-
558 ,----
559 | #!/bin/bash
560 | # edit
561 | export GDK_NATIVE_WINDOWS=1
562 | exec emacsclient --alternate-editor="" -c "$@"
563 `----
565 Its in the man page for emacsclient and the wiki.
568 ASCII ribbon campaign ( )
569 - against HTML email X
570 & vCards / \
572 * Reading a sequence of bytes as one integer
574 Date: Sat, 13 Mar 2010 20:49:28 +0200
575 From: Eli Zaretskii <eliz@gnu.org>
576 To: help-gnu-emacs@gnu.org
577 Subject: Re: Emacs Lisp - Reading a sequence of bytes as one integer
579 > Date: Sat, 13 Mar 2010 11:56:17 -0500 (EST)
580 > From: Jeff Clough <jeff@chaosphere.com>
582 > I have a (mostly) binary file I want to inspect. There are several
583 > places in this file where I want to read a series of bytes (in this
584 > case, three consecutive bytes) and treat those bytes as a single
585 > integer. I can grab them as a buffer-substring obviously, but I'm at
586 > a loss for how to make that into the integer I need, nor can I find
587 > anything obvious that would be easier.
589 Take a look at bindat.el (it's bundled with Emacs).
591 * how to access a large datastructure efficiently?
593 To: help-gnu-emacs@gnu.org
594 From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
595 Date: Thu, 04 Mar 2010 17:09:35 +0100
596 Subject: Re: how to access a large datastructure efficiently?
598 Andreas Röhler <andreas.roehler@easy-emacs.de> writes:
600 > Thierry Volpiatto wrote:
601 >> Andreas Röhler <andreas.roehler@easy-emacs.de> writes:
603 >>> Thierry Volpiatto wrote:
604 >>>> Thierry Volpiatto <thierry.volpiatto@gmail.com> writes:
605 >>>>
606 >>>>> Hi,
607 >>>>>
608 >>>>> Christian Wittern <cwittern@gmail.com> writes:
609 >>>>>
610 >>>>>> Hi there,
611 >>>>>>
612 >>>>>> Here is the problem I am trying to solve:
613 >>>>>>
614 >>>>>> I have a large list of items which I want to access. The items are in
615 >>>>>> sequential order, but many are missing in between, like:
616 >>>>>>
617 >>>>>> (1 8 17 23 25 34 45 47 50) [in reality, there is a value associated
618 >>>>>> with this, but I took it out for simplicity]
619 >>>>>>
620 >>>>>> Now when I am trying to access with a key that is not in the list, I
621 >>>>>> want to have the one with the closest smaller key returned, so for 6
622 >>>>>> and 7 this would be 1, but for 8 and 9 this would be 8.
623 >>>>>>
624 >>>>>> Since the list will have thousands of elements, I do not want to simply
625 >>>>>> loop through it but am looking for better ways to do this in Emacs lisp.
626 >>>>>> Any ideas how to achieve this?
627 >>>>> ,----
628 >>>>> | (defun closest-elm-in-seq (n seq)
629 >>>>> | (let ((pair (loop with elm = n with last-elm
630 >>>>> | for i in seq
631 >>>>> | if (and last-elm (< last-elm elm) (> i elm)) return (list last-elm i)
632 >>>>> | do (setq last-elm i))))
633 >>>>> | (if (< (- n (car pair)) (- (cadr pair) n))
634 >>>>> | (car pair) (cadr pair))))
635 >>>>> `----
636 >>>>>
637 >>>>> That return the closest, but not the smaller closest, but it should be
638 >>>>> easy to adapt.
639 >>>> Case where your element is member of list, return it:
640 >>>>
641 >>>> ,----
642 >>>> | (defun closest-elm-in-seq (n seq)
643 >>>> | (let ((pair (loop with elm = n with last-elm
644 >>>> | for i in seq
645 >>>> | if (eq i elm) return (list i)
646 >>>> | else if (and last-elm (< last-elm elm) (> i elm)) return (list last-elm i)
647 >>>> | do (setq last-elm i))))
648 >>>> | (if (> (length pair) 1)
649 >>>> | (if (< (- n (car pair)) (- (cadr pair) n))
650 >>>> | (car pair) (cadr pair))
651 >>>> | (car pair))))
652 >>>> `----
653 >>>> For the smallest just return the car...
654 >>>>
655 >>> if n is member of the seq, maybe equal-operator too
657 >>> (<= last-elm elm)
659 >>> is correct?
661 >> No, in this case:
663 >> if (eq i elm) return (list i) ==> (i) ; which is n
665 >> and finally (car pair) ==> n
668 > Hmm, sorry being the imprecise,
669 > aimed at the first form, whose result equals the the second form once implemented this "="
671 Ok, i understand, yes, we can do what you say and it's more elegant, i
672 just notice also i forget to remove a unuseful else:
674 ,----
675 | (defun closest-elm-in-seq (n seq)
676 | (let ((pair (loop with elm = n with last-elm
677 | for i in seq
678 | if (and last-elm (<= last-elm elm) (> i elm)) return (list last-elm i)
679 | do (setq last-elm i))))
680 | (if (< (- n (car pair)) (- (cadr pair) n))
681 | (car pair) (cadr pair))))
682 `----
684 That should work the same.
685 Thanks. ;-)
688 Thierry Volpiatto
689 Gpg key: http://pgp.mit.edu/
691 To: help-gnu-emacs@gnu.org
692 From: Andreas Politz <politza@fh-trier.de>
693 Date: Thu, 04 Mar 2010 17:49:54 +0100
694 Subject: Re: how to access a large datastructure efficiently?
696 [ ... ]
698 I don't know how hash-table could help in this case, but maybe you want
699 to consider binary trees, as implemented in the avl-tree.el package.
700 Though, there is no function for finding the closest member with respect
701 to some data and a distance-function, but see below.
703 Finding a (closest) member should be constraint in logarithmic time.
705 (require 'avl-tree)
707 (setq avl (avl-tree-create '<))
709 (dotimes (i 2000)
710 (when (= 0 (% i 4))
711 (avl-tree-enter avl i)))
713 (avl-tree-member avl 80)
714 => 80
715 (avl-tree-member avl 70)
716 => nil
718 (defun avl-tree-closest-member (tree data delta-fn)
719 ;; delta-fn : data x data -> Z
720 (flet ((comp-delta (node)
721 (funcall delta-fn data
722 (avl-tree--node-data node))))
723 (let* ((node (avl-tree--root tree))
724 closest
725 (delta most-positive-fixnum)
726 (compare-function (avl-tree--cmpfun tree))
727 found)
728 (while (and node
729 (not found))
730 (when (< (comp-delta node) delta)
731 (setq delta (comp-delta node)
732 closest node))
733 (cond
734 ((funcall compare-function data (avl-tree--node-data node))
735 (setq node (avl-tree--node-left node)))
736 ((funcall compare-function (avl-tree--node-data node) data)
737 (setq node (avl-tree--node-right node)))
739 (setq found t))))
740 (if closest
741 (avl-tree--node-data closest)
742 nil))))
744 (mapcar
745 (lambda (data)
746 (avl-tree-closest-member
747 avl data (lambda (n1 n2)
748 (abs (- n1 n2)))))
749 '(1001 1002 1003 1004))
751 => (1000 1004 1004 1004)
755 * cedet and auto-complete
757 To: help-gnu-emacs@gnu.org
758 From: Richard Riley <rileyrgdev@gmail.com>
759 Date: Tue, 02 Mar 2010 08:26:20 +0100
760 Subject: Re: cedet and auto-complete
762 Richard Riley <rileyrgdev@gmail.com> writes:
764 > Just another quick poll to see if anyone has got the new cedet working
765 > with auto-complete. cedet has developed its own completion UIs but
766 > ideally I would like to use auto-complete as I do in all other
767 > modes. Has anyone a workaround or instructions for allowing this to
768 > work? Unfortunately if you only enable the minimum features which then
769 > turns off the cedet completion mechs, it also turns off the semantic
770 > navigation features which kind of detracts from its usefulness.
772 > Any help or pointer appreciated,
774 > r.
777 OK, user errror to a degree - I have now moved back to company-mode
778 (newer version is available) and made sure it was on the load path
779 before the version that came with nxhtml.
781 Works well.
783 Here is my simple setup which turns on ispell support in text-mode and
784 so works in gnus message modes.
786 (add-to-list 'load-path "~/.emacs.d/company-mode")
787 (require 'company)
788 (add-hook 'text-mode-hook (lambda()(add-to-list 'company-backends 'company-ispell)))
789 (require 'company-ispell)
790 (global-company-mode)
792 This is with cedet 1.0pre7 and company-mode 0.5
794 * hide-show squash-minor-modes
796 From: Thien-Thi Nguyen <ttn@gnuvola.org>
797 To: Andreas Roehler <andreas.roehler@online.de>
798 Subject: Re: hideshow condensed view
799 Date: Thu, 25 Feb 2010 16:21:27 +0100
801 () Andreas Roehler <andreas.roehler@online.de>
802 () Thu, 25 Feb 2010 15:24:24 +0100
804 thanks a lot maintaining hideshow.el.
806 In the last few years, i can't take any credit for its improvement.
807 See Emacs' ChangeLog for those who actually merit the appreciation...
809 Use it as default outline-mode now.
811 Cool.
813 Just one thing remains so far: hs displays an empty
814 line between headers. Would prefer a more condensed
815 view.
817 You must have good eyes (still); i use hideshow not
818 for density but for spaciousness.
820 Might it be possible to introduce a function
821 `hide-all-empty-lines', inclusive a variable which may
822 be customized?
824 Yeah, it would be possible, but i tend to think of empty lines outside
825 the (top-level) blocks as also outside the scope of hideshow. It's like
826 in the book GEB, there is the tree and there is the stuff outside the
827 tree. Get it?
829 I am already troubled by hideshow straying from its conceptual (both
830 meanings) simplicity -- a block is between matching parens, no more no
831 less -- but that's just me...
833 All this is to say, you probably should write a "squash minor mode",
834 which puts the invisible property on an overlay for text that matches
835 the empty line regexp (which of course, could be made customizable), at
836 which point my advice would be to add a hook to `hs-hide-all' to enable
837 this mode.
839 Here's a quick (but tested (lightly)) sketch:
841 (defvar empty-lines-rx "^\\s-*\\(\n\\s-*\\)+")
843 (defvar empty-lines nil
844 "List of empty lines (overlays).")
846 (defvar squash-minor-mode nil)
848 (defun squash-minor-mode ()
849 (interactive)
850 (setq squash-minor-mode (not squash-minor-mode))
851 (if squash-minor-mode
852 (save-excursion
853 (goto-char (point-min))
854 (while (re-search-forward empty-lines-rx nil t)
855 (let ((ov (make-overlay (match-beginning 0) (match-end 0))))
856 (overlay-put ov 'display "")
857 (push ov empty-lines))))
858 (mapc 'delete-overlay empty-lines)
859 (setq empty-lines nil))
860 (message "empty-lines squashing %s" (if squash-minor-mode 'on 'off)))
862 Have fun!
866 * questioning let
868 Andreas Roehler <andreas.roehler@online.de> writes:
870 > Hi,
872 > behaviour of the example code below puzzles me. Would
873 > expect setting of arg by external function, but inside
874 > `let', recognised. But remains `1'.
876 > (defun arg-setting ()
877 > (interactive)
878 > (let ((arg 1))
879 > (message "%s" arg)
880 > (arg-extern arg)
881 > (message "%s" arg)))
883 > (defun arg-extern (arg)
884 > (setq arg (1- arg)))
886 > Any help?
888 From: David Kastrup <dak@gnu.org>
889 Date: Wed, 24 Feb 2010 19:10:38 +0100
890 To: help-gnu-emacs@gnu.org
891 Subject: Re: questioning let
893 >> The argument binding in arg-extern is the innermost one and consequently
894 >> the only affected one. If you make the function argument-less, it will
895 >> likely work as expected by you, affecting the binding in arg-setting.
897 > That works, thanks a lot!
898 > However, stored in some eil.el, get a compiler warning than:
901 > In arg-extern:
902 > eil.el:9:9:Warning: reference to free variable `arg'
903 > eil.el:9:17:Warning: assignment to free variable `arg'
905 > Would think a useless warning, as the compiler should know being inside a let (?)
907 The warning is completely accurate since arg-extern can be called from
908 outside arg-setting, in which case it will assign to a global variable
909 called "arg".
911 Whether or not some let-binding might be effective at the point of
912 calling arg-extern is unknown to the compiler.
914 In a Lisp variant with lexical binding (like Common Lisp or Scheme),
915 arg-extern has no way to fiddle with the let-binding of arg-setting: it
916 is completely inaccessible by name outside of arg-setting itself.
919 David Kastrup
921 From: pjb@informatimago.com (Pascal J. Bourguignon)
922 Date: Wed, 24 Feb 2010 12:59:22 +0100
923 To: help-gnu-emacs@gnu.org
924 Subject: Re: questioning let
926 let is equivalent to lambda:
928 (let ((a 1) (b 2)) (list a b)) <=> ((lambda (a b) (list a b)) 1 2)
930 defun is binding a lambda to a function cell:
932 (defun f (a b) (list a b))
933 <=> (setf (symbol-function 'f) (lambda (a b) (list a b)))
935 Therefore you can see that calling a function defined by defun is a let
936 in disguise.
938 If you transformed your code following these equivalences, you would
939 notice that you have actually TWO variables named arg, one as parameter
940 of the function arg-extern, and one as variable in the let in
941 arg-setting.
943 The setq in arg-extern will modify only the variable parameter of
944 arg-extern. Because they have the same name, this variable hides the
945 one defined in the let of arg-setting. There's no way to access it from
946 within arg-extern.
948 If they had a different name, you could modify a variable from an outer
949 dynamic scope from an inner dynamic scope, because in emacs all the
950 variables are dynamic. But it is considered very bad form to do so:
951 this is a big side effect, and what's more, one that depends on the call
952 chain. You should avoid side effects, to increase the readability and
953 debugability of your code. Therefore you should avoid setq and setf.
954 Try to write pure function, never try to modify a variable.
956 One way to write your code would be:
958 (defun do-what-you-need-to-do-with (arg)
961 (defun arg-binding ()
962 (interactive)
963 (let ((arg 1))
964 (message "before arg = %s" arg)
965 (let ((arg (arg-extern arg)))
966 (message "after arg = %s" arg)
967 (do-what-you-need-to-do-with arg))
968 (message "original arg = %s" arg)))
970 (defun arg-extern (arg)
971 (message "arg-extern before arg = %s" arg)
972 (message "arg-extern returns = %s" (1- arg))
973 (1- arg))
975 before arg = 1
976 arg-extern before arg = 1
977 arg-extern returns = 0
978 after arg = 0
979 original arg = 1
981 If you need a global variable (perhaps because you need to keep some
982 data across command invocations), the I would advise to distringuish it
983 from the other by giving it a name surrounded by stars: *var*. Then, it
984 will have a different name, and won't be shadowed (inadvertantly) by
985 inner lets, defuns or lambdas.
987 (defvar *var* 42)
989 (defun arg-extern (arg)
990 (message "arg-extern before arg = %s" arg)
991 (setf *var* (1- arg))
992 (message "arg-extern returns = %s" *var*)
993 *var*)
995 (arg-binding)
996 var* --> 0
999 __Pascal Bourguignon__
1001 * real tab completion in shell-mode
1003 To: help-gnu-emacs@gnu.org
1004 From: Andreas Politz <politza@fh-trier.de>
1005 Date: Tue, 23 Feb 2010 19:43:58 +0100
1006 Subject: Re: real tab completion in shell-mode
1008 Nathaniel Flath <flat0103@gmail.com> writes:
1010 > Hello,
1011 > Is there any way to get shell mode to use the background shell's
1012 > (zsh or bash) tab completion as opposed to just tab-completing
1013 > filenames?  I know about ansi-term, but I hate how it steals a lot
1014 > of my keybindings and so I would prefer it if I could get this to
1015 > work in shell-mode.
1017 There is not really a clean way to implement this for different reasons.
1020 > Thanks,
1021 > Nathaniel Flath
1023 A while ago, I did it anyway and wrote a special mode for bash-shells
1024 which features
1026 + emulation of bash completion
1027 + dir-tracking (local and remote)
1028 + tracking of history files
1029 + displaying dir stack in header line
1031 I took the opportunity to comment the code somewhat, you may give it
1032 a try, if you dare:
1034 http://www.fh-trier.de/~politza/emacs/bash-mode-1_0.tgz
1036 Extract it somewhere in your load-path and read the commentary, or just
1037 M-x bash RET
1041 * reduce repeated backslashes
1043 From: Teemu Likonen <tlikonen@iki.fi>
1044 Date: Mon, 15 Feb 2010 10:51:28 +0200
1045 To: help-gnu-emacs@gnu.org
1046 Subject: Re: reduce repeated backslashes
1048 2010-02-15 09:40 (+0100), Andreas Roehler wrote:
1050 > don't know how to replace/reduce repeated backslashes from inside a
1051 > string.
1053 > Let's assume "abcdef\\\\"
1055 What is the end result you want? I guess:
1057 (replace-regexp-in-string "\\\\\\\\" "\\" "abcdef\\\\" nil t)
1058 => "abcdef\\"
1060 There are 8 backslashes in the regexp string because backslash is a meta
1061 character in Lisp strings and also in regexps. 8 backslashes in a regexp
1062 Lisp string means 2 literal backslashes. In the resulting string there
1063 is only one backslash but it is displayed as two "\\" because it's a
1064 printed representation of Lisp string.
1066 * Why can't I use xargs emacs?
1068 From: pjb@informatimago.com (Pascal J. Bourguignon)
1069 Date: Tue, 02 Feb 2010 23:40:30 +0100
1070 To: help-gnu-emacs@gnu.org
1071 Subject: Re: Why can't I use xargs emacs?
1073 Adam Funk <a24061@ducksburg.com> writes:
1075 > The emacs command can take a list of filename arguments, so why can't
1076 > I get xargs to work with it?
1078 > $ find -name '*.txt' |xargs emacs -nw
1079 > emacs: standard input is not a tty
1081 > $ grep -rl 'foo' some/path |xargs emacs -nw
1082 > emacs: standard input is not a tty
1084 emacs is an interactive program. It expects its stdin and stdout to
1085 be hooked to the terminal, where it can display a character matrix,
1086 and from which it can read user input.
1088 When you use a pipe to send paths to xargs, you disconnect the
1089 terminal from the stdin, and replace it with a pipe. When xargs forks
1090 and exec emacs, emacs inherit this pipe as stdin, and cannot get user
1091 input, but will get instead further path from grep.
1093 % echo hello > file ; ( echo -b ; echo file ) | xargs -L 1 cat
1094 hello
1096 To open several files in emacs, you could either use emacsclient, or
1097 an emacs lisp script.
1099 Launch emacs in a separate terminal: xterm -e emacs -nw &
1100 In emacs, start the server: M-x server-start RET
1101 In a shell, you can then type: find -name '*.txt' | xargs emacsclient -n
1103 Simplier would be to just open the file in emacs:
1105 Launch emacs: emacs -nw
1106 Then type: C-x C-f *.txt RET
1108 For the second case, you could type:
1109 M-: (map nil 'find-file (split-string (shell-command-to-string "grep -rl 'foo' some/path") "\n")) RET
1112 __Pascal Bourguignon__
1114 From: Harald Hanche-Olsen <hanche@math.ntnu.no>
1115 Date: Tue, 02 Feb 2010 18:51:39 -0500
1116 To: help-gnu-emacs@gnu.org
1117 Subject: Re: Why can't I use xargs emacs?
1119 + Adam Funk <a24061@ducksburg.com>:
1121 > The emacs command can take a list of filename arguments, so why can't
1122 > I get xargs to work with it?
1124 > $ find -name '*.txt' |xargs emacs -nw
1125 > emacs: standard input is not a tty
1127 $ find -name '*.txt' |xargs sh -c 'emacs -nw "$@" </dev/tty' -
1129 (untested)
1132 Harald Hanche-Olsen <URL:http://www.math.ntnu.no/~hanche/>
1133 - It is undesirable to believe a proposition
1134 when there is no ground whatsoever for supposing it is true.
1135 -- Bertrand Russell
1137 To: help-gnu-emacs@gnu.org
1138 From: Thierry Volpiatto <thierry.volpiatto@gmail.com>
1139 Date: Wed, 03 Feb 2010 08:23:52 +0100
1140 Subject: Re: Why can't I use xargs emacs?
1142 Why not a simple:
1144 emacs -nw -Q $(find . -name '*.txt')
1146 Why do you want to use xargs?
1148 Bill Marcum <marcumbill@bellsouth.net> writes:
1150 > ["Followup-To:" header set to comp.unix.shell.]
1151 > On 2010-02-02, Adam Funk <a24061@ducksburg.com> wrote:
1152 >> The emacs command can take a list of filename arguments, so why can't
1153 >> I get xargs to work with it?
1155 >> $ find -name '*.txt' |xargs emacs -nw
1156 >> emacs: standard input is not a tty
1158 >> $ grep -rl 'foo' some/path |xargs emacs -nw
1159 >> emacs: standard input is not a tty
1161 > It says: standard input is not a tty. I don't normally use emacs, so there
1162 > may be a better way to do this, but you could write a function:
1163 > my_emacs () { emacs "$@" </dev/tty >&0 2>&0 ; }
1168 Thierry Volpiatto
1170 From: Adam Funk <a24061@ducksburg.com>
1171 Date: Wed, 03 Feb 2010 14:18:58 +0000
1172 To: help-gnu-emacs@gnu.org
1173 Subject: Re: Why can't I use xargs emacs?
1175 On 2010-02-02, Bit Twister wrote:
1177 > On Tue, 02 Feb 2010 20:22:17 +0000, Adam Funk wrote:
1178 >> The emacs command can take a list of filename arguments, so why can't
1179 >> I get xargs to work with it?
1181 >> $ find -name '*.txt' |xargs emacs -nw
1182 >> emacs: standard input is not a tty
1184 >> $ grep -rl 'foo' some/path |xargs emacs -nw
1185 >> emacs: standard input is not a tty
1188 > Maybe it's the -nw switch. Try
1189 > find -name '*.txt' |xargs emacs
1191 Yes, it works without -nw, but I'm often logged into an ssh server so
1192 it's faster to open emacs in the xterm.
1194 Anyway, the solution (posted by Thierry in gnu.emacs.help) turns out
1195 to be this:
1197 emacs -nw $(find . -name '*.txt')
1199 Thanks.
1202 ..the reason why so many professional artists drink a lot is not
1203 necessarily very much to do with the artistic temperament, etc. It is
1204 simply that they can afford to, because they can normally take a large
1205 part of a day off to deal with the ravages. [Amis _On Drink_]
1207 * getting emacs from launchpad
1209 From: Lennart Borgman <lennart.borgman@gmail.com>
1210 Date: Tue, 26 Jan 2010 22:46:47 +0100
1211 To: henry atting <nsmp_01@online.de>
1212 Subject: Re: bazaar question
1214 On Tue, Jan 26, 2010 at 10:31 PM, henry atting <nsmp_01@online.de> wrote:
1215 > I find it annoying that I have to grapple with bazaar if I
1216 > want to go on building emacs from source. Do I really have to pull
1217 > the whole emacs tree starting from the first published sources in the
1218 > Roman Empire?
1220 > Tried it with `bazaar branch http://bzr.savannah.gnu.org/r/emacs/trunk/'
1221 > but stopped, unpatiently, after 20 minutes.
1222 > Is this the only way to get the latest source?
1224 You can get them from Launchpad too. That is faster since they have
1225 installed the bazaar fast server (or what it is called).
1227 bzr branch lp:emacs trunk
1229 After that updates are quick (in the trunk subdir):
1231 bzr update
1233 * debug modification of a variable
1235 From: Tassilo Horn <tassilo@member.fsf.org>
1236 To: emacs-devel@gnu.org
1237 Date: Tue, 26 Jan 2010 21:26:49 +0100
1238 Subject: Re: How to debug modification to a variable value?
1240 "alin.s" <alinsoar@voila.fr> writes:
1242 > I suggest you to use the WATCH in gdb for modifications of the
1243 > variable, AWATCH for reading of the variable, etc.
1245 > To detect the location of the variable is easy: install a breakpoint
1246 > in make-variable-buffer-local that stops when exactly the variable you
1247 > are interested about is set, then see there the location of
1248 > tg-schema-alist. Afterward you can use the x* functions from .gdbinit
1249 > of emacs to debug your problem...
1251 Thanks for that explanation. I'll try that out as soon as I find some
1252 time. And also thanks for volunteering on implementing a debug facility
1253 for cases like that.
1255 Bye,
1256 Tassilo
1258 * strip mail head to fit for elbb
1260 (defun mail2elbb (&optional beg end)
1262 (interactive "*")
1263 (let ((beg (cond (beg beg)
1264 ((region-active-p)
1265 (region-beginning))
1266 (t (point-min))))
1267 (end (cond (end end)
1268 ((region-active-p)
1269 (copy-marker (region-end)))
1270 (t (point-max)))))
1271 (save-restriction
1272 (narrow-to-region beg end)
1273 (goto-char beg)
1274 (mail2elbb-intern beg end)
1275 (widen))))
1277 (defun mail2elbb-intern (beg end)
1278 (while (search-forward "=A0" nil t 1)
1279 (replace-match ""))
1280 (goto-char beg)
1281 (let ((end (progn (while
1282 (re-search-forward "^[[:alpha:]-]+To:" nil t 1) 't) (copy-marker (line-end-position)))))
1283 (goto-char beg)
1284 (beginning-of-line)
1285 (while
1286 (< (point) end)
1287 (if (looking-at "Subject:\\|From:\\|Date:\\|To:")
1288 (forward-line 1)
1289 (delete-region (line-beginning-position) (1+ (line-end-position)))))))
1291 * testing configuration
1293 From: pjb@informatimago.com (Pascal J. Bourguignon)
1294 Date: Sun, 27 Dec 2009 18:53:08 +0100
1295 To: help-gnu-emacs@gnu.org
1296 Subject: Re: testing configuration
1298 andrea <andrea.crotti.0@gmail.com> writes:
1300 > I was wondering if it would be possible to automatically check if my
1301 > emacs configuration is correct.
1302 > This can be then put in a post-commit hook to check that whenever I add
1303 > some new features or change I don't mess up somethin else
1305 > I would only need to:
1306 > - load my whole configuration
1307 > - exit and returns 0 if everything goes fine, returns another number
1308 > otherwise.
1310 > Any ideas?
1312 > I can already launch a new emacs with certain conf, but how can I get a
1313 > return value?
1315 What about:
1317 emacs -q --eval '(condition-case err (progn (load "~/.emacs") (kill-emacs 0)) (error (kill-emacs 1)))'
1322 __Pascal Bourguignon__ http://www.informatimago.com/
1324 * self-typing
1326 From: pjb@informatimago.com (Pascal J. Bourguignon)
1327 Date: Sat, 26 Dec 2009 14:32:16 +0100
1328 To: help-gnu-emacs@gnu.org
1329 Subject: Re: what is `self-typing' ?
1331 waterloo <waterloo2005@gmail.com> writes:
1333 > I can not understand a para in Elisp manual :
1335 > Lisp is unlike many other languages in that its objects are
1336 > "self-typing": the primitive type of each object is implicit in the
1337 > object itself.  For example, if an object is a vector, nothing can
1338 > treat it as a number; Lisp knows it is a vector, not a number.
1340 > What is `self-typing' ? thanks
1342 As mentionned by Eli. But this require some more explaination.
1344 Some languages are defined so that variables can hold only objects of
1345 a given type (you declare the type of the variable, or it is infered
1346 automatically at compilation time). For example, C or Haskell.
1348 Since all the types of the objects are known at compilation time from
1349 the variables they're stored in, the compiler doesn't need to generate
1350 code to store the type along with the object, or along with the
1351 variable: the type is implicity. In C, typeof(42) or int a=42; typeof(a)
1352 is computed at compilation time.
1354 Some languages are defined so that variables can hold objects of
1355 different types, at different times. In that case, the type is not
1356 attached to the variable, but must be attached to the objects
1357 themselves. For example, Lisp or Smalltalk.
1359 Since it is rarely possible to know what type of object a variable
1360 will hold (some type inference can still be applied to optimize out
1361 some function, but there remains a lot of functions where type
1362 inference doesn't restrict much the possible types, the more so when
1363 global analysis is not practical or possible), then the type of each
1364 object has to be kept with the object. In Lisp, (type-of 42) or (let
1365 ((a 42)) (type-of 42)) is computed at run-time. Notably, you can
1366 write: (type-of (read)) and depending on what you enter at run-time,
1367 will get back one type or another:
1369 M-x ielm RET
1370 *** Welcome to IELM *** Type (describe-mode) for help.
1371 ELISP> (type-of (read))
1372 Lisp expression: 42
1373 integer
1374 ELISP> (type-of (read))
1375 Lisp expression: "fourty-two"
1376 string
1377 ELISP> (type-of (read))
1378 Lisp expression: fourty-two
1379 symbol
1380 ELISP> (type-of (read))
1381 Lisp expression: (fourty two)
1382 cons
1383 ELISP>
1385 There are also languages where both things are more or less possible,
1386 you can have variables with pre-defined types holding only one type of
1387 object, and variables which can hold objects of various types, but of
1388 a same root class. For example, C++ or Java.
1390 These languages usually provide so called "Plain Old Data" types which
1391 correspond to the type of objects that can only be stored in variables
1392 with predefined types. These objects of these POD types usually
1393 don't have the type attached to them, these objects can only be stored
1394 in variables with pre-defined type. When you want to store such an
1395 object in a variable-type variable, you have to wrap it in another
1396 object, a typed object, instance of a class. In Java, you have a POD
1397 type int and an object class Integer.
1400 __Pascal Bourguignon__ http://www.informatimago.com/
1402 * Emacs uptime
1404 From: pjb@informatimago.com (Pascal J. Bourguignon)
1405 Date: Wed, 23 Dec 2009 20:16:03 +0100
1406 To: help-gnu-emacs@gnu.org
1407 Subject: Re: Some functions I hope are useful for others to
1409 [ ... ]
1411 For example, here is my emacs-uptime:
1413 (defvar com.informatimago.time/*emacs-start-time* (current-time)
1414 "For (emacs-uptime)")
1416 (defun com.informatimago.time/emacs-uptime ()
1417 "Gives Emacs' uptime, based on global var `com.informatimago.time/*emacs-start-time*'."
1418 (interactive)
1419 (let* ((st com.informatimago.time/*emacs-start-time*)
1420 (cur (current-time))
1421 (hi-diff (- (car cur) (car st)))
1422 (tot-sec (+ (ash hi-diff 16) (- (cadr cur) (cadr st))))
1423 (days (/ tot-sec (* 60 60 24)))
1424 (hrs (/ (- tot-sec (* days 60 60 24)) (* 60 60)))
1425 (mins (/ (- tot-sec (* days 60 60 24) (* hrs 60 60)) 60))
1426 (secs (/ (- tot-sec (* days 60 60 24) (* hrs 60 60) (* mins 60)) 1)))
1427 (message "Up %dd %dh %dm %ds (%s), %d buffers, %d files"
1428 days hrs mins secs
1429 (format-time-string "%a %Y-%m-%d %T" st)
1430 (length (buffer-list))
1431 (count t (buffer-list)
1432 :test-not
1433 (lambda (ignore buf)
1434 (null (cdr (assoc 'buffer-file-truename
1435 (buffer-local-variables buf)))))))))
1437 (defalias 'emacs-uptime 'com.informatimago.time/emacs-uptime)
1439 [ .... ]
1441 * Using Emacs Lisp for script writing
1443 To: help-gnu-emacs@gnu.org
1444 From: David Engster <deng@randomsample.de>
1445 Date: Sat, 19 Dec 2009 11:02:22 +0100
1446 Subject: Re: Using Emacs Lisp for script writing
1448 Andreas Politz <politza@fh-trier.de> writes:
1449 > Cecil Westerhof <Cecil@decebal.nl> writes:
1451 >> I already use 'emacs -batch' for scripting where no user input is used,
1452 >> but I would like to use it also for interactive scripting. Until now I
1453 >> did not find any usable information about this. Anybody using Emacs for
1454 >> interactive scripts?
1456 > No, but it should be noted, that this is not very difficult :
1458 > $ emacs -Q -batch -eval '(yes-or-no-p "Want some cookies ?")'
1460 You can also use the '--script' option in a shebang line:
1462 ----------------- test.sh -----------------
1463 #!/usr/bin/emacs --script
1465 (if (yes-or-no-p "Choose ")
1466 (message "You said yes")
1467 (message "You said no"))
1468 -------------------------------------------
1470 I use emacs regularly for writing scripts, since with its thousands of
1471 packages you have a huge library readily available.
1473 -David
1475 * until found
1477 From: Helmut Eller
1478 Subject: Re: until-found
1479 Date: Fri, 11 Dec 2009 17:10:38 +0100
1480 User-agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1.50 (gnu/linux)
1482 * Andreas Roehler [2009-12-10 13:50+0100] writes:
1484 > And here my implementation so far:
1486 > (defun until-found (search-string liste)
1487 > (let ((liste liste) element)
1488 > (while liste
1489 > (if (member search-string (car liste))
1490 > (setq element (car liste) liste nil))
1491 > (setq liste (cdr liste)))
1492 > element))
1494 This seems to be the same as:
1496 (car (member (lambda (element) (member search-string element)) liste))
1500 (find-if (lambda (element) (member search-string element)) liste)
1504 (find search-string liste :test #'member)
1508 (loop for e in liste if (member search-string e) return e)
1510 Helmut
1512 * favourite directories implementation
1514 Author: Florent Georges, source:
1515 http://fgeorges.blogspot.com/2008/01/emacs-favourite-directories.html
1517 Today, I have finally taken a look at one of the
1518 simple features I always missed in Emacs: the
1519 ability to define a set of "favourite directories."
1520 That is, a set of named directories that one can use
1521 in the minibuffer when prompted for instance to open
1522 a file. Given a set of such dirs:
1524 emacs-src -> /enter/your/path/to/emacs/sources
1525 projects -> /path/to/some/company/projects
1526 now -> @projects/the/project/I/am/working/on
1528 one can use the following path in the minibuffer to open a file,
1529 for instance using C-x C-f:
1531 @emacs-src/lisp/files.el
1532 @emacs-src/src/alloc.c
1533 @projects/great/README
1534 @now/src/some/stuff.txt
1536 Doing so, completion is available for both directory names and
1537 files under their target directories. For instance, to open the
1538 third file above, you only have to type:
1540 C-x C-f @ p <tab> g <tab> R <tab> <enter>
1542 The implementation I have just written is really simple, but
1543 useful yet. It implements all described above (including
1544 recursive defined directories, as the '@now' above.) Thanks to
1545 Emacs, I am still suprised by the facility to implement such a
1546 feature!
1548 The code was written on GNU Emacs 22.1 on Windows, but should
1549 work on any platform, and I think on Emacs 21 as well.
1551 TODO: Make a custom variable.
1552 (defvar drkm-fav:favourite-directories-alist
1553 '(("saxon-src" . "y:/Saxon/saxon-resources9-0-0-1/source/net/sf/saxon")
1554 ("kernow-src" . "~/xslt/kernow/svn-2007-09-29/kernow/trunk/src/net/sf/kernow"))
1555 "See `drkm-fav:handler'.")
1557 (defvar drkm-fav::fav-dirs-re
1558 ;; TODO: Is tehre really no other way (than mapcar) to get the list
1559 ;; of the keys of an alist?!?
1560 (concat
1561 "^@"
1562 (regexp-opt
1563 (mapcar 'car drkm-fav:favourite-directories-alist)
1565 "Internal variable that stores a regex computed from
1566 `drkm-fav:favourite-directories-alist'. WARNING: This is not
1567 updated automatically if the later variable is changed.")
1569 (defun drkm-fav:handler (primitive &rest args)
1570 "Magic handler for favourite directories.
1572 With this handler installed into `file-name-handler-alist', it is
1573 possible to use shortcuts for often used directories. It uses
1574 the mapping in the alist `drkm-fav:favourite-directories-alist'.
1576 Once installed, say you have the following alist in the mapping
1577 variable:
1579 ((\"dir-1\" . \"~/some/real/dir\")
1580 (\"dir-2\" . \"c:/other/dir/for/windows/users\"))
1582 You can now use \"@dir-1\" while opening a file with C-x C-f for
1583 instance, with completion for the abbreviation names themselves
1584 as well as for files under the target directory."
1585 (cond
1586 ;; expand-file-name
1587 ((and (eq primitive 'expand-file-name)
1588 (string-match drkm-fav::fav-dirs-re (car args)))
1589 (replace-match
1590 (cdr (assoc (match-string 1 (car args))
1591 drkm-fav:favourite-directories-alist))
1592 t t (car args)))
1593 ;; file-name-completion
1594 ((and (eq primitive 'file-name-completion)
1595 (string-match "^@\\([^/]*\\)$" (car args)))
1596 (let ((compl (try-completion
1597 (match-string 1 (car args))
1598 drkm-fav:favourite-directories-alist)))
1599 (cond ((eq t compl)
1600 (concat "@" (match-string 1 (car args)) "/"))
1601 ((not compl)
1602 nil)
1604 (concat "@" compl)))))
1605 ;; file-name-all-completions
1606 ((and (eq primitive 'file-name-all-completions)
1607 (string-match "^@\\([^/]*\\)$" (car args)))
1608 (all-completions
1609 (match-string 1 (car args))
1610 drkm-fav:favourite-directories-alist))
1611 ;; Handle any primitive we don't know about (from the info node
1612 ;; (info "(elisp)Magic File Names")).
1613 (t (let ((inhibit-file-name-handlers
1614 (cons 'drkm-fav:handler
1615 (and (eq inhibit-file-name-operation primitive)
1616 inhibit-file-name-handlers)))
1617 (inhibit-file-name-operation primitive))
1618 (apply primitive args)))))
1620 ;; Actually plug the feature into Emacs.
1621 (push '("\\`@" . drkm-fav:handler) file-name-handler-alist)
1623 * lisp interface to ispell
1625 From: Teemu Likonen <tlikonen@iki.fi>
1626 Date: Fri, 06 Nov 2009 22:05:53 +0200
1627 To: help-gnu-emacs@gnu.org
1628 Subject: Re: lisp interface to ispell ?
1630 On 2009-11-06 20:39 (+0100), Andreas Politz wrote:
1632 > Does someone have a hack, or know a different package, in order to allow
1633 > elisp access to spelling functions ? E.g. like
1635 > (spell word language)
1637 > which at least returns t or nil.
1639 Something like this?
1641 (defun my-ispell-string (word lang)
1642 (with-temp-buffer
1643 (insert word)
1644 (call-process-region (point-min) (point-max)
1645 "ispell" t t nil "-l" "-d" lang)
1646 (if (= (point-min) (point-max))
1647 t)))
1649 * Python workflow
1651 From: Simon <bbbscarter@gmail.com>
1652 Date: Fri, 6 Nov 2009 03:42:44 -0800 (PST)
1653 To: help-gnu-emacs@gnu.org
1654 Subject: Python workflow
1656 Hi, apologies in advance for a potentially numpty post.
1658 I've been using Emacs for a little while now, but I've yet to settle
1659 on a satisfactory python edit-run-debug cycle, and I was wondering
1660 what wiser minds than mine have settled upon. So far I've tried:
1662 - Edit code and run with emacs PDB. After fiddling the lisp code to
1663 automatically pick up the current buffer as the default run candidate,
1664 this is nearly okay. The main issue is that, after editing code,
1665 there's no easy way to rerun the code, so I end up killing the gud
1666 buffer every time. As such, entering and leaving the debugger is quite
1667 a few key presses and accidentally leaving it running is also easy.
1669 - Tried Pydb to similar effect.
1671 - Run everything in a seperate shell. And debug by hand. This is a
1672 little too low-fi, even for me.
1674 - Use the 'import/reload file' and 'eval def/class' functions, and run
1675 everything from the emacs python shell, using pdbtrack to help with
1676 debugging. Problems so far:
1677 - It's very easy to forget which modules you've modified and fail to
1678 reload them; because the state is carried over I continually find
1679 myself running the old versions of code I've just edited, especially
1680 if it's across several files.
1681 - More than that, sometimes the stuff I expect to reload simply
1682 doesn't, and I have no indication as to why. For example, if pdb has
1683 module open and you stick a deliberate error in the code and reload
1684 it, the minibuffer tells you the module has been loaded, even though
1685 it clearly can't have been.
1686 - I have to run pdb.pm() to debug the exception. If I run *anything*
1687 else by accident, I lose the exception context. This can be annoying
1688 if you're incompetent enough to keep making typos (I am).
1690 Does anyone have any tips on their workflow?
1692 Many thanks!
1694 Simon
1696 * strip out UTF-8 BOMs
1697 From: "Edward O'Connor" <hober0@gmail.com>
1698 Date: Thu, 5 Nov 2009 16:13:27 -0800
1699 To: emacs-devel@gnu.org
1700 Subject: find-file-literally-at-point
1704 I recently found myself in need of such a function (I was going through
1705 a bunch of files to strip out their UTF-8 BOMs, if you're curious), and
1706 it was quick enough to put together:
1708 (autoload 'ffap-guesser "ffap")
1709 (defun find-file-literally-at-point ()
1710 "Open the file at point (like `ffap') with `find-file-literally'."
1711 (interactive)
1712 (find-file-literally (ffap-guesser)))
1714 * xml and n3
1716 From: "Eric Schulte" <schulte.eric@gmail.com>
1717 Subject: Re: [Orgmode] org-babel-tangle xml text
1718 Date: Tue, 03 Nov 2009 09:18:34 -0700
1720 "Martin G. Skjæveland" <martige@ifi.uio.no> writes:
1722 > Is there a way I can add xml and n3 to the list of supported
1723 > languages? These languages does not need interpretation, so I'm
1724 > thinking it should be quite easy to add. I have fumblingly tried
1726 > (add-to-list 'org-babel-tangle-langs '("xml"))
1728 > and
1730 > (add-to-list 'org-babel-tangle-langs '("css" "xml"))
1732 > but it as no effect.
1735 Hi Martin,
1737 The attached org-mode file contains instructions for adding xml and n3
1738 to org-babel and org-babel-tangle. Note that there may be another step
1739 if the major mode for n3 is not n3-mode. Best -- Eric
1741 introduce org-babel to =xml= and =n3=
1743 #+begin_src emacs-lisp :results silent
1744 (org-babel-add-interpreter "xml")
1745 (org-babel-add-interpreter "n3")
1746 #+end_src
1748 if say =n3= should be edited using =xml-mode=, then evaluate the
1749 following adding this pair to =org-src-lang-modes=
1751 #+begin_src emacs-lisp :results silent
1752 (add-to-list 'org-src-lang-modes '("n3" . xml))
1753 #+end_src
1755 ;; inform org-babel-tangle of their existence and file extensions
1756 #+begin_src emacs-lisp :results silent
1757 (add-to-list 'org-babel-tangle-langs '("xml" "xml" nil t))
1758 (add-to-list 'org-babel-tangle-langs '("n3" "n3" nil t))
1759 #+end_src
1761 #+begin_src xml :tangle example
1762 <first>
1763 </first>
1764 #+end_src
1766 #+begin_src n3 :tangle example
1767 n3 stuff
1768 #+end_src
1770 * How to check regexp for syntax-errors?
1772 From: Kevin Rodgers <kevin.d.rodgers@gmail.com>
1773 Date: Tue, 20 Oct 2009 01:54:56 -0600
1774 Subject: Re: How to check regexp for syntax-errors?
1775 David Combs wrote:
1776 > Isn't there some .el that that will try to parse a regexp, and tell
1777 > me where it got confused, and what to look for for errors?
1779 (defun valid-regexp-p (regexp)
1780 (interactive "sRegexp: ")
1781 (with-temp-buffer
1782 (condition-case error-data
1783 (progn
1784 (re-search-forward regexp nil t)
1786 (invalid-regexp
1787 (when (interactive-p) (message "Invalid regexp: %s" (cdr error-data)))
1788 nil))))
1791 Kevin Rodgers
1792 Denver, Colorado, USA
1794 * prefer cond over case?
1796 From: David Kastrup <dak@gnu.org>
1797 Date: Mon, 12 Oct 2009 10:03:39 +0200
1798 To: help-gnu-emacs@gnu.org
1799 Subject: Re: Perferr cond over case?
1801 pjb@informatimago.com (Pascal J. Bourguignon) writes:
1803 > Nordlöw <per.nordlow@gmail.com> writes:
1805 >> Does the use of the cl macro case() incurr some loss of performance
1806 >> compare to using cond() instead?
1808 > (macroexpand '(case (* 2 2 2 2 3)
1809 > (10 'one)
1810 > ((24 42) 'two)
1811 > ((3 33) 'three)
1812 > (otherwise 'unknown)))
1813 > -->
1814 > (let ((--cl-var-- (* 2 2 2 2 3)))
1815 > (cond ((eql --cl-var-- (quote 10)) (quote one))
1816 > ((member* --cl-var-- (quote (24 42))) (quote two))
1817 > ((member* --cl-var-- (quote (3 33))) (quote three))
1818 > (t (quote unknown))))
1820 > What do you think?
1822 Before or after byte compilation?
1825 David Kastrup
1827 * batch-mode
1829 From: Decebal <cldwesterhof@gmail.com>
1830 Newsgroups: gnu.emacs.help
1831 Date: Sat, 10 Oct 2009 11:33:17 -0700 (PDT)
1832 To: help-gnu-emacs@gnu.org
1833 Envelope-To: andreas.roehler@easy-emacs.de
1835 In a Bash script I changed:
1837 local i
1838 local length=${#1}
1840 for i in $(seq ${1}) ; do
1841 printf " Regel %${length}d voor de test\n" ${i}
1842 done >${2}
1845 emacs -batch -nw --eval='
1846 (let (
1848 (nr-of-lines '${1}')
1849 (nr-of-lines-length)
1850 (output-file "'"${2}"'"))
1851 (setq nr-of-lines-length (length (number-to-string nr-of-lines)))
1852 (dotimes (i nr-of-lines t)
1853 (insert (format (format " Regel %%%dd voor de test\n" nr-of-lines-length) (1+ i))))
1854 (write-file output-file))
1855 ' 2>/dev/null
1857 The Bash version took 293 seconds and the Emacs Lisp version 59
1858 seconds. So it is about 5 times as fast.
1859 The Emacs batch gives output like:
1860 Saving file /home/cecil/temp/inputEmacs...
1861 Wrote /home/cecil/temp/inputEmacs
1862 That is why I use the 2>/dev/null.
1863 Is there a way to circumvent the generation of the above output?
1864 Because when there is an error it is also thrown away and that is not
1865 nice.
1867 From: Vassil Nikolov <vnikolov@pobox.com>
1868 Newsgroups: gnu.emacs.help
1869 Date: Sat, 10 Oct 2009 16:55:31 -0400
1870 To: help-gnu-emacs@gnu.org
1871 Envelope-To: andreas.roehler@easy-emacs.de
1873 On Sat, 10 Oct 2009 11:33:17 -0700 (PDT), Decebal <cldwesterhof@gmail.com> said:
1874 > ...
1875 > local i
1876 > local length=${#1}
1877 > for i in $(seq ${1}) ; do
1878 > printf " Regel %${length}d voor de test\n" ${i}
1879 > done >${2}
1881 translates to
1883 emacs -Q -batch -eval '
1884 (dotimes (i '"${1}"')
1885 (princ (format " Regel %'"${#1}"'d voor de test\n" (1+ i))))
1886 ' > "${2}"
1888 ---Vassil.
1890 From: Decebal <cldwesterhof@gmail.com>
1891 Newsgroups: gnu.emacs.help
1892 Date: Sat, 10 Oct 2009 13:57:07 -0700 (PDT)
1893 To: help-gnu-emacs@gnu.org
1894 Envelope-To: andreas.roehler@easy-emacs.de
1896 On Oct 10, 10:06pm, Andreas R=F6hler <andreas.roeh...@easy-emacs.de>
1897 wrote:
1898 > > The Emacs batch gives output like:
1899 > > Saving file /home/cecil/temp/inputEmacs...
1901 > it's in files.el, save-buffer, AFAIS
1903 > (if (and modp (buffer-file-name))
1904 > (message "Saving file %s..." (buffer-file-name)))
1906 > commenting out these lines should cancel the message
1908 The problem with that is that it only works for me. But I found a way.
1909 I replaced:
1911 (write-file output-file))
1913 with:
1915 (set-visited-file-name output-file)
1916 (basic-save-buffer))
1918 But maybe there should be more consideration for the possibility that
1919 Emacs is used as a batch program.
1921 > > Wrote /home/cecil/temp/inputEmacs
1923 I still have to find something for this.
1925 That is not possible I am afraid. In the C-source there is a call to
1926 message_with_string.
1928 * vectors and lists
1930 From: pjb@informatimago.com (Pascal J. Bourguignon)
1931 Newsgroups: gnu.emacs.help
1932 Date: Fri, 09 Oct 2009 19:19:49 +0200
1933 To: help-gnu-emacs@gnu.org
1934 Envelope-To: andreas.roehler@easy-emacs.de
1936 Nordlöw <per.nordlow@gmail.com> writes:
1938 > If I have an association list say,
1940 > '(
1941 > ("key" sym1 val1 num1)
1942 > ("key2" sym2 val2 num2)
1945 > , where each entry is a fixed sequence of various objects.
1947 If this is an a-list, then you could write it as:
1949 (("key1" . (sym1 val1 num1))
1950 ("key2" . (sym2 val2 numb2)))
1952 to show that it is a list of cons cells.
1954 (a . (b c d)) <=> (a b c d), but the first notation shows that you
1955 consider it as a list of cons, and notably that you don't expect nil
1956 ie. () to be in the toplevel of the a-list.
1958 Also, if we write the a-list properly like this, we can better answer
1959 the following question:
1961 > I might
1962 > aswell use a vector to represent an entry in this alist, right?
1964 You cannot use a vector instead of the cons cells of the a-list, but
1965 you can use a vector as a value of an a-list entry. Values can be of
1966 any type. In the case of emacs lisp, you could also easily use
1967 vectors (or any other type) as keys in an a-list, since it uses equal
1968 to compare keys.
1970 (("key1" . [sym1 val1 num1])
1971 ("key2" . [sym2 val2 num2])
1972 ([?k ?e ?y ?3] . [sym3 val3 num3]))
1974 > In this case, what do I gain by using a vector instead of list?
1976 In general, vectors take half the space of lists, and access to the
1977 nth element is done in O(1) instead of O(n) with lists. However,
1978 adding or removing an element in a vector is O(n) while in the case of
1979 lists, it may be O(1) (prepending an element or removing the first
1980 element or one of the few firsts elements) or O(n) (inserting,
1981 appending an element or removing the nth element).
1983 > What about performance?: aref() faster than nth() only for large
1984 > vectors?
1986 aref has to compute a multiplication and a sum, before doing one
1987 memory load to get the element. In the case of emacs lisp, the
1988 multiplication is always by the same fixed factor AFAIK.
1990 nth has to do n memory loads to get the element.
1992 So indeed, aref will probably be faster than nth, even for indices as
1993 small as 1 or 0.
1995 > Is there vector-variant of assoc()?
1997 No. Unless you count hash-tables as a vector variant.
1999 > If not, why?
2001 Because there's no point. The advantage of using a list for a-list,
2002 apart from the historical simplicity, is that you can easily prepend
2003 the a-list with new associations, and therefore use the a-list in a
2004 purely functional way.
2006 (defun f (bindings)
2007 (let ((val (cdr (assoc 'x bindings))))
2008 (if (zerop val)
2009 (list val)
2010 (cons val (f (cons (cons 'x (1- val)) bindings))))))
2012 (let ((bindings '((y . 0) (x . 1))))
2013 (list (f (cons (cons 'x 2) bindings))
2014 (cdr (assoc 'x bindings))))
2015 ;; --> ((2 1 0) 1)
2017 Note: you could use (require 'cl) (acons key value a-list)
2018 instead of (cons (cons key value) a-list).
2020 > Has any one already written such a function?
2022 Not AFAIK, but you can write it. However, the semantics of assoc
2023 require a sequential search of the keys in the list, so there would be
2024 no gain. On the contrary, we would now have O(n) complexity to
2025 prepend a new entry to the a-vector.
2028 __Pascal Bourguignon__