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