Lisp HUG Maillist Archive

LispWorks handling of Emacs-style -*- line (aka File Variables)

Hi

This weekend I was bitten by LispWorks' handling of Emacs-style -*-
file variables. Given a UTF-8 encoded source file like the one below:

;;;; -*- mode: lisp; syntax: common-lisp; base: 10; coding: utf-8-unix; external-format: (:utf-8 :eol-style :lf) -*-
;;;; encoding.lisp

(in-package #:cl-user)

(defun foo ()
  (format t "foo: © ë ~A~&" #\U+03BB))

;;;; eof


CL-USER 1 > (load "encoding.lisp")
; Loading text file /home/ndj/devel/cl/test/src/encoding.lisp
#P"/home/ndj/devel/cl/test/src/encoding.lisp"

CL-USER 2 > (foo)
foo: © ë λ       <== Why is it read as Latin-1?
NIL

CL-USER 3 > (load "encoding.lisp" :external-format '(:utf-8 :eol-style :lf))
; Loading text file /home/ndj/devel/cl/test/src/encoding.lisp
#P"/home/ndj/devel/cl/test/src/encoding.lisp"

CL-USER 4 > (foo)
foo: © ë λ         <== Ok, the source file is UTF-8 encoded after all.
NIL


After re-reading the Internationalization chapter in the LW User Guide &
Reference Manual, looking at several -*- variable examples in LW and
open source code, and finally reading the Emacs Info section on File
Variables, I realized that I was somewhat misled by the LW manual
examples 
(e.g. http://www.lispworks.com/documentation/lw60/EDUG-U/html/eduser-u-96.htm), 
as well as by most of the -*- file variable examples in LW and open
source code. system:find-encoding-option is not as forgiving as Emacs
and will ignore the external-format/encoding file variable if it is not
terminated by a semicolon. So this fixed it:

;;;; -*- mode: lisp; syntax: common-lisp; base: 10; coding: utf-8-unix; external-format: (:utf-8 :eol-style :lf); -*-

The external-format and encoding variables values are read in the keyword
package, so it can also be specified as e.g.:
;;;; -*- encoding: (utf-8 eol-style lf); -*-

I'm probably just stupid/inexperienced, but maybe this will save someone
else a couple of hours or remind you how great it is to program in a
language with a simple syntax. I wish Emacs (and thus LispWorks) used
an association or property list for file variables...

Regards.
Nico


Updated at: 2020-12-10 08:38 UTC