summaryrefslogtreecommitdiff
path: root/lua/talha/functions.lua
blob: ff320e372ea03640713475061d25087619463bff (plain)
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
function GetCurrFileName()
  local filename = vim.api.nvim_buf_get_name(0)
  return filename
end

local function _ReloadConfigsInPath(path_selector)
    local config_paths = vim.fn.glob(path_selector, true, true)

    for _, filepath in ipairs(config_paths) do
        dofile(filepath)
    end
end

function ReloadLuaConfig()
    for name,_ in pairs(package.loaded) do
        if name:match('^linux') then
            package.loaded[name] = nil
        end
    end

    -- Reload init/ directory
    local lua_path_selector = vim.fn.stdpath('config') .. '/lua/**/*.lua'
    _ReloadConfigsInPath(lua_path_selector)

    local after_path_selector = vim.fn.stdpath('config') .. '/after/**/*.lua'
    _ReloadConfigsInPath(after_path_selector)

    print('Nvim configurations reloaded')
end

function ToggleSpellCheck()
    vim.opt.spell = not(vim.opt.spell:get())
    vim.opt.spelllang = 'en_us'
end

function GetHomeDir()
  -- for linux/mac
  local home = os.getenv("HOME")
  -- for windows
  local user_profile = os.getenv("UserProfile")

  if home ~= nil then
    return home
  elseif user_profile ~= nil then
    return user_profile
  end
  return nil
end

function SafeCListNav(navdir)
  -- dir:
  --  1 is cnext (forwards)
  -- -1 is cprev (backwards)
  local dircmd = ''
  if navdir == 1 then
    dircmd = 'cnext'
  elseif navdir == -1 then
    dircmd = 'cprev'
  end
  local status, result = pcall(vim.cmd, dircmd)

  if status == false then
    return nil
  end
end

function MakeTags()
  local on_exit = function(obj)
    print('re-generated tags')
  end

  vim.system({'ctags', '-R', '.'}, {text = true}, on_exit)
end

function LoadSnippet(stype)
  local ft = '.lua'
  local buft = vim.bo.filetype
  if buft == 'cpp' then
    ft = '.c'
  else
    ft = '.' .. vim.bo.filetype
  end
	vim.cmd.read { 
		GetHomeDir() .. '/.config/nvim/snippets/' .. stype .. ft
	}
end