Skip to content

Instantly share code, notes, and snippets.

Created April 9, 2024 17:22
Show Gist options
  • Save kickingvegas/de17e6640011aa6ed57a6d9505ffdbd0 to your computer and use it in GitHub Desktop.
Save kickingvegas/de17e6640011aa6ed57a6d9505ffdbd0 to your computer and use it in GitHub Desktop.
Some Elisp Questions on Changing an Alist
;;; cc-org-smart-quotes.el --- CC Org Smart Quotes Config -*- lexical-binding: t; -*-
;; Copyright (C) 2024 Charles Choi
;; Author: Charles Choi <>
;; Keywords: tools
;; This program is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;; This program is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with this program. If not, see <>.
;;; Commentary:
;;; Code:
(require 'org)
(require 'ox)
(defun cc/reconfig-org-smart-quotes-lang (lang)
"Reconfigure Org smart quotes to use utf-8 per LANG."
(let* ((db-entry (assoc-string lang org-export-smart-quotes-alist))
(utf8-primary-opening (plist-get (assoc-default 'primary-opening db-entry) :utf-8))
(utf8-primary-closing (plist-get (assoc-default 'primary-closing db-entry) :utf-8))
(utf8-secondary-opening (plist-get (assoc-default 'secondary-opening db-entry) :utf-8))
(utf8-secondary-closing (plist-get (assoc-default 'secondary-closing db-entry) :utf-8))
(utf8-apostrophe (plist-get (assoc-default 'apostrophe db-entry) :utf-8))
(setf (plist-get
(assoc-default 'primary-opening
(assoc-string lang org-export-smart-quotes-alist))
(setf (plist-get
(assoc-default 'primary-closing
(assoc-string lang org-export-smart-quotes-alist))
(setf (plist-get
(assoc-default 'secondary-opening
(assoc-string lang org-export-smart-quotes-alist))
(setf (plist-get
(assoc-default 'secondary-closing
(assoc-string lang org-export-smart-quotes-alist))
(setf (plist-get
(assoc-default 'apostrophe
(assoc-string lang org-export-smart-quotes-alist))
(provide 'cc-org-smart-quotes)
;;; cc-org-smart-quotes.el ends here
Copy link

Byte compiling this file gives this warning.

⛔ Warning (comp): cc-org-smart-quotes.el: Warning: the function ‘(setf assoc-default)’ is not known to be defined.

How do I fix this?

Copy link

kickingvegas commented Apr 9, 2024

Is setf the right function to mutate an existing alist? Looking at the alist org-export-smart-quotes-alist, it seems that setf inserts a reference as a value to the key :html to the value for the key :utf-8:

  (primary-opening :utf-8 #1="“" :html #1# :latex "``" :texinfo "``")
  (primary-closing :utf-8 #2="”" :html #2# :latex "''" :texinfo "''")
  (secondary-opening :utf-8 #3="‘" :html #3# :latex "`" :texinfo "`")
  (secondary-closing :utf-8 #4="’" :html #4# :latex "'" :texinfo "'")
  (apostrophe :utf-8 #5="’" :html #5#))

Is there a way to just set a value and not a reference?

Copy link

Is there a simpler way to access these alist values? Seems like I'm working too hard to travel the key path of "en" -> 'primary-opening -> :utf-8.

Copy link

;; Use UTF-8 smart quotes instead of HTML entities in HTML and Markdown export. This was originally inspired by the post at
;;, but the author issued a challenge at the
;; end to do it cleaner. I vaguely remembered function advice from reading the elisp manual, and it turned out that this was
;; an effective approach!
(defun dcb/make-utf8-encoding-org-export (args)
  "Override the ARGS for smartquotes to make :html encoded entities use utf-8 instead."
  (if (eq (nth 1 args) :html) ;; if the encoding is html (which both md and gfm derive from)
      (progn (setcar (nthcdr 1 args) :utf-8) ;; ...replace the encoding arg with :utf-8 to force utf-8 characters

(setq org-export-with-smart-quotes t) ;; Can be overridden with ':nil in the OPTIONS
(advice-add 'org-export-activate-smart-quotes :filter-args 'dcb/make-utf8-encoding-org-export)

Copy link

@dchenbecker Nice! That said, not sure if using advice is the "cleanest" thing as it monkey-patches behavior that could change in the future.

Copy link

That's fair. The cleanest thing would be to submit a PR to Org making this configurable, I suppose :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment