Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/jingtaozf/literate-elisp
Load Emacs Lisp code blocks from Org files
https://github.com/jingtaozf/literate-elisp
elisp emacs literate literate-programs org org-mode
Last synced: about 2 months ago
JSON representation
Load Emacs Lisp code blocks from Org files
- Host: GitHub
- URL: https://github.com/jingtaozf/literate-elisp
- Owner: jingtaozf
- Created: 2018-11-18T07:01:17.000Z (about 6 years ago)
- Default Branch: master
- Last Pushed: 2023-05-26T23:40:26.000Z (over 1 year ago)
- Last Synced: 2024-11-22T21:46:16.710Z (2 months ago)
- Topics: elisp, emacs, literate, literate-programs, org, org-mode
- Language: Emacs Lisp
- Homepage:
- Size: 2.31 MB
- Stars: 45
- Watchers: 3
- Forks: 6
- Open Issues: 3
-
Metadata Files:
- Readme: readme.org
Awesome Lists containing this project
README
# -*- encoding:utf-8 Mode: POLY-ORG; -*- ---
#+Startup: noindent
#+PROPERTY: header-args :results silent :eval no-export :comments org
#+OPTIONS: num:nil toc:nil todo:nil tasks:nil tags:nil
#+OPTIONS: skip:nil author:nil email:nil creator:nil timestamp:t
#+INFOJS_OPT: view:nil toc:nil ltoc:t mouse:underline buttons:0 path:http://orgmode.org/org-info.js
#+PROPERTY: literate-lang elisp
#+PROPERTY: literate-load yes[[https://melpa.org/#/literate-elisp][file:https://melpa.org/packages/literate-elisp-badge.svg]]
[[https://stable.melpa.org/#/literate-elisp][file:https://stable.melpa.org/packages/literate-elisp-badge.svg]]
[[https://travis-ci.com/jingtaozf/literate-elisp][file:https://travis-ci.com/jingtaozf/literate-elisp.svg?branch=master]]
[[https://github.com/jingtaozf/literate-elisp/actions][file:https://github.com/jingtaozf/literate-elisp/workflows/Continous%20Integration/badge.svg]]* Table of Contents :TOC:
- [[#introduction][Introduction]]
- [[#tutorial][Tutorial]]
- [[#install-polymode-in-emacs][install Polymode in Emacs]]
- [[#install-literate-elisp][install literate-elisp]]
- [[#how-to-insert-code-block-quickly][how to insert code block quickly]]
- [[#load-an-org-file][load an org file]]
- [[#byte-compile-an-org-file][byte compile an org file]]
- [[#a-new-code-block-header-argument-load][a new code block header argument ~load~]]
- [[#compatibility-with-other-libraries][compatibility with other libraries]]
- [[#support-for-emacs-lisp-refs][support for Emacs Lisp-Refs]]
- [[#support-for-library-helpful][support for library helpful]]
- [[#faq][FAQ]]
- [[#what-can-i-do-when-it-failed-to-load-some-file][What can I do when it failed to load some file]]
- [[#can-i-use-autoload-for-a-elisp-routine-in-an-org-file][Can I use =autoload= for a Elisp routine in an org file?]]
- [[#demo-routines][demo routines]]
- [[#a-demo-macro][a demo macro]]
- [[#a-demo-emacs-configuration][a demo Emacs configuration]]
- [[#a-demo-configuration][a demo configuration]]
- [[#enable-org-mode-for-org-files][enable org mode for org files]]
- [[#load-this-org-file-in-emacs][load this org file in .emacs]]
- [[#packages-written-by-literate-elisp][packages written by literate-elisp]]
- [[#test][Test]]
- [[#introduction-1][Introduction]]
- [[#test-macro-aif][test macro aif]]
- [[#test-org-mode-configuration][test org mode configuration]]
- [[#copyright-and-license][Copyright and license]]* Introduction
[[https://github.com/jingtaozf/literate-elisp][literate-elisp]] is an Emacs lisp library to provide an easy way to use [[http://www.literateprogramming.com/][literal programming]] in Emacs lisp.It extends the Emacs [[https://www.gnu.org/software/emacs/manual/html_node/elisp/How-Programs-Do-Loading.html#How-Programs-Do-Loading][load]] mechanism so Emacs can load [[https://orgmode.org/manual/Working-with-source-code.html#Working-with-source-code][org files]] as lisp source files directly.
The implementation details of [[https://github.com/jingtaozf/literate-elisp][literate-elisp]] is in file [[./literate-elisp.org]] ([[./literate-elisp.pdf][pdf version]]).
This library export an Emacs Lisp function ~literate-elisp-load~
and this function can load Emacs Lisp codes in all Emacs Lisp code blocks
surrounded by ~#+begin_src elisp~ and ~#+end_src~ in an org file directly.For example, if you have an org file and it contains such Emacs Lisp code block:
#+BEGIN_EXAMPLE
,#+BEGIN_SRC elisp :load yes
(defun test ()
(message "this is a test from org file.~%"))
,#+END_SRC
#+END_EXAMPLEThen you can load this org file like this:
#+BEGIN_SRC elisp :load no
(load "~/projects/literate-elisp/literate-elisp.el")
(literate-elisp-load "test.org")
#+END_SRC
Now the Emacs Lisp function ~test~ is ready to use,
and you can jump to the definition of ~test~ in the org file by using Emacs library [[https://github.com/emacs-mirror/emacs/blob/master/lisp/emacs-lisp/find-func.el][find-func]] directly
without tangling them to an Emacs Lisp file(.el).You can also open an org file by [[https://polymode.github.io/][polymode]] when edit, which will switch Emacs Lisp code block to Emacs Lisp mode.
So you will have a perfect literate environment which can write Emacs Lisp codes in an org file, and
load it directly without tangling an org file to an Emacs Lisp file.
Emacs lisp's source code editing and navigation feature work well to this org file.This library contains the following files:
- [[./literate-elisp.org]] \\
The implementation and documentation of literate Emacs Lisp reader.
- [[./literate-elisp.el]] \\
The tangled codes of literate Emacs Lisp reader, generated from [[./literate-elisp.org]].
- [[./literate-elisp.pdf]] \\
The weaved documentation, generated from [[./literate-elisp.org]] by org mode's [[https://orgmode.org/manual/Triggering-publication.html#Triggering-publication][publish feature]].
- [[./readme.org]] \\
This file contains introduction and demo codes for how to do literate Emacs Lisp in an org file.
- [[./.travis.yml]] \\
The config file used by Web service [[https://travis-ci.com/jingtaozf/literate-lisp][travis ci]] to test this library.* Tutorial
** install Polymode in Emacs
The org file can open as [[https://polymode.github.io/][polymode]],the following Emacs lisp scripts should add in ~.emacs~.
#+BEGIN_SRC elisp :load no
(use-package poly-org
:ensure t)
#+END_SRC
** install literate-elisp
~literate-lisp~ is available on the two major ~package.el~ community maintained repos - [[https://stable.melpa.org/#/literate-elisp][MELPA Stable]] and [[https://melpa.org/#/literate-elisp][MELPA]].
You can install ~literate-elisp~, or more commonly a specific ~literate-elisp~, interactively
#+BEGIN_SRC text
M-x package-install [RET] literate-elisp [RET]
#+END_SRC
Or you can install it from source directly
#+BEGIN_SRC elisp :load no
(load "~/projects/literate-elisp/literate-elisp.el")
#+END_SRC** how to insert code block quickly
Please have a look of the section [[./literate-elisp.org#how-to-insert-code-block-in-org-file][How to insert code block in org file]].
** load an org fileTo load an org file, we can use Emacs interactive command ~literate-elisp-load~.
For example, This org file can load directly with the following code.
#+BEGIN_SRC elisp :load no
(literate-elisp-load "readme.org")
#+END_SRCIf you want to load an org file with a Emacs command, please press "Alt-X" and type ~literate-elisp-load-file~.
If you want to load an org file in batch mode, please use function ~literate-elisp-batch-load~.
** byte compile an org file
To byte compile an org file to an ~elc~ file, we can use Emacs interactive command ~literate-elisp-byte-compile-file~.
For example, This org file can be compiled with the following code.
#+BEGIN_SRC elisp :load no
(literate-elisp-byte-compile-file "readme.org")
#+END_SRC
Now the target file ~readme.org.elc~ is ready to use.** a new code block header argument ~load~
Source blocks in a literate program can serve a variety of
purposes—implementation, examples, testing, and so on—so we define a
~load~ [[https://orgmode.org/manual/Structure-of-code-blocks.html][Org code block]] [[https://orgmode.org/manual/Code-block-specific-header-arguments.html#Code-block-specific-header-arguments][header argument]] to decide whether to read them
or not, which accepts the following values -
- yes \\
The code block should be loaded normally.
This is the default when the header argument ~load~ is not provided.
#+BEGIN_EXAMPLE
,#+BEGIN_SRC elisp :load yes
(defun a-function-to-load ()
(message "this function will be loaded by literate-elisp.~%"))
,#+END_SRC
#+END_EXAMPLE- no \\
The code block should be ignored by the Emacs Lisp reader.
#+BEGIN_EXAMPLE
,#+BEGIN_SRC elisp :load no
(defun a-function-to-ignore ()
(message "this function will be ingored by literate-elisp.~%"))
,#+END_SRC
#+END_EXAMPLE
- test \\
The code block should be loaded only when the variable ~literate-elisp-test-p~ is true.
#+BEGIN_EXAMPLE
,#+BEGIN_SRC elisp :load test
(defun a-function-to-test ()
(message "this function will be loaded by literate-elisp only if literate-elisp-test-p is true.~%"))
,#+END_SRC
#+END_EXAMPLE
- the name of a variable or function \\
The code block is loaded if the value of the variable or the return value of the function is non-nil.
** compatibility with other libraries
We have some extension to keep compatibility with other libraries, the following sections show how to setup them.
*** support for Emacs Lisp-Refs
#+BEGIN_SRC elisp
;; ;; To make `elisp-refs' work with `literate-elisp', we need to add an advice to `elisp-refs--read-all-buffer-forms'.
(eval-after-load "elisp-refs"
'(advice-add 'elisp-refs--read-all-buffer-forms :around #'literate-elisp-refs--read-all-buffer-forms));; To make `elisp-refs' work with `literate-elisp', we need to add an advice to `elisp-refs--loaded-paths'.
(eval-after-load "elisp-refs"
'(advice-add 'elisp-refs--loaded-paths :filter-return #'literate-elisp-refs--loaded-paths))
#+END_SRC
*** support for library helpful
#+BEGIN_SRC elisp
;; To make `helpful' work with `literate-elisp', we need to add an advice to `helpful--find-by-macroexpanding'.
(with-eval-after-load 'helpful
(advice-add 'helpful--find-by-macroexpanding :around #'literate-elisp-helpful--find-by-macroexpanding))
#+END_SRC* FAQ
** What can I do when it failed to load some file
- try to use Emacs command =check-parens=.
- toggle on the debug mode of literate-elisp
#+begin_src elisp :load no
(setf literate-elisp-debug-p t)
#+end_src
** Can I use =autoload= for a Elisp routine in an org file?
Yes you can, just like the original function in a elisp source file.* demo routines
** a demo macro
As a demo org file, we write a simple demo macro ~aif~ here.Sometimes we want to use the expression value of ~if~ condition form when it yields non-nil.
That's the purpose of ~aif~ which will bind variable ~it~ to the value of ~if~ condition form.We will use some common lisp macros, so let's load this library now.
#+BEGIN_SRC elisp
(require 'cl)
#+END_SRCLet's implement ~if-bind~ firstly,
which can bind the value of ~if~ condition form to any specified variable.
#+BEGIN_SRC elisp
(defmacro if-bind (var test &rest then/else)
"Anaphoric IF control structure.VAR (a symbol) will be bound to the primary value of TEST. If
TEST returns a true value then THEN will be executed, otherwise
ELSE will be executed."
(cl-assert (car then/else)
(then/else)
"IF-BIND missing THEN clause.")
(cl-destructuring-bind (then &optional else)
then/else
`(lexical-let ((,var ,test))
(if ,var ,then ,else))))
#+END_SRCNow ~aif~ is easy to finish.
#+BEGIN_SRC elisp
(defmacro aif (test then &optional else)
"Just like IF-BIND but the var is always IT."
`(if-bind it ,test ,then ,else))
#+END_SRCYou can use it like this
#+BEGIN_SRC elisp :load no
(aif (and (y-or-n-p "Try it")
10)
(message "it is %s" it))
#+END_SRC
After loading this org file by function ~literate-elisp-load~,
you can use macro ~aif~ directly in your other Emacs Lisp files.* a demo Emacs configuration
Of course the one purpose of this library is to write Emacs configuration directly in an org file. \\
Here we give a demo configuration and the way to load such org config file.
** a demo configuration
*** enable org mode for org files
#+BEGIN_SRC elisp
(add-to-list 'auto-mode-alist '("\\.\\(org\\|org_archive\\)$" . org-mode))
#+END_SRC
** load this org file in .emacs
Then to load routines and configurations in this org file, I add the following codes in my ~.emacs~
#+BEGIN_SRC elisp :load no
(load "~/projects/literate-elisp/literate-elisp.el")
(literate-elisp-load "~/projects/literate-elisp/readme.org")
#+END_SRC
** packages written by literate-elisp
- [[https://github.com/jingtao-net/helm-q.el/blob/master/helm-q.org][helm-q]] Manage remote q sessions with Helm and q-mode
* Test
** Introduction
We use [[https://www.gnu.org/software/emacs/manual/html_node/ert/Introduction.html#Introduction][ERT]] library to define and run tests.
Web service [[https://travis-ci.com/jingtaozf/literate-lisp][travis ci]] will load config file [[./.travis.yml]] to run these tests automatically
every time there is a new git change.
** test macro aif
#+BEGIN_SRC elisp :load test
(ert-deftest literate-demo-aif ()
"A spec of macro aif."
(should (equal (aif 10 it 9) 10)))
#+END_SRC
** test org mode configuration
#+BEGIN_SRC elisp :load test
(ert-deftest literate-demo-org-mode ()
"A spec of macro aif."
(should (equal (cl-loop for (x . y) in auto-mode-alist
if (eq y 'org-mode)
return x)
"\\.\\(org\\|org_archive\\)$")))
#+END_SRC
* Copyright and license
Code and documentation copyright 2018-2019 Jingtao Xu.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 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the 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 http://www.gnu.org/licenses/.