You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2171 lines
71 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Code:
  3. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  4. ;; SETUP_LOAD: (and (file-readable-p file)
  5. ;; SETUP_LOAD: (byte-recompile-file file nil 0 t)))
  6. (setq debug-on-error t)
  7. ;; make directories
  8. (unless (file-directory-p (expand-file-name user-emacs-directory))
  9. (make-directory (expand-file-name user-emacs-directory)))
  10. (require 'cl-lib)
  11. (require 'simple)
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13. ;; Some macros for internals
  14. ;; TODO: Rename to eval-after-init
  15. (defmacro call-after-init (&rest body)
  16. "If `after-init-hook' has been run, run BODY immediately.
  17. Otherwize hook it."
  18. (declare (indent 0) (debug t))
  19. `(if after-init-time
  20. ;; Currently after-init-hook is run just after setting after-init-hook
  21. (progn
  22. ,@body)
  23. (add-hook 'after-init-hook
  24. (lambda ()
  25. ,@body))))
  26. ;; TODO: Remove: Just checking fboundp should be enough for most cases
  27. (defmacro safe-require-or-eval (feature)
  28. "Require FEATURE if available.
  29. At compile time the feature will be loaded immediately."
  30. `(eval-and-compile
  31. (message "safe-require-or-eval: Trying to require %s" ,feature)
  32. (require ,feature nil t)))
  33. ;; TODO: Remove: Defining autoload is no more needed for most cases
  34. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  35. "Define autoloading FEATURE that defines FUNCTIONS.
  36. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  37. the function same as FEATURE is defined as autoloaded function. BODY is passed
  38. to `eval-after-load'.
  39. After this macro is expanded, this returns the path to library if FEATURE
  40. found, otherwise returns nil."
  41. (declare (indent 2) (debug t))
  42. (let* ((libname (symbol-name (eval feature)))
  43. (libpath (locate-library libname)))
  44. `(progn
  45. (when (locate-library ,libname)
  46. ,@(mapcar (lambda (f)
  47. `(unless (fboundp ',f)
  48. (progn
  49. (message "Autoloaded function `%S' defined (%s)"
  50. (quote ,f)
  51. ,libpath)
  52. (autoload (quote ,f)
  53. ,libname
  54. ,(concat "Autoloaded function defined in \""
  55. libpath
  56. "\".")
  57. t))))
  58. (or (eval functions)
  59. `(,(eval feature)))))
  60. (eval-after-load ,feature
  61. (quote (progn
  62. ,@body)))
  63. (locate-library ,libname))))
  64. (when (autoload-eval-lazily 'tetris nil
  65. (message "Tetris loaded!"))
  66. (message "Tetris found!"))
  67. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  68. ;; package
  69. (set (defvar 10sr-package-list)
  70. '(
  71. vimrc-mode
  72. markdown-mode
  73. yaml-mode
  74. gnuplot-mode
  75. php-mode
  76. erlang
  77. js2-mode
  78. js-doc
  79. git-commit
  80. gitignore-mode
  81. adoc-mode
  82. go-mode
  83. ;; It seems malabar has been merged into jdee and this package
  84. ;; already removed
  85. ;; malabar-mode
  86. gosh-mode
  87. scala-mode
  88. ;;ensime
  89. web-mode
  90. color-moccur
  91. ggtags
  92. flycheck
  93. auto-highlight-symbol
  94. hl-todo
  95. ;; Currently not available
  96. ;; pp-c-l
  97. xclip
  98. foreign-regexp
  99. multi-term
  100. term-run
  101. editorconfig
  102. git-ps1-mode
  103. restart-emacs
  104. fill-column-indicator
  105. pkgbuild-mode
  106. minibuffer-line
  107. which-key
  108. ;; I think this works in place of my autosave lib
  109. super-save
  110. pipenv
  111. imenu-list
  112. page-break-lines
  113. aggressive-indent
  114. dired-filter
  115. wgrep
  116. magit
  117. git-gutter
  118. end-mark
  119. sl
  120. ;; TODO: Configure pony-tpl-mode
  121. pony-mode
  122. gited
  123. highlight-indentation
  124. diminish
  125. fzf
  126. fic-mode
  127. term-cursor
  128. editorconfig
  129. editorconfig-custom-majormode
  130. git-command
  131. prompt-text
  132. ;; 10sr repository
  133. ;; 10sr-extras
  134. terminal-title
  135. dired-list-all-mode
  136. pack
  137. set-modeline-color
  138. read-only-only-mode
  139. smart-revert
  140. autosave
  141. ;;window-organizer
  142. ilookup
  143. pasteboard
  144. awk-preview
  145. recently
  146. ))
  147. (when (safe-require-or-eval 'package)
  148. (setq package-archives
  149. `(,@package-archives
  150. ("melpa" . "https://melpa.org/packages/")
  151. ;; Somehow fails to download via https
  152. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  153. (package-initialize)
  154. (defun my-auto-install-package ()
  155. "Install packages semi-automatically."
  156. (interactive)
  157. (package-refresh-contents)
  158. (mapc (lambda (pkg)
  159. (or (package-installed-p pkg)
  160. (package-install pkg)))
  161. 10sr-package-list))
  162. )
  163. ;; (lazy-load-eval 'sudoku)
  164. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  165. ;; my-idle-hook
  166. (defvar my-idle-hook nil
  167. "Hook run when idle for several secs.")
  168. (defvar my-idle-hook-sec 5
  169. "Second to run `my-idle-hook'.")
  170. (run-with-idle-timer my-idle-hook-sec
  171. t
  172. (lambda ()
  173. (run-hooks 'my-idle-hook)))
  174. ;; (add-hook 'my-idle-hook
  175. ;; (lambda ()
  176. ;; (message "idle hook message")))
  177. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  178. ;; start and quit
  179. (setq inhibit-startup-message t)
  180. (setq initial-buffer-choice 'messages-buffer)
  181. (setq confirm-kill-emacs 'y-or-n-p)
  182. (setq gc-cons-threshold (* 1024 1024 16))
  183. (setq garbage-collection-messages nil)
  184. (when window-system
  185. (add-to-list 'default-frame-alist '(cursor-type . box))
  186. (add-to-list 'default-frame-alist '(background-color . "white"))
  187. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  188. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  189. ;; does not work?
  190. )
  191. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  192. (menu-bar-mode 1)
  193. (define-key ctl-x-map "m" 'menu-bar-open)
  194. (and (fboundp 'tool-bar-mode)
  195. (tool-bar-mode 0))
  196. (and (fboundp 'set-scroll-bar-mode)
  197. (set-scroll-bar-mode nil))
  198. (call-after-init
  199. (message "%s %s" invocation-name emacs-version)
  200. (message "Invocation directory: %s" default-directory)
  201. (message "%s was taken to initialize emacs." (emacs-init-time))
  202. ;; (view-echo-area-messages)
  203. ;; (view-emacs-news)
  204. )
  205. (with-current-buffer "*Messages*"
  206. (emacs-lock-mode 'kill))
  207. (cd ".") ; when using windows use / instead of \ in `default-directory'
  208. ;; locale
  209. (set-language-environment "Japanese")
  210. (set-default-coding-systems 'utf-8-unix)
  211. (prefer-coding-system 'utf-8-unix)
  212. (setq system-time-locale "C")
  213. ;; my prefix map
  214. (defvar my-prefix-map nil
  215. "My prefix map.")
  216. (define-prefix-command 'my-prefix-map)
  217. (global-set-key (kbd "C-^") 'my-prefix-map)
  218. ;; (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  219. ;; (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  220. ;; (comint-show-maximum-output)
  221. ;; kill scratch
  222. (call-after-init
  223. (let ((buf (get-buffer "*scratch*")))
  224. (when buf
  225. (kill-buffer buf))))
  226. ;; modifier keys
  227. ;; (setq mac-option-modifier 'control)
  228. ;; display
  229. (setq visible-bell t)
  230. (setq ring-bell-function 'ignore)
  231. (mouse-avoidance-mode 'banish)
  232. (setq echo-keystrokes 0.1)
  233. (defun reload-init-file ()
  234. "Reload Emacs init file."
  235. (interactive)
  236. (when (and user-init-file
  237. (file-readable-p user-init-file))
  238. (load-file user-init-file)))
  239. (safe-require-or-eval 'session)
  240. ;; server
  241. (set-variable 'server-name (concat "server"
  242. (number-to-string (emacs-pid))))
  243. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  244. ;; In Darwin environment, init fails with message like 'Service name too long'
  245. ;; when server-use-tcp is nil
  246. (when (or (eq system-type
  247. 'cygwin)
  248. (eq system-type
  249. 'darwin))
  250. (set-variable 'server-use-tcp t))
  251. ;; MSYS2 fix
  252. (when (eq system-type
  253. 'windows-nt)
  254. (setq shell-file-name
  255. (executable-find "bash"))
  256. '(setq function-key-map
  257. `(,@function-key-map ([pause] . [?\C-c])
  258. ))
  259. (define-key key-translation-map
  260. (kbd "<pause>")
  261. (kbd "C-c"))
  262. '(keyboard-translate [pause]
  263. (kbd "C-c")p)
  264. ;; TODO: move to other place later
  265. (when (not window-system)
  266. (setq interprogram-paste-function nil)
  267. (setq interprogram-cut-function nil)))
  268. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  269. ;; global keys
  270. (global-set-key (kbd "<up>") 'scroll-down-line)
  271. (global-set-key (kbd "<down>") 'scroll-up-line)
  272. (global-set-key (kbd "<left>") 'scroll-down)
  273. (global-set-key (kbd "<right>") 'scroll-up)
  274. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  275. (global-set-key (kbd "C-\\") help-map)
  276. (define-key ctl-x-map (kbd "DEL") help-map)
  277. (define-key ctl-x-map (kbd "C-h") help-map)
  278. (define-key help-map "a" 'apropos)
  279. ;; disable annoying keys
  280. (global-set-key [prior] 'ignore)
  281. (global-set-key (kbd "<next>") 'ignore)
  282. (global-set-key [menu] 'ignore)
  283. (global-set-key [down-mouse-1] 'ignore)
  284. (global-set-key [down-mouse-2] 'ignore)
  285. (global-set-key [down-mouse-3] 'ignore)
  286. (global-set-key [mouse-1] 'ignore)
  287. (global-set-key [mouse-2] 'ignore)
  288. (global-set-key [mouse-3] 'ignore)
  289. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  290. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  291. ;; Interactively evaluate Emacs Lisp expressions
  292. (define-key ctl-x-map "i" 'ielm)
  293. (when (safe-require-or-eval 'which-key)
  294. (which-key-mode))
  295. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  296. ;; editor
  297. ;; Used from term-cursor
  298. (defun my-cursor-type-change (&rest args)
  299. "ARGS are discarded."
  300. ;; TODO: Support wdired
  301. (if buffer-read-only
  302. (setq cursor-type 'bar)
  303. (setq cursor-type 'box)))
  304. (add-hook 'switch-buffer-functionsp
  305. 'my-cursor-type-change)
  306. (add-hook 'read-only-mode-hook
  307. 'my-cursor-type-change)
  308. (when (fboundp 'global-term-cursor-mode)
  309. (global-term-cursor-mode 1))
  310. ;; (term-cursor--eval)
  311. (setq kill-whole-line t)
  312. (setq scroll-conservatively 35
  313. scroll-margin 2)
  314. (setq-default major-mode 'text-mode)
  315. (setq next-line-add-newlines nil)
  316. (setq kill-read-only-ok t)
  317. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  318. ;; (setq-default line-spacing 0.2)
  319. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  320. ;; (setq-default tab-width 4)
  321. (setq-default indent-tabs-mode nil)
  322. (setq-default indent-line-function 'indent-to-left-margin)
  323. ;; (setq-default indent-line-function nil)
  324. (setq-default truncate-lines nil)
  325. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  326. (delete-selection-mode 1)
  327. (cua-mode 0)
  328. (setq line-move-visual nil)
  329. (setq create-lockfiles nil)
  330. (add-hook 'before-save-hook
  331. 'time-stamp)
  332. ;; Add Time-stamp: <> to insert timestamp there
  333. (set-variable 'time-stamp-format
  334. "%:y-%02m-%02d %02H:%02M:%02S %Z 10sr")
  335. ;; key bindings
  336. ;; moving around
  337. ;;(keyboard-translate ?\M-j ?\C-j)
  338. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  339. (define-key esc-map "p" 'backward-paragraph)
  340. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  341. (define-key esc-map "n" 'forward-paragraph)
  342. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  343. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  344. (global-set-key (kbd "C-<left>") 'scroll-down)
  345. (global-set-key (kbd "C-<right>") 'scroll-up)
  346. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  347. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  348. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  349. ;; C-h and DEL
  350. (global-set-key (kbd "C-h") (kbd "DEL"))
  351. ;; (normal-erase-is-backspace-mode 1)
  352. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  353. (global-set-key (kbd "C-m") 'newline-and-indent)
  354. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  355. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  356. (define-key esc-map "u" 'undo)
  357. (define-key esc-map "i" (kbd "ESC TAB"))
  358. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  359. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  360. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  361. (require 'page-ext nil t)
  362. (when (safe-require-or-eval 'page-break-lines)
  363. (global-page-break-lines-mode 1))
  364. (when (safe-require-or-eval 'git-gutter)
  365. (declare-function global-git-gutter-mode "git-gutter")
  366. (custom-set-variables
  367. '(git-gutter:lighter " Gttr"))
  368. (custom-set-variables
  369. '(git-gutter:update-interval 2))
  370. (custom-set-variables
  371. '(git-gutter:unchanged-sign " "))
  372. (when (>= (display-color-cells)
  373. 256)
  374. (let ((c "color-233"))
  375. (set-face-background 'git-gutter:modified c)
  376. (set-face-background 'git-gutter:added c)
  377. (set-face-background 'git-gutter:deleted c)
  378. (set-face-background 'git-gutter:unchanged c)))
  379. (global-git-gutter-mode 1)
  380. )
  381. ;; (when (safe-require-or-eval 'fancy-narrow)
  382. ;; (fancy-narrow-mode 1))
  383. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  384. ;; title and mode-line
  385. (when (safe-require-or-eval 'terminal-title)
  386. ;; if TERM is not screen use default value
  387. (if (getenv "TMUX")
  388. ;; if use tmux locally just basename of current dir
  389. (set-variable 'terminal-title-format
  390. '((file-name-nondirectory (directory-file-name
  391. default-directory))))
  392. (if (and (let ((tty-type (frame-parameter nil
  393. 'tty-type)))
  394. (and tty-type
  395. (equal (car (split-string tty-type
  396. "-"))
  397. "screen")))
  398. (not (getenv "SSH_CONNECTION")))
  399. (set-variable 'terminal-title-format
  400. '((file-name-nondirectory (directory-file-name
  401. default-directory))))
  402. ;; seems that TMUX is used locally and ssh to remote host
  403. (set-variable 'terminal-title-format
  404. `("em:"
  405. ,user-login-name
  406. "@"
  407. ,(car (split-string (system-name)
  408. "\\."))
  409. ":"
  410. default-directory))
  411. )
  412. )
  413. (terminal-title-mode))
  414. (setq eol-mnemonic-dos "\\r\\n")
  415. (setq eol-mnemonic-mac "\\r")
  416. (setq eol-mnemonic-unix "")
  417. (which-function-mode 1)
  418. (line-number-mode 0)
  419. (column-number-mode 0)
  420. (size-indication-mode 0)
  421. (setq mode-line-position
  422. '(:eval (format ":%%l:%%c<%d%s>"
  423. (count-lines (point-max)
  424. (point-min))
  425. (if (buffer-narrowed-p)
  426. "[N]"
  427. "")
  428. )))
  429. (when (safe-require-or-eval 'diminish)
  430. ;; FIXME: Eval after enabling mode
  431. (call-after-init
  432. (diminish 'recently-mode)
  433. (diminish 'editorconfig-mode)
  434. (diminish 'auto-highlight-symbol-mode)
  435. (diminish 'global-whitespace-mode)
  436. (diminish 'which-key-mode)
  437. (diminish 'page-break-lines-mode)
  438. (diminish 'highlight-indentation-mode)))
  439. (setq mode-line-front-space "")
  440. ;; (setq mode-line-end-spaces "")
  441. ;; Set current frame name to empty string
  442. (setq-default mode-line-format
  443. (let* ((l mode-line-format)
  444. (l (cl-substitute " " " "
  445. l
  446. :test 'equal))
  447. (l (cl-substitute " " " "
  448. l
  449. :test 'equal))
  450. )
  451. l))
  452. (set-frame-parameter nil 'name "")
  453. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  454. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  455. ;; minibuffer
  456. (setq insert-default-directory t)
  457. (setq completion-ignore-case t
  458. read-file-name-completion-ignore-case t
  459. read-buffer-completion-ignore-case t)
  460. (setq resize-mini-windows t)
  461. (temp-buffer-resize-mode 1)
  462. (savehist-mode 1)
  463. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  464. (set-variable 'help-at-pt-display-when-idle t)
  465. (fset 'yes-or-no-p 'y-or-n-p)
  466. ;; complete symbol when `eval'
  467. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  468. (define-key minibuffer-local-map (kbd "C-u")
  469. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  470. ;; I dont know these bindings are good
  471. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  472. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  473. (when (safe-require-or-eval 'minibuffer-line)
  474. (set-face-underline 'minibuffer-line nil)
  475. (set-variable 'minibuffer-line-refresh-interval
  476. 25)
  477. ;; Set idle timer
  478. (defvar my-minibuffer-line--idle-timer nil)
  479. (defvar minibuffer-line-mode)
  480. (add-hook 'minibuffer-line-mode-hook
  481. (lambda ()
  482. (when my-minibuffer-line--idle-timer
  483. (cancel-timer my-minibuffer-line--idle-timer)
  484. (setq my-minibuffer-line--idle-timer nil))
  485. (when minibuffer-line-mode
  486. (setq my-minibuffer-line--idle-timer
  487. (run-with-idle-timer 0.5
  488. t
  489. 'minibuffer-line--update)))))
  490. (set-variable 'minibuffer-line-format
  491. `(,(concat user-login-name
  492. "@"
  493. (car (split-string (system-name)
  494. "\\."))
  495. ":")
  496. (:eval (abbreviate-file-name (or buffer-file-name
  497. default-directory)))
  498. (:eval (and (fboundp 'git-ps1-mode-get-current)
  499. (git-ps1-mode-get-current " [GIT:%s]")))
  500. " "
  501. (:eval (format-time-string display-time-format))))
  502. (minibuffer-line-mode 1)
  503. )
  504. (when (safe-require-or-eval 'prompt-text)
  505. (set-variable 'prompt-text-format
  506. `(,(concat ""
  507. user-login-name
  508. "@"
  509. (car (split-string (system-name)
  510. "\\."))
  511. ":")
  512. (:eval (abbreviate-file-name (or buffer-file-name
  513. default-directory)))
  514. (:eval (and (fboundp 'git-ps1-mode-get-current)
  515. (git-ps1-mode-get-current " [GIT:%s]")))
  516. " "
  517. (:eval (format-time-string display-time-format))
  518. "\n"
  519. (:eval (symbol-name this-command))
  520. ": "))
  521. (prompt-text-mode 1))
  522. (autoload-eval-lazily 'helm nil
  523. (defvar helm-map)
  524. (define-key helm-map (kbd "C-h") (kbd "DEL")))
  525. (setq-default header-line-format
  526. '(:eval (let ((f (or (buffer-file-name)
  527. default-directory)))
  528. (when f
  529. (abbreviate-file-name f)))))
  530. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  531. ;; letters, font-lock mode and fonts
  532. (setq text-quoting-style 'grave)
  533. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  534. ;; (set-window-margins (selected-window) 1 1)
  535. (unless window-system
  536. (setq frame-background-mode 'dark))
  537. (and (or (eq system-type 'Darwin)
  538. (eq system-type 'darwin))
  539. (fboundp 'mac-set-input-method-parameter)
  540. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  541. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  542. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  543. (boundp 'input-method-inactivate-hook))
  544. (add-hook 'input-method-activate-hook
  545. (lambda () (set-cursor-color "red")))
  546. (add-hook 'input-method-inactivate-hook
  547. (lambda () (set-cursor-color "black"))))
  548. (when (safe-require-or-eval 'paren)
  549. (show-paren-mode 1)
  550. (setq show-paren-delay 0.5
  551. show-paren-style 'parenthesis) ; mixed is hard to read
  552. ;; (set-face-background 'show-paren-match
  553. ;; "black")
  554. ;; ;; (face-foreground 'default))
  555. ;; (set-face-foreground 'show-paren-match
  556. ;; "white")
  557. ;; (set-face-inverse-video-p 'show-paren-match
  558. ;; t)
  559. )
  560. (transient-mark-mode 1)
  561. (global-font-lock-mode 1)
  562. (setq font-lock-global-modes
  563. '(not
  564. help-mode
  565. eshell-mode
  566. ;;term-mode
  567. Man-mode
  568. magit-diff-mode
  569. magit-revision-mode))
  570. ;; (standard-display-ascii ?\n "$\n")
  571. ;; (defvar my-eol-face
  572. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  573. ;; )
  574. ;; (defvar my-tab-face
  575. ;; '(("\t" . '(0 highlight t nil))))
  576. (defvar my-jspace-face
  577. '(("\u3000" . '(0 highlight t nil))))
  578. (add-hook 'font-lock-mode-hook
  579. (lambda ()
  580. ;; (font-lock-add-keywords nil my-eol-face)
  581. (font-lock-add-keywords nil my-jspace-face)
  582. ))
  583. (when (safe-require-or-eval 'whitespace)
  584. (add-to-list 'whitespace-display-mappings
  585. ;; We need t since last one takes precedence
  586. `(tab-mark ?\t ,(vconcat "|>\t")) t)
  587. ;; (add-to-list 'whitespace-display-mappings
  588. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  589. (setq whitespace-style '(face
  590. trailing ; trailing blanks
  591. ;; tabs
  592. ;; spaces
  593. ;; lines
  594. lines-tail ; lines over 80
  595. newline ; newlines
  596. ;; empty ; empty lines at beg or end of buffer
  597. ;; big-indent
  598. ;; space-mark
  599. tab-mark
  600. newline-mark ; use display table for newline
  601. ))
  602. ;; (setq whitespace-newline 'font-lock-comment-face)
  603. ;; (setq whitespace-style (delq 'newline-mark whitespace-style))
  604. (defun my-whitesspace-mode-reload ()
  605. "Reload whitespace-mode config."
  606. (interactive)
  607. (when whitespace-mode
  608. (whitespace-mode 0)
  609. (whitespace-mode 1)))
  610. (set-variable 'whitespace-line-column nil)
  611. (global-whitespace-mode t)
  612. (add-hook 'dired-mode-hook
  613. (lambda ()
  614. (set (make-local-variable 'whitespace-style) nil)))
  615. (if (>= (display-color-cells)
  616. 256)
  617. (set-face-foreground 'whitespace-newline "color-109")
  618. ;; (progn
  619. ;; (set-face-bold-p 'whitespace-newline
  620. ;; t))
  621. ))
  622. (and nil
  623. '(safe-require-or-eval 'fill-column-indicator)
  624. (setq fill-column-indicator))
  625. ;; highlight current line
  626. ;; http://wiki.riywo.com/index.php?Meadow
  627. (face-spec-set 'hl-line
  628. '((((min-colors 256)
  629. (background dark))
  630. (:background "color-234"))
  631. (((min-colors 256)
  632. (background light))
  633. (:background "color-234"))
  634. (t
  635. (:underline "black"))))
  636. (set-variable 'hl-line-global-modes
  637. '(not
  638. term-mode))
  639. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  640. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  641. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  642. ;;(safe-require-or-eval 'set-modeline-color)
  643. ;; (let ((fg (face-foreground 'default))
  644. ;; (bg (face-background 'default)))
  645. ;; (set-face-background 'mode-line-inactive
  646. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  647. ;; (set-face-foreground 'mode-line-inactive
  648. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  649. ;; (set-face-underline 'mode-line-inactive
  650. ;; t)
  651. ;; (set-face-underline 'vertical-border
  652. ;; nil)
  653. ;; (when (safe-require-or-eval 'end-mark)
  654. ;; (global-end-mark-mode))
  655. ;; M-x highlight-* to highlight things
  656. (global-hi-lock-mode 1)
  657. (unless (fboundp 'highlight-region-text)
  658. (defun highlight-region-text (beg end)
  659. "Highlight text between BEG and END."
  660. (interactive "r")
  661. (highlight-regexp (regexp-quote (buffer-substring-no-properties beg
  662. end)))
  663. (setq deactivate-mark t)))
  664. (when (safe-require-or-eval 'auto-highlight-symbol)
  665. (set-variable 'ahs-idle-interval 0.6)
  666. (global-auto-highlight-symbol-mode 1))
  667. (when (safe-require-or-eval 'highlight-indentation)
  668. (set-face-background 'highlight-indentation-face "color-236")
  669. (dolist (hook
  670. '(
  671. prog-mode-hook
  672. text-mode-hook
  673. ))
  674. (add-hook hook
  675. 'highlight-indentation-mode)))
  676. ;; (set-face-background 'highlight-indentation-current-column-face "#c3b3b3")
  677. (when (fboundp 'fic-mode)
  678. (add-hook 'prog-mode-hook
  679. 'fic-mode))
  680. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  681. ;; file handling
  682. (auto-insert-mode 1)
  683. ;; fzf
  684. ;; Too slow in term buffer!
  685. ;; (set-variable 'fzf/executable "sk")
  686. ;; (set-variable 'fzf/args "--color bw --print-query")
  687. ;; Modified from hardcoded default to include:
  688. ;; - directories
  689. ;; - hidden files
  690. ;; - root directory (.)
  691. ;; - parent directory (..)
  692. ;; ripgrep cannot list directories...
  693. ;; (setenv "FZF_DEFAULT_COMMAND" "rg --files --hidden --follow --glob '!.git/*' --no-ignore")
  694. (let* ((find (if (executable-find "bfs")
  695. ;; Breadth-first find https://github.com/tavianator/bfs
  696. "bfs"
  697. ;; Use gfind if available?
  698. "find"))
  699. (findcmd (concat "set -eu; set -o pipefail; "
  700. "echo .; "
  701. "echo ..; "
  702. "command " find " -L . "
  703. "-mindepth 1 "
  704. "\\( -fstype 'sysfs' -o -fstype 'devfs' -o -fstype 'devtmpfs' -o -fstype 'proc' \\) -prune "
  705. "-o -print "
  706. "2> /dev/null "
  707. "| "
  708. "cut -b3-"))
  709. (fdcmd (concat "set -eu; set -o pipefail; "
  710. "echo .; "
  711. "echo ..; "
  712. "command fd "
  713. "--follow --hidden --no-ignore "
  714. "--color always "
  715. "2>/dev/null")))
  716. (if (executable-find "fd")
  717. (setenv "FZF_DEFAULT_COMMAND" fdcmd)
  718. (setenv "FZF_DEFAULT_COMMAND" findcmd)))
  719. (set-variable 'fzf/window-height 45)
  720. (set-variable 'fzf/args "--print-query --ansi --color='bg+:-1' --inline-info --cycle")
  721. ;; (set-variable 'fzf/args "--print-query --ansi --inline-info --cycle")
  722. ;; (set-variable 'fzf/args "--print-query --ansi --color=bw --inline-info --cycle")
  723. (defun my-fzf-or-find-file ()
  724. "Call fzf if usable or call find-file."
  725. (declare (interactive-only t))
  726. (interactive)
  727. (if (and (executable-find "fzf")
  728. (fboundp 'fzf)
  729. (not (file-remote-p default-directory)))
  730. (fzf)
  731. (call-interactively 'find-file)))
  732. (define-key ctl-x-map "f" 'my-fzf-or-find-file)
  733. (defun my-fzf-all-lines ()
  734. "Fzf all lines."
  735. (interactive)
  736. (let ((process-environment (cl-copy-list process-environment)))
  737. (setenv "FZF_DEFAULT_COMMAND" "rg -nH --no-heading --hidden --follow --glob '!.git/*' --color=always ^")
  738. (fzf)))
  739. (define-key ctl-x-map "S" 'my-fzf-all-lines)
  740. ;; recently
  741. (when (safe-require-or-eval 'recently)
  742. (define-key ctl-x-map (kbd "C-r") 'recently-show)
  743. (set-variable 'recently-max 1000)
  744. (recently-mode 1))
  745. (when (safe-require-or-eval 'editorconfig)
  746. (set-variable 'editorconfig-get-properties-function
  747. 'editorconfig-core-get-properties-hash)
  748. (editorconfig-mode 1)
  749. (set-variable 'editorconfig-mode-lighter "")
  750. (when (fboundp 'ws-butler-mode)
  751. (set-variable 'editorconfig-trim-whitespaces-mode
  752. 'ws-butler-mode))
  753. (with-eval-after-load 'org-src
  754. ;; [*.org\[\*Org Src*\[ c \]*\]]
  755. (add-hook 'org-src-mode-hook
  756. 'editorconfig-mode-apply t)))
  757. (when (fboundp 'editorconfig-custom-majormode)
  758. (add-hook 'editorconfig-after-apply-functions
  759. 'editorconfig-custom-majormode))
  760. ;; Add readonly=true to set read-only-mode
  761. (add-hook 'editorconfig-after-apply-functions
  762. (lambda (props)
  763. (let ((r (gethash 'readonly props)))
  764. (when (and (string= r "true")
  765. (not buffer-read-only))
  766. (read-only-mode 1)))))
  767. (add-hook 'editorconfig-hack-properties-functions
  768. '(lambda (props)
  769. (when (derived-mode-p 'makefile-mode)
  770. (puthash 'indent_style "tab" props))))
  771. (when (fboundp 'editorconfig-auto-apply-enable)
  772. (add-hook 'editorconfig-conf-mode-hook
  773. 'editorconfig-auto-apply-enable))
  774. ;; (when (fboundp 'editorconfig-charset-extras)
  775. ;; (add-hook 'editorconfig-custom-hooks
  776. ;; 'editorconfig-charset-extras))
  777. (setq revert-without-query '(".+"))
  778. ;; save cursor position
  779. (when (safe-require-or-eval 'saveplace)
  780. (setq-default save-place t)
  781. (setq save-place-file (concat user-emacs-directory
  782. "places")))
  783. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  784. (setq make-backup-files t)
  785. (setq vc-make-backup-files t)
  786. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  787. (setq backup-directory-alist
  788. (cons (cons "." (expand-file-name (concat user-emacs-directory
  789. "backup")))
  790. backup-directory-alist))
  791. (setq version-control 't)
  792. (setq delete-old-versions t)
  793. (setq kept-new-versions 20)
  794. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  795. "auto-save/")))
  796. ;; (setq delete-auto-save-files t)
  797. (setq auto-save-visited-interval 8)
  798. (auto-save-visited-mode 1)
  799. ;; (add-to-list 'auto-save-file-name-transforms
  800. ;; `(".*" ,(concat user-emacs-directory "auto-save-dir") t))
  801. ;; (setq auto-save-interval 3)
  802. ;; (auto-save-mode 1)
  803. (add-to-list 'completion-ignored-extensions ".bak")
  804. (set-variable 'completion-cycle-threshold nil) ;; NEVER use
  805. (setq delete-by-moving-to-trash t)
  806. ;; trash-directory "~/.emacs.d/trash")
  807. (add-hook 'after-save-hook
  808. 'executable-make-buffer-file-executable-if-script-p)
  809. (set-variable 'bookmark-default-file
  810. (expand-file-name (concat user-emacs-directory
  811. "bmk")))
  812. (set-variable 'bookmark-save-flag
  813. 1)
  814. (with-eval-after-load 'recentf
  815. (defvar recentf-exclude)
  816. (defvar bookmark-default-file)
  817. (add-to-list 'recentf-exclude
  818. (regexp-quote bookmark-default-file)))
  819. (when (safe-require-or-eval 'smart-revert)
  820. (smart-revert-on))
  821. ;; autosave
  822. ;; auto-save-visited-mode can be used instead?
  823. ;; (when (safe-require-or-eval 'autosave)
  824. ;; (autosave-set 8))
  825. ;; bookmarks
  826. ;; (define-key ctl-x-map "m" 'list-bookmarks)
  827. ;; vc
  828. (set-variable 'vc-handled-backends '(RCS))
  829. (set-variable 'vc-rcs-register-switches "-l")
  830. (set-variable 'vc-rcs-checkin-switches "-l")
  831. (set-variable 'vc-command-messages t)
  832. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  833. ;; share clipboard with x
  834. ;; this page describes this in details, but only these sexps seem to be needed
  835. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  836. (and nil
  837. (not window-system)
  838. (not (eq window-system 'mac))
  839. (getenv "DISPLAY")
  840. (not (equal (getenv "DISPLAY") ""))
  841. (executable-find "xclip")
  842. ;; (< emacs-major-version 24)
  843. '(safe-require-or-eval 'xclip)
  844. nil
  845. (turn-on-xclip))
  846. (and (eq system-type 'darwin)
  847. (safe-require-or-eval 'pasteboard)
  848. (turn-on-pasteboard))
  849. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  850. ;; some modes and hooks
  851. ;; Include some extra modes
  852. (require 'generic-x)
  853. (when (fboundp 'web-mode)
  854. (add-to-list 'auto-mode-alist
  855. '("\\.html\\.j2\\'" . web-mode))
  856. )
  857. (when (autoload-eval-lazily 'wgrep)
  858. (set-variable 'wgrep-auto-save-buffer t)
  859. (with-eval-after-load 'grep
  860. (defvar grep-mode-map)
  861. (define-key grep-mode-map
  862. "e"
  863. 'wgrep-change-to-wgrep-mode)))
  864. (when (fboundp 'grep-context-mode)
  865. (add-hook 'compilation-mode-hook #'grep-context-mode))
  866. (with-eval-after-load 'remember
  867. (defvar remember-mode-map (make-sparse-keymap))
  868. (define-key remember-mode-map (kbd "C-x C-s") 'ignore))
  869. (with-eval-after-load 'magit-files
  870. ;; `global-magit-file-mode' is enabled by default and this mode overwrites
  871. ;; existing keybindings.
  872. ;; Apparently it is a HARMFUL behavior and it is really awful that I have
  873. ;; to disable thie mode here, but do anyway.
  874. ;; See also https://github.com/magit/magit/issues/3517
  875. (global-magit-file-mode -1))
  876. (with-eval-after-load 'magit-section
  877. (set-face-background 'magit-section-highlight
  878. nil))
  879. (with-eval-after-load 'magit-diff
  880. (set-face-background 'magit-diff-added-highlight
  881. nil)
  882. (set-face-background 'magit-diff-removed-highlight
  883. nil)
  884. (set-face-background 'magit-diff-context-highlight
  885. nil)
  886. )
  887. (when (boundp 'git-rebase-filename-regexp)
  888. (add-to-list 'auto-mode-alist
  889. `(,git-rebase-filename-regexp . text-mode)))
  890. (when (safe-require-or-eval 'aggressive-indent)
  891. (defvar aggressive-indent-excluded-modes)
  892. (setq aggressive-indent-excluded-modes
  893. `(diff-mode
  894. toml-mode
  895. conf-mode
  896. dockerfile-mode
  897. groovy-mode
  898. scala-mode
  899. ,@aggressive-indent-excluded-modes))
  900. (global-aggressive-indent-mode 1))
  901. (when (autoload-eval-lazily 'ggtags)
  902. (add-hook 'c-mode-common-hook
  903. (lambda ()
  904. (when (derived-mode-p 'c-mode 'c++-mode 'java-mode)
  905. (ggtags-mode 1))))
  906. (add-hook 'python-mode-hook
  907. (lambda ()
  908. (ggtags-mode 1))))
  909. (when (autoload-eval-lazily 'imenu-list)
  910. ;; (set-variable 'imenu-list-auto-resize t)
  911. (set-variable 'imenu-list-focus-after-activation t)
  912. (define-key ctl-x-map "l" 'imenu-list-smart-toggle))
  913. (add-hook 'emacs-lisp-mode-hook
  914. (lambda ()
  915. (setq imenu-generic-expression
  916. `(("Sections" ";;;\+\n;; \\(.*\\)\n" 1)
  917. ,@imenu-generic-expression))))
  918. ;; TODO: Try paraedit http://daregada.blogspot.com/2012/03/paredit.html
  919. (with-eval-after-load 'compile
  920. (defvar compilation-filter-start)
  921. (defvar compilation-error-regexp-alist)
  922. (require 'ansi-color)
  923. (add-hook 'compilation-filter-hook
  924. (lambda ()
  925. (let ((inhibit-read-only t))
  926. (ansi-color-apply-on-region compilation-filter-start
  927. (point)))))
  928. (add-to-list 'compilation-error-regexp-alist
  929. ;; ansible-lint
  930. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  931. ;; Workaround to avoid ensime error
  932. (defvar ensime-mode-key-prefix nil)
  933. (when (safe-require-or-eval 'company)
  934. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  935. ;; https://qiita.com/yuze/items/a145b1e3edb6d0c24cbf
  936. (global-company-mode)
  937. (set-variable 'company-idle-delay nil)
  938. (set-variable 'company-minimum-prefix-length 2)
  939. (set-variable 'company-selection-wrap-around t)
  940. (defvar company-mode-map)
  941. (define-key company-mode-map (kbd "C-i") 'company-indent-or-complete-common)
  942. (with-eval-after-load 'python
  943. (defvar python-indent-trigger-commands)
  944. ;; TODO: This disables completion in puthon?
  945. (add-to-list 'python-indent-trigger-commands
  946. 'company-indent-or-complete-common))
  947. (define-key ctl-x-map (kbd "C-i") 'company-complete) ; Originally `indent-rigidly'
  948. (defvar company-active-map)
  949. (define-key company-active-map (kbd "C-n") 'company-select-next)
  950. (define-key company-active-map (kbd "C-p") 'company-select-previous)
  951. (define-key company-active-map (kbd "C-s") 'company-filter-candidates)
  952. (define-key company-active-map (kbd "C-i") 'company-complete-selection)
  953. (define-key company-active-map (kbd "C-f") 'company-complete-selection)
  954. (defvar company-mode)
  955. (defvar company-candidates)
  956. (defvar company-candidates-length)
  957. ;; (popup-tip "Hello, World!")
  958. (defun my-company-lighter-current-length ()
  959. "Get current candidate length."
  960. (interactive)
  961. (let ((l nil)
  962. (inhibit-message t))
  963. (when (and company-mode
  964. (not (minibufferp))
  965. ;; Do nothing when already in company completion
  966. (not company-candidates))
  967. (ignore-errors
  968. ;; (company-auto-begin)
  969. (company-manual-begin)
  970. (setq l company-candidates-length)
  971. (company-cancel)))
  972. (if l
  973. (format "[%d]" l)
  974. "")))
  975. (defvar company-lighter)
  976. (set-variable 'company-lighter-base "Cmp")
  977. (add-to-list 'company-lighter
  978. '(:eval (my-company-lighter-current-length))
  979. t)
  980. ;; This breaks japanese text input
  981. ;; (set-variable 'my-company-length-popup-tip-timer
  982. ;; (run-with-idle-timer 0.2 t
  983. ;; 'my-company-length-popup-tip))
  984. ;; (current-active-maps)
  985. ;; (lookup-key)
  986. '(mapcar (lambda (map)
  987. (lookup-key map (kbd "C-i")))
  988. (current-active-maps))
  989. ;; https://qiita.com/syohex/items/8d21d7422f14e9b53b17
  990. (set-face-attribute 'company-tooltip nil
  991. :foreground "black" :background "lightgrey")
  992. (set-face-attribute 'company-tooltip-common nil
  993. :foreground "black" :background "lightgrey")
  994. (set-face-attribute 'company-tooltip-common-selection nil
  995. :foreground "white" :background "steelblue")
  996. (set-face-attribute 'company-tooltip-selection nil
  997. :foreground "black" :background "steelblue")
  998. (set-face-attribute 'company-preview-common nil
  999. :background nil :foreground "lightgrey" :underline t)
  1000. (set-face-attribute 'company-scrollbar-fg nil
  1001. :background "orange")
  1002. (set-face-attribute 'company-scrollbar-bg nil
  1003. :background "gray40")
  1004. )
  1005. ;; https://github.com/lunaryorn/flycheck
  1006. (when (safe-require-or-eval 'flycheck)
  1007. (call-after-init (global-flycheck-mode)))
  1008. (with-eval-after-load 'flycheck
  1009. (when (fboundp 'flycheck-black-check-setup)
  1010. (flycheck-black-check-setup)))
  1011. (set-variable 'flycheck-python-mypy-ini
  1012. ".mypy.ini")
  1013. (when (autoload-eval-lazily 'ilookup)
  1014. (define-key ctl-x-map "d" 'ilookup-open-word))
  1015. (set-variable 'ac-ignore-case nil)
  1016. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  1017. (define-key ctl-x-map "t" 'term-run-shell-command))
  1018. (add-to-list 'safe-local-variable-values
  1019. '(encoding utf-8))
  1020. (setq enable-local-variables :safe)
  1021. ;; Detect file type from shebang and set major-mode.
  1022. (add-to-list 'interpreter-mode-alist
  1023. '("python3" . python-mode))
  1024. (add-to-list 'interpreter-mode-alist
  1025. '("python2" . python-mode))
  1026. (with-eval-after-load 'python
  1027. (defvar python-mode-map (make-sparse-keymap))
  1028. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  1029. (when (autoload-eval-lazily 'pipenv)
  1030. ;; (declare-function pipenv-projectile-after-switch-default "pipenv")
  1031. ;; (add-hook 'python-mode-hook
  1032. ;; (lambda ()
  1033. ;; (pipenv-mode 1)
  1034. ;; (pipenv-projectile-after-switch-default)))
  1035. )
  1036. (set-variable 'flycheck-python-pycompile-executable "python3")
  1037. (set-variable 'python-indent-guess-indent-offset nil)
  1038. (with-eval-after-load 'blacken
  1039. (when (require 'with-venv nil t)
  1040. (with-venv-advice-add 'blacken-buffer)))
  1041. ;; http://fukuyama.co/foreign-regexp
  1042. '(and (safe-require-or-eval 'foreign-regexp)
  1043. (progn
  1044. (setq foreign-regexp/regexp-type 'perl)
  1045. '(setq reb-re-syntax 'foreign-regexp)
  1046. ))
  1047. (autoload-eval-lazily 'sql '(sql-mode)
  1048. (require 'sql-indent nil t))
  1049. (add-to-list 'auto-mode-alist
  1050. '("\\.hql\\'" . sql-mode))
  1051. (when (autoload-eval-lazily 'git-command)
  1052. (define-key ctl-x-map "g" 'git-command))
  1053. (when (autoload-eval-lazily 'gited)
  1054. (define-key ctl-x-map (kbd "C-g") 'gited-list))
  1055. (when (safe-require-or-eval 'git-commit)
  1056. (global-git-commit-mode 1))
  1057. (with-eval-after-load 'git-commit
  1058. (add-hook 'git-commit-setup-hook
  1059. 'turn-off-auto-fill t))
  1060. (autoload-eval-lazily 'sl)
  1061. (with-eval-after-load 'rst
  1062. (defvar rst-mode-map)
  1063. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  1064. (with-eval-after-load 'jdee
  1065. (add-hook 'jdee-mode-hook
  1066. (lambda ()
  1067. (make-local-variable 'global-mode-string)
  1068. (add-to-list 'global-mode-string
  1069. mode-line-position))))
  1070. ;; Cannot enable error thrown. Why???
  1071. ;; https://github.com/m0smith/malabar-mode#Installation
  1072. ;; (when (autoload-eval-lazily 'malabar-mode)
  1073. ;; (add-to-list 'load-path
  1074. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  1075. ;; (safe-require-or-eval 'cedet-devel-load)
  1076. ;; (call-after-init (activate-malabar-mode)))
  1077. (with-eval-after-load 'make-mode
  1078. (defvar makefile-mode-map (make-sparse-keymap))
  1079. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  1080. ;; this functions is set in write-file-functions, i cannot find any
  1081. ;; good way to remove this.
  1082. (fset 'makefile-warn-suspicious-lines 'ignore))
  1083. (with-eval-after-load 'verilog-mode
  1084. (defvar verilog-mode-map (make-sparse-keymap))
  1085. (define-key verilog-mode-map ";" 'self-insert-command))
  1086. (setq diff-switches "-u")
  1087. (with-eval-after-load 'diff-mode
  1088. ;; (when (and (eq major-mode
  1089. ;; 'diff-mode)
  1090. ;; (not buffer-file-name))
  1091. ;; ;; do not pass when major-mode is derived mode of diff-mode
  1092. ;; (view-mode 1))
  1093. (set-face-attribute 'diff-header nil
  1094. :foreground nil
  1095. :background nil
  1096. :weight 'bold)
  1097. (set-face-attribute 'diff-file-header nil
  1098. :foreground nil
  1099. :background nil
  1100. :weight 'bold)
  1101. (set-face-foreground 'diff-index "blue")
  1102. (set-face-attribute 'diff-hunk-header nil
  1103. :foreground "cyan"
  1104. :weight 'normal)
  1105. (set-face-attribute 'diff-context nil
  1106. ;; :foreground "white"
  1107. :foreground nil
  1108. :weight 'normal)
  1109. (set-face-foreground 'diff-removed "red")
  1110. (set-face-foreground 'diff-added "green")
  1111. (set-face-background 'diff-removed nil)
  1112. (set-face-background 'diff-added nil)
  1113. (set-face-attribute 'diff-changed nil
  1114. :foreground "magenta"
  1115. :weight 'normal)
  1116. (set-face-attribute 'diff-refine-changed nil
  1117. :foreground nil
  1118. :background nil
  1119. :weight 'bold
  1120. :inverse-video t)
  1121. ;; Annoying !
  1122. ;;(diff-auto-refine-mode)
  1123. )
  1124. ;; (ffap-bindings)
  1125. (set-variable 'browse-url-browser-function
  1126. 'eww-browse-url)
  1127. (set-variable 'sh-here-document-word "__EOC__")
  1128. (when (autoload-eval-lazily 'adoc-mode
  1129. nil
  1130. (defvar adoc-mode-map (make-sparse-keymap))
  1131. (define-key adoc-mode-map (kbd "C-m") 'newline))
  1132. (setq auto-mode-alist
  1133. `(("\\.adoc\\'" . adoc-mode)
  1134. ("\\.asciidoc\\'" . adoc-mode)
  1135. ,@auto-mode-alist)))
  1136. (with-eval-after-load 'markup-faces
  1137. ;; Is this too match ?
  1138. (set-face-foreground 'markup-meta-face
  1139. "color-245")
  1140. (set-face-foreground 'markup-meta-hide-face
  1141. "color-245")
  1142. )
  1143. ;; TODO: check if this is required
  1144. (when (autoload-eval-lazily 'groovy-mode nil
  1145. (defvar groovy-mode-map (make-sparse-keymap))
  1146. (define-key groovy-mode-map "(" 'self-insert-command)
  1147. (define-key groovy-mode-map ")" 'self-insert-command)
  1148. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  1149. )
  1150. (add-to-list 'auto-mode-alist
  1151. '("build\\.gradle\\'" . groovy-mode)))
  1152. (add-to-list 'auto-mode-alist
  1153. '("\\.gawk\\'" . awk-mode))
  1154. (with-eval-after-load 'yaml-mode
  1155. (defvar yaml-mode-map (make-sparse-keymap))
  1156. (define-key yaml-mode-map (kbd "C-m") 'newline))
  1157. (with-eval-after-load 'html-mode
  1158. (defvar html-mode-map (make-sparse-keymap))
  1159. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  1160. (with-eval-after-load 'text-mode
  1161. (define-key text-mode-map (kbd "C-m") 'newline))
  1162. (autoload-eval-lazily 'info nil
  1163. (defvar Info-additional-directory-list)
  1164. (dolist (dir (directory-files (concat user-emacs-directory
  1165. "info")
  1166. t
  1167. "^[^.].*"))
  1168. (when (file-directory-p dir)
  1169. (add-to-list 'Info-additional-directory-list
  1170. dir)))
  1171. (let ((dir (expand-file-name "~/.brew/share/info")))
  1172. (when (file-directory-p dir)
  1173. (add-to-list 'Info-additional-directory-list
  1174. dir))))
  1175. (with-eval-after-load 'apropos
  1176. (defvar apropos-mode-map (make-sparse-keymap))
  1177. (define-key apropos-mode-map "n" 'next-line)
  1178. (define-key apropos-mode-map "p" 'previous-line))
  1179. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  1180. ;; (define-key isearch-mode-map
  1181. ;; (kbd "C-j") 'isearch-other-control-char)
  1182. ;; (define-key isearch-mode-map
  1183. ;; (kbd "C-k") 'isearch-other-control-char)
  1184. ;; (define-key isearch-mode-map
  1185. ;; (kbd "C-h") 'isearch-other-control-char)
  1186. (define-key isearch-mode-map (kbd "C-h") 'isearch-del-char)
  1187. (define-key isearch-mode-map (kbd "M-r")
  1188. 'isearch-query-replace-regexp)
  1189. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1190. (setq lazy-highlight-cleanup nil)
  1191. ;; face for isearch highlighing
  1192. (set-face-attribute 'lazy-highlight
  1193. nil
  1194. :foreground `unspecified
  1195. :background `unspecified
  1196. :underline t
  1197. ;; :weight `bold
  1198. )
  1199. (add-hook 'outline-mode-hook
  1200. (lambda ()
  1201. (when (string-match "\\.md\\'" buffer-file-name)
  1202. (set (make-local-variable 'outline-regexp) "#+ "))))
  1203. (add-hook 'outline-mode-hook
  1204. 'outline-show-all)
  1205. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1206. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1207. (when (autoload-eval-lazily 'markdown-mode
  1208. '(markdown-mode gfm-mode)
  1209. (defvar gfm-mode-map (make-sparse-keymap))
  1210. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  1211. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1212. (set-variable 'markdown-command (or (executable-find "markdown")
  1213. (executable-find "markdown.pl")
  1214. ""))
  1215. (add-hook 'markdown-mode-hook
  1216. (lambda ()
  1217. (outline-minor-mode 1)
  1218. (flyspell-mode)
  1219. (set (make-local-variable 'comment-start) ";")))
  1220. )
  1221. ;; c-mode
  1222. ;; http://www.emacswiki.org/emacs/IndentingC
  1223. ;; http://en.wikipedia.org/wiki/Indent_style
  1224. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1225. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1226. (with-eval-after-load 'cc-vars
  1227. (defvar c-default-style nil)
  1228. (add-to-list 'c-default-style
  1229. '(c-mode . "k&r"))
  1230. (add-to-list 'c-default-style
  1231. '(c++-mode . "k&r")))
  1232. (autoload-eval-lazily 'js2-mode nil
  1233. ;; currently do not use js2-mode
  1234. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1235. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1236. ;; (defvar js2-mode-map (make-sparse-keymap))
  1237. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  1238. ;; (interactive)
  1239. ;; (js2-enter-key)
  1240. ;; (indent-for-tab-command)))
  1241. ;; (add-hook (kill-local-variable 'before-save-hook)
  1242. ;; 'js2-before-save)
  1243. ;; (add-hook 'before-save-hook
  1244. ;; 'my-indent-buffer
  1245. ;; nil
  1246. ;; t)
  1247. )
  1248. (add-to-list 'interpreter-mode-alist
  1249. '("node" . js-mode))
  1250. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1251. (with-eval-after-load 'uniquify
  1252. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1253. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1254. (setq uniquify-min-dir-content 1))
  1255. (with-eval-after-load 'view
  1256. (defvar view-mode-map (make-sparse-keymap))
  1257. (define-key view-mode-map "j" 'scroll-up-line)
  1258. (define-key view-mode-map "k" 'scroll-down-line)
  1259. (define-key view-mode-map "v" 'toggle-read-only)
  1260. (define-key view-mode-map "q" 'bury-buffer)
  1261. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1262. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1263. ;; (define-key view-mode-map
  1264. ;; "n" 'nonincremental-repeat-search-forward)
  1265. ;; (define-key view-mode-map
  1266. ;; "N" 'nonincremental-repeat-search-backward)
  1267. ;; N conflicts with git-walktree
  1268. ;; (define-key view-mode-map "/" 'isearch-forward-regexp)
  1269. ;; (define-key view-mode-map "?" 'isearch-backward-regexp)
  1270. ;; (define-key view-mode-map "n" 'isearch-repeat-forward)
  1271. ;; (define-key view-mode-map "N" 'isearch-repeat-backward)
  1272. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  1273. (global-set-key "\M-r" 'view-mode)
  1274. ;; (setq view-read-only t)
  1275. (with-eval-after-load 'term
  1276. (defvar term-raw-map (make-sparse-keymap))
  1277. (define-key term-raw-map (kbd "C-x")
  1278. (lookup-key (current-global-map)
  1279. (kbd "C-x"))))
  1280. (add-hook 'term-mode-hook
  1281. (lambda ()
  1282. ;; Stop current line highlighting
  1283. (set (make-local-variable (defvar hl-line-range-function))
  1284. (lambda () '(0 . 0)))
  1285. (set (make-local-variable 'scroll-margin)
  1286. 0)
  1287. (set-variable 'term-buffer-maximum-size 20480)
  1288. ))
  1289. (add-hook 'Man-mode-hook
  1290. (lambda ()
  1291. (view-mode 1)
  1292. (setq truncate-lines nil)))
  1293. (set-variable 'Man-notify-method (if window-system
  1294. 'newframe
  1295. 'aggressive))
  1296. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  1297. "woman_cache.el")))
  1298. ;; not work because man.el will be loaded when man called
  1299. (defalias 'man 'woman)
  1300. (add-to-list 'auto-mode-alist
  1301. '("tox\\.ini\\'" . conf-unix-mode))
  1302. (when (autoload-eval-lazily 'toml-mode)
  1303. (add-to-list 'auto-mode-alist
  1304. '("/tox\\.ini\\'" . toml-mode))
  1305. (add-to-list 'auto-mode-alist
  1306. '("/Pipfile\\'" . toml-mode))
  1307. (add-to-list 'auto-mode-alist
  1308. '("/poetry\\.lock\\'" . toml-mode))
  1309. )
  1310. (when (autoload-eval-lazily 'json-mode)
  1311. (add-to-list 'auto-mode-alist
  1312. '("/Pipfile\\.lock\\'" . json-mode)))
  1313. (add-hook 'go-mode-hook
  1314. (lambda()
  1315. (defvar go-mode-map)
  1316. (add-hook 'before-save-hook' 'gofmt-before-save nil t)
  1317. (define-key go-mode-map (kbd "M-.") 'godef-jump)))
  1318. (when (autoload-eval-lazily 'k8s-mode)
  1319. (add-to-list 'auto-mode-alist
  1320. '("\\.k8s\\'" . k8s-mode)))
  1321. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1322. ;; buffers
  1323. (defvar bs-configurations)
  1324. (declare-function bs-set-configuration "bs")
  1325. (declare-function bs-refresh "bs")
  1326. (declare-function bs-message-without-log "bs")
  1327. (declare-function bs--current-config-message "bs")
  1328. (when (autoload-eval-lazily 'bs '(bs-show)
  1329. (add-to-list 'bs-configurations
  1330. '("specials" "^\\*" nil ".*" nil nil))
  1331. (add-to-list 'bs-configurations
  1332. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  1333. (defvar bs-mode-map)
  1334. (defvar bs-current-configuration)
  1335. (define-key bs-mode-map (kbd "t")
  1336. ;; TODO: fix toggle feature
  1337. (lambda ()
  1338. (interactive)
  1339. (if (string= "specials"
  1340. bs-current-configuration)
  1341. (bs-set-configuration "files")
  1342. (bs-set-configuration "specials"))
  1343. (bs-refresh)
  1344. (bs-message-without-log "%s"
  1345. (bs--current-config-message))))
  1346. ;; (setq bs-configurations (list
  1347. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1348. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1349. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1350. )
  1351. (defalias 'list-buffers 'bs-show)
  1352. (set-variable 'bs-default-configuration "files-and-specials")
  1353. (set-variable 'bs-default-sort-name "by nothing")
  1354. (add-hook 'bs-mode-hook
  1355. (lambda ()
  1356. (set (make-local-variable 'scroll-margin) 0))))
  1357. ;;(iswitchb-mode 1)
  1358. (icomplete-mode)
  1359. (defun iswitchb-buffer-display-other-window ()
  1360. "Do iswitchb in other window."
  1361. (interactive)
  1362. (let ((iswitchb-default-method 'display))
  1363. (call-interactively 'iswitchb-buffer)))
  1364. ;; buffer killing
  1365. ;; (defun my-delete-window-killing-buffer () nil)
  1366. (defun my-query-kill-current-buffer ()
  1367. "Interactively kill current buffer."
  1368. (interactive)
  1369. (if (y-or-n-p (concat "kill current buffer? :"))
  1370. (kill-buffer (current-buffer))))
  1371. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  1372. (substitute-key-definition 'kill-buffer
  1373. 'my-query-kill-current-buffer
  1374. global-map)
  1375. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1376. ;; dired
  1377. (defun my-file-head (filename &optional n)
  1378. "Return list of first N lines of file FILENAME."
  1379. ;; TODO: Fix for janapese text
  1380. ;; TODO: Fix for short text
  1381. (let ((num (or n 10))
  1382. (size 100)
  1383. (beg 0)
  1384. (end 0)
  1385. (result '())
  1386. (read -1))
  1387. (with-temp-buffer
  1388. (erase-buffer)
  1389. (while (or (<= (count-lines (point-min)
  1390. (point-max))
  1391. num)
  1392. (eq read 0))
  1393. (setq end (+ beg size))
  1394. (setq read (nth 1 (insert-file-contents-literally filename
  1395. nil
  1396. beg
  1397. end)))
  1398. (goto-char (point-max))
  1399. (setq beg (+ beg size)))
  1400. (goto-char (point-min))
  1401. (while (< (length result) num)
  1402. (let ((start (point)))
  1403. (forward-line 1)
  1404. (setq result
  1405. `(,@result ,(buffer-substring-no-properties start
  1406. (point))))))
  1407. result
  1408. ;; (buffer-substring-no-properties (point-min)
  1409. ;; (progn
  1410. ;; (forward-line num)
  1411. ;; (point)))
  1412. )))
  1413. ;; (apply 'concat (my-file-head "./shrc" 10)
  1414. (defun my-dired-echo-file-head (arg)
  1415. "Echo head of current file.
  1416. ARG is num to show, or defaults to 7."
  1417. (interactive "P")
  1418. (let ((f (dired-get-filename)))
  1419. (message "%s"
  1420. (apply 'concat
  1421. (my-file-head f
  1422. 7)))))
  1423. (defun my-dired-diff ()
  1424. "Show diff of marked file and file of current line."
  1425. (interactive)
  1426. (let ((files (dired-get-marked-files nil nil nil t)))
  1427. (if (eq (car files)
  1428. t)
  1429. (diff (cadr files) (dired-get-filename))
  1430. (message "One file must be marked!"))))
  1431. (defun dired-get-file-info ()
  1432. "Print information of current line file."
  1433. (interactive)
  1434. (let ((f (shell-quote-argument (dired-get-filename t))))
  1435. (if (file-directory-p f)
  1436. (progn
  1437. (message "Calculating disk usage...")
  1438. (shell-command (concat "du -hsD "
  1439. f)))
  1440. (shell-command (concat "file "
  1441. f)))))
  1442. (defun my-dired-scroll-up ()
  1443. "Scroll up."
  1444. (interactive)
  1445. (my-dired-previous-line (- (window-height) 1)))
  1446. (defun my-dired-scroll-down ()
  1447. "Scroll down."
  1448. (interactive)
  1449. (my-dired-next-line (- (window-height) 1)))
  1450. ;; (defun my-dired-forward-line (arg)
  1451. ;; ""
  1452. ;; (interactive "p"))
  1453. (defun my-dired-previous-line (arg)
  1454. "Move ARG lines up."
  1455. (interactive "p")
  1456. (if (> arg 0)
  1457. (progn
  1458. (if (eq (line-number-at-pos)
  1459. 1)
  1460. (goto-char (point-max))
  1461. (forward-line -1))
  1462. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1463. (dired-get-subdir))
  1464. (- arg 1)
  1465. arg)))
  1466. (dired-move-to-filename)))
  1467. (defun my-dired-next-line (arg)
  1468. "Move ARG lines down."
  1469. (interactive "p")
  1470. (if (> arg 0)
  1471. (progn
  1472. (if (eq (point)
  1473. (point-max))
  1474. (goto-char (point-min))
  1475. (forward-line 1))
  1476. (my-dired-next-line (if (or (dired-get-filename nil t)
  1477. (dired-get-subdir))
  1478. (- arg 1)
  1479. arg)))
  1480. (dired-move-to-filename)))
  1481. (defun my-tramp-remote-find-file (f)
  1482. "Open F."
  1483. (interactive (list (read-file-name "My Find File Tramp: "
  1484. "/scp:"
  1485. nil ;; "/scp:"
  1486. (confirm-nonexistent-file-or-buffer))))
  1487. (find-file f))
  1488. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1489. (if (eq window-system 'mac)
  1490. (setq dired-listing-switches "-lhF")
  1491. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1492. )
  1493. (setq dired-listing-switches "-lhF")
  1494. ;; when using dired-find-alternate-file
  1495. ;; reuse current dired buffer for the file to open
  1496. ;; (put 'dired-find-alternate-file 'disabled nil)
  1497. (set-variable 'dired-ls-F-marks-symlinks t)
  1498. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1499. (set-variable 'ls-lisp-dirs-first t)
  1500. (set-variable 'ls-lisp-use-localized-time-format t)
  1501. (set-variable 'ls-lisp-format-time-list
  1502. '("%Y-%m-%d %H:%M"
  1503. "%Y-%m-%d "))
  1504. (set-variable 'dired-dwim-target t)
  1505. (set-variable 'dired-isearch-filenames t)
  1506. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1507. (set-variable 'dired-hide-details-hide-information-lines nil)
  1508. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1509. (set-variable 'dired-recursive-deletes 'always)
  1510. ;; (add-hook 'dired-after-readin-hook
  1511. ;; 'my-replace-nasi-none)
  1512. (with-eval-after-load 'dired
  1513. (safe-require-or-eval 'ls-lisp)
  1514. (defvar dired-mode-map (make-sparse-keymap))
  1515. ;; dired-do-chgrp sometimes cause system hung
  1516. (define-key dired-mode-map "G" 'ignore)
  1517. (define-key dired-mode-map "e" 'wdired-change-to-wdired-mode)
  1518. (define-key dired-mode-map "i" 'dired-get-file-info)
  1519. ;; (define-key dired-mode-map "f" 'find-file)
  1520. (define-key dired-mode-map "f" 'my-fzf-or-find-file)
  1521. (define-key dired-mode-map "z" 'fzf)
  1522. (define-key dired-mode-map "!" 'shell-command)
  1523. (define-key dired-mode-map "&" 'async-shell-command)
  1524. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1525. (define-key dired-mode-map "=" 'my-dired-diff)
  1526. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1527. (define-key dired-mode-map "b" 'gtkbm)
  1528. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1529. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1530. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1531. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1532. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1533. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1534. (substitute-key-definition 'dired-next-line
  1535. 'my-dired-next-line
  1536. dired-mode-map)
  1537. (substitute-key-definition 'dired-previous-line
  1538. 'my-dired-previous-line
  1539. dired-mode-map)
  1540. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1541. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1542. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1543. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1544. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1545. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1546. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1547. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1548. (add-hook 'dired-mode-hook
  1549. (lambda ()
  1550. (when (fboundp 'dired-hide-details-mode)
  1551. (dired-hide-details-mode t)
  1552. (local-set-key "l" 'dired-hide-details-mode))
  1553. (let ((file "._Icon\015"))
  1554. (when nil
  1555. '(file-readable-p file)
  1556. (delete-file file)))))
  1557. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack)
  1558. (defvar pack-program-alist)
  1559. (add-to-list 'pack-program-alist
  1560. '("\\.txz\\'" :pack "tar -cJf" :unpack "tar -xf")))
  1561. (set-variable 'pack-silence
  1562. t)
  1563. (with-eval-after-load 'dired
  1564. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1565. (when (autoload-eval-lazily 'dired-list-all-mode)
  1566. (setq dired-listing-switches "-lhF")
  1567. (with-eval-after-load 'dired
  1568. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1569. (when (autoload-eval-lazily 'dired-filter)
  1570. (add-hook 'dired-mode-hook
  1571. 'dired-filter-mode))
  1572. (set-variable 'dired-filter-stack nil)
  1573. ;; Currently disabled in favor of dired-from-git-ls-files
  1574. ;; (define-key ctl-x-map "f" 'find-dired)
  1575. (defvar my-dired-git-ls-files-history
  1576. "History for `my-dired-git-ls-files'." nil)
  1577. (defun my-dired-git-ls-files (arg)
  1578. "Dired from git ls-files."
  1579. (interactive (list
  1580. (read-shell-command "git ls-files: "
  1581. "git ls-files -z ")))
  1582. (pop-to-buffer-same-window
  1583. (dired-noselect `(,default-directory
  1584. ,@(split-string (shell-command-to-string arg)
  1585. "\0" t))
  1586. ""))
  1587. )
  1588. (define-key ctl-x-map (kbd "G") 'my-dired-git-ls-files)
  1589. (with-eval-after-load 'dired
  1590. (defvar dired-mode-map (make-sparse-keymap))
  1591. (define-key dired-mode-map "G" 'my-dired-git-ls-files))
  1592. ;; (define-minor-mode my-dired-glob-filter)
  1593. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1594. ;; misc funcs
  1595. (define-key ctl-x-map "T" 'git-worktree)
  1596. (define-key ctl-x-map "W" 'git-walktree)
  1597. (when (fboundp 'browse-url-default-macosx-browser)
  1598. (defalias 'browse-osx 'browse-url-default-macosx-browser))
  1599. (defalias 'qcalc 'quick-calc)
  1600. (defun memo (&optional dir)
  1601. "Open memo.txt in DIR."
  1602. (interactive)
  1603. (pop-to-buffer (find-file-noselect (concat (if dir
  1604. (file-name-as-directory dir)
  1605. "")
  1606. "memo.txt"))))
  1607. ;; TODO: remember-projectile
  1608. (set (defvar my-privnotes-path nil
  1609. "My privnotes repository path.")
  1610. (expand-file-name "~/my/privnotes"))
  1611. (defun my-privnotes-readme (dir)
  1612. "Open my privnotes DIR."
  1613. (interactive (list
  1614. (read-file-name "Privnotes: "
  1615. (expand-file-name (format-time-string "%Y%m%d_")
  1616. my-privnotes-path))))
  1617. (let ((path (expand-file-name "README.md" dir)))
  1618. (with-current-buffer (find-file path)
  1619. (unless (file-exists-p path)
  1620. (insert (file-name-base dir)
  1621. "\n"
  1622. "=======\n"
  1623. "\n\n")))))
  1624. (define-key ctl-x-map "p" 'my-privnotes-readme)
  1625. (set (defvar my-rgrep-alist nil
  1626. "Alist of rgrep command.
  1627. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1628. condition to choose COMMAND when evaluated.")
  1629. `(
  1630. ;; ripgrep
  1631. ("rg"
  1632. (executable-find "rg")
  1633. "rg -nH --no-heading --color=always --hidden --glob '!.git/' --smart-case -M 1280 ")
  1634. ;; git grep
  1635. ("gitgrep"
  1636. (eq 0
  1637. (shell-command "git rev-parse --git-dir"))
  1638. "git --no-pager -c color.grep=always grep -nH -e ")
  1639. ;; sift
  1640. ("sift"
  1641. (executable-find "sift")
  1642. ("sift --binary-skip --filename --line-number --git --smart-case "))
  1643. ;; the silver searcher
  1644. ("ag"
  1645. (executable-find "ag")
  1646. "ag --nogroup --nopager --filename ")
  1647. ;; ack
  1648. ("ack"
  1649. (executable-find "ack")
  1650. "ack --nogroup --nopager --with-filename ")
  1651. ;; gnu global
  1652. ("global"
  1653. (and (require 'ggtags nil t)
  1654. (executable-find "global")
  1655. (ggtags-current-project-root))
  1656. "global --result grep ")
  1657. ;; grep
  1658. ("grep"
  1659. t
  1660. ,(concat "find . "
  1661. "-path '*/.git' -prune -o "
  1662. "-path '*/.svn' -prune -o "
  1663. "-type f -print0 | "
  1664. "xargs -0 grep -nH -e "))
  1665. )
  1666. )
  1667. (defvar my-rgrep-default nil
  1668. "Default command name for my-rgrep.")
  1669. (defun my-rgrep-grep-command (&optional name alist)
  1670. "Return recursive grep command for current directory or nil.
  1671. If NAME is given, use that without testing.
  1672. Commands are searched from ALIST."
  1673. (if alist
  1674. (if name
  1675. ;; if name is given search that from alist and return the command
  1676. (nth 2 (assoc name
  1677. alist))
  1678. ;; if name is not given try test in 1th elem
  1679. (let ((car (car alist))
  1680. (cdr (cdr alist)))
  1681. (if (eval (nth 1 car))
  1682. ;; if the condition is true return the command
  1683. (nth 2 car)
  1684. ;; try next one
  1685. (and cdr
  1686. (my-rgrep-grep-command name cdr)))))
  1687. ;; if alist is not given set default value
  1688. (my-rgrep-grep-command name my-rgrep-alist)))
  1689. (defun my-rgrep (command-args)
  1690. "My recursive grep. Run COMMAND-ARGS.
  1691. If prefix argument is given, use current symbol as default search target
  1692. and search from projectile root (if projectile is available)."
  1693. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1694. nil)))
  1695. (if cmd
  1696. (list (read-shell-command "grep command: "
  1697. (concat cmd
  1698. (if current-prefix-arg
  1699. (thing-at-point 'symbol t)
  1700. ""))
  1701. 'grep-find-history))
  1702. (error "My-Rgrep: Command for rgrep not found")
  1703. )))
  1704. (if (and current-prefix-arg
  1705. (safe-require-or-eval 'projectile)
  1706. (projectile-project-p))
  1707. (projectile-with-default-dir (projectile-project-root)
  1708. (compilation-start command-args
  1709. 'grep-mode))
  1710. (compilation-start command-args
  1711. 'grep-mode)))
  1712. (defun my-rgrep-thing-at-point-projectile-root ()
  1713. "My recursive grep to find thing at point from project root."
  1714. (interactive)
  1715. (let* ((cmd (my-rgrep-grep-command my-rgrep-default
  1716. nil))
  1717. (command-args
  1718. (if cmd
  1719. (concat cmd
  1720. (or (thing-at-point 'symbol t)
  1721. (error "No symbol at point")))
  1722. (error "My-Rgrep: Command for rgrep not found"))))
  1723. (if (safe-require-or-eval 'projectile)
  1724. (projectile-with-default-dir (or (projectile-project-root)
  1725. default-directory)
  1726. (compilation-start command-args
  1727. 'grep-mode))
  1728. (compilation-start command-args
  1729. 'grep-mode))))
  1730. (defmacro define-my-rgrep (name)
  1731. "Define rgrep for NAME."
  1732. `(defun ,(intern (concat "my-rgrep-"
  1733. name)) ()
  1734. ,(format "My recursive grep by %s."
  1735. name)
  1736. (interactive)
  1737. (let ((my-rgrep-default ,name))
  1738. (if (called-interactively-p 'any)
  1739. (call-interactively 'my-rgrep)
  1740. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1741. )
  1742. (define-my-rgrep "ack")
  1743. (define-my-rgrep "ag")
  1744. (define-my-rgrep "rg")
  1745. (define-my-rgrep "sift")
  1746. (define-my-rgrep "gitgrep")
  1747. (define-my-rgrep "grep")
  1748. (define-my-rgrep "global")
  1749. (define-key ctl-x-map "s" 'my-rgrep)
  1750. (define-key ctl-x-map "." 'my-rgrep-thing-at-point-projectile-root)
  1751. (defun my-occur (regexp &optional region)
  1752. "My occur command to search REGEXP."
  1753. (interactive (list (read-string "List lines matching regexp: "
  1754. (thing-at-point 'symbol t))))
  1755. (occur regexp nil region))
  1756. (define-key ctl-x-map (kbd "C-o") 'my-occur)
  1757. (set-variable 'dumb-jump-prefer-searcher 'rg)
  1758. (defalias 'make 'compile)
  1759. (define-key ctl-x-map "c" 'compile)
  1760. (defun my-pushbullet-note (text &optional title)
  1761. "Push TEXT."
  1762. (interactive "sText to Push: ")
  1763. (pb/push-item '("") text "note" (or title "")))
  1764. ;;;;;;;;;;;;;;;;;;;;;
  1765. ;; git-bug
  1766. (defconst git-bug-ls-regexp
  1767. (eval-when-compile
  1768. (rx bol
  1769. (submatch (one-or-more alphanumeric)) ; id
  1770. ;; (one-or-more any)
  1771. (one-or-more space)
  1772. (submatch (or "open" "close")) ; status
  1773. (one-or-more space)
  1774. (submatch (maximal-match (zero-or-more print))) ; title
  1775. "\t"
  1776. (submatch (one-or-more alphanumeric)) ; user
  1777. (one-or-more space)
  1778. "C:"
  1779. (submatch (one-or-more digit)) ; Comment num
  1780. (one-or-more space)
  1781. "L:"
  1782. (submatch (one-or-more digit)) ; Label num
  1783. eol
  1784. ))
  1785. "Regexp to parse line of output of git-bug ls.
  1786. Used by `git-bug-ls'.")
  1787. (defun git-bug-bugs ()
  1788. "Get list of git-bug bugs."
  1789. (with-temp-buffer
  1790. (git-bug--call-process "bug" "ls")
  1791. (goto-char (point-min))
  1792. (let ((bugs nil))
  1793. (while (not (eq (point) (point-max)))
  1794. (save-match-data
  1795. (when (re-search-forward git-bug-ls-regexp (point-at-eol) t)
  1796. (setq bugs `(,@bugs
  1797. ,(list
  1798. :id (match-string 1)
  1799. :status (match-string 2)
  1800. :title (string-trim (match-string 3))
  1801. :user (match-string 4)
  1802. :comment-num (match-string 5)
  1803. :label-num (match-string 6)
  1804. )))))
  1805. (forward-line 1)
  1806. (goto-char (point-at-bol)))
  1807. bugs)))
  1808. (defun git-bug-ls-noselect (&optional directory)
  1809. "Open git bug list buffer.
  1810. If optional arg DIRECTORY is given change current directory to there before
  1811. initializing."
  1812. (setq directory (expand-file-name (or directory
  1813. default-directory)))
  1814. (cl-assert (file-directory-p directory))
  1815. (let* ((root (git-bug--get-repository-root directory))
  1816. (name (file-name-nondirectory root))
  1817. (bname (format "*GitBug<%s>*" name)))
  1818. (with-current-buffer (get-buffer-create bname)
  1819. (cd root)
  1820. (git-bug-ls--set-tabulated-list-mode-variables)
  1821. (git-bug-ls-mode)
  1822. (current-buffer))))
  1823. (defun git-bug--get-repository-root (dir)
  1824. "Resolve repository root of DIR.
  1825. If DIR is not inside of any git repository, signal an error."
  1826. (cl-assert (file-directory-p dir))
  1827. (with-temp-buffer
  1828. (cd dir)
  1829. (git-bug--call-process "rev-parse" "--show-toplevel")
  1830. (goto-char (point-min))
  1831. (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
  1832. (defun git-bug--call-process (&rest args)
  1833. "Start git process synchronously with ARGS.
  1834. Raise error when git process ends with non-zero status.
  1835. Any output will be written to current buffer."
  1836. (let ((status (apply 'call-process
  1837. "git"
  1838. nil
  1839. t
  1840. nil
  1841. args)))
  1842. (cl-assert (eq status 0)
  1843. nil
  1844. (buffer-substring-no-properties (point-min) (point-max)))))
  1845. ;;;;;;;;;;;;;;;;;;;
  1846. ;; peek-file-mode
  1847. (defun peek-file (file)
  1848. "Peek FILE."
  1849. (interactive)
  1850. (message "%s" file))
  1851. ;; Local Variables:
  1852. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1853. ;; flycheck-checker: emacs-lisp
  1854. ;; End:
  1855. ;;; emancs.el ends here