Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

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