]> git.lyx.org Git - lyx.git/blob - src/FuncRequest.cpp
Clean includes using the output of iwyu tool
[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/docstring.h"
18 #include "support/lstrings.h"
19
20 #include <climits>
21 #include <iostream>
22 #include <sstream>
23 #include <vector>
24
25 using namespace std;
26 using namespace lyx::support;
27
28 namespace lyx {
29
30 FuncRequest const FuncRequest::unknown(LFUN_UNKNOWN_ACTION);
31 FuncRequest const FuncRequest::noaction(LFUN_NOACTION);
32
33 FuncRequest::FuncRequest(Origin o)
34         : action_(LFUN_NOACTION), origin_(o), view_origin_(nullptr), x_(0), y_(0),
35           button_(mouse_button::none), modifier_(NoModifier), allow_async_(true)
36 {}
37
38
39 FuncRequest::FuncRequest(FuncCode act, Origin o)
40         : action_(act), origin_(o), view_origin_(nullptr), x_(0), y_(0),
41         button_(mouse_button::none), modifier_(NoModifier), allow_async_(true)
42 {}
43
44
45 FuncRequest::FuncRequest(FuncCode act, docstring const & arg, Origin o)
46         : action_(act), argument_(arg), origin_(o), view_origin_(nullptr), x_(0), y_(0),
47           button_(mouse_button::none), modifier_(NoModifier), allow_async_(true)
48 {}
49
50
51 FuncRequest::FuncRequest(FuncCode act, string const & arg, Origin o)
52         : action_(act), argument_(from_utf8(arg)),
53           origin_(o), view_origin_(nullptr), x_(0), y_(0),
54           button_(mouse_button::none), modifier_(NoModifier), allow_async_(true)
55 {}
56
57
58 FuncRequest::FuncRequest(FuncCode act, int ax, int ay,
59                          mouse_button::state but, KeyModifier modifier, Origin o)
60         : action_(act), origin_(o), view_origin_(nullptr), x_(ax), y_(ay),
61           button_(but), modifier_(modifier), allow_async_(true)
62 {}
63
64
65 FuncRequest::FuncRequest(FuncRequest const & cmd, docstring const & arg, Origin o)
66         : action_(cmd.action()), argument_(arg),
67           origin_(o), view_origin_(nullptr), x_(cmd.x_), y_(cmd.y_),
68           button_(cmd.button_), modifier_(NoModifier), allow_async_(true)
69 {}
70
71
72 namespace {
73
74 // Extracts arguments from str into args. Arguments are delimited by
75 // whitespace or by double quotes.
76 // We extract at most max + 1 arguments, treating args[max] as
77 // continuing to eol.
78 void splitArg(vector<string> & args, string const & str,
79                 unsigned int max = UINT_MAX)
80 {
81         istringstream is(str);
82         while (is) {
83                 if (args.size() == max) {
84                         string s;
85                         getline(is, s);
86                         args.push_back(trim(s));
87                         return;
88                 }
89
90                 char c;
91                 string s;
92                 is >> c;
93                 if (is) {
94                         if (c == '"')
95                                 // get quote delimited argument
96                                 getline(is, s, '"');
97                         else {
98                                 // get whitespace delimited argument
99                                 is.putback(c);
100                                 is >> s;
101                         }
102                         args.push_back(s);
103                 }
104         }
105 }
106
107 } // namespace
108
109 string FuncRequest::getArg(unsigned int i) const
110 {
111         vector<string> args;
112         splitArg(args, to_utf8(argument_));
113         return i < args.size() ? args[i] : string();
114 }
115
116
117 string FuncRequest::getLongArg(unsigned int i) const
118 {
119         vector<string> args;
120         splitArg(args, to_utf8(argument_), i);
121         return i < args.size() ? args[i] : string();
122 }
123
124 bool operator==(FuncRequest const & lhs, FuncRequest const & rhs)
125 {
126         return lhs.action() == rhs.action() && lhs.argument() == rhs.argument();
127 }
128
129
130 ostream & operator<<(ostream & os, FuncRequest const & cmd)
131 {
132         return os
133                 << " action: " << cmd.action()
134                 << " [" << lyxaction.getActionName(cmd.action()) << "] "
135                 << " arg: '" << to_utf8(cmd.argument()) << "'"
136                 << " x: " << cmd.x()
137                 << " y: " << cmd.y();
138 }
139
140
141 LyXErr & operator<<(LyXErr &l, FuncRequest const &fr)
142 {
143         ostringstream oss;
144         oss << fr;
145         return l << oss.str();
146 }
147
148 } // namespace lyx