From 49e5231ea1c64b4cced041a6d5d63e5871965018 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Erik=20Sj=C3=B6strand?= Date: Wed, 3 Jun 2020 14:43:00 +0200 Subject: [PATCH] Add polymode support. --- README.org | 21 +++++++++++++++++---- org-brain.el | 52 +++++++++++++++++++++++++++++++++++++++++++++++++--- 2 files changed, 66 insertions(+), 7 deletions(-) diff --git a/README.org b/README.org index 95cf4a1..57cabd1 100644 --- a/README.org +++ b/README.org @@ -83,10 +83,10 @@ annotated connection. * Setup and requirements The easiest way is to get =org-brain= from MELPA. If you do not want to do that, -clone this git repository or download =org-brain.el= and add it to your -load-path. The example below is using [[https://github.com/jwiegley/use-package][use-package]] and assumes that you're using -MELPA, but you could use =(require 'org-brain)= or add a =:load-path= to -=use-package= instead. +clone this git repository or download =org-brain.el= and add it to your load-path. +The example below is using [[https://github.com/jwiegley/use-package][use-package]] and assumes that you're using MELPA, but +you could use =(require 'org-brain)= or add a =:load-path= to =use-package= instead. +Most of the configuration below isn't necessary, but showcases some options. #+BEGIN_SRC emacs-lisp (use-package org-brain :ensure t @@ -107,6 +107,11 @@ MELPA, but you could use =(require 'org-brain)= or add a =:load-path= to (setq org-brain-title-max-length 12) (setq org-brain-include-file-entries nil org-brain-file-entries-use-title nil)) + + ;; Allows you to edit entries directly from org-brain-visualize + (use-package polymode + :config + (add-hook 'org-brain-visualize-mode-hook #'org-brain-polymode)) #+END_SRC 1. =org-brain= requires Emacs 25 and org-mode 9. These need to be part of your @@ -147,6 +152,10 @@ MELPA, but you could use =(require 'org-brain)= or add a =:load-path= to also set =org-brain-file-entries-use-title= to =nil=. Another possibility is if you're only using file entries, in which case you can set =org-brain-scan-for-header-entries= to =nil=. +11. =polymode= is a package (available on MELPA) which allows for several + major-modes in the same buffer. If you have required the package you can use + =M-x org-brain-polymode= inside =org-brain-visualize=, or (as in the example + above) add =org-brain-polymode= to =org-brain-visualize-mode-hook=. ** Category icons @@ -323,6 +332,10 @@ entry, or =S P= to remove the parent relationship of the selected entries. When you're done and wish to clear the selection use =org-brain-clear-selected=, which is bound to =S s=. +** Editing text from =org-brain-visualize-mode= + +If you have the =polymode= package installed you can edit your entries directly from =org-brain-visualize-mode=. Run =M-x org-brain-polymode= or add =org-brain-polymode= to =org-brain-visualize-mode-hook=. After editing you can use =C-x C-s= (bound to =org-brain-polymode-save=) to save your changes. + ** Editing from =org-mode= You can edit =org-brain= entries directly from =org-mode=. You can use the default diff --git a/org-brain.el b/org-brain.el index 91c0b76..5cffa01 100644 --- a/org-brain.el +++ b/org-brain.el @@ -7,7 +7,7 @@ ;; URL: http://github.com/Kungsgeten/org-brain ;; Keywords: outlines hypermedia ;; Package-Requires: ((emacs "25.1") (org "9.2")) -;; Version: 0.93 +;; Version: 0.94 ;;; Commentary: @@ -2407,7 +2407,8 @@ Unless WANDER is t, `org-brain-stop-wandering' will be run." (run-hooks 'org-brain-after-visualize-hook)) (unless (eq major-mode 'org-brain-visualize-mode) (org-brain-visualize-mode)) - (goto-char entry-pos)) + (goto-char entry-pos) + (set-buffer-modified-p nil)) (unless nofocus (when org-brain--visualize-follow (org-brain-goto-current) @@ -3025,7 +3026,9 @@ Helper function for `org-brain-visualize'." (if-let ((text (org-brain-text entry))) (progn (setq text (string-trim text)) - (if (or (> (length text) 0) org-brain-show-full-entry) + (if (or (boundp org-brain-polymode) + org-brain-show-full-entry + (> (length text) 0)) (progn (insert "\n\n") (setq org-brain--vis-entry-text-marker (point-marker)) @@ -3179,6 +3182,49 @@ Return the position of ENTRY in the buffer." (define-obsolete-function-alias 'org-brain-visualize-remove-grandparent 'org-brain-hide-ancestor-level "0.5") +;;;;; Polymode + +;; This code has been adapted from Dustin Lacewell's project polybrain +;; Have a look at: https://github.com/dustinlacewell/polybrain.el/ + +(with-eval-after-load "polymode" + (define-hostmode org-brain-poly-hostmode + :mode 'org-brain-visualize-mode) + + (define-innermode org-brain-poly-innermode + :mode 'org-mode + :head-matcher "^[─-]\\{3\\} Entry [─-]+\n" + :tail-matcher "\\'" + :head-mode 'host + :tail-mode 'host) + + (define-polymode org-brain-polymode + :hostmode 'org-brain-poly-hostmode + :innermodes '(org-brain-poly-innermode) + (setq-local polymode-move-these-vars-from-old-buffer + (delq 'buffer-read-only polymode-move-these-vars-from-old-buffer))) + + (defun org-brain-polymode-save () + "Save entry text to the entry's file." + (interactive) + (when (buffer-modified-p) + (let ((text (save-excursion + (goto-char org-brain--vis-entry-text-marker) + (end-of-line) + (buffer-substring (point) (point-max))))) + (find-file (org-brain-entry-path org-brain--vis-entry)) + (seq-let (entry-min entry-max) (org-brain-text-positions org-brain--vis-entry) + (goto-char entry-min) + (delete-region entry-min entry-max) + (insert text) + (unless (looking-at-p "\n") + (insert "\n\n")) + (save-buffer) + (switch-to-buffer (other-buffer (current-buffer) 1)) + (set-buffer-modified-p nil))))) + + (define-key org-brain-polymode-map "\C-x\C-s" 'org-brain-polymode-save)) + ;;;; Brain link (defun org-brain-link-complete (&optional link-type)