summaryrefslogtreecommitdiff
path: root/ar/.config/LazyVim/lua/config/autocmds.lua
blob: 6550daf96bc2c59c7ec17a7229eec29d446de6db (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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
--------------------------------------------------------------
-- ######################################################## --
-- ##################      Custom      #################### --
-- ######################################################## --
--------------------------------------------------------------

-- Save file as sudo on files that require root permission
vim.api.nvim_create_user_command("SudoWrite", function()
  vim.cmd("write !sudo tee % >/dev/null")
  vim.cmd("edit!")
end, {})

-- Enable Goyo by default for mutt writing
local goyo_group = vim.api.nvim_create_augroup("GoyoForMutt", { clear = true })
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
  pattern = "/tmp/neomutt*",
  group = goyo_group,
  callback = function()
    vim.g.goyo_width = 80
    vim.cmd("Goyo")
    vim.cmd("set bg=light")
    vim.cmd("set linebreak")
    vim.cmd("set wrap")
    vim.cmd("set textwidth=0")
    vim.cmd("set wrapmargin=0")
    vim.cmd("colorscheme seoul256")
    vim.api.nvim_buf_set_keymap(
      0,
      "n",
      "<leader>gx",
      ":Goyo|x!<CR>",
      { noremap = true, silent = true, desc = "Goyo Quit" }
    )
    vim.api.nvim_buf_set_keymap(
      0,
      "n",
      "<leader>gq",
      ":Goyo|q!<CR>",
      { noremap = true, silent = true, desc = "Goyo Abort" }
    )
  end,
})

-- Vimwiki
-- Ensure files are read with the desired filetype
vim.g.vimwiki_ext2syntax = {
  [".Rmd"] = "markdown",
  [".rmd"] = "markdown",
  [".md"] = "markdown",
  [".markdown"] = "markdown",
  [".mdown"] = "markdown",
}
-- Set up Vimwiki list
vim.g.vimwiki_list = { {
  path = vim.fn.expand("~/.local/share/vimwiki"),
  syntax = "markdown",
  ext = ".md",
} }
-- Markdown for specific files and directories
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
  pattern = { "/tmp/calcurse*", "~/.calcurse/notes/*" },
  command = "set filetype=markdown",
})

-- Groff for specific file extensions
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
  pattern = { "*.ms", "*.me", "*.mom", "*.man" },
  command = "set filetype=groff",
})

-- TeX for .tex files
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
  pattern = { "*.tex" },
  command = "set filetype=tex",
})

-- When shortcut files are updated, renew bash and lf configs with new material:
local config_group = vim.api.nvim_create_augroup("ConfigUpdate", { clear = true })
vim.api.nvim_create_autocmd("BufWritePost", {
  pattern = { "bm-files", "bm-dirs" },
  group = config_group,
  callback = function()
    -- Execute the 'shortcuts' shell command
    vim.fn.system("shortcuts")

    -- Check if the 'shortcuts' command was successful
    if vim.v.shell_error == 0 then
      -- Display a message in Neovim
      vim.api.nvim_echo({ { "shortcuts updated", "None" } }, true, {})
    else
      -- Optional: Display an error message if the 'shortcuts' command fails
      vim.api.nvim_echo({ { "failed to update shortcuts", "ErrorMsg" } }, true, {})
    end
  end,
})

-- Run xrdb whenever Xdefaults or Xresources are updated.
vim.api.nvim_create_autocmd({ "BufRead", "BufNewFile" }, {
  pattern = { "Xresources", "Xdefaults", "xresources", "xdefaults" },
  group = config_group,
  callback = function()
    vim.bo.filetype = "xdefaults"
  end,
})
vim.api.nvim_create_autocmd("BufWritePost", {
  pattern = { "Xresources", "Xdefaults", "xresources", "xdefaults" },
  group = config_group,
  callback = function()
    vim.cmd("!xrdb %")
  end,
})

-- Recompile dwmblocks on config edit.
local home = os.getenv("HOME") -- Gets the home directory
local dwmblocks_path = home .. "/.local/src/suckless/dwmblocks/config.h"
vim.api.nvim_create_autocmd("BufWritePost", {
  pattern = dwmblocks_path,
  group = vim.api.nvim_create_augroup("DwmblocksConfigGroup", { clear = true }),
  callback = function()
    vim.cmd(
      "!cd "
        .. home
        .. "/.local/src/suckless/dwmblocks/ && sudo make install && { killall -q dwmblocks; setsid -f dwmblocks; }"
    )
  end,
})

-- Autocommand group for DWM
vim.api.nvim_create_augroup("DwmConfigGroup", { clear = true })
vim.api.nvim_create_autocmd("BufWritePost", {
  pattern = home .. "/.local/src/suckless/dwm/config.h",
  group = "DwmConfigGroup",
  callback = function()
    vim.cmd("!extractkeys")
  end,
})

-- Autocommand group for ST
vim.api.nvim_create_augroup("StConfigGroup", { clear = true })
vim.api.nvim_create_autocmd("BufWritePost", {
  pattern = home .. "/.local/src/suckless/st/config.h",
  group = "StConfigGroup",
  callback = function()
    vim.cmd("!extractkeys")
  end,
})

vim.api.nvim_create_autocmd({ "BufRead", "BufEnter" }, {
  pattern = { "*.c", "*.cpp", "*.h", "*.hpp" },
  callback = function()
    local suckless_path = vim.fn.expand("~/.local/src"):gsub("/+$", "")
    local file_path = vim.fn.expand("%:p"):gsub("/+$", "")
    if file_path == suckless_path or file_path:find("^" .. suckless_path .. "/") then
      vim.b.autoformat = false
    end
  end,
})