Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 
 
 
 
 

2745 rindas
95 KiB

  1. ;;; emacs.el --- 10sr emacs initialization
  2. ;;; Commentary:
  3. ;;; Code:
  4. ;; SETUP_LOAD: (let ((file "DOTFILES_DIR/emacs.el"))
  5. ;; SETUP_LOAD: (and (file-readable-p file)
  6. ;; SETUP_LOAD: (load-file file)))
  7. ;; make directories
  8. (unless (file-directory-p (expand-file-name user-emacs-directory))
  9. (make-directory (expand-file-name user-emacs-directory)))
  10. (let ((d (expand-file-name (concat user-emacs-directory
  11. "lisp"))))
  12. (unless (file-directory-p d)
  13. (make-directory d))
  14. (add-to-list 'load-path d))
  15. (eval-when-compile
  16. ;; TODO: Use cl-lib intead
  17. (require 'cl nil t))
  18. ;; (add-hook 'after-change-major-mode-hook
  19. ;; (lambda ()
  20. ;; (message "cmm: %S %s"
  21. ;; major-mode
  22. ;; buffer-file-name)))
  23. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  24. ;; Some macros for internals
  25. (defun call-after-init (func)
  26. "If `after-init-hook' has been run, call FUNC immediately.
  27. Otherwize hook it."
  28. (if after-init-time
  29. (funcall func)
  30. (add-hook 'after-init-hook
  31. func)))
  32. (defmacro defvar-set (symbol value &optional docstring)
  33. "Define SYMBOL as a variable and set to VALUE.
  34. Variable will be defined with DOCSTRING if given, otherwise do not set even
  35. VALUE when defining SYMBOL."
  36. `(set (if ,docstring
  37. (defvar ,symbol
  38. nil
  39. ,docstring)
  40. (defvar ,symbol))
  41. ,value))
  42. (defmacro safe-require-or-eval (feature)
  43. "Require FEATURE if available.
  44. At compile time the feature will be loaded immediately."
  45. `(eval-and-compile
  46. (require ,feature nil t)))
  47. (defmacro autoload-eval-lazily (feature &optional functions &rest body)
  48. "Define autoloading FEATURE that defines FUNCTIONS.
  49. FEATURE is a symbol. FUNCTIONS is a list of symbols. If FUNCTIONS is nil,
  50. the function same as FEATURE is defined as autoloaded function. BODY is passed
  51. to `eval-after-load'.
  52. After this macro is expanded, this returns the path to library if FEATURE
  53. found, otherwise returns nil."
  54. (let* ((libname (symbol-name (eval feature)))
  55. (libpath (locate-library libname)))
  56. (and libpath
  57. `(progn
  58. ,@(mapcar (lambda (f)
  59. (unless (fboundp f)
  60. `(progn
  61. (message "Autoloaded function `%S' defined (%s)"
  62. (quote ,f)
  63. ,libpath)
  64. (autoload (quote ,f)
  65. ,libname
  66. ,(concat "Autoloaded function defined in \""
  67. libpath
  68. "\".")
  69. t))))
  70. (or (eval functions)
  71. `(,(eval feature))))
  72. (eval-after-load ,feature
  73. (quote (progn
  74. ,@body)))
  75. (locate-library ,libname)))))
  76. (put 'autoload-eval-lazily 'lisp-indent-function 2)
  77. (when (autoload-eval-lazily 'tetris nil
  78. (message "Tetris loaded!"))
  79. (message "Tetris found!"))
  80. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  81. ;; download library from web
  82. (defvar fetch-library-enabled-p t
  83. "Set nil to skip downloading with `fetch-library'.")
  84. (defun fetch-library (url &optional byte-compile-p force-download-p)
  85. "Download a library from URL and locate it in \"~/emacs.d/lisp/\".
  86. Return nil if library unfound and failed to download,
  87. otherwise the path where the library installed.
  88. If BYTE-COMPILE-P is t byte compile the file after downloading.
  89. If FORCE-DOWNLOAD-P it t ignore exisiting library and always download.
  90. This function also checks the value of `fetch-library-enabled-p' and do not
  91. fetch libraries if this value is nil. In this case all arguments (including
  92. FORCE-DOWNLOAD-P) will be ignored."
  93. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  94. (lib (file-name-sans-extension (file-name-nondirectory url)))
  95. (lpath (concat dir lib ".el"))
  96. (locate-p (locate-library lib)))
  97. (if (and fetch-library-enabled-p
  98. (or force-download-p
  99. (not locate-p)))
  100. (if (progn (message "Downloading %s..."
  101. url)
  102. (download-file url
  103. lpath
  104. t))
  105. (progn (message "Downloading %s...done"
  106. url)
  107. (when (and byte-compile-p
  108. (require 'bytecomp nil t))
  109. (and (file-exists-p (byte-compile-dest-file lpath))
  110. (delete-file (byte-compile-dest-file lpath)))
  111. (message "Byte-compiling %s..."
  112. lpath)
  113. (byte-compile-file lpath)
  114. (message "Byte-compiling %s...done"
  115. lpath)))
  116. (progn (and (file-writable-p lpath)
  117. (delete-file lpath))
  118. (message "Downloading %s...failed"
  119. url))))
  120. (locate-library lib)))
  121. ;; If EMACS_EL_DRY_RUN is set and it is not an empty string, fetch-library
  122. ;; does not actually fetch library.
  123. (let ((dryrun (getenv "EMACS_EL_DRY_RUN")))
  124. (when (and dryrun
  125. (< 0
  126. (length dryrun)))
  127. (setq fetch-library-enabled-p
  128. nil)
  129. (message "EMACS_EL_DRY_RUN is set. Skip fetching libraries.")))
  130. (defun download-file (url path &optional ok-if-already-exists)
  131. "Download file from URL and output to PATH.
  132. IF OK-IF-ALREADY-EXISTS is true force download."
  133. (let ((curl (executable-find "curl"))
  134. (wget (executable-find "wget")))
  135. (cond (wget
  136. (if (and (not ok-if-already-exists)
  137. (file-exists-p path))
  138. nil
  139. (and (eq 0
  140. (call-process wget
  141. nil
  142. nil
  143. nil
  144. "-O"
  145. path
  146. url
  147. ))
  148. path)))
  149. (curl
  150. (if (and (not ok-if-already-exists)
  151. (file-exists-p path))
  152. nil
  153. (and (eq 0
  154. (call-process curl
  155. nil
  156. nil
  157. nil
  158. "--output"
  159. path
  160. "-L"
  161. url
  162. ))
  163. path)))
  164. (t
  165. (ignore-errors
  166. (require 'url)
  167. (url-copy-file url
  168. path
  169. ok-if-already-exists)
  170. path)))))
  171. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  172. ;; package
  173. (defvar-set my-package-list
  174. '(
  175. markdown-mode
  176. yaml-mode
  177. gnuplot-mode
  178. erlang
  179. js2-mode
  180. git-commit-mode
  181. gitignore-mode
  182. ;; ack
  183. color-moccur
  184. gtags
  185. flycheck
  186. ;; is flymake installs are required?
  187. ;;flymake-jshint
  188. ;;flymake-python-pyflakes
  189. xclip
  190. foreign-regexp
  191. multi-term
  192. dirtree
  193. term-run
  194. editorconfig
  195. git-ps1-mode
  196. restart-emacs
  197. scala-mode2
  198. ensime
  199. editorconfig
  200. editorconfig-core
  201. )
  202. "Package list just for me.")
  203. (when (safe-require-or-eval 'package)
  204. ;; (add-to-list 'package-archives
  205. ;; '("ELPA" . "http://tromey.com/elpa/"))
  206. (add-to-list 'package-archives
  207. '("melpa" . "http://melpa.milkbox.net/packages/")
  208. t)
  209. (add-to-list 'package-archives
  210. '("marmalade" . "http://marmalade-repo.org/packages/"))
  211. (package-initialize)
  212. (defun my-auto-install-package ()
  213. "Install packages semi-automatically."
  214. (interactive)
  215. (package-refresh-contents)
  216. (mapc (lambda (pkg)
  217. (or (package-installed-p pkg)
  218. (locate-library (symbol-name pkg))
  219. (package-install pkg)))
  220. my-package-list))
  221. )
  222. ;; (lazy-load-eval 'sudoku)
  223. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  224. ;; my-idle-hook
  225. (defvar my-idle-hook nil
  226. "Hook run when idle for several secs.")
  227. (defvar my-idle-hook-sec 5
  228. "Second to run `my-idle-hook'.")
  229. (run-with-idle-timer my-idle-hook-sec
  230. t
  231. (lambda ()
  232. (run-hooks 'my-idle-hook)))
  233. ;; (add-hook 'my-idle-hook
  234. ;; (lambda ()
  235. ;; (message "idle hook message")))
  236. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  237. ;; start and quit
  238. (setq inhibit-startup-message t)
  239. (setq confirm-kill-emacs 'y-or-n-p)
  240. (setq gc-cons-threshold (* 1024 1024 4))
  241. (when window-system
  242. (add-to-list 'default-frame-alist '(cursor-type . box))
  243. (add-to-list 'default-frame-alist '(background-color . "white"))
  244. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  245. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100)))
  246. ;; does not work?
  247. )
  248. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  249. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  250. (and (fboundp 'tool-bar-mode)
  251. (tool-bar-mode 0))
  252. (and (fboundp 'set-scroll-bar-mode)
  253. (set-scroll-bar-mode nil))
  254. (add-hook 'kill-emacs-hook
  255. ;; load init file when terminating emacs to ensure file is not broken
  256. 'reload-init-file)
  257. (defun my-force-kill-emacs ()
  258. "My force kill Emacs."
  259. (interactive)
  260. (let ((kill-emacs-hook nil))
  261. (kill-emacs)))
  262. (call-after-init
  263. (lambda ()
  264. (message "%s %s" invocation-name emacs-version)
  265. (message "%s was taken to initialize emacs." (emacs-init-time))
  266. (switch-to-buffer "*Messages*")))
  267. (cd ".") ; when using windows use / instead of \ in `default-directory'
  268. ;; locale
  269. (set-language-environment "Japanese")
  270. (set-default-coding-systems 'utf-8-unix)
  271. (prefer-coding-system 'utf-8-unix)
  272. (setq system-time-locale "C")
  273. ;; my prefix map
  274. (defvar my-prefix-map nil
  275. "My prefix map.")
  276. (define-prefix-command 'my-prefix-map)
  277. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  278. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  279. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  280. ;; (comint-show-maximum-output)
  281. ;; kill scratch
  282. (call-after-init (lambda ()
  283. (let ((buf (get-buffer "*scratch*")))
  284. (when buf
  285. (kill-buffer buf)))))
  286. ;; modifier keys
  287. ;; (setq mac-option-modifier 'control)
  288. ;; display
  289. (setq redisplay-dont-pause t)
  290. (setq visible-bell t)
  291. (setq ring-bell-function 'ignore)
  292. (mouse-avoidance-mode 'banish)
  293. (and window-system
  294. (fetch-library
  295. "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el"
  296. t)
  297. (safe-require-or-eval 'save-window-size))
  298. (defun reload-init-file ()
  299. "Reload Emacs init file."
  300. (interactive)
  301. (when (and user-init-file
  302. (file-readable-p user-init-file))
  303. (load-file user-init-file)))
  304. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  305. ;; for windows
  306. (defun start-ckw-bash ()
  307. "Start ckw in windows."
  308. (interactive)
  309. (start-process
  310. "ckw_bash"
  311. nil
  312. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe"))
  313. ;; command seems to have to be in c drive
  314. (defun my-w32-add-export-path (&rest args)
  315. "Add pathes ARGS for windows."
  316. (mapc (lambda (path)
  317. (add-to-list 'exec-path (expand-file-name path)))
  318. (reverse args))
  319. (setenv "PATH"
  320. (mapconcat 'convert-standard-filename
  321. exec-path
  322. ";")))
  323. (when (eq system-type 'windows-nt)
  324. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  325. ;; (setq python-python-command "c:/Python26/python.exe")
  326. ;; (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  327. (my-w32-add-export-path "c:/Windows/system"
  328. "c:/Windows/System32"
  329. "c:/Program Files/Git/bin"
  330. "c:/MinGW/bin"
  331. "c:/MinGW/mingw32/bin"
  332. (expand-file-name "~/.local/bin")
  333. (expand-file-name "~/dbx/apps/bin"))
  334. (when window-system
  335. (defvar-set w32-enable-synthesized-fonts t))
  336. (defvar-set w32-apps-modifier 'meta)
  337. (setq file-name-coding-system 'sjis))
  338. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  339. ;; global keys
  340. (global-set-key (kbd "<up>") 'scroll-down-line)
  341. (global-set-key (kbd "<down>") 'scroll-up-line)
  342. (global-set-key (kbd "<left>") 'scroll-down)
  343. (global-set-key (kbd "<right>") 'scroll-up)
  344. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  345. (global-set-key (kbd "C-\\") help-map)
  346. (define-key ctl-x-map (kbd "DEL") help-map)
  347. (define-key ctl-x-map (kbd "C-h") help-map)
  348. (define-key help-map "a" 'apropos)
  349. ;; disable annoying keys
  350. (global-set-key [prior] 'ignore)
  351. (global-set-key (kbd "<next>") 'ignore)
  352. (global-set-key [menu] 'ignore)
  353. (global-set-key [down-mouse-1] 'ignore)
  354. (global-set-key [down-mouse-2] 'ignore)
  355. (global-set-key [down-mouse-3] 'ignore)
  356. (global-set-key [mouse-1] 'ignore)
  357. (global-set-key [mouse-2] 'ignore)
  358. (global-set-key [mouse-3] 'ignore)
  359. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  360. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  361. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  362. ;; title and mode-line
  363. (when (and (fetch-library
  364. "https://raw.github.com/10sr/emacs-lisp/master/terminal-title.el"
  365. t)
  366. (safe-require-or-eval 'terminal-title))
  367. ;; if TERM is not screen use default value
  368. (if (getenv "TMUX")
  369. ;; if use tmux locally just basename of current dir
  370. (defvar-set terminal-title-format
  371. '((file-name-nondirectory (directory-file-name
  372. default-directory))))
  373. (if (and (let ((tty-type (frame-parameter nil
  374. 'tty-type)))
  375. (and tty-type
  376. (equal (car (split-string tty-type
  377. "-"))
  378. "screen")))
  379. (not (getenv "SSH_CONNECTION")))
  380. (defvar-set terminal-title-format
  381. '((file-name-nondirectory (directory-file-name
  382. default-directory))))
  383. ;; seems that TMUX is used locally and ssh to remote host
  384. (defvar-set terminal-title-format
  385. `("em:"
  386. ,user-login-name
  387. "@"
  388. ,(car (split-string system-name
  389. "\\."))
  390. ":"
  391. default-directory))
  392. )
  393. )
  394. (terminal-title-mode))
  395. (setq eol-mnemonic-dos "\\r\\n")
  396. (setq eol-mnemonic-mac "\\r")
  397. (setq eol-mnemonic-unix "\\n")
  398. (which-function-mode 0)
  399. (line-number-mode 0)
  400. (column-number-mode 0)
  401. (size-indication-mode 0)
  402. (setq mode-line-position
  403. '(:eval (format "L%%l/%d,C%%c"
  404. (count-lines (point-max)
  405. (point-min)))))
  406. (when (require 'git-ps1-mode nil t)
  407. (git-ps1-mode))
  408. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  409. ;; display date
  410. (call-after-init (lambda ()
  411. (when display-time-mode
  412. (display-time-update))))
  413. (when (safe-require-or-eval 'time)
  414. (setq display-time-interval 29)
  415. (setq display-time-day-and-date t)
  416. (setq display-time-format "%a, %d %b %Y %T")
  417. (if window-system
  418. (display-time-mode 0)
  419. (display-time-mode 1)))
  420. ;; ;; current directory
  421. ;; (let ((ls (member 'mode-line-buffer-identification
  422. ;; mode-line-format)))
  423. ;; (setcdr ls
  424. ;; (cons '(:eval (concat " ("
  425. ;; (abbreviate-file-name default-directory)
  426. ;; ")"))
  427. ;; (cdr ls))))
  428. ;; ;; display last modified time
  429. ;; (let ((ls (member 'mode-line-buffer-identification
  430. ;; mode-line-format)))
  431. ;; (setcdr ls
  432. ;; (cons '(:eval (concat " "
  433. ;; my-buffer-file-last-modified-time))
  434. ;; (cdr ls))))
  435. (defun buffer-list-not-start-with-space ()
  436. "Return a list of buffers that not start with whitespaces."
  437. (let ((bl (buffer-list))
  438. b nbl)
  439. (while bl
  440. (setq b (pop bl))
  441. (unless (string-equal " "
  442. (substring (buffer-name b)
  443. 0
  444. 1))
  445. (add-to-list 'nbl b)))
  446. nbl))
  447. ;; http://www.masteringemacs.org/articles/2012/09/10/hiding-replacing-modeline-strings/
  448. ;; (add-to-list 'minor-mode-alist
  449. ;; '(global-whitespace-mode ""))
  450. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  451. ;; system info
  452. (defun my-message-current-info ()
  453. "Echo current login name, hostname and directory."
  454. (interactive)
  455. (message "%s@%s:%s"
  456. user-login-name
  457. system-name
  458. (abbreviate-file-name default-directory)))
  459. ;; (run-with-idle-timer 3
  460. ;; t
  461. ;; 'my-message-current-info)
  462. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  463. ;; minibuffer
  464. (setq insert-default-directory t)
  465. (setq completion-ignore-case t
  466. read-file-name-completion-ignore-case t
  467. read-buffer-completion-ignore-case t)
  468. (setq resize-mini-windows t)
  469. (temp-buffer-resize-mode 1)
  470. (savehist-mode 1)
  471. (fset 'yes-or-no-p 'y-or-n-p)
  472. ;; complete symbol when `eval'
  473. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol)
  474. (define-key minibuffer-local-map (kbd "C-u")
  475. (lambda () (interactive) (delete-region (point-at-bol) (point))))
  476. ;; I dont know these bindings are good
  477. (define-key minibuffer-local-map (kbd "C-p") (kbd "ESC p"))
  478. (define-key minibuffer-local-map (kbd "C-n") (kbd "ESC n"))
  479. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  480. ;; letters, font-lock mode and fonts
  481. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  482. ;; (set-window-margins (selected-window) 1 1)
  483. (and (or (eq system-type 'Darwin)
  484. (eq system-type 'darwin))
  485. (fboundp 'mac-set-input-method-parameter)
  486. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  487. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  488. (when (and (boundp 'input-method-activate-hook) ; i dont know this is correct
  489. (boundp 'input-method-inactivate-hook))
  490. (add-hook 'input-method-activate-hook
  491. (lambda () (set-cursor-color "red")))
  492. (add-hook 'input-method-inactivate-hook
  493. (lambda () (set-cursor-color "black"))))
  494. (when (safe-require-or-eval 'paren)
  495. (show-paren-mode 1)
  496. (setq show-paren-delay 0.5
  497. show-paren-style 'parenthesis) ; mixed is hard to read
  498. ;; (set-face-background 'show-paren-match
  499. ;; "black")
  500. ;; ;; (face-foreground 'default))
  501. ;; (set-face-foreground 'show-paren-match
  502. ;; "white")
  503. ;; (set-face-inverse-video-p 'show-paren-match
  504. ;; t)
  505. )
  506. (transient-mark-mode 1)
  507. (global-font-lock-mode 1)
  508. (setq font-lock-global-modes
  509. '(not
  510. help-mode
  511. eshell-mode
  512. term-mode
  513. Man-mode))
  514. ;; (standard-display-ascii ?\n "$\n")
  515. (defvar my-eol-face
  516. '(("\n" . (0 font-lock-comment-face t nil)))
  517. )
  518. (defvar my-tab-face
  519. '(("\t" . '(0 highlight t nil))))
  520. (defvar my-jspace-face
  521. '(("\u3000" . '(0 highlight t nil))))
  522. (add-hook 'font-lock-mode-hook
  523. (lambda ()
  524. ;; (font-lock-add-keywords nil my-eol-face)
  525. (font-lock-add-keywords nil my-jspace-face)
  526. ))
  527. (when (safe-require-or-eval 'whitespace)
  528. (add-to-list 'whitespace-display-mappings ; not work
  529. `(tab-mark ?\t ,(vconcat "^I\t")))
  530. (add-to-list 'whitespace-display-mappings
  531. `(newline-mark ?\n ,(vconcat "$\n")))
  532. (setq whitespace-style '(face
  533. trailing ; trailing blanks
  534. newline ; newlines
  535. newline-mark ; use display table for newline
  536. ;; tab-mark
  537. empty ; empty lines at beg or end of buffer
  538. lines-tail ; lines over 80
  539. ))
  540. ;; (setq whitespace-newline 'font-lock-comment-face)
  541. (global-whitespace-mode t)
  542. (if (eq (display-color-cells)
  543. 256)
  544. (set-face-foreground 'whitespace-newline "brightblack")
  545. ;; (progn
  546. ;; (set-face-bold-p 'whitespace-newline
  547. ;; t))
  548. ))
  549. (and nil
  550. (fetch-library
  551. "http://www.emacswiki.org/emacs/download/fill-column-indicator.el"
  552. t)
  553. (safe-require-or-eval 'fill-column-indicator)
  554. (setq fill-column-indicator))
  555. ;; highlight current line
  556. ;; http://wiki.riywo.com/index.php?Meadow
  557. (defface my-hl-line
  558. '((((min-colors 256)
  559. (background dark))
  560. (:background "color-234"))
  561. (((min-colors 256)
  562. (background light))
  563. (:background "color-234"))
  564. (t
  565. (:underline "black")))
  566. "*Face used by hl-line.")
  567. (defvar-set hl-line-face 'my-hl-line) ;; (setq hl-line-face nil)
  568. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  569. (defvar-set hl-line-global-modes
  570. '(not
  571. term-mode))
  572. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  573. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  574. ;; fonts
  575. (defun my-set-ascii-and-jp-font (list)
  576. "Set font configuration to LIST."
  577. (let ((fspec1 (if (> emacs-major-version 22)
  578. ;; font spec is available in emacs23 and later
  579. (font-spec :family (nth 2 list) :size (nth 3 list))
  580. (cons (nth 2 list) "jisx0208.*")))
  581. (fspec2 (if (> emacs-major-version 22)
  582. (font-spec :family (nth 2 list) :size (nth 3 list))
  583. (cons (nth 2 list) "jisx0201.*"))))
  584. (set-face-attribute 'default nil
  585. :family (nth 0 list)
  586. :height (nth 1 list))
  587. (set-fontset-font "fontset-default"
  588. 'japanese-jisx0208
  589. fspec1)
  590. (set-fontset-font "fontset-default"
  591. 'katakana-jisx0201
  592. fspec2)))
  593. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 90 "takaogothic" 13))
  594. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "takaogothic" 14))
  595. ;; (my-set-ascii-and-jp-font '("dejavu sans mono" 100 "ms gothic" 14))
  596. ;; (my-set-ascii-and-jp-font '("monaco" 75 "takaogothic" 11))
  597. ;; (my-set-ascii-and-jp-font '("monaco" 90 "takaogothic" 13))
  598. ;; (my-set-ascii-and-jp-font '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  599. ;; あ a
  600. (and (fetch-library
  601. "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  602. t)
  603. (progn
  604. (safe-require-or-eval 'set-modeline-color)))
  605. (let ((fg (face-foreground 'default))
  606. (bg (face-background 'default)))
  607. (set-face-background 'mode-line-inactive
  608. (if (face-inverse-video-p 'mode-line) fg bg))
  609. (set-face-foreground 'mode-line-inactive
  610. (if (face-inverse-video-p 'mode-line) bg fg)))
  611. (set-face-underline 'mode-line-inactive
  612. t)
  613. (set-face-underline 'vertical-border
  614. nil)
  615. (and (fetch-library
  616. "https://raw.github.com/tarao/elisp/master/end-mark.el"
  617. t)
  618. (safe-require-or-eval 'end-mark)
  619. (global-end-mark-mode))
  620. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  621. ;; file handling
  622. (when (require 'editorconfig nil t)
  623. (defvar-set editorconfig-get-properties-function
  624. 'editorconfig-core-get-properties-hash)
  625. (editorconfig-mode 1))
  626. (setq revert-without-query '(".+"))
  627. ;; save cursor position
  628. (when (safe-require-or-eval 'saveplace)
  629. (setq-default save-place t)
  630. (setq save-place-file (concat user-emacs-directory
  631. "places")))
  632. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  633. (setq make-backup-files t)
  634. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  635. (setq backup-directory-alist
  636. (cons (cons "\\.*$" (expand-file-name (concat user-emacs-directory
  637. "backup")))
  638. backup-directory-alist))
  639. (setq version-control 'never)
  640. (setq delete-old-versions t)
  641. (setq auto-save-list-file-prefix (expand-file-name (concat user-emacs-directory
  642. "auto-save/")))
  643. (setq delete-auto-save-files t)
  644. (add-to-list 'completion-ignored-extensions ".bak")
  645. ;; (setq delete-by-moving-to-trash t
  646. ;; trash-directory "~/.emacs.d/trash")
  647. (add-hook 'after-save-hook
  648. 'executable-make-buffer-file-executable-if-script-p)
  649. (defvar-set bookmark-default-file (concat user-emacs-directory
  650. "bmk"))
  651. (add-hook 'recentf-load-hook
  652. (lambda ()
  653. (defvar recentf-exclude)
  654. (add-to-list 'recentf-exclude
  655. (regexp-quote bookmark-default-file))))
  656. (and (fetch-library
  657. "https://raw.github.com/10sr/emacs-lisp/master/read-only-only-mode.el"
  658. t)
  659. (autoload-eval-lazily 'read-only-only-mode))
  660. (and (fetch-library
  661. "https://raw.github.com/10sr/emacs-lisp/master/smart-revert.el"
  662. t)
  663. (safe-require-or-eval 'smart-revert)
  664. (smart-revert-on))
  665. ;; autosave
  666. (and (fetch-library
  667. "https://raw.github.com/10sr/emacs-lisp/master/autosave.el"
  668. t)
  669. (safe-require-or-eval 'autosave)
  670. (autosave-set 2))
  671. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  672. ;; editting
  673. (defun my-copy-whole-line ()
  674. "Copy whole line."
  675. (interactive)
  676. (kill-new (concat (buffer-substring (point-at-bol)
  677. (point-at-eol))
  678. "\n")))
  679. (setq require-final-newline t)
  680. (setq kill-whole-line t)
  681. (setq scroll-conservatively 35
  682. scroll-margin 2
  683. scroll-step 0)
  684. (setq-default major-mode 'text-mode)
  685. (setq next-line-add-newlines nil)
  686. (setq kill-read-only-ok t)
  687. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  688. ;; (setq-default line-spacing 0.2)
  689. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  690. (setq-default tab-width 4)
  691. (setq-default indent-tabs-mode nil)
  692. (setq-default indent-line-function nil)
  693. ;; (pc-selection-mode 1) ; make some already defined keybind back to default
  694. (delete-selection-mode 1)
  695. (cua-mode 0)
  696. (setq line-move-visual nil)
  697. ;; key bindings
  698. ;; moving around
  699. ;; (global-set-key (kbd "M-j") 'next-line)
  700. ;; (global-set-key (kbd "M-k") 'previous-line)
  701. ;; (global-set-key (kbd "M-h") 'backward-char)
  702. ;; (global-set-key (kbd "M-l") 'forward-char)
  703. ;;(keyboard-translate ?\M-j ?\C-j)
  704. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  705. (define-key esc-map "p" 'backward-paragraph)
  706. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  707. (define-key esc-map "n" 'forward-paragraph)
  708. (global-set-key (kbd "C-<up>") 'scroll-down-line)
  709. (global-set-key (kbd "C-<down>") 'scroll-up-line)
  710. (global-set-key (kbd "C-<left>") 'scroll-down)
  711. (global-set-key (kbd "C-<right>") 'scroll-up)
  712. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  713. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  714. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  715. ;; C-h and DEL
  716. (global-set-key (kbd "C-h") (kbd "DEL"))
  717. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  718. (global-set-key (kbd "C-o") (kbd "C-e C-m"))
  719. (define-key esc-map "k" 'my-copy-whole-line)
  720. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  721. (define-key esc-map "u" 'undo)
  722. (define-key esc-map "i" (kbd "ESC TAB"))
  723. ;; (global-set-key (kbd "C-r") 'query-replace-regexp)
  724. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  725. (global-set-key (kbd "C-r") 'isearch-backward-regexp)
  726. (define-key my-prefix-map (kbd "C-o") 'occur)
  727. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  728. ;; japanese input method
  729. (defun my-load-scim ()
  730. "Use scim-bridge.el as japanese im."
  731. ;; Load scim-bridge.
  732. (when (safe-require-or-eval 'scim-bridge)
  733. ;; Turn on scim-mode automatically after loading .emacs
  734. (call-after-init 'scim-mode-on)
  735. (defvar-set scim-cursor-color "red")
  736. (scim-define-preedit-key ?\^h t)
  737. (scim-define-common-key ?\* nil)
  738. (scim-define-common-key ?\^/ nil)))
  739. (defun my-load-anthy ()
  740. "Use anthy.el as japanese im."
  741. ;; anthy
  742. (when (safe-require-or-eval 'anthy)
  743. (global-set-key
  744. (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  745. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  746. (when (>= emacs-major-version 23)
  747. (defvar-set anthy-accept-timeout 1))))
  748. ;; quail
  749. ;; aproposs input-method for some information
  750. ;; (setq default-input-method "japanese")
  751. (defun my-load-mozc-el ()
  752. "Use mozc.el as japanese im."
  753. (when (safe-require-or-eval 'mozc)
  754. (defvar-set defauit-input-method "japanese-mozc")
  755. (defvar-set mozc-leim-title "[MZ]")
  756. ))
  757. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  758. ;; gmail
  759. (setq mail-interactive t
  760. send-mail-function 'smtpmail-send-it
  761. ;; message-send-mail-function 'smtpmail-send-it
  762. smtpmail-smtp-server "smtp.gmail.com"
  763. smtpmail-smtp-service 587
  764. smtpmail-starttls-credentials '(("smtp.gmail.com" 587
  765. "8.slashes@gmail.com" nil))
  766. smtpmail-auth-credentials '(("smtp.gmail.com" 587
  767. "8.slashes@gmail.com" nil))
  768. user-mail-address "8.slashes@gmail.com")
  769. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  770. ;; buffer killing
  771. ;; (defun my-delete-window-killing-buffer () nil)
  772. (defun my-query-kill-current-buffer ()
  773. "Interactively kill current buffer."
  774. (interactive)
  775. (if (y-or-n-p (concat "kill current buffer? :"))
  776. (kill-buffer (current-buffer))))
  777. (substitute-key-definition 'kill-buffer
  778. 'my-query-kill-current-buffer
  779. global-map)
  780. ;;(global-set-key "\C-xk" 'my-query-kill-current-buffer)
  781. (defun my-kill-buffers ()
  782. "Kill buffers that visit files."
  783. (interactive)
  784. (mapcar (lambda (buf)
  785. (when (buffer-file-name buf)
  786. (kill-buffer buf)))
  787. (buffer-list)))
  788. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  789. ;; share clipboard with x
  790. ;; this page describes this in details, but only these sexps seem to be needed
  791. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  792. (and (not window-system)
  793. (not (eq window-system 'mac))
  794. (getenv "DISPLAY")
  795. (not (equal (getenv "DISPLAY") ""))
  796. (executable-find "xclip")
  797. ;; (< emacs-major-version 24)
  798. (safe-require-or-eval 'xclip)
  799. nil
  800. (turn-on-xclip))
  801. (and (eq system-type 'darwin)
  802. (fetch-library
  803. "https://raw.github.com/10sr/emacs-lisp/master/pasteboard.el"
  804. t)
  805. (safe-require-or-eval 'pasteboard)
  806. (turn-on-pasteboard)
  807. (getenv "TMUX")
  808. (pasteboard-enable-rtun))
  809. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  810. ;; https://github.com/lunaryorn/flycheck
  811. (when (safe-require-or-eval 'flycheck)
  812. (call-after-init 'global-flycheck-mode))
  813. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  814. ;; window
  815. '(and (fetch-library
  816. "https://raw.github.com/10sr/emacs-lisp/master/window-organizer.el"
  817. t)
  818. (autoload-eval-lazily 'window-organizer)
  819. (define-key ctl-x-map (kbd "w") 'window-organizer))
  820. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  821. ;; server
  822. (when (safe-require-or-eval 'server)
  823. (setq server-name (concat "server"
  824. (number-to-string (emacs-pid))))
  825. ;; In Cygwin Environment `server-runnning-p' stops when server-use-tcp is nil
  826. ;; In Darwin environment, init fails with message like 'Service name too long'
  827. ;; when server-use-tcp is nil
  828. (when (or (eq system-type
  829. 'cygwin)
  830. (eq system-type
  831. 'darwin))
  832. (setq server-use-tcp t))
  833. (defun my-construct-emacsclient-editor-command ()
  834. "Construct and return command in a string to connect to current Emacs server."
  835. (if server-use-tcp
  836. (format "%s -f \"%s/%s\""
  837. "emacsclient"
  838. (expand-file-name server-auth-dir)
  839. server-name)
  840. (format "%s -s \"%s/%s\""
  841. "emacsclient"
  842. server-socket-dir
  843. server-name)))
  844. (setq process-environment
  845. `(,(concat "EDITOR="
  846. (my-construct-emacsclient-editor-command))
  847. ,(concat "GIT_EDITOR="
  848. (my-construct-emacsclient-editor-command))
  849. ,@process-environment))
  850. (server-start))
  851. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  852. ;; some modes and hooks
  853. (defvar-set ac-ignore-case nil)
  854. ;; (when (require 'ensime nil t)
  855. ;; (defvar-set ensime-ac-case-sensitive t)
  856. ;; (defvar-set ensime-company-case-sensitive t)
  857. ;; (add-hook 'scala-mode-hook
  858. ;; 'ensime-scala-mode-hook)
  859. ;; (add-hook 'ensime-scala-mode-hook
  860. ;; 'ac-stop))
  861. (when (autoload-eval-lazily 'term-run '(term-run-shell-command term-run))
  862. (define-key ctl-x-map "t" 'term-run-shell-command))
  863. (add-to-list 'safe-local-variable-values
  864. '(encoding utf-8))
  865. (setq enable-local-variables :safe)
  866. (when (autoload-eval-lazily 'dirtree nil
  867. (defun my-dirtree-current-line-directory-p ()
  868. "Return nil if element on current line is not a directory."
  869. (file-directory-p (widget-get (tree-mode-button-current-line)
  870. :file)))
  871. ;; This fix is actually a little strange. Strictly speaking
  872. ;; judging tree should be done by whether the widget is a tree one.
  873. (defun my-dirtree-next-node (arg)
  874. "Fix the problem that `tree-mode-next-node' moves cursor 2 lines."
  875. (interactive "p")
  876. (if (my-dirtree-current-line-directory-p)
  877. (widget-forward (* arg 2))
  878. (widget-forward arg)))
  879. (defun my-dirtree-previous-node (arg)
  880. "Fix the problem that `tree-mode-previous-node' moves cursor 2 lines."
  881. (interactive "p")
  882. (my-dirtree-next-node (- arg)))
  883. (define-key dirtree-mode-map "n" 'my-dirtree-next-node)
  884. (define-key dirtree-mode-map "p" 'my-dirtree-previous-node))
  885. (define-key ctl-x-map "d" 'dirtree))
  886. (and (fetch-library
  887. "https://raw.github.com/10sr/emacs-lisp/master/remember-major-modes-mode.el"
  888. t)
  889. (safe-require-or-eval 'remember-major-modes-mode)
  890. (remember-major-modes-mode 1)
  891. )
  892. ;; Detect file type from shebang and set major-mode.
  893. (add-to-list 'interpreter-mode-alist
  894. '("python3" . python-mode))
  895. (add-to-list 'interpreter-mode-alist
  896. '("python2" . python-mode))
  897. ;; http://fukuyama.co/foreign-regexp
  898. '(and (safe-require-or-eval 'foreign-regexp)
  899. (progn
  900. (setq foreign-regexp/regexp-type 'perl)
  901. '(setq reb-re-syntax 'foreign-regexp)
  902. ))
  903. (safe-require-or-eval 'session)
  904. (autoload-eval-lazily 'sql '(sql-mode)
  905. (safe-require-or-eval 'sql-indent))
  906. '(and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  907. t)
  908. (autoload-eval-lazily 'gtkbm)
  909. (global-set-key (kbd "C-x C-d") 'gtkbm))
  910. (and (fetch-library
  911. "https://raw.github.com/10sr/git-command-el/master/git-command.el"
  912. t)
  913. (autoload-eval-lazily 'git-command
  914. nil
  915. ;; for git-command old version
  916. (when (boundp 'git-command-major-mode-alist)
  917. (message "You are using old git-command ! Update it !!!")
  918. (add-to-list 'git-command-major-mode-alist
  919. '("di" . diff-mode))
  920. (add-to-list 'git-command-major-mode-alist
  921. '("graph" . fundamental-mode))
  922. (add-to-list 'git-command-major-mode-alist
  923. '("log" . fundamental-mode)))
  924. ;; for git-command new version
  925. (when (boundp 'git-command-view-command-list)
  926. (add-to-list 'git-command-view-command-list
  927. "graph")
  928. (add-to-list 'git-command-view-command-list
  929. "blame")
  930. (add-to-list 'git-command-view-command-list
  931. "help"))
  932. (when (boundp 'git-command-aliases-alist)
  933. ;; (message "new version of git-command!")
  934. (add-to-list 'git-command-aliases-alist
  935. '("di" . (lambda (options cmd args new-buffer-p)
  936. (git-command-exec options
  937. "diff"
  938. args
  939. new-buffer-p))))
  940. (add-to-list 'git-command-aliases-alist
  941. '("grep" . (lambda (options cmd args new-buffer-p)
  942. (my-rgrep
  943. (concat
  944. "git "
  945. (git-command-construct-commandline
  946. `(,@options "--no-pager"
  947. "-c" "color.grep=false")
  948. cmd
  949. `("-nHe" ,@args))))))))
  950. (setq git-command-use-emacsclient t)
  951. (or git-command-prompt-file
  952. (setq git-command-prompt-file
  953. (git-command-find-git-ps1
  954. "/usr/share/git-core/contrib/completion/git-prompt.sh"))))
  955. ;; (setq git-command-default-options "-c color.ui=always")
  956. (define-key ctl-x-map "g" 'git-command))
  957. (and (fetch-library
  958. "http://www.emacswiki.org/emacs/download/sl.el"
  959. t)
  960. (autoload-eval-lazily 'sl))
  961. (defalias 'qcalc 'quick-calc)
  962. (safe-require-or-eval 'simple)
  963. (add-hook 'makefile-mode-hook
  964. (lambda ()
  965. (local-set-key (kbd "C-m") 'newline-and-indent)
  966. ;; this functions is set in write-file-functions, i cannot find any
  967. ;; good way to remove this.
  968. (fset 'makefile-warn-suspicious-lines 'ignore)
  969. ))
  970. (add-hook 'verilog-mode-hook
  971. (lambda ()
  972. (local-set-key ";" 'self-insert-command)))
  973. (setq diff-switches "-u")
  974. (add-hook 'diff-mode-hook
  975. (lambda ()
  976. ;; (when (and (eq major-mode
  977. ;; 'diff-mode)
  978. ;; (not buffer-file-name))
  979. ;; ;; do not pass when major-mode is derived mode of diff-mode
  980. ;; (view-mode 1))
  981. (set-face-attribute 'diff-header nil
  982. :foreground nil
  983. :background nil
  984. :weight 'bold)
  985. (set-face-attribute 'diff-file-header nil
  986. :foreground nil
  987. :background nil
  988. :weight 'bold)
  989. (set-face-foreground 'diff-index-face "blue")
  990. (set-face-attribute 'diff-hunk-header nil
  991. :foreground "cyan"
  992. :weight 'normal)
  993. (set-face-attribute 'diff-context nil
  994. ;; :foreground "white"
  995. :foreground nil
  996. :weight 'normal)
  997. (set-face-foreground 'diff-removed-face "red")
  998. (set-face-foreground 'diff-added-face "green")
  999. (set-face-background 'diff-removed-face nil)
  1000. (set-face-background 'diff-added-face nil)
  1001. (set-face-attribute 'diff-changed nil
  1002. :foreground "magenta"
  1003. :weight 'normal)
  1004. (set-face-attribute 'diff-refine-change nil
  1005. :foreground nil
  1006. :background nil
  1007. :weight 'bold
  1008. :inverse-video t)
  1009. ;; Annoying !
  1010. ;;(diff-auto-refine-mode)
  1011. ))
  1012. ;; (ffap-bindings)
  1013. (add-hook 'sh-mode-hook
  1014. (lambda ()
  1015. (local-set-key
  1016. (kbd "C-x C-e")
  1017. 'my-execute-shell-command-current-line)))
  1018. (defvar-set sh-here-document-word "__EOC__")
  1019. (defun my-execute-shell-command-current-line ()
  1020. "Run current line as shell command."
  1021. (interactive)
  1022. (shell-command (buffer-substring-no-properties (point-at-bol)
  1023. (point))))
  1024. (setq auto-mode-alist
  1025. `(("autostart\\'" . sh-mode)
  1026. ("xinitrc\\'" . sh-mode)
  1027. ("xprograms\\'" . sh-mode)
  1028. ("PKGBUILD\\'" . sh-mode)
  1029. ,@auto-mode-alist))
  1030. (and (autoload-eval-lazily 'pkgbuild-mode)
  1031. (setq auto-mode-alist (append '(("PKGBUILD\\'" . pkgbuild-mode))
  1032. auto-mode-alist)))
  1033. (and (autoload-eval-lazily 'groovy-mode)
  1034. (add-to-list 'auto-mode-alist
  1035. '("build.gradle\\'" . groovy-mode)))
  1036. (add-hook 'yaml-mode-hook
  1037. (lambda ()
  1038. (local-set-key(kbd "C-m") 'newline)))
  1039. (add-hook 'html-mode-hook
  1040. (lambda ()
  1041. (local-set-key(kbd "C-m") 'reindent-then-newline-and-indent)))
  1042. (add-hook 'text-mode-hook
  1043. (lambda ()
  1044. (local-set-key (kbd "C-m") 'newline)))
  1045. (add-to-list 'Info-default-directory-list
  1046. (expand-file-name "~/.info/emacs-ja"))
  1047. (add-hook 'apropos-mode-hook
  1048. (lambda ()
  1049. (local-set-key "n" 'next-line)
  1050. (local-set-key "p" 'previous-line)
  1051. ))
  1052. (add-hook 'isearch-mode-hook
  1053. (lambda ()
  1054. ;; (define-key isearch-mode-map
  1055. ;; (kbd "C-j") 'isearch-other-control-char)
  1056. ;; (define-key isearch-mode-map
  1057. ;; (kbd "C-k") 'isearch-other-control-char)
  1058. ;; (define-key isearch-mode-map
  1059. ;; (kbd "C-h") 'isearch-other-control-char)
  1060. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  1061. (define-key isearch-mode-map (kbd "M-r")
  1062. 'isearch-query-replace-regexp)))
  1063. ;; do not cleanup isearch highlight: use `lazy-highlight-cleanup' to remove
  1064. (setq lazy-highlight-cleanup nil)
  1065. ;; face for isearch highlighing
  1066. (set-face-attribute 'lazy-highlight
  1067. nil
  1068. :foreground `unspecified
  1069. :background `unspecified
  1070. :underline t
  1071. ;; :weight `bold
  1072. )
  1073. (add-hook 'outline-mode-hook
  1074. (lambda ()
  1075. (if (string-match "\\.md\\'" buffer-file-name)
  1076. (set (make-local-variable 'outline-regexp) "#+ "))))
  1077. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  1078. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  1079. (when (autoload-eval-lazily 'markdown-mode
  1080. '(markdown-mode gfm-mode))
  1081. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'gfm-mode))
  1082. (setq markdown-command (or (executable-find "markdown")
  1083. (executable-find "markdown.pl")))
  1084. (add-hook 'markdown-mode-hook
  1085. (lambda ()
  1086. (outline-minor-mode 1)
  1087. (flyspell-mode)
  1088. (set (make-local-variable 'comment-start) ";")))
  1089. (add-hook 'gfm-mode-hook
  1090. (lambda ()
  1091. (define-key gfm-mode-map (kbd "C-m") 'electric-indent-just-newline)
  1092. ;;(electric-indent-mode 0)
  1093. )))
  1094. ;; c-mode
  1095. ;; http://www.emacswiki.org/emacs/IndentingC
  1096. ;; http://en.wikipedia.org/wiki/Indent_style
  1097. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  1098. ;; http://seesaawiki.jp/whiteflare503/d/Emacs%20%a5%a4%a5%f3%a5%c7%a5%f3%a5%c8
  1099. (when (autoload-eval-lazily 'cc-vars
  1100. nil
  1101. (defvar c-default-style)
  1102. (add-to-list 'c-default-style
  1103. '(c-mode . "k&r"))
  1104. (add-to-list 'c-default-style
  1105. '(c++-mode . "k&r"))
  1106. (add-hook 'c-mode-common-hook
  1107. (lambda ()
  1108. ;; why c-basic-offset in k&r style defaults to 5 ???
  1109. (defvar-set c-basic-offset 4)
  1110. (defvar-set indent-tabs-mode nil)
  1111. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  1112. (c-toggle-hungry-state -1)
  1113. ;; (and (require 'gtags nil t)
  1114. ;; (gtags-mode 1))
  1115. ))))
  1116. (when (autoload-eval-lazily 'php-mode)
  1117. (add-hook 'php-mode-hook
  1118. (lambda ()
  1119. (setq c-basic-offset 2))))
  1120. (when (autoload-eval-lazily 'js2-mode)
  1121. ;; currently do not use js2-mode
  1122. ;; (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  1123. ;; (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode))
  1124. (add-hook 'js2-mode-hook
  1125. (lambda ()
  1126. (define-key js2-mode-map (kbd "C-m") (lambda ()
  1127. (interactive)
  1128. (js2-enter-key)
  1129. (indent-for-tab-command)))
  1130. ;; (add-hook (kill-local-variable 'before-save-hook)
  1131. ;; 'js2-before-save)
  1132. ;; (add-hook 'before-save-hook
  1133. ;; 'my-indent-buffer
  1134. ;; nil
  1135. ;; t)
  1136. )))
  1137. (eval-after-load "js"
  1138. (defvar-set js-indent-level 2))
  1139. (add-to-list 'interpreter-mode-alist
  1140. '("node" . js-mode))
  1141. (when (autoload-eval-lazily 'flymake-jslint
  1142. '(flymake-jslint-load))
  1143. (autoload-eval-lazily 'js nil
  1144. (add-hook 'js-mode-hook
  1145. 'flymake-jslint-load)))
  1146. (safe-require-or-eval 'js-doc)
  1147. (add-hook 'haskell-mode-hook 'turn-on-haskell-indentation)
  1148. (when (safe-require-or-eval 'uniquify)
  1149. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  1150. (setq uniquify-ignore-buffers-re "*[^*]+*")
  1151. (setq uniquify-min-dir-content 1))
  1152. (add-hook 'view-mode-hook
  1153. (lambda()
  1154. (defvar view-mode-map)
  1155. (define-key view-mode-map "j" 'scroll-up-line)
  1156. (define-key view-mode-map "k" 'scroll-down-line)
  1157. (define-key view-mode-map "v" 'toggle-read-only)
  1158. (define-key view-mode-map "q" 'bury-buffer)
  1159. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  1160. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  1161. ;; (define-key view-mode-map
  1162. ;; "n" 'nonincremental-repeat-search-forward)
  1163. ;; (define-key view-mode-map
  1164. ;; "N" 'nonincremental-repeat-search-backward)
  1165. (define-key view-mode-map "/" 'isearch-forward-regexp)
  1166. (define-key view-mode-map "?" 'isearch-backward-regexp)
  1167. (define-key view-mode-map "n" 'isearch-repeat-forward)
  1168. (define-key view-mode-map "N" 'isearch-repeat-backward)
  1169. (define-key view-mode-map (kbd "C-m") 'my-rgrep-symbol-at-point)
  1170. ))
  1171. (global-set-key "\M-r" 'view-mode)
  1172. ;; (setq view-read-only t)
  1173. ;; (defun my-view-mode-search-word (word)
  1174. ;; "Search for word current directory and subdirectories.
  1175. ;; If called intearctively, find word at point."
  1176. ;; (interactive (list (thing-at-point 'symbol)))
  1177. ;; (if word
  1178. ;; (if (and (require 'gtags nil t)
  1179. ;; (gtags-get-rootpath))
  1180. ;; (gtags-goto-tag word "s")
  1181. ;; (my-rgrep word))
  1182. ;; (message "No word at point.")
  1183. ;; nil))
  1184. (add-hook 'Man-mode-hook
  1185. (lambda ()
  1186. (view-mode 1)
  1187. (setq truncate-lines nil)))
  1188. (defvar-set Man-notify-method (if window-system
  1189. 'newframe
  1190. 'aggressive))
  1191. (defvar-set woman-cache-filename (expand-file-name (concat user-emacs-directory
  1192. "woman_cache.el")))
  1193. (defalias 'man 'woman)
  1194. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1195. ;; python
  1196. (when (autoload-eval-lazily 'python '(python-mode))
  1197. (defvar-set python-python-command (or (executable-find "python3")
  1198. (executable-find "python")))
  1199. ;; (defun my-python-run-as-command ()
  1200. ;; ""
  1201. ;; (interactive)
  1202. ;; (shell-command (concat python-python-command " " buffer-file-name)))
  1203. (defun my-python-display-python-buffer ()
  1204. ""
  1205. (interactive)
  1206. (defvar python-buffer)
  1207. (set-window-text-height (display-buffer python-buffer
  1208. t)
  1209. 7))
  1210. (add-hook 'python-mode-hook
  1211. (lambda ()
  1212. (local-set-key (kbd "C-c C-e") 'my-python-run-as-command)
  1213. (local-set-key (kbd "C-c C-b") 'my-python-display-python-buffer)
  1214. (local-set-key (kbd "C-m") 'newline-and-indent)))
  1215. (add-hook 'inferior-python-mode-hook
  1216. (lambda ()
  1217. (my-python-display-python-buffer)
  1218. (local-set-key (kbd "<up>") 'comint-previous-input)
  1219. (local-set-key (kbd "<down>") 'comint-next-input))))
  1220. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1221. ;; GNU GLOBAL(gtags)
  1222. ;; http://uguisu.skr.jp/Windows/gtags.html
  1223. ;; http://eigyr.dip.jp/gtags.html
  1224. ;; http://cha.la.coocan.jp/doc/gnu_global.html
  1225. (let ((d "/opt/local/share/gtags/"))
  1226. (and (file-directory-p d)
  1227. (add-to-list 'load-path
  1228. d)))
  1229. (when (autoload-eval-lazily 'gtags '(gtags-mode))
  1230. (add-hook 'gtags-mode-hook
  1231. (lambda ()
  1232. (view-mode gtags-mode)
  1233. (setq gtags-select-buffer-single t)
  1234. ;; (local-set-key "\M-t" 'gtags-find-tag)
  1235. ;; (local-set-key "\M-r" 'gtags-find-rtag)
  1236. ;; (local-set-key "\M-s" 'gtags-find-symbol)
  1237. ;; (local-set-key "\C-t" 'gtags-pop-stack)
  1238. (define-key gtags-mode-map (kbd "C-x t h")
  1239. 'gtags-find-tag-from-here)
  1240. (define-key gtags-mode-map (kbd "C-x t t") 'gtags-find-tag)
  1241. (define-key gtags-mode-map (kbd "C-x t r") 'gtags-find-rtag)
  1242. (define-key gtags-mode-map (kbd "C-x t s") 'gtags-find-symbol)
  1243. (define-key gtags-mode-map (kbd "C-x t p") 'gtags-find-pattern)
  1244. (define-key gtags-mode-map (kbd "C-x t f") 'gtags-find-file)
  1245. (define-key gtags-mode-map (kbd "C-x t b") 'gtags-pop-stack) ;back
  1246. ))
  1247. (add-hook 'gtags-select-mode-hook
  1248. (lambda ()
  1249. (define-key gtags-select-mode-map (kbd "C-m") 'gtags-select-tag)
  1250. ))
  1251. )
  1252. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1253. ;; term mode
  1254. ;; (setq multi-term-program shell-file-name)
  1255. (when (autoload-eval-lazily 'multi-term)
  1256. (setq multi-term-switch-after-close nil)
  1257. (setq multi-term-dedicated-select-after-open-p t)
  1258. (setq multi-term-dedicated-window-height 20))
  1259. (when (autoload-eval-lazily 'term '(term ansi-term))
  1260. (defun my-term-quit-or-send-raw ()
  1261. ""
  1262. (interactive)
  1263. (if (get-buffer-process (current-buffer))
  1264. (call-interactively 'term-send-raw)
  1265. (kill-buffer)))
  1266. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  1267. ;; (setq term-ansi-default-program shell-file-name)
  1268. (add-hook 'term-setup-hook
  1269. (lambda ()
  1270. (defvar-set term-display-table (make-display-table))))
  1271. (add-hook 'term-mode-hook
  1272. (lambda ()
  1273. (defvar term-raw-map)
  1274. (unless (memq (current-buffer)
  1275. (and (featurep 'multi-term)
  1276. (defvar multi-term-buffer-list)
  1277. ;; current buffer is not multi-term buffer
  1278. multi-term-buffer-list))
  1279. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  1280. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  1281. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  1282. ;; (define-key term-raw-map "\C-f" 'forward-char)
  1283. ;; (define-key term-raw-map "\C-b" 'backward-char)
  1284. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  1285. (define-key term-raw-map
  1286. "\C-x" (lookup-key (current-global-map) "\C-x"))
  1287. (define-key term-raw-map
  1288. "\C-z" (lookup-key (current-global-map) "\C-z"))
  1289. )
  1290. ;; (define-key term-raw-map "\C-xl" 'term-line-mode)
  1291. ;; (define-key term-mode-map "\C-xc" 'term-char-mode)
  1292. (define-key term-raw-map (kbd "<up>") 'scroll-down-line)
  1293. (define-key term-raw-map (kbd "<down>") 'scroll-up-line)
  1294. (define-key term-raw-map (kbd "<right>") 'scroll-up)
  1295. (define-key term-raw-map (kbd "<left>") 'scroll-down)
  1296. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  1297. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  1298. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  1299. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  1300. (define-key term-raw-map [delete] 'term-send-raw)
  1301. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  1302. (define-key term-raw-map "\C-y" 'term-paste)
  1303. (define-key term-raw-map
  1304. "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  1305. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  1306. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  1307. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  1308. ;; (define-key term-raw-map "\C-d" 'delete-char)
  1309. (set (make-local-variable 'scroll-margin) 0)
  1310. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  1311. ;; (cua-mode 0)
  1312. ;; (and cua-mode
  1313. ;; (local-unset-key (kbd "C-c")))
  1314. ;; (define-key cua--prefix-override-keymap
  1315. ;;"\C-c" 'term-interrupt-subjob)
  1316. (set (make-local-variable (defvar hl-line-range-function))
  1317. (lambda ()
  1318. '(0 . 0)))
  1319. ))
  1320. ;; (add-hook 'term-exec-hook 'forward-char)
  1321. )
  1322. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1323. ;; buffer switching
  1324. (defvar bs-configurations)
  1325. (when (autoload-eval-lazily 'bs '(bs-show)
  1326. ;; (add-to-list 'bs-configurations
  1327. ;; '("processes" nil get-buffer-process ".*" nil nil))
  1328. (add-to-list 'bs-configurations
  1329. '("files-and-terminals" nil nil nil
  1330. (lambda (buf)
  1331. (and (bs-visits-non-file buf)
  1332. (save-excursion
  1333. (set-buffer buf)
  1334. (not (memq major-mode
  1335. '(term-mode
  1336. eshell-mode))))))))
  1337. ;; (setq bs-configurations (list
  1338. ;; '("processes" nil get-buffer-process ".*" nil nil)
  1339. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil
  1340. ;; bs-visits-non-file bs-sort-buffer-interns-are-last)))
  1341. )
  1342. ;; (global-set-key "\C-x\C-b" 'bs-show)
  1343. (defalias 'list-buffers 'bs-show)
  1344. (defvar-set bs-default-configuration "files-and-terminals")
  1345. (defvar-set bs-default-sort-name "by nothing")
  1346. (add-hook 'bs-mode-hook
  1347. (lambda ()
  1348. ;; (setq bs-default-configuration "files")
  1349. ;; (and bs--show-all
  1350. ;; (call-interactively 'bs-toggle-show-all))
  1351. (set (make-local-variable 'scroll-margin) 0))))
  1352. ;;(iswitchb-mode 1)
  1353. (icomplete-mode)
  1354. (defun iswitchb-buffer-display-other-window ()
  1355. "Do iswitchb in other window."
  1356. (interactive)
  1357. (let ((iswitchb-default-method 'display))
  1358. (call-interactively 'iswitchb-buffer)))
  1359. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1360. ;; sdic
  1361. (when (autoload-eval-lazily 'sdic '(sdic-describe-word-at-point))
  1362. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  1363. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo)
  1364. (defun sdic-describe-word-at-point-echo ()
  1365. ""
  1366. (interactive)
  1367. (save-window-excursion
  1368. (sdic-describe-word-at-point))
  1369. (save-excursion
  1370. (set-buffer sdic-buffer-name)
  1371. (message (buffer-substring (point-min)
  1372. (progn (goto-char (point-min))
  1373. (or (and (re-search-forward "^\\w"
  1374. nil
  1375. t
  1376. 4)
  1377. (progn (previous-line) t)
  1378. (point-at-eol))
  1379. (point-max)))))))
  1380. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  1381. (setq sdic-waei-dictionary-list
  1382. '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  1383. (setq sdic-disable-select-window t)
  1384. (setq sdic-window-height 7))
  1385. ;;;;;;;;;;;;;;;;;;;;;;;;
  1386. ;; ilookup
  1387. (when (fetch-library
  1388. "https://raw.github.com/10sr/emacs-lisp/master/ilookup.el"
  1389. t)
  1390. (autoload-eval-lazily 'ilookup
  1391. '(ilookup-open)
  1392. (setq ilookup-dict-alist
  1393. '(
  1394. ("sdcv" . (lambda (word)
  1395. (shell-command-to-string
  1396. (format "sdcv -n '%s'"
  1397. word))))
  1398. ("en" . (lambda (word)
  1399. (shell-command-to-string
  1400. (format "sdcv -n -u dictd_www.dict.org_gcide '%s'"
  1401. word))))
  1402. ("ja" . (lambda (word)
  1403. (shell-command-to-string
  1404. (format "sdcv -n -u EJ-GENE95 -u jmdict-en-ja '%s'"
  1405. word))))
  1406. ("jaj" . (lambda (word)
  1407. (shell-command-to-string
  1408. (format "sdcv -n -u jmdict-en-ja '%s'"
  1409. word))))
  1410. ("jag" .
  1411. (lambda (word)
  1412. (with-temp-buffer
  1413. (insert (shell-command-to-string
  1414. (format "sdcv -n -u 'Genius English-Japanese' '%s'"
  1415. word)))
  1416. (html2text)
  1417. (buffer-substring (point-min)
  1418. (point-max)))))
  1419. ("alc" . (lambda (word)
  1420. (shell-command-to-string
  1421. (format "alc '%s' | head -n 20"
  1422. word))))
  1423. ("app" . (lambda (word)
  1424. (shell-command-to-string
  1425. (format "dict_app '%s'"
  1426. word))))
  1427. ;; letters broken
  1428. ("ms" .
  1429. (lambda (word)
  1430. (let ((url (concat
  1431. "http://api.microsofttranslator.com/V2/Ajax.svc/"
  1432. "Translate?appId=%s&text=%s&to=%s"))
  1433. (apikey "3C9778666C5BA4B406FFCBEE64EF478963039C51")
  1434. (target "ja")
  1435. (eword (url-hexify-string word)))
  1436. (with-current-buffer (url-retrieve-synchronously
  1437. (format url
  1438. apikey
  1439. eword
  1440. target))
  1441. (message "")
  1442. (goto-char (point-min))
  1443. (search-forward-regexp "^$"
  1444. nil
  1445. t)
  1446. (url-unhex-string (buffer-substring-no-properties
  1447. (point)
  1448. (point-max)))))))
  1449. ))
  1450. ;; (funcall (cdr (assoc "ms"
  1451. ;; ilookup-alist))
  1452. ;; "dictionary")
  1453. ;; (switch-to-buffer (url-retrieve-synchronously "http://api.microsofttranslator.com/V2/Ajax.svc/Translate?appId=3C9778666C5BA4B406FFCBEE64EF478963039C51&text=dictionary&to=ja"))
  1454. ;; (switch-to-buffer (url-retrieve-synchronously "http://google.com"))
  1455. (setq ilookup-default "ja")
  1456. (when (locate-library "google-translate")
  1457. (add-to-list 'ilookup-dict-alist
  1458. '("gt" .
  1459. (lambda (word)
  1460. (save-excursion
  1461. (google-translate-translate "auto"
  1462. "ja"
  1463. word))
  1464. (with-current-buffer "*Google Translate*"
  1465. (buffer-substring-no-properties (point-min)
  1466. (point-max)))))))
  1467. ))
  1468. (when (autoload-eval-lazily 'google-translate '(google-translate-translate
  1469. google-translate-at-point))
  1470. (setq google-translate-default-source-language "auto")
  1471. (setq google-translate-default-target-language "ja"))
  1472. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1473. ;; vc
  1474. (require 'vc)
  1475. (setq vc-handled-backends '())
  1476. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1477. ;; gauche-mode
  1478. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  1479. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  1480. (when (and (fetch-library
  1481. "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  1482. t)
  1483. (autoload-eval-lazily 'gauche-mode '(gauche-mode run-scheme)))
  1484. (let ((s (executable-find "gosh")))
  1485. (setq scheme-program-name s
  1486. gauche-program-name s))
  1487. (defun run-gauche-other-window ()
  1488. "Run gauche on other window"
  1489. (interactive)
  1490. (switch-to-buffer-other-window
  1491. (get-buffer-create "*scheme*"))
  1492. (run-gauche))
  1493. (defun run-gauche ()
  1494. "run gauche"
  1495. (interactive)
  1496. (run-scheme gauche-program-name)
  1497. )
  1498. (defun scheme-send-buffer ()
  1499. ""
  1500. (interactive)
  1501. (scheme-send-region (point-min) (point-max))
  1502. (my-scheme-display-scheme-buffer)
  1503. )
  1504. (defun my-scheme-display-scheme-buffer ()
  1505. ""
  1506. (interactive)
  1507. (set-window-text-height (display-buffer scheme-buffer
  1508. t)
  1509. 7))
  1510. (add-hook 'scheme-mode-hook
  1511. (lambda ()
  1512. nil))
  1513. (add-hook 'inferior-scheme-mode-hook
  1514. (lambda ()
  1515. ;; (my-scheme-display-scheme-buffer)
  1516. ))
  1517. (setq auto-mode-alist
  1518. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  1519. (setq auto-mode-alist
  1520. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  1521. (add-hook 'gauche-mode-hook
  1522. (lambda ()
  1523. (define-key gauche-mode-map
  1524. (kbd "C-c C-z") 'run-gauche-other-window)
  1525. (define-key scheme-mode-map
  1526. (kbd "C-c C-c") 'scheme-send-buffer)
  1527. (define-key scheme-mode-map
  1528. (kbd "C-c C-b") 'my-scheme-display-scheme-buffer))))
  1529. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1530. ;; recentf-mode
  1531. (setq recentf-save-file (expand-file-name (concat user-emacs-directory
  1532. "recentf"))
  1533. recentf-max-menu-items 20
  1534. recentf-max-saved-items 30
  1535. recentf-show-file-shortcuts-flag nil)
  1536. (when (safe-require-or-eval 'recentf)
  1537. (add-to-list 'recentf-exclude
  1538. (regexp-quote recentf-save-file))
  1539. (add-to-list 'recentf-exclude
  1540. (regexp-quote (expand-file-name user-emacs-directory)))
  1541. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  1542. (remove-hook 'find-file-hook
  1543. 'recentf-track-opened-file)
  1544. (defun my-recentf-load-track-save-list ()
  1545. "Load current recentf list from file, track current visiting file, then save
  1546. the list."
  1547. (recentf-load-list)
  1548. (recentf-track-opened-file)
  1549. (recentf-save-list))
  1550. (add-hook 'find-file-hook
  1551. 'my-recentf-load-track-save-list)
  1552. (add-hook 'kill-emacs-hook
  1553. 'recentf-load-list)
  1554. ;;(run-with-idle-timer 5 t 'recentf-save-list)
  1555. ;; (add-hook 'find-file-hook
  1556. ;; (lambda ()
  1557. ;; (recentf-add-file default-directory)))
  1558. (and (fetch-library
  1559. "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  1560. t)
  1561. (autoload-eval-lazily 'recentf-show)
  1562. (define-key ctl-x-map (kbd "C-r") 'recentf-show)
  1563. (add-hook 'recentf-show-before-listing-hook
  1564. 'recentf-load-list))
  1565. (recentf-mode 1)
  1566. (add-hook 'recentf-dialog-mode-hook
  1567. (lambda ()
  1568. ;; (recentf-save-list)
  1569. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f")
  1570. ;; 'my-recentf-cd-and-find-file)
  1571. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  1572. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  1573. (define-key recentf-dialog-mode-map "p" 'previous-line)
  1574. (define-key recentf-dialog-mode-map "n" 'next-line)
  1575. (cd "~/"))))
  1576. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1577. ;; dired
  1578. (when (autoload-eval-lazily 'dired nil)
  1579. (defun my-dired-echo-file-head (arg)
  1580. ""
  1581. (interactive "P")
  1582. (let ((f (dired-get-filename)))
  1583. (message "%s"
  1584. (with-temp-buffer
  1585. (insert-file-contents f)
  1586. (buffer-substring-no-properties
  1587. (point-min)
  1588. (progn (goto-line (if arg
  1589. (prefix-numeric-value arg)
  1590. 7))
  1591. (point-at-eol)))))))
  1592. (defun my-dired-diff ()
  1593. ""
  1594. (interactive)
  1595. (let ((files (dired-get-marked-files nil nil nil t)))
  1596. (if (eq (car files)
  1597. t)
  1598. (diff (cadr files) (dired-get-filename))
  1599. (message "One files must be marked!"))))
  1600. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  1601. "pop up buffer using `display-buffer' and return that buffer."
  1602. (let ((bf (get-buffer-create buffer-or-name)))
  1603. (with-current-buffer bf
  1604. (cd ".")
  1605. (erase-buffer))
  1606. (display-buffer bf)
  1607. bf))
  1608. (defun my-replace-nasi-none ()
  1609. ""
  1610. (save-excursion
  1611. (let ((buffer-read-only nil))
  1612. (goto-char (point-min))
  1613. (while (search-forward "なし" nil t)
  1614. (replace-match "none")))))
  1615. (defun dired-get-file-info ()
  1616. "dired get file info"
  1617. (interactive)
  1618. (let ((f (shell-quote-argument (dired-get-filename t))))
  1619. (if (file-directory-p f)
  1620. (progn
  1621. (message "Calculating disk usage...")
  1622. (shell-command (concat "du -hsD "
  1623. f)))
  1624. (shell-command (concat "file "
  1625. f)))))
  1626. (defun my-dired-scroll-up ()
  1627. ""
  1628. (interactive)
  1629. (my-dired-previous-line (- (window-height) 1)))
  1630. (defun my-dired-scroll-down ()
  1631. ""
  1632. (interactive)
  1633. (my-dired-next-line (- (window-height) 1)))
  1634. ;; (defun my-dired-forward-line (arg)
  1635. ;; ""
  1636. ;; (interactive "p"))
  1637. (defun my-dired-previous-line (arg)
  1638. ""
  1639. (interactive "p")
  1640. (if (> arg 0)
  1641. (progn
  1642. (if (eq (line-number-at-pos)
  1643. 1)
  1644. (goto-char (point-max))
  1645. (forward-line -1))
  1646. (my-dired-previous-line (if (or (dired-get-filename nil t)
  1647. (dired-get-subdir))
  1648. (- arg 1)
  1649. arg)))
  1650. (dired-move-to-filename)))
  1651. (defun my-dired-next-line (arg)
  1652. ""
  1653. (interactive "p")
  1654. (if (> arg 0)
  1655. (progn
  1656. (if (eq (point)
  1657. (point-max))
  1658. (goto-char (point-min))
  1659. (forward-line 1))
  1660. (my-dired-next-line (if (or (dired-get-filename nil t)
  1661. (dired-get-subdir))
  1662. (- arg 1)
  1663. arg)))
  1664. (dired-move-to-filename)))
  1665. (defun my-dired-print-current-dir-and-file ()
  1666. (message "%s %s"
  1667. default-directory
  1668. (buffer-substring-no-properties (point-at-bol)
  1669. (point-at-eol))))
  1670. (defun dired-do-execute-as-command ()
  1671. ""
  1672. (interactive)
  1673. (let ((file (dired-get-filename t)))
  1674. (if (file-executable-p file)
  1675. (start-process file nil file)
  1676. (when (y-or-n-p
  1677. "This file cant be executed. Mark as executable and go? ")
  1678. (set-file-modes file
  1679. (file-modes-symbolic-to-number "u+x" (file-modes file)))
  1680. (start-process file nil file)))))
  1681. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  1682. (defun my-dired-x-open ()
  1683. ""
  1684. (interactive)
  1685. (my-x-open (dired-get-filename t t)))
  1686. (if (eq window-system 'mac)
  1687. (setq dired-listing-switches "-lhF")
  1688. (setq dired-listing-switches "-lhF --time-style=long-iso")
  1689. )
  1690. (setq dired-listing-switches "-lhF")
  1691. (put 'dired-find-alternate-file 'disabled nil)
  1692. ;; when using dired-find-alternate-file
  1693. ;; reuse current dired buffer for the file to open
  1694. (defvar-set dired-ls-F-marks-symlinks t)
  1695. (when (safe-require-or-eval 'ls-lisp)
  1696. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  1697. (setq ls-lisp-dirs-first t)
  1698. (setq ls-lisp-use-localized-time-format t)
  1699. (setq ls-lisp-format-time-list
  1700. '("%Y-%m-%d %H:%M"
  1701. "%Y-%m-%d ")))
  1702. (defvar-set dired-dwim-target t)
  1703. (defvar-set dired-isearch-filenames t)
  1704. (defvar-set dired-hide-details-hide-symlink-targets nil)
  1705. (defvar-set dired-hide-details-hide-information-lines nil)
  1706. ;; (add-hook 'dired-after-readin-hook
  1707. ;; 'my-replace-nasi-none)
  1708. ;; (add-hook 'after-init-hook
  1709. ;; (lambda ()
  1710. ;; (dired ".")))
  1711. (add-hook 'dired-mode-hook
  1712. (lambda ()
  1713. (local-set-key "o" 'my-dired-x-open)
  1714. (local-set-key "i" 'dired-get-file-info)
  1715. (local-set-key "f" 'find-file)
  1716. (local-set-key "!" 'shell-command)
  1717. (local-set-key "&" 'async-shell-command)
  1718. (local-set-key "X" 'dired-do-async-shell-command)
  1719. (local-set-key "=" 'my-dired-diff)
  1720. (local-set-key "B" 'gtkbm-add-current-dir)
  1721. (local-set-key "b" 'gtkbm)
  1722. (local-set-key "h" 'my-dired-echo-file-head)
  1723. (local-set-key "@" (lambda ()
  1724. (interactive) (my-x-open ".")))
  1725. (local-set-key (kbd "TAB") 'other-window)
  1726. ;; (local-set-key "P" 'my-dired-do-pack-or-unpack)
  1727. (local-set-key "/" 'dired-isearch-filenames)
  1728. (local-set-key (kbd "DEL") 'dired-up-directory)
  1729. (local-set-key (kbd "C-h") 'dired-up-directory)
  1730. (substitute-key-definition 'dired-next-line
  1731. 'my-dired-next-line
  1732. (current-local-map))
  1733. (substitute-key-definition 'dired-previous-line
  1734. 'my-dired-previous-line
  1735. (current-local-map))
  1736. ;; (local-set-key (kbd "C-p") 'my-dired-previous-line)
  1737. ;; (local-set-key (kbd "p") 'my-dired-previous-line)
  1738. ;; (local-set-key (kbd "C-n") 'my-dired-next-line)
  1739. ;; (local-set-key (kbd "n") 'my-dired-next-line)
  1740. (local-set-key (kbd "<left>") 'my-dired-scroll-up)
  1741. (local-set-key (kbd "<right>") 'my-dired-scroll-down)
  1742. (local-set-key (kbd "ESC p") 'my-dired-scroll-up)
  1743. (local-set-key (kbd "ESC n") 'my-dired-scroll-down)
  1744. (when (fboundp 'dired-hide-details-mode)
  1745. (dired-hide-details-mode t)
  1746. (local-set-key "l" 'dired-hide-details-mode))
  1747. (let ((file "._Icon\015"))
  1748. (when nil (file-readable-p file)
  1749. (delete-file file)))))
  1750. (and (fetch-library "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  1751. t)
  1752. (autoload-eval-lazily 'pack '(dired-do-pack-or-unpack pack))
  1753. (add-hook 'dired-mode-hook
  1754. (lambda ()
  1755. (local-set-key "P" 'dired-do-pack-or-unpack))))
  1756. (and (fetch-library
  1757. "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  1758. t)
  1759. (autoload-eval-lazily 'dired-list-all-mode)
  1760. (setq dired-listing-switches "-lhF")
  1761. (add-hook 'dired-mode-hook
  1762. (lambda ()
  1763. (local-set-key "a" 'dired-list-all-mode)
  1764. )))
  1765. ) ; when dired locate
  1766. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  1767. (defvar dired-marker-char)
  1768. (defun my-dired-toggle-mark()
  1769. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  1770. (t dired-marker-char))))
  1771. (delete-char 1)
  1772. (insert cur)))
  1773. (defun my-dired-mark (arg)
  1774. "Toggle mark the current (or next ARG) files.
  1775. If on a subdir headerline, mark all its files except `.' and `..'.
  1776. Use \\[dired-unmark-all-files] to remove all marks
  1777. and \\[dired-unmark] on a subdir to remove the marks in
  1778. this subdir."
  1779. (interactive "P")
  1780. (if (dired-get-subdir)
  1781. (save-excursion (dired-mark-subdir-files))
  1782. (let ((inhibit-read-only t))
  1783. (dired-repeat-over-lines
  1784. (prefix-numeric-value arg)
  1785. 'my-dired-toggle-mark))))
  1786. (defun my-dired-mark-backward (arg)
  1787. "In Dired, move up lines and toggle mark there.
  1788. Optional prefix ARG says how many lines to unflag; default is one line."
  1789. (interactive "p")
  1790. (my-dired-mark (- arg)))
  1791. (add-hook 'dired-mode-hook
  1792. (lambda ()
  1793. (local-set-key (kbd "SPC") 'my-dired-mark)
  1794. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1795. )
  1796. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1797. ;; eshell
  1798. (autoload-eval-lazily 'eshell nil
  1799. (defvar-set eshell-banner-message (format "Welcome to the Emacs shell
  1800. %s
  1801. C-x t to toggling emacs-text-mode
  1802. "
  1803. (shell-command-to-string "uname -a")
  1804. ))
  1805. (defvar eshell-text-mode-map
  1806. (let ((map (make-sparse-keymap)))
  1807. (define-key map (kbd "C-x t") 'eshell-text-mode-toggle)
  1808. map))
  1809. (define-derived-mode eshell-text-mode text-mode
  1810. "Eshell-Text"
  1811. "Text-mode for Eshell."
  1812. nil)
  1813. (defun eshell-text-mode-toggle ()
  1814. "Toggle eshell-text-mode and eshell-mode."
  1815. (interactive)
  1816. (cond ((eq major-mode
  1817. 'eshell-text-mode)
  1818. (goto-char (point-max))
  1819. (message "Eshell text mode disabled")
  1820. (eshell-mode))
  1821. ((eq major-mode
  1822. 'eshell-mode)
  1823. (message "Eshell text mode enabled")
  1824. (eshell-write-history)
  1825. (eshell-text-mode))
  1826. (t
  1827. (message "Not in eshell buffer")
  1828. nil)))
  1829. (defun my-eshell-backward-delete-char ()
  1830. (interactive)
  1831. (when (< (save-excursion
  1832. (eshell-bol)
  1833. (point))
  1834. (point))
  1835. (backward-delete-char 1)))
  1836. (defun my-file-owner-p (file)
  1837. "t if FILE is owned by me."
  1838. (eq (user-uid) (nth 2 (file-attributes file))))
  1839. "http://www.bookshelf.jp/pukiwiki/pukiwiki.php\
  1840. ?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9"
  1841. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1842. ;; (defun eshell/less (&rest args)
  1843. ;; "Invoke `view-file' on the file.
  1844. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1845. ;; (if args
  1846. ;; (while args
  1847. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1848. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1849. ;; (file (pop args)))
  1850. ;; (view-file file)
  1851. ;; (goto-line line))
  1852. ;; (view-file (pop args))))))
  1853. (defun eshell/o (&optional file)
  1854. (my-x-open (or file ".")))
  1855. ;; (defun eshell/vi (&rest args)
  1856. ;; "Invoke `find-file' on the file.
  1857. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1858. ;; (while args
  1859. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1860. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1861. ;; (file (pop args)))
  1862. ;; (find-file file)
  1863. ;; (goto-line line))
  1864. ;; (find-file (pop args)))))
  1865. (defun eshell/clear ()
  1866. "Clear the current buffer, leaving one prompt at the top."
  1867. (interactive)
  1868. (let ((inhibit-read-only t))
  1869. (erase-buffer)))
  1870. (defun eshell-clear ()
  1871. (interactive)
  1872. (let ((inhibit-read-only t))
  1873. (erase-buffer)
  1874. (insert (funcall eshell-prompt-function))))
  1875. (defun eshell/d (&optional dirname switches)
  1876. "if first arg is omitted open current directory."
  1877. (dired (or dirname ".") switches))
  1878. (defun eshell/v ()
  1879. (view-mode 1))
  1880. ;; (defun eshell/aaa (&rest args)
  1881. ;; (message "%S"
  1882. ;; args))
  1883. (defalias 'eshell/: 'ignore)
  1884. (defalias 'eshell/type 'eshell/which)
  1885. ;; (defalias 'eshell/vim 'eshell/vi)
  1886. (defalias 'eshell/ff 'find-file)
  1887. (defalias 'eshell/q 'eshell/exit)
  1888. (defun eshell-goto-prompt ()
  1889. ""
  1890. (interactive)
  1891. (goto-char (point-max)))
  1892. (defun eshell-delete-char-or-logout (n)
  1893. (interactive "p")
  1894. (if (equal (eshell-get-old-input)
  1895. "")
  1896. (progn
  1897. (insert "exit")
  1898. (eshell-send-input))
  1899. (delete-char n)))
  1900. (defun eshell-kill-input ()
  1901. (interactive)
  1902. (delete-region (point)
  1903. (progn (eshell-bol)
  1904. (point))))
  1905. (defalias 'eshell/logout 'eshell/exit)
  1906. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1907. "open eshell and change wd
  1908. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1909. (interactive)
  1910. (let ((dir (expand-file-name default-directory)))
  1911. (switch-to-buffer (or eshell-buffer-or-name
  1912. (eshell t)))
  1913. (unless (equal dir (expand-file-name default-directory))
  1914. ;; (cd dir)
  1915. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1916. ;; (eshell-emit-prompt)
  1917. (goto-char (point-max))
  1918. (eshell-kill-input)
  1919. (insert "cd " dir)
  1920. (eshell-send-input))))
  1921. (defadvice eshell-next-matching-input-from-input
  1922. ;; do not cycle history
  1923. (around eshell-history-do-not-cycle activate)
  1924. (if (= 0
  1925. (or eshell-history-index
  1926. 0))
  1927. (progn
  1928. (delete-region eshell-last-output-end (point))
  1929. (insert-and-inherit eshell-matching-input-from-input-string)
  1930. (setq eshell-history-index nil))
  1931. ad-do-it))
  1932. (defvar-set eshell-directory-name (concat user-emacs-directory
  1933. "eshell/"))
  1934. (defvar-set eshell-term-name "eterm-color")
  1935. (defvar-set eshell-scroll-to-bottom-on-input t)
  1936. (defvar-set eshell-cmpl-ignore-case t)
  1937. (defvar-set eshell-cmpl-cycle-completions nil)
  1938. (defvar-set eshell-highlight-prompt nil)
  1939. (if (eq system-type 'darwin)
  1940. (defvar-set eshell-ls-initial-args '("-hCFG")
  1941. (defvar-set eshell-ls-initial-args '("-hCFG"
  1942. "--color=auto"
  1943. "--time-style=long-iso")) ; "-hF")
  1944. ))
  1945. (defvar-set eshell-prompt-function
  1946. 'my-eshell-prompt-function)
  1947. (defvar eshell-last-command-status)
  1948. (defun my-eshell-prompt-function()
  1949. "Prompt function.
  1950. It looks like:
  1951. :: [10sr@darwin:~/][ESHELL]
  1952. :: $
  1953. "
  1954. (concat ":: ["
  1955. (let ((str (concat user-login-name
  1956. "@"
  1957. (car (split-string system-name
  1958. "\\."))
  1959. )))
  1960. (put-text-property 0
  1961. (length str)
  1962. 'face
  1963. 'underline
  1964. str)
  1965. str)
  1966. ":"
  1967. (let ((str (abbreviate-file-name default-directory)))
  1968. (put-text-property 0
  1969. (length str)
  1970. 'face
  1971. 'underline
  1972. str)
  1973. str)
  1974. "][ESHELL]\n:: "
  1975. (if (eq 0
  1976. eshell-last-command-status)
  1977. ""
  1978. (format "[STATUS:%d] "
  1979. eshell-last-command-status))
  1980. (if (= (user-uid)
  1981. 0)
  1982. "# "
  1983. "$ ")
  1984. ))
  1985. (add-hook 'eshell-mode-hook
  1986. (lambda ()
  1987. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1988. ;; (interactive)
  1989. ;; (switch-to-buffer (other-buffer))))
  1990. ;; (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1991. ;; (interactive)
  1992. ;; (eshell-goto-prompt)
  1993. ;; (keyboard-quit)))
  1994. (local-set-key (kbd "C-x t") 'eshell-text-mode-toggle)
  1995. (local-set-key (kbd "C-u") 'eshell-kill-input)
  1996. (local-set-key (kbd "C-d") 'eshell-delete-char-or-logout)
  1997. ;; (define-key eshell-mode-map (kbd "C-l")
  1998. ;; 'eshell-clear)
  1999. (local-set-key (kbd "DEL") 'my-eshell-backward-delete-char)
  2000. (local-set-key (kbd "<up>") 'scroll-down-line)
  2001. (local-set-key (kbd "<down>") 'scroll-up-line)
  2002. ;; (define-key eshell-mode-map
  2003. ;; (kbd "C-p") 'eshell-previous-matching-input-from-input)
  2004. ;; (define-key eshell-mode-map
  2005. ;; (kbd "C-n") 'eshell-next-matching-input-from-input)
  2006. (apply 'eshell/addpath exec-path)
  2007. (set (make-local-variable 'scroll-margin) 0)
  2008. ;; (eshell/export "GIT_PAGER=")
  2009. ;; (eshell/export "GIT_EDITOR=")
  2010. (eshell/export "LC_MESSAGES=C")
  2011. (switch-to-buffer (current-buffer)) ; move buffer top of list
  2012. (set (make-local-variable (defvar hl-line-range-function))
  2013. (lambda ()
  2014. '(0 . 0)))
  2015. (defvar eshell-virtual-targets)
  2016. (add-to-list 'eshell-virtual-targets
  2017. '("/dev/less"
  2018. (lambda (str)
  2019. (if str
  2020. (with-current-buffer nil)))
  2021. nil))
  2022. ))
  2023. (add-hook 'eshell-mode-hook
  2024. (lambda ()
  2025. (defvar eshell-visual-commands)
  2026. (defvar eshell-output-filter-functions)
  2027. (defvar eshell-command-aliases-list)
  2028. (add-to-list 'eshell-visual-commands "vim")
  2029. ;; (add-to-list 'eshell-visual-commands "git")
  2030. (add-to-list 'eshell-output-filter-functions
  2031. 'eshell-truncate-buffer)
  2032. (mapcar (lambda (alias)
  2033. (add-to-list 'eshell-command-aliases-list
  2034. alias))
  2035. '(
  2036. ;; ("ll" "ls -l $*")
  2037. ;; ("la" "ls -a $*")
  2038. ;; ("lla" "ls -al $*")
  2039. ("git" "git -c color.ui=always $*")
  2040. ("g" "git $*")
  2041. ("eless"
  2042. (concat "cat >>> (with-current-buffer "
  2043. "(get-buffer-create \"*eshell output\") "
  2044. "(erase-buffer) "
  2045. "(setq buffer-read-only nil) "
  2046. "(current-buffer)) "
  2047. "(view-buffer (get-buffer \"*eshell output*\"))"))
  2048. )
  2049. )))
  2050. ) ; eval after load eshell
  2051. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2052. ;; my-term
  2053. (defvar my-term nil
  2054. "My terminal buffer.")
  2055. (defvar my-term-function nil
  2056. "Function to create terminal buffer.
  2057. This function accept no argument and return newly created buffer of terminal.")
  2058. (defun my-term (&optional arg)
  2059. "Open terminal buffer and return that buffer.
  2060. If ARG is given or called with prefix argument, create new buffer."
  2061. (interactive "P")
  2062. (if (and (not arg)
  2063. my-term
  2064. (buffer-name my-term))
  2065. (pop-to-buffer my-term)
  2066. (setq my-term
  2067. (save-window-excursion
  2068. (funcall my-term-function)))
  2069. (and my-term
  2070. (my-term))))
  2071. ;; (setq my-term-function
  2072. ;; (lambda ()
  2073. ;; (if (eq system-type 'windows-nt)
  2074. ;; (eshell)
  2075. ;; (if (require 'multi-term nil t)
  2076. ;; (multi-term)
  2077. ;; (ansi-term shell-file-name)))))
  2078. (setq my-term-function (lambda () (eshell t)))
  2079. ;;(define-key my-prefix-map (kbd "C-s") 'my-term)
  2080. (define-key ctl-x-map "i" 'my-term)
  2081. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2082. ;; x open
  2083. (defvar my-filer nil)
  2084. (setq my-filer (or (executable-find "pcmanfm")
  2085. (executable-find "nautilus")))
  2086. (defun my-x-open (file)
  2087. "Open FILE."
  2088. (interactive "FOpen File: ")
  2089. (setq file (expand-file-name file))
  2090. (message "Opening %s..." file)
  2091. (cond ((eq system-type 'windows-nt)
  2092. (call-process "cmd.exe" nil 0 nil
  2093. "/c" "start" "" (convert-standard-filename file)))
  2094. ((eq system-type 'darwin)
  2095. (call-process "open" nil 0 nil file))
  2096. ((getenv "DISPLAY")
  2097. (call-process (or my-filer "xdg-open") nil 0 nil file))
  2098. (t
  2099. (find-file file))
  2100. )
  2101. ;; (recentf-add-file file)
  2102. (message "Opening %s...done" file))
  2103. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2104. ;; misc funcs
  2105. (defun my-git-apply-index-from-buffer (&optional buf)
  2106. "Git apply buffer. BUF is buffer to apply. nil to use current buffer."
  2107. (interactive)
  2108. (let ((buf (or buf
  2109. (current-buffer)))
  2110. (file (make-temp-file "git-apply-diff.emacs")))
  2111. (with-current-buffer buf
  2112. (write-region (point-min)
  2113. (point-max)
  2114. file)
  2115. (call-process "git"
  2116. nil
  2117. nil
  2118. nil
  2119. "apply"
  2120. "--cached"
  2121. file))))
  2122. (defun memo (&optional dir)
  2123. "Open memo.txt in DIR."
  2124. (interactive)
  2125. (pop-to-buffer (find-file-noselect (concat (if dir
  2126. (file-name-as-directory dir)
  2127. "")
  2128. "memo.txt"))))
  2129. (defvar my-rgrep-alist
  2130. `(
  2131. ;; the silver searcher
  2132. ("ag"
  2133. (executable-find "ag")
  2134. "ag --nocolor --nogroup --nopager --filename ")
  2135. ;; ack
  2136. ("ack"
  2137. (executable-find "ack")
  2138. "ack --nocolor --nogroup --nopager --with-filename ")
  2139. ;; gnu global
  2140. ("global"
  2141. (and (require 'gtags nil t)
  2142. (executable-find "global")
  2143. (gtags-get-rootpath))
  2144. "global --result grep ")
  2145. ;; git grep
  2146. ("gitgrep"
  2147. (eq 0
  2148. (shell-command "git rev-parse --git-dir"))
  2149. "git --no-pager -c color.grep=false grep -nH -e ")
  2150. ;; grep
  2151. ("grep"
  2152. t
  2153. ,(concat "find . "
  2154. "-path '*/.git' -prune -o "
  2155. "-path '*/.svn' -prune -o "
  2156. "-type f -print0 | "
  2157. "xargs -0 grep -nH -e "))
  2158. )
  2159. "Alist of rgrep command.
  2160. Each element is in the form like (NAME SEXP COMMAND), where SEXP returns the
  2161. condition to choose COMMAND when evaluated.")
  2162. (defvar my-rgrep-default nil
  2163. "Default command name for my-rgrep.")
  2164. (defun my-rgrep-grep-command (&optional name alist)
  2165. "Return recursive grep command for current directory or nil.
  2166. If NAME is given, use that without testing.
  2167. Commands are searched from ALIST."
  2168. (if alist
  2169. (if name
  2170. ;; if name is given search that from alist and return the command
  2171. (nth 2 (assoc name
  2172. alist))
  2173. ;; if name is not given try test in 1th elem
  2174. (let ((car (car alist))
  2175. (cdr (cdr alist)))
  2176. (if (eval (nth 1 car))
  2177. ;; if the condition is true return the command
  2178. (nth 2 car)
  2179. ;; try next one
  2180. (and cdr
  2181. (my-rgrep-grep-command name cdr)))))
  2182. ;; if alist is not given set default value
  2183. (my-rgrep-grep-command name my-rgrep-alist)))
  2184. (defun my-rgrep (command-args)
  2185. "My recursive grep. Run COMMAND-ARGS."
  2186. (interactive (let ((cmd (my-rgrep-grep-command my-rgrep-default
  2187. nil)))
  2188. (if cmd
  2189. (list (read-shell-command "grep command: "
  2190. cmd
  2191. 'grep-find-history))
  2192. (error "My-Rgrep: Command for rgrep not found")
  2193. )))
  2194. (compilation-start command-args
  2195. 'grep-mode))
  2196. ;; (defun my-rgrep-symbol-at-point (command-args)
  2197. ;; "My recursive grep. Run COMMAND-ARGS."
  2198. ;; (interactive (list (read-shell-command "grep command: "
  2199. ;; (concat (my-rgrep-grep-command)
  2200. ;; " "
  2201. ;; (thing-at-point 'symbol))
  2202. ;; 'grep-find-history)))
  2203. ;; (compilation-start command-args
  2204. ;; 'grep-mode))
  2205. (defmacro define-my-rgrep (name)
  2206. "Define rgrep for NAME."
  2207. `(defun ,(intern (concat "my-rgrep-"
  2208. name)) ()
  2209. ,(format "My recursive grep by %s."
  2210. name)
  2211. (interactive)
  2212. (let ((my-rgrep-default ,name))
  2213. (if (called-interactively-p 'any)
  2214. (call-interactively 'my-rgrep)
  2215. (error "Not intended to be called noninteractively. Use `my-rgrep'"))))
  2216. )
  2217. (define-my-rgrep "ack")
  2218. (define-my-rgrep "ag")
  2219. (define-my-rgrep "gitgrep")
  2220. (define-my-rgrep "grep")
  2221. (define-my-rgrep "global")
  2222. (define-key ctl-x-map "s" 'my-rgrep)
  2223. ;; (defun make ()
  2224. ;; "Run \"make -k\" in current directory."
  2225. ;; (interactive)
  2226. ;; (compile "make -k"))
  2227. (defalias 'make 'compile)
  2228. (define-key ctl-x-map "c" 'compile)
  2229. (defvar sed-in-place-history nil
  2230. "History of `sed-in-place'.")
  2231. (defvar sed-in-place-command "sed --in-place=.bak -e")
  2232. (defun sed-in-place (command)
  2233. "Issue sed in place COMMAND."
  2234. (interactive (list (read-shell-command "sed in place: "
  2235. (concat sed-in-place-command " ")
  2236. 'sed-in-place-history)))
  2237. (shell-command command
  2238. "*sed in place*"))
  2239. (defun dired-do-sed-in-place (&optional arg)
  2240. "Issue sed in place dired. If ARG is given, use the next ARG files."
  2241. (interactive "p")
  2242. (require 'dired-aux)
  2243. (let* ((files (dired-get-marked-files t arg))
  2244. (expr (dired-mark-read-string "Run sed-in-place for %s: "
  2245. nil
  2246. 'sed-in-place
  2247. arg
  2248. files)))
  2249. (if (equal expr
  2250. "")
  2251. (error "No expression specified")
  2252. (shell-command (concat sed-in-place-command
  2253. " '"
  2254. expr
  2255. "' "
  2256. (mapconcat 'shell-quote-argument
  2257. files
  2258. " "))
  2259. "*sed in place*"))))
  2260. (defun dir-show (&optional dir)
  2261. "Show DIR list."
  2262. (interactive)
  2263. (let ((bf (get-buffer-create "*dir show*"))
  2264. (list-directory-brief-switches "-C"))
  2265. (with-current-buffer bf
  2266. (list-directory (or nil
  2267. default-directory)
  2268. nil))
  2269. ))
  2270. (defun my-convmv-sjis2utf8-test ()
  2271. "Run `convmv -r -f sjis -t utf8 *'.
  2272. this is test, does not rename files."
  2273. (interactive)
  2274. (shell-command "convmv -r -f sjis -t utf8 *"))
  2275. (defun my-convmv-sjis2utf8-notest ()
  2276. "Run `convmv -r -f sjis -t utf8 * --notest'."
  2277. (interactive)
  2278. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  2279. (defun kill-ring-save-buffer-file-name ()
  2280. "Get current filename."
  2281. (interactive)
  2282. (let ((file buffer-file-name))
  2283. (if file
  2284. (progn (kill-new file)
  2285. (message file))
  2286. (message "not visiting file."))))
  2287. (defvar kill-ring-buffer-name "*kill-ring*"
  2288. "Buffer name for `kill-ring-buffer'.")
  2289. (defun open-kill-ring-buffer ()
  2290. "Open kill- ring buffer."
  2291. (interactive)
  2292. (pop-to-buffer
  2293. (with-current-buffer (get-buffer-create kill-ring-buffer-name)
  2294. (erase-buffer)
  2295. (yank)
  2296. (text-mode)
  2297. (current-local-map)
  2298. (goto-char (point-min))
  2299. (yank)
  2300. (current-buffer))))
  2301. (defun set-terminal-header (string)
  2302. "Set terminal header STRING."
  2303. (let ((savepos "\033[s")
  2304. (restorepos "\033[u")
  2305. (movecursor "\033[0;%dH")
  2306. (inverse "\033[7m")
  2307. (restorecolor "\033[0m")
  2308. (cols (frame-parameter nil 'width))
  2309. (length (length string)))
  2310. ;; (redraw-frame (selected-frame))
  2311. (send-string-to-terminal (concat savepos
  2312. (format movecursor
  2313. (1+ (- cols length)))
  2314. inverse
  2315. string
  2316. restorecolor
  2317. restorepos))
  2318. ))
  2319. (defun my-set-terminal-header ()
  2320. "Set terminal header."
  2321. (set-terminal-header (concat " "
  2322. user-login-name
  2323. "@"
  2324. (car (split-string system-name
  2325. "\\."))
  2326. " "
  2327. (format-time-string "%Y/%m/%d %T %z")
  2328. " ")))
  2329. ;; (run-with-timer
  2330. ;; 0.1
  2331. ;; 1
  2332. ;; 'my-set-terminal-header)
  2333. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  2334. ;; ;; savage emacs
  2335. ;; ;; when enabled emacs fails to complete
  2336. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  2337. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  2338. ;; (setq arg
  2339. ;; (concat arg
  2340. ;; (if (eq nil
  2341. ;; (string-match "\\. *$"
  2342. ;; arg))
  2343. ;; ".")
  2344. ;; " Stupid!")))
  2345. ;; TODO: make these a library
  2346. (defvar info-in-prompt
  2347. nil
  2348. "System info in the form of \"[user@host] \".")
  2349. (setq info-in-prompt
  2350. (concat "["
  2351. user-login-name
  2352. "@"
  2353. (car (split-string system-name
  2354. "\\."))
  2355. "]"))
  2356. (defun my-real-function-subr-p (function)
  2357. "Return t if FUNCTION is a built-in function even if it is advised."
  2358. (let* ((advised (and (symbolp function)
  2359. (featurep 'advice)
  2360. (ad-get-advice-info function)))
  2361. (real-function
  2362. (or (and advised (let ((origname (cdr (assq 'origname advised))))
  2363. (and (fboundp origname)
  2364. origname)))
  2365. function))
  2366. (def (if (symbolp real-function)
  2367. (symbol-function real-function)
  2368. function)))
  2369. (subrp def)))
  2370. ;; (my-real-function-subr-p 'my-real-function-subr-p)
  2371. ;; (defadvice read-from-minibuffer (before info-in-prompt activate)
  2372. ;; "Show system info when use `read-from-minibuffer'."
  2373. ;; (ad-set-arg 0
  2374. ;; (concat my-system-info
  2375. ;; (ad-get-arg 0))))
  2376. ;; (defadvice read-string (before info-in-prompt activate)
  2377. ;; "Show system info when use `read-string'."
  2378. ;; (ad-set-arg 0
  2379. ;; (concat my-system-info
  2380. ;; (ad-get-arg 0))))
  2381. ;; (when (< emacs-major-version 24)
  2382. ;; (defadvice completing-read (before info-in-prompt activate)
  2383. ;; "Show system info when use `completing-read'."
  2384. ;; (ad-set-arg 0
  2385. ;; (concat my-system-info
  2386. ;; (ad-get-arg 0)))))
  2387. (defmacro info-in-prompt-set (&rest functions)
  2388. "Set info-in-prompt advices for FUNCTIONS."
  2389. `(progn
  2390. ,@(mapcar (lambda (f)
  2391. `(defadvice ,f (before info-in-prompt activate)
  2392. "Show info in prompt."
  2393. (let ((orig (ad-get-arg 0)))
  2394. (unless (string-match-p (regexp-quote info-in-prompt)
  2395. orig)
  2396. (ad-set-arg 0
  2397. (concat info-in-prompt
  2398. " "
  2399. orig))))))
  2400. functions)))
  2401. (info-in-prompt-set read-from-minibuffer
  2402. read-string
  2403. completing-read)
  2404. ;;; emacs.el ends here