aboutsummaryrefslogtreecommitdiff
path: root/emacs/.emacs.d
diff options
context:
space:
mode:
authorYuchen Pei <id@ypei.org>2025-08-28 08:49:51 +1000
committerYuchen Pei <id@ypei.org>2025-08-28 08:49:51 +1000
commit124a2a6eaa2a8d96405168693f9c8e8afc51b10a (patch)
tree3cdfc91d93c5aec48acf6a0a31d4dc9fe30013a9 /emacs/.emacs.d
parent4ec9311b65f343ceb9efd6709908a6e5ee44b6d7 (diff)
[emacs] misc fixes
Diffstat (limited to 'emacs/.emacs.d')
-rw-r--r--emacs/.emacs.d/init/ycp-prog.el1
-rw-r--r--emacs/.emacs.d/lisp/my/my-editing.el7
-rw-r--r--emacs/.emacs.d/lisp/my/my-mariadb.el66
-rw-r--r--emacs/.emacs.d/lisp/my/my-media-segment.el46
-rw-r--r--emacs/.emacs.d/lisp/my/my-prog.el32
-rw-r--r--emacs/.emacs.d/lisp/my/my-ytdl.el1
6 files changed, 119 insertions, 34 deletions
diff --git a/emacs/.emacs.d/init/ycp-prog.el b/emacs/.emacs.d/init/ycp-prog.el
index 6584491..f74e339 100644
--- a/emacs/.emacs.d/init/ycp-prog.el
+++ b/emacs/.emacs.d/init/ycp-prog.el
@@ -210,6 +210,7 @@
(my-package my-prog
(:delay 10)
(my-keybind global-map "C-c 8" #'my-set-tab-width-to-8)
+ (my-keybind prog-mode-map "C-c M-w" 'my-copy-with-func)
(add-hook 'c-mode-hook 'my-c-set-compile-command)
(define-key c-mode-map (kbd "C-c s") 'my-c-switch-between-header-and-source)
(define-key c++-mode-map (kbd "C-c s")
diff --git a/emacs/.emacs.d/lisp/my/my-editing.el b/emacs/.emacs.d/lisp/my/my-editing.el
index e6499ff..8ce68dd 100644
--- a/emacs/.emacs.d/lisp/my/my-editing.el
+++ b/emacs/.emacs.d/lisp/my/my-editing.el
@@ -189,7 +189,10 @@ by passing optional prefix ARG (\\[universal-argument])."
(beginning-of-line)
(newline)
(forward-line -1)
- (indent-according-to-mode))
+ ;; `indent-according-to-mode' causes cursor to jump to the
+ ;; beginning of an org src block
+ (unless (and (derived-mode-p 'org-mode) (org-in-src-block-p))
+ (indent-according-to-mode)))
(forward-line -1)
(my-new-line-below))))
@@ -547,7 +550,7 @@ With an prefix-arg, copy the file name relative to project root."
(defun my-elide-region (b e)
(interactive "r")
(let ((message-elide-ellipsis
- (if (> 1 (count-lines b (min (1+ e) (point-max))))
+ (if (> (count-lines b (min (1+ e) (point-max))) 1)
(concat comment-start
" [... %l lines elided]
")
diff --git a/emacs/.emacs.d/lisp/my/my-mariadb.el b/emacs/.emacs.d/lisp/my/my-mariadb.el
index d6c2463..1759af2 100644
--- a/emacs/.emacs.d/lisp/my/my-mariadb.el
+++ b/emacs/.emacs.d/lisp/my/my-mariadb.el
@@ -65,7 +65,7 @@
(replace-regexp-in-string
"/src"
"/build/mysql-test/var/log/mysqld.1.1.rr/latest-trace"
- ;; "/build/mysql-test/var/log/mysqld.2.2.rr/latest-trace"
+ ;; "/build/mysql-test/var/log/mysqld.2.1.rr/latest-trace"
(project-root (project-current t))))
(expand-file-name "~/bin/gdb-mi.sh"))))
@@ -291,11 +291,24 @@ switches to the buffer."
(my-save-text-and-switch-to-buffer source file-name)))
(defvar my-mtr-compilation-error-re
- '(mtr "^mysqltest: At line \\([0-9]+\\)" nil 1))
+ '(mtr "^\\([^ ]+\\) +\\(w[0-9]+ \\)?\\[ fail \\]"
+ my-mtr-compilation-error-filename))
-;; (defun my-mtr-find-test-file (test-name &optional dir)
-;; (unless dir (setq dir default-directory))
-;; ())
+(defun my-mtr-compilation-error-filename ()
+ (save-excursion
+ (save-match-data
+ (my-mtr-find-test-file
+ (match-string 1)
+ (project-root (project-current))))))
+
+(defun my-mtr-find-test-file (test-name dir)
+ (pcase-let ((`(,suite ,base) (string-split test-name "\\.")))
+ (seq-find
+ (lambda (file)
+ (string-match-p (format "%s\\(/t\\)?/%s.test$" suite base) file))
+ (directory-files-recursively dir
+ (format "%s.test" base))))
+ )
(defun my-mtr-set-compile-command ()
(when (and buffer-file-name
@@ -306,11 +319,11 @@ switches to the buffer."
(test-name
(progn
(when (string-match
- "^.*/mysql-test/\\(.+?\\)/\\(t/\\)?\\([^/]+\\)\\.test$"
+ "^.*/mysql-test/\\(suite/\\)?\\(.+?\\)/\\(t/\\)?\\([^/]+\\)\\.test$"
buffer-file-name)
(format "%s.%s"
- (match-string 1 buffer-file-name)
- (match-string 3 buffer-file-name))))))
+ (match-string 2 buffer-file-name)
+ (match-string 4 buffer-file-name))))))
(setq-local
compile-command
(format "%s %s %s %s"
@@ -319,5 +332,42 @@ switches to the buffer."
test-name
"--rr")))))
+(defun my-mtr-remove-if-1 ()
+ "Remove if (1) blocks"
+ (interactive)
+ (while (re-search-forward
+ (rx bol (0+ space) "if" (0+ space) "(1)" (0+ space) eol)
+ nil t)
+ (kill-whole-line)
+ (my-delete-pair-dwim)))
+
+(defun my-mtr-remove-if-0 ()
+ "Remove if (0) blocks"
+ (interactive)
+ (while (re-search-forward
+ (rx bol (0+ space) "if" (0+ space) "(0)" (0+ space) eol)
+ nil t)
+ (kill-whole-line)
+ (kill-sexp)))
+
+(defun my-mtr-average ()
+ "Calculate average time of mtr --repeat output."
+ (interactive)
+ (let ((run (make-hash-table :test 'equal))
+ (name) (time))
+ (while (re-search-forward "^\\([^ ]+\\).*pass \\] +\\([0-9]+\\)$" nil t)
+ (setq name (match-string 1)
+ time (string-to-number (match-string 2)))
+ (puthash name (cons time (gethash name run)) run))
+ (with-temp-buffer
+ (maphash
+ (lambda (k v)
+ (insert k " " (format "%d" (/ (seq-reduce '+ v 0) (length v))) "\n"))
+ run)
+ (goto-char (point-min))
+ (sort-lines nil (point-min) (point-max))
+ (align-regexp (point-min) (point-max) "\\(\\s-*\\) ")
+ (message (buffer-string)))))
+
(provide 'my-mariadb)
;;; my-mariadb.el ends here
diff --git a/emacs/.emacs.d/lisp/my/my-media-segment.el b/emacs/.emacs.d/lisp/my/my-media-segment.el
index f222316..003953a 100644
--- a/emacs/.emacs.d/lisp/my/my-media-segment.el
+++ b/emacs/.emacs.d/lisp/my/my-media-segment.el
@@ -50,18 +50,20 @@ The process can be started by applying 'start-process' on START-PROCESS-ARGS."
(when my-media-segment-queued-jobs
(funcall (pop my-media-segment-queued-jobs))))
-(defun my-segment-media-file-1 (media-file-name desc-file-name)
+(defun my-segment-media-file-1 (media-file-name info-file-name)
"Run ffmpeg asynchronously to segment file-name according to description.
Uses `my-media-segment-max-inflight' to limit number of inflight tasks."
(interactive (list
(read-file-name "Choose media file: ")
- (read-file-name "Choose description file: ")))
+ (read-file-name
+ "Choose description file (.info.json or .description): "
+ ;; nil
+ ;; (file-name-with-extension media-file-name ".info.json")
+ ;; t
+ )))
(let* ((dir (file-name-sans-extension (expand-file-name media-file-name)))
- (info (my-get-media-segments
- (with-temp-buffer
- (insert-file-contents desc-file-name)
- (buffer-string))))
+ (info (my-get-media-segments info-file-name))
(total (length info))
(pad (1+ (floor (log10 total))))
(idx 0)
@@ -94,12 +96,29 @@ Uses `my-media-segment-max-inflight' to limit number of inflight tasks."
(funcall thunk)
(my-media-segment-enqueue-process thunk))))))
-(defun my-get-media-segments (description)
+(defun my-get-media-segments (info-file-name)
+ (if (equal (file-name-extension info-file-name) "json")
+ (my-get-media-segments-from-json info-file-name)
+ (my-get-media-segments-from-descr info-file-name)))
+
+(defun my-get-media-segments-from-json (json-file-name)
+ (let ((info
+ (with-temp-buffer
+ (insert-file-contents json-file-name)
+ (goto-char (point-min))
+ (json-read))))
+ (seq-map
+ (lambda (ch)
+ (let-alist ch
+ (list :title (my-make-filename .title) :start (format "%s" .start_time)
+ :end (format "%s" .end_time))))
+ (alist-get 'chapters info))))
+
+(defun my-get-media-segments-from-descr (descr-file-name)
"Output title start end triplets."
(let ((results) (title) (start) (end))
(with-temp-buffer
- (erase-buffer)
- (insert description)
+ (insert-file-contents descr-file-name)
(goto-char (point-min))
(save-excursion
(while (re-search-forward
@@ -127,19 +146,16 @@ Uses `my-media-segment-max-inflight' to limit number of inflight tasks."
)))
(defvar my-segment-media-max-async 10)
-(defun my-segment-media-file (media-file-name desc-file-name synchronously)
+(defun my-segment-media-file (media-file-name info-file-name synchronously)
"Run ffmpeg asynchronously to segment file-name according to description.
With a prefix-arg, run synchronously."
(interactive (list
(read-file-name "Choose media file: ")
- (read-file-name "Choose description file: ")
+ (read-file-name "Choose info file: ")
current-prefix-arg))
(let* ((dir (file-name-sans-extension (expand-file-name media-file-name)))
- (info (my-get-media-segments
- (with-temp-buffer
- (insert-file-contents desc-file-name)
- (buffer-string))))
+ (info (my-get-media-segments info-file-name))
(total (length info))
(idx 0))
(when (or synchronously (<= total my-segment-media-max-async)
diff --git a/emacs/.emacs.d/lisp/my/my-prog.el b/emacs/.emacs.d/lisp/my/my-prog.el
index 92fcf21..eead408 100644
--- a/emacs/.emacs.d/lisp/my/my-prog.el
+++ b/emacs/.emacs.d/lisp/my/my-prog.el
@@ -444,7 +444,12 @@ overlay arrow in source buffer."
(defun my-gud-watch-expr (expr)
(with-current-buffer gud-comint-buffer
- (insert "watch -l " expr)
+ (insert "wl " expr)
+ (comint-send-input)))
+
+(defun my-gud-break-expr (expr)
+ (with-current-buffer gud-comint-buffer
+ (insert "b " expr)
(comint-send-input)))
(defun my-gud-print-expr (expr)
@@ -454,11 +459,12 @@ overlay arrow in source buffer."
(defun my-gud-print-expr-region (b e)
(interactive "r")
- (unless (eq (gdb-get-source-buffer) (current-buffer))
- (error "Not in the source buffer"))
- (if current-prefix-arg
- (my-gud-watch-expr (buffer-substring b e))
- (my-gud-print-expr (buffer-substring b e))))
+ (let ((selection (buffer-substring b e)))
+ (pcase (prefix-numeric-value current-prefix-arg)
+ (16 (my-gud-break-expr selection))
+ (4 (my-gud-watch-expr selection))
+ (_ (my-gud-print-expr selection))))
+ (deactivate-mark))
;;; which-func
(defun my-copy-which-func ()
@@ -466,11 +472,19 @@ overlay arrow in source buffer."
(kill-new (which-function))
)
+(defun my-copy-with-func (b e)
+ (interactive "r")
+ (kill-new
+ (concat comment-start "in " (which-function) ":" comment-end "\n"
+ (buffer-substring b e)))
+ (deactivate-mark)
+ (message "Copied current region with function name"))
+
(defun my-set-header-line-to-which-func ()
(setq header-line-format
- '((which-func-mode
- ("" which-func-format " ")
- ))))
+ '((which-func-mode
+ ("" which-func-format " ")
+ ))))
;; override bookmark-make-record for easier default bookmark name.
(defun my-bookmark-make-record ()
diff --git a/emacs/.emacs.d/lisp/my/my-ytdl.el b/emacs/.emacs.d/lisp/my/my-ytdl.el
index 26700fd..5676101 100644
--- a/emacs/.emacs.d/lisp/my/my-ytdl.el
+++ b/emacs/.emacs.d/lisp/my/my-ytdl.el
@@ -61,6 +61,7 @@
;; "%(id)s.%(ext)s" ;; for long names
"%(playlist|.)s/%(playlist_index|)s%(playlist_index&-|)s%(title)s.%(ext)s"
"--write-description"
+ "--write-info-json"
"--write-thumbnail"))
(defvar my-ytdl-audio-download-dir "~/Downloads"