[gedit-latex/doc-prefs] detect preferences in file modelines



commit cbe45a3f98660b83307974f2e8e983e88d2ff9d3
Author: John Stowers <john stowers gmail com>
Date:   Tue Aug 16 12:55:01 2011 +1200

    detect preferences in file modelines

 latex/latex/editor.py         |    3 +++
 latex/preferences/__init__.py |   28 ++++++++++++++++++++++++----
 test/article.tex              |    1 +
 test/chap3.tex                |    5 +++++
 4 files changed, 33 insertions(+), 4 deletions(-)
---
diff --git a/latex/latex/editor.py b/latex/latex/editor.py
index 3740251..9c9daef 100644
--- a/latex/latex/editor.py
+++ b/latex/latex/editor.py
@@ -259,6 +259,9 @@ class LaTeXEditor(Editor, IIssueHandler):
                 del self._document
             self._document = self._parser.parse(self.content, self._file, self)
 
+            # update document preferences
+            self._preferences.parse_content(self.content)
+
             if BENCHMARK: self._log.info("LaTeXParser.parse: %f" % (time.clock() - t))
 
             # create a copy that won't be expanded (e.g. for spell check)
diff --git a/latex/preferences/__init__.py b/latex/preferences/__init__.py
index c146f34..d847843 100644
--- a/latex/preferences/__init__.py
+++ b/latex/preferences/__init__.py
@@ -24,6 +24,7 @@ preferences
 
 from gi.repository import GObject, Gio, GLib
 
+import re
 import os.path
 import logging
 import ConfigParser
@@ -32,7 +33,7 @@ from ..util import singleton
 
 LOG = logging.getLogger(__name__)
 
-class _LatexConfigParser(ConfigParser.RawConfigParser):
+class _DocumentConfigParser(ConfigParser.RawConfigParser):
 
     SECTION = "LATEX"
 
@@ -110,19 +111,38 @@ class DocumentPreferences(_Preferences):
         self._sysprefs = Preferences()
         self._sysprefs.connect("preferences-changed", self._on_prefs_changed)
         self._file = file
-        self._cp = _LatexConfigParser(
+        self._cp = _DocumentConfigParser(
                         "%s/.%s.ini" % (file.dirname, file.basename))
 
+        self._re = re.compile("^\s*%+\s*gedit:(.*)\s*=\s*(.*)")
+        self._modelines = {}
+
     def _on_prefs_changed(self, p, key, value):
         self.emit("preferences-changed", key, value)
 
     def _is_docpref(self,key):
         return key.startswith("document-") and key in self.KEYS
 
+    def parse_content(self, content, max_lines=100):
+        """ Parses txt content from the document looking for modelines """
+        self._modelines = {}
+
+        i = 0
+        for l in content.splitlines():
+            if i > max_lines:
+                break
+            try:
+                key,val = self._re.match(l).groups()
+                LOG.debug("Detected preference modeline: %s = %s" % (key,val))
+                self._modelines[key.strip()] = val
+            except AttributeError:
+                pass
+            i = i+1
+
     def get(self, key):
         if self._is_docpref(key):
-            LOG.debug("Get document pref")
-            return self._cp.get(key)
+            LOG.debug("Get document pref: %s (modelines: %s)" % (key,",".join(self._modelines.keys())))
+            return self._modelines.get(key, self._cp.get(key))
         else:
             return self._sysprefs.get(key)
 
diff --git a/test/article.tex b/test/article.tex
index a0b0fc4..9598be4 100644
--- a/test/article.tex
+++ b/test/article.tex
@@ -40,6 +40,7 @@ I like \cite{dijkstra76}
 lets use a \testcite{dijkstra68}
 
 \include{chap2}
+\include{chap3}
 
 \bibliography{article}
 \bibliographystyle{IEEEtran}
diff --git a/test/chap3.tex b/test/chap3.tex
new file mode 100644
index 0000000..0cabb61
--- /dev/null
+++ b/test/chap3.tex
@@ -0,0 +1,5 @@
+% gedit:document-master-filename = article.tex
+
+\chapter{A Third Chapter}
+\section{Imma Let You Finish}
+This is the best \LaTeX plugin of all time.



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]