ido-ubiquitous.el 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632
  1. ;;; ido-ubiquitous.el --- Use ido (nearly) everywhere.
  2. ;; Author: Ryan C. Thompson
  3. ;; URL: https://github.com/DarwinAwardWinner/ido-ubiquitous
  4. ;; Version: 2.0.4
  5. ;; Created: 2011-09-01
  6. ;; Keywords: convenience
  7. ;; EmacsWiki: InteractivelyDoThings
  8. ;; This file is NOT part of GNU Emacs.
  9. ;;; Commentary:
  10. ;; If you use the excellent `ido-mode' for efficient completion of
  11. ;; file names and buffers, you might wonder if you can get ido-style
  12. ;; completion everywhere else too. Well, that's what this package
  13. ;; does! ido-ubiquitous is here to enable ido-style completion for
  14. ;; (almost) every function that uses the standard completion function
  15. ;; `completing-read'.
  16. ;;; License:
  17. ;; This program is free software; you can redistribute it and/or modify
  18. ;; it under the terms of the GNU General Public License as published by
  19. ;; the Free Software Foundation; either version 3, or (at your option)
  20. ;; any later version.
  21. ;;
  22. ;; This program is distributed in the hope that it will be useful,
  23. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  24. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  25. ;; GNU General Public License for more details.
  26. ;;
  27. ;; You should have received a copy of the GNU General Public License
  28. ;; along with GNU Emacs; see the file COPYING. If not, write to the
  29. ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  30. ;; Boston, MA 02110-1301, USA.
  31. ;;; Code:
  32. (eval-when-compile
  33. (when (or (not (boundp 'completing-read-function))
  34. (< emacs-major-version 24))
  35. (error "Could not find required variable `completing-read-function'. Are you using Emacs version 24 or higher? If you have Emacs 23 or lower, please downgrade to ido-ubiquitous version 1.7.")))
  36. (require 'ido)
  37. (require 'advice)
  38. (require 'cl)
  39. ;;; Internal utility functions
  40. (defun ido-ubiquitous--as-string (sym-or-str)
  41. "Return name of symbol, return string as is."
  42. (if (symbolp sym-or-str)
  43. (symbol-name sym-or-str)
  44. sym-or-str))
  45. (defun ido-ubiquitous--as-symbol (sym-or-str)
  46. "Return name of symbol, return string as is."
  47. (if (symbolp sym-or-str)
  48. sym-or-str
  49. (intern sym-or-str)))
  50. ;;; Custom widget definitions
  51. ;; We need to define some custom widget types for use in the override
  52. ;; variables.
  53. (define-widget 'lazy-notag 'lazy
  54. "Like lazy widget, but does not display its tag, only its value."
  55. :format "%v")
  56. ;; Define matcher functions and widgets for match specifications
  57. (defvar ido-ubiquitous-match-spec-widget-types nil
  58. "List of widget names for match specs.")
  59. (defvar ido-ubiquitous-spec-matchers nil
  60. "Alist of functions for matching function specs against function names.")
  61. (loop for (widget-name widget-tag key field-type matcher) in
  62. '((exact-match "Exact match" exact string string=)
  63. (prefix-match "Prefix match" prefix string string-prefix-p)
  64. (regexp-match "Regexp match" regexp regexp string-match-p))
  65. do (define-widget (ido-ubiquitous--as-symbol widget-name) 'lazy-notag widget-tag
  66. :menu-tag widget-tag
  67. :type `(list :tag ,widget-tag :format "%v"
  68. (const :format ""
  69. :tag ,widget-tag
  70. ,key)
  71. (,field-type :tag ,widget-tag)))
  72. do (add-to-list 'ido-ubiquitous-match-spec-widget-types
  73. widget-name 'append)
  74. do (add-to-list 'ido-ubiquitous-spec-matchers
  75. (cons key matcher) 'append))
  76. (define-widget 'ido-ubiquitous-match-spec 'lazy-notag
  77. "Choice of exact, prefix, or regexp match."
  78. :type `(choice :tag "Match type"
  79. ,@ido-ubiquitous-match-spec-widget-types))
  80. (define-widget 'ido-ubiquitous-command-override-spec 'lazy-notag
  81. "Choice of override action plus match specification."
  82. :type '(cons :tag "Override rule"
  83. (choice :tag "For matching commands"
  84. (const :menu-tag "Disable"
  85. :tag "Disable ido-ubiquitous"
  86. disable)
  87. (const :menu-tag "Enable"
  88. :tag "Enable ido-ubiquitous in normal default mode"
  89. enable)
  90. (const :menu-tag "Enable old-style default"
  91. :tag "Enable ido-ubiquitous in old-style default mode"
  92. enable-old))
  93. ido-ubiquitous-match-spec))
  94. (define-widget 'ido-ubiquitous-function-override-spec 'lazy-notag
  95. "Choice of override action and function name. (Exact match only.)"
  96. :type '(list :tag "Override rule"
  97. (choice :tag "Do the following"
  98. (const :menu-tag "Disable"
  99. :tag "Disable ido-ubiquitous"
  100. disable)
  101. (const :menu-tag "Enable"
  102. :tag "Enable ido-ubiquitous in normal default mode"
  103. enable)
  104. (const :menu-tag "Enable old-style default"
  105. :tag "Enable ido-ubiquitous in old-style default mode"
  106. enable-old))
  107. (const :format "" exact)
  108. (string :tag "For function")))
  109. ;;; Custom Declarations
  110. (defgroup ido-ubiquitous nil
  111. "Use ido for (almost) all completion."
  112. :group 'ido)
  113. ;;;###autoload
  114. (define-obsolete-variable-alias 'ido-ubiquitous
  115. 'ido-ubiquitous-mode "0.8")
  116. ;;;###autoload
  117. (define-obsolete-function-alias 'ido-ubiquitous
  118. 'ido-ubiquitous-mode "0.8")
  119. ;;;###autoload
  120. (define-minor-mode ido-ubiquitous-mode
  121. "Use `ido-completing-read' instead of `completing-read' almost everywhere.
  122. This mode has no effect unles `ido-mode' is also enabled.
  123. If this mode causes problems for a function, you can customize
  124. when ido completion is or is not used by customizing
  125. `ido-ubiquitous-command-overrides' or
  126. `ido-ubiquitous-function-overrides'."
  127. nil
  128. :global t
  129. :group 'ido-ubiquitous
  130. ;; Handle warning about ido disabled
  131. (when ido-ubiquitous-mode
  132. (ido-ubiquitous-warn-about-ido-disabled))
  133. ;; Ensure emacs 23 code disabled (in case user upgraded in this session)
  134. (ignore-errors
  135. (ad-disable-advice 'completing-read 'around 'ido-ubiquitous-legacy)
  136. (ad-activate 'completing-read))
  137. ;; Actually enable/disable the mode
  138. (setq completing-read-function
  139. (if ido-ubiquitous-mode
  140. 'completing-read-ido
  141. ido-ubiquitous-fallback-completing-read-function)))
  142. (defcustom ido-ubiquitous-fallback-completing-read-function
  143. ;; Initialize to the current value of `completing-read-function',
  144. ;; unless that is already set to the ido completer, in which case
  145. ;; use `completing-read-default'.
  146. (if (eq completing-read-function 'completing-read-ido)
  147. 'completing-read-default
  148. completing-read-function)
  149. "Alternate completing-read function to use when ido is not wanted.
  150. This will be used for functions that are incompatibile with ido
  151. or if ido cannot handle the completion arguments.
  152. If you turn off ido-ubiquitous mode, `completing-read-function'
  153. will be set back to this."
  154. :type '(choice (const :tag "Standard emacs completion"
  155. completing-read-default)
  156. (function :tag "Other function"))
  157. :group 'ido-ubiquitous)
  158. (defcustom ido-ubiquitous-enable-old-style-default t
  159. "Allow ido to emulate a quirk of `completing-read'.
  160. From the `completing-read' docstring:
  161. > If the input is null, `completing-read' returns DEF, or the
  162. > first element of the list of default values, or an empty string
  163. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  164. If this variable is non-nil, then ido-ubiquitous will attempt to
  165. emulate this behavior. Specifically, if RET is pressed
  166. immediately upon entering completion, an empty string will be
  167. returned instead of the first element in the list. This behavior
  168. is only enabled when ido is being used as a substitute for
  169. `completing-read', and not when it is used directly.
  170. This odd behavior is required for compatibility with an old-style
  171. usage pattern whereby the default was requested by returning an
  172. empty string. In this mode, the caller receives the empty string
  173. and handles the default case manually, while `completing-read'
  174. never has any knowledge of the default. This is a problem for
  175. ido, which always returns the first element in the list when the
  176. input is empty. Without knowledge of the default, it cannot
  177. ensure that the default is first on the list, so returning the
  178. first item is not the correct behavior. Instead, it must return
  179. an empty string like `completing-read'.
  180. You can termporarily invert this behavior by prefixing \"RET\"
  181. with \"C-u\".
  182. If you want to enable old-style default selection selectively for
  183. specific commands or functions, set appropriate overrides in
  184. `ido-ubiquitous-command-overrides' or
  185. `ido-ubiquitous-function-overrides'."
  186. :type 'boolean
  187. :group 'ido-ubiquitous)
  188. (defconst ido-ubiquitous-default-command-overrides
  189. '(;; If you want ido for M-x, install smex
  190. (disable exact "execute-extended-command")
  191. ;; https://github.com/technomancy/ido-ubiquitous/issues/13#issuecomment-8033522
  192. (enable prefix "wl-")
  193. ;; https://github.com/technomancy/ido-ubiquitous/issues/7
  194. (enable-old prefix "Info-")
  195. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  196. (enable exact "webjump"))
  197. "Default value of `ido-ubiquitous-command-overrides'.
  198. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  199. (defconst ido-ubiquitous-default-function-overrides
  200. '((disable exact "read-file-name")
  201. (disable exact "read-file-name-internal")
  202. (disable exact "read-buffer")
  203. (disable exact "gnus-emacs-completing-read")
  204. (disable exact "gnus-iswitchb-completing-read")
  205. (disable exact "man")
  206. ;; https://github.com/technomancy/ido-ubiquitous/issues/3
  207. (disable exact "grep-read-files")
  208. ;; https://github.com/technomancy/ido-ubiquitous/issues/9
  209. (enable exact "bookmark-completing-read")
  210. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/4
  211. (enable-old exact "webjump-read-choice")
  212. (enable-old exact "webjump-read-url-choice")
  213. ;; https://github.com/DarwinAwardWinner/ido-ubiquitous/issues/9
  214. (disable exact "isearchp-read-unicode-char"))
  215. "Default value of `ido-ubiquitous-function-overrides'.
  216. You can restore these using the command `ido-ubiquitous-restore-default-overrides'.")
  217. (defcustom ido-ubiquitous-command-overrides ido-ubiquitous-default-command-overrides
  218. "List of command override specifications for ido-ubiquitous
  219. Each override specification describes how ido-ubiquitous should
  220. behave one or many commands. A specification has the
  221. form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'. BEHAVIOR is one of the
  222. following:
  223. * `disable': ido-ubiquitous should not be used at all for the
  224. specified commands;
  225. * `enable': ido-ubiquitous may be used with the specified
  226. commands, without emulating the old-style default selection
  227. of `completing-read';
  228. * `enable-old': ido-ubiquitous may be used with the specified
  229. commands, and should emulate the old-style default selection
  230. of `completing-read'.
  231. MATCH-TYPE affects how MATCH-TEXT is interpreted, as follows:
  232. * `exact': the specification only affects the one command
  233. whose name is MATCH-TEXT;
  234. * `prefix': the specification affects any command whose name
  235. starts with MATCH-TEXT (This is useful for specifying a
  236. certain behavior for an entire package);
  237. * `regexp': the specification affects any command whose name
  238. matches MATCH-TEXT (with MATCH-TEXT being interpreted as a
  239. regular expression)
  240. MATCH-TEXT should be a string.
  241. Since this variable's has a somewhat complex structure, it is
  242. recommended that you set this variable through Customize.
  243. Note that this variable only affects *commands*, which are
  244. functions marked as interactive. See
  245. `ido-ubiquitous-function-overrides' for how to modify the
  246. behavior of ido-ubiquitous for arbitrary functions.
  247. If you need to add a new specification to this list, please also
  248. file a bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues"
  249. :type '(repeat ido-ubiquitous-command-override-spec)
  250. :group 'ido-ubiquitous)
  251. (defun ido-ubiquitous-set-function-overrides (sym newval)
  252. "Custom setter function for `ido-ubiquitous-function-overrides'.
  253. In addition to setting the variable, this also sets up advice on
  254. each function to apply the appropriate override."
  255. ;; Unset all previous overrides
  256. (when (boundp sym)
  257. (let ((oldval (eval sym)))
  258. (loop for (action match-type func) in oldval
  259. do (ido-ubiquitous-apply-function-override func nil))))
  260. ;; Ensure that function names are strings, not symbols
  261. (setq newval
  262. (loop for (action match-type func) in newval
  263. collect (list action match-type
  264. (ido-ubiquitous--as-string func))))
  265. (set-default sym newval)
  266. ;; set new overrides
  267. (loop for (action match-type func) in (eval sym)
  268. do (ido-ubiquitous-apply-function-override func action)))
  269. (defun ido-ubiquitous-apply-function-override (func override)
  270. "Set the override property on FUNC to OVERRIDE and set up advice to apply the override."
  271. (setq func (ido-ubiquitous--as-symbol func)
  272. override (ido-ubiquitous--as-symbol override))
  273. (put func 'ido-ubiquitous-override override)
  274. (when override
  275. (let ((docstring
  276. (format "Override ido-ubiquitous behavior in %s if its `ido-ubiquitous-override' property is non-nil." func))
  277. (body-form
  278. (macroexpand
  279. `(ido-ubiquitous-with-override
  280. (get ',func 'ido-ubiquitous-override)
  281. ad-do-it))))
  282. (eval
  283. `(defadvice ,func (around ido-ubiquitous-override activate)
  284. ,docstring
  285. ,body-form)))))
  286. (defcustom ido-ubiquitous-function-overrides ido-ubiquitous-default-function-overrides
  287. "List of function override specifications for ido-ubiquitous
  288. Function override specifications have a similar structure to
  289. command override specifications (see
  290. `ido-ubiquitous-command-overrides'). A function override
  291. specification has the form `(BEHAVIOR MATCH-TYPE MATCH-TEXT)'.
  292. However, `MATCH-TYPE' may ONLY be `exact'; No other match type is
  293. supported.
  294. If you need to add a new specification to this list, please also file a
  295. bug report at https://github.com/DarwinAwardWinner/ido-ubiquitous/issues
  296. Setting this variable directly has no effect. You must set it
  297. through Customize."
  298. :type '(repeat ido-ubiquitous-function-override-spec)
  299. :set 'ido-ubiquitous-set-function-overrides
  300. :group 'ido-ubiquitous)
  301. ;;; ido-ubiquitous core
  302. (defvar ido-ubiquitous-next-call-replaces-completing-read nil
  303. "If t, then the next call to `ido-completing-read' is by ido-ubiquitous.")
  304. (defvar ido-ubiquitous-this-call-replaces-completing-read nil
  305. "If t, then the current call to `ido-completing-read' is by ido-ubiquitous.")
  306. (defvar ido-ubiquitous-next-override nil
  307. "This holds the override to be applied on the next call to `completing-read'.")
  308. (defvar ido-ubiquitous-active-override nil
  309. "This holds the override being applied to the current call to `completing-read'.")
  310. (defun ido-ubiquitous-completing-read (&rest args)
  311. "Wrapper for `ido-completing-read' that enables ido-ubiquitous features."
  312. (let ((ido-ubiquitous-next-call-replaces-completing-read t))
  313. (apply 'ido-completing-read args)))
  314. (defadvice ido-completing-read (around detect-replacing-cr activate)
  315. "Enable workarounds if this call was done through ido-ubiquitous.
  316. This advice implements the logic required for
  317. `ido-completing-read' to handle a number of special cases that
  318. `completing-read' can handle. It only has an effect if
  319. `ido-completing-read' is called through
  320. `ido-ubiquitous-completing-read', so other packages that use
  321. `ido-completing-read', such as `smex', will not be affected."
  322. (let* ((ido-ubiquitous-this-call-replaces-completing-read ido-ubiquitous-next-call-replaces-completing-read)
  323. (ido-ubiquitous-next-call-replaces-completing-read nil))
  324. (when ido-ubiquitous-this-call-replaces-completing-read
  325. ;; ido doesn't natively handle DEF being a list. If DEF is a
  326. ;; list, prepend it to CHOICES and set DEF to just the car of
  327. ;; the default list.
  328. (when (and def (listp def))
  329. (setq choices (delete-dups (append def choices))
  330. def (car def)))
  331. ;; Work around a bug in ido when both INITIAL-INPUT and DEF are provided
  332. ;; More info: https://github.com/technomancy/ido-ubiquitous/issues/18
  333. (let ((initial (cond ((null initial-input) "")
  334. ((stringp initial-input) initial-input)
  335. ((consp initial-input) (car initial-input))
  336. (t initial-input)))
  337. (deflist (if (listp def)
  338. def
  339. (list def))))
  340. (when (and deflist initial
  341. (stringp initial)
  342. (not (string= initial "")))
  343. ;; Both default and initial input were provided. So keep the
  344. ;; initial input and preprocess the choices list to put the
  345. ;; default at the head, then proceed with default = nil.
  346. (setq choices (delete-dups (append deflist choices))
  347. def nil))))
  348. ad-do-it))
  349. (defun completing-read-ido (prompt collection &optional predicate
  350. require-match initial-input
  351. hist def inherit-input-method)
  352. "ido-based method for reading from the minibuffer with completion.
  353. See `completing-read' for the meaning of the arguments.
  354. This function is a wrapper for `ido-completing-read' designed to
  355. be used as the value of `completing-read-function'. Importantly,
  356. it detects edge cases that ido cannot handle and uses normal
  357. completion for them."
  358. (let* (;; Set the active override and clear the "next" one so it
  359. ;; doesn't apply to nested calls.
  360. (ido-ubiquitous-active-override ido-ubiquitous-next-override)
  361. (ido-ubiquitous-next-override nil)
  362. ;; Pre-expand list of possible completions
  363. (collection
  364. (all-completions "" collection predicate))
  365. ;; Don't need this any more
  366. (predicate nil)
  367. ;; Check for conditions that ido can't or shouldn't handle
  368. (ido-allowed
  369. (and ido-mode
  370. ido-ubiquitous-mode
  371. ;; Don't use ido if there are no completions.
  372. collection
  373. ;; Check for disable override
  374. (not (eq ido-ubiquitous-active-override 'disable))
  375. ;; Can't handle this arg
  376. (not inherit-input-method)
  377. ;; Can't handle this being set
  378. (not (bound-and-true-p completion-extra-properties))))
  379. (comp-read-fun
  380. (if ido-allowed
  381. 'ido-ubiquitous-completing-read
  382. ido-ubiquitous-fallback-completing-read-function)))
  383. (funcall comp-read-fun
  384. prompt collection predicate
  385. require-match initial-input
  386. hist def inherit-input-method)))
  387. ;;; Old-style default support
  388. (defvar ido-ubiquitous-initial-item nil
  389. "The first item selected when ido starts.
  390. This is initialized to the first item in the list of completions
  391. when ido starts, and is cleared when any character is entered
  392. into the prompt or the list is cycled. If it is non-nil and still
  393. equal to the first item in the completion list when ido exits,
  394. then if `ido-ubiquitous-enable-old-style-default' is
  395. non-nil, ido returns an empty string instead of the first item on
  396. the list.")
  397. (defadvice ido-read-internal (before clear-initial-item activate)
  398. (setq ido-ubiquitous-initial-item nil))
  399. (defadvice ido-make-choice-list (after set-initial-item activate)
  400. (when (and ad-return-value (listp ad-return-value))
  401. (setq ido-ubiquitous-initial-item (car ad-return-value))))
  402. (defadvice ido-next-match (after clear-initial-item activate)
  403. (setq ido-ubiquitous-initial-item nil))
  404. (defadvice ido-prev-match (after clear-initial-item activate)
  405. (setq ido-ubiquitous-initial-item nil))
  406. (defadvice ido-exit-minibuffer (around compatibility activate)
  407. "Emulate a quirk of `completing-read'.
  408. > If the input is null, `completing-read' returns DEF, or the
  409. > first element of the list of default values, or an empty string
  410. > if DEF is nil, regardless of the value of REQUIRE-MATCH.
  411. See `ido-ubiquitous-enable-old-style-default', which
  412. controls whether this advice has any effect."
  413. (not current-prefix-arg)
  414. (let* ((enable-oldstyle
  415. (and
  416. ;; Completing a list, not a buffer or file
  417. (eq ido-cur-item 'list)
  418. ;; Only enable if we are replacing `completing-read'
  419. ido-ubiquitous-this-call-replaces-completing-read
  420. ;; Default is nil
  421. (null ido-default-item)
  422. ;; Input is empty
  423. (string= ido-text "")
  424. ;; Old-style default enabled
  425. (if ido-ubiquitous-active-override
  426. (eq ido-ubiquitous-active-override 'enable-old)
  427. ido-ubiquitous-enable-old-style-default)
  428. ;; First item on the list hasn't changed
  429. (string= (car ido-cur-list)
  430. ido-ubiquitous-initial-item)))
  431. ;; Prefix inverts oldstyle behavior
  432. (should-invert current-prefix-arg)
  433. (actually-enable-oldstyle
  434. (if should-invert (not enable-oldstyle) enable-oldstyle)))
  435. (if actually-enable-oldstyle
  436. (ido-select-text)
  437. ad-do-it))
  438. (setq ido-ubiquitous-initial-item nil))
  439. ;;; Overrides
  440. (defun ido-ubiquitous-restore-default-overrides (&optional save)
  441. "Re-add the default overrides for ido-ubiquitous.
  442. This will ensure that the default overrides are all present and
  443. at the head of the list in `ido-ubiquitous-command-overrides' and
  444. `ido-ubiquitous-function-overrides'. User-added overrides will
  445. not be removed, but they may be masked if one of the default
  446. overrides affects the same functions.
  447. With a prefix arg, also save the above variables' new values for
  448. future sessions."
  449. (interactive "P")
  450. (let ((setter (if save
  451. 'customize-save-variable
  452. 'customize-set-variable)))
  453. (loop for (var def) in '((ido-ubiquitous-command-overrides
  454. ido-ubiquitous-default-command-overrides)
  455. (ido-ubiquitous-function-overrides
  456. ido-ubiquitous-default-function-overrides))
  457. do (let* ((curval (eval var))
  458. (defval (eval def))
  459. (newval (delete-dups (append defval curval))))
  460. (funcall setter var newval)))
  461. (message (if save
  462. "ido-ubiquitous: Restored default command and function overrides and saved for future sessions."
  463. "ido-ubiquitous: Restored default command and function overrides for current session only."))))
  464. (defmacro ido-ubiquitous-with-override (override &rest body)
  465. "Eval BODY with specicified OVERRIDE in place.
  466. The OVERRIDE argument is evaluated normally, so if it is a
  467. literal symbol, it must be quoted.
  468. See `ido-ubiquitous-command-overrides' for valid override types."
  469. ;; Eval override
  470. (setq override (eval override))
  471. `(let ((ido-ubiquitous-next-override ',override))
  472. ,@body))
  473. ;; (append
  474. ;; (case override
  475. ;; (disable
  476. ;; '(let ((ido-ubiquitous-disable-for-one-command t))))
  477. ;; (enable
  478. ;; '(let ((ido-ubiquitous-newstyle-default-for-one-command t))))
  479. ;; (enable-old
  480. ;; '(let ((ido-ubiquitous-oldstyle-default-for-one-command t))))
  481. ;; ;; No override
  482. ;; (t '(progn)))
  483. ;; body))
  484. (put 'ido-ubiquitous-with-override 'lisp-indent-function
  485. (eval-when-compile (get 'prog1 'lisp-indent-function)))
  486. (defun ido-ubiquitous-spec-match (spec symbol)
  487. "Returns t if SPEC matches SYMBOL (which should be a function name).
  488. See `ido-ubiquitous-command-overrides'."
  489. (destructuring-bind (type text) spec
  490. (let ((matcher (cdr (assoc type ido-ubiquitous-spec-matchers)))
  491. (text (ido-ubiquitous--as-string text))
  492. (symname (ido-ubiquitous--as-string symbol)))
  493. (when (null matcher)
  494. (error "ido-ubiquitous: Unknown match spec type \"%s\". See `ido-ubiquitous-spec-matchers' for valid types." type))
  495. (funcall matcher text symname))))
  496. (defun ido-ubiquitous-get-command-override (cmd)
  497. "Return the override associated with the command CMD.
  498. If there is no override set for CMD in
  499. `ido-ubiquitous-command-overrides', return nil."
  500. (loop for (action . spec) in ido-ubiquitous-command-overrides
  501. when (memq action '(disable enable enable-old nil))
  502. when (ido-ubiquitous-spec-match spec cmd)
  503. return action
  504. finally return nil))
  505. (defadvice call-interactively (around ido-ubiquitous activate)
  506. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  507. (ido-ubiquitous-with-override
  508. (ido-ubiquitous-get-command-override (ad-get-arg 0))
  509. ad-do-it))
  510. (defadvice command-execute (around ido-ubiquitous activate)
  511. "Implements the behavior specified in `ido-ubiquitous-command-overrides'."
  512. (ido-ubiquitous-with-override
  513. (ido-ubiquitous-get-command-override (ad-get-arg 0))
  514. ad-do-it))
  515. ;;; Other
  516. (defun ido-ubiquitous-warn-about-ido-disabled ()
  517. "Warn if ido-ubiquitous is enabled without ido.
  518. Don't warn if emacs is still initializing, since ido-ubiquitous
  519. could be enabled first during init."
  520. (when (and ido-ubiquitous-mode
  521. after-init-time
  522. (not (bound-and-true-p ido-mode)))
  523. (warn "ido-ubiquitous-mode enabled without ido mode. ido-ubiquitous requires ido mode to be enabled.")))
  524. (defun ido-ubiquitous-initialize ()
  525. "Do initial setup for ido-ubiquitous.
  526. This only needs to be called once when the file is first loaded."
  527. ;; Clean up old versions of ido-ubiquitous that defined advice on
  528. ;; `completing-read' instead of modifying
  529. ;; `completing-read-function'.
  530. (when (ad-find-advice 'completing-read 'around 'ido-ubiquitous)
  531. (ad-remove-advice 'completing-read 'around 'ido-ubiquitous)
  532. (ad-activate 'completing-read))
  533. ;; Make sure the mode is turned on/off as specified by the value of
  534. ;; the mode variable
  535. (ido-ubiquitous-mode (if ido-ubiquitous-mode 1 0)))
  536. (ido-ubiquitous-initialize)
  537. (provide 'ido-ubiquitous) ;;; ido-ubiquitous.el ends here