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,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)))