") #'noa/org-capture)
(defun noa/org-capture ()
"Capture a simple note without the dispatcher."
(interactive)
(org-capture nil "j"))
(defun noa/org-agenda ()
"Directly open the agenda with todo list."
(interactive)
(org-agenda nil "n"))
(global-set-key (kbd "C-c a") #'noa/org-agenda)
(global-set-key (kbd "C-c l") #'org-store-link)
;; Current org mode top level heading
(defun noa/get-current-org-heading ()
(interactive)
(message (car (org-get-outline-path t))))
;; Nicer org-mode tables
(package-ensure 'valign)
(add-hook 'markdown-mode-hook #'valign-mode)
(add-hook 'org-mode-hook #'valign-mode)
(setopt valign-fancy-bar t)
(setopt valign-max-table-size 0)
;; Markdown
;; (package-ensure 'markdown-mode)
(add-to-list 'auto-mode-alist
'("\\.\\(?:md\\|markdown\\|mkd\\|mdown\\|mkdn\\|mdwn\\)\\'" . markdown-mode))
;; Autocorrection
;; Abbrev mode expands one string into another string. I use it as a simple autocorrect mode. If i misspell a word, i run C-x a i g which will prompt me for what to expand the previous word into. I type the correct spelling, and whenever i make that mistake again, it will automatically be corrected. It's important to be careful not to set something that could be a typo for two words though, because otherwise it gets even more annoying. Luckily it's easy to update the abbrevs which are stored in ~/.config/emacs/abbrev_defs. M-x list-abbrevs is also a nice command which shows all the saved abbrevs and how many times they've been expanded.
(add-hook 'text-mode-hook #'abbrev-mode)
;; Spellcheck
;; Jinx is a package for spellchecking. Previously i used [[https://www.gnu.org/software/emacs/manual/html_node/emacs/Spelling.html][flyspell]], which is built in, and [[https://codeberg.org/ideasman42/emacs-spell-fu][spell-fu]]. Flyspell is not ideal because it only checks the word under the point. Furthermore, the correction interface is not pleasant to look at with a proportional font as it uses spaces to align the candidates. Spell-fu checks all the words that are visible, thereby behaving much more like a traditional spell checker. Jinx improves on spell-fu by interacting with the spellcheck process in a more efficient way, and has a nicer interface to corrections.
(package-ensure 'jinx)
(setopt global-jinx-mode t)
;; Replace the default spellcheck binding with jinx
(keymap-global-set "M-$" #'jinx-correct)
(keymap-global-set "C-M-$" #'jinx-languages)
;; Add corrected words to abbrev
;; This is a cool function i took from the jinx wiki. It automatically creates an abbrev for words i correct, so if i make the same error again, it gets fixed without me having to do anything!
(defun jinx--add-to-abbrev (overlay word)
"Add abbreviation to `global-abbrev-table'.
The misspelled word is taken from OVERLAY. WORD is the corrected word."
(let ((abbrev (buffer-substring-no-properties
(overlay-start overlay)
(overlay-end overlay))))
(message "Abbrev: %s -> %s" abbrev word)
(define-abbrev global-abbrev-table abbrev word)))
(advice-add 'jinx--correct-replace :before #'jinx--add-to-abbrev)
;; Consistent face on hover
;; Almost everywhere else, the ~highlight~ face is used for interactive text on mouse over. However, jinx reuses its own ~jinx-highlight~ face, which i don't really like. This face clashes with the face i use for the active region, which means selecting text across a misspelled word can be a bit confusing. If i change the ~jinx-highlight~ face, it looks wrong when i've activated jinx to correct the word.
(put 'jinx-overlay 'mouse-face '(jinx-misspelled highlight))
;; I mentioned that i thought this wasn't the best default behaviour [[https://github.com/minad/jinx/discussions/184][in the jinx repository]], but Daniel seems happy with it. I appreciate that he's made it so easy to change the behaviour to something i prefer.
;; Publishing my website
;; I generate my website using org mode. I use the built in ox-publish subsystem for convenience. However, it isn't all that convenient, as it doesn't actually work very well at the moment.
(defun read-file-as-string (filename)
"Read file contents from FILENAME."
(with-temp-buffer
(insert-file-contents filename)
(buffer-string)))
;; (setq noa/website-header (read-file-as-string "/home/noa/projects/org-website/templates/header.html"))
(setq noa/website-footer "noa.pub ·
cc by-sa ·
feed ·
archive ·
friends
")
(setq noa/website-footer "")
(setq org-publish-project-alist
`(("website"
:components ("website-pages" "website-assets" "website-feed"))
("website-pages"
:publishing-function org-html-publish-to-html
:base-directory "~/Documents/website"
:publishing-directory "~/Documents/public_html"
:base-extension "org"
:with-drawers t
:html-link-home "/"
:html-head-include-default-style nil
:html-head-include-scripts nil
:html-doctype "html5"
;; :html-validation-link nil
:html-preamble ""
:html-postamble ,noa/website-footer
:html-home/up-format ""
:html-link-up ""
:html-html5-fancy t
:html-indent nil ;; breaks tag
:html-head "
"
:auto-sitemap t
:sitemap-filename "~/Documents/website/index.org"
:sitemap-title "noa.pub"
:sitemap-style list
:sitemap-sort-folders ignore
:sitemap-ignore-case t
)
("website-assets"
:publishing-function org-publish-attachment
:base-directory "~/Documents/website"
:publishing-directory "/home/noa/data/public_html"
:base-extension "css\\|js\\|png|\\jpg|\\ico"
:recursive t)
("website-feed"
:base-directory "~/Documents/website"
:base-extension "org"
:publishing-directory "~/Documents/public_html"
:publishing-function (org-rss-publish-to-rss)
:html-link-home "https://noa.pub/"
:html-link-use-abs-url t
:exclude ".*"
:include ("index.org" "boke.org"))))
;; We also need to install a newer version of htmlize so that we can properly convert syntax highlighted code into html. I wish there was a way to turn this off, but i couldn't easily find it, so we're stuck with it for now.
;; (package-ensure 'htmlize)
;; Getting help
;; Emacs is great because of its great built in help system!
;; Close help buffers more easily
;; By default, opening a help buffer keeps the original window active, but this means that i have to go into the help buffer to close it. Some people might like to always have the help buffer hanging around, but i like my screen empty of distractions. Luckily there's a simple setting to select the help window so that i can easily close it again with q.
(setopt help-window-select t)
;; Also to that end, we use the same window as the help buffer to open source files.
(setopt help-window-keep-selected t)
;; Better aesthetics
;; Help buffers put quotes around links in the buffer. I don't think this adds anything to the output.
(setopt help-clean-buttons t)
;; The arguments are shown in upper case. I don't want to be shouted at! I already define them to be bold, this is all the attention they need.
(setopt help-downcase-arguments t)
;; Edit variables from help buffers
;; Often, i will look at the documentation for a variable and immediately want to play with it. This removes one step between wanting that and being able to do that.
(setopt help-enable-variable-value-editing t)
;; Web browsing
;; I hear it's now possible to run a full fat browser inside emacs. But this is surely quite heavyweight, and doesn't get to take advantage of things like ublock origin. When it comes to alternative browsers, it's always the same story; i like the concept, but most websites are a horrific experience without a good ad blocker.
;; Instead, i use eww, a browser more closely aligned with browsers for the terminal. Despite the name, eww is a delight to use for text-heavy websites. If a website doesn't render well in it, because it uses fancy layout tricks or lots of javascript, we can press ~&~ to open the url in firefox.
(setopt browse-url-browser-function 'eww-browse-url
browse-url-secondary-browser-function 'browse-url-default-browser)
;; For the kind of sites i use eww to visit, i've not had a use for cookies. We can tell emacs that we don't trust cookies from any sites, we don't trust cookies from all sites, and frankly, we don't want to use cookies.
(setopt url-cookie-trusted-urls '()
url-cookie-untrusted-urls '(".*")
shr-cookie-policy nil)
;; Eww has rudimentary support for colours. But i don't want web pages to be able to specify their own colours, because i like the colours i already have set.
(setopt shr-use-colors nil)
;; Shr has the ability to break paragraphs to fit on the screen. Instead of this, we set it to not break any lines, and use visual-fill-column-mode to do this for us instead.
(setopt shr-max-width nil)
;; We can set what the maximum size of an image in a window should be. This is a fraction of the total window width or height, and if the image would be bigger than this, it'll be resized to fit. It's useful to have it smaller because emacs still sort of chokes on scrolling when there are large images in a buffer. This is the default value of this option.
(setopt shr-max-image-proportion 0.9
shr-discard-aria-hidden t)
(setopt shr-bullet " • ")
;; The default name for the eww buffer is *eww*. This is unhelpful because it makes having more than one eww buffer open a bit of a chore to navigate. We can set it to 'url, 'title, or a function. I set it to 'title because marginalia already shows me the url. However, this means that i can't search for a url name when switching buffers. See the help for this variable for an example of a function which gives the page title and the url.
(setopt eww-auto-rename-buffer 'title)
;; Goto address mode makes urls and email address in a buffer clickable. I want these clickable links to look like links, because that's what they are. The two mouse face variables are what face is used on hover, which at the moment i ignore. It might also be worth setting them to 'highlight.
(setopt global-goto-address-mode t)
(setopt goto-address-mail-face 'link)
(setopt goto-address-mail-mouse-face 'highlight)
(setopt goto-address-url-face 'link)
(setopt goto-address-url-mouse-face 'highlight)
(setopt global-eldoc-mode t)
;; Use a bar cursor and blink it and don't stop blinking it. i don't know how i feel about this yet to be honest, but it helps me know which window is active so for now i'm keeping it
(setopt
cursor-type 'bar
blink-cursor-mode t
blink-cursor-interval 0.7)
;;; File management
;; Dired is a really nice package which, as with a lot of emacs, has some dodgy defaults. Here we round off some of the sharp edges to make it more enjoyable to use.
;; By default, dired permanently deletes files. But i have quite a bit of storage and also make bad decisions regularly, so it seems fitting to make use of the wonderful invention that is the trash. People who have used systems from the last forty years or so will likely be familiar with this innovation.
(setopt delete-by-moving-to-trash t)
;; It's not fun to be asked every time whether we want to delete a directory recursively. It's an understandable default for safety reasons, but because we are not deleting permanently but rather just moving to the trash, it's not such a concern.
(setopt dired-recursive-deletes 'always)
;; Recursive copying isn't even destructive, so i definitely don't want to be asked about that.
(setopt dired-recursive-copies 'always)
;; After we delete some files or directories, it makes sense to get rid of any buffers which are looking at those files or directories.
(setopt dired-clean-up-buffers-too nil)
;; With this set, if we have two dired buffers open next to one another, a rename operation in one will default to the directory shown in the other. In this way, we can pretend we are using some kind of norton commander like file browser instead of slumming it in emacs.
(setopt dired-dwim-target t)
;; These are some useful ls switches. We have to keep -l. To show dotfiles as well, we use -a. To sort numbers by number order instead of lumping together ones, twos, and so on, we use -v. Because we don't have colour, it's nice to have a clear indicator of what is a file and what is a directory, as well as other different things like symlinks which i never remember. By using -F, a forward slash is appended to every directory. And to get more easily understandable file sizes, we use -h, which will tell us the file size in kilobytes or megabytes rather than a huge number that means nothing to me. I won't explain the meaning of the long flag.
(setopt dired-listing-switches "-alvFh --group-directories-first")
;; By default, don't show dired details
(add-hook 'dired-mode-hook #'dired-hide-details-mode)
;; I find it useful to see the recursive sizes of directories. This can be a little slow, so setting it as always on might not be the best idea, but the longest i've had to wait is about a second, and that's only if i run it on my home directory, so i think it's worth it at the moment.
;; (package-ensure 'dired-du)
(setopt dired-du-size-format t)
(add-hook 'dired-mode-hook #'dired-du-mode)
;; Version control
;; I don't use magit. I tried it once, but my use of version control is very limited to just making some changes and then committing them. I don't work in programming and my projects are simple.
;; Tramp
;; Some tramp settings.
(setopt remote-file-name-inhibit-locks t)
(setopt tramp-inline-compress-start-size 1000)
(setopt tramp-verbose 3)
;; (add-to-list 'tramp-remote-path 'tramp-own-remote-path)
;; The version control system will try each of these methods in order. Because almost everything source controlled i do uses git, i put it first in the list. But at the moment, because i don't think i actually use any of the other methods, i remove the rest of them from the list.
(setopt vc-handled-backends '(Git))
;; (setopt vc-handled-backends '(Git RCS CVS SVN SCCS SRC Bzr Hg))
;; It seems that tramp can also be made faster with these .ssh/config settings.
;; Host *
;; ControlMaster auto
;; ControlPath ~/.ssh/master-%h:%p
;; ControlPersist 10m
;; ForwardAgent yes
;; ServerAliveInterval 60
;;; Behaviour
;; Switching buffers
;; There is a distinction in emacs between manual buffer switching that i initiate, and automatic buffer switching when emacs wants to show a buffer. In practice this means that any rules i write for where to display buffers get ignored if i try to show that buffer myself. This line changes that behaviour.
(setopt switch-to-buffer-obey-display-actions t)
;; History
(setopt history-length 250
kill-ring-max 25)
(setopt savehist-file "~/.config/emacs/savehist")
(setopt savehist-additional-variables
'(kill-ring
command-history
set-variable-value-history
custom-variable-history
query-replace-history
read-expression-history
minibuffer-history
read-char-history
face-name-history
bookmark-history
file-name-history))
(setopt savehist-mode t)
(setopt window-divider-mode t)
(setopt window-divider-default-right-width 1)
(setopt window-divider-default-bottom-width 1)
(setopt window-divider-default-places t)
;; Taken from configuration for the vertico stack:
;; Add prompt indicator to `completing-read-multiple'.
;; We display [CRM], e.g., [CRM,] if the separator is a comma.
(defun crm-indicator (args)
(cons (format "[CRM%s] %s"
(replace-regexp-in-string
"\\`\\[.*?]\\*\\|\\[.*?]\\*\\'" ""
crm-separator)
(car args))
(cdr args)))
(advice-add #'completing-read-multiple :filter-args #'crm-indicator)
;; Do not allow the cursor in the minibuffer prompt
(setopt minibuffer-prompt-properties
'(read-only t cursor-intangible t face minibuffer-prompt))
(add-hook 'minibuffer-setup-hook #'cursor-intangible-mode)
;; Support opening new minibuffers from inside existing minibuffers.
(setopt enable-recursive-minibuffers t)
;; Whether to drop into the debugger on any error. This seems cool, but in practice is a bit annoying.
(setopt debug-on-error nil)
;; Hide commands in M-x which do not work in the current mode.
(setopt read-extended-command-predicate 'command-completion-default-include-p)
(setopt recentf-max-menu-items 25
recentf-save-file "~/.config/emacs/recentf"
recentf-mode t
bookmark-default-file "~/.config/emacs/bookmarks")
;; Undo
;; Undo is on C-/ and redo is on C-S-/. It's not standard, but these bindings are easier to remember. And with this setting, it behaves for the most part like undo in other programs, which isn't as good as i'd really want, but is something i can reason about much more easily than the default undo.
(setopt undo-no-redo t)
;; Saving
;; Backups are pointless in long emacs sessions imo, but autosaves are useful.
(setopt remote-file-name-inhibit-auto-save t)
(setopt remote-file-name-inhibit-auto-save-visited t)
(setopt backup-directory-alist '(("." . "~/.config/emacs/backups/"))
make-backup-files nil
backup-by-copying t
create-lockfiles nil
auto-save-mode t
auto-save-interval 6 ;; every six keystrokes
auto-save-timeout 5 ;; every 5 seconds
auto-save-default t
auto-save-no-message t
save-silently t
version-control t
;; this will auto save to the current file
auto-save-visited-mode t)
(add-hook 'focus-out-hook (lambda () (interactive) (save-some-buffers t)))
(add-hook 'mouse-leave-buffer-hook (lambda () (interactive) (save-some-buffers t)))
;; Unfill commands
(defun unfill-paragraph ()
"Takes a multi-line paragraph and makes it into a single line of text."
(interactive)
(let ((fill-column (point-max)))
(fill-paragraph nil)))
(global-set-key (kbd "M-Q") #'unfill-paragraph)
;; The following functions were written by acdw for use with buffers like dict and help, where it's often ugly to read them with their hard wrapping
;; unfill/refill a buffer
;; unfill makes all paragraphs 1 line
(defun unfill-region (beg end)
(let ((fill-column most-positive-fixnum))
(fill-region beg end)))
;; this command is what you'd run in a hook
;; visual-line-mode makes it so it doesn't look shit
(defun unfill-buffer-force ()
(interactive)
(let ((buffer-read-only nil))
(unfill-region (point-min) (point-max))
(visual-line-mode)))
;; refill makes the width equal to the window-width minus 2
;; (you could change it ofc)
(defun refill-region (beg end)
(let ((fill-column (- (window-width) 2)))
(fill-region beg end)))
;; this command is what you'd run in a hook or w/e
(defun refill-buffer-force ()
(interactive)
(let ((buffer-read-only nil))
(refill-region (point-min) (point-max))))
;; Better control l
;; C-l goes in order, rather than first centering the cursor. This is particularly pleasant with a ~scroll-margin~ greater than the default of zero, which serves to keep a line of context at each edge of the screen, as well as triggering a scroll when the point is that far away from the screen edge.
(setopt recenter-positions '(top middle bottom))
(setopt scroll-margin 1)
;; Smooth scrolling
;; Emacs uses choppy scrolling by default. If i scoll with my trackpad, it's nice to have it move tiny amounts at the same time as my fingers, which pixel-scroll-precision-mode allows for. This also has the benefit of making scrolling over images a little bit of a nicer experience.
;; This doesn't work well sometimes for some reason, so i've disabled it for now.
;; (setopt pixel-scroll-precision-mode t
;; pixel-scroll-precision-use-momentum t)
;; Remember my position in files
(setopt save-place-mode t)
;; More pleasant prompts
;; The former means that when given a list of choices, we can use single character abbreviations to answer. The latter is a fancy way of defaliasing yes-or-no-p to y-or-n-p.
(setopt read-answer-short t)
(setopt use-short-answers t)
;; Disable disabled commands
(setq disabled-command-function nil)
;; Don't save changes in the customize interface
(setopt custom-file (make-temp-file "custom"))
;; Scrolling in compilation
;; Scroll along with text in compilation mode, and stop scrolling at the first error.
(setopt compilation-scroll-output 'first-error)
;; Don't advertise gnu on startup
(setq inhibit-startup-echo-area-message "noa") ;; #userfreedom
;; Better buffer naming
(setopt uniquify-after-kill-buffer-p t
uniquify-buffer-name-style 'forward
uniquify-ignore-buffers-re "^\\*"
uniquify-separator "/")
;; Emacs server
;; (unless (server-running-p) (server-start)))
;; Automatic help at point
;; This puts some help in the minibuffer when we leave the point on some interactive text.
(setopt help-at-pt-display-when-idle 'never)
;; Long lines
;; Better support for long lines.
(setopt global-so-long-mode t)
;; Kill processes without asking
;; This will stop us being prompted before killing a buffer with a running process:
(setopt kill-buffer-query-functions
(remq 'process-kill-buffer-query-function
kill-buffer-query-functions))
;; Automatically revert buffers
;; Automatically revert buffers when they change on disk. This doesn't apply to tramp.
(setopt global-auto-revert-mode t)
;; Visit symlinks
;; This behaviour changes how we visit symlinks.
(setopt find-file-visit-truename t)
(setopt vc-follow-symlinks 'ask)
;; Ibuffer
;; Use ibuffer instead of list-buffers
(global-set-key [remap list-buffers] 'ibuffer)
;; Clicking around
;; By default, clicking on a character will always put the point in front of that character. But it generally feels nicer for a click to put the point on the nearest side of the character to where the mouse clicked, to allow for slightly sloppier clicking.
(setopt mouse-prefer-closest-glyph t)
;; Completing-read everywhere with consult
;; Consult is a package to provide navigation commands that take advantage of completing-read. I set up a nice completing-read environment earlier with vertico. There are a lot of commands built in to consult, and it's possible to define more. But i use it very simply.
;; (package-ensure 'consult)
;; (package-activate 'consult)
;; Consult buffer can be used instead of the default buffer menu. It lists recently used files and bookmarks as well as open buffers.
(autoload #'consult-buffer "consult" nil t)
(global-set-key [remap switch-to-buffer] #'consult-buffer)
;; These are some other almost default functions but with extra interactivity.
(global-set-key [remap yank-pop] #'consult-yank-pop)
(global-set-key [remap goto-line] #'consult-goto-line)
(global-set-key [remap imenu] #'consult-imenu)
(with-eval-after-load 'org
(bind-key [remap imenu] #'consult-org-heading #'org-mode-map))
(global-set-key [remap info] #'consult-info)
;; Annotations for completing-read
;; Marginalia provides us with annotations for candidates in completing read functions. This is things like docstrings for functions, file permissions in find-file, and so on. It's a small quality of life improvement.
;; (package-ensure 'marginalia)
(setopt marginalia-mode t)
;; We want to always show the relative age of a file. By default, files which haven't been modified for more than two weeks will display an absolute date.
(setopt marginalia-max-relative-age most-positive-fixnum)
;; My keyboard has a tab key and an i key. For legacy reasons, by default emacs converts C-i to mean the same thing as the tab key, but i don't really want that. The tab key is called and it gets translated to TAB. C-i is TAB, but i'd rather it by C-i. That's what this decode line does.
(define-key input-decode-map [?\C-i] [C-i])
;; Now that tab and C-i are properly distinguished, i can bind C-i to completion at point.
(global-set-key (kbd "") 'completion-at-point)
;; I also want to make the completion at point function a bit more friendly than the default, so i ask consult to provide the completion functionality.
(setopt completion-in-region-function 'consult-completion-in-region)
;; Minibuffer candidate completion
;; Vertico is a package for a nice minibuffer completion experience. It displays a vertical list of candidates. It integrates well with the emacs ecosystem and lets me use other packages that also play nicely.
;; (package-ensure 'vertico)
(setopt vertico-mode t)
;; We want vertico to take up a maximum of 12 lines on the display. My screen is quite small, so that's fine, but if i had a bigger screen, i might want to look into setting a percentage or increasing this.
(setopt vertico-count 12)
;; By default, vertico uses a font-face trick to put a horizontal line across group titles. It looks quite nice, but doesn't really conform to my design sensibilities, so here i redefine the group format to not have this. Because we no longer have the line, we also align the group name to the left edge.
(setopt vertico-group-format #("%s " 0 3 (face vertico-group-title)))
;; We also want to be able to jump to the bottom of the list by moving up from the top of the list, and the opposite. I've rarely made use of this functionality and i don't know if it's actually a best practice from an interaction perspective, but i'm going to keep it on until it causes an issue for me.
(setopt vertico-cycle t)
;; And of course, i want to be able to interact with vertico with the mouse.
(with-eval-after-load 'vertico
(setopt vertico-mouse-mode t))
;; When completing a filename, i want to be able to easily delete directories in one fell swoop, instead of character by character or word by word. Usually C- would be fine, but if directories have a hyphen or space in their name, i have to press multiple times, which is almost never desirable.
(with-eval-after-load 'vertico
(bind-key (kbd "RET") #'vertico-directory-enter 'vertico-map)
(bind-key (kbd "") #'vertico-directory-delete-char 'vertico-map)
(bind-key (kbd "") #'vertico-directory-delete-word 'vertico-map))
;; If i type ~/ etc in a find-file prompt, get rid of the preceding directory names for a cleaner look.
(add-hook 'rfn-eshadow-update-overlay-hook #'vertico-directory-tidy)
;; Better default completion
;; Some settings for nicer completion with the default emacs completion buffer. I don't use this, because i use vertico.
;; (setopt completion-auto-help 'lazy
;; completion-auto-select 'second-tab
;; completion-show-help nil
;; completions-sort nil
;; completions-header-format nil)
;; Completion styles
;; When given a prompt to select from a list of candidates, there are quite a lot of things we can tweak to improve the experience.
;; The first thing we do is to ignore case, which in these cases is rarely useful. I find that thinking about the case of a candidate is slower than just typing more to narrow down the options. I don't actually know if these make any difference when i've specified a different completion style.
(setopt read-buffer-completion-ignore-case t)
(setopt read-file-name-completion-ignore-case t)
(setopt completion-ignore-case t)
;; Next, we want to set orderless and basic as the two completion style. Basic matches candidates with the same text before the point, and the text after the point as a substring. Orderless takes any number of space separated components and displays candidates that much every component in any order. We specify basic first. What this means in practice is that first we will try and complete exactly what i've input, and if that fails, widen the search with orderless to pick up more options.
;; (package-ensure 'orderless)
(setopt completion-styles '(orderless basic))
(setopt completion-category-overrides '((file (styles basic partial-completion))))
;; By default, emacs overrides the completion styles for email address, but i'm happy with my configuration above.
(setopt completion-category-defaults nil)
;;; Programming
;; Indentation: tabs and whitespace settings
;; In general, my rules for inserting tabs are that the tab key should insert tabs. I personally prefer tabs to spaces, because tabs work reasonably well whatever font or tab width one chooses to set, whereas spaces are the same width for everyone, except when someone uses a proportional font in which case they are narrower than expected. Furthermore, people tend to use spaces for alignment, which looks bad when you can't rely on every character being the same width.
;; However, i'm in the minority, and fighting with the very complicated emacs indentation systems is simply not fun. That said, i refuse to use a monospaced font. Luckily the minority is more than one and someone has already done the hard work for me of writing a mode to make spaces for indentation work reasonably well with a proportional font. That mode is elastic-indent-mode, and it very simply makes leading whitespace characters the same width as the characters on the line above. It's a simple solution but most of the time it does what i want.
(require 'elastic-indent)
(add-hook 'prog-mode-hook #'elastic-indent-mode)
;; Elastic-table-mode is similar; for tab characters within lines, ensure that they change width to make subsequent lines form a table-like layout.
(require 'elastic-table)
(add-hook 'prog-mode-hook #'elastic-table-mode)
;; Previously i used a function to naïvely copy the whitespace from the line above. This is the way that vi, nano, and acme all implement auto-indentation. However, for now i'm experimenting with using the built-in indentation functions again. I'm leaving this defun here for posterity.
;; (defun noa/naive-return-and-indent ()
;; "Insert a newline and copy the indentation of the previous line, vi/nano style."
;; (interactive)
;; (open-line 1)
;; (let* ((start (progn (beginning-of-line) (point)))
;; (indent (progn (back-to-indentation) (point)))
;; (end (progn (end-of-line) (point)))
;; (whitespace (buffer-substring start indent)))
;; (delete-trailing-whitespace start end)
;; (beginning-of-line 2)
;; (insert whitespace)))
;; We will only be trying to indent at the start of a line, and sometimes we will want to insert a standard tab character. We can also set this option to 'complete, which will run completion at point if the region is already indented.
(setopt tab-always-indent nil)
;; Usually, we want indentation to be done with tabs. Some modes make more sense to use spaces to indent. Lisp is a particular example, and emacs's default behaviour of converting tabs into spaces is frankly horrific. I've taken the below code from acdw to use spaces in these modes.
(defvar space-indent-modes '(emacs-lisp-mode
lisp-interaction-mode
lisp-mode
scheme-mode
python-mode)
"Modes to indent with spaces, not tabs.")
(add-hook 'prog-mode-hook
(defun indent-tabs-mode-maybe ()
(setq indent-tabs-mode
(if (apply #'derived-mode-p space-indent-modes) nil t))))
;;; Ocaml
;;; Sentences
;; I prefer to double space sentences. But it seems that most other people do not, and the sentence navigation commands still work for my sentences with this set to nil, but don't work for other people's with it set to t. There are of course some little errors with this, like ending a title with a full stop, but for the most part it's fine.
(setopt sentence-end-double-space nil)
;; If i write a script, i will always run chmod +x after saving it. This command means i don't have to do that.
(add-hook 'after-save-hook #'executable-make-buffer-file-executable-if-script-p)
;; We are on a unix system, so it makes sense to end files in the unix system way. I'm surprised this isn't the default.
(setopt require-final-newline t)
(setopt window-min-height 1
window-combination-resize t
window-resize-pixelwise t
frame-resize-pixelwise t)
;;; Keybindings
;; Zap up to char
(global-set-key (kbd "M-z") 'zap-up-to-char)
;; Insert date and time at point
;; Sometimes it's useful to be able to add a timestamp. I use this for notes. Currently it's bound to as that's what notepad uses.
(defun noa/insert-date-time ()
(interactive)
(insert (format-time-string "<%Y-%m-%d>")))
(global-set-key (kbd "") 'noa/insert-date-time)
;;; Search in buffer
;; Isearch is good, but it has some rough edges. The easiest way forward was just to use ctrlf, which fixes most of them. But i still had some gripes with ctrlf, like that it doesn't play well with a lot of other commands and packages and the general ecosystem built around isearch. So i've tried to fix as many of the issues as i can while keeping real isearch.
;; It makes more sense to go to the start of the match, because i start searching where i want to be.
(defun isearch-exit-at-front ()
"always exit isearch, at the front of search match."
(interactive)
(isearch-exit)
(when isearch-forward
(goto-char isearch-other-end)))
(defun isearch-exit-at-end ()
"Always exit isearch, at the end of search match."
(interactive)
(isearch-exit)
(when (not isearch-forward)
(goto-char isearch-other-end)))
;; My preferred behaviour is for the point to be at the start of the match. Because the search is incremental, usually i won't finish typing something useful before exiting the search, but i always start searching at a place i can reason about. However, i can't figure out how to get this to work along with isearch-mb
;; (define-key isearch-mb-minibuffer-map (kbd "") #'isearch-exit-at-front)
;; (define-key isearch-mb-minibuffer-map (kbd "C-") #'isearch-exit-at-end)
;; Make isearch always quit on C-g
(define-key isearch-mode-map (kbd "C-g") #'isearch-cancel)
(define-key isearch-mode-map (kbd "C-o") #'isearch-occur)
(setopt search-whitespace-regexp ".*?")
(setopt isearch-lax-whitespace t)
(setopt isearch-lazy-count t)
(setopt isearch-allow-motion t)
(setopt isearch-repeat-on-direction-change t)
(setopt isearch-wrap-pause 'no)
;; Okay, i lied about being vanilla. I want isearch to use the minibuffer like a good normal part of emacs
;; (package-ensure 'isearch-mb)
;; (setopt isearch-mb-mode t)
(global-set-key (kbd "M-o") 'other-window)
(global-set-key (kbd "C-x k") 'kill-current-buffer)
;; Smart scan
;; This is a nice package to easily jump between identical things at point, like the * operator in vim. I disabled it because i'm trying out using the same keybindings to jump between compile mode errors.
;; (use-package smartscan)
;; (global-set-key (kbd "M-n") #'smartscan-symbol-go-forward)
;; (global-set-key (kbd "M-p") #'smartscan-symbol-go-backward)
;; (global-set-key (kbd "M-n") #'next-error)
;; (global-set-key (kbd "M-p") #'previous-error)
;; Jef Raskin's leap
;; As a concept, i really like the canon cat. As described [[https://news.ycombinator.com/item?id=33286408][here]], it has two leap keys to navigate the buffer. [[https://dercuano.github.io/notes/eink-design.html][According to Kragen Javier Sitaker]], you could move side to side with the arrow keys, but not up and down. The idea was that any up and down movement would be better served by incremental search. This is something that i want to explore.
;; > If you press and release the LEAP key, it advances the cursor one character forward (or backwards if you hit the left leap key.)
;; > If you press down (but do not release) the LEAP key you enter a search semi-mode. As you type a search term in this semi-mode, the cursor moves to the first instance of that search term it finds. After moving to the first instance of the search term, you release the leap key to exit the search semi-mode.
;; > If you want to move the cursor to a subsequent instance of the search term, you press (and do not release) the "USE FRONT" key and press the leap key again (whose key front is labeled "Leap Again.")
;; Semi-modes seem hard to implement in emacs, although in a lot of ways they are already here. I am not a fan of modes in the vi-sense at all, and isearch in emacs is a mode like this. That is, keybindings work differently when isearching, and you have to press C-g or RET to go back to the normal editing flow.
;; Window management
;; My computer has a small screen, so i find that it's more beneficial for me to split the frame into columns, so i get more context. However, splitting in this way only gives me a (window-width) of 61, so emacs will always split into vertically stacked windows. By setting this to 80, the first split should always be vertical.
(setopt split-width-threshold 80)
;;; Global text search
;; Define a handy function that allows me to do a full text search of every file in my home directory. For the most part, this works well; ripgrep avoids binary files. However, in some files with embedded images, it can add a lot of junk to the output.
(defun noa/consult-rg-home ()
(interactive)
(consult-ripgrep "~/"))
(global-set-key (kbd "M-") #'noa/consult-rg-home)
(setopt shell-file-name "/bin/sh")
(defun snarf-song (url)
(interactive "sYoutube url:")
(async-shell-command
(concat "yt-dlp -x --audio-format=mp3 -o "
(shell-quote-argument "~/media/music/%(title)s [%(id)s].%(ext)s")
" "
(shell-quote-argument url))))
;; Put a quote in the scratch buffer
(setopt cookie-file "~/Documents/quotes")
(setopt initial-scratch-message
(concat (with-temp-buffer
(emacs-lisp-mode)
(insert (cookie cookie-file))
(mark-whole-buffer)
(comment-region (mark) (point))
(buffer-substring (mark) (point)))
"\n\n"))
;;; Other
(setopt confirm-kill-emacs 'y-or-n-p)
(global-set-key (kbd "C-=") #'calc)
;;; Email
;; I like to have my email offline. Of course my preference is also to have it inside of emacs for consistency with everything else. I use some external tools to fetch and send the mail.
;; Reading mail
;; I have experimented with lots of different methods of reading mail, both in and out of emacs. But i keep coming back to rmail, despite its many, many warts.
(setopt rmail-primary-inbox-list
(directory-files "~/Documents/mail/inbox" t "^[^\.]"))
(setopt rmail-file-name "~/Documents/mail/rmail.mbox")
(setopt rmail-user-mail-address-regexp
(rx "noa@noa.pub"))
(setopt rmail-mime-prefer-html nil)
(setopt rmail-mime-attachment-dirs-alist '(("" "~/media")))
(setopt rmail-displayed-headers
(rx bol (or "To" "Cc" "From" "Date" "Subject") ":"))
(setopt rmail-secondary-file-directory "~/Documents/mail/archive/")
(setopt rmail-secondary-file-regexp "\\.mbox\\'")
(setopt rmail-delete-after-output t)
(setopt rmail-default-file "~/Documents/mail/archive/")
(setopt mail-dont-reply-to-names rmail-user-mail-address-regexp)
(add-hook 'rmail-show-message-hook #'visual-line-fill-column-mode)
(setopt rmail-display-summary nil)
(setopt rmail-redisplay-redisplay-summary t)
(setopt rmail-summary-line-count-flag nil)
(setopt rmail-summary-window-size 12)
(defun noa/message-default-headers ()
(format "Fcc: ~/Documents/mail/outbox/%s.mbox"
(format-time-string "%Y-%m")))
(setopt message-default-headers #'noa/message-default-headers)
;; Composing mail
;; Setting this to nil stops auto-fill from being automatically enabled in message buffers.
(setopt message-fill-column nil)
;; It's nice to have a message signature. I want the signature to be loaded from a file, which is stored in my configuration directory.
(setopt message-signature t)
(setopt message-signature-file "~/.config/signature")
;; Sending mail
(setopt message-send-mail-function 'message-send-mail-with-sendmail)
(add-to-list 'exec-path "/home/noa/.config/Scripts")
(setopt sendmail-program (executable-find "msmtpq"))
(setopt message-sendmail-extra-arguments '("--read-envelope-from"))
(setopt message-sendmail-envelope-from 'header)
(setopt message-kill-buffer-on-exit t)
(setopt message-sendmail-f-is-evil t)
(setopt message-forward-as-mime t)
(setopt message-interactive t)
(setopt message-auto-save-directory "~/Documents/mail/drafts")
;;; System administration
;; Eshell
;; Eshell is a command shell written in elisp. It integrates with emacs in a more consistent manner than the other shells available, although it still has its own quirks.
;; In general, i try and avoid using a shell if possible, because i think that bespoke emacs interfaces to different commands tend to have more pleasant interaction methods. But there are still lots of things which are simply easier to do with a shell.
;; Set the eshell banner
;; This is equivalent to the message of the day present in some shells. I wanted it to print a new quote every time eshell opened, but when i tried that eshell refused to load. Probably some mistake on my end.
(defun noa/eshell-banner-message ()
(concat (cookie cookie-file)
"\n\n"))
(setopt eshell-banner-message (noa/eshell-banner-message))
;; Environment variables
(setenv "PAGER" "cat")
(setenv "TERM" "dumb")
(setenv "NO_COLOR")
(setenv "GPG_AGENT_INFO" nil)
;; Network management
;; (package-ensure 'enwc)
;; (setopt enwc-default-backend 'nm)
;; Dictionary
;; Emacs has built in support for interfacing with dictd. With dictd and some dictionaries installed on debian, this works fine out of the box.
;; Unfortunately, dictionaries in this format tend to be hard wrapped and there isn't a lot of coverage outside of english
(setopt dictionary-search-interface nil)
;; Dictionary tooltip mode lets me hover over a word to view the definition.
(setopt dictionary-tooltip-mode t)
;; Unsorted
;; Just a few settings i haven't put into another category yet.
(setopt save-interprogram-paste-before-kill t
mouse-yank-at-point t
require-final-newline t
load-prefer-newer t
ediff-window-setup-function 'ediff-setup-windows-plain)
;; Bibliography management
;; We use ebib to browse the bibliography.
(setopt ebib-preload-bib-files '("~/Documents/references.bib"))
(setopt org-cite-global-bibliography '("~/Documents/references.bib"))
;; The biblio package allows us to import references from online sources.
(package-ensure 'biblio)
(require 'ebib-biblio)
(with-eval-after-load 'ebib
(define-key ebib-index-mode-map (kbd "B") #'ebib-biblio-import-doi)
(define-key biblio-selection-mode-map (kbd "e") #'ebib-biblio-selection-import))
(setopt biblio-download-directory "~/media/papers")
;; Taking notes on papers:
(setopt ebib-notes-directory "~/Documents")
(setopt ebib-notes-locations '("~/Documents/references.org"))
(setopt ebib-notes-default-file "~/Documents/references.org")
(setopt ebib-notes-storage 'multiple-notes-per-file)
;;; Voice notes transcription
(require 'audio-notes-mode)
(setq anm/notes-directory "~/Documents/Voice notes/")
(setq anm/goto-file "~/Documents/notes/notes.org")
(setq anm/player-command '("mpv" "--quiet" file))