Posts

Showing posts from May 5, 2013

is XML Syntax Regular?

Even XML, whose syntax is more regular than lisp, cannot escape irregularities.Here's a sample valid XML, from ATOM webfeed.<?xmlversion="1.0"encoding="utf-8"?> <feedxmlns="http://www.w3.org/2005/Atom"xml:base="http://xahlee.info/comp/"> <title>…</title> <subtitle>…</subtitle> <linkrel="self"href="blog.xml"/> <linkrel="alternate"href="blog.html"/> <updated>…</updated> <author> <name>…</name> <uri>…</uri> </author> <id>…</id> <icon>…</icon> <rights>…</rights> <entry> <title>…</title> <id>…</id> <updated>…</updated> <summary>…</summary> <contenttype="xhtml"> <divxmlns="http://www.w3.org/1999/xhtml"> <p><ahref="……

emacs: convert Unicode chars to ASCII (Zap Gremlins)

Perm URL with updates: http://ergoemacs.org/emacs/emacs_zap_gremlins.htmlThis page shows a emacs lisp command that changes Unicode string into ASCII. For example “passé” becomes “passe”, “voilà” becomes “voila”.Emacs Lisp SolutionHere's a solution.(defun asciify-text (ξstring &optional ξfrom ξto) "Change some Unicode characters into equivalent ASCII ones. For example, “passé” becomes “passe”. This function works on chars in European languages, and does not transcode arbitrary Unicode chars (such as Greek, math symbols). Un-transformed unicode char remains in the string. When called interactively, work on text selection or current block. When called in lisp code, if ξfrom is nil, returns a changed string, else, change text in the region between positions ξfrom ξto." (interactive (if (region-active-p) (listnil (region-beginning) (region-end)) (let ((bds (bounds-of-thing-at-point 'paragraph)) ) (listnil (car bds) (cdr bds)) ) ) ) (requ…

Emacs: Change {Round, Square, Curly} Brackets in Text

when you have data in different languages, sometimes you need to convert round brackets to square brackets, or curly brackets.When you need to do that, it's often tedious. You have to do replacement twice, once for the left bracket, then right bracket. Here's a command that helps:(defun change-bracket-pairs (fromType toType) "Change bracket pairs from one type to another on text selection or text block. For example, change all parenthesis () to square brackets []. In lisp code, fromType is a string of a bracket pair. ⁖ \"()\", likewise for toType." (interactive (let ( (bracketTypes '("[]""()""{}""〈〉""《》""「」""『』""【】""〖〗")) ) (list (ido-completing-read "Replace this:" bracketTypes "PREDICATE" ) (ido-completing-read "To:" bracketTypes "PREDICATE" ) ) ) ) (let* ( (bds (get-se…

Perl: Sort List, Matrix, Object

Perm URL with updates: http://xahlee.info/perl/perl_sort.htmlThis page shows you how to sort in Perlhere's a example of sort (Perl 5.14):#-*- coding: utf-8 -*- # perl # sort a list @li = (1,9,2,3); @li2 = sort {$a <=> $b} @li; # original list is not changed printjoin(' ', @li2); # 1 2 3 9In Perl, sort is a function. It returns the sorted result as another list.“sort” takes the form sort {…} @myList. Inside the enclosing braces is the body of the ordering function, where variables 「$a」 and 「$b」 inside are predefined by the language to represent two elements in the list. The operator <=> returns -1 if left operand is less than the right operand. If equal, it returns 0, else 1. It is equivalent to Python's “cmp” function.Another form of sort is sort orderFunctionName @list, which uses a function name in place of the comparison block. The function should have 2 parameters, and return one of {-1, 0, 1}.Compare as Number or as StringPerl has 2 comparison operato…

Syntax = Most Important Aspect of a Programing Language

the quality of a programing language, can be judged by how much of it can be explained by its syntax alone.with this criterion, the order are roughly: Mathematica ≻ PHP ≻ Lisp ≻ JavaScript ≻ Ruby? ≻ Perl ≻ Python ≻ Java.If you have coded one of {Haskell, ML/OCaml/F#, erlang, Pascal/Ada, Lua, tcl, PostScript}, i'd be interested in your opinion on their placement in the above. (you should have coded in the lang for a few years)

Python 3: Sort List, Matrix, Object

Perm URL with updates: http://xahlee.info/python/python3_sort.htmlThis page shows you how to sort in Pythonsort MethodYou can use the “sort” method. For example:# -*- coding: utf-8 -*- # python 3 li = [1,9,2,3] li.sort() # the variable is modified print(li) # [1, 2, 3, 9] sort FunctionYou can use the “sorted” function. This does not modify the variable. For example:# -*- coding: utf-8 -*- # python 3 li = [1,9,2,3] li2 = sorted(li) print(li) # [1, 9, 2, 3] print(li2) # [1, 2, 3, 9] sort by Column/KeyYou can sort by specifying a optional parameter “key”. This is most useful for sorting a matrix.# -*- coding: utf-8 -*- # python 3 # sort a matrix li = [[2,6],[1,3],[5,4]] li.sort(key=lambda x:x[1]) print(li); # prints [[1, 3], [5, 4], [2, 6]] Sort and ReverseAnother optional parameter is “reverse”. You can use it like this# -*- coding: utf-8 -*- # python 3 # sort a matrix, by 2nd column, reverse order li = [[2,6],[1,3],[5…

Programing Language Design: Syntax Sugar Problem: Irregularity vs Convenience

Perm URL with updates: http://xahlee.info/comp/syntax_irregularity_vs_convenience.htmlone of the idiocy of HTML spec is that the “pre” tag discards the first blank line.for example, if you have: <prestyle="border:solid thin red"> x = 3 </pre>Here's how your browser renders it: x = 3 The first blank line is ignored. However, only the FIRST blank line is ignored. If you have 2 blank lines in the beginning, it'll be rendered with 1 blank line. <prestyle="border:solid thin red"> x = 3 </pre> x = 3 They do this, because, it's convenient for coder. Because, one likes to see the pre content aligned to the left in raw HTML.For example, you rather write it this way: <pre> 1 2 3 </pre>than <pre>1 2 3</pre>this is a idiocy because it mixes convenience with syntax.The problem comes, when you have programs that deal with code. That's why, in programing, computing tech, there are one hundred exceptions, irregulari…

Emacs: How Do You Insert Current Date?

here's how i do it. (defuninsert-date (&optional addTimeStamp-p) "Insert current date and or time. • In this format yyyy-mm-dd. • When called with `universal-argument', insert date and time, e.g. 2012-05-28T07:06:23-07:00 • Replaces text selection. See also `current-date-time-string'." (interactive "P") (when (region-active-p) (delete-region (region-beginning) (region-end) ) ) (cond ((equal addTimeStamp-p nil ) (insert (format-time-string "%Y-%m-%d"))) (t (insert (current-date-time-string))) ) ) (defuncurrent-date-time-string () "Returns current date-time string in full ISO 8601 format. Example: 「2012-04-05T21:08:24-07:00」. Note, for the time zone offset, both the formats 「hhmm」 and 「hh:mm」 are valid ISO 8601. However, Atom Webfeed spec seems to require 「hh:mm」." (concat (format-time-string "%Y-%m-%dT%T") ((lambda (ξx) (format "%s:%s" (substring ξx 0 3) (substring ξx 3 5))) (format-…

emacs: xah-html-mode, improved xhm-make-citation

much improved “xhm-make-citation”. Now, the order of lines for {title, url, author, date} doesn't matter. Get it in Emacs: Xah HTML Modeto learn how to write it, see Emacs Lisp: Writing a make-citation Command

keyboard: one thousand function keys

Logic Write Style: the Incongruousness of the Word “Actually”

Perm URL with updates: http://wordyenglish.com/lit/the_word_actually.htmlHere's the inline doc of “assoc” from GNU Emacs 24.3.1: assoc is a built-in function in `C source code'. (assoc KEY LIST) Return non-nil if KEY is `equal' to the car of an element of LIST. The value is actually the first element of LIST whose car equals KEY. note the word “actually”.the word “actually” is often used for emphasis purposes. It means “in fact”. It came from “actual” + “ly”.actually «early 15c., “in fact, in reality” (as opposed to in possibility), from actual + -ly (2). Meaning “actively, vigorously” is from mid-15c.; that of “at this time, at present” is from 1660s. As an intensive added to a statement and suggesting “as a matter of fact, really, in truth” it is attested from 1762.»when writing in a logical style, the use of “actually” is a oddity, incongruous, or redundant.On the Postposition of Conjunction in Penultimate Position of a SequencePlurality in English GrammarWriting Styl…

Linux: Convert HTML to PDF

to convert HTML to PDF on Linux, you can use wkhtmltopdf. It's based on webkit, the web browser engine used by Safari an Google Chrome.# install sudo apt-get install wkhtmltopdfwkhtmltopdf my_resume.html my_resume.pdfif you just have one single file, you can also use libreoffice.# install libreoffice sudo apt-get install libreofficeType libreoffice to start it, then, open the HTML file, then use menu 〖File ▸ Export…〗.

video: Emacs, Shell, Abbrev, and ELISP Power to Bear!

Image
here's a video version of today tutorial.Emacs, Shell, Abbrev, and ELISP Power to Bear!code at Emacs, Shell, Abbrev, and ELISP Power to Bear!

Emacs, Shell, Abbrev, and ELISP Power to Bear!

Perm URL with updates: http://ergoemacs.org/misc/emacs_abbrev_shell_elisp.htmlUsing Abbrev for Shell CommandsYou can define abbrev for frequently used shell commands. For example, i type “3rs” and it expands torsync -z -r -v -t --chmod=Dugo+x --chmod=ugo+r --delete --exclude='*~' --exclude='.bash_history' --exclude='logs/' --rsh='ssh -l u89150' ~/web/ u89150@s72750.example.com:~/Is emacs abbrev better than bash alias?Emacs abbrev is better than bash alias, because you see the full command. what about using shell 【Ctrl+r】 back search feature?That's great, but for frequently used command, alias or abbrev is better, because you get EXACTLY the command you want.With back search, you might have modified a command, and you have to eye-ball to be sure it's the command you want. Bash alias or emacs abbrev are muscle memory. for how to set abbrev, see: Using Emacs Abbrev Mode for AbbreviationAdvantage of Using Shell Inside Emacsusing shell inside emacs is…

unix linux shell uniq unicode bug

Perm URL with updates: http://xahlee.info/comp/unix_uniq_unicode_bug.htmlHere's a bug of unix/linux GNU shellutil uniq.Create a file of the following text: ═ ═ ═ ║ ║ ║ ╒ ╓ ╔ ╕ ╖ ╗ ╘ ╙ ╚ ╛ ╜ ╝ ╞ ╟ ╠ ╡ ╢ ╣ ╤ ╥ ╦ ╧ ╨ ╩ ╪ ╫ ╬save it as 〔unicode.txt〕, then do cat unicode.txt | unicq -c. You get “33 ═”. Idiotic unix.◆ uniq --version uniq (GNU coreutils) 8.13 Copyright (C) 2011 Free Software Foundation, Inc. License GPLv3+: GNU GPL version 3 or later . This is free software: you are free to change and redistribute it. There is NO WARRANTY, to the extent permitted by law. Written by Richard M. Stallman and David MacKenzie.The man page doesn't mention anything about Unicode. Here's my locale setting anyhow.◆ locale LANG=en_US.UTF-8 LANGUAGE= LC_CTYPE="en_US.UTF-8" LC_NUMERIC="en_US.UTF-8" LC_TIME="en_US.UTF-8" LC_COLLATE="en_US.UTF-8" LC_MONETARY="en_US.UTF-8" LC_MESSAGES="en_US.UTF-8" LC_PAPER="en_US.UTF-8" LC_…

Functional Programing Meta Language (ML) in Emacs Lisp!

discovered that emacs has a bundled library for functional programing pattern matching! The package file is 〔pcase.el〕. It's part of GNU Emacs 24.3.1.it's written by Stefan Monnier, a professor of functional programing research, and one of the two current leader of emacs dev.you can get to the file by calling describe-function then pcase-let, then click on the file name.Note: ML stands for Meta Language. It was a family of languages. Current popular decendents include OCaml and Microsoft's FSharp. 〔☛ Xah's OCaml Tutorial〕 Among functional programing languages, OCaml is one of those that's heavily used in the industry (⁖ Mldonkey, Unison 〔☛ Unison Tutorial〕), especially in math formal proof systems ⁖ Coq. And it's famously used in JaneStreet. 〔☛ OCaml Use in Industry: Janestreet Talk by Yaron Minsky 📺〕 Proof systems written in OCaml includes: Coq, HOL Light. 〔☛ State of Theorem Proving Systems 2008〕Also, the designer of ML is Robin Milner (1934 〜 2010), he die…

Writing grep/sed in Python, Perl, Emacs Lisp

emacs: xah-html-mode

Perm URL with updates: http://ergoemacs.org/emacs/xah-html-mode.htmlThis is the home page of xah-html-mode, a emacs major mode for HTML5.How's it diff from default HTML mode or other HTML modes?The basic idea of this mode is simple keyword based coloring. Just color keywords, that's all, no fancy syntax parsing. (this also means you'll get wrong colors in text.)The idea of simple keyword coloring is that, if a word is colored in a particular way, you know for sure it is a keyword in one of {HTML, CSS, JavaScript, PHP, …}, and you can tell if it is {type, class, var, function, property} by the coloring (most of the time). It lets you easily recognize typos too, because it won't be colored.that's the basic idea. This mode currently alpha software. It is result of my several years of manual coding HTML. All commands have fairly complete and correct inline doc. But there's no keybinding as of now. You'll have to set them yourself. some command names might also c…