qscilexerbash.cpp 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. // This module implements the QsciLexerBash class.
  2. //
  3. // Copyright (c) 2017 Riverbank Computing Limited <info@riverbankcomputing.com>
  4. //
  5. // This file is part of QScintilla.
  6. //
  7. // This file may be used under the terms of the GNU General Public License
  8. // version 3.0 as published by the Free Software Foundation and appearing in
  9. // the file LICENSE included in the packaging of this file. Please review the
  10. // following information to ensure the GNU General Public License version 3.0
  11. // requirements will be met: http://www.gnu.org/copyleft/gpl.html.
  12. //
  13. // If you do not wish to use this file under the terms of the GPL version 3.0
  14. // then you may purchase a commercial license. For more information contact
  15. // info@riverbankcomputing.com.
  16. //
  17. // This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
  18. // WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  19. #include "Qsci/qscilexerbash.h"
  20. #include <qcolor.h>
  21. #include <qfont.h>
  22. #include <qsettings.h>
  23. // The ctor.
  24. QsciLexerBash::QsciLexerBash(QObject *parent)
  25. : QsciLexer(parent), fold_comments(false), fold_compact(true)
  26. {
  27. }
  28. // The dtor.
  29. QsciLexerBash::~QsciLexerBash()
  30. {
  31. }
  32. // Returns the language name.
  33. const char *QsciLexerBash::language() const
  34. {
  35. return "Bash";
  36. }
  37. // Returns the lexer name.
  38. const char *QsciLexerBash::lexer() const
  39. {
  40. return "bash";
  41. }
  42. // Return the style used for braces.
  43. int QsciLexerBash::braceStyle() const
  44. {
  45. return Operator;
  46. }
  47. // Return the string of characters that comprise a word.
  48. const char *QsciLexerBash::wordCharacters() const
  49. {
  50. return "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789_$@%&";
  51. }
  52. // Returns the foreground colour of the text for a style.
  53. QColor QsciLexerBash::defaultColor(int style) const
  54. {
  55. switch (style)
  56. {
  57. case Default:
  58. return QColor(0x80,0x80,0x80);
  59. case Error:
  60. case Backticks:
  61. return QColor(0xff,0xff,0x00);
  62. case Comment:
  63. return QColor(0x00,0x7f,0x00);
  64. case Number:
  65. return QColor(0x00,0x7f,0x7f);
  66. case Keyword:
  67. return QColor(0x00,0x00,0x7f);
  68. case DoubleQuotedString:
  69. case SingleQuotedString:
  70. case SingleQuotedHereDocument:
  71. return QColor(0x7f,0x00,0x7f);
  72. case Operator:
  73. case Identifier:
  74. case Scalar:
  75. case ParameterExpansion:
  76. case HereDocumentDelimiter:
  77. return QColor(0x00,0x00,0x00);
  78. }
  79. return QsciLexer::defaultColor(style);
  80. }
  81. // Returns the end-of-line fill for a style.
  82. bool QsciLexerBash::defaultEolFill(int style) const
  83. {
  84. switch (style)
  85. {
  86. case SingleQuotedHereDocument:
  87. return true;
  88. }
  89. return QsciLexer::defaultEolFill(style);
  90. }
  91. // Returns the font of the text for a style.
  92. QFont QsciLexerBash::defaultFont(int style) const
  93. {
  94. QFont f;
  95. switch (style)
  96. {
  97. case Comment:
  98. #if defined(Q_OS_WIN)
  99. f = QFont("Comic Sans MS",9);
  100. #elif defined(Q_OS_MAC)
  101. f = QFont("Comic Sans MS", 12);
  102. #else
  103. f = QFont("Bitstream Vera Serif",9);
  104. #endif
  105. break;
  106. case Keyword:
  107. case Operator:
  108. f = QsciLexer::defaultFont(style);
  109. f.setBold(true);
  110. break;
  111. case DoubleQuotedString:
  112. case SingleQuotedString:
  113. #if defined(Q_OS_WIN)
  114. f = QFont("Courier New",10);
  115. #elif defined(Q_OS_MAC)
  116. f = QFont("Courier", 12);
  117. #else
  118. f = QFont("Bitstream Vera Sans Mono",9);
  119. #endif
  120. break;
  121. default:
  122. f = QsciLexer::defaultFont(style);
  123. }
  124. return f;
  125. }
  126. // Returns the set of keywords.
  127. const char *QsciLexerBash::keywords(int set) const
  128. {
  129. if (set == 1)
  130. return
  131. "alias ar asa awk banner basename bash bc bdiff break "
  132. "bunzip2 bzip2 cal calendar case cat cc cd chmod "
  133. "cksum clear cmp col comm compress continue cp cpio "
  134. "crypt csplit ctags cut date dc dd declare deroff dev "
  135. "df diff diff3 dircmp dirname do done du echo ed "
  136. "egrep elif else env esac eval ex exec exit expand "
  137. "export expr false fc fgrep fi file find fmt fold for "
  138. "function functions getconf getopt getopts grep gres "
  139. "hash head help history iconv id if in integer jobs "
  140. "join kill local lc let line ln logname look ls m4 "
  141. "mail mailx make man mkdir more mt mv newgrp nl nm "
  142. "nohup ntps od pack paste patch pathchk pax pcat perl "
  143. "pg pr print printf ps pwd read readonly red return "
  144. "rev rm rmdir sed select set sh shift size sleep sort "
  145. "spell split start stop strings strip stty sum "
  146. "suspend sync tail tar tee test then time times touch "
  147. "tr trap true tsort tty type typeset ulimit umask "
  148. "unalias uname uncompress unexpand uniq unpack unset "
  149. "until uudecode uuencode vi vim vpax wait wc whence "
  150. "which while who wpaste wstart xargs zcat "
  151. "chgrp chown chroot dir dircolors factor groups "
  152. "hostid install link md5sum mkfifo mknod nice pinky "
  153. "printenv ptx readlink seq sha1sum shred stat su tac "
  154. "unlink users vdir whoami yes";
  155. return 0;
  156. }
  157. // Returns the user name of a style.
  158. QString QsciLexerBash::description(int style) const
  159. {
  160. switch (style)
  161. {
  162. case Default:
  163. return tr("Default");
  164. case Error:
  165. return tr("Error");
  166. case Comment:
  167. return tr("Comment");
  168. case Number:
  169. return tr("Number");
  170. case Keyword:
  171. return tr("Keyword");
  172. case DoubleQuotedString:
  173. return tr("Double-quoted string");
  174. case SingleQuotedString:
  175. return tr("Single-quoted string");
  176. case Operator:
  177. return tr("Operator");
  178. case Identifier:
  179. return tr("Identifier");
  180. case Scalar:
  181. return tr("Scalar");
  182. case ParameterExpansion:
  183. return tr("Parameter expansion");
  184. case Backticks:
  185. return tr("Backticks");
  186. case HereDocumentDelimiter:
  187. return tr("Here document delimiter");
  188. case SingleQuotedHereDocument:
  189. return tr("Single-quoted here document");
  190. }
  191. return QString();
  192. }
  193. // Returns the background colour of the text for a style.
  194. QColor QsciLexerBash::defaultPaper(int style) const
  195. {
  196. switch (style)
  197. {
  198. case Error:
  199. return QColor(0xff,0x00,0x00);
  200. case Scalar:
  201. return QColor(0xff,0xe0,0xe0);
  202. case ParameterExpansion:
  203. return QColor(0xff,0xff,0xe0);
  204. case Backticks:
  205. return QColor(0xa0,0x80,0x80);
  206. case HereDocumentDelimiter:
  207. case SingleQuotedHereDocument:
  208. return QColor(0xdd,0xd0,0xdd);
  209. }
  210. return QsciLexer::defaultPaper(style);
  211. }
  212. // Refresh all properties.
  213. void QsciLexerBash::refreshProperties()
  214. {
  215. setCommentProp();
  216. setCompactProp();
  217. }
  218. // Read properties from the settings.
  219. bool QsciLexerBash::readProperties(QSettings &qs, const QString &prefix)
  220. {
  221. int rc = true;
  222. fold_comments = qs.value(prefix + "foldcomments", false).toBool();
  223. fold_compact = qs.value(prefix + "foldcompact", true).toBool();
  224. return rc;
  225. }
  226. // Write properties to the settings.
  227. bool QsciLexerBash::writeProperties(QSettings &qs, const QString &prefix) const
  228. {
  229. int rc = true;
  230. qs.setValue(prefix + "foldcomments", fold_comments);
  231. qs.setValue(prefix + "foldcompact", fold_compact);
  232. return rc;
  233. }
  234. // Return true if comments can be folded.
  235. bool QsciLexerBash::foldComments() const
  236. {
  237. return fold_comments;
  238. }
  239. // Set if comments can be folded.
  240. void QsciLexerBash::setFoldComments(bool fold)
  241. {
  242. fold_comments = fold;
  243. setCommentProp();
  244. }
  245. // Set the "fold.comment" property.
  246. void QsciLexerBash::setCommentProp()
  247. {
  248. emit propertyChanged("fold.comment", (fold_comments ? "1" : "0"));
  249. }
  250. // Return true if folds are compact.
  251. bool QsciLexerBash::foldCompact() const
  252. {
  253. return fold_compact;
  254. }
  255. // Set if folds are compact
  256. void QsciLexerBash::setFoldCompact(bool fold)
  257. {
  258. fold_compact = fold;
  259. setCompactProp();
  260. }
  261. // Set the "fold.compact" property.
  262. void QsciLexerBash::setCompactProp()
  263. {
  264. emit propertyChanged("fold.compact", (fold_compact ? "1" : "0"));
  265. }