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 static GList
*dynamic_searchengines
= NULL
;
24 gboolean
read_rcfile(const char *config
)
29 gboolean returnval
= TRUE
;
31 if ((fpin
= fopen(config
, "r")) == NULL
)
33 while (fgets(s
, 254, fpin
)) {
35 * ignore lines that begin with #, / and such
48 void save_command_history(char *line
)
53 while (isspace(*c
) && *c
)
57 strncpy(commandhistory
[lastcommand
], ":", 1);
58 strncat(commandhistory
[lastcommand
], c
, 254);
60 if (maxcommands
< COMMANDHISTSIZE
- 1)
62 if (lastcommand
== COMMANDHISTSIZE
)
64 commandpointer
= lastcommand
;
68 process_save_qmark(const char *bm
, WebKitWebView
*webview
)
72 const char *uri
= webkit_web_view_get_uri(webview
);
79 if ( mark
< 1 || mark
> 9 )
82 a
.s
= g_strdup_printf("Invalid quickmark, only 1-9");
87 if ( uri
== NULL
) return FALSE
;
88 for( i
=0; i
< 9; ++i
) strcpy( qmarks
[i
], "");
90 filename
= g_strdup_printf(QUICKMARK_FILE
);
92 /* get current quickmarks */
94 fp
= fopen(filename
, "r");
96 for( i
=0; i
< 10; ++i
) {
102 while (buf
[l
] && l
< 100 && buf
[l
] != '\n') {
111 /* save quickmarks */
112 strcpy( qmarks
[mark
-1], uri
);
113 fp
= fopen(filename
, "w");
114 if (fp
== NULL
) return FALSE
;
115 for( i
=0; i
< 10; ++i
)
116 fprintf(fp
, "%s\n", qmarks
[i
]);
119 a
.s
= g_strdup_printf("Saved as quickmark %d: %s", mark
, uri
);
130 KeyList
*ptr
, *current
;
135 while ( keys
[i
].key
!= 0 )
137 current
= malloc(sizeof(KeyList
));
138 if (current
== NULL
) {
139 printf("Not enough memory\n");
142 current
->Element
= keys
[i
];
143 current
->next
= NULL
;
144 if (keylistroot
== NULL
) keylistroot
= current
;
145 if (ptr
!= NULL
) ptr
->next
= current
;
152 parse_colour(char *color
) {
156 colorlen
= (int)strlen(color
);
161 /* help the user a bit by making string like
162 #a10 and strings like ffffff full 6digit
163 strings with # in front :)
166 if (color
[0] == '#') {
169 strncpy(goodcolor
, color
, 7);
172 goodcolor
[1] = color
[1];
173 goodcolor
[2] = color
[1];
174 goodcolor
[3] = color
[2];
175 goodcolor
[4] = color
[2];
176 goodcolor
[5] = color
[3];
177 goodcolor
[6] = color
[3];
180 goodcolor
[1] = color
[1];
181 goodcolor
[2] = color
[1];
182 goodcolor
[3] = color
[1];
183 goodcolor
[4] = color
[1];
184 goodcolor
[5] = color
[1];
185 goodcolor
[6] = color
[1];
191 strncpy(&goodcolor
[1], color
, 6);
194 goodcolor
[1] = color
[0];
195 goodcolor
[2] = color
[0];
196 goodcolor
[3] = color
[1];
197 goodcolor
[4] = color
[1];
198 goodcolor
[5] = color
[2];
199 goodcolor
[6] = color
[2];
202 goodcolor
[1] = color
[0];
203 goodcolor
[2] = color
[0];
204 goodcolor
[3] = color
[0];
205 goodcolor
[4] = color
[0];
206 goodcolor
[5] = color
[0];
207 goodcolor
[6] = color
[0];
212 if (strlen (goodcolor
) != 7) {
215 strncpy(color
, goodcolor
, 8);
221 process_line_arg(const Arg
*arg
) {
222 return process_line(arg
->s
);
226 changemapping(Key
*search_key
, int maprecord
, char *cmd
) {
227 KeyList
*current
, *newkey
;
228 Arg a
= { .s
= cmd
};
231 if (maprecord
< 0 && cmd
== NULL
) {
233 * - maprecord >= 0 && cmd == NULL: mapping to internal symbol
234 * - maprecord < 0 && cmd != NULL: mapping to command line
235 * - maprecord >= 0 && cmd != NULL: cmd will be ignored, treated as mapping to internal symbol
236 * - anything else (gets in here): an error, hence we return FALSE */
240 current
= keylistroot
;
243 while (current
->next
!= NULL
) {
245 current
->Element
.mask
== search_key
->mask
&&
246 current
->Element
.modkey
== search_key
->modkey
&&
247 current
->Element
.key
== search_key
->key
249 if (maprecord
>= 0) {
250 /* mapping to an internal signal */
251 current
->Element
.func
= commands
[maprecord
].func
;
252 current
->Element
.arg
= commands
[maprecord
].arg
;
254 /* mapping to a command line */
255 current
->Element
.func
= process_line_arg
;
256 current
->Element
.arg
= a
;
260 current
= current
->next
;
262 newkey
= malloc(sizeof(KeyList
));
263 if (newkey
== NULL
) {
264 printf("Not enough memory\n");
267 newkey
->Element
.mask
= search_key
->mask
;
268 newkey
->Element
.modkey
= search_key
->modkey
;
269 newkey
->Element
.key
= search_key
->key
;
270 if (maprecord
>= 0) {
271 /* mapping to an internal signal */
272 newkey
->Element
.func
= commands
[maprecord
].func
;
273 newkey
->Element
.arg
= commands
[maprecord
].arg
;
275 /* mapping to a command line */
276 newkey
->Element
.func
= process_line_arg
;
277 newkey
->Element
.arg
= a
;
281 if (keylistroot
== NULL
) keylistroot
= newkey
;
283 if (current
!= NULL
) current
->next
= newkey
;
289 mappings(const Arg
*arg
) {
293 set_error("Missing argument.");
296 strncpy(line
, arg
->s
, 254);
297 if (process_map_line(line
))
300 set_error("Invalid mapping.");
306 get_modkey(char key
) {
309 return GDK_MOD1_MASK
;
311 return GDK_MOD2_MASK
;
313 return GDK_MOD3_MASK
;
315 return GDK_MOD4_MASK
;
317 return GDK_MOD5_MASK
;
324 process_mapping(char *keystring
, int maprecord
, char *cmd
) {
328 search_key
.modkey
= 0;
331 if (strlen(keystring
) == 1) {
332 search_key
.key
= keystring
[0];
335 if (strlen(keystring
) == 2) {
336 search_key
.modkey
= keystring
[0];
337 search_key
.key
= keystring
[1];
340 /* process stuff like <S-v> for Shift-v or <C-v> for Ctrl-v (strlen == 5),
341 stuff like <S-v>a for Shift-v,a or <C-v>a for Ctrl-v,a (strlen == 6 && keystring[4] == '>')
342 stuff like <M1-v> for Mod1-v (strlen == 6 && keystring[5] == '>')
343 or stuff like <M1-v>a for Mod1-v,a (strlen = 7)
346 ((strlen(keystring
) == 5 || strlen(keystring
) == 6) && keystring
[0] == '<' && keystring
[4] == '>') ||
347 ((strlen(keystring
) == 6 || strlen(keystring
) == 7) && keystring
[0] == '<' && keystring
[5] == '>')
349 switch (toupper(keystring
[1])) {
351 search_key
.mask
= GDK_SHIFT_MASK
;
352 if (strlen(keystring
) == 5) {
353 keystring
[3] = toupper(keystring
[3]);
355 keystring
[3] = tolower(keystring
[3]);
356 keystring
[5] = toupper(keystring
[5]);
360 search_key
.mask
= GDK_CONTROL_MASK
;
363 search_key
.mask
= get_modkey(keystring
[2]);
366 if (!search_key
.mask
)
368 if (strlen(keystring
) == 5) {
369 search_key
.key
= keystring
[3];
370 } else if (strlen(keystring
) == 7) {
371 search_key
.modkey
= keystring
[4];
372 search_key
.key
= keystring
[6];
374 if (search_key
.mask
== 'S' || search_key
.mask
== 'C') {
375 search_key
.modkey
= keystring
[3];
376 search_key
.key
= keystring
[5];
378 search_key
.key
= keystring
[4];
383 /* process stuff like a<S-v> for a,Shift-v or a<C-v> for a,Ctrl-v (strlen == 6)
384 or stuff like a<M1-v> for a,Mod1-v (strlen == 7)
387 (strlen(keystring
) == 6 && keystring
[1] == '<' && keystring
[5] == '>') ||
388 (strlen(keystring
) == 7 && keystring
[1] == '<' && keystring
[6] == '>')
390 switch (toupper(keystring
[2])) {
392 search_key
.mask
= GDK_SHIFT_MASK
;
393 keystring
[4] = toupper(keystring
[4]);
396 search_key
.mask
= GDK_CONTROL_MASK
;
399 search_key
.mask
= get_modkey(keystring
[3]);
402 if (!search_key
.mask
)
404 search_key
.modkey
= keystring
[0];
405 if (strlen(keystring
) == 6) {
406 search_key
.key
= keystring
[4];
408 search_key
.key
= keystring
[5];
411 return (changemapping(&search_key
, maprecord
, cmd
));
415 process_map_line(char *line
) {
421 if (!strlen(my_pair
.what
))
423 while (isspace(*c
) && *c
)
426 if (*c
== ':' || *c
== '=')
430 if (!strlen(my_pair
.value
))
432 listlen
= LENGTH(commands
);
433 for (i
= 0; i
< listlen
; i
++) {
434 /* commands is fixed size */
435 if (commands
[i
].cmd
== NULL
)
437 if (strlen(commands
[i
].cmd
) == strlen(my_pair
.value
) && strncmp(commands
[i
].cmd
, my_pair
.value
, strlen(my_pair
.value
)) == 0) {
438 /* map to an internal symbol */
439 return process_mapping(my_pair
.what
, i
, NULL
);
442 /* if this is reached, the mapping is not for one of the internal symbol - test for command line structure */
443 if (strlen(my_pair
.value
) > 1 && strncmp(my_pair
.value
, ":", 1) == 0) {
444 /* The string begins with a colon, like a command line, but it's not _just_ a colon,
445 * i.e. increasing the pointer by one will not go 'out of bounds'.
446 * We don't actually check that the command line after the = is valid.
447 * This is user responsibility, the worst case is the new mapping simply doing nothing.
448 * Since we will pass the command to the same function which also handles the config file lines,
449 * we have to strip the colon itself (a colon counts as a commented line there - like in vim).
450 * Last, but not least, the second argument being < 0 signifies to the function that this is a
451 * command line mapping, not a mapping to an existing internal symbol. */
452 cmd
= (char *)malloc(sizeof(char) * strlen(my_pair
.value
));
453 strncpy(cmd
, (my_pair
.value
+ 1), strlen(my_pair
.value
) - 1);
454 cmd
[strlen(cmd
)] = '\0';
455 return process_mapping(my_pair
.what
, -1, cmd
);
461 build_taglist(const Arg
*arg
, FILE *f
) {
462 int k
= 0, in_tag
= 0;
463 int t
= 0, marker
= 0;
464 char foundtab
[MAXTAGSIZE
+1];
466 if (!isspace(arg
->s
[k
]) && !in_tag
) {
470 if (isspace(arg
->s
[k
]) && in_tag
) {
473 while (marker
< k
&& t
< MAXTAGSIZE
) foundtab
[t
++] = arg
->s
[marker
++];
475 fprintf(f
, " [%s]", foundtab
);
482 while (marker
< strlen(arg
->s
) && t
< MAXTAGSIZE
) foundtab
[t
++] = arg
->s
[marker
++];
484 fprintf(f
, " [%s]", foundtab
);
490 set_error(const char *error
) {
491 /* it should never happen that set_error is called more than once,
492 * but to avoid any potential memory leaks, we ignore any subsequent
493 * error if the current one has not been shown */
494 if (error_msg
== NULL
) {
495 error_msg
= g_strdup_printf("%s", error
);
500 give_feedback(const char *feedback
)
502 Arg a
= { .i
= Info
};
504 a
.s
= g_strdup_printf("%s", feedback
);
510 complete_list(const char *searchfor
, const int mode
, Listelement
*elementlist
)
513 const char *filename
;
514 Listelement
*candidatelist
= NULL
, *candidatepointer
= NULL
;
515 char s
[255] = "", readelement
[MAXTAGSIZE
+ 1] = "";
519 /* open in history file */
520 filename
= g_strdup_printf(HISTORY_STORAGE_FILENAME
);
522 /* open in bookmark file (for tags and bookmarks) */
523 filename
= g_strdup_printf(BOOKMARKS_STORAGE_FILENAME
);
525 f
= fopen(filename
, "r");
527 g_free((gpointer
)filename
);
528 return (elementlist
);
531 while (fgets(s
, 254, f
)) {
533 /* just tags (could be more than one per line) */
535 while (s
[i
] && i
< 254) {
536 while (s
[i
] != '[' && s
[i
])
542 while (s
[i
] != ']' && s
[i
] && t
< MAXTAGSIZE
)
543 readelement
[t
++] = s
[i
++];
544 readelement
[t
] = '\0';
545 candidatelist
= add_list(readelement
, candidatelist
);
549 /* complete string (bookmarks & history) */
550 candidatelist
= add_list(s
, candidatelist
);
552 candidatepointer
= candidatelist
;
553 while (candidatepointer
!= NULL
) {
554 if (!complete_case_sensitive
) {
555 g_strdown(candidatepointer
->element
);
557 if (!strlen(searchfor
) || strstr(candidatepointer
->element
, searchfor
) != NULL
) {
558 /* only use string up to the first space */
559 memset(readelement
, 0, MAXTAGSIZE
+ 1);
560 if (strchr(candidatepointer
->element
, ' ') != NULL
) {
561 i
= strcspn(candidatepointer
->element
, " ");
562 strncpy(readelement
, candidatepointer
->element
, i
);
564 strncpy(readelement
, candidatepointer
->element
, MAXTAGSIZE
);
566 /* in the case of URLs without title, remove the line break */
567 if (readelement
[strlen(readelement
) - 1] == '\n') {
568 readelement
[strlen(readelement
) - 1] = '\0';
570 elementlist
= add_list(readelement
, elementlist
);
571 n
= count_list(elementlist
);
573 if (n
>= MAX_LIST_SIZE
)
575 candidatepointer
= candidatepointer
->next
;
577 free_list(candidatelist
);
578 candidatelist
= NULL
;
579 if (n
>= MAX_LIST_SIZE
)
582 g_free((gpointer
)filename
);
583 return (elementlist
);
587 add_list(const char *element
, Listelement
*elementlist
)
590 Listelement
*newelement
, *elementpointer
, *lastelement
;
592 if (elementlist
== NULL
) { /* first element */
593 newelement
= malloc(sizeof(Listelement
));
594 if (newelement
== NULL
)
595 return (elementlist
);
596 strncpy(newelement
->element
, element
, 254);
597 newelement
->next
= NULL
;
600 elementpointer
= elementlist
;
603 /* check if element is already in list */
604 while (elementpointer
!= NULL
) {
605 if (strlen(elementpointer
->element
) == n
&&
606 strncmp(elementpointer
->element
, element
, n
) == 0)
607 return (elementlist
);
608 lastelement
= elementpointer
;
609 elementpointer
= elementpointer
->next
;
613 newelement
= malloc(sizeof(Listelement
));
614 if (newelement
== NULL
)
615 return (elementlist
);
616 lastelement
->next
= newelement
;
617 strncpy(newelement
->element
, element
, 254);
618 newelement
->next
= NULL
;
623 free_list(Listelement
*elementlist
)
625 Listelement
*elementpointer
;
627 while (elementlist
!= NULL
) {
628 elementpointer
= elementlist
->next
;
630 elementlist
= elementpointer
;
635 count_list(Listelement
*elementlist
)
637 Listelement
*elementpointer
= elementlist
;
640 while (elementpointer
!= NULL
) {
642 elementpointer
= elementpointer
->next
;
648 /* split the string at the first occurence of whitespace and return the
649 * position of the second half.
650 * Unlike strtok, the substrings can be empty and the second string is
651 * stripped of trailing and leading whitespace.
652 * Return -1 if `string' contains no whitespace */
653 static int split_string_at_whitespace(char *string
)
655 int index
= strcspn(string
, "\n\t ");
656 if (string
[index
] != '\0') {
658 g_strstrip(string
+index
);
664 /* return TRUE, if the string contains exactly one unescaped %s and no other
665 * printf directives */
666 static gboolean
sanity_check_search_url(const char *string
)
668 int was_percent_char
= 0, percent_s_count
= 0;
670 for (; *string
; string
++) {
673 was_percent_char
= !was_percent_char
;
676 if (was_percent_char
)
678 was_percent_char
= 0;
681 if (was_percent_char
)
683 was_percent_char
= 0;
688 return !was_percent_char
&& percent_s_count
== 1;
692 read_searchengines(const char *filename
)
695 char buffer
[BUFFERSIZE
], c
;
696 int linum
= 0, index
;
697 gboolean found_malformed_lines
= FALSE
;
700 if (access(filename
, F_OK
) != 0)
701 return FILE_NOT_FOUND
;
703 file
= fopen(filename
, "r");
705 return READING_FAILED
;
707 while (fgets(buffer
, BUFFERSIZE
, file
)) {
710 /* skip empty lines */
711 if (!strcmp(buffer
, "\n")) continue;
713 /* skip too long lines */
714 if (buffer
[strlen(buffer
)-1] != '\n') {
716 if (c
!= EOF
) { /* this is not the last line */
717 while ((c
=getc(file
)) != EOF
&& c
!= '\n');
718 fprintf(stderr
, "searchengines: syntax error on line %d\n", linum
);
719 found_malformed_lines
= TRUE
;
724 /* split line at whitespace */
725 index
= split_string_at_whitespace(buffer
);
727 if (index
< 0 || buffer
[0] == '\0' || buffer
[index
] == '\0'
728 || !sanity_check_search_url(buffer
+index
)) {
729 fprintf(stderr
, "searchengines: syntax error on line %d\n", linum
);
730 found_malformed_lines
= TRUE
;
734 new = malloc(sizeof(Searchengine
));
736 fprintf(stderr
, "Memory exhausted while loading search engines.\n");
740 new->handle
= g_strdup(buffer
);
741 new->uri
= g_strdup(buffer
+index
);
743 dynamic_searchengines
= g_list_prepend(dynamic_searchengines
, new);
748 return READING_FAILED
;
753 return found_malformed_lines
? SYNTAX_ERROR
: SUCCESS
;
756 void make_searchengines_list(Searchengine
*searchengines
, int length
)
759 for (i
= 0; i
< length
; i
++, searchengines
++) {
760 dynamic_searchengines
= g_list_prepend(dynamic_searchengines
, searchengines
);
764 /* find a searchengine with a given handle and return its URI or NULL if
766 * The returned string is internal and must not be freed or modified. */
767 char *find_uri_for_searchengine(const char *handle
)
771 if (dynamic_searchengines
!= NULL
) {
772 for (l
= dynamic_searchengines
; l
; l
= g_list_next(l
)) {
773 Searchengine
*s
= (Searchengine
*)l
->data
;
774 if (!strcmp(s
->handle
, handle
)) {