Skip to content

Instantly share code, notes, and snippets.

@tkojitu
Last active April 1, 2025 07:23
Show Gist options
  • Save tkojitu/efc4fc905babeb06d9b0 to your computer and use it in GitHub Desktop.
Save tkojitu/efc4fc905babeb06d9b0 to your computer and use it in GitHub Desktop.
; (set-language-environment "Japanese")
(auto-compression-mode t)
;; (setq gc-cons-threshold 5000000)
(tool-bar-mode -1)
(global-set-key [delete] 'delete-char)
(global-set-key [home] 'beginning-of-buffer)
(global-set-key [end] 'end-of-buffer)
(add-to-list 'load-path "C:/Users/tcd/site-lisp/")
(setq mode-require-final-newline nil)
(setq inhibit-startup-message t)
(setq scroll-conservatively 1)
(setq scroll-step 1)
(setq make-backup-files nil)
(setq visible-bell t)
(put 'eval-expression 'disabled nil)
(setq next-line-add-newlines nil)
(setq vc-diff-switches "-x -wp")
(setq vc-handled-backends '(SVN Git))
(setq default-major-mode 'text-mode)
(setq text-mode-hook 'turn-on-auto-fill)
(custom-set-variables
;; custom-set-variables was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(tool-bar-mode nil))
(custom-set-variables '(sort-fold-case t t))
(setq confirm-kill-emacs 'yes-or-no-p)
(autoload 'ruby-mode "ruby-mode"
"Mode for editing ruby source files" t)
(setq auto-mode-alist
(append '(("\\.rb$" . ruby-mode)) auto-mode-alist))
(setq interpreter-mode-alist (append '(("ruby" . ruby-mode))
interpreter-mode-alist))
(autoload 'run-ruby "inf-ruby"
"Run an inferior Ruby process")
(autoload 'inf-ruby-keys "inf-ruby"
"Set local key defs for inf-ruby in ruby-mode")
(add-hook 'ruby-mode-hook
'(lambda ()
(inf-ruby-keys)))
(add-hook 'ruby-mode-hook #'(lambda () (require 'rubymode-aux)))
(add-hook 'ruby-mode-hook #'(lambda () (setq require-final-newline nil)))
(setq auto-mode-alist
(cons '("\\.note\\'" . text-mode) auto-mode-alist))
(setq auto-mode-alist
(cons '("\\.xml\\'" . code-mode) auto-mode-alist))
(require 'ehelp)
(define-key ctl-x-map "?" 'ehelp-command)
(global-set-key "\C-h" 'delete-backward-char)
(global-set-key "\C-q" 'scroll-n-lines-down)
(global-set-key "\C-z" 'scroll-n-lines-up)
(global-set-key "\C-xt" 'insert-date-and-time)
(global-set-key "\C-cg" 'goto-line)
(global-set-key "\C-cf" 'auto-fill-mode)
(global-set-key "\M-t" 'quoted-insert)
(global-set-key "\C-x\C-z" 'keyboard-quit)
(global-set-key "\C-c\C-z" 'suspend-emacs)
(global-set-key "\C-c\M-%" 'query-replace-regexp)
(global-set-key "\M-u" 'upcase-backward)
(global-set-key "\C-cc" 'compile)
(global-set-key "\C-s" 'isearch-forward-regexp)
(setq Buffer-menu-buffer+size-width 36)
(define-key ctl-x-map "\C-b" 'electric-buffer-list)
(global-set-key (kbd "<f11>") '(lambda ()
(interactive)
(switch-to-buffer "*eshell*")))
(defun my-c-common-mode ()
(c-toggle-hungry-state 1)
(setq indent-tabs-mode nil)
(setq c-basic-offset 4))
(add-hook 'c-mode-common-hook 'my-c-common-mode)
(defun my-java-mode-hook ()
(setq c-basic-offset 4)
(c-set-offset 'statement-cont '++)
(c-set-offset 'func-decl-cont 0)
(c-set-offset 'arglist-intro '++)
(c-set-offset 'inher-intro '++)
(setq indent-tabs-mode nil))
(add-hook 'java-mode-hook 'my-java-mode-hook)
(setq auto-mode-alist
(append '(("\\.h$" . c++-mode)) auto-mode-alist))
(defun my-c++-mode-hook ()
;; (c-set-style "ellemtel")
(c-set-style "stroustrup")
(setq c-basic-offset 4)
(setq indent-tabs-mode nil)
(c-set-offset 'arglist-close 0)
(local-set-key [f9] 'switch-c++-buffer))
(add-hook 'c++-mode-hook 'my-c++-mode-hook)
(defun switch-c++-buffer (&optional buffername)
(interactive)
(if (not buffername)
(setq buffername (buffer-name)))
(let ((basename (file-name-sans-extension buffername))
(extension (file-name-extension buffername)))
(let ((filename (concat basename "."
(c++-paired-extension basename extension))))
(find-file filename))))
(defun c++-paired-extension (basename extension)
(if (string= "cpp" extension)
(find-cpp-header basename)
(cond ((string= "h" extension) "cpp")
((string= "hpp" extension) "cpp")
((string= "hh" extension) "cc")
((string= "cc" extension) "hh")
((string= "hxx" extension) "cxx")
((string= "cxx" extension) "hxx")
((string= "H" extension) "C")
((string= "C" extension) "H")
(t nil))))
(defun find-cpp-header (basename)
(if (file-exists-p (concat basename ".h"))
"h"
"hpp"))
(defun my-text-mode-hook ()
(turn-on-auto-fill)
(set-fill-column 68))
;; colorize modes
(defvar paren-face 'paren-face)
(make-face 'paren-face)
(set-face-foreground 'paren-face "#88aaff")
(defvar brace-face 'brace-face)
(make-face 'brace-face)
(set-face-foreground 'brace-face "#ffaa88")
(defvar bracket-face 'bracket-face)
(make-face 'bracket-face)
(set-face-foreground 'bracket-face "#aaaa00")
(defvar bang-face 'bang-face)
(make-face 'bang-face)
(set-face-foreground 'bang-face "#FF0000")
(setq lisp-font-lock-keywords-2
(append '(("(\\|)" . paren-face))
lisp-font-lock-keywords-2))
(setq lisp-font-lock-keywords-2
(append '(("(\\|)" . paren-face))
lisp-font-lock-keywords-2))
(defun scroll-n-lines-down (&optional n)
"Scroll down N lines (1 by default)."
(interactive "P")
(scroll-down (prefix-numeric-value n)))
(defun scroll-n-lines-up (&optional n)
"Scroll up N lines (1 by default)."
(interactive "P")
(scroll-up (prefix-numeric-value n)))
(defun insert-date-and-time ()
"Insert the current date and time."
(interactive "*")
(let* ((time (current-time))
(dow (week-abbrev time))
(fmt (concat "%Y-%m-%d (" dow "), %H:%M")))
(insert (format-time-string fmt time))))
(defun week-abbrev(time)
(let ((dow (nth 6 (decode-time time))))
(cond ((= dow 0) "Sun")
((= dow 1) "Mon")
((= dow 2) "Tue")
((= dow 3) "Wed")
((= dow 4) "Thu")
((= dow 5) "Fri")
((= dow 6) "Sat"))))
(defun gtnote ()
"Delete backups, then open the latest note."
(interactive)
(let ((notes-dir "~/notes"))
(dirclean notes-dir)
(tko-open-latest notes-dir))
(goto-char (point-max)))
(defun tko-open-latest (dir)
(find-file (tko-latest-file (directory-files dir t)))
(goto-char (point-max)))
(defun tko-cmp-file-mdate (a b)
(let ((a-mdate (tko-file-mdate a))
(b-mdate (tko-file-mdate b)))
(tko-cmp-timelist a-mdate b-mdate)))
(defun tko-file-mdate (file)
(nth 5 (file-attributes file)))
(defun tko-cmp-timelist (a b)
(let ((high-result (tko-cmp-timelist-high a b)))
(if (/= high-result 0) high-result
(tko-cmp-timelist-low a b))))
(defun tko-cmp-timelist-high (a b)
(- (car a) (car b)))
(defun tko-cmp-timelist-low (a b)
(- (nth 1 a) (nth 1 b)))
(defun tko-latest-file (files)
(let ((result nil))
(dolist (each files)
(cond
((car (file-attributes each)))
((or (null result) (< (tko-cmp-file-mdate result each) 0))
(setq result each))))
result))
(defun find-yyyymmdd-txt-file ()
(interactive)
(find-file (format-time-string "%Y%m%d.txt")))
(defun dirclean (dir)
"Delete backups in the DIR directory."
(interactive "DWhere is cleaned up?: ")
(let ((files (directory-files dir t))
(file))
(while files
(setq file (car files))
(if (string-match "/#[^/]*#$\\|.*~$" file)
(if (not (file-directory-p file))
(delete-file file)))
(setq files (cdr files)))))
(defun upcase-backward ()
"upcase one word (includs underscore) backward."
(interactive)
(save-excursion
(save-match-data
(let ((current (point)))
(re-search-backward "[^A-Za-z0-9_]\\|^")
(upcase-region (point) current)))))
(defun esc-html (begin end)
(interactive "r")
(save-restriction
(narrow-to-region begin end)
(save-excursion
(save-match-data
(tko-replace-elt "&" "&amp;" begin)
(tko-replace-elt "\"" "&quot;" begin)
(tko-replace-elt "<" "&lt;" begin)
(tko-replace-elt ">" "&gt;" begin)))))
(defun tko-replace-elt (from-string replacement begin)
(goto-char begin)
(while (search-forward from-string nil t)
(replace-match replacement)))
(defun uri-to-link (begin end)
(interactive "r")
(let* ((text-list (split-string (buffer-substring begin end)))
(uri (car text-list))
(body (cadr text-list)))
(if (null body)
(setq body uri))
(save-restriction
(save-excursion
(goto-char begin)
(delete-region begin end)
(insert (format "<a href=\"%s\">%s</a>" uri body))))))
(defun cleanup-buffers ()
(interactive)
(dolist (buf (buffer-list))
(if (or (buffer-file-name buf)
(string-prefix-p "\*vc-diff" (buffer-name buf)))
(kill-buffer buf))))
(defun come-back-scratch ()
(interactive)
(let ((scratch (get-buffer-create "*scratch*")))
(set-buffer-major-mode scratch)
(switch-to-buffer scratch)))
(defun reopen-all ()
(interactive)
(let ((bufname (buffer-name)))
(dolist (buf (buffer-list))
(let ((filename (buffer-file-name buf)))
(if filename
(progn
(kill-buffer buf)
(find-file-noselect filename))))
(switch-to-buffer bufname))))
(defun tko-hungry-backspace (arg)
"Deletes preceding character or all whitespaces."
(interactive "*P")
(let ((here (point)))
(skip-chars-backward " \t\n")
(if (/= (point) here)
(delete-region (point) here)
(delete-backward-char 1))))
(defun joinlines (begin end)
(interactive "r")
(save-excursion
(save-match-data
(subst-char-in-region begin end ?\n ?\ ))))
(defun rupscan ()
(interactive)
(insert "X:/ct/KBProj/develop/src/rupscan"))
(defun clearehsell ()
(interactive)
(let ((eshell-buffer-maximum-lines 0))
(eshell-truncate-buffer)))
(defun switch-to-eshell ()
(interactive)
(switch-to-buffer "*eshell*"))
(defun tko-multi-str (str n)
(let* ((i 0)
(ret ""))
(while (< i n)
(setq ret (concat ret str))
(setq i (+ i 1)))
ret))
(defun cascade-callstack (start end)
(interactive "r")
(save-restriction
(save-excursion
(save-match-data
(reverse-region start end)
(transform-to-tree start end)))))
(defun transform-to-tree (start end)
(interactive "r")
(save-restriction
(save-excursion
(save-match-data
(let ((lines (split-string (buffer-substring-no-properties start end) "\n" t))
(depth 0))
(delete-region start end)
(dolist (line lines)
(insert (repeat-string-n-times "| " (- depth 1)))
(if (> depth 0)
(insert "|- "))
(insert line "\n")
(setq depth (1+ depth))))))))
(defun repeat-string-n-times (str n)
(let ((result ""))
(dotimes (_ n result)
(setq result (concat result str)))))
; .*\.\(db\|dll\|pdb\|exe\|tlog\|log\|obj\|lib\|htm\|idb\|ilk\|pch\|suo\|bsc\|ipch\)$
(defun delete-binary-lines ()
(interactive)
(while (re-search-forward ".*\\.\\(db\\|dll\\|pdb\\|exe\\|tlog\\|log\\|obj\\|lib\\|htm\\|idb\\|ilk\\|pch\\|suo\\|bsc\\|ipch\\|ib_pdb_index\\|ib_tag\\|map\\|iobj\\|ipdb\\|exp\\|tlh\\|tli\\|cache\\|sbr\\)$" nil t)
(replace-match "")))
;*.dll;*.exe;*.exp;*.iobj;*.ipdb;*.obj;*.pch;*.pdb;*.tlog;Entries;Tag;Entries.Log;CVS
(custom-set-faces
;; custom-set-faces was added by Custom.
;; If you edit it by hand, you could mess it up, so be careful.
;; Your init file should contain only one such instance.
;; If there is more than one, they won't work right.
'(default ((t (:family "Courier New" :foundry "outline" :slant normal :weight normal :height 120 :width normal)))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment