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.
 
 
 
 
 
 

1607 lines
52 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Commentary:
  3. ;;; Code:
  4. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  5. ;; SETUP_LOAD: (and (file-readable-p file)
  6. ;; SETUP_LOAD: (load-file file)))
  7. (setq debug-on-error t)
  8. ;; make directories
  9. (unless (file-directory-p (expand-file-name user-emacs-directory))
  10. (make-directory (expand-file-name user-emacs-directory)))
  11. (require 'cl-lib)
  12. (require 'simple)
  13. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  14. ;; Some macros for internals
  15. (defun call-after-init (func)
  16. "If `after-init-hook' has been run, call FUNC immediately.
  17. Otherwize hook it."
  18. (if after-init-time
  19. (funcall func)
  20. (add-hook 'after-init-hook
  21. func)))
  22. (defmacro safe-require-or-eval (feature)
  23. "Require FEATURE if available.
  24. At compile time the feature will be loaded immediately."
  25. `(eval-and-compile
  26. (message "safe-require-or-eval: Trying to require %s" ,feature)
  27. (require ,feature nil t)))
  28. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  29. "Define autoloading FEATURE that defines FUNCTIONS.
  30. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  31. the function same as FEATURE is defined as autoloaded function. BODY is passed
  32. to `eval-after-load'.
  33. After this macro is expanded, this returns the path to library if FEATURE
  34. found, otherwise returns nil."
  35. (declare (indent 2) (debug t))
  36. (let* ((libname (symbol-name (eval feature)))
  37. (libpath (locate-library libname)))
  38. `(progn
  39. (when (locate-library ,libname)
  40. ,@(mapcar (lambda (f)
  41. `(unless (fboundp ',f)
  42. (progn
  43. (message "Autoloaded function `%S' defined (%s)"
  44. (quote ,f)
  45. ,libpath)
  46. (autoload (quote ,f)
  47. ,libname
  48. ,(concat "Autoloaded function defined in \""
  49. libpath
  50. "\".")
  51. t))))
  52. (or (eval functions)
  53. `(,(eval feature)))))
  54. (eval-after-load ,feature
  55. (quote (progn
  56. ,@body)))
  57. (locate-library ,libname))))
  58. (when (autoload-eval-lazily 'tetris nil
  59. (message "Tetris loaded!"))
  60. (message "Tetris found!"))
  61. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  62. ;; package
  63. (set (defvar 10sr-package-list)
  64. '(
  65. vimrc-mode
  66. markdown-mode
  67. yaml-mode
  68. gnuplot-mode
  69. php-mode
  70. erlang
  71. js2-mode
  72. js-doc
  73. git-commit
  74. gitignore-mode
  75. adoc-mode
  76. ;; It seems malabar has been merged into jdee and this package
  77. ;; already removed
  78. ;; malabar-mode
  79. ;; ack
  80. color-moccur
  81. ggtags
  82. flycheck
  83. auto-highlight-symbol
  84. hl-todo
  85. ;; Currently not available
  86. ;; pp-c-l
  87. ;; is flymake installs are required?
  88. ;;flymake-jshint
  89. ;;flymake-python-pyflakes
  90. xclip
  91. foreign-regexp
  92. multi-term
  93. term-run
  94. editorconfig
  95. git-ps1-mode
  96. restart-emacs
  97. fill-column-indicator
  98. pkgbuild-mode
  99. minibuffer-line
  100. which-key
  101. ;; I think this works in place of my autosave lib
  102. super-save
  103. pipenv
  104. imenu-list
  105. scala-mode
  106. ;;ensime
  107. editorconfig
  108. editorconfig-custom-majormode
  109. git-command
  110. prompt-text
  111. ;; 10sr repository
  112. ;; 10sr-extras
  113. terminal-title
  114. recentf-show
  115. dired-list-all-mode
  116. pack
  117. set-modeline-color
  118. read-only-only-mode
  119. smart-revert
  120. autosave
  121. ;;window-organizer
  122. remember-major-modes-mode
  123. ilookup
  124. pasteboard
  125. end-mark
  126. sl
  127. gosh-mode
  128. ))
  129. (when (safe-require-or-eval 'package)
  130. (setq package-archives
  131. `(,@package-archives
  132. ("melpa" . "https://melpa.org/packages/")
  133. ;; Somehow fails to download via https
  134. ("10sr-el" . "http://10sr.github.io/emacs-lisp/elpa/")))
  135. (package-initialize)
  136. (defun my-auto-install-package ()
  137. "Install packages semi-automatically."
  138. (interactive)
  139. (package-refresh-contents)
  140. (mapc (lambda (pkg)
  141. (or (package-installed-p pkg)
  142. (package-install pkg)))
  143. 10sr-package-list))
  144. )
  145. ;; (lazy-load-eval 'sudoku)
  146. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  147. ;; my-idle-hook
  148. (defvar my-idle-hook nil
  149. "Hook run when idle for several secs.")
  150. (defvar my-idle-hook-sec 5
  151. "Second to run `my-idle-hook'.")
  152. (run-with-idle-timer my-idle-hook-sec
  153. t
  154. (lambda ()
  155. (run-hooks 'my-idle-hook)))
  156. ;; (add-hook 'my-idle-hook
  157. ;; (lambda ()
  158. ;; (message "idle hook message")))
  159. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  160. ;; start and quit
  161. (setq inhibit-startup-message t)
  162. (setq confirm-kill-emacs 'y-or-n-p)
  163. (setq gc-cons-threshold (* 1024 1024 4))
  164. (when window-system
  165. (add-to-list 'default-frame-alist '(cursor-type . box))
  166. (add-to-list 'default-frame-alist '(background-color . "white"))
  167. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  168. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  169. ;; does not work?
  170. )
  171. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  172. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  173. (and (fboundp 'tool-bar-mode)
  174. (tool-bar-mode 0))
  175. (and (fboundp 'set-scroll-bar-mode)
  176. (set-scroll-bar-mode nil))
  177. (call-after-init
  178. (lambda ()
  179. (message "%s %s" invocation-name emacs-version)
  180. (message "Invocation directory: %s" default-directory)
  181. (message "%s was taken to initialize emacs." (emacs-init-time))
  182. (switch-to-buffer "*Messages*")))
  183. (cd ".") ; when using windows use / instead of \ in `default-directory'
  184. ;; locale
  185. (set-language-environment "Japanese")
  186. (set-default-coding-systems 'utf-8-unix)
  187. (prefer-coding-system 'utf-8-unix)
  188. (setq system-time-locale "C")
  189. ;; my prefix map
  190. (defvar my-prefix-map nil
  191. "My prefix map.")
  192. (define-prefix-command 'my-prefix-map)
  193. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  194. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  195. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  196. ;; (comint-show-maximum-output)
  197. ;; kill scratch
  198. (call-after-init (lambda ()
  199. (let ((buf (get-buffer "*scratch*")))
  200. (when buf
  201. (kill-buffer buf)))))
  202. ;; modifier keys
  203. ;; (setq mac-option-modifier 'control)
  204. ;; display
  205. (setq visible-bell t)
  206. (setq ring-bell-function 'ignore)
  207. (mouse-avoidance-mode 'banish)
  208. (setq echo-keystrokes 0.1)
  209. (defun reload-init-file ()
  210. "Reload Emacs init file."
  211. (interactive)
  212. (when (and user-init-file
  213. (file-readable-p user-init-file))
  214. (load-file user-init-file)))
  215. (safe-require-or-eval 'session)
  216. ;; server
  217. (set-variable 'server-name (concat "server"
  218. (number-to-string (emacs-pid))))
  219. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  220. ;; In Darwin environment, init fails with message like 'Service name too long'
  221. ;; when server-use-tcp is nil
  222. (when (or (eq system-type
  223. 'cygwin)
  224. (eq system-type
  225. 'darwin))
  226. (set-variable 'server-use-tcp t))
  227. ;; MSYS2 fix
  228. (when (eq system-type
  229. 'windows-nt)
  230. (setq shell-file-name
  231. (executable-find "bash"))
  232. '(setq function-key-map
  233. `(,@function-key-map ([pause] . [?\C-c])
  234. ))
  235. (define-key key-translation-map
  236. (kbd "<pause>")
  237. (kbd "C-c"))
  238. '(keyboard-translate [pause]
  239. (kbd "C-c")p)
  240. ;; TODO: move to other place later
  241. (when (not window-system)
  242. (setq interprogram-paste-function nil)
  243. (setq interprogram-cut-function nil)))
  244. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  245. ;; global keys
  246. (global-set-key (kbd "<up>") 'scroll-down-line)
  247. (global-set-key (kbd "<down>") 'scroll-up-line)
  248. (global-set-key (kbd "<left>") 'scroll-down)
  249. (global-set-key (kbd "<right>") 'scroll-up)
  250. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  251. (global-set-key (kbd "C-\\") help-map)
  252. (define-key ctl-x-map (kbd "DEL") help-map)
  253. (define-key ctl-x-map (kbd "C-h") help-map)
  254. (define-key help-map "a" 'apropos)
  255. ;; disable annoying keys
  256. (global-set-key [prior] 'ignore)
  257. (global-set-key (kbd "<next>") 'ignore)
  258. (global-set-key [menu] 'ignore)
  259. (global-set-key [down-mouse-1] 'ignore)
  260. (global-set-key [down-mouse-2] 'ignore)
  261. (global-set-key [down-mouse-3] 'ignore)
  262. (global-set-key [mouse-1] 'ignore)
  263. (global-set-key [mouse-2] 'ignore)
  264. (global-set-key [mouse-3] 'ignore)
  265. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  266. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  267. (when (safe-require-or-eval 'which-key)
  268. (which-key-mode))
  269. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  270. ;; editting
  271. (setq require-final-newline t)
  272. (setq kill-whole-line t)
  273. (setq scroll-conservatively 35
  274. scroll-margin 2
  275. scroll-step 0)
  276. (setq-default major-mode 'text-mode)
  277. (setq next-line-add-newlines nil)
  278. (setq kill-read-only-ok t)
  279. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  280. ;; (setq-default line-spacing 0.2)
  281. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  282. ;; (setq-default tab-width 4)
  283. (setq-default indent-tabs-mode nil)
  284. (setq-default indent-line-function 'indent-to-left-margin)
  285. ;; (setq-default indent-line-function nil)
  286. (setq-default truncate-lines nil)
  287. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  288. (delete-selection-mode 1)
  289. (cua-mode 0)
  290. (setq line-move-visual nil)
  291. ;; key bindings
  292. ;; moving around
  293. ;;(keyboard-translate ?\M-j ?\C-j)
  294. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  295. (define-key esc-map "p" 'backward-paragraph)
  296. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  297. (define-key esc-map "n" 'forward-paragraph)
  298. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  299. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  300. (global-set-key (kbd "C-<left>") 'scroll-down)
  301. (global-set-key (kbd "C-<right>") 'scroll-up)
  302. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  303. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  304. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  305. ;; C-h and DEL
  306. (global-set-key (kbd "C-h") (kbd "DEL"))
  307. ;;(global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  308. (global-set-key (kbd "C-m") 'newline-and-indent)
  309. ;; (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  310. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  311. (define-key esc-map "u" 'undo)
  312. (define-key esc-map "i" (kbd "ESC TAB"))
  313. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  314. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  315. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  316. (define-key my-prefix-map (kbd "C-o") 'occur)
  317. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  318. ;; title and mode-line
  319. (when (safe-require-or-eval 'terminal-title)
  320. ;; if TERM is not screen use default value
  321. (if (getenv "TMUX")
  322. ;; if use tmux locally just basename of current dir
  323. (set-variable 'terminal-title-format
  324. '((file-name-nondirectory (directory-file-name
  325. default-directory))))
  326. (if (and (let ((tty-type (frame-parameter nil
  327. 'tty-type)))
  328. (and tty-type
  329. (equal (car (split-string tty-type
  330. "-"))
  331. "screen")))
  332. (not (getenv "SSH_CONNECTION")))
  333. (set-variable 'terminal-title-format
  334. '((file-name-nondirectory (directory-file-name
  335. default-directory))))
  336. ;; seems that TMUX is used locally and ssh to remote host
  337. (set-variable 'terminal-title-format
  338. `("em:"
  339. ,user-login-name
  340. "@"
  341. ,(car (split-string (system-name)
  342. "\\."))
  343. ":"
  344. default-directory))
  345. )
  346. )
  347. (terminal-title-mode))
  348. (setq eol-mnemonic-dos "\\r\\n")
  349. (setq eol-mnemonic-mac "\\r")
  350. (setq eol-mnemonic-unix "")
  351. (which-function-mode 0)
  352. (line-number-mode 0)
  353. (column-number-mode 0)
  354. (size-indication-mode 0)
  355. (setq mode-line-position
  356. '(:eval (format "L%%l/%d:C%%c"
  357. (count-lines (point-max)
  358. (point-min)))))
  359. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  360. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  361. ;; minibuffer
  362. (setq insert-default-directory t)
  363. (setq completion-ignore-case t
  364. read-file-name-completion-ignore-case t
  365. read-buffer-completion-ignore-case t)
  366. (setq resize-mini-windows t)
  367. (temp-buffer-resize-mode 1)
  368. (savehist-mode 1)
  369. (defvar display-time-format "%Y/%m/%d %a %H:%M")
  370. (fset 'yes-or-no-p 'y-or-n-p)
  371. ;; complete symbol when `eval'
  372. (define-key read-expression-map (kbd "TAB") 'completion-at-point)
  373. (define-key minibuffer-local-map (kbd "C-u")
  374. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  375. ;; I dont know these bindings are good
  376. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  377. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  378. (when (safe-require-or-eval 'minibuffer-line)
  379. (set-face-underline 'minibuffer-line nil)
  380. (set-variable 'minibuffer-line-refresh-interval
  381. 25)
  382. ;; Set idle timer
  383. (defvar my-minibuffer-line--idle-timer nil)
  384. (defvar minibuffer-line-mode)
  385. (add-hook 'minibuffer-line-mode-hook
  386. (lambda ()
  387. (when my-minibuffer-line--idle-timer
  388. (cancel-timer my-minibuffer-line--idle-timer)
  389. (setq my-minibuffer-line--idle-timer nil))
  390. (when minibuffer-line-mode
  391. (setq my-minibuffer-line--idle-timer
  392. (run-with-idle-timer 0.5
  393. t
  394. 'minibuffer-line--update)))))
  395. (set-variable 'minibuffer-line-format
  396. `(,(concat user-login-name
  397. "@"
  398. (car (split-string (system-name)
  399. "\\."))
  400. ":")
  401. (:eval (abbreviate-file-name (or buffer-file-name
  402. default-directory)))
  403. (:eval (and (fboundp 'git-ps1-mode-get-current)
  404. (git-ps1-mode-get-current " [GIT:%s]")))
  405. " "
  406. (:eval (format-time-string display-time-format))))
  407. (minibuffer-line-mode 1)
  408. )
  409. (when (safe-require-or-eval 'prompt-text)
  410. (set-variable 'prompt-text-format
  411. `(,(concat ""
  412. user-login-name
  413. "@"
  414. (car (split-string (system-name)
  415. "\\."))
  416. ":")
  417. (:eval (abbreviate-file-name (or buffer-file-name
  418. default-directory)))
  419. (:eval (and (fboundp 'git-ps1-mode-get-current)
  420. (git-ps1-mode-get-current " [GIT:%s]")))
  421. " "
  422. (:eval (format-time-string display-time-format))
  423. "\n"
  424. (:eval (symbol-name this-command))
  425. ": "))
  426. (prompt-text-mode 1))
  427. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  428. ;; letters, font-lock mode and fonts
  429. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  430. ;; (set-window-margins (selected-window) 1 1)
  431. (and (or (eq system-type 'Darwin)
  432. (eq system-type 'darwin))
  433. (fboundp 'mac-set-input-method-parameter)
  434. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  435. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  436. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  437. (boundp 'input-method-inactivate-hook))
  438. (add-hook 'input-method-activate-hook
  439. (lambda () (set-cursor-color "red")))
  440. (add-hook 'input-method-inactivate-hook
  441. (lambda () (set-cursor-color "black"))))
  442. (when (safe-require-or-eval 'paren)
  443. (show-paren-mode 1)
  444. (setq show-paren-delay 0.5
  445. show-paren-style 'parenthesis) ; mixed is hard to read
  446. ;; (set-face-background 'show-paren-match
  447. ;; "black")
  448. ;; ;; (face-foreground 'default))
  449. ;; (set-face-foreground 'show-paren-match
  450. ;; "white")
  451. ;; (set-face-inverse-video-p 'show-paren-match
  452. ;; t)
  453. )
  454. (transient-mark-mode 1)
  455. (global-font-lock-mode 1)
  456. (setq font-lock-global-modes
  457. '(not
  458. help-mode
  459. eshell-mode
  460. ;;term-mode
  461. Man-mode))
  462. ;; (standard-display-ascii ?\n "$\n")
  463. ;; (defvar my-eol-face
  464. ;; '(("\n" . (0 font-lock-comment-face t nil)))
  465. ;; )
  466. ;; (defvar my-tab-face
  467. ;; '(("\t" . '(0 highlight t nil))))
  468. (defvar my-jspace-face
  469. '(("\u3000" . '(0 highlight t nil))))
  470. (add-hook 'font-lock-mode-hook
  471. (lambda ()
  472. ;; (font-lock-add-keywords nil my-eol-face)
  473. (font-lock-add-keywords nil my-jspace-face)
  474. ))
  475. (when (safe-require-or-eval 'whitespace)
  476. (add-to-list 'whitespace-display-mappings
  477. ;; We need t since last one takes precedence
  478. `(tab-mark ?\t ,(vconcat "^I\t")) t)
  479. ;; (add-to-list 'whitespace-display-mappings
  480. ;; `(newline-mark ?\n ,(vconcat "$\n")))
  481. (setq whitespace-style '(face
  482. trailing ; trailing blanks
  483. newline ; newlines
  484. newline-mark ; use display table for newline
  485. tab-mark
  486. empty ; empty lines at beg or end of buffer
  487. lines-tail ; lines over 80
  488. ))
  489. ;; (setq whitespace-newline 'font-lock-comment-face)
  490. (set-variable 'whitespace-line-column nil)
  491. (global-whitespace-mode t)
  492. (add-hook 'dired-mode-hook
  493. (lambda ()
  494. (set (make-local-variable 'whitespace-style) nil)))
  495. (if (eq (display-color-cells)
  496. 256)
  497. (set-face-foreground 'whitespace-newline "color-109")
  498. ;; (progn
  499. ;; (set-face-bold-p 'whitespace-newline
  500. ;; t))
  501. ))
  502. (and nil
  503. '(safe-require-or-eval 'fill-column-indicator)
  504. (setq fill-column-indicator))
  505. ;; highlight current line
  506. ;; http://wiki.riywo.com/index.php?Meadow
  507. (face-spec-set 'hl-line
  508. '((((min-colors 256)
  509. (background dark))
  510. (:background "color-234"))
  511. (((min-colors 256)
  512. (background light))
  513. (:background "color-234"))
  514. (t
  515. (:underline "black"))))
  516. (set-variable 'hl-line-global-modes
  517. '(not
  518. term-mode))
  519. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  520. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  521. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  522. ;;(safe-require-or-eval 'set-modeline-color)
  523. ;; (let ((fg (face-foreground 'default))
  524. ;; (bg (face-background 'default)))
  525. ;; (set-face-background 'mode-line-inactive
  526. ;; (if (face-inverse-video-p 'mode-line) fg bg))
  527. ;; (set-face-foreground 'mode-line-inactive
  528. ;; (if (face-inverse-video-p 'mode-line) bg fg)))
  529. ;; (set-face-underline 'mode-line-inactive
  530. ;; t)
  531. ;; (set-face-underline 'vertical-border
  532. ;; nil)
  533. (when (safe-require-or-eval 'end-mark)
  534. (global-end-mark-mode))
  535. (when (safe-require-or-eval 'auto-highlight-symbol)
  536. (set-variable 'ahs-idle-interval 0.6)
  537. (global-auto-highlight-symbol-mode 1))
  538. ;; (when (safe-require-or-eval 'cyberpunk-theme)
  539. ;; (load-theme 'cyberpunk t)
  540. ;; (set-face-attribute 'button
  541. ;; nil
  542. ;; :inherit 'highlight)
  543. ;; (set-face-foreground 'mode-line-inactive
  544. ;; "white")
  545. ;; (face-spec-set 'term-color-black (face-default-spec 'term-color-black))
  546. ;; (face-spec-set 'term-color-red (face-default-spec 'term-color-red))
  547. ;; (face-spec-set 'term-color-green (face-default-spec 'term-color-green))
  548. ;; (face-spec-set 'term-color-yellow (face-default-spec 'term-color-yellow))
  549. ;; (face-spec-set 'term-color-blue (face-default-spec 'term-color-blue))
  550. ;; (face-spec-set 'term-color-magenta (face-default-spec 'term-color-magenta))
  551. ;; (face-spec-set 'term-color-cyan (face-default-spec 'term-color-cyan))
  552. ;; (face-spec-set 'term-color-white (face-default-spec 'term-color-white))
  553. ;; )
  554. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  555. ;; file handling
  556. (when (safe-require-or-eval 'editorconfig)
  557. ;; (set-variable 'editorconfig-get-properties-function
  558. ;; 'editorconfig-core-get-properties-hash)
  559. (editorconfig-mode 1)
  560. (set-variable 'editorconfig-mode-lighter " EC")
  561. (with-eval-after-load 'org-src
  562. ;; [*.org\[\*Org Src*\[ c \]*\]]
  563. (add-hook 'org-src-mode-hook
  564. 'editorconfig-mode-apply t)))
  565. (when (fboundp 'editorconfig-custom-majormode)
  566. (add-hook 'editorconfig-custom-hooks
  567. 'editorconfig-custom-majormode))
  568. ;; (when (fboundp 'editorconfig-charset-extras)
  569. ;; (add-hook 'editorconfig-custom-hooks
  570. ;; 'editorconfig-charset-extras))
  571. (setq revert-without-query '(".+"))
  572. ;; save cursor position
  573. (when (safe-require-or-eval 'saveplace)
  574. (setq-default save-place t)
  575. (setq save-place-file (concat user-emacs-directory
  576. "places")))
  577. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  578. (setq make-backup-files t)
  579. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  580. (setq backup-directory-alist
  581. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  582. "backup")))
  583. backup-directory-alist))
  584. (setq version-control 'never)
  585. (setq delete-old-versions t)
  586. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  587. "auto-save/")))
  588. (setq delete-auto-save-files t)
  589. (add-to-list 'completion-ignored-extensions ".bak")
  590. (setq delete-by-moving-to-trash t)
  591. ;; trash-directory "~/.emacs.d/trash")
  592. (add-hook 'after-save-hook
  593. 'executable-make-buffer-file-executable-if-script-p)
  594. (set (defvar bookmark-default-file)
  595. (expand-file-name (concat user-emacs-directory
  596. "bmk")))
  597. (with-eval-after-load 'recentf
  598. (defvar recentf-exclude nil)
  599. (add-to-list 'recentf-exclude
  600. (regexp-quote bookmark-default-file)))
  601. (when (safe-require-or-eval 'smart-revert)
  602. (smart-revert-on))
  603. ;; autosave
  604. (when (safe-require-or-eval 'autosave)
  605. (autosave-set 8))
  606. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  607. ;; buffer killing
  608. ;; (defun my-delete-window-killing-buffer () nil)
  609. (defun my-query-kill-current-buffer ()
  610. "Interactively kill current buffer."
  611. (interactive)
  612. (if (y-or-n-p (concat "kill current buffer? :"))
  613. (kill-buffer (current-buffer))))
  614. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  615. (substitute-key-definition 'kill-buffer
  616. 'my-query-kill-current-buffer
  617. global-map)
  618. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  619. ;; share clipboard with x
  620. ;; this page describes this in details, but only these sexps seem to be needed
  621. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  622. (and nil
  623. (not window-system)
  624. (not (eq window-system 'mac))
  625. (getenv "DISPLAY")
  626. (not (equal (getenv "DISPLAY") ""))
  627. (executable-find "xclip")
  628. ;; (< emacs-major-version 24)
  629. '(safe-require-or-eval 'xclip)
  630. nil
  631. (turn-on-xclip))
  632. (and (eq system-type 'darwin)
  633. (safe-require-or-eval 'pasteboard)
  634. (turn-on-pasteboard))
  635. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  636. ;; some modes and hooks
  637. ;; Include some extra modes
  638. (require 'generic-x)
  639. (with-eval-after-load 'compile
  640. (defvar compilation-filter-start)
  641. (defvar compilation-error-regexp-alist)
  642. (require 'ansi-color)
  643. (add-hook 'compilation-filter-hook
  644. (lambda ()
  645. (let ((inhibit-read-only t))
  646. (ansi-color-apply-on-region compilation-filter-start
  647. (point)))))
  648. (add-to-list 'compilation-error-regexp-alist
  649. ;; ansible-lint
  650. '("^\\([^ \n]+\\):\\([0-9]+\\)$" 1 2)))
  651. ;; Workaround to avoid ensime error
  652. (defvar ensime-mode-key-prefix nil)
  653. ;; http://qiita.com/sune2/items/b73037f9e85962f5afb7
  654. (when (safe-require-or-eval 'company)
  655. (global-company-mode)
  656. (set-variable 'company-idle-delay 0.5)
  657. (set-variable 'company-minimum-prefix-length 2)
  658. (set-variable 'company-selection-wrap-around t))
  659. ;; https://github.com/lunaryorn/flycheck
  660. (when (safe-require-or-eval 'flycheck)
  661. (call-after-init 'global-flycheck-mode))
  662. (when (autoload-eval-lazily 'ilookup)
  663. (define-key ctl-x-map "d" 'ilookup-open-word))
  664. (set-variable 'ac-ignore-case nil)
  665. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  666. (define-key ctl-x-map "t" 'term-run-shell-command))
  667. (add-to-list 'safe-local-variable-values
  668. '(encoding utf-8))
  669. (setq enable-local-variables :safe)
  670. ;; (when (safe-require-or-eval 'remember-major-modes-mode)
  671. ;; (remember-major-modes-mode 1))
  672. ;; Detect file type from shebang and set major-mode.
  673. (add-to-list 'interpreter-mode-alist
  674. '("python3" . python-mode))
  675. (add-to-list 'interpreter-mode-alist
  676. '("python2" . python-mode))
  677. (with-eval-after-load 'python
  678. (defvar python-mode-map (make-sparse-keymap))
  679. (define-key python-mode-map (kbd "C-m") 'newline-and-indent))
  680. ;; http://fukuyama.co/foreign-regexp
  681. '(and (safe-require-or-eval 'foreign-regexp)
  682. (progn
  683. (setq foreign-regexp/regexp-type 'perl)
  684. '(setq reb-re-syntax 'foreign-regexp)
  685. ))
  686. (autoload-eval-lazily 'sql '(sql-mode)
  687. (require 'sql-indent nil t))
  688. (when (autoload-eval-lazily 'git-command)
  689. (define-key ctl-x-map "g" 'git-command))
  690. (when (safe-require-or-eval 'git-commit)
  691. (global-git-commit-mode 1))
  692. (with-eval-after-load 'git-commit
  693. (add-hook 'git-commit-setup-hook
  694. 'turn-off-auto-fill t))
  695. (autoload-eval-lazily 'sl)
  696. (with-eval-after-load 'rst
  697. (defvar rst-mode-map)
  698. (define-key rst-mode-map (kbd "C-m") 'newline-and-indent))
  699. (with-eval-after-load 'jdee
  700. (add-hook 'jdee-mode-hook
  701. (lambda ()
  702. (make-local-variable 'global-mode-string)
  703. (add-to-list 'global-mode-string
  704. mode-line-position))))
  705. ;; Cannot enable error thrown. Why???
  706. ;; https://github.com/m0smith/malabar-mode#Installation
  707. ;; (when (autoload-eval-lazily 'malabar-mode)
  708. ;; (add-to-list 'load-path
  709. ;; (expand-file-name (concat user-emacs-directory "/cedet")))
  710. ;; (safe-require-or-eval 'cedet-devel-load)
  711. ;; (call-after-init 'activate-malabar-mode))
  712. (with-eval-after-load 'make-mode
  713. (defvar makefile-mode-map (make-sparse-keymap))
  714. (define-key makefile-mode-map (kbd "C-m") 'newline-and-indent)
  715. ;; this functions is set in write-file-functions, i cannot find any
  716. ;; good way to remove this.
  717. (fset 'makefile-warn-suspicious-lines 'ignore))
  718. (with-eval-after-load 'verilog-mode
  719. (defvar verilog-mode-map (make-sparse-keymap))
  720. (define-key verilog-mode-map ";" 'self-insert-command))
  721. (setq diff-switches "-u")
  722. (with-eval-after-load 'diff-mode
  723. ;; (when (and (eq major-mode
  724. ;; 'diff-mode)
  725. ;; (not buffer-file-name))
  726. ;; ;; do not pass when major-mode is derived mode of diff-mode
  727. ;; (view-mode 1))
  728. (set-face-attribute 'diff-header nil
  729. :foreground nil
  730. :background nil
  731. :weight 'bold)
  732. (set-face-attribute 'diff-file-header nil
  733. :foreground nil
  734. :background nil
  735. :weight 'bold)
  736. (set-face-foreground 'diff-index "blue")
  737. (set-face-attribute 'diff-hunk-header nil
  738. :foreground "cyan"
  739. :weight 'normal)
  740. (set-face-attribute 'diff-context nil
  741. ;; :foreground "white"
  742. :foreground nil
  743. :weight 'normal)
  744. (set-face-foreground 'diff-removed "red")
  745. (set-face-foreground 'diff-added "green")
  746. (set-face-background 'diff-removed nil)
  747. (set-face-background 'diff-added nil)
  748. (set-face-attribute 'diff-changed nil
  749. :foreground "magenta"
  750. :weight 'normal)
  751. (set-face-attribute 'diff-refine-changed nil
  752. :foreground nil
  753. :background nil
  754. :weight 'bold
  755. :inverse-video t)
  756. ;; Annoying !
  757. ;;(diff-auto-refine-mode)
  758. )
  759. ;; (ffap-bindings)
  760. (set-variable 'browse-url-browser-function
  761. 'eww-browse-url)
  762. (set-variable 'sh-here-document-word "__EOC__")
  763. (when (autoload-eval-lazily 'adoc-mode
  764. nil
  765. (defvar adoc-mode-map (make-sparse-keymap))
  766. (define-key adoc-mode-map (kbd "C-m") 'newline))
  767. (setq auto-mode-alist
  768. `(("\\.adoc\\'" . adoc-mode)
  769. ("\\.asciidoc\\'" . adoc-mode)
  770. ,@auto-mode-alist)))
  771. (with-eval-after-load 'markup-faces
  772. ;; Is this too match ?
  773. (set-face-foreground 'markup-meta-face
  774. "color-245")
  775. (set-face-foreground 'markup-meta-hide-face
  776. "color-245")
  777. )
  778. ;; TODO: check if this is required
  779. (when (autoload-eval-lazily 'groovy-mode nil
  780. (defvar groovy-mode-map (make-sparse-keymap))
  781. (define-key groovy-mode-map "(" 'self-insert-command)
  782. (define-key groovy-mode-map ")" 'self-insert-command)
  783. (define-key groovy-mode-map (kbd "C-m") 'newline-and-indent)
  784. )
  785. (add-to-list 'auto-mode-alist
  786. '("build\\.gradle\\'" . groovy-mode)))
  787. (with-eval-after-load 'yaml-mode
  788. (defvar yaml-mode-map (make-sparse-keymap))
  789. (define-key yaml-mode-map (kbd "C-m") 'newline))
  790. (with-eval-after-load 'html-mode
  791. (defvar html-mode-map (make-sparse-keymap))
  792. (define-key html-mode-map (kbd "C-m") 'reindent-then-newline-and-indent))
  793. (with-eval-after-load 'text-mode
  794. (define-key text-mode-map (kbd "C-m") 'newline))
  795. (with-eval-after-load 'info
  796. (defvar Info-directory-list nil)
  797. (add-to-list 'Info-directory-list
  798. (expand-file-name "~/.brew/share/info")))
  799. (with-eval-after-load 'apropos
  800. (defvar apropos-mode-map (make-sparse-keymap))
  801. (define-key apropos-mode-map "n" 'next-line)
  802. (define-key apropos-mode-map "p" 'previous-line))
  803. ;; `isearch' library does not call `provide' so cannot use with-eval-after-load
  804. ;; (define-key isearch-mode-map
  805. ;; (kbd "C-j") 'isearch-other-control-char)
  806. ;; (define-key isearch-mode-map
  807. ;; (kbd "C-k") 'isearch-other-control-char)
  808. ;; (define-key isearch-mode-map
  809. ;; (kbd "C-h") 'isearch-other-control-char)
  810. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  811. (define-key isearch-mode-map (kbd "M-r")
  812. 'isearch-query-replace-regexp)
  813. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  814. (setq lazy-highlight-cleanup nil)
  815. ;; face for isearch highlighing
  816. (set-face-attribute 'lazy-highlight
  817. nil
  818. :foreground `unspecified
  819. :background `unspecified
  820. :underline t
  821. ;; :weight `bold
  822. )
  823. (add-hook 'outline-mode-hook
  824. (lambda ()
  825. (when (string-match "\\.md\\'" buffer-file-name)
  826. (set (make-local-variable 'outline-regexp) "#+ "))))
  827. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  828. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  829. (when (autoload-eval-lazily 'markdown-mode
  830. '(markdown-mode gfm-mode)
  831. (defvar gfm-mode-map (make-sparse-keymap))
  832. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline))
  833. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  834. (set-variable 'markdown-command (or (executable-find "markdown")
  835. (executable-find "markdown.pl")
  836. ""))
  837. (add-hook 'markdown-mode-hook
  838. (lambda ()
  839. (outline-minor-mode 1)
  840. (flyspell-mode)
  841. (set (make-local-variable 'comment-start) ";")))
  842. )
  843. ;; c-mode
  844. ;; http://www.emacswiki.org/emacs/IndentingC
  845. ;; http://en.wikipedia.org/wiki/Indent_style
  846. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  847. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  848. (with-eval-after-load 'cc-vars
  849. (defvar c-default-style nil)
  850. (add-to-list 'c-default-style
  851. '(c-mode . "k&r"))
  852. (add-to-list 'c-default-style
  853. '(c++-mode . "k&r")))
  854. (autoload-eval-lazily 'js2-mode nil
  855. ;; currently do not use js2-mode
  856. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  857. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  858. ;; (defvar js2-mode-map (make-sparse-keymap))
  859. ;; (define-key js2-mode-map (kbd "C-m") (lambda ()
  860. ;; (interactive)
  861. ;; (js2-enter-key)
  862. ;; (indent-for-tab-command)))
  863. ;; (add-hook (kill-local-variable 'before-save-hook)
  864. ;; 'js2-before-save)
  865. ;; (add-hook 'before-save-hook
  866. ;; 'my-indent-buffer
  867. ;; nil
  868. ;; t)
  869. )
  870. (add-to-list 'interpreter-mode-alist
  871. '("node" . js-mode))
  872. (when (autoload-eval-lazily 'flymake-jslint
  873. '(flymake-jslint-load))
  874. (autoload-eval-lazily 'js nil
  875. (add-hook 'js-mode-hook
  876. 'flymake-jslint-load)))
  877. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  878. (with-eval-after-load 'uniquify
  879. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  880. (setq uniquify-ignore-buffers-re "*[^*]+*")
  881. (setq uniquify-min-dir-content 1))
  882. (with-eval-after-load 'view
  883. (defvar view-mode-map (make-sparse-keymap))
  884. (define-key view-mode-map "j" 'scroll-up-line)
  885. (define-key view-mode-map "k" 'scroll-down-line)
  886. (define-key view-mode-map "v" 'toggle-read-only)
  887. (define-key view-mode-map "q" 'bury-buffer)
  888. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  889. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  890. ;; (define-key view-mode-map
  891. ;; "n" 'nonincremental-repeat-search-forward)
  892. ;; (define-key view-mode-map
  893. ;; "N" 'nonincremental-repeat-search-backward)
  894. (define-key view-mode-map "/" 'isearch-forward-regexp)
  895. (define-key view-mode-map "?" 'isearch-backward-regexp)
  896. (define-key view-mode-map "n" 'isearch-repeat-forward)
  897. (define-key view-mode-map "N" 'isearch-repeat-backward)
  898. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point))
  899. (global-set-key "\M-r" 'view-mode)
  900. ;; (setq view-read-only t)
  901. (with-eval-after-load 'term
  902. (defvar term-raw-map (make-sparse-keymap))
  903. (define-key term-raw-map (kbd "C-x")
  904. (lookup-key (current-global-map)
  905. (kbd "C-x"))))
  906. (add-hook 'term-mode-hook
  907. (lambda ()
  908. ;; Stop current line highlighting
  909. (set (make-local-variable (defvar hl-line-range-function))
  910. (lambda () '(0 . 0)))
  911. (set (make-local-variable 'scroll-margin)
  912. 0)))
  913. (add-hook 'Man-mode-hook
  914. (lambda ()
  915. (view-mode 1)
  916. (setq truncate-lines nil)))
  917. (set-variable 'Man-notify-method (if window-system
  918. 'newframe
  919. 'aggressive))
  920. (set-variable 'woman-cache-filename (expand-file-name (concat user-emacs-directory
  921. "woman_cache.el")))
  922. ;; not work because man.el will be loaded when man called
  923. (defalias 'man 'woman)
  924. (add-to-list 'auto-mode-alist
  925. '("tox\\.ini\\'" . conf-unix-mode))
  926. (when (autoload-eval-lazily 'toml-mode)
  927. (add-to-list 'auto-mode-alist
  928. '("/Pipfile\\'" . toml-mode)))
  929. (when (autoload-eval-lazily 'json-mode)
  930. (add-to-list 'auto-mode-alist
  931. '("/Pipfile\\.lock\\'" . json-mode)))
  932. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  933. ;; buffer switching
  934. (defvar bs-configurations)
  935. (when (autoload-eval-lazily 'bs '(bs-show)
  936. (add-to-list 'bs-configurations
  937. '("specials" "^\\*" nil ".*" nil nil))
  938. (add-to-list 'bs-configurations
  939. '("files-and-specials" "^\\*" buffer-file-name ".*" nil nil))
  940. (defvar bs-mode-map)
  941. (defvar bs-current-configuration)
  942. (define-key bs-mode-map (kbd "t")
  943. ;; TODO: fix toggle feature
  944. (lambda ()
  945. (interactive)
  946. (if (string= "specials"
  947. bs-current-configuration)
  948. (bs-set-configuration "files")
  949. (bs-set-configuration "specials"))
  950. (bs-refresh)
  951. (bs-message-without-log "%s"
  952. (bs--current-config-message))))
  953. ;; (setq bs-configurations (list
  954. ;; '("processes" nil get-buffer-process ".*" nil nil)
  955. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  956. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  957. )
  958. (defalias 'list-buffers 'bs-show)
  959. (set-variable 'bs-default-configuration "files-and-specials")
  960. (set-variable 'bs-default-sort-name "by nothing")
  961. (add-hook 'bs-mode-hook
  962. (lambda ()
  963. (set (make-local-variable 'scroll-margin) 0))))
  964. ;;(iswitchb-mode 1)
  965. (icomplete-mode)
  966. (defun iswitchb-buffer-display-other-window ()
  967. "Do iswitchb in other window."
  968. (interactive)
  969. (let ((iswitchb-default-method 'display))
  970. (call-interactively 'iswitchb-buffer)))
  971. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  972. ;; vc
  973. (set-variable 'vc-handled-backends '())
  974. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  975. ;; recentf-mode
  976. (set-variable 'recentf-save-file (expand-file-name (concat user-emacs-directory
  977. "recentf")))
  978. (set-variable 'recentf-max-menu-items 20)
  979. (set-variable 'recentf-max-saved-items 30)
  980. (set-variable 'recentf-show-file-shortcuts-flag nil)
  981. (when (safe-require-or-eval 'recentf)
  982. (add-to-list 'recentf-exclude
  983. (regexp-quote recentf-save-file))
  984. (add-to-list 'recentf-exclude
  985. (regexp-quote (expand-file-name user-emacs-directory)))
  986. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  987. (remove-hook 'find-file-hook
  988. 'recentf-track-opened-file)
  989. (defun my-recentf-load-track-save-list ()
  990. "Load current recentf list from file, track current visiting file, then save
  991. the list."
  992. (recentf-load-list)
  993. (recentf-track-opened-file)
  994. (recentf-save-list))
  995. (add-hook 'find-file-hook
  996. 'my-recentf-load-track-save-list)
  997. (add-hook 'kill-emacs-hook
  998. 'recentf-load-list)
  999. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1000. ;; (add-hook 'find-file-hook
  1001. ;; (lambda ()
  1002. ;; (recentf-add-file default-directory)))
  1003. (and (autoload-eval-lazily 'recentf-show)
  1004. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1005. (add-hook 'recentf-show-before-listing-hook
  1006. 'recentf-load-list))
  1007. (recentf-mode 1)
  1008. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1009. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1010. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1011. (define-key recentf-dialog-mode-map "n" 'next-line)
  1012. (add-hook 'recentf-dialog-mode-hook
  1013. (lambda ()
  1014. ;; (recentf-save-list)
  1015. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1016. ;; 'my-recentf-cd-and-find-file)
  1017. (cd "~/"))))
  1018. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1019. ;; dired
  1020. (defun my-dired-echo-file-head (arg)
  1021. "Echo head of current file.
  1022. ARG is num to show, or defaults to 7."
  1023. (interactive "P")
  1024. (let ((f (dired-get-filename)))
  1025. (message "%s"
  1026. (with-temp-buffer
  1027. (insert-file-contents f)
  1028. (buffer-substring-no-properties
  1029. (point-min)
  1030. (progn (goto-char (point-min))
  1031. (forward-line (1- (if arg
  1032. (prefix-numeric-value arg)
  1033. 7)))
  1034. (point-at-eol)))))))
  1035. (defun my-dired-diff ()
  1036. "Show diff of marked file and file of current line."
  1037. (interactive)
  1038. (let ((files (dired-get-marked-files nil nil nil t)))
  1039. (if (eq (car files)
  1040. t)
  1041. (diff (cadr files) (dired-get-filename))
  1042. (message "One file must be marked!"))))
  1043. (defun dired-get-file-info ()
  1044. "Print information of current line file."
  1045. (interactive)
  1046. (let ((f (shell-quote-argument (dired-get-filename t))))
  1047. (if (file-directory-p f)
  1048. (progn
  1049. (message "Calculating disk usage...")
  1050. (shell-command (concat "du -hsD "
  1051. f)))
  1052. (shell-command (concat "file "
  1053. f)))))
  1054. (defun my-dired-scroll-up ()
  1055. "Scroll up."
  1056. (interactive)
  1057. (my-dired-previous-line (- (window-height) 1)))
  1058. (defun my-dired-scroll-down ()
  1059. "Scroll down."
  1060. (interactive)
  1061. (my-dired-next-line (- (window-height) 1)))
  1062. ;; (defun my-dired-forward-line (arg)
  1063. ;; ""
  1064. ;; (interactive "p"))
  1065. (defun my-dired-previous-line (arg)
  1066. "Move ARG lines up."
  1067. (interactive "p")
  1068. (if (> arg 0)
  1069. (progn
  1070. (if (eq (line-number-at-pos)
  1071. 1)
  1072. (goto-char (point-max))
  1073. (forward-line -1))
  1074. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1075. (dired-get-subdir))
  1076. (- arg 1)
  1077. arg)))
  1078. (dired-move-to-filename)))
  1079. (defun my-dired-next-line (arg)
  1080. "Move ARG lines down."
  1081. (interactive "p")
  1082. (if (> arg 0)
  1083. (progn
  1084. (if (eq (point)
  1085. (point-max))
  1086. (goto-char (point-min))
  1087. (forward-line 1))
  1088. (my-dired-next-line (if (or (dired-get-filename nil t)
  1089. (dired-get-subdir))
  1090. (- arg 1)
  1091. arg)))
  1092. (dired-move-to-filename)))
  1093. (defun my-dired-tramp-open (host)
  1094. "Open HOST home directory."
  1095. (interactive "sHostname: ")
  1096. (find-file (format "/scp:%s:" host)))
  1097. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1098. (if (eq window-system 'mac)
  1099. (setq dired-listing-switches "-lhF")
  1100. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1101. )
  1102. (setq dired-listing-switches "-lhF")
  1103. (put 'dired-find-alternate-file 'disabled nil)
  1104. ;; when using dired-find-alternate-file
  1105. ;; reuse current dired buffer for the file to open
  1106. (set-variable 'dired-ls-F-marks-symlinks t)
  1107. (set-variable 'ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1108. (set-variable 'ls-lisp-dirs-first t)
  1109. (set-variable 'ls-lisp-use-localized-time-format t)
  1110. (set-variable 'ls-lisp-format-time-list
  1111. '("%Y-%m-%d %H:%M"
  1112. "%Y-%m-%d "))
  1113. (set-variable 'dired-dwim-target t)
  1114. (set-variable 'dired-isearch-filenames t)
  1115. (set-variable 'dired-hide-details-hide-symlink-targets nil)
  1116. (set-variable 'dired-hide-details-hide-information-lines nil)
  1117. (set-variable 'dired-deletion-confirmer 'y-or-n-p)
  1118. (set-variable 'dired-recursive-deletes 'always)
  1119. ;; (add-hook 'dired-after-readin-hook
  1120. ;; 'my-replace-nasi-none)
  1121. (with-eval-after-load 'dired
  1122. (safe-require-or-eval 'ls-lisp)
  1123. (defvar dired-mode-map (make-sparse-keymap))
  1124. (define-key dired-mode-map "i" 'dired-get-file-info)
  1125. (define-key dired-mode-map "f" 'find-file)
  1126. (define-key dired-mode-map "!" 'shell-command)
  1127. (define-key dired-mode-map "&" 'async-shell-command)
  1128. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  1129. (define-key dired-mode-map "=" 'my-dired-diff)
  1130. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  1131. (define-key dired-mode-map "b" 'gtkbm)
  1132. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  1133. (define-key dired-mode-map (kbd "TAB") 'other-window)
  1134. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  1135. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  1136. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  1137. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  1138. (substitute-key-definition 'dired-next-line
  1139. 'my-dired-next-line
  1140. dired-mode-map)
  1141. (substitute-key-definition 'dired-previous-line
  1142. 'my-dired-previous-line
  1143. dired-mode-map)
  1144. ;; (define-key dired-mode-map (kbd "C-p") 'my-dired-previous-line)
  1145. ;; (define-key dired-mode-map (kbd "p") 'my-dired-previous-line)
  1146. ;; (define-key dired-mode-map (kbd "C-n") 'my-dired-next-line)
  1147. ;; (define-key dired-mode-map (kbd "n") 'my-dired-next-line)
  1148. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  1149. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  1150. (define-key dired-mode-map (kbd "ESC p") 'my-dired-scroll-up)
  1151. (define-key dired-mode-map (kbd "ESC n") 'my-dired-scroll-down)
  1152. (add-hook 'dired-mode-hook
  1153. (lambda ()
  1154. (when (fboundp 'dired-hide-details-mode)
  1155. (dired-hide-details-mode t)
  1156. (local-set-key "l" 'dired-hide-details-mode))
  1157. (let ((file "._Icon\015"))
  1158. (when nil
  1159. '(file-readable-p file)
  1160. (delete-file file)))))
  1161. (when (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack-pack))
  1162. (with-eval-after-load 'dired
  1163. (define-key dired-mode-map "P" 'pack-dired-dwim)))
  1164. (when (autoload-eval-lazily 'dired-list-all-mode)
  1165. (setq dired-listing-switches "-lhF")
  1166. (with-eval-after-load 'dired
  1167. (define-key dired-mode-map "a" 'dired-list-all-mode))))
  1168. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1169. ;; misc funcs
  1170. (defalias 'qcalc 'quick-calc)
  1171. (defun memo (&optional dir)
  1172. "Open memo.txt in DIR."
  1173. (interactive)
  1174. (pop-to-buffer (find-file-noselect (concat (if dir
  1175. (file-name-as-directory dir)
  1176. "")
  1177. "memo.txt"))))
  1178. (defvar my-rgrep-alist
  1179. `(
  1180. ;; git grep
  1181. ("gitgrep"
  1182. (eq 0
  1183. (shell-command "git rev-parse --git-dir"))
  1184. "git --no-pager -c color.grep=false grep -nH -e ")
  1185. ;; ripgrep
  1186. ("rg"
  1187. (executable-find "rg")
  1188. "rg --no-heading --color=never --smart-case ")
  1189. ;; sift
  1190. ("sift"
  1191. (executable-find "sift")
  1192. ("sift --no-color --binary-skip --filename --line-number --git --smart-case "))
  1193. ;; the silver searcher
  1194. ("ag"
  1195. (executable-find "ag")
  1196. "ag --nocolor --nogroup --nopager --filename ")
  1197. ;; ack
  1198. ("ack"
  1199. (executable-find "ack")
  1200. "ack --nocolor --nogroup --nopager --with-filename ")
  1201. ;; gnu global
  1202. ("global"
  1203. (and (require 'gtags nil t)
  1204. (executable-find "global")
  1205. (gtags-get-rootpath))
  1206. "global --result grep ")
  1207. ;; grep
  1208. ("grep"
  1209. t
  1210. ,(concat "find . "
  1211. "-path '*/.git' -prune -o "
  1212. "-path '*/.svn' -prune -o "
  1213. "-type f -print0 | "
  1214. "xargs -0 grep -nH -e "))
  1215. )
  1216. "Alist of rgrep command.
  1217. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  1218. condition to choose COMMAND when evaluated.")
  1219. (defvar my-rgrep-default nil
  1220. "Default command name for my-rgrep.")
  1221. (defun my-rgrep-grep-command (&optional name alist)
  1222. "Return recursive grep command for current directory or nil.
  1223. If NAME is given, use that without testing.
  1224. Commands are searched from ALIST."
  1225. (if alist
  1226. (if name
  1227. ;; if name is given search that from alist and return the command
  1228. (nth 2 (assoc name
  1229. alist))
  1230. ;; if name is not given try test in 1th elem
  1231. (let ((car (car alist))
  1232. (cdr (cdr alist)))
  1233. (if (eval (nth 1 car))
  1234. ;; if the condition is true return the command
  1235. (nth 2 car)
  1236. ;; try next one
  1237. (and cdr
  1238. (my-rgrep-grep-command name cdr)))))
  1239. ;; if alist is not given set default value
  1240. (my-rgrep-grep-command name my-rgrep-alist)))
  1241. (defun my-rgrep (command-args)
  1242. "My recursive grep. Run COMMAND-ARGS."
  1243. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  1244. nil)))
  1245. (if cmd
  1246. (list (read-shell-command "grep command: "
  1247. cmd
  1248. 'grep-find-history))
  1249. (error "My-Rgrep: Command for rgrep not found")
  1250. )))
  1251. (compilation-start command-args
  1252. 'grep-mode))
  1253. ;; (defun my-rgrep-symbol-at-point (command-args)
  1254. ;; "My recursive grep. Run COMMAND-ARGS."
  1255. ;; (interactive (list (read-shell-command "grep command: "
  1256. ;; (concat (my-rgrep-grep-command)
  1257. ;; " "
  1258. ;; (thing-at-point 'symbol))
  1259. ;; 'grep-find-history)))
  1260. ;; (compilation-start command-args
  1261. ;; 'grep-mode))
  1262. (defmacro define-my-rgrep (name)
  1263. "Define rgrep for NAME."
  1264. `(defun ,(intern (concat "my-rgrep-"
  1265. name)) ()
  1266. ,(format "My recursive grep by %s."
  1267. name)
  1268. (interactive)
  1269. (let ((my-rgrep-default ,name))
  1270. (if (called-interactively-p 'any)
  1271. (call-interactively 'my-rgrep)
  1272. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  1273. )
  1274. (define-my-rgrep "ack")
  1275. (define-my-rgrep "ag")
  1276. (define-my-rgrep "rg")
  1277. (define-my-rgrep "sift")
  1278. (define-my-rgrep "gitgrep")
  1279. (define-my-rgrep "grep")
  1280. (define-my-rgrep "global")
  1281. (define-key ctl-x-map "s" 'my-rgrep)
  1282. (defalias 'make 'compile)
  1283. (define-key ctl-x-map "c" 'compile)
  1284. ;;;;;;;;;;;;;;;;;;;;;;;
  1285. ;; adoc-simple-mode
  1286. (when (safe-require-or-eval 'adoc-mode)
  1287. (defvar adoc-simple-font-lock-keywords
  1288. nil)
  1289. (define-derived-mode adoc-simple-mode adoc-mode
  1290. "Adoc-Simple"
  1291. "Major mode for editing AsciiDoc text files.
  1292. This mode is a simplified version of `adoc-mode'."
  1293. '(set (make-local-variable 'font-lock-defaults)
  1294. '(adoc-simple-font-lock-keywords
  1295. nil nil nil nil
  1296. (font-lock-multiline . t)
  1297. (font-lock-mark-block-function . adoc-font-lock-mark-block-function))))
  1298. (add-to-list 'auto-mode-alist
  1299. '("\\.adoc\\'" . adoc-simple-mode)))
  1300. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1301. ;; awk-preview
  1302. (defgroup awk-preview nil
  1303. "Awk previewer."
  1304. :tag "Awk Preview"
  1305. :prefix "awk-preview-"
  1306. :group 'tools)
  1307. (defcustom awk-preview-program
  1308. (or (executable-find "gawk")
  1309. (executable-find "awk")
  1310. "awk")
  1311. "Awk program to execute."
  1312. :type 'string
  1313. :group 'awk-preview)
  1314. (defcustom awk-preview-switches
  1315. '("--sandbox" "--" "{print}")
  1316. "String of awk options appended when running awk preview."
  1317. :type '(repeat string)
  1318. :group 'awk-preview)
  1319. (defun awk-preview--invoke (start end output)
  1320. "Execute awk process and get result."
  1321. (let ((proc (apply 'start-process
  1322. "awk-preview"
  1323. output
  1324. awk-preview-program
  1325. awk-preview-switches)))
  1326. (message "%S" proc)
  1327. (process-send-region proc start end)
  1328. (process-send-eof proc)
  1329. (accept-process-output proc)
  1330. )
  1331. output)
  1332. (defun awk-preview (start end)
  1333. "Run awk and preview result."
  1334. (interactive "r")
  1335. (let ((output (with-current-buffer (get-buffer-create "*awk-preview output*")
  1336. (erase-buffer)
  1337. (current-buffer))))
  1338. (awk-preview--invoke start end output)
  1339. (message "awk-preview: %s" (with-current-buffer output
  1340. (buffer-substring-no-properties (point-min) (point-max))))
  1341. ))
  1342. (defun my-file-head (filename &optional n)
  1343. "Return list of first N lines of file FILENAME."
  1344. (let ((num (or n 10))
  1345. (size 100)
  1346. (beg 0)
  1347. (end 0)
  1348. (result '()))
  1349. (with-temp-buffer
  1350. (while (<= (count-lines (point-min)
  1351. (point-max))
  1352. num)
  1353. (setq end (+ beg size))
  1354. (insert-file-contents filename
  1355. nil
  1356. beg
  1357. end)
  1358. (setq beg end))
  1359. (goto-char (point-min))
  1360. (while (< (length result) num)
  1361. (let ((start (point)))
  1362. (forward-line 1)
  1363. (setq result
  1364. `(,@result ,(buffer-substring-no-properties start
  1365. (point))))))
  1366. result)))
  1367. ;; (apply 'concat (car (my-file-head "./emacs.el"))
  1368. ;; Local Variables:
  1369. ;; flycheck-disabled-checkers: (emacs-lisp-checkdoc)
  1370. ;; flycheck-checker: emacs-lisp
  1371. ;; End:
  1372. ;;; emancs.el ends here