]> git.lyx.org Git - lyx.git/blob - src/Mover.h
fix a visual cursor edge-case:
[lyx.git] / src / Mover.h
1 // -*- C++ -*-
2 /**
3  * \file Mover.h
4  * This file is part of LyX, the document processor.
5  * Licence details can be found in the file COPYING.
6  *
7  * \author Angus Leeming
8  *
9  * Full author contact details are available in file CREDITS.
10  */
11
12 #ifndef MOVER_H
13 #define MOVER_H
14
15 #include <map>
16 #include <string>
17
18
19 namespace lyx {
20
21 namespace support { class FileName; }
22
23 /**
24  *  Utility to copy a file of a specified format from one place to another.
25  *  This base class simply invokes the command support::copy().
26  */
27 class Mover
28 {
29 public:
30         virtual ~Mover() {}
31
32         /** Copy file @c from to @c to.
33          *  This version should be used to copy files from the original
34          *  location to the temporary directory, since @c to and @c latex
35          *  would be equal in this case.
36          *  \returns true if successful.
37          */
38         bool
39         copy(support::FileName const & from, support::FileName const & to) const;
40
41         /** Copy file @c from to @c to.
42          *  \see SpecialisedMover::SpecialisedMover() for an explanation of
43          *  @c latex.
44          *  This version should be used to copy files from the temporary
45          *  directory to the export location, since @c to and @c latex may
46          *  not be equal in this case.
47          *  \returns true if successful.
48          */
49         bool
50         copy(support::FileName const & from, support::FileName const & to,
51              std::string const & latex) const
52         {
53                 return do_copy(from, to, latex);
54         }
55
56         /** Rename file @c from as @c to.
57          *  This version should be used to move files from the original
58          *  location to the temporary directory, since @c to and @c latex
59          *  would be equal in this case.
60          *  \returns true if successful.
61          */
62         bool
63         rename(support::FileName const & from, support::FileName const & to) const;
64
65         /** Rename file @c from as @c to.
66          *  \see SpecialisedMover::SpecialisedMover() for an explanation of
67          *  @c latex.
68          *  This version should be used to move files from the temporary
69          *  directory to the export location, since @c to and @c latex may
70          *  not be equal in this case.
71          *  \returns true if successful.
72          */
73         bool
74         rename(support::FileName const & from, support::FileName const & to,
75                std::string const & latex) const
76         {
77                 return do_rename(from, to, latex);
78         }
79
80 protected:
81         virtual bool
82         do_copy(support::FileName const & from, support::FileName const & to,
83                 std::string const &) const;
84
85         virtual bool
86         do_rename(support::FileName const & from, support::FileName const & to,
87                   std::string const &) const;
88 };
89
90
91 /**
92  *  Specialisation of the Mover concept that uses an external command
93  *  to copy a file.
94  *
95  *  For example, an Xfig .fig file can contain references to external
96  *  picture files. If such a reference has a relative path, then the
97  *  copied .fig file will require a transformation of the picture file
98  *  reference if it is to be found by Xfig.
99  */
100 class SpecialisedMover : public Mover
101 {
102 public:
103         SpecialisedMover() {}
104
105         virtual ~SpecialisedMover() {}
106
107         /** @c command should be of the form
108          *  <code>
109          *      python $$s/scripts/fig_copy.py $$i $$o $$l
110          *  </code>
111          *  where $$s is a placeholder for the lyx support directory,
112          *        $$i is a placeholder for the name of the file to be moved,
113          *        $$o is a placeholder for the name of the file after moving,
114          *        $$l is a placeholder for the name of the file after moving,
115          *        suitable as argument to a latex include command. This is
116          *        either an absolute filename or relative to the master
117          *        document.
118          *        $$o and $$l can only differ if the file is copied from the
119          *        temporary directory to the export location. If it is copied
120          *        from the original location to the temporary directory, they
121          *        are the same, so $$l may be ommitted in this case.
122          */
123         SpecialisedMover(std::string const & command)
124                 : command_(command) {}
125
126         /// The template used to launch the external command.
127         std::string const & command() const { return command_; }
128
129 private:
130         virtual bool
131         do_copy(support::FileName const & from, support::FileName const & to,
132                 std::string const & latex) const;
133
134         virtual bool
135         do_rename(support::FileName const & from, support::FileName const & to,
136                   std::string const & latex) const;
137
138         std::string command_;
139 };
140
141
142 /**
143  *  Manage the store of (Mover)s.
144  */
145 class Movers
146 {
147 public:
148         /** Register a specialised @c command to be used to copy a file
149          *  of format @c fmt.
150          */
151         void set(std::string const & fmt, std::string const & command);
152
153         /// @c returns the Mover registered for format @c fmt.
154         Mover const & operator()(std::string  const & fmt) const;
155
156         /** @returns the command template if @c fmt 'finds' a
157          *  SpecialisedMover. Otherwise, returns an empty string.
158          */
159         std::string const command(std::string  const & fmt) const;
160
161 private:
162         typedef std::map<std::string, SpecialisedMover> SpecialsMap;
163
164 public:
165         typedef SpecialsMap::const_iterator const_iterator;
166         const_iterator begin() const { return specials_.begin(); }
167         const_iterator end() const { return specials_.end(); }
168
169 private:
170         Mover default_;
171         SpecialsMap specials_;
172 };
173
174
175 extern Movers & theMovers();
176 /// @c returns the Mover registered for format @c fmt.
177 extern Mover const & getMover(std::string  const & fmt);
178 /** Register a specialised @c command to be used to copy a file
179  *  of format @c fmt.
180  */
181 extern void setMover(std::string const & fmt, std::string const & command);
182 extern Movers & theSystemMovers();
183
184
185 } // namespace lyx
186
187 #endif // MOVER_H