This repository has been archived by the owner on Aug 14, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #225 from thomasjo/evince
Add support for Evince with SyncTex. Resolves #162
- Loading branch information
Showing
3 changed files
with
125 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
/** @babel */ | ||
|
||
import Opener from '../opener' | ||
import dbus from 'dbus-native' | ||
import url from 'url' | ||
|
||
const EVINCE_OBJECT = '/org/gnome/evince/Evince' | ||
const EVINCE_APPLICATION_INTERFACE = 'org.gnome.evince.Application' | ||
|
||
const DAEMON_SERVICE = 'org.gnome.evince.Daemon' | ||
const DAEMON_OBJECT = '/org/gnome/evince/Daemon' | ||
const DAEMON_INTERFACE = 'org.gnome.evince.Daemon' | ||
|
||
const WINDOW_INTERFACE = 'org.gnome.evince.Window' | ||
|
||
const GTK_APPLICATION_OBJECT = '/org/gtk/Application/anonymous' | ||
const GTK_APPLICATION_INTERFACE = 'org.freedesktop.Application' | ||
|
||
function syncSource (uri, point) { | ||
atom.workspace.open(url.parse(uri).pathname).then(editor => editor.setCursorBufferPosition(point)) | ||
} | ||
|
||
function getInterface (bus, serviceName, objectPath, interfaceName) { | ||
return new Promise((resolve, reject) => { | ||
bus.getInterface(serviceName, objectPath, interfaceName, (error, interfaceInstance) => { | ||
if (error) { | ||
reject(error) | ||
} else { | ||
resolve(interfaceInstance) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
function getWindowList (evinceApplication) { | ||
return new Promise((resolve, reject) => { | ||
evinceApplication.GetWindowList((error, windowNames) => { | ||
if (error) { | ||
reject(error) | ||
} else { | ||
resolve(windowNames) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
function findDocument (daemon, filePath) { | ||
return new Promise((resolve, reject) => { | ||
const uri = url.format({ protocol: 'file:', slashes: true, pathname: filePath }) | ||
daemon.FindDocument(uri, true, (error, documentName) => { | ||
if (error) { | ||
reject(error) | ||
} else { | ||
resolve(documentName) | ||
} | ||
}) | ||
}) | ||
} | ||
|
||
export default class EvinceOpener extends Opener { | ||
constructor () { | ||
super() | ||
this.bus = dbus.sessionBus() | ||
this.windows = {} | ||
} | ||
|
||
async getWindow (filePath, texPath) { | ||
if (this.windows[texPath]) return this.windows[texPath] | ||
|
||
// First get the Evince daemon interface so we can find the internal document name | ||
const daemon = await getInterface(this.bus, DAEMON_SERVICE, DAEMON_OBJECT, DAEMON_INTERFACE) | ||
const documentName = await findDocument(daemon, filePath) | ||
|
||
// Get the application interface and get the window list of the application | ||
const evinceApplication = await getInterface(this.bus, documentName, EVINCE_OBJECT, EVINCE_APPLICATION_INTERFACE) | ||
const windowNames = await getWindowList(evinceApplication) | ||
|
||
// Get the window interface of the of the first (only) window and get the | ||
// GTK/FreeDesktop application interface so we can activate the window | ||
const interfaces = { | ||
evinceWindow: await getInterface(this.bus, documentName, windowNames[0], WINDOW_INTERFACE), | ||
gtkApplication: await getInterface(this.bus, documentName, GTK_APPLICATION_OBJECT, GTK_APPLICATION_INTERFACE) | ||
} | ||
|
||
interfaces.evinceWindow.on('SyncSource', syncSource) | ||
interfaces.evinceWindow.on('Closed', () => delete this.windows[texPath]) | ||
this.windows[texPath] = interfaces | ||
|
||
// This seems to help with future syncs | ||
interfaces.evinceWindow.SyncView(texPath, [0, 0], 0) | ||
|
||
return interfaces | ||
} | ||
|
||
open (filePath, texPath, lineNumber, callback) { | ||
this.getWindow(filePath, texPath) | ||
.then(interfaces => { | ||
if (!this.shouldOpenInBackground()) { | ||
interfaces.gtkApplication.Activate({}) | ||
} | ||
// SyncView seems to want to activate the window sometimes | ||
interfaces.evinceWindow.SyncView(texPath, [lineNumber, 0], 0) | ||
if (callback) callback(0) | ||
}, error => { | ||
if (callback) callback(error) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters