2009-03-18

emacs, how to attach text property

how to write a function that colors particular words matching regex inside a region?

I was thinking using font-lock-add-keywords, but that does to the whole buffer.

i haven't studied emacs's face, display property etc systems. But could anyone give me a rough guide on what function i should use or lookup?

Here's a bit detail on what i need to do. I'm writing a mode for Linden scripting language (LSL). Colors are specified using lsl's vector type, like this:

vector red = <1,0,0>;
vector green = <0,1,0>;
vector blue = <0,0,1>;
vector black = <0,0,0>;
vector white = <1,1,1>;
vector pink = <1,0.07843,0.5765>;
vector orange = <1,0.6471,0>;
vector brown = <0.5451,0.1373,0.1373>;
vector purple = <0.3333,0.102,0.5451>;
vector gold = <1,0.8431,0>;

I want to be able write a function, something named color-vectors-region, so that any 's background is colored using the rgb value. (each is a float from 0 to 1.) The part i don't know how, is how to attach a text color property to a piece of text.

Thanks.

Xah
∑ http://xahlee.org/



Here's the code i did.

(defun convert-rgb-vector-to-hex (rgb)
"Convert color RGB from “[r g b]” notation to “\"rrggbb\"” notation.
Example:
(convert-rgb-vector-to-hex [0 1 0.5])
returns
\"00ff80\"
"
(mapconcat
(lambda (x)
(format "%02x" (round (* x 255.0)) ))
rgb ""))

(defun syntax-color-vector (start end)
"Make vectors syntax colored "
(interactive "r")
(save-restriction
(narrow-to-region start end)
(goto-char (point-min))
(while (search-forward-regexp
"< *\\([0-9.]+\\) *, *\\([0-9.]+\\) *, *\\([0-9.]+\\) *>" nil t)
(put-text-property (match-beginning 0)
(match-end 0) 'face (list :background

(concat "#" (convert-rgb-vector-to-hex
(vector
(string-to-number (match-string 1))
(string-to-number (match-string 2))
(string-to-number (match-string 3))
)))
)))))

spent some 4 hours on this. Some 80% time is spend on the hex/decimal conversion, digging into some details of emacs float, string, hex, round, etc issues. (^_^)

Xah
∑ http://xahlee.org/

emacs modernization: margins and softwrap

2009-03-18

Try Meta+x longlines-mode.

That gives you soft wrapping. Note: as of now it's kinda buggy. My experiences is that sometimes if you paste something long, it screws up, or if you call some command that transform the selected region.

I think no softwrap or visual margin is a major usability problem of emacs. I think aquamacs emacs did a great emacs modenization job,

somehow it missed this one.


Xah wrote:
>> somehow it missed this one. [soft wrap or display margin]
>
>On Mar 18, 2009 4:20pm, David Reitter wrote:
>> Somehow, it didn't miss it.
>
>LOL. Good. (^_^)

wait, i replied too fast.

Aquamacs emacs actually does not support display margin, which i think is the original poster wanted.

In aquamacs emacs, if you have a long line say 3000 chars, and you turn on “Soft Word Wrap” under “Options”. Then, drag your window to wide, you'll see that the line keeps going from left window border to right window border. That is, there is no right margin.

longlines-mode, effectively introduces right margin. Buggy and the way its done is a hack. Possibly emacs will need to introduce the concept of margins in its display system.

I just looked at XCode. Apparently it doesn't have margin neither. I looked at Eclisp, apparently no. Nor TextWrangler/BBEdit. I wonder if TextMate support it, or MS Visual Studio. (note: all word processors support margins. TextEdit also does if you are in rich text mode.)

Xah
∑ http://xahlee.org/

2009-03-11

lisp sort destructive

2008-01-05

I'm a perl expert, programing it in industry daily from 1998 to 2002. I started to learn elisp in 2005 casually. (am a daily emacs user since 1997) To my surprise, for tasks of text processing (and sys admin), elisp's power, ease, and convenience is almost a order above Perl. This is particular painful to realize for me because even being a Perl hater from the very beginning, fully aware of its rampant lies, i was nevertheless strongly deceived by the wide-spread understanding that Perl IS the most suitable language for text processing tasks. (and conversely, due to emacs lisper's lack of emacs lisp advocacy, partly due to the supression by Scheme lisp and Common Lisp factions, emacs's power as a text-processing computer language system is basically unknown to the programers at large (often, emacs lisp is understood to programers who actually heard of lisp, as “just a extension niche language” usually with demeaning connotations))

Besides industrial programing, my personal use of perl is primarily text processing. So, i was quite pissed by the extent of damage perl's lies caused to me on this regard. I'm currently, casually doing all my text processing needs in emacs instead of Perl or Python. (e.g. typically a function that process tens to tens of thousand files.)

Anyway, the main point for this post is something i realized in emacs lisp that's rather bizarre. That is: its “sort” function destroys the variable of list it is sorting!!!

Here's a example:

(setq mylist '("c" "d" "a"))
(sort mylist 'string< )
mylist ; ⇒ ("c" "d")

O my god???

This actually caused me few hours to debug. (among other reasons. e.g. i'm still learning elisp system)

In high level languages, sort will either sort the variable in-place, or return the sorted list. I have not come across any language that actually destroy the variable in the process. I asked a bit, and it seems Common Lisp's sort has the same behavior.

So, my question is: What could possibly be the use of this? When does a programer EVER need to do simply just (sort mylist 'f) ?

Of course, lisp is old, with many quaint qualities (e.g. the cons business; elisp's char being integers; elisp's dynamic scope; Common Lisp's big baggage ...etc.), but what could have possibly made the design of “sort” function the way it is?

Xah
∑ http://xahlee.org/



http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/606f3c8f0ab8cc53

2009-03-10

emacs lisp; pairs

2009-03-10

On Mar 9, 7:14 pm, Richard Riley wrote:
> Could someone please recommend the best way to remove the 3 similar lines
> doing string-match on the "account" assign and iterate a variable list to
> which I can "add-to-list" in other .el libraries for example?
>
> ,----
> | (if (message-mail-p)
> | (save-excursion
> | (let* ((from
> | (save-restriction
> | (message-narrow-to-headers)
> | (message-fetch-field "from")))
> | (account
> | (cond
> | ((string-match ".*root.*" from)"richardriley")
> | ((string-match ".*richardriley.*" from)"richardriley")
> | ((string-match ".*rileyrgdev.*" from)"rileyrgdev")
> | ))
> | )
> | (setq message-sendmail-extra-arguments (list "-a" account))
> | )))
> | )
> `----

perhaps something like the following. The code is tested.

(defun canonicalString (from pairs)
"Returns the canonical string of FROM, determined by the pairs in PAIRS.

The PAIRS should be a nested vector of the form:
“[[\"a\" \"α\"] [\"b\" \"β\"] [\"γ\" \"g\"] ...]”
where the first element is a regex string to be matched with FROM.
If match, then the second element is returned.

If no match is found, nil is returned.

Example:
(canonicalString \"b\" [[\"a\" \"α\"] [\"b\" \"β\"] [\"γ\" \"g\"]]) returns \"β\".
"
(let (totalItems matchFound i result)
(setq totalItems (length pairs))
(setq foundMatch nil)
(setq i 0)
(while (and (not matchFound)
(not (= i totalItems)))
(if (string-match (elt (elt pairs i) 0) from)
(progn
(setq result (elt (elt pairs i) 1))
(setq matchFound t)))
(setq i (1+ i)))
result))

; testing
(canonicalString "b" [["a" "α"] ["b" "β"] ["γ" "g"]])

Xah
∑ http://xahlee.org/

2009-03-09

xah lee, netiquette, tech geeking idiots

2009-03-09

Christian wrote:

On Mar 9, 1:22 pm, Christian wrote:
> XahLeeschrieb:> Of interest:
>
> > • Why Can't You Be Normal?
> > http://xahlee.org/Netiquette_dir/why_cant_you_be_normal.html
>
> IMHO the point that you never reply to responds is what makes it
> problematic.
> I have seen 10 or more threads started by you and in not a single one
> of those I have seen any sort of second post by you.
>
> Also the other thing that makes you appear like a troll is that the
> only posts where you are visible on the usenet are your own!
>
> Usenet is there for discussion. What you do seems to be mostly doing a
> often highly intelligent monologue and awaiting comment on it.
>
> Its not the purpose of Usenet. Simply calling you a troll is wrong.
> You are after all better than that. Though I think you are misusing the
> Usenet. For what you do you should rather write a weblog so people
> interested in your monologues could follow them in a place where they
> are by definition on topic.
>
> Christian

In the article you quoted:
http://xahlee.org/Netiquette_dir/why_cant_you_be_normal.html

contains this passage:

«
Some people says that i don't participate in discussion, and this is part of the reason they think i'm a so-called “troll”. Actually i do, and read every reply to my post, as well have replied to technical questions other posted. Most replies to my posts are attacks or trivial (of few sentences) i don't consider worthy to reply.

A few, maybe 10% replies to my unconventional posts, i consider having some value. But if i don't have sufficiently remarkable opinion on what they remarked, i don't reply. Also, if all i wanted to say is “thanks”, i tend to avoid posting such trivial posts too. (i used to reply by personal email in such cases, I still do sometimes now, but today that can be considered intrusive.)
»

if you didn't start your message with “IMHO”, which indicated to me that at least you are sincere, i would not have replied. (no offense intended) Btw, i'm not some kind of saint. You (guys) do whatever chatty style you want, i write or choose to reply in my abstruse & ascetic manners. Just don't accuse when my style is not compatible with your drivels. (insult intentional)

I have written quite a lot on netiquette issues in the past decade. You can find many answers about my reasons or posting behavior here:

• Netiquette Anthropology
http://xahlee.org/Netiquette_dir/troll.html

Recently i started a blog that is collection of my online posts. If you need to talk about me, feel free to comment there. I am more likely to reply there for questions pertaining just me. The url is here: http://xah-forum.blogspot.com/

If anyone likes me to give answers particular to this thread, or desire me to reply to all the messages directed to me in this thread, i'll be more than happy to do so.

Also, thanks to many supporters over the past years.

Truly Your Superior,

Xah
∑ http://xahlee.org/

2009-03-05

what's your favorite lisp

2009-03-05

Continued from
http://xahlee.org/UnixResource_dir/writ/whats_your_fav_lisp.html


On Mar 5, 12:43 am, Javier wrote:
> Interesting.
> Do you actually consider yourself an Emacs expert?

Using emacs daily, pretty much whole day, since about late 1998. Learning & coding elisp on and off seriously, since 2006. Written many text processing scripts, a major mode, and written a book on emacs and elisp.

• Emacs xlsl-mode for Linden Scripting Language
http://xahlee.org/sl/ls-emacs.html

• Xah's Emacs Tutorial
http://xahlee.org/emacs/emacs.html

I am a emacs expert. I'm also a elisp expert in the context of programing industry. But when compared to dedicated emacs developers, i'm rather a beginner. For example, some corners of elisp i still don't understand quite well: its display mechanism, faces, overlay, windows and frames, key mapping and events complexities.

>> For example, i consider emacs lisp, more powerful than Perl, as a text
>> processing language,
>
>Oh, I think no people has ever done any comparative of these two
>languages. Are you preparing a more in deep comparative or
>something like that? I truly would like to read your expert
>opinion, ...

See:

• Text Processing: Elisp vs Perl
http://xahlee.org/emacs/elisp_text_processing_lang.html

> Do you think Emacs Lisp is probably the most functional Lisp of
> all the Lisps? What are your experiences with those languages?
> As I see your page, have you done any study on this phenomena?
> Do you have any ideas on how to achieve that elegance?

you have also lots of one-sentence questions. You will find some answers here:

http://xahlee.org/emacs/emacs_essays_index.html

which is a index page of collection of essays related to emacs. About 15 of them, are related to lisp in general. Each page usually has “Related Essays” links at the bottom that takes you to other essays on the topic, giving you more detail, or covering other aspects.

I recommend to start with the following, more quality ones:

• Fundamental Problems of Lisp
http://xahlee.org/UnixResource_dir/writ/lisp_problems.html

• The Concepts and Confusions of Prefix, Infix, Postfix and Fully Nested Notations
http://xahlee.org/UnixResource_dir/writ/notations.html

• What are OOP's Jargons and Complexities
http://xahlee.org/Periodic_dosage_dir/t2/oop.html

> Thanks for your nice opinion.

Thank you. I enjoy teaching.

Xah
∑ http://xahlee.org/

2009-03-03

functional level programing

2009-03-03

On Mar 3, 5:26 am, p...@informatimago.com (Pascal J. Bourguignon) wrote:
> gavino writes:
> > Hello I was wondering if lispers even implemented the Ideas john
> > backus had about function level progarmming...
>
> There's already higher level functions in lisp. For example (see the
> paper of which the url is given below), insert is reduce, apply-to-all
> is mapcar & apply. So you can easily write your functions in
> functional style:

Boy, lisp IDIOTS are quick.

Lisp does not do functional level programing. Can't. Nor does almost all the well known functional langs such as Ocaml, Haskell, Mathematica.

See:
Function-level programming

I learned about it last year. See:
http://groups.google.com/group/comp.lang.lisp/browse_frm/thread/8793538dc0d909d1

I quote:

hard to summarize in one sentence... but basically like functional programing but with one characteristic formalism that sets it apart, namely: creation of functions are limited to a particular set of higher-order functions, and you cannot arbitrary birth functions (e.g. the moronicity of lisp's macros).

The force of this particular formalism is that it makes it more subject to mathematical analysis (and thus makes it more powerful and flexible), similar to for example to the clear separation of features in 2nd order logic from first order logic. Wikipedia said it best, quote:

«This restriction means that functions in FP are a module (generated by the built-in functions) over the algebra of functional forms, and are thus algebraically tractable. For instance, the general question of equality of two functions is equivalent to the halting problem, and is undecidable, but equality of two functions in FP is just equality in the algebra, and thus (Backus imagines) easier.»

«Even today, many users of lambda style languages often misinterpret Backus' function-level approach as a restrictive variant of the lambda style, which is a de facto value-level style. In fact, Backus would not have disagreed with the 'restrictive' accusation: he argued that it was precisely due to such restrictions that a well-formed mathematical space could arise, in a manner analogous to the way structured programming limits programming to a restricted version of all the control-flow possibilities available in plain, unrestricted unstructured programs.»

--------------------------------

This thread, illustrates, how often comp lang fanatics propagate patently false things, in their fanaticism. This, includes those lispers who are regulars at comp.lang.lisp. (other similar issues, is how lisp syntax is regular, and how lisp are adept at lisp manipulation)

Xah
∑ http://xahlee.org/



http://groups.google.com/group/comp.lang.lisp/msg/50837f07424e9ac4

emacs, PEG (parsing expression grammar), regex

2009-03-03

On Mar 3, 9:59 am, Mike Mattie wrote:
> On Tue, 03 Mar 2009 17:34:41 +0000
>
> Leo wrote:
> > On 2008-12-21 09:49 +0000, Helmut Eller wrote:
> > >> I'm pretty sure if you create it, more and more people will join
> > >> it. I'm very interested in PEG and think it is of critical
> > >> importance.
>
> > > I'll try to set up project at savannah.
>
> > I have seen the project on
> > http://savannah.nongnu.org/projects/emacs-peg. I wonder it might be a
> > good idea to make a newsgroup on gmane to link to the mailing list. It
> > will make more Emacs users subscribe to it.
>
> > Best wishes,
>
> I was working on a PEG/CFG parser compiler: http://www.emacswiki.org/cgi-bin/wiki/ParserCompiler
>
> I will be resuming the development soon. Please keep me in the loop on such efforts.

Folks,

when you create a PEG parser, please please make it user oriented one, so that any user of emacs familiar with regex find-replace will be able to use PEG for find-replace. In particular, when doing find-replace on nested text such as XML.

regex is powerful, but it doesn't do nested text. PEG comes to the rescue. However, it needs to be regex-like, in the sense that the program interface will be a simple source text and replacement text. e.g. a function peg-replace that takes 2 args, pattern text, and text source. The pattern text can be the region, buffer, or a filename, the text source to work on can be similar. (thus, maybe peg-replace-region, peg-replace-buffer, peg-replace-file etc.)

last time i was looking at PEG, i opted to try Helmut Eller's version because it seems simpler. (mike's version is far more compiler geeking incomprehensible) But still problematic to use. I got busy in other things so i didn't continue on studying it, so i dropped out of this thread (havn't read Helmut's last message in detail). Rather, i simply want just to use it. Last in this thread, he mentioned about stacks and i went huh... and just didn't have time to go further.

Regex is practically extremely useful, a tool every programer uses today. However, regex cannot work with nested text such as XML/HTML, which is used extensively, probably more so than any programing lang or text. So, brigining regex power to html/xml will be a major impact on not just emacs, but the whole programing industry. PEG, practically speaking, is basically just the next generation of so-called regex. Emacs can be the first to have such a feature. (existing PEG implementations in various lang, at this point, as far as i know, are all tech geeking toys, done by geekers interested in language parsing and so on.)

Personally, i have huge need for regex that can work on html.
PEG is of course not just a regex replacement, but a BNF replacement in the sense it is actually for machines to read. For these reasons that's how i got heavily interested in PEG. (see:
• Pattern Matching vs Lexical Grammar Specification
http://xahlee.org/cmaci/notation/pattern_matching_vs_pattern_spec.html
)

Please make your PEG in emacs with a regex-like API. Something any emacs user familiar with regex will be able to use brainlessly. This will be huge...

i had plans to open a mailing list and stuff... but got busy with other things. I'll come back to this. But i hope you are convienced about making PEG usable as a text-editing tool, as opposed to a tool for computer scientist or compiler/parser writers.

Also, Mike & Helmut, please consider putting your code in goode code. Google Code is very popular, probably today the most popular code building service, and extremely easy to use, and from my studies Google's products and services are all extremely high quality. It would help a lot in your software at least in the marketing aspect if you use Google Code. Also, open a google group is very useful and popular. (yasnippet is a successful example for a emacs project on google code. There are several others, including e.g. js2, ejacs, the erlang one, etc.) Going into Savana or anything on FSF services tend to be a dead end. (yeah, controversy, but whatever.)

Xah
∑ http://xahlee.org/



http://groups.google.com/group/gnu.emacs.help/msg/a30229e8d6e3d2d8