Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.

emacs.el 64 KiB

12 lat temu
13 lat temu
13 lat temu
13 lat temu
12 lat temu
12 lat temu
12 lat temu
13 lat temu
12 lat temu
12 lat temu
13 lat temu
13 lat temu
12 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
12 lat temu
12 lat temu
13 lat temu
13 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
12 lat temu
13 lat temu
12 lat temu
13 lat temu
13 lat temu
12 lat temu
12 lat temu
12 lat temu
13 lat temu
13 lat temu
13 lat temu
12 lat temu
12 lat temu
13 lat temu
13 lat temu
12 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
12 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
12 lat temu
13 lat temu
12 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
12 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
12 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
12 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
12 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
12 lat temu
12 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
13 lat temu
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791
  1. ;; load another file
  2. ;; (load-file "~/filepath")
  3. (unless (file-directory-p (expand-file-name user-emacs-directory))
  4. (make-directory (expand-file-name user-emacs-directory)))
  5. (let ((d (expand-file-name (concat user-emacs-directory
  6. "lisp"))))
  7. (unless (file-directory-p d)
  8. (make-directory d))
  9. (add-to-list 'load-path d))
  10. (require 'cl nil t)
  11. (progn
  12. (defvar buffer-file-changed-functions nil "Hook run when buffer file changed.
  13. Each function is called with two args, the filename before changing and after changing.")
  14. (declare-function run-buffer-file-changed-functions "emacs.el")
  15. (add-hook 'post-command-hook
  16. 'run-buffer-file-changed-functions)
  17. (lexical-let (previous-file)
  18. (defun run-buffer-file-changed-functions ()
  19. ""
  20. (unless (and previous-file
  21. (equal previous-file
  22. (expand-file-name (or buffer-file-name
  23. default-directory))))
  24. (let ((pfile previous-file)
  25. (cfile (expand-file-name (or buffer-file-name
  26. default-directory))))
  27. (setq previous-file cfile)
  28. (run-hook-with-args 'buffer-file-changed-functions pfile cfile)))))
  29. ;; (add-hook 'buffer-file-changed-function
  30. ;; (lambda (pdir cdir)
  31. ;; (message "dir changed %s to %s !" pdir cdir)))
  32. )
  33. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  34. ;; download library from web
  35. (require 'url)
  36. (defun dllib-if-unfound (url &optional byte-compile-p force-download-p)
  37. "If library does not exist, download it from URL and locate it in \"~/emacs.d/lisp/\".
  38. Return nil if library unfound and failed to download, otherwise the path where the library installed."
  39. (let* ((dir (expand-file-name (concat user-emacs-directory "lisp/")))
  40. (lib (file-name-sans-extension (file-name-nondirectory url)))
  41. (lpath (concat dir lib ".el"))
  42. (locate-p (locate-library lib)))
  43. (if (or force-download-p (not locate-p))
  44. (progn (condition-case nil
  45. (progn (message "downloading %s..." url)
  46. (url-copy-file url
  47. lpath
  48. t)
  49. (when (and byte-compile-p
  50. (require 'bytecomp nil t))
  51. (and (file-exists-p (byte-compile-dest-file lpath))
  52. (delete-file (byte-compile-dest-file lpath)))
  53. (byte-compile-file lpath))
  54. )
  55. (error (and (file-writable-p lpath)
  56. (delete-file lpath))
  57. (message "downloading %s...something wrong happened!" url)
  58. nil))
  59. (locate-library lib))
  60. locate-p)))
  61. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  62. ;; start and quit
  63. (setq inhibit-startup-message t)
  64. (setq frame-title-format (list '(:eval (format-time-string (or display-time-format "")))
  65. " | %b "
  66. '(:eval (number-to-string (length (buffer-list-not-start-with-space))))
  67. " buffers ["
  68. invocation-name
  69. " "
  70. emacs-version
  71. " "
  72. (symbol-name system-type)
  73. "] "
  74. '(:eval (symbol-name last-command))))
  75. (setq set-terminal-title-regexp "^\\(rxvt\\|xterm\\|aterm$\\|screen\\)")
  76. (defun set-terminal-title (&rest args)
  77. ""
  78. (interactive "sString to set as title: ")
  79. (let ((tty (frame-parameter nil
  80. 'tty-type)))
  81. (when (and tty
  82. (string-match set-terminal-title-regexp
  83. tty))
  84. (send-string-to-terminal (apply 'concat
  85. "\033]0;"
  86. `(,@args "\007"))))))
  87. (defun my-set-terminal-title ()
  88. ""
  89. (set-terminal-title "["
  90. invocation-name
  91. " "
  92. emacs-version
  93. " "
  94. (symbol-name system-type)
  95. "] "
  96. (abbreviate-file-name (or buffer-file-name default-directory))))
  97. (add-hook 'buffer-file-changed-functions
  98. (lambda (p c)
  99. (my-set-terminal-title)))
  100. (add-hook 'suspend-resume-hook
  101. 'my-set-terminal-title)
  102. (defun buffer-list-not-start-with-space ()
  103. (let ((bl (buffer-list))
  104. b nbl)
  105. (while bl
  106. (setq b (pop bl))
  107. (unless (string-equal " "
  108. (substring (buffer-name b)
  109. 0
  110. 1))
  111. (add-to-list 'nbl b)))
  112. nbl))
  113. (setq confirm-kill-emacs 'y-or-n-p)
  114. (setq gc-cons-threshold (* 1024 1024 4))
  115. (when window-system
  116. (add-to-list 'default-frame-alist '(cursor-type . box))
  117. (add-to-list 'default-frame-alist '(background-color . "white"))
  118. (add-to-list 'default-frame-alist '(foreground-color . "gray10"))
  119. ;; (add-to-list 'default-frame-alist '(alpha . (80 100 100 100))) ; does not work?
  120. )
  121. ;; (add-to-list 'default-frame-alist '(cursor-type . box))
  122. (if window-system (menu-bar-mode 1) (menu-bar-mode 0))
  123. (tool-bar-mode 0)
  124. (set-scroll-bar-mode nil)
  125. (add-hook 'kill-emacs-hook ; load when exitting to examine if file is written properly
  126. (lambda ()
  127. (when (file-readable-p "~/.emacs")
  128. (load-file "~/.emacs"))
  129. ))
  130. (add-hook 'after-init-hook
  131. (lambda ()
  132. (message (emacs-init-time))
  133. (switch-to-buffer "*Messages*")
  134. ))
  135. (cd ".") ; when using windows use / instead of \ in `default-directory'
  136. ;; locale
  137. (set-language-environment "Japanese")
  138. (set-default-coding-systems 'utf-8-unix)
  139. (prefer-coding-system 'utf-8-unix)
  140. (setq system-time-locale "C")
  141. ;; my prefix map
  142. (define-prefix-command 'my-prefix-map)
  143. '(add-hook 'after-init-hook
  144. (lambda ()
  145. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  146. ))
  147. (define-key ctl-x-map (kbd "C-x") 'my-prefix-map)
  148. (define-key my-prefix-map (kbd "C-q") 'quoted-insert)
  149. (define-key my-prefix-map (kbd "C-z") 'suspend-frame)
  150. ;; (comint-show-maximum-output)
  151. ;; kill scratch
  152. (add-hook 'after-init-hook
  153. (lambda ()
  154. (kill-buffer "*scratch*")))
  155. ;; modifier keys
  156. (setq mac-option-modifier 'control)
  157. (setq w32-apps-modifier 'meta)
  158. ;; display
  159. (setq redisplay-dont-pause t)
  160. (setq visible-bell t)
  161. (setq ring-bell-function 'ignore)
  162. (mouse-avoidance-mode 'banish)
  163. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  164. ;; global keys
  165. (and (dllib-if-unfound "https://raw.github.com/k1LoW/emacs-drill-instructor/master/drill-instructor.el"
  166. t)
  167. (require 'drill-instructor nil t)
  168. (setq drill-instructor-global t)
  169. (let ((map drill-instructor-key-map))
  170. (define-key map (kbd "RET") nil)
  171. (define-key map (kbd "DEL") nil)
  172. (define-key map (kbd "C-h") nil)))
  173. '(mapc (lambda (key)
  174. (global-set-key (read-kbd-macro key) 'ignore))
  175. '("<up>" "<down>" "<right>" "<left>"))
  176. (define-key my-prefix-map (kbd "C-o") 'occur)
  177. ;; (define-key my-prefix-map (kbd "C-h") help-map)
  178. (global-set-key (kbd "C-\\") help-map)
  179. (define-key ctl-x-map (kbd "DEL") help-map)
  180. (define-key ctl-x-map (kbd "C-h") help-map)
  181. (define-key help-map "a" 'apropos)
  182. ;; compose window
  183. (global-set-key [?\C--] 'other-window)
  184. (global-set-key [?\C-0] 'delete-window)
  185. (global-set-key [?\C-1] 'delete-other-windows)
  186. (global-set-key [?\C-2] 'split-window-vertically)
  187. (global-set-key [?\C-3] 'split-window-horizontally)
  188. ;; disable annoying keys
  189. (global-set-key [prior] 'ignore)
  190. (global-set-key (kbd "<next>") 'ignore)
  191. (global-set-key [menu] 'ignore)
  192. (global-set-key [down-mouse-1] 'ignore)
  193. (global-set-key [down-mouse-2] 'ignore)
  194. (global-set-key [down-mouse-3] 'ignore)
  195. (global-set-key [mouse-1] 'ignore)
  196. (global-set-key [mouse-2] 'ignore)
  197. (global-set-key [mouse-3] 'ignore)
  198. (global-set-key (kbd "<eisu-toggle>") 'ignore)
  199. (global-set-key (kbd "C-<eisu-toggle>") 'ignore)
  200. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  201. ;; mode-line
  202. (setq eol-mnemonic-dos "crlf")
  203. (setq eol-mnemonic-mac "cr")
  204. (setq eol-mnemonic-unix "lf")
  205. (which-function-mode 0)
  206. (line-number-mode 0)
  207. (column-number-mode 0)
  208. (size-indication-mode 0)
  209. (setq mode-line-position
  210. '(:eval (format "L%%l/%d,C%%c"
  211. (count-lines (point-max)
  212. (point-min)))))
  213. ;; http://www.geocities.jp/simizu_daisuke/bunkei-meadow.html#frame-title
  214. ;; display date
  215. (add-hook 'after-init-hook
  216. (lambda ()
  217. (when display-time-mode
  218. (display-time-update))
  219. ))
  220. (setq display-time-interval 29)
  221. (setq display-time-day-and-date t)
  222. (setq display-time-format "%a, %d %b %Y %T")
  223. (if window-system
  224. (display-time-mode 0)
  225. (display-time-mode 1))
  226. ;; ;; current directory
  227. ;; (let ((ls (member 'mode-line-buffer-identification
  228. ;; mode-line-format)))
  229. ;; (setcdr ls
  230. ;; (cons '(:eval (concat " ("
  231. ;; (abbreviate-file-name default-directory)
  232. ;; ")"))
  233. ;; (cdr ls))))
  234. ;; ;; display last modified time
  235. ;; (let ((ls (member 'mode-line-buffer-identification
  236. ;; mode-line-format)))
  237. ;; (setcdr ls
  238. ;; (cons '(:eval (concat " "
  239. ;; my-buffer-file-last-modified-time))
  240. ;; (cdr ls))))
  241. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  242. ;; minibuffer
  243. (setq insert-default-directory t)
  244. (setq completion-ignore-case t
  245. read-file-name-completion-ignore-case t
  246. read-buffer-completion-ignore-case t)
  247. (setq resize-mini-window t)
  248. (temp-buffer-resize-mode 1)
  249. (savehist-mode 1)
  250. (fset 'yes-or-no-p 'y-or-n-p)
  251. (define-key read-expression-map (kbd "TAB") 'lisp-complete-symbol) ; complete symbol when `eval'
  252. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  253. ;; letters, font-lock mode and fonts
  254. ;; (set-face-background 'vertical-border (face-foreground 'mode-line))
  255. (when (eq system-type 'Darwin)
  256. (mac-set-input-method-parameter 'japanese 'cursor-color "red")
  257. (mac-set-input-method-parameter 'roman 'cursor-color "black"))
  258. (when (and (boundp 'input-method-activate-hook) ;ちょっと正しいかわかんない
  259. (boundp 'input-method-inactivate-hook))
  260. (add-hook 'input-method-activate-hook
  261. (lambda () (set-cursor-color "red")))
  262. (add-hook 'input-method-inactivate-hook
  263. (lambda () (set-cursor-color "black"))))
  264. (show-paren-mode 1)
  265. (setq show-paren-style 'mixed)
  266. (transient-mark-mode 1)
  267. (global-font-lock-mode 1)
  268. (setq font-lock-global-modes
  269. '(not
  270. help-mode
  271. eshell-mode
  272. term-mode
  273. Man-mode))
  274. (standard-display-ascii ?\n "$\n")
  275. (defvar my-eol-face
  276. '(("\n" . (0 font-lock-comment-face t nil)))
  277. )
  278. (defvar my-tab-face
  279. '(("\t" . '(0 highlight t nil))))
  280. (defvar my-jspace-face
  281. '(("\u3000" . '(0 highlight t nil))))
  282. (add-hook 'font-lock-mode-hook
  283. (lambda ()
  284. (font-lock-add-keywords nil my-eol-face)
  285. (font-lock-add-keywords nil my-jspace-face)
  286. ))
  287. ;; highlight current line
  288. ;; http://wiki.riywo.com/index.php?Meadow
  289. (defface hlline-face
  290. '((((type x w32)
  291. (class color)
  292. (background dark))
  293. (:background "midnightblue")) ; :foreground "white")) ;; ハイライトの文字色は変えない方がいいかも
  294. (((type x w32)
  295. (class color)
  296. (background light))
  297. (:background "gainsboro"))
  298. (t
  299. (:underline "black")))
  300. "*Face used by hl-line.")
  301. ;; (defface hlline-ul-face
  302. ;; '((t (:underline "yellow")))
  303. ;; "underline yellow")
  304. (setq hl-line-face 'hlline-face) ;; (setq hl-line-face nil)
  305. (global-hl-line-mode 1) ;; (hl-line-mode 1)
  306. (setq hl-line-global-modes
  307. '(not
  308. term-mode))
  309. (set-face-foreground 'font-lock-regexp-grouping-backslash "#666")
  310. (set-face-foreground 'font-lock-regexp-grouping-construct "#f60")
  311. ;; fonts
  312. (defun my-set-ascii-and-jp-font-with-size (list)
  313. ""
  314. (if (> emacs-major-version 22)
  315. (progn ; 23 or later
  316. (set-face-attribute 'default nil
  317. :family (nth 0 list)
  318. :height (nth 1 list))
  319. (set-fontset-font "fontset-default"
  320. 'japanese-jisx0208
  321. (font-spec :family (nth 2 list) :size (nth 3 list)))
  322. (set-fontset-font "fontset-default"
  323. 'katakana-jisx0201
  324. (font-spec :family (nth 2 list) :size (nth 3 list))))
  325. ; font spec is available in emacs23 and later, cannot used in emacs22
  326. (progn ; 22
  327. (set-face-attribute 'default nil
  328. :family (nth 0 list)
  329. :height (nth 1 list))
  330. (set-fontset-font "fontset-default"
  331. 'japanese-jisx0208
  332. (cons (nth 2 list) "jisx0208.*"))
  333. (set-fontset-font "fontset-default"
  334. 'katakana-jisx0201
  335. (cons (nth 2 list) "jisx0201.*"))
  336. )))
  337. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 90 "takaogothic" 13))
  338. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "takaogothic" 14))
  339. ;; (my-set-ascii-and-jp-font-with-size '("dejavu sans mono" 100 "ms gothic" 14))
  340. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 75 "takaogothic" 11))
  341. ;; (my-set-ascii-and-jp-font-with-size '("monaco" 90 "takaogothic" 13))
  342. ;; (my-set-ascii-and-jp-font-with-size '("ProggyCleanTTSZ" 120 "takaogothic" 11))
  343. ;; あ a
  344. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/set-modeline-color.el"
  345. t)
  346. (progn
  347. (require 'set-modeline-color nil t)))
  348. (let ((fg (face-foreground 'default))
  349. (bg (face-background 'default)))
  350. (set-face-background 'mode-line-inactive (if (face-inverse-video-p 'mode-line) fg bg))
  351. (set-face-foreground 'mode-line-inactive (if (face-inverse-video-p 'mode-line) bg fg)))
  352. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  353. ;; file handling
  354. (setq revert-without-query '(".+"))
  355. ;; save cursor position
  356. (setq save-place-file (concat user-emacs-directory
  357. "places"))
  358. (when (require 'saveplace nil t)
  359. (setq-default save-place t))
  360. ;; http://www.bookshelf.jp/soft/meadow_24.html#SEC260
  361. (setq make-backup-files t)
  362. ;; (make-directory (expand-file-name "~/.emacsbackup"))
  363. (setq backup-directory-alist
  364. (cons (cons "\\.*$" (expand-file-name "~/.emacs.d/backup"))
  365. backup-directory-alist))
  366. (setq version-control 'never)
  367. (setq delete-old-versions t)
  368. (setq auto-save-list-file-prefix (expand-file-name "~/.emacs.d/autosave/"))
  369. (setq delete-auto-save-files t)
  370. (add-to-list 'completion-ignored-extensions ".bak")
  371. ;; (setq delete-by-moving-to-trash t
  372. ;; trash-directory "~/.emacs.d/trash")
  373. (add-hook 'after-save-hook
  374. 'executable-make-buffer-file-executable-if-script-p)
  375. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  376. ;; editting
  377. (setq require-final-newline t)
  378. (setq kill-whole-line t)
  379. (setq scroll-conservatively 35
  380. scroll-margin 2
  381. scroll-step 0)
  382. (setq default-major-mode 'text-mode)
  383. (setq next-line-add-newlines nil)
  384. (setq kill-read-only-ok t)
  385. (setq truncate-partial-width-windows nil) ; when splitted horizontally
  386. ;; (setq-default line-spacing 0.2)
  387. (setq-default indicate-empty-lines t) ; when using x indicate empty line
  388. (setq-default tab-width 4)
  389. (setq-default indent-tabs-mode nil)
  390. (setq-default indent-line-function nil)
  391. ;(pc-selection-mode 1) ; this make some already defined keybind back to default
  392. (delete-selection-mode 1)
  393. (cua-mode 0)
  394. (setq line-move-visual nil)
  395. ;; key bindings
  396. ;; moving around
  397. ;; (global-set-key (kbd "M-j") 'next-line)
  398. ;; (global-set-key (kbd "M-k") 'previous-line)
  399. ;; (global-set-key (kbd "M-h") 'backward-char)
  400. ;; (global-set-key (kbd "M-l") 'forward-char)
  401. ;;(keyboard-translate ?\M-j ?\C-j)
  402. ;; (global-set-key (kbd "M-p") 'backward-paragraph)
  403. (define-key esc-map "p" 'backward-paragraph)
  404. ;; (global-set-key (kbd "M-n") 'forward-paragraph)
  405. (define-key esc-map "n" 'forward-paragraph)
  406. (global-set-key (kbd "C-<up>") (lambda () (interactive)(scroll-down 1)))
  407. (global-set-key (kbd "C-<down>") (lambda () (interactive)(scroll-up 1)))
  408. (global-set-key (kbd "C-<left>") 'scroll-down)
  409. (global-set-key (kbd "C-<right>") 'scroll-up)
  410. (global-set-key (kbd "<select>") 'ignore) ; 'previous-line-mark)
  411. (define-key ctl-x-map (kbd "ESC x") 'execute-extended-command)
  412. (define-key ctl-x-map (kbd "ESC :") 'eval-expression)
  413. ;; C-h and DEL
  414. (global-set-key (kbd "C-h") (kbd "DEL"))
  415. (global-set-key (kbd "C-m") 'reindent-then-newline-and-indent)
  416. (global-set-key (kbd "C-o")
  417. ;; (lambda ()
  418. ;; (interactive)
  419. ;; (move-end-of-line nil)
  420. ;; (newline-and-indent))
  421. (kbd "C-e C-m")
  422. )
  423. (global-set-key (kbd "C-k") 'kill-whole-line)
  424. (global-set-key (kbd "M-k") 'my-copy-whole-line)
  425. ;; (global-set-key "\C-z" 'undo) ; undo is M-u
  426. (global-set-key (kbd "M-u") 'undo)
  427. (global-set-key (kbd "C-r") 'query-replace-regexp)
  428. (global-set-key (kbd "C-s") 'isearch-forward-regexp)
  429. (global-set-key (kbd "M-i") (kbd "ESC TAB"))
  430. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  431. ;; gmail
  432. (setq mail-interactive t
  433. send-mail-function 'smtpmail-send-it
  434. ;; message-send-mail-function 'smtpmail-send-it
  435. smtpmail-smtp-server "smtp.gmail.com"
  436. smtpmail-smtp-service 587
  437. smtpmail-starttls-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  438. smtpmail-auth-credentials '(("smtp.gmail.com" 587 "8.slashes@gmail.com" nil))
  439. user-mail-address "8.slashes@gmail.com")
  440. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  441. ;; buffer killing
  442. (defun my-delete-window-killing-buffer () nil)
  443. (defun my-query-kill-this-buffer ()
  444. ""
  445. (interactive)
  446. (if (y-or-n-p (concat "kill this buffer? :"))
  447. (kill-buffer (current-buffer))))
  448. (substitute-key-definition 'kill-buffer 'my-query-kill-this-buffer global-map)
  449. ;;(global-set-key "\C-xk" 'my-query-kill-this-buffer)
  450. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  451. ;; package
  452. '(setq package-archives '(("ELPA" . "http://tromey.com/elpa/")
  453. ("gnu" . "http://elpa.gnu.org/packages/")
  454. ("marmalade" . "http://marmalade-repo.org/packages/")))
  455. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  456. ;; some modes and hooks
  457. (require 'simple nil t)
  458. (and window-system
  459. (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/save-window-size.el" t)
  460. (require 'save-window-size nil t))
  461. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  462. ;; share clipboard with x
  463. ;; this page describes this in details, but only these sexps are needed
  464. ;; http://garin.jp/doc/Linux/xwindow_clipboard
  465. (when (and window-system
  466. (not (eq window-system 'mac))
  467. )
  468. (setq x-select-enable-clipboard t ; these settings seems to be useless when using emacs in terminal
  469. x-select-enable-primary nil)
  470. ;; (global-set-key "\C-y" 'x-clipboard-yank)
  471. )
  472. (and (not x-select-enable-clipboard)
  473. (getenv "DISPLAY")
  474. (executable-find "xclip")
  475. (dllib-if-unfound "http://www.emacswiki.org/emacs/download/xclip.el" t)
  476. (require 'xclip nil t)
  477. (turn-on-xclip))
  478. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  479. ;; mode
  480. (add-hook 'verilog-mode-hook
  481. (lambda ()
  482. (define-key verilog-mode-map ";" 'self-insert-command)))
  483. (setq diff-switches "-u")
  484. (add-hook 'diff-mode-hook
  485. (lambda ()
  486. (view-mode 1)
  487. (set-face-foreground 'diff-file-header-face nil)
  488. (set-face-foreground 'diff-header-face nil)
  489. (set-face-foreground 'diff-index-face "blue")
  490. (set-face-foreground 'diff-hunk-header-face "cyan")
  491. (set-face-foreground 'diff-context-face nil)
  492. (set-face-foreground 'diff-removed-face "red")
  493. (set-face-foreground 'diff-added-face "green")
  494. (set-face-foreground 'diff-changed-face "magenda")
  495. ))
  496. ;; (ffap-bindings)
  497. (add-hook 'sh-mode-hook
  498. (lambda ()
  499. (define-key sh-mode-map (kbd "C-x C-e") 'my-execute-shell-command-current-line)))
  500. (defun my-execute-shell-command-current-line ()
  501. ""
  502. (interactive)
  503. (shell-command (buffer-substring-no-properties (point-at-bol)
  504. (point))))
  505. ;; (add-to-list 'auto-mode-alist
  506. ;; '("\\(xinitrc\\|xprograms\\)\\'" . sh-mode))
  507. (setq auto-mode-alist
  508. `(("autostart\\'" . sh-mode)
  509. ("xinitrc\\'" . sh-mode)
  510. ("xprograms\\'" . sh-mode)
  511. ,@auto-mode-alist))
  512. (setq python-python-command (or (executable-find "python3")
  513. (executable-find "python")))
  514. (defun my-python-run-as-command ()
  515. ""
  516. (interactive)
  517. (shell-command (concat python-python-command " " buffer-file-name)))
  518. (defun my-python-display-python-buffer ()
  519. ""
  520. (interactive)
  521. (set-window-text-height (display-buffer python-buffer
  522. t)
  523. 7))
  524. (add-hook 'python-mode-hook
  525. (lambda ()
  526. (define-key python-mode-map (kbd "C-c C-e") 'my-python-run-as-command)
  527. (define-key python-mode-map (kbd "C-c C-b") 'my-python-display-python-buffer)
  528. (define-key python-mode-map (kbd "C-m") 'newline-and-indent)))
  529. (add-hook 'inferior-python-mode-hook
  530. (lambda ()
  531. (my-python-display-python-buffer)
  532. (define-key inferior-python-mode-map (kbd "<up>") 'comint-previous-input)
  533. (define-key inferior-python-mode-map (kbd "<down>") 'comint-next-input)))
  534. (add-hook 'text-mode-hook
  535. (lambda ()
  536. (define-key text-mode-map (kbd "C-m") 'newline)))
  537. (add-to-list 'Info-default-directory-list (expand-file-name "~/.info/emacs-ja"))
  538. (setq bookmark-default-file "~/.emacs.d/bmk")
  539. (add-hook 'apropos-mode-hook
  540. (lambda ()
  541. (define-key apropos-mode-map "n" 'next-line)
  542. (define-key apropos-mode-map "p" 'previous-line)
  543. ))
  544. (define-key minibuffer-local-map (kbd "C-u") (lambda () (interactive) (delete-region (point-at-bol) (point))))
  545. (add-hook 'isearch-mode-hook
  546. (lambda ()
  547. ;; (define-key isearch-mode-map (kbd "C-j") 'isearch-other-control-char)
  548. ;; (define-key isearch-mode-map (kbd "C-k") 'isearch-other-control-char)
  549. ;; (define-key isearch-mode-map (kbd "C-h") 'isearch-other-control-char)
  550. (define-key isearch-mode-map (kbd "C-h") 'isearch-delete-char)
  551. (define-key isearch-mode-map (kbd "M-r") 'isearch-query-replace-regexp)))
  552. (add-hook 'outline-mode-hook
  553. (lambda ()
  554. (if (string-match "\\.md\\'" buffer-file-name)
  555. (set (make-local-variable 'outline-regexp) "#+ "))))
  556. (add-to-list 'auto-mode-alist (cons "\\.ol\\'" 'outline-mode))
  557. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'outline-mode))
  558. (setq markdown-command (or (executable-find "markdown")
  559. (executable-find "markdown.pl")))
  560. (when (dllib-if-unfound "http://jblevins.org/projects/markdown-mode/markdown-mode.el"
  561. t)
  562. (add-to-list 'auto-mode-alist (cons "\\.md\\'" 'markdown-mode))
  563. (autoload 'markdown-mode "markdown-mode" "Major mode for editing Markdown files." nil)
  564. (add-hook 'markdown-mode-hook
  565. (lambda ()
  566. (outline-minor-mode 1)
  567. (set (make-local-variable 'comment-start) ";"))))
  568. ;; http://d.hatena.ne.jp/emergent/20070203/1170512717
  569. ;; c-mode
  570. ;; (setq c-default-style "bsd")
  571. ;; BackSpace キーを「賢く」し,インデント幅は2桁,タブはスペースに展開
  572. (add-hook 'c-mode-common-hook
  573. (lambda ()
  574. (setq c-basic-offset 2
  575. indent-tabs-mode nil)
  576. ;; (set-face-foreground 'font-lock-keyword-face "blue")
  577. (c-toggle-hungry-state 1)
  578. ))
  579. (when (dllib-if-unfound "https://raw.github.com/mooz/js2-mode/master/js2-mode.el"
  580. t)
  581. (autoload 'js2-mode "js2-mode" nil t)
  582. (add-to-list 'auto-mode-alist '("\\.js\\'" . js2-mode))
  583. (add-to-list 'auto-mode-alist '("\\.jsm\\'" . js2-mode)))
  584. ;; (add-hook 'js2-mode-hook
  585. ;; (lambda ()
  586. ;; (add-hook 'before-save-hook
  587. ;; 'my-indent-buffer
  588. ;; nil
  589. ;; t)))
  590. (add-hook 'js2-mode-hook
  591. (lambda ()
  592. (define-key js2-mode-map (kbd "C-m") (lambda ()
  593. (interactive)
  594. (js2-enter-key)
  595. (indent-for-tab-command)))
  596. (add-hook (kill-local-variable 'before-save-hook)
  597. 'js2-before-save)))
  598. (and nil
  599. (require 'zone nil t)
  600. (not (eq system-type 'windows-nt))
  601. ;; (zone-when-idle 180)
  602. (run-with-idle-timer 180 t (lambda ()
  603. (unless (memq major-mode
  604. '(term-mode))
  605. (zone)))))
  606. (when (require 'uniquify nil t)
  607. (setq uniquify-buffer-name-style 'post-forward-angle-brackets)
  608. (setq uniquify-ignore-buffers-re "*[^*]+*")
  609. (setq uniquify-min-dir-content 1))
  610. (add-hook 'view-mode-hook
  611. (lambda()
  612. (define-key view-mode-map "j" (lambda() (interactive) (scroll-up 1)))
  613. (define-key view-mode-map "k" (lambda() (interactive) (scroll-down 1)))
  614. (define-key view-mode-map "v" 'toggle-read-only)
  615. (define-key view-mode-map "q" 'bury-buffer)
  616. ;; (define-key view-mode-map "/" 'nonincremental-re-search-forward)
  617. ;; (define-key view-mode-map "?" 'nonincremental-re-search-backward)
  618. ;; (define-key view-mode-map "n" 'nonincremental-repeat-search-forward)
  619. ;; (define-key view-mode-map "N" 'nonincremental-repeat-search-backward)
  620. (define-key view-mode-map "/" 'isearch-forward-regexp)
  621. (define-key view-mode-map "?" 'isearch-backward-regexp)
  622. (define-key view-mode-map "n" 'isearch-repeat-forward)
  623. (define-key view-mode-map "N" 'isearch-repeat-backward)
  624. ))
  625. (global-set-key "\M-r" 'view-mode)
  626. (setq view-read-only t)
  627. (add-hook 'Man-mode-hook
  628. (lambda ()
  629. (view-mode 1)
  630. (setq truncate-lines nil)))
  631. (setq Man-notify-method (if window-system
  632. 'newframe
  633. 'pushy))
  634. (require 'session nil t)
  635. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/gtkbm.el"
  636. t)
  637. (require 'gtkbm nil t)
  638. (global-set-key (kbd "C-x C-d") 'gtkbm))
  639. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/git-command.el"
  640. t)
  641. (require 'git-command nil t)
  642. (define-key ctl-x-map "g" 'git-command))
  643. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  644. ;; term mode
  645. ;; (setq multi-term-program shell-file-name)
  646. (and (dllib-if-unfound "http://www.emacswiki.org/emacs/download/multi-term.el"
  647. t)
  648. (require 'multi-term nil t)
  649. (setq multi-term-switch-after-close nil))
  650. (defun my-term-quit-or-send-raw ()
  651. ""
  652. (interactive)
  653. (if (get-buffer-process (current-buffer))
  654. (call-interactively 'term-send-raw)
  655. (kill-buffer)))
  656. ;; http://d.hatena.ne.jp/goinger/20100416/1271399150
  657. ;; (setq term-ansi-default-program shell-file-name)
  658. (add-hook 'term-setup-hook
  659. (lambda ()
  660. (setq term-display-table (make-display-table))))
  661. (add-hook 'term-mode-hook
  662. (lambda ()
  663. (unless (memq (current-buffer) (and (featurep 'multi-term) ; current buffer is not multi-term buffer
  664. (multi-term-list)))
  665. ;; (define-key term-raw-map "\C-q" 'move-beginning-of-line)
  666. ;; (define-key term-raw-map "\C-r" 'term-send-raw)
  667. ;; (define-key term-raw-map "\C-s" 'term-send-raw)
  668. ;; (define-key term-raw-map "\C-f" 'forward-char)
  669. ;; (define-key term-raw-map "\C-b" 'backward-char)
  670. ;; (define-key term-raw-map "\C-t" 'set-mark-command)
  671. (define-key term-raw-map "\C-x" (lookup-key (current-global-map) "\C-x"))
  672. (define-key term-raw-map "\C-z" (lookup-key (current-global-map) "\C-z")))
  673. (define-key term-raw-map (kbd "C-p") 'term-send-raw)
  674. (define-key term-raw-map (kbd "C-n") 'term-send-raw)
  675. (define-key term-raw-map "q" 'my-term-quit-or-send-raw)
  676. ;; (define-key term-raw-map (kbd "ESC") 'term-send-raw)
  677. (define-key term-raw-map [delete] 'term-send-raw)
  678. (define-key term-raw-map (kbd "DEL") 'term-send-backspace)
  679. (define-key term-raw-map "\C-y" 'term-paste)
  680. (define-key term-raw-map "\C-c" 'term-send-raw) ;; 'term-interrupt-subjob)
  681. '(define-key term-mode-map (kbd "C-x C-q") 'term-pager-toggle)
  682. ;; (dolist (key '("<up>" "<down>" "<right>" "<left>"))
  683. ;; (define-key term-raw-map (read-kbd-macro key) 'term-send-raw))
  684. ;; (define-key term-raw-map "\C-d" 'delete-char)
  685. (set (make-local-variable 'scroll-margin) 0)
  686. ;; (set (make-local-variable 'cua-enable-cua-keys) nil)
  687. ;; (cua-mode 0)
  688. ;; (and cua-mode
  689. ;; (local-unset-key (kbd "C-c")))
  690. ;; (define-key cua--prefix-override-keymap "\C-c" 'term-interrupt-subjob)
  691. (set (make-local-variable 'hl-line-range-function)
  692. (lambda ()
  693. '(0 . 0)))
  694. ))
  695. ;; (add-hook 'term-exec-hook 'forward-char)
  696. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  697. ;; buffer switching
  698. (when (require 'bs nil t)
  699. ;; (global-set-key "\C-x\C-b" 'bs-show)
  700. (defalias 'list-buffers 'bs-show))
  701. ;; (add-to-list 'bs-configurations '("processes" nil get-buffer-process ".*" nil nil))
  702. (add-to-list 'bs-configurations '("same-dir" nil buffer-same-dir-p ".*" nil nil))
  703. (add-to-list 'bs-configurations '("this-frame" nil (lambda (buf) (memq buf (my-frame-buffer-get))) ".*" nil nil))
  704. ;; (setq bs-configurations (list '("processes" nil get-buffer-process ".*" nil nil)
  705. ;; '("files-and-scratch" "^\\*scratch\\*$" nil nil bs-visits-non-file bs-sort-buffer-interns-are-last)))
  706. (setq bs-default-configuration "this-frame")
  707. (setq bs-default-sort-name "by name")
  708. (add-hook 'bs-mode-hook
  709. (lambda ()
  710. (setq bs-default-configuration "this-frame")
  711. ;; (and bs--show-all
  712. ;; (call-interactively 'bs-toggle-show-all))
  713. (set (make-local-variable 'scroll-margin) 0)
  714. ))
  715. (defun buffer-same-dir-p (bf)
  716. "return t if BF's dir is same as current dir, otherwise nil."
  717. (let ((cdir (expand-file-name default-directory)))
  718. (with-current-buffer bf
  719. (equal (expand-file-name default-directory) cdir))))
  720. (iswitchb-mode 1)
  721. (defun iswitchb-buffer-display-other-window ()
  722. ""
  723. (interactive)
  724. (let ((iswitchb-default-method 'display))
  725. (call-interactively 'iswitchb-buffer)))
  726. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  727. ;; sdic
  728. (defun sdic-describe-word-at-point-echo ()
  729. ""
  730. (interactive)
  731. (save-window-excursion
  732. (sdic-describe-word-at-point))
  733. (save-excursion
  734. (set-buffer sdic-buffer-name)
  735. (message (buffer-substring (point-min)
  736. (progn (goto-char (point-min))
  737. (or (and (re-search-forward "^\\w" nil t 4)
  738. (progn (previous-line) t)
  739. (point-at-eol))
  740. (point-max)))))))
  741. (setq sdic-eiwa-dictionary-list '((sdicf-client "/usr/share/dict/gene.sdic")))
  742. (setq sdic-waei-dictionary-list '((sdicf-client "/usr/share/dict/jedict.sdic" (add-keys-to-headword t))))
  743. (setq sdic-disable-select-window t)
  744. (setq sdic-window-height 7)
  745. (when (require 'sdic nil t)
  746. ;; (define-key my-prefix-map "\C-w" 'sdic-describe-word)
  747. (define-key my-prefix-map "\C-t" 'sdic-describe-word-at-point-echo))
  748. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  749. ;; vc
  750. ;; (require 'vc)
  751. (setq vc-handled-backends '())
  752. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  753. ;; gauche-mode
  754. (let ((s (executable-find "gosh")))
  755. (setq scheme-program-name s
  756. gauche-program-name s))
  757. (defun run-gauche-other-window ()
  758. "Run gauche on other window"
  759. (interactive)
  760. (switch-to-buffer-other-window
  761. (get-buffer-create "*scheme*"))
  762. (run-gauche))
  763. (defun run-gauche ()
  764. "run gauche"
  765. (run-scheme gauche-program-name)
  766. )
  767. (defun scheme-send-buffer ()
  768. ""
  769. (interactive)
  770. (scheme-send-region (point-min) (point-max))
  771. (my-scheme-display-scheme-buffer)
  772. )
  773. (defun my-scheme-display-scheme-buffer ()
  774. ""
  775. (interactive)
  776. (set-window-text-height (display-buffer scheme-buffer
  777. t)
  778. 7))
  779. (add-hook 'scheme-mode-hook
  780. (lambda ()
  781. nil))
  782. (add-hook 'inferior-scheme-mode-hook
  783. (lambda ()
  784. ;; (my-scheme-display-scheme-buffer)
  785. ))
  786. ;; http://d.hatena.ne.jp/kobapan/20090305/1236261804
  787. ;; http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el
  788. (when (dllib-if-unfound "http://www.katch.ne.jp/~leque/software/repos/gauche-mode/gauche-mode.el"
  789. t)
  790. (setq auto-mode-alist
  791. (cons '("\.gosh\\'" . gauche-mode) auto-mode-alist))
  792. (setq auto-mode-alist
  793. (cons '("\.gaucherc\\'" . gauche-mode) auto-mode-alist))
  794. (autoload 'gauche-mode "gauche-mode" "Major mode for Scheme." t)
  795. (autoload 'run-scheme "gauche-mode" "Run an inferior Scheme process." t)
  796. (add-hook 'gauche-mode-hook
  797. (lambda ()
  798. (define-key gauche-mode-map (kbd "C-c C-z") 'run-gauche-other-window)
  799. (define-key scheme-mode-map (kbd "C-c C-c") 'scheme-send-buffer)
  800. (define-key scheme-mode-map (kbd "C-c C-b") 'my-scheme-display-scheme-buffer)
  801. )))
  802. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  803. ;; recentf-mode
  804. (setq recentf-save-file (expand-file-name "~/.emacs.d/recentf")
  805. recentf-max-menu-items 20
  806. recentf-max-saved-items 30
  807. recentf-show-file-shortcuts-flag nil)
  808. (when (require 'recentf nil t)
  809. ;; (global-set-key "\C-x\C-r" 'recentf-open-files)
  810. (define-key ctl-x-map (kbd "C-r") 'recentf-open-files)
  811. ;; (add-hook 'find-file-hook
  812. ;; (lambda ()
  813. ;; (recentf-add-file default-directory)))
  814. (recentf-mode 1)
  815. ;; (add-to-list 'recentf-filename-handlers 'abbreviate-file-name)
  816. (add-to-list 'recentf-exclude (regexp-quote recentf-save-file))
  817. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/recentf-show.el"
  818. t)
  819. (require 'recentf-show nil t)
  820. (define-key ctl-x-map (kbd "C-r") 'recentf-show)))
  821. (add-hook 'recentf-dialog-mode-hook
  822. (lambda ()
  823. (recentf-save-list)
  824. ;; (define-key recentf-dialog-mode-map (kbd "C-x C-f") 'my-recentf-cd-and-find-file)
  825. (define-key recentf-dialog-mode-map (kbd "<up>") 'previous-line)
  826. (define-key recentf-dialog-mode-map (kbd "<down>") 'next-line)
  827. (define-key recentf-dialog-mode-map "p" 'previous-line)
  828. (define-key recentf-dialog-mode-map "n" 'next-line)
  829. (cd "~/")))
  830. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  831. ;; dired
  832. (require 'dired)
  833. (defun my-dired-echo-file-head (arg)
  834. ""
  835. (interactive "P")
  836. (let ((f (dired-get-filename)))
  837. (message "%s"
  838. (with-temp-buffer
  839. (insert-file-contents f)
  840. (buffer-substring-no-properties (point-min)
  841. (progn (goto-line (if arg
  842. (prefix-numeric-value arg)
  843. 10))
  844. (point-at-eol)))))))
  845. (defun my-dired-diff ()
  846. ""
  847. (interactive)
  848. (let ((files (dired-get-marked-files nil nil nil t)))
  849. (if (eq (car files)
  850. t)
  851. (diff (cadr files) (dired-get-filename))
  852. (message "One files must be marked!"))))
  853. (defun my-pop-to-buffer-erase-noselect (buffer-or-name)
  854. "pop up buffer using `display-buffer' and return that buffer."
  855. (let ((bf (get-buffer-create buffer-or-name)))
  856. (with-current-buffer bf
  857. (cd ".")
  858. (erase-buffer))
  859. (display-buffer bf)
  860. bf))
  861. (defun my-replace-nasi-none ()
  862. ""
  863. (save-excursion
  864. (let ((buffer-read-only nil))
  865. (goto-char (point-min))
  866. (while (search-forward "なし" nil t)
  867. (replace-match "none")))))
  868. (defun dired-get-file-info ()
  869. "dired get disk usage"
  870. (interactive)
  871. (let ((f (dired-get-filename)))
  872. (if (file-directory-p f)
  873. (progn
  874. (message "calculating du...")
  875. (shell-command (concat "du -hsD "
  876. f)))
  877. (shell-command (concat "file "
  878. f)))))
  879. (defun my-dired-scroll-up ()
  880. ""
  881. (interactive)
  882. (my-dired-previous-line (- (window-height) 1)))
  883. (defun my-dired-scroll-down ()
  884. ""
  885. (interactive)
  886. (my-dired-next-line (- (window-height) 1)))
  887. (defun my-dired-previous-line (arg)
  888. ""
  889. (interactive "p")
  890. (when (> arg 0)
  891. ;; (ignore 'my-dired-print-current-dir-and-file)
  892. (dired-previous-line 1)
  893. (when (eq (line-number-at-pos)
  894. 2)
  895. (goto-line (- (line-number-at-pos (point-max))
  896. 1))
  897. (dired-move-to-filename))
  898. (my-dired-previous-line (- arg 1))
  899. ))
  900. (defun my-dired-next-line (arg)
  901. ""
  902. (interactive "p")
  903. (when (> arg 0)
  904. ;; (ignore 'my-dired-print-current-dir-and-file)
  905. (dired-next-line 1)
  906. (when (eq (point)
  907. (point-max))
  908. (goto-line 3)
  909. (dired-move-to-filename))
  910. (my-dired-next-line (- arg 1))
  911. ))
  912. (defun my-dired-print-current-dir-and-file ()
  913. (message "%s %s"
  914. default-directory
  915. (buffer-substring-no-properties (point-at-bol)
  916. (point-at-eol))))
  917. (defun dired-do-execute-as-command ()
  918. ""
  919. (interactive)
  920. (let ((file (dired-get-filename t)))
  921. (if (file-executable-p file)
  922. (start-process file nil file)
  923. (when (y-or-n-p "this file cant be executed. mark as executable and go? : ")
  924. (set-file-modes file (file-modes-symbolic-to-number "u+x" (file-modes file)))
  925. (start-process file nil file)))))
  926. ;;http://bach.istc.kobe-u.ac.jp/lect/tamlab/ubuntu/emacs.html
  927. (defun my-dired-x-open ()
  928. ""
  929. (interactive)
  930. (my-x-open (dired-get-filename t t)))
  931. (if (eq window-system 'mac)
  932. (setq dired-listing-switches "-lhFG")
  933. (setq dired-listing-switches "-lhFG --time-style=long-iso")
  934. )
  935. (setq dired-listing-switches "-lhFG")
  936. (put 'dired-find-alternate-file 'disabled nil) ; when using dired-find-alternate-file reuse current dired buffer for the file to open
  937. (setq dired-ls-F-marks-symlinks t)
  938. (require 'ls-lisp)
  939. (setq ls-lisp-use-insert-directory-program nil) ; always use ls-lisp
  940. (setq ls-lisp-dirs-first t)
  941. (setq ls-lisp-use-localized-time-format t)
  942. (setq ls-lisp-format-time-list
  943. '("%Y-%m-%d %H:%M"
  944. "%Y-%m-%d "))
  945. (setq dired-dwim-target t)
  946. ;; (add-hook 'dired-after-readin-hook
  947. ;; 'my-replace-nasi-none)
  948. ;; (add-hook 'after-init-hook
  949. ;; (lambda ()
  950. ;; (dired ".")))
  951. (add-hook 'dired-mode-hook
  952. (lambda ()
  953. (define-key dired-mode-map "o" 'my-dired-x-open)
  954. (define-key dired-mode-map "i" 'dired-get-file-info)
  955. (define-key dired-mode-map "f" 'find-file)
  956. (define-key dired-mode-map "!" 'shell-command)
  957. (define-key dired-mode-map "&" 'async-shell-command)
  958. (define-key dired-mode-map "X" 'dired-do-async-shell-command)
  959. (define-key dired-mode-map "=" 'my-dired-diff)
  960. (define-key dired-mode-map "B" 'gtkbm-add-current-dir)
  961. (define-key dired-mode-map "b" 'gtkbm)
  962. (define-key dired-mode-map "h" 'my-dired-echo-file-head)
  963. (define-key dired-mode-map "@" (lambda () (interactive) (my-x-open ".")))
  964. (define-key dired-mode-map (kbd "TAB") 'other-window)
  965. ;; (define-key dired-mode-map "P" 'my-dired-do-pack-or-unpack)
  966. (define-key dired-mode-map "/" 'dired-isearch-filenames)
  967. (define-key dired-mode-map (kbd "DEL") 'dired-up-directory)
  968. (define-key dired-mode-map (kbd "C-h") 'dired-up-directory)
  969. (substitute-key-definition 'dired-next-line 'my-dired-next-line dired-mode-map)
  970. (substitute-key-definition 'dired-previous-line 'my-dired-previous-line dired-mode-map)
  971. (define-key dired-mode-map (kbd "<left>") 'my-dired-scroll-up)
  972. (define-key dired-mode-map (kbd "<right>") 'my-dired-scroll-down)
  973. (let ((file "._Icon\015"))
  974. (when (file-readable-p file)
  975. (delete-file file)))))
  976. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/pack.el"
  977. t)
  978. (require 'pack nil t)
  979. (add-hook 'dired-mode-hook
  980. (lambda ()
  981. (define-key dired-mode-map "P" 'dired-do-pack-or-unpack))))
  982. (and (dllib-if-unfound "https://raw.github.com/10sr/emacs-lisp/master/dired-list-all-mode.el"
  983. t)
  984. (require 'dired-list-all-mode nil t)
  985. (setq dired-listing-switches "-lhFG")
  986. (add-hook 'dired-mode-hook
  987. (lambda ()
  988. (define-key dired-mode-map "a" 'dired-list-all-mode)
  989. )))
  990. ;; http://blog.livedoor.jp/tek_nishi/archives/4693204.html
  991. (defun my-dired-toggle-mark()
  992. (let ((cur (cond ((eq (following-char) dired-marker-char) ?\040)
  993. (t dired-marker-char))))
  994. (delete-char 1)
  995. (insert cur)))
  996. (defun my-dired-mark (arg)
  997. "toggle mark the current (or next ARG) files.
  998. If on a subdir headerline, mark all its files except `.' and `..'.
  999. Use \\[dired-unmark-all-files] to remove all marks
  1000. and \\[dired-unmark] on a subdir to remove the marks in
  1001. this subdir."
  1002. (interactive "P")
  1003. (if (dired-get-subdir)
  1004. (save-excursion (dired-mark-subdir-files))
  1005. (let ((inhibit-read-only t))
  1006. (dired-repeat-over-lines
  1007. (prefix-numeric-value arg)
  1008. 'my-dired-toggle-mark))))
  1009. (defun my-dired-mark-backward (arg)
  1010. "In Dired, move up lines and toggle mark there.
  1011. Optional prefix ARG says how many lines to unflag; default is one line."
  1012. (interactive "p")
  1013. (my-dired-mark (- arg)))
  1014. (add-hook 'dired-mode-hook
  1015. (lambda ()
  1016. (local-set-key (kbd "SPC") 'my-dired-mark)
  1017. (local-set-key (kbd "S-SPC") 'my-dired-mark-backward))
  1018. )
  1019. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1020. ;; eshell
  1021. (defun my-eshell-backward-delete-char ()
  1022. (interactive)
  1023. (when (< (save-excursion
  1024. (eshell-bol)
  1025. (point))
  1026. (point))
  1027. (backward-delete-char 1)))
  1028. (defun my-file-owner-p (file)
  1029. "t if FILE is owned by me."
  1030. (eq (user-uid) (nth 2 (file-attributes file))))
  1031. ;; ;; http://www.bookshelf.jp/pukiwiki/pukiwiki.php?Eshell%A4%F2%BB%C8%A4%A4%A4%B3%A4%CA%A4%B9
  1032. ;; ;; written by Stefan Reichoer <reichoer@web.de>
  1033. ;; (defun eshell/less (&rest args)
  1034. ;; "Invoke `view-file' on the file.
  1035. ;; \"less +42 foo\" also goes to line 42 in the buffer."
  1036. ;; (if args
  1037. ;; (while args
  1038. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1039. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1040. ;; (file (pop args)))
  1041. ;; (view-file file)
  1042. ;; (goto-line line))
  1043. ;; (view-file (pop args))))))
  1044. (defun eshell/o (&optional file)
  1045. (my-x-open (or file ".")))
  1046. ;; (defun eshell/vi (&rest args)
  1047. ;; "Invoke `find-file' on the file.
  1048. ;; \"vi +42 foo\" also goes to line 42 in the buffer."
  1049. ;; (while args
  1050. ;; (if (string-match "\\`\\+\\([0-9]+\\)\\'" (car args))
  1051. ;; (let* ((line (string-to-number (match-string 1 (pop args))))
  1052. ;; (file (pop args)))
  1053. ;; (find-file file)
  1054. ;; (goto-line line))
  1055. ;; (find-file (pop args)))))
  1056. (defun eshell/clear ()
  1057. "Clear the current buffer, leaving one prompt at the top."
  1058. (let ((inhibit-read-only t))
  1059. (erase-buffer)))
  1060. (defun eshell/d (&optional dirname switches)
  1061. "if first arg is omitted open current directory."
  1062. (dired (or dirname ".") switches))
  1063. (defun eshell/v ()
  1064. (view-mode 1))
  1065. (defun eshell/git (&rest args)
  1066. ""
  1067. (if (member (car args)
  1068. '("di" "diff" "log" "show"))
  1069. (apply 'eshell-exec-visual "git" args)
  1070. (shell-command (mapconcat 'shell-quote-argument
  1071. `("git" ,@args)
  1072. " ")
  1073. t)
  1074. ;; (eshell-external-command "git" args)
  1075. ))
  1076. (defalias 'eshell/: 'ignore)
  1077. (defalias 'eshell/type 'eshell/which)
  1078. ;; (defalias 'eshell/vim 'eshell/vi)
  1079. (defalias 'eshell/ff 'find-file)
  1080. (defalias 'eshell/q 'eshell/exit)
  1081. (defun eshell-goto-prompt ()
  1082. ""
  1083. (interactive)
  1084. (goto-char (point-max)))
  1085. (defun eshell-cd-default-directory (&optional eshell-buffer-or-name)
  1086. "open eshell and change wd
  1087. if arg given, use that eshell buffer, otherwise make new eshell buffer."
  1088. (interactive)
  1089. (let ((dir (expand-file-name default-directory)))
  1090. (switch-to-buffer (or eshell-buffer-or-name
  1091. (eshell t)))
  1092. (unless (equal dir (expand-file-name default-directory))
  1093. ;; (cd dir)
  1094. ;; (eshell-interactive-print (concat "cd " dir "\n"))
  1095. ;; (eshell-emit-prompt)
  1096. (goto-char (point-max))
  1097. (eshell-kill-input)
  1098. (insert "cd " dir)
  1099. (eshell-send-input))))
  1100. (setq eshell-directory-name "~/.emacs.d/eshell/")
  1101. (setq eshell-term-name "eterm-color")
  1102. (setq eshell-scroll-to-bottom-on-input t)
  1103. (setq eshell-cmpl-ignore-case t)
  1104. (setq eshell-cmpl-cycle-completions nil)
  1105. (setq eshell-highlight-prompt nil)
  1106. (setq eshell-ls-initial-args '("-hCFG"
  1107. "--color=auto"
  1108. "--time-style=long-iso")) ; "-hF")
  1109. (setq eshell-prompt-function
  1110. (lambda ()
  1111. (with-temp-buffer
  1112. (let (p1 p2 p3 p4)
  1113. (insert " [")
  1114. (setq p1 (point))
  1115. (insert (abbreviate-file-name default-directory))
  1116. (setq p2 (point))
  1117. (insert "]"
  1118. "\n")
  1119. (setq p3 (point))
  1120. (insert user-login-name
  1121. "@"
  1122. (or (getenv "HOSTNAME")
  1123. (substring (shell-command-to-string (or (executable-find "hostname")
  1124. "echo ''"))
  1125. 0
  1126. -1)))
  1127. (setq p4 (point))
  1128. (insert " "
  1129. (format-time-string "%a, %d %b %Y %T %z")
  1130. " eshell\n"
  1131. "last:"
  1132. (number-to-string eshell-last-command-status)
  1133. (if (= (user-uid)
  1134. 0)
  1135. " # "
  1136. " $ "))
  1137. (add-text-properties p1
  1138. p2
  1139. '(face ((foreground-color . "yellow"))))
  1140. (add-text-properties p3
  1141. p4
  1142. '(face ((foreground-color . "cyan"))))
  1143. (buffer-substring (point-min)
  1144. (point-max))))))
  1145. (add-hook 'eshell-mode-hook
  1146. (lambda ()
  1147. ;; (define-key eshell-mode-map (kbd "C-x C-x") (lambda ()
  1148. ;; (interactive)
  1149. ;; (switch-to-buffer (other-buffer))))
  1150. (define-key eshell-mode-map (kbd "C-u") (lambda ()
  1151. (interactive)
  1152. (eshell-goto-prompt)
  1153. (eshell-kill-input)))
  1154. (define-key eshell-mode-map (kbd "C-g") (lambda ()
  1155. (interactive)
  1156. (eshell-goto-prompt)
  1157. (my-keyboard-quit)))
  1158. (define-key eshell-mode-map (kbd "DEL") 'my-eshell-backward-delete-char)
  1159. (define-key eshell-mode-map (kbd "C-p") 'eshell-previous-matching-input-from-input)
  1160. (define-key eshell-mode-map (kbd "C-n") 'eshell-next-matching-input-from-input)
  1161. (apply 'eshell/addpath exec-path)
  1162. (set (make-local-variable 'scroll-margin) 0)
  1163. ;; (eshell/export "GIT_PAGER=")
  1164. ;; (eshell/export "GIT_EDITOR=")
  1165. (eshell/export "LC_MESSAGES=C")
  1166. (switch-to-buffer (current-buffer)) ; move buffer top of list
  1167. (set (make-local-variable 'hl-line-range-function)
  1168. (lambda ()
  1169. '(0 . 0)))
  1170. (add-to-list 'eshell-virtual-targets
  1171. '("/dev/less"
  1172. (lambda (str)
  1173. (if str
  1174. (with-current-buffer nil)))
  1175. nil))
  1176. ))
  1177. (add-hook 'eshell-mode-hook
  1178. (lambda ()
  1179. (add-to-list 'eshell-visual-commands "vim")
  1180. ;; (add-to-list 'eshell-visual-commands "git")
  1181. (add-to-list 'eshell-output-filter-functions 'eshell-truncate-buffer)
  1182. (mapcar (lambda (alias)
  1183. (add-to-list 'eshell-command-aliases-list
  1184. alias))
  1185. '(
  1186. ; ("ll" "ls -l $*")
  1187. ; ("la" "ls -a $*")
  1188. ; ("lla" "ls -al $*")
  1189. ("ut" "slogin 03110414@un001.ecc.u-tokyo.ac.jp $*")
  1190. ("aptin" "apt-get install $*")
  1191. ("eless" "cat >>> (with-current-buffer (get-buffer-create \"*eshell output\") (erase-buffer) (setq buffer-read-only nil) (current-buffer)); (view-buffer (get-buffer \"*eshell output*\"))")
  1192. ("g" "git $*")
  1193. ))
  1194. ))
  1195. ;; (eval-after-load "em-alias"
  1196. ;; '(progn ;; (eshell/alias "ll" "ls -l")
  1197. ;; ;; (eshell/alias "la" "ls -a")
  1198. ;; ;; (eshell/alias "lla" "ls -al")
  1199. ;; (eshell/alias "sgcc" (if (eq system-type 'windows-nt)
  1200. ;; "gcc -o win.$1.exe $1"
  1201. ;; "gcc -o ${uname}.$1.out $1"))
  1202. ;; (eshell/alias "slmgcc" (if (eq system-type 'windows-nt)
  1203. ;; "gcc -lm -o win.$1.exe $1"
  1204. ;; "gcc -lm -o ${uname}.$1.out $1"))
  1205. ;; ;; (eshell/alias "ut" "ssh g841105@un001.ecc.u-tokyo.ac.jp")
  1206. ;; (add-to-list 'recentf-exclude (concat eshell-directory-name "alias"))))
  1207. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1208. ;; get last modified date
  1209. (defvar my-buffer-file-last-modified-time nil "")
  1210. (make-variable-buffer-local 'my-buffer-file-last-modified-time)
  1211. (defun my-get-file-last-modified-time (file)
  1212. ""
  1213. (nth 5
  1214. (file-attributes file)))
  1215. (defun my-set-buffer-file-last-modified-time ()
  1216. ""
  1217. (make-local-variable 'my-buffer-file-last-modified-time)
  1218. (setq my-buffer-file-last-modified-time
  1219. (format-time-string "%Y/%m/%d %H:%M" (my-get-file-last-modified-time buffer-file-name))))
  1220. (add-hook 'find-file-hook
  1221. 'my-set-buffer-file-last-modified-time)
  1222. (add-hook 'after-save-hook
  1223. 'my-set-buffer-file-last-modified-time)
  1224. (add-hook 'after-revert-hook
  1225. 'my-set-buffer-file-last-modified-time)
  1226. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1227. ;; frame buffer
  1228. ;; todo: work well when opening the file that was already opened on another window
  1229. (add-hook 'after-make-frame-functions
  1230. (lambda (f)
  1231. (set-window-buffer (frame-selected-window f)
  1232. "*Messages*")))
  1233. (defun make-frame-command-with-name (name)
  1234. "Make frame with name specified."
  1235. (interactive "sName for new frame: ")
  1236. (set-frame-parameter (make-frame-command)
  1237. 'name
  1238. name))
  1239. (defvar my-frame-buffer-plist nil)
  1240. (defun my-frame-buffer-add (&optional buf frame)
  1241. ""
  1242. (setq my-frame-buffer-plist
  1243. (plist-put my-frame-buffer-plist
  1244. (or frame
  1245. (selected-frame))
  1246. (let ((lst (my-frame-buffer-get frame)))
  1247. (if lst
  1248. (add-to-list 'lst
  1249. (or buf
  1250. (current-buffer)))
  1251. (list (or buf
  1252. (current-buffer))))))))
  1253. (defun my-frame-buffer-remove (&optional buf frame)
  1254. ""
  1255. (setq my-frame-buffer-plist
  1256. (plist-put my-frame-buffer-plist
  1257. (or frame
  1258. (selected-frame))
  1259. (delq (or buf
  1260. (current-buffer))
  1261. (my-frame-buffer-get frame)))))
  1262. (defun my-frame-buffer-get (&optional frame)
  1263. ""
  1264. (plist-get my-frame-buffer-plist
  1265. (or frame
  1266. (selected-frame))))
  1267. (defun my-frame-buffer-kill-all-buffer (&optional frame)
  1268. ""
  1269. (mapcar 'kill-buffer
  1270. (my-frame-buffer-get frame)))
  1271. (add-hook 'find-file-hook
  1272. 'my-frame-buffer-add)
  1273. (add-hook 'term-mode-hook
  1274. 'my-frame-buffer-add)
  1275. (add-hook 'eshell-mode-hook
  1276. 'my-frame-buffer-add)
  1277. (add-hook 'Man-mode-hook
  1278. 'my-frame-buffer-add)
  1279. (add-hook 'kill-buffer-hook
  1280. 'my-frame-buffer-remove)
  1281. (add-hook 'delete-frame-functions
  1282. 'my-frame-buffer-kill-all-buffer)
  1283. (defvar my-desktop-terminal "roxterm")
  1284. (defun my-execute-terminal ()
  1285. ""
  1286. (interactive)
  1287. (if (and (or (eq system-type 'windows-nt)
  1288. window-system)
  1289. my-desktop-terminal
  1290. )
  1291. (let ((process-environment (cons "TERM=xterm" process-environment)))
  1292. (start-process "terminal"
  1293. nil
  1294. my-desktop-terminal))
  1295. (my-term)))
  1296. (defun my-term ()
  1297. "open terminal buffer and return that buffer."
  1298. (interactive)
  1299. (if (eq system-type 'windows-nt)
  1300. (eshell t)
  1301. (if (featurep 'multi-term)
  1302. (multi-term)
  1303. (ansi-term "/bin/bash"))))
  1304. (defun my-delete-frame-or-kill-emacs ()
  1305. "delete frame when opening multiple frame, kill emacs when only one."
  1306. (interactive)
  1307. (if (eq 1
  1308. (length (frame-list)))
  1309. (save-buffers-kill-emacs)
  1310. (delete-frame)))
  1311. (define-key my-prefix-map (kbd "C-s") 'my-execute-terminal)
  1312. (define-key my-prefix-map (kbd "C-f") 'make-frame-command-with-name)
  1313. (global-set-key (kbd "C-x C-c") 'my-delete-frame-or-kill-emacs)
  1314. (define-key my-prefix-map (kbd "C-x C-c") 'save-buffers-kill-emacs)
  1315. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1316. ;; auto saving
  1317. (defun my-save-this-buffer (silent-p)
  1318. "save current buffer if can without asking"
  1319. (let ((cm (if (current-message)
  1320. (format "%s\n" (current-message))
  1321. ""))
  1322. (fun (symbol-function (if silent-p
  1323. 'ignore
  1324. 'message))))
  1325. (cond ((active-minibuffer-window) nil)
  1326. ((not buffer-file-name) (funcall fun "%ssaving... this buffer doesn't visit any file." cm) nil)
  1327. ((not (file-exists-p buffer-file-name)) (funcall fun "%ssaving... file not exist. save manually first." com) nil)
  1328. (buffer-read-only (funcall fun "%ssaving... this buffer is read-only." cm) nil)
  1329. ((not (buffer-modified-p)) (funcal fun "%ssaving... not modified yet." cm) nil)
  1330. ((not (file-writable-p buffer-file-name)) (funcall fun "%ssaving... you cannot change this file." cm) nil)
  1331. (t (funcall fun "%ssaving..." cm)
  1332. (save-buffer)
  1333. (funcall fun "%ssaving... done." cm)))))
  1334. ;; (if (and buffer-file-name
  1335. ;; (not buffer-read-only)
  1336. ;; (buffer-modified-p)
  1337. ;; (file-writable-p buffer-file-name))
  1338. ;; (save-buffer))) ; silent one
  1339. (defvar my-auto-save-this-buffer nil "auto save timer object")
  1340. (defun my-auto-save-this-buffer (sec &optional silent-p)
  1341. "auto save current buffer if idle for SEC.
  1342. when SEC is nil, stop auto save if enabled."
  1343. (if sec
  1344. (progn (when my-auto-save-this-buffer
  1345. (cancel-timer my-auto-save-this-buffer)
  1346. (setq my-auto-save-this-buffer nil))
  1347. (setq my-auto-save-this-buffer (run-with-idle-timer sec t 'my-save-this-buffer silent-p)))
  1348. (when my-auto-save-this-buffer
  1349. (cancel-timer my-auto-save-this-buffer)
  1350. (setq my-auto-save-this-buffer nil))))
  1351. (my-auto-save-this-buffer 2 t)
  1352. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1353. ;; misc funcs
  1354. (defun dir-show (&optional dir)
  1355. (interactive)
  1356. (let ((bf (get-buffer-create "*dir show*"))
  1357. (list-directory-brief-switches "-C"))
  1358. (with-current-buffer bf
  1359. (list-directory (or nil
  1360. default-directory)
  1361. nil))
  1362. ))
  1363. (defalias 'qcalc 'quick-calc)
  1364. (defun my-kill-buffers ()
  1365. ""
  1366. (interactive)
  1367. (mapcar (lambda (buf)
  1368. (when (buffer-file-name buf)
  1369. (kill-buffer buf)))
  1370. (buffer-list)))
  1371. (defvar my-filer nil)
  1372. (setq my-filer (or (executable-find "pcmanfm")
  1373. (executable-find "nautilus")))
  1374. (defun my-x-open (file)
  1375. "open file."
  1376. (interactive "FOpen File: ")
  1377. (setq file (expand-file-name file))
  1378. (message "Opening %s..." file)
  1379. (cond ((eq system-type 'windows-nt)
  1380. (call-process "cmd.exe" nil 0 nil "/c" "start" "" (convert-standard-filename file)))
  1381. ((eq system-type 'darwin)
  1382. (call-process "open" nil 0 nil file))
  1383. ((getenv "DISPLAY")
  1384. (call-process (or my-filer "xdg-open") nil 0 nil file))
  1385. (t
  1386. (find-file file))
  1387. )
  1388. ;; (recentf-add-file file)
  1389. (message "Opening %s...done" file))
  1390. (defun my-keyboard-quit ()
  1391. ""
  1392. (interactive)
  1393. (run-hooks 'before-keyboard-quit-hook)
  1394. ;; (redisplay t)
  1395. (redraw-display)
  1396. ;; (run-hooks 'window-configuration-change-hook)
  1397. (my-revert-buffer-if-needed)
  1398. ;; (revert-buffer t t)
  1399. (keyboard-quit)
  1400. (insert "insert me")
  1401. (run-hooks 'after-keyboard-quit-hook))
  1402. (substitute-key-definition 'keyboard-quit 'my-keyboard-quit global-map)
  1403. ;; (global-set-key (kbd "C-g") 'my-keyboard-quit)
  1404. (defun my-convmv-sjis2utf8-test ()
  1405. "run `convmv -r -f sjis -t utf8 *'
  1406. this is test, does not rename files"
  1407. (interactive)
  1408. (shell-command "convmv -r -f sjis -t utf8 *"))
  1409. (defun my-convmv-sjis2utf8-notest ()
  1410. "run `convmv -r -f sjis -t utf8 * --notest'"
  1411. (interactive)
  1412. (shell-command "convmv -r -f sjis -t utf8 * --notest"))
  1413. (defun my-copy-whole-line ()
  1414. ""
  1415. (interactive)
  1416. (kill-new (concat (buffer-substring (point-at-bol)
  1417. (point-at-eol))
  1418. "\n")))
  1419. (defun kill-ring-save-buffer-file-name ()
  1420. "get current filename"
  1421. (interactive)
  1422. (let ((file buffer-file-name))
  1423. (if file
  1424. (progn (kill-new file)
  1425. (message file))
  1426. (message "not visiting file."))))
  1427. (defvar my-revert-buffer-if-needed-last-buffer nil)
  1428. (defun my-revert-buffer-if-needed ()
  1429. ""
  1430. (interactive)
  1431. (unless (eq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1432. (setq my-revert-buffer-if-needed-last-buffer (current-buffer))
  1433. (when (or (and (eq major-mode 'dired-mode)
  1434. (dired-directory-changed-p default-directory))
  1435. (not (verify-visited-file-modtime (current-buffer))))
  1436. (revert-buffer t t)
  1437. (message "%s reverted." (buffer-name))
  1438. )))
  1439. (add-hook 'post-command-hook ; 'window-configuration-change-hook
  1440. 'my-revert-buffer-if-needed)
  1441. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1442. ;; forked from http://d.hatena.ne.jp/khiker/20100119/window_resize
  1443. (define-key my-prefix-map (kbd "C-w") 'my-window-organizer)
  1444. (defun my-window-organizer ()
  1445. "Control window size and position."
  1446. (interactive)
  1447. (save-selected-window
  1448. (select-window (window-at 0 0))
  1449. (let ( ;; (window-obj (selected-window))
  1450. ;; (current-width (window-width))
  1451. ;; (current-height (window-height))
  1452. action
  1453. c)
  1454. (catch 'end-flag
  1455. (while t
  1456. (setq action
  1457. (read-key-sequence-vector (format "size[%dx%d] 1: maximize; 2, 3: split; 0: delete; o: select other; j, l: enlarge; h, k: shrink; q: quit."
  1458. (window-width)
  1459. (window-height))))
  1460. (setq c (aref action 0))
  1461. (cond ((= c ?l)
  1462. (unless (eq (window-width) (frame-width))
  1463. (enlarge-window-horizontally 1)))
  1464. ((= c ?h)
  1465. (unless (eq (window-width) (frame-width))
  1466. (shrink-window-horizontally 1)))
  1467. ((= c ?j)
  1468. (enlarge-window 1))
  1469. ((= c ?k)
  1470. (shrink-window 1))
  1471. ((= c ?o)
  1472. (other-window 1))
  1473. ((memq c '(?d ?0))
  1474. (unless (eq (selected-window) (next-window (selected-window) 0 1))
  1475. (delete-window (selected-window))))
  1476. ((= c ?1)
  1477. (delete-other-windows))
  1478. ((= c ?2)
  1479. (split-window-vertically))
  1480. ((= c ?3)
  1481. (split-window-horizontally))
  1482. ((memq c '(?q ?\C-g))
  1483. (message "Quit")
  1484. (throw 'end-flag t))
  1485. (t
  1486. (beep))))))))
  1487. ;; (aref (read-key-sequence-vector "aa") 0)
  1488. ;; ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1489. ;; ;; emacsを殺伐とさせる
  1490. ;; ;; 補完させるとき失敗するからなし
  1491. ;; ;; http://e-arrows.sakura.ne.jp/2010/05/emacs-should-be-more-savage.html
  1492. ;; (defadvice message (before message-for-stupid (arg &rest arg2) activate)
  1493. ;; (setq arg
  1494. ;; (concat arg
  1495. ;; (if (eq nil (string-match "\\. *$" arg)) ".")
  1496. ;; " Stupid!")))
  1497. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1498. ;; japanese input method
  1499. (defun my-load-scim ()
  1500. "use scim-bridge.el as japanese im."
  1501. ;; Load scim-bridge.
  1502. (when (require 'scim-bridge nil t)
  1503. ;; Turn on scim-mode automatically after loading .emacs
  1504. (add-hook 'after-init-hook 'scim-mode-on)
  1505. (setq scim-cursor-color "red")
  1506. (scim-define-preedit-key ?\^h t)
  1507. (scim-define-common-key ?\* nil)
  1508. (scim-define-common-key ?\^/ nil)))
  1509. (defun my-load-anthy ()
  1510. "use anthy.el as japanese im."
  1511. ;; anthy
  1512. (when (require 'anthy nil t)
  1513. (global-set-key (kbd "<muhenkan>") (lambda () (interactive) (anthy-mode-off)))
  1514. (global-set-key (kbd "<henkan>") (lambda () (interactive) (anthy-mode-on)))
  1515. (when (>= emacs-major-version 23)
  1516. (setq anthy-accept-timeout 1))))
  1517. ;; quail
  1518. ;; aproposs input-method for some information
  1519. ;; (setq default-input-method "japanese")
  1520. (defun my-load-mozc-el ()
  1521. ""
  1522. (setq mozc-leim-title "[MZ]")
  1523. (when (require 'mozc nil t)
  1524. (setq defauit-input-method "japanese-mozc")
  1525. ))
  1526. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  1527. ;; for windows
  1528. ;; (add-to-list 'exec-path "c:/Program Files/Gauche/bin/")
  1529. (defun start-ckw-bash ()
  1530. ""
  1531. (interactive)
  1532. (start-process "ckw_bash"
  1533. nil
  1534. "C:/Documents and Settings/sr/Application Data/dbx/apps/ckw/ckw.exe")) ; cじゃないといけないらしい
  1535. (defun my-w32-add-export-path (&rest args)
  1536. ""
  1537. (mapcar (lambda (path)
  1538. (add-to-list 'exec-path (expand-file-name path)))
  1539. (reverse args))
  1540. (setenv "PATH"
  1541. (mapconcat 'convert-standard-filename
  1542. exec-path
  1543. ";")))
  1544. (when (eq system-type 'windows-nt)
  1545. ;; (setq scheme-program-name "\"c:/Program Files/Gauche/bin/gosh.exe\" -i")
  1546. ;; (setq python-python-command "c:/Python26/python.exe")
  1547. (define-key my-prefix-map (kbd "C-c") 'start-ckw-bash)
  1548. (my-w32-add-export-path "c:/Windows/system"
  1549. "c:/Windows/System32"
  1550. "c:/Program Files/Git/bin"
  1551. "c:/MinGW/bin"
  1552. "c:/MinGW/mingw32/bin"
  1553. (expand-file-name "~/bin")
  1554. (expand-file-name "~/dbx/apps/bin"))
  1555. (when window-system
  1556. (setq w32-enable-synthesized-fonts t))
  1557. (setq file-name-coding-system 'sjis))