2 (c) 2009 by Leon Winter
3 (c) 2009-2011 by Hannes Schueller
4 (c) 2009-2010 by Matto Fransen
5 (c) 2010-2011 by Hans-Peter Deifel
6 (c) 2010-2011 by Thomas Adam
11 #include "vimprobable.h"
13 #include "utilities.h"
15 extern char commandhistory
[COMMANDHISTSIZE
][255];
16 extern Command commands
[COMMANDSIZE
];
17 extern int lastcommand
, maxcommands
, commandpointer
;
18 extern KeyList
*keylistroot
;
20 extern char *error_msg
;
21 extern gboolean complete_case_sensitive
;
22 extern char *config_base
;
23 static GList
*dynamic_searchengines
= NULL
;
25 gboolean
read_rcfile(const char *config
)
30 gboolean returnval
= TRUE
;
32 if ((fpin
= fopen(config
, "r")) == NULL
)
34 while (fgets(s
, 254, fpin
)) {
36 * ignore lines that begin with #, / and such
49 void save_command_history(char *line
)
54 while (isspace(*c
) && *c
)
58 strncpy(commandhistory
[lastcommand
], ":", 1);
59 strncat(commandhistory
[lastcommand
], c
, 254);
61 if (maxcommands
< COMMANDHISTSIZE
- 1)
63 if (lastcommand
== COMMANDHISTSIZE
)
65 commandpointer
= lastcommand
;
69 process_save_qmark(const char *bm
, WebKitWebView
*webview
)
73 const char *uri
= webkit_web_view_get_uri(webview
);
80 if ( mark
< 1 || mark
> 9 )
83 a
.s
= g_strdup_printf("Invalid quickmark, only 1-9");
88 if ( uri
== NULL
) return FALSE
;
89 for( i
=0; i
< 9; ++i
) strcpy( qmarks
[i
], "");
91 filename
= g_strdup_printf(QUICKMARK_FILE
);
93 /* get current quickmarks */
95 fp
= fopen(filename
, "r");
97 for( i
=0; i
< 10; ++i
) {
103 while (buf
[l
] && l
< 100 && buf
[l
] != '\n') {
112 /* save quickmarks */
113 strcpy( qmarks
[mark
-1], uri
);
114 fp
= fopen(filename
, "w");
115 g_free((gpointer
*)filename
);
116 if (fp
== NULL
) return FALSE
;
117 for( i
=0; i
< 10; ++i
)
118 fprintf(fp
, "%s\n", qmarks
[i
]);
121 a
.s
= g_strdup_printf("Saved as quickmark %d: %s", mark
, uri
);
132 KeyList
*ptr
, *current
;
137 while ( keys
[i
].key
!= 0 )
139 current
= malloc(sizeof(KeyList
));
140 if (current
== NULL
) {
141 printf("Not enough memory\n");
144 current
->Element
= keys
[i
];
145 current
->next
= NULL
;
146 if (keylistroot
== NULL
) keylistroot
= current
;
147 if (ptr
!= NULL
) ptr
->next
= current
;
154 parse_colour(char *color
) {
158 colorlen
= (int)strlen(color
);
163 /* help the user a bit by making string like
164 #a10 and strings like ffffff full 6digit
165 strings with # in front :)
168 if (color
[0] == '#') {
171 strncpy(goodcolor
, color
, 7);
174 goodcolor
[1] = color
[1];
175 goodcolor
[2] = color
[1];
176 goodcolor
[3] = color
[2];
177 goodcolor
[4] = color
[2];
178 goodcolor
[5] = color
[3];
179 goodcolor
[6] = color
[3];
182 goodcolor
[1] = color
[1];
183 goodcolor
[2] = color
[1];
184 goodcolor
[3] = color
[1];
185 goodcolor
[4] = color
[1];
186 goodcolor
[5] = color
[1];
187 goodcolor
[6] = color
[1];
193 strncpy(&goodcolor
[1], color
, 6);
196 goodcolor
[1] = color
[0];
197 goodcolor
[2] = color
[0];
198 goodcolor
[3] = color
[1];
199 goodcolor
[4] = color
[1];
200 goodcolor
[5] = color
[2];
201 goodcolor
[6] = color
[2];
204 goodcolor
[1] = color
[0];
205 goodcolor
[2] = color
[0];
206 goodcolor
[3] = color
[0];
207 goodcolor
[4] = color
[0];
208 goodcolor
[5] = color
[0];
209 goodcolor
[6] = color
[0];
214 if (strlen (goodcolor
) != 7) {
217 strncpy(color
, goodcolor
, 8);
223 process_line_arg(const Arg
*arg
) {
224 return process_line(arg
->s
);
228 changemapping(Key
*search_key
, int maprecord
, char *cmd
) {
229 KeyList
*current
, *newkey
;
230 Arg a
= { .s
= cmd
};
233 if (maprecord
< 0 && cmd
== NULL
) {
235 * - maprecord >= 0 && cmd == NULL: mapping to internal symbol
236 * - maprecord < 0 && cmd != NULL: mapping to command line
237 * - maprecord >= 0 && cmd != NULL: cmd will be ignored, treated as mapping to internal symbol
238 * - anything else (gets in here): an error, hence we return FALSE */
242 current
= keylistroot
;
245 while (current
->next
!= NULL
) {
247 current
->Element
.mask
== search_key
->mask
&&
248 current
->Element
.modkey
== search_key
->modkey
&&
249 current
->Element
.key
== search_key
->key
251 if (maprecord
>= 0) {
252 /* mapping to an internal signal */
253 current
->Element
.func
= commands
[maprecord
].func
;
254 current
->Element
.arg
= commands
[maprecord
].arg
;
256 /* mapping to a command line */
257 current
->Element
.func
= process_line_arg
;
258 current
->Element
.arg
= a
;
262 current
= current
->next
;
264 newkey
= malloc(sizeof(KeyList
));
265 if (newkey
== NULL
) {
266 printf("Not enough memory\n");
269 newkey
->Element
.mask
= search_key
->mask
;
270 newkey
->Element
.modkey
= search_key
->modkey
;
271 newkey
->Element
.key
= search_key
->key
;
272 if (maprecord
>= 0) {
273 /* mapping to an internal signal */
274 newkey
->Element
.func
= commands
[maprecord
].func
;
275 newkey
->Element
.arg
= commands
[maprecord
].arg
;
277 /* mapping to a command line */
278 newkey
->Element
.func
= process_line_arg
;
279 newkey
->Element
.arg
= a
;
283 if (keylistroot
== NULL
) keylistroot
= newkey
;
285 if (current
!= NULL
) current
->next
= newkey
;
291 mappings(const Arg
*arg
) {
295 set_error("Missing argument.");
298 strncpy(line
, arg
->s
, 254);
299 if (process_map_line(line
))
302 set_error("Invalid mapping.");
308 get_modkey(char key
) {
311 return GDK_MOD1_MASK
;
313 return GDK_MOD2_MASK
;
315 return GDK_MOD3_MASK
;
317 return GDK_MOD4_MASK
;
319 return GDK_MOD5_MASK
;
326 process_mapping(char *keystring
, int maprecord
, char *cmd
) {
330 search_key
.modkey
= 0;
333 if (strlen(keystring
) == 1) {
334 search_key
.key
= keystring
[0];
337 if (strlen(keystring
) == 2) {
338 search_key
.modkey
= keystring
[0];
339 search_key
.key
= keystring
[1];
342 /* process stuff like <S-v> for Shift-v or <C-v> for Ctrl-v (strlen == 5),
343 stuff like <S-v>a for Shift-v,a or <C-v>a for Ctrl-v,a (strlen == 6 && keystring[4] == '>')
344 stuff like <M1-v> for Mod1-v (strlen == 6 && keystring[5] == '>')
345 or stuff like <M1-v>a for Mod1-v,a (strlen = 7)
348 ((strlen(keystring
) == 5 || strlen(keystring
) == 6) && keystring
[0] == '<' && keystring
[4] == '>') ||
349 ((strlen(keystring
) == 6 || strlen(keystring
) == 7) && keystring
[0] == '<' && keystring
[5] == '>')
351 switch (toupper(keystring
[1])) {
353 search_key
.mask
= GDK_SHIFT_MASK
;
354 if (strlen(keystring
) == 5) {
355 keystring
[3] = toupper(keystring
[3]);
357 keystring
[3] = tolower(keystring
[3]);
358 keystring
[5] = toupper(keystring
[5]);
362 search_key
.mask
= GDK_CONTROL_MASK
;
365 search_key
.mask
= get_modkey(keystring
[2]);
368 if (!search_key
.mask
)
370 if (strlen(keystring
) == 5) {
371 search_key
.key
= keystring
[3];
372 } else if (strlen(keystring
) == 7) {
373 search_key
.modkey
= keystring
[4];
374 search_key
.key
= keystring
[6];
376 if (search_key
.mask
== 'S' || search_key
.mask
== 'C') {
377 search_key
.modkey
= keystring
[3];
378 search_key
.key
= keystring
[5];
380 search_key
.key
= keystring
[4];
385 /* process stuff like a<S-v> for a,Shift-v or a<C-v> for a,Ctrl-v (strlen == 6)
386 or stuff like a<M1-v> for a,Mod1-v (strlen == 7)
389 (strlen(keystring
) == 6 && keystring
[1] == '<' && keystring
[5] == '>') ||
390 (strlen(keystring
) == 7 && keystring
[1] == '<' && keystring
[6] == '>')
392 switch (toupper(keystring
[2])) {
394 search_key
.mask
= GDK_SHIFT_MASK
;
395 keystring
[4] = toupper(keystring
[4]);
398 search_key
.mask
= GDK_CONTROL_MASK
;
401 search_key
.mask
= get_modkey(keystring
[3]);
404 if (!search_key
.mask
)
406 search_key
.modkey
= keystring
[0];
407 if (strlen(keystring
) == 6) {
408 search_key
.key
= keystring
[4];
410 search_key
.key
= keystring
[5];
413 return (changemapping(&search_key
, maprecord
, cmd
));
417 process_map_line(char *line
) {
423 if (!strlen(my_pair
.what
))
425 while (isspace(*c
) && *c
)
428 if (*c
== ':' || *c
== '=')
432 if (!strlen(my_pair
.value
))
434 listlen
= LENGTH(commands
);
435 for (i
= 0; i
< listlen
; i
++) {
436 /* commands is fixed size */
437 if (commands
[i
].cmd
== NULL
)
439 if (strlen(commands
[i
].cmd
) == strlen(my_pair
.value
) && strncmp(commands
[i
].cmd
, my_pair
.value
, strlen(my_pair
.value
)) == 0) {
440 /* map to an internal symbol */
441 return process_mapping(my_pair
.what
, i
, NULL
);
444 /* if this is reached, the mapping is not for one of the internal symbol - test for command line structure */
445 if (strlen(my_pair
.value
) > 1 && strncmp(my_pair
.value
, ":", 1) == 0) {
446 /* The string begins with a colon, like a command line, but it's not _just_ a colon,
447 * i.e. increasing the pointer by one will not go 'out of bounds'.
448 * We don't actually check that the command line after the = is valid.
449 * This is user responsibility, the worst case is the new mapping simply doing nothing.
450 * Since we will pass the command to the same function which also handles the config file lines,
451 * we have to strip the colon itself (a colon counts as a commented line there - like in vim).
452 * Last, but not least, the second argument being < 0 signifies to the function that this is a
453 * command line mapping, not a mapping to an existing internal symbol. */
454 cmd
= (char *)malloc(sizeof(char) * strlen(my_pair
.value
));
455 strncpy(cmd
, (my_pair
.value
+ 1), strlen(my_pair
.value
) - 1);
456 cmd
[strlen(cmd
)] = '\0';
457 return process_mapping(my_pair
.what
, -1, cmd
);
463 build_taglist(const Arg
*arg
, FILE *f
) {
464 int k
= 0, in_tag
= 0;
465 int t
= 0, marker
= 0;
466 char foundtab
[MAXTAGSIZE
+1];
468 if (!isspace(arg
->s
[k
]) && !in_tag
) {
472 if (isspace(arg
->s
[k
]) && in_tag
) {
475 while (marker
< k
&& t
< MAXTAGSIZE
) foundtab
[t
++] = arg
->s
[marker
++];
477 fprintf(f
, " [%s]", foundtab
);
484 while (marker
< strlen(arg
->s
) && t
< MAXTAGSIZE
) foundtab
[t
++] = arg
->s
[marker
++];
486 fprintf(f
, " [%s]", foundtab
);
492 set_error(const char *error
) {
493 /* it should never happen that set_error is called more than once,
494 * but to avoid any potential memory leaks, we ignore any subsequent
495 * error if the current one has not been shown */
496 if (error_msg
== NULL
) {
497 error_msg
= g_strdup_printf("%s", error
);
502 give_feedback(const char *feedback
)
504 Arg a
= { .i
= Info
};
506 a
.s
= g_strdup_printf("%s", feedback
);
512 complete_list(const char *searchfor
, const int mode
, Listelement
*elementlist
)
515 const char *filename
;
516 Listelement
*candidatelist
= NULL
, *candidatepointer
= NULL
;
517 char s
[255] = "", readelement
[MAXTAGSIZE
+ 1] = "";
521 /* open in history file */
522 filename
= g_strdup_printf(HISTORY_STORAGE_FILENAME
);
524 /* open in bookmark file (for tags and bookmarks) */
525 filename
= g_strdup_printf(BOOKMARKS_STORAGE_FILENAME
);
527 f
= fopen(filename
, "r");
529 g_free((gpointer
*)filename
);
530 return (elementlist
);
533 while (fgets(s
, 254, f
)) {
535 /* just tags (could be more than one per line) */
537 while (s
[i
] && i
< 254) {
538 while (s
[i
] != '[' && s
[i
])
544 while (s
[i
] != ']' && s
[i
] && t
< MAXTAGSIZE
)
545 readelement
[t
++] = s
[i
++];
546 readelement
[t
] = '\0';
547 candidatelist
= add_list(readelement
, candidatelist
);
551 /* complete string (bookmarks & history) */
552 candidatelist
= add_list(s
, candidatelist
);
554 candidatepointer
= candidatelist
;
555 while (candidatepointer
!= NULL
) {
556 if (!complete_case_sensitive
) {
557 g_strdown(candidatepointer
->element
);
559 if (!strlen(searchfor
) || strstr(candidatepointer
->element
, searchfor
) != NULL
) {
560 /* only use string up to the first space */
561 memset(readelement
, 0, MAXTAGSIZE
+ 1);
562 if (strchr(candidatepointer
->element
, ' ') != NULL
) {
563 i
= strcspn(candidatepointer
->element
, " ");
566 strncpy(readelement
, candidatepointer
->element
, i
);
568 strncpy(readelement
, candidatepointer
->element
, MAXTAGSIZE
);
570 /* in the case of URLs without title, remove the line break */
571 if (readelement
[strlen(readelement
) - 1] == '\n') {
572 readelement
[strlen(readelement
) - 1] = '\0';
574 elementlist
= add_list(readelement
, elementlist
);
575 n
= count_list(elementlist
);
577 if (n
>= MAX_LIST_SIZE
)
579 candidatepointer
= candidatepointer
->next
;
581 free_list(candidatelist
);
582 candidatelist
= NULL
;
583 if (n
>= MAX_LIST_SIZE
)
586 g_free((gpointer
)filename
);
587 return (elementlist
);
591 add_list(const char *element
, Listelement
*elementlist
)
594 Listelement
*newelement
, *elementpointer
, *lastelement
;
596 if (elementlist
== NULL
) { /* first element */
597 newelement
= malloc(sizeof(Listelement
));
598 if (newelement
== NULL
)
599 return (elementlist
);
600 strncpy(newelement
->element
, element
, 254);
601 newelement
->next
= NULL
;
604 elementpointer
= elementlist
;
607 /* check if element is already in list */
608 while (elementpointer
!= NULL
) {
609 if (strlen(elementpointer
->element
) == n
&&
610 strncmp(elementpointer
->element
, element
, n
) == 0)
611 return (elementlist
);
612 lastelement
= elementpointer
;
613 elementpointer
= elementpointer
->next
;
617 newelement
= malloc(sizeof(Listelement
));
618 if (newelement
== NULL
)
619 return (elementlist
);
620 lastelement
->next
= newelement
;
621 strncpy(newelement
->element
, element
, 254);
622 newelement
->next
= NULL
;
627 free_list(Listelement
*elementlist
)
629 Listelement
*elementpointer
;
631 while (elementlist
!= NULL
) {
632 elementpointer
= elementlist
->next
;
634 elementlist
= elementpointer
;
639 count_list(Listelement
*elementlist
)
641 Listelement
*elementpointer
= elementlist
;
644 while (elementpointer
!= NULL
) {
646 elementpointer
= elementpointer
->next
;
652 /* split the string at the first occurence of whitespace and return the
653 * position of the second half.
654 * Unlike strtok, the substrings can be empty and the second string is
655 * stripped of trailing and leading whitespace.
656 * Return -1 if `string' contains no whitespace */
657 static int split_string_at_whitespace(char *string
)
659 int index
= strcspn(string
, "\n\t ");
660 if (string
[index
] != '\0') {
662 g_strstrip(string
+index
);
668 /* return TRUE, if the string contains exactly one unescaped %s and no other
669 * printf directives */
670 static gboolean
sanity_check_search_url(const char *string
)
672 int was_percent_char
= 0, percent_s_count
= 0;
674 for (; *string
; string
++) {
677 was_percent_char
= !was_percent_char
;
680 if (was_percent_char
)
682 was_percent_char
= 0;
685 if (was_percent_char
)
687 was_percent_char
= 0;
692 return !was_percent_char
&& percent_s_count
== 1;
696 read_searchengines(const char *filename
)
699 char buffer
[BUFFERSIZE
], c
;
700 int linum
= 0, index
;
701 gboolean found_malformed_lines
= FALSE
;
704 if (access(filename
, F_OK
) != 0)
705 return FILE_NOT_FOUND
;
707 file
= fopen(filename
, "r");
709 return READING_FAILED
;
711 while (fgets(buffer
, BUFFERSIZE
, file
)) {
714 /* skip empty lines */
715 if (!strcmp(buffer
, "\n")) continue;
717 /* skip too long lines */
718 if (buffer
[strlen(buffer
)-1] != '\n') {
720 if (c
!= EOF
) { /* this is not the last line */
721 while ((c
=getc(file
)) != EOF
&& c
!= '\n');
722 fprintf(stderr
, "searchengines: syntax error on line %d\n", linum
);
723 found_malformed_lines
= TRUE
;
728 /* split line at whitespace */
729 index
= split_string_at_whitespace(buffer
);
731 if (index
< 0 || buffer
[0] == '\0' || buffer
[index
] == '\0'
732 || !sanity_check_search_url(buffer
+index
)) {
733 fprintf(stderr
, "searchengines: syntax error on line %d\n", linum
);
734 found_malformed_lines
= TRUE
;
738 new = malloc(sizeof(Searchengine
));
740 fprintf(stderr
, "Memory exhausted while loading search engines.\n");
744 new->handle
= g_strdup(buffer
);
745 new->uri
= g_strdup(buffer
+index
);
747 dynamic_searchengines
= g_list_prepend(dynamic_searchengines
, new);
752 return READING_FAILED
;
757 return found_malformed_lines
? SYNTAX_ERROR
: SUCCESS
;
760 void make_searchengines_list(Searchengine
*searchengines
, int length
)
763 for (i
= 0; i
< length
; i
++, searchengines
++) {
764 dynamic_searchengines
= g_list_prepend(dynamic_searchengines
, searchengines
);
768 /* find a searchengine with a given handle and return its URI or NULL if
770 * The returned string is internal and must not be freed or modified. */
771 char *find_uri_for_searchengine(const char *handle
)
775 if (dynamic_searchengines
!= NULL
) {
776 for (l
= dynamic_searchengines
; l
; l
= g_list_next(l
)) {
777 Searchengine
*s
= (Searchengine
*)l
->data
;
778 if (!strcmp(s
->handle
, handle
)) {