Skip to content

Commit 43b4967

Browse files
authored
Merge pull request #1390 from ychin/fix-macos-13-ventura-printing
Fix :hardcopy not working in macOS 13 Ventura
2 parents 01bcda7 + e3a5374 commit 43b4967

File tree

4 files changed

+54
-8
lines changed

4 files changed

+54
-8
lines changed

runtime/autoload/macvim.vim

+41-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
vim9script
22
# Support scripts for MacVim-specific functionality
33
# Maintainer: Yee Cheng Chin (macvim-dev@macvim.org)
4-
# Last Change: 2022-10-14
4+
# Last Change: 2023-03-15
55

66
# Retrieves the text under the selection, without polluting the registers.
77
# This is easier if we could yank, but we don't know what the user has been
@@ -76,4 +76,44 @@ export def ShowDefinitionUnderCursor()
7676
endif
7777
enddef
7878

79+
# Print functionality. We simply show the file in Preview and let the user
80+
# decide what to do. This allows for more control instead of immediately
81+
# piping the file to lpr which will actually print the file.
82+
#
83+
# PreviewConvertPostScript:
84+
# Convert the provided PostScript file to PDF, then show in Preview. This is
85+
# necessary in macOS 13+ as Preview doesn't support .ps files anymore.
86+
# PreviewPostScript:
87+
# Directly open PostScript file in Preview. Can use this if
88+
# PreviewConvertPostScript doesn't work.
89+
export def PreviewConvertPostScript(deltimer = 10000): number
90+
# Convert PS to PDF because Preview can't use PS files in macOS 13+
91+
system($"pstopdf {v:fname_in} -o {v:fname_in}.pdf")
92+
if v:shell_error != 0
93+
return v:shell_error
94+
endif
95+
system($"open -a Preview {v:fname_in}.pdf")
96+
delete(v:fname_in)
97+
98+
# Delete the file after it's opened in Preview for privacy. We don't have an
99+
# easy way to detect that Preview has opened the file already, so we just
100+
# use a generous 10 secs timer.
101+
# Note that we can't use `open -W` instead because 1) it will block
102+
# synchronously, and 2) it will only return if Preview.app has closed, which
103+
# may not happen for a while if it has other unrelated documents opened.
104+
var to_delete_file = $"{v:fname_in}.pdf"
105+
timer_start(deltimer, (timer) => delete(to_delete_file))
106+
107+
return v:shell_error
108+
enddef
109+
110+
export def PreviewPostScript(deltimer = 10000): number
111+
system($"open -a Preview {v:fname_in}")
112+
113+
var to_delete_file = v:fname_in
114+
timer_start(deltimer, (timer) => delete(to_delete_file))
115+
116+
return v:shell_error
117+
enddef
118+
79119
# vim: set sw=2 ts=2 et :

runtime/doc/gui_mac.txt

+2-2
Original file line numberDiff line numberDiff line change
@@ -930,8 +930,8 @@ prominent bugs/missing features.
930930
- Sometimes multibyte characters look "too wide", i.e. they overlap the
931931
following character. It might help to change 'ambiwidth', or override the
932932
automatic font substitution by setting 'guifontwide' manually.
933-
- Printing. As a temporary solution <D-p> creates a PostScript file which is
934-
then opened in Preview where it may be printed. See |hardcopy|.
933+
- Built-in printing. |:hardcopy| / <D-p> creates a PDF file which is then
934+
opened in Preview where it may be printed. See |pexpr-option|.
935935

936936
General bugs and issues are tracked on Github. If you find new bugs or have
937937
feature requests then please file an issue there:

runtime/doc/print.txt

+9-2
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@ Note: If you have problems printing with |:hardcopy|, an alternative is to use
4747
paper size, duplex, etc.
4848
Note: If you want PDF, there are tools such as
4949
"ps2pdf" that can convert the PostScript to PDF.
50+
On macOS, you can also use "pstopdf" which comes
51+
bundled with the system.
5052

5153
:[range]ha[rdcopy][!] >{filename}
5254
As above, but write the resulting PostScript in file
@@ -136,8 +138,13 @@ The arguments to the ":hardcopy" command are in |v:cmdarg|.
136138
The expression must take care of deleting the file after printing it.
137139
When there is an error, the expression must return a non-zero number.
138140
If there is no error, return zero or an empty string.
139-
The default for non MS-Windows or VMS systems is to simply use "lpr" to print
140-
the file: >
141+
142+
The default for MacVim is to convert the PostScript file to PDF and then open
143+
it in Preview.app where you can print from there, using the function
144+
`macvim#PreviewConvertPostScript()`.
145+
146+
The default for other non MS-Windows or VMS systems is to simply use "lpr" to
147+
print the file: >
141148
142149
system('lpr' .. (&printdevice == '' ? '' : ' -P' .. &printdevice)
143150
.. ' ' .. v:fname_in) .. delete(v:fname_in) + v:shell_error

src/MacVim/gvimrc

+2-3
Original file line numberDiff line numberDiff line change
@@ -32,9 +32,8 @@ if empty(&guitablabel)
3232
set guitablabel=%M%t
3333
endif
3434

35-
" Send print jobs to Preview.app. This does not delete the temporary ps file
36-
" that is generated by :hardcopy.
37-
set printexpr=system('open\ -a\ Preview\ '.v:fname_in)\ +\ v:shell_error
35+
" Send print jobs to Preview.app. The user can then print from it.
36+
set printexpr=macvim#PreviewConvertPostScript()
3837

3938
" askpass
4039
let $SSH_ASKPASS = simplify($VIM . '/../../Resources') . '/macvim-askpass'

0 commit comments

Comments
 (0)