aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d/lisp/my/my-prog.el
blob: 6b7c7057278470fb5ecc69e82ec32badb3fd94e8 (plain) (blame)
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
;;; my-prog.el -- Programming related extensions for emacs core -*- lexical-binding: t -*-

;; Copyright (C) 2023 Free Software Foundation.

;; Author: Yuchen Pei <id@ypei.org>
;; 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 <https://www.gnu.org/licenses/>.

;;; 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))
  )

(provide 'my-prog)
;;; my-prog.el ends here