Move modules/org/* back to lang/org

This commit is contained in:
Henrik Lissner 2017-12-08 22:59:42 -05:00
parent 346d7bdf36
commit b111303d20
No known key found for this signature in database
GPG key ID: 5F6C0EA160557395
25 changed files with 159 additions and 167 deletions

View file

@ -0,0 +1,74 @@
;;; lang/org/+attach.el -*- lexical-binding: t; -*-
(add-hook 'org-load-hook #'+org-attach|init)
;; I believe Org's native attachment system is over-complicated and litters
;; files with metadata I don't want. So I wrote my own, which:
;;
;; + Causes attachments to be placed in a centralized location,
;; + Adds drag-and-drop support for images (with inline image preview)
;; + Adds drag-and-drop support for media files (pdfs, zips, etc) with a
;; filetype icon and short link.
;; + TODO Offers an attachment management system.
;; Some commands of interest:
;; + `org-download-screenshot'
;; + `+org-attach/file'
;; + `+org-attach/url'
;; + :org [FILE/URL]
(defvar +org-attach-dir (expand-file-name ".attach/" +org-dir)
"Where to store attachments (relative to current org file).")
(def-package! org-download
:commands (org-download-dnd org-download-dnd-base64)
:init
;; Add these myself, so that org-download is lazy-loaded...
(setq dnd-protocol-alist
`(("^\\(https?\\|ftp\\|file\\|nfs\\):" . +org-attach-download-dnd)
("^data:" . org-download-dnd-base64)
,@dnd-protocol-alist))
(advice-add #'org-download-enable :override #'ignore)
:config
(setq-default org-download-image-dir +org-attach-dir
org-download-heading-lvl nil
org-download-timestamp "_%Y%m%d_%H%M%S")
(setq org-download-screenshot-method
(cond (IS-MAC "screencapture -i %s")
(IS-LINUX
(cond ((executable-find "maim") "maim -s %s")
((executable-find "scrot") "scrot -s %s")))))
;; Ensure that relative inline image paths are relative to the attachment folder.
(advice-add #'org-display-inline-images :around #'+org-attach*relative-to-attach-dir)
;; Handle non-image files a little differently. Images should be inserted
;; as-is, as image previews. Other files, like pdfs or zips, should be linked
;; to, with an icon indicating the type of file.
(advice-add #'org-download-insert-link :override #'+org-attach*insert-link)
(defun +org-attach*download-subdir ()
(when (file-in-directory-p buffer-file-name +org-dir)
(file-relative-name buffer-file-name +org-dir)))
;; Write download paths relative to current file
(defun +org-attach*download-fullname (path)
(file-relative-name path (file-name-directory buffer-file-name)))
(advice-add #'org-download--dir-2 :override #'ignore)
(advice-add #'org-download--fullname
:filter-return #'+org-attach*download-fullname))
;;
(defun +org-attach|init ()
(setq org-attach-directory +org-attach-dir)
(push (car (last (split-string +org-attach-dir "/" t)))
projectile-globally-ignored-directories)
(after! recentf
(push (format "%s.+$" (regexp-quote +org-attach-dir))
recentf-exclude)))

View file

@ -0,0 +1,50 @@
;;; lang/org/+babel.el -*- lexical-binding: t; -*-
(add-hook 'org-load-hook #'+org-babel|init)
(defvar +org-babel-languages
'(calc
css
emacs-lisp
haskell
js
latex
ledger
lilypond
lisp
matlab
plantuml
python
restclient ; ob-restclient
ruby
rust ; ob-rust
shell
sqlite
sql-mode ; ob-sql-mode
translate) ; ob-translate
"A list of org-babel languages to load.")
(defun +org-babel|init ()
(setq org-src-fontify-natively t ; make code pretty
org-src-preserve-indentation t ; use native major-mode indentation
org-src-tab-acts-natively t
org-src-window-setup 'current-window
org-confirm-babel-evaluate nil) ; you don't need my permission
(org-babel-do-load-languages
'org-babel-load-languages
(cl-loop for sym in +org-babel-languages
collect (cons sym t)))
;; I prefer C-c C-c for confirming over the default C-c '
(map! :map org-src-mode-map "C-c C-c" #'org-edit-src-exit)
;; In a recent update, `org-babel-get-header' was removed from org-mode, which
;; is something a fair number of babel plugins use. So until those plugins
;; update, this polyfill will do:
(defun org-babel-get-header (params key &optional others)
(cl-loop with fn = (if others #'not #'identity)
for p in params
if (funcall fn (eq (car p) key))
collect p)))

View file

@ -0,0 +1,37 @@
;;; lang/org/+capture.el -*- lexical-binding: t; -*-
(add-hook 'org-load-hook #'+org-capture|init)
;; Sets up two `org-capture' workflows that I like:
;;
;; 1. The traditional way: invoking `org-capture' directly (or through a
;; command, like :org).
;;
;; 2. Through a org-capture popup frame that is invoked from outside Emacs (the
;; script is in ~/.emacs.d/bin). This lets me open an org-capture box
;; anywhere I can call org-capture (whether or not Emacs is open/running),
;; like, say, from qutebrowser, vimperator, dmenu or a global keybinding.
(defvar +org-default-notes-file "notes.org"
"TODO")
(setq org-capture-templates
'(("t" "Todo" entry
(file+headline (expand-file-name "todo.org" +org-dir) "Inbox")
"* [ ] %?\n%i" :prepend t :kill-buffer t)
("n" "Notes" entry
(file+headline org-default-notes-file "Inbox")
"* %u %?\n%i" :prepend t :kill-buffer t)))
(defun +org-capture|init ()
(defvaralias 'org-default-notes-file '+org-default-notes-file)
(setq org-default-notes-file (expand-file-name +org-default-notes-file +org-dir))
(add-hook 'org-capture-after-finalize-hook #'+org-capture|cleanup-frame)
(when (featurep! :feature evil)
(add-hook 'org-capture-mode-hook #'evil-insert-state))
(when (featurep! :ui doom-dashboard)
(add-hook '+doom-dashboard-inhibit-functions #'+org-capture-frame-p)))

View file

@ -0,0 +1,36 @@
;;; lang/org/+export.el -*- lexical-binding: t; -*-
(add-hook 'org-load-hook #'+org-export|init)
;; I don't have any beef with org's built-in export system, but I do wish it
;; would export to a central directory, rather than `default-directory'. This is
;; because all my org files are usually in one place, and I want to be able to
;; refer back to old exports if needed.
(def-package! ox-pandoc
:defer t
:config
(unless (executable-find "pandoc")
(warn "org-export: couldn't find pandoc, disabling pandoc export"))
(setq org-pandoc-options
'((standalone . t)
(mathjax . t)
(parse-raw . t))))
;;
(defun +org-export|init ()
(setq org-export-directory (expand-file-name ".export" +org-dir)
org-export-backends '(ascii html latex md pandoc)
org-export-with-toc t
org-export-with-author t)
;; Always export to a central location
(unless (file-directory-p org-export-directory)
(make-directory org-export-directory t))
(defun +org*export-output-file-name (args)
"Return a centralized export location."
(unless (nth 2 args)
(setq args (append args (list org-export-directory))))
args)
(advice-add #'org-export-output-file-name
:filter-args #'+org*export-output-file-name))

View file

@ -0,0 +1,51 @@
;;; lang/org/+present.el -*- lexical-binding: t; -*-
(defvar +org-present-text-scale 7
"The `text-scale-amount' for `org-tree-slide-mode'.")
(add-hook 'org-load-hook #'+org-present|init)
;;
;; Plugins
;;
(def-package! ox-reveal
:defer t
:config
(setq org-reveal-root "http://cdn.jsdelivr.net/reveal.js/3.0.0/"
org-reveal-mathjax t))
(def-package! org-tree-slide
:commands org-tree-slide-mode
:config
(org-tree-slide-simple-profile)
(setq org-tree-slide-skip-outline-level 2
org-tree-slide-activate-message " "
org-tree-slide-deactivate-message " "
org-tree-slide-modeline-display nil)
(map! :map org-tree-slide-mode-map
:n [right] #'org-tree-slide-move-next-tree
:n [left] #'org-tree-slide-move-previous-tree)
(add-hook! 'org-tree-slide-mode-after-narrow-hook
#'(+org-present|detect-slide +org-present|add-overlays org-display-inline-images))
(add-hook 'org-tree-slide-mode-hook #'+org-present|init-org-tree-window)
(advice-add #'org-tree-slide--display-tree-with-narrow
:around #'+org-present*narrow-to-subtree))
(def-package! centered-window-mode :commands centered-window-mode)
;;
;; Bootstrap
;;
(defun +org-present|init ()
(require 'ox-reveal)
(map! :map org-mode-map "<f8>" #'+org-present/start))

View file

View file

@ -0,0 +1,11 @@
;;; lang/org/autoload/evil.el -*- lexical-binding: t; -*-
;;;###if (featurep! :feature evil)
;; TODO +org-attach:find
;;;###autoload (autoload '+org-attach:uri "lang/org/autoload/evil" nil t)
(evil-define-command +org-attach:uri (uri)
"Downloads the file at URL and places an org link to it at the cursor."
(interactive "<f>")
(+org-attach/uri uri))

View file

@ -0,0 +1,133 @@
;;; lang/org/autoload/org-attach.el -*- lexical-binding: t; -*-
;;;###if (featurep! +attach)
(defun +org-attach--icon (path)
(char-to-string
(pcase (downcase (file-name-extension path))
((or "jpg" "jpeg" "png" "gif") ?)
("pdf" ?)
((or "ppt" "pptx") ?)
((or "xls" "xlsx") ?)
((or "doc" "docx") ?)
((or "ogg" "mp3" "wav" "aiff" "flac") ?)
((or "mp4" "mov" "avi") ?)
((or "zip" "gz" "tar" "7z" "rar") ?)
(_ ?))))
;; (defun +org-attach-cleanup ()
;; ;; "Deletes any attachments that are no longer present in the org-mode buffer."
;; (let* ((attachments-local (+org-attachments))
;; (attachments (directory-files org-attach-directory t "^[^.]" t))
;; (to-delete (cl-set-difference attachments-local attachments)))
;; ;; TODO
;; to-delete))
;; (defun +org-attachments ()
;; "List all attachments in the current buffer."
;; (unless (eq major-mode 'org-mode)
;; (user-error "Not an org buffer"))
;; (org-save-outline-visibility nil
;; (let ((attachments '())
;; element)
;; (when (and (file-directory-p org-attach-directory)
;; (> (length (file-expand-wildcards (expand-file-name "*" org-attach-directory))) 0))
;; (save-excursion
;; (goto-char (point-min))
;; (while (progn (org-next-link) (not org-link-search-failed))
;; (setq element (org-element-context))
;; (when-let (file (and (eq (org-element-type element) 'link)
;; (expand-file-name (org-element-property :path element))))
;; (when (and (string= (org-element-property :type element) "file")
;; (string= (concat (file-name-base (directory-file-name (file-name-directory file))) "/")
;; org-attach-directory)
;; (file-exists-p file))
;; (push file attachments))))))
;; (cl-remove-duplicates attachments))))
;;;###autoload
(defun +org-attach/file (path)
"Copies the file at PATH to `+org-attach-dir' and places an org link to it at
the cursor."
(interactive "fAttach file: ")
(+org-attach/uri path))
;;;###autoload
(defun +org-attach/uri (uri)
"Downloads the file at URL and place an org link to it at the cursor."
(interactive "sUri/file: ")
(unless (eq major-mode 'org-mode)
(user-error "Not in an org buffer"))
(require 'org-download)
(condition-case ex
(cond ((string-match-p "^data:image/png;base64," uri)
(org-download-dnd-base64 uri nil))
((image-type-from-file-name uri)
(org-download-image uri))
(t
(let ((new-path (expand-file-name (org-download--fullname uri))))
;; Download the file
(if (string-match-p (concat "^" (regexp-opt '("http" "https" "nfs" "ftp" "file")) ":/") uri)
(url-copy-file uri new-path)
(copy-file uri new-path))
;; insert the link
(org-download-insert-link uri new-path))))
(error
(user-error "Failed to attach file: %s" (error-message-string ex)))))
;;;###autoload
(defun +org-attach-download-dnd (uri action)
"TODO"
(if (eq major-mode 'org-mode)
(+org-attach:url uri)
(let ((dnd-protocol-alist
(rassq-delete-all '+org-attach-download-dnd
(copy-alist dnd-protocol-alist))))
(dnd-handle-one-url nil action uri))))
;;;###autoload
(defun +org-attach*link-format (filename &optional ext)
(format "%s%s.%s"
(file-name-sans-extension filename)
(format-time-string org-download-timestamp)
(or ext (file-name-extension filename))))
;;;###autoload
(defun +org-attach*insert-link (_link filename)
"TODO"
(if (looking-back "^[ \t]+" (line-beginning-position))
(delete-region (match-beginning 0) (match-end 0))
(newline))
(cond ((image-type-from-file-name filename)
(when (file-in-directory-p filename +org-attach-dir)
(setq filename (file-relative-name filename +org-dir)))
(insert
(concat (if (= org-download-image-html-width 0)
""
(format "#+attr_html: :width %dpx\n" org-download-image-html-width))
(if (= org-download-image-latex-width 0)
""
(format "#+attr_latex: :width %dcm\n" org-download-image-latex-width))
(format org-download-link-format
(file-relative-name filename (file-name-directory buffer-file-name)))))
(org-display-inline-images))
(t
(insert
(format "%s [[./%s][%s]] "
(org-attach--icon filename)
(file-relative-name filename buffer-file-name)
(file-name-nondirectory (directory-file-name rel-path)))))))
;;;###autoload
(defun +org-attach*relative-to-attach-dir (orig-fn &rest args)
"TODO"
(if (file-in-directory-p buffer-file-name +org-dir)
(let* ((context (save-match-data (org-element-context)))
(file (org-link-unescape (org-element-property :path context)))
(default-directory
(if (string-prefix-p
(concat "./" (car (last (split-string +org-attach-dir "/" t))))
file)
+org-dir
default-directory)))
(apply orig-fn args))
(apply orig-fn args)))

View file

@ -0,0 +1,13 @@
;;; lang/org/autoload/org-babel.el -*- lexical-binding: t; -*-
;;;###if (featurep! +babel)
;;;###autoload
(defun +org-babel/edit (arg)
"Edit the source block at point in a popup.
If ARG is non-nil (universal argument), use the current window."
(interactive "P")
(if arg
(call-interactively #'org-edit-special)
(with-popup-rules! (("^\\*Org Src" :regexp t :select t :noesc t :same t))
(call-interactively #'org-edit-special))))

View file

@ -0,0 +1,82 @@
;;; lang/org/autoload/org-capture.el -*- lexical-binding: t; -*-
;;;###if (featurep! +capture)
(when (featurep! :feature evil)
;;;###autoload (autoload '+org-capture:open "lang/org/autoload/org-capture" nil t)
(evil-define-operator +org-capture:open (&optional beg end)
"Evil ex interface to `+org-capture/dwim'."
:move-point nil :type inclusive
(interactive "<r>")
(+org-capture/open
(unless (or (evil-normal-state-p) (evil-insert-state-p))
(buffer-substring beg end)))))
;;;###autoload
(defun +org-capture/open (&optional string key)
"Sends STRING, the current selection or prompted input to `org-capture'.
Uses the capture template specified by KEY. Otherwise, prompts you for one."
(interactive)
(let ((key (or key "n")))
(if-let (string (cond ((not (equal string ""))
string)
((region-active-p)
(buffer-substring-no-properties
(region-beginning)
(region-end)))))
(org-capture-string string key)
(org-capture nil key))))
;; --- External frame ---------------------
(defvar +org-capture-window-params
`((name . "org-capture")
(width . 70)
(height . 25)
(window-system . ,(cond (IS-MAC 'ns)
(IS-LINUX 'x)
(t 'w32)))
,(if IS-LINUX '(display . ":0")))
"TODO")
;;;###autoload
(defun +org-capture|cleanup-frame ()
"Closes the org-capture frame once done adding an entry."
(when (+org-capture-frame-p)
(delete-frame nil t)))
;;;###autoload
(defun +org-capture-frame-p (&rest _)
"Return t if the current frame is an org-capture frame opened by
`+org-capture/open-frame'."
(equal "org-capture" (frame-parameter nil 'name)))
;;;###autoload
(defun +org-capture/open-frame (&optional string key)
"Opens the org-capture window in a floating frame that cleans itself up once
you're done. This can be called from an external shell script."
(interactive)
(require 'org)
(let (after-make-frame-functions before-make-frame-hook)
(let ((frame (if (+org-capture-frame-p)
(selected-frame)
(make-frame +org-capture-window-params))))
(with-selected-frame frame
(condition-case ex
(cl-letf (((symbol-function #'pop-to-buffer)
(symbol-function #'switch-to-buffer)))
(if (and (stringp string)
(not (string-empty-p string)))
(org-capture-string string key)
(org-capture nil key))
(when (featurep 'solaire-mode)
(solaire-mode +1)))
('error
(message "org-capture: %s" (error-message-string ex))
(delete-frame frame)))))))
;;;###autoload
(defun +org-capture-available-keys ()
"TODO"
(string-join (mapcar #'car org-capture-templates) ""))

View file

@ -0,0 +1,15 @@
;;; org/org/autoload/org-link.el -*- lexical-binding: t; -*-
;;;###autoload
(defun +org-link-read-file (key dir)
(let ((file (read-file-name (format "%s: " (capitalize key)) dir)))
(format "%s:%s"
key
(file-relative-name file dir))))
;;;###autoload
(defun +org-link-read-directory (key dir)
(let ((file (read-directory-name (format "%s: " (capitalize key)) dir)))
(format "%s:%s"
key
(file-relative-name file dir))))

View file

@ -0,0 +1,104 @@
;;; lang/org/autoload/org-present.el -*- lexical-binding: t; -*-
;;;###if (featurep! +present)
(defvar +org-present--overlays nil)
;;;###autoload
(defun +org-present/start ()
"TODO"
(interactive)
(unless (derived-mode-p 'org-mode)
(error "Not in an org buffer"))
(call-interactively 'org-tree-slide-mode)
(add-hook 'kill-buffer-hook '+org-present--cleanup-org-tree-slides-mode))
;; --- Hooks ------------------------------
;;;###autoload
(defun +org-present|add-overlays ()
(add-to-invisibility-spec '(+org-present))
(save-excursion
;; hide org-mode options starting with #+
(goto-char (point-min))
(while (re-search-forward "^[[:space:]]*\\(#\\+\\)\\(\\(?:BEGIN\\|END\\|ATTR\\)[^[:space:]]+\\).*" nil t)
(+org-present--make-invisible
(match-beginning 1)
(match-end 0)))
;; hide stars in headings
(goto-char (point-min))
(while (re-search-forward "^\\(\\*+\\s-\\)" nil t)
(+org-present--make-invisible (match-beginning 1) (match-end 1)))))
;;;###autoload
(defun +org-present|remove-overlays ()
(mapc #'delete-overlay +org-present--overlays)
(remove-from-invisibility-spec '(+org-present)))
;;;###autoload
(defun +org-present|detect-slide ()
(outline-show-all)
(if (member "title" (org-get-tags-at))
(text-scale-set 10)
(text-scale-set +org-present-text-scale)))
;;;###autoload
(defun +org-present|init-org-tree-window ()
"Set up the org window for presentation."
(doom/window-zoom)
(let ((cwm-use-vertical-padding t)
(cwm-frame-internal-border 110)
(cwm-left-fringe-ratio -10)
(cwm-centered-window-width 240)
(arg (if org-tree-slide-mode +1 -1)))
(when (fboundp 'centered-window-mode)
(centered-window-mode arg))
(window-divider-mode (* arg -1))
(doom-hide-modeline-mode arg)
(+org-pretty-mode arg)
(cond (org-tree-slide-mode
(org-indent-mode -1)
(text-scale-set +org-present-text-scale)
(ignore-errors (org-toggle-latex-fragment '(4)))
(set-face-attribute 'org-level-2 nil :height 1.4))
(t
(org-indent-mode +1)
(text-scale-set 0)
(org-remove-latex-fragment-image-overlays)
(set-face-attribute 'org-level-2 nil :height 1.0)
(+org-present|remove-overlays)
(org-remove-inline-images)))))
;; --- Helpers ----------------------------
(defun +org-present--cleanup-org-tree-slides-mode ()
(unless (cl-loop for buf in (doom-buffers-in-mode 'org-mode)
if (buffer-local-value 'org-tree-slide-mode buf)
return t)
(org-tree-slide-mode -1)
(remove-hook 'kill-buffer-hook #'+org-present--cleanup-org-tree-slides-mode)))
(defun +org-present--make-invisible (beg end)
(let ((overlay (make-overlay beg end)))
(push overlay +org-present--overlays)
(overlay-put overlay 'invisible '+org-present)))
;; --- Advice -----------------------------
;;;###autoload
(defun +org-present*narrow-to-subtree (orig-fn &rest args)
"Narrow to the target subtree when you start the presentation."
(cl-letf (((symbol-function 'org-narrow-to-subtree)
(lambda () (save-excursion
(save-match-data
(org-with-limited-levels
(narrow-to-region
(progn (org-back-to-heading t)
(forward-line 1)
(point))
(progn (org-end-of-subtree t t)
(when (and (org-at-heading-p) (not (eobp))) (backward-char 1))
(point)))))))))
(apply orig-fn args)))

View file

@ -0,0 +1,297 @@
;;; org/org/autoload/org.el -*- lexical-binding: t; -*-
;;;###autoload
(define-minor-mode +org-pretty-mode
"TODO"
:init-value nil
:lighter " *"
:group 'evil-org
(setq org-hide-emphasis-markers +org-pretty-mode)
(org-toggle-pretty-entities)
(org-with-silent-modifications
;; In case the above un-align tables
(org-table-map-tables 'org-table-align t)))
;;;###autoload
(defun +org|realign-table-maybe ()
"Auto-align table under cursor and re-calculate formulas."
(when (org-at-table-p)
(save-excursion
(quiet! (org-table-recalculate)))))
;;;###autoload
(defun +org|update-cookies ()
"Update counts in headlines (aka \"cookies\")."
(when (and buffer-file-name (file-exists-p buffer-file-name))
(org-update-statistics-cookies t)))
;;;###autoload
(defun +org/dwim-at-point ()
"Do-what-I-mean at point.
If on a:
- checkbox list item or todo heading: toggle it.
- clock: update its time.
- headline: toggle latex fragments and inline images underneath.
- footnote definition: jump to the footnote
- table-row or a TBLFM: recalculate the table's formulas
- table-cell: clear it and go into insert mode. If this is a formula cell,
recaluclate it instead.
- babel-call: execute the source block
- statistics-cookie: update it.
- latex fragment: toggle it.
- link: follow it
- otherwise, refresh all inline images in current tree."
(interactive)
(let* ((scroll-pt (window-start))
(context (org-element-context))
(type (org-element-type context)))
;; skip over unimportant contexts
(while (and context (memq type '(verbatim code bold italic underline strike-through subscript superscript)))
(setq context (org-element-property :parent context)
type (org-element-type context)))
(pcase type
((guard (org-element-property :checkbox (org-element-lineage context '(item) t)))
(let ((match (and (org-at-item-checkbox-p) (match-string 1))))
(org-toggle-checkbox (if (equal match "[ ]") '(16)))))
(`headline
(cond ((org-element-property :todo-type context)
(org-todo
(if (eq (org-element-property :todo-type context) 'done) 'todo 'done)))
((string= "ARCHIVE" (car-safe (org-get-tags)))
(org-force-cycle-archived))
(t
(org-remove-latex-fragment-image-overlays)
(org-toggle-latex-fragment '(4)))))
(`clock (org-clock-update-time-maybe))
(`footnote-definition
(goto-char (org-element-property :post-affiliated context))
(call-interactively #'org-footnote-action))
((or `planning `timestamp)
(org-follow-timestamp-link))
((or `table `table-row)
(if (org-at-TBLFM-p)
(org-table-calc-current-TBLFM)
(ignore-errors
(save-excursion
(goto-char (org-element-property :contents-begin context))
(org-call-with-arg 'org-table-recalculate (or arg t))))))
(`table-cell
(org-table-blank-field)
(org-table-recalculate)
(when (and (string-empty-p (string-trim (org-table-get-field)))
(bound-and-true-p evil-mode))
(evil-change-state 'insert)))
(`babel-call
(org-babel-lob-execute-maybe))
(`statistics-cookie
(save-excursion (org-update-statistics-cookies nil)))
((or `src-block `inline-src-block)
(org-babel-execute-src-block))
((or `latex-fragment `latex-environment)
(org-toggle-latex-fragment))
(`link
(let ((path (org-element-property :path (org-element-lineage context '(link) t))))
(if (and path (image-type-from-file-name path))
(+org/refresh-inline-images)
(org-open-at-point))))
(_ (+org/refresh-inline-images)))
(set-window-start nil scroll-pt)))
;;;###autoload
(defun +org/indent ()
"Indent the current item (header or item). Otherwise, forward to
`self-insert-command'."
(interactive)
(cond ((org-at-item-p)
(org-indent-item-tree))
((org-at-heading-p)
(ignore-errors (org-demote)))
((org-in-src-block-p t)
(doom/dumb-indent))
(t
(call-interactively #'self-insert-command))))
;;;###autoload
(defun +org/indent-or-next-field-or-yas-expand ()
"Depending on the context either a) indent the current line, b) go the next
table field or c) run `yas-expand'."
(interactive)
(call-interactively
(cond ((and (bound-and-true-p yas-minor-mode)
(yas--templates-for-key-at-point))
#'yas-expand)
((org-at-table-p)
#'org-table-next-field)
(t
#'+org/indent))))
;;;###autoload
(defun +org/dedent ()
"Dedent the current item (header or item). Otherwise, forward to
`self-insert-command'."
(interactive)
(cond ((org-at-item-p)
(org-list-indent-item-generic
-1 nil
(save-excursion
(when (org-region-active-p)
(goto-char (region-beginning)))
(org-list-struct))))
((org-at-heading-p)
(ignore-errors (org-promote)))
(t
(call-interactively #'self-insert-command))))
;;;###autoload
(defun +org/dedent-or-prev-field ()
"Depending on the context either dedent the current item or go the previous
table field."
(interactive)
(call-interactively
(if (org-at-table-p)
#'org-table-previous-field
#'+org/dedent)))
;;;###autoload
(defun +org/insert-item (direction)
"Inserts a new heading, table cell or item, depending on the context.
DIRECTION can be 'above or 'below.
I use this instead of `org-insert-item' or `org-insert-heading' which are too
opinionated and perform this simple task incorrectly (e.g. whitespace in the
wrong places)."
(interactive)
(let* ((context (org-element-lineage
(org-element-context)
'(table table-row headline inlinetask item plain-list)
t))
(type (org-element-type context)))
(cond ((memq type '(item plain-list))
(let ((marker (org-element-property :bullet context))
(pad (save-excursion
(back-to-indentation)
(- (point) (line-beginning-position)))))
(pcase direction
('below
(org-end-of-item)
(goto-char (line-beginning-position))
(insert (make-string pad 32) (or marker ""))
(save-excursion (insert "\n")))
('above
(goto-char (line-beginning-position))
(insert (make-string pad 32) (or marker ""))
(save-excursion (insert "\n")))))
(when (org-element-property :checkbox context)
(insert "[ ] ")))
((memq type '(table table-row))
(pcase direction
('below (org-table-insert-row t))
('above (org-shiftmetadown))))
((memq type '(headline inlinetask))
(let ((level (if (eq (org-element-type context) 'headline)
(org-element-property :level context)
1)))
(pcase direction
('below
(let ((at-eol (= (point) (1- (line-end-position)))))
(goto-char (line-end-position))
(org-end-of-subtree)
(insert (concat "\n"
(when (= level 1)
(if at-eol
(ignore (cl-incf level))
"\n"))
(make-string level ?*)
" "))))
('above
(org-back-to-heading)
(org-insert-heading)
(when (= level 1)
(save-excursion (insert "\n")))))
(when (org-element-property :todo-type context)
(org-todo 'todo))))
(t (user-error "Not a valid list, heading or table")))
(when (bound-and-true-p evil-mode)
(evil-append-line 1))))
;;;###autoload
(defun +org-get-property (name &optional _file) ; TODO Add FILE
"Get a propery from an org file."
(save-excursion
(goto-char 1)
(re-search-forward (format "^#\\+%s:[ \t]*\\([^\n]+\\)" (upcase name)) nil t)
(buffer-substring-no-properties (match-beginning 1) (match-end 1))))
;;;###autoload
(defun +org/refresh-inline-images ()
"Refresh image previews in the current heading/tree."
(interactive)
(if (> (length org-inline-image-overlays) 0)
(org-remove-inline-images)
(org-display-inline-images
t t
(if (org-before-first-heading-p)
(line-beginning-position)
(save-excursion (org-back-to-heading) (point)))
(if (org-before-first-heading-p)
(line-end-position)
(save-excursion (org-end-of-subtree) (point))))))
;;;###autoload
(defun +org/toggle-checkbox ()
"Toggle the presence of a checkbox in the current item."
(interactive)
(org-toggle-checkbox '(4)))
;;;###autoload
(defun +org/toggle-fold ()
"Toggle the local fold at the point (as opposed to cycling through all levels
with `org-cycle'). Also:
+ If in a babel block, removes result blocks.
+ If in a table, realign it, if necessary."
(interactive)
(save-excursion
(org-beginning-of-line)
(cond ((org-at-table-p)
(org-table-align))
((org-in-src-block-p)
(org-babel-remove-result))
((org-at-heading-p)
(outline-toggle-children))
((org-at-item-p)
(let ((window-beg (window-start)))
(org-cycle)
(set-window-start nil window-beg))))))
;;;###autoload
(defun +org/remove-link ()
"Unlink the text at point."
(interactive)
(unless (org-in-regexp org-bracket-link-regexp 1)
(user-error "No link at point"))
(save-excursion
(let ((remove (list (match-beginning 0) (match-end 0)))
(description (if (match-end 3)
(org-match-string-no-properties 3)
(org-match-string-no-properties 1))))
(apply #'delete-region remove)
(insert description))))

View file

@ -0,0 +1,53 @@
;;; org/org/autoload/tables.el -*- lexical-binding: t; -*-
;;;###autoload
(defun +org/table-next-row ()
"Go to the next row (same column) in the current table."
(interactive)
(if (org-at-table-p)
(org-table-next-row)
(org-down-element)))
;;;###autoload
(defun +org/table-previous-row ()
"Go to the previous row (same column) in the current table. Before doing so,
re-align the table if necessary. (Necessary because org-mode has a
`org-table-next-row', but not `org-table-previous-row')"
(interactive)
(if (org-at-table-p)
(progn
(org-table-maybe-eval-formula)
(org-table-maybe-recalculate-line)
(if (and org-table-automatic-realign
org-table-may-need-update)
(org-table-align))
(let ((col (org-table-current-column)))
(beginning-of-line 0)
(when (or (not (org-at-table-p)) (org-at-table-hline-p))
(beginning-of-line))
(org-table-goto-column col)
(skip-chars-backward "^|\n\r")
(when (org-looking-at-p " ") (forward-char))))
(org-up-element)))
;;;###autoload
(defun +org/table-next-field ()
(interactive)
(if (org-at-table-p) (org-table-next-field) (org-end-of-line)))
;;;###autoload
(defun +org/table-previous-field ()
(interactive)
(if (org-at-table-p) (org-table-previous-field) (org-beginning-of-line)))
;;;###autoload
(defun +org/table-append-field-or-shift-right ()
(interactive)
(org-shiftmetaright)
(when (org-at-table-p) (org-metaright)))
;;;###autoload
(defun +org/table-prepend-field-or-shift-left ()
(interactive)
(if (org-at-table-p) (org-shiftmetaright) (org-shiftmetaleft)))

231
modules/lang/org/config.el Normal file
View file

@ -0,0 +1,231 @@
;;; lang/org/config.el -*- lexical-binding: t; -*-
(defvar +org-dir (expand-file-name "~/work/org/")
"The directory where org files are kept.")
;; Ensure ELPA org is prioritized above built-in org.
(when-let (path (locate-library "org" nil doom--package-load-path))
(setq load-path (delete path load-path))
(push (file-name-directory path) load-path))
;; Sub-modules
(if (featurep! +attach) (load! +attach))
(if (featurep! +babel) (load! +babel))
(if (featurep! +capture) (load! +capture))
(if (featurep! +export) (load! +export))
(if (featurep! +present) (load! +present))
;; TODO (if (featurep! +publish) (load! +publish))
(after! org (+org|init))
(add-hook 'org-mode-hook #'+org|hook)
;;
;; Plugins
;;
(def-package! toc-org
:hook (org-mode . toc-org-enable))
(def-package! org-crypt ; built-in
:hook (org-load . org-crypt-use-before-save-magic)
:config
(setq org-tags-exclude-from-inheritance '("crypt")
org-crypt-key user-mail-address
epa-file-encrypt-to user-mail-address))
(def-package! org-bullets
:hook (org-mode . org-bullets-mode))
;;
;; Hooks & bootstraps
;;
(defun +org|init ()
"Run once, when org is first loaded."
(defvaralias 'org-directory '+org-dir)
(require 'org)
(+org-init-ui)
(+org-init-keybinds)
(+org-hacks))
(defun +org|hook ()
"Run everytime `org-mode' is enabled."
(when (featurep! :feature evil)
(add-hook 'evil-insert-state-exit-hook #'+org|realign-table-maybe nil t)
(add-hook 'evil-insert-state-exit-hook #'+org|update-cookies nil t))
(add-hook 'before-save-hook #'+org|update-cookies nil t)
;;
(setq line-spacing 1)
(visual-line-mode +1)
(org-indent-mode +1)
(doom|disable-line-numbers)
;; show-paren-mode causes problems for org-indent-mode, so disable it
(set (make-local-variable 'show-paren-mode) nil)
(unless org-agenda-inhibit-startup
;; My version of the 'overview' #+STARTUP option: expand first-level
;; headings. Expands the first level, but no further.
(when (eq org-startup-folded t)
(outline-hide-sublevels 2))
;; If saveplace places the point in a folded position, unfold it on load
(when (outline-invisible-p)
(ignore-errors
(save-excursion
(outline-previous-visible-heading 1)
(org-show-subtree))))))
;;
(defun +org-init-ui ()
"Configures the UI for `org-mode'."
(setq-default
org-adapt-indentation nil
org-agenda-dim-blocked-tasks nil
org-agenda-files (directory-files +org-dir t "\\.org$" t)
org-agenda-inhibit-startup t
org-agenda-skip-unavailable-files nil
org-cycle-include-plain-lists t
org-cycle-separator-lines 1
org-entities-user '(("flat" "\\flat" nil "" "" "266D" "") ("sharp" "\\sharp" nil "" "" "266F" ""))
;; org-ellipsis " ... "
org-fontify-done-headline t
org-fontify-quote-and-verse-blocks t
org-fontify-whole-heading-line t
org-footnote-auto-label 'plain
org-hidden-keywords nil
org-hide-emphasis-markers nil
org-hide-leading-stars t
org-hide-leading-stars-before-indent-mode t
org-image-actual-width nil
org-indent-indentation-per-level 2
org-indent-mode-turns-on-hiding-stars t
org-pretty-entities nil
org-pretty-entities-include-sub-superscripts t
org-priority-faces
`((?a . ,(face-foreground 'error))
(?b . ,(face-foreground 'warning))
(?c . ,(face-foreground 'success)))
org-startup-folded t
org-startup-indented t
org-startup-with-inline-images nil
org-tags-column 0
org-todo-keywords
'((sequence "[ ](t)" "[-](p)" "[?](m)" "|" "[X](d)")
(sequence "TODO(T)" "|" "DONE(D)")
(sequence "NEXT(n)" "ACTIVE(a)" "WAITING(w)" "LATER(l)" "|" "CANCELLED(c)"))
org-use-sub-superscripts '{}
outline-blank-line t
;; LaTeX previews are too small and usually render to light backgrounds, so
;; this enlargens them and ensures their background (and foreground) match the
;; current theme.
org-preview-latex-image-directory (concat doom-cache-dir "org-latex/")
org-format-latex-options (plist-put org-format-latex-options :scale 1.5)
org-format-latex-options
(plist-put org-format-latex-options
:background (face-attribute (or (cadr (assq 'default face-remapping-alist))
'default)
:background nil t)))
;; Custom links
(org-link-set-parameters
"org"
:complete (lambda () (+org-link-read-file "org" +org-dir))
:follow (lambda (link) (find-file (expand-file-name link +org-dir)))
:face (lambda (link)
(if (file-exists-p (expand-file-name link +org-dir))
'org-link
'error))))
(defun +org-init-keybinds ()
"Sets up org-mode and evil keybindings. Tries to fix the idiosyncrasies
between the two."
(map! :map org-mode-map
"RET" #'org-return-indent
"C-c C-S-l" #'+org/remove-link
:n "C-c C-i" #'org-toggle-inline-images
:n "RET" #'+org/dwim-at-point
;; Navigate table cells (from insert-mode)
:i "C-l" #'+org/table-next-field
:i "C-h" #'+org/table-previous-field
:i "C-k" #'+org/table-previous-row
:i "C-j" #'+org/table-next-row
;; Expand tables (or shiftmeta move)
:ni "C-S-l" #'+org/table-append-field-or-shift-right
:ni "C-S-h" #'+org/table-prepend-field-or-shift-left
:ni "C-S-k" #'org-metaup
:ni "C-S-j" #'org-metadown
:n [tab] #'+org/toggle-fold
:i [tab] #'+org/indent-or-next-field-or-yas-expand
:i [backtab] #'+org/dedent-or-prev-field
:ni [M-return] (λ! (+org/insert-item 'below))
:ni [S-M-return] (λ! (+org/insert-item 'above))
:m "]]" (λ! (org-forward-heading-same-level nil) (org-beginning-of-line))
:m "[[" (λ! (org-backward-heading-same-level nil) (org-beginning-of-line))
:m "]l" #'org-next-link
:m "[l" #'org-previous-link
:m "$" #'org-end-of-line
:m "^" #'org-beginning-of-line
:n "gQ" #'org-fill-paragraph
:n "<" #'org-metaleft
:n ">" #'org-metaright
:v "<" (λ! (org-metaleft) (evil-visual-restore))
:v ">" (λ! (org-metaright) (evil-visual-restore))
;; Fix code-folding keybindings
:n "za" #'+org/toggle-fold
:n "zA" #'org-shifttab
:n "zc" #'outline-hide-subtree
:n "zC" (λ! (outline-hide-sublevels 1))
:n "zd" (lambda (&optional arg) (interactive "p") (outline-hide-sublevels (or arg 3)))
:n "zm" (λ! (outline-hide-sublevels 1))
:n "zo" #'outline-show-subtree
:n "zO" #'outline-show-all
:n "zr" #'outline-show-all
(:after org-agenda
(:map org-agenda-mode-map
:e "<escape>" #'org-agenda-Quit
:e "m" #'org-agenda-month-view
:e "C-j" #'org-agenda-next-item
:e "C-k" #'org-agenda-previous-item
:e "C-n" #'org-agenda-next-item
:e "C-p" #'org-agenda-previous-item))))
;;
(defun +org-hacks ()
"Getting org to behave."
;; Don't open separate windows
(push '(file . find-file) org-link-frame-setup)
;; Let OS decide what to do with files when opened
(setq org-file-apps
`(("\\.org$" . emacs)
(t . ,(cond (IS-MAC "open -R \"%s\"")
(IS-LINUX "xdg-open \"%s\"")))))
(defun +org|remove-occur-highlights ()
"Remove org occur highlights on ESC in normal mode."
(when (and (derived-mode-p 'org-mode)
org-occur-highlights)
(org-remove-occur-highlights)))
(add-hook '+evil-esc-hook #'+org|remove-occur-highlights)
(after! recentf
;; Don't clobber recentf with agenda files
(defun +org-is-agenda-file (filename)
(cl-find (file-truename filename) org-agenda-files
:key #'file-truename
:test #'equal))
(add-to-list 'recentf-exclude #'+org-is-agenda-file)))

View file

@ -0,0 +1,33 @@
;; -*- no-byte-compile: t; -*-
;;; lang/org/packages.el
;; NOTE This is an insecure source, but unavoidable if we want org 9.0+ (which
;; this module requires). orgmode.org offers no secure access to this repo. If
;; this bothers you, comment out this `package!' block and download
;; org-plus-contrib from orgmode.org.
(package! org-plus-contrib :recipe (:fetcher git :url "http://orgmode.org/org-mode.git"))
(package! org-bullets :recipe (:fetcher github :repo "hlissner/org-bullets"))
(package! toc-org)
(when (featurep! +attach)
(package! org-download))
(when (featurep! +babel)
(package! ob-go)
(package! ob-mongo)
(package! ob-redis)
(package! ob-restclient)
(package! ob-rust :recipe (:fetcher github :repo "zweifisch/ob-rust"))
(package! ob-sql-mode)
(package! ob-translate))
(when (featurep! +export)
(package! ox-pandoc))
(when (featurep! +present)
(package! centered-window-mode)
(package! org-tree-slide)
(package! ox-reveal))
;; (when (featurep! +publish))

View file

@ -0,0 +1,42 @@
;; -*- no-byte-compile: t; -*-
;;; org/org/test/autoload-org.el
(defmacro should-org-buffer! (source expected &rest body)
`(should-buffer! ,source ,expected
(org-mode)
,@body))
;; `+org/insert-item'
(def-test! insert-item-h1
"Should append/prepend new first-level headers with an extra newline."
(should-org-buffer! ("* {0}Header") ("* Header\n\n* {|}")
(+org/insert-item 'below))
(should-org-buffer! ("* {0}Header") ("* {|}\n\n* Header")
(+org/insert-item 'above)))
(def-test! insert-item-h2
"Should append/prepend new second-level (and higher) headers without an extra
newline."
(should-org-buffer! ("** {0}Header") ("** Header\n** {|}")
(+org/insert-item 'below))
(should-org-buffer! ("** {0}Header") ("** {|}\n** Header")
(+org/insert-item 'above)))
(def-test! insert-item-plain-list
"Should append/prepend new second-level (and higher) headers without an extra
newline."
(should-org-buffer! ("+ {0}List item") ("+ List item\n+ {|}")
(+org/insert-item 'below))
(should-org-buffer! ("+ {0}List item"
" + Sub item")
("+ List item"
" + Sub item"
"+ {|}")
(+org/insert-item 'below))
(should-org-buffer! ("+ {0}List item"
"+ Next item")
("+ List item"
"+ {|}"
"+ Next item")
(+org/insert-item 'below)))

View file

@ -0,0 +1,5 @@
;;; org/org/test/org.el -*- lexical-binding: t; -*-
(when (featurep 'org) (unload-feature 'org t))
(require! :org org)
(require 'org (locate-library "org" nil doom--package-load-path))