Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.
 
 
 
 
 
 

2726 рядки
94 KiB

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