]> git.lyx.org Git - lyx.git/blob - src/FuncRequest.cpp
Rename LATEX debug level to OUTFILE and use it for DocBook, HTML, and XML messages.
[lyx.git] / src / FuncRequest.cpp
1 /**
2  * \file FuncRequest.cpp
3  * This file is part of LyX, the document processor.
4  * Licence details can be found in the file COPYING.
5  *
6  * \author André Pönitz
7  *
8  * Full author contact details are available in file CREDITS.
9  */
10
11 #include <config.h>
12
13 #include "FuncRequest.h"
14 #include "LyXAction.h"
15
16 #include "support/debug.h"
17 #include "support/lstrings.h"
18
19 #include <climits>
20 #include <iostream>
21 #include <sstream>
22 #include <vector>
23
24 using namespace std;
25 using namespace lyx::support;
26
27 namespace lyx {
28
29 FuncRequest const FuncRequest::unknown(LFUN_UNKNOWN_ACTION);
30 FuncRequest const FuncRequest::noaction(LFUN_NOACTION);
31
32 FuncRequest::FuncRequest(Origin o)
33         : origin_(o)
34 {}
35
36
37 FuncRequest::FuncRequest(FuncCode act, Origin o)
38         : action_(act), origin_(o)
39 {}
40
41
42 FuncRequest::FuncRequest(FuncCode act, docstring const & arg, Origin o)
43         : action_(act), argument_(arg), origin_(o)
44 {}
45
46
47 FuncRequest::FuncRequest(FuncCode act, string const & arg, Origin o)
48         : FuncRequest(act, from_utf8(arg), o)
49 {}
50
51
52 FuncRequest::FuncRequest(FuncCode act, int ax, int ay,
53                          mouse_button::state button, KeyModifier modifier, Origin o)
54         : action_(act), origin_(o),
55           x_(ax), y_(ay), button_(button), modifier_(modifier)
56 {}
57
58
59 FuncRequest::FuncRequest(FuncRequest const & cmd, docstring const & arg, Origin o)
60         : action_(cmd.action()), argument_(arg), origin_(o),
61           x_(cmd.x_), y_(cmd.y_), button_(cmd.button_)
62 {}
63
64
65 namespace {
66
67 // Extracts arguments from str into args. Arguments are delimited by
68 // whitespace or by double quotes.
69 // We extract at most max + 1 arguments, treating args[max] as
70 // continuing to eol.
71 void splitArg(vector<string> & args, string const & str,
72                 unsigned int max = UINT_MAX)
73 {
74         istringstream is(str);
75         while (is) {
76                 if (args.size() == max) {
77                         string s;
78                         getline(is, s);
79                         args.push_back(trim(s));
80                         return;
81                 }
82
83                 char c;
84                 string s;
85                 is >> c;
86                 if (is) {
87                         if (c == '"')
88                                 // get quote delimited argument
89                                 getline(is, s, '"');
90                         else {
91                                 // get whitespace delimited argument
92                                 is.putback(c);
93                                 is >> s;
94                         }
95                         args.push_back(s);
96                 }
97         }
98 }
99
100 } // namespace
101
102 string FuncRequest::getArg(unsigned int i) const
103 {
104         vector<string> args;
105         splitArg(args, to_utf8(argument_));
106         return i < args.size() ? args[i] : string();
107 }
108
109
110 string FuncRequest::getLongArg(unsigned int i) const
111 {
112         vector<string> args;
113         splitArg(args, to_utf8(argument_), i);
114         return i < args.size() ? args[i] : string();
115 }
116
117 bool operator==(FuncRequest const & lhs, FuncRequest const & rhs)
118 {
119         return lhs.action() == rhs.action() && lhs.argument() == rhs.argument();
120 }
121
122
123 ostream & operator<<(ostream & os, FuncRequest const & cmd)
124 {
125         return os
126                 << " action: " << cmd.action()
127                 << " [" << lyxaction.getActionName(cmd.action()) << "] "
128                 << " arg: '" << to_utf8(cmd.argument()) << "'"
129                 << " x: " << cmd.x()
130                 << " y: " << cmd.y();
131 }
132
133
134 LyXErr & operator<<(LyXErr &l, FuncRequest const &fr)
135 {
136         ostringstream oss;
137         oss << fr;
138         return l << oss.str();
139 }
140
141 } // namespace lyx