viper-util.el 53 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551
  1. ;;; viper-util.el --- Utilities used by viper.el
  2. ;; Copyright (C) 1994-1997, 1999-2017 Free Software Foundation, Inc.
  3. ;; Author: Michael Kifer <kifer@cs.stonybrook.edu>
  4. ;; Package: viper
  5. ;; This file is part of GNU Emacs.
  6. ;; GNU Emacs is free software: you can redistribute it and/or modify
  7. ;; it under the terms of the GNU General Public License as published by
  8. ;; the Free Software Foundation, either version 3 of the License, or
  9. ;; (at your option) any later version.
  10. ;; GNU Emacs is distributed in the hope that it will be useful,
  11. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ;; GNU General Public License for more details.
  14. ;; You should have received a copy of the GNU General Public License
  15. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  16. ;;; Commentary:
  17. ;;; Code:
  18. (provide 'viper-util)
  19. ;; Compiler pacifier
  20. (defvar viper-overriding-map)
  21. (defvar viper-minibuffer-current-face)
  22. (defvar viper-minibuffer-insert-face)
  23. (defvar viper-minibuffer-vi-face)
  24. (defvar viper-minibuffer-emacs-face)
  25. (defvar viper-replace-overlay-face)
  26. (defvar viper-fast-keyseq-timeout)
  27. (defvar ex-unix-type-shell)
  28. (defvar ex-unix-type-shell-options)
  29. (defvar viper-ex-tmp-buf-name)
  30. (defvar viper-syntax-preference)
  31. (defvar viper-saved-mark)
  32. (require 'ring)
  33. (eval-and-compile
  34. (unless (fboundp 'declare-function) (defmacro declare-function (&rest r))))
  35. ;; end pacifier
  36. (require 'viper-init)
  37. (defalias 'viper-overlay-p
  38. (if (featurep 'xemacs) 'extentp 'overlayp))
  39. (defalias 'viper-make-overlay
  40. (if (featurep 'xemacs) 'make-extent 'make-overlay))
  41. (defalias 'viper-overlay-live-p
  42. (if (featurep 'xemacs) 'extent-live-p 'overlayp))
  43. (defalias 'viper-move-overlay
  44. (if (featurep 'xemacs) 'set-extent-endpoints 'move-overlay))
  45. (defalias 'viper-overlay-start
  46. (if (featurep 'xemacs) 'extent-start-position 'overlay-start))
  47. (defalias 'viper-overlay-end
  48. (if (featurep 'xemacs) 'extent-end-position 'overlay-end))
  49. (defalias 'viper-overlay-get
  50. (if (featurep 'xemacs) 'extent-property 'overlay-get))
  51. (defalias 'viper-overlay-put
  52. (if (featurep 'xemacs) 'set-extent-property 'overlay-put))
  53. (defalias 'viper-read-event
  54. (if (featurep 'xemacs) 'next-command-event 'read-event))
  55. (defalias 'viper-characterp
  56. (if (featurep 'xemacs) 'characterp 'integerp))
  57. (defalias 'viper-int-to-char
  58. (if (featurep 'xemacs) 'int-to-char 'identity))
  59. (defalias 'viper-get-face
  60. (if (featurep 'xemacs) 'get-face 'facep))
  61. (defalias 'viper-color-defined-p
  62. (if (featurep 'xemacs) 'valid-color-name-p 'x-color-defined-p))
  63. (defalias 'viper-iconify
  64. (if (featurep 'xemacs) 'iconify-frame 'iconify-or-deiconify-frame))
  65. ;; CHAR is supposed to be a char or an integer (positive or negative)
  66. ;; LIST is a list of chars, nil, and negative numbers
  67. ;; Check if CHAR is a member by trying to convert in characters, if necessary.
  68. ;; Introduced for compatibility with XEmacs, where integers are not the same as
  69. ;; chars.
  70. (defun viper-memq-char (char list)
  71. (cond ((and (integerp char) (>= char 0))
  72. (memq (viper-int-to-char char) list))
  73. ((memq char list))))
  74. ;; Check if char-or-int and char are the same as characters
  75. (defun viper-char-equal (char-or-int char)
  76. (cond ((and (integerp char-or-int) (>= char-or-int 0))
  77. (= (viper-int-to-char char-or-int) char))
  78. ((eq char-or-int char))))
  79. ;; Like =, but accommodates null and also is t for eq-objects
  80. (defun viper= (char char1)
  81. (cond ((eq char char1) t)
  82. ((and (viper-characterp char) (viper-characterp char1))
  83. (= char char1))
  84. (t nil)))
  85. (defsubst viper-color-display-p ()
  86. (if (featurep 'xemacs) (eq (device-class (selected-device)) 'color)
  87. (x-display-color-p)))
  88. (defun viper-get-cursor-color (&optional frame)
  89. (if (featurep 'xemacs)
  90. (color-instance-name
  91. (frame-property (or frame (selected-frame)) 'cursor-color))
  92. (cdr (assoc 'cursor-color (frame-parameters)))))
  93. (defmacro viper-frame-value (variable)
  94. "Return the value of VARIABLE local to the current frame, if there is one.
  95. Otherwise return the normal value."
  96. `(if (featurep 'xemacs)
  97. ,variable
  98. ;; Frame-local variables are obsolete from Emacs 22.2 onwards,
  99. ;; so we do it by hand instead.
  100. ;; Buffer-local values take precedence over frame-local ones.
  101. (if (local-variable-p ',variable)
  102. ,variable
  103. ;; Distinguish between no frame parameter and a frame parameter
  104. ;; with a value of nil.
  105. (let ((fp (assoc ',variable (frame-parameters))))
  106. (if fp (cdr fp)
  107. ,variable)))))
  108. ;; cursor colors
  109. (defun viper-change-cursor-color (new-color &optional frame)
  110. (if (and (viper-window-display-p) (viper-color-display-p)
  111. (stringp new-color) (viper-color-defined-p new-color)
  112. (not (string= new-color (viper-get-cursor-color))))
  113. (if (featurep 'xemacs)
  114. (set-frame-property
  115. (or frame (selected-frame))
  116. 'cursor-color (make-color-instance new-color))
  117. (modify-frame-parameters
  118. (or frame (selected-frame))
  119. (list (cons 'cursor-color new-color))))))
  120. ;; Note that the colors this function uses might not be those
  121. ;; associated with FRAME, if there are frame-local values.
  122. ;; This was equally true before the advent of viper-frame-value.
  123. ;; Now it could be changed by passing frame to v-f-v.
  124. (defun viper-set-cursor-color-according-to-state (&optional frame)
  125. (cond ((eq viper-current-state 'replace-state)
  126. (viper-change-cursor-color
  127. (viper-frame-value viper-replace-overlay-cursor-color)
  128. frame))
  129. ((and (eq viper-current-state 'emacs-state)
  130. (viper-frame-value viper-emacs-state-cursor-color))
  131. (viper-change-cursor-color
  132. (viper-frame-value viper-emacs-state-cursor-color)
  133. frame))
  134. ((eq viper-current-state 'insert-state)
  135. (viper-change-cursor-color
  136. (viper-frame-value viper-insert-state-cursor-color)
  137. frame))
  138. (t
  139. (viper-change-cursor-color
  140. (viper-frame-value viper-vi-state-cursor-color)
  141. frame))))
  142. ;; By default, saves current frame cursor color before changing viper state
  143. (defun viper-save-cursor-color (before-which-mode)
  144. (if (and (viper-window-display-p) (viper-color-display-p))
  145. (let ((color (viper-get-cursor-color)))
  146. (if (and (stringp color) (viper-color-defined-p color)
  147. ;; there is something fishy in that the color is not saved if
  148. ;; it is the same as frames default cursor color. need to be
  149. ;; checked.
  150. (not (string= color
  151. (viper-frame-value
  152. viper-replace-overlay-cursor-color))))
  153. (modify-frame-parameters
  154. (selected-frame)
  155. (list
  156. (cons
  157. (cond ((eq before-which-mode 'before-replace-mode)
  158. 'viper-saved-cursor-color-in-replace-mode)
  159. ((eq before-which-mode 'before-emacs-mode)
  160. 'viper-saved-cursor-color-in-emacs-mode)
  161. (t
  162. 'viper-saved-cursor-color-in-insert-mode))
  163. color)))))))
  164. (defun viper-get-saved-cursor-color-in-replace-mode ()
  165. (or
  166. (funcall
  167. (if (featurep 'emacs) 'frame-parameter 'frame-property)
  168. (selected-frame)
  169. 'viper-saved-cursor-color-in-replace-mode)
  170. (or (and (eq viper-current-state 'emacs-mode)
  171. (viper-frame-value viper-emacs-state-cursor-color))
  172. (viper-frame-value viper-vi-state-cursor-color))))
  173. (defun viper-get-saved-cursor-color-in-insert-mode ()
  174. (or
  175. (funcall
  176. (if (featurep 'emacs) 'frame-parameter 'frame-property)
  177. (selected-frame)
  178. 'viper-saved-cursor-color-in-insert-mode)
  179. (or (and (eq viper-current-state 'emacs-mode)
  180. (viper-frame-value viper-emacs-state-cursor-color))
  181. (viper-frame-value viper-vi-state-cursor-color))))
  182. (defun viper-get-saved-cursor-color-in-emacs-mode ()
  183. (or
  184. (funcall
  185. (if (featurep 'emacs) 'frame-parameter 'frame-property)
  186. (selected-frame)
  187. 'viper-saved-cursor-color-in-emacs-mode)
  188. (viper-frame-value viper-vi-state-cursor-color)))
  189. ;; restore cursor color from replace overlay
  190. (defun viper-restore-cursor-color(after-which-mode)
  191. (if (viper-overlay-p viper-replace-overlay)
  192. (viper-change-cursor-color
  193. (cond ((eq after-which-mode 'after-replace-mode)
  194. (viper-get-saved-cursor-color-in-replace-mode))
  195. ((eq after-which-mode 'after-emacs-mode)
  196. (viper-get-saved-cursor-color-in-emacs-mode))
  197. (t (viper-get-saved-cursor-color-in-insert-mode)))
  198. )))
  199. ;; Check the current version against the major and minor version numbers
  200. ;; using op: cur-vers op major.minor If emacs-major-version or
  201. ;; emacs-minor-version are not defined, we assume that the current version
  202. ;; is hopelessly outdated. We assume that emacs-major-version and
  203. ;; emacs-minor-version are defined. Otherwise, for Emacs/XEmacs 19, if the
  204. ;; current minor version is < 10 (xemacs) or < 23 (emacs) the return value
  205. ;; will be nil (when op is =, >, or >=) and t (when op is <, <=), which may be
  206. ;; incorrect. However, this gives correct result in our cases, since we are
  207. ;; testing for sufficiently high Emacs versions.
  208. (defun viper-check-version (op major minor &optional type-of-emacs)
  209. (if (and (boundp 'emacs-major-version) (boundp 'emacs-minor-version))
  210. (and (cond ((eq type-of-emacs 'xemacs) (featurep 'xemacs))
  211. ((eq type-of-emacs 'emacs) (featurep 'emacs))
  212. (t t))
  213. (cond ((eq op '=) (and (= emacs-minor-version minor)
  214. (= emacs-major-version major)))
  215. ((memq op '(> >= < <=))
  216. (and (or (funcall op emacs-major-version major)
  217. (= emacs-major-version major))
  218. (if (= emacs-major-version major)
  219. (funcall op emacs-minor-version minor)
  220. t)))
  221. (t
  222. (error "%S: Invalid op in viper-check-version" op))))
  223. (cond ((memq op '(= > >=)) nil)
  224. ((memq op '(< <=)) t))))
  225. (defun viper-get-visible-buffer-window (wind)
  226. (if (featurep 'xemacs)
  227. (get-buffer-window wind t)
  228. (get-buffer-window wind 'visible)))
  229. ;; Return line position.
  230. ;; If pos is 'start then returns position of line start.
  231. ;; If pos is 'end, returns line end. If pos is 'mid, returns line center.
  232. ;; Pos = 'indent returns beginning of indentation.
  233. ;; Otherwise, returns point. Current point is not moved in any case."
  234. (defun viper-line-pos (pos)
  235. (let ((cur-pos (point))
  236. (result))
  237. (cond
  238. ((equal pos 'start)
  239. (beginning-of-line))
  240. ((equal pos 'end)
  241. (end-of-line))
  242. ((equal pos 'mid)
  243. (goto-char (+ (viper-line-pos 'start) (viper-line-pos 'end) 2)))
  244. ((equal pos 'indent)
  245. (back-to-indentation))
  246. (t nil))
  247. (setq result (point))
  248. (goto-char cur-pos)
  249. result))
  250. ;; Emacs used to count each multibyte character as several positions in the buffer,
  251. ;; so we had to use Emacs's chars-in-region to count characters. Since 20.3,
  252. ;; Emacs counts multibyte characters as 1 position. XEmacs has always been
  253. ;; counting each char as just one pos. So, now we can simply subtract beg from
  254. ;; end to determine the number of characters in a region.
  255. (defun viper-chars-in-region (beg end &optional preserve-sign)
  256. ;;(let ((count (abs (if (fboundp 'chars-in-region)
  257. ;; (chars-in-region beg end)
  258. ;; (- end beg)))))
  259. (let ((count (abs (- end beg))))
  260. (if (and (< end beg) preserve-sign)
  261. (- count)
  262. count)))
  263. ;; Test if POS is between BEG and END
  264. (defsubst viper-pos-within-region (pos beg end)
  265. (and (>= pos (min beg end)) (>= (max beg end) pos)))
  266. ;; Like move-marker but creates a virgin marker if arg isn't already a marker.
  267. ;; The first argument must eval to a variable name.
  268. ;; Arguments: (var-name position &optional buffer).
  269. ;;
  270. ;; This is useful for moving markers that are supposed to be local.
  271. ;; For this, VAR-NAME should be made buffer-local with nil as a default.
  272. ;; Then, each time this var is used in `viper-move-marker-locally' in a new
  273. ;; buffer, a new marker will be created.
  274. (defun viper-move-marker-locally (var pos &optional buffer)
  275. (if (markerp (eval var))
  276. ()
  277. (set var (make-marker)))
  278. (move-marker (eval var) pos buffer))
  279. ;; Print CONDITIONS as a message.
  280. (defun viper-message-conditions (conditions)
  281. (let ((case (car conditions)) (msg (cdr conditions)))
  282. (if (null msg)
  283. (message "%s" case)
  284. (message "%s: %s" case (mapconcat 'prin1-to-string msg " ")))
  285. (beep 1)))
  286. ;;; List/alist utilities
  287. ;; Convert LIST to an alist
  288. (defun viper-list-to-alist (lst)
  289. (let ((alist))
  290. (while lst
  291. (setq alist (cons (list (car lst)) alist))
  292. (setq lst (cdr lst)))
  293. alist))
  294. ;; Convert ALIST to a list.
  295. (defun viper-alist-to-list (alst)
  296. (let ((lst))
  297. (while alst
  298. (setq lst (cons (car (car alst)) lst))
  299. (setq alst (cdr alst)))
  300. lst))
  301. ;; Filter ALIST using REGEXP. Return alist whose elements match the regexp.
  302. (defun viper-filter-alist (regexp alst)
  303. (interactive "s x")
  304. (let ((outalst) (inalst alst))
  305. (while (car inalst)
  306. (if (string-match regexp (car (car inalst)))
  307. (setq outalst (cons (car inalst) outalst)))
  308. (setq inalst (cdr inalst)))
  309. outalst))
  310. ;; Filter LIST using REGEXP. Return list whose elements match the regexp.
  311. (defun viper-filter-list (regexp lst)
  312. (interactive "s x")
  313. (let ((outlst) (inlst lst))
  314. (while (car inlst)
  315. (if (string-match regexp (car inlst))
  316. (setq outlst (cons (car inlst) outlst)))
  317. (setq inlst (cdr inlst)))
  318. outlst))
  319. ;; Append LIS2 to LIS1, both alists, by side-effect and returns LIS1
  320. ;; LIS2 is modified by filtering it: deleting its members of the form
  321. ;; (car elt) such that (car elt') is in LIS1.
  322. (defun viper-append-filter-alist (lis1 lis2)
  323. (let ((temp lis1)
  324. elt)
  325. ;;filter-append the second list
  326. (while temp
  327. ;; delete all occurrences
  328. (while (setq elt (assoc (car (car temp)) lis2))
  329. (setq lis2 (delq elt lis2)))
  330. (setq temp (cdr temp)))
  331. (append lis1 lis2)))
  332. (declare-function viper-forward-Word "viper-cmd" (arg))
  333. ;;; Support for :e, :r, :w file globbing
  334. ;; Glob the file spec.
  335. ;; This function is designed to work under Unix.
  336. (defun viper-glob-unix-files (filespec)
  337. (let ((gshell
  338. (cond (ex-unix-type-shell shell-file-name)
  339. (t "sh"))) ; probably Unix anyway
  340. (gshell-options
  341. ;; using cond in anticipation of further additions
  342. (cond (ex-unix-type-shell-options)
  343. ))
  344. (command (cond (viper-ms-style-os-p (format "\"ls -1 -d %s\"" filespec))
  345. (t (format "ls -1 -d %s" filespec))))
  346. status)
  347. (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name)
  348. (erase-buffer)
  349. (setq status
  350. (if gshell-options
  351. (call-process gshell nil t nil
  352. gshell-options
  353. "-c"
  354. command)
  355. (call-process gshell nil t nil
  356. "-c"
  357. command)))
  358. (goto-char (point-min))
  359. ;; Issue an error, if no match.
  360. (unless (eq 0 status)
  361. (save-excursion
  362. (skip-chars-forward " \t\n")
  363. (if (looking-at "ls:")
  364. (viper-forward-Word 1))
  365. (error "%s: %s"
  366. (if (stringp gshell)
  367. gshell
  368. "shell")
  369. (buffer-substring (point) (viper-line-pos 'end)))
  370. ))
  371. (goto-char (point-min))
  372. (viper-get-filenames-from-buffer 'one-per-line))
  373. ))
  374. ;; Interpret the stuff in the buffer as a list of file names
  375. ;; return a list of file names listed in the buffer beginning at point
  376. ;; If optional arg is supplied, assume each filename is listed on a separate
  377. ;; line
  378. (defun viper-get-filenames-from-buffer (&optional one-per-line)
  379. (let ((skip-chars (if one-per-line "\t\n" " \t\n"))
  380. result fname delim)
  381. (skip-chars-forward skip-chars)
  382. (while (not (eobp))
  383. (if (cond ((looking-at "\"")
  384. (setq delim ?\")
  385. (re-search-forward "[^\"]+" nil t)) ; noerror
  386. ((looking-at "'")
  387. (setq delim ?')
  388. (re-search-forward "[^']+" nil t)) ; noerror
  389. (t
  390. (re-search-forward
  391. (concat "[^" skip-chars "]+") nil t))) ;noerror
  392. (setq fname
  393. (buffer-substring (match-beginning 0) (match-end 0))))
  394. (if delim
  395. (forward-char 1))
  396. (skip-chars-forward " \t\n")
  397. (setq result (cons fname result)))
  398. result))
  399. ;; convert MS-DOS wildcards to regexp
  400. (defun viper-wildcard-to-regexp (wcard)
  401. (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name)
  402. (erase-buffer)
  403. (insert wcard)
  404. (goto-char (point-min))
  405. (while (not (eobp))
  406. (skip-chars-forward "^*?.\\\\")
  407. (cond ((eq (char-after (point)) ?*) (insert ".")(forward-char 1))
  408. ((eq (char-after (point)) ?.) (insert "\\")(forward-char 1))
  409. ((eq (char-after (point)) ?\\) (insert "\\")(forward-char 1))
  410. ((eq (char-after (point)) ??) (delete-char 1)(insert ".")))
  411. )
  412. (buffer-string)
  413. ))
  414. ;; glob windows files
  415. ;; LIST is expected to be in reverse order
  416. (defun viper-glob-mswindows-files (filespec)
  417. (let ((case-fold-search t)
  418. tmp tmp2)
  419. (with-current-buffer (get-buffer-create viper-ex-tmp-buf-name)
  420. (erase-buffer)
  421. (insert filespec)
  422. (goto-char (point-min))
  423. (setq tmp (viper-get-filenames-from-buffer))
  424. (while tmp
  425. (setq tmp2 (cons (directory-files
  426. ;; the directory part
  427. (or (file-name-directory (car tmp))
  428. "")
  429. t ; return full names
  430. ;; the regexp part: globs the file names
  431. (concat "^"
  432. (viper-wildcard-to-regexp
  433. (file-name-nondirectory (car tmp)))
  434. "$"))
  435. tmp2))
  436. (setq tmp (cdr tmp)))
  437. (reverse (apply 'append tmp2)))))
  438. ;;; Insertion ring
  439. ;; Rotate RING's index. DIRection can be positive or negative.
  440. (defun viper-ring-rotate1 (ring dir)
  441. (if (and (ring-p ring) (> (ring-length ring) 0))
  442. (progn
  443. (setcar ring (cond ((> dir 0)
  444. (ring-plus1 (car ring) (ring-length ring)))
  445. ((< dir 0)
  446. (ring-minus1 (car ring) (ring-length ring)))
  447. ;; don't rotate if dir = 0
  448. (t (car ring))))
  449. (viper-current-ring-item ring)
  450. )))
  451. (defun viper-special-ring-rotate1 (ring dir)
  452. (if (memq viper-intermediate-command
  453. '(repeating-display-destructive-command
  454. repeating-insertion-from-ring))
  455. (viper-ring-rotate1 ring dir)
  456. ;; don't rotate otherwise
  457. (viper-ring-rotate1 ring 0)))
  458. ;; current ring item; if N is given, then so many items back from the
  459. ;; current
  460. (defun viper-current-ring-item (ring &optional n)
  461. (setq n (or n 0))
  462. (if (and (ring-p ring) (> (ring-length ring) 0))
  463. (aref (cdr (cdr ring)) (mod (- (car ring) 1 n) (ring-length ring)))))
  464. ;; Push item onto ring. The second argument is a ring-variable, not value.
  465. (defun viper-push-onto-ring (item ring-var)
  466. (or (ring-p (eval ring-var))
  467. (set ring-var (make-ring (eval (intern (format "%S-size" ring-var))))))
  468. (or (null item) ; don't push nil
  469. (and (stringp item) (string= item "")) ; or empty strings
  470. (equal item (viper-current-ring-item (eval ring-var))) ; or old stuff
  471. ;; Since viper-set-destructive-command checks if we are inside
  472. ;; viper-repeat, we don't check whether this-command-keys is a `.'. The
  473. ;; cmd viper-repeat makes a call to the current function only if `.' is
  474. ;; executing a command from the command history. It doesn't call the
  475. ;; push-onto-ring function if `.' is simply repeating the last
  476. ;; destructive command. We only check for ESC (which happens when we do
  477. ;; insert with a prefix argument, or if this-command-keys doesn't give
  478. ;; anything meaningful (in that case we don't know what to show to the
  479. ;; user).
  480. (and (eq ring-var 'viper-command-ring)
  481. (string-match "\\([0-9]*\e\\|^[ \t]*$\\|escape\\)"
  482. (viper-array-to-string (this-command-keys))))
  483. (viper-ring-insert (eval ring-var) item))
  484. )
  485. ;; removing elts from ring seems to break it
  486. (defun viper-cleanup-ring (ring)
  487. (or (< (ring-length ring) 2)
  488. (null (viper-current-ring-item ring))
  489. ;; last and previous equal
  490. (if (equal (viper-current-ring-item ring)
  491. (viper-current-ring-item ring 1))
  492. (viper-ring-pop ring))))
  493. ;; ring-remove seems to be buggy, so we concocted this for our purposes.
  494. (defun viper-ring-pop (ring)
  495. (let* ((ln (ring-length ring))
  496. (vec (cdr (cdr ring)))
  497. (veclen (length vec))
  498. (hd (car ring))
  499. (idx (max 0 (ring-minus1 hd ln)))
  500. (top-elt (aref vec idx)))
  501. ;; shift elements
  502. (while (< (1+ idx) veclen)
  503. (aset vec idx (aref vec (1+ idx)))
  504. (setq idx (1+ idx)))
  505. (aset vec idx nil)
  506. (setq hd (max 0 (ring-minus1 hd ln)))
  507. (if (= hd (1- ln)) (setq hd 0))
  508. (setcar ring hd) ; move head
  509. (setcar (cdr ring) (max 0 (1- ln))) ; adjust length
  510. top-elt
  511. ))
  512. (defun viper-ring-insert (ring item)
  513. (let* ((ln (ring-length ring))
  514. (vec (cdr (cdr ring)))
  515. (veclen (length vec))
  516. (hd (car ring))
  517. (vecpos-after-hd (if (= hd 0) ln hd))
  518. (idx ln))
  519. (if (= ln veclen)
  520. (progn
  521. (aset vec hd item) ; hd is always 1+ the actual head index in vec
  522. (setcar ring (ring-plus1 hd ln)))
  523. (setcar (cdr ring) (1+ ln))
  524. (setcar ring (ring-plus1 vecpos-after-hd (1+ ln)))
  525. (while (and (>= idx vecpos-after-hd) (> ln 0))
  526. (aset vec idx (aref vec (1- idx)))
  527. (setq idx (1- idx)))
  528. (aset vec vecpos-after-hd item))
  529. item))
  530. ;;; String utilities
  531. ;; If STRING is longer than MAX-LEN, truncate it and print ...... instead
  532. ;; PRE-STRING is a string to prepend to the abbrev string.
  533. ;; POST-STRING is a string to append to the abbrev string.
  534. ;; ABBREV_SIGN is a string to be inserted before POST-STRING
  535. ;; if the orig string was truncated.
  536. (defun viper-abbreviate-string (string max-len
  537. pre-string post-string abbrev-sign)
  538. (let (truncated-str)
  539. (setq truncated-str
  540. (if (stringp string)
  541. (substring string 0 (min max-len (length string)))))
  542. (cond ((null truncated-str) "")
  543. ((> (length string) max-len)
  544. (format "%s%s%s%s"
  545. pre-string truncated-str abbrev-sign post-string))
  546. (t (format "%s%s%s" pre-string truncated-str post-string)))))
  547. ;; tells if we are over a whitespace-only line
  548. (defsubst viper-over-whitespace-line ()
  549. (save-excursion
  550. (beginning-of-line)
  551. (looking-at "^[ \t]*$")))
  552. ;;; Saving settings in custom file
  553. ;; Save the current setting of VAR in CUSTOM-FILE.
  554. ;; If given, MESSAGE is a message to be displayed after that.
  555. ;; This message is erased after 2 secs, if erase-msg is non-nil.
  556. ;; Arguments: var message custom-file &optional erase-message
  557. (defun viper-save-setting (var message custom-file &optional erase-msg)
  558. (let* ((var-name (symbol-name var))
  559. (var-val (if (boundp var) (eval var)))
  560. (regexp (format "^[^;]*%s[ \t\n]*[a-zA-Z---_']*[ \t\n)]" var-name))
  561. (buf (find-file-noselect (substitute-in-file-name custom-file)))
  562. )
  563. (message "%s" (or message ""))
  564. (with-current-buffer buf
  565. (goto-char (point-min))
  566. (if (re-search-forward regexp nil t)
  567. (let ((reg-end (1- (match-end 0))))
  568. (search-backward var-name)
  569. (delete-region (match-beginning 0) reg-end)
  570. (goto-char (match-beginning 0))
  571. (insert (format "%s '%S" var-name var-val)))
  572. (goto-char (point-max))
  573. (if (not (bolp)) (insert "\n"))
  574. (insert (format "(setq %s '%S)\n" var-name var-val)))
  575. (save-buffer))
  576. (kill-buffer buf)
  577. (if erase-msg
  578. (progn
  579. (sit-for 2)
  580. (message "")))
  581. ))
  582. ;; Save STRING in CUSTOM-FILE. If PATTERN is non-nil, remove strings that
  583. ;; match this pattern.
  584. (defun viper-save-string-in-file (string custom-file &optional pattern)
  585. (let ((buf (find-file-noselect (substitute-in-file-name custom-file))))
  586. (with-current-buffer buf
  587. (let (buffer-read-only)
  588. (goto-char (point-min))
  589. (if pattern (delete-matching-lines pattern))
  590. (goto-char (point-max))
  591. (if string (insert string))
  592. (save-buffer)))
  593. (kill-buffer buf)
  594. ))
  595. ;; This is a simple-minded check for whether a file is under version control.
  596. ;; If file,v exists but file doesn't, this file is considered to be not checked
  597. ;; in and not checked out for the purpose of patching (since patch won't be
  598. ;; able to read such a file anyway).
  599. ;; FILE is a string representing file name
  600. ;;(defun viper-file-under-version-control (file)
  601. ;; (let* ((filedir (file-name-directory file))
  602. ;; (file-nondir (file-name-nondirectory file))
  603. ;; (trial (concat file-nondir ",v"))
  604. ;; (full-trial (concat filedir trial))
  605. ;; (full-rcs-trial (concat filedir "RCS/" trial)))
  606. ;; (and (stringp file)
  607. ;; (file-exists-p file)
  608. ;; (or
  609. ;; (and
  610. ;; (file-exists-p full-trial)
  611. ;; ;; in FAT FS, `file,v' and `file' may turn out to be the same!
  612. ;; ;; don't be fooled by this!
  613. ;; (not (equal (file-attributes file)
  614. ;; (file-attributes full-trial))))
  615. ;; ;; check if a version is in RCS/ directory
  616. ;; (file-exists-p full-rcs-trial)))
  617. ;; ))
  618. (defsubst viper-file-checked-in-p (file)
  619. (and (featurep 'vc-hooks)
  620. ;; CVS files are considered not checked in
  621. ;; FIXME: Should this deal with more than CVS?
  622. (not (memq (vc-backend file) '(nil CVS)))
  623. (if (fboundp 'vc-state)
  624. (and
  625. (not (memq (vc-state file) '(edited needs-merge)))
  626. (not (stringp (vc-state file))))
  627. ;; XEmacs has no vc-state
  628. (if (featurep 'xemacs) (not (vc-locking-user file))))))
  629. ;; checkout if visited file is checked in
  630. (defun viper-maybe-checkout (buf)
  631. (let ((file (expand-file-name (buffer-file-name buf)))
  632. (checkout-function (key-binding "\C-x\C-q")))
  633. (if (and (viper-file-checked-in-p file)
  634. (or (beep 1) t)
  635. (y-or-n-p
  636. (format
  637. "File %s is checked in. Check it out? "
  638. (viper-abbreviate-file-name file))))
  639. (with-current-buffer buf
  640. (command-execute checkout-function)))))
  641. ;;; Overlays
  642. (defun viper-put-on-search-overlay (beg end)
  643. (if (viper-overlay-p viper-search-overlay)
  644. (viper-move-overlay viper-search-overlay beg end)
  645. (setq viper-search-overlay (viper-make-overlay beg end (current-buffer)))
  646. (viper-overlay-put
  647. viper-search-overlay 'priority viper-search-overlay-priority))
  648. (viper-overlay-put viper-search-overlay 'face viper-search-face))
  649. ;; Search
  650. (defun viper-flash-search-pattern ()
  651. (if (not (viper-has-face-support-p))
  652. nil
  653. (viper-put-on-search-overlay (match-beginning 0) (match-end 0))
  654. (sit-for 2)
  655. (viper-overlay-put viper-search-overlay 'face nil)))
  656. (defun viper-hide-search-overlay ()
  657. (if (not (viper-overlay-p viper-search-overlay))
  658. (progn
  659. (setq viper-search-overlay
  660. (viper-make-overlay (point-min) (point-min) (current-buffer)))
  661. (viper-overlay-put
  662. viper-search-overlay 'priority viper-search-overlay-priority)))
  663. (viper-overlay-put viper-search-overlay 'face nil))
  664. ;; Replace state
  665. (defsubst viper-move-replace-overlay (beg end)
  666. (viper-move-overlay viper-replace-overlay beg end))
  667. (defun viper-set-replace-overlay (beg end)
  668. (if (viper-overlay-live-p viper-replace-overlay)
  669. (viper-move-replace-overlay beg end)
  670. (setq viper-replace-overlay (viper-make-overlay beg end (current-buffer)))
  671. ;; never detach
  672. (viper-overlay-put
  673. viper-replace-overlay (if (featurep 'emacs) 'evaporate 'detachable) nil)
  674. (viper-overlay-put
  675. viper-replace-overlay 'priority viper-replace-overlay-priority)
  676. ;; If Emacs will start supporting overlay maps, as it currently supports
  677. ;; text-property maps, we could do away with viper-replace-minor-mode and
  678. ;; just have keymap attached to replace overlay.
  679. ;;(viper-overlay-put
  680. ;; viper-replace-overlay
  681. ;; (if (featurep 'xemacs) 'keymap 'local-map)
  682. ;; viper-replace-map)
  683. )
  684. (if (viper-has-face-support-p)
  685. (viper-overlay-put
  686. viper-replace-overlay 'face viper-replace-overlay-face))
  687. (viper-save-cursor-color 'before-replace-mode)
  688. (viper-change-cursor-color
  689. (viper-frame-value viper-replace-overlay-cursor-color)))
  690. (defun viper-set-replace-overlay-glyphs (before-glyph after-glyph)
  691. (or (viper-overlay-live-p viper-replace-overlay)
  692. (viper-set-replace-overlay (point-min) (point-min)))
  693. (if (or (not (viper-has-face-support-p))
  694. viper-use-replace-region-delimiters)
  695. (let ((before-name (if (featurep 'xemacs) 'begin-glyph 'before-string))
  696. (after-name (if (featurep 'xemacs) 'end-glyph 'after-string)))
  697. (viper-overlay-put viper-replace-overlay before-name before-glyph)
  698. (viper-overlay-put viper-replace-overlay after-name after-glyph))))
  699. (defun viper-hide-replace-overlay ()
  700. (viper-set-replace-overlay-glyphs nil nil)
  701. (viper-restore-cursor-color 'after-replace-mode)
  702. (viper-restore-cursor-color 'after-insert-mode)
  703. (if (viper-has-face-support-p)
  704. (viper-overlay-put viper-replace-overlay 'face nil)))
  705. (defsubst viper-replace-start ()
  706. (viper-overlay-start viper-replace-overlay))
  707. (defsubst viper-replace-end ()
  708. (viper-overlay-end viper-replace-overlay))
  709. ;; Minibuffer
  710. (defun viper-set-minibuffer-overlay ()
  711. (viper-check-minibuffer-overlay)
  712. (when (viper-has-face-support-p)
  713. (viper-overlay-put
  714. viper-minibuffer-overlay 'face viper-minibuffer-current-face)
  715. (viper-overlay-put
  716. viper-minibuffer-overlay 'priority viper-minibuffer-overlay-priority)
  717. ;; never detach
  718. (viper-overlay-put
  719. viper-minibuffer-overlay
  720. (if (featurep 'emacs) 'evaporate 'detachable)
  721. nil)
  722. ;; make viper-minibuffer-overlay open-ended
  723. ;; In emacs, it is made open ended at creation time
  724. (when (featurep 'xemacs)
  725. (viper-overlay-put viper-minibuffer-overlay 'start-open nil)
  726. (viper-overlay-put viper-minibuffer-overlay 'end-open nil))))
  727. (defun viper-check-minibuffer-overlay ()
  728. (if (viper-overlay-live-p viper-minibuffer-overlay)
  729. (viper-move-overlay
  730. viper-minibuffer-overlay
  731. (if (fboundp 'minibuffer-prompt-end) (minibuffer-prompt-end) 1)
  732. (1+ (buffer-size)))
  733. (setq viper-minibuffer-overlay
  734. (if (featurep 'xemacs)
  735. (viper-make-overlay 1 (1+ (buffer-size)) (current-buffer))
  736. ;; make overlay open-ended
  737. (viper-make-overlay
  738. (if (fboundp 'minibuffer-prompt-end) (minibuffer-prompt-end) 1)
  739. (1+ (buffer-size))
  740. (current-buffer) nil 'rear-advance)))))
  741. (defsubst viper-is-in-minibuffer ()
  742. (save-match-data
  743. (string-match "\\*Minibuf-" (buffer-name))))
  744. ;;; XEmacs compatibility
  745. (defun viper-abbreviate-file-name (file)
  746. (if (featurep 'xemacs)
  747. (abbreviate-file-name file t) ; XEmacs requires addl argument
  748. (abbreviate-file-name file)))
  749. ;; Sit for VAL milliseconds. XEmacs doesn't support the millisecond arg
  750. ;; in sit-for, so this function smooths out the differences.
  751. (defsubst viper-sit-for-short (val &optional nodisp)
  752. (sit-for (/ val 1000.0) nodisp))
  753. ;; EVENT may be a single event of a sequence of events
  754. (defsubst viper-ESC-event-p (event)
  755. (let ((ESC-keys '(?\e (control \[) escape))
  756. (key (viper-event-key event)))
  757. (member key ESC-keys)))
  758. ;; checks if object is a marker, has a buffer, and points to within that buffer
  759. (defun viper-valid-marker (marker)
  760. (if (and (markerp marker) (marker-buffer marker))
  761. (let ((buf (marker-buffer marker))
  762. (pos (marker-position marker)))
  763. (with-current-buffer buf
  764. (and (<= pos (point-max)) (<= (point-min) pos))))))
  765. (defsubst viper-mark-marker ()
  766. (if (featurep 'xemacs) (mark-marker t)
  767. (mark-marker)))
  768. ;; like (set-mark-command nil) but doesn't push twice, if (car mark-ring)
  769. ;; is the same as (mark t).
  770. (defsubst viper-set-mark-if-necessary ()
  771. (setq mark-ring (delete (viper-mark-marker) mark-ring))
  772. (set-mark-command nil)
  773. (setq viper-saved-mark (point)))
  774. ;; In transient mark mode (zmacs mode), it is annoying when regions become
  775. ;; highlighted due to Viper's pushing marks. So, we deactivate marks, unless
  776. ;; the user explicitly wants highlighting, e.g., by hitting '' or ``
  777. (defun viper-deactivate-mark ()
  778. (if (featurep 'xemacs)
  779. (zmacs-deactivate-region)
  780. (deactivate-mark)))
  781. (defsubst viper-leave-region-active ()
  782. (if (featurep 'xemacs) (setq zmacs-region-stays t)))
  783. ;; Check if arg is a valid character for register
  784. ;; TYPE is a list that can contain `letter', `Letter', and `digit'.
  785. ;; Letter means lowercase letters, Letter means uppercase letters, and
  786. ;; digit means digits from 1 to 9.
  787. ;; If TYPE is nil, then down/uppercase letters and digits are allowed.
  788. (defun viper-valid-register (reg &optional type)
  789. (or type (setq type '(letter Letter digit)))
  790. (or (if (memq 'letter type)
  791. (and (<= ?a reg) (<= reg ?z)))
  792. (if (memq 'digit type)
  793. (and (<= ?1 reg) (<= reg ?9)))
  794. (if (memq 'Letter type)
  795. (and (<= ?A reg) (<= reg ?Z)))
  796. ))
  797. ;; it is suggested that an event must be copied before it is assigned to
  798. ;; last-command-event in XEmacs
  799. (defun viper-copy-event (event)
  800. (if (featurep 'xemacs) (copy-event event)
  801. event))
  802. ;; Uses different timeouts for ESC-sequences and others
  803. (defun viper-fast-keysequence-p ()
  804. (not (viper-sit-for-short
  805. (if (viper-ESC-event-p last-input-event)
  806. (viper-ESC-keyseq-timeout)
  807. viper-fast-keyseq-timeout)
  808. t)))
  809. ;; like read-event, but in XEmacs also try to convert to char, if possible
  810. (defun viper-read-event-convert-to-char ()
  811. (let (event)
  812. (if (featurep 'xemacs)
  813. (progn
  814. (setq event (next-command-event))
  815. (or (event-to-character event)
  816. event))
  817. (read-event))))
  818. ;; Viperized read-key-sequence
  819. (defun viper-read-key-sequence (prompt &optional continue-echo)
  820. (let (inhibit-quit event keyseq)
  821. (setq keyseq (read-key-sequence prompt continue-echo))
  822. (setq event (if (featurep 'xemacs)
  823. (elt keyseq 0) ; XEmacs returns vector of events
  824. (elt (listify-key-sequence keyseq) 0)))
  825. (if (viper-ESC-event-p event)
  826. (let (unread-command-events)
  827. (if (viper-fast-keysequence-p)
  828. (let ((viper-vi-global-user-minor-mode nil)
  829. (viper-vi-local-user-minor-mode nil)
  830. (viper-vi-intercept-minor-mode nil)
  831. (viper-insert-intercept-minor-mode nil)
  832. (viper-replace-minor-mode nil) ; actually unnecessary
  833. (viper-insert-global-user-minor-mode nil)
  834. (viper-insert-local-user-minor-mode nil))
  835. ;; Note: set unread-command-events only after testing for fast
  836. ;; keysequence. Otherwise, viper-fast-keysequence-p will be
  837. ;; always t -- whether there is anything after ESC or not
  838. (viper-set-unread-command-events keyseq)
  839. (setq keyseq (read-key-sequence nil)))
  840. (viper-set-unread-command-events keyseq)
  841. (setq keyseq (read-key-sequence nil)))))
  842. keyseq))
  843. ;; This function lets function-key-map convert key sequences into logical
  844. ;; keys. This does a better job than viper-read-event when it comes to kbd
  845. ;; macros, since it enables certain macros to be shared between X and TTY modes
  846. ;; by correctly mapping key sequences for Left/Right/... (on an ascii
  847. ;; terminal) into logical keys left, right, etc.
  848. (defun viper-read-key () ;; FIXME: Use `read-key'?
  849. (let ((overriding-local-map viper-overriding-map)
  850. (inhibit-quit t)
  851. help-char key)
  852. (use-global-map viper-overriding-map)
  853. (unwind-protect
  854. (setq key (elt (viper-read-key-sequence nil) 0))
  855. (use-global-map global-map))
  856. key))
  857. ;; Emacs has a bug in eventp, which causes (eventp nil) to return (nil)
  858. ;; instead of nil, if '(nil) was previously inadvertently assigned to
  859. ;; unread-command-events
  860. (defun viper-event-key (event)
  861. (or (and event (eventp event))
  862. (error "viper-event-key: Wrong type argument, eventp, %S" event))
  863. (when (if (featurep 'xemacs)
  864. (or (key-press-event-p event) (mouse-event-p event)) ; xemacs
  865. t ; emacs
  866. )
  867. (let ((mod (event-modifiers event))
  868. basis)
  869. (setq basis
  870. (if (featurep 'xemacs)
  871. ;; XEmacs
  872. (cond ((key-press-event-p event)
  873. (event-key event))
  874. ((button-event-p event)
  875. (concat "mouse-" (prin1-to-string (event-button event))))
  876. (t
  877. (error "viper-event-key: Unknown event, %S" event)))
  878. ;; Emacs doesn't handle capital letters correctly, since
  879. ;; \S-a isn't considered the same as A (it behaves as
  880. ;; plain `a' instead). So we take care of this here
  881. (cond ((and (viper-characterp event) (<= ?A event) (<= event ?Z))
  882. (setq mod nil
  883. event event))
  884. ;; Emacs has the oddity whereby characters 128+char
  885. ;; represent M-char *if* this appears inside a string.
  886. ;; So, we convert them manually to (meta char).
  887. ((and (viper-characterp event)
  888. (< ?\C-? event) (<= event 255))
  889. (setq mod '(meta)
  890. event (- event ?\C-? 1)))
  891. ((and (null mod) (eq event 'return))
  892. (setq event ?\C-m))
  893. ((and (null mod) (eq event 'space))
  894. (setq event ?\ ))
  895. ((and (null mod) (eq event 'delete))
  896. (setq event ?\C-?))
  897. ((and (null mod) (eq event 'backspace))
  898. (setq event ?\C-h))
  899. (t (event-basic-type event)))
  900. ) ; (featurep 'xemacs)
  901. )
  902. (if (viper-characterp basis)
  903. (setq basis
  904. (if (viper= basis ?\C-?)
  905. (list 'control '\?) ; taking care of an emacs bug
  906. (intern (char-to-string basis)))))
  907. (if mod
  908. (append mod (list basis))
  909. basis))))
  910. (defun viper-last-command-char ()
  911. (if (featurep 'xemacs)
  912. (event-to-character last-command-event)
  913. last-command-event))
  914. (defun viper-key-to-emacs-key (key)
  915. (let (key-name char-p modifiers mod-char-list base-key base-key-name)
  916. (cond ((featurep 'xemacs) key)
  917. ((symbolp key)
  918. (setq key-name (symbol-name key))
  919. (cond ((= (length key-name) 1) ; character event
  920. (string-to-char key-name))
  921. ;; Emacs doesn't recognize `return' and `escape' as events on
  922. ;; dumb terminals, so we translate them into characters
  923. ((and (featurep 'emacs) (not (viper-window-display-p))
  924. (string= key-name "return"))
  925. ?\C-m)
  926. ((and (featurep 'emacs) (not (viper-window-display-p))
  927. (string= key-name "escape"))
  928. ?\e)
  929. ;; pass symbol-event as is
  930. (t key)))
  931. ((listp key)
  932. (setq modifiers (viper-subseq key 0 (1- (length key)))
  933. base-key (viper-seq-last-elt key)
  934. base-key-name (symbol-name base-key)
  935. char-p (= (length base-key-name) 1))
  936. (setq mod-char-list
  937. (mapcar
  938. (lambda (elt) (upcase (substring (symbol-name elt) 0 1)))
  939. modifiers))
  940. (if char-p
  941. (setq key-name
  942. (car (read-from-string
  943. (concat
  944. "?\\"
  945. (mapconcat 'identity mod-char-list "-\\")
  946. "-"
  947. base-key-name))))
  948. (setq key-name
  949. (intern
  950. (concat
  951. (mapconcat 'identity mod-char-list "-")
  952. "-"
  953. base-key-name))))))
  954. ))
  955. ;; LIS is assumed to be a list of events of characters
  956. (defun viper-eventify-list-xemacs (lis)
  957. (if (featurep 'xemacs)
  958. (mapcar
  959. (lambda (elt)
  960. (cond ((viper-characterp elt) (character-to-event elt))
  961. ((eventp elt) elt)
  962. (t (error
  963. "viper-eventify-list-xemacs: can't convert to event, %S"
  964. elt))))
  965. lis)))
  966. ;; Smooths out the difference between Emacs's unread-command-events
  967. ;; and XEmacs unread-command-event. Arg is a character, an event, a list of
  968. ;; events or a sequence of keys.
  969. ;;
  970. ;; Due to the way unread-command-events in Emacs (not XEmacs), a non-event
  971. ;; symbol in unread-command-events list may cause Emacs to turn this symbol
  972. ;; into an event. Below, we delete nil from event lists, since nil is the most
  973. ;; common symbol that might appear in this wrong context.
  974. (defun viper-set-unread-command-events (arg)
  975. (if (featurep 'emacs)
  976. (setq
  977. unread-command-events
  978. (let ((new-events
  979. (cond ((eventp arg) (list arg))
  980. ((listp arg) arg)
  981. ((sequencep arg)
  982. (listify-key-sequence arg))
  983. (t (error
  984. "viper-set-unread-command-events: Invalid argument, %S"
  985. arg)))))
  986. (if (not (eventp nil))
  987. (setq new-events (delq nil new-events)))
  988. (append new-events unread-command-events)))
  989. ;; XEmacs
  990. (setq
  991. unread-command-events
  992. (append
  993. (cond ((viper-characterp arg) (list (character-to-event arg)))
  994. ((eventp arg) (list arg))
  995. ((stringp arg) (mapcar 'character-to-event arg))
  996. ((vectorp arg) (append arg nil)) ; turn into list
  997. ((listp arg) (viper-eventify-list-xemacs arg))
  998. (t (error
  999. "viper-set-unread-command-events: Invalid argument, %S" arg)))
  1000. unread-command-events))))
  1001. ;; Check if vec is a vector of key-press events representing characters
  1002. ;; XEmacs only
  1003. (defun viper-event-vector-p (vec)
  1004. (and (vectorp vec)
  1005. (eval (cons 'and (mapcar (lambda (elt) (if (eventp elt) t)) vec)))))
  1006. ;; check if vec is a vector of character symbols
  1007. (defun viper-char-symbol-sequence-p (vec)
  1008. (and
  1009. (sequencep vec)
  1010. (eval
  1011. (cons 'and
  1012. (mapcar (lambda (elt)
  1013. (and (symbolp elt) (= (length (symbol-name elt)) 1)))
  1014. vec)))))
  1015. (defun viper-char-array-p (array)
  1016. (eval (cons 'and (mapcar 'viper-characterp array))))
  1017. ;; Args can be a sequence of events, a string, or a Viper macro. Will try to
  1018. ;; convert events to keys and, if all keys are regular printable
  1019. ;; characters, will return a string. Otherwise, will return a string
  1020. ;; representing a vector of converted events. If the input was a Viper macro,
  1021. ;; will return a string that represents this macro as a vector.
  1022. (defun viper-array-to-string (event-seq)
  1023. (let (temp temp2)
  1024. (cond ((stringp event-seq) event-seq)
  1025. ((viper-event-vector-p event-seq)
  1026. (setq temp (mapcar 'viper-event-key event-seq))
  1027. (cond ((viper-char-symbol-sequence-p temp)
  1028. (mapconcat 'symbol-name temp ""))
  1029. ((and (viper-char-array-p
  1030. (setq temp2 (mapcar 'viper-key-to-character temp))))
  1031. (mapconcat 'char-to-string temp2 ""))
  1032. (t (prin1-to-string (vconcat temp)))))
  1033. ((viper-char-symbol-sequence-p event-seq)
  1034. (mapconcat 'symbol-name event-seq ""))
  1035. ((and (vectorp event-seq)
  1036. (viper-char-array-p
  1037. (setq temp (mapcar 'viper-key-to-character event-seq))))
  1038. (mapconcat 'char-to-string temp ""))
  1039. (t (prin1-to-string event-seq)))))
  1040. (defun viper-key-press-events-to-chars (events)
  1041. (mapconcat (if (featurep 'xemacs)
  1042. (lambda (elt) (char-to-string (event-to-character elt))) ; xemacs
  1043. 'char-to-string ; emacs
  1044. )
  1045. events
  1046. ""))
  1047. (defun viper-read-char-exclusive ()
  1048. (let (char
  1049. (echo-keystrokes 1))
  1050. (while (null char)
  1051. (condition-case nil
  1052. (setq char (read-char))
  1053. (error
  1054. ;; skip event if not char
  1055. (viper-read-event))))
  1056. char))
  1057. ;; key is supposed to be in viper's representation, e.g., (control l), a
  1058. ;; character, etc.
  1059. (defun viper-key-to-character (key)
  1060. (cond ((eq key 'space) ?\ )
  1061. ((eq key 'delete) ?\C-?)
  1062. ((eq key 'return) ?\C-m)
  1063. ((eq key 'backspace) ?\C-h)
  1064. ((and (symbolp key)
  1065. (= 1 (length (symbol-name key))))
  1066. (string-to-char (symbol-name key)))
  1067. ((and (listp key)
  1068. (eq (car key) 'control)
  1069. (symbol-name (nth 1 key))
  1070. (= 1 (length (symbol-name (nth 1 key)))))
  1071. (read (format "?\\C-%s" (symbol-name (nth 1 key)))))
  1072. (t key)))
  1073. (defun viper-setup-master-buffer (&rest other-files-or-buffers)
  1074. "Set up the current buffer as a master buffer.
  1075. Arguments become related buffers. This function should normally be used in
  1076. the `Local variables' section of a file."
  1077. (setq viper-related-files-and-buffers-ring
  1078. (make-ring (1+ (length other-files-or-buffers))))
  1079. (mapc (lambda (elt)
  1080. (viper-ring-insert viper-related-files-and-buffers-ring elt))
  1081. other-files-or-buffers)
  1082. (viper-ring-insert viper-related-files-and-buffers-ring (buffer-name))
  1083. )
  1084. ;;; Movement utilities
  1085. ;; Characters that should not be considered as part of the word, in reformed-vi
  1086. ;; syntax mode.
  1087. ;; Note: \\ (quoted \) must appear before `-' because this string is listified
  1088. ;; into characters at some point and then put back to string. The result is
  1089. ;; used in skip-chars-forward, which treats - specially. Here we achieve the
  1090. ;; effect of quoting - and preventing it from being special.
  1091. (defconst viper-non-word-characters-reformed-vi
  1092. "!@#$%^&*()\\-+=|\\~`{}[];:'\",<.>/?")
  1093. ;; These are characters that are not to be considered as parts of a word in
  1094. ;; Viper.
  1095. ;; Set each time state changes and at loading time
  1096. (viper-deflocalvar viper-non-word-characters nil)
  1097. ;; must be buffer-local
  1098. (viper-deflocalvar viper-ALPHA-char-class "w"
  1099. "String of syntax classes characterizing Viper's alphanumeric symbols.
  1100. In addition, the symbol `_' may be considered alphanumeric if
  1101. `viper-syntax-preference' is `strict-vi' or `reformed-vi'.")
  1102. (defconst viper-strict-ALPHA-chars "a-zA-Z0-9_"
  1103. "Regexp matching the set of alphanumeric characters acceptable to strict
  1104. Vi.")
  1105. (defconst viper-strict-SEP-chars " \t\n"
  1106. "Regexp matching the set of alphanumeric characters acceptable to strict
  1107. Vi.")
  1108. (defconst viper-strict-SEP-chars-sans-newline " \t"
  1109. "Regexp matching the set of alphanumeric characters acceptable to strict
  1110. Vi.")
  1111. (defconst viper-SEP-char-class " -"
  1112. "String of syntax classes for Vi separators.
  1113. Usually contains ` ', linefeed, TAB or formfeed.")
  1114. ;; Set Viper syntax classes and related variables according to
  1115. ;; `viper-syntax-preference'.
  1116. (defun viper-update-syntax-classes (&optional set-default)
  1117. (let ((preference (cond ((eq viper-syntax-preference 'emacs)
  1118. "w") ; Viper words have only Emacs word chars
  1119. ((eq viper-syntax-preference 'extended)
  1120. "w_") ; Viper words have Emacs word & symbol chars
  1121. (t "w"))) ; Viper words are Emacs words plus `_'
  1122. (non-word-chars (cond ((eq viper-syntax-preference 'reformed-vi)
  1123. (viper-string-to-list
  1124. viper-non-word-characters-reformed-vi))
  1125. (t nil))))
  1126. (if set-default
  1127. (setq-default viper-ALPHA-char-class preference
  1128. viper-non-word-characters non-word-chars)
  1129. (setq viper-ALPHA-char-class preference
  1130. viper-non-word-characters non-word-chars))
  1131. ))
  1132. ;; SYMBOL is used because customize requires it, but it is ignored, unless it
  1133. ;; is nil. If nil, use setq.
  1134. (defun viper-set-syntax-preference (&optional symbol value)
  1135. "Set Viper syntax preference.
  1136. If called interactively or if SYMBOL is nil, sets syntax preference in current
  1137. buffer. If called non-interactively, preferably via the customization widget,
  1138. sets the default value."
  1139. (interactive)
  1140. (or value
  1141. (setq value
  1142. (completing-read
  1143. "Viper syntax preference: "
  1144. '(("strict-vi") ("reformed-vi") ("extended") ("emacs"))
  1145. nil 'require-match)))
  1146. (if (stringp value) (setq value (intern value)))
  1147. (or (memq value '(strict-vi reformed-vi extended emacs))
  1148. (error "Invalid Viper syntax preference, %S" value))
  1149. (if symbol
  1150. (setq-default viper-syntax-preference value)
  1151. (setq viper-syntax-preference value))
  1152. (viper-update-syntax-classes))
  1153. (defcustom viper-syntax-preference 'reformed-vi
  1154. "Syntax type characterizing Viper's alphanumeric symbols.
  1155. Affects movement and change commands that deal with Vi-style words.
  1156. Works best when set in the hooks to various major modes.
  1157. `strict-vi' means Viper words are (hopefully) exactly as in Vi.
  1158. `reformed-vi' means Viper words are like Emacs words \(as determined using
  1159. Emacs syntax tables, which are different for different major modes) with two
  1160. exceptions: the symbol `_' is always part of a word and typical Vi non-word
  1161. symbols like `\\=`', `\\='', `:', `\"', `)', and `{' are excluded.
  1162. This behaves very close to `strict-vi', but also works well with non-ASCII
  1163. characters from various alphabets.
  1164. `extended' means Viper word constituents are symbols that are marked as being
  1165. parts of words OR symbols in Emacs syntax tables.
  1166. This is most appropriate for major modes intended for editing programs.
  1167. `emacs' means Viper words are the same as Emacs words as specified by Emacs
  1168. syntax tables.
  1169. This option is appropriate if you like Emacs-style words."
  1170. :type '(radio (const strict-vi) (const reformed-vi)
  1171. (const extended) (const emacs))
  1172. :set 'viper-set-syntax-preference
  1173. :group 'viper)
  1174. (make-variable-buffer-local 'viper-syntax-preference)
  1175. ;; addl-chars are characters to be temporarily considered as alphanumerical
  1176. (defun viper-looking-at-alpha (&optional addl-chars)
  1177. (or (stringp addl-chars) (setq addl-chars ""))
  1178. (if (eq viper-syntax-preference 'reformed-vi)
  1179. (setq addl-chars (concat addl-chars "_")))
  1180. (let ((char (char-after (point))))
  1181. (if char
  1182. (if (eq viper-syntax-preference 'strict-vi)
  1183. (looking-at (concat "[" viper-strict-ALPHA-chars addl-chars "]"))
  1184. (or
  1185. ;; or one of the additional chars being asked to include
  1186. (viper-memq-char char (viper-string-to-list addl-chars))
  1187. (and
  1188. ;; not one of the excluded word chars (note:
  1189. ;; viper-non-word-characters is a list)
  1190. (not (viper-memq-char char viper-non-word-characters))
  1191. ;; char of the Viper-word syntax class
  1192. (viper-memq-char (char-syntax char)
  1193. (viper-string-to-list viper-ALPHA-char-class))))))
  1194. ))
  1195. (defun viper-looking-at-separator ()
  1196. (let ((char (char-after (point))))
  1197. (if char
  1198. (if (eq viper-syntax-preference 'strict-vi)
  1199. (viper-memq-char char (viper-string-to-list viper-strict-SEP-chars))
  1200. (or (eq char ?\n) ; RET is always a separator in Vi
  1201. (viper-memq-char (char-syntax char)
  1202. (viper-string-to-list viper-SEP-char-class)))))
  1203. ))
  1204. (defsubst viper-looking-at-alphasep (&optional addl-chars)
  1205. (or (viper-looking-at-separator) (viper-looking-at-alpha addl-chars)))
  1206. (defun viper-skip-alpha-forward (&optional addl-chars)
  1207. (or (stringp addl-chars) (setq addl-chars ""))
  1208. (viper-skip-syntax
  1209. 'forward
  1210. (cond ((eq viper-syntax-preference 'strict-vi)
  1211. "")
  1212. (t viper-ALPHA-char-class))
  1213. (cond ((eq viper-syntax-preference 'strict-vi)
  1214. (concat viper-strict-ALPHA-chars addl-chars))
  1215. (t addl-chars))))
  1216. (defun viper-skip-alpha-backward (&optional addl-chars)
  1217. (or (stringp addl-chars) (setq addl-chars ""))
  1218. (viper-skip-syntax
  1219. 'backward
  1220. (cond ((eq viper-syntax-preference 'strict-vi)
  1221. "")
  1222. (t viper-ALPHA-char-class))
  1223. (cond ((eq viper-syntax-preference 'strict-vi)
  1224. (concat viper-strict-ALPHA-chars addl-chars))
  1225. (t addl-chars))))
  1226. ;; weird syntax tables may confuse strict-vi style
  1227. (defsubst viper-skip-all-separators-forward (&optional within-line)
  1228. (if (eq viper-syntax-preference 'strict-vi)
  1229. (if within-line
  1230. (skip-chars-forward viper-strict-SEP-chars-sans-newline)
  1231. (skip-chars-forward viper-strict-SEP-chars))
  1232. (viper-skip-syntax 'forward
  1233. viper-SEP-char-class
  1234. (or within-line "\n")
  1235. (if within-line (viper-line-pos 'end)))))
  1236. (defsubst viper-skip-all-separators-backward (&optional within-line)
  1237. (if (eq viper-syntax-preference 'strict-vi)
  1238. (if within-line
  1239. (skip-chars-backward viper-strict-SEP-chars-sans-newline)
  1240. (skip-chars-backward viper-strict-SEP-chars))
  1241. (viper-skip-syntax 'backward
  1242. viper-SEP-char-class
  1243. (or within-line "\n")
  1244. (if within-line (viper-line-pos 'start)))))
  1245. (defun viper-skip-nonseparators (direction)
  1246. (viper-skip-syntax
  1247. direction
  1248. (concat "^" viper-SEP-char-class)
  1249. nil
  1250. (viper-line-pos (if (eq direction 'forward) 'end 'start))))
  1251. ;; skip over non-word constituents and non-separators
  1252. (defun viper-skip-nonalphasep-forward ()
  1253. (if (eq viper-syntax-preference 'strict-vi)
  1254. (skip-chars-forward
  1255. (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars))
  1256. (viper-skip-syntax
  1257. 'forward
  1258. (concat "^" viper-ALPHA-char-class viper-SEP-char-class)
  1259. ;; Emacs may consider some of these as words, but we don't want them
  1260. viper-non-word-characters
  1261. (viper-line-pos 'end))))
  1262. (defun viper-skip-nonalphasep-backward ()
  1263. (if (eq viper-syntax-preference 'strict-vi)
  1264. (skip-chars-backward
  1265. (concat "^" viper-strict-SEP-chars viper-strict-ALPHA-chars))
  1266. (viper-skip-syntax
  1267. 'backward
  1268. (concat "^" viper-ALPHA-char-class viper-SEP-char-class)
  1269. ;; Emacs may consider some of these as words, but we don't want them
  1270. viper-non-word-characters
  1271. (viper-line-pos 'start))))
  1272. ;; Skip SYNTAX like skip-syntax-* and ADDL-CHARS like skip-chars-*
  1273. ;; Return the number of chars traveled.
  1274. ;; Both SYNTAX or ADDL-CHARS can be strings or lists of characters.
  1275. ;; When SYNTAX is "w", then viper-non-word-characters are not considered to be
  1276. ;; words, even if Emacs syntax table says they are.
  1277. (defun viper-skip-syntax (direction syntax addl-chars &optional limit)
  1278. (let ((total 0)
  1279. (local 1)
  1280. (skip-chars-func
  1281. (if (eq direction 'forward)
  1282. 'skip-chars-forward 'skip-chars-backward))
  1283. (skip-syntax-func
  1284. (if (eq direction 'forward)
  1285. 'viper-forward-char-carefully 'viper-backward-char-carefully))
  1286. char-looked-at syntax-of-char-looked-at negated-syntax)
  1287. (setq addl-chars
  1288. (cond ((listp addl-chars) (viper-charlist-to-string addl-chars))
  1289. ((stringp addl-chars) addl-chars)
  1290. (t "")))
  1291. (setq syntax
  1292. (cond ((listp syntax) syntax)
  1293. ((stringp syntax) (viper-string-to-list syntax))
  1294. (t nil)))
  1295. (if (memq ?^ syntax) (setq negated-syntax t))
  1296. (while (and (not (= local 0))
  1297. (cond ((eq direction 'forward)
  1298. (not (eobp)))
  1299. (t (not (bobp)))))
  1300. (setq char-looked-at (viper-char-at-pos direction)
  1301. ;; if outside the range, set to nil
  1302. syntax-of-char-looked-at (if char-looked-at
  1303. (char-syntax char-looked-at)))
  1304. (setq local
  1305. (+ (if (and
  1306. (cond ((and limit (eq direction 'forward))
  1307. (< (point) limit))
  1308. (limit ; backward & limit
  1309. (> (point) limit))
  1310. (t t)) ; no limit
  1311. ;; char under/before cursor has appropriate syntax
  1312. (if negated-syntax
  1313. (not (memq syntax-of-char-looked-at syntax))
  1314. (memq syntax-of-char-looked-at syntax))
  1315. ;; if char-syntax class is "word", make sure it is not one
  1316. ;; of the excluded characters
  1317. (if (and (eq syntax-of-char-looked-at ?w)
  1318. (not negated-syntax))
  1319. (not (viper-memq-char
  1320. char-looked-at viper-non-word-characters))
  1321. t))
  1322. (funcall skip-syntax-func 1)
  1323. 0)
  1324. (funcall skip-chars-func addl-chars limit)))
  1325. (setq total (+ total local)))
  1326. total
  1327. ))
  1328. ;; tells when point is at the beginning of field
  1329. (defun viper-beginning-of-field ()
  1330. (or (bobp)
  1331. (not (eq (get-char-property (point) 'field)
  1332. (get-char-property (1- (point)) 'field)))))
  1333. ;; this is copied from cl-extra.el
  1334. ;; Return the subsequence of SEQ from START to END.
  1335. ;; If END is omitted, it defaults to the length of the sequence.
  1336. ;; If START or END is negative, it counts from the end.
  1337. (defun viper-subseq (seq start &optional end)
  1338. (if (stringp seq) (substring seq start end)
  1339. (let (len)
  1340. (and end (< end 0) (setq end (+ end (setq len (length seq)))))
  1341. (if (< start 0) (setq start (+ start (or len (setq len (length seq))))))
  1342. (cond ((listp seq)
  1343. (if (> start 0) (setq seq (nthcdr start seq)))
  1344. (if end
  1345. (let ((res nil))
  1346. (while (>= (setq end (1- end)) start)
  1347. (push (pop seq) res))
  1348. (nreverse res))
  1349. (copy-sequence seq)))
  1350. (t
  1351. (or end (setq end (or len (length seq))))
  1352. (let ((res (make-vector (max (- end start) 0) nil))
  1353. (i 0))
  1354. (while (< start end)
  1355. (aset res i (aref seq start))
  1356. (setq i (1+ i) start (1+ start)))
  1357. res))))))
  1358. ;; Local Variables:
  1359. ;; eval: (put 'viper-deflocalvar 'lisp-indent-hook 'defun)
  1360. ;; End:
  1361. ;;; viper-util.el ends here