commit 98621be1ba2a00a931e89020a164b4b8b54c597f
Author: Juergen Spitzmueller <sp...@lyx.org>
Date:   Mon May 21 13:43:16 2018 +0200

    Support \cite command in tufte classes
    
    This is an extra command used for sidenote citations. Since we do not
    provide \cite with natbib (which tufte uses) as a choice, we use the
    new AddToCiteEngine feature to add it.
    
    File format change.
    
    Fixes: #11150
---
 development/FORMAT                |    4 ++
 lib/examples/de/tufte-handout.lyx |   18 ++++++++---
 lib/examples/tufte-book.lyx       |   26 ++++++++++++++---
 lib/examples/tufte-handout.lyx    |   16 +++++++---
 lib/layouts/tufte-book.layout     |   14 +++++++++
 lib/lyx2lyx/lyx_2_4.py            |   56 ++++++++++++++++++++++++++++++++++--
 src/version.h                     |    4 +-
 7 files changed, 117 insertions(+), 21 deletions(-)

diff --git a/development/FORMAT b/development/FORMAT
index d443601..170d087 100644
--- a/development/FORMAT
+++ b/development/FORMAT
@@ -7,6 +7,10 @@ changes happened in particular if possible. A good example 
would be
 
 -----------------------
 
+2018-05-21  Jürgen Spitzmüller <sp...@lyx.org>
+       * format incremented to 553: Support \cite command in tufte classes.
+         This builds on an extension of the natbib CiteEngine by tufte.
+
 2018-05-10  Jürgen Spitzmüller <sp...@lyx.org>
        * format incremented to 552: support for float alignment
          - add buffer param \float_alignment [class|left|center|right]
diff --git a/lib/examples/de/tufte-handout.lyx 
b/lib/examples/de/tufte-handout.lyx
index 49fd84e..decd0cc 100644
--- a/lib/examples/de/tufte-handout.lyx
+++ b/lib/examples/de/tufte-handout.lyx
@@ -1,5 +1,5 @@
-#LyX 2.3 created this file. For more info see http://www.lyx.org/
-\lyxformat 544
+#LyX 2.4 created this file. For more info see https://www.lyx.org/
+\lyxformat 553
 \begin_document
 \begin_header
 \save_transient_properties true
@@ -10,7 +10,7 @@
 \language ngerman
 \language_package default
 \inputencoding auto
-\fontencoding global
+\fontencoding auto
 \font_roman "default" "default"
 \font_sans "helvet" "default"
 \font_typewriter "default" "default"
@@ -28,6 +28,8 @@
 \output_sync 0
 \bibtex_command bibtex
 \index_command default
+\float_placement class
+\float_alignment class
 \paperfontsize default
 \spacing single
 \use_hyperref false
@@ -43,8 +45,8 @@
 \use_package stackrel 1
 \use_package stmaryrd 1
 \use_package undertilde 1
-\cite_engine basic
-\cite_engine_type default
+\cite_engine natbib
+\cite_engine_type authoryear
 \biblio_style plain
 \use_bibtopic false
 \use_indices false
@@ -556,6 +558,8 @@ Leider ist die Zählung im \SpecialChar LyX
 
  
 \begin_inset Float marginfigure
+placement document
+alignment document
 wide false
 sideways false
 status open
@@ -639,6 +643,8 @@ reference "fig:Textabbildung"
  eine, die dies nicht tut (normale Gleitabbildung).
  
 \begin_inset Float figure
+placement document
+alignment document
 wide true
 sideways false
 status open
@@ -708,6 +714,7 @@ name "fig:Ganzabbildung"
 \begin_layout Standard
 \begin_inset Float figure
 placement h
+alignment document
 wide false
 sideways false
 status open
@@ -802,6 +809,7 @@ formal
 \begin_layout Standard
 \begin_inset Float table
 placement ht
+alignment document
 wide false
 sideways false
 status open
diff --git a/lib/examples/tufte-book.lyx b/lib/examples/tufte-book.lyx
index 952a5c4..d164277 100644
--- a/lib/examples/tufte-book.lyx
+++ b/lib/examples/tufte-book.lyx
@@ -1,5 +1,5 @@
-#LyX 2.3 created this file. For more info see http://www.lyx.org/
-\lyxformat 544
+#LyX 2.4 created this file. For more info see https://www.lyx.org/
+\lyxformat 553
 \begin_document
 \begin_header
 \save_transient_properties true
@@ -10,7 +10,7 @@
 \language english
 \language_package default
 \inputencoding auto
-\fontencoding global
+\fontencoding auto
 \font_roman "default" "default"
 \font_sans "default" "default"
 \font_typewriter "default" "default"
@@ -28,6 +28,8 @@
 \output_sync 0
 \bibtex_command bibtex
 \index_command default
+\float_placement class
+\float_alignment class
 \paperfontsize default
 \spacing single
 \use_hyperref true
@@ -52,8 +54,8 @@
 \use_package stackrel 1
 \use_package stmaryrd 1
 \use_package undertilde 1
-\cite_engine basic
-\cite_engine_type default
+\cite_engine natbib
+\cite_engine_type authoryear
 \biblio_style plain
 \use_bibtopic false
 \use_indices false
@@ -367,6 +369,8 @@ Tufte uses ordinary figure floats such as the following:
 
 \begin_layout Standard
 \begin_inset Float figure
+placement document
+alignment document
 wide false
 sideways false
 status open
@@ -437,6 +441,8 @@ reference "fig:margin-figure"
 
 \begin_layout Standard
 \begin_inset Float marginfigure
+placement document
+alignment document
 wide false
 sideways false
 status open
@@ -563,6 +569,8 @@ reference "fig:full-width-figure"
 
 \begin_layout Standard
 \begin_inset Float figure
+placement document
+alignment document
 wide true
 sideways false
 status open
@@ -928,6 +936,8 @@ Visualizing Rotation
 
 \begin_layout Standard
 \begin_inset Float marginfigure
+placement document
+alignment document
 wide false
 sideways false
 status open
@@ -986,6 +996,8 @@ A graph of
 
 \begin_layout Standard
 \begin_inset Float marginfigure
+placement document
+alignment document
 wide false
 sideways false
 status open
@@ -1302,6 +1314,8 @@ washer
 
 \begin_layout Standard
 \begin_inset Float marginfigure
+placement document
+alignment document
 wide false
 sideways false
 status open
@@ -1417,6 +1431,8 @@ Then, by summing the volumes of all the washers between 
points
 \begin_layout Standard
 In the case of our quadratic drill bit::
 \begin_inset Float marginfigure
+placement document
+alignment document
 wide false
 sideways false
 status open
diff --git a/lib/examples/tufte-handout.lyx b/lib/examples/tufte-handout.lyx
index a8a4c2e..71c135d 100644
--- a/lib/examples/tufte-handout.lyx
+++ b/lib/examples/tufte-handout.lyx
@@ -1,5 +1,5 @@
-#LyX 2.3 created this file. For more info see http://www.lyx.org/
-\lyxformat 544
+#LyX 2.4 created this file. For more info see https://www.lyx.org/
+\lyxformat 553
 \begin_document
 \begin_header
 \save_transient_properties true
@@ -10,7 +10,7 @@
 \language english
 \language_package default
 \inputencoding auto
-\fontencoding global
+\fontencoding auto
 \font_roman "default" "default"
 \font_sans "helvet" "default"
 \font_typewriter "default" "default"
@@ -52,8 +52,8 @@
 \use_package stackrel 1
 \use_package stmaryrd 1
 \use_package undertilde 1
-\cite_engine basic
-\cite_engine_type default
+\cite_engine natbib
+\cite_engine_type authoryear
 \biblio_style plain
 \use_bibtopic false
 \use_indices false
@@ -554,6 +554,8 @@ Note: unfortunately, the counting inside the \SpecialChar 
LyX
 
  
 \begin_inset Float marginfigure
+placement document
+alignment document
 wide false
 sideways false
 status open
@@ -664,6 +666,8 @@ reference "fig:textfig"
  is an example of the normal figure float.
  
 \begin_inset Float figure
+placement document
+alignment document
 wide true
 sideways false
 status open
@@ -740,6 +744,7 @@ name "fig:fullfig"
 \begin_layout Standard
 \begin_inset Float figure
 placement h
+alignment document
 wide false
 sideways false
 status open
@@ -834,6 +839,7 @@ formal
 \begin_layout Standard
 \begin_inset Float table
 placement ht
+alignment document
 wide false
 sideways false
 status open
diff --git a/lib/layouts/tufte-book.layout b/lib/layouts/tufte-book.layout
index 0bf5fd8..1d41ffd 100644
--- a/lib/layouts/tufte-book.layout
+++ b/lib/layouts/tufte-book.layout
@@ -17,6 +17,20 @@ ClassOptions
        Other           nohyper
 end
 
+# Tufte uses \cite for margin note citations
+AddToCiteEngine authoryear
+       cite[][]
+End
+
+CiteFormat authoryear
+       _sidenote Sidenote
+       _side sidenote
+       _fullcite bibl. entry
+       _bibentry Full bibliography entry.
+       # "Margin: Full bibliography entry." (dialog) / "Margin: Key 
(bibliography entry)"
+       cite {%dialog%[[%_sidenote%]][[%_side%]]}: 
{%dialog%[[%_bibentry%]][[{%label%[[%!fullcite%]][[%key%%!nextkey% 
(%_fullcite%)]]}]]}
+End
+
 Style Standard
        Margin          Static
        LatexType       Paragraph
diff --git a/lib/lyx2lyx/lyx_2_4.py b/lib/lyx2lyx/lyx_2_4.py
index fae273d..2b03ada 100644
--- a/lib/lyx2lyx/lyx_2_4.py
+++ b/lib/lyx2lyx/lyx_2_4.py
@@ -24,12 +24,13 @@ import sys, os
 
 # Uncomment only what you need to import, please.
 
-from parser_tools import (find_end_of_inset, find_end_of_layout, find_token, 
get_bool_value, get_value) 
+from parser_tools import (find_end_of_inset, find_end_of_layout, find_token,
+get_bool_value, get_value, get_quoted_value) 
 #    del_token, del_value, del_complete_lines,
 #    find_complete_lines, find_end_of, 
 #    find_re, find_substring, find_token_backwards,
-#    get_containing_inset, get_containing_layout, get_value,
-#    get_quoted_value, is_in_inset, set_bool_value
+#    get_containing_inset, get_containing_layout,
+#    is_in_inset, set_bool_value
 #    find_tokens, find_token_exact, check_token, get_option_value
 
 from lyx2lyx_tools import (put_cmd_in_ert, add_to_preamble)
@@ -281,6 +282,51 @@ def revert_floatalignment(document):
         i = j 
 
 
+def revert_tuftecite(document):
+    " Revert \cite commands in tufte classes "
+
+    tufte = ["tufte-book", "tufte-handout"]
+    if document.textclass not in tufte:
+        return
+
+    i = 0
+    while (True):
+        i = find_token(document.body, "\\begin_inset CommandInset citation", i)
+        if i == -1:
+            break
+        j = find_end_of_inset(document.body, i)
+        if j == -1:
+            document.warning("Can't find end of citation inset at line %d!!" 
%(i))
+            i += 1
+            continue
+        k = find_token(document.body, "LatexCommand", i, j)
+        if k == -1:
+            document.warning("Can't find LatexCommand for citation inset at 
line %d!" %(i))
+            i = j + 1
+            continue
+        cmd = get_value(document.body, "LatexCommand", k)
+        if cmd != "cite":
+            i = j + 1
+            continue
+        pre = get_quoted_value(document.body, "before", i, j)
+        post = get_quoted_value(document.body, "after", i, j)
+        key = get_quoted_value(document.body, "key", i, j)
+        if not key:
+            document.warning("Citation inset at line %d does not have a key!" 
%(i))
+            key = "???"
+        # Replace command with ERT
+        res = "\\cite"
+        if pre:
+            res += "[" + pre + "]"
+        if post:
+            res += "[" + post + "]"
+        elif pre:
+            res += "[]"
+        res += "{" + key + "}"
+        document.body[i:j+1] = put_cmd_in_ert([res])
+        i = j + 1
+
+
 ##
 # Conversion hub
 #
@@ -294,10 +340,12 @@ convert = [
            [549, []],
            [550, [convert_fontenc]],
            [551, []],
-           [552, []]
+           [552, []],
+           [553, []]
           ]
 
 revert =  [
+           [552, [revert_tuftecite]],
            [551, [revert_floatpclass, revert_floatalignment]],
            [550, [revert_nospellcheck]],
            [549, [revert_fontenc]],
diff --git a/src/version.h b/src/version.h
index c2dd25a..1b93a17 100644
--- a/src/version.h
+++ b/src/version.h
@@ -32,8 +32,8 @@ extern char const * const lyx_version_info;
 
 // Do not remove the comment below, so we get merge conflict in
 // independent branches. Instead add your own.
-#define LYX_FORMAT_LYX 552 // spitz: float alignment
-#define LYX_FORMAT_TEX2LYX 552
+#define LYX_FORMAT_LYX 553 // spitz: tufte \cite
+#define LYX_FORMAT_TEX2LYX 553
 
 #if LYX_FORMAT_TEX2LYX != LYX_FORMAT_LYX
 #ifndef _MSC_VER

Reply via email to