Skip to content

Commit

Permalink
Improve behavior for opening file buffers
Browse files Browse the repository at this point in the history
  • Loading branch information
jlumpe committed Nov 9, 2019
1 parent d7e8682 commit b7ed385
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 15 deletions.
35 changes: 25 additions & 10 deletions pyorg.el
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,31 @@
(org-export-to-file 'json dest)))


(defun pyorg-open-org-file (path &optional focus)
"Open an org file for interactive editing/viewing.
PATH is the path to the file to open.
If FOCUS is non-nil the window will be given active focus.
This function should switch to an existing buffer visiting the file if it
exists, rather than opening a new one."
(find-file path)
(if focus (x-focus-frame nil)))
(defun pyorg-switch-to-file-buffer (file &optional focus)
"Switch to a buffer visiting FILE and display it in a window.
FILE is the path to the file to open. If not absolute it is taken to be relative
to `org-directory'.
If FOCUS is non-nil set the window system's focus to the
active frame.
Uses an existing buffer visiting the file if possible, otherwise
creates one. If this buffer is already displayed in some window
select it, otherwise open it in a new window (where?). The window
(and its frame, if different than the current one) are then given
focus.
Returns the selected buffer."
(setq file (expand-file-name file org-directory))
(let ((buffer (find-file-noselect file)))
(pop-to-buffer
buffer
'(display-buffer-reuse-window
(inhibit-same-window . nil)
(inhibit-switch-frame . nil)
(reusable-frames . t)))
(if focus (x-focus-frame nil))
buffer))


(provide 'pyorg)
Expand Down
24 changes: 24 additions & 0 deletions pyorg/elisp.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,3 +47,27 @@ def export_org_file(emacs, file, dest):
form = export_org_file_el(file, dest)
emacs.eval(form)


def switch_to_file_buffer(emacs, file, focus=False):
"""Get or create a buffer visiting a file and display it.
Uses an existing buffer visiting the file if possible, otherwise
creates one. If this buffer is already displayed in some window
select it, otherwise open it in a new window (where?). The window
(and its frame, if different than the current one) are then given
focus.
Parameters
----------
emacs : emacs.emacs.Emacs
file : str
Absolute path to file to open.
focus : bool
Switch window system focus to the active Emacs frame.
Raises
------
emacs.emacs.EmacsException
"""
form = E.pyorg_switch_to_file_buffer(str(file), bool(focus))
emacs.eval(form)
20 changes: 15 additions & 5 deletions pyorg/interface.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from emacs.elisp import E
from .files import OrgDirectory, DirectFileLoader
import pyorg.elisp as pyel


class Org:
Expand Down Expand Up @@ -87,14 +88,23 @@ def read_org_file(self, file, raw=None):
"""
return self.loader.load_file(file, raw=raw)

def open_org_file(self, path, focus=False):
def open_org_file(self, file, focus=False):
"""Open an org file in the org directory for editing in Emacs.
Parameters
----------
path : str or pathlib.Path
File path relative to org directory.
file : str or pathlib.Path
Path to file to open. If not absolute it is taken to be relative to
:attr:`orgdir`.
focus : bool
Switch window/input focus to opened buffer.
Switch window system focus to the active Emacs frame.
Raises
------
emacs.emacs.EmacsException
FileNotFoundError
"""
self.emacs.eval(E.pyorg_open_file(str(path), bool(focus)))
file = self.orgdir.get_abs_path(file, outside_ok=True)
if not file.is_file():
raise FileNotFoundError(file)
pyel.switch_to_file_buffer(self.emacs, str(file), focus)

0 comments on commit b7ed385

Please sign in to comment.