ido-ubiquitous.el 26 KB

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