about summary refs log tree commit diff stats
path: root/tangle.mu
blob: 91f12deade5635bfc0bab86308fda0fbea63bdec (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# example program: constructing functions out of order
#
# We construct a factorial function with separate base and recursive cases.
# Compare factorial.mu.
#
# This isn't a very tasteful example, just a basic demonstration of
# possibilities.

def factorial n:num -> result:num [
  local-scope
  load-inputs
  <factorial-cases>
]

after <factorial-cases> [
  # if n=0 return 1
  return-unless n, 1
]

after <factorial-cases> [
  # return n * factorial(n - 1)
  {
    break-unless n
    x:num <- subtract n, 1
    subresult:num <- factorial x
    result <- multiply subresult, n
    return result
  }
]

def main [
  1:num <- factorial 5
  # trailing space in next line is to help with syntax highlighting
  $print [result: ], 1:num, [ 
]
]
/* Literal.Number.Integer.Long */
/* 
 * preferences.c
 *
 * Copyright (C) 2012 James Booth <boothj5@gmail.com>
 * 
 * This file is part of Profanity.
 *
 * Profanity is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Profanity is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with Profanity.  If not, see <http://www.gnu.org/licenses/>.
 *
 */

#include <stdlib.h>
#include <string.h>

#include <ncurses.h>
#include <glib.h>

#include "log.h"
#include "preferences.h"
#include "prof_autocomplete.h"

static GString *prefs_loc;
static GKeyFile *prefs;

// search logins list
static PAutocomplete ac;

struct colour_string_t {
    char *str;
    NCURSES_COLOR_T colour;
};

static int num_colours = 7;
static struct colour_string_t colours[] = {
    { "default", -1 },
    { "white", COLOR_WHITE },
    { "green", COLOR_GREEN },
    { "red", COLOR_RED },
    { "yellow", COLOR_YELLOW },
    { "blue", COLOR_BLUE },
    { "cyan", COLOR_CYAN },
};

// colour preferences
static struct colours_t {
        NCURSES_COLOR_T bkgnd;
        NCURSES_COLOR_T text;
        NCURSES_COLOR_T online;
        NCURSES_COLOR_T err;
        NCURSES_COLOR_T inc;
        NCURSES_COLOR_T bar;
        NCURSES_COLOR_T bar_text;
} colour_prefs;

static NCURSES_COLOR_T _lookup_colour(const char * const colour);
static void _set_colour(gchar *val, NCURSES_COLOR_T *pref, 
    NCURSES_COLOR_T def);
static void _load_colours(void);
static void _save_prefs(void);

void prefs_load(void)
{
    ac = p_autocomplete_new();
    prefs_loc = g_string_new(getenv("HOME"));
    g_string_append(prefs_loc, "/.profanity");

    prefs = g_key_file_new();
    g_key_file_load_from_file(prefs, prefs_loc->str, G_KEY_FILE_NONE, NULL);

    // create the logins searchable list for autocompletion
    gsize njids;
    gchar **jids =
        g_key_file_get_string_list(prefs, "connections", "logins", &njids, NULL);

    gsize i;
    for (i = 0; i < njids; i++) {
        p_autocomplete_add(ac, jids[i]);
    }

    _load_colours();
}

static NCURSES_COLOR_T _lookup_colour(const char * const colour)
{
    int i;
    for (i = 0; i < num_colours; i++) {
        if (strcmp(colours[i].str, colour) == 0) {
            return colours[i].colour;
        }
    }

    return -99;
}

static void _set_colour(gchar *val, NCURSES_COLOR_T *pref, 
    NCURSES_COLOR_T def)
{
    if(!val) {
        *pref = def;
    } else {
        NCURSES_COLOR_T col = _lookup_colour(val);
        if (col == -99) {
            *pref = def;
        } else {
            *pref = col;   
        }
    }
}

static void _load_colours(void)
{
    gchar *bkgnd_val = g_key_file_get_string(prefs, "colours", "bkgnd", NULL);
    _set_colour(bkgnd_val, &colour_prefs.bkgnd, -1);

    gchar *text_val = g_key_file_get_string(prefs, "colours", "text", NULL);
    _set_colour(text_val, &colour_prefs.text, COLOR_WHITE);

    gchar *online_val = g_key_file_get_string(prefs, "colours", "online", NULL);
    _set_colour(online_val, &colour_prefs.online, COLOR_GREEN);
    
    gchar *err_val = g_key_file_get_string(prefs, "colours", "err", NULL);
    _set_colour(err_val, &colour_prefs.err, COLOR_RED);

    gchar *inc_val = g_key_file_get_string(prefs, "colours", "inc", NULL);
    _set_colour(inc_val, &colour_prefs.inc, COLOR_YELLOW);
    
    gchar *bar_val = g_key_file_get_string(prefs, "colours", "bar", NULL);
    _set_colour(bar_val, &colour_prefs.bar, COLOR_BLUE);
    
    gchar *bar_text_val = g_key_file_get_string(prefs, "colours", "bar_text", NULL);
    _set_colour(bar_text_val, &colour_prefs.bar_text, COLOR_CYAN);
}

char * find_login(char *prefix)
{
    return p_autocomplete_complete(ac, prefix);
}

void reset_login_search(void)
{
    p_autocomplete_reset(ac);
}

gboolean prefs_get_beep(void)
{
    return g_key_file_get_boolean(prefs, "ui", "beep", NULL);
}

void prefs_set_beep(gboolean value)
{
    g_key_file_set_boolean(prefs, "ui", "beep", value);
    _save_prefs();
}

gboolean prefs_get_flash(void)
{
    return g_key_file_get_boolean(prefs, "ui", "flash", NULL);
}

void prefs_set_flash(gboolean value)
{
    g_key_file_set_boolean(prefs, "ui", "flash", value);
    _save_prefs();
}

void prefs_add_login(const char *jid)
{
    gsize njids;
    gchar **jids = 
        g_key_file_get_string_list(prefs, "connections", "logins", &njids, NULL);

    // no logins remembered yet
    if (jids == NULL) {
        njids = 1;
        jids = (gchar**) g_malloc(sizeof(gchar *) * 2);
        jids[0] = g_strdup(jid);
        jids[1] = NULL;
        g_key_file_set_string_list(prefs, "connections", "logins", 
            (const gchar * const *)jids, njids);
        _save_prefs();
        g_strfreev(jids);
        
        return;
    } else {
        gsize i;
        for (i = 0; i < njids; i++) {
            if (strcmp(jid, jids[i]) == 0) {
                g_strfreev(jids);
                return;
            }
        }
    
        // jid not found, add to the list
        jids = (gchar **) g_realloc(jids, (sizeof(gchar *) * (njids+2)));
        jids[njids] = g_strdup(jid);
        njids++;
        jids[njids] = NULL;
        g_key_file_set_string_list(prefs, "connections", "logins",
            (const gchar * const *)jids, njids);
        _save_prefs();
        g_strfreev(jids);

        return;
    }
}

gboolean prefs_get_showsplash(void)
{
    return g_key_file_get_boolean(prefs, "ui", "showsplash", NULL);
}

void prefs_set_showsplash(gboolean value)
{
    g_key_file_set_boolean(prefs, "ui", "showsplash", value);
    _save_prefs();
}

static void _save_prefs(void)
{
    gsize g_data_size;
    char *g_prefs_data = g_key_file_to_data(prefs, &g_data_size, NULL);
    g_file_set_contents(prefs_loc->str, g_prefs_data, g_data_size, NULL);
}

NCURSES_COLOR_T prefs_get_bkgnd() 
{
    return colour_prefs.bkgnd;
}

NCURSES_COLOR_T prefs_get_text() 
{
    return colour_prefs.text;
}

NCURSES_COLOR_T prefs_get_online() 
{
    return colour_prefs.online;
}

NCURSES_COLOR_T prefs_get_err() 
{
    return colour_prefs.err;
}

NCURSES_COLOR_T prefs_get_inc() 
{
    return colour_prefs.inc;
}

NCURSES_COLOR_T prefs_get_bar() 
{
    return colour_prefs.bar;
}

NCURSES_COLOR_T prefs_get_bar_text() 
{
    return colour_prefs.bar_text;
}