選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

emacs.el 49 KiB

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