You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

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