Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 
 
 
 

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