Emacs: Updates helper functions and misc fixes, also readme

This commit is contained in:
hedy 2023-09-12 22:35:15 +08:00
parent 2dffb9f480
commit a5c7d5125b
Signed by: hedy
GPG Key ID: B51B5A8D1B176372
3 changed files with 124 additions and 27 deletions

View File

@ -79,27 +79,109 @@ I've adapted it to automatically use =emacs-lisp :tangle yes= if the file curren
Otherwise (editing any other files) it will read input from the minibuffer for text to insert after the =#+BEGIN_SRC=. The original function used ido completing read with a list of known code types and I've nuked it and just let the user append what ever they want after =#+BEGIN_SRC=: this could be useful for adding =:stuff like this= after the code-type.
#+begin_src emacs-lisp :tangle yes
(defun ./org-insert-src ()
"Insert SRC Block with prompted code type.
Recently I've also added ability to wrap a selected region with src instead, if a region is active, without editting it afterwards.
This is useful when I transfer org documents written with fixed-pitch font environment where I relied on plain text formatting, so when editting in variable-pitch font I can wrap it within src blocks to have it be formatted properly.
Most-recently I've also added deleting an empty src block (created by =./org-insert-src= itself) if point is within a src block with no content other than a single empty line.
#+BEGIN_SRC emacs-lisp :tangle yes
(defun ./org-insert-src (beg end)
"Insert (or wrap region with, or cancel) src block.
If cursor currently on an empty line, it is wrapped in src block, with
no other content in the src block other than the empty line, then the
src block together with the line after it (if empty) is deleted. This
undoes the effect of ./org-insert-src without active region, and
cancelling org-edit-src with C-c C-k.
Code type is prompted and `org-edit-src-code' called only for insert,
not for wrap.
Uses 'emacs-lisp :tangle yes' if currently in user-emacs-directory."
(interactive)
(let ((code-type (if (string=
(file-truename user-emacs-directory)
(file-name-directory (buffer-file-name)))
"emacs-lisp :tangle yes"
(read-from-minibuffer "#+BEGIN_SRC "))))
(progn
(newline)
(insert (format "#+BEGIN_SRC %s\n" code-type))
(newline)
(insert "#+END_SRC\n")
(previous-line 2)
(org-edit-src-code))))
(interactive (if (use-region-p)
(list (region-beginning) (region-end))
(list (point-min) (point-min))))
(let ((selection (buffer-substring-no-properties beg end))
(code-type '(if (string=
(file-truename user-emacs-directory)
(file-name-directory (buffer-file-name)))
"emacs-lisp :tangle yes"
(read-from-minibuffer "#+BEGIN_SRC "))))
(if (< (length selection) 2)
(if (./org-empty-src-p)
;; Delete empty src block and exit
(progn
(previous-line)
(delete-line) ;; Newline also deleted
(delete-line)
(delete-line)
;; Delete empty line inserted by ./org-insert-src itself
(if (./match-line-p "")
(delete-line)))
;; Otherwise:
;; Insert src block with given code type and edit
(progn
(setq code-type (eval code-type))
(deactivate-mark)
(beginning-of-line)
(newline)
(insert (format "#+BEGIN_SRC %s\n" code-type))
(newline)
(insert "#+END_SRC\n")
(previous-line 2)
(org-edit-src-code)))
;; Wrap selected region
;;(setq code-type (eval code-type))
(goto-char beg)
(previous-line) (end-of-line)
(newline)
(insert "#+BEGIN_SRC ")
;; save-excursion doesn't seem to work here
(goto-char (+ end 11)) (end-of-line)
(newline)
(insert "#+END_SRC")
;; FIXME: putting cursor at the begin src part afterwards doesn't work
(re-search-backward "^\\s-*#\\+BEGIN_SRC")
(end-of-line))))
(defun ./match-line-p (regexp &optional move keep-pos start)
"Wrapper around string-match-p to use contents of current line.
Returns whether current line, after moving down by MOVE lines, can be
matched with REGEXP.
If REGEXP is an empty string, return t for empty line, nil otherwise.
MOVE argument is passed to `next-line'.
If REGEXP and is non-nil, REGEXP and START is passed to
`string-match-p' with no changes, and its return-value is returned
as-is.
MOVE argument is passed as-is to `next-line' immediately.
If KEEP-POS is non-nil, pass MOVE argument to `previous-line' after obtaining
contents of the required line."
(next-line move)
(let ((line (buffer-substring-no-properties (line-beginning-position) (line-end-position))))
(if keep-pos
(previous-line move))
(if (= (length regexp) 0)
(if (= (length line) 0)
t nil)
(string-match-p regexp line start))))
(defun ./org-empty-src-p ()
"Return whether point is within a org src block with a single empty line."
(let ((line))
(save-excursion
(if (./match-line-p "^\\s-*#\\+begin_src" -1)
(if (./match-line-p "" +1)
(if (./match-line-p "^\\s-*#\\+end_src" +1)
t nil) nil) nil))))
#+END_SRC
This is also another helper function for editing my literate configuration. It uses the =#+BEGIN_SRC= content from the current code block (that the cursor is in) and splits the block into two by inserting =#+END_SRC= and copying whatever =#+BEGIN_SRC <...>= that was used in the current code block.
Below is also another helper function for editing my literate configuration. It uses the =#+BEGIN_SRC= content from the current code block (that the cursor is in) and splits the block into two by inserting =#+END_SRC= and copying whatever =#+BEGIN_SRC <...>= that was used in the current code block.
- ~save-excursion~ allows execution of some expressions then move the point back to where it was (before the save-excursion call).
- Note that the ~re-search-backward~ seems to be case-insensitive, surprisingly, so both =BEGIN_SRC= and =begin_src= could be matched.

View File

@ -1,6 +1,6 @@
#+title: Packages.El Literate
:PROPERTIES:
:tangle: ~/.config/emacsd/packages.el
:tangle: ~/.config/emacs/packages.el
:END:
#+auto_tangle: t
@ -235,8 +235,8 @@ Enjoy emacs' editting key chords while there's still a glimmer of space in your
:hook (org-mode . org-auto-tangle-mode)
:config
(setq org-auto-tangle-babel-safelist '(
"~/.config/emacsd/packages.el"
"~/.config/emacsd/init.el")))
"~/.config/emacs/packages.el"
"~/.config/emacs/init.el")))
#+END_SRC
@ -487,14 +487,14 @@ With references from System Crafter's crafted-emacs configuration
#+BEGIN_SRC emacs-lisp :tangle yes
(use-package cape
;;:after math-symbol-lists
;;:elpaca (:repo "~/projects/cape/")
:elpaca (:repo "hedyhli/cape")
:elpaca (:repo "~/projects/cape/")
;;:elpaca (:repo "hedyhli/cape")
;;:defer t
:config
;; Add useful defaults completion sources from cape
;; (add-to-list 'completion-at-point-functions #'cape-file)
;; ;;(add-to-list 'completion-at-point-functions #'cape-dabbrev)
(add-to-list 'completion-at-point-functions #'cape-tex)
;;(add-to-list 'completion-at-point-functions #'cape-tex)
(add-to-list 'completion-at-point-functions #'cape-emoji)
;; Silence the pcomplete capf, no errors or messages!
@ -511,10 +511,14 @@ With references from System Crafter's crafted-emacs configuration
;; (define-key evil-insert-state-map (kbd "C-x C-$") (cape-company-to-capf #'company-math-symbols-unicode))
;; (define-key evil-insert-state-map (kbd "C-x C-:") (cape-company-to-capf #'company-emoji))
(cape-char--define math "math" ?\\)
(add-to-list 'completion-at-point-functions #'cape-math)
:hook (eshell-mode-hook . (lambda () (setq-local corfu-quit-at-boundary t
corfu-quit-no-match t
corfu-auto nil)
(corfu-mode)))
)
#+END_SRC

View File

@ -29,6 +29,13 @@ Note that WSL support of clipboard pastes is deprecated on NVIM as of
2023-06-30. Before which, you need to have a `pbcopy`/`pbpaste` binary in path
that handles clipboard operations with windows.
## CHANGELOG
- 2023-09: I've switched from neovim to Doom, and to my own Emacs!
- 2023-06: (nvim) Switched from vim to full-lua set up
---
Repository: [SourceHut](https://sr.ht/~hedy/dotfiles)
Mirrors: [tildegit (gitea)](https://tildegit.org/hedy/dotfiles) |
@ -71,7 +78,7 @@ Mirrors: [tildegit (gitea)](https://tildegit.org/hedy/dotfiles) |
- Editor:
- Vim ([`.vimrc`](.vimrc))
- Neovim ([`.config/nvim`](.config/nvim))
- Emacs ([`.config/emacsd`](.config/emacsd)) (with chemacs: [.emacs-profiles.el](.emacs-profiles.el))
- Emacs ([`.config/emacs`](.config/emacs)) (with chemacs: [.emacs-profiles.el](.emacs-profiles.el))
- Doom ([`.config/doom`](.config/doom))
- Gemini client:
- [amfora](https://github.com/makeworld-the-better-one/amfora) ([`.config/amfora`](.config/amfora))
@ -202,7 +209,7 @@ Other local files recognized:
## Editor
- Vim: only for systems that don't have neovim installed
- Neovim: I use this as my primary editor and IDE
- Neovim: used to be my primary editor, but now **I've switched to emacs** 😈
- Emacs: just to play around and learn elisp
- Doom: looking for IDE features and inspiration for my nvim setup. I might
drop this soon - takes so much time and resources to set it up and maintain
@ -336,6 +343,8 @@ software.
#### TODO for neovim
(I might never complete these since I recently switched to emacs)
- [x] Drop CoC and \<v0.5 support
- [x] Use Lazy.nvim package manager
- [x] Replace lightline with lualine
@ -398,8 +407,10 @@ configuration in there yet.
## TODO
- [ ] add LSP and completion to emacs
- [x] add LSP and completion to emacs
- [x] (n)vim mapping for gg=<backtick><backtick> (not needed anymore since we had `g@` mapping for LSP format)
- [x] `.addpath` file
- [ ] Wait for nvim 0.5 to because more widely adopted in system packages, then
- [x] Wait for nvim 0.5 to because more widely adopted in system packages, then
do the things in [nvim TODO](#todo-for-neovim)
- [ ] Flesh out emacs config to support *most* of my needs for programming and writing
- [ ] (planning) Drop Doom support