25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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