;;; my-prog.el -- Programming related extensions for emacs core -*- lexical-binding: t -*- ;; Copyright (C) 2023 Free Software Foundation. ;; Author: Yuchen Pei ;; Package-Requires: ((emacs "28.2")) ;; This file is part of dotfiles. ;; dotfiles is free software: you can redistribute it and/or modify it under ;; the terms of the GNU Affero General Public License as published by ;; the Free Software Foundation, either version 3 of the License, or ;; (at your option) any later version. ;; dotfiles 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 Affero General ;; Public License for more details. ;; You should have received a copy of the GNU Affero General Public ;; License along with dotfiles. If not, see . ;;; Commentary: ;; Programming related extensions for emacs core. Covers comint, ;; shell, eshell, elisp, prog-mode, c, c++, etc. ;;; Code: ;;; comint, shell, eshell (defvar comint-buffer-list nil) (setq display-buffer-alist '(("\\*shell\\*.*" . (display-buffer-same-window)))) (defun my-shell-with-directory (dir) "Starts a new shell with prompted directory as the cwd" (interactive (list (read-directory-name "Current dir: "))) (let ((tmp-dir default-directory) (old-buffer (current-buffer))) (setq default-directory dir) (shell (generate-new-buffer-name "*shell*")) (with-current-buffer old-buffer (setq default-directory tmp-dir)))) (defun my-comint-send-input-and-return-prompt () (interactive) (comint-send-input) (comint-previous-prompt 1) (recenter 0 t)) ;; FIXME: not working properly (defun my-restart-shell () (interactive) (ignore-error (comint-send-eof)) (shell (current-buffer)) (message "Shell restarted!")) (defun my-shell-disable-company-if-remote () (when (and (fboundp 'company-mode) (file-remote-p default-directory)) (company-mode -1))) (defun my-eshell-insert-prompt-prefix () (interactive) (let ((prompt (funcall eshell-prompt-function))) (string-match "\\(^.*:\\).*$" prompt) (when (match-string 1 prompt) (insert (match-string 1 prompt))))) (defun my-eshell-send-input-and-return-prompt () (interactive) (eshell-send-input) (eshell-previous-prompt 1)) ;;; c (defun my-c-set-compile-command () (unless (file-exists-p "Makefile") (setq compile-command (let ((file (file-name-nondirectory buffer-file-name))) (format "%s -o %s %s %s %s" ;;"%s -c -o %s.o %s %s %s" (or (getenv "CC") "gcc") (file-name-sans-extension file) (or (getenv "CPPFLAGS") "-DDEBUG=9") (or (getenv "CFLAGS") "-ansi -pedantic -Wall -g") file))))) ;;; To override `xref-query-replace-in-results'. (defun my-xref-query-replace-in-results (from to) "Perform interactive replacement of FROM with TO in all displayed xrefs. This function interactively replaces FROM with TO in the names of the references displayed in the current *xref* buffer. When called interactively, it uses '.*' as FROM, which means replace the whole name, and prompts the user for TO. If invoked with prefix argument, it prompts the user for both FROM and TO. As each match is found, the user must type a character saying what to do with it. Type SPC or `y' to replace the match, DEL or `n' to skip and go to the next match. For more directions, type \\[help-command] at that time. Note that this function cannot be used in *xref* buffers that show a partial list of all references, such as the *xref* buffer created by \\[xref-find-definitions] and its variants, since those list only some of the references to the identifiers." (interactive (let* ((fr (if current-prefix-arg (read-regexp "Query-replace (regexp)" ".*") "\\(.*\\)")) (prompt (if current-prefix-arg (format "Query-replace (regexp) %s with: " fr) "Query-replace all matches with: "))) (list fr (read-regexp prompt)))) (let* (item xrefs iter) (save-excursion (while (setq item (xref--search-property 'xref-item)) (when (xref-match-length item) (push item xrefs)))) (unwind-protect (progn (goto-char (point-min)) (setq iter (xref--buf-pairs-iterator (nreverse xrefs))) (xref--query-replace-1 from to iter)) (funcall iter :cleanup)))) (defun my-set-tab-width-to-8 () (interactive) (setq tab-width 8)) (defun my-toggle-debug-on-error-quit (arg) (interactive "P") (if arg (toggle-debug-on-quit) (toggle-debug-on-error)) ) (require 'my-buffer) (defun my-switch-or-create-shell-buffer (arg) "Switch to or create a shell buffer. If there's no buffer with shell mode, or with a prefix-arg, create a shell buffer using `my-shell-with-directory'" (interactive "P") (if (or arg (not (seq-filter (lambda (buffer) (with-current-buffer buffer (derived-mode-p 'shell-mode))) (buffer-list)))) (call-interactively 'my-shell-with-directory) (my-buffer-quick-major-mode 'shell-mode))) (provide 'my-prog) ;;; my-prog.el ends here