Skip to content

Commit

Permalink
Add polymode support.
Browse files Browse the repository at this point in the history
  • Loading branch information
Kungsgeten committed Jun 3, 2020
1 parent c9c6995 commit 49e5231
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 7 deletions.
21 changes: 17 additions & 4 deletions README.org
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
52 changes: 49 additions & 3 deletions org-brain.el
Original file line number Diff line number Diff line change
Expand Up @@ -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:

Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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)
Expand Down

2 comments on commit 49e5231

@Kungsgeten
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Kungsgeten
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.