Du kan inte välja fler än 25 ämnen Ämnen måste starta med en bokstav eller siffra, kan innehålla bindestreck ('-') och vara max 35 tecken långa.
 
 
 
 
 
 

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