Visible line-numbers in emacs


I was asked about adding line-numbers down the side of code in emacs; This is the solution.

A note on programming fonts

The standard X11 fonts aren’t bad, but there are better options. In particular there are fonts designed for programming that give better symbol/number/parentheses contrast and are clearer at small sizes:

For terminal apps I use SGI’s ‘screen‘.

For applications that use fontconfig you should just be able to drop these into your ~/.fonts directory and (possibly) run fc-cache. However if your application doesn’t use fontconfig (Emacs is probably the only one you’ll use nowadays) the following is how to install these into the X server:

Copy the font files to a directory that is visible to the x-server’s Font Path. You may check the font path of the running server by typing the command:

xset q

On my FreeBSD machine I chose /usr/X11R6/lib/X11/fonts/misc/ to hold these new fonts. Next, you need to update the index file ‘fonts.dir’ by running ‘mkfontdir’. Here’s the command I used for my path:

mkfontdir /usr/X11R6/lib/X11/fonts/misc/

If you don’t want to restart your X server, you can update your current X session using:

xset fp rehash

Otherwise restarting X will also load the new fonts.

(I got these instructions off the web, I forget where from.) You may also need to restart the X font server.

Also for emacs users, alt-font-menu makes selecting these fonts easier.

How not to run a business

I ordered a wireless controller from Games-Warehouse for my home Xbox (which is hacked to act as a media-center). This morning I received an email from them (edited highlights follow):

We have been unable to locate you in the White Pages at the address supplied (the National Australia Bank recommends we use the White Pages as a tool to help us prevent credit card fraud).

The address was the University of Sydney.

We accept payment by alternate methods which do not require address verification. These payment options are Direct Deposit, Money Orders and Cheque.

… however we won’t supply information on how to do this here.

We will need you to photocopy an official letter showing the credit card billing address (eg. Credit Card Statement showing the cards billing address, a rates notice/telephone bill showing billing address or a letter from a government department etc.) and fax it to us at…

Sure, I carry those around with me all the time.

My reply:

Hi,

Please cancel this order.

Thanks,
Steve

I sympathise with them wishing to prevent CC fraud, but I’ve never been asked for this before, and preemptively implying your customer may be a thief and then making it difficult give them your money is no way to run a business.

Cairo in Breezy

I’ve been fiddling with upgrading a couple of Ubuntu installations to the beta version of the upcoming ‘Breezy’ release. The now includes Cairo as the GTK backend, but I couldn’t work out why the colour-picker wasn’t being anti-aliased. As it turns out the Gimp uses its own picker, but if you access GTK built-in version (eg. in the panel properties) you get this:

Neato

http://hugin.sourceforge.net/

Interesting link

http://www.gvisit.com/

Geographically track site visitors in real-time.

Weekend lisp hacking

I spent a bit of the weekend on lisp-hacking and revisited my lisp version of the 'Lattice' server. I'd started this at LinuxConf but continually ran into issues with implementing truely asyncronous reads in SBCL's sockets. There are various fragments of code showing how to do this around, but they all use Lisp's native read, and my experiments show it would block, which is potentially disastrous in real-world usage. However I eventually worked out that I should be using sb-bsd-sockets:socket-receive, which is implemented with recvfrom. After nailing-down the various error conditions that can be returned (it's rather inconsistent there) I now have a toy single-threaded server implementation:

(Update: as somebody has asked to use this, I hearby place my portions of the code in the public domain)

Lisp:
  1. (require 'sb-bsd-sockets)
  2.  
  3. ;; Most of the following is cribbed from the trivial-sockets package
  4. ;; (http://www.cliki.net/trivial-sockets).  I've ripped it out as it's
  5. ;; not clear that the return values will remain the same in future
  6. ;; versions and I need to get at the underlying file descriptors for
  7. ;; serve-event.
  8. (defun resolve-hostname (name)
  9.   (cond
  10.     ((eql name :any)  #(0 0 0 0))
  11.     ((typep name '(vector * 4)) name)
  12.     (t (car (sb-bsd-sockets:host-ent-addresses
  13.          (sb-bsd-sockets:get-host-by-name name))))))
  14.  
  15. (defun open-server (&key (host :any) (port 0)
  16.             (reuse-address t)
  17.             (backlog 1)
  18.             (protocol :tcp))
  19.   "Returns a server socket"
  20.   (let ((sock (make-instance 'sb-bsd-sockets:inet-socket
  21.                  :type :stream
  22.                  :protocol protocol)))
  23.     (when reuse-address
  24.       (setf (sb-bsd-sockets:sockopt-reuse-address sock) t))
  25.     (sb-bsd-sockets:socket-bind  sock (resolve-hostname host) port)
  26.     (sb-bsd-sockets:socket-listen sock backlog)
  27.     sock))
  28.  
  29. (defmacro with-server ((name arguments) &body forms)
  30.   `(let (,name)
  31.     (unwind-protect
  32.      (progn
  33.        (setf ,name (open-server ,@arguments))
  34.        ,@forms)
  35.       (when ,name (sb-bsd-sockets:socket-close ,name)))))
  36.  
  37. ;; ;; End trivial-sockets ;; ;;
  38.  
  39. ;; Server implementation
  40.  
  41. (defconstant +buflen+ 16)      ; Short for overrun testing
  42. (defstruct server-session
  43.   sock ;; Socket returned by accept
  44.   fd ;; Raw file handle
  45.   stream ;; Lisp stream
  46.   buffer ;; Pre-allocated incoming buffer
  47.   handler ;; serve-event handler
  48.   )
  49.  
  50. (defun data-received-handler (session)
  51.   "Reads all pending characters on a socket into the session buffer"
  52.   (format t "Got incoming data event ... ~%")
  53.   (let ((buffer (server-session-buffer session))
  54.         (sock (server-session-sock session)))
  55.     (do ((fin nil))
  56.     (fin t)
  57.       (setf (fill-pointer buffer) +buflen+)
  58.       (multiple-value-bind (buf len raddr)
  59.           (sb-bsd-sockets:socket-receive sock buffer nil)
  60.         (declare (ignore raddr))
  61.         (if (null buf)
  62.             (setf fin t)
  63.             (setf (fill-pointer buffer) len)))
  64.       (cond ((= (length buffer) 0)
  65.              (format t "  Got 0 bytes, closing socket and removing handler~%")
  66.              (sb-bsd-sockets:socket-close sock)
  67.              (sb-sys:remove-fd-handler (server-session-handler session))
  68.              (setf fin t))
  69.             (fin (format t "Got NIL, returning~%"))
  70.             (t
  71.              (format t "  Read ~a bytes: ~a~%" (length buffer) buffer))))))
  72.  
  73. (defun accept-handler(socket)
  74.   (format t "I've got a new connection on fd ~a~%" socket)
  75.   (let* ((conn (sb-bsd-sockets:socket-accept socket))
  76.          (fd (sb-bsd-sockets:socket-file-descriptor conn))
  77.          (session (make-server-session
  78.            :sock conn
  79.            :fd fd
  80.            :stream (sb-bsd-sockets:socket-make-stream 
  81.                 conn :input t :output t
  82.                 :element-type 'character
  83.                 :buffering :none)
  84.            :buffer (make-array +buflen+
  85.                        :element-type 'character
  86.                        :adjustable nil
  87.                        :fill-pointer t)))
  88.          (handler (sb-sys:add-fd-handler
  89.            fd :input
  90.            #'(lambda (fd) (declare (ignore fd))
  91.                  (data-received-handler session)))))
  92.     (format t "New socket is ~a~%" conn)(force-output)
  93.     (setf (sb-bsd-sockets:non-blocking-mode conn) t)
  94.     (setf (server-session-handler session) handler)))
  95.  
  96.  
  97. (defun start-server ()
  98.   (with-server (socket (:port 8000 :reuse-address t))
  99.     (sb-sys:with-fd-handler ((sb-bsd-sockets:socket-file-descriptor socket)
  100.                              :input #'(lambda (fd) (declare (ignore fd))
  101.                           (accept-handler socket)))
  102.       (loop ;; FIXME: End condition
  103.        (format t "Entering serve-all-events...~%")(force-output)
  104.        (sb-sys:serve-all-events)
  105.        (format t "Events served~%")))))

  1. Archives

  2. Categories

  3. Twitter

    • Just got pictures of the earthquake damage from my sister in Kaiapoi. Real "the ground opened up" stuff. 2 minutes ago
    • Ooo, the beta of Angry Birds is out on Android. 17 hours ago
    • Released a new version of my Android Internode widget; fixes the problem with Internode's new SSL cert. 22 hours ago
    • @wangjammer5: Cool, here's something to get you started: http://is.gd/eT33b :) 1 day ago
    • So, if iTune Ping is Apple's social network will the perpetual Apple tweet circle-jerk move there now? 1 day ago
  4. RSS Google Reader Shared Items