Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 
 
 
 
 

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