ido-ubiquitous.el 26 KB

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