how to install autopair from emacs package system

how to install autopair

autopair is available in Marmalade ELPA. So your init file should have these lines:

(require 'package)
(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/") t)

then you can do M-x list-packages and install autopair from there.

how to enable autopair without using require

If you include the following code in your emacs init file

(require 'autopair)
(autopair-global-mode 1)

then start up error may occur because path for autopair is not added to load-path until after package-initialize has run. One way to avoid this problem is to put the line (package-initialize) somewhere before (require 'autopair) in your init file, another way is to use a hook that runs after package-initialize. A third way which I prefer is to use eval-after-load like this:

(eval-after-load "autopair-autoloads" ; <-- "autopair-autoloads" not "autopair"
  '(progn
     (require 'autopair)
     (autopair-global-mode 1)

     ;; and some further customization of autopair
     ;; ...
     ))

How does that work? Emacs always executes package-initialize after loading your init file (unless -q option is passed). What package-initialize does is for each installed package, say xyz, it adds to load-path the path to the directory where xyz.el is, but it doesn’t run xyz.el. Instead it runs xyz-autoloads.el. If xyz package were for a major mode, running xyz-autoloads.el would add an entry to auto-mode-alist so that when Emacs opens a file with the relevant filename extension, it turns on the xyz major mode automatically by running the command xyz-mode. The xyz-autoloads would also setup autoload for the command xyz-mode by telling Emacs that the definition of the function xyz-mode can be found in a file named xyz.el. That was for a major mode package. What about a package like autopair that gives you a global minor mode?

autopair-autoloads.el could do either (1) do nothing or (2) setup autoload for the global minor mode command (autopair-global-mode) or (3) not only setup autoload for the global minor mode command but also execute that command to turn on the global mode. (1) means that after Emacs has finished loading, you have to execute (require ‘autopair) before being able to do M-x autopair-global-mode. (2) means that you don’t have to, but you still have to do M-x autopair-global-mode to enable the global mode. (3) means that the global mode will be automatically turned on after Emacs start up is finished. If you include the eval-after-load code above anywhere in your init file (as a top level form of course), it will turn on the global mode for you automatically in all three cases. Also, graceful degradation: the same eval-after-load code above does not cause start up error even in an environment where autopair is not installed.

how to disable autopair in some modes while keeping autopair globally on

;; Disables autopair mode in JavaScript mode (js-mode) and C mode buffers.
(dolist (hk (list 'js-mode-hook 'c-mode-common-hook))
  (add-hook hk
            #'(lambda ()
                (autopair-mode -1))))

A nice thing about (add-hook HOOK FUNCTION) is that unlike add-to-list it works even when the hook variable is not defined at the time (add-hook HOOK FUNCTION) runs. This means that it will be fine to place the add-hook code outside the eval-after-load form.

In order to find the name of the hook you want to use, press C-h m and then click on the relevant el file and search for -hook.

further customization of autopair

One would expect that one can customize autopair a lot from M-x customize-group RET autopair RET but last time I checked there was just one autopair user option shown in the Customize interface, the option for turning it on and off. The official autopair page has information on how to customize the behavior of autopair.

Posted in Emacs | Tagged , | 1 Comment

how to know which archive an emacs ELPA package is from

The buffer from M-x list-packages doesn’t display which archive each package is from. M-x describe-package doesn’t either. But there is a function for it. The function is package-archive-base.

For example, I use two additional ELPA archives:

(require 'package)
(add-to-list 'package-archives
             '("marmalade" . "http://marmalade-repo.org/packages/") t)
(add-to-list 'package-archives '("org" . "http://orgmode.org/elpa/") t)

So I get packages from three archives: GNU ELPA (default), Marmalade, and Org ELPA. Suppose I wonder where the package htmlize is from. When I run

(require 'htmlize)

in my Emacs, it doesn’t report error, which means that the htmlize package is installed in my Emacs. But when Alice runs that in her Emacs, it reports error. Alice asks me which archive she should add to install htmlize via M-x list-packages. To find out, I just have to run:

(package-archive-base "htmlize") ; => "http://marmalade-repo.org/packages/"

htmlize is available in Marmalade.

Posted in Emacs | Tagged , , | Leave a comment

batch converting raw image files to JPEG using Photoshop and Python

Recently I had to batch convert a bunch of raw image files to JPEG. I had a folder given to me and this folder had a lot of image files, some were in JPEG format, and some other were in NEF format. The NEF files are a kind of raw image files taken right from some digital camera. Windows 7′s built-in file browser could not display the images for NEF files, but Photoshop could. They were large in file size compared to JPEG files. My task was to convert all NEF files to JPEG and discard the NEF files to save disk space.

I thought to write a Python script to do the job and googled around but there seemed to be no module for conversion. PIL is a popular image library for Python and it doesn’t support reading NEF files.

So I thought maybe what if I could automate Photoshop? Or what if Photoshop had some kind of API? So I googled around further and found that Photoshop has a built-in batch conversion feature. You can access the feature by clicking File > Scripts > Image Processor… menu from Photoshop. See Getting To Know Photoshop: Image Processor

The images folder I had to work with had many subfolders. Image files in some subfolders were all JPEG, but some subfolders only had NEF files in them, and then there were subfolders which had both JPEG files and NEF files. I made a simplified test folder called pic-folder which has two subfolders and six image files in it. The file list of pic-folder:

pic-folder\10\A.jpg
pic-folder\10\A.NEF
pic-folder\10\B.jpg
pic-folder\10\B.NEF
pic-folder\20\C.NEF
pic-folder\20\D.NEF

The subfolder 10 represents a subfolder where somebody else has already converted the NEF files to JPEG but not discarded the NEF files. The subfolder 20 represents a subfolder where conversion is not done.

Let’s see what Photoshop can do to that folder. Open Image Processor from Photoshop. In “1. Select the images to process” section, select pic-folder, and check “Include All sub-folders”. In “2. Select location to save processed images” section, select a different folder (an empty folder), say pic-folder-dest, and check “Keep folder structure”. In “3. File Type” section, make sure the option “Save as JPEG” is checked, which is by default checked. In “4. Preferences” section, make sure that “Run Action” is unchecked. I don’t know what the option “Include ICC Profile” does, but it’s checked by default, so let’s leave it checked. Now click “Run”. Photoshop will open each image file in subfolders of pic-folder and save as JPEG in appropriate subfolders of pic-folder-dest. After Photoshop finishes its job, the file list of pic-folder-dest should be like this:

pic-folder-dest\10\A.jpg
pic-folder-dest\10\A_1.jpg
pic-folder-dest\10\B.jpg
pic-folder-dest\10\B_1.jpg
pic-folder-dest\20\C.jpg
pic-folder-dest\20\D.jpg

Photoshop processed all six files so we got six JPEG files in pic-folder-dest. It didn’t skip JPEG files. It first processed pic-folder\10\A.jpg, which is already in JPEG format, and saved the result as pic-folder-dest\10\A.jpg, and then it processed pic-folder\10\A.NEF, and saved the result as pic-folder-dest\10\A_1.jpg because the name pic-folder-dest\10\A.jpg was occupied by then, and so on. That’s a problem.

How to skip jpg files, and also skip NEF files which already have corresponding jpg files? Photoshop has scripting feature. Maybe there is a way to customize the job of Image Processor further by scripting, but I had no time to learn how to script Photoshop. The folder I was given had to be processed within few days. So what I did was write and run a pre-processing Python script which takes out all NEF files without corresponding jpg files to a separate folder, then run Image Processor on that separate folder, and finish with a post-processing Python script that takes output jpg files and put them into the original folder.

Part of pre-processing script:

import shutil, os, errno

# http://stackoverflow.com/questions/273192/python-best-way-to-create-directory-if-it-doesnt-exist-for-file-write
def ensuredirs(path):
    try:
        os.makedirs(path)
    except OSError as exc:
        if exc.errno == errno.EEXIST:
            pass
        else:
            raise

def move_file(src, dst, dryrun=False):
    if dryrun:
        print 'os.rename(A, B)'
        print 'A:', src
        print 'B:', dst
    else:
        ensuredirs(os.path.dirname(dst))
        os.rename(src, dst)

JPG_EXTS =  ['.JPG', '.JPEG', '.jpg', '.jpeg']

def is_nef(fn):
    return fn.endswith('.nef') or fn.endswith('.NEF')

def is_jpg(fn):
    root, ext = os.path.splitext(fn)
    return ext in JPG_EXTS

def take_nefs_out(src_dir, dest_dir, dryrun=False):
    """Take out NEF files in src_dir with no accompanying JPEG files, 
    create folder dest_dir, and move the files to dest_dir,
    preserving the folder structure.
    """
    assert os.path.isdir(src_dir)
    assert not os.path.exists(dest_dir)
    for p, dirs, files in os.walk(src_dir):
        for fn in files:
            if is_nef(fn):
                root, ext = os.path.splitext(fn)
                if any(root + jpg_ext in files for jpg_ext in JPG_EXTS):
                    continue
                fullpath = os.path.join(p, fn)
                newfullpath = fullpath.replace(src_dir, dest_dir, 1)
                move_file(fullpath, newfullpath, dryrun)

Try a dry run on the test folder.

os.chdir(parent_folder_of_pic_folder)
take_nefs_out("pic-folder", "nef-folder", dryrun=True)
os.rename(A, B)
A: pic-folder\20\C.NEF
B: nef-folder\20\C.NEF
os.rename(A, B)
A: pic-folder\20\D.NEF
B: nef-folder\20\D.NEF

OK, it’s selecting the right NEF files and it seems they’ll move to right places. Run take_nefs_out.

os.chdir(parent_folder_of_pic_folder)
take_nefs_out("pic-folder", "nef-folder")

After that, the file list for pic-folder should be:

pic-folder\10\A.jpg
pic-folder\10\A.NEF
pic-folder\10\B.jpg
pic-folder\10\B.NEF

and the file list for nef-folder should be:

nef-folder\20\C.NEF
nef-folder\20\D.NEF

Run Image Processor on nef-folder to create JPEG files in jpg-folder. Then the file list for jpg-folder should be:

jpg-folder\20\C.jpg
jpg-folder\20\D.jpg

Finally, we need to move JPEG files in jpg-folder to pic-folder and remove NEF files.

Part of post-processing script:

def move_jpgs(src_dir, dest_dir, dryrun=False):
    assert all(os.path.isdir(d) for d in [dest_dir, src_dir])
    for p, dirs, files in os.walk(src_dir):
        for fn in files:
            assert is_jpg(fn)
            src_path = os.path.join(p,fn)
            dst_path = src_path.replace(src_dir,dest_dir, 1)
            move_file(src_path, dst_path, dryrun)

move_jpgs("jpg-folder", "pic-folder", dryrun=True)

After running move_jpgs(“jpg-folder”, “pic-folder”), the file list of pic-folder should be:

pic-folder\10\A.jpg
pic-folder\10\A.NEF
pic-folder\10\B.jpg
pic-folder\10\B.NEF
pic-folder\20\C.jpg
pic-folder\20\D.jpg

To delete NEF files:

def remove_file(fn, dryrun=False):
    assert os.path.isfile(fn)
    if dryrun:
        print "os.remove on:", fn
    else:
        os.remove(fn)

def remove_nefs(adir, dryrun=False):
    """Remove NEF files from folder adir."""
    assert os.path.isdir(adir)
    for p, dirs, files in os.walk(adir):
        for fn in files:
            if is_nef(fn):
                fullpath = os.path.join(p, fn)
                remove_file(fullpath, dryrun)

remove_nefs("pic-folder")

After that, the file list of pic-folder should be:

pic-folder\10\A.jpg
pic-folder\10\B.jpg
pic-folder\20\C.jpg
pic-folder\20\D.jpg
Posted in Python | Tagged , , , | Leave a comment

js2-mode setup recommendation

Update @ 2013 March: cleaned up deprecated recommendations

js2-mode is a JavaScript mode for Emacs with JavaScript parser built-in which allows syntax error detection on the fly and JavaScript refactoring (js2-refactor.el). If you are going to use js2-mode, either install the mooz fork or install the GNU ELPA version available via M-x list-packages:

How to install the mooz fork of js2-mode

The original old js2-mode had a problem of enforcing an unpopular indentation style, did not play well with idle-highlight-mode, and was not a derived mode of prog-mode. The mooz fork fixed them and then those changes were incorporated back to the original js2-mode. I don’t know which of the two the GNU ELPA one is based on but it also has those changes.

My setup recommendation for js2-mode

0. Enabling js2-mode or js2-minor-mode

If you want all js files to be open in js2-mode instead of the Emacs built-in js-mode (formerly known as espresso-mode), add this line:

(add-to-list 'auto-mode-alist (cons (rx ".js" eos) 'js2-mode))

Instead of using js2-mode as a JavaScript major mode, you could keep using the built-in js-mode and use that with js2-minor-mode to enable js2-mode’s syntax checking:

(add-hook 'js-mode-hook 'js2-minor-mode)

Latest release note from original js2-mode (as of 2013 March) says

This is the first major new release for js2-mode in three years. It incorporates several sets of changes:

* changes and fixes from Mooz’s github fork
* changes suggested by Stefan Monnier and the emacs maintainers
changes I made to allow js2 to be used as a minor mode

js2-mode is really best for its linting; its major-mode facilities are a bit weak. So I’ve added js2-minor-mode, with instructions in the file for how to enable it. You can use it in conjunction with your favorite JavaScript major mode (such as the one that is now bundled with Emacs 24), and get the best of both worlds.

Work is continuing on js2-mode by various contributors, but there is currently not a “canonical” version that I’m aware of — that is, the GitHub forks are not(?) directly contributing back to the ELPA version, nor vice-versa. So I will periodically (perhaps every 6 months) hand-merge them and upload the results here after it’s been tested by enough dogfooders.

This latest version is also the head SVN revision if you want to grab the source that way. The internal elisp version number, `js2-mode-version’, is 20120726.

1. Run prog-mode-hook in js2-mode. (deprecated recommendation that now only applies to old js2-mode)

For some reason, old js2-mode is not a derived mode of prog-mode when it should be. prog-mode is available from Emacs 24 on. Here’s an excerpt from Emacs 24 NEWS:

`prog-mode’ is a new major mode from which programming modes
should be derived.

`prog-mode-hook’ can be used to enable features for programming
modes, e.g. (add-hook ‘prog-mode-hook ‘flyspell-prog-mode) to enable
on-the-fly spell checking for comments and strings.

To ensure that prog-mode-hook runs in js2-mode, add:

;; add buffer-local indicator for whether prog-mode-hook has run.
(defun my-set-pmh-ran ()
  (set (make-local-variable 'my-pmh-ran) t))

(add-hook 'prog-mode-hook 'my-set-pmh-ran)

(add-hook 'js2-mode-hook 'my-run-pmh-if-not-ran)
(defun my-run-pmh-if-not-ran ()
  (unless (bound-and-true-p my-pmh-ran)
    (run-hooks 'prog-mode-hook)))

The reason I’ve added the check to see whether prog-mode-hook is run is that you don’t want to end up running prog-mode-hook twice when you upgrade to the latest version of js2-mode.

2. Turn off js2 mirror mode and use autopair mode instead.

3. Make js2-mode use spaces instead of tabs if that’s what you prefer. (deprecated recommendation that now only applies to old js2-mode)

(add-hook 'js2-mode-hook 'my-disable-indent-tabs-mode)
(defun my-disable-indent-tabs-mode ()
  (set-variable 'indent-tabs-mode nil))

(Update: It seems TAB now inserts spaces even when indent-tabs-mode is on.)

4. Etc

(eval-after-load "js2-mode"
  '(progn
     (setq js2-missing-semi-one-line-override t)
     (setq-default js2-basic-offset 2) ; 2 spaces for indentation (if you prefer 2 spaces instead of default 4 spaces for tab)

     ;; add from jslint global variable declarations to js2-mode globals list
     ;; modified from one in http://www.emacswiki.org/emacs/Js2Mode
     (defun my-add-jslint-declarations ()
       (when (> (buffer-size) 0)
         (let ((btext (replace-regexp-in-string
                       (rx ":" (* " ") "true") " "
                       (replace-regexp-in-string
                        (rx (+ (char "\n\t\r "))) " "
                        ;; only scans first 1000 characters
                        (save-restriction (widen) (buffer-substring-no-properties (point-min) (min (1+ 1000) (point-max)))) t t))))
           (mapc (apply-partially 'add-to-list 'js2-additional-externs)
                 (split-string
                  (if (string-match (rx "/*" (* " ") "global" (* " ") (group (*? nonl)) (* " ") "*/") btext)
                      (match-string-no-properties 1 btext) "")
                  (rx (* " ") "," (* " ")) t))
           )))
     (add-hook 'js2-post-parse-callbacks 'my-add-jslint-declarations)))

js2-parse-global-vars-decls makes js2-mode recognize JsLint global variables directives such as:

/*global console, jQuery, $, _, Backbone */

5. M-x customize-group RET js2-mode

Posted in Emacs | Tagged , | 2 Comments

using autopair mode in js2-mode

(Update @ 2013 March: This article is now deprecated. With the latest js2-mode from GNU ELPA, autopair works fine with js2-mode without any tweaks. Old js2-mode’s own pairing mode js2-mirror-mode is gone. But now Emacs 24 includes electric-pair-mode which is another pairing mode other than autopair. electric-pair-mode is turned off by default. As of 2013 March, autopair has more features than electric-pair-mode. See my js2-mode setup recommendation and how to install autopair.)

js2-mode already does some of what autopair mode does. When both autopair-mode and js2-mode are turned on in a JavaScript buffer, the two modes seem to conflict. For example, inserting an open parenthesis results in two closing parentheses being auto-inserted. There are two ways to resolve this.

1. Turning off autopair mode in js2-mode

(eval-after-load "autopair"
  '(progn
     (autopair-global-mode 1)

     (setq my-autopair-off-modes '(js2-mode))
     (dolist (m my-autopair-off-modes)
       (add-hook (intern (concat (symbol-name m) "-hook"))
                 #'(lambda () (setq autopair-dont-activate t))))
     ))

With the code above, you can turn on autopair-global-mode while using js2-mode’s own autopairing feature in js2-mode buffers.

2. Turning off js2-mode’s own autopairing feature and using autopair mode in js2-mode buffers.

If you prefer to use autopair mode in js2-mode buffers like me, use the following code.

(eval-after-load "autopair"
  '(progn
     (autopair-global-mode 1)))

(eval-after-load "js2-mode"
  '(progn
     (setq js2-mirror-mode nil)))

Following is a minimal dotemacs setup example for autopair and js2-mode.

;; the whole content of the minimal dotemacs file.

(eval-after-load "autopair-autoloads"
  '(progn
     (require 'autopair)))

(eval-after-load "autopair"
  '(progn
     (autopair-global-mode 1)

     (setq my-autopair-off-modes '(
                                   ;; js2-mode
                                   ))
     (dolist (m my-autopair-off-modes)
       (add-hook (intern (concat (symbol-name m) "-hook"))
                 #'(lambda () (setq autopair-dont-activate t))))
     ))

(eval-after-load "js2-mode"
  '(progn
     (if (and (boundp 'my-autopair-off-modes)
              (not (memq 'js2-mode my-autopair-off-modes)))
         (setq js2-mirror-mode nil))
     ))

(require 'package)
(package-initialize)
Posted in Emacs | Tagged , , , | 1 Comment

Install and use the mooz fork of js2-mode

Update (2013 March) : Consider this article deprecated. The ELPA version of js2-mode now has features of mooz’s fork. Do M-x list-packages RET and then install js2-mode from there. See my js2-mode setup recommendations.

Steve Yegge released a JavaScript mode for Emacs called js2-mode in 2008. Its selling point over other JavaScript modes was its built-in JavaScript parser, syntax error detection and indentation based on the parser. Its biggest con was that it enforced an unpopular indentation style. But there’s a fork fixing that:

mooz/js2-mode · GitHub

If you want to try js2-mode, you should use the mooz fork instead of the original js2-mode. The link to the original js2-mode follows:

js2-mode – Enhanced JavaScript IDE Emacs Mode – Google Project Hosting

The js2-mode included in the default emacs package archive is Steve Yegge’s js2-mode version 20090814 as of now (2012 March). So if you installed that js2-mode package, you will have to either disable or override the package in order to use the mooz fork version instead.

Installation of the mooz fork:

1. Download and decompress the ZIP file linked from the GitHub page for the mooz fork. Move the decompressed directory to where you like it to be.

direct link to the ZIP file

2. Open the file js2-mode.el in Emacs and do M-x byte-compile-file RET to byte compile the el file.

3. With the js2-mode.el buffer open, do C-h v default-directory RET and copy the directory path shown.

4. Add the following code to your dotemacs file. Replace "PATH-TO-FORK-DIR" with the directory path you have copied.

(add-to-list 'load-path "PATH-TO-FORK-DIR")
(autoload 'js2-mode "js2-mode" nil t)
(add-to-list 'auto-mode-alist '("\\.js$" . js2-mode))

5. To check if the installation was successful, restart Emacs, open a new JavaScript file. Press C-h m and you should see the following line:

JavaScript-IDE mode defined in `js2-mode.el'

Now write the following:

var chunky = 1,
bacon = 2;
setTimeout(function(){
window.alert(chunky + bacon);
}, 3000);

Select the whole region in the buffer and press C-M-\ (or M-x indent-region) or TAB (or M-x indent-for-tab-command) to see the code indented as:

var chunky = 1,
    bacon = 2;
setTimeout(function(){
    window.alert(chunky + bacon);
}, 3000);

If the code gets indended like that, then it means that the fork version is successfully loaded. If you were using the original js2-mode, the code would be indented as:

var chunky = 1,
bacon = 2;
setTimeout(function(){
               window.alert(chunky + bacon);
           }, 3000);

If that’s what you prefer, the fork version lets you have that by setting js2-consistent-level-indent-inner-bracket-p and js2-pretty-multiline-decl-indentation-p to nil. Enter M-x customize-group RET js2-mode to customizer further.

Installation troubles:

1. If you followed the installation steps and opened a new JavaScript file only to see the buffer still getting associated with some other JavaScript mode, the culprit is probably nXHTML. In that case, you can add:

/* -*- mode:js2 -*- */

as the first line of the JavaScript file you want to edit in js2-mode. Here is a quote from the comment section in js2-mode.el:

This mode does not yet work with “multi-mode” modes such as `mmm-mode’ and `mumamo’, although it could be made to do so with some effort. This means that `js2-mode’ is currently only useful for editing JavaScript files, and not for editing JavaScript within tags or templates.

Does anybody know a fork of js2-mode that works with “multi-mode” modes?

2. If you are using a package that depends on the js2-mode package, you cannot disable the js2-mode package and JavaScript files may open in the Steve Yegge’s original js2-mode instead of the fork version you have installed. M-x locate-library RET js2-mode shows which js2-mode is going to be loaded. In order for the fork version to load, the path to the directory containing the fork version should come before that of the package version in load-path. One way to ensure that is to make sure that the line

(add-to-list 'load-path "PATH-TO-FORK-DIR")

executes only after (package-initialize) is run. If you are one of those people who prefer to put the line (package-initialize) at the end, not the beginning, of dotemacs, you can put the following anywhere in dotemacs:

;; got from esk-eval-after-init
(defun my-eval-after-init (form)
  (let ((func (list 'lambda nil form)))
    (add-hook 'after-init-hook func)
    (when after-init-time
      (eval form))))

(let ((path "PATH-TO-FORK-DIR"))
  (when (file-exists-p path)
    (setq mooz-js2-mode-directory path)
    (my-eval-after-init
     '(add-to-list 'load-path mooz-js2-mode-directory))))
Posted in Emacs | Tagged , | 3 Comments

lexical scoping and dynamic scoping in Emacs Lisp

In this article, I demonstrate:

  1. difference between dynamic scoping and lexical scoping in Emacs Lisp
  2. what to watch out for with dynamic scoping
  3. what you can do with lexical scoping and lexical closures
  4. what happens when you mix lexical scoping code and dynamic scoping code

Emacs Lisp is always dynamically scoped in Emacs 23 and below. Support for lexical scoping is added to Emacs 24. Nice because many agree that lexical scoping makes more sense in most cases than dynamic scoping does. You’ll see why soon in this article. If you have an el file that you want to load with lexical scoping, you can add -*- lexical-binding: t -*- as the first line, then when Emacs 24 loads the file, it will apply lexical scoping to the code in that el file. For example, the first line of my current init file is

;; -*- coding: utf-8 -*-

and if I change that line to

;; -*- coding: utf-8; lexical-binding: t -*-

then the code in my init file will be lexically scoped in Emacs 24. See file variables.

To experiment with lexical scoping, first create an empty el file (C-x C-f lexical-scratch.el RET) and add this line:

;; -*- lexical-binding: t -*-

and save it, and then revert the buffer (M-x revert-buffer). Now you can use the buffer as a sort of scratch buffer with lexical scoping on.

What are dynamic scoping and lexical scoping? Let’s take a look at a simple example code.

(setq a 17)
(defun my-print-a ()
  (print a))
(setq a 1717)
(let ((a 8))
  (my-print-a))

Notice that the value of a is not specified within my-print-a, making it what some call a “free variable“. What will be the result of running the code above? Will it print 1717? Or is it going to be 8? With dynamic scoping, it prints 8. With lexical scoping, it prints 1717. With dynamic scoping, what the name a in my-print-a refers to is determined by when my-print-a is called. With lexical scoping, it is determined by where my-print-a is defined.

With dynamic scoping, the code prints 8 because by the time my-print-a is called, we’re are in the let form which locally binds a to 8. If you call my-print-a after the let form, it will print 1717.

With lexical scoping, the code prints 1717 because, first, where my-print-a is defined is outside of the let form, so a in my-print-a refers to the global binding for a (binding or associating the name a to a memory location), not the local binding created by the let form, and second, by the time my-print-a is called, the global value of a is 1717, which is separate from the local value of a being 8. If you move the definition of my-print-a into the let form, the printed value will be 8 because then a in my-print-a will refer to the local binding for a created by the let form.

If you know JavaScript, an equivalent code in JavaScript is

var a;
a = 17;
function myPrintA() {
  console.log(a);
}
a = 1717;
(function () {
  var a = 8;
  myPrintA();
}());

That will print 1717. Most programming languages are lexically scoped these days.

If you are using Emacs 24, you can test if my example in lexical scoping actually prints 1717 by running the following code in the scratch buffer.

(eval
 '(progn
    (setq a 17)
    (defun my-print-a ()
      (print a))
    (setq a 1717)
    (let ((a 8))
      (my-print-a)))
 t)

The function eval in Emacs 24 takes a second argument (optional), and if that is t, eval evaluates using lexical scoping. Don’t forget the quote ' in front of (progn ...).

Lexical scoping makes lexical closures possible. What are lexical closures? Let’s see with the following code.

(setq a 0)
(let ((a 17))
  (defun my-print-a ()
    (print a))
  (setq a 1717))
(let ((a 8))
  (my-print-a))

With lexical scoping, above prints 1717. Here’s what Alice thought about above:

At first, that’s not strange. But if you look at that code again, something’s strange. At first, I thought “This is lexical scoping, so the name a in the body of my-print-a refers to the local binding for a created by the first let form. So 1717 should be printed” but then I looked again. By the time my-print-a is called, that local binding created by the first let form is supposed to have expired! You never drink expired milk! Why is 1717 printed instead of “Sorry, I don’t exist anymore.”? Why is lexical scope resolution of a working without error even when it shouldn’t?

The first local binding for a somehow survives even after the first let form is exited and waits for my-print-a to access it. The first local binding for a expired for all purposes except for my-print-a‘s access. That must mean that Emacs manages things behind so that lexical scoping works even better than it “should”.

So what is a lexical closure? This relates to how “lexical scoping working even better” is implemented behind the scenes. The function cell of my-print-a contains a link to the relevant expired binding for a, as you can see by evaluating (symbol-function 'my-print-a). This combination of the function definition and the link to the scope at the time the function was created is called a lexical closure. Or you can call any lexically scoped function accessing an expired binding a lexical closure. Lexical closures are often simply called closures. Not all lexically scoped languages support closures.

In lexical scoping, when you want to see what a variable in a function body refers to, you just look around where the function body is placed in the code text and find the relevant binding. That’s why lexical scoping is easy to wrap our heads around, because all we have to do to is look around where the variable is written in the code text, and we don’t even have to worry about when the relevant binding expires.

Anyway, an equivalent code in JavaScript:

var a, myPrintA;
a = 0;
(function () {
  // local variable a
  var a = 17;
  myPrintA = function () {
    console.log(a);
  };
  a = 1717;
}());
(function () {
  // local variable a
  var a = 8;
  myPrintA();
}());

That will print 1717 because JavaScript supports lexical closures.

In Emacs 24, lexically scoped (interpreted) functions are represented by a form of function value that looks like (closure ENV ARGS BODY...) while dynamically scoped functions are represented by a form of function value that looks like (lambda ARGS BODY...), the same form you use to write an anonymous function in Emacs Lisp. The following code prints (lambda (x y) (+ x y)) twice in dynamic scoping.

(defun my-sum (x y)
  (+ x y))
;; print the contents of function cell of my-sum
(print (symbol-function 'my-sum))
;; print an anonymous function
(print (lambda (x y) (+ x y)))

That prints (closure (t) (x y) (+ x y)) twice in lexical scoping. It seems that (lambda ...) evaluates to itself in dynamic scoping, while it evaluates to (closure ...) in lexical scoping.

Now onto the nesting. In lexical scoping, when function A defines function B and function B defines function C and function C prints a, what that a should refer to is first searched within C, and if not found, then search continues within B (which is where C is defined), and so on.

In the case of dynamic scoping, let’s say we have a function named my-func1 that calls another function my-func2 that calls my-func3 that prints a. Say my-func2 locally sets a to 2 when calling my-func3. What happens when we call my-func1 in dynamic scoping? It prints 2. What if we call my-func1 in an environment where a is 1? It still prints 2 instead of 1. Test with the following code.

(defun my-func1 ()
  (my-func2))
(defun my-func2 ()
  (let ((a 2))
    (my-func3)))
(defun my-func3 ()
  (print a))
(let ((a 1))
  (my-func1))

What’s happening is that while a local binding for a to 1 is active, my-func1 is called, then my-func1 calls my-func2, going deeper. my-func2 establishes another local binding for a which shadows the former binding for a to 1. At that point, it’s as if we are in the spot X in (let ((a 1)) (let ((a 2)) X )). It’s at that point that my-func3 is called. So 2 is printed.

There is one nasty gotcha you should know about dynamic scoping. Let’s say you want to use a function that takes a function as an argument. Let me give you a simple example of such a function.

(defun my-call (f n)
  (funcall f n))

(my-call #'1+ 5) ; => 6
(my-call #'oddp 5) ; => t

(dolist (i (list 1 2 3))
  (print
   (my-call (lambda (x) (* i x)) 5))) ; prints 5 10 15

Nothing surprising so far. Here we go.

(dolist (n (list 1 2 3))
  (print
   (my-call (lambda (x) (* n x)) 5))) ; prints 25 25 25 in dynamic scoping.

What’s going on? Why is it doing that? The problem is that the name n used in (lambda (x) (* n x)) is also one of the argument names of my-call. The anonymous function (lambda (x) (* n x)) is called inside my-call where n, as an argument, is bound to 5. In lexical scoping, the above code prints 5 10 15 as expected.

Gotcha 1 – Passing a dynamically scoped function as an argument to another function can get you!

Another gotcha. Try to define a function that takes two functions f and g and returns a composed function that is equivalent to applying g first and then f.

;; in dynamic scoping
(defun my-compose (f g)
  (lambda (x)
    (funcall f (funcall g x))))

(funcall
 (my-compose (lambda (n) (+ n 3)) (lambda (n) (+ n 20)))
 100) ; results in error, Lisp error: (void-variable f)

The error says f is not defined. Why? The composed function is created in my-compose, but is called in a different place where f and g are not bound. Again, in lexical scoping, the above code works as you expect.

Gotcha 2 – Using a function returned from a dynamically scoped function can get you.

In Emacs 24, defvar creates things called special variables. Special variables are dynamically scoped variables that will be bound dynamically even in lexically scoped functions. case-fold-search is an example of a special variable. Case sensitivity of the function search-forward depends on the value of the special variable case-fold-search. (search-forward "hello") matches HELLO when case-fold-search is t, while it doesn’t when case-fold-search is nil. Let’s say you define your own function my-search-forward maybe with some additional features in your lexically scoped el file, and my-search-forward also uses case-fold-search to decide case sensitivity. Because case-fold-search is a special variable, when you call

(let ((case-fold-search t))
  (my-search-forward "hello"))

you can be certain that the search will be case insensitive.

You can use the function special-variable-p to check if a variable is special.

(special-variable-p 'print-level) ; => t
(special-variable-p 'print-length) ; => t
(special-variable-p 'debug-on-error) ; => t
(special-variable-p 'debug-on-quit) ; => t

Special variables can be useful. gsg on reddit said:

Dynamic scope allows you to parameterise code without having to pass an explicit parameter. It’s not a good default, but some kinds of code do benefit from it.

kragensitaker said:

Thread-local variables, exception handlers, the current locale, and the current clipping region and image transform are some examples of things that it makes sense to scope dynamically.

Now let’s see what we can do with lexical closures.

Run the following code in lexical scoping.

(let (c)
  (defun my-get-c ()
    c)
  (defun my-set-c (new-c)
    (setq c new-c))
  (defun my-add-to-c (x)
    (setq c (+ x c))))

Then run the following code that use the three functions. The result is the same whether you run it with lexical scoping or not, because lexically scoped functions called in a dynamically scoped environment are still lexically scoped functions.

(my-set-c 10)
(my-add-to-c 5)
(print (my-get-c)) ; prints 15.
(my-add-to-c 1)
(print (my-get-c)) ; prints 16
(let ((c 0))
  (print c) ; prints 0
  (print (my-get-c))) ; prints 16.

The binding for c shared by my-get-c, my-set-c, and my-add-to-c acts like a sort of a private variable and is independent of other bindings of the name c such as one in the (let ((c 0)) ...) part. The reason this works is because the binding for c created by the let form surrounding the three defun forms has expired for all purposes except for the three functions’ access.

Now let’s test using lexical closures to do what static variables in C do.

(require 'cl) ; for incf
(eval
 '(let ((i 0))
    (defun my-counter ()
      (prog1
          i
        (incf i))))
 t)
(my-counter) ; => 0
(my-counter) ; => 1
(my-counter) ; => 2
(let ((i 10))
  (my-counter)) ; => 3
(my-counter) ; => 4

For those confused as to why the above code works that way, here is a demonstrative example code.

(eval
 '(let ((i1 0))
    (defun my-test ()
      (let ((i2 0))
        (prog1
            (list i1 i2)
          (incf i1)
          (incf i2)))))
 t)
(my-test) ; => (0 0)
(my-test) ; => (1 0)
(my-test) ; => (2 0)

my-test is defined and then it’s called three times. The let form (let ((i2 0)) ..) in my-test was executed three times my-test was called. On the other hand, the let form (let ((i1 0)) ... ) was executed once and that was when my-test was defined. I hope that helps.

Now let’s test a function that returns functions that are lexical closures.

(eval
 '(defun my-get-counter (start step)
    (let ((count start))
      (lambda ()
        (prog1
            count
          (setq count (+ count step)))))
    )
 t)

(setq my-get-even-numbers (my-get-counter 0 2)
      my-get-odd-numbers (my-get-counter 1 2))

(funcall my-get-even-numbers) ; => 0
(funcall my-get-even-numbers) ; => 2
(funcall my-get-even-numbers) ; => 4

(funcall my-get-odd-numbers) ; => 1
(funcall my-get-odd-numbers) ; => 3
(funcall my-get-odd-numbers) ; => 5

(funcall my-get-even-numbers) ; => 6
(funcall my-get-even-numbers) ; => 8

(setq my-get-even-numbers-2 (my-get-counter 0 2))
(funcall my-get-even-numbers-2) ; => 0
(funcall my-get-even-numbers-2) ; => 2
(funcall my-get-even-numbers-2) ; => 4

(funcall my-get-even-numbers) ; => 10
(funcall my-get-even-numbers) ; => 12
(funcall my-get-even-numbers) ; => 14

You might be wondering why my-get-even-numbers, my-get-odd-numbers and my-get-even-numbers-2 seem to have their own count instead of sharing a single count. They actually have their own count. If you are confused, what if you run the following code with lexical scoping?

(let ((count 0))
  (setq my-count
        (lambda ()
          (prog1
              count
            (setq count (1+ count))))))
(let ((count 0))
  (setq my-count-2
        (lambda ()
          (prog1
              count
            (setq count (1+ count))))))

my-count and my-count-2 have their own count. Each of the two let forms enclose each of the two (setq .. (lambda ...)) forms. That’s actually similar to what’s going on with my-get-counter. Each time (my-get-counter ..) is executed, (let ((count ..)) (lambda ..)) is executed again, each creating a new separate binding for count that each new returned function can access. When you execute (my-get-counter ..) three times, (let ((count ..)) (lambda ..)) is executed three times, creating three bindings of count and three returned functions.

Alice now writes all of her new Emacs Lisp code in lexically scoped el files. When lexically scoped new code written by Alice and dynamically scoped old code written by others interact, what will happen? Will things break?

Let’s start with a simple example.

(eval
 '(defun my-bah ())
 t)

(eval
 '(fset 'my-bah-2 (symbol-function 'my-bah))
 nil)

The function my-bah is defined in a lexically scoped environment. So it must be a lexically scoped function. What about my-bah-2? Alice says “The function my-bah-2 is defined in a dynamically scoped environment. So it must be a dynamically scoped function.” On the other hand, Bob says “What is in the function cell of my-bah is copied to the function cell of my-bah-2. The function cell of my-bah contains a lexically scoped function. What is in the function cell of my-bah-2 should be the same lexically scoped function.” Alice says “Wait. These functions do nothing. Let’s make them do something. Let’s make them tell us whether they are lexically scoped by their return values.” The following code returns t in a lexically scoped environment, nil otherwise. Checking the value of lexical-binding instead here is a bad idea.

(let ((x nil)
      (f (let ((x t)) (lambda () x))))
  (funcall f))

Alice modifies the my-bah & my-bah-2 code.

(eval
 '(defun my-bah ()
    (let ((x nil)
          (f (let ((x t)) (lambda () x))))
      (funcall f)))
 t)

(eval
 '(fset 'my-bah-2 (symbol-function 'my-bah))
 nil)

Let’s see if my-bah-2 is a lexically scoped function.

(my-bah) ; => t
(my-bah-2) ; => t

So Bob guessed right? Let’s test a similar code that does not use defun.

(eval
 '(setq my-nah
        (lambda ()
          (let ((x nil)
                (f (let ((x t)) (lambda () x))))
            (funcall f))))
 t)

(eval
 '(setq my-nah-2 my-nah)
 nil)

(funcall my-nah) ; => t
(funcall my-nah-2) ; => t

When you run (setq abc (+ 1 1)), the expression (+ 1 1) describing a sum is evaluated first, and then the evaluation result 2, a number, is assigned to the variable abc. Likewise, when you run (setq my-nah (lambda ...)), the expression (lambda ...) describing an anonymous function is evaluated first. In lexical scoping, the evaluation result is something that looks like (closure ....), a lexically scoped function value. Then that result (closure ....) is assigned to the variable my-nah.

When you run (setq abc (+ 1 1)) and then run (setq abc-2 abc), evaluation of the expression (+ 1 1) happens only once. The statement (setq abc-2 abc) does not evaluate (+ 1 1) again, it just saves the already computed result 2 to abc-2. What it does evaluate is the symbol abc itself, and the symbol abc evaluates to 2. Likewise, in the my-nah & my-nah-2 example code, evaluation of the expression (lambda ...) happens only once and the result (closure ...) is not evaluated when you run (setq my-nah-2 my-nah), it is simply saved to my-nah-2. Even though (setq my-nah-2 my-nah) is run in a dynamically scoped environment, because evaluation of the anonymous function expression happens in a lexically scoped environment, the variable my-nah-2 ends up holding a lexically scoped function.

A lexically scoped function is created and it gets passed around in a dynamically scoped environment, and the function remains a lexically scoped function.

The defun my-bah example is similar. The function cell of the symbol my-bah holds a lexically scoped function, which simply gets passed around. Check with the following test.

(print my-nah-2)
(print (symbol-function 'my-bah-2))

So when you have a defun in a lexically scoped el file, to see the meaning of free variables names in it, you just look around them in the el file, regardless of whether that function gets another name in a dynamically scoped file.

Now that my-nah-2 & my-bah-2 example is understood, let’s revisit my-get-counter. As long as (defun my-get-counter ...) is in a lexically scoped el file, functions returned by my-get-counter are lexically scoped. Let’s see.

(eval
 '(progn
    (setq my-get-even-numbers (my-get-counter 0 2))
    (print (funcall my-get-even-numbers))
    (print (funcall my-get-even-numbers))
    (print (funcall my-get-even-numbers)))
 nil)

That prints 0 2 4. Alice’s argument repeated here would be like “The function my-get-even-numbers is defined in a dynamically scoped environment. So why is it acting like a lexically scoped function?” The variable my-get-even-numbers ends up holding a lexically scoped function for the same reason my-nah-2 does. In case you are confused, let’s get our head around my-get-sum first.

(defun my-get-sum (x y)
  (+ x y))

(+ x y) in my-get-sum is an expression describing a sum and my-get-sum returns the result of evaluation of (+ x y), not the expression (+ x y) itself. When you run (my-get-sum 1 2), it does not return the literal expression (+ x y), it returns 3, which is what (+ x y) evaluated to inside my-get-sum.

Back to my-get-counter. (lambda ...) in my-get-counter is an expression describing an anonymous function. That expression is evaluated once inside my-get-counter. The result of its evaluation is something that looks like (closure ...) which is immediately returned and gets stored in the variable my-get-even-numbers. Evaluation of the (lambda ...) happens only once and that happens inside the lexically scoped function my-get-counter. Evaluation of a lambda form inside a lexically scoped function always results in (closure ...). That is how my-get-even-numbers ends up holding a lexically scoped function.

By the way, lexically scoped functions can create and return a dynamically scoped function if the evaluation of a lambda form is somehow avoided maybe unintentionally.

(eval
 '(defun my-return-dynamically-scoped-function ()
    (list 'lambda '() 'a)
    )
 t)

(eval
 '(defun my-return-dynamically-scoped-function ()
    '(lambda () a) ; quoted lambda
    )
 t)

I don’t know why anybody would do that intentionally, but it can be done.

Now let’s revisit the my-call example.

(eval
 '(defun my-call (f n)
    (funcall f n))
 nil)

(eval
 '(dolist (n (list 1 2 3))
    (print
     (my-call (lambda (x) (* n x)) 5)))
 t)

That prints 5 10 15. Alice argument repeated would be “The function f is defined in a dynamically scoped environment. So why is it acting like a lexically scoped function?”. The anonymous functions to be passed to my-call are defined in a lexically scoped environment, so it stays as a lexically scoped function even after it is passed to my-call. In case you are still confused, the (lambda ...) is evaluated and then its result is passed to my-call. my-call stores the result to its local variable f. So f ends up referring to a lexically scoped function.

The function mapcar* is like my-call in that it accepts a function as an argument and is defined in a dynamically scoped el file (for now). The following dynamic scoping gotcha example is from some StackOverflow answer.

(let ((cl-x 10))
  (mapcar* (lambda (elt) (* cl-x elt)) '(1 2 3)))

The name cl-x is also used as an argument name in the definition of mapcar*. So running the code above in a dynamically scoped environment leads to a surprise (Gotcha 1). But when you run the code in a lexically scoped environment, it works fine, because lexically scoped anonymous functions passed to mapcar* stays as lexically scoped functions.

Judging by these examples, it seems that lexically scoped code blend in well. Time to go forth and enjoy lexical scoping!

Posted in Emacs | Tagged , , , , | 13 Comments