<div dir="ltr"><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">I remember we discussed compactifying perl on this list a long time ago, and this time I decided to have some fun with emacs overlays with python. The resulting mode is attached below. With this mode, when typing the following:<br>
<br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif"><span style="font-family:courier new,monospace">f = lambda x: x**2<br>for i in range(5):<br> print f(i)<br><br>v = range(10,20,2)<br>for i,y in enumerate(v):<br>
print i,y</span><br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">you see the following in the emacs buffer:<br><span style="font-family:courier new,monospace"><br>f = λ x: x**2<br>
∀ i ∈ ℜ(5):<br> print f(i)<br> <br>v = ℜ(10,20,2)<br>∀ i,y ∈ €(v):<br> print i,y</span><br> <br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">Is this a good idea? Probably absolutely not, but it is quite fun. :-)<br>
<br>;;; pretty-mode.el --- Pretty-print lambdas and other functions<br><br>;; Author: Mark Triggs <<a href="mailto:mst@dishevelled.net">mst@dishevelled.net</a>>, Dov Grobgeld<<a href="mailto:dov.grobgeld@gmail.com">dov.grobgeld@gmail.com</a>><br>
;; <br><br>;; This file is free software; you can redistribute it and/or modify<br>;; it under the terms of the GNU General Public License as published by<br>;; the Free Software Foundation; either version 2, or (at your option)<br>
;; any later version.<br><br>;; This file is distributed in the hope that it will be useful,<br>;; but WITHOUT ANY WARRANTY; without even the implied warranty of<br>;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the<br>
;; GNU General Public License for more details.<br><br>;; You should have received a copy of the GNU General Public License<br>;; along with GNU Emacs; see the file COPYING. If not, write to<br>;; the Free Software Foundation, Inc., 59 Temple Place - Suite 330,<br>
;; Boston, MA 02111-1307, USA.<br><br>;;; Commentary:<br><br>;; This idea is *completely* stolen from Luke Gorrie's pretty-lambda.el. I'm<br>;; just curious to see how easily it can be done with font-locking alone.<br>
;;<br>;; Modified by Dov Grobgeld to support changing lots of variables<br>;; to make python look like APL. This is probably a very bad idea, but<br>;; quite fun. :-)<br><br><br>;;; Code:<br><br>(setq pretty-symbols (make-hash-table :test 'equal))<br>
<br>;; Setup the display table various matchings<br>(puthash "enumerate" "€" pretty-symbols)<br>(puthash "lambda" "λ" pretty-symbols)<br>(puthash "for" "∀" pretty-symbols)<br>
(puthash "in" "∈" pretty-symbols)<br>(puthash "range" "ℜ" pretty-symbols)<br><br>(defun hash-keys (hashtable)<br> "Return all keys in hashtable."<br> (let (allkeys)<br>
(maphash (lambda (kk vv) (setq allkeys (cons kk allkeys))) hashtable)<br> allkeys<br> )<br>)<br><br>(defun join-list (my-list glue)<br> "Join a string list with glue between the list items"<br> (let ((joined-list (car my-list)))<br>
(mapcar (lambda (v) (setq joined-list (concat joined-list glue v))) (cdr my-list))<br> joined-list))<br><br>;; build the regex<br>(setq pretty-regex<br> (concat<br> "\\b\\("<br> (join-list (hash-keys pretty-symbols) "\\|")<br>
"\\)\\b"<br> ))<br><br>(defun pretty-fontify (beg end)<br> (save-excursion<br> (pretty-unfontify beg end)<br> ;; Mark incorrect uses of spacing.<br> (goto-char beg)<br> (while (re-search-forward pretty-regex end t)<br>
(let ((o (car (overlays-at (match-beginning 1)))))<br> (unless (and o (eq (overlay-get o 'type) 'pretty))<br> (let* ((overlay (make-overlay (match-beginning 1) (match-end 1)))<br> (match-text (buffer-substring-no-properties (match-beginning 1) (match-end 1))))<br>
(message match-text)<br> (overlay-put overlay 'type 'pretty)<br> (overlay-put overlay 'evaporate t)<br> (overlay-put overlay 'display (gethash match-text pretty-symbols))))))))<br>
<br><br>(defun pretty-unfontify (beg end)<br> (mapc #'(lambda (o)<br> (when (eq (overlay-get o 'type) 'pretty)<br> (delete-overlay o)))<br> (overlays-in beg end)))<br><br><br>(define-minor-mode pretty-mode<br>
"Indicate where only a single space has been used."<br> nil " λ" nil<br> (cond ((not pretty-mode)<br> (jit-lock-unregister 'pretty-fontify)<br> (pretty-unfontify (point-min) (point-max)))<br>
(t (pretty-fontify (point-min) (point-max))<br> (jit-lock-register 'pretty-fontify))))<br><br>(provide 'pretty-mode)<br>;;; pretty-mode.el ends here<br><br><br><br></div><div class="gmail_default" style="font-family:arial,helvetica,sans-serif">
<br></div></div>