├── README.md └── org-notebook.el /README.md: -------------------------------------------------------------------------------- 1 | [![MELPA](http://melpa.org/packages/org-notebook-badge.svg)](http://melpa.org/#/org-notebook) 2 | 3 | # org-notebook 4 | 5 | Ease the use of org-mode as a notebook 6 | 7 | ### Introduction 8 | 9 | I use org-mode to take notes in class. I find it better than all those other note-taking apps like OneNote. The problem is that it was a relatively large hassle to open up an image editor and quickly draw up a diagram an input it into the org file. So I decided to make a series of functions to ease said process, and in the meantime make a package to also manage the notebook. 10 | 11 | Recently I have also added functionality to easily add HTML and LaTeX headers as a template to ease the process of exporting the org notebook to HTML or LaTeX/PDF. 12 | 13 | ### Requirements 14 | 15 | The is for Emacs, so obviously Emacs. I only tested it on 24. 16 | 17 | Also org and ido. 18 | 19 | ### Installation 20 | 21 | Install from MELPA. 22 | 23 | `M-x package-install RET org-notebook` 24 | 25 | ### Getting Started 26 | 27 | Bind a key combination to `org-notebook-insert-image`. 28 | 29 | Then when you're in your org notebook file and you want to quickly draw up a diagram and insert it, run that key combo, and it'll fire up kolourpaint and you can draw your thing, save it in the default directory, and the link will already be put in the org file. (you will need a `img/` directory if you don't already have one) 30 | 31 | Or run `org-notebook-new-notebook` from `M-x` to create a new notebook, complete with the org file with the template headers and the `img/` directory! 32 | 33 | ### Configuration 34 | 35 | - `org-notebook-drawing-program` to use a different program to draw your diagrams, such as inkscape or gimp. The default value is "kolourpaint". Tip: If you want to just insert an image link and create the image later, you can set this value to an empty string and it won't load any drawing program. 36 | 37 | - `org-notebook-image-type` to use a different image type besides png. 38 | 39 | - `org-notebook-language` to use a different language for your org notebook (mainly important for export purposes). 40 | 41 | - `org-notebook-image-width` to change the width (and therefore size) of the images displayed in org mode. The default value is 600. 42 | 43 | - `org-notebook-headers` to set the org headers template. `TITLE`, `AUTHOR`, `mode: org`, `EMAIL`, `LANGUAGE`, and `ATTR_ORG: :width` should not be included here. This is a list of cons. The default value is empty. 44 | 45 | Example value of `org-notebbok-headers`: 46 | ```elisp 47 | ( 48 | ("LATEX_CLASS_OPTIONS" "[letter,12pt]") 49 | ("LATEX_CLASS" "article") 50 | ("HTML_HEAD" "") 51 | ("HTML_HEAD" "") 52 | ) 53 | ``` 54 | 55 | ### TODO 56 | 57 | - Support multiple org files (=pages) in one notebook. 58 | - Allow per-subdirectory auto-customization of variables (drawing program, headers, language, and image type) 59 | - Check svg compatability 60 | - Make readme uniform in elisp file and markdown 61 | 62 | ### Changelog 63 | 64 | - 1.0 - Initial version, includes insert-image and new-notebook. 65 | - 1.1 - Allows templating with org headers. Also added image-width customization. 66 | - 1.2 - Smart-decide paint program and autocreate img/ directory on insert-image. 67 | 68 | ### Contributors 69 | 70 | Paul Elder 71 | 72 | ### License 73 | 74 | 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. 75 | 76 | 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. 77 | 78 | You should have received a copy of the GNU General Public License along with this program. If not, see . 79 | -------------------------------------------------------------------------------- /org-notebook.el: -------------------------------------------------------------------------------- 1 | ;;; org-notebook.el --- Ease the use of org-mode as a notebook -*- lexical-binding: t; -*- 2 | 3 | ;; Copyright (C) 2016 Paul Elder 4 | 5 | ;; Author: Paul Elder 6 | ;; Keywords: convenience, tools 7 | ;; Version: 1.1 8 | ;; Package-Requires: ((emacs "24") (org "8") (cl-lib "0.5")) 9 | 10 | ;; This program is free software; you can redistribute it and/or modify 11 | ;; it under the terms of the GNU General Public License as published by 12 | ;; the Free Software Foundation, either version 3 of the License, or 13 | ;; (at your option) any later version. 14 | 15 | ;; This program is distributed in the hope that it will be useful, 16 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of 17 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 | ;; GNU General Public License for more details. 19 | 20 | ;; You should have received a copy of the GNU General Public License 21 | ;; along with this program. If not, see . 22 | 23 | ;;; Commentary: 24 | 25 | ;; The main function is org-notebook-insert-image 26 | ;; Bind this to a convenient key combination 27 | ;; 28 | ;; There is also org-notebook-new-notebook 29 | ;; This creates a directory with the name of the 30 | ;; notebook you provide, and then creates 31 | ;; a notebook.org file and an img directory in it. 32 | ;; It populates the notebook.org file with org 33 | ;; headers, asking you for a title only since 34 | ;; the author, email, and language are extracted 35 | ;; automatically. 36 | ;; 37 | ;; For customization there is: 38 | ;; 39 | ;; org-notebook-drawing-program 40 | ;; By default this is set to kolourpaint 41 | ;; This determines what image-drawing program 42 | ;; will be launched when org-notebook-insert-image 43 | ;; is called. 44 | ;; 45 | ;; org-notebook-image-type 46 | ;; By default this is set to png 47 | ;; This determines the default filetype that 48 | ;; the drawn diagrams will be saved and linked. 49 | ;; 50 | ;; org-notebook-language 51 | ;; By default this is set to en 52 | ;; This determines the language org header that 53 | ;; will be inserted when org-notebook-new-notebook 54 | ;; is called. 55 | ;; 56 | ;; org-notebook-image-width 57 | ;; By default this is set to 600 58 | ;; This determines the image width org header that 59 | ;; will be inserted when org-notebook-new-notebook 60 | ;; is called. The header determines the width that 61 | ;; the images will be displayed in in org-mode. 62 | ;; 63 | ;; org-notebook-headers 64 | ;; By default this is an empty list 65 | ;; This is a list of cons where the first element 66 | ;; of each con is the header name (eg. LATEX_CLASS, 67 | ;; HTML_HEAD, etc) and the second element of each 68 | ;; con is the value of the header. These will be 69 | ;; inserted into the notebook org file when 70 | ;; org-notebook-new-notebook is called. 71 | 72 | ;;; Code: 73 | 74 | (require 'org) 75 | (require 'ido) 76 | (require 'cl-lib) 77 | 78 | (defgroup org-notebook nil 79 | "Ease the use of org-mode as a notebook" 80 | :group 'convenience 81 | :group 'tools 82 | :link '(emacs-library-link :tag "Lisp File" "org-notebook.el")) 83 | 84 | (defcustom org-notebook-drawing-program (cond 85 | ((executable-find "kolourpaint") "kolourpaint") 86 | ((executable-find "mypaint") "mypaint") 87 | ((executable-find "krita") "krita") 88 | ((executable-find "gimp") "gimp")) 89 | "Drawing program to be used" 90 | :type 'string 91 | :group 'org-notebook) 92 | 93 | (defcustom org-notebook-image-type "png" 94 | "Image type to be used" 95 | :type 'string 96 | :group 'org-notebook) 97 | 98 | (defcustom org-notebook-language "en" 99 | "Language that the notebook will be in, mostly just for the org header" 100 | :type 'string 101 | :group 'org-notebook) 102 | 103 | (defcustom org-notebook-image-width 600 104 | "Width of images in org" 105 | :type 'number 106 | :group 'org-notebook) 107 | 108 | (defcustom org-notebook-headers '() 109 | "List of cons of html headers, latex headers, latex classes, etc" 110 | :type 'alist 111 | :group 'org-notebook) 112 | 113 | ;;;###autoload 114 | (defun org-notebook-new-notebook () 115 | "Create a new org-notebook notebook" 116 | (interactive) 117 | (let ((org-notebook-filepath (ido-read-file-name "Notebook name: " default-directory))) 118 | (make-directory org-notebook-filepath) 119 | (make-directory (concat org-notebook-filepath "/img")) 120 | (find-file (concat org-notebook-filepath "/notebook.org")) 121 | (insert "#+TITLE: " 122 | (read-from-minibuffer 123 | "Title: " (car (last (split-string org-notebook-filepath "/")))) 124 | "\n" 125 | "# -*- mode: org; -*-" "\n" 126 | "#+AUTHOR: " (user-full-name) "\n" 127 | "#+EMAIL: " user-mail-address "\n" 128 | "#+LANGUAGE: " org-notebook-language "\n" 129 | "#+ATTR_ORG: :width " (number-to-string org-notebook-image-width) "\n" 130 | (cl-loop for (name value) in org-notebook-headers 131 | concat (format "#+%s: %s\n" name value))))) 132 | 133 | ;;;###autoload 134 | (defun org-notebook-insert-image () 135 | "Insert an image with auto-completion for the next image name and open the drawing program" 136 | (interactive) 137 | (unless (file-directory-p "./img") (make-directory "./img")) 138 | (let ((org-notebook-image-filepath 139 | (concat 140 | "./img/" 141 | (read-from-minibuffer 142 | "Filename: " 143 | (format "img%d.png" 144 | (+ (string-to-number 145 | (substring 146 | (car 147 | (split-string 148 | (car (last 149 | (sort 150 | (or 151 | (cddr (directory-files "./img")) 152 | (list (concat "img0." org-notebook-image-type))) 153 | 'org-notebook-dictionary-lessp))) 154 | (concat "." org-notebook-image-type))) 155 | 3)) 156 | 1)))))) 157 | (insert "[[" org-notebook-image-filepath "]]") 158 | (start-process "org-notebook-drawing" nil org-notebook-drawing-program 159 | org-notebook-image-filepath))) 160 | 161 | ;; The following is code for a custom comparison to allow for natural sorting to extract the guessed next-image name 162 | ;; Source: http://stackoverflow.com/questions/1942045/natural-order-sort-for-emacs-lisp 163 | 164 | (defun org-notebook-dictionary-lessp (str1 str2) 165 | "return t if STR1 is < STR2 when doing a dictionary compare 166 | (splitting the string at numbers and doing numeric compare with them)" 167 | (let ((str1-components (org-notebook-dict-split str1)) 168 | (str2-components (org-notebook-dict-split str2))) 169 | (org-notebook-dict-lessp str1-components str2-components))) 170 | 171 | (defun org-notebook-dict-lessp (slist1 slist2) 172 | "compare the two lists of strings & numbers" 173 | (cond ((null slist1) 174 | (not (null slist2))) 175 | ((null slist2) 176 | nil) 177 | ((and (numberp (car slist1)) 178 | (stringp (car slist2))) 179 | t) 180 | ((and (numberp (car slist2)) 181 | (stringp (car slist1))) 182 | nil) 183 | ((and (numberp (car slist1)) 184 | (numberp (car slist2))) 185 | (or (< (car slist1) (car slist2)) 186 | (and (= (car slist1) (car slist2)) 187 | (org-notebook-dict-lessp (cdr slist1) (cdr slist2))))) 188 | (t 189 | (or (string-lessp (car slist1) (car slist2)) 190 | (and (string-equal (car slist1) (car slist2)) 191 | (org-notebook-dict-lessp (cdr slist1) (cdr slist2))))))) 192 | 193 | (defun org-notebook-dict-split (str) 194 | "split a string into a list of number and non-number components" 195 | (save-match-data 196 | (let ((res nil)) 197 | (while (and str (not (string-equal "" str))) 198 | (let ((p (string-match "[0-9]*\\.?[0-9]+" str))) 199 | (cond ((null p) 200 | (setq res (cons str res)) 201 | (setq str nil)) 202 | ((= p 0) 203 | (setq res (cons (string-to-number (match-string 0 str)) res)) 204 | (setq str (substring str (match-end 0)))) 205 | (t 206 | (setq res (cons (substring str 0 (match-beginning 0)) res)) 207 | (setq str (substring str (match-beginning 0))))))) 208 | (reverse res)))) 209 | 210 | (provide 'org-notebook) 211 | ;;; org-notebook.el ends here 212 | --------------------------------------------------------------------------------